Completed
Push — master ( e887f8...0c9b68 )
by
unknown
01:02
created

framework.game.GameManager.__init__()   B

Complexity

Conditions 2

Size

Total Lines 86

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 86
rs 8.6584

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 gem import vector
11
from ed2d import texture
12
from ed2d import mesh
13
from ed2d.physics import rectangle
14
from ed2d.physics import cmodel
15
from ed2d.physics import physobj
16
from ed2d.physics import physengine
17
18
class GameManager(object):
19
    ''' Entry point into the game, and manages the game in general '''
20
    def __init__(self):
21
22
        self.width = 800
23
        self.height = 600
24
        self.title = "Edge Engine - Texture Filtering Demo"
25
        self.running = False
26
27
        self.fpsTimer = timing.FpsCounter()
28
        self.fpsEstimate = 0
29
30
        self.events = events.Events()
31
        self.window = window.Window(self.title, self.width, self.height, window.WindowedMode)
32
        self.context = context.Context(3, 3, 2)
33
        self.context.window = self.window
34
35
        self.events.add_listener(self.process_event)
36
37
        self.keys = []
38
39
        gl.init()
40
        major = pgl.glGetInteger(gl.GL_MAJOR_VERSION)
41
        minor = pgl.glGetInteger(gl.GL_MINOR_VERSION)
42
        print ('OpenGL Version: {}.{}'.format(major, minor))
43
44
        gl.glViewport(0, 0, self.width, self.height)
45
46
        # Shaders setup
47
        vsPath = files.resolve_path('data', 'shaders', 'main.vs')
48
        fsPath = files.resolve_path('data', 'shaders', 'texturefiltering.fs')
49
50
        vertex = shaders.VertexShader(vsPath)
51
        fragment = shaders.FragmentShader(fsPath)
52
        self.program = shaders.ShaderProgram(vertex, fragment)
53
        self.program.use()
54
        self.orthoID = self.program.new_uniform(b'ortho')
55
        self.viewPortID = self.program.new_uniform(b'viewPortResolution')
56
57
        self.vao = pgl.glGenVertexArrays(1)
58
59
        # Load character image into new opengl texture
60
        imagePath = files.resolve_path('data', 'images', 'cubix.png')
61
        self.texAtlas = texture.Texture(imagePath, self.program)
62
63
        ### Physics Scene ###
64
65
        # Create a physics engine
66
        self.physicsEngineTest = physengine.PhysEngine()
67
68
        ### Player physical model ###
69
70
        # Create a rectangle the long way, this will be the player
71
        self.cModelTestRect = rectangle.Rectangle(0.0, 0.0, width=800, height=600)
72
        self.cModelTestRect.update()
73
74
        # Creating a object steps:
75
        # Create a collision model object
76
        # Create a physics object to simulate
77
        # Create a mesh object to render
78
        self.cModelTest = cmodel.cModel(self.cModelTestRect)
79
        self.physicsObjectTest = physobj.PhysObj(self.cModelTest, vector.Vector(3, data=[0.0, 0.0, 1.0]))
80
        self.physicsEngineTest.addObject(self.physicsObjectTest)
81
        self.meshObjectTest = mesh.Mesh()
82
83
        self.meshObjectTest.fromData(data=[
84
             [0.0, 1.0, 0.0],
85
             [1.0, 1.0, 0.0],
86
             [0.0, 0.0, 0.0],
87
             [1.0, 0.0, 0.0]])
88
89
        self.meshObjectTest.setBuffers()
90
        self.meshObjectTest.addProgram(self.program)
91
        self.meshObjectTest.addTexture(self.texAtlas)
92
        self.meshObjectTest.addPhysicsObject(self.physicsObjectTest)
93
94
        ### End Player ###
95
96
        # Setup projection
97
        self.ortho = matrix.orthographic(0.0, self.width, self.height, 0.0, -1.0, 1.0)
98
99
        # Update the uniforms in the shaders
100
        self.program.set_uniform_matrix(self.orthoID, self.ortho)
101
        self.program.set_uniform_array(self.viewPortID, [float(self.width), float(self.height)])
102
103
        glerr = gl.glGetError()
104
        if glerr != 0:
105
            print ('GLError:', glerr)
106
107
    def resize(self, width, height):
108
        self.width = width
109
        self.height = height
110
        gl.glViewport(0, 0, self.width, self.height)
111
        self.ortho = matrix.orthographic(0.0, self.width, self.height, 0.0, -1.0, 1.0)
112
        self.program.set_uniform_matrix(self.orthoID, self.ortho)
113
114
    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...
115
        if event == 'quit' or event == 'window_close':
116
            self.running = False
117
        elif event == 'window_resized':
118
            winID, x, y = data
119
            self.resize(x, y)
120
        elif event == 'mouse_move':
121
            x, y = data
122
            # Translate and then update it, this can be handled better but for now, this will do
123
            #self.physicsObjectTest.translate(x,y)
124
            #self.meshObjectTest.update(self.physicsObjectTest)
125
        elif event == 'key_down':
126
            self.keys.append(data[0])
127
            print (self.keys)
128
        elif event == 'key_up':
129
            self.keys.remove(data[0])
130
131
    def update(self):
132
        pass
133
        #Disabled because it can get really annoying, really fast >:[
134
        #self.physicsEngineTest.simulate(self.fpsTimer.tick())
135
136
    def render(self):
137
        gl.glClearColor(0.5, 0.5, 0.5, 1.0)
138
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
139
        gl.glEnable(gl.GL_BLEND)
140
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
141
        gl.glBindVertexArray(self.vao)
142
143
        self.meshObjectTest.render()
144
145
        gl.glBindVertexArray(0)
146
147
    def do_run(self):
148
        ''' Process a single loop '''
149
        self.events.process()
150
        self.update()
151
        self.render()
152
        self.window.flip()
153
        self.fpsTimer.tick()
154
        if self.fpsTimer.fpsTime >= 2000:
155
            self.fpsEstimate = self.fpsTimer.get_fps()
156
            print ("{:.2f} fps".format(self.fpsEstimate))
157
158
    def run(self):
159
        ''' Called from launcher doesnt exit until the game is quit '''
160
        self.running = True
161
        while self.running:
162
            self.do_run()
163