1 """
2 The documentation for python-tdl. A Pythonic port of
3 U{libtcod<http://doryen.eptalys.net/libtcod/>}.
4
5 You can find the project page on Google Code
6 U{here<http://code.google.com/p/python-tdl/>}.
7
8 Getting Started
9 ===============
10 Once the library is imported you can load the font you want to use with
11 L{tdl.setFont}.
12 This is optional and when skipped will use a decent default font.
13
14 After that you call L{tdl.init} to set the size of the window and get the
15 root console in return.
16 This console is the canvas to what will appear on the screen.
17
18 Indexing Consoles
19 =================
20 For most methods taking a position you can use Python-style negative
21 indexes to refer to the opposite side of a console with (-1, -1)
22 starting at the bottom right.
23 You can also check if a point is part of a console using containment
24 logic i.e. ((x, y) in console).
25
26 Drawing
27 =======
28 Once you have the root console from L{tdl.init} you can start drawing on
29 it using a method such as L{Console.drawChar}.
30 When using this method you can have the char parameter be an integer or a
31 single character string.
32 The fgcolor and bgcolor parameters expect a three item list
33 [red, green, blue] with integers in the 0-255 range with [0, 0, 0] being
34 black and [255, 255, 255] being white.
35 Or instead you can use None for any of the three parameters to tell the
36 library to keep what is at that spot instead of overwriting it.
37 After the drawing functions are called a call to L{tdl.flush} will update
38 the screen.
39 """
40
41 import sys
42 import os
43
44 import ctypes
45 import weakref
46 import array
47 import itertools
48 import textwrap
49 import struct
50 import re
51 import warnings
52
53 from . import event, map, noise
54 from .__tcod import _lib, _Color, _unpackfile
55
56 _IS_PYTHON3 = (sys.version_info[0] == 3)
57
58 if _IS_PYTHON3:
59 _INTTYPES = (int,)
60 _NUMTYPES = (int, float)
61 _STRTYPES = (str, bytes)
62 else:
63 _INTTYPES = (int, long)
64 _NUMTYPES = (int, long, float)
65 _STRTYPES = (str,)
68 "changes string into bytes if running in python 3, for sending to ctypes"
69 if _IS_PYTHON3 and isinstance(string, str):
70 return string.encode()
71 return string
72
90
91 _fontinitialized = False
92 _rootinitialized = False
93 _rootConsoleRef = None
94
95 _setchar = _lib.TCOD_console_set_char
96 _setfore = _lib.TCOD_console_set_char_foreground
97 _setback = _lib.TCOD_console_set_char_background
98 _setcharEX = _lib.TCOD_console_put_char_ex
100 """Used internally.
101 Raise an assertion error if the parameters can not be converted into colors.
102 """
103 for color in colors:
104 assert _iscolor(color), 'a color must be a 3 item tuple, web format, or None, received %s' % repr(color)
105 return True
106
108 """Used internally.
109 A debug function to see if an object can be used as a TCOD color struct.
110 None counts as a parameter to keep the current colors instead.
111
112 This function is often part of an inner-loop and can slow a program down.
113 It has been made to work with assert and can be skipped with the -O flag.
114 Still it's called often and must be optimized.
115 """
116 if color is None:
117 return True
118 if isinstance(color, (tuple, list, _Color)):
119 return len(color) == 3
120 if isinstance(color, _INTTYPES):
121 return True
122 return False
123
152
154 """Try to get the width and height of a bmp of png image file"""
155 file = open(filename, 'rb')
156 if file.read(8) == b'\x89PNG\r\n\x1a\n':
157 while 1:
158 length, = struct.unpack('>i', file.read(4))
159 chunkID = file.read(4)
160 if chunkID == '':
161 return None
162 if chunkID == b'IHDR':
163
164 return struct.unpack('>ii', file.read(8))
165 file.seek(4 + length, 1)
166 file.seek(0)
167 if file.read(8) == b'BM':
168 file.seek(18, 0)
169
170 return struct.unpack('<ii', file.read(8))
171
174 """
175 The catch all for most TDL specific errors.
176 """
177
280
331
381
400
427
484
491
518 def getCover(x, length):
519 """return the (x, width) ranges of what is covered and uncovered"""
520 cover = (0, length)
521 uncover = None
522 if x > 0:
523 cover = (x, length - x)
524 uncover = (0, x)
525 elif x < 0:
526 x = abs(x)
527 cover = (0, length - x)
528 uncover = (length - x, x)
529 return cover, uncover
530
531 width, height = self.getSize()
532 if abs(x) >= width or abs(y) >= height:
533 return self.clear()
534
535
536 coverX, uncoverX = getCover(x, width)
537 coverY, uncoverY = getCover(y, height)
538
539
540
541
542
543
544
545 x, width, srcx = getSlide(x, width)
546 y, height, srcy = getSlide(y, height)
547 self.blit(self, x, y, width, height, srcx, srcy)
548
549 if uncoverX:
550 self.drawRect(uncoverX[0], coverY[0], uncoverX[1], coverY[1], 0x20)
551 if uncoverY:
552 self.drawRect(coverX[0], uncoverY[0], coverX[1], uncoverY[1], 0x20)
553 if uncoverX and uncoverY:
554 self.drawRect(uncoverX[0], uncoverY[0], uncoverX[1], uncoverY[1], 0x20)
555
568
574
576 """Contains character and color data and can be drawn to.
577
578 The console created by the L{tdl.init} function is the root console and is the
579 console that is rendered to the screen with L{flush}.
580
581 Any console created from the Console class is an off-screen console that
582 can be drawn on before being L{blit} to the root console.
583 """
584
585 __slots__ = ('_as_parameter_', '_typewriter')
586
588 """Create a new offscreen console.
589
590 @type width: int
591 @param width: Width of the console in tiles
592 @type height: int
593 @param height: Height of the console in tiles
594 """
595 if not _rootinitialized:
596 raise TDLError('Can not create Console\'s before tdl.init')
597 self._as_parameter_ = _lib.TCOD_console_new(width, height)
598 self.console = self
599 self.width = width
600 self.height = height
601 self._typewriter = None
602
603 @classmethod
605 """Make a Console instance, from a console ctype"""
606 self = cls.__new__(cls)
607 self._as_parameter_ = console
608 self.console = self
609 self.width = _lib.TCOD_console_get_width(self)
610 self.height = _lib.TCOD_console_get_height(self)
611 self._typewriter = None
612 return self
613
627
642
644 """Convertion x and y to their position on the root Console for this Window
645
646 Because this is a Console instead of a Window we return the paramaters
647 untouched"""
648 return x, y
649
650 - def clear(self, fgcolor=(0, 0, 0), bgcolor=(0, 0, 0)):
651 """Clears the entire Console.
652
653 @type fgcolor: (r, g, b)
654 @param fgcolor: Foreground color.
655
656 Must be a 3-item list with integers that range 0-255.
657
658 Unlike most other operations you can not use None here.
659 @type bgcolor: (r, g, b)
660 @param bgcolor: Background color. See fgcolor.
661 """
662 assert _verify_colors(fgcolor, bgcolor)
663 assert fgcolor and bgcolor, 'Can not use None with clear'
664 self._typewriter = None
665 _lib.TCOD_console_set_default_background(self, _formatColor(bgcolor))
666 _lib.TCOD_console_set_default_foreground(self, _formatColor(fgcolor))
667 _lib.TCOD_console_clear(self)
668
669 - def _setChar(self, x, y, char, fgcolor=None, bgcolor=None, bgblend=1):
670 """
671 Sets a character.
672 This is called often and is designed to be as fast as possible.
673
674 Because of the need for speed this function will do NO TYPE CHECKING
675 AT ALL, it's up to the drawing functions to use the functions:
676 _formatChar and _formatColor before passing to this."""
677
678 console = self._as_parameter_
679
680 if char is not None and fgcolor is not None and bgcolor is not None:
681 _setcharEX(console, x, y, char, fgcolor, bgcolor)
682 return
683 if char is not None:
684 _setchar(console, x, y, char)
685 if fgcolor is not None:
686 _setfore(console, x, y, fgcolor)
687 if bgcolor is not None:
688 _setback(console, x, y, bgcolor, bgblend)
689
690 - def _setCharBatch(self, batch, fgcolor, bgcolor, bgblend=1, nullChar=False):
691 """
692 Try to perform a batch operation otherwise fall back to _setChar.
693 If fgcolor and bgcolor are defined then this is faster but not by very
694 much.
695
696 batch is a iterable of [(x, y), ch] items
697 """
698 if fgcolor and not nullChar:
699
700 self._typewriter = None
701 console = self._as_parameter_
702 if not bgcolor:
703 bgblend = 0
704 bgblend = ctypes.c_int(bgblend)
705
706 _lib.TCOD_console_set_default_background(console, bgcolor)
707 _lib.TCOD_console_set_default_foreground(console, fgcolor)
708 _putChar = _lib.TCOD_console_put_char
709 for (x, y), char in batch:
710 _putChar(console, x, y, char, bgblend)
711 else:
712 for (x, y), char in batch:
713 self._setChar(x, y, char, fgcolor, bgcolor, bgblend)
714
716
717 x, y = self._normalizePoint(x, y)
718 char = _lib.TCOD_console_get_char(self, x, y)
719 bgcolor = _lib.TCOD_console_get_char_background_wrapper(self, x, y)
720 fgcolor = _lib.TCOD_console_get_char_foreground_wrapper(self, x, y)
721 return char, tuple(fgcolor), tuple(bgcolor)
722
724 return "<Console (Width=%i Height=%i)>" % (self.width, self.height)
725
726
727 -class Window(_MetaConsole):
728 """A Window contains a small isolated part of a Console.
729
730 Drawing on the Window draws on the Console.
731
732 Making a Window and setting its width or height to None will extend it to
733 the edge of the console.
734 """
735
736 __slots__ = ('parent', 'x', 'y')
737
738 - def __init__(self, console, x, y, width, height):
739 """Isolate part of a L{Console} or L{Window} instance.
740
741 @type console: L{Console} or L{Window}
742 @param console: The parent object which can be a L{Console} or another
743 L{Window} instance.
744
745 @type x: int
746 @param x: X coordinate to place the Window.
747
748 This follows the normal rules for indexing so you can use a
749 negative integer to place the Window relative to the bottom
750 right of the parent Console instance.
751 @type y: int
752 @param y: Y coordinate to place the Window.
753
754 See x.
755
756 @type width: int or None
757 @param width: Width of the Window.
758
759 Can be None to extend as far as possible to the
760 bottom right corner of the parent Console or can be a
761 negative number to be sized reltive to the Consoles total
762 size.
763 @type height: int or None
764 @param height: Height of the Window.
765
766 See width.
767 """
768 assert isinstance(console, (Console, Window)), 'console parameter must be a Console or Window instance, got %s' % repr(console)
769 self.parent = console
770 self.x, self.y, self.width, self.height = console._normalizeRect(x, y, width, height)
771 if isinstance(console, Console):
772 self.console = console
773 else:
774 self.console = self.parent.console
775
777 """Convertion x and y to their position on the root Console"""
778
779 return self.parent._translate((x + self.x), (y + self.y))
780
781 - def clear(self, fgcolor=(0, 0, 0), bgcolor=(0, 0, 0)):
782 """Clears the entire Window.
783
784 @type fgcolor: (r, g, b)
785 @param fgcolor: Foreground color.
786
787 Must be a 3-item list with integers that range 0-255.
788
789 Unlike most other operations you can not use None here.
790 @type bgcolor: (r, g, b)
791 @param bgcolor: Background color. See fgcolor.
792 """
793 assert _verify_colors(fgcolor, bgcolor)
794 assert fgcolor and bgcolor, 'Can not use None with clear'
795 self.draw_rect(0, 0, None, None, 0x20, fgcolor, bgcolor)
796
797 - def _setChar(self, x, y, char=None, fgcolor=None, bgcolor=None, bgblend=1):
798 self.parent._setChar((x + self.x), (y + self.y), char, fgcolor, bgcolor, bgblend)
799
801 myX = self.x
802 myY = self.y
803 self.parent._setCharBatch((((x + myX, y + myY), ch) for ((x, y), ch) in batch),
804 fgcolor, bgcolor, bgblend)
805
806
807 - def drawChar(self, x, y, char, fgcolor=(255, 255, 255), bgcolor=(0, 0, 0)):
811
812 - def drawRect(self, x, y, width, height, string, fgcolor=(255, 255, 255), bgcolor=(0, 0, 0)):
813
814 x, y, width, height = self._normalizeRect(x, y, width, height)
815 self.parent.drawRect(x + self.x, y + self.y, width, height, string, fgcolor, bgcolor)
816
817 - def drawFrame(self, x, y, width, height, string, fgcolor=(255, 255, 255), bgcolor=(0, 0, 0)):
818
819 x, y, width, height = self._normalizeRect(x, y, width, height)
820 self.parent.drawFrame(x + self.x, y + self.y, width, height, string, fgcolor, bgcolor)
821
826
828 return "<Window(X=%i Y=%i Width=%i Height=%i)>" % (self.x, self.y,
829 self.width,
830 self.height)
831
834 """Converts a console into a scrolling text log that respects special
835 characters.
836
837 This class works best on a L{Window} or off-screen L{Console} instance.
838 In a L{Window} for example the scrolling text is limited to the L{Window}'s
839 isolated area.
840 """
841
843 """Add a virtual cursor to a L{Console} or L{Window} instance.
844
845 @type console: L{Console} or L{Window}
846 """
847 assert isinstance(console, (Console, Window)), 'console parameter must be a Console or Window instance, got %s' % repr(console)
848 self.parent = console
849 if isinstance(self.parent, Console):
850 self.console = self.parent
851 else:
852 self.console = self.parent.console
853 self.cursor = (0, 0)
854 self.scrollMode = 'scroll'
855 self.fgcolor = _formatColor((255, 255, 255))
856 self.bgcolor = _formatColor((0, 0, 0))
857 self._bgblend = 1
858
860 """return the normalized the cursor position."""
861 width, height = self.parent.getSize()
862 while x >= width:
863 x -= width
864 y += 1
865 while y >= height:
866 if self.scrollMode == 'scroll':
867 y -= 1
868 self.parent.scroll(0, -1)
869 elif self.scrollMode == 'error':
870
871 self.cursor = (0, 0)
872 raise TDLError('Typewriter cursor has reached the end of the console')
873 return (x, y)
874
876 """Return the virtual cursor position.
877
878 @rtype: (int, int)
879 @return: Returns (x, y) a 2-integer tuple containing where the next
880 L{addChar} or L{addStr} will start at.
881
882 This can be changed with the L{move} method."""
883 x, y = self.cursor
884 width, height = self.parent.getSize()
885 while x >= width:
886 x -= width
887 y += 1
888 if y >= height and self.scrollMode == 'scroll':
889 y = height - 1
890 return x, y
891
892 - def move(self, x, y):
893 """Move the virtual cursor.
894
895 @type x: int
896 @param x: X position to place the cursor.
897 @type y: int
898 @param y: Y position to place the cursor.
899 """
900 self.cursor = self.parent._normalizePoint(x, y)
901
903 """Change the foreground color"""
904 assert _iscolor(color)
905 assert color is not None
906 self.fgcolor = _formatColor(color)
907 if self.console._typewriter is self:
908 _lib.TCOD_console_set_default_foreground(self.console, self.fgcolor)
909
911 """Change the background color"""
912 assert _iscolor(color)
913 assert color is not None
914 self.bgcolor = _formatColor(color)
915 if self.console._typewriter is self:
916 _lib.TCOD_console_set_default_background(self.console, self.bgcolor)
917
919 """Make sure the colors on a console match the Typewriter instance"""
920 if self.console._typewriter is not self:
921 self.console._typewriter = self
922
923 _lib.TCOD_console_set_default_background(self.console, self.bgcolor)
924 _lib.TCOD_console_set_default_foreground(self.console, self.fgcolor)
925
926
928 """Draw a single character at the cursor."""
929 if char == '\n':
930 x = 0
931 y += 1
932 return
933 if char == '\r':
934 x = 0
935 return
936 x, y = self._normalize(*self.cursor)
937 self.cursor = [x + 1, y]
938 self._updateConsole()
939 x, y = self.parent._translate(x, y)
940 _lib.TCOD_console_put_char(self.console._as_parameter_, x, y, _formatChar(char), self._bgblend)
941
942
944 """Write a string at the cursor. Handles special characters such as newlines.
945
946 @type string: string
947 @param string:
948 """
949 x, y = self.cursor
950 for char in string:
951 if char == '\n':
952 x = 0
953 y += 1
954 continue
955 if char == '\r':
956 x = 0
957 continue
958 x, y = self._normalize(x, y)
959 self.parent.drawChar(x, y, char, self.fgcolor, self.bgcolor)
960 x += 1
961 self.cursor = (x, y)
962
963 - def write(self, string):
964 """This method mimics basic file-like behaviour.
965
966 Because of this method you can replace sys.stdout or sys.stderr with
967 a L{Typewriter} instance.
968
969 @type string: string
970 """
971
972
973
974 x, y = self._normalize(*self.cursor)
975 width, height = self.parent.getSize()
976 wrapper = textwrap.TextWrapper(initial_indent=(' '*x), width=width)
977 writeLines = []
978 for line in string.split('\n'):
979 if line:
980 writeLines += wrapper.wrap(line)
981 wrapper.initial_indent = ''
982 else:
983 writeLines.append([])
984
985 for line in writeLines:
986 x, y = self._normalize(x, y)
987 self.parent.drawStr(x, y, line[x:], self.fgcolor, self.bgcolor)
988 y += 1
989 x = 0
990 y -= 1
991 self.cursor = (x, y)
992
993
994 -def init(width, height, title=None, fullscreen=False, renderer='OPENGL'):
995 """Start the main console with the given width and height and return the
996 root console.
997
998 Call the consoles drawing functions. Then remember to use L{tdl.flush} to
999 make what's drawn visible on the console.
1000
1001 @type width: int
1002 @param width: width of the root console (in tiles)
1003
1004 @type height: int
1005 @param height: height of the root console (in tiles)
1006
1007 @type title: string
1008 @param title: Text to display as the window title.
1009
1010 If left None it defaults to the running scripts filename.
1011
1012 @type fullscreen: boolean
1013 @param fullscreen: Can be set to True to start in fullscreen mode.
1014
1015 @type renderer: string
1016 @param renderer: Can be one of 'GLSL', 'OPENGL', or 'SDL'.
1017
1018 Due to way Python works you're unlikely to see much of an
1019 improvement by using 'GLSL' or 'OPENGL' as most of the
1020 time Python is slow interacting with the console and the
1021 rendering itself is pretty fast even on 'SDL'.
1022
1023 @rtype: L{Console}
1024 @return: The root console. Only what is drawn on the root console is
1025 what's visible after a call to L{tdl.flush}.
1026 After the root console is garbage collected, the window made by
1027 this function will close.
1028 """
1029 RENDERERS = {'GLSL': 0, 'OPENGL': 1, 'SDL': 2}
1030 global _rootinitialized, _rootConsoleRef
1031 if not _fontinitialized:
1032 setFont(_unpackfile('terminal8x8.png'), None, None, True, True)
1033
1034 if renderer.upper() not in RENDERERS:
1035 raise TDLError('No such render type "%s", expected one of "%s"' % (renderer, '", "'.join(RENDERERS)))
1036 renderer = RENDERERS[renderer.upper()]
1037
1038
1039 if _rootConsoleRef and _rootConsoleRef():
1040 oldroot = _rootConsoleRef()
1041 rootreplacement = Console(oldroot.width, oldroot.height)
1042 rootreplacement.blit(oldroot)
1043 oldroot._replace(rootreplacement)
1044 del rootreplacement
1045
1046 if title is None:
1047 if sys.argv:
1048
1049 title = os.path.basename(sys.argv[0])
1050 else:
1051 title = 'python-tdl'
1052
1053 _lib.TCOD_console_init_root(width, height, _encodeString(title), fullscreen, renderer)
1054
1055
1056
1057
1058 event._eventsflushed = False
1059 _rootinitialized = True
1060 rootconsole = Console._newConsole(ctypes.c_void_p())
1061 _rootConsoleRef = weakref.ref(rootconsole)
1062
1063 return rootconsole
1064
1066 """Make all changes visible and update the screen.
1067
1068 Remember to call this function after drawing operations.
1069 Calls to flush will enfore the frame rate limit set by L{tdl.setFPS}.
1070
1071 This function can only be called after L{tdl.init}
1072 """
1073 if not _rootinitialized:
1074 raise TDLError('Cannot flush without first initializing with tdl.init')
1075
1076 _lib.TCOD_console_flush()
1077
1078 -def setFont(path, columns=None, rows=None, columnFirst=False,
1079 greyscale=False, altLayout=False):
1080 """Changes the font to be used for this session.
1081 This should be called before L{tdl.init}
1082
1083 If the font specifies its size in its filename (i.e. font_NxN.png) then this
1084 function can auto-detect the tileset formatting and the parameters columns
1085 and rows can be left None.
1086
1087 While it's possible you can change the font mid program it can sometimes
1088 break in rare circumstances. So use caution when doing this.
1089
1090 @type path: string
1091 @param path: Must be a string filepath where a bmp or png file is found.
1092
1093 @type columns: int
1094 @param columns: Number of columns in the tileset.
1095
1096 Can be left None for auto-detection.
1097
1098 @type rows: int
1099 @param rows: Number of rows in the tileset.
1100
1101 Can be left None for auto-detection.
1102
1103 @type columnFirst: boolean
1104 @param columnFirst: Defines if the characer order goes along the rows or
1105 colomns.
1106 It should be True if the charater codes 0-15 are in the
1107 first column.
1108 And should be False if the characters 0-15
1109 are in the first row.
1110
1111 @type greyscale: boolean
1112 @param greyscale: Creates an anti-aliased font from a greyscale bitmap.
1113 Otherwise it uses the alpha channel for anti-aliasing.
1114
1115 Unless you actually need anti-aliasing from a font you
1116 know uses a smooth greyscale channel you should leave
1117 this on False.
1118
1119 @type altLayout: boolean
1120 @param altLayout: An alternative layout with space in the upper left
1121 corner.
1122 The colomn parameter is ignored if this is True,
1123 find examples of this layout in the font/libtcod/
1124 directory included with the python-tdl source.
1125
1126 @raise TDLError: Will be raised if no file is found at path or if auto-
1127 detection fails.
1128
1129 @note: A png file that's been optimized can fail to load correctly on
1130 MAC OS X creating a garbled mess when rendering.
1131 Don't use a program like optipng or just use bmp files instead if
1132 you want your program to work on macs.
1133 """
1134
1135 FONT_LAYOUT_ASCII_INCOL = 1
1136 FONT_LAYOUT_ASCII_INROW = 2
1137 FONT_TYPE_GREYSCALE = 4
1138 FONT_LAYOUT_TCOD = 8
1139 global _fontinitialized
1140 _fontinitialized = True
1141 flags = 0
1142 if altLayout:
1143 flags |= FONT_LAYOUT_TCOD
1144 elif columnFirst:
1145 flags |= FONT_LAYOUT_ASCII_INCOL
1146 else:
1147 flags |= FONT_LAYOUT_ASCII_INROW
1148 if greyscale:
1149 flags |= FONT_TYPE_GREYSCALE
1150 if not os.path.exists(path):
1151 raise TDLError('no file exists at: "%s"' % path)
1152 path = os.path.abspath(path)
1153
1154
1155 imgSize = _getImageSize(path)
1156 if imgSize:
1157 imgWidth, imgHeight = imgSize
1158
1159 match = re.match('.*?([0-9]+)[xX]([0-9]+)', os.path.basename(path))
1160 if match:
1161 fontWidth, fontHeight = match.groups()
1162 fontWidth, fontHeight = int(fontWidth), int(fontHeight)
1163
1164
1165 estColumns, remC = divmod(imgWidth, fontWidth)
1166 estRows, remR = divmod(imgHeight, fontHeight)
1167 if remC or remR:
1168 warnings.warn("Font may be incorrectly formatted.")
1169
1170 if not columns:
1171 columns = estColumns
1172 if not rows:
1173 rows = estRows
1174 else:
1175
1176 if not (columns and rows):
1177
1178 raise TDLError('%s has no font size in filename' % os.path.basename(path))
1179
1180 if columns and rows:
1181
1182 if (fontWidth * columns != imgWidth or
1183 fontHeight * rows != imgHeight):
1184 warnings.warn("setFont parameters are set as if the image size is (%d, %d) when the detected size is actually (%i, %i)"
1185 % (fontWidth * columns, fontHeight * rows,
1186 imgWidth, imgHeight))
1187 else:
1188 warnings.warn("%s is probably not an image." % os.path.basename(path))
1189
1190 if not (columns and rows):
1191
1192 raise TDLError('Can not auto-detect the tileset of %s' % os.path.basename(path))
1193
1194 _lib.TCOD_console_set_custom_font(_encodeString(path), flags, columns, rows)
1195
1197 """Returns True if program is fullscreen.
1198
1199 @rtype: boolean
1200 @return: Returns True if the window is in fullscreen mode.
1201 Otherwise returns False.
1202 """
1203 if not _rootinitialized:
1204 raise TDLError('Initialize first with tdl.init')
1205 return _lib.TCOD_console_is_fullscreen()
1206
1208 """Changes the fullscreen state.
1209
1210 @type fullscreen: boolean
1211 """
1212 if not _rootinitialized:
1213 raise TDLError('Initialize first with tdl.init')
1214 _lib.TCOD_console_set_fullscreen(fullscreen)
1215
1217 """Change the window title.
1218
1219 @type title: string
1220 """
1221 if not _rootinitialized:
1222 raise TDLError('Not initilized. Set title with tdl.init')
1223 _lib.TCOD_console_set_window_title(_encodeString(title))
1224
1226 """Capture the screen and save it as a png file
1227
1228 @type path: string
1229 @param path: The filepath to save the screenshot.
1230
1231 If path is None then the image will be placed in the current
1232 folder with the names:
1233 screenshot001.png, screenshot002.png, ...
1234 """
1235 if not _rootinitialized:
1236 raise TDLError('Initialize first with tdl.init')
1237 if isinstance(fileobj, str):
1238 _lib.TCOD_sys_save_screenshot(_encodeString(fileobj))
1239 elif isinstance(fileobj, file):
1240 tmpname = os.tempnam()
1241 _lib.TCOD_sys_save_screenshot(_encodeString(tmpname))
1242 with tmpname as tmpfile:
1243 fileobj.write(tmpfile.read())
1244 os.remove(tmpname)
1245 elif fileobj is None:
1246 filelist = os.listdir('.')
1247 n = 1
1248 filename = 'screenshot%.3i.png' % n
1249 while filename in filelist:
1250 n += 1
1251 filename = 'screenshot%.4i.png' % n
1252 _lib.TCOD_sys_save_screenshot(_encodeString(filename))
1253 else:
1254 raise TypeError('fileobj is an invalid type: %s' % type(fileobj))
1255
1257 """Set the maximum frame rate.
1258
1259 @type frameRate: int
1260 @param frameRate: Further calls to L{tdl.flush} will limit the speed of
1261 the program to run at <frameRate> frames per second. Can
1262 also be set to 0 to run without a limit.
1263
1264 Defaults to None.
1265 """
1266 if frameRate is None:
1267 frameRate = 0
1268 assert isinstance(frameRate, _INTTYPES), 'frameRate must be an integer or None, got: %s' % repr(frameRate)
1269 _lib.TCOD_sys_set_fps(frameRate)
1270
1272 """Return the current frames per second of the running program set by
1273 L{setFPS}
1274
1275 @rtype: int
1276 @return: Returns the frameRate set by setFPS.
1277 If set to no limit, this will return 0.
1278 """
1279 return _lib.TCOD_sys_get_fps()
1280
1282 """Change the fullscreen resoulution
1283
1284 @type width: int
1285 @type height: int
1286 """
1287 _lib.TCOD_sys_force_fullscreen_resolution(width, height)
1288
1289 __all__ = [_var for _var in locals().keys() if _var[0] != '_' and _var not in ['sys', 'os', 'ctypes', 'array', 'weakref', 'itertools', 'textwrap', 'struct', 're', 'warnings']]
1290 __all__ += ['_MetaConsole']
1291