refactor: floats to doubles
This commit is contained in:
parent
15c47e36ac
commit
06156e5436
@ -6,16 +6,16 @@
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/22 14:40:47 by qmennen #+# #+# */
|
||||
/* Updated: 2025/04/22 14:42:35 by qmennen ### ########.fr */
|
||||
/* Updated: 2025/05/06 15:20:37 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef COLLISION_H
|
||||
# define COLLISION_H
|
||||
#define COLLISION_H
|
||||
|
||||
# include "cub3d.h"
|
||||
#include "cub3d.h"
|
||||
|
||||
int collision_horizontal(t_map *map, t_player *player, float xa);
|
||||
int collision_vertical(t_map *map, t_player *player, float ya);
|
||||
int collision_horizontal(t_map *map, t_player *player, double xa);
|
||||
int collision_vertical(t_map *map, t_player *player, double ya);
|
||||
|
||||
#endif
|
||||
|
||||
12
inc/player.h
12
inc/player.h
@ -6,17 +6,17 @@
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/15 18:53:27 by qmennen #+# #+# */
|
||||
/* Updated: 2025/04/22 15:23:45 by qmennen ### ########.fr */
|
||||
/* Updated: 2025/05/06 15:20:37 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PLAYER_H
|
||||
# define PLAYER_H
|
||||
#define PLAYER_H
|
||||
|
||||
# include "cub3d.h"
|
||||
#include "cub3d.h"
|
||||
|
||||
int player_create(t_game **game);
|
||||
void player_render(t_screen *screen, t_player *player);
|
||||
void player_update(t_game *game, float delta_time);
|
||||
int player_create(t_game **game);
|
||||
void player_render(t_screen *screen, t_player *player);
|
||||
void player_update(t_game *game, double delta_time);
|
||||
|
||||
#endif
|
||||
|
||||
110
inc/types.h
110
inc/types.h
@ -1,19 +1,19 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* types.h :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/06 13:58:16 by whaffman ######## odam.nl */
|
||||
/* ::: :::::::: */
|
||||
/* types.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/06 15:20:37 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef TYPES_H
|
||||
# define TYPES_H
|
||||
#define TYPES_H
|
||||
|
||||
# include "cub3d.h"
|
||||
#include "cub3d.h"
|
||||
|
||||
typedef enum TILE
|
||||
{
|
||||
@ -22,63 +22,63 @@ typedef enum TILE
|
||||
TILE_EMPTY = 0,
|
||||
TILE_WALL = 1,
|
||||
TILE_PLAYER = 2,
|
||||
} t_tile;
|
||||
} t_tile;
|
||||
|
||||
typedef struct s_vec2
|
||||
{
|
||||
float x;
|
||||
float y;
|
||||
} t_vec2;
|
||||
double x;
|
||||
double y;
|
||||
} t_vec2;
|
||||
|
||||
typedef struct s_vec2_int
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
} t_vec2_int;
|
||||
int x;
|
||||
int y;
|
||||
} t_vec2_int;
|
||||
|
||||
typedef struct s_vec2_line
|
||||
{
|
||||
t_vec2 support;
|
||||
t_vec2 dir;
|
||||
} t_vec2_line;
|
||||
t_vec2 support;
|
||||
t_vec2 dir;
|
||||
} t_vec2_line;
|
||||
|
||||
typedef struct s_player
|
||||
{
|
||||
t_vec2 pos;
|
||||
t_vec2 dir;
|
||||
t_vec2 camera;
|
||||
float speed;
|
||||
float fov;
|
||||
} t_player;
|
||||
t_vec2 pos;
|
||||
t_vec2 dir;
|
||||
t_vec2 camera;
|
||||
double speed;
|
||||
double fov;
|
||||
} t_player;
|
||||
|
||||
typedef struct s_map
|
||||
{
|
||||
unsigned int width;
|
||||
unsigned int height;
|
||||
t_tile **grid;
|
||||
char *NO_texture;
|
||||
char *SO_texture;
|
||||
char *WE_texture;
|
||||
char *EA_texture;
|
||||
unsigned int floor_color;
|
||||
unsigned int ceiling_color;
|
||||
} t_map;
|
||||
unsigned int width;
|
||||
unsigned int height;
|
||||
t_tile **grid;
|
||||
char *NO_texture;
|
||||
char *SO_texture;
|
||||
char *WE_texture;
|
||||
char *EA_texture;
|
||||
unsigned int floor_color;
|
||||
unsigned int ceiling_color;
|
||||
} t_map;
|
||||
|
||||
typedef struct s_keyboard
|
||||
{
|
||||
int keys[NUM_KEYS];
|
||||
int last_keys[NUM_KEYS];
|
||||
} t_keyboard;
|
||||
int keys[NUM_KEYS];
|
||||
int last_keys[NUM_KEYS];
|
||||
} t_keyboard;
|
||||
|
||||
typedef struct s_screen
|
||||
{
|
||||
mlx_t *mlx;
|
||||
mlx_image_t *img;
|
||||
mlx_image_t *minimap;
|
||||
mlx_image_t *background;
|
||||
unsigned int width;
|
||||
unsigned int height;
|
||||
} t_screen;
|
||||
mlx_t *mlx;
|
||||
mlx_image_t *img;
|
||||
mlx_image_t *minimap;
|
||||
mlx_image_t *background;
|
||||
unsigned int width;
|
||||
unsigned int height;
|
||||
} t_screen;
|
||||
|
||||
typedef enum e_side
|
||||
{
|
||||
@ -86,21 +86,21 @@ typedef enum e_side
|
||||
SIDE_SOUTH,
|
||||
SIDE_WEST,
|
||||
SIDE_EAST,
|
||||
} t_side;
|
||||
} t_side;
|
||||
|
||||
typedef struct s_render
|
||||
{
|
||||
float perp_dist;
|
||||
t_side side;
|
||||
float wall_x;
|
||||
} t_render;
|
||||
double perp_dist;
|
||||
t_side side;
|
||||
double wall_x;
|
||||
} t_render;
|
||||
|
||||
typedef struct s_game
|
||||
{
|
||||
t_map *map;
|
||||
t_player *player;
|
||||
t_screen *screen;
|
||||
t_keyboard *keyboard;
|
||||
} t_game;
|
||||
t_map *map;
|
||||
t_player *player;
|
||||
t_screen *screen;
|
||||
t_keyboard *keyboard;
|
||||
} t_game;
|
||||
|
||||
#endif
|
||||
|
||||
@ -1,54 +1,53 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* vec_math.h :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/25 10:11:44 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/25 10:44:33 by whaffman ######## odam.nl */
|
||||
/* ::: :::::::: */
|
||||
/* vec_math.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/25 10:11:44 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/06 15:20:37 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef VEC_MATH_H
|
||||
# define VEC_MATH_H
|
||||
# include "types.h"
|
||||
|
||||
#define VEC_MATH_H
|
||||
#include "types.h"
|
||||
|
||||
/**
|
||||
* @brief Calculates the distance between two 2D vectors.
|
||||
*
|
||||
* @param a The first 2D vector.
|
||||
* @param b The second 2D vector.
|
||||
* @return The distance between vector a and vector b as a float.
|
||||
* @return The distance between vector a and vector b as a double.
|
||||
*/
|
||||
float dist(t_vec2 a, t_vec2 b);
|
||||
double dist(t_vec2 a, t_vec2 b);
|
||||
|
||||
/**
|
||||
* @brief Calculates the norm (length) of a 2D vector.
|
||||
*
|
||||
* @param a The 2D vector.
|
||||
* @return The norm of vector a as a float.
|
||||
* @return The norm of vector a as a double.
|
||||
*/
|
||||
float norm(t_vec2 a);
|
||||
double norm(t_vec2 a);
|
||||
|
||||
/**
|
||||
* @brief Calculates the dot product of two 2D vectors.
|
||||
*
|
||||
* @param a The first 2D vector.
|
||||
* @param b The second 2D vector.
|
||||
* @return The dot product of vector a and vector b as a float.
|
||||
* @return The dot product of vector a and vector b as a double.
|
||||
*/
|
||||
float dot(t_vec2 a, t_vec2 b);
|
||||
double dot(t_vec2 a, t_vec2 b);
|
||||
|
||||
/**
|
||||
* @brief Calculates the distance from a point to a line.
|
||||
*
|
||||
* @param point The point to calculate the distance from.
|
||||
* @param line The line defined by a (support)point and a direction.
|
||||
* @return The distance from the point to the line as a float.
|
||||
* @return The distance from the point to the line as a double.
|
||||
*/
|
||||
float dist_point_line(t_vec2 point, t_vec2_line line);
|
||||
double dist_point_line(t_vec2 point, t_vec2_line line);
|
||||
|
||||
/**
|
||||
* @brief Adds two 2D vectors.
|
||||
@ -57,7 +56,7 @@ float dist_point_line(t_vec2 point, t_vec2_line line);
|
||||
* @param b The second 2D vector.
|
||||
* @return The sum of vector a and vector b as a new 2D vector.
|
||||
*/
|
||||
t_vec2 add(t_vec2 a, t_vec2 b);
|
||||
t_vec2 add(t_vec2 a, t_vec2 b);
|
||||
|
||||
/**
|
||||
* @brief Subtracts one 2D vector from another.
|
||||
@ -66,7 +65,7 @@ t_vec2 add(t_vec2 a, t_vec2 b);
|
||||
* @param b The second 2D vector to subtract from the first.
|
||||
* @return The result of subtracting vector b from vector a as a new 2D vector.
|
||||
*/
|
||||
t_vec2 sub(t_vec2 a, t_vec2 b);
|
||||
t_vec2 sub(t_vec2 a, t_vec2 b);
|
||||
|
||||
/**
|
||||
* @brief Multiplies a 2D vector by a scalar.
|
||||
@ -75,7 +74,7 @@ t_vec2 sub(t_vec2 a, t_vec2 b);
|
||||
* @param b The scalar to multiply the vector by.
|
||||
* @return The result of multiplying vector a by scalar b as a new 2D vector.
|
||||
*/
|
||||
t_vec2 mul(t_vec2 a, float b);
|
||||
t_vec2 mul(t_vec2 a, double b);
|
||||
|
||||
/**
|
||||
* @brief Rotates a 2D vector by a given angle.
|
||||
@ -84,7 +83,7 @@ t_vec2 mul(t_vec2 a, float b);
|
||||
* @param angle The angle in radians to rotate the vector by.
|
||||
* @return The rotated vector as a new 2D vector.
|
||||
*/
|
||||
t_vec2 rot(t_vec2 a, float angle);
|
||||
t_vec2 rot(t_vec2 a, double angle);
|
||||
|
||||
/**
|
||||
* @brief Calculates the perpendicular vector of a 2D vector.
|
||||
@ -92,6 +91,6 @@ t_vec2 rot(t_vec2 a, float angle);
|
||||
* @param a The 2D vector to calculate the perpendicular of.
|
||||
* @return The perpendicular vector as a new 2D vector.
|
||||
*/
|
||||
t_vec2 perp(t_vec2 a);
|
||||
t_vec2 perp(t_vec2 a);
|
||||
|
||||
#endif // VEC_MATH_H
|
||||
@ -1,28 +1,28 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* collision.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/22 14:40:59 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/06 14:18:07 by whaffman ######## odam.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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "collision.h"
|
||||
|
||||
int collision_horizontal(t_map *map, t_player *player, float 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, float 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: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/06 14:18:50 by whaffman ######## odam.nl */
|
||||
/* ::: :::::::: */
|
||||
/* game.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#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;
|
||||
float 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);
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* dist.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/25 10:07:59 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/25 10:39:59 by whaffman ######## odam.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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,7 +14,7 @@
|
||||
#include "math.h"
|
||||
#include "vec_math.h"
|
||||
|
||||
float 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: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/25 10:09:45 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/25 10:46:06 by whaffman ######## odam.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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,10 +14,10 @@
|
||||
#include "math.h"
|
||||
#include "vec_math.h"
|
||||
|
||||
float dist_point_line(t_vec2 point, t_vec2_line line)
|
||||
double dist_point_line(t_vec2 point, t_vec2_line line)
|
||||
{
|
||||
float 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: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/25 09:58:29 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/25 10:40:02 by whaffman ######## odam.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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,7 +14,7 @@
|
||||
#include "math.h"
|
||||
#include "vec_math.h"
|
||||
|
||||
float 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: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/25 09:56:39 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/25 10:40:05 by whaffman ######## odam.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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,9 +14,9 @@
|
||||
#include "math.h"
|
||||
#include "vec_math.h"
|
||||
|
||||
t_vec2 mul(t_vec2 a, float 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: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/25 09:59:13 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/25 10:40:13 by whaffman ######## odam.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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,7 +14,7 @@
|
||||
#include "math.h"
|
||||
#include "vec_math.h"
|
||||
|
||||
float norm(t_vec2 a)
|
||||
double norm(t_vec2 a)
|
||||
{
|
||||
return (sqrtf(a.x * a.x + a.y * a.y));
|
||||
}
|
||||
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* rot.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/25 10:35:58 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/25 10:40:23 by whaffman ######## odam.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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,11 +14,11 @@
|
||||
#include "math.h"
|
||||
#include "vec_math.h"
|
||||
|
||||
t_vec2 rot(t_vec2 a, float angle)
|
||||
t_vec2 rot(t_vec2 a, double angle)
|
||||
{
|
||||
t_vec2 result;
|
||||
float cos_angle;
|
||||
float sin_angle;
|
||||
t_vec2 result;
|
||||
double cos_angle;
|
||||
double sin_angle;
|
||||
|
||||
cos_angle = cosf(angle);
|
||||
sin_angle = sinf(angle);
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* parse_map.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/22 13:12:04 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/04 15:07:44 by whaffman ######## odam.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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -15,20 +15,20 @@
|
||||
|
||||
t_vec2 parse_dir(int x, int y)
|
||||
{
|
||||
t_vec2 dir;
|
||||
t_vec2 dir;
|
||||
|
||||
dir.x = (float)x;
|
||||
dir.y = (float)y;
|
||||
dir.x = (double)x;
|
||||
dir.y = (double)y;
|
||||
return (dir);
|
||||
}
|
||||
|
||||
int parse_player(t_game *game, int y, int x, char c)
|
||||
{
|
||||
t_player *player;
|
||||
t_player *player;
|
||||
|
||||
player = game->player;
|
||||
player->pos.x = ((float)x + 0.5f);
|
||||
player->pos.y = ((float)y + 0.5f);
|
||||
player->pos.x = ((double)x + 0.5f);
|
||||
player->pos.y = ((double)y + 0.5f);
|
||||
|
||||
if (c == 'N')
|
||||
player->dir = parse_dir(0, -1);
|
||||
@ -43,10 +43,10 @@ int parse_player(t_game *game, int y, int x, char c)
|
||||
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));
|
||||
@ -70,10 +70,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])
|
||||
|
||||
45
src/player.c
45
src/player.c
@ -1,20 +1,20 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* player.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/15 18:53:19 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/06 14:20:37 by whaffman ######## odam.nl */
|
||||
/* ::: :::::::: */
|
||||
/* player.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/15 18:53:19 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#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)
|
||||
@ -31,10 +31,10 @@ int player_create(t_game **game)
|
||||
return (SUCCESS);
|
||||
}
|
||||
|
||||
static void move(t_map *map, t_player *player, int dir, float delta)
|
||||
static void move(t_map *map, t_player *player, int dir, double delta)
|
||||
{
|
||||
float xa;
|
||||
float ya;
|
||||
double xa;
|
||||
double ya;
|
||||
|
||||
xa = dir * player->dir.x * player->speed * delta;
|
||||
ya = dir * player->dir.y * player->speed * delta;
|
||||
@ -44,10 +44,10 @@ static void move(t_map *map, t_player *player, int dir, float delta)
|
||||
player->pos.y += ya;
|
||||
}
|
||||
|
||||
static void strave(t_map *map, t_player *player, int dir, float delta)
|
||||
static void strave(t_map *map, t_player *player, int dir, double delta)
|
||||
{
|
||||
float xa;
|
||||
float ya;
|
||||
double xa;
|
||||
double ya;
|
||||
|
||||
xa = dir * perp(player->dir).x * player->speed * delta;
|
||||
ya = dir * perp(player->dir).y * player->speed * delta;
|
||||
@ -57,13 +57,13 @@ static void strave(t_map *map, t_player *player, int dir, float delta)
|
||||
player->pos.y += ya;
|
||||
}
|
||||
|
||||
static void rotate(t_player *player, float 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, float 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);
|
||||
@ -79,15 +79,12 @@ void player_update(t_game *game, float 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;
|
||||
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: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/05/02 11:58:09 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/06 14:25:00 by whaffman ######## odam.nl */
|
||||
/* ::: :::::::: */
|
||||
/* DDAscratch.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/02 11:58:09 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/06 15:21:22 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,46 +14,49 @@
|
||||
#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;
|
||||
|
||||
delta_dist.x = (ray_dir.x == 0) * 1e30
|
||||
+ (ray_dir.x != 0) * fabs(1 / ray_dir.x);
|
||||
delta_dist.y = (ray_dir.y == 0) * 1e30
|
||||
+ (ray_dir.y != 0) * fabs(1 / ray_dir.y);
|
||||
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
|
||||
delta_dist.y = fabs(1 / ray_dir.y);
|
||||
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)
|
||||
@ -63,8 +66,7 @@ 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);
|
||||
}
|
||||
@ -74,22 +76,21 @@ 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
|
||||
float 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, float perp_dist)
|
||||
t_side get_side(t_vec2 ray_dir, double perp_dist)
|
||||
{
|
||||
if (perp_dist > 0)
|
||||
{
|
||||
@ -107,49 +108,45 @@ t_side get_side(t_vec2 ray_dir, float 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;
|
||||
float 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 / (float)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);
|
||||
render.side = get_side(ray_dir, perp_dist);
|
||||
render.wall_x = (perp_dist > 0) * (pos.x + ray_dir.x * perp_dist)
|
||||
+ (perp_dist <= 0) * (pos.y + ray_dir.y * perp_dist);
|
||||
render.wall_x = (perp_dist > 0) * (pos.x + ray_dir.x * perp_dist) + (perp_dist <= 0) * (pos.y + ray_dir.y * perp_dist);
|
||||
render.wall_x -= floor(render.wall_x);
|
||||
return (render);
|
||||
}
|
||||
|
||||
unsigned int get_color(t_render render)
|
||||
unsigned int get_color(t_render render)
|
||||
{
|
||||
float dist;
|
||||
const unsigned int color[4] = {
|
||||
double dist;
|
||||
const unsigned int color[4] = {
|
||||
0x488B49,
|
||||
0x4AAD52,
|
||||
0x6EB257,
|
||||
0xC5E063
|
||||
};
|
||||
0xC5E063};
|
||||
|
||||
dist = (render.perp_dist == 0) * 1e30
|
||||
+ (render.perp_dist > 1) * render.perp_dist
|
||||
+ (render.perp_dist <= 1) * 1;
|
||||
return (color[render.side] << 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 (color[render.side] << 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 drawEnd;
|
||||
int y;
|
||||
int color;
|
||||
int lineHeight;
|
||||
int drawStart;
|
||||
int drawEnd;
|
||||
|
||||
y = 0;
|
||||
lineHeight = (int)(game->screen->height / render.perp_dist);
|
||||
@ -162,11 +159,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
|
||||
| (int)fabs(2 * y * 0xFF / (float)game->screen->height - 0xFF);
|
||||
color = game->map->ceiling_color << 8 | (int)fabs(2 * y * 0xFF / (double)game->screen->height - 0xFF);
|
||||
else if (y > drawEnd)
|
||||
color = game->map->floor_color << 8
|
||||
| (int)fabs(2 * y * 0xFF / (float)game->screen->height - 0xFF);
|
||||
color = game->map->floor_color << 8 | (int)fabs(2 * y * 0xFF / (double)game->screen->height - 0xFF);
|
||||
else
|
||||
color = get_color(render);
|
||||
mlx_put_pixel(game->screen->img, x, y, color);
|
||||
@ -174,10 +169,10 @@ void draw_line(t_game *game, t_render render, int x)
|
||||
}
|
||||
}
|
||||
|
||||
void cast_rays(t_game *game)
|
||||
void cast_rays(t_game *game)
|
||||
{
|
||||
int x;
|
||||
t_render render;
|
||||
int x;
|
||||
t_render render;
|
||||
|
||||
x = 0;
|
||||
while (x < game->screen->width)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user