This repository was archived by the owner on Feb 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathLSRender.cpp
415 lines (334 loc) · 8.95 KB
/
LSRender.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "LSRender.h"
LSRender::LSRender()
{
sDevice = 0;
sSprite = 0;
for (size_t i = 0; i < 200; i++)
{
fonts[i] = 0;
}
ready = false;
}
LSRender::~LSRender()
{
}
bool LSRender::Attach(LPDIRECT3DDEVICE9 pDevice)
{
if (!pDevice)
return false;
if (!sDevice)
sDevice = pDevice;
if (!sSprite)
D3DXCreateSprite(sDevice, &sSprite);
if (!sLine)
D3DXCreateLine(pDevice, &sLine);
if (sDevice && sSprite && sLine)
ready = true;
else
ready = false;
static POINT cursorpoint;
GetCursorPos(&cursorpoint);
ScreenToClient(GetForegroundWindow(), &cursorpoint);
CursorPos.x = (FLOAT)cursorpoint.x;
CursorPos.y = (FLOAT)cursorpoint.y;
//DrawBox(CursorPos, D3DXVECTOR2(20, 20), D3DCOLOR_ARGB(255, 255, 255, 0));
return ready;
}
void LSRender::LostDevice()
{
for (size_t i = 0; i < 255; i++)
{
if (fonts[i])
{
fonts[i]->OnLostDevice();
}
}
if (sLine)
sLine->OnLostDevice();
if (sSprite)
sSprite->OnLostDevice();
}
void LSRender::ResetDevice()
{
for (size_t i = 0; i < 255; i++)
{
if (fonts[i])
{
fonts[i]->OnResetDevice();
fonts[i] = 0;
}
}
if (sLine)
{
sLine->OnResetDevice();
sLine = 0;
}
if (sSprite)
{
sSprite->OnResetDevice();
sSprite = 0;
}
}
bool LSRender::Ready()
{
return ready;
}
LPDIRECTINPUT8 di;
LPDIRECTINPUTDEVICE8 keyboard;
#define keydown(name,key) (name[key] & 0x80)
HRESULT initializedirectinput8() {
HRESULT hr;
// Create a DirectInput device
if (FAILED(hr = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION,
IID_IDirectInput8, (VOID**)&di, NULL))) {
}
return hr;
}
void createdikeyboard() {
di->CreateDevice(GUID_SysKeyboard, &keyboard, NULL);
keyboard->SetDataFormat(&c_dfDIKeyboard);
keyboard->SetCooperativeLevel(NULL, DISCL_FOREGROUND | DISCL_EXCLUSIVE);
keyboard->Acquire();
}
bool inited = true;
void initdirect()
{
if (!inited)
return;
initializedirectinput8();
createdikeyboard();
inited = true;
}
DWORD curtick = GetTickCount();
BYTE dikeys[256];
#define GetAsyncKeyStateB(kod) keydown(dikeys,#kod) && GetTickCount() - curtick > 250
struct CUSTOMVERTEX { FLOAT X, Y, Z; DWORD COLOR; };
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)
bool LSRender::GetKeyPressed(int vKey)
{
bool isPressed = GetAsyncKeyState(vKey) & 0x8000 && GetTickCount() - curtick > 300;
if (isPressed)
{
curtick = GetTickCount();
}
return isPressed;
}
bool GetDIKeyPressed(int vKey)
{
initdirect();
keyboard->GetDeviceState(256, dikeys);
bool isPressed = (dikeys[vKey] & 0x80) && GetTickCount() - curtick > 300;
if (isPressed)
{
curtick = GetTickCount();
}
return isPressed;
}
LPD3DXFONT LSRender::CreateFontBySize(int size, char* font)
{
if (fonts[size])
return fonts[size];
if (sDevice)
if (fonts[size] == 0)
if (FAILED(D3DXCreateFontA(sDevice, size, 0, 500, 0, 0, TURKISH_CHARSET, OUT_TT_ONLY_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE, font, &fonts[size])))
{
return 0;
}
return fonts[size];
}
void LSRender::DrawString(float x, float y, D3DCOLOR color, DWORD Flag, int fontsize, const char * text, ...)
{
if (!Ready())
return;
if (!fonts[fontsize])
{
fonts[fontsize] = CreateFontBySize(fontsize);
return;
}
if (!fonts[fontsize])
return;
if (!sSprite)
return;
RECT rct;
rct.left = (LONG)x - 1;
rct.right = (LONG)x + 1;
rct.top = (LONG)y - 1;
rct.bottom = (LONG)y + 1;
va_list va_alist;
char vaBuffer[256] = { 0 };
va_start(va_alist, text);
_vsnprintf(vaBuffer + strlen(vaBuffer), sizeof(vaBuffer) - strlen(vaBuffer), text, va_alist);
va_end(va_alist);
sSprite->Begin(D3DXSPRITE_ALPHABLEND);
fonts[fontsize]->DrawTextA(sSprite, vaBuffer, -1, &rct, Flag | DT_NOCLIP, color);
sSprite->End();
}
void LSRender::DrawStringL(float x, float y, int fontsize, DWORD color, char * text)
{
if (!Ready())
return;
if (!fonts[fontsize])
{
fonts[fontsize] = CreateFontBySize(fontsize);
return;
}
if (!fonts[fontsize])
return;
#pragma warning (push)
#pragma warning (disable:4244)
RECT rect;
SetRect(&rect, (float)x, (float)y, (float)x, (float)y);
fonts[fontsize]->DrawTextA(NULL, text, -1, &rect, DT_LEFT | DT_NOCLIP, color);
#pragma warning (pop)
}
void LSRender::DrawLine(float fx, float fy, float tx, float ty, float width, D3DCOLOR color)
{
if (!Ready())
return;
D3DXVECTOR2 dLine[2];
sLine->SetWidth(width);
sLine->SetAntialias(true);
sLine->SetGLLines(true);
dLine[0].x = fx;
dLine[0].y = fy;
dLine[1].x = tx;
dLine[1].y = ty;
sLine->Draw(dLine, 2, color);
}
void LSRender::GradientBox(D3DXVECTOR2 Pos, D3DXVECTOR2 Size, D3DCOLOR color, D3DCOLOR color2, bool vertical) {
struct Vertex
{
float x, y, z, ht;
DWORD Color;
}
pVertex[4] = { { Pos.x, Pos.y, 0.0f, 1.0f, color },{ Pos.x + Size.x, Pos.y, 0.0f, 1.0f, vertical ? color : color2 },{ Pos.x, Pos.y + Size.y, 0.0f, 1.0f, vertical ? color2 : color },{ Pos.x + Size.x, Pos.y + Size.y, 0.0f, 1.0f, color2 } };
sDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
sDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, pVertex, sizeof(Vertex));
}
void LSRender::DrawBox(D3DXVECTOR2 Pos, D3DXVECTOR2 Size, D3DCOLOR Color)
{
if (!Ready())
return;
struct Vertex
{
float x, y, z, ht;
DWORD Color;
}
V[17] = { { Pos.x, Pos.y + Size.y, 0.0f, 0.0f, Color },{ Pos.x, Pos.y, 0.0f, 0.0f, Color },{ Pos.x + Size.x, Pos.y + Size.y, 0.0f, 0.0f, Color },{ Pos.x + Size.x, Pos.y, 0.0f, 0.0f, Color } };
sDevice->SetTexture(0, NULL);
sDevice->SetPixelShader(0);
sDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
sDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
sDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
sDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
sDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
sDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, V, sizeof(Vertex));
return;
}
void LSRender::FillRGB(int x, int y, int w, int h, D3DCOLOR Color)
{
if (!Ready())
return;
D3DRECT rec = { x, y, x + w, y + h };
sDevice->Clear(1, &rec, D3DCLEAR_TARGET, Color, 0, 0);
}
void LSRender::DrawBorder(D3DXVECTOR2 Pos, D3DXVECTOR2 Size, int px, D3DCOLOR BorderColor)
{
if (!Ready())
return;
FillRGB(Pos.x, (Pos.y + Size.y - px), Size.x, px, BorderColor);
FillRGB(Pos.x, Pos.y, px, Size.y, BorderColor);
FillRGB(Pos.x, Pos.y, Size.x, px, BorderColor);
FillRGB(Pos.x + Size.x - px, Pos.y, px, Size.y, BorderColor);
}
int LSRender::GetTextWidth(const char * szText, int fontsize)
{
if (!Ready())
return 0;
if (!fonts[fontsize])
return 0;
RECT rcRect = { 0,0,0,0 };
if (fonts[fontsize])
{
fonts[fontsize]->DrawText(NULL, szText, strlen(szText), &rcRect, DT_CALCRECT, D3DCOLOR_XRGB(0, 0, 0));
}
return rcRect.right - rcRect.left;
}
void LSRender::MoveByMouse(D3DXVECTOR2 &Pos, D3DXVECTOR2& Size)
{
if (BoundBox(CursorPos, D3DXVECTOR2(Pos.x, Pos.y), D3DXVECTOR2(Size.x, Size.y)))
{
if (GetAsyncKeyState(VK_LBUTTON) < 0)
{
Pos.x = CursorPos.x - ClickPos.x;
Pos.y = CursorPos.y - ClickPos.y;
}
else
{
ClickPos.x = (CursorPos.x - Pos.x);
ClickPos.y = (CursorPos.y - Pos.y);
}
}
}
void LSRender::ResizeByMouse(D3DXVECTOR2 &Pos, D3DXVECTOR2& Size)
{
static D3DXVECTOR2 defsize = Size;
if (BoundBox(CursorPos, D3DXVECTOR2(Pos.x, Pos.y), D3DXVECTOR2(Size.x, Size.y)))
{
if (GetAsyncKeyState(VK_LBUTTON) < 0)
{
Size.x = CursorPos.x - ClickPos.x;
Size.y = CursorPos.y - ClickPos.y;
}
else
{
ClickPos.x = (CursorPos.x - Size.x);
ClickPos.y = (CursorPos.y - Size.y);
}
}
}
D3DCOLOR LSRender::FromHex(DWORD hexcolor)
{
BYTE r = (BYTE)((hexcolor >> 16) & 0xFF);
BYTE g = (BYTE)((hexcolor >> 8) & 0xFF);
BYTE b = (BYTE)(hexcolor & 0xFF);
return D3DCOLOR_ARGB(255, r, g, b);
}
void LSRender::MoveByKeyboard(D3DXVECTOR2 & Pos)
{
if (GetAsyncKeyState(VK_UP))
Pos.y -= 0.5f;
else if (GetAsyncKeyState(VK_DOWN))
Pos.y += 0.5f;
if (GetAsyncKeyState(VK_LEFT)) { Pos.x -= 0.5f; }
if (GetAsyncKeyState(VK_RIGHT)) { Pos.x += 0.5f; }
}
bool LSRender::r3dWorldToScreen( RVector3* p_World, RVector3* p_Screen)
{
__try
{
D3DXMATRIX mWorld;
memset(&mWorld, 0, sizeof(mWorld));
D3DXMatrixIdentity(&mWorld);
r3DRitoRender * ritorender = (r3DRitoRender*)*(DWORD*)(MAKE_RVA(Offsets::r3dRenderer::r3dRendererInstance));
D3DVIEWPORT9 v_Viewport;
sDevice->GetViewport(&v_Viewport);
D3DXVec3Project(p_Screen, p_World, &v_Viewport, &*ritorender->GetprojectionMatrix(), &*ritorender->GetviewMatrix(), &mWorld);
p_Screen->x = (p_Screen->x) / (*ritorender->GetscreenWidth()) * *ritorender->GetscreenWidth();
p_Screen->y = (p_Screen->y) / (*ritorender->GetscreenHeight()) * *ritorender->GetscreenHeight();
if (p_Screen->x < 0.f || p_Screen->x > *ritorender->GetscreenWidth())
return false;
if (p_Screen->y < 0.f || p_Screen->y > *ritorender->GetscreenHeight())
return false;
//printf("W %d H %d X : %.f , Y : %.f\n", ritorender->screenWidth, ritorender->screenHeight, p_Screen->x, p_Screen->y);
}
__except (1)
{
}
return true;
}
float LSRender::r3dDistance(RVector3 a, RVector3 b)
{
RVector3 vector = RVector3(a.x - b.x, a.y - b.y, a.z - b.z);
return sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z);
}