-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.cpp
85 lines (77 loc) · 2 KB
/
Menu.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
#include "Menu.h"
Menu::Menu(int x, int y) {
mOptionCount = 0;
mOptionStrings = nullptr;
mUnselectedFg = { 255, 255, 255, 255 };
mSelectedFg = {0, 0, 0, 255};
mSelectedBg = { 255, 255, 255, 255 };
mX = x;
mY = y;
mOptionSelected = 0;
}
void Menu::addOption(std::string str) {
if (mOptionCount == 0) {
mOptionStrings = new std::string[mOptionCount + 1];
mOptionStrings[mOptionCount] = str;
mOptionCount += 1;
}
else {
std::string* copy = new std::string[mOptionCount];
for (int i = 0; i < mOptionCount; i++) {
copy[i] = mOptionStrings[i];
}
mOptionStrings = new std::string[mOptionCount + 1];
for (int i = 0; i < mOptionCount; i++) {
mOptionStrings[i] = copy[i];
}
mOptionStrings[mOptionCount] = str;
mOptionCount += 1;
}
}
void Menu::draw() {
SDL_Surface* surface = nullptr;
SDL_Texture* texture = nullptr;
SDL_Rect dstRect;
for (int i = 0; i < mOptionCount; i++) {
if (i == mOptionSelected) {
//surface = TTF_RenderText_Solid(getFont(), mOptionStrings[i].c_str(), mSelectedColor);
surface = TTF_RenderText_Shaded(getFont(), mOptionStrings[i].c_str(), mSelectedFg, mSelectedBg);
}
else {
surface = TTF_RenderText_Solid(getFont(), mOptionStrings[i].c_str(), mUnselectedFg);
}
dstRect.w = surface->w;
dstRect.h = surface->h;
dstRect.x = mX;
dstRect.y = mY + i * dstRect.h;
texture = SDL_CreateTextureFromSurface(getRenderer(), surface);
SDL_RenderCopy(getRenderer(), texture, NULL, &dstRect);
SDL_FreeSurface(surface);
SDL_DestroyTexture(texture);
}
}
void Menu::moveCursor(int nPos) {
if (nPos < 0) {
while (abs(nPos) > 0) {
if (mOptionSelected <= 0) {
mOptionSelected = mOptionCount - 1;
}
else {
mOptionSelected -= 1;
}
nPos += 1;
}
}
else {
while (abs(nPos) > 0) {
if (mOptionSelected >= mOptionCount - 1) {
mOptionSelected = 0;
}
else {
mOptionSelected += 1;
}
nPos -= 1;
}
}
printf("%d\n", mOptionSelected);
}