Hey guys, im trying to return a sf::Image to the RenderWindow.draw function,
I have some code so in a class a sf::Image is created, and i use a function to return it like so
sf::Image gGraphic::draw()
{
return image;
}
but when i put it into code like this
screen.draw(button.draw());
the . between screen and draw is red, and displays this error
Error: no instance of overloaded function "sf::RenderWindow::draw"" matches the argument list
now I have looked at the tutorials for 1.6 (im using 2.0 but there is no image tutorial for that) and to draw a sprite you just need to return the image..so how come mind isnt working?
Canvas
I have changed my code and this is what i have now
in gButton.cpp
gButton::gButton(int x,int y,int w,int h,int ident)
{
state = 0;
xPos = x;
yPos = y;
width = w;
height = h;
id = ident;
graphic.loadFromFile("images/newgamebtn.png");
img.setTexture(graphic);
}
sf::Sprite gButton::draw()
{
return img;
}
and in my gState i have
screen.draw(button.draw());
when i compile it now, it runs but before it loads up the window i get this error
Unhandled exception at 0x5d972382 in Phor_Temp1_SFML.exe: 0xC0000094: Integer division by zero.
then it closes, any idea? i just want to be able to return a sprite from another class
I just placed this code
sf::Sprite temp = button.draw();
screen.clear(sf::Color(currentState,currentState,currentState));
screen.draw(temp);
and i get a white box the same size of the image in the top left corner, but no image..., Do i have to pass back the image + sprite, then make them in my main loop? There must be a better way as im going to be loading alot of sprites into my game
more code has been changed, here is my gButton.h
//Player header
#ifndef gBUTTON_H
#define gBUTTON_H
//includes
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
//Using namespace
//body
class gButton{
private:
int id;
int state;
float xPos, yPos;
int width, height;
public:
//Variables
sf::Texture graphic;
sf::Sprite img;
gButton();
gButton(int x,int y,int w,int h, int ident);
sf::Sprite draw();
void handleEvents(sf::Event iEvent);
int rtnState(){return state;}
int rtnID(){return id;}
};
//end body
#endif
and my gButton.cpp
#include "gButton.h"
gButton::gButton()
{
//nothing
}
gButton::gButton(int x,int y,int w,int h,int ident)
{
state = 0;
xPos = x;
yPos = y;
width = w;
height = h;
id = ident;
graphic.loadFromFile("images/newgamebtn.png");
img.setTexture(graphic);
}
sf::Sprite gButton::draw()
{
return img;
}
now here is my gState update method
void gState::update()
{
while (rtnQuit() == false)
{
currentState += 1;
//Event checker
while(screen.pollEvent(iEvent))
{
if(iEvent.type == sf::Event::Closed)
{
quit = true;
}
//Events
//stateEvents();
}
//State checker
if(currentState == 1)
{
//if(buttons.at(0).rtnState() == 1 && buttons.at(0).rtnID() == 1)
//{
// currentState = 2;
// changeState();
//}
}
//Collision
//stateCollision();
//Update
//stateUpdate();
if(fps.getElapsedTime().asMilliseconds() > frameRate)
{
fps.restart();
}
screen.clear(sf::Color(currentState,currentState,currentState));
screen.draw(button.img);
screen.display();
//DRAW
//stateDraw();
}
}
when i compile i get a white box and still not image...
Now i have read around, and apparently my code will set the texture up to the sprite, but then it will delete the texture because it comes out of scope, but in every forums i find no one every explains how to actually fix that, anyone have a idea?
Canvas
...
Just so you know I have just followed this tutorial
https://github.com/SFML/SFML/wiki/TutorialImageManager and changed all the sf::Image's to sf::Texture's and it still comes up as a white box