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

Author Topic: RenderWindow bounds?!  (Read 5619 times)

0 Members and 1 Guest are viewing this topic.

bitano

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
RenderWindow bounds?!
« on: April 03, 2015, 03:34:24 pm »
Hi there!

Not too long ago i made a game which progressed pretty well, but for some reason crashed after approximately 30 minutes (sometimes 40). It seemed like one of my functions / pointers was leaking somewhere but i couldn't find any cause. As far as i could tell, i made sure to clear up any unused pointers.

I started considering that the leak was caused by one of the SFML functions i might have misinterpreted...I tried to troubleshoot some but in the end let it rest...

-----

Now i'm starting on a new game and this still bugs (pun...lol) me. What i'm thinking at the moment is that the most used function while the game is running is the one drawing tiles to the RenderWindow.

I realized that i never found out whether i should set the outer bounds of a RenderWindow.

Should i define the outer bounds of the RenderWindow somewhere?
What are the size limits of RenderWindow (considering i'd want to use a huge tilemap) ?
What if a function ended up drawing something outside the boundaries of RenderWindow (for example starting from negative X,Y coords)?

I hope i was clear enough on this and that someone is able / willing to help me.

Thanks!

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: RenderWindow bounds?!
« Reply #1 on: April 03, 2015, 03:43:56 pm »
The render window has no bounds. However, the sf::View (there's always one) has bounds, and it can be of any size. (same size as the window if you use the default view)
If you draw something out of what the render window shows, it stills takes (/wastes) time. Thus you should avoid drawing things that are outside the screen.

But drawing things outside the window isn't related to memory leaks. ;)
« Last Edit: April 03, 2015, 03:46:53 pm by G. »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: RenderWindow bounds?!
« Reply #2 on: April 03, 2015, 03:49:00 pm »
There is no "window bounds"... that isn't how it works. Of course there is a max value and that is limited to the maximum value of floats. But there is no limit to how much you can draw or how big your map is. However you may want to limit what you do draw however because of performance reasons.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: RenderWindow bounds?!
« Reply #3 on: April 03, 2015, 03:54:09 pm »
Not too long ago i made a game which progressed pretty well, but for some reason crashed after approximately 30 minutes (sometimes 40). It seemed like one of my functions / pointers was leaking somewhere but i couldn't find any cause. As far as i could tell, i made sure to clear up any unused pointers.
What you describe is undefined behavior, which can have many causes. Most common are:
  • Dereferencing of invalid pointers/references/iterators
  • Array out-of-bound access
  • Wrong use of delete or delete[] operators
  • Linking of incompatible libraries (compiled with different compiler versions/settings)
The occurrence of such bugs may be a hint that you use badly abstracted code. Modern C++ features (smart pointers, STL containers) as well as compiler debug support (assertions, checked iterators) often mitigate such issues -- make sure you use them. For example, using new and delete in your program is usually a bad idea.

I started considering that the leak was caused by one of the SFML functions i might have misinterpreted...I tried to troubleshoot some but in the end let it rest... [...] What i'm thinking at the moment is that the most used function while the game is running is the one drawing tiles to the RenderWindow.
SFML shouldn't cause such issues. And the number of times you call a function is no reliable bug indicator -- it is enough to call a function with UB once, and the error may manifest only half an hour later. That's why these things are terribly tedious to debug, and you'd better avoid them in the first place by writing good code. Some time ago, I assembled a list of C++ tips that may help you in this regard.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

bitano

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: RenderWindow bounds?!
« Reply #4 on: April 03, 2015, 04:00:10 pm »
Excellent advice! Thanks.

Yes i am aware of drawing only the tiles / sprites that are actually visible in the view, but thanks for that :-).
My code could've been cleaner and was based on what i knew from older projects (actually did use new and delete, etc).

This too is the reason why i want to start all over and make the code as clean as possible from the get go. That's also why i raised this question. Not that i don't trust the SFML library (that would be silly), but that i might've made an incorrect assumption

Thanks again! I will certainly check out the provided links. Feel free to pass some other related links if you're aware of any :-)

bitano

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: RenderWindow bounds?!
« Reply #5 on: April 09, 2015, 09:21:05 pm »
Kind of hijacking my own thread here, but it felt silly to make a new post for such a small (and somewhat related) question:

For readability in the code i'd still like to use function-local pointers.

So instead of having to address this->that->stuffz->thingies[4]->id each time i want to access a property, i'd instead use thingy->id

Is it still the proper way to define a pointer like:

Code: [Select]
ThingyClass *thingy = &this->that->stuffz->thingies[4];

And am i correct in assuming i don't have to clean up this pointer at function end as it's only a reference and i have the actual contents stored elsewhere (in a vector array) anyways?

Thanks!
« Last Edit: April 09, 2015, 09:23:21 pm by bitano »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: RenderWindow bounds?!
« Reply #6 on: April 10, 2015, 12:13:21 am »
And am i correct in assuming i don't have to clean up this pointer at function end as it's only a reference and i have the actual contents stored elsewhere (in a vector array) anyways?
Yes. In general, it's very simple: for every memory allocation, you need a corresponding deallocation.

The best is still if you use RAII and avoid new/delete completely. STL containers, smart pointers and other abstractions can do most of the work for you.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

bitano

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: RenderWindow bounds?!
« Reply #7 on: April 10, 2015, 08:41:21 am »
And am i correct in assuming i don't have to clean up this pointer at function end as it's only a reference and i have the actual contents stored elsewhere (in a vector array) anyways?
Yes. In general, it's very simple: for every memory allocation, you need a corresponding deallocation.

The best is still if you use RAII and avoid new/delete completely. STL containers, smart pointers and other abstractions can do most of the work for you.

I'm not quite following you. In my example i'm not allocating memory, am i? I'm just defining a function-local reference to data stored in another structure...Or so i thought.

Or are you referring to the vector array? I was under the impression those also get cleaned up in the destructor.
I'm not dismissing the use of RAII - i actually like it a lot. I'm not seeing its application though with regards to this specific question :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: RenderWindow bounds?!
« Reply #8 on: April 10, 2015, 08:52:23 am »
I'm not quite following you. In my example i'm not allocating memory, am i? I'm just defining a function-local reference to data stored in another structure...Or so i thought.
Exactly... That's why my answer to the question "am I correct in assuming..." was "yes" :)

To clarify, std::vector cleans up automatically (this is RAII), and you don't have to worry about pointers to its elements. Just keep them valid as long as you use them, but that's a different topic.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

bitano

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: RenderWindow bounds?!
« Reply #9 on: April 10, 2015, 08:59:29 am »
I'm not quite following you. In my example i'm not allocating memory, am i? I'm just defining a function-local reference to data stored in another structure...Or so i thought.
Exactly... That's why my answer to the question "am I correct in assuming..." was "yes" :)

To clarify, std::vector cleans up automatically (this is RAII), and you don't have to worry about pointers to its elements. Just keep them valid as long as you use them, but that's a different topic.

Ah right. Thanks for the clarification and your patience. It's really appreciated :D