From c67b409dd32e4aa795e5aa9868202c910fc9ffd0 Mon Sep 17 00:00:00 2001 From: quou Date: Wed, 17 Jul 2024 21:29:29 +1000 Subject: simple mouse look --- 3de.c | 20 +++++++++++++++----- Makefile | 1 + player.c | 33 +++++++++++++++++++++++++++++++++ player.h | 16 ++++++++++++++++ services.h | 9 +++++++++ 5 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 player.c create mode 100644 player.h create mode 100644 services.h diff --git a/3de.c b/3de.c index 23e4d13..6197321 100644 --- a/3de.c +++ b/3de.c @@ -3,7 +3,9 @@ #include "maths.h" #include "memory.h" #include "plat.h" +#include "player.h" #include "render.h" +#include "services.h" #include "tile.h" #include @@ -21,13 +23,13 @@ void draw_monkey(Renderer* r) { static int a = 0; const Mesh* m = get_mesh(asset_id_monkey); Bitmap tex = { (Colour*)tile_pixels, 16, 16 }; - int p[] = { 0, 0, 5 << fbits, f1 }; + int p[] = { 0, 0, 0, f1 }; mtx_push_trans(p); - mtx_push_rot_x(a); + /*mtx_push_rot_x(a); mtx_push_rot_y(a); - mtx_push_rot_z(a); + mtx_push_rot_z(a);*/ ren_mesh(r, m, &tex); - mtx_popn(4); + mtx_popn(1); a++; } @@ -50,6 +52,8 @@ int entrypoint(int argc, const char** argv, Arena* a) { int* depth = 0; Arena preload; Colour blue = make_aliceblue(); + Player p; + Services s; (void)argc; (void)argv; init_maths(); @@ -72,11 +76,15 @@ int entrypoint(int argc, const char** argv, Arena* a) { return error_out_of_memory; } preload_assets(&preload); + s.a = app; + s.r = &r; cfg_mouse(app, 0); + init_player(&p); while (app->o) { fps_begin(&f); while (f.now >= f.next) { app_begin(app); + update_player(&p, &s); ren_begin(&r, app->fb, depth, app->w, app->h); ren_clear(&r); ren_cleard(&r, f1 * 300); @@ -86,8 +94,10 @@ int entrypoint(int argc, const char** argv, Arena* a) { ren_texts(&r, blue, 3, 8, buf); sprintf(buf, "MOUSE: %d, %d", app->mx, app->my); ren_texts(&r, blue, 3, 13, buf); + push_player_cam(&p); draw_monkey(&r); - draw_tri(&r, app->mx, app->my); + pop_player_cam(); + /*draw_tri(&r, app->mx, app->my);*/ ren_end(&r); app_end(app); fps_update(&f); diff --git a/Makefile b/Makefile index d183d38..33c0c01 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,7 @@ sources = \ maths.c \ memory.c \ plat.c \ + player.c \ rect.c \ render.c \ standard.c diff --git a/player.c b/player.c new file mode 100644 index 0000000..2ba3885 --- /dev/null +++ b/player.c @@ -0,0 +1,33 @@ +#include "maths.h" +#include "plat.h" +#include "player.h" +#include "services.h" + +void init_player(Player* p) { + int ip[] = { 0, 0, -(5 << fbits) }; + vec_cpy(p->p, ip, 3); + p->r[0] = 0; + p->r[1] = 0; +} + +void update_player(Player* p, struct Services* s) { + App* a = s->a; +/* p->r[0] -= a->dmy;*/ + p->r[1] -= a->dmx; +/* p->p[0] -= sin_table[p->r[1] & sin_table_mask] / 3; + p->p[2] += cos_table[p->r[1] & sin_table_mask] / 3;*/ +} + +void push_player_cam(const Player* p) { + int v[3]; + v[0] = -p->p[0]; + v[1] = -p->p[1]; + v[2] = -p->p[2]; + mtx_push_rot_y(p->r[1] / 10); + mtx_push_rot_x(p->r[0] / 10); + mtx_push_trans(v); +} + +void pop_player_cam(void) { + mtx_popn(3); +} diff --git a/player.h b/player.h new file mode 100644 index 0000000..5c26b9d --- /dev/null +++ b/player.h @@ -0,0 +1,16 @@ +#ifndef player_h +#define player_h + +struct Services; + +typedef struct { + int p[3]; + int r[2]; +} Player; + +void init_player(Player* p); +void update_player(Player* p, struct Services* s); +void push_player_cam(const Player* p); +void pop_player_cam(void); + +#endif diff --git a/services.h b/services.h new file mode 100644 index 0000000..86adee2 --- /dev/null +++ b/services.h @@ -0,0 +1,9 @@ +#ifndef services_h +#define services_h + +typedef struct Services { + App* a; + Renderer* r; +} Services; + +#endif -- cgit v1.2.3-54-g00ecf