minimap on different image, fix dda bug

This commit is contained in:
Willem Haffmans 2025-05-04 17:03:40 +02:00
parent 540e9acf63
commit fba7312770
14 changed files with 118 additions and 81 deletions

View File

@ -6,7 +6,7 @@
# By: whaffman <whaffman@student.codam.nl> +#+ #
# +#+ #
# Created: 2024/10/15 11:48:46 by whaffman #+# #+# #
# Updated: 2025/05/04 13:30:55 by whaffman ######## odam.nl #
# Updated: 2025/05/04 16:46:48 by whaffman ######## odam.nl #
# #
# **************************************************************************** #

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */
/* Updated: 2025/05/04 13:35:12 by whaffman ######## odam.nl */
/* Updated: 2025/05/04 16:46:00 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -16,8 +16,8 @@
# define FAILURE 0
# define SUCCESS 1
# define WIDTH 1280
# define HEIGHT 720
# define WIDTH 1920
# define HEIGHT 1080
# define TITLE "Cub3D"
# define RESET "\033[0m"
@ -31,7 +31,7 @@
# define WHITE "\033[0;37m"
# define NUM_KEYS 256
# define TILE_SIZE 1
# define TILE_SIZE 16
# include "MLX42.h"
# include "allowed.h"

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */
/* Updated: 2025/05/04 13:17:15 by whaffman ######## odam.nl */
/* Updated: 2025/05/04 16:53:48 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -74,6 +74,7 @@ typedef struct s_screen
{
mlx_t *mlx;
mlx_image_t *img;
mlx_image_t *minimap;
unsigned int width;
unsigned int height;
} t_screen;

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* collision.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* collision.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 14:40:59 by qmennen #+# #+# */
/* Updated: 2025/04/22 14:42:09 by qmennen ### ########.fr */
/* Updated: 2025/05/04 14:51:40 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -17,7 +17,7 @@ int collision_horizontal(t_map *map, t_player *player, float xa)
{
t_tile tile;
tile = get_tile(map, (int) ((player->pos.x + xa) / TILE_SIZE), (int) ((player->pos.y) / TILE_SIZE));
tile = get_tile(map, (int) ((player->pos.x + xa)), (int) ((player->pos.y)));
return (tile != TILE_WALL && tile != TILE_VOID);
}
@ -25,6 +25,6 @@ int collision_vertical(t_map *map, t_player *player, float ya)
{
t_tile tile;
tile = get_tile(map, (int) ((player->pos.x) / TILE_SIZE), (int) ((player->pos.y + ya) / TILE_SIZE));
tile = get_tile(map, (int) ((player->pos.x)), (int) ((player->pos.y + ya)));
return (tile != TILE_WALL && tile != TILE_VOID);
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
/* Updated: 2025/05/04 13:21:37 by whaffman ######## odam.nl */
/* Updated: 2025/05/04 16:44:45 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -42,14 +42,23 @@ void game_loop(void *param)
{
t_game *game;
float delta_time;
static int framecount = 0;
static int fps = 0;
framecount++;
game = (t_game *)param;
delta_time = game->screen->mlx->delta_time;
fps += (int)(1.f / delta_time);
if (framecount % 20 == 0)
{
fprintf(stderr, "FPS: %d\n", fps/20);
fps = 0;
}
render_clear(game->screen);
player_update(game, delta_time);
// render_entities(game);
// render_map(game->screen, game->map);
cast_rays(game);
render_map(game->screen, game->map);
render_entities(game);
keyboard_update(game); // Goes last
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 13:12:04 by whaffman #+# #+# */
/* Updated: 2025/04/25 11:55:27 by whaffman ######## odam.nl */
/* Updated: 2025/05/04 15:07:44 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -27,15 +27,15 @@ int parse_player(t_game *game, int y, int x, char c)
t_player *player;
player = game->player;
player->pos.x = ((float)x + 0.5f) * TILE_SIZE;
player->pos.y = ((float)y + 0.5f) * TILE_SIZE;
player->pos.x = ((float)x + 0.5f);
player->pos.y = ((float)y + 0.5f);
if (c == 'N')
player->dir = parse_dir(0, 1);
player->dir = parse_dir(0, -1);
else if (c == 'E')
player->dir = parse_dir(1, 0);
else if (c == 'S')
player->dir = parse_dir(0, -1);
player->dir = parse_dir(0, 1);
else if (c == 'W')
player->dir = parse_dir(-1, 0);

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/15 18:53:19 by qmennen #+# #+# */
/* Updated: 2025/05/04 13:39:49 by whaffman ######## odam.nl */
/* Updated: 2025/05/04 16:58:40 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -19,13 +19,13 @@ int player_create(t_game **game)
player = malloc(sizeof(t_player));
if (!player)
return (FAILURE);
player->pos.x = 0;
player->pos.y = 0;
player->pos.x = 1;
player->pos.y = 1;
player->dir.x = 1;
player->dir.y = 0;
player->camera.x = 0;
player->camera.y = 0.66f;
player->speed = 1.f;
player->speed = 3.f;
player->fov = 90.f;
(*game)->player = player;
return (SUCCESS);
@ -38,7 +38,19 @@ static void move(t_map *map, t_player *player, int dir, float delta)
float ya;
xa = dir * player->dir.x * player->speed * delta;
ya = dir * -1 * player->dir.y * player->speed * delta;
ya = dir * player->dir.y * player->speed * delta;
if ( xa != 0 && collision_horizontal(map, player, xa))
player->pos.x += xa;
if ( ya != 0 && collision_vertical(map, player, ya))
player->pos.y += ya;
}
static void strave(t_map *map, t_player *player, int dir, float delta)
{
float xa;
float ya;
xa = dir * perp(player->dir).x * player->speed * delta;
ya = dir * perp(player->dir).y * player->speed * delta;
if ( xa != 0 && collision_horizontal(map, player, xa))
player->pos.x += xa;
if ( ya != 0 && collision_vertical(map, player, ya))
@ -62,10 +74,14 @@ void player_update(t_game *game, float delta_time)
move(game->map, game->player, 1, delta_time);
else if (get_key(game, MLX_KEY_S))
move(game->map, game->player, -1, delta_time);
if (get_key(game, MLX_KEY_A))
strave(game->map, game->player, -1, delta_time);
else if (get_key(game, MLX_KEY_D))
strave(game->map, game->player, 1, delta_time);
if (get_key(game, MLX_KEY_LEFT))
rotate(game->player, .1f);
rotate(game->player, -.05f);
else if (get_key(game, MLX_KEY_RIGHT))
rotate(game->player, -.1f);
rotate(game->player, .05f);
}
void player_render(t_screen *screen, t_player *player)
@ -74,8 +90,7 @@ void player_render(t_screen *screen, t_player *player)
if (player->pos.x < 0 || player->pos.x >= screen->width || player->pos.y < 0 || player->pos.y >= screen->height)
return ;
render_circle(screen, player->pos, 4, 0x3294a8ff);
direction.x = player->pos.x + player->dir.x * 30;
direction.y = player->pos.y - player->dir.y * 30;
render_line(screen, player->pos, direction, 0xa83232ff);
render_circle(screen, mul(player->pos, TILE_SIZE), 4, 0x111111ff);
direction = add(mul(player->pos,TILE_SIZE), mul(player->dir, TILE_SIZE));
render_line(screen, mul(player->pos,TILE_SIZE), direction, 0xa83232ff);
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/05/02 11:58:09 by whaffman #+# #+# */
/* Updated: 2025/05/04 13:38:50 by whaffman ######## odam.nl */
/* Updated: 2025/05/04 17:01:12 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -44,7 +44,7 @@ t_vec2 get_side_dist(t_vec2 ray_dir, t_vec2 pos, t_vec2 delta_dist)
side_dist.x = ((1 - raydir_x_pos) * frac_pos.x
+ raydir_x_pos * (1 - frac_pos.x)) * delta_dist.x;
side_dist.y = ((1 - raydir_y_pos) * frac_pos.y
+ raydir_x_pos * (1 - frac_pos.y)) * delta_dist.y;
+ raydir_y_pos * (1 - frac_pos.y)) * delta_dist.y;
return (side_dist);
}
@ -133,6 +133,7 @@ t_render cast_ray(t_game *game, int x)
unsigned int get_color(t_render render)
{
float dist;
//int alpha;
const unsigned int color[4] = {
0x488B49,
0x4AAD52,
@ -141,7 +142,9 @@ unsigned int get_color(t_render render)
};
dist = (render.perp_dist == 0) * 1e30
+ (render.perp_dist != 0) * render.perp_dist;
+ (render.perp_dist > 1) * render.perp_dist
+ (render.perp_dist <= 1) * 1;
//alpha = (int)(1.0 / dist * 255);
return (color[render.side] << 8 |(int)(1.0 / dist * 255));
}
void draw_line(t_game *game, t_render render, int x)
@ -161,9 +164,9 @@ void draw_line(t_game *game, t_render render, int x)
while (y < game->screen->height)
{
if (y < drawStart)
color = game->map->ceiling_color << 8 | 0xFF;
color = game->map->ceiling_color << 8 | (int)fabs(2 * y * 0xFF /(float) game->screen->height - 0xFF);
else if (y > drawEnd)
color = game->map->floor_color << 8 | 0xFF;
color = game->map->floor_color << 8 | (int)fabs(2 * y * 0xFF /(float) game->screen->height - 0xFF);
else
color = get_color(render);
mlx_put_pixel(game->screen->img, x, y, color);
@ -180,7 +183,6 @@ void cast_rays(t_game *game)
while (x < game->screen->width)
{
render = cast_ray(game, x);
(void)render;
draw_line(game, render, x);
//ETC

View File

@ -3,10 +3,10 @@
/* :::::::: */
/* render.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/15 16:28:10 by qmennen #+# #+# */
/* Updated: 2025/04/20 14:00:47 by whaffman ######## odam.nl */
/* Updated: 2025/05/04 16:54:33 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -25,7 +25,7 @@ void render_tile(t_screen *screen, int x, int y, t_tile tile)
int yp;
i = 0;
if (tile == 0)
if (tile < 0)
return;
while ((i++) < TILE_SIZE * TILE_SIZE)
{
@ -34,7 +34,10 @@ void render_tile(t_screen *screen, int x, int y, t_tile tile)
if (xp < 0 || xp >= screen->width || yp < 0 || yp >= screen->height)
continue;
if (tile == TILE_WALL)
mlx_put_pixel(screen->img, xp, yp, 0xA88132ff);
mlx_put_pixel(screen->minimap, xp, yp, 0xA88132aa);
else if (tile == TILE_EMPTY || tile == TILE_PLAYER)
mlx_put_pixel(screen->minimap, xp, yp, 0xaaaaaa44);
}
}

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render_circle.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* render_circle.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/17 20:06:19 by qmennen #+# #+# */
/* Updated: 2025/04/17 20:06:32 by qmennen ### ########.fr */
/* Updated: 2025/05/04 16:55:14 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -26,7 +26,7 @@ void render_circle(t_screen *screen, t_vec2 point, int radius, unsigned int colo
x = i % size - radius;
y = i / size - radius;
if (x * x + y * y <= radius * radius)
mlx_put_pixel(screen->img, (int) point.x + x, (int) point.y + y, color);
mlx_put_pixel(screen->minimap, (int) point.x + x, (int) point.y + y, color);
i++;
}
}

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* clear_screen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* render_clear.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/17 20:05:51 by qmennen #+# #+# */
/* Updated: 2025/04/17 20:06:00 by qmennen ### ########.fr */
/* Updated: 2025/05/04 16:55:44 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -18,6 +18,6 @@ void render_clear(t_screen *screen)
i = 0;
while (i++ < screen->width * screen->height)
mlx_put_pixel(screen->img, i % screen->width, i / screen->width, 0x212121FF);
mlx_put_pixel(screen->minimap, i % screen->width, i / screen->width, 0x0);
}

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* render_line.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/17 20:06:40 by qmennen #+# #+# */
/* Updated: 2025/04/17 20:08:17 by qmennen ### ########.fr */
/* Updated: 2025/05/04 16:56:16 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -29,7 +29,7 @@ static void line_low(t_screen *screen, t_vec2 start, t_vec2 end, unsigned int co
while (current.x <= end.x)
{
if (render_check_bounds(screen, &current))
mlx_put_pixel(screen->img, (int)current.x, (int)current.y, color);
mlx_put_pixel(screen->minimap, (int)current.x, (int)current.y, color);
if (delta > 0)
{
current.y += yi;
@ -57,7 +57,7 @@ static void line_high(t_screen *screen, t_vec2 start, t_vec2 end, unsigned int c
while (current.y <= end.y)
{
if (render_check_bounds(screen, &current))
mlx_put_pixel(screen->img, (int)current.x, (int)current.y, color);
mlx_put_pixel(screen->minimap, (int)current.x, (int)current.y, color);
if (delta > 0)
{
current.x += xi;

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* screen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* screen.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/15 15:30:27 by qmennen #+# #+# */
/* Updated: 2025/04/22 17:25:13 by qmennen ### ########.fr */
/* Updated: 2025/05/04 16:57:06 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -16,6 +16,7 @@ int screen_create(t_game **game)
{
t_screen *screen;
mlx_image_t *img;
mlx_image_t *minimap;
mlx_t *mlx;
screen = malloc(sizeof(t_screen));
@ -31,7 +32,11 @@ int screen_create(t_game **game)
img = mlx_new_image(screen->mlx, WIDTH, HEIGHT);
if (!img)
return (FAILURE);
minimap = mlx_new_image(screen->mlx, WIDTH , HEIGHT );
if (!minimap)
return (FAILURE);
screen->img = img;
screen->minimap = minimap;
(*game)->screen = screen;
return (SUCCESS);
}
@ -48,6 +53,11 @@ int screen_display(t_screen *screen)
printf(RED"Failed to display buffer image\n"RESET);
return (FAILURE);
}
if (mlx_image_to_window(screen->mlx, screen->minimap, 0, 0) < 0)
{
printf(RED"Failed to display buffer image\n"RESET);
return (FAILURE);
}
mlx_get_monitor_size(0, &m_width, &m_height);
if (m_width == 0 || m_height == 0)
{

View File

@ -8,13 +8,10 @@ SO ./path/file.png
EA ./path/file.png
F 200,200,200
F 90,30,30
C 100,100,10
C 100,100,200
1111111 111 1111 111111 1111111111 111111