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

Author Topic: Using setTexture for sprites as function parameters  (Read 3760 times)

0 Members and 1 Guest are viewing this topic.

kitchenoven

  • Newbie
  • *
  • Posts: 1
    • View Profile
Using setTexture for sprites as function parameters
« on: August 17, 2014, 04:05:56 am »
I'm sorta a noob with c++ and sfml, so bear with me if this is a stupid question. I can't seem to use the setTexture function while referring to it's sprite as a parameter. Here is an example:
(click to show/hide)
This code compiles, but the image simply does not appear. If I create a texture and apply it in exactly the same way, except by referring to the sprite directly, it works fine. Is there anything I can do to make this work? Or do I have to work around it.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Using setTexture for sprites as function parameters
« Reply #1 on: August 17, 2014, 04:25:52 am »
You need to know C++ to make at least a mediocre use of SFML.

Your spriteTexture function takes an sf::Sprite as a parameter and make a coy of it. Hence the setTexture functions is called on that copy and your sf::Sprite named sprite doesn't change at all.
You may want to learn to pass arguments by reference or by pointer.

Soon after that, another problem (the infamous white square) will arise. The sf::Texture you use need to be alive when you draw your sprite. In your case your texture is destroyed at the end of spriteTexture because it goes out of scope.
Don't create it locally to the function.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Using setTexture for sprites as function parameters
« Reply #2 on: August 18, 2014, 12:58:20 am »
There is information in the tutorial about keeping textures alive for the duration that they are needed, not just to initialise the sprite.

@G, I feel that I must defend people that start SFML before mastering C++ as I have done pretty much that too. C++ can be more fun to learn when you're not just staring at console text.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Re: Using setTexture for sprites as function parameters
« Reply #3 on: August 18, 2014, 02:01:53 am »
@G, I feel that I must defend people that start SFML before mastering C++ as I have done pretty much that too. C++ can be more fun to learn when you're not just staring at console text.
While it's really best to read a good book about C++ first and play around with "pure" C++, everyone can do whatever they want, BUT if they fail to understand basic C++ things SFML uses/requires, then it's not our job to teach them C++.
But without a good basis in C++ how would you even be able to tell, what a C++ problem looks like or what a SFML problem looks like.
Thus SFML does require a basic if not mediocre understanding of C++, regardless of "fun".
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: AW: Re: Using setTexture for sprites as function parameters
« Reply #4 on: August 18, 2014, 02:08:15 am »
Thus SFML does require a basic if not mediocre understanding of C++
I agree. I wasn't saying that you can start with SFML at exactly the same time. I was saying that you don't need to master C++ before venturing into SFML territory, as has been often insisted upon within these forums.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Using setTexture for sprites as function parameters
« Reply #5 on: August 18, 2014, 03:29:42 am »
There's a slight difference between mastering C++ and knowing how to pass arguments by value or reference. :p
You can't do shit without knowing the basics.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Using setTexture for sprites as function parameters
« Reply #6 on: August 24, 2014, 07:19:39 pm »
I'm sorry, G. I didn't know that when you wrote "know", you meant "know the basics". I read it as "know everything about it" as its common to see that here.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything