/
main.py
91 lines (67 loc) · 2.41 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
def main():
import os
import sys
os.chdir(os.path.dirname(__file__))
import glfw
import time
from engine import Engine
# Initialize the library
if not glfw.init():
sys.exit()
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
glfw.window_hint(glfw.SAMPLES, 16)
# Create a windowed mode window and its OpenGL context
window = glfw.create_window(640, 480, "Window Name", None, None)
if not window:
print "Couldn't initialize OpenGL. Check that your OpenGL version >4.3."
glfw.terminate()
sys.exit()
# Make the window's context current
glfw.make_context_current(window)
# Get window size
width, height = glfw.get_framebuffer_size(window)
# Create engine
engine = Engine(window)
engine.setWindowHeight(height)
engine.setWindowWidth(width)
def on_resize(window, width, height):
engine.setWindowWidth(width)
engine.setWindowHeight(height)
# Install a window size handler
glfw.set_window_size_callback(window, on_resize)
def on_key(window, key, scancode, action, mods):
if key == glfw.KEY_ESCAPE and action == glfw.PRESS:
glfw.set_window_should_close(window, 1)
# Install a key handler
glfw.set_key_callback(window, on_key)
def on_mouse(window, button, action, mods):
if button == glfw.MOUSE_BUTTON_1 and action == glfw.PRESS:
engine.shoot_on()
if button == glfw.MOUSE_BUTTON_1 and action == glfw.RELEASE:
engine.shoot_off()
if button == glfw.MOUSE_BUTTON_2 and action == glfw.PRESS:
engine.camera_switch()
glfw.set_mouse_button_callback(window, on_mouse)
def on_scroll(window, x, y):
engine.camera_scroll(y)
glfw.set_scroll_callback(window, on_scroll)
old_time = time.time()
elapsed_time = 0.0
# Loop until the user closes the window
while not glfw.window_should_close(window):
# Calculate elapsed time
elapsed_time = time.time() - old_time
old_time = time.time()
# Process
engine.step(elapsed_time)
# Swap front and back buffers
glfw.swap_interval(1)
glfw.swap_buffers(window)
# Poll for and process events
glfw.poll_events()
# Don't be egoist :)
time.sleep(0.01)
glfw.terminate()
if __name__ == '__main__':
main()