shitload of norm errors and double player detection
This commit is contained in:
parent
0a25f91436
commit
25d2322b40
@ -1,28 +1,28 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 */
|
||||
/* :::::::: */
|
||||
/* collision.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/22 14:40:59 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/07 11:33:06 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "collision.h"
|
||||
|
||||
int collision_horizontal(t_map *map, t_player *player, double xa)
|
||||
int collision_horizontal(t_map *map, t_player *player, double xa)
|
||||
{
|
||||
t_tile tile;
|
||||
t_tile tile;
|
||||
|
||||
tile = get_tile(map, (int)((player->pos.x + xa)), (int)((player->pos.y)));
|
||||
return (tile != TILE_WALL && tile != TILE_VOID);
|
||||
}
|
||||
|
||||
int collision_vertical(t_map *map, t_player *player, double ya)
|
||||
int collision_vertical(t_map *map, t_player *player, double ya)
|
||||
{
|
||||
t_tile tile;
|
||||
t_tile tile;
|
||||
|
||||
tile = get_tile(map, (int)((player->pos.x)), (int)((player->pos.y + ya)));
|
||||
return (tile != TILE_WALL && tile != TILE_VOID);
|
||||
|
||||
32
src/game.c
32
src/game.c
@ -1,18 +1,18 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 */
|
||||
/* :::::::: */
|
||||
/* game.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/07 11:33:38 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cub3d.h"
|
||||
|
||||
int game_create(t_game **game)
|
||||
int game_create(t_game **game)
|
||||
{
|
||||
*game = malloc(sizeof(t_game));
|
||||
if (!game)
|
||||
@ -24,7 +24,7 @@ int game_create(t_game **game)
|
||||
return (SUCCESS);
|
||||
}
|
||||
|
||||
void free_game(t_game **game)
|
||||
void free_game(t_game **game)
|
||||
{
|
||||
if (game && *game)
|
||||
{
|
||||
@ -38,12 +38,12 @@ void free_game(t_game **game)
|
||||
}
|
||||
}
|
||||
|
||||
void game_loop(void *param)
|
||||
void game_loop(void *param)
|
||||
{
|
||||
t_game *game;
|
||||
double delta_time;
|
||||
static int framecount = 0;
|
||||
static int fps = 0;
|
||||
t_game *game;
|
||||
double delta_time;
|
||||
static int framecount = 0;
|
||||
static int fps = 0;
|
||||
|
||||
framecount++;
|
||||
game = (t_game *)param;
|
||||
@ -62,7 +62,7 @@ void game_loop(void *param)
|
||||
keyboard_update(game);
|
||||
}
|
||||
|
||||
void game_free(t_game *game)
|
||||
void game_free(t_game *game)
|
||||
{
|
||||
if (game->screen)
|
||||
{
|
||||
@ -80,7 +80,7 @@ void game_free(t_game *game)
|
||||
free(game);
|
||||
}
|
||||
|
||||
void game_terminate(t_game *game)
|
||||
void game_terminate(t_game *game)
|
||||
{
|
||||
game_free(game);
|
||||
exit(EXIT_SUCCESS);
|
||||
|
||||
@ -6,13 +6,13 @@
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cub3d.h"
|
||||
|
||||
t_tile get_tile(t_map *map, int x, int y)
|
||||
t_tile get_tile(t_map *map, int x, int y)
|
||||
{
|
||||
if (x < 0 || y < 0 || x >= map->width || y >= map->height)
|
||||
return (TILE_WALL);
|
||||
|
||||
@ -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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -22,7 +22,11 @@ void grid_free(t_tile **grid, int height)
|
||||
i = 0;
|
||||
while (i < height)
|
||||
{
|
||||
free(grid[i]);
|
||||
if (grid[i])
|
||||
{
|
||||
free(grid[i]);
|
||||
grid[i] = NULL;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
free(grid);
|
||||
|
||||
@ -1,18 +1,17 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 */
|
||||
/* :::::::: */
|
||||
/* map_create.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/23 12:21:13 by whaffman #+# #+# */
|
||||
/* 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);
|
||||
}
|
||||
@ -6,17 +6,16 @@
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "cub3d.h"
|
||||
|
||||
void map_free(t_map *map)
|
||||
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);
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 */
|
||||
/* :::::::: */
|
||||
/* dist.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/25 10:07:59 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/07 11:50:10 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,7 +14,7 @@
|
||||
#include "math.h"
|
||||
#include "vec_math.h"
|
||||
|
||||
double dist(t_vec2 a, t_vec2 b)
|
||||
double dist(t_vec2 a, t_vec2 b)
|
||||
{
|
||||
return (sqrtf((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)));
|
||||
}
|
||||
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 */
|
||||
/* :::::::: */
|
||||
/* dist_point_line.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/25 10:09:45 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/07 11:50:20 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,10 +14,10 @@
|
||||
#include "math.h"
|
||||
#include "vec_math.h"
|
||||
|
||||
double dist_point_line(t_vec2 point, t_vec2_line line)
|
||||
double dist_point_line(t_vec2 point, t_vec2_line line)
|
||||
{
|
||||
double d;
|
||||
t_vec2 ps;
|
||||
double d;
|
||||
t_vec2 ps;
|
||||
|
||||
ps = sub(line.support, point);
|
||||
d = norm(sub(ps, mul(line.dir, dot(ps, line.dir))));
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 */
|
||||
/* :::::::: */
|
||||
/* dot.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/25 09:58:29 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/07 11:50:27 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,7 +14,7 @@
|
||||
#include "math.h"
|
||||
#include "vec_math.h"
|
||||
|
||||
double dot(t_vec2 a, t_vec2 b)
|
||||
double dot(t_vec2 a, t_vec2 b)
|
||||
{
|
||||
return (a.x * b.x + a.y * b.y);
|
||||
}
|
||||
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 */
|
||||
/* :::::::: */
|
||||
/* mul.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/25 09:56:39 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/07 11:50:43 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,9 +14,9 @@
|
||||
#include "math.h"
|
||||
#include "vec_math.h"
|
||||
|
||||
t_vec2 mul(t_vec2 a, double b)
|
||||
t_vec2 mul(t_vec2 a, double b)
|
||||
{
|
||||
t_vec2 result;
|
||||
t_vec2 result;
|
||||
|
||||
result.x = a.x * b;
|
||||
result.y = a.y * b;
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 */
|
||||
/* :::::::: */
|
||||
/* norm.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/25 09:59:13 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/07 11:50:52 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,7 +14,7 @@
|
||||
#include "math.h"
|
||||
#include "vec_math.h"
|
||||
|
||||
double norm(t_vec2 a)
|
||||
double norm(t_vec2 a)
|
||||
{
|
||||
return (sqrtf(a.x * a.x + a.y * a.y));
|
||||
}
|
||||
@ -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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 */
|
||||
/* :::::::: */
|
||||
/* rot.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/25 10:35:58 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/07 11:51:19 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,11 +14,11 @@
|
||||
#include "math.h"
|
||||
#include "vec_math.h"
|
||||
|
||||
t_vec2 rot(t_vec2 a, double angle)
|
||||
t_vec2 rot(t_vec2 a, double angle)
|
||||
{
|
||||
t_vec2 result;
|
||||
double cos_angle;
|
||||
double sin_angle;
|
||||
t_vec2 result;
|
||||
double cos_angle;
|
||||
double sin_angle;
|
||||
|
||||
cos_angle = cosf(angle);
|
||||
sin_angle = sinf(angle);
|
||||
|
||||
@ -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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
@ -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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
@ -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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
@ -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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
@ -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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
@ -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]);
|
||||
|
||||
@ -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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
@ -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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,8 +14,8 @@
|
||||
|
||||
int parse_file(char *buffer, t_game *game)
|
||||
{
|
||||
char **lines;
|
||||
int i;
|
||||
char **lines;
|
||||
int i;
|
||||
|
||||
lines = pointer_lines(buffer, '\n');
|
||||
if (!lines)
|
||||
|
||||
@ -1,35 +1,39 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 */
|
||||
/* :::::::: */
|
||||
/* parse_map.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/22 13:12:04 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/07 11:49:18 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cub3d.h"
|
||||
#include <math.h>
|
||||
|
||||
t_vec2 parse_dir(int x, int y)
|
||||
t_vec2 parse_dir(int x, int y)
|
||||
{
|
||||
t_vec2 dir;
|
||||
t_vec2 dir;
|
||||
|
||||
dir.x = (double)x;
|
||||
dir.y = (double)y;
|
||||
return (dir);
|
||||
}
|
||||
|
||||
int parse_player(t_game *game, int y, int x, char c)
|
||||
int parse_player(t_game *game, int y, int x, char c)
|
||||
{
|
||||
t_player *player;
|
||||
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,15 +42,14 @@ 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);
|
||||
}
|
||||
|
||||
int parse_map_line(char **lines, t_game *game, int y)
|
||||
int parse_map_line(char **lines, t_game *game, int y)
|
||||
{
|
||||
int x;
|
||||
t_map *map;
|
||||
int x;
|
||||
t_map *map;
|
||||
|
||||
map = game->map;
|
||||
map->grid[y] = malloc(sizeof(t_tile) * (map->width + 1));
|
||||
@ -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)
|
||||
return (FAILURE);
|
||||
{
|
||||
if (!parse_player(game, y, x, lines[y][x]))
|
||||
return (FAILURE);
|
||||
}
|
||||
if (map->grid[y][x] == -2)
|
||||
return (FAILURE);
|
||||
x++;
|
||||
@ -70,10 +74,10 @@ int parse_map_line(char **lines, t_game *game, int y)
|
||||
return (SUCCESS);
|
||||
}
|
||||
|
||||
int parse_map(char **lines, t_game *game)
|
||||
int parse_map(char **lines, t_game *game)
|
||||
{
|
||||
int y;
|
||||
t_map *map;
|
||||
int y;
|
||||
t_map *map;
|
||||
|
||||
y = 0;
|
||||
while (lines[y])
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
@ -6,14 +6,14 @@
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "cub3d.h"
|
||||
|
||||
char **pointer_lines(char *buffer, char c)
|
||||
char **pointer_lines(char *buffer, char c)
|
||||
{
|
||||
char **lines;
|
||||
size_t count;
|
||||
|
||||
@ -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);
|
||||
}
|
||||
@ -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>
|
||||
|
||||
|
||||
45
src/player.c
45
src/player.c
@ -1,26 +1,26 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 */
|
||||
/* :::::::: */
|
||||
/* player.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/15 18:53:19 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/07 11:35:06 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cub3d.h"
|
||||
|
||||
int player_create(t_game **game)
|
||||
int player_create(t_game **game)
|
||||
{
|
||||
t_player *player;
|
||||
t_player *player;
|
||||
|
||||
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;
|
||||
@ -32,10 +32,10 @@ int player_create(t_game **game)
|
||||
return (SUCCESS);
|
||||
}
|
||||
|
||||
static void move(t_map *map, t_player *player, int dir, double delta)
|
||||
static void move(t_map *map, t_player *player, int dir, double delta)
|
||||
{
|
||||
double xa;
|
||||
double ya;
|
||||
double xa;
|
||||
double ya;
|
||||
|
||||
xa = dir * player->dir.x * player->speed * delta;
|
||||
ya = dir * player->dir.y * player->speed * delta;
|
||||
@ -51,10 +51,10 @@ static void move(t_map *map, t_player *player, int dir, double delta)
|
||||
}
|
||||
}
|
||||
|
||||
static void strave(t_map *map, t_player *player, int dir, double delta)
|
||||
static void strave(t_map *map, t_player *player, int dir, double delta)
|
||||
{
|
||||
double xa;
|
||||
double ya;
|
||||
double xa;
|
||||
double ya;
|
||||
|
||||
xa = dir * perp(player->dir).x * player->speed * delta;
|
||||
ya = dir * perp(player->dir).y * player->speed * delta;
|
||||
@ -64,13 +64,13 @@ static void strave(t_map *map, t_player *player, int dir, double delta)
|
||||
player->pos.y += ya;
|
||||
}
|
||||
|
||||
static void rotate(t_player *player, double rot_speed)
|
||||
static void rotate(t_player *player, double rot_speed)
|
||||
{
|
||||
player->dir = rot(player->dir, rot_speed);
|
||||
player->camera = rot(player->camera, rot_speed);
|
||||
}
|
||||
|
||||
void player_update(t_game *game, double delta_time)
|
||||
void player_update(t_game *game, double delta_time)
|
||||
{
|
||||
if (get_key(game, MLX_KEY_W))
|
||||
move(game->map, game->player, 1, delta_time);
|
||||
@ -86,12 +86,13 @@ void player_update(t_game *game, double delta_time)
|
||||
rotate(game->player, .05f);
|
||||
}
|
||||
|
||||
void player_render(t_screen *screen, t_player *player)
|
||||
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);
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 */
|
||||
/* :::::::: */
|
||||
/* DDAscratch.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/05/02 11:58:09 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/07 11:52:24 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,15 +14,14 @@
|
||||
#include "vec_math.h"
|
||||
#include <math.h>
|
||||
|
||||
t_vec2 get_delta_dist(t_vec2 ray_dir)
|
||||
t_vec2 get_delta_dist(t_vec2 ray_dir)
|
||||
{
|
||||
t_vec2 delta_dist;
|
||||
t_vec2 delta_dist;
|
||||
|
||||
if (ray_dir.x == 0)
|
||||
delta_dist.x = 1e30;
|
||||
else
|
||||
delta_dist.x = fabs(1 / ray_dir.x);
|
||||
|
||||
if (ray_dir.y == 0)
|
||||
delta_dist.y = 1e30;
|
||||
else
|
||||
@ -30,33 +29,35 @@ t_vec2 get_delta_dist(t_vec2 ray_dir)
|
||||
return (delta_dist);
|
||||
}
|
||||
|
||||
t_vec2 get_step(t_vec2 ray_dir)
|
||||
t_vec2 get_step(t_vec2 ray_dir)
|
||||
{
|
||||
t_vec2 step;
|
||||
t_vec2 step;
|
||||
|
||||
step.x = 2 * (ray_dir.x >= 0) - 1;
|
||||
step.y = 2 * (ray_dir.y >= 0) - 1;
|
||||
return (step);
|
||||
}
|
||||
|
||||
t_vec2 get_side_dist(t_vec2 ray_dir, t_vec2 pos, t_vec2 delta_dist)
|
||||
t_vec2 get_side_dist(t_vec2 ray_dir, t_vec2 pos, t_vec2 delta_dist)
|
||||
{
|
||||
const t_vec2 frac_pos = (t_vec2){pos.x - (int)pos.x, pos.y - (int)pos.y};
|
||||
const int raydir_x_pos = (ray_dir.x >= 0);
|
||||
const int raydir_y_pos = (ray_dir.y >= 0);
|
||||
t_vec2 side_dist;
|
||||
const t_vec2 frac_pos = (t_vec2){pos.x - (int)pos.x, pos.y - (int)pos.y};
|
||||
const int raydir_x_pos = (ray_dir.x >= 0);
|
||||
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);
|
||||
}
|
||||
|
||||
int dda_main(t_vec2 ray_dir, t_vec2_int map_pos, t_vec2 *side_dist, t_map *map)
|
||||
int dda_main(t_vec2 ray_dir, t_vec2_int map_pos, t_vec2 *side_dist, t_map *map)
|
||||
{
|
||||
const t_vec2 delta_dist = get_delta_dist(ray_dir);
|
||||
const t_vec2 step = get_step(ray_dir);
|
||||
int side;
|
||||
int hit;
|
||||
const t_vec2 delta_dist = get_delta_dist(ray_dir);
|
||||
const t_vec2 step = get_step(ray_dir);
|
||||
int side;
|
||||
int hit;
|
||||
|
||||
hit = 0;
|
||||
while (hit == 0)
|
||||
@ -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);
|
||||
}
|
||||
@ -76,21 +80,22 @@ int dda_main(t_vec2 ray_dir, t_vec2_int map_pos, t_vec2 *side_dist, t_map *map)
|
||||
// Returns the distance to the wall hit if the wall is hit in y
|
||||
// direction the result is negative, if the wall is hit
|
||||
// in x direction the result is positive
|
||||
double dda(t_vec2 ray_dir, t_vec2 pos, t_map *map)
|
||||
double dda(t_vec2 ray_dir, t_vec2 pos, t_map *map)
|
||||
{
|
||||
const t_vec2 delta_dist = get_delta_dist(ray_dir);
|
||||
t_vec2 side_dist;
|
||||
int side;
|
||||
const t_vec2 delta_dist = get_delta_dist(ray_dir);
|
||||
t_vec2 side_dist;
|
||||
int side;
|
||||
|
||||
side_dist = get_side_dist(ray_dir, pos, delta_dist);
|
||||
side = dda_main(ray_dir,
|
||||
(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));
|
||||
(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));
|
||||
}
|
||||
|
||||
t_side get_side(t_vec2 ray_dir, double perp_dist)
|
||||
t_side get_side(t_vec2 ray_dir, double perp_dist)
|
||||
{
|
||||
if (perp_dist > 0)
|
||||
{
|
||||
@ -108,16 +113,16 @@ t_side get_side(t_vec2 ray_dir, double perp_dist)
|
||||
}
|
||||
}
|
||||
|
||||
t_render cast_ray(t_game *game, int x)
|
||||
t_render cast_ray(t_game *game, int x)
|
||||
{
|
||||
t_vec2 ray_dir;
|
||||
t_vec2 pos;
|
||||
t_render render;
|
||||
double perp_dist;
|
||||
t_vec2 ray_dir;
|
||||
t_vec2 pos;
|
||||
t_render render;
|
||||
double perp_dist;
|
||||
|
||||
ray_dir = add(game->player->dir,
|
||||
mul(game->player->camera,
|
||||
(2.0f * x / (double)game->screen->width - 1)));
|
||||
mul(game->player->camera,
|
||||
(2.0f * x / (double)game->screen->width - 1)));
|
||||
pos = game->player->pos;
|
||||
perp_dist = dda(ray_dir, pos, game->map);
|
||||
render.perp_dist = fabs(perp_dist);
|
||||
@ -130,77 +135,81 @@ 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;
|
||||
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)
|
||||
void draw_line(t_game *game, t_render render, int x)
|
||||
{
|
||||
int y;
|
||||
int color;
|
||||
int lineHeight;
|
||||
int drawStart;
|
||||
int texDrawStart;
|
||||
int drawEnd;
|
||||
int y;
|
||||
int color;
|
||||
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++;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void draw_floor(t_game *game)
|
||||
void draw_floor(t_game *game)
|
||||
{
|
||||
int y;
|
||||
int x;
|
||||
int color;
|
||||
double row_dist;
|
||||
t_vec2 floor_step;
|
||||
t_vec2 floor_pos;
|
||||
t_vec2_int floor_cell;
|
||||
t_vec2_int tex_coords;
|
||||
const t_vec2 left_ray = sub(game->player->dir, game->player->camera);
|
||||
const t_vec2 right_ray = add(game->player->dir, game->player->camera);
|
||||
int y;
|
||||
int x;
|
||||
int color;
|
||||
double row_dist;
|
||||
t_vec2 floor_step;
|
||||
t_vec2 floor_pos;
|
||||
t_vec2_int floor_cell;
|
||||
t_vec2_int tex_coords;
|
||||
const t_vec2 left_ray = sub(game->player->dir, game->player->camera);
|
||||
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);
|
||||
@ -212,7 +221,7 @@ void draw_floor(t_game *game)
|
||||
}
|
||||
}
|
||||
|
||||
void cast_rays(t_game *game)
|
||||
void cast_rays(t_game *game)
|
||||
{
|
||||
int x;
|
||||
t_render render;
|
||||
@ -226,4 +235,3 @@ void cast_rays(t_game *game)
|
||||
x++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,64 +1,67 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 */
|
||||
/* :::::::: */
|
||||
/* render.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/15 16:28:10 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/07 11:38:34 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "render.h"
|
||||
#include "MLX42.h"
|
||||
|
||||
int render_check_bounds(t_screen *screen, t_vec2 *point)
|
||||
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)
|
||||
void render_tile(t_screen *screen, int x, int y, t_tile tile)
|
||||
{
|
||||
int i;
|
||||
int xp;
|
||||
int yp;
|
||||
int i;
|
||||
int xp;
|
||||
int yp;
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void render_map(t_screen *screen, t_map *map)
|
||||
void render_map(t_screen *screen, t_map *map)
|
||||
{
|
||||
int i;
|
||||
int x;
|
||||
int y;
|
||||
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++;
|
||||
}
|
||||
}
|
||||
|
||||
void render_entities(t_game *game)
|
||||
void render_entities(t_game *game)
|
||||
{
|
||||
player_render(game->screen, game->player);
|
||||
}
|
||||
|
||||
@ -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++;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
@ -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,27 +26,28 @@ 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, ¤t))
|
||||
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 delta_point;
|
||||
t_vec2 cur;
|
||||
t_vec2 delta_point;
|
||||
|
||||
delta_point.x = abs((int) end.x - (int) start.x);
|
||||
delta_point.y = end.y - start.y;
|
||||
@ -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, ¤t))
|
||||
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)
|
||||
|
||||
@ -1,20 +1,20 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 */
|
||||
/* :::::::: */
|
||||
/* texutre_load.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/05/06 15:45:58 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/07 11:49:37 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "texture.h"
|
||||
|
||||
void texture_delete(t_game *game)
|
||||
void texture_delete(t_game *game)
|
||||
{
|
||||
int i;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < 4)
|
||||
@ -35,7 +35,7 @@ void texture_delete(t_game *game)
|
||||
}
|
||||
}
|
||||
|
||||
int texture_load(t_game *game)
|
||||
int texture_load(t_game *game)
|
||||
{
|
||||
game->map->textures[SIDE_NORTH] = mlx_load_png(game->map->NO_texture);
|
||||
game->map->textures[SIDE_EAST] = mlx_load_png(game->map->EA_texture);
|
||||
|
||||
@ -1,19 +1,19 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 */
|
||||
/* :::::::: */
|
||||
/* errors.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/15 15:40:14 by qmennen #+# #+# */
|
||||
/* 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
|
||||
|
||||
@ -1,18 +1,18 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 */
|
||||
/* :::::::: */
|
||||
/* initialize.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/22 17:08:26 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/07 11:35:18 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cub3d.h"
|
||||
|
||||
int initialize_cub3d(t_game **game, const char *mapfile)
|
||||
int initialize_cub3d(t_game **game, const char *mapfile)
|
||||
{
|
||||
if (!game_create(game))
|
||||
return (FAILURE);
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 */
|
||||
/* :::::::: */
|
||||
/* keyboard.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/17 19:29:29 by qmennen #+# #+# */
|
||||
/* 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)
|
||||
|
||||
2
test.cub
2
test.cub
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user