Skip to content

Commit

Permalink
Merge pull request #16 from ys-zm/raycaster
Browse files Browse the repository at this point in the history
Raycaster
  • Loading branch information
JopjeKnopje authored Jan 2, 2024
2 parents a439c12 + 1b7d149 commit b24ff23
Show file tree
Hide file tree
Showing 39 changed files with 891 additions and 760 deletions.
2 changes: 1 addition & 1 deletion MLX42/include/MLX42/MLX42_Int.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: W2Wizard <[email protected]> +#+ */
/* +#+ */
/* Created: 2021/12/27 23:55:34 by W2Wizard #+# #+# */
/* Updated: 2023/11/10 03:22:56 by joppe ######## odam.nl */
/* Updated: 2023/12/21 01:39:28 by joppe ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down
32 changes: 10 additions & 22 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: #
# +:+ +:+ +:+ #
# By: jboeve <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/08/22 13:32:22 by jboeve #+# #+# #
# Updated: 2023/12/13 16:25:01 by jboeve ######## odam.nl #
# #
# **************************************************************************** #

######################
# OS Dependent flags #
######################
Expand All @@ -23,7 +11,7 @@ else ifeq ($(shell uname -m),x86_64)
endif

NAME := app
RUN_CMD := ./$(NAME) test_maps/simple.cub
RUN_CMD := ./$(NAME) test_maps/valid.cub

# CFLAGS += -Wall -Wextra -Werror
CFLAGS += -Wall -Wextra
Expand All @@ -49,17 +37,17 @@ SRCS = parser/check_elements.c \
parser/utils_one.c \
parser/utils_two.c \
utils/error.c \
utils/free.c \
utils/free.c \
utils/vec2d_utils.c \
utils/colors.c \
game/game.c \
game/draw.c \
game/keys.c \
game/player.c \
game/raycaster.c \
game/render.c \
cub3d.c \
game.c \
input.c \
render.c \
draw.c \
player.c \
utils.c \
test_utils.c \
map.c \
vector.c \
timer.c

HEADER_DIR := include
Expand Down
173 changes: 58 additions & 115 deletions include/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jboeve <[email protected]> +#+ */
/* +#+ */
/* Created: 2023/11/01 20:07:37 by jboeve #+# #+# */
/* Updated: 2023/12/13 16:24:24 by jboeve ######## odam.nl */
/* Updated: 2024/01/02 19:51:20 by joppe ######## odam.nl */
/* */
/* ************************************************************************** */

Expand All @@ -22,11 +22,11 @@
#include <unistd.h>
#include <fcntl.h>
#include <stdbool.h>
#include <limits.h>

#include "timer.h"
#include "vector.h"
#include "libft.h"
#include "parser.h"
#include "vector.h"
#include "get_next_line.h"
#include "MLX42/MLX42.h"

Expand All @@ -45,43 +45,44 @@
#define PI 3.1415926535

// Window settings
#define WINDOW_WIDTH 1920
#define WINDOW_HEIGHT 1080
#define WINDOW_TITLE "Gammoe"
// #define WINDOW_WIDTH 1920
// #define WINDOW_HEIGHT 1080

#define PLAYER_VIEWPORT_X 720
#define PLAYER_VIEWPORT_Y 512
#define PLAYER_VIEWPORT_WALL_WIDTH 8
#define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 720

#define WINDOW_TITLE "Gammoe"

#define TICK_RATE (1.0 / 60.0)

// TODO Move all this stuff to some kind of game.h
#define CELL_WIDTH 64
#define CELL_HEIGHT 64
#define CELL_SIZE 64

#define PLAYER_WIDTH 16
#define PLAYER_HEIGHT 16
#define PLAYER_RAY_COUNT 90
#define PLAYER_WALK_SPEED 15
#define PLAYER_ROTATE_SPEED 5
#define PLAYER_MOVE_SPEED 5.0
#define PLAYER_RUN_MODIFIER 2.5
#define PLAYER_ROTATE_SPEED 5.0
#define PLAYER_ROTATE_MODIFIER 2.5


#define PLAYER_MOV_SPEED 0.08

#define COLOR_BACKGROUND 0x111111FF
#define COLOR_PLAYER 0xFFFFFFFF

#define VEC_X 0
#define VEC_Y 1

// #define FOV 0.66
#define FOV 0.80

typedef bool (t_ray_hitfunc) (void *p, uint32_t x, uint32_t y);
typedef struct s_meta t_meta;

typedef enum e_cell_type {
MAP_EMPTY,
MAP_WALL,
MAP_SPACE,
} t_cell_type;



typedef union s_rgba
{
uint32_t value;
Expand All @@ -94,30 +95,35 @@ typedef union s_rgba
};
} t_rgba;


typedef enum e_side {
HIT_NONE,
HIT_NS,
HIT_EW,
} t_side;

typedef struct s_ray {
t_vec2f start;
t_vec2f end;
t_side hit_side;
double length;
t_vec2d direction;
} t_ray;

typedef struct s_meta t_meta;

