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

Author Topic: SFML Texture only showing up as a black box?  (Read 8795 times)

0 Members and 1 Guest are viewing this topic.

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
SFML Texture only showing up as a black box?
« on: December 08, 2014, 02:31:03 pm »
I've heard of the white box issue, but why is only a black box rendering where my sprite is? My code was working fine until I gave the sprite its own class, then it stopped responding to all setPositions()'s and is only showing a 10*10 black box (the texture is 10*10 as well). Here is the new code I tried to implement
Flashlight::Flashlight(){
        light.setOrigin(light.getGlobalBounds().width / 4, light.getGlobalBounds().height / 2);
        light.setPosition(250, 250);
        light.setTexture(imgr.GetImage("images/light.png"));
}
light light is a class sprite. I set their velocites like this:
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
                flashlight.AddVelocity(sf::Vector2f(-1, 0));
        }

//and

void Flashlight::AddVelocity(sf::Vector2f increment){
        velocityX += increment.x;
        velocityY += increment.y;
}

//return functions
sf::Vector2f &Flashlight::GetVelocity(){
        if(velocityX >= MAX_FLASHLIGHT_VELOCITY.x){
                velocityX = MAX_FLASHLIGHT_VELOCITY.x;
        }
        if(velocityY >= MAX_FLASHLIGHT_VELOCITY.y){
                velocityY = MAX_FLASHLIGHT_VELOCITY.y;
        }
        return sf::Vector2f(velocityX, velocityY);
}

sf::Sprite Flashlight::GetLight(){
        return light;
}

//and move the sprite, which is also not working
void Engine::Update(float interpolation){
        if(CollisionTest() == false){
                flashlight.GetLight().move(flashlight.GetVelocity().x * interpolation * .8, flashlight.GetVelocity().y * interpolation * .8);
                flashlight.GetShader().setPosition(flashlight.GetLight().getPosition());
        }
}
 
Where flashlight is an object of my new class I created.
That's everything related to my new code. I'm not sure why the sprite is now unresponsive, but any help would be appreciated.

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: SFML Texture only showing up as a black box?
« Reply #1 on: December 08, 2014, 04:28:28 pm »
instead of
sf::Sprite Flashlight::GetLight(){
    return light;
}
you should have a function
Flashlight::move(dt)
 

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: SFML Texture only showing up as a black box?
« Reply #2 on: December 08, 2014, 06:02:59 pm »
Ok, but what about the texture not rendering properly?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: SFML Texture only showing up as a black box?
« Reply #3 on: December 08, 2014, 07:06:45 pm »
You should by now know what the rules are. Provide a minimal and complete example. Describe what you're doing - give context. Add your OS, compiler, etc. etc.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: SFML Texture only showing up as a black box?
« Reply #4 on: December 08, 2014, 07:25:48 pm »
vs c++11, windows 8, and the code was honestly as minimal as I was able to make it because I wanted to show that the sprite is in a seperate class from where it is being accessed. The context I thought was fairly given in the OP? If not, I'm trying to draw a sprite stored in a different class, but upon drawing it I cannot set its position and a same-sized black box appears where the sprite's initial position is.

smguyk

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Re: SFML Texture only showing up as a black box?
« Reply #5 on: December 08, 2014, 07:29:53 pm »
I think it's because you return a copy in this function

sf::Sprite Flashlight::GetLight(){
    return light;
}

instead of a reference or pointer to the sprite.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: SFML Texture only showing up as a black box?
« Reply #6 on: December 08, 2014, 07:31:16 pm »
GetLight returns a copy of your sprite.
That's far from complete and minimal, have you read the link given by expl0iter? ???

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: SFML Texture only showing up as a black box?
« Reply #7 on: December 08, 2014, 07:32:49 pm »
It does say minimal and complete. While you have everything in your head and know what gets drawn where and when and how, we've no idea. ;)

For example: What does imgr.GetImage() return? If we couldn't deduce from the functions called, we wouldn't be able to know that light is a sprite. How are light and flashlight connected? How do you draw things? Do you copy that "different class" around? etc. etc.

I think it's because you return a copy in this function

sf::Sprite Flashlight::GetLight(){
    return light;
}

instead of a reference or pointer to the sprite.
That doesn't have to be an issue. The texture reference will get copied as well.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: SFML Texture only showing up as a black box?
« Reply #8 on: December 08, 2014, 08:07:23 pm »
Actually, that was half the issue. Returning a reference gave me control of the sprite's position. So about the code I provided, how would I change it any while still keeping the original functions? It seems to give the complete idea, mostly.
Quote
What does imgr.GetImage() return?
That function returns a sf::texture which is mapped to the given filename.
And I did create just a sprite without the Flashlight class and it rendered fine, so I know it's not an issue with the .png file.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: SFML Texture only showing up as a black box?
« Reply #9 on: December 08, 2014, 08:10:23 pm »
The goal is not to keep all the functions, but it's to keep all the code that generates the issue.
Create new stub classes with just one function or don't use classes at all. Start from scratch with a plain old main function and build up your scenario, etc.
It's not about throwing the code out there and hoping someone spots the issue, but it's about cutting out everything that has nothing to do with the issue, thus isolating the issue itself and making it more obvious where the issue is. THIS is what a programmers main skill should be: Isolating and solving problems. ;)

If it returns a plain sf::Texture, that is not a reference or pointer to said object, then the texture of which you give the reference to the sprite, will be deleted after setTexture() has been executed.
« Last Edit: December 08, 2014, 08:12:46 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: SFML Texture only showing up as a black box?
« Reply #10 on: December 08, 2014, 08:12:48 pm »
Sorry, you're right, it does return a reference. It's just something from a tutorial I found, but it has worked before so I knew that wasn't the issue either.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: SFML Texture only showing up as a black box?
« Reply #11 on: December 08, 2014, 08:14:20 pm »
You knew, we didn't and that's exactly the reason why a complete example is so important. ;)

Anyways maybe someone else understands your setup just from that vague description and these random bits of code. Unfortunately I don't.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: SFML Texture only showing up as a black box?
« Reply #12 on: December 08, 2014, 08:25:35 pm »
Fair enough, but hopefully that's not to be confused with a "case closed." The situation is exactly like the infamous "white square problem" except with a black square. Has anybody even heard of this?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: SFML Texture only showing up as a black box?
« Reply #13 on: December 08, 2014, 08:34:17 pm »
I can go on a bit with wild guesses, but at some point I'll stop bothering. ;)

The "white square" problem only is white, because the default color of the sprite is white. If you change the sprite color to black, it will be black. Or if the alpha channel is fully transparent and you render it with some blend modes, it will let through what ever lies behind it, which might be black.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: SFML Texture only showing up as a black box?
« Reply #14 on: December 08, 2014, 10:23:30 pm »
How is one supposed to tell, why you are seeing a black square, without you even showing the code where your sprite is drawn? I can only say what has been said. Create a minimal example, that compiles, which illustrates your problem. Who knows, you might solve it yourself trying to do so. At least show more code. Maybe someone can find the error without making a program from it.

Nobody can guess what the error might be. Or maybe one could, but it wold most likely lead to nothing

One more thing: I checked the docs and couldn't find a ´setPosition()´ member function for shaders. This makes
flashlight.GetShader().setPosition(...
rather confusing
« Last Edit: December 08, 2014, 10:26:08 pm by Raincode »

 

anything