C++ Project 2016-2017: Arkanoid
arkanoid.cpp
Go to the documentation of this file.
1 
3 #include "arkanoid.h"
5 
6 #include <SFML/Graphics.hpp>
7 #include <iostream>
8 #include <memory>
9 #include <string>
10 
11 using namespace std;
12 
13 const sf::Time TIME_PER_FRAME = sf::seconds(1.0/60.0);
14 
16 windowSFML(sf::VideoMode(896.0, 704.0), "arkanoid"), factory(windowSFML) {
17 
18  // Create singleton Transformation first instance
19  transformation = transformation->getInstance(9, 7, windowSFML.getSize().x, windowSFML.getSize().y);
20 
21  windowSFML.setFramerateLimit(60);
22 
23  // Create background
24  if(!texture.loadFromFile("data/sprites/background/background.png")) {
25  throw runtime_error("Couldn't load background texture image: data/sprites/background/background.jpg");
26  }
27  texture.setSmooth(true);
28  texture.setRepeated(true); // Repeat background texture, so that it fits the screen
29  background.setTexture(texture);
30  background.setTextureRect(sf::IntRect(0, 0, windowSFML.getSize().x, windowSFML.getSize().y));
31 }
32 
33 void Arkanoid::initialise() {
34  // Create Player
35  world.setPlayer(std::move(factory.createPlayer()));
36 
37  // Create Ball
38  world.setBall(std::move(factory.createBall()));
39 
40  // Create Walls
41  vector<unique_ptr<arkanoid::Wall>> walls = std::move(factory.createWalls());
42  for(auto &w: walls) {
43  world.addWall(std::move(w));
44  }
45 }
46 
47 void Arkanoid::loadLevel(int level) {
48  // Create Blocks
49  try {
50  vector<unique_ptr<arkanoid::Block>> blocks = std::move(factory.createBlocks("data/levels/level_" + to_string(level) + "/blocks.json"));
51  for(auto &b: blocks) {
52  world.addBlock(std::move(b));
53  }
54  } catch(...) {
55  throw runtime_error("Cannot find level " + to_string(level));
56  }
57 }
58 
59 void Arkanoid::run(int level) {
60  currentLevel = level;
61 
62  // Create game entities and load first level
63  initialise();
64  loadLevel(currentLevel);
65 
66  sf::Clock clock;
67  sf::Time timeSinceLastUpdate = sf::Time::Zero;
68 
69  while(windowSFML.isOpen()) {
70 
71  sf::Time deltaTime = clock.restart();
72  timeSinceLastUpdate += deltaTime;
73 
74  while(timeSinceLastUpdate > TIME_PER_FRAME) {
75  processInput();
76  if(!world.levelEnded()) {
77  world.update();
78  } else {
79  // Level ended --> load next level
80  currentLevel++;
81  try {
82  loadLevel(currentLevel);
83  } catch(...) {
84  // No more levels available --> end game
85  windowSFML.close();
86  cout << "Congratulations, you beat the game! \\(^o^)/" << endl;
87  return;
88  }
89  world.reset();
90  }
91  timeSinceLastUpdate -= TIME_PER_FRAME;
92  }
93 
94  render();
95  }
96 }
97 
98 void Arkanoid::processInput() {
99  sf::Event event;
100  while(windowSFML.pollEvent(event)) {
101 
102  // Reset world (if game happen to bug out)
103  if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) {
104  world.reset();
105  }
106 
107  if(event.type == sf::Event::Closed) {
108  windowSFML.close();
109  }
110  }
111 }
112 
113 void Arkanoid::render() {
114  windowSFML.clear();
115  windowSFML.draw(background);
116  world.draw();
117  windowSFML.display();
118 }
static Transformation * getInstance()
unique_ptr< arkanoid::Player > createPlayer()
void run(int level=1)
Definition: arkanoid.cpp:59
vector< unique_ptr< arkanoid::Wall > > createWalls()
void reset()
Definition: world.cpp:109
void setBall(unique_ptr< arkanoid::Ball > newBall)
Definition: world.cpp:97
void setPlayer(unique_ptr< arkanoid::Player > newPlayer)
Definition: world.cpp:101
vector< unique_ptr< arkanoid::Block > > createBlocks(const string &file)
void addBlock(unique_ptr< arkanoid::Block > block)
Definition: world.cpp:89
bool levelEnded() const
Definition: world.cpp:105
void addWall(unique_ptr< arkanoid::Wall > wall)
Definition: world.cpp:93
void draw() const
Definition: world.cpp:77
Arkanoid()
Definition: arkanoid.cpp:15
void update()
Definition: world.cpp:55
unique_ptr< arkanoid::Ball > createBall()