new rlEngine(containerId, name, debug, width, height, glContextAttributesopt, useOwnTimeropt) → {rlEngine}
Instantiate an object for easily handling an application/game main loop(gathering input and performing logic state and view updates at regular intervals).
An engine will also provide two Canvas elements along with a webgl and a 2d context, ready-to-use in view updates.
The canvas elements are layered in a way that the 2d canvas will be rendered on top of the webgl canvas.
The 2d canvas will also be used by the engine to display debug information (if enabled).
By default, the engine will instantiate and handle a timer running in a separate thread, so that logic updates can be performed even when the engine is not in the active browser-tab.
An engine starts in Paused state to allow setting the onInputEvent
, onUpdateLogic
and onUpdateViews
properties before calling togglePause
to start it.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
containerId |
string | the DOM id of an Element into which to insert the engines views |
||
name |
string | the name of the engine as it appears in debug messages, if debug is enabled |
||
debug |
boolean | set to true to enable debug messages and debug info to be displayed on view updates |
||
width |
number | the width of the engines views in pixels |
||
height |
number | the height of the engines views in pixels |
||
glContextAttributes |
WebGLContextAttributes |
<optional> |
||
useOwnTimer |
boolean |
<optional> |
true | set to false to prevent the engine from instantiating and managing its own logic update timer |
Returns:
- Type
- rlEngine
Members
-
onInputEvent :rlEngine~callbackOnInputEvent
-
Type:
-
onUpdateLogic :rlEngine~callbackOnUpdateLogic
-
Type:
-
onUpdateViews :rlEngine~callbackOnUpdateViews
-
Type:
Methods
-
changeLUPS(newLUPSTarget)
-
Change the desired number of logic updates per second to perform. (Does not apply if the engine was prevented from managing its own timer at instantiation time.)
Parameters:
Name Type Description newLUPSTarget
number the desired number of logic updates per second
-
forceUpdateLogic()
-
Force a single logic update (even if the engine is in Paused state).
-
forceUpdateViews()
-
Force a single views update.
-
getG2D() → {CanvasRenderingContext2D|null}
-
Get the 2D rendering context which was instantiated at construction time.
Returns:
the 2D rendering context or null if none is available
- Type
- CanvasRenderingContext2D | null
-
getGL() → {WebGLRenderingContext|null}
-
Get the webgl rendering context which was instantiated at construction time.
Returns:
the webgl rendering context or null if none is available
- Type
- WebGLRenderingContext | null
-
getInputState() → {rlInputEvent.mouseKeyboardEvent}
-
Get the current mouse and keyboard input state (as known by the last processed input event).
Returns:
-
isPaused() → {boolean}
-
Get the Paused state.
Returns:
- Type
- boolean
-
manualTick()
-
Provided to be called by any external timer on tick (does nothing if engine is in Paused state). (Meant to be use by multiple engine instances sharing the same external timer.)
-
setCursorImage(newImage, cax, cay)
-
Set the image to use for rendering the mouse cursor while it is within the engines view area.
e.g.:myEngine.setCursorImage(rlCursors.getDefaultCursorImage(), 0, 0);
to set it back to the built-in default cursor.Parameters:
Name Type Description newImage
HTMLImageElement | string the image to display as the mouse cursor (or a string to set the cursor CSS property of the engines views)
cax
number cursor active x: horizontal-axis position of the cursor images active pixel
cay
number cursor active y: vertical-axis position of the cursor images active pixel
- See:
-
toggleDebug()
-
Toggle the debug state.
-
togglePause()
-
Toggle the Paused state. As long as an engine is paused, it will not call logic updates.
It will still gather input events and call view updates.
Type Definitions
-
callbackOnInputEvent(previousEvent, currentEvent)
-
Function signature for callbacks to use with
onInputEvent
.Parameters:
Name Type Description previousEvent
rlInputEvent.mouseKeyboardEvent currentEvent
rlInputEvent.mouseKeyboardEvent -
callbackOnUpdateLogic(LUTick)
-
Function signature for callbacks to use with
onUpdateLogic
.Parameters:
Name Type Description LUTick
number the logic update tick (increases by 1 with each logic update)
-
callbackOnUpdateViews(GL, G2D, time)
-
Function signature for callbacks to use with
onUpdateViews
.Parameters:
Name Type Description GL
WebGLRenderingContext a WebGLRenderingContext for the lower layer of the engines view
G2D
CanvasRenderingContext2D a CanvasRenderingContext2D for the upper layer of the engines view
time
DOMTimeStamp | DOMHighResTimeStamp a timestamp as provided by requestAnimationFrame