KTech 1.1.0
C++ 2D terminal game engine library
Loading...
Searching...
No Matches
KTech::Engine Class Reference

Complete engine containing all engine components. More...

#include <engine.hpp>

Public Member Functions

 Engine (UPoint imageSize, size_t ticksPerSecondLimit=24, bool noGameLoopMode=false)
 Construct the engine components.
 
void Quit ()
 Set Engine::running to false.
 

Public Attributes

bool running = true
 States whether the game loop should be running or not.
 
const bool noGameLoopMode
 States whether the engine is in "no-game-loop mode" or not.
 
Collision collision
 Collision engine component.
 
Input input
 Input engine component.
 
Memory memory
 Memory engine component.
 
Output output
 Output engine component.
 
Time time
 Time engine component.
 

Detailed Description

Complete engine containing all engine components.

You need an Engine instance to do nearly everything in KTech (including creating world structures). Normally, you should have 1 instance of this class in your game. Make sure you don't accidentally create duplicate instances, as those could lead to unexpected behavior, especially in the Input engine componenet.

However, the need for multiple instances of Engine in the same process is probable. For example, you might want 1 process managing multiple Engines (that were made headless, or that their Input and Output componenets were redirected), each handling a lobby in a server. Who knows.

Constructor & Destructor Documentation

◆ Engine()

KTech::Engine::Engine ( UPoint imageSize,
size_t ticksPerSecondLimit = 24,
bool noGameLoopMode = false )

Construct the engine components.

The engine components are your game's dependencies. For example, without Memory, you don't have anywhere to register your world structures at.

As part of that, a couple of noticeable things will happen to the terminal on construction (unless noGameLoopMode is set to true):

  • Input will set some terminal settings, and start waiting for user inputs.
  • Output will move to an alternative terminal buffer, leaving the terminal empty until it starts printing images.

Input and Output also correctly revert these changes in their destructors, as should a terminal application do.

Parameters
imageSizeThe size of the game's viewport.
ticksPerSecondLimitThe rate at which your game loop should iterate.
noGameLoopModefalse: normal IO behavior. true: change some IO behavior so it's possible to write a test program with a game loop. See Engine::noGameLoopMode.
See also
Engine::noGameLoopMode

Member Function Documentation

◆ Quit()

void KTech::Engine::Quit ( )

Set Engine::running to false.

Simply sets Engine::running to false (signifying the game loop should break). Use this if you need a function to do this operation (e.g., to provide as a callback function), and don't want to write one yourself.

Member Data Documentation

◆ noGameLoopMode

const bool KTech::Engine::noGameLoopMode

States whether the engine is in "no-game-loop mode" or not.

You can optionally set this mode to true in Engine::Engine().

"No-game-loop mode" changes some of the behavior of the Input and Output engine components in order to facilitate writing a test program. For example, if you want to isolate testing of Textures, Object movement, etc., in order to see how these features work, you can use this mode.

This is what changes when no-game-loop mode is enabled (jargon warning):

  • Doesn't start input-loop thread.
  • Doesn't disable canonical mode (input will be received after the user enters a new line).
  • Doesn't disable echo (user input will be displayed while being typed).
  • Doesn't disable signals (Ctrl+C, Ctrl+D, etc., will work normally).
  • Doesn't reset cursor position on each Output::Print() (images will be printed bellow each other).
  • Doesn't enable alternative buffer.
  • In Windows exclusively:
    • Doesn't enable input as escape sequences.
    • Doesn't disable selecting text.
    • Doesn't switch to us-ascii input (code page 20127).

The most significant change is the input-loop thread not starting, which is the thread that collects user input for later distribution by Input::CallCallbacks(): this change allows you to wait for user input yourself with something like std::cin. The rest of the changes mean the terminal will behave like a shell rather than a game. All of this allows you to write a test program without the burden of a game loop. Tutorial chapter 5 is an example of such program that enables this mode and doesn't use a game loop paradigm.

See also
Engine::Engine()
Tutorial chapter 5

◆ running

bool KTech::Engine::running = true

States whether the game loop should be running or not.

Your game loop should continue iterating as long as this variable is true, and should exit if it's false. For example:

// Game loop
while (engine.running)
{
// Game loop iteration ...
}
See also
Engine::Quit()

The documentation for this class was generated from the following files: