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.1Added 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()
ConstructorEach 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;
}