Yeah I'm aware that the usual call is "window.draw(sprite)". But because my sprite and image are loaded via my class file, I can't call them using my main function because the window is defined in my main, not my class.
My class:
class Invador
{
public :
static bool Init(const std::string& ImageFile)
{
return alien.LoadFromFile("sprite.png");
}
Invador()
{
sprite.SetImage(alien);
sprite.SetPosition(100,100);
}
private :
static sf::Image alien;
sf::Sprite sprite;
};
hi
this question bothered for a long time, too. it's a C++ question, not SFML.
for this, you should use the reference and dereference operators.
you need a function inside your class that gets the variable from main(). like this:
int classFunction (sf::Renderwindow* window){
window->draw(sprite); //here you use -> instead of a dot.
return 0;
}
and in the main(), when you call the classFunction:
invadorObject.classFunction(&window);
hope that it helps :)
int classFunction (sf::Renderwindow* window){
window->draw(sprite); //here you use -> instead of a dot.
return 0;
}
and in the main(), when you call the classFunction:
invadorObject.classFunction(&window);
hope that it helps :)
Thanks for the replies, I'm just testing both and seeing which would help me in the long run.
But in this example I don't really know what you meant by
invadorObject.classFunction(&window);
I just get the error "a nonstatic member reference must be relative to a specific object" when I use the call
Invador.draw(&window);
If you want to define a draw function, then It's suggested to derive from sf::Drawable, so you can call
window.draw(object);
rahter than
object.draw(window);
But both work fine. ;)
Other than Stauricus suggested I'd use a reference to the window rather than a pointer, e.g.
void MyClass::draw(const sf::RenderWindow& window)
{
window.draw(m_sprite);
}
Ok so going from the draw function, I get an error about the pointer
Error:
error C2662: 'sf::RenderTarget::Draw' : cannot convert 'this' pointer from 'const sf::RenderWindow' to 'sf::RenderTarget &'
void Invador::draw (const sf::RenderWindow& window)
{
window.Draw(sprite);
}
the "window" part of the "window.draw(sprite)" gets the error.
Hi everyone!
I followed those posts, and that helped me a lot, but when I try to draw a instance of my class, derived from sf::Sprite, it appears black on the screen, with no error listed...
I tried many things, being quite a beginner, but nothing seems to work...
My image manager is working, thanks to one of the tutorials on this site, because spriteMenuDebutPetit is correctly displayed and there's no error log in the console.
Could anyone help me?
Here is my class header:
#include <string>
#include <SFML/Graphics.hpp>
#include "ImageManager.h"
class InterfaceElement : public sf::Sprite
{
public:
InterfaceElement (const std::string& imageToLoad, int positionX, int positionY, ImageManager ImageManager);
InterfaceElement::~InterfaceElement ();
void draw (sf::RenderWindow* window);
private:
sf::Sprite m_sprite;
};
my class .cpp:
#include <SFML/Graphics.hpp>
#include <map>
#include <string>
#include <iostream>
#include "InterfaceElement.h"
#include "ImageManager.h"
InterfaceElement::InterfaceElement ( const std::string& imageToLoad, int positionX, int positionY, ImageManager imageManager)
{
sf::Sprite m_sprite;
m_sprite.setTexture (imageManager.getTexture (imageToLoad) ) ;
m_sprite.setPosition (positionX, positionY);
}
InterfaceElement::~InterfaceElement ()
{
}
void InterfaceElement::draw (sf::RenderWindow* window)
{
window->draw (m_sprite);
}
and my main.cpp:
#include <SFML/Graphics.hpp>
#include "ImageManager.h"
#include "InterfaceElement.h"
int main ()
{
sf::RenderWindow Window (sf::VideoMode (1024, 640), "", sf::Style::Titlebar | sf::Style::Close);
ImageManager imageManager;
InterfaceElement background ("ressources/menu/fond_menu_lancement.png", 0, 0, imageManager);
sf::Sprite spriteMenuDebutPetit;
spriteMenuDebutPetit.setTexture (imageManager.getTexture ("ressources/menu/menu_debut_petit.png") );
spriteMenuDebutPetit.setPosition ((1024/2-384/2), (640-384));
InterfaceElement secondMenu ("ressources/menu/pwned-DH.jpg", (1024/2 - 384/2), (640-384), imageManager);
while (Window.isOpen())
{
sf::Event Event;
while (Window.pollEvent (Event) )
{
switch (Event.type)
{
case sf::Event::Closed:
Window.close();
break;
default:
break;
}
}
background.draw (&Window);
Window.draw (spriteMenuDebutPetit);
Window.draw (secondMenu);
Window.display ();
}
return 0;
}
Deleted the "public: sf::Sprite" line in the class header.
I realized that was pointless after your "where do you derive from sf::Sprite" remark.
Oh now I've seen it. ;D
And nicely done: 1000th post! ;)
Thanks! ;)
I deleted the "sf::Sprite m_sprite" line in the constructor, and my sprites don't appear black anymore...
Now they're white... ^^
This means that the texture got lost somewhere on the way from loading to display it. My guess would be that this happens at the point where you hand over your image manager by value instead of a reference on the constructor of InterfaceElement. ;)
What happens if you change the decleration to the following code:
InterfaceElement (const std::string& imageToLoad, int positionX, int positionY, ImageManager& ImageManager);
Or maybe even a const ref
InterfaceElement (const std::string& imageToLoad, int positionX, int positionY, const ImageManager& ImageManager);