1
System / Re: Is sf::Clock accurate enough for fixed Time step game loops?
« on: February 10, 2013, 04:18:19 pm »
Thank you very much!
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
sfml-system-d.lib
sfml-main-d.lib
sfml-graphics-d.lib
sfml-window-d.lib
sfgui-d.lib
glew32.lib
#include <SFML\System.hpp>
#include <SFML\Window.hpp>
#include <SFML\Graphics.hpp>
#include <SFGUI\SFGUI.hpp>
#include "GameEngine.h"
#include "IntroState.h"
int main(int argc, char* argv[]) {
CGameEngine game;
// initialize the engine
game.Init( "Engine Test v1.0" );
// load the intro
game.ChangeState( CIntroState::Instance() );
// main loop
while ( game.Running() )
{
game.HandleEvents();
game.Update();
game.Draw();
}
// cleanup the engine
game.Cleanup();
return EXIT_SUCCESS;
}
#pragma once
#include <SFML\Graphics.hpp>
#include <vector>
using namespace std;
class CGameState;
class CGameEngine
{
public:
//void Init(const char* title, int width=640, int height=480,
// int bpp=0, bool fullscreen=false);
void Init(const char* title, int width=800, int height=600);
void Cleanup();
void ChangeState(CGameState* state);
void PushState(CGameState* state);
void PopState();
void HandleEvents();
void Update();
void Draw();
bool Running() { return m_running; }
void Quit() { m_running = false; }
sf::RenderWindow app_window;
private:
// the stack of states
vector<CGameState*> states;
bool m_running;
bool m_fullscreen;
};
#include "GameEngine.h"
#include "GameState.h"
void CGameEngine::Init(const char* title, int width, int height)
{
app_window.create( sf::VideoMode( width, height ), title, sf::Style::Titlebar | sf::Style::Close );
printf("CGameInit\n");
}
void CGameEngine::Cleanup()
{
// cleanup the all states
while ( !states.empty() ) {
states.back()->Cleanup();
states.pop_back();
}
printf("CGameEngine Cleanup\n");
}
void CGameEngine::ChangeState(CGameState* state)
{
// cleanup the current state
if ( !states.empty() ) {
states.back()->Cleanup();
states.pop_back();
}
// store and init the new state
states.push_back(state);
states.back()->Init();
}
void CGameEngine::PushState(CGameState* state)
{
// pause current state
if ( !states.empty() ) {
states.back()->Pause();
}
// store and init the new state
states.push_back(state);
states.back()->Init();
}
void CGameEngine::PopState()
{
// cleanup the current state
if ( !states.empty() ) {
states.back()->Cleanup();
states.pop_back();
}
// resume previous state
if ( !states.empty() ) {
states.back()->Resume();
}
}
void CGameEngine::HandleEvents()
{
// let the state handle events
states.back()->HandleEvents(this);
}
void CGameEngine::Update()
{
// let the state update the game
states.back()->Update(this);
}
void CGameEngine::Draw()
{
// let the state draw the screen
states.back()->Draw(this);
}
#pragma once
#include "GameEngine.h"
class CGameState
{
public:
virtual void Init() = 0;
virtual void Cleanup() = 0;
virtual void Pause() = 0;
virtual void Resume() = 0;
virtual void HandleEvents(CGameEngine* game) = 0;
virtual void Update(CGameEngine* game) = 0;
virtual void Draw(CGameEngine* game) = 0;
void ChangeState(CGameEngine* game, CGameState* state) {
game->ChangeState(state);
}
protected:
CGameState() { }
};
#pragma once
#include <SFML\Graphics.hpp>
#include "gamestate.h"
class CIntroState : public CGameState
{
public:
void Init();
void Cleanup();
void Pause();
void Resume();
void HandleEvents(CGameEngine* game);
void Update(CGameEngine* game);
void Draw(CGameEngine* game);
static CIntroState* Instance() {
return &m_IntroState;
}
protected:
CIntroState() { }
private:
static CIntroState m_IntroState;
sf::Sprite bg;
sf::Texture* bgTexture;
int alpha;
};
#include "IntroState.h"
#include "TextureManager.h"
CIntroState CIntroState::m_IntroState;
void CIntroState::Init()
{
bgTexture = gTextureManager.getResource("C:\\Users\\SonicD007\\Desktop\\DragonBall Zeta\\BMPs\\loginscreen.png");
if (bgTexture != NULL)
bg.setTexture(*bgTexture);
bg.setPosition(0,0);
}
void CIntroState::Cleanup()
{
//if (bgTexture != NULL)
//gTextureManager.releaseResource("C:\\Users\\SonicD007\\Desktop\\DragonBall Zeta\\BMPs\\loginscreen.png");
}
void CIntroState::Pause()
{
}
void CIntroState::Resume()
{
}
void CIntroState::HandleEvents(CGameEngine* game)
{
sf::Event event;
while (game->app_window.pollEvent(event) )
{
switch (event.type)
{
case sf::Event::Closed:
game->Quit();
break;
}
}
}
void CIntroState::Update(CGameEngine* game)
{
}
void CIntroState::Draw(CGameEngine* game)
{
game->app_window.clear();
game->app_window.draw(bg);
game->app_window.display();
}
#pragma once
#include <string>
#include <map>
template< class T >
class ResourceManager {
public:
typedef std::pair< std::string, T* > Resource;
typedef std::map< std::string, T* > ResourceMap;
private:
ResourceMap m_resource;
T* find( const std::string& strId ) {
T* resource = NULL;
typename ResourceMap::iterator it = m_resource.find( strId );
if ( it != m_resource.end() ) {
resource = it->second;
}
return resource;
}
protected:
virtual T* load( const std::string& strId ) = 0;
public:
ResourceManager(){}
virtual ~ResourceManager() { releaseAllResources(); }
T* getResource( const std::string& strId) {
T* resource = find( strId );
if ( resource == NULL ) {
resource = load( strId);
// If the resource loaded successfully
if ( resource != NULL )
m_resource.insert( Resource( strId, resource ) );
}
return resource;
}
void releaseResource( const std::string& strId) {
T* resource = find( strId );
if ( resource != NULL ) {
delete resource;
m_resource.erase( m_resource.find( strId ) );
}
}
void releaseAllResources() {
while( m_resource.begin() != m_resource.end() )
delete m_resource.begin()->second;
m_resource.erase( m_resource.begin() );
}
};
#pragma once
#include <SFML\Graphics.hpp>
#include "ResourceManager.h"
class TextureManager : public ResourceManager< sf::Texture > {
private:
protected:
virtual sf::Texture* load( const std::string& strId );
public:
};
extern TextureManager gTextureManager;
#include <iostream>
#include "TextureManager.h"
using namespace std;
TextureManager gTextureManager;
sf::Texture* TextureManager::load( const std::string& strId ) {
sf::Texture* image = new sf::Texture();
if( !image->loadFromFile(strId) ) {
cout << "[WARN] ImageManager failed to load: " << strId << endl;
delete image;
image = NULL;
}
return image;
}
An internal OpenGL call failed in Texture.cpp (95) : GL_INVALID_OPERATION, the s
pecified operation is not allowed in the current state