Game Development Reference
In-Depth Information
def
draw
(camera)
viewport
=
camera
.
viewport
viewport
.
map! {
|
p
|
p
/
TILE_SIZE
}
x0, x1, y0, y1
=
viewport
.
map(
&
:to_i
)
(x0
.
.x1)
.
each
do
|
x
|
(y0
.
.y1)
.
each
do
|
y
|
row
=
@map
[
x
]
if
row
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
This optimization yielded astounding results. We are now getting nearly stable 60 FPS even
when profiling the code! Compare that to 2 FPS while profiling when we started.
Profiling results for
PlayState
after
Map#draw
optimization
consuming 1/3 of our CPU cycles! Even though game is already flying so fast that we will
have to reduce tank and bullet speeds to make it look more realistic, we cannot let ourselves
leave this low hanging fruit remain unpicked.
We will update the caption once per second, it should remove the bottleneck:
class
PlayState
<
GameState
# ...
def
update
# ...
update_caption
end
# ...
private
def
update_caption