]> git.lizzy.rs Git - minetest.git/blob - src/particles.h
Fix memory leaks due to messed up memory handling for particles as well as their...
[minetest.git] / src / particles.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef PARTICLES_HEADER
21 #define PARTICLES_HEADER
22
23 #define DIGGING_PARTICLES_AMOUNT 10
24
25 #include <iostream>
26 #include "irrlichttypes_extrabloated.h"
27 #include "tile.h"
28 #include "localplayer.h"
29 #include "environment.h"
30
31 struct ClientEvent;
32 class ParticleManager;
33
34 class Particle : public scene::ISceneNode
35 {
36         public:
37         Particle(
38                 IGameDef* gamedef,
39                 scene::ISceneManager* mgr,
40                 LocalPlayer *player,
41                 ClientEnvironment *env,
42                 v3f pos,
43                 v3f velocity,
44                 v3f acceleration,
45                 float expirationtime,
46                 float size,
47                 bool collisiondetection,
48                 bool vertical,
49                 video::ITexture *texture,
50                 v2f texpos,
51                 v2f texsize
52         );
53         ~Particle();
54
55         virtual const core::aabbox3d<f32>& getBoundingBox() const
56         {
57                 return m_box;
58         }
59
60         virtual u32 getMaterialCount() const
61         {
62                 return 1;
63         }
64
65         virtual video::SMaterial& getMaterial(u32 i)
66         {
67                 return m_material;
68         }
69
70         virtual void OnRegisterSceneNode();
71         virtual void render();
72
73         void step(float dtime);
74
75         bool get_expired ()
76         { return m_expiration < m_time; }
77
78 private:
79         void updateLight();
80         void updateVertices();
81
82         video::S3DVertex m_vertices[4];
83         float m_time;
84         float m_expiration;
85
86         ClientEnvironment *m_env;
87         IGameDef *m_gamedef;
88         core::aabbox3d<f32> m_box;
89         core::aabbox3d<f32> m_collisionbox;
90         video::SMaterial m_material;
91         v2f m_texpos;
92         v2f m_texsize;
93         v3f m_pos;
94         v3f m_velocity;
95         v3f m_acceleration;
96         LocalPlayer *m_player;
97         float m_size;
98         u8 m_light;
99         bool m_collisiondetection;
100         bool m_vertical;
101         v3s16 m_camera_offset;
102 };
103
104 class ParticleSpawner
105 {
106         public:
107         ParticleSpawner(IGameDef* gamedef,
108                 scene::ISceneManager *smgr,
109                 LocalPlayer *player,
110                 u16 amount,
111                 float time,
112                 v3f minp, v3f maxp,
113                 v3f minvel, v3f maxvel,
114                 v3f minacc, v3f maxacc,
115                 float minexptime, float maxexptime,
116                 float minsize, float maxsize,
117                 bool collisiondetection,
118                 bool vertical,
119                 video::ITexture *texture,
120                 u32 id,
121                 ParticleManager* p_manager);
122
123         ~ParticleSpawner();
124
125         void step(float dtime, ClientEnvironment *env);
126
127         bool get_expired ()
128         { return (m_amount <= 0) && m_spawntime != 0; }
129
130         private:
131         ParticleManager* m_particlemanager;
132         float m_time;
133         IGameDef *m_gamedef;
134         scene::ISceneManager *m_smgr;
135         LocalPlayer *m_player;
136         u16 m_amount;
137         float m_spawntime;
138         v3f m_minpos;
139         v3f m_maxpos;
140         v3f m_minvel;
141         v3f m_maxvel;
142         v3f m_minacc;
143         v3f m_maxacc;
144         float m_minexptime;
145         float m_maxexptime;
146         float m_minsize;
147         float m_maxsize;
148         video::ITexture *m_texture;
149         std::vector<float> m_spawntimes;
150         bool m_collisiondetection;
151         bool m_vertical;
152
153 };
154
155 /**
156  * Class doing particle as well as their spawners handling
157  */
158 class ParticleManager
159 {
160 friend class ParticleSpawner;
161 public:
162         ParticleManager(ClientEnvironment* env);
163         ~ParticleManager();
164
165         void step (float dtime);
166
167         void handleParticleEvent(ClientEvent *event,IGameDef *gamedef,
168                         scene::ISceneManager* smgr, LocalPlayer *player);
169
170         void addDiggingParticles(IGameDef* gamedef, scene::ISceneManager* smgr,
171                 LocalPlayer *player, v3s16 pos, const TileSpec tiles[]);
172
173         void addPunchingParticles(IGameDef* gamedef, scene::ISceneManager* smgr,
174                 LocalPlayer *player, v3s16 pos, const TileSpec tiles[]);
175
176         void addNodeParticle(IGameDef* gamedef, scene::ISceneManager* smgr,
177                 LocalPlayer *player, v3s16 pos, const TileSpec tiles[]);
178
179 protected:
180         void addParticle(Particle* toadd);
181
182 private:
183
184         void stepParticles (float dtime);
185         void stepSpawners (float dtime);
186
187         void clearAll ();
188
189         std::vector<Particle*> m_particles;
190         std::map<u32, ParticleSpawner*> m_particle_spawners;
191
192         ClientEnvironment* m_env;
193         JMutex m_particle_list_lock;
194         JMutex m_spawner_list_lock;
195 };
196
197 #endif