Convert entire loaded map into a giant decal for performance reasons.

main
sigonasr2 3 months ago
parent 946c329b37
commit 5e66fd009e
  1. 2
      src/Hamster.cpp
  2. 30
      src/HamsterGame.cpp
  3. 3
      src/HamsterGame.h

@ -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;

@ -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::vector<Powerup>mapPowerups;
@ -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&currentFrame{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;
}
}
}

@ -70,7 +70,6 @@ public:
static std::unordered_map<uint32_t,Animate2D::FrameSequence>ANIMATED_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::optional<TMXParser>currentMap;
std::optional<TSXParser>currentTileset;
double runTime{};
Camera2D camera;
Renderable mapImage;
};
Loading…
Cancel
Save