deepstone

diff 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
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/firstp/firstp.c	Tue Nov 29 07:22:49 2011 +0200
     1.3 @@ -0,0 +1,251 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <math.h>
     1.7 +#include <signal.h>
     1.8 +#include <conio.h>
     1.9 +#include "vga.h"
    1.10 +#include "mingl.h"
    1.11 +#include "timer.h"
    1.12 +#include "mouse.h"
    1.13 +#include "texture.h"
    1.14 +#include "palman.h"
    1.15 +#include "scene.h"
    1.16 +
    1.17 +#define DEG2RAD(x)	(M_PI * (x) / 180.0)
    1.18 +
    1.19 +static int init(void);
    1.20 +static void shutdown(void);
    1.21 +static void redraw(void);
    1.22 +static int proc_events(void);
    1.23 +static int keyb(int key);
    1.24 +static void mouse_button(int bn, int x, int y);
    1.25 +static void mouse_motion(int x, int y);
    1.26 +static void sighandler(int s);
    1.27 +
    1.28 +
    1.29 +static float cam_x, cam_y, cam_z = 10;
    1.30 +static float cam_theta, cam_phi;
    1.31 +
    1.32 +static float walk_speed = 0.1;
    1.33 +static float look_speed = 1.0;
    1.34 +static int mouse_look = 0;
    1.35 +
    1.36 +static void *fbuf;
    1.37 +static struct scene scn;
    1.38 +
    1.39 +
    1.40 +int main(void)
    1.41 +{
    1.42 +	if(init() == -1) {
    1.43 +		return 1;
    1.44 +	}
    1.45 +
    1.46 +	while(proc_events()) {
    1.47 +		redraw();
    1.48 +	}
    1.49 +
    1.50 +	shutdown();
    1.51 +	return 0;
    1.52 +}
    1.53 +
    1.54 +
    1.55 +static int init(void)
    1.56 +{
    1.57 +	init_timer(100);
    1.58 +
    1.59 +	set_video_mode(0x13);
    1.60 +
    1.61 +	signal(SIGINT, sighandler);
    1.62 +	signal(SIGSEGV, sighandler);
    1.63 +	signal(SIGFPE, sighandler);
    1.64 +	signal(SIGILL, sighandler);
    1.65 +	signal(SIGABRT, sighandler);
    1.66 +
    1.67 +	if(mgl_init(320, 200) == -1) {
    1.68 +		fprintf(stderr, "mgl init failed\n");
    1.69 +		return -1;
    1.70 +	}
    1.71 +	fbuf = mgl_framebuffer();
    1.72 +
    1.73 +	mgl_enable(MGL_CULL_FACE);
    1.74 +	mgl_enable(MGL_SMOOTH);
    1.75 +	mgl_enable(MGL_LIGHTING);
    1.76 +	mgl_enable(MGL_DEPTH_TEST);
    1.77 +
    1.78 +	mgl_matrix_mode(MGL_PROJECTION);
    1.79 +	mgl_load_identity();
    1.80 +	mgl_perspective(45.0, 320.0 / 200.0, 0.5, 200.0);
    1.81 +
    1.82 +	/* setup palette */
    1.83 +	palm_add_color(255, 255, 255);
    1.84 +
    1.85 +	scn_init(&scn);
    1.86 +	if(scn_load(&scn, "data/hall.obj") == -1) {
    1.87 +		return -1;
    1.88 +	}
    1.89 +
    1.90 +	palm_build();
    1.91 +	{
    1.92 +		int i, palsz = palm_palette_size();
    1.93 +		struct palm_color *pal = palm_palette();
    1.94 +
    1.95 +		for(i=0; i<palsz; i++) {
    1.96 +			set_palette(i, pal[i].r, pal[i].g, pal[i].b);
    1.97 +		}
    1.98 +	}
    1.99 +
   1.100 +	mgl_color_range(palm_color_range() - 1);
   1.101 +	return 0;
   1.102 +}
   1.103 +
   1.104 +static void shutdown(void)
   1.105 +{
   1.106 +	mgl_free();
   1.107 +	set_video_mode(3);
   1.108 +}
   1.109 +
   1.110 +static void redraw(void)
   1.111 +{
   1.112 +	mgl_clear(0);
   1.113 +	mgl_clear_depth();
   1.114 +
   1.115 +	mgl_matrix_mode(MGL_MODELVIEW);
   1.116 +	mgl_load_identity();
   1.117 +	mgl_rotate(cam_theta, 0, 1, 0);
   1.118 +	mgl_rotate(cam_phi, 1, 0, 0);
   1.119 +	mgl_translate(-cam_x, -cam_y, -cam_z);
   1.120 +
   1.121 +	mgl_light_intensity(0, 1.0);
   1.122 +	mgl_light_position(0, 0, 5, 0, 1);
   1.123 +
   1.124 +	/*mgl_torus(0.75, 0.25, 16, 8);*/
   1.125 +	scn_render(&scn);
   1.126 +
   1.127 +	copy_frame(fbuf);
   1.128 +}
   1.129 +
   1.130 +static int proc_events(void)
   1.131 +{
   1.132 +	static int prev_mx, prev_my, prev_bnmask;
   1.133 +	int mx, my, bnmask;
   1.134 +
   1.135 +	if(kbhit()) {
   1.136 +		if(keyb(getch()) == 0) {
   1.137 +			return 0;
   1.138 +		}
   1.139 +	}
   1.140 +
   1.141 +	bnmask = read_mouse(&mx, &my);
   1.142 +	if(bnmask != prev_bnmask) {
   1.143 +		mouse_button(bnmask, mx, my);
   1.144 +		prev_bnmask = bnmask;
   1.145 +	}
   1.146 +	if(mx != prev_mx || my != prev_my) {
   1.147 +		mouse_motion(mx, my);
   1.148 +		prev_mx = mx;
   1.149 +		prev_my = my;
   1.150 +	}
   1.151 +	return 1;
   1.152 +}
   1.153 +
   1.154 +static int keyb(int key)
   1.155 +{
   1.156 +	float dir_x, dir_y, right_x, right_y;
   1.157 +
   1.158 +	dir_x = sin(DEG2RAD(cam_theta)) * walk_speed;
   1.159 +	dir_y = cos(DEG2RAD(cam_theta)) * walk_speed;
   1.160 +	right_x = dir_y;
   1.161 +	right_y = -dir_x;
   1.162 +
   1.163 +	switch(key) {
   1.164 +	case 27:
   1.165 +		return 0;
   1.166 +
   1.167 +	case 'w':
   1.168 +		cam_x += dir_x;
   1.169 +		cam_z -= dir_y;
   1.170 +		break;
   1.171 +
   1.172 +	case 's':
   1.173 +		cam_x -= dir_x;
   1.174 +		cam_z += dir_y;
   1.175 +		break;
   1.176 +
   1.177 +	case 'a':
   1.178 +		cam_x -= right_x;
   1.179 +		cam_z += right_y;
   1.180 +		break;
   1.181 +
   1.182 +	case 'd':
   1.183 +		cam_x += right_x;
   1.184 +		cam_z -= right_y;
   1.185 +		break;
   1.186 +
   1.187 +	case '`':
   1.188 +		mouse_look = !mouse_look;
   1.189 +		break;
   1.190 +
   1.191 +	default:
   1.192 +		break;
   1.193 +	}
   1.194 +	return 1;
   1.195 +}
   1.196 +
   1.197 +static int bnstate;
   1.198 +static void mouse_button(int bn, int x, int y)
   1.199 +{
   1.200 +	bnstate = bn;
   1.201 +}
   1.202 +
   1.203 +static void mouse_motion(int x, int y)
   1.204 +{
   1.205 +	static int prev_x, prev_y;
   1.206 +	int dx = x - prev_x;
   1.207 +	int dy = y - prev_y;
   1.208 +	prev_x = x;
   1.209 +	prev_y = y;
   1.210 +
   1.211 +	if(mouse_look || bnstate) {
   1.212 +		cam_theta += dx * look_speed;
   1.213 +		cam_phi += dy * look_speed;
   1.214 +
   1.215 +		if(cam_phi < -90) {
   1.216 +			cam_phi = -90;
   1.217 +		}
   1.218 +		if(cam_phi > 90) {
   1.219 +			cam_phi = 90;
   1.220 +		}
   1.221 +	}
   1.222 +}
   1.223 +
   1.224 +static void sighandler(int s)
   1.225 +{
   1.226 +	set_video_mode(3);
   1.227 +
   1.228 +	switch(s) {
   1.229 +	case SIGABRT:
   1.230 +		fprintf(stderr, "abort\n");
   1.231 +		break;
   1.232 +
   1.233 +	case SIGILL:
   1.234 +		fprintf(stderr, "illegal operation\n");
   1.235 +		break;
   1.236 +
   1.237 +	case SIGSEGV:
   1.238 +		fprintf(stderr, "segmentation fault\n");
   1.239 +		break;
   1.240 +
   1.241 +	case SIGINT:
   1.242 +		fprintf(stderr, "interrupted\n");
   1.243 +		break;
   1.244 +
   1.245 +	case SIGFPE:
   1.246 +		fprintf(stderr, "floating point exception\n");
   1.247 +		break;
   1.248 +
   1.249 +	default:
   1.250 +		fprintf(stderr, "unexpected signal\n");
   1.251 +	}
   1.252 +
   1.253 +	exit(1);
   1.254 +}