comparison pytouhou/ui/window.pyx @ 503:c622eaf64428

Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 04 Oct 2013 14:27:11 +0200
parents 104c737ce8b3
children 64a72df88de5
comparison
equal deleted inserted replaced
502:3d3285918ba1 503:c622eaf64428
9 ## This program is distributed in the hope that it will be useful, 9 ## This program is distributed in the hope that it will be useful,
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
15 cimport cython
16 14
17 from pytouhou.lib.opengl cimport \ 15 from pytouhou.lib.opengl cimport \
18 (glEnable, glHint, glEnableClientState, GL_TEXTURE_2D, GL_BLEND, 16 (glEnable, glHint, glEnableClientState, GL_TEXTURE_2D, GL_BLEND,
19 GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST, 17 GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST,
20 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY) 18 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY)
86 return False 84 return False
87 85
88 86
89 87
90 cdef class Window: 88 cdef class Window:
91 def __init__(self, tuple size=None, bint double_buffer=True, long fps_limit=-1, 89 def __init__(self, bint double_buffer=True, long fps_limit=-1,
92 bint fixed_pipeline=False, bint sound=True): 90 bint fixed_pipeline=False, bint sound=True):
93 self.fps_limit = fps_limit 91 self.fps_limit = fps_limit
94 self.use_fixed_pipeline = fixed_pipeline 92 self.use_fixed_pipeline = fixed_pipeline
95 self.runner = None 93 self.runner = None
96 94
97 sdl.gl_set_attribute(sdl.GL_CONTEXT_MAJOR_VERSION, 2) 95 sdl.gl_set_attribute(sdl.GL_CONTEXT_MAJOR_VERSION, 2)
98 sdl.gl_set_attribute(sdl.GL_CONTEXT_MINOR_VERSION, 1) 96 sdl.gl_set_attribute(sdl.GL_CONTEXT_MINOR_VERSION, 1)
99 sdl.gl_set_attribute(sdl.GL_DOUBLEBUFFER, int(double_buffer)) 97 sdl.gl_set_attribute(sdl.GL_DOUBLEBUFFER, int(double_buffer))
100 sdl.gl_set_attribute(sdl.GL_DEPTH_SIZE, 24) 98 sdl.gl_set_attribute(sdl.GL_DEPTH_SIZE, 24)
101 99
102 self.width, self.height = size if size is not None else (640, 480)
103
104 flags = sdl.WINDOW_OPENGL | sdl.WINDOW_SHOWN 100 flags = sdl.WINDOW_OPENGL | sdl.WINDOW_SHOWN
105 if not self.use_fixed_pipeline: 101 if not self.use_fixed_pipeline:
106 flags |= sdl.WINDOW_RESIZABLE 102 flags |= sdl.WINDOW_RESIZABLE
107 103
108 self.win = sdl.Window('PyTouhou', 104 self.win = sdl.Window('PyTouhou',
109 sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED, 105 sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED,
110 self.width, self.height, 106 640, 480, #XXX
111 flags) 107 flags)
112 self.win.gl_create_context() 108 self.win.gl_create_context()
113 109
114 IF USE_GLEW: 110 IF USE_GLEW:
115 if glewInit() != 0: 111 if glewInit() != 0:
126 glEnableClientState(GL_TEXTURE_COORD_ARRAY) 122 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
127 123
128 self.clock = Clock(self.fps_limit) 124 self.clock = Clock(self.fps_limit)
129 125
130 126
131 @cython.cdivision(True)
132 cdef void set_size(self, int width, int height) nogil: 127 cdef void set_size(self, int width, int height) nogil:
133 self.win.set_window_size(width, height) 128 self.win.set_window_size(width, height)
134
135 runner_width = float(self.runner.width)
136 runner_height = float(self.runner.height)
137
138 scale = min(width / runner_width,
139 height / runner_height)
140
141 self.width = int(runner_width * scale)
142 self.height = int(runner_height * scale)
143
144 self.x = (width - self.width) // 2
145 self.y = (height - self.height) // 2
146 129
147 130
148 cpdef set_runner(self, Runner runner=None): 131 cpdef set_runner(self, Runner runner=None):
149 self.runner = runner 132 self.runner = runner
150 if runner is not None: 133 if runner is not None: