From bc05757b84550a01ab0a35ba8dc1b671338e2859 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Fri, 16 Aug 2024 18:24:32 -0500 Subject: [PATCH] Wrote Animation loading functions. --- Hamster.h | 44 ++++++++++++++++++++++++++++++++++++++++ assets/hamster.png | Bin 0 -> 1195 bytes hamster.vcxproj | 4 ++++ hamster.vcxproj.filters | 3 +++ src/HamsterGame.cpp | 32 ++++++++++++++++++++--------- src/HamsterGame.h | 3 ++- 6 files changed, 75 insertions(+), 11 deletions(-) create mode 100644 Hamster.h create mode 100644 assets/hamster.png diff --git a/Hamster.h b/Hamster.h new file mode 100644 index 0000000..fb4e9ce --- /dev/null +++ b/Hamster.h @@ -0,0 +1,44 @@ +#pragma region License +/* +License (OLC-3) +~~~~~~~~~~~~~~~ + +Copyright 2024 Joshua Sigona + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions or derivations of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + +2. Redistributions or derivative works in binary form must reproduce the above +copyright notice. This list of conditions and the following disclaimer must be +reproduced in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may +be used to endorse or promote products derived from this software without specific +prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT +SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. + +Portions of this software are copyright © 2024 The FreeType +Project (www.freetype.org). Please see LICENSE_FT.txt for more information. +All rights reserved. +*/ +#pragma endregion +#pragma once + +#include + +class Hamster{ + //static std::vectorHAMSTER_LIST; +}; \ No newline at end of file diff --git a/assets/hamster.png b/assets/hamster.png new file mode 100644 index 0000000000000000000000000000000000000000..af6ff59073f67c741a614418a548f9eb27202e2a GIT binary patch literal 1195 zcmeAS@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7xzrU~I{Bb`J1#c2+1T%1_J8No8Qr zm{>c}*5j~)%+dJZqe`N#%^emgEBwj=1&;?ERf^EaZ4Hz4(fiw^#_QSDz4$>?!H49H zuO2*Fv$}b;0!Lkgg|&s|E|1Rt-dws#wifS>@2>9OUEUC&Z+!Mu7sIT)*(W1HPNjq| zofpnxz#!NxF>Pk*k>Kvk5}Ek#A3lE7JG0qp{_o;1D=oVj?K&+E@myBBr!epC7T$Be zk0@!M()wumafzq6O5KxPk3LF2Z#vC%UgT^^(Yy$iX=H z>N(pjvY%>Y-WnJv>z58)Z53}uVhsDn+)@fo^Oux1_cE6ss>jNhyE6(1e{WFv6r_F~txBF9A+wvOknXcdd%+61~ z!K8F+4YQ04lQ8S4xc494IaKHWpO^XVJ9Gb)qD?E`e+mXh3ul2xWHAGSo-znCRxGtI z0}8U2c>21szu*uM=j5y0$+HS5Bw6AbQR1ARo12D2D?eX&6CB45)x+P}|9=vcbC=^i_Tz>fI z(%p*#K4`@9-O|6X^8Ny)MGF?QPn&G9g>z^2y1jEO*IvKR@3h`mul(%i`uR_4fvy9B zhyqXV=O_PPdVXP(b?)4Z{kx@q-@V(pzu0(b?%Wq2N|x&U`F70FMdnENy~nSuTKKkT z$R^lu2nw#>^xg19IuLEVf4BExK}25e<;G{?4K=fb_g4O1qEqE^A=75 zyrZju)?7Zn|0;iDrD4>$KC8tiEiL;tzi=t7-?uOF-RfZDPm>}Kg^M4UeW_7wCG(Tg zwVR|`9xV#{{`B?iiF0J8+rRG1Hu|N`TJXpE&!2x)ak3r^X@R`XrVOr%`cWm>jC)ow z_p)ygYI9+q5zKJRIl$EQWBRW@mL&>|KZ1{T=yC+~0GyGMi}p z^W}K3HjZ6m{=Um5y_*;N&5Dz9tJbJXctGcMvamC5j`ClGfH)%Lp-1x|R z4x8TlFQ+QM{5p5p yx@r8sB7a(~`D + + + + diff --git a/hamster.vcxproj.filters b/hamster.vcxproj.filters index d1d8d33..8f2c85d 100644 --- a/hamster.vcxproj.filters +++ b/hamster.vcxproj.filters @@ -52,5 +52,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/src/HamsterGame.cpp b/src/HamsterGame.cpp index 19b28aa..47a05a5 100644 --- a/src/HamsterGame.cpp +++ b/src/HamsterGame.cpp @@ -17,25 +17,37 @@ bool HamsterGame::OnUserCreate(){ return true; } -void HamsterGame::LoadGraphics(){ - - #undef LoadImage - auto LoadImage=[this](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::_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))}; +} - LoadImage("border.png"); +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::vectorframes,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; } diff --git a/src/HamsterGame.h b/src/HamsterGame.h index de4b33c..ffe76f5 100644 --- a/src/HamsterGame.h +++ b/src/HamsterGame.h @@ -45,7 +45,7 @@ class HamsterGame : public olc::PixelGameEngine { const static std::string ASSETS_DIR; public: - enum AnimationStates{ + enum AnimationState{ DEFAULT }; @@ -62,6 +62,7 @@ public: private: void LoadGraphics(); void LoadAnimations(); + void _LoadImage(const std::string_view img); static std::unordered_mapGFX; static std::unordered_map>ANIMATIONS; }; \ No newline at end of file