comparison pytouhou/ui/window.pyx @ 553:8f51e34d911c

Refactor graphics backend selection, to make them fallbackable and optional.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 29 May 2014 12:31:55 +0200
parents aad758aef26d
children 00f228b57840
comparison
equal deleted inserted replaced
552:aad758aef26d 553:8f51e34d911c
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 cimport cython 15 cimport cython
16
17 IF USE_OPENGL:
18 from pytouhou.lib.opengl cimport \
19 (glEnable, glHint, glEnableClientState, GL_TEXTURE_2D, GL_BLEND,
20 GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST,
21 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY)
22
23 IF USE_GLEW:
24 from pytouhou.lib.opengl cimport glewInit
25 16
26 17
27 cdef class Clock: 18 cdef class Clock:
28 def __init__(self, long fps=-1): 19 def __init__(self, long fps=-1):
29 self._target_fps = 0 20 self._target_fps = 0
84 return False 75 return False
85 76
86 77
87 78
88 cdef class Window: 79 cdef class Window:
89 def __init__(self, bint double_buffer=True, long fps_limit=-1, 80 def __init__(self, backend, long fps_limit=-1):
90 bint fixed_pipeline=False, bint sound=True, bint opengl=True):
91 self.use_fixed_pipeline = fixed_pipeline
92 self.runner = None 81 self.runner = None
93 82
94 flags = sdl.WINDOW_SHOWN 83 if backend is not None:
95 84 self.win = backend.create_window(
96 if USE_OPENGL and opengl: 85 'PyTouhou',
97 sdl.gl_set_attribute(sdl.GL_CONTEXT_MAJOR_VERSION, 2) 86 sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED,
98 sdl.gl_set_attribute(sdl.GL_CONTEXT_MINOR_VERSION, 1) 87 640, 480) #XXX
99 sdl.gl_set_attribute(sdl.GL_DOUBLEBUFFER, int(double_buffer))
100 sdl.gl_set_attribute(sdl.GL_DEPTH_SIZE, 24)
101
102 flags |= sdl.WINDOW_OPENGL
103
104 #TODO: implement it in the SDL backend too.
105 if not self.use_fixed_pipeline:
106 flags |= sdl.WINDOW_RESIZABLE
107
108 self.win = sdl.Window('PyTouhou',
109 sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED,
110 640, 480, #XXX
111 flags)
112
113 if USE_OPENGL and opengl:
114 self.win.gl_create_context()
115
116 IF USE_GLEW:
117 if glewInit() != 0:
118 raise Exception('GLEW init fail!')
119
120 # Initialize OpenGL
121 glEnable(GL_BLEND)
122 if self.use_fixed_pipeline:
123 glEnable(GL_TEXTURE_2D)
124 glHint(GL_FOG_HINT, GL_NICEST)
125 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
126 glEnableClientState(GL_COLOR_ARRAY)
127 glEnableClientState(GL_VERTEX_ARRAY)
128 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
129 else:
130 self.win.create_renderer(0)
131 88
132 self.clock = Clock(fps_limit) 89 self.clock = Clock(fps_limit)
133 90
134 91
135 cdef void set_size(self, int width, int height) nogil: 92 cdef void set_size(self, int width, int height) nogil:
152 109
153 cdef bint run_frame(self) except? False: 110 cdef bint run_frame(self) except? False:
154 cdef bint running = False 111 cdef bint running = False
155 if self.runner is not None: 112 if self.runner is not None:
156 running = self.runner.update() 113 running = self.runner.update()
157 self.win.present() 114 if self.win is not None:
115 self.win.present()
158 self.clock.tick() 116 self.clock.tick()
159 return running 117 return running
160 118
161 119
162 cdef double get_fps(self) nogil: 120 cdef double get_fps(self) nogil: