31
General / SFML fps runs very low
« on: January 10, 2011, 10:29:47 pm »
Blah, I had to update the driver thanks for the tip.
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.
#ifndef ENGINE_HPP_
#define ENGINE_HPP_
class AudioManager;
#define SYSTEM Engine::SYSTEM
#define AUDIO SYSTEM.GetAudio()
#include <SFML/Audio.hpp>
class Engine
{
AudioManager * audio;
public:
Engine();
static Engine SYSTEM;
AudioManager & GetAudio();
void SetSound(AudioManager * audio);
};
#endif
#include "Headers/Engine.hpp"
Engine::Engine()
{}
AudioManager & Engine::GetAudio()
{
return *audio;
}
void Engine::SetSound(AudioManager * sound)
{
this->audio = audio;
}
#ifndef AUDIO_DAT_H
#define AUDIO_DAT_H
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
struct sAudioDATHeader
{
char uniqueID[5]; /// Unique ID used to know if this file is a DAT File from this class
char version[3]; /// Version of the DAT file format
unsigned int nb_files; /// Number of files in the DAT file
};
struct sAudioEntry
{
char name[300]; /// Name of the data file
long size; /// Size of the data file
long offset; /// Offset, in the DAT file where the file is
};
class AudioDAT
{
private :
std::string m_datfile; /// name of the DAT file
sAudioDATHeader m_header; /// file header
std::vector<sAudioEntry> m_entries; /// vector of files entries
char* m_buffer; /// Buffer pointing on a file in memory
public :
AudioDAT (void);
~AudioDAT (void);
bool Create (std::vector<std::string> files, std::string destination);
void Read (std::string source);
char* GetAudio (std::string filename);
long int GetAudioSize (std::string filename);
};
#endif
#include "Headers/AudioDAT.hpp"
AudioDAT::AudioDAT (void)
{
m_buffer = NULL;
}
AudioDAT::~AudioDAT (void)
{
if (m_buffer!=NULL)
delete (m_buffer);
}
bool AudioDAT::Create (std::vector<std::string> files, std::string destination)
{
//A file entry in order to push it in the object's std::vector
sAudioEntry entry;
//An input file stream to read each file included
std::ifstream file;
//An output file stream to write our DAT file
std::ofstream datfile;
//The buffer used to read/write the DAT file
char buffer[1];
//DATHeader
//We start by filling it with 0
memset (&m_header, 0, sizeof(m_header));
//Then we copy the ID
memcpy (m_header.uniqueID, "IMGDAT", 5);
//Then the version
memcpy (m_header.version, "0.1", 3);
//Then the number of files to include
m_header.nb_files = files.size();
//Next, we open each file in orderto create the File Entries Table
for (unsigned int i = 0; i<files.size(); i++)
{
file.open (files[i].c_str(), std::ifstream::in | std::ifstream::binary);
if (file.is_open())
{
//Filling the FileEntry with 0
memset (&entry, 0, sizeof(sAudioEntry) );
//We keep the file name
memcpy (entry.name, files[i].c_str(), strlen ( files[i].c_str() ) );
//We calculate its size
file.seekg (0, std::ios::end);
entry.size = file.tellg();
//Since we don't know exactly its final position in the DAT file, let's use 0
entry.offset = 0;
//We finished with this file
file.close();
//Finally, we add this File Entry in our std::vector
m_entries.push_back(entry);
}
else
{
//Simple error track
std::cout<<"File "<<files[i]<<" raise an error."<<std::endl;
return (false);
}
}
//Now, we know everything about our files, we can update offsets
long actual_offset = 0;
actual_offset += sizeof(sAudioDATHeader);
actual_offset += m_header.nb_files * sizeof(sAudioEntry);
for (unsigned int i=0;i<m_entries.size();i++)
{
m_entries[i].offset = actual_offset;
actual_offset += m_entries[i].size;
}
//And finally, we are writing the DAT file
datfile.open (destination.c_str(), std::ofstream::out | std::ofstream::binary);
//First, we write the header
datfile.write ((char*)&m_header, sizeof(sAudioDATHeader) );
//Then, the File Entries Table
for (unsigned int i=0;i<m_entries.size();i++)
{
datfile.write ((char*)&m_entries[i], sizeof(sAudioEntry) );
}
//Finally, we write each file
for (unsigned int i = 0; i<m_entries.size(); i++)
{
file.open (m_entries[i].name, std::ifstream::in | std::ifstream::binary);
if (file.is_open())
{
file.seekg (0, std::ios::beg);
while (file.read (buffer, 1))
{
datfile.write (buffer, 1);
}
file.close();
}
file.clear();
}
//And it's finished
datfile.close();
return (true);
}
void AudioDAT::Read (std::string source)
{
//The input file stream from which we want informations
std::ifstream datfile;
//A file entry in order to push it in the object's std::vector
sAudioEntry entry;
//Filling the header with 0
memset (&m_header, 0, sizeof(m_header));
//We open the DAT file to read it
datfile.open (source.c_str(), std::ifstream::in | std::ifstream::binary);
if (datfile.is_open())
{
//Getting to the Header position
datfile.seekg (0, std::ios::beg);
//Reading the DAT Header
datfile.read ((char*)&m_header, sizeof(sAudioDATHeader));
//Next we are reading each file entry
for (unsigned int i=0;i<m_header.nb_files;i++)
{
//Reading a File Entry
datfile.read ((char*)&entry, sizeof(sAudioEntry));
//Pushing it in our std::vector
m_entries.push_back(entry);
}
//Since all seems ok, we keep the DAT file name
m_datfile = source;
}
//Closing the DAT file
datfile.close();
}
char* AudioDAT::GetAudio (std::string filename) //The PROBLEM STARTS HERE
{
//The input file stream from which we want information
std::ifstream datfile;
//Cleaning properly an ancient file loaded
if (m_buffer != NULL) //The Program freezes here
{
delete (m_buffer);
m_buffer = NULL;
}
//First, we have to find the file needed
for (unsigned int i=0; i<m_header.nb_files;i++)
{
//If we found it
if (m_entries[i].name == filename)
{
//We are allocating memory to the buffer
m_buffer = new char[(m_entries[i].size)];
//Simple error catch
if (m_buffer==NULL)
return (NULL);
//Opening the DAT file ot read the file datas needed
datfile.open (m_datfile.c_str(), std::ifstream::in | std::ifstream::binary);
if (datfile.is_open())
{
//Going to the right position
datfile.seekg (m_entries[i].offset, std::ios::beg);
//Reading
datfile.read (m_buffer, m_entries[i].size);
//We can close the DAT file
datfile.close();
//Returning the buffer
return (m_buffer);
}
}
}
//Finally, there is no such file in our DAT file
return (NULL);
}
long int AudioDAT::GetAudioSize (std::string filename)
{
//First, we have to find the file needed
for (unsigned int i=0; i<m_header.nb_files;i++)
{
//If we found it
if (m_entries[i].name == filename)
{
//Returning the size of the file found
return (m_entries[i].size);
}
}
return (0);
}
#ifndef AUDIO_MANAGER_H
#define AUDIO_MANAGER_H
#include <map>
#include <vector>
#include <string>
#include "Headers/Engine.hpp"
#include "Headers/AudioDAT.hpp"
class AudioManager
{
std::vector<std::string> audio_container;
std::map<std::string, sf::Music> static_audio_map;
AudioDAT data;
public:
AudioManager();
const sf::Music & GetAudio(sf::Music & temp, std::string audio_id);
};
#endif
#include "Headers/AudioManager.hpp"
#include <iostream>
AudioManager::AudioManager()
{
//Create DAT file and Load Audio
audio_container.push_back("Test.ogg");
if(!data.Create(audio_container,"AUD.dat"))
{
std::cout << "Could not create AUD.dat" << std::endl;
}
data.Read("AUD.dat");
}
const sf::Music & AudioManager::GetAudio(sf::Music & temp, std::string audio_id)
{
char * temp_data = data.GetAudio(audio_id);// The Program freezes inside this function
if(temp_data == NULL)
{
std::cout << "Error Loading Audio: " << audio_id << std::endl;
exit(EXIT_FAILURE);
}
temp.OpenFromMemory(temp_data, data.GetAudioSize(audio_id));
return temp;
}
#include "Headers/Engine.hpp"
#include "Headers/AudioManager.hpp"
Engine SYSTEM;
int main()
{
sf::Music test;
AudioManager audio;
AUDIO.GetAudio(test,"test.ogg");
test.Play();
while(true)
{
}
return 0;
}
SwitchSong = 3.f;
DELAY_SONG = true;
...
ElapsedTime = Clock.GetElapsedTime();
Clock.Reset();
if( DELAY_SONG )
{
std::cout << "BEFORE: " << SwitchSong << std::endl;
SwitchSong -= ElapsedTime;
std::cout << "AFTER: " << SwitchSong << std::endl;
if( SwitchSong <= 0 )
{
BackGround.Stop();
BackGround2.Play();
BackGround2.SetLoop(true);
DELAY_SONG = false;
}
}
\\in Engine.hpp
sf::RenderWindow screen;
\\in Engine.cpp
void Engine::Draw()
{
states.back()->Draw(&screen);
}
\\in gamestate.hpp
virtual void Draw(sf::RenderWindow* canvas) = 0;
\\in introstate.hpp
void Draw(sf::RenderWindow* canvas);
\\in introstate.cpp
void CIntroState::Draw(sf::RenderWindow* canvas)
{
canvas->Clear();
canvas->Draw(OpenMenu);
canvas->Display();
}
void CIntroState::Draw(Engine *game)
void CIntroState::Draw(sf::RenderTarget *canvas)
//Engine.hpp
void HandleEvents();
void Update();
void Draw();
//Engine.cpp
void Engine::HandleEvents()
{
// let the state handle events
states.back()->HandleEvents(this);
}
void Engine::Update()
{
// let the state update the game
states.back()->Update(this);
}
void Engine::Draw()
{
// let the state draw the screen
states.back()->Draw(this);
//std::cout << "engine draw" << std::endl;
}
//gamestate.hpp
virtual void HandleEvents(Engine* game, sf::RenderWindow* canvas) = 0;
virtual void Update(Engine* game) = 0;
virtual void Draw(sf::RenderTarget *canvas) = 0;
//CIntroState.hpp
void HandleEvents(Engine* game,sf::RenderTarget *canvas);
void Update(Engine* game);
void Draw(sf::RenderTarget *canvas);
//CIntroState.cpp
void CIntroState::HandleEvents(Engine* game,sf::RenderTarget *canvas)
{
sf::Event event;
while( canvas->GetEvent(event) )
{
//std::cout << "IN EVENT" << std::endl;
if( event.Type == sf::Event::Closed )
{
game->Quit();
}
else if( event.Type == sf::Event::MouseButtonReleased && event.MouseButton.Button == sf::Mouse::Left )
{
game->ChangeState( CPlayState::Instance() );
}
}
}
void CIntroState::Update(Engine* game)
{
}
void CIntroState::Draw(sf::RenderTarget *canvas)
{
canvas->Clear();
canvas->Draw(OpenMenu);
canvas->Draw(str);
canvas->Display();
}
//in Engine.hpp
private:
sf::RenderWindow screen;
public:
sf::RenderWindow* GetWindow();
......
......
//in Engine.cpp
sf::RenderWindow* GetWindow()
{
return *screen;
}
void CIntroState::Init()
{
std::cout << "start" << std::endl;
sf::Image Open;
if( !Open.LoadFromFile("OpenGameMenu.png") )
std::cout << "ERROR" << std::endl;
OpenMenu.SetImage(Open);
str = sf::String("STRINGY!");
str.SetSize(50);
str.SetColor(sf::Color(180,0,0));
.........
.........
void CIntroState::Update(Engine* game)
{
game->screen.Clear();
}
void CIntroState::Draw(Engine* game)
{
game->screen.Draw(OpenMenu);
game->screen.Draw(str);
game->screen.Display();
}
#ifndef ENGINE_HPP_
#define ENGINE_HPP_
#include <iostream>
#include <vector>
#include <SFML/Graphics.hpp>
class CGameState;
class Engine
{
public:
void Init(const string& title, int width=1024, int height=768,
int bpp=32, bool fullscreen=false);
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 screen;
private:
std::vector<CGameState*> states;
bool m_running;
bool m_fullscreen;
};
#endif
#include "Engine.hpp"
#include "gamestate.hpp"
void Engine::Init(const string& title, int width, int height,
int bpp, bool fullscreen)
{
mode = sf::VideoMode(width, height, bpp);
Window.Create(mode, title);
m_fullscreen = fullscreen;
m_running = true;
printf("CGameEngine Init\n");
}
void Engine::Cleanup()
{
// cleanup the all states
while ( !states.empty() )
{
states.back()->Cleanup();
states.pop_back();
}
printf("GameEngine Cleanup\n");
}
void Engine::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 Engine::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 Engine::PopState()
{
// cleanup the current state
if ( !states.empty() ) {
states.back()->Cleanup();
states.pop_back();
}
// resume previous state
if ( !states.empty() ) {
states.back()->Resume();
}
}
void Engine::HandleEvents()
{
// let the state handle events
states.back()->HandleEvents(this);
}
void Engine::Update()
{
// let the state update the game
states.back()->Update(this);
}
void Engine::Draw()
{
// let the state draw the screen
states.back()->Draw(this);
}
#ifndef GAMESTATE_H
#define GAMESTATE_H
#include "Engine.hpp"
class CGameState
{
public:
virtual void Init() = 0;
virtual void Cleanup() = 0;
virtual void Pause() = 0;
virtual void Resume() = 0;
virtual void HandleEvents(Engine* game) = 0;
virtual void Update(Engine* game) = 0;
virtual void Draw(Engine* game) = 0;
void ChangeState(Engine* game, CGameState* state) {
game->ChangeState(state);
}
protected:
CGameState() { }
};
#endif
#ifndef INTROSTATE_HPP_
#define INTROSTATE_HPP_
#include <SFML/Graphics.hpp>
#include "gamestate.hpp"
class CIntroState : public CGameState
{
public:
void Init();
void Cleanup();
void Pause();
void Resume();
void HandleEvents(Engine* game);
void Update(Engine* game);
void Draw(Engine* game);
static CIntroState* Instance() {
return &m_IntroState;
}
protected:
CIntroState() { }
private:
static CIntroState m_IntroState;
sf::Sprite OpenMenu;
};
#endif
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Engine.hpp"
#include "IntroState.hpp"
CIntroState CIntroState::m_IntroState;
void CIntroState::Init()
{
std::cout << "start" << std::endl;
sf::Image Open;
if( !Open.LoadFromFile("OpenGameMenu.png") )
std::cout << "ERROR" << std::endl;
OpenMenu.SetImage(Open);
}
void CIntroState::HandleEvents(Engine* game)
{
sf::Event event;
while( game->screen.GetEvent(event) )
{
if( event.Type == sf::Event::Closed )
{
game->Quit();
}
}
}
void CIntroState::Draw(Engine* game)
{
game->screen.Draw(OpenMenu);
}
void CIntroState::Update(Engine* game)
{
game->screen.Clear();
game->screen.Display();
}
#include <iostream>
#include "Engine.hpp"
#include "IntroState.hpp"
int main()
{
Engine game;
// initialize the engine
game.Init( "Engine" );
// load the intro
game.ChangeState( CIntroState::Instance() );
// main loop
while ( game.Running() )
{
game.HandleEvents();
game.Update();
game.Draw();
}
// cleanup the engine
game.Cleanup();
std::cout << "END OF MAIN" << std::endl;
std::cin.get();
return 0;
}