mouselook
authorJohn Tsiombikas <nuclear@member.fsf.org>
Sun, 16 Oct 2022 16:22:04 +0000 (19:22 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Sun, 16 Oct 2022 16:22:04 +0000 (19:22 +0300)
src/main.c

index f690a0e..e05118b 100644 (file)
@@ -24,6 +24,8 @@ void idle(void);
 void reshape(int x, int y);
 void keyb(unsigned char key, int x, int y);
 void keyb_up(unsigned char key, int x, int y);
+void mouse(int bn, int st, int x, int y);
+void motion(int x, int y);
 
 int win_width, win_height;
 
@@ -31,8 +33,10 @@ int win_width, win_height;
 #define FB_H   480
 unsigned int fb[FB_W * FB_H];
 
+int mouse_x, mouse_y, mwarp, mbstate[3];
+
 unsigned int input;
-int32_t pos[2], angle;// = 0x4000;
+int32_t pos[2], angle;
 
 struct voxscape *vox;
 
@@ -45,10 +49,12 @@ int main(int argc, char **argv)
        glutCreateWindow("voxel landscape test");
 
        glutDisplayFunc(display);
+       glutIdleFunc(idle);
        glutReshapeFunc(reshape);
        glutKeyboardFunc(keyb);
        glutKeyboardUpFunc(keyb_up);
-       glutIdleFunc(idle);
+       glutMouseFunc(mouse);
+       glutMotionFunc(motion);
 
        if(init() == -1) {
                return 1;
@@ -88,14 +94,33 @@ void cleanup(void)
 
 void update(void)
 {
-       if(input & INP_FWD) pos[1] += WALK_SPEED;
-       if(input & INP_BACK) pos[1] -= WALK_SPEED;
-       if(input & INP_LEFT) pos[0] -= WALK_SPEED;
-       if(input & INP_RIGHT) pos[0] += WALK_SPEED;
+       int32_t fwd[2], right[2];
 
        if(input & INP_LTURN) angle += TURN_SPEED;
        if(input & INP_RTURN) angle -= TURN_SPEED;
 
+       fwd[0] = -SIN(angle);
+       fwd[1] = COS(angle);
+       right[0] = fwd[1];
+       right[1] = -fwd[0];
+
+       if(input & INP_FWD) {
+               pos[0] += fwd[0];
+               pos[1] += fwd[1];
+       }
+       if(input & INP_BACK) {
+               pos[0] -= fwd[0];
+               pos[1] -= fwd[1];
+       }
+       if(input & INP_RIGHT) {
+               pos[0] += right[0];
+               pos[1] += right[1];
+       }
+       if(input & INP_LEFT) {
+               pos[0] -= right[0];
+               pos[1] -= right[1];
+       }
+
        vox_view(vox, pos[0], pos[1], angle);
 }
 
@@ -113,6 +138,11 @@ void display(void)
 
        glutSwapBuffers();
        assert(glGetError() == GL_NO_ERROR);
+
+       if(mbstate[0]) {
+               mwarp = 1;
+               glutWarpPointer(win_width / 2, win_height / 2);
+       }
 }
 
 void idle(void)
@@ -184,3 +214,38 @@ void keyb_up(unsigned char key, int x, int y)
                break;
        }
 }
+
+void mouse(int bn, int st, int x, int y)
+{
+       int bidx = bn - GLUT_LEFT_BUTTON;
+
+       if(bidx < 3) {
+               mbstate[bidx] = st == GLUT_DOWN;
+       }
+       mouse_x = x;
+       mouse_y = y;
+
+       if(st == GLUT_DOWN) {
+               glutSetCursor(GLUT_CURSOR_NONE);
+       } else {
+               glutSetCursor(GLUT_CURSOR_LEFT_ARROW);
+       }
+}
+
+void motion(int x, int y)
+{
+       int dx = x - mouse_x;
+       int dy = y - mouse_y;
+       mouse_x = x;
+       mouse_y = y;
+
+       if(mwarp) {
+               mwarp = 0;
+               return;
+       }
+       if(!(dx | dy)) return;
+
+       if(mbstate[0]) {
+               angle -= dx << 6;
+       }
+}