Codename Hamster - For olcCodeJam 2024
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hamster/src/HamsterGame.cpp

78 lines
2.6 KiB

#include "HamsterGame.h"
#include <stdexcept>
geom2d::rect<float>HamsterGame::SCREEN_FRAME{{96,0},{320,288}};
std::unordered_map<std::string,Animate2D::Animation<int>>HamsterGame::ANIMATIONS;
std::unordered_map<std::string,Renderable>HamsterGame::GFX;
const std::string HamsterGame::ASSETS_DIR{"assets/"};
HamsterGame::HamsterGame()
{
sAppName = "Project Hamster";
}
bool HamsterGame::OnUserCreate(){
LoadGraphics();
LoadAnimations();
return true;
}
void HamsterGame::_LoadImage(const std::string_view img){
GFX.insert({ASSETS_DIR+std::string(img),Renderable{}});
rcode result{GFX[ASSETS_DIR+std::string(img)].Load(ASSETS_DIR+std::string(img))};
if(result!=OK)throw std::runtime_error{std::format("Failed to Load Image {}. OLC Rcode: {}",img,int(result))};
}
void HamsterGame::LoadGraphics(){
_LoadImage("border.png");
}
void HamsterGame::LoadAnimations(){
auto LoadStillAnimation=[this](const AnimationState state,const std::string_view img){
Animate2D::FrameSequence stillAnimation{0.f,Animate2D::Style::OneShot};
if(!GFX.count(ASSETS_DIR+std::string(img)))_LoadImage(img);
stillAnimation.AddFrame(Animate2D::Frame{&GetGFX(img),{{},GetGFX(img).Sprite()->Size()}});
ANIMATIONS[ASSETS_DIR+std::string(img)].AddState(state,stillAnimation);
};
auto LoadAnimation=[this](const AnimationState state,const std::string_view img,const std::vector<vf2d>frames,const float frameDuration=0.1f,const Animate2D::Style style=Animate2D::Style::Repeat,vf2d frameSize={32,32}){
Animate2D::FrameSequence stillAnimation{0.f,Animate2D::Style::OneShot};
if(!GFX.count(ASSETS_DIR+std::string(img)))_LoadImage(img);
stillAnimation.AddFrame(Animate2D::Frame{&GetGFX(img),{{},GetGFX(img).Sprite()->Size()}});
ANIMATIONS[ASSETS_DIR+std::string(img)].AddState(state,stillAnimation);
};
LoadAnimation(DEFAULT,"hamster.png",{{},{0,32}},0.3f);
}
bool HamsterGame::OnUserUpdate(float fElapsedTime){
DrawDecal({},GetGFX("border.png").Decal());
gameWindow.FillRectDecal({},{500.f,150.f},WHITE);
return true;
}
const Renderable&HamsterGame::GetGFX(const std::string_view img){
if(!GFX.count(ASSETS_DIR+std::string(img)))throw std::runtime_error{std::format("Image {} does not exist!",img)};
return GFX[ASSETS_DIR+std::string(img)];
}
const Animate2D::Animation<int>&HamsterGame::GetAnimations(const std::string_view img){
if(!ANIMATIONS.count(ASSETS_DIR+std::string(img)))throw std::runtime_error{std::format("Animations for {} does not exist!",img)};
return ANIMATIONS[ASSETS_DIR+std::string(img)];
}
bool HamsterGame::OnUserDestroy(){
ANIMATIONS.clear();
GFX.clear();
return true;
}
int main()
{
HamsterGame game;
if(game.Construct(512, 288, 2, 2))
game.Start();
return 0;
}