first working version
[shapestoy] / src / demo.c
index 265e410..4e857f3 100644 (file)
@@ -1,15 +1,30 @@
 #include <stdio.h>
+#include <stdlib.h>
 #include <stdint.h>
 #include "demo.h"
 #include "opengl.h"
 #include "sanegl.h"
 #include "assman.h"
 #include "cmesh.h"
+#include "music.h"
 
+static void draw_texquad(float x, float y, float w, float h);
+static int piece_hit(int x, int y);
 static void gen_default_textures(void);
 
 static unsigned int sdr_foo;
 static unsigned int tex_logo;
+static unsigned int tex_bg, tex_piece;
+static int bg_width, bg_height;
+static float bg_aspect;
+static int piece_width, piece_height;
+static float vis_width, vis_height;
+static float xoffs, yoffs;
+static float xpos, ypos;
+static int dragging;
+static int prev_mx, prev_my;
+static int xtarg, ytarg;
+static int inplace;
 
 int demo_init(void)
 {
@@ -20,6 +35,10 @@ int demo_init(void)
                return -1;
        }
 
+       if(init_music() == -1) {
+               return -1;
+       }
+
        /* global "debug" shader which just visualizes normals */
        if(!(sdr_dbg = get_sdrprog("sdr/dbg.v.glsl", "sdr/dbg.p.glsl"))) {
                return -1;
@@ -49,13 +68,33 @@ int demo_init(void)
        gl_end();
        swap_buffers();
 
-       glEnable(GL_DEPTH_TEST);
+       if(!(tex_bg = get_tex2d("data/bunny_bg.jpg"))) {
+               return -1;
+       }
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+       bg_width = 2048;
+       bg_height = 1360;
+       bg_aspect = (float)bg_width / (float)bg_height;
+
+       if(!(tex_piece = get_tex2d("data/bunny.png"))) {
+               return -1;
+       }
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+       piece_width = piece_height = 512;
+
+       xtarg = 933;
+       ytarg = 443;
+
+       glDisable(GL_DEPTH_TEST);
        glEnable(GL_CULL_FACE);
        return 0;
 }
 
 void demo_cleanup(void)
 {
+       destroy_music();
        destroy_assman();
 
        glDeleteTextures(1, &deftex_white);
@@ -67,18 +106,44 @@ void demo_display(void)
 {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
-       glBindTexture(GL_TEXTURE_2D, tex_logo);
+       gl_matrix_mode(GL_MODELVIEW);
+       gl_load_identity();
+       gl_translatef(xoffs, yoffs, 0);
+
+       glBindTexture(GL_TEXTURE_2D, tex_bg);
        glUseProgram(sdr_foo);
-       gl_begin(GL_QUADS);
+
        gl_color3f(1, 1, 1);
+       draw_texquad(0, 0, bg_width, bg_height);
+
+       gl_load_identity();
+       gl_translatef(xpos, vis_height - ypos - piece_height, 0);
+
+       glEnable(GL_BLEND);
+       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+
+       glBindTexture(GL_TEXTURE_2D, tex_piece);
+       if(!inplace) {
+               gl_color4f(0, 0, 0, 0.65);
+               draw_texquad(10, -15, piece_width, piece_height);
+       }
+       gl_color3f(1, 1, 1);
+       draw_texquad(0, 0, piece_width, piece_height);
+
+       glDisable(GL_BLEND);
+}
+
+static void draw_texquad(float x, float y, float w, float h)
+{
+       gl_begin(GL_QUADS);
        gl_texcoord2f(0, 1);
-       gl_vertex2f(-1, -1);
+       gl_vertex2f(x, y);
        gl_texcoord2f(1, 1);
-       gl_vertex2f(1, -1);
+       gl_vertex2f(x + w, 0);
        gl_texcoord2f(1, 0);
-       gl_vertex2f(1, 1);
+       gl_vertex2f(x + w, y + h);
        gl_texcoord2f(0, 0);
-       gl_vertex2f(-1, 1);
+       gl_vertex2f(x, y + h);
        gl_end();
 }
 
@@ -88,18 +153,98 @@ void demo_reshape(int x, int y)
        win_width = x;
        win_height = y;
        win_aspect = (float)x / (float)y;
+
+       gl_matrix_mode(GL_PROJECTION);
+       gl_load_identity();
+       if(bg_aspect >= win_aspect) {
+               vis_width = bg_width;
+               vis_height = bg_width / win_aspect;
+               xoffs = 0;
+               yoffs = (vis_height - bg_height) * 0.5f;
+       } else {
+               vis_width = bg_height * win_aspect;
+               vis_height = bg_height;
+               xoffs = (bg_height * win_aspect - bg_width) * 0.5f;
+               yoffs = 0;
+       }
+       gl_ortho(0, vis_width, 0, vis_height, -1, 1);
 }
 
 void demo_keyboard(int key, int pressed)
 {
+       if(!pressed) return;
+
+       switch(key) {
+       case ' ':
+               xpos = xtarg + xoffs;
+               ypos = ytarg + yoffs;
+               break;
+       }
+}
+
+static void scr_to_vis(int *xp, int *yp)
+{
+       float x = (float)*xp;
+       float y = (float)*yp;
+       *xp = x / (float)win_width * (float)vis_width;
+       *yp = y / (float)win_height * (float)vis_height;
 }
 
 void demo_mouse(int bn, int pressed, int x, int y)
 {
+       if(pressed) {
+               scr_to_vis(&x, &y);
+               if(piece_hit(x, y)) {
+                       dragging = 1;
+                       prev_mx = x;
+                       prev_my = y;
+                       inplace = 0;
+               }
+       } else {
+               if(dragging) {
+                       dragging = 0;
+
+                       if(fabs(xpos - (xtarg + xoffs)) < 20 && fabs(ypos - (ytarg + yoffs)) < 20) {
+                               xpos = xtarg + xoffs;
+                               ypos = ytarg + yoffs;
+                               inplace = 1;
+                               play_music();
+                       }
+               }
+       }
 }
 
 void demo_motion(int x, int y)
 {
+       int dx, dy;
+
+       scr_to_vis(&x, &y);
+
+       dx = x - prev_mx;
+       dy = y - prev_my;
+       prev_mx = x;
+       prev_my = y;
+
+       if(!dragging || !(dx | dy)) {
+               return;
+       }
+
+       xpos += dx;
+       ypos += dy;
+
+       if(xpos < 0) xpos = 0;
+       if(ypos < 0) ypos = 0;
+       if(xpos + piece_width > vis_width) xpos = vis_width - piece_width;
+       if(ypos + piece_height > vis_height) ypos = vis_height - piece_height;
+}
+
+static int piece_hit(int x, int y)
+{
+       if(x < xpos) return 0;
+       if(x >= xpos + piece_width) return 0;
+       if(y < ypos) return 0;
+       if(y >= ypos + piece_height) return 0;
+       return 1;
 }
 
 static void gen_default_textures(void)