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

Author Topic: SFML Resource Wrapper (Update)  (Read 5461 times)

0 Members and 1 Guest are viewing this topic.

iride

  • Jr. Member
  • **
  • Posts: 88
    • View Profile
    • Email
SFML Resource Wrapper (Update)
« on: November 10, 2012, 04:18:34 am »
I think someone already put something similar to this, but I wanted to share.
I wrote a simple resource wrapper for sf::Texture, sf::Music, sf::Font and sf::Sound
This does reference counting so that you will never load a same resource twice
Download it here:https://sourceforge.net/projects/kiwongames/files/SFML%20resource%20wrapper/

Update: 1.1
Added FontResource.
Fixed internal design using templates and inheritance. But no change in public interface.

There are 4 Resource classes.
Each of them has a static manager class to manage the resources.
But to make this simple, I made the manager class private, so you don't have any access to the manager class. (The manager's constructor is private too)
  • MusicResource-wrapper for sf::Music
  • SoundResource-wrapper for sf::Sound
  • TextureResource-wrapper for sf::Texture
  • FontResource-wrapper for sf::Font

There are only 2 things you need to know to use these
-constructor
-get()

Constructor
Each resource constructor takes one std::string argument for the name of the resource
ex) MusicResource music("music.wav");

get()
Each resource object has get() method for retrieving your resource
  • TextureResource::get() returns a reference to sf::Sprite. This sprite is binded to a sf::Texture object
  • MusicResource::get() returns a reference to sf::Music.
  • SoundResource::get() returns a reference to sf::Sound. This sound is binded to a sf::SoundBuffer object
  • FontResource::get() returns a reference to sf::Font.

Below is a sample code to show you how easy it is to use Simple Resource Wrapper for SFML

#include "MusicResource.h"
#include "SoundResource.h"
#include "TextureResource.h"


int main()
 {
     // Create the main window
     sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
 
     // Load a sprite to display
        TextureResource texture1("Pretty_Image.png");
        TextureResource texture2("Pretty_Image.png");//This won't load Pretty_Image.png twice.
        texture2.get().setRotation(10); //change rotation

        //Load a third texture to display
        TextureResource texture3("Even_Prettier.png");
        texture3.get().setPosition(100, 100);//change position
 
     // Load a music to play
        MusicResource music("good_music.wav");
        //play the music
        music.get().play();

        //Load a font
        FontResource font("C:/Users/Park/Desktop/Resources/Bedizen.ttf");
        sf::Text text("Hello", font.get());
        sf::Text text2("World", font.get());
        text2.move(400, 0);
 
     while (window.isOpen())
     {
         sf::Event event;
         while (window.pollEvent(event))
         {
             if (event.type == sf::Event::Closed)
                 window.close();
         }
         window.clear();
 
         // Draw the first texture
         window.draw(texture1.get());
         // Draw the second texture
         window.draw(texture2.get());
         // Draw the third texture
          window.draw(texture3.get());
         //draw the texts
         window.draw(text);
         window.draw(text2);
         window.display();
     }
        //load a short sound for finale
        SoundResource sound("Bye_Bye.wav");
       
        //Play the bye bye sound
        sound.get().play();
     return EXIT_SUCCESS;
 }

 
« Last Edit: November 11, 2012, 08:32:05 am by iride »

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Re: SFML Resource Wrapper
« Reply #1 on: November 10, 2012, 07:06:38 am »
Why can't the user use default SFML to do the same thing..?
Current Projects:
Technoport

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML Resource Wrapper
« Reply #2 on: November 10, 2012, 09:53:04 am »
Why can't the user use default SFML to do the same thing..?
SFML provides only basic resource handling. There is no custom lifetime management or automatic mapping of duplicate keys to the same value.

If you mean why SFML does not provide this, it is because this functionality is too high-level and many people want it slightly different. For example, I have also written a ResourceCache in the Thor library.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Re: SFML Resource Wrapper
« Reply #3 on: November 10, 2012, 02:40:22 pm »
Ah yes, I didn't see that it had reference counting. The resource manager seems pretty good though.
Current Projects:
Technoport

greeniekin

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: SFML Resource Wrapper
« Reply #4 on: November 11, 2012, 02:41:36 am »
I really like the way this is set up. You could convert existing code with nearly no changes.

You may not need this, but i like it myself.

I have a reloadModified method on my resource manager which goes through and checks the modified date when loaded and what it is now and reloads the file if it has changed.

Then just in main method set it to check every second. I can have my game running in one screen and modifying a file in another and the game auto updates.

Not really useful when your done developing though.

Krofna

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: SFML Resource Wrapper (Update)
« Reply #5 on: November 11, 2012, 08:31:50 pm »
Cool tip: Instead of annoying .get() function, why not overload operator sf::Texture&() ?
« Last Edit: November 11, 2012, 09:40:50 pm by Krofna »

 

anything