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

Author Topic: Some textures become white square when I'm in release mode.  (Read 2636 times)

0 Members and 2 Guests are viewing this topic.

Sphynxinator

  • Newbie
  • *
  • Posts: 11
    • View Profile
Some textures become white square when I'm in release mode.
« on: March 01, 2018, 11:37:14 pm »
Hi. I have a question again.  ::) Recently, I tried to show tiles on the screen, and there were 2500x3 sprites on the screen. I tried to render them all and it got slow. I tried to render a portion of them according to their position but it didn't work either. It got slow again. I'm always re instantiating the Sprite in a function in draw calls, maybe because of it, but what to do? When I declare the sf::Sprite in class declaration, it gives error when I try to call Window->Draw(sf::Sprite); It's weird.

Whatever, my here is my question: It basically doesn't draw some of my texture on the screen, actually just one texture. You can see texture_interior.png on the code:

#include "TextureManager.hpp"
#include <iostream>

sf::Texture TextureManager::textures[TEXTURENAMES_TEXTURECOUNT];
std::string TextureManager::texturePaths[TEXTURENAMES_TEXTURECOUNT];

TextureManager::TextureManager() {
        texturePaths[TEXTURENAMES_ARROWS] = "Bitmaps/System/arrows.png";
        texturePaths[TEXTURENAMES_BUTTONS] = "Bitmaps/System/buttons.png";
        texturePaths[TEXTURENAMES_FADE] = "Bitmaps/System/fade.png";
        texturePaths[TEXTURENAMES_FONT] = "Bitmaps/System/game_font.png";
        texturePaths[TEXTURENAMES_ICON] = "Bitmaps/System/icon.png";
        texturePaths[TEXTURENAMES_LANGUAGES] = "Bitmaps/System/languages.png";
        texturePaths[TEXTURENAMES_SHINE] = "Bitmaps/System/shine.png";
        texturePaths[TEXTURENAMES_LOGO] = "Bitmaps/System/system_logo.png";
        texturePaths[TEXTURENAMES_WHITEBRUSH] = "Bitmaps/System/white_brush.png";
        texturePaths[TEXTURENAMES_CHARBODY] = "Bitmaps/Character/Base/tileset_character_body.png";
        texturePaths[TEXTURENAMES_CHARDRESS] = "Bitmaps/Character/Armor/tileset_character_dress.png";
        texturePaths[TEXTURENAMES_CHARTOKA] = "Bitmaps/Character/Accessory/tileset_character_toka.png";
        texturePaths[TEXTURENAMES_CHARHAIR] = "Bitmaps/Character/Helmet/tileset_character_hair.png";
        texturePaths[TEXTURENAMES_WEAPONKIZILCIKSOPASI] = "Bitmaps/Character/Weapon/weapon_kizilcik_sopasi.png";
        texturePaths[TEXTURENAMES_TILES_INTERIOR] = "Bitmaps/Environment/tiles_interior.png";
        texturePaths[TEXTURENAMES_TILES_FOREST] = "Bitmaps/Environment/tiles_forest.png";
        texturePaths[TEXTURENAMES_EMPTY] = "Bitmaps/System/empty.png";
}

bool TextureManager::Initialize() {
        if (!SetTextures()) {
                return false;
        } else {
                return true;
        }
}

bool TextureManager::SetTextures() {
        bool textureLoadedCorrectly = true;
        for (int i = 0; i < TEXTURENAMES_TEXTURECOUNT; i++) {
                if (!textures[i].loadFromFile(texturePaths[i])) {
                        textureLoadedCorrectly = false;
                        break;
                }
        }

        return textureLoadedCorrectly;
}

sf::Texture* TextureManager::GetTexture(const int textureName) {
        return &textures[textureName];
}
 

