src/sdl.cpp
changeset 5835 e0ff603ae0b7
parent 5726 8f399788f6c9
child 6298 c30fe89622df
equal deleted inserted replaced
5834:7bf92d5a5a0f 5835:e0ff603ae0b7
       
     1 /* $Id$ */
       
     2 
       
     3 #include "stdafx.h"
       
     4 
       
     5 #ifdef WITH_SDL
       
     6 
       
     7 #include "openttd.h"
       
     8 #include "sdl.h"
       
     9 #include <SDL.h>
       
    10 
       
    11 #ifdef UNIX
       
    12 #include <signal.h>
       
    13 
       
    14 #ifdef __MORPHOS__
       
    15 	// The system supplied definition of SIG_DFL is wrong on MorphOS
       
    16 	#undef SIG_DFL
       
    17 	#define SIG_DFL (void (*)(int))0
       
    18 #endif
       
    19 #endif
       
    20 
       
    21 static int _sdl_usage;
       
    22 
       
    23 #ifdef DYNAMICALLY_LOADED_SDL
       
    24 
       
    25 #include "win32.h"
       
    26 
       
    27 #define M(x) x "\0"
       
    28 static const char sdl_files[] =
       
    29 	M("sdl.dll")
       
    30 	M("SDL_Init")
       
    31 	M("SDL_InitSubSystem")
       
    32 	M("SDL_GetError")
       
    33 	M("SDL_QuitSubSystem")
       
    34 	M("SDL_UpdateRect")
       
    35 	M("SDL_UpdateRects")
       
    36 	M("SDL_SetColors")
       
    37 	M("SDL_WM_SetCaption")
       
    38 	M("SDL_ShowCursor")
       
    39 	M("SDL_FreeSurface")
       
    40 	M("SDL_PollEvent")
       
    41 	M("SDL_WarpMouse")
       
    42 	M("SDL_GetTicks")
       
    43 	M("SDL_OpenAudio")
       
    44 	M("SDL_PauseAudio")
       
    45 	M("SDL_CloseAudio")
       
    46 	M("SDL_LockSurface")
       
    47 	M("SDL_UnlockSurface")
       
    48 	M("SDL_GetModState")
       
    49 	M("SDL_Delay")
       
    50 	M("SDL_Quit")
       
    51 	M("SDL_SetVideoMode")
       
    52 	M("SDL_EnableKeyRepeat")
       
    53 	M("SDL_EnableUNICODE")
       
    54 	M("SDL_VideoDriverName")
       
    55 	M("SDL_ListModes")
       
    56 	M("SDL_GetKeyState")
       
    57 	M("SDL_LoadBMP_RW")
       
    58 	M("SDL_RWFromFile")
       
    59 	M("SDL_SetColorKey")
       
    60 	M("SDL_WM_SetIcon")
       
    61 	M("SDL_MapRGB")
       
    62 	M("")
       
    63 ;
       
    64 #undef M
       
    65 
       
    66 SDLProcs sdl_proc;
       
    67 
       
    68 static const char *LoadSdlDLL(void)
       
    69 {
       
    70 	if (sdl_proc.SDL_Init != NULL)
       
    71 		return NULL;
       
    72 	if (!LoadLibraryList((Function *)(void *)&sdl_proc, sdl_files))
       
    73 		return "Unable to load sdl.dll";
       
    74 	return NULL;
       
    75 }
       
    76 
       
    77 #endif // DYNAMICALLY_LOADED_SDL
       
    78 
       
    79 
       
    80 #ifdef UNIX
       
    81 static void SdlAbort(int sig)
       
    82 {
       
    83 	/* Own hand-made parachute for the cases of failed assertions. */
       
    84 	SDL_CALL SDL_Quit();
       
    85 
       
    86 	switch (sig) {
       
    87 		case SIGSEGV:
       
    88 		case SIGFPE:
       
    89 			signal(sig, SIG_DFL);
       
    90 			raise(sig);
       
    91 			break;
       
    92 
       
    93 		default:
       
    94 			break;
       
    95 	}
       
    96 }
       
    97 #endif
       
    98 
       
    99 
       
   100 const char* SdlOpen(uint32 x)
       
   101 {
       
   102 #ifdef DYNAMICALLY_LOADED_SDL
       
   103 	{
       
   104 		const char *s = LoadSdlDLL();
       
   105 		if (s != NULL) return s;
       
   106 	}
       
   107 #endif
       
   108 	if (_sdl_usage++ == 0) {
       
   109 		if (SDL_CALL SDL_Init(x) == -1)
       
   110 			return SDL_CALL SDL_GetError();
       
   111 	} else if (x != 0) {
       
   112 		if (SDL_CALL SDL_InitSubSystem(x) == -1)
       
   113 			return SDL_CALL SDL_GetError();
       
   114 	}
       
   115 
       
   116 #ifdef UNIX
       
   117 	signal(SIGABRT, SdlAbort);
       
   118 	signal(SIGSEGV, SdlAbort);
       
   119 	signal(SIGFPE, SdlAbort);
       
   120 #endif
       
   121 
       
   122 	return NULL;
       
   123 }
       
   124 
       
   125 void SdlClose(uint32 x)
       
   126 {
       
   127 	if (x != 0)
       
   128 		SDL_CALL SDL_QuitSubSystem(x);
       
   129 	if (--_sdl_usage == 0) {
       
   130 		SDL_CALL SDL_Quit();
       
   131 		#ifdef UNIX
       
   132 		signal(SIGABRT, SIG_DFL);
       
   133 		signal(SIGSEGV, SIG_DFL);
       
   134 		signal(SIGFPE, SIG_DFL);
       
   135 		#endif
       
   136 	}
       
   137 }
       
   138 
       
   139 #endif