Module libtcodpy
[hide private]
[frames] | no frames]

Source Code for Module libtcodpy

   1  # 
   2  # libtcod 1.5.0 python wrapper 
   3  # Copyright (c) 2008 Jice 
   4  # All rights reserved. 
   5  # 
   6  # Redistribution and use in source and binary forms, with or without 
   7  # modification, are permitted provided that the following conditions are met: 
   8  #     * Redistributions of source code must retain the above copyright 
   9  #       notice, this list of conditions and the following disclaimer. 
  10  #     * Redistributions in binary form must reproduce the above copyright 
  11  #       notice, this list of conditions and the following disclaimer in the 
  12  #       documentation and/or other materials provided with the distribution. 
  13  #     * The name of Jice may not be used to endorse or promote products 
  14  #       derived from this software without specific prior written permission. 
  15  # 
  16  # THIS SOFTWARE IS PROVIDED BY Jice ``AS IS'' AND ANY 
  17  # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
  18  # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
  19  # DISCLAIMED. IN NO EVENT SHALL Jice BE LIABLE FOR ANY 
  20  # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  21  # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
  22  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
  23  # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
  24  # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
  25  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
  26  # 
  27   
  28  import sys 
  29  import ctypes 
  30  from ctypes import * 
  31   
  32  try:  #import NumPy if available 
  33          import numpy 
  34          numpy_available = True 
  35  except ImportError: 
  36          numpy_available = False 
  37   
  38  if sys.platform.find('linux') != -1: 
  39      _lib = ctypes.cdll['./libtcod.so'] 
  40  else: 
  41      _lib = ctypes.cdll['./libtcod-mingw.dll'] 
  42   
  43  HEXVERSION = 0x010500 
  44  STRVERSION = "1.5.0" 
  45  TECHVERSION = 0x01050003 
  46   
  47  ############################ 
  48  # color module 
  49  ############################ 
