deepstone

annotate src/main.c @ 27:dcfe615c4c5f

moved firstp.c to main.c
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Sep 2013 02:47:46 +0300
parents
children 11d14f688485
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@27 9 #include "mouse.h"
nuclear@27 10 #include "texture.h"
nuclear@27 11 #include "palman.h"
nuclear@27 12 #include "scene.h"
nuclear@27 13
nuclear@27 14 #define DEG2RAD(x) (M_PI * (x) / 180.0)
nuclear@27 15
nuclear@27 16 static int init(void);
nuclear@27 17 static void shutdown(void);
nuclear@27 18 static void redraw(void);
nuclear@27 19 static int proc_events(void);
nuclear@27 20 static int keyb(int key);
nuclear@27 21 static void mouse_button(int bn, int x, int y);
nuclear@27 22 static void mouse_motion(int x, int y);
nuclear@27 23 static void sighandler(int s);
nuclear@27 24
nuclear@27 25
nuclear@27 26 static float cam_x, cam_y, cam_z;
nuclear@27 27 static float cam_theta, cam_phi;
nuclear@27 28
nuclear@27 29 static float walk_speed = 0.5;
nuclear@27 30 static float look_speed = 1.0;
nuclear@27 31 static int mouse_look = 0;
nuclear@27 32
nuclear@27 33 static void *fbuf;
nuclear@27 34 static struct scene scn;
nuclear@27 35
nuclear@27 36
nuclear@27 37 int main(void)
nuclear@27 38 {
nuclear@27 39 if(init() == -1) {
nuclear@27 40 return 1;
nuclear@27 41 }
nuclear@27 42
nuclear@27 43 while(proc_events()) {
nuclear@27 44 redraw();
nuclear@27 45 }
nuclear@27 46
nuclear@27 47 shutdown();
nuclear@27 48 return 0;
nuclear@27 49 }
nuclear@27 50
nuclear@27 51
nuclear@27 52 static int init(void)
nuclear@27 53 {
nuclear@27 54 init_timer(100);
nuclear@27 55
nuclear@27 56 set_video_mode(0x13);
nuclear@27 57
nuclear@27 58 signal(SIGINT, sighandler);
nuclear@27 59 signal(SIGSEGV, sighandler);
nuclear@27 60 signal(SIGFPE, sighandler);
nuclear@27 61 signal(SIGILL, sighandler);
nuclear@27 62 signal(SIGABRT, sighandler);
nuclear@27 63
nuclear@27 64 if(mgl_init(320, 200) == -1) {
nuclear@27 65 fprintf(stderr, "mgl init failed\n");
nuclear@27 66 return -1;
nuclear@27 67 }
nuclear@27 68 fbuf = mgl_framebuffer();
nuclear@27 69
nuclear@27 70 mgl_enable(MGL_CULL_FACE);
nuclear@27 71 mgl_enable(MGL_SMOOTH);
nuclear@27 72 mgl_enable(MGL_LIGHTING);
nuclear@27 73 mgl_enable(MGL_DEPTH_TEST);
nuclear@27 74
nuclear@27 75 mgl_matrix_mode(MGL_PROJECTION);
nuclear@27 76 mgl_load_identity();
nuclear@27 77 mgl_perspective(45.0, 320.0 / 200.0, 0.5, 200.0);
nuclear@27 78
nuclear@27 79 /* setup palette */
nuclear@27 80 palm_add_color(255, 255, 255);
nuclear@27 81
nuclear@27 82 scn_init(&scn);
nuclear@27 83 if(scn_load(&scn, "data/hall.obj") == -1) {
nuclear@27 84 return -1;
nuclear@27 85 }
nuclear@27 86
nuclear@27 87 palm_build();
nuclear@27 88 {
nuclear@27 89 int i, palsz = palm_palette_size();
nuclear@27 90 struct palm_color *pal = palm_palette();
nuclear@27 91
nuclear@27 92 for(i=0; i<palsz; i++) {
nuclear@27 93 set_pal_entry(i, pal[i].r, pal[i].g, pal[i].b);
nuclear@27 94 }
nuclear@27 95 }
nuclear@27 96
nuclear@27 97 mgl_color_range(palm_color_range() - 1);
nuclear@27 98 return 0;
nuclear@27 99 }
nuclear@27 100
nuclear@27 101 static void shutdown(void)
nuclear@27 102 {
nuclear@27 103 mgl_free();
nuclear@27 104 set_video_mode(3);
nuclear@27 105 }
nuclear@27 106
nuclear@27 107 static void redraw(void)
nuclear@27 108 {
nuclear@27 109 mgl_clear(0);
nuclear@27 110 mgl_clear_depth();
nuclear@27 111
nuclear@27 112 mgl_matrix_mode(MGL_MODELVIEW);
nuclear@27 113 mgl_load_identity();
nuclear@27 114 mgl_rotate(cam_phi, 1, 0, 0);
nuclear@27 115 mgl_rotate(cam_theta, 0, 1, 0);
nuclear@27 116 mgl_translate(-cam_x, -cam_y, -cam_z);
nuclear@27 117 mgl_translate(0, -2.0, 0);
nuclear@27 118
nuclear@27 119 mgl_light_intensity(0, 1.0);
nuclear@27 120 mgl_light_position(0, 0, 5, 0, 1);
nuclear@27 121
nuclear@27 122 /*mgl_torus(0.75, 0.25, 16, 8);*/
nuclear@27 123 scn_render(&scn);
nuclear@27 124
nuclear@27 125 copy_frame(fbuf);
nuclear@27 126 }
nuclear@27 127
nuclear@27 128 #define DEG_TO_RAD(x) (M_PI * (x) / 180.0)
nuclear@27 129 void cam_move(float dx, float dy)
nuclear@27 130 {
nuclear@27 131 float angle = DEG_TO_RAD(cam_theta);
nuclear@27 132 cam_x += cos(angle) * dx + sin(angle) * dy;
nuclear@27 133 cam_z -= -sin(angle) * dx + cos(angle) * dy;
nuclear@27 134 }
nuclear@27 135
nuclear@27 136 static int proc_events(void)
nuclear@27 137 {
nuclear@27 138 static int prev_mx, prev_my, prev_bnmask;
nuclear@27 139 int mx, my, bnmask;
nuclear@27 140
nuclear@27 141 if(kbhit()) {
nuclear@27 142 if(keyb(getch()) == 0) {
nuclear@27 143 return 0;
nuclear@27 144 }
nuclear@27 145 }
nuclear@27 146
nuclear@27 147 bnmask = read_mouse(&mx, &my);
nuclear@27 148 if(bnmask != prev_bnmask) {
nuclear@27 149 mouse_button(bnmask, mx, my);
nuclear@27 150 prev_bnmask = bnmask;
nuclear@27 151 }
nuclear@27 152 if(mx != prev_mx || my != prev_my) {
nuclear@27 153 mouse_motion(mx, my);
nuclear@27 154 prev_mx = mx;
nuclear@27 155 prev_my = my;
nuclear@27 156 }
nuclear@27 157 return 1;
nuclear@27 158 }
nuclear@27 159
nuclear@27 160 static int keyb(int key)
nuclear@27 161 {
nuclear@27 162 switch(key) {
nuclear@27 163 case 27:
nuclear@27 164 return 0;
nuclear@27 165
nuclear@27 166 case 'w':
nuclear@27 167 cam_move(0, walk_speed);
nuclear@27 168 break;
nuclear@27 169
nuclear@27 170 case 's':
nuclear@27 171 cam_move(0, -walk_speed);
nuclear@27 172 break;
nuclear@27 173
nuclear@27 174 case 'a':
nuclear@27 175 cam_move(-walk_speed, 0);
nuclear@27 176 break;
nuclear@27 177
nuclear@27 178 case 'd':
nuclear@27 179 cam_move(walk_speed, 0);
nuclear@27 180 break;
nuclear@27 181
nuclear@27 182 case '`':
nuclear@27 183 mouse_look = !mouse_look;
nuclear@27 184 break;
nuclear@27 185
nuclear@27 186 default:
nuclear@27 187 break;
nuclear@27 188 }
nuclear@27 189 return 1;
nuclear@27 190 }
nuclear@27 191
nuclear@27 192 static int bnstate;
nuclear@27 193 static void mouse_button(int bn, int x, int y)
nuclear@27 194 {
nuclear@27 195 bnstate = bn;
nuclear@27 196 }
nuclear@27 197
nuclear@27 198 static void mouse_motion(int x, int y)
nuclear@27 199 {
nuclear@27 200 static int prev_x, prev_y;
nuclear@27 201 int dx = x - prev_x;
nuclear@27 202 int dy = y - prev_y;
nuclear@27 203 prev_x = x;
nuclear@27 204 prev_y = y;
nuclear@27 205
nuclear@27 206 if(mouse_look || bnstate) {
nuclear@27 207 cam_theta += dx * look_speed;
nuclear@27 208 cam_phi += dy * look_speed;
nuclear@27 209
nuclear@27 210 if(cam_phi < -90) {
nuclear@27 211 cam_phi = -90;
nuclear@27 212 }
nuclear@27 213 if(cam_phi > 90) {
nuclear@27 214 cam_phi = 90;
nuclear@27 215 }
nuclear@27 216 }
nuclear@27 217 }
nuclear@27 218
nuclear@27 219 static void sighandler(int s)
nuclear@27 220 {
nuclear@27 221 set_video_mode(3);
nuclear@27 222
nuclear@27 223 switch(s) {
nuclear@27 224 case SIGABRT:
nuclear@27 225 fprintf(stderr, "abort\n");
nuclear@27 226 break;
nuclear@27 227
nuclear@27 228 case SIGILL:
nuclear@27 229 fprintf(stderr, "illegal operation\n");
nuclear@27 230 break;
nuclear@27 231
nuclear@27 232 case SIGSEGV:
nuclear@27 233 fprintf(stderr, "segmentation fault\n");
nuclear@27 234 break;
nuclear@27 235
nuclear@27 236 case SIGINT:
nuclear@27 237 fprintf(stderr, "interrupted\n");
nuclear@27 238 break;
nuclear@27 239
nuclear@27 240 case SIGFPE:
nuclear@27 241 fprintf(stderr, "floating point exception\n");
nuclear@27 242 break;
nuclear@27 243
nuclear@27 244 default:
nuclear@27 245 fprintf(stderr, "unexpected signal\n");
nuclear@27 246 }
nuclear@27 247
nuclear@27 248 exit(1);
nuclear@27 249 }