From 628f82dbb9d44dfda7ebde0a54b6559b275e204c Mon Sep 17 00:00:00 2001 From: Charles Date: Sun, 2 Feb 2020 09:57:02 +0100 Subject: sprite kinda working but like walls (fml) --- include/cub3d.h | 7 ++++--- minimalist.cub | 4 ++-- src/capture.c | 4 ++-- src/event.c | 5 +++-- src/render.c | 27 +++++++++++++++------------ src/render_state.c | 6 +++--- src/state.c | 16 +++++++++++++++- src/texture.c | 4 +++- textures/barrel.xpm | 2 +- 9 files changed, 48 insertions(+), 27 deletions(-) diff --git a/include/cub3d.h b/include/cub3d.h index 9c609d8..12c6fe1 100644 --- a/include/cub3d.h +++ b/include/cub3d.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:40:37 by cacharle #+# #+# */ -/* Updated: 2020/02/01 14:06:36 by cacharle ### ########.fr */ +/* Updated: 2020/02/02 09:36:22 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include @@ -56,7 +56,7 @@ typedef union t_byte b; t_byte g; t_byte r; - t_byte empty; + t_byte alpha; } rgb; } t_color; @@ -109,7 +109,7 @@ typedef struct s_state t_color ceilling_color; t_color floor_color; t_image window; - t_image window_sprite; + t_image sprite_window; t_image *surface; char *textures_path[TEXTURES_NUM]; t_image textures[TEXTURES_NUM]; @@ -134,6 +134,7 @@ typedef struct s_render_state t_vector map_step; int draw_start; int draw_end; + t_cell target; } t_render_state; typedef t_bool (*t_option_parser_func)(t_state *state, char *line); diff --git a/minimalist.cub b/minimalist.cub index 413dcfb..aefdc70 100644 --- a/minimalist.cub +++ b/minimalist.cub @@ -6,7 +6,7 @@ EA ./textures/eagle.xpm S ./textures/barrel.xpm F 10,10,10 -C 20,20,0 +C 40,40,40 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 @@ -17,7 +17,7 @@ C 20,20,0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 0 0 1 0 0 0 1 1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 1 -1 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 1 +1 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 2 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1 0 0 0 1 1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 0 0 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 N 0 1 diff --git a/src/capture.c b/src/capture.c index 042eb14..a052709 100644 --- a/src/capture.c +++ b/src/capture.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/11 13:15:11 by cacharle #+# #+# */ -/* Updated: 2020/02/01 13:49:43 by cacharle ### ########.fr */ +/* Updated: 2020/02/02 08:33:14 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ int capture(t_state *state) { t_bmp_header header; - render_update_window(state); + render_update_window(state, CELL_WALL); bmp_fill_header(&state->window, &header); if (!bmp_write(&state->window, &header)) { diff --git a/src/event.c b/src/event.c index b1d465f..3517978 100644 --- a/src/event.c +++ b/src/event.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:39:37 by cacharle #+# #+# */ -/* Updated: 2020/02/01 14:00:48 by cacharle ### ########.fr */ +/* Updated: 2020/02/02 09:52:09 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,7 +38,8 @@ int event_keydown(int key, t_state *state) helper_rotate_player(state, -ROTATE_STEP); else if (key == MLXK_RIGHT) helper_rotate_player(state, ROTATE_STEP); - if (state->map[(int)state->pos.y][(int)state->pos.x] == CELL_WALL) + if (state->map[(int)state->pos.y][(int)state->pos.x] == CELL_WALL || + state->map[(int)state->pos.y][(int)state->pos.x] == CELL_ITEM) state->pos = saved_pos; return (0); } diff --git a/src/render.c b/src/render.c index fec701c..5d3c5d4 100644 --- a/src/render.c +++ b/src/render.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/11 13:37:17 by cacharle #+# #+# */ -/* Updated: 2020/02/01 14:15:44 by cacharle ### ########.fr */ +/* Updated: 2020/02/02 09:56:37 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,10 +25,14 @@ int render_update(void *param) } state->surface = &state->window; render_update_window(state, CELL_WALL); - /* state->surface = &state->sprite_window; */ - /* render_update_window(state, CELL_ITEM); */ mlx_put_image_to_window(state->mlx_ptr, state->window_ptr, - state->window.id, 0, 0); + state->surface->id, 0, 0); + state->surface = &state->sprite_window; + for (int i = 0; i < state->surface->width * state->surface->height; i++) + ((unsigned int*)state->surface->data)[i] = 0xff000000; + render_update_window(state, CELL_ITEM); + mlx_put_image_to_window(state->mlx_ptr, state->window_ptr, + state->surface->id, 0, 0); return (0); } @@ -37,7 +41,7 @@ void render_update_window(t_state *state, t_cell target) int x; x = -1; - while (++x < state->window.width) + while (++x < state->surface->width) render_column(state, x, target); } @@ -45,6 +49,7 @@ void render_column(t_state *state, int x, t_cell target) { t_render_state rstate; + rstate.target = target; rstate.x = x; rstate_ray(state, &rstate); rstate.map_pos = vector_new((double)((int)state->pos.x), (double)((int)state->pos.y)); @@ -53,13 +58,11 @@ void render_column(t_state *state, int x, t_cell target) rstate.map_step = vector_new(rstate.ray.x < 0.0 ? -1.0 : 1.0, rstate.ray.y < 0.0 ? -1.0 : 1.0); while (TRUE) { - if (rstate.probe.x < 0 || rstate.probe.y < 0 - || rstate.probe.x > state->map_width - || rstate.probe.y > state->map_height) - return ; rstate.side = rstate.probe.x < rstate.probe.y ? SIDE_WE : SIDE_NS; rstate_next_probe(&rstate); - if (state->map[(int)rstate.map_pos.y][(int)rstate.map_pos.x] == CELL_WALL) + if (target == CELL_ITEM && state->map[(int)rstate.map_pos.y][(int)rstate.map_pos.x] == CELL_WALL) + return ; + if (state->map[(int)rstate.map_pos.y][(int)rstate.map_pos.x] == target) break ; } rstate_perp_dist(state, &rstate); @@ -106,7 +109,7 @@ void render_texture(t_state *state, t_render_state *rstate, int *i) { tex_y = (int)tex_pos & (texture->height - 1); tex_pos += tex_step; - /* ((t_color*)state->surface->data)[*i * state->surface->width + rstate->x] = */ - /* ((t_color*)texture->data)[texture->height * tex_y + tex_x]; */ + ((t_color*)state->surface->data)[*i * state->surface->width + rstate->x] = + ((t_color*)texture->data)[texture->height * tex_y + tex_x]; } } diff --git a/src/render_state.c b/src/render_state.c index ff777e2..09afe25 100644 --- a/src/render_state.c +++ b/src/render_state.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/15 14:40:14 by cacharle #+# #+# */ -/* Updated: 2020/02/01 11:39:09 by cacharle ### ########.fr */ +/* Updated: 2020/02/02 09:00:38 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,7 @@ void rstate_ray(t_state *state, t_render_state *rstate) { double camera_x; - camera_x = 2.0 * (double)rstate->x / (double)state->window.width - 1.0; + camera_x = 2.0 * (double)rstate->x / (double)state->surface->width - 1.0; rstate->ray = vector_add(state->dir, vector_scale(state->plane, camera_x)); } @@ -157,5 +157,5 @@ void rstate_perp_dist(t_state *state, t_render_state *rstate) void rstate_line_height(t_state *state, t_render_state *rstate) { - rstate->line_height = (int)((double)state->window.height / rstate->perp_dist); + rstate->line_height = (int)((double)state->surface->height / rstate->perp_dist); } diff --git a/src/state.c b/src/state.c index 11e1c65..267c6c9 100644 --- a/src/state.c +++ b/src/state.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/19 16:39:57 by cacharle #+# #+# */ -/* Updated: 2020/01/30 15:13:42 by cacharle ### ########.fr */ +/* Updated: 2020/02/02 09:13:06 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,6 +42,15 @@ t_state *state_new(t_state *state) &state->window.depth, &state->window.size_line, &state->window.endian); printf("init mlx window image\n"); + state->sprite_window.width = state->window.width; + state->sprite_window.height = state->window.height; + if ((state->sprite_window.id = mlx_new_image(state->mlx_ptr, + state->window.width, state->window.height)) == NULL) + return (state_destroy(state)); + state->sprite_window.data = mlx_get_data_addr(state->sprite_window.id, + &state->sprite_window.depth, &state->sprite_window.size_line, + &state->sprite_window.endian); + printf("init mlx sprite image\n"); state_init_player(state); printf("init player\n"); return (state); @@ -75,6 +84,8 @@ t_state *state_new_empty(void) return (NULL); state->mlx_ptr = NULL; state->window_ptr = NULL; + state->window.id = NULL; + state->sprite_window.id = NULL; i = -1; while (++i < TEXTURES_NUM) { @@ -105,6 +116,9 @@ void *state_destroy(t_state *state) printf("free window image\n"); if (state->mlx_ptr != NULL && state->window.id != NULL) mlx_destroy_image(state->mlx_ptr, state->window.id); + printf("free sprite window image\n"); + if (state->mlx_ptr != NULL && state->sprite_window.id != NULL) + mlx_destroy_image(state->mlx_ptr, state->sprite_window.id); printf("free window\n"); if (state->mlx_ptr && state->window_ptr) mlx_destroy_window(state->mlx_ptr, state->window_ptr); diff --git a/src/texture.c b/src/texture.c index 5f858eb..ba95bd4 100644 --- a/src/texture.c +++ b/src/texture.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/01 11:38:43 by cacharle #+# #+# */ -/* Updated: 2020/02/01 14:11:07 by cacharle ### ########.fr */ +/* Updated: 2020/02/02 09:25:36 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,8 @@ t_image *texture_select(t_state *state, t_render_state *rstate) { + if (rstate->target == CELL_ITEM) + return (state->textures + TEX_SPRITE); if (rstate->side == SIDE_NS) { if (rstate->map_pos.y < state->pos.y) diff --git a/textures/barrel.xpm b/textures/barrel.xpm index 7d45baf..4d4c1d2 100644 --- a/textures/barrel.xpm +++ b/textures/barrel.xpm @@ -2,7 +2,7 @@ static char *barrel[] = { /* columns rows colors chars-per-pixel */ "64 64 27 1 ", -" c black", +" c None", ". c gray11", "X c #231C0A", "o c #312611", -- cgit