vrfileman

changeset 3:2cbf85690e03

more stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Feb 2015 20:56:16 +0200
parents 282da6123fd4
children 85e26116ba5a
files src/app.cc src/timer.cc src/timer.h src/user.cc src/user.h
diffstat 5 files changed, 227 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- a/src/app.cc	Sun Feb 01 12:51:10 2015 +0200
     1.2 +++ b/src/app.cc	Sun Feb 01 20:56:16 2015 +0200
     1.3 @@ -2,12 +2,18 @@
     1.4  #include "opengl.h"
     1.5  #include "app.h"
     1.6  #include "user.h"
     1.7 +#include "timer.h"
     1.8  
     1.9  static void draw_grid(int num, float sep);
    1.10  
    1.11  static User user;
    1.12  static float eye_level = 1.6;
    1.13  
    1.14 +static int win_width, win_height;
    1.15 +static bool keystate[256];
    1.16 +static bool bnstate[16];
    1.17 +static int prev_x, prev_y, click_x, click_y;
    1.18 +
    1.19  bool app_init()
    1.20  {
    1.21  	if(!init_opengl()) {
    1.22 @@ -20,8 +26,40 @@
    1.23  {
    1.24  }
    1.25  
    1.26 +static void update(unsigned int msec)
    1.27 +{
    1.28 +	static unsigned int prev_upd;
    1.29 +	float dt = (float)(msec - prev_upd) / 1000.0f;
    1.30 +	prev_upd = msec;
    1.31 +
    1.32 +	static const float walk_speed = 1.0;
    1.33 +	float fwd = 0.0f, right = 0.0f;
    1.34 +
    1.35 +	if(keystate['w'] || keystate['W']) {
    1.36 +		fwd += walk_speed * dt;
    1.37 +		redisplay();
    1.38 +	}
    1.39 +	if(keystate['s'] || keystate['S']) {
    1.40 +		fwd -= walk_speed * dt;
    1.41 +		redisplay();
    1.42 +	}
    1.43 +	if(keystate['d'] || keystate['D']) {
    1.44 +		right += walk_speed * 0.5 * dt;
    1.45 +		redisplay();
    1.46 +	}
    1.47 +	if(keystate['a'] || keystate['A']) {
    1.48 +		right -= walk_speed * 0.5 * dt;
    1.49 +		redisplay();
    1.50 +	}
    1.51 +
    1.52 +	user.posrot.move(fwd, right);
    1.53 +}
    1.54 +
    1.55  void app_display()
    1.56  {
    1.57 +	unsigned int msec = get_time_msec();
    1.58 +	update(msec);
    1.59 +
    1.60  	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.61  
    1.62  	glMatrixMode(GL_MODELVIEW);
    1.63 @@ -40,6 +78,9 @@
    1.64  
    1.65  void app_reshape(int x, int y)
    1.66  {
    1.67 +	win_width = x;
    1.68 +	win_height = y;
    1.69 +
    1.70  	glViewport(0, 0, x, y);
    1.71  
    1.72  	glMatrixMode(GL_PROJECTION);
    1.73 @@ -55,14 +96,39 @@
    1.74  			quit();
    1.75  		}
    1.76  	}
    1.77 +
    1.78 +	if(key < 256) {
    1.79 +		keystate[key] = pressed;
    1.80 +	}
    1.81 +
    1.82 +	redisplay();
    1.83  }
    1.84  
    1.85  void app_mouse_button(int bn, bool pressed, int x, int y)
    1.86  {
    1.87 +	bnstate[bn] = pressed;
    1.88 +	prev_x = x;
    1.89 +	prev_y = y;
    1.90 +
    1.91 +	if(bn == 0 && pressed) {
    1.92 +		click_x = x;
    1.93 +		click_y = y;
    1.94 +	}
    1.95  }
    1.96  
    1.97  void app_mouse_motion(int x, int y)
    1.98  {
    1.99 +	static const float rot_speed = 10.0;
   1.100 +
   1.101 +	float dx = (x - prev_x) / win_width;
   1.102 +	float dy = (y - prev_y) / win_height;
   1.103 +	prev_x = x;
   1.104 +	prev_y = y;
   1.105 +
   1.106 +	if(bnstate[0]) {
   1.107 +		user.posrot.rotate(dx * rot_speed, dy * rot_speed);
   1.108 +		redisplay();
   1.109 +	}
   1.110  }
   1.111  
   1.112  void app_sball_motion(float x, float y, float z)
   1.113 @@ -83,6 +149,7 @@
   1.114  	float max_dist = hnum * sep;
   1.115  
   1.116  	glBegin(GL_LINES);
   1.117 +	glColor3f(0.4, 0.4, 0.4);
   1.118  	glVertex3f(0, 0, -max_dist);
   1.119  	glVertex3f(0, 0, max_dist);
   1.120  	glVertex3f(-max_dist, 0, 0);
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/timer.cc	Sun Feb 01 20:56:16 2015 +0200
     2.3 @@ -0,0 +1,118 @@
     2.4 +#include "timer.h"
     2.5 +
     2.6 +#if defined(__APPLE__) && !defined(__unix__)
     2.7 +#define __unix__
     2.8 +#endif
     2.9 +
    2.10 +#ifdef __unix__
    2.11 +#include <time.h>
    2.12 +#include <unistd.h>
    2.13 +#include <sys/time.h>
    2.14 +
    2.15 +#ifdef CLOCK_MONOTONIC
    2.16 +unsigned long get_time_msec(void)
    2.17 +{
    2.18 +	struct timespec ts;
    2.19 +	static struct timespec ts0;
    2.20 +
    2.21 +	clock_gettime(CLOCK_MONOTONIC, &ts);
    2.22 +	if(ts0.tv_sec == 0 && ts0.tv_nsec == 0) {
    2.23 +		ts0 = ts;
    2.24 +		return 0;
    2.25 +	}
    2.26 +	return (ts.tv_sec - ts0.tv_sec) * 1000 + (ts.tv_nsec - ts0.tv_nsec) / 1000000;
    2.27 +}
    2.28 +#else	/* no fancy POSIX clocks, fallback to good'ol gettimeofday */
    2.29 +unsigned long get_time_msec(void)
    2.30 +{
    2.31 +	struct timeval tv;
    2.32 +	static struct timeval tv0;
    2.33 +
    2.34 +	gettimeofday(&tv, 0);
    2.35 +	if(tv0.tv_sec == 0 && tv0.tv_usec == 0) {
    2.36 +		tv0 = tv;
    2.37 +		return 0;
    2.38 +	}
    2.39 +	return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
    2.40 +}
    2.41 +#endif	/* !posix clock */
    2.42 +
    2.43 +void sleep_msec(unsigned long msec)
    2.44 +{
    2.45 +	usleep(msec * 1000);
    2.46 +}
    2.47 +#endif
    2.48 +
    2.49 +#ifdef WIN32
    2.50 +#include <windows.h>
    2.51 +#pragma comment(lib, "winmm.lib")
    2.52 +
    2.53 +unsigned long get_time_msec(void)
    2.54 +{
    2.55 +	return timeGetTime();
    2.56 +}
    2.57 +
    2.58 +void sleep_msec(unsigned long msec)
    2.59 +{
    2.60 +	Sleep(msec);
    2.61 +}
    2.62 +#endif
    2.63 +
    2.64 +double get_time_sec(void)
    2.65 +{
    2.66 +	return get_time_msec() / 1000.0f;
    2.67 +}
    2.68 +
    2.69 +void sleep_sec(double sec)
    2.70 +{
    2.71 +	if(sec > 0.0f) {
    2.72 +		sleep_msec(sec * 1000.0f);
    2.73 +	}
    2.74 +}
    2.75 +
    2.76 +
    2.77 +Timer::Timer()
    2.78 +{
    2.79 +	reset();
    2.80 +}
    2.81 +
    2.82 +void Timer::reset()
    2.83 +{
    2.84 +	pause_time = 0;
    2.85 +	start_time = get_time_msec();
    2.86 +}
    2.87 +
    2.88 +void Timer::start()
    2.89 +{
    2.90 +	if(!is_running()) {
    2.91 +		// resuming
    2.92 +		start_time += get_time_msec() - pause_time;
    2.93 +		pause_time = 0;
    2.94 +	}
    2.95 +}
    2.96 +
    2.97 +void Timer::stop()
    2.98 +{
    2.99 +	if(is_running()) {
   2.100 +		pause_time = get_time_msec();
   2.101 +	}
   2.102 +}
   2.103 +
   2.104 +bool Timer::is_running() const
   2.105 +{
   2.106 +	return pause_time == 0;
   2.107 +}
   2.108 +
   2.109 +unsigned long Timer::get_msec() const
   2.110 +{
   2.111 +	if(!is_running()) {
   2.112 +		// in paused state...
   2.113 +		return pause_time - start_time;
   2.114 +	}
   2.115 +	return get_time_msec() - start_time;
   2.116 +}
   2.117 +
   2.118 +double Timer::get_sec() const
   2.119 +{
   2.120 +	return (double)get_msec() / 1000.0;
   2.121 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/timer.h	Sun Feb 01 20:56:16 2015 +0200
     3.3 @@ -0,0 +1,29 @@
     3.4 +#ifndef TIMER_H_
     3.5 +#define TIMER_H_
     3.6 +
     3.7 +unsigned long get_time_msec(void);
     3.8 +void sleep_msec(unsigned long msec);
     3.9 +
    3.10 +double get_time_sec(void);
    3.11 +void sleep_sec(double sec);
    3.12 +
    3.13 +
    3.14 +class Timer {
    3.15 +private:
    3.16 +	unsigned long start_time, pause_time;
    3.17 +
    3.18 +public:
    3.19 +	Timer();
    3.20 +
    3.21 +	void reset();
    3.22 +
    3.23 +	void start();
    3.24 +	void stop();
    3.25 +
    3.26 +	bool is_running() const;
    3.27 +
    3.28 +	unsigned long get_msec() const;
    3.29 +	double get_sec() const;
    3.30 +};
    3.31 +
    3.32 +#endif	// TIMER_H_
     4.1 --- a/src/user.cc	Sun Feb 01 12:51:10 2015 +0200
     4.2 +++ b/src/user.cc	Sun Feb 01 20:56:16 2015 +0200
     4.3 @@ -2,16 +2,28 @@
     4.4  
     4.5  void PosRot::move(float dfwd, float dright, float dup)
     4.6  {
     4.7 +	Vector3 dir = Vector3(dright, dup, dfwd);
     4.8 +	dir.transform(rot);
     4.9 +	pos += dir;
    4.10  }
    4.11  
    4.12  void PosRot::rotate(float dhoriz, float dvert)
    4.13  {
    4.14 +	rot.rotate(Vector3(1, 0, 0), dvert);
    4.15 +	rot.rotate(Vector3(0, 1, 0), dhoriz);
    4.16  }
    4.17  
    4.18  void PosRot::calc_matrix(Matrix4x4 *res) const
    4.19  {
    4.20 +	Matrix4x4 rmat = rot.get_rotation_matrix();
    4.21 +	Matrix4x4 tmat;
    4.22 +	tmat.set_translation(pos);
    4.23 +
    4.24 +	*res = tmat * rmat;
    4.25  }
    4.26  
    4.27  void PosRot::calc_inv_matrix(Matrix4x4 *res) const
    4.28  {
    4.29 +	calc_matrix(res);
    4.30 +	*res = res->inverse();
    4.31  }
     5.1 --- a/src/user.h	Sun Feb 01 12:51:10 2015 +0200
     5.2 +++ b/src/user.h	Sun Feb 01 20:56:16 2015 +0200
     5.3 @@ -7,7 +7,7 @@
     5.4  	Vector3 pos;
     5.5  	Quaternion rot;
     5.6  
     5.7 -	void move(float dfwd, float dright, float dup);
     5.8 +	void move(float dfwd, float dright, float dup = 0.0f);
     5.9  	void rotate(float dhoriz, float dvert);
    5.10  
    5.11  	void calc_matrix(Matrix4x4 *res) const;