Skip to content

Commit

Permalink
playing around with textures
Browse files Browse the repository at this point in the history
  • Loading branch information
ys-zm committed Jan 15, 2024
1 parent 3c1cb27 commit 837855b
Show file tree
Hide file tree
Showing 22 changed files with 309 additions and 94 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"files.associations": {
"cstdlib": "c"
"cstdlib": "c",
"stdio.h": "c"
}
}
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ RUN_CMD := ./$(NAME) test_maps/valid.cub

# CFLAGS += -Wall -Wextra -Werror
CFLAGS += -Wall -Wextra
# CFLAGS += -g -fsanitize=address
CFLAGS += -g -fsanitize=address
# CFLAGS += -g
# CFLAGS += -Ofast -flto -march=native

Expand Down Expand Up @@ -48,12 +48,14 @@ SRCS = parser/check_elements.c \
game/render_minimap.c \
game/render_viewport.c \
game/font/font_renderer.c \
renderer/pixel_picker.c \
vector/vec2i.c \
vector/vec2d.c \
vector/vec_utils.c \
cub3d.c \
test_utils.c \
timer.c
timer.c \
set_textures.c

HEADER_DIR := include
HEADERS := meta.h \
Expand Down
20 changes: 20 additions & 0 deletions app.dSYM/Contents/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.app</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
53 changes: 37 additions & 16 deletions include/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ typedef struct s_font_atlas
unsigned int font_w;
unsigned int font_h;
unsigned int bpp;
char *pixels;
char *pixels;
} t_font_atlas;


Expand All @@ -128,17 +128,23 @@ typedef union s_rgba


typedef enum e_side {
HIT_NONE,
HIT_NS,
HIT_EW,
SIDE_N,
SIDE_S,
SIDE_E,
SIDE_W,
} t_side;

typedef struct s_ray {
t_vec2d direction;
t_vec2d end;
t_side hit_side;
double length;
double wall_x;
t_vec2d direction;
t_vec2d end;
t_vec2d map_pos;
t_vec2i texture;
t_side hit_side;
double length;
double wall_x;
double line_height;
double texture_position;
double step;
} t_ray;

typedef struct s_player {
Expand All @@ -158,14 +164,21 @@ typedef struct s_map {
char player_start_dir;
} t_map;


typedef struct s_tex {
char *no;
char *so;
char *we;
char *ea;
char *tex_path;
mlx_texture_t *tex;
} t_tex;

typedef struct s_attr {
t_tex n;
t_tex s;
t_tex e;
t_tex w;
t_rgba floor_c;
t_rgba ceiling_c;
} t_tex;
} t_attr;


typedef struct s_minimap {
mlx_image_t *minimap_image;
Expand All @@ -183,7 +196,7 @@ typedef struct s_meta {
t_player player;
uint32_t fps;
t_map map;
t_tex tex;
t_attr attributes;
char *map_element;
} t_meta;

Expand Down Expand Up @@ -231,9 +244,17 @@ t_ray raycaster_cast(t_vec2d pp, t_vec2d dir, t_ray_hitfunc hit, const void *pa

// 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);
int32_t find_wall_color(t_attr atrributes, t_ray *ray, t_vec2d line_points, uint32_t h);
int32_t find_color(t_rgba rgba);

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

// set_textures.c
int set_textures(t_attr *attributes);

//pixel_picker.c
uint32_t pixel_picker(t_tex texture, uint32_t x, uint32_t y);
void wall_texture_position(t_tex texture, t_ray *ray, t_vec2d line_points, uint32_t h);

#endif
9 changes: 5 additions & 4 deletions include/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# define ELEMENT_MISSING "One or more definition of elements is missing\n"
# define NO_MAP "Map element is missing\n"
# define INV_COLOR_CODE "Please check color codes for F and C elements\nRGB values should be between 0-255\n"

# define MLX_ERR "MLX error occured"
# include "meta.h"

typedef enum e_err {
Expand All @@ -47,6 +47,7 @@ NO_PLAYER,
MISSING_ELEMENTS,
MISSING_MAP,
COLOR_CODE_WRONG,
MLX_ERROR,
} t_err;

// error.c
Expand All @@ -70,9 +71,9 @@ 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 input_texture(t_attr *attributes, char *file);
int input_colour(t_attr *attributes, char *file);
int save_elements(t_attr *attributes, char *file);
int parse_elements(t_meta *meta, char *file);

// check_colors.c
Expand Down
47 changes: 45 additions & 2 deletions src/cub3d.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,42 @@ int init_mlx_images(t_meta *meta)
}

return (EXIT_SUCCESS);
}

uint32_t make_color(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{

uint32_t color = 0;

// Combine the color components using bitwise OR operations
color |= (uint32_t)r << 24; // Red component (shifted to the left by 24 bits)
color |= (uint32_t)g << 16; // Green component (shifted to the left by 16 bits)
color |= (uint32_t)b << 8; // Blue component (shifted to the left by 8 bits)
color |= (uint32_t)a; // Alpha component

return color;
}

void print_texture(t_meta *meta)
{
int i = 0;
int x = 0;
int y = 0;
int32_t color;
mlx_texture_t *texture = meta->attributes.n.tex;

while (y < 64)
{
x = 0;
while (x < 64)
{
color = make_color(texture->pixels[i], texture->pixels[i + 1], texture->pixels[i + 2], texture->pixels[i + 3]);
mlx_put_pixel(meta->image, x, y, color);
x++;
i += 4;
}
y++;
}
}

int cub3d(int argc, char **argv)
Expand All @@ -104,13 +140,20 @@ int cub3d(int argc, char **argv)
ft_bzero(&meta, sizeof(t_meta));
if (parser(&meta, argv[1]))
return(meta_free(&meta), EXIT_FAILURE);
// TODO Error check.
if (set_textures(&meta.attributes))
return (EXIT_FAILURE);
printf("Tex_N: %s\n", meta.attributes.n.tex_path);
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);

