deepstone

diff src/main.c @ 34:c6406e4aa0fb

better input, fixed emulated code to work again
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 23 Sep 2013 05:58:24 +0300
parents 03a0b307706a
children 17a5107b6fa4
line diff
     1.1 --- a/src/main.c	Mon Sep 23 04:34:43 2013 +0300
     1.2 +++ b/src/main.c	Mon Sep 23 05:58:24 2013 +0300
     1.3 @@ -16,9 +16,9 @@
     1.4  
     1.5  static int init(void);
     1.6  static void shutdown(void);
     1.7 +static void update(unsigned long dtmsec);
     1.8  static void redraw(void);
     1.9  static int proc_events(void);
    1.10 -static int proc_keyb_input(void);
    1.11  static void mouse_button(int bn, int x, int y);
    1.12  static void mouse_motion(int x, int y);
    1.13  static void sighandler(int s);
    1.14 @@ -27,14 +27,10 @@
    1.15  static float cam_x, cam_y, cam_z;
    1.16  static float cam_theta, cam_phi;
    1.17  
    1.18 -static float walk_speed = 0.5;
    1.19 +static float walk_speed = 6.0;
    1.20  static float look_speed = 1.0;
    1.21  
    1.22 -#ifdef __DOS__
    1.23  static int mouse_look = 1;
    1.24 -#else
    1.25 -static int mouse_look = 0;
    1.26 -#endif
    1.27  
    1.28  static void *fbuf;
    1.29  static struct scene scn;
    1.30 @@ -42,11 +38,22 @@
    1.31  
    1.32  int main(void)
    1.33  {
    1.34 +	unsigned long prev_msec = 0;
    1.35 +
    1.36  	if(init() == -1) {
    1.37  		return 1;
    1.38  	}
    1.39  
    1.40 -	while(proc_events()) {
    1.41 +	for(;;) {
    1.42 +		unsigned long msec = get_msec();
    1.43 +		unsigned long dt = msec - prev_msec;
    1.44 +		prev_msec = msec;
    1.45 +
    1.46 +		if(!proc_events()) {
    1.47 +			break;
    1.48 +		}
    1.49 +
    1.50 +		update(dt);
    1.51  		redraw();
    1.52  	}
    1.53  
    1.54 @@ -57,11 +64,10 @@
    1.55  
    1.56  static int init(void)
    1.57  {
    1.58 -	float vfov, hfov, aspect;
    1.59 +	float vfov, aspect;
    1.60  
    1.61  	aspect = 320.0 / 200.0;
    1.62  	vfov = 60.0;
    1.63 -	hfov = vfov * aspect;
    1.64  
    1.65  	init_timer(100);
    1.66  	kb_init(16); /* 16 characters input buffer */
    1.67 @@ -129,6 +135,33 @@
    1.68  	kb_shutdown();
    1.69  }
    1.70  
    1.71 +
    1.72 +#define DEG_TO_RAD(x)	(M_PI * (x) / 180.0)
    1.73 +void cam_move(float dx, float dy)
    1.74 +{
    1.75 +	float angle = DEG_TO_RAD(cam_theta);
    1.76 +	cam_x += cos(angle) * dx + sin(angle) * dy;
    1.77 +	cam_z -= -sin(angle) * dx + cos(angle) * dy;
    1.78 +}
    1.79 +
    1.80 +static void update(unsigned long dtmsec)
    1.81 +{
    1.82 +	float dt = (float)dtmsec / 1000.0f;
    1.83 +	float offs = walk_speed * dt;
    1.84 +
    1.85 +	if(kb_isdown('w') || kb_isdown('W'))
    1.86 +		cam_move(0, offs);
    1.87 +
    1.88 +	if(kb_isdown('s') || kb_isdown('S'))
    1.89 +		cam_move(0, -offs);
    1.90 +
    1.91 +	if(kb_isdown('a') || kb_isdown('A'))
    1.92 +		cam_move(-offs, 0);
    1.93 +
    1.94 +	if(kb_isdown('d') || kb_isdown('D'))
    1.95 +		cam_move(offs, 0);
    1.96 +}
    1.97 +
    1.98  static void redraw(void)
    1.99  {
   1.100  	mgl_clear(0);
   1.101 @@ -150,39 +183,11 @@
   1.102  	copy_frame(fbuf);
   1.103  }
   1.104  
   1.105 -#define DEG_TO_RAD(x)	(M_PI * (x) / 180.0)
   1.106 -void cam_move(float dx, float dy)
   1.107 -{
   1.108 -	float angle = DEG_TO_RAD(cam_theta);
   1.109 -	cam_x += cos(angle) * dx + sin(angle) * dy;
   1.110 -	cam_z -= -sin(angle) * dx + cos(angle) * dy;
   1.111 -}
   1.112 -
   1.113  static int proc_events(void)
   1.114  {
   1.115  	static int prev_mx, prev_my, prev_bnmask;
   1.116 -	int mx, my, bnmask, key;
   1.117 +	int mx, my, bnmask;
   1.118  
   1.119 -	if(!proc_keyb_input()) {
   1.120 -		return 0;
   1.121 -	}
   1.122 -
   1.123 -	bnmask = read_mouse(&mx, &my);
   1.124 -	if(bnmask != prev_bnmask) {
   1.125 -		mouse_button(bnmask, mx, my);
   1.126 -		prev_bnmask = bnmask;
   1.127 -	}
   1.128 -	if(mx != prev_mx || my != prev_my) {
   1.129 -		mouse_motion(mx, my);
   1.130 -		prev_mx = mx;
   1.131 -		prev_my = my;
   1.132 -	}
   1.133 -	return 1;
   1.134 -}
   1.135 -
   1.136 -static int proc_keyb_input(void)
   1.137 -{
   1.138 -	/* first examine all keypresses and handle non-movement keys */
   1.139  	int key;
   1.140  	while((key = kb_getkey()) != -1) {
   1.141  		switch(key) {
   1.142 @@ -198,19 +203,16 @@
   1.143  		}
   1.144  	}
   1.145  
   1.146 -	/* for the movement keys we just care if they are pressed at the moment */
   1.147 -	if(kb_isdown('w') || kb_isdown('W'))
   1.148 -		cam_move(0, walk_speed);
   1.149 -
   1.150 -	if(kb_isdown('s') || kb_isdown('S'))
   1.151 -		cam_move(0, -walk_speed);
   1.152 -
   1.153 -	if(kb_isdown('a') || kb_isdown('A'))
   1.154 -		cam_move(-walk_speed, 0);
   1.155 -
   1.156 -	if(kb_isdown('d') || kb_isdown('D'))
   1.157 -		cam_move(walk_speed, 0);
   1.158 -
   1.159 +	bnmask = read_mouse(&mx, &my);
   1.160 +	if(bnmask != prev_bnmask) {
   1.161 +		mouse_button(bnmask, mx, my);
   1.162 +		prev_bnmask = bnmask;
   1.163 +	}
   1.164 +	if(mx != prev_mx || my != prev_my) {
   1.165 +		mouse_motion(mx, my);
   1.166 +		prev_mx = mx;
   1.167 +		prev_my = my;
   1.168 +	}
   1.169  	return 1;
   1.170  }
   1.171