From b3a3e1806393ad38bb5213f06be8be311372e11a Mon Sep 17 00:00:00 2001 From: djonker Date: Mon, 6 Nov 2023 00:08:25 +0100 Subject: [PATCH] weapons are animated and fire --- Makefile | 3 +- assets/weapon/ggun/{ggun1.png => 1.png} | Bin assets/weapon/ggun/{ggun2.png => 2.png} | Bin assets/weapon/ggun/{ggun3.png => 3.png} | Bin assets/weapon/ggun/{ggun4.png => 4.png} | Bin assets/weapon/ggun/{ggun5.png => 5.png} | Bin assets/weapon/knife/{knife1.png => 1.png} | Bin assets/weapon/knife/{knife2.png => 2.png} | Bin assets/weapon/knife/{knife3.png => 3.png} | Bin assets/weapon/knife/{knife4.png => 4.png} | Bin assets/weapon/knife/{knife5.png => 5.png} | Bin assets/weapon/mgun/{mgun1.png => 1.png} | Bin assets/weapon/mgun/{mgun2.png => 2.png} | Bin assets/weapon/mgun/{mgun3.png => 3.png} | Bin assets/weapon/mgun/{mgun4.png => 4.png} | Bin assets/weapon/mgun/{mgun5.png => 5.png} | Bin assets/weapon/pistol/{gun1.png => 1.png} | Bin assets/weapon/pistol/{gun2.png => 2.png} | Bin assets/weapon/pistol/{gun3.png => 3.png} | Bin assets/weapon/pistol/{gun4.png => 4.png} | Bin assets/weapon/pistol/{gun5.png => 5.png} | Bin cub3d.h | 11 +- src/draw/texture.c | 6 +- src/draw/weapon.c | 131 ++++++++++++++++++++++ src/draw/world.c | 4 +- src/input/rest.c | 31 ++++- src/main/init.c | 33 +++++- src/main/main.c | 15 +-- 28 files changed, 213 insertions(+), 21 deletions(-) rename assets/weapon/ggun/{ggun1.png => 1.png} (100%) rename assets/weapon/ggun/{ggun2.png => 2.png} (100%) rename assets/weapon/ggun/{ggun3.png => 3.png} (100%) rename assets/weapon/ggun/{ggun4.png => 4.png} (100%) rename assets/weapon/ggun/{ggun5.png => 5.png} (100%) rename assets/weapon/knife/{knife1.png => 1.png} (100%) rename assets/weapon/knife/{knife2.png => 2.png} (100%) rename assets/weapon/knife/{knife3.png => 3.png} (100%) rename assets/weapon/knife/{knife4.png => 4.png} (100%) rename assets/weapon/knife/{knife5.png => 5.png} (100%) rename assets/weapon/mgun/{mgun1.png => 1.png} (100%) rename assets/weapon/mgun/{mgun2.png => 2.png} (100%) rename assets/weapon/mgun/{mgun3.png => 3.png} (100%) rename assets/weapon/mgun/{mgun4.png => 4.png} (100%) rename assets/weapon/mgun/{mgun5.png => 5.png} (100%) rename assets/weapon/pistol/{gun1.png => 1.png} (100%) rename assets/weapon/pistol/{gun2.png => 2.png} (100%) rename assets/weapon/pistol/{gun3.png => 3.png} (100%) rename assets/weapon/pistol/{gun4.png => 4.png} (100%) rename assets/weapon/pistol/{gun5.png => 5.png} (100%) create mode 100644 src/draw/weapon.c diff --git a/Makefile b/Makefile index 09ab868..5884e04 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: houtworm +#+ # # +#+ # # Created: 2023/10/26 10:46:29 by houtworm #+# #+# # -# Updated: 2023/11/05 11:49:30 by houtworm ######## odam.nl # +# Updated: 2023/11/05 22:29:47 by houtworm ######## odam.nl # # # # **************************************************************************** # @@ -27,6 +27,7 @@ SRC =src/main/main.c\ src/draw/raycast.c\ src/draw/world.c\ src/draw/texture.c\ + src/draw/weapon.c\ src/draw/stats.c\ src/input/game.c\ src/input/move.c\ diff --git a/assets/weapon/ggun/ggun1.png b/assets/weapon/ggun/1.png similarity index 100% rename from assets/weapon/ggun/ggun1.png rename to assets/weapon/ggun/1.png diff --git a/assets/weapon/ggun/ggun2.png b/assets/weapon/ggun/2.png similarity index 100% rename from assets/weapon/ggun/ggun2.png rename to assets/weapon/ggun/2.png diff --git a/assets/weapon/ggun/ggun3.png b/assets/weapon/ggun/3.png similarity index 100% rename from assets/weapon/ggun/ggun3.png rename to assets/weapon/ggun/3.png diff --git a/assets/weapon/ggun/ggun4.png b/assets/weapon/ggun/4.png similarity index 100% rename from assets/weapon/ggun/ggun4.png rename to assets/weapon/ggun/4.png diff --git a/assets/weapon/ggun/ggun5.png b/assets/weapon/ggun/5.png similarity index 100% rename from assets/weapon/ggun/ggun5.png rename to assets/weapon/ggun/5.png diff --git a/assets/weapon/knife/knife1.png b/assets/weapon/knife/1.png similarity index 100% rename from assets/weapon/knife/knife1.png rename to assets/weapon/knife/1.png diff --git a/assets/weapon/knife/knife2.png b/assets/weapon/knife/2.png similarity index 100% rename from assets/weapon/knife/knife2.png rename to assets/weapon/knife/2.png diff --git a/assets/weapon/knife/knife3.png b/assets/weapon/knife/3.png similarity index 100% rename from assets/weapon/knife/knife3.png rename to assets/weapon/knife/3.png diff --git a/assets/weapon/knife/knife4.png b/assets/weapon/knife/4.png similarity index 100% rename from assets/weapon/knife/knife4.png rename to assets/weapon/knife/4.png diff --git a/assets/weapon/knife/knife5.png b/assets/weapon/knife/5.png similarity index 100% rename from assets/weapon/knife/knife5.png rename to assets/weapon/knife/5.png diff --git a/assets/weapon/mgun/mgun1.png b/assets/weapon/mgun/1.png similarity index 100% rename from assets/weapon/mgun/mgun1.png rename to assets/weapon/mgun/1.png diff --git a/assets/weapon/mgun/mgun2.png b/assets/weapon/mgun/2.png similarity index 100% rename from assets/weapon/mgun/mgun2.png rename to assets/weapon/mgun/2.png diff --git a/assets/weapon/mgun/mgun3.png b/assets/weapon/mgun/3.png similarity index 100% rename from assets/weapon/mgun/mgun3.png rename to assets/weapon/mgun/3.png diff --git a/assets/weapon/mgun/mgun4.png b/assets/weapon/mgun/4.png similarity index 100% rename from assets/weapon/mgun/mgun4.png rename to assets/weapon/mgun/4.png diff --git a/assets/weapon/mgun/mgun5.png b/assets/weapon/mgun/5.png similarity index 100% rename from assets/weapon/mgun/mgun5.png rename to assets/weapon/mgun/5.png diff --git a/assets/weapon/pistol/gun1.png b/assets/weapon/pistol/1.png similarity index 100% rename from assets/weapon/pistol/gun1.png rename to assets/weapon/pistol/1.png diff --git a/assets/weapon/pistol/gun2.png b/assets/weapon/pistol/2.png similarity index 100% rename from assets/weapon/pistol/gun2.png rename to assets/weapon/pistol/2.png diff --git a/assets/weapon/pistol/gun3.png b/assets/weapon/pistol/3.png similarity index 100% rename from assets/weapon/pistol/gun3.png rename to assets/weapon/pistol/3.png diff --git a/assets/weapon/pistol/gun4.png b/assets/weapon/pistol/4.png similarity index 100% rename from assets/weapon/pistol/gun4.png rename to assets/weapon/pistol/4.png diff --git a/assets/weapon/pistol/gun5.png b/assets/weapon/pistol/5.png similarity index 100% rename from assets/weapon/pistol/gun5.png rename to assets/weapon/pistol/5.png diff --git a/cub3d.h b/cub3d.h index bdb502f..709215d 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 21:40:56 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 23:27:12 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -53,11 +53,16 @@ typedef struct s_varlist mlx_image_t *fstat; mlx_image_t *tstat; mlx_image_t *kstat; + mlx_image_t *wimg; mlx_texture_t *temptext; mlx_texture_t **walltext; mlx_texture_t **decotext; mlx_texture_t **picktext; mlx_texture_t **nazitext; + mlx_texture_t **gun0text; + mlx_texture_t **gun1text; + mlx_texture_t **gun2text; + mlx_texture_t **gun3text; t_sprite *sprite; int spritecount; int w; @@ -95,6 +100,7 @@ typedef struct s_varlist int ggun; int ammo; int weapon; + int reload; } t_varlist; // MAIN @@ -122,8 +128,9 @@ void cursorhook(double x, double y, void *param); // DRAW void ft_raycast(t_varlist *vl, int x, int mapx, int mapy); void ft_drawmap(t_varlist *vl); +void ft_drawweapon(t_varlist *vl); int ft_gettextx(t_varlist *vl); -uint32_t ft_gettextcolor(t_varlist *vl, int texty, int textx); +uint32_t ft_gettextcolor(mlx_texture_t *texture, int texty, int textx); // SPRITE void ft_drawsprite(t_varlist *vl, t_draw *draw, int x); t_draw *ft_initdrawsprite(t_varlist *vl, int i); diff --git a/src/draw/texture.c b/src/draw/texture.c index 009bc3a..5bad97f 100644 --- a/src/draw/texture.c +++ b/src/draw/texture.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */ -/* Updated: 2023/11/05 08:17:39 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 23:26:38 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -49,12 +49,12 @@ int ft_gettextx(t_varlist *vl) return (textx); } -uint32_t ft_gettextcolor(t_varlist *vl, int texty, int textx) +uint32_t ft_gettextcolor(mlx_texture_t *texture, int texty, int textx) { uint8_t *texel; uint32_t color; - texel = &vl->temptext->pixels[(vl->temptext->width * texty + textx) * 4]; + texel = &texture->pixels[(texture->width * texty + textx) * 4]; color = texel[0] << 24 | texel[1] << 16 | texel[2] << 8 | texel[3]; return (color); } diff --git a/src/draw/weapon.c b/src/draw/weapon.c new file mode 100644 index 0000000..63583dc --- /dev/null +++ b/src/draw/weapon.c @@ -0,0 +1,131 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* weapon.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ +/* Updated: 2023/11/05 23:44:25 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +void ft_drawknife(t_varlist *vl) +{ + int x; + int y; + int i; + uint32_t color; + + x = 0; + y = 0; + i = 1; + while (i < 4096) + { + color = ft_gettextcolor(vl->gun0text[vl->reload], y, x); + if (color != 0x980088FF) + mlx_put_pixel(vl->wimg, x, y, color); + x++; + if (i % 64 == 0) + { + x = 0; + y++; + } + i++; + } +} + +void ft_drawpistol(t_varlist *vl) +{ + int x; + int y; + int i; + uint32_t color; + + x = 0; + y = 0; + i = 1; + while (i < 4096) + { + color = ft_gettextcolor(vl->gun1text[vl->reload], y, x); + if (color != 0x980088FF) + mlx_put_pixel(vl->wimg, x, y, color); + x++; + if (i % 64 == 0) + { + x = 0; + y++; + } + i++; + } +} + +void ft_drawmgun(t_varlist *vl) +{ + int x; + int y; + int i; + uint32_t color; + + x = 0; + y = 0; + i = 1; + while (i < 4096) + { + color = ft_gettextcolor(vl->gun2text[vl->reload], y, x); + if (color != 0x980088FF) + mlx_put_pixel(vl->wimg, x, y, color); + x++; + if (i % 64 == 0) + { + x = 0; + y++; + } + i++; + } +} + +void ft_drawggun(t_varlist *vl) +{ + int x; + int y; + int i; + uint32_t color; + + x = 0; + y = 0; + i = 1; + while (i < 4096) + { + color = ft_gettextcolor(vl->gun3text[vl->reload], y, x); + if (color != 0x980088FF) + mlx_put_pixel(vl->wimg, x, y, color); + x++; + if (i % 64 == 0) + { + x = 0; + y++; + } + i++; + } +} + +void ft_drawweapon(t_varlist *vl) +{ + int size; + + if (vl->weapon == 0) + ft_drawknife(vl); + if (vl->weapon == 1) + ft_drawpistol(vl); + if (vl->weapon == 2) + ft_drawmgun(vl); + if (vl->weapon == 3) + ft_drawggun(vl); + size = vl->h * 0.8; + mlx_resize_image(vl->wimg, size, size); + mlx_image_to_window(vl->mlx, vl->wimg, vl->w / 2 - size / 2, vl->h - size); + mlx_set_instance_depth(vl->wimg->instances, 5); +} diff --git a/src/draw/world.c b/src/draw/world.c index 9645343..4435bad 100644 --- a/src/draw/world.c +++ b/src/draw/world.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */ -/* Updated: 2023/11/05 07:32:02 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 23:27:55 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -40,7 +40,7 @@ int ft_drawwall(int x, t_varlist *vl, int drawstart, int drawend, int y) { texty = (int)textpos & (64 - 1); textpos += step; - color = ft_gettextcolor(vl, texty, textx); + color = ft_gettextcolor(vl->temptext, texty, textx); mlx_put_pixel(vl->img, x, y, color); y++; } diff --git a/src/input/rest.c b/src/input/rest.c index 5d2c907..a8626bb 100644 --- a/src/input/rest.c +++ b/src/input/rest.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:50:23 by houtworm #+# #+# */ -/* Updated: 2023/11/05 21:51:13 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 23:53:12 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -30,8 +30,35 @@ void ft_processacro(t_varlist *vl, double movespeed) void ft_processguns(t_varlist *vl) { - if (mlx_is_mouse_down(vl->mlx, MLX_MOUSE_BUTTON_LEFT)) + if (vl->reload == 4) + { + + vl->reload = 0; + ft_putendl("recovered"); + } + if (vl->reload == 3) + { + + vl->reload = 4; + ft_putendl("recover"); + } + if (vl->reload == 2) + { + + vl->reload = 3; ft_putendl("shoot"); + } + if (vl->reload == 1) + { + + vl->reload = 2; + ft_putendl("aim"); + } + if (mlx_is_mouse_down(vl->mlx, MLX_MOUSE_BUTTON_LEFT) && vl->reload == 0) + { + vl->reload = 1; + ft_putendl("ready"); + } if (mlx_is_mouse_down(vl->mlx, MLX_MOUSE_BUTTON_RIGHT)) ft_putendl("zoom"); } diff --git a/src/main/init.c b/src/main/init.c index d5bc213..0d849f8 100644 --- a/src/main/init.c +++ b/src/main/init.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:49:12 by houtworm #+# #+# */ -/* Updated: 2023/11/05 11:42:47 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 22:29:03 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -52,6 +52,34 @@ void ft_initenemies(t_varlist *vl) vl->nazitext[0] = mlx_load_png("./assets/nazi/guard/guard1.png"); } +void ft_initweapons(t_varlist *vl) +{ + vl->gun0text = ft_calloc(4096, 8); + vl->gun0text[0] = mlx_load_png("./assets/weapon/knife/1.png"); + vl->gun0text[1] = mlx_load_png("./assets/weapon/knife/2.png"); + vl->gun0text[2] = mlx_load_png("./assets/weapon/knife/3.png"); + vl->gun0text[3] = mlx_load_png("./assets/weapon/knife/4.png"); + vl->gun0text[4] = mlx_load_png("./assets/weapon/knife/5.png"); + vl->gun1text = ft_calloc(4096, 8); + vl->gun1text[0] = mlx_load_png("./assets/weapon/pistol/1.png"); + vl->gun1text[1] = mlx_load_png("./assets/weapon/pistol/2.png"); + vl->gun1text[2] = mlx_load_png("./assets/weapon/pistol/3.png"); + vl->gun1text[3] = mlx_load_png("./assets/weapon/pistol/4.png"); + vl->gun1text[4] = mlx_load_png("./assets/weapon/pistol/5.png"); + vl->gun2text = ft_calloc(4096, 8); + vl->gun2text[0] = mlx_load_png("./assets/weapon/mgun/1.png"); + vl->gun2text[1] = mlx_load_png("./assets/weapon/mgun/2.png"); + vl->gun2text[2] = mlx_load_png("./assets/weapon/mgun/3.png"); + vl->gun2text[3] = mlx_load_png("./assets/weapon/mgun/4.png"); + vl->gun2text[4] = mlx_load_png("./assets/weapon/mgun/5.png"); + vl->gun3text = ft_calloc(4096, 8); + vl->gun3text[0] = mlx_load_png("./assets/weapon/mgun/1.png"); + vl->gun3text[1] = mlx_load_png("./assets/weapon/mgun/2.png"); + vl->gun3text[2] = mlx_load_png("./assets/weapon/mgun/3.png"); + vl->gun3text[3] = mlx_load_png("./assets/weapon/mgun/4.png"); + vl->gun3text[4] = mlx_load_png("./assets/weapon/mgun/5.png"); +} + void ft_initmainstuff(t_varlist *vl) { vl->w = 800; @@ -69,6 +97,8 @@ void ft_initmainstuff(t_varlist *vl) vl->mgun = 0; vl->ggun = 0; vl->ammo = 10; + vl->weapon = 0; + vl->reload = 0; vl->ccolor = 0; vl->fcolor = 0; vl->walltext[0] = NULL; @@ -85,6 +115,7 @@ t_varlist initgame(void) ft_initsprites(&vl); ft_initpickups(&vl); ft_initenemies(&vl); + ft_initweapons(&vl); vl.mlx = mlx_init(vl.w, vl.h, "Cub3D", true); if (!vl.mlx) ft_errorexit("MLX failed to init", "initvarlist", 1); diff --git a/src/main/main.c b/src/main/main.c index 20298e9..c3fcaba 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ -/* Updated: 2023/11/05 21:50:28 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 23:41:31 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,7 +18,9 @@ void ft_replaceimage(t_varlist *vl) mlx_delete_image(vl->mlx, vl->fstat); mlx_delete_image(vl->mlx, vl->kstat); mlx_delete_image(vl->mlx, vl->tstat); + mlx_delete_image(vl->mlx, vl->wimg); vl->img = mlx_new_image(vl->mlx, vl->w, vl->h); + vl->wimg = mlx_new_image(vl->mlx, 64, 64); } void ft_printmap(t_varlist *vl) @@ -40,15 +42,8 @@ void ft_printmap(t_varlist *vl) printf("backward: x: %c, y: %c\n", vl->map[(int)(vl->posx - vl->dirx * movespeed)][(int)vl->posy], vl->map[(int)vl->posx][(int)(vl->posy - vl->diry * movespeed)]); printf("left: x: %c, y: %c\n", vl->map[(int)(vl->posx - vl->dirx * movespeed)][(int)vl->posy], vl->map[(int)vl->posx][(int)(vl->posy + vl->diry * movespeed)]); printf("right: x: %c, y: %c\n", vl->map[(int)(vl->posx + vl->dirx * movespeed)][(int)vl->posy], vl->map[(int)vl->posx][(int)(vl->posy - vl->diry * movespeed)]); - printf("weapon: %d\n", vl->weapon); } -/*void ft_drawweapon(t_varlist *vl)*/ -/*{*/ - - -/*}*/ - void mainloop(void *param) { t_varlist *vl; @@ -59,13 +54,13 @@ void mainloop(void *param) ft_checkpickup(vl); /*ft_enemyaction(vl);*/ ft_drawsprites(vl); - /*ft_drawweapon(vl);*/ + ft_drawweapon(vl); /*ft_drawminimap(vl);*/ vl->frametime = vl->mlx->delta_time; ft_printstats(vl); vl->frametime = vl->mlx->delta_time; ft_processinput(vl); - ft_printmap(vl); + /*ft_printmap(vl);*/ if (!vl->img || (mlx_image_to_window(vl->mlx, vl->img, 0, 0) < 0)) ft_errorexit("image to window failed ", "mainloop", 1); mlx_set_instance_depth(vl->img->instances, 1);