Mercurial > touhou
comparison pytouhou/lib/sdl.pyx @ 421:b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 16 Jul 2013 21:07:15 +0200 |
parents | 3a7b36324611 |
children | 52829ebe2561 |
comparison
equal
deleted
inserted
replaced
420:3a7b36324611 | 421:b1248bab2d0f |
---|---|
35 SCANCODE_ESCAPE = SDL_SCANCODE_ESCAPE | 35 SCANCODE_ESCAPE = SDL_SCANCODE_ESCAPE |
36 | 36 |
37 KEYDOWN = SDL_KEYDOWN | 37 KEYDOWN = SDL_KEYDOWN |
38 QUIT = SDL_QUIT | 38 QUIT = SDL_QUIT |
39 | 39 |
40 DEFAULT_FORMAT = MIX_DEFAULT_FORMAT | |
41 | |
40 | 42 |
41 class SDLError(Exception): | 43 class SDLError(Exception): |
42 pass | 44 pass |
43 | 45 |
44 | 46 |
95 for i in xrange(nb_pixels): | 97 for i in xrange(nb_pixels): |
96 # Only use the red value, assume the others are equal. | 98 # Only use the red value, assume the others are equal. |
97 image[3+4*i] = alpha[3*i] | 99 image[3+4*i] = alpha[3*i] |
98 | 100 |
99 | 101 |
102 cdef class Music: | |
103 cdef Mix_Music *music | |
104 | |
105 def __dealloc__(self): | |
106 if self.music != NULL: | |
107 Mix_FreeMusic(self.music) | |
108 | |
109 def play(self, int loops): | |
110 Mix_PlayMusic(self.music, loops) | |
111 | |
112 def set_loop_points(self, double start, double end): | |
113 #Mix_SetLoopPoints(self.music, start, end) | |
114 pass | |
115 | |
116 | |
117 cdef class Chunk: | |
118 cdef Mix_Chunk *chunk | |
119 | |
120 def __dealloc__(self): | |
121 if self.chunk != NULL: | |
122 Mix_FreeChunk(self.chunk) | |
123 | |
124 property volume: | |
125 def __set__(self, float volume): | |
126 Mix_VolumeChunk(self.chunk, int(volume * 128)) | |
127 | |
128 def play(self, int channel, int loops): | |
129 Mix_PlayChannel(channel, self.chunk, loops) | |
130 | |
131 | |
100 def init(Uint32 flags): | 132 def init(Uint32 flags): |
101 if SDL_Init(flags) < 0: | 133 if SDL_Init(flags) < 0: |
102 raise SDLError(SDL_GetError()) | 134 raise SDLError(SDL_GetError()) |
103 | 135 |
104 | 136 |
105 def img_init(Uint32 flags): | 137 def img_init(Uint32 flags): |
106 if IMG_Init(flags) != flags: | 138 if IMG_Init(flags) != flags: |
107 raise SDLError(SDL_GetError()) | 139 raise SDLError(SDL_GetError()) |
108 | 140 |
109 | 141 |
142 def mix_init(int flags): | |
143 if Mix_Init(flags) != flags: | |
144 raise SDLError(SDL_GetError()) | |
145 | |
146 | |
110 def quit(): | 147 def quit(): |
111 SDL_Quit() | 148 SDL_Quit() |
112 | 149 |
113 | 150 |
114 def img_quit(): | 151 def img_quit(): |
115 IMG_Quit() | 152 IMG_Quit() |
153 | |
154 | |
155 def mix_quit(): | |
156 Mix_Quit() | |
116 | 157 |
117 | 158 |
118 def gl_set_attribute(SDL_GLattr attr, int value): | 159 def gl_set_attribute(SDL_GLattr attr, int value): |
119 if SDL_GL_SetAttribute(attr, value) < 0: | 160 if SDL_GL_SetAttribute(attr, value) < 0: |
120 raise SDLError(SDL_GetError()) | 161 raise SDLError(SDL_GetError()) |
156 if surface.surface == NULL: | 197 if surface.surface == NULL: |
157 raise SDLError(SDL_GetError()) | 198 raise SDLError(SDL_GetError()) |
158 return surface | 199 return surface |
159 | 200 |
160 | 201 |
202 def mix_open_audio(int frequency, Uint16 format_, int channels, int chunksize): | |
203 if Mix_OpenAudio(frequency, format_, channels, chunksize) < 0: | |
204 raise SDLError(SDL_GetError()) | |
205 | |
206 | |
207 def mix_close_audio(): | |
208 Mix_CloseAudio() | |
209 | |
210 | |
211 def mix_allocate_channels(int numchans): | |
212 if Mix_AllocateChannels(numchans) != numchans: | |
213 raise SDLError(SDL_GetError()) | |
214 | |
215 | |
216 def mix_volume(int channel, float volume): | |
217 return Mix_Volume(channel, int(volume * 128)) | |
218 | |
219 | |
220 def mix_volume_music(float volume): | |
221 return Mix_VolumeMusic(int(volume * 128)) | |
222 | |
223 | |
224 def load_music(const char *filename): | |
225 music = Music() | |
226 music.music = Mix_LoadMUS(filename) | |
227 if music.music == NULL: | |
228 raise SDLError(SDL_GetError()) | |
229 return music | |
230 | |
231 | |
232 def load_chunk(file_): | |
233 cdef SDL_RWops *rwops | |
234 chunk = Chunk() | |
235 data = file_.read() | |
236 rwops = SDL_RWFromConstMem(<char*>data, len(data)) | |
237 chunk.chunk = Mix_LoadWAV_RW(rwops, 1) | |
238 if chunk.chunk == NULL: | |
239 raise SDLError(SDL_GetError()) | |
240 return chunk | |
241 | |
242 | |
161 def get_ticks(): | 243 def get_ticks(): |
162 return SDL_GetTicks() | 244 return SDL_GetTicks() |
163 | 245 |
164 | 246 |
165 def delay(Uint32 ms): | 247 def delay(Uint32 ms): |