deepstone

diff dosemu/dosemu.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 11d14f688485
children
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  }