feat: reorganize the code

This commit is contained in:
mcolonna 2024-04-02 14:45:03 +02:00
parent a00a02e527
commit 5103cbf709
40 changed files with 326 additions and 325 deletions

View file

@ -6,16 +6,16 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 16:27:06 by mcolonna #+# #+# */
/* Updated: 2024/03/11 15:54:15 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 17:27:27 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "data_assets.h"
#include "includes.h"
/**
* Path of each asset relative to assets/, without extension.
*/
const t_assetinfo g_assetsmap[NB_ASSETS] = {
const t_assetmeta g_assetsmap[NB_ASSETS] = {
{"floor/1", 50, 100, 50, 100, true},
{"floor/2", 50, 100, 50, 100, true},
// WALL

View file

@ -6,13 +6,13 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/11 16:02:29 by mcolonna #+# #+# */
/* Updated: 2024/04/02 12:39:30 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 17:37:34 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "data_sprites.h"
#include "includes.h"
const t_spriteinfo g_spritesmap[NB_SPRITES] = {
const t_spritemeta g_spritesmap[NB_SPRITES] = {
{WALL_BORDER, 1, 50, 50, ANIMATION},
{FLOOR_1, 1, 50, 50, ANIMATION},
{FLOOR_2, 1, 50, 50, ANIMATION},

View file

@ -6,17 +6,12 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 15:05:30 by mcolonna #+# #+# */
/* Updated: 2024/03/11 13:03:37 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 18:03:21 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "display.h"
#include "includes.h"
#include "display_utils.h"
#include "env.h"
#include "error.h"
#include "mlx.h"
#include "libtf.h"
#include <stdlib.h>
void display_init(void)
{
@ -30,7 +25,7 @@ void display_init(void)
}
g_screenbuf.img = mlx_new_image(g_env.mlx, WINDOW_WIDTH, WINDOW_HEIGHT);
if (!g_screenbuf.img)
err("on mlx_new_image()");
error_err("on mlx_new_image()");
g_screenbuf.width = WINDOW_WIDTH;
g_screenbuf.height = WINDOW_HEIGHT;
g_screenbuf.data = get_data_addr(g_screenbuf.img);

View file

@ -6,17 +6,12 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 15:05:30 by mcolonna #+# #+# */
/* Updated: 2024/03/06 13:40:18 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 17:43:34 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "display.h"
#include "includes.h"
#include "display_utils.h"
#include "env.h"
#include "error.h"
#include "mlx.h"
#include "libtf.h"
#include <stdlib.h>
static inline void add_color(t_pixel *dest, t_pixel src)
{
@ -98,7 +93,7 @@ static inline void display_draw_opaque(int x, int y, t_image img)
}
}
void display_draw(int x, int y, t_assetid asset)
void display_draw(int x, int y, t_assetsmap_id asset)
{
const t_image img = g_allassets[asset];

View file

@ -6,14 +6,11 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/06 13:03:30 by mcolonna #+# #+# */
/* Updated: 2024/03/06 13:48:02 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 18:03:15 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "display_utils.h"
#include "mlx.h"
#include "error.h"
#include "env.h"
#include "includes.h"
t_image g_allassets[NB_ASSETS];
t_image g_screenbuf;
@ -27,7 +24,7 @@ t_pixel *get_data_addr(void *img)
r = (t_pixel *)mlx_get_data_addr(img, &bpp, &size_line, &endian);
if (!r || bpp != 32 || endian != 0)
err("wrong image format");
error_err("wrong image format");
return (r);
}
@ -50,17 +47,17 @@ void load_xpm_file_with_alpha(int i)
t_string src_mask;
void *mask;
tmp = str_join(err, g_env.mc, "assets/", g_assetsmap[i].src);
src = str_join(err, g_env.mc, tmp, ".xpm");
src_mask = str_join(err, g_env.mc, tmp, ".alpha.xpm");
tmp = str_join(error_err, g_env.mc, "assets/", g_assetsmap[i].src);
src = str_join(error_err, g_env.mc, tmp, ".xpm");
src_mask = str_join(error_err, g_env.mc, tmp, ".alpha.xpm");
g_allassets[i].img = mlx_xpm_file_to_image(g_env.mlx, src,
&(g_allassets[i].width), &g_allassets[i].height);
if (!g_allassets[i].img)
err("image loading failed");
error_err("image loading failed");
mask = mlx_xpm_file_to_image(g_env.mlx, src_mask,
&(g_allassets[i].width), &g_allassets[i].height);
if (!mask)
err("image loading failed");
error_err("image loading failed");
apply_mask_to_image(g_allassets[i].img, mask,
g_allassets[i].width * g_allassets[i].height);
mlx_destroy_image(g_env.mlx, mask);

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* display_utils.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/06 12:59:24 by mcolonna #+# #+# */
/* Updated: 2024/04/02 14:39:19 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef DISPLAY_UTILS_H
# define DISPLAY_UTILS_H
extern t_image g_allassets[NB_ASSETS];
extern t_image g_screenbuf;
typedef struct s_imglimits
{
int xmin;
int xmax;
int ymin;
int ymax;
} t_imglimits;
t_pixel *get_data_addr(void *img);
void apply_mask_to_image(void *img, void *mask, int nb_px);
void load_xpm_file_with_alpha(int i);
#endif

View file

@ -6,19 +6,17 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 22:28:52 by mcolonna #+# #+# */
/* Updated: 2024/03/28 16:02:01 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 18:02:35 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "sprite.h"
#include "display.h"
#include "error.h"
#include "includes.h"
t_sprite sprite_init(t_spriteid id)
t_sprite sprite_init(t_spritesmap_id id)
{
t_sprite r;
r.info = (t_spriteinfo *)(g_spritesmap + id);
r.meta = (t_spritemeta *)(g_spritesmap + id);
r.index = 0;
return (r);
}
@ -26,19 +24,19 @@ t_sprite sprite_init(t_spriteid id)
void sprite_draw(int x, int y, t_sprite *spr)
{
display_draw(
x - spr->info->origin_x, y - spr->info->origin_y,
spr->info->first_frame + spr->index);
if (spr->info->type == ANIMATION)
x - spr->meta->origin_x, y - spr->meta->origin_y,
spr->meta->first_frame + spr->index);
if (spr->meta->type == ANIMATION)
{
spr->index++;
if (spr->index >= spr->info->nb_frames)
if (spr->index >= spr->meta->nb_frames)
spr->index = 0;
}
}
void sprite_character_set_direction(t_sprite *spr, t_direction value)
{
if (spr->info->type != CHARACTER_STILL)
err("the sprite must be a CHARACTER_STILL sprite.");
if (spr->meta->type != CHARACTER_STILL)
error_err("the sprite must be a CHARACTER_STILL sprite.");
spr->index = value;
}

View file

@ -6,10 +6,10 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 16:32:36 by mcolonna #+# #+# */
/* Updated: 2024/02/27 16:42:22 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 14:41:12 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "env.h"
#include "includes.h"
t_env g_env;

View file

@ -6,17 +6,11 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 16:05:21 by mcolonna #+# #+# */
/* Updated: 2024/03/11 13:37:12 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 17:56:31 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "error.h"
#include "libtf.h"
#include "display.h"
#include "env.h"
#include "includes.h"
static void end(int no)
{
@ -25,7 +19,7 @@ static void end(int no)
exit(no);
}
void err(t_const_string msg)
void error_err(t_const_string msg)
{
print_line(err_remember, 2, "Error");
if (str_eq(msg, "errno"))
@ -35,7 +29,7 @@ void err(t_const_string msg)
end(1);
}
void err_perror(t_const_string s)
void error_perror(t_const_string s)
{
print_line(err_remember, 2, "Error");
print_str(err_remember, 2, s);
@ -44,7 +38,7 @@ void err_perror(t_const_string s)
end(1);
}
void err_perror_str(t_const_string s, t_const_string msg)
void error_str(t_const_string s, t_const_string msg)
{
print_line(err_remember, 2, "Error");
print_str(err_remember, 2, s);
@ -53,7 +47,7 @@ void err_perror_str(t_const_string s, t_const_string msg)
end(1);
}
void finish(void)
void success(void)
{
end(0);
}

View file

@ -6,14 +6,11 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/26 12:26:17 by mcolonna #+# #+# */
/* Updated: 2024/03/29 18:32:27 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 14:41:14 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "error.h"
#include "libtf.h"
#include "env.h"
#include <X11/keysym.h>
#include "includes.h"
static int g_input_i = 0;
static bool g_to_unpress[4] = {false, false, false, false};

View file

@ -6,19 +6,11 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 15:28:34 by mcolonna #+# #+# */
/* Updated: 2024/03/29 18:33:10 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 18:04:18 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "mlx.h"
#include "env.h"
#include "error.h"
#include "display.h"
#include "data_assets.h"
#include "room.h"
#include "input.h"
#include "timedloop.h"
#include <X11/Xlib.h>
#include "includes.h"
int expose_hook(void)
{
@ -29,7 +21,7 @@ int expose_hook(void)
int close_hook(void)
{
mlx_do_key_autorepeaton(g_env.mlx);
finish();
success();
return (0);
}
@ -50,15 +42,15 @@ int loop_hook(void)
int main(void)
{
g_env.mc = mem_newclass(err);
g_env.mc = mem_newclass(error_err);
g_env.mlx = mlx_init();
if (!g_env.mlx)
err("mlx_init() failed");
error_err("mlx_init() failed");
display_init();
g_env.win = mlx_new_window(g_env.mlx, WINDOW_WIDTH, WINDOW_HEIGHT,
WINDOW_TITLE);
if (!g_env.win)
err("mlx_new_window() failed");
error_err("mlx_new_window() failed");
g_env.room = room_fromfile("room/room.ber");
room_draw(g_env.room);
mlx_expose_hook(g_env.win, expose_hook, NULL);

View file

@ -6,12 +6,11 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/26 14:54:49 by mcolonna #+# #+# */
/* Updated: 2024/03/26 15:51:04 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 14:41:31 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include <time.h>
#include "env.h"
#include "includes.h"
static long get_nanos(void)
{

View file

@ -6,19 +6,15 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 15:27:03 by mcolonna #+# #+# */
/* Updated: 2024/03/29 15:51:38 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 17:35:56 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "object.h"
#include "error.h"
#include "sprite.h"
#include "libtf.h"
#include "env.h"
#include "includes.h"
static void character_initstate(t_character *character)
{
const t_spriteid walk_sprites[] = {
const t_spritesmap_id walk_sprites[] = {
character->sprites->walk_down,
character->sprites->walk_up,
character->sprites->walk_left,

View file

@ -6,15 +6,11 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 15:27:03 by mcolonna #+# #+# */
/* Updated: 2024/03/29 18:29:12 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 18:02:48 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "object.h"
#include "error.h"
#include "sprite.h"
#include "libtf.h"
#include "env.h"
#include "includes.h"
static t_direction snas_brain(void)
{
@ -55,7 +51,7 @@ t_object snas_init(t_memclass mc)
t_snas_data *data;
r.type = type;
data = mem_alloc(err, mc, sizeof(t_snas_data));
data = mem_alloc(error_err, mc, sizeof(t_snas_data));
data->character = character_init((t_character_sprites *)&sprites);
r.data = data;
r.solid = true;

View file

@ -6,15 +6,11 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 15:27:03 by mcolonna #+# #+# */
/* Updated: 2024/03/29 15:52:32 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 18:02:25 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "object.h"
#include "error.h"
#include "sprite.h"
#include "libtf.h"
#include "env.h"
#include "includes.h"
static t_point wall_loop(t_object *obj, t_point pos)
{
@ -36,7 +32,7 @@ t_object wall_init(t_memclass mc)
t_wall_data *data;
r.type = type;
data = mem_alloc(err, mc, sizeof(t_wall_data));
data = mem_alloc(error_err, mc, sizeof(t_wall_data));
data->spr = sprite_init(SPR_WALL);
r.data = data;
r.solid = true;

View file

@ -6,15 +6,11 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 23:31:22 by mcolonna #+# #+# */
/* Updated: 2024/04/02 12:42:18 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 18:28:28 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include <fcntl.h>
#include "room.h"
#include "error.h"
#include "libtf.h"
#include "env.h"
#include "includes.h"
#include "room_utils.h"
static bool room_fromfile2(
@ -22,11 +18,11 @@ static bool room_fromfile2(
{
t_const_string line;
t_roomcase *roomcase;
const t_memclass mc = mem_subclass(err, g_env.mc);
const t_memclass mc = mem_subclass(error_err, g_env.mc);
line = read_line(err_remember, mc, fd);
if (err_get())
err_perror(path);
error_perror(path);
if (!line)
return (false);
while (*line && *line != '\n')
@ -34,7 +30,8 @@ static bool room_fromfile2(
roomcase = getroomcase(path, *line);
if (roomcase->object)
{
room->objects[*i] = mem_alloc(err, room->mc, sizeof(t_object));
room->objects[*i] = mem_alloc(error_err, room->mc,
sizeof(t_object));
*(room->objects[*i]) = roomcase->object(room->mc);
}
else
@ -50,16 +47,18 @@ t_room room_fromfile(t_const_string path)
{
t_room r;
int fd;
const t_memclass mc = mem_subclass(err, g_env.mc);
const t_memclass mc = mem_subclass(error_err, g_env.mc);
int i;
room_getsize(mc, &r, path);
r.mc = mem_subclass(err, g_env.mc);
r.mc = mem_subclass(error_err, g_env.mc);
fd = open(path, O_RDONLY);
if (fd == -1)
err_perror(path);
r.surfaces = mem_alloc(err, r.mc, r.width * r.height * sizeof(t_sprite));
r.objects = mem_alloc(err, r.mc, r.width * r.height * sizeof(t_object *));
error_perror(path);
r.surfaces = mem_alloc(error_err, r.mc, r.width * r.height
* sizeof(t_sprite));
r.objects = mem_alloc(error_err, r.mc, r.width * r.height
* sizeof(t_object *));
i = 0;
while (room_fromfile2(fd, path, &i, &r))
;
@ -99,7 +98,7 @@ void room_loop(t_room room)
t_point pos;
t_object *object;
t_point move;
const t_memclass mc = mem_subclass(err, g_env.mc);
const t_memclass mc = mem_subclass(error_err, g_env.mc);
t_list objects_done;
objects_done = list_createempty(mc);
@ -114,7 +113,7 @@ void room_loop(t_room room)
{
move = object->type.loop(object, pos);
moveobject(room, pos, move);
list_add(err, &objects_done, object);
list_add(error_err, &objects_done, object);
}
pos.y++;
}

View file

@ -6,15 +6,11 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 23:31:22 by mcolonna #+# #+# */
/* Updated: 2024/03/29 17:32:47 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 17:44:12 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include <fcntl.h>
#include "room.h"
#include "error.h"
#include "libtf.h"
#include "env.h"
#include "includes.h"
#include "room_utils.h"
t_object *room_getobjectfaced(

29
src/room/room_utils.h Normal file
View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* room_utils.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 16:34:01 by mcolonna #+# #+# */
/* Updated: 2024/04/02 17:51:05 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ROOM_UTILS_H
# define ROOM_UTILS_H
typedef struct s_roomcase
{
char c;
t_object_init object;
} t_roomcase;
int getlinelen(t_const_string line);
void room_getsize(t_memclass mc, t_room *r, t_const_string path);
t_roomcase *getroomcase(t_const_string path, char c);
void moveobject(t_room room, t_point start, t_point move);
bool isinlist(void *addr, t_list list);
void room_patch(t_room *room);
#endif

View file

@ -6,14 +6,12 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 16:26:28 by mcolonna #+# #+# */
/* Updated: 2024/04/02 12:44:30 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 17:58:59 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include <fcntl.h>
#include "includes.h"
#include "room_utils.h"
#include "error.h"
#include "object.h"
static t_roomcase g_roomcases[] = {
{c: '0', object: NULL},
@ -39,23 +37,23 @@ void room_getsize(t_memclass mc, t_room *r, t_const_string path)
fd = open(path, O_RDONLY);
if (fd == -1)
err_perror(path);
error_perror(path);
line = read_line(err_remember, mc, fd);
if (err_get())
err_perror(path);
error_perror(path);
if (!line)
err_perror_str(path, "room is empty");
error_str(path, "room is empty");
r->width = getlinelen(line);
r->height = 1;
while (true)
{
line = read_line(err_remember, mc, fd);
if (err_get())
err_perror(path);
error_perror(path);
if (!line)
break ;
if (getlinelen(line) != r->width)
err_perror_str(path, "the lines have different lengths");
error_str(path, "the lines have different lengths");
r->height++;
}
}
@ -70,7 +68,7 @@ t_roomcase *getroomcase(t_const_string path, char c)
if (g_roomcases[i].c == c)
return (&g_roomcases[i]);
}
err_perror_str(path, "unknown char");
error_str(path, "unknown char");
return (NULL);
}

View file

@ -6,12 +6,12 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 12:33:48 by mcolonna #+# #+# */
/* Updated: 2024/04/02 12:51:20 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 17:52:14 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes.h"
#include "room_utils.h"
#include "data_sprites.h"
void room_patch(t_room *room)
{

View file

@ -6,11 +6,11 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 15:24:37 by mcolonna #+# #+# */
/* Updated: 2024/03/27 18:23:24 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 14:41:19 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "point.h"
#include "includes.h"
t_point point_init(int x, int y)
{