Skip to content

Commit

Permalink
extreme changes may have made this code unusable
Browse files Browse the repository at this point in the history
  • Loading branch information
HexDecimal committed Jul 13, 2015
1 parent 4385794 commit 3013f68
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 56 deletions.
17 changes: 17 additions & 0 deletions dev/benchmark.py
Expand Up @@ -104,6 +104,21 @@ def test(self, console):
self.tiles += 1
tdl.flush()

class Benchmark_SetItem(Benchmark):

def test(self, console):
for x,y in console:
console[x,y] = (ord('D'), 0xffffff, 0x000000)
self.tiles += 1
tdl.flush()

class Benchmark_SetRange(Benchmark):

def test(self, console):
console[:,:] = (ord('E'), 0xffffff, 0x000000)
self.tiles += console.width * console.height
tdl.flush()


class Benchmark_DrawStr16_DefaultColor(Benchmark):
default_frames = 100
Expand Down Expand Up @@ -138,6 +153,8 @@ def run_benchmark():
print_result('In %s mode' % (['release', 'debug'][__debug__]))
print_result('%i characters/frame' % (WIDTH * HEIGHT))
print_result('Opened console in %s mode' % RENDERER)
Benchmark_SetItem().run(console)
Benchmark_SetRange().run(console)
Benchmark_DrawChar_Ch_Attribute().run(console)
Benchmark_Get_FG_Attribute().run(console)
Benchmark_Set_FG_Attribute().run(console)
Expand Down
10 changes: 7 additions & 3 deletions dev/run_regression_test.py
Expand Up @@ -19,6 +19,8 @@

DEFAULT_CHAR = (0x20, (0, 0, 0), (0, 0, 0))

BLACK = tdl.Color(0, 0, 0)

class TDLTemplate(unittest.TestCase):
"Nearly all tests need tdl.init to be called"

Expand All @@ -32,7 +34,7 @@ def setUpClass(cls):
def setUp(self):
tdl.setFont('../fonts/libtcod/terminal8x8_gs_ro.png')
tdl.event.get()
self.console.set_colors((0,0,0), (0,0,0))
self.console.set_colors(BLACK, BLACK)
self.console.clear()

@classmethod
Expand Down Expand Up @@ -62,7 +64,9 @@ def getRandomCharacter(self):

def getRandomColor(self):
"returns a single random color"
return (random.getrandbits(8), random.getrandbits(8), random.getrandbits(8))
return tdl.Color((random.getrandbits(8),
random.getrandbits(8),
random.getrandbits(8)))

def getDrawables(self, console=None):
"""return a list of all drawable (x,y) positions
Expand Down Expand Up @@ -263,7 +267,7 @@ def test_scrolling(self):
random.randint(-HEIGHT, HEIGHT)))
for sx, sy in scrollTests:
noiseData = dict(self.randomizeConsole())
self.console.set_colors((0, 0, 0), (0, 0, 0))
self.console.set_colors(BLACK, BLACK)
self.console.scroll(sx, sy)
self.flush() # show progress
for x, y in self.getDrawables():
Expand Down
231 changes: 179 additions & 52 deletions tdl/__init__.py
Expand Up @@ -184,17 +184,29 @@ class Color(object):
"""

def __new__(cls, *rgb):
self = object.__new__(cls)
if not rgb:
self = object.__new__(cls)
self._r = self._g = self._b = 0
return self # blank (0, 0, 0)
try: # check if int-like
rgb = int(rgb)
self._r, self._g, self._b = _lib.TDL_color_int_to_array(color)[0:3]
return self
length = len(rgb)
if length == 3:
self = object.__new__(cls)
except TypeError:
pass # not an int
try: # try to unpack (r, g, b)
self.r, self.g, self.b = rgb
return self
if length == 1:
return cls.from_int(int(rgb[0]))
except TypeError:
pass # not a tuple-like
except ValueError:
pass # not 3 items
try: # try to unpack ((r, g, b),)
(self.r, self.g, self.b), = rgb
return self
except TypeError:
pass
except ValueError:
pass
raise TypeError('Parameters must be (r,g,b) or (int), got: %a' %
repr(rgb))

