Skip to content

Commit

Permalink
Merge pull request #22 from ys-zm/floor-ceiling-textures
Browse files Browse the repository at this point in the history
Floor ceiling textures
  • Loading branch information
JopjeKnopje authored Jan 25, 2024
2 parents 9418314 + a701802 commit 9a89751
Show file tree
Hide file tree
Showing 28 changed files with 259 additions and 96 deletions.
7 changes: 4 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"files.associations": {
"cstdlib": "c",
"stdio.h": "c"
}
"cstdlib": "c",
"stdio.h": "c",
"meta.h": "c"
}
}
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ SRCS = parser/check_elements.c \
cub3d.c \
test_utils.c \
timer.c \
set_textures.c
set_textures.c \
game/floorcaster.c

HEADER_DIR := include
HEADERS := meta.h \
Expand Down
18 changes: 16 additions & 2 deletions include/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <fcntl.h>
#include <stdbool.h>
#include <limits.h>
#include <math.h>

#include "timer.h"
#include "libft.h"
Expand Down Expand Up @@ -81,7 +82,6 @@
#define VIEWPORT_COLOR_WALL_NS 0x4B0082FF
#define VIEWPORT_COLOR_WALL_EW 0x8A30E2FF

// #define FOV 0.66
#define FOV 0.85

typedef bool (t_ray_hitfunc) (const void *p, uint32_t x, uint32_t y);
Expand All @@ -92,6 +92,7 @@ typedef enum e_cell_type {
MAP_EMPTY,
MAP_WALL,
MAP_SPACE,
MAP_DOOR
} t_cell_type;

typedef enum e_font_family {
Expand Down Expand Up @@ -147,9 +148,16 @@ typedef struct s_ray {
double step;
} t_ray;

typedef struct s_vray {
t_vec2d floor;
t_vec2d step;
} t_vray;

typedef struct s_player {
t_meta *meta;
t_ray rays[WINDOW_WIDTH];
t_vray vrays[WINDOW_HEIGHT];
bool should_render;
t_vec2d cam_plane;
t_vec2d position;
t_vec2d direction;
Expand All @@ -171,10 +179,13 @@ typedef struct s_tex {
} t_tex;

typedef struct s_attr {
t_tex n;
t_tex n; //add bit flag, if tex_path is missing then it means it is a color value
t_tex s;
t_tex e;
t_tex w;
t_tex f;
t_tex c;
t_tex c_alt;
t_rgba floor_c;
t_rgba ceiling_c;
} t_attr;
Expand Down Expand Up @@ -258,4 +269,7 @@ int set_textures(t_attr *attributes);
uint32_t pixel_picker(mlx_texture_t *texture, int32_t x, int32_t y);
void wall_texture_position(mlx_texture_t *texture, t_ray *ray, t_vec2i line_points, uint32_t h);

// floorcaster.c
t_vray floorcaster(t_vec2d pp, t_vec2d dir, t_vec2d cam_plane, uint32_t y);

#endif
15 changes: 8 additions & 7 deletions include/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,21 @@ 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);
bool is_wall(char *file);
bool is_floor_or_ceiling(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);
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);
bool is_path(char *str);


#endif
16 changes: 9 additions & 7 deletions src/cub3d.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cub3d.c :+: :+: */
/* +:+ +:+ +:+ */
/* By: yzaim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/08 15:24:47 by yzaim #+# #+# */
/* Updated: 2024/01/18 10:45:29 by jboeve ######## odam.nl */
/* :::::::: */
/* cub3d.c :+: :+: */
/* +:+ */
/* By: yzaim <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/08 15:24:47 by yzaim #+# #+# */
/* Updated: 2024/01/24 11:14:56 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -111,6 +111,8 @@ int cub3d(int argc, char **argv)
printf("Tex_S: %s\n", meta.attributes.s.tex_path);
printf("Tex_E: %s\n", meta.attributes.e.tex_path);
printf("Tex_W: %s\n", meta.attributes.w.tex_path);
printf("Tex_C: %s\n", meta.attributes.c.tex_path);
printf("Tex_F: %s\n", meta.attributes.f.tex_path);

init_mlx_images(&meta);
// TODO Error check.
Expand Down
34 changes: 34 additions & 0 deletions src/game/floorcaster.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* floorcaster.c :+: :+: */
/* +:+ */
/* By: yzaim <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/24 13:13:37 by yzaim #+# #+# */
/* Updated: 2024/01/24 18:23:45 by jboeve ######## odam.nl */
/* */
/* ************************************************************************** */

#include "meta.h"

t_vray floorcaster(t_vec2d pp, t_vec2d dir, t_vec2d cam_plane, uint32_t y)
{
t_vray ray;
t_vec2d raydir_left;
t_vec2d raydir_right;

raydir_left = ((t_vec2d){dir.x - cam_plane.x, dir.y - cam_plane.y});
raydir_right = ((t_vec2d){dir.x + cam_plane.x, dir.y + cam_plane.y});

int p = y - WINDOW_HEIGHT / 2;
double posZ = 0.5 * WINDOW_HEIGHT;
double row_distance = posZ / p;
ray.step.x = row_distance * (raydir_right.x - raydir_left.x) / WINDOW_WIDTH;
ray.step.y = row_distance * (raydir_right.y - raydir_left.y) / WINDOW_WIDTH;

ray.floor.x = pp.x + row_distance * raydir_left.x;
ray.floor.y = pp.y + row_distance * raydir_left.y;

return (ray);
}
16 changes: 14 additions & 2 deletions src/game/player.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* :::::::: */
/* player.c :+: :+: */
/* +:+ +:+ +:+ */
/* By: yzaim <[email protected]> +#+ +:+ +#+ */
Expand Down Expand Up @@ -91,10 +91,22 @@ void player_turn(t_player *p, float radiant)

