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

Author Topic: How to Fix White Square?  (Read 2426 times)

0 Members and 1 Guest are viewing this topic.

Knockback

  • Newbie
  • *
  • Posts: 5
  • The Guild - CEO
    • AOL Instant Messenger - skylearj
    • View Profile
How to Fix White Square?
« on: April 30, 2013, 06:26:08 pm »
I already read the section for this issue on the Tutorials page, and I've read some instances of this issue around the forums; the problem is that none of this helps me much.

Since I'm a newbie at C++, I don't know the proper way to "extend the texture's lifetime as long as they are used by sprites." I was hoping someone would be able to help me fix this.

I'm currently using just a temporary method (for the purpose of testing) to draw a sprite to the window. This is all taking place within Engine:

Engine.h
#ifndef Engine_h
#define Engine_h

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

#include "TextureManager.h"
#include "Tile.h"

class Engine
{
    private:
        sf::RenderWindow* window;
   
        bool Initialize();
        void MainLoop();
        void ProcessInput();
        void RenderFrame();
        void Update();
       
        TextureManager textureManager;
        void LoadImages();
       
        Tile* testTile;
   
   
    public:
        Engine();
        ~Engine();
   
        void Go();
   
        sf::Texture BackgroundTexture;
        sf::Sprite Background;
};

#endif
 

Engine.cpp
#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 if(!BackgroundTexture.loadFromFile("BattleGame.app/Contents/Resources/BG.png"))
            return false;
         else
            Background.setTexture(BackgroundTexture);
            LoadImages();
            return true;
}




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::LoadImages()
{
    sf::Texture sprite;
    sprite.loadFromFile("BattleGame.app/Contents/Resources/ConsoleGUI.png");
   
    textureManager.AddTexture(sprite);
    testTile = new Tile(sprite);
}




void Engine::RenderFrame()
{
    window->clear();
    window->draw(Background);
    testTile->Draw(0, 486, window);
    window->display();
}




void Engine::Go()
{
    if(!Initialize())
        throw "The engine failed to initialize.";
    else
        MainLoop();
}
 

Raphman

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: How to Fix White Square?
« Reply #1 on: April 30, 2013, 07:46:23 pm »
First of all, you should post a complete and minimal piece of code that shows your issue. What're your textureManager and Tile classes doing?

It looks like sprite is going out of scope from in load images. Does textureManager keep it? if so then your tile should reference the texture from texture manager. Also, sf::Sprite is a thing, it gets confusing when you start calling textures sprites. Read the tutorials more :)

Also I suggest you start with basic C++ and learn how to use the language etc before you jump into making a game with SFML

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: How to Fix White Square?
« Reply #2 on: May 01, 2013, 12:46:13 am »
My question is why are you using new/delete? You don't need to use new/delete, and you really shouldn't be using them in the first place.

Also, please show us your complete and minimal code so we can give you proper assistance.
I use the latest build of SFML2

Knockback

  • Newbie
  • *
  • Posts: 5
  • The Guild - CEO
    • AOL Instant Messenger - skylearj
    • View Profile
Re: How to Fix White Square?
« Reply #3 on: May 05, 2013, 11:42:27 pm »
Does textureManager keep it? if so then your tile should reference the texture from texture manager.

After calling myself an idiot after seeing what I did wrong...

I...

I love you. q-q

Kidding, but I did manage to solve it. Thanks for that! And I've already learned the C++ basics, but all-out OOP is still new ground for me.

 

anything