int load_sectors(const char *path) { // Sector 0 is usually reserved/null state.sectors.n = 1; state.walls.n = 0; FILE *f = fopen(path, "r"); if (!f) { printf("Could not open file: %s\n", path); return -1; } enum { SCAN_SECTOR, SCAN_WALL, SCAN_NONE } ss = SCAN_NONE; char line[1024]; while (fgets(line, sizeof(line), f)) { char *p = line; // Trim leading whitespace while (isspace(*p)) p++; // Skip comments and empty lines if (!*p || *p == '#') continue; // Check for section headers like [SECTOR] if (*p == '[') { if (strstr(p, "[SECTOR]")) ss = SCAN_SECTOR; else if (strstr(p, "[WALL]")) ss = SCAN_WALL; continue; } // Parse data based on current section if (ss == SCAN_SECTOR) { struct Sector *s = &state.sectors.arr[state.sectors.n++]; // Format: ID, FirstWallIndex, NumWalls, FloorZ, CeilZ if (sscanf(p, "%d %zu %zu %f %f", &s->id, &s->firstwall, &s->nwalls, &s->floor, &s->ceil) != 5) { fclose(f); return -5; } } else if (ss == SCAN_WALL) { struct Wall *w = &state.walls.arr[state.walls.n++]; // Format: x1, y1, x2, y2, PortalID if (sscanf(p, "%f %f %f %f %d", &w->v1.x, &w->v1.y, &w->v2.x, &w->v2.y, &w->portalID) != 5) { fclose(f); return -4; } } } fclose(f); return 0; // Success } void build_sector_flats(struct Sector *s) { // We need at least 3 walls to make a floor (a triangle) if (s->nwalls < 3) return; // 1. Pick a pivot point (the start of the first wall) struct Wall *first_w = &state.walls.arr[s->firstwall]; glm::vec3 pivot_floor = glm::vec3((float)first_w->v1.x, s->floor, (float)first_w->v1.y); glm::vec3 pivot_ceil = glm::vec3((float)first_w->v1.x, s->ceil, (float)first_w->v1.y); // 2. Loop through the rest of the walls to create a "fan" of triangles for (size_t i = 1; i < s->nwalls - 1; i++) { struct Wall *w1 = &state.walls.arr[s->firstwall + i]; struct Wall *w2 = &state.walls.arr[s->firstwall + i + 1]; glm::vec3 p1_f = glm::vec3((float)w1->v1.x, s->floor, (float)w1->v1.y); glm::vec3 p2_f = glm::vec3((float)w2->v1.x, s->floor, (float)w2->v1.y); glm::vec3 p1_c = glm::vec3((float)w1->v1.x, s->ceil, (float)w1->v1.y); glm::vec3 p2_c = glm::vec3((float)w2->v1.x, s->ceil, (float)w2->v1.y); // FLOOR TRIANGLE (Facing Up) // Winding: Pivot -> P1 -> P2 (Counter-Clockwise) map_mesh.push_back({pivot_floor, {pivot_floor.x, pivot_floor.z}}); map_mesh.push_back({p1_f, {p1_f.x, p1_f.z}}); map_mesh.push_back({p2_f, {p2_f.x, p2_f.z}}); // CEILING TRIANGLE (Facing Down) // Winding: Pivot -> P2 -> P1 (Clockwise from top, CCW from bottom) map_mesh.push_back({pivot_ceil, {pivot_ceil.x, pivot_ceil.z}}); map_mesh.push_back({p2_c, {p2_c.x, p2_c.z}}); map_mesh.push_back({p1_c, {p1_c.x, p1_c.z}}); } } void push_wall_quad(glm::vec2 a, glm::vec2 b, float y_low, float y_high) { float length = glm::distance(a, b); float height = y_high - y_low; // Vertex data: Position(x, y, z), TexCoords(u, v) // Triangle 1 map_mesh.push_back({{a.x, y_low, a.y}, {0.0f, 0.0f}}); map_mesh.push_back({{b.x, y_low, b.y}, {length, 0.0f}}); map_mesh.push_back({{b.x, y_high, b.y}, {length, height}}); // Triangle 2 map_mesh.push_back({{a.x, y_low, a.y}, {0.0f, 0.0f}}); map_mesh.push_back({{b.x, y_high, b.y}, {length, height}}); map_mesh.push_back({{a.x, y_high, a.y}, {0.0f, height}}); } void build_map_mesh() { map_mesh.clear(); // Loop through sectors starting from 1 for (size_t i = 1; i < state.sectors.n; i++) { struct Sector *s = &state.sectors.arr[i]; // Loop through the walls assigned to this sector for (size_t j = 0; j < s->nwalls; j++) { struct Wall *w = &state.walls.arr[s->firstwall + j]; // Map coordinates directly from the wall struct // We cast to float because sscanf read them as ints glm::vec2 pA = glm::vec2((float)w->v1.x, (float)w->v1.y); glm::vec2 pB = glm::vec2((float)w->v2.x, (float)w->v2.y); if (w->portalID == 0) { // It's a solid wall: draw from floor to ceiling push_wall_quad(pA, pB, s->floor, s->ceil); } else { // It's a portal: draw the upper/lower differences struct Sector *neighbor = &state.sectors.arr[w->portalID]; // Draw lower "step" if neighbor floor is higher if (neighbor->floor > s->floor) push_wall_quad(pA, pB, s->floor, neighbor->floor); // Draw upper "overhang" if neighbor ceiling is lower if (neighbor->ceil < s->ceil) push_wall_quad(pA, pB, neighbor->ceil, s->ceil); } } build_sector_flats(s); } }