deepstone

annotate firstp/firstp.c @ 14:be61704c4cc8

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