Пример #1
0
GfxTexture *TileMapLayer_create_atlas(TileMapLayer *layer, TileMap *map) {
  int i = 0;

  if (layer->raw_atlas == NULL) {
      layer->raw_atlas = malloc(sizeof(uint8_t) * layer->gid_count * 4);
      check(layer->raw_atlas != NULL, "Could not alloc raw atlas");
  }

  for (i = 0; i < layer->gid_count * 4; i += 4) {
    int tile_idx = i / 4;
    TilesetTile *tile = TileMap_resolve_tile_gid(map, layer->tile_gids[tile_idx]);
    uint32_t first_gid = 0;
    uint32_t diff_gid = UINT32_MAX;
    if (tile) {
      layer->tileset = tile->tileset;
      first_gid = tile->tileset->first_gid;
      diff_gid = layer->tile_gids[tile_idx] - first_gid;
      free(tile);
    }

    layer->raw_atlas[i]   = (diff_gid & 0x000000ff);
    layer->raw_atlas[i+1] = (diff_gid & 0x0000ff00) >> 8;
    layer->raw_atlas[i+2] = (diff_gid & 0x00ff0000) >> 16;
    layer->raw_atlas[i+3] = (diff_gid & 0xff000000) >> 24;
  }
  return GfxTexture_from_data((unsigned char **)&layer->raw_atlas,
                              map->cols, map->rows, GL_RGBA);
error:
  return NULL;
}
Пример #2
0
GfxTexture *TileMapLayer_create_atlas(TileMapLayer *layer, TileMap *map) {
  int i = 0;

  if (layer->raw_atlas == NULL) {
      layer->raw_atlas = malloc(sizeof(GLfloat) * layer->gid_count * 4);
      check(layer->raw_atlas != NULL, "Could not alloc raw atlas");
  }

  int sheet_cols, sheet_rows;
  for (i = 0; i < layer->gid_count * 4; i += 4) {
    int tile_idx = i / 4;
    TilesetTile *tile = TileMap_resolve_tile_gid(map, layer->tile_gids[tile_idx]);
    uint32_t first_gid = 0;
    uint32_t diff_gid = UINT32_MAX;
    uint32_t this_gid = 0;

    int flip_x = 0;
    int flip_y = 0;
    int flip_xy = 0;
    float base_x = 0;
    float base_y = 0;
    if (tile) {
      layer->tileset = tile->tileset;

      sheet_cols = layer->tileset->texture->size.w / layer->tileset->tile_size.w;
      sheet_rows = layer->tileset->texture->size.h / layer->tileset->tile_size.h;

      first_gid = tile->tileset->first_gid;
      this_gid = layer->tile_gids[tile_idx];

      flip_x = this_gid & FLIPPED_HORIZONTALLY_FLAG;
      flip_y = this_gid & FLIPPED_VERTICALLY_FLAG;
      flip_xy = this_gid & FLIPPED_DIAGONALLY_FLAG;

      this_gid &= ~(FLIPPED_HORIZONTALLY_FLAG |
                    FLIPPED_VERTICALLY_FLAG |
                    FLIPPED_DIAGONALLY_FLAG);

      diff_gid = this_gid - first_gid;

      int col = diff_gid % sheet_cols;
      int row = diff_gid / sheet_rows;
      base_x = col * layer->tileset->tile_size.w;
      base_y = row * layer->tileset->tile_size.h;
      base_x /= layer->tileset->texture->size.w;
      base_y /= layer->tileset->texture->size.h;

      free(tile);
    }

    // TODO support flipping

    layer->raw_atlas[i]   = base_x;
    layer->raw_atlas[i+1] = base_y;
    layer->raw_atlas[i+2] = 0.0;
    layer->raw_atlas[i+3] = tile ? 1.0 : 0.0;
  }
  return GfxTexture_from_data((unsigned char **)&layer->raw_atlas,
                              map->cols, map->rows, GL_RGBA, 0, sizeof(float));
error:
  return NULL;
}