Class: RGame::Engine::View

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#cameraObject (readonly)

Returns the value of attribute camera.



32
33
34
# File 'lib/rgame/engine/view.rb', line 32

def camera
  @camera
end

#heightObject (readonly)

Returns the value of attribute height.



32
33
34
# File 'lib/rgame/engine/view.rb', line 32

def height
  @height
end

#playerObject (readonly)

Returns the value of attribute player.



32
33
34
# File 'lib/rgame/engine/view.rb', line 32

def player
  @player
end

#widthObject (readonly)

Returns the value of attribute width.



32
33
34
# File 'lib/rgame/engine/view.rb', line 32

def width
  @width
end

#xObject (readonly)

Returns the value of attribute x.



32
33
34
# File 'lib/rgame/engine/view.rb', line 32

def x
  @x
end

#yObject (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_xObject

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_yObject

hot-path



73
# File 'lib/rgame/engine/view.rb', line 73

def offset_y = @y - origin_y

#origin_xObject

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_yObject

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

Returns:

  • (Boolean)


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