diff options
-rw-r--r-- | inc/cub3d.h | 4 | ||||
-rw-r--r-- | inc/cub3d_defines.h | 4 | ||||
-rw-r--r-- | inc/cub3d_structs.h | 47 | ||||
-rw-r--r-- | map/map_one.cub | 4 | ||||
-rw-r--r-- | src/ft_draw_scene.c | 2 | ||||
-rw-r--r-- | src/ft_draw_sprite.c | 128 | ||||
-rw-r--r-- | src/ft_draw_verline.c | 1 | ||||
-rw-r--r-- | src/ft_get_player_spawn.c | 1 | ||||
-rw-r--r-- | src/ft_get_sprite.c | 9 | ||||
-rw-r--r-- | src/ft_key_loop.c | 2 | ||||
-rw-r--r-- | src/ft_raycasting.c | 8 |
11 files changed, 157 insertions, 53 deletions
diff --git a/inc/cub3d.h b/inc/cub3d.h index a961dcb..615112b 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -68,11 +68,13 @@ void ft_draw_texture(t_cub *cl, int x, int y, int tex_y); int8_t ft_draw_hud(t_cub *clist); void ft_draw_scene(t_cub *clist); void ft_draw_scene_bmp(t_cub *clist); -void ft_draw_sprite(t_cub *cl, int i); void ft_draw_circle(float a, float b, int32_t color, t_cub *cl); int8_t ft_draw_verline(t_cub *cl, int32_t x, int32_t y1, int32_t y2); +void ft_calc_sprite(t_cub *cl); +void ft_draw_sprite(t_cub *cl, t_sprite *sprite); +void ft_get_sprite_spawn(t_cub *cl); /* ** ====== PARSING ====== diff --git a/inc/cub3d_defines.h b/inc/cub3d_defines.h index f20e5d4..b3dd986 100644 --- a/inc/cub3d_defines.h +++ b/inc/cub3d_defines.h @@ -93,8 +93,8 @@ enum */ # define FT_MOVE_SPEED 0.2 -# define FT_STRAFE_SPEED 0.1 -# define FT_ROT_SPEED 0.09 +# define FT_STRAFE_SPEED 0.2 +# define FT_ROT_SPEED 0.2 /* ** ====== COLLISION ====== diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h index 1160015..3c7ae28 100644 --- a/inc/cub3d_structs.h +++ b/inc/cub3d_structs.h @@ -86,25 +86,29 @@ typedef struct s_rgb typedef struct s_sprite { - int32_t s_screen_x; - int32_t s_pos_x; - int32_t s_pos_y; - double s_x; - double s_y; - int32_t s_h; - int32_t s_w; - int32_t s_start_x; - int32_t s_start_y; - int32_t s_end_x; - int32_t s_end_y; - int32_t s_tex_y; - double sprite_transform_x; - double sprite_transform_y; - double sprite_dist; - double inv_c_m; -} t_sprite; - -typedef struct s_player + int32_t spritescreenx; + int32_t x; + int32_t y; + int32_t tex_x; + int32_t tex_y; + int32_t s_pos_x; + int32_t s_pos_y; + double spritex; + double spritey; + int32_t spriteheight; + int32_t spritewidth; + int32_t drawstartx; + int32_t drawstarty; + int32_t drawendx; + int32_t drawendy; + int32_t s_tex_y; + double transformx; + double transformy; + double sprite_dist; + double invdet; +} t_sprite; + +typedef struct s_player { float pos_x; float pos_y; @@ -122,6 +126,7 @@ typedef struct s_ray { uint16_t line_h; float wall_dist; + float *wall_dist_tab; float x_ray_pos; float y_ray_pos; float x_ray_dir; @@ -172,6 +177,8 @@ typedef struct s_map size_t map_w; size_t map_h; size_t mapl_len; + int32_t sprite_nbr; + int32_t sprite_order[12]; size_t line_chk; size_t map_start; uint8_t isspawn; @@ -210,7 +217,7 @@ typedef struct s_cub struct s_rgb f_rgb; struct s_rgb c_rgb; struct s_img tlist[8]; - struct s_sprite sp_list; + struct s_sprite sprites[12]; } t_cub; # endif diff --git a/map/map_one.cub b/map/map_one.cub index 9a82a7f..6a8ad51 100644 --- a/map/map_one.cub +++ b/map/map_one.cub @@ -9,11 +9,11 @@ S ./media/img/pillar.xpm C 50,100,200 F 50,190,124 -SH 4 +SH 6 111111111111111111 120000000011000001 -10010E000000000001 +10010E000000000201 101100000111000001 111111111111110011 1000001 100001 diff --git a/src/ft_draw_scene.c b/src/ft_draw_scene.c index 087457d..68a72e9 100644 --- a/src/ft_draw_scene.c +++ b/src/ft_draw_scene.c @@ -50,6 +50,7 @@ void { ft_draw_hud(clist); } + ft_calc_sprite(clist); mlx_put_image_to_window(clist->wlist.wlx, clist->wlist.winptr, clist->img.img, 0, 0); if (clist->ishud && clist->mlist.isnlvl) @@ -70,6 +71,7 @@ void clist->img.ptr = mlx_get_data_addr(clist->img.img, &clist->img.bpp, &clist->img.sizeline, &clist->img.endian); ft_castray(clist); + ft_calc_sprite(clist); if (ft_save_to_bmp(clist) < 0) ft_error(FT_RET_BMP_ERR, FT_ERR_WR_BMP, clist); mlx_destroy_image(clist->wlist.wlx, clist->img.img); diff --git a/src/ft_draw_sprite.c b/src/ft_draw_sprite.c index 9dc1b63..1393d38 100644 --- a/src/ft_draw_sprite.c +++ b/src/ft_draw_sprite.c @@ -18,36 +18,114 @@ #include <math.h> void - ft_draw_verline_sprite(t_cub *cl, int x, int y, int tex_y) + ft_sort_sprites(t_cub *cl) { - if (cl->tlist[4].tex_x) - cl->img.ptr[x * 4 + (cl->img.sizeline * y)] = - (char)cl->tlist[4].ptr[cl->tlist[4].tex_x * 4 + 4 * - cl->tlist[4].img_h * tex_y]; - cl->img.ptr[x * 4 + (cl->img.sizeline * y) + 1] = - (char)cl->tlist[4].ptr[cl->tlist[4].tex_x * 4 + 4 * - cl->tlist[4].img_h * tex_y + 1]; - cl->img.ptr[x * 4 + (cl->img.sizeline * y) + 2] = - (char)cl->tlist[4].ptr[cl->tlist[4].tex_x * 4 + 4 * - cl->tlist[4].img_h * tex_y + 2]; - cl->img.ptr[x * 4 + cl->wlist.x_size * y + 3] = (char)0; + float dist_tab[4096]; + uint32_t tmp; + int32_t it; + + it = 0; + while (it < cl->mlist.sprite_nbr) + { + dist_tab[it] = ((cl->plist.pos_x - cl->sprites[it].s_pos_x) * + (cl->plist.pos_x - cl->sprites[it].s_pos_x) + + (cl->plist.pos_y - cl->sprites[it].s_pos_y) * + (cl->plist.pos_y - cl->sprites[it].s_pos_y)); + cl->mlist.sprite_order[it] = it; + it++; + } + it = 0; + while (it < cl->mlist.sprite_nbr) + { + if (dist_tab[it] > dist_tab[it + 1]) + { + tmp = dist_tab[it]; + dist_tab[it] = dist_tab[it + 1]; + dist_tab[it + 1] = tmp; + tmp = cl->mlist.sprite_order[it]; + cl->mlist.sprite_order[it] = cl->mlist.sprite_order[it + 1]; + cl->mlist.sprite_order[it + 1] = tmp; + it = 0; + } + } +} + +void + ft_sprite_height(t_cub *cl, t_sprite *sprite) +{ + sprite->spriteheight = abs((int)(cl->wlist.y_size / (sprite->transformy))); + sprite->drawstarty = -sprite->spriteheight / 2 + cl->wlist.y_size / 2; + if(sprite->drawstarty < 0) + sprite->drawstarty = 0; + sprite->drawendy = sprite->spriteheight / 2 + cl->wlist.y_size / 2; + if(sprite->drawendy >= (int)cl->wlist.y_size) + sprite->drawendy = cl->wlist.y_size - 1; +} + +void + ft_sprite_width(t_cub *cl, t_sprite *sprite) +{ + sprite->spritewidth = abs((int)(cl->wlist.x_size / (sprite->transformy))); + sprite->drawstartx = -sprite->spritewidth / 2 + sprite->spritescreenx; + if(sprite->drawstartx < 0) + sprite->drawstartx = 0; + sprite->drawendx = sprite->spritewidth / 2 + sprite->spritescreenx; + if(sprite->drawendx >= (int)cl->wlist.x_size) sprite->drawendx = cl->wlist.x_size - 1; +} + +void + ft_calc_sprite(t_cub *cl) +{ + t_sprite sprite; + int i = 0; + + while (i < cl->mlist.sprite_nbr) + { + sprite = cl->sprites[i]; + sprite.spritey = sprite.s_pos_x - (cl->plist.pos_x - 0.5); + sprite.spritex = sprite.s_pos_y - (cl->plist.pos_y - 0.5); + sprite.invdet = 1.0 / (cl->plist.plane_x * cl->plist.dir_y - cl->plist.dir_x * cl->plist.plane_y); + sprite.transformx = sprite.invdet * (cl->plist.dir_y * sprite.spritex- cl->plist.dir_x* sprite.spritey); + sprite.transformy = sprite.invdet * (-cl->plist.plane_y* sprite.spritex + cl->plist.plane_x * sprite.spritey); + sprite.spritescreenx = (int)(cl->wlist.x_size / 2) * (1 + sprite.transformx / sprite.transformy); + ft_sprite_height(cl, &sprite); + ft_sprite_width(cl, &sprite); + ft_draw_sprite(cl, &sprite); + i++; + } } void - ft_draw_sprite(t_cub *cl, int x) + ft_draw_sprite(t_cub *cl, t_sprite *sprite) { - int hor_it; - int d; - int tex_y; + int32_t d; - hor_it = cl->sp_list.s_start_y; - while (hor_it < cl->sp_list.s_end_y) + sprite->x = sprite->drawstartx; + while (sprite->x < sprite->drawendx) { - d = hor_it * 256 - cl->wlist.y_size * 128 + cl->rlist.line_h * 128; - d = (d <= 0) ? (-d) : (d); - tex_y = ((d * cl->tlist[4].img_h) / cl->rlist.line_h) / 256; - (tex_y < 0) ? (tex_y = 0) : 0; - ft_draw_verline_sprite(cl, x, hor_it, tex_y); - hor_it++; - } + sprite->tex_x = (int32_t)((sprite->x - (-sprite->spritewidth / 2 + + sprite->spritescreenx)) * cl->tlist[4].img_w / sprite->spritewidth); + sprite->y = sprite->drawstarty; + while (sprite->y < sprite->drawendy) + { + d = sprite->y * 256 - cl->wlist.y_size * 128 + + sprite->spriteheight * 128; + sprite->tex_y = ((d * cl->tlist[4].img_h / 2) / + sprite->spriteheight) / 128; + if (cl->tlist[4].ptr[sprite->tex_x * 4 + 4 * cl->tlist[4].img_h * sprite->tex_y] + && cl->rlist.wall_dist_tab[sprite->x] > sprite->transformy) + { + cl->img.ptr[sprite->x * 4 + (sprite->y * cl->img.sizeline)] = + cl->tlist[4].ptr[sprite->tex_x * 4 + 4 * cl->tlist[4].img_h * sprite->tex_y]; + cl->img.ptr[sprite->x * 4 + (sprite->y * + cl->img.sizeline) + 1] = + cl->tlist[4].ptr[sprite->tex_x * 4 + 4 * cl->tlist[4].img_h * sprite->tex_y + 1]; + cl->img.ptr[sprite->x * 4 + (sprite->y * + cl->img.sizeline) + 2] = + cl->tlist[4].ptr[sprite->tex_x * 4 + 4 * cl->tlist[4].img_h * sprite->tex_y + 2]; + } + sprite->y++; + } + sprite->x++; + } } diff --git a/src/ft_draw_verline.c b/src/ft_draw_verline.c index 7bfba8d..16dc63c 100644 --- a/src/ft_draw_verline.c +++ b/src/ft_draw_verline.c @@ -10,6 +10,7 @@ /* */ /* ************************************************************************** */ +#include <stdio.h> #include <cub3d.h> #include <stdint.h> diff --git a/src/ft_get_player_spawn.c b/src/ft_get_player_spawn.c index c506775..6853313 100644 --- a/src/ft_get_player_spawn.c +++ b/src/ft_get_player_spawn.c @@ -82,6 +82,7 @@ void plist->start_x = plist->pos_x; plist->start_y = plist->pos_y; ft_get_start_side(clist->mlist.map[y][x], plist); + ft_get_sprite_spawn(clist); return ; } x++; diff --git a/src/ft_get_sprite.c b/src/ft_get_sprite.c index 4580681..10c6bdd 100644 --- a/src/ft_get_sprite.c +++ b/src/ft_get_sprite.c @@ -19,18 +19,21 @@ void { size_t x; size_t y; + uint8_t i; x = 1; y = 1; + i = 0; while (clist->mlist.map[y]) { while (clist->mlist.map[y][x]) { if (ft_ischarset("2", clist->mlist.map[y][x])) { - clist->sp_list.s_pos_x = x + 0.5; - clist->sp_list.s_pos_y = y + 0.5; - return ; + clist->mlist.sprite_nbr++; + clist->sprites[i].s_pos_x = x; + clist->sprites[i].s_pos_y = y; + i++; } x++; } diff --git a/src/ft_key_loop.c b/src/ft_key_loop.c index 1d463bc..d40de34 100644 --- a/src/ft_key_loop.c +++ b/src/ft_key_loop.c @@ -81,6 +81,8 @@ int i++; } if (cl->key_input[0] != -1) + { ft_draw_scene(cl); + } return (0); } diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c index bf84b9e..9657b18 100644 --- a/src/ft_raycasting.c +++ b/src/ft_raycasting.c @@ -13,6 +13,7 @@ #include <libft.h> #include <cub3d.h> #include <stdint.h> +#include <stdio.h> #include <stdlib.h> #include <math.h> @@ -71,11 +72,16 @@ void { uint16_t i; t_win *wl; + float *dist_tab; wl = &cl->wlist; i = (wl->y_size / 2) + 1; while (++i < wl->y_size) ft_floor_cast(i, cl); + if (!(dist_tab = malloc(sizeof(float) * cl->wlist.x_size))) + return ; + if (!(cl->rlist.wall_dist_tab = malloc(sizeof(float) * cl->wlist.x_size))) + return ; i = 0; while (i < wl->x_size) { @@ -90,6 +96,8 @@ void ft_choose_tex(cl); ft_calc_tex(cl); ft_draw_verline(cl, i, cl->rlist.wall_t, cl->rlist.wall_b); + dist_tab[i] = cl->rlist.wall_dist; + cl->rlist.wall_dist_tab = dist_tab; i++; } } |