「🏗️」 wip(Merge): Merged raycasting
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,13 +1,9 @@
|
|||||||
obj/
|
obj/
|
||||||
cub3D
|
cub3D
|
||||||
.direnv
|
.direnv
|
||||||
.envrc
|
|
||||||
flake.nix
|
|
||||||
compile_commands.json
|
compile_commands.json
|
||||||
.cache
|
.cache
|
||||||
libft/**/*.o
|
libft/**/*.o
|
||||||
libft/**/*.a
|
libft/**/*.a
|
||||||
src/main.c
|
src/main.c
|
||||||
vgcore.*
|
vgcore.*
|
||||||
Makefile
|
|
||||||
src/cub3d.c
|
|
||||||
|
Submodule MacroLibX updated: 4d640ed70c...fc6aeab678
51
Makefile
51
Makefile
@ -1,8 +1,20 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# ::: :::::::: #
|
||||||
|
# Makefile :+: :+: :+: #
|
||||||
|
# +:+ +:+ +:+ #
|
||||||
|
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
|
||||||
|
# +#+#+#+#+#+ +#+ #
|
||||||
|
# Created: 2024/10/07 16:02:18 by adjoly #+# #+# #
|
||||||
|
# Updated: 2024/11/06 12:52:42 by adjoly ### ########.fr #
|
||||||
|
# #
|
||||||
|
# **************************************************************************** #
|
||||||
|
|
||||||
SHELL = bash
|
SHELL = bash
|
||||||
|
|
||||||
NAME = cub3D
|
NAME = cub3D
|
||||||
|
|
||||||
CC = gcc
|
CC = cc
|
||||||
|
|
||||||
OBJSDIR = obj/
|
OBJSDIR = obj/
|
||||||
|
|
||||||
@ -16,36 +28,21 @@ MACRO_DIR = MacroLibX/
|
|||||||
|
|
||||||
INCLUDE = -I $(I_DIR) -I $(LIBFT_DIR)/$(I_DIR) -I $(MACRO_DIR)/$(I_DIR)
|
INCLUDE = -I $(I_DIR) -I $(LIBFT_DIR)/$(I_DIR) -I $(MACRO_DIR)/$(I_DIR)
|
||||||
|
|
||||||
SRCS = src/utils/mess_error.c \
|
SRCS = $(shell find src -name *.c)
|
||||||
src/utils/parse_utils.c \
|
|
||||||
src/utils/parse_utils_two.c \
|
|
||||||
src/utils/parse_utils_three.c \
|
|
||||||
src/utils/utils.c \
|
|
||||||
src/utils/clear_free.c \
|
|
||||||
src/parsing/check_image.c \
|
|
||||||
src/parsing/check_map.c \
|
|
||||||
src/parsing/check_arg.c \
|
|
||||||
src/parsing/check_id_text.c \
|
|
||||||
src/parsing/check_rgb.c \
|
|
||||||
src/parsing/color_utils.c \
|
|
||||||
src/parsing/find_player.c \
|
|
||||||
src/parsing/principal_pars.c \
|
|
||||||
src/parsing/start.c \
|
|
||||||
src/cub3d.c
|
|
||||||
|
|
||||||
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.c=.o))
|
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.c=.o))
|
||||||
|
|
||||||
FLAGS = -Wall -Werror -Wextra -g -lm -lSDL2
|
FLAGS = -Wall -Werror -Wextra -g -MMD -MP
|
||||||
|
|
||||||
LIB = libft/libft.a \
|
LIB = libft/libft.a \
|
||||||
MacroLibX/libmlx.so
|
MacroLibX/libmlx.so
|
||||||
|
|
||||||
$(NAME): $(OBJS)
|
$(NAME): $(OBJS)
|
||||||
@make -sj $(nproc) -C $(LIBFT_DIR)
|
@make -sj -C $(LIBFT_DIR)
|
||||||
@echo "✅ Libft compiled"
|
@echo "✅ Libft compiled"
|
||||||
@make -sj $(nproc) -C $(MACRO_DIR)
|
@#@make -sj -C $(MACRO_DIR)
|
||||||
@echo "✅ MacroLibX compiled"
|
@#@echo "✅ MacroLibX compiled"
|
||||||
@$(CC) $(OBJS) $(LIB) -o $(NAME) $(FLAGS)
|
@$(CC) $(OBJS) $(LIB) -o $(NAME) $(FLAGS) -lm -lSDL2
|
||||||
@echo "✅ Compiled"
|
@echo "✅ Compiled"
|
||||||
|
|
||||||
$(OBJSDIR)%.o: %.c
|
$(OBJSDIR)%.o: %.c
|
||||||
@ -55,15 +52,15 @@ $(OBJSDIR)%.o: %.c
|
|||||||
all: $(NAME)
|
all: $(NAME)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@make -s -C $(LIBFT_DIR) clean
|
@make -sC $(LIBFT_DIR) clean
|
||||||
@make -s -C $(MACRO_DIR) clean > /dev/null
|
@#@make -sC $(MACRO_DIR) clean > /dev/null
|
||||||
@rm -f $(OBJS)
|
@rm -f $(OBJS)
|
||||||
|
|
||||||
fclean: clean
|
fclean: clean
|
||||||
@make -s -C $(LIBFT_DIR) fclean
|
@make -sC $(LIBFT_DIR) fclean
|
||||||
@echo "🧹 Libft Cleaned"
|
@echo "🧹 Libft Cleaned"
|
||||||
@make -s -C $(MACRO_DIR) fclean > /dev/null
|
@#@make -sC $(MACRO_DIR) fclean > /dev/null
|
||||||
@echo "🧹 MacroLibX Cleaned"
|
@#@echo "🧹 MacroLibX Cleaned"
|
||||||
@rm -f $(NAME)
|
@rm -f $(NAME)
|
||||||
@rm -Rf $(OBJSDIR)
|
@rm -Rf $(OBJSDIR)
|
||||||
@echo "🧹 Cleaned"
|
@echo "🧹 Cleaned"
|
||||||
|
61
flake.lock
generated
Normal file
61
flake.lock
generated
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"flake-utils": {
|
||||||
|
"inputs": {
|
||||||
|
"systems": "systems"
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1726560853,
|
||||||
|
"narHash": "sha256-X6rJYSESBVr3hBoH0WbKE5KvhPU5bloyZ2L4K60/fPQ=",
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"rev": "c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1729256560,
|
||||||
|
"narHash": "sha256-/uilDXvCIEs3C9l73JTACm4quuHUsIHcns1c+cHUJwA=",
|
||||||
|
"owner": "nixos",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "4c2fcb090b1f3e5b47eaa7bd33913b574a11e0a0",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nixos",
|
||||||
|
"ref": "nixos-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"flake-utils": "flake-utils",
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"systems": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1681028828,
|
||||||
|
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
||||||
|
"owner": "nix-systems",
|
||||||
|
"repo": "default",
|
||||||
|
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nix-systems",
|
||||||
|
"repo": "default",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
39
flake.nix
Normal file
39
flake.nix
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
{
|
||||||
|
description = "A Nix-flake-based C/C++ development environment";
|
||||||
|
inputs = {
|
||||||
|
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
||||||
|
flake-utils.url = "github:numtide/flake-utils";
|
||||||
|
};
|
||||||
|
|
||||||
|
outputs = { nixpkgs, flake-utils, ... }: flake-utils.lib.eachDefaultSystem (system:
|
||||||
|
let
|
||||||
|
pkgs = nixpkgs.legacyPackages.${system};
|
||||||
|
in
|
||||||
|
{
|
||||||
|
devShells = ({
|
||||||
|
default = pkgs.mkShell.override
|
||||||
|
{
|
||||||
|
}
|
||||||
|
{
|
||||||
|
buildInputs = with pkgs; [
|
||||||
|
gcc
|
||||||
|
clang
|
||||||
|
SDL2
|
||||||
|
vulkan-loader
|
||||||
|
];
|
||||||
|
|
||||||
|
LD_LIBRARY_PATH="${pkgs.vulkan-loader}/lib";
|
||||||
|
hardeningDisable = [ "all" ];
|
||||||
|
|
||||||
|
packages = with pkgs; [
|
||||||
|
bear
|
||||||
|
clang
|
||||||
|
gcc
|
||||||
|
gdb
|
||||||
|
norminette
|
||||||
|
valgrind
|
||||||
|
];
|
||||||
|
};
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
@ -6,13 +6,20 @@
|
|||||||
/* By: madumerg <madumerg@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: madumerg <madumerg@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/09/04 17:01:05 by madumerg #+# #+# */
|
/* Created: 2024/09/04 17:01:05 by madumerg #+# #+# */
|
||||||
/* Updated: 2024/10/05 14:30:04 by madumerg ### ########.fr */
|
/* Updated: 2024/11/06 12:49:47 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef CUB3D_H
|
#ifndef CUB3D_H
|
||||||
# define CUB3D_H
|
# define CUB3D_H
|
||||||
|
|
||||||
|
# include "libft.h"
|
||||||
|
# include "mess_err.h"
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include <math.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <fcntl.h>
|
||||||
|
# include <stdio.h>
|
||||||
# include "parsing.h"
|
# include "parsing.h"
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,41 +1,43 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* game.h :+: :+: :+: */
|
/* dda.h :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/09/11 16:38:40 by adjoly #+# #+# */
|
/* Created: 2024/10/28 13:04:36 by adjoly #+# #+# */
|
||||||
/* Updated: 2024/09/12 17:04:59 by adjoly ### ########.fr */
|
/* Updated: 2024/11/06 12:30:52 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef GAME_H
|
#ifndef DDA_H
|
||||||
# define GAME_H
|
# define DDA_H
|
||||||
|
|
||||||
typedef struct s_cub
|
# include <math.h>
|
||||||
|
|
||||||
|
# include "game.h"
|
||||||
|
# include "game/vectwo.h"
|
||||||
|
|
||||||
|
typedef struct s_dda
|
||||||
{
|
{
|
||||||
void *mlx;
|
bool h;
|
||||||
void *win;
|
bool s;
|
||||||
void *images;
|
int i;
|
||||||
char **map;
|
t_vec2 map;
|
||||||
} t_cub;
|
t_vec2 vert;
|
||||||
|
t_vec2 hori;
|
||||||
|
t_vec2 distance;
|
||||||
|
} t_dda;
|
||||||
|
|
||||||
#define ESCAPE_KEY 41
|
typedef struct s_ray
|
||||||
#define W_KEY 26
|
{
|
||||||
#define S_KEY 22
|
t_vec2 pos;
|
||||||
#define A_KEY 4
|
float tan;
|
||||||
#define D_KEY 7
|
float angle;
|
||||||
#define WHITE 0xFFFFFFFF
|
t_vec2 offset;
|
||||||
|
float distance;
|
||||||
|
} t_ray;
|
||||||
|
|
||||||
/**
|
void dda_algo(t_render *render, t_dda *dda, t_ray *ray);
|
||||||
* @brief This function is used to handle keypress
|
|
||||||
*
|
|
||||||
* @param key The key code of the pressed key
|
|
||||||
* @param mlx The mlx pointer(internal to MacroLibX)
|
|
||||||
*
|
|
||||||
* @return Minilibx go brrrr(useless always 0)
|
|
||||||
*/
|
|
||||||
int key_hook(int key, void *mlx);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
86
includes/game/game.h
Normal file
86
includes/game/game.h
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* game.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/09/11 16:38:40 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2024/11/02 15:55:39 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef GAME_H
|
||||||
|
# define GAME_H
|
||||||
|
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <stdint.h>
|
||||||
|
# include <math.h>
|
||||||
|
# include <stdbool.h>
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include <stddef.h>
|
||||||
|
# include <stdio.h>
|
||||||
|
|
||||||
|
# include "settings.h"
|
||||||
|
|
||||||
|
# include "vectwo.h"
|
||||||
|
|
||||||
|
# include "../parsing.h"
|
||||||
|
|
||||||
|
typedef struct s_map
|
||||||
|
{
|
||||||
|
char **arr;
|
||||||
|
int floor;
|
||||||
|
int celling;
|
||||||
|
char p_side;
|
||||||
|
t_coord p_spawnpoint;
|
||||||
|
t_coord size;
|
||||||
|
} t_map;
|
||||||
|
|
||||||
|
typedef struct s_player
|
||||||
|
{
|
||||||
|
t_vec2 coord;
|
||||||
|
float direction;
|
||||||
|
} t_player;
|
||||||
|
|
||||||
|
typedef struct s_render
|
||||||
|
{
|
||||||
|
t_map *world;
|
||||||
|
void *mlx;
|
||||||
|
void *win;
|
||||||
|
void *img;
|
||||||
|
t_player *player;
|
||||||
|
void *texture[4];
|
||||||
|
} t_render;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief This function is used to handle keypress
|
||||||
|
*
|
||||||
|
* @param key The key code of the pressed key
|
||||||
|
* @param mlx The mlx pointer(internal to MacroLibX)
|
||||||
|
*
|
||||||
|
* @return Minilibx go brrrr(useless always 0)
|
||||||
|
*/
|
||||||
|
int key_hook(int key, void *param);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief This function is here to change the direction of the player
|
||||||
|
* by setting the t_player->direction and check overflow to be
|
||||||
|
* sure it stays between 0 and 2pi because it is expressed in
|
||||||
|
* radians
|
||||||
|
*
|
||||||
|
* @param speed The speed a which the player rotate
|
||||||
|
* @param clockwise The direction which the player rotate
|
||||||
|
* @param player A pointer to a t_player struct
|
||||||
|
*/
|
||||||
|
void change_direction(float speed, bool clockwise, t_player *player);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Render a cub3d frame by executing a dda on every ray and
|
||||||
|
* putting the pixel on the window
|
||||||
|
*
|
||||||
|
* @param render The render struct with everything i need
|
||||||
|
*/
|
||||||
|
void render_frame(t_render *render);
|
||||||
|
|
||||||
|
#endif
|
14
includes/game/math.h
Normal file
14
includes/game/math.h
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* math.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/29 10:07:24 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2024/10/29 10:08:53 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef
|
||||||
|
void fix_ang(float *ang);
|
33
includes/game/settings.h
Normal file
33
includes/game/settings.h
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* settings.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/04 14:06:39 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2024/11/02 15:55:02 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef SETTINGS_H
|
||||||
|
# define SETTINGS_H
|
||||||
|
|
||||||
|
// Key code
|
||||||
|
# define ESCAPE_KEY 41
|
||||||
|
# define W_KEY 26
|
||||||
|
# define S_KEY 22
|
||||||
|
# define A_KEY 4
|
||||||
|
# define D_KEY 7
|
||||||
|
|
||||||
|
# define WHITE 0xFFFFFFFF
|
||||||
|
|
||||||
|
# define WINDOW_W 1600
|
||||||
|
# define WINDOW_H 900
|
||||||
|
# define PLAYER_ROT_SPEED 0.034906585039887
|
||||||
|
# define PLAYER_SPEED 1.5
|
||||||
|
# define FOV 70
|
||||||
|
|
||||||
|
# define CHUNK_SIZE 64
|
||||||
|
|
||||||
|
#endif
|
20
includes/game/utils_math.h
Normal file
20
includes/game/utils_math.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* utils_math.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/29 10:07:24 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2024/11/02 15:54:32 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef UTILS_MATH_H
|
||||||
|
# define UTILS_MATH_H
|
||||||
|
|
||||||
|
# include <math.h>
|
||||||
|
|
||||||
|
void fix_ang(float *ang);
|
||||||
|
|
||||||
|
#endif
|
45
includes/game/vectwo.h
Normal file
45
includes/game/vectwo.h
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* vectwo.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/28 13:32:14 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2024/11/02 15:51:49 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef VECTWO_H
|
||||||
|
# define VECTWO_H
|
||||||
|
|
||||||
|
# include <stdint.h>
|
||||||
|
|
||||||
|
typedef struct s_vec2
|
||||||
|
{
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
} t_vec2;
|
||||||
|
|
||||||
|
/* ------------------ Vec2 Utils ------------------ */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gives you the distance between two point
|
||||||
|
*/
|
||||||
|
float vec2_dist(t_vec2 first, t_vec2 second);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Just add second to first
|
||||||
|
*/
|
||||||
|
void vec2_add(t_vec2 *first, t_vec2 second);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Just substract second to first
|
||||||
|
*/
|
||||||
|
void vec2_sub(t_vec2 *first, t_vec2 second);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Multiply a vec2 by a number n and returns it
|
||||||
|
*/
|
||||||
|
t_vec2 vec2_mul_ret(t_vec2 vec, float n);
|
||||||
|
#endif
|
@ -6,7 +6,7 @@
|
|||||||
/* By: madumerg <madumerg@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: madumerg <madumerg@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/09/12 12:49:16 by madumerg #+# #+# */
|
/* Created: 2024/09/12 12:49:16 by madumerg #+# #+# */
|
||||||
/* Updated: 2024/10/06 16:16:31 by madumerg ### ########.fr */
|
/* Updated: 2024/11/06 12:50:01 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: madumerg <madumerg@student.42angouleme. +#+ +:+ +#+ */
|
/* By: madumerg <madumerg@student.42angouleme. +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/11/26 15:52:39 by madumerg #+# #+# */
|
/* Created: 2023/11/26 15:52:39 by madumerg #+# #+# */
|
||||||
/* Updated: 2024/02/02 14:25:22 by madumerg ### ########.fr */
|
/* Updated: 2024/10/17 01:28:48 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
int ft_putchar(char c)
|
int ft_putchar(char c)
|
||||||
{
|
{
|
||||||
write(1, &c, 1);
|
(void)write(1, &c, 1);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,7 +56,7 @@ int ft_putstr(char *str)
|
|||||||
return (ft_putstr("(null)"));
|
return (ft_putstr("(null)"));
|
||||||
while (str[i] != '\0')
|
while (str[i] != '\0')
|
||||||
{
|
{
|
||||||
write(1, &str[i], 1);
|
(void)write(1, &str[i], 1);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (i);
|
return (i);
|
||||||
|
@ -51,7 +51,7 @@ HEADERS = includes/
|
|||||||
all : ${NAME}
|
all : ${NAME}
|
||||||
|
|
||||||
${OBJS} :
|
${OBJS} :
|
||||||
@gcc -Wall -Wextra -Werror -I ${HEADERS} -c ${@:.o=.c} -o $@
|
@cc -Wall -Wextra -Werror -I ${HEADERS} -c ${@:.o=.c} -o $@
|
||||||
|
|
||||||
${NAME} : ${OBJS}
|
${NAME} : ${OBJS}
|
||||||
@ar rcs ${NAME} ${OBJS}
|
@ar rcs ${NAME} ${OBJS}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: madumerg <madumerg@student.42.fr> +#+ +:+ +#+ */
|
/* By: madumerg <madumerg@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/10/31 13:02:45 by madumerg #+# #+# */
|
/* Created: 2023/10/31 13:02:45 by madumerg #+# #+# */
|
||||||
/* Updated: 2024/09/04 16:24:48 by madumerg ### ########.fr */
|
/* Updated: 2024/10/16 20:58:23 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: madumerg <madumerg@student.42.fr> +#+ +:+ +#+ */
|
/* By: madumerg <madumerg@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/11/03 14:32:56 by madumerg #+# #+# */
|
/* Created: 2023/11/03 14:32:56 by madumerg #+# #+# */
|
||||||
/* Updated: 2023/11/03 14:52:19 by madumerg ### ########.fr */
|
/* Updated: 2024/10/17 01:13:30 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,5 +14,5 @@
|
|||||||
|
|
||||||
void ft_putchar_fd(char c, int fd)
|
void ft_putchar_fd(char c, int fd)
|
||||||
{
|
{
|
||||||
write(fd, &c, 1);
|
(void)write(fd, &c, 1);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: madumerg <madumerg@student.42.fr> +#+ +:+ +#+ */
|
/* By: madumerg <madumerg@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/11/03 14:33:06 by madumerg #+# #+# */
|
/* Created: 2023/11/03 14:33:06 by madumerg #+# #+# */
|
||||||
/* Updated: 2024/04/04 17:11:24 by madumerg ### ########.fr */
|
/* Updated: 2024/10/17 01:28:14 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -21,8 +21,8 @@ void ft_putendl_fd(char *s, int fd)
|
|||||||
return ;
|
return ;
|
||||||
while (s[i] != '\0')
|
while (s[i] != '\0')
|
||||||
{
|
{
|
||||||
write(fd, &s[i], 1);
|
(void)write(fd, &s[i], 1);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
write(fd, "\n", 1);
|
(void)write(fd, "\n", 1);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: madumerg <madumerg@student.42.fr> +#+ +:+ +#+ */
|
/* By: madumerg <madumerg@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/11/03 14:33:15 by madumerg #+# #+# */
|
/* Created: 2023/11/03 14:33:15 by madumerg #+# #+# */
|
||||||
/* Updated: 2023/11/09 16:26:33 by madumerg ### ########.fr */
|
/* Updated: 2024/10/17 01:13:01 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,14 +14,5 @@
|
|||||||
|
|
||||||
void ft_putstr_fd(char *s, int fd)
|
void ft_putstr_fd(char *s, int fd)
|
||||||
{
|
{
|
||||||
int i;
|
(void)write(fd, s, ft_strlen(s));
|
||||||
|
|
||||||
i = 0;
|
|
||||||
if (!s)
|
|
||||||
return ;
|
|
||||||
while (s[i] != '\0')
|
|
||||||
{
|
|
||||||
write(fd, &s[i], 1);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
53
src/cub3d.c
53
src/cub3d.c
@ -6,22 +6,59 @@
|
|||||||
/* By: madumerg <madumerg@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: madumerg <madumerg@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/09/04 16:58:27 by madumerg #+# #+# */
|
/* Created: 2024/09/04 16:58:27 by madumerg #+# #+# */
|
||||||
|
<<<<<<< HEAD
|
||||||
/* Updated: 2024/10/05 19:18:00 by madumerg ### ########.fr */
|
/* Updated: 2024/10/05 19:18:00 by madumerg ### ########.fr */
|
||||||
|
=======
|
||||||
|
/* Updated: 2024/11/06 11:50:38 by adjoly ### ########.fr */
|
||||||
|
>>>>>>> raycasting
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "cub3d.h"
|
#include "cub3d.h"
|
||||||
|
#include "game/game.h"
|
||||||
|
#include "game/settings.h"
|
||||||
|
#include "libft.h"
|
||||||
|
#include "mlx.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int loop(void *param)
|
||||||
|
{
|
||||||
|
t_render *render;
|
||||||
|
|
||||||
|
render = (t_render *)param;
|
||||||
|
mlx_destroy_image(render->mlx, render->img);
|
||||||
|
render->img = mlx_new_image(render->mlx, WINDOW_W, WINDOW_H);
|
||||||
|
render_frame(render);
|
||||||
|
mlx_put_image_to_window(render->mlx, render->win, render->img, 0, 0);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int ac, char **av)
|
int main(int ac, char **av)
|
||||||
{
|
{
|
||||||
t_pars pars;
|
t_render render;
|
||||||
|
t_map world;
|
||||||
|
t_player player;
|
||||||
|
|
||||||
pars = init_pars();
|
render.player = &player;
|
||||||
if (basics_check(ac, av, &pars) == 1)
|
render.world = &world;
|
||||||
{
|
world.celling = 0xffffa07a;
|
||||||
free_pars(&pars);
|
world.floor = 0xffadd8e6;
|
||||||
return (1);
|
world.arr = ft_split("11111 10001 10001 10001 11111", ' ');
|
||||||
}
|
world.size.x = 5;
|
||||||
free_pars(&pars);
|
world.size.y = 5;
|
||||||
|
player.coord.x = 2 * CHUNK_SIZE;
|
||||||
|
player.coord.y = 3 * CHUNK_SIZE;
|
||||||
|
player.direction = 0;
|
||||||
|
render.mlx = mlx_init();
|
||||||
|
render.win = mlx_new_window(render.mlx, WINDOW_W, WINDOW_H, "WTF");
|
||||||
|
render.img = mlx_new_image(render.mlx, WINDOW_W, WINDOW_H);
|
||||||
|
(void)ac;
|
||||||
|
(void)av;
|
||||||
|
mlx_on_event(render.mlx, render.win, MLX_KEYDOWN, key_hook, &render);
|
||||||
|
mlx_loop_hook(render.mlx, loop, &render);
|
||||||
|
mlx_loop(render.mlx);
|
||||||
|
mlx_destroy_image(render.mlx, render.img);
|
||||||
|
mlx_destroy_window(render.mlx, render.win);
|
||||||
|
mlx_destroy_display(render.mlx);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: madumerg <madumerg@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: madumerg <madumerg@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/09/12 12:44:52 by madumerg #+# #+# */
|
/* Created: 2024/09/12 12:44:52 by madumerg #+# #+# */
|
||||||
/* Updated: 2024/10/07 11:28:45 by madumerg ### ########.fr */
|
/* Updated: 2024/11/06 12:52:08 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
114
src/raycasting/dda.c
Normal file
114
src/raycasting/dda.c
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* dda.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/07 16:55:09 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2024/11/06 12:34:58 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "game/vectwo.h"
|
||||||
|
#include <game/settings.h>
|
||||||
|
#include <game/dda.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
void setup_dda_ver(t_player *play, t_ray *ray, t_dda *dda)
|
||||||
|
{
|
||||||
|
dda->s = true;
|
||||||
|
dda->h = false;
|
||||||
|
dda->distance.x = 100000;
|
||||||
|
ray->tan = tan(ray->angle);
|
||||||
|
if (cos(ray->angle) < -0.001)
|
||||||
|
{
|
||||||
|
ray->offset.x = -CHUNK_SIZE;
|
||||||
|
ray->offset.y = CHUNK_SIZE * ray->tan;
|
||||||
|
ray->pos.x = (((int)play->coord.x >> 6) << 6) - 0.001;
|
||||||
|
ray->pos.y = (play->coord.x - ray->pos.x) * ray->tan + play->coord.y;
|
||||||
|
}
|
||||||
|
else if (cos(ray->angle) > 0.001)
|
||||||
|
{
|
||||||
|
ray->offset.x = CHUNK_SIZE;
|
||||||
|
ray->offset.y = -CHUNK_SIZE * ray->tan;
|
||||||
|
ray->pos.x = (((int)play->coord.x >> 6) << 6) + CHUNK_SIZE;
|
||||||
|
ray->pos.y = (play->coord.x - ray->pos.x) * ray->tan + play->coord.y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup_dda_hor(t_player *play, t_ray *ray, t_dda *dda)
|
||||||
|
{
|
||||||
|
dda->h = true;
|
||||||
|
dda->s = true;
|
||||||
|
dda->distance.y = 100000;
|
||||||
|
ray->tan = 1 / tan(ray->angle);
|
||||||
|
if (sin(ray->angle) < -0.001)
|
||||||
|
{
|
||||||
|
ray->offset.y = CHUNK_SIZE;
|
||||||
|
ray->offset.x = -CHUNK_SIZE * ray->tan;
|
||||||
|
ray->pos.y = (((int)play->coord.y >> 6) << 6) + CHUNK_SIZE;
|
||||||
|
ray->pos.x = (play->coord.y - ray->pos.y) * ray->tan + play->coord.x;
|
||||||
|
}
|
||||||
|
else if (sin(ray->angle) > 0.001)
|
||||||
|
{
|
||||||
|
ray->offset.y = -CHUNK_SIZE;
|
||||||
|
ray->offset.x = CHUNK_SIZE * ray->tan;
|
||||||
|
ray->pos.y = (((int)play->coord.y >> 6) << 6) - 0.001;
|
||||||
|
ray->pos.x = (play->coord.y - ray->pos.y) * ray->tan + play->coord.x;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
dda->s = false;
|
||||||
|
ray->pos.x = play->coord.x;
|
||||||
|
ray->pos.y = play->coord.y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void dda_loop(t_dda *dda, t_ray *ray, t_map *map, t_player *play)
|
||||||
|
{
|
||||||
|
while (dda->s && dda->i) {
|
||||||
|
dda->map.x = (ray->pos.x / 64);
|
||||||
|
dda->map.y = (ray->pos.y / 64);
|
||||||
|
if (dda->map.x >= 0 && dda->map.x < map->size.x && dda->map.y >= 0 && \
|
||||||
|
dda->map.y < map->size.y && \
|
||||||
|
map->arr[(int)dda->map.y][(int)dda->map.x] == '1')
|
||||||
|
{
|
||||||
|
dda->i = 0;
|
||||||
|
if (dda->h)
|
||||||
|
dda->distance.y = vec2_dist(play->coord, ray->pos);
|
||||||
|
else
|
||||||
|
dda->distance.x = vec2_dist(play->coord, ray->pos);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
vec2_add(&ray->pos, ray->offset);
|
||||||
|
dda->i--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void dda_algo(t_render *render, t_dda *dda, t_ray *ray)
|
||||||
|
{
|
||||||
|
setup_dda_hor(render->player, ray, dda);
|
||||||
|
dda->i = render->world->size.y;
|
||||||
|
dda_loop(dda, ray, render->world, render->player);
|
||||||
|
dda->hori = ray->pos;
|
||||||
|
setup_dda_ver(render->player, ray, dda);
|
||||||
|
dda->i = render->world->size.x;
|
||||||
|
dda_loop(dda, ray, render->world, render->player);
|
||||||
|
dda->vert = ray->pos;
|
||||||
|
if (dda->distance.y < dda->distance.x)
|
||||||
|
{
|
||||||
|
ray->pos.x = fabsf(dda->hori.x);
|
||||||
|
ray->pos.y = fabsf(dda->hori.y);
|
||||||
|
ray->distance = dda->distance.y;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ray->pos.x = fabsf(dda->vert.x);
|
||||||
|
ray->pos.y = fabsf(dda->vert.y);
|
||||||
|
ray->distance = dda->distance.x;
|
||||||
|
}
|
||||||
|
}
|
53
src/raycasting/key_hook.c
Normal file
53
src/raycasting/key_hook.c
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* key_hook.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/09/11 16:37:56 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2024/11/02 15:50:23 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "game/settings.h"
|
||||||
|
#include "mlx.h"
|
||||||
|
#include <game/game.h>
|
||||||
|
#include <game/utils_math.h>
|
||||||
|
|
||||||
|
void change_direction(float speed, bool clockwise, t_player *player)
|
||||||
|
{
|
||||||
|
if (clockwise)
|
||||||
|
player->direction += speed;
|
||||||
|
else
|
||||||
|
player->direction -= speed;
|
||||||
|
fix_ang(&player->direction);
|
||||||
|
}
|
||||||
|
|
||||||
|
int key_hook(int key, void *param)
|
||||||
|
{
|
||||||
|
t_render *render;
|
||||||
|
|
||||||
|
render = (t_render *)param;
|
||||||
|
if (key == ESCAPE_KEY)
|
||||||
|
return (mlx_loop_end(render->mlx));
|
||||||
|
if (key == W_KEY)
|
||||||
|
{
|
||||||
|
render->player->coord.x += PLAYER_SPEED * \
|
||||||
|
cos(render->player->direction);
|
||||||
|
render->player->coord.y += PLAYER_SPEED * \
|
||||||
|
-sin(render->player->direction);
|
||||||
|
}
|
||||||
|
else if (key == S_KEY)
|
||||||
|
{
|
||||||
|
render->player->coord.x -= PLAYER_SPEED * \
|
||||||
|
cos(render->player->direction);
|
||||||
|
render->player->coord.y -= PLAYER_SPEED * \
|
||||||
|
-sin(render->player->direction);
|
||||||
|
}
|
||||||
|
else if (key == D_KEY)
|
||||||
|
change_direction(PLAYER_ROT_SPEED, false, render->player);
|
||||||
|
else if (key == A_KEY)
|
||||||
|
change_direction(PLAYER_ROT_SPEED, true, render->player);
|
||||||
|
return (0);
|
||||||
|
}
|
@ -1,31 +1,21 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* event_handler.c :+: :+: :+: */
|
/* maths.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/09/11 16:37:56 by adjoly #+# #+# */
|
/* Created: 2024/10/29 10:05:15 by adjoly #+# #+# */
|
||||||
/* Updated: 2024/09/12 14:40:22 by adjoly ### ########.fr */
|
/* Updated: 2024/11/01 17:25:32 by madumerg ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <math.h>
|
||||||
#include "mlx.h"
|
|
||||||
#include "game.h"
|
|
||||||
|
|
||||||
int key_hook(int key, t_cub *cub)
|
void fix_ang(float *ang)
|
||||||
{
|
{
|
||||||
(void)mlx;
|
if (*ang < 0)
|
||||||
if (key == ESCAPE_KEY)
|
*ang += 2 * M_PI;
|
||||||
mlx_loop_end(cub->mlx);
|
if (*ang > 2 * M_PI)
|
||||||
if (key == W_KEY)
|
*ang -= 2 * M_PI;
|
||||||
printf("UP\n");
|
|
||||||
if (key == S_KEY)
|
|
||||||
printf("DOWN\n");
|
|
||||||
if (key == D_KEY)
|
|
||||||
printf("RIGHT\n");
|
|
||||||
if (key == A_KEY)
|
|
||||||
printf("LEFT\n");
|
|
||||||
return (0);
|
|
||||||
}
|
}
|
99
src/raycasting/renderer.c
Normal file
99
src/raycasting/renderer.c
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* renderer.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/28 15:48:39 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2024/11/06 11:59:23 by madumerg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "game/utils_math.h"
|
||||||
|
#include "mlx.h"
|
||||||
|
#include <game/dda.h>
|
||||||
|
#include <game/settings.h>
|
||||||
|
#include <game/game.h>
|
||||||
|
|
||||||
|
void draw_floor(t_render *render)
|
||||||
|
{
|
||||||
|
size_t x;
|
||||||
|
size_t y;
|
||||||
|
|
||||||
|
y = WINDOW_H / 2;
|
||||||
|
while (y < WINDOW_H)
|
||||||
|
{
|
||||||
|
x = 0;
|
||||||
|
while (x < WINDOW_W)
|
||||||
|
{
|
||||||
|
mlx_set_image_pixel(render->mlx, render->img, x, y, \
|
||||||
|
render->world->floor);
|
||||||
|
x++;
|
||||||
|
}
|
||||||
|
y++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw_celling(t_render *render)
|
||||||
|
{
|
||||||
|
size_t x;
|
||||||
|
size_t y;
|
||||||
|
|
||||||
|
y = 0;
|
||||||
|
while (y < WINDOW_H / 2)
|
||||||
|
{
|
||||||
|
x = 0;
|
||||||
|
while (x < WINDOW_W)
|
||||||
|
{
|
||||||
|
mlx_set_image_pixel(render->mlx, render->img, x, y, \
|
||||||
|
render->world->celling);
|
||||||
|
x++;
|
||||||
|
}
|
||||||
|
y++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_line(t_render *render, t_ray *ray, int x)
|
||||||
|
{
|
||||||
|
int line_size;
|
||||||
|
int line_stop;
|
||||||
|
int line_start;
|
||||||
|
|
||||||
|
line_size = (CHUNK_SIZE * WINDOW_H) / ray->distance;
|
||||||
|
line_start = -line_size / 2 + WINDOW_H / 2;
|
||||||
|
line_stop = line_size / 2 + WINDOW_H / 2;
|
||||||
|
if (line_start < 0)
|
||||||
|
line_start = 0;
|
||||||
|
if (line_stop > WINDOW_H)
|
||||||
|
line_stop = WINDOW_H - 1;
|
||||||
|
while (line_start < line_stop)
|
||||||
|
{
|
||||||
|
mlx_set_image_pixel(render->mlx, render->img, x, line_start, WHITE);
|
||||||
|
line_start++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void render_frame(t_render *render)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
t_dda dda;
|
||||||
|
t_ray ray;
|
||||||
|
float ang;
|
||||||
|
|
||||||
|
i = WINDOW_W;
|
||||||
|
ray.angle = render->player->direction - ((M_PI / 180) * ((float)FOV / 2));
|
||||||
|
fix_ang(&ray.angle);
|
||||||
|
draw_celling(render);
|
||||||
|
draw_floor(render);
|
||||||
|
while (i > -1)
|
||||||
|
{
|
||||||
|
dda_algo(render, &dda, &ray);
|
||||||
|
ang = cos(render->player->direction - ray.angle);
|
||||||
|
ray.distance *= ang;
|
||||||
|
print_line(render, &ray, i);
|
||||||
|
ray.angle += (M_PI / 180) / ((float)WINDOW_W / FOV);
|
||||||
|
fix_ang(&ray.angle);
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
}
|
41
src/raycasting/vectwo.c
Normal file
41
src/raycasting/vectwo.c
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* vectwo.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/26 15:47:29 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2024/11/02 15:42:28 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "game/vectwo.h"
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
float vec2_dist(t_vec2 first, t_vec2 second)
|
||||||
|
{
|
||||||
|
return (sqrt(powf(second.x - first.x, 2) + powf(second.y - first.y, 2)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void vec2_add(t_vec2 *first, t_vec2 second)
|
||||||
|
{
|
||||||
|
first->x += second.x;
|
||||||
|
first->y += second.y;
|
||||||
|
}
|
||||||
|
// to be removed
|
||||||
|
void vec2_sub(t_vec2 *first, t_vec2 second)
|
||||||
|
{
|
||||||
|
first->x -= second.x;
|
||||||
|
first->y -= second.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
t_vec2 vec2_mul_ret(t_vec2 vec, float n)
|
||||||
|
{
|
||||||
|
t_vec2 ret;
|
||||||
|
|
||||||
|
ret.x = vec.x * n;
|
||||||
|
ret.y = vec.y * n;
|
||||||
|
return (ret);
|
||||||
|
}
|
Reference in New Issue
Block a user