feat: reorganize the code

This commit is contained in:
mcolonna 2024-04-02 14:45:03 +02:00
parent a00a02e527
commit 5103cbf709
40 changed files with 326 additions and 325 deletions

View file

@ -6,22 +6,16 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/29 17:43:01 by mcolonna #+# #+# */
/* Updated: 2024/03/28 15:09:30 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 17:27:46 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef DATA_ASSETS_H
# define DATA_ASSETS_H
# include <stdint.h>
# include "libtf.h"
# define NB_ASSETS 36
typedef uint32_t t_pixel;
typedef uint8_t t_component;
typedef struct s_asset
typedef struct s_assetmeta
{
t_const_string src;
int limit_left;
@ -29,9 +23,9 @@ typedef struct s_asset
int limit_up;
int limit_down;
bool opaque;
} t_assetinfo;
} t_assetmeta;
extern const t_assetinfo g_assetsmap[NB_ASSETS];
extern const t_assetmeta g_assetsmap[NB_ASSETS];
/**
* Index for each asset. Must be in the same order than g_assetsmap.
@ -39,7 +33,7 @@ extern const t_assetinfo g_assetsmap[NB_ASSETS];
* The images of an object with directions must be in this order:
* down, up, left, right
*/
typedef enum e_assetid
typedef enum e_assetsmap_id
{
FLOOR_1,
FLOOR_2,
@ -77,16 +71,7 @@ typedef enum e_assetid
OBJECT_EXIT_USING2_5,
OBJECT_EXIT_USING2_6,
OBJECT_EXIT_USING2_7
} t_assetid;
typedef enum e_direction
{
DOWN,
UP,
LEFT,
RIGHT,
NO_DIRECTION
} t_direction;
} t_assetsmap_id;
# define OBJECT_SNAS OBJECT_SNAS_DOWN
# define OBJECT_FIRSK OBJECT_FIRSK_DOWN

View file

