rogueutil
Cross-platform C/C++ utility for creating text-based user interfaces.
Macros | Typedefs | Enumerations | Functions | Variables
rogueutil.h File Reference

Cross-platform C/C++ utility for creating text-based user interfaces. More...

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <termios.h>
#include <unistd.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>

Go to the source code of this file.

Macros

#define RUTIL_USE_ANSI
 Define to use ANSI escape sequences instead of WinAPI on Windows.
 
#define RUTIL_STRING   char*
 Define to override rogueutil's string type. More...
 

Typedefs

typedef enum color_code color_code
 Provides easy color codes with similar numbers to QBasic.
 
typedef enum key_code key_code
 Provides keycodes for special keys.
 

Enumerations

enum  color_code {
  BLACK, BLUE, GREEN, CYAN,
  RED, MAGENTA, BROWN, GREY,
  DARKGREY, LIGHTBLUE, LIGHTGREEN, LIGHTCYAN,
  LIGHTRED, LIGHTMAGENTA, YELLOW, WHITE
}
 Provides easy color codes with similar numbers to QBasic.
 
enum  key_code {
  KEY_ESCAPE = 0, KEY_ENTER = 1, KEY_SPACE = 32, KEY_INSERT = 2,
  KEY_HOME = 3, KEY_PGUP = 4, KEY_DELETE = 5, KEY_END = 6,
  KEY_PGDOWN = 7, KEY_UP = 14, KEY_DOWN = 15, KEY_LEFT = 16,
  KEY_RIGHT = 17, KEY_F1 = 18, KEY_F2 = 19, KEY_F3 = 20,
  KEY_F4 = 21, KEY_F5 = 22, KEY_F6 = 23, KEY_F7 = 24,
  KEY_F8 = 25, KEY_F9 = 26, KEY_F10 = 27, KEY_F11 = 28,
  KEY_F12 = 29, KEY_NUMDEL = 30, KEY_NUMPAD0 = 31, KEY_NUMPAD1 = 127,
  KEY_NUMPAD2 = 128, KEY_NUMPAD3 = 129, KEY_NUMPAD4 = 130, KEY_NUMPAD5 = 131,
  KEY_NUMPAD6 = 132, KEY_NUMPAD7 = 133, KEY_NUMPAD8 = 134, KEY_NUMPAD9 = 135
}
 Provides keycodes for special keys.
 

Functions

void locate (int x, int y)
 Sets the cursor position to one defined by x and y.
 
int getch (void)
 Get a charater without waiting on a Return. More...
 
int kbhit (void)
 Determines if a button was pressed. More...
 
void gotoxy (int x, int y)
 Sets the cursor to the specified x and y position. More...
 
static void rutil_print (char *st)
 Printing wrapper independent of C/C++. More...
 
int getkey (void)
 Reads a key press (blocking) More...
 
int nb_getch (void)
 Non-blocking version of getch() More...
 
char * getANSIColor (const int c)
 Returns ANSI color escape sequence for specified number. More...
 
char * getANSIBgColor (const int c)
 Returns the ANSI background color escape sequence. More...
 
void setColor (int c)
 Changes color as specified by a number. More...
 
void setBackgroundColor (int c)
 Changes the background color as specified by a number. More...
 
int saveDefaultColor (void)
 Saves the color to use in resetColor() on Windows @detail Returns -1 if not on Windows or if RUTIL_USE_ANSI is defined.
 
void resetColor (void)
 Resets the color to one set by saveDefaultColor() More...
 
void cls (void)
 Clears screen, resets all attributes and moves cursor home.
 
void setString (char *str)
 Prints the supplied string without advancing the cursor.
 
void setChar (char ch)
 Sets the character at the cursor without advancing the cursor.
 
void setCursorVisibility (char visible)
 Shows/hides the cursor. More...
 
void hidecursor (void)
 Hides the cursor. More...
 
void showcursor (void)
 Shows the cursor. More...
 
void msleep (unsigned int ms)
 Pauses the program for a given number of milliseconds.
 
int trows (void)
 Returns the number of rows in the terminal window or -1 on error.
 
int tcols (void)
 Returns the number of columns in the terminal or -1 on error.
 
void anykey (char *msg)
 Waits until a key is pressed. More...
 
void setConsoleTitle (char *title)
 Sets the console title given a string.
 
void colorPrint (color_code color, color_code bgcolor, const char *fmt,...)
 Prints a message in a given color. More...
 
char * getUsername (void)
 Returns the username of the user running the program.
 
void printXY (int x, int y, char *msg)
 Print a message at a position given by x and y. More...
 

Variables

static const char * ANSI_CLS = "\033[2J\033[3J"
 
static const char * ANSI_CONSOLE_TITLE_PRE = "\033]0;"
 
static const char * ANSI_CONSOLE_TITLE_POST = "\007"
 
static const char * ANSI_ATTRIBUTE_RESET = "\033[0m"
 
static const char * ANSI_CURSOR_HIDE = "\033[?25l"
 
static const char * ANSI_CURSOR_SHOW = "\033[?25h"
 
static const char * ANSI_CURSOR_HOME = "\033[H"
 
static const char * ANSI_BLACK = "\033[22;30m"
 
