comparison pytouhou/lib/sdl.pyx @ 460:ec327e58b477

Add a context manager to initialize and shut down SDL outside of Window.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 06 Sep 2013 23:29:19 +0200
parents cae1ae9de430
children 36bc577b2392
comparison
equal deleted inserted replaced
459:6e733ed817bd 460:ec327e58b477
9 ## This program is distributed in the hope that it will be useful, 9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of 10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14
15 INIT_VIDEO = SDL_INIT_VIDEO
16 INIT_PNG = IMG_INIT_PNG
17 14
18 GL_CONTEXT_MAJOR_VERSION = SDL_GL_CONTEXT_MAJOR_VERSION 15 GL_CONTEXT_MAJOR_VERSION = SDL_GL_CONTEXT_MAJOR_VERSION
19 GL_CONTEXT_MINOR_VERSION = SDL_GL_CONTEXT_MINOR_VERSION 16 GL_CONTEXT_MINOR_VERSION = SDL_GL_CONTEXT_MINOR_VERSION
20 GL_DOUBLEBUFFER = SDL_GL_DOUBLEBUFFER 17 GL_DOUBLEBUFFER = SDL_GL_DOUBLEBUFFER
21 GL_DEPTH_SIZE = SDL_GL_DEPTH_SIZE 18 GL_DEPTH_SIZE = SDL_GL_DEPTH_SIZE
35 SCANCODE_ESCAPE = SDL_SCANCODE_ESCAPE 32 SCANCODE_ESCAPE = SDL_SCANCODE_ESCAPE
36 33
37 KEYDOWN = SDL_KEYDOWN 34 KEYDOWN = SDL_KEYDOWN
38 QUIT = SDL_QUIT 35 QUIT = SDL_QUIT
39 36
40 DEFAULT_FORMAT = MIX_DEFAULT_FORMAT
41
42 37
43 class SDLError(Exception): 38 class SDLError(Exception):
44 pass 39 pass
40
41
42 class SDL(object):
43 def __init__(self, sound=True):
44 self.sound = sound
45
46 def __enter__(self):
47 IF UNAME_SYSNAME == "Windows":
48 SDL_SetMainReady()
49 init(SDL_INIT_VIDEO)
50 img_init(IMG_INIT_PNG)
51 ttf_init()
52
53 if self.sound:
54 mix_init(0)
55 mix_open_audio(44100, MIX_DEFAULT_FORMAT, 2, 4096)
56 mix_allocate_channels(MAX_CHANNELS) #TODO: make it dependent on the SFX number.
57
58 def __exit__(self, *args):
59 if self.sound:
60 Mix_CloseAudio()
61 Mix_Quit()
62
63 TTF_Quit()
64 IMG_Quit()
65 SDL_Quit()
45 66
46 67
47 cdef class Window: 68 cdef class Window:
48 def __init__(self, const char *title, int x, int y, int w, int h, Uint32 flags): 69 def __init__(self, const char *title, int x, int y, int w, int h, Uint32 flags):
49 self.window = SDL_CreateWindow(title, x, y, w, h, flags) 70 self.window = SDL_CreateWindow(title, x, y, w, h, flags)
153 174
154 175
155 cdef void ttf_init() except *: 176 cdef void ttf_init() except *:
156 if TTF_Init() < 0: 177 if TTF_Init() < 0:
157 raise SDLError(SDL_GetError()) 178 raise SDLError(SDL_GetError())
158
159
160 IF UNAME_SYSNAME == "Windows":
161 cdef void set_main_ready():
162 SDL_SetMainReady()
163 179
164 180
165 cdef void quit() nogil: 181 cdef void quit() nogil:
166 SDL_Quit() 182 SDL_Quit()
167 183
220 cdef void mix_open_audio(int frequency, Uint16 format_, int channels, int chunksize) except *: 236 cdef void mix_open_audio(int frequency, Uint16 format_, int channels, int chunksize) except *:
221 if Mix_OpenAudio(frequency, format_, channels, chunksize) < 0: 237 if Mix_OpenAudio(frequency, format_, channels, chunksize) < 0:
222 raise SDLError(SDL_GetError()) 238 raise SDLError(SDL_GetError())
223 239
224 240
225 cdef void mix_close_audio() nogil:
226 Mix_CloseAudio()
227
228
229 cdef void mix_allocate_channels(int numchans) except *: 241 cdef void mix_allocate_channels(int numchans) except *:
230 if Mix_AllocateChannels(numchans) != numchans: 242 if Mix_AllocateChannels(numchans) != numchans:
231 raise SDLError(SDL_GetError()) 243 raise SDLError(SDL_GetError())
232 244
233 245