Chapter 04. Windowing

Table of Contents

Windowing

The built-in window in tilengine provides a basic, easy to setup environment for quick test and evaluation. It's a very simple environment to ease prototyping but is not intended to be used in production environment.

Features

Single threaded window

The single threaded window runs inside the main thread and must be handled inside the game loop for each frame. The window is created with TLN_CreateWindow. With default parameters it creates a window with an integer scaling as large as possible for the desktop resolution, and with CRT emulation effect enabled:

TLN_CreateWindow (NULL, 0);

Once the window is created, it must be processed calling TLN_ProcessWindow for each frame. This function returns true while the window is alive, or false when the user has requested to terminate it. Draw the frames with TLN_DrawFrame. This function takes an optional integer value, that represents a timestamp used by the animation engine. If there are not animations it can be left as 0.

This basic sample show how to initialize the engine, create the window and do the window loop until the user requests to exit:

int frame = 0;
TLN_Init (400,240, 2,8,0); /* init the engine */
TLN_CreateWindow (NULL, 0); /* create the window */
while (TLN_ProcessWindow()) /* the window loop */
{
TLN_DrawFrame (frame); /* draw next frame */
frame += 1;
}
TLN_Deinit (); /* release resources */

Multi-threaded window

The multi-threaded window spans its own thread and runs in parallel, without a window loop. It's mainly used for interactive use within an editor as in python. The multi-threaded window is created with TLN_CreateWindowThread and doesn't require continuous calls to TLN_ProcessWindow or TLN_DrawFrame. Instead you query its active state with TLN_IsWindowActive, and optionally you can sync to it with TLN_WaitRedraw.

This is the same sample with the multi-threaded window:

TLN_Init (400,240, 2,8,0); /* init the engine */
TLN_CreateWindowThread (NULL, 0); /* create the window */
while (TLN_IsWindowActive()) /* check window state */
{
TLN_WaitRedraw (); /* optionally sync to window drawing for fps control */
}
TLN_Deinit (); /* release resources */

User input

User input in tilengine simulates a basic arcade setup, with for directions and four action buttons. It can be controlled with keyboard or joystick/gamepad:

To check the state of an input, call TLN_GetInput with one of the possible TLN_Input values. For example, to check if left arrow is pushed do this:

{
/* do your treatment */
}

Time & delay

Tilengine window provides some basic timing functions. TLN_GetTicks returns the number of milliseconds elapsed since system started, and TLN_Delay pauses execution for the given amount of milliseconds.

The CRT effect

All low resolution, pixel art games were played on CRT displays that provided its unique texture and feel. Trying to watch this type of games in crystal-clear, big square pixels, is just wrong. The CRT effect simulates more or less faithfully the characteristics of a CRT display: visible RGB strips, horizontal blur, bright pixel bloom...

Plain output without CRT:

CRT disabled

The same output but with default CRT enabled:

CRT enabled

By default the CRT effect is enabled when the window is created, but it can be disabled with the TLN_DisableCRTEffect function. It can be toggled pressing the Escape key, too.

The effect is highly configurable with the TLN_EnableCRTEffect function. It takes many parameters to customize its appearance and strength. The default parameters are:

TLN_EnableCRTEffect (TLN_OVERLAY_APERTURE, 128, 192, 0,64, 64,128, false, 255);