This repository was archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 587
Expand file tree
/
Copy pathUIBatch.cpp
More file actions
executable file
·229 lines (185 loc) · 7.45 KB
/
Copy pathUIBatch.cpp
File metadata and controls
executable file
·229 lines (185 loc) · 7.45 KB
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
//
// Copyright (c) 2008-2014 the Urho3D project.
// Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#include "Precompiled.h"
#include "../Graphics/Graphics.h"
#include "../Math/Matrix3x4.h"
#include "../Graphics/ShaderVariation.h"
#include "../Graphics/Texture.h"
#include "../UI/UIBatch.h"
#include "../DebugNew.h"
namespace Atomic
{
#ifdef ATOMIC_OPENGL
static const float posAdjust = 0.0f;
#else
static const float posAdjust = 0.5f;
#endif
static const Vector3 posAdjustVec(posAdjust, posAdjust, 0.0f);
UIBatch::UIBatch() :
blendMode_(BLEND_REPLACE),
texture_(0),
invTextureSize_(Vector2::ONE),
vertexData_(0),
vertexStart_(0),
vertexEnd_(0)
{
SetDefaultColor();
}
UIBatch::UIBatch(BlendMode blendMode, const IntRect& scissor, Texture* texture, PODVector<float>* vertexData) :
blendMode_(blendMode),
scissor_(scissor),
texture_(texture),
invTextureSize_(texture ? Vector2(1.0f / (float)texture->GetWidth(), 1.0f / (float)texture->GetHeight()) : Vector2::ONE),
vertexData_(vertexData),
vertexStart_(vertexData->Size()),
vertexEnd_(vertexData->Size())
{
SetDefaultColor();
}
void UIBatch::SetColor(const Color& color, bool overrideAlpha)
{
useGradient_ = false;
color_ = color.ToUInt();
}
void UIBatch::SetDefaultColor()
{
color_ = 0xffffffff;
useGradient_ = false;
}
void UIBatch::AddQuad(int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight)
{
// If alpha is 0, nothing will be rendered, so do not add the quad
if (!(color_ & 0xff000000))
return;
//const IntVector2& screenPos = element_->GetScreenPosition();
IntVector2 screenPos(0, 0);
float left = (float)(x + screenPos.x_) - posAdjust;
float right = left + (float)width;
float top = (float)(y + screenPos.y_) - posAdjust;
float bottom = top + (float)height;
float leftUV = texOffsetX * invTextureSize_.x_;
float topUV = texOffsetY * invTextureSize_.y_;
float rightUV = (texOffsetX + (texWidth ? texWidth : width)) * invTextureSize_.x_;
float bottomUV = (texOffsetY + (texHeight ? texHeight : height)) * invTextureSize_.y_;
unsigned begin = vertexData_->Size();
vertexData_->Resize(begin + 6 * UI_VERTEX_SIZE);
float* dest = &(vertexData_->At(begin));
vertexEnd_ = vertexData_->Size();
dest[0] = left; dest[1] = top; dest[2] = 0.0f;
((unsigned&)dest[3]) = color_;
dest[4] = leftUV; dest[5] = topUV;
dest[6] = right; dest[7] = top; dest[8] = 0.0f;
((unsigned&)dest[9]) = color_;
dest[10] = rightUV; dest[11] = topUV;
dest[12] = left; dest[13] = bottom; dest[14] = 0.0f;
((unsigned&)dest[15]) = color_;
dest[16] = leftUV; dest[17] = bottomUV;
dest[18] = right; dest[19] = top; dest[20] = 0.0f;
((unsigned&)dest[21]) = color_;
dest[22] = rightUV; dest[23] = topUV;
dest[24] = right; dest[25] = bottom; dest[26] = 0.0f;
((unsigned&)dest[27]) = color_;
dest[28] = rightUV; dest[29] = bottomUV;
dest[30] = left; dest[31] = bottom; dest[32] = 0.0f;
((unsigned&)dest[33]) = color_;
dest[34] = leftUV; dest[35] = bottomUV;
dest += 36;
}
void UIBatch::AddQuad(const Matrix3x4& transform, int x, int y, int width, int height, int texOffsetX, int texOffsetY,
int texWidth, int texHeight)
{
Vector3 v1 = (transform * Vector3((float)x, (float)y, 0.0f)) - posAdjustVec;
Vector3 v2 = (transform * Vector3((float)x + (float)width, (float)y, 0.0f)) - posAdjustVec;
Vector3 v3 = (transform * Vector3((float)x, (float)y + (float)height, 0.0f)) - posAdjustVec;
Vector3 v4 = (transform * Vector3((float)x + (float)width, (float)y + (float)height, 0.0f)) - posAdjustVec;
float leftUV = ((float)texOffsetX) * invTextureSize_.x_;
float topUV = ((float)texOffsetY) * invTextureSize_.y_;
float rightUV = ((float)(texOffsetX + (texWidth ? texWidth : width))) * invTextureSize_.x_;
float bottomUV = ((float)(texOffsetY + (texHeight ? texHeight : height))) * invTextureSize_.y_;
unsigned begin = vertexData_->Size();
vertexData_->Resize(begin + 6 * UI_VERTEX_SIZE);
float* dest = &(vertexData_->At(begin));
vertexEnd_ = vertexData_->Size();
dest[0] = v1.x_; dest[1] = v1.y_; dest[2] = 0.0f;
((unsigned&)dest[3]) = color_;
dest[4] = leftUV; dest[5] = topUV;
dest[6] = v2.x_; dest[7] = v2.y_; dest[8] = 0.0f;
((unsigned&)dest[9]) = color_;
dest[10] = rightUV; dest[11] = topUV;
dest[12] = v3.x_; dest[13] = v3.y_; dest[14] = 0.0f;
((unsigned&)dest[15]) = color_;
dest[16] = leftUV; dest[17] = bottomUV;
dest[18] = v2.x_; dest[19] = v2.y_; dest[20] = 0.0f;
((unsigned&)dest[21]) = color_;
dest[22] = rightUV; dest[23] = topUV;
dest[24] = v4.x_; dest[25] = v4.y_; dest[26] = 0.0f;
((unsigned&)dest[27]) = color_;
dest[28] = rightUV; dest[29] = bottomUV;
dest[30] = v3.x_; dest[31] = v3.y_; dest[32] = 0.0f;
((unsigned&)dest[33]) = color_;
dest[34] = leftUV; dest[35] = bottomUV;
}
void UIBatch::AddQuad(int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight, bool tiled)
{
if (!tiled)
{
AddQuad(x, y, width, height, texOffsetX, texOffsetY, texWidth, texHeight);
return;
}
int tileX = 0;
int tileY = 0;
int tileW = 0;
int tileH = 0;
while (tileY < height)
{
tileX = 0;
tileH = Min(height - tileY, texHeight);
while (tileX < width)
{
tileW = Min(width - tileX, texWidth);
AddQuad(x + tileX, y + tileY, tileW, tileH, texOffsetX, texOffsetY, tileW, tileH);
tileX += tileW;
}
tileY += tileH;
}
}
bool UIBatch::Merge(const UIBatch& batch)
{
if (batch.blendMode_ != blendMode_ ||
batch.scissor_ != scissor_ ||
batch.texture_ != texture_ ||
batch.vertexData_ != vertexData_ ||
batch.vertexStart_ != vertexEnd_)
return false;
vertexEnd_ = batch.vertexEnd_;
return true;
}
void UIBatch::AddOrMerge(const UIBatch& batch, PODVector<UIBatch>& batches)
{
if (batch.vertexEnd_ == batch.vertexStart_)
return;
if (!batches.Empty() && batches.Back().Merge(batch))
return;
batches.Push(batch);
}
}