-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation.cpp
126 lines (105 loc) · 3.29 KB
/
animation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "animation.h"
#include "shaders.h"
#include <glm/gtc/matrix_transform.hpp>
frame::frame(texture2D* image, unsigned int cframe, unsigned int frames) :
image(image), cframe(cframe), frames(frames) {}
void frame::draw(glm::vec2 pos, glm::vec2 scale, int flags)
{
auto shader = getShaders()->at("animation");
shader.use();
glm::mat2 dframe = { {1.0f / frames, 0.0}, {0.0, 1.0f} };
shader.setMat2("frameM", dframe);
float df = (float)(cframe) / (float)(frames);
shader.setFloat("frame", df);
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(pos, 0.0f));
model = glm::scale(model, glm::vec3(scale, 1.0f));
shader.setMat4("model", model);
shader.setInt("flags", flags);
glBindTexture(GL_TEXTURE_2D, image->ID);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
void animSequence::update(unsigned int& curFrame)
{
++curFrame %= sequence.size() << 2;
}
void animSequence::draw(unsigned int& curFrame, glm::vec2 pos, glm::vec2 scale, int flags)
{
sequence[curFrame >> 2]->draw(pos, scale, flags);
update(curFrame);
}
#define ATTACK1_FRAMES 6
#define ATTACK2_FRAMES 6
#define DEATH_FRAMES 6
#define FALL_FRAMES 2
#define IDLE_FRAMES 8
#define JUMP_FRAMES 2
#define RUN_FRAMES 8
#define TAKEHIT_FRAMES 4
#define TAKEHITW_FRAMES 4
std::map<std::string, animSequence*> animations;
void loadAnimations()
{
auto textures = getTextures();
texture2D* anim = &textures->at("attack1");
animSequence* tmpA = new animSequence();
frame* tmp;
for (unsigned int i = 0; i < ATTACK1_FRAMES; i++)
{
tmp = new frame(anim, i, ATTACK1_FRAMES);
tmpA->sequence.push_back(tmp);
}
animations.insert(std::pair<std::string, animSequence*>("attack1", tmpA));
anim = &textures->at("attack2");
tmpA = new animSequence();
for (unsigned int i = 0; i < ATTACK2_FRAMES; i++)
{
tmp = new frame(anim, i, ATTACK2_FRAMES);
tmpA->sequence.push_back(tmp);
}
animations.insert(std::pair<std::string, animSequence*>("attack2", tmpA));
anim = &textures->at("death");
tmpA = new animSequence();
for (unsigned int i = 0; i < DEATH_FRAMES; i++)
{
tmp = new frame(anim, i, DEATH_FRAMES);
tmpA->sequence.push_back(tmp);
}
animations.insert(std::pair<std::string, animSequence*>("death", tmpA));
anim = &textures->at("idle");
tmpA = new animSequence();
for (unsigned int i = 0; i < IDLE_FRAMES; i++)
{
tmp = new frame(anim, i, IDLE_FRAMES);
tmpA->sequence.push_back(tmp);
}
animations.insert(std::pair<std::string, animSequence*>("idle", tmpA));
anim = &textures->at("run");
tmpA = new animSequence();
for (unsigned int i = 0; i < RUN_FRAMES; i++)
{
tmp = new frame(anim, i, RUN_FRAMES);
tmpA->sequence.push_back(tmp);
}
animations.insert(std::pair<std::string, animSequence*>("run", tmpA));
anim = &textures->at("takehit");
tmpA = new animSequence();
for (unsigned int i = 0; i < TAKEHIT_FRAMES; i++)
{
tmp = new frame(anim, i, TAKEHIT_FRAMES);
tmpA->sequence.push_back(tmp);
}
animations.insert(std::pair<std::string, animSequence*>("takehit", tmpA));
anim = &textures->at("takehitW");
tmpA = new animSequence();
for (unsigned int i = 0; i < TAKEHITW_FRAMES; i++)
{
tmp = new frame(anim, i, TAKEHITW_FRAMES);
tmpA->sequence.push_back(tmp);
}
animations.insert(std::pair<std::string, animSequence*>("takehitW", tmpA));
}
std::map<std::string, animSequence*>& getAnimations()
{
return animations;
}