Skip to content

Commit 3af43d0

Browse files
author
4b796c65
committed
added scrolling modes, attemting to access SDL
1 parent 7f5d4fe commit 3af43d0

File tree

5 files changed

+76
-35
lines changed

5 files changed

+76
-35
lines changed

CHANGELOG.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* drawStr can be configured to scroll or raise an error
2+
13
1.1.4
24
* Merged the Typewriter and MetaConsole classes,
35
You now have a virtual cursor with Console and Window objects

dev/runRegressionTest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def test_drawStrArray(self):
219219

220220
def test_drawRect(self):
221221
consoleCopy = tdl.Console(*(self.console.getSize()))
222-
for x,y in self.getDrawables():
222+
for x,y in random.sample(list(self.getDrawables()), 100):
223223
consoleCopy.blit(self.console) # copy the console to compare untouched areas
224224
ch, fg, bg = self.getRandomCharacter()
225225
width, height = self.console.getSize()
@@ -234,7 +234,7 @@ def test_drawRect(self):
234234

235235
def test_drawFrame(self):
236236
consoleCopy = tdl.Console(*(self.console.getSize()))
237-
for x,y in self.getDrawables():
237+
for x,y in random.sample(list(self.getDrawables()), 100):
238238
consoleCopy.blit(self.console) # copy the console to compare untouched areas
239239
ch, fg, bg = self.getRandomCharacter()
240240
width, height = self.console.getSize()

dev/stressTest.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class TestApp(tdl.event.App):
4242

4343
def __init__(self, console):
4444
self.console = console
45-
self.writer = tdl.Typewriter(console)
45+
self.writer = self.console
46+
self.console.setScrollMode('scroll')
4647
self.width, self.height = self.console.getSize()
4748
self.total = self.width * self.height
4849
self.cells = list(itertools.product(range(self.width), range(self.height)))
@@ -59,7 +60,7 @@ class FullDrawCharTest(TestApp):
5960

6061
def updateTest(self, deltaTime):
6162
# getrandbits is around 5x faster than using randint
62-
bgcolors = [(random.getrandbits(6), random.getrandbits(6), random.getrandbits(6)) for _ in range(self.total)]
63+
bgcolors = [(random.getrandbits(7), random.getrandbits(7), random.getrandbits(7)) for _ in range(self.total)]
6364
char = [random.getrandbits(8) for _ in range(self.total)]
6465
fgcolor = (255, 255, 255)
6566
for (x,y), bgcolor, char in zip(self.cells, bgcolors, char):
@@ -81,7 +82,7 @@ def updateTest(self, deltaTime):
8182
char = [random.getrandbits(8) for _ in range(self.total)]
8283
for (x,y), char in zip(self.cells, char):
8384
self.writer.move(x, y)
84-
self.writer.addChar(char)
85+
self.writer.printStr(chr(char))
8586

8687
class ColorOnlyTest(TestApp):
8788

tdl/__init__.py

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,12 @@ def _formatChar(char):
8585
"""
8686
if char is None:
8787
return None
88-
if isinstance(char, _INTTYPES):
89-
return char
88+
#if isinstance(char, _INTTYPES):
89+
# return char
9090
if isinstance(char, _STRTYPES) and len(char) == 1:
9191
return ord(char)
92-
raise TypeError('Expected char parameter to be a single character string, number, or None, got: %s' % repr(char))
92+
return int(char) # conversion faster than type check
93+
#raise TypeError('Expected char parameter to be a single character string, number, or None, got: %s' % repr(char))
9394

9495
_fontinitialized = False
9596
_rootinitialized = False
@@ -139,19 +140,8 @@ def _iscolor(color):
139140
# def __len__(self):
140141
# return 3
141142

142-
def _formatColor(color):
143-
"""Format the color to ctypes
144-
"""
145-
if color is None or color is False:
146-
return color
147-
if isinstance(color, _Color):
148-
return color
149-
#if isinstance(color, Color):
150-
# return color._getCType()
151-
if isinstance(color, _INTTYPES):
152-
# format a web style color with the format 0xRRGGBB
153-
return _Color(color >> 16 & 0xff, color >> 8 & 0xff, color & 0xff)
154-
return _Color(*color)
143+
# Format the color to ctypes, will preserve None and False
144+
_formatColor = _Color.new
155145

156146
def _getImageSize(filename):
157147
"""Try to get the width and height of a bmp of png image file"""
@@ -199,8 +189,11 @@ def _normalizePoint(self, x, y):
199189
Respects Pythons negative indexes. -1 starts at the bottom right.
200190
Replaces the _drawable function
201191
"""
202-
assert isinstance(x, _INTTYPES), 'x must be an integer, got %s' % repr(x)
203-
assert isinstance(y, _INTTYPES), 'y must be an integer, got %s' % repr(y)
192+
#assert isinstance(x, _INTTYPES), 'x must be an integer, got %s' % repr(x)
193+
#assert isinstance(y, _INTTYPES), 'y must be an integer, got %s' % repr(y)
194+
# force int, always faster than type checking
195+
x = int(x)
196+
y = int(y)
204197

