void TileRepository::importBitmap(BITMAP* tileBitmap, const char* group_name, int tile_w, int tile_h, int tile_spacing) { BITMAP *tempBitmap; TileType *tempTileType; char tempTilename[256]; int x, y; ASSERT(tileBitmap); for (y = 0; y < (tileBitmap->h / (tile_h + tile_spacing)); y++) { for (x = 0; x < (tileBitmap->w / (tile_w + tile_spacing)); x++) { // Create a new tile type and add it to the hash_map tempBitmap = create_bitmap(tile_w, tile_h); blit( tileBitmap, tempBitmap, x * (tile_w + tile_spacing), y * (tile_h + tile_spacing), 0, 0, tile_w, tile_h ); sprintf(tempTilename, "%s%03d", group_name, y * (tileBitmap->w / tile_w) + x); tempTileType = new TileType(tempBitmap, tempTilename); tileTypes.insert(make_pair(tempTileType->getName(), tempTileType)); } } }
void TileRepository::importDatafile(DATAFILE *file) { if (!file) return; TileType *tempTileType; BITMAP *tempBitmap; // Import bitmaps from the datafile while (file->type != DAT_END) { switch (file->type) { case DAT_FILE: // Go recursively into nested datafiles importDatafile((DATAFILE*)file->dat); break; case DAT_BITMAP: // Create a new tile type and add it to the hash_map tempBitmap = create_bitmap(((BITMAP*)file->dat)->w, ((BITMAP*)file->dat)->h); blit((BITMAP*)file->dat, tempBitmap, 0, 0, 0, 0, tempBitmap->w, tempBitmap->h); tempTileType = new TileType(tempBitmap, get_datafile_property(file, DAT_ID('N','A','M','E'))); tileTypes.insert(make_pair(tempTileType->getName(), tempTileType)); break; } file++; } }
/** * Returns the name of this tile type. * * @returns string This tile type's name. */ int TileType_name(lua_State *lua) { TileType *tile = castUData<TileType>(lua, 1); if (tile) { lua_pushstring(lua, tile->getName()); return 1; } return LuaState::expectedContext(lua, "name", "TileType"); }
void TileRepository::exportBitmap(const char *filename, int tile_w, int tile_h, int tile_spacing, int tiles_in_row) { list<TileType*> tiles_to_save; map<const char*, TileType*, ltstr>::iterator i; list<TileType*>::iterator j; char tempTilename[256]; char tempFilename[256]; replace_extension(tempFilename, get_filename(filename), "", 256); if (!(tiles_in_row > 0 && tile_w > 0 && tile_h > 0)) { allegro_message("WARNING: tiles_in_row (%d), tile_w (%d) and tile_h (%d) must all be larger than 0.", tiles_in_row, tile_w, tile_h); return; } for (i = tileTypes.begin(); i != tileTypes.end(); i++) { TileType* tempTileType = (*i).second; replace_extension(tempTilename, tempTileType->getName(), "", 256); if (ustrcmp(tempFilename, tempTilename) == 0) { tiles_to_save.push_back(tempTileType); } } if (tiles_to_save.empty()) { allegro_message("WARNING: No tiles to save in %s.", filename); return; } BITMAP *tile_bitmap; PALETTE pal; tile_bitmap = create_bitmap ( tiles_in_row * tile_w, (tiles_to_save.size() / tiles_in_row + tiles_to_save.size() % tiles_in_row) * tile_h ); int x = 0; int y = 0; for (j = tiles_to_save.begin(); j != tiles_to_save.end(); j++) { blit((*j)->getBitmap(), tile_bitmap, 0, 0, x * tile_w, y * tile_h, tile_w, tile_h); x++; if (x == tiles_in_row) { y++; x = 0; } } get_palette(pal); save_bitmap(filename, tile_bitmap, pal); destroy_bitmap(tile_bitmap); }
void TileRepository::importBitmap(const char *filename, int tile_w, int tile_h, int tile_spacing) { BITMAP *tileBitmap; BITMAP *tempBitmap; TileType *tempTileType; char tempTilename[256]; char tempFilename[256]; PALETTE pal; int x, y; tileBitmap = load_bitmap(filename, pal); if (!tileBitmap) { allegro_message("Warning, %s is not a valid tile bitmap!\n", filename); return; } set_palette(pal); replace_extension(tempFilename, get_filename(filename), "", 256); ASSERT(tileBitmap); for (y = 0; y < (tileBitmap->h / (tile_h + tile_spacing)); y++) { for (x = 0; x < (tileBitmap->w / (tile_w + tile_spacing)); x++) { // Create a new tile type and add it to the hash_map tempBitmap = create_bitmap(tile_w, tile_h); blit( tileBitmap, tempBitmap, x * (tile_w + tile_spacing), y * (tile_h + tile_spacing), 0, 0, tile_w, tile_h ); sprintf(tempTilename, "%s%03d", tempFilename, y * (tileBitmap->w / tile_w) + x); tempTileType = new TileType(tempBitmap, tempTilename); tileTypes.insert(make_pair(tempTileType->getName(), tempTileType)); } } destroy_bitmap(tileBitmap); }
bool TileType::loadStandardTileTypesLua(const char *filename) { LuaState lua(false); if (!lua.loadFile(filename)) { lua.logStack("TITLE"); lua.close(); stringstream errss; errss << "Unable to load standard tile types"; am_log("TILE", errss); return false; } lua_getglobal(lua, "types"); if (!lua_istable(lua, -1)) { stringstream errss; errss << "Unable to load standard tile types, loaded file was of type: " << lua_typename(lua, -1); am_log("TILE", errss); lua.close(); return false; } /* table is in the stack at index 't' */ lua_pushnil(lua); /* first key */ while (lua_next(lua, -2) != 0) { /* uses 'key' (at index -2) and 'value' (at index -1) */ if (lua_isstring(lua, -2)) { if (!lua_istable(lua, -1)) { stringstream errss; errss << "Tile type '" << lua_tostring(lua, -2) << "' was of type '" << lua_typename(lua, -1) << "' and not an object."; am_log("TILE", errss); continue; } string tileName = Utils::toLowerCase(lua_tostring(lua, -2)); TileType *loadedType = new TileType(tileName.c_str()); if (!loadedType->loadFromDef(lua)) { stringstream errss; errss << "Failed to load '" << tileName << "' object in definition was invalid."; am_log("TILE", errss); delete loadedType; continue; } stringstream ss; ss << "Added tile type '" << loadedType->getName() << "'"; am_log("TILE", ss); // TODO: Change to not needing this loadStandard function. Engine::getEngine()->addTileType(loadedType); } /* removes 'value'; keeps 'key' for next iteration */ lua_pop(lua, 1); } lua.close(); return true; }