SFML community forums

Help => Graphics => Topic started by: ichineko on November 18, 2012, 10:36:57 pm

Title: Explanation of interactions between Image, Texture, and Sprite?
Post by: ichineko on November 18, 2012, 10:36:57 pm
a problem with sprites showing up as a white box.  I found an explanation here http://en.sfml-dev.org/forums/index.php?topic=8961.0  which makes complete sense to me.

I believe I'm running into the same issue, although I'm not completely sure why.  It isn't clear to me exactly what the relationship in SFML is between Images, Textures, and Sprites, nor do I understand how their references are kept, and when they're released.

In particular, here's some code for a tetrimino factory function.  Ignore the render window parameter,
it's there strictly for testing purposes so I could try and get an idea of how and where the sprites were losing their texture references:

Tetrimino buildTetrimino(sf::Vector2f vecs[], std::string block_name, sf::RenderWindow *w) {
    sf::Texture bt;
    if(!bt.loadFromFile(block_name)) {
      std::cout << "failed to load " << block_name << std::endl;
      exit(1);
    }

    sf::Vector2f sz(bt.getSize());
    sf::Vector2f factors(30/sz.x,30/sz.y);  // blocks are 30x30 pixels

    sf::Sprite blocks[4];

    for(int i = 0; i < 4; ++i) {
      sf::Sprite sp(bt);
      sp.setScale(factors);
      sp.setPosition(vecs[i].x,vecs[i].y);
     
      blocks[i] = sp;
    }
    Tetrimino tet(blocks,sf::Vector2f(0,0),w);
    //tet.draw(*w);
    return tet;
  }
 


This is the function that's calling the factory function:

Tetrimino createI(sf::RenderWindow &w) {
    sf::Vector2f vecs[] = {
      sf::Vector2f(-1.5, 0.5),
      sf::Vector2f(-0.5,-0.5),
      sf::Vector2f( 0.5,-0.5),
      sf::Vector2f( 0.5, 0.5),
    };

    Tetrimino t = buildTetrimino(vecs,"./resources/block6.jpg",&w);
    t.draw(w);
    return t;
 


Note the t.draw.  The one in the factory function displays the block just fine.  The one in the createI() function displays a white block.


Here are the relevant constructors.

Tetrimino::Tetrimino(sf::Sprite inpBlocks[], sf::Vector2f center, sf::RenderWindow *w) {
  for(int i = 0; i < 4; i++) {
    this->blocks[i] = inpBlocks[i];
  }
  this->center = center;
  this->translateToTopLeft();
}

Tetrimino::Tetrimino(const Tetrimino &rhs) {
    for(int i = 0; i < 4; i++) {
      this->blocks[i] = rhs.blocks[i];
    }
}
 


I would have expected the copy constructors for the Sprite's to do the right thing, but apparently they're not?

Also, this code is unfinished, it's true that all of the blocks currently will render into the exact same position.  The vector you see in the code is defined in terms of a block size of 1 and the factory method will need to translate accordingly, but for now, I'm more worried about the white block being displayed :)
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: eXpl0it3r on November 18, 2012, 10:44:46 pm
Tetrimino buildTetrimino(...) {
    sf::Texture bt;
    // ...
    return tet;
  }
 
You create a texture within your function, thus as soon as the function is left the texture will get destroyed = white rectangle.
The texture has to be kept alive as long as it's in use.

Also for further post, please make use of the code=cpp tag to highlight your code examples and provide complete and minimal examples, rather than providing some pieces of your live code.
Additionally this post (http://en.sfml-dev.org/forums/index.php?topic=5559.0) gives some guidance towards nice posts, which is directly connected to fast and precise answers. ;)

PS: There are quite a few bad practices in your code. Maybe you could take a look at some more example code in the forum. ;)
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: ichineko on November 18, 2012, 11:30:59 pm
You know exploiter, I appreciate your response, but these types of replies annoy me.

I used the search function or I wouldn't have been able to link you to the other post.
I gave you enough of an example to see exactly what was needed.  It was enough or you wouldn't have been able to point out the issue.  I made a subjective decision on that, and it was the right decision.  Don't ask for a complete code listing when I gave you enough.  Ask for a complete code listing when I don't.

But outside all of that, you

1.  Tell me my code is using bad practices for SFML, but choose not to link me to anything I can actually learn from, and

2.  Failed to answer the question I specifically asked for.  Which is, what is the interaction between images, textures, and sprites.

Am I expected to keep the images around as well, or do the underlying textures share the images?  How and when are they released?  These are exactly the types of things I was hoping to learn because I've googled around and the documentation for SFML is hard for me to find.

If your critique was of the general quality of the C++ code, I would ask you to be quiet.  It's tetris.

I really want your input, but you didn't really give it.  It certainly wasn't enough to help the next person who finds this thread, or to keep me from running into other problems because I don't have the proper mental model for how these things interact with each other in SFML (and I mostly mean from a memory standpoint).
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: eigenbom on November 19, 2012, 12:08:53 am
Don't get so offended, exploiter had no idea about your experience and what you had or had not done, and actually solved the exact question you asked about.

When he said a 'complete and minimal' example, he really meant for you to go and reproduce the problem in the smallest program you can (e.g., a 20 line program), and then post that, instead of fragments of a complex program, which may have all sorts of things going on.

In addition he offered some extra tips which you are free to take or not. Unfortunately your attitude tells me that you will struggle to learn things because you take offence when advice is given. It would have been far better to reply with something like "Thanks exploiter, that fixed my problem. However I'm still a bit confused about how Textures and Images are related, for example does a texture keep an internal reference to the image it was created from?" The answer to that is no btw, afaik.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: eXpl0it3r on November 19, 2012, 12:21:04 am
Hey I can also make nice listings ;D

I gave you enough of an example to see exactly what was needed.  It was enough or you wouldn't have been able to point out the issue.  I made a subjective decision on that, and it was the right decision.  Don't ask for a complete code listing when I gave you enough.  Ask for a complete code listing when I don't.
I never requested a full listing for your current code, I simply said "Also for further post", which obviously doesn't include this one. I gave you this advice since I saw that you're new to this forum and most of the new comers don't even think about providing complete examples and me having to answer in every other post "please provide a complete and minimal example that reproduces the error" gets quite annoying too. Prevention is better than reaction. ;)
Besides that, I can request whatever I want, like: "I want a chocolate cake from you."
But you're not forced to follow my requests. (It's called freedom of speech and freedom of decisions.) :D


1.  Tell me my code is using bad practices for SFML, but choose not to link me to anything I can actually learn from, and
But then again this is all some mock-up code, so maybe you usually do follow all of these things.

2.  Failed to answer the question I specifically asked for.  Which is, what is the interaction between images, textures, and sprites.
sf::Image is a representation of an image and lives on the RAM.
sf::Texture is a representation of an image and lives on the memory of the GPU.
To get a sf::Texture things get internally loaded as a sf::Image and then copied to the GPU's memory. This operation is slow, thus one shouldn't have to copy stuff from the RAM to the GPU memory too much.
sf::Sprite holds a reference to a sf::Texture. If the reference is dead, i.e. the sf::Texture went out of scope or no texture has been set, then you'll get a rectangle filled with the color defined for the sprite.

To get picture on the screen, these rules need to be followed:

Am I expected to keep the images around as well, or do the underlying textures share the images?
sf::Image and sf::Texture are both representation of images (i.e. pure memory), thus you actually only need sf::Texture to display something on the screen.

How and when are they released?
The SFML objects use all RAII (google it), thus they get released as soon as they leave the scope.

and the documentation for SFML is hard for me to find.
You mean what you're looking for is hard to find in the doc right?
The doc is easy to find, here (http://www.sfml-dev.org/documentation/2.0/).

If your critique was of the general quality of the C++ code, I would ask you to be quiet.  It's tetris.
Don't post stuff nobody should ever see or comment upon. ;)

I really want your input, but you didn't really give it.  It certainly wasn't enough to help the next person who finds this thread, or to keep me from running into other problems because I don't have the proper mental model for how these things interact with each other in SFML (and I mostly mean from a memory standpoint).
Read, learn, experiment. :)

Do I now get paid for my 30+min essay just for you? ;)
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: ichineko on November 19, 2012, 01:37:45 am
Quote
Hey I can also make nice listings

lets not be passive aggressive.  I mean that seriously.  I appreciate blunt honesty, I prefer it actually, but I don't like passive aggressive behavior.  It never does anyone any good, so can we agree to refrain from things like this please?


Quote
I was typing on my phone and didn't want to write an complete essay on my touch keyboard. So I apologize that I didn't add tons of examples.

Ok, that's fair, I shouldn't have assumed.  How anyone can type up anything on those keyboards is beyond me.


Quote
I'm aware that ranks and all don't really matter, but it's just very sad to see people join a new forum, totally unaware of their surroundings, what the rules are, how people talk to each other, etc. and the within in the first few posts just show their hate/annoyance towards members that have been around for quite a bit and help quite a few people. I personally try always to be most respectful to anyone when I join a new community. It preserves the atmosphere and you as newbie gain some respect in return.

It was pushback in the hopes that you would respond with a better answer.  You did, and I want you to know I appreciate it.  Maybe you'll take that negatively, but it isn't meant to be taken so.  In my experience, people sometimes need pushback in order to understand that they weren't as helpful as they thought.


Quote
- Don't pass the window as pointer, pass it as reference. Also instead of passing by value (e.g. std::string), pass them as const reference.
- Don't use arrays, specially if you have to pass it around. If you (for some reason) don't want to use std::vector then at least use std::array (TR1/C++11). The problem with a pure array is, that you never really know it's size and thus you'll have to use many hard coded numbers or other not so nice hacks.

-Don't create a additional sprite object just to initialize it, if you're only purpose is to copy that object to another place later on. E.g. just use setTexture() to set the texture instead of sp(bt).


That last point is exactly the types of things I'm wanting to get out of this thread.  The other points are stylistic issues which I'm not interested in.

The array point in particular is ideology.  Vectors have their place, but I prefer the initializer syntax, and tetriminos have been defined as 4 blocks for well on 25 years now. 



Quote
You mean what you're looking for is hard to find in the doc right?
The doc is easy to find, here.

You know, I was going to complain about that link, I *have* been reading those documents.  But then I realized the sprite page did describe the issue I was running into.  I read it the first time I read over the page, but on subsequent visits I didn't hit the 'more' link and so missed it (and had forgotten the warning about keeping the texture alive, although I did remember that they were kept separate which caused unnecessary fears on my part).

So I apologize for that, I should have been able to find that myself.  But having said that, you should link directly to the page, it's more useful and it means when others use that search button and find this thread, they can be directed straight to what they're looking for too.


Quote
The SFML objects use all RAII (google it), thus they get released as soon as they leave the scope.

That's not quite what I was asking for.  A reference counted "Smart Object" would use RAII as well, but that doesn't mean the memory would get released.  I have to assume something similar is being used underneath or drawing a texture would blow up after the fact.

Those types of details are what I was looking for.



Quote
Don't post stuff nobody should ever see or comment upon.

I get that, but I'm also not interested in it.  fyi, I've been using C++ since before a standard *existed*.  I've seen all kinds of code, and I've worked with all kinds of people.  Personally, I believe in scaling the solution to the problem, this means using an array for something as small and well defined as tetris is not a big deal.  If someone wants to get offended about it, that's their business, but it's a waste of my time.  And I'm not saying you acted that way, I'm just explaining why I take took the approach I did.

I'm here to get answers for things I need to learn, not rehash inane internet arguments about things that have no real bearing on the 'success' of my tetris clone.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: ichineko on November 19, 2012, 01:53:18 am
And actually, I have a few more questions.

