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

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

0 Members and 1 Guest are viewing this topic.

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: SFML Texture only showing up as a black box?
« Reply #15 on: December 09, 2014, 01:13:43 am »
Ok, so here is a compile-able piece of code. The movement is glitchy, but whatever, the image draws on screen just fine. The black box issue comes in whenever I give the sprite flashlight its own class, which is why I had all the random bits of code.
#include "stdafx.h"
#include "SFML\Graphics.hpp"


int _tmain(int argc, _TCHAR* argv[])
{
        sf::RenderWindow window;
        window.create(sf::VideoMode(500, 500, 32), "Test");
        sf::Sprite flashlight;
        sf::Texture texture;
        texture.loadFromFile("images/light.png");
        flashlight.setTexture(texture);

        const sf::Vector2i MAX_FLASHLIGHT_VELOCITY = sf::Vector2i(5, 5);
        sf::Vector2f flashlightVelocity(0, 0);

        while(window.isOpen()){
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
                        flashlightVelocity.x -= 1;
                }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
                        flashlightVelocity.x += 1;
                }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
                        flashlightVelocity.y += 1;
                }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
                        flashlightVelocity.y -= 1;
                }
                if(flashlightVelocity.x > MAX_FLASHLIGHT_VELOCITY.x){
                        flashlightVelocity.x = MAX_FLASHLIGHT_VELOCITY.x;
                }
                if(flashlightVelocity.x < MAX_FLASHLIGHT_VELOCITY.x * -1){
                        flashlightVelocity.x = MAX_FLASHLIGHT_VELOCITY.x * -1;
                }
                if(flashlightVelocity.y > MAX_FLASHLIGHT_VELOCITY.y){
                        flashlightVelocity.y = MAX_FLASHLIGHT_VELOCITY.y;
                }
                if(flashlightVelocity.y < MAX_FLASHLIGHT_VELOCITY.y * -1){
                        flashlightVelocity.y = MAX_FLASHLIGHT_VELOCITY.y * -1;
                }
                flashlight.move(flashlightVelocity);
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                        window.close();

                window.clear();
                window.draw(flashlight);
                window.display();
        }

        return 0;
}

Like I said, that works. This will be long, but here is the full code of the flashlight class just so you guys can see what I am working with:
flashlight.h
#include "stdafx.h"
#include "SFML\Graphics.hpp"
        #pragma once
#include "stdafx.h"
#include "ImageManager.h"

class Flashlight{
public:
        Flashlight();
        ~Flashlight();

        //individual functions for independent collision movement, set both x and y for regular movement
        void AddVelocityX(float increment);
        void AddVelocityY(float increment);
        void AddVelocity(sf::Vector2f increment);
        void SetVelocityX(float velocity);
        void SetVelocityY(float velocity);
        void SetVelocity(sf::Vector2f velocity);
        sf::Vector2f &GetVelocity();

        sf::Sprite &GetLight();
        sf::Sprite &GetShader();
private:
        void CheckVelocities();
        //class objects
        ImageManager imgr;

        float velocityX, velocityY;

        sf::Sprite light;
        sf::Sprite shader;
};
flashlight.cpp
#include "stdafx.h"
#include "Flashlight.h"

const int SCREEN_WIDTH = 500;
const int SCREEN_HEIGHT = 500;

const sf::Vector2i MAX_FLASHLIGHT_VELOCITY = sf::Vector2i(5, 5);

