- added glutIgnoreKeyRepeat in miniglut
authorJohn Tsiombikas <nuclear@member.fsf.org>
Mon, 27 Sep 2021 20:56:07 +0000 (23:56 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Mon, 27 Sep 2021 20:56:07 +0000 (23:56 +0300)
- cleaned up movement mechanics, keyboard-based for now

src/game.c
src/game.h
src/main.c
src/miniglut.c
src/miniglut.h
src/player.c
src/player.h

index 2007694..8dc8d70 100644 (file)
@@ -23,6 +23,8 @@ float view_matrix[16], proj_matrix[16];
 
 unsigned int sdr_foo;
 
+static long prev_step, prev_turn;
+
 int game_init(void)
 {
        if(init_opengl() == -1) {
@@ -59,48 +61,41 @@ void game_shutdown(void)
        free_program(sdr_foo);
 }
 
-#define STEP_INTERVAL  128
+#define STEP_INTERVAL  250
+#define TURN_INTERVAL  500
 
 void update(float dt)
 {
-       static long prev_step;
-       int dir;
-       int step[][2] = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
+       int fwd = 0, right = 0, turn = 0;
 
+       /*
        cgm_vec3 vdir = {0, 0, -1};
 
        cgm_vmul_m3v3(&vdir, player.view_xform);
 
        player.dir = (int)(2.0f * (-atan2(vdir.z, vdir.x) + M_PI) / M_PI + 0.5f) & 3;
+       */
 
-       if(time_msec - prev_step >= STEP_INTERVAL) {
-               if(input_state[INP_FWD]) {
-                       player.cx += step[player.dir][0];
-                       player.cy += step[player.dir][1];
-                       prev_step = time_msec;
-                       printf("step[%d] %d,%d\n", player.dir, player.cx, player.cy);
-               }
-               if(input_state[INP_BACK]) {
-                       player.cx -= step[player.dir][0];
-                       player.cy -= step[player.dir][1];
-                       prev_step = time_msec;
-                       printf("step[%d] %d,%d\n", player.dir, player.cx, player.cy);
-               }
-               if(input_state[INP_LEFT]) {
-                       dir = (player.dir + 3) & 3;
-                       player.cx += step[dir][0];
-                       player.cy += step[dir][1];
-                       prev_step = time_msec;
-                       printf("step[%d] %d,%d\n", player.dir, player.cx, player.cy);
+       if(time_msec - prev_turn >= TURN_INTERVAL) {
+               if(input_state[INP_LTURN]) turn--;
+               if(input_state[INP_RTURN]) turn++;
+
+               if(turn) {
+                       turn_player(&player, turn);
+                       prev_turn = time_msec;
                }
-               if(input_state[INP_RIGHT]) {
-                       dir = (player.dir + 1) & 3;
-                       player.cx += step[dir][0];
-                       player.cy += step[dir][1];
+       }
+
+       if(time_msec - prev_step >= STEP_INTERVAL) {
+               if(input_state[INP_FWD]) fwd++;
+               if(input_state[INP_BACK]) fwd--;
+               if(input_state[INP_LEFT]) right--;
+               if(input_state[INP_RIGHT]) right++;
+
+               if(fwd | right) {
+                       move_player(&player, right, fwd);
                        prev_step = time_msec;
-                       printf("step[%d] %d,%d\n", player.dir, player.cx, player.cy);
                }
-               memset(input_state, 0, sizeof input_state);
        }
 
        upd_player_xform(&player);
@@ -120,7 +115,7 @@ void game_display(void)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
        cgm_midentity(proj_matrix);
-       cgm_mperspective(proj_matrix, cgm_deg_to_rad(50), win_aspect, 0.5, 500.0);
+       cgm_mperspective(proj_matrix, cgm_deg_to_rad(80), win_aspect, 0.5, 500.0);
        glMatrixMode(GL_PROJECTION);
        glLoadMatrixf(proj_matrix);
 
@@ -187,21 +182,56 @@ void game_keyboard(int key, int press)
                return;
        }
 
+       /* TODO key remapping */
        switch(key) {
        case 'w':
                input_state[INP_FWD] = press;
+               if(press) {
+                       move_player(&player, 0, 1);
+                       prev_step = time_msec;
+               }
                break;
 
        case 'a':
                input_state[INP_LEFT] = press;
+               if(press) {
+                       move_player(&player, -1, 0);
+                       prev_step = time_msec;
+               }
                break;
 
        case 's':
                input_state[INP_BACK] = press;
+               if(press) {
+                       move_player(&player, 0, -1);
+                       prev_step = time_msec;
+               }
                break;
 
        case 'd':
                input_state[INP_RIGHT] = press;
+               if(press) {
+                       move_player(&player, 1, 0);
+                       prev_step = time_msec;
+               }
+               break;
+
+       case 'q':
+               input_state[INP_LTURN] = press;
+               if(press) {
+                       turn_player(&player, -1);
+                       prev_turn = time_msec;
+                       player.dir = (player.dir + 3) & 3;
+               }
+               break;
+
+       case 'e':
+               input_state[INP_RTURN] = press;
+               if(press) {
+                       turn_player(&player, 1);
+                       prev_turn = time_msec;
+                       player.dir = (player.dir + 1) & 3;
+               }
                break;
        }
 }
index 10d7658..7a2f7f5 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef GAME_H_
 #define GAME_H_
 
+enum { DIR_N, DIR_E, DIR_S, DIR_W };
+
 enum {
        INP_LEFT,
        INP_RIGHT,
index 0965147..e2dd7bf 100644 (file)
@@ -34,6 +34,8 @@ int main(int argc, char **argv)
        glutMotionFunc(game_mmotion);
        glutPassiveMotionFunc(game_mmotion);
 
+       glutIgnoreKeyRepeat(1);
+
        if(game_init() == -1) {
                return 1;
        }
index bda9491..c362882 100644 (file)
@@ -88,6 +88,7 @@ static unsigned int init_mode;
 
 static struct ctx_info ctx_info;
 static int cur_cursor = GLUT_CURSOR_INHERIT;
+static int ignore_key_repeat;
 
 static glut_cb cb_display;
 static glut_cb cb_idle;
@@ -205,6 +206,11 @@ void glutPostRedisplay(void)
        upd_pending = 1;
 }
 
+void glutIgnoreKeyRepeat(int ignore)
+{
+       ignore_key_repeat = ignore;
+}
+
 #define UPD_EVMASK(x) \
        do { \
                if(func) { \
@@ -580,7 +586,22 @@ static void handle_event(XEvent *ev)
                break;
 
        case KeyPress:
+               if(0) {
        case KeyRelease:
+                       if(ignore_key_repeat && XEventsQueued(dpy, QueuedAfterReading)) {
+                               XEvent next;
+                               XPeekEvent(dpy, &next);
+
+                               if(next.type == KeyPress && next.xkey.keycode == ev->xkey.keycode &&
+                                               next.xkey.time == ev->xkey.time) {
+                                       /* this is a key-repeat event, ignore the release and consume
+                                        * the following press
+                                        */
+                                       XNextEvent(dpy, &next);
+                                       break;
+                               }
+                       }
+               }
                modstate = ev->xkey.state & (ShiftMask | ControlMask | Mod1Mask);
                if(!(sym = XLookupKeysym(&ev->xkey, 0))) {
                        break;
@@ -798,6 +819,15 @@ void glutSetCursor(int cidx)
        cur_cursor = cidx;
 }
 
+void glutSetKeyRepeat(int repmode)
+{
+       if(repmode) {
+               XAutoRepeatOn(dpy);
+       } else {
+               XAutoRepeatOff(dpy);
+       }
+}
+
 static XVisualInfo *choose_visual(unsigned int mode)
 {
        XVisualInfo *vi;
@@ -1229,6 +1259,10 @@ void glutSetCursor(int cidx)
        }
 }
 
+void glutSetKeyRepeat(int repmode)
+{
+}
+
 #define WGL_DRAW_TO_WINDOW     0x2001
 #define WGL_SUPPORT_OPENGL     0x2010
 #define WGL_DOUBLE_BUFFER      0x2011
index 666e5fc..a757369 100644 (file)
@@ -116,6 +116,12 @@ enum {
 #define GLUT_ACTIVE_CTRL       4
 #define GLUT_ACTIVE_ALT                8
 
+enum {
+       GLUT_KEY_REPEAT_OFF,
+       GLUT_KEY_REPEAT_ON
+};
+#define GLUT_KEY_REPEAT_DEFAULT GLUT_KEY_REPEAT_ON
+
 typedef void (*glut_cb)(void);
 typedef void (*glut_cb_reshape)(int x, int y);
 typedef void (*glut_cb_state)(int state);
@@ -149,6 +155,9 @@ void glutSetWindowTitle(const char *title);
 void glutSetIconTitle(const char *title);
 void glutSetCursor(int cursor);
 
+void glutIgnoreKeyRepeat(int ignore);
+void glutSetKeyRepeat(int repmode);
+
 void glutIdleFunc(glut_cb func);
 void glutDisplayFunc(glut_cb func);
 void glutReshapeFunc(glut_cb_reshape func);
index 820dfbd..24c5851 100644 (file)
@@ -10,6 +10,21 @@ void init_player(struct player *p)
        p->mp = p->mp_max = 10;
 }
 
+void move_player(struct player *p, int right, int fwd)
+{
+       static const int step[][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
+       int rdir = (p->dir + 1) & 3;
+       p->cx += step[p->dir][0] * fwd + step[rdir][0] * right;
+       p->cy += step[p->dir][1] * fwd + step[rdir][1] * right;
+}
+
+void turn_player(struct player *p, int turn)
+{
+       if(!turn) return;
+       turn = turn > 0 ? 1 : 3;
+       p->dir = (p->dir + turn) & 3;
+}
+
 void upd_player_xform(struct player *p)
 {
        cgm_vec3 pos;
@@ -20,7 +35,14 @@ void upd_player_xform(struct player *p)
 
        cgm_midentity(p->view_xform);
        cgm_mprerotate_x(p->view_xform, -p->phi);
-       cgm_mprerotate_y(p->view_xform, -p->theta);
+       cgm_mprerotate_y(p->view_xform, p->dir * M_PI / 2.0f);
        cgm_mrotate_quat(p->view_xform, &p->vrot);
        cgm_mpretranslate(p->view_xform, -pos.x, -pos.y, -pos.z);
 }
+
+void upd_player_vis(struct player *p)
+{
+       p->vis = 0;
+
+
+}
index f7451c9..ddba877 100644 (file)
@@ -6,6 +6,7 @@
 
 struct player {
        struct level *lvl;
+       struct cell *vis;       /* cell visibility list */
 
        int cx, cy;
        cgm_vec3 cpos;          /* cell position (derived from cx,cy) */
@@ -13,7 +14,7 @@ struct player {
        cgm_vec3 vpos;          /* VR position within the cell */
        cgm_quat vrot;          /* VR orientation */
 
-       int dir;                        /* cardinal direction, clockwise, 0 is west */
+       int dir;                        /* cardinal direction, clockwise, 0 is north */
        float height;
 
        /* view matrix, derived from all of the above by upd_player_xform */
@@ -23,6 +24,12 @@ struct player {
 };
 
 void init_player(struct player *p);
+
+void move_player(struct player *p, int right, int fwd);
+void turn_player(struct player *p, int turn);
+
 void upd_player_xform(struct player *p);
 
+void upd_level_vis(struct player *p);
+
 #endif /* PLAYER_H_ */