little cleanup

This commit is contained in:
djonker 2023-11-05 08:00:55 +01:00
parent bd890b5798
commit adc7a5bcb4
6 changed files with 163 additions and 99 deletions

24
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/05 07:24:54 by houtworm ######## odam.nl */
/* Updated: 2023/11/05 07:59:59 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -84,14 +84,15 @@ typedef struct s_varlist
int ggun;
} t_varlist;
void ft_frametime(t_varlist *vl);
// init.c
t_varlist initvarlist(void);
// parse.c
// MAIN
t_varlist initgame(void);
int ft_errorexit(char *reason, char *function, int code);
void ft_printstats(t_varlist *vl);
void ft_cleanup(t_varlist *vl);
// PARSE
t_varlist ft_parseconfigfile(t_varlist vl, char *filename);
// map.c
char **ft_getmap(t_varlist *vl, int fd);
// keys.c
// INPUT
void ft_processinput(t_varlist *vl);
void ft_processturn(t_varlist *vl, double rotspeed);
void ft_processmove(t_varlist *vl, double movespeed);
@ -101,13 +102,12 @@ void keyhook(mlx_key_data_t kd, void *param);
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
// DRAW
void ft_raycast(t_varlist *vl, int x, int mapx, int mapy);
// draw.c
void ft_drawmap(t_varlist *vl);
void ft_drawsprites(t_varlist *vl);
int ft_gettextx(t_varlist *vl);
uint32_t ft_gettextcolor(t_varlist *vl, int texty, int textx);
// error.c
int ft_errorexit(char *reason, char *function, int code);
// SPRITE
void ft_drawsprites(t_varlist *vl);
void ft_pickup(t_varlist *vl);
#endif

38
src/draw/stats.c Normal file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* stats.c :+: :+: */
/* +:+ */
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */
/* Updated: 2023/11/05 07:56:06 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
#include "../../cub3d.h"
void ft_printstats(t_varlist *vl)
{
char *total;
char *current;
char *temp;
current = ft_itoa(1 / vl->frametime);
temp = ft_strjoin(current, " FPS");
vl->fstat = mlx_put_string(vl->mlx, temp, 10, 10);
ft_vafree(2, temp, current);
total = ft_itoa(vl->enemies);
current = ft_itoa(vl->kills);
temp = ft_vastrjoin(4, "Kills: ", current, "/", total);
vl->tstat = mlx_put_string(vl->mlx, temp, 10, 30);
ft_vafree(3, temp, total, current);
total = ft_itoa(vl->tottreasure);
current = ft_itoa(vl->treasure);
temp = ft_vastrjoin(4, "Treasure: ", current, "/", total);
vl->kstat = mlx_put_string(vl->mlx, temp, 10, 50);
ft_vafree(3, temp, total, current);
mlx_set_instance_depth(vl->fstat->instances, 2);
mlx_set_instance_depth(vl->tstat->instances, 3);
mlx_set_instance_depth(vl->kstat->instances, 4);
}

23
src/main/cleanup.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* cleanup.c :+: :+: */
/* +:+ */
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */
/* Updated: 2023/11/05 07:56:34 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
#include "../../cub3d.h"
void ft_cleanup(t_varlist *vl)
{
mlx_delete_texture(vl->northtext);
mlx_delete_texture(vl->easttext);
mlx_delete_texture(vl->southtext);
mlx_delete_texture(vl->westtext);
mlx_delete_texture(vl->barreltext);
mlx_delete_image(vl->mlx, vl->img);
}

View File

@ -6,41 +6,61 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 16:49:12 by houtworm #+# #+# */
/* Updated: 2023/11/05 04:42:29 by houtworm ######## odam.nl */
/* Updated: 2023/11/05 07:51:11 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
#include "../../cub3d.h"
t_varlist initvarlist(void)
void ft_initsprites(t_varlist *vl)
{
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->endtext = mlx_load_png("./assets/bkey.png");
}
void ft_initpickups(t_varlist *vl)
{
vl->treasuretext = mlx_load_png("./assets/treasure3.png");
}
void ft_initenemies(t_varlist *vl)
{
vl->nazitext = mlx_load_png("./assets/guard1.png");
}
void ft_initmainstuff(t_varlist *vl)
{
vl->w = 800;
vl->h = 600;
vl->sprite = ft_calloc(4096, 8);
vl->distance = ft_calloc(4096, 8);
vl->spritecount = 0;
vl->vaim = 0;
vl->jump = 0;
vl->treasure = 0;
vl->tottreasure = 0;
vl->enemies = 0;
vl->kills = 0;
vl->mgun = 0;
vl->ggun = 0;
vl->ccolor = 0;
vl->fcolor = 0;
vl->northtext = NULL;
vl->easttext = NULL;
vl->southtext = NULL;
vl->westtext = NULL;
}
t_varlist initgame(void)
{
t_varlist vl;
vl.w = 800;
vl.h = 600;
vl.sprite = ft_calloc(4096, 8);
vl.distance = ft_calloc(4096, 8);
vl.spritecount = 0;
vl.vaim = 0;
vl.jump = 0;
vl.treasure = 0;
vl.tottreasure = 0;
vl.enemies = 0;
vl.kills = 0;
vl.mgun = 0;
vl.ggun = 0;
vl.ccolor = 0;
vl.fcolor = 0;
vl.northtext = NULL;
vl.easttext = NULL;
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.treasuretext = mlx_load_png("./assets/treasure3.png");
vl.endtext = mlx_load_png("./assets/bkey.png");
vl.nazitext = mlx_load_png("./assets/guard1.png");
ft_initmainstuff(&vl);
ft_initsprites(&vl);
ft_initpickups(&vl);
ft_initenemies(&vl);
vl.mlx = mlx_init(vl.w, vl.h, "Cub3D", true);
if (!vl.mlx)
ft_errorexit("MLX failed to init", "initvarlist", 1);

View File

@ -6,64 +6,12 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */
/* Updated: 2023/11/05 07:35:27 by houtworm ######## odam.nl */
/* Updated: 2023/11/05 07:55:52 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
#include "../../cub3d.h"
void ft_printstats(t_varlist *vl)
{
char *total;
char *current;
char *temp;
current = ft_itoa(1 / vl->frametime);
temp = ft_strjoin(current, " FPS");
vl->fstat = mlx_put_string(vl->mlx, temp, 10, 10);
ft_vafree(2, temp, current);
total = ft_itoa(vl->enemies);
current = ft_itoa(vl->kills);
temp = ft_vastrjoin(4, "Kills: ", current, "/", total);
vl->tstat = mlx_put_string(vl->mlx, temp, 10, 30);
ft_vafree(3, temp, total, current);
total = ft_itoa(vl->tottreasure);
current = ft_itoa(vl->treasure);
temp = ft_vastrjoin(4, "Treasure: ", current, "/", total);
vl->kstat = mlx_put_string(vl->mlx, temp, 10, 50);
ft_vafree(3, temp, total, current);
mlx_set_instance_depth(vl->fstat->instances, 2);
mlx_set_instance_depth(vl->tstat->instances, 3);
mlx_set_instance_depth(vl->kstat->instances, 4);
}
void ft_pickup(t_varlist *vl)
{
int i;
i = 0;
while (vl->sprite[i].x)
{
if ((int)vl->sprite[i].x == (int)vl->posx)
{
if ((int)vl->sprite[i].y == (int)vl->posy)
{
if (vl->sprite[i].type == 4)
{
while (vl->sprite[i].x)
{
vl->sprite[i] = vl->sprite[i + 1];
i++;
}
vl->spritecount--;
vl->treasure++;
}
}
}
i++;
}
}
void ft_replaceimage(t_varlist *vl)
{
mlx_delete_image(vl->mlx, vl->img);
@ -97,7 +45,7 @@ int main(int argc, char **argv)
if (argc > 2)
ft_errorexit("Too many arguments", "", 2);
vl = initvarlist();
vl = initgame();
if (argc == 2)
vl = ft_parseconfigfile(vl, argv[1]);
else
@ -111,12 +59,7 @@ int main(int argc, char **argv)
mlx_cursor_hook(vl.mlx, &cursorhook, &vl);
mlx_loop_hook(vl.mlx, &mainloop, &vl);
mlx_loop(vl.mlx);
mlx_delete_texture(vl.northtext);
mlx_delete_texture(vl.easttext);
mlx_delete_texture(vl.southtext);
mlx_delete_texture(vl.westtext);
mlx_delete_texture(vl.barreltext);
mlx_delete_image(vl.mlx, vl.img);
ft_cleanup(&vl);
mlx_terminate(vl.mlx);
exit (0);
}

40
src/sprite/pickup.c Normal file
View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* pickup.c :+: :+: */
/* +:+ */
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */
/* Updated: 2023/11/05 07:54:44 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
#include "../../cub3d.h"
void ft_pickup(t_varlist *vl)
{
int i;
i = 0;
while (vl->sprite[i].x)
{
if ((int)vl->sprite[i].x == (int)vl->posx)
{
if ((int)vl->sprite[i].y == (int)vl->posy)
{
if (vl->sprite[i].type == 4)
{
while (vl->sprite[i].x)
{
vl->sprite[i] = vl->sprite[i + 1];
i++;
}
vl->spritecount--;
vl->treasure++;
}
}
}
i++;
}
}