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

Author Topic: Sprite is blank after loading from texture  (Read 2417 times)

0 Members and 1 Guest are viewing this topic.

mikesinclair66

  • Newbie
  • *
  • Posts: 3
    • View Profile
Sprite is blank after loading from texture
« on: October 18, 2020, 03:16:35 am »
Hello,

I am using SFML to display a sprite on the screen. I first use a texture and call loadFromFile() to load the image. This works well with no errors occurring. It seems that the image path is correct and it loads the image, but it doesn't seem to store the data in the sprite. The sprite is displayed without error, but it shows a blank white image.

Is there a problem with my code in the following snippets? They should be fairly easy to read :).

My code:

Main.cpp
#include <SFML/Graphics.hpp>
#include "Menu.h"

using namespace sf;

int main()
{
    VideoMode vm = VideoMode::getDesktopMode();
    int width = vm.width, height = vm.height;
    RenderWindow window(VideoMode(width, height), "Score Arena v1.0", Style::Fullscreen);
    window.setPosition(Vector2i(0, 0));

    Menu menu(width, height);

    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            switch (event.type) {
                case Event::Closed:
                    window.close();
                    break;

                //when a key is pressed
                case Event::KeyPressed:
                    if (Keyboard::isKeyPressed(Keyboard::Escape))
                        window.close();
                    break;
            }
        }

        window.clear();
       
        menu.draw(window);

        window.display();
    }

    return 0;
}
 

Menu.h
#pragma once
#include <string>
#include <SFML/Graphics.hpp>

using namespace std;
using namespace sf;

class Menu {
        int page = 0;
        string title = "Score Arena";
        string labels[4]{
                "Single Player",
                "Multi Player",
                "Options",
                "Exit"
        };
        Sprite background;

        int width, height;

public:
        Menu(int, int);
        void draw(RenderWindow &window);
};
 

Menu.cpp
#include "Menu.h"
#include <SFML/Graphics.hpp>
#include <iostream>

Menu::Menu(int width, int height) {
        this->width = width;
        this->height = height;

        Texture bg;
        if (!bg.loadFromFile("menuBg.jpg"))
                std::cout << "Error loading image!" << std::endl;
        background.setTexture(bg);
}

void Menu::draw(RenderWindow &window) {
        window.draw(background, RenderStates::Default);
}
 
« Last Edit: October 18, 2020, 09:00:13 am by mikesinclair66 »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Sprite is blank after loading from texture
« Reply #1 on: October 18, 2020, 03:41:53 am »
https://www.sfml-dev.org/tutorials/2.5/graphics-sprite.php#the-white-square-problem
https://www.sfml-dev.org/faq.php#graphics-white-rect

Your texture is local to the Menu constructor, thus it is destroyed at the end of the constructor.
You could make it a member of Menu, or declare the variable in your main and pass it (by ref) to your constructor, or anything that doesn't destroy the texture while you still need it.

mikesinclair66

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Sprite is blank after loading from texture
« Reply #2 on: October 18, 2020, 04:10:12 am »
https://www.sfml-dev.org/tutorials/2.5/graphics-sprite.php#the-white-square-problem
https://www.sfml-dev.org/faq.php#graphics-white-rect

Your texture is local to the Menu constructor, thus it is destroyed at the end of the constructor.
You could make it a member of Menu, or declare the variable in your main and pass it (by ref) to your constructor, or anything that doesn't destroy the texture while you still need it.

Wow, perfect thank you very much! Rather than creating Texture bg in the Menu constructor, I made Texture bg a member in Menu.h. It is working great now!
« Last Edit: October 18, 2020, 08:58:47 am by mikesinclair66 »