misc: many things
This commit is contained in:
parent
664b18eb2e
commit
8076487071
25 changed files with 554 additions and 73 deletions
28
Makefile
28
Makefile
|
@ -2,36 +2,54 @@
|
|||
NAME = so_long
|
||||
SRCS = src/
|
||||
INCLUDES = includes/
|
||||
CODE = main assets display1 display2 display_utils env error
|
||||
CODE = main error env \
|
||||
data_assets data_sprites \
|
||||
display1 display2 display_utils sprite \
|
||||
room room_utils
|
||||
LIBRARIES = mlx libtf
|
||||
LIBRARIES_FILES = libtf/libtf.a
|
||||
LIBRARIES_LINK = mlx
|
||||
LINK=Xext X11 m z
|
||||
LINK = Xext X11 m z
|
||||
|
||||
# It works and I probably won't change it for the rest of my life
|
||||
C_FILES = $(addsuffix .c,$(addprefix $(SRCS),$(CODE)))
|
||||
O_FILES = $(addsuffix .o,$(addprefix $(SRCS),$(CODE)))
|
||||
ifdef DEBUG
|
||||
CC = cc -Wall -Wextra -Werror -g
|
||||
else
|
||||
CC = cc -Wall -Wextra -Werror
|
||||
endif
|
||||
|
||||
all : $(NAME)
|
||||
|
||||
$(NAME) : $(O_FILES) $(LIBRARIES)
|
||||
$(CC) -o $(NAME) $(O_FILES) $(addprefix -L ,$(LIBRARIES_LINK)) $(addprefix -l ,$(LIBRARIES_LINK) $(LINK)) $(LIBRARIES_FILES)
|
||||
@echo "\t\t \e[0;92m\(^o^)/ \e[0;102;30;1m $(NAME) compiled! \e[0;92m \(^o^)/\e[0m"
|
||||
|
||||
$(LIBRARIES) :
|
||||
ifdef DEBUG
|
||||
make -C $@ debug || make -C $@
|
||||
else
|
||||
make -C $@
|
||||
endif
|
||||
@echo "\e[30;47;1m library $@ made! \e[0m"
|
||||
|
||||
%.o : %.c
|
||||
$(CC) $(addprefix -I ,$(INCLUDES) $(LIBRARIES)) -c $< -o $@
|
||||
|
||||
clean :
|
||||
-true $(addprefix && make clean -C ,$(LIBRARIES))
|
||||
-rm -f $(O_FILES)
|
||||
@echo "\e[30;47;1m clean finished! \e[0m"
|
||||
|
||||
fclean : clean
|
||||
-true $(addprefix && make fclean -C ,$(LIBRARIES))
|
||||
$(foreach lib,$(LIBRARIES), make fclean -C $(lib) || make clean -C $(lib); )
|
||||
-rm -f $(NAME)
|
||||
@echo "\e[30;47;1m fclean finished! \e[0m"
|
||||
|
||||
|
||||
re : fclean all
|
||||
|
||||
.PHONY : all clean fclean re $(LIBRARIES)
|
||||
debug :
|
||||
make all DEBUG=yes
|
||||
|
||||
.PHONY : all debug clean fclean re $(LIBRARIES)
|
||||
|
|
47
dev/map.md
Normal file
47
dev/map.md
Normal file
|
@ -0,0 +1,47 @@
|
|||
data\_assets
|
||||
- `NB_ASSETS`
|
||||
- `t_assetinfo`
|
||||
- `g_assetsmap`
|
||||
- `t_assetid`...
|
||||
- `t_direction`
|
||||
- `t_pixel`
|
||||
- `t_component`
|
||||
data\_sprites
|
||||
- `NB_SPRITES`
|
||||
- `t_spriteinfo`
|
||||
- `t_spritesmap`
|
||||
- `t_spriteid`
|
||||
display
|
||||
- `t_image`
|
||||
- `display_init()`
|
||||
- `display_erase()`
|
||||
- `display_draw()`
|
||||
- `display_flush()`
|
||||
- `display_destroyall()`
|
||||
env
|
||||
- `WINDOW_WIDTH`
|
||||
- `WINDOW_HEIGHT`
|
||||
- `WINDOW_TITLE`
|
||||
- `t_env`
|
||||
- `g_env`
|
||||
error
|
||||
- `err()`
|
||||
- `err_perror()`
|
||||
- `finish()`
|
||||
room
|
||||
- `t_surface`...
|
||||
- `t_object`
|
||||
- `t_room`
|
||||
- `t_roomcase`
|
||||
- `room_fromfile()`
|
||||
- `room_draw()`
|
||||
- `room_free()`
|
||||
sprite
|
||||
- `t_sprite`
|
||||
- `sprite_init()`
|
||||
- `sprite_draw()`
|
||||
main
|
||||
- `main()`
|
||||
- `expose_hook()`
|
||||
- `close_hook()`
|
||||
- `loop_hook()`
|
|
@ -1,6 +0,0 @@
|
|||
#include "mlx.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
mlx_xpm_file_to_img("../assets/object/snas/down.xpm");
|
||||
}
|
|
@ -1,23 +1,38 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* assets.h :+: :+: :+: */
|
||||
/* data_assets.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/29 17:43:01 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/03/06 13:42:57 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/03/13 15:43:09 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef ASSETS_H
|
||||
# define ASSETS_H
|
||||
#ifndef DATA_ASSETS_H
|
||||
# define DATA_ASSETS_H
|
||||
|
||||
# include <stdint.h>
|
||||
# include "display.h"
|
||||
# include "libtf.h"
|
||||
|
||||
# define NB_ASSETS 36
|
||||
|
||||
typedef uint32_t t_pixel;
|
||||
typedef uint8_t t_component;
|
||||
|
||||
typedef struct s_asset
|
||||
{
|
||||
t_const_string src;
|
||||
int limit_left;
|
||||
int limit_right;
|
||||
int limit_up;
|
||||
int limit_down;
|
||||
bool opaque;
|
||||
} t_assetinfo;
|
||||
|
||||
extern const t_assetinfo g_assetsmap[NB_ASSETS];
|
||||
|
||||
/**
|
||||
* Index for each asset. Must be in the same order than g_assetsmap.
|
||||
* The frames of an animation must be together and sorted.
|
||||
|
@ -61,7 +76,7 @@ typedef enum e_assetid
|
|||
OBJECT_EXIT_USING2_4,
|
||||
OBJECT_EXIT_USING2_5,
|
||||
OBJECT_EXIT_USING2_6,
|
||||
OBJECT_EXIT_USING2_7,
|
||||
OBJECT_EXIT_USING2_7
|
||||
} t_assetid;
|
||||
|
||||
typedef enum e_direction
|
36
includes/data_sprites.h
Normal file
36
includes/data_sprites.h
Normal file
|
@ -0,0 +1,36 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* data_sprites.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/11 15:56:31 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/03/11 16:31:33 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef DATA_SPRITES_H
|
||||
# define DATA_SPRITES_H
|
||||
|
||||
# include "data_assets.h"
|
||||
|
||||
# define NB_SPRITES 2
|
||||
|
||||
typedef struct s_spriteinfo
|
||||
{
|
||||
t_assetid first_frame;
|
||||
int nb_frames;
|
||||
int origin_x;
|
||||
int origin_y;
|
||||
} t_spriteinfo;
|
||||
|
||||
extern const t_spriteinfo g_spritesmap[NB_SPRITES];
|
||||
|
||||
typedef enum e_spriteid
|
||||
{
|
||||
CASE_WALL,
|
||||
CASE_FLOOR
|
||||
} t_spriteid;
|
||||
|
||||
#endif
|
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/26 14:32:08 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/03/06 14:20:39 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/03/11 15:53:57 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -15,31 +15,16 @@
|
|||
|
||||
# include <stdint.h>
|
||||
# include "libtf.h"
|
||||
# include "assets.h"
|
||||
|
||||
typedef uint32_t t_pixel;
|
||||
typedef uint8_t t_component;
|
||||
|
||||
typedef struct s_asset
|
||||
{
|
||||
t_const_string src;
|
||||
int limit_left;
|
||||
int limit_right;
|
||||
int limit_up;
|
||||
int limit_down;
|
||||
bool opaque;
|
||||
} t_asset;
|
||||
# include "data_assets.h"
|
||||
|
||||
typedef struct s_image
|
||||
{
|
||||
int width;
|
||||
int height;
|
||||
t_asset asset;
|
||||
void *img;
|
||||
t_pixel *data;
|
||||
} t_image;
|
||||
|
||||
extern const t_asset g_assetsmap[];
|
||||
int width;
|
||||
int height;
|
||||
t_assetinfo asset;
|
||||
void *img;
|
||||
t_pixel *data;
|
||||
} t_image;
|
||||
|
||||
/**
|
||||
* Load all assets at the start of the program and prepare the screen buffer.
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/26 15:26:13 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/02/27 16:47:57 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/03/08 13:54:52 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -14,6 +14,7 @@
|
|||
# define ENV_H
|
||||
|
||||
# include "libtf.h"
|
||||
# include "room.h"
|
||||
|
||||
# define WINDOW_WIDTH 600
|
||||
# define WINDOW_HEIGHT 400
|
||||
|
@ -24,6 +25,7 @@ typedef struct s_env
|
|||
t_memclass mc;
|
||||
void *mlx;
|
||||
void *win;
|
||||
t_room room;
|
||||
} t_env;
|
||||
|
||||
extern t_env g_env;
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/26 16:04:31 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/02/27 16:12:12 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/03/11 13:23:02 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -16,6 +16,8 @@
|
|||
# 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);
|
||||
|
||||
#endif
|
||||
|
|
45
includes/room.h
Normal file
45
includes/room.h
Normal file
|
@ -0,0 +1,45 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* room.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/07 23:00:45 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/03/13 14:11:13 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef ROOM_H
|
||||
# define ROOM_H
|
||||
|
||||
# include "sprite.h"
|
||||
# include "libtf.h"
|
||||
|
||||
typedef struct s_object
|
||||
{
|
||||
t_sprite spr;
|
||||
} t_object;
|
||||
|
||||
typedef struct s_room
|
||||
{
|
||||
int width;
|
||||
int height;
|
||||
t_sprite *surfaces;
|
||||
t_object **objects;
|
||||
t_memclass mc;
|
||||
} t_room;
|
||||
|
||||
typedef struct s_roomcase
|
||||
{
|
||||
char c;
|
||||
t_spriteid surface_spr;
|
||||
} t_roomcase;
|
||||
|
||||
t_room room_fromfile(t_const_string path);
|
||||
|
||||
void room_draw(t_room room);
|
||||
|
||||
void room_free(t_room room);
|
||||
|
||||
#endif
|
23
includes/room_utils.h
Normal file
23
includes/room_utils.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* room_utils.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/13 16:34:01 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/03/13 16:46:44 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);
|
||||
|
||||
#endif
|
29
includes/sprite.h
Normal file
29
includes/sprite.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sprite.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/07 22:13:20 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/03/11 16:26:48 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef SPRITE_H
|
||||
# define SPRITE_H
|
||||
|
||||
# include "data_assets.h"
|
||||
# include "data_sprites.h"
|
||||
|
||||
typedef struct s_sprite
|
||||
{
|
||||
const t_spriteinfo *info;
|
||||
int index;
|
||||
} t_sprite;
|
||||
|
||||
t_sprite sprite_init(t_spriteid spr);
|
||||
|
||||
void sprite_draw(int x, int y, t_sprite *spr);
|
||||
|
||||
#endif
|
|
@ -11,7 +11,7 @@ PRINTF_CODE = \
|
|||
ft_strjoin stream1 stream2 utils1 utils2
|
||||
PRINTF_SRCS = tf_printf/src/
|
||||
PRINTF_INCLUDES = ./ tf_printf/include/
|
||||
WHAT = LIBTF v4.2
|
||||
WHAT = LIBTF v7
|
||||
USED = malloc() free() write()
|
||||
|
||||
# It works and I probably won't change it for the rest of my life
|
||||
|
@ -21,18 +21,16 @@ PRINTF_C_FILES = $(addsuffix .c,$(addprefix $(PRINTF_SRCS),$(PRINTF_CODE)))
|
|||
PRINTF_O_FILES = $(addsuffix .o,$(addprefix $(PRINTF_SRCS),$(PRINTF_CODE)))
|
||||
ifdef DEBUG
|
||||
CC = cc -Wall -Wextra -Werror -g
|
||||
FINISH_MSG = "\e[7;33m $(WHAT) compiled! (debug) \e[0m"
|
||||
FINISH_MSG = "| $(WHAT) compiled! (debug)"
|
||||
else
|
||||
CC = cc -Wall -Wextra -Werror
|
||||
FINISH_MSG = "\e[7;36m $(WHAT) compiled! \e[0m"
|
||||
FINISH_MSG = "| $(WHAT) compiled!"
|
||||
endif
|
||||
AR = ar rcs
|
||||
|
||||
all : $(NAME)
|
||||
@echo ""
|
||||
@echo $(FINISH_MSG)
|
||||
@echo "\e[36mFunctions used: $(USED) \e[0m"
|
||||
@echo ""
|
||||
@echo "| Functions used: $(USED)"
|
||||
|
||||
debug :
|
||||
make DEBUG=yes
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/18 16:37:23 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/01/23 18:13:29 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/03/11 14:03:48 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -20,9 +20,12 @@
|
|||
typedef struct s_memclass_in *t_memclass_in;
|
||||
typedef struct s_element t_element;
|
||||
|
||||
typedef struct s_memclass_in *t_memclass_in;
|
||||
|
||||
typedef struct s_element
|
||||
{
|
||||
void *address;
|
||||
t_memclass_in subclass;
|
||||
t_element *previous;
|
||||
t_element *next;
|
||||
t_memclass_in mc;
|
||||
|
@ -31,6 +34,7 @@ typedef struct s_element
|
|||
typedef struct s_memclass_in
|
||||
{
|
||||
t_element *first;
|
||||
t_element *parent_element;
|
||||
} *t_memclass_in;
|
||||
|
||||
void *create_address_with_element(t_element *element, size_t size);
|
||||
|
@ -39,4 +43,6 @@ t_element *get_address_element(void *address);
|
|||
|
||||
void free_address_with_element(void *address);
|
||||
|
||||
void freeelement(t_element *el);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/18 11:07:59 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/02/23 14:54:14 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/03/11 12:12:03 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -73,6 +73,14 @@ typedef void *t_memclass;
|
|||
*/
|
||||
t_memclass mem_newclass(t_err *err);
|
||||
|
||||
/**
|
||||
* Create a t_memclass and add it to parent.
|
||||
* It means if we freeall(parent), this mc is freeall too.
|
||||
* Possible errors: "alloc error"
|
||||
* Return NULL on error.
|
||||
*/
|
||||
t_memclass mem_subclass(t_err *err, t_memclass parent);
|
||||
|
||||
/**
|
||||
* Alloc memory of the class 'mc' and of the size 'size'.
|
||||
* If mc is NULL, do nothing.
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/18 11:08:16 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/02/23 13:27:46 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/03/11 14:03:06 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -18,10 +18,11 @@ t_memclass mem_newclass(t_err *err)
|
|||
{
|
||||
t_memclass_in r;
|
||||
|
||||
r = malloc(sizeof(t_memclass_in));
|
||||
r = malloc(sizeof(struct s_memclass_in));
|
||||
if (!r)
|
||||
return ((*err)("alloc error"), NULL);
|
||||
r->first = NULL;
|
||||
r->parent_element = NULL;
|
||||
return (r);
|
||||
}
|
||||
|
||||
|
@ -37,8 +38,9 @@ void *mem_alloc(t_err *err, t_memclass mc, size_t size)
|
|||
return ((*err)("alloc error"), NULL);
|
||||
r = create_address_with_element(new_element, size);
|
||||
if (!r)
|
||||
return ((*err)("alloc error"), NULL);
|
||||
return (free(new_element), (*err)("alloc error"), NULL);
|
||||
new_element->address = r;
|
||||
new_element->subclass = NULL;
|
||||
new_element->next = mc_in->first;
|
||||
if (new_element->next)
|
||||
new_element->next->previous = new_element;
|
||||
|
@ -60,9 +62,14 @@ void mem_freeall(t_memclass mc)
|
|||
{
|
||||
next = el->next;
|
||||
free_address_with_element(el->address);
|
||||
free(el);
|
||||
if (el->subclass)
|
||||
mem_freeall(el->subclass);
|
||||
else
|
||||
free(el);
|
||||
el = next;
|
||||
}
|
||||
if (((t_memclass_in) mc)->parent_element)
|
||||
freeelement(((t_memclass_in) mc)->parent_element);
|
||||
free(mc);
|
||||
}
|
||||
|
||||
|
@ -74,11 +81,30 @@ void mem_free(void *address)
|
|||
return ;
|
||||
el = get_address_element(address);
|
||||
free_address_with_element(address);
|
||||
if (el->previous)
|
||||
el->previous->next = el->next;
|
||||
else
|
||||
el->mc->first = el->next;
|
||||
if (el->next)
|
||||
el->next->previous = el->previous;
|
||||
free(el);
|
||||
freeelement(el);
|
||||
}
|
||||
|
||||
t_memclass mem_subclass(t_err *err, t_memclass parent)
|
||||
{
|
||||
t_memclass_in r;
|
||||
t_memclass_in parent_in;
|
||||
t_element *new_element;
|
||||
|
||||
parent_in = (t_memclass_in) parent;
|
||||
r = mem_newclass(err);
|
||||
if (!r)
|
||||
return (err("alloc error"), NULL);
|
||||
new_element = malloc(sizeof(t_element));
|
||||
if (!new_element)
|
||||
return (err("alloc error"), mem_freeall(r), NULL);
|
||||
new_element->address = NULL;
|
||||
new_element->subclass = r;
|
||||
new_element->next = parent_in->first;
|
||||
if (new_element->next)
|
||||
new_element->next->previous = new_element;
|
||||
new_element->previous = NULL;
|
||||
new_element->mc = parent;
|
||||
parent_in->first = new_element;
|
||||
r->parent_element = new_element;
|
||||
return ((t_memclass) r);
|
||||
}
|
||||
|
|
|
@ -6,12 +6,23 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/18 16:36:49 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/01/23 18:16:18 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/03/11 14:03:20 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "mem_utils.h"
|
||||
|
||||
void freeelement(t_element *el)
|
||||
{
|
||||
if (el->previous)
|
||||
el->previous->next = el->next;
|
||||
else
|
||||
el->mc->first = el->next;
|
||||
if (el->next)
|
||||
el->next->previous = el->previous;
|
||||
free(el);
|
||||
}
|
||||
|
||||
void *create_address_with_element(t_element *element, size_t size)
|
||||
{
|
||||
void *r;
|
||||
|
@ -30,5 +41,6 @@ t_element *get_address_element(void *address)
|
|||
|
||||
void free_address_with_element(void *address)
|
||||
{
|
||||
free((char *)address - sizeof(t_element *));
|
||||
if (address)
|
||||
free((char *)address - sizeof(t_element *));
|
||||
}
|
||||
|
|
5
room/room.ber
Normal file
5
room/room.ber
Normal file
|
@ -0,0 +1,5 @@
|
|||
11111
|
||||
10011
|
||||
11111
|
||||
10001
|
||||
11111
|
|
@ -1,21 +1,21 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* assets.c :+: :+: :+: */
|
||||
/* data_assets.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/26 16:27:06 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/02/29 18:05:41 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/03/11 15:54:15 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "display.h"
|
||||
#include "data_assets.h"
|
||||
|
||||
/**
|
||||
* Path of each asset relative to assets/, without extension.
|
||||
*/
|
||||
const t_asset g_assetsmap[NB_ASSETS] = {
|
||||
const t_assetinfo g_assetsmap[NB_ASSETS] = {
|
||||
{"floor/1", 50, 100, 50, 100, true},
|
||||
{"floor/2", 50, 100, 50, 100, true},
|
||||
// WALL
|
18
src/data_sprites.c
Normal file
18
src/data_sprites.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* data_sprites.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/11 16:02:29 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/03/11 16:37:05 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "data_sprites.h"
|
||||
|
||||
const t_spriteinfo g_spritesmap[NB_SPRITES] = {
|
||||
{WALL_BORDER, 1, 50, 50},
|
||||
{FLOOR_1, 1, 50, 50}
|
||||
};
|
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/26 15:05:30 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/03/06 13:08:51 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/03/11 13:03:37 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -29,6 +29,8 @@ void display_init(void)
|
|||
i++;
|
||||
}
|
||||
g_screenbuf.img = mlx_new_image(g_env.mlx, WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||
if (!g_screenbuf.img)
|
||||
err("on mlx_new_image()");
|
||||
g_screenbuf.width = WINDOW_WIDTH;
|
||||
g_screenbuf.height = WINDOW_HEIGHT;
|
||||
g_screenbuf.data = get_data_addr(g_screenbuf.img);
|
||||
|
|
27
src/error.c
27
src/error.c
|
@ -6,11 +6,13 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/26 16:05:21 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/02/29 18:04:54 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/03/11 13:37:12 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include "error.h"
|
||||
#include "libtf.h"
|
||||
#include "display.h"
|
||||
|
@ -25,7 +27,28 @@ static void end(int no)
|
|||
|
||||
void err(t_const_string msg)
|
||||
{
|
||||
print_str(err_remember, 2, "error: ");
|
||||
print_line(err_remember, 2, "Error");
|
||||
if (str_eq(msg, "errno"))
|
||||
print_line(err_remember, 2, strerror(errno));
|
||||
else
|
||||
print_line(err_remember, 2, msg);
|
||||
end(1);
|
||||
}
|
||||
|
||||
void err_perror(t_const_string s)
|
||||
{
|
||||
print_line(err_remember, 2, "Error");
|
||||
print_str(err_remember, 2, s);
|
||||
print_str(err_remember, 2, ": ");
|
||||
print_line(err_remember, 2, strerror(errno));
|
||||
end(1);
|
||||
}
|
||||
|
||||
void err_perror_str(t_const_string s, t_const_string msg)
|
||||
{
|
||||
print_line(err_remember, 2, "Error");
|
||||
print_str(err_remember, 2, s);
|
||||
print_str(err_remember, 2, ": ");
|
||||
print_line(err_remember, 2, msg);
|
||||
end(1);
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/26 15:28:34 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/03/06 16:18:09 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/03/11 15:50:28 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -14,7 +14,8 @@
|
|||
#include "env.h"
|
||||
#include "error.h"
|
||||
#include "display.h"
|
||||
#include "assets.h"
|
||||
#include "data_assets.h"
|
||||
#include "room.h"
|
||||
|
||||
int expose_hook(void)
|
||||
{
|
||||
|
@ -35,7 +36,6 @@ int loop_hook(void)
|
|||
|
||||
int main(void)
|
||||
{
|
||||
g_env.mc = NULL;
|
||||
g_env.mc = mem_newclass(err);
|
||||
g_env.mlx = mlx_init();
|
||||
if (!g_env.mlx)
|
||||
|
@ -45,6 +45,8 @@ int main(void)
|
|||
WINDOW_TITLE);
|
||||
if (!g_env.win)
|
||||
err("mlx_new_window() failed");
|
||||
g_env.room = room_fromfile("room/room.ber");
|
||||
room_draw(g_env.room);
|
||||
mlx_expose_hook(g_env.win, expose_hook, NULL);
|
||||
mlx_hook(g_env.win, 17, 1L << 0, close_hook, NULL);
|
||||
mlx_loop_hook(g_env.mlx, loop_hook, NULL);
|
||||
|
|
79
src/room.c
Normal file
79
src/room.c
Normal file
|
@ -0,0 +1,79 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* room.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/07 23:31:22 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/03/13 16:37:06 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <fcntl.h>
|
||||
#include "room.h"
|
||||
#include "error.h"
|
||||
#include "libtf.h"
|
||||
#include "env.h"
|
||||
#include "room_utils.h"
|
||||
|
||||
static bool room_fromfile2(
|
||||
int fd, t_memclass mc, t_const_string path, t_sprite **surface)
|
||||
{
|
||||
t_const_string line;
|
||||
|
||||
line = read_line(err_remember, mc, fd);
|
||||
if (err_get())
|
||||
err_perror(path);
|
||||
if (!line)
|
||||
return (false);
|
||||
while (*line && *line != '\n')
|
||||
{
|
||||
**surface = sprite_init(getroomcase(path, *line)->surface_spr);
|
||||
line++;
|
||||
(*surface)++;
|
||||
}
|
||||
return (true);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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))
|
||||
;
|
||||
mem_freeall(mc);
|
||||
return (r);
|
||||
}
|
||||
|
||||
void room_draw(t_room room)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
|
||||
y = -1;
|
||||
while (++y < room.height)
|
||||
{
|
||||
x = -1;
|
||||
while (++x < room.width)
|
||||
{
|
||||
sprite_draw(x * 50, y * 50, room.surfaces + y * room.width + x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void room_free(t_room room)
|
||||
{
|
||||
mem_freeall(room.mc);
|
||||
}
|
73
src/room_utils.c
Normal file
73
src/room_utils.c
Normal file
|
@ -0,0 +1,73 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* room_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/13 16:26:28 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/03/13 16:55:56 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <fcntl.h>
|
||||
#include "room_utils.h"
|
||||
#include "error.h"
|
||||
|
||||
static t_roomcase g_roomcases[] = {
|
||||
{c: '0', surface_spr: CASE_FLOOR},
|
||||
{c: '1', surface_spr: CASE_WALL},
|
||||
{c: '\0'}
|
||||
};
|
||||
|
||||
int getlinelen(t_const_string line)
|
||||
{
|
||||
int r;
|
||||
|
||||
r = str_len(line);
|
||||
if (line[r - 1] == '\n')
|
||||
r--;
|
||||
return (r);
|
||||
}
|
||||
|
||||
void room_getsize(t_memclass mc, t_room *r, t_const_string path)
|
||||
{
|
||||
int fd;
|
||||
t_const_string line;
|
||||
|
||||
fd = open(path, O_RDONLY);
|
||||
if (fd == -1)
|
||||
err_perror(path);
|
||||
line = read_line(err_remember, mc, fd);
|
||||
if (err_get())
|
||||
err_perror(path);
|
||||
if (!line)
|
||||
err_perror_str(path, "room is empty");
|
||||
r->width = getlinelen(line);
|
||||
r->height = 1;
|
||||
while (true)
|
||||
{
|
||||
line = read_line(err_remember, mc, fd);
|
||||
if (err_get())
|
||||
err_perror(path);
|
||||
if (!line)
|
||||
break ;
|
||||
if (getlinelen(line) != r->width)
|
||||
err_perror_str(path, "the lines have different lengths");
|
||||
r->height++;
|
||||
}
|
||||
}
|
||||
|
||||
t_roomcase *getroomcase(t_const_string path, char c)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = -1;
|
||||
while (g_roomcases[++i].c)
|
||||
{
|
||||
if (g_roomcases[i].c == c)
|
||||
return (&g_roomcases[i]);
|
||||
}
|
||||
err_perror_str(path, "unknown char");
|
||||
return (NULL);
|
||||
}
|
33
src/sprite.c
Normal file
33
src/sprite.c
Normal file
|
@ -0,0 +1,33 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sprite.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/07 22:28:52 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/03/11 16:36:45 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "sprite.h"
|
||||
#include "display.h"
|
||||
|
||||
t_sprite sprite_init(t_spriteid id)
|
||||
{
|
||||
t_sprite r;
|
||||
|
||||
r.info = g_spritesmap + id;
|
||||
r.index = 0;
|
||||
return (r);
|
||||
}
|
||||
|
||||
void sprite_draw(int x, int y, t_sprite *spr)
|
||||
{
|
||||
display_draw(
|
||||
x - spr->info->origin_x, y - spr->info->origin_y,
|
||||
spr->info->first_frame + spr->index);
|
||||
spr->index++;
|
||||
if (spr->index >= spr->info->nb_frames)
|
||||
spr->index = 0;
|
||||
}
|
Loading…
Add table
Reference in a new issue