Game Development Reference
In-Depth Information
Initial profiling results
It's obvious, that
Camera#viewport
and
Camera#can_view?
are top CPU burners.
This means either that our implementation is either very bad, or the assumption that
checking if camera can view object is slower than drawing the object off screen.
Here are those slow methods:
class
Camera
# ...
def
can_view?
(x, y, obj)
x0, x1, y0, y1
=
viewport
(x0
-
obj
.
width
.
.x1)
.
include?(x)
&&
(y0
-
obj
.
height
.
.y1)
.
include?(y)
end
# ...
def
viewport
x0
=
@x
-
(
$window
.
width
/ 2
)
/
@zoom
x1
=
@x
+
(
$window
.
width
/ 2
)
/
@zoom
y0
=
@y
-
(
$window
.
height
/ 2
)
/
@zoom
y1
=
@y
+
(
$window
.
height
/ 2
)
/
@zoom
[
x0, x1, y0, y1
]
end
# ...
end
It doesn't look fundamentally broken, so we will try our “checking is slower than
rendering” hypothesis by short-circuiting
can_view?
to return
true
every time:
class
Camera
# ...
def
can_view?
(x, y, obj)
return
true
# short circuiting
x0, x1, y0, y1
=
viewport
(x0
-
obj
.
width
.
.x1)
.
include?(x)
&&
(y0
-
obj
.
height
.
.y1)
.
include?(y)
end
# ...
end