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

Engine component responsible for distributing user input. More...

#include <input.hpp>

Classes

struct  Callback
 
class  CallbackGroup
 Input callbacks creator and manager. More...
 
struct  Handler
 

Public Member Functions

auto Is (const std::string &stringKey) const -> bool
 Checks if input equals given string.
 
auto Is (char charKey) const -> bool
 Checks if input equals given character.
 
auto Bigger (char charKey) const -> bool
 Checks if given character is bigger than input.
 
auto Smaller (char charKey) const -> bool
 Checks if given character is smaller than input.
 
auto Between (char start, char end) const -> bool
 Checks if input is between range of characters.
 
auto GetInt () const -> uint8_t
 Get the first character of input as a 1-digit number.
 
void CallCallbacks ()
 Distribute accumulated inputs.
 

Public Attributes

std::string input
 Input for the last-called callback function.
 
std::string quitKey {"\x03"}
 Input that if received, breaks the input loop and sets Engine::running to false.
 

Friends

class Engine
 
class Output
 

Detailed Description

Engine component responsible for distributing user input.

Member Function Documentation

◆ Between()

auto KTech::Input::Between ( char start,
char end ) const -> bool
nodiscard

Checks if input is between range of characters.

Parameters
startStart of (ASCII) character range.
endEnd of (ASCII) character range.
Returns
True if between range (and Input::input is 1 character long), otherwise false.

◆ Bigger()

auto KTech::Input::Bigger ( char charKey) const -> bool
nodiscard

Checks if given character is bigger than input.

Parameters
charKeyCharacter to compare with Input::input.
Returns
True if bigger (and Input::input is 1 character long), otherwise false.

◆ CallCallbacks()

void KTech::Input::CallCallbacks ( )

Distribute accumulated inputs.

Input queues received inputs until this function is called. This very function is what calls your input callback functions.

Normally placed at the start of each game loop's iteration, with the other callback-calling functions of engine components (right now Memory::CallOnTicks() and Time::CallInvocations(), though this may change). For example:

// Game loop
while (engine.running)
{
// Call various callback-functions
engine.input.CallCallbacks(); // <- Distribute inputs to input callback functions
engine.memory.CallOnTicks();
// Graphics (render-on-demand)
{
map.Render();
engine.output.Print();
}
else if (engine.output.ShouldPrintThisTick())
{
engine.output.Print();
}
}
World structure that renders Objects of Layers, and exists within Map.
Definition camera.hpp:41
void Draw(Point position=Point(0, 0), UPoint start=UPoint(0, 0), UPoint end=UPoint(0, 0), uint8_t alpha=std::numeric_limits< uint8_t >::max())
Draw the rendered image (Camera::m_image) to Output so it can be printed to the terminal.
Definition camera.cpp:182
Output output
Output engine component.
Definition engine.hpp:49
bool running
States whether the game loop should be running or not.
Definition engine.hpp:43
Time time
Time engine component.
Definition engine.hpp:50
Memory memory
Memory engine component.
Definition engine.hpp:48
Input input
Input engine component.
Definition engine.hpp:47
void CallCallbacks()
Distribute accumulated inputs.
Definition input.cpp:159
void CallOnTicks()
Call the virtual OnTick() functions of all registered world structures.
Definition memory.cpp:51
void Print()
Print the internal image buffer.
Definition output.cpp:264
auto ShouldRenderThisTick() -> bool
Check whether things changed and require a new render.
Definition output.cpp:357
auto ShouldPrintThisTick() const -> bool
Check whether the terminal changed and requires a new print.
Definition output.cpp:407
void WaitUntilNextTick()
Sleeps and returns when the next tick should start.
Definition time.cpp:111
void CallInvocations()
Call callback functions of finished Invocations.
Definition time.cpp:51
See also
Memory::CallOnTicks()
Time::CallInvocations()

◆ GetInt()

auto KTech::Input::GetInt ( ) const -> uint8_t
nodiscard

Get the first character of input as a 1-digit number.

Returns
The first character of Input::input subtracted by 48 (the character '0'). Can return a value that is not 1-digit-long, so unless only digit characters ('0'-'9') can call your function, you should consider confirming with Input::Between() and the arguments ('0', '9') that input is indeed a digit.
See also
Input::Between()

◆ Is() [1/2]

auto KTech::Input::Is ( char charKey) const -> bool
nodiscard

Checks if input equals given character.

Parameters
charKeyCharacter to compare with Input::input.
Returns
True if equal (and Input::input is 1 character long), otherwise false.

◆ Is() [2/2]

auto KTech::Input::Is ( const std::string & stringKey) const -> bool
nodiscard

Checks if input equals given string.

Parameters
stringKeyString to compare with Input::input.
Returns
True if equal, otherwise false.

◆ Smaller()

auto KTech::Input::Smaller ( char charKey) const -> bool
nodiscard

Checks if given character is smaller than input.

Parameters
charKeyCharacter to compare with Input::input.
Returns
True if smaller (and Input::input is 1 character long), otherwise false.

Member Data Documentation

◆ input

std::string KTech::Input::input

Input for the last-called callback function.

Before Input calls your function, it will set this string to the exact input which lead to the calling of your function. It's especially useful if you have a function that can be triggered by different inputs, like a ranged callback function (created with CallbackGroup::RegisterRangedCallback()): use this variable to evaluate the actual user input.

◆ quitKey

std::string KTech::Input::quitKey {"\x03"}

Input that if received, breaks the input loop and sets Engine::running to false.

By default, it's "\x03" (Ctrl+C), which is a common quit key among terminal applications. You may change it, but don't go Vim on your players.


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