205198
assert (-self.width <= x < self.width) and (-self.height <= y < self.height), \
206199
('(%i, %i) is an invalid postition on %s' % (x, y, self))
@@ -263,6 +256,32 @@ def _lockColors(self, forceUpdate=False):
263256
_lib.TCOD_console_set_default_foreground(self.console, self.fgcolor)
264257
#
265258

259+
def setScrollMode(self, mode):
260+
"""Configure how this console will react to the cursor writing past the
261+
end if the console.
262+
263+
This is for methods that use the virtual cursor, such as L{printStr}.
264+
265+
@type mode: string
266+
@param mode: Possible settings are:
267+
268+
- 'error' - A TDLError will be raised once the cursor
269+
reaches the end of the console. Everything up until
270+
the error will still be drawn.
271+
272+
This is the default setting.
273+
274+
- 'scroll' - The console will scroll up as stuff is
275+
written to the end.
276+
277+
You can restrict the region with L{tdl.Window} when
278+
doing this.
279+
"""
280+
MODES = ['error', 'scroll']
281+
if mode.lower() not in MODES:
282+
raise TDLError('mode must be one of %s, got %s' % (MODES, repr(mode)))
283+
self._scrollMode = mode.lower()
284+
266285
def setColors(self, fg=None, bg=None):
267286
"""Sets the colors to be used with the L{printStr} function.
268287

tdl/__tcod.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,37 @@ def _unpackfile(filename):
1818
return os.path.abspath(os.path.join(__path__[0], filename))
1919

2020
def _unpackFramework(framework, path):
21-
# get framework.tar file, remove ".tar" and add path
21+
"""get framework.tar file, remove ".tar" and add path"""
2222
return os.path.abspath(os.path.join(_unpackfile(framework)[:-4], path))
2323

2424
def _loadDLL(dll):
25-
# shorter version of file unpacking and linking
25+
"""shorter version of file unpacking and linking"""
2626
return cdll.LoadLibrary(_unpackfile(dll))
2727

2828

2929
def _get_library_crossplatform():
3030
bits, linkage = platform.architecture()
3131
libpath = None
3232
if 'win32' in sys.platform:
33-
_loadDLL('lib/win32/SDL.dll')
33+
libSDL = _loadDLL('lib/win32/SDL.dll')
3434
_loadDLL('lib/win32/zlib1.dll')
35-
return _loadDLL('lib/win32/libtcod-VS.dll')
36-
#return _loadDLL('lib/win32/libtcod-mingw.dll')
35+
libTCOD = _loadDLL('lib/win32/libtcod-VS.dll')
3736
elif 'linux' in sys.platform:
37+
libSDL = None
38+
#libSDL = cdll.LoadLibrary('SDL.so')
3839
if bits == '32bit':
39-
return _loadDLL('lib/linux32/libtcod.so')
40+
libTCOD = _loadDLL('lib/linux32/libtcod.so')
4041
elif bits == '64bit':
41-
return _loadDLL('lib/linux64/libtcod.so')
42+
libTCOD = _loadDLL('lib/linux64/libtcod.so')
4243
elif 'darwin' in sys.platform:
43-
#sys.path.insert(0, _unpackfile('lib/darwin/Frameworks/'))
44-
#_loadDLL('lib/darwin/libpng14')
45-
return _loadDLL('lib/darwin/libtcod.dylib')
44+
libSDL = _loadDLL('lib/darwin/SDL.dylib')
45+
libTCOD = _loadDLL('lib/darwin/libtcod.dylib')
4646

4747
else:
4848
raise ImportError('Operating system "%s" has no supported dynamic link libarary. (%s, %s)' % (sys.platform, bits, linkage))
49+
return libTCOD, libSDL
4950

50-
_lib = _get_library_crossplatform()
51+
_lib, _libSDL = _get_library_crossplatform()
5152

5253
try:
5354
c_bool
@@ -58,7 +59,25 @@ def _get_library_crossplatform():
5859

5960
class _Color(Structure):
6061
_fields_ = [('r', c_uint8), ('g', c_uint8), ('b', c_uint8)]
61-
62+
63+
@staticmethod
64+
def new(color):
65+
if color is None or color is False:
66+
return color
67+
if isinstance(color, _Color):
68+
return color
69+
if isinstance(color, int):
70+
# format a web style color with the format 0xRRGGBB
71+
return _Color(color >> 16 & 0xff, color >> 8 & 0xff, color & 0xff)
72+
return _Color(*color)
73+
74+
def set(self, r, g, b):
75+
"""perform a slightly faster in-place assignment of this object"""
76+
self.r = r
77+
self.g = g
78+
self.b = b
79+
return self
80+
6281
def __iter__(self):
6382
'to make this class more tuple-like'
6483
return iter((self.r, self.g, self.b))

0 commit comments

Comments
 (0)