-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphics.py
More file actions
207 lines (167 loc) · 6.5 KB
/
graphics.py
File metadata and controls
207 lines (167 loc) · 6.5 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import pygame
from pygame.locals import *
import termModel
class GlyphManager(object):
VT220_GLYPH_FILENAME = "vt220-rom.png"
def __init__(self):
pass
def loadSprite(self, spriteFile, glyphHeight, glyphWidth):
self.spriteImg = pygame.image.load(spriteFile)
self.glyphWidth = glyphWidth
self.glyphHeight = glyphHeight
self.maxCol = self.spriteImg.get_width() / glyphWidth
self.maxRow = self.spriteImg.get_height() / glyphHeight
def getGlyph(self, charSet, char):
if type(char) is str:
char = ord(char)
if char == 32:
r, c = 0, 0
else:
r = char % self.maxRow
c = char / 16
return self.spriteImg.subsurface(
pygame.Rect(c * self.glyphWidth, r * self.glyphHeight,
self.glyphWidth, self.glyphHeight))
class GfxCharFactory(object):
def __init__(self, glyphManager, gfxTerm, charGroup):
self.glyphManager = glyphManager
self.gfxTerm = gfxTerm
self.charGroup = charGroup
@property
def charHeight(self):
return self.gfxTerm.height / self.gfxTerm.termModel.r
@property
def charWidth(self):
return self.gfxTerm.width / self.gfxTerm.termModel.c
def getChar(self, r, c, char, attrs):
rect = Rect(c * self.charWidth, r * self.charHeight,
self.charWidth, self.charHeight)
return GfxChar(char, self.glyphManager.getGlyph(0, char),
attrs, self.charGroup, rect)
class GfxChar(termModel.Char, pygame.sprite.DirtySprite):
"A graphical sprit that represents a character."
def __init__(self, char, glyph, attrs, spriteGroup, rect):
super(GfxChar, self).__init__(char, attrs)
super(pygame.sprite.DirtySprite, self).__init__(spriteGroup)
self.attrs = attrs
self.rect = rect
self.image = pygame.Surface((self.rect.width, self.rect.height), pygame.HWSURFACE, 8)
self.image.fill((255,255,255))
self.image.blit(glyph, (0, 0))
self.image.set_palette([self.attrs['fgColor'], self.attrs['bgColor']])
self.dirty = 1
self.add(spriteGroup)
def setCoord(self, r, c):
self.rect.top, self.rect.left = r * self.rect.height, c * self.rect.width
class GfxTerm(object):
def getSpriteGroup(self):
return self.group
def loop(self):
# Event loop
self.running = True
while self.running:
self.sink()
def sink(self):
self.clock.tick(60)
for event in pygame.event.get():
if not self._processEvent(event):
return
#d = self.termModel.getAndClearDirtySet()
#if d:
#self.updateDisplay([self._coordToRect(c) for c in d])
self.group.update()
#pygame.display.flip()
pygame.display.update(self.group.draw(self.screen))
def _processEvent(self, event):
if event.type == QUIT:
return False
elif event.type == KEYDOWN:
if event.key == K_q:
#Quit when you press Q
return False
if event.key == K_r:
#Restart the engine
return True
if event.key == K_z:
for i in xrange(256):
self.termModel.printString(chr(i))
if event.key == K_x:
self.termModel.printString("".join([chr(i) for i in xrange(256)]))
if event.key == K_h:
self.termModel.printString("Hello World")
if event.key == K_r:
self.termModel.setRendition(32, 40)
if event.key == K_c:
self.termModel.eraseInScreen(2)
if event.key == K_g:
print self.group
self.group.update()
self.group.draw(self.screen)
pygame.display.update()
#self.termModel.setRendition(46, 34)
if event.key == K_d:
for r in self.termModel.termBuf:
print "".join(map(str,r))
print len(self.termModel.termBuf), len(self.termModel.termBuf[0])
return True
# if event.key == K_UP:
# self.player.moveup()
# if event.key == K_DOWN:
# self.player.movedown()
# if event.key == K_RIGHT:
# self.player.moveright()
# if event.key == K_LEFT:
# self.player.moveleft()
# return True
# def updateDisplay(self, dirtySet = None):
# self.screen.fill((255,255,255))
# for r in xrange(self.r):
# for c in xrange(self.c):
# char = self.termModel.getChar(r,c)
# if char:
# self.screen.blit(self._getGlyph(char.char), (c * self.glyphWidth, r * self.glyphHeight))
# pygame.display.update()
def _coordToRect(self, coord):
r, c = coord
return Rect(c * self.glyphWidth, r * self.glyphHeight, self.glyphWidth, self.glyphHeight)
def _getGlyph(self, char):
try:
return self._glyphCache[char]
except KeyError:
self._glyphCache[char] = self.glyphManager.getGlyph(0, char)
return self._glyphCache[char]
def __init__(self):
pygame.init()
pygame.display.init()
self.clock = pygame.time.Clock()
self.group = pygame.sprite.RenderUpdates()
# def __init__(self):
# self.height = height
# self.width = width
# self.running = True
def setTermModel(self, termModel):
# self._glyphCache = {}
self.termModel = termModel
self.r, self.c = termModel.r ,termModel.c
self.width = self.c * 8
self.height = self.r * 10
self.glyphHeight = 10
self.glyphWidth = 8
self.screen = pygame.display.set_mode((self.width, self.height+10))
self.screen.fill((255,255,255))
self.area = pygame.rect.Rect(0, 0, self.width, self.height)
pygame.display.set_caption("TERM")
if __name__ == "__main__":
import termModel
#def __init__(self, glyphManager, gfxTerm, charGroup):
glyphManager = GlyphManager()
glyphManager.loadSprite(GlyphManager.VT220_GLYPH_FILENAME, 10, 8)
g = GfxTerm()
group = g.getSpriteGroup()
gfxCharFactory = GfxCharFactory(glyphManager, g, group)
t = termModel.Terminal(gfxCharFactory, 25, 80)
g.setTermModel(t)
t.setDisplay(g)
print 'z'
g.loop()
print 'z'