deepstone

annotate src/main.c @ 35:1870c4ef8b76

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