Skip to content

Commit 9b0a9b7

Browse files
committed
Console.__del__ now actually deletes the root console, tdl now imports libtcod from the libtcod-cffi module
1 parent 7083bd6 commit 9b0a9b7

File tree

4 files changed

+50
-94
lines changed

4 files changed

+50
-94
lines changed

tdl/__init__.py

Lines changed: 38 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
import sys as _sys
6161
import os as _os
6262

63-
#import ctypes as _ctypes
6463
import array as _array
6564
import weakref as _weakref
6665
import itertools as _itertools
@@ -69,8 +68,10 @@
6968
import re as _re
7069
import warnings as _warnings
7170

71+
from tcod import ffi as _ffi
72+
from tcod import lib as _lib
73+
7274
from . import event, map, noise
73-
from .libtcod import _ffi, _lib
7475
from . import style as _style
7576

7677

@@ -123,7 +124,7 @@ def _format_str(string):
123124
_rootinitialized = False
124125
_rootConsoleRef = None
125126

126-
_set_char = _lib.set_char
127+
_put_char_ex = _lib.TDL_console_put_char_ex
127128

128129
# python 2 to 3 workaround
129130
if _sys.version_info[0] == 2:
@@ -387,8 +388,8 @@ def draw_char(self, x, y, char, fg=Ellipsis, bg=Ellipsis):
387388
@see: L{get_char}
388389
"""
389390
#x, y = self._normalizePoint(x, y)
390-
_set_char(self._as_parameter_, x, y, _format_char(char),
391-
_format_color(fg, self._fg), _format_color(bg, self._bg), 1)
391+
_put_char_ex(self._as_parameter_, x, y, _format_char(char),
392+
_format_color(fg, self._fg), _format_color(bg, self._bg), 1)
392393

393394
def draw_str(self, x, y, string, fg=Ellipsis, bg=Ellipsis):
394395
"""Draws a string starting at x and y.
@@ -813,24 +814,40 @@ def _newConsole(cls, console):
813814
self.height = _lib.TCOD_console_get_height(console)
814815
self._typewriter = None
815816
return self
817+
818+
def _root_unhook(self):
819+
"""Change this root console into a normal Console object and
820+
delete the root console from TCOD
821+
"""
822+
global _rootinitialized, _rootConsoleRef
823+
# do we recognise this as the root console?
824+
# if not then assume the console has already been taken care of
825+
if(_rootConsoleRef and _rootConsoleRef() is self):
826+
# turn this console into a regular console
827+
unhooked = _lib.TCOD_console_new(self.width, self.height)
828+
_lib.TCOD_console_blit(self._as_parameter_,
829+
0, 0, self.width, self.height,
830+
unhooked, 0, 0, 1, 1)
831+
# delete root console from TDL and TCOD
832+
_rootinitialized = False
833+
_rootConsoleRef = None
834+
_lib.TCOD_console_delete(self._as_parameter_)
835+
# this Console object is now a regular console
836+
self._as_parameter_ = unhooked
816837

817838
def __del__(self):
818839
"""
819840
If the main console is garbage collected then the window will be closed as well
820841
"""
821-
global _rootinitialized, _rootConsoleRef
822-
# check of see if the pointer is to the special root console
823-
#if isinstance(self._as_parameter_, _ctypes.c_void_p):
842+
if self._as_parameter_ is None:
843+
return # this console was already deleted
824844
if self._as_parameter_ is _ffi.NULL:
825-
# do we recognise this root console?
826-
if(_rootConsoleRef and _rootConsoleRef() is self):
827-
_rootinitialized = False
828-
_rootConsoleRef = None
829-
_lib.TCOD_console_delete(self._as_parameter_)
830-
# if not then assume the console has already been taken care of
831-
else:
832-
# this is a normal console pointer and can be safely deleted
833-
_lib.TCOD_console_delete(self._as_parameter_)
845+
# a pointer to the special root console
846+
self._root_unhook() # unhook the console and leave it to the GC
847+
return
848+
# this is a normal console pointer and can be safely deleted
849+
_lib.TCOD_console_delete(self._as_parameter_)
850+
self._as_parameter_ = None
834851

835852
def __copy__(self):
836853
# make a new class and blit
@@ -897,37 +914,7 @@ def _set_char(self, x, y, char, fg=None, bg=None,
897914
AT ALL, it's up to the drawing functions to use the functions:
898915
_format_char and _format_color before passing to this."""
899916
# values are already formatted, honestly this function is redundant
900-
return _lib.set_char(self._as_parameter_, x, y, char, fg, bg, bgblend)
901-
902-
903-
# if char is not None and fg is not None and bg is not None:
904-
# if fg is bg is Ellipsis:
905-
# # char is not None and all colors are Ellipsis
906-
# # use default colors previously set in this console
907-
# _put_char(console, x, y, char, bgblend)
908-
# return
909-
# # all parameters are not None
910-
# # use default colors for any ellipsis
911-
# if fg is Ellipsis:
912-
# fg = self._fg
913-
# if bg is Ellipsis:
914-
# bg = self._bg
915-
916-
# _put_char_ex(console, x, y, char, fg[0], bg[0])
917-
# return
918-
# # some parameters are None
919-
# # selectively commit parameters to the console
920-
# # use default colors for any ellipsis
921-
# if char is not None:
922-
# _set_char(console, x, y, char)
923-
# if fg is not None:
924-
# if fg is Ellipsis:
925-
# fg = self._fg
926-
# _set_fg(console, x, y, fg[0])
927-
# if bg is not None:
928-
# if bg is Ellipsis:
929-
# bg = self._bg
930-
# _set_bg(console, x, y, bg[0], bgblend)
917+
return _put_char_ex(self._as_parameter_, x, y, char, fg, bg, bgblend)
931918

932919
def _set_batch(self, batch, fg, bg, bgblend=1, nullChar=False):
933920
"""
@@ -939,37 +926,8 @@ def _set_batch(self, batch, fg, bg, bgblend=1, nullChar=False):
939926
940927
batch is a iterable of [(x, y), ch] items
941928
"""
942-
if fg is Ellipsis:
943-
fg = self._fg
944-
if bg is Ellipsis:
945-
bg = self._bg
946-
947-
#if fg != -1:
948-
# fg = _to_tcod_color(fg)
949-
#if bg != -1:
950-
# bg = _to_tcod_color(bg)
951-
952929
for (x, y), char in batch:
953930
self._set_char(x, y, char, fg, bg, bgblend)
954-
955-
# if fg != -1 and not nullChar:
956-
# # buffer values as ctypes objects
957-
# self._typewriter = None # clear the typewriter as colors will be set
958-
# console = self._as_parameter_
959-
# #bgblend = _ctypes.c_int(bgblend)
960-
961-
# if bg == -1:
962-
# bgblend = 0
963-
# else:
964-
# _lib.TCOD_console_set_default_background(console, bg[0])
965-
# _lib.TCOD_console_set_default_foreground(console, fg[0])
966-
# _putChar = _lib.TCOD_console_put_char # remove dots and make local
967-
# for (x, y), char in batch:
968-
# _putChar(console, x, y, char, bgblend)
969-
970-
# else:
971-
# for (x, y), char in batch:
972-
# self._set_char(x, y, char, fg, bg, bgblend)
973931

974932
def get_char(self, x, y):
975933
# inherit docstring
@@ -1148,11 +1106,9 @@ def init(width, height, title=None, fullscreen=False, renderer='OPENGL'):
11481106

11491107
# If a console already exists then make a clone to replace it
11501108
if _rootConsoleRef and _rootConsoleRef():
1151-
oldroot = _rootConsoleRef()
1152-
rootreplacement = Console(oldroot.width, oldroot.height)
1153-
rootreplacement.blit(oldroot)
1154-
oldroot._replace(rootreplacement)
1155-
del rootreplacement
1109+
# unhook the root console, turning into a regular console and deleting
1110+
# the root console from libTCOD
1111+
_rootConsoleRef()._root_unhook()
11561112

11571113
if title is None: # use a default title
11581114
if _sys.argv:

tdl/event.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@
3232

3333
import time as _time
3434

35-
#from .__tcod import _lib, _Mouse, _Key
36-
from .libtcod import _ffi, _lib
35+
from tcod import ffi as _ffi
36+
from tcod import lib as _lib
3737

38-
#from . import __tcod as _tcod
38+
import . as _tdl
3939
from . import style as _style
40-
import tdl as _tdl
4140

4241
_eventQueue = []
4342
_pushedEvents = []

tdl/map.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
Rogue-like map utilitys such as line-of-sight, field-of-view, and path-finding.
33
44
"""
5-
#import ctypes as _ctypes
5+
66
import itertools as _itertools
77
import math as _math
88

9-
import tdl as _tdl
10-
#from .__tcod import _lib, _PATHCALL
11-
from .libtcod import _ffi, _lib
9+
from tcod import ffi as _ffi
10+
from tcod import lib as _lib
11+
12+
import . as _tdl
1213
from . import style as _style
1314

1415
_FOVTYPES = {'BASIC' : 0, 'DIAMOND': 1, 'SHADOW': 2, 'RESTRICTIVE': 12, 'PERMISSIVE': 11}

tdl/noise.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212

1313
import random as _random
14-
#import ctypes as _ctypes
1514

16-
import tdl as _tdl
17-
#from .__tcod import _lib
18-
from .libtcod import _ffi, _lib
15+
from tcod import ffi as _ffi
16+
from tcod import lib as _lib
17+
18+
import . as _tdl
1919
from . import style as _style
2020

2121
_MERSENNE_TWISTER = 1

0 commit comments

Comments
 (0)