Hello world

This commit is contained in:
2025-11-14 15:07:40 +01:00
commit 06267858e8
5 changed files with 1579 additions and 0 deletions

30
Makefile Normal file
View File

@@ -0,0 +1,30 @@
NAME := cub3d
CFLAGS := -Wextra -Wall -Werror -Wunreachable-code -Ofast
LIBMLX := ./lib/mlx
HEADERS := -I ./include -I $(LIBMLX)/include
LIBS := $(LIBMLX)/build/libmlx42.a -ldl -lglfw -pthread -lm
SRCS := $(shell find ./src -iname "*.c")
OBJS := ${SRCS:.c=.o}
all: libmlx $(NAME)
libmlx:
@cmake $(LIBMLX) -B $(LIBMLX)/build && make -C $(LIBMLX)/build -j4
%.o: %.c
@$(CC) $(CFLAGS) -o $@ -c $< $(HEADERS) && printf "Compiling: $(notdir $<)"
$(NAME): $(OBJS)
@$(CC) $(OBJS) $(LIBS) $(HEADERS) -o $(NAME)
clean:
@rm -rf $(OBJS)
@rm -rf $(LIBMLX)/build
fclean: clean
@rm -rf $(NAME)
re: clean all
.PHONY: all, clean, fclean, re, libmlx

35
includes/cub3d.h Normal file
View File

@@ -0,0 +1,35 @@
#ifndef CUB3D_H
# define CUB3D_H
# include <unistd.h>
# include <stdlib.h>
# include <math.h>
# include "../lib/mlx/include/MLX42/MLX42.h"
# define S_W 800
# define S_H 600
# define FOV 60
# define TILE_SIZE 50
typedef struct s_player
{
int player_x;
int player_y;
double angel;
float fov;
int rot;
int l_r;
int u_d;
} t_player;
typedef struct s_mlx
{
mlx_image_t *img;
mlx_t *mlx_ptr;
t_player *player;
char **map;
} t_mlx;
#endif

1
lib/mlx Submodule

Submodule lib/mlx added at 5b5b7a1a66

1396
raw.txt Normal file

File diff suppressed because it is too large Load Diff

117
src/main.c Normal file
View File

@@ -0,0 +1,117 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/14 09:35:53 by aortigos #+# #+# */
/* Updated: 2025/11/14 09:43:55 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/cub3d.h"
void draw_player(mlx_image_t *img, t_player *player, int color)
{
for (int i = 0; i < 20; i++)
for (int j = 0; j < 20; j++)
mlx_put_pixel(img, player->player_x + i, player->player_y + j, color);
}
void draw_map(mlx_image_t *img, char **map)
{
int x, y, i, j;
i = 0;
while (map[i])
{
j = 0;
while (map[i][j])
{
if (map[i][j] == '1')
{
for (y = 0; y < TILE_SIZE; y++)
for (x = 0; x < TILE_SIZE; x++)
mlx_put_pixel(img, j * TILE_SIZE + x, i * TILE_SIZE + y, 0xFFFFFFFF);
}
j++;
}
i++;
}
}
// ------------------ Input ------------------
void key_hook(mlx_key_data_t keydata, void *param)
{
t_player *player = (t_player *)param;
if (keydata.key == MLX_KEY_W && keydata.action == MLX_PRESS)
player->player_y -= 10;
if (keydata.key == MLX_KEY_S && keydata.action == MLX_PRESS)
player->player_y += 10;
if (keydata.key == MLX_KEY_A && keydata.action == MLX_PRESS)
player->player_x -= 10;
if (keydata.key == MLX_KEY_D && keydata.action == MLX_PRESS)
player->player_x += 10;
}
// ------------------ Game Loop ------------------
void game_loop(void *param)
{
t_mlx *mlx_data = (t_mlx *)param;
for (int y = 0; y < S_H; y++)
for (int x = 0; x < S_W; x++)
mlx_put_pixel(mlx_data->img, x, y, 0x000000FF); // negro
draw_map(mlx_data->img, mlx_data->map); // dibujar mapa
draw_player(mlx_data->img, mlx_data->player, 0xFF0000FF); // dibujar jugador
}
// ------------------ Main ------------------
int main(void)
{
mlx_t *mlx;
mlx_image_t *img;
t_player player;
t_mlx mlx_data;
// mapa simple
char *map[] = {
"111111111",
"100000011",
"101000001",
"100001001",
"111111111",
NULL
};
// inicializar MLX
mlx = mlx_init(S_W, S_H, "Cub3D", false);
if (!mlx)
return (1);
img = mlx_new_image(mlx, S_W, S_H);
mlx_image_to_window(mlx, img, 0, 0);
// inicializar jugador
player.player_x = 100;
player.player_y = 100;
// inicializar estructura de MLX
mlx_data.mlx_ptr = mlx;
mlx_data.img = img;
mlx_data.player = &player;
mlx_data.map = map;
// hooks
mlx_key_hook(mlx, key_hook, &player);
mlx_loop_hook(mlx, game_loop, &mlx_data);
// loop
mlx_loop(mlx);
return (0);
}