Thank you so much. I don't know what to present more, so please ask more if you didn't understand a thing. (I didn't understand too actually :D)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Some textures become white square when I'm in release mode.
« Reply #1 on: March 02, 2018, 03:18:22 am »
Don't use globally initialized SFML resources such as sf::Texture.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Sphynxinator

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Some textures become white square when I'm in release mode.
« Reply #2 on: March 02, 2018, 09:17:40 am »
So what to do? I don't want to use third-party libraries. I have to make it static because I have to reach the texture like this: TextureManager::GetTexture(TEXTURENAMES_FONT);

And it only occurs when I'm in release mode. If it didn't occur on debug mode, doesn't it mean that it works well? I don't get it.

Edit:
When I try to use the texture somewhere else, it did show. :O It only shows as white square in this code:

#include "InteriorPlan.hpp"
#include "../../AssetManagers/TextureManager/TextureManager.hpp"

const int backPlan[MapPlan::LENGTH] = {
        0, 0, 0, ...(I cropped out these data for simplicity) 0, 0, 0
};

const int midPlan[MapPlan::LENGTH] = {
        0, 0, 0, ...(I cropped out these data for simplicity) 0, 0, 0
};

const int frontPlan[MapPlan::LENGTH] = {
        0, 0, 0, ...(I cropped out these data for simplicity) 0, 0, 0
};

InteriorPlan::InteriorPlan() {

}

void InteriorPlan::Initialize() {
        int i = 0;
        for (i = 0; i < LENGTH; i++) {
                backTiles[i].SetPosition(sf::Vector2<float>((Tile::GetWidth()*(i % LENGTH)), (Tile::GetHeight()*(i / LENGTH))));
                backTiles[i].SetTexture(*TextureManager::GetTexture(TextureManager::TEXTURENAMES_TILES_INTERIOR));
                backTiles[i].SetTextureStartPosition(sf::Vector2f(
                        Tile::GetWidth() * (backPlan[i] % TextureManager::GetTexture(TextureManager::TEXTURENAMES_TILES_INTERIOR)->getSize().x),
                        Tile::GetHeight() * ((backPlan[i] / TextureManager::GetTexture(TextureManager::TEXTURENAMES_TILES_INTERIOR)->getSize().y))));
        }

        for (i = 0; i < LENGTH; i++) {
                midTiles[i].SetPosition(sf::Vector2<float>((Tile::GetWidth()*(i % LENGTH)), (Tile::GetHeight()*(i / LENGTH))));
                midTiles[i].SetTexture(*TextureManager::GetTexture(TextureManager::TEXTURENAMES_TILES_INTERIOR));
                midTiles[i].SetTextureStartPosition(sf::Vector2f(
                        Tile::GetWidth() * (midPlan[i] % TextureManager::GetTexture(TextureManager::TEXTURENAMES_TILES_INTERIOR)->getSize().x),
                        Tile::GetHeight() * ((midPlan[i] / TextureManager::GetTexture(TextureManager::TEXTURENAMES_TILES_INTERIOR)->getSize().y))));
        }

        for (i = 0; i < LENGTH; i++) {
                frontTiles[i].SetPosition(sf::Vector2<float>((Tile::GetWidth()*(i % LENGTH)), (Tile::GetHeight()*(i / LENGTH))));
                frontTiles[i].SetTexture(*TextureManager::GetTexture(TextureManager::TEXTURENAMES_TILES_INTERIOR));
                frontTiles[i].SetTextureStartPosition(sf::Vector2f(
                        Tile::GetWidth() * (frontPlan[i] % TextureManager::GetTexture(TextureManager::TEXTURENAMES_TILES_INTERIOR)->getSize().x),
                        Tile::GetHeight() * ((frontPlan[i] / TextureManager::GetTexture(TextureManager::TEXTURENAMES_TILES_INTERIOR)->getSize().y))));
        }
}

void InteriorPlan::Draw() {
        MapPlan::Draw();
}
 
« Last Edit: March 02, 2018, 09:52:23 am by Sphynxinator »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Some textures become white square when I'm in release mode.
« Reply #3 on: March 06, 2018, 10:38:42 pm »
Don't use globally initialized SFML resources such as sf::Texture.
So what to do?
Create the textures inside the main function like so:
int main()
{
    sf::Texture texture;
}
and pass them around to functions as a parameter (as a reference or pointer).
Or, create them inside a class but the instances of those classes should still be within main function and passed around.

Another option is to put them in an anonymous/unnamed namespace (whichever people are calling it now) lilke so:
namespace
{
    sf::Texture texture;
} // namespace

int main()
{
    texture.loadFromFile("");
}

But I'd still recommend creating objects in the main function as much as possible and then just passing them to the code that needs them.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Some textures become white square when I'm in release mode.
« Reply #4 on: March 07, 2018, 09:09:14 am »
Quote
Another option is to put them in an anonymous/unnamed namespace
This only limits its visibility to the current translation unit, but the variable is still global and constructed/destroyed at global startup/exit, so that doesn't solve anything.
Laurent Gomila - SFML developer

anthnich

  • Newbie
  • *
  • Posts: 34
    • View Profile
    • Long Division LLC
Re: Some textures become white square when I'm in release mode.
« Reply #5 on: March 07, 2018, 04:36:01 pm »
Create TextureManager::textures as a private member (consider using a map instead of an array) of your TextureManager class and use your GetTexture accessor to return from it.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Some textures become white square when I'm in release mode.
« Reply #6 on: March 09, 2018, 11:39:40 pm »
Quote
Another option is to put them in an anonymous/unnamed namespace
This only limits its visibility to the current translation unit, but the variable is still global and constructed/destroyed at global startup/exit, so that doesn't solve anything.
My bad. I honestly thought that it had a more determined construction time.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*