-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamewidget.cpp
More file actions
141 lines (107 loc) · 3.11 KB
/
gamewidget.cpp
File metadata and controls
141 lines (107 loc) · 3.11 KB
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
#include "gamewidget.h"
#include "gamescene.h"
#include "gamesound.h"
#include "gameprofile.h"
#include "gamemenu.h"
#include "displaywrapper.h"
#include "scaler.h"
QString GameWidget::resourcePath;
GameWidget::GameWidget(const QString &respath, QWidget *parent)
: QGraphicsView(parent)
{
resourcePath = respath;
qsrand(QTime::currentTime().msec());
setWindowTitle("Bubble Chains");
// create sound engine
sndEngine = new GameSound();
connect(sndEngine, SIGNAL(musicFinished()), this, SLOT(playRandomMusic()));
// create and load profile
gameProfile = new GameProfile(this);
gameProfile->loadProfile();
// play music if enabled
playRandomMusic();
// use acceleration
if (gameProfile->isAccelerated())
setViewport(new QGLWidget());
setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// create and setup scene
scene = new GameScene(this);
setScene(scene);
// setup window
connect(scene->gameMenu(), SIGNAL(menuVideoModeChanged()),
this, SLOT(setVideoMode()));
setVideoMode();
}
GameWidget::~GameWidget()
{
gameProfile->setWindowPos(x(),y());
gameProfile->saveProfile();
delete sndEngine;
delete gameProfile;
QApplication::quit();
}
void GameWidget::closeEvent(QCloseEvent *event)
{
if (!scene->confirmExit())
event->ignore();
}
void GameWidget::drawBackground ( QPainter * painter, const QRectF & rect )
{
painter->fillRect(rect, Qt::black);
scene->drawBackground(painter, rect);
}
void GameWidget::keyPressEvent ( QKeyEvent * keyEvent )
{
scene->keyPressEvent(keyEvent);
}
void GameWidget::focusOutEvent ( QFocusEvent * event )
{
QWidget::focusOutEvent(event);
scene->pauseGame();
}
void GameWidget::setVideoMode()
{
// setup scaler
scaler->rescale(gameProfile->screenWidth(), gameProfile->screenHeight());
// restore video mode (to avoid any issues)
DisplayWrapper::restoreVideoMode();
showNormal();
QApplication::processEvents();
if (gameProfile->isFullscreen())
{
// set full screen mode
if (!DisplayWrapper::switchVideoMode(WIDTH,HEIGHT,0,true))
{
qDebug() << "Unable to change resolution.";
}
showFullScreen();
}
else
{
PlayerInfo *pl = gameProfile->currentPlayer();
move(pl->x, pl->y);
}
// resize window
setFixedSize(WIDTH,HEIGHT);
setSceneRect(1, 1, WIDTH-2, HEIGHT-2);
// update profile mode
gameProfile->setVideoMode(WIDTH, HEIGHT);
// invoke graphics update
scene->initGraphics();
}
void GameWidget::playRandomMusic()
{
QStringList ext; ext << "*.mp3" << "*.wav" << "*.mod" << "*.mid";
QString path(resourcePath + "/music");
QStringList entryList = QDir(path).entryList(ext);
// qDebug() << entryList;
if (entryList.count())
{
int r = qrand() % entryList.count();
// qDebug() << path + "/" + entryList.at(r);
sndEngine->loadMusic(path + "/" + entryList.at(r));
sndEngine->playMusic();
}
}