it works
[voxscape] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <assert.h>
6 #include <GL/glut.h>
7 #include "glfb.h"
8 #include "voxscape.h"
9
10 enum {
11         INP_FWD         = 1,
12         INP_BACK        = 2,
13         INP_LEFT        = 4,
14         INP_RIGHT       = 8
15 };
16
17 int init(void);
18 void cleanup(void);
19 void display(void);
20 void idle(void);
21 void reshape(int x, int y);
22 void keyb(unsigned char key, int x, int y);
23 void keyb_up(unsigned char key, int x, int y);
24
25 int win_width, win_height;
26
27 #define FB_W    640
28 #define FB_H    480
29 unsigned int fb[FB_W * FB_H];
30
31 unsigned int input;
32 int32_t pos[2];
33
34 struct voxscape *vox;
35
36
37 int main(int argc, char **argv)
38 {
39         glutInit(&argc, argv);
40         glutInitWindowSize(1280, 960);
41         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
42         glutCreateWindow("voxel landscape test");
43
44         glutDisplayFunc(display);
45         glutReshapeFunc(reshape);
46         glutKeyboardFunc(keyb);
47         glutKeyboardUpFunc(keyb_up);
48         glutIdleFunc(idle);
49
50         if(init() == -1) {
51                 return 1;
52         }
53         atexit(cleanup);
54
55         glutMainLoop();
56         return 0;
57 }
58
59
60 int init(void)
61 {
62         pos[0] = 512 << 16;
63         pos[1] = 512 << 16;
64
65         if(!(vox = vox_open("data/height.png", "data/color.png"))) {
66                 return -1;
67         }
68         vox_framebuf(vox, FB_W, FB_H, fb);
69         vox_proj(vox, 45, 1, 200);
70         vox_view(vox, pos[0], pos[1], 0);
71
72         glfb_setup(FB_W, FB_H, GLFB_RGBA32, FB_W * 4);
73         return 0;
74 }
75
76 void cleanup(void)
77 {
78         vox_free(vox);
79 }
80
81 #define WALK_SPEED      0x40000
82 void update(void)
83 {
84         if(input & INP_FWD) pos[1] += WALK_SPEED;
85         if(input & INP_BACK) pos[1] -= WALK_SPEED;
86         if(input & INP_LEFT) pos[0] -= WALK_SPEED;
87         if(input & INP_RIGHT) pos[0] += WALK_SPEED;
88
89         vox_view(vox, pos[0], pos[1], 0);
90 }
91
92 void display(void)
93 {
94         update();
95
96         memset(fb, 0, sizeof fb);
97
98         vox_render(vox);
99         vox_sky_grad(vox, 0xcc77ff, 0x5588cc);
100
101         glfb_update(fb);
102         glfb_display();
103
104         glutSwapBuffers();
105         assert(glGetError() == GL_NO_ERROR);
106 }
107
108 void idle(void)
109 {
110         glutPostRedisplay();
111 }
112
113 void reshape(int x, int y)
114 {
115         glViewport(0, 0, x, y);
116
117         win_width = x;
118         win_height = y;
119 }
120
121 void keyb(unsigned char key, int x, int y)
122 {
123         switch(key) {
124         case 27:
125                 exit(0);
126
127         case 'w':
128                 input |= INP_FWD;
129                 break;
130         case 's':
131                 input |= INP_BACK;
132                 break;
133         case 'a':
134                 input |= INP_LEFT;
135                 break;
136         case 'd':
137                 input |= INP_RIGHT;
138                 break;
139
140         default:
141                 break;
142         }
143 }
144
145 void keyb_up(unsigned char key, int x, int y)
146 {
147         switch(key) {
148         case 'w':
149                 input &= ~INP_FWD;
150                 break;
151         case 's':
152                 input &= ~INP_BACK;
153                 break;
154         case 'a':
155                 input &= ~INP_LEFT;
156                 break;
157         case 'd':
158                 input &= ~INP_RIGHT;
159                 break;
160
161         default:
162                 break;
163         }
164 }