deepstone

diff 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
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main.c	Sun Sep 22 02:47:46 2013 +0300
     1.3 @@ -0,0 +1,249 @@
     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 "wvga.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;
    1.30 +static float cam_theta, cam_phi;
    1.31 +
    1.32 +static float walk_speed = 0.5;
    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_pal_entry(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_phi, 1, 0, 0);
   1.118 +	mgl_rotate(cam_theta, 0, 1, 0);
   1.119 +	mgl_translate(-cam_x, -cam_y, -cam_z);
   1.120 +	mgl_translate(0, -2.0, 0);
   1.121 +
   1.122 +	mgl_light_intensity(0, 1.0);
   1.123 +	mgl_light_position(0, 0, 5, 0, 1);
   1.124 +
   1.125 +	/*mgl_torus(0.75, 0.25, 16, 8);*/
   1.126 +	scn_render(&scn);
   1.127 +
   1.128 +	copy_frame(fbuf);
   1.129 +}
   1.130 +
   1.131 +#define DEG_TO_RAD(x)	(M_PI * (x) / 180.0)
   1.132 +void cam_move(float dx, float dy)
   1.133 +{
   1.134 +	float angle = DEG_TO_RAD(cam_theta);
   1.135 +	cam_x += cos(angle) * dx + sin(angle) * dy;
   1.136 +	cam_z -= -sin(angle) * dx + cos(angle) * dy;
   1.137 +}
   1.138 +
   1.139 +static int proc_events(void)
   1.140 +{
   1.141 +	static int prev_mx, prev_my, prev_bnmask;
   1.142 +	int mx, my, bnmask;
   1.143 +
   1.144 +	if(kbhit()) {
   1.145 +		if(keyb(getch()) == 0) {
   1.146 +			return 0;
   1.147 +		}
   1.148 +	}
   1.149 +
   1.150 +	bnmask = read_mouse(&mx, &my);
   1.151 +	if(bnmask != prev_bnmask) {
   1.152 +		mouse_button(bnmask, mx, my);
   1.153 +		prev_bnmask = bnmask;
   1.154 +	}
   1.155 +	if(mx != prev_mx || my != prev_my) {
   1.156 +		mouse_motion(mx, my);
   1.157 +		prev_mx = mx;
   1.158 +		prev_my = my;
   1.159 +	}
   1.160 +	return 1;
   1.161 +}
   1.162 +
   1.163 +static int keyb(int key)
   1.164 +{
   1.165 +	switch(key) {
   1.166 +	case 27:
   1.167 +		return 0;
   1.168 +
   1.169 +	case 'w':
   1.170 +		cam_move(0, walk_speed);
   1.171 +		break;
   1.172 +
   1.173 +	case 's':
   1.174 +		cam_move(0, -walk_speed);
   1.175 +		break;
   1.176 +
   1.177 +	case 'a':
   1.178 +		cam_move(-walk_speed, 0);
   1.179 +		break;
   1.180 +
   1.181 +	case 'd':
   1.182 +		cam_move(walk_speed, 0);
   1.183 +		break;
   1.184 +
   1.185 +	case '`':
   1.186 +		mouse_look = !mouse_look;
   1.187 +		break;
   1.188 +
   1.189 +	default:
   1.190 +		break;
   1.191 +	}
   1.192 +	return 1;
   1.193 +}
   1.194 +
   1.195 +static int bnstate;
   1.196 +static void mouse_button(int bn, int x, int y)
   1.197 +{
   1.198 +	bnstate = bn;
   1.199 +}
   1.200 +
   1.201 +static void mouse_motion(int x, int y)
   1.202 +{
   1.203 +	static int prev_x, prev_y;
   1.204 +	int dx = x - prev_x;
   1.205 +	int dy = y - prev_y;
   1.206 +	prev_x = x;
   1.207 +	prev_y = y;
   1.208 +
   1.209 +	if(mouse_look || bnstate) {
   1.210 +		cam_theta += dx * look_speed;
   1.211 +		cam_phi += dy * look_speed;
   1.212 +
   1.213 +		if(cam_phi < -90) {
   1.214 +			cam_phi = -90;
   1.215 +		}
   1.216 +		if(cam_phi > 90) {
   1.217 +			cam_phi = 90;
   1.218 +		}
   1.219 +	}
   1.220 +}
   1.221 +
   1.222 +static void sighandler(int s)
   1.223 +{
   1.224 +	set_video_mode(3);
   1.225 +
   1.226 +	switch(s) {
   1.227 +	case SIGABRT:
   1.228 +		fprintf(stderr, "abort\n");
   1.229 +		break;
   1.230 +
   1.231 +	case SIGILL:
   1.232 +		fprintf(stderr, "illegal operation\n");
   1.233 +		break;
   1.234 +
   1.235 +	case SIGSEGV:
   1.236 +		fprintf(stderr, "segmentation fault\n");
   1.237 +		break;
   1.238 +
   1.239 +	case SIGINT:
   1.240 +		fprintf(stderr, "interrupted\n");
   1.241 +		break;
   1.242 +
   1.243 +	case SIGFPE:
   1.244 +		fprintf(stderr, "floating point exception\n");
   1.245 +		break;
   1.246 +
   1.247 +	default:
   1.248 +		fprintf(stderr, "unexpected signal\n");
   1.249 +	}
   1.250 +
   1.251 +	exit(1);
   1.252 +}