deepstone

changeset 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 1870c4ef8b76
files dosemu/dosemu.c src/main.c
diffstat 2 files changed, 166 insertions(+), 56 deletions(-) [+]
line diff
     1.1 --- a/dosemu/dosemu.c	Mon Sep 23 04:34:43 2013 +0300
     1.2 +++ b/dosemu/dosemu.c	Mon Sep 23 05:58:24 2013 +0300
     1.3 @@ -8,10 +8,18 @@
     1.4  #include "wvga.h"
     1.5  #include "conio.h"
     1.6  #include "mouse.h"
     1.7 +#include "keyb.h"
     1.8  #include "timer.h"
     1.9  
    1.10  static void proc_events(void);
    1.11  
    1.12 +static void init_sdl()
    1.13 +{
    1.14 +	if(!SDL_WasInit(SDL_INIT_EVERYTHING)) {
    1.15 +		SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
    1.16 +	}
    1.17 +}
    1.18 +
    1.19  /* ----- graphics (wvga.c implementation) ----- */
    1.20  static SDL_Surface *fbsurf;
    1.21  static int scale = 1;
    1.22 @@ -39,16 +47,17 @@
    1.23  		sdl_flags |= SDL_FULLSCREEN;
    1.24  	}
    1.25  
    1.26 +	init_sdl();
    1.27 +
    1.28  	switch(mode) {
    1.29  	case 0x13:
    1.30 -		SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
    1.31  		if(!(fbsurf = SDL_SetVideoMode(resx, resy, 8, sdl_flags))) {
    1.32  			fprintf(stderr, "failed to set video mode\n");
    1.33  			abort();
    1.34  		}
    1.35  		SDL_WM_SetCaption("Deepstone", 0);
    1.36  		/*SDL_ShowCursor(0);*/
    1.37 -		SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
    1.38 +		/*SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);*/
    1.39  		break;
    1.40  
    1.41  	case 3:
    1.42 @@ -114,6 +123,25 @@
    1.43  		SDL_UnlockSurface(fbsurf);
    1.44  	}
    1.45  	SDL_Flip(fbsurf);
    1.46 +
    1.47 +
    1.48 +	/* also print fps every second ... */
    1.49 +	{
    1.50 +		static long prev_fps, num_frames;
    1.51 +		long msec, dt;
    1.52 +
    1.53 +		msec = get_msec();
    1.54 +		dt = msec - prev_fps;
    1.55 +		if(dt >= 1000) {
    1.56 +			float fps = (float)num_frames / ((float)dt / 1000.0f);
    1.57 +			printf("framerate: %.1f      \r", fps);
    1.58 +			fflush(stdout);
    1.59 +			num_frames = 0;
    1.60 +			prev_fps = msec;
    1.61 +		} else {
    1.62 +			num_frames++;
    1.63 +		}
    1.64 +	}
    1.65  }
    1.66  
    1.67  void wait_vsync(void)
    1.68 @@ -124,6 +152,9 @@
    1.69  static SDL_Event *keybev;
    1.70  static int mousex, mousey, bnmask;
    1.71  
    1.72 +static int keystate[256];
    1.73 +static int num_pressed;
    1.74 +
    1.75  int kbhit(void)
    1.76  {
    1.77  	if(!keybev) {
    1.78 @@ -147,12 +178,57 @@
    1.79  	return res;
    1.80  }
    1.81  
    1.82 +/* ----- improved event handling (keyb.h) ---- */
    1.83 +
    1.84 +int kb_init(int bufsz)
    1.85 +{
    1.86 +	init_sdl();
    1.87 +
    1.88 +	return 0;
    1.89 +}
    1.90 +
    1.91 +void kb_shutdown(void)
    1.92 +{
    1.93 +}
    1.94 +
    1.95 +int kb_getkey(void)
    1.96 +{
    1.97 +	int res = -1;
    1.98 +
    1.99 +	proc_events();
   1.100 +	if(keybev) {
   1.101 +		res = keybev->key.keysym.sym;
   1.102 +		keybev = 0;
   1.103 +	}
   1.104 +	return res;
   1.105 +}
   1.106 +
   1.107 +int kb_isdown(int key)
   1.108 +{
   1.109 +	if(key == KB_ANY) {
   1.110 +		return num_pressed;
   1.111 +	}
   1.112 +	return keystate[key];
   1.113 +}
   1.114 +
   1.115  /* mouse handling (mouse.c implementation) */
   1.116 +static unsigned long last_mouse_hide_time;
   1.117 +
   1.118  int have_mouse(void)
   1.119  {
   1.120  	return 1;
   1.121  }
   1.122  
   1.123 +void set_mouse(int x, int y)
   1.124 +{
   1.125 +	SDL_ShowCursor(0);
   1.126 +	last_mouse_hide_time = get_msec();
   1.127 +
   1.128 +	SDL_WarpMouse(x * scale, y * scale);
   1.129 +	mousex = x;
   1.130 +	mousey = y;
   1.131 +}
   1.132 +
   1.133  int read_mouse(int *xp, int *yp)
   1.134  {
   1.135  	if(xp) *xp = mousex;
   1.136 @@ -164,11 +240,40 @@
   1.137  {
   1.138  	static SDL_Event ev;
   1.139  
   1.140 +	if(last_mouse_hide_time > 0 && get_msec() - last_mouse_hide_time > 3000) {
   1.141 +		last_mouse_hide_time = 0;
   1.142 +		SDL_ShowCursor(1);
   1.143 +	}
   1.144 +
   1.145  	while(SDL_PollEvent(&ev)) {
   1.146  		switch(ev.type) {
   1.147  		case SDL_KEYDOWN:
   1.148 -			keybev = &ev;
   1.149 -			return;
   1.150 +			{
   1.151 +				int key = ev.key.keysym.sym;
   1.152 +
   1.153 +				if(!keybev) {
   1.154 +					keybev = &ev;
   1.155 +				}
   1.156 +
   1.157 +				if(!keystate[key]) {
   1.158 +					keystate[key] = 1;
   1.159 +					num_pressed++;
   1.160 +				}
   1.161 +			}
   1.162 +			break;
   1.163 +
   1.164 +		case SDL_KEYUP:
   1.165 +			{
   1.166 +				int key = ev.key.keysym.sym;
   1.167 +
   1.168 +				if(keystate[key]) {
   1.169 +					keystate[key] = 0;
   1.170 +					if(--num_pressed < 0) {
   1.171 +						num_pressed = 0;
   1.172 +					}
   1.173 +				}
   1.174 +			}
   1.175 +			break;
   1.176  
   1.177  		case SDL_MOUSEMOTION:
   1.178  			mousex = ev.motion.x / scale;
   1.179 @@ -214,15 +319,18 @@
   1.180  
   1.181  void init_timer(int res_hz)
   1.182  {
   1.183 +	init_sdl();
   1.184  	reset_timer();
   1.185  }
   1.186  
   1.187  void reset_timer(void)
   1.188  {
   1.189  	start_time = SDL_GetTicks();
   1.190 +	printf("resetting timer: %u, %lu\n", start_time, get_msec());
   1.191  }
   1.192  
   1.193  unsigned long get_msec(void)
   1.194  {
   1.195 -	return (unsigned long)(SDL_GetTicks() - start_time);
   1.196 +	Uint32 ticks = SDL_GetTicks();
   1.197 +	return (unsigned long)(ticks - start_time);
   1.198  }
     2.1 --- a/src/main.c	Mon Sep 23 04:34:43 2013 +0300
     2.2 +++ b/src/main.c	Mon Sep 23 05:58:24 2013 +0300
     2.3 @@ -16,9 +16,9 @@
     2.4  
     2.5  static int init(void);
     2.6  static void shutdown(void);
     2.7 +static void update(unsigned long dtmsec);
     2.8  static void redraw(void);
     2.9  static int proc_events(void);
    2.10 -static int proc_keyb_input(void);
    2.11  static void mouse_button(int bn, int x, int y);
    2.12  static void mouse_motion(int x, int y);
    2.13  static void sighandler(int s);
    2.14 @@ -27,14 +27,10 @@
    2.15  static float cam_x, cam_y, cam_z;
    2.16  static float cam_theta, cam_phi;
    2.17  
    2.18 -static float walk_speed = 0.5;
    2.19 +static float walk_speed = 6.0;
    2.20  static float look_speed = 1.0;
    2.21  
    2.22 -#ifdef __DOS__
    2.23  static int mouse_look = 1;
    2.24 -#else
    2.25 -static int mouse_look = 0;
    2.26 -#endif
    2.27  
    2.28  static void *fbuf;
    2.29  static struct scene scn;
    2.30 @@ -42,11 +38,22 @@
    2.31  
    2.32  int main(void)
    2.33  {
    2.34 +	unsigned long prev_msec = 0;
    2.35 +
    2.36  	if(init() == -1) {
    2.37  		return 1;
    2.38  	}
    2.39  
    2.40 -	while(proc_events()) {
    2.41 +	for(;;) {
    2.42 +		unsigned long msec = get_msec();
    2.43 +		unsigned long dt = msec - prev_msec;
    2.44 +		prev_msec = msec;
    2.45 +
    2.46 +		if(!proc_events()) {
    2.47 +			break;
    2.48 +		}
    2.49 +
    2.50 +		update(dt);
    2.51  		redraw();
    2.52  	}
    2.53  
    2.54 @@ -57,11 +64,10 @@
    2.55  
    2.56  static int init(void)
    2.57  {
    2.58 -	float vfov, hfov, aspect;
    2.59 +	float vfov, aspect;
    2.60  
    2.61  	aspect = 320.0 / 200.0;
    2.62  	vfov = 60.0;
    2.63 -	hfov = vfov * aspect;
    2.64  
    2.65  	init_timer(100);
    2.66  	kb_init(16); /* 16 characters input buffer */
    2.67 @@ -129,6 +135,33 @@
    2.68  	kb_shutdown();
    2.69  }
    2.70  
    2.71 +
    2.72 +#define DEG_TO_RAD(x)	(M_PI * (x) / 180.0)
    2.73 +void cam_move(float dx, float dy)
    2.74 +{
    2.75 +	float angle = DEG_TO_RAD(cam_theta);
    2.76 +	cam_x += cos(angle) * dx + sin(angle) * dy;
    2.77 +	cam_z -= -sin(angle) * dx + cos(angle) * dy;
    2.78 +}
    2.79 +
    2.80 +static void update(unsigned long dtmsec)
    2.81 +{
    2.82 +	float dt = (float)dtmsec / 1000.0f;
    2.83 +	float offs = walk_speed * dt;
    2.84 +
    2.85 +	if(kb_isdown('w') || kb_isdown('W'))
    2.86 +		cam_move(0, offs);
    2.87 +
    2.88 +	if(kb_isdown('s') || kb_isdown('S'))
    2.89 +		cam_move(0, -offs);
    2.90 +
    2.91 +	if(kb_isdown('a') || kb_isdown('A'))
    2.92 +		cam_move(-offs, 0);
    2.93 +
    2.94 +	if(kb_isdown('d') || kb_isdown('D'))
    2.95 +		cam_move(offs, 0);
    2.96 +}
    2.97 +
    2.98  static void redraw(void)
    2.99  {
   2.100  	mgl_clear(0);
   2.101 @@ -150,39 +183,11 @@
   2.102  	copy_frame(fbuf);
   2.103  }
   2.104  
   2.105 -#define DEG_TO_RAD(x)	(M_PI * (x) / 180.0)
   2.106 -void cam_move(float dx, float dy)
   2.107 -{
   2.108 -	float angle = DEG_TO_RAD(cam_theta);
   2.109 -	cam_x += cos(angle) * dx + sin(angle) * dy;
   2.110 -	cam_z -= -sin(angle) * dx + cos(angle) * dy;
   2.111 -}
   2.112 -
   2.113  static int proc_events(void)
   2.114  {
   2.115  	static int prev_mx, prev_my, prev_bnmask;
   2.116 -	int mx, my, bnmask, key;
   2.117 +	int mx, my, bnmask;
   2.118  
   2.119 -	if(!proc_keyb_input()) {
   2.120 -		return 0;
   2.121 -	}
   2.122 -
   2.123 -	bnmask = read_mouse(&mx, &my);
   2.124 -	if(bnmask != prev_bnmask) {
   2.125 -		mouse_button(bnmask, mx, my);
   2.126 -		prev_bnmask = bnmask;
   2.127 -	}
   2.128 -	if(mx != prev_mx || my != prev_my) {
   2.129 -		mouse_motion(mx, my);
   2.130 -		prev_mx = mx;
   2.131 -		prev_my = my;
   2.132 -	}
   2.133 -	return 1;
   2.134 -}
   2.135 -
   2.136 -static int proc_keyb_input(void)
   2.137 -{
   2.138 -	/* first examine all keypresses and handle non-movement keys */
   2.139  	int key;
   2.140  	while((key = kb_getkey()) != -1) {
   2.141  		switch(key) {
   2.142 @@ -198,19 +203,16 @@
   2.143  		}
   2.144  	}
   2.145  
   2.146 -	/* for the movement keys we just care if they are pressed at the moment */
   2.147 -	if(kb_isdown('w') || kb_isdown('W'))
   2.148 -		cam_move(0, walk_speed);
   2.149 -
   2.150 -	if(kb_isdown('s') || kb_isdown('S'))
   2.151 -		cam_move(0, -walk_speed);
   2.152 -
   2.153 -	if(kb_isdown('a') || kb_isdown('A'))
   2.154 -		cam_move(-walk_speed, 0);
   2.155 -
   2.156 -	if(kb_isdown('d') || kb_isdown('D'))
   2.157 -		cam_move(walk_speed, 0);
   2.158 -
   2.159 +	bnmask = read_mouse(&mx, &my);
   2.160 +	if(bnmask != prev_bnmask) {
   2.161 +		mouse_button(bnmask, mx, my);
   2.162 +		prev_bnmask = bnmask;
   2.163 +	}
   2.164 +	if(mx != prev_mx || my != prev_my) {
   2.165 +		mouse_motion(mx, my);
   2.166 +		prev_mx = mx;
   2.167 +		prev_my = my;
   2.168 +	}
   2.169  	return 1;
   2.170  }
   2.171