Changeset - r423:c15cfc680b31
[Not reviewed]
master
0 1 0
tron - 20 years ago 2004-11-15 09:05:06
tron@openttd.org
(svn r621) Merge r450 to trunk:
Cleanups and #if 0 some unused debug code
1 file changed with 121 insertions and 120 deletions:
sdl.c
121
120
0 comments (0 inline, 0 general)
sdl.c
Show inline comments
 
@@ -51,8 +51,8 @@ typedef struct {
 
	SDLMod (SDLCALL *SDL_GetModState)();
 
	void (SDLCALL *SDL_Delay)(Uint32);
 
	void (SDLCALL *SDL_Quit)();
 
	SDL_Surface *(SDLCALL *SDL_SetVideoMode)(int,int,int,Uint32);
 
	int (SDLCALL *SDL_EnableKeyRepeat)(int,int);
 
	SDL_Surface *(SDLCALL *SDL_SetVideoMode)(int, int, int, Uint32);
 
	int (SDLCALL *SDL_EnableKeyRepeat)(int, int);
 
	void (SDLCALL *SDL_EnableUNICODE)(int);
 
	void (SDLCALL *SDL_VideoDriverName)(char *, int);
 
	SDL_Rect **(SDLCALL *SDL_ListModes)(void *, int);
 
@@ -95,7 +95,7 @@ static const char sdl_files[] =
 

	
 
static SDLProcs _proc;
 

	
 
static char *LoadSdlDLL()
 
static char *LoadSdlDLL(void)
 
{
 
	if (_proc.SDL_Init != NULL)
 
		return NULL;
 
@@ -122,12 +122,15 @@ static void SdlAbort(int sig)
 
static char *SdlOpen(uint32 x)
 
{
 
#if defined(DYNAMICALLY_LOADED_SDL) && defined(WIN32)
 
	{ char *s = LoadSdlDLL(); if (s) return s; }
 
	{
 
		char *s = LoadSdlDLL();
 
		if (s != NULL) return s;
 
	}
 
#endif
 
	if (_sdl_usage++ == 0) {
 
		if (SDL_CALL SDL_Init(x) == -1)
 
			return SDL_CALL SDL_GetError();
 
	} else if (x) {
 
	} else if (x != 0) {
 
		if (SDL_CALL SDL_InitSubSystem(x) == -1)
 
			return SDL_CALL SDL_GetError();
 
	}
 
@@ -141,17 +144,17 @@ static char *SdlOpen(uint32 x)
 

	
 
static void SdlClose(uint32 x)
 
{
 
	if (x)
 
	if (x != 0)
 
		SDL_CALL SDL_QuitSubSystem(x);
 
	if (--_sdl_usage == 0) {
 
		SDL_CALL SDL_Quit();
 
#ifdef UNIX
 
#ifndef __MORPHOS__
 
		signal(SIGABRT, SIG_DFL);
 
#else
 
		signal(SIGABRT, (void (*)(int))0 );
 
#endif
 
#endif
 
		#ifdef UNIX
 
			#ifndef __MORPHOS__
 
				signal(SIGABRT, SIG_DFL);
 
			#else
 
				signal(SIGABRT, (void (*)(int))0);
 
			#endif
 
		#endif
 
	}
 
}
 

	
 
@@ -170,11 +173,12 @@ static void SdlVideoMakeDirty(int left, 
 

	
 
static SDL_Color pal[256];
 

	
 
static void UpdatePalette(uint start, uint end) {
 
static void UpdatePalette(uint start, uint end)
 
{
 
	uint i;
 
	byte *b;
 

	
 
	for(i = start, b = _cur_palette + start * 3; i != end; i++, b += 3) {
 
	for (i = start, b = _cur_palette + start * 3; i != end; i++, b += 3) {
 
		pal[i].r = b[0];
 
		pal[i].g = b[1];
 
		pal[i].b = b[2];
 
@@ -184,11 +188,12 @@ static void UpdatePalette(uint start, ui
 
	SDL_CALL SDL_SetColors(_sdl_screen, pal, start, end);
 
}
 

	
 
static void InitPalette(void) {
 
static void InitPalette(void)
 
{
 
	UpdatePalette(0, 256);
 
}
 

	
 
static void CheckPaletteAnim()
 
static void CheckPaletteAnim(void)
 
{
 
	if(_pal_last_dirty != -1) {
 
		UpdatePalette(_pal_first_dirty, _pal_last_dirty + 1);
 
@@ -196,45 +201,44 @@ static void CheckPaletteAnim()
 
	}
 
}
 

	
 
static void DrawSurfaceToScreen()
 
static void DrawSurfaceToScreen(void)
 
{
 
	int n;
 

	
 
	if ((n=_num_dirty_rects) != 0) {
 
	int n = _num_dirty_rects;
 
	if (n != 0) {
 
		_num_dirty_rects = 0;
 
		if (n > MAX_DIRTY_RECTS)
 
			SDL_CALL SDL_UpdateRect(_sdl_screen, 0, 0, 0, 0);
 
		else {
 
		else
 
			SDL_CALL SDL_UpdateRects(_sdl_screen, n, _dirty_rects);
 
		}
 
	}
 
}
 

	
 
static int CDECL compare_res(const void *pa, const void *pb)
 
{
 
	int x = ((const uint16*)pa)[0] - ((const uint16*)pb)[0];
 
	if (x) return x;
 
	if (x != 0) return x;
 
	return ((const uint16*)pa)[1] - ((const uint16*)pb)[1];
 
}
 

	
 
static const uint16 default_resolutions[][2] = {
 
	{640,480},
 
	{800,600},
 
	{1024,768},
 
	{1152,864},
 
	{1280,960},
 
	{1280,1024},
 
	{1400,1050},
 
	{1600,1200},
 
	{ 640,  480},
 
	{ 800,  600},
 
	{1024,  768},
 
	{1152,  864},
 
	{1280,  960},
 
	{1280, 1024},
 
	{1400, 1050},
 
	{1600, 1200}
 
};
 

	
 
static void GetVideoModes(void) {
 
static void GetVideoModes(void)
 
{
 
	int i;
 
	SDL_Rect **modes;
 

	
 
	modes = SDL_CALL SDL_ListModes(NULL, SDL_SWSURFACE + (_fullscreen ? SDL_FULLSCREEN : 0));
 

	
 
	if(modes == NULL)
 
	if (modes == NULL)
 
		error("sdl: no modes available");
 

	
 
	_all_modes = (modes == (void*)-1);
 
@@ -245,7 +249,7 @@ static void GetVideoModes(void) {
 
		_num_resolutions = lengthof(default_resolutions);
 
	} else {
 
		int n = 0;
 
		for(i = 0; modes[i]; i++) {
 
		for (i = 0; modes[i]; i++) {
 
			int w = modes[i]->w;
 
			int h = modes[i]->h;
 
			if (IS_INT_INSIDE(w, 640, MAX_SCREEN_WIDTH + 1) &&
 
@@ -292,8 +296,6 @@ static int GetAvailableVideoMode(int *w,
 
			delta = newdelta;
 
		}
 
	}
 

	
 
	// use the default mode
 
	*w = _resolutions[best][0];
 
	*h = _resolutions[best][1];
 
	return 2;
 
@@ -308,8 +310,8 @@ static bool CreateMainSurface(int w, int
 
	DEBUG(misc, 0) ("sdl: using mode %dx%d", w, h);
 

	
 
	// DO NOT CHANGE TO HWSURFACE, IT DOES NOT WORK
 
	newscreen = SDL_CALL SDL_SetVideoMode(w, h, 8, SDL_SWSURFACE + SDL_HWPALETTE + (_fullscreen?SDL_FULLSCREEN:SDL_RESIZABLE));
 
	if(newscreen == NULL)
 
	newscreen = SDL_CALL SDL_SetVideoMode(w, h, 8, SDL_SWSURFACE | SDL_HWPALETTE | (_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE));
 
	if (newscreen == NULL)
 
		return false;
 

	
 
	_screen.width = newscreen->w;
 
@@ -327,18 +329,18 @@ static bool CreateMainSurface(int w, int
 
	return true;
 
}
 

	
 
typedef struct {
 
typedef struct VkMapping {
 
	uint16 vk_from;
 
	byte vk_count;
 
	byte map_to;
 
} VkMapping;
 

	
 
#define AS(x,z) {x,0,z}
 
#define AM(x,y,z,w) {x,y-x,z}
 
#define AS(x, z) {x, 0, z}
 
#define AM(x, y, z, w) {x, y - x, z}
 

	
 
static const VkMapping _vk_mapping[] = {
 
	// Pageup stuff + up/down
 
	AM(SDLK_PAGEUP,SDLK_PAGEDOWN, WKC_PAGEUP, WKC_PAGEDOWN),
 
	AM(SDLK_PAGEUP, SDLK_PAGEDOWN, WKC_PAGEUP, WKC_PAGEDOWN),
 
	AS(SDLK_UP,			WKC_UP),
 
	AS(SDLK_DOWN,		WKC_DOWN),
 
	AS(SDLK_LEFT,		WKC_LEFT),
 
@@ -351,8 +353,8 @@ static const VkMapping _vk_mapping[] = {
 
	AS(SDLK_DELETE,	WKC_DELETE),
 

	
 
	// Map letters & digits
 
	AM(SDLK_a,SDLK_z,'A','Z'),
 
	AM(SDLK_0,SDLK_9,'0','9'),
 
	AM(SDLK_a, SDLK_z, 'A', 'Z'),
 
	AM(SDLK_0, SDLK_9, '0', '9'),
 

	
 
	AS(SDLK_ESCAPE,	WKC_ESC),
 
	AS(SDLK_BACKSPACE,	WKC_BACKSPACE),
 
@@ -362,34 +364,26 @@ static const VkMapping _vk_mapping[] = {
 
	AS(SDLK_TAB,			WKC_TAB),
 

	
 
	// Function keys
 
	AM(SDLK_F1, SDLK_F12,	WKC_F1, WKC_F12),
 
	AM(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),
 

	
 
	// Numeric part.
 
	// What is the virtual keycode for numeric enter??
 
	AM(SDLK_KP0,SDLK_KP9, WKC_NUM_0, WKC_NUM_9),
 
	AM(SDLK_KP0, SDLK_KP9, WKC_NUM_0, WKC_NUM_9),
 
	AS(SDLK_KP_DIVIDE,		WKC_NUM_DIV),
 
	AS(SDLK_KP_MULTIPLY,	WKC_NUM_MUL),
 
	AS(SDLK_KP_MINUS,			WKC_NUM_MINUS),
 
	AS(SDLK_KP_PLUS,			WKC_NUM_PLUS),
 
	AS(SDLK_KP_ENTER,			WKC_NUM_ENTER),
 
	AS(SDLK_KP_PERIOD,		WKC_NUM_DECIMAL),
 
	{0, 0, 0}
 
	AS(SDLK_KP_PERIOD,		WKC_NUM_DECIMAL)
 
};
 

	
 
static uint32 ConvertSdlKeyIntoMy(SDL_keysym *sym)
 
{
 
	const VkMapping	*map = _vk_mapping - 1;
 
	uint from;
 
	uint key = sym->sym;
 
	for(;;) {
 
		map++;
 
		from = map->vk_from;
 
		if (from == 0) {
 
			key = 0;
 
			break;
 
		}
 
		if ((uint)(key - from) <= map->vk_count) {
 
			key = key - from + map->map_to;
 
	const VkMapping	*map;
 
	uint key = 0;
 
	for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
 
		if ((uint)(sym->sym - map->vk_from) <= map->vk_count) {
 
			key = sym->sym - map->vk_from + map->map_to;
 
			break;
 
		}
 
	}
 
@@ -397,35 +391,32 @@ static uint32 ConvertSdlKeyIntoMy(SDL_ke
 
	// check scancode for BACKQUOTE key, because we want the key left of "1", not anything else (on non-US keyboards)
 
#if defined(WIN32)
 
	if (sym->scancode == 41) key |= WKC_BACKQUOTE;
 
#elif defined(__APPLE__)
 
	if (sym->scancode == 10) key |= WKC_BACKQUOTE;
 
#elif defined(__MORPHOS__)
 
	if (sym->scancode == 0)  key |= WKC_BACKQUOTE;  // yes, that key is code '0' under MorphOS :)
 
#else
 
	#if defined(__APPLE__)
 
		if (sym->scancode == 10) key |= WKC_BACKQUOTE;
 
	#else
 
		#if defined(__MORPHOS__)
 
			if (sym->scancode == 0)  key |= WKC_BACKQUOTE;  // yes, that key is code '0' under MorphOS :)
 
		#else
 
			if (sym->scancode == 49) key |= WKC_BACKQUOTE;
 
		#endif
 
	#endif
 
	if (sym->scancode == 49) key |= WKC_BACKQUOTE;
 
#endif
 
	// META are the command keys on mac
 
	if (sym->mod & KMOD_META) key |= WKC_META;
 
	if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
 
	if (sym->mod & KMOD_CTRL) key |= WKC_CTRL;
 
	if (sym->mod & KMOD_ALT) key |= WKC_ALT;
 
	// these two lines really helps porting hotkey combos. Uncomment to use -- Bjarni
 
	// these two lines really help porting hotkey combos. Uncomment to use -- Bjarni
 
	//printf("scancode character pressed %d\n", sym->scancode);
 
	//printf("unicode character pressed %d\n", sym->unicode);
 
	return (key << 16) + sym->unicode;
 
}
 

	
 
static int PollEvent() {
 
static int PollEvent(void)
 
{
 
	SDL_Event ev;
 

	
 
	if (!SDL_CALL SDL_PollEvent(&ev))
 
		return -2;
 

	
 
	switch(ev.type) {
 
	switch (ev.type) {
 
	case SDL_MOUSEMOTION:
 
		if (_cursor.fix_at) {
 
			int dx = ev.motion.x - _cursor.pos.x;
 
@@ -448,19 +439,25 @@ static int PollEvent() {
 
		if (_rightclick_emulate && (SDL_CALL SDL_GetModState() & (KMOD_LCTRL | KMOD_RCTRL)))
 
			ev.button.button = SDL_BUTTON_RIGHT;
 

	
 
		if (ev.button.button == SDL_BUTTON_LEFT) {
 
			_left_button_down = true;
 
		} else if (ev.button.button == SDL_BUTTON_RIGHT) {
 
			_right_button_down = true;
 
			_right_button_clicked = true;
 
		}
 
		switch (ev.button.button) {
 
			case SDL_BUTTON_LEFT:
 
				_left_button_down = true;
 
				break;
 
			case SDL_BUTTON_RIGHT:
 
				_right_button_down = true;
 
				_right_button_clicked = true;
 
				break;
 
#if !defined(WIN32)
 
		 else if (ev.button.button == SDL_BUTTON_WHEELUP) {
 
			_cursor.wheel--;
 
		} else if (ev.button.button == SDL_BUTTON_WHEELDOWN) {
 
			_cursor.wheel++;
 
			case SDL_BUTTON_WHEELUP:
 
				_cursor.wheel--;
 
				break;
 
			case SDL_BUTTON_WHEELDOWN:
 
				_cursor.wheel++;
 
				break;
 
#endif
 
			default:
 
				break;
 
		}
 
#endif
 
		break;
 

	
 
	case SDL_MOUSEBUTTONUP:
 
@@ -478,14 +475,15 @@ static int PollEvent() {
 

	
 
	case SDL_QUIT:
 
		// do not ask to quit on the main screen
 
		if(_game_mode != GM_MENU)
 
		if (_game_mode != GM_MENU)
 
			AskExitGame();
 
		else
 
			return ML_QUIT;
 
		break;
 

	
 
	case SDL_KEYDOWN:
 
		if ((((ev.key.keysym.sym == SDLK_RETURN) || (ev.key.keysym.sym == SDLK_f)) && (ev.key.keysym.mod & KMOD_ALT)) || (((ev.key.keysym.sym == SDLK_RETURN) || (ev.key.keysym.sym == SDLK_f)) && (ev.key.keysym.mod & KMOD_META))) {
 
		if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_META)) &&
 
				(ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) {
 
			_fullscreen ^= true;
 
			GetVideoModes();
 
			CreateMainSurface(_screen.width, _screen.height);
 
@@ -496,26 +494,21 @@ static int PollEvent() {
 
		break;
 

	
 
	case SDL_VIDEORESIZE: {
 
			int w, h;
 
			w = ev.resize.w;
 
			h = ev.resize.h;
 

	
 
			w = clamp(w, 64, MAX_SCREEN_WIDTH);
 
			h = clamp(h, 64, MAX_SCREEN_HEIGHT);
 

	
 
			ChangeResInGame(w, h);
 

	
 
			break;
 
		}
 
		int w = clamp(ev.resize.w, 64, MAX_SCREEN_WIDTH);
 
		int h = clamp(ev.resize.h, 64, MAX_SCREEN_HEIGHT);
 
		ChangeResInGame(w, h);
 
		break;
 
	}
 
	}
 
	return -1;
 
}
 

	
 
static const char *SdlVideoStart(char **parm)
 
{
 
    char buf[30];
 
	char buf[30];
 

	
 
	{char *s;if ((s = SdlOpen(SDL_INIT_VIDEO)) != NULL) return s;}
 
	const char *s = SdlOpen(SDL_INIT_VIDEO);
 
	if (s != NULL) return s;
 

	
 
	SDL_CALL SDL_VideoDriverName(buf, 30);
 
	DEBUG(misc, 0) ("sdl: using driver '%s'", buf);
 
@@ -529,40 +522,43 @@ static const char *SdlVideoStart(char **
 
	return NULL;
 
}
 

	
 
static void SdlVideoStop()
 
static void SdlVideoStop(void)
 
{
 
	SdlClose(SDL_INIT_VIDEO);
 
}
 

	
 
static int SdlVideoMainLoop()
 
static int SdlVideoMainLoop(void)
 
{
 
	uint32 next_tick = SDL_CALL SDL_GetTicks() + 30, cur_ticks, pal_tick = 0;
 
	uint32 next_tick = SDL_CALL SDL_GetTicks() + 30;
 
	uint32 cur_ticks;
 
	uint32 pal_tick = 0;
 
	int i;
 
	uint32 mod;
 
	int numkeys;
 
	Uint8 *keys;
 

	
 
	while (true) {
 
	for (;;) {
 
		InteractiveRandom(); // randomness
 

	
 
		while ((i=PollEvent()) == -1) {}
 
		if (i>=0) return i;
 
		while ((i = PollEvent()) == -1) {}
 
		if (i >= 0) return i;
 

	
 
		if (_exit_game)	return ML_QUIT;
 

	
 
		mod = SDL_CALL SDL_GetModState();
 
		keys = SDL_CALL SDL_GetKeyState(&numkeys);
 
#if defined(_DEBUG)
 
		if (_shift_pressed) {
 
		if (_shift_pressed)
 
#else
 
		if (keys[SDLK_TAB]) {
 
		if (keys[SDLK_TAB])
 
#endif
 
		{
 
			if (!_networking) _fast_forward |= 2;
 
		} else if (_fast_forward&2) {
 
		} else if (_fast_forward & 2) {
 
			_fast_forward = 0;
 
		}
 

	
 
		cur_ticks=SDL_CALL SDL_GetTicks();
 
		cur_ticks = SDL_CALL SDL_GetTicks();
 
		if ((_fast_forward && !_pause) || cur_ticks > next_tick)
 
			next_tick = cur_ticks;
 

	
 
@@ -575,17 +571,17 @@ static int SdlVideoMainLoop()
 

	
 
			// determine which directional keys are down
 
			_dirkeys =
 
			    (keys[SDLK_LEFT] ? 1 : 0) +
 
			    (keys[SDLK_UP] ? 2 : 0) +
 
			    (keys[SDLK_RIGHT] ? 4 : 0) +
 
			    (keys[SDLK_DOWN] ? 8 : 0);
 
				(keys[SDLK_LEFT]  ? 1 : 0) |
 
				(keys[SDLK_UP]    ? 2 : 0) |
 
				(keys[SDLK_RIGHT] ? 4 : 0) |
 
				(keys[SDLK_DOWN]  ? 8 : 0);
 
			GameLoop();
 

	
 
			_screen.dst_ptr = _sdl_screen->pixels;
 
			UpdateWindows();
 
			if (++ pal_tick > 4){
 
			    CheckPaletteAnim();
 
			    pal_tick = 1;
 
			if (++pal_tick > 4) {
 
				CheckPaletteAnim();
 
				pal_tick = 1;
 
			}
 
 			DrawSurfaceToScreen();
 
		} else {
 
@@ -617,25 +613,27 @@ const HalVideoDriver _sdl_video_driver =
 

	
 
static void CDECL fill_sound_buffer(void *userdata, Uint8 *stream, int len)
 
{
 
	MxMixSamples(_mixer, stream, len >> 2);
 
	MxMixSamples(_mixer, stream, len / 4);
 
}
 

	
 
static char *SdlSoundStart(char **parm)
 
{
 
	SDL_AudioSpec spec;
 

	
 
	{char *s;if ((s = SdlOpen(SDL_INIT_AUDIO)) != NULL) return s;}
 
	char *s = SdlOpen(SDL_INIT_AUDIO);
 
	if (s != NULL) return s;
 

	
 
	spec.freq = GetDriverParamInt(parm, "hz", 11025);
 
	spec.format = AUDIO_S16SYS;
 
	spec.channels = 2;
 
	spec.samples = 512;
 
	*(void**)&spec.callback = fill_sound_buffer;
 
	spec.callback = fill_sound_buffer;
 
	SDL_CALL SDL_OpenAudio(&spec, &spec);
 
	SDL_CALL SDL_PauseAudio(0);
 
	return NULL;
 
}
 

	
 
static void SdlSoundStop()
 
static void SdlSoundStop(void)
 
{
 
	SDL_CALL SDL_CloseAudio();
 
	SdlClose(SDL_INIT_AUDIO);
 
@@ -647,6 +645,7 @@ const HalSoundDriver _sdl_sound_driver =
 
};
 

	
 

	
 
#if 0 /* XXX what the heck is that? */
 
#include "viewport.h"
 
void redsq_debug(int tile)
 
{
 
@@ -664,4 +663,6 @@ static void DbgRedraw()
 
	SdlVideoMakeDirty(0,0,_screen.width,_screen.height);
 
	DrawSurfaceToScreen();
 
}
 
#endif
 

	
 
#endif // WITH_SDL
0 comments (0 inline, 0 general)