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

Author Topic: calss not drawing a sprite properly  (Read 1147 times)

0 Members and 1 Guest are viewing this topic.

Casey

  • Newbie
  • *
  • Posts: 1
    • View Profile
calss not drawing a sprite properly
« on: December 21, 2010, 04:53:31 am »
Hi all,
I'm trying to write a class that manages a cursor but I'm getting some weird behaviour and I need some help to see if someone else can figure it out before it drives me insane.
class is defined as follows

Code: [Select]
class cursor
{
public:
int getCursorX();
int getCursorY();
bool isPressed();
cursor();
void update();
private:

sf::Sprite sprite;

};

cursor::cursor()
{

sf::Image cursor1;
cursor1.LoadFromFile("cursor.png");
sf::Sprite sprite(cursor1);
sprite.SetCenter(10.f,10.f);
myWindow.Draw(sprite);
myWindow.Display();
sf::Sleep(3);




}

int cursor::getCursorX()
{
return myWindow.GetInput().GetMouseX();
}

int cursor::getCursorY()
{
return myWindow.GetInput().GetMouseY();
}

bool cursor::isPressed()
{
return myWindow.GetInput().IsMouseButtonDown(sf::Mouse::Left);
}
void cursor::update()
{
int x=getCursorX();
int y=getCursorY();
sprite.SetPosition((float) (x-10),(float) (y-10));
myWindow.Draw(sprite);


}


In the constructor, I placed a line to draw and display the loaded sprite so that I could make sure that it loaded properly.
when I create a new cursor with the command

Code: [Select]
cursor *-cursorName- = new cursor();

the image is displayed in the top left corner of the window for 3 seconds as would be expected but then when I call

Code: [Select]
-cursorName->update();

it only draws a single white pixel at the mouse location, offest by 10 pixels, rather than my loaded cursor sprite. Ive even loaded a completely black image and the pixel stilll comes up as white.

DevBug

  • Newbie
  • *
  • Posts: 12
    • View Profile
calss not drawing a sprite properly
« Reply #1 on: December 21, 2010, 05:55:06 am »
Are you sure the image is being loaded?

Try this.

Code: [Select]

class Cursor {
public:
Cursor() {
m_Sprite = sf::Sprite(m_Image);
}

Cursor(std::string& path) {
if(!m_Image.LoadFromFile(path)) {
// Exit or do whatever
}

m_Sprite = sf::Sprite(m_Image);
}

virtual ~Cursor() { }

virtual void Draw(sf::Window& Window) {
Window.Draw(m_Sprite);
}

virtual void Update(sf::Window& Window) {
m_Position.x = Window.GetInput().GetMouseX();
m_Position.y = Window.GetInput().GetMouseY();
m_Pressed = Window.GetInput().IsMouseButtonDown(sf::Mouse::Left);

m_Sprite.SetPosition(m_Position);
}

virtual sf::Vector2f GetCursorPosition() { return m_Position; }
virtual float GetCursorX() { m_Position.x }
virtual float GetCursorY() { m_Position.y }
protected:
sf::Image m_Image;
sf::Sprite m_Sprite;
sf::Vector2f m_Position;
bool m_Pressed;
};

class MyCursor : public Cursor {
public:
MyCursor() {
Cursor::Cursor("cursor.png");
}
};


Edit:

Call it like this:

Code: [Select]

int main(int argc, char** argv) {
// Create video mode, window, etc.
MyCursor myCursor = MyCursor();

// Inside the loop
myCursor.Update(myWindow);
myCursor.Draw(myWindow);
}

 

anything