Mercurial > touhou
annotate pytouhou/ui/window.pyx @ 533:de778a80820a
Optimise Clock a bit more.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Mon, 10 Feb 2014 18:03:50 +0100 |
parents | 5e3e0b09a531 |
children | aad758aef26d |
rev | line source |
---|---|
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
1 # -*- encoding: utf-8 -*- |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
2 ## |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
3 ## Copyright (C) 2013 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
4 ## |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
5 ## This program is free software; you can redistribute it and/or modify |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
7 ## by the Free Software Foundation; version 3 only. |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 ## |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
9 ## This program is distributed in the hope that it will be useful, |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
12 ## GNU General Public License for more details. |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
13 ## |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
14 |
533
de778a80820a
Optimise Clock a bit more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
513
diff
changeset
|
15 cimport cython |
de778a80820a
Optimise Clock a bit more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
513
diff
changeset
|
16 |
513
5e3e0b09a531
Move the OpenGL backend to its own package.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
512
diff
changeset
|
17 IF USE_OPENGL: |
5e3e0b09a531
Move the OpenGL backend to its own package.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
512
diff
changeset
|
18 from pytouhou.lib.opengl cimport \ |
5e3e0b09a531
Move the OpenGL backend to its own package.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
512
diff
changeset
|
19 (glEnable, glHint, glEnableClientState, GL_TEXTURE_2D, GL_BLEND, |
5e3e0b09a531
Move the OpenGL backend to its own package.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
512
diff
changeset
|
20 GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST, |
5e3e0b09a531
Move the OpenGL backend to its own package.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
512
diff
changeset
|
21 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY) |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
22 |
513
5e3e0b09a531
Move the OpenGL backend to its own package.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
512
diff
changeset
|
23 IF USE_GLEW: |
5e3e0b09a531
Move the OpenGL backend to its own package.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
512
diff
changeset
|
24 from pytouhou.lib.opengl cimport glewInit |
450
2a352118c55a
Add back Windows support, using GLEW for OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
423
diff
changeset
|
25 |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
26 |
455
6864a38b2413
Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
452
diff
changeset
|
27 cdef class Clock: |
6864a38b2413
Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
452
diff
changeset
|
28 def __init__(self, long fps=-1): |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
29 self._target_fps = 0 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
30 self._ref_tick = 0 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
31 self._ref_frame = 0 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
32 self._fps_tick = 0 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
33 self._fps_frame = 0 |
533
de778a80820a
Optimise Clock a bit more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
513
diff
changeset
|
34 self.fps = 0 |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 self.set_target_fps(fps) |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
36 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
37 |
455
6864a38b2413
Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
452
diff
changeset
|
38 cdef void set_target_fps(self, long fps) nogil: |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
39 self._target_fps = fps |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
40 self._ref_tick = 0 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
41 self._fps_tick = 0 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
42 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
43 |
533
de778a80820a
Optimise Clock a bit more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
513
diff
changeset
|
44 @cython.cdivision(True) |
455
6864a38b2413
Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
452
diff
changeset
|
45 cdef void tick(self) nogil except *: |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
46 current = sdl.get_ticks() |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
47 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
48 if not self._ref_tick: |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
49 self._ref_tick = current |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
50 self._ref_frame = 0 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
51 |
452
1f5156093785
By default, only enable fps limiting if vsync doesn't do the job.
Thibaut Girka <thib@sitedethib.com>
parents:
450
diff
changeset
|
52 if self._fps_frame >= (self._target_fps if self._target_fps > 0 else 60): |
533
de778a80820a
Optimise Clock a bit more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
513
diff
changeset
|
53 self.fps = self._fps_frame * 1000. / (current - self._fps_tick) |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
54 self._fps_tick = current |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
55 self._fps_frame = 0 |
452
1f5156093785
By default, only enable fps limiting if vsync doesn't do the job.
Thibaut Girka <thib@sitedethib.com>
parents:
450
diff
changeset
|
56 # If we are relying on vsync, but vsync doesn't work or is higher |
1f5156093785
By default, only enable fps limiting if vsync doesn't do the job.
Thibaut Girka <thib@sitedethib.com>
parents:
450
diff
changeset
|
57 # than 60 fps, limit ourselves to 60 fps. |
533
de778a80820a
Optimise Clock a bit more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
513
diff
changeset
|
58 if self._target_fps < 0 and self.fps > 64.: |
452
1f5156093785
By default, only enable fps limiting if vsync doesn't do the job.
Thibaut Girka <thib@sitedethib.com>
parents:
450
diff
changeset
|
59 self._target_fps = 60 |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
60 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
61 self._ref_frame += 1 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
62 self._fps_frame += 1 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
63 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
64 target_tick = self._ref_tick |
496
104c737ce8b3
Test target FPS for natural instead of non-zero.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
491
diff
changeset
|
65 if self._target_fps > 0: |
491
2276229282fd
Fix gcc’s warnings with -Wall -Wextra.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
469
diff
changeset
|
66 target_tick += <unsigned long>(self._ref_frame * 1000 / self._target_fps) |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
67 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
68 if current <= target_tick: |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
69 sdl.delay(target_tick - current) |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
70 else: |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
71 self._ref_tick = current |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
72 self._ref_frame = 0 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
73 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
74 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
75 |
463
11708a1d0a1a
Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
460
diff
changeset
|
76 cdef class Runner: |
11708a1d0a1a
Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
460
diff
changeset
|
77 cdef void start(self) except *: |
11708a1d0a1a
Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
460
diff
changeset
|
78 pass |
11708a1d0a1a
Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
460
diff
changeset
|
79 |
11708a1d0a1a
Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
460
diff
changeset
|
80 cdef void finish(self) except *: |
11708a1d0a1a
Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
460
diff
changeset
|
81 pass |
11708a1d0a1a
Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
460
diff
changeset
|
82 |
11708a1d0a1a
Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
460
diff
changeset
|
83 cdef bint update(self) except *: |
11708a1d0a1a
Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
460
diff
changeset
|
84 return False |
11708a1d0a1a
Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
460
diff
changeset
|
85 |
11708a1d0a1a
Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
460
diff
changeset
|
86 |
11708a1d0a1a
Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
460
diff
changeset
|
87 |
455
6864a38b2413
Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
452
diff
changeset
|
88 cdef class Window: |
503
c622eaf64428
Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
496
diff
changeset
|
89 def __init__(self, bint double_buffer=True, long fps_limit=-1, |
510
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
90 bint fixed_pipeline=False, bint sound=True, bint opengl=True): |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
91 self.fps_limit = fps_limit |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
92 self.use_fixed_pipeline = fixed_pipeline |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
93 self.runner = None |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
94 |
510
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
95 flags = sdl.WINDOW_SHOWN |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
96 |
513
5e3e0b09a531
Move the OpenGL backend to its own package.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
512
diff
changeset
|
97 if USE_OPENGL and opengl: |
510
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
98 sdl.gl_set_attribute(sdl.GL_CONTEXT_MAJOR_VERSION, 2) |
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
99 sdl.gl_set_attribute(sdl.GL_CONTEXT_MINOR_VERSION, 1) |
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
100 sdl.gl_set_attribute(sdl.GL_DOUBLEBUFFER, int(double_buffer)) |
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
101 sdl.gl_set_attribute(sdl.GL_DEPTH_SIZE, 24) |
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
102 |
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
103 flags |= sdl.WINDOW_OPENGL |
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
104 |
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
510
diff
changeset
|
105 #TODO: implement it in the SDL backend too. |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
510
diff
changeset
|
106 if not self.use_fixed_pipeline: |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
510
diff
changeset
|
107 flags |= sdl.WINDOW_RESIZABLE |
464
36bc577b2392
Make the window resizable, and scale its content correctly.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
463
diff
changeset
|
108 |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
109 self.win = sdl.Window('PyTouhou', |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
110 sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED, |
503
c622eaf64428
Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
496
diff
changeset
|
111 640, 480, #XXX |
464
36bc577b2392
Make the window resizable, and scale its content correctly.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
463
diff
changeset
|
112 flags) |
510
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
113 |
513
5e3e0b09a531
Move the OpenGL backend to its own package.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
512
diff
changeset
|
114 if USE_OPENGL and opengl: |
510
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
115 self.win.gl_create_context() |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
116 |
510
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
117 IF USE_GLEW: |
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
118 if glewInit() != 0: |
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
119 raise Exception('GLEW init fail!') |
450
2a352118c55a
Add back Windows support, using GLEW for OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
423
diff
changeset
|
120 |
510
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
121 # Initialize OpenGL |
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
122 glEnable(GL_BLEND) |
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
123 if self.use_fixed_pipeline: |
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
124 glEnable(GL_TEXTURE_2D) |
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
125 glHint(GL_FOG_HINT, GL_NICEST) |
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
126 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) |
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
127 glEnableClientState(GL_COLOR_ARRAY) |
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
128 glEnableClientState(GL_VERTEX_ARRAY) |
64a72df88de5
Make Window able to not initialise OpenGL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
503
diff
changeset
|
129 glEnableClientState(GL_TEXTURE_COORD_ARRAY) |
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
510
diff
changeset
|
130 else: |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
510
diff
changeset
|
131 self.win.create_renderer(0) |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
132 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
133 self.clock = Clock(self.fps_limit) |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
134 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
135 |
455
6864a38b2413
Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
452
diff
changeset
|
136 cdef void set_size(self, int width, int height) nogil: |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
137 self.win.set_window_size(width, height) |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
138 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
139 |
463
11708a1d0a1a
Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
460
diff
changeset
|
140 cpdef set_runner(self, Runner runner=None): |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
141 self.runner = runner |
455
6864a38b2413
Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
452
diff
changeset
|
142 if runner is not None: |
6864a38b2413
Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
452
diff
changeset
|
143 runner.start() |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
144 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
145 |
455
6864a38b2413
Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
452
diff
changeset
|
146 cpdef run(self): |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
147 try: |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
148 while self.run_frame(): |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
149 pass |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
150 finally: |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
151 self.runner.finish() |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
152 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
153 |
455
6864a38b2413
Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
452
diff
changeset
|
154 cdef bint run_frame(self) except? False: |
6864a38b2413
Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
452
diff
changeset
|
155 cdef bint running = False |
6864a38b2413
Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
452
diff
changeset
|
156 if self.runner is not None: |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
157 running = self.runner.update() |
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
510
diff
changeset
|
158 self.win.present() |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
159 self.clock.tick() |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
160 return running |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
161 |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
162 |
463
11708a1d0a1a
Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
460
diff
changeset
|
163 cdef double get_fps(self) nogil: |
533
de778a80820a
Optimise Clock a bit more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
513
diff
changeset
|
164 return self.clock.fps |