shitload of norm errors and double player detection

This commit is contained in:
whaffman 2025-05-07 11:53:10 +02:00
parent 0a25f91436
commit 25d2322b40
38 changed files with 373 additions and 353 deletions

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* collision.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* collision.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 14:40:59 by qmennen #+# #+# */
/* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
/* Updated: 2025/05/07 11:33:06 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* game.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* game.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
/* Updated: 2025/05/06 18:56:57 by qmennen ### ########.fr */
/* Updated: 2025/05/07 11:33:38 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/23 12:23:05 by whaffman #+# #+# */
/* Updated: 2025/04/23 12:23:14 by whaffman ######## odam.nl */
/* Updated: 2025/05/07 11:51:33 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/23 12:20:38 by whaffman #+# #+# */
/* Updated: 2025/04/23 12:21:00 by whaffman ######## odam.nl */
/* Updated: 2025/05/07 11:21:14 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -21,8 +21,12 @@ void grid_free(t_tile **grid, int height)
return ;
i = 0;
while (i < height)
{
if (grid[i])
{
free(grid[i]);
grid[i] = NULL;
}
i++;
}
free(grid);

View File

@ -1,18 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map_create.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* map_create.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/23 12:21:13 by whaffman #+# #+# */
/* Updated: 2025/05/06 16:00:10 by qmennen ### ########.fr */
/* Updated: 2025/05/07 11:52:14 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "cub3d.h"
int map_create(t_game **game, const char *mapfile)
{
t_tile **grid;
@ -26,7 +25,8 @@ int map_create(t_game **game, const char *mapfile)
return (FAILURE);
}
ft_memset((*game)->map->textures, 0, sizeof((*game)->map->textures));
parse_args(mapfile, (*game));
if (!parse_args(mapfile, (*game)))
return (FAILURE);
grid = copy_map((*game)->map->grid, (*game)->map->width, (*game)->map->height);
if (!grid)
{
@ -44,6 +44,5 @@ int map_create(t_game **game, const char *mapfile)
grid_free((*game)->map->grid, (*game)->map->height);
(*game)->map->grid = grid;
print_config((*game)->map);
return (SUCCESS);
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/23 12:22:28 by whaffman #+# #+# */
/* Updated: 2025/04/23 12:22:49 by whaffman ######## odam.nl */
/* Updated: 2025/05/07 11:51:46 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -16,7 +16,6 @@
void map_free(t_map *map)
{
grid_free(map->grid, map->height);
// free(map->grid);
free(map->NO_texture);
free(map->SO_texture);
free(map->WE_texture);

View File

@ -6,14 +6,13 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/25 09:54:24 by whaffman #+# #+# */
/* Updated: 2025/04/25 10:39:42 by whaffman ######## odam.nl */
/* Updated: 2025/05/07 11:49:58 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "cub3d.h"
#include "math.h"
t_vec2 add(t_vec2 a, t_vec2 b)
{
t_vec2 result;

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dist.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* dist.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/25 10:07:59 by whaffman #+# #+# */
/* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
/* Updated: 2025/05/07 11:50:10 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dist_point_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* dist_point_line.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/25 10:09:45 by whaffman #+# #+# */
/* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
/* Updated: 2025/05/07 11:50:20 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dot.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* dot.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/25 09:58:29 by whaffman #+# #+# */
/* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
/* Updated: 2025/05/07 11:50:27 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mul.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* mul.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/25 09:56:39 by whaffman #+# #+# */
/* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
/* Updated: 2025/05/07 11:50:43 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* norm.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* norm.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/25 09:59:13 by whaffman #+# #+# */
/* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
/* Updated: 2025/05/07 11:50:52 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/25 10:34:39 by whaffman #+# #+# */
/* Updated: 2025/04/25 10:40:17 by whaffman ######## odam.nl */
/* Updated: 2025/05/07 11:50:59 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rot.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* rot.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/25 10:35:58 by whaffman #+# #+# */
/* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
/* Updated: 2025/05/07 11:51:19 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/25 09:57:55 by whaffman #+# #+# */
/* Updated: 2025/04/25 10:40:27 by whaffman ######## odam.nl */
/* Updated: 2025/05/07 11:51:07 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 13:02:31 by whaffman #+# #+# */
/* Updated: 2025/04/22 13:03:03 by whaffman ######## odam.nl */
/* Updated: 2025/05/07 11:47:41 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 13:04:07 by whaffman #+# #+# */
/* Updated: 2025/04/22 13:06:41 by whaffman ######## odam.nl */
/* Updated: 2025/05/07 11:47:48 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 13:08:26 by whaffman #+# #+# */
/* Updated: 2025/04/22 13:08:37 by whaffman ######## odam.nl */
/* Updated: 2025/05/07 11:48:11 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 13:13:07 by whaffman #+# #+# */
/* Updated: 2025/04/22 13:19:40 by whaffman ######## odam.nl */
/* Updated: 2025/05/07 11:48:00 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 13:08:52 by whaffman #+# #+# */
/* Updated: 2025/04/22 13:09:33 by whaffman ######## odam.nl */
/* Updated: 2025/05/07 11:47:55 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -26,7 +26,6 @@ unsigned int parse_color(const char *color_str)
|| !(tokens[2] && ft_strlen(tokens[2]) <= 3)
|| tokens[3])
return (FAILURE);
r = ft_atoi(tokens[0]);
g = ft_atoi(tokens[1]);
b = ft_atoi(tokens[2]);

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 13:10:06 by whaffman #+# #+# */
/* Updated: 2025/04/22 13:10:23 by whaffman ######## odam.nl */
/* Updated: 2025/05/07 11:48:16 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 13:11:37 by whaffman #+# #+# */
/* Updated: 2025/04/25 11:38:44 by whaffman ######## odam.nl */
/* Updated: 2025/05/07 11:48:25 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* parse_map.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 13:12:04 by whaffman #+# #+# */
/* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
/* Updated: 2025/05/07 11:49:18 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -27,9 +27,13 @@ int parse_player(t_game *game, int y, int x, char c)
t_player *player;
player = game->player;
if (player->pos.x != -1 || player->pos.y != -1)
{
ft_putstr_fd("Error: Multiple players found\n", 2);
return (FAILURE);
}
player->pos.x = ((double)x + 0.5f);
player->pos.y = ((double)y + 0.5f);
if (c == 'N')
player->dir = parse_dir(0, -1);
else if (c == 'E')
@ -38,7 +42,6 @@ int parse_player(t_game *game, int y, int x, char c)
player->dir = parse_dir(0, 1);
else if (c == 'W')
player->dir = parse_dir(-1, 0);
player->camera = rot(player->dir, 0.5f * M_PI);
return (SUCCESS);
}
@ -60,9 +63,10 @@ int parse_map_line(char **lines, t_game *game, int y)
else
map->grid[y][x] = parse_tile(lines[y][x]);
if (map->grid[y][x] == TILE_PLAYER)
parse_player(game, y, x, lines[y][x]);
else if (map->grid[y][x] == -2)
{
if (!parse_player(game, y, x, lines[y][x]))
return (FAILURE);
}
if (map->grid[y][x] == -2)
return (FAILURE);
x++;
@ -84,13 +88,13 @@ int parse_map(char **lines, t_game *game)
map->grid = malloc(sizeof(t_tile *) * (map->height + 1));
if (!map->grid)
return (FAILURE);
ft_memset(map->grid, 0, sizeof(t_tile *) * (map->height + 1));
y = 0;
while (y < map->height)
{
if (!parse_map_line(lines, game, y))
return (map_free(map), FAILURE);
return (FAILURE);
y++;
}
map->grid[y] = NULL;
return (SUCCESS);
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 13:12:31 by whaffman #+# #+# */
/* Updated: 2025/04/22 13:12:56 by whaffman ######## odam.nl */
/* Updated: 2025/05/07 11:48:30 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 13:03:24 by whaffman #+# #+# */
/* Updated: 2025/04/22 13:17:28 by whaffman ######## odam.nl */
/* Updated: 2025/05/07 11:48:40 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 13:11:18 by whaffman #+# #+# */
/* Updated: 2025/04/22 13:11:28 by whaffman ######## odam.nl */
/* Updated: 2025/05/07 11:49:07 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -24,6 +24,5 @@ void print_config(t_map *map)
printf("Floor color: %u\n", map->floor_color);
printf("Ceiling color: %u\n", map->ceiling_color);
printf("Grid:\n");
print_map(map);
}

View File

@ -6,11 +6,10 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 13:07:09 by whaffman #+# #+# */
/* Updated: 2025/04/22 13:07:56 by whaffman ######## odam.nl */
/* Updated: 2025/05/07 11:49:04 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include <fcntl.h>
#include <unistd.h>

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* player.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* player.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/15 18:53:19 by qmennen #+# #+# */
/* Updated: 2025/05/06 19:22:37 by qmennen ### ########.fr */
/* Updated: 2025/05/07 11:35:06 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -19,8 +19,8 @@ int player_create(t_game **game)
player = malloc(sizeof(t_player));
if (!player)
return (FAILURE);
player->pos.x = 1;
player->pos.y = 1;
player->pos.x = -1;
player->pos.y = -1;
player->dir.x = 1;
player->dir.y = 0;
player->camera.x = 0;
@ -90,8 +90,9 @@ void player_render(t_screen *screen, t_player *player)
{
t_vec2 direction;
if (player->pos.x < 0 || player->pos.x >= screen->width || player->pos.y < 0 || player->pos.y >= screen->height)
return;
if (player->pos.x < 0 || player->pos.x >= screen->width
|| player->pos.y < 0 || player->pos.y >= screen->height)
return ;
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

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* DDAscratch.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* DDAscratch.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/05/02 11:58:09 by whaffman #+# #+# */
/* Updated: 2025/05/06 19:49:13 by qmennen ### ########.fr */
/* Updated: 2025/05/07 11:52:24 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -22,7 +22,6 @@ t_vec2 get_delta_dist(t_vec2 ray_dir)
delta_dist.x = 1e30;
else
delta_dist.x = fabs(1 / ray_dir.x);
if (ray_dir.y == 0)
delta_dist.y = 1e30;
else
@ -46,8 +45,10 @@ t_vec2 get_side_dist(t_vec2 ray_dir, t_vec2 pos, t_vec2 delta_dist)
const int raydir_y_pos = (ray_dir.y >= 0);
t_vec2 side_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_y_pos * (1 - frac_pos.y)) * delta_dist.y;
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_y_pos * (1 - frac_pos.y)) * delta_dist.y;
return (side_dist);
}
@ -66,7 +67,10 @@ int dda_main(t_vec2 ray_dir, t_vec2_int map_pos, t_vec2 *side_dist, t_map *map)
side_dist->y += delta_dist.y * side;
map_pos.x += step.x * (1 - side);
map_pos.y += step.y * side;
if (map_pos.x < 0 || map_pos.x >= map->width || map_pos.y < 0 || map_pos.y >= map->height)
if (map_pos.x < 0
|| map_pos.x >= map->width
|| map_pos.y < 0
|| map_pos.y >= map->height)
printf("Out of bounds: %d %d\n", map_pos.x, map_pos.y);
hit = (map->grid[map_pos.y][map_pos.x] == TILE_WALL);
}
@ -87,7 +91,8 @@ double dda(t_vec2 ray_dir, t_vec2 pos, t_map *map)
(t_vec2_int){(int)pos.x, (int)pos.y},
&side_dist,
map);
return ((1 - side) * (side_dist.x - delta_dist.x) - side * (side_dist.y - delta_dist.y));
return ((1 - side) * (side_dist.x - delta_dist.x)
- side * (side_dist.y - delta_dist.y));
}
t_side get_side(t_vec2 ray_dir, double perp_dist)
@ -130,48 +135,53 @@ t_render cast_ray(t_game *game, int x)
return (render);
}
unsigned int get_texture_color(mlx_texture_t *texture, t_render render, int tex_x, int tex_y)
unsigned int get_texture_color(mlx_texture_t *texture,
t_render render, int tex_x, int tex_y)
{
int index;
double dist;
index = (tex_x + tex_y * texture->height) * texture->bytes_per_pixel;
dist = (render.perp_dist == 0) * 1e30 + (render.perp_dist > 1) * render.perp_dist + (render.perp_dist <= 1) * 1;
return texture->pixels[index] << 24 | texture->pixels[index + 1] << 16 | texture->pixels[index + 2] << 8 | (int)(1.0 / dist * 255);
dist = (render.perp_dist == 0) * 1e30
+ (render.perp_dist > 1) * render.perp_dist
+ (render.perp_dist <= 1) * 1;
return (texture->pixels[index] << 24
| texture->pixels[index + 1] << 16
| texture->pixels[index + 2] << 8
| (int)(1.0 / dist * 255));
}
void draw_line(t_game *game, t_render render, int x)
{
int y;
int color;
int lineHeight;
int drawStart;
int texDrawStart;
int drawEnd;
int height;
int start;
int tex_start;
int end;
int tex_x;
int tex_y;
lineHeight = (int)(game->screen->height / render.perp_dist);
drawStart = -lineHeight / 2 + game->screen->height / 2;
texDrawStart = drawStart;
if (drawStart < 0)
drawStart = 0;
drawEnd = lineHeight / 2 + game->screen->height / 2;
if (drawEnd >= game->screen->height)
drawEnd = game->screen->height - 1;
height = (int)(game->screen->height / render.perp_dist);
start = -height / 2 + game->screen->height / 2;
tex_start = start;
if (start < 0)
start = 0;
end = height / 2 + game->screen->height / 2;
if (end >= game->screen->height)
end = game->screen->height - 1;
tex_x = (int)(render.wall_x * (double)game->map->textures[render.side]->width);
if (render.side == SIDE_NORTH || render.side == SIDE_EAST)
tex_x = game->map->textures[render.side]->width - tex_x - 1;
y = drawStart;
while (y < drawEnd)
y = start;
while (y < end)
{
tex_y = (int)(((double) game->map->textures[render.side]->height / (double) lineHeight) * (y - texDrawStart));
tex_y = (int)(((double) game->map->textures[render.side]->height / (double) height) * (y - tex_start));
color = get_texture_color(game->map->textures[render.side], render, tex_x, tex_y);
if (x < 0 || x >= game->screen->width || y < 0 || y >= game->screen->height)
break;
break ;
mlx_put_pixel(game->screen->img, x, y, color);
y++;
}
}
@ -189,18 +199,17 @@ void draw_floor(t_game *game)
const t_vec2 right_ray = add(game->player->dir, game->player->camera);
y = game->screen->height / 2;
while (y < game->screen->height )
while (y < game->screen->height)
{
row_dist = 0.5 * game->screen->height / (y - game->screen->height / 2.0);
floor_step = mul(mul(sub(right_ray, left_ray), row_dist), 1.0 / game->screen->width);
floor_pos = add(game->player->pos, mul(left_ray, row_dist));
x = 0;
while (x < game->screen->width)
{
floor_cell = (t_vec2_int){(int)floor_pos.x, (int)floor_pos.y};
tex_coords = (t_vec2_int){(int) (64 * (floor_pos.x - floor_cell.x)) & 63,
(int) (64 * (floor_pos.y - floor_cell.y)) & 63};
tex_coords = (t_vec2_int){(int)(64 * (floor_pos.x - floor_cell.x)) & 63,
(int)(64 * (floor_pos.y - floor_cell.y)) & 63};
floor_pos = add(floor_pos, floor_step);
color = get_texture_color(game->map->texture_floor, (t_render){row_dist, 0, 0}, tex_coords.x, tex_coords.y);
mlx_put_pixel(game->screen->img, x, y, color);
@ -226,4 +235,3 @@ void cast_rays(t_game *game)
x++;
}
}

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* render.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/15 16:28:10 by qmennen #+# #+# */
/* Updated: 2025/05/06 19:02:53 by qmennen ### ########.fr */
/* Updated: 2025/05/07 11:38:34 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -15,7 +15,10 @@
int render_check_bounds(t_screen *screen, t_vec2 *point)
{
return (point->x >= 0 && point->x < screen->width && point->y > 0 && point->y < screen->height);
return (point->x >= 0
&& point->x < screen->width
&& point->y > 0
&& point->y < screen->height);
}
void render_tile(t_screen *screen, int x, int y, t_tile tile)
@ -26,18 +29,17 @@ void render_tile(t_screen *screen, int x, int y, t_tile tile)
i = 0;
if (tile < 0)
return;
return ;
while ((i++) < TILE_SIZE * TILE_SIZE)
{
xp = x + (i % TILE_SIZE);
yp = y + (i / TILE_SIZE);
if (xp < 0 || xp >= screen->width || yp < 0 || yp >= screen->height)
continue;
continue ;
if (tile == TILE_WALL)
mlx_put_pixel(screen->minimap, xp, yp, 0xA88132aa);
else if (tile == TILE_EMPTY || tile == TILE_PLAYER)
mlx_put_pixel(screen->minimap, xp, yp, 0xaaaaaa44);
}
}
@ -46,13 +48,14 @@ void render_map(t_screen *screen, t_map *map)
int i;
int x;
int y;
i = 0;
while (i < map->width * map->height)
{
x = i % map->width;
y = i / map->width;
if (x < 0 || x >= map->width || y < 0 || y >= map->height)
continue;
continue ;
render_tile(screen, x * TILE_SIZE, y * TILE_SIZE, map->grid[y][x]);
i++;
}

View File

@ -6,13 +6,14 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/17 20:06:19 by qmennen #+# #+# */
/* Updated: 2025/05/04 16:55:14 by whaffman ######## odam.nl */
/* Updated: 2025/05/07 11:37:29 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "render.h"
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)
{
int i;
int size;
@ -26,7 +27,8 @@ 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->minimap, (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

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/17 20:05:51 by qmennen #+# #+# */
/* Updated: 2025/05/04 16:55:44 by whaffman ######## odam.nl */
/* Updated: 2025/05/07 11:37:36 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->minimap, i % screen->width, i / screen->width, 0x0);
mlx_put_pixel(screen->minimap,
i % screen->width, i / screen->width, 0x0);
}

View File

@ -6,17 +6,18 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/17 20:06:40 by qmennen #+# #+# */
/* Updated: 2025/05/04 16:56:16 by whaffman ######## odam.nl */
/* Updated: 2025/05/07 11:36:57 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "render.h"
static void line_low(t_screen *screen, t_vec2 start, t_vec2 end, unsigned int color)
static void line_low(t_screen *screen,
t_vec2 start, t_vec2 end, unsigned int color)
{
int delta;
int yi;
t_vec2 current;
t_vec2 cur;
t_vec2 delta_point;
delta_point.x = end.x - start.x;
@ -25,26 +26,27 @@ static void line_low(t_screen *screen, t_vec2 start, t_vec2 end, unsigned int co
if (end.y - start.y < 0)
yi = -1;
delta = 2 * delta_point.y - delta_point.x;
current = start;
while (current.x <= end.x)
cur = start;
while (cur.x <= end.x)
{
if (render_check_bounds(screen, &current))
mlx_put_pixel(screen->minimap, (int)current.x, (int)current.y, color);
if (render_check_bounds(screen, &cur))
mlx_put_pixel(screen->minimap, (int)cur.x, (int)cur.y, color);
if (delta > 0)
{
current.y += yi;
cur.y += yi;
delta -= 2 * delta_point.x;
}
delta += 2 * delta_point.y;
current.x++;
cur.x++;
}
}
static void line_high(t_screen *screen, t_vec2 start, t_vec2 end, unsigned int color)
static void line_high(t_screen *screen,
t_vec2 start, t_vec2 end, unsigned int color)
{
int delta;
int xi;
t_vec2 current;
t_vec2 cur;
t_vec2 delta_point;
delta_point.x = abs((int) end.x - (int) start.x);
@ -53,22 +55,23 @@ static void line_high(t_screen *screen, t_vec2 start, t_vec2 end, unsigned int c
if (end.x - start.x < 0)
xi = -1;
delta = 2 * delta_point.x - delta_point.y;
current = start;
while (current.y <= end.y)
cur = start;
while (cur.y <= end.y)
{
if (render_check_bounds(screen, &current))
mlx_put_pixel(screen->minimap, (int)current.x, (int)current.y, color);
if (render_check_bounds(screen, &cur))
mlx_put_pixel(screen->minimap, (int)cur.x, (int)cur.y, color);
if (delta > 0)
{
current.x += xi;
cur.x += xi;
delta -= 2 * delta_point.y;
}
delta += 2 * delta_point.x;
current.y++;
cur.y++;
}
}
void render_line(t_screen *screen, t_vec2 start, t_vec2 end, unsigned int color)
void render_line(t_screen *screen,
t_vec2 start, t_vec2 end, unsigned int color)
{
if ((start.x < 0 || start.x >= (int) screen->img->width
|| start.y < 0 || start.y >= (int) screen->img->height)

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* texutre_load.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* texutre_load.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/05/06 15:45:58 by qmennen #+# #+# */
/* Updated: 2025/05/06 19:01:15 by qmennen ### ########.fr */
/* Updated: 2025/05/07 11:49:37 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -1,19 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* errors.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* errors.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/15 15:40:14 by qmennen #+# #+# */
/* Updated: 2025/04/15 16:10:57 by qmennen ### ########.fr */
/* Updated: 2025/05/07 11:34:57 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "MLX42.h"
#include "cub3d.h"
const char *last_error()
const char *last_error(void)
{
if (mlx_errno > 0)
return (mlx_strerror(mlx_errno));
@ -26,6 +26,7 @@ const char *last_error()
void game_error(t_game *game, const char *msg)
{
const char *last_err = last_error();
if (msg == NULL && last_err)
printf(RED"%s\n"RESET, last_err);
else

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* initialize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* initialize.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 17:08:26 by qmennen #+# #+# */
/* Updated: 2025/05/06 16:18:44 by qmennen ### ########.fr */
/* Updated: 2025/05/07 11:35:18 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* keyboard.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* keyboard.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/17 19:29:29 by qmennen #+# #+# */
/* Updated: 2025/04/22 14:46:18 by qmennen ### ########.fr */
/* Updated: 2025/05/07 11:35:41 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -44,7 +44,7 @@ void keyboard_update(t_game *game)
int get_key(t_game *game, int k)
{
return mlx_is_key_down(game->screen->mlx, k);
return (mlx_is_key_down(game->screen->mlx, k));
}
int get_key_down(t_game *game, int k)

View File

@ -19,7 +19,7 @@ C 100,100,200
1000001110000001110011111111 110000001000000001
1000001 10001000000010000001 100000001000000001
1000001110001000000000000001111100000001000000001
100W000000001111111111100000000000111111000000001
1000000000001111111111100000000000111111000000001
1000000W00001 1000011111111 1000001001
1000011111001 111111100001 1111111111110001
100001 1001 1000000000011111111000010111 10001