-
Notifications
You must be signed in to change notification settings - Fork 0
/
movepanel.cpp
190 lines (156 loc) · 6.07 KB
/
movepanel.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
#include <iostream>
#include "colors.h"
#include "movepanel.h"
MovePanel::MovePanel(SDL_Renderer* renderer, int xOff, int width, int height):
_renderer(renderer)
{
const int ptSizePriming = 32;
if (!(_font = TTF_OpenFont("resources/Lekton-Bold.ttf", ptSizePriming)))
{
std::cerr << "Can't load font from resources/Lekton-Bold.ttf\n";
return;
}
SDL_Color color = { 255, 255, 255, 255 };
SDL_Surface* surface = TTF_RenderText_Solid(_font, "Ng1-f3+", color);
SDL_Texture* texture = SDL_CreateTextureFromSurface(_renderer, surface);
int texW = 0, texH = 0;
SDL_QueryTexture(texture, NULL, NULL, &texW, &texH);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
int ptSizeTarget = ptSizePriming * width * 6 / (texW * 16);
if (ptSizeTarget != ptSizePriming)
{
TTF_CloseFont(_font);
_font = TTF_OpenFont("resources/Lekton-Bold.ttf", ptSizeTarget);
surface = TTF_RenderText_Solid(_font, "Ng1-f3+", color);
texture = SDL_CreateTextureFromSurface(_renderer, surface);
SDL_QueryTexture(texture, NULL, NULL, &texW, &texH);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
}
_backgroundRect.x = xOff;
_backgroundRect.y = 0;
_backgroundRect.w = width;
_backgroundRect.h = height;
_movesTextureRect[White].x = _backgroundRect.x + _backgroundRect.w / 32;
_movesTextureRect[White].y = texH + texH / 2;
_movesTextureRect[Black].x = _movesTextureRect[White].x + _backgroundRect.w / 2;
_movesTextureRect[Black].y = _movesTextureRect[White].y;
_winDrawIndicatorTextureRect.x = _movesTextureRect[White].x;
_winDrawIndicatorTextureRect.y = _backgroundRect.h - texH - texH / 4;
_turnIndicatorTextureRect[White].x = _turnIndicatorTextureRect[Black].x = _movesTextureRect[White].x;
_turnIndicatorTextureRect[White].y = _turnIndicatorTextureRect[Black].y = _backgroundRect.w / 32;
SDL_Color colors[] = {{ 255, 255, 255, 255 }, { 0, 0, 0, 255 }};
for (int c = White; c <= Black; ++c)
{
SDL_Surface* surface = TTF_RenderText_Blended_Wrapped(_font, (c ? "Black turn" : "White turn"), colors[c], _backgroundRect.w);
UpdateTexture(surface, &_turnIndicatorTexture[c], &_turnIndicatorTextureRect[c]);
}
_position.setInitial();
}
MovePanel::~MovePanel()
{
for (int c = White; c <= Black; ++c)
{
if (_movesTexture[c])
SDL_DestroyTexture(_movesTexture[c]);
if (_turnIndicatorTexture[c])
SDL_DestroyTexture(_turnIndicatorTexture[c]);
}
if (_winDrawIndicatorTexture)
SDL_DestroyTexture(_winDrawIndicatorTexture);
if (_font)
TTF_CloseFont(_font);
}
void MovePanel::SetPosition(const fatpup::Position& pos)
{
for (int c = White; c <= Black; ++c)
{
_movesString[c].clear();
if (!pos.isWhiteTurn() && c == White)
_movesString[c] += "...\n";
}
_position = pos;
_positionUpdated = true;
}
void MovePanel::Move(fatpup::Move move)
{
const auto c = (_position.isWhiteTurn() ? White : Black);
_movesString[c] += _position.moveToString(move) + "\n";
_position += move;
_positionUpdated = true;
}
void MovePanel::UpdateTexture(SDL_Surface* surface, SDL_Texture** texture, SDL_Rect* textureRect)
{
if (*texture)
SDL_DestroyTexture(*texture), *texture = nullptr;
if (surface)
{
*texture = SDL_CreateTextureFromSurface(_renderer, surface);
SDL_FreeSurface(surface);
}
if (*texture)
{
int texW = 0, texH = 0;
SDL_QueryTexture(*texture, NULL, NULL, &texW, &texH);
textureRect->w = texW;
textureRect->h = texH;
}
}
void MovePanel::UpdateTextures()
{
if (!_positionUpdated)
return;
_positionUpdated = false;
_positionState = _position.getState();
if (_font)
{
static const SDL_Color colors[] = {{ 255, 255, 255, 255 }, { 0, 0, 0, 255 }};
if (_positionState == fatpup::Position::State::Checkmate)
{
const auto c = _position.isWhiteTurn() ? Black : White;
const std::string winText(std::string(c ? "Black" : "White") + " won");
SDL_Surface* surface = TTF_RenderText_Blended(_font, winText.c_str(), colors[c]);
UpdateTexture(surface, &_winDrawIndicatorTexture, &_winDrawIndicatorTextureRect);
}
else if (_positionState == fatpup::Position::State::Stalemate)
{
const SDL_Color gray = { 128, 128, 128, 255 };
SDL_Surface* surface = TTF_RenderText_Blended(_font, "Draw", gray);
UpdateTexture(surface, &_winDrawIndicatorTexture, &_winDrawIndicatorTextureRect);
}
for (int c = White; c <= Black; ++c)
{
SDL_Surface* surface = TTF_RenderText_Blended_Wrapped(_font, _movesString[c].c_str(), colors[c], _backgroundRect.w / 2);
UpdateTexture(surface, &_movesTexture[c], &_movesTextureRect[c]);
}
}
}
void MovePanel::Render()
{
static const uint8_t backgroundColor[] =
{
(DARK_SQUARE[0] + LIGHT_SQUARE[0]) / 2,
(DARK_SQUARE[1] + LIGHT_SQUARE[1]) / 2,
(DARK_SQUARE[2] + LIGHT_SQUARE[2]) / 2
};
SDL_SetRenderDrawColor(_renderer, backgroundColor[0], backgroundColor[1], backgroundColor[2], 255);
SDL_RenderFillRect(_renderer, &_backgroundRect);
UpdateTextures();
for (int c = White; c <= Black; ++c)
{
if (_movesTexture[c])
SDL_RenderCopy(_renderer, _movesTexture[c], NULL, &_movesTextureRect[c]);
}
if (_positionState != fatpup::Position::State::Checkmate && _positionState != fatpup::Position::State::Stalemate)
{
const auto c = _position.isWhiteTurn() ? White : Black;
if (_turnIndicatorTexture[c])
SDL_RenderCopy(_renderer, _turnIndicatorTexture[c], NULL, &_turnIndicatorTextureRect[c]);
}
if (_positionState == fatpup::Position::State::Checkmate || _positionState == fatpup::Position::State::Stalemate)
{
if (_winDrawIndicatorTexture)
SDL_RenderCopy(_renderer, _winDrawIndicatorTexture, NULL, &_winDrawIndicatorTextureRect);
}
}