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

Author Topic: [SOLVED] Background Image Not Loading  (Read 10152 times)

0 Members and 1 Guest are viewing this topic.

Knockback

  • Newbie
  • *
  • Posts: 5
  • The Guild - CEO
    • AOL Instant Messenger - skylearj
    • View Profile
[SOLVED] Background Image Not Loading
« 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();
}
 
« Last Edit: April 26, 2013, 04:03:34 am by Knockback »

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Re: Background Image Not Loading
« Reply #1 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Background Image Not Loading
« Reply #2 on: April 25, 2013, 04:16:31 pm »
Your texture is loaded after the function returns. The code is never executed.
Laurent Gomila - SFML developer

Knockback

  • Newbie
  • *
  • Posts: 5
  • The Guild - CEO
    • AOL Instant Messenger - skylearj
    • View Profile
Re: Background Image Not Loading
« Reply #3 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.  :-\
« Last Edit: April 25, 2013, 04:33:43 pm by Knockback »

Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: Background Image Not Loading
« Reply #4 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;
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [SOLVED] Background Image Not Loading
« Reply #5 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Luponius

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: [SOLVED] Background Image Not Loading
« Reply #6 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...

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [SOLVED] Background Image Not Loading
« Reply #7 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 ;)
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.
« Last Edit: April 27, 2013, 08:49:06 am by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Luponius

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: [SOLVED] Background Image Not Loading
« Reply #8 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

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: [SOLVED] Background Image Not Loading
« Reply #9 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
Back to C++ gamedev with SFML in May 2023