From 4cacaad9e392bf5fe63a90019411f21a72db0e7a Mon Sep 17 00:00:00 2001 From: quou Date: Sat, 27 Jul 2024 15:44:45 +1000 Subject: WIP raycasting --- 3de.c | 52 ++++++++++++++++++++++++++++++++++++++++------------ map.h | 8 ++++++++ player.c | 10 +++++++--- player.h | 1 + render.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ render.h | 10 ++++++++++ 6 files changed, 129 insertions(+), 15 deletions(-) create mode 100644 map.h diff --git a/3de.c b/3de.c index e18b643..1e40f91 100644 --- a/3de.c +++ b/3de.c @@ -1,5 +1,6 @@ #include "asset.h" #include "config.h" +#include "map.h" #include "maths.h" #include "memory.h" #include "plat.h" @@ -21,9 +22,9 @@ void draw_spinny(Renderer* r, Colour c) { void draw_gun(Renderer* r) { static int a = 0; - const Mesh* m = get_mesh(asset_id_gun_mesh); + const Mesh* m = get_mesh(asset_id_monkey); const Texture* tex = get_texture(asset_id_gun_texture); - int p[] = { 0, 0, 0, f1 }; + int p[] = { f1 * 7, 0, f1 * 5, f1 }; mtx_push_trans(p); /*mtx_push_rot_x(a); mtx_push_rot_y(a); @@ -44,11 +45,39 @@ void draw_tri(Renderer* r, int x, int y) { } void draw_line(Renderer* r, int x, int y) { - int s[] = { 100, 100 }; - int e[] = { x, y }; + int s[] = { 100, 100 }, e[2]; + e[0] = x; + e[1] = y; ren_line(r, make_red(), s, e); } +void draw_map(Renderer* r, Player* p) { + int data[512] = { + 16, 16, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, + 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + }; + int pos[2]; + Map* m = (Map*)data; + pos[0] = p->p[0]; + pos[1] = p->p[2]; + ren_map(r, m, pos, p->f, p->l); +} + int entrypoint(int argc, const char** argv, Arena* a) { App* app; Renderer r = { 0 }; @@ -89,24 +118,23 @@ int entrypoint(int argc, const char** argv, Arena* a) { while (app->o) { fps_begin(&f); while (f.now >= f.next) { - Rect clip = { 30, 80, 400, 800 }; app_begin(app); update_player(&p, &s); ren_begin(&r, app->fb, depth, app->w, app->h); - ren_clip(&r, &clip); - ren_clearc(&r, make_blue()); + ren_clear(&r); ren_cleard(&r, f1 * 300); +/* draw_line(&r, app->mx, app->my);*/ + push_player_cam(&p); + draw_gun(&r); + draw_map(&r, &p); + pop_player_cam(); + /*draw_tri(&r, app->mx, app->my);*/ sprintf(buf, "FPS: %d", app->fps); ren_texts(&r, blue, 3, 3, buf); sprintf(buf, "CAP: %d", f.fps); ren_texts(&r, blue, 3, 8, buf); sprintf(buf, "MOUSE: %d, %d", app->mx, app->my); ren_texts(&r, blue, 3, 13, buf); - draw_line(&r, app->mx, app->my); - push_player_cam(&p); - draw_gun(&r); - pop_player_cam(); - draw_tri(&r, app->mx, app->my); ren_end(&r); app_end(app); fps_update(&f); diff --git a/map.h b/map.h new file mode 100644 index 0000000..6b99d9a --- /dev/null +++ b/map.h @@ -0,0 +1,8 @@ +#ifndef map_h +#define map_h + +typedef struct Map { + int w, h; +} Map; + +#endif diff --git a/player.c b/player.c index 0110b56..a94b36b 100644 --- a/player.c +++ b/player.c @@ -4,7 +4,7 @@ #include "services.h" void init_player(Player* p) { - int ip[] = { 0, 0, -(5 << fbits) }; + int ip[] = { 5 << fbits, 0, (5 << fbits) }; vec_cpy(p->p, ip, 3); p->r[0] = 0; p->r[1] = 0; @@ -26,8 +26,12 @@ void update_player(Player* p, struct Services* s) { dir[1] = cos_table[t]; dir[2] = -sin_table[t2]; dir[3] = cos_table[t2]; - p->p[0] += (dir[0] * fb + dir[2] * lr) / 3; - p->p[2] += (dir[1] * fb + dir[3] * lr) / 3; + p->p[0] += (dir[0] * fb + dir[2] * lr) / 10; + p->p[2] += (dir[1] * fb + dir[3] * lr) / 10; + p->f[0] = dir[0]; + p->f[1] = dir[1]; + p->l[0] = dir[2]; + p->l[1] = dir[3]; } void push_player_cam(const Player* p) { diff --git a/player.h b/player.h index 5c26b9d..c30d9e7 100644 --- a/player.h +++ b/player.h @@ -6,6 +6,7 @@ struct Services; typedef struct { int p[3]; int r[2]; + int f[2], l[2]; } Player; void init_player(Player* p); 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); + } + } +} + + diff --git a/render.h b/render.h index 2299a30..9ce3015 100644 --- a/render.h +++ b/render.h @@ -3,6 +3,8 @@ #include "rect.h" +struct Map; + typedef struct { unsigned char r, g, b, a; } Colour; @@ -125,4 +127,12 @@ void ren_mesh( const Texture* tex ); +void ren_map( + Renderer* r, + const struct Map* map, + const int* pos, + const int* dir, + const int* left +); + #endif -- cgit v1.2.3-54-g00ecf