void ZeldaGame::loadMap(const std::string& fileName) { TMX tmx(fileName); TMX::Object* pPlayer = nullptr; int playerSortOrder = 0; std::vector<TMX::ObjectGroup> objectGroups = tmx.getObjectGroups(); for (auto& objectGroup : objectGroups) { for (auto& object : objectGroup.objects) { if (object.name == "Player") { if (pPlayer != nullptr) { throw std::runtime_error("Multiple Player objects found."); } pPlayer = &object; playerSortOrder = objectGroup.index; } } } if (pPlayer == nullptr) { throw std::runtime_error("No Player object found."); } auto upPlayer = Player::make(*this, *pPlayer); upPlayer->setDrawOrder(playerSortOrder); mPlayerID = upPlayer->getID(); getSceneGraph().attachNode(std::move(upPlayer)); mpCamera = std::make_unique<Camera>(getEntityManager(), mPlayerID, sf::Vector2f(16 * 24.f, 9 * 24.f)); setTileMap(TileMap::make(*this, getTextureManager(), std::move(tmx))); getMap().setDrawColliderEnabled(true); getMap().setDrawNavGraphEnabled(true); // TODO: DELETE SECOND MAP auto upMap2 = TileMap::make(*this, getTextureManager(), TMX{fileName}); upMap2->setDrawColliderEnabled(true); upMap2->setDrawNavGraphEnabled(true); getMap().stitch(sf::Vector2i{ 0, 0 }, *upMap2, sf::Vector2i{ 0, 20 }); auto* pMap = upMap2.get(); getMap().attachNode(std::move(upMap2)); auto upMap3 = TileMap::make(*this, getTextureManager(), TMX{fileName}); pMap->stitch({ 0, 0 }, *upMap3, { 30, 0 }); getMap().attachNode(std::move(upMap3)); //getMap().move({ -4, 0 }); }
/* Load the buttons based on the current screen. Method will not be called unless buttonsCreated is false. Loads a .tmx file containing buttons. Loads button properties and stores them to temp button and then the buttons are added to the button storage. DESIGN_WINDOW_WIDTH/HEIGHT is used to offset the buttons if the design window for the buttons does not match the current window resolution. @param currentScreen which screen the buttons should be loaded for @return void */ void ButtonHandler::LoadButtons(const std::string& currentScreen) { TMXLoader tmx(BUTTON_PATH); const int DESIGN_WINDOW_WIDTH = tmx.map.width * tmx.map.tilewidth; const int DESIGN_WINDOW_HEIGHT = tmx.map.height * tmx.map.tileheight; for (ObjectLayer layer : tmx.objectLayers) { if (layer.name == currentScreen) { for (Object button : layer.objects) { int frameX = 0, frameY = 0; ButtonAnchor anchor = ButtonAnchor::TOP_LEFT; Action::Type none = Action::Type::DO_NOTHING; Action::Type hover = Action::Type::DO_NOTHING; Action::Type pressed = Action::Type::DO_NOTHING; Action::Type released = Action::Type::DO_NOTHING; Action::Type held = Action::Type::DO_NOTHING; ButtonState state = ButtonState::NONE; /* for (Support::CustomProperty prop : button.properties) { if (prop.name == "frame_x") { frameX = prop.value; } else if (prop.name == "frame_y") { frameY = prop.value; } else if (prop.name == "none") { none = static_cast<Action::Type> (prop.value); } else if (prop.name == "hover") { hover = static_cast<Action::Type> (prop.value); } else if (prop.name == "pressed") { pressed = static_cast<Action::Type> (prop.value); } else if (prop.name == "released") { released = static_cast<Action::Type> (prop.value); } else if (prop.name == "held") { held = static_cast<Action::Type> (prop.value); } else if (prop.name == "anchor") { anchor = static_cast<ButtonAnchor>(prop.value); } else if (prop.name == "state") { state = static_cast<ButtonState> (prop.value); } }*/ int x = button.x, y = button.y, width = button.w, height = button.h; OffsetButton(x, y, width, height, anchor, DESIGN_WINDOW_WIDTH, DESIGN_WINDOW_HEIGHT); buttonStorage.emplace_back(x, y, width, height, frameX, frameY, Action::CreateAction(none), Action::CreateAction(hover), Action::CreateAction(pressed), Action::CreateAction(released), Action::CreateAction(held), anchor); } } } }
int main(int argc, char* argv[]) { TMXLoader tmx("resource/desert.tmx"); // TMXLoader tmx; // tmx.LoadTmx("resource/desert.tmx"); tmx.Print(); std::cout << "TMXLoader size: " << sizeof(tmx) << " bytes\n\n"; /* // SDL TEST. // Desert.tmx must be at least 21 x 16 tiles for test to work. // Use arrow keys to scroll map. SDL_Init(SDL_INIT_EVERYTHING); IMG_Init(IMG_INIT_PNG); SDL_Window* window = SDL_CreateWindow("Tile Display Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN); SDL_Renderer* renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_PRESENTVSYNC); SDL_Texture* texture = SDL_CreateTextureFromSurface( renderer, IMG_Load("resource/tmw_desert_spacing.png")); SDL_Event event; View view; bool quit = false; while (!quit) { while (SDL_PollEvent(&event) != 0) { if (event.type == SDL_QUIT) { quit = true; } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) { quit = true; } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_UP && view.point.y > 0){ view.point.y -= 20; } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_DOWN && view.point.y < (tmx.map.height - (view.viewHeight + 1)) * tmx.map.tileheight) { view.point.y += 20; } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_LEFT && view.point.x > 0) { view.point.x -= 20; } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RIGHT && view.point.x < (tmx.map.width - (view.viewWidth + 1)) * tmx.map.tilewidth) { view.point.x += 20; } } view.tile.x = view.point.x / tmx.map.tilewidth; view.tile.y = view.point.y / tmx.map.tileheight; view.offset.x = view.point.x % tmx.map.tilewidth; view.offset.y = view.point.y % tmx.map.tileheight; for (int y = 0; y < view.viewHeight + 1; ++y) { for (int x = 0; x < view.viewWidth + 1; ++x) { int gid = tmx.layers[0].data.gids [y + view.tile.y][x + view.tile.x]; Tileset tileset; for (unsigned int i = 0; i < tmx.tilesets.size(); ++i) { int firstgid = tmx.tilesets[i].firstgid; int lastgid = tmx.tilesets[i].lastgid; if (gid >= firstgid && gid <= lastgid) { tileset = tmx.tilesets[i]; break; } } int tilesetWidth = tileset.image.width / tmx.map.tilewidth; int spacing = tileset.spacing; int height = (int) ceil((double) gid / tilesetWidth) - 1; int width = gid - (tilesetWidth * height) - 1; SDL_Rect srcrect{ width * (tmx.map.tilewidth + spacing) + spacing, height * (tmx.map.tileheight + spacing) + spacing, tmx.map.tilewidth, tmx.map.tileheight}; SDL_Rect dstrect{ (x * tmx.map.tilewidth) - view.offset.x, (y * tmx.map.tileheight) - view.offset.y, tmx.map.tilewidth, tmx.map.tileheight}; SDL_RenderCopy(renderer, texture, &srcrect, &dstrect); } } SDL_RenderPresent(renderer); SDL_RenderClear(renderer); } SDL_DestroyTexture(texture); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); IMG_Quit(); SDL_Quit(); */ return 0; }