@@ -22,12 +22,11 @@ def _formatChar(char):
2222 "Prepares a single character for passing to ctypes calls"
2323 if char is None :
2424 return None
25- elif isinstance (char , (str , bytes ))and len (char ) == 1 :
25+ if isinstance (char , (str , bytes )) and len (char ) == 1 :
2626 return ord (char )
27- elif isinstance (char , ( int )):
27+ if isinstance (char , int ) or not _IS_PYTHON3 and isinstance ( char , long ):
2828 return char
29- else :
30- raise TypeError ('Expected char parameter to be a single character string, number, or None, got %s' % repr (char ))
29+ raise TypeError ('Expected char parameter to be a single character string, number, or None, got: %s' % repr (char ))
3130
3231_fontinitialized = False
3332_rootinitialized = False
@@ -41,7 +40,7 @@ def _verify_colors(*colors):
4140 "raise an error if the parameters can not be converted into colors"
4241 for color in colors :
4342 if not _iscolor (color ):
44- raise TypeError ('a color must be a 3 item tuple or None, recived %s' % repr ())
43+ raise TypeError ('a color must be a 3 item tuple or None, received %s' % repr ())
4544
4645def _iscolor (color ):
4746 """Used internally
@@ -51,9 +50,10 @@ def _iscolor(color):
5150 # this is called often and must be optimized
5251 if color is None :
5352 return True
54- if isinstance (color , (tuple , list , _Color )) and len (color ) == 3 :
55- return True
56- if isinstance (color , int ) and 0x000000 <= color <= 0xffffff :
53+ if isinstance (color , (tuple , list , _Color )):
54+ #if color.__class__ in (tuple, list, _Color):
55+ return len (color ) == 3
56+ if color .__class__ is int and 0x000000 <= color <= 0xffffff :
5757 return True
5858 return False
5959
@@ -62,24 +62,27 @@ def _formatColor(color):
6262 """
6363 if color is None :
6464 return None
65- if isinstance (color , _Color ):
65+ # avoid isinstance, checking __class__ gives a small speed increase
66+ if color .__class__ is _Color :
6667 return color
67- if isinstance ( color , int ) :
68+ if color . __class__ is int :
6869 # format a web style color with the format 0xRRGGBB
6970 return _Color (color >> 16 & 0xff , color >> 8 & 0xff , color & 0xff )
7071 return _Color (* color )
7172
7273class TDLError (Exception ):
73- pass
74+ """
75+ The catch all for most TDL specific errors.
76+ """
7477
75- class TDLDrawError (TDLError ):
76- pass
78+ # class TDLDrawError(TDLError):
79+ # pass
7780
7881#class TDLBlitError(TDLError):
7982# pass
8083
81- class TDLIndexError (TDLError ):
82- pass
84+ # class TDLIndexError(TDLError):
85+ # pass
8386
8487class Console (object ):
8588 """The Console is the main class of the tdl library.
@@ -177,13 +180,17 @@ def _clampRect(self, x=0, y=0, width=None, height=None):
177180 width = height = 0
178181 return x , y , width , height
179182
180- def blit (self , source , x = 0 , y = 0 , width = None , height = None , srcx = 0 , srcy = 0 , fgalpha = 1.0 , bgalpha = 1.0 ):
183+ def blit (self , source , x = 0 , y = 0 , width = None , height = None , srcx = 0 , srcy = 0 ):
181184 """Blit another console or Window onto the current console.
182185
183186 By default it blits the entire source to the topleft corner.
184187
185- If nothing is blited then TDLBlitError is raised
188+ If nothing is blited then TDLError is raised
186189 """
190+ # hardcode alpha settings for now
191+ fgalpha = 1.0
192+ bgalpha = 1.0
193+
187194 assert isinstance (source , (Console , Window )), "source muse be a Window or Console instance"
188195
189196 # if None is used, get the cursor position
@@ -228,7 +235,6 @@ def _cursor_normalize(self):
228235 def _cursor_advance (self , rate = 1 ):
229236 "Move the virtual cursor forward, one step by default"
230237 self .cursorX += rate
231- self ._cursor_normalize ()
232238
233239 def _cursor_newline (self ):
234240 "Move the cursor down and set x=0"
@@ -238,6 +244,8 @@ def _cursor_newline(self):
238244
239245 def _cursor_get (self , x = None , y = None ):
240246 "Fill in blanks with the cursor position"
247+ if x is None or y is None :
248+ self ._cursor_normalize () # make sure cursor is valid first
241249 if x is None :
242250 x = self .cursorX
243251 if y is None :
@@ -267,7 +275,7 @@ def _drawable(self, x, y):
267275
268276 if (x is None or 0 <= x < self .width ) and (y is None or 0 <= y < self .height ):
269277 return
270- raise TDLIndexError ('(%i, %i) is an invalid postition. %s size is (%i, %i)' %
278+ raise TDLError ('(%i, %i) is an invalid postition. %s size is (%i, %i)' %
271279 (x , y , self .__class__ .__name__ , self .width , self .height ))
272280
273281 def _translate (self , x , y ):
@@ -279,8 +287,8 @@ def clear(self, fgcolor=C_WHITE, bgcolor=C_BLACK):
279287 """
280288 #assert _iscolor(fillcolor), 'fillcolor must be a 3 item list'
281289 #assert fillcolor is not None, 'fillcolor can not be None'
282- _lib .TCOD_console_set_foreground_color (self , _Color ( * fgcolor ))
283- _lib .TCOD_console_set_background_color (self , _Color ( * bgcolor ))
290+ _lib .TCOD_console_set_foreground_color (self , _formatColor ( fgcolor ))
291+ _lib .TCOD_console_set_background_color (self , _formatColor ( bgcolor ))
284292 _lib .TCOD_console_clear (self )
285293
286294 def _setChar (self , char , x , y , fgcolor = None , bgcolor = None , bgblend = BND_SET ):
@@ -293,34 +301,33 @@ def _setChar(self, char, x, y, fgcolor=None, bgcolor=None, bgblend=BND_SET):
293301 if bgcolor is not None :
294302 _setback (self , x , y , _formatColor (bgcolor ), bgblend )
295303
296- def drawChar (self , char , x = None , y = None , fgcolor = None , bgcolor = None , bgblend = BND_SET ):
304+ def drawChar (self , char , x = None , y = None , fgcolor = None , bgcolor = None ):
297305 """Draws a single character.
298306
299307 char should be an integer, single character string, or None
300308 you can set the char parameter as None if you only want to change
301309 the colors of the tile.
302310
303- For fgcolor and bgcolor you use a 3 item list or a Color instance. None
304- will keep the color unchanged.
305-
306- bgblend is used to tell how the bgcolor blends into the background. The
307- default is BND_SET.
311+ For fgcolor and bgcolor you use a 3 item list or None
312+ None will keep the color unchanged.
308313
309- Having the x or y values outside of the console will raise a
310- tdl.BadIndex error.
314+ Having the x or y values outside of the console will raise a TDLError.
311315 """
312- if isinstance (char , (str , bytes )):
313- assert len (char ) == 1 , 'strings must only have one character'
314- char = ord (char )
315- assert isinstance (char , int ) or char is None , \
316- 'char must be an integer, single character string, or None'
316+ # hardcode alpha settings for now
317+ 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'
323+ char = _formatChar (char )
317324 _verify_colors (fgcolor , bgcolor )
318325 x , y = self ._cursor_move (x , y )
319326
320327 self ._setChar (char , x , y , _formatColor (fgcolor ), _formatColor (bgcolor ), bgblend )
321328 self ._cursor_advance ()
322329
323- def drawStr (self , string , x = None , y = None , fgcolor = None , bgcolor = None , bgblend = BND_SET ):
330+ def drawStr (self , string , x = None , y = None , fgcolor = None , bgcolor = None ):
324331 """Draws a string starting at x and y.
325332
326333 A string that goes past the end will wrap around. No warning will be
@@ -332,6 +339,9 @@ def drawStr(self, string, x=None, y=None, fgcolor=None, bgcolor=None, bgblend=BN
332339
333340 If a large enough tileset is loaded you can use a unicode string.
334341 """
342+ # hardcode alpha settings for now
343+ bgblend = BND_SET
344+
335345 x , y = self ._cursor_move (x , y )
336346 _verify_colors (fgcolor , bgcolor )
337347 fgcolor , bgcolor = _formatColor (fgcolor ), _formatColor (bgcolor )
@@ -340,28 +350,35 @@ def drawStr(self, string, x=None, y=None, fgcolor=None, bgcolor=None, bgblend=BN
340350 self ._setChar (char , self .cursorX , self .cursorY , fgcolor , bgcolor , bgblend )
341351 self ._cursor_advance ()
342352
343- def drawRect (self , char , x = 0 , y = 0 , width = None , height = None , fgcolor = None , bgcolor = None , bgblend = BND_SET ):
353+ def drawRect (self , char , x = 0 , y = 0 , width = None , height = None , fgcolor = None , bgcolor = None ):
344354 """Draws a rectangle starting from x and y and extending to width and
345355 height. If width or height are None then it will extend to the edge
346356 of the console. The rest are the same as drawChar.
347357 """
358+ # hardcode alpha settings for now
359+ bgblend = BND_SET
360+
348361 # replace None with cursor position
349362 x , y = self ._cursor_get (x , y )
350363 # clamp rect to bounds
351364 x , y , width , height = self ._clampRect (x , y , width , height )
352365 if (width , height ) == (0 , 0 ):
353- raise TDLDrawError ('Rectange is entirely out of bounds' )
366+ raise TDLError ('Rectange is out of bounds at (%i, %i), bounds are (%i, %i)' % (( x , y ) + self . getSize ()) )
354367 _verify_colors (fgcolor , bgcolor )
355368 fgcolor , bgcolor = _formatColor (fgcolor ), _formatColor (bgcolor )
356369 for cellY in range (y , y + height ):
357370 for cellX in range (x , x + width ):
358371 self ._setChar (char , cellX , cellY , fgcolor , bgcolor , bgblend )
359372
360- def drawFrame (self , char , x = 0 , y = 0 , width = None , height = None , fgcolor = None , bgcolor = None , bgblend = BND_SET ):
373+ def drawFrame (self , char , x = 0 , y = 0 , width = None , height = None , fgcolor = None , bgcolor = None ):
374+ "Similar to drawRect but only draws the outline of the rectangle"
375+ # hardcode alpha settings for now
376+ bgblend = BND_SET
377+
361378 x , y = self ._cursor_get (x , y )
362379 x , y , width , height = self ._clampRect (x , y , width , height )
363380 if (width , height ) == (0 , 0 ):
364- raise TDLDrawError ('Rectange is entirely out of bounds' )
381+ raise TDLError ('Rectange is out of bounds at (%i, %i), bounds are (%i, %i)' % (( x , y ) + self . getSize ()) )
365382 _verify_colors (fgcolor , bgcolor )
366383 fgcolor , bgcolor = _formatColor (fgcolor ), _formatColor (bgcolor )
367384 #self.draw_rect(fgcolor, bgcolor, char, x, y, width, height, False, bgblend)
@@ -382,8 +399,6 @@ def getChar(self, x, y):
382399 """
383400 self ._drawable (x , y )
384401 char = _lib .TCOD_console_get_char (self , x , y )
385- #if _IS_PYTHON3:
386- # char = char.decode()
387402 fgcolor = tuple (_lib .TCOD_console_get_fore (self , x , y ))
388403 bgcolor = tuple (_lib .TCOD_console_get_back (self , x , y ))
389404 return char , fgcolor , bgcolor
@@ -407,7 +422,7 @@ class Window(object):
407422
408423 def __init__ (self , console , x = 0 , y = 0 , width = None , height = None ):
409424 if not isinstance (console , (Console , Window )):
410- raise TypeError ('console must be a Console or Window instance' )
425+ raise TypeError ('console parameter must be a Console or Window instance' )
411426 assert isinstance (x , int )
412427 assert isinstance (y , int )
413428 assert isinstance (width , int ) or width is None
@@ -505,7 +520,7 @@ def flush():
505520 """Make all changes visible and update the screen.
506521 """
507522 if not _rootinitialized :
508- raise TDLError ('Cannot flush without first initializing' )
523+ raise TDLError ('Cannot flush without first initializing with tdl.init ' )
509524 if event ._autoflush and not event ._eventsflushed :
510525 event .get ()
511526 else : # do not flush events after the user starts using them
@@ -536,17 +551,17 @@ def getFullscreen():
536551 """Returns if the window is fullscreen
537552
538553 The user can't make the window fullscreen so you must use the
539- set_fullscreen function
554+ setFullscreen function
540555 """
541556 if not _rootinitialized :
542- raise TDLError ('Initialize first' )
557+ raise TDLError ('Initialize first with tdl.init ' )
543558 return _lib .TCOD_console_is_fullscreen ()
544559
545560def setFullscreen (fullscreen ):
546561 """Sets the fullscreen state to the boolen value
547562 """
548563 if not _rootinitialized :
549- raise TDLError ('Initialize first' )
564+ raise TDLError ('Initialize first with tdl.init ' )
550565 _lib .TCOD_console_set_fullscreen (fullscreen )
551566
552567def setTitle (title ):
@@ -556,26 +571,6 @@ def setTitle(title):
556571 raise TDLError ('Not initilized. Set title with tdl.init' )
557572 _lib .TCOD_console_set_window_title (_format_string (title ))
558573
559- def setFade (color , fade ):
560- """Have the screen fade out into a color.
561-
562- color is a 3 item tuple or list.
563- fade is a number between 0 and 255 with 0 making the screen one solid color
564- and 255 turning the fade effect off.
565- """
566- if not _rootinitialized :
567- raise TDLError ('Initialize first' )
568- _lib .TCOD_console_set_fade (fade , _Color (* color ))
569-
570- def getFade ():
571- """Return the current fade of the screen as (color, fade)
572- """
573- if not _rootinitialized :
574- raise TDLError ('Initialize first' )
575- fade = _lib .TCOD_console_get_fade ()
576- fadecolor = tuple (_lib .TCOD_console_get_fading_color ())
577- return fadecolor , fade
578-
579574def screenshot (fileobj = None ):
580575 """Capture the screen and place it in fileobj.
581576
@@ -584,7 +579,7 @@ def screenshot(fileobj=None):
584579 screenshotNNNN.png
585580 """
586581 if not _rootinitialized :
587- raise TDLError ('Initialize first' )
582+ raise TDLError ('Initialize first with tdl.init ' )
588583 if isinstance (fileobj , str ):
589584 _lib .TCOD_sys_save_screenshot (_format_string (fileobj ))
590585 elif isinstance (fileobj , file ):
@@ -623,19 +618,5 @@ def forceResolution(width, height):
623618 """
624619 _lib .TCOD_sys_force_fullscreen_resolution (width , height )
625620
626- def bresenham (startx , starty , endx , endy ):
627- """Returns a list of points between and including the start and end points.
628- The points create a bresenham line.
629- """
630- _lib .TCOD_line_init (startx , starty , endx , endy )
631- finished = False
632- pointlist = []
633- x = ctypes .c_int ()
634- y = ctypes .c_int ()
635- while not finished :
636- finished = _lib .TCOD_line_step (x , y )
637- pointlist .append ((x .value , y .value ))
638- return pointlist
639-
640621__all__ = [var for var in locals ().keys () if not '_' in var [0 ]]
641622
0 commit comments