// NOTE: Maybe switch to double instead of float?
typedef struct s_player {
t_meta *meta;
// TODO Have a map_position which will be the position relative to the leftmost square.
// Based on that position we can just `position / CELL_WIDTH` to find the cell position.
t_vec2i map_cell;
t_vec2f position;
t_vec2f direction;
t_vec2f beam;
t_ray rays[PLAYER_RAY_COUNT];
float angle_rad;
t_meta *meta;
t_ray rays[WINDOW_WIDTH];
t_vec2d cam_plane;
t_vec2d position;
t_vec2d direction;
} t_player;


typedef struct s_map {
t_cell_type *level;
uint32_t width;
uint32_t height;
t_vec2u player_start;
char player_start_dir;
} t_map;

typedef struct s_tex {
Expand All @@ -134,13 +140,14 @@ typedef struct s_meta {
mlx_image_t *image;
t_timer update_timer;
t_timer fps_timer;
t_player player;
t_player player;
uint32_t fps;
t_map map;
t_tex tex;
char *map_file;
} t_meta;


// cub3d.c
int cub3d(int argc, char *argv[]);

Expand All @@ -149,109 +156,45 @@ void game_init(t_meta *meta);
void game_loop(void* param);

// player.c
void player_move(t_player *p, t_vec2f transform);
void player_look(t_player *p, double angle);
void player_move(t_player *p, t_vec2d transform);
void player_turn(t_player *p, float radiant);
void player_raycast(t_player *p);

// input.c
void key_hook(mlx_key_data_t keydata, void* param);
void cursor_hook(double xpos, double ypos, void* param);
// keys.c
void mouse_hook(double xpos, double ypos, void* param);
void keys_handle(t_meta *meta, double time_delta);

// render.c
t_vec2i render_get_draw_offset();
void render_player_view(mlx_image_t *image, t_player *p);
void render_player(mlx_image_t *image, t_player *p);
void render_player_viewport(mlx_image_t *image, t_player *p);
void render_clear_bg(mlx_image_t *image);
void render_map_grid(mlx_image_t *image, t_map *m);

// map.c
t_cell_type map_get_cell_type(t_map *m, t_vec2f pos);
void render_minimap(mlx_image_t *image, t_map *m);

// draw.c
void draw_rect(mlx_image_t* image, uint32_t x_pos, uint32_t y_pos, uint32_t width, uint32_t height, uint32_t color);
void draw_line(mlx_image_t *image, t_vec2i start, t_vec2i end, t_rgba c);
void draw_put_pixel(mlx_image_t* image, uint32_t x, uint32_t y, uint32_t color);

// utils.c
float deg_to_rad(float deg);

// test_utils.c
void print_vec2f(const char *s, t_vec2f vec);
void print_vec2i(const char *s, t_vec2i vec);
void print_cell(t_cell_type cell);
void game_init(t_meta *meta);
void game_loop(void* param);

// keys.c
void keyhook(mlx_key_data_t keydata, void* param);
void keys_update(mlx_key_data_t keydata, void *param);

// draw.c
void draw_square(mlx_image_t* image, uint32_t x_pos, uint32_t y_pos, uint32_t width, uint32_t height, uint32_t color);
void cube_put_pixel(mlx_image_t* image, uint32_t x, uint32_t y, uint32_t color);

// check_map.c
int check_map(t_meta *meta, char *rect);
int find_index(t_meta *meta, uint32_t x, uint32_t y);

// free.c
void meta_free(t_meta *meta);

// PARSER

// parser.c
char *read_file(int fd);
int map_ext(char *file);
int parser(t_meta *meta, char *map_file);

// parse_map.c
bool is_map_line(char *file);
int input_map(t_meta *meta, char *file);

// parse_elements.c
int input_texture(t_tex *tex, char *file);
int input_colour(t_tex *tex, char *file);
int save_elements(t_tex *tex, char *file);
int parse_elements(t_meta *meta, char *file);

// check_colors.c
bool valid_rgb_value(char *file);
bool is_valid_color(char *file);
bool colors_valid(char *file);

// check_elements.c
bool is_valid_element(char *file);
bool only_spaces(char *file);
bool is_map_element(char *file);
bool elements_order(char *file);
bool check_missing(int *found);
bool is_missing(char *file);
bool is_duplicate(char *file);

// check_map.c
bool is_map_chars_valid(char *map);
int flood_fill(t_meta *meta, char *map, int x, int y);
bool save_start_pos(t_meta *meta, char *map);
bool is_floor_exposed(t_meta *meta, char *map);

// parse_textures.c
void get_colour_value(char *file, t_rgba *col);
char *get_tex_val(char *file);
bool is_texture(char *file);
bool is_colour(char *file);

// utils_one.c
void skip_line(char **file);
void skip_spaces(char **file);
void skip_digits(char **file);
int valid_map_char(char c);
int player_pos_char(char c);

// utils_two.c
uint32_t find_width(char *map);
uint32_t find_height(char *map);
char *make_rect(char *map, uint32_t w, uint32_t h);
// raycaster.c
t_ray raycaster_cast(t_meta *meta, t_vec2d pp, t_vec2d dir, t_ray_hitfunc hit);

// test_utils.c
void print_map(char *map, uint32_t w, uint32_t h);
void print_map(char *map, uint32_t w, uint32_t h);

// colors.c
int32_t set_color(int32_t r, int32_t g, int32_t b, int32_t a);
int32_t find_wall_color(t_side side);

// free.c
void meta_free(t_meta *meta);

#endif
Loading

0 comments on commit b24ff23

Please sign in to comment.