SFML community forums

Help => Graphics => Topic started by: traxys on May 25, 2014, 06:33:20 pm

Title: Texture loaded , drawn , but not shown on the screen
Post by: traxys on May 25, 2014, 06:33:20 pm
I am coding a game , and just started some of the graphical part so I just started to try using SFML for it , but I had a bug where a sprite would not show on screen, so I tought that ther was bugs deep into my code , and so I made a fresh project with what could be considered like the most basic code to show a Sprite , but it is doing the same thing , and I can't understand why...

     Here is the basic code(even if you could guess it x) ):

#include <iostream>
#include <SFML/Graphics.hpp>

using namespace std;
using namespace sf;

int main(){
    RenderWindow window(VideoMode(500,500),"Test");

    Texture lavaFlow;
    lavaFlow.loadFromFile("lavaFlow.png");

    Sprite lavaFlowSprite;
    lavaFlowSprite.setTexture(lavaFlow);

    while(window.isOpen()){
        Event event;

        while(window.pollEvent(event)){
            if(event.type == Event::Closed){
                window.close();
            }
            window.clear();

            window.draw(lavaFlowSprite);

            window.display();
        }
    }
    return 0;
}

And few things to know , since I am compiling on Debug , some of the error shows , so I know that the Image has been loaded , and the image was firts in jpeg so I thank that it was maybe a type problem , so I converted it into png

Thanks to anyone who could help me !
Title: AW: Texture loaded , drawn , but not shown on the screen
Post by: eXpl0it3r on May 25, 2014, 06:52:25 pm
Try
setTexture(LavaFlow, true)

That will reset the texture rect. If you never set the texture rect, nothing will be "cut out" from the texture.
Title: Re: Texture loaded , drawn , but not shown on the screen
Post by: Geheim on May 25, 2014, 06:52:57 pm
window.clear();

window.draw(lavaFlowSprite);

window.display();
This stuff should not be in the event loop ;)
Title: Re: Texture loaded , drawn , but not shown on the screen
Post by: traxys on May 25, 2014, 07:58:34 pm
Where should I put it for it to be refersehd  ??? ?
Title: Re: Texture loaded , drawn , but not shown on the screen
Post by: traxys on May 25, 2014, 08:02:58 pm
@eXpl0it3r

This doesn't do anything : My screen is still a big black box
Title: AW: Re: Texture loaded , drawn , but not shown on the screen
Post by: eXpl0it3r on May 25, 2014, 08:11:17 pm
Where should I put it for it to be refersehd  ??? ?
If you don't know what the event loop is and since you set things up wrong, you should definitely read the official tutorials, it's all explained there.
Title: Re: Texture loaded , drawn , but not shown on the screen
Post by: traxys on May 25, 2014, 09:35:56 pm
Ok so looking at my code I understood the problem of it (for the event loop) , yeah my thing was in the wrong loop because it was being refreshed only on events so I bumped it little down , in the while window.isOpen() loop , but I still have a big black screen ........

Looking at the tutorials I saw the one on drawing shapes , so should I draw A ssquare and apply a texture to it instead ?

EDIT: Nevermind RectangleShape has the same problem, could it be the texture not loading properly ?
Title: Re: Texture loaded , drawn , but not shown on the screen
Post by: Jesper Juhl on May 25, 2014, 09:48:06 pm
I'll just leave this here: https://github.com/SFML/SFML/wiki/FAQ#tr-grl-minimal
Reduce your code to a minimal example and you are likely to spot the bug. You are also likely to have a better piece of code to post and ask questions about that people will actually bother to read.

