Skip to content

Commit 16ba5b2

Browse files
4B796C65@gmail.com4B796C65@gmail.com
authored andcommitted
Rearranged drawing parameters to make code look better.
1 parent 2b61ef3 commit 16ba5b2

File tree

5 files changed

+36
-37
lines changed

5 files changed

+36
-37
lines changed

examples/eventget.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@ def print_line_append(string):
1313
# move everything on the console up a line with blit
1414
console.blit(console, 0, 0, 80, 57, 0, 1)
1515
# clear the bottom line with drawRect
16-
console.drawRect(' ', 0, 57, None, 1)
16+
console.drawRect(0, 57, None, 1, ' ')
1717
# print the string
18-
console.drawStr(string, 0, 57)
18+
console.drawStr(0, 57, string)
1919

2020
tdl.setFPS(24) # slow down the program so that the user can clearly see the motion events
2121

2222
while 1:
2323
for event in tdl.event.get():
2424
if event.type == tdl.QUIT:
2525
raise SystemExit()
26-
elif event.type == tdl.KEYDOWN:
26+
elif event.type == 'KEYDOWN':
2727
print_line_append('KEYDOWN event - key=%.2i char=%s keyname=%s alt=%i ctrl=%i shift=%i' % (event.key, repr(event.char), repr(event.keyname), event.alt, event.ctrl, event.shift))
28-
elif event.type == tdl.KEYUP:
28+
elif event.type == 'KEYUP':
2929
print_line_append('KEYUP event - key=%.2i char=%s keyname=%s alt=%i ctrl=%i shift=%i' % (event.key, repr(event.char), repr(event.keyname), event.alt, event.ctrl, event.shift))
30-
elif event.type == tdl.MOUSEMOTION:
31-
console.drawRect(' ', 0, 59)
32-
console.drawStr('MOUSEMOTION event - pos=%i,%i cell=%i,%i motion=%i,%i cellmotion=%i,%i' % (event.pos + event.cell + event.motion + event.cellmotion), 0, 59)
33-
elif event.type == tdl.MOUSEDOWN:
30+
elif event.type == 'MOUSEMOTION':
31+
console.drawRect(0, 59, None, None, ' ')
32+
console.drawStr(0, 59, 'MOUSEMOTION event - pos=%i,%i cell=%i,%i motion=%i,%i cellmotion=%i,%i' % (event.pos + event.cell + event.motion + event.cellmotion))
33+
elif event.type == 'MOUSEDOWN':
3434
print_line_append('MOUSEDOWN event - pos=%i,%i cell=%i,%i button=%i' % (event.pos + event.cell + (event.button,)))
35-
elif event.type == tdl.MOUSEUP:
35+
elif event.type == 'MOUSEUP':
3636
print_line_append('MOUSEUP event - pos=%i,%i cell=%i,%i button=%i' % (event.pos + event.cell + (event.button,)))
3737
tdl.flush()

examples/hello_world.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
# draw the string "Hello World" at the top left corner using the default colors:
1818
# a white forground on a black background.
19-
console.drawStr('Hello World', 0,0)
19+
console.drawStr(0, 0, 'Hello World')
2020

2121
# display the changes to the console with flush.
2222
# if you forget this part the screen will stay black emptiness forever.

examples/stress_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ def main():
6161
char = [random.getrandbits(8) for _ in range(TOTAL)]
6262
with drawTimer:
6363
for (x,y), bgcolor, char in zip(CELLS, bgcolors, char):
64-
console.drawChar(char=char, x=x, y=y, fgcolor=(255, 255, 255), bgcolor=bgcolor)
65-
console.drawStr('Random%7.2fms ' % (randomTimer.getMeanTime() * 1000), 0, 0, tdl.C_WHITE, tdl.C_BLACK)
66-
console.drawStr('DrawCh%7.2fms ' % (drawTimer.getMeanTime() * 1000), 0, 1, tdl.C_WHITE, tdl.C_BLACK)
67-
console.drawStr('Flush %7.2fms ' % (flushTimer.getMeanTime() * 1000), 0, 2, tdl.C_WHITE, tdl.C_BLACK)
64+
console.drawChar(x=x, y=y, char=char, fgcolor=(255, 255, 255), bgcolor=bgcolor)
65+
console.drawStr(0, 0, 'Random%7.2fms ' % (randomTimer.getMeanTime() * 1000), tdl.C_WHITE, tdl.C_BLACK)
66+
console.drawStr(0, 1, 'DrawCh%7.2fms ' % (drawTimer.getMeanTime() * 1000), tdl.C_WHITE, tdl.C_BLACK)
67+
console.drawStr(0, 2, 'Flush %7.2fms ' % (flushTimer.getMeanTime() * 1000), tdl.C_WHITE, tdl.C_BLACK)
6868
with flushTimer:
6969
tdl.flush()
7070
tdl.setTitle('%i FPS' % tdl.getFPS())

tdl/__init__.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def _setChar(self, char, x, y, fgcolor=None, bgcolor=None, bgblend=BND_SET):
301301
if bgcolor is not None:
302302
_setback(self, x, y, _formatColor(bgcolor), bgblend)
303303

304-
def drawChar(self, char, x=None, y=None, fgcolor=None, bgcolor=None):
304+
def drawChar(self, x=None, y=None, char=None, fgcolor=C_WHITE, bgcolor=C_BLACK):
305305
"""Draws a single character.
306306
307307
char should be an integer, single character string, or None
@@ -315,19 +315,15 @@ def drawChar(self, char, x=None, y=None, fgcolor=None, bgcolor=None):
315315
"""
316316
# hardcode alpha settings for now
317317
bgblend=BND_SET
318-
#if isinstance(char, (str, bytes)):
319-
# assert len(char) == 1, 'strings must only have one character'
320-
# char = ord(char)
321-
#assert isinstance(char, int) or char is None, \
322-
# 'char must be an integer, single character string, or None'
318+
323319
char = _formatChar(char)
324320
_verify_colors(fgcolor, bgcolor)
325321
x, y = self._cursor_move(x, y)
326322

327323
self._setChar(char, x, y, _formatColor(fgcolor), _formatColor(bgcolor), bgblend)
328324
self._cursor_advance()
329325

330-
def drawStr(self, string, x=None, y=None, fgcolor=None, bgcolor=None):
326+
def drawStr(self, x=None, y=None, string='', fgcolor=C_WHITE, bgcolor=C_BLACK):
331327
"""Draws a string starting at x and y.
332328
333329
A string that goes past the end will wrap around. No warning will be
@@ -347,10 +343,10 @@ def drawStr(self, string, x=None, y=None, fgcolor=None, bgcolor=None):
347343
fgcolor, bgcolor = _formatColor(fgcolor), _formatColor(bgcolor)
348344

349345
for char in string:
350-
self._setChar(char, self.cursorX, self.cursorY, fgcolor, bgcolor, bgblend)
346+
self._setChar(x=self.cursorX, y=self.cursorY, char=char, fgcolor=fgcolor, bgcolor=bgcolor)
351347
self._cursor_advance()
352348

353-
def drawRect(self, char, x=0, y=0, width=None, height=None, fgcolor=None, bgcolor=None):
349+
def drawRect(self, x=0, y=0, width=None, height=None, char=None, fgcolor=C_WHITE, bgcolor=C_BLACK):
354350
"""Draws a rectangle starting from x and y and extending to width and
355351
height. If width or height are None then it will extend to the edge
356352
of the console. The rest are the same as drawChar.
@@ -370,7 +366,7 @@ def drawRect(self, char, x=0, y=0, width=None, height=None, fgcolor=None, bgcolo
370366
for cellX in range(x, x + width):
371367
self._setChar(char, cellX, cellY, fgcolor, bgcolor, bgblend)
372368

373-
def drawFrame(self, char, x=0, y=0, width=None, height=None, fgcolor=None, bgcolor=None):
369+
def drawFrame(self, x=0, y=0, width=None, height=None, char=None, fgcolor=C_WHITE, bgcolor=C_BLACK):
374370
"Similar to drawRect but only draws the outline of the rectangle"
375371
# hardcode alpha settings for now
376372
bgblend=BND_SET
@@ -425,8 +421,8 @@ def __init__(self, console, x=0, y=0, width=None, height=None):
425421
raise TypeError('console parameter must be a Console or Window instance')
426422
assert isinstance(x, int)
427423
assert isinstance(y, int)
428-
assert isinstance(width, int) or width is None
429-
assert isinstance(height, int) or height is None
424+
assert (isinstance(width, int) or width is None)
425+
assert (isinstance(height, int) or height is None)
430426
if not console._rectInBounds(x, y, width, height):
431427
raise TDLError('New Window is not within bounds of it\'s parent')
432428
self.parent = console
@@ -481,8 +477,6 @@ def __repr__(self):
481477
self.height)
482478

483479

484-
485-
486480
def init(width, height, title='TDL', fullscreen=False):
487481
"""Start the main console with the given width and height and return the
488482
root console.
@@ -521,10 +515,14 @@ def flush():
521515
"""
522516
if not _rootinitialized:
523517
raise TDLError('Cannot flush without first initializing with tdl.init')
518+
519+
# old hack to prevent locking up on old libtcod
520+
# you can probably delete all autoflush releated stuff
524521
if event._autoflush and not event._eventsflushed:
525522
event.get()
526523
else: # do not flush events after the user starts using them
527524
event._autoflush = False
525+
528526
event._eventsflushed = False
529527
_lib.TCOD_console_flush()
530528

@@ -582,15 +580,16 @@ def screenshot(fileobj=None):
582580
raise TDLError('Initialize first with tdl.init')
583581
if isinstance(fileobj, str):
584582
_lib.TCOD_sys_save_screenshot(_format_string(fileobj))
585-
elif isinstance(fileobj, file):
586-
filename = os.tempnam()
587-
_lib.TCOD_sys_save_screenshot(_format_string(filename))
588-
fileobj.write(file(filename, 'r').read())
589-
os.remove(filename)
590-
elif fileobj is None:
583+
elif isinstance(fileobj, file): # save to temp file and copy to file-like obj
584+
tmpname = os.tempnam()
585+
_lib.TCOD_sys_save_screenshot(_format_string(tmpname))
586+
with tmpname as tmpfile:
587+
fileobj.write(tmpfile.read())
588+
os.remove(tmpname)
589+
elif fileobj is None: # save to screenshot001.png, screenshot002.png, ...
591590
filelist = os.listdir('.')
592-
n = 0
593-
filename = 'screenshot%.4i.png' % n
591+
n = 1
592+
filename = 'screenshot%.3i.png' % n
594593
while filename in filelist:
595594
n += 1
596595
filename = 'screenshot%.4i.png' % n

tdl/tcod.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class _Color(Structure):
5555
_fields_ = [('r', c_uint8), ('g', c_uint8), ('b', c_uint8)]
5656

5757
def __iter__(self):
58-
'to make this class more tuple-like
58+
'to make this class more tuple-like'
5959
return iter((self.r, self.g, self.b))
6060

6161
def __len__(self):

0 commit comments

Comments
 (0)