From b7a2d0494f5ac07029aa6dc9139e104838ae4787 Mon Sep 17 00:00:00 2001 From: mcolonna Date: Tue, 9 Apr 2024 15:30:40 +0200 Subject: [PATCH] dev: norminette * mostly reorganize room src files --- Makefile | 2 +- src/display/display1.c | 4 +- src/room/object_snas.c | 5 +- src/room/object_wall.c | 5 +- src/room/room1.c | 64 ++++++++----------- src/room/room2.c | 80 +++++++++++++++++------- src/room/room3.c | 41 ++++++++++++ src/room/{room_utils1.c => room_utils.c} | 35 ++--------- src/room/room_utils.h | 8 +-- src/room/room_utils2.c | 37 ----------- 10 files changed, 139 insertions(+), 142 deletions(-) create mode 100644 src/room/room3.c rename src/room/{room_utils1.c => room_utils.c} (71%) delete mode 100644 src/room/room_utils2.c diff --git a/Makefile b/Makefile index a9942e2..40ad6ef 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ CODE = $(addprefix main/, \ display1 display2 display_utils sprite \ ) \ $(addprefix room/, \ - visual room1 room2 room_utils1 room_utils2 \ + visual room1 room2 room3 room_utils \ object_character object_snas object_wall object_ketchup object_exit \ ) \ utils/point diff --git a/src/display/display1.c b/src/display/display1.c index a1a5a1e..f596bdb 100644 --- a/src/display/display1.c +++ b/src/display/display1.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/26 15:05:30 by mcolonna #+# #+# */ -/* Updated: 2024/04/09 13:55:56 by mcolonna ### ########.fr */ +/* Updated: 2024/04/09 15:29:39 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,7 +45,7 @@ void display_erase(void) void display_flush(void) { const t_memclass mc = mem_subclass(error_err, g_env.mc); - t_string str; + t_string str; mlx_put_image_to_window(g_env.mlx, g_env.win, g_screenbuf.img, 0, 0); str = str_inttostr(error_err, mc, g_env.moves); diff --git a/src/room/object_snas.c b/src/room/object_snas.c index a354ea6..a080c2a 100644 --- a/src/room/object_snas.c +++ b/src/room/object_snas.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/15 15:27:03 by mcolonna #+# #+# */ -/* Updated: 2024/04/09 14:35:48 by mcolonna ### ########.fr */ +/* Updated: 2024/04/09 15:28:02 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,7 +43,8 @@ static void snas_draw(t_object *obj, int x, int y) t_object snas_init(t_memclass mc) { static const t_objecttype type - = {init: snas_init, 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 = { still: SPR_SNAS, walk_left: SPR_SNAS_WALK_LEFT, diff --git a/src/room/object_wall.c b/src/room/object_wall.c index 537d084..b61a023 100644 --- a/src/room/object_wall.c +++ b/src/room/object_wall.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/15 15:27:03 by mcolonna #+# #+# */ -/* Updated: 2024/04/09 14:36:04 by mcolonna ### ########.fr */ +/* Updated: 2024/04/09 15:27:39 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,7 +27,8 @@ static void wall_draw(t_object *obj, int x, int y) t_object wall_init(t_memclass mc) { static const t_objecttype type - = {init: wall_init, 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_wall_data *data; diff --git a/src/room/room1.c b/src/room/room1.c index e91aa04..7fb457d 100644 --- a/src/room/room1.c +++ b/src/room/room1.c @@ -5,14 +5,37 @@ /* +:+ +:+ +:+ */ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/03/07 23:31:22 by mcolonna #+# #+# */ -/* Updated: 2024/04/09 14:40:32 by mcolonna ### ########.fr */ +/* Created: 2024/04/09 15:11:29 by mcolonna #+# #+# */ +/* Updated: 2024/04/09 15:28:31 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ #include "includes.h" #include "room_utils.h" +static void room_fromfile3(t_room *room) +{ + int x; + int y; + + x = -1; + while (++x < room->width) + { + y = -1; + while (++y < room->height) + { + if (y == 0 || y == room->height - 1) + { + room->surfaces[y * room->width + x] = sprite_init(CASE_WALL); + room->objects[y * room->width + x] = NULL; + } + else + room->surfaces[y * room->width + x] = sprite_init( + CASE_FLOOR_1 + (x + y) % 2); + } + } +} + static bool room_fromfile2( int fd, t_const_string path, int *i, t_room *room) { @@ -82,7 +105,8 @@ void room_init(t_const_string path) 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) + if (g_env.room.objects[i] + && g_env.room.objects[i]->type.init == ketchup_init) g_env.max_ketchup++; } @@ -112,37 +136,3 @@ void room_draw(t_room room) } } } - -void room_loop(t_room room) -{ - t_point pos; - t_object *object; - t_point move; - const t_memclass mc = mem_subclass(error_err, g_env.mc); - t_list objects_done; - - objects_done = list_createempty(mc); - pos.x = 0; - while (pos.x < room.width) - { - pos.y = 0; - while (pos.y < room.height) - { - object = room.objects[pos.y * room.width + pos.x]; - if (object && !isinlist(object, objects_done)) - { - move = object->type.loop(object, pos); - moveobject(room, pos, move); - list_add(error_err, &objects_done, object); - } - pos.y++; - } - pos.x++; - } - mem_freeall(mc); -} - -void room_free(t_room room) -{ - mem_freeall(room.mc); -} diff --git a/src/room/room2.c b/src/room/room2.c index 1a836b1..5833951 100644 --- a/src/room/room2.c +++ b/src/room/room2.c @@ -5,38 +5,70 @@ /* +:+ +:+ +:+ */ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/03/07 23:31:22 by mcolonna #+# #+# */ -/* Updated: 2024/04/08 15:36:42 by mcolonna ### ########.fr */ +/* Created: 2024/04/09 15:14:49 by mcolonna #+# #+# */ +/* Updated: 2024/04/09 15:21:02 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ #include "includes.h" -#include "room_utils.h" -t_object *room_getobjectfaced( - t_room room, t_character *character, t_point pos) +static void moveobject(t_room room, t_point start, t_point move) { - point_addto(&pos, point_fromdirection(character->direction)); - return (room.objects[pos.y * room.width + pos.x]); + t_point end; + + end = start; + point_addto(&end, move); + if (start.x == end.x && start.y == end.y) + return ; + room.objects[end.y * room.width + end.x] + = room.objects[start.y * room.width + start.x]; + room.objects[start.y * room.width + start.x] = NULL; } -// if this returns true, then character *must* walk forward. -bool room_canwalk(t_room room, t_character *character, t_point pos) +static bool isinlist(void *addr, t_list list) { - t_point after; - t_object *obj; - bool r; + t_list_element *el; - after = pos; - point_addto(&after, point_fromdirection(character->direction)); - if (after.y <= 0 || after.y >= room.height - 1 - || after.x <= 0 || after.x >= room.width - 1) - return (false); - obj = room_getobjectfaced(room, character, pos); - r = (!obj || ( - obj->type.walk_through - && obj->type.walk_through(obj, character, after))); - if (r) - mem_free(obj); - return (r); + el = list.first; + while (el) + { + if (el->value == addr) + return (true); + el = el->next; + } + return (false); +} + +void room_loop(t_room room) +{ + t_point pos; + t_object *object; + t_point move; + const t_memclass mc = mem_subclass(error_err, g_env.mc); + t_list objects_done; + + objects_done = list_createempty(mc); + pos.x = 0; + while (pos.x < room.width) + { + pos.y = 0; + while (pos.y < room.height) + { + object = room.objects[pos.y * room.width + pos.x]; + if (object && !isinlist(object, objects_done)) + { + move = object->type.loop(object, pos); + moveobject(room, pos, move); + list_add(error_err, &objects_done, object); + } + pos.y++; + } + pos.x++; + } + mem_freeall(mc); +} + +void room_free(t_room room) +{ + mem_freeall(room.mc); } diff --git a/src/room/room3.c b/src/room/room3.c new file mode 100644 index 0000000..9203a32 --- /dev/null +++ b/src/room/room3.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* room3.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mcolonna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/09 15:18:38 by mcolonna #+# #+# */ +/* Updated: 2024/04/09 15:25:41 by mcolonna ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "includes.h" + +t_object *room_getobjectfaced( + t_room room, t_character *character, t_point pos) +{ + point_addto(&pos, point_fromdirection(character->direction)); + return (room.objects[pos.y * room.width + pos.x]); +} + +// if this returns true, then character *must* walk forward. +bool room_canwalk(t_room room, t_character *character, t_point pos) +{ + t_point after; + t_object *obj; + bool r; + + after = pos; + point_addto(&after, point_fromdirection(character->direction)); + if (after.y <= 0 || after.y >= room.height - 1 + || after.x <= 0 || after.x >= room.width - 1) + return (false); + obj = room_getobjectfaced(room, character, pos); + r = (!obj || ( + obj->type.walk_through + && obj->type.walk_through(obj, character, after))); + if (r) + mem_free(obj); + return (r); +} diff --git a/src/room/room_utils1.c b/src/room/room_utils.c similarity index 71% rename from src/room/room_utils1.c rename to src/room/room_utils.c index d0ad48d..2dbbab5 100644 --- a/src/room/room_utils1.c +++ b/src/room/room_utils.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* room_utils1.c :+: :+: :+: */ +/* room_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/03/13 16:26:28 by mcolonna #+# #+# */ -/* Updated: 2024/04/08 16:21:01 by mcolonna ### ########.fr */ +/* Created: 2024/04/09 15:21:33 by mcolonna #+# #+# */ +/* Updated: 2024/04/09 15:24:42 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ static t_roomcase g_roomcases[] = { {c: '\0'} }; -int getlinelen(t_const_string line) +static int getlinelen(t_const_string line) { int r; @@ -73,30 +73,3 @@ t_roomcase *getroomcase(t_const_string path, char c) error_str(path, "unknown char"); return (NULL); } - -void moveobject(t_room room, t_point start, t_point move) -{ - t_point end; - - end = start; - point_addto(&end, move); - if (start.x == end.x && start.y == end.y) - return ; - room.objects[end.y * room.width + end.x] - = room.objects[start.y * room.width + start.x]; - room.objects[start.y * room.width + start.x] = NULL; -} - -bool isinlist(void *addr, t_list list) -{ - t_list_element *el; - - el = list.first; - while (el) - { - if (el->value == addr) - return (true); - el = el->next; - } - return (false); -} diff --git a/src/room/room_utils.h b/src/room/room_utils.h index ba71e5e..24e2ba1 100644 --- a/src/room/room_utils.h +++ b/src/room/room_utils.h @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/03/13 16:34:01 by mcolonna #+# #+# */ -/* Updated: 2024/04/08 15:13:43 by mcolonna ### ########.fr */ +/* Created: 2024/04/09 15:23:43 by mcolonna #+# #+# */ +/* Updated: 2024/04/09 15:24:07 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,11 +19,7 @@ typedef struct s_roomcase 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_fromfile3(t_room *room); #endif diff --git a/src/room/room_utils2.c b/src/room/room_utils2.c deleted file mode 100644 index d553cff..0000000 --- a/src/room/room_utils2.c +++ /dev/null @@ -1,37 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* room_utils2.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mcolonna +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/04/02 12:33:48 by mcolonna #+# #+# */ -/* Updated: 2024/04/08 15:16:01 by mcolonna ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "includes.h" -#include "room_utils.h" - -void room_fromfile3(t_room *room) -{ - int x; - int y; - - x = -1; - while (++x < room->width) - { - y = -1; - while (++y < room->height) - { - if (y == 0 || y == room->height - 1) - { - room->surfaces[y * room->width + x] = sprite_init(CASE_WALL); - room->objects[y * room->width + x] = NULL; - } - else - room->surfaces[y * room->width + x] = sprite_init( - CASE_FLOOR_1 + (x + y) % 2); - } - } -}