diff --git a/include/meta.h b/include/meta.h index 9482e89..1814502 100644 --- a/include/meta.h +++ b/include/meta.h @@ -6,7 +6,7 @@ /* By: jboeve +#+ */ /* +#+ */ /* Created: 2023/11/01 20:07:37 by jboeve #+# #+# */ -/* Updated: 2023/11/13 21:19:38 by joppe ######## odam.nl */ +/* Updated: 2023/11/18 20:45:26 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -45,10 +45,14 @@ #define PI 3.1415926535 // Window settings -#define WINDOW_WIDTH 1024 -#define WINDOW_HEIGHT 720 +#define WINDOW_WIDTH 1920 +#define WINDOW_HEIGHT 1080 #define WINDOW_TITLE "Gammoe" +#define PLAYER_VIEWPORT_X 720 +#define PLAYER_VIEWPORT_Y 512 +#define PLAYER_VIEWPORT_WALL_WIDTH 8 + #define TICK_RATE (1.0 / 60.0) // TODO Move all this stuff to some kind of game.h @@ -57,7 +61,7 @@ #define PLAYER_WIDTH 16 #define PLAYER_HEIGHT 16 -#define PLAYER_RAY_COUNT 360 +#define PLAYER_RAY_COUNT 90 #define PLAYER_WALK_SPEED 15 #define PLAYER_ROTATE_SPEED 5 #define PLAYER_RUN_MODIFIER 2.5 @@ -99,10 +103,15 @@ typedef struct s_meta t_meta; // NOTE: Maybe switch to double instead of float? typedef struct s_player { - uint32_t x; - uint32_t y; - uint32_t start_x; - uint32_t start_y; + 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_player; typedef struct s_map { @@ -136,6 +145,40 @@ typedef struct s_meta { int cub3d(int argc, char *argv[]); // game.c +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_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); + +// 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_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); + +// 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); diff --git a/include/vector.h b/include/vector.h index da91eea..85205b8 100644 --- a/include/vector.h +++ b/include/vector.h @@ -6,7 +6,7 @@ /* By: joppe +#+ */ /* +#+ */ /* Created: 2023/11/11 03:51:14 by joppe #+# #+# */ -/* Updated: 2023/11/11 03:58:12 by joppe ######## odam.nl */ +/* Updated: 2023/11/18 13:21:55 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ #define VECTOR_H #include +#include #define VEC_X 0 diff --git a/src/.render.c.swp b/src/.render.c.swp new file mode 100644 index 0000000..7236d4c Binary files /dev/null and b/src/.render.c.swp differ diff --git a/src/draw.c b/src/draw.c index ba912ed..760c5cf 100644 --- a/src/draw.c +++ b/src/draw.c @@ -15,16 +15,16 @@ #include "meta.h" // TODO Struct that contains all this info because well. tHe nORm -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 draw_rect(mlx_image_t* image, uint32_t x_pos, uint32_t y_pos, uint32_t width, uint32_t height, uint32_t color) { size_t x; size_t y; y = 0; - while (y < width) + while (y < height) { x = 0; - while (x < height) + while (x < width) { draw_put_pixel(image, x_pos + x, y_pos + y, color); x++; diff --git a/src/game.c b/src/game.c index 5f0203e..e35f28a 100644 --- a/src/game.c +++ b/src/game.c @@ -6,7 +6,7 @@ /* By: joppe +#+ */ /* +#+ */ /* Created: 2023/11/08 22:35:05 by joppe #+# #+# */ -/* Updated: 2023/11/09 17:56:57 by yzaim ### ########.fr */ +/* Updated: 2023/11/18 20:39:45 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -62,9 +62,6 @@ void game_init(t_meta *meta) p->position[VEC_X] = (float) meta->map.width * CELL_WIDTH / 2; p->position[VEC_Y] = (float) meta->map.height * CELL_WIDTH/ 2; - // p->position[VEC_X] = 0; - // p->position[VEC_Y] = 0; - player_look(p, deg_to_rad(180.0f)); } @@ -84,9 +81,9 @@ static void game_update(t_meta *meta, double time_delta) player_move(p, -move * p->direction); if (mlx_is_key_down(meta->mlx, MLX_KEY_A)) - player_move(p, vec2f_normalize(vec2f_rotate2d(p->angle + (3 * PI / 2))) * move); + player_move(p, vec2f_normalize(vec2f_rotate2d(p->angle_rad + (3 * PI / 2))) * move); if (mlx_is_key_down(meta->mlx, MLX_KEY_D)) - player_move(p, vec2f_normalize(vec2f_rotate2d(p->angle + (PI / 2))) * move); + player_move(p, vec2f_normalize(vec2f_rotate2d(p->angle_rad + (PI / 2))) * move); if (mlx_is_key_down(meta->mlx, MLX_KEY_Q)) player_look(p, -rotate); @@ -118,5 +115,6 @@ void game_loop(void* param) render_clear_bg(meta->image); render_map_grid(meta->image, &meta->map); + render_player_view(meta->image, &meta->player); render_player(meta->image, &meta->player); } diff --git a/src/main.c b/src/main.c index 4007715..a5ac7e0 100644 --- a/src/main.c +++ b/src/main.c @@ -6,11 +6,13 @@ /* By: jboeve +#+ */ /* +#+ */ /* Created: 2023/11/07 15:28:54 by jboeve #+# #+# */ -/* Updated: 2023/11/09 17:57:13 by yzaim ### ########.fr */ +/* Updated: 2023/11/18 20:55:05 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ #include "meta.h" +#include "vector.h" +#include int main(int argc, char *argv[]) { diff --git a/src/player.c b/src/player.c index 8a10b7e..1455aa1 100644 --- a/src/player.c +++ b/src/player.c @@ -6,7 +6,7 @@ /* By: joppe +#+ */ /* +#+ */ /* Created: 2023/11/10 02:25:34 by joppe #+# #+# */ -/* Updated: 2023/11/13 21:31:05 by joppe ######## odam.nl */ +/* Updated: 2023/11/18 20:45:45 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -30,11 +30,11 @@ void player_look(t_player *p, double angle) { const uint32_t len = 50; - p->angle = fmod(p->angle + angle, 2 * PI); - if (p->angle < 0) - p->angle += 2 * PI; + p->angle_rad = fmod(p->angle_rad + angle, 2 * PI); + if (p->angle_rad < 0) + p->angle_rad += 2 * PI; - p->direction = vec2f_normalize(vec2f_rotate2d(p->angle)); + p->direction = vec2f_normalize(vec2f_rotate2d(p->angle_rad)); p->beam = p->position + p->direction * (t_vec2f) {len, len}; player_raycast(p); @@ -72,8 +72,8 @@ void player_raycast(t_player *p) i = 0; while (i < PLAYER_RAY_COUNT) { - dir = vec2f_normalize(vec2f_rotate2d(p->angle + deg_to_rad(i))); - p->rays[i] = raycast(&p->meta->map, p->position, p->direction * dir, depth); + dir = vec2f_normalize(vec2f_rotate2d(p->angle_rad + deg_to_rad(i) - (deg_to_rad(PLAYER_RAY_COUNT) / 2))); + p->rays[i] = raycast(&p->meta->map, p->position, dir, depth); i++; } } diff --git a/src/prog.c b/src/prog.c new file mode 100644 index 0000000..eec57fa --- /dev/null +++ b/src/prog.c @@ -0,0 +1,16 @@ +#include "enemy.h" +#include +#include +#include + +typedef void enemy; + +enemy_t *enemy_init(const char *name); +void enemy_die(void *ptr); + + +//int main() +//{ +// enemy *e = enemy_init("sjon"); +// enemy_die(e); +//} diff --git a/src/raycaster.c b/src/raycaster.c new file mode 100644 index 0000000..1e92a5d --- /dev/null +++ b/src/raycaster.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* raycaster.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yzaim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 17:58:20 by yzaim #+# #+# */ +/* Updated: 2023/11/09 18:05:01 by yzaim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "meta.h" + +void raycaster(t_meta *meta) +{ + int x; + + x = -1; + while (++x < MAP_WIDTH) + { + //search the intersections with x-lines + } +} diff --git a/src/render.c b/src/render.c index f9897d3..7eb6932 100644 --- a/src/render.c +++ b/src/render.c @@ -6,7 +6,7 @@ /* By: joppe +#+ */ /* +#+ */ /* Created: 2023/11/08 23:14:20 by joppe #+# #+# */ -/* Updated: 2023/11/09 17:55:01 by yzaim ### ########.fr */ +/* Updated: 2023/11/18 20:56:34 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -35,20 +35,36 @@ const t_rgba CELL_COLORS[] = { t_vec2i render_get_draw_offset() { return (DRAW_OFFSET); -} +} :::::::: */ +4 +/* render.c :+: :+: :+: */ +5 +/* +:+ */ +6 +/* By: joppe +#+ */ +7 +/* +#+ */ +8 +/* Created: 2023/11/08 23:14:20 by joppe #+# #+# */ +9 +/* Updated: 2023/11/18 20:56:34 by jboeve ######## odam.nl */ +10 +/* */ +11 +/* ************************************************************************** */ void draw_cell(mlx_image_t *image, t_map *m, uint32_t cell_x, uint32_t cell_y) { - const size_t x_offset = (cell_x * CELL_WIDTH) + cell_x + DRAW_OFFSET[VEC_X]; - const size_t y_offset = (cell_y * CELL_HEIGHT) + cell_y + DRAW_OFFSET[VEC_Y]; + // const size_t x_offset = (cell_x * CELL_WIDTH) + cell_x + DRAW_OFFSET[VEC_X]; + // const size_t y_offset = (cell_y * CELL_HEIGHT) + cell_y + DRAW_OFFSET[VEC_Y]; - // const size_t x_offset = (cell_x * CELL_WIDTH) + DRAW_OFFSET[VEC_X]; - // const size_t y_offset = (cell_y * CELL_HEIGHT) + DRAW_OFFSET[VEC_Y]; + const size_t x_offset = (cell_x * CELL_WIDTH) + DRAW_OFFSET[VEC_X]; + const size_t y_offset = (cell_y * CELL_HEIGHT) + DRAW_OFFSET[VEC_Y]; const t_cell_type cell = (m->level[(cell_y * m->width) + cell_x]); - draw_square(image, x_offset, y_offset, CELL_WIDTH, CELL_HEIGHT, CELL_COLORS[cell].value); + draw_rect(image, x_offset, y_offset, CELL_WIDTH, CELL_HEIGHT, CELL_COLORS[cell].value); } // The player is essentially just a single point/pixel, around which we draw a square with the "player point" in its center. @@ -59,20 +75,42 @@ void render_player(mlx_image_t *image, t_player *p) draw_pos[VEC_Y] -= ((float) PLAYER_HEIGHT / 2); // Draw the player square. - draw_square(image, draw_pos[VEC_X], draw_pos[VEC_Y], + draw_rect(image, draw_pos[VEC_X], draw_pos[VEC_Y], PLAYER_WIDTH, PLAYER_HEIGHT, COLOR_PLAYER); + size_t i = 0; + while (i < PLAYER_RAY_COUNT) + { + draw_line(image, vec2f_to_vec2i(p->rays[i].start) + DRAW_OFFSET, + vec2f_to_vec2i(p->rays[i].end) + DRAW_OFFSET, + (t_rgba) {0xFF1500FF}); + i++; + } + // Draw the player look direction. draw_line(image, vec2f_to_vec2i(p->position) + DRAW_OFFSET, vec2f_to_vec2i(p->beam) + DRAW_OFFSET, (t_rgba) {0x00FF00FF}); +} +float ray_distance(t_ray r) +{ + t_vec2f diff = r.end - r.start; + return sqrtf((r.end[VEC_X] - r.start[VEC_X]) * (r.end[VEC_X] - r.start[VEC_X]) + (r.end[VEC_Y] - r.start[VEC_Y]) * (r.end[VEC_Y] - r.start[VEC_Y])); +} + +void render_player_view(mlx_image_t *image, t_player *p) +{ size_t i = 0; + t_ray tmp; + int height = 1; + const int x_offset = PLAYER_VIEWPORT_X + 1; + while (i < PLAYER_RAY_COUNT) { - draw_line(image, vec2f_to_vec2i(p->rays[i].start) + DRAW_OFFSET, - vec2f_to_vec2i(p->rays[i].end) + DRAW_OFFSET, - (t_rgba) {0xFF1500FF}); + tmp = p->rays[i]; + height = 500 - ray_distance(tmp); + draw_rect(image, x_offset + (PLAYER_VIEWPORT_WALL_WIDTH * i), PLAYER_VIEWPORT_Y, PLAYER_VIEWPORT_WALL_WIDTH, height, 0xfc7b25ff); i++; } }