ok heres Engine.cpp:
#include <iostream>
#include "Engine.h"
#include "Link.h"
template<typename T, int size>
int GetArrLength(T(&)[size]){return size;};
Engine::Engine()
:fps(0)
{
}
Engine::~Engine()
{
}
bool Engine::Init()
{
window = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "Legend Of Zelda: A Link To The Future");
window->SetFramerateLimit(30);
window->EnableVerticalSync(false);
if(!window)
return false;
return true;
}
void Engine::Render()
{
window->Clear();
Load_Level();
window->Display();
}
void Engine::ProcessInput()
{
sf::Event Event;
while(window->PollEvent(Event))
{
if(Event.Type == sf::Event::Closed)
window->Close();
link.KeyReleases();
}
}
void Engine::Load_Level()
{
int map[25][25] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
};
std::vector<sf::Image> images(3);
images[0].LoadFromFile("grass.png");
images[1].LoadFromFile("rock.png");
// Create the sprite
std::vector<sf::Sprite> tiles(3);
tiles[0] = sf::Sprite(images[0]); // Grass
tiles[1] = sf::Sprite(images[1]); // rock
for(int x=0; x < GetArrLength(map); ++x)
{
for(int y=0; y < GetArrLength(map[x]); ++y)
{
int tileId = map[x][y];
const sf::Image* image = tiles[tileId].GetImage();
int width = image->GetWidth();
int height = image->GetHeight();
tiles[tileId].SetPosition(x * width, y * height);
window->Draw(tiles[tileId]);
}
}
}
void Engine::Update()
{
link.Movement();
}
void Engine::FramesPerSecond()
{
fps++;
if(Clock.GetElapsedTime() >= 1.f)
{
int Framerate = (1.f / Clock.GetElapsedTime()) * 1000;
std::cout << "FPS: " << Framerate<< std::endl;
Clock.Reset();
}
}
void Engine::MainLoop()
{
while(window->IsOpened())
{
ProcessInput();
FramesPerSecond();
Update();
Render();
}
}
void Engine::Run()
{
if(!Init())
throw "Could Not Initialize!";
MainLoop();
}
and heres Engine.h:
#ifndef ENGINE_H
#define ENGINE_H
#include <SFML/Graphics.hpp>
#include "Link.h"
class Engine
{
public:
Engine();
~Engine();
void Run();
sf::RenderWindow* window;
sf::Event Event;
private:
bool Init();
void MainLoop();
void ProcessInput();
void Render();
void Load_Level();
void Update();
void FramesPerSecond();
sf::Clock Clock;
int fps;
Link link;
};
#endif // ENGINE_H
heres main.cpp:
#include <SFML/Graphics.hpp>
#include "Engine.h"
#include "Link.h"
int main(int argc, char* args[])
{
Engine* engine = new Engine();
engine->Run();
delete engine;
return 0;
}