2d Map Culling OpenGL/SDL

Anything related in any way to game development as a whole is welcome here. Tell us about your game, grace us with your project, show us your new YouTube video, etc.

Moderator: PC Supremacists

Post Reply
jjackdev
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Mon Aug 30, 2010 11:55 pm

2d Map Culling OpenGL/SDL

Post by jjackdev »

Hello,

I am working on a game with a 2d scrolling tilemap. I am currently finding ways to optimize it. I found a couple but one that I can never get to work in 2d map culling. Is there a way someone can point me or show me how I could implement this with SDL and OpenGL.

Thanks
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: 2d Map Culling OpenGL/SDL

Post by Ginto8 »

"Premature optimization is the root of all evil." For modern graphics hardware, drawing a 2D tilemap is about as labor-intensive as it is for you to lift a 3-pound weight. Not only is there no need to worry about culling backfaces, there are no optimizations that you need. If you really need to do some optimization, look into vertex arrays. Other than that, any optimizations you do will have minimal effect (maybe a millisecond or two less draw time).
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
WSPSNIPER
Chaos Rift Regular
Chaos Rift Regular
Posts: 145
Joined: Sun Jan 03, 2010 6:19 pm
Current Project: top down shooter
Favorite Gaming Platforms: ps3
Programming Language of Choice: c++

Re: 2d Map Culling OpenGL/SDL

Post by WSPSNIPER »

agreed. but what you could do is in the map rendering function check to see if the tile is on the screen before you render it.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: 2d Map Culling OpenGL/SDL

Post by GroundUpEngine »

Ginto8 wrote:If you really need to do some optimization, look into vertex arrays. Other than that, any optimizations you do will have minimal effect (maybe a millisecond or two less draw time).
Very good advice.
User avatar
Lord Pingas
Chaos Rift Regular
Chaos Rift Regular
Posts: 178
Joined: Thu Dec 31, 2009 9:33 am
Favorite Gaming Platforms: NES, SNES, Nintendo 64, Dreamcast, Wii
Programming Language of Choice: C++
Location: Hiding In My Mum's Basement With My Pokemon Cards

Re: 2d Map Culling OpenGL/SDL

Post by Lord Pingas »

User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: 2d Map Culling OpenGL/SDL

Post by dandymcgee »

WSPSNIPER wrote:agreed. but what you could do is in the map rendering function check to see if the tile is on the screen before you render it.
This is what culling means, and contrary to Ginto's advice I think that culling off-screen tiles is a logical and completely necessary optimization. If your world is 4000x3000 tiles big and you can only ever see 20x15 on the screen at once, that's a shit ton of render time being wasted on drawing.. well nothing.

To expand slightly on what WSPSNIPER said, you need to check the coords of a tile against the camera's location and if tile overlaps the camera rectangle render it, otherwise don't. Depending on how you represent the map, you can probably hack together an algorithm to only call the render function of the visible tiles rather than calling render on every one and then checking whether or not it's visible.

Check the link Lord Pingas posted.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: 2d Map Culling OpenGL/SDL

Post by MrDeathNote »

dandymcgee wrote:
WSPSNIPER wrote:agreed. but what you could do is in the map rendering function check to see if the tile is on the screen before you render it.
This is what culling means, and contrary to Ginto's advice I think that culling off-screen tiles is a logical and completely necessary optimization. If your world is 4000x3000 tiles big and you can only ever see 20x15 on the screen at once, that's a shit ton of render time being wasted on drawing.. well nothing.

To expand slightly on what WSPSNIPER said, you need to check the coords of a tile against the camera's location and if tile overlaps the camera rectangle render it, otherwise don't. Depending on how you represent the map, you can probably hack together an algorithm to only call the render function of the visible tiles rather than calling render on every one and then checking whether or not it's visible.

Check the link Lord Pingas posted.
I would have to agree with you man, good advice. You can just alter the limits of your rendering loop to draw only on screen tiles.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
Post Reply