little cleanup

This commit is contained in:
djonker 2023-11-05 06:48:52 +01:00
parent e15684fd69
commit 2ea2c8b89a
4 changed files with 190 additions and 177 deletions

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* 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);

View File

@ -6,138 +6,12 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* 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++;
}
}

View File

@ -6,7 +6,7 @@
/* By: djonker <codam@houtworm.net> +#+ */
/* +#+ */
/* 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++;
}
}

139
src/sprite/draw.c Normal file
View File

@ -0,0 +1,139 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* draw.c :+: :+: */
/* +:+ */
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* 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++;
}
}