diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-11-18 19:00:53 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-11-18 19:00:53 +0100 |
| commit | 264676907b55f429e65b4de523eeb729fb64e9f9 (patch) | |
| tree | 5b2cc36bc91abf91ff89b68c70958dad0777931a | |
| parent | 542ae0113ac850e0d5216c7e2dc4543e66a1237f (diff) | |
| download | cub3d-264676907b55f429e65b4de523eeb729fb64e9f9.tar.gz cub3d-264676907b55f429e65b4de523eeb729fb64e9f9.tar.bz2 cub3d-264676907b55f429e65b4de523eeb729fb64e9f9.zip | |
floor and ceiling color, WIP window image buffer
| -rw-r--r-- | cub3d.h | 25 | ||||
| -rw-r--r-- | event.c | 7 | ||||
| -rw-r--r-- | graphics.c | 73 | ||||
| -rw-r--r-- | minimalist.cub | 6 | ||||
| -rw-r--r-- | parse/parse.c | 4 | ||||
| -rw-r--r-- | parse/parse_ceilling_color.c | 11 | ||||
| -rw-r--r-- | parse/parse_floor_color.c | 11 |
7 files changed, 79 insertions, 58 deletions
@@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:40:37 by cacharle #+# #+# */ -/* Updated: 2019/11/18 02:43:17 by cacharle ### ########.fr */ +/* Updated: 2019/11/18 19:00:20 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include <stdio.h> @@ -23,6 +23,9 @@ # define MLXK_LEFT 123 # define MLXK_RIGHT 124 +# define MLX_LITTLE_ENDIAN 0 +# define MLX_BIG_ENDIAN 1 + # include <unistd.h> # include <fcntl.h> # include <stdlib.h> @@ -43,16 +46,16 @@ typedef struct double y; } t_vector; -typedef struct +typedef union { - int hexcode; + unsigned int hexcode; struct { - t_byte b; - t_byte g; - t_byte r; - t_byte empty; - } rgb; + t_byte empty; + t_byte r; + t_byte g; + t_byte b; + } rgb; } t_color; typedef enum @@ -84,6 +87,8 @@ typedef struct int map_height; } t_parsing; +typedef unsigned int* t_image; + typedef struct s_state { t_bool running; @@ -99,6 +104,10 @@ typedef struct s_state int map_height; t_color ceilling_color; t_color floor_color; + t_image window_buf; + int window_img_depth; + int window_img_line_size; + int window_img_endian; } t_state; typedef t_bool (*t_option_parser_func)(t_parsing *parsing, char *line); @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:39:37 by cacharle #+# #+# */ -/* Updated: 2019/11/18 02:31:23 by cacharle ### ########.fr */ +/* Updated: 2019/11/18 17:49:10 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,14 +19,13 @@ 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 = vector_add(state->pos, vector_rotate(vector_scale(state->dir, MOVE_SPEED), M_PI_2)); + state->pos = vector_add(state->pos, vector_scale(vector_rotate(state->dir, M_PI_2), MOVE_SPEED)); if (key == MLXK_D) - state->pos = vector_add(state->pos, vector_rotate(vector_scale(state->dir, MOVE_SPEED), -M_PI_2)); + state->pos = vector_add(state->pos, vector_scale(vector_rotate(state->dir, -M_PI_2), MOVE_SPEED)); if (key == MLXK_W) state->pos = vector_add(state->pos, vector_scale(state->dir, MOVE_SPEED)); if (key == MLXK_S) @@ -6,15 +6,20 @@ t_state *create_state(void *mlx_ptr, void *window_ptr, t_parsing *parsing) if ((state = (t_state*)malloc(sizeof(t_state))) == NULL) return (NULL); + if ((state->window_img = mlx_new_image(mlx_ptr, parsing->resolution_width, parsing->resolution_height)) == NULL) + return (NULL); + state->window_buf = mlx_get_data_addr(state->window_img, + &state->window_img_depth, &state->window_img_line_size, &state->window_img_endian); 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.0; - state->pos.y = 1.0; + state->pos.x = 1.1; + state->pos.y = 1.1; + /* need to be normalized */ state->dir.x = 1.0; - state->dir.y = 1.0; + state->dir.y = 0.0; state->plane.x = 0.0; state->plane.y = 0.66; state->map = parsing->map; @@ -27,28 +32,16 @@ t_state *create_state(void *mlx_ptr, void *window_ptr, t_parsing *parsing) int graphics_update(void *param) { - t_state *state = param; + int x; + t_state *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); */ - mlx_clear_window(state->mlx_ptr, state->window_ptr); - int x; - x = -1; while (++x < state->window_width) draw_column(state, x); @@ -127,22 +120,34 @@ void draw_column(t_state *state, int x) break; } if (side == SIDE_WEST_EAST) - perp_wall_dist = (map_x - state->pos.x + (1 - map_step_x) / 2) / ray.x; + perp_wall_dist = ((double)map_x - state->pos.x + (map_step_x == -1 ? 1 : 0)) / 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); + perp_wall_dist = ((double)map_y - state->pos.y + (map_step_y == -1 ? 1 : 0)) / ray.y; + /* perp_wall_dist = sqrt(SQUARE(state->pos.x - side_dist_x) + SQUARE(state->pos.y - side_dist_y)); */ + + int line_height; + /* if (perp_wall_dist <= 0) */ + /* line_height = state->window_height; */ + /* else */ + 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; - /* if (draw_start < 0) */ - /* draw_start = 0; */ + 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; */ - - /* for (int i = 0; i < draw_start; i++) */ - /* mlx_pixel_put(state->mlx_ptr, state->window_ptr, x, i, state->ceilling_color.hexcode); */ - for (int i = draw_start; i < draw_end; i++) - mlx_pixel_put(state->mlx_ptr, state->window_ptr, x, i, 0x00ffffff); - /* for (int i = draw_end; i < state->window_height; i++) */ - /* mlx_pixel_put(state->mlx_ptr, state->window_ptr, x, i, state->floor_color.hexcode); */ + if (draw_end >= state->window_height) + draw_end = state->window_height - 1; + + int i; + i = 0; + while (i < draw_start ) + state->window_buf[i * state->window_width + x] = state->ceilling_color.hexcode; + /* mlx_pixel_put(state->mlx_ptr, state->window_ptr, x, i++, state->ceilling_color.hexcode); */ + while (i < draw_end) + state->window_buf[i * state->window_width + x] = 0x00ffffff; + /* mlx_pixel_put(state->mlx_ptr, state->window_ptr, x, i++, 0x00ffffff); */ + while (i < state->window_height) + state->window_buf[i * state->window_width + x] = state->floor_color.hexcode; + /* mlx_pixel_put(state->mlx_ptr, state->window_ptr, x, i++, state->floor_color.hexcode); */ } diff --git a/minimalist.cub b/minimalist.cub index b9ce665..51a4e99 100644 --- a/minimalist.cub +++ b/minimalist.cub @@ -1,12 +1,12 @@ -R 400 400 +R 640 480 NO ./path_to_the_north_texture SO ./path_to_the_south_texture WE ./path_to_the_west_texture EA ./path_to_the_east_texture S ./path_to_the_sprite_texture -F 220,100,0 -C 225,30,0 +F 220,0,0 +C 0,225,0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 diff --git a/parse/parse.c b/parse/parse.c index f543ca5..8f3124d 100644 --- a/parse/parse.c +++ b/parse/parse.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 09:29:21 by cacharle #+# #+# */ -/* Updated: 2019/11/15 09:32:54 by cacharle ### ########.fr */ +/* Updated: 2019/11/18 17:21:38 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,8 @@ t_parsing *parse(char *filename) if ((parsing = (t_parsing*)malloc(sizeof(t_parsing))) == NULL) return (NULL); parsing->map = NULL; + parsing->ceilling_color.hexcode = 0; + parsing->floor_color.hexcode = 0; i = -1; while (lines[++i] != NULL) { diff --git a/parse/parse_ceilling_color.c b/parse/parse_ceilling_color.c index a451f94..3b6c334 100644 --- a/parse/parse_ceilling_color.c +++ b/parse/parse_ceilling_color.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 09:31:32 by cacharle #+# #+# */ -/* Updated: 2019/11/18 02:43:46 by cacharle ### ########.fr */ +/* Updated: 2019/11/18 17:22:49 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,10 +15,13 @@ t_bool parse_ceilling_color(t_parsing *parsing, char *line) { line++; - parsing->ceilling_color.rgb.r = ft_atoi(line); + if ((parsing->ceilling_color.rgb.r = ft_atoi(line)) > 255) + return (FALSE); line = ft_strchr(line, ',') + 1; - parsing->ceilling_color.rgb.g = ft_atoi(line); + if ((parsing->ceilling_color.rgb.g = ft_atoi(line)) > 255) + return (FALSE); line = ft_strchr(line, ',') + 1; - parsing->ceilling_color.rgb.b = ft_atoi(line); + if ((parsing->ceilling_color.rgb.b = ft_atoi(line)) > 255) + return (FALSE); return (TRUE); } diff --git a/parse/parse_floor_color.c b/parse/parse_floor_color.c index 3885586..ab16bd9 100644 --- a/parse/parse_floor_color.c +++ b/parse/parse_floor_color.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 09:29:32 by cacharle #+# #+# */ -/* Updated: 2019/11/18 02:43:36 by cacharle ### ########.fr */ +/* Updated: 2019/11/18 17:23:26 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,10 +15,13 @@ t_bool parse_floor_color(t_parsing *parsing, char *line) { line++; - parsing->floor_color.rgb.r = ft_atoi(line); + if ((parsing->floor_color.rgb.r = ft_atoi(line)) > 255) + return (FALSE); line = ft_strchr(line, ',') + 1; - parsing->floor_color.rgb.g = ft_atoi(line); + if ((parsing->floor_color.rgb.g = ft_atoi(line)) > 255) + return (FALSE); line = ft_strchr(line, ',') + 1; - parsing->floor_color.rgb.b = ft_atoi(line); + if ((parsing->floor_color.rgb.b = ft_atoi(line)) > 255) + return (FALSE); return (TRUE); } |
