Completed
Push — master ( 67fa70...e887f8 )
by
unknown
01:02
created

framework.game.GameManager.__init__()   A

Complexity

Conditions 3

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 50
rs 9.3333
1
from ed2d import window
2
from ed2d import events
3
from ed2d import context
4
from ed2d import timing
5
from ed2d import files
6
from ed2d import shaders
7
from ed2d.opengl import gl
8
from ed2d.opengl import pgl
9
from gem import matrix
10
from ed2d import text
11
from ed2d import view
12
13
class GameManager(object):
14
    ''' Entry point into the game, and manages the game in general '''
15
    def __init__(self):
16
17
        self.width = 800
18
        self.height = 600
19
        self.title = "Cubix"
20
        self.running = False
21
22
        self.fpsTimer = timing.FpsCounter()
23
        self.fpsEstimate = 0
24
25
        self.events = events.Events()
26
        self.window = window.Window(self.title, self.width, self.height, window.WindowedMode)
27
        self.context = context.Context(3, 3, 2)
28
        self.context.window = self.window
29
30
        self.events.add_listener(self.process_event)
31
32
        self.keys = []
33
34
        gl.init()
35
        major = pgl.glGetInteger(gl.GL_MAJOR_VERSION)
36
        minor = pgl.glGetInteger(gl.GL_MINOR_VERSION)
37
        print('OpenGL Version: ', major, '.', minor)
38
39
        gl.glViewport(0, 0, self.width, self.height)
40
41
        vsPath = files.resolve_path('data', 'shaders', 'font.vs')
42
        fsPath = files.resolve_path('data', 'shaders', 'font.fs')
43
44
        vertex = shaders.VertexShader(vsPath)
45
        fragment = shaders.FragmentShader(fsPath)
46
        self.program = shaders.ShaderProgram(vertex, fragment)
47
48
        fontPath = files.resolve_path('data', 'SourceCodePro-Regular.ttf')
49
        self.font = text.Font(12, fontPath)
50
        self.text = text.Text(self.program, self.font)
51
        self.textScroll = 0
52
        self.meshes = []
53
54
        self.view = view.View()
55
        self.ortho = matrix.orthographic(0.0, self.width, self.height, 0.0, -1.0, 1.0)
56
        self.view.new_projection('ortho', self.ortho)
57
        self.view.register_shader('ortho', self.program)
58
59
        with open("./game/gametestmanager.py", "r") as myfile:
60
            self.data=myfile.read()
61
62
        glerr = gl.glGetError()
63
        if glerr != 0:
64
            print('GLError:', glerr)
65
66
    def resize(self, width, height):
67
        self.width = width
68
        self.height = height
69
        gl.glViewport(0, 0, self.width, self.height)
70
        self.ortho = matrix.orthographic(0.0, self.width, self.height, 0.0, -1.0, 1.0)
71
        self.view.set_projection('ortho', self.ortho)
72
73
    def process_event(self, event, data):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.

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.

Loading history...
74
        if event == 'quit' or event == 'window_close':
75
            self.running = False
76
        elif event == 'window_resized':
77
            winID, x, y = data
78
            self.resize(x, y)
79
        elif event == 'mouse_move':
80
            x, y = data
81
        elif event == 'key_down':
82
            self.keys.append(data[0])
83
            print(self.keys)
84
        elif event == 'key_up':
85
            self.keys.remove(data[0])
86
87
    def update(self):
88
        if 'DOWN' in self.keys:
89
            self.textScroll -= 6
90
        elif 'UP' in self.keys:
91
            self.textScroll += 6
92
93
94
    def render(self):
95
        gl.glClearColor(0.5, 0.5, 0.5, 1.0)
96
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
97
        gl.glEnable(gl.GL_BLEND)
98
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
99
100
        self.text.draw_text(self.data, 0, 10 + self.textScroll)
101
102
    def exit(self):
103
        '''Commands to run before exit.'''
104
        self.font.delete()
105
106
    def do_run(self):
107
        ''' Process a single loop '''
108
        self.events.process()
109
        self.update()
110
        self.render()
111
        self.window.flip()
112
        self.fpsTimer.tick()
113
        if self.fpsTimer.fpsTime >= 2000:
114
            self.fpsEstimate = self.fpsTimer.get_fps()
115
            print("{:.2f} fps".format(self.fpsEstimate))
116
117
    def run(self):
118
        ''' Called from launcher doesnt exit until the game is quit '''
119
        self.running = True
120
        while self.running:
121
            self.do_run()
122
        self.exit()
123