GlistEngine
Loading...
Searching...
No Matches
gSpriteAnimation.h
Go to the documentation of this file.
1/*
2 * gAnimation.h
3 *
4 * Created on: 22 Eyl 2021
5 * Author: kayra
6 */
7
8#ifndef ANIMATION_GANIMATION_H_
9#define ANIMATION_GANIMATION_H_
10
11#include <functional>
12#include <memory>
13
14#include "gImage.h"
15
27
36public:
37 template<typename First, typename Second>
38 constexpr gAnimationTrigger(First* first, Second* second, TriggerCondition cond) {
39 switch(cond) {
41 triggerfunc = [first, second]() -> bool { return *first < *second; };
42 break;
44 triggerfunc = [first, second]() -> bool { return *first > *second; };
45 break;
47 triggerfunc = [first, second]() -> bool { return *first <= *second; };
48 break;
50 triggerfunc = [first, second]() -> bool { return *first >= *second; };
51 break;
53 triggerfunc = [first, second]() -> bool { return *first == *second; };
54 break;
56 triggerfunc = [first]() -> bool { return *first == false; };
57 break;
59 triggerfunc = [first]() -> bool { return *first == true; };
60 break;
62 break;
63 }
64 }
65
67 return triggerfunc();
68 }
69private:
70 std::function<bool()> triggerfunc;
71};
72
73class gSpriteAnimation : public gObject {
74public:
77
83 void loadFrame(const std::string& framePath);
84
85 void setLoop(bool isLooped);
86 void setFps(int fps);
87 int getFps();
88
89 void addConditionTrigger(std::shared_ptr<gAnimationTrigger> trigger);
91
95 void reset();
96
102 void update();
103
111 void draw(int x, int y);
112
122 void draw(int x, int y, int w, int h);
123
124private:
125 bool shouldStop();
126
127 int fps;
128 int framecount;
129 int framecounter;
130 int currentframe;
131
132 bool islooped;
133
134 std::shared_ptr<gAnimationTrigger> trigger;
135 bool hastriggered;
136
137 std::vector<std::unique_ptr<gImage>> frames;
138};
139
176gSpriteAnimation* createSpriteAnim(std::shared_ptr<gAnimationTrigger> animationTrigger);
177
186template<typename First, typename Second>
187std::shared_ptr<gAnimationTrigger> createTrigger(First* first, Second* second, TriggerCondition cond) {
188 return std::shared_ptr<gAnimationTrigger>(new gAnimationTrigger{first, second, cond});
189}
190
191// These macros can be used instead of calling the createTrigger function.
192// The macro parameters First and Second should be the pointer to the values
193// that are to be compared.
194#define TRIGGER_NONE nullptr
195#define TRIGGER_LESS(First, Second) createTrigger(First, Second, TriggerCondition::LESS)
196#define TRIGGER_GREATER(First, Second) createTrigger(First, Second, TriggerCondition::GREATER)
197#define TRIGGER_LESS_EQUAL(First, Second) createTrigger(First, Second, TriggerCondition::LESS_EQUAL)
198#define TRIGGER_GREATER_EQUAL(First, Second) createTrigger(First, Second, TriggerCondition::GREATER_EQUAL)
199#define TRIGGER_EQUAL(First, Second) createTrigger(First, Second, TriggerCondition::EQUAL)
200#define TRIGGER_FALSE(First, Second) createTrigger(First, Second, TriggerCondition::FALSE)
201#define TRIGGER_TRUE(First, Second) createTrigger(First, Second, TriggerCondition::TRUE)
202
203#endif /* ANIMATION_GANIMATION_H_ */
Definition gSpriteAnimation.h:35
bool isConditionTriggered()
Definition gSpriteAnimation.h:66
constexpr gAnimationTrigger(First *first, Second *second, TriggerCondition cond)
Definition gSpriteAnimation.h:38
Definition gObject.h:33
Definition gSpriteAnimation.h:73
virtual ~gSpriteAnimation()
void draw(int x, int y, int w, int h)
void draw(int x, int y)
void loadFrame(const std::string &framePath)
bool isConditionTriggered()
void setLoop(bool isLooped)
void addConditionTrigger(std::shared_ptr< gAnimationTrigger > trigger)
void setFps(int fps)
TriggerCondition
Definition gSpriteAnimation.h:16
@ GREATER
Definition gSpriteAnimation.h:18
@ GREATER_EQUAL
Definition gSpriteAnimation.h:20
@ EQUAL
Definition gSpriteAnimation.h:21
@ MAX_TRIGGER_CONDITIONS
Definition gSpriteAnimation.h:25
@ LESS
Definition gSpriteAnimation.h:17
@ FALSE
Definition gSpriteAnimation.h:22
@ TRUE
Definition gSpriteAnimation.h:23
@ LESS_EQUAL
Definition gSpriteAnimation.h:19
std::shared_ptr< gAnimationTrigger > createTrigger(First *first, Second *second, TriggerCondition cond)
Definition gSpriteAnimation.h:187
gSpriteAnimation * createSpriteAnim(std::shared_ptr< gAnimationTrigger > animationTrigger)