deepstone

diff src/main.c @ 38:17a5107b6fa4

- added perspective correct interpolation - added recording functionality - added video capture functionality
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 10 Mar 2014 17:24:42 +0200
parents c6406e4aa0fb
children
line diff
     1.1 --- a/src/main.c	Mon Sep 23 07:42:56 2013 +0300
     1.2 +++ b/src/main.c	Mon Mar 10 17:24:42 2014 +0200
     1.3 @@ -1,6 +1,8 @@
     1.4  #include <stdio.h>
     1.5  #include <stdlib.h>
     1.6 +#include <string.h>
     1.7  #include <math.h>
     1.8 +#include <limits.h>
     1.9  #include <signal.h>
    1.10  #include <conio.h>
    1.11  #include "wvga.h"
    1.12 @@ -11,35 +13,46 @@
    1.13  #include "texture.h"
    1.14  #include "palman.h"
    1.15  #include "scene.h"
    1.16 +#include "record.h"
    1.17  
    1.18  #define DEG2RAD(x)	(M_PI * (x) / 180.0)
    1.19  
    1.20  static int init(void);
    1.21  static void shutdown(void);
    1.22 -static void update(unsigned long dtmsec);
    1.23 +static void update(unsigned long msec, unsigned long dtmsec);
    1.24  static void redraw(void);
    1.25  static int proc_events(void);
    1.26  static void mouse_button(int bn, int x, int y);
    1.27  static void mouse_motion(int x, int y);
    1.28  static void sighandler(int s);
    1.29 +static int parse_args(int argc, char **argv);
    1.30  
    1.31  
    1.32  static float cam_x, cam_y, cam_z;
    1.33  static float cam_theta, cam_phi;
    1.34  
    1.35  static float walk_speed = 6.0;
    1.36 -static float look_speed = 1.0;
    1.37 +static float look_speed = 0.3;
    1.38  
    1.39  static int mouse_look = 1;
    1.40 +static int use_vsync;
    1.41  
    1.42  static void *fbuf;
    1.43  static struct scene scn;
    1.44  
    1.45 +#define REC_FNAME	"game.rec"
    1.46 +static int rec_playing, rec_recording;
    1.47 +static unsigned long rec_start_time;
    1.48  
    1.49 -int main(void)
    1.50 +
    1.51 +int main(int argc, char **argv)
    1.52  {
    1.53  	unsigned long prev_msec = 0;
    1.54  
    1.55 +	if(parse_args(argc, argv) == -1) {
    1.56 +		return 1;
    1.57 +	}
    1.58 +
    1.59  	if(init() == -1) {
    1.60  		return 1;
    1.61  	}
    1.62 @@ -53,7 +66,7 @@
    1.63  			break;
    1.64  		}
    1.65  
    1.66 -		update(dt);
    1.67 +		update(msec, dt);
    1.68  		redraw();
    1.69  	}
    1.70  
    1.71 @@ -144,7 +157,7 @@
    1.72  	cam_z -= -sin(angle) * dx + cos(angle) * dy;
    1.73  }
    1.74  
    1.75 -static void update(unsigned long dtmsec)
    1.76 +static void update(unsigned long msec, unsigned long dtmsec)
    1.77  {
    1.78  	float dt = (float)dtmsec / 1000.0f;
    1.79  	float offs = walk_speed * dt;
    1.80 @@ -160,6 +173,21 @@
    1.81  
    1.82  	if(kb_isdown('d') || kb_isdown('D'))
    1.83  		cam_move(offs, 0);
    1.84 +
    1.85 +	if(rec_playing) {
    1.86 +		rec_get(msec - rec_start_time, &cam_x, &cam_y, &cam_z, &cam_theta, &cam_phi);
    1.87 +	} else if(rec_recording) {
    1.88 +		static float px, py, pz, ptheta, pphi;
    1.89 +
    1.90 +		if(cam_x != px || cam_y != py || cam_z != pz || cam_theta != ptheta || cam_phi != pphi) {
    1.91 +			rec_add(msec - rec_start_time, cam_x, cam_y, cam_z, cam_theta, cam_phi);
    1.92 +			px = cam_x;
    1.93 +			py = cam_y;
    1.94 +			pz = cam_z;
    1.95 +			ptheta = cam_theta;
    1.96 +			pphi = cam_phi;
    1.97 +		}
    1.98 +	}
    1.99  }
   1.100  
   1.101  static void redraw(void)
   1.102 @@ -181,6 +209,9 @@
   1.103  	scn_render(&scn);
   1.104  
   1.105  	copy_frame(fbuf);
   1.106 +	if(use_vsync) {
   1.107 +		wait_vsync();
   1.108 +	}
   1.109  }
   1.110  
   1.111  static int proc_events(void)
   1.112 @@ -194,10 +225,66 @@
   1.113  		case 27:
   1.114  			return 0;
   1.115  
   1.116 +		case '\b':
   1.117 +			begin_capture(1);
   1.118 +			break;
   1.119 +		case '\\':
   1.120 +			{
   1.121 +				static int capturing;
   1.122 +				if(capturing) {
   1.123 +					printf("stop video capture\n");
   1.124 +					end_capture();
   1.125 +					capturing = 0;
   1.126 +				} else {
   1.127 +					printf("start video capture\n");
   1.128 +					begin_capture(INT_MAX);
   1.129 +					capturing = 1;
   1.130 +				}
   1.131 +			}
   1.132 +			break;
   1.133 +
   1.134  		case '`':
   1.135  			mouse_look = !mouse_look;
   1.136  			break;
   1.137  
   1.138 +		case 'r':
   1.139 +			if(rec_recording) {
   1.140 +				/* stop recording and save it */
   1.141 +				rec_recording = 0;
   1.142 +				printf("done, saving %s\n", REC_FNAME);
   1.143 +				rec_save(REC_FNAME);
   1.144 +			} else {
   1.145 +				printf("recording ...\n");
   1.146 +				rec_recording = 1;
   1.147 +				rec_playing = 0;
   1.148 +				rec_reset();
   1.149 +				rec_start_time = get_msec();
   1.150 +			}
   1.151 +			break;
   1.152 +
   1.153 +		case 'l':
   1.154 +			printf("loading recording %s\n", REC_FNAME);
   1.155 +			rec_recording = 0;
   1.156 +			rec_playing = 0;
   1.157 +			rec_reset();
   1.158 +			rec_load(REC_FNAME);
   1.159 +			break;
   1.160 +
   1.161 +		case ' ':
   1.162 +			if(rec_recording) {
   1.163 +				break;
   1.164 +			}
   1.165 +
   1.166 +			if(rec_playing) {
   1.167 +				printf("stop rec playback\n");
   1.168 +				rec_playing = 0;
   1.169 +			} else {
   1.170 +				printf("start rec playback\n");
   1.171 +				rec_playing = 1;
   1.172 +				rec_start_time = get_msec();
   1.173 +			}
   1.174 +			break;
   1.175 +
   1.176  		default:
   1.177  			break;
   1.178  		}
   1.179 @@ -285,3 +372,26 @@
   1.180  
   1.181  	exit(1);
   1.182  }
   1.183 +
   1.184 +static int parse_args(int argc, char **argv)
   1.185 +{
   1.186 +	int i;
   1.187 +
   1.188 +	for(i=1; i<argc; i++) {
   1.189 +		if(argv[i][0] == '-') {
   1.190 +			if(strcmp(argv[i], "-vsync") == 0) {
   1.191 +				use_vsync = 1;
   1.192 +			} else {
   1.193 +				goto invalid;
   1.194 +			}
   1.195 +		} else {
   1.196 +			goto invalid;
   1.197 +		}
   1.198 +	}
   1.199 +
   1.200 +	return 0;
   1.201 +
   1.202 +invalid:
   1.203 +	fprintf(stderr, "invalid argument: %s\n", argv[i]);
   1.204 +	return -1;
   1.205 +}