summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2023-04-10 23:34:22 +0300
committerkartofen <mladenovnasko0@gmail.com>2023-04-10 23:34:22 +0300
commit878726dbc3fd8803f436ee45b1e3ccff5693de24 (patch)
tree3b7a10018e0dd8dd018f5c4d0032987bbdebf752
parent685076399535d47e056eb2341baa18888d15482b (diff)
actually works
-rw-r--r--src/main.c5
-rw-r--r--src/sector.c40
2 files changed, 38 insertions, 7 deletions
diff --git a/src/main.c b/src/main.c
index 326286c..16d127e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -36,6 +36,11 @@ int main(void)
SDL_Window *window = NULL;
map_t map = {0};
+ if(SDL_ShowCursor(SDL_DISABLE) < 0) {
+ log_error(LOG_INPUT, "SDL_ShowCursor: %s", SDL_GetError());
+ }
+
+
if(SDL_Init(SDL_INIT_VIDEO) != 0) {
log_critical(LOG_VIDEO, "SDL_Init: %s", SDL_GetError());
goto exit;
diff --git a/src/sector.c b/src/sector.c
index 2d9b087..a76b698 100644
--- a/src/sector.c
+++ b/src/sector.c
@@ -16,15 +16,27 @@ void vline(int x, int y1, int y2, uint32_t col);
int map_draw(map_t *map, int SW, int SH)
{
+ int sect_rendered[map->nsects];
+ for(size_t i = 0; i < map->nsects; i++) sect_rendered[i] = 0;
+
int ytop[SW], ybottom[SW];
for(int i = 0; i < SW; i++) {
ytop[i] = 0;
ybottom[i] = SH-1;
}
- struct { size_t sectno; int sx1, sx2; } now = {map->player.sector, 0, SW-1};
- struct sector *sect = &map->sectors[now.sectno];
+ struct render_info { size_t sectno; int sx1, sx2; } queue[MAX_QUEUE];
+ int head = -1, tail = -1;
+
+ queue[++tail] = (struct render_info){ map->player.sector, 0, SW-1 };
+
+ do {
+ head = (head+1) % MAX_QUEUE;
+ struct render_info now = queue[head];
+ if(sect_rendered[now.sectno] >= 30) continue;
+
+ struct sector *sect = &map->sectors[now.sectno];
for(size_t i = 0; i < sect->nverts; i++)
{
size_t s1 = sect->vertices[i], s0 = sect->vertices[i+1];
@@ -78,11 +90,11 @@ int map_draw(map_t *map, int SW, int SH)
int ya = (x - x1) * (y2a-y1a) / (x2-x1) + y1a, cya = clamp(ya, ytop[x],ybottom[x]); // top
int yb = (x - x1) * (y2b-y1b) / (x2-x1) + y1b, cyb = clamp(yb, ytop[x],ybottom[x]); // bottom
- /* Render ceiling: everything above this sector's ceiling height. */
- vline(x, ytop[x], cya-1, COLOR_GREEN);
- /* Render floor: everything below this sector's floor height. */
- vline(x, cyb+1, ybottom[x], COLOR_BLUE);
+ // save because we need to draw the ceilling and floor
+ // after (over) the walls
+ int old_ytop = ytop[x], old_ybottom = ybottom[x];
+ // TODO: add depth shading
if(neighbor == (size_t)-1) { // no neighbor
// vline(x, cya, cyb, x==x1||x==x2 ? : );
vline(x, cya, cyb, COLOR_WHITE);
@@ -91,14 +103,28 @@ int map_draw(map_t *map, int SW, int SH)
int nya = (x - x1) * (ny2a-ny1a) / (x2-x1) + ny1a, cnya = clamp(nya, ytop[x],ybottom[x]);
vline(x, cya, cnya-1, COLOR_WHITE); // upper wall
- // vline(x, cnya, cnyb, COLOR_RED); // portal
+ // vline(x, cnya, cnyb, COLOR_RED); // portal
vline(x, cnyb+1, cyb, COLOR_WHITE); // lower wall
ytop[x] = clamp(max(cya, cnya), ytop[x], SH-1); // Shrink the remaining window below these ceilings
ybottom[x] = clamp(min(cyb, cnyb), 0, ybottom[x]); // Shrink the remaining window above these floors
}
+
+ /* Render ceiling: everything above this sector's ceiling height. */
+ vline(x, old_ytop, cya-1, COLOR_GREEN);
+ /* Render floor: everything below this sector's floor height. */
+ vline(x, cyb+1, old_ybottom, COLOR_BLUE);
+ }
+
+ if((neighbor != (size_t)-1) && (endx >= beginx) && ((tail+1)%MAX_QUEUE != head)) {
+ tail = (tail+1) % MAX_QUEUE;
+ queue[tail] = (struct render_info){ neighbor, beginx, endx };
}
}
+
+ sect_rendered[now.sectno]++;
+ } while(head != tail);
+
return 0;
}