Teo-CD
d53f8b32e9
It caused the game to update only if events were received (Mouse movement, button press, etc)
70 lines
1.6 KiB
C++
70 lines
1.6 KiB
C++
#include <memory>
|
|
|
|
//
|
|
// Created by trotfunky on 06/06/19.
|
|
//
|
|
|
|
#include "Game.h"
|
|
|
|
|
|
Game::Game(std::vector<std::string>& levels, std::vector<std::string>& textures) : levelFiles(std::move(levels)),
|
|
textureFiles(std::move(textures))
|
|
{
|
|
loadTextures();
|
|
}
|
|
|
|
void Game::loadLevel(int levelId)
|
|
{
|
|
pugi::xml_document document;
|
|
pugi::xml_parse_result result = document.load_file((pro_maat::levelFolder+levelFiles.at(levelId)).c_str());
|
|
|
|
if(!result)
|
|
{
|
|
pro_maat::errorWindow(result.description());
|
|
}
|
|
|
|
currentLevel = std::make_unique<Level>(document,textures);
|
|
}
|
|
|
|
void Game::loadTextures()
|
|
{
|
|
textures.reserve(textureFiles.size());
|
|
for(const std::string& filePath : textureFiles)
|
|
{
|
|
textures.emplace_back(std::make_unique<sf::Texture>());
|
|
textures.back()->loadFromFile(pro_maat::textureFolder+filePath);
|
|
}
|
|
}
|
|
|
|
void Game::runGame()
|
|
{
|
|
sf::RenderWindow window(sf::VideoMode(1280,1024),"Project Maat");
|
|
|
|
sf::Clock clock;
|
|
|
|
while (window.isOpen())
|
|
{
|
|
sf::Event event;
|
|
while (window.pollEvent(event))
|
|
{
|
|
if (event.type == sf::Event::Closed)
|
|
{
|
|
window.close();
|
|
}
|
|
}
|
|
if (clock.getElapsedTime().asMilliseconds() >= 200)
|
|
{
|
|
currentLevel->runStep();
|
|
clock.restart();
|
|
}
|
|
window.clear(sf::Color::White);
|
|
// TODO : Consider drawing in a sf::RenderTexture to allow positioning the level at a fixed position ?
|
|
currentLevel->render(window);
|
|
window.display();
|
|
}
|
|
}
|
|
|
|
Game::operator bool() const
|
|
{
|
|
return (currentLevel ? true : false);
|
|
}
|