deepstone

view src/main.c @ 34:c6406e4aa0fb

better input, fixed emulated code to work again
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 23 Sep 2013 05:58:24 +0300
parents 03a0b307706a
children 17a5107b6fa4
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 update(unsigned long dtmsec);
20 static void redraw(void);
21 static int proc_events(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 = 6.0;
31 static float look_speed = 1.0;
33 static int mouse_look = 1;
35 static void *fbuf;
36 static struct scene scn;
39 int main(void)
40 {
41 unsigned long prev_msec = 0;
43 if(init() == -1) {
44 return 1;
45 }
47 for(;;) {
48 unsigned long msec = get_msec();
49 unsigned long dt = msec - prev_msec;
50 prev_msec = msec;
52 if(!proc_events()) {
53 break;
54 }
56 update(dt);
57 redraw();
58 }
60 shutdown();
61 return 0;
62 }
65 static int init(void)
66 {
67 float vfov, aspect;
69 aspect = 320.0 / 200.0;
70 vfov = 60.0;
72 init_timer(100);
73 kb_init(16); /* 16 characters input buffer */
75 set_video_mode(0x13);
77 signal(SIGINT, sighandler);
78 signal(SIGSEGV, sighandler);
79 signal(SIGFPE, sighandler);
80 signal(SIGILL, sighandler);
81 signal(SIGABRT, sighandler);
83 if(mgl_init(320, 200) == -1) {
84 fprintf(stderr, "mgl init failed\n");
85 return -1;
86 }
87 fbuf = mgl_framebuffer();
89 mgl_enable(MGL_CULL_FACE);
90 mgl_enable(MGL_SMOOTH);
91 mgl_enable(MGL_LIGHTING);
92 mgl_enable(MGL_DEPTH_TEST);
94 mgl_matrix_mode(MGL_PROJECTION);
95 mgl_load_identity();
96 mgl_perspective(vfov, aspect, 0.5, 200.0);
98 #if 0
99 mgl_enable(MGL_CLIP_PLANE0);
100 mgl_enable(MGL_CLIP_PLANE1);
101 mgl_enable(MGL_CLIP_PLANE2);
102 mgl_enable(MGL_CLIP_PLANE3);
103 mgl_clip_plane(MGL_CLIP_PLANE0, -1, 0, -1, 0); /* positive X */
104 mgl_clip_plane(MGL_CLIP_PLANE1, 1, 0, -1, 0); /* negative X */
105 mgl_clip_plane(MGL_CLIP_PLANE2, 0, -1, -0.5, 0); /* positive Y */
106 mgl_clip_plane(MGL_CLIP_PLANE3, 0, 1, -0.5, 0); /* negative Y */
107 #endif
109 /* setup palette */
110 palm_add_color(255, 255, 255);
112 scn_init(&scn);
113 if(scn_load(&scn, "data/hall.obj") == -1) {
114 return -1;
115 }
117 palm_build();
118 {
119 int i, palsz = palm_palette_size();
120 struct palm_color *pal = palm_palette();
122 for(i=0; i<palsz; i++) {
123 set_pal_entry(i, pal[i].r, pal[i].g, pal[i].b);
124 }
125 }
127 mgl_color_range(palm_color_range() - 1);
128 return 0;
129 }
131 static void shutdown(void)
132 {
133 mgl_free();
134 set_video_mode(3);
135 kb_shutdown();
136 }
139 #define DEG_TO_RAD(x) (M_PI * (x) / 180.0)
140 void cam_move(float dx, float dy)
141 {
142 float angle = DEG_TO_RAD(cam_theta);
143 cam_x += cos(angle) * dx + sin(angle) * dy;
144 cam_z -= -sin(angle) * dx + cos(angle) * dy;
145 }
147 static void update(unsigned long dtmsec)
148 {
149 float dt = (float)dtmsec / 1000.0f;
150 float offs = walk_speed * dt;
152 if(kb_isdown('w') || kb_isdown('W'))
153 cam_move(0, offs);
155 if(kb_isdown('s') || kb_isdown('S'))
156 cam_move(0, -offs);
158 if(kb_isdown('a') || kb_isdown('A'))
159 cam_move(-offs, 0);
161 if(kb_isdown('d') || kb_isdown('D'))
162 cam_move(offs, 0);
163 }
165 static void redraw(void)
166 {
167 mgl_clear(0);
168 mgl_clear_depth();
170 mgl_matrix_mode(MGL_MODELVIEW);
171 mgl_load_identity();
172 mgl_rotate(cam_phi, 1, 0, 0);
173 mgl_rotate(cam_theta, 0, 1, 0);
174 mgl_translate(-cam_x, -cam_y, -cam_z);
175 mgl_translate(0, -2.0, 0);
177 mgl_light_intensity(0, 1.0);
178 mgl_light_position(0, 0, 5, 0, 1);
180 /*mgl_torus(0.75, 0.25, 16, 8);*/
181 scn_render(&scn);
183 copy_frame(fbuf);
184 }
186 static int proc_events(void)
187 {
188 static int prev_mx, prev_my, prev_bnmask;
189 int mx, my, bnmask;
191 int key;
192 while((key = kb_getkey()) != -1) {
193 switch(key) {
194 case 27:
195 return 0;
197 case '`':
198 mouse_look = !mouse_look;
199 break;
201 default:
202 break;
203 }
204 }
206 bnmask = read_mouse(&mx, &my);
207 if(bnmask != prev_bnmask) {
208 mouse_button(bnmask, mx, my);
209 prev_bnmask = bnmask;
210 }
211 if(mx != prev_mx || my != prev_my) {
212 mouse_motion(mx, my);
213 prev_mx = mx;
214 prev_my = my;
215 }
216 return 1;
217 }
219 static int bnstate;
220 static void mouse_button(int bn, int x, int y)
221 {
222 bnstate = bn;
223 }
225 static void mouse_motion(int x, int y)
226 {
227 static int prev_x = -1, prev_y;
228 int dx = x - prev_x;
229 int dy = y - prev_y;
231 if(mouse_look) {
232 if(prev_x == -1) {
233 dx = dy = 0;
234 }
236 set_mouse(160, 100);
237 prev_x = 160;
238 prev_y = 100;
239 } else {
240 prev_x = x;
241 prev_y = y;
242 }
244 if(mouse_look || bnstate) {
245 cam_theta += dx * look_speed;
246 cam_phi += dy * look_speed;
248 if(cam_phi < -90) {
249 cam_phi = -90;
250 }
251 if(cam_phi > 90) {
252 cam_phi = 90;
253 }
254 }
255 }
257 static void sighandler(int s)
258 {
259 set_video_mode(3);
261 switch(s) {
262 case SIGABRT:
263 fprintf(stderr, "abort\n");
264 break;
266 case SIGILL:
267 fprintf(stderr, "illegal operation\n");
268 break;
270 case SIGSEGV:
271 fprintf(stderr, "segmentation fault\n");
272 break;
274 case SIGINT:
275 fprintf(stderr, "interrupted\n");
276 break;
278 case SIGFPE:
279 fprintf(stderr, "floating point exception\n");
280 break;
282 default:
283 fprintf(stderr, "unexpected signal\n");
284 }
286 exit(1);
287 }