diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Makefile | 8 | ||||
| -rw-r--r-- | cub3d.h | 23 | ||||
| -rw-r--r-- | event.c | 11 | ||||
| -rw-r--r-- | graphics.c | 150 | ||||
| m--------- | libft | 0 | ||||
| -rw-r--r-- | main.c | 42 | ||||
| -rw-r--r-- | minimalist.cub | 2 | ||||
| -rw-r--r-- | test.cub | 5 |
9 files changed, 208 insertions, 34 deletions
@@ -3,3 +3,4 @@ *.a a.out cub3D +*.dSYM @@ -5,23 +5,23 @@ CC = gcc CCFLAGS = -I$(LIBFT_PATH) -I$(MINILIBX_PATH) -I. -Wall -Wextra #-Werror LDFLAGS = -L$(LIBFT_PATH) -lft \ -L$(MINILIBX_PATH) -lmlx \ - -framework OpenGL -framework AppKit + -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 + parse/parse_sprite_texture.c graphics.c OBJ = $(SRC:.c=.o) INCLUDE = cub3d.h all: libft_all minilibx_all $(NAME) -$(NAME): $(OBJ) $(INCLUDE) +$(NAME): $(OBJ) $(CC) $(LDFLAGS) -o $@ $(OBJ) -%.o: %.c +%.o: %.c $(INCLUDE) $(CC) $(CCFLAGS) -c -o $@ $< clean: libft_clean minilibx_clean @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:40:37 by cacharle #+# #+# */ -/* Updated: 2019/11/15 09:28:43 by cacharle ### ########.fr */ +/* Updated: 2019/11/16 12:51:46 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include <stdio.h> @@ -14,6 +14,7 @@ #ifndef CUB3D_H # define CUB3D_H +# define WINDOW_TITLE "cub3D" # define MLXK_W 13 # define MLXK_A 0 # define MLXK_S 1 @@ -23,6 +24,7 @@ # include <unistd.h> # include <fcntl.h> # include <stdlib.h> +# include <math.h> # include "mlx.h" # include "libft.h" @@ -33,8 +35,8 @@ typedef int t_bool; typedef struct { - float x; - float y; + double x; + double y; } t_vector; typedef struct @@ -76,13 +78,16 @@ typedef struct typedef struct s_state { t_bool running; + void *mlx_ptr; + void *window_ptr; + int window_width; + int window_height; t_vector pos; t_vector dir; t_vector plane; t_map map; - int width; - int height; - + int map_width; + int map_height; } t_state; typedef t_bool (*t_option_parser_func)(t_parsing *parsing, char *line); @@ -126,7 +131,11 @@ int handle_key(int key, void *param); ** graphics.c */ -t_state *create_state(t_parsing *parsing); +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); +t_vector vector_add(t_vector a, t_vector b); +t_vector vector_scale(t_vector v, double scalar); /* ** render.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:39:37 by cacharle #+# #+# */ -/* Updated: 2019/11/15 09:27:55 by cacharle ### ########.fr */ +/* Updated: 2019/11/16 12:44:15 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,8 +16,17 @@ int handle_key(int key, void *param) { t_state *state; + /* printf("%d\n", key); */ state = (t_state*)param; if (key == MLXK_ESC) state->running = FALSE; + if (key == MLXK_A) + state->pos.x -= 0.5; + if (key == MLXK_D) + state->pos.x += 0.5; + if (key == MLXK_W) + state->pos.y -= 0.5; + if (key == MLXK_S) + state->pos.y += 0.5; return (0); } @@ -1,30 +1,168 @@ #include "cub3d.h" -t_state *create_state(t_parsing *parsing) +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->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 = 8.0; - state->pos.y = 8.0; - state->dir.x = -1.0; + state->pos.x = 1.0; + state->pos.y = 1.0; + 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; return (state); } -void update(t_state *state) +int graphics_update(void *param) { + t_state *state = param; + if (!state->running) + { + mlx_destroy_window(state->mlx_ptr, state->window_ptr); + exit(0); + } + /* int color = 0x00FFFFFF; */ + /* int x = state->pos.x; */ + /* int y = state->pos.y; */ + /* for (int i = 0; i < 4; i++) */ + /* for (int j = 0; j < 4; j++) */ + /* mlx_pixel_put(state->mlx_ptr, state->window_ptr, x + i - 2,y + j - 2, 0x00ffffff); */ + /* int x2 = state->dir.x; */ + /* int y2 = state->dir.y; */ + /* for (int i = 0; i < 4; i++) */ + /* for (int j = 0; j < 4; j++) */ + /* mlx_pixel_put(state->mlx_ptr, state->window_ptr, x + x2 + i - 2,y + y2 + j - 2, 0x00ffff00); */ + int x; + + x = -1; + while (++x < state->window_width) + draw_column(state, x); + + return (0); } -void draw_column(int x) +typedef enum +{ + SIDE_NORTH_SOUTH, + SIDE_WEST_EAST +} t_side; + +void draw_column(t_state *state, int x) { + /* + * -1 0 1 + * v v v + * #################### + * # | # + * # | # + * # | # + * # | # + * # | # + * #################### + */ + double camera_x = 2 * x / (double)state->window_width - 1; + + /* camera plane length related to current column */ + t_vector ray; + ray = vector_add(state->dir, vector_scale(state->plane, camera_x)); + /* ray.x = state->dir.x + state->plane.x * camera_x; */ + /* ray.y = state->dir.y + state->plane.y * camera_x; */ + /* printf("[%f %f]\n", ray.x, ray.y); */ + + /* map pos*/ + int map_x = (int)state->pos.x; + int map_y = (int)state->pos.y; + + /* dist to first encounter wall */ + double side_dist_x; + double side_dist_y; + + /* delta between grid lines from ray percepective */ + double delta_dist_x = fabs(1.0 / ray.x); + double delta_dist_y = fabs(1.0 / ray.y); + + /* dist to wall (perpendicular to avoid fisheye effect) */ + double perp_wall_dist; + + /* step on size for the `map_. +=` */ + int map_step_x = ray.x < 0 ? -1 : 1; + int map_step_y = ray.y < 0 ? -1 : 1; + + if (ray.x < 0) + side_dist_x = (state->pos.x - map_x) * delta_dist_x; + else + side_dist_x = (map_x + 1.0 - state->pos.x) * delta_dist_x; + + if (ray.y < 0) + side_dist_y = (state->pos.y - map_y) * delta_dist_y; + else + side_dist_y = (map_y + 1.0 - state->pos.y) * delta_dist_y; + + t_side side; + int hit = FALSE; + while (!hit) + { + if (side_dist_x < side_dist_y) + { + side_dist_x += delta_dist_x; /* increment real dist */ + map_x += map_step_x; /* increment map dist */ + side = SIDE_WEST_EAST; + } + else + { + side_dist_y += delta_dist_y; + map_y += map_step_y; + side = SIDE_NORTH_SOUTH; + } + if (state->map[map_y][map_x] == CELL_WALL) + break; + } + if (side == SIDE_WEST_EAST) + perp_wall_dist = (map_x - state->pos.x + (1 - map_step_x) / 2) / ray.x; + else //if (side == SIDE_NORTH_SOUTH) + perp_wall_dist = (map_y - state->pos.y + (1 - map_step_y) / 2) / ray.y; + int line_height = (int)(state->window_height / perp_wall_dist); + int draw_start = -line_height / 2 + state->window_height / 2; + if (draw_start < 0) + draw_start = 0; + int draw_end = line_height / 2 + state->window_height / 2; + if (draw_end >= state->window_height) + draw_end = state->window_height - 1; + + for (int i = draw_start; i < draw_end; i++) + mlx_pixel_put(state->mlx_ptr, state->window_ptr, x, i, 0x00ffffff); +} + +t_vector vector_add(t_vector a, t_vector b) +{ + a.x += b.x; + a.y += b.y; + return a; +} + +t_vector vector_scale(t_vector v, double scalar) +{ + v.x *= scalar; + v.y *= scalar; + return v; } + + +/* sqrt( x^2 + y^2 ) / x */ + + + diff --git a/libft b/libft -Subproject e0f11e486518930e82e67f2dc305595671c074b +Subproject d0c146c2274198814e106b17ea1f9461a1b0b81 @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:39:39 by cacharle #+# #+# */ -/* Updated: 2019/11/15 09:26:16 by cacharle ### ########.fr */ +/* Updated: 2019/11/16 12:58:08 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,32 +20,44 @@ int main(int argc, char **argv) return (1); } t_parsing *p = parse(argv[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); */ for (int i = 0; i < p->map_height; i++) { for (int j = 0; j < p->map_width; j++) { - printf("%d", p->map[i][j]); + if (p->map[i][j] == CELL_WALL) + printf("#"); + else if (p->map[i][j] == CELL_EMPTY) + printf(" "); + else + printf("%d", p->map[i][j]); if (j != p->map_width - 1) printf(" "); } printf("\n"); } - /* void *mlx_ptr = mlx_init(); */ - /* void *mlx_window = mlx_new_window(mlx_ptr, 640, 480, "bonjour"); */ - /* mlx_key_hook(mlx_window, handle_key, NULL); */ - /* mlx_loop(mlx_ptr); */ - /* mlx_destroy_window(mlx_ptr, mlx_window); */ + void *mlx_ptr = mlx_init(); + void *window_ptr = mlx_new_window(mlx_ptr, p->resolution_width, p->resolution_height, WINDOW_TITLE); + + t_state *state = create_state(mlx_ptr, window_ptr, p); + + /* 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); } diff --git a/minimalist.cub b/minimalist.cub index a2a92fe..b9ce665 100644 --- a/minimalist.cub +++ b/minimalist.cub @@ -1,4 +1,4 @@ -R 1920 1080 +R 400 400 NO ./path_to_the_north_texture SO ./path_to_the_south_texture WE ./path_to_the_west_texture diff --git a/test.cub b/test.cub new file mode 100644 index 0000000..6324582 --- /dev/null +++ b/test.cub @@ -0,0 +1,5 @@ +11111 +10001 +10001 +10001 +11111 |
