|
1
|
|
|
"""This module handles loading of the libtcod cffi API. |
|
2
|
|
|
""" |
|
3
|
|
|
from __future__ import absolute_import as _ |
|
4
|
|
|
|
|
5
|
|
|
import sys as _sys |
|
6
|
|
|
import os as _os |
|
7
|
|
|
|
|
8
|
|
|
import platform as _platform |
|
9
|
|
|
|
|
10
|
|
|
from tcod import __path__ |
|
11
|
|
|
|
|
12
|
|
|
# add Windows dll's to PATH |
|
13
|
|
|
if _sys.platform == 'win32': |
|
14
|
|
|
_bits, _linkage = _platform.architecture() |
|
15
|
|
|
_os.environ['PATH'] += (';' + |
|
16
|
|
|
_os.path.join(__path__[0], 'x86/' if _bits == '32bit' else 'x64')) |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
def _import_library_functions(lib): |
|
|
|
|
|
|
20
|
|
|
# imports libtcod namespace into thie module |
|
21
|
|
|
# does not override existing names |
|
22
|
|
|
g = globals() |
|
23
|
|
|
for name in dir(lib): |
|
24
|
|
|
if name[:5] == 'TCOD_': |
|
25
|
|
|
if (isinstance(getattr(lib, name), ffi.CData) and |
|
|
|
|
|
|
26
|
|
|
ffi.typeof(getattr(lib, name)) == ffi.typeof('TCOD_color_t')): |
|
|
|
|
|
|
27
|
|
|
g[name[5:]] = Color._new_from_cdata(getattr(lib, name)) |
|
28
|
|
|
elif name.isupper(): |
|
29
|
|
|
g[name[5:]] = getattr(lib, name) # const names |
|
30
|
|
|
#else: |
|
31
|
|
|
# g[name[5:]] = getattr(lib, name) # function names |
|
32
|
|
|
elif name[:6] == 'TCODK_': # key name |
|
33
|
|
|
g['KEY_' + name[6:]] = getattr(lib, name) |
|
34
|
|
|
|
|
35
|
|
|
NOISE_DEFAULT_HURST = 0.5 |
|
36
|
|
|
NOISE_DEFAULT_LACUNARITY = 2.0 |
|
37
|
|
|
|
|
38
|
|
|
def FOV_PERMISSIVE(p) : |
|
|
|
|
|
|
39
|
|
|
return FOV_PERMISSIVE_0+p |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
def BKGND_ALPHA(a): |
|
|
|
|
|
|
42
|
|
|
return BKGND_ALPH | (int(a * 255) << 8) |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
def BKGND_ADDALPHA(a): |
|
|
|
|
|
|
45
|
|
|
return BKGND_ADDA | (int(a * 255) << 8) |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
class _MockFFI(object): |
|
|
|
|
|
|
48
|
|
|
def def_extern(self): |
|
|
|
|
|
|
49
|
|
|
return lambda func:func |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
if _os.environ.get('READTHEDOCS'): |
|
52
|
|
|
# Mock the lib and ffi objects needed to compile docs for readthedocs.io |
|
53
|
|
|
# Allows an import without building the cffi module first. |
|
54
|
|
|
lib = object() |
|
55
|
|
|
ffi = _MockFFI() |
|
56
|
|
|
RENDERER_SDL = 2 |
|
57
|
|
|
FONT_LAYOUT_ASCII_INCOL = 1 |
|
58
|
|
|
BKGND_SET = 1 |
|
59
|
|
|
BKGND_DEFAULT = 13 |
|
60
|
|
|
KEY_RELEASED = 2 |
|
61
|
|
|
NOISE_DEFAULT = 0 |
|
62
|
|
|
NOISE_SIMPLEX = 2 |
|
63
|
|
|
else: |
|
64
|
|
|
from tcod._libtcod import lib, ffi |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
class Color(list): |
|
67
|
|
|
""" |
|
68
|
|
|
|
|
69
|
|
|
Args: |
|
70
|
|
|
r (int): Red value, from 0 to 255. |
|
71
|
|
|
g (int): Green value, from 0 to 255. |
|
72
|
|
|
b (int): Blue value, from 0 to 255. |
|
73
|
|
|
""" |
|
74
|
|
|
|
|
75
|
|
|
def __init__(self, r=0, g=0, b=0): |
|
|
|
|
|
|
76
|
|
|
self[:] = (r & 0xff, g & 0xff, b & 0xff) |
|
77
|
|
|
|
|
78
|
|
|
@property |
|
79
|
|
|
def r(self): |
|
80
|
|
|
"""int: Red value, always normalised to 0-255.""" |
|
81
|
|
|
return self[0] |
|
82
|
|
|
|
|
83
|
|
|
@r.setter |
|
84
|
|
|
def r(self, value): |
|
|
|
|
|
|
85
|
|
|
self[0] = value & 0xff |
|
86
|
|
|
|
|
87
|
|
|
@property |
|
88
|
|
|
def g(self): |
|
89
|
|
|
"""int: Green value, always normalised to 0-255.""" |
|
90
|
|
|
return self[1] |
|
91
|
|
|
@g.setter |
|
92
|
|
|
def g(self, value): |
|
|
|
|
|
|
93
|
|
|
self[1] = value & 0xff |
|
94
|
|
|
|
|
95
|
|
|
@property |
|
96
|
|
|
def b(self): |
|
97
|
|
|
"""int: Blue value, always normalised to 0-255.""" |
|
98
|
|
|
return self[2] |
|
99
|
|
|
@b.setter |
|
100
|
|
|
def b(self, value): |
|
|
|
|
|
|
101
|
|
|
self[2] = value & 0xff |
|
102
|
|
|
|
|
103
|
|
|
@classmethod |
|
104
|
|
|
def _new_from_cdata(cls, cdata): |
|
105
|
|
|
"""new in libtcod-cffi""" |
|
106
|
|
|
return cls(cdata.r, cdata.g, cdata.b) |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
@classmethod |
|
110
|
|
|
def _new_from_int(cls, integer): |
|
111
|
|
|
"""a TDL int color: 0xRRGGBB |
|
112
|
|
|
|
|
113
|
|
|
new in libtcod-cffi""" |
|
114
|
|
|
return cls(lib.TDL_color_from_int(integer)) |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
def __eq__(self, other): |
|
117
|
|
|
"""Compare equality between colors.""" |
|
118
|
|
|
return (isinstance(other, (Color)) and |
|
119
|
|
|
lib.TCOD_color_equals(self, other)) |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
def __add__(self, other): |
|
122
|
|
|
"""Add two colors together.""" |
|
123
|
|
|
return Color._new_from_cdata(lib.TCOD_color_add(self, other)) |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
def __sub__(self, other): |
|
126
|
|
|
"""Subtract one color from another.""" |
|
127
|
|
|
return Color._new_from_cdata(lib.TCOD_color_subtract(self, other)) |
|
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
def __mul__(self, other): |
|
130
|
|
|
"""Multiply with a scaler or another color.""" |
|
131
|
|
|
if isinstance(other, (Color, list, tuple)): |
|
132
|
|
|
return Color._new_from_cdata(lib.TCOD_color_multiply(self, other)) |
|
|
|
|
|
|
133
|
|
|
else: |
|
134
|
|
|
return Color._new_from_cdata( |
|
135
|
|
|
lib.TCOD_color_multiply_scalar(self, other)) |
|
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
def __bytes__(self): |
|
138
|
|
|
"""Return this color in a format suited for color control.""" |
|
139
|
|
|
return b'%c%c%c' % tuple(self) |
|
140
|
|
|
|
|
141
|
|
|
def __str__(self): |
|
142
|
|
|
"""Return this color in a format suited for color control.""" |
|
143
|
|
|
return '%c%c%c' % tuple(self) |
|
144
|
|
|
|
|
145
|
|
|
def __repr__(self): |
|
146
|
|
|
"""Return a printable representation of the current color.""" |
|
147
|
|
|
return "%s(%i,%i,%i)" % (self.__class__.__name__, |
|
148
|
|
|
self.r, self.g, self.b) |
|
149
|
|
|
|
|
150
|
|
|
def __int__(self): |
|
151
|
|
|
"""Return this color as an integer in 0xRRGGBB format.""" |
|
152
|
|
|
return lib.TDL_color_RGB(*self) |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
_import_library_functions(lib) |
|
156
|
|
|
|
|
157
|
|
|
__all__ = [_name for _name in list(globals()) if _name[0] != '_'] |
|
158
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.