carbontwelve / roguelikedev2019

My attempt at learning roguelike gamedev in 2019
MIT License
2 stars 0 forks source link

Implement tilesheet loading as in libtcod #1

Closed carbontwelve closed 5 years ago

carbontwelve commented 5 years ago

To keep project in parity with the TCOD font image format, implement colour key as with https://github.com/libtcod/libtcod/blob/master/src/libtcod/tileset/tilesheet.cpp and character mapping (somewhere near https://github.com/libtcod/libtcod/blob/197b6c34a5f3f08d3a64008a13276971552ddefd/src/libtcod/console_c.cpp#L140).

carbontwelve commented 5 years ago

ASCII Constants https://libtcod.readthedocs.io/en/latest/core/console.html#ascii-constants see https://github.com/libtcod/libtcod/blob/ab49ad79663da6925354bdd9cad8ebc5e92a0a48/src/libtcod/console_types.h#L126-L217

TCOD_console_map_ascii_code_to_font: https://bitbucket.org/libtcod/libtcod/src/19090b92a5f9812d3777dc59b7df48e885914066/src/console_c.c?at=default#lines-800

carbontwelve commented 5 years ago
void TCOD_sys_map_ascii_to_font(int asciiCode, int fontCharX, int fontCharY) {
  if (asciiCode <= 0) { return; } /* can't reassign 0 or negatives */
  // Assign to new-style Tileset.
  auto tileset = tcod::engine::get_tileset();
  auto tilesheet = tcod::engine::get_tilesheet();
  if (tileset && tilesheet) {
    try {
      int tile_id = fontCharX + fontCharY * tilesheet->get_columns();
      tileset->set_tile(asciiCode, tilesheet->get_tile(tile_id));
    } catch (const std::runtime_error&) { // Ignore errors and continue.
    } catch (const std::logic_error&) {
    }
  }
  // Assign to legacy character table.
  if (asciiCode >= TCOD_ctx.max_font_chars) {
    /* reduce total allocations by resizing in increments of 256 */
    if (realloc_ascii_tables((asciiCode | 0xff) + 1)) {
      return; /* Failed to realloc table (old table pointer is still good) */
    }
  }
  TCOD_ctx.ascii_to_tcod[asciiCode] =
      fontCharX + fontCharY * TCOD_ctx.fontNbCharHoriz;
}

From https://github.com/libtcod/libtcod/blob/197b6c34a5f3f08d3a64008a13276971552ddefd/src/libtcod/sys_sdl_c.cpp#L260

carbontwelve commented 5 years ago
/**
 *  Codec for TCOD_FONT_LAYOUT_TCOD.
 *
 *  Converts from EASCII code-point -> raw tile position.
 */
constexpr std::array<int, 256> tcod_codec_{
  0,  0,  0,  0,  0,  0,  0,  0,  0, 76, 77,  0,  0,  0,  0,  0, /* 0 to 15 */
 71, 70, 72,  0,  0,  0,  0,  0, 64, 65, 67, 66,  0, 73, 68, 69, /* 16 to 31 */
  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /* 32 to 47 */
 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 48 to 63 */
 32, 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110, /* 64 to 79 */
111,112,113,114,115,116,117,118,119,120,121, 33, 34, 35, 36, 37, /* 80 to 95 */
 38,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142, /* 96 to 111 */
143,144,145,146,147,148,149,150,151,152,153, 39, 40, 41, 42,  0, /* 112 to 127 */
  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, /* 128 to 143 */
  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, /* 144 to 159 */
  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, /* 160 to 175 */
 43, 44, 45, 46, 49,  0,  0,  0,  0, 81, 78, 87, 88,  0,  0, 55, /* 176 to 191 */
 53, 50, 52, 51, 47, 48,  0,  0, 85, 86, 82, 84, 83, 79, 80,  0, /* 192 to 207 */
  0,  0,  0,  0,  0,  0,  0,  0,  0, 56, 54,  0,  0,  0,  0,  0, /* 208 to 223 */
 74, 75, 57, 58, 59, 60, 61, 62, 63,  0,  0,  0,  0,  0,  0,  0, /* 224 to 239 */
  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, /* 240 to 255 */
};
carbontwelve commented 5 years ago

image