void GameMap::drawMap() { if (screen == NULL) return; if (mapSurface != NULL) SDL_FreeSurface(mapSurface); mapSurface = GameDraw::CreateBlankSurface(mapWidth * 16, mapHeight * 16); if (tilesetSurface == NULL) LoadTileSet(string(tileset)); for (int i = 0; i < mapHeight; ++i) { for (int j = 0; j < mapWidth; j++) { Tile* _tmpTile = &mapTiles[i][j]; SDL_Rect clip; clip.x = defTile.tX * 16; clip.y = defTile.tY * 16; clip.w = clip.h = 16; // Draw the default tile GameDraw::RenderToSurface(j*16, i*16, tilesetSurface, mapSurface, &clip); // Draw the actual tile overtop of it. clip.x = _tmpTile->tX * 16; clip.y = _tmpTile->tY * 16; GameDraw::RenderToSurface(j*16, i*16, tilesetSurface, mapSurface, &clip); } } if (mapEdit) { for (unsigned int i = 0; i < PH.size(); i++) GameDraw::RenderToSurface(PH[i].r.x, PH[i].r.y, PH[i].surface, mapSurface, &PH[i].surface->clip_rect); for (unsigned int i = 0; i < mapBlock.size(); i++) GameDraw::DrawSquare(mapBlock[i], mapSurface, SDL_MapRGB(mapSurface->format, 0xFF, 0, 0)); for (unsigned int i = 0; i < mapLink.size(); i++) GameDraw::DrawSquare(mapLink[i].r, mapSurface, SDL_MapRGB(mapSurface->format, 0, 0xFF, 0xFF)); } }
void GameMap::SetTileset(string ts) { if (tileset) delete [] tileset; tileset = new char[ts.length() + 1]; memcpy(tileset, ts.c_str(), ts.length()); tileset[ts.length()] = '\0'; LoadTileSet(tileset); }
TileMap::TileMap(Game &game, Model model, char* tileSetPath, Camera* camera) { this->tileModel = model; this->camera = camera; std::cout << "Loading Tiles, this may take a while..." << std::endl; LoadTileSet(tileSetPath); for (int y = 0; y < MAP_SIZE_IN_TILES_HEIGHT; y++) { for (int x = 0; x < MAP_SIZE_IN_TILES_WIDTH; x++) { map[x][y] = Tile(game, model, glm::vec3(x * TILE_SIZE_WIDTH, (y * TILE_SIZE_HEIGHT) + TILE_SIZE_HEIGHT, 0.0f)); } } for (int x = 0; x < MAP_SIZE_IN_TILES_WIDTH; x++) { UI[x] = Tile(game, model, glm::vec3(x * TILE_SIZE_WIDTH, 0.0f, 0.1f)); } LoadUI(); LoadMap("content/levels/map1.lvl"); std::cout << MAP_SIZE_IN_TILES_WIDTH * MAP_SIZE_IN_TILES_HEIGHT << " Tiles Loaded! " << std::endl; }
bool TmxFile2D::EndLoad() { if (!loadXMLFile_) return false; XMLElement rootElem = loadXMLFile_->GetRoot("map"); String version = rootElem.GetAttribute("version"); if (version != "1.0") { URHO3D_LOGERROR("Invalid version"); return false; } String orientation = rootElem.GetAttribute("orientation"); if (orientation == "orthogonal") info_.orientation_ = O_ORTHOGONAL; else if (orientation == "isometric") info_.orientation_ = O_ISOMETRIC; else if (orientation == "staggered") info_.orientation_ = O_STAGGERED; else if (orientation == "hexagonal") info_.orientation_ = O_HEXAGONAL; else { URHO3D_LOGERROR("Unsupported orientation type " + orientation); return false; } info_.width_ = rootElem.GetInt("width"); info_.height_ = rootElem.GetInt("height"); info_.tileWidth_ = rootElem.GetFloat("tilewidth") * PIXEL_SIZE; info_.tileHeight_ = rootElem.GetFloat("tileheight") * PIXEL_SIZE; for (unsigned i = 0; i < layers_.Size(); ++i) delete layers_[i]; layers_.Clear(); for (XMLElement childElement = rootElem.GetChild(); childElement; childElement = childElement.GetNext()) { bool ret = true; String name = childElement.GetName(); if (name == "tileset") ret = LoadTileSet(childElement); else if (name == "layer") { TmxTileLayer2D* tileLayer = new TmxTileLayer2D(this); ret = tileLayer->Load(childElement, info_); layers_.Push(tileLayer); } else if (name == "objectgroup") { TmxObjectGroup2D* objectGroup = new TmxObjectGroup2D(this); ret = objectGroup->Load(childElement, info_); layers_.Push(objectGroup); } else if (name == "imagelayer") { TmxImageLayer2D* imageLayer = new TmxImageLayer2D(this); ret = imageLayer->Load(childElement, info_); layers_.Push(imageLayer); } if (!ret) { loadXMLFile_.Reset(); tsxXMLFiles_.Clear(); return false; } } loadXMLFile_.Reset(); tsxXMLFiles_.Clear(); return true; }
Level* LevelManager::LoadLevel (const std::string& path) { //Load file std::ifstream input (path); //Ensure it opened if (!input) { printf ("Warning! Could not open file: %s\n", path.c_str ()); return nullptr; } //Get the contents std::string fileContents; std::string line; while (std::getline (input, line)) fileContents += line; //Convert to rapidxml format std::vector<char> data = std::vector<char> (fileContents.begin (), fileContents.end ()); data.push_back ('\0'); rapidxml::xml_document<> doc; doc.parse<rapidxml::parse_no_data_nodes> (&data[0]); rapidxml::xml_node<>* root = doc.first_node (); int width = atoi (root->first_attribute ("width")->value ()); int height = atoi (root->first_attribute ("height")->value ()); rapidxml::xml_node<>* tileset = root->first_node ("tileset"); Level* level = new Level (width, height, LoadTileSet (tileset->first_attribute ("path")->value ()), root->first_attribute ("name")->value ()); std::string tiles = root->first_node ("tiles")->value(); int x = 0; int y = 0; if (!level) { printf ("Warning! Could not create level\n"); return nullptr; } for (std::string::iterator itr = tiles.begin (); itr != tiles.end (); ++itr) { if (*itr != ' ' && *itr != '\n' && *itr != '\r' && *itr != '\t') { Tile* tile = new Tile (GetTile ((TileType)std::atoi (&(*itr)))); tile->SetPos(Vec2{(float) x * tile->GetTileSize(), (float) y * tile->GetTileSize() }); level->AddTile (x, y, tile); ++x; if (x % width == 0) { x = 0; ++y; } } } App::GetInst ()->GetGOManager ()->LoadLevel (level->TileSize (), level->NumTilesX (), level->NumTilesY ()); printf ("Success: Loaded level: %s!\n", path.c_str ()); return level; }