desaturated next block
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Wed, 13 Mar 2019 18:21:14 +0000 (20:21 +0200)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Wed, 13 Mar 2019 18:21:14 +0000 (20:21 +0200)
src/color.c [new file with mode: 0644]
src/color.h [new file with mode: 0644]
src/gamescr.c

diff --git a/src/color.c b/src/color.c
new file mode 100644 (file)
index 0000000..f1de5f4
--- /dev/null
@@ -0,0 +1,85 @@
+#include <math.h>
+#include "color.h"
+
+static float min3(float a, float b, float c)
+{
+       if(a < b && a < c) return a;
+       return b < c ? b : c;
+}
+
+static float max3(float a, float b, float c)
+{
+       if(a > b && a > c) return a;
+       return b > c ? b : c;
+}
+
+void rgb_to_hsv(float r, float g, float b, float *h, float *s, float *v)
+{
+       float min, max, delta;
+
+       min = min3(r, g, b);
+       max = max3(r, g, b);
+       *v = max;
+
+       delta = max - min;
+
+       if(max != 0) {
+               *s = delta / max;
+       } else {
+               *s = 0;
+               *h = -1;
+               return;
+       }
+
+       if(!delta) delta = 1.0f;
+
+       if(r == max)
+               *h = (g - b) / delta;
+       else if( g == max )
+               *h = 2 + (b - r) / delta;
+       else
+               *h = 4 + (r - g) / delta;
+
+       *h *= 60;
+       if(*h < 0) {
+               *h += 360.0f;
+       }
+       *h /= 360.0f;
+}
+
+#define RETRGB(red, green, blue) \
+       do { \
+               *r = (red); \
+               *g = (green); \
+               *b = (blue); \
+               return; \
+       } while(0)
+
+void hsv_to_rgb(float h, float s, float v, float *r, float *g, float *b)
+{
+       float sec, frac, o, p, q;
+       int hidx;
+
+       if(s == 0.0f) {
+               *r = *g = *b = v;
+               return;
+       }
+
+       sec = floor(h * (360.0f / 60.0f));
+       frac = (h * (360.0f / 60.0f)) - sec;
+
+       o = v * (1.0f - s);
+       p = v * (1.0f - s * frac);
+       q = v * (1.0f - s * (1.0f - frac));
+
+       hidx = (int)sec;
+       switch(hidx) {
+       default:
+       case 0: RETRGB(v, q, o);
+       case 1: RETRGB(p, v, o);
+       case 2: RETRGB(o, v, q);
+       case 3: RETRGB(o, p, v);
+       case 4: RETRGB(q, o, v);
+       case 5: RETRGB(v, o, p);
+       }
+}
diff --git a/src/color.h b/src/color.h
new file mode 100644 (file)
index 0000000..b955b84
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef COLOR_H_
+#define COLOR_H_
+
+void rgb_to_hsv(float r, float g, float b, float *h, float *s, float *v);
+void hsv_to_rgb(float h, float s, float v, float *r, float *g, float *b);
+
+#endif /* COLOR_H_ */
index fde286d..07193fa 100644 (file)
@@ -10,6 +10,7 @@
 #include "blocks.h"
 #include "logger.h"
 #include "gameinp.h"
 #include "blocks.h"
 #include "logger.h"
 #include "gameinp.h"
+#include "color.h"
 
 int init_starfield(void);
 void draw_starfield(void);
 
 int init_starfield(void);
 void draw_starfield(void);
@@ -20,7 +21,7 @@ static void start(void);
 static void stop(void);
 static void update(float dt);
 static void draw(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);
 static void drawpf(void);
 static void reshape(int x, int y);
 static void keyboard(int key, int pressed);
@@ -328,27 +329,41 @@ static void draw(void)
 
        drawpf();
        if(cur_block >= 0) {
 
        drawpf();
        if(cur_block >= 0) {
-               draw_block(cur_block, pos, cur_rot);
+               draw_block(cur_block, pos, cur_rot, 1.0f, 1.0f);
        }
        glPopMatrix();
 
        }
        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);
        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);
+       draw_block(next_block, nextblk_pos, 0, 0.25f, 0.75f);
+       glPopMatrix();
+
+       glPopAttrib();
 }
 
 static const float blkspec[] = {0.85, 0.85, 0.85, 1};
 
 }
 
 static const float blkspec[] = {0.85, 0.85, 0.85, 1};
 
-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)
 {
        int i;
        unsigned char *p = blocks[block][rot];
 {
        int i;
        unsigned char *p = blocks[block][rot];
+       float col[4], hsv[3];
+
+       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, blkcolor[block]);
+       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);
 
        glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, blkspec);
        glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0f);