SingleRectTest   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 5
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A updateTest() 0 3 1
1
#!/usr/bin/env python
2
"""
3
    Stress test designed to test tdl under harsh conditions
4
5
    Note that one of the slower parts is converting colors over ctypes so you
6
    can get a bit of speed avoiding the fgcolor and bgcolor parameters.
7
    Another thing slowing things down are mass calls to ctypes functions.
8
    Eventually these will be optimized to work better.
9
"""
10
import sys
11
12
import itertools
13
import random
14
import time
15
16
sys.path.insert(0, '../')
17
import tdl
18
19
class StopWatch:
20
    "Simple tool used to count time within a block using the with statement"
21
    MAXSNAPSHOTS = 10
22
23
    def __init__(self):
24
        self.enterTime = None
25
        self.snapshots = []
26
27
    def __enter__(self):
28
        self.enterTime = time.clock()
29
30
    def __exit__(self, *exc):
31
        self.snapshots.append(time.clock() - self.enterTime)
32
        if len(self.snapshots) > self.MAXSNAPSHOTS:
33
            self.snapshots.pop(0)
34
35
    def getMeanTime(self):
36
        if not self.snapshots:
37
            return 0
38
        return sum(self.snapshots) / len(self.snapshots)
39
40
41
class TestApp(tdl.event.App):
42
43
    def __init__(self, console):
44
        self.console = console
45
        self.writer = self.console
46
        self.console.setMode('scroll')
47
        self.width, self.height = self.console.getSize()
48
        self.total = self.width * self.height
49
        self.cells = list(itertools.product(range(self.width), range(self.height)))
50
        self.tick = 0
51
        self.init()
52
53
    def init(self):
54
        pass
55
56
    def ev_MOUSEDOWN(self, event):
57
        self.suspend()
58
59
    def update(self, deltaTime):
60
        self.updateTest(deltaTime)
61
        self.tick += 1
62
        #if self.tick % 50 == 0:
63
        tdl.setTitle('%s: %i FPS' % (self.__class__.__name__, tdl.getFPS()))
64
        tdl.flush()
65
66
class FullDrawCharTest(TestApp):
67
68
    def updateTest(self, deltaTime):
69
        # getrandbits is around 5x faster than using randint
70
        bgcolors = [(random.getrandbits(7), random.getrandbits(7), random.getrandbits(7)) for _ in range(self.total)]
71
        char = [random.getrandbits(8) for _ in range(self.total)]
72
        fgcolor = (255, 255, 255)
73
        for (x,y), bgcolor, char in zip(self.cells, bgcolors, char):
74
            self.console.draw_char(x, y, char, fgcolor, bgcolor)
75
76
class PreCompiledColorTest(TestApp):
77
    """
78
        This test was to see if the cast to the ctypes Color object was a big
79
        impact on performance, turns out there's no hit with this class.
80
    """
81
82
    def init(self):
83
        self.bgcolors = [tdl._Color(random.getrandbits(7),
84
                                    random.getrandbits(7),
85
                                    random.getrandbits(7))
86
                         for _ in range(256)]
87
        self.fgcolor = tdl._Color(255, 255, 255)
88
89
    def updateTest(self, deltaTime):
90
        # getrandbits is around 5x faster than using randint
91
        char = [random.getrandbits(8) for _ in range(self.total)]
92
        for (x,y), char in zip(self.cells, char):
93
            self.console.draw_char(x, y, char,
94
                                   self.fgcolor, random.choice(self.bgcolors))
95
96
97
class CharOnlyTest(TestApp):
98
99
    def updateTest(self, deltaTime):
100
        self.console.clear((255, 255, 255), (0, 0, 0))
101
        char = [random.getrandbits(8) for _ in range(self.total)]
102
        for (x,y), char in zip(self.cells, char):
103
            self.console.draw_char(x, y, char)
104
105
class TypewriterCharOnlyTest(TestApp):
106
107
    def updateTest(self, deltaTime):
108
        self.writer.move(0, 0)
109
        char = [random.getrandbits(8) for _ in range(self.total)]
110
        for (x,y), char in zip(self.cells, char):
111
            self.writer.move(x, y)
112
            self.writer.print_str(chr(char))
113
114
class ColorOnlyTest(TestApp):
115
116
    def updateTest(self, deltaTime):
117
        # getrandbits is around 5x faster than using randint
118
        bgcolors = [(random.getrandbits(6), random.getrandbits(6), random.getrandbits(6)) for _ in range(self.total)]
119
        for (x,y), bgcolor in zip(self.cells, bgcolors):
120
            self.console.draw_char(x, y, None, None, bgcolor)
121
122
class GetCharTest(TestApp):
123
124
    def init(self):
125
        for (x,y) in self.cells:
126
            bgcolor = (random.getrandbits(6), random.getrandbits(6), random.getrandbits(6))
127
            ch = random.getrandbits(8)
128
            self.console.get_char(x, y, ch, bgcolor=bgcolor)
129
130
    def updateTest(self, deltaTime):
131
        for (x,y) in self.cells:
132
            self.console.draw_char(x, y, *self.console.get_char(x, y))
133
134
class SingleRectTest(TestApp):
135
136
    def updateTest(self, deltaTime):
137
        bgcolor = (random.getrandbits(6), random.getrandbits(6), random.getrandbits(6))
138
        self.console.draw_rect(0, 0, None, None, ' ', (255, 255, 255), bgcolor)
139
140
class DrawStrTest(TestApp):
141
142
    def updateTest(self, deltaTime):
143
        for y in range(self.height):
144
            bgcolor = (random.getrandbits(6), random.getrandbits(6), random.getrandbits(6))
145
            string = [random.getrandbits(8) for x in range(self.width)]
146
            self.console.draw_str(0, y, string, (255, 255, 255), bgcolor)
147
148
class BlitScrollTest(TestApp):
149
    def updateTest(self, deltaTime):
150
        self.console.scroll(0, 1)
151
        for x in range(self.width):
152
            bgcolor = (random.getrandbits(6), random.getrandbits(6), random.getrandbits(6))
153
            ch = random.getrandbits(8)
154
            self.console.draw_char(x, 0, ch, bg=bgcolor)
155
156
# match libtcod sample screen
157
WIDTH = 46
158
HEIGHT = 20
159
def main():
160
    console = tdl.init(46, 20, renderer='OPENGL')
161
    for Test in [FullDrawCharTest, CharOnlyTest, TypewriterCharOnlyTest, ColorOnlyTest, GetCharTest,
162
                 SingleRectTest, DrawStrTest, BlitScrollTest]:
163
        Test(console).run()
164
        console.clear()
165
166
if __name__ == '__main__':
167
    main()
168