-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserInterface.cpp
More file actions
executable file
·187 lines (146 loc) · 5.44 KB
/
UserInterface.cpp
File metadata and controls
executable file
·187 lines (146 loc) · 5.44 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
//Headers to include
#include "UserInterface.h"
#include "Game.h"
UserInterface::UserInterface(SDL_Renderer* renderer, Game* gameInstanceSet)
{
gameInstance = gameInstanceSet;
leftScore = rightScore = 0;
rendererInstance = renderer;
TTF_Init();
scoreFont = TTF_OpenFont(SCORE_FONT_DIRECTORY, scoreSize);
menuFont = TTF_OpenFont(MENU_FONT_DIRECTORY, menuSize);
backgroundTexture = SDL_CreateTextureFromSurface(rendererInstance, backgroundTempSurface);
if (!backgroundTexture) std::cerr << "couldn't load background textrue";
SDL_FreeSurface(backgroundTempSurface);
selectTexture = SDL_CreateTextureFromSurface(rendererInstance, selectTempSurface);
if (!selectTexture) std::cerr << "couldn't load selection texture";
SDL_FreeSurface(selectTempSurface);
}
void UserInterface::LoadScene(float frameSpeed)
{
switch (currentScene)
{
//Main menu
case 0:
{
textRect.w = SCREEN_WIDTH / 2;
textRect.h = SCREEN_WIDTH / 2;
LoadText("Select a mode:\n\n2 Player\nEasy AI\nImpossible AI", 0, (int)((SCREEN_WIDTH / 2) - (textRect.w / 2)), (int)((SCREEN_HEIGHT / 2) - (textRect.h / 2)));
selectRect.w = selectRect.h = menuSize - selectSizeDecreace;
selectRect.x = textRect.x - selectRect.w - selectSizeDecreace;
selectRect.y = (textRect.y + (menuSize * 2) + selectSizeDecreace) + ((menuSize + 10) * currentSelectedOption);
break;
}
//Game screen - displays score
case 1:
{
isImpossibleAi = false;
textRect.w = scoreSize; textRect.h = scoreSize;
LoadText(std::to_string(leftScore) + " " + std::to_string(rightScore), 1, (int)((SCREEN_WIDTH / 2) - (scoreSize / 2)) - 25, 0); //Subtract 25 because text was offset due to the query texture and i can't be bothered to fix it so have some hardcoding
break;
}
//Win screen
case 2:
{
textRect.w = SCREEN_WIDTH / 2;
textRect.h = SCREEN_WIDTH / 2;
if (leftScore > rightScore && !isImpossibleAi)
LoadText("Left player wins!\n\nPlay Again\nMain Menu", 0, (int)((SCREEN_WIDTH / 2) - (textRect.w / 2)), (int)((SCREEN_HEIGHT / 2) - (textRect.h / 2))); //Left wins
else if(!isImpossibleAi)
LoadText("Right player wins!\n\nPlay Again\nMain Menu", 0, (int)((SCREEN_WIDTH / 2) - (textRect.w / 2)), (int)((SCREEN_HEIGHT / 2) - (textRect.h / 2))); //Right wins
else
LoadText("You lasted " + impossibleTimeString + "s!\n\nPlay Again\nMain Menu", 0, (int)((SCREEN_WIDTH / 2) - (textRect.w / 2)), (int)((SCREEN_HEIGHT / 2) - (textRect.h / 2))); //Impossible AI Time
leftScore = rightScore = impossibleTime = 0;
selectRect.w = selectRect.h = menuSize - selectSizeDecreace;
selectRect.x = textRect.x - selectRect.w - selectSizeDecreace;
selectRect.y = (textRect.y + (menuSize * 2) + selectSizeDecreace) + ((menuSize + 10) * currentSelectedOption);
break;
}
//Impossible AI
case 3:
{
isImpossibleAi = true;
impossibleTime += (0.01 * (1 + frameSpeed));
impossibleTimeString = std::to_string(impossibleTime);
impossibleTimeString = impossibleTimeString.substr(0, impossibleTimeString.find('.') + 3);
textRect.w = scoreSize; textRect.h = scoreSize;
LoadText(impossibleTimeString, 1, ((SCREEN_WIDTH / 4) * 3) - (textRect.w / 2), 0);
}
}
}
void UserInterface::LoadText(const std::string& text, int textType, int x, int y)
{
textRect.x = x;
textRect.y = y;
switch (textType)
{
case 0:
{
if (menuFont == nullptr)
std::cout << "Failed to load font" << std::endl;
textSurface = TTF_RenderText_Blended_Wrapped(menuFont, text.c_str(), scoreFontColour, 0);
break;
}
//Score
case 1:
{
if (scoreFont == nullptr)
std::cout << "Failed to load font" << std::endl;
textSurface = TTF_RenderText_Blended_Wrapped(scoreFont, text.c_str(), scoreFontColour, 0);
break;
}
case 2:
{
break;
}
}
if (!textSurface)
std::cerr << "Failed to load text surface" << std::endl;
textTexture = SDL_CreateTextureFromSurface(rendererInstance, textSurface);
}
void UserInterface::ChangeSelect(bool isUp)
{
if (currentScene == 1 || currentScene == 3)
return;
if (((currentScene == 0 && !isUp && currentSelectedOption >= 2) || (isUp && currentSelectedOption <= 0)) || (currentScene == 2 && ((!isUp && currentSelectedOption >= 1))))
return;
if (isUp) currentSelectedOption--; else currentSelectedOption++;
}
int UserInterface::GetCurrentOption()
{
if (currentScene == 0 && currentSelectedOption != 1)
return currentSelectedOption + 1;
else if (currentScene == 2)
{
switch (currentSelectedOption)
{
//Play again
case 0:
{
//std::cout << previousScene;
return previousScene;
}
}
}
return 0;
}
void UserInterface::GivePoint(bool toRightPlayer)
{
toRightPlayer ? ++rightScore : ++leftScore;
if (rightScore >= 10 || leftScore >= 10 || isImpossibleAi)
{
gameInstance->LoadGameScene(2);
ChangeScene(2);
}
};
void UserInterface::Render(float frameSpeed)
{
LoadScene(frameSpeed);
std::cout << SDL_GetError();
if (!textTexture)
std::cerr << "Failed to load text texture" << std::endl;
SDL_QueryTexture(textTexture, nullptr, nullptr, &textRect.w, &textRect.h);
SDL_RenderCopy(rendererInstance, textTexture, nullptr, &textRect);
if (currentScene == 0 || currentScene == 2) SDL_RenderCopy(rendererInstance, selectTexture, &selectImgSrcRect, &selectRect);
SDL_FreeSurface(textSurface); SDL_DestroyTexture(textTexture);
}