diff options
author | Rudy Bousset <rbousset@z2r4p3.le-101.fr> | 2020-02-24 19:08:03 +0100 |
---|---|---|
committer | Rudy Bousset <rbousset@z2r4p3.le-101.fr> | 2020-02-24 19:08:03 +0100 |
commit | 7b7d5343c6042ce90ed213e967c34a07875e2e85 (patch) | |
tree | 0943f8592c6d480a85099750dbbd6b9b045e66eb | |
parent | Bug fix, 1px bug is back (diff) | |
parent | oy (diff) | |
download | 42-cub3d-7b7d5343c6042ce90ed213e967c34a07875e2e85.tar.gz 42-cub3d-7b7d5343c6042ce90ed213e967c34a07875e2e85.tar.bz2 42-cub3d-7b7d5343c6042ce90ed213e967c34a07875e2e85.tar.xz 42-cub3d-7b7d5343c6042ce90ed213e967c34a07875e2e85.tar.zst 42-cub3d-7b7d5343c6042ce90ed213e967c34a07875e2e85.zip |
Merge branch 'sprites'
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | inc/cub3d.h | 1 | ||||
-rw-r--r-- | inc/cub3d_structs.h | 12 | ||||
-rw-r--r-- | map/map_one.cub | 12 | ||||
-rw-r--r-- | media/img/pillar.xpm | 91 | ||||
-rw-r--r-- | src/ft_detect.c | 3 | ||||
-rw-r--r-- | src/ft_draw_sprite.c | 41 | ||||
-rw-r--r-- | src/ft_get_sprite.c | 20 | ||||
-rw-r--r-- | src/ft_raycasting.c | 30 |
9 files changed, 207 insertions, 4 deletions
@@ -61,6 +61,7 @@ SRCS_NAME += ft_convert_keycode.c SRCS_NAME += ft_key_loop.c SRCS_NAME += ft_draw_hud.c SRCS_NAME += ft_music.c +SRCS_NAME += ft_draw_sprite.c #--------------------------------------------------------------------------------------------------# SRCS = $(addprefix ${SRCS_DIR},${SRCS_NAME}) #--------------------------------------------------------------------------------------------------# diff --git a/inc/cub3d.h b/inc/cub3d.h index cf855b2..5f1c6f8 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -90,5 +90,6 @@ void ft_draw_life_bar(size_t map_w, const int8_t life, t_cub *cl); void ft_music(t_cub *cl); +void ft_draw_sprite(t_cub *cl, int i); # endif diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h index 639de11..a173e8c 100644 --- a/inc/cub3d_structs.h +++ b/inc/cub3d_structs.h @@ -49,11 +49,22 @@ 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 @@ -126,6 +137,7 @@ typedef struct s_cub struct s_rgb f_rgb; struct s_rgb c_rgb; struct s_img tlist[5]; + struct s_sprite sp_list; } t_cub; # endif diff --git a/map/map_one.cub b/map/map_one.cub index 22f3bc5..cc4b774 100644 --- a/map/map_one.cub +++ b/map/map_one.cub @@ -1,10 +1,18 @@ R 1400 900 +<<<<<<< HEAD NO ./media/img/BRIQUASSES.xpm SO ./media/img/BRIQUASSE_3.xpm EA ./media/img/BRIQUASSES_2.xpm WE ./media/img/MURLOL.xpm S ./media/img/MURLOL.xpm +======= +NO ./map/img/BRIQUASSES.xpm +SO ./map/img/BRIQUASSE_3.xpm +EA ./map/img/BRIQUASSES_2.xpm +WE ./map/img/MURLOL.xpm +S ./map/img/pillar.xpm +>>>>>>> sprites C 50,100,200 F 50,190,124 @@ -12,7 +20,11 @@ F 50,190,124 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 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +<<<<<<< HEAD 1 S 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 1 +======= +1 E 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 1 +>>>>>>> sprites 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 diff --git a/media/img/pillar.xpm b/media/img/pillar.xpm new file mode 100644 index 0000000..4a0c656 --- /dev/null +++ b/media/img/pillar.xpm @@ -0,0 +1,91 @@ +/* XPM */ +static char *_8e53538c4e441a6b69ed6027602859e[] = { +/* columns rows colors chars-per-pixel */ +"64 64 21 1 ", +" c black", +". c #202020", +"X c #2C2C2C", +"o c gray22", +"O c gray25", +"+ c #484848", +"@ c #4C4C4C", +"# c gray33", +"$ c gray36", +"% c #646464", +"& c gray44", +"* c #7C7C7C", +"= c gray55", +"- c #989898", +"; c gray66", +": c #B4B4B4", +"> c #C0C0C0", +", c #D0D0D0", +"< c gainsboro", +"1 c #ECECEC", +"2 c gray99", +/* pixels */ +" ;;;;=======&&&&% ", +" ;;<<,,,>>>::;;--==*&&%#+## ", +" >22111<<<,,,>>::;-&>=*&%#+OooX ", +" 12222111222<<,>>::;--=**+#+Ooo.X ", +" <112222221<,>>::;;-=**&%%%%oOoX.XX ", +" <<1122211,,,>>;,;;-=**&%%#O+XX..XX ", +" ><<2122111<<,,>>::;-=*&%#+OXo..XXX ", +" >>211221122<<,>>;;-=*&%#OoX...XX ", +" :1122221<,>;:-=*&%#+OXoX.... ", +" :>>2221<,>:;-=*&%#+OoXX..X ", +" ->1221<,>:;-=*&#%+OoX..X ", +" ->1221<,>:;-=*&%#+OoX..X ", +" ->2221<,>:;=;*&%#+OXo..X ", +" ->2221<,>:=-;*&%#+OoX..X ", +" ->2221<,>:;-=*&%O%OoX..X ", +" ->2221<,>:;-=*&%#%OoX..X ", +" ->11<,>:;-=*&%#+O+X....X ", +" -=;:;;==*&&%%#+OoX...... ", +" <2<1221<,>:;-=*&#%+o.X ", +" ><2222<,>:;-=*&%#+#oXX.o ", +" ><2221<,,:;-*=&%#+OoX..X ", +" ->2221<:1:;-=&*%#+OoX..X ", +" ->2221<>1:;-=*&%#+OXo..X ", +" ->2221<,,:;-=*&#%+OoX .X ", +" ->2221<,>:;-=*&%#+X+X..X ", +" -;2221:2>:;-=*&%#+OoX..X ", +" -<2221<,>:;-*-&%#+oOX..X ", +" ->2221<:<:;-=*&%+#OoX..X ", +" ->2221<,>:;=-*&%#+OXo..X ", +" *-<1<,>:;-=*&%#+OoX..... ", +" *-;-==**&&%%#+OoX.. ", +" ;,22222><,>:;-=*&%#+oXXo ", +" ->2221<1>:;-=*&%#+OoX..X ", +" ->2221<,>:;=-*%*#+OXO .X ", +" ->2221<:1:;-=*&%#O+oX..X ", +" ->22<2<,>:;-=*&%+#Oo.X.X ", +" ->2121<:>;:-*=&%##OoX... ", +" ->2221:2>:;-=*&%#+oOX..X ", +" ->22212,>:;-=&*%#+OoX..X ", +" ->22<2<,>:;=-*&%#O+Xo..X ", +" ->2221<,:<;-=*&#%+OoX..X ", +" ->2221,1>:;-=&*%#+OXo..X ", +" *>1211<,>:--=*&%+#OoX..o ", +" ;,<>>>:;-&-&%#+OoX.... ", +" ;1:>,>-=*&#%%#+OoX...... ", +" -,212221<,>:=-=*&%#+o.XX ", +" ->2221<,>:;-*-&%#+OXO..X ", +" ->222,2,>-<-=*&%#+OoX..X ", +" ->2221<,>:;-*-&%+#OoX..X ", +" ->2221<,>:;-=*&#%+OoX..X ", +" ->2221<,>->-=*%&#+OoX..X ", +" ->2221<,;<;-=*&%#O#oX..X ", +" ->122,2,>:;-=*&%#+OoX..X ", +" ->122,2,>:;-=*&%+#OoX..X ", +" :<>2221<,>:-:=*&%#+OoXo..X ", +" :1122221<,>:;-=*&%#+OXoX...XX ", +" >>212221122<<,>>;;-=*&%#OoX...XXXO+@##$$%% ", +" ><<2122111<<,,>>::;-=*&%#+OXo..XXXooO++@##$%% ", +" <<1122211,,,>>::;;=;**&%%#O+XX..XXooooO++@#$$% ", +" <112222221<,>>::;;-=**&%%&%@@oX.XXooooO++@#$$% ", +" 12222111222<<,>>::;--=**&$O$oo.XoooOO+@@#$$% ", +" >22111<<<,,,>:,:;--=*=&%$+OooXOOO+++@#$$% ", +" ;;<<,,,>>>::;;--==*&&%#+OO+#$$$%%%%%% ", +" ;;;;=======&&&&%#OO+#$%% " +}; diff --git a/src/ft_detect.c b/src/ft_detect.c index fc49a2d..10d7147 100644 --- a/src/ft_detect.c +++ b/src/ft_detect.c @@ -74,8 +74,7 @@ void cl->rlist.sqy += cl->mlist->y_step; cl->rlist.side = 1; } - if (cl->mlist->map[cl->rlist.sqx][cl->rlist.sqy] == '1' - || cl->mlist->map[cl->rlist.sqx][cl->rlist.sqy] == '2') + if (cl->mlist->map[cl->rlist.sqx][cl->rlist.sqy] == '1') cl->rlist.hit = 1; } } diff --git a/src/ft_draw_sprite.c b/src/ft_draw_sprite.c new file mode 100644 index 0000000..c90c9ac --- /dev/null +++ b/src/ft_draw_sprite.c @@ -0,0 +1,41 @@ +#include <libft.h> +#include <cub3d.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <math.h> + +void +ft_draw_verline_sprite(t_cub *cl, int x, int y, int tex_y) +{ + 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; +} + +void + ft_draw_sprite(t_cub *cl, int x) +{ + int hor_it;/*y*/ + int d; + int tex_y; + + hor_it = cl->sp_list.s_start_y; + while (hor_it < cl->sp_list.s_end_y) + { + 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++; + } +} diff --git a/src/ft_get_sprite.c b/src/ft_get_sprite.c index 43de290..806489f 100644 --- a/src/ft_get_sprite.c +++ b/src/ft_get_sprite.c @@ -17,8 +17,26 @@ void ft_get_sprite_spawn(t_cub *clist) { + size_t x; + size_t y; - (void)clist; + x = 1; + y = 1; + 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 ; + } + x++; + } + x = 1; + y++; + } } int8_t diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c index dd2be64..dd68cd3 100644 --- a/src/ft_raycasting.c +++ b/src/ft_raycasting.c @@ -13,9 +13,35 @@ #include <libft.h> #include <cub3d.h> #include <stdint.h> +#include <stdlib.h> #include <math.h> void + ft_calc_sprite(t_cub *cl) +{ + cl->sp_list.s_x = cl->sp_list.s_pos_x - cl->plist->pos_x; + cl->sp_list.s_y = cl->sp_list.s_pos_y - cl->plist->pos_y; + cl->sp_list.inv_c_m = 1.0 / (cl->plist->plane_x * cl->plist->dir_x - cl->plist->dir_y * cl->plist->plane_y); + cl->sp_list.sprite_transform_x = cl->sp_list.inv_c_m * (cl->plist->dir_y * cl->sp_list.s_x - cl->plist->dir_x * cl->sp_list.s_y); + cl->sp_list.sprite_transform_y = cl->sp_list.inv_c_m * (cl->plist->plane_y * cl->sp_list.s_x - cl->plist->plane_x * cl->sp_list.s_y); + cl->sp_list.s_screen_x = (int)((cl->wlist->y_size / 2) * (1 + cl->sp_list.sprite_transform_x / cl->sp_list.sprite_transform_y)); + cl->sp_list.s_h = abs((int)(cl->wlist->y_size * cl->sp_list.sprite_transform_y)); /*sprite height*/ + cl->sp_list.s_start_y = -cl->sp_list.s_h / 2 + cl->wlist->y_size / 2; + if (cl->sp_list.s_start_y < 0) + cl->sp_list.s_start_y = 0; + cl->sp_list.s_end_y = cl->sp_list.s_h / 2 + cl->wlist->y_size / 2; + if (cl->sp_list.s_end_y > (int)cl->wlist->y_size) + cl->sp_list.s_end_y = cl->wlist->y_size - 1;; + cl->sp_list.s_w = abs((int)(cl->wlist->x_size * cl->sp_list.sprite_transform_y)); /*sprite width*/ + cl->sp_list.s_start_x = -cl->sp_list.s_w / 2 + cl->sp_list.s_screen_x; + if (cl->sp_list.s_start_x < 0) + cl->sp_list.s_start_y = 0; + cl->sp_list.s_end_x = cl->sp_list.s_w / 2 + cl->wlist->x_size; + if (cl->sp_list.s_end_x < (int)cl->wlist->x_size) + cl->sp_list.s_end_x = cl->wlist->x_size - 1; +} + +static void ft_calc_tex(t_cub *clist) { if (clist->rlist.side == 0) @@ -24,7 +50,7 @@ void else clist->rlist.wall_hit_x = (clist->plist->pos_y) + clist->rlist.wall_dist * clist->rlist.x_ray_dir; - clist->rlist.wall_hit_x -= floor(clist->rlist.wall_hit_x); + clist->rlist.wall_hit_x -= floor(clist->rlist.wall_hit_x); clist->tlist[clist->w_side].tex_x = (int)(clist->rlist.wall_hit_x * (double)clist->tlist[clist->w_side].img_w); if (clist->rlist.side == 0 && clist->rlist.x_ray_dir > 0) @@ -83,7 +109,9 @@ void cl->rlist.wall_b = wl->y_size - 1; ft_choose_tex(cl); ft_calc_tex(cl); + ft_calc_sprite(cl); ft_draw_verline(cl, i, cl->rlist.wall_t, cl->rlist.wall_b); + ft_draw_sprite(cl , i); i++; } } |