From 2ea2c8b89a4effa67ef492c329c1008435048a3c Mon Sep 17 00:00:00 2001 From: djonker Date: Sun, 5 Nov 2023 06:48:52 +0100 Subject: [PATCH] little cleanup --- cub3d.h | 6 +- src/draw/draw.c | 170 ++++++++++++--------------------------------- src/draw/raycast.c | 52 ++------------ src/sprite/draw.c | 139 ++++++++++++++++++++++++++++++++++++ 4 files changed, 190 insertions(+), 177 deletions(-) create mode 100644 src/sprite/draw.c diff --git a/cub3d.h b/cub3d.h index dc3ea6d..e35569f 100644 --- a/cub3d.h +++ b/cub3d.h @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 10:46:35 by houtworm #+# #+# */ -/* Updated: 2023/11/05 06:38:03 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 06:43:10 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -102,9 +102,9 @@ void scrollhook(double xdelta, double ydelta, void *param); void resizehook(int x, int y, void *param); void cursorhook(double x, double y, void *param); // raycast.c -void ft_drawmap(t_varlist *vl); +void ft_raycast(t_varlist *vl, int x, int mapx, int mapy); // draw.c -void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend); +void ft_drawmap(t_varlist *vl); void ft_drawsprites(t_varlist *vl); // error.c int ft_errorexit(char *reason, char *function, int code); diff --git a/src/draw/draw.c b/src/draw/draw.c index 3f2690a..5001f58 100644 --- a/src/draw/draw.c +++ b/src/draw/draw.c @@ -6,138 +6,12 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */ -/* Updated: 2023/11/05 06:38:15 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 06:46:26 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ #include "../../cub3d.h" -mlx_texture_t *ft_selectsprite(t_varlist *vl, int type) -{ - mlx_texture_t *sprite; - - if (type == 1) - sprite = vl->barreltext; - else if (type == 2) - sprite = vl->slamptext; - else if (type == 3) - sprite = vl->hlamptext; - else if (type == 4) - sprite = vl->treasuretext; - else if (type == 5) - sprite = vl->endtext; - else if (type == 6) - sprite = vl->nazitext; - else - ft_errorexit("what?", "what?", 1); - return (sprite); -} - -void ft_sortsprites(t_varlist *vl) -{ - int i; - t_sprite temp; - - i = 0; - while (vl->spritecount > i) - { - vl->sprite[i].distance = pow((vl->posx - vl->sprite[i].x), 2) + pow((vl->posy - vl->sprite[i].y), 2); - i++; - } - while (i) - { - if (vl->sprite[i].distance > vl->sprite[i - 1].distance) - { - temp = vl->sprite[i]; - vl->sprite[i] = vl->sprite[i - 1]; - vl->sprite[i - 1] = temp; - } - i--; - } -} - -void ft_drawsprites(t_varlist *vl) -{ - int i; - int x; - int y; - double spritex; - double spritey; - double invdet; - double transformx; - double transformy; - int spritescreenx; - int vmovescreen; - int spriteheight; - int spritewidth; - int drawstarty; - int drawstartx; - int drawendy; - int drawendx; - int texx; - int texy; - int d; - uint32_t color; - - i = 0; - ft_sortsprites(vl); - while (vl->spritecount > i) - { - spritex = vl->sprite[i].x - vl->posx; - spritey = vl->sprite[i].y - vl->posy; - vl->curtext = ft_selectsprite(vl, vl->sprite[i].type); - invdet = 1.0 / (vl->planex * vl->diry - vl->dirx * vl->planey); - transformx = invdet * (vl->diry * spritex - vl->dirx * spritey); - transformy = invdet * (-vl->planey * spritex + vl->planex * spritey); - spritescreenx = (vl->w / 2) * (1 + transformx / transformy); - vmovescreen = vl->vaim + vl->jump / transformy; - spriteheight = fabs((vl->h / transformy)); - drawstarty = -spriteheight / 2 + vl->h / 2 + vmovescreen; - if (drawstarty < 0) - drawstarty = 0; - drawendy = spriteheight / 2 + vl->h / 2 + vmovescreen; - if (drawendy >= vl->h) - drawendy = vl->h - 1; - spritewidth = abs((int)(vl->h / transformy)); - drawstartx = -spritewidth / 2 + spritescreenx; - if (drawstartx < 0) - drawstartx = 0; - drawendx = spritewidth / 2 + spritescreenx; - if (drawendx >= vl->w) - drawendx = vl->w - 1; - x = drawstartx; - while (x < drawendx) - { - texx = (int)(256 * (x - (-spritewidth / 2 + spritescreenx)) * 64 / spritewidth) / 256; - if (transformy > 0 && x > 0 && x < vl->w && transformy < vl->distance[x] + 0.7) - { - y = drawstarty; - while (y < drawendy) - { - d = (y - vmovescreen) * 256 - vl->h * 128 + spriteheight * 128; - texy = (d * 64) / spriteheight / 256; - uint8_t *texel; - if (texy < 0) - texy = 0; - if (texy > 64) - texy = 64; - if (texx < 0) - texx = 0; - if (texx > 64) - texx = 64; - texel = &vl->curtext->pixels[(vl->curtext->width * texy + texx) * 4]; - color = texel[0] << 24 | texel[1] << 16 | texel[2] << 8 | texel[3]; - if (color != 0x980088FF) - mlx_put_pixel(vl->img, x, y, color); - y++; - } - } - x++; - } - i++; - } -} - void ft_selecttexture(t_varlist *vl) { if (vl->side == 0) @@ -201,3 +75,45 @@ void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend) y++; } } + +int ft_getwallheight(t_varlist *vl, int mode) +{ + int ret; + + if (mode == 1) + { + ret = -vl->lineheight / 2 + vl->h / 2 + vl->vaim + (vl->jump / vl->walldist); + if (ret < 0) + ret = 0; + } + if (mode == 2) + { + ret = vl->lineheight / 2 + vl->h / 2 + vl->vaim + (vl->jump / vl->walldist); + if (ret >= vl->h) + ret = vl->h - 1; + } + return (ret); +} + +void ft_drawmap(t_varlist *vl) +{ + int x; + int mapx; + int mapy; + + mapx = (int)vl->posx; + mapy = (int)vl->posy; + x = 0; + while (x <= vl->w) + { + ft_raycast(vl, x, mapx, mapy); + ft_drawline(x, vl, ft_getwallheight(vl, 1), ft_getwallheight(vl, 2)); + if (vl->side == 0) + vl->walldist = (vl->sidedistx - vl->deltadistx); + else + vl->walldist = (vl->sidedisty - vl->deltadisty); + vl->lineheight = vl->h / vl->walldist; + vl->distance[x] = vl->walldist; + x++; + } +} diff --git a/src/draw/raycast.c b/src/draw/raycast.c index e446c79..9b9be4b 100644 --- a/src/draw/raycast.c +++ b/src/draw/raycast.c @@ -6,7 +6,7 @@ /* By: djonker +#+ */ /* +#+ */ /* Created: 2023/10/27 14:36:42 by djonker #+# #+# */ -/* Updated: 2023/11/05 06:38:23 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 06:45:03 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -46,7 +46,7 @@ int ft_getstepy(t_varlist *vl, int mapy) return (stepy); } -void ft_prepcast(t_varlist *vl, int x) +int ft_prepcast(t_varlist *vl, int x) { double camerax; @@ -55,17 +55,18 @@ void ft_prepcast(t_varlist *vl, int x) vl->raydiry = vl->diry + vl->planey * camerax; vl->deltadistx = fabs(1 / vl->raydirx); vl->deltadisty = fabs(1 / vl->raydiry); + return (0); } -void ft_raycast(t_varlist *vl, int mapx, int mapy) +void ft_raycast(t_varlist *vl, int x, int mapx, int mapy) { int hit; int stepx; int stepy; + hit = ft_prepcast(vl, x); stepx = ft_getstepx(vl, mapx); stepy = ft_getstepy(vl, mapy); - hit = 0; while (hit == 0) { if (vl->sidedistx < vl->sidedisty) @@ -84,46 +85,3 @@ void ft_raycast(t_varlist *vl, int mapx, int mapy) hit = 1; } } - -int ft_getwallheight(t_varlist *vl, int mode) -{ - int ret; - - if (mode == 1) - { - ret = -vl->lineheight / 2 + vl->h / 2 + vl->vaim + (vl->jump / vl->walldist); - if (ret < 0) - ret = 0; - } - if (mode == 2) - { - ret = vl->lineheight / 2 + vl->h / 2 + vl->vaim + (vl->jump / vl->walldist); - if (ret >= vl->h) - ret = vl->h - 1; - } - return (ret); -} - -void ft_drawmap(t_varlist *vl) -{ - int x; - int mapx; - int mapy; - - mapx = (int)vl->posx; - mapy = (int)vl->posy; - x = 0; - while (x <= vl->w) - { - ft_prepcast(vl, x); - ft_raycast(vl, mapx, mapy); - ft_drawline(x, vl, ft_getwallheight(vl, 1), ft_getwallheight(vl, 2)); - if (vl->side == 0) - vl->walldist = (vl->sidedistx - vl->deltadistx); - else - vl->walldist = (vl->sidedisty - vl->deltadisty); - vl->lineheight = vl->h / vl->walldist; - vl->distance[x] = vl->walldist; - x++; - } -} diff --git a/src/sprite/draw.c b/src/sprite/draw.c new file mode 100644 index 0000000..38ecd8f --- /dev/null +++ b/src/sprite/draw.c @@ -0,0 +1,139 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* draw.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */ +/* Updated: 2023/11/05 06:40:15 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +mlx_texture_t *ft_selectsprite(t_varlist *vl, int type) +{ + mlx_texture_t *sprite; + + if (type == 1) + sprite = vl->barreltext; + else if (type == 2) + sprite = vl->slamptext; + else if (type == 3) + sprite = vl->hlamptext; + else if (type == 4) + sprite = vl->treasuretext; + else if (type == 5) + sprite = vl->endtext; + else if (type == 6) + sprite = vl->nazitext; + else + ft_errorexit("what?", "what?", 1); + return (sprite); +} + +void ft_sortsprites(t_varlist *vl) +{ + int i; + t_sprite temp; + + i = 0; + while (vl->spritecount > i) + { + vl->sprite[i].distance = pow((vl->posx - vl->sprite[i].x), 2) + pow((vl->posy - vl->sprite[i].y), 2); + i++; + } + while (i) + { + if (vl->sprite[i].distance > vl->sprite[i - 1].distance) + { + temp = vl->sprite[i]; + vl->sprite[i] = vl->sprite[i - 1]; + vl->sprite[i - 1] = temp; + } + i--; + } +} + +void ft_drawsprites(t_varlist *vl) +{ + int i; + int x; + int y; + double spritex; + double spritey; + double invdet; + double transformx; + double transformy; + int spritescreenx; + int vmovescreen; + int spriteheight; + int spritewidth; + int drawstarty; + int drawstartx; + int drawendy; + int drawendx; + int texx; + int texy; + int d; + uint32_t color; + + i = 0; + ft_sortsprites(vl); + while (vl->spritecount > i) + { + spritex = vl->sprite[i].x - vl->posx; + spritey = vl->sprite[i].y - vl->posy; + vl->curtext = ft_selectsprite(vl, vl->sprite[i].type); + invdet = 1.0 / (vl->planex * vl->diry - vl->dirx * vl->planey); + transformx = invdet * (vl->diry * spritex - vl->dirx * spritey); + transformy = invdet * (-vl->planey * spritex + vl->planex * spritey); + spritescreenx = (vl->w / 2) * (1 + transformx / transformy); + vmovescreen = vl->vaim + vl->jump / transformy; + spriteheight = fabs((vl->h / transformy)); + drawstarty = -spriteheight / 2 + vl->h / 2 + vmovescreen; + if (drawstarty < 0) + drawstarty = 0; + drawendy = spriteheight / 2 + vl->h / 2 + vmovescreen; + if (drawendy >= vl->h) + drawendy = vl->h - 1; + spritewidth = abs((int)(vl->h / transformy)); + drawstartx = -spritewidth / 2 + spritescreenx; + if (drawstartx < 0) + drawstartx = 0; + drawendx = spritewidth / 2 + spritescreenx; + if (drawendx >= vl->w) + drawendx = vl->w - 1; + x = drawstartx; + while (x < drawendx) + { + texx = (int)(256 * (x - (-spritewidth / 2 + spritescreenx)) * 64 / spritewidth) / 256; + if (transformy > 0 && x > 0 && x < vl->w && transformy < vl->distance[x] + 0.7) + { + y = drawstarty; + while (y < drawendy) + { + d = (y - vmovescreen) * 256 - vl->h * 128 + spriteheight * 128; + texy = (d * 64) / spriteheight / 256; + uint8_t *texel; + if (texy < 0) + texy = 0; + if (texy > 64) + texy = 64; + if (texx < 0) + texx = 0; + if (texx > 64) + texx = 64; + texel = &vl->curtext->pixels[(vl->curtext->width * texy + texx) * 4]; + color = texel[0] << 24 | texel[1] << 16 | texel[2] << 8 | texel[3]; + if (color != 0x980088FF) + mlx_put_pixel(vl->img, x, y, color); + y++; + } + } + x++; + } + i++; + } +}