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

Author Topic: Returning a image to .draw  (Read 3588 times)

0 Members and 1 Guest are viewing this topic.

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Returning a image to .draw
« on: June 15, 2012, 04:56:53 pm »
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 :(
« Last Edit: June 15, 2012, 11:46:01 pm by Canvas »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Returning a image to .draw
« Reply #1 on: June 16, 2012, 01:26:19 am »
... but in every forums i find no one every explains how to actually fix that, anyone have a idea?

Oh I'd go all-in on this bet that it's explained: Read a book about C++! ::)

If you don't know what a (const) reference or a pointer is or how to use them, then you've skipped quite a big chapter in learning C++.

Now at every point you return a sprite, your sprite gets physically copied in your memory wasting a lot of time and space. Using const references would pass one single sprite around without copying anything.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Returning a image to .draw
« Reply #2 on: June 16, 2012, 02:02:44 am »
Its ok, I am now using the image manager above, but with some changes as I didnt like some of his code :), Well i was using SDL for some time now, and moving to SFML is abit different, I do understand what a pointer and a reference is but im not 100% comfortable using them.

Rosme

  • Full Member
  • ***
  • Posts: 169
  • Proud member of the shoe club
    • View Profile
    • Code-Concept
Re: Returning a image to .draw
« Reply #3 on: June 17, 2012, 07:53:50 pm »
I do understand what a pointer and a reference is but im not 100% comfortable using them.
You should before you start doing game and graphics!
GitHub
Code Concept
Twitter
Rosme on IRC/Discord

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Returning a image to .draw
« Reply #4 on: June 17, 2012, 09:35:29 pm »
        //Variables
        sf::Texture graphic;
        sf::Sprite img;
 

make them private.

sf::Sprite gButton::draw()
{
        return img;
}
 

try with this code:

sf::Sprite &gButton::getSprite()
{
        return img;
}
 


then draw your sprite this way:

screen.draw (button.getSprite());
 
« Last Edit: June 17, 2012, 09:39:14 pm by mateandmetal »
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

 

anything