Engine.cpp and Engine.h
#include "Engine.h"
#include <SFML\Graphics.hpp>
#include "TextureManager.h"
Engine::Engine()
{
}
Engine::~Engine()
{
{
delete window;
//this is so that the 'window = new sf::RenderWindow' is removed from memory when the engine stops)
delete textureManager;
//this is so that the texturemanager is also deleted once the engine is stopped
}
}
bool Engine::Init()
{
// Setting up the window
window = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "Vanity Engine V0.1");
textureManager = new TextureManager();
// If the window is already open, don't make a new one
if(!window)
return false;
LoadTextures();
return true;
}
void Engine::LoadTextures()
{
sf::Texture texture;
// Load tile textue
texture.loadFromFile("grassTile.png");
// Add the image to imageManager
textureManager->AddTexture(texture);
// Initialize grassTile as a new tile and set it's texture
grassTile = new Tile(textureManager->GetTexture(0));
}
void Engine::RenderFrame()
{
// Clear the window
window->clear();
// Draw the tile to X = 0, Y = 0 on the render window
grassTile->Draw(0,0, window);
// Render window displays the tile
window->display();
}
void Engine::ProcessInput()
{
// Initiate an event
sf::Event evt;
// Loop through all the window events
while(window->pollEvent(evt))
{
// If the user closes the program the window closes
if(evt.type == sf::Event::Closed)
window->close();
}
}
void Engine::Update()
{
}
void Engine::MainLoop()
{
//Loop until the window is closed
while(window->isOpen())
{
ProcessInput();
Update();
RenderFrame();
}
}
void Engine::Go()
{
if(!Init())
throw "Could not intialize Engine";
MainLoop();
}
#ifndef _ENGINE_H
#define _ENGINE_H
#include <SFML\Graphics.hpp>
#include "TextureManager.h"
#include "Tile.h"
class Engine
{
private:
// The window where everything is rendered
sf::RenderWindow* window;
// Adding instance of ImageManager
TextureManager* textureManager;
// Loads the images
void LoadTextures();
// Set up the tile
Tile* grassTile;
//Initialising the engine
bool Init();
//The main loop of the game
void MainLoop();
//Renders one frame
void RenderFrame();
//Processes any user input
void ProcessInput();
//Updates the internal processes of the Engine
void Update();
public:
//Constructer
Engine();
//Destructer
~Engine();
//Starts the engine
void Go();
};
#endif