intra to gitea

This commit is contained in:
root
2025-11-15 22:16:13 +00:00
parent 0c0d28ab36
commit 50b45e597f
27 changed files with 888 additions and 624 deletions
+44 -23
View File
@@ -6,7 +6,7 @@
/* 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 */
/* Updated: 2025/10/29 15:30:00 by jle-neze ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,46 +14,67 @@
void draw_background(t_game *g)
{
int x, y, half = g->gfx.h / 2;
int x;
int y;
int half;
for (y = 0; y < g->gfx.h; y++)
y = 0;
half = g->gfx.h / 2;
while (y < g->gfx.h)
{
for (x = 0; x < g->gfx.w; x++)
x = 0;
while (x < g->gfx.w)
{
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);
x++;
}
y++;
}
}
/* 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)
void draw_vline(t_game *g, int x, t_vline v)
{
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);
int y;
if (v.y0 < 0)
v.y0 = 0;
if (v.y1 >= g->gfx.h)
v.y1 = g->gfx.h - 1;
y = v.y0;
while (y <= v.y1)
{
img_put_pixel(&g->gfx.frame, x, y, v.color);
y++;
}
}
void draw_tex_vline(t_game *g, int x, int y0, int y1,
t_tex *tex, int tex_x, double step, double tex_pos)
void draw_tex_vline(t_game *g, int x, t_texline l)
{
if (y0 < 0)
int y;
int tex_y;
unsigned int color;
if (l.y0 < 0)
{
tex_pos += step * (-y0);
y0 = 0;
l.tex_pos += l.step * (-l.y0);
l.y0 = 0;
}
if (y1 >= g->gfx.h)
y1 = g->gfx.h - 1;
for (int y = y0; y <= y1; y++)
if (l.y1 >= g->gfx.h)
l.y1 = g->gfx.h - 1;
y = l.y0;
while (y <= l.y1)
{
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);
tex_y = (int)l.tex_pos;
if (tex_y < 0)
tex_y = 0;
if (tex_y >= l.tex->h)
tex_y = l.tex->h - 1;
color = tex_get_pixel(l.tex, l.tex_x, tex_y);
img_put_pixel(&g->gfx.frame, x, y, (int)color);
tex_pos += step;
l.tex_pos += l.step;
y++;
}
}
+15 -14
View File
@@ -19,7 +19,6 @@ 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);
@@ -29,15 +28,20 @@ int on_destroy(t_game *g)
return (0);
}
static void set_key(int key, t_game *g, int press)
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;
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)
@@ -56,11 +60,8 @@ int on_keyup(int key, t_game *g)
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_hook(g->gfx.win, 2, 1L << 0, on_keydown, g);
mlx_hook(g->gfx.win, 3, 1L << 1, on_keyup, g);
mlx_loop_hook(g->gfx.mlx, game_loop, g);
}
+1 -1
View File
@@ -52,4 +52,4 @@ void cleanup_window(t_game *g)
free(g->gfx.mlx);
g->gfx.mlx = NULL;
}
}
}
-3
View File
@@ -14,12 +14,9 @@
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 */
+51 -54
View File
@@ -5,76 +5,73 @@
/* +:+ +:+ +:+ */
/* 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 */
/* Created: 2025/10/28 16:20:00 by jle-neze #+# #+# */
/* Updated: 2025/10/28 16:20:00 by jle-neze ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
#include <math.h>
void player_init(t_game *g, double px, double py, double dx, double dy)
void player_init(t_game *g, t_player_init p)
{
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 */
g->cam.pos_x = p.px;
g->cam.pos_y = p.py;
g->cam.dir_x = p.dx;
g->cam.dir_y = p.dy;
g->cam.plane_x = -p.dy * 0.66;
g->cam.plane_y = p.dx * 0.66;
g->move_speed = 0.03;
g->rot_speed = 0.05;
}
/* petits helpers collision */
static int is_wall(t_world *w, int mx, int my)
static int is_wall(t_world *w, int mx, int my)
{
if (mx < 0 || my < 0 || mx >= w->w || my >= w->h) return (1);
if (mx < 0 || my < 0 || mx >= w->w || my >= w->h)
return (1);
return (w->grid[my][mx] == '1');
}
static void player_move(t_game *g, double dx, double dy)
{
double nx;
double ny;
nx = g->cam.pos_x + dx * g->move_speed;
ny = g->cam.pos_y + dy * 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;
}
static void player_rotate(t_game *g, int dir)
{
double rs;
double old_dir_x;
double old_plane_x;
rs = g->rot_speed * (dir);
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);
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);
}
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;
}
player_move(g, g->cam.dir_x, g->cam.dir_y);
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) */
player_move(g, -g->cam.dir_x, -g->cam.dir_y);
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;
}
player_move(g, -g->cam.dir_y, g->cam.dir_x);
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);
}
player_move(g, g->cam.dir_y, -g->cam.dir_x);
if (g->in.left)
player_rotate(g, -1);
else if (g->in.right)
player_rotate(g, 1);
}
+79 -224
View File
@@ -5,239 +5,94 @@
/* +:+ +:+ +:+ */
/* 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 */
/* Created: 2025/10/29 18:15:00 by jle-neze #+# #+# */
/* Updated: 2025/10/29 18:15:00 by jle-neze ### ########.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)
static void init_ray_vars(t_game *g, int x, t_ray *r)
{
const double EPS = 1e-6;
double cam;
for (int x = 0; x < g->gfx.w; x++)
cam = 2.0 * x / (double)g->gfx.w - 1.0;
r->ray_dir_x = g->cam.dir_x + g->cam.plane_x * cam;
r->ray_dir_y = g->cam.dir_y + g->cam.plane_y * cam;
r->map_x = (int)g->cam.pos_x;
r->map_y = (int)g->cam.pos_y;
if (r->ray_dir_x == 0)
r->delta_dist_x = 1e30;
else
r->delta_dist_x = fabs(1.0 / r->ray_dir_x);
if (r->ray_dir_y == 0)
r->delta_dist_y = 1e30;
else
r->delta_dist_y = fabs(1.0 / r->ray_dir_y);
}
static void calc_step_side(t_game *g, t_ray *r)
{
if (r->ray_dir_x < 0)
{
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. */
}
r->step_x = -1;
r->side_dist_x = (g->cam.pos_x - r->map_x) * r->delta_dist_x;
}
else
{
r->step_x = 1;
r->side_dist_x = (r->map_x + 1.0 - g->cam.pos_x) * r->delta_dist_x;
}
if (r->ray_dir_y < 0)
{
r->step_y = -1;
r->side_dist_y = (g->cam.pos_y - r->map_y) * r->delta_dist_y;
}
else
{
r->step_y = 1;
r->side_dist_y = (r->map_y + 1.0 - g->cam.pos_y) * r->delta_dist_y;
}
}
static void perform_dda(t_game *g, t_ray *r)
{
int hit;
hit = 0;
while (!hit)
{
if (r->side_dist_x < r->side_dist_y)
{
r->side_dist_x += r->delta_dist_x;
r->map_x += r->step_x;
r->side = 0;
}
else
{
r->side_dist_y += r->delta_dist_y;
r->map_y += r->step_y;
r->side = 1;
}
if (r->map_x < 0 || r->map_y < 0 || r->map_x >= g->world.w
|| r->map_y >= g->world.h
|| g->world.grid[r->map_y][r->map_x] == '1')
hit = 1;
}
}
void raycast_frame(t_game *g)
{
int x;
t_ray r;
x = 0;
while (x < g->gfx.w)
{
init_ray_vars(g, x, &r);
calc_step_side(g, &r);
perform_dda(g, &r);
draw_column(g, x, &r);
x++;
}
}
+97
View File
@@ -0,0 +1,97 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* raycast_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jle-neze <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/29 18:20:00 by jle-neze #+# #+# */
/* Updated: 2025/10/29 18:20:00 by jle-neze ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
static void draw_textured(t_game *g, t_texdraw d)
{
double wall_x;
int tex_x;
double step;
double tex_pos;
t_texline l;
if (d.r->side == 0)
wall_x = g->cam.pos_y + d.c.perp_dist * d.r->ray_dir_y;
else
wall_x = g->cam.pos_x + d.c.perp_dist * d.r->ray_dir_x;
wall_x -= floor(wall_x);
tex_x = (int)(wall_x * (double)d.t->w);
if ((d.r->side == 0 && d.r->ray_dir_x > 0)
|| (d.r->side == 1 && d.r->ray_dir_y < 0))
tex_x = d.t->w - tex_x - 1;
step = (double)d.t->h / (double)d.c.line_h;
tex_pos = (d.c.draw_start - g->gfx.h / 2 + d.c.line_h / 2) * step;
l.tex = d.t;
l.tex_x = tex_x;
l.step = step;
l.tex_pos = tex_pos;
l.y0 = d.c.draw_start;
l.y1 = d.c.draw_end;
draw_tex_vline(g, d.x, l);
}
static void set_tex_id(t_ray *r)
{
if (r->side == 0)
{
if (r->step_x < 0)
r->tex_id = 2;
else
r->tex_id = 3;
}
else
{
if (r->step_y < 0)
r->tex_id = 1;
else
r->tex_id = 0;
}
}
static void draw_color_column(t_game *g, int x, t_ray *r, t_col c)
{
t_vline v;
v.y0 = c.draw_start;
v.y1 = c.draw_end;
v.color = g->colors.wall_nsew[r->tex_id];
if (r->side == 1)
v.color = (v.color & 0xFEFEFE) >> 1;
draw_vline(g, x, v);
}
void draw_column(t_game *g, int x, t_ray *r)
{
t_tex *t;
t_col c;
t_texdraw d;
if (r->side == 0)
c.perp_dist = r->side_dist_x - r->delta_dist_x;
else
c.perp_dist = r->side_dist_y - r->delta_dist_y;
if (c.perp_dist < 1e-6)
c.perp_dist = 1e-6;
c.line_h = (int)(g->gfx.h / c.perp_dist);
c.draw_start = -c.line_h / 2 + g->gfx.h / 2;
c.draw_end = c.line_h / 2 + g->gfx.h / 2;
set_tex_id(r);
if (!g->has_tex)
return (draw_color_column(g, x, r, c));
t = &g->tex[r->tex_id];
d.x = x;
d.r = r;
d.t = t;
d.c = c;
draw_textured(g, d);
}
+13 -8
View File
@@ -21,13 +21,12 @@ static int load_one(t_game *g, t_tex *t, const char *path)
return (0);
}
int textures_load(t_game *g, const char *no, const char *so,
const char *we, const char *ea)
int textures_load(t_game *g, t_texpaths *paths)
{
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))
if (load_one(g, &g->tex[0], paths->no)
|| load_one(g, &g->tex[1], paths->so)
|| load_one(g, &g->tex[2], paths->we)
|| load_one(g, &g->tex[3], paths->ea))
{
textures_free(g);
return (1);
@@ -38,16 +37,22 @@ int textures_load(t_game *g, const char *no, const char *so,
void textures_free(t_game *g)
{
for (int i = 0; i < 4; i++)
int i;
i = 0;
while (i < 4)
{
if (g->tex[i].ptr)
mlx_destroy_image(g->gfx.mlx, g->tex[i].ptr);
g->tex[i].ptr = NULL;
i++;
}
}
unsigned int tex_get_pixel(t_tex *t, int x, int y)
{
char *px = t->addr + (y * t->line_len + x * (t->bpp / 8));
char *px;
px = t->addr + (y * t->line_len + x * (t->bpp / 8));
return (*(unsigned int *)px);
}
+53 -46
View File
@@ -25,57 +25,64 @@
// "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
};
// 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);
}
// 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++)
int y;
if (!w->grid)
return ;
y = 0;
while (y < w->h)
{
free(w->grid[y]);
y++;
}
free(w->grid);
w->grid = NULL;
}