aboutsummaryrefslogtreecommitdiff
path: root/sim.c
diff options
context:
space:
mode:
authorKrow Savcik <krow@savcik.xyz>2023-12-25 12:20:59 +0200
committerKrow Savcik <krow@savcik.xyz>2023-12-25 12:20:59 +0200
commitfb26949fe3c1b6145f1c77af2198d999a856cd37 (patch)
tree4c73c35d0fa4d6083373a0369c47bd5a3be0d885 /sim.c
initial commit
Diffstat (limited to 'sim.c')
-rw-r--r--sim.c199
1 files changed, 199 insertions, 0 deletions
diff --git a/sim.c b/sim.c
new file mode 100644
index 0000000..638571d
--- /dev/null
+++ b/sim.c
@@ -0,0 +1,199 @@
+/* #include <stdio.h> */
+#include <math.h>
+#include <SDL2/SDL.h>
+
+#include "sim.h"
+#include "draw.h"
+#include "types.h"
+
+#define MOUSE_LOCK 1
+
+float hfov, vfov;
+float PI;
+Camera cam;
+uint fps = 30;
+
+SDL_Window *win;
+void *ren;
+
+typedef struct{
+ SDL_Keymod mod;
+ SDL_Keycode sym;
+ void (*func)(const Arg *);
+ const Arg arg;
+} Key;
+
+int
+main_init()
+{
+ if (SDL_Init(SDL_INIT_VIDEO) < 0) {
+ fprintf(stderr, "%s:%d:main_init() error %s", __FILE__, __LINE__, SDL_GetError());
+ return 1;
+ }
+
+ win = SDL_CreateWindow("3D Sim",
+ SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
+ 2*w, 2*h, SDL_WINDOW_RESIZABLE);
+ if (win == NULL) {
+ fprintf(stderr, "%s:%d:main_init() error %s", __FILE__, __LINE__, SDL_GetError());
+ return 1;
+ }
+
+ ren = SDL_CreateRenderer(win, -1, 0);
+ if (ren == NULL) {
+ fprintf(stderr, "%s:%d:main_init() error %s", __FILE__, __LINE__, SDL_GetError());
+ return 1;
+ }
+ SDL_RenderSetLogicalSize(ren, w, h);
+ /* SDL_RenderSetScale(ren, 2, 2); */
+
+ if (draw_init())
+ return 1;
+
+ return 0;
+}
+
+int
+main_quit()
+{
+ SDL_DestroyWindow(win);
+ SDL_Quit();
+ return 1;
+}
+
+int
+main_event()
+{
+ static SDL_Event event;
+ static uint inp = MOUSE_LOCK * 1024;
+ static double s = 0.5;
+ uint8 q;
+
+ while(SDL_PollEvent(&event)) {
+ switch (event.type) {
+ case SDL_QUIT:
+ q = 1;
+ break;
+ case SDL_KEYDOWN:
+ if (event.key.keysym.sym == SDLK_q)
+ q = 1;
+ switch (event.key.keysym.sym) {
+ case SDLK_w: inp |= 1; break;
+ case SDLK_s: inp |= 2; break;
+ case SDLK_a: inp |= 4; break;
+ case SDLK_d: inp |= 8; break;
+ case SDLK_RIGHT: inp |= 16; break;
+ case SDLK_LEFT: inp |= 32; break;
+ case SDLK_UP: inp |= 64; break;
+ case SDLK_DOWN: inp |= 128; break;
+ case SDLK_j: inp |= 256; break;
+ case SDLK_k: inp |= 512; break;
+ case SDLK_m: inp ^= 1024; break;
+ case SDLK_i: add_obj_interactive(); break;
+ case SDLK_r: redo_obj(); break;
+ case SDLK_EQUALS: s += 0.1; break;
+ case SDLK_MINUS: s -= 0.1; break;
+ }
+ break;
+ case SDL_KEYUP:
+ switch (event.key.keysym.sym) {
+ case SDLK_w: inp &= (inp^1); break;
+ case SDLK_s: inp &= (inp^2); break;
+ case SDLK_a: inp &= (inp^4); break;
+ case SDLK_d: inp &= (inp^8); break;
+ case SDLK_RIGHT: inp &= (inp^16); break;
+ case SDLK_LEFT: inp &= (inp^32); break;
+ case SDLK_UP: inp &= (inp^64); break;
+ case SDLK_DOWN: inp &= (inp^128); break;
+ case SDLK_j: inp &= (inp^256); break;
+ case SDLK_k: inp &= (inp^512); break;
+ }
+ break;
+ case SDL_MOUSEMOTION:
+ if (~inp & 1024) break;
+ cam.azim -= ((float)event.motion.yrel)/25.0;
+ cam.rad += ((float)event.motion.xrel)/25.0;
+ break;
+ }
+ }
+
+ if (inp&1024)
+ SDL_SetRelativeMouseMode(SDL_TRUE);
+ else
+ SDL_SetRelativeMouseMode(SDL_FALSE);
+
+ if (inp&1) {
+ cam.pos.x += cos(cam.rad) * s;
+ cam.pos.z += sin(cam.rad) * s;
+ }
+ if (inp&2) {
+ cam.pos.x -= cos(cam.rad) * s;
+ cam.pos.z -= sin(cam.rad) * s;
+ }
+ if (inp&4) {
+ cam.pos.x += sin(cam.rad) * s;
+ cam.pos.z -= cos(cam.rad) * s;
+ }
+ if (inp&8) {
+ cam.pos.x -= sin(cam.rad) * s;
+ cam.pos.z += cos(cam.rad) * s;
+ }
+
+ if (inp&256) cam.pos.y -= s;
+ if (inp&512) cam.pos.y += s;
+ if (inp&16)
+ cam.rad += 0.05;
+ if (inp&32)
+ cam.rad -= 0.05;
+
+ if (inp&64)
+ cam.azim += 0.05;
+ if (inp&128)
+ cam.azim -= 0.05;
+
+ if (cam.azim < 0)
+ cam.azim += 2.0 * PI;
+ if (cam.azim > 2.0 * PI)
+ cam.azim -= 2.0 * PI;
+
+ if (cam.rad < 0)
+ cam.rad += 2.0 * PI;
+ if (cam.rad > 2.0 * PI)
+ cam.rad -= 2.0 * PI;
+
+ if (q)
+ return main_quit();
+
+ if (draw_screen())
+ return 1;
+
+ return 0;
+}
+
+int main()
+{
+ Uint32 ltick, fdif;
+ cam.pos.x = cam.pos.z = 0;
+ cam.pos.y = 10;
+ cam.rad = cam.azim = 0;
+ PI = 4.0 * atan(1);
+ h = 288;
+ w = 384;
+ hfov = 1;
+
+ if (main_init())
+ return 1;
+
+ vfov = 0.75;
+
+ fdif = 1000 / fps;
+ while (1) {
+ ltick = SDL_GetTicks();
+ if (main_event())
+ break;
+ ltick = SDL_GetTicks() - ltick;
+
+ if (ltick < fdif)
+ SDL_Delay(fdif - ltick);
+ }
+}