SFML community forums
Help => Graphics => Topic started by: Rexona for men on August 28, 2011, 01:18:40 am
-
Im really confused what is wrong in my code :shock:
All is fine but the sprite is full displayed and not only it's subrect.
#include "Racetrack.hpp"
int main()
{
// Create the main rendering window
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
Racetrack::Init ("data/Track2.png");
Racetrack track1;
track1.GetSprite ().SetSubRect (sf::IntRect (100, 100, 200, 200) );
// Start 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();
}
// Clear the screen (fill it with black color)
App.Clear(sf::Color (sf::Uint8 (40), sf::Uint8 (200), sf::Uint8 (30) ) );
App.Draw (track1.GetSprite () );
// Display window contents on screen
App.Display();
}
return EXIT_SUCCESS;
}
-
Umm, we couldnt tell since you dont post the full code. (racetrack.hpp and cpp)
-
Huh, dont think you need the code, im very sorry.
#include <SFML/Graphics.hpp>
#include <string>
class Racetrack
{
public:
Racetrack ();
sf::Sprite GetSprite ();
static void Init (const std::string& filename);
private:
sf::Sprite Tracksprite;
static sf::Image TrackImage;
};
#include "Racetrack.hpp"
Racetrack::Racetrack ()
{
Tracksprite.SetImage (TrackImage);
Tracksprite.SetPosition (100.0f, 100.0f);
}
sf::Sprite Racetrack::GetSprite ()
{
return Tracksprite;
}
sf::Image Racetrack::TrackImage;
void Racetrack::Init (const std::string& filename)
{
TrackImage.LoadFromFile (filename);
}
-
It's simple really, GetSprite returns a copy of the sprite and not the actual sprite. So the changes you made was actually on a copy.
-
Thanks for help, I defined a new memberfunction for setting the subrect :D