/
main.py
96 lines (73 loc) · 2.93 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import sys
import logging.config
import ctypes
import pyglet
from pyglet import gl
from pyglet.window import key
import shaders
from gltools.glprogram import GlProgram
from gltools import gltexture
from dungeon_map import DungeonMap
class Application:
def __init__(self, window):
self.window = window
self.initialize_gl()
self.dungeon_map = DungeonMap()
def update(self, dt):
pass
def initialize_gl(self):
self.program = GlProgram(shaders.vertex_sprite, shaders.fragment_sprite)
self.program.uniform2f(b'offset', 0, 0)
self.buffer = gl.GLuint(0)
gl.glGenBuffers(1, ctypes.pointer(self.buffer))
gl.glActiveTexture(gl.GL_TEXTURE0)
self.sprite_texture = gltexture.make_texture('sprites.png')
gl.glBindTexture(gl.GL_TEXTURE_2D, self.sprite_texture)
self.program.uniform1i(b'tex', 0)
gl.glEnable(gl.GL_BLEND)
gl.glEnable(gl.GL_DEPTH_TEST)
gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
def on_resize(self, width, height):
logging.debug('window resized to %sx%s', width, height)
gl.glViewport(0, 0, width, height)
TILE_SIZE = 64
self.program.uniform2f(b'scale', 2*TILE_SIZE/width, 2*TILE_SIZE/height)
self.vp_width = width
self.vp_height = height
def on_draw(self):
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
STRIDE = 8
self.program.use()
self.program.vertex_attrib_pointer(self.buffer, b'position', 4, stride=STRIDE * ctypes.sizeof(gl.GLfloat))
self.program.vertex_attrib_pointer(self.buffer, b'tex_coord', 4, stride=STRIDE * ctypes.sizeof(gl.GLfloat), offset=4 * ctypes.sizeof(gl.GLfloat))
nb_vertices = 6*40*40
data = self.dungeon_map.vertex_data()
data = (gl.GLfloat * (STRIDE * nb_vertices))(*data)
gl.glBufferData(gl.GL_ARRAY_BUFFER, ctypes.sizeof(data), data, gl.GL_DYNAMIC_DRAW)
gl.glDrawArrays(gl.GL_TRIANGLES, 0, nb_vertices)
def on_key_press(self, symbol, modifiers):
logging.debug('Key Press {} {}'.format(symbol, modifiers))
if symbol == key.I:
logging.info('FPS: {}'.format(pyglet.clock.get_fps()))
def initialize_gl(context):
logging.info('OpenGL Version {}'.format(context.get_info().get_version()))
gl.glClearColor(0.5, 0.5, 0.35, 1)
def main():
logging.config.fileConfig('logging.conf')
try:
if not ctypes.windll.user32.SetProcessDPIAware():
logging.error('could not set dpi awareness')
except AttributeError:
pass
try:
window = pyglet.window.Window(resizable=True)
initialize_gl(window.context)
app = Application(window)
window.push_handlers(app)
pyglet.clock.schedule_interval(app.update, 0.01)
pyglet.app.run()
except:
logging.exception('Uncaught Exception')
sys.exit(1)
if __name__ == '__main__':
main()