Edit: sorry, I meant this for a different thread. Still can't hurt though  ;)
Anyway, you can check if the texture loaded OK.
Title: Re: Texture loaded , drawn , but not shown on the screen
Post by: traxys on May 25, 2014, 10:21:32 pm
Is there anything for checking that the texture is loaded ? Not going throught a sprite , just cheking if there is a texture like with checking a size that is the size of the file you defined it with (in pixel) or if a pixel RGB is equal to the one on the original picture , because my code is nearrly the minimal , + texture&sprite defined + clean/draw/display
Title: Re: Texture loaded , drawn , but not shown on the screen
Post by: Jesper Juhl on May 25, 2014, 10:24:10 pm
sf::Texture's loadFromFile() member function returns a bool. You could start by checking that.
Title: Re: Texture loaded , drawn , but not shown on the screen
Post by: traxys on May 25, 2014, 10:32:31 pm
The loading seems to be fine because this :

    if(!lavaFlow.loadFromFile("lavaFlow.png")){
        cout<<"Error"<<endl;
    }

doesn't print anything , but is the loading of a picture wich is 260*260 long take some time , or is it like 0.1 sec ?

EDIT : Nevermind ,even if the loading time was a bit long , It would not be over 23 minutes .....
Title: Re: Texture loaded , drawn , but not shown on the screen
Post by: traxys on May 26, 2014, 12:29:09 am
Ok know I am trying to figure out if the the texture is correctly put on a Sprite , but I can't figure out how to checks it , anyone has an idea how could I do it ?
Title: AW: Texture loaded , drawn , but not shown on the screen
Post by: eXpl0it3r on May 26, 2014, 08:10:42 am
If you draw a simple shape and it doesn't show up something is wrong to begin with.

Is your graphics driver uptodate?
What's your GPU?
Title: Re: Texture loaded , drawn , but not shown on the screen
Post by: traxys on May 26, 2014, 07:15:35 pm
My drivers could be wrong because I had problemens with them before(With a GPU that was not seen by my OS), but I have built SFML from the sources with MinGW , could It generate some problems ? And I am on a Laptop with the Graphical part of a CPU and a Radeon HD 7600M with up to date drivers. I am going to try deactivating one or the other.
Title: Re: Texture loaded , drawn , but not shown on the screen
Post by: traxys on May 26, 2014, 07:29:43 pm
Ok so If I have just my GPU activated I get a SIGSEGV in the SFML code Oo :

#0 ??   ?? () (??:??)
#1 68ED4511   sf::RenderTarget::applyBlendMode(this=0x28fcc0, mode=...) (C:\Users\qbsec_000\Desktop\SFML\src\SFML\Graphics\RenderTarget.cpp:431)
#2 68ED413C   sf::RenderTarget::resetGLStates(this=0x28fcc0) (C:\Users\qbsec_000\Desktop\SFML\src\SFML\Graphics\RenderTarget.cpp:362)
#3 68ED39C9   sf::RenderTarget::draw(this=0x28fcc0, vertices=0x5047f8, vertexCount=32, type=sf::TrianglesFan, states=...) (C:\Users\qbsec_000\Desktop\SFML\src\SFML\Graphics\RenderTarget.cpp:211)
#4 68EDD30B   sf::VertexArray::draw(this=0x28fc48, target=..., states=...) (C:\Users\qbsec_000\Desktop\SFML\src\SFML\Graphics\VertexArray.cpp:147)
#5 68ED3963   sf::RenderTarget::draw(this=0x28fcc0, drawable=..., states=...) (C:\Users\qbsec_000\Desktop\SFML\src\SFML\Graphics\RenderTarget.cpp:185)
#6 68EDA437   sf::Shape::draw(this=0x28fb7c, target=..., states=...) (C:\Users\qbsec_000\Desktop\SFML\src\SFML\Graphics\Shape.cpp:219)
#7 68ED3963   sf::RenderTarget::draw(this=0x28fcc0, drawable=..., states=...) (C:\Users\qbsec_000\Desktop\SFML\src\SFML\Graphics\RenderTarget.cpp:185)
#8 00401922   main() (C:\Users\qbsec_000\Traxys\Documents\Cpp\Programms\SFMLTest\main.cpp:24)

And AMD Catalyst says now that My drivers are bugged.....

EDIT : When I deactivated my AMD Car my laptop did a BSOD
Title: Re: Texture loaded , drawn , but not shown on the screen
Post by: traxys on May 28, 2014, 08:36:48 pm
Just needed a re-build for it
Title: Re: Texture loaded , drawn , but not shown on the screen
Post by: lyvinhloi.cntt on May 29, 2014, 10:43:58 pm
Hi there, I have almost the same problem with @traxys, so I don't create new topic, hope anybody help me to solve it :)
This is my code:
#include <SFML/Graphics.hpp>
#include <sstream>
#include <iostream>