void player_raycast(t_player *p)
{
uint32_t w = p->meta->image->width;
uint32_t h = p->meta->image->height;
uint32_t col;
double camera_x;
uint32_t row;
t_vec2d ray_start;
double camera_x;


row = 0;
while (row < h)
{
p->vrays[row] = floorcaster(p->position, p->direction, p->cam_plane, row);
row++;
}
p->should_render = true;

// TODO Just create the player.plane here instead of saving it.
col = 0;
while (col < p->meta->image->width)
Expand Down
14 changes: 7 additions & 7 deletions src/game/raycaster.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* raycaster.c :+: :+: */
/* +:+ +:+ +:+ */
/* By: yzaim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/08 15:27:33 by yzaim #+# #+# */
/* Updated: 2024/01/18 12:05:53 by jboeve ######## odam.nl */
/* :::::::: */
/* raycaster.c :+: :+: */
/* +:+ */
/* By: yzaim <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/08 15:27:33 by yzaim #+# #+# */
/* Updated: 2024/01/24 13:11:30 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down
50 changes: 47 additions & 3 deletions src/game/render_viewport.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,60 @@ static void draw_column(t_meta *meta, t_ray *ray, uint32_t col, uint32_t h)
ray->texture_point.y = ((int) ray->texture_position) & (texture->height - 1);
ray->texture_position += ray->step;
color = pixel_picker(texture, (int)round(ray->texture_point.x), (int)round(ray->texture_point.y));
mlx_put_pixel(meta->image, col, y, color);
}
mlx_put_pixel(meta->image, col, y, color);
y++;
}
}

void draw_fc(mlx_image_t *image, t_vray *vray, mlx_texture_t *f_tex, mlx_texture_t *c_tex, mlx_texture_t *c_alt_tex, uint32_t col, uint32_t row)
{
const t_vec2i cell = vec2d_to_vec2i(vray->floor);
// coordinate of pixel in texture
const t_vec2i c_t = (t_vec2i){(int)(c_tex->width * (vray->floor.x - cell.x)) & (c_tex->width - 1),
(int)(c_tex->height * (vray->floor.y - cell.y)) & (c_tex->height - 1)};
const t_vec2i f_t = (t_vec2i){(int)(f_tex->width * (vray->floor.x - cell.x)) & (f_tex->width - 1),
(int)(f_tex->height * (vray->floor.y - cell.y)) & (f_tex->height - 1)};

const uint32_t c_pixel = pixel_picker(c_tex, c_t.x, c_t.y);
const uint32_t c_alt_pixel = pixel_picker(c_alt_tex, c_t.x, c_t.y);
const uint32_t f_pixel = pixel_picker(f_tex, f_t.x, f_t.y);

vray->floor = vec2d_add(vray->floor, vray->step);

if (cell.y % 2 && cell.x % 2)
mlx_put_pixel(image, col, WINDOW_HEIGHT - row - 1, c_alt_pixel);
else
mlx_put_pixel(image, col, WINDOW_HEIGHT - row - 1, c_pixel);

mlx_put_pixel(image, col, row, f_pixel);
}

void render_viewport(mlx_image_t *image, t_player *p)
{
uint32_t col = 0;
while(col < image->width)
uint32_t col = 0;
uint32_t row = 0;


//floor and ceiling
if (p->should_render)
{
while (row < image->height)
{
col = 0;
while (col < image->width)
{
draw_fc(p->meta->image, &p->vrays[row], p->meta->attributes.f.tex, p->meta->attributes.c.tex, p->meta->attributes.c_alt.tex, col, row);
col++;
}
row++;
}
p->should_render = false;
}


col = 0;
while (col < image->width)
{
draw_column(p->meta, &p->rays[col], col, image->height);
col++;
Expand Down
16 changes: 8 additions & 8 deletions src/parser/check_colors.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_colors.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yzaim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/08 15:30:18 by yzaim #+# #+# */
/* Updated: 2024/01/08 15:33:48 by yzaim ### ########.fr */
/* :::::::: */
/* check_colors.c :+: :+: */
/* +:+ */
/* By: yzaim <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/08 15:30:18 by yzaim #+# #+# */
/* Updated: 2024/01/24 11:18:50 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -64,7 +64,7 @@ bool colors_valid(char *file)
skip_spaces(&file);
if (is_valid_element(file))
{
if (is_colour(file) && !is_valid_color(file))
if (!is_path(file) && is_floor_or_ceiling(file) && !is_valid_color(file))
return (false);
}
skip_line(&file);
Expand Down
18 changes: 9 additions & 9 deletions src/parser/check_elements.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_elements.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yzaim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/08 15:33:58 by yzaim #+# #+# */
/* Updated: 2024/01/08 15:42:20 by yzaim ### ########.fr */
/* :::::::: */
/* check_elements.c :+: :+: */
/* +:+ */
/* By: yzaim <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/08 15:33:58 by yzaim #+# #+# */
/* Updated: 2024/01/24 11:02:13 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -96,7 +96,7 @@ bool is_missing(char *file)
{
i = 0;
skip_spaces(&file);
if ((is_valid_element(file) || is_colour(file)))
if ((is_valid_element(file) || is_floor_or_ceiling(file)))
{
while (*file && *file != element[i] && i < 6)
i++;
Expand All @@ -118,7 +118,7 @@ bool is_duplicate(char *file)
{
i = 0;
skip_spaces(&file);
if ((is_valid_element(file) || is_colour(file)))
if ((is_valid_element(file) || is_floor_or_ceiling(file)))
{
while (*file && *file != element[i] && i < 6)
i++;
Expand Down
Loading

0 comments on commit 9a89751

Please sign in to comment.