rlutil.h

Summary
rlutil.h
DescriptionThis file provides some useful utilities for console mode roguelike game development with C and C++.
Copyright© 2010 Tapio Vierros
LicensingSee License
Macros
RLUTIL_USE_ANSIDefine this to use ANSI escape sequences also on Windows (defaults to using WinAPI instead).
RLUTIL_STRING_TDefine/typedef this to your preference to override rlutil’s string type.
Functions
getchGet character without waiting for Return to be pressed.
kbhitDetermines if keyboard has been hit.
gotoxySame as rlutil.locate.
rlutilIn C++ all functions except getch, kbhit and gotoxy are arranged under namespace rlutil.
Macros
Internal typedefs and macros
Enumerations
Color codes
Constants
ANSI color strings
Key codes for keyhit()
Functions
getkeyReads a key press (blocking) and returns a key code.
nb_getchNon-blocking getch().
getANSIColorReturn ANSI color escape sequence for specified number 0-15.
setColorChange color specified by number (Windows / QBasic colors).
clsClears screen and moves cursor home.
locateSets the cursor position to 1-based x,y.
hidecursorHides the cursor.
showcursorShows the cursor.
msleepWaits given number of milliseconds before continuing.
trowsGet the number of rows in the terminal window or -1 on error.
tcolsGet the number of columns in the terminal window or -1 on error.
anykeyWaits until a key is pressed.
minReturns the lesser of the two arguments.
maxReturns the greater of the two arguments.
CursorHiderRAII OOP wrapper for rlutil.hidecursor.

Description

This file provides some useful utilities for console mode roguelike game development with C and C++.  It is aimed to be cross-platform (at least Windows and Linux).

Copyright

© 2010 Tapio Vierros

Licensing

Macros

RLUTIL_USE_ANSI

Define this to use ANSI escape sequences also on Windows (defaults to using WinAPI instead).

RLUTIL_STRING_T

Define/typedef this to your preference to override rlutil’s string type.

Defaults to std::string with C++ and char* with C.

Functions

getch

int getch(void)

Get character without waiting for Return to be pressed.  Windows has this in conio.h

kbhit

int kbhit(void)

Determines if keyboard has been hit.  Windows has this in conio.h

gotoxy

inline void gotoxy(int x,
int y)

Same as rlutil.locate.

rlutil

namespace rlutil

In C++ all functions except getch, kbhit and gotoxy are arranged under namespace rlutil.  That is because some platforms have them defined outside of rlutil.

Summary
Macros
Internal typedefs and macros
Enumerations
Color codes
Constants
ANSI color strings
Key codes for keyhit()
Functions
getkeyReads a key press (blocking) and returns a key code.
nb_getchNon-blocking getch().
getANSIColorReturn ANSI color escape sequence for specified number 0-15.
setColorChange color specified by number (Windows / QBasic colors).
clsClears screen and moves cursor home.
locateSets the cursor position to 1-based x,y.
hidecursorHides the cursor.
showcursorShows the cursor.
msleepWaits given number of milliseconds before continuing.
trowsGet the number of rows in the terminal window or -1 on error.
tcolsGet the number of columns in the terminal window or -1 on error.
anykeyWaits until a key is pressed.
minReturns the lesser of the two arguments.
maxReturns the greater of the two arguments.

Macros

Internal typedefs and macros

RLUTIL_STRING_TString type depending on which one of C or C++ is used
RLUTIL_PRINT(str)Printing macro independent of C/C++

Enumerations

Color codes

BLACKBlack
BLUEBlue
GREENGreen
CYANCyan
REDRed
MAGENTAMagenta / purple
BROWNBrown / dark yellow
GREYGrey / dark white
DARKGREYDark grey / light black
LIGHTBLUELight blue
LIGHTGREENLight green
LIGHTCYANLight cyan
LIGHTREDLight red
LIGHTMAGENTALight magenta / light purple
YELLOWYellow (bright)
WHITEWhite (bright)

Constants

ANSI color strings

ANSI_CLSClears screen
ANSI_BLACKBlack
ANSI_REDRed
ANSI_GREENGreen
ANSI_BROWNBrown / dark yellow
ANSI_BLUEBlue
ANSI_MAGENTAMagenta / purple
ANSI_CYANCyan
ANSI_GREYGrey / dark white
ANSI_DARKGREYDark grey / light black
ANSI_LIGHTREDLight red
ANSI_LIGHTGREENLight green
ANSI_YELLOWYellow (bright)
ANSI_LIGHTBLUELight blue
ANSI_LIGHTMAGENTALight magenta / light purple
ANSI_LIGHTCYANLight cyan
ANSI_WHITEWhite (bright)