50 -class Color(Structure):
51 _fields_ = [('r', c_uint, 8), 52 ('g', c_uint, 8), 53 ('b', c_uint, 8), 54 ] 55
56 - def __init__(self, r=0,g=0,b=0):
57 self.r = r 58 self.g = g 59 self.b = b
60
61 - def __eq__(self, c):
62 return (self.r == c.r) and (self.g == c.g) and (self.b == c.b)
63
64 - def __mul__(self, c):
65 iret=0 66 if isinstance(c,Color): 67 iret=_lib.TCOD_color_multiply_wrapper(col_to_int(self), col_to_int(c)) 68 else: 69 iret=_lib.TCOD_color_multiply_scalar_wrapper(col_to_int(self), c_float(c)) 70 return int_to_col(iret)
71
72 - def __add__(self, c):
73 iret=_lib.TCOD_color_add_wrapper(col_to_int(self), col_to_int(c)) 74 return int_to_col(iret)
75
76 - def __sub__(self, c):
77 iret=_lib.TCOD_color_subtract_wrapper(col_to_int(self), col_to_int(c)) 78 return int_to_col(iret)
79
80 -def int_to_col(i) :
81 c=Color() 82 c.r=(i&0xFF0000)>>16 83 c.g=(i&0xFF00)>>8 84 c.b=(i&0xFF) 85 return c
86
87 -def col_to_int(c) :
88 return (int(c.r) <<16) | (c.g<<8) | c.b;
89 90 # default colors 91 # grey levels 92 black=Color(0,0,0) 93 darker_grey=Color(31,31,31) 94 dark_grey=Color(63,63,63) 95 grey=Color(128,128,128) 96 light_grey=Color(191,191,191) 97 darker_gray=Color(31,31,31) 98 dark_gray=Color(63,63,63) 99 gray=Color(128,128,128) 100 light_gray=Color(191,191,191) 101 white=Color(255,255,255) 102 103 #standard colors 104 red=Color(255,0,0) 105 orange=Color(255,127,0) 106 yellow=Color(255,255,0) 107 chartreuse=Color(127,255,0) 108 green=Color(0,255,0) 109 sea=Color(0,255,127) 110 cyan=Color(0,255,255) 111 sky=Color(0,127,255) 112 blue=Color(0,0,255) 113 violet=Color(127,0,255) 114 magenta=Color(255,0,255) 115 pink=Color(255,0,127) 116 117 # dark colors 118 dark_red=Color(127,0,0) 119 dark_orange=Color(127,63,0) 120 dark_yellow=Color(127,127,0) 121 dark_chartreuse=Color(63,127,0) 122 dark_green=Color(0,127,0) 123 dark_sea=Color(0,127,63) 124 dark_cyan=Color(0,127,127) 125 dark_sky=Color(0,63,127) 126 dark_blue=Color(0,0,127) 127 dark_violet=Color(63,0,127) 128 dark_magenta=Color(127,0,127) 129 dark_pink=Color(127,0,63) 130 131 # darker colors 132 darker_red=Color(63,0,0) 133 darker_orange=Color(63,31,0) 134 darker_yellow=Color(63,63,0) 135 darker_chartreuse=Color(31,63,0) 136 darker_green=Color(0,63,0) 137 darker_sea=Color(0,63,31) 138 darker_cyan=Color(0,63,63) 139 darker_sky=Color(0,31,63) 140 darker_blue=Color(0,0,63) 141 darker_violet=Color(31,0,63) 142 darker_magenta=Color(63,0,63) 143 darker_pink=Color(63,0,31) 144 145 # light colors 146 light_red=Color(255,127,127) 147 light_orange=Color(255,191,127) 148 light_yellow=Color(255,255,127) 149 light_chartreuse=Color(191,255,127) 150 light_green=Color(127,255,127) 151 light_sea=Color(127,255,191) 152 light_cyan=Color(127,255,255) 153 light_sky=Color(127,191,255) 154 light_blue=Color(127,127,255) 155 light_violet=Color(191,127,255) 156 light_magenta=Color(255,127,255) 157 light_pink=Color(255,127,191) 158 159 # desaturated colors 160 desaturated_red=Color(127,63,63) 161 desaturated_orange=Color(127,95,63) 162 desaturated_yellow=Color(127,127,63) 163 desaturated_chartreuse=Color(95,127,63) 164 desaturated_green=Color(63,127,63) 165 desaturated_sea=Color(63,127,95) 166 desaturated_cyan=Color(63,127,127) 167 desaturated_sky=Color(63,95,127) 168 desaturated_blue=Color(63,63,127) 169 desaturated_violet=Color(95,63,127) 170 desaturated_magenta=Color(127,63,127) 171 desaturated_pink=Color(127,63,95) 172 173 # special 174 silver=Color(203,203,203) 175 gold=Color(255,255,102) 176 177 # color functions
178 -def color_lerp(c1, c2, a):
179 iret = _lib.TCOD_color_lerp_wrapper(col_to_int(c1), col_to_int(c2), c_float(a)) 180 return int_to_col(iret)
181
182 -def color_set_hsv(c, h, s, v):
183 _lib.TCOD_color_set_HSV(byref(c), c_float(h), c_float(s), c_float(v))
184
185 -def color_get_hsv(c):
186 h = c_float() 187 s = c_float() 188 v = c_float() 189 _lib.TCOD_color_get_HSV(c, byref(h), byref(s), byref(v)) 190 return h.value, s.value, v.value
191
192 -def color_gen_map(colors, indexes):
193 COLOR_ARRAY = c_ubyte * (3 * len(colors)) 194 IDX_ARRAY = c_int * len(indexes) 195 ccolors = COLOR_ARRAY() 196 cindexes = IDX_ARRAY() 197 maxidx = 0 198 for i in range(len(colors)): 199 ccolors[3 * i] = colors[i].r 200 ccolors[3 * i + 1] = colors[i].g 201 ccolors[3 * i + 2] = colors[i].b 202 cindexes[i] = c_int(indexes[i]) 203 if indexes[i] > maxidx: 204 maxidx = indexes[i] 205 RES_ARRAY = c_ubyte * (3 * (maxidx + 1)) 206 cres = RES_ARRAY() 207 _lib.TCOD_color_gen_map(cres, len(colors), ccolors, cindexes) 208 res = list() 209 i = 0 210 while i < 3 * (maxidx + 1): 211 col = Color(cres[i], cres[i + 1], cres[i + 2]) 212 res.append(col) 213 i += 3 214 return res
215 216 ############################ 217 # console module 218 ############################
219 -class Key(Structure):
220 _fields_=[('vk', c_int, 32), 221 ('c', c_int, 8), 222 ('pressed', c_uint, 8), 223 ('lalt', c_uint, 8), 224 ('lctrl', c_uint, 8), 225 ('ralt', c_uint, 8), 226 ('rctrl', c_uint, 8), 227 ('shift', c_uint, 8), 228 ]
229 230 _lib.TCOD_console_wait_for_keypress.restype = Key 231 _lib.TCOD_console_check_for_keypress.restype = Key 232 _lib.TCOD_console_credits_render.restype = c_uint 233 _lib.TCOD_console_set_custom_font.argtypes=[c_char_p,c_int] 234 235 # background rendering modes 236 BKGND_NONE = 0 237 BKGND_SET = 1 238 BKGND_MULTIPLY = 2 239 BKGND_LIGHTEN = 3 240 BKGND_DARKEN = 4 241 BKGND_SCREEN = 5 242 BKGND_COLOR_DODGE = 6 243 BKGND_COLOR_BURN = 7 244 BKGND_ADD = 8 245 BKGND_ADDA = 9 246 BKGND_BURN = 10 247 BKGND_OVERLAY = 11 248 BKGND_ALPH = 12 249
250 -def BKGND_ALPHA(a):
251 return BKGND_ALPH | (int(a * 255) << 8)
252
253 -def BKGND_ADDALPHA(a):
254 return BKGND_ADDA | (int(a * 255) << 8)
255 256 # non blocking key events types 257 KEY_PRESSED = 1 258 KEY_RELEASED = 2 259 # key codes 260 KEY_NONE = 0 261 KEY_ESCAPE = 1 262 KEY_BACKSPACE = 2 263 KEY_TAB = 3 264 KEY_ENTER = 4 265 KEY_SHIFT = 5 266 KEY_CONTROL = 6 267 KEY_ALT = 7 268 KEY_PAUSE = 8 269 KEY_CAPSLOCK = 9 270 KEY_PAGEUP = 10 271 KEY_PAGEDOWN = 11 272 KEY_END = 12 273 KEY_HOME = 13 274 KEY_UP = 14 275 KEY_LEFT = 15 276 KEY_RIGHT = 16 277 KEY_DOWN = 17 278 KEY_PRINTSCREEN = 18 279 KEY_INSERT = 19 280 KEY_DELETE = 20 281 KEY_LWIN = 21 282 KEY_RWIN = 22 283 KEY_APPS = 23 284 KEY_0 = 24 285 KEY_1 = 25 286 KEY_2 = 26 287 KEY_3 = 27 288 KEY_4 = 28 289 KEY_5 = 29 290 KEY_6 = 30 291 KEY_7 = 31 292 KEY_8 = 32 293 KEY_9 = 33 294 KEY_KP0 = 34 295 KEY_KP1 = 35 296 KEY_KP2 = 36 297 KEY_KP3 = 37 298 KEY_KP4 = 38 299 KEY_KP5 = 39 300 KEY_KP6 = 40 301 KEY_KP7 = 41 302 KEY_KP8 = 42 303 KEY_KP9 = 43 304 KEY_KPADD = 44 305 KEY_KPSUB = 45 306 KEY_KPDIV = 46 307 KEY_KPMUL = 47 308 KEY_KPDEC = 48 309 KEY_KPENTER = 49 310 KEY_F1 = 50 311 KEY_F2 = 51 312 KEY_F3 = 52 313 KEY_F4 = 53 314 KEY_F5 = 54 315 KEY_F6 = 55 316 KEY_F7 = 56 317 KEY_F8 = 57 318 KEY_F9 = 58 319 KEY_F10 = 59 320 KEY_F11 = 60 321 KEY_F12 = 61 322 KEY_NUMLOCK = 62 323 KEY_SCROLLLOCK = 63 324 KEY_SPACE = 64 325 KEY_CHAR = 65 326 # special chars 327 # single walls 328 CHAR_HLINE = 196 329 CHAR_VLINE = 179 330 CHAR_NE = 191 331 CHAR_NW = 218 332 CHAR_SE = 217 333 CHAR_SW = 192 334 CHAR_TEEW = 180 335 CHAR_TEEE = 195 336 CHAR_TEEN = 193 337 CHAR_TEES = 194 338 CHAR_CROSS = 197 339 # double walls 340 CHAR_DHLINE = 205 341 CHAR_DVLINE = 186 342 CHAR_DNE = 187 343 CHAR_DNW = 201 344 CHAR_DSE = 188 345 CHAR_DSW = 200 346 CHAR_DTEEW = 185 347 CHAR_DTEEE = 204 348 CHAR_DTEEN = 202 349 CHAR_DTEES = 203 350 CHAR_DCROSS = 206 351 # blocks 352 CHAR_BLOCK1 = 176 353 CHAR_BLOCK2 = 177 354 CHAR_BLOCK3 = 178 355 # arrows 356 CHAR_ARROW_N = 24 357 CHAR_ARROW_S = 25 358 CHAR_ARROW_E = 26 359 CHAR_ARROW_W = 27 360 # arrows without tail 361 CHAR_ARROW2_N = 30 362 CHAR_ARROW2_S = 31 363 CHAR_ARROW2_E = 16 364 CHAR_ARROW2_W = 17 365 # double arrows 366 CHAR_DARROW_H = 29 367 CHAR_DARROW_V = 18 368 # GUI stuff 369 CHAR_CHECKBOX_UNSET = 224 370 CHAR_CHECKBOX_SET = 225 371 CHAR_RADIO_UNSET = 9 372 CHAR_RADIO_SET = 10 373 # sub-pixel resolution kit 374 CHAR_SUBP_NW = 226 375 CHAR_SUBP_NE = 227 376 CHAR_SUBP_N = 228 377 CHAR_SUBP_SE = 229 378 CHAR_SUBP_DIAG = 230 379 CHAR_SUBP_E = 231 380 CHAR_SUBP_SW = 232 381 # font flags 382 FONT_LAYOUT_ASCII_INCOL = 1 383 FONT_LAYOUT_ASCII_INROW = 2 384 FONT_TYPE_GREYSCALE = 4 385 FONT_TYPE_GRAYSCALE = 4 386 FONT_LAYOUT_TCOD = 8 387 # color control codes 388 COLCTRL_1=1 389 COLCTRL_2=2 390 COLCTRL_3=3 391 COLCTRL_4=4 392 COLCTRL_5=5 393 COLCTRL_NUMBER=5 394 COLCTRL_FORE_RGB=6 395 COLCTRL_BACK_RGB=7 396 COLCTRL_STOP=8 397 398 # initializing the console
399 -def console_init_root(w, h, title, fullscreen=False):
400 _lib.TCOD_console_init_root(w, h, title, c_uint(fullscreen))
401
402 -def console_get_width(con):
403 return _lib.TCOD_console_get_width(con)
404
405 -def console_get_height(con):
406 return _lib.TCOD_console_get_height(con)
407
408 -def console_set_custom_font(fontFile, flags=FONT_LAYOUT_ASCII_INCOL, nb_char_horiz=0, nb_char_vertic=0):
409 _lib.TCOD_console_set_custom_font(fontFile, flags, nb_char_horiz, nb_char_vertic)
410
411 -def console_map_ascii_code_to_font(asciiCode, fontCharX, fontCharY):
412 if type(asciiCode) == str: 413 _lib.TCOD_console_map_ascii_code_to_font(ord(asciiCode), fontCharX, 414 fontCharY) 415 else: 416 _lib.TCOD_console_map_ascii_code_to_font(asciiCode, fontCharX, 417 fontCharY)
418
419 -def console_map_ascii_codes_to_font(firstAsciiCode, nbCodes, fontCharX, 420 fontCharY):
421 if type(firstAsciiCode) == str: 422 _lib.TCOD_console_map_ascii_codes_to_font(ord(firstAsciiCode), nbCodes, 423 fontCharX, fontCharY) 424 else: 425 _lib.TCOD_console_map_ascii_codes_to_font(firstAsciiCode, nbCodes, 426 fontCharX, fontCharY)
427
428 -def console_map_string_to_font(s, fontCharX, fontCharY):
429 _lib.TCOD_console_map_string_to_font(s, fontCharX, fontCharY)
430
431 -def console_is_fullscreen():
432 return _lib.TCOD_console_is_fullscreen() == 1
433
434 -def console_set_fullscreen(fullscreen):
435 _lib.TCOD_console_set_fullscreen(c_int(fullscreen))
436
437 -def console_is_window_closed():
438 return _lib.TCOD_console_is_window_closed() == 1
439
440 -def console_set_window_title(title):
441 _lib.TCOD_console_set_window_title(title)
442
443 -def console_credits():
444 _lib.TCOD_console_credits()
445
446 -def console_credits_reset():
447 _lib.TCOD_console_credits_reset()
448
449 -def console_credits_render(x, y, alpha):
450 return _lib.TCOD_console_credits_render(x, y, c_int(alpha)) == 1
451
452 -def console_flush():
453 _lib.TCOD_console_flush()
454 455 # drawing on a console
456 -def console_set_background_color(con, col):
457 _lib.TCOD_console_set_background_color(con, col)
458
459 -def console_set_foreground_color(con, col):
460 _lib.TCOD_console_set_foreground_color(con, col)
461
462 -def console_clear(con):
463 return _lib.TCOD_console_clear(con)
464
465 -def console_put_char(con, x, y, c, flag=BKGND_SET):
466 if type(c) == str: 467 _lib.TCOD_console_put_char(con, x, y, ord(c), flag) 468 else: 469 _lib.TCOD_console_put_char(con, x, y, c, flag)
470
471 -def console_put_char_ex(con, x, y, c, fore, back):
472 if type(c) == str: 473 _lib.TCOD_console_put_char_ex(con, x, y, ord(c), fore, back) 474 else: 475 _lib.TCOD_console_put_char_ex(con, x, y, c, fore, back)
476
477 -def console_set_back(con, x, y, col, flag=BKGND_SET):
478 _lib.TCOD_console_set_back(con, x, y, col, flag)
479
480 -def console_set_fore(con, x, y, col):
481 _lib.TCOD_console_set_fore(con, x, y, col)
482
483 -def console_set_char(con, x, y, c):
484 if type(c) == str: 485 _lib.TCOD_console_set_char(con, x, y, ord(c)) 486 else: 487 _lib.TCOD_console_set_char(con, x, y, c)
488
489 -def console_print_left(con, x, y, bk, s):
490 _lib.TCOD_console_print_left(con, x, y, bk, s)
491
492 -def console_print_right(con, x, y, bk, s):
493 _lib.TCOD_console_print_right(con, x, y, bk, s)
494
495 -def console_print_center(con, x, y, bk, s):
496 _lib.TCOD_console_print_center(con, x, y, bk, s)
497
498 -def console_print_left_rect(con, x, y, w, h, bk, s):
499 return _lib.TCOD_console_print_left_rect(con, x, y, w, h, bk, s)
500
501 -def console_print_right_rect(con, x, y, w, h, bk, s):
502 return _lib.TCOD_console_print_right_rect(con, x, y, w, h, bk, s)
503
504 -def console_print_center_rect(con, x, y, w, h, bk, s):
505 return _lib.TCOD_console_print_center_rect(con, x, y, w, h, bk, s)
506
507 -def console_height_left_rect(con, x, y, w, h, s):
508 return _lib.TCOD_console_height_left_rect(con, x, y, w, h, s)
509
510 -def console_height_right_rect(con, x, y, w, h, s):
511 return _lib.TCOD_console_height_right_rect(con, x, y, w, h, s)
512
513 -def console_height_center_rect(con, x, y, w, h, s):
514 return _lib.TCOD_console_height_center_rect(con, x, y, w, h, s)
515
516 -def console_rect(con, x, y, w, h, clr, flag=BKGND_SET):
517 _lib.TCOD_console_rect(con, x, y, w, h, c_int(clr), flag)
518
519 -def console_hline(con, x, y, l):
520 _lib.TCOD_console_hline( con, x, y, l)
521
522 -def console_vline(con, x, y, l):
523 _lib.TCOD_console_vline( con, x, y, l)
524
525 -def console_print_frame(con, x, y, w, h, clr, bkflg, s):
526 _lib.TCOD_console_print_frame(con, x, y, w, h, c_int(clr), bkflg, s)
527
528 -def console_set_color_control(con,fore,back) :
529 _lib.TCOD_console_set_color_control(con,fore,back)
530
531 -def console_get_background_color(con):
532 iret=_lib.TCOD_console_get_background_color_wrapper(con) 533 return int_to_col(iret)
534
535 -def console_get_foreground_color(con):
536 iret=_lib.TCOD_console_get_foreground_color_wrapper(con) 537 return int_to_col(iret)
538
539 -def console_get_back(con, x, y):
540 iret=_lib.TCOD_console_get_back_wrapper(con, x, y) 541 return int_to_col(iret)
542
543 -def console_get_fore(con, x, y):
544 iret=_lib.TCOD_console_get_fore_wrapper(con, x, y) 545 return int_to_col(iret)
546
547 -def console_get_char(con, x, y):
548 return _lib.TCOD_console_get_char(con, x, y)
549
550 -def console_set_fade(fade, fadingColor):
551 _lib.TCOD_console_set_fade_wrapper(fade, col_to_int(fadingColor))
552
553 -def console_get_fade():
554 return _lib.TCOD_console_get_fade().value
555
556 -def console_get_fading_color():
557 return int_to_col(_lib.TCOD_console_get_fading_color_wrapper())
558 559 # handling keyboard input
560 -def console_wait_for_keypress(flush):
561 k=Key() 562 _lib.TCOD_console_wait_for_keypress_wrapper(byref(k),c_int(flush)) 563 return k
564
565 -def console_check_for_keypress(flags=KEY_RELEASED):
566 k=Key() 567 _lib.TCOD_console_check_for_keypress_wrapper(byref(k),c_int(flags)) 568 return k
569
570 -def console_is_key_pressed(key):
571 return _lib.TCOD_console_is_key_pressed(key) == 1
572
573 -def console_set_keyboard_repeat(initial_delay, interval):
574 _lib.TCOD_console_set_keyboard_repeat(initial_delay, interval)
575
576 -def console_disable_keyboard_repeat():
577 _lib.TCOD_console_disable_keyboard_repeat()
578 579 # using offscreen consoles
580 -def console_new(w, h):
581 return _lib.TCOD_console_new(w, h)
582
583 -def console_get_width(con):
584 return _lib.TCOD_console_get_width(con)
585
586 -def console_get_height(con):
587 return _lib.TCOD_console_get_height(con)
588
589 -def console_blit(src, x, y, w, h, dst, xdst, ydst, ffade=1.0,bfade=1.0):
590 _lib.TCOD_console_blit(src, x, y, w, h, dst, xdst, ydst, c_float(ffade), c_float(bfade))
591
592 -def console_set_key_color(con, col):
593 _lib.TCOD_console_set_key_color(con, col)
594
595 -def console_delete(con):
596 _lib.TCOD_console_delete(con)
597 598 # fast color filling
599 -def console_fill_foreground(con,r,g,b) :
600 if (numpy_available and isinstance(r, numpy.ndarray) and 601 isinstance(g, numpy.ndarray) and isinstance(b, numpy.ndarray)): 602 #numpy arrays, use numpy's ctypes functions 603 r = numpy.ascontiguousarray(r, dtype=numpy.int_) 604 g = numpy.ascontiguousarray(g, dtype=numpy.int_) 605 b = numpy.ascontiguousarray(b, dtype=numpy.int_) 606 cr = r.ctypes.data_as(ctypes.POINTER(ctypes.c_int)) 607 cg = g.ctypes.data_as(ctypes.POINTER(ctypes.c_int)) 608 cb = b.ctypes.data_as(ctypes.POINTER(ctypes.c_int)) 609 610 elif (isinstance(r, list) and isinstance(g, list) and isinstance(b, list)): 611 #simple python lists, convert using ctypes 612 cr = (c_int * len(r))(*r) 613 cg = (c_int * len(g))(*g) 614 cb = (c_int * len(b))(*b) 615 else: 616 raise TypeError('R, G and B must all be of the same type (list or NumPy array)') 617 618 if len(r) != len(g) or len(r) != len(b): 619 raise TypeError('R, G and B must all have the same size.') 620 621 _lib.TCOD_console_fill_foreground(con, cr, cg, cb)
622
623 -def console_fill_background(con,r,g,b) :
624 if (numpy_available and isinstance(r, numpy.ndarray) and 625 isinstance(g, numpy.ndarray) and isinstance(b, numpy.ndarray)): 626 #numpy arrays, use numpy's ctypes functions 627 628 r = numpy.ascontiguousarray(r, dtype=numpy.int_) 629 g = numpy.ascontiguousarray(g, dtype=numpy.int_) 630 b = numpy.ascontiguousarray(b, dtype=numpy.int_) 631 cr = r.ctypes.data_as(ctypes.POINTER(ctypes.c_int)) 632 cg = g.ctypes.data_as(ctypes.POINTER(ctypes.c_int)) 633 cb = b.ctypes.data_as(ctypes.POINTER(ctypes.c_int)) 634 635 elif (isinstance(r, list) and isinstance(g, list) and isinstance(b, list)): 636 #simple python lists, convert using ctypes 637 cr = (c_int * len(r))(*r) 638 cg = (c_int * len(g))(*g) 639 cb = (c_int * len(b))(*b) 640 else: 641 raise TypeError('R, G and B must all be of the same type (list or NumPy array)') 642 643 if len(r) != len(g) or len(r) != len(b): 644 raise TypeError('R, G and B must all have the same size.') 645 646 _lib.TCOD_console_fill_background(con, cr, cg, cb)
647 648 ############################ 649 # sys module 650 ############################ 651 _lib.TCOD_sys_get_last_frame_length.restype = c_float 652 _lib.TCOD_sys_elapsed_seconds.restype = c_float 653 654 # high precision time functions
655 -def sys_set_fps(fps):
656 _lib.TCOD_sys_set_fps(fps)
657
658 -def sys_get_fps():
659 return _lib.TCOD_sys_get_fps()
660
661 -def sys_get_last_frame_length():
662 return _lib.TCOD_sys_get_last_frame_length()
663
664 -def sys_sleep_milli(val):
665 _lib.TCOD_sys_sleep_milli(c_uint(val))
666
667 -def sys_update_char(ascii,img,x,y):
668 _lib.TCOD_sys_update_char(c_uint(ascii),img,c_int(x),c_int(y))
669
670 -def sys_elapsed_milli():
671 return _lib.TCOD_sys_elapsed_milli()
672
673 -def sys_elapsed_seconds():
674 return _lib.TCOD_sys_elapsed_seconds()
675 676 # easy screenshots
677 -def sys_save_screenshot(name=0):
678 _lib.TCOD_sys_save_screenshot(name)
679 680 # custom fullscreen resolution
681 -def sys_force_fullscreen_resolution(width, height):
682 _lib.TCOD_sys_force_fullscreen_resolution(width, height)
683
684 -def sys_get_current_resolution():
685 w = c_int() 686 h = c_int() 687 _lib.TCOD_sys_get_current_resolution(byref(w), byref(h)) 688 return w.value, h.value
689
690 -def sys_get_char_size():
691 w = c_int() 692 h = c_int() 693 _lib.TCOD_sys_get_char_size(byref(w), byref(h)) 694 return w.value, h.value
695 696 # update font bitmap
697 -def sys_update_char(asciiCode, fontx, fonty, img, x, y) :
698 _lib.TCOD_sys_update_char(c_int(asciiCode),c_int(fontx),c_int(fonty),img,c_int(x),c_int(y))
699 700 # custom SDL post renderer 701 SDL_RENDERER_FUNC = None 702 sdl_renderer_func = None
703 -def sys_register_SDL_renderer(func):
704 global SDL_RENDERER_FUNC 705 global sdl_renderer_func 706 SDL_RENDERER_FUNC = CFUNCTYPE(None, c_void_p) 707 sdl_renderer_func = SDL_RENDERER_FUNC(func) 708 _lib.TCOD_sys_register_SDL_renderer(sdl_renderer_func)
709 710 ############################ 711 # line module 712 ############################ 713 _lib.TCOD_line_step.restype = c_uint 714 _lib.TCOD_line.restype=c_uint 715
716 -def line_init(xo, yo, xd, yd):
717 _lib.TCOD_line_init(xo, yo, xd, yd)
718
719 -def line_step():
720 x = c_int() 721 y = c_int() 722 ret = _lib.TCOD_line_step(byref(x), byref(y)) 723 if not ret: 724 return x.value, y.value 725 return None,None
726
727 -def line(xo,yo,xd,yd,py_callback) :
728 LINE_CBK_FUNC=CFUNCTYPE(c_uint,c_int,c_int) 729 def int_callback(x,y) : 730 if py_callback(x,y) : 731 return 1 732 return 0
733 c_callback=LINE_CBK_FUNC(int_callback) 734 return _lib.TCOD_line(xo,yo,xd,yd,c_callback) == 1 735 736 ############################ 737 # image module 738 ############################ 739 _lib.TCOD_image_is_pixel_transparent.restype = c_uint 740
741 -def image_new(width, height):
742 return _lib.TCOD_image_new(width, height)
743
744 -def image_clear(image,col) :
745 _lib.TCOD_image_clear(image,col)
746
747 -def image_invert(image) :
748 _lib.TCOD_image_invert(image)
749
750 -def image_hflip(image) :
751 _lib.TCOD_image_hflip(image)
752
753 -def image_vflip(image) :
754 _lib.TCOD_image_vflip(image)
755
756 -def image_scale(image, neww, newh) :
757 _lib.TCOD_image_scale(image,c_int(neww),c_int(newh))
758
759 -def image_set_key_color(image,col) :
760 _lib.TCOD_image_set_key_color(image,col)
761
762 -def image_get_alpha(image,x,y) :
763 return _lib.TCOD_image_get_alpha(image,c_int(x),c_int(y))
764
765 -def image_is_pixel_transparent(image,x,y) :
766 return _lib.TCOD_image_is_pixel_transparent(image,c_int(x),c_int(y)) == 1
767
768 -def image_load(filename):
769 return _lib.TCOD_image_load(filename)
770
771 -def image_from_console(console):
772 return _lib.TCOD_image_from_console(console)
773
774 -def image_refresh_console(image, console):
775 _lib.TCOD_image_refresh_console(image, console)
776
777 -def image_get_size(image):
778 w=c_int() 779 h=c_int() 780 _lib.TCOD_image_get_size(image, byref(w), byref(h)) 781 return w.value, h.value
782
783 -def image_get_pixel(image, x, y):
784 return int_to_col(_lib.TCOD_image_get_pixel_wrapper(image, x, y))
785
786 -def image_get_mipmap_pixel(image, x0, y0, x1, y1):
787 return int_to_col(_lib.TCOD_image_get_mipmap_pixel_wrapper(image, c_float(x0), c_float(y0), 788 c_float(x1), c_float(y1)))
789 -def image_put_pixel(image, x, y, col):
790 _lib.TCOD_image_put_pixel_wrapper(image, x, y, col_to_int(col))
791
792 -def image_blit(image, console, x, y, bkgnd_flag, scalex, scaley, angle):
793 _lib.TCOD_image_blit(image, console, c_float(x), c_float(y), bkgnd_flag, 794 c_float(scalex), c_float(scaley), c_float(angle))
795
796 -def image_blit_rect(image, console, x, y, w, h, bkgnd_flag):
797 _lib.TCOD_image_blit_rect(image, console, x, y, w, h, bkgnd_flag)
798
799 -def image_blit_2x(image, console, dx, dy, sx=0, sy=0, w=-1, h=-1):
800 _lib.TCOD_image_blit_2x(image, console, dx,dy,sx,sy,w,h)
801
802 -def image_save(image, filename):
803 _lib.TCOD_image_save(image, filename)
804
805 -def image_delete(image):
806 _lib.TCOD_image_delete(image)
807 808 ############################ 809 # mouse module 810 ############################
811 -class Mouse(Structure):
812 _fields_=[('x', c_int, 32), 813 ('y', c_int, 32), 814 ('dx', c_int, 32), 815 ('dy', c_int, 32), 816 ('cx', c_int, 32), 817 ('cy', c_int, 32), 818 ('dcx', c_int, 32), 819 ('dcy', c_int, 32), 820 ('lbutton', c_uint, 8), 821 ('rbutton', c_uint, 8), 822 ('mbutton', c_uint, 8), 823 ('lbutton_pressed', c_uint, 8), 824 ('rbutton_pressed', c_uint, 8), 825 ('mbutton_pressed', c_uint, 8), 826 ('wheel_up', c_uint, 8), 827 ('wheel_down', c_uint, 8), 828 ]
829 830 _lib.TCOD_mouse_is_cursor_visible.restype = c_uint 831
832 -def mouse_show_cursor(visible):
833 _lib.TCOD_mouse_show_cursor(c_int(visible))
834
835 -def mouse_is_cursor_visible():
836 return _lib.TCOD_mouse_is_cursor_visible() == 1
837
838 -def mouse_move(x, y):
839 _lib.TCOD_mouse_move(x, y)
840
841 -def mouse_get_status():
842 m = Mouse() 843 _lib.TCOD_mouse_get_status_wrapper(byref(m)) 844 return m
845 846 ############################ 847 # parser module 848 ############################ 849 _lib.TCOD_struct_get_name.restype = c_char_p 850 _lib.TCOD_struct_is_mandatory.restype = c_uint 851 _lib.TCOD_parser_get_bool_property.restype = c_uint 852 _lib.TCOD_parser_get_float_property.restype = c_float 853 _lib.TCOD_parser_get_string_property.restype = c_char_p 854
855 -class Dice(Structure):
856 _fields_=[('nb_dices', c_int), 857 ('nb_faces', c_int), 858 ('multiplier', c_float), 859 ('addsub', c_float), 860 ]
861
862 -class _CValue(Union):
863 _fields_=[('c',c_uint), 864 ('i',c_int), 865 ('f',c_float), 866 ('s',c_char_p), 867 ('col',Color), 868 ('dice',Dice), 869 ('custom',c_void_p), 870 ]
871 872 _CFUNC_NEW_STRUCT = CFUNCTYPE(c_uint, c_void_p, c_char_p) 873 _CFUNC_NEW_FLAG = CFUNCTYPE(c_uint, c_char_p) 874 _CFUNC_NEW_PROPERTY = CFUNCTYPE(c_uint, c_char_p, c_int, _CValue) 875
876 -class _CParserListener(Structure):
877 _fields_=[('new_struct', _CFUNC_NEW_STRUCT), 878 ('new_flag',_CFUNC_NEW_FLAG), 879 ('new_property',_CFUNC_NEW_PROPERTY), 880 ('end_struct',_CFUNC_NEW_STRUCT), 881 ('error',_CFUNC_NEW_FLAG), 882 ]
883 884 # property types 885 TYPE_NONE = 0 886 TYPE_BOOL = 1 887 TYPE_CHAR = 2 888 TYPE_INT = 3 889 TYPE_FLOAT = 4 890 TYPE_STRING = 5 891 TYPE_COLOR = 6 892 TYPE_DICE = 7 893 TYPE_VALUELIST00 = 8 894 TYPE_VALUELIST01 = 9 895 TYPE_VALUELIST02 = 10 896 TYPE_VALUELIST03 = 11 897 TYPE_VALUELIST04 = 12 898 TYPE_VALUELIST05 = 13 899 TYPE_VALUELIST06 = 14 900 TYPE_VALUELIST07 = 15 901 TYPE_VALUELIST08 = 16 902 TYPE_VALUELIST09 = 17 903 TYPE_VALUELIST10 = 18 904 TYPE_VALUELIST11 = 19 905 TYPE_VALUELIST12 = 20 906 TYPE_VALUELIST13 = 21 907 TYPE_VALUELIST14 = 22 908 TYPE_VALUELIST15 = 23 909 TYPE_LIST = 1024 910
911 -def parser_new():
912 return _lib.TCOD_parser_new()
913
914 -def parser_new_struct(parser, name):
915 return _lib.TCOD_parser_new_struct(parser, name)
916
917 -def struct_add_flag(struct, name):
918 _lib.TCOD_struct_add_flag(struct, name)
919
920 -def struct_add_property(struct, name, typ, mandatory):
921 _lib.TCOD_struct_add_property(struct, name, typ, c_int(mandatory))
922
923 -def struct_add_value_list(struct, name, value_list, mandatory):
924 CARRAY = c_char_p * (len(value_list) + 1) 925 cvalue_list = CARRAY() 926 for i in range(len(value_list)): 927 cvalue_list[i] = cast(value_list[i], c_char_p) 928 cvalue_list[len(value_list)] = 0 929 _lib.TCOD_struct_add_value_list(struct, name, cvalue_list, c_int(mandatory))
930
931 -def struct_add_list_property(struct, name, typ, mandatory):
932 _lib.TCOD_struct_add_list_property(struct, name, typ, c_int(mandatory))
933
934 -def struct_add_structure(struct, sub_struct):
935 _lib.TCOD_struct_add_structure(struct, sub_struct)
936
937 -def struct_get_name(struct):
938 return _lib.TCOD_struct_get_name(struct)
939
940 -def struct_is_mandatory(struct, name):
941 return _lib.TCOD_struct_is_mandatory(struct, name) == 1
942
943 -def struct_get_type(struct, name):
944 return _lib.TCOD_struct_get_type(struct, name)
945
946 -def parser_run(parser, filename, listener=0):
947 if listener != 0: 948 clistener=_CParserListener() 949 def value_converter(name, typ, value): 950 if typ == TYPE_BOOL: 951 return listener.new_property(name, typ, value.c == 1) 952 elif typ == TYPE_CHAR: 953 return listener.new_property(name, typ, '%c' % (value.c & 0xFF)) 954 elif typ == TYPE_INT: 955 return listener.new_property(name, typ, value.i) 956 elif typ == TYPE_FLOAT: 957 return listener.new_property(name, typ, value.f) 958 elif typ == TYPE_STRING or \ 959 TYPE_VALUELIST15 >= typ >= TYPE_VALUELIST00: 960 return listener.new_property(name, typ, value.s) 961 elif typ == TYPE_COLOR: 962 return listener.new_property(name, typ, value.col) 963 elif typ == TYPE_DICE: 964 return listener.new_property(name, typ, value.dice) 965 return True
966 clistener.new_struct = _CFUNC_NEW_STRUCT(listener.new_struct) 967 clistener.new_flag = _CFUNC_NEW_FLAG(listener.new_flag) 968 clistener.new_property = _CFUNC_NEW_PROPERTY(value_converter) 969 clistener.end_struct = _CFUNC_NEW_STRUCT(listener.end_struct) 970 clistener.error = _CFUNC_NEW_FLAG(listener.error) 971 _lib.TCOD_parser_run(parser, filename, byref(clistener)) 972 else: 973 _lib.TCOD_parser_run(parser, filename, 0) 974
975 -def parser_delete(parser):
976 _lib.TCOD_parser_delete(parser)
977
978 -def parser_get_bool_property(parser, name):
979 return _lib.TCOD_parser_get_bool_property(parser, name) == 1
980
981 -def parser_get_int_property(parser, name):
982 return _lib.TCOD_parser_get_int_property(parser, name)
983
984 -def parser_get_char_property(parser, name):
985 return '%c' % _lib.TCOD_parser_get_char_property(parser, name)
986
987 -def parser_get_float_property(parser, name):
988 return _lib.TCOD_parser_get_float_property(parser, name)
989
990 -def parser_get_string_property(parser, name):
991 return _lib.TCOD_parser_get_string_property(parser, name)
992
993 -def parser_get_color_property(parser, name):
994 iret=_lib.TCOD_parser_get_color_property_wrapper(parser, name) 995 return int_to_col(iret)
996
997 -def parser_get_dice_property(parser, name):
998 d = Dice() 999 _lib.TCOD_parser_get_dice_property_py(parser, name, byref(d)) 1000 return d
1001
1002 -def parser_get_list_property(parser, name):
1003 clist = _lib.TCOD_parser_get_list_property(parser, name) 1004 res = list() 1005 for i in range(_lib.TCOD_list_size(clist)): 1006 elt = _lib.TCOD_list_get(clist, i) 1007 res.append(elt) 1008 return res
1009 1010 ############################ 1011 # random module 1012 ############################ 1013 _lib.TCOD_random_get_float.restype = c_float 1014 _lib.TCOD_random_get_gaussian_float.restype = c_float 1015 RNG_MT = 0 1016 RNG_CMWC = 1 1017
1018 -def random_get_instance():
1019 return _lib.TCOD_random_get_instance()
1020
1021 -def random_new(algo=RNG_CMWC):
1022 return _lib.TCOD_random_new(algo)
1023
1024 -def random_new_from_seed(seed, algo=RNG_CMWC):
1025 return _lib.TCOD_random_new_from_seed(algo,c_uint(seed))
1026
1027 -def random_get_int(rnd, mi, ma):
1028 return _lib.TCOD_random_get_int(rnd, mi, ma)
1029
1030 -def random_get_float(rnd, mi, ma):
1031 return _lib.TCOD_random_get_float(rnd, c_float(mi), c_float(ma))
1032
1033 -def random_get_gaussian_float(rnd, mi, ma):
1034 return _lib.TCOD_random_get_gaussian_float(rnd, c_float(mi), c_float(ma))
1035
1036 -def random_get_gaussian_int(rnd, mi, ma):
1037 return _lib.TCOD_random_get_gaussian_int(rnd, mi, ma)
1038
1039 -def random_save(rnd):
1040 return _lib.TCOD_random_save(rnd)
1041
1042 -def random_restore(rnd, backup):
1043 _lib.TCOD_random_restore(rnd, backup)
1044
1045 -def random_delete(rnd):
1046 _lib.TCOD_random_delete(rnd)
1047 1048 ############################ 1049 # noise module 1050 ############################ 1051 _lib.TCOD_noise_perlin.restype = c_float 1052 _lib.TCOD_noise_simplex.restype = c_float 1053 _lib.TCOD_noise_wavelet.restype = c_float 1054 _lib.TCOD_noise_fbm_perlin.restype = c_float 1055 _lib.TCOD_noise_fbm_simplex.restype = c_float 1056 _lib.TCOD_noise_fbm_wavelet.restype = c_float 1057 _lib.TCOD_noise_turbulence_perlin.restype = c_float 1058 _lib.TCOD_noise_turbulence_simplex.restype = c_float 1059 _lib.TCOD_noise_turbulence_wavelet.restype = c_float 1060 1061 NOISE_DEFAULT_HURST = 0.5 1062 NOISE_DEFAULT_LACUNARITY = 2.0 1063
1064 -def noise_new(dim, h=NOISE_DEFAULT_HURST, l=NOISE_DEFAULT_LACUNARITY, rnd=0):
1065 return _lib.TCOD_noise_new(dim, c_float(h), c_float(l), rnd)
1066
1067 -def _noise_int(n, f, func):
1068 ct = c_float * len(f) 1069 cf = ct() 1070 i = 0 1071 for value in f: 1072 cf[i] = c_float(value) 1073 i += 1 1074 return func(n, cf)
1075
1076 -def noise_perlin(n, f):
1077 return _noise_int(n, f, _lib.TCOD_noise_perlin)
1078
1079 -def noise_simplex(n, f):
1080 return _noise_int(n, f, _lib.TCOD_noise_simplex)
1081
1082 -def noise_wavelet(n, f):
1083 return _noise_int(n, f, _lib.TCOD_noise_wavelet)
1084
1085 -def _noise_int2(n, f, oc, func):
1086 ct = c_float * len(f) 1087 cf = ct() 1088 i = 0 1089 for value in f: 1090 cf[i] = c_float(value) 1091 i += 1 1092 return func(n, cf, c_float(oc))
1093
1094 -def noise_fbm_perlin(n, f, oc):
1095 return _noise_int2(n, f, oc, _lib.TCOD_noise_fbm_perlin)
1096
1097 -def noise_fbm_simplex(n, f, oc):
1098 return _noise_int2(n, f, oc, _lib.TCOD_noise_fbm_simplex)
1099
1100 -def noise_fbm_wavelet(n, f, oc):
1101 return _noise_int2(n, f, oc, _lib.TCOD_noise_fbm_wavelet)
1102
1103 -def noise_turbulence_perlin(n, f, oc):
1104 return _noise_int2(n, f, oc, _lib.TCOD_noise_turbulence_perlin)
1105
1106 -def noise_turbulence_simplex(n, f, oc):
1107 return _noise_int2(n, f, oc, _lib.TCOD_noise_turbulence_simplex)
1108
1109 -def noise_turbulence_wavelet(n, f, oc):
1110 return _noise_int2(n, f, oc, _lib.TCOD_noise_turbulence_wavelet)
1111
1112 -def noise_delete(n):
1113 _lib.TCOD_noise_delete(n)
1114 1115 ############################ 1116 # fov module 1117 ############################ 1118 FOV_BASIC = 0 1119 FOV_DIAMOND = 1 1120 FOV_SHADOW = 2 1121 FOV_PERMISSIVE_0 = 3 1122 FOV_PERMISSIVE_1 = 4 1123 FOV_PERMISSIVE_2 = 5 1124 FOV_PERMISSIVE_3 = 6 1125 FOV_PERMISSIVE_4 = 7 1126 FOV_PERMISSIVE_5 = 8 1127 FOV_PERMISSIVE_6 = 9 1128 FOV_PERMISSIVE_7 = 10 1129 FOV_PERMISSIVE_8 = 11 1130 FOV_RESTRICTIVE = 12 1131 NB_FOV_ALGORITHMS = 13 1132
1133 -def FOV_PERMISSIVE(p) :
1134 return FOV_PERMISSIVE_0+p
1135
1136 -def map_new(w, h):
1137 return _lib.TCOD_map_new(w, h)
1138
1139 -def map_copy(source, dest):
1140 return _lib.TCOD_map_copy(source, dest)
1141
1142 -def map_set_properties(m, x, y, isTrans, isWalk):
1143 _lib.TCOD_map_set_properties(m, x, y, c_int(isTrans), c_int(isWalk))
1144
1145 -def map_clear(m):
1146 _lib.TCOD_map_clear(m)
1147
1148 -def map_compute_fov(m, x, y, radius=0, light_walls=True, algo=FOV_RESTRICTIVE ):
1149 _lib.TCOD_map_compute_fov(m, x, y, c_int(radius), c_uint(light_walls), c_int(algo))
1150
1151 -def map_is_in_fov(m, x, y):
1152 return _lib.TCOD_map_is_in_fov(m, x, y) == 1
1153
1154 -def map_is_transparent(m, x, y):
1155 return _lib.TCOD_map_is_transparent(m, x, y) == 1
1156
1157 -def map_is_walkable(m, x, y):
1158 return _lib.TCOD_map_is_walkable(m, x, y) == 1
1159
1160 -def map_delete(m):
1161 return _lib.TCOD_map_delete(m)
1162 1163 ############################ 1164 # pathfinding module 1165 ############################ 1166 _lib.TCOD_path_compute.restype = c_uint 1167 _lib.TCOD_path_is_empty.restype = c_uint 1168 _lib.TCOD_path_walk.restype = c_uint 1169
1170 -def path_new_using_map(m, dcost=1.41):
1171 return _lib.TCOD_path_new_using_map(c_void_p(m), c_float(dcost))
1172 1173 PATH_CBK_FUNC = None 1174 cbk_func = None
1175 -def path_new_using_function(w, h, func, userdata=0, dcost=1.41):
1176 global PATH_CBK_FUNC 1177 global cbk_func 1178 PATH_CBK_FUNC = CFUNCTYPE(c_float, c_int, c_int, c_int, c_int, py_object) 1179 cbk_func = PATH_CBK_FUNC(func) 1180 return _lib.TCOD_path_new_using_function(w, h, cbk_func, 1181 py_object(userdata), c_float(dcost))
1182
1183 -def path_compute(p, ox, oy, dx, dy):
1184 return _lib.TCOD_path_compute(p, ox, oy, dx, dy) == 1
1185
1186 -def path_get_origin(p):
1187 x = c_int() 1188 y = c_int() 1189 _lib.TCOD_path_get_origin(p, byref(x), byref(y)) 1190 return x.value, y.value
1191
1192 -def path_get_destination(p):
1193 x = c_int() 1194 y = c_int() 1195 _lib.TCOD_path_get_destination(p, byref(x), byref(y)) 1196 return x.value, y.value
1197
1198 -def path_size(p):
1199 return _lib.TCOD_path_size(p)
1200
1201 -def path_get(p, idx):
1202 x = c_int() 1203 y = c_int() 1204 _lib.TCOD_path_get(p, idx, byref(x), byref(y)) 1205 return x.value, y.value
1206
1207 -def path_is_empty(p):
1208 return _lib.TCOD_path_is_empty(p) == 1
1209
1210 -def path_walk(p, recompute):
1211 x = c_int() 1212 y = c_int() 1213 if _lib.TCOD_path_walk(p, byref(x), byref(y), c_int(recompute)): 1214 return x.value, y.value 1215 return None,None
1216
1217 -def path_delete(p):
1218 _lib.TCOD_path_delete(p)
1219 1220 _lib.TCOD_dijkstra_path_set.restype = c_uint 1221 _lib.TCOD_dijkstra_is_empty.restype = c_uint 1222 _lib.TCOD_dijkstra_size.restype = c_int 1223 _lib.TCOD_dijkstra_path_walk.restype = c_uint 1224 _lib.TCOD_dijkstra_get_distance.restype = c_float 1225
1226 -def dijkstra_new(m, dcost=1.41):
1227 return _lib.TCOD_dijkstra_new(c_void_p(m), c_float(dcost))
1228 1229 PATH_CBK_FUNC = None 1230 cbk_func = None
1231 -def dijkstra_new_using_function(w, h, func, userdata=0, dcost=1.41):
1232 global PATH_CBK_FUNC 1233 global cbk_func 1234 PATH_CBK_FUNC = CFUNCTYPE(c_float, c_int, c_int, c_int, c_int, py_object) 1235 cbk_func = PATH_CBK_FUNC(func) 1236 return _lib.TCOD_path_dijkstra_using_function(w, h, cbk_func, 1237 py_object(userdata), c_float(dcost))
1238
1239 -def dijkstra_compute(p, ox, oy):
1240 _lib.TCOD_dijkstra_compute(p, c_int(ox), c_int(oy))
1241
1242 -def dijkstra_path_set(p, x, y):
1243 return _lib.TCOD_dijkstra_path_set(p, c_int(x), c_int(y))
1244
1245 -def dijkstra_get_distance(p, x, y):
1246 return _lib.TCOD_dijkstra_get_distance(p, c_int(x), c_int(y))
1247
1248 -def dijkstra_size(p):
1249 return _lib.TCOD_dijkstra_size(p)
1250
1251 -def dijkstra_get(p, idx):
1252 x = c_int() 1253 y = c_int() 1254 _lib.TCOD_dijkstra_get(p, c_int(idx), byref(x), byref(y)) 1255 return x.value, y.value
1256
1257 -def dijkstra_is_empty(p):
1258 return _lib.TCOD_dijkstra_is_empty(p) == 1
1259
1260 -def dijkstra_path_walk(p):
1261 x = c_int() 1262 y = c_int() 1263 if _lib.TCOD_dijkstra_path_walk(p, byref(x), byref(y)): 1264 return x.value, y.value 1265 return None,None
1266
1267 -def dijkstra_delete(p):
1268 _lib.TCOD_dijkstra_delete(p)
1269 1270 ############################ 1271 # bsp module 1272 ############################
1273 -class _CBsp(Structure):
1274 _fields_ = [('next', c_int, 32), 1275 ('father', c_int, 32), 1276 ('son', c_int, 32), 1277 ('x', c_int, 32), 1278 ('y', c_int, 32), 1279 ('w', c_int, 32), 1280 ('h', c_int, 32), 1281 ('position', c_int, 32), 1282 ('level', c_uint, 32), 1283 ('horizontal', c_uint, 32), 1284 ]
1285 1286 _lib.TCOD_bsp_new_with_size.restype = POINTER(_CBsp) 1287 _lib.TCOD_bsp_left.restype = POINTER(_CBsp) 1288 _lib.TCOD_bsp_right.restype = POINTER(_CBsp) 1289 _lib.TCOD_bsp_father.restype = POINTER(_CBsp) 1290 _lib.TCOD_bsp_is_leaf.restype = c_uint 1291 _lib.TCOD_bsp_contains.restype = c_uint 1292 _lib.TCOD_bsp_find_node.restype = POINTER(_CBsp) 1293 1294 # python class encapsulating the _CBsp pointer
1295 -class Bsp(object):
1296 - def __init__(self, cnode):
1297 pcbsp = cast(cnode, POINTER(_CBsp)) 1298 self.p = pcbsp
1299
1300 - def getx(self):
1301 return self.p.contents.x
1302 - def setx(self, value):
1303 self.p.contents.x = value
1304 x = property(getx, setx) 1305
1306 - def gety(self):
1307 return self.p.contents.y
1308 - def sety(self, value):
1309 self.p.contents.y = value
1310 y = property(gety, sety) 1311
1312 - def getw(self):
1313 return self.p.contents.w
1314 - def setw(self, value):
1315 self.p.contents.w = value
1316 w = property(getw, setw) 1317
1318 - def geth(self):
1319 return self.p.contents.h
1320 - def seth(self, value):
1321 self.p.contents.h = value
1322 h = property(geth, seth) 1323
1324 - def getpos(self):
1325 return self.p.contents.position
1326 - def setpos(self, value):
1327 self.p.contents.position = value
1328 position = property(getpos, setpos) 1329
1330 - def gethor(self):
1331 return self.p.contents.horizontal == 1
1332 - def sethor(self,value):
1333 self.p.contents.horizontal = c_int(value)
1334 horizontal = property(gethor, sethor) 1335
1336 - def getlev(self):
1337 return self.p.contents.level
1338 - def setlev(self,value):
1339 self.p.contents.level = value
1340 level = property(getlev, setlev)
1341 1342
1343 -def bsp_new_with_size(x, y, w, h):
1344 return Bsp(_lib.TCOD_bsp_new_with_size(x, y, w, h))
1345
1346 -def bsp_split_once(node, horizontal, position):
1347 _lib.TCOD_bsp_split_once(node.p, c_int(horizontal), position)
1348
1349 -def bsp_split_recursive(node, randomizer, nb, minHSize, minVSize, maxHRatio, 1350 maxVRatio):
1351 _lib.TCOD_bsp_split_recursive(node.p, randomizer, nb, minHSize, minVSize, 1352 c_float(maxHRatio), c_float(maxVRatio))
1353
1354 -def bsp_resize(node, x, y, w, h):
1355 _lib.TCOD_bsp_resize(node.p, x, y, w, h)
1356
1357 -def bsp_left(node):
1358 return Bsp(_lib.TCOD_bsp_left(node.p))
1359
1360 -def bsp_right(node):
1361 return Bsp(_lib.TCOD_bsp_right(node.p))
1362
1363 -def bsp_father(node):
1364 return Bsp(_lib.TCOD_bsp_father(node.p))
1365
1366 -def bsp_is_leaf(node):
1367 return _lib.TCOD_bsp_is_leaf(node.p) == 1
1368
1369 -def bsp_contains(node, cx, cy):
1370 return _lib.TCOD_bsp_contains(node.p, cx, cy) == 1
1371
1372 -def bsp_find_node(node, cx, cy):
1373 return Bsp(_lib.TCOD_bsp_find_node(node.p, cx, cy))
1374
1375 -def _bsp_traverse(node, callback, userData, func):
1376 BSP_CBK_FUNC = CFUNCTYPE(c_int, c_void_p, c_void_p) 1377 # convert the c node into a python node 1378 #before passing it to the actual callback 1379 def node_converter(cnode, data): 1380 node = Bsp(cnode) 1381 return callback(node, data)
1382 cbk_func = BSP_CBK_FUNC(node_converter) 1383 func(node.p, cbk_func, userData) 1384
1385 -def bsp_traverse_pre_order(node, callback, userData=0):
1386 _bsp_traverse(node, callback, userData, _lib.TCOD_bsp_traverse_pre_order)
1387
1388 -def bsp_traverse_in_order(node, callback, userData=0):
1389 _bsp_traverse(node, callback, userData, _lib.TCOD_bsp_traverse_in_order)
1390
1391 -def bsp_traverse_post_order(node, callback, userData=0):
1392 _bsp_traverse(node, callback, userData, _lib.TCOD_bsp_traverse_post_order)
1393
1394 -def bsp_traverse_level_order(node, callback, userData=0):
1395 _bsp_traverse(node, callback, userData, _lib.TCOD_bsp_traverse_level_order)
1396
1397 -def bsp_traverse_inverted_level_order(node, callback, userData=0):
1398 _bsp_traverse(node, callback, userData, 1399 _lib.TCOD_bsp_traverse_inverted_level_order)
1400
1401 -def bsp_remove_sons(node):
1402 _lib.TCOD_bsp_remove_sons(node.p)
1403
1404 -def bsp_delete(node):
1405 _lib.TCOD_bsp_delete(node.p)
1406 1407 ############################ 1408 # heightmap module 1409 ############################
1410 -class _CHeightMap(Structure):
1411 _fields_=[('w', c_int), 1412 ('h', c_int), 1413 ('values', POINTER(c_float)), 1414 ]
1415 1416 _lib.TCOD_heightmap_new.restype = POINTER(_CHeightMap) 1417 _lib.TCOD_heightmap_get_value.restype = c_float 1418 _lib.TCOD_heightmap_has_land_on_border.restype = c_uint 1419
1420 -class HeightMap(object):
1421 - def __init__(self, chm):
1422 pchm = cast(chm, POINTER(_CHeightMap)) 1423 self.p = pchm
1424
1425 - def getw(self):
1426 return self.p.contents.w
1427 - def setw(self, value):
1428 self.p.contents.w = value
1429 w = property(getw, setw) 1430
1431 - def geth(self):
1432 return self.p.contents.h
1433 - def seth(self, value):
1434 self.p.contents.h = value
1435 h = property(geth, seth)
1436
1437 -def heightmap_new(w, h):
1438 phm = _lib.TCOD_heightmap_new(w, h) 1439 return HeightMap(phm)
1440
1441 -def heightmap_set_value(hm, x, y, value):
1442 _lib.TCOD_heightmap_set_value(hm.p, x, y, c_float(value))
1443
1444 -def heightmap_add(hm, value):
1445 _lib.TCOD_heightmap_add(hm.p, c_float(value))
1446
1447 -def heightmap_scale(hm, value):
1448 _lib.TCOD_heightmap_scale(hm.p, c_float(value))
1449
1450 -def heightmap_clear(hm):
1451 _lib.TCOD_heightmap_clear(hm.p)
1452
1453 -def heightmap_clamp(hm, mi, ma):
1454 _lib.TCOD_heightmap_clamp(hm.p, c_float(mi),c_float(ma))
1455
1456 -def heightmap_copy(hm1, hm2):
1457 _lib.TCOD_heightmap_copy(hm1.p, hm2.p)
1458
1459 -def heightmap_normalize(hm, mi=0.0, ma=1.0):
1460 _lib.TCOD_heightmap_normalize(hm.p, c_float(mi), c_float(ma))
1461
1462 -def heightmap_lerp_hm(hm1, hm2, hm3, coef):
1463 _lib.TCOD_heightmap_lerp_hm(hm1.p, hm2.p, hm3.p, c_float(coef))
1464
1465 -def heightmap_add_hm(hm1, hm2, hm3):
1466 _lib.TCOD_heightmap_add_hm(hm1.p, hm2.p, hm3.p)
1467
1468 -def heightmap_multiply_hm(hm1, hm2, hm3):
1469 _lib.TCOD_heightmap_multiply_hm(hm1.p, hm2.p, hm3.p)
1470
1471 -def heightmap_add_hill(hm, x, y, radius, height):
1472 _lib.TCOD_heightmap_add_hill(hm.p, c_float( x), c_float( y), 1473 c_float( radius), c_float( height))
1474
1475 -def heightmap_dig_hill(hm, x, y, radius, height):
1476 _lib.TCOD_heightmap_dig_hill(hm.p, c_float( x), c_float( y), 1477 c_float( radius), c_float( height))
1478
1479 -def heightmap_rain_erosion(hm, nbDrops, erosionCoef, sedimentationCoef, rnd=0):
1480 _lib.TCOD_heightmap_rain_erosion(hm.p, nbDrops, c_float( erosionCoef), 1481 c_float( sedimentationCoef), rnd)
1482
1483 -def heightmap_kernel_transform(hm, kernelsize, dx, dy, weight, minLevel, 1484 maxLevel):
1485 FARRAY = c_float * kernelsize 1486 IARRAY = c_int * kernelsize 1487 cdx = IARRAY() 1488 cdy = IARRAY() 1489 cweight = FARRAY() 1490 for i in range(kernelsize): 1491 cdx[i] = c_int(dx[i]) 1492 cdy[i] = c_int(dy[i]) 1493 cweight[i] = c_float(weight[i]) 1494 _lib.TCOD_heightmap_kernel_transform(hm.p, kernelsize, cdx, cdy, cweight, 1495 c_float(minLevel), c_float(maxLevel))
1496
1497 -def heightmap_add_voronoi(hm, nbPoints, nbCoef, coef, rnd=0):
1498 FARRAY = c_float * nbCoef 1499 ccoef = FARRAY() 1500 for i in range(nbCoef): 1501 ccoef[i] = c_float(coef[i]) 1502 _lib.TCOD_heightmap_add_voronoi(hm.p, nbPoints, nbCoef, ccoef, rnd)
1503
1504 -def heightmap_add_fbm(hm, noise, mulx, muly, addx, addy, octaves, delta, scale):
1505 _lib.TCOD_heightmap_add_fbm(hm.p, noise, c_float(mulx), c_float(muly), 1506 c_float(addx), c_float(addy), 1507 c_float(octaves), c_float(delta), 1508 c_float(scale))
1509 -def heightmap_scale_fbm(hm, noise, mulx, muly, addx, addy, octaves, delta, 1510 scale):
1511 _lib.TCOD_heightmap_scale_fbm(hm.p, noise, c_float(mulx), c_float(muly), 1512 c_float(addx), c_float(addy), 1513 c_float(octaves), c_float(delta), 1514 c_float(scale))
1515 -def heightmap_dig_bezier(hm, px, py, startRadius, startDepth, endRadius, 1516 endDepth):
1517 IARRAY = c_int * 4 1518 cpx = IARRAY() 1519 cpy = IARRAY() 1520 for i in range(4): 1521 cpx[i] = c_int(px[i]) 1522 cpy[i] = c_int(py[i]) 1523 _lib.TCOD_heightmap_dig_bezier(hm.p, cpx, cpy, c_float(startRadius), 1524 c_float(startDepth), c_float(endRadius), 1525 c_float(endDepth))
1526
1527 -def heightmap_get_value(hm, x, y):
1528 return _lib.TCOD_heightmap_get_value(hm.p, x, y)
1529
1530 -def heightmap_get_interpolated_value(hm, x, y):
1531 return _lib.TCOD_heightmap_get_interplated_value(hm.p, c_float(x), 1532 c_float(y))
1533
1534 -def heightmap_get_slope(hm, x, y):
1535 return _lib.TCOD_heightmap_get_slope(hm.p, x, y)
1536
1537 -def heightmap_get_normal(hm, x, y, waterLevel):
1538 FARRAY = c_float * 3 1539 cn = FARRAY() 1540 _lib.TCOD_heightmap_get_normal(hm.p, c_float(x), c_float(y), cn, 1541 c_float(waterLevel)) 1542 return cn[0], cn[1], cn[2]
1543
1544 -def heightmap_count_cells(hm, mi, ma):
1545 return _lib.TCOD_heightmap_count_cells(hm.p, c_float(mi), c_float(ma))
1546
1547 -def heightmap_has_land_on_border(hm, waterlevel):
1548 return _lib.TCOD_heightmap_has_land_on_border(hm.p, 1549 c_float(waterlevel)) == 1
1550
1551 -def heightmap_get_minmax(hm):
1552 mi = c_float() 1553 ma = c_float() 1554 _lib.TCOD_heightmap_get_minmax(hm.p, byref(mi), byref(ma)) 1555 return mi.value, ma.value
1556
1557 -def heightmap_delete(hm):
1558 _lib.TCOD_heightmap_delete(hm.p)
1559 1560 1561 ############################ 1562 # name generator module 1563 ############################ 1564 1565 _lib.TCOD_namegen_get_nb_sets_wrapper.restype = c_int 1566
1567 -def namegen_parse(filename,random=0) :
1568 _lib.TCOD_namegen_parse(filename,random)
1569
1570 -def namegen_generate(name, allocate=0) :
1571 return _lib.TCOD_namegen_generate(name, c_int(allocate))
1572
1573 -def namegen_generate_custom(name, rule, allocate=0) :
1574 return _lib.TCOD_namegen_generate(name, rule, c_int(allocate))
1575
1576 -def namegen_get_sets():
1577 nb=_lib.TCOD_namegen_get_nb_sets_wrapper() 1578 SARRAY = c_char_p * nb; 1579 setsa = SARRAY() 1580 _lib.TCOD_namegen_get_sets_wrapper(setsa) 1581 ret=list() 1582 for i in range(nb): 1583 ret.append(setsa[i]) 1584 return ret
1585
1586 -def namegen_destroy() :
1587 _lib.TCOD_namegen_destroy()
1588