forked from albertvaka/nbgf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_drawraw.h
216 lines (178 loc) · 5.83 KB
/
window_drawraw.h
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
#pragma once
#include "SDL_gpu.h"
namespace Window {
extern GPU_Target* currentDrawTarget;
namespace DrawRaw {
const unsigned short MAX_VERTICES = 60000; //From SDL_GPU's GPU_BLIT_BUFFER_ABSOLUTE_MAX_VERTICES
const unsigned int MAX_INDICES = (MAX_VERTICES / 4) * 6; //6 indices for each 4 vertices (quads drawn as 2 triangles)
const unsigned int MAX_COMPONENTS_PER_VERTEX = 8; // Max of GPU_BATCH_XY_ST (4), GPU_BATCH_XY_RGB (5) and GPU_BATCH_XY_ST_RGBA (8)
extern unsigned short vertex_count;
extern unsigned int index_count;
extern float vertices[MAX_VERTICES * MAX_COMPONENTS_PER_VERTEX];
extern unsigned short indices[MAX_INDICES];
inline void FlushTexturedQuads(GPU_Image* t) {
//Debug::out << "vertices:" << vertex_count << " indices:" << index_count;
GPU_TriangleBatch(t, Window::currentDrawTarget, vertex_count, vertices, index_count, indices, GPU_BATCH_XY_ST);
vertex_count = 0;
index_count = 0;
}
inline void FlushRGBQuads() {
//Debug::out << "vertices:" << vertex_count << " indices:" << index_count;
GPU_TriangleBatch(NULL, Window::currentDrawTarget, vertex_count, vertices, index_count, indices, GPU_BATCH_XY_RGB);
vertex_count = 0;
index_count = 0;
}
inline void FlushColoredTexturedQuads(GPU_Image* t) {
//Debug::out << "vertices:" << vertex_count << " indices:" << index_count;
GPU_TriangleBatch(t, Window::currentDrawTarget, vertex_count, vertices, index_count, indices, GPU_BATCH_XY_ST_RGBA);
vertex_count = 0;
index_count = 0;
}
//Colors and texture coords between 0 and 1
inline void BatchColoredTexturedQuad(GPU_Image* t, float x, float y, float w, float h, const GPU_Rect& tr, float r, float g, float b, float a)
{
const int COMPONENTS_PER_VERTEX = 8; // GPU_BATCH_XY_ST_RGBA
unsigned int i = vertex_count * COMPONENTS_PER_VERTEX;
//bottom left
vertices[i++] = x;
vertices[i++] = y + h;
vertices[i++] = tr.x;
vertices[i++] = tr.y + tr.h;
vertices[i++] = r;
vertices[i++] = g;
vertices[i++] = b;
vertices[i++] = a;
//top left
vertices[i++] = x;
vertices[i++] = y;
vertices[i++] = tr.x;
vertices[i++] = tr.y;
vertices[i++] = r;
vertices[i++] = g;
vertices[i++] = b;
vertices[i++] = a;
//top right
vertices[i++] = x + w;
vertices[i++] = y;
vertices[i++] = tr.x + tr.w;
vertices[i++] = tr.y;
vertices[i++] = r;
vertices[i++] = g;
vertices[i++] = b;
vertices[i++] = a;
//bottom right
vertices[i++] = x + w;
vertices[i++] = y + h;
vertices[i++] = tr.x + tr.w;
vertices[i++] = tr.y + tr.h;
vertices[i++] = r;
vertices[i++] = g;
vertices[i++] = b;
vertices[i++] = a;
indices[index_count++] = vertex_count;
indices[index_count++] = vertex_count + 1;
indices[index_count++] = vertex_count + 2;
indices[index_count++] = vertex_count;
indices[index_count++] = vertex_count + 2;
indices[index_count++] = vertex_count + 3;
vertex_count += 4;
if (vertex_count + 4 >= MAX_VERTICES) {
// Flush what we have so we don't go over MAX_VERTICES
FlushColoredTexturedQuads(t);
};
}
//Texture coords between 0 and 1
inline void BatchTexturedQuad(GPU_Image* t, float x, float y, float w, float h, const GPU_Rect& tr)
{
const int COMPONENTS_PER_VERTEX = 4; // GPU_BATCH_XY_ST
unsigned int i = vertex_count * COMPONENTS_PER_VERTEX;
//bottom left
vertices[i++] = x;
vertices[i++] = y + h;
vertices[i++] = tr.x;
vertices[i++] = tr.y + tr.h;
//top left
vertices[i++] = x;
vertices[i++] = y;
vertices[i++] = tr.x;
vertices[i++] = tr.y;
//top right
vertices[i++] = x + w;
vertices[i++] = y;
vertices[i++] = tr.x + tr.w;
vertices[i++] = tr.y;
//bottom right
vertices[i++] = x + w;
vertices[i++] = y + h;
vertices[i++] = tr.x + tr.w;
vertices[i++] = tr.y + tr.h;
indices[index_count++] = vertex_count;
indices[index_count++] = vertex_count + 1;
indices[index_count++] = vertex_count + 2;
indices[index_count++] = vertex_count;
indices[index_count++] = vertex_count + 2;
indices[index_count++] = vertex_count + 3;
vertex_count += 4;
if (vertex_count + 4 >= MAX_VERTICES) {
// Flush what we have so we don't go over MAX_VERTICES
FlushTexturedQuads(t);
};
}
//Colors between 0 and 1
inline void BatchRGBQuad(float x, float y, float width, float height, float r, float g, float b)
{
const int COMPONENTS_PER_VERTEX = 5; // GPU_BATCH_XY_RGB
unsigned int i = vertex_count * COMPONENTS_PER_VERTEX;
//bottom left
vertices[i++] = x;
vertices[i++] = y + height;
vertices[i++] = r;
vertices[i++] = g;
vertices[i++] = b;
//top left
vertices[i++] = x;
vertices[i++] = y;
vertices[i++] = r;
vertices[i++] = g;
vertices[i++] = b;
//top right
vertices[i++] = x + width;
vertices[i++] = y;
vertices[i++] = r;
vertices[i++] = g;
vertices[i++] = b;
//bottom right
vertices[i++] = x + width;
vertices[i++] = y + height;
vertices[i++] = r;
vertices[i++] = g;
vertices[i++] = b;
indices[index_count++] = vertex_count;
indices[index_count++] = vertex_count + 1;
indices[index_count++] = vertex_count + 2;
indices[index_count++] = vertex_count;
indices[index_count++] = vertex_count + 2;
indices[index_count++] = vertex_count + 3;
vertex_count += 4;
if (vertex_count + 4 >= MAX_VERTICES) {
// Flush what we have so we don't go over MAX_VERTICES
FlushRGBQuads();
};
}
}
}
inline void FixTextureBleeding(GPU_Rect& tr) {
// I made a similar fix in SDL_GPU's BlitTransformX, but when drawing raw vertices it's not used so we need it here as well
const float e = 0.1f;
tr.x += e;
tr.y += e;
tr.w -= 2*e;
tr.h -= 2*e;
}
inline void RectToTextureCoordinates(const GPU_Image* i, GPU_Rect& tr) {
FixTextureBleeding(tr);
tr.x /= i->texture_w;
tr.y /= i->texture_h;
tr.w /= i->texture_w;
tr.h /= i->texture_h;
}