Ive been trying to work out the best way to manage the drawing of all sprites, text or any sf::Drawable for that matter within my game.
I wanted all the drawables to be displayed stored in a vector so that I can draw them all at once within the main function instead of passing around a reference to the RenderWindow.
To solve this I created a vector of sf::Drawable pointers so that I can fill the vector with the any object that is derived from sf::Drawable
Here is my solution:
main.cpp
#include <vector>
#include "title_screen.h"
int main()
{
sf::RenderWindow window(...);
std::vector<sf::Drawable*> drawBuffer;
TitleScreen titleScreen(window.getSize());
titleScreen.pushDrawablesToVector(drawBuffer);
// Event Handler
while (window.isOpen) {...}
window.clear(sf::Color::White);
for (std::vector<sf::Drawable*>::iterator i = drawBuffer.begin(); i < drawBuffer.end(); i++)
{
window.draw(**i);
}
window.display();
}
title_screen.h
#include <vector>
#include <SFML/Graphics.hpp>
class TitleScreen
{
sf::Text title;
sf::Text newGame;
public:
// Constructor which sets text position using winSize and strings
TitleScreen(sf::Vector2u winSize) {...}
void pushDrawablesToVector(std::vector<sf::Drawable*> &dB)
{
dB.push_back(&title);
dB.push_back(&newGame);
}
};
The code compiles and runs fine, the problem is I get a black white screen without any text.
Im assuming this has something to do with the size of sf::Drawable* vs sf::Text* (and sf::Text private member data not inherited from sf::Drawable).
Any ideas or suggestions?
Thanks in advance