SFML community forums

Help => Graphics => Topic started by: Knockback on April 25, 2013, 03:14:48 pm

Title: [SOLVED] Background Image Not Loading
Post by: Knockback on April 25, 2013, 03:14:48 pm
While the subject title sounds retarded and newbish, it's actually an interesting problem. I am a newbie at C++ coding, and SFML is just another layer of stuff for me to learn.

The general issue is simple enough; I create a Texture, load a .png file to it, then .setTexture it to a sprite object. I did this all in one method. Next, I try to use the aforementioned Sprite in another method, to be drawn to a window. It doesn't display.

Then I'm all like WTF and go crazy trying to fix it, then I end up putting it in the method that draws to the RenderWindow and it works fine. Why be this?

#include <iostream>

#include "Engine.h"

Engine::Engine()
{
   
}

Engine::~Engine()
{
    delete window;
}

bool Engine::Initialize()
{
    window = new sf::RenderWindow(sf::VideoMode(800, 612, 32), "BattleGame - V2");
   
    if(!window)
        return false;
    else
        return true;
   
    // Let's create the background image here, where everything initializes.
    if(!BackgroundTexture.loadFromFile("BattleGame.app/Contents/Resources/BG.png"))
        throw "Failure!";
    else
        Background.setTexture(BackgroundTexture);
}

void Engine::ProcessInput()
{
    sf::Event Event;
   
    while(window->pollEvent(Event))
    {
        switch(Event.type)
        {
            case sf::Event::Closed:
                window->close();
                break;
        }
    }
}

void Engine::MainLoop()
{
    while(window->isOpen())
    {
        ProcessInput();
        Update();
        RenderFrame();
    }
}

void Engine::Update()
{
   
}

void Engine::RenderFrame()
{
    window->clear();
    window->draw(Background); // And now we draw the background; but it doesn't work.
    window->display();
}

void Engine::Go()
{
    if(!Initialize())
        throw "The engine failed to initialize.";
    else
        MainLoop();
}
 
Title: Re: Background Image Not Loading
Post by: Grimshaw on April 25, 2013, 04:08:25 pm
Could you be a little more clear on what fails and what succeeds exactly?

From looking at your question and code, I don't see anything wrong that would cause such issues, it should work as-is, unless there's some error going on, like missing image file or something.

What exactly happens?
Title: Re: Background Image Not Loading
Post by: Laurent on April 25, 2013, 04:16:31 pm
Your texture is loaded after the function returns. The code is never executed.
Title: Re: Background Image Not Loading
Post by: Knockback on April 25, 2013, 04:29:56 pm
Your texture is loaded after the function returns. The code is never executed.

So I have to set the texture before I create the window?

EDIT: Did that and it works fine. I guess initializing the graphics before trying to draw them would be smart.  :-\
Title: Re: Background Image Not Loading
Post by: Perde on April 25, 2013, 04:38:23 pm
So I have to set the texture before I create the window?

EDIT: Did that and it works fine. I guess initializing the graphics before trying to draw them would be smart.  :-\

bool Engine::Initialize()
{
    window = new sf::RenderWindow(sf::VideoMode(800, 612, 32), "BattleGame - V2");
   
    if(!window)
        return false;
   
    // Let's create the background image here, where everything initializes.
    if(!BackgroundTexture.loadFromFile("BattleGame.app/Contents/Resources/BG.png"))
        throw "Failure!";
    else
        Background.setTexture(BackgroundTexture);
   
    return true;
}
Title: Re: [SOLVED] Background Image Not Loading
Post by: Nexus on April 26, 2013, 06:03:59 am
Why don't you use the constructor instead of public Initialize() functions?

Also, the new operator never returns nullptr, so your check and the Initialize() return type are useless. You should not use manual memory management anyway, you can declare window as a sf::RenderWindow variable in the class.
Title: Re: [SOLVED] Background Image Not Loading
Post by: Luponius on April 27, 2013, 02:51:04 am
@Nexus Just speculation but I think he's following this tutorial to the letter http://www.dreamincode.net/forums/topic/230524-c-tile-engine-from-scratch-part-1/

I'm following it too, although doing changes here and there when things just don't make much sense.

I can see some practices are not ideal in it, but for lack of better tutorials it's better than nothing to get started quickly on Tile Engines...
Title: Re: [SOLVED] Background Image Not Loading
Post by: Nexus on April 27, 2013, 08:43:46 am
Ah, this is the site that contained so many mistakes such that I even registered and wrote an answer (http://www.dreamincode.net/forums/topic/230524-c-tile-engine-from-scratch-part-1/page__view__findpost__p__1621619?s=96e5768a50ce53d4a6e860488c860d26) ;)
Apparently, still nobody reads it. Then it seems like I have to repeat it here again and again...

Please don't learn SFML or C++ from internet tutorials (especially not ones from YouTube). Almost all except the official ones are very questionable, and you will get used to a lot of bad habits that are difficult to get rid of.
Title: Re: [SOLVED] Background Image Not Loading
Post by: Luponius on April 27, 2013, 01:58:56 pm
I read your reply, and I did spot about half the stuff you pointed out.  The leaks however didn't occur to me, sadly since I'm working on getting used to a Tile Engine I have no other reference, I'm following this guide against my heart but I'll try to keep a high guard up, possibly share bits and pieces of my code as I go along the development and have some stuff lined out :p
Title: Re: [SOLVED] Background Image Not Loading
Post by: FRex on April 27, 2013, 02:36:47 pm
Quote
Ah, this is the site that contained so many mistakes such that I even registered and wrote an answer ;)
Your two posts and way everyone says this tutorial is good made my day. ;D