- Nou sector afegit al mapa de proba.

- Portals per a pasar entre sectors.
- Dibuixat de sectors més allà dels portals.
- Calculs de colisió concretats i afegida condició per a pasar de sector en sector al pasar un portal.
- Moviment amb acceleració.
This commit is contained in:
2025-09-26 14:42:22 +02:00
parent 513f7426bf
commit 2a147fb7f8

185
main.cpp
View File

@@ -7,7 +7,7 @@
#define DEG_TO_RAD M_PI/180.0f
struct vec2 { float x, y; };
struct wall { Uint16 v1, v2; vec2 normal; float u1, u2; };
struct wall { Uint16 v1, v2; vec2 normal; float u1, u2; int portal; };
struct sector {
std::vector<wall> walls;
std::vector<vec2> verts;
@@ -25,10 +25,17 @@ std::vector<sector> sectors;
int current_sector;
vec2 position = { 128.0f, 192.0f };
float speed = 200.0f;
float height = 32.0f;
float orientation = 90.0f;
float accel = 500.0f;
float speed = 0.0f;
float max_speed = 200.0f;
float rspeed = 200.0f;
float dt;
// Returns 1 if the lines intersect, otherwise 0. In addition, if the lines
// intersect the intersection point may be stored in the floats i_x and i_y.
const bool get_line_intersection(vec2 p0, vec2 p1, vec2 p2, vec2 p3, vec2 *i)
@@ -109,35 +116,53 @@ void line(int x1, int y1, int x2, int y2, Uint8 color)
void createMap()
{
current_sector = 0;
sector s;
s.verts.push_back({ 64.0f, 0.0f});
s.verts.push_back({256.0f, 0.0f});
s.verts.push_back({256.0f, 64.0f});
s.verts.push_back({320.0f, 64.0f});
s.verts.push_back({320.0f, 320.0f});
s.verts.push_back({ 0.0f, 320.0f});
s.verts.push_back({ 0.0f, 64.0f});
s.verts.push_back({ 64.0f, 64.0f});
s.walls.push_back({0,1,{0,0},0.0f,0.0f});
s.walls.push_back({1,2,{0,0},0.0f,0.0f});
s.walls.push_back({2,3,{0,0},0.0f,0.0f});
s.walls.push_back({3,4,{0,0},0.0f,0.0f});
s.walls.push_back({4,5,{0,0},0.0f,0.0f});
s.walls.push_back({5,6,{0,0},0.0f,0.0f});
s.walls.push_back({6,7,{0,0},0.0f,0.0f});
s.walls.push_back({7,0,{0,0},0.0f,0.0f});
for (auto &w : s.walls )
{
w.u2 = distance(s.verts[w.v1], s.verts[w.v2]) / 64.0f;
vec2 norm = { s.verts[w.v2].x - s.verts[w.v1].x, s.verts[w.v2].y - s.verts[w.v1].y};
normalize(&norm);
const float tmp = norm.x; norm.x = -norm.y; norm.y = tmp;
w.normal = norm;
}
sectors.push_back(s);
sector s;
s.verts.push_back({ 64.0f, 0.0f});
s.verts.push_back({256.0f, 0.0f});
s.verts.push_back({256.0f, 64.0f});
s.verts.push_back({320.0f, 64.0f});
s.verts.push_back({320.0f, 320.0f});
s.verts.push_back({ 0.0f, 320.0f});
s.verts.push_back({ 0.0f, 64.0f});
s.verts.push_back({ 64.0f, 64.0f});
s.walls.push_back({0,1,{0,0},0.0f,0.0f,1});
s.walls.push_back({1,2,{0,0},0.0f,0.0f,-1});
s.walls.push_back({2,3,{0,0},0.0f,0.0f,-1});
s.walls.push_back({3,4,{0,0},0.0f,0.0f,-1});
s.walls.push_back({4,5,{0,0},0.0f,0.0f,-1});
s.walls.push_back({5,6,{0,0},0.0f,0.0f,-1});
s.walls.push_back({6,7,{0,0},0.0f,0.0f,-1});
s.walls.push_back({7,0,{0,0},0.0f,0.0f,-1});
sectors.push_back(s);
}
{
sector s;
s.verts.push_back({256.0f, 0.0f});
s.verts.push_back({ 64.0f, 0.0f});
s.verts.push_back({ 64.0f,-256.0f});
s.verts.push_back({256.0f,-256.0f});
s.walls.push_back({0,1,{0,0},0.0f,0.0f,0});
s.walls.push_back({1,2,{0,0},0.0f,0.0f,-1});
s.walls.push_back({2,3,{0,0},0.0f,0.0f,-1});
s.walls.push_back({3,0,{0,0},0.0f,0.0f,-1});
sectors.push_back(s);
}
for (auto &s : sectors )
{
for (auto &w : s.walls )
{
w.u2 = distance(s.verts[w.v1], s.verts[w.v2]) / 64.0f;
vec2 norm = { s.verts[w.v2].x - s.verts[w.v1].x, s.verts[w.v2].y - s.verts[w.v1].y};
normalize(&norm);
const float tmp = norm.x; norm.x = -norm.y; norm.y = tmp;
w.normal = norm;
}
}
}
void drawColumn(sector &s, int screen_column, int start, int end, float a_inc, vec2 infi)
@@ -161,7 +186,8 @@ void drawColumn(sector &s, int screen_column, int start, int end, float a_inc, v
}
}
}
if (w) {
if (w)
{
putp(int(result.x/8),int(result.y/8),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};
@@ -187,11 +213,17 @@ void drawColumn(sector &s, int screen_column, int start, int end, float a_inc, v
putp(screen_column, y, gif[tx+ty*64]);
}
// Pinta la pared
for (int i=0; i<wall_height; ++i) {
if (wall_start+i>=end) break;
putp(screen_column, wall_start+i, gif[(int(v)%64)+int(cpix)*64]);
cpix += dpix;
if (w->portal == -1)
{
// Pinta la pared
for (int i=0; i<wall_height; ++i) {
if (wall_start+i>=end) break;
putp(screen_column, wall_start+i, gif[(int(v)%64)+int(cpix)*64]);
cpix += dpix;
}
} else {
const int wall_end = SDL_min(wall_start+wall_height+1, 240);
drawColumn(sectors[w->portal], screen_column, wall_start, wall_end, a_inc, infi);
}
// Pinta el piso
@@ -207,6 +239,48 @@ void drawColumn(sector &s, int screen_column, int start, int end, float a_inc, v
}
}
bool tryMove(float angle, float speed)
{
bool moved = false;
sector &s = sectors[current_sector];
vec2 newpos = { position.x + SDL_cosf(angle*DEG_TO_RAD)*5, position.y };
bool collision=false;
for (auto w : s.walls) {
if (get_line_intersection(position, newpos, s.verts[w.v1], s.verts[w.v2], NULL))
{
if (w.portal != -1) {
newpos.x = position.x + SDL_cosf(angle*DEG_TO_RAD)*dt*speed;
if (get_line_intersection(position, newpos, s.verts[w.v1], s.verts[w.v2], NULL) )
current_sector = w.portal;
} else {
collision=true;
}
break;
}
}
if (!collision) { moved = true; position.x += SDL_cosf(angle*DEG_TO_RAD)*dt*speed; }
newpos = { position.x, position.y + SDL_sinf(angle*DEG_TO_RAD)*5 };
collision=false;
for (auto w : s.walls) {
if (get_line_intersection(position, newpos, s.verts[w.v1], s.verts[w.v2], NULL))
{
if (w.portal != -1) {
newpos.y = position.y + SDL_sinf(angle*DEG_TO_RAD)*dt*speed;
if (get_line_intersection(position, newpos, s.verts[w.v1], s.verts[w.v2], NULL) )
current_sector = w.portal;
} else {
collision=true;
}
break;
}
}
if (!collision) { moved = true; position.y += SDL_sinf(angle*DEG_TO_RAD)*dt*speed; }
return moved;
}
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
@@ -234,7 +308,6 @@ int main(int argc, char *argv[])
SDL_Event e;
bool should_exit = false;
Uint32 millis = SDL_GetTicks();
float dt;
int fps = 0;
int fps_count = 0;
@@ -251,30 +324,35 @@ int main(int argc, char *argv[])
if (e.type==SDL_EVENT_KEY_DOWN && e.key.scancode==SDL_SCANCODE_ESCAPE) { should_exit=true; break; }
}
sector &s = sectors[current_sector];
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_RIGHT]) orientation += dt*speed;
if (keys[SDL_SCANCODE_LEFT]) orientation -= dt*speed;
if (keys[SDL_SCANCODE_RIGHT])
{
orientation += dt*rspeed;
}
else if (keys[SDL_SCANCODE_LEFT])
{
orientation -= dt*rspeed;
}
if (keys[SDL_SCANCODE_UP])
{
vec2 newpos = { position.x + SDL_cosf(orientation*DEG_TO_RAD)*5, position.y };
bool collision=false;
for (auto w : s.walls) if (get_line_intersection(position, newpos, s.verts[w.v1], s.verts[w.v2], NULL)) { collision=true; break; }
if (!collision) position.x += SDL_cosf(orientation*DEG_TO_RAD)*dt*speed;
newpos = { position.x, position.y + SDL_sinf(orientation*DEG_TO_RAD)*5 };
collision=false;
for (auto w : s.walls) if (get_line_intersection(position, newpos, s.verts[w.v1], s.verts[w.v2], NULL)) { collision=true; break; }
if (!collision) position.y += SDL_sinf(orientation*DEG_TO_RAD)*dt*speed;
if (speed < max_speed) speed += dt*accel;
}
if (keys[SDL_SCANCODE_DOWN])
else if (keys[SDL_SCANCODE_DOWN])
{
position.x -= SDL_cosf(orientation*DEG_TO_RAD)*dt*speed;
position.y -= SDL_sinf(orientation*DEG_TO_RAD)*dt*speed;
if (speed > -max_speed) speed -= dt*accel;
}
else
{
if (speed > 0.0f) { speed -= dt*accel; if (speed < 0.0f) speed = 0.0f; }
if (speed < 0.0f) { speed += dt*accel; if (speed > 0.0f) speed = 0.0f; }
}
if (speed != 0.0f) if (!tryMove(orientation, speed)) speed = 0.0f;
sector &s = sectors[current_sector];
// Clear screen
SDL_memset4(screen, 0x00000000, (320*240)>>2);
@@ -287,7 +365,7 @@ int main(int argc, char *argv[])
infi.x = position.x + SDL_cosf(angle*DEG_TO_RAD)*40000;
infi.y = position.y + SDL_sinf(angle*DEG_TO_RAD)*40000;
drawColumn(s, screen_column, 10, 220, a_inc, infi);
drawColumn(s, screen_column, 0, 240, a_inc, infi);
/*
vec2 result, tmp_result;
wall *w = nullptr;
@@ -384,6 +462,7 @@ int main(int argc, char *argv[])
fps_time = SDL_GetTicks();
}
debug::println("fps:", fps);
debug::println("sector:", current_sector);
debug::render(sdl_renderer);
SDL_RenderPresent(sdl_renderer);