Skip to content

Commit

Permalink
Merge pull request #10 from ys-zm/raycaster
Browse files Browse the repository at this point in the history
Raycaster
  • Loading branch information
JopjeKnopje authored Dec 13, 2023
2 parents 6ad474f + 1a0f074 commit 13232a0
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 37 deletions.
59 changes: 51 additions & 8 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/11/13 21:19:38 by joppe ######## odam.nl */
/* Updated: 2023/11/18 20:45:26 by jboeve ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion include/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
/* By: joppe <[email protected]> +#+ */
/* +#+ */
/* 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 */
/* */
/* ************************************************************************** */

#ifndef VECTOR_H
#define VECTOR_H

#include <inttypes.h>
#include <sys/cdefs.h>


#define VEC_X 0
Expand Down
Binary file added src/.render.c.swp
Binary file not shown.
6 changes: 3 additions & 3 deletions src/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
10 changes: 4 additions & 6 deletions src/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: joppe <[email protected]> +#+ */
/* +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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));
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
4 changes: 3 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
/* By: jboeve <[email protected]> +#+ */
/* +#+ */
/* 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 <stdio.h>

int main(int argc, char *argv[])
{
Expand Down
14 changes: 7 additions & 7 deletions src/player.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: joppe <[email protected]> +#+ */
/* +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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);
Expand Down Expand Up @@ -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++;
}
}
16 changes: 16 additions & 0 deletions src/prog.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "enemy.h"
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

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);
//}
24 changes: 24 additions & 0 deletions src/raycaster.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* raycaster.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yzaim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
}
}
60 changes: 49 additions & 11 deletions src/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: joppe <[email protected]> +#+ */
/* +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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 <[email protected]> +#+ */
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.
Expand All @@ -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++;
}
}
Expand Down

0 comments on commit 13232a0

Please sign in to comment.