Skip to content

Commit 904c4b3

Browse files
author
4b796c65
committed
1 parent d14cb44 commit 904c4b3

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

tdl/__init__.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ def __init__(self, width, height):
453453
self._as_parameter_ = _lib.TCOD_console_new(width, height)
454454
self.width = width
455455
self.height = height
456-
self._typewriter = None # "typewriter" lock
456+
self._typewriter = None # "typewriter" lock, makes sure the colors are set to the typewriter
457457
#self.clear()
458458

459459
@classmethod
@@ -463,6 +463,7 @@ def _newConsole(cls, console):
463463
self._as_parameter_ = console
464464
self.width = _lib.TCOD_console_get_width(self)
465465
self.height = _lib.TCOD_console_get_height(self)
466+
self._typewriter = None
466467
#self.clear()
467468
return self
468469

@@ -660,8 +661,9 @@ def __init__(self, console):
660661
self.console = self.parent.console
661662
self.cursor = (0, 0) # cursor position
662663
self.scrollMode = 'scroll'
663-
self.fgcolor = (255, 255, 255)
664-
self.bgcolor = (0, 0, 0)
664+
self.fgcolor = _formatColor((255, 255, 255))
665+
self.bgcolor = _formatColor((0, 0, 0))
666+
self._bgblend = 1 # SET
665667

666668
def _normalize(self, x, y):
667669
"""return the normalized the cursor position."""
@@ -695,11 +697,13 @@ def move(self, x, y):
695697
self.cursor = (x, y)
696698

697699
def setFG(self, color):
700+
assert _iscolor(color)
698701
self.fgcolor = _formatColor(color)
699702
if self.console._typewriter is self:
700703
_lib.TCOD_console_set_default_foreground(self.console, self.fgcolor)
701704

702705
def setBG(self, color):
706+
assert _iscolor(color)
703707
self.bgcolor = _formatColor(color)
704708
if self.console._typewriter is self:
705709
_lib.TCOD_console_set_default_background(self.console, self.bgcolor)
@@ -714,8 +718,11 @@ def _updateConsole(self):
714718

715719
def addChar(self, char):
716720
x, y = self._normalize(*self.cursor)
717-
self.parent.drawChar(x, y, char, self.fgcolor, self.bgcolor)
718-
self.cursor = (x + 1, y)
721+
self.cursor = [x + 1, y] # advance cursor on next draw
722+
self._updateConsole()
723+
x, y = self.parent._translate(x, y)
724+
_lib.TCOD_console_put_char(self.console._as_parameter_, x, y, _formatChar(char), self._bgblend)
725+
719726

720727
def addStr(self, string, lineBreak='\n'):
721728
x, y = self.cursor

testing/stressTest.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class TestApp(tdl.event.App):
4242

4343
def __init__(self, console):
4444
self.console = console
45+
self.writer = tdl.Typewriter(console)
4546
self.width, self.height = self.console.getSize()
4647
self.total = self.width * self.height
4748
self.cells = list(itertools.product(range(self.width), range(self.height)))
@@ -70,6 +71,15 @@ def updateTest(self, deltaTime):
7071
for (x,y), char in zip(self.cells, char):
7172
self.console.drawChar(x, y, char, None, None)
7273

74+
class TypewriterCharOnlyTest(TestApp):
75+
76+
def updateTest(self, deltaTime):
77+
self.writer.move(0, 0)
78+
char = [random.getrandbits(8) for _ in range(self.total)]
79+
for (x,y), char in zip(self.cells, char):
80+
self.writer.move(x, y)
81+
self.writer.addChar(char)
82+
7383
class ColorOnlyTest(TestApp):
7484

7585
def updateTest(self, deltaTime):
@@ -109,7 +119,7 @@ def updateTest(self, deltaTime):
109119

110120
def main():
111121
console = tdl.init(60, 40)
112-
for Test in [FullDrawCharTest, CharOnlyTest, ColorOnlyTest, GetCharTest,
122+
for Test in [FullDrawCharTest, CharOnlyTest, TypewriterCharOnlyTest, ColorOnlyTest, GetCharTest,
113123
SingleRectTest, DrawStrTest, BlitScrollTest]:
114124
Test(console).run()
115125
console.clear()

0 commit comments

Comments
 (0)