Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

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.


Messages - AdventWolf

Pages: 1 2 [3] 4
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.

32
General / SFML fps runs very low
« on: January 10, 2011, 09:20:38 pm »
My computer blue screened last week so I've been trying to get everything back to normal. When it first happened, I bought a new hard drive and installed SFML and Visual Studio and the game ran just fine. But I was able to get my old hard drive back in working condition and I installed SFML and Visual Studio back on it but the fps averages around 8.

Is there something in SFML or Visual Studio that might cause this?

33
General / Need help with an audio manager using a DAT file
« on: January 05, 2011, 02:43:07 am »
I'm using the tutorial on storing your files inside a .DAT file.
The problem is inside this function, the program just freezes.
char* AudioDAT::GetAudio (std::string filename)

If I comment out this part: if (m_buffer != NULL){}
then the debugger points to the next part of the function to be the problem.

Any tips are appreciated!

Code: [Select]

#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


Code: [Select]
#include "Headers/Engine.hpp"

Engine::Engine()
{}

AudioManager & Engine::GetAudio()
{
return *audio;
}

void Engine::SetSound(AudioManager * sound)
{
this->audio = audio;
}


Code: [Select]
#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


Code: [Select]
#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);
}


Code: [Select]
#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


Code: [Select]
#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;
}


Code: [Select]
#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;
}

34
SFML projects / It's Raining Snow! - A retro-arcade style Christmas game!
« on: December 24, 2010, 07:11:09 pm »
Cool game, I'd definitely add some more to it.

35
System / sf::Clock countdown timer (solved)
« on: December 24, 2010, 06:28:15 pm »
I figured it out, thanks anyway  :) .

36
System / sf::Clock countdown timer (solved)
« on: December 23, 2010, 11:34:39 pm »
I created a timer just fine it my main menu state but I'm trying to create a countdown timer in different state but it isn't working. Though for some reason the Clock creates a huge number for ElapsedTime, so the countdown of 3 seconds only lasts a millisecond.

Here is what I have:
Code: [Select]

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;
}
}


Thanks.

37
General / Questions about managing states
« on: November 12, 2010, 03:27:24 am »
I finally found some more free time today so I went back at it and it works good. This is what I have:

Code: [Select]
\\in Engine.hpp
sf::RenderWindow screen;


Code: [Select]
\\in Engine.cpp
void Engine::Draw()
{
states.back()->Draw(&screen);
}


Code: [Select]
\\in gamestate.hpp
virtual void Draw(sf::RenderWindow* canvas) = 0;


Code: [Select]
\\in introstate.hpp
void Draw(sf::RenderWindow* canvas);


Code: [Select]
\\in introstate.cpp
void CIntroState::Draw(sf::RenderWindow* canvas)
{
canvas->Clear();
canvas->Draw(OpenMenu);
canvas->Display();
}


Now this code works, but is it correct? I thought it was a little awkward to have &screen but it worked. Thanks so much for your help.

38
General / Questions about managing states
« on: November 07, 2010, 11:58:06 pm »
I'm not sure what the deal was with screen there, I initially tried the reference but it didn't work so I tried the pointer.

And I see what you mean with RenderTarget, that seems like a great idea but changing
Code: [Select]
void CIntroState::Draw(Engine *game)

to
Code: [Select]
void CIntroState::Draw(sf::RenderTarget *canvas)

caused nothing but problems, should I change it in Engine.hpp and Engine.cpp? Because they are initially empty.

I have this:
Code: [Select]
//Engine.hpp
void HandleEvents();
void Update();
void Draw();

Code: [Select]
//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;
}

Code: [Select]
//gamestate.hpp
virtual void HandleEvents(Engine* game, sf::RenderWindow* canvas) = 0;
virtual void Update(Engine* game) = 0;
virtual void Draw(sf::RenderTarget *canvas) = 0;

Code: [Select]
//CIntroState.hpp
void HandleEvents(Engine* game,sf::RenderTarget *canvas);
void Update(Engine* game);
void Draw(sf::RenderTarget *canvas);

Code: [Select]
//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();
}


There is a lot of (this) conflict, so how do I need to format the code somehow to allow me to use RenderTarget? I spent a while trying to change it up but it seems to have endless errors.

39
General / Questions about managing states
« on: November 07, 2010, 11:02:44 pm »
Ok, screen is private and I suppose you mean I need a getter for it so the states can access it?

I have this:
Code: [Select]

//in Engine.hpp
private:
sf::RenderWindow screen;

public:
sf::RenderWindow* GetWindow();

......
......

//in Engine.cpp
sf::RenderWindow* GetWindow()
{
return *screen;
}


But i get an undeclared identifier error on screen for some reason. I only get the error in the GetWindow() function.

40
General / Questions about managing states
« on: November 07, 2010, 09:51:38 pm »
Yeah that was the problem. I had the Sprite object saved in the class, but I didn't have the Image object saved in the class.

And yeah I know the difference with copying and references, but I didn't know I had to keep the Image object around even after declaring the Sprite object.

EDIT: I created another state and kept switching between the two and I didn't see any memory leaks so that's cool  :D. I'm going to keep messing with it to figure some stuff out. This takes a load off, I can finally do states.

41
General / Questions about managing states
« on: November 07, 2010, 09:30:05 pm »
"Make it work, do it right then make it fast", I've been going by that mindset, when I try to do something I try to do it as simple as I can then expand from there.

And thanks! I probably wouldn't have noticed the mistake in the Update() function. I am getting closer now, when I run the program I can see a white background where the sprite should be, but the sprite doesn't show.

Though I did try to display a string and it works just fine.

Code: [Select]
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();
}


It goes - create objects ->
clear -> draw -> display -> (loop)

It seems right, but I'm not sure where the problem lies.

EDIT: Well I copied the code to create the sprite over to the draw function and the sprite appeared, so I need to fix the code in the Init() function to get the sprite to appear.

EDIT: Ok I have it in a workable condition now, lol. I'll try to add another state to the program. What tips do you have to make it better?

42
General / Questions about managing states
« on: November 07, 2010, 07:53:01 pm »
Could you show an example please? I spent some more time trying to work it out but I couldn't get it   :(

43
General / Questions about managing states
« on: November 07, 2010, 07:03:06 pm »
Alright it has been a while since I have programmed but I have found some free time. State managing has really held me back because I can't seem to grasp how to do it! I started trying to put together a very simple state manager by looking at different resources but I'm having problems.

Here's the basic jest of it:

Code: [Select]
#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


Code: [Select]
#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);
}


Code: [Select]
#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


Code: [Select]
#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


Code: [Select]
#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();
}


Code: [Select]
#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;
}


But my problem is I'm not sure how to manage the Window, when I try to draw a sprite to the window, I get nothing. In here, I create a window in the engine class, but how do I reach the window while I'm in the states to change the window? What would be a simple implementation of the IntroState to create an window output, while being able to switch to another state that uses the same window?

44
Window / Possibility of displaying Zoom/Rotate in increments
« on: September 24, 2010, 05:40:05 am »
Thanks guys, I've kind of backed away from sfml for a bit to head back to the books to better understand the basics. I'll be sure to look into this.

45
System / sfml on 64bit archetecture
« on: September 11, 2010, 10:19:31 pm »
I use w7 64 bit so you should be good to go.

Pages: 1 2 [3] 4