ok now it works nicely in VR
[vrtris] / src / gamescr.c
index 07193fa..c31700d 100644 (file)
@@ -2,7 +2,10 @@
 #include <time.h>
 #include <assert.h>
 #include <imago2.h>
+#ifdef BUILD_VR
 #include <goatvr.h>
+#endif
+#include <drawtext.h>
 #include "opengl.h"
 #include "game.h"
 #include "screen.h"
@@ -12,6 +15,8 @@
 #include "gameinp.h"
 #include "color.h"
 
+#define FONTSZ 75
+
 int init_starfield(void);
 void draw_starfield(void);
 
@@ -56,8 +61,11 @@ struct game_screen game_screen = {
 static struct cmesh *blkmesh, *wellmesh;
 static unsigned int tex_well;
 
-static float cam_theta, cam_phi, cam_dist = 30;
-static int bnstate[16];
+static struct dtx_font *scorefont;
+
+static float cam_theta, cam_phi, cam_dist;
+static float cam_height;
+static unsigned int bnstate;
 static int prev_mx, prev_my;
 
 static long tick_interval;
@@ -82,6 +90,11 @@ static int pause;
 static int score, level, lines;
 static int just_spawned;
 
+#ifdef BUILD_VR
+static int vrbn_a = 0, vrbn_x = 4;
+static float vrscale = 40.0f;
+#endif
+
 #define NUM_LEVELS     21
 static const long level_speed[NUM_LEVELS] = {
        887, 820, 753, 686, 619, 552, 469, 368, 285, 184,
@@ -104,6 +117,13 @@ static const float blkcolor[][4] = {
 
 static int init(void)
 {
+       if(!(scorefont = dtx_open_font("data/score.font", 0))) {
+               error_log("failed to open score font\n");
+               return -1;
+       }
+       dtx_prepare_range(scorefont, FONTSZ, 32, 127);
+       dtx_save_glyphmap("foo.ppm", dtx_get_glyphmap(scorefont, 0));
+
        if(init_starfield() == -1) {
                return -1;
        }
@@ -131,12 +151,15 @@ static void cleanup(void)
        cmesh_free(blkmesh);
        cmesh_free(wellmesh);
        glDeleteTextures(1, &tex_well);
+       dtx_close_font(scorefont);
 }
 
 static void start(void)
 {
        srand(time(0));
 
+       glClearColor(0.12, 0.12, 0.18, 1);
+
        pause = 0;
        gameover = 0;
        num_complines = 0;
@@ -149,10 +172,37 @@ static void start(void)
        memset(pfield, 0, PF_COLS * PF_ROWS * sizeof *pfield);
 
        ginp_repeat(500, 75, GINP_LEFT | GINP_RIGHT | GINP_DOWN);
+
+       dtx_use_font(scorefont, FONTSZ);
+
+       cam_theta = 0;
+       cam_phi = 0;
+       cam_dist = 30;
+       cam_height = 0;
+
+#ifdef BUILD_VR
+       if(goatvr_invr()) {
+               int bn = goatvr_lookup_button("A");
+               if(bn >= 0) vrbn_a = bn;
+
+               bn = goatvr_lookup_button("X");
+               if(bn >= 0) vrbn_x = bn;
+
+               /* switch to VR-optimized camera parameters */
+               cam_theta = 0;
+               cam_phi = -2.5;
+               cam_dist = 20;
+               cam_height = 3.5;
+               vrscale = 40.0f;
+
+               goatvr_set_units_scale(vrscale);
+       }
+#endif
 }
 
 static void stop(void)
 {
+       goatvr_set_units_scale(1.0f);
 }
 
 #define JTHRES 0.6
@@ -164,20 +214,33 @@ static void stop(void)
 
 static void update_input(float dtsec)
 {
+#ifdef BUILD_VR
        int num_vr_sticks;
 
-       if((num_vr_sticks = goatvr_num_sticks()) > 0) {
+       if(goatvr_invr() && (num_vr_sticks = goatvr_num_sticks()) > 0) {
                float p[2];
 
                goatvr_stick_pos(0, p);
+               p[1] *= 0.65;   /* drops harder to trigger accidentally */
 
                if(fabs(p[0]) > fabs(joy_axis[GPAD_LSTICK_X])) {
                        joy_axis[GPAD_LSTICK_X] = p[0];
                }
                if(fabs(p[1]) > fabs(joy_axis[GPAD_LSTICK_Y])) {
-                       joy_axis[GPAD_LSTICK_Y] = p[1];
+                       joy_axis[GPAD_LSTICK_Y] = -p[1];
+               }
+
+               if(goatvr_button_state(vrbn_a)) {
+                       joy_bnstate |= 1 << GPAD_A;
+               }
+               if(goatvr_button_state(vrbn_x)) {
+                       joy_bnstate |= 1 << GPAD_START;
+               }
+               if(goatvr_action(0, GOATVR_ACTION_TRIGGER) || goatvr_action(1, GOATVR_ACTION_TRIGGER)) {
+                       joy_bnstate |= 1 << GPAD_UP;
                }
        }
+#endif /* BUILD_VR */
 
        ginp_bnstate = 0;
 
@@ -221,6 +284,11 @@ static void update_input(float dtsec)
        if(GINP_PRESS(GINP_PAUSE)) {
                game_keyboard('p', 1);
        }
+
+#ifdef BUILD_VR
+       memset(joy_axis, 0, sizeof joy_axis);
+       joy_bnstate = 0;
+#endif
 }
 
 static void update(float dtsec)
@@ -310,15 +378,17 @@ static void draw(void)
        glTranslatef(0, 0, -cam_dist);
        glRotatef(cam_phi, 1, 0, 0);
        glRotatef(cam_theta, 0, 1, 0);
+       glTranslatef(0, -cam_height, 0);
 
        glLightfv(GL_LIGHT0, GL_POSITION, lpos);
 
        draw_starfield();
 
        glPushAttrib(GL_ENABLE_BIT);
+       glEnable(GL_COLOR_MATERIAL);
        glBindTexture(GL_TEXTURE_2D, tex_well);
        glEnable(GL_TEXTURE_2D);
-       glDisable(GL_LIGHTING);
+       //glDisable(GL_LIGHTING);
        glColor3f(1, 1, 1);
        cmesh_draw(wellmesh);
        glPopAttrib();
@@ -346,7 +416,40 @@ static void draw(void)
        glTranslatef(-1.5, 1, 0);
        draw_block(next_block, nextblk_pos, 0, 0.25f, 0.75f);
        glPopMatrix();
+       glPopAttrib();
 
+       glPushAttrib(GL_ENABLE_BIT);
+       glDisable(GL_LIGHTING);
+
+       glPushMatrix();
+       glTranslatef(-11, 6, 0);
+       glScalef(0.05, 0.05, 0.05);
+
+       glColor3f(1, 1, 1);
+       dtx_string("Score");
+       glTranslatef(0, -dtx_line_height() * 1.5, 0);
+       glPushMatrix();
+       glScalef(1.5, 1.5, 1.5);
+       dtx_printf("%d", score);
+       glPopMatrix();
+
+       glTranslatef(0, -dtx_line_height() * 2, 0);
+       dtx_string("Level");
+       glTranslatef(0, -dtx_line_height() * 1.5, 0);
+       glPushMatrix();
+       glScalef(1.5, 1.5, 1.5);
+       dtx_printf("%d", level);
+       glPopMatrix();
+
+       glTranslatef(0, -dtx_line_height() * 2, 0);
+       dtx_string("Lines");
+       glTranslatef(0, -dtx_line_height() * 1.5, 0);
+       glPushMatrix();
+       glScalef(1.5, 1.5, 1.5);
+       dtx_printf("%d", lines);
+       glPopMatrix();
+
+       glPopMatrix();
        glPopAttrib();
 }
 
@@ -512,7 +615,11 @@ static void keyboard(int key, int pressed)
 
 static void mouse(int bn, int pressed, int x, int y)
 {
-       bnstate[bn] = pressed;
+       if(pressed) {
+               bnstate |= 1 << bn;
+       } else {
+               bnstate &= ~(1 << bn);
+       }
        prev_mx = x;
        prev_my = y;
 }
@@ -524,21 +631,39 @@ static void motion(int x, int y)
        prev_mx = x;
        prev_my = y;
 
-       if(bnstate[0]) {
+       if(bnstate & 1) {
                cam_theta += dx * 0.5;
                cam_phi += dy * 0.5;
 
                if(cam_phi < -90) cam_phi = -90;
                if(cam_phi > 90) cam_phi = 90;
        }
-       if(bnstate[2]) {
+       if(bnstate & 2) {
+               cam_height += dy * 0.1;
+       }
+       if(bnstate & 4) {
                cam_dist += dy * 0.1;
                if(cam_dist < 0) cam_dist = 0;
        }
+
+#ifdef DBG_PRINT_VIEW
+       if(bnstate) {
+               debug_log("Camera params\n");
+               debug_log("  theta: %g  phi: %g  dist: %g  height: %g\n", cam_theta,
+                               cam_phi, cam_dist, cam_height);
+       }
+#endif
 }
 
 static void wheel(int dir)
 {
+       /* debug code, used to figure out the best scales */
+       /*
+       vrscale += dir * 0.01;
+       if(vrscale < 0.01) vrscale = 0.01;
+       goatvr_set_units_scale(vrscale);
+       debug_log("VR scale: %g\n", vrscale);
+       */
 }
 
 static void update_cur_block(void)