OpenTTD
sdl.cpp
Go to the documentation of this file.
1 /* $Id: sdl.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #include "stdafx.h"
13 
14 #ifdef WITH_SDL
15 
16 #include "sdl.h"
17 #include <SDL.h>
18 
20 static int _sdl_usage;
21 
22 #ifdef DYNAMICALLY_LOADED_SDL
23 
24 #include "os/windows/win32.h"
25 
26 #define M(x) x "\0"
27 static const char sdl_files[] =
28  M("sdl.dll")
29  M("SDL_Init")
30  M("SDL_InitSubSystem")
31  M("SDL_GetError")
32  M("SDL_QuitSubSystem")
33  M("SDL_UpdateRect")
34  M("SDL_UpdateRects")
35  M("SDL_SetColors")
36  M("SDL_WM_SetCaption")
37  M("SDL_ShowCursor")
38  M("SDL_FreeSurface")
39  M("SDL_PollEvent")
40  M("SDL_WarpMouse")
41  M("SDL_GetTicks")
42  M("SDL_OpenAudio")
43  M("SDL_PauseAudio")
44  M("SDL_CloseAudio")
45  M("SDL_LockSurface")
46  M("SDL_UnlockSurface")
47  M("SDL_GetModState")
48  M("SDL_Delay")
49  M("SDL_Quit")
50  M("SDL_SetVideoMode")
51  M("SDL_EnableKeyRepeat")
52  M("SDL_EnableUNICODE")
53  M("SDL_VideoDriverName")
54  M("SDL_ListModes")
55  M("SDL_GetKeyState")
56  M("SDL_LoadBMP_RW")
57  M("SDL_RWFromFile")
58  M("SDL_SetColorKey")
59  M("SDL_WM_SetIcon")
60  M("SDL_MapRGB")
61  M("SDL_VideoModeOK")
62  M("SDL_Linked_Version")
63  M("")
64 ;
65 #undef M
66 
67 SDLProcs sdl_proc;
68 
69 static const char *LoadSdlDLL()
70 {
71  if (sdl_proc.SDL_Init != NULL) {
72  return NULL;
73  }
74  if (!LoadLibraryList((Function *)(void *)&sdl_proc, sdl_files)) {
75  return "Unable to load sdl.dll";
76  }
77  return NULL;
78 }
79 
80 #endif /* DYNAMICALLY_LOADED_SDL */
81 
82 #include "safeguards.h"
83 
88 const char *SdlOpen(uint32 x)
89 {
90 #ifdef DYNAMICALLY_LOADED_SDL
91  {
92  const char *s = LoadSdlDLL();
93  if (s != NULL) return s;
94  }
95 #endif
96  if (_sdl_usage++ == 0) {
97  if (SDL_CALL SDL_Init(x | SDL_INIT_NOPARACHUTE) == -1) return SDL_CALL SDL_GetError();
98  } else if (x != 0) {
99  if (SDL_CALL SDL_InitSubSystem(x) == -1) return SDL_CALL SDL_GetError();
100  }
101 
102  return NULL;
103 }
104 
109 void SdlClose(uint32 x)
110 {
111  if (x != 0) {
112  SDL_CALL SDL_QuitSubSystem(x);
113  }
114  if (--_sdl_usage == 0) {
115  SDL_CALL SDL_Quit();
116  }
117 }
118 
119 #endif
static int _sdl_usage
Number of users of the SDL library.
Definition: sdl.cpp:20
const char * SdlOpen(uint32 x)
Open the SDL library.
Definition: sdl.cpp:88
bool LoadLibraryList(Function proc[], const char *dll)
Helper function needed by dynamically loading libraries XXX: Hurray for MS only having an ANSI GetPro...
Definition: win32.cpp:58
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
SDL support.
void SdlClose(uint32 x)
Close the SDL library.
Definition: sdl.cpp:109
declarations of functions for MS windows systems