6 #include <SFML/Graphics.hpp> 13 const sf::Time TIME_PER_FRAME = sf::seconds(1.0/60.0);
16 windowSFML(sf::VideoMode(896.0, 704.0),
"arkanoid"), factory(windowSFML) {
19 transformation = transformation->
getInstance(9, 7, windowSFML.getSize().x, windowSFML.getSize().y);
21 windowSFML.setFramerateLimit(60);
24 if(!texture.loadFromFile(
"data/sprites/background/background.png")) {
25 throw runtime_error(
"Couldn't load background texture image: data/sprites/background/background.jpg");
27 texture.setSmooth(
true);
28 texture.setRepeated(
true);
29 background.setTexture(texture);
30 background.setTextureRect(sf::IntRect(0, 0, windowSFML.getSize().x, windowSFML.getSize().y));
33 void Arkanoid::initialise() {
41 vector<unique_ptr<arkanoid::Wall>> walls = std::move(factory.
createWalls());
47 void Arkanoid::loadLevel(
int level) {
50 vector<unique_ptr<arkanoid::Block>> blocks = std::move(factory.
createBlocks(
"data/levels/level_" + to_string(level) +
"/blocks.json"));
51 for(
auto &b: blocks) {
55 throw runtime_error(
"Cannot find level " + to_string(level));
64 loadLevel(currentLevel);
67 sf::Time timeSinceLastUpdate = sf::Time::Zero;
69 while(windowSFML.isOpen()) {
71 sf::Time deltaTime = clock.restart();
72 timeSinceLastUpdate += deltaTime;
74 while(timeSinceLastUpdate > TIME_PER_FRAME) {
82 loadLevel(currentLevel);
86 cout <<
"Congratulations, you beat the game! \\(^o^)/" << endl;
91 timeSinceLastUpdate -= TIME_PER_FRAME;
98 void Arkanoid::processInput() {
100 while(windowSFML.pollEvent(event)) {
103 if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) {
107 if(event.type == sf::Event::Closed) {
113 void Arkanoid::render() {
115 windowSFML.draw(background);
117 windowSFML.display();
unique_ptr< arkanoid::Player > createPlayer()
vector< unique_ptr< arkanoid::Wall > > createWalls()
void setBall(unique_ptr< arkanoid::Ball > newBall)
void setPlayer(unique_ptr< arkanoid::Player > newPlayer)
vector< unique_ptr< arkanoid::Block > > createBlocks(const string &file)
void addBlock(unique_ptr< arkanoid::Block > block)
void addWall(unique_ptr< arkanoid::Wall > wall)
unique_ptr< arkanoid::Ball > createBall()