Key codes for keyhit()

KEY_ESCAPEEscape
KEY_ENTEREnter
KEY_SPACESpace
KEY_INSERTInsert
KEY_HOMEHome
KEY_ENDEnd
KEY_DELETEDelete
KEY_PGUPPageUp
KEY_PGDOWNPageDown
KEY_UPUp arrow
KEY_DOWNDown arrow
KEY_LEFTLeft arrow
KEY_RIGHTRight arrow
KEY_F1F1
KEY_F2F2
KEY_F3F3
KEY_F4F4
KEY_F5F5
KEY_F6F6
KEY_F7F7
KEY_F8F8
KEY_F9F9
KEY_F10F10
KEY_F11F11
KEY_F12F12
KEY_NUMDELNumpad del
KEY_NUMPAD0Numpad 0
KEY_NUMPAD1Numpad 1
KEY_NUMPAD2Numpad 2
KEY_NUMPAD3Numpad 3
KEY_NUMPAD4Numpad 4
KEY_NUMPAD5Numpad 5
KEY_NUMPAD6Numpad 6
KEY_NUMPAD7Numpad 7
KEY_NUMPAD8Numpad 8
KEY_NUMPAD9Numpad 9

Functions

getkey

int getkey(void)

Reads a key press (blocking) and returns a key code.

See Key codes for keyhit()

Note

Only Arrows, Esc, Enter and Space are currently working properly.

nb_getch

inline int nb_getch(void)

Non-blocking getch().  Returns 0 if no key was pressed.

getANSIColor

RLUTIL_STRING_T getANSIColor(const int c)

Return ANSI color escape sequence for specified number 0-15.

See Color Codes

setColor

inline void setColor(int c)

Change color specified by number (Windows / QBasic colors).

See Color Codes

cls

inline void cls(void)

Clears screen and moves cursor home.

locate

void locate(int x,
int y)

Sets the cursor position to 1-based x,y.

hidecursor

inline void hidecursor(void)

Hides the cursor.

showcursor

inline void showcursor(void)

Shows the cursor.

msleep

inline void msleep(unsigned int ms)

Waits given number of milliseconds before continuing.

trows

int trows(void)

Get the number of rows in the terminal window or -1 on error.

tcols

int tcols(void)

Get the number of columns in the terminal window or -1 on error.

anykey

inline void anykey(void)

Waits until a key is pressed.

min

#ifdef __cplusplus template <class T> const T& min (const &a,
const &b)

Returns the lesser of the two arguments.

max

#ifdef __cplusplus template <class T> const T& max (const &a,
const &b)

Returns the greater of the two arguments.

CursorHider

struct CursorHider

RAII OOP wrapper for rlutil.hidecursor.  Hides the cursor and shows it again when the object goes out of scope.

int getch(void)
Get character without waiting for Return to be pressed.
int kbhit(void)
Determines if keyboard has been hit.
inline void gotoxy(int x,
int y)
Same as rlutil.locate.
void locate(int x,
int y)
Sets the cursor position to 1-based x,y.
namespace rlutil
In C++ all functions except getch, kbhit and gotoxy are arranged under namespace rlutil.
int getkey(void)
Reads a key press (blocking) and returns a key code.
inline int nb_getch(void)
Non-blocking getch().
RLUTIL_STRING_T getANSIColor(const int c)
Return ANSI color escape sequence for specified number 0-15.
inline void setColor(int c)
Change color specified by number (Windows / QBasic colors).
inline void cls(void)
Clears screen and moves cursor home.
inline void hidecursor(void)
Hides the cursor.
inline void showcursor(void)
Shows the cursor.
inline void msleep(unsigned int ms)
Waits given number of milliseconds before continuing.
int trows(void)
Get the number of rows in the terminal window or -1 on error.
int tcols(void)
Get the number of columns in the terminal window or -1 on error.
inline void anykey(void)
Waits until a key is pressed.
#ifdef __cplusplus template <class T> const T& min (const &a,
const &b)
Returns the lesser of the two arguments.
#ifdef __cplusplus template <class T> const T& max (const &a,
const &b)
Returns the greater of the two arguments.
struct CursorHider
RAII OOP wrapper for rlutil.hidecursor.
These are the color codes used by rlutil’s functions.
Close