Skip to content

Commit 1ef58f1

Browse files
committed
start of CFFI experiments
1 parent 6faf92d commit 1ef58f1

File tree

5 files changed

+739
-32
lines changed

5 files changed

+739
-32
lines changed

tdl/__init__.py

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@
7070

7171
from . import event, map, noise
7272
from .__tcod import _lib, _Color, _unpackfile
73+
from .libtcod import _ffi, _lib
7374
from . import __style as _style
7475

76+
7577
_IS_PYTHON3 = (_sys.version_info[0] == 3)
7678

7779
if _IS_PYTHON3: # some type lists to use with isinstance
@@ -133,13 +135,32 @@ def _iscolor(color):
133135
return True
134136
if color is None:
135137
return True
136-
if isinstance(color, (tuple, list, _Color)):
138+
if isinstance(color, (tuple, list, _ffi.CData)):
137139
return len(color) == 3
138140
if isinstance(color, _INTTYPES):
139141
return True
140142
return False
141143

142-
_formatColor = _Color.parse
144+
# python 2 to 3 workaround
145+
if _sys.version_info[0] == 2:
146+
int_types = (int, long)
147+
else:
148+
int_types = int
149+
150+
151+
#_formatColor = _Color.parse
152+
def _formatColor(color):
153+
if color is Ellipsis or color is None or color is False:
154+
return color
155+
if (isinstance(color, _ffi.CData) and
156+
_ffi.typeof(color) is _ffi.typeof('TCOD_color_t *')):
157+
return color
158+
if isinstance(color, int_types):
159+
# format a web style color with the format 0xRRGGBB
160+
return _ffi.new('TCOD_color_t *', (color >> 16 & 0xff,
161+
color >> 8 & 0xff,
162+
color & 0xff))
163+
return _ffi.new('TCOD_color_t *', color)
143164

144165
def _getImageSize(filename):
145166
"""Try to get the width and height of a bmp of png image file"""
@@ -381,7 +402,7 @@ def draw_char(self, x, y, char, fg=Ellipsis, bg=Ellipsis):
381402

382403
assert _verify_colors(fg, bg)
383404
x, y = self._normalizePoint(x, y)
384-
x, y = _ctypes.c_int(x), _ctypes.c_int(y)
405+
#x, y = _ctypes.c_int(x), _ctypes.c_int(y)
385406
self._set_char(x, y, _formatChar(char),
386407
_formatColor(fg), _formatColor(bg))
387408

@@ -489,8 +510,10 @@ def draw_rect(self, x, y, width, height, string, fg=Ellipsis, bg=Ellipsis):
489510
char = _formatChar(string)
490511
# use itertools to make an x,y grid
491512
# using ctypes here reduces type converstions later
492-
grid = _itertools.product((_ctypes.c_int(x) for x in range(x, x + width)),
493-
(_ctypes.c_int(y) for y in range(y, y + height)))
513+
#grid = _itertools.product((_ctypes.c_int(x) for x in range(x, x + width)),
514+
# (_ctypes.c_int(y) for y in range(y, y + height)))
515+
grid = _itertools.product((x for x in range(x, x + width)),
516+
(y for y in range(y, y + height)))
494517
# zip the single character in a batch variable
495518
batch = zip(grid, _itertools.repeat(char, width * height))
496519
self._set_batch(batch, fg, bg, nullChar=(char is None))
@@ -595,10 +618,15 @@ def blit(self, source, x=0, y=0, width=None, height=None, srcX=0, srcY=0):
595618
# onto the data, otherwise it tries to copy into itself and
596619
# starts destroying everything
597620
tmp = Console(width, height)
598-
_lib.TCOD_console_blit(source, srcX, srcY, width, height, tmp, 0, 0, fgalpha, bgalpha)
599-
_lib.TCOD_console_blit(tmp, 0, 0, width, height, self, x, y, fgalpha, bgalpha)
621+
_lib.TCOD_console_blit(source._as_parameter_,
622+
srcX, srcY, width, height,
623+
tmp._as_parameter_, 0, 0, fgalpha, bgalpha)
624+
_lib.TCOD_console_blit(tmp._as_parameter_, 0, 0, width, height,
625+
self._as_parameter_, x, y, fgalpha, bgalpha)
600626
else:
601-
_lib.TCOD_console_blit(source, srcX, srcY, width, height, self, x, y, fgalpha, bgalpha)
627+
_lib.TCOD_console_blit(source._as_parameter_,
628+
srcX, srcY, width, height,
629+
self._as_parameter_, x, y, fgalpha, bgalpha)
602630

603631
def get_cursor(self):
604632
"""Return the virtual cursor position.
@@ -775,8 +803,8 @@ def _newConsole(cls, console):
775803
_BaseConsole.__init__(self)
776804
self._as_parameter_ = console
777805
self.console = self
778-
self.width = _lib.TCOD_console_get_width(self)
779-
self.height = _lib.TCOD_console_get_height(self)
806+
self.width = _lib.TCOD_console_get_width(console)
807+
self.height = _lib.TCOD_console_get_height(console)
780808
self._typewriter = None
781809
return self
782810

@@ -791,11 +819,11 @@ def __del__(self):
791819
if(_rootConsoleRef and _rootConsoleRef is self):
792820
_rootinitialized = False
793821
_rootConsoleRef = None
794-
_lib.TCOD_console_delete(self)
822+
_lib.TCOD_console_delete(self._as_parameter_)
795823
# if not then assume the console has already been taken care of
796824
else:
797825
# this is a normal console pointer and can be safely deleted
798-
_lib.TCOD_console_delete(self)
826+
_lib.TCOD_console_delete(self._as_parameter_)
799827

800828
def __copy__(self):
801829
# make a new class and blit
@@ -828,8 +856,8 @@ def _replace(self, console):
828856
console._as_parameter_, self._as_parameter_ # swap tcod consoles
829857
else:
830858
self._as_parameter_ = console
831-
self.width = _lib.TCOD_console_get_width(self)
832-
self.height = _lib.TCOD_console_get_height(self)
859+
self.width = _lib.TCOD_console_get_width(self._as_parameter_)
860+
self.height = _lib.TCOD_console_get_height(self._as_parameter_)
833861
return self
834862

835863
def _translate(self, x, y):
@@ -864,9 +892,9 @@ def clear(self, fg=Ellipsis, bg=Ellipsis):
864892
bg = self._bg
865893
else:
866894
bg = _formatColor(bg)
867-
_lib.TCOD_console_set_default_foreground(self, fg)
868-
_lib.TCOD_console_set_default_background(self, bg)
869-
_lib.TCOD_console_clear(self)
895+
_lib.TCOD_console_set_default_foreground(self._as_parameter_, fg[0])
896+
_lib.TCOD_console_set_default_background(self._as_parameter_, bg[0])
897+
_lib.TCOD_console_clear(self._as_parameter_)
870898

871899

872900
def _set_char(self, x, y, char, fg=Ellipsis, bg=Ellipsis, bgblend=1):
@@ -892,7 +920,7 @@ def _set_char(self, x, y, char, fg=Ellipsis, bg=Ellipsis, bgblend=1):
892920
if bg is Ellipsis:
893921
bg = self._bg
894922

895-
_put_char_ex(console, x, y, char, fg, bg)
923+
_put_char_ex(console, x, y, char, fg[0], bg[0])
896924
return
897925
# some parameters are None
898926
# selectively commit parameters to the console
@@ -902,11 +930,11 @@ def _set_char(self, x, y, char, fg=Ellipsis, bg=Ellipsis, bgblend=1):
902930
if fg is not None:
903931
if fg is Ellipsis:
904932
fg = self._fg
905-
_set_fg(console, x, y, fg)
933+
_set_fg(console, x, y, fg[0])
906934
if bg is not None:
907935
if bg is Ellipsis:
908936
bg = self._bg
909-
_set_bg(console, x, y, bg, bgblend)
937+
_set_bg(console, x, y, bg[0], bgblend)
910938

911939
def _set_batch(self, batch, fg, bg, bgblend=1, nullChar=False):
912940
"""
@@ -945,9 +973,9 @@ def _set_batch(self, batch, fg, bg, bgblend=1, nullChar=False):
945973
def get_char(self, x, y):
946974
# inherit docstring
947975
x, y = self._normalizePoint(x, y)
948-
char = _lib.TCOD_console_get_char(self, x, y)
949-
bg = _lib.TCOD_console_get_char_background_wrapper(self, x, y)
950-
fg = _lib.TCOD_console_get_char_foreground_wrapper(self, x, y)
976+
char = _lib.TCOD_console_get_char(self._as_parameter_, x, y)
977+
bg = _lib.TCOD_console_get_char_background(self._as_parameter_, x, y)
978+
fg = _lib.TCOD_console_get_char_foreground(self._as_parameter_, x, y)
951979
return char, tuple(fg), tuple(bg)
952980

953981
def __repr__(self):
@@ -1079,8 +1107,6 @@ def __repr__(self):
10791107
return "<Window(X=%i Y=%i Width=%i Height=%i)>" % (self.x, self.y,
10801108
self.width,
10811109
self.height)
1082-
#
1083-
10841110

10851111

10861112
def init(width, height, title=None, fullscreen=False, renderer='OPENGL'):
@@ -1150,7 +1176,7 @@ def init(width, height, title=None, fullscreen=False, renderer='OPENGL'):
11501176

11511177
event._eventsflushed = False
11521178
_rootinitialized = True
1153-
rootconsole = Console._newConsole(_ctypes.c_void_p())
1179+
rootconsole = Console._newConsole(_ffi.NULL)
11541180
_rootConsoleRef = _weakref.ref(rootconsole)
11551181

11561182
return rootconsole

0 commit comments

Comments
 (0)