Expand Down Expand Up @@ -234,6 +246,11 @@ def _set_b(self, value):
g = property(_get_g, _set_g)
b = property(_get_b, _set_b)

def __eq__(self, other):
return (self._r == other._r and
self._g == other._g and
self._b == other._b)

def __repr__(self):
return '<%s[%i, %i, %i]>' % (self.__class__.__name__,
self._r, self._g, self._b)
Expand Down Expand Up @@ -307,51 +324,129 @@ def _get_slice(self, slice_x, slice_y):
slice_x = slice(slice_y, slice_y + 1)
return self.__class__(self._console, self._range_x[slice_x],
self._range_y[slice_y])

class _AttributeCh(_ConsoleAttribute):


def __getitem__(self, key):
if isinstance(key[0], slice) or isinstance(key[1], slice):
return self._get_slice(*key)
x = self._range_x[key[0]]
y = self._range_y[key[1]]
return _lib.TCOD_console_get_char(self._console.tcod_console, x, y)
return self._get_item(self._range_x[key[0]], self._range_y[key[1]])

def _get_item(self, x, y):
raise NotImplementedError('function should be overwritten by subclass')

def __setitem__(self, key, value):
if isinstance(key[0], slice) or isinstance(key[1], slice):
x, y = key
if not isinstance(x, slice):
x = slice(x, x + 1)
if not isinstance(y, slice):
y = slice(y, y + 1)
self._set_range(self._range_x[x], self._range_y[y])
else:
self._set_item(self._range_x[key[0]], self._range_y[key[1]], value)

def _set_item(self, x, y, value):
raise NotImplementedError('function should be overwritten by subclass')

def __setitem__(self, key, ch):
x = self._range_x[key[0]]
y = self._range_y[key[1]]
def _set_range(self, range_x, range_y, value):
raise NotImplementedError('function should be overwritten by subclass')

class _AttributeCh(_ConsoleAttribute):

def _get_item(self, x, y):
return _lib.TCOD_console_get_char(self._console.tcod_console, x, y)

def _set_item(self, x, y, ch):
_lib.TCOD_console_set_char(self._console.tcod_console, x, y, ch)

def _set_range(self, range_x, range_y, ch):
for x in range_x:
for y in range_y:
_lib.TCOD_console_set_char(self._console.tcod_console, x, y, ch)

# def __getitem__(self, key):
# if isinstance(key[0], slice) or isinstance(key[1], slice):
# return self._get_slice(*key)
# x = self._range_x[key[0]]
# y = self._range_y[key[1]]
# return _lib.TCOD_console_get_char(self._console.tcod_console, x, y)

# def __setitem__(self, key, ch):
# if isinstance(key[0], slice) or isinstance(key[1], slice):
# for y_ in self._range_y[key[1]]:
# for x_ in self._range_x[key[0]]:
# _lib.TCOD_console_set_char(self._console.tcod_console,
# x_, y_, ch)
# return
# _lib.TCOD_console_set_char(self._console.tcod_console,
# self._range_x[key[0]], self._range_y[key[1]], ch)

class _AttributeFG(_ConsoleAttribute):

def __getitem__(self, key):
if isinstance(key[0], slice) or isinstance(key[1], slice):
return self._get_slice(*key)
x = self._range_x[key[0]]
y = self._range_y[key[1]]
def _get_item(self, x, y):
return Color.from_int(
_lib.TDL_console_get_fg(self._console.tcod_console, x, y))

def __setitem__(self, key, fg):
x = self._range_x[key[0]]
y = self._range_y[key[1]]

def _set_item(self, x, y, fg):
_lib.TDL_console_set_fg(self._console.tcod_console, x, y, fg)