init_mlx_images(&meta);
// TODO Error check.
game_init(&meta);
mlx_set_cursor_mode(meta.mlx, MLX_MOUSE_HIDDEN);
mlx_loop_hook(meta.mlx, game_loop, &meta);
mlx_loop_hook(meta.mlx, fps_hook, &meta);
mlx_cursor_hook(meta.mlx, cursor_hook, &meta);
// mlx_cursor_hook(meta.mlx, cursor_hook, &meta);
mlx_loop(meta.mlx);
mlx_terminate(meta.mlx);
meta_free(&meta);
Expand Down
22 changes: 11 additions & 11 deletions src/game/font/font_renderer.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/* ************************************************************************** */
/**/
/* :::::::: */
/* font_renderer.c :+::+: */
/*+:+ */
/* By: joppe <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/05 00:02:23 by joppe #+##+# */
/* Updated: 2024/01/07 03:28:05 by joppe ######## odam.nl */
/**/
/* */
/* ::: :::::::: */
/* game.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yzaim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/08 15:26:51 by yzaim #+# #+# */
/* Updated: 2024/01/08 15:29:57 by yzaim ### ########.fr */
/* */
/* ************************************************************************** */

/**
* This file is basically a copy of the `mlx_font.c`
* but with the ability to specifiy what font to draw.
*/

#include "MLX42/MLX42_Int.h"
// #include "MLX42/MLX42_Int.h"
#include "font_dejavu_14.h"
#include "font_comicsans.h"
#include "font_mlx.h"
Expand Down Expand Up @@ -72,7 +72,7 @@ mlx_image_t *cube_put_string(mlx_image_t *image, const char *s, const t_font_atl

if (image_len != image->width)
{
ft_bzero(image->pixels, image->width * image->height * BPP);
ft_bzero(image->pixels, image->width * image->height * sizeof(int32_t));
if (!mlx_resize_image(image, image_len, atlas->font_h))
return (NULL);
}
Expand Down
1 change: 1 addition & 0 deletions src/game/player.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ void player_raycast(t_player *p)
camera_x = (2 * col / ((double) w) - 1);
ray_start = vec2d_add(p->direction, vec2d_scalar_product(p->cam_plane, camera_x));
p->rays[col] = raycaster_cast(p->position, ray_start, bound_check, p->meta);
// printf("wall x: %f\n", p->rays[col].wall_x);
col++;
}
}
31 changes: 18 additions & 13 deletions src/game/raycaster.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ inline static t_vec2d calculate_step_size(t_vec2d ray_direction)
inline static double calculate_ray_length(t_side hit_side, \
t_vec2d side_dist, t_vec2d delta_dist)
{
if (hit_side == HIT_NS)
if (hit_side == SIDE_N || hit_side == SIDE_S)
return (side_dist.x - delta_dist.x);
else
return (side_dist.y - delta_dist.y);
Expand All @@ -75,47 +75,52 @@ inline static t_side ray_move(t_vec2d *side_dist, t_vec2d *delta_dist, \
{
side_dist->x += delta_dist->x;
map_pos->x += step_size.x;
return (HIT_NS);
if (step_size.x > 0)
return (SIDE_N);
else
return(SIDE_S);
}
else
{
side_dist->y += delta_dist->y;
map_pos->y += step_size.y;
return (HIT_EW);
if (step_size.y > 0)
return (SIDE_E);
else
return (SIDE_W);
}
}

t_ray raycaster_cast(t_vec2d pp, t_vec2d dir, t_ray_hitfunc hit, const void *param)
{
t_ray r;
t_vec2d map_pos;
t_vec2d side_dist;
t_vec2d step_size;
t_vec2d delta_dist;

map_pos.x = (int)pp.x;
map_pos.y = (int)pp.y;
r.map_pos.x = (int)pp.x;
r.map_pos.y = (int)pp.y;
delta_dist = calculate_delta_dist(dir);
side_dist = calculate_side_dist(dir, pp, map_pos, delta_dist);
side_dist = calculate_side_dist(dir, pp, r.map_pos, delta_dist);
step_size = calculate_step_size(dir);
size_t limit = 25;
while (limit)
{
r.hit_side = ray_move(&side_dist, &delta_dist, step_size, &map_pos);
if (hit && hit(param, map_pos.x, map_pos.y))
r.hit_side = ray_move(&side_dist, &delta_dist, step_size, &r.map_pos);
if (hit && hit(param, r.map_pos.x, r.map_pos.y))
break;
limit--;
}
if (!limit)
WARNING("Raycaster limit reached!");
r.length = calculate_ray_length(r.hit_side, side_dist, delta_dist);
r.direction = dir;
r.end = map_pos;
r.end = r.map_pos;

if (r.hit_side == HIT_NS)
r.wall_x = map_pos.y + r.length * r.direction.y;
if (r.hit_side == SIDE_N || r.hit_side == SIDE_S)
r.wall_x = r.map_pos.y + r.length * r.direction.y;
else
r.wall_x = map_pos.x + r.length * r.direction.x;
r.wall_x = r.map_pos.x + r.length * r.direction.x;
r.wall_x -= floor(r.wall_x);
return (r);
}
Loading

0 comments on commit 837855b

Please sign in to comment.