From 8c20e2a6c4ba2a1793f9bb6194f3ee1505e6a5db Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Mon, 29 Sep 2025 12:45:07 +0200 Subject: [PATCH] - Prototip de dibuixat de sprites funcional --- main.cpp | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index 67e4d30..6f74499 100644 --- a/main.cpp +++ b/main.cpp @@ -103,6 +103,16 @@ void putpd(int x, int y, Uint8 color, float depth) depth_buffer[x+y*320]=depth; } +void putps(int x, int y, Uint8 color, float depth) +{ + if (x<0 || y<0 || x>=320 || y>=240) return; + if (depth_buffer[x+y*320]>=depth) + { + screen[x+y*320]=color; + depth_buffer[x+y*320]=depth; + } +} + void line(int x1, int y1, int x2, int y2, Uint8 color) { float dx = float(x2-x1); @@ -258,7 +268,7 @@ void drawColumn(sector &s, int screen_column, int start, int end, float a_inc, v float actual_dist = straight_dist / SDL_cosf(a_inc*DEG_TO_RAD); int tx = abs(int(actual_dist * SDL_cosf(angle*DEG_TO_RAD) - position.x)) % tex_height; int ty = abs(int(actual_dist * SDL_sinf(angle*DEG_TO_RAD) - position.y)) % tex_height; - putpd(screen_column, y, gif[tx+ty*tex_height], straight_dist); + putpd(screen_column, y, gif[tx+ty*tex_height], -straight_dist); } @@ -419,6 +429,9 @@ int main(int argc, char *argv[]) palette = LoadPalette(buffer); free(buffer); + // [DEBUG] Paleta per al depth buffer + //for(int i=0;i<256;++i) palette[i] = (255-i) | ((255-i)<<8) | ((255-i)<<16); + createMap(); SDL_Event e; @@ -517,11 +530,26 @@ int main(int argc, char *argv[]) map::putp(enemy.x, enemy.y, 9); float angle_to_enemy = is_enemy_in_fov(position.x, position.y, orientation, enemy.x, enemy.y); - if (SDL_fabsf(angle_to_enemy) <= 32.0f) + if (SDL_fabsf(angle_to_enemy) <= 64.0f) { + const float d = distance(position, enemy);// * SDL_cosf(a_inc*DEG_TO_RAD); + float dist = d * SDL_cosf(angle_to_enemy*DEG_TO_RAD); + float wall_height = (32.0f*277)/dist; debug::print("enemy in fov: "); debug::print(angle_to_enemy); debug::newline(); int column = int((angle_to_enemy+32.0f)/0.2f); - line(column, 0, column, 239, 0); + for (int i=0; i=0 && c<320) { + for (int j=0;j