def _set_range(self, range_x, range_y, fg):
cdata = self._console.tcod_console
for x in range_x:
for y in range_y:
_lib.TDL_console_set_fg(cdata, x, y, fg)

# def __getitem__(self, key):
# if isinstance(key[0], slice) or isinstance(key[1], slice):
# return self._get_slice(*key)
# x = self._range_x[key[0]]
# y = self._range_y[key[1]]
# return Color.from_int(
# _lib.TDL_console_get_fg(self._console.tcod_console, x, y))

# def __setitem__(self, key, fg):
# x = self._range_x[key[0]]
# y = self._range_y[key[1]]
# if isinstance(x, range) or isinstance(y, range):
# for y_ in y:
# for x_ in x:
# _lib.TDL_console_set_fg(self._console.tcod_console,
# x_, y_, fg)
# return
# _lib.TDL_console_set_fg(self._console.tcod_console, x, y, fg)

class _AttributeBG(_ConsoleAttribute):

def __getitem__(self, key):
if isinstance(key[0], slice) or isinstance(key[1], slice):
return self._get_slice(*key)
x = self._range_x[key[0]]
y = self._range_y[key[1]]
return _lib.TCOD_console_get_char_background(
self._console.tcod_console, x, y)
def _get_item(self, x, y):
return Color.from_int(
_lib.TDL_console_get_bg(self._console.tcod_console, x, y))

def _set_item(self, x, y, fg):
_lib.TDL_console_set_bg(self._console.tcod_console, x, y, fg, 1)

def _set_range(self, range_x, range_y, fg):
cdata = self._console.tcod_console
for x in range_x:
for y in range_y:
_lib.TDL_console_set_bg(cdata, x, y, bg, 1)
# def __getitem__(self, key):
# if isinstance(key[0], slice) or isinstance(key[1], slice):
# return self._get_slice(*key)
# x = self._range_x[key[0]]
# y = self._range_y[key[1]]
# return _lib.TCOD_console_get_char_background(
# self._console.tcod_console, x, y)

def __setitem__(self, key, bg):
x = self._range_x[key[0]]
y = self._range_y[key[1]]
_lib.TCOD_console_set_char_background(
self._console.tcod_console, x, y, fg, 1)
# def __setitem__(self, key, bg):
# x = self._range_x[key[0]]
# y = self._range_y[key[1]]
# if isinstance(x, range) or isinstance(y, range):
# for y_ in y:
# for x_ in x:
# _lib.TDL_console_set_bg(self._console.tcod_console,
# x_, y_, bg)
# return
# _lib.TDL_console_set_bg(
# self._console.tcod_console, x, y, bg, 1)


def __init__(self):
Expand All @@ -373,7 +468,6 @@ def _normalizePoint(self, x, y):
# cast to int, always faster than type checking
x = int(x)
y = int(y)

# handle negative indexes
return self._range_x[x], self._range_y[y]

Expand Down Expand Up @@ -840,7 +934,7 @@ def move(self, x, y):
@param y: Y position to place the cursor.
@see: L{get_cursor}, L{print_str}, L{write}
"""
self._cursor = self._normalizePoint(x, y)
self._cursor = range(self.width)[x], range(self.height)[y]

def scroll(self, x, y):
"""Scroll the contents of the console in the direction of x,y.
Expand Down Expand Up @@ -1153,8 +1247,24 @@ def __getitem__(self, key):
x = self._range_x[x]
y = self._range_y[y]
return (_lib.TCOD_console_get_char(self.tcod_console, x, y),
_lib.TDL_console_get_fg(self.tcod_console, x, y),
_lib.TDL_console_get_bg(self.tcod_console, x, y))
Color.from_int(_lib.TDL_console_get_fg(self.tcod_console, x, y)),
Color.from_int(_lib.TDL_console_get_bg(self.tcod_console, x, y)))

