animations!
This commit is contained in:
parent
caca8f69ba
commit
ba2f5a05d8
1
.normignore
Normal file
1
.normignore
Normal file
@ -0,0 +1 @@
|
||||
lib/MLX42/
|
||||
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -8,7 +8,8 @@
|
||||
"vec_math.h": "c",
|
||||
"texture.h": "c",
|
||||
"libft.h": "c",
|
||||
"render.h": "c"
|
||||
"render.h": "c",
|
||||
"math.h": "c"
|
||||
},
|
||||
"cmake.ignoreCMakeListsMissing": true
|
||||
}
|
||||
2
Makefile
2
Makefile
@ -6,7 +6,7 @@
|
||||
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||
# +#+ #
|
||||
# Created: 2024/10/15 11:48:46 by whaffman #+# #+# #
|
||||
# Updated: 2025/05/14 16:06:31 by whaffman ######## odam.nl #
|
||||
# Updated: 2025/05/15 17:44:48 by whaffman ######## odam.nl #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
|
||||
BIN
assets/plant.png
Normal file
BIN
assets/plant.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 9.2 KiB |
@ -6,7 +6,7 @@
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/14 12:42:27 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/16 15:01:40 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -20,6 +20,10 @@
|
||||
# define HEIGHT 1080
|
||||
# define TITLE "Cub3D"
|
||||
|
||||
# ifndef M_PI
|
||||
# define M_PI 3.14159265358979323846
|
||||
# endif
|
||||
|
||||
# define RESET "\033[0m"
|
||||
# define BLACK "\033[0;30m"
|
||||
# define RED "\033[0;31m"
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/15 18:53:27 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/14 20:08:48 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/15 13:28:11 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -19,5 +19,4 @@ int player_create(t_game **game);
|
||||
void player_render(t_screen *screen, t_player *player);
|
||||
void player_update(t_game *game, double delta_time);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/15 16:28:16 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/14 21:24:48 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/15 14:25:46 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -36,8 +36,7 @@ void sort_sprites(t_game *game);
|
||||
void calculate_sprite_dist(t_game *game);
|
||||
void cam_fraction(t_game *game, t_sprite *sprite);
|
||||
unsigned int calculate_alpha(int x, int y, double dist);
|
||||
unsigned int sample_texture_color(mlx_texture_t *texture,
|
||||
t_vec2_int tex, int alpha);
|
||||
|
||||
unsigned int sample_texture_color(t_sprite *sprite,
|
||||
t_vec2_int tex, int n);
|
||||
|
||||
#endif
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/15 13:14:53 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/16 14:59:09 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -62,7 +62,7 @@ typedef struct s_sprite
|
||||
int n_frames;
|
||||
double dist;
|
||||
double cam_frac;
|
||||
double alpha;
|
||||
int alpha;
|
||||
mlx_texture_t *texture;
|
||||
t_vec2 pos;
|
||||
} t_sprite;
|
||||
@ -130,6 +130,8 @@ typedef struct s_game
|
||||
t_player *player;
|
||||
t_screen *screen;
|
||||
t_keyboard *keyboard;
|
||||
int framecount;
|
||||
int fps;
|
||||
} t_game;
|
||||
|
||||
#endif
|
||||
|
||||
18
src/game.c
18
src/game.c
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/14 19:59:05 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/16 15:01:28 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -17,10 +17,8 @@ int game_create(t_game **game)
|
||||
*game = malloc(sizeof(t_game));
|
||||
if (!game)
|
||||
return (FAILURE);
|
||||
(*game)->player = NULL;
|
||||
(*game)->map = NULL;
|
||||
(*game)->screen = NULL;
|
||||
(*game)->keyboard = NULL;
|
||||
memset(*game, 0, sizeof(t_game));
|
||||
(*game)->fps = 20;
|
||||
return (SUCCESS);
|
||||
}
|
||||
|
||||
@ -42,15 +40,15 @@ void game_loop(void *param)
|
||||
{
|
||||
t_game *game;
|
||||
double delta_time;
|
||||
static int framecount = 0;
|
||||
static int fps = 0;
|
||||
|
||||
framecount++;
|
||||
game = (t_game *)param;
|
||||
game->framecount++;
|
||||
delta_time = game->screen->mlx->delta_time;
|
||||
fps += (int)(1.f / delta_time);
|
||||
if (framecount % 20 == 0)
|
||||
if (game->framecount % 20 == 0)
|
||||
{
|
||||
game->fps = (int)(fps / 20);
|
||||
fprintf(stderr, "FPS: %d\n", fps / 20);
|
||||
fps = 0;
|
||||
}
|
||||
@ -61,8 +59,8 @@ void game_loop(void *param)
|
||||
keyboard_update(game);
|
||||
if (game->player->is_moving)
|
||||
{
|
||||
game->screen->img->instances[0].x = sin((double)framecount / 4.0) * 20;
|
||||
game->screen->img->instances[0].y = cos((double)framecount / 2.0) * 10;
|
||||
game->screen->img->instances[0].x = sin(game->framecount / 4.0) * 20;
|
||||
game->screen->img->instances[0].y = cos(game->framecount / 2.0) * 10;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/05/12 11:31:34 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/12 11:31:43 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/16 13:35:05 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/05/08 12:23:17 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/14 21:21:55 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/15 17:51:57 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -19,10 +19,11 @@ static void get_start_end(t_game *game,
|
||||
|
||||
sprite_scale = 16.0 / sprite->dist;
|
||||
start->x = 0.5 * (game->screen->width * (1.0 + sprite->cam_frac)
|
||||
- sprite->texture->width * sprite_scale);
|
||||
- sprite->texture->width / sprite->n_frames * sprite_scale);
|
||||
start->y = 0.5 * (game->screen->height
|
||||
- sprite->texture->height * sprite_scale);
|
||||
end->x = start->x + (sprite->texture->width * sprite_scale);
|
||||
end->x = start->x;
|
||||
end->x += (sprite->texture->width / sprite->n_frames * sprite_scale);
|
||||
end->y = start->y + (sprite->texture->height * sprite_scale);
|
||||
}
|
||||
|
||||
@ -42,21 +43,23 @@ static void draw_sprite_column(
|
||||
{
|
||||
t_vec2_int tex;
|
||||
t_vec2 inv_range;
|
||||
int alpha;
|
||||
int y;
|
||||
unsigned int color;
|
||||
|
||||
inv_range.y = 1.0 / (column.end.y - column.start.y);
|
||||
inv_range.x = 1.0 / (column.end.x - column.start.x);
|
||||
alpha = calculate_alpha(column.start.x, column.start.y, sprite->dist);
|
||||
tex.x = (column.x - column.start.x) * sprite->texture->width;
|
||||
sprite->alpha = calculate_alpha(column.start.x,
|
||||
column.start.y, sprite->dist);
|
||||
tex.x = (column.x - column.start.x);
|
||||
tex.x *= sprite->texture->width / sprite->n_frames;
|
||||
tex.x *= inv_range.x;
|
||||
y = column.start.y;
|
||||
while (y < column.end.y)
|
||||
{
|
||||
tex.y = (y - column.start.y) * sprite->texture->height;
|
||||
tex.y *= inv_range.y;
|
||||
color = sample_texture_color(sprite->texture, tex, alpha);
|
||||
color = sample_texture_color(sprite, tex,
|
||||
(int)(game->framecount / 20) % sprite->n_frames);
|
||||
if (y > 0 && y < game->screen->height && (color & 0xFF) != 0)
|
||||
mlx_put_pixel(game->screen->img, column.x, y, color);
|
||||
y++;
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/05/14 21:21:57 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/14 21:25:24 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/15 15:10:39 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -79,16 +79,25 @@ unsigned int calculate_alpha(int x, int y, double dist)
|
||||
return (alpha);
|
||||
}
|
||||
|
||||
unsigned int sample_texture_color(
|
||||
mlx_texture_t *texture,
|
||||
t_vec2_int tex, int alpha
|
||||
unsigned int
|
||||
sample_texture_color(
|
||||
t_sprite *sprite,
|
||||
t_vec2_int tex,
|
||||
int n
|
||||
)
|
||||
{
|
||||
mlx_texture_t *texture;
|
||||
int index;
|
||||
int width;
|
||||
int n_frames;
|
||||
|
||||
index = (tex.x + tex.y * texture->width) * texture->bytes_per_pixel;
|
||||
n_frames = sprite->n_frames;
|
||||
width = sprite->texture->width / n_frames;
|
||||
texture = sprite->texture;
|
||||
index = (tex.x + n * width + tex.y * width * n_frames);
|
||||
index *= texture->bytes_per_pixel;
|
||||
return (texture->pixels[index] << 24
|
||||
| texture->pixels[index + 1] << 16
|
||||
| texture->pixels[index + 2] << 8
|
||||
| (texture->pixels[index + 3] != 0) * alpha);
|
||||
| (texture->pixels[index + 3] != 0) * sprite->alpha);
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/22 17:08:26 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/14 20:33:11 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/15 17:09:38 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -35,12 +35,12 @@ static int init_temp(t_game **game)
|
||||
return (FAILURE);
|
||||
sprites = (*game)->map->sprites;
|
||||
sprites[0] = make_sprite("./assets/battery.png", 3.5, 3.5, 1);
|
||||
sprites[1] = make_sprite("./assets/battery.png", 2.5, 3.5, 1);
|
||||
sprites[2] = make_sprite("./assets/battery.png", 1.5, 3.5, 1);
|
||||
sprites[1] = make_sprite("./assets/plant.png", 2.5, 3.5, 4);
|
||||
sprites[2] = make_sprite("./assets/plant.png", 1.5, 3.5, 4);
|
||||
sprites[3] = make_sprite("./assets/broken_mirror.png", 3.5, 4.5, 1);
|
||||
sprites[4] = make_sprite("./assets/lamp.png", 2.5, 5.5, 1);
|
||||
sprites[5] = make_sprite("./assets/battery.png", 10.5, 6.5, 1);
|
||||
sprites[6] = make_sprite("./assets/battery.png", 14.5, 3.5, 1);
|
||||
sprites[6] = make_sprite("./assets/plant.png", 14.5, 3.5, 4);
|
||||
sprites[7] = make_sprite("./assets/battery.png", 36.5, 3.5, 1);
|
||||
sprites[8] = make_sprite("./assets/broken_mirror.png", 42.5, 4.5, 1);
|
||||
sprites[9] = make_sprite("./assets/lamp.png", 9.5, 10.5, 1);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user