1
|
|
|
import math |
2
|
|
|
from ed2d import window |
3
|
|
|
from ed2d import sysevents |
4
|
|
|
from ed2d.events import Events |
5
|
|
|
from ed2d import context |
6
|
|
|
from ed2d import timing |
7
|
|
|
from ed2d import files |
8
|
|
|
from ed2d import shaders |
9
|
|
|
from ed2d.opengl import gl |
10
|
|
|
from ed2d.opengl import pgl |
11
|
|
|
from gem import vector |
12
|
|
|
from gem import matrix |
13
|
|
|
from ed2d import texture |
14
|
|
|
from ed2d import mesh |
15
|
|
|
from ed2d import view |
16
|
|
|
from ed2d import text |
17
|
|
|
from ed2d import camera |
18
|
|
|
from ed2d.scenegraph import SceneGraph |
19
|
|
|
from ed2d.assets import objloader |
20
|
|
|
from ed2d import cursor |
21
|
|
|
|
22
|
|
|
class GameManager(object): |
23
|
|
|
''' Entry point into the game, and manages the game in general ''' |
24
|
|
|
def __init__(self): |
25
|
|
|
|
26
|
|
|
self.width = 800 |
27
|
|
|
self.height = 600 |
28
|
|
|
self.title = "ed2d" |
29
|
|
|
self.running = False |
30
|
|
|
|
31
|
|
|
self.fpsTimer = timing.FpsCounter() |
32
|
|
|
self.fpsEstimate = 0 |
33
|
|
|
|
34
|
|
|
self.sysEvents = sysevents.SystemEvents() |
35
|
|
|
self.window = window.Window(self.title, self.width, self.height, window.WindowedMode) |
36
|
|
|
self.context = context.Context(3, 3, 2) |
37
|
|
|
self.context.window = self.window |
38
|
|
|
|
39
|
|
|
Events.add_listener(self.process_event) |
40
|
|
|
|
41
|
|
|
self.keys = [] |
42
|
|
|
|
43
|
|
|
# Mouse Information |
44
|
|
|
self.mousePos = [0.0, 0.0] |
45
|
|
|
self.mouseButtons = [] |
46
|
|
|
self.oldMouseX = 0 |
47
|
|
|
self.oldMouseY = 0 |
48
|
|
|
self.mousePosX = 0 |
49
|
|
|
self.mousePosY = 0 |
50
|
|
|
cursor.set_relative_mode(False) |
51
|
|
|
cursor.show_cursor() |
52
|
|
|
|
53
|
|
|
gl.init() |
54
|
|
|
major = pgl.glGetInteger(gl.GL_MAJOR_VERSION) |
55
|
|
|
minor = pgl.glGetInteger(gl.GL_MINOR_VERSION) |
56
|
|
|
print('OpenGL Version: {}.{}'.format(major, minor)) |
57
|
|
|
|
58
|
|
|
gl.glViewport(0, 0, self.width, self.height) |
59
|
|
|
|
60
|
|
|
# For CSG to work properly |
61
|
|
|
gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) |
62
|
|
|
gl.glEnable(gl.GL_DEPTH_TEST) |
63
|
|
|
gl.glEnable(gl.GL_CULL_FACE) |
64
|
|
|
gl.glEnable(gl.GL_MULTISAMPLE) |
65
|
|
|
|
66
|
|
|
gl.glClearColor(0.0, 0.0, 0.4, 0.0) |
67
|
|
|
|
68
|
|
|
vsPath = files.resolve_path('data', 'shaders', 'main2.vs') |
69
|
|
|
fsPath = files.resolve_path('data', 'shaders', 'main2.fs') |
70
|
|
|
|
71
|
|
|
vertex = shaders.VertexShader(vsPath) |
72
|
|
|
fragment = shaders.FragmentShader(fsPath) |
73
|
|
|
self.program = shaders.ShaderProgram(vertex, fragment) |
74
|
|
|
self.program.use() |
75
|
|
|
|
76
|
|
|
#self.testID1 = self.program.new_uniform(b'perp') |
77
|
|
|
self.testID2 = self.program.new_uniform(b'view') |
78
|
|
|
|
79
|
|
|
self.vao = pgl.glGenVertexArrays(1) |
80
|
|
|
|
81
|
|
|
self.scenegraph = SceneGraph() |
82
|
|
|
|
83
|
|
|
# Creating a object steps: |
84
|
|
|
# Create a mesh object to render |
85
|
|
|
objFL = objloader.OBJ('buildings') |
86
|
|
|
self.meshTest = mesh.Mesh() |
87
|
|
|
self.meshTest.fromData(objFL) |
88
|
|
|
self.meshTest.addProgram(self.program) |
89
|
|
|
self.meshTestID = self.scenegraph.establish(self.meshTest) |
90
|
|
|
self.meshTest.translate(0.0, 0.0, 0.0) |
91
|
|
|
|
92
|
|
|
self.camera = camera.Camera() |
93
|
|
|
self.camera.orthographicProjection(0.0, self.width, self.height, 0.0, -1.0, 1.0) |
94
|
|
|
self.camera.perspectiveProjection(75.0, float(self.width) / float(self.height), 0.1, 10000.0) |
95
|
|
|
self.camera.setPosition(vector.Vector(3, data=[0.5, -2.0, 10.0])) |
96
|
|
|
self.camera.set_program(2, self.program) |
97
|
|
|
self.model = matrix.Matrix(4) |
98
|
|
|
#self.model = matrix.Matrix(4).translate(vector.Vector(3, data=[4.0, -2.0, -8])) |
99
|
|
|
self.loadText() |
100
|
|
|
|
101
|
|
|
glerr = gl.glGetError() |
102
|
|
|
if glerr != 0: |
103
|
|
|
print('GLError:', glerr) |
104
|
|
|
|
105
|
|
|
def loadText(self): |
106
|
|
|
vsPath = files.resolve_path('data', 'shaders', 'font.vs') |
107
|
|
|
fsPath = files.resolve_path('data', 'shaders', 'font.fs') |
108
|
|
|
|
109
|
|
|
vertex = shaders.VertexShader(vsPath) |
110
|
|
|
fragment = shaders.FragmentShader(fsPath) |
111
|
|
|
self.textProgram = shaders.ShaderProgram(vertex, fragment) |
112
|
|
|
|
113
|
|
|
fontPath = files.resolve_path('data', 'SourceCodePro-Regular.ttf') |
114
|
|
|
self.font = text.Font(12, fontPath) |
115
|
|
|
self.text = text.Text(self.textProgram, self.font) |
116
|
|
|
|
117
|
|
|
self.camera.set_program(1, self.textProgram) |
118
|
|
|
|
119
|
|
|
def resize(self, width, height): |
120
|
|
|
self.width = width |
121
|
|
|
self.height = height |
122
|
|
|
gl.glViewport(0, 0, self.width, self.height) |
123
|
|
|
self.camera.perspectiveProjection(75.0, float(self.width) / float(self.height), 0.1, 10000.0) |
124
|
|
|
self.camera.orthographicProjection(0.0, self.width, self.height, 0.0, -1.0, 1.0) |
125
|
|
|
|
126
|
|
|
def process_event(self, event, data): |
127
|
|
|
if event == 'quit' or event == 'window_close': |
128
|
|
|
self.running = False |
129
|
|
|
elif event == 'window_resized': |
130
|
|
|
winID, x, y = data |
131
|
|
|
self.resize(x, y) |
132
|
|
|
elif event == 'mouse_move': |
133
|
|
|
if cursor.is_relative(): |
134
|
|
|
xrel, yrel = data |
135
|
|
|
self.mousePosX += xrel |
136
|
|
|
self.mousePosY += yrel |
137
|
|
|
else: |
138
|
|
|
self.mousePosX, self.mousePosY = data |
139
|
|
|
elif event == 'key_down': |
|
|
|
|
140
|
|
|
if data[0] == 'c': |
141
|
|
|
cursor.set_relative_mode(True) |
142
|
|
|
elif data[0] == 'r': |
143
|
|
|
cursor.set_relative_mode(False) |
144
|
|
|
self.keys.append(data[0]) |
145
|
|
|
print(self.keys) |
146
|
|
|
elif event == 'key_up': |
147
|
|
|
self.keys.remove(data[0]) |
148
|
|
|
elif event == 'mouse_button_down': |
149
|
|
|
self.mouseButtons.append(data[0]) |
150
|
|
|
print(self.mouseButtons) |
151
|
|
|
elif event == 'mouse_button_up': |
152
|
|
|
self.mouseButtons.remove(data[0]) |
153
|
|
|
|
154
|
|
|
def keyUpdate(self): |
155
|
|
|
self.camera.onKeys(self.keys, self.fpsTimer.tickDelta) |
156
|
|
|
|
157
|
|
|
def mouseUpdate(self): |
158
|
|
|
if cursor.is_relative(): |
159
|
|
|
if 1 in self.mouseButtons: |
160
|
|
|
self.camera.onMouseMove(self.oldMouseX - self.mousePosX, self.oldMouseY - self.mousePosY, self.fpsTimer.tickDelta) |
161
|
|
|
|
162
|
|
|
self.oldMouseX = self.mousePosX |
163
|
|
|
self.oldMouseY = self.mousePosY |
164
|
|
|
|
165
|
|
|
def update(self): |
166
|
|
|
self.scenegraph.update() |
167
|
|
|
|
168
|
|
|
def render(self): |
169
|
|
|
gl.glClearColor(0.3, 0.3, 0.3, 1.0) |
170
|
|
|
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) |
171
|
|
|
|
172
|
|
|
# Change view to perspective projection |
173
|
|
|
gl.glDisable(gl.GL_BLEND) |
174
|
|
|
|
175
|
|
|
self.program.use() |
176
|
|
|
self.camera.set_mode(2) |
177
|
|
|
view = self.camera.getViewMatrix() |
178
|
|
|
self.program.set_uniform_matrix(self.testID2, view) |
179
|
|
|
|
180
|
|
|
# Draw 3D stuff |
181
|
|
|
gl.glBindVertexArray(self.vao) |
182
|
|
|
|
183
|
|
|
self.scenegraph.render() |
184
|
|
|
|
185
|
|
|
gl.glBindVertexArray(0) |
186
|
|
|
|
187
|
|
|
gl.glEnable(gl.GL_BLEND) |
188
|
|
|
gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) |
189
|
|
|
|
190
|
|
|
# Change to orthographic projection to draw the text |
191
|
|
|
self.textProgram.use() |
192
|
|
|
self.camera.set_mode(1) |
193
|
|
|
self.text.draw_text(str(self.fpsEstimate) + ' FPS', 0, 10) |
194
|
|
|
|
195
|
|
|
gl.glDisable(gl.GL_BLEND) |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
def do_run(self): |
199
|
|
|
''' Process a single loop ''' |
200
|
|
|
self.sysEvents.process() |
201
|
|
|
self.mouseUpdate() |
202
|
|
|
self.keyUpdate() |
203
|
|
|
self.update() |
204
|
|
|
self.render() |
205
|
|
|
self.window.flip() |
206
|
|
|
self.fpsTimer.tick() |
207
|
|
|
|
208
|
|
|
if self.fpsTimer.fpsTime >= 2000: |
209
|
|
|
self.fpsEstimate = self.fpsTimer.get_fps() |
210
|
|
|
print("{:.2f} fps".format(self.fpsEstimate)) |
211
|
|
|
|
212
|
|
|
def run(self): |
213
|
|
|
''' Called from launcher doesnt exit until the game is quit ''' |
214
|
|
|
self.running = True |
215
|
|
|
while self.running: |
216
|
|
|
self.do_run() |
217
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.