github to gitea

This commit is contained in:
Leo Firmin
2025-10-27 23:12:49 +01:00
parent cd1882c3cc
commit 0c0d28ab36
134 changed files with 9850 additions and 338 deletions
+59
View File
@@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* draw.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jle-neze <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/12 18:18:26 by jle-neze #+# #+# */
/* Updated: 2025/09/12 18:18:52 by jle-neze ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
void draw_background(t_game *g)
{
int x, y, half = g->gfx.h / 2;
for (y = 0; y < g->gfx.h; y++)
{
for (x = 0; x < g->gfx.w; x++)
{
if (y < half)
img_put_pixel(&g->gfx.frame, x, y, g->colors.ceil);
else
img_put_pixel(&g->gfx.frame, x, y, g->colors.floor);
}
}
}
/* aussi exposée dans raycast.c, mais on garde la déclaration ici pour réutiliser ailleurs */
void draw_vline(t_game *g, int x, int y0, int y1, int color)
{
if (y0 < 0) y0 = 0;
if (y1 >= g->gfx.h) y1 = g->gfx.h - 1;
for (int y = y0; y <= y1; y++)
img_put_pixel(&g->gfx.frame, x, y, color);
}
void draw_tex_vline(t_game *g, int x, int y0, int y1,
t_tex *tex, int tex_x, double step, double tex_pos)
{
if (y0 < 0)
{
tex_pos += step * (-y0);
y0 = 0;
}
if (y1 >= g->gfx.h)
y1 = g->gfx.h - 1;
for (int y = y0; y <= y1; y++)
{
int tex_y = (int)tex_pos;
if (tex_y < 0) tex_y = 0;
if (tex_y >= tex->h) tex_y = tex->h - 1;
unsigned int color = tex_get_pixel(tex, tex_x, tex_y);
img_put_pixel(&g->gfx.frame, x, y, (int)color);
tex_pos += step;
}
}
+66
View File
@@ -0,0 +1,66 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hooks.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jle-neze <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/12 18:19:19 by jle-neze #+# #+# */
/* Updated: 2025/09/12 18:19:22 by jle-neze ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
#include "keys.h"
#include <stdlib.h>
int on_destroy(t_game *g)
{
textures_free(g);
cleanup_window(g);
world_free(&g->world);
/* Libérer data si disponible */
if (g->data)
{
free_textures(g->data->texture);
free_data(g->data);
}
exit(0);
return (0);
}
static void set_key(int key, t_game *g, int press)
{
if (key == KEY_W) g->in.w = press;
if (key == KEY_A) g->in.a = press;
if (key == KEY_S) g->in.s = press;
if (key == KEY_D) g->in.d = press;
if (key == KEY_LEFT) g->in.left = press;
if (key == KEY_RIGHT) g->in.right = press;
}
int on_keydown(int key, t_game *g)
{
if (key == KEY_ESC)
on_destroy(g);
set_key(key, g, 1);
return (0);
}
int on_keyup(int key, t_game *g)
{
set_key(key, g, 0);
return (0);
}
void setup_hooks(t_game *g)
{
/* croix de fermeture */
mlx_hook(g->gfx.win, 17, 0, on_destroy, g);
/* KeyPress / KeyRelease */
mlx_hook(g->gfx.win, 2, 1L<<0, on_keydown, g);
mlx_hook(g->gfx.win, 3, 1L<<1, on_keyup, g);
/* boucle */
mlx_loop_hook(g->gfx.mlx, game_loop, g);
}
+23
View File
@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* image.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jle-neze <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/12 18:19:47 by jle-neze #+# #+# */
/* Updated: 2025/09/12 18:19:49 by jle-neze ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
void img_put_pixel(t_img *img, int x, int y, int color)
{
char *dst;
if (x < 0 || y < 0 || x >= img->w || y >= img->h)
return ;
dst = img->addr + (y * img->line_len + x * (img->bpp / 8));
*(unsigned int *)dst = (unsigned int)color;
}
+55
View File
@@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jle-neze <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/12 18:20:06 by jle-neze #+# #+# */
/* Updated: 2025/09/12 18:20:08 by jle-neze ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
int init_window(t_game *g, int w, int h, char *title)
{
g->gfx.w = w;
g->gfx.h = h;
g->gfx.mlx = mlx_init();
if (!g->gfx.mlx)
return (1);
g->gfx.win = mlx_new_window(g->gfx.mlx, w, h, title);
if (!g->gfx.win)
return (1);
g->gfx.frame.ptr = mlx_new_image(g->gfx.mlx, w, h);
if (!g->gfx.frame.ptr)
return (1);
g->gfx.frame.addr = mlx_get_data_addr(g->gfx.frame.ptr, &g->gfx.frame.bpp,
&g->gfx.frame.line_len, &g->gfx.frame.endian);
g->gfx.frame.w = w;
g->gfx.frame.h = h;
return (0);
}
void cleanup_window(t_game *g)
{
if (!g)
return ;
if (g->gfx.frame.ptr)
{
mlx_destroy_image(g->gfx.mlx, g->gfx.frame.ptr);
g->gfx.frame.ptr = NULL;
}
if (g->gfx.win)
{
mlx_destroy_window(g->gfx.mlx, g->gfx.win);
g->gfx.win = NULL;
}
if (g->gfx.mlx)
{
mlx_destroy_display(g->gfx.mlx);
free(g->gfx.mlx);
g->gfx.mlx = NULL;
}
}
+25
View File
@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* loop.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jle-neze <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/12 18:24:03 by jle-neze #+# #+# */
/* Updated: 2025/09/12 18:24:05 by jle-neze ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
int game_loop(t_game *g)
{
/* 1) Update joueur selon input */
player_update(g);
/* 2) Dessin */
draw_background(g);
raycast_frame(g);
mlx_put_image_to_window(g->gfx.mlx, g->gfx.win, g->gfx.frame.ptr, 0, 0);
return (0);
}
/* Note: on pourrait optimiser en ne redessinant que ce qui change */
+80
View File
@@ -0,0 +1,80 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* player.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jle-neze <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/12 18:24:21 by jle-neze #+# #+# */
/* Updated: 2025/09/12 18:24:22 by jle-neze ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
void player_init(t_game *g, double px, double py, double dx, double dy)
{
g->cam.pos_x = px;
g->cam.pos_y = py;
g->cam.dir_x = dx;
g->cam.dir_y = dy;
/* FOV ≈ 66° → plane perpendiculaire (0.66) */
g->cam.plane_x = -dy * 0.66;
g->cam.plane_y = dx * 0.66;
g->move_speed = 0.07; /* ajuste si besoin */
g->rot_speed = 0.05; /* rad/frame */
}
/* petits helpers collision */
static int is_wall(t_world *w, int mx, int my)
{
if (mx < 0 || my < 0 || mx >= w->w || my >= w->h) return (1);
return (w->grid[my][mx] == '1');
}
void player_update(t_game *g)
{
double nx, ny;
/* Avancer/retour W/S */
if (g->in.w)
{
nx = g->cam.pos_x + g->cam.dir_x * g->move_speed;
ny = g->cam.pos_y + g->cam.dir_y * g->move_speed;
if (!is_wall(&g->world, (int)nx, (int)g->cam.pos_y)) g->cam.pos_x = nx;
if (!is_wall(&g->world, (int)g->cam.pos_x, (int)ny)) g->cam.pos_y = ny;
}
if (g->in.s)
{
nx = g->cam.pos_x - g->cam.dir_x * g->move_speed;
ny = g->cam.pos_y - g->cam.dir_y * g->move_speed;
if (!is_wall(&g->world, (int)nx, (int)g->cam.pos_y)) g->cam.pos_x = nx;
if (!is_wall(&g->world, (int)g->cam.pos_x, (int)ny)) g->cam.pos_y = ny;
}
/* Strafe A/D (gauche/droite) */
if (g->in.a)
{
nx = g->cam.pos_x - g->cam.dir_y * g->move_speed;
ny = g->cam.pos_y + g->cam.dir_x * g->move_speed;
if (!is_wall(&g->world, (int)nx, (int)g->cam.pos_y)) g->cam.pos_x = nx;
if (!is_wall(&g->world, (int)g->cam.pos_x, (int)ny)) g->cam.pos_y = ny;
}
if (g->in.d)
{
nx = g->cam.pos_x + g->cam.dir_y * g->move_speed;
ny = g->cam.pos_y - g->cam.dir_x * g->move_speed;
if (!is_wall(&g->world, (int)nx, (int)g->cam.pos_y)) g->cam.pos_x = nx;
if (!is_wall(&g->world, (int)g->cam.pos_x, (int)ny)) g->cam.pos_y = ny;
}
/* Rotation gauche/droite */
if (g->in.left || g->in.right)
{
double rs = g->rot_speed * (g->in.right ? 1.0 : -1.0);
double old_dir_x = g->cam.dir_x;
g->cam.dir_x = g->cam.dir_x * cos(rs) - g->cam.dir_y * sin(rs);
g->cam.dir_y = old_dir_x * sin(rs) + g->cam.dir_y * cos(rs);
double old_plane_x = g->cam.plane_x;
g->cam.plane_x = g->cam.plane_x * cos(rs) - g->cam.plane_y * sin(rs);
g->cam.plane_y = old_plane_x * sin(rs) + g->cam.plane_y * cos(rs);
}
}
+243
View File
@@ -0,0 +1,243 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* raycast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jle-neze <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/12 18:24:37 by jle-neze #+# #+# */
/* Updated: 2025/10/26 02:20:06 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
// /* petite fonction utilitaire: trace une ligne verticale remplie */
// void draw_vline(t_game *g, int x, int y0, int y1, int color)
// {
// if (y0 < 0) y0 = 0;
// if (y1 >= g->gfx.h) y1 = g->gfx.h - 1;
// for (int y = y0; y <= y1; y++)
// img_put_pixel(&g->gfx.frame, x, y, color);
// }
/* Version 1: simple couleurs */
// void raycast_frame(t_game *g)
// {
// for (int x = 0; x < g->gfx.w; x++)
// {
// double camera_x = 2.0 * x / (double)g->gfx.w - 1.0;
// double ray_dir_x = g->cam.dir_x + g->cam.plane_x * camera_x;
// double ray_dir_y = g->cam.dir_y + g->cam.plane_y * camera_x;
// int map_x = (int)g->cam.pos_x;
// int map_y = (int)g->cam.pos_y;
// double delta_dist_x = (ray_dir_x == 0) ? 1e30 : fabs(1.0 / ray_dir_x);
// double delta_dist_y = (ray_dir_y == 0) ? 1e30 : fabs(1.0 / ray_dir_y);
// int step_x, step_y;
// double side_dist_x, side_dist_y;
// if (ray_dir_x < 0)
// {
// step_x = -1;
// side_dist_x = (g->cam.pos_x - map_x) * delta_dist_x;
// }
// else
// {
// step_x = 1;
// side_dist_x = (map_x + 1.0 - g->cam.pos_x) * delta_dist_x;
// }
// if (ray_dir_y < 0)
// {
// step_y = -1;
// side_dist_y = (g->cam.pos_y - map_y) * delta_dist_y;
// }
// else
// {
// step_y = 1;
// side_dist_y = (map_y + 1.0 - g->cam.pos_y) * delta_dist_y;
// }
// int hit = 0;
// int side = 0; /* 0:x, 1:y */
// while (!hit)
// {
// if (side_dist_x < side_dist_y)
// {
// side_dist_x += delta_dist_x;
// map_x += step_x;
// side = 0;
// }
// else
// {
// side_dist_y += delta_dist_y;
// map_y += step_y;
// side = 1;
// }
// if (map_x < 0 || map_y < 0 || map_x >= g->world.w || map_y >= g->world.h
// || g->world.grid[map_y][map_x] == '1')
// hit = 1;
// }
// double perp_wall_dist;
// if (side == 0)
// perp_wall_dist = (side_dist_x - delta_dist_x);
// else
// perp_wall_dist = (side_dist_y - delta_dist_y);
// int line_h = (int)(g->gfx.h / (perp_wall_dist > 1e-6 ? perp_wall_dist : 1e-6));
// int draw_start = -line_h / 2 + g->gfx.h / 2;
// int draw_end = line_h / 2 + g->gfx.h / 2;
// /* Couleur: simple code NSEW selon step & side */
// int color_idx;
// if (side == 0)
// color_idx = (step_x < 0) ? 2 /* E */ : 3 /* W */;
// else
// color_idx = (step_y < 0) ? 1 /* S */ : 0 /* N */;
// int color = g->colors.wall_nsew[color_idx];
// if (side == 1) color = (color & 0xFEFEFE) >> 1; /* ombre simple */
// draw_vline(g, x, draw_start, draw_end, color);
// }
// }
/* 2eme test de la meme focntion (pas encore choisi laquelle)*/
// void raycast_frame(t_game *g)
// {
// for (int x = 0; x < g->gfx.w; x++)
// {
// double camera_x = 2.0 * x / (double)g->gfx.w - 1.0;
// double ray_dir_x = g->cam.dir_x + g->cam.plane_x * camera_x;
// double ray_dir_y = g->cam.dir_y + g->cam.plane_y * camera_x;
// int map_x = (int)g->cam.pos_x;
// int map_y = (int)g->cam.pos_y;
// double delta_dist_x = (ray_dir_x == 0) ? 1e30 : fabs(1.0 / ray_dir_x);
// double delta_dist_y = (ray_dir_y == 0) ? 1e30 : fabs(1.0 / ray_dir_y);
// int step_x, step_y;
// double side_dist_x, side_dist_y;
// if (ray_dir_x < 0) { step_x = -1; side_dist_x = (g->cam.pos_x - map_x) * delta_dist_x; }
// else { step_x = 1; side_dist_x = (map_x + 1.0 - g->cam.pos_x) * delta_dist_x; }
// if (ray_dir_y < 0) { step_y = -1; side_dist_y = (g->cam.pos_y - map_y) * delta_dist_y; }
// else { step_y = 1; side_dist_y = (map_y + 1.0 - g->cam.pos_y) * delta_dist_y; }
// int hit = 0, side = 0;
// while (!hit)
// {
// if (side_dist_x < side_dist_y) { side_dist_x += delta_dist_x; map_x += step_x; side = 0; }
// else { side_dist_y += delta_dist_y; map_y += step_y; side = 1; }
// if (map_x < 0 || map_y < 0 || map_x >= g->world.w || map_y >= g->world.h
// || g->world.grid[map_y][map_x] == '1')
// hit = 1;
// }
// double perp_wall_dist = (side == 0) ? (side_dist_x - delta_dist_x) : (side_dist_y - delta_dist_y);
// if (perp_wall_dist < 1e-6) perp_wall_dist = 1e-6;
// int line_h = (int)(g->gfx.h / perp_wall_dist);
// int draw_start = -line_h / 2 + g->gfx.h / 2;
// int draw_end = line_h / 2 + g->gfx.h / 2;
// int color_idx;
// if (side == 0) color_idx = (step_x < 0) ? 2 /* E */ : 3 /* W */;
// else color_idx = (step_y < 0) ? 1 /* S */ : 0 /* N */;
// int color = g->colors.wall_nsew[color_idx];
// if (side == 1) color = (color & 0xFEFEFE) >> 1; /* ombre simple */
// draw_vline(g, x, draw_start, draw_end, color); /* impl. dans draw.c */
// }
// }
/* Version 3: textures */
void raycast_frame(t_game *g)
{
const double EPS = 1e-6;
for (int x = 0; x < g->gfx.w; x++)
{
double camera_x = 2.0 * x / (double)g->gfx.w - 1.0;
double ray_dir_x = g->cam.dir_x + g->cam.plane_x * camera_x;
double ray_dir_y = g->cam.dir_y + g->cam.plane_y * camera_x;
int map_x = (int)g->cam.pos_x;
int map_y = (int)g->cam.pos_y;
double delta_dist_x = (ray_dir_x == 0) ? 1e30 : fabs(1.0 / ray_dir_x);
double delta_dist_y = (ray_dir_y == 0) ? 1e30 : fabs(1.0 / ray_dir_y);
int step_x, step_y;
double side_dist_x, side_dist_y;
if (ray_dir_x < 0) { step_x = -1; side_dist_x = (g->cam.pos_x - map_x) * delta_dist_x; }
else { step_x = 1; side_dist_x = (map_x + 1.0 - g->cam.pos_x) * delta_dist_x; }
if (ray_dir_y < 0) { step_y = -1; side_dist_y = (g->cam.pos_y - map_y) * delta_dist_y; }
else { step_y = 1; side_dist_y = (map_y + 1.0 - g->cam.pos_y) * delta_dist_y; }
int hit = 0, side = 0;
while (!hit)
{
if (side_dist_x < side_dist_y) { side_dist_x += delta_dist_x; map_x += step_x; side = 0; }
else { side_dist_y += delta_dist_y; map_y += step_y; side = 1; }
if (map_x < 0 || map_y < 0 || map_x >= g->world.w || map_y >= g->world.h
|| g->world.grid[map_y][map_x] == '1')
hit = 1;
}
double perp_wall_dist = (side == 0) ? (side_dist_x - delta_dist_x) : (side_dist_y - delta_dist_y);
if (perp_wall_dist < EPS) perp_wall_dist = EPS;
int line_h = (int)(g->gfx.h / perp_wall_dist);
int draw_start = -line_h / 2 + g->gfx.h / 2;
int draw_end = line_h / 2 + g->gfx.h / 2;
/* Choix de texture 0:N 1:S 2:E 3:W (même logique que couleurs) */
int tex_id;
if (side == 0) tex_id = (step_x < 0) ? 2 /* E */ : 3 /* W */;
else tex_id = (step_y < 0) ? 1 /* S */ : 0 /* N */;
if (!g->has_tex)
{
int color = g->colors.wall_nsew[tex_id];
if (side == 1) color = (color & 0xFEFEFE) >> 1;
draw_vline(g, x, draw_start, draw_end, color);
continue;
}
/* --- TEXTURE MAPPING --- */
t_tex *t = &g->tex[tex_id];
/* Position exacte du hit sur le mur (0..1) */
double wall_x;
if (side == 0)
wall_x = g->cam.pos_y + perp_wall_dist * ray_dir_y;
else
wall_x = g->cam.pos_x + perp_wall_dist * ray_dir_x;
wall_x -= floor(wall_x);
/* Coordonnée X dans la texture */
int tex_x = (int)(wall_x * (double)t->w);
if (side == 0 && ray_dir_x > 0) tex_x = t->w - tex_x - 1;
if (side == 1 && ray_dir_y < 0) tex_x = t->w - tex_x - 1;
/* Échantillonnage vertical */
double step = (double)t->h / (double)line_h;
double tex_pos = (draw_start - g->gfx.h / 2 + line_h / 2) * step;
/* Ombre légère sur faces Y (optionnel) */
draw_tex_vline(g, x, draw_start, draw_end, t, tex_x, step, tex_pos);
if (side == 1) {
/* sombrement simple: on pourrait parcourir y et foncer; plus simple: rien faire
pour rester portable / endianness-safe. */
}
}
}
+53
View File
@@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* textures.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jle-neze <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/12 18:24:56 by jle-neze #+# #+# */
/* Updated: 2025/09/12 18:24:58 by jle-neze ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
static int load_one(t_game *g, t_tex *t, const char *path)
{
t->ptr = mlx_xpm_file_to_image(g->gfx.mlx, (char *)path, &t->w, &t->h);
if (!t->ptr)
return (1);
t->addr = mlx_get_data_addr(t->ptr, &t->bpp, &t->line_len, &t->endian);
return (0);
}
int textures_load(t_game *g, const char *no, const char *so,
const char *we, const char *ea)
{
if (load_one(g, &g->tex[0], no)
|| load_one(g, &g->tex[1], so)
|| load_one(g, &g->tex[2], we)
|| load_one(g, &g->tex[3], ea))
{
textures_free(g);
return (1);
}
g->has_tex = 1;
return (0);
}
void textures_free(t_game *g)
{
for (int i = 0; i < 4; i++)
{
if (g->tex[i].ptr)
mlx_destroy_image(g->gfx.mlx, g->tex[i].ptr);
g->tex[i].ptr = NULL;
}
}
unsigned int tex_get_pixel(t_tex *t, int x, int y)
{
char *px = t->addr + (y * t->line_len + x * (t->bpp / 8));
return (*(unsigned int *)px);
}
+81
View File
@@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* world.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jle-neze <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/12 18:25:13 by jle-neze #+# #+# */
/* Updated: 2025/09/12 18:25:17 by jle-neze ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
#include <stdlib.h>
// static const char *demo_map[] = {
// "111111111111",
// "100000000001",
// "100011110001",
// "100000010001",
// "101111010001",
// "100100010001",
// "10010N010001", /* N = start dir nord, on l'ignore ici (juste info) */
// "100000000001",
// "111111111111",
// NULL
// };
static const char *demo_map[] = {
"111111111111",
"100000000001",
"100000000001",
"100000000001",
"100000001111",
"100000000001",
"100000000001", /* N = start dir nord, on l'ignore ici (juste info) */
"100000000001",
"111111111111",
NULL
};
int world_init_demo(t_world *w)
{
int h = 0;
while (demo_map[h])
h++;
w->h = h;
w->w = 0;
for (int i = 0; i < h; i++)
{
int len = 0;
while (demo_map[i][len]) len++;
if (len > w->w) w->w = len;
}
w->grid = (char **)malloc(sizeof(char *) * (h + 1));
if (!w->grid) return (1);
for (int y = 0; y < h; y++)
{
w->grid[y] = (char *)malloc(w->w + 1);
if (!w->grid[y]) return (1);
for (int x = 0; x < w->w; x++)
{
char c = demo_map[y][x];
if (c == 0) c = '1';
if (c == 'N' || c == 'S' || c == 'E' || c == 'W')
c = '0';
w->grid[y][x] = c;
}
w->grid[y][w->w] = '\0';
}
w->grid[h] = NULL;
return (0);
}
void world_free(t_world *w)
{
if (!w->grid) return ;
for (int y = 0; y < w->h; y++)
free(w->grid[y]);
free(w->grid);
w->grid = NULL;
}
+207
View File
@@ -0,0 +1,207 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/22 14:56:25 by lfirmin #+# #+# */
/* Updated: 2025/10/08 16:25:42 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
// void print_int_array(int *array, int size)
// {
// int i;
// if (!array)
// return ;
// i = 0;
// while (i < size)
// {
// ft_putnbr_fd(array[i], 1);
// if (i < size - 1)
// write(1, ", ", 2);
// i++;
// }
// write(1, "\n", 1);
// }
// void debug(t_data data)
// {
// printf("map:\n\n");
// print_array(data.map);
// printf("\n");
// printf("c: ");
// fflush(stdout);
// print_int_array(data.texture->ceiling, 3);
// printf("f: ");
// fflush(stdout);
// print_int_array(data.texture->floor, 3);
// printf("p: ");
// fflush(stdout);
// print_int_array(data.parsing.player, 3);
// printf("\n");
// printf("\ntexture:\n\n");
// printf("%s\n", data.texture->east);
// printf("%s\n", data.texture->north);
// printf("%s\n", data.texture->west);
// printf("%s\n", data.texture->south);
// }
// int main(int ac, char **av)
// {
// t_data data;
// init_data(&data, av[1]);
// if (parsing(&data));
// debug(data);
// free_char_array(data.parsing.raw_map);
// free_textures(data.texture);
// free_data(&data);
// }
int rgb_to_int(int r, int g, int b)
{
if (r < 0) r = 0;
if (r > 255) r = 255;
if (g < 0) g = 0;
if (g > 255) g = 255;
if (b < 0) b = 0;
if (b > 255) b = 255;
return ((r << 16) | (g << 8) | b);
}
static int world_init_from_parsing(t_world *world, t_data *data)
{
int y;
world->h = 0;
while (data->map[world->h])
world->h++;
world->w = ft_strlen(data->map[0]);
world->grid = malloc(sizeof(char *) * (world->h + 1));
if (!world->grid)
return (1);
y = 0;
while (y < world->h)
{
world->grid[y] = ft_strdup(data->map[y]);
if (!world->grid[y])
return (1);
y++;
}
world->grid[world->h] = NULL;
return (0);
}
// int main(int ac, char **av)
// {
// t_game g;
// t_data data;
// if (ac != 2)
// return (printf("Usage: ./cub3d <map.cub>\n"), 1);
// init_data(&data, av[1]);
// if (parsing(&data))
// return (printf("Parsing error\n"), 1);
// free_char_array(data.parsing.raw_map);
// /* init couleurs et textures */
// g.colors.floor = rgb_to_int(data.texture->floor[0], data.texture->floor[1], data.texture->floor[2]); // ok jai change le fonction rgb_to_int elle attend 3 int le r le g et le b
// g.colors.ceil = rgb_to_int(data.texture->ceiling[0], data.texture->ceiling[1], data.texture->ceiling[2]);
// if (textures_load(&g,
// data.texture->north, data.texture->south,
// data.texture->west, data.texture->east) != 0)
// g.has_tex = 0;
// /* init map et player */
// if (world_init_from_parsing(&g.world, &data) != 0)
// return (printf("World init failed\n"), 1);
// double dx = 0, dy = 0;
// if (data.parsing.player[2] == 'N') dy = -1;
// if (data.parsing.player[2] == 'S') dy = 1;
// if (data.parsing.player[2] == 'E') dx = 1;
// if (data.parsing.player[2] == 'W') dx = -1;
// player_init(&g,
// data.parsing.player[0] + 0.5,
// data.parsing.player[1] + 0.5,
// dx, dy);
// /* window + loop */
// if (init_window(&g, 1024, 768, "cub3D") != 0)
// return (1);
// setup_hooks(&g);
// mlx_loop(g.gfx.mlx);
// world_free(&g.world);
// free_data(&data);
// return (0);
// }
int main(int ac, char **av)
{
t_game g;
t_data data;
if (ac != 2)
return (printf("Usage: ./cub3d <map.cub>\n"), 1);
memset(&g, 0, sizeof(t_game)); // IMPORTANT : initialiser g
init_data(&data, av[1]);
if (parsing(&data))
{
free_textures(data.texture);
free_data(&data);
return (1);
}
/* init couleurs */
g.colors.floor = rgb_to_int(data.texture->floor[0], data.texture->floor[1], data.texture->floor[2]);
g.colors.ceil = rgb_to_int(data.texture->ceiling[0], data.texture->ceiling[1], data.texture->ceiling[2]);
/* init map et player */
if (world_init_from_parsing(&g.world, &data) != 0)
return (printf("World init failed\n"), 1);
/* Libérer data->map après la duplication vers world->grid */
free_char_array(data.map);
data.map = NULL;
double dx = 0, dy = 0;
if (data.parsing.player[2] == 'S') dy = -1;
if (data.parsing.player[2] == 'N') dy = 1;
if (data.parsing.player[2] == 'E') dx = 1;
if (data.parsing.player[2] == 'W') dx = -1;
player_init(&g,
data.parsing.player[0] + 0.5,
data.parsing.player[1] + 0.5,
dx, dy);
/* window AVANT textures ! */
if (init_window(&g, 1024, 768, "cub3D") != 0)
return (1);
/* MAINTENANT on charge les textures */
if (textures_load(&g,
data.texture->north, data.texture->south,
data.texture->west, data.texture->east) != 0)
g.has_tex = 0;
/* Passer data à g pour le cleanup */
g.data = &data;
setup_hooks(&g);
/* Vider le buffer gnl avant de rentrer dans mlx_loop */
get_next_line(-2);
mlx_loop(g.gfx.mlx);
world_free(&g.world);
free_textures(data.texture);
free_data(&data);
return (0);
}
+35
View File
@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_colors.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/25 18:41:26 by lfirmin #+# #+# */
/* Updated: 2025/10/07 10:06:32 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
int check_colors(t_textures *texture)
{
if (is_rgb(texture->floor))
return (ft_error(ERROR_COL), 1);
if (is_rgb(texture->ceiling))
return (ft_error(ERROR_COL), 1);
return (0);
}
int is_rgb(int color[3])
{
int i;
i = 0;
while (i < 3)
{
if (color[i] > 255 || color[i] < 0)
return (1);
i++;
}
return (0);
}
+40
View File
@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_file.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/22 15:07:29 by lfirmin #+# #+# */
/* Updated: 2025/10/07 10:11:50 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
int check_file(char *map_path, t_data_parsing *parsing)
{
if (!map_path)
return (ft_error(ERROR_EMPT_PATH), 1);
if (check_extension(map_path))
return (1);
parsing->fd_map = open(map_path, O_RDONLY);
if (parsing->fd_map < 0)
return (perror("Error\n"), 1);
parsing->fd_map_dup = open(map_path, O_RDONLY);
if (parsing->fd_map_dup < 0)
return (perror("Error\n"), 1);
return (0);
}
int check_extension(char *map_path)
{
int len_map_path;
len_map_path = ft_strlen(map_path);
if (len_map_path < 4)
return (ft_error(ERROR_EXT), 1);
if (ft_strcmp(map_path + len_map_path - 4, ".cub") != 0)
return (ft_error(ERROR_EXT), 1);
return (0);
}
+129
View File
@@ -0,0 +1,129 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/22 15:07:29 by lfirmin #+# #+# */
/* Updated: 2025/10/07 10:11:50 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
int check_char_and_count(char c, int *p)
{
if (c == '1' || c == '0' || c == ' ' || c == '\n')
return (0);
else if (c == 'S' || c == 'N' || c == 'E' || c == 'W')
{
(*p)++;
return (0);
}
else
return (2);
}
int check_map_char(char **map)
{
int i;
int j;
int p;
int ret;
i = 0;
p = 0;
while (map[i])
{
j = 0;
while (map[i][j])
{
ret = check_char_and_count(map[i][j], &p);
if (ret != 0)
return (ret);
j++;
}
i++;
}
if (p != 1)
return (1);
return (0);
}
void find_player_pos(char **map, int *player)
{
int i;
int j;
i = 0;
while (map[i])
{
j = 0;
while (map[i][j])
{
if (map[i][j] == 'S' || map[i][j] == 'N' ||
map[i][j] == 'E' || map[i][j] == 'W')
{
player[0] = j;
player[1] = i;
player[2] = map[i][j];
return ;
}
j++;
}
i++;
}
}
int flood_fill(char **map_cp, int x, int y)
{
if (!map_cp || y < 0 || x < 0 || !map_cp[y] || !map_cp[y][x])
return (1);
if (map_cp[y][x] == 'v')
return (0);
if (map_cp[y][x] == '1')
{
map_cp[y][x] = 'v';
return (0);
}
if (map_cp[y][x] == '0' || map_cp[y][x] == 'N' || map_cp[y][x] == 'S'
|| map_cp[y][x] == 'E' || map_cp[y][x] == 'W')
{
map_cp[y][x] = 'v';
if (flood_fill(map_cp, x + 1, y))
return (1);
if (flood_fill(map_cp, x - 1, y))
return (1);
if (flood_fill(map_cp, x, y + 1))
return (1);
if (flood_fill(map_cp, x, y - 1))
return (1);
return (0);
}
return (1);
}
int rep_ex_wall(char **map_cp, int x, int y)
{
int len;
len = 0;
if (y < 0 || x < 0)
return (0);
if (!map_cp[y])
return (0);
len = ft_strlen(map_cp[y]);
if (x >= (int)len)
return (0);
if (map_cp[y][x] == '1')
map_cp[y][x] = 'v';
if (map_cp[y][x] == 'v')
{
map_cp[y][x] = 'c';
rep_ex_wall(map_cp, x + 1, y);
rep_ex_wall(map_cp, x - 1, y);
rep_ex_wall(map_cp, x, y + 1);
rep_ex_wall(map_cp, x, y - 1);
}
return (0);
}
+54
View File
@@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_map_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/22 15:07:29 by lfirmin #+# #+# */
/* Updated: 2025/10/07 12:09:03 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
int hasinvalidchar(char **map)
{
int y;
int x;
y = 0;
while (map[y])
{
x = 0;
while (map[y][x])
{
if (map[y][x] != 'c' && map[y][x] != ' ' && map[y][x] != ' '
&& map[y][x] != '\n')
return (1);
x++;
}
y++;
}
return (0);
}
int validate_map(char **map, int *player)
{
int result;
char **map_cp;
map_cp = ft_arrcpy(map);
result = check_map_char(map);
if (result == 1)
return (free_char_array(map_cp), ft_error(ERROR_PLAYER), 1);
if (result == 2)
return (free_char_array(map_cp), ft_error(ERROR_CHAR), 1);
find_player_pos(map, player);
if (flood_fill(map_cp, player[0], player[1]))
return (free_char_array(map_cp), ft_error(ERROR_NOT_CLOSE), 1);
rep_ex_wall(map_cp, player[0], player[1]);
if (hasinvalidchar(map_cp))
return (free_char_array(map_cp), ft_error(ERROR_CHAR), 1);
free_char_array(map_cp);
return (0);
}
+64
View File
@@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/23 13:29:28 by lfirmin #+# #+# */
/* Updated: 2025/10/07 10:13:07 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
int get_map(t_data *data)
{
int nb_line;
nb_line = line_counter(data->parsing.fd_map);
if (nb_line <= 0)
return (ft_error(ERROR_EMPTY), 1);
data->parsing.raw_map = ft_calloc(nb_line + 1, sizeof(char *));
if (put_map_on_array(data))
{
free_char_array(data->parsing.raw_map);
return (1);
}
return (0);
}
int line_counter(int fd)
{
int nb_line;
char *line;
line = NULL;
nb_line = 0;
line = get_next_line(fd);
while (line != NULL)
{
nb_line++;
free(line);
line = get_next_line(fd);
}
free(line);
line = get_next_line(-1);
while (line != NULL)
{
free(line);
line = get_next_line(-1);
}
get_next_line(-2);
return (nb_line);
}
char *clean_line(char *raw_line)
{
char *cleaned;
if (!raw_line)
return (NULL);
cleaned = ft_strtrim(raw_line, " \t\n\r");
return (cleaned);
}
+77
View File
@@ -0,0 +1,77 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_map_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/22 15:07:29 by lfirmin #+# #+# */
/* Updated: 2025/10/07 10:11:50 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
static int process_config_line(char *line, t_data *data, int *j)
{
char *cleaned;
cleaned = clean_line(line);
if (get_texture_path(cleaned, data->texture, j))
{
free(line);
free(cleaned);
return (1);
}
free(cleaned);
return (0);
}
static void flush_remaining_lines(int fd)
{
char *line;
line = get_next_line(fd);
while (line != NULL)
{
free(line);
line = get_next_line(fd);
}
}
static int handle_line(char *line, t_data *data, int *i, int *j)
{
if (is_config_line(line))
{
if (process_config_line(line, data, j))
{
flush_remaining_lines(-1);
return (1);
}
free(line);
}
else if (is_empty_line(line) && *i == 0)
free(line);
else
data->parsing.raw_map[(*i)++] = line;
return (0);
}
int put_map_on_array(t_data *data)
{
char *line;
int i;
int j[6];
ft_memset(j, 0, sizeof(j));
i = 0;
line = get_next_line(data->parsing.fd_map_dup);
while (line != NULL)
{
if (handle_line(line, data, &i, j))
return (1);
line = get_next_line(data->parsing.fd_map_dup);
}
data->parsing.raw_map[i] = NULL;
get_next_line(-2);
return (0);
}
+91
View File
@@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_textures.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/24 12:37:11 by lfirmin #+# #+# */
/* Updated: 2025/10/07 10:13:41 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
int get_texture_path(char *line, t_textures *texture, int *j)
{
if (check_extension_2(line) && !is_color_line(line))
return (ft_error(ERROR_NOT_XMP), 1);
else if (is_texture_line(line) == 1 && j[0]++ == 0)
texture->north = ft_strtrim(line + 2, " \t\n\r");
else if (is_texture_line(line) == 2 && j[1]++ == 0)
texture->south = ft_strtrim(line + 2, " \t\n\r");
else if (is_texture_line(line) == 3 && j[2]++ == 0)
texture->west = ft_strtrim(line + 2, " \t\n\r");
else if (is_texture_line(line) == 4 && j[3]++ == 0)
texture->east = ft_strtrim(line + 2, " \t\n\r");
else if (is_color_line(line) == 1 && j[4]++ == 0)
get_rgb_values(line, texture->floor);
else if (is_color_line(line) == 2 && j[5]++ == 0)
get_rgb_values(line, texture->ceiling);
else
return (ft_error(ERROR_DOUBLE), 2);
return (0);
}
int is_valid_number(char *str)
{
int i;
if (!str || !*str)
return (0);
i = 0;
while (str[i] == ' ' || str[i] == '\t')
i++;
if (!str[i] || !(str[i] >= '0' && str[i] <= '9'))
return (0);
while (str[i] >= '0' && str[i] <= '9')
i++;
while (str[i] == ' ' || str[i] == '\t')
i++;
if (str[i] != '\0')
return (0);
return (1);
}
int validate_and_convert_rgb(char **parts, int rgb[3])
{
int i;
i = -1;
while (++i < 3)
{
if (!is_valid_number(parts[i]))
return (free_char_array(parts), 1);
rgb[i] = ft_atoi(parts[i]);
if (rgb[i] < 0 || rgb[i] > 255)
return (free_char_array(parts), 1);
}
return (free_char_array(parts), 0);
}
int get_rgb_values(char *line, int rgb[3])
{
char *start;
char **parts;
int i;
if (!line || !rgb)
return (1);
start = ft_strchr(line, ' ');
if (!start)
return (1);
parts = ft_split(start + 1, ',');
if (!parts)
return (1);
i = 0;
while (parts[i])
i++;
if (i != 3)
return (free_char_array(parts), 1);
return (validate_and_convert_rgb(parts, rgb));
}
+56
View File
@@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/23 13:33:00 by lfirmin #+# #+# */
/* Updated: 2025/10/07 10:14:37 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
int init_parsing(t_data_parsing *parsing)
{
if (!parsing)
return (ft_error(ERROR_INIT_PARS), 1);
parsing->fd_map = 0;
parsing->raw_map = NULL;
return (0);
}
int init_textures(t_textures *textures)
{
int i;
if (!textures)
return (ft_error(ERROR_INIT_TEX), 1);
textures->north = NULL;
textures->south = NULL;
textures->east = NULL;
textures->west = NULL;
i = 0;
while (i < 3)
{
textures->floor[i] = -1;
textures->ceiling[i] = -1;
i++;
}
return (0);
}
int free_textures(t_textures *textures)
{
if (!textures)
return (0);
if (textures->north)
free(textures->north);
if (textures->south)
free(textures->south);
if (textures->east)
free(textures->east);
if (textures->west)
free(textures->west);
return (0);
}
+104
View File
@@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* line_detect.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/24 11:58:39 by lfirmin #+# #+# */
/* Updated: 2025/10/08 14:05:47 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
int check_extension_2(char *str)
{
size_t len;
len = strlen(str);
if (len < 4)
return (1);
return (ft_strcmp(str + len - 4, ".xpm"));
}
int is_config_line(char *line)
{
char *trimmed;
int result;
result = 0;
if (!line)
return (0);
trimmed = ft_strtrim(line, " \t\n\r");
if (!trimmed)
return (0);
if (ft_strncmp(trimmed, "NO ", 3) == 0
|| ft_strncmp(trimmed, "SO ", 3) == 0
|| ft_strncmp(trimmed, "WE ", 3) == 0
|| ft_strncmp(trimmed, "EA ", 3) == 0
|| ft_strncmp(trimmed, "F ", 2) == 0
|| ft_strncmp(trimmed, "C ", 2) == 0)
result = 1;
free(trimmed);
return (result);
}
int is_empty_line(char *line)
{
int i;
i = 0;
if (!line)
return (1);
while (line[i])
{
if (line[i] != ' ' && line[i] != '\t'
&& line[i] != '\n' && line[i] != '\r')
return (0);
i++;
}
return (1);
}
int is_color_line(char *line)
{
char *trimmed;
int result;
result = 0;
if (!line)
return (0);
trimmed = ft_strtrim(line, " \t\n\r");
if (!trimmed)
return (0);
if (ft_strncmp(trimmed, "F ", 2) == 0)
result = 1;
else if (ft_strncmp(trimmed, "C ", 2) == 0)
result = 2;
free(trimmed);
return (result);
}
int is_texture_line(char *line)
{
char *trimmed;
int result;
result = 0;
if (!line)
return (0);
trimmed = ft_strtrim(line, " \t\n\r");
if (!trimmed)
return (0);
if (ft_strncmp(trimmed, "NO ", 3) == 0)
result = 1;
else if (ft_strncmp(trimmed, "SO ", 3) == 0)
result = 2;
else if (ft_strncmp(trimmed, "WE ", 3) == 0)
result = 3;
else if (ft_strncmp(trimmed, "EA ", 3) == 0)
result = 4;
free(trimmed);
return (result);
}
+62
View File
@@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/22 14:17:58 by lfirmin #+# #+# */
/* Updated: 2025/10/26 02:10:59 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
int check_xpm_file(char *filepath)
{
int fd;
if (!filepath)
return (1);
fd = open(filepath, O_RDONLY);
if (fd == -1)
return (1);
close(fd);
return (0);
}
int check_texture_file(t_textures *texture)
{
if (check_xpm_file(texture->north))
return (ft_error(ERR_I_N), 1);
if (check_xpm_file(texture->south))
return (ft_error(ERR_I_S), 1);
if (check_xpm_file(texture->east))
return (ft_error(ERR_I_E), 1);
if (check_xpm_file(texture->west))
return (ft_error(ERR_I_W), 1);
return (0);
}
int parsing(t_data *data)
{
if (check_file(data->map_path, &data->parsing) == 1)
return (1);
if (get_map(data))
{
close(data->parsing.fd_map);
close(data->parsing.fd_map_dup);
return (1);
}
close(data->parsing.fd_map);
close(data->parsing.fd_map_dup);
get_next_line(-2);
if (check_colors(data->texture) == 1)
return (free_char_array(data->parsing.raw_map), 1);
if (check_texture_file(data->texture) == 1)
return (free_char_array(data->parsing.raw_map), 1);
if (validate_map(data->parsing.raw_map, data->parsing.player))
return (free_char_array(data->parsing.raw_map), 1);
data->map = data->parsing.raw_map;
data->parsing.raw_map = NULL;
return (0);
}
+35
View File
@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/22 18:57:21 by lfirmin #+# #+# */
/* Updated: 2025/10/08 16:21:46 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
int init_data(t_data *data, char *path)
{
if (!data)
return (ft_error(ERROR_INIT_DATA), 1);
data->texture = malloc(sizeof(t_textures));
if (!data->texture)
return (ft_error(ERROR_INIT_TEX), 1);
init_parsing(&data->parsing);
init_textures(data->texture);
data->map = NULL;
data->map_path = path;
return (0);
}
void free_data(t_data *data)
{
if (!data)
return ;
if (data->texture)
free(data->texture);
free_char_array(data->map);
}
+70
View File
@@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/22 14:56:25 by lfirmin #+# #+# */
/* Updated: 2025/10/07 12:14:42 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
void ft_error(char *message)
{
int len;
len = 0;
while (message[len])
len++;
write(2, ERROR_PREFIX, 6);
write(2, message, len);
write(2, "\n", 1);
}
void free_char_array(char **array)
{
int i;
if (!array)
return ;
i = 0;
while (array[i])
free(array[i++]);
free(array);
}
int ft_arrlen(char **arr)
{
int i;
while (arr[i])
i++;
return (i);
}
void print_array(char **array)
{
int i;
i = 0;
if (!array)
return ;
while (array[i])
{
printf("%s", array[i]);
i++;
}
}
int ft_strlen_2d(char **str)
{
int i;
i = 0;
while (str[i])
++i;
return (i);
}