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

@ -5,7 +5,7 @@ INCLUDES = includes/
CODE = main error env \ CODE = main error env \
data_assets data_sprites \ data_assets data_sprites \
display1 display2 display_utils sprite \ display1 display2 display_utils sprite \
room room_utils room room_utils object
LIBRARIES = mlx libtf LIBRARIES = mlx libtf
LIBRARIES_FILES = libtf/libtf.a LIBRARIES_FILES = libtf/libtf.a
LIBRARIES_LINK = mlx LIBRARIES_LINK = mlx

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/11 15:56:31 by mcolonna #+# #+# */ /* Created: 2024/03/11 15:56:31 by mcolonna #+# #+# */
/* Updated: 2024/03/11 16:31:33 by mcolonna ### ########.fr */ /* Updated: 2024/03/15 18:49:12 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,7 @@
# include "data_assets.h" # include "data_assets.h"
# define NB_SPRITES 2 # define NB_SPRITES 3
typedef struct s_spriteinfo typedef struct s_spriteinfo
{ {
@ -30,7 +30,8 @@ extern const t_spriteinfo g_spritesmap[NB_SPRITES];
typedef enum e_spriteid typedef enum e_spriteid
{ {
CASE_WALL, CASE_WALL,
CASE_FLOOR CASE_FLOOR,
SPR_SNAS_DOWN
} t_spriteid; } t_spriteid;
#endif #endif

46
includes/object.h Normal file
View file

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* object.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 14:58:25 by mcolonna #+# #+# */
/* Updated: 2024/03/15 20:06:45 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef OBJECT_H
# define OBJECT_H
# include "point.h"
# include "data_assets.h"
# include "libtf.h"
# include "display.h"
# include "sprite.h"
typedef struct s_object t_object;
typedef struct s_objecttype
{
t_point (*loop)(t_object *, t_point pos);
void (*draw)(t_object *, int x, int y);
} t_objecttype;
typedef struct s_object
{
t_objecttype type;
void *data;
} t_object;
typedef struct s_snas_data
{
t_direction direction;
t_sprite spr;
} t_snas_data;
typedef t_object (*t_object_init)(t_memclass);
t_object snas_init(t_memclass mc);
#endif

27
includes/point.h Normal file
View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* point.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 15:05:03 by mcolonna #+# #+# */
/* Updated: 2024/03/15 19:38:46 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef POINT_H
# define POINT_H
typedef struct s_point
{
int x;
int y;
} t_point;
/*
t_point point_init(int x, int y);
void point_addto(t_point *dest, t_point src);
*/
#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/03/13 14:11:13 by mcolonna ### ########.fr */ /* Updated: 2024/03/15 18:51:14 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,11 +15,7 @@
# include "sprite.h" # include "sprite.h"
# include "libtf.h" # include "libtf.h"
# include "object.h"
typedef struct s_object
{
t_sprite spr;
} t_object;
typedef struct s_room typedef struct s_room
{ {
@ -34,6 +30,7 @@ typedef struct s_roomcase
{ {
char c; char c;
t_spriteid surface_spr; t_spriteid surface_spr;
t_object_init object;
} t_roomcase; } t_roomcase;
t_room room_fromfile(t_const_string path); t_room room_fromfile(t_const_string path);

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 22:13:20 by mcolonna #+# #+# */ /* Created: 2024/03/07 22:13:20 by mcolonna #+# #+# */
/* Updated: 2024/03/11 16:26:48 by mcolonna ### ########.fr */ /* Updated: 2024/03/15 19:55:46 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/30 15:36:49 by mcolonna #+# #+# */ /* Created: 2023/10/30 15:36:49 by mcolonna #+# #+# */
/* Updated: 2024/02/23 14:17:46 by mcolonna ### ########.fr */ /* Updated: 2024/03/25 14:21:52 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,6 +23,7 @@
typedef struct s_buf typedef struct s_buf
{ {
t_memclass mc;
int size; int size;
char str[BUFFER_SIZE]; char str[BUFFER_SIZE];
} t_buf; } t_buf;

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/30 15:39:50 by mcolonna #+# #+# */ /* Created: 2023/10/30 15:39:50 by mcolonna #+# #+# */
/* Updated: 2024/02/23 14:16:55 by mcolonna ### ########.fr */ /* Updated: 2024/03/25 14:29:49 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -65,7 +65,7 @@ static void *free_and_return(
{ {
if (buf) if (buf)
{ {
mem_free(*buf); mem_freeall((*buf)->mc);
*buf = NULL; *buf = NULL;
} }
if (string) if (string)

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 14:28:19 by mcolonna #+# #+# */ /* Created: 2023/11/06 14:28:19 by mcolonna #+# #+# */
/* Updated: 2024/02/23 14:18:00 by mcolonna ### ########.fr */ /* Updated: 2024/03/25 14:53:04 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,11 +16,13 @@
int create_empty_buf(t_buf **result) int create_empty_buf(t_buf **result)
{ {
const t_errmc errmc = setget_errmc(NULL, NULL); const t_errmc errmc = setget_errmc(NULL, NULL);
const t_memclass mc = mem_newclass(errmc.err);
*result = mem_alloc(errmc.err, errmc.mc, sizeof(t_buf)); *result = mem_alloc(errmc.err, mc, sizeof(t_buf));
if (!*result) if (!*result)
return (0); return (0);
(*result)->size = 0; (*result)->size = 0;
(*result)->mc = mc;
return (1); return (1);
} }

View file

@ -1,5 +1,5 @@
11111 11111
10011 1P011
11111 11111
10001 10001
11111 11111

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/11 16:02:29 by mcolonna #+# #+# */ /* 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] = { const t_spriteinfo g_spritesmap[NB_SPRITES] = {
{WALL_BORDER, 1, 50, 50}, {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> +#+ +:+ +#+ */ /* 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/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" #include "room_utils.h"
static bool room_fromfile2( 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_const_string line;
t_roomcase *roomcase;
const t_memclass mc = mem_subclass(err, g_env.mc);
line = read_line(err_remember, mc, fd); line = read_line(err_remember, mc, fd);
if (err_get()) if (err_get())
@ -29,10 +31,19 @@ static bool room_fromfile2(
return (false); return (false);
while (*line && *line != '\n') while (*line && *line != '\n')
{ {
**surface = sprite_init(getroomcase(path, *line)->surface_spr); roomcase = getroomcase(path, *line);
line++; room->surfaces[*i] = sprite_init(roomcase->surface_spr);
(*surface)++; 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++;
(*i)++;
}
mem_freeall(mc);
return (true); return (true);
} }
@ -40,18 +51,18 @@ t_room room_fromfile(t_const_string path)
{ {
t_room r; t_room r;
int fd; int fd;
t_sprite *surface;
const t_memclass mc = mem_subclass(err, g_env.mc); const t_memclass mc = mem_subclass(err, g_env.mc);
int i;
room_getsize(mc, &r, path); room_getsize(mc, &r, path);
r.mc = mem_subclass(err, g_env.mc); 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); fd = open(path, O_RDONLY);
if (fd == -1) if (fd == -1)
err_perror(path); err_perror(path);
surface = r.surfaces; r.surfaces = mem_alloc(err, r.mc, r.width * r.height * sizeof(t_sprite));
while (surface - r.surfaces < r.width * r.height r.objects = mem_alloc(err, r.mc, r.width * r.height * sizeof(t_object *));
&& room_fromfile2(fd, mc, path, &surface)) i = 0;
while (room_fromfile2(fd, path, &i, &r))
; ;
mem_freeall(mc); mem_freeall(mc);
return (r); return (r);
@ -61,15 +72,25 @@ void room_draw(t_room room)
{ {
int x; int x;
int y; int y;
t_object *obj;
y = -1; y = -1;
while (++y < room.height) while (++y < room.height)
{ {
x = -1; x = -1;
while (++x < room.width) while (++x < room.width)
{
sprite_draw(x * 50, y * 50, room.surfaces + y * room.width + x); 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> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 16:26:28 by mcolonna #+# #+# */ /* 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 <fcntl.h>
#include "room_utils.h" #include "room_utils.h"
#include "error.h" #include "error.h"
#include "object.h"
static t_roomcase g_roomcases[] = { static t_roomcase g_roomcases[] = {
{c: '0', surface_spr: CASE_FLOOR}, {c: '0', surface_spr: CASE_FLOOR, object: NULL},
{c: '1', surface_spr: CASE_WALL}, {c: '1', surface_spr: CASE_WALL, object: NULL},
{c: 'P', surface_spr: CASE_FLOOR, object: snas_init},
{c: '\0'} {c: '\0'}
}; };