-
Notifications
You must be signed in to change notification settings - Fork 2
/
render.cpp
290 lines (219 loc) · 6.49 KB
/
render.cpp
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
286
287
288
289
290
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
#include "render.h"
#include "config\config.h"
Renderer render;
SurfaceRender surface_render;
void SurfaceRender::set_alphafactor(float factor)
{
alpha_factor = factor;
}
float SurfaceRender::get_alphafactor()
{
return alpha_factor;
}
void SurfaceRender::rect(int x, int y, int w, int h, Color color)
{
if (!surface)
return;
color.SetAlpha(static_cast<int>(color.a() * alpha_factor));
surface->DrawSetColor(color);
surface->DrawOutlinedRect(x, y, x + w, y + h);
}
void SurfaceRender::rect_filled(int x, int y, int w, int h, Color color)
{
if (!surface)
return;
color.SetAlpha(static_cast<int>(color.a() * alpha_factor));
surface->DrawSetColor(color);
surface->DrawFilledRect(x, y, x + w, y + h);
}
void SurfaceRender::rounded_box(int x, int y, int w, int h, int points, int radius, Color color)
{
if (!surface)
return;
color.SetAlpha(static_cast<int>(color.a() * alpha_factor));
Vertex_t* round = new Vertex_t[4 * points]; //-V121
for (int i = 0; i < 4; i++) {
int _x = x + ((i < 2) ? (w - radius) : radius);
int _y = y + ((i % 3) ? (h - radius) : radius);
float a = 90.f * i;
for (int j = 0; j < points; j++) {
float _a = DirectX::XMConvertToRadians(a + (j / (float)(points - 1)) * 90.f);
round[(i * points) + j] = Vertex_t(Vector2D(_x + radius * sin(_a), _y - radius * cos(_a)));
}
}
static int Texture = surface->CreateNewTextureID(true);
unsigned char buffer[4] = { 255, 255, 255, 255 };
surface->DrawSetTextureRGBA(Texture, buffer, 1, 1);
surface->DrawSetColor(color);
surface->DrawSetTexture(Texture);
surface->DrawTexturedPolygon(4 * points, round);
}
void SurfaceRender::circle(int x, int y, int points, int radius, Color color)
{
if (!surface)
return;
color.SetAlpha(static_cast<int>(color.a() * alpha_factor));
static bool once = true;
static std::vector<float> temppointsx;
static std::vector<float> temppointsy;
if (once)
{
float step = (float)DirectX::XM_PI * 2.0f / points;
for (float a = 0; a < (DirectX::XM_PI * 2.0f); a += step) //-V1034
{
temppointsx.push_back(cosf(a));
temppointsy.push_back(sinf(a));
}
once = false;
}
std::vector<int> pointsx;
std::vector<int> pointsy;
std::vector<Vertex_t> vertices;
for (int i = 0; i < temppointsx.size(); i++)
{
float eeks = radius * temppointsx[i] + x;
float why = radius * temppointsy[i] + y;
pointsx.push_back(eeks);
pointsy.push_back(why);
vertices.emplace_back(Vertex_t(Vector2D(eeks, why)));
}
surface->DrawSetColor(color);
surface->DrawPolyLine(pointsx.data(), pointsy.data(), points); // only if you want en extra outline
}
void SurfaceRender::circle_filled(int x, int y, int points, int radius, Color color)
{
if (!surface)
return;
color.SetAlpha(static_cast<int>(color.a() * alpha_factor));
static bool once = true;
static std::vector<float> temppointsx;
static std::vector<float> temppointsy;
if (once)
{
float step = (float)DirectX::XM_PI * 2.0f / points;
for (float a = 0; a < (DirectX::XM_PI * 2.0f); a += step) //-V1034
{
temppointsx.push_back(cosf(a));
temppointsy.push_back(sinf(a));
}
once = false;
}
std::vector<int> pointsx;
std::vector<int> pointsy;
std::vector<Vertex_t> vertices;
for (int i = 0; i < temppointsx.size(); i++)
{
float eeks = radius * temppointsx[i] + x;
float why = radius * temppointsy[i] + y;
pointsx.push_back(eeks);
pointsy.push_back(why);
vertices.emplace_back(Vertex_t(Vector2D(eeks, why)));
}
surface->DrawSetColor(color);
surface->DrawTexturedPolygon(points, vertices.data());
}
void SurfaceRender::triangle(Vector2D point_one, Vector2D point_two, Vector2D point_three, Color color)
{
if (!surface)
return;
color.SetAlpha(static_cast<int>(color.a() * alpha_factor));
Vertex_t verts[3] = {
Vertex_t(point_one),
Vertex_t(point_two),
Vertex_t(point_three)
};
static int texture = surface->CreateNewTextureID(true);
unsigned char buffer[4] = { 255, 255, 255, 255 };
surface->DrawSetTextureRGBA(texture, buffer, 1, 1);
surface->DrawSetColor(color);
surface->DrawSetTexture(texture);
surface->DrawTexturedPolygon(3, verts);
}
void SurfaceRender::line(int x, int y, int x2, int y2, Color color)
{
if (!surface)
return;
color.SetAlpha(static_cast<int>(color.a() * alpha_factor));
surface->DrawSetColor(color);
surface->DrawLine(x, y, x2, y2);
}
void SurfaceRender::text(vgui::HFont font, int x, int y, Color color, DWORD flags, const char* msg, ...)
{
if (!surface)
return;
color.SetAlpha(static_cast<int>(color.a() * alpha_factor));
va_list va_alist;
char buffer[1024];
va_start(va_alist, msg);
_vsnprintf(buffer, sizeof(buffer), msg, va_alist);
va_end(va_alist);
wchar_t wbuf[1024];
MultiByteToWideChar(CP_UTF8, 0, buffer, 256, wbuf, 256);
int width, height;
surface->GetTextSize(font, wbuf, width, height);
if (!(flags & HFONT_CENTERED_NONE))
{
if (flags & HFONT_CENTERED_X)
x -= width * 0.5f;
if (flags & HFONT_CENTERED_Y)
y -= height * 0.5f;
}
surface->DrawSetTextFont(font);
surface->DrawSetTextColor(color);
surface->DrawSetTextPos(x, y);
surface->DrawPrintText(wbuf, wcslen(wbuf));
}
void SurfaceRender::wtext(vgui::HFont font, int x, int y, Color color, DWORD flags, wchar_t* msg, ...)
{
if (!surface)
return;
color.SetAlpha(static_cast<int>(color.a() * alpha_factor));
wchar_t buffer[256];
va_list args;
va_start(args, msg);
wvsprintfW(buffer, msg, args);
va_end(args);
int width, height;
surface->GetTextSize(font, buffer, width, height);
if (!(flags & HFONT_CENTERED_NONE)) {
if (flags & HFONT_CENTERED_X)
x -= width * 0.5f;
if (flags & HFONT_CENTERED_Y)
y -= height * 0.5f;
}
surface->DrawSetTextFont(font);
surface->DrawSetTextColor(color);
surface->DrawSetTextPos(x, y);
surface->DrawPrintText(buffer, wcslen(buffer));
}
int SurfaceRender::text_width(vgui::HFont font, const char* msg, ...)
{
if (!surface)
return 0.0f;
va_list va_alist;
char buffer[1024];
va_start(va_alist, msg);
_vsnprintf(buffer, sizeof(buffer), msg, va_alist);
va_end(va_alist);
wchar_t wbuf[1024];
MultiByteToWideChar(CP_UTF8, 0, buffer, 256, wbuf, 256);
int width, height;
surface->GetTextSize(font, wbuf, width, height);
return width;
}
Renderer::Renderer()
{
width = 0;
height = 0;
}
crypt_ptr <Render> Renderer::operator->()
{
return &renderer;
}
void Renderer::update(crypt_ptr <IDirect3DDevice9> device)
{
renderer.device = device.get();
engine->GetScreenSize(width, height);
}