-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.cpp
288 lines (254 loc) · 7.94 KB
/
interface.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "interface.h"
#include <vector>
#include <fstream>
// Global variable for storing players
std::vector<Player> players;
char whitePlayerName[NAME_MAX_LENGTH] = { "Unknown" };
char blackPlayerName[NAME_MAX_LENGTH] = { "Unknown" };
Interface::Interface()
: display(false)
{
rc = SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT, 0, &window, &renderer);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
screen = SDL_CreateRGBSurface(0, SCREEN_WIDTH, SCREEN_HEIGHT, 32,
0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
if (screen == nullptr)
{
std::cerr << "Error creating surface: " << SDL_GetError() << std::endl;
}
scrtex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
SCREEN_WIDTH, SCREEN_HEIGHT);
// wczytanie obrazka cs8x8.bmp
charset = SDL_LoadBMP("./ChessPieces/cs8x8.bmp");
SDL_SetColorKey(charset, true, 0x000000);
black = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
green = SDL_MapRGB(screen->format, 0x00, 0xFF, 0x00);
red = SDL_MapRGB(screen->format, 0xFF, 0x00, 0x00);
blue = SDL_MapRGB(screen->format, 0x11, 0x11, 0xCC);
white = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF);
grey = SDL_MapRGB(screen->format, 0x24, 0x24, 0x24);
gold = SDL_MapRGB(screen->format, 0xff, 0xc8, 0x37);
silver = SDL_MapRGB(screen->format, 0xc1, 0xc9, 0xc8);
brown = SDL_MapRGB(screen->format, 0xa5, 0x2a, 0x2a);
quit = 0;
text[0] = '\0';
trophyIcon = SDL_LoadBMP("./ChessPieces/golden_cup.bmp");
authenticateIcon = SDL_LoadBMP("./ChessPieces/report.bmp");
playIcon = SDL_LoadBMP("./ChessPieces/play.bmp");
}
Interface::~Interface()
{
SDL_FreeSurface(charset);
SDL_FreeSurface(screen);
SDL_DestroyTexture(scrtex);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
}
void Interface::displayInterface()
{
DrawSurface(screen, playIcon, 150, ICON_Y);
DrawSurface(screen, authenticateIcon, 405, ICON_Y);
DrawSurface(screen, trophyIcon, SCREEN_WIDTH - 150, ICON_Y);
// display all the rectangles and text
DrawRectangle(screen, 4, 20, SCREEN_WIDTH - 8, 75, white, grey);
sprintf_s(text, 256, "C++ creafted chess game made by Mateusz Wieczorek");
DrawString(screen, screen->w / 2 - strlen(text) * 8 / 2, 30, text, charset, Format::Big);
sprintf_s(text, 256, "Click on appropriate rectangle, or type 1, 2, 3 on your keyboard to choose what you want to do.");
DrawString(screen, screen->w / 2 - strlen(text) * 8 / 2, 50, text, charset);
sprintf_s(text, 256, "1 - play chess 2 - authenticate yourself in database 3 - check HallOfFame");
DrawString(screen, screen->w / 2 - strlen(text) * 8 / 2, 70, text, charset);
DrawRectangle(screen, 4, SCREEN_HEIGHT - 55, SCREEN_WIDTH - 8, 55, white, grey);
sprintf_s(text, 256, "White player is currently logged as: %s", whitePlayerName);
DrawString(screen, screen->w / 2 - strlen(text) * 8 / 2, 560, text, charset, Format::Medium);
sprintf_s(text, 256, "Black player is currently logged as: %s", blackPlayerName);
DrawString(screen, screen->w / 2 - strlen(text) * 8 / 2, 580, text, charset, Format::Medium);
SDL_UpdateTexture(scrtex, NULL, screen->pixels, screen->pitch);
SDL_RenderCopy(renderer, scrtex, NULL, NULL);
SDL_RenderPresent(renderer);
}
void Interface::showInterface()
{
// Open file and update players metrics
std::ifstream file("result.txt", std::ios_base::in);
if (!file.is_open())
{
std::cerr << "Error opening result.txt file\n";
exit(EXIT_FAILURE);
}
std::string colorWon;
file >> colorWon;
auto it1 = std::find_if(players.begin(), players.end(), [](const Player& p) {
return strcmp(p.getName(), whitePlayerName) == 0;
});
auto it2 = std::find_if(players.begin(), players.end(), [](const Player& p) {
return strcmp(p.getName(), blackPlayerName) == 0;
});
if (colorWon == "white")
{
if (it1 != players.end())
it1->getWhiteWins()++;
if (it2 != players.end())
it2->getBlackLoses()++;
}
else if (colorWon == "black")
{
if (it1 != players.end())
it1->getWhiteLoses()++;
if (it2 != players.end())
it2->getBlackWins()++;
}
file.close();
file.clear();
displayInterface();
while (!quit)
{
while (SDL_WaitEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
{
quit = 1;
exit(EXIT_SUCCESS);
}
else if (event.key.keysym.sym == SDLK_1)
{
display = true;
quit = 1;
return;
}
else if (event.key.keysym.sym == SDLK_2)
{
authenticationInterface();
}
else if (event.key.keysym.sym == SDLK_3)
{
hallOfFameInterface(players);
}
break;
case SDL_QUIT:
exit(EXIT_SUCCESS);
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT)
{
int xValue = event.button.x;
int yValue = event.button.y;
if (xValue >= 70 && xValue <= 229 && yValue >= 312 && yValue <= 478)
{
display = true;
quit = 1;
return;
}
else if (xValue >= 255 + 70 && xValue <= 255 + 229 && yValue >= 312 && yValue <= 478)
{
authenticationInterface();
}
else if (xValue >= 255 + 255 + 70 && xValue <= 255 + 255 + 229 && yValue >= 312 && yValue <= 478)
{
hallOfFameInterface(players);
}
}
}
};
};
}
void Interface::authenticationInterface()
{
int index = displayAuthenticationInterface(players);
if (index != players.size() - 1 || (players.size() == 1 && index == 0))
{
strcpy_s(whitePlayerName, NAME_MAX_LENGTH, "Unknown");
strcpy_s(blackPlayerName, NAME_MAX_LENGTH, "Unknown");
}
if (players[index].getPlayingColor() == WHITE)
strcpy_s(whitePlayerName, NAME_MAX_LENGTH, players[index].getName());
else
strcpy_s(blackPlayerName, NAME_MAX_LENGTH, players[index].getName());
displayInterface();
}
/*
draw a text txt on surface screen, starting from the point (x, y)
charset is a 128x128 bitmap containing character images
*/
void Interface::DrawString(SDL_Surface* screen, int x, int y, const char* text, SDL_Surface* charset, Format format)
{
int px, py, c;
SDL_Rect s, d;
s.w = 8;
s.h = 8;
int charWidth; // Desired character width
int charHeight; // Desired character height
switch (format)
{
case Format::Big:
charHeight = charWidth = 40;
break;
case Format::Medium:
charHeight = charWidth = 20;
break;
case Format::Small:
charHeight = charWidth = 8;
break;
default:
charHeight = charWidth = 8;
break;
}
d.w = charWidth;
d.h = charHeight;
while (*text)
{
c = *text & 255;
px = (c % 16) * 8;
py = (c / 16) * 8;
s.x = px;
s.y = py;
d.x = x;
d.y = y;
SDL_BlitSurface(charset, &s, screen, &d);
// Letter spacing
x += 8;
text++;
};
};
// draw a surface sprite on a surface screen in point (x, y)
// (x, y) is the center of sprite on screen
void Interface::DrawSurface(SDL_Surface* screen, SDL_Surface* sprite, int x, int y)
{
SDL_Rect dest;
dest.x = x - sprite->w / 2;
dest.y = y - sprite->h / 2;
dest.w = sprite->w;
dest.h = sprite->h;
SDL_BlitSurface(sprite, NULL, screen, &dest);
};
// draw a single pixel
void Interface::DrawPixel(SDL_Surface* surface, int x, int y, Uint32 color)
{
int bpp = surface->format->BytesPerPixel;
Uint8* p = (Uint8*)surface->pixels + y * surface->pitch + x * bpp;
*(Uint32*)p = color;
};
// draw a vertical (when dx = 0, dy = 1) or horizontal (when dx = 1, dy = 0) line
void Interface::DrawLine(SDL_Surface* screen, int x, int y, int l, int dx, int dy, Uint32 color)
{
for (int i = 0; i < l; i++) {
DrawPixel(screen, x, y, color);
x += dx;
y += dy;
};
};
// draw a rectangle of size l by k
void Interface::DrawRectangle(SDL_Surface* screen, int x, int y, int l, int k, Uint32 outlineColor, Uint32 fillColor)
{
int i;
DrawLine(screen, x, y, k, 0, 1, outlineColor);
DrawLine(screen, x + l - 1, y, k, 0, 1, outlineColor);
DrawLine(screen, x, y, l, 1, 0, outlineColor);
DrawLine(screen, x, y + k - 1, l, 1, 0, outlineColor);
for (i = y + 1; i < y + k - 1; i++)
DrawLine(screen, x + 1, i, l - 2, 1, 0, fillColor);
};