comparison pytouhou/lib/sdl.pyx @ 418:63f59be04a54

Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 16 Jul 2013 21:07:15 +0200
parents
children 1c92721f8e49
comparison
equal deleted inserted replaced
417:efae61ad6efe 418:63f59be04a54
1 # -*- encoding: utf-8 -*-
2 ##
3 ## Copyright (C) 2013 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
4 ##
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published
7 ## by the Free Software Foundation; version 3 only.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
13 ##
14
15 INIT_VIDEO = SDL_INIT_VIDEO
16
17 GL_CONTEXT_MAJOR_VERSION = SDL_GL_CONTEXT_MAJOR_VERSION
18 GL_CONTEXT_MINOR_VERSION = SDL_GL_CONTEXT_MINOR_VERSION
19 GL_DOUBLEBUFFER = SDL_GL_DOUBLEBUFFER
20 GL_DEPTH_SIZE = SDL_GL_DEPTH_SIZE
21
22 WINDOWPOS_CENTERED = SDL_WINDOWPOS_CENTERED
23 WINDOW_OPENGL = SDL_WINDOW_OPENGL
24 WINDOW_SHOWN = SDL_WINDOW_SHOWN
25
26 SCANCODE_Z = SDL_SCANCODE_Z
27 SCANCODE_X = SDL_SCANCODE_X
28 SCANCODE_LSHIFT = SDL_SCANCODE_LSHIFT
29 SCANCODE_UP = SDL_SCANCODE_UP
30 SCANCODE_DOWN = SDL_SCANCODE_DOWN
31 SCANCODE_LEFT = SDL_SCANCODE_LEFT
32 SCANCODE_RIGHT = SDL_SCANCODE_RIGHT
33 SCANCODE_LCTRL = SDL_SCANCODE_LCTRL
34 SCANCODE_ESCAPE = SDL_SCANCODE_ESCAPE
35
36 KEYDOWN = SDL_KEYDOWN
37 QUIT = SDL_QUIT
38
39
40 class SDLError(Exception):
41 pass
42
43
44 cdef class Window:
45 cdef SDL_Window *window
46 cdef SDL_GLContext context
47
48 def __init__(self, const char *title, int x, int y, int w, int h, Uint32 flags):
49 self.window = SDL_CreateWindow(title, x, y, w, h, flags)
50 if self.window == NULL:
51 raise SDLError(SDL_GetError())
52
53 def destroy_window(self):
54 SDL_DestroyWindow(self.window)
55
56 def gl_create_context(self):
57 self.context = SDL_GL_CreateContext(self.window)
58
59 def gl_swap_window(self):
60 SDL_GL_SwapWindow(self.window)
61
62 def gl_delete_context(self):
63 SDL_GL_DeleteContext(self.context)
64
65
66 def init(Uint32 flags):
67 if SDL_Init(flags) < 0:
68 raise SDLError(SDL_GetError())
69
70
71 def quit():
72 SDL_Quit()
73
74
75 def gl_set_attribute(SDL_GLattr attr, int value):
76 if SDL_GL_SetAttribute(attr, value) < 0:
77 raise SDLError(SDL_GetError())
78
79
80 def poll_events():
81 cdef SDL_Event event
82 ret = []
83 while SDL_PollEvent(&event):
84 if event.type == SDL_KEYDOWN:
85 ret.append((event.type, event.key.keysym.scancode))
86 elif event.type == SDL_QUIT:
87 ret.append((event.type,))
88 return ret
89
90
91 def get_keyboard_state():
92 cdef int numkeys
93 cdef bint k
94 cdef const Uint8 *state
95 state = SDL_GetKeyboardState(&numkeys)
96 return tuple([k is not False for k in state[:numkeys]])