Mercurial > touhou
annotate pytouhou/opengl/gamerenderer.py @ 130:11ab06f4c4c6
Introduce characters!
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sat, 10 Sep 2011 22:48:56 +0200 |
parents | 9d7129ee2c4f |
children |
rev | line source |
---|---|
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
1 # -*- encoding: utf-8 -*- |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
2 ## |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
4 ## |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
5 ## This program is free software; you can redistribute it and/or modify |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
7 ## by the Free Software Foundation; version 3 only. |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
8 ## |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
9 ## This program is distributed in the hope that it will be useful, |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
12 ## GNU General Public License for more details. |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
13 ## |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
14 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
15 import struct |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
16 from itertools import chain |
125
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
17 import ctypes |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
18 |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
19 import pyglet |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
20 from pyglet.gl import * |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
21 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
22 from pytouhou.opengl.texture import TextureManager |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
23 from pytouhou.opengl.sprite import get_sprite_rendering_data |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
24 from pytouhou.opengl.background import get_background_rendering_data |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
25 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
26 |
125
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
27 MAX_ELEMENTS = 10000 |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
28 |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
29 |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
30 class GameRenderer(pyglet.window.Window): |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
31 def __init__(self, resource_loader, game=None, background=None): |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
32 pyglet.window.Window.__init__(self, caption='PyTouhou', resizable=False) |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
33 self.keys = pyglet.window.key.KeyStateHandler() |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
34 self.push_handlers(self.keys) |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
35 |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
36 self.texture_manager = TextureManager(resource_loader) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
37 |
130 | 38 self.fps_display = pyglet.clock.ClockDisplay() |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
39 |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
40 self.game = game |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
41 self.background = background |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
42 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
43 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
44 def start(self, width=384, height=448): |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
45 self.set_size(width, height) |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
46 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
47 # Initialize OpenGL |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
48 glMatrixMode(GL_PROJECTION) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
49 glLoadIdentity() |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
50 gluPerspective(30, float(width)/float(height), |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
51 101010101./2010101., 101010101./10101.) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
52 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
53 glEnable(GL_BLEND) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
54 glEnable(GL_TEXTURE_2D) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
55 glEnable(GL_FOG) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
56 glHint(GL_FOG_HINT, GL_NICEST) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
57 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
58 glEnableClientState(GL_COLOR_ARRAY) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
59 glEnableClientState(GL_VERTEX_ARRAY) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
60 glEnableClientState(GL_TEXTURE_COORD_ARRAY) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
61 |
125
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
62 # Allocate buffers |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
63 buff = ctypes.c_buffer(MAX_ELEMENTS * 4 * (3 * 4 + 2 * 4 + 4)) |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
64 self.buffers = (buff, |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
65 ctypes.byref(buff, 3 * 4), |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
66 ctypes.byref(buff, 3 * 4 + 2 * 4)) |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
67 |
130 | 68 # Use our own loop to ensure 60 (for now, 120) fps |
69 pyglet.clock.set_fps_limit(120) | |
70 while not self.has_exit: | |
71 pyglet.clock.tick() | |
72 self.dispatch_events() | |
73 self.update() | |
74 self.on_draw() | |
75 self.flip() | |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
76 |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
77 |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
78 def on_resize(self, width, height): |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
79 glViewport(0, 0, width, height) |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
80 |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
81 |
130 | 82 def on_key_press(self, symbol, modifiers): |
83 if symbol == pyglet.window.key.ESCAPE: | |
84 self.has_exit = True | |
85 # XXX: Fullscreen will be enabled the day pyglet stops sucking | |
86 elif symbol == pyglet.window.key.F11: | |
87 self.set_fullscreen(not self.fullscreen) | |
88 | |
89 | |
90 def update(self): | |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
91 if self.background: |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
92 self.background.update(self.game.game_state.frame) |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
93 if self.game: |
130 | 94 #TODO: allow user settings |
95 keystate = 0 | |
96 if self.keys[pyglet.window.key.W]: | |
97 keystate |= 1 | |
98 if self.keys[pyglet.window.key.X]: | |
99 keystate |= 2 | |
100 #TODO: on some configurations, LSHIFT is Shift_L when pressed | |
101 # and ISO_Prev_Group when released, confusing the hell out of pyglet | |
102 # and leading to a always-on LSHIFT... | |
103 if self.keys[pyglet.window.key.LSHIFT]: | |
104 keystate |= 4 | |
105 if self.keys[pyglet.window.key.UP]: | |
106 keystate |= 16 | |
107 if self.keys[pyglet.window.key.DOWN]: | |
108 keystate |= 32 | |
109 if self.keys[pyglet.window.key.LEFT]: | |
110 keystate |= 64 | |
111 if self.keys[pyglet.window.key.RIGHT]: | |
112 keystate |= 128 | |
113 if self.keys[pyglet.window.key.LCTRL]: | |
114 keystate |= 256 | |
115 self.game.run_iter(keystate) #TODO: self.keys... | |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
116 |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
117 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
118 def render_elements(self, elements): |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
119 texture_manager = self.texture_manager |
125
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
120 |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
121 pack_data = struct.Struct('fff ff BBBB' * 4).pack_into |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
122 _vertices, _uvs, _colors = self.buffers |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
123 |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
124 nb_vertices = 0 |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
125 indices_by_texture = {} |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
126 |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
127 for element in elements: |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
128 sprite = element._sprite |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
129 if sprite: |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
130 ox, oy = element.x, element.y |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
131 key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite) |
126 | 132 rec = indices_by_texture.setdefault(key, []) |
125
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
133 |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
134 # Pack data in buffer |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
135 (x1, y1, z1), (x2, y2, z2), (x3, y3, z3), (x4, y4, z4) = vertices |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
136 r1, g1, b1, a1, r2, g2, b2, a2, r3, g3, b3, a3, r4, g4, b4, a4 = colors |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
137 u1, v1, u2, v2, u3, v3, u4, v4 = uvs |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
138 pack_data(_vertices, nb_vertices * (3 * 4 + 2 * 4 + 4), |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
139 x1 + ox, y1 + oy, z1, |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
140 u1, v1, |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
141 r1, g1, b1, a1, |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
142 |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
143 x2 + ox, y2 + oy, z2, |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
144 u2, v2, |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
145 r2, g2, b2, a2, |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
146 |
125
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
147 x3 + ox, y3 + oy, z3, |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
148 u3, v3, |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
149 r3, g3, b3, a3, |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
150 |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
151 x4 + ox, y4 + oy, z4, |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
152 u4, v4, |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
153 r4, g4, b4, a4) |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
154 |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
155 # Add indices |
126 | 156 index = nb_vertices |
157 rec.extend((index, index + 1, index + 2, index + 3)) | |
125
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
158 |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
159 nb_vertices += 4 |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
160 |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
161 glVertexPointer(3, GL_FLOAT, 24, _vertices) |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
162 glTexCoordPointer(2, GL_FLOAT, 24, _uvs) |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
163 glColorPointer(4, GL_UNSIGNED_BYTE, 24, _colors) |
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
164 |
126 | 165 for (texture_key, blendfunc), indices in indices_by_texture.items(): |
166 nb_indices = len(indices) | |
125
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
167 indices = struct.pack(str(nb_indices) + 'H', *indices) |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
168 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc]) |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
169 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key].id) |
125
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
170 glDrawElements(GL_QUADS, nb_indices, GL_UNSIGNED_SHORT, indices) |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
171 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
172 |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
173 def on_draw(self): |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
174 glClear(GL_DEPTH_BUFFER_BIT) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
175 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
176 back = self.background |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
177 game = self.game |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
178 texture_manager = self.texture_manager |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
179 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
180 if back is not None: |
111
340fcda8e64a
Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents:
108
diff
changeset
|
181 fog_b, fog_g, fog_r, fog_start, fog_end = back.fog_interpolator.values |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
182 x, y, z = back.position_interpolator.values |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
183 dx, dy, dz = back.position2_interpolator.values |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
184 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
185 glFogi(GL_FOG_MODE, GL_LINEAR) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
186 glFogf(GL_FOG_START, fog_start) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
187 glFogf(GL_FOG_END, fog_end) |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
188 glFogfv(GL_FOG_COLOR, (GLfloat * 4)(fog_r / 255., fog_g / 255., fog_b / 255., 1.)) |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
189 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
190 glMatrixMode(GL_MODELVIEW) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
191 glLoadIdentity() |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
192 # Some explanations on the magic constants: |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
193 # 192. = 384. / 2. = width / 2. |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
194 # 224. = 448. / 2. = height / 2. |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
195 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2)) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
196 # This is so that objects on the (O, x, y) plane use pixel coordinates |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
197 gluLookAt(192., 224., - 835.979370 * dz, |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
198 192. + dx, 224. - dy, 0., 0., -1., 0.) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
199 glTranslatef(-x, -y, -z) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
200 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
201 glEnable(GL_DEPTH_TEST) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
202 for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in get_background_rendering_data(back): |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
203 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc]) |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
204 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key].id) |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
205 glVertexPointer(3, GL_FLOAT, 0, vertices) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
206 glTexCoordPointer(2, GL_FLOAT, 0, uvs) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
207 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
208 glDrawArrays(GL_QUADS, 0, nb_vertices) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
209 glDisable(GL_DEPTH_TEST) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
210 else: |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
211 glClear(GL_COLOR_BUFFER_BIT) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
212 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
213 if game is not None: |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
214 glMatrixMode(GL_MODELVIEW) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
215 glLoadIdentity() |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
216 # Some explanations on the magic constants: |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
217 # 192. = 384. / 2. = width / 2. |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
218 # 224. = 448. / 2. = height / 2. |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
219 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2)) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
220 # This is so that objects on the (O, x, y) plane use pixel coordinates |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
221 gluLookAt(192., 224., - 835.979370, |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
222 192., 224., 0., 0., -1., 0.) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
223 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
224 glDisable(GL_FOG) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
225 self.render_elements(game.enemies) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
226 self.render_elements(game.game_state.bullets) |
130 | 227 self.render_elements(game.players) |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
228 glEnable(GL_FOG) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
229 |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
230 #TODO |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
231 glMatrixMode(GL_MODELVIEW) |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
232 glLoadIdentity() |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
233 gluLookAt(192., 224., 835.979370, |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
234 192, 224., 0., 0., 1., 0.) |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
235 self.fps_display.draw() |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
236 |