「🏗️」 wip: Map is printing yayyy
This commit is contained in:
Submodule MacroLibX updated: 5a09ebb179...3dd68f9942
14
Makefile
14
Makefile
@ -20,15 +20,15 @@ SRCS = $(shell find src -name *.c)
|
||||
|
||||
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.c=.o))
|
||||
|
||||
FLAGS = -Wall -Werror -Wextra -g -lm -lSDL2
|
||||
FLAGS = -Wall -Werror -Wextra -g -lm -lSDL2 -MMD -MP
|
||||
|
||||
LIB = libft/libft.a \
|
||||
MacroLibX/libmlx.so
|
||||
|
||||
$(NAME): $(OBJS)
|
||||
@make -sj $(nproc) -C $(LIBFT_DIR)
|
||||
@make -sj -C $(LIBFT_DIR)
|
||||
@echo "✅ Libft compiled"
|
||||
@make -sj $(nproc) -C $(MACRO_DIR) > /dev/null
|
||||
@make -sj -C $(MACRO_DIR)
|
||||
@echo "✅ MacroLibX compiled"
|
||||
@$(CC) $(FLAGS) $(OBJS) $(LIB) -o $(NAME)
|
||||
@echo "✅ Compiled"
|
||||
@ -40,14 +40,14 @@ $(OBJSDIR)%.o: %.c
|
||||
all: $(NAME)
|
||||
|
||||
clean:
|
||||
@make -s -C $(LIBFT_DIR) clean
|
||||
@make -s -C $(MACRO_DIR) clean > /dev/null
|
||||
@make -sC $(LIBFT_DIR) clean
|
||||
@make -sC $(MACRO_DIR) clean > /dev/null
|
||||
@rm -f $(OBJS)
|
||||
|
||||
fclean: clean
|
||||
@make -s -C $(LIBFT_DIR) fclean
|
||||
@make -sC $(LIBFT_DIR) fclean
|
||||
@echo "🧹 Libft Cleaned"
|
||||
@make -s -C $(MACRO_DIR) fclean > /dev/null
|
||||
@make -sC $(MACRO_DIR) fclean > /dev/null
|
||||
@echo "🧹 MacroLibX Cleaned"
|
||||
@rm -f $(NAME)
|
||||
@rm -Rf $(OBJSDIR)
|
||||
|
8
asdf
Normal file
8
asdf
Normal file
@ -0,0 +1,8 @@
|
||||
# 1 "<stdin>"
|
||||
# 1 "<built-in>" 1
|
||||
# 1 "<built-in>" 3
|
||||
# 384 "<built-in>" 3
|
||||
# 1 "<command line>" 1
|
||||
# 1 "<built-in>" 2
|
||||
# 1 "<stdin>" 2
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/11 16:38:40 by adjoly #+# #+# */
|
||||
/* Updated: 2024/09/17 10:47:26 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/09/24 16:50:56 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -15,6 +15,15 @@
|
||||
|
||||
# include "parsing.h"
|
||||
# include <stdint.h>
|
||||
# include <math.h>
|
||||
# include <stdbool.h>
|
||||
# include <unistd.h>
|
||||
|
||||
typedef struct s_player
|
||||
{
|
||||
t_coord coords;
|
||||
double direction;
|
||||
} t_player;
|
||||
|
||||
typedef struct s_cub
|
||||
{
|
||||
@ -23,7 +32,7 @@ typedef struct s_cub
|
||||
void *sprites;
|
||||
void *img;
|
||||
char **map;
|
||||
t_coord p_coord;
|
||||
t_player player;
|
||||
} t_cub;
|
||||
|
||||
# define ESCAPE_KEY 41
|
||||
@ -34,6 +43,9 @@ typedef struct s_cub
|
||||
# define WHITE 0xFFFFFFFF
|
||||
# define WINDOW_Y 900
|
||||
# define WINDOW_X 1600
|
||||
# define PLAYER_ROT_SPEED (2 * M_PI) / 8
|
||||
# define PLAYER_SPEED 3
|
||||
# define MAP_CHUNK_SIZE 64
|
||||
|
||||
/**
|
||||
* @brief This function is used to handle keypress
|
||||
@ -52,6 +64,36 @@ int key_hook(int key, void *param);
|
||||
* thing
|
||||
*
|
||||
*/
|
||||
void *get_player_image(t_cub *cub, uint8_t key_pressed);
|
||||
void get_player_image(t_cub *cub, uint8_t key_pressed);
|
||||
|
||||
/**
|
||||
* @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(double speed, bool clockwise, t_player *player);
|
||||
|
||||
/**
|
||||
* @brief Function used to draw a square
|
||||
*
|
||||
* @param cub The adress of a t_cub struct
|
||||
* @param coord The coordinate of the printed square
|
||||
* @param size The size of the printed square
|
||||
* @param color The color of the printed square
|
||||
*
|
||||
*/
|
||||
void draw_square(t_cub *cub, t_coord coord, uint16_t size, int color);
|
||||
|
||||
/**
|
||||
* @brief Function used to draw the map
|
||||
*
|
||||
* @param cub The address of the t_cub struct
|
||||
*/
|
||||
void print_map(t_cub *cub);
|
||||
#endif
|
||||
|
19
src/cub3d.c
19
src/cub3d.c
@ -6,7 +6,7 @@
|
||||
/* By: madumerg <madumerg@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/04 16:58:27 by madumerg #+# #+# */
|
||||
/* Updated: 2024/09/17 10:36:01 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/09/24 18:31:05 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -18,13 +18,22 @@ int main(int ac, char **av)
|
||||
{
|
||||
t_cub cub;
|
||||
|
||||
cub.map = (char *[]){
|
||||
"1111111\0",
|
||||
"1000101\0",
|
||||
"1000101\0",
|
||||
"1000001\0",
|
||||
"1111111\0",
|
||||
NULL
|
||||
};
|
||||
(void)ac;
|
||||
(void)av;
|
||||
cub.mlx = mlx_init();
|
||||
cub.win = mlx_new_window(cub.mlx, 400, 400, "WTF");
|
||||
cub.p_coord.x = 100;
|
||||
cub.p_coord.y = 100;
|
||||
cub.img = get_player_image(&cub, 0);
|
||||
cub.win = mlx_new_window(cub.mlx, 600, 600, "WTF");
|
||||
cub.player.coords.x = 100;
|
||||
cub.player.coords.y = 100;
|
||||
cub.img = mlx_new_image(cub.mlx, 600, 600);
|
||||
get_player_image(&cub, 0);
|
||||
mlx_put_image_to_window(cub.mlx, cub.win, cub.img, 0, 0);
|
||||
mlx_on_event(cub.mlx, cub.win, MLX_KEYDOWN, key_hook, &cub);
|
||||
mlx_loop(cub.mlx);
|
||||
|
49
src/raycasting/get_player_image.c
Normal file
49
src/raycasting/get_player_image.c
Normal file
@ -0,0 +1,49 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_player_image.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/13 11:05:02 by adjoly #+# #+# */
|
||||
/* Updated: 2024/09/24 14:40:23 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "game.h"
|
||||
#include "mlx.h"
|
||||
#include "stdio.h"
|
||||
|
||||
void change_direction(double speed, bool clockwise, t_player *player)
|
||||
{
|
||||
if (clockwise)
|
||||
player->direction -= speed;
|
||||
else
|
||||
player->direction += speed;
|
||||
if (player->direction >= 2 * M_PI)
|
||||
player->direction -= 2 * M_PI;
|
||||
else if (player->direction < 0)
|
||||
player->direction += 2 * M_PI;
|
||||
printf("%f\n", player->direction);
|
||||
}
|
||||
|
||||
void get_player_image(t_cub *cub, uint8_t key_pressed)
|
||||
{
|
||||
if (key_pressed == W_KEY)
|
||||
{
|
||||
cub->player.coords.x += PLAYER_SPEED * cos(cub->player.direction);
|
||||
cub->player.coords.y += PLAYER_SPEED * sin(cub->player.direction);
|
||||
}
|
||||
else if (key_pressed == S_KEY)
|
||||
{
|
||||
cub->player.coords.x -= PLAYER_SPEED * cos(cub->player.direction);
|
||||
cub->player.coords.y -= PLAYER_SPEED * sin(cub->player.direction);
|
||||
}
|
||||
else if (key_pressed == D_KEY)
|
||||
change_direction(PLAYER_ROT_SPEED, false, &cub->player);
|
||||
else if (key_pressed == A_KEY)
|
||||
change_direction(PLAYER_ROT_SPEED, true, &cub->player);
|
||||
print_map(cub);
|
||||
draw_square(cub, (t_coord){cub->player.coords.x - 2, cub->player.coords.y - 2}, \
|
||||
5, WHITE);
|
||||
}
|
@ -1,32 +1,31 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* player_print.c :+: :+: :+: */
|
||||
/* draw_square.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/13 11:05:02 by adjoly #+# #+# */
|
||||
/* Updated: 2024/09/17 10:30:44 by adjoly ### ########.fr */
|
||||
/* Created: 2024/09/17 11:09:52 by adjoly #+# #+# */
|
||||
/* Updated: 2024/09/23 14:05:54 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "game.h"
|
||||
#include <stdint.h>
|
||||
#include "mlx.h"
|
||||
|
||||
void *get_player_image(t_cub *cub, uint8_t key_pressed)
|
||||
void draw_square(t_cub *cub, t_coord coord, uint16_t size, int color)
|
||||
{
|
||||
void *img;
|
||||
t_coord start;
|
||||
|
||||
img = mlx_new_image(cub->mlx, WINDOW_X, WINDOW_Y);
|
||||
if (key_pressed == W_KEY)
|
||||
cub->p_coord.y--;
|
||||
else if (key_pressed == S_KEY)
|
||||
cub->p_coord.y++;
|
||||
else if (key_pressed == D_KEY)
|
||||
cub->p_coord.x++;
|
||||
else if (key_pressed == A_KEY)
|
||||
cub->p_coord.x--;
|
||||
mlx_set_image_pixel(cub->mlx, img, cub->p_coord.x, cub->p_coord.y, WHITE);
|
||||
return (img);
|
||||
start = coord;
|
||||
while (coord.x - start.x <= size)
|
||||
{
|
||||
coord.y = start.y;
|
||||
while (coord.y - start.y <= size)
|
||||
{
|
||||
mlx_set_image_pixel(cub->mlx, cub->img, coord.x, coord.y, color);
|
||||
coord.y++;
|
||||
}
|
||||
coord.x++;
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/11 16:37:56 by adjoly #+# #+# */
|
||||
/* Updated: 2024/09/17 10:33:35 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/09/23 14:41:31 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -29,7 +29,8 @@ int key_hook(int key, void *param)
|
||||
{
|
||||
mlx_clear_window(cub->mlx, cub->win);
|
||||
mlx_destroy_image(cub->mlx, cub->img);
|
||||
cub->img = get_player_image(cub, key);
|
||||
cub->img = mlx_new_image(cub->mlx, 600, 600);
|
||||
get_player_image(cub, key);
|
||||
mlx_put_image_to_window(cub->mlx, cub->win, cub->img, 0, 0);
|
||||
}
|
||||
return (0);
|
||||
|
36
src/raycasting/print_map.c
Normal file
36
src/raycasting/print_map.c
Normal file
@ -0,0 +1,36 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* print_map.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/23 14:09:17 by adjoly #+# #+# */
|
||||
/* Updated: 2024/09/23 14:41:51 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "game.h"
|
||||
#include "mlx.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void print_map(t_cub *cub)
|
||||
{
|
||||
size_t i;
|
||||
size_t j;
|
||||
|
||||
i = 0;
|
||||
while (cub->map[i])
|
||||
{
|
||||
j = 0;
|
||||
while (cub->map[i][j])
|
||||
{
|
||||
if (cub->map[i][j] == '1')
|
||||
{
|
||||
draw_square(cub, (t_coord){j * MAP_CHUNK_SIZE, i * MAP_CHUNK_SIZE}, MAP_CHUNK_SIZE, WHITE);
|
||||
}
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user