deepstone

diff src/main.c @ 33:03a0b307706a

added proper keyboard handling
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 23 Sep 2013 04:34:43 +0300
parents 11d14f688485
children c6406e4aa0fb
line diff
     1.1 --- a/src/main.c	Sun Sep 22 18:37:46 2013 +0300
     1.2 +++ b/src/main.c	Mon Sep 23 04:34:43 2013 +0300
     1.3 @@ -6,6 +6,7 @@
     1.4  #include "wvga.h"
     1.5  #include "mingl.h"
     1.6  #include "timer.h"
     1.7 +#include "keyb.h"
     1.8  #include "mouse.h"
     1.9  #include "texture.h"
    1.10  #include "palman.h"
    1.11 @@ -17,7 +18,7 @@
    1.12  static void shutdown(void);
    1.13  static void redraw(void);
    1.14  static int proc_events(void);
    1.15 -static int keyb(int key);
    1.16 +static int proc_keyb_input(void);
    1.17  static void mouse_button(int bn, int x, int y);
    1.18  static void mouse_motion(int x, int y);
    1.19  static void sighandler(int s);
    1.20 @@ -28,7 +29,12 @@
    1.21  
    1.22  static float walk_speed = 0.5;
    1.23  static float look_speed = 1.0;
    1.24 +
    1.25 +#ifdef __DOS__
    1.26 +static int mouse_look = 1;
    1.27 +#else
    1.28  static int mouse_look = 0;
    1.29 +#endif
    1.30  
    1.31  static void *fbuf;
    1.32  static struct scene scn;
    1.33 @@ -58,6 +64,7 @@
    1.34  	hfov = vfov * aspect;
    1.35  
    1.36  	init_timer(100);
    1.37 +	kb_init(16); /* 16 characters input buffer */
    1.38  
    1.39  	set_video_mode(0x13);
    1.40  
    1.41 @@ -119,6 +126,7 @@
    1.42  {
    1.43  	mgl_free();
    1.44  	set_video_mode(3);
    1.45 +	kb_shutdown();
    1.46  }
    1.47  
    1.48  static void redraw(void)
    1.49 @@ -153,12 +161,10 @@
    1.50  static int proc_events(void)
    1.51  {
    1.52  	static int prev_mx, prev_my, prev_bnmask;
    1.53 -	int mx, my, bnmask;
    1.54 +	int mx, my, bnmask, key;
    1.55  
    1.56 -	if(kbhit()) {
    1.57 -		if(keyb(getch()) == 0) {
    1.58 -			return 0;
    1.59 -		}
    1.60 +	if(!proc_keyb_input()) {
    1.61 +		return 0;
    1.62  	}
    1.63  
    1.64  	bnmask = read_mouse(&mx, &my);
    1.65 @@ -174,35 +180,37 @@
    1.66  	return 1;
    1.67  }
    1.68  
    1.69 -static int keyb(int key)
    1.70 +static int proc_keyb_input(void)
    1.71  {
    1.72 -	switch(key) {
    1.73 -	case 27:
    1.74 -		return 0;
    1.75 +	/* first examine all keypresses and handle non-movement keys */
    1.76 +	int key;
    1.77 +	while((key = kb_getkey()) != -1) {
    1.78 +		switch(key) {
    1.79 +		case 27:
    1.80 +			return 0;
    1.81  
    1.82 -	case 'w':
    1.83 +		case '`':
    1.84 +			mouse_look = !mouse_look;
    1.85 +			break;
    1.86 +
    1.87 +		default:
    1.88 +			break;
    1.89 +		}
    1.90 +	}
    1.91 +
    1.92 +	/* for the movement keys we just care if they are pressed at the moment */
    1.93 +	if(kb_isdown('w') || kb_isdown('W'))
    1.94  		cam_move(0, walk_speed);
    1.95 -		break;
    1.96  
    1.97 -	case 's':
    1.98 +	if(kb_isdown('s') || kb_isdown('S'))
    1.99  		cam_move(0, -walk_speed);
   1.100 -		break;
   1.101  
   1.102 -	case 'a':
   1.103 +	if(kb_isdown('a') || kb_isdown('A'))
   1.104  		cam_move(-walk_speed, 0);
   1.105 -		break;
   1.106  
   1.107 -	case 'd':
   1.108 +	if(kb_isdown('d') || kb_isdown('D'))
   1.109  		cam_move(walk_speed, 0);
   1.110 -		break;
   1.111  
   1.112 -	case '`':
   1.113 -		mouse_look = !mouse_look;
   1.114 -		break;
   1.115 -
   1.116 -	default:
   1.117 -		break;
   1.118 -	}
   1.119  	return 1;
   1.120  }
   1.121  
   1.122 @@ -214,11 +222,22 @@
   1.123  
   1.124  static void mouse_motion(int x, int y)
   1.125  {
   1.126 -	static int prev_x, prev_y;
   1.127 +	static int prev_x = -1, prev_y;
   1.128  	int dx = x - prev_x;
   1.129  	int dy = y - prev_y;
   1.130 -	prev_x = x;
   1.131 -	prev_y = y;
   1.132 +
   1.133 +	if(mouse_look) {
   1.134 +		if(prev_x == -1) {
   1.135 +			dx = dy = 0;
   1.136 +		}
   1.137 +
   1.138 +		set_mouse(160, 100);
   1.139 +		prev_x = 160;
   1.140 +		prev_y = 100;
   1.141 +	} else {
   1.142 +		prev_x = x;
   1.143 +		prev_y = y;
   1.144 +	}
   1.145  
   1.146  	if(mouse_look || bnstate) {
   1.147  		cam_theta += dx * look_speed;