-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
109 lines (78 loc) · 3.06 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
97
98
99
100
101
102
103
104
105
106
107
108
109
import math
import ctypes
import pyglet
pyglet.options["shadow_window"] = False
pyglet.options["debug_gl"] = False
import pyglet.gl as gl
import matrix
import shader
import camera
import world
class Window(pyglet.window.Window):
def __init__(self, **args):
super().__init__(**args)
self.world = world.World()
self.set_exclusive_mouse(True)
self.shader = shader.Shader("vert.glsl", "frag.glsl")
self.shader_sampler_location = self.shader.find_uniform(b"texture_array_sampler")
self.shader.use()
pyglet.clock.schedule_interval(self.update, 1.0 / 10000)
self.mouse_captured = False
self.camera = camera.Camera(self.shader, self.width, self.height)
def update(self, delta_time):
print(f"FPS {1 / delta_time}")
if not self.mouse_captured:
self.camera.input = [0, 0, 0]
self.camera.update_camera(delta_time)
def on_draw(self):
self.camera.update_matrices()
gl.glActiveTexture(gl.GL_TEXTURE0)
gl.glBindTexture(gl.GL_TEXTURE_2D_ARRAY, self.world.texture_manager.texture_array)
gl.glUniform1i(self.shader_sampler_location, 0)
gl.glEnable(gl.GL_DEPTH_TEST)
gl.glEnable(gl.GL_CULL_FACE)
gl.glClearColor(0.0, 0.0, 0.0, 1.0)
self.clear()
self.world.draw()
gl.glFinish()
def on_resize(self, width, height):
print(f"Resize {width} * {height}")
gl.glViewport(0, 0, width, height)
self.camera.width = width
self.camera.height = height
def on_mouse_press(self, x, y, button, modifiers):
self.mouse_captured = not self.mouse_captured
self.set_exclusive_mouse(self.mouse_captured)
def on_mouse_motion(self, x, y, delta_x, delta_y):
if self.mouse_captured:
sensitivity = 0.004
self.camera.rotation[0] -= delta_x * sensitivity
self.camera.rotation[1] += delta_y * sensitivity
self.camera.rotation[1] = max(-math.tau / 4, min(math.tau / 4, self.camera.rotation[1]))
def on_key_press(self, key, modifiers):
if not self.mouse_captured:
return
if key == pyglet.window.key.D: self.camera.input[0] += 1
elif key == pyglet.window.key.A: self.camera.input[0] -= 1
elif key == pyglet.window.key.W: self.camera.input[2] += 1
elif key == pyglet.window.key.S: self.camera.input[2] -= 1
elif key == pyglet.window.key.SPACE : self.camera.input[1] += 1
elif key == pyglet.window.key.LSHIFT: self.camera.input[1] -= 1
def on_key_release(self, key, modifiers):
if not self.mouse_captured:
return
if key == pyglet.window.key.D: self.camera.input[0] -= 1
elif key == pyglet.window.key.A: self.camera.input[0] += 1
elif key == pyglet.window.key.W: self.camera.input[2] -= 1
elif key == pyglet.window.key.S: self.camera.input[2] += 1
elif key == pyglet.window.key.SPACE : self.camera.input[1] -= 1
elif key == pyglet.window.key.LSHIFT: self.camera.input[1] += 1
class Game:
def __init__(self):
self.config = gl.Config(double_buffer = True, major_version = 3, minor_version = 3, depth_size = 16)
self.window = Window(config = self.config, width = 800, height = 600, caption = "MinePy", resizable = True, vsync = False)
def run(self):
pyglet.app.run()
if __name__ == "__main__":
game = Game()
game.run()