-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
452 lines (402 loc) · 14.2 KB
/
game.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include <queue>
#include <cassert>
#include <iostream>
#include <thread>
#include <chrono>
#include <algorithm>
#include <fstream>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "game.h"
#include "utils.h"
using namespace std;
static const int MIN_UPDATE_TIME = 100;
static const int MAX_UPDATE_TIME = 5000;
static const int UPDATE_INCREMENT = 50;
static const int DEFAULT_UPDATE_TIME = 500;
static const int ANTI_ALIASING_LEVEL = 8;
static const string GAME_NAME = "Game of Life";
static const int DEFAULT_WINDOW_WIDTH = 800;
static const int DEFAULT_WINDOW_HEIGHT = 600;
static const sf::Color BACKGROUND_COLOUR = sf::Color(253, 246, 227);
static const unsigned long INIT_X_COORD = 9223372036854775800;
static const unsigned long INIT_Y_COORD = 9223372036854775800;
static const string PATTERN_CONFIG_SEP = " ";
TextBox::TextBox() {
if (!font.loadFromFile("font.ttf")) {
cerr << "Font not loading!" << endl;
} else {
cout << "Font loaded" << endl;
}
buffer.clear();
assert(buffer.empty());
}
void
TextBox::Clear() {
prefix.clear();
buffer.clear();
}
void
TextBox::Draw(sf::RenderTarget& texture) const {
if (buffer.empty() && prefix.empty()) {
return;
}
sf::Text text;
text.setFont(font);
text.setString(prefix + buffer);
text.setCharacterSize(20);
text.setColor(sf::Color::Black);
texture.draw(text);
}
void
Game::ApplyPatternAtMouse() {
sf::Vector2i mousePosition = sf::Mouse::getPosition(_window);
if (mousePosition.x >= 0 && mousePosition.x <= _view.screenWidth &&
mousePosition.y >= 0 && mousePosition.y <= _view.screenHeight) {
const Cell& mouseCell = _view.PosnToCell(mousePosition.x, mousePosition.y);
_gameBoard.ApplyPattern(*_activePattern, mouseCell);
}
}
void
Game::LoadPatterns(const string& patternFileName) {
ifstream patternFile(patternFileName);
string line;
if (patternFile.is_open()) {
CellSet patternCells;
while (getline(patternFile, line)) {
if (line.empty()) {
_patterns.push_back(patternCells);
patternCells.clear();
continue;
}
size_t pos = line.find(PATTERN_CONFIG_SEP);
if (pos == string::npos) {
cerr << "Invalid config line. Expecting space-separated longs" << endl;
patternCells.clear();
break;
} else {
string xStr = line.substr(0, pos);
string yStr = line.substr(pos+1);
long x = atol(xStr.c_str());
// TODO: (not important) what if string is 0000
if (x == 0 && xStr != "0") {
cerr << "Could not convert " << xStr << " to long" << endl;
patternCells.clear();
break;
}
long y = atol(yStr.c_str());
if (y == 0 && yStr != "0") {
cerr << "Could not convert " << yStr << " to long" << endl;
patternCells.clear();
break;
}
// Input format in signed long, but want to deal with unsigned
// long internally so convert here.
// TODO: What if input is unsigned long?
unsigned long adjustedX = x + LONG_MAX + 1;
unsigned long adjustedY = y + LONG_MAX + 1;
patternCells.insert(Cell(adjustedX, adjustedY));
}
}
if (!patternCells.empty()) {
_patterns.push_back(patternCells);
}
}
}
Game::Game(const CellSet& startingPoints,
const string& patternFileName)
: _running(false), _collectInput(false), _collectJump(false),
_collectCentre(false), _buildingPattern(false), _patternIndex(0),
_gameBoard(startingPoints), _activePattern(NULL) {
LoadPatterns(patternFileName);
sf::ContextSettings settings;
settings.antialiasingLevel = ANTI_ALIASING_LEVEL;
_window.create(
sf::VideoMode(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT),
GAME_NAME,
sf::Style::Default,
settings);
_window.setVerticalSyncEnabled(true);
// Initialize view
const sf::Vector2u& size = _window.getSize();
_view.Init(size.x, size.y, INIT_X_COORD, INIT_Y_COORD);
}
void
Game::Draw() {
_window.clear(BACKGROUND_COLOUR);
_gameBoard.Draw(_view, _window, _running);
_inputBuffer.Draw(_window);
_window.display();
}
void
Game::RotateActivePattern() {
assert(_activePattern != NULL);
// Rotate around first element in pattern
CellSet newPattern;
CellSet::iterator it = _activePattern->begin();
const Cell& origin = *it;
newPattern.insert(origin);
for (++it; it != _activePattern->end(); ++it) {
try {
bool xOffsetNeg;
bool yOffsetNeg;
unsigned long xOffset = CalculateOffset(origin.x, it->x, &xOffsetNeg);
unsigned long yOffset = CalculateOffset(origin.y, it->y, &yOffsetNeg);
bool newXOffsetNeg = !yOffsetNeg;
bool newYOffsetNeg = xOffsetNeg;
unsigned long xNewOffset = yOffset;
unsigned long yNewOffset = xOffset;
unsigned long newX = ApplyOffset(origin.x, xNewOffset, newXOffsetNeg);
unsigned long newY = ApplyOffset(origin.y, yNewOffset, newYOffsetNeg);
newPattern.insert(Cell(newX, newY));
} catch (const out_of_range& err) {
// Can't apply pattern
newPattern.clear();
break;
}
}
if (!newPattern.empty()) {
_activePattern->swap(newPattern);
}
}
void Game::ActOnInput() {
_collectInput = false;
assert(!(_collectJump && _collectCentre));
if ((_collectJump || _collectCentre) && !_inputBuffer.buffer.empty()) {
size_t sep = _inputBuffer.buffer.find(" ");
if (sep != string::npos) {
// TODO: check for out-of-bounds
string xStr = _inputBuffer.buffer.substr(0, sep);
string yStr = _inputBuffer.buffer.substr(sep+1);
long x = atol(xStr.c_str());
long y = atol(yStr.c_str());
// TODO: deal with strings like 0000
if (!((xStr != "0" && x == 0) || (yStr != "0" && x == 0))) {
// Need to adjust coords to unsigned long
unsigned long xUl = x + LONG_MAX + 1;
unsigned long yUl = y + LONG_MAX + 1;
if (_collectJump) {
_view.Centre(xUl, yUl);
} else if (_collectCentre) {
// TODO: Deal with empty board
const Cell& nearest = _gameBoard.FindNearest(Cell(xUl, yUl));
_view.Centre(nearest.x, nearest.y);
}
}
}
} else if (_collectCentre) {
// Jump to nearest from current view
const Cell& nearest = _gameBoard.FindNearest(Cell(_view.xCentre,
_view.yCentre));
_view.Centre(nearest.x, nearest.y);
}
_collectCentre = false;
_collectJump = false;
_inputBuffer.Clear();
}
void
Game::ExitBuildMode() {
if (!_running) {
_gameBoard.CommitChanges();
if (_activePattern != NULL) {
_gameBoard.UndoPattern();
_activePattern = NULL;
}
_patternIndex = 0;
}
_running = !_running;
}
void
Game::ClearState() {
_patternIndex = 0;
_inputBuffer.Clear();
// If placing a pattern, don't clear the other in-progress changes.
if (!_running && _activePattern == NULL) {
_gameBoard.UndoChanges();
}
if (_collectInput || _collectJump) {
_collectInput = false;
_collectJump = false;
}
if (_buildingPattern) {
_buildingPattern = false;
_patterns.pop_back();
}
if (_activePattern != NULL) {
_gameBoard.UndoPattern();
_activePattern = NULL;
}
}
void
Game::Start() {
_running = true;
int msBetweenUpdates = DEFAULT_UPDATE_TIME;
sf::Clock clock;
bool resized = false;
while (_window.isOpen()) {
Draw();
sf::Time elapsed = clock.getElapsedTime();
int timeDiff = elapsed.asMilliseconds() - msBetweenUpdates;
if (_running && timeDiff >= 0) {
_gameBoard.Update();
clock.restart();
}
sf::Event event;
while (_window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
_window.close();
break;
case sf::Event::Resized:
// Coalesce resized events into one when resize done.
resized = true;
break;
case sf::Event::MouseMoved:
if (_activePattern != NULL && !_buildingPattern) {
assert(!_running);
ApplyPatternAtMouse();
}
break;
case sf::Event::TextEntered:
// Ignore non-ASCI-alpha-numeric characters, except space and dash
if (_collectInput &&
(event.text.unicode == 32 || event.text.unicode == 45 ||
(event.text.unicode >= 48 && event.text.unicode <= 57) ||
(event.text.unicode >= 65 && event.text.unicode <= 90) ||
(event.text.unicode >= 97 && event.text.unicode <= 122))) {
_inputBuffer.buffer += static_cast<char>(event.text.unicode);
}
break;
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::Up) {
_view.Move(ViewInfo::MOVE_UP);
} else if (event.key.code == sf::Keyboard::Down) {
_view.Move(ViewInfo::MOVE_DOWN);
} else if (event.key.code == sf::Keyboard::Left) {
_view.Move(ViewInfo::MOVE_LEFT);
} else if (event.key.code == sf::Keyboard::Right) {
_view.Move(ViewInfo::MOVE_RIGHT);
}
break;
case sf::Event::KeyReleased:
if (event.key.code == sf::Keyboard::P && !_collectInput) {
if (_buildingPattern) {
_buildingPattern = false;
_patternIndex = 0;
_inputBuffer.prefix.clear();
} else {
_inputBuffer.Clear();
_inputBuffer.prefix = "Recording Pattern...";
if (_activePattern == NULL) {
_patterns.push_back(CellSet());
_activePattern = &_patterns.at(_patterns.size()-1);
_buildingPattern = true;
}
}
} else if (event.key.code == sf::Keyboard::BackSpace &&
_collectInput) {
_inputBuffer.buffer.pop_back();
} else if (event.key.code == sf::Keyboard::Return &&
_collectInput) {
ActOnInput();
} else if (event.key.code == sf::Keyboard::G && !_collectInput &&
!_buildingPattern) {
_collectInput = true;
_collectJump = true;
_inputBuffer.Clear();
_inputBuffer.prefix = "Go to: ";
} else if (event.key.code == sf::Keyboard::Space && !_collectInput) {
ExitBuildMode();
clock.restart();
} else if (event.key.code == sf::Keyboard::Escape) {
ClearState();
} else if (event.key.code == sf::Keyboard::R && !_collectInput) {
_gameBoard.Reset();
clock.restart();
Draw();
} else if (event.key.code == sf::Keyboard::Equal &&
event.key.shift && !_collectInput) {
msBetweenUpdates = max(msBetweenUpdates - UPDATE_INCREMENT,
MIN_UPDATE_TIME);
} else if (event.key.code == sf::Keyboard::Equal &&
!event.key.shift && !_collectInput) {
msBetweenUpdates = min(msBetweenUpdates + UPDATE_INCREMENT,
MAX_UPDATE_TIME);
} else if (event.key.code == sf::Keyboard::Z && !_collectInput) {
_view.Zoom(ViewInfo::ZOOM_IN);
} else if (event.key.code == sf::Keyboard::X && !_collectInput) {
_view.Zoom(ViewInfo::ZOOM_OUT);
} else if (event.key.code == sf::Keyboard::J && !_collectInput &&
!_buildingPattern) {
_inputBuffer.Clear();
_inputBuffer.prefix = "Centre at nearest: ";
_collectInput = true;
_collectCentre = true;
} else if (event.key.code == sf::Keyboard::E && !_collectInput &&
_activePattern != NULL) {
RotateActivePattern();
ApplyPatternAtMouse();
} else if (event.key.code == sf::Keyboard::Tab && !_running &&
!_buildingPattern) {
if (_patterns.size() > 0) {
_activePattern = &_patterns.at(_patternIndex % _patterns.size());
++_patternIndex;
ApplyPatternAtMouse();
}
}
break;
case sf::Event::MouseButtonReleased:
if (event.mouseButton.button == sf::Mouse::Left && !_running) {
try {
// Pattern in progress
if (_buildingPattern) {
assert(_activePattern != NULL);
_activePattern->insert(_view.PosnToCell(event.mouseButton.x,
event.mouseButton.y));
_gameBoard.ApplyPattern(*_activePattern,
*(_activePattern->begin()));
} else if (_activePattern == NULL) {
_gameBoard.ChangeCell(_view.PosnToCell(event.mouseButton.x,
event.mouseButton.y));
} else {
// Left click with a pattern selected commits pattern
_gameBoard.CommitPattern();
_activePattern = NULL;
}
} catch (const out_of_range& err) {
cerr << "Out of range error when trying to modify cell" << endl;
}
} else if (event.mouseButton.button == sf::Mouse::Right) {
try {
const Cell& clickedCell = _view.PosnToCell(event.mouseButton.x,
event.mouseButton.y);
_view.Centre(clickedCell.x, clickedCell.y);
} catch (const out_of_range& err) {
cerr << "Out of range when trying to navigate" << endl;
}
}
default:
break;
}
}
if (resized) {
int newWidth = _window.getSize().x;
int newHeight = _window.getSize().y;
_view.Resize(newWidth, newHeight);
// Need to create a new window because SFML changes
// the pixel density of a window properly after resize.
// i.e. width 10 for a shape maps to a different number
// of pixels before vs after resize
const sf::Vector2i& prevPosn = _window.getPosition();
_window.create(
sf::VideoMode(newWidth, newHeight),
GAME_NAME,
sf::Style::Default,
_window.getSettings());
_window.setVerticalSyncEnabled(true);
_window.setPosition(prevPosn);
Draw();
resized = false;
}
}
}