unsigned int sdr_foo;
+static long prev_step, prev_turn;
+
int game_init(void)
{
if(init_opengl() == -1) {
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);
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);
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;
}
}
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;
upd_pending = 1;
}
+void glutIgnoreKeyRepeat(int ignore)
+{
+ ignore_key_repeat = ignore;
+}
+
#define UPD_EVMASK(x) \
do { \
if(func) { \
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;
cur_cursor = cidx;
}
+void glutSetKeyRepeat(int repmode)
+{
+ if(repmode) {
+ XAutoRepeatOn(dpy);
+ } else {
+ XAutoRepeatOff(dpy);
+ }
+}
+
static XVisualInfo *choose_visual(unsigned int mode)
{
XVisualInfo *vi;
}
}
+void glutSetKeyRepeat(int repmode)
+{
+}
+
#define WGL_DRAW_TO_WINDOW 0x2001
#define WGL_SUPPORT_OPENGL 0x2010
#define WGL_DOUBLE_BUFFER 0x2011
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;
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;
+
+
+}
struct player {
struct level *lvl;
+ struct cell *vis; /* cell visibility list */
int cx, cy;
cgm_vec3 cpos; /* cell position (derived from cx,cy) */
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 */
};
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_ */