added starfield
[vrtris] / src / gamescr.c
index 469fc12..a55f432 100644 (file)
@@ -1,11 +1,16 @@
 #include <stdlib.h>
 #include <time.h>
 #include <assert.h>
+#include <imago2.h>
 #include "opengl.h"
 #include "game.h"
 #include "screen.h"
 #include "cmesh.h"
 #include "blocks.h"
+#include "logger.h"
+
+int init_starfield(void);
+void draw_starfield(void);
 
 static int init(void);
 static void cleanup(void);
@@ -21,6 +26,7 @@ static void mouse(int bn, int pressed, int x, int y);
 static void motion(int x, int y);
 static void wheel(int dir);
 
+static void update_cur_block(void);
 static void addscore(int nlines);
 static int spawn(void);
 static int collision(int block, const int *pos);
@@ -44,7 +50,9 @@ struct game_screen game_screen = {
        wheel
 };
 
-static struct cmesh *blkmesh;
+static struct cmesh *blkmesh, *wellmesh;
+static unsigned int tex_well;
+
 static float cam_theta, cam_phi, cam_dist = 30;
 static int bnstate[16];
 static int prev_mx, prev_my;
@@ -77,22 +85,49 @@ 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(!(blkmesh = cmesh_alloc())) {
+       if(init_starfield() == -1) {
                return -1;
        }
-       if(cmesh_load(blkmesh, "data/noisecube.obj") == -1) {
-               fprintf(stderr, "failed to load block model\n");
+
+       if(!(blkmesh = cmesh_alloc()) || cmesh_load(blkmesh, "data/noisecube.obj") == -1) {
+               error_log("failed to load block mesh\n");
                return -1;
        }
+
+       if(!(wellmesh = cmesh_alloc()) || cmesh_load(wellmesh, "data/well.obj") == -1) {
+               error_log("failed to load well mesh\n");
+               return -1;
+       }
+
+       if(!(tex_well = img_gltexture_load("data/grid.png"))) {
+               error_log("failed to load well texture\n");
+               return -1;
+       }
+
        return 0;
 }
 
 static void cleanup(void)
 {
        cmesh_free(blkmesh);
+       cmesh_free(wellmesh);
+       glDeleteTextures(1, &tex_well);
 }
 
 static void start(void)
@@ -126,10 +161,9 @@ static void update(float dtsec)
        }
        dt = time_msec - prev_tick;
 
-       /*
        if(gameover) {
                int i, row = PF_ROWS - gameover;
-               int *ptr;
+               unsigned int *ptr;
 
                if(dt < GAMEOVER_FILL_RATE) {
                        return;
@@ -138,15 +172,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 */
@@ -162,6 +195,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;
@@ -188,17 +222,33 @@ static void update(float dtsec)
                dt -= tick_interval;
                prev_tick = time_msec;
        }
+
+       update_cur_block();
 }
 
 static void draw(void)
 {
+       const float lpos[] = {-1, 1, 6, 1};
+
        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);
+       glDisable(GL_LIGHTING);
+       glColor3f(1, 1, 1);
+       cmesh_draw(wellmesh);
+       glPopAttrib();
+
        /* center playfield */
        glPushMatrix();
-       glTranslatef(-PF_COLS / 2, PF_ROWS / 2, 0);
+       glTranslatef(-PF_COLS / 2 + 0.5, PF_ROWS / 2 - 0.5, 0);
 
        drawpf();
        if(cur_block >= 0) {
@@ -208,12 +258,16 @@ static void draw(void)
        glPopMatrix();
 }
 
+static const float blkspec[] = {0.85, 0.85, 0.85, 1};
+
 static void draw_block(int block, const int *pos, int rot)
 {
-       int i, pal;
+       int i;
        unsigned char *p = blocks[block][rot];
 
-       /*pal = FIRST_BLOCK_PAL + block;*/
+       glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blkcolor[block]);
+       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);
@@ -223,7 +277,7 @@ static void draw_block(int block, const int *pos, int rot)
                if(y < 0) continue;
 
                glPushMatrix();
-               glTranslatef(x, y, 0);
+               glTranslatef(x, -y, 0);
                cmesh_draw(blkmesh);
                glPopMatrix();
        }
@@ -237,9 +291,14 @@ 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);
+                               glTranslatef(j, -i, 0);
                                cmesh_draw(blkmesh);
                                glPopMatrix();
                        }
@@ -299,6 +358,7 @@ static void keyboard(int key, int pressed)
                        next_pos[0] = pos[0] + 1;
                        if(collision(cur_block, next_pos)) {
                                next_pos[0] = pos[0];
+                               update_cur_block();
                                stick(cur_block, next_pos);     /* stick immediately */
                        }
                }
@@ -312,6 +372,7 @@ static void keyboard(int key, int pressed)
                                next_pos[0]++;
                        }
                        next_pos[0]--;
+                       update_cur_block();
                        stick(cur_block, next_pos);     /* stick immediately */
                }
                break;
@@ -376,6 +437,14 @@ static void wheel(int dir)
 {
 }
 
+static void update_cur_block(void)
+{
+       if(cur_block < 0) return;
+
+       memcpy(pos, next_pos, sizeof pos);
+       prev_rot = cur_rot;
+}
+
 static void addscore(int nlines)
 {
        static const int stab[] = {40, 100, 300, 1200}; /* bonus per line completed */
@@ -427,6 +496,7 @@ static int collision(int block, const int *pos)
 
                if(y < 0) continue;
 
+               if(x < 0 || x >= PF_COLS || y >= PF_ROWS) return 1;
                if(pfield[y * PF_COLS + x] & PF_FULL) return 1;
        }