comparison pytouhou/lib/sdl.pyx @ 635:80687f258001

Make sdl.Window inherit from gui.Window, so we can swap implementations.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 14 Apr 2016 21:18:03 +0100
parents a6af3ff86612
children a6875f90c141
comparison
equal deleted inserted replaced
634:5270c34b4c00 635:80687f258001
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of 10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14 14
15 import pytouhou.lib.gui as gui
16
15 from pytouhou.utils.helpers import get_logger 17 from pytouhou.utils.helpers import get_logger
16 18
17 logger = get_logger(__name__) 19 logger = get_logger(__name__)
18 20
19 21
51 KEYDOWN = SDL_KEYDOWN 53 KEYDOWN = SDL_KEYDOWN
52 QUIT = SDL_QUIT 54 QUIT = SDL_QUIT
53 WINDOWEVENT = SDL_WINDOWEVENT 55 WINDOWEVENT = SDL_WINDOWEVENT
54 56
55 57
56 class SDLError(Exception): 58 class SDLError(gui.Error):
57 def __init__(self): 59 def __init__(self):
58 error = SDL_GetError() 60 error = SDL_GetError()
59 Exception.__init__(self, error.decode()) 61 Exception.__init__(self, error.decode())
60 62
61 63
94 TTF_Quit() 96 TTF_Quit()
95 IMG_Quit() 97 IMG_Quit()
96 SDL_Quit() 98 SDL_Quit()
97 99
98 100
99 cdef class Window: 101 cdef class Window(gui.Window):
100 def __init__(self, str title, int x, int y, int w, int h, Uint32 flags): 102 def __init__(self, str title, int x, int y, int w, int h, Uint32 flags):
101 title_bytes = title.encode() 103 title_bytes = title.encode()
102 self.window = SDL_CreateWindow(title_bytes, x, y, w, h, flags) 104 self.window = SDL_CreateWindow(title_bytes, x, y, w, h, flags)
103 if self.window == NULL: 105 if self.window == NULL:
104 raise SDLError() 106 raise SDLError()
107 if self.context != NULL: 109 if self.context != NULL:
108 SDL_GL_DeleteContext(self.context) 110 SDL_GL_DeleteContext(self.context)
109 if self.window != NULL: 111 if self.window != NULL:
110 SDL_DestroyWindow(self.window) 112 SDL_DestroyWindow(self.window)
111 113
112 cdef bint gl_create_context(self) except True: 114 cdef void create_gl_context(self) except *:
113 self.context = SDL_GL_CreateContext(self.window) 115 self.context = SDL_GL_CreateContext(self.window)
114 if self.context == NULL: 116 if self.context == NULL:
115 raise SDLError() 117 raise SDLError()
116 118
117 cdef void present(self) nogil: 119 cdef void present(self) nogil:
120 else: 122 else:
121 SDL_RenderPresent(self.renderer) 123 SDL_RenderPresent(self.renderer)
122 124
123 cdef void set_window_size(self, int width, int height) nogil: 125 cdef void set_window_size(self, int width, int height) nogil:
124 SDL_SetWindowSize(self.window, width, height) 126 SDL_SetWindowSize(self.window, width, height)
127
128 cdef void set_swap_interval(self, int interval) except *:
129 if SDL_GL_SetSwapInterval(interval) < 0:
130 raise SDLError()
131
132 cdef list get_events(self):
133 cdef SDL_Event event
134 ret = []
135 while SDL_PollEvent(&event):
136 if event.type == SDL_KEYDOWN:
137 scancode = event.key.keysym.scancode
138 if scancode == SDL_SCANCODE_ESCAPE:
139 ret.append((gui.PAUSE, None))
140 elif scancode in (SDL_SCANCODE_P, SDL_SCANCODE_HOME):
141 ret.append((gui.SCREENSHOT, None))
142 elif scancode == SDL_SCANCODE_DOWN:
143 ret.append((gui.DOWN, None))
144 elif scancode == SDL_SCANCODE_F11:
145 ret.append((gui.FULLSCREEN, None))
146 elif scancode == SDL_SCANCODE_RETURN:
147 mod = event.key.keysym.mod
148 if mod & KMOD_ALT:
149 ret.append((gui.FULLSCREEN, None))
150 elif event.type == SDL_QUIT:
151 ret.append((gui.EXIT, None))
152 elif event.type == SDL_WINDOWEVENT:
153 if event.window.event == SDL_WINDOWEVENT_RESIZED:
154 ret.append((gui.RESIZE, (event.window.data1, event.window.data2)))
155 return ret
156
157 cdef int get_keystate(self) nogil:
158 cdef int keystate = 0
159 cdef const Uint8 *keys = keyboard_state
160 if keys[SCANCODE_Z]:
161 keystate |= 1
162 if keys[SCANCODE_X]:
163 keystate |= 2
164 if keys[SCANCODE_LSHIFT]:
165 keystate |= 4
166 if keys[SCANCODE_UP]:
167 keystate |= 16
168 if keys[SCANCODE_DOWN]:
169 keystate |= 32
170 if keys[SCANCODE_LEFT]:
171 keystate |= 64
172 if keys[SCANCODE_RIGHT]:
173 keystate |= 128
174 if keys[SCANCODE_LCTRL]:
175 keystate |= 256
176 return keystate
177
178 cdef void toggle_fullscreen(self) nogil:
179 ret = SDL_SetWindowFullscreen(self.window, 0 if self.is_fullscreen else SDL_WINDOW_FULLSCREEN_DESKTOP)
180 if ret == -1:
181 with gil:
182 raise SDLError()
183 self.is_fullscreen = not self.is_fullscreen
125 184
126 # The following functions are there for the pure SDL backend. 185 # The following functions are there for the pure SDL backend.
127 cdef bint create_renderer(self, Uint32 flags) except True: 186 cdef bint create_renderer(self, Uint32 flags) except True:
128 self.renderer = SDL_CreateRenderer(self.window, -1, flags) 187 self.renderer = SDL_CreateRenderer(self.window, -1, flags)
129 if self.renderer == NULL: 188 if self.renderer == NULL:
287 346
288 cdef bint gl_set_attribute(SDL_GLattr attr, int value) except True: 347 cdef bint gl_set_attribute(SDL_GLattr attr, int value) except True:
289 if SDL_GL_SetAttribute(attr, value) < 0: 348 if SDL_GL_SetAttribute(attr, value) < 0:
290 raise SDLError() 349 raise SDLError()
291 350
292 cdef bint gl_set_swap_interval(int interval) except True:
293 if SDL_GL_SetSwapInterval(interval) < 0:
294 raise SDLError()
295
296
297 cdef list poll_events():
298 cdef SDL_Event event
299 ret = []
300 while SDL_PollEvent(&event):
301 if event.type == SDL_KEYDOWN:
302 ret.append((event.type, event.key.keysym.scancode))
303 elif event.type == SDL_QUIT:
304 ret.append((event.type,))
305 elif event.type == SDL_WINDOWEVENT:
306 ret.append((event.type, event.window.event, event.window.data1, event.window.data2))
307 return ret
308
309 351
310 cdef Surface load_png(file_): 352 cdef Surface load_png(file_):
311 data = file_.read() 353 data = file_.read()
312 rwops = SDL_RWFromConstMem(<char*>data, len(data)) 354 rwops = SDL_RWFromConstMem(<char*>data, len(data))
313 surface = Surface() 355 surface = Surface()