summaryrefslogtreecommitdiff
path: root/src/render.cpp
diff options
context:
space:
mode:
authorPaweł Redman <pawel.redman@gmail.com>2017-10-14 15:34:53 +0200
committerPaweł Redman <pawel.redman@gmail.com>2017-10-14 15:34:53 +0200
commitbb0a7bfe31714ff56ce1c1ce428cdf240804db14 (patch)
treeb5c5137f80443343e9d46bda3c7b96c777142181 /src/render.cpp
parentb0edfd3fa2ad078677d43b743fdb78e5af52e99a (diff)
Oriented sprites and the human sprite.
Diffstat (limited to 'src/render.cpp')
-rw-r--r--src/render.cpp110
1 files changed, 102 insertions, 8 deletions
diff --git a/src/render.cpp b/src/render.cpp
index 08a4524..f56cb37 100644
--- a/src/render.cpp
+++ b/src/render.cpp
@@ -94,26 +94,46 @@ void state_t::render(game::state_t *game)
draw_sector(window, sector);
for (world::entity_t *ent : game->world.get_entities(bbox))
- ent->render(this);
+ ent->render_to(this);
}
-void state_t::render(animated_texture_t *anim, sf::FloatRect bounds)
-{
+void state_t::render(animated_texture_t *anim, sf::FloatRect bounds, bool mirror){
size_t frame;
+ if (!anim->frame_count)
+ return;
+
frame = floor(fmod(now * 20.0, anim->frame_count));
wot_rect.setTexture(anim->frames + frame, true);
wot_rect.setFillColor(sf::Color::White);
- wot_rect.setPosition(bounds.left, bounds.top);
- wot_rect.setSize(sf::Vector2f(bounds.width, bounds.height));
+
+ if (!mirror) {
+ wot_rect.setPosition(bounds.left, bounds.top);
+ wot_rect.setSize(sf::Vector2f(bounds.width, bounds.height));
+ } else {
+ wot_rect.setPosition(bounds.left + bounds.width, bounds.top);
+ wot_rect.setSize(sf::Vector2f(bounds.width, bounds.height));
+ wot_rect.setScale(sf::Vector2f(-1, 1));
+ }
+
window->draw(wot_rect);
wot_rect.setTexture(NULL);
+ wot_rect.setScale(sf::Vector2f(1, 1));
+}
+
+void state_t::render(oriented_sprite_t *sprite, sf::FloatRect bounds, float angle)
+{
+ size_t index;
+ bool mirror;
+
+ index = sprite->select_index(angle, &mirror);
+ render(sprite->textures + index, bounds, mirror);
}
animated_texture_t::~animated_texture_t(void)
{
- //delete[] frames;
+ delete[] frames;
}
bool animated_texture_t::load(std::string prefix, size_t frame_count_)
@@ -128,8 +148,8 @@ bool animated_texture_t::load(std::string prefix, size_t frame_count_)
std::cout << "load " << path << "\n";
if (!frames[i].loadFromFile(path)) {
- // FIXME
- //delete[] frames;
+ delete[] frames;
+ frames = NULL;
return false;
}
}
@@ -137,4 +157,78 @@ bool animated_texture_t::load(std::string prefix, size_t frame_count_)
return true;
}
+oriented_sprite_t::~oriented_sprite_t(void)
+{
+ delete[] textures;
+}
+
+float normalize_angle(float angle)
+{
+ float t;
+
+ t = angle / (2 * M_PI);
+ t -= floor(t);
+
+ return t * 2 * M_PI;
+}
+
+size_t oriented_sprite_4M_t::select_index(float angle, bool *mirror)
+{
+ angle = normalize_angle(angle);
+
+ if (angle < 0.25f * M_PI) {
+ select_x:
+ *mirror = false;
+ return 0;
+ } else if (angle < 0.75f * M_PI) {
+ *mirror = false;
+ return 1;
+ } else if (angle < 1.25f * M_PI) {
+ *mirror = true;
+ return 0;
+ } else if (angle < 1.75f * M_PI) {
+ *mirror = false;
+ return 2;
+ } else
+ goto select_x;
+}
+
+void oriented_sprite_4M_t::load(std::string prefix, size_t xc, size_t yc, size_t nyc)
+{
+ textures = new animated_texture_t[3];
+
+ textures[0].load(prefix + "_x_", xc);
+ textures[1].load(prefix + "_y_", yc);
+ textures[2].load(prefix + "_ny_", nyc);
+}
+
+size_t oriented_sprite_4M2_t::select_index(float angle, bool *mirror)
+{
+ angle = normalize_angle(angle);
+
+ if (angle < 0.25f * M_PI) {
+ select_x:
+ *mirror = false;
+ return 0;
+ } else if (angle < 0.75f * M_PI) {
+ *mirror = false;
+ return 1;
+ } else if (angle < 1.25f * M_PI) {
+ *mirror = true;
+ return 0;
+ } else if (angle < 1.75f * M_PI) {
+ *mirror = false;
+ return 1;
+ } else
+ goto select_x;
+}
+
+void oriented_sprite_4M2_t::load(std::string prefix, size_t xc, size_t yc)
+{
+ textures = new animated_texture_t[2];
+
+ textures[0].load(prefix + "_x_", xc);
+ textures[1].load(prefix + "_y_", yc);
+}
+
} // namespace render