Game Development Reference
In-Depth Information
Profiling results for
PlayState
Gosu::Window#caption
,
but we need it to measure FPS, so we will leave it alone,
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
# ...