dev: norminette *

mostly reorganize room src files
This commit is contained in:
mcolonna 2024-04-09 15:30:40 +02:00
parent cc657ba53d
commit b7a2d0494f
10 changed files with 139 additions and 142 deletions

View file

@ -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

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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,

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;

View file

@ -5,14 +5,37 @@
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -5,38 +5,70 @@
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

41
src/room/room3.c Normal file
View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* room3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* room_utils1.c :+: :+: :+: */
/* room_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -1,37 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* room_utils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
}
}