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 and Constants
Color codes
ANSI escape 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.
getANSIBackgroundColorReturn ANSI background color escape sequence for specified number 0-15.
setColorChange color specified by number (Windows / QBasic colors).
setBackgroundColorChange background color specified by number (Windows / QBasic colors).
saveDefaultColorCall once to preserve colors for use in resetColor() on Windows without ANSI, no-op otherwise
resetColorReset color to default Requires a call to saveDefaultColor() to set the defaults
clsClears screen, resets all attributes and moves cursor home.
locateSets the cursor position to 1-based x,y.
setCharSets the character at the cursor without advancing the cursor
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

RLUTIL_INLINE int getch(void)

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

kbhit

RLUTIL_INLINE int kbhit(void)

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

gotoxy

RLUTIL_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 and Constants
Color codes
ANSI escape 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.
getANSIBackgroundColorReturn ANSI background color escape sequence for specified number 0-15.
setColorChange color specified by number (Windows / QBasic colors).
setBackgroundColorChange background color specified by number (Windows / QBasic colors).
saveDefaultColorCall once to preserve colors for use in resetColor() on Windows without ANSI, no-op otherwise
resetColorReset color to default Requires a call to saveDefaultColor() to set the defaults
clsClears screen, resets all attributes and moves cursor home.
locateSets the cursor position to 1-based x,y.
setCharSets the character at the cursor without advancing the cursor
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 and Constants

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)

ANSI escape strings

ANSI_CLSClears screen
ANSI_ATTRIBUTE_RESETResets all attributes
ANSI_CURSOR_HIDEHides the cursor
ANSI_CURSOR_SHOWShows the cursor
ANSI_CURSOR_HOMEMoves the cursor home (0,0)
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)
ANSI_BACKGROUND_BLACKBlack background
ANSI_BACKGROUND_REDRed background
ANSI_BACKGROUND_GREENGreen background
ANSI_BACKGROUND_YELLOWYellow background
ANSI_BACKGROUND_BLUEBlue background
ANSI_BACKGROUND_MAGENTAMagenta / purple background
ANSI_BACKGROUND_CYANCyan background
ANSI_BACKGROUND_WHITEWhite background

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

RLUTIL_INLINE 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

RLUTIL_INLINE int nb_getch(void)

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

getANSIColor

RLUTIL_INLINE RLUTIL_STRING_T getANSIColor(const int c)

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

See Color Codes

getANSIBackgroundColor

RLUTIL_INLINE RLUTIL_STRING_T getANSIBackgroundColor(const int c)

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

See Color Codes

setColor

RLUTIL_INLINE void setColor(int c)

Change color specified by number (Windows / QBasic colors).  Don’t change the background color

See Color Codes

setBackgroundColor

RLUTIL_INLINE void setBackgroundColor(int c)

Change background color specified by number (Windows / QBasic colors).  Don’t change the foreground color

See Color Codes

saveDefaultColor

RLUTIL_INLINE int saveDefaultColor()

Call once to preserve colors for use in resetColor() on Windows without ANSI, no-op otherwise

See Color Codes See resetColor

resetColor

RLUTIL_INLINE void resetColor()

Reset color to default Requires a call to saveDefaultColor() to set the defaults

See Color Codes See setColor See saveDefaultColor

cls

RLUTIL_INLINE void cls(void)

Clears screen, resets all attributes and moves cursor home.

locate

RLUTIL_INLINE void locate(int x,
int y)

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

setChar

RLUTIL_INLINE void setChar(char ch)

Sets the character at the cursor without advancing the cursor

hidecursor

RLUTIL_INLINE void hidecursor(void)

Hides the cursor.

showcursor

RLUTIL_INLINE void showcursor(void)

Shows the cursor.

msleep

RLUTIL_INLINE void msleep(unsigned int ms)

Waits given number of milliseconds before continuing.

trows

RLUTIL_INLINE int trows(void)

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

tcols

RLUTIL_INLINE int tcols(void)

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

anykey

#ifdef __cplusplus RLUTIL_INLINE void anykey()

Waits until a key is pressed.  In C++, it either takes no arguments or a template-type-argument-deduced argument.  In C, it takes a const char* representing the message to be displayed, or NULL for no message.

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.

RLUTIL_INLINE int getch(void)
Get character without waiting for Return to be pressed.
RLUTIL_INLINE int kbhit(void)
Determines if keyboard has been hit.
RLUTIL_INLINE void gotoxy(int x,
int y)
Same as rlutil.locate.
RLUTIL_INLINE 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.
RLUTIL_INLINE int getkey(void)
Reads a key press (blocking) and returns a key code.
RLUTIL_INLINE int nb_getch(void)
Non-blocking getch().
RLUTIL_INLINE RLUTIL_STRING_T getANSIColor(const int c)
Return ANSI color escape sequence for specified number 0-15.
RLUTIL_INLINE RLUTIL_STRING_T getANSIBackgroundColor(const int c)
Return ANSI background color escape sequence for specified number 0-15.
RLUTIL_INLINE void setColor(int c)
Change color specified by number (Windows / QBasic colors).
RLUTIL_INLINE void setBackgroundColor(int c)
Change background color specified by number (Windows / QBasic colors).
RLUTIL_INLINE int saveDefaultColor()
Call once to preserve colors for use in resetColor() on Windows without ANSI, no-op otherwise
RLUTIL_INLINE void resetColor()
Reset color to default Requires a call to saveDefaultColor() to set the defaults
RLUTIL_INLINE void cls(void)
Clears screen, resets all attributes and moves cursor home.
RLUTIL_INLINE void setChar(char ch)
Sets the character at the cursor without advancing the cursor
RLUTIL_INLINE void hidecursor(void)
Hides the cursor.
RLUTIL_INLINE void showcursor(void)
Shows the cursor.
RLUTIL_INLINE void msleep(unsigned int ms)
Waits given number of milliseconds before continuing.
RLUTIL_INLINE int trows(void)
Get the number of rows in the terminal window or -1 on error.
RLUTIL_INLINE int tcols(void)
Get the number of columns in the terminal window or -1 on error.
#ifdef __cplusplus RLUTIL_INLINE void anykey()
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