-
Notifications
You must be signed in to change notification settings - Fork 2
/
Countdown.h
171 lines (137 loc) · 4.72 KB
/
Countdown.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
#pragma once
#include "stdafx.h"
#include "Globals.h"
#include "RBR/RBR.h"
#include "RBR/D3D9Helpers.h"
#include "Utils/LogUtil.h"
#include "Utils/StringUtil.h"
#include "Utils/INIUtil.h"
namespace Countdown {
IMAGE_TEXTURE countdownTex[6];
IMAGE_TEXTURE dot_tex;
int countdownNumberTexSize = 256;
INIUtil::INIManager ini(Globals::PluginFolder + "\\RBRCountdown.ini");
bool iniCentered = true;
int iniX = 0;
int iniY = 0;
float iniScale = 1.0f;
void DrawCountdownNumbers(float countdown, int centerX, int centerY) {
int i = (int)std::ceil(countdown);
i = std::clamp(i, 0, 5);
IMAGE_TEXTURE tex = countdownTex[i];
float multiplier = 1 - std::abs(countdown - std::floor(countdown));
//int size = 256 + (int)(multiplier * 100);
int size = (int)std::round(countdownNumberTexSize * iniScale);
int x = centerX - size / 2;
int y = centerY - size;
HRESULT hResult = D3D9CreateVertexesForTex(
&tex, (float)x, (float)y, (float)size, (float)size,
0
);
if(SUCCEEDED(hResult)) {
D3D9DrawVertexTex2D(g_pRBRIDirect3DDevice9, tex.pTexture, tex.vertexes2D);
} else {
LogUtil::ToFile("Failed creating vertexes.");
}
}
void DrawCountdownDots(float countdown, int centerX, int centerY) {
if(countdown < 0) { return; }
IMAGE_TEXTURE tex = dot_tex;
float decimal = std::abs(countdown - std::floor(countdown));
int dots = (int)std::floor(decimal * 10); // floor
int size = (int)std::round(16 * iniScale);
int dotDistance = (int)std::round(10 * iniScale);
int y = (int)std::round(50 * iniScale);
for(int i = 0; i < dots; i++) {
int x = i * (size + dotDistance);
HRESULT hResult = D3D9CreateVertexesForTex(
&tex, (float)(centerX + x - size / 2), (float)(centerY + y), (float)size, (float)size,
0
);
if(SUCCEEDED(hResult)) {
D3D9DrawVertexTex2D(g_pRBRIDirect3DDevice9, tex.pTexture, tex.vertexes2D);
} else {
LogUtil::ToFile("Failed creating vertexes for dots.");
}
hResult = D3D9CreateVertexesForTex(
&tex, (float)(centerX - x - size / 2), (float)(centerY + y), (float)size, (float)size,
0
);
if(SUCCEEDED(hResult)) {
D3D9DrawVertexTex2D(g_pRBRIDirect3DDevice9, tex.pTexture, tex.vertexes2D);
} else {
LogUtil::ToFile("Failed creating vertexes for dots.");
}
}
}
void DrawCountdown() {
float countdown = g_pRBRCarInfo->stageStartCountdown;
int centerX = 0;
int centerY = 0;
if(iniCentered) {
RBRAPI_MapRBRPointToScreenPoint(320, 240, ¢erX, ¢erY);
}
centerX += iniX;
centerY += iniY;
// Get this number by taking the font height (=388) divide
// by 2 (=194) then divide by image height (=194/1024=0.189453125)
// This will center the text on screen.
centerY += 0.1894531f * countdownNumberTexSize * iniScale;
DrawCountdownNumbers(countdown, centerX, centerY);
DrawCountdownDots(countdown, centerX, centerY);
}
void LoadINI() {
try {
iniCentered = ini.Get("Settings", "Centered", true);
iniX = ini.Get("Settings", "XOffset", 0);
iniY = ini.Get("Settings", "YOffset", 0);
{
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
RBRAPI_MapRBRPointToScreenPoint(0, 0, &x1, &y1);
RBRAPI_MapRBRPointToScreenPoint(640, 480, &x2, &y2);
// 1440 pixels is the size when screen size is 1920
iniScale = (float)std::abs(x2 - x1) / 1440.0f;
iniScale *= ini.Get("Settings", "Scale", 1.0f);
}
ini.Save();
} catch(...) {
LogUtil::LastExceptionToFile("Failed loading INI.");
}
}
void InitCountdown() {
LoadINI();
std::wstring countdownTexPath = StringUtil::string_to_wide_string(Globals::PluginFolder);
for(int i = 0; i < 6; i++) {
std::wstring numberStr = std::to_wstring(i);
std::wstring path = countdownTexPath + L"\\countdown_000" + numberStr + L"_" + numberStr + L".png";
HRESULT hResult =
D3D9CreateRectangleVertexTexBufferFromFile(g_pRBRIDirect3DDevice9,
path,
0, 0, 0, 0,
&countdownTex[i],
0);
if(SUCCEEDED(hResult)) {
LogUtil::ToFile(L"Success loading TEX: " + path);
} else {
LogUtil::ToFile(L"Failed loading TEX: " + path);
}
}
{
std::wstring path = countdownTexPath + L"\\countdown_dot.png";
HRESULT hResult =
D3D9CreateRectangleVertexTexBufferFromFile(g_pRBRIDirect3DDevice9,
path,
0, 0, 0, 0,
&dot_tex,
0);
if(SUCCEEDED(hResult)) {
LogUtil::ToFile(L"Success loading TEX: " + path);
} else {
LogUtil::ToFile(L"Failed loading TEX: " + path);
}
}
}
}