From 5e66fd009ed3b721f475fd7c9d8f2efe90ec2b71 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Sun, 18 Aug 2024 05:00:23 -0500 Subject: [PATCH] Convert entire loaded map into a giant decal for performance reasons. --- src/Hamster.cpp | 2 -- src/HamsterGame.cpp | 30 ++++++++++++++++++++---------- src/HamsterGame.h | 3 ++- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/Hamster.cpp b/src/Hamster.cpp index 22bead6..f669ead 100644 --- a/src/Hamster.cpp +++ b/src/Hamster.cpp @@ -178,13 +178,11 @@ void Hamster::HandlePlayerControls(){ } if(HamsterGame::Game().GetKey(PGUP).bPressed){ HamsterGame::Game().tv.ZoomAtScreenPos(0.95f,HamsterGame::Game().tv.WorldToScreen(GetPos())); - HamsterGame::Game().camera.SetViewSize(HamsterGame::Game().camera.GetViewSize()*1.05f); z+=HamsterGame::Game().GetElapsedTime(); } if(HamsterGame::Game().GetKey(PGDN).bPressed){ z=std::max(0.f,z-HamsterGame::Game().GetElapsedTime()); HamsterGame::Game().tv.ZoomAtScreenPos(1.05f,HamsterGame::Game().tv.WorldToScreen(GetPos())); - HamsterGame::Game().camera.SetViewSize(HamsterGame::Game().camera.GetViewSize()*0.95f); } if(aimingDir!=vf2d{}){ targetRot=aimingDir.norm().polar().y; diff --git a/src/HamsterGame.cpp b/src/HamsterGame.cpp index 1651cb4..f387914 100644 --- a/src/HamsterGame.cpp +++ b/src/HamsterGame.cpp @@ -79,6 +79,12 @@ void HamsterGame::LoadLevel(const std::string_view mapName){ Hamster::LoadHamsters(levelSpawnLoc); camera.SetTarget(Hamster::GetPlayer().GetPos()); + + + mapImage.Create(currentMap.value().GetData().GetMapData().width*16,currentMap.value().GetData().GetMapData().height*16); + SetDrawTarget(mapImage.Sprite()); + Clear(BLANK); + SetPixelMode(Pixel::MASK); #pragma region Detect powerup tiles std::vectormapPowerups; @@ -87,11 +93,23 @@ void HamsterGame::LoadLevel(const std::string_view mapName){ for(size_t x:std::ranges::iota_view(0U,layer.tiles[y].size())){ const int tileID{layer.tiles[y][x]-1}; if(Powerup::TileIDIsUpperLeftPowerupTile(tileID))mapPowerups.emplace_back(vf2d{float(x),float(y)}*16+vf2d{16,16},Powerup::TileIDPowerupType(tileID)); + + const int numTilesWide{GetGFX("gametiles.png").Sprite()->width/16}; + const int numTilesTall{GetGFX("gametiles.png").Sprite()->height/16}; + + int imgTileX{tileID%numTilesWide}; + int imgTileY{tileID/numTilesWide}; + if(tileID==-1||Powerup::TileIDIsPowerupTile(tileID))continue; + DrawPartialSprite(vf2d{float(x),float(y)}*16,GetGFX("gametiles.png").Sprite(),vf2d{float(imgTileX),float(imgTileY)}*16.f,vf2d{16.f,16.f}); } } } Powerup::Initialize(mapPowerups); #pragma endregion + + mapImage.Decal()->Update(); + SetPixelMode(Pixel::NORMAL); + SetDrawTarget(nullptr); } void HamsterGame::UpdateGame(const float fElapsedTime){ @@ -158,27 +176,19 @@ const Terrain::TerrainType HamsterGame::GetTerrainTypeAtPos(const vf2d pos)const void HamsterGame::DrawLevelTiles(){ float extendedBounds{SCREEN_FRAME.pos.x}; extendedBounds*=1/tv.GetWorldScale().x; + tv.DrawDecal({},mapImage.Decal()); for(const LayerTag&layer:currentMap.value().GetData().GetLayers()){ for(float y=tv.GetWorldTL().y-17;y<=tv.GetWorldBR().y+16;y+=16){ for(float x=tv.GetWorldTL().x-17+extendedBounds;x<=tv.GetWorldBR().x+16+extendedBounds;x+=16){ if(x<=0.f||y<=0.f||x>=currentMap.value().GetData().GetMapData().width*16||y>=currentMap.value().GetData().GetMapData().height*16)continue; - const int numTilesWide{GetGFX("gametiles.png").Sprite()->width/16}; - const int numTilesTall{GetGFX("gametiles.png").Sprite()->height/16}; - int tileX{int(floor(x)/16)}; int tileY{int(floor(y)/16)}; int tileID{layer.tiles[tileY][tileX]-1}; - if(tileID==-1||Powerup::TileIDIsPowerupTile(tileID))continue; - - int imgTileX{tileID%numTilesWide}; - int imgTileY{tileID/numTilesWide}; if(ANIMATED_TILE_IDS.count(tileID)){ Animate2D::FrameSequence&animatedTile{ANIMATED_TILE_IDS[tileID]}; const Animate2D::Frame¤tFrame{animatedTile.GetFrame(runTime)}; tv.DrawPartialDecal(vf2d{float(tileX),float(tileY)}*16,currentFrame.GetSourceImage()->Decal(),currentFrame.GetSourceRect().pos,currentFrame.GetSourceRect().size); - }else{ - tv.DrawPartialDecal(vf2d{float(tileX),float(tileY)}*16,GetGFX("gametiles.png").Decal(),vf2d{float(imgTileX),float(imgTileY)}*16,{16,16}); - } + }else continue; } } } diff --git a/src/HamsterGame.h b/src/HamsterGame.h index 3574be9..40b93ef 100644 --- a/src/HamsterGame.h +++ b/src/HamsterGame.h @@ -70,7 +70,6 @@ public: static std::unordered_mapANIMATED_TILE_IDS; const double GetRuntime()const; const Terrain::TerrainType GetTerrainTypeAtPos(const vf2d pos)const; - Camera2D camera; private: void UpdateGame(const float fElapsedTime); void DrawGame(); @@ -86,4 +85,6 @@ private: std::optionalcurrentMap; std::optionalcurrentTileset; double runTime{}; + Camera2D camera; + Renderable mapImage; }; \ No newline at end of file