2a5459a2e0f196914838bad2547a54a66b4ec331
[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 #include "lut.h"
10
11 enum {
12         INP_FWD         = 0x01,
13         INP_BACK        = 0x02,
14         INP_LEFT        = 0x04,
15         INP_RIGHT       = 0x08,
16         INP_LTURN       = 0x10,
17         INP_RTURN       = 0x20
18 };
19
20 int init(void);
21 void cleanup(void);
22 void display(void);
23 void idle(void);
24 void reshape(int x, int y);
25 void keyb(unsigned char key, int x, int y);
26 void keyb_up(unsigned char key, int x, int y);
27 void mouse(int bn, int st, int x, int y);
28 void motion(int x, int y);
29
30 int win_width, win_height;
31
32 #define FB_W    640
33 #define FB_H    480
34 unsigned int fb[FB_W * FB_H];
35
36 int mouse_x, mouse_y, mwarp, mbstate[3];
37
38 unsigned int input;
39 int32_t pos[2], angle;
40
41 struct voxscape *vox;
42
43
44 int main(int argc, char **argv)
45 {
46         glutInit(&argc, argv);
47         glutInitWindowSize(1280, 960);
48         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
49         glutCreateWindow("voxel landscape test");
50
51         glutDisplayFunc(display);
52         glutIdleFunc(idle);
53         glutReshapeFunc(reshape);
54         glutKeyboardFunc(keyb);
55         glutKeyboardUpFunc(keyb_up);
56         glutMouseFunc(mouse);
57         glutMotionFunc(motion);
58
59         if(init() == -1) {
60                 return 1;
61         }
62         atexit(cleanup);
63
64         glutMainLoop();
65         return 0;
66 }
67
68
69 int init(void)
70 {
71         init_lut();
72
73         pos[0] = 512 << 16;
74         pos[1] = 512 << 16;
75
76         if(!(vox = vox_open("data/height.png", "data/color.png"))) {
77                 return -1;
78         }
79         vox_framebuf(vox, FB_W, FB_H, fb);
80         vox_proj(vox, 45, 1, 300);
81
82         glfb_setup(FB_W, FB_H, GLFB_RGBA32, FB_W * 4);
83         return 0;
84 }
85
86 void cleanup(void)
87 {
88         vox_free(vox);
89 }
90
91 #define WALK_SPEED      0x40000
92 #define TURN_SPEED      0x100
93
94 void update(void)
95 {
96         int32_t fwd[2], right[2];
97
98         if(input & INP_LTURN) angle += TURN_SPEED;
99         if(input & INP_RTURN) angle -= TURN_SPEED;
100
101         fwd[0] = -SIN(angle);
102         fwd[1] = COS(angle);
103         right[0] = fwd[1];
104         right[1] = -fwd[0];
105
106         if(input & INP_FWD) {
107                 pos[0] += fwd[0];
108                 pos[1] += fwd[1];
109         }
110         if(input & INP_BACK) {
111                 pos[0] -= fwd[0];
112                 pos[1] -= fwd[1];
113         }
114         if(input & INP_RIGHT) {
115                 pos[0] += right[0];
116                 pos[1] += right[1];
117         }
118         if(input & INP_LEFT) {
119                 pos[0] -= right[0];
120                 pos[1] -= right[1];
121         }
122
123         vox_view(vox, pos[0], pos[1], -30, angle);
124 }
125
126 void display(void)
127 {
128         update();
129
130         memset(fb, 0, sizeof fb);
131
132         vox_render(vox);
133         vox_sky_grad(vox, 0xcc77ff, 0x5588cc);
134
135         glfb_update(fb);
136         glfb_display();
137
138         glutSwapBuffers();
139         assert(glGetError() == GL_NO_ERROR);
140
141         if(mbstate[0]) {
142                 mwarp = 1;
143                 glutWarpPointer(win_width / 2, win_height / 2);
144         }
145 }
146
147 void idle(void)
148 {
149         glutPostRedisplay();
150 }
151
152 void reshape(int x, int y)
153 {
154         glViewport(0, 0, x, y);
155
156         win_width = x;
157         win_height = y;
158 }
159
160 void keyb(unsigned char key, int x, int y)
161 {
162         switch(key) {
163         case 27:
164                 exit(0);
165
166         case 'w':
167                 input |= INP_FWD;
168                 break;
169         case 's':
170                 input |= INP_BACK;
171                 break;
172         case 'a':
173                 input |= INP_LEFT;
174                 break;
175         case 'd':
176                 input |= INP_RIGHT;
177                 break;
178         case 'q':
179                 input |= INP_LTURN;
180                 break;
181         case 'e':
182                 input |= INP_RTURN;
183                 break;
184
185         default:
186                 break;
187         }
188 }
189
190 void keyb_up(unsigned char key, int x, int y)
191 {
192         switch(key) {
193         case 'w':
194                 input &= ~INP_FWD;
195                 break;
196         case 's':
197                 input &= ~INP_BACK;
198                 break;
199         case 'a':
200                 input &= ~INP_LEFT;
201                 break;
202         case 'd':
203                 input &= ~INP_RIGHT;
204                 break;
205         case 'q':
206                 input &= ~INP_LTURN;
207                 break;
208         case 'e':
209                 input &= ~INP_RTURN;
210                 break;
211
212         default:
213                 break;
214         }
215 }
216
217 void mouse(int bn, int st, int x, int y)
218 {
219         int bidx = bn - GLUT_LEFT_BUTTON;
220
221         if(bidx < 3) {
222                 mbstate[bidx] = st == GLUT_DOWN;
223         }
224         mouse_x = x;
225         mouse_y = y;
226
227         if(st == GLUT_DOWN) {
228                 glutSetCursor(GLUT_CURSOR_NONE);
229         } else {
230                 glutSetCursor(GLUT_CURSOR_LEFT_ARROW);
231         }
232 }
233
234 void motion(int x, int y)
235 {
236         int dx = x - mouse_x;
237         int dy = y - mouse_y;
238         mouse_x = x;
239         mouse_y = y;
240
241         if(mwarp) {
242                 mwarp = 0;
243                 return;
244         }
245         if(!(dx | dy)) return;
246
247         if(mbstate[0]) {
248                 angle -= dx << 6;
249         }
250 }