Class: RGame::Engine::View
- Inherits:
-
Object
- Object
- RGame::Engine::View
- Defined in:
- lib/rgame/engine/view.rb
Overview
One viewport being drawn: a rectangle of the screen, and (for a world view) the camera to look through it with.
view.x, view.y, view.width, view.height # the screen rect
view.camera # nil in screen space
view.player # whose view this is, or nil
view.visible?(x, y, w, h) # is this worth drawing
What a view is for
draw(renderer, view) hands every node the view it is being drawn into,
which answers two questions nothing else can. Where the edges are: a
HUD laying itself out against the whole window is wrong the moment the
window is a player's half of one, and until this existed there was nothing
else to ask. And what is worth drawing: with the world drawn once per
player, culling stops being an optimisation and starts being the
difference between one frame's work and four.
It is reused, not rebuilt
Viewports owns one of these per viewport and mutates it in place each frame, the way ActionMapper reuses its Actions. Building fresh ones would allocate a handful of objects every frame — invisible by every measure except the frame that stutters. Hold the player or the viewports, never this: the object a node was handed last frame is the same one, with different numbers in it.
Instance Attribute Summary collapse
-
#camera ⇒ Object
readonly
Returns the value of attribute camera.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#player ⇒ Object
readonly
Returns the value of attribute player.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
-
#initialize(x: 0, y: 0, width: 0, height: 0, camera: nil, player: nil) ⇒ View
constructor
A new instance of View.
-
#offset_x ⇒ Object
The offset to translate by so that content at
originlands at this view's corner on screen. -
#offset_y ⇒ Object
hot-path.
-
#origin_x ⇒ Object
Where this view's contents start, in the space its nodes draw in: the camera's offset for a world view, and the origin for a screen-space one, whose nodes draw relative to the view's own corner.
-
#origin_y ⇒ Object
hot-path.
-
#set(x, y, width, height, camera: nil, player: nil) ⇒ Object
Mutated in place by Viewports once per frame.
-
#visible?(x, y, width, height) ⇒ Boolean
Does a rectangle overlap what this view shows? Coordinates are in the space the caller draws in — world coordinates under a camera, view-local ones in screen space — which is the same space
origin_xis in.
Constructor Details
#initialize(x: 0, y: 0, width: 0, height: 0, camera: nil, player: nil) ⇒ View
Returns a new instance of View.
34 35 36 |
# File 'lib/rgame/engine/view.rb', line 34 def initialize(x: 0, y: 0, width: 0, height: 0, camera: nil, player: nil) set(x, y, width, height, camera: camera, player: player) end |
Instance Attribute Details
#camera ⇒ Object (readonly)
Returns the value of attribute camera.
32 33 34 |
# File 'lib/rgame/engine/view.rb', line 32 def camera @camera end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
32 33 34 |
# File 'lib/rgame/engine/view.rb', line 32 def height @height end |
#player ⇒ Object (readonly)
Returns the value of attribute player.
32 33 34 |
# File 'lib/rgame/engine/view.rb', line 32 def player @player end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
32 33 34 |
# File 'lib/rgame/engine/view.rb', line 32 def width @width end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
32 33 34 |
# File 'lib/rgame/engine/view.rb', line 32 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
32 33 34 |
# File 'lib/rgame/engine/view.rb', line 32 def y @y end |
Instance Method Details
#offset_x ⇒ Object
The offset to translate by so that content at origin lands at this
view's corner on screen. The whole of split-screen, in two numbers.
hot-path
71 72 |
# File 'lib/rgame/engine/view.rb', line 71 def offset_x = @x - origin_x # hot-path |
#offset_y ⇒ Object
hot-path
73 |
# File 'lib/rgame/engine/view.rb', line 73 def offset_y = @y - origin_y |
#origin_x ⇒ Object
Where this view's contents start, in the space its nodes draw in: the camera's offset for a world view, and the origin for a screen-space one, whose nodes draw relative to the view's own corner. hot-path
53 54 |
# File 'lib/rgame/engine/view.rb', line 53 def origin_x = @camera ? @camera.x : 0 # hot-path |
#origin_y ⇒ Object
hot-path
55 |
# File 'lib/rgame/engine/view.rb', line 55 def origin_y = @camera ? @camera.y : 0 |
#set(x, y, width, height, camera: nil, player: nil) ⇒ Object
Mutated in place by Viewports once per frame. Not for game code.
39 40 41 42 43 44 45 46 47 |
# File 'lib/rgame/engine/view.rb', line 39 def set(x, y, width, height, camera: nil, player: nil) @x = x @y = y @width = width @height = height @camera = camera @player = player self end |
#visible?(x, y, width, height) ⇒ Boolean
Does a rectangle overlap what this view shows? Coordinates are in the
space the caller draws in — world coordinates under a camera, view-local
ones in screen space — which is the same space origin_x is in.
hot-path
61 62 63 64 65 66 |
# File 'lib/rgame/engine/view.rb', line 61 def visible?(x, y, width, height) left = origin_x top = origin_y x + width > left && x < left + @width && y + height > top && y < top + @height end |