forked from WenlinMao/SleepWalking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimation2D.cpp
More file actions
122 lines (101 loc) · 3 KB
/
Copy pathAnimation2D.cpp
File metadata and controls
122 lines (101 loc) · 3 KB
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
/**
* @ Author: Wenlin Mao
* @ Create Time: 2021-11-11 21:05:07
* @ Modified by: Wenlin Mao
* @ Modified time: 2021-11-12 22:02:15
* @ Description: Animation System
*/
#include "Animation2D.hpp"
#include "GLCall.hpp"
#include "PlayerStats.hpp"
#include <string>
#include <sstream>
Animation2D::Animation2D(const std::string& filename)
: anim_cursor(0),
current_frame_indx(0),
speed(0.05f)
{
FILE* fp = nullptr;
const int bufferlen = 255;
char line[bufferlen];
std::cout << filename << std::endl;
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
fopen_s(&fp, filename.c_str(), "r");
#elif __APPLE__ || __linux__
fp = fopen(filename.c_str(), "r");
#else
# error "Unknown compiler"
#endif
if(fp == nullptr)
{
printf("erorr in reading animation file \n");
}
else
{
while (fgets(line, bufferlen, fp))
{
vector<int> result;
stringstream ss(line);
string token;
while (getline(ss,token,','))
{
result.push_back(stoi(token));
}
glm::vec4 frame = glm::vec4(result[0], result[1], result[2], result[3]);
frames.push_back(frame);
}
}
frames_count = (int)frames.size();
// for (glm::vec4& frame : frames)
// std::cout << frame.x << " " << frame.y << " " << frame.z << " " << frame.w << std::endl;
// exit(0);
fclose(fp);
}
Animation2D::~Animation2D()
{
}
void Animation2D::play(GLuint VAO, GLuint VBO_texcoords,
const glm::uvec2& texture_size, double deltatime)
{
anim_cursor += deltatime;
if(anim_cursor > speed)
{
current_frame_indx = (current_frame_indx + 1) % frames_count;
anim_cursor = 0;
}
glm::vec4 frame = frames[current_frame_indx];
// normalization
frame.x /= texture_size.x;
frame.y /= texture_size.y;
frame.z /= texture_size.x;
frame.w /= texture_size.y;
vector<glm::vec2> uv = {
glm::vec2(frame.x + frame.z, frame.y + frame.w), // lower right
glm::vec2(frame.x + frame.z, frame.y), // upper right
glm::vec2(frame.x, frame.y), // upper left
glm::vec2(frame.x, frame.y + frame.w) // lower left
};
if (PlayerStats::Instance().isFacingLeft)
uv = {
glm::vec2(frame.x, frame.y + frame.w), // lower left
glm::vec2(frame.x, frame.y), // upper left
glm::vec2(frame.x + frame.z, frame.y), // upper right
glm::vec2(frame.x + frame.z, frame.y + frame.w) // lower right
};
// std::cout << frame.x << " " << frame.y << " " << frame.z << " " << frame.w << std::endl;
GLCall(glBindVertexArray(VAO));
GLCall(glBindBuffer(GL_ARRAY_BUFFER, VBO_texcoords));
{ //realocation for memory
glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(glm::vec2), &uv[0], GL_DYNAMIC_DRAW);
}
// best practice to send data to gpu memory..
//void* gpubuffer = nullptr;
//gpubuffer = glMapBufferRange(GL_ARRAY_BUFFER, 0, 4 * sizeof(glm::vec2), GL_MAP_WRITE_BIT| GL_MAP_INVALIDATE_BUFFER_BIT);
//memcpy(gpubuffer, uv.data(), 4 * sizeof(glm::vec2));
//GLCall(glUnmapBuffer(GL_ARRAY_BUFFER));
//GLCall(glBindBuffer(GL_ARRAY_BUFFER, 0));
}
void Animation2D::set_animation_speed(float newspeed)
{
speed = newspeed;
}