static const char * ANSI_RED = "\033[22;31m"
 
static const char * ANSI_GREEN = "\033[22;32m"
 
static const char * ANSI_BROWN = "\033[22;33m"
 
static const char * ANSI_BLUE = "\033[22;34m"
 
static const char * ANSI_MAGENTA = "\033[22;35m"
 
static const char * ANSI_CYAN = "\033[22;36m"
 
static const char * ANSI_GREY = "\033[22;37m"
 
static const char * ANSI_DARKGREY = "\033[01;30m"
 
static const char * ANSI_LIGHTRED = "\033[01;31m"
 
static const char * ANSI_LIGHTGREEN = "\033[01;32m"
 
static const char * ANSI_YELLOW = "\033[01;33m"
 
static const char * ANSI_LIGHTBLUE = "\033[01;34m"
 
static const char * ANSI_LIGHTMAGENTA = "\033[01;35m"
 
static const char * ANSI_LIGHTCYAN = "\033[01;36m"
 
static const char * ANSI_WHITE = "\033[01;37m"
 
static const char * ANSI_BACKGROUND_BLACK = "\033[40m"
 
static const char * ANSI_BACKGROUND_RED = "\033[41m"
 
static const char * ANSI_BACKGROUND_GREEN = "\033[42m"
 
static const char * ANSI_BACKGROUND_YELLOW = "\033[43m"
 
static const char * ANSI_BACKGROUND_BLUE = "\033[44m"
 
static const char * ANSI_BACKGROUND_MAGENTA = "\033[45m"
 
static const char * ANSI_BACKGROUND_CYAN = "\033[46m"
 
static const char * ANSI_BACKGROUND_WHITE = "\033[47m"
 

Detailed Description

Cross-platform C/C++ utility for creating text-based user interfaces.

LICENSE

Copyright 2020 Sergei Akhmatdinov

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied *. See the License for the specific language governing permissions and limitations under the License.

Description

Rogueutil provides some useful functions for creating TUIs and console-based games, such as color printing and clearing the console. It can be used both as a C or C++ header file and it aims at being cross-platform between Windows and *nix.

Macro Definition Documentation

◆ RUTIL_STRING

#define RUTIL_STRING   char*

Define to override rogueutil's string type.

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

Function Documentation

◆ anykey()

void anykey ( char *  msg)

Waits until a key is pressed.

Parameters
msgThe message to display or NULL. Optional in C++.

◆ colorPrint()

void colorPrint ( color_code  color,
color_code  bgcolor,
const char *  fmt,
  ... 
)

Prints a message in a given color.

Parameters
fmtprintf-style formatted string to print in C or a list of objects in C++
colorForeground color to be used, use -1 to use the currently set foreground color
bgcolorBackground color to be used, use -1 to use the currently set background color
See also
color_code

◆ getANSIBgColor()

char* getANSIBgColor ( const int  c)

Returns the ANSI background color escape sequence.

Parameters
cNumber 0-15 corresponding to the color code
See also
color_code

◆ getANSIColor()

char* getANSIColor ( const int  c)

Returns ANSI color escape sequence for specified number.

Parameters
cNumber 0-15 corresponding to the color code
See also
color_code

◆ getch()

int getch ( void  )

Get a charater without waiting on a Return.

Windows has this functionality in conio.h

Returns
The character

◆ getkey()

int getkey ( void  )

Reads a key press (blocking)

At the moment, only Arrows, ESC, Enter and Space are working

Returns
Key code that was read

◆ gotoxy()

void gotoxy ( int  x,
int  y 
)

Sets the cursor to the specified x and y position.

Windows has this functionality in conio.h

See also
locate()

◆ hidecursor()

void hidecursor ( void  )

Hides the cursor.

See also
setCursorVisibility()

◆ kbhit()

int kbhit ( void  )

Determines if a button was pressed.

Windows has this in conio.h

Returns
Number of characters read

◆ nb_getch()

int nb_getch ( void  )

Non-blocking version of getch()

Returns
The character pressed or 0 if no key was pressed.
See also
getch()

◆ printXY()

void printXY ( int  x,
int  y,
char *  msg 
)

Print a message at a position given by x and y.

See also
locate()
rutil_print()

◆ resetColor()

void resetColor ( void  )

Resets the color to one set by saveDefaultColor()

See also
color_code
setColor()
saveDefaultColor()

◆ rutil_print()

static void rutil_print ( char *  st)
static

Printing wrapper independent of C/C++.

Parameters
stString to print
See also
colorPrint()
colorPrintBG()

◆ setBackgroundColor()

void setBackgroundColor ( int  c)

Changes the background color as specified by a number.

Parameters
cNumber 0-15 corresponding to a color code
See also
color_code

◆ setColor()

void setColor ( int  c)

Changes color as specified by a number.

Parameters
cNumber 0-15 corresponding to a color code
See also
color_code

◆ setCursorVisibility()

void setCursorVisibility ( char  visible)

Shows/hides the cursor.

Parameters
visible0 to hide the cursor, anything else to show the cursor

◆ showcursor()

void showcursor ( void  )

Shows the cursor.

See also
setCursorVisibility()