SFML community forums

General => SFML projects => Topic started by: iride on November 10, 2012, 04:18:34 am

Title: SFML Resource Wrapper (Update)
Post by: iride 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/ (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)

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

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

 
Title: Re: SFML Resource Wrapper
Post by: The Terminator on November 10, 2012, 07:06:38 am
Why can't the user use default SFML to do the same thing..?
Title: Re: SFML Resource Wrapper
Post by: Nexus 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 (http://www.bromeon.ch/libraries/thor/v2.0/tutorial-resources.html) in the Thor library.
Title: Re: SFML Resource Wrapper
Post by: The Terminator 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.
Title: Re: SFML Resource Wrapper
Post by: greeniekin 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.
Title: Re: SFML Resource Wrapper (Update)
Post by: Krofna on November 11, 2012, 08:31:50 pm
Cool tip: Instead of annoying .get() function, why not overload operator sf::Texture&() ?