-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·156 lines (125 loc) · 4.25 KB
/
main.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
#include <string>
#include <vector>
#include <memory>
#include <unordered_map>
#include <QApplication>
#include <QGraphicsView>
#include <QCloseEvent>
#include "window.h"
#include "game.h"
#include "menu.h"
#include "sprite.h"
void startGame();
void gameOver();
void exitGame();
int main(int argc, char* argv[])
{
srand(unsigned(int(time(nullptr))));
QApplication app(argc, argv);
// Loading sprites.
Sprite::Set background_sprites;
Sprite::Set player_sprites;
Sprite::Set asteroid_sprites;
Sprite::Set bullet_sprites;
background_sprites["default"] = {
std::make_shared<Sprite>(":/assets/images/background.png", 0),
};
player_sprites["default"] = {
std::make_shared<Sprite>(":/assets/images/player.png", 0)
};
asteroid_sprites["default"] = {
std::make_shared<Sprite>(":/assets/images/asteroid.png", 0)
};
player_sprites["die"] = asteroid_sprites["die"] = {
std::make_shared<Sprite>(":/assets/images/explosion00.png", 0.01),
std::make_shared<Sprite>(":/assets/images/explosion01.png", 0.01),
std::make_shared<Sprite>(":/assets/images/explosion02.png", 0.02),
std::make_shared<Sprite>(":/assets/images/explosion03.png", 0.02),
std::make_shared<Sprite>(":/assets/images/explosion04.png", 0.03),
std::make_shared<Sprite>(":/assets/images/explosion05.png", 0.03),
std::make_shared<Sprite>(":/assets/images/explosion06.png", 0.03),
std::make_shared<Sprite>(":/assets/images/explosion07.png", 0.02),
std::make_shared<Sprite>(":/assets/images/explosion08.png", 0.02),
std::make_shared<Sprite>(":/assets/images/explosion09.png", 0.01),
std::make_shared<Sprite>(":/assets/images/explosion10.png", 0.01)
};
bullet_sprites["default"] = {
std::make_shared<Sprite>(":/assets/images/bullet.png", 0)
};
Game::sprites.insert({"background", background_sprites});
Game::sprites.insert({"player", player_sprites});
Game::sprites.insert({"asteroid", asteroid_sprites});
Game::sprites.insert({"bullet", bullet_sprites});
// Configuring the view.
Window::view = std::make_unique<QGraphicsView>();
Window::view->setBackgroundBrush(QBrush(QColor(Qt::black)));
Window::view->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
Window::view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
Window::view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
Window::view->setMouseTracking(true);
Window::view->setFixedSize(800, 600);
// Configuring sound.
Sound::channels(5);
// Configuring menu.
int font_id = QFontDatabase::addApplicationFont(":/assets/fonts/starjedi.ttf");
Menu::font_family = QFontDatabase::applicationFontFamilies(font_id).at(0).toStdString();
auto start_game = std::make_shared<Menu>(":/assets/images/start.png",
"space debris", "START", "EXIT",
startGame, exitGame);
Window::show(start_game);
/*
Window::view->setScene(start_game.get());
Window::view->show();
Window::scene.reset();
Window::scene = move(start_game);
*/
return app.exec();
}
/**
* Starts the game.
*
* Removes the start game menu and effectively starts the game
*/
void startGame()
{
auto game = std::make_shared<Game>(gameOver);
Window::show(game);
game->start();
/*
Window::view->setScene(game.get());
Window::view->show();
game->start();
// Holds the scene reference.
Window::scene.reset();
Window::scene = std::move(game);
*/
}
/**
* Game over.
*
* It's called when the game ends.
* Indeed, removes the game and creates a game over menu.
*/
void gameOver()
{
auto game_over = std::make_shared<Menu>(":/assets/images/gameover.png",
"game over", "RESTART", "EXIT",
startGame, exitGame);
Window::show(game_over);
/*
Window::view->setScene(game_over.get());
Window::view->show();
// Holds the scene reference.
Window::scene.reset();
Window::scene = std::move(game_over);
*/
}
/**
* Exit game.
*
* Clears objects and destroy the window.
*/
void exitGame()
{
QApplication::quit();
}