# sdl.cpp

Go to the documentation of this file.

    00001 /* $Id: sdl.cpp 14949 2009-01-10 00:31:47Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006
    00007 #ifdef WITH_SDL
    00008
    00009 #include "openttd.h"
    00010 #include "sdl.h"
    00011 #include <SDL.h>
    00012
    00013 #ifdef UNIX
    00014 #include <signal.h>
    00015
    00016 #ifdef __MORPHOS__
    00017   // The system supplied definition of SIG_DFL is wrong on MorphOS
    00018   #undef SIG_DFL
    00019   #define SIG_DFL (void (*)(int))0
    00020 #endif
    00021 #endif
    00022
    00023 static int _sdl_usage;
    00024
    00025 #ifdef DYNAMICALLY_LOADED_SDL
    00026
    00027 #include "win32.h"
    00028
    00029 #define M(x) x "\0"
    00030 static const char sdl_files[] =
    00031   M("sdl.dll")
    00032   M("SDL_Init")
    00033   M("SDL_InitSubSystem")
    00034   M("SDL_GetError")
    00035   M("SDL_QuitSubSystem")
    00036   M("SDL_UpdateRect")
    00037   M("SDL_UpdateRects")
    00038   M("SDL_SetColors")
    00039   M("SDL_WM_SetCaption")
    00040   M("SDL_ShowCursor")
    00041   M("SDL_FreeSurface")
    00042   M("SDL_PollEvent")
    00043   M("SDL_WarpMouse")
    00044   M("SDL_GetTicks")
    00045   M("SDL_OpenAudio")
    00046   M("SDL_PauseAudio")
    00047   M("SDL_CloseAudio")
    00048   M("SDL_LockSurface")
    00049   M("SDL_UnlockSurface")
    00050   M("SDL_GetModState")
    00051   M("SDL_Delay")
    00052   M("SDL_Quit")
    00053   M("SDL_SetVideoMode")
    00054   M("SDL_EnableKeyRepeat")
    00055   M("SDL_EnableUNICODE")
    00056   M("SDL_VideoDriverName")
    00057   M("SDL_ListModes")
    00058   M("SDL_GetKeyState")
    00059   M("SDL_LoadBMP_RW")
    00060   M("SDL_RWFromFile")
    00061   M("SDL_SetColorKey")
    00062   M("SDL_WM_SetIcon")
    00063   M("SDL_MapRGB")
    00064   M("")
    00065 ;
    00066 #undef M
    00067
    00068 SDLProcs sdl_proc;
    00069
    00070 static const char *LoadSdlDLL()
    00071 {
    00072   if (sdl_proc.SDL_Init != NULL)
    00073     return NULL;
    00074   if (!LoadLibraryList((Function *)(void *)&sdl_proc, sdl_files))
    00075     return "Unable to load sdl.dll";
    00076   return NULL;
    00077 }
    00078
    00079 #endif // DYNAMICALLY_LOADED_SDL
    00080
    00081
    00082 #ifdef UNIX
    00083 static void SdlAbort(int sig)
    00084 {
    00085   /* Own hand-made parachute for the cases of failed assertions. */
    00086   SDL_CALL SDL_Quit();
    00087
    00088   switch (sig) {
    00089     case SIGSEGV:
    00090     case SIGFPE:
    00091       signal(sig, SIG_DFL);
    00092       raise(sig);
    00093       break;
    00094
    00095     default:
    00096       break;
    00097   }
    00098 }
    00099 #endif
    00100
    00101
    00102 const char *SdlOpen(uint32 x)
    00103 {
    00104 #ifdef DYNAMICALLY_LOADED_SDL
    00105   {
    00106     const char *s = LoadSdlDLL();
    00107     if (s != NULL) return s;
    00108   }
    00109 #endif
    00110   if (_sdl_usage++ == 0) {
    00111     if (SDL_CALL SDL_Init(x) == -1)
    00112       return SDL_CALL SDL_GetError();
    00113   } else if (x != 0) {
    00114     if (SDL_CALL SDL_InitSubSystem(x) == -1)
    00115       return SDL_CALL SDL_GetError();
    00116   }
    00117
    00118 #ifdef UNIX
    00119   signal(SIGABRT, SdlAbort);
    00120   signal(SIGSEGV, SdlAbort);
    00121   signal(SIGFPE, SdlAbort);
    00122 #endif
    00123
    00124   return NULL;
    00125 }
    00126
    00127 void SdlClose(uint32 x)
    00128 {
    00129   if (x != 0)
    00130     SDL_CALL SDL_QuitSubSystem(x);
    00131   if (--_sdl_usage == 0) {
    00132     SDL_CALL SDL_Quit();
    00133     #ifdef UNIX
    00134     signal(SIGABRT, SIG_DFL);
    00135     signal(SIGSEGV, SIG_DFL);
    00136     signal(SIGFPE, SIG_DFL);
    00137     #endif
    00138   }
    00139 }
    00140
    00141 #endif