Drawing outside window

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
kloki
Prole
Posts: 4
Joined: Thu Jul 26, 2012 10:13 am
Location: Netherlands

Drawing outside window

Post by kloki »

I'm making a 2d platformer. I have a table of all the dynamic objects in the game world. Right now I'm naively looping through the table and drawing all the objects in the table. But because the game world is much bigger than the window size most object dont make it on screen.

Am I wasting resources like this? Should I make function that checks if a objects would appear on screen or does love.graphics.draw already handle this efficiently?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Drawing outside window

Post by Robin »

In general that's not necessary.

If you find yourself having a lot of things to draw, most of which are off-screen anyway, you might want to use something like a spatial hash.

Basically, instead of having one table containing all the entities, you have a table of tables. Each table refers to a bit of space, and contains all the objects that are in that space.

That way, you only have to loop over the tables that are (partially or completely) visible. Note that how big the pieces of space are matters a lot for how it performs. You might want to experiment a bit with that.
Help us help you: attach a .love.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Drawing outside window

Post by ivan »

Am I wasting resources like this?
I'm not too familiar with the Love2D renderer but yes drawing geometry from Lua would probably cause extra overhead on the backend.
Depending on how large your levels are this might or might not be an issue.
Should I make function that checks if a objects would appear on screen or does love.graphics.draw already handle this efficiently?
Probably a better idea to just iterate your visible tiles and draw them which would be fairly trivial especially if your objects are already stored in a 2D table.
kloki
Prole
Posts: 4
Joined: Thu Jul 26, 2012 10:13 am
Location: Netherlands

Re: Drawing outside window

Post by kloki »

Ok thanks, the spatial hash is a good idea but then of course I have to make sure the object can't leave the region of the hash.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Drawing outside window

Post by Robin »

Or move them to a different table when they leave the region.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 64 guests