diff --git a/.gitignore b/.gitignore index dd7aac1..4bfcdb2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ *.d *.a cub3D -build/ \ No newline at end of file +build/ +.cache/ \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..245caf4 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,26 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Debug Run Debug", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/build/debug/cub3D", // Replace with your executable path + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "miDebuggerPath": "/usr/bin/gdb", // Adjust if gdb is in a different location + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ], + "preLaunchTask": "Debug" + }, + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7f7aa60 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "files.associations": { + "game.h": "c", + "cub3d.h": "c", + "screen.h": "c", + "mlx42.h": "c" + } +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..a7f1a0b --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,41 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "Debug", + "type": "shell", + "command": "make", + "args": ["debug"], + "group": { + "kind": "build", + "isDefault": true + }, + "problemMatcher": [], + "detail": "Runs the make target for run_debug" + }, + { + "label": "Run ASAN", + "type": "shell", + "command": "make", + "args": ["run_asan"], + "group": { + "kind": "build", + "isDefault": false + }, + "problemMatcher": [], + "detail": "Runs the make target for run_asan" + }, + { + "label": "Make Run", + "type": "shell", + "command": "make", + "args": ["run"], + "group": { + "kind": "build", + "isDefault": false + }, + "problemMatcher": [], + "detail": "Runs the make run command" + } + ] +} \ No newline at end of file diff --git a/build/release/cub3D b/build/release/cub3D deleted file mode 100755 index e8d560d..0000000 Binary files a/build/release/cub3D and /dev/null differ diff --git a/compile_commands.json b/compile_commands.json deleted file mode 100644 index 6d1b83f..0000000 --- a/compile_commands.json +++ /dev/null @@ -1,7 +0,0 @@ -[ - { - "command": "cc -c -Wall -Werror -Werror -O2 -I./inc -I./lib/libft/inc -I./lib/MLX42/include/MLX42 -o build/release/obj/main.o src/main.c", - "directory": "/home/whaffman/Projects/cub3d", - "file": "/home/whaffman/Projects/cub3d/src/main.c" - } -] \ No newline at end of file diff --git a/compile_flags.txt b/compile_flags.txt new file mode 100644 index 0000000..1e38c10 --- /dev/null +++ b/compile_flags.txt @@ -0,0 +1,6 @@ +-I +./inc +-I +./lib/libft/inc +-I +./lib/MLX42/include/MLX42 \ No newline at end of file diff --git a/inc/allowed.h b/inc/allowed.h new file mode 100644 index 0000000..f665beb --- /dev/null +++ b/inc/allowed.h @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* allowed.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/15 15:31:54 by qmennen #+# #+# */ +/* Updated: 2025/04/15 15:57:15 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ALLOWED_H +# define ALLOWED_H + +# include +# include +# include +# include +# include +# include +#include +# include // TODO: Check if this is allowed? +#endif diff --git a/inc/cub3d.h b/inc/cub3d.h index 3ccdb7d..e8644a8 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -6,7 +6,7 @@ /* By: qmennen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */ -/* Updated: 2025/04/15 12:51:19 by qmennen ### ########.fr */ +/* Updated: 2025/04/15 16:53:27 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,48 +15,28 @@ # include # include +# include +# include "types.h" +# include "errors.h" +# include "game.h" +# include "screen.h" +# include "render.h" -typedef enum TILE -{ - TILE_VOID = -1, - TILE_EMPTY = 0, - TILE_WALL = 1, - TILE_PLAYER = 2, -} t_tile; +# define FAILURE 0 +# define SUCCESS 1 -typedef struct s_vec2 -{ - float x; - float y; -} t_vec2; +# define WIDTH 1280 +# define HEIGHT 720 +# define TITLE "Cub3D" -typedef struct s_player -{ - t_vec2 pos; - float speed; - float angle; - float fov; -} t_player; +# define RESET "\033[0m" +# define BLACK "\033[0;30m" +# define RED "\033[0;31m" +# define GREEN "\033[0;32m" +# define YELLOW "\033[0;33m" +# define BLUE "\033[0;34m" +# define MAGENTA "\033[0;35m" +# define CYAN "\033[0;36m" +# define WHITE "\033[0;37m" -typedef struct s_map -{ - unsigned int width; - unsigned int height; - t_tile **grid; -} t_map; - -typedef struct s_screen -{ - mlx_t *mlx; - mlx_image_t *img; - unsigned int width; - unsigned int height; -} t_screen; - -typedef struct s_game -{ - t_map *map; - t_player *player; -} t_game; - -#endif \ No newline at end of file +#endif diff --git a/inc/errors.h b/inc/errors.h new file mode 100644 index 0000000..7337d4d --- /dev/null +++ b/inc/errors.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* errors.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/15 15:42:55 by qmennen #+# #+# */ +/* Updated: 2025/04/15 15:59:42 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ERRORS_H +# define ERRORS_H + +# include "cub3d.h" + +void game_error(t_game *game, const char *msg, int mlx_error); + +#endif diff --git a/inc/game.h b/inc/game.h new file mode 100644 index 0000000..0d2ed6d --- /dev/null +++ b/inc/game.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* game.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/15 15:46:16 by qmennen #+# #+# */ +/* Updated: 2025/04/15 17:21:36 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GAME_H +# define GAME_H + +# include "cub3d.h" + +int game_create(t_game **game); +void game_loop(void *param); +void game_terminate(t_game *game); + +#endif diff --git a/inc/render.h b/inc/render.h new file mode 100644 index 0000000..07ae0ce --- /dev/null +++ b/inc/render.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* render.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/15 16:28:16 by qmennen #+# #+# */ +/* Updated: 2025/04/15 16:30:51 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef RENDER_H +# define RENDER_H + +# include "cub3d.h" + +void render_clear(t_screen *screen); + +#endif diff --git a/inc/screen.h b/inc/screen.h new file mode 100644 index 0000000..960982e --- /dev/null +++ b/inc/screen.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* screen.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/15 15:30:34 by qmennen #+# #+# */ +/* Updated: 2025/04/15 16:48:24 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SCREEN_H +# define SCREEN_H + +#include "cub3d.h" + +t_screen *screen_create(); +void screen_center(t_screen *screen); + +#endif diff --git a/inc/types.h b/inc/types.h new file mode 100644 index 0000000..2bed026 --- /dev/null +++ b/inc/types.h @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* types.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */ +/* Updated: 2025/04/15 15:53:29 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TYPES_H +# define TYPES_H + +# include "cub3d.h" + +typedef enum TILE +{ + TILE_VOID = -1, + TILE_EMPTY = 0, + TILE_WALL = 1, + TILE_PLAYER = 2, +} t_tile; + +typedef struct s_vec2 +{ + float x; + float y; +} t_vec2; + +typedef struct s_player +{ + t_vec2 pos; + float speed; + float angle; + float fov; +} t_player; + +typedef struct s_map +{ + unsigned int width; + unsigned int height; + t_tile **grid; +} t_map; + +typedef struct s_screen +{ + mlx_t *mlx; + mlx_image_t *img; + unsigned int width; + unsigned int height; +} t_screen; + +typedef struct s_game +{ + t_map *map; + t_player *player; + t_screen *screen; +} t_game; + +#endif diff --git a/src/errors.c b/src/errors.c new file mode 100644 index 0000000..b7669c4 --- /dev/null +++ b/src/errors.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* errors.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/15 15:40:14 by qmennen #+# #+# */ +/* Updated: 2025/04/15 16:10:57 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "MLX42.h" +#include "cub3d.h" + +void game_error(t_game *game, const char *msg, int mlx_error) +{ + if (mlx_error) + printf(RED"%s\n"RESET, mlx_strerror(mlx_errno)); + else if (msg == NULL) + printf(RED"%s\n"RESET, strerror(errno)); + else + printf(RED"%s\n"RESET, msg); + game_terminate(game); + exit(FAILURE); +} diff --git a/src/game.c b/src/game.c new file mode 100644 index 0000000..fb01e68 --- /dev/null +++ b/src/game.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* game.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */ +/* Updated: 2025/04/15 17:23:29 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "game.h" + +int game_create(t_game **game) +{ + *game = malloc(sizeof(t_game)); + if (!game) + { + printf("malloc: game failed\n"); + exit(FAILURE); + } + (*game)->player = NULL; + (*game)->map = NULL; + (*game)->screen = screen_create(); + if ((*game)->screen == NULL) + return (FAILURE); + if (((*game)->screen->mlx == NULL) || ((*game)->screen->img == NULL)) + return (FAILURE); + mlx_image_to_window((*game)->screen->mlx, (*game)->screen->img, 0, 0); + return (SUCCESS); +} + +void game_loop(void *param) +{ + t_game *game; + + game = (t_game *)param; + render_clear(game->screen); +} + +void game_terminate(t_game *game) +{ + if (game->screen) + { + mlx_close_window(game->screen->mlx); + mlx_delete_image(game->screen->mlx, game->screen->img); + mlx_terminate(game->screen->mlx); + free(game->screen); + } + if (game->player) + free(game->player); + if (game->map) + free(game->map); + free(game); +} diff --git a/src/main.c b/src/main.c index 97a8a43..b27cb00 100644 --- a/src/main.c +++ b/src/main.c @@ -1,108 +1,29 @@ -#include -#include -#include -#include +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/15 16:01:29 by qmennen #+# #+# */ +/* Updated: 2025/04/15 17:23:45 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ -#include "MLX42.h" +# include "cub3d.h" -#define WIDTH 1024 -#define HEIGHT 1024 - -typedef struct s_things t_things; -typedef struct s_things +int main(void) { - mlx_t* mlx; - mlx_image_t* img; -} t_things; + t_game *game; -// Exit the program as failure. -static void ft_error(void) -{ - fprintf(stderr, "%s", mlx_strerror(mlx_errno)); - exit(EXIT_FAILURE); -} - -static int check_bounds(int x, int y, int width, int height) -{ - if (x < 0 || x >= width || y < 0 || y >= height) - return (1); - return (0); -} - -static void draw_circle(mlx_image_t* img, int x, int y, int radius, uint32_t color) -{ - for (int i = -radius; i <= radius; i++) - { - for (int j = -radius; j <= radius; j++) - { - if (i * i + j * j <= radius * radius) - { - if (check_bounds(x + i, y + j, img->width, img->height)) - continue; - mlx_put_pixel(img, x + i, y + j, color << 8 |(int)(((float)radius * radius - i * i - j*j ) / (float)(radius * radius) * 255)); - } - } + game = NULL; + if (! game_create(&game)){ + game_error(game, "Failed to create game", 0); + return (EXIT_FAILURE); } -} - -static void draw_random_circle(mlx_image_t* img) -{ - int x = rand() % img->width; - int y = rand() % img->height; - int radius = rand() % 50 + 10; // Random radius between 10 and 60 - uint32_t color = (rand() % 256 << 16) | (rand() % 256 << 8) | (rand() % 256); - draw_circle(img, x, y, radius, color); -} - - -// Print the window width and height. -static void ft_hook(void* param) -{ - t_things *arg = param; - const mlx_t* mlx = arg->mlx; - mlx_image_t* img = arg->img; - static float last_time; - - last_time += mlx->delta_time; - // printf("delta_time: %f last_time: %f\n", mlx->delta_time, last_time); - if (last_time > 0.05) - { - - draw_random_circle(img); - printf("FPS: %d\n", (int)(1.0 / last_time)); - last_time = 0; - } -} - -int32_t main(void) -{ - t_things *things; - // MLX allows you to define its core behaviour before startup. - - things = malloc(sizeof(t_things)); - if (!things) - ft_error(); - - mlx_t* mlx = mlx_init(WIDTH, HEIGHT, "42Balls", true); - if (!mlx) - ft_error(); - - things->mlx = mlx; - /* Do stuff */ - - // Create and display the image. - mlx_image_t* img = mlx_new_image(mlx, WIDTH, HEIGHT); - if (!img || (mlx_image_to_window(mlx, img, 0, 0) < 0)) - ft_error(); - things->img = img; - - // Register a hook and pass mlx as an optional param. - // NOTE: Do this before calling mlx_loop! - mlx_loop_hook(mlx, ft_hook, things); - mlx_loop(mlx); - mlx_delete_image(mlx, img); - mlx_terminate(mlx); - free(things); - + screen_center(game->screen); + mlx_loop_hook(game->screen->mlx, game_loop, game); + mlx_loop(game->screen->mlx); + game_terminate(game); return (EXIT_SUCCESS); -} \ No newline at end of file +} diff --git a/src/render.c b/src/render.c new file mode 100644 index 0000000..420ba89 --- /dev/null +++ b/src/render.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* render.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/15 16:28:10 by qmennen #+# #+# */ +/* Updated: 2025/04/15 16:38:13 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "render.h" +#include "MLX42.h" + +void render_clear(t_screen *screen) +{ + int i; + + i = 0; + while (i++ < screen->width * screen->height) + mlx_put_pixel(screen->img, i % screen->width, i / screen->width, 0x212121ff); +} diff --git a/src/screen.c b/src/screen.c new file mode 100644 index 0000000..2ffdc25 --- /dev/null +++ b/src/screen.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* screen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/15 15:30:27 by qmennen #+# #+# */ +/* Updated: 2025/04/15 16:47:25 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "screen.h" +#include "MLX42.h" +#include + +t_screen *screen_create() +{ + t_screen *screen; + mlx_image_t *img; + mlx_t *mlx; + + screen = malloc(sizeof(t_screen)); + if (!screen) + return (NULL); + screen->width = WIDTH; + screen->height = HEIGHT; + mlx = mlx_init(WIDTH, HEIGHT, TITLE, false); + screen->mlx = mlx; + img = mlx_new_image(screen->mlx, WIDTH, HEIGHT); + screen->img = img; + return (screen); +} + +void screen_center(t_screen *screen) +{ + int m_width; + int m_height; + + m_width = 0; + m_height = 0; + mlx_get_monitor_size(0, &m_width, &m_height); + if (m_width == 0 || m_height == 0) + { + printf(RED"Failed to retrieve monitor size to center window\n"RESET); + return ; + } + mlx_set_window_pos(screen->mlx, (m_width - screen->width) / 2, (m_height - screen->height) / 2); +}