From 8076487071c75700543ba127182680a19120c212 Mon Sep 17 00:00:00 2001 From: mcolonna Date: Thu, 7 Mar 2024 23:37:34 +0100 Subject: [PATCH] misc: many things --- Makefile | 28 ++++++++-- dev/map.md | 47 +++++++++++++++++ dev/tmp.c | 6 --- includes/{assets.h => data_assets.h} | 27 +++++++--- includes/data_sprites.h | 36 +++++++++++++ includes/display.h | 31 +++-------- includes/env.h | 4 +- includes/error.h | 4 +- includes/room.h | 45 ++++++++++++++++ includes/room_utils.h | 23 ++++++++ includes/sprite.h | 29 ++++++++++ libtf/Makefile | 10 ++-- libtf/include/mem_utils.h | 8 ++- libtf/libtf.h | 10 +++- libtf/src/mem.c | 48 +++++++++++++---- libtf/src/mem_utils.c | 16 +++++- room/room.ber | 5 ++ src/{assets.c => data_assets.c} | 8 +-- src/data_sprites.c | 18 +++++++ src/display1.c | 4 +- src/error.c | 27 +++++++++- src/main.c | 8 +-- src/room.c | 79 ++++++++++++++++++++++++++++ src/room_utils.c | 73 +++++++++++++++++++++++++ src/sprite.c | 33 ++++++++++++ 25 files changed, 554 insertions(+), 73 deletions(-) create mode 100644 dev/map.md delete mode 100644 dev/tmp.c rename includes/{assets.h => data_assets.h} (80%) create mode 100644 includes/data_sprites.h create mode 100644 includes/room.h create mode 100644 includes/room_utils.h create mode 100644 includes/sprite.h create mode 100644 room/room.ber rename src/{assets.c => data_assets.c} (93%) create mode 100644 src/data_sprites.c create mode 100644 src/room.c create mode 100644 src/room_utils.c create mode 100644 src/sprite.c diff --git a/Makefile b/Makefile index 677643d..67f2544 100644 --- a/Makefile +++ b/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) diff --git a/dev/map.md b/dev/map.md new file mode 100644 index 0000000..60d09fa --- /dev/null +++ b/dev/map.md @@ -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()` diff --git a/dev/tmp.c b/dev/tmp.c deleted file mode 100644 index 0852c03..0000000 --- a/dev/tmp.c +++ /dev/null @@ -1,6 +0,0 @@ -#include "mlx.h" - -int main() -{ - mlx_xpm_file_to_img("../assets/object/snas/down.xpm"); -} diff --git a/includes/assets.h b/includes/data_assets.h similarity index 80% rename from includes/assets.h rename to includes/data_assets.h index c5421ae..aed8bec 100644 --- a/includes/assets.h +++ b/includes/data_assets.h @@ -1,23 +1,38 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* assets.h :+: :+: :+: */ +/* data_assets.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 -# 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 diff --git a/includes/data_sprites.h b/includes/data_sprites.h new file mode 100644 index 0000000..c80146d --- /dev/null +++ b/includes/data_sprites.h @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* data_sprites.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mcolonna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 diff --git a/includes/display.h b/includes/display.h index 4c09379..03957bd 100644 --- a/includes/display.h +++ b/includes/display.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 # 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. diff --git a/includes/env.h b/includes/env.h index aef242e..9f450cc 100644 --- a/includes/env.h +++ b/includes/env.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; diff --git a/includes/error.h b/includes/error.h index 69e88a4..83b70da 100644 --- a/includes/error.h +++ b/includes/error.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/includes/room.h b/includes/room.h new file mode 100644 index 0000000..eb6fbb5 --- /dev/null +++ b/includes/room.h @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* room.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mcolonna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 diff --git a/includes/room_utils.h b/includes/room_utils.h new file mode 100644 index 0000000..53c04a9 --- /dev/null +++ b/includes/room_utils.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* room_utils.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mcolonna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 diff --git a/includes/sprite.h b/includes/sprite.h new file mode 100644 index 0000000..0143ddc --- /dev/null +++ b/includes/sprite.h @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sprite.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mcolonna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 diff --git a/libtf/Makefile b/libtf/Makefile index 4dc5e3e..eff7697 100644 --- a/libtf/Makefile +++ b/libtf/Makefile @@ -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 diff --git a/libtf/include/mem_utils.h b/libtf/include/mem_utils.h index 547d47f..eddc4a8 100644 --- a/libtf/include/mem_utils.h +++ b/libtf/include/mem_utils.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/libtf/libtf.h b/libtf/libtf.h index ff15467..953cae3 100644 --- a/libtf/libtf.h +++ b/libtf/libtf.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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. diff --git a/libtf/src/mem.c b/libtf/src/mem.c index 5e1219f..cef6d54 100644 --- a/libtf/src/mem.c +++ b/libtf/src/mem.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } diff --git a/libtf/src/mem_utils.c b/libtf/src/mem_utils.c index ec73d71..70ddf35 100644 --- a/libtf/src/mem_utils.c +++ b/libtf/src/mem_utils.c @@ -6,12 +6,23 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 *)); } diff --git a/room/room.ber b/room/room.ber new file mode 100644 index 0000000..e87449e --- /dev/null +++ b/room/room.ber @@ -0,0 +1,5 @@ +11111 +10011 +11111 +10001 +11111 diff --git a/src/assets.c b/src/data_assets.c similarity index 93% rename from src/assets.c rename to src/data_assets.c index 27f548b..edc2799 100644 --- a/src/assets.c +++ b/src/data_assets.c @@ -1,21 +1,21 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* assets.c :+: :+: :+: */ +/* data_assets.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/src/data_sprites.c b/src/data_sprites.c new file mode 100644 index 0000000..f00e0f5 --- /dev/null +++ b/src/data_sprites.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* data_sprites.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mcolonna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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} +}; diff --git a/src/display1.c b/src/display1.c index c9279a3..606b2ca 100644 --- a/src/display1.c +++ b/src/display1.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); diff --git a/src/error.c b/src/error.c index 9edbec6..8892638 100644 --- a/src/error.c +++ b/src/error.c @@ -6,11 +6,13 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 +#include +#include #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); } diff --git a/src/main.c b/src/main.c index 105dc45..cb193df 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); diff --git a/src/room.c b/src/room.c new file mode 100644 index 0000000..cd91fd0 --- /dev/null +++ b/src/room.c @@ -0,0 +1,79 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* room.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mcolonna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/07 23:31:22 by mcolonna #+# #+# */ +/* Updated: 2024/03/13 16:37:06 by mcolonna ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#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); +} diff --git a/src/room_utils.c b/src/room_utils.c new file mode 100644 index 0000000..fd5130a --- /dev/null +++ b/src/room_utils.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* room_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mcolonna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/13 16:26:28 by mcolonna #+# #+# */ +/* Updated: 2024/03/13 16:55:56 by mcolonna ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#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); +} diff --git a/src/sprite.c b/src/sprite.c new file mode 100644 index 0000000..b3cecc7 --- /dev/null +++ b/src/sprite.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sprite.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mcolonna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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; +}