1 | from ed2d import window |
||
2 | from ed2d import sysevents |
||
3 | from ed2d.events import Events |
||
4 | from ed2d import context |
||
5 | from ed2d import timing |
||
6 | from ed2d import files |
||
7 | from ed2d import shaders |
||
8 | from ed2d.opengl import gl |
||
9 | from ed2d.opengl import pgl |
||
10 | from gem import matrix |
||
11 | from ed2d import text |
||
12 | from ed2d import view |
||
13 | from ed2d import menu |
||
14 | |||
15 | class GameManager(object): |
||
16 | ''' Entry point into the game, and manages the game in general ''' |
||
17 | def __init__(self): |
||
18 | |||
19 | self.width = 800 |
||
20 | self.height = 600 |
||
21 | self.title = "Cubix" |
||
22 | self.running = False |
||
23 | |||
24 | self.fpsTimer = timing.FpsCounter() |
||
25 | self.fpsEstimate = 0 |
||
26 | |||
27 | self.sysEvents = sysevents.SystemEvents() |
||
28 | self.window = window.Window(self.title, self.width, self.height, window.WindowedMode) |
||
29 | self.context = context.Context(3, 3, 2) |
||
30 | self.context.window = self.window |
||
31 | |||
32 | Events.add_listener(self.process_event) |
||
33 | |||
34 | self.keys = [] |
||
35 | |||
36 | gl.init() |
||
37 | major = pgl.glGetInteger(gl.GL_MAJOR_VERSION) |
||
38 | minor = pgl.glGetInteger(gl.GL_MINOR_VERSION) |
||
39 | print('OpenGL Version: ', major, '.', minor) |
||
40 | |||
41 | gl.glViewport(0, 0, self.width, self.height) |
||
42 | |||
43 | vsPath = files.resolve_path('data', 'shaders', 'font.vs') |
||
44 | fsPath = files.resolve_path('data', 'shaders', 'font.fs') |
||
45 | |||
46 | vertex = shaders.VertexShader(vsPath) |
||
47 | fragment = shaders.FragmentShader(fsPath) |
||
48 | self.program = shaders.ShaderProgram(vertex, fragment) |
||
49 | |||
50 | fontPath = files.resolve_path('data', 'SourceCodePro-Regular.ttf') |
||
51 | self.font = text.Font(12, fontPath) |
||
52 | self.text = text.Text(self.program, self.font) |
||
53 | self.textScroll = 0 |
||
54 | self.meshes = [] |
||
55 | |||
56 | self.view = view.View() |
||
57 | self.ortho = matrix.orthographic(0.0, self.width, self.height, 0.0, -1.0, 1.0) |
||
58 | self.view.new_projection('ortho', self.ortho) |
||
59 | self.view.register_shader('ortho', self.program) |
||
60 | |||
61 | glerr = gl.glGetError() |
||
62 | if glerr != 0: |
||
63 | print('GLError:', glerr) |
||
64 | |||
65 | def resize(self, width, height): |
||
66 | self.width = width |
||
67 | self.height = height |
||
68 | gl.glViewport(0, 0, self.width, self.height) |
||
69 | self.ortho = matrix.orthographic(0.0, self.width, self.height, 0.0, -1.0, 1.0) |
||
70 | self.view.set_projection('ortho', self.ortho) |
||
71 | |||
72 | View Code Duplication | def process_event(self, event, data): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
73 | if event == 'quit' or event == 'window_close': |
||
74 | self.running = False |
||
75 | elif event == 'window_resized': |
||
76 | winID, x, y = data |
||
77 | self.resize(x, y) |
||
78 | elif event == 'mouse_move': |
||
79 | x, y = data |
||
80 | elif event == 'key_down': |
||
81 | self.keys.append(data[0]) |
||
82 | print(self.keys) |
||
83 | elif event == 'key_up': |
||
84 | self.keys.remove(data[0]) |
||
85 | |||
86 | def update(self): |
||
87 | pass |
||
88 | |||
89 | def render(self): |
||
90 | gl.glClearColor(0.5, 0.5, 0.5, 1.0) |
||
91 | gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) |
||
92 | gl.glEnable(gl.GL_BLEND) |
||
93 | gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) |
||
94 | |||
95 | self.text.draw_text(str(self.fpsEstimate), 0, 10) |
||
96 | |||
97 | def exit(self): |
||
98 | '''Commands to run before exit.''' |
||
99 | self.font.delete() |
||
100 | |||
101 | View Code Duplication | def do_run(self): |
|
0 ignored issues
–
show
|
|||
102 | ''' Process a single loop ''' |
||
103 | self.sysEvents.process() |
||
104 | self.update() |
||
105 | self.render() |
||
106 | self.window.flip() |
||
107 | self.fpsTimer.tick() |
||
108 | if self.fpsTimer.fpsTime >= 2000: |
||
109 | self.fpsEstimate = self.fpsTimer.get_fps() |
||
110 | print("{:.2f} fps".format(self.fpsEstimate)) |
||
111 | |||
112 | def run(self): |
||
113 | ''' Called from launcher doesnt exit until the game is quit ''' |
||
114 | self.running = True |
||
115 | while self.running: |
||
116 | self.do_run() |
||
117 | self.exit() |
||
118 |