Does all of this mean I'm responsible for the lifetime of the texture?  I can't assume that it's going to be cleaned up after there are no more references to it?  I don't mean the representation on the video card, I mean the object itself.

So, even though the constructor for sprite takes a *reference*, it's assumed that reference is to heap memory, or the sprite is not going to last past the current scope?
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: eXpl0it3r on November 19, 2012, 02:01:35 am
Well good luck with your attitude then, I don't like it, but who am I and why would you care? ;D

Does all of this mean I'm responsible for the lifetime of the texture?  I can't assume that it's going to be cleaned up after there are no more references to it?  I don't mean the representation on the video card, I mean the object itself.
What? ???
sf::Texture is not a smart pointer or anything. It's a normal object and one always have to take care of the lifetime of the object one is using...

For example:
// tex doesn't exist
void foo()
{
    sf::Texture tex;
    // tex exists
} // tex gets released/deleted
// tex doesn't exist


So, even though the constructor for sprite takes a *reference*, it's assumed that reference is to heap memory, or the sprite is not going to last past the current scope?
No it can be on the stack, but it (the texture) has to exist when you call window.draw(sprite).

For example this would work
void foo(sf::RenderWindow& rw)
{
    sf::Texture tex;
    tex.loadFromFile("img.png");
    sf::Sprite spr(tex, true);
    rw.clear();
    rw.draw(spr);
    rw.display();
} // tex & spr get released/deleted
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: masskiller on November 19, 2012, 02:39:16 am
I wasn't going to butt into this thread (since it was already answered anyway), but ichineko's attitude pretty much blew it all up.

As a courtesy rule if you are asking for help in something you don't know you do it politely not demanding it. eXpl0it3r might be too much of a nice guy, but most people wouldn't even think of helping you if you come all high and mighty when you're trying to ask about something you are ignorant about. Just like we all started.

I won't make this any longer since I don't want to cause a flame war in a help thread, but if you want help at least abide to what I wrote above. Nobody here is forced to help you just because you think you are special or whatever it may be.

I'll leave it here, even if you answer I won't so just take what I wrote as a self note for possible future threads. That way people will want to help you instead of arguing with you.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: cire on November 19, 2012, 06:40:09 am
fyi, I've been using C++ since before a standard *existed*.

So, even though the constructor for sprite takes a *reference*, it's assumed that reference is to heap memory, or the sprite is not going to last past the current scope?

The question after the claim just boggles my mind.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: ichineko on November 19, 2012, 08:28:31 am
Quote
Well good luck with your attitude then, I don't like it, but who am I and why would you care?

That's the internet equivalent of asking me if I know who you are, and I should think you'd be embarrassed, but I'm guessing you're not.

I may or may not continue with SFML, but I'll be diving into the source from here on out.  I don't deal well with fragile egos, and doubly so with passive aggressive behavior.  Lets call it a personality quirk.

FWIW, I don't like the idea that these texture objects destroy the data on the vid card when they go out of scope, I think it's counter intuitive and makes it non-obvious when something is getting removed.;  The API made the construction and loading of these objects awkward due to the expense of the operations, I think they should have made the destruction just as awkward.  Files don't get created when you create a file object, and they aren't deleted when the file object goes out of scope, in case you're looking for precedence for that sort of behavior.

It certainly caused confusion on my part, and it took reading the source before I realized what was going on.  Arguably that's a good thing, but you would think this board would be able to convey that information.  Unfortunately ... egos and all that.


Quote
The question after the claim just boggles my mind.

I was referring to the texture.  It's conceivable for a sprite to lay claim to the actual OpenGL texture handle so the underlying resources don't get thrown away, regardless of the texture object's lifetime.

But they apparently chose to make it mimic the object's lifetime, which means I have to maintain the texture's lifetime as long as the sprite's lifetime, or we get bugs.

I don't like that design.  I understand it to some extent, but it isn't how I would have done it, at least not conceptually.  I'm sure there are valid reasons for them to do it (performance), but it's awkward and it's akin to a dangling pointer.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: Laurent on November 19, 2012, 08:56:22 am
Stop it please, and focus on the problem. If you have something personal to say, say it with PMs.

I'll delete any new message that contains anything other than questions and answers concerning the initial problem.

