annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 ##
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 ## Copyright (C) 2013 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 ##
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 ##
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 ## GNU General Public License for more details.
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 ##
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 INIT_VIDEO = SDL_INIT_VIDEO
420
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
16 INIT_PNG = IMG_INIT_PNG
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 GL_CONTEXT_MAJOR_VERSION = SDL_GL_CONTEXT_MAJOR_VERSION
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 GL_CONTEXT_MINOR_VERSION = SDL_GL_CONTEXT_MINOR_VERSION
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 GL_DOUBLEBUFFER = SDL_GL_DOUBLEBUFFER
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 GL_DEPTH_SIZE = SDL_GL_DEPTH_SIZE
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 WINDOWPOS_CENTERED = SDL_WINDOWPOS_CENTERED
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 WINDOW_OPENGL = SDL_WINDOW_OPENGL
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 WINDOW_SHOWN = SDL_WINDOW_SHOWN
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 SCANCODE_Z = SDL_SCANCODE_Z
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 SCANCODE_X = SDL_SCANCODE_X
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 SCANCODE_LSHIFT = SDL_SCANCODE_LSHIFT
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 SCANCODE_UP = SDL_SCANCODE_UP
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 SCANCODE_DOWN = SDL_SCANCODE_DOWN
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 SCANCODE_LEFT = SDL_SCANCODE_LEFT
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 SCANCODE_RIGHT = SDL_SCANCODE_RIGHT
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 SCANCODE_LCTRL = SDL_SCANCODE_LCTRL
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 SCANCODE_ESCAPE = SDL_SCANCODE_ESCAPE
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 KEYDOWN = SDL_KEYDOWN
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 QUIT = SDL_QUIT
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39
421
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
40 DEFAULT_FORMAT = MIX_DEFAULT_FORMAT
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
41
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 class SDLError(Exception):
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 pass
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 cdef class Window:
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 cdef SDL_Window *window
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 cdef SDL_GLContext context
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 def __init__(self, const char *title, int x, int y, int w, int h, Uint32 flags):
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 self.window = SDL_CreateWindow(title, x, y, w, h, flags)
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 if self.window == NULL:
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 raise SDLError(SDL_GetError())
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 def destroy_window(self):
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 SDL_DestroyWindow(self.window)
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 def gl_create_context(self):
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 self.context = SDL_GL_CreateContext(self.window)
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 def gl_swap_window(self):
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 SDL_GL_SwapWindow(self.window)
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 def gl_delete_context(self):
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 SDL_GL_DeleteContext(self.context)
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68
420
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
69 cdef class Surface:
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
70 cdef SDL_Surface *surface
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
71
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
72 def __dealloc__(self):
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
73 if self.surface != NULL:
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
74 SDL_FreeSurface(self.surface)
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
75
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
76 property width:
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
77 def __get__(self):
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
78 return self.surface.w
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
79
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
80 property height:
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
81 def __get__(self):
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
82 return self.surface.h
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
83
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
84 property pixels:
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
85 def __get__(self):
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
86 return bytes(self.surface.pixels[:self.surface.w * self.surface.h * 4])
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
87
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
88 def blit(self, Surface other):
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
89 if SDL_BlitSurface(other.surface, NULL, self.surface, NULL) < 0:
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
90 raise SDLError(SDL_GetError())
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
91
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
92 def set_alpha(self, Surface alpha_surface):
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
93 nb_pixels = self.surface.w * self.surface.h
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
94 image = self.surface.pixels
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
95 alpha = alpha_surface.surface.pixels
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
96
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
97 for i in xrange(nb_pixels):
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
98 # Only use the red value, assume the others are equal.
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
99 image[3+4*i] = alpha[3*i]
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
100
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
101
421
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
102 cdef class Music:
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
103 cdef Mix_Music *music
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
104
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
105 def __dealloc__(self):
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
106 if self.music != NULL:
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
107 Mix_FreeMusic(self.music)
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
108
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
109 def play(self, int loops):
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
110 Mix_PlayMusic(self.music, loops)
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
111
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
112 def set_loop_points(self, double start, double end):
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
113 #Mix_SetLoopPoints(self.music, start, end)
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
114 pass
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
115
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
116
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
117 cdef class Chunk:
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
118 cdef Mix_Chunk *chunk
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
119
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
120 def __dealloc__(self):
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
121 if self.chunk != NULL:
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
122 Mix_FreeChunk(self.chunk)
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
123
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
124 property volume:
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
125 def __set__(self, float volume):
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
126 Mix_VolumeChunk(self.chunk, int(volume * 128))
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
127
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
128 def play(self, int channel, int loops):
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
129 Mix_PlayChannel(channel, self.chunk, loops)
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
130
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
131
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132 def init(Uint32 flags):
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133 if SDL_Init(flags) < 0:
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134 raise SDLError(SDL_GetError())
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
136
420
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
137 def img_init(Uint32 flags):
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
138 if IMG_Init(flags) != flags:
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
139 raise SDLError(SDL_GetError())
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
140
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
141
421
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
142 def mix_init(int flags):
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
143 if Mix_Init(flags) != flags:
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
144 raise SDLError(SDL_GetError())
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
145
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
146
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
147 def quit():
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
148 SDL_Quit()
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
150
420
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
151 def img_quit():
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
152 IMG_Quit()
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
153
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
154
421
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
155 def mix_quit():
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
156 Mix_Quit()
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
157
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
158
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
159 def gl_set_attribute(SDL_GLattr attr, int value):
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
160 if SDL_GL_SetAttribute(attr, value) < 0:
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
161 raise SDLError(SDL_GetError())
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
162
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
163
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
164 def poll_events():
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
165 cdef SDL_Event event
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
166 ret = []
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
167 while SDL_PollEvent(&event):
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
168 if event.type == SDL_KEYDOWN:
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
169 ret.append((event.type, event.key.keysym.scancode))
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
170 elif event.type == SDL_QUIT:
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
171 ret.append((event.type,))
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172 return ret
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
173
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
174
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
175 def get_keyboard_state():
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
176 cdef int numkeys
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
177 cdef bint k
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
178 cdef const Uint8 *state
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
179 state = SDL_GetKeyboardState(&numkeys)
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
180 return tuple([k is not False for k in state[:numkeys]])
419
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
181
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
182
420
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
183 def load_png(file_):
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
184 data = file_.read()
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
185 rwops = SDL_RWFromConstMem(<char*>data, len(data))
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
186 surface = Surface()
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
187 surface.surface = IMG_LoadPNG_RW(rwops)
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
188 SDL_RWclose(rwops)
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
189 if surface.surface == NULL:
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
190 raise SDLError(SDL_GetError())
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
191 return surface
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
192
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
193
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
194 def create_rgb_surface(int width, int height, int depth, Uint32 rmask=0, Uint32 gmask=0, Uint32 bmask=0, Uint32 amask=0):
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
195 surface = Surface()
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
196 surface.surface = SDL_CreateRGBSurface(0, width, height, depth, rmask, gmask, bmask, amask)
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
197 if surface.surface == NULL:
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
198 raise SDLError(SDL_GetError())
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
199 return surface
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
200
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
201
421
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
202 def mix_open_audio(int frequency, Uint16 format_, int channels, int chunksize):
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
203 if Mix_OpenAudio(frequency, format_, channels, chunksize) < 0:
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
204 raise SDLError(SDL_GetError())
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
205
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
206
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
207 def mix_close_audio():
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
208 Mix_CloseAudio()
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
209
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
210
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
211 def mix_allocate_channels(int numchans):
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
212 if Mix_AllocateChannels(numchans) != numchans:
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
213 raise SDLError(SDL_GetError())
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
214
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
215
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
216 def mix_volume(int channel, float volume):
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
217 return Mix_Volume(channel, int(volume * 128))
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
218
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
219
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
220 def mix_volume_music(float volume):
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
221 return Mix_VolumeMusic(int(volume * 128))
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
222
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
223
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
224 def load_music(const char *filename):
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
225 music = Music()
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
226 music.music = Mix_LoadMUS(filename)
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
227 if music.music == NULL:
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
228 raise SDLError(SDL_GetError())
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
229 return music
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
230
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
231
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
232 def load_chunk(file_):
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
233 cdef SDL_RWops *rwops
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
234 chunk = Chunk()
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
235 data = file_.read()
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
236 rwops = SDL_RWFromConstMem(<char*>data, len(data))
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
237 chunk.chunk = Mix_LoadWAV_RW(rwops, 1)
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
238 if chunk.chunk == NULL:
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
239 raise SDLError(SDL_GetError())
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
240 return chunk
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
241
b1248bab2d0f Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 420
diff changeset
242
419
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
243 def get_ticks():
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
244 return SDL_GetTicks()
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
245
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
246
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
247 def delay(Uint32 ms):
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
248 SDL_Delay(ms)