/
OptimizedNametags.cpp
154 lines (127 loc) · 5.01 KB
/
OptimizedNametags.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
#include "pch.h"
#include "OptimizedNametags.h"
#pragma intrinsic(_ReturnAddress)
bool OptimizedNametags::shouldRedrawNametag(NameTag& nt, D3DCOLOR color, bool isAfk, const char* name) {
bool ret = (!nt.sprite || !nt.texture || !nt.surface || !nt.renderToSurface) || nt.redraw || nt.isAfk != isAfk || nt.color != color || strcmp(nt.name, name) != 0;
if (nt.redraw) nt.redraw = false;
return ret;
}
bool OptimizedNametags::createElements(NameTag& nt, SIZE& textureSize)
{
static D3DSURFACE_DESC desc;
if (!nt.sprite || !nt.texture || !nt.surface || !nt.renderToSurface)
{
DX_SAFE_RELEASE(nt.sprite);
DX_SAFE_RELEASE(nt.texture);
DX_SAFE_RELEASE(nt.renderToSurface);
DX_SAFE_RELEASE(nt.surface);
/* Reserved due to some unknown reasons: nicknames sometimes gets cropped and can't be fully rendered.
* I have no idea why it's happening */
textureSize.cx += 1 /*outline*/ + 48 /*reserved*/ + 1;
textureSize.cy += 1 /*outline*/ + 4 /*AFK padding*/ + 48 /*AFK icon*/;
if ((textureSize.cx % 2) != 0) textureSize.cx++;
if ((textureSize.cy % 2) != 0) textureSize.cy++;
if (FAILED(D3DXCreateSprite(mD3DDevice, &nt.sprite))) return false;
if (FAILED(D3DXCreateTexture(mD3DDevice, textureSize.cx, textureSize.cy, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &nt.texture)))
return false;
if (FAILED(nt.texture->GetSurfaceLevel(0, &nt.surface))) return false;
if (FAILED(nt.surface->GetDesc(&desc))) return false;
if (FAILED(D3DXCreateRenderToSurface(mD3DDevice, desc.Width,
desc.Height, desc.Format, TRUE, D3DFMT_D16, &nt.renderToSurface))) return false;
}
return true;
}
void __fastcall CPlayerTags__Draw(uintptr_t self, int id, CVector* playerPos, const char* szText,
D3DCOLOR color, float fDistanceToCamera, bool bDrawStatus, int nStatus)
{
auto& nt = gInstance.mNametags[id];
auto& pDevice = gInstance.mD3DDevice;
auto isAfk = bDrawStatus && nStatus == 2;
if (gInstance.shouldRedrawNametag(nt, color, isAfk, szText))
{
auto textSize = sampGetMeasuredTextSize(szText);
SIZE textureSize = textSize;
if (!gInstance.createElements(nt, textureSize)) return;
RECT rect{ 1, 1, textureSize.cx, textureSize.cy };
if (SUCCEEDED(nt.renderToSurface->BeginScene(nt.surface, NULL)))
{
pDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0, 0, 0);
if (SUCCEEDED(nt.sprite->Begin(D3DXSPRITE_ALPHABLEND)))
{
pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_BLENDFACTOR);
nt.center = 1.f + static_cast<float>(textSize.cx) / 2.f;
sampDrawText(nt.sprite, szText, rect, color, true);
if (isAfk)
{
auto auxFont = sampGetAuxFont();
if (auxFont)
{
rect.top += textSize.cy + 4;
rect.left = static_cast<long>(nt.center / 2.6f);
auxFont->DrawTextA(nt.sprite, "C", 1, &rect, DT_NOCLIP | DT_LEFT, D3DCOLOR_XRGB(0, 0, 0));
rect.left += 3;
rect.top -= 1;
auxFont->DrawTextA(nt.sprite, "E", 1, &rect, DT_NOCLIP | DT_LEFT, -1);
}
}
nt.sprite->End();
nt.color = color;
nt.isAfk = isAfk;
strcpy_s(nt.name, sizeof(nt.name), szText);
}
nt.renderToSurface->EndScene(0);
}
}
D3DXVECTOR3 TagPos{ playerPos->x, playerPos->y, playerPos->z + 0.21f + (fDistanceToCamera * 0.05f)};
D3DVIEWPORT9 Viewport;
pDevice->GetViewport(&Viewport);
D3DXVECTOR3 Out;
D3DXMATRIX matIdent;
D3DXMatrixIdentity(&matIdent);
D3DXVec3Project(&Out, &TagPos, &Viewport, sampGetProjectionMatrix(), sampGetViewMatrix(), &matIdent);
Out.x -= nt.center;
Out.x = static_cast<float>(static_cast<long>(Out.x));
Out.y = static_cast<float>(static_cast<long>(Out.y));
if (SUCCEEDED(nt.sprite->Begin(D3DXSPRITE_ALPHABLEND)))
{
nt.sprite->Draw(nt.texture, NULL, NULL, &Out, 0xFFFFFFFF);
nt.sprite->End();
}
/*reinterpret_cast<void(__thiscall*)(uintptr_t, CVector * playerPos, const char* szText,
D3DCOLOR color, float fDistanceToCamera, bool bDrawStatus, int nStatus)>(gInstance.mHooks.CPlayerTags__Draw->getHookedFunctionAddress())
(self, playerPos, szText, color, fDistanceToCamera, bDrawStatus, nStatus);*/
}
__declspec(naked) void CPlayerTags__Draw_Naked()
{
__asm {
mov edx, edi
jmp CPlayerTags__Draw
}
}
double __fastcall CEntity__GetDistanceToCamera(uintptr_t self)
{
static void *label, *health;
uintptr_t CEntity = 0;
std::tie(label, health) = sampGetPedDistanceGetterReturnAddresses();
auto retAddress = _ReturnAddress();
if (retAddress != label && retAddress != health)
goto ret;
CEntity = *reinterpret_cast<uintptr_t*>(self + 0x40);
if (!reinterpret_cast<bool(__thiscall*)(uintptr_t)>(0x534540)(CEntity))
return DBL_MAX;
ret:
return reinterpret_cast<decltype(CEntity__GetDistanceToCamera)*>(gInstance.mHooks.CEntity__GetDistanceToCamera->getTrampoline())
(self);
}
void __fastcall CPlayerTags__OnLostDevice(void* self)
{
for (auto& nt : gInstance.mNametags)
{
DX_SAFE_RELEASE(nt.sprite);
DX_SAFE_RELEASE(nt.texture);
DX_SAFE_RELEASE(nt.renderToSurface);
DX_SAFE_RELEASE(nt.surface);
}
reinterpret_cast<decltype(CPlayerTags__OnLostDevice)*>(gInstance.mHooks.CPlayerTags__OnLostDevice->getTrampoline())
(self);
}