#define WND_WIDTH 400 // Window Width
#define WND_HEIGHT 400 // Window Height
#define S_WIDTH 100  // Square Width
#define S_HEIGHT 100 // Square Height
#define SPEED 10

int main(void)
{
    sf::RenderTexture rtexture;
    rtexture.create(S_WIDTH,S_HEIGHT);
    rtexture.setSmooth(true);

    sf::Font font;
    if(!font.loadFromFile("arial.ttf")) std::cout << "Error ! Can't load font file [arial.ttf]";

    sf::Text text;
    text.setFont(font);
    text.setColor(sf::Color::Red);
    text.setString("1234");
    text.setCharacterSize(30);
    text.setOrigin((text.getLocalBounds().width+text.getLocalBounds().left)/2,(text.getLocalBounds().height+text.getLocalBounds().top)/2);
    text.setPosition(S_WIDTH/2.0,S_HEIGHT/2.0);

    sf::RectangleShape square;
    square.setOrigin(S_WIDTH/2,S_HEIGHT/2);
    square.setOutlineThickness(2);
    square.setPosition(S_WIDTH/2,S_HEIGHT/2);
    square.setSize(sf::Vector2f(S_WIDTH,S_HEIGHT));
    square.setFillColor(sf::Color::Black);

    sf::RenderWindow window(sf::VideoMode(WND_WIDTH,WND_HEIGHT),"Testing");
    window.setFramerateLimit(60);
// this is the problem
    sf::RectangleShape rect;
    rect.setTexture(&rtexture.getTexture(),true);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed) window.close();
        }

        window.clear(sf::Color::Black);
        //window.draw(rect);
        window.draw(square);
        window.draw(text);
        window.display();
    }
}
 
Humm... I've created my texture had square and text in the middle of it by using rendertexture. But I can't getTexture() to use even though I can draw on main window ( like the code above ). When comment
window.draw(square);
window.draw(text);
 
and uncomment window.draw(rect), nothing on the screen, why ?
Title: Re: Texture loaded , drawn , but not shown on the screen
Post by: Ixrec on May 29, 2014, 10:47:42 pm
You don't appear to have drawn anything to the rendertexture.
Title: Re: Texture loaded , drawn , but not shown on the screen
Post by: lyvinhloi.cntt on May 29, 2014, 10:56:30 pm
^: Okay, I missed that, but it still black screen  :-\
Title: Re: Texture loaded , drawn , but not shown on the screen
Post by: Ixrec on May 29, 2014, 10:59:19 pm
Can you show a complete example where you actually draw something to your rendertexture? (preferably without the text and other drawables since they aren't relevant)
Title: Re: Texture loaded , drawn , but not shown on the screen
Post by: lyvinhloi.cntt on May 29, 2014, 11:08:31 pm
I don't know  ;D
I just want to add my text, my square to rendertexture to draw once. ( example if there weren't error, I could draw 1 rect that had both text in the middle and border around :) )

In fact, it is black screen, I don't know how to solve ... I think it would be the same, I captured my screen, it would be shown like the top but it actually shown like the bottom :(

EDIT: Sr about my english >.<
Title: Re: Texture loaded , drawn , but not shown on the screen
Post by: Ixrec on May 29, 2014, 11:34:42 pm
Screenshots without the code that produced them doesn't tell us anything.

It sounds like you want to draw the square and the text to the rendertexture once at the beginning, then draw the rendertexture's texture to the window every frame.  Try actually doing that.
Title: Re: Texture loaded , drawn , but not shown on the screen
Post by: lyvinhloi.cntt on May 30, 2014, 07:03:18 pm
Tks, after clearing code and test each line, I saw that the text had been draw, but square.setFillColor(...) -> so, it will draw over it and swap those lines, it work perfectly ^^ ... thanks for reply :)