deepstone

view src/main.c @ 33:03a0b307706a

added proper keyboard handling
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 23 Sep 2013 04:34:43 +0300
parents 11d14f688485
children c6406e4aa0fb
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <signal.h>
5 #include <conio.h>
6 #include "wvga.h"
7 #include "mingl.h"
8 #include "timer.h"
9 #include "keyb.h"
10 #include "mouse.h"
11 #include "texture.h"
12 #include "palman.h"
13 #include "scene.h"
15 #define DEG2RAD(x) (M_PI * (x) / 180.0)
17 static int init(void);
18 static void shutdown(void);
19 static void redraw(void);
20 static int proc_events(void);
21 static int proc_keyb_input(void);
22 static void mouse_button(int bn, int x, int y);
23 static void mouse_motion(int x, int y);
24 static void sighandler(int s);
27 static float cam_x, cam_y, cam_z;
28 static float cam_theta, cam_phi;
30 static float walk_speed = 0.5;
31 static float look_speed = 1.0;
33 #ifdef __DOS__
34 static int mouse_look = 1;
35 #else
36 static int mouse_look = 0;
37 #endif
39 static void *fbuf;
40 static struct scene scn;
43 int main(void)
44 {
45 if(init() == -1) {
46 return 1;
47 }
49 while(proc_events()) {
50 redraw();
51 }
53 shutdown();
54 return 0;
55 }
58 static int init(void)
59 {
60 float vfov, hfov, aspect;
62 aspect = 320.0 / 200.0;
63 vfov = 60.0;
64 hfov = vfov * aspect;
66 init_timer(100);
67 kb_init(16); /* 16 characters input buffer */
69 set_video_mode(0x13);
71 signal(SIGINT, sighandler);
72 signal(SIGSEGV, sighandler);
73 signal(SIGFPE, sighandler);
74 signal(SIGILL, sighandler);
75 signal(SIGABRT, sighandler);
77 if(mgl_init(320, 200) == -1) {
78 fprintf(stderr, "mgl init failed\n");
79 return -1;
80 }
81 fbuf = mgl_framebuffer();
83 mgl_enable(MGL_CULL_FACE);
84 mgl_enable(MGL_SMOOTH);
85 mgl_enable(MGL_LIGHTING);
86 mgl_enable(MGL_DEPTH_TEST);
88 mgl_matrix_mode(MGL_PROJECTION);
89 mgl_load_identity();
90 mgl_perspective(vfov, aspect, 0.5, 200.0);
92 #if 0
93 mgl_enable(MGL_CLIP_PLANE0);
94 mgl_enable(MGL_CLIP_PLANE1);
95 mgl_enable(MGL_CLIP_PLANE2);
96 mgl_enable(MGL_CLIP_PLANE3);
97 mgl_clip_plane(MGL_CLIP_PLANE0, -1, 0, -1, 0); /* positive X */
98 mgl_clip_plane(MGL_CLIP_PLANE1, 1, 0, -1, 0); /* negative X */
99 mgl_clip_plane(MGL_CLIP_PLANE2, 0, -1, -0.5, 0); /* positive Y */
100 mgl_clip_plane(MGL_CLIP_PLANE3, 0, 1, -0.5, 0); /* negative Y */
101 #endif
103 /* setup palette */
104 palm_add_color(255, 255, 255);
106 scn_init(&scn);
107 if(scn_load(&scn, "data/hall.obj") == -1) {
108 return -1;
109 }
111 palm_build();
112 {
113 int i, palsz = palm_palette_size();
114 struct palm_color *pal = palm_palette();
116 for(i=0; i<palsz; i++) {
117 set_pal_entry(i, pal[i].r, pal[i].g, pal[i].b);
118 }
119 }
121 mgl_color_range(palm_color_range() - 1);
122 return 0;
123 }
125 static void shutdown(void)
126 {
127 mgl_free();
128 set_video_mode(3);
129 kb_shutdown();
130 }
132 static void redraw(void)
133 {
134 mgl_clear(0);
135 mgl_clear_depth();
137 mgl_matrix_mode(MGL_MODELVIEW);
138 mgl_load_identity();
139 mgl_rotate(cam_phi, 1, 0, 0);
140 mgl_rotate(cam_theta, 0, 1, 0);
141 mgl_translate(-cam_x, -cam_y, -cam_z);
142 mgl_translate(0, -2.0, 0);
144 mgl_light_intensity(0, 1.0);
145 mgl_light_position(0, 0, 5, 0, 1);
147 /*mgl_torus(0.75, 0.25, 16, 8);*/
148 scn_render(&scn);
150 copy_frame(fbuf);
151 }
153 #define DEG_TO_RAD(x) (M_PI * (x) / 180.0)
154 void cam_move(float dx, float dy)
155 {
156 float angle = DEG_TO_RAD(cam_theta);
157 cam_x += cos(angle) * dx + sin(angle) * dy;
158 cam_z -= -sin(angle) * dx + cos(angle) * dy;
159 }
161 static int proc_events(void)
162 {
163 static int prev_mx, prev_my, prev_bnmask;
164 int mx, my, bnmask, key;
166 if(!proc_keyb_input()) {
167 return 0;
168 }
170 bnmask = read_mouse(&mx, &my);
171 if(bnmask != prev_bnmask) {
172 mouse_button(bnmask, mx, my);
173 prev_bnmask = bnmask;
174 }
175 if(mx != prev_mx || my != prev_my) {
176 mouse_motion(mx, my);
177 prev_mx = mx;
178 prev_my = my;
179 }
180 return 1;
181 }
183 static int proc_keyb_input(void)
184 {
185 /* first examine all keypresses and handle non-movement keys */
186 int key;
187 while((key = kb_getkey()) != -1) {
188 switch(key) {
189 case 27:
190 return 0;
192 case '`':
193 mouse_look = !mouse_look;
194 break;
196 default:
197 break;
198 }
199 }
201 /* for the movement keys we just care if they are pressed at the moment */
202 if(kb_isdown('w') || kb_isdown('W'))
203 cam_move(0, walk_speed);
205 if(kb_isdown('s') || kb_isdown('S'))
206 cam_move(0, -walk_speed);
208 if(kb_isdown('a') || kb_isdown('A'))
209 cam_move(-walk_speed, 0);
211 if(kb_isdown('d') || kb_isdown('D'))
212 cam_move(walk_speed, 0);
214 return 1;
215 }
217 static int bnstate;
218 static void mouse_button(int bn, int x, int y)
219 {
220 bnstate = bn;
221 }
223 static void mouse_motion(int x, int y)
224 {
225 static int prev_x = -1, prev_y;
226 int dx = x - prev_x;
227 int dy = y - prev_y;
229 if(mouse_look) {
230 if(prev_x == -1) {
231 dx = dy = 0;
232 }
234 set_mouse(160, 100);
235 prev_x = 160;
236 prev_y = 100;
237 } else {
238 prev_x = x;
239 prev_y = y;
240 }
242 if(mouse_look || bnstate) {
243 cam_theta += dx * look_speed;
244 cam_phi += dy * look_speed;
246 if(cam_phi < -90) {
247 cam_phi = -90;
248 }
249 if(cam_phi > 90) {
250 cam_phi = 90;
251 }
252 }
253 }
255 static void sighandler(int s)
256 {
257 set_video_mode(3);
259 switch(s) {
260 case SIGABRT:
261 fprintf(stderr, "abort\n");
262 break;
264 case SIGILL:
265 fprintf(stderr, "illegal operation\n");
266 break;
268 case SIGSEGV:
269 fprintf(stderr, "segmentation fault\n");
270 break;
272 case SIGINT:
273 fprintf(stderr, "interrupted\n");
274 break;
276 case SIGFPE:
277 fprintf(stderr, "floating point exception\n");
278 break;
280 default:
281 fprintf(stderr, "unexpected signal\n");
282 }
284 exit(1);
285 }