comparison pytouhou/lib/sdl.pyx @ 591:2dfa4aa135d2

Make the SDLError exception class automatically call SDL_GetError().
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 15 Oct 2014 13:17:07 +0200
parents e15672733c93
children 19d930f9e3f0
comparison
equal deleted inserted replaced
590:e15672733c93 591:2dfa4aa135d2
47 QUIT = SDL_QUIT 47 QUIT = SDL_QUIT
48 WINDOWEVENT = SDL_WINDOWEVENT 48 WINDOWEVENT = SDL_WINDOWEVENT
49 49
50 50
51 class SDLError(Exception): 51 class SDLError(Exception):
52 pass 52 def __init__(self):
53 error = SDL_GetError()
54 Exception.__init__(self, error.decode())
53 55
54 56
55 class SDL(object): 57 class SDL(object):
56 def __init__(self, sound=True): 58 def __init__(self, sound=True):
57 self.sound = sound 59 self.sound = sound
91 93
92 cdef class Window: 94 cdef class Window:
93 def __init__(self, str title, int x, int y, int w, int h, Uint32 flags): 95 def __init__(self, str title, int x, int y, int w, int h, Uint32 flags):
94 self.window = SDL_CreateWindow(title.encode(), x, y, w, h, flags) 96 self.window = SDL_CreateWindow(title.encode(), x, y, w, h, flags)
95 if self.window == NULL: 97 if self.window == NULL:
96 raise SDLError(SDL_GetError()) 98 raise SDLError()
97 99
98 def __dealloc__(self): 100 def __dealloc__(self):
99 if self.context != NULL: 101 if self.context != NULL:
100 SDL_GL_DeleteContext(self.context) 102 SDL_GL_DeleteContext(self.context)
101 if self.window != NULL: 103 if self.window != NULL:
102 SDL_DestroyWindow(self.window) 104 SDL_DestroyWindow(self.window)
103 105
104 cdef void gl_create_context(self) except *: 106 cdef void gl_create_context(self) except *:
105 self.context = SDL_GL_CreateContext(self.window) 107 self.context = SDL_GL_CreateContext(self.window)
106 if self.context == NULL: 108 if self.context == NULL:
107 raise SDLError(SDL_GetError()) 109 raise SDLError()
108 110
109 cdef void present(self) nogil: 111 cdef void present(self) nogil:
110 if self.renderer == NULL: 112 if self.renderer == NULL:
111 SDL_GL_SwapWindow(self.window) 113 SDL_GL_SwapWindow(self.window)
112 else: 114 else:
117 119
118 # The following functions are there for the pure SDL backend. 120 # The following functions are there for the pure SDL backend.
119 cdef void create_renderer(self, Uint32 flags): 121 cdef void create_renderer(self, Uint32 flags):
120 self.renderer = SDL_CreateRenderer(self.window, -1, flags) 122 self.renderer = SDL_CreateRenderer(self.window, -1, flags)
121 if self.renderer == NULL: 123 if self.renderer == NULL:
122 raise SDLError(SDL_GetError()) 124 raise SDLError()
123 125
124 cdef void render_clear(self): 126 cdef void render_clear(self):
125 ret = SDL_RenderClear(self.renderer) 127 ret = SDL_RenderClear(self.renderer)
126 if ret == -1: 128 if ret == -1:
127 raise SDLError(SDL_GetError()) 129 raise SDLError()
128 130
129 cdef void render_copy(self, Texture texture, Rect srcrect, Rect dstrect): 131 cdef void render_copy(self, Texture texture, Rect srcrect, Rect dstrect):
130 ret = SDL_RenderCopy(self.renderer, texture.texture, &srcrect.rect, &dstrect.rect) 132 ret = SDL_RenderCopy(self.renderer, texture.texture, &srcrect.rect, &dstrect.rect)
131 if ret == -1: 133 if ret == -1:
132 raise SDLError(SDL_GetError()) 134 raise SDLError()
133 135
134 cdef void render_copy_ex(self, Texture texture, Rect srcrect, Rect dstrect, double angle, bint flip): 136 cdef void render_copy_ex(self, Texture texture, Rect srcrect, Rect dstrect, double angle, bint flip):
135 ret = SDL_RenderCopyEx(self.renderer, texture.texture, &srcrect.rect, &dstrect.rect, angle, NULL, flip) 137 ret = SDL_RenderCopyEx(self.renderer, texture.texture, &srcrect.rect, &dstrect.rect, angle, NULL, flip)
136 if ret == -1: 138 if ret == -1:
137 raise SDLError(SDL_GetError()) 139 raise SDLError()
138 140
139 cdef void render_set_clip_rect(self, Rect rect): 141 cdef void render_set_clip_rect(self, Rect rect):
140 ret = SDL_RenderSetClipRect(self.renderer, &rect.rect) 142 ret = SDL_RenderSetClipRect(self.renderer, &rect.rect)
141 if ret == -1: 143 if ret == -1:
142 raise SDLError(SDL_GetError()) 144 raise SDLError()
143 145
144 cdef void render_set_viewport(self, Rect rect): 146 cdef void render_set_viewport(self, Rect rect):
145 ret = SDL_RenderSetViewport(self.renderer, &rect.rect) 147 ret = SDL_RenderSetViewport(self.renderer, &rect.rect)
146 if ret == -1: 148 if ret == -1:
147 raise SDLError(SDL_GetError()) 149 raise SDLError()
148 150
149 cdef Texture create_texture_from_surface(self, Surface surface): 151 cdef Texture create_texture_from_surface(self, Surface surface):
150 texture = Texture() 152 texture = Texture()
151 texture.texture = SDL_CreateTextureFromSurface(self.renderer, surface.surface) 153 texture.texture = SDL_CreateTextureFromSurface(self.renderer, surface.surface)
152 if texture.texture == NULL: 154 if texture.texture == NULL:
153 raise SDLError(SDL_GetError()) 155 raise SDLError()
154 return texture 156 return texture
155 157
156 158
157 cdef class Texture: 159 cdef class Texture:
158 cpdef set_color_mod(self, Uint8 r, Uint8 g, Uint8 b): 160 cpdef set_color_mod(self, Uint8 r, Uint8 g, Uint8 b):
159 ret = SDL_SetTextureColorMod(self.texture, r, g, b) 161 ret = SDL_SetTextureColorMod(self.texture, r, g, b)
160 if ret == -1: 162 if ret == -1:
161 raise SDLError(SDL_GetError()) 163 raise SDLError()
162 164
163 cpdef set_alpha_mod(self, Uint8 alpha): 165 cpdef set_alpha_mod(self, Uint8 alpha):
164 ret = SDL_SetTextureAlphaMod(self.texture, alpha) 166 ret = SDL_SetTextureAlphaMod(self.texture, alpha)
165 if ret == -1: 167 if ret == -1:
166 raise SDLError(SDL_GetError()) 168 raise SDLError()
167 169
168 cpdef set_blend_mode(self, SDL_BlendMode blend_mode): 170 cpdef set_blend_mode(self, SDL_BlendMode blend_mode):
169 ret = SDL_SetTextureBlendMode(self.texture, blend_mode) 171 ret = SDL_SetTextureBlendMode(self.texture, blend_mode)
170 if ret == -1: 172 if ret == -1:
171 raise SDLError(SDL_GetError()) 173 raise SDLError()
172 174
173 175
174 cdef class Rect: 176 cdef class Rect:
175 def __init__(self, int x, int y, int w, int h): 177 def __init__(self, int x, int y, int w, int h):
176 self.rect.x = x 178 self.rect.x = x
196 def __get__(self): 198 def __get__(self):
197 return bytes(self.surface.pixels[:self.surface.w * self.surface.h * 4]) 199 return bytes(self.surface.pixels[:self.surface.w * self.surface.h * 4])
198 200
199 cdef void blit(self, Surface other): 201 cdef void blit(self, Surface other):
200 if SDL_BlitSurface(other.surface, NULL, self.surface, NULL) < 0: 202 if SDL_BlitSurface(other.surface, NULL, self.surface, NULL) < 0:
201 raise SDLError(SDL_GetError()) 203 raise SDLError()
202 204
203 cdef void set_alpha(self, Surface alpha_surface) nogil: 205 cdef void set_alpha(self, Surface alpha_surface) nogil:
204 nb_pixels = self.surface.w * self.surface.h 206 nb_pixels = self.surface.w * self.surface.h
205 image = self.surface.pixels 207 image = self.surface.pixels
206 alpha = alpha_surface.surface.pixels 208 alpha = alpha_surface.surface.pixels
237 239
238 cdef class Font: 240 cdef class Font:
239 def __init__(self, str filename, int ptsize): 241 def __init__(self, str filename, int ptsize):
240 self.font = TTF_OpenFont(filename.encode(), ptsize) 242 self.font = TTF_OpenFont(filename.encode(), ptsize)
241 if self.font == NULL: 243 if self.font == NULL:
242 raise SDLError(SDL_GetError()) 244 raise SDLError()
243 245
244 def __dealloc__(self): 246 def __dealloc__(self):
245 if self.font != NULL: 247 if self.font != NULL:
246 TTF_CloseFont(self.font) 248 TTF_CloseFont(self.font)
247 249
250 white = SDL_Color(255, 255, 255, 255) 252 white = SDL_Color(255, 255, 255, 255)
251 surface = Surface() 253 surface = Surface()
252 string = text.encode('utf-8') 254 string = text.encode('utf-8')
253 surface.surface = TTF_RenderUTF8_Blended(self.font, string, white) 255 surface.surface = TTF_RenderUTF8_Blended(self.font, string, white)
254 if surface.surface == NULL: 256 if surface.surface == NULL:
255 raise SDLError(SDL_GetError()) 257 raise SDLError()
256 return surface 258 return surface
257 259
258 260
259 cdef void init(Uint32 flags) except *: 261 cdef void init(Uint32 flags) except *:
260 if SDL_Init(flags) < 0: 262 if SDL_Init(flags) < 0:
261 raise SDLError(SDL_GetError()) 263 raise SDLError()
262 264
263 265
264 cdef void img_init(int flags) except *: 266 cdef void img_init(int flags) except *:
265 if IMG_Init(flags) != flags: 267 if IMG_Init(flags) != flags:
266 raise SDLError(SDL_GetError()) 268 raise SDLError()
267 269
268 270
269 cdef void mix_init(int flags) except *: 271 cdef void mix_init(int flags) except *:
270 if Mix_Init(flags) != flags: 272 if Mix_Init(flags) != flags:
271 raise SDLError(SDL_GetError()) 273 raise SDLError()
272 274
273 275
274 cdef void ttf_init() except *: 276 cdef void ttf_init() except *:
275 if TTF_Init() < 0: 277 if TTF_Init() < 0:
276 raise SDLError(SDL_GetError()) 278 raise SDLError()
277 279
278 280
279 cdef void gl_set_attribute(SDL_GLattr attr, int value) except *: 281 cdef void gl_set_attribute(SDL_GLattr attr, int value) except *:
280 if SDL_GL_SetAttribute(attr, value) < 0: 282 if SDL_GL_SetAttribute(attr, value) < 0:
281 raise SDLError(SDL_GetError()) 283 raise SDLError()
282 284
283 cdef int gl_set_swap_interval(int interval) except *: 285 cdef int gl_set_swap_interval(int interval) except *:
284 if SDL_GL_SetSwapInterval(interval) < 0: 286 if SDL_GL_SetSwapInterval(interval) < 0:
285 raise SDLError(SDL_GetError()) 287 raise SDLError()
286 288
287 289
288 cdef list poll_events(): 290 cdef list poll_events():
289 cdef SDL_Event event 291 cdef SDL_Event event
290 ret = [] 292 ret = []
303 rwops = SDL_RWFromConstMem(<char*>data, len(data)) 305 rwops = SDL_RWFromConstMem(<char*>data, len(data))
304 surface = Surface() 306 surface = Surface()
305 surface.surface = IMG_LoadPNG_RW(rwops) 307 surface.surface = IMG_LoadPNG_RW(rwops)
306 SDL_RWclose(rwops) 308 SDL_RWclose(rwops)
307 if surface.surface == NULL: 309 if surface.surface == NULL:
308 raise SDLError(SDL_GetError()) 310 raise SDLError()
309 return surface 311 return surface
310 312
311 313
312 cdef Surface create_rgb_surface(int width, int height, int depth, Uint32 rmask=0, Uint32 gmask=0, Uint32 bmask=0, Uint32 amask=0): 314 cdef Surface create_rgb_surface(int width, int height, int depth, Uint32 rmask=0, Uint32 gmask=0, Uint32 bmask=0, Uint32 amask=0):
313 surface = Surface() 315 surface = Surface()
314 surface.surface = SDL_CreateRGBSurface(0, width, height, depth, rmask, gmask, bmask, amask) 316 surface.surface = SDL_CreateRGBSurface(0, width, height, depth, rmask, gmask, bmask, amask)
315 if surface.surface == NULL: 317 if surface.surface == NULL:
316 raise SDLError(SDL_GetError()) 318 raise SDLError()
317 return surface 319 return surface
318 320
319 321
320 cdef void mix_open_audio(int frequency, Uint16 format_, int channels, int chunksize) except *: 322 cdef void mix_open_audio(int frequency, Uint16 format_, int channels, int chunksize) except *:
321 if Mix_OpenAudio(frequency, format_, channels, chunksize) < 0: 323 if Mix_OpenAudio(frequency, format_, channels, chunksize) < 0:
322 raise SDLError(SDL_GetError()) 324 raise SDLError()
323 325
324 326
325 cdef void mix_allocate_channels(int numchans) except *: 327 cdef void mix_allocate_channels(int numchans) except *:
326 if Mix_AllocateChannels(numchans) != numchans: 328 if Mix_AllocateChannels(numchans) != numchans:
327 raise SDLError(SDL_GetError()) 329 raise SDLError()
328 330
329 331
330 cdef int mix_volume(int channel, float volume) nogil: 332 cdef int mix_volume(int channel, float volume) nogil:
331 return Mix_Volume(channel, int(volume * 128)) 333 return Mix_Volume(channel, int(volume * 128))
332 334
337 339
338 cdef Music load_music(str filename): 340 cdef Music load_music(str filename):
339 music = Music() 341 music = Music()
340 music.music = Mix_LoadMUS(filename.encode()) 342 music.music = Mix_LoadMUS(filename.encode())
341 if music.music == NULL: 343 if music.music == NULL:
342 raise SDLError(SDL_GetError()) 344 raise SDLError()
343 return music 345 return music
344 346
345 347
346 cdef Chunk load_chunk(file_): 348 cdef Chunk load_chunk(file_):
347 cdef SDL_RWops *rwops 349 cdef SDL_RWops *rwops
348 chunk = Chunk() 350 chunk = Chunk()
349 data = file_.read() 351 data = file_.read()
350 rwops = SDL_RWFromConstMem(<char*>data, len(data)) 352 rwops = SDL_RWFromConstMem(<char*>data, len(data))
351 chunk.chunk = Mix_LoadWAV_RW(rwops, 1) 353 chunk.chunk = Mix_LoadWAV_RW(rwops, 1)
352 if chunk.chunk == NULL: 354 if chunk.chunk == NULL:
353 raise SDLError(SDL_GetError()) 355 raise SDLError()
354 return chunk 356 return chunk
355 357
356 358
357 cdef Uint32 get_ticks() nogil: 359 cdef Uint32 get_ticks() nogil:
358 return SDL_GetTicks() 360 return SDL_GetTicks()