summaryrefslogtreecommitdiff
path: root/render.c
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2024-07-27 15:44:45 +1000
committerquou <quou@disroot.org>2024-07-27 15:49:46 +1000
commit4cacaad9e392bf5fe63a90019411f21a72db0e7a (patch)
tree34e3cd4d576c8cbafca6f8f4d5ef381dece3a325 /render.c
parentb8b18d982af8cc8e10372f90c6c7bff4f0b58f69 (diff)
WIP raycasting
Diffstat (limited to 'render.c')
-rw-r--r--render.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/render.c b/render.c
index d36d528..8e74082 100644
--- a/render.c
+++ b/render.c
@@ -1,3 +1,4 @@
+#include "map.h"
#include "maths.h"
#include "plat.h"
#include "render.h"
@@ -686,3 +687,65 @@ void ren_mesh(
}
}
+void ren_map(
+ Renderer* r,
+ const struct Map* map,
+ const int* pos,
+ const int* dir,
+ const int* left
+) {
+ int x, w, h, hh;
+ const int* data = (const int*)&map[1];
+ w = r->vp[0] << fbits;
+ h = r->vp[1] << fbits;
+ hh = h / 2;
+ for (x = r->clip[0]; x < r->clip[2]; x++) {
+ Line l;
+ int ray = ((2 * x) << (fbits * 2)) / w - f1;
+ int d[2], plane[2];
+ d[0] = ((left[0] * ray) >> fbits);
+ d[1] = ((left[1] * ray) >> fbits);
+ plane[0] = pos[0] - d[0];
+ plane[1] = pos[1] - d[1];
+ d[0] = dir[0] - d[0];
+ d[1] = dir[1] - d[1];
+ d[0] = (pos[0] + d[0] * 4);
+ d[1] = (pos[1] + d[1] * 4);
+ init_line(&l, pos, d);
+ while (1) {
+ int mp[2], tile;
+ mp[0] = l.x >> fbits;
+ mp[1] = l.y >> fbits;
+ if (
+ mp[0] < 0 ||
+ mp[1] < 0 ||
+ mp[0] >= map->w ||
+ mp[1] >= map->h
+ ) break;
+ tile = data[mp[0] + mp[1] * map->w];
+ if (tile) {
+ int lp[2], sy, ey, d, y;
+ lp[0] = l.x;
+ lp[1] = l.y;
+ d = vec_dist(lp, lp, plane, 2);
+ sy = (r->vp[1] / 2) - (((hh << fbits) / d) >> fbits);
+ ey = (r->vp[1] / 2) + (((hh << fbits) / d) >> fbits);
+ sy = maxi(sy, r->clip[1]);
+ ey = mini(ey, r->clip[3]);
+ for (y = sy; y < ey; y++) {
+ int coord = x + y * r->vp[0];
+ Colour c = { 0, 0, 0, 255 };
+ c.r = (((16 << fbits ) - d) / 16);
+ if (d < r->d[coord]) {
+ r->d[coord] = d;
+ r->t[coord] = c;
+ }
+ }
+ break;
+ }
+ step_line(&l);
+ }
+ }
+}
+
+