Thanks for spoiling my forum >:(
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: Laurent on November 19, 2012, 09:04:29 am
Quote
FWIW, I don't like the idea that these texture objects destroy the data on the vid card when they go out of scope, I think it's counter intuitive and makes it non-obvious when something is getting removed
SFML is a simple library, not a complex engine. There's no hidden/automatic ownership, if a sf::Texture instance is destroyed then the corresponding texture data is destroyed. That's the most obvious behaviour that can be implemented.

Your file example is totally different. The hard disk is a permanent storage, once written to the disk the file can be accessed by other applications, the std::ofstream instance doesn't own it, it's just an interface to write to it. But a texture is not supposed to live after your application is finished, and nobody has public access to textures in video memory; so how would it be accessed if no sf::Texture instance refers to it anymore?

Maybe you're coming from languages with garbage collection? Because in C++, it's very unusual to let a resource live after its owner object(s) is (are) destroyed.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: ichineko on November 19, 2012, 02:49:25 pm
It's also unusual to destroy a resource while it's in use by another object.

In my head the texture object is just a handle, hence the initial expectation that it would not completely disappear when the object gets cleaned up, especially since you chose to separate textures from sprites.

I'm not arguing that your approach shouldn't be done, just that it's surprising, to me at least.  You don't delete a file, you close the handle.  You don't delete the DB, you close the connection.  All of these are external resources whose lifetimes are controlled by an external entity.  Much like an OGL texture.  It makes perfect sense to me.

To me, the upshot is that you end up having to manually manage the lifetime of that texture, just like you would a raw pointer.  But your approach is also mostly thread safe, pros and cons, I just found it surprising.

But I do think a blurb in your documentation making this behavior a little more explicit wouldn't hurt.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: Laurent on November 19, 2012, 03:19:50 pm
Quote
It's also unusual to destroy a resource while it's in use by another object.
Yep, so don't do it ;)
The point is that you are responsible of the texture, it's you who knows when the texture is in use or not, and who decides when to destroy it.
In fact, Sprite::setTexture is just a convenience function, I could remove it and you would have to call window.draw(sprite, texture);, which would make it obvious that the texture is used at this point and that is has to be alive.

Quote
In my head the texture object is just a handle, hence the initial expectation that it would not completely disappear when the object gets cleaned up, especially since you chose to separate textures from sprites.
Even if it was a handle, if the only thing that refers to it (the sf::Texture instance) is destroyed, it must be destroyed too. Otherwise it would be lost in the nature and could be considered a leak.
And... it's not a handle, otherwise it would be named sf::TextureHandle. sf::Texture really is what it means: a texture. If you destroy it, you destroy the texture. I think that things are overcomplicated in your head; or at least biased by your personal background ;)

Quote
You don't delete a file, you close the handle.  You don't delete the DB, you close the connection.  All of these are external resources whose lifetimes are controlled by an external entity.  Much like an OGL texture.  It makes perfect sense to me.
In my opinion these comparisons are irrelevant. Files and databases are persistent external objects that applications use; textures are resources that you create, manage and destroy during the lifetime of the application. Nobody else will use them from another program, and nothing must remain when your program ends.

If you have a std::vector, you expect it to release its memory when it is destroyed, because the vector is the array, not a handle to it. Now what makes sf::Texture, an array of pixels, different? Is it special because it interacts with another class? Who is supposed to control its lifetime, if not you?

Of couse I could use shared ownership between user and sf::Sprite, but that would involve to manipulate sf::Texture instances through pointers and to allocate them on the heap. That would be a pretty bad design for C++; it would be ok for garbage collected languages.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: cire on November 19, 2012, 05:39:44 pm
It's fairly trivial to roll up your own sprite/texture type that behave the way you think is more natural from the existing system.  On the other hand, it's nearly impossible to do the reverse from a system where the behavior you prefer is enforced. 

The existing approach is quite flexible, which is something I value in a library.  It doesn't hurt that it doesn't impose overhead for something that I really don't need (and that certainly follows in the grand tradition of C++.)
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: FRex on November 19, 2012, 06:47:18 pm
If you spent half the time you spent disliking the design you could already have a sprite that uses reference counting:
1. Copy and paste sprite.hpp and sprite.cpp
2. Change the name, include guards and add notice under copyright saying that it's not original sfml file but your modified one.
3. Change all sf::Texture * occurences to your* shared pointer of choice(like a dozen or two of brain-dead simple changes).

*As you can see there is slight problem with shared pointers: they are not guaranteed to be in pre 0x standard abiding compiler, boost is too big dependancy, not everyone needs them.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: Laurent on November 19, 2012, 08:30:54 pm
Quote
If you spent half the time you spent disliking the design you could already have a sprite that uses reference counting
Design discussions are important, it helps to improve the library (even when I disagree). Implementing your own stuff is always possible but it doesn't help the community ;)
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: ichineko on November 20, 2012, 12:29:35 am
Quote
I think that things are overcomplicated in your head; or at least biased by your personal background

Can we please stop with the implications that I'm not a C++ developer?  I've been using C++ for something like 18+ years now.  It doesn't really add to the conversation, all it does is make it easy to dismiss anything I say, and that isn't fair to me.


Quote
Of couse I could use shared ownership between user and sf::Sprite, but that would involve to manipulate sf::Texture instances through pointers and to allocate them on the heap. That would be a pretty bad design for C++; it would be ok for garbage collected languages.

That's disingenuous.  At the end of the day, that texture is sitting on the vid card, and what you have is an OGL handle referencing it.  You could just as easily have passed that handle around via copy constructors and treated the Texture class as a glorified strong reference that indicated to the OGL driver it should be destroyed when there were no more references.

Thinking about it, is that why you keep implying my background is GC'd languages?  I don't believe that usage pattern would be foreign to any C++ developer with even a little bit of experience, or I should say I hope not.  These types of mechanisms are why I'm able to tell people that if they're manually managing memory in modern day C++, they're either working really close to the hardware, or they're not using C++ optimally.

But I find myself managing the textures much like I would a raw pointer.  In C++.

And I want to be clear, I'm not saying you should have done it this way.  Pros and cons to everything, the approaches I'm referring to have downsides as well.  Slightly larger memory consumption depending on how they're implemented, possible issues in multi-threaded environments, possible performance implications due to the larger size of the objects, and so on.

But your approach has definite downsides too.  expensive copy, manually having to track the lifetime of the texture objects or risk running into a 'dangling pointer'-esque issue, possible multi-threaded issues, and so on.

But I think citing RAII is misguided.

Ultimately it's a question of value semantics vs reference semantics.  I would have expected reference semantics to the underlying texture, but I got value semantics, and it caught me off guard.

But whether you agree with me or not, I see no reason why you couldn't add a small blurb about that onto your code documentation to make it more explicit.  From epxloiter's own mouth in the other thread I linked to, this is a common misconception for people new to SFML.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: ichineko on November 20, 2012, 12:36:26 am
Quote
It's fairly trivial to roll up your own sprite/texture type that behave the way you think is more natural from the existing system.  On the other hand, it's nearly impossible to do the reverse from a system where the behavior you prefer is enforced. 

I think that's a really good point.  I don't necessarily know that I agree with you about it being more difficult going one direction vs the other, but I can see the argument, and I think it's a good one.


Quote
*As you can see there is slight problem with shared pointers: they are not guaranteed to be in pre 0x standard abiding compiler, boost is too big dependancy, not everyone needs them.

Sure, but I like to use the verbiage 'smart object' to refer to objects whose sole purpose is RAII.  Not necessary as full featured as a smart pointer, but that impose some sort of resource management tied to scope.

Quote
If you spent half the time you spent disliking the design you could already have a sprite that uses reference counting:

Sure, I had considered that.  But ultimately I dislike moving so far away from the intended design without really good reasons to do so.  That doesn't mean I have to agree with it, but the design isn't going to hamper what I do significantly, it's just surprising imo.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: Laurent on November 20, 2012, 08:11:05 am
Quote
Can we please stop with the implications that I'm not a C++ developer?  I've been using C++ for something like 18+ years now.  It doesn't really add to the conversation, all it does is make it easy to dismiss anything I say, and that isn't fair to me.
Ok, I'm sorry. I didn't want to imply anything, I just had the feeling that your point of view was somehow biased by your background, which can be an important consideration when talking about design decisions. No offense.

Quote
I see no reason why you couldn't add a small blurb about that onto your code documentation to make it more explicit
Of course! I never said I wouldn't. In fact it's already explicitly mentioned in Sprite::setTexture:
Quote
The texture argument refers to a texture that must exist as long as the sprite uses it. Indeed, the sprite doesn't store its own copy of the texture, but rather keeps a pointer to the one that you passed to this function. If the source texture is destroyed and the sprite tries to use it, the behaviour is undefined

And it is emphasized too in the 2.0 tutorial that is not online yet.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: ichineko on November 23, 2012, 04:18:47 am
Quote
Of course! I never said I wouldn't. In fact it's already explicitly mentioned in Sprite::setTexture:

That's not enough or we wouldn't be seeing even more posts about this exact issue.

It's a mistake to think doxygen is proper documentation.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: FRex on November 23, 2012, 04:28:24 am
1. SFML code is self documenting, you can freely read, use, modify and distribute it(maybe you don't know how, that's not our problem mr. pre-standard), it's on the most permissive license avaible.
2. There are official tutorials up and more are in writting(in case your c++ experience doesn't cover that and you can't read 1 paragraph worth of text on index.php : RC = RELEASE CANDIDATE, ie.: NOT end version with full documentation).
3. (Almost) every class page of documentation contains: explanation of every single function, use example, long description, if this is not good documentation I don't know what is.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: masskiller on November 23, 2012, 06:44:52 am
Justified sarcasm aside all it takes is checking the doc for the functions you plan on using, not just classes in general since they only have an overview and don't necessarily cover everything, but I guess that for an issue such as textures and their lifetime it wouldn't hurt to have it mentioned in the general description of sf::Texture, mainly because it's a very common mistake.

Of course threads about it won't stop with just that, some people just don't read regardless of whether the info is there or not (not necessarily implying you didn't read, so that it won't be misunderstood). I myself have made threads about issues that could have been solved with simple reading just because I didn't check well enough or read too fast.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: Laurent on November 23, 2012, 08:01:44 am
Quote
That's not enough or we wouldn't be seeing even more posts about this exact issue.
It's in Sprite::setTexture and in the "sprite/texture" tutorial. Where else should it be? Certainly not in the general documentation of sf::Texture, since this issue is related to the link between sprites and textures. There's no problem if you use a texture alone.

Mentioning that the texture is destroyed when its sf::Texture instance is destroyed... well... if it's not obvious then I don't know what's going on in your brain. Then I should do the same with all classes, saying that their destructor destroys the underlying stuff? :P

The problem here is not the lack of documentation, it's that you started to write code with certain assumptions. I can't write the doc as "if you think xxx then you're wrong, the behaviour is standard". And by "standard" I mean "the destructor destroys". Anything else (shared ownership) would have to be documented, but not that.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: ichineko on November 23, 2012, 10:02:33 am
masskiller, I want to thank you.  Despite your disagreement with my position, you've chosen not to be insulting.  I'm unsure as to why the other posters cannot follow from your example.  It's one thing to disagree, even heatedly.  It's another to constantly insult the other party.


Quote
It's in Sprite::setTexture and in the "sprite/texture" tutorial.

http://www.sfml-dev.org/tutorials/2.0/
https://github.com/SFML/SFML/wiki/Tutorials

Give me the name of the link on either of those pages that goes to the 'sprite/texture' tutorial.  I mean that seriously, I specifically looked on that page as soon as I realized the sprites, textures, and images interacted in a manner that was unexpected for me.


Quote
Mentioning that the texture is destroyed when its sf::Texture instance is destroyed... well... if it's not obvious then I don't know what's going on in your brain. Then I should do the same with all classes, saying that their destructor destroys the underlying stuff?

yes.  Why is that ridiculous?

Exploiter cited RAII.  RAII is about resource cleanup, not resource destruction, and they are not always the same thing.  I understand what's going on underneath, maybe that's part of the problem too.  To me, the OGL driver owns that texture, and anything you do in userspace will be nothing more than requests using a handle.  Given that, and the fact that your APi allows me to hand a single texture to multiple sprites, and I have to ask why you think I should assume that texture is getting destroyed on the vid card when the sf::Texture object is destroyed.  Why is that a better assumption?

From my perspective, it makes no sense.  BUT, it doesn't have to make sense, I just need to know the behavior.  Proper documentation is one way for you to convey that behavior.

The entire reason I started this thread is because I realized my expectations were off, and rather than flail around, I wanted a concrete description of the interactions between these objects.  Proper documentation could have prevented me from creating this thread in the first place.  Despite what you and some of the other seem to want to imply, I did look through the documentation before posting, and that includes the tutorials.


And look at it this way, you may think I'm an asshole, but I'm giving you solid feedback on what I did when I realized things weren't working correctly, and what would have prevented me from creating this thread.  That you're dismissing it as unduly biased is not helping you fix such a common misunderstanding.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: Laurent on November 23, 2012, 10:24:23 am
Quote
Give me the name of the link on either of those pages that goes to the 'sprite/texture' tutorial.  I mean that seriously, I specifically looked on that page as soon as I realized the sprites, textures, and images interacted in a manner that was unexpected for me.
As I told you in the first post where I mention the tutorial:
Quote
And it is emphasized too in the 2.0 tutorial that is not online yet.
In case you didn't notice, SFML 2.0 has not been released yet and none of the graphics tutorials are online ;)

Quote
Despite what you and some of the other seem to want to imply, I did look through the documentation before posting, and that includes the tutorials
I never implied that you didn't read the documentation, I simply answered to you saying that "this behaviour should be mentioned".

Quote
I understand what's going on underneath, maybe that's part of the problem too
I think we agree on this point, you make too many assumptions. Clear your mind, forget that this is about OpenGL textures. You have an object, and after it is destroyed it can no longer be used. I don't feel like I need to explain that in the doc and/or tutorials.

The only thing to document is the fact that an implicit link exists between a sprite and its texture, and that this link requires to keep the texture alive as long as the sprite uses it. And it is emphasized in both the tutorial and the documentation of Sprite::setTexture.

So if I summarize this whole thread, the actual problem is that the tutorial is not online yet... ;)

