1
0

🏗️」 wip: Map is printing yayyy

This commit is contained in:
2024-09-24 18:32:34 +02:00
parent 0a91d7f2c6
commit 3e1e619497
9 changed files with 184 additions and 40 deletions

View File

@ -20,15 +20,15 @@ SRCS = $(shell find src -name *.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 -lm -lSDL2 -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) > /dev/null @make -sj -C $(MACRO_DIR)
@echo "✅ MacroLibX compiled" @echo "✅ MacroLibX compiled"
@$(CC) $(FLAGS) $(OBJS) $(LIB) -o $(NAME) @$(CC) $(FLAGS) $(OBJS) $(LIB) -o $(NAME)
@echo "✅ Compiled" @echo "✅ Compiled"
@ -40,14 +40,14 @@ $(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)

8
asdf Normal file
View 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

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/11 16:38:40 by adjoly #+# #+# */ /* 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,15 +15,24 @@
# include "parsing.h" # include "parsing.h"
# include <stdint.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 typedef struct s_cub
{ {
void *mlx; void *mlx;
void *win; void *win;
void *sprites; void *sprites;
void *img; void *img;
char **map; char **map;
t_coord p_coord; t_player player;
} t_cub; } t_cub;
# define ESCAPE_KEY 41 # define ESCAPE_KEY 41
@ -34,6 +43,9 @@ typedef struct s_cub
# define WHITE 0xFFFFFFFF # define WHITE 0xFFFFFFFF
# define WINDOW_Y 900 # define WINDOW_Y 900
# define WINDOW_X 1600 # 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 * @brief This function is used to handle keypress
@ -52,6 +64,36 @@ int key_hook(int key, void *param);
* thing * 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 #endif

View File

@ -6,7 +6,7 @@
/* 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 #+# #+# */
/* 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; t_cub cub;
cub.map = (char *[]){
"1111111\0",
"1000101\0",
"1000101\0",
"1000001\0",
"1111111\0",
NULL
};
(void)ac; (void)ac;
(void)av; (void)av;
cub.mlx = mlx_init(); cub.mlx = mlx_init();
cub.win = mlx_new_window(cub.mlx, 400, 400, "WTF"); cub.win = mlx_new_window(cub.mlx, 600, 600, "WTF");
cub.p_coord.x = 100; cub.player.coords.x = 100;
cub.p_coord.y = 100; cub.player.coords.y = 100;
cub.img = get_player_image(&cub, 0); 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_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_on_event(cub.mlx, cub.win, MLX_KEYDOWN, key_hook, &cub);
mlx_loop(cub.mlx); mlx_loop(cub.mlx);

View 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);
}

View File

@ -1,32 +1,31 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* player_print.c :+: :+: :+: */ /* draw_square.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/13 11:05:02 by adjoly #+# #+# */ /* Created: 2024/09/17 11:09:52 by adjoly #+# #+# */
/* Updated: 2024/09/17 10:30:44 by adjoly ### ########.fr */ /* Updated: 2024/09/23 14:05:54 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "game.h" #include "game.h"
#include <stdint.h>
#include "mlx.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); start = coord;
if (key_pressed == W_KEY) while (coord.x - start.x <= size)
cub->p_coord.y--; {
else if (key_pressed == S_KEY) coord.y = start.y;
cub->p_coord.y++; while (coord.y - start.y <= size)
else if (key_pressed == D_KEY) {
cub->p_coord.x++; mlx_set_image_pixel(cub->mlx, cub->img, coord.x, coord.y, color);
else if (key_pressed == A_KEY) coord.y++;
cub->p_coord.x--; }
mlx_set_image_pixel(cub->mlx, img, cub->p_coord.x, cub->p_coord.y, WHITE); coord.x++;
return (img); }
} }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/11 16:37:56 by adjoly #+# #+# */ /* 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_clear_window(cub->mlx, cub->win);
mlx_destroy_image(cub->mlx, cub->img); 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); mlx_put_image_to_window(cub->mlx, cub->win, cub->img, 0, 0);
} }
return (0); return (0);

View 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++;
}
}