Skip to content

Instantly share code, notes, and snippets.

@itsbth
Created July 23, 2010 06:10
Show Gist options
  • Save itsbth/487087 to your computer and use it in GitHub Desktop.
Save itsbth/487087 to your computer and use it in GitHub Desktop.
/*
* Game.cpp
*
* Created on: Jul 3, 2010
* Author: itsbth
*/
#include "Game.h"
#include <SFML/System/Clock.hpp>
namespace pw
{
Game::Game(sf::RenderWindow& App) :
App(App)
{
// TODO Auto-generated constructor stub
}
Game::~Game()
{
// TODO Auto-generated destructor stub
while (!States.empty())
States.pop(); // Free all states
}
void Game::Run()
{
sf::Clock Clock;
while (!States.empty())
{
sf::Event Event;
while (App.GetEvent(Event))
;
State& Top = *States.top();
if (!Top.Update(Clock.GetElapsedTime()))
{
States.pop();
continue;
}
Clock.Reset();
App.Clear();
Top.Draw(App);
App.Display();
}
}
/**
* Get the input
*/
const sf::Input& Game::GetInput() const
{
return App.GetInput();
}
}
/*
* Game.h
*
* Created on: Jul 3, 2010
* Author: itsbth
*/
#ifndef GAME_H_
#define GAME_H_
#include <stack>
#include <SFML/Graphics/RenderWindow.hpp>
#include "IGame.h"
#include "State.h"
namespace pw
{
class Game : public pw::IGame
{
public:
Game(sf::RenderWindow& App);
virtual ~Game();
void Run();
template<class T>
T& PushState()
{
T *ptr = new T(*this);
States.push(state_ptr(ptr));
return *ptr;
}
// IGame members
const sf::Input& GetInput() const;
// End IGame members
private:
sf::RenderWindow& App;
std::stack<pw::state_ptr> States;
};
}
#endif /* GAME_H_ */
/*
* main.cpp
*
* Created on: Jul 3, 2010
* Author: itsbth
*/
#include <iostream>
#include <cmath>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Shape.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/String.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/System/Randomizer.hpp>
#include "Game.h"
#include "State.h"
#include "DisplayList.h"
class TestState : public pw::State
{
public:
TestState(pw::IGame& Game) :
State(Game)
{
LED.LoadFromFile("Resources/Inconsolata.otf");
Text.SetFont(LED);
Text.SetText("LOADING...");
sf::FloatRect R = Text.GetRect();
DList.Add(&Text);
Sym.LoadFromFile("Resources/symbol.png");
Spr.SetImage(Sym);
Spr.Move(320, 200);
Spr.SetCenter(Sym.GetWidth() / 3, Sym.GetHeight() / 2);
Spr.Scale(0.5, 0.5);
DList.Add(&Spr);
}
virtual bool Update(float DeltaTime)
{
//std::cout << "Hello from TestState<" << this << ">" << std::endl;
const sf::Input& Inp = Game.GetInput();
Text.SetStyle(Inp.IsKeyDown(sf::Key::Space) ? 0x4 : 0x0);
Spr.Rotate(180 * DeltaTime);
Text.SetText(static_cast<int> (LeTime.GetElapsedTime() * 1000) % 1000
> 500 ? "Loading" : "Loading...");
return !Inp.IsKeyDown(sf::Key::Escape);
}
virtual void Draw(sf::RenderWindow& App)
{
//App.Draw(sf::Shape::Line(XStart, 0, 100, 100, 2, sf::Color(255, 0, 0)));
Text.SetColor(sf::Color(0, 0xAA, 0, 127 + std::sin(
LeTime.GetElapsedTime()) * 127));
//App.Draw(Text);
for (int i = 0; i < 6; i++)
{
Spr.SetPosition(320 + sf::Randomizer::Random(-20, 20), 250
+ sf::Randomizer::Random(-20, 20));
Spr.SetColor(sf::Color(255, 255, 255, sf::Randomizer::Random(127,
255)));
App.Draw(Spr);
}
App.Draw(DList);
}
private:
sf::Font LED;
sf::String Text;
sf::Clock LeTime;
sf::Image Sym;
sf::Sprite Spr;
pw::DisplayList DList;
};
int main(int argc, char **argv)
{
std::cout << "Hello, World!" << std::endl;
sf::RenderWindow App(sf::VideoMode(640, 480), "Test app");
pw::Game Game(App);
//Game.PushState<pw::State>();
Game.PushState<TestState> ();
Game.Run();
return 0;
}
/*
* State.cpp
*
* Created on: Jul 3, 2010
* Author: itsbth
*/
#include <iostream>
#include "State.h"
namespace pw
{
State::State(IGame& Game) :
Game(Game)
{
// TODO Auto-generated constructor stub
}
State::~State()
{
// TODO Auto-generated destructor stub
}
bool State::Update(float DeltaTime)
{
return true;
}
void State::Draw(sf::RenderWindow& App)
{
}
}
/*
* State.h
*
* Created on: Jul 3, 2010
* Author: itsbth
*/
#ifndef STATE_H_
#define STATE_H_
#include <boost/smart_ptr.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include "IGame.h"
namespace pw
{
class State : sf::NonCopyable
{
public:
State(pw::IGame& Game);
virtual ~State();
virtual void Draw(sf::RenderWindow& App);
virtual bool Update(float DeltaTime);
protected:
pw::IGame& Game;
};
typedef boost::shared_ptr<pw::State> state_ptr;
}
#endif /* STATE_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment