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

Author Topic: how to set background.  (Read 15390 times)

0 Members and 1 Guest are viewing this topic.

Nabeel Rehman

  • Newbie
  • *
  • Posts: 13
    • View Profile
how to set background.
« on: April 19, 2017, 08:34:19 am »
Hi guys, i'm having trouble in setting up the background for my tank game, the image 'background.png' just don't show up!!!  ???
------------------------------------------------------------
// Image size is 1280x1024

        RenderWindow window(VideoMode(1024, 786, 32), "Test 32");

        Texture texture;
        texture.loadFromFile("background.png");
        Sprite sprite;
        Vector2u size = texture.getSize();
        sprite.setTexture(texture);
        sprite.setOrigin(size.x / 2, size.y / 2);

        while (window.isOpen())
        {
                Event e;
                while (window.pollEvent(e))
                {
                        if (e.type == Event::Closed)
                                window.close();
                }
                window.draw(sprite);
                window.display();
        }

sjaustirni

  • Jr. Member
  • **
  • Posts: 95
    • View Profile
Re: how to set background.
« Reply #1 on: April 19, 2017, 02:26:59 pm »
"image doesn't show up" is lacking information needed to diagnose the mistake you've made. Read this page thoroughly, I believe it contains the solution to your problem (hint: it has something to do with working directories). If following the solution in the tutorial doesn't help, come back to the forum and explain what have you tried and what error messages you've got.

Good luck! ;)

Hapax

  • Hero Member
  • *****
  • Posts: 3043
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: how to set background.
« Reply #2 on: April 19, 2017, 08:57:11 pm »
What is the returned value from texture.loadFromFile("background.png")?
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

chablahblah

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: how to set background.
« Reply #3 on: April 23, 2017, 07:12:44 pm »
It may be better to put an if statement around the load from file.

if(!texture.loadFromFile("background.png")) { // error: handle didn't load }

xYodax

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: how to set background.
« Reply #4 on: July 13, 2021, 07:20:50 pm »
The problem here is that while using the .setTexture() function and an sf::Texture the .setTexture() function expects to be given a pointer so:

RenderWindow window(VideoMode(1024, 786, 32), "Test 32");

        Texture texture;
        texture.loadFromFile("background.png");
        Sprite sprite;
        Vector2u size = texture.getSize();
        sprite.setTexture(&texture);  //This is where you add an & to designate texture as a pointer
        sprite.setOrigin(size.x / 2, size.y / 2);

        while (window.isOpen())
        {
                Event e;
                while (window.pollEvent(e))
                {
                        if (e.type == Event::Closed)
                                window.close();
                }
                window.draw(sprite);
                window.display();
        }

kojack

  • Jr. Member
  • **
  • Posts: 81
    • View Profile
Re: how to set background.
« Reply #5 on: July 14, 2021, 03:52:33 am »
setTexture() doesn't take a pointer. It's prototype is:
void setTexture(const Texture& texture, bool resetRect = false);
The texture parameter is a reference. Doing sprite.setTexture(&texture); would be a compile error.


Stauricus

  • Sr. Member
  • ****
  • Posts: 301
    • View Profile
    • Email
Re: how to set background.
« Reply #6 on: July 14, 2021, 01:35:35 pm »
The problem here is that while using the .setTexture() function and an sf::Texture the .setTexture() function expects to be given a pointer so:

after 4 years i think the poster either already solved it or gave up  ::)

 

anything