-
Notifications
You must be signed in to change notification settings - Fork 0
How Surface Works
Next Chapter >> Creating the Application
Previous Chapter << Introduction to Surface
The Surface Engine, in it's current form, is an API for constructing interactive applications. Basically, it handles all the boring stuff and exposes functions that let you build games. There are a few core concepts that power Surface, so let's get some basic definitions out of the way before continuing.
-
Application
: The literal application that runs, responsible for handling user inputs and renderingViews
. -
View
: ContainsLayers
andOverlays
. A singleApplication
can have multipleViews
, but only oneView
can be active at a time. -
Layer
: HoldObjects
and receiveEvents
, these are rendered such that the lastLayer
in aView
is drawn above everything else. -
Overlay
: Identical toLayers
, except they are guaranteed to be drawn above all otherLayers
and receive allEvents
first, regardless if they are visible or not. -
Event
: Basically containers for application events. Anything that happens to the application is propagated down to theOverlays
, then the Surface Engine itself, theLayers
, and subsequently allObjects
-
Object
: The building blocks of theApplication
, they are stored insideLayers
andOverlays
and can basically be anything. The Surface Engine provides a few templateObjects
that can used as they are or extended with custom classes. -
Group
: This is a built-in type ofObject
which acts as a container for multipleObjects
. More information about this special type will be discussed later. -
Camera
: This is another built-in type ofObject
, except it is also registered in theView
itself. MultipleCameras
can exist, however the ordering of them is crucial to deciding what is rendered and where it is on the screen.
Building a game with the Surface Engine is designed to be very simple, but you still have enough control to turn it into whatever you want. Just to get things straight, here is the model for how events flow throught the Surface Engine.
- Event occurs
- Derived
Application
receives event inOnEvent()
- All
Overlays
in the currently activeView
receive the event inOnEvent()
regardless of visibility - Base
Application
class receives event and could terminate here - All
Layers
in the currently activeView
receive the event inOnEvent()
only if they are visible
Let's use a thought experiment to better understand the flow of Events
. Imagine we have a basic FPS game, and currently there is only one level where the player is standing in front a single enemy on the screen.
In this example, there is a single View
, Layer
, Overlay
and five Objects
. The Surface Engine let's you give everything a name, so here is a simple tree view that describes our game:
-
Application
"Game"-
View
"World"-
Overlay
"HUD"-
Object
"Cross-hair" -
Object
"Health Bar"
-
-
Layer
"Level 1"-
Group
"Player"-
Camera
"Main"
-
-
Object
"Enemy"
-
-
-
Because we can name everything, it makes everything pretty straight-forward. Below is an example of what happens, and in what order, whenever a player moves their mouse.
-
A
MouseMovedEvent
is created by theApplication
, and is propagated down to all theOverlays
andLayers
in the "World"View
. -
The "HUD"
Overlay
receives the event first. Because, in this game, the "HUD" only has static UI elements, we tell it to completely ignore theMouseMovedEvent
and to not pass it down to theObjects
. -
Layer
"Level 1" receives the event last. In this game we definitely want theMouseMovedEvent
to be handled by our "Player"Group
, so we allow the event to be propagated to all its children. -
The "Player"
Group
receives the event first. In order for theCamera
to turn, we have the "Player" update its rotation based on the data contained in theMouseMovedEvent
. -
The
Object
named "Enemy" receives the event next. Because we want the "Enemy" to be autonomous and uncontrolled by the player, we tell the "Enemy" to ignore the event.
As a slight modification, we could have marked the event as "inactive" at the end of Step 4, which would have prevented the event from even reaching the "Enemy". This is up to you as a developer, however. It may be beneficial in some situations, like to prevent clicking on a menu option from also triggering the player to fire their weapon. However it could also be good to not do this, such as when changing the rotation of a minimap on your HUD and the player's direction by allowing both to handle the event.
WIP
Next Chapter >> Creating the Application
Previous Chapter << Introduction to Surface