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

Author Topic: Optimizing tilerendering  (Read 9381 times)

0 Members and 1 Guest are viewing this topic.

Kingdom of Fish

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
Optimizing tilerendering
« on: September 09, 2010, 11:23:54 pm »
I'm curious if there are any simple optimizations to rendering tiles in SFML2 that you really need to know about.

I was thinking about caching parts of the screen in larger images instead of drawing each tile every frame. Is this even worth it? SFML2 seems to be pretty fast with tiles anyway and I would hate to hack together a bunch of code and figure out it was just a big waste of time and clever thinking.

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
Optimizing tilerendering
« Reply #1 on: September 10, 2010, 02:17:39 am »
It really depends on what number of tiles you are drawing in a frame. If you have it optimized to only draw what you see, I think that you don't need to do anything more.

You can always create some maps and check out their frame rate.

PeterWelzien

  • Newbie
  • *
  • Posts: 38
    • View Profile
Optimizing tilerendering
« Reply #2 on: September 10, 2010, 07:35:13 am »
Quote from: "panithadrum"
If you have it optimized to only draw what you see, I think that you don't need to do anything more.

Does SFML draw sprites that are completely off screen? Do I have to do my own frustum culling?
/Peter Welzien

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Optimizing tilerendering
« Reply #3 on: September 10, 2010, 08:19:50 am »
Quote
I'm curious if there are any simple optimizations to rendering tiles in SFML2 that you really need to know about.

No, basically SFML already optimizes things a lot internally.
The only thing that *may* give better performances is to group sprites by image for drawing.

Quote
Does SFML draw sprites that are completely off screen? Do I have to do my own frustum culling?

Yes, SFML draws everything so if you don't want to draw off-screen entities you must filter them out manually.
Laurent Gomila - SFML developer

Gibgezr

  • Newbie
  • *
  • Posts: 33
    • View Profile
Optimizing tilerendering
« Reply #4 on: February 18, 2011, 01:56:38 am »
Interesting. I've coded a lot of 2d tile-based scrolling engines over the years, and no matter what the API, always found it best to implement the classic approach if I needed sub-tile (i.e pixel) scrolling:

1) Create an offscreen buffer that is two tiles wider/higher than the display area.
2) predraw the buffer ONCE, using the individual tiles
3)in the render loop, do one large blit, the size of the display, from the buffer, using scroll offsets to position the source rectangle for the blit.
4)if the player has scrolled more than one tile in any direction, update the offscreen buffer by resetting the scroll offsets and either:
 a) shuffling rows or columns of tiles on the offscreen buffer (blits with same source and destination that do not overlap), then drawing in the new row or column of tiles as individual tile blits, or
 b)do one large blit from the offscreen buffer to a second offscreen buffer, shifted by one row/column, that becomes the new offscreen buffer for our tilemap, and then fill in the new row/column with individual tile blits.

The benefit can be seen by supposing you have a 40x30 tile display: for many frames, your tiled background goes from 1200+ blits down to ONE blit, and even when the scrolling reaches the buffer edge and has to update ala #4, it goes down to either 33 or 43 blits for that one frame (using the ping=ponging offscreen buffers method, or 63 or 83 for the single buffer method). Yes, some of those blits are larger in area than the individual tile blits that make up updating a 1200+ blit screen, but in most APIs, doing one large blit is about the same speed as doing one small blit.

I can't imagine what optimizations SFML has that make the above not true anymore; can someone enlighten me? Now, I can see possibly in SFML 2 that Views might be used as part of the above 4 steps, but I don't see how that changes the steps. I'm quite willing to learn new tricks, though!

Also, something close to the above is beneficial even if you are doing whole-tile scrolling, but the offscreen buffer need not be any bigger than the intended display area, and it gets updated on every scroll. Still better than doing 1200 blits.

vidjogamer

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
Optimizing tilerendering
« Reply #5 on: March 08, 2011, 09:27:32 am »
Gibgezr thank you that is genius! I cant wait to get that implemented. =)

l0calh05t

  • Full Member
  • ***
  • Posts: 200
    • View Profile
Optimizing tilerendering
« Reply #6 on: March 08, 2011, 12:14:12 pm »
why not use the wrapping texture capability of opengl? no need to "shuffle" all rows. just update a single row/column and your texcoords. should be even faster.

Gibgezr

  • Newbie
  • *
  • Posts: 33
    • View Profile
Optimizing tilerendering
« Reply #7 on: March 15, 2011, 09:04:15 pm »
Quote from: "l0calh05t"
why not use the wrapping texture capability of opengl? no need to "shuffle" all rows. just update a single row/column and your texcoords. should be even faster.


Not sure how to implement that, as adjusting texcoords adjusts the coords across the entire buffer...ahhh, I see, shift the texcoords, then update the rest of the buffer? How do I get access to the texcoords of an sfml sprite object?

For me personally, it wouldn't be worth the hassle writing that, especially if you are using sfml; the whole point of sfml for 2D graphics is to use sfml sprites and the nice, clean OO interfaces. If I was going to muck around with texcoords, I'd probably go straight to OpenGL and forgo sfml.

l0calh05t

  • Full Member
  • ***
  • Posts: 200
    • View Profile
Optimizing tilerendering
« Reply #8 on: March 15, 2011, 09:42:09 pm »
Quote from: "Gibgezr"
Quote from: "l0calh05t"
why not use the wrapping texture capability of opengl? no need to "shuffle" all rows. just update a single row/column and your texcoords. should be even faster.


Not sure how to implement that, as adjusting texcoords adjusts the coords across the entire buffer...ahhh, I see, shift the texcoords, then update the rest of the buffer? How do I get access to the texcoords of an sfml sprite object?

For me personally, it wouldn't be worth the hassle writing that, especially if you are using sfml; the whole point of sfml for 2D graphics is to use sfml sprites and the nice, clean OO interfaces. If I was going to muck around with texcoords, I'd probably go straight to OpenGL and forgo sfml.


Dunno really, I use OpenGL directly most of the time anyways. I think Get/SetSubRect might be used for this, although it depends on which wrapping mode SFML uses.