build-time config option for VR mode
[vrtris] / src / gamescr.c
index 4fd0b2b..9d15659 100644 (file)
@@ -2,12 +2,20 @@
 #include <time.h>
 #include <assert.h>
 #include <imago2.h>
+#ifdef BUILD_VR
+#include <goatvr.h>
+#endif
 #include "opengl.h"
 #include "game.h"
 #include "screen.h"
 #include "cmesh.h"
 #include "blocks.h"
 #include "logger.h"
+#include "gameinp.h"
+#include "color.h"
+
+int init_starfield(void);
+void draw_starfield(void);
 
 static int init(void);
 static void cleanup(void);
@@ -15,7 +23,7 @@ static void start(void);
 static void stop(void);
 static void update(float dt);
 static void draw(void);
-static void draw_block(int block, const int *pos, int rot);
+static void draw_block(int block, const int *pos, int rot, float sat, float alpha);
 static void drawpf(void);
 static void reshape(int x, int y);
 static void keyboard(int key, int pressed);
@@ -82,9 +90,26 @@ static const long level_speed[NUM_LEVELS] = {
        167, 151, 134, 117, 107, 98, 88, 79, 69, 60, 50
 };
 
+static const float blkcolor[][4] = {
+       {1.0, 0.65, 0.0, 1},
+       {0.16, 1.0, 0.4, 1},
+       {0.65, 0.65, 1.0, 1},
+       {1.0, 0.9, 0.1, 1},
+       {0.0, 1.0, 1.0, 1},
+       {1.0, 0.5, 1.0, 1},
+       {1.0, 0.35, 0.2, 1},
+       {0.5, 0.5, 0.5, 1}
+};
+
+#define GAMEOVER_FILL_RATE     50
+
 
 static int init(void)
 {
+       if(init_starfield() == -1) {
+               return -1;
+       }
+
        if(!(blkmesh = cmesh_alloc()) || cmesh_load(blkmesh, "data/noisecube.obj") == -1) {
                error_log("failed to load block mesh\n");
                return -1;
@@ -106,6 +131,8 @@ static int init(void)
 static void cleanup(void)
 {
        cmesh_free(blkmesh);
+       cmesh_free(wellmesh);
+       glDeleteTextures(1, &tex_well);
 }
 
 static void start(void)
@@ -122,27 +149,100 @@ static void start(void)
        next_block = rand() % NUM_BLOCKS;
 
        memset(pfield, 0, PF_COLS * PF_ROWS * sizeof *pfield);
+
+       ginp_repeat(500, 75, GINP_LEFT | GINP_RIGHT | GINP_DOWN);
 }
 
 static void stop(void)
 {
 }
 
+#define JTHRES 0.6
+
+#define CHECK_BUTTON(idx, gbn) \
+       if(joy_bnstate & (1 << idx)) { \
+               ginp_bnstate |= gbn; \
+       }
+
+static void update_input(float dtsec)
+{
+#ifdef BUILD_VR
+       int num_vr_sticks;
+
+       if((num_vr_sticks = goatvr_num_sticks()) > 0) {
+               float p[2];
+
+               goatvr_stick_pos(0, p);
+
+               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];
+               }
+       }
+#endif /* BUILD_VR */
+
+       ginp_bnstate = 0;
+
+       /* joystick axis */
+       if(joy_axis[GPAD_LSTICK_X] >= JTHRES) {
+               ginp_bnstate |= GINP_RIGHT;
+       } else if(joy_axis[GPAD_LSTICK_X] <= -JTHRES) {
+               ginp_bnstate |= GINP_LEFT;
+       }
+
+       if(joy_axis[GPAD_LSTICK_Y] >= JTHRES) {
+               ginp_bnstate |= GINP_DOWN;
+       } else if(joy_axis[GPAD_LSTICK_Y] <= -JTHRES) {
+               ginp_bnstate |= GINP_UP;
+       }
+
+       CHECK_BUTTON(GPAD_LEFT, GINP_LEFT);
+       CHECK_BUTTON(GPAD_RIGHT, GINP_RIGHT);
+       CHECK_BUTTON(GPAD_UP, GINP_UP);
+       CHECK_BUTTON(GPAD_DOWN, GINP_DOWN);
+       CHECK_BUTTON(GPAD_A, GINP_ROTATE);
+       CHECK_BUTTON(GPAD_START, GINP_PAUSE);
+
+       update_ginp();
+
+       if(GINP_PRESS(GINP_LEFT)) {
+               game_keyboard('a', 1);
+       }
+       if(GINP_PRESS(GINP_RIGHT)) {
+               game_keyboard('d', 1);
+       }
+       if(GINP_PRESS(GINP_DOWN)) {
+               game_keyboard('s', 1);
+       }
+       if(GINP_PRESS(GINP_UP)) {
+               game_keyboard('\t', 1);
+       }
+       if(GINP_PRESS(GINP_ROTATE)) {
+               game_keyboard('w', 1);
+       }
+       if(GINP_PRESS(GINP_PAUSE)) {
+               game_keyboard('p', 1);
+       }
+}
+
 static void update(float dtsec)
 {
        static long prev_tick;
        long dt;
 
+       update_input(dtsec);
+
        if(pause) {
                prev_tick = time_msec;
                return;
        }
        dt = time_msec - prev_tick;
 
-       /*
        if(gameover) {
                int i, row = PF_ROWS - gameover;
-               int *ptr;
+               unsigned int *ptr;
 
                if(dt < GAMEOVER_FILL_RATE) {
                        return;
@@ -151,15 +251,14 @@ static void update(float dtsec)
                if(row >= 0) {
                        ptr = pfield + row * PF_COLS;
                        for(i=0; i<PF_COLS; i++) {
-                               *ptr++ = TILE_GAMEOVER;
+                               *ptr++ = PF_VIS | PF_FULL | 7;
                        }
 
                        gameover++;
-                       prev_tick = msec;
+                       prev_tick = time_msec;
                }
                return;
        }
-       */
 
        if(num_complines) {
                /* lines where completed, we're in blinking mode */
@@ -175,6 +274,7 @@ static void update(float dtsec)
                        unsigned int *ptr = pfield + complines[i] * PF_COLS;
                        for(j=0; j<PF_COLS; j++) {
                                *ptr = (*ptr & ~PF_VIS) | ((blink & 1) << PF_VIS_SHIFT);
+                               ptr++;
                        }
                }
                return;
@@ -207,10 +307,18 @@ static void update(float dtsec)
 
 static void draw(void)
 {
+       static const int nextblk_pos[] = {0, 0};
+       static const float lpos[] = {-1, 1, 6, 1};
+       float t;
+
        glTranslatef(0, 0, -cam_dist);
        glRotatef(cam_phi, 1, 0, 0);
        glRotatef(cam_theta, 0, 1, 0);
 
+       glLightfv(GL_LIGHT0, GL_POSITION, lpos);
+
+       draw_starfield();
+
        glPushAttrib(GL_ENABLE_BIT);
        glBindTexture(GL_TEXTURE_2D, tex_well);
        glEnable(GL_TEXTURE_2D);
@@ -225,18 +333,43 @@ static void draw(void)
 
        drawpf();
        if(cur_block >= 0) {
-               draw_block(cur_block, pos, cur_rot);
+               draw_block(cur_block, pos, cur_rot, 1.0f, 1.0f);
        }
+       glPopMatrix();
+
+       glPushAttrib(GL_ENABLE_BIT);
+       glEnable(GL_BLEND);
+       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
+       glPushMatrix();
+       t = (float)time_msec / 1000.0f;
+       glTranslatef(-PF_COLS / 2 + 0.5 + PF_COLS + 3, PF_ROWS / 2 - 0.5, 0);
+       glTranslatef(1.5, -1, 0);
+       glRotatef(cos(t) * 8.0f, 1, 0, 0);
+       glRotatef(sin(t * 1.2f) * 10.0f, 0, 1, 0);
+       glTranslatef(-1.5, 1, 0);
+       draw_block(next_block, nextblk_pos, 0, 0.25f, 0.75f);
        glPopMatrix();
+
+       glPopAttrib();
 }
 
-static void draw_block(int block, const int *pos, int rot)
+static const float blkspec[] = {0.85, 0.85, 0.85, 1};
+
+static void draw_block(int block, const int *pos, int rot, float sat, float alpha)
 {
-       int i, pal;
+       int i;
        unsigned char *p = blocks[block][rot];
+       float col[4], hsv[3];
 
-       /*pal = FIRST_BLOCK_PAL + block;*/
+       rgb_to_hsv(blkcolor[block][0], blkcolor[block][1], blkcolor[block][2],
+                       hsv, hsv + 1, hsv + 2);
+       hsv_to_rgb(hsv[0], hsv[1] * sat, hsv[2], col, col + 1, col + 2);
+       col[3] = alpha;
+
+       glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
+       glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, blkspec);
+       glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0f);
 
        for(i=0; i<4; i++) {
                int x = pos[1] + BLKX(*p);
@@ -260,7 +393,12 @@ static void drawpf(void)
        for(i=0; i<PF_ROWS; i++) {
                for(j=0; j<PF_COLS; j++) {
                        unsigned int val = *sptr++;
-                       if(val & PF_FULL) {
+
+                       glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blkcolor[val & 7]);
+                       glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, blkspec);
+                       glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0f);
+
+                       if((val & (PF_FULL | PF_VIS)) == (PF_FULL | PF_VIS)) {
                                glPushMatrix();
                                glTranslatef(j, -i, 0);
                                cmesh_draw(blkmesh);
@@ -283,6 +421,7 @@ static void keyboard(int key, int pressed)
 
        switch(key) {
        case 'a':
+       case KEY_LEFT:
                if(!pause) {
                        next_pos[1] = pos[1] - 1;
                        if(collision(cur_block, next_pos)) {
@@ -294,6 +433,7 @@ static void keyboard(int key, int pressed)
                break;
 
        case 'd':
+       case KEY_RIGHT:
                if(!pause) {
                        next_pos[1] = pos[1] + 1;
                        if(collision(cur_block, next_pos)) {
@@ -305,6 +445,8 @@ static void keyboard(int key, int pressed)
                break;
 
        case 'w':
+       case KEY_UP:
+       case ' ':
                if(!pause) {
                        prev_rot = cur_rot;
                        cur_rot = (cur_rot + 1) & 3;
@@ -317,6 +459,7 @@ static void keyboard(int key, int pressed)
                break;
 
        case 's':
+       case KEY_DOWN:
                /* ignore drops until the first update after a spawn */
                if(cur_block >= 0 && !just_spawned && !pause) {
                        next_pos[0] = pos[0] + 1;
@@ -330,6 +473,7 @@ static void keyboard(int key, int pressed)
 
        case '\n':
        case '\t':
+       case '0':
                if(!pause && cur_block >= 0) {
                        next_pos[0] = pos[0] + 1;
                        while(!collision(cur_block, next_pos)) {