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

Author Topic: Drawing using RenderTarget not Drawing to Window [SOLVED]  (Read 3555 times)

0 Members and 2 Guests are viewing this topic.

Charsmud

  • Newbie
  • *
  • Posts: 30
    • View Profile
Drawing using RenderTarget not Drawing to Window [SOLVED]
« on: December 02, 2014, 03:57:22 am »
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)
{
}
« Last Edit: December 03, 2014, 01:50:19 am by Charsmud »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Drawing using RenderTarget not Drawing to Window
« Reply #1 on: December 02, 2014, 07:46:08 am »
And where do you call clear and display on the render target?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Charsmud

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Drawing using RenderTarget not Drawing to Window
« Reply #2 on: December 03, 2014, 12:08:19 am »
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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Drawing using RenderTarget not Drawing to Window
« Reply #3 on: December 03, 2014, 12:25:15 am »
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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Drawing using RenderTarget not Drawing to Window
« Reply #4 on: December 03, 2014, 12:34:11 am »
On top of what eXpl0it3r said, it looks like you're using mSprite a lot here and there's not a whole lot of usage of mText.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Charsmud

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Drawing using RenderTarget not Drawing to Window
« Reply #5 on: December 03, 2014, 12:34:30 am »
Hm... When I switched over the target.draw(mSprite, states) to target.draw(mText, states), I get an Access violation reading location error.  Here is the stack call:

    sfml-graphics-d-2.dll!0f8ad316()    
    [Frames below may be incorrect and/or missing, no symbols loaded for sfml-graphics-d-2.dll]   
    sfml-graphics-d-2.dll!0f8ab16e()    
    sfml-graphics-d-2.dll!0f8a8c5a()    
    sfml-graphics-d-2.dll!0f8a5f6d()    
    sfml-graphics-d-2.dll!0f8f76a6()    
    sfml-graphics-d-2.dll!0f8d4cc3()    
>   Byte.exe!TextElement::drawCurrent(sf::RenderTarget & target, sf::RenderStates states)  Line 22 + 0x19 bytes   C++
    Byte.exe!TextWindow::drawCurrent(sf::RenderTarget & target, sf::RenderStates states)  Line 29 + 0x3c bytes   C++
    Byte.exe!SceneNode::draw(sf::RenderTarget & target, sf::RenderStates states)  Line 55 + 0x2c bytes   C++
    Byte.exe!SceneNode::drawChildren(sf::RenderTarget & target, sf::RenderStates states)  Line 67 + 0x3b bytes   C++
    Byte.exe!SceneNode::draw(sf::RenderTarget & target, sf::RenderStates states)  Line 57   C++
    Byte.exe!SceneNode::drawChildren(sf::RenderTarget & target, sf::RenderStates states)  Line 67 + 0x3b bytes   C++
    Byte.exe!SceneNode::draw(sf::RenderTarget & target, sf::RenderStates states)  Line 57   C++
    sfml-graphics-d-2.dll!0f8d4cc3()    
    Byte.exe!World::draw()  Line 51 + 0x43 bytes   C++
    Byte.exe!GameState::draw()  Line 14   C++
    Byte.exe!StateStack::draw()  Line 33 + 0x23 bytes   C++
    Byte.exe!Application::render()  Line 100   C++
    Byte.exe!Application::run()  Line 73   C++
    Byte.exe!main()  Line 16   C++
    Byte.exe!__tmainCRTStartup()  Line 555 + 0x19 bytes   C
    Byte.exe!mainCRTStartup()  Line 371   C
    kernel32.dll!74c6338a()    
    ntdll.dll!77299f72()    
    ntdll.dll!77299f45()    

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Drawing using RenderTarget not Drawing to Window
« Reply #6 on: December 03, 2014, 12:44:10 am »
Did you change what I said?

If you get an access violation run your debugger and find out why. You're a programmer, you should know how to work with a debugger, otherwise look it up. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Drawing using RenderTarget not Drawing to Window
« Reply #7 on: December 03, 2014, 01:29:56 am »
target.draw() was not the only place that mSprite appears. You initialise it and set it up but never do for mText.

Also, sort out your font as mentioned above already  :P
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Charsmud

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Drawing using RenderTarget not Drawing to Window
« Reply #8 on: December 03, 2014, 01:50:10 am »
Seems like the mText(text, font, textSize) was causing the problem.  Setting the constructor to mText() and filling in with the methods fixed the problem.  :)