SFML community forums

Help => Graphics => Topic started by: andreaszdw on November 27, 2008, 10:13:13 am

Title: White Rectangle
Post by: andreaszdw 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;
}
Title: White Rectangle
Post by: Laurent 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.
Title: Thank's - It works
Post by: andreaszdw on November 27, 2008, 10:24:12 am
It was so simple - thank's a lot!  :oops: