diff options
| -rw-r--r-- | Makefile | 6 | ||||
| -rw-r--r-- | cub3d.h | 71 | ||||
| -rw-r--r-- | error.c | 11 | ||||
| -rw-r--r-- | event.c | 6 | ||||
| m--------- | generator | 0 | ||||
| m--------- | libft | 0 | ||||
| -rw-r--r-- | main.c | 83 | ||||
| -rw-r--r-- | render.c (renamed from graphics.c) | 104 | ||||
| -rw-r--r-- | state.c | 95 | ||||
| -rw-r--r-- | vector.c (renamed from linear_algebra.c) | 7 |
10 files changed, 245 insertions, 138 deletions
@@ -2,7 +2,7 @@ LIBFT_PATH = ./libft MINILIBX_PATH = ./miniLibX CC = gcc -CCFLAGS = -I$(LIBFT_PATH) -I$(MINILIBX_PATH) -I. -Wall -Wextra -g #-Werror +CCFLAGS = -I$(LIBFT_PATH) -I$(MINILIBX_PATH) -I. -Wall -Wextra #-Werror LDFLAGS = -L$(LIBFT_PATH) -lft \ -L$(MINILIBX_PATH) -lmlx \ -framework OpenGL -framework AppKit -lm @@ -12,7 +12,7 @@ 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 graphics.c linear_algebra.c + parse/parse_sprite_texture.c state.c vector.c render.c OBJ = $(SRC:.c=.o) INCLUDE = cub3d.h @@ -32,6 +32,8 @@ fclean: clean libft_fclean re: fclean all +bonus: all + libft_all: make -C $(LIBFT_PATH) all @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:40:37 by cacharle #+# #+# */ -/* Updated: 2019/11/19 17:38:22 by cacharle ### ########.fr */ +/* Updated: 2020/01/10 11:29:16 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include <stdio.h> @@ -14,6 +14,13 @@ #ifndef CUB3D_H # define CUB3D_H +# include <unistd.h> +# include <fcntl.h> +# include <stdlib.h> +# include <math.h> +# include "mlx.h" +# include "libft.h" + # define WINDOW_TITLE "cub3D" # define MLXK_ESC 53 # define MLXK_W 13 @@ -26,13 +33,6 @@ # define MLX_LITTLE_ENDIAN 0 # define MLX_BIG_ENDIAN 1 -# include <unistd.h> -# include <fcntl.h> -# include <stdlib.h> -# include <math.h> -# include "mlx.h" -# include "libft.h" - # define TRUE 1 # define FALSE 0 @@ -55,18 +55,18 @@ typedef union t_byte r; t_byte g; t_byte b; - } rgb; + } rgb; } t_color; typedef enum { - CELL_EMPTY, - CELL_WALL, - CELL_ITEM, - CELL_LOOK_NORTH, - CELL_LOOK_SOUTH, - CELL_LOOK_WEST, - CELL_LOOK_EAST + CELL_EMPTY = 1 << 1, + CELL_WALL = 1 << 2, + CELL_ITEM = 1 << 3, + CELL_LOOK_NORTH = 1 << 4, + CELL_LOOK_SOUTH = 1 << 5, + CELL_LOOK_WEST = 1 << 6, + CELL_LOOK_EAST = 1 << 7 } t_cell; typedef t_cell** t_map; @@ -114,20 +114,20 @@ typedef struct s_state 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 *north_texture; + t_image *south_texture; + t_image *west_texture; + t_image *east_texture; } t_state; -typedef t_bool (*t_option_parser_func)(t_parsing *parsing, 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, @@ -161,28 +161,37 @@ t_bool parse_ceilling_color(t_parsing *parsing, char *line); ** event.c */ -int handle_key(int key, void *param); +int handle_keydown(int key, void *param); /* ** graphics.c */ -t_state *create_state(void *mlx_ptr, void *window_ptr, t_parsing *parsing); -int graphics_update(void *param); -void draw_column(t_state *state, int x); -void write_color(char *data, int y, int x, t_color color); +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); /* ** render.c */ +int render_update(void *param); +void render_column(t_state *state, int x); + +/* +** vector.c +*/ + +t_vector vector_add(t_vector a, t_vector b); +t_vector vector_scale(t_vector v, double scalar); +t_vector vector_rotate(t_vector v, double angle); +double vector_norm(t_vector v); /* -** linear_algebra.c +** error.c */ -t_vector vector_add(t_vector a, t_vector b); -t_vector vector_scale(t_vector v, double scalar); -t_vector vector_rotate(t_vector v, double angle); +void error_put_usage(void); #endif @@ -0,0 +1,11 @@ +#include "cub3d.h" + +void error_put_usage_exit(char *name) +{ + ft_putstr_fd(name, STDERR_FILENO); + ft_putendl_fd(": missing file operand", STDERR_FILENO); + ft_putstr_fd("Usage: ", STDERR_FILENO); + ft_putstr_fd(name, STDERR_FILENO); + ft_putendl_fd(" [.cub file] [--save]", STDERR_FILENO); + exit(EXIT_FAILURE); +} @@ -6,16 +6,16 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:39:37 by cacharle #+# #+# */ -/* Updated: 2019/11/18 17:49:10 by cacharle ### ########.fr */ +/* Updated: 2020/01/10 11:27:56 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" #define ROTATE_SIZE (M_PI / 20.0) -#define MOVE_SPEED 0.5 +#define MOVE_SPEED 0.25 -int handle_key(int key, void *param) +int handle_keydown(int key, void *param) { t_state *state; diff --git a/generator b/generator deleted file mode 160000 -Subproject fa840dd345f1f23cc419afdbfe67ace4eb018cb diff --git a/libft b/libft -Subproject d0c146c2274198814e106b17ea1f9461a1b0b81 +Subproject 7fbd92d957e5d30d3478b94ec0abd07aacc054b @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:39:39 by cacharle #+# #+# */ -/* Updated: 2019/11/19 17:09:53 by cacharle ### ########.fr */ +/* Updated: 2020/01/10 10:59:09 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,24 +14,61 @@ int main(int argc, char **argv) { - if (argc != 2) + 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) + 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("Error"); + ft_putendl_fd("Error: minilibx window creation", STDERR_FILENO); return (1); } + if ((state = state_new(mlx_ptr, window_ptr, p)) == 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); + + return (0); +} + +/* +int main(int argc, char argv) +{ t_parsing *p = parse(argv[1]); if (p == NULL) return (1); - /* printf("R %d %d\n", p->resolution_width, p->resolution_height); */ - /* printf("NO %s\n", p->north_texture_path); */ - /* printf("SO %s\n", p->south_texture_path); */ - /* printf("WE %s\n", p->west_texture_path); */ - /* printf("EA %s\n\n", p->east_texture_path); */ - /* printf("S %s\n", p->sprite_texture_path); */ - /* printf("F %d,%d,%d\n", p->floor_color.r, p->floor_color.g, p->floor_color.b); */ - /* printf("C %d,%d,%d\n\n", p->ceilling_color.r, p->ceilling_color.g, p->ceilling_color.b); */ + printf("R %d %d\n", p->resolution_width, p->resolution_height); + printf("NO %s\n", p->north_texture_path); + printf("SO %s\n", p->south_texture_path); + printf("WE %s\n", p->west_texture_path); + printf("EA %s\n\n", p->east_texture_path); + printf("S %s\n", p->sprite_texture_path); + printf("F %d,%d,%d\n", p->floor_color.r, p->floor_color.g, p->floor_color.b); + printf("C %d,%d,%d\n\n", p->ceilling_color.r, p->ceilling_color.g, p->ceilling_color.b); - /* printf("%dx%d\n", p->map_height, p->map_width); */ + printf("%dx%d\n", p->map_height, p->map_width); for (int i = 0; i < p->map_height; i++) { for (int j = 0; j < p->map_width; j++) @@ -47,24 +84,6 @@ int main(int argc, char **argv) } printf("\n"); } - - void *mlx_ptr = mlx_init(); - if (mlx_ptr == NULL) - return (1); - void *window_ptr = mlx_new_window(mlx_ptr, p->resolution_width, p->resolution_height, WINDOW_TITLE); - if (window_ptr == NULL) - return (1); - - t_state *state = create_state(mlx_ptr, window_ptr, p); - if (state == NULL) - return (1); - - /* for (int i = 0; i < 20; i++) */ - /* draw_column(state, i); */ - - mlx_key_hook(window_ptr, handle_key, (void*)state); - mlx_loop_hook(mlx_ptr, graphics_update, (void*)state); - mlx_loop(mlx_ptr); - - return (0); + return 0; } +*/ @@ -1,81 +1,21 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* graphics.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/11/19 16:39:57 by cacharle #+# #+# */ -/* Updated: 2019/11/19 17:50:51 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - #include "cub3d.h" -t_state *create_state(void *mlx_ptr, void *window_ptr, t_parsing *parsing) -{ - t_state *state; - - if ((state = (t_state*)malloc(sizeof(t_state))) == NULL) - return (NULL); - 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); - printf("%d\n", state->window_img.width); - printf("%d\n", state->window_img.height); - printf("%d\n", state->window_img.depth); - printf("%d\n", state->window_img.size_line); - printf("%d\n", state->window_img.endian); - - state->north_texture.id = mlx_xpm_file_to_image(mlx_ptr, - parsing->north_texture_path, &state->north_texture.width, &state->north_texture.height); - if (state->north_texture.id == NULL) - return (NULL); - state->north_texture.data = mlx_get_data_addr(state->north_texture.id, &state->north_texture.depth, - &state->north_texture.size_line, &state->north_texture.endian); - /* printf("%d\n", state->north_texture.endian); */ - /* state->south_texture = ; */ - /* state->west_texture = ; */ - /* state->east_texture = ; */ - - state->mlx_ptr = mlx_ptr; - state->window_ptr = window_ptr; - state->window_width = parsing->resolution_width; - state->window_height = parsing->resolution_height; - state->running = TRUE; - state->pos.x = 1.1; - state->pos.y = 1.1; - /* need to be normalized */ - state->dir.x = 1.0; - state->dir.y = 0.0; - state->plane.x = 0.0; - state->plane.y = 0.66; - 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; - return (state); -} - -int graphics_update(void *param) +int render_update(void *param) { int x; t_state *state; - + state = param; if (!state->running) { - mlx_destroy_window(state->mlx_ptr, state->window_ptr); + state_destroy(state); exit(0); } mlx_clear_window(state->mlx_ptr, state->window_ptr); x = -1; while (++x < state->window_width) - draw_column(state, x); - + 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); @@ -85,7 +25,7 @@ int graphics_update(void *param) return (0); } -void draw_column(t_state *state, int x) +void render_column(t_state *state, int x) { /* * -1 0 1 @@ -169,10 +109,29 @@ void draw_column(t_state *state, int x) if (draw_end >= state->window_height) draw_end = state->window_height - 1; - int tex_x; - + /* int tex_x; */ /* int wall_x = side == SIDE_WEST_EAST ? pos */ - + /* t_vector unit_ray = vector_scale(ray, 1.0 / vector_norm(ray)); */ + /* t_vector dist_v = vector_scale(unit_ray, side == SIDE_WEST_EAST ? side_dist_y : side_dist_x); */ + /* printf("[%f %f]\n", dist_v.x, dist_v.y); */ + /* #<{(| printf("[%f %f]\n", dist_v.x, dist_v.y); |)}># */ + /* */ + /* double mod = side == SIDE_WEST_EAST ? dist_v.y : dist_v.x; */ + /* mod = fabs(mod); */ + /* mod -= floor(mod); */ + + + /* tex_x = (int)map_range(side == SIDE_WEST_EAST ? dist_v.y : dist_v.x, 0, 1, 0, state->north_texture.width); */ + /* tex_x = side = SIDE_WEST_EAST ? side_dist_y : side_dist_x) */ + /* printf("%d\n", tex_x); */ + + /* double wall_x; */ + /* if (side == SIDE_WEST_EAST) */ + /* wall_x = pos_y + prep_wall_dist * ray.y; */ + /* else */ + /* wall_x = pos_x + prep_wall_dist * ray.x; */ + /* wall_x -= floor(wall_x); */ + int i; i = 0; t_color white; @@ -184,3 +143,10 @@ void draw_column(t_state *state, int x) while (i < state->window_height) ((t_color*)state->window_img.data)[i++ * state->window_img.width + x] = state->floor_color; } + +/* double map_range(double x, double src_lo, double src_hi, double dest_lo, double dest_hi) */ +/* { */ +/* double src_len = src_hi - src_lo; */ +/* double dest_len = dest_hi - dest_lo; */ +/* return ((x - src_lo) / src_len) * dest_len + dest_lo; */ +/* } */ @@ -0,0 +1,95 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* graphics.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/19 16:39:57 by cacharle #+# #+# */ +/* Updated: 2020/01/10 11:44:24 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); + + 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); + + 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); + + return (state); +} + +void state_new_player(t_state *state, t_parsing *parsing) +{ + int i; + int j; + + i = -1; + while (++i < parsing->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) + { + state->pos.x = (double)j + 0.5; + state->pos.y = (double)i + 0.5; + // break 2nd loop? + break; + } + } + /* 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) + state->dir.y = 1.0; + else if (parsing->map[(int)state.y][(int)state.x] & CELL_LOOK_SOUTH) + state->dir.y = -1.0; + else if (parsing->map[(int)state.y][(int)state.x] & CELL_LOOK_WEST) + state->dir.x = -1.0; + else if (parsing->map[(int)state.y][(int)state.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) +{ + 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); + free(state); +} diff --git a/linear_algebra.c b/vector.c index 881f8ff..babaf12 100644 --- a/linear_algebra.c +++ b/vector.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/18 01:28:01 by cacharle #+# #+# */ -/* Updated: 2019/11/18 01:32:41 by cacharle ### ########.fr */ +/* Updated: 2019/11/19 18:21:22 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,3 +38,8 @@ t_vector vector_rotate(t_vector v, double angle) rotated.y = sin(angle) * v.x + cos(angle) * v.y; return (rotated); } + +double vector_norm(t_vector v) +{ + return sqrt(SQUARE(v.x) + SQUARE(v.y)); +} |
