# sdl.cpp

Go to the documentation of this file.

    00001 /* $Id: sdl.cpp 18478 2009-12-13 00:33:00Z 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("SDL_VideoModeOK")
    00065   M("")
    00066 ;
    00067 #undef M
    00068
    00069 SDLProcs sdl_proc;
    00070
    00071 static const char *LoadSdlDLL()
    00072 {
    00073   if (sdl_proc.SDL_Init != NULL)
    00074     return NULL;
    00075   if (!LoadLibraryList((Function *)(void *)&sdl_proc, sdl_files))
    00076     return "Unable to load sdl.dll";
    00077   return NULL;
    00078 }
    00079
    00080 #endif /* DYNAMICALLY_LOADED_SDL */
    00081
    00082
    00083 #ifdef UNIX
    00084 static void SdlAbort(int sig)
    00085 {
    00086   /* Own hand-made parachute for the cases of failed assertions. */
    00087   SDL_CALL SDL_Quit();
    00088
    00089   switch (sig) {
    00090     case SIGSEGV:
    00091     case SIGFPE:
    00092       signal(sig, SIG_DFL);
    00093       raise(sig);
    00094       break;
    00095
    00096     default:
    00097       break;
    00098   }
    00099 }
    00100 #endif
    00101
    00102
    00103 const char *SdlOpen(uint32 x)
    00104 {
    00105 #ifdef DYNAMICALLY_LOADED_SDL
    00106   {
    00107     const char *s = LoadSdlDLL();
    00108     if (s != NULL) return s;
    00109   }
    00110 #endif
    00111   if (_sdl_usage++ == 0) {
    00112     if (SDL_CALL SDL_Init(x) == -1)
    00113       return SDL_CALL SDL_GetError();
    00114   } else if (x != 0) {
    00115     if (SDL_CALL SDL_InitSubSystem(x) == -1)
    00116       return SDL_CALL SDL_GetError();
    00117   }
    00118
    00119 #ifdef UNIX
    00120   signal(SIGABRT, SdlAbort);
    00121   signal(SIGSEGV, SdlAbort);
    00122   signal(SIGFPE, SdlAbort);
    00123 #endif
    00124
    00125   return NULL;
    00126 }
    00127
    00128 void SdlClose(uint32 x)
    00129 {
    00130   if (x != 0)
    00131     SDL_CALL SDL_QuitSubSystem(x);
    00132   if (--_sdl_usage == 0) {
    00133     SDL_CALL SDL_Quit();
    00134     #ifdef UNIX
    00135     signal(SIGABRT, SIG_DFL);
    00136     signal(SIGSEGV, SIG_DFL);
    00137     signal(SIGFPE, SIG_DFL);
    00138     #endif
    00139   }
    00140 }
    00141
    00142 #endif