From 1d8f42283b9e3fba25c759dda8c9f2200636e99e Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Sat, 26 Aug 2023 11:16:26 -0500 Subject: [PATCH] Refactored how images are stored. --- olcCodeJam2023Entry/Constant.cpp | 3 -- olcCodeJam2023Entry/Constant.h | 2 - olcCodeJam2023Entry/Image.h | 12 ++++++ olcCodeJam2023Entry/Unit.cpp | 6 +-- olcCodeJam2023Entry/Unit.h | 5 ++- olcCodeJam2023Entry/VirusAttack.cpp | 42 ++++++++++++------- olcCodeJam2023Entry/VirusAttack.h | 5 ++- .../olcCodeJam2023Entry.vcxproj | 1 + .../olcCodeJam2023Entry.vcxproj.filters | 3 ++ 9 files changed, 52 insertions(+), 27 deletions(-) create mode 100644 olcCodeJam2023Entry/Image.h diff --git a/olcCodeJam2023Entry/Constant.cpp b/olcCodeJam2023Entry/Constant.cpp index 3238e81..ffecd6a 100644 --- a/olcCodeJam2023Entry/Constant.cpp +++ b/olcCodeJam2023Entry/Constant.cpp @@ -9,8 +9,5 @@ Pixel CONSTANT::PROCEDURE_COLOR={212, 11, 162}; vf2d CONSTANT::UNSELECTED={-99,-99}; -Renderable CONSTANT::VIRUS_IMG1; -Renderable CONSTANT::SELECTION_CIRCLE; - vi2d CONSTANT::TILE_SIZE={24,24}; vi2d CONSTANT::WORLD_SIZE={64,64}; \ No newline at end of file diff --git a/olcCodeJam2023Entry/Constant.h b/olcCodeJam2023Entry/Constant.h index 4072d6c..50a5dd3 100644 --- a/olcCodeJam2023Entry/Constant.h +++ b/olcCodeJam2023Entry/Constant.h @@ -12,8 +12,6 @@ public: static vf2d UNSELECTED; - static Renderable VIRUS_IMG1,SELECTION_CIRCLE; - static vi2d TILE_SIZE; static vi2d WORLD_SIZE; }; \ No newline at end of file diff --git a/olcCodeJam2023Entry/Image.h b/olcCodeJam2023Entry/Image.h new file mode 100644 index 0000000..dda8e4d --- /dev/null +++ b/olcCodeJam2023Entry/Image.h @@ -0,0 +1,12 @@ +#pragma once + + +enum Image{ + TILE, + MINIMAP_HUD, + OUTLINE, + MINIMAP_OUTLINE, + VIRUS_IMG1, + SELECTION_CIRCLE +}; + diff --git a/olcCodeJam2023Entry/Unit.cpp b/olcCodeJam2023Entry/Unit.cpp index 04ce039..4910b10 100644 --- a/olcCodeJam2023Entry/Unit.cpp +++ b/olcCodeJam2023Entry/Unit.cpp @@ -68,14 +68,14 @@ Unit::Unit(std::vectormemory,vf2d pos,Renderable&img,bool friendly) -void Unit::Draw(TileTransformedView&game){ +void Unit::Draw(TileTransformedView&game,std::map>&IMAGES){ game.DrawRotatedDecal(ghostPos,img.Decal(),0,img.Sprite()->Size()/2,{1,1},friendly?Pixel{192,192,255}:Pixel{255,192,192}); if(IsSelected()){ - game.DrawRotatedDecal(ghostPos,CONSTANT::SELECTION_CIRCLE.Decal(),0,CONSTANT::SELECTION_CIRCLE.Sprite()->Size()/2,vf2d(img.Sprite()->Size())/CONSTANT::SELECTION_CIRCLE.Sprite()->Size(),WHITE); + game.DrawRotatedDecal(ghostPos,IMAGES[SELECTION_CIRCLE]->Decal(),0,IMAGES[SELECTION_CIRCLE]->Sprite()->Size()/2,vf2d(img.Sprite()->Size())/IMAGES[SELECTION_CIRCLE]->Sprite()->Size(),WHITE); } } -void Unit::DrawHud(TileTransformedView&game){ +void Unit::DrawHud(TileTransformedView&game,std::map>&IMAGES){ int initialBarX=ghostPos.x-GetMemorySize()/2*CONSTANT::BAR_SQUARE_SIZE.x; int initialBarY=ghostPos.y-CONSTANT::BAR_SQUARE_SIZE.y-img.Sprite()->height/2-2; Pixel col=0; diff --git a/olcCodeJam2023Entry/Unit.h b/olcCodeJam2023Entry/Unit.h index f48f76c..47b70ed 100644 --- a/olcCodeJam2023Entry/Unit.h +++ b/olcCodeJam2023Entry/Unit.h @@ -3,6 +3,7 @@ #include "olcPixelGameEngine.h" #include "olcPGEX_TransformedView.h" #include "Constant.h" +#include "Image.h" struct Marker{ size_t index; @@ -35,8 +36,8 @@ public: std::vectorghostMemory; void Update(float fElapsedTime); virtual void Attack(Unit&victim)=0; - virtual void Draw(TileTransformedView&game); - virtual void DrawHud(TileTransformedView&game); + virtual void Draw(TileTransformedView&game,std::map>&IMAGES); + virtual void DrawHud(TileTransformedView&game,std::map>&IMAGES); bool IsFriendly(); bool IsSelected(); void Select(); diff --git a/olcCodeJam2023Entry/VirusAttack.cpp b/olcCodeJam2023Entry/VirusAttack.cpp index 3603488..5ef6d31 100644 --- a/olcCodeJam2023Entry/VirusAttack.cpp +++ b/olcCodeJam2023Entry/VirusAttack.cpp @@ -12,23 +12,33 @@ VirusAttack::VirusAttack() sAppName = "olcCodeJam 2023 Entry"; } +void VirusAttack::InitializeImages(){ + auto LoadImage=[&](Image img,std::string filepath,bool filter=false,bool clamp=true){ + IMAGES[img]=std::make_unique(); + IMAGES[img]->Load(filepath,nullptr,filter,clamp); + }; + LoadImage(TILE,"assets/tile.png",false,false); + LoadImage(MINIMAP_HUD,"assets/minimap_hud.png"); + LoadImage(OUTLINE,"assets/outline.png"); + LoadImage(VIRUS_IMG1,"assets/unit.png"); + LoadImage(SELECTION_CIRCLE,"assets/selection_circle.png"); + +} + bool VirusAttack::OnUserCreate(){ game.Initialise(GetScreenSize()); - CONSTANT::VIRUS_IMG1.Load("assets/unit.png"); - CONSTANT::SELECTION_CIRCLE.Load("assets/selection_circle.png"); + InitializeImages(); - TILE.Load("assets/tile.png",nullptr,false,false); - MINIMAP_HUD.Load("assets/minimap_hud.png"); - OUTLINE.Load("assets/outline.png"); - MINIMAP_OUTLINE.Create(64,64); + IMAGES[MINIMAP_OUTLINE]=std::make_unique(); + IMAGES[MINIMAP_OUTLINE]->Create(64,64); - units.push_back(std::make_unique(vf2d{32,32},CONSTANT::VIRUS_IMG1,true)); + units.push_back(std::make_unique(vf2d{32,32},*IMAGES[VIRUS_IMG1],true)); for(int i=0;i<10;i++){ if(rand()%2==0){ - units.push_back(std::make_unique(vf2d{float(rand()%ScreenWidth()),float(rand()%ScreenHeight())},CONSTANT::VIRUS_IMG1,true)); + units.push_back(std::make_unique(vf2d{float(rand()%ScreenWidth()),float(rand()%ScreenHeight())},*IMAGES[VIRUS_IMG1],true)); } else { - units.push_back(std::make_unique(vf2d{float(rand()%ScreenWidth()),float(rand()%ScreenHeight())},CONSTANT::VIRUS_IMG1,false)); + units.push_back(std::make_unique(vf2d{float(rand()%ScreenWidth()),float(rand()%ScreenHeight())},*IMAGES[VIRUS_IMG1],false)); } } return true; @@ -121,11 +131,11 @@ void VirusAttack::IdentifyClosestTarget(Unit*&closestUnit,float&closestDist,std: } void VirusAttack::DrawMinimap(){ - DrawDecal(GetScreenSize()-MINIMAP_HUD.Sprite()->Size(),MINIMAP_HUD.Decal(),{1,1}); + DrawDecal(GetScreenSize()-IMAGES[MINIMAP_HUD]->Sprite()->Size(),IMAGES[MINIMAP_HUD]->Decal(),{1,1}); vf2d minimapTL=GetScreenSize()-vf2d{64,64}; vi2d worldPixelSize=CONSTANT::WORLD_SIZE*CONSTANT::TILE_SIZE; vf2d viewingTilesPct=vf2d{float(ScreenWidth()),float(ScreenHeight())}/CONSTANT::TILE_SIZE/CONSTANT::WORLD_SIZE; - SetDrawTarget(MINIMAP_OUTLINE.Sprite()); + SetDrawTarget(IMAGES[MINIMAP_OUTLINE]->Sprite()); for(int y=0;y<64;y++){ for(int x=0;x<64;x++){ Draw(x,y,BLANK); @@ -135,9 +145,9 @@ void VirusAttack::DrawMinimap(){ for(auto&u:units){ FillRect(u->GetGhostPos()/worldPixelSize*64,vf2d{2,2}*u->GetUnitSize()/24,u->IsFriendly()?GREEN:RED); } - MINIMAP_OUTLINE.Decal()->Update(); + IMAGES[MINIMAP_OUTLINE]->Decal()->Update(); SetDrawTarget(nullptr); - DrawDecal(minimapTL,MINIMAP_OUTLINE.Decal()); + DrawDecal(minimapTL,IMAGES[MINIMAP_OUTLINE]->Decal()); } void VirusAttack::HandlePanAndZoom(float fElapsedTime){ @@ -208,13 +218,13 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){ u->HideGhost(); } } - game.DrawPartialDecal({0,0},CONSTANT::WORLD_SIZE*CONSTANT::TILE_SIZE,TILE.Decal(),{0,0},CONSTANT::WORLD_SIZE*CONSTANT::TILE_SIZE,DARK_GREEN); + game.DrawPartialDecal({0,0},CONSTANT::WORLD_SIZE*CONSTANT::TILE_SIZE,IMAGES[TILE]->Decal(),{0,0},CONSTANT::WORLD_SIZE*CONSTANT::TILE_SIZE,DARK_GREEN); for(auto&u:units){ - u->Draw(game); + u->Draw(game,IMAGES); } for(auto&u:units){ - u->DrawHud(game); + u->DrawHud(game,IMAGES); } DrawSelectionRectangle(); diff --git a/olcCodeJam2023Entry/VirusAttack.h b/olcCodeJam2023Entry/VirusAttack.h index 1c6871f..eaba096 100644 --- a/olcCodeJam2023Entry/VirusAttack.h +++ b/olcCodeJam2023Entry/VirusAttack.h @@ -3,12 +3,14 @@ #include "olcPGEX_TransformedView.h" #include "Unit.h" #include "Constant.h" +#include "Image.h" + class VirusAttack : public olc::PixelGameEngine { private: std::vector>units; - Renderable TILE,MINIMAP_HUD,OUTLINE,MINIMAP_OUTLINE; + std::map>IMAGES; TileTransformedView game; @@ -22,6 +24,7 @@ private: void HandlePanAndZoom(float fElapsedTime); void DrawMinimap(); void HandleMinimapClick(); + void InitializeImages(); public: VirusAttack(); diff --git a/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj b/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj index 315b7cf..76f7398 100644 --- a/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj +++ b/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj @@ -132,6 +132,7 @@ + diff --git a/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj.filters b/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj.filters index 9f7ce7e..261b99a 100644 --- a/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj.filters +++ b/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj.filters @@ -60,6 +60,9 @@ Header Files + + Header Files +