feat: program takes an argument for the room

This commit is contained in:
mcolonna 2024-04-09 14:51:10 +02:00
parent bdb258f6b2
commit cc657ba53d
10 changed files with 40 additions and 28 deletions

View file

@ -6,4 +6,3 @@
- rectangular - rectangular
- walls all around - walls all around
- a possible way to exit - a possible way to exit
- Program takes the map file

View file

@ -6,13 +6,13 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 13:39:54 by mcolonna #+# #+# */ /* Created: 2024/04/02 13:39:54 by mcolonna #+# #+# */
/* Updated: 2024/04/02 14:39:40 by mcolonna ### ########.fr */ /* Updated: 2024/04/09 14:45:55 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef MAIN_H #ifndef MAIN_H
# define MAIN_H # define MAIN_H
int main(void); int main(int argc, t_const_string *argv);
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 14:58:25 by mcolonna #+# #+# */ /* Created: 2024/03/15 14:58:25 by mcolonna #+# #+# */
/* Updated: 2024/04/08 15:35:33 by mcolonna ### ########.fr */ /* Updated: 2024/04/09 14:34:13 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,8 +16,11 @@
typedef struct s_object t_object; typedef struct s_object t_object;
typedef struct s_character t_character; typedef struct s_character t_character;
typedef t_object (*t_object_init)(t_memclass);
typedef struct s_objecttype typedef struct s_objecttype
{ {
t_object_init init;
t_point (*loop)(t_object *, t_point pos); t_point (*loop)(t_object *, t_point pos);
void (*draw)(t_object *, int x, int y); void (*draw)(t_object *, int x, int y);
// Called when a character want to walk through this object. // Called when a character want to walk through this object.
@ -32,6 +35,4 @@ typedef struct s_object
void *data; void *data;
} t_object; } t_object;
typedef t_object (*t_object_init)(t_memclass);
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 23:00:45 by mcolonna #+# #+# */ /* Created: 2024/03/07 23:00:45 by mcolonna #+# #+# */
/* Updated: 2024/04/08 15:17:12 by mcolonna ### ########.fr */ /* Updated: 2024/04/09 14:22:51 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,7 +23,7 @@ typedef struct s_room
t_memclass mc; t_memclass mc;
} t_room; } t_room;
t_room room_fromfile(t_const_string path); void room_init(t_const_string path);
void room_loop(t_room room); void room_loop(t_room room);

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 15:28:34 by mcolonna #+# #+# */ /* Created: 2024/02/26 15:28:34 by mcolonna #+# #+# */
/* Updated: 2024/04/09 14:16:56 by mcolonna ### ########.fr */ /* Updated: 2024/04/09 14:46:08 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -39,26 +39,24 @@ int loop_hook(void)
return (0); return (0);
} }
int main(void) int main(int argc, t_const_string *argv)
{ {
g_env.max_ketchup = 0; if (argc != 2)
g_env.moves = 0; error_str("so_long", "takes 1 argument");
g_env.mc = mem_newclass(error_err);
g_env.mlx = mlx_init(); g_env.mlx = mlx_init();
if (!g_env.mlx) if (!g_env.mlx)
error_err("mlx_init() failed"); error_err("mlx_init() failed");
g_env.mc = mem_newclass(error_err); mlx_do_key_autorepeatoff(g_env.mlx);
display_init(); display_init();
g_env.win = mlx_new_window(g_env.mlx, WINDOW_WIDTH, WINDOW_HEIGHT, g_env.win = mlx_new_window(g_env.mlx, WINDOW_WIDTH, WINDOW_HEIGHT,
WINDOW_TITLE); WINDOW_TITLE);
if (!g_env.win) if (!g_env.win)
error_err("mlx_new_window() failed"); error_err("mlx_new_window() failed");
g_env.room = room_fromfile("room/room.ber"); room_init(argv[1]);
g_env.ketchup = 0;
room_draw(g_env.room);
mlx_expose_hook(g_env.win, expose_hook, NULL); mlx_expose_hook(g_env.win, expose_hook, NULL);
mlx_hook(g_env.win, DestroyNotify, StructureNotifyMask, close_hook, NULL); mlx_hook(g_env.win, DestroyNotify, StructureNotifyMask, close_hook, NULL);
mlx_loop_hook(g_env.mlx, loop_hook, NULL); mlx_loop_hook(g_env.mlx, loop_hook, NULL);
mlx_do_key_autorepeatoff(g_env.mlx);
mlx_hook(g_env.win, KeyPress, KeyPressMask, key_press_hook, NULL); mlx_hook(g_env.win, KeyPress, KeyPressMask, key_press_hook, NULL);
mlx_hook(g_env.win, KeyRelease, KeyReleaseMask, key_release_hook, NULL); mlx_hook(g_env.win, KeyRelease, KeyReleaseMask, key_release_hook, NULL);
mlx_loop(g_env.mlx); mlx_loop(g_env.mlx);

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/08 15:01:16 by mcolonna #+# #+# */ /* Created: 2024/04/08 15:01:16 by mcolonna #+# #+# */
/* Updated: 2024/04/08 15:36:22 by mcolonna ### ########.fr */ /* Updated: 2024/04/09 14:35:20 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -47,7 +47,8 @@ static bool exit_walk_through(
t_object exit_init(t_memclass mc) t_object exit_init(t_memclass mc)
{ {
static const t_objecttype type static const t_objecttype type
= {loop: exit_loop, draw: exit_draw, walk_through: exit_walk_through}; = {init: exit_init, loop: exit_loop, draw: exit_draw,
walk_through: exit_walk_through};
t_object r; t_object r;
t_exit_data *data; t_exit_data *data;

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 15:27:03 by mcolonna #+# #+# */ /* Created: 2024/03/15 15:27:03 by mcolonna #+# #+# */
/* Updated: 2024/04/08 15:36:10 by mcolonna ### ########.fr */ /* Updated: 2024/04/09 14:35:36 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,7 +37,7 @@ static bool ketchup_walk_through(
t_object ketchup_init(t_memclass mc) t_object ketchup_init(t_memclass mc)
{ {
static const t_objecttype type static const t_objecttype type
= {loop: ketchup_loop, draw: ketchup_draw, = {init: ketchup_init, loop: ketchup_loop, draw: ketchup_draw,
walk_through: ketchup_walk_through}; walk_through: ketchup_walk_through};
t_object r; t_object r;
t_wall_data *data; t_wall_data *data;
@ -46,6 +46,5 @@ t_object ketchup_init(t_memclass mc)
data = mem_alloc(error_err, mc, sizeof(t_wall_data)); data = mem_alloc(error_err, mc, sizeof(t_wall_data));
data->spr = sprite_init(SPR_KETCHUP); data->spr = sprite_init(SPR_KETCHUP);
r.data = data; r.data = data;
g_env.max_ketchup++;
return (r); return (r);
} }

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 15:27:03 by mcolonna #+# #+# */ /* Created: 2024/03/15 15:27:03 by mcolonna #+# #+# */
/* Updated: 2024/04/08 17:11:46 by mcolonna ### ########.fr */ /* Updated: 2024/04/09 14:35:48 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -43,7 +43,7 @@ static void snas_draw(t_object *obj, int x, int y)
t_object snas_init(t_memclass mc) t_object snas_init(t_memclass mc)
{ {
static const t_objecttype type static const t_objecttype type
= {loop: snas_loop, draw: snas_draw, walk_through: NULL}; = {init: snas_init, loop: snas_loop, draw: snas_draw, walk_through: NULL};
static const t_character_sprites sprites = { static const t_character_sprites sprites = {
still: SPR_SNAS, still: SPR_SNAS,
walk_left: SPR_SNAS_WALK_LEFT, walk_left: SPR_SNAS_WALK_LEFT,

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 15:27:03 by mcolonna #+# #+# */ /* Created: 2024/03/15 15:27:03 by mcolonna #+# #+# */
/* Updated: 2024/04/03 15:17:07 by mcolonna ### ########.fr */ /* Updated: 2024/04/09 14:36:04 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -27,7 +27,7 @@ static void wall_draw(t_object *obj, int x, int y)
t_object wall_init(t_memclass mc) t_object wall_init(t_memclass mc)
{ {
static const t_objecttype type static const t_objecttype type
= {loop: wall_loop, draw: wall_draw, walk_through: NULL}; = {init: wall_init, loop: wall_loop, draw: wall_draw, walk_through: NULL};
t_object r; t_object r;
t_wall_data *data; t_wall_data *data;

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 23:31:22 by mcolonna #+# #+# */ /* Created: 2024/03/07 23:31:22 by mcolonna #+# #+# */
/* Updated: 2024/04/08 15:13:16 by mcolonna ### ########.fr */ /* Updated: 2024/04/09 14:40:32 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -43,7 +43,7 @@ static bool room_fromfile2(
return (true); return (true);
} }
t_room room_fromfile(t_const_string path) static t_room room_fromfile(t_const_string path)
{ {
t_room r; t_room r;
int fd; int fd;
@ -72,6 +72,20 @@ t_room room_fromfile(t_const_string path)
return (r); return (r);
} }
void room_init(t_const_string path)
{
int i;
g_env.room = room_fromfile(path);
g_env.moves = 0;
g_env.ketchup = 0;
g_env.max_ketchup = 0;
i = -1;
while (++i < g_env.room.width * g_env.room.height)
if (g_env.room.objects[i] && g_env.room.objects[i]->type.init == ketchup_init)
g_env.max_ketchup++;
}
void room_draw(t_room room) void room_draw(t_room room)
{ {
int x; int x;