Skip to content

Commit 5f4645e

Browse files
author
Nikita
committed
add bUseDynamicSinCos. DrawCircle and DrawRing will calculate sin and cos in runtime.
1 parent 97ec0a4 commit 5f4645e

File tree

7 files changed

+160
-109
lines changed

7 files changed

+160
-109
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ git submodule add https://github.com/Bulb4/renderer.git
1616

1717
***
1818
#### Initializing:
19+
If bUseDynamicSinCos is true, circle drawing will be faster but also it will take more memory.
20+
Use it if you do not change points count in runtime
21+
1922
```cpp
20-
cRender* pRender = new cRender(g_pd3dDevice);//g_pd3dDevice is our IDirect3DDevice9
23+
cRender* pRender = new cRender(g_pd3dDevice, true);//g_pd3dDevice is our IDirect3DDevice9
2124

2225
ID3DXFont* font1 = nullptr;
2326
pRender->AddFont(&font1, "Consolas", 48, false);

example/main.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ static D3DPRESENT_PARAMETERS g_d3dpp;
1818
LPDIRECT3DTEXTURE9 pRadarTexture = nullptr;
1919
LPD3DXSPRITE pRadarSprite = nullptr;
2020

21-
2221
short GetUsageOfCPU()
2322
{
2423
const static HANDLE hCurrentProcess = GetCurrentProcess();
@@ -129,10 +128,9 @@ int CALLBACK WinMain(
129128
ShowWindow(hwnd, SW_SHOWDEFAULT);
130129
UpdateWindow(hwnd);
131130

132-
cRender* pRender = new cRender(g_pd3dDevice);
131+
cRender* pRender = new cRender(g_pd3dDevice, true);
133132

134-
ID3DXFont* font1 = nullptr;
135-
pRender->AddFont(&font1, "System", 24, false);
133+
cFont font1(g_pd3dDevice, "System", 24);
136134

137135
pRender->SetFramerateUpdateRate(400U);
138136

@@ -176,14 +174,14 @@ int CALLBACK WinMain(
176174
pRender->DrawGradientBox(460, 20, 200, 50, rainbow_color[0], rainbow_color[1], rainbow_color[2], rainbow_color[3]);
177175
pRender->DrawBox(680, 20, 200, 50, 8, Colors::Pink);
178176

179-
pRender->DrawCircle(120, 190, 100, 32, RenderDrawType_Outlined, Colors::Red);
180-
pRender->DrawCircle(340, 190, 100, 32, RenderDrawType_Filled, Colors::Green);
177+
pRender->DrawCircle(120, 190, 100, 32, RenderDrawType_Outlined, Colors::Red, 0);
178+
pRender->DrawCircle(340, 190, 100, 32, RenderDrawType_Filled, Colors::Green, 0);
181179
pRender->DrawCircle(560, 190, 100, 32, RenderDrawType_Gradient, rainbow_color[0], rainbow_color[1]);
182180

183-
pRender->DrawRing(780, 190, 100, 80, 64, RenderDrawType_Filled, Colors::Blue);
181+
pRender->DrawRing(780, 190, 100, 80, 64, RenderDrawType_Filled, Colors::Blue, 0);
184182

185-
pRender->DrawTriangle(120, 310, 20, 480, 220, 480, RenderDrawType_Outlined, Colors::Green);
186-
pRender->DrawTriangle(340, 310, 240, 480, 440, 480, RenderDrawType_Filled, Colors::SkyBlue);
183+
pRender->DrawTriangle(120, 310, 20, 480, 220, 480, RenderDrawType_Outlined, Colors::Green, 0, 0);
184+
pRender->DrawTriangle(340, 310, 240, 480, 440, 480, RenderDrawType_Filled, Colors::SkyBlue, 0, 0);
187185
pRender->DrawTriangle(560, 310, 460, 480, 660, 480, RenderDrawType_FilledGradient, rainbow_color[0], rainbow_color[1], rainbow_color[2]);
188186

189187
pRender->DrawCircleSector(780, 380, 80, 30, time, time + 45, rainbow_color[0], rainbow_color[1]);
@@ -202,7 +200,7 @@ int CALLBACK WinMain(
202200
last_fps = current_fps;
203201

204202
pRender->DrawString(
205-
900, 10, Colors::White, font1, true, false,
203+
900, 10, Colors::White, &font1, true, false,
206204
"CPU: %i%%\nFPS: %d\nCPU Cores: %i\n%s",
207205
cpu_usage, current_fps,
208206
info.dwNumberOfProcessors,

include/font.hpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#pragma once
2+
3+
#include "renderer.h"
4+
5+
class cFont
6+
{
7+
public:
8+
cFont(IDirect3DDevice9* pDevice)
9+
{
10+
this->pDevice = pDevice;
11+
Update();
12+
}
13+
cFont(IDirect3DDevice9* pDevice, const char* szName, uint8_t iSize)
14+
{
15+
sprintf(this->szName, szName);
16+
17+
this->iSize = iSize;
18+
this->pDevice = pDevice;
19+
20+
Update();
21+
}
22+
cFont(IDirect3DDevice9* pDevice, const char* szName, uint8_t iSize, uint16_t iFontWeight, uint16_t iCharset, bool bItalic, bool bAntiAliased)
23+
{
24+
sprintf(this->szName, szName);
25+
26+
this->iSize = iSize;
27+
this->iFontWeight = iFontWeight;
28+
this->iCharset = iCharset;
29+
this->bItalic = bItalic;
30+
this->bAntiAliased = bAntiAliased;
31+
this->pDevice = pDevice;
32+
33+
Update();
34+
}
35+
36+
~cFont()
37+
{
38+
RELEASE_INTERFACE(pFont);
39+
}
40+
41+
char szName[32] = "System";
42+
int32_t iSize = 14;
43+
//0, 100, 200 ... 1000
44+
uint32_t iFontWeight = FW_NORMAL;
45+
uint32_t iCharset = DEFAULT_CHARSET;
46+
bool bItalic = false;
47+
bool bAntiAliased = false;
48+
49+
bool Update()
50+
{
51+
RELEASE_INTERFACE(pFont);
52+
53+
return D3DXCreateFontA(pDevice, iSize, 0,
54+
iFontWeight, 1, BOOL(bItalic), iCharset, OUT_DEFAULT_PRECIS,
55+
bAntiAliased ? ANTIALIASED_QUALITY : NONANTIALIASED_QUALITY,
56+
DEFAULT_PITCH, szName, &pFont) == D3D_OK;
57+
}
58+
59+
ID3DXFont* GetFont() { return pFont; }
60+
61+
void OnLostDevice()
62+
{
63+
if (pFont)
64+
pFont->OnLostDevice();
65+
}
66+
void OnResetDevice()
67+
{
68+
if (pFont)
69+
{
70+
pFont->OnResetDevice();
71+
Update();
72+
}
73+
}
74+
private:
75+
IDirect3DDevice9* pDevice;
76+
ID3DXFont* pFont = nullptr;
77+
};

include/renderer.cpp

Lines changed: 42 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "renderer.h"
22

3-
cRender::cRender(IDirect3DDevice9* device)
3+
cRender::cRender(IDirect3DDevice9* device, bool bUseDynamicSinCos) :
4+
m_bUseDynamicSinCos(bUseDynamicSinCos)
45
{
56
if (!device)
67
throw std::exception("device == nullptr");
@@ -15,14 +16,7 @@ cRender::~cRender()
1516
delete[] i->second;
1617
i->second = nullptr;
1718
}
18-
19-
for (auto& a : m_FontsList)
20-
if (*a.pFont)
21-
{
22-
(*a.pFont)->Release();
23-
*a.pFont = nullptr;
24-
}
25-
19+
2620
m_pDevice = nullptr;
2721
}
2822

@@ -76,42 +70,10 @@ void cRender::PushRenderState(const D3DRENDERSTATETYPE dwState, DWORD dwValue)
7670
m_pDevice->SetRenderState(dwState, dwValue);
7771
}
7872

79-
void cRender::OnLostDevice()
80-
{
81-
for (auto& a : m_FontsList)
82-
if (*a.pFont)
83-
(*a.pFont)->OnLostDevice();
84-
}
85-
86-
void cRender::OnResetDevice()
87-
{
88-
for (auto& a : m_FontsList)
89-
if (*a.pFont)
90-
{
91-
(*a.pFont)->OnResetDevice();
92-
a.update();
93-
}
94-
}
95-
96-
bool cRender::AddFont(ID3DXFont** pFont, const char* szName, uint8_t iSize, bool bAntiAliased)
97-
{
98-
font_t font;
99-
100-
font.pDevice = m_pDevice;
101-
font.pFont = pFont;
102-
font.iSize = iSize;
103-
font.bAntiAliased = bAntiAliased;
104-
sprintf_s(font.szName, szName);
73+
void cRender::OnLostDevice() { }
74+
void cRender::OnResetDevice() { }
10575

106-
if (!font.update())
107-
return false;
108-
109-
m_FontsList.push_back(font);
110-
111-
return true;
112-
}
113-
114-
void cRender::DrawString(int16_t x, int16_t y, color_t color, ID3DXFont* font, bool outlined, bool centered, const char* text, ...)
76+
void cRender::DrawString(int16_t x, int16_t y, color_t color, cFont* font, bool outlined, bool centered, const char* text, ...)
11577
{
11678
va_list args;
11779
char buf[256];
@@ -126,7 +88,7 @@ void cRender::DrawString(int16_t x, int16_t y, color_t color, ID3DXFont* font, b
12688
if (centered)
12789
{
12890
RECT size_rect = { 0 };
129-
font->DrawTextA(NULL, buf, size, &size_rect, DT_CALCRECT | DT_NOCLIP, 0);
91+
font->GetFont()->DrawTextA(NULL, buf, size, &size_rect, DT_CALCRECT | DT_NOCLIP, 0);
13092

13193
rect.left -= size_rect.right / 2;
13294
rect.top -= size_rect.bottom / 2;
@@ -138,17 +100,17 @@ void cRender::DrawString(int16_t x, int16_t y, color_t color, ID3DXFont* font, b
138100
auto outline_color = static_cast<const color_t>((color.color >> 24) << 24);
139101

140102
rect.top++;
141-
font->DrawTextA(NULL, buf, size, &rect, DT_NOCLIP, outline_color);//x; y + 1
103+
font->GetFont()->DrawTextA(NULL, buf, size, &rect, DT_NOCLIP, outline_color);//x; y + 1
142104
rect.left++; rect.top--;
143-
font->DrawTextA(NULL, buf, size, &rect, DT_NOCLIP, outline_color);//x + 1; y
105+
font->GetFont()->DrawTextA(NULL, buf, size, &rect, DT_NOCLIP, outline_color);//x + 1; y
144106
rect.left--; rect.top--;
145-
font->DrawTextA(NULL, buf, size, &rect, DT_NOCLIP, outline_color);//x; y - 1
107+
font->GetFont()->DrawTextA(NULL, buf, size, &rect, DT_NOCLIP, outline_color);//x; y - 1
146108
rect.left--; rect.top++;
147-
font->DrawTextA(NULL, buf, size, &rect, DT_NOCLIP, outline_color);//x - 1; y
109+
font->GetFont()->DrawTextA(NULL, buf, size, &rect, DT_NOCLIP, outline_color);//x - 1; y
148110
rect.left++;
149111
}
150112

151-
font->DrawTextA(NULL, buf, size, &rect, DT_NOCLIP, color);
113+
font->GetFont()->DrawTextA(NULL, buf, size, &rect, DT_NOCLIP, color);
152114
}
153115

154116
void cRender::DrawLine(int16_t x1, int16_t y1, int16_t x2, int16_t y2, color_t color)
@@ -192,11 +154,6 @@ void cRender::DrawBox(int16_t x, int16_t y, int16_t width, int16_t height, color
192154
m_pDevice->DrawPrimitiveUP(D3DPT_LINESTRIP, 4, pVertex, sizeof(Vertex_t));
193155
}
194156

195-
void cRender::DrawGradientBox(int16_t x, int16_t y, int16_t width, int16_t height, color_t color1, color_t color2, bool vertical)
196-
{
197-
DrawGradientBox(x, y, width, height, color1, vertical ? color1 : color2, vertical ? color2 : color1, color2);
198-
}
199-
200157
void cRender::DrawGradientBox(int16_t x, int16_t y, int16_t width, int16_t height, color_t color1, color_t color2, color_t color3, color_t color4)
201158
{
202159
Vertex_t pVertex[4];
@@ -215,16 +172,22 @@ void cRender::DrawCircle(int16_t x, int16_t y, int16_t radius, uint16_t points,
215172

216173
Vertex_t* verticles = new Vertex_t[points + gradient + 1];
217174

218-
#if _USE_DYNAMIC_SIN_COS != 1
219-
SinCos_t* pSinCos = GetSinCos(points);
220-
#endif // !_USE_DYNAMIC_SIN_COS
175+
SinCos_t* pSinCos = m_bUseDynamicSinCos ? nullptr : GetSinCos(points);
221176

222177
if (gradient)
223178
verticles[0] = Vertex_t(x, y, color2);
224179

225180
for (uint16_t i = gradient; i < points + gradient; i++)
226181
{
227-
verticles[i] = Vertex_t(x + pSinCos[i - gradient].flCos * radius, y + pSinCos[i - gradient].flSin * radius, color1);
182+
if (m_bUseDynamicSinCos)
183+
{
184+
const float angle = (2 * D3DX_PI) / points * (i - gradient);
185+
verticles[i] = Vertex_t(x + cos(angle) *radius, y + sin(angle) * radius, color1);
186+
}
187+
else
188+
{
189+
verticles[i] = Vertex_t(x + pSinCos[i - gradient].flCos * radius, y + pSinCos[i - gradient].flSin * radius, color1);
190+
}
228191

229192
if (filled)
230193
{
@@ -276,24 +239,37 @@ void cRender::DrawRing(int16_t x, int16_t y, int16_t radius1, int16_t radius2, u
276239

277240
if (flags & RenderDrawType_Outlined)
278241
{
279-
DrawCircle(x, y, radius1, points, RenderDrawType_Outlined, color1);
280-
DrawCircle(x, y, radius2, points, RenderDrawType_Outlined, color2);
242+
DrawCircle(x, y, radius1, points, RenderDrawType_Outlined, color1, 0);
243+
DrawCircle(x, y, radius2, points, RenderDrawType_Outlined, color2, 0);
281244
return;
282245
}
283246

284247
constexpr uint8_t modifier = 4;
285248
Vertex_t* verticles = new Vertex_t[points * modifier];
286249

287-
SinCos_t* pSinCos = GetSinCos(points);
250+
SinCos_t* pSinCos = m_bUseDynamicSinCos ? nullptr : GetSinCos(points);
288251

289252
for (uint16_t i = 0; i < points; i++)
290253
{
291254
uint16_t it = i * modifier;
255+
256+
if (m_bUseDynamicSinCos)
257+
{
258+
const float angle1 = (2 * D3DX_PI) / points * i;
259+
const float angle2 = (2 * D3DX_PI) / points * (i + 1);
292260

293-
verticles[it] = Vertex_t(x + pSinCos[i].flCos * radius1, y + pSinCos[i].flSin * radius1, color1);
294-
verticles[it + 1] = Vertex_t(x + pSinCos[i].flCos * radius2, y + pSinCos[i].flSin * radius2, color2);
295-
verticles[it + 2] = Vertex_t(x + pSinCos[i + 1].flCos * radius1, y + pSinCos[i + 1].flSin * radius1, color1);
296-
verticles[it + 3] = Vertex_t(x + pSinCos[i + 1].flCos * radius2, y + pSinCos[i + 1].flSin * radius2, color2);
261+
verticles[it] = Vertex_t(x + cos(angle1) * radius1, y + sin(angle1) * radius1, color1);
262+
verticles[it + 1] = Vertex_t(x + cos(angle1) * radius2, y + sin(angle1) * radius2, color2);
263+
verticles[it + 2] = Vertex_t(x + cos(angle2) * radius1, y + sin(angle2) * radius1, color1);
264+
verticles[it + 3] = Vertex_t(x + cos(angle2) * radius2, y + sin(angle2) * radius2, color2);
265+
}
266+
else
267+
{
268+
verticles[it] = Vertex_t(x + pSinCos[i].flCos * radius1, y + pSinCos[i].flSin * radius1, color1);
269+
verticles[it + 1] = Vertex_t(x + pSinCos[i].flCos * radius2, y + pSinCos[i].flSin * radius2, color2);
270+
verticles[it + 2] = Vertex_t(x + pSinCos[i + 1].flCos * radius1, y + pSinCos[i + 1].flSin * radius1, color1);
271+
verticles[it + 3] = Vertex_t(x + pSinCos[i + 1].flCos * radius2, y + pSinCos[i + 1].flSin * radius2, color2);
272+
}
297273

298274
for (uint8_t a = 0; a < modifier; a++)
299275
{

0 commit comments

Comments
 (0)