def __setitem__(self, key, value):
if isinstance(key[0], slice) or isinstance(key[1], slice):
if isinstance(value, Window):
raise NotImplementedError('blit stub')
else:
ch, fg, bg = value
for y in self._range_y[key[1]]:
for x in self._range_x[key[0]]:
_lib.TDL_console_put_char_ex(self.tcod_console, x, y,
ch, fg, bg, 1)
else:
_lib.TDL_console_put_char_ex(self.tcod_console,
self._range_x[key[0]], self._range_y[key[1]], value[0],
value[1], value[2], 1)


def __repr__(self):
return "<Console (Width=%i Height=%i)>" % (self.width, self.height)
Expand Down Expand Up @@ -1204,7 +1314,10 @@ def __init__(self, console, x, y, width, height):
_BaseConsole.__init__(self)
assert isinstance(console, (Console, Window)), 'console parameter must be a Console or Window instance, got %s' % repr(console)
self.parent = console

if width is None:
width = console.width
if height is None:
height = console.height
slice_x = slice(x, x + width)
slice_y = slice(y, y + height)
self._range_x = console._range_x[slice_x]
Expand Down Expand Up @@ -1238,7 +1351,8 @@ def height(self):
def _translate(self, x, y):
"""Convertion x and y to their position on the root Console"""
# we add our position relative to our parent and then call then next parent up
return self.parent._translate(self._range_x[x], self._range_y[y])

return self.parent._translate(x + self._range_x[0], y + self._range_y[0])

def clear(self, fg=Ellipsis, bg=Ellipsis):
# inherit docstring
Expand Down Expand Up @@ -1271,23 +1385,21 @@ def draw_char(self, x, y, char, fg=Ellipsis, bg=Ellipsis):

def draw_rect(self, x, y, width, height, string, fg=Ellipsis, bg=Ellipsis):
# inherit docstring
x, y, width, height = self._normalizeRect(x, y, width, height)
if fg is Ellipsis:
fg = self._default_fg
if bg is Ellipsis:
bg = self._default_bg
self.parent.draw_rect(self._range_x[x], self._range_y[y], width, height,
string, fg, bg)
slice_x = slice(x, x + width)
slice_y = slice(y, y + height)
fg = _format_color(fg, self._default_fg)
bg = _format_color(bg, self._default_bg)
self[slice_x, slice_y] = (string, fg, bg)

def draw_frame(self, x, y, width, height, string, fg=Ellipsis, bg=Ellipsis):
# inherit docstring
x, y, width, height = self._normalizeRect(x, y, width, height)
x, y = self._translate(x, y)
if fg is Ellipsis:
fg = self._default_fg
if bg is Ellipsis:
bg = self._default_bg
self.parent.draw_frame(self._range_x[x], self._range_y[y], width, height,
string, fg, bg)
self.parent.draw_frame(x, y, width, height, string, fg, bg)

def get_char(self, x, y):
# inherit docstring
Expand Down Expand Up @@ -1317,6 +1429,21 @@ def __getitem__(self, key):
_lib.TDL_console_get_fg(self.console.tcod_console, x, y),
_lib.TDL_console_get_bg(self.console.tcod_console, x, y))

def __setitem__(self, key, value):
if isinstance(key[0], slice) or isinstance(key[1], slice):
if isinstance(value, Window):
raise NotImplementedError('blit stub')
else:
ch, fg, bg = value
for y in self._range_y[key[1]]:
for x in self._range_x[key[0]]:
_lib.TDL_console_put_char_ex(self.console.tcod_console,
x, y, ch, fg, bg, 1)
else:
_lib.TDL_console_put_char_ex(self.console.tcod_console,
self._range_x[key[0]], self._range_y[key[1]], value[0],
value[1], value[2], 1)


def __repr__(self):
return "<Window(X=%i Y=%i Width=%i Height=%i)>" % (self.x, self.y,
Expand Down

0 comments on commit 3013f68

Please sign in to comment.