From 4cacaad9e392bf5fe63a90019411f21a72db0e7a Mon Sep 17 00:00:00 2001 From: quou Date: Sat, 27 Jul 2024 15:44:45 +1000 Subject: WIP raycasting --- render.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'render.c') 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); + } + } +} + + -- cgit v1.2.3-54-g00ecf