it works
[voxscape] / src / main.c
index a2440af..c2192ee 100644 (file)
@@ -5,12 +5,22 @@
 #include <assert.h>
 #include <GL/glut.h>
 #include "glfb.h"
+#include "voxscape.h"
+
+enum {
+       INP_FWD         = 1,
+       INP_BACK        = 2,
+       INP_LEFT        = 4,
+       INP_RIGHT       = 8
+};
 
 int init(void);
+void cleanup(void);
 void display(void);
 void idle(void);
 void reshape(int x, int y);
 void keyb(unsigned char key, int x, int y);
+void keyb_up(unsigned char key, int x, int y);
 
 int win_width, win_height;
 
@@ -18,6 +28,11 @@ int win_width, win_height;
 #define FB_H   480
 unsigned int fb[FB_W * FB_H];
 
+unsigned int input;
+int32_t pos[2];
+
+struct voxscape *vox;
+
 
 int main(int argc, char **argv)
 {
@@ -29,11 +44,13 @@ int main(int argc, char **argv)
        glutDisplayFunc(display);
        glutReshapeFunc(reshape);
        glutKeyboardFunc(keyb);
+       glutKeyboardUpFunc(keyb_up);
        glutIdleFunc(idle);
 
        if(init() == -1) {
                return 1;
        }
+       atexit(cleanup);
 
        glutMainLoop();
        return 0;
@@ -42,29 +59,45 @@ int main(int argc, char **argv)
 
 int init(void)
 {
-       int i, j, xor, r, g, b;
-       unsigned int *ptr;
-
-       ptr = fb;
-       for(i=0; i<FB_H; i++) {
-               for(j=0; j<FB_W; j++) {
-                       xor = i ^ j;
-                       r = (xor >> 1) & 0xff;
-                       g = xor & 0xff;
-                       b = (xor << 1) & 0xff;
-                       *ptr++ = b | (g << 8) | (r << 16);
-               }
-       }
+       pos[0] = 512 << 16;
+       pos[1] = 512 << 16;
 
-       win_width = glutGet(GLUT_WINDOW_WIDTH);
-       win_height = glutGet(GLUT_WINDOW_HEIGHT);
+       if(!(vox = vox_open("data/height.png", "data/color.png"))) {
+               return -1;
+       }
+       vox_framebuf(vox, FB_W, FB_H, fb);
+       vox_proj(vox, 45, 1, 200);
+       vox_view(vox, pos[0], pos[1], 0);
 
        glfb_setup(FB_W, FB_H, GLFB_RGBA32, FB_W * 4);
        return 0;
 }
 
+void cleanup(void)
+{
+       vox_free(vox);
+}
+
+#define WALK_SPEED     0x40000
+void update(void)
+{
+       if(input & INP_FWD) pos[1] += WALK_SPEED;
+       if(input & INP_BACK) pos[1] -= WALK_SPEED;
+       if(input & INP_LEFT) pos[0] -= WALK_SPEED;
+       if(input & INP_RIGHT) pos[0] += WALK_SPEED;
+
+       vox_view(vox, pos[0], pos[1], 0);
+}
+
 void display(void)
 {
+       update();
+
+       memset(fb, 0, sizeof fb);
+
+       vox_render(vox);
+       vox_sky_grad(vox, 0xcc77ff, 0x5588cc);
+
        glfb_update(fb);
        glfb_display();
 
@@ -91,6 +124,40 @@ void keyb(unsigned char key, int x, int y)
        case 27:
                exit(0);
 
+       case 'w':
+               input |= INP_FWD;
+               break;
+       case 's':
+               input |= INP_BACK;
+               break;
+       case 'a':
+               input |= INP_LEFT;
+               break;
+       case 'd':
+               input |= INP_RIGHT;
+               break;
+
+       default:
+               break;
+       }
+}
+
+void keyb_up(unsigned char key, int x, int y)
+{
+       switch(key) {
+       case 'w':
+               input &= ~INP_FWD;
+               break;
+       case 's':
+               input &= ~INP_BACK;
+               break;
+       case 'a':
+               input &= ~INP_LEFT;
+               break;
+       case 'd':
+               input &= ~INP_RIGHT;
+               break;
+
        default:
                break;
        }