-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
120 lines (100 loc) · 3.61 KB
/
model.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python2
from basecomponent import Component
from numpy import array
from glhelpers import *
import sdl2.sdlttf as sdlttf
from sdl2.pixels import *
from sdl2.surface import *
from eventmanager import *
MODEL_BOX = 0
MODEL_TEXT = 1
MODEL_CIRCLE = 2
class Model(Component):
def __init__(self, position):
Component.__init__(self)
self.position = position
self.compName = "model"
def getModelToWorldMat():
mat = identityMatrix()
mat[0,3] = self.position.x
mat[1,3] = self.position.y
return mat
class BoxModel(Model):
def __init__(self, position, width=10, height=10):
super(BoxModel, self).__init__(position)
self.type = MODEL_BOX
self.width, self.height = width, height
def getModelToWorldMat(self):
trans = identityMatrix()
scale = identityMatrix()
scale[0,0] = self.width
scale[1,1] = self.height
trans[0,3] = self.position.x
trans[1,3] = self.position.y
rot = rotationMatrix(self.position.angle)
return trans.dot(rot.dot(scale))
class CircleModel(Model):
def __init__(self, position, r):
super(CircleModel, self).__init__(position)
self.type = MODEL_CIRCLE
self.r = r
def getModelToWorldMat(self):
transScal = identityMatrix()
transScal[0,0] = self.r
transScal[1,1] = self.r
transScal[0,3] = self.position.x
transScal[1,3] = self.position.y
rot = rotationMatrix(self.position.angle)
return transScal.dot(rot)
class TextModel(Model):
def __init__(self, evtMngr, position, fontSize=32, fn=None):
Model.__init__(self, position)
self.type = MODEL_TEXT
self.colour = SDL_Colour(255, 255, 255, 255)
self.fontFilename = "fonts/DejaVuSerif.ttf"
self.text = " "
self.fn = fn
evtMngr.attachHandler(E_ON_DRAW, self.onDraw)
if not sdlttf.TTF_WasInit():
sdlttf.TTF_Init()
self.ttfFont = sdlttf.TTF_OpenFont(self.fontFilename, fontSize)
try: self.ttfFont.contents
except: raise Exception(sdlttf.TTF_GetError())
self.surfacep = self.getFontSurface()
def getModelToWorldMat(self):
mat = identityMatrix()
mat[0,0] = self.surfacep.contents.w
mat[1,1] = self.surfacep.contents.h
mat[0,3] = self.position.x
mat[1,3] = self.position.y
return mat
def getFontSurface(self):
try: SDL_FreeSurface(self.surfacep)
except AttributeError: pass
surfacep = sdlttf.TTF_RenderText_Blended(self.ttfFont,
self.text, self.colour)
try: surfacep.contents
except: raise Exception(sdlttf.TTF_GetError())
return surfacep
def onDraw(self):
try: self.text = self.fn(None)
except: self.text = " "
self.surfacep = self.getFontSurface()
class FPSModel(TextModel):
def __init__(self, evtMngr, position, counter, fontSize=32):
self.counter = counter
self.currentFPS = 0
TextModel.__init__(self, evtMngr, position, fontSize)
def onDraw(self):
self.counter.updateCounter()
if(self.currentFPS != self.counter.fps):
self.currentFPS = self.counter.fps
self.surfacep = self.getFontSurface()
def getFontSurface(self):
try: SDL_FreeSurface(self.surfacep)
except AttributeError: pass
surfacep = sdlttf.TTF_RenderText_Blended(self.ttfFont,
str(self.currentFPS), self.colour)
try: surfacep.contents
except: raise Exception(sdlttf.TTF_GetError())
return surfacep