Quote
you may think I'm an asshole
I don't, I really appreciate your feedback, documentation and design discussions are really valuable to me. And I didn't mean to insult you (I don't think I did, but sorry if you felt insulted by me).
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: eXpl0it3r on November 23, 2012, 10:37:27 am
I'm unsure as to why the other posters cannot follow from your example.  It's one thing to disagree, even heatedly.  It's another to constantly insult the other party.
You're the one who started to insult people; you also take every comment extremely personal, but then don't stop at nothing and call other people to have a "personality quirk". The way you're acting is very immature and thus I had a very hard time believing that you've actually been working with C++ for a bit.

Exploiter cited RAII.  RAII is about resource cleanup, not resource destruction, and they are not always the same thing.  I understand what's going on underneath, maybe that's part of the problem too.
No RAII is about the lifetime of resources.
Quote from: Wikipedia
Resource Acquisition Is Initialization (RAII) is a programming idiom used in several object-oriented languages like C++, D and Ada. The technique was invented by Bjarne Stroustrup to deal with resource allocation and deallocation in C++. In this language, the only code that can be guaranteed to be executed after an exception is thrown are the destructors of objects residing on the stack. Resource management therefore needs to be tied to the lifespan of suitable objects in order to gain automatic allocation and reclamation. They are acquired during initialization, when there is no chance of them being used before they are available, and released with the destruction of the same objects, which is guaranteed to take place even in case of errors.

To me, the OGL driver owns that texture, and anything you do in userspace will be nothing more than requests using a handle.  Given that, and the fact that your APi allows me to hand a single texture to multiple sprites, and I have to ask why you think I should assume that texture is getting destroyed on the vid card when the sf::Texture object is destroyed.  Why is that a better assumption?
As you say it yourself, this is only the way you think and it's not what one would usually expect.
Tell me, when and how would the texture then get destroyed? Because if the design was intended as you say, the OGL driver would be reference counting on its own, which it isn't. So we would still have to do this after all by hands and thus the OGL driver is not directly the owner.

What I agree with you in this whole discussion, is that it's very confusing that a sprite can reference a texture although it has been destroyed. draw(sprite, texture) might be a better way, but then again the current design is only a way to simplify the usage, so one can manage it... ;)
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: ichineko on November 23, 2012, 11:41:05 am
You know Laurent, that "you may think I'm an asshole" quote was taken out of context, perhaps you should edit that so it doesn't appear I'm meaning something I didn't actually mean.

Quote
So if I summarize this whole thread, the actual problem is that the tutorial is not online yet...

I think that's mostly accurate, they should be online.


Quote
I think we agree on this point, you make too many assumptions.

The difference between you and me is that I'm aware of my assumptions, hence the existence of this thread in which I asked for details about the interactions instead of just 'please fix my code'.


You said the following:

Quote
Mentioning that the texture is destroyed when its sf::Texture instance is destroyed... well... if it's not obvious then I don't know what's going on in your brain. Then I should do the same with all classes, saying that their destructor destroys the underlying stuff?

That's an assumption, albeit, one you think is reasonable.  And one I do not.

You think my experience with C++ should make assumption X reasonable, I think my experience with OGL should make assumption Y reasonable.  Different assumptions, not less.


So lets not play this game of growling at each other because we want to pretend the other is somehow making less assumptions.


And your documentation isn't enough.  The evidence for that is clear, but I'm going to shut my mouth about it.  It's apparent you would rather spend your time answering the same question every day than to admit that the documentation should probably be improved.  It's your time to spend.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: ichineko on November 23, 2012, 11:42:54 am
Quote
You're the one who started to insult people; you also take every comment extremely personal, but then don't stop at nothing and call other people to have a "personality quirk". The way you're acting is very immature and thus I had a very hard time believing that you've actually been working with C++ for a bit.

Are you a native English speaker exploiter?  It's an honest question.  The 'personality quirks' line you're referring to;  I was stating that *I* had the personality quirk, not others.  Specifically, I really dislike passive aggressive behavior.   Open hostility is preferable to passive aggressiveness, atleast one of them is honest.  It's a 'quirk' of mine.


Also, it would be a mistake to let your dislike of my posting style affect your evaluation of my expertise as a C++ developer.


Quote
No RAII is about the lifetime of resources.

Everyone understood my point, including you.  I will not get into a semantic debate with you about RAII.


Quote
As you say it yourself, this is only the way you think and it's not what one would usually expect.

Tell me, when and how would the texture then get destroyed? Because if the design was intended as you say, the OGL driver would be reference counting on its own, which it isn't. So we would still have to do this after all by hands and thus the OGL driver is not directly the owner.

I think maybe we're getting verbiage mixed up?  sf::texture vs the OGL texture is getting confused by the word 'texture'?

You effectively destroy the OGL texture by asking the OGL to destroy it.  Currently, SFML tracks this *anyway*.  It has to because the sprite doesn't hold a reference to the sf::Texture object, but to the OGL texture handle by proxy.  The danger with this approach is that the OGL driver is free to recycle that handle, so if a texture gets destroyed, and another one gets created with the *same* handle, that means your hero sprite may show up as a brick instead of mario.  SFML already has code in place to make sure that doesn't happen, which means you're 90% of the way there now in the current implementation, you just need something to kick off and actually *destroy* the texture when there are no more references from sf::texture objects.

Someone feel free to correct me if I'm misunderstanding the cache mechanism you have in place, I didn't look too closely at the specific implementation.

Please stop trying to imply that it's outlandish for someone to think that an external resource that can be handed to multiple sprite objects might be reference counted (or some other mechanism thereof).  It is not correct, but it *is* an understandable mistake.



PS

The question about being a native English Speaker is not meant as an insult, but as a way to attribute some of the animosity to miscommunication.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: Laurent on November 23, 2012, 11:52:35 am
Quote
And your documentation isn't enough.  The evidence for that is clear, but I'm going to shut my mouth about it.  It's apparent you would rather spend your time answering the same question every day than to admit that the documentation should probably be improved.  It's your time to spend.
I'm the first to admit when the documentation is not complete, and I'm happy to improve it and reduce the amount of dumb questions on the forum. Really.

But how can you say that my documentation is not enough, since the most important piece of documentation, namely the sprite/texture tutorial, is not online yet? Or, if you imply that the tutorial won't be enough no matter what it contains, what else do you suggest?

And you're right about assumptions. I should have answered from the start what I always answer: "don't assume, read the doc" ;)
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: FRex on November 23, 2012, 04:59:12 pm
Here is the link to 2.0 documentation(strangely, the 2.0 documentation contains the 2.0 documentation):
http://www.sfml-dev.org/documentation/2.0/annotated.php
Quote
when there are no more references from sf::texture objects.
Every texture is unique, copy of texture will get own resources assigned to it, there is no reference counting in texture.
Here is the link to github texture.cpp, read it:
https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/Texture.cpp
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: ichineko on November 23, 2012, 08:25:58 pm
Quote
But how can you say that my documentation is not enough, since the most important piece of documentation, namely the sprite/texture tutorial, is not online yet? Or, if you imply that the tutorial won't be enough no matter what it contains, what else do you suggest?


I think we're speaking from different perspectives Laurent.

The sprite/texture tutorial isn't online, so from my perspective it doesn't exist.  When I say your documentation isn't enough, I mean the documentation you have online right now is not enough.

Documentation that exists, but is not accessible, may as well not exist.


Quote
And you're right about assumptions. I should have answered from the start what I always answer: "don't assume, read the doc"

It is not enough.  I don't know how else to say it.  I did not realize a sprite had a lazily calculated transformation matrix that you could not combine with other transformations until I read the source.  I still don't understand why your circle primitive draws from the upper left corner.  I understand it from a technical perspective, but not from a design perspective.

It wasn't until I did go read the source that the meaning behind the API reference started to become clear.  This is what I'm trying to tell you.

You need more, what you have up now is not enough.  If the sprite/texture tutorial is it, great, awesome, I'm happy.  PM me a link, because I'd love to read over it, complete or not.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: ichineko on November 23, 2012, 08:27:03 pm
also, somewhat off topic, but is there a way to add someone to an ignore list on these boards?  Nothing is jumping out at me as the obvious way to do that.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: masskiller on November 23, 2012, 08:30:52 pm
It is somewhere in the profile options, it's easily found.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: cire on November 23, 2012, 08:42:44 pm
And your documentation isn't enough.  The evidence for that is clear, but I'm going to shut my mouth about it.  It's apparent you would rather spend your time answering the same question every day than to admit that the documentation should probably be improved.  It's your time to spend.
I'm actually not seeing any evidence at all.

 
That's not enough or we wouldn't be seeing even more posts about this exact issue.


I did a little searching.  The issue actually seems to be pretty rare on the forums, as far as I could determine using google to search the site.  I found 3 subjects that were obviously related to your assumption in skimming the first 70 or so hits.

http://en.sfml-dev.org/forums/index.php?topic=8586.0
http://en.sfml-dev.org/forums/index.php?topic=8112.0
http://en.sfml-dev.org/forums/index.php?topic=7930.0

The question apparently doesn't come up all that often.  All of them could've been resolved by reading the documentation.  Your assumption could've been resolved by reading the documentation.   What is needed is a magical way to force people to read the documentation.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: Laurent on November 23, 2012, 10:11:24 pm
Quote
The sprite/texture tutorial isn't online, so from my perspective it doesn't exist.  When I say your documentation isn't enough, I mean the documentation you have online right now is not enough.

Documentation that exists, but is not accessible, may as well not exist.
Quote
You need more, what you have up now is not enough
Ok, now I think you're an asshole ;)
(no offense, I'm kidding)

Seriously, what's the point of complaining about the incomplete tutorials? It's pretty obvious that I'm still writing them (the graphics section is empty), SFML 2.0 is a development version and has not been released yet. So don't say it's not enough, I know that.

I was expecting you to answer "ok, I understand that the tutorials are incomplete, I'll wait until the final release then" at some point. I don't understand what you're trying to say/prove in this thread.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: ichineko on November 24, 2012, 12:33:31 am
Thank you masskiller, I was looking for it in the users profile and not my own.

Quote
What is needed is a magical way to force people to read the documentation.

If you say so.  Personally, I thought I did pore over the documentation, but I'm guessing I didn't because otherwise why would you tell me I didn't?  Far be it for me to tell you what I did.


Quote
Seriously, what's the point of complaining about the incomplete tutorials? It's pretty obvious that I'm still writing them (the graphics section is empty), SFML 2.0 is a development version and has not been released yet. So don't say it's not enough, I know that.

I'm complaining about the expectation that reading the documentation is enough when the documentation is incomplete.  I had to read the source.  And that's fine, I'm a developer, not a big deal, but I don't like being told to read documentation when it's obviously incomplete.

It's a lame, cop-out answer where a simple explanation would have sufficed.

What I'm *doing* is repeatedly defending that.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: cire on November 24, 2012, 03:51:22 am

Quote
What is needed is a magical way to force people to read the documentation.

If you say so.  Personally, I thought I did pore over the documentation, but I'm guessing I didn't because otherwise why would you tell me I didn't?  Far be it for me to tell you what I did.

In fact, you told us what you did:

Quote
You know, I was going to complain about that link, I *have* been reading those documents.  But then I realized the sprite page did describe the issue I was running into.  I read it the first time I read over the page, but on subsequent visits I didn't hit the 'more' link and so missed it (and had forgotten the warning about keeping the texture alive, although I did remember that they were kept separate which caused unnecessary fears on my part).

You read and didn't retain.  Then you went back and failed to reread (the magical 'more' link just scrolls the page down.)  This doesn't indicate a deficiency in the documentation.  I'm not sure why you want to take my comments as a personal attack.  You say the evidence is obvious, but don't present any. Presenting some would be the logical response.


Quote
I'm complaining about the expectation that reading the documentation is enough when the documentation is incomplete.  I had to read the source.  And that's fine, I'm a developer, not a big deal, but I don't like being told to read documentation when it's obviously incomplete.

It's quite possible there are areas of the documentation that are lacking, and I'm sure if you were to bring them to Laurent's attention he would be happy to fix them, but this particular case isn't one of them.

I don't see this conversation going anywhere constructive, so I'll be taking my leave.  Good luck to you.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: ichineko on November 25, 2012, 01:36:22 am
Personal?  I think you misunderstand Cire, my point is that you have no business telling me what I did.

I often find people online explaining to me what my argument is, and my response is always the same.  If you have to tell me what my argument is, chances are you're involved in a strawman.

Likewise, if you feel the need to tell me what I did, or didn't do, *despite* my protestations, then chances are you're not correct.

I skimmed over a lot of pages when I first started reading the documentation, to get a general feel for how things work.  I'm sorry you feel I should have remembered every single tidbit, but I didn't.  Call me less if you will, stupid if you must, but don't treat it as something that no one else will do.

Also, I acknowledged the mistake and apologized.  You tell me I'm taking things personally, but no one in this thread has been 'pushed back on' more than me. If I truly took things personally, I'd tell you all to fuck off and go grab Allegro.

Don't mistake my blunt posting style for strong emotion.  I will push back when I feel it necessary, but nothing you say here is going to be taken 'personally' by me.

I'm extremely opinionated, and it rubs people the wrong way many times.  I understand that, there's only been 1 person in this thread whom I thought went too far and was being malicious, that person is being ignored now.


The evidence I was speaking of was the other posts I saw pop up about roughly the same issue.  Am I mistaken?  Then tell me I'm mistaken, and be done with it.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: masskiller on November 25, 2012, 06:07:21 am
Ichineko, if you had at least focused only in the question and not on useless arguments this thread would have been resolved long ago and so would have been your doubts. The reason I chose not to butt into this was to see if it calmed down on it's own, given it's not I will give you one effective but blunt advice:

Shut Up!

If you keep on reacting with a wall of text over every single comment you'll annoy everyone here, and eventually yourself since there will most likely someone else to talk further. If you have a question just ask clearly and don't make a fuss over the slightest issues. If you had just ignored Cire's post I wouldn't have written anything and that would have been probably the end of it.

Yes I may be making things a bit longer, but I am at least hoping that with this post you'll understand that making an argument in a help thread will only get you bashed. I have been refraining myself from not doing so, and surely not just me...

Quote
but no one in this thread has been 'pushed back on' more than me.

Expect that if you try to push back others when asking for help. I find it unreasonable, but then again I can't do anything against it, can I? And yes I pushed you back once more, so what? Sometimes you just have to swallow things up. It almost looks like you've never been or seen a true argument in a forum or that you are doing this on purpose and are seriously looking for a ban. I prefer to think it's the former.

Quote
Personal?  I think you misunderstand Cire, my point is that you have no business telling me what I did.

That quote is the embodiment of taking things personal in my world. Can't really say about yours...

Quote
The evidence I was speaking of was the other posts I saw pop up about roughly the same issue.  Am I mistaken?  Then tell me I'm mistaken, and be done with it.

Take up your own words then, making this any longer is rather tedious if you ask me. I won't make flames. I'll just leave it here once again, hoping this is enough to satisfy whatever desire you have in keeping this on.
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: ichineko on November 25, 2012, 08:59:29 am
Quote
The reason I chose not to butt into this was to see if it calmed down on it's own, given it's not I will give you one effective but blunt advice:

Shut Up!


I like you masskiller :)

As for the rest of it, this forum has a culture that I find a shitty in a lot of ways.  From exploiter whining that a newbie disrespected him (lol), to cire telling me I didn't read something I most definitely did read (but it was personal on my end?) to gyscos telling me I need to post code and then telling me he or she didn't care about my code(say what?).  Maybe I should roll over and ask for my belly to be rubbed, but that's not my personality.  If I get banned for it, so be it.  Sometimes a person just doesn't belong.

Having said that, my view of Laurent is mostly positive, the difference between him and Exploiter is like night and day imo.  Laurent had more reason to be offended, and if he was, he was atleast fair about it.  Exploiter is just ... a prima donna.  And I'm an asshole, so feel free to point it out as often as you wish ;)
Title: Re: Explanation of interactions between Image, Texture, and Sprite?
Post by: Laurent on November 25, 2012, 10:27:10 am
This thread is not going anywhere, so I'm going to lock it.

It's sad to see this kind of behaviour -- and I'm talking about everyone. I don't like threads like this one, it really gives a bad feeling about this forum. I'm sure none of you likes that either. So please, focus on helping and keep your personal feelings for PMs or for yourself.