Skip to content

Commit 56fc525

Browse files
committed
set_colors method now changes draw* colors
draw* methods now default to the set_colors parameters by using Python's Ellipsis type, this can break old code
1 parent 2062ed6 commit 56fc525

File tree

5 files changed

+131
-84
lines changed

5 files changed

+131
-84
lines changed

dev/runRegressionTest.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def setUpClass(cls):
3232
def setUp(self):
3333
tdl.setFont('../fonts/libtcod/terminal8x8_gs_ro.png')
3434
tdl.event.get()
35+
self.console.set_colors((0,0,0), (0,0,0))
3536
self.console.clear()
3637

3738
@classmethod
@@ -219,7 +220,7 @@ def test_drawStrArray(self):
219220

220221
def test_drawRect(self):
221222
consoleCopy = tdl.Console(*(self.console.getSize()))
222-
for x,y in random.sample(list(self.getDrawables()), 100):
223+
for x,y in random.sample(list(self.getDrawables()), 20):
223224
consoleCopy.blit(self.console) # copy the console to compare untouched areas
224225
ch, fg, bg = self.getRandomCharacter()
225226
width, height = self.console.getSize()
@@ -234,7 +235,7 @@ def test_drawRect(self):
234235

235236
def test_drawFrame(self):
236237
consoleCopy = tdl.Console(*(self.console.getSize()))
237-
for x,y in random.sample(list(self.getDrawables()), 100):
238+
for x,y in random.sample(list(self.getDrawables()), 20):
238239
consoleCopy.blit(self.console) # copy the console to compare untouched areas
239240
ch, fg, bg = self.getRandomCharacter()
240241
width, height = self.console.getSize()

dev/stressTest.py

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
Eventually these will be optimized to work better.
99
"""
1010
import sys
11+
import os
12+
13+
import cProfile as profile
14+
import pstats
1115

1216
import itertools
1317
import random
@@ -47,6 +51,10 @@ def __init__(self, console):
4751
self.width, self.height = self.console.getSize()
4852
self.total = self.width * self.height
4953
self.cells = list(itertools.product(range(self.width), range(self.height)))
54+
self.init()
55+
56+
def init(self):
57+
pass
5058

5159
def ev_MOUSEDOWN(self, event):
5260
self.suspend()
@@ -64,16 +72,36 @@ def updateTest(self, deltaTime):
6472
char = [random.getrandbits(8) for _ in range(self.total)]
6573
fgcolor = (255, 255, 255)
6674
for (x,y), bgcolor, char in zip(self.cells, bgcolors, char):
67-
self.console.drawChar(x, y, char, fgcolor, bgcolor)
68-
75+
self.console.draw_char(x, y, char, fgcolor, bgcolor)
76+
77+
class PreCompiledColorTest(TestApp):
78+
"""
79+
This test was to see if the cast to the ctypes Color object was a big
80+
impact on performance, turns out there's no hit with this class.
81+
"""
82+
83+
def init(self):
84+
self.bgcolors = [tdl._Color(random.getrandbits(7),
85+
random.getrandbits(7),
86+
random.getrandbits(7))
87+
for _ in range(256)]
88+
self.fgcolor = tdl._Color(255, 255, 255)
89+
90+
def updateTest(self, deltaTime):
91+
# getrandbits is around 5x faster than using randint
92+
char = [random.getrandbits(8) for _ in range(self.total)]
93+
for (x,y), char in zip(self.cells, char):
94+
self.console.draw_char(x, y, char,
95+
self.fgcolor, random.choice(self.bgcolors))
96+
6997

7098
class CharOnlyTest(TestApp):
7199

72100
def updateTest(self, deltaTime):
73101
self.console.clear((255, 255, 255), (0, 0, 0))
74102
char = [random.getrandbits(8) for _ in range(self.total)]
75103
for (x,y), char in zip(self.cells, char):
76-
self.console.drawChar(x, y, char, None, None)
104+
self.console.draw_char(x, y, char)
77105

78106
class TypewriterCharOnlyTest(TestApp):
79107

@@ -82,54 +110,64 @@ def updateTest(self, deltaTime):
82110
char = [random.getrandbits(8) for _ in range(self.total)]
83111
for (x,y), char in zip(self.cells, char):
84112
self.writer.move(x, y)
85-
self.writer.printStr(chr(char))
113+
self.writer.print_str(chr(char))
86114

87115
class ColorOnlyTest(TestApp):
88116

89117
def updateTest(self, deltaTime):
90118
# getrandbits is around 5x faster than using randint
91119
bgcolors = [(random.getrandbits(6), random.getrandbits(6), random.getrandbits(6)) for _ in range(self.total)]
92120
for (x,y), bgcolor in zip(self.cells, bgcolors):
93-
self.console.drawChar(x, y, None, None, bgcolor)
121+
self.console.draw_char(x, y, None, None, bgcolor)
94122

95123
class GetCharTest(TestApp):
96124

97125
def updateTest(self, deltaTime):
98126
for (x,y) in self.cells:
99-
self.console.getChar(x, y)
127+
self.console.get_char(x, y)
100128

101129
class SingleRectTest(TestApp):
102130

103131
def updateTest(self, deltaTime):
104132
bgcolor = (random.getrandbits(6), random.getrandbits(6), random.getrandbits(6))
105-
self.console.drawRect(0, 0, None, None, ' ', (255, 255, 255), bgcolor)
133+
self.console.draw_rect(0, 0, None, None, ' ', (255, 255, 255), bgcolor)
106134

107135
class DrawStrTest(TestApp):
108136

109137
def updateTest(self, deltaTime):
110138
for y in range(self.height):
111139
bgcolor = (random.getrandbits(6), random.getrandbits(6), random.getrandbits(6))
112140
string = [random.getrandbits(8) for x in range(self.width)]
113-
self.console.drawStr(0, y, string, (255, 255, 255), bgcolor)
141+
self.console.draw_str(0, y, string, (255, 255, 255), bgcolor)
114142

115143
class BlitScrollTest(TestApp):
116144
def updateTest(self, deltaTime):
117145
self.console.scroll(0, 1)
118146
for x in range(self.width):
119147
bgcolor = (random.getrandbits(6), random.getrandbits(6), random.getrandbits(6))
120148
ch = random.getrandbits(8)
121-
self.console.drawChar(x, 0, ch, bgcolor=bgcolor)
149+
self.console.draw_char(x, 0, ch, bgcolor=bgcolor)
122150

123151
# match libtcod sample screen
124152
WIDTH = 46
125153
HEIGHT = 20
126154
def main():
127155
console = tdl.init(46, 20)
128-
for Test in [FullDrawCharTest, CharOnlyTest, TypewriterCharOnlyTest, ColorOnlyTest, GetCharTest,
156+
for Test in [FullDrawCharTest, PreCompiledColorTest, CharOnlyTest, TypewriterCharOnlyTest, ColorOnlyTest, GetCharTest,
129157
SingleRectTest, DrawStrTest, BlitScrollTest]:
130158
Test(console).run()
131159
console.clear()
132160

133161
if __name__ == '__main__':
134-
main()
162+
pr = profile.Profile()
163+
try:
164+
pr.runcall(main)
165+
finally:
166+
pr.dump_stats('profile.prof')
167+
stats = pstats.Stats('profile.prof')
168+
stats.strip_dirs()
169+
stats.sort_stats('time')
170+
stats.reverse_order()
171+
stats.print_stats()
172+
os.remove('profile.prof')
135173

0 commit comments

Comments
 (0)