/
qtgameoflifefighterwidget.cpp
executable file
·401 lines (358 loc) · 12.5 KB
/
qtgameoflifefighterwidget.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
#include "qtgameoflifefighterwidget.h"
#include <cassert>
#include <chrono>
#include <iostream>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include <QImage>
#include <QPainter>
#include <QPixmap>
#include <QTimer>
#include <QKeyEvent>
#include <QDesktopWidget>
#pragma GCC diagnostic pop
#include <SFML/Graphics.hpp>
#include "qtgameoflifefighterplayerindex.h"
#include "qtgameoflifefighterhelper.h"
#include "qtgameoflifefightersprite.h"
#include "gameoflifefightertrace.h"
#include "gameoflifefightertrace.h"
#include "gameoflifefighterplayerindex.h"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include "ui_qtgameoflifefighterwidget.h"
#pragma GCC diagnostic pop
golf::QtGameOfLifeFighterWidget::QtGameOfLifeFighterWidget(
const int width,
const int height,
QWidget *parent
)
: QWidget(parent),
ui(new Ui::QtGameOfLifeFighterWidget),
m_game{},
m_key_map{CreateInitialKeyMap()},
m_keys{},
m_pixmap(width * QtSprite().GetWidth(),height * QtSprite().GetHeight()),
m_tick{0}
{
#ifndef NDEBUG
Test();
#endif
ui->setupUi(this);
OnTimer();
{
//Put the dialog in the screen center
const QRect screen = QApplication::desktop()->screenGeometry();
//this->setGeometry(0,0,screen.width() * 20 / 24,screen.height() * 8 / 24);
this->setGeometry(0,0,m_pixmap.width(),m_pixmap.height());
this->move( screen.center() - this->rect().center() );
}
//Start the timer
{
QTimer * const timer{new QTimer(this)};
QObject::connect(timer,SIGNAL(timeout()),this,SLOT(OnTimer()));
timer->setInterval(20);
timer->start();
}
{
QTimer * const timer{new QTimer(this)};
QObject::connect(timer,SIGNAL(timeout()),this,SLOT(OnJoystickCheck()));
timer->setInterval(20);
timer->start();
}
}
golf::QtGameOfLifeFighterWidget::~QtGameOfLifeFighterWidget()
{
delete ui;
}
void golf::QtGameOfLifeFighterWidget::AddKey(const Key key)
{
if (std::count(std::begin(m_keys),std::end(m_keys),key) == 0)
{
m_keys.push_back(key);
}
assert(std::count(std::begin(m_keys),std::end(m_keys),key) == 1);
}
std::map<int,golf::Key> golf::QtGameOfLifeFighterWidget::CreateInitialKeyMap() noexcept
{
std::map<int,Key> m;
m.insert( { Qt::Key_Q, Key::toggle_cell1} );
m.insert( { Qt::Key_W, Key::up1} );
m.insert( { Qt::Key_E, Key::toggle_hangar1} );
m.insert( { Qt::Key_A, Key::left1} );
m.insert( { Qt::Key_S, Key::down1} );
m.insert( { Qt::Key_D, Key::right1} );
m.insert( { Qt::Key_Z, Key::pattern_a1} );
m.insert( { Qt::Key_X, Key::pattern_b1} );
m.insert( { Qt::Key_C, Key::pattern_c1} );
m.insert( { Qt::Key_U, Key::toggle_cell2} );
m.insert( { Qt::Key_I, Key::up2} );
m.insert( { Qt::Key_O, Key::toggle_hangar2} );
m.insert( { Qt::Key_J, Key::left2} );
m.insert( { Qt::Key_K, Key::down2} );
m.insert( { Qt::Key_L, Key::right2} );
m.insert( { Qt::Key_M , Key::pattern_a2} );
m.insert( { Qt::Key_Comma , Key::pattern_b2} );
m.insert( { Qt::Key_Period, Key::pattern_c2} );
return m;
}
void golf::QtGameOfLifeFighterWidget::keyPressEvent(QKeyEvent * e)
{
const auto iter = m_key_map.find(e->key());
if (iter == std::end(m_key_map)) return;
AddKey((*iter).second);
}
void golf::QtGameOfLifeFighterWidget::keyReleaseEvent(QKeyEvent * e)
{
const auto iter = m_key_map.find(e->key());
if (iter == std::end(m_key_map)) return;
RemoveKey((*iter).second);
}
void golf::QtGameOfLifeFighterWidget::OnJoystickCheck()
{
sf::Joystick::update();
const int key_activate{0};
const int key_build{2};
const int key_glider{3};
const int key_spaceship{1};
const int key_grower{4};
if (sf::Joystick::isConnected(0))
{
#ifdef ACCEPT_HEAD
//const int n_buttons = sf::Joystick::getButtonCount(0);
const bool pressed0{sf::Joystick::isButtonPressed(0,0)};
if (pressed0) { m_keys.insert(Key::toggle_hangar1); }
const bool pressed1{sf::Joystick::isButtonPressed(0,1)};
if ( pressed1) { m_keys.insert(Key::toggle_cell1); }
if (!pressed1) { m_keys.erase(Key::toggle_cell1); }
const bool pressed2{sf::Joystick::isButtonPressed(0,2)};
if ( pressed2) { m_keys.insert(Key::pattern_a1); }
const bool pressed3{sf::Joystick::isButtonPressed(0,3)};
if ( pressed3) { m_keys.insert(Key::pattern_b1); }
const bool pressed4{sf::Joystick::isButtonPressed(0,4)};
if ( pressed4) { m_keys.insert(Key::pattern_c1); }
m_keys.erase(Key::up1);
m_keys.erase(Key::right1);
m_keys.erase(Key::down1);
m_keys.erase(Key::left1);
if (sf::Joystick::hasAxis(0, sf::Joystick::X))
{
const double dx{sf::Joystick::getAxisPosition(0, sf::Joystick::X)};
if (dx < -50.0) { m_keys.insert(Key::left1 ); }
if (dx > 50.0) { m_keys.insert(Key::right1); }
}
if (sf::Joystick::hasAxis(0, sf::Joystick::Y))
{
const double dy{sf::Joystick::getAxisPosition(0, sf::Joystick::Y)};
if (dy < -50.0) { m_keys.insert(Key::up1 ); }
if (dy > 50.0) { m_keys.insert(Key::down1); }
}
}
if (sf::Joystick::isConnected(1))
{
//const int n_buttons = sf::Joystick::getButtonCount(0);
const bool pressed0{sf::Joystick::isButtonPressed(1,0)};
if (pressed0) { m_keys.insert(Key::toggle_hangar2); }
const bool pressed1{sf::Joystick::isButtonPressed(1,1)};
if ( pressed1) { m_keys.insert(Key::toggle_cell2); }
if (!pressed1) { m_keys.erase(Key::toggle_cell2); }
const bool pressed2{sf::Joystick::isButtonPressed(1,2)};
if ( pressed2) { m_keys.insert(Key::pattern_a2); }
const bool pressed3{sf::Joystick::isButtonPressed(1,3)};
if ( pressed3) { m_keys.insert(Key::pattern_b2); }
const bool pressed4{sf::Joystick::isButtonPressed(1,4)};
if ( pressed4) { m_keys.insert(Key::pattern_c2); }
m_keys.erase(Key::up2);
m_keys.erase(Key::right2);
m_keys.erase(Key::down2);
m_keys.erase(Key::left2);
if (sf::Joystick::hasAxis(1, sf::Joystick::X))
{
const double dx{sf::Joystick::getAxisPosition(1, sf::Joystick::X)};
if (dx < -50.0) { m_keys.insert(Key::left2 ); }
if (dx > 50.0) { m_keys.insert(Key::right2); }
}
if (sf::Joystick::hasAxis(0, sf::Joystick::Y))
{
const double dy{sf::Joystick::getAxisPosition(1, sf::Joystick::Y)};
if (dy < -50.0) { m_keys.insert(Key::up2 ); }
if (dy > 50.0) { m_keys.insert(Key::down2); }
#else //ACCEPT_HEAD
//First move, then build
RemoveKey(Key::up2);
RemoveKey(Key::right2);
RemoveKey(Key::down2);
RemoveKey(Key::left2);
if (sf::Joystick::hasAxis(0, sf::Joystick::X))
{
const double dx{sf::Joystick::getAxisPosition(0, sf::Joystick::X)};
if (dx < -50.0) { AddKey(Key::left2 ); }
if (dx > 50.0) { AddKey(Key::right2); }
}
if (sf::Joystick::hasAxis(0, sf::Joystick::Y))
{
const double dy{sf::Joystick::getAxisPosition(0, sf::Joystick::Y)};
if (dy < -50.0) { AddKey(Key::up2 ); }
if (dy > 50.0) { AddKey(Key::down2); }
}
const bool pressed0{sf::Joystick::isButtonPressed(0,key_activate)};
if (pressed0) { AddKey(Key::toggle_hangar2); }
const bool pressed1{sf::Joystick::isButtonPressed(0,key_build)};
if ( pressed1) { AddKey(Key::toggle_cell2); }
if (!pressed1) { RemoveKey(Key::toggle_cell2); }
const bool pressed2{sf::Joystick::isButtonPressed(0,key_glider)};
if ( pressed2) { AddKey(Key::pattern_a2); }
const bool pressed3{sf::Joystick::isButtonPressed(0,key_spaceship)};
if ( pressed3) { AddKey(Key::pattern_b2); }
const bool pressed4{sf::Joystick::isButtonPressed(0,key_grower)};
if ( pressed4) { AddKey(Key::pattern_c2); }
}
if (sf::Joystick::isConnected(1))
{
//First move, then build
RemoveKey(Key::up1);
RemoveKey(Key::right1);
RemoveKey(Key::down1);
RemoveKey(Key::left1);
if (sf::Joystick::hasAxis(1, sf::Joystick::X))
{
const double dx{sf::Joystick::getAxisPosition(1, sf::Joystick::X)};
if (dx < -50.0) { AddKey(Key::left1 ); }
if (dx > 50.0) { AddKey(Key::right1); }
}
if (sf::Joystick::hasAxis(1, sf::Joystick::Y))
{
const double dy{sf::Joystick::getAxisPosition(1, sf::Joystick::Y)};
if (dy < -50.0) { AddKey(Key::up1 ); }
if (dy > 50.0) { AddKey(Key::down1); }
#endif // ACCEPT_HEAD
}
const bool pressed0{sf::Joystick::isButtonPressed(1,key_activate)};
if (pressed0) { AddKey(Key::toggle_hangar1); }
const bool pressed1{sf::Joystick::isButtonPressed(1,key_build)};
if ( pressed1) { AddKey(Key::toggle_cell1); }
if (!pressed1) { RemoveKey(Key::toggle_cell1); }
const bool pressed2{sf::Joystick::isButtonPressed(1,key_glider)};
if ( pressed2) { AddKey(Key::pattern_a1); }
const bool pressed3{sf::Joystick::isButtonPressed(1,key_spaceship)};
if ( pressed3) { AddKey(Key::pattern_b1); }
const bool pressed4{sf::Joystick::isButtonPressed(1,key_grower)};
if ( pressed4) { AddKey(Key::pattern_c1); }
}
}
void golf::QtGameOfLifeFighterWidget::OnTimer()
{
++m_tick;
if (m_tick % 4 == 0)
{
m_game.Next();
//Game over? Then close the game after 5000 msecs
if (m_game.GetGameState() != GameState::playing)
{
QTimer::singleShot(5000,this,SLOT(close()));
}
}
m_game.PressKeys(m_keys);
//Keys manipulation
RemoveKey(Key::toggle_hangar1);
RemoveKey(Key::toggle_hangar2);
RemoveKey(Key::pattern_a1);
RemoveKey(Key::pattern_a2);
RemoveKey(Key::pattern_b1);
RemoveKey(Key::pattern_b2);
RemoveKey(Key::pattern_c1);
RemoveKey(Key::pattern_c2);
const int grid_rows{m_game.GetHeight()};
const int grid_cols{m_game.GetWidth()};
QImage image(
grid_cols * QtSprite().GetWidth(),
grid_rows * QtSprite().GetHeight(),
QImage::Format_ARGB32
);
const auto grid = m_game.GetCellStateGrid();
for (int y=0; y!=grid_rows; ++y)
{
const auto& grid_row = grid[y];
for (int x=0; x!=grid_cols; ++x)
{
QtHelper().DrawImage(
image,
QtSprite().Create(grid_row[x]),
x * QtSprite().GetWidth(),
y * QtSprite().GetHeight()
);
}
}
QtHelper().DrawImage(
image,
QtSprite().Create(PlayerIndex::player1),
2 + (m_game.GetPlayer(PlayerIndex::player1).GetX() * QtSprite().GetWidth()),
2 + (m_game.GetPlayer(PlayerIndex::player1).GetY() * QtSprite().GetHeight())
);
QtHelper().DrawImage(
image,
QtSprite().Create(PlayerIndex::player2),
2 + (m_game.GetPlayer(PlayerIndex::player2).GetX() * QtSprite().GetWidth()),
2 + (m_game.GetPlayer(PlayerIndex::player2).GetY() * QtSprite().GetHeight())
);
m_pixmap = QPixmap::fromImage(image);
update(); //Essential
}
void golf::QtGameOfLifeFighterWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawPixmap(
rect(),
m_pixmap
);
}
void golf::QtGameOfLifeFighterWidget::RemoveKey(const Key key)
{
assert(std::count(std::begin(m_keys),std::end(m_keys),key) <= 1);
const auto new_end = std::remove(
std::begin(m_keys),
std::end(m_keys),
key
);
m_keys.erase(new_end,std::end(m_keys));
}
QKeyEvent CreateDel() { return QKeyEvent(QEvent::KeyPress,Qt::Key_Delete,Qt::NoModifier); }
QKeyEvent CreateK() { return QKeyEvent(QEvent::KeyPress,Qt::Key_K,Qt::NoModifier); }
QKeyEvent CreateS() { return QKeyEvent(QEvent::KeyPress,Qt::Key_S,Qt::NoModifier); }
QKeyEvent CreateSpace() { return QKeyEvent(QEvent::KeyPress,Qt::Key_Space,Qt::NoModifier); }
QKeyEvent CreateDown() { return QKeyEvent(QEvent::KeyPress,Qt::Key_Down,Qt::NoModifier); }
QKeyEvent CreateControlDown() { return QKeyEvent(QEvent::KeyPress,Qt::Key_Down,Qt::ControlModifier); }
QKeyEvent CreateControlE() { return QKeyEvent(QEvent::KeyPress,Qt::Key_E,Qt::ControlModifier); }
QKeyEvent CreateControlN() { return QKeyEvent(QEvent::KeyPress,Qt::Key_N,Qt::ControlModifier); }
#ifndef NDEBUG
void golf::QtGameOfLifeFighterWidget::Test() noexcept
{
{
static bool is_tested{false};
if (is_tested) return;
is_tested = true;
}
QtHelper();
QtSprite();
//A key 'S' press should move player 1 down
{
QtGameOfLifeFighterWidget w;
auto down = CreateS();
const int y_before{w.m_game.GetPlayer(PlayerIndex::player1).GetY()};
w.keyPressEvent(&down);
w.OnTimer();
const int y_after{w.m_game.GetPlayer(PlayerIndex::player1).GetY()};
assert(y_after == y_before + 1);
}
//A key 'K' (arrows do not do anything) press should move player 2 down
{
QtGameOfLifeFighterWidget w;
auto down = CreateK();
const int y_before{w.m_game.GetPlayer(PlayerIndex::player2).GetY()};
w.keyPressEvent(&down);
w.OnTimer();
const int y_after{w.m_game.GetPlayer(PlayerIndex::player2).GetY()};
assert(y_after == y_before + 1);
}
}
#endif