basic structure

This commit is contained in:
Quinten Mennen 2025-04-15 17:29:54 +02:00
parent 2350a85f9a
commit 1c1ee35ca0
19 changed files with 451 additions and 152 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
*.a
cub3D
build/
.cache/

26
.vscode/launch.json vendored Normal file
View File

@ -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"
},
]
}

8
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,8 @@
{
"files.associations": {
"game.h": "c",
"cub3d.h": "c",
"screen.h": "c",
"mlx42.h": "c"
}
}

41
.vscode/tasks.json vendored Normal file
View File

@ -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"
}
]
}

Binary file not shown.

View File

@ -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"
}
]

6
compile_flags.txt Normal file
View File

@ -0,0 +1,6 @@
-I
./inc
-I
./lib/libft/inc
-I
./lib/MLX42/include/MLX42

24
inc/allowed.h Normal file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* allowed.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <fcntl.h>
# include <unistd.h>
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <math.h>
#include <sys/time.h>
# include <errno.h> // TODO: Check if this is allowed?
#endif

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <MLX42.h>
# include <libft.h>
# include <allowed.h>
# 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;
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;
# 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"
#endif

20
inc/errors.h Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* errors.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

22
inc/game.h Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* game.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

20
inc/render.h Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

21
inc/screen.h Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* screen.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

62
inc/types.h Normal file
View File

@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* types.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

26
src/errors.c Normal file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* errors.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

56
src/game.c Normal file
View File

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* game.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -1,108 +1,29 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
game = NULL;
if (! game_create(&game)){
game_error(game, "Failed to create game", 0);
return (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));
}
}
}
}
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);
}

23
src/render.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

49
src/screen.c Normal file
View File

@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* screen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <GLFW/glfw3.h>
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);
}