Flashlight::Flashlight(){
        //light.setOrigin(light.getGlobalBounds().width / 2, light.getGlobalBounds().height / 2);
        light.setPosition(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
        light.setTexture(imgr.GetImage("images/light.png"));

        shader.setOrigin(SCREEN_WIDTH, SCREEN_HEIGHT);
        shader.setPosition(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
        light.setTexture(imgr.GetImage("images/shader.png"));

        velocityX = 0;
        velocityY = 0;
}

Flashlight::~Flashlight(){

}

void Flashlight::AddVelocityX(float increment){
        velocityX += increment;
}

void Flashlight::AddVelocityY(float increment){
        velocityY += increment;
}

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

void Flashlight::SetVelocityX(float velocity){
        velocityX = velocity;
}

void Flashlight::SetVelocityY(float velocity){
        velocityY = velocity;
}

void Flashlight::SetVelocity(sf::Vector2f velocity){
        velocityX = velocity.x;
        velocityY = velocity.y;
}

sf::Vector2f &Flashlight::GetVelocity(){
        if(velocityX > MAX_FLASHLIGHT_VELOCITY.x){
                velocityX = MAX_FLASHLIGHT_VELOCITY.x;
        }
        if(velocityX < MAX_FLASHLIGHT_VELOCITY.x * -1){
                velocityX = MAX_FLASHLIGHT_VELOCITY.x * -1;
        }
        if(velocityY > MAX_FLASHLIGHT_VELOCITY.y){
                velocityY = MAX_FLASHLIGHT_VELOCITY.y;
        }
        if(velocityY < MAX_FLASHLIGHT_VELOCITY.y * -1){
                velocityY = MAX_FLASHLIGHT_VELOCITY.y * -1;
        }
        return sf::Vector2f(velocityX, velocityY);
}

void Flashlight::CheckVelocities(){
        if(velocityX >= MAX_FLASHLIGHT_VELOCITY.x){
                velocityX = MAX_FLASHLIGHT_VELOCITY.x;
        }
        if(velocityY >= MAX_FLASHLIGHT_VELOCITY.y){
                velocityY = MAX_FLASHLIGHT_VELOCITY.y;
        }
}

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

sf::Sprite &Flashlight::GetShader(){
        return shader;
}
 

I draw these sprites the exact same way as I did in the compile-able code:
window.clear(sf::Color::Black);
window.draw(flashlight.GetLight());
        window.draw(flashlight.GetShader());
window.display();
These functions can be seen in the flashlight.cpp code. Sorry that's a little long, it's just hard to find a balance between too much code and incomplete code. Please continute to bear with me.

Hapax

  • Hero Member
  • *****
  • Posts: 3383
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: SFML Texture only showing up as a black box?
« Reply #16 on: December 09, 2014, 09:59:32 am »
Have you tried the class without using your ImageManager?
We can only assume that the fault is with ImageManager since we cannot see any of its code (especially since it's supposed to be doing the thing that is failing).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11042
    • View Profile
    • development blog
    • Email
Re: SFML Texture only showing up as a black box?
« Reply #17 on: December 09, 2014, 10:26:17 am »
Is your ImageManager a global? What does the shader thing do? What happens if you disable it?
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: 3383
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: SFML Texture only showing up as a black box?
« Reply #18 on: December 09, 2014, 10:55:38 am »
Is your ImageManager a global?
I initially thought this too but I found it; it's a private member of the Flashlight class.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11042
    • View Profile
    • development blog
    • Email
Re: SFML Texture only showing up as a black box?
« Reply #19 on: December 09, 2014, 11:03:36 am »
Ah I see. Well if a flashlight instance gets copied somewhere, the whole thing would make sense, of course we don't know, since nothing has been provided.

Anyways my last few tries for the "Guess My Code" game are upcoming, if you know what I mean.
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 #20 on: December 09, 2014, 02:34:05 pm »
Wow, ok I found out the soultion while trying to provide a better code sample  ;D So can anybody explain this to me? The solution was that I had to create the sf::Texture on a lower base class than the flashlight class. I'll demonstrate:
class Flashlight{
//...
sf::Sprite flashlight;
//sf::Texture texture; this doesn't work here, it must "go down" a class
};

class BaseClass{
//...
//this class holds both the instance of the Flashlight object and the sf::Texture I was trying to use
Flashlight flashlight;
sf::Texture texture; //I moved the sf::Texture declaration to the class which holds the Flashlight object
//^^why is this necessary
}
 

My question: why does the sf::Texture need to be in the same class as the instance of the Flashlight object? Why can't a texture be in the same class as its sprite is? And I know this code is just pseudo-code, but that's because I'm just looking for a more abstract answer now as my problem seems to be solved.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11042
    • View Profile
    • development blog
    • Email
Re: SFML Texture only showing up as a black box?
« Reply #21 on: December 09, 2014, 02:46:27 pm »
You don't derive from the Flashlight class.
Also if you do wonky stuff - which of course we still have no idea about - things can go wrong. Guess you want to read up on polymorphism 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 #22 on: December 09, 2014, 02:52:32 pm »
So it is possible to have the sf::Texture and sf::Sprite in the same class/object? Is there an example of this you can point to?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11042
    • View Profile
    • development blog
    • Email
Re: SFML Texture only showing up as a black box?
« Reply #23 on: December 09, 2014, 03:02:23 pm »
Yes it's possible and yet I wouldn't recommend it, instead texture should be managed independently from the drawing objects, because they are heavy resources, which shouldn't get copied around.

https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/