f690a0e3e763e4f2866de02f9a873bddcac8b8c4
[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
28 int win_width, win_height;
29
30 #define FB_W    640
31 #define FB_H    480
32 unsigned int fb[FB_W * FB_H];
33
34 unsigned int input;
35 int32_t pos[2], angle;// = 0x4000;
36
37 struct voxscape *vox;
38
39
40 int main(int argc, char **argv)
41 {
42         glutInit(&argc, argv);
43         glutInitWindowSize(1280, 960);
44         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
45         glutCreateWindow("voxel landscape test");
46
47         glutDisplayFunc(display);
48         glutReshapeFunc(reshape);
49         glutKeyboardFunc(keyb);
50         glutKeyboardUpFunc(keyb_up);
51         glutIdleFunc(idle);
52
53         if(init() == -1) {
54                 return 1;
55         }
56         atexit(cleanup);
57
58         glutMainLoop();
59         return 0;
60 }
61
62
63 int init(void)
64 {
65         init_lut();
66
67         pos[0] = 512 << 16;
68         pos[1] = 512 << 16;
69
70         if(!(vox = vox_open("data/height.png", "data/color.png"))) {
71                 return -1;
72         }
73         vox_framebuf(vox, FB_W, FB_H, fb);
74         vox_proj(vox, 45, 1, 200);
75         vox_view(vox, pos[0], pos[1], angle);
76
77         glfb_setup(FB_W, FB_H, GLFB_RGBA32, FB_W * 4);
78         return 0;
79 }
80
81 void cleanup(void)
82 {
83         vox_free(vox);
84 }
85
86 #define WALK_SPEED      0x40000
87 #define TURN_SPEED      0x100
88
89 void update(void)
90 {
91         if(input & INP_FWD) pos[1] += WALK_SPEED;
92         if(input & INP_BACK) pos[1] -= WALK_SPEED;
93         if(input & INP_LEFT) pos[0] -= WALK_SPEED;
94         if(input & INP_RIGHT) pos[0] += WALK_SPEED;
95
96         if(input & INP_LTURN) angle += TURN_SPEED;
97         if(input & INP_RTURN) angle -= TURN_SPEED;
98
99         vox_view(vox, pos[0], pos[1], angle);
100 }
101
102 void display(void)
103 {
104         update();
105
106         memset(fb, 0, sizeof fb);
107
108         vox_render(vox);
109         vox_sky_grad(vox, 0xcc77ff, 0x5588cc);
110
111         glfb_update(fb);
112         glfb_display();
113
114         glutSwapBuffers();
115         assert(glGetError() == GL_NO_ERROR);
116 }
117
118 void idle(void)
119 {
120         glutPostRedisplay();
121 }
122
123 void reshape(int x, int y)
124 {
125         glViewport(0, 0, x, y);
126
127         win_width = x;
128         win_height = y;
129 }
130
131 void keyb(unsigned char key, int x, int y)
132 {
133         switch(key) {
134         case 27:
135                 exit(0);
136
137         case 'w':
138                 input |= INP_FWD;
139                 break;
140         case 's':
141                 input |= INP_BACK;
142                 break;
143         case 'a':
144                 input |= INP_LEFT;
145                 break;
146         case 'd':
147                 input |= INP_RIGHT;
148                 break;
149         case 'q':
150                 input |= INP_LTURN;
151                 break;
152         case 'e':
153                 input |= INP_RTURN;
154                 break;
155
156         default:
157                 break;
158         }
159 }
160
161 void keyb_up(unsigned char key, int x, int y)
162 {
163         switch(key) {
164         case 'w':
165                 input &= ~INP_FWD;
166                 break;
167         case 's':
168                 input &= ~INP_BACK;
169                 break;
170         case 'a':
171                 input &= ~INP_LEFT;
172                 break;
173         case 'd':
174                 input &= ~INP_RIGHT;
175                 break;
176         case 'q':
177                 input &= ~INP_LTURN;
178                 break;
179         case 'e':
180                 input &= ~INP_RTURN;
181                 break;
182
183         default:
184                 break;
185         }
186 }