added starfield
authorJohn Tsiombikas <nuclear@member.fsf.org>
Sun, 3 Mar 2019 01:48:39 +0000 (03:48 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Sun, 3 Mar 2019 01:48:39 +0000 (03:48 +0200)
src/gamescr.c
src/starfield.c [new file with mode: 0644]

index f2a2ee4..a55f432 100644 (file)
@@ -9,6 +9,9 @@
 #include "blocks.h"
 #include "logger.h"
 
+int init_starfield(void);
+void draw_starfield(void);
+
 static int init(void);
 static void cleanup(void);
 static void start(void);
@@ -84,17 +87,24 @@ static const long level_speed[NUM_LEVELS] = {
 
 static const float blkcolor[][4] = {
        {1.0, 0.65, 0.0, 1},
-       {0.16, 1.0, 0.0, 1},
+       {0.16, 1.0, 0.4, 1},
        {0.65, 0.65, 1.0, 1},
-       {1.0, 1.0, 0.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.25, 0.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;
@@ -116,6 +126,8 @@ static int init(void)
 static void cleanup(void)
 {
        cmesh_free(blkmesh);
+       cmesh_free(wellmesh);
+       glDeleteTextures(1, &tex_well);
 }
 
 static void start(void)
@@ -149,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;
@@ -161,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 */
@@ -185,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;
@@ -225,6 +236,8 @@ static void draw(void)
 
        glLightfv(GL_LIGHT0, GL_POSITION, lpos);
 
+       draw_starfield();
+
        glPushAttrib(GL_ENABLE_BIT);
        glBindTexture(GL_TEXTURE_2D, tex_well);
        glEnable(GL_TEXTURE_2D);
@@ -245,7 +258,7 @@ static void draw(void)
        glPopMatrix();
 }
 
-static const float blkspec[] = {1, 1, 1, 1};
+static const float blkspec[] = {0.85, 0.85, 0.85, 1};
 
 static void draw_block(int block, const int *pos, int rot)
 {
@@ -283,7 +296,7 @@ static void drawpf(void)
                        glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, blkspec);
                        glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0f);
 
-                       if(val & PF_FULL) {
+                       if((val & (PF_FULL | PF_VIS)) == (PF_FULL | PF_VIS)) {
                                glPushMatrix();
                                glTranslatef(j, -i, 0);
                                cmesh_draw(blkmesh);
diff --git a/src/starfield.c b/src/starfield.c
new file mode 100644 (file)
index 0000000..6f7b9ae
--- /dev/null
@@ -0,0 +1,123 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include <cgmath/cgmath.h>
+#include <imago2.h>
+#include "opengl.h"
+#include "game.h"
+#include "logger.h"
+
+static unsigned int tex_bolt, tex_star;
+static float star_speed = 100.0f;
+static float star_depth = 1000.0f;
+static float star_size = 0.35f;
+
+#define STAR_ZOFFS     100.0f
+#define STAR_COUNT     4096
+static cgm_vec3 star[STAR_COUNT];
+static float star_lenxy[STAR_COUNT];
+
+int init_starfield(void)
+{
+       int i;
+       float width;
+
+       if(!(tex_star = img_gltexture_load("data/pimg.png"))) {
+               error_log("failed to load star texture\n");
+               return -1;
+       }
+       if(!(tex_bolt = img_gltexture_load("data/bolt.png"))) {
+               error_log("failed to load star tail texture\n");
+               return -1;
+       }
+
+       width = star_depth / 4.0f;
+       for(i=0; i<STAR_COUNT; i++) {
+               float x = (2.0f * rand() / RAND_MAX) - 1.0;
+               float y = (2.0f * rand() / RAND_MAX) - 1.0;
+               float z = (float)rand() / RAND_MAX;
+
+               cgm_vcons(star + i, x * width, y * width, z * star_depth);
+               star_lenxy[i] = sqrt(star[i].x * star[i].x + star[i].y * star[i].y);
+       }
+       return 0;
+}
+
+void draw_starfield(void)
+{
+       int i;
+       cgm_vec3 pos;
+       float x, y, z, t;
+       float x0, y0, x1, y1;
+       float theta, sz;
+
+       glPushAttrib(GL_ENABLE_BIT);
+       glDisable(GL_DEPTH_TEST);
+       glDisable(GL_LIGHTING);
+       glDisable(GL_CULL_FACE);
+       glEnable(GL_BLEND);
+       glBlendFunc(GL_SRC_ALPHA, GL_ONE);
+
+       glEnable(GL_TEXTURE_2D);
+       glBindTexture(GL_TEXTURE_2D, tex_bolt);
+
+       glBegin(GL_QUADS);
+       for(i=0; i<STAR_COUNT; i++) {
+               pos = star[i];
+               z = fmod(pos.z + time_msec * star_speed / 1000.0f, star_depth);
+               t = z / star_depth;
+               pos.z = z - star_depth + STAR_ZOFFS;
+
+               theta = atan2(pos.y / star_lenxy[i], pos.x / star_lenxy[i]);
+
+               y = -star_size;
+               x = 0;
+
+               x0 = x * cos(theta) - y * sin(theta);
+               y0 = x * sin(theta) + y * cos(theta);
+
+               y = star_size;
+
+               x1 = x * cos(theta) - y * sin(theta);
+               y1 = x * sin(theta) + y * cos(theta);
+
+               x0 += pos.x;
+               x1 += pos.x;
+               y0 += pos.y;
+               y1 += pos.y;
+
+               glColor4f(1, 1, 1, t);
+               glTexCoord2f(0, 1);
+               glVertex3f(x0, y0, pos.z);
+               glTexCoord2f(1, 1);
+               glVertex3f(x1, y1, pos.z);
+               glTexCoord2f(1, 0);
+               glVertex3f(x1, y1, pos.z - star_size * 16.0);
+               glTexCoord2f(0, 0);
+               glVertex3f(x0, y0, pos.z - star_size * 16.0);
+       }
+       glEnd();
+
+       glBindTexture(GL_TEXTURE_2D, tex_star);
+       sz = star_size * 4.0f;
+       glBegin(GL_QUADS);
+       for(i=0; i<STAR_COUNT; i++) {
+               pos = star[i];
+               z = fmod(pos.z + time_msec * star_speed / 1000.0f, star_depth);
+               t = z / star_depth;
+               pos.z = z - star_depth + STAR_ZOFFS;
+
+               glColor4f(1, 1, 1, t);
+               glTexCoord2f(0, 0);
+               glVertex3f(pos.x - sz, pos.y - sz, pos.z);
+               glTexCoord2f(1, 0);
+               glVertex3f(pos.x + sz, pos.y - sz, pos.z);
+               glTexCoord2f(1, 1);
+               glVertex3f(pos.x + sz, pos.y + sz, pos.z);
+               glTexCoord2f(0, 1);
+               glVertex3f(pos.x - sz, pos.y + sz, pos.z);
+       }
+       glEnd();
+
+       glPopAttrib();
+}