Fix for coverity #29401
[supertux.git] / src / object / sprite_particle.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "object/camera.hpp"
19 #include "sprite/sprite.hpp"
20 #include "sprite/sprite_manager.hpp"
21 #include "object/sprite_particle.hpp"
22 #include "supertux/globals.hpp"
23 #include "supertux/sector.hpp"
24
25 #include <stdexcept>
26
27 SpriteParticle::SpriteParticle(std::string sprite_name, std::string action,
28                                Vector position_, AnchorPoint anchor, Vector velocity_, Vector acceleration_,
29                                int drawing_layer_) :
30   sprite(),
31   position(position_),
32   velocity(velocity_),
33   acceleration(acceleration_),
34   drawing_layer(drawing_layer_),
35   light(0.0f,0.0f,0.0f),
36   lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-tiny.sprite")),
37   glow(false)
38 {
39   sprite = SpriteManager::current()->create(sprite_name);
40   if (!sprite.get()) throw std::runtime_error("Could not load sprite "+sprite_name);
41   sprite->set_action(action, 1);
42   sprite->set_animation_loops(1); //TODO: this is necessary because set_action will not set "loops" when "action" is the default action
43
44   this->position -= get_anchor_pos(sprite->get_current_hitbox(), anchor);
45
46   if(sprite_name=="images/objects/particles/sparkle.sprite") {
47     glow = true;
48     if(action=="dark") {
49       lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
50       lightsprite->set_color(Color(0.1f, 0.1f, 0.1f));
51     }
52   }
53 }
54
55 SpriteParticle::~SpriteParticle()
56 {
57   remove_me();
58 }
59
60 void
61 SpriteParticle::hit(Player& )
62 {
63 }
64
65 void
66 SpriteParticle::update(float elapsed_time)
67 {
68   // die when animation is complete
69   if (sprite->animation_done()) {
70     remove_me();
71     return;
72   }
73
74   // calculate new position and velocity
75   position.x += velocity.x * elapsed_time;
76   position.y += velocity.y * elapsed_time;
77   velocity.x += acceleration.x * elapsed_time;
78   velocity.y += acceleration.y * elapsed_time;
79
80   // die when too far offscreen
81   Vector camera = Sector::current()->camera->get_translation();
82   if ((position.x < camera.x - 128) || (position.x > SCREEN_WIDTH + camera.x + 128) ||
83       (position.y < camera.y - 128) || (position.y > SCREEN_HEIGHT + camera.y + 128)) {
84     remove_me();
85     return;
86   }
87 }
88
89 void
90 SpriteParticle::draw(DrawingContext& context)
91 {
92   sprite->draw(context, position, drawing_layer);
93
94   //Sparkles glow in the dark
95   if(glow){
96     context.get_light(position, &light );
97     if (light.red + light.green + light.blue < 3.0){
98       context.push_target();
99       context.set_target(DrawingContext::LIGHTMAP);
100       sprite->draw(context, position, drawing_layer);
101       lightsprite->draw(context, position + Vector(12,12), 0);
102       context.pop_target();
103     }
104   }
105
106 }
107
108 /* EOF */