-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShifter.cpp
194 lines (155 loc) · 4.9 KB
/
Shifter.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
#include <iostream>
#include <algorithm>
#include <string>
#include "Shifter.hpp"
#pragma region ShifterHandle
ShifterHandle::ShifterHandle(GearBox *gearBox) {
this->gearBox = gearBox;
auto ngear = &gearBox->gears[0];
x = ngear->x + ngear->w/2;
y = ngear->y + ngear->h/2;
}
void ShifterHandle::move(int &dX, int &dY, int &w, int &h) {
int nX = x + dX;
int nY = y + dY;
bool gear = checkGearBounds(nX, nY);
if (disableSideLimits)
{
nX = std::clamp(nX, 0, w);
nY = std::clamp(nY, 0, h);
}
else if (!gear) {
if (checkGearBounds(x, nY)) {
y = nY;
return;
}
if (checkGearBounds(nX, y)) {
x = nX;
return;
}
return;
}
x = nX;
y = nY;
// std::cout << "Moved to " << x << ", " << y << std::endl;
}
void ShifterHandle::render(SDL_Renderer* rend) const {
gearBox->render(rend);
SDL_SetRenderDrawColor(rend, 20, 20, 240, 180);
auto rect = SDL_Rect{ x - 10, y - 10, 20, 20 };
SDL_RenderFillRect(rend, &rect);
}
bool ShifterHandle::checkGearBounds(int nX, int nY) {
for (uint8_t i = 0, l = gearBox->length; i < l; ++i)
{
auto gear = gearBox->gears[i];
if (gear.x <= nX && nX <= gear.x + gear.w &&
gear.y <= nY && nY <= gear.y + gear.h)
{
gearBox->changeGear(i);
return true;
}
}
return false;
}
void ShifterHandle::center(int deltaTicks) {
auto currentGear = gearBox->activeGear();
int tX = currentGear->x + currentGear->w / 2;
int tY = currentGear->y + currentGear->h / 2;
double dtS = deltaTicks / 1000.0;
// std::cout << dtS << std::endl;
dtS *= 10;
x = (abs(tX - x) > 6) ? (tX * dtS) + (x * (1.0 - dtS)) : tX;
y = (abs(tY - y) > 6) ? (tY * dtS) + (y * (1.0 - dtS)) : tY;
}
#pragma endregion
#pragma region GearBox
GearBox::GearBox(uint8_t gearsCnt, int &w, int &h, SDL_Renderer * rend) {
gears = new SDL_Rect[gearsCnt + 2];
length = gearsCnt + 2;
generate(w, h, rend);
}
void GearBox::generate(int &w, int &h, SDL_Renderer * rend)
{
const int spacing = w / 100 * 2;
const int dblSpacing = spacing * 2;
printf("resized to %i x %i\n", w, h);
int offset = (w / (length / 2) - dblSpacing) / 2;
int gearWidth = (w - offset) / (length / 2) - dblSpacing;
int gearHeight = h / 3;
int bottomRow = gearHeight * 2;
//neutral
gears[0] = { 0, gearHeight, w, gearHeight };
//main gears
for (uint8_t i = 2; i < length; ++i)
{
uint8_t horIndex = (i / 2) - 1;
gears[i] = { horIndex * (gearWidth + dblSpacing) + spacing + (i % 2 ? offset : 0), i % 2 ? bottomRow : 0, gearWidth, gearHeight };
}
//reverse
gears[1] = {w - gearWidth - spacing, bottomRow, gearWidth, gearHeight };
if (gearIdText != nullptr)
SDL_DestroyTexture(gearIdText);
TTF_Font *font = TTF_OpenFont("Arial.ttf", 24);
auto baseSurf = SDL_CreateRGBSurfaceWithFormat(0, w, h, 0, SDL_PIXELFORMAT_ARGB32);
for (uint8_t i = 0, l = length; i < l; ++i)
{
auto gear = gears[i];
auto txt = TTF_RenderText_Blended(font, GetGearName(i), SDL_Color{0,0,0,0});
gear.x += gear.w/2 - 12;
gear.y += gearHeight/2 - 12;
SDL_BlitSurface(txt, nullptr, baseSurf, &gear);
SDL_FreeSurface(txt);
}
gearIdText = SDL_CreateTextureFromSurface(rend, baseSurf);
SDL_FreeSurface(baseSurf);
TTF_CloseFont(font);
}
SDL_Rect *GearBox::activeGear() const {
if (activeGearId < 0 || activeGearId >= length)
return nullptr;
return &gears[activeGearId];
}
GearBox::~GearBox() {
delete[] gears;
SDL_DestroyTexture(gearIdText);
}
void GearBox::render(SDL_Renderer *rend) const {
SDL_SetRenderDrawColor(rend, INACTIVE_GEAR);
SDL_RenderFillRects(rend, gears, length);
if (activeGearId < 0 || activeGearId >= length)
return;
SDL_SetRenderDrawColor(rend, ACTIVE_GEAR);
SDL_RenderFillRect(rend, activeGear());
SDL_RenderCopy(rend, gearIdText, nullptr, nullptr);
}
void GearBox::changeGear(int i) {
if (activeGearId == i) return;
// std::cout << std::endl;
printf("Gear changed from %i to %i\n", activeGearId, i);
if (activeGearId != 0)
controller_emu_set_input(activeGearId - 1, false);
activeGearId = i;
if(activeGearId != 0)
controller_emu_set_input(activeGearId - 1, true);
}
const char *GearBox::GetGearName(uint8_t id) {
switch (id) {
case 0:
return "N";
case 1:
return "R";
default:
return std::to_string(id - 1).c_str();
}
}
#pragma endregion
//#pragma region Gear
//
//void Gear::render(bool active, SDL_Renderer *rend) {
// auto c = active ? ACTIVE_GEAR : INACTIVE_GEAR;
// SDL_SetRenderDrawColor(rend, c.r, c.g, c.b, c.a);
// SDL_RenderFillRect(rend, &rect);
//}
//
//#pragma endregion