From cae12a86afaeadf47d35e41046737d25ba3e709e Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Sun, 28 Sep 2025 13:14:04 +0200 Subject: [PATCH] - Inici de soport per a diferents altures de sostre i piso dels sectors --- main.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 18 deletions(-) diff --git a/main.cpp b/main.cpp index da66ae3..07fc972 100644 --- a/main.cpp +++ b/main.cpp @@ -9,6 +9,8 @@ struct vec2 { float x, y; }; struct wall { Uint16 v1, v2; vec2 normal; float u1, u2; int portal; }; struct sector { + float floor_height; + float ceiling_height; std::vector walls; std::vector verts; }; @@ -25,7 +27,8 @@ std::vector sectors; int current_sector; vec2 position = { 128.0f, 192.0f }; -float height = 32.0f; +float height = 48.0f; +float real_height = 48.0f; float orientation = 90.0f; float accel = 500.0f; @@ -133,6 +136,9 @@ void createMap() current_sector = 0; { sector s; + s.floor_height = 0.0f; + s.ceiling_height = 128.0f; + s.verts.push_back({ 64.0f, 0.0f}); s.verts.push_back({256.0f, 0.0f}); s.verts.push_back({256.0f, 64.0f}); @@ -155,6 +161,9 @@ void createMap() { sector s; + s.floor_height = 0.0f; + s.ceiling_height = 128.0f; + s.verts.push_back({256.0f, 0.0f}); s.verts.push_back({ 64.0f, 0.0f}); s.verts.push_back({ 64.0f,-256.0f}); @@ -203,16 +212,19 @@ void drawColumn(sector &s, int screen_column, int start, int end, float a_inc, v } if (w) { + const int tex_height = 64; + const float sector_height = s.ceiling_height - s.floor_height; + map::putp(result.x, result.y, 6); dist *= SDL_cosf(a_inc*DEG_TO_RAD); const vec2 AB = {s.verts[w->v2].x-s.verts[w->v1].x, s.verts[w->v2].y-s.verts[w->v1].y}; const vec2 AP = {result.x-s.verts[w->v1].x, result.y-s.verts[w->v1].y}; - float v = dot(AP,AB) / dot(AB,AB); v *= w->u2; v = (v-int(v))*64.0f; - //const float v = distance(s.verts[w.v1], result); - float wall_height = (64*277)/dist; - float dpix = 64/wall_height; + float v = dot(AP,AB) / dot(AB,AB); v *= w->u2; v = (v-int(v))*tex_height; + + float wall_height = (sector_height*277)/dist; // [64=altura sector] + float dpix = tex_height/wall_height; // [64=crec que altura sector] float cpix = 0; - float wall_start = 120-(wall_height/64)*(64-height); + float wall_start = 120-(wall_height/sector_height)*(sector_height-(height-s.floor_height)); // [64=els dos crec que altura sector] if (wall_start> 1)); + float straight_dist = (277 * (sector_height-(height-s.floor_height))) / (y - (240 >> 1)); 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)) % 64; - int ty = abs(int(actual_dist * SDL_sinf(angle*DEG_TO_RAD) - position.y)) % 64; - putp(screen_column, y, gif[tx+ty*64]); + 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; + putp(screen_column, y, gif[tx+ty*tex_height]); } + if (w->portal == -1) { // Pinta la pared for (int i=0; i=end) break; - putp(screen_column, wall_start+i, gif[(int(v)%64)+int(cpix)*64]); + putp(screen_column, wall_start+i, gif[(int(v)%tex_height)+int(cpix)*tex_height]); cpix += dpix; } } else { @@ -244,11 +257,11 @@ void drawColumn(sector &s, int screen_column, int start, int end, float a_inc, v // Pinta el piso int paint_end = wall_start+wall_height-1; for (int y=paint_end+1; y> 1)); + float straight_dist = (277 * (height-s.floor_height)) / (y - (240 >> 1)); 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)) % 64; - int ty = abs(int(actual_dist * SDL_sinf(angle*DEG_TO_RAD) + position.y)) % 64; - putp(screen_column, y, gif[tx+ty*64]); + 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; + putp(screen_column, y, gif[tx+ty*tex_height]); } //line(screen_column, 120-(wall_height), screen_column, 120+(wall_height), 5); } @@ -337,7 +350,7 @@ int main(int argc, char *argv[]) millis = SDL_GetTicks(); bobbing += dt*1000.0f; - height = 32 + SDL_sinf(bobbing*DEG_TO_RAD)*(speed/100.0f); + height = real_height + SDL_sinf(bobbing*DEG_TO_RAD)*(speed/100.0f); while (SDL_PollEvent(&e)) { @@ -346,8 +359,31 @@ int main(int argc, char *argv[]) } const bool *keys = SDL_GetKeyboardState(NULL); - if (keys[SDL_SCANCODE_Q]) height += dt*speed; - if (keys[SDL_SCANCODE_A]) height -= dt*speed; + if (keys[SDL_SCANCODE_Q]) + { + real_height += dt*100.0f; + } + if (keys[SDL_SCANCODE_A]) + { + real_height -= dt*100.0f; + } + if (keys[SDL_SCANCODE_W]) + { + //real_height += dt*100.0f; + sectors[0].ceiling_height -= dt*100.0f; + sectors[1].ceiling_height -= dt*100.0f; + sectors[0].floor_height -= dt*100.0f; + sectors[1].floor_height -= dt*100.0f; + } + if (keys[SDL_SCANCODE_S]) + { + //real_height -= dt*100.0f; + sectors[0].ceiling_height += dt*100.0f; + sectors[1].ceiling_height += dt*100.0f; + sectors[0].floor_height += dt*100.0f; + sectors[1].floor_height += dt*100.0f; + } + if (keys[SDL_SCANCODE_RIGHT]) { orientation += dt*rspeed; @@ -428,6 +464,9 @@ int main(int argc, char *argv[]) } debug::println("fps:", fps); debug::println("sector:", current_sector); + debug::println("height:", real_height); + debug::println("ceil_height:", sectors[0].ceiling_height); + debug::println("floor_height:", sectors[0].floor_height); debug::render(sdl_renderer); SDL_RenderPresent(sdl_renderer);