annotate pytouhou/lib/sdl.pyx @ 721:bbf0da7de481

ecl_vm: work on laevateinn
author Gauvain "GovanifY" Roussel-Tarbouriech <gauvain@govanify.com>
date Mon, 28 Oct 2019 19:52:06 +0100
parents 80687f258001
children a6875f90c141
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
635
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
15 import pytouhou.lib.gui as gui
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
16
501
4778c482f24a Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 491
diff changeset
17 from pytouhou.utils.helpers import get_logger
4778c482f24a Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 491
diff changeset
18
4778c482f24a Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 491
diff changeset
19 logger = get_logger(__name__)
4778c482f24a Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 491
diff changeset
20
4778c482f24a Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 491
diff changeset
21
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
22 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
23 GL_CONTEXT_MINOR_VERSION = SDL_GL_CONTEXT_MINOR_VERSION
556
c34b23e29d16 Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 538
diff changeset
24 GL_CONTEXT_PROFILE_MASK = SDL_GL_CONTEXT_PROFILE_MASK
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
25 GL_DOUBLEBUFFER = SDL_GL_DOUBLEBUFFER
612
73f134f84c7f Request a RGB888 context, since SDL2’s default of RGB332 sucks.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
26 GL_RED_SIZE = SDL_GL_RED_SIZE
73f134f84c7f Request a RGB888 context, since SDL2’s default of RGB332 sucks.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
27 GL_GREEN_SIZE = SDL_GL_GREEN_SIZE
73f134f84c7f Request a RGB888 context, since SDL2’s default of RGB332 sucks.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
28 GL_BLUE_SIZE = SDL_GL_BLUE_SIZE
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
29 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
30
556
c34b23e29d16 Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 538
diff changeset
31 GL_CONTEXT_PROFILE_CORE = SDL_GL_CONTEXT_PROFILE_CORE
c34b23e29d16 Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 538
diff changeset
32 GL_CONTEXT_PROFILE_COMPATIBILITY = SDL_GL_CONTEXT_PROFILE_COMPATIBILITY
c34b23e29d16 Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 538
diff changeset
33 GL_CONTEXT_PROFILE_ES = SDL_GL_CONTEXT_PROFILE_ES
c34b23e29d16 Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 538
diff changeset
34
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
35 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
36 WINDOW_OPENGL = SDL_WINDOW_OPENGL
464
36bc577b2392 Make the window resizable, and scale its content correctly.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 460
diff changeset
37 WINDOW_RESIZABLE = SDL_WINDOW_RESIZABLE
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
38
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 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
40 SCANCODE_X = SDL_SCANCODE_X
592
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 591
diff changeset
41 SCANCODE_P = SDL_SCANCODE_P
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 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
43 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
44 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
45 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
46 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
47 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
48 SCANCODE_ESCAPE = SDL_SCANCODE_ESCAPE
592
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 591
diff changeset
49 SCANCODE_HOME = SDL_SCANCODE_HOME
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
50
464
36bc577b2392 Make the window resizable, and scale its content correctly.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 460
diff changeset
51 WINDOWEVENT_RESIZED = SDL_WINDOWEVENT_RESIZED
36bc577b2392 Make the window resizable, and scale its content correctly.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 460
diff changeset
52
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
53 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
54 QUIT = SDL_QUIT
464
36bc577b2392 Make the window resizable, and scale its content correctly.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 460
diff changeset
55 WINDOWEVENT = SDL_WINDOWEVENT
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
56
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
635
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
58 class SDLError(gui.Error):
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
59 def __init__(self):
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
60 error = SDL_GetError()
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
61 Exception.__init__(self, error.decode())
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
62
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
615
d1f0bb0b7a17 Don’t inherit explicitely from object, we are not on Python 2.7 anymore. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 612
diff changeset
64 class SDL:
460
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
65 def __init__(self, sound=True):
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
66 self.sound = sound
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
67
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
68 def __enter__(self):
538
1abb6f7db8cc Don’t retrieve the keyboard_state pointer at each frame.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 531
diff changeset
69 global keyboard_state
1abb6f7db8cc Don’t retrieve the keyboard_state pointer at each frame.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 531
diff changeset
70
460
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
71 IF UNAME_SYSNAME == "Windows":
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
72 SDL_SetMainReady()
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
73 init(SDL_INIT_VIDEO)
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
74 img_init(IMG_INIT_PNG)
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
75 ttf_init()
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
76
538
1abb6f7db8cc Don’t retrieve the keyboard_state pointer at each frame.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 531
diff changeset
77 keyboard_state = SDL_GetKeyboardState(NULL)
1abb6f7db8cc Don’t retrieve the keyboard_state pointer at each frame.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 531
diff changeset
78
460
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
79 if self.sound:
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
80 mix_init(0)
501
4778c482f24a Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 491
diff changeset
81 try:
4778c482f24a Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 491
diff changeset
82 mix_open_audio(44100, MIX_DEFAULT_FORMAT, 2, 4096)
4778c482f24a Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 491
diff changeset
83 except SDLError as error:
4778c482f24a Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 491
diff changeset
84 logger.error(u'Impossible to set up audio subsystem: %s', error)
4778c482f24a Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 491
diff changeset
85 self.sound = False
4778c482f24a Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 491
diff changeset
86 else:
528
7c3c90468996 Inherit music players from a base class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 512
diff changeset
87 # TODO: make it dependent on the number of sound files in the
7c3c90468996 Inherit music players from a base class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 512
diff changeset
88 # archives.
7c3c90468996 Inherit music players from a base class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 512
diff changeset
89 mix_allocate_channels(MAX_SOUNDS)
460
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
90
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
91 def __exit__(self, *args):
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
92 if self.sound:
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
93 Mix_CloseAudio()
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
94 Mix_Quit()
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
95
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
96 TTF_Quit()
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
97 IMG_Quit()
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
98 SDL_Quit()
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
99
ec327e58b477 Add a context manager to initialize and shut down SDL outside of Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
100
635
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
101 cdef class Window(gui.Window):
590
e15672733c93 Switch to Python 3.x instead of 2.7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 589
diff changeset
102 def __init__(self, str title, int x, int y, int w, int h, Uint32 flags):
604
aca9551ee8b4 Fix compiling issues with Cython 0.20 ; don't pretend to concat shader sources
Thibaut Girka <thib@sitedethib.com>
parents: 592
diff changeset
103 title_bytes = title.encode()
aca9551ee8b4 Fix compiling issues with Cython 0.20 ; don't pretend to concat shader sources
Thibaut Girka <thib@sitedethib.com>
parents: 592
diff changeset
104 self.window = SDL_CreateWindow(title_bytes, x, y, w, h, flags)
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
105 if self.window == NULL:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
106 raise SDLError()
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
107
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 453
diff changeset
108 def __dealloc__(self):
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 453
diff changeset
109 if self.context != NULL:
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 453
diff changeset
110 SDL_GL_DeleteContext(self.context)
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 453
diff changeset
111 if self.window != NULL:
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 453
diff changeset
112 SDL_DestroyWindow(self.window)
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
113
635
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
114 cdef void create_gl_context(self) except *:
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
115 self.context = SDL_GL_CreateContext(self.window)
453
17a7f3b028f3 Raise an exception if GL context creation fails instead of failing silently.
Thibaut Girka <thib@sitedethib.com>
parents: 450
diff changeset
116 if self.context == NULL:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
117 raise SDLError()
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
118
512
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
119 cdef void present(self) nogil:
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
120 if self.renderer == NULL:
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
121 SDL_GL_SwapWindow(self.window)
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
122 else:
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
123 SDL_RenderPresent(self.renderer)
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
124
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 453
diff changeset
125 cdef void set_window_size(self, int width, int height) nogil:
422
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 421
diff changeset
126 SDL_SetWindowSize(self.window, width, height)
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 421
diff changeset
127
635
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
128 cdef void set_swap_interval(self, int interval) except *:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
129 if SDL_GL_SetSwapInterval(interval) < 0:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
130 raise SDLError()
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
131
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
132 cdef list get_events(self):
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
133 cdef SDL_Event event
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
134 ret = []
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
135 while SDL_PollEvent(&event):
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
136 if event.type == SDL_KEYDOWN:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
137 scancode = event.key.keysym.scancode
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
138 if scancode == SDL_SCANCODE_ESCAPE:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
139 ret.append((gui.PAUSE, None))
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
140 elif scancode in (SDL_SCANCODE_P, SDL_SCANCODE_HOME):
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
141 ret.append((gui.SCREENSHOT, None))
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
142 elif scancode == SDL_SCANCODE_DOWN:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
143 ret.append((gui.DOWN, None))
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
144 elif scancode == SDL_SCANCODE_F11:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
145 ret.append((gui.FULLSCREEN, None))
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
146 elif scancode == SDL_SCANCODE_RETURN:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
147 mod = event.key.keysym.mod
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
148 if mod & KMOD_ALT:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
149 ret.append((gui.FULLSCREEN, None))
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
150 elif event.type == SDL_QUIT:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
151 ret.append((gui.EXIT, None))
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
152 elif event.type == SDL_WINDOWEVENT:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
153 if event.window.event == SDL_WINDOWEVENT_RESIZED:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
154 ret.append((gui.RESIZE, (event.window.data1, event.window.data2)))
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
155 return ret
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
156
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
157 cdef int get_keystate(self) nogil:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
158 cdef int keystate = 0
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
159 cdef const Uint8 *keys = keyboard_state
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
160 if keys[SCANCODE_Z]:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
161 keystate |= 1
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
162 if keys[SCANCODE_X]:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
163 keystate |= 2
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
164 if keys[SCANCODE_LSHIFT]:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
165 keystate |= 4
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
166 if keys[SCANCODE_UP]:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
167 keystate |= 16
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
168 if keys[SCANCODE_DOWN]:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
169 keystate |= 32
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
170 if keys[SCANCODE_LEFT]:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
171 keystate |= 64
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
172 if keys[SCANCODE_RIGHT]:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
173 keystate |= 128
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
174 if keys[SCANCODE_LCTRL]:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
175 keystate |= 256
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
176 return keystate
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
177
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
178 cdef void toggle_fullscreen(self) nogil:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
179 ret = SDL_SetWindowFullscreen(self.window, 0 if self.is_fullscreen else SDL_WINDOW_FULLSCREEN_DESKTOP)
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
180 if ret == -1:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
181 with gil:
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
182 raise SDLError()
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
183 self.is_fullscreen = not self.is_fullscreen
80687f258001 Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
184
512
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
185 # The following functions are there for the pure SDL backend.
616
4ce3ef053a25 Remove every case where an exception could be silently eaten by a cdef function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 615
diff changeset
186 cdef bint create_renderer(self, Uint32 flags) except True:
512
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
187 self.renderer = SDL_CreateRenderer(self.window, -1, flags)
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
188 if self.renderer == NULL:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
189 raise SDLError()
512
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
190
616
4ce3ef053a25 Remove every case where an exception could be silently eaten by a cdef function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 615
diff changeset
191 cdef bint render_clear(self) except True:
512
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
192 ret = SDL_RenderClear(self.renderer)
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
193 if ret == -1:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
194 raise SDLError()
512
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
195
616
4ce3ef053a25 Remove every case where an exception could be silently eaten by a cdef function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 615
diff changeset
196 cdef bint render_copy(self, Texture texture, Rect srcrect, Rect dstrect) except True:
512
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
197 ret = SDL_RenderCopy(self.renderer, texture.texture, &srcrect.rect, &dstrect.rect)
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
198 if ret == -1:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
199 raise SDLError()
512
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
200
616
4ce3ef053a25 Remove every case where an exception could be silently eaten by a cdef function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 615
diff changeset
201 cdef bint render_copy_ex(self, Texture texture, Rect srcrect, Rect dstrect, double angle, bint flip) except True:
512
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
202 ret = SDL_RenderCopyEx(self.renderer, texture.texture, &srcrect.rect, &dstrect.rect, angle, NULL, flip)
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
203 if ret == -1:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
204 raise SDLError()
512
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
205
616
4ce3ef053a25 Remove every case where an exception could be silently eaten by a cdef function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 615
diff changeset
206 cdef bint render_set_clip_rect(self, Rect rect) except True:
512
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
207 ret = SDL_RenderSetClipRect(self.renderer, &rect.rect)
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
208 if ret == -1:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
209 raise SDLError()
512
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
210
616
4ce3ef053a25 Remove every case where an exception could be silently eaten by a cdef function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 615
diff changeset
211 cdef bint render_set_viewport(self, Rect rect) except True:
512
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
212 ret = SDL_RenderSetViewport(self.renderer, &rect.rect)
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
213 if ret == -1:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
214 raise SDLError()
512
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
215
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
216 cdef Texture create_texture_from_surface(self, Surface surface):
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
217 texture = Texture()
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
218 texture.texture = SDL_CreateTextureFromSurface(self.renderer, surface.surface)
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
219 if texture.texture == NULL:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
220 raise SDLError()
512
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
221 return texture
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
222
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
223
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
224 cdef class Texture:
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
225 cpdef set_color_mod(self, Uint8 r, Uint8 g, Uint8 b):
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
226 ret = SDL_SetTextureColorMod(self.texture, r, g, b)
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
227 if ret == -1:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
228 raise SDLError()
512
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
229
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
230 cpdef set_alpha_mod(self, Uint8 alpha):
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
231 ret = SDL_SetTextureAlphaMod(self.texture, alpha)
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
232 if ret == -1:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
233 raise SDLError()
512
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
234
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
235 cpdef set_blend_mode(self, SDL_BlendMode blend_mode):
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
236 ret = SDL_SetTextureBlendMode(self.texture, blend_mode)
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
237 if ret == -1:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
238 raise SDLError()
512
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
239
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
240
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
241 cdef class Rect:
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
242 def __init__(self, int x, int y, int w, int h):
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
243 self.rect.x = x
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
244 self.rect.y = y
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
245 self.rect.w = w
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
246 self.rect.h = h
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
247
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
248
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
249 cdef class Color:
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
250 def __init__(self, Uint8 b, Uint8 g, Uint8 r, Uint8 a=255):
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
251 self.color.r = r
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
252 self.color.g = g
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
253 self.color.b = b
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
254 self.color.a = a
b39ad30c6620 Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 501
diff changeset
255
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
256
420
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
257 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
258 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
259 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
260 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
261
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
262 property pixels:
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
263 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
264 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
265
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 616
diff changeset
266 cdef bint blit(self, Surface other) except True:
420
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
267 if SDL_BlitSurface(other.surface, NULL, self.surface, NULL) < 0:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
268 raise SDLError()
420
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
269
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 453
diff changeset
270 cdef void set_alpha(self, Surface alpha_surface) nogil:
420
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
271 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
272 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
273 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
274
590
e15672733c93 Switch to Python 3.x instead of 2.7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 589
diff changeset
275 for i in range(nb_pixels):
420
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
276 # 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
277 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
278
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
279
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
280 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
281 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
282 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
283 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
284
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 453
diff changeset
285 cdef void play(self, int loops) nogil:
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
286 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
287
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 453
diff changeset
288 cdef void set_loop_points(self, double start, double end) nogil:
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
289 #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
290 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
291
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
292
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
293 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
294 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
295 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
296 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
297
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 453
diff changeset
298 cdef void play(self, int channel, int loops) nogil:
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
299 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
300
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 453
diff changeset
301 cdef void set_volume(self, float volume) nogil:
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 453
diff changeset
302 Mix_VolumeChunk(self.chunk, int(volume * 128))
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
303
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 453
diff changeset
304
456
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 455
diff changeset
305 cdef class Font:
590
e15672733c93 Switch to Python 3.x instead of 2.7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 589
diff changeset
306 def __init__(self, str filename, int ptsize):
604
aca9551ee8b4 Fix compiling issues with Cython 0.20 ; don't pretend to concat shader sources
Thibaut Girka <thib@sitedethib.com>
parents: 592
diff changeset
307 path = filename.encode()
aca9551ee8b4 Fix compiling issues with Cython 0.20 ; don't pretend to concat shader sources
Thibaut Girka <thib@sitedethib.com>
parents: 592
diff changeset
308 self.font = TTF_OpenFont(path, ptsize)
456
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 455
diff changeset
309 if self.font == NULL:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
310 raise SDLError()
456
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 455
diff changeset
311
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 455
diff changeset
312 def __dealloc__(self):
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 455
diff changeset
313 if self.font != NULL:
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 455
diff changeset
314 TTF_CloseFont(self.font)
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 455
diff changeset
315
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 455
diff changeset
316 cdef Surface render(self, unicode text):
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 455
diff changeset
317 cdef SDL_Color white
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 455
diff changeset
318 white = SDL_Color(255, 255, 255, 255)
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 455
diff changeset
319 surface = Surface()
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 455
diff changeset
320 string = text.encode('utf-8')
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 455
diff changeset
321 surface.surface = TTF_RenderUTF8_Blended(self.font, string, white)
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 455
diff changeset
322 if surface.surface == NULL:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
323 raise SDLError()
456
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 455
diff changeset
324 return surface
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 455
diff changeset
325
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 455
diff changeset
326
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 616
diff changeset
327 cdef bint init(Uint32 flags) except True:
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
328 if SDL_Init(flags) < 0:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
329 raise SDLError()
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
330
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
331
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 616
diff changeset
332 cdef bint img_init(int flags) except True:
420
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
333 if IMG_Init(flags) != flags:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
334 raise SDLError()
420
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
335
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
336
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 616
diff changeset
337 cdef bint mix_init(int flags) except True:
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
338 if Mix_Init(flags) != flags:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
339 raise SDLError()
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
340
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
341
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 616
diff changeset
342 cdef bint ttf_init() except True:
456
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 455
diff changeset
343 if TTF_Init() < 0:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
344 raise SDLError()
456
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 455
diff changeset
345
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 455
diff changeset
346
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 616
diff changeset
347 cdef bint gl_set_attribute(SDL_GLattr attr, int value) except True:
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
348 if SDL_GL_SetAttribute(attr, value) < 0:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
349 raise SDLError()
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
350
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
351
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 453
diff changeset
352 cdef Surface load_png(file_):
420
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
353 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
354 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
355 surface = Surface()
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
356 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
357 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
358 if surface.surface == NULL:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
359 raise SDLError()
420
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
360 return surface
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
361
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
362
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 453
diff changeset
363 cdef Surface create_rgb_surface(int width, int height, int depth, Uint32 rmask=0, Uint32 gmask=0, Uint32 bmask=0, Uint32 amask=0):
420
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
364 surface = Surface()
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
365 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
366 if surface.surface == NULL:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
367 raise SDLError()
420
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
368 return surface
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
369
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 419
diff changeset
370
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 616
diff changeset
371 cdef bint mix_open_audio(int frequency, Uint16 format_, int channels, int chunksize) except True:
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
372 if Mix_OpenAudio(frequency, format_, channels, chunksize) < 0:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
373 raise SDLError()
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
374
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
375
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 616
diff changeset
376 cdef bint mix_allocate_channels(int numchans) except True:
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
377 if Mix_AllocateChannels(numchans) != numchans:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
378 raise SDLError()
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
379
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
380
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 453
diff changeset
381 cdef int mix_volume(int channel, float volume) nogil:
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
382 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
383
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
384
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 453
diff changeset
385 cdef int mix_volume_music(float volume) nogil:
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
386 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
387
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
388
590
e15672733c93 Switch to Python 3.x instead of 2.7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 589
diff changeset
389 cdef Music load_music(str filename):
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
390 music = Music()
604
aca9551ee8b4 Fix compiling issues with Cython 0.20 ; don't pretend to concat shader sources
Thibaut Girka <thib@sitedethib.com>
parents: 592
diff changeset
391 path = filename.encode()
aca9551ee8b4 Fix compiling issues with Cython 0.20 ; don't pretend to concat shader sources
Thibaut Girka <thib@sitedethib.com>
parents: 592
diff changeset
392 music.music = Mix_LoadMUS(path)
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
393 if music.music == NULL:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
394 raise SDLError()
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
395 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
396
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
397
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 453
diff changeset
398 cdef Chunk load_chunk(file_):
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
399 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
400 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
401 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
402 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
403 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
404 if chunk.chunk == NULL:
591
2dfa4aa135d2 Make the SDLError exception class automatically call SDL_GetError().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
405 raise SDLError()
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
406 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
407
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
408
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 453
diff changeset
409 cdef Uint32 get_ticks() nogil:
419
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
410 return SDL_GetTicks()
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
411
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
412
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 453
diff changeset
413 cdef void delay(Uint32 ms) nogil:
419
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
414 SDL_Delay(ms)
531
a7dc55ad9380 Display important messages in popups, instead of the terminal.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 528
diff changeset
415
a7dc55ad9380 Display important messages in popups, instead of the terminal.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 528
diff changeset
416
616
4ce3ef053a25 Remove every case where an exception could be silently eaten by a cdef function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 615
diff changeset
417 cpdef bint show_simple_message_box(unicode message) except True:
531
a7dc55ad9380 Display important messages in popups, instead of the terminal.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 528
diff changeset
418 text = message.encode('UTF-8')
616
4ce3ef053a25 Remove every case where an exception could be silently eaten by a cdef function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 615
diff changeset
419 ret = SDL_ShowSimpleMessageBox(1, 'PyTouhou', text, NULL)
4ce3ef053a25 Remove every case where an exception could be silently eaten by a cdef function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 615
diff changeset
420 if ret == -1:
4ce3ef053a25 Remove every case where an exception could be silently eaten by a cdef function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 615
diff changeset
421 raise SDLError()