sprites render

This commit is contained in:
djonker 2023-11-03 22:51:26 +01:00
parent 495ba620af
commit 64d1ff7ee9
8 changed files with 145 additions and 12 deletions

View File

@ -6,13 +6,13 @@
# By: houtworm <codam@houtworm.net> +#+ #
# +#+ #
# Created: 2023/10/26 10:46:29 by houtworm #+# #+# #
# Updated: 2023/11/02 04:04:36 by houtworm ######## odam.nl #
# Updated: 2023/11/02 18:31:43 by houtworm ######## odam.nl #
# #
# **************************************************************************** #
NAME =cub3d
CC =gcc
FC =-Wall -Werror -Wextra -Wunreachable-code -Ofast -g #-fsanitize=address
FC =-Wall -Werror -Wextra -Wunreachable-code -flto -Ofast -march=native #-g #-fsanitize=address
HEAD =-I ./include -I $(MLX)/include
RM =rm -rf
LIB =libft/libft.a getnextline/get_next_line.a mlx/build/libmlx42.a -ldl -lglfw -pthread -lm

View File

@ -19,6 +19,11 @@ Cub3D is a simple raycasting game using the mlx library
- Moving Enemies?
- Weapon Sprite that fires?
- Zoom with right mouse button?
- Weapon pickups
- HP system
- Timer
- Score system
- End level screen
- Skybox?
- Sounds?
- Music?
@ -46,6 +51,8 @@ Cub3D is a simple raycasting game using the mlx library
- Player can Run
- Map sizes up to 1 Megabyte (1000x1000)
- Vertical Aiming
- Fullscreen
---
## Bugs

14
cub3d.h
View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 10:46:35 by houtworm #+# #+# */
/* Updated: 2023/11/02 04:18:39 by houtworm ######## odam.nl */
/* Updated: 2023/11/03 22:43:10 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -21,6 +21,13 @@
# include "mlx/include/MLX42/MLX42.h"
# include <stdio.h>
typedef struct s_sprite
{
int x;
int y;
int type;
} t_sprite;
typedef struct s_varlist
{
mlx_t *mlx;
@ -32,6 +39,10 @@ typedef struct s_varlist
mlx_texture_t *southtext;
mlx_texture_t *westtext;
mlx_texture_t *barreltext;
mlx_texture_t *hlamptext;
mlx_texture_t *slamptext;
t_sprite *sprite;
int spritecount;
int fpsrefresh;
int w;
int h;
@ -87,6 +98,7 @@ void cursorhook(double x, double y, void *param);
void ft_raycast(t_varlist *vl);
// draw.c
void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend);
void ft_drawsprites(t_varlist *vl);
// error.c
int ft_errorexit(char *reason, char *function, int code);
#endif

View File

@ -6,12 +6,112 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */
/* Updated: 2023/11/01 14:39:54 by houtworm ######## odam.nl */
/* Updated: 2023/11/03 22:45:06 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->hlamptext;
else if (type == 3)
sprite = vl->hlamptext;
else if (type == 4)
sprite = vl->slamptext;
else
ft_errorexit("what?", "what?", 1);
return (sprite);
}
void ft_drawsprites(t_varlist *vl)
{
int i;
double spritex;
double spritey;
mlx_texture_t *sprite;
double invdet;
double transformx;
double transformy;
int udiv;
int vdiv;
double vmove;
int spritescreenx;
int vmovescreen;
int spriteheight;
int spritewidth;
int drawstarty;
int drawstartx;
int drawendy;
int drawendx;
int texx;
int texy;
int x;
int y;
int d;
uint32_t color;
i = 0;
while (vl->spritecount > i)
{
spritex = vl->sprite[i].x - vl->posx;
spritey = vl->sprite[i].y - vl->posy;
sprite = 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) * (int)(1 + transformx / transformy);
udiv = 1;
vdiv = 1;
vmove = 0.0;
vmovescreen = (int)(vmove / transformy) + vl->hoffset + 0 / transformy;
spriteheight = abs((int)(vl->h / (transformy))) / vdiv;
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)) / udiv;
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)
{
y = drawstarty;
while (y < drawendy)
{
d = (y - vmovescreen) * 256 - vl->h * 128 + spriteheight * 128;
texy = (d * 64) / spriteheight / 256;
uint8_t *texel;
texel = &sprite->pixels[(sprite->width * texy + texx) * sprite->bytes_per_pixel];
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)

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 16:49:12 by houtworm #+# #+# */
/* Updated: 2023/11/02 04:28:53 by houtworm ######## odam.nl */
/* Updated: 2023/11/03 22:42:48 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -18,6 +18,8 @@ t_varlist initvarlist(void)
vl.w = 800;
vl.h = 600;
vl.sprite = ft_calloc(4096, 8);
vl.spritecount = 0;
vl.hoffset = 0;
vl.ccolor = 0;
vl.fcolor = 0;
@ -26,6 +28,8 @@ t_varlist initvarlist(void)
vl.southtext = NULL;
vl.westtext = NULL;
vl.barreltext = mlx_load_png("./assets/barrel.png");
vl.hlamptext = mlx_load_png("./assets/hlamp.png");
vl.slamptext = mlx_load_png("./assets/slamp.png");
vl.mlx = mlx_init(vl.w, vl.h, "Cub3D", true);
if (!vl.mlx)
ft_errorexit("MLX failed to init", "initvarlist", 1);

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */
/* Updated: 2023/11/02 03:57:40 by houtworm ######## odam.nl */
/* Updated: 2023/11/03 22:41:58 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -36,6 +36,7 @@ void mainloop(void *param)
mlx_delete_image(vl->mlx, vl->fps);
vl->img = mlx_new_image(vl->mlx, vl->w, vl->h);
ft_raycast(vl);
ft_drawsprites(vl);
ft_frametime(vl);
ft_movementkeys(vl);
if (!vl->img || (mlx_image_to_window(vl->mlx, vl->img, 0, 0) < 0))

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 17:33:50 by houtworm #+# #+# */
/* Updated: 2023/11/02 03:56:15 by houtworm ######## odam.nl */
/* Updated: 2023/11/03 21:54:07 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -48,6 +48,15 @@ char ft_setplayerpos(t_varlist *vl, char dir, int y, int x)
return ('0');
}
char ft_addsprite(t_varlist *vl, int x, int y, int type)
{
vl->sprite[vl->spritecount].x = x;
vl->sprite[vl->spritecount].y = y;
vl->sprite[vl->spritecount].type = type;
vl->spritecount++;
return ('0');
}
char **ft_getmap(t_varlist *vl, int fd)
{
int y;
@ -91,13 +100,13 @@ char **ft_getmap(t_varlist *vl, int fd)
else if (line[x] == 'D')
map[y][x] = 'D';
else if (line[x] == 'C')
map[y][x] = 'C';
map[y][x] = ft_addsprite(vl, x, y, 2);
else if (line[x] == 'B')
map[y][x] = 'B';
map[y][x] = ft_addsprite(vl, x, y, 1);
else if (line[x] == 'X')
map[y][x] = 'X';
map[y][x] = ft_addsprite(vl, x, y, 3);
else if (line[x] == 'K')
map[y][x] = 'K';
map[y][x] = ft_addsprite(vl, x, y, 4);
else
{
ft_putstr("Invalid character on the map\n Valid options are\n");

View File

@ -6,7 +6,7 @@
/* By: djonker <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/27 14:36:42 by djonker #+# #+# */
/* Updated: 2023/10/31 15:30:06 by houtworm ######## odam.nl */
/* Updated: 2023/11/03 22:08:37 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */