minimap first

This commit is contained in:
whaffman 2025-05-11 13:23:50 +02:00
parent 93e7932084
commit d4319f0789
11 changed files with 203 additions and 90 deletions

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* :::::::: */
/* cub3d.h :+: :+: :+: */ /* cub3d.h :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+ */
/* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */ /* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */
/* Updated: 2025/05/06 19:10:44 by qmennen ### ########.fr */ /* Updated: 2025/05/09 15:05:36 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,7 +31,7 @@
# define WHITE "\033[0;37m" # define WHITE "\033[0;37m"
# define NUM_KEYS 256 # define NUM_KEYS 256
# define TILE_SIZE 16 # define TILE_SIZE 8
# include "MLX42.h" # include "MLX42.h"
# include "allowed.h" # include "allowed.h"

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* :::::::: */
/* render.h :+: :+: :+: */ /* render.h :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+ */
/* Created: 2025/04/15 16:28:16 by qmennen #+# #+# */ /* Created: 2025/04/15 16:28:16 by qmennen #+# #+# */
/* Updated: 2025/05/08 14:50:38 by qmennen ### ########.fr */ /* Updated: 2025/05/09 14:34:27 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,7 +20,7 @@ void render_line(t_screen *screen, t_vec2 start, t_vec2 end, unsigned int color)
void render_circle(t_screen *screen, t_vec2 point, int radius, unsigned int color); void render_circle(t_screen *screen, t_vec2 point, int radius, unsigned int color);
void render_clear(t_screen *screen); void render_clear(t_screen *screen);
void render_entities(t_game *game); void render_entities(t_game *game);
void render_map(t_screen *screen, t_map *map); void render_map(t_game *game);
void cast_rays(t_game *game); void cast_rays(t_game *game);
void render_sprites(t_render* render, t_game *game); void render_sprites(t_render* render, t_game *game);

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* :::::::: */
/* types.h :+: :+: :+: */ /* types.h :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+ */
/* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */ /* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */
/* Updated: 2025/05/08 14:51:17 by qmennen ### ########.fr */ /* Updated: 2025/05/09 12:39:56 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -57,6 +57,7 @@ typedef struct s_sprite
int n_frames; int n_frames;
double dist; double dist;
double cam_frac; double cam_frac;
double alpha;
mlx_texture_t *texture; mlx_texture_t *texture;
t_vec2 pos; t_vec2 pos;
} t_sprite; } t_sprite;

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* :::::::: */
/* game.c :+: :+: :+: */ /* game.c :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+ */
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */ /* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
/* Updated: 2025/05/08 16:35:41 by qmennen ### ########.fr */ /* Updated: 2025/05/09 14:51:14 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -57,8 +57,8 @@ void game_loop(void *param)
render_clear(game->screen); render_clear(game->screen);
player_update(game, delta_time); player_update(game, delta_time);
cast_rays(game); cast_rays(game);
render_map(game->screen, game->map); render_map(game);
render_entities(game); //render_entities(game);
keyboard_update(game); keyboard_update(game);
if (game->player->is_moving) if (game->player->is_moving)
{ {

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* :::::::: */
/* DDAscratch.c :+: :+: :+: */ /* DDAscratch.c :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+ */
/* Created: 2025/05/02 11:58:09 by whaffman #+# #+# */ /* Created: 2025/05/02 11:58:09 by whaffman #+# #+# */
/* Updated: 2025/05/08 16:56:17 by qmennen ### ########.fr */ /* Updated: 2025/05/09 15:20:04 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/04/15 16:28:10 by qmennen #+# #+# */ /* Created: 2025/04/15 16:28:10 by qmennen #+# #+# */
/* Updated: 2025/05/07 11:38:34 by whaffman ######## odam.nl */ /* Updated: 2025/05/09 15:20:24 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -43,7 +43,7 @@ void render_tile(t_screen *screen, int x, int y, t_tile tile)
} }
} }
void render_map(t_screen *screen, t_map *map) void render_map2(t_screen *screen, t_map *map)
{ {
int i; int i;
int x; int x;

View File

@ -0,0 +1,49 @@
#include "cub3d.h"
#define MINIMAP_SIZE 200
void render_map(t_game *game)
{
t_vec2_int disp;
t_vec2 map;
disp.x = 0;
while (disp.x < MINIMAP_SIZE)
{
disp.y = 0;
while (disp.y < MINIMAP_SIZE)
{
if (disp.x == 0.5 * MINIMAP_SIZE || disp.y == 0.5 * MINIMAP_SIZE)
{
mlx_put_pixel(game->screen->minimap, disp.x, disp.y, 0x00ff00dd);
disp.y++;
continue ;
}
if ((disp.x - 0.5 * MINIMAP_SIZE) *(disp.x - 0.5 * MINIMAP_SIZE) + (disp.y - 0.5 * MINIMAP_SIZE) * (disp.y - 0.5 * MINIMAP_SIZE) > 0.25* MINIMAP_SIZE * MINIMAP_SIZE)
{
disp.y++;
continue ;
}
map.x = ((double)disp.x/TILE_SIZE) + game->player->pos.x - (0.5 * MINIMAP_SIZE/TILE_SIZE);
map.y = ((double)disp.y/TILE_SIZE) + game->player->pos.y - (0.5 * MINIMAP_SIZE/TILE_SIZE);
if (map.x < 0 || map.x >= game->map->width
|| map.y < 0 || map.y >= game->map->height)
{
mlx_put_pixel(game->screen->minimap, disp.x, disp.y, 0x00ff0044);
disp.y++;
continue ;
}
if (game->map->grid[(int)map.y][(int)map.x] == TILE_WALL)
mlx_put_pixel(game->screen->minimap, disp.x, disp.y, 0x00ff00ff);
else if (game->map->grid[(int)map.y][(int)map.x] == TILE_EMPTY
|| game->map->grid[(int)map.y][(int)map.x] == TILE_PLAYER)
mlx_put_pixel(game->screen->minimap, disp.x, disp.y, 0x00ff0077);
else
mlx_put_pixel(game->screen->minimap, disp.x, disp.y, 0x00ff0044);
disp.y++;
}
disp.x++;
}
}

View File

@ -6,39 +6,60 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/05/08 12:23:17 by qmennen #+# #+# */ /* Created: 2025/05/08 12:23:17 by qmennen #+# #+# */
/* Updated: 2025/05/08 23:18:06 by whaffman ######## odam.nl */ /* Updated: 2025/05/09 15:13:05 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "render.h" #include "render.h"
static void sort_sprites(t_game *game) static void sort_sprites(t_game *game)
{ {
t_sprite sprite_temp; int i;
int n; int j;
int i; t_sprite key;
int new_n;
n = game->map->n_sprites; i = 1;
while (n > 1) while (i < game->map->n_sprites)
{ {
i = 1; key = game->map->sprites[i];
new_n = 0; j = i - 1;
while (i <= n - 1) while (j >= 0 && game->map->sprites[j].dist < key.dist)
{ {
if (game->map->sprites[i - 1].dist < game->map->sprites[i].dist) game->map->sprites[j + 1] = game->map->sprites[j];
{ j--;
sprite_temp = game->map->sprites[i - 1];
game->map->sprites[i - 1] = game->map->sprites[i];
game->map->sprites[i] = sprite_temp;
new_n = i;
}
i++;
} }
n = new_n; game->map->sprites[j + 1] = key;
i++;
} }
} }
// static void sort_sprites(t_game *game)
// {
// t_sprite sprite_temp;
// int n;
// int i;
// int new_n;
// n = game->map->n_sprites;
// while (n > 1)
// {
// i = 1;
// new_n = 0;
// while (i <= n - 1)
// {
// if (game->map->sprites[i - 1].dist < game->map->sprites[i].dist)
// {
// sprite_temp = game->map->sprites[i - 1];
// game->map->sprites[i - 1] = game->map->sprites[i];
// game->map->sprites[i] = sprite_temp;
// new_n = i;
// }
// i++;
// }
// n = new_n;
// }
// }
static void calculate_sprite_dist(t_game *game) static void calculate_sprite_dist(t_game *game)
{ {
int i; int i;
@ -78,27 +99,41 @@ static void cam_fraction(t_game *game, t_sprite *sprite)
} }
return; return;
} }
unsigned int calculate_alpha(mlx_texture_t *texture, int x, int y, double dist)
unsigned int sample_texture_color(mlx_texture_t *texture, int x, int y, double dist)
{ {
int index; int index;
int alpha; int alpha;
index = (x + y * texture->width) * texture->bytes_per_pixel; index = (x + y * texture->width) * texture->bytes_per_pixel;
dist = (dist > 1) * dist + (dist <= 1) * 1; dist = (dist > 1) * dist + (dist <= 1) * 1;
alpha = (texture->pixels[index + 3] != 0) * (255.0 / dist); alpha = (255.0 / dist);
return texture->pixels[index] << 24 | texture->pixels[index + 1] << 16 | texture->pixels[index + 2] << 8 | alpha; return (alpha);
}
unsigned int sample_texture_color(mlx_texture_t *texture, int x, int y, int alpha)
{
int index;
index = (x + y * texture->width) * 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);
} }
void draw_sprite(t_game *game, t_sprite *sprite, t_render *render) void draw_sprite(t_game *game, t_sprite *sprite, t_render *render)
{ {
double sprite_scale; double sprite_scale;
double x_invrange;
double y_invrange;
int x_start; int x_start;
int y_start; int y_start;
int x_end; int x_end;
int y_end; int y_end;
int x; int x;
int y; int y;
int alpha;
double tex_x; double tex_x;
double tex_y; double tex_y;
unsigned int color; unsigned int color;
@ -110,17 +145,20 @@ void draw_sprite(t_game *game, t_sprite *sprite, t_render *render)
y_start = (game->screen->height - sprite->texture->height * sprite_scale )* 0.5; y_start = (game->screen->height - sprite->texture->height * sprite_scale )* 0.5;
x_end = x_start + (sprite->texture->width * sprite_scale); x_end = x_start + (sprite->texture->width * sprite_scale);
y_end = y_start + (sprite->texture->height * sprite_scale); y_end = y_start + (sprite->texture->height * sprite_scale);
y_invrange = 1.0 / (y_end - y_start);
x_invrange = 1.0 / (x_end - x_start);
x = x_start; x = x_start;
alpha = calculate_alpha(sprite->texture, x_start, y_start, sprite->dist);
while (x < x_end) while (x < x_end)
{ {
if (x > 0 && x < game->screen->width && sprite->dist <= render[x].perp_dist) if (x > 0 && x < game->screen->width && sprite->dist <= render[x].perp_dist)
{ {
y = y_start; y = y_start;
tex_x = ((double)((x - x_start) / (double)(x_end - x_start))) * sprite->texture->width; tex_x = (x - x_start) * x_invrange * sprite->texture->width;
while (y < y_end) while (y < y_end)
{ {
tex_y = ((double)((y- y_start) / (double)(y_end - y_start))) * sprite->texture->height; tex_y = (y- y_start) * y_invrange * sprite->texture->height;
color = sample_texture_color(sprite->texture, (int)(tex_x), (int)(tex_y), sprite->dist); color = sample_texture_color(sprite->texture, (int)(tex_x), (int)(tex_y), alpha);
if (y > 0 && y < game->screen->height && (color & 0xFF) != 0) if (y > 0 && y < game->screen->height && (color & 0xFF) != 0)
{ {
mlx_put_pixel(game->screen->img, x, y, color); mlx_put_pixel(game->screen->img, x, y, color);

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* :::::::: */
/* screen.c :+: :+: :+: */ /* screen.c :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+ */
/* Created: 2025/04/15 15:30:27 by qmennen #+# #+# */ /* Created: 2025/04/15 15:30:27 by qmennen #+# #+# */
/* Updated: 2025/05/08 17:10:50 by qmennen ### ########.fr */ /* Updated: 2025/05/09 15:27:37 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,7 +22,7 @@ int screen_create(t_game **game)
return (FAILURE); return (FAILURE);
screen->width = WIDTH; screen->width = WIDTH;
screen->height = HEIGHT; screen->height = HEIGHT;
mlx_set_setting(MLX_FULLSCREEN, 1); mlx_set_setting(MLX_FULLSCREEN, 0);
mlx = mlx_init(WIDTH, HEIGHT, TITLE, true); mlx = mlx_init(WIDTH, HEIGHT, TITLE, true);
if (!mlx) if (!mlx)
return (FAILURE); return (FAILURE);
@ -78,7 +78,7 @@ int screen_display(t_screen *screen)
printf(RED"Failed to display buffer image\n"RESET); printf(RED"Failed to display buffer image\n"RESET);
return (FAILURE); return (FAILURE);
} }
if (mlx_image_to_window(screen->mlx, screen->minimap, 0, 0) < 0) if (mlx_image_to_window(screen->mlx, screen->minimap, 200, 200) < 0)
{ {
printf(RED"Failed to display buffer image\n"RESET); printf(RED"Failed to display buffer image\n"RESET);
return (FAILURE); return (FAILURE);

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* :::::::: */
/* initialize.c :+: :+: :+: */ /* initialize.c :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+ */
/* Created: 2025/04/22 17:08:26 by qmennen #+# #+# */ /* Created: 2025/04/22 17:08:26 by qmennen #+# #+# */
/* Updated: 2025/05/08 17:28:50 by qmennen ### ########.fr */ /* Updated: 2025/05/09 13:07:25 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -42,7 +42,32 @@ static int init_temp(t_game **game)
(*game)->map->sprites[4].pos.y = 5.5; (*game)->map->sprites[4].pos.y = 5.5;
(*game)->map->sprites[4].dist = 0; (*game)->map->sprites[4].dist = 0;
(*game)->map->sprites[4].texture = mlx_load_png("./assets/lamp.png"); (*game)->map->sprites[4].texture = mlx_load_png("./assets/lamp.png");
(*game)->map->n_sprites = 5; (*game)->map->sprites[5].n_frames = 1;
(*game)->map->sprites[5].pos.x = 10.5;
(*game)->map->sprites[5].pos.y = 6.5;
(*game)->map->sprites[5].dist = 0;
(*game)->map->sprites[5].texture = mlx_load_png("./assets/battery.png");
(*game)->map->sprites[6].n_frames = 1;
(*game)->map->sprites[6].pos.x = 14.5;
(*game)->map->sprites[6].pos.y = 3.5;
(*game)->map->sprites[6].dist = 0;
(*game)->map->sprites[6].texture = mlx_load_png("./assets/battery.png");
(*game)->map->sprites[7].n_frames = 1;
(*game)->map->sprites[7].pos.x = 36.5;
(*game)->map->sprites[7].pos.y = 3.5;
(*game)->map->sprites[7].dist = 0;
(*game)->map->sprites[7].texture = mlx_load_png("./assets/battery.png");
(*game)->map->sprites[8].n_frames = 1;
(*game)->map->sprites[8].pos.x = 42.5;
(*game)->map->sprites[8].pos.y = 4.5;
(*game)->map->sprites[8].dist = 0;
(*game)->map->sprites[8].texture = mlx_load_png("./assets/broken_mirror.png");
(*game)->map->sprites[9].n_frames = 1;
(*game)->map->sprites[9].pos.x = 9.5;
(*game)->map->sprites[9].pos.y = 10.5;
(*game)->map->sprites[9].dist = 0;
(*game)->map->sprites[9].texture = mlx_load_png("./assets/lamp.png");
(*game)->map->n_sprites = 10;
(*game)->screen->hud = mlx_texture_to_image((*game)->screen->mlx, mlx_load_png("./assets/overlay.png")); (*game)->screen->hud = mlx_texture_to_image((*game)->screen->mlx, mlx_load_png("./assets/overlay.png"));
return (SUCCESS); return (SUCCESS);
} }

View File

@ -14,7 +14,7 @@ C 100,100,200
1111111 111 1111 111111 1111111111 111111 1111111 111 1111 111111 1111111111 111111
1000001110111001 100001 1000001001 10001 1000001110111001 100001 1000000001 10001
1001000000000001 100001 100000110111100011 1001000000000001 100001 100000110111100011
1000001110000001110011111111 110000001000000001 1000001110000001110011111111 110000001000000001
1000001 10001000000010000001 100000001000000001 1000001 10001000000010000001 100000001000000001