@ -6,35 +6,33 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/11 15:56:31 by mcolonna #+# #+# */
/* Updated: 2024/04/02 12:43:24 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 17:38:37 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef DATA_SPRITES_H
# define DATA_SPRITES_H
# include "data_assets.h"
# define NB_SPRITES 9
typedef enum e_assettype
typedef enum e_spritetype
{
ANIMATION, // loops through all the sprites
CHARACTER_STILL, // down up left and right positions
} t_assettype;
} t_spritetype;
typedef struct s_spriteinfo
typedef struct s_spritemeta
{
t_assetid first_frame;
int nb_frames;
int origin_x;
int origin_y;
t_assettype type;
} t_spriteinfo;
t_assetsmap_id first_frame;
int nb_frames;
int origin_x;
int origin_y;
t_spritetype type;
} t_spritemeta;
extern const t_spriteinfo g_spritesmap[NB_SPRITES];
extern const t_spritemeta g_spritesmap[NB_SPRITES];
typedef enum e_spriteid
typedef enum e_spritesmap_id
{
CASE_WALL,
CASE_FLOOR_1,
@ -45,6 +43,6 @@ typedef enum e_spriteid
SPR_SNAS_LEFT,
SPR_SNAS_RIGHT,
SPR_WALL,
} t_spriteid;
} t_spritesmap_id;
#endif

View file

@ -6,22 +6,18 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 14:32:08 by mcolonna #+# #+# */
/* Updated: 2024/03/11 15:53:57 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 17:26:51 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef DISPLAY_H
# define DISPLAY_H
# include <stdint.h>
# include "libtf.h"
# include "data_assets.h"
typedef struct s_image
{
int width;
int height;
t_assetinfo asset;
t_assetmeta asset;
void *img;
t_pixel *data;
} t_image;
@ -41,7 +37,7 @@ void display_erase(void);
* You must call display_flush() after drawing all images.
* x,y is the top-left position of the image.
*/
void display_draw(int x, int y, t_assetid asset);
void display_draw(int x, int y, t_assetsmap_id asset);
/**
* Draw all the screen in the buffer.

19
includes/display/pixel.h Normal file
View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pixel.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 17:17:44 by mcolonna #+# #+# */
/* Updated: 2024/04/02 17:18:22 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PIXEL_H
# define PIXEL_H
typedef uint32_t t_pixel;
typedef uint8_t t_component;
#endif

View file

@ -6,23 +6,20 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 22:13:20 by mcolonna #+# #+# */
/* Updated: 2024/03/28 15:59:30 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 17:37:12 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SPRITE_H
# define SPRITE_H
# include "data_assets.h"
# include "data_sprites.h"
typedef struct s_sprite
{
t_spriteinfo *info;
t_spritemeta *meta;
int index;
} t_sprite;
t_sprite sprite_init(t_spriteid spr);
t_sprite sprite_init(t_spritesmap_id spr);
void sprite_draw(int x, int y, t_sprite *spr);

View file

@ -1,35 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* display_utils.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/06 12:59:24 by mcolonna #+# #+# */
/* Updated: 2024/03/06 13:47:02 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef DISPLAY_UTILS_H
# define DISPLAY_UTILS_H
# include "display.h"
extern t_image g_allassets[NB_ASSETS];
extern t_image g_screenbuf;
typedef struct s_imglimits
{
int xmin;
int xmax;
int ymin;
int ymax;
} t_imglimits;
t_pixel *get_data_addr(void *img);
void apply_mask_to_image(void *img, void *mask, int nb_px);
void load_xpm_file_with_alpha(int i);
#endif

49
includes/includes.h Normal file
View file

@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* includes.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 13:53:41 by mcolonna #+# #+# */
/* Updated: 2024/04/02 18:26:03 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef INCLUDES_H
# define INCLUDES_H
# include <fcntl.h>
# include <time.h>
# include <stdbool.h>
# include <string.h>
# include <stdlib.h>
# include <stddef.h>
# include <stdint.h>
# include <X11/keysym.h>
# include <X11/Xlib.h>
# include <errno.h>
# include "libtf.h"
# include "mlx.h"
# include "utils/direction.h"
# include "utils/point.h"
# include "display/pixel.h"
# include "display/data_assets.h"
# include "display/data_sprites.h"
# include "display/display.h"
# include "display/sprite.h"
# include "room/object.h"
# include "room/character.h"
# include "room/objects.h"
# include "room/room.h"
# include "main/env.h"
# include "main/error.h"
# include "main/input.h"
# include "main/timedloop.h"
# include "main/main.h"
#endif

View file

@ -6,16 +6,13 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 15:26:13 by mcolonna #+# #+# */
/* Updated: 2024/03/29 18:27:02 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 14:39:26 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ENV_H
# define ENV_H
# include "libtf.h"
# include "room.h"
# define WINDOW_WIDTH 600
# define WINDOW_HEIGHT 400
# define WINDOW_TITLE "undretale"

View file

@ -6,18 +6,16 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 16:04:31 by mcolonna #+# #+# */
/* Updated: 2024/03/11 13:23:02 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 18:04:00 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ERROR_H
# define ERROR_H
# include "libtf.h"
void err(t_const_string msg);
void err_perror(t_const_string s);
void err_perror_str(t_const_string s, t_const_string msg);
void finish(void);
void error_err(t_const_string msg);
void error_perror(t_const_string s);
void error_str(t_const_string s, t_const_string msg);
void success(void);
#endif

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/26 12:16:32 by mcolonna #+# #+# */
/* Updated: 2024/03/29 18:32:38 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 14:39:38 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */

18
includes/main/main.h Normal file
View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 13:39:54 by mcolonna #+# #+# */
/* Updated: 2024/04/02 14:39:40 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MAIN_H
# define MAIN_H
int main(void);
#endif

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/26 15:02:16 by mcolonna #+# #+# */
/* Updated: 2024/03/26 15:02:50 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 14:39:46 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -1,50 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* object.h :+: :+: :+: */
/* character.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 14:58:25 by mcolonna #+# #+# */
/* Updated: 2024/03/29 15:29:58 by mcolonna ### ########.fr */
/* Created: 2024/04/02 17:46:05 by mcolonna #+# #+# */
/* Updated: 2024/04/02 17:46:46 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef OBJECT_H
# define OBJECT_H
#ifndef CHARACTER_H
# define CHARACTER_H
# include "point.h"
# include "data_assets.h"
# include "libtf.h"
# include "display.h"
# include "sprite.h"
/**** OBJECT ****/
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;
bool solid;
} t_object;
typedef t_object (*t_object_init)(t_memclass);
/**** CHARACTER ****/
typedef struct s_character_sprites
{
t_spriteid still;
t_spriteid walk_left;
t_spriteid walk_right;
t_spriteid walk_up;
t_spriteid walk_down;
t_spritesmap_id still;
t_spritesmap_id walk_left;
t_spritesmap_id walk_right;
t_spritesmap_id walk_up;
t_spritesmap_id walk_down;
} t_character_sprites;
typedef struct s_character
@ -60,20 +35,4 @@ t_point character_loop(t_character *character, t_point pos,
t_direction (*brain)(void));
t_character character_init(t_character_sprites *sprites);
/**** SNAS ****/
typedef struct s_snas_data
{
t_character character;
} t_snas_data;
t_object snas_init(t_memclass mc);
/**** WALL ****/
typedef struct s_wall_data
{
t_sprite spr;
} t_wall_data;
t_object wall_init(t_memclass mc);
#endif

33
includes/room/object.h Normal file
View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* object.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 14:58:25 by mcolonna #+# #+# */
/* Updated: 2024/04/02 17:49:28 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef OBJECT_H
# define OBJECT_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;
bool solid;
} t_object;
typedef t_object (*t_object_init)(t_memclass);
#endif

32
includes/room/objects.h Normal file
View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* objects.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 17:47:01 by mcolonna #+# #+# */
/* Updated: 2024/04/02 17:48:38 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef OBJECTS_H
# define OBJECTS_H
// SNAS
typedef struct s_snas_data
{
t_character character;
} t_snas_data;
t_object snas_init(t_memclass mc);
// WALL
typedef struct s_wall_data
{
t_sprite spr;
} t_wall_data;
t_object wall_init(t_memclass mc);
#endif

View file

@ -6,17 +6,13 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 23:00:45 by mcolonna #+# #+# */
/* Updated: 2024/04/02 12:42:52 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 17:50:58 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ROOM_H
# define ROOM_H
# include "sprite.h"
# include "libtf.h"
# include "object.h"
typedef struct s_room
{
int width;
@ -26,12 +22,6 @@ typedef struct s_room
t_memclass mc;
} t_room;
typedef struct s_roomcase
{
char c;
t_object_init object;
} t_roomcase;
t_room room_fromfile(t_const_string path);
void room_loop(t_room room);

View file

@ -1,26 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* room_utils.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 16:34:01 by mcolonna #+# #+# */
/* Updated: 2024/04/02 12:32:46 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ROOM_UTILS_H
# define ROOM_UTILS_H
# include "libtf.h"
# include "room.h"
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_patch(t_room *room);
#endif

View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* direction.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 17:28:40 by mcolonna #+# #+# */
/* Updated: 2024/04/02 17:28:56 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef DIRECTION_H
# define DIRECTION_H
typedef enum e_direction
{
DOWN,
UP,
LEFT,
RIGHT,
NO_DIRECTION
} t_direction;
#endif

View file

@ -6,15 +6,13 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 15:05:03 by mcolonna #+# #+# */
/* Updated: 2024/03/27 18:22:51 by mcolonna ### ########.fr */
/* Updated: 2024/04/02 14:39:41 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef POINT_H
# define POINT_H
# include "data_assets.h"
typedef struct s_point
{
int x;