comparison pytouhou/lib/sdl.pyx @ 455:6864a38b2413

Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 02 Sep 2013 22:16:38 +0200
parents 17a7f3b028f3
children cae1ae9de430
comparison
equal deleted inserted replaced
454:a502887557ac 455:6864a38b2413
43 class SDLError(Exception): 43 class SDLError(Exception):
44 pass 44 pass
45 45
46 46
47 cdef class Window: 47 cdef class Window:
48 cdef SDL_Window *window
49 cdef SDL_GLContext context
50
51 def __init__(self, const char *title, int x, int y, int w, int h, Uint32 flags): 48 def __init__(self, const char *title, int x, int y, int w, int h, Uint32 flags):
52 self.window = SDL_CreateWindow(title, x, y, w, h, flags) 49 self.window = SDL_CreateWindow(title, x, y, w, h, flags)
53 if self.window == NULL: 50 if self.window == NULL:
54 raise SDLError(SDL_GetError()) 51 raise SDLError(SDL_GetError())
55 52
56 def destroy_window(self): 53 def __dealloc__(self):
57 SDL_DestroyWindow(self.window) 54 if self.context != NULL:
58 55 SDL_GL_DeleteContext(self.context)
59 def gl_create_context(self): 56 if self.window != NULL:
57 SDL_DestroyWindow(self.window)
58
59 cdef void gl_create_context(self) except *:
60 self.context = SDL_GL_CreateContext(self.window) 60 self.context = SDL_GL_CreateContext(self.window)
61 if self.context == NULL: 61 if self.context == NULL:
62 raise SDLError(SDL_GetError()) 62 raise SDLError(SDL_GetError())
63 63
64 def gl_swap_window(self): 64 cdef void gl_swap_window(self) nogil:
65 SDL_GL_SwapWindow(self.window) 65 SDL_GL_SwapWindow(self.window)
66 66
67 def gl_delete_context(self): 67 cdef void set_window_size(self, int width, int height) nogil:
68 SDL_GL_DeleteContext(self.context)
69
70 def set_window_size(self, width, height):
71 SDL_SetWindowSize(self.window, width, height) 68 SDL_SetWindowSize(self.window, width, height)
72 69
73 70
74 cdef class Surface: 71 cdef class Surface:
75 cdef SDL_Surface *surface
76
77 def __dealloc__(self): 72 def __dealloc__(self):
78 if self.surface != NULL: 73 if self.surface != NULL:
79 SDL_FreeSurface(self.surface) 74 SDL_FreeSurface(self.surface)
80
81 property width:
82 def __get__(self):
83 return self.surface.w
84
85 property height:
86 def __get__(self):
87 return self.surface.h
88 75
89 property pixels: 76 property pixels:
90 def __get__(self): 77 def __get__(self):
91 return bytes(self.surface.pixels[:self.surface.w * self.surface.h * 4]) 78 return bytes(self.surface.pixels[:self.surface.w * self.surface.h * 4])
92 79
93 def blit(self, Surface other): 80 cdef void blit(self, Surface other):
94 if SDL_BlitSurface(other.surface, NULL, self.surface, NULL) < 0: 81 if SDL_BlitSurface(other.surface, NULL, self.surface, NULL) < 0:
95 raise SDLError(SDL_GetError()) 82 raise SDLError(SDL_GetError())
96 83
97 def set_alpha(self, Surface alpha_surface): 84 cdef void set_alpha(self, Surface alpha_surface) nogil:
98 nb_pixels = self.surface.w * self.surface.h 85 nb_pixels = self.surface.w * self.surface.h
99 image = self.surface.pixels 86 image = self.surface.pixels
100 alpha = alpha_surface.surface.pixels 87 alpha = alpha_surface.surface.pixels
101 88
102 for i in xrange(nb_pixels): 89 for i in xrange(nb_pixels):
103 # Only use the red value, assume the others are equal. 90 # Only use the red value, assume the others are equal.
104 image[3+4*i] = alpha[3*i] 91 image[3+4*i] = alpha[3*i]
105 92
106 93
107 cdef class Music: 94 cdef class Music:
108 cdef Mix_Music *music
109
110 def __dealloc__(self): 95 def __dealloc__(self):
111 if self.music != NULL: 96 if self.music != NULL:
112 Mix_FreeMusic(self.music) 97 Mix_FreeMusic(self.music)
113 98
114 def play(self, int loops): 99 cdef void play(self, int loops) nogil:
115 Mix_PlayMusic(self.music, loops) 100 Mix_PlayMusic(self.music, loops)
116 101
117 def set_loop_points(self, double start, double end): 102 cdef void set_loop_points(self, double start, double end) nogil:
118 #Mix_SetLoopPoints(self.music, start, end) 103 #Mix_SetLoopPoints(self.music, start, end)
119 pass 104 pass
120 105
121 106
122 cdef class Chunk: 107 cdef class Chunk:
123 cdef Mix_Chunk *chunk
124
125 def __dealloc__(self): 108 def __dealloc__(self):
126 if self.chunk != NULL: 109 if self.chunk != NULL:
127 Mix_FreeChunk(self.chunk) 110 Mix_FreeChunk(self.chunk)
128 111
129 property volume: 112 cdef void play(self, int channel, int loops) nogil:
130 def __set__(self, float volume):
131 Mix_VolumeChunk(self.chunk, int(volume * 128))
132
133 def play(self, int channel, int loops):
134 Mix_PlayChannel(channel, self.chunk, loops) 113 Mix_PlayChannel(channel, self.chunk, loops)
135 114
136 115 cdef void set_volume(self, float volume) nogil:
137 def init(Uint32 flags): 116 Mix_VolumeChunk(self.chunk, int(volume * 128))
117
118
119 cdef void init(Uint32 flags) except *:
138 if SDL_Init(flags) < 0: 120 if SDL_Init(flags) < 0:
139 raise SDLError(SDL_GetError()) 121 raise SDLError(SDL_GetError())
140 122
141 123
142 def img_init(Uint32 flags): 124 cdef void img_init(Uint32 flags) except *:
143 if IMG_Init(flags) != flags: 125 if IMG_Init(flags) != flags:
144 raise SDLError(SDL_GetError()) 126 raise SDLError(SDL_GetError())
145 127
146 128
147 def mix_init(int flags): 129 cdef void mix_init(int flags) except *:
148 if Mix_Init(flags) != flags: 130 if Mix_Init(flags) != flags:
149 raise SDLError(SDL_GetError()) 131 raise SDLError(SDL_GetError())
150 132
151 133
152 IF UNAME_SYSNAME == "Windows": 134 IF UNAME_SYSNAME == "Windows":
153 def set_main_ready(): 135 cdef void set_main_ready():
154 SDL_SetMainReady() 136 SDL_SetMainReady()
155 137
156 138
157 def quit(): 139 cdef void quit() nogil:
158 SDL_Quit() 140 SDL_Quit()
159 141
160 142
161 def img_quit(): 143 cdef void img_quit() nogil:
162 IMG_Quit() 144 IMG_Quit()
163 145
164 146
165 def mix_quit(): 147 cdef void mix_quit() nogil:
166 Mix_Quit() 148 Mix_Quit()
167 149
168 150
169 def gl_set_attribute(SDL_GLattr attr, int value): 151 cdef void gl_set_attribute(SDL_GLattr attr, int value) except *:
170 if SDL_GL_SetAttribute(attr, value) < 0: 152 if SDL_GL_SetAttribute(attr, value) < 0:
171 raise SDLError(SDL_GetError()) 153 raise SDLError(SDL_GetError())
172 154
173 155
174 def poll_events(): 156 cdef list poll_events():
175 cdef SDL_Event event 157 cdef SDL_Event event
176 ret = [] 158 ret = []
177 while SDL_PollEvent(&event): 159 while SDL_PollEvent(&event):
178 if event.type == SDL_KEYDOWN: 160 if event.type == SDL_KEYDOWN:
179 ret.append((event.type, event.key.keysym.scancode)) 161 ret.append((event.type, event.key.keysym.scancode))
180 elif event.type == SDL_QUIT: 162 elif event.type == SDL_QUIT:
181 ret.append((event.type,)) 163 ret.append((event.type,))
182 return ret 164 return ret
183 165
184 166
185 def get_keyboard_state(): 167 cdef const Uint8* get_keyboard_state() nogil:
186 cdef int numkeys 168 return SDL_GetKeyboardState(NULL)
187 cdef bint k 169
188 cdef const Uint8 *state 170
189 state = SDL_GetKeyboardState(&numkeys) 171 cdef Surface load_png(file_):
190 return tuple([k is not False for k in state[:numkeys]])
191
192
193 def load_png(file_):
194 data = file_.read() 172 data = file_.read()
195 rwops = SDL_RWFromConstMem(<char*>data, len(data)) 173 rwops = SDL_RWFromConstMem(<char*>data, len(data))
196 surface = Surface() 174 surface = Surface()
197 surface.surface = IMG_LoadPNG_RW(rwops) 175 surface.surface = IMG_LoadPNG_RW(rwops)
198 SDL_RWclose(rwops) 176 SDL_RWclose(rwops)
199 if surface.surface == NULL: 177 if surface.surface == NULL:
200 raise SDLError(SDL_GetError()) 178 raise SDLError(SDL_GetError())
201 return surface 179 return surface
202 180
203 181
204 def create_rgb_surface(int width, int height, int depth, Uint32 rmask=0, Uint32 gmask=0, Uint32 bmask=0, Uint32 amask=0): 182 cdef Surface create_rgb_surface(int width, int height, int depth, Uint32 rmask=0, Uint32 gmask=0, Uint32 bmask=0, Uint32 amask=0):
205 surface = Surface() 183 surface = Surface()
206 surface.surface = SDL_CreateRGBSurface(0, width, height, depth, rmask, gmask, bmask, amask) 184 surface.surface = SDL_CreateRGBSurface(0, width, height, depth, rmask, gmask, bmask, amask)
207 if surface.surface == NULL: 185 if surface.surface == NULL:
208 raise SDLError(SDL_GetError()) 186 raise SDLError(SDL_GetError())
209 return surface 187 return surface
210 188
211 189
212 def mix_open_audio(int frequency, Uint16 format_, int channels, int chunksize): 190 cdef void mix_open_audio(int frequency, Uint16 format_, int channels, int chunksize) except *:
213 if Mix_OpenAudio(frequency, format_, channels, chunksize) < 0: 191 if Mix_OpenAudio(frequency, format_, channels, chunksize) < 0:
214 raise SDLError(SDL_GetError()) 192 raise SDLError(SDL_GetError())
215 193
216 194
217 def mix_close_audio(): 195 cdef void mix_close_audio() nogil:
218 Mix_CloseAudio() 196 Mix_CloseAudio()
219 197
220 198
221 def mix_allocate_channels(int numchans): 199 cdef void mix_allocate_channels(int numchans) except *:
222 if Mix_AllocateChannels(numchans) != numchans: 200 if Mix_AllocateChannels(numchans) != numchans:
223 raise SDLError(SDL_GetError()) 201 raise SDLError(SDL_GetError())
224 202
225 203
226 def mix_volume(int channel, float volume): 204 cdef int mix_volume(int channel, float volume) nogil:
227 return Mix_Volume(channel, int(volume * 128)) 205 return Mix_Volume(channel, int(volume * 128))
228 206
229 207
230 def mix_volume_music(float volume): 208 cdef int mix_volume_music(float volume) nogil:
231 return Mix_VolumeMusic(int(volume * 128)) 209 return Mix_VolumeMusic(int(volume * 128))
232 210
233 211
234 def load_music(const char *filename): 212 cdef Music load_music(const char *filename):
235 music = Music() 213 music = Music()
236 music.music = Mix_LoadMUS(filename) 214 music.music = Mix_LoadMUS(filename)
237 if music.music == NULL: 215 if music.music == NULL:
238 raise SDLError(SDL_GetError()) 216 raise SDLError(SDL_GetError())
239 return music 217 return music
240 218
241 219
242 def load_chunk(file_): 220 cdef Chunk load_chunk(file_):
243 cdef SDL_RWops *rwops 221 cdef SDL_RWops *rwops
244 chunk = Chunk() 222 chunk = Chunk()
245 data = file_.read() 223 data = file_.read()
246 rwops = SDL_RWFromConstMem(<char*>data, len(data)) 224 rwops = SDL_RWFromConstMem(<char*>data, len(data))
247 chunk.chunk = Mix_LoadWAV_RW(rwops, 1) 225 chunk.chunk = Mix_LoadWAV_RW(rwops, 1)
248 if chunk.chunk == NULL: 226 if chunk.chunk == NULL:
249 raise SDLError(SDL_GetError()) 227 raise SDLError(SDL_GetError())
250 return chunk 228 return chunk
251 229
252 230
253 def get_ticks(): 231 cdef Uint32 get_ticks() nogil:
254 return SDL_GetTicks() 232 return SDL_GetTicks()
255 233
256 234
257 def delay(Uint32 ms): 235 cdef void delay(Uint32 ms) nogil:
258 SDL_Delay(ms) 236 SDL_Delay(ms)