Skip to content

Commit

Permalink
Merge pull request #20 from ys-zm/textures
Browse files Browse the repository at this point in the history
Textures
  • Loading branch information
JopjeKnopje authored Jan 18, 2024
2 parents a998b7c + 1d7509e commit dea0b0d
Show file tree
Hide file tree
Showing 29 changed files with 343 additions and 147 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.vscode/
obj/
app
app.dSYM
compile_commands.json
libft/build/libft.a
Session.vim
Expand Down
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: 3 additions & 3 deletions MLX42/include/MLX42/MLX42_Int.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* MLX42_Int.h :+: :+: */
/* MLX42_Int.h :+: :+: */
/* +:+ */
/* By: W2Wizard <[email protected]> +#+ */
/* +#+ */
/* Created: 2021/12/27 23:55:34 by W2Wizard #+# #+# */
/* Updated: 2022/07/21 10:46:43 by sbos ######## odam.nl */
/* Updated: 2024/01/17 12:49:14 by jboeve ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -34,7 +34,7 @@
# include <stdarg.h> /* va_arg, va_end, ... */
# include <assert.h> /* assert, static_assert, ... */
# ifndef MLX_SWAP_INTERVAL
# define MLX_SWAP_INTERVAL 1
# define MLX_SWAP_INTERVAL 0
# endif
# ifndef MLX_BATCH_SIZE
# define MLX_BATCH_SIZE 12000
Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ else ifeq ($(shell uname -m),x86_64)
endif

NAME := app
RUN_CMD := ./$(NAME) test_maps/valid.cub
RUN_CMD := ./$(NAME) test_maps/valid_tex.cub
# RUN_CMD := ./$(NAME) test_maps/small.cub

# CFLAGS += -Wall -Wextra -Werror
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
62 changes: 42 additions & 20 deletions include/meta.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/**/
/* :::::::: */
/* meta.h:+::+: */
/* meta.h :+: :+: */
/*+:+ */
/* By: jboeve <[email protected]>+#+ */
/* +#+ */
/* Created: 2023/11/01 20:07:37 by jboeve#+##+# */
/* Updated: 2024/01/07 03:51:44 by joppe ######## odam.nl */
/* Updated: 2024/01/18 11:56:06 by jboeve ######## odam.nl */
/**/
/* ************************************************************************** */

Expand Down Expand Up @@ -56,8 +56,6 @@
#define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 720

#define WINDOW_TITLE "Gammoe"



// Game
Expand Down Expand Up @@ -110,7 +108,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 +126,25 @@ 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_vec2i map_pos;
t_vec2i texture_point;
t_vec2i line_point;
t_side hit_side;

double line_height;
double length;
double wall_x;
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,8 @@ typedef struct s_meta {
t_player player;
uint32_t fps;
t_map map;
t_tex tex;
t_attr attributes;
const char *scene_name;
char *map_element;
} t_meta;

Expand Down Expand Up @@ -231,9 +245,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_vec2i 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(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);

#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
18 changes: 13 additions & 5 deletions src/cub3d.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cub3d.c :+: :+: :+: */
/* cub3d.c :+: :+: */
/* +:+ +:+ +:+ */
/* By: yzaim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/08 15:24:47 by yzaim #+# #+# */
/* Updated: 2024/01/08 15:25:06 by yzaim ### ########.fr */
/* Updated: 2024/01/18 10:45:29 by jboeve ######## odam.nl */
/* */
/* ************************************************************************** */

#include "MLX42/MLX42.h"
#include "MLX42/MLX42_Int.h"
#include "meta.h"
#include "parser.h"
#include <stdint.h>
Expand Down Expand Up @@ -51,7 +52,7 @@ void leaks(void)
// change to create a different image for the minimap vs. main viewport
int init_mlx_images(t_meta *meta)
{
meta->mlx = mlx_init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, true);
meta->mlx = mlx_init(WINDOW_WIDTH, WINDOW_HEIGHT, meta->scene_name, true);
if (!meta->mlx)
{
ft_error();
Expand Down Expand Up @@ -93,7 +94,7 @@ int init_mlx_images(t_meta *meta)
}

return (EXIT_SUCCESS);
}
}

int cub3d(int argc, char **argv)
{
Expand All @@ -104,8 +105,15 @@ 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);
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
4 changes: 2 additions & 2 deletions src/game/game.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* game.c :+: :+: :+: */
/* 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 */
/* Updated: 2024/01/17 13:04:46 by jboeve ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down
6 changes: 4 additions & 2 deletions src/game/player.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* player.c :+: :+: :+: */
/* player.c :+: :+: */
/* +:+ +:+ +:+ */
/* By: yzaim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/08 15:27:23 by yzaim #+# #+# */
/* Updated: 2024/01/08 15:27:25 by yzaim ### ########.fr */
/* Updated: 2024/01/17 15:55:18 by jboeve ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -39,6 +39,7 @@ void player_move(t_player *p, t_vec2d transform)
new_position.x = (p->position.x + (transform.x));
new_position.y = (p->position.y + (transform.y));

// TODO Fix.
t_ray r = raycaster_cast(p->position, vec2d_normalize(transform), bound_check, p->meta);
// print_ray("bound_ray", &r);

Expand Down Expand Up @@ -72,6 +73,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++;
}
}
Loading

0 comments on commit dea0b0d

Please sign in to comment.