File: LineDrawer.h

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (154 lines) | stat: -rw-r--r-- 3,573 bytes parent folder | download | duplicates (2)
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef _LINE_DRAWER_H
#define _LINE_DRAWER_H

#include <array>
#include <vector>
#include <array>

#include "Game/UI/CursorIcons.h"
#include "Rendering/GL/VertexArrayTypes.h"
#include "System/Color.h"

class CLineDrawer {
public:
	CLineDrawer() {
		for (size_t i = 0; i < regularLines.size(); i++) {
			regularLines[i].reserve(64);
			stippleLines[i].reserve(64);
		}
	}

	enum LineWidth {
		Default   = 0,
		QueuedCmd = 1
	};


	void Configure(
		bool useColorRestarts,
		bool useRestartColor,
		const float* restartColor,
		float restartAlpha
	);
	void SetWidth(LineWidth w) { width = w; }

	void DrawAll(bool onMiniMap);
	void SetupLineStipple();
	void UpdateLineStipple();

	void StartPath(const float3& pos, const float* color);
	void DrawLine(const float3& endPos, const float* color);
	void DrawLineAndIcon(int cmdID, const float3& endPos, const float* color);
	void DrawIconAtLastPos(int cmdID);
	void Break(const float3& endPos, const float* color);
	void Restart();
	/// same as restart; only way for this to work would be using glGet so it's left broken
	void RestartSameColor() { Restart(); }
	void RestartWithColor(const float* color);

	const float3& GetLastPos() const { return lastPos; }

	bool HaveRegularLines() const { for (auto& v: regularLines) { if (!v.empty()) return true; } return false; }
	bool HaveStippleLines() const { for (auto& v: stippleLines) { if (!v.empty()) return true; } return false; }

private:
	bool lineStipple = false;
	bool useColorRestarts = false;
	bool useRestartColor = false;

	float restartAlpha = 0.0f;
	float stippleTimer = 0.0f;

	LineWidth width = Default;

	float3 lastPos;

	const float* restartColor = nullptr;
	const float* lastColor = nullptr;

	typedef std::vector<VA_TYPE_C> Line;

	// queue all lines and draw them in one go later
	// even := GL_LINE_LOOP, odd := GL_LINES (useColorRestarts)
	// width:
	// [0,1] := 1.0f or 2.5f on minimap
	// [2,3] := cmdColors.QueuedLineWidth() or 2.5f on minimap

	std::array<std::vector<Line>, 4> regularLines;
	std::array<std::vector<Line>, 4> stippleLines;
};


extern CLineDrawer lineDrawer;


/******************************************************************************/
//
//  Inlines
//

inline void CLineDrawer::Configure(bool ucr, bool urc, const float* rc, float ra)
{
	restartAlpha = ra;
	restartColor = rc;

	useRestartColor = urc;
	useColorRestarts = ucr;
}


inline void CLineDrawer::Break(const float3& endPos, const float* color)
{
	lastPos = endPos;
	lastColor = color;
}


inline void CLineDrawer::RestartWithColor(const float *color)
{
	lastColor = color;
	Restart();
}


inline void CLineDrawer::StartPath(const float3& pos, const float* color)
{
	lastPos = pos;
	lastColor = color;
	Restart();
}


inline void CLineDrawer::DrawLine(const float3& endPos, const float* color)
{
	const int idx = width * 2 + useColorRestarts;
	Line& line = (lineStipple)? stippleLines[idx].back(): regularLines[idx].back();

	if (!useColorRestarts) {
		line.push_back({endPos, color});
	} else {
		line.push_back({lastPos, useRestartColor? restartColor: SColor{color[0], color[1], color[2], color[3] * restartAlpha}});
		line.push_back({endPos, color});
	}

	lastPos = endPos;
	lastColor = color;
}


inline void CLineDrawer::DrawLineAndIcon(int cmdID, const float3& endPos, const float* color)
{
	cursorIcons.AddIcon(cmdID, endPos);
	DrawLine(endPos, color);
}


inline void CLineDrawer::DrawIconAtLastPos(int cmdID)
{
	cursorIcons.AddIcon(cmdID, lastPos);
}


#endif // _LINE_DRAWER_H