From 0dcba6ff7e68ed13f8e6caadd80b77506b917050 Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 11 Jan 2020 10:38:41 +0100 Subject: Hardcore refactoring --- Makefile | 8 +- cub3d.h | 97 +++++++++++---------- event.c | 4 +- helper.c | 28 ++++++ libft | 2 +- main.c | 39 ++------- miniLibX_man/mlx.1 | 115 ------------------------- miniLibX_man/mlx_loop.1 | 143 ------------------------------- miniLibX_man/mlx_new_image.1 | 192 ------------------------------------------ miniLibX_man/mlx_new_window.1 | 79 ----------------- miniLibX_man/mlx_pixel_put.1 | 81 ------------------ minilibx_man/mlx.1 | 115 +++++++++++++++++++++++++ minilibx_man/mlx_loop.1 | 143 +++++++++++++++++++++++++++++++ minilibx_man/mlx_new_image.1 | 192 ++++++++++++++++++++++++++++++++++++++++++ minilibx_man/mlx_new_window.1 | 79 +++++++++++++++++ minilibx_man/mlx_pixel_put.1 | 81 ++++++++++++++++++ parse/parse.c | 71 +++++++++------- parse/parse_ceilling_color.c | 27 ------ parse/parse_color.c | 55 ++++++++++++ parse/parse_east_texture.c | 19 ----- parse/parse_floor_color.c | 27 ------ parse/parse_north_texture.c | 19 ----- parse/parse_resolution.c | 13 +-- parse/parse_south_texture.c | 19 ----- parse/parse_sprite_texture.c | 19 ----- parse/parse_textures.c | 48 +++++++++++ parse/parse_west_texture.c | 19 ----- render.c | 30 +++---- state.c | 133 ++++++++++++++++++----------- 29 files changed, 952 insertions(+), 945 deletions(-) create mode 100644 helper.c delete mode 100644 miniLibX_man/mlx.1 delete mode 100644 miniLibX_man/mlx_loop.1 delete mode 100644 miniLibX_man/mlx_new_image.1 delete mode 100644 miniLibX_man/mlx_new_window.1 delete mode 100644 miniLibX_man/mlx_pixel_put.1 create mode 100644 minilibx_man/mlx.1 create mode 100644 minilibx_man/mlx_loop.1 create mode 100644 minilibx_man/mlx_new_image.1 create mode 100644 minilibx_man/mlx_new_window.1 create mode 100644 minilibx_man/mlx_pixel_put.1 delete mode 100644 parse/parse_ceilling_color.c create mode 100644 parse/parse_color.c delete mode 100644 parse/parse_east_texture.c delete mode 100644 parse/parse_floor_color.c delete mode 100644 parse/parse_north_texture.c delete mode 100644 parse/parse_south_texture.c delete mode 100644 parse/parse_sprite_texture.c create mode 100644 parse/parse_textures.c delete mode 100644 parse/parse_west_texture.c diff --git a/Makefile b/Makefile index 84a4fae..fbdfdb1 100644 --- a/Makefile +++ b/Makefile @@ -8,11 +8,9 @@ LDFLAGS = -L$(LIBFT_PATH) -lft \ -framework OpenGL -framework AppKit -lm NAME = cub3D -SRC = main.c event.c parse/parse.c parse/parse_east_texture.c \ - parse/parse_north_texture.c parse/parse_south_texture.c \ - parse/parse_west_texture.c parse/parse_ceilling_color.c \ - parse/parse_floor_color.c parse/parse_resolution.c \ - parse/parse_sprite_texture.c state.c vector.c render.c +SRC = main.c event.c parse/parse.c parse/parse_textures.c \ + parse/parse_color.c parse/parse_resolution.c \ + state.c vector.c render.c helper.c error.c OBJ = $(SRC:.c=.o) INCLUDE = cub3d.h diff --git a/cub3d.h b/cub3d.h index 8c5f06c..e6326e4 100644 --- a/cub3d.h +++ b/cub3d.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:40:37 by cacharle #+# #+# */ -/* Updated: 2020/01/10 11:29:16 by cacharle ### ########.fr */ +/* Updated: 2020/01/11 10:24:44 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include @@ -51,10 +51,10 @@ typedef union unsigned int hexcode; struct { - t_byte empty; t_byte r; t_byte g; t_byte b; + t_byte alpha; } rgb; } t_color; @@ -71,21 +71,16 @@ typedef enum typedef t_cell** t_map; -typedef struct +enum { - int resolution_height; - int resolution_width; - char *north_texture_path; - char *south_texture_path; - char *west_texture_path; - char *east_texture_path; - char *sprite_texture_path; - t_color floor_color; - t_color ceilling_color; - t_map map; - int map_width; - int map_height; -} t_parsing; + TEX_NORTH, + TEX_SOUTH, + TEX_WEST, + TEX_EAST, + TEX_SPRITE +}; + +# define TEXTURES_NUM (TEX_SPRITE - TEX_NORTH + 1) typedef struct { @@ -113,21 +108,19 @@ typedef struct s_state int map_height; t_color ceilling_color; t_color floor_color; - t_image window_img; - t_image *north_texture; - t_image *south_texture; - t_image *west_texture; - t_image *east_texture; + t_image window; + char *textures_path[TEXTURES_NUM]; + t_image textures[TEXTURES_NUM]; } t_state; +typedef t_bool (*t_option_parser_func)(t_state *state, char *line); + typedef struct s_option_parser { char *id; t_option_parser_func func; } t_option_parser; -typedef t_bool (*t_option_parser_func)(t_parsing *parsing, char *line); - typedef enum { SIDE_NORTH_SOUTH, @@ -135,42 +128,53 @@ typedef enum } t_side; /* -** parse.c +** parse/parse.c */ -t_parsing *parse(char *filename); +t_state *parse(char *filename); char **get_file_lines(char *filename); -t_bool parse_line(t_parsing *parsing, char *line); -t_parsing *parse_map(t_parsing *parsing, char **lines); +t_bool parse_line(t_state *state, char *line); +t_state *parse_map(t_state *state, char **lines); t_cell *create_map_row(char *line); /* -** parse_*.c +** parse/parse_resolution.c +*/ + +t_bool parse_resolution(t_state *state, char *line); + +/* +** parse/parse_textures.c */ -t_bool parse_resolution(t_parsing *parsing, char *line); -t_bool parse_north_texture(t_parsing *parsing, char *line); -t_bool parse_south_texture(t_parsing *parsing, char *line); -t_bool parse_west_texture(t_parsing *parsing, char *line); -t_bool parse_east_texture(t_parsing *parsing, char *line); -t_bool parse_sprite_texture(t_parsing *parsing, char *line); -t_bool parse_floor_color(t_parsing *parsing, char *line); -t_bool parse_ceilling_color(t_parsing *parsing, char *line); +t_bool parse_north_texture(t_state *state, char *line); +t_bool parse_south_texture(t_state *state, char *line); +t_bool parse_west_texture(t_state *state, char *line); +t_bool parse_east_texture(t_state *state, char *line); +t_bool parse_sprite_texture(t_state *state, char *line); + +/* +** parse/parse_color.c +*/ + +t_bool parse_floor_color(t_state *state, char *line); +t_bool parse_ceilling_color(t_state *state, char *line); /* ** event.c */ -int handle_keydown(int key, void *param); +int event_keydown(int key, void *param); /* -** graphics.c +** state.c */ -t_state *state_new(void *mlx_ptr, void *window_ptr, t_parsing *parsing); -void state_new_player(t_state *state, t_parsing *parsing); -void state_destroy(t_state *state); -t_image *load_texture(char *path); +t_state *state_new(t_state *state); +void state_init_player(t_state *state); +t_state *state_new_empty(void); +void *state_destroy(t_state *state); +void load_texture(void *mlx_ptr, t_image *image, char *path); /* ** render.c @@ -192,6 +196,13 @@ double vector_norm(t_vector v); ** error.c */ -void error_put_usage(void); +void error_put_usage_exit(char *name); + +/* +** helper.c +*/ + +t_bool helper_is_player_cell(t_cell cell); +void helper_free_splited(char **splited); #endif diff --git a/event.c b/event.c index 58c2eac..8c2049c 100644 --- a/event.c +++ b/event.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:39:37 by cacharle #+# #+# */ -/* Updated: 2020/01/10 11:27:56 by cacharle ### ########.fr */ +/* Updated: 2020/01/11 10:10:05 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ #define ROTATE_SIZE (M_PI / 20.0) #define MOVE_SPEED 0.25 -int handle_keydown(int key, void *param) +int event_keydown(int key, void *param) { t_state *state; diff --git a/helper.c b/helper.c new file mode 100644 index 0000000..fb16406 --- /dev/null +++ b/helper.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/11 07:32:20 by cacharle #+# #+# */ +/* Updated: 2020/01/11 10:37:54 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +t_bool helper_is_player_cell(t_cell cell) +{ + return (cell == CELL_LOOK_NORTH || cell == CELL_LOOK_SOUTH || + cell == CELL_LOOK_WEST || cell == CELL_LOOK_EAST); +} + +void helper_free_splited(char **splited) +{ + if (splited == NULL) + return ; + while (splited != NULL) + free(*splited++); + free(splited); +} diff --git a/libft b/libft index 7fbd92d..d0c146c 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 7fbd92d957e5d30d3478b94ec0abd07aacc054bb +Subproject commit d0c146c2274198814e106b17ea1f9461a1b0b81a diff --git a/main.c b/main.c index ef2ec56..754e5de 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:39:39 by cacharle #+# #+# */ -/* Updated: 2020/01/10 10:59:09 by cacharle ### ########.fr */ +/* Updated: 2020/01/11 10:09:51 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,42 +14,21 @@ int main(int argc, char **argv) { - t_parsing *p; - void *mlx_ptr; - void *window_ptr; t_state *state; - if (argc == 3 && ft_strcmp(argv[2], "--save") == 0) - return (save_image()); - else if (argc != 2) + /* if (argc == 3 && ft_strcmp(argv[2], "--save") == 0) */ + /* return (save_image()); */ + /*else*/ + if (argc != 2) error_put_usage_exit(argv[0]); - if ((p = parse(argv[1])) == NULL) - { - ft_putendl_fd("Error: wrong file format", STDERR_FILENO); - return (1); - } - - if ((mlx_ptr = mlx_init()) == NULL) - { - ft_putendl_fd("Error: minilibx init", STDERR_FILENO); - return (1); - } - if ((window_ptr = mlx_new_window(mlx_ptr, p->resolution_width, - p->resolution_height, WINDOW_TITLE)) == NULL) - { - ft_putendl_fd("Error: minilibx window creation", STDERR_FILENO); - return (1); - } - if ((state = state_new(mlx_ptr, window_ptr, p)) == NULL) + if ((state = state_new(parse(argv[1]))) == NULL) { ft_putendl_fd("Error: state creation", STDERR_FILENO); return (1); } - - mlx_hook(window_ptr, 2, (1L<<1), handle_keydown, (void*)state); - mlx_loop_hook(mlx_ptr, graphics_update, (void*)state); - mlx_loop(mlx_ptr); - + mlx_hook(state->window_ptr, 2, (1L << 1), event_keydown, (void*)state); + mlx_loop_hook(state->mlx_ptr, render_update, (void*)state); + mlx_loop(state->mlx_ptr); return (0); } diff --git a/miniLibX_man/mlx.1 b/miniLibX_man/mlx.1 deleted file mode 100644 index cfdac3d..0000000 --- a/miniLibX_man/mlx.1 +++ /dev/null @@ -1,115 +0,0 @@ -.TH MiniLibX 3 "September 19, 2002" -.SH NAME -MiniLibX - Simple Graphical Interface Library for students -.SH SYNOPSYS -#include - -.nf -.I void * -.fi -.B mlx_init -(); - -.SH DESCRIPTION -MiniLibX is an easy way to create graphical software, -without any X-Window/Cocoa programming knowledge. It provides -simple window creation, a drawing tool, image and basic events -management. - -.SH BSD/LINUX X-WINDOW CONCEPT - -X-Window is a network-oriented graphical system for Unix. -It is based on two main parts: -.br -On one side, your software wants to draw something on the screen and/or -get keyboard & mouse entries. -.br -On the other side, the X-Server manages the screen, keyboard and mouse -(It is often refered to as a "display"). -.br -A network connection must be established between these two entities to send -drawing orders (from the software to the X-Server), and keyboard/mouse -events (from the X-Server to the software). - -.SH MACOSX CONCEPT - -The MacOSX operating system handle graphical access to the screen (or "display"). -.br -On one side, your software wants to draw something on the screen and/or -get keyboard & mouse entries. -.br -On the other side, the underlying MacOSX graphical framework that handles -the screen, the windowing system, keyboard and mouse. -.br -A connection between these two entities must be established. - -.SH INCLUDE FILE -.B mlx.h -should be included for a correct use of the MiniLibX API. -It only contains function prototypes, no structure is needed. - -.SH LIBRARY FUNCTIONS -.P -First of all, you need to initialize the connection -between your software and the display. -Once this connection is established, you'll be able to -use other MiniLibX functions to send the graphical orders, -like "I want to draw a yellow pixel in this window" or "did the -user hit a key?". -.P -The -.B mlx_init -function will create this connection. No parameters are needed, ant it will -return a -.I "void *" -identifier, used for further calls to the library routines. -.P -All other MiniLibX functions are described in the following man pages: - -.TP 20 -.B mlx_new_window -: manage windows -.TP 20 -.B mlx_pixel_put -: draw inside window -.TP 20 -.B mlx_new_image -: manipulate images -.TP 20 -.B mlx_loop -: handle keyboard or mouse events - -.SH LINKING MiniLibX on BSD/Linux and X-Window -To use MiniLibX functions, you'll need to link -your software with several libraries, including the MiniLibX library itself. -To do this, simply add the following arguments at linking time: - -.B -lmlx -lXext -lX11 - -You may also need to specify the path to these libraries, using -the -.B -L -flag. - -.SH LINKING MiniLibX on MACOSX -To use MiniLibX functions, you'll need to link your software with the -MiniLibX library, and several system frameworks: - -.B -lmlx -framework OpenGL -framework AppKit - -You may also need to specify the path to the MiniLibX library, using -the -.B -L -flag. - -.SH RETURN VALUES -If -.B mlx_init() -fails to set up the connection to the graphical system, it will return NULL, otherwise -a non-null pointer is returned as a connection identifier. - -.SH SEE ALSO -mlx_new_window(3), mlx_pixel_put(3), mlx_new_image(3), mlx_loop(3) - -.SH AUTHOR -Copyright ol@ - 2002-2015 - Olivier Crouzet diff --git a/miniLibX_man/mlx_loop.1 b/miniLibX_man/mlx_loop.1 deleted file mode 100644 index fa2b835..0000000 --- a/miniLibX_man/mlx_loop.1 +++ /dev/null @@ -1,143 +0,0 @@ -.TH MiniLibX 3 "September 19, 2002" -.SH NAME -MiniLibX - Handle events -.SH SYNOPSYS - -.nf -.I int -.fi -.B mlx_loop -( -.I void *mlx_ptr -); - -.nf -.I int -.fi -.B mlx_key_hook -( -.I void *win_ptr, int (*funct_ptr)(), void *param -); - -.nf -.I int -.fi -.B mlx_mouse_hook -( -.I void *win_ptr, int (*funct_ptr)(), void *param -); - -.nf -.I int -.fi -.B mlx_expose_hook -( -.I void *win_ptr, int (*funct_ptr)(), void *param -); - -.nf -.I int -.fi -.B mlx_loop_hook -( -.I void *mlx_ptr, int (*funct_ptr)(), void *param -); - -.SH EVENTS - -Both X-Window and MacOSX graphical systems are bi-directionnal. -On one hand, the program sends orders to -the screen to display pixels, images, and so on. On the other hand, -it can get information from the keyboard and mouse associated to -the screen. To do so, the program receives "events" from the keyboard or the -mouse. - -.SH DESCRIPTION - -To receive events, you must use -.B mlx_loop -(). This function never returns. It is an infinite loop that waits for -an event, and then calls a user-defined function associated with this event. -A single parameter is needed, the connection identifier -.I mlx_ptr -(see the -.B mlx manual). - -You can assign different functions to the three following events: -.br -- A key is pressed -.br -- The mouse button is pressed -.br -- A part of the window should be re-drawn -(this is called an "expose" event, and it is your program's job to handle it). -.br - -Each window can define a different function for the same event. - -The three functions -.B mlx_key_hook -(), -.B mlx_mouse_hook -() and -.B mlx_expose_hook -() work exactly the same way. -.I funct_ptr -is a pointer to the function you want to be called -when an event occurs. This assignment is specific to the window defined by the -.I win_ptr -identifier. The -.I param -adress will be passed to the function everytime it is called, and should be -used to store the parameters it might need. - -The syntax for the -.B mlx_loop_hook -() function is identical to the previous ones, but the given function will be -called when no event occurs. - -When it catches an event, the MiniLibX calls the corresponding function -with fixed parameters: -.nf - - expose_hook(void *param); - key_hook(int keycode,void *param); - mouse_hook(int button,int x,int y,void *param); - loop_hook(void *param); - -.fi -These function names are arbitrary. They here are used to distinguish -parameters according to the event. These functions are NOT part of the -MiniLibX. - -.I param -is the address specified in the mlx_*_hook calls. This address is never -used nor modified by the MiniLibX. On key and mouse events, additional -information is passed: -.I keycode -tells you which key is pressed (X11 : look for the include file "keysymdef.h", -MacOS : create a small software and find out by yourself), -( -.I x -, -.I y -) are the coordinates of the mouse click in the window, and -.I button -tells you which mouse button was pressed. - -.SH GOING FURTHER WITH EVENTS -The MiniLibX provides a much generic access to all type of events. The -.I mlx.h -include define -.B mlx_hook() -in the same manner mlx_*_hook functions work. The event and mask values -will be taken from the X11 include file "X.h" (even for MacOSX, for compatibility purposes) - -See source code of mlx_int_param_event.c to find out how the MiniLibX will -call your own function for a specific event. - -.SH SEE ALSO -mlx(3), mlx_new_window(3), mlx_pixel_put(3), mlx_new_image(3) - -.SH AUTHOR -Copyright ol@ - 2002-2015 - Olivier Crouzet diff --git a/miniLibX_man/mlx_new_image.1 b/miniLibX_man/mlx_new_image.1 deleted file mode 100644 index d46bf59..0000000 --- a/miniLibX_man/mlx_new_image.1 +++ /dev/null @@ -1,192 +0,0 @@ -.TH MiniLibX 3 "September 19, 2002" -.SH NAME -MiniLibX - Manipulating images -.SH SYNOPSYS - -.nf -.I void * -.fi -.B mlx_new_image -( -.I void *mlx_ptr, int width, int height -); - -.nf -.I char * -.fi -.B mlx_get_data_addr -( -.I void *img_ptr, int *bits_per_pixel, int *size_line, int *endian -); - -.nf -.I int -.fi -.B mlx_put_image_to_window -( -.I void *mlx_ptr, void *win_ptr, void *img_ptr, int x, int y -); - -.nf -.I unsigned int -.fi -.B mlx_get_color_value -( -.I void *mlx_ptr, int color -); - -.nf -.I void * -.fi -.B mlx_xpm_to_image -( -.I void *mlx_ptr, char **xpm_data, int *width, int *height -); - -.nf -.I void * -.fi -.B mlx_xpm_file_to_image -( -.I void *mlx_ptr, char *filename, int *width, int *height -); - -.nf -.I int -.fi -.B mlx_destroy_image -( -.I void *mlx_ptr, void *img_ptr -); - - -.SH DESCRIPTION - -.B mlx_new_image -() creates a new image in memory. It returns a -.I void * -identifier needed to manipulate this image later. It only needs -the size of the image to be created, using the -.I width -and -.I height -parameters, and the -.I mlx_ptr -connection identifier (see the -.B mlx -manual). - -The user can draw inside the image (see below), and -can dump the image inside a specified window at any time to -display it on the screen. This is done using -.B mlx_put_image_to_window -(). Three identifiers are needed here, for the connection to the -display, the window to use, and the image (respectively -.I mlx_ptr -, -.I win_ptr -and -.I img_ptr -). The ( -.I x -, -.I y -) coordinates define where the image should be placed in the window. - -.B mlx_get_data_addr -() returns information about the created image, allowing a user -to modify it later. The -.I img_ptr -parameter specifies the image to use. The three next parameters should -be the addresses of three different valid integers. -.I bits_per_pixel -will be filled with the number of bits needed to represent a pixel color -(also called the depth of the image). -.I size_line -is the number of bytes used to store one line of the image in memory. -This information is needed to move from one line to another in the image. -.I endian -tells you wether the pixel color in the image needs to be stored in -little endian ( -.I endian -== 0), or big endian ( -.I endian -== 1). - -.B mlx_get_data_addr -returns a -.I char * -address that represents the begining of the memory area where the image -is stored. From this adress, the first -.I bits_per_pixel -bits represent the color of the first pixel in the first line of -the image. The second group of -.I bits_per_pixel -bits represent the second pixel of the first line, and so on. -Add -.I size_line -to the adress to get the begining of the second line. You can reach any -pixels of the image that way. - -.B mlx_destroy_image -destroys the given image ( -.I img_ptr -). - -.SH STORING COLOR INSIDE IMAGES - -Depending on the display, the number of bits used to store a pixel color -can change. The user usually represents a color in RGB mode, using -one byte for each component (see -.B mlx_pixel_put -manual). This must be translated to fit the -.I bits_per_pixel -requirement of the image, and make the color understandable to the graphical system. -That is the purpose of the -.B mlx_get_color_value -() function. It takes a standard RGB -.I color -parameter, and returns an -.I unsigned int -value. -The -.I bits_per_pixel -least significant bits of this value can be stored in the image. - -Keep in mind that the least significant bits position depends on the local -computer's endian. If the endian of the image (in fact the endian of -the X-Server's computer for remote X11 display) differs from the local endian, then the value should -be transformed before being used. - -.SH XPM IMAGES - -The -.B mlx_xpm_to_image -() and -.B mlx_xpm_file_to_image -() functions will create a new image the same way. -They will fill it using the specified -.I xpm_data -or -.I filename -, depending on which function is used. -Note that MiniLibX does not use the standard -Xpm library to deal with xpm images. You may not be able to -read all types of xpm images. It however handles transparency. - -.SH RETURN VALUES -The three functions that create images, -.B mlx_new_image() -, -.B mlx_xpm_to_image() -and -.B mlx_xpm_file_to_image() -, will return NULL if an error occurs. Otherwise they return a non-null pointer -as an image identifier. - - -.SH SEE ALSO -mlx(3), mlx_new_window(3), mlx_pixel_put(3), mlx_loop(3) - -.SH AUTHOR -Copyright ol@ - 2002-2015 - Olivier Crouzet diff --git a/miniLibX_man/mlx_new_window.1 b/miniLibX_man/mlx_new_window.1 deleted file mode 100644 index 36176d7..0000000 --- a/miniLibX_man/mlx_new_window.1 +++ /dev/null @@ -1,79 +0,0 @@ -.TH MiniLibX 3 "September 19, 2002" -.SH NAME -MiniLibX - Managing windows -.SH SYNOPSYS - -.nf -.I void * -.fi -.B mlx_new_window -( -.I void *mlx_ptr, int size_x, int size_y, char *title -); - -.nf -.I int -.fi -.B mlx_clear_window -( -.I void *mlx_ptr, void *win_ptr -); - -.nf -.I int -.fi -.B mlx_destroy_window -( -.I void *mlx_ptr, void *win_ptr -); - - -.SH DESCRIPTION -The -.B mlx_new_window -() function creates a new window on the screen, using the -.I size_x -and -.I size_y -parameters to determine its size, and -.I title -as the text that should be displayed in the window's title bar. -The -.I mlx_ptr -parameter is the connection identifier returned by -.B mlx_init -() (see the -.B mlx -man page). -.B mlx_new_window -() returns a -.I void * -window identifier that can be used by other MiniLibX calls. -Note that the MiniLibX -can handle an arbitrary number of separate windows. - -.B mlx_clear_window -() and -.B mlx_destroy_window -() respectively clear (in black) and destroy the given window. They both have -the same parameters: -.I mlx_ptr -is the screen connection identifier, and -.I win_ptr -is a window identifier. - -.SH RETURN VALUES -If -.B mlx_new_window() -fails to create a new window (for wathever reason), it will return NULL, -otherwise a non-null pointer is returned as a window identifier. -.B mlx_clear_window -and -.B mlx_destroy_window -right now return nothing. - -.SH SEE ALSO -mlx(3), mlx_pixel_put(3), mlx_new_image(3), mlx_loop(3) - -.SH AUTHOR -Copyright ol@ - 2002-2015 - Olivier Crouzet diff --git a/miniLibX_man/mlx_pixel_put.1 b/miniLibX_man/mlx_pixel_put.1 deleted file mode 100644 index caf89f2..0000000 --- a/miniLibX_man/mlx_pixel_put.1 +++ /dev/null @@ -1,81 +0,0 @@ -.TH MiniLibX 3 "September 19, 2002" -.SH NAME -MiniLibX - Drawing inside windows -.SH SYNOPSYS - -.nf -.I int -.fi -.B mlx_pixel_put -( -.I void *mlx_ptr, void *win_ptr, int x, int y, int color -); - -.nf -.I int -.fi -.B mlx_string_put -( -.I void *mlx_ptr, void *win_ptr, int x, int y, int color, char *string -); - - -.SH DESCRIPTION -The -.B mlx_pixel_put -() function draws a defined pixel in the window -.I win_ptr -using the ( -.I x -, -.I y -) coordinates, and the specified -.I color -\&. The origin (0,0) is the upper left corner of the window, the x and y axis -respectively pointing right and down. The connection -identifier, -.I mlx_ptr -, is needed (see the -.B mlx -man page). - -Parameters for -.B mlx_string_put -() have the same meaning. Instead of a simple pixel, the specified -.I string -will be displayed at ( -.I x -, -.I y -). - -In both functions, it is impossible to display anything outside the -specified window, nor display in another window in front of the selected one. - -.SH COLOR MANAGEMENT -The -.I color -parameter has an integer type. The displayed color needs to be encoded -in this integer, following a defined scheme. All displayable colors -can be split in 3 basic colors: red, green and blue. Three associated -values, in the 0-255 range, represent how much of each color is mixed up -to create the original color. Theses three values must be set inside the -integer to display the right color. The three least significant bytes of -this integer are filled as shown in the picture below: - -.nf - | 0 | R | G | B | color integer - +---+---+---+---+ -.fi - - -While filling the integer, make sure you avoid endian problems. Remember -that the "blue" byte should always be the least significant one. - - -.SH SEE ALSO -mlx(3), mlx_new_window(3), mlx_new_image(3), mlx_loop(3) - - -.SH AUTHOR -Copyright ol@ - 2002-2015 - Olivier Crouzet diff --git a/minilibx_man/mlx.1 b/minilibx_man/mlx.1 new file mode 100644 index 0000000..cfdac3d --- /dev/null +++ b/minilibx_man/mlx.1 @@ -0,0 +1,115 @@ +.TH MiniLibX 3 "September 19, 2002" +.SH NAME +MiniLibX - Simple Graphical Interface Library for students +.SH SYNOPSYS +#include + +.nf +.I void * +.fi +.B mlx_init +(); + +.SH DESCRIPTION +MiniLibX is an easy way to create graphical software, +without any X-Window/Cocoa programming knowledge. It provides +simple window creation, a drawing tool, image and basic events +management. + +.SH BSD/LINUX X-WINDOW CONCEPT + +X-Window is a network-oriented graphical system for Unix. +It is based on two main parts: +.br +On one side, your software wants to draw something on the screen and/or +get keyboard & mouse entries. +.br +On the other side, the X-Server manages the screen, keyboard and mouse +(It is often refered to as a "display"). +.br +A network connection must be established between these two entities to send +drawing orders (from the software to the X-Server), and keyboard/mouse +events (from the X-Server to the software). + +.SH MACOSX CONCEPT + +The MacOSX operating system handle graphical access to the screen (or "display"). +.br +On one side, your software wants to draw something on the screen and/or +get keyboard & mouse entries. +.br +On the other side, the underlying MacOSX graphical framework that handles +the screen, the windowing system, keyboard and mouse. +.br +A connection between these two entities must be established. + +.SH INCLUDE FILE +.B mlx.h +should be included for a correct use of the MiniLibX API. +It only contains function prototypes, no structure is needed. + +.SH LIBRARY FUNCTIONS +.P +First of all, you need to initialize the connection +between your software and the display. +Once this connection is established, you'll be able to +use other MiniLibX functions to send the graphical orders, +like "I want to draw a yellow pixel in this window" or "did the +user hit a key?". +.P +The +.B mlx_init +function will create this connection. No parameters are needed, ant it will +return a +.I "void *" +identifier, used for further calls to the library routines. +.P +All other MiniLibX functions are described in the following man pages: + +.TP 20 +.B mlx_new_window +: manage windows +.TP 20 +.B mlx_pixel_put +: draw inside window +.TP 20 +.B mlx_new_image +: manipulate images +.TP 20 +.B mlx_loop +: handle keyboard or mouse events + +.SH LINKING MiniLibX on BSD/Linux and X-Window +To use MiniLibX functions, you'll need to link +your software with several libraries, including the MiniLibX library itself. +To do this, simply add the following arguments at linking time: + +.B -lmlx -lXext -lX11 + +You may also need to specify the path to these libraries, using +the +.B -L +flag. + +.SH LINKING MiniLibX on MACOSX +To use MiniLibX functions, you'll need to link your software with the +MiniLibX library, and several system frameworks: + +.B -lmlx -framework OpenGL -framework AppKit + +You may also need to specify the path to the MiniLibX library, using +the +.B -L +flag. + +.SH RETURN VALUES +If +.B mlx_init() +fails to set up the connection to the graphical system, it will return NULL, otherwise +a non-null pointer is returned as a connection identifier. + +.SH SEE ALSO +mlx_new_window(3), mlx_pixel_put(3), mlx_new_image(3), mlx_loop(3) + +.SH AUTHOR +Copyright ol@ - 2002-2015 - Olivier Crouzet diff --git a/minilibx_man/mlx_loop.1 b/minilibx_man/mlx_loop.1 new file mode 100644 index 0000000..fa2b835 --- /dev/null +++ b/minilibx_man/mlx_loop.1 @@ -0,0 +1,143 @@ +.TH MiniLibX 3 "September 19, 2002" +.SH NAME +MiniLibX - Handle events +.SH SYNOPSYS + +.nf +.I int +.fi +.B mlx_loop +( +.I void *mlx_ptr +); + +.nf +.I int +.fi +.B mlx_key_hook +( +.I void *win_ptr, int (*funct_ptr)(), void *param +); + +.nf +.I int +.fi +.B mlx_mouse_hook +( +.I void *win_ptr, int (*funct_ptr)(), void *param +); + +.nf +.I int +.fi +.B mlx_expose_hook +( +.I void *win_ptr, int (*funct_ptr)(), void *param +); + +.nf +.I int +.fi +.B mlx_loop_hook +( +.I void *mlx_ptr, int (*funct_ptr)(), void *param +); + +.SH EVENTS + +Both X-Window and MacOSX graphical systems are bi-directionnal. +On one hand, the program sends orders to +the screen to display pixels, images, and so on. On the other hand, +it can get information from the keyboard and mouse associated to +the screen. To do so, the program receives "events" from the keyboard or the +mouse. + +.SH DESCRIPTION + +To receive events, you must use +.B mlx_loop +(). This function never returns. It is an infinite loop that waits for +an event, and then calls a user-defined function associated with this event. +A single parameter is needed, the connection identifier +.I mlx_ptr +(see the +.B mlx manual). + +You can assign different functions to the three following events: +.br +- A key is pressed +.br +- The mouse button is pressed +.br +- A part of the window should be re-drawn +(this is called an "expose" event, and it is your program's job to handle it). +.br + +Each window can define a different function for the same event. + +The three functions +.B mlx_key_hook +(), +.B mlx_mouse_hook +() and +.B mlx_expose_hook +() work exactly the same way. +.I funct_ptr +is a pointer to the function you want to be called +when an event occurs. This assignment is specific to the window defined by the +.I win_ptr +identifier. The +.I param +adress will be passed to the function everytime it is called, and should be +used to store the parameters it might need. + +The syntax for the +.B mlx_loop_hook +() function is identical to the previous ones, but the given function will be +called when no event occurs. + +When it catches an event, the MiniLibX calls the corresponding function +with fixed parameters: +.nf + + expose_hook(void *param); + key_hook(int keycode,void *param); + mouse_hook(int button,int x,int y,void *param); + loop_hook(void *param); + +.fi +These function names are arbitrary. They here are used to distinguish +parameters according to the event. These functions are NOT part of the +MiniLibX. + +.I param +is the address specified in the mlx_*_hook calls. This address is never +used nor modified by the MiniLibX. On key and mouse events, additional +information is passed: +.I keycode +tells you which key is pressed (X11 : look for the include file "keysymdef.h", +MacOS : create a small software and find out by yourself), +( +.I x +, +.I y +) are the coordinates of the mouse click in the window, and +.I button +tells you which mouse button was pressed. + +.SH GOING FURTHER WITH EVENTS +The MiniLibX provides a much generic access to all type of events. The +.I mlx.h +include define +.B mlx_hook() +in the same manner mlx_*_hook functions work. The event and mask values +will be taken from the X11 include file "X.h" (even for MacOSX, for compatibility purposes) + +See source code of mlx_int_param_event.c to find out how the MiniLibX will +call your own function for a specific event. + +.SH SEE ALSO +mlx(3), mlx_new_window(3), mlx_pixel_put(3), mlx_new_image(3) + +.SH AUTHOR +Copyright ol@ - 2002-2015 - Olivier Crouzet diff --git a/minilibx_man/mlx_new_image.1 b/minilibx_man/mlx_new_image.1 new file mode 100644 index 0000000..d46bf59 --- /dev/null +++ b/minilibx_man/mlx_new_image.1 @@ -0,0 +1,192 @@ +.TH MiniLibX 3 "September 19, 2002" +.SH NAME +MiniLibX - Manipulating images +.SH SYNOPSYS + +.nf +.I void * +.fi +.B mlx_new_image +( +.I void *mlx_ptr, int width, int height +); + +.nf +.I char * +.fi +.B mlx_get_data_addr +( +.I void *img_ptr, int *bits_per_pixel, int *size_line, int *endian +); + +.nf +.I int +.fi +.B mlx_put_image_to_window +( +.I void *mlx_ptr, void *win_ptr, void *img_ptr, int x, int y +); + +.nf +.I unsigned int +.fi +.B mlx_get_color_value +( +.I void *mlx_ptr, int color +); + +.nf +.I void * +.fi +.B mlx_xpm_to_image +( +.I void *mlx_ptr, char **xpm_data, int *width, int *height +); + +.nf +.I void * +.fi +.B mlx_xpm_file_to_image +( +.I void *mlx_ptr, char *filename, int *width, int *height +); + +.nf +.I int +.fi +.B mlx_destroy_image +( +.I void *mlx_ptr, void *img_ptr +); + + +.SH DESCRIPTION + +.B mlx_new_image +() creates a new image in memory. It returns a +.I void * +identifier needed to manipulate this image later. It only needs +the size of the image to be created, using the +.I width +and +.I height +parameters, and the +.I mlx_ptr +connection identifier (see the +.B mlx +manual). + +The user can draw inside the image (see below), and +can dump the image inside a specified window at any time to +display it on the screen. This is done using +.B mlx_put_image_to_window +(). Three identifiers are needed here, for the connection to the +display, the window to use, and the image (respectively +.I mlx_ptr +, +.I win_ptr +and +.I img_ptr +). The ( +.I x +, +.I y +) coordinates define where the image should be placed in the window. + +.B mlx_get_data_addr +() returns information about the created image, allowing a user +to modify it later. The +.I img_ptr +parameter specifies the image to use. The three next parameters should +be the addresses of three different valid integers. +.I bits_per_pixel +will be filled with the number of bits needed to represent a pixel color +(also called the depth of the image). +.I size_line +is the number of bytes used to store one line of the image in memory. +This information is needed to move from one line to another in the image. +.I endian +tells you wether the pixel color in the image needs to be stored in +little endian ( +.I endian +== 0), or big endian ( +.I endian +== 1). + +.B mlx_get_data_addr +returns a +.I char * +address that represents the begining of the memory area where the image +is stored. From this adress, the first +.I bits_per_pixel +bits represent the color of the first pixel in the first line of +the image. The second group of +.I bits_per_pixel +bits represent the second pixel of the first line, and so on. +Add +.I size_line +to the adress to get the begining of the second line. You can reach any +pixels of the image that way. + +.B mlx_destroy_image +destroys the given image ( +.I img_ptr +). + +.SH STORING COLOR INSIDE IMAGES + +Depending on the display, the number of bits used to store a pixel color +can change. The user usually represents a color in RGB mode, using +one byte for each component (see +.B mlx_pixel_put +manual). This must be translated to fit the +.I bits_per_pixel +requirement of the image, and make the color understandable to the graphical system. +That is the purpose of the +.B mlx_get_color_value +() function. It takes a standard RGB +.I color +parameter, and returns an +.I unsigned int +value. +The +.I bits_per_pixel +least significant bits of this value can be stored in the image. + +Keep in mind that the least significant bits position depends on the local +computer's endian. If the endian of the image (in fact the endian of +the X-Server's computer for remote X11 display) differs from the local endian, then the value should +be transformed before being used. + +.SH XPM IMAGES + +The +.B mlx_xpm_to_image +() and +.B mlx_xpm_file_to_image +() functions will create a new image the same way. +They will fill it using the specified +.I xpm_data +or +.I filename +, depending on which function is used. +Note that MiniLibX does not use the standard +Xpm library to deal with xpm images. You may not be able to +read all types of xpm images. It however handles transparency. + +.SH RETURN VALUES +The three functions that create images, +.B mlx_new_image() +, +.B mlx_xpm_to_image() +and +.B mlx_xpm_file_to_image() +, will return NULL if an error occurs. Otherwise they return a non-null pointer +as an image identifier. + + +.SH SEE ALSO +mlx(3), mlx_new_window(3), mlx_pixel_put(3), mlx_loop(3) + +.SH AUTHOR +Copyright ol@ - 2002-2015 - Olivier Crouzet diff --git a/minilibx_man/mlx_new_window.1 b/minilibx_man/mlx_new_window.1 new file mode 100644 index 0000000..36176d7 --- /dev/null +++ b/minilibx_man/mlx_new_window.1 @@ -0,0 +1,79 @@ +.TH MiniLibX 3 "September 19, 2002" +.SH NAME +MiniLibX - Managing windows +.SH SYNOPSYS + +.nf +.I void * +.fi +.B mlx_new_window +( +.I void *mlx_ptr, int size_x, int size_y, char *title +); + +.nf +.I int +.fi +.B mlx_clear_window +( +.I void *mlx_ptr, void *win_ptr +); + +.nf +.I int +.fi +.B mlx_destroy_window +( +.I void *mlx_ptr, void *win_ptr +); + + +.SH DESCRIPTION +The +.B mlx_new_window +() function creates a new window on the screen, using the +.I size_x +and +.I size_y +parameters to determine its size, and +.I title +as the text that should be displayed in the window's title bar. +The +.I mlx_ptr +parameter is the connection identifier returned by +.B mlx_init +() (see the +.B mlx +man page). +.B mlx_new_window +() returns a +.I void * +window identifier that can be used by other MiniLibX calls. +Note that the MiniLibX +can handle an arbitrary number of separate windows. + +.B mlx_clear_window +() and +.B mlx_destroy_window +() respectively clear (in black) and destroy the given window. They both have +the same parameters: +.I mlx_ptr +is the screen connection identifier, and +.I win_ptr +is a window identifier. + +.SH RETURN VALUES +If +.B mlx_new_window() +fails to create a new window (for wathever reason), it will return NULL, +otherwise a non-null pointer is returned as a window identifier. +.B mlx_clear_window +and +.B mlx_destroy_window +right now return nothing. + +.SH SEE ALSO +mlx(3), mlx_pixel_put(3), mlx_new_image(3), mlx_loop(3) + +.SH AUTHOR +Copyright ol@ - 2002-2015 - Olivier Crouzet diff --git a/minilibx_man/mlx_pixel_put.1 b/minilibx_man/mlx_pixel_put.1 new file mode 100644 index 0000000..caf89f2 --- /dev/null +++ b/minilibx_man/mlx_pixel_put.1 @@ -0,0 +1,81 @@ +.TH MiniLibX 3 "September 19, 2002" +.SH NAME +MiniLibX - Drawing inside windows +.SH SYNOPSYS + +.nf +.I int +.fi +.B mlx_pixel_put +( +.I void *mlx_ptr, void *win_ptr, int x, int y, int color +); + +.nf +.I int +.fi +.B mlx_string_put +( +.I void *mlx_ptr, void *win_ptr, int x, int y, int color, char *string +); + + +.SH DESCRIPTION +The +.B mlx_pixel_put +() function draws a defined pixel in the window +.I win_ptr +using the ( +.I x +, +.I y +) coordinates, and the specified +.I color +\&. The origin (0,0) is the upper left corner of the window, the x and y axis +respectively pointing right and down. The connection +identifier, +.I mlx_ptr +, is needed (see the +.B mlx +man page). + +Parameters for +.B mlx_string_put +() have the same meaning. Instead of a simple pixel, the specified +.I string +will be displayed at ( +.I x +, +.I y +). + +In both functions, it is impossible to display anything outside the +specified window, nor display in another window in front of the selected one. + +.SH COLOR MANAGEMENT +The +.I color +parameter has an integer type. The displayed color needs to be encoded +in this integer, following a defined scheme. All displayable colors +can be split in 3 basic colors: red, green and blue. Three associated +values, in the 0-255 range, represent how much of each color is mixed up +to create the original color. Theses three values must be set inside the +integer to display the right color. The three least significant bytes of +this integer are filled as shown in the picture below: + +.nf + | 0 | R | G | B | color integer + +---+---+---+---+ +.fi + + +While filling the integer, make sure you avoid endian problems. Remember +that the "blue" byte should always be the least significant one. + + +.SH SEE ALSO +mlx(3), mlx_new_window(3), mlx_new_image(3), mlx_loop(3) + + +.SH AUTHOR +Copyright ol@ - 2002-2015 - Olivier Crouzet diff --git a/parse/parse.c b/parse/parse.c index 8f3124d..145d5ad 100644 --- a/parse/parse.c +++ b/parse/parse.c @@ -6,60 +6,65 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 09:29:21 by cacharle #+# #+# */ -/* Updated: 2019/11/18 17:21:38 by cacharle ### ########.fr */ +/* Updated: 2020/01/11 10:12:10 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" -t_parsing *parse(char *filename) +t_state *parse(char *filename) { - int i; - char **lines; - t_parsing *parsing; + int i; + char **lines; + t_state *state; - if ((lines = get_file_lines(filename)) == NULL) - return (NULL); - if ((parsing = (t_parsing*)malloc(sizeof(t_parsing))) == NULL) + if ((state = state_new_empty()) == NULL) return (NULL); - parsing->map = NULL; - parsing->ceilling_color.hexcode = 0; - parsing->floor_color.hexcode = 0; + if ((lines = get_file_lines(filename)) == NULL) + return (state_destroy(state)); i = -1; while (lines[++i] != NULL) { if (*lines[i] == '1') break ; - if (!parse_line(parsing, lines[i])) - return (NULL); + if (!parse_line(state, lines[i])) + { + helper_free_splited(lines); + return (state_destroy(state)); + } } - if ((parsing = parse_map(parsing, lines + i)) == NULL) - return (NULL); - free(lines); - return (parsing); + if ((state = parse_map(state, lines + i)) == NULL) + { + helper_free_splited(lines); + return (state_destroy(state)); + } + helper_free_splited(lines); + return (state); } char **get_file_lines(char *filename) { int fd; int ret; - char *line; + char buf[BUFFER_SIZE]; char *file; fd = open(filename, O_RDONLY); if ((file = ft_strdup("")) == NULL) return (NULL); - while ((ret = get_next_line(fd, &line)) == 1) - if ((file = ft_strjoin_free(file, ft_strjoin_free(line, ft_strdup("\n"), 2), 2)) == NULL) + while ((ret = read(fd, buf, BUFFER_SIZE)) > 0) + { + buf[ret] = '\0'; + if ((file = ft_strjoin_free(file, buf, 1)) == NULL) return (NULL); + } if (ret == -1) return (NULL); - free(line); close(fd); return (ft_split(file, '\n')); } -static t_option_parser option_parsers[] = +static t_option_parser g_option_parsers[] = { {"R", parse_resolution}, {"NO", parse_north_texture}, @@ -71,9 +76,9 @@ static t_option_parser option_parsers[] = {"C", parse_ceilling_color} }; -#define OPTIONS_PARSERS_SIZE (sizeof(option_parsers) / sizeof(t_option_parser)) +#define OPTIONS_PARSERS_SIZE (sizeof(g_option_parsers) / sizeof(t_option_parser)) -t_bool parse_line(t_parsing *parsing, char *line) +t_bool parse_line(t_state *state, char *line) { int i; @@ -81,12 +86,14 @@ t_bool parse_line(t_parsing *parsing, char *line) return (TRUE); i = -1; while (++i < (int)OPTIONS_PARSERS_SIZE) - if (ft_strncmp(option_parsers[i].id, line, ft_strlen(option_parsers[i].id)) == 0) - return (option_parsers[i].func(parsing, line + ft_strlen(option_parsers[i].id))); + if (ft_strncmp(g_option_parsers[i].id, line, + ft_strlen(g_option_parsers[i].id)) == 0) + return (g_option_parsers[i].func( + state, line + ft_strlen(g_option_parsers[i].id) + 1)); return (FALSE); } -t_parsing *parse_map(t_parsing *parsing, char **lines) +t_state *parse_map(t_state *state, char **lines) { int i; @@ -94,15 +101,15 @@ t_parsing *parse_map(t_parsing *parsing, char **lines) while (lines[++i] != NULL) if (*lines[i] != '1') return (NULL); - parsing->map_height = i; - if ((parsing->map = (t_map)malloc(sizeof(t_cell*) * i)) == NULL) + state->map_height = i; + if ((state->map = (t_map)malloc(sizeof(t_cell*) * i)) == NULL) return (NULL); - parsing->map_width = ft_strcount(*lines, '1'); + state->map_width = ft_strcount(*lines, '1'); i = -1; while (lines[++i] != NULL) - if ((parsing->map[i] = create_map_row(lines[i])) == NULL) + if ((state->map[i] = create_map_row(lines[i])) == NULL) return (NULL); - return (parsing); + return (state); } t_cell *create_map_row(char *line) diff --git a/parse/parse_ceilling_color.c b/parse/parse_ceilling_color.c deleted file mode 100644 index 3b6c334..0000000 --- a/parse/parse_ceilling_color.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_ceilling_color.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/11/15 09:31:32 by cacharle #+# #+# */ -/* Updated: 2019/11/18 17:22:49 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "cub3d.h" - -t_bool parse_ceilling_color(t_parsing *parsing, char *line) -{ - line++; - if ((parsing->ceilling_color.rgb.r = ft_atoi(line)) > 255) - return (FALSE); - line = ft_strchr(line, ',') + 1; - if ((parsing->ceilling_color.rgb.g = ft_atoi(line)) > 255) - return (FALSE); - line = ft_strchr(line, ',') + 1; - if ((parsing->ceilling_color.rgb.b = ft_atoi(line)) > 255) - return (FALSE); - return (TRUE); -} diff --git a/parse/parse_color.c b/parse/parse_color.c new file mode 100644 index 0000000..f602706 --- /dev/null +++ b/parse/parse_color.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_color.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/11 09:52:34 by cacharle #+# #+# */ +/* Updated: 2020/01/11 10:14:29 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +t_bool parse_ceilling_color(t_state *state, char *line) +{ + int tmp; + + line++; + if ((tmp = ft_atoi(line)) > 255 || tmp < 0) + return (FALSE); + state->ceilling_color.rgb.r = (t_byte)tmp; + if ((line = ft_strchr(line, ',') + 1) == NULL) + return (FALSE); + if ((tmp = ft_atoi(line)) > 255 || tmp < 0) + return (FALSE); + state->ceilling_color.rgb.g = (t_byte)tmp; + if ((line = ft_strchr(line, ',') + 1) == NULL) + return (FALSE); + if ((tmp = ft_atoi(line)) > 255 || tmp < 0) + return (FALSE); + state->ceilling_color.rgb.b = (t_byte)tmp; + return (TRUE); +} + +t_bool parse_floor_color(t_state *state, char *line) +{ + int tmp; + + line++; + if ((tmp = ft_atoi(line)) > 255 || tmp < 0) + return (FALSE); + state->floor_color.rgb.r = (t_byte)tmp; + if ((line = ft_strchr(line, ',') + 1) == NULL) + return (FALSE); + if ((tmp = ft_atoi(line)) > 255 || tmp < 0) + return (FALSE); + state->floor_color.rgb.g = (t_byte)tmp; + if ((line = ft_strchr(line, ',') + 1) == NULL) + return (FALSE); + if ((tmp = ft_atoi(line)) > 255 || tmp < 0) + return (FALSE); + state->floor_color.rgb.b = (t_byte)tmp; + return (TRUE); +} diff --git a/parse/parse_east_texture.c b/parse/parse_east_texture.c deleted file mode 100644 index 846b36d..0000000 --- a/parse/parse_east_texture.c +++ /dev/null @@ -1,19 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_east_texture.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/11/15 09:29:37 by cacharle #+# #+# */ -/* Updated: 2019/11/15 09:31:02 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "cub3d.h" - -t_bool parse_east_texture(t_parsing *parsing, char *line) -{ - parsing->east_texture_path = ft_strdup(line + 1); - return (TRUE); -} diff --git a/parse/parse_floor_color.c b/parse/parse_floor_color.c deleted file mode 100644 index ab16bd9..0000000 --- a/parse/parse_floor_color.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_floor_color.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/11/15 09:29:32 by cacharle #+# #+# */ -/* Updated: 2019/11/18 17:23:26 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "cub3d.h" - -t_bool parse_floor_color(t_parsing *parsing, char *line) -{ - line++; - if ((parsing->floor_color.rgb.r = ft_atoi(line)) > 255) - return (FALSE); - line = ft_strchr(line, ',') + 1; - if ((parsing->floor_color.rgb.g = ft_atoi(line)) > 255) - return (FALSE); - line = ft_strchr(line, ',') + 1; - if ((parsing->floor_color.rgb.b = ft_atoi(line)) > 255) - return (FALSE); - return (TRUE); -} diff --git a/parse/parse_north_texture.c b/parse/parse_north_texture.c deleted file mode 100644 index eb8cb3c..0000000 --- a/parse/parse_north_texture.c +++ /dev/null @@ -1,19 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_north_texture.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/11/15 09:29:47 by cacharle #+# #+# */ -/* Updated: 2019/11/15 09:30:52 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "cub3d.h" - -t_bool parse_north_texture(t_parsing *parsing, char *line) -{ - parsing->north_texture_path = ft_strdup(line + 1); - return (TRUE); -} diff --git a/parse/parse_resolution.c b/parse/parse_resolution.c index e63317e..d6c5759 100644 --- a/parse/parse_resolution.c +++ b/parse/parse_resolution.c @@ -6,16 +6,19 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 09:29:27 by cacharle #+# #+# */ -/* Updated: 2019/11/15 09:31:47 by cacharle ### ########.fr */ +/* Updated: 2020/01/11 09:44:18 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" -t_bool parse_resolution(t_parsing *parsing, char *line) +t_bool parse_resolution(t_state *state, char *line) { - parsing->resolution_width = ft_atoi(line); - line = ft_strrchr(line, ' ') + 1; - parsing->resolution_height = ft_atoi(line); + if ((state->window.width = ft_atoi(line)) < 0) + return (FALSE); + if ((line = ft_strrchr(line, ' ') + 1) == NULL) + return (FALSE); + if ((state->window.height = ft_atoi(line)) < 0) + return (FALSE); return (TRUE); } diff --git a/parse/parse_south_texture.c b/parse/parse_south_texture.c deleted file mode 100644 index 10ee403..0000000 --- a/parse/parse_south_texture.c +++ /dev/null @@ -1,19 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_south_texture.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/11/15 09:29:55 by cacharle #+# #+# */ -/* Updated: 2019/11/15 09:30:46 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "cub3d.h" - -t_bool parse_south_texture(t_parsing *parsing, char *line) -{ - parsing->south_texture_path = ft_strdup(line + 1); - return (TRUE); -} diff --git a/parse/parse_sprite_texture.c b/parse/parse_sprite_texture.c deleted file mode 100644 index 8e3f40b..0000000 --- a/parse/parse_sprite_texture.c +++ /dev/null @@ -1,19 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_sprite_texture.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/11/15 09:30:05 by cacharle #+# #+# */ -/* Updated: 2019/11/15 09:31:56 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "cub3d.h" - -t_bool parse_sprite_texture(t_parsing *parsing, char *line) -{ - parsing->sprite_texture_path = ft_strdup(line + 1); - return (TRUE); -} diff --git a/parse/parse_textures.c b/parse/parse_textures.c new file mode 100644 index 0000000..a0fb8f6 --- /dev/null +++ b/parse/parse_textures.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_textures.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/11 09:47:53 by cacharle #+# #+# */ +/* Updated: 2020/01/11 09:51:03 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +t_bool parse_north_texture(t_state *state, char *line) +{ + if ((state->textures_path[TEX_NORTH] = ft_strdup(line)) == NULL) + return (FALSE); + return (TRUE); +} + +t_bool parse_south_texture(t_state *state, char *line) +{ + if ((state->textures_path[TEX_SOUTH] = ft_strdup(line)) == NULL) + return (FALSE); + return (TRUE); +} + +t_bool parse_west_texture(t_state *state, char *line) +{ + if ((state->textures_path[TEX_WEST] = ft_strdup(line)) == NULL) + return (FALSE); + return (TRUE); +} + +t_bool parse_east_texture(t_state *state, char *line) +{ + if ((state->textures_path[TEX_EAST] = ft_strdup(line)) == NULL) + return (FALSE); + return (TRUE); +} + +t_bool parse_sprite_texture(t_state *state, char *line) +{ + if ((state->textures_path[TEX_SPRITE] = ft_strdup(line)) == NULL) + return (FALSE); + return (TRUE); +} diff --git a/parse/parse_west_texture.c b/parse/parse_west_texture.c deleted file mode 100644 index dca1629..0000000 --- a/parse/parse_west_texture.c +++ /dev/null @@ -1,19 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_west_texture.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/11/15 09:29:42 by cacharle #+# #+# */ -/* Updated: 2019/11/15 09:30:40 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "cub3d.h" - -t_bool parse_west_texture(t_parsing *parsing, char *line) -{ - parsing->west_texture_path = ft_strdup(line + 1); - return (TRUE); -} diff --git a/render.c b/render.c index 687e2e0..2025a46 100644 --- a/render.c +++ b/render.c @@ -9,19 +9,13 @@ int render_update(void *param) if (!state->running) { state_destroy(state); - exit(0); + exit(EXIT_SUCCESS); } mlx_clear_window(state->mlx_ptr, state->window_ptr); x = -1; - while (++x < state->window_width) + while (++x < state->window.width) render_column(state, x); - - /* for (int i = 0; i < 200000; i++) */ - /* state->window_img.data[i] = 127; */ - mlx_put_image_to_window(state->mlx_ptr, state->window_ptr, state->window_img.id, 0, 0); - /* mlx_put_image_to_window(state->mlx_ptr, state->window_ptr, state->north_texture.id, 0, 0); */ - /* for (int i = 0; i < 200; i++) */ - /* printf("%d ", state->window_img.data[i]); */ + mlx_put_image_to_window(state->mlx_ptr, state->window_ptr, state->window.id, 0, 0); return (0); } @@ -38,7 +32,7 @@ void render_column(t_state *state, int x) * # | # * #################### */ - double camera_x = 2 * x / (double)state->window_width - 1; + double camera_x = 2 * x / (double)state->window.width - 1; /* camera plane length related to current column */ t_vector ray; @@ -99,15 +93,15 @@ void render_column(t_state *state, int x) /* if (perp_wall_dist <= 0) */ /* line_height = state->window_height; */ /* else */ - line_height = (int)(state->window_height / perp_wall_dist); + line_height = (int)(state->window.height / perp_wall_dist); /* printf("%f\n", perp_wall_dist); */ /* printf("%d\n", line_height); */ - int draw_start = state->window_height / 2 - line_height / 2; + int draw_start = state->window.height / 2 - line_height / 2; if (draw_start < 0) draw_start = 0; - int draw_end = state->window_height / 2 + line_height / 2; - if (draw_end >= state->window_height) - draw_end = state->window_height - 1; + int draw_end = state->window.height / 2 + line_height / 2; + if (draw_end >= state->window.height) + draw_end = state->window.height - 1; /* int tex_x; */ /* int wall_x = side == SIDE_WEST_EAST ? pos */ @@ -137,11 +131,11 @@ void render_column(t_state *state, int x) t_color white; white.hexcode = 0x00ffffff; while (i < draw_start) - ((t_color*)state->window_img.data)[i++ * state->window_img.width + x] = state->ceilling_color; + ((t_color*)state->window.data)[i++ * state->window.width + x] = state->ceilling_color; while (i < draw_end) - ((t_color*)state->window_img.data)[i++ * state->window_img.width + x] = white; + ((t_color*)state->window.data)[i++ * state->window.width + x] = white; while (i < state->window_height) - ((t_color*)state->window_img.data)[i++ * state->window_img.width + x] = state->floor_color; + ((t_color*)state->window.data)[i++ * state->window.width + x] = state->floor_color; } /* double map_range(double x, double src_lo, double src_hi, double dest_lo, double dest_hi) */ diff --git a/state.c b/state.c index eb9b3eb..139904b 100644 --- a/state.c +++ b/state.c @@ -6,61 +6,53 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/19 16:39:57 by cacharle #+# #+# */ -/* Updated: 2020/01/10 11:44:24 by cacharle ### ########.fr */ +/* Updated: 2020/01/11 10:24:53 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" -t_state *state_new(void *mlx_ptr, void *window_ptr, t_parsing *parsing) -{ - t_state *state; - - if ((state = (t_state*)malloc(sizeof(t_state))) == NULL) - return (NULL); - if ((state->north_texture = load_texture(parsing->north_texture_path)) == NULL) - return (NULL); - if ((state->south_texture = load_texture(parsing->south_texture_path)) == NULL) - return (NULL); - if ((state->west_texture = load_texture(parsing->west_texture_path)) == NULL) - return (NULL); - if ((state->east_texture = load_texture(parsing->east_texture_path)) == NULL) - return (NULL); +/* +** Initialize the state attributes that weren't already filled by the parsing +*/ - state->window_img.id = mlx_new_image(mlx_ptr, parsing->resolution_width, parsing->resolution_height); - state->window_img.width = parsing->resolution_width; - state->window_img.height = parsing->resolution_height; - state->window_img.data = mlx_get_data_addr(state->window_img.id, - &state->window_img.depth, &state->window_img.size_line, &state->window_img.endian); +t_state *state_new(t_state *state) +{ + int i; state->running = TRUE; - state->mlx_ptr = mlx_ptr; - state->window_ptr = window_ptr; - state->window_width = parsing->resolution_width; - state->window_height = parsing->resolution_height; - state->map = parsing->map; - state->map_width = parsing->map_width; - state->map_height = parsing->map_height; - state->ceilling_color = parsing->ceilling_color; - state->floor_color = parsing->floor_color; - - state_new_player(state, parsing); - + if ((state->mlx_ptr = mlx_init()) == NULL) + return (state_destroy(state)); + if ((state->window_ptr = mlx_new_window(state->mlx_ptr, + state->window.width, state->window.height, WINDOW_TITLE)) == NULL) + return (state_destroy(state)); + i = -1; + while (++i < TEXTURES_NUM) + { + load_texture(state->mlx_ptr, &state->textures[i], state->textures_path[i]); + if (state->textures[i].id == NULL) + return (state_destroy(state)); + } + if ((state->window.id = mlx_new_image(state->mlx_ptr, + state->window.width, state->window.height)) == NULL) + return (state_destroy(state)); + state->window.data = mlx_get_data_addr(state->window.id, + &state->window.depth, &state->window.size_line, &state->window.endian); + state_init_player(state); return (state); } -void state_new_player(t_state *state, t_parsing *parsing) +void state_init_player(t_state *state) { int i; int j; i = -1; - while (++i < parsing->map_height) + while (++i < state->map_height) { j = -1; - while (++j < parsing->map_width) - if (parsing->map[i][j] & (CELL_LOOK_NORTH | CELL_LOOK_SOUTH - | CELL_LOOK_WEST | CELL_LOOK_EAST) + while (++j < state->map_width) + if (helper_is_player_cell(state->map[i][j])) { state->pos.x = (double)j + 0.5; state->pos.y = (double)i + 0.5; @@ -69,27 +61,68 @@ void state_new_player(t_state *state, t_parsing *parsing) } } /* need to be normalized */ - state->dir.x = 0.0; - state->dir.y = 0.0; - if (parsing->map[(int)state.y][(int)state.x] & CELL_LOOK_NORTH) + if (state->map[(int)state->pos.y][(int)state->pos.x] == CELL_LOOK_NORTH) state->dir.y = 1.0; - else if (parsing->map[(int)state.y][(int)state.x] & CELL_LOOK_SOUTH) + else if (state->map[(int)state->pos.y][(int)state->pos.x] == CELL_LOOK_SOUTH) state->dir.y = -1.0; - else if (parsing->map[(int)state.y][(int)state.x] & CELL_LOOK_WEST) + else if (state->map[(int)state->pos.y][(int)state->pos.x] == CELL_LOOK_WEST) state->dir.x = -1.0; - else if (parsing->map[(int)state.y][(int)state.x] & CELL_LOOK_EAST) + else if (state->map[(int)state->pos.y][(int)state->pos.x] == CELL_LOOK_EAST) state->dir.x = 1.0; - state->plane.x = 0.0; state->plane.y = 0.66; } -void state_destroy(t_state *state) +t_state *state_new_empty(void) { - mlx_destroy_window(state->mlx_ptr, state->window_ptr); - free(state->north_texture); - free(state->south_texture); - free(state->west_texture); - free(state->east_texture); + int i; + t_state *state; + + if ((state = (t_state*)malloc(sizeof(t_state))) == NULL) + return (NULL); + state->mlx_ptr = NULL; + state->window_ptr = NULL; + i = -1; + while (++i < TEXTURES_NUM) + { + state->textures_path[i] = NULL; + state->textures[i].id = NULL; + } + state->dir.x = 0.0; + state->dir.y = 0.0; + state->map = NULL; + state->ceilling_color.hexcode = 0x0; + state->floor_color.hexcode = 0x0; + return (state); +} + +void *state_destroy(t_state *state) +{ + int i; + + if (state == NULL) + return (NULL); + i = -1; + while (++i < TEXTURES_NUM) + { + free(state->textures_path[i]); + mlx_destroy_image(state->mlx_ptr, state->textures[i].id); + } + if (state->mlx_ptr && state->window_ptr) + mlx_destroy_window(state->mlx_ptr, state->window_ptr); + if (state->map != NULL) + while (state->map_height-- > 0 && state->map[state->map_height] != NULL) + free(state->map[state->map_height]); + free(state->map); free(state); + return (NULL); +} + +void load_texture(void *mlx_ptr, t_image *image, char *path) +{ + if ((image->id = mlx_xpm_file_to_image( + mlx_ptr, path, &image->width, &image->height)) == NULL) + return ; + image->data = mlx_get_data_addr(image->id, &image->depth, + &image->size_line, &image->endian); } -- cgit