feat: engine finished (i think)

This commit is contained in:
mcolonna 2024-03-15 20:01:38 +01:00
parent 8076487071
commit 3abc6cbfd6
15 changed files with 207 additions and 41 deletions

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/11 16:02:29 by mcolonna #+# #+# */
/* Updated: 2024/03/11 16:37:05 by mcolonna ### ########.fr */
/* Updated: 2024/03/15 15:31:48 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,5 +14,6 @@
const t_spriteinfo g_spritesmap[NB_SPRITES] = {
{WALL_BORDER, 1, 50, 50},
{FLOOR_1, 1, 50, 50}
{FLOOR_1, 1, 50, 50},
{OBJECT_SNAS_DOWN, 1, 50, 50}
};

42
src/object.c Normal file
View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* object.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 15:27:03 by mcolonna #+# #+# */
/* Updated: 2024/03/15 20:00:25 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "object.h"
#include "error.h"
#include "sprite.h"
static t_point snas_loop(t_object *obj, t_point pos)
{
const t_point r = {0, 0};
(void)((void)obj, pos);
return (r);
}
static void snas_draw(t_object *obj, int x, int y)
{
sprite_draw(x, y, &((t_snas_data *)obj->data)->spr);
}
t_object snas_init(t_memclass mc)
{
static const t_objecttype type = {loop: snas_loop, draw: snas_draw};
t_object r;
t_snas_data *data;
r.type = type;
data = mem_alloc(err, mc, sizeof(t_snas_data));
data->direction = DOWN;
data->spr = sprite_init(SPR_SNAS_DOWN);
r.data = data;
return (r);
}

26
src/point.c Normal file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* point.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 15:24:37 by mcolonna #+# #+# */
/* Updated: 2024/03/15 15:26:34 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "point.h"
t_point point_init(int x, int y)
{
const t_point r = {x, y};
return (r);
}
void point_addto(t_point *dest, t_point src)
{
dest->x += src.x;
dest->y += src.y;
}

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 23:31:22 by mcolonna #+# #+# */
/* Updated: 2024/03/13 16:37:06 by mcolonna ### ########.fr */
/* Updated: 2024/03/25 14:42:55 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,9 +18,11 @@
#include "room_utils.h"
static bool room_fromfile2(
int fd, t_memclass mc, t_const_string path, t_sprite **surface)
int fd, t_const_string path, int *i, t_room *room)
{
t_const_string line;
t_roomcase *roomcase;
const t_memclass mc = mem_subclass(err, g_env.mc);
line = read_line(err_remember, mc, fd);
if (err_get())
@ -29,10 +31,19 @@ static bool room_fromfile2(
return (false);
while (*line && *line != '\n')
{
**surface = sprite_init(getroomcase(path, *line)->surface_spr);
roomcase = getroomcase(path, *line);
room->surfaces[*i] = sprite_init(roomcase->surface_spr);
if (roomcase->object)
{
room->objects[*i] = mem_alloc(err, room->mc, sizeof(t_object));
*(room->objects[*i]) = roomcase->object(room->mc);
}
else
room->objects[*i] = NULL;
line++;
(*surface)++;
(*i)++;
}
mem_freeall(mc);
return (true);
}
@ -40,18 +51,18 @@ t_room room_fromfile(t_const_string path)
{
t_room r;
int fd;
t_sprite *surface;
const t_memclass mc = mem_subclass(err, g_env.mc);
int i;
room_getsize(mc, &r, path);
r.mc = mem_subclass(err, g_env.mc);
r.surfaces = mem_alloc(err, r.mc, r.width * r.height * sizeof(t_sprite));
fd = open(path, O_RDONLY);
if (fd == -1)
err_perror(path);
surface = r.surfaces;
while (surface - r.surfaces < r.width * r.height
&& room_fromfile2(fd, mc, path, &surface))
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 *));
i = 0;
while (room_fromfile2(fd, path, &i, &r))
;
mem_freeall(mc);
return (r);
@ -59,16 +70,26 @@ t_room room_fromfile(t_const_string path)
void room_draw(t_room room)
{
int x;
int y;
int x;
int y;
t_object *obj;
y = -1;
while (++y < room.height)
{
x = -1;
while (++x < room.width)
{
sprite_draw(x * 50, y * 50, room.surfaces + y * room.width + x);
}
y = -1;
while (++y < room.height)
{
x = -1;
while (++x < room.width)
{
obj = room.objects[y * room.width + x];
if (obj)
obj->type.draw(obj, x * 50, y * 50);
}
}
}

View file

@ -6,17 +6,19 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 16:26:28 by mcolonna #+# #+# */
/* Updated: 2024/03/13 16:55:56 by mcolonna ### ########.fr */
/* Updated: 2024/03/15 18:55:48 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include <fcntl.h>
#include "room_utils.h"
#include "error.h"
#include "object.h"
static t_roomcase g_roomcases[] = {
{c: '0', surface_spr: CASE_FLOOR},
{c: '1', surface_spr: CASE_WALL},
{c: '0', surface_spr: CASE_FLOOR, object: NULL},
{c: '1', surface_spr: CASE_WALL, object: NULL},
{c: 'P', surface_spr: CASE_FLOOR, object: snas_init},
{c: '\0'}
};