-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation.cpp
140 lines (123 loc) · 3.59 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "Animation.hpp"
#include <Graphics/Graphics.hpp>
#include <Graphics/Font.hpp>
#include <Vector/VectorGL.hpp>
#include <iostream>
using std::cout;
namespace {
Module *create_animation(const std::string ¶ms) {
return new AnimationModule(params);
}
class Fred {
public:
Fred() {
register_module("anim", create_animation, "[noframe] filename[0000].png another.png");
}
} fred;
}
AnimationModule::AnimationModule(std::string params) {
frame = 0;
bad_filename = "";
if (params.size() >= 7 && params.substr(0,7) == "noframe" && (params.size() == 7 || params.substr(7,1) == " ")) {
no_frame = true;
params.erase(0,7);
if (params.size()) {
params.erase(0,1);
}
} else {
no_frame = false;
}
while (!params.empty()) {
for (unsigned int i = 0; i + 4 <= params.size(); ++i) {
if (params.substr(i,4) == ".png" && (i + 4 == params.size() || params[i+4] == ' ')) {
filenames.push_back(params.substr(0,i+4));
params.erase(0,i+5); //get space, if exists.
break;
}
}
}
}
Vector2f AnimationModule::size() {
if (frame < filenames.size() && filenames[frame] != bad_filename) {
Graphics::TextureRef tex = Graphics::get_texture(filenames[frame], false, false);
if (tex.ref) {
return make_vector(tex->w / float(tex->h), 1.0f);
} else {
bad_filename = filenames[frame];
}
}
return make_vector(1.0f, 1.0f);
}
void AnimationModule::draw(Box2f viewport, Box2f screen_viewport, float scale, unsigned int recurse) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glBoxToBox(viewport, screen_viewport);
Vector2f sz = size();
if (frame < filenames.size() && filenames[frame] != bad_filename) {
Graphics::TextureRef tex = Graphics::get_texture(filenames[frame], false, false);
if (tex.ref) {
glBindTexture(GL_TEXTURE_2D, tex->obj);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.5f * sz.x, -0.5f * sz.y);
glTexCoord2f(1.0f, 0.0f); glVertex2f( 0.5f * sz.x, -0.5f * sz.y);
glTexCoord2f(1.0f, 1.0f); glVertex2f( 0.5f * sz.x, 0.5f * sz.y);
glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.5f * sz.x, 0.5f * sz.y);
glEnd();
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
} else {
bad_filename = filenames[frame];
glBegin(GL_QUADS);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex2f(-0.5f * sz.x, -0.5f * sz.y);
glVertex2f( 0.5f * sz.x, -0.5f * sz.y);
glVertex2f( 0.5f * sz.x, 0.5f * sz.y);
glVertex2f(-0.5f * sz.x, 0.5f * sz.y);
glEnd();
}
}
if (!no_frame) {
glBegin(GL_LINE_LOOP);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(-0.5f * sz.x, -0.5f * sz.y);
glVertex2f( 0.5f * sz.x, -0.5f * sz.y);
glVertex2f( 0.5f * sz.x, 0.5f * sz.y);
glVertex2f(-0.5f * sz.x, 0.5f * sz.y);
glEnd();
}
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
Graphics::gl_errors("Animation::draw");
}
bool AnimationModule::get_pixel_size(Vector2ui &into) {
if (frame < filenames.size()) {
Graphics::TextureRef tex = Graphics::get_texture(filenames[frame], false, false);
if (tex.ref) {
into.x = tex->w;
into.y = tex->h;
return true;
}
}
into = make_vector(0U, 0U);
return false;
}
void AnimationModule::update(float elapsed_time) {
}
bool AnimationModule::handle_event(SDL_Event const &event, Vector2f local_mouse) {
if (event.type == SDL_MOUSEBUTTONDOWN) {
if (filenames.size()) {
if (event.button.button == SDL_BUTTON_LEFT) {
frame = frame + 1;
if (frame >= filenames.size()) frame = filenames.size() - 1;
return true;
}
if (event.button.button == SDL_BUTTON_RIGHT) {
if (frame > 0) frame = frame - 1;
return true;
}
}
}
return false;
}