-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDLscherm.h
290 lines (251 loc) · 9.5 KB
/
SDLscherm.h
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
289
290
const int resolution_X = 1024, resolution_Y = 1024;
class SDLscherm {
public:
SDLscherm() : calculate_thread(&SDLscherm::CalculatingThread, this), clickState(*this) {
InitSDL();
InitPictures();
}
void Run(){
bool quit = false;
SDL_Event event;
UpdateDisplay();
while (!quit) {
SDL_WaitEvent(&event);
switch(event.type) {
case SDLK_ESCAPE:
case SDL_QUIT:
quit = true;
break;
case SDL_MOUSEBUTTONDOWN: {
auto pinPosition = FindPinForPosition(std::make_pair(event.button.x, event.button.y));
if( onBoard(pinPosition) ){
clickState.MouseDown(pinPosition);
} else if( pinPosition.first == 1 && pinPosition.second == 5 ){
reverseMove();
}
break;
}
case SDL_MOUSEBUTTONUP:
clickState.MouseUp(FindPinForPosition(std::make_pair(event.button.x, event.button.y)));
break;
}
UpdateDisplay();
SDL_RenderPresent(renderer);
}
}
bool ownPiece(std::pair<int,int> mouseTile){
if( !onBoard(mouseTile) ){
return false;
}
return gaten[mouseTile.first][mouseTile.second] == knikker;
}
void select(std::pair<int,int> mouseTile){}
void deselect(){}
bool validMove(std::pair<int,int> from, std::pair<int,int> to){
//printf("Checking move: %i, %i to %i, %i\n", from.first, from.second, to.first, to.second);
if( !onBoard(from) || !onBoard(to) ){
return false;
}
return zetKan(from.first, from.second, to.first, to.second);
}
void makeMove(std::pair<int,int> from, std::pair<int,int> to){
doeZet(from.first, from.second, to.first, to.second);
movesDone.push(std::make_tuple(from.first, from.second, to.first, to.second));
HandleMoveMade();
}
~SDLscherm(){
shutDown = true;
calculationCancelled = true;
isCalculating = false;
newCalculationRequired.notify_one();
CleanUp(boardTexture);
CleanUp(pinTexture);
CleanUp(tickTexture);
CleanUp(crossTexture);
CleanUp(processingTexture);
CleanUp(renderer);
CleanUp(window);
IMG_Quit();
SDL_Quit();
if( calculate_thread.joinable() ){
calculate_thread.join();
}
}
private:
std::stack<tuple<int,int,int,int>> movesDone;
std::condition_variable newCalculationRequired;
std::atomic<bool> canStillWin { true };
std::atomic<bool> isCalculating { false };
std::atomic<bool> shutDown { false };
std::atomic<bool> calculationCancelled { false };
std::thread calculate_thread;
int clickSquareSizeX = 127 ;
const int squareStartPosX = 56;
const int clickSquareSizeY = 125;
const int squareStartPosY = 54;
SDL_Window * window = nullptr ;
SDL_Renderer * renderer { nullptr };
SDL_Texture * boardTexture { nullptr };
SDL_Texture * pinTexture { nullptr };
SDL_Texture * tickTexture { nullptr };
SDL_Texture * crossTexture { nullptr };
SDL_Texture * processingTexture { nullptr };
TwoClickOrDrag<SDLscherm> clickState;
const std::map<std::pair<int,int>,std::pair<int,int>> holeToPositionMap {
{ std::pair<int,int>(2,0), std::pair<int,int>(370,96) }, // (x,y) position -> (xPixels, yPixels)
{ std::pair<int,int>(3,0), std::pair<int,int>(496,97) },
{ std::pair<int,int>(4,0), std::pair<int,int>(626,98) },
{ std::pair<int,int>(2,1), std::pair<int,int>(366,226) },
{ std::pair<int,int>(3,1), std::pair<int,int>(495,225) },
{ std::pair<int,int>(4,1), std::pair<int,int>(622,224) },
{ std::pair<int,int>(0,2), std::pair<int,int>(112,356) },
{ std::pair<int,int>(1,2), std::pair<int,int>(239,354) },
{ std::pair<int,int>(2,2), std::pair<int,int>(366,352) },
{ std::pair<int,int>(3,2), std::pair<int,int>(493,350) },
{ std::pair<int,int>(4,2), std::pair<int,int>(625,348) },
{ std::pair<int,int>(5,2), std::pair<int,int>(751,350) },
{ std::pair<int,int>(6,2), std::pair<int,int>(877,350) },
{ std::pair<int,int>(0,3), std::pair<int,int>(111,483) },
{ std::pair<int,int>(1,3), std::pair<int,int>(236,484) },
{ std::pair<int,int>(2,3), std::pair<int,int>(364,483) },
{ std::pair<int,int>(4,3), std::pair<int,int>(624,479) },
{ std::pair<int,int>(5,3), std::pair<int,int>(750,478) },
{ std::pair<int,int>(6,3), std::pair<int,int>(877,473) },
{ std::pair<int,int>(0,4), std::pair<int,int>(115,604) },
{ std::pair<int,int>(1,4), std::pair<int,int>(239,606) },
{ std::pair<int,int>(2,4), std::pair<int,int>(368,605) },
{ std::pair<int,int>(3,4), std::pair<int,int>(495,604) },
{ std::pair<int,int>(4,4), std::pair<int,int>(620,604) },
{ std::pair<int,int>(5,4), std::pair<int,int>(750,605) },
{ std::pair<int,int>(6,4), std::pair<int,int>(876,606) },
{ std::pair<int,int>(2,5), std::pair<int,int>(363,737) },
{ std::pair<int,int>(3,5), std::pair<int,int>(493,737) },
{ std::pair<int,int>(4,5), std::pair<int,int>(620,737) },
{ std::pair<int,int>(2,6), std::pair<int,int>(367,862) },
{ std::pair<int,int>(3,6), std::pair<int,int>(490,864) },
{ std::pair<int,int>(4,6), std::pair<int,int>(621,862) },
{ std::pair<int,int>(3,3), std::pair<int,int>(496,480) } //middle pin
};
std::pair<int,int> GetScreenLocationForHole(int xPos, int yPos){
//return std::pair<int, int>(xPos*127 + 112, yPos*125 + 108 - xPos*2); //approximation
return holeToPositionMap.at(std::pair<int,int>(xPos,yPos));
}
void CalculatingThread(){
stellingTeRedden(encodeerBord(), canStillWin, calculationCancelled);
while(true){
std::mutex mut;
std::unique_lock<std::mutex> lk(mut);
newCalculationRequired.wait( lk, [this]{ //wait on this line of code until
return isCalculating.load() or shutDown.load(); //need to calculate or shutdown
} );
if( shutDown ){ //in destructor this is set and then above condition is notified
return;
}
calculationCancelled = false;
//pass these by reference so the other thread can check calculationCancelled and set the answer in canStillWin
stellingTeRedden(encodeerBord(), canStillWin, calculationCancelled);
isCalculating = calculationCancelled.load();
}
}
void UpdateDisplay(){
SDL_RenderCopy(renderer, boardTexture, nullptr, nullptr);
if( isCalculating ){
DisplayTick(processingTexture);
} else if( canStillWin ){
DisplayTick( tickTexture );
} else {
DisplayTick( crossTexture );
}
DisplayPins();
}
void DisplayPins(){
//std::unique_lock<std::mutex> lk(gamestate_mutex);
for( int y = 0; y < 7; ++y ){
for( int x = 0; x < 7; ++x ){
if( gaten[x][y] == knikker ){
auto location = GetScreenLocationForHole(x, y);
ApplySurface(location.first, location.second, pinTexture);
}
}
}
}
void DisplayTick(SDL_Texture * tickTexture){
ApplySurface(750, 150, tickTexture);
}
std::pair<int,int> FindPinForPosition(std::pair<int,int> mousePosition){
auto pins = std::make_pair((mousePosition.first-squareStartPosX)/clickSquareSizeX, (mousePosition.second-squareStartPosY)/clickSquareSizeY);
//printf("Position %i, %i maps to %i, %i\n", mousePosition.first, mousePosition.second, pins.first, pins.second);
return pins;
}
bool onBoard(std::pair<int,int> tileIndex){
return tileIndex.first >= 0 && tileIndex.first < 7 && tileIndex.second >= 0 && tileIndex.second < 7
&& holeIsOnBoard(tileIndex.first, tileIndex.second);
}
void HandleMoveMade(){
if( isCalculating ){
calculationCancelled = true;
}
isCalculating = true;
newCalculationRequired.notify_one();
}
void reverseMove(){
if( movesDone.empty() ){
return;
}
auto lastMove = movesDone.top();
zetTerug(std::get<0>(lastMove), std::get<1>(lastMove), std::get<2>(lastMove), std::get<3>(lastMove));
movesDone.pop();
HandleMoveMade();
}
void InitSDL(){
SDL_Init(SDL_INIT_VIDEO);
IMG_Init(IMG_INIT_PNG);
window = SDL_CreateWindow("SDL2 Displaying Image", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, resolution_X, resolution_Y, 0);
if (window == nullptr){
SDL_ShowSimpleMessageBox(0, "Window init error", SDL_GetError(), window);
}
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer == nullptr){
SDL_ShowSimpleMessageBox(0, "Renderer init error", SDL_GetError(), window);
}
}
void InitPictures(){
LoadImage("gfx/board-empty.jpg", boardTexture);
LoadImage("gfx/pin.gif", pinTexture);
LoadImage("gfx/tick.gif", tickTexture);
LoadImage("gfx/cross.gif", crossTexture);
LoadImage("gfx/processing.gif", processingTexture);
}
void LoadImage(std::string filename, SDL_Texture *& textureptr){
SDL_Surface * imageptr = IMG_Load(filename.c_str());
if (imageptr == nullptr){
SDL_ShowSimpleMessageBox(0, "Image init error", SDL_GetError(), window);
return;
}
textureptr = SDL_CreateTextureFromSurface(renderer, imageptr);
if (textureptr == nullptr){
SDL_ShowSimpleMessageBox(0, "Texture init error", SDL_GetError(), window);
}
}
void ApplySurface(const int x, const int y, SDL_Texture * source) {
int w, h;
SDL_QueryTexture(source, NULL, NULL, &w, &h);
SDL_Rect offset {x, y, w, h};
SDL_RenderCopy( renderer, source, nullptr, &offset );
}
void CleanUp(SDL_Texture * texture){
if( texture != nullptr ){
SDL_DestroyTexture(texture);
}
}
void CleanUp(SDL_Renderer * renderer){
if( renderer != nullptr ){
SDL_DestroyRenderer(renderer);
}
}
void CleanUp(SDL_Window * window){
if( window != nullptr){
SDL_DestroyWindow(window);
}
}
};