This commit is contained in:
Ladebeze66 2024-01-10 22:53:13 +01:00
parent 7931a8d92d
commit 2fd2c96579
12 changed files with 8914 additions and 8055 deletions

View File

@ -4,7 +4,7 @@
"C_Cpp_Runner.debuggerPath": "gdb", "C_Cpp_Runner.debuggerPath": "gdb",
"C_Cpp_Runner.cStandard": "", "C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "", "C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat", "C_Cpp_Runner.msvcBatchPath": "",
"C_Cpp_Runner.useMsvc": false, "C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [ "C_Cpp_Runner.warnings": [
"-Wall", "-Wall",

View File

@ -15,7 +15,7 @@
# define BUFFER_SIZE 100000 # define BUFFER_SIZE 100000
# endif # endif
# define PI 3.1415926535 # define PI 3.14159265359
# define P2 PI/2 # define P2 PI/2
# define P3 3*PI/2 # define P3 3*PI/2
//# define DR 0.0174533 //# define DR 0.0174533

View File

@ -41,28 +41,66 @@ void move(int key, t_structure_main *w) {
int ipx_sub_xo = (w->s_player.px - xo) / w->s_map.mapS; int ipx_sub_xo = (w->s_player.px - xo) / w->s_map.mapS;
int ipy_sub_yo = (w->s_player.py - yo) / w->s_map.mapS; int ipy_sub_yo = (w->s_player.py - yo) / w->s_map.mapS;
int collisionBuffer = 2; // Marge de collision pour éviter de coller aux murs
// Déplacement vers l'avant // Déplacement vers l'avant
if (key == 119) { if (key == 119) {
if (w->s_map.map[ipy * w->s_map.mapX + ipx_add_xo] == '0') { w->s_player.px += w->s_player.pdx; } int future_py = (int)(w->s_player.py + w->s_player.pdy + collisionBuffer); // Calculez la position future en y avec la marge
if (w->s_map.map[ipy_add_yo * w->s_map.mapX + ipx] == '0') { w->s_player.py += w->s_player.pdy; } int future_px = (int)(w->s_player.px + w->s_player.pdx + collisionBuffer); // Calculez la position future en x
int future_ipy = future_py / w->s_map.mapS; // Convertissez en coordonnées de grille
int future_ipx = future_px / w->s_map.mapS; // Convertissez en coordonnées de grille
if (w->s_map.map[future_ipy * w->s_map.mapX + future_ipx] == '0') {
w->s_player.px += w->s_player.pdx;
w->s_player.py += w->s_player.pdy;
} }
// Déplacement vers la droite
else if (key == 100) {
if (w->s_map.map[ipy * w->s_map.mapX + ipx_add_xo] == '0') { w->s_player.px += cos(w->s_player.pa + M_PI_2) * 5; }
if (w->s_map.map[ipy_add_yo * w->s_map.mapX + ipx] == '0') { w->s_player.py += sin(w->s_player.pa + M_PI_2) * 5; }
} }
// Déplacement vers l'arrière // Déplacement vers la droite
else if (key == 115) { else if (key == 100) {
if (w->s_map.map[ipy * w->s_map.mapX + ipx_sub_xo] == '0') { w->s_player.px -= w->s_player.pdx; } int future_px = w->s_player.px + (int)(cos(w->s_player.pa + M_PI_2) * 5);
if (w->s_map.map[ipy_sub_yo * w->s_map.mapX + ipx] == '0') { w->s_player.py -= w->s_player.pdy; } int future_py = w->s_player.py + (int)(sin(w->s_player.pa + M_PI_2) * 5);
} int collision_px = future_px + (int)(cos(w->s_player.pa) * collisionBuffer);
// Déplacement vers la gauche int collision_py = future_py + (int)(sin(w->s_player.pa) * collisionBuffer);
else if (key == 97) { int future_ipx = collision_px / w->s_map.mapS;
if (w->s_map.map[ipy * w->s_map.mapX + ipx_sub_xo] == '0') { w->s_player.px += cos(w->s_player.pa - M_PI_2) * 5; } int future_ipy = collision_py / w->s_map.mapS;
if (w->s_map.map[ipy_sub_yo * w->s_map.mapX + ipx] == '0') { w->s_player.py += sin(w->s_player.pa - M_PI_2) * 5; }
if (w->s_map.map[future_ipy * w->s_map.mapX + future_ipx] == '0') {
w->s_player.px = future_px;
w->s_player.py = future_py;
} }
} }
// Déplacement vers l'arrière (s)
// Déplacement vers l'arrière
else if (key == 115) {
int future_px = w->s_player.px - (int)(w->s_player.pdx * collisionBuffer);
int future_py = w->s_player.py - (int)(w->s_player.pdy * collisionBuffer);
int future_ipx = future_px / w->s_map.mapS;
int future_ipy = future_py / w->s_map.mapS;
if (w->s_map.map[future_ipy * w->s_map.mapX + future_ipx] == '0') {
w->s_player.px -= w->s_player.pdx;
w->s_player.py -= w->s_player.pdy;
}
}
// Déplacement vers la gauche
else if (key == 97) {
int future_px = w->s_player.px + (int)(cos(w->s_player.pa - M_PI_2) * 5);
int future_py = w->s_player.py + (int)(sin(w->s_player.pa - M_PI_2) * 5);
int collision_px = future_px + (int)(cos(w->s_player.pa) * collisionBuffer);
int collision_py = future_py + (int)(sin(w->s_player.pa) * collisionBuffer);
int future_ipx = collision_px / w->s_map.mapS;
int future_ipy = collision_py / w->s_map.mapS;
if (w->s_map.map[future_ipy * w->s_map.mapX + future_ipx] == '0') {
w->s_player.px = future_px;
w->s_player.py = future_py;
}
}
}
int deal_key(int key, t_structure_main *w) int deal_key(int key, t_structure_main *w)
{ {
@ -71,7 +109,7 @@ int deal_key(int key, t_structure_main *w)
kill_prog(w); kill_prog(w);
else if (key == 65361) else if (key == 65361)
{ {
w->s_player.pa -= 3*((PI/3)/1280); w->s_player.pa -= 16*((PI/3)/1280);
if (w->s_player.pa < 0) if (w->s_player.pa < 0)
w->s_player.pa += 2 * PI; w->s_player.pa += 2 * PI;
w->s_player.pdx = cos(w->s_player.pa) * 5; w->s_player.pdx = cos(w->s_player.pa) * 5;
@ -79,7 +117,7 @@ int deal_key(int key, t_structure_main *w)
} }
else if (key == 65363) else if (key == 65363)
{ {
w->s_player.pa += 3*((PI/3)/1280); w->s_player.pa += 16*((PI/3)/1280);
if (w->s_player.pa > 2 * PI) if (w->s_player.pa > 2 * PI)
w->s_player.pa -= 2 * PI; w->s_player.pa -= 2 * PI;
w->s_player.pdx = cos(w->s_player.pa) * 5; w->s_player.pdx = cos(w->s_player.pa) * 5;

62
main.c
View File

@ -13,8 +13,6 @@ void put_pixel_img(t_structure_main *w, int x, int y, int color) {
} }
} }
void draw_square_raw(t_structure_main *w, int x, int y, int xo, int yo, int color) void draw_square_raw(t_structure_main *w, int x, int y, int xo, int yo, int color)
{ {
int i; int i;
@ -22,8 +20,8 @@ void draw_square_raw(t_structure_main *w, int x, int y, int xo, int yo, int colo
int size_x = abs(xo - x); int size_x = abs(xo - x);
int size_y = abs(yo - y); int size_y = abs(yo - y);
printf("draw_square_raw called with x: %d, y: %d, xo: %d, yo: %d\n", x, y, xo, yo); //printf("draw_square_raw called with x: %d, y: %d, xo: %d, yo: %d\n", x, y, xo, yo);
printf("Calculated size_x: %d, size_y: %d\n", size_x, size_y); //printf("Calculated size_x: %d, size_y: %d\n", size_x, size_y);
for (i = 0; i < size_y; i++) for (i = 0; i < size_y; i++)
{ {
@ -144,23 +142,12 @@ void rescale_image(void *mlx, void *win, void *original_img, int original_width,
} }
float correctFisheye(float distance, float ra, float playerAngle) { float correctFisheye(float distance, float ra, float playerAngle) {
// Ajouter des logs pour vérifier les valeurs en entrée
//printf("Input Distance: %f\n", distance);
//printf("Input Ray Angle: %f\n", ra);
//printf("Input Player Angle: %f\n", playerAngle);
float ca = playerAngle - ra; float ca = playerAngle - ra;
if (ca < 0) ca += 2 * PI; if (ca < 0) ca += 2 * PI;
if (ca > 2 * PI) ca -= 2 * PI; if (ca > 2 * PI) ca -= 2 * PI;
float correctedDistance = distance * cos(ca); return distance * cos(ca);
// Ajouter un log pour vérifier la valeur corrigée
//printf("Corrected Distance: %f\n", correctedDistance);
return correctedDistance;
} }
void calculateVerticalRay(t_structure_main *w, float ra, float *disV, float *vx, float *vy, WallDirection *wallDir) { void calculateVerticalRay(t_structure_main *w, float ra, float *disV, float *vx, float *vy, WallDirection *wallDir) {
int dof = 0, mx, my, mp; int dof = 0, mx, my, mp;
float rx, ry, xo, yo; float rx, ry, xo, yo;
@ -377,12 +364,14 @@ int getTextureColor(t_structure_main *w, WallDirection wallDir, int textureX, in
return color; return color;
} }
static void draw_texture(t_structure_main *w, int startX, int endX, int lineOff, int lineH, WallDirection wallDir, float rx, float ry) { static void draw_texture(t_structure_main *w, int startX, int endX, int lineOff, int lineH, WallDirection wallDir, float rx, float ry, float disT) {
int textureWidth = w->s_img.texture_width; int textureWidth = w->s_img.texture_width;
int textureHeight = w->s_img.texture_height; int textureHeight = w->s_img.texture_height;
for (int y = lineOff; y < lineOff + lineH; y++) { for (int y = lineOff; y < lineOff + lineH; y++) {
int textureY = ((float)(y - lineOff) / lineH) * textureHeight; // Ajustement de la texture Y // La variable perspectiveFactor permet de mapper la texture en tenant compte de la perspective
float perspectiveFactor = (float)(y - lineOff) / lineH;
int textureY = perspectiveFactor * textureHeight;
if (textureY >= textureHeight) { if (textureY >= textureHeight) {
textureY = textureHeight - 1; textureY = textureHeight - 1;
} }
@ -392,11 +381,13 @@ static void draw_texture(t_structure_main *w, int startX, int endX, int lineOff,
switch (wallDir) { switch (wallDir) {
case NORTH: case NORTH:
case SOUTH: case SOUTH:
textureX = (int)(rx * textureWidth) % textureWidth; // Assurez-vous que rx est normalisé correctement pour le mappage de texture
textureX = (int)(rx * textureWidth / w->s_map.mapS) % textureWidth;
break; break;
case WEST: case WEST:
case EAST: case EAST:
textureX = (int)(ry * textureWidth) % textureWidth; // Assurez-vous que ry est normalisé correctement pour le mappage de texture
textureX = (int)(ry * textureWidth / w->s_map.mapS) % textureWidth;
break; break;
} }
if (textureX >= textureWidth) { if (textureX >= textureWidth) {
@ -409,6 +400,8 @@ static void draw_texture(t_structure_main *w, int startX, int endX, int lineOff,
} }
void drawRay(t_structure_main *w, int r, float rx, float ry, float disT, WallDirection wallDir, int numRays, int color) { void drawRay(t_structure_main *w, int r, float rx, float ry, float disT, WallDirection wallDir, int numRays, int color) {
int tileSize = w->s_map.mapS; int tileSize = w->s_map.mapS;
int start3DHeight = 0; // Début en haut de la fenêtre int start3DHeight = 0; // Début en haut de la fenêtre
@ -422,7 +415,7 @@ void drawRay(t_structure_main *w, int r, float rx, float ry, float disT, WallDir
float lineH = (tileSize * max3DHeight) / disT; float lineH = (tileSize * max3DHeight) / disT;
lineH = lineH > max3DHeight ? max3DHeight : lineH; lineH = lineH > max3DHeight ? max3DHeight : lineH;
float lineOff = ((max3DHeight - lineH) / 2); float lineOff = ((max3DHeight - lineH) / 2);
//printf("drawRay - r: %d, rx: %f, ry: %f, disT: %f, wallDir: %d, numRays: %d\n", r, rx, ry, disT, wallDir, numRays); printf("drawRay - rx: %f, ry: %f\n", rx, ry);
// Décalage horizontal de l'arrière-plan // Décalage horizontal de l'arrière-plan
@ -442,27 +435,21 @@ void drawRay(t_structure_main *w, int r, float rx, float ry, float disT, WallDir
// Ajouter des instructions de débogage pour afficher les valeurs de startX et endX // Ajouter des instructions de débogage pour afficher les valeurs de startX et endX
//printf("Ray %d - startX: %d, endX: %d\n", r, startX, endX); //printf("Ray %d - startX: %d, endX: %d\n", r, startX, endX);
printf("drawRay - rx: %f, ry: %f\n", rx, ry);
// Dessiner le rayon // Dessiner le rayon
draw_line(w, (int)w->s_player.px, (int)w->s_player.py, (int)rx + backgroundOffsetX, (int)ry, color); draw_line(w, (int)w->s_player.px, (int)w->s_player.py, (int)rx + backgroundOffsetX, (int)ry, color);
// Dessiner la texture ajustée pour le décalage // Dessiner la texture ajustée pour le décalage
draw_texture(w, startX, endX, lineOff, lineH, wallDir, rx + backgroundOffsetX, ry); draw_texture(w, startX, endX, lineOff, lineH, wallDir, rx + backgroundOffsetX, ry, disT);
} }
void drawRays2D(t_structure_main *w) { void drawRays2D(t_structure_main *w) {
int r, color; int r, color;
float ra, disH, disV, disT, hx, hy, vx, vy; float ra, disH, disV, disT, hx, hy, vx, vy;
WallDirection hWallDir, vWallDir; WallDirection hWallDir, vWallDir;
int tileSize = w->s_map.mapS; int tileSize = w->s_map.mapS;
int numRays = 1280; int numRays = 1280;
float FOV = 80 * (PI / 180); float FOV = 60 * (PI / 180);
float DR = FOV / numRays; float DR = FOV / numRays;
// Ajouter des logs pour vérifier les valeurs // Ajouter des logs pour vérifier les valeurs
@ -485,6 +472,7 @@ void drawRays2D(t_structure_main *w) {
disT = correctFisheye(disT, ra, w->s_player.pa); disT = correctFisheye(disT, ra, w->s_player.pa);
// Ajouter des logs pour déboguer // Ajouter des logs pour déboguer
//printf("Ray ID: %d\n", r); //printf("Ray ID: %d\n", r);
//printf("Ray Angle: %f\n", ra); //printf("Ray Angle: %f\n", ra);
@ -527,16 +515,13 @@ void draw_map(t_structure_main *w) {
switch (w->s_map.map[index]) { switch (w->s_map.map[index]) {
case '1': case '1':
// Couleur grise avec une transparence de 30% color = 0xFFFFFF;
color = (int)(0.5 * 255) << 24 | 0xAAAAAA;
break; break;
case '0': case '0':
// Couleur noire avec une transparence de 30% color = 0x000000;
color = (int)(0.0 * 255) << 24 | 0x000000;
break; break;
default: default:
// Couleur par défaut avec une transparence de 30% color = 0x000000;
color = (int)(0.0 * 255) << 24 | 0x000000;
break; break;
} }
@ -555,8 +540,9 @@ void test2(t_structure_main *w) {
mlx_destroy_image(w->s_win.mlx, w->s_img.buffer); mlx_destroy_image(w->s_win.mlx, w->s_img.buffer);
w->s_img.buffer = mlx_new_image(w->s_win.mlx, w->s_win.width, w->s_win.height); w->s_img.buffer = mlx_new_image(w->s_win.mlx, w->s_win.width, w->s_win.height);
w->s_img.addr = mlx_get_data_addr(w->s_img.buffer, &(w->s_img.bpp), &(w->s_img.line_len), &(w->s_img.endian)); w->s_img.addr = mlx_get_data_addr(w->s_img.buffer, &(w->s_img.bpp), &(w->s_img.line_len), &(w->s_img.endian));
drawRays2D(w); draw_map(w);
draw_map(w); drawRays2D(w);
//drawRays2D(w); //drawRays2D(w);
mlx_put_image_to_window(w->s_win.mlx, w->s_win.win, w->s_img.buffer, 0, 0); mlx_put_image_to_window(w->s_win.mlx, w->s_win.win, w->s_img.buffer, 0, 0);

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff