-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNameBoard.cpp
More file actions
223 lines (183 loc) · 5.33 KB
/
NameBoard.cpp
File metadata and controls
223 lines (183 loc) · 5.33 KB
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
#include "NameBoard.h"
NameBoard::NameBoard()
{
for (size_t i = 0; i < 100; i++)
{
for (size_t j = 0; j < 100; j++)
{
this->possiablePos[i][j] = 0;
}
}
}
NameBoard::~NameBoard()
{
}
void NameBoard::changeCurrentPos(int x, int y)
{
auto nextX = this->current.x + x;
auto nextY = this->current.y + y;
if (nextX < 0 || nextY < 0)
{
return;
}
if (this->possiablePos[nextX][nextY] == 1)
{
this->current.x += x;
this->current.y += y;
return;
}
if (this->possiablePos[0][nextY] == 1)
{
this->current.x = 0;
this->current.y += y;
return;
}
}
void NameBoard::appendToName()
{
if (this->name.size() >= 16)
{
return;
}
auto letter = this->alphabet[this->selection];
this->name += letter;
}
void NameBoard::backspaceName()
{
if (this->name.empty())
{
return;
}
this->name.pop_back();
}
void NameBoard::render(SDL_Renderer* renderer, TTF_Font* font)
{
Pos option = { 0, 0 };
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);
SDL_RenderClear(renderer);
constexpr SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 };
std::string msg = this->YOUR_NAME + this->name;
auto nameText = TTF_RenderUTF8_Solid(font, msg.c_str(), white);
SDL_Rect msgRect{};
msgRect.x = WINDOW_WIDTH / 2 - nameText->w / 2;
msgRect.y = WINDOW_HEIGHT / 10;
msgRect.w = nameText->w;
msgRect.h = nameText->h;
auto nameTexture = SDL_CreateTextureFromSurface(renderer, nameText);
SDL_RenderCopy(renderer, nameTexture, nullptr, &msgRect);
SDL_Surface* text[ALPHABET_SIZE]{};
SDL_Texture* texture[ALPHABET_SIZE]{};
auto TEXT_WIDTH = 30;
auto TEXT_HEIGHT = 40;
auto rectX = WINDOW_WIDTH / 10;
auto rectY = WINDOW_HEIGHT / 10 * 3;
for (size_t i = 0; i < ALPHABET_SIZE; i++)
{
text[i] = TTF_RenderUTF8_Solid(font, this->alphabet[i].c_str(), white);
SDL_Rect messageRect{};
if (rectX + TEXT_WIDTH + 100 >= WINDOW_WIDTH)
{
rectX = WINDOW_WIDTH / 10;
rectY += TEXT_HEIGHT + WINDOW_HEIGHT / 10 / 2;
option.x = 0;
option.y += 1;
}
else if (i != 0)
{
rectX += 75;
option.x += 1;
}
messageRect.x = rectX;
messageRect.y = rectY;
messageRect.w = TEXT_WIDTH;
messageRect.h = TEXT_HEIGHT;
texture[i] = SDL_CreateTextureFromSurface(renderer, text[i]);
SDL_RenderCopy(renderer, texture[i], nullptr, &messageRect);
this->possiablePos[option.x][option.y] = 1;
if (this->current.x == option.x && this->current.y == option.y)
{
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_Rect outlinedQuad = { messageRect.x - 4, messageRect.y - 4, messageRect.w + 4, messageRect.h + 4 };
SDL_RenderDrawRect(renderer, &outlinedQuad);
this->selection = i;
}
}
auto backspaceText = TTF_RenderUTF8_Solid(font, this->BACKSPACE.c_str(), white);
msgRect.x = WINDOW_WIDTH / 10;
rectY += WINDOW_HEIGHT / 10;
msgRect.y = rectY;
msgRect.w = backspaceText->w;
msgRect.h = backspaceText->h;
auto backspaceTexture = SDL_CreateTextureFromSurface(renderer, backspaceText);
SDL_RenderCopy(renderer, backspaceTexture, nullptr, &msgRect);
option.x = 0;
option.y += 1;
this->possiablePos[option.x][option.y] = 1;
if (this->current.x == option.x && this->current.y == option.y)
{
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_Rect outlinedQuad = { msgRect.x - 4, msgRect.y - 4, msgRect.w + 4, msgRect.h + 4 };
SDL_RenderDrawRect(renderer, &outlinedQuad);
this->selection = ALPHABET_SIZE;
}
auto confirmText = TTF_RenderUTF8_Solid(font, this->CONFIRM.c_str(), white);
msgRect.x = WINDOW_WIDTH / 10 * 3;
rectY += WINDOW_HEIGHT / 10 * 2;
msgRect.y = rectY;
msgRect.w = confirmText->w;
msgRect.h = confirmText->h;
auto confirmTexture = SDL_CreateTextureFromSurface(renderer, confirmText);
SDL_RenderCopy(renderer, confirmTexture, nullptr, &msgRect);
option.x = 0;
option.y += 1;
this->possiablePos[option.x][option.y] = 1;
if (this->current.x == option.x && this->current.y == option.y)
{
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_Rect outlinedQuad = { msgRect.x - 4, msgRect.y - 4, msgRect.w + 4, msgRect.h + 4 };
SDL_RenderDrawRect(renderer, &outlinedQuad);
this->selection = ALPHABET_SIZE + 1;
}
auto cancelText = TTF_RenderUTF8_Solid(font, this->CANCEL.c_str(), white);
msgRect.x = WINDOW_WIDTH / 10 * 6;
msgRect.y = rectY;
msgRect.w = cancelText->w;
msgRect.h = cancelText->h;
auto cancelTexture = SDL_CreateTextureFromSurface(renderer, cancelText);
SDL_RenderCopy(renderer, cancelTexture, nullptr, &msgRect);
option.x += 1;
this->possiablePos[option.x][option.y] = 1;
if (this->current.x == option.x && this->current.y == option.y)
{
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_Rect outlinedQuad = { msgRect.x - 4, msgRect.y - 4, msgRect.w + 4, msgRect.h + 4 };
SDL_RenderDrawRect(renderer, &outlinedQuad);
this->selection = ALPHABET_SIZE + 2;
}
SDL_RenderPresent(renderer);
for (size_t i = 0; i < ALPHABET_SIZE; i++)
{
SDL_FreeSurface(text[i]);
SDL_DestroyTexture(texture[i]);
}
SDL_FreeSurface(nameText);
SDL_DestroyTexture(nameTexture);
SDL_FreeSurface(backspaceText);
SDL_DestroyTexture(backspaceTexture);
SDL_FreeSurface(confirmText);
SDL_DestroyTexture(confirmTexture);
SDL_FreeSurface(cancelText);
SDL_DestroyTexture(cancelTexture);
}
bool NameBoard::isNameEmpty() const
{
return this->name.empty();
}
std::string NameBoard::getName() const
{
return this->name;
}
size_t NameBoard::getSelection() const
{
return this->selection;
}