File: GroundDecalHandler.h

package info (click to toggle)
spring 98.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 41,928 kB
  • ctags: 60,665
  • sloc: cpp: 356,167; ansic: 39,434; python: 12,228; java: 12,203; awk: 5,856; sh: 1,719; xml: 997; perl: 405; php: 253; objc: 194; makefile: 72; sed: 2
file content (285 lines) | stat: -rw-r--r-- 6,007 bytes parent folder | download
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef GROUND_DECAL_HANDLER_H
#define GROUND_DECAL_HANDLER_H

#include <set>
#include <list>
#include <vector>
#include <string>

#include "Rendering/Env/IGroundDecalDrawer.h"
#include "System/float3.h"
#include "System/EventClient.h"
#include "Sim/Projectiles/ExplosionListener.h"

class CSolidObject;
class CUnit;
class CVertexArray;
struct SolidObjectGroundDecal;
struct SolidObjectDecalType;
struct S3DModel;

namespace Shader {
	struct IProgramObject;
}

struct TrackPart {
	TrackPart()
		: pos1(ZeroVector)
		, pos2(ZeroVector)
		, texPos(0.0f)
		, connected(false)
		, creationTime(0)
	{}
	float3 pos1;
	float3 pos2;
	float texPos;
	bool connected;
	unsigned int creationTime;
};

struct UnitTrackStruct {
	UnitTrackStruct(CUnit* owner)
		: owner(owner)
		, lastUpdate(0)
		, lifeTime(0)
		, alphaFalloff(0.0f)
		, lastAdded(NULL)
	{}

	CUnit* owner;

	unsigned int lastUpdate;
	unsigned int lifeTime;

	float alphaFalloff;

	TrackPart* lastAdded;
	std::list<TrackPart*> parts;
};

struct TrackToAdd {
	TrackToAdd()
		: tp(NULL)
		, unit(NULL)
		, ts(NULL)
	{}
	TrackPart* tp;
	CUnit* unit;
	UnitTrackStruct* ts;
};

struct TrackToClean {
	TrackToClean()
		: track(NULL)
		, tracks(NULL)
	{}
	TrackToClean(UnitTrackStruct* t, std::set<UnitTrackStruct*>* ts)
		: track(t)
		, tracks(ts)
	{}
	UnitTrackStruct* track;
	std::set<UnitTrackStruct*>* tracks;
};


struct SolidObjectGroundDecal {
	SolidObjectGroundDecal()
		: va(NULL)
		, owner(NULL)
		, gbOwner(NULL)
		, posx(0)
		, posy(0)
		, xsize(0)
		, ysize(0)
		, facing(-1)
		, pos(ZeroVector)
		, radius(0.0f)
		, alpha(1.0f)
		, alphaFalloff(1.0f)
	{}
	~SolidObjectGroundDecal();

	CVertexArray* va;
	CSolidObject* owner;
	GhostSolidObject* gbOwner;

	int posx;
	int posy;
	int xsize;
	int ysize;
	int facing;

	float3 pos;
	float radius;

	float alpha;
	float alphaFalloff;
};


class CGroundDecalHandler: public IGroundDecalDrawer, public CEventClient, public IExplosionListener
{
public:
	CGroundDecalHandler();
	virtual ~CGroundDecalHandler();

	virtual void Draw();
	virtual void Update() {}

	virtual void GhostCreated(CSolidObject* object, GhostSolidObject* gb);
	virtual void GhostDestroyed(GhostSolidObject* gb);

	virtual void RemoveSolidObject(CSolidObject* object, GhostSolidObject* gb);
	virtual void ForceRemoveSolidObject(CSolidObject* object);

private:
	void AddExplosion(float3 pos, float damage, float radius, bool);
	void MoveSolidObject(CSolidObject* object, const float3& pos);
	int GetSolidObjectDecalType(const std::string& name);
	int GetTrackType(const std::string& name);

public:
	//CEventClient
	bool WantsEvent(const std::string& eventName) {
		return 
			(eventName == "SunChanged") ||
			(eventName == "RenderUnitCreated") ||
			(eventName == "RenderUnitDestroyed") ||
			(eventName == "RenderUnitMoved") ||
			(eventName == "RenderFeatureCreated") ||
			(eventName == "RenderFeatureMoved") ||
			(eventName == "UnitLoaded") ||
			(eventName == "UnitUnloaded");
	}
	bool GetFullRead() const { return true; }
	int GetReadAllyTeam() const { return AllAccessTeam; }

	void SunChanged(const float3& sunDir);
	void RenderUnitCreated(const CUnit*, int cloaked);
	void RenderUnitDestroyed(const CUnit*);
	void RenderFeatureCreated(const CFeature* feature);
	void RenderFeatureMoved(const CFeature* feature, const float3& oldpos, const float3& newpos);
	void RenderUnitMoved(const CUnit* unit, const float3& newpos);
	void UnitLoaded(const CUnit* unit, const CUnit* transport);
	void UnitUnloaded(const CUnit* unit, const CUnit* transport);

	//IExplosionListener
	void ExplosionOccurred(const CExplosionEvent& event);

private:
	struct TrackType {
		TrackType()
			: texture(0)
		{}
		std::string name;
		std::set<UnitTrackStruct*> tracks;
		unsigned int texture;
	};

	struct SolidObjectDecalType {
		SolidObjectDecalType(): texture(0) {}

		std::string name;
		std::set<SolidObjectGroundDecal*> objectDecals;

		unsigned int texture;
	};

	struct Scar {
		Scar()
			: pos(ZeroVector)
			, radius(0.0f)
			, creationTime(0)
			, lifeTime(0)
			, alphaFalloff(0.0f)
			, startAlpha(1.0f)
			, texOffsetX(0.0f)
			, texOffsetY(0.0f)
			, x1(0), x2(0)
			, y1(0), y2(0)
			, basesize(0.0f)
			, overdrawn(0.0f)
			, lastTest(0)
			, va(NULL)
		{}
		~Scar();

		float3 pos;
		float radius;
		int creationTime;
		int lifeTime;
		float alphaFalloff;
		float startAlpha;
		float texOffsetX;
		float texOffsetY;

		int x1, x2;
		int y1, y2;

		float basesize;
		float overdrawn;

		int lastTest;
		CVertexArray* va;
	};

	void LoadDecalShaders();
	void DrawObjectDecals();

	void AddTracks();
	void DrawTracks();
	void CleanTracks();

	void AddScars();
	void DrawScars();

	void GatherDecalsForType(SolidObjectDecalType* decalType);
	void AddDecalAndTrack(CUnit* unit, const float3& newPos);

private:
	unsigned int scarTex;
	bool groundScarAlphaFade;

	std::vector<SolidObjectDecalType*> objectDecalTypes;
	std::vector<TrackType*> trackTypes;

	enum DecalShaderProgram {
		DECAL_SHADER_ARB,
		DECAL_SHADER_GLSL,
		DECAL_SHADER_CURR,
		DECAL_SHADER_LAST
	};

	std::vector<Shader::IProgramObject*> decalShaders;
	std::vector<SolidObjectGroundDecal*> decalsToDraw;

	std::list<Scar*> scars;
	std::vector<Scar*> scarsToBeAdded;
	std::vector<Scar*> scarsToBeChecked;

	std::vector<TrackToAdd> tracksToBeAdded;
	std::vector<TrackToClean> tracksToBeCleaned;
	std::vector<UnitTrackStruct*> tracksToBeDeleted;

	int lastTest;
	float maxOverlap;

	std::set<Scar*>* scarField;
	int scarFieldX;
	int scarFieldY;

	void DrawObjectDecal(SolidObjectGroundDecal* decal);
	void DrawGroundScar(Scar* scar, bool fade);

	int OverlapSize(Scar* s1, Scar* s2);
	void TestOverlaps(Scar* scar);
	void RemoveScar(Scar* scar, bool removeFromScars);
	unsigned int LoadTexture(const std::string& name);
	void LoadScar(const std::string& file, unsigned char* buf, int xoffset, int yoffset);
};

extern CGroundDecalHandler* groundDecals_;

#endif // GROUND_DECAL_HANDLER_H