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

Author Topic: White Rectangle  (Read 3711 times)

0 Members and 1 Guest are viewing this topic.

andreaszdw

  • Newbie
  • *
  • Posts: 26
    • View Profile
White Rectangle
« on: November 27, 2008, 10:13:13 am »
When I do this the tank is drawn only
as a white rectangle in the top left.

What's wrong?

Here is my CTank.h:

Code: [Select]

#ifndef __CTANK_H__
#define __CTANK_H__

class CTank
{
public:
CTank ();
~CTank ();
int LoadImages ();

public:
sf::Sprite *tanksprite;
};

#endif


Here is my CTank.cpp
Code: [Select]

#include <SFML/Graphics.hpp>

#include "CTank.h"

CTank::CTank ()
{
tanksprite = new sf::Sprite;
}

CTank::~CTank ()
{
}

int CTank::LoadImages ()
{
sf::Image Image;
if (!Image.LoadFromFile("../graphic/tank2.png"))
return EXIT_FAILURE;

tanksprite->SetImage (Image);

return 0;
}


Here is my Main:
Code: [Select]

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include "CTank.h"


int main()
{
CTank *tt = new CTank;

tt->LoadImages ();

// Create the main window
sf::RenderWindow App(sf::VideoMode(800, 600), "Test Tank");

// Start the game loop
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close ();

if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close ();
}

App.Draw (*tt->tanksprite);

// Update the window
App.Display ();
}

return EXIT_SUCCESS;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
White Rectangle
« Reply #1 on: November 27, 2008, 10:17:59 am »
The image assigned to your tank sprite no longer exists when you draw it, because it is local to CTank::LoadImages and is destroyed when it terminates.
Laurent Gomila - SFML developer

andreaszdw

  • Newbie
  • *
  • Posts: 26
    • View Profile
Thank's - It works
« Reply #2 on: November 27, 2008, 10:24:12 am »
It was so simple - thank's a lot!  :oops: