deepstone

diff src/dosemu/dosemu.c @ 35:1870c4ef8b76

added visual studio project moved the dosemu directory
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 23 Sep 2013 07:41:05 +0300
parents dosemu/dosemu.c@c6406e4aa0fb
children e234f2a4b6fa
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/dosemu/dosemu.c	Mon Sep 23 07:41:05 2013 +0300
     1.3 @@ -0,0 +1,342 @@
     1.4 +/* This file implements all calls made to dos-specific code using SDL
     1.5 + * Don't ask why ...
     1.6 + */
     1.7 +
     1.8 +#include <stdlib.h>
     1.9 +#include <assert.h>
    1.10 +#include <SDL.h>
    1.11 +#include "wvga.h"
    1.12 +#include "conio.h"
    1.13 +#include "mouse.h"
    1.14 +#include "keyb.h"
    1.15 +#include "timer.h"
    1.16 +
    1.17 +static void proc_events(void);
    1.18 +
    1.19 +static void init_sdl()
    1.20 +{
    1.21 +	const SDL_version *ver;
    1.22 +
    1.23 +	if(!SDL_WasInit(SDL_INIT_EVERYTHING)) {
    1.24 +		SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
    1.25 +
    1.26 +		if((ver = SDL_Linked_Version())) {
    1.27 +			printf("SDL %d.%d.%d initialized\n", ver->major, ver->minor, ver->patch);
    1.28 +		}
    1.29 +	}
    1.30 +}
    1.31 +
    1.32 +/* ----- graphics (wvga.c implementation) ----- */
    1.33 +static SDL_Surface *fbsurf;
    1.34 +static int scale = 3;
    1.35 +
    1.36 +int set_video_mode(int mode)
    1.37 +{
    1.38 +	int resx = 320, resy = 200;
    1.39 +	unsigned int sdl_flags = SDL_HWPALETTE;
    1.40 +	char *env;
    1.41 +
    1.42 +	if(getenv("DOSEMU_DOUBLESIZE")) {
    1.43 +		scale = 2;
    1.44 +	}
    1.45 +
    1.46 +	if((env = getenv("DOSEMU_SCALE"))) {
    1.47 +		int n = atoi(env);
    1.48 +		if(n > 0) {
    1.49 +			scale = n;
    1.50 +		}
    1.51 +	}
    1.52 +	resx *= scale;
    1.53 +	resy *= scale;
    1.54 +
    1.55 +	if(getenv("DOSEMU_FULLSCREEN")) {
    1.56 +		sdl_flags |= SDL_FULLSCREEN;
    1.57 +	}
    1.58 +
    1.59 +	init_sdl();
    1.60 +
    1.61 +	switch(mode) {
    1.62 +	case 0x13:
    1.63 +		if(!(fbsurf = SDL_SetVideoMode(resx, resy, 8, sdl_flags))) {
    1.64 +			fprintf(stderr, "failed to set video mode\n");
    1.65 +			abort();
    1.66 +		}
    1.67 +		SDL_WM_SetCaption("Deepstone", 0);
    1.68 +		/*SDL_ShowCursor(0);*/
    1.69 +		/*SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);*/
    1.70 +		break;
    1.71 +
    1.72 +	case 3:
    1.73 +		SDL_ShowCursor(1);
    1.74 +		SDL_EnableKeyRepeat(0, 0);
    1.75 +		SDL_Quit();
    1.76 +		break;
    1.77 +
    1.78 +	default:
    1.79 +		break;
    1.80 +	}
    1.81 +
    1.82 +	return 0;
    1.83 +}
    1.84 +
    1.85 +void set_palette(int idx, int *col, int count)
    1.86 +{
    1.87 +	int i;
    1.88 +
    1.89 +	for(i=0; i<count; i++) {
    1.90 +		set_pal_entry(idx + i, col[0], col[1], col[2]);
    1.91 +		col += 3;
    1.92 +	}
    1.93 +}
    1.94 +
    1.95 +void set_pal_entry(int idx, int r, int g, int b)
    1.96 +{
    1.97 +	SDL_Color col;
    1.98 +	col.r = r;
    1.99 +	col.g = g;
   1.100 +	col.b = b;
   1.101 +
   1.102 +	if(SDL_SetPalette(fbsurf, SDL_LOGPAL | SDL_PHYSPAL, &col, idx, 1) != 1) {
   1.103 +		fprintf(stderr, "set_palette failed to set the required color\n");
   1.104 +	}
   1.105 +}
   1.106 +
   1.107 +void copy_frame(void *pixels)
   1.108 +{
   1.109 +	unsigned char *frame = (unsigned char*)pixels;
   1.110 +
   1.111 +	if(SDL_MUSTLOCK(fbsurf)) {
   1.112 +		SDL_LockSurface(fbsurf);
   1.113 +	}
   1.114 +
   1.115 +	if(scale > 1) {
   1.116 +		int i, j, xsz, ysz;
   1.117 +		unsigned char *dest = fbsurf->pixels;
   1.118 +
   1.119 +		xsz = 320 * scale;
   1.120 +		ysz = 200 * scale;
   1.121 +
   1.122 +		for(i=0; i<ysz; i++) {
   1.123 +			for(j=0; j<xsz; j++) {
   1.124 +				*dest++ = frame[(i / scale) * 320 + (j / scale)];
   1.125 +			}
   1.126 +		}
   1.127 +	} else {
   1.128 +		memcpy(fbsurf->pixels, frame, 64000);
   1.129 +	}
   1.130 +
   1.131 +	if(SDL_MUSTLOCK(fbsurf)) {
   1.132 +		SDL_UnlockSurface(fbsurf);
   1.133 +	}
   1.134 +	SDL_Flip(fbsurf);
   1.135 +
   1.136 +
   1.137 +	/* also print fps every second ... */
   1.138 +	{
   1.139 +		static long prev_fps, num_frames;
   1.140 +		long msec, dt;
   1.141 +
   1.142 +		msec = get_msec();
   1.143 +		dt = msec - prev_fps;
   1.144 +		if(dt >= 1000) {
   1.145 +			float fps = (float)num_frames / ((float)dt / 1000.0f);
   1.146 +			printf("framerate: %.1f      \r", fps);
   1.147 +			fflush(stdout);
   1.148 +			num_frames = 0;
   1.149 +			prev_fps = msec;
   1.150 +		} else {
   1.151 +			num_frames++;
   1.152 +		}
   1.153 +	}
   1.154 +}
   1.155 +
   1.156 +void wait_vsync(void)
   1.157 +{
   1.158 +}
   1.159 +
   1.160 +/* ----- event handling (conio.h) ----- */
   1.161 +static SDL_Event *keybev;
   1.162 +static int mousex, mousey, bnmask;
   1.163 +
   1.164 +static int keystate[256];
   1.165 +static int num_pressed;
   1.166 +
   1.167 +int kbhit(void)
   1.168 +{
   1.169 +	if(!keybev) {
   1.170 +		proc_events();
   1.171 +	}
   1.172 +	return keybev != 0;
   1.173 +}
   1.174 +
   1.175 +int getch(void)
   1.176 +{
   1.177 +	int res;
   1.178 +
   1.179 +	while(!keybev) {
   1.180 +		SDL_Event ev;
   1.181 +		SDL_WaitEvent(&ev);
   1.182 +		SDL_PushEvent(&ev);
   1.183 +		proc_events();
   1.184 +	}
   1.185 +	res = keybev->key.keysym.sym;
   1.186 +	keybev = 0;
   1.187 +	return res;
   1.188 +}
   1.189 +
   1.190 +/* ----- improved event handling (keyb.h) ---- */
   1.191 +
   1.192 +int kb_init(int bufsz)
   1.193 +{
   1.194 +	init_sdl();
   1.195 +
   1.196 +	return 0;
   1.197 +}
   1.198 +
   1.199 +void kb_shutdown(void)
   1.200 +{
   1.201 +}
   1.202 +
   1.203 +int kb_getkey(void)
   1.204 +{
   1.205 +	int res = -1;
   1.206 +
   1.207 +	proc_events();
   1.208 +	if(keybev) {
   1.209 +		res = keybev->key.keysym.sym;
   1.210 +		keybev = 0;
   1.211 +	}
   1.212 +	return res;
   1.213 +}
   1.214 +
   1.215 +int kb_isdown(int key)
   1.216 +{
   1.217 +	if(key == KB_ANY) {
   1.218 +		return num_pressed;
   1.219 +	}
   1.220 +	return keystate[key];
   1.221 +}
   1.222 +
   1.223 +/* mouse handling (mouse.c implementation) */
   1.224 +static unsigned long last_mouse_hide_time;
   1.225 +
   1.226 +int have_mouse(void)
   1.227 +{
   1.228 +	return 1;
   1.229 +}
   1.230 +
   1.231 +void set_mouse(int x, int y)
   1.232 +{
   1.233 +	/*SDL_ShowCursor(0);
   1.234 +	last_mouse_hide_time = get_msec();*/
   1.235 +
   1.236 +	SDL_WarpMouse(x * scale, y * scale);
   1.237 +	mousex = x;
   1.238 +	mousey = y;
   1.239 +}
   1.240 +
   1.241 +int read_mouse(int *xp, int *yp)
   1.242 +{
   1.243 +	if(xp) *xp = mousex;
   1.244 +	if(yp) *yp = mousey;
   1.245 +	return bnmask;
   1.246 +}
   1.247 +
   1.248 +static void proc_events(void)
   1.249 +{
   1.250 +	static SDL_Event ev;
   1.251 +
   1.252 +	if(last_mouse_hide_time > 0 && get_msec() - last_mouse_hide_time > 3000) {
   1.253 +		last_mouse_hide_time = 0;
   1.254 +		SDL_ShowCursor(1);
   1.255 +	}
   1.256 +
   1.257 +	while(SDL_PollEvent(&ev)) {
   1.258 +		switch(ev.type) {
   1.259 +		case SDL_KEYDOWN:
   1.260 +			{
   1.261 +				int key = ev.key.keysym.sym;
   1.262 +
   1.263 +				if(!keybev) {
   1.264 +					keybev = &ev;
   1.265 +				}
   1.266 +
   1.267 +				if(!keystate[key]) {
   1.268 +					keystate[key] = 1;
   1.269 +					num_pressed++;
   1.270 +				}
   1.271 +			}
   1.272 +			break;
   1.273 +
   1.274 +		case SDL_KEYUP:
   1.275 +			{
   1.276 +				int key = ev.key.keysym.sym;
   1.277 +
   1.278 +				if(keystate[key]) {
   1.279 +					keystate[key] = 0;
   1.280 +					if(--num_pressed < 0) {
   1.281 +						num_pressed = 0;
   1.282 +					}
   1.283 +				}
   1.284 +			}
   1.285 +			break;
   1.286 +
   1.287 +		case SDL_MOUSEMOTION:
   1.288 +			mousex = ev.motion.x / scale;
   1.289 +			mousey = ev.motion.y / scale;
   1.290 +			break;
   1.291 +
   1.292 +		case SDL_MOUSEBUTTONDOWN:
   1.293 +		case SDL_MOUSEBUTTONUP:
   1.294 +			{
   1.295 +				int mask = 0;
   1.296 +				switch(ev.button.button) {
   1.297 +				case SDL_BUTTON_LEFT:
   1.298 +					mask = MOUSE_LEFT;
   1.299 +					break;
   1.300 +				case SDL_BUTTON_MIDDLE:
   1.301 +					mask = MOUSE_MIDDLE;
   1.302 +					break;
   1.303 +				case SDL_BUTTON_RIGHT:
   1.304 +					mask = MOUSE_RIGHT;
   1.305 +				default:
   1.306 +					break;
   1.307 +				}
   1.308 +				if(!mask) {
   1.309 +					break;
   1.310 +				}
   1.311 +
   1.312 +				if(ev.button.state == SDL_PRESSED) {
   1.313 +					bnmask |= mask;
   1.314 +				} else {
   1.315 +					bnmask &= ~mask;
   1.316 +				}
   1.317 +			}
   1.318 +			break;
   1.319 +
   1.320 +		default:
   1.321 +			break;
   1.322 +		}
   1.323 +	}
   1.324 +}
   1.325 +
   1.326 +/* ---- timer.c implementation ---- */
   1.327 +static Uint32 start_time;
   1.328 +
   1.329 +void init_timer(int res_hz)
   1.330 +{
   1.331 +	init_sdl();
   1.332 +	reset_timer();
   1.333 +}
   1.334 +
   1.335 +void reset_timer(void)
   1.336 +{
   1.337 +	start_time = SDL_GetTicks();
   1.338 +	printf("resetting timer: %u, %lu\n", start_time, get_msec());
   1.339 +}
   1.340 +
   1.341 +unsigned long get_msec(void)
   1.342 +{
   1.343 +	Uint32 ticks = SDL_GetTicks();
   1.344 +	return (unsigned long)(ticks - start_time);
   1.345 +}