Game Development Reference
In-Depth Information
Profiling results for PlayState
We can see that Gosu::Image#draw takes up to 20% of all execution time. Then goes
Gosu::Window#caption , but we need it to measure FPS, so we will leave it alone,
and finally we can see Hash#each , which is guaranteed to be the one from Map#draw ,
and it triggers all those Gosu::Image#draw calls.
Optimizing Inefficient Code
According to profiling results, we need to optimize this method:
class Map
# ...
def draw (camera)
@map . each do | x, row |
row . each do | y, val |
tile = @map [ x ][ y ]
map_x = x * TILE_SIZE
map_y = y * TILE_SIZE
tile . draw(map_x, map_y, 0 )
end
end
end
# ...
end
But we have to optimize it in more clever way than we did before. If instead of looping
through all map rows and columns and blindly rendering every tile or checking if tile is
visible we could calculate the exact map cells that need to be displayed, we would reduce
method complexity and get major performance boost. Let's do that.
We will use Camera#viewport to return map boundaries that are visible by camera,
then divide those boundaries by Map#TILE_SIZE to get tile numbers instead of pixels,
and retrieve them from the map.
class Map
# ...
Search WWH ::




Custom Search