comparison pytouhou/opengl/gamerenderer.py @ 119:fad7b44cebf2

Switch from pygame + PyOpenGL to pyglet
author Thibaut Girka <thib@sitedethib.com>
date Wed, 07 Sep 2011 18:12:24 +0200
parents 340fcda8e64a
children 4300a832f033
comparison
equal deleted inserted replaced
118:c596a1a69402 119:fad7b44cebf2
13 ## 13 ##
14 14
15 import struct 15 import struct
16 from itertools import chain 16 from itertools import chain
17 17
18 import pygame 18 import pyglet
19 19 from pyglet.gl import *
20 import OpenGL
21 OpenGL.FORWARD_COMPATIBLE_ONLY = True
22 from OpenGL.GL import *
23 from OpenGL.GLU import *
24
25 20
26 from pytouhou.opengl.texture import TextureManager 21 from pytouhou.opengl.texture import TextureManager
27 from pytouhou.opengl.sprite import get_sprite_rendering_data 22 from pytouhou.opengl.sprite import get_sprite_rendering_data
28 from pytouhou.opengl.background import get_background_rendering_data 23 from pytouhou.opengl.background import get_background_rendering_data
29 24
30 25
31 class GameRenderer(object): 26 class GameRenderer(pyglet.window.Window):
32 def __init__(self, resource_loader, game=None, background=None): 27 def __init__(self, resource_loader, game=None, background=None):
28 pyglet.window.Window.__init__(self, caption='PyTouhou', resizable=False)
29 self.keys = pyglet.window.key.KeyStateHandler()
30 self.push_handlers(self.keys)
31
33 self.texture_manager = TextureManager(resource_loader) 32 self.texture_manager = TextureManager(resource_loader)
33
34 self.fps_display = pyglet.clock.ClockDisplay()
34 35
35 self.game = game 36 self.game = game
36 self.background = background 37 self.background = background
37 38
38 self.window = None
39
40 39
41 def start(self, width=384, height=448): 40 def start(self, width=384, height=448):
42 # Initialize pygame 41 self.set_size(width, height)
43 pygame.init()
44 self.window = pygame.display.set_mode((width, height),
45 pygame.OPENGL | pygame.DOUBLEBUF)
46 42
47 # Initialize OpenGL 43 # Initialize OpenGL
48 glMatrixMode(GL_PROJECTION) 44 glMatrixMode(GL_PROJECTION)
49 glLoadIdentity() 45 glLoadIdentity()
50 gluPerspective(30, float(width)/float(height), 46 gluPerspective(30, float(width)/float(height),
56 glHint(GL_FOG_HINT, GL_NICEST) 52 glHint(GL_FOG_HINT, GL_NICEST)
57 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) 53 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
58 glEnableClientState(GL_COLOR_ARRAY) 54 glEnableClientState(GL_COLOR_ARRAY)
59 glEnableClientState(GL_VERTEX_ARRAY) 55 glEnableClientState(GL_VERTEX_ARRAY)
60 glEnableClientState(GL_TEXTURE_COORD_ARRAY) 56 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
57
58 pyglet.clock.schedule_interval(self.update, 1./120)
59 pyglet.app.run()
60
61
62 def on_resize(self, width, height):
63 glViewport(0, 0, width, height)
64
65
66
67 def update(self, dt):
68 if self.background:
69 self.background.update(self.game.game_state.frame)
70 if self.game:
71 self.game.run_iter(0) #TODO: self.keys...
72
73
74 def on_key_press(self, symbol, modifiers):
75 if symbol == pyglet.window.key.ESCAPE:
76 pyglet.app.exit()
77 # XXX: Fullscreen will be enabled the day pyglet stops sucking
78 elif symbol == pyglet.window.key.F11:
79 self.set_fullscreen(not self.fullscreen)
61 80
62 81
63 def render_elements(self, elements): 82 def render_elements(self, elements):
64 texture_manager = self.texture_manager 83 texture_manager = self.texture_manager
65 objects_by_texture = {} 84 objects_by_texture = {}
74 rec[1].extend(uvs) 93 rec[1].extend(uvs)
75 rec[2].extend(colors) 94 rec[2].extend(colors)
76 95
77 for (texture_key, blendfunc), (vertices, uvs, colors) in objects_by_texture.items(): 96 for (texture_key, blendfunc), (vertices, uvs, colors) in objects_by_texture.items():
78 nb_vertices = len(vertices) 97 nb_vertices = len(vertices)
98 vertices = struct.pack(str(3 * nb_vertices) + 'f', *chain(*vertices))
99 uvs = struct.pack(str(2 * nb_vertices) + 'f', *chain(*uvs))
100 colors = struct.pack(str(4 * nb_vertices) + 'B', *chain(*colors))
79 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc]) 101 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc])
80 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key]) 102 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key].id)
81 glVertexPointer(3, GL_FLOAT, 0, struct.pack(str(3 * nb_vertices) + 'f', *chain(*vertices))) 103 glVertexPointer(3, GL_FLOAT, 0, vertices)
82 glTexCoordPointer(2, GL_FLOAT, 0, struct.pack(str(2 * nb_vertices) + 'f', *chain(*uvs))) 104 glTexCoordPointer(2, GL_FLOAT, 0, uvs)
83 glColorPointer(4, GL_UNSIGNED_BYTE, 0, struct.pack(str(4 * nb_vertices) + 'B', *chain(*colors))) 105 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors)
84 glDrawArrays(GL_QUADS, 0, nb_vertices) 106 glDrawArrays(GL_QUADS, 0, nb_vertices)
85 107
86 108
87 def render(self): 109 def on_draw(self):
88 glClear(GL_DEPTH_BUFFER_BIT) 110 glClear(GL_DEPTH_BUFFER_BIT)
89 111
90 back = self.background 112 back = self.background
91 game = self.game 113 game = self.game
92 texture_manager = self.texture_manager 114 texture_manager = self.texture_manager
97 dx, dy, dz = back.position2_interpolator.values 119 dx, dy, dz = back.position2_interpolator.values
98 120
99 glFogi(GL_FOG_MODE, GL_LINEAR) 121 glFogi(GL_FOG_MODE, GL_LINEAR)
100 glFogf(GL_FOG_START, fog_start) 122 glFogf(GL_FOG_START, fog_start)
101 glFogf(GL_FOG_END, fog_end) 123 glFogf(GL_FOG_END, fog_end)
102 glFogfv(GL_FOG_COLOR, (fog_r / 255., fog_g / 255., fog_b / 255., 1.)) 124 glFogfv(GL_FOG_COLOR, (GLfloat * 4)(fog_r / 255., fog_g / 255., fog_b / 255., 1.))
103 125
104 glMatrixMode(GL_MODELVIEW) 126 glMatrixMode(GL_MODELVIEW)
105 glLoadIdentity() 127 glLoadIdentity()
106 # Some explanations on the magic constants: 128 # Some explanations on the magic constants:
107 # 192. = 384. / 2. = width / 2. 129 # 192. = 384. / 2. = width / 2.
113 glTranslatef(-x, -y, -z) 135 glTranslatef(-x, -y, -z)
114 136
115 glEnable(GL_DEPTH_TEST) 137 glEnable(GL_DEPTH_TEST)
116 for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in get_background_rendering_data(back): 138 for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in get_background_rendering_data(back):
117 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc]) 139 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc])
118 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key]) 140 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key].id)
119 glVertexPointer(3, GL_FLOAT, 0, vertices) 141 glVertexPointer(3, GL_FLOAT, 0, vertices)
120 glTexCoordPointer(2, GL_FLOAT, 0, uvs) 142 glTexCoordPointer(2, GL_FLOAT, 0, uvs)
121 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors) 143 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors)
122 glDrawArrays(GL_QUADS, 0, nb_vertices) 144 glDrawArrays(GL_QUADS, 0, nb_vertices)
123 glDisable(GL_DEPTH_TEST) 145 glDisable(GL_DEPTH_TEST)
124 else: 146 else:
125 glClear(GL_COLOR_BUFFER_BIT) 147 glClear(GL_COLOR_BUFFER_BIT)
126
127 148
128 if game is not None: 149 if game is not None:
129 glMatrixMode(GL_MODELVIEW) 150 glMatrixMode(GL_MODELVIEW)
130 glLoadIdentity() 151 glLoadIdentity()
131 # Some explanations on the magic constants: 152 # Some explanations on the magic constants:
139 glDisable(GL_FOG) 160 glDisable(GL_FOG)
140 self.render_elements(game.enemies) 161 self.render_elements(game.enemies)
141 self.render_elements(game.game_state.bullets) 162 self.render_elements(game.game_state.bullets)
142 glEnable(GL_FOG) 163 glEnable(GL_FOG)
143 164
165 #TODO
166 glMatrixMode(GL_MODELVIEW)
167 glLoadIdentity()
168 gluLookAt(192., 224., 835.979370,
169 192, 224., 0., 0., 1., 0.)
170 self.fps_display.draw()
171