I am attempting to store a variable amounts of objects inside of another and iterate through the list, drawing each as I go. Currently, the objects do iterate through and the drawCurrent method does indeed run, but nothing is rendered besides the base that the other objects (called Elements) should be rendering on. Here are related files:
#pragma once
#include "Element.hpp"
#include <SFML/Graphics/Text.hpp>
class TextElement : public Element
{
public:
TextElement(sf::String text, sf::Font font, int charSize, int xOrig, int yOrig, int xPos, int yPos);
virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const;
virtual void updateCurrent(sf::Time dt);
private:
sf::Texture mTexture;
sf::Sprite mSprite;
sf::Text mText;
int xpos, ypos;
};
#include "TextElement.hpp"
#include "ResourceIdentifiers.hpp"
#include <iostream>
TextElement::TextElement(sf::String text, sf::Font font, int textSize, int xOrig, int yOrig, int xPos, int yPos)
: mText(text, font, textSize)
, xpos(xPos)
, ypos(yPos)
, mSprite(mTexture)
{
mTexture.loadFromFile("Media/Textures/Raptor.png");
mSprite.setTexture(mTexture);
mSprite.setOrigin(100, 100);
mText.setOrigin(xOrig, yOrig);
mText.setColor(sf::Color::Green);
}
void TextElement::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const
{
std::cout << "TEXTELEMENT" << std::endl;
target.draw(mSprite, states);
std::cout << "DONE " << std::endl;
}
void TextElement::updateCurrent(sf::Time dt)
{
//Do nothing, text doesn't need to update!
}
#include <SFML\Graphics\Sprite.hpp>
#include <SFML\Graphics\RenderStates.hpp>
#include <SFML\Graphics\RenderTarget.hpp>
#include <SFML\Graphics\Text.hpp>
#include <SFML\System\Time.hpp>
#include <SFML\Window\Window.hpp>
#include <list>
#include "Element.hpp"
#include "Entity.hpp"
#include "ResourceIdentifiers.hpp"
#include "TextElement.hpp"
class TextWindow : public Entity
{
public:
TextWindow(const TextureHolder& textures, const FontHolder& fonts, sf::Window& window, Fonts::ID fontID, sf::String text);
void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const;
void updateCurrent(sf::Time dt);
void addElements(sf::String text, sf::Font font);
private:
sf::Sprite mSprite;
int mHitpoints;
sf::Window& mWindow;
float xPos, yPos;
std::list<std::unique_ptr<Element>> mElements;
};
#include "TextWindow.hpp"
#include "TextElement.hpp"
#include <iostream>
TextWindow::TextWindow(const TextureHolder& textures, const FontHolder& fonts, sf::Window& windows, Fonts::ID fontID, sf::String text)
: mSprite(textures.get(Textures::WindowDefault))
, mHitpoints(10)
, mWindow(windows)
{
sf::FloatRect bounds = mSprite.getLocalBounds();
mSprite.setOrigin(bounds.width / 2.f, bounds.height / 2.f);
addElements(text, fonts.get(fontID));
}
void TextWindow::addElements(sf::String text, sf::Font font)
{
std::unique_ptr<TextElement> e(new TextElement(text, font, 10, xPos, yPos, 10, 10));
mElements.push_back(std::move(e));
}
void TextWindow::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(mSprite, states);
for(std::list<std::unique_ptr<Element>>::const_iterator iterator = mElements.begin(); iterator != mElements.end(); ++iterator)
{
//std::cout << "IN TEXTWINDOW PRINT ITERATOR" << std::endl;
(*iterator).get()->drawCurrent(target, states);
}
}
void TextWindow::updateCurrent(sf::Time dt)
{
}
void Application::render()
{
mWindow.clear();
mStateStack.draw();
mWindow.setView(mWindow.getDefaultView());
mWindow.draw(mStatisticsText);
mGui.draw();
mWindow.display();
}
I am able to draw other things to the screen except the TextElement inside of TextWindow. Rendering sprites text, etc. work.
Ah, now that I'm not at my phone, I think this line is your issue:
void TextWindow::addElements(sf::String text, sf::Font font)
You're copying the font object, thus inside the addElements function you have a temporary font object, which gets deleted after the function is left, leaving the text object inside mElements pointing to a none existing font, thus nothing is rendered.