System layer

Time functions

uint32_t TCOD_sys_elapsed_milli(void)
float TCOD_sys_elapsed_seconds(void)
void TCOD_sys_sleep_milli(uint32_t val)
void TCOD_sys_set_fps(int val)
int TCOD_sys_get_fps(void)
float TCOD_sys_get_last_frame_length(void)

Easy screenshots

void TCOD_sys_save_screenshot(const char *filename)

Miscellaneous utilities

struct SDL_Window *TCOD_sys_get_sdl_window(void)

Return an SDL_Window pointer if one is in use, returns NULL otherwise.

New in version 1.11.

struct SDL_Renderer *TCOD_sys_get_sdl_renderer(void)

Return an SDL_Renderer pointer if one is in use, returns NULL otherwise.

New in version 1.11.

C++

class TCODSystem

Public Static Functions

void setFps(int val)

system_time system High precision time functions These are functions specifically aimed at real time game development.

Limit the frames per second The setFps function allows you to limit the number of frames per second. If a frame is rendered faster than expected, the TCOD_console_flush function will wait so that the frame rate never exceed this value. You can call this function during your game initialization. You can dynamically change the frame rate. Just call this function once again. You should always limit the frame rate, except during benchmarks, else your game will use 100% of the CPU power static void TCODSystem::setFps(int val) void TCOD_sys_set_fps(int val) sys_set_fps(val) # static void TCODSystem::setFps(int val) tcod.system.setFps(val) val Maximum number of frames per second. 0 means unlimited frame rate.

int getFps()

system_time Get the number of frames rendered during the last second The value returned by this function is updated every second.

static int TCODSystem::getFps() int TCOD_sys_get_fps() sys_get_fps() # static int TCODSystem::getFps() tcod.system.getFps()

float getLastFrameLength()

system_time Get the duration of the last frame This function returns the length in seconds of the last rendered frame.

You can use this value to update every time dependent object in the world. static float TCODSystem::getLastFrameLength() float TCOD_sys_get_last_frame_length() sys_get_last_frame_length() # static float TCODSystem::getLastFrameLength() tcod.system.getLastFrameLength() moving an objet at 5 console cells per second float x=0,y=0; // object coordinates x += 5 * TCODSystem::getLastFrameLength(); TCODConsole::root->putChar((int)(x),(int)(y),’X’); float x=0,y=0; x += 5 * TCOD_sys_get_last_frame_length(); TCOD_console_put_char(NULL,(int)(x),(int)(y),’X’); x=0.0 y=0.0 x += 5 * libtcod.sys_get_last_frame_length() libtcod.console_put_char(0,int(x),int(y),’X’) moving an objet at 5 console cells per second x=0 y=0 object coordinates x = x + 5 * tcod.system.getLastFrameLength() libtcod.TCODConsole_root:putChar(x,y,’X’)

void sleepMilli(uint32_t val)

system_time Pause the program Use this function to stop the program execution for a specified number of milliseconds.

static void TCODSystem::sleepMilli(uint32_t val) void TCOD_sys_sleep_milli(uint32_t val) sys_sleep_milli(val) # static void TCODSystem::sleepMilli(uint val) tcod.system.sleepMilli(val) val number of milliseconds before the function returns

uint32_t getElapsedMilli()

system_time Get global timer in milliseconds This function returns the number of milliseconds since the program has started.

static uint32_t TCODSystem::getElapsedMilli() uint32_t TCOD_sys_elapsed_milli() sys_elapsed_milli() # static uint TCODSystem::getElapsedMilli() tcod.system.getElapsedMilli()

float getElapsedSeconds()

system_time Get global timer in seconds This function returns the number of seconds since the program has started.

static float TCODSystem::getElapsedSeconds() float TCOD_sys_elapsed_seconds() sys_elapsed_seconds() # static float TCODSystem::getElapsedSeconds() tcod.system.getElapsedSeconds()

TCOD_event_t waitForEvent(int eventMask, TCOD_key_t *key, TCOD_mouse_t *mouse, bool flush)

console_blocking_input Waiting for any event (mouse or keyboard) This function waits for an event from the user.

The eventMask shows what events we’re waiting for. The return value indicate what event was actually triggered. Values in key and mouse structures are updated accordingly. If flush is false, the function waits only if there are no pending events, else it returns the first event in the buffer. typedef enum { TCOD_EVENT_NONE=0, TCOD_EVENT_KEY_PRESS=1, TCOD_EVENT_KEY_RELEASE=2, TCOD_EVENT_KEY=TCOD_EVENT_KEY_PRESS|TCOD_EVENT_KEY_RELEASE, TCOD_EVENT_MOUSE_MOVE=4, TCOD_EVENT_MOUSE_PRESS=8, TCOD_EVENT_MOUSE_RELEASE=16, TCOD_EVENT_MOUSE=TCOD_EVENT_MOUSE_MOVE|TCOD_EVENT_MOUSE_PRESS|TCOD_EVENT_MOUSE_RELEASE, TCOD_EVENT_ANY=TCOD_EVENT_KEY|TCOD_EVENT_MOUSE, } TCOD_event_t; static TCOD_event_t TCODSystem::waitForEvent(int eventMask, TCOD_key_t *key, TCOD_mouse_t *mouse, bool flush) TCOD_event_t TCOD_sys_wait_for_event(int eventMask, TCOD_key_t *key, TCOD_mouse_t *mouse, bool flush) sys_wait_for_event(eventMask,key,mouse,flush) eventMask event types to wait for (other types are discarded) key updated in case of a key event. Can be null if eventMask contains no key event type mouse updated in case of a mouse event. Can be null if eventMask contains no mouse event type flush if true, all pending events are flushed from the buffer. Else, return the first available event TCOD_key_t key; TCOD_mouse_t mouse; TCOD_event_t ev = TCODSystem::waitForEvent(TCOD_EVENT_ANY,&key,&mouse,true); if ( ev == TCOD_EVENT_KEY_PRESS && key.c == ‘i’ ) { … open inventory … } TCOD_key_t key; TCOD_mouse_t mouse; TCOD_event_t ev = TCOD_sys_wait_for_event(TCOD_EVENT_ANY,&key,&mouse,true); if ( ev == TCOD_EVENT_KEY_PRESS && key.c == ‘i’ ) { … open inventory … }

TCOD_event_t checkForEvent(int eventMask, TCOD_key_t *key, TCOD_mouse_t *mouse)

console_non_blocking_input Checking for any event (mouse or keyboard) This function checks if an event from the user is in the buffer.

The eventMask shows what events we’re waiting for. The return value indicate what event was actually found. Values in key and mouse structures are updated accordingly. typedef enum { TCOD_EVENT_KEY_PRESS=1, TCOD_EVENT_KEY_RELEASE=2, TCOD_EVENT_KEY=TCOD_EVENT_KEY_PRESS|TCOD_EVENT_KEY_RELEASE, TCOD_EVENT_MOUSE_MOVE=4, TCOD_EVENT_MOUSE_PRESS=8, TCOD_EVENT_MOUSE_RELEASE=16, TCOD_EVENT_MOUSE=TCOD_EVENT_MOUSE_MOVE|TCOD_EVENT_MOUSE_PRESS|TCOD_EVENT_MOUSE_RELEASE, TCOD_EVENT_ANY=TCOD_EVENT_KEY|TCOD_EVENT_MOUSE, } TCOD_event_t; static TCOD_event_t TCODSystem::checkForEvent(int eventMask, TCOD_key_t *key, TCOD_mouse_t *mouse) TCOD_event_t TCOD_sys_check_for_event(int eventMask, TCOD_key_t *key, TCOD_mouse_t *mouse) sys_check_for_event(eventMask,key,mouse) eventMask event types to wait for (other types are discarded) key updated in case of a key event. Can be null if eventMask contains no key event type mouse updated in case of a mouse event. Can be null if eventMask contains no mouse event type TCOD_key_t key; TCOD_mouse_t mouse; TCOD_event_t ev = TCODSystem::checkForEvent(TCOD_EVENT_ANY,&key,&mouse); if ( ev == TCOD_EVENT_KEY_PRESS && key.c == ‘i’ ) { … open inventory … } TCOD_key_t key; TCOD_mouse_t mouse; TCOD_event_t ev = TCOD_sys_check_for_event(TCOD_EVENT_ANY,&key,&mouse); if ( ev == TCOD_EVENT_KEY_PRESS && key.c == ‘i’ ) { … open inventory … }

void saveScreenshot(const char *filename)

system_screenshots system Easy screenshots This function allows you to save the current game screen in a png file, or possibly a bmp file if you provide a filename ending with .bmp.

static void TCODSystem::saveScreenshot(const char *filename) void TCOD_sys_save_screenshot(const char *filename) sys_save_screenshot(filename) # static void TCODSystem::saveScreenshot(string filename); tcod.system.saveScreenshot(filename) filename Name of the file. If NULL, a filename is automatically generated with the form “./screenshotNNN.png”, NNN being the first free number (if a file named screenshot000.png already exist, screenshot001.png will be used, and so on…).

bool createDirectory(const char *path)

system_filesystem system Filesystem utilities Those are a few function that cannot be easily implemented in a portable way in C/C++.

They have no Python wrapper since Python provides its own builtin functions. All those functions return false if an error occurred. Create a directory static bool TCODSystem::createDirectory(const char *path) bool TCOD_sys_create_directory(const char *path) path Directory path. The immediate father directory (<path>/..) must exist and be writable.

bool deleteDirectory(const char *path)

system_filesystem Delete an empty directory static bool TCODSystem::deleteDirectory(const char *path) bool TCOD_sys_delete_directory(const char *path) path directory path.

This directory must exist, be writable and empty

bool deleteFile(const char *path)

system_filesystem Delete a file static bool TCODSystem::deleteFile(const char *path) bool TCOD_sys_delete_file(const char *path) path File path.

This file must exist and be writable.

bool isDirectory(const char *path)

system_filesystem Check if a path is a directory static bool TCODSystem::isDirectory(const char *path) bool TCOD_sys_is_directory(const char *path) path a path to check

TCOD_list_t getDirectoryContent(const char *path, const char *pattern)

system_filesystem List files in a directory To get the list of entries in a directory (including sub-directories, except .

and ..). The returned list is allocated by the function and must be deleted by you. All the const char * inside must be also freed with TCODList::clearAndDelete. static TCODList TCODSystem::getDirectoryContent(const char *path, const char *pattern) TCOD_list_t TCOD_sys_get_directory_content(const char *path) path a directory pattern If NULL or empty, returns all directory entries. Else returns only entries matching the pattern. The pattern is NOT a regular expression. It can only handle one ‘*’ wildcard. Examples : *.png, saveGame*, font*.png

bool fileExists(const char *filename, ...)

system_filesystem Check if a given file exists In order to check whether a given file exists in the filesystem.

Useful for detecting errors caused by missing files. static bool TCODSystem::fileExists(const char *filename, …) bool TCOD_sys_file_exists(const char * filename, …) filename the file name, using printf-like formatting … optional arguments for filename formatting if (!TCODSystem::fileExists(“myfile.%s”,”txt”)) { fprintf(stderr,”no such file!”); } if (!TCOD_sys_file_exists(“myfile.%s”,”txt”)) { fprintf(stderr,”no such file!”); }

bool readFile(const char *filename, unsigned char **buf, size_t *size)

system_filesystem Read the content of a file into memory This is a portable function to read the content of a file from disk or from the application apk (android).

buf must be freed with free(buf). static bool TCODSystem::readFile(const char *filename, unsigned char **buf, uint32_t *size) bool TCOD_sys_read_file(const char *filename, unsigned char **buf, uint32_t *size) filename the file name buf a buffer to be allocated and filled with the file content size the size of the allocated buffer. unsigned char *buf; uint32_t size; if (TCODSystem::readFile(“myfile.dat”,&buf,&size)) { do something with buf free(buf); } if (TCOD_sys_read_file(“myfile.dat”,&buf,&size)) { do something with buf free(buf); }

bool writeFile(const char *filename, unsigned char *buf, uint32_t size)

system_filesystem Write the content of a memory buffer to a file This is a portable function to write some data to a file.

static bool TCODSystem::writeFile(const char *filename, unsigned char *buf, uint32_t size) bool TCOD_sys_write_file(const char *filename, unsigned char *buf, uint32_t size) filename the file name buf a buffer containing the data to write size the number of bytes to write. TCODSystem::writeFile(“myfile.dat”,buf,size)); TCOD_sys_write_file(“myfile.dat”,buf,size));

void registerSDLRenderer(ITCODSDLRenderer *renderer)

system_sdlcbk system Draw custom graphics on top of the root console You can register a callback that will be called after the libtcod rendering phase, but before the screen buffer is swapped.

This callback receives the screen SDL_Surface reference. This makes it possible to use any SDL drawing functions (including openGL) on top of the libtcod console. Render custom graphics To disable the custom renderer, call the same method with a NULL parameter. Note that to keep libtcod from requiring the SDL headers, the callback parameter is a void pointer. You have to include SDL headers and cast it to SDL_Surface in your code. class TCODLIB_API ITCODSDLRenderer { public : virtual void render(void *sdlSurface) = 0; }; static void TCODSystem::registerSDLRenderer(ITCODSDLRenderer *callback); typedef void (*SDL_renderer_t) (void *sdl_surface); void TCOD_sys_register_SDL_renderer(SDL_renderer_t callback) def renderer ( sdl_surface ) : … TCOD_sys_register_SDL_renderer( callback ) callback The renderer to call before swapping the screen buffer. If NULL, custom rendering is disabled class MyRenderer : public ITCODSDLRenderer { public : void render(void *sdlSurface) { SDL_Surface *s = (SDL_Surface *)sdlSurface; … draw something on s } }; TCODSystem::registerSDLRenderer(new MyRenderer()); void my_renderer( void *sdl_surface ) { SDL_Surface *s = (SDL_Surface *)sdl_surface; … draw something on s } TCOD_sys_register_SDL_renderer(my_renderer); def my_renderer(sdl_surface) : … draw something on sdl_surface using pygame libtcod.sys_register_SDL_renderer(my_renderer)

void forceFullscreenResolution(int width, int height)

system_sdlcbk Managing screen redraw libtcod is not aware of the part of the screen your SDL renderer has updated.

If no change occurred in the console, it won’t redraw them except if you tell him to do so with this function void TCODConsole::setDirty(int x, int y, int w, int h) void TCOD_console_set_dirty(int x, int y, int w, int h) TCOD_console_set_dirty(x, y, w, h) x,y,w,h Part of the root console you want to redraw even if nothing has changed in the console back/fore/char. system_misc system Miscellaneous utilities Using a custom resolution for the fullscreen mode This function allows you to force the use of a specific resolution in fullscreen mode. The default resolution depends on the root console size and the font character size. static void TCODSystem::forceFullscreenResolution(int width, int height) void TCOD_sys_force_fullscreen_resolution(int width, int height) sys_force_fullscreen_resolution(width, height) # static void TCODSystem::forceFullscreenResolution(int width, int height); tcod.system.forceFullscreenResolution(width,height) width,height Resolution to use when switching to fullscreen. Will use the smallest available resolution so that : resolution width >= width and resolution width >= root console width * font char width resolution width >= height and resolution height >= root console height * font char height TCODSystem::forceFullscreenResolution(800,600); // use 800x600 in fullscreen instead of 640x400 TCODConsole::initRoot(80,50,”“,true); // 80x50 console with 8x8 char => 640x400 default resolution TCOD_sys_force_fullscreen_resolution(800,600); TCOD_console_init_root(80,50,”“,true); libtcod.sys_force_fullscreen_resolution(800,600) libtcod.console_init_root(80,50,”“,True) tcod.system.forceFullscreenResolution(800,600) use 800x600 in fullscreen instead of 640x400 tcod.console.initRoot(80,50,”“,true) 80x50 console with 8x8 char => 640x400 default resolution

void getCurrentResolution(int *w, int *h)

system_misc Get current resolution You can get the current screen resolution with getCurrentResolution.

You can use it for example to get the desktop resolution before initializing the root console. static void TCODSystem::getCurrentResolution(int *width, int *height) void TCOD_sys_get_current_resolution(int *width, int *height) sys_get_current_resolution() # returns w,h # static void TCODSystem::getCurrentResolution(out int w, out int h); width,height contains current resolution when the function returns

void getFullscreenOffsets(int *offx, int *offy)

system_misc Get fullscreen offset If the fullscreen resolution does not matches the console size in pixels, black borders are added.

This function returns the position in pixels of the console top left corner in the screen. static void TCODSystem::getFullscreenOffsets(int *offx, int *offy) void TCOD_sys_get_fullscreen_offsets(int *offx, int *offy) # static void TCODSystem::getFullscreenOffsets(out int offx, out int offy); offx,offy contains the position of the console on the screen when using fullscreen mode.

void getCharSize(int *w, int *h)

system_misc Get the font size You can get the size of the characters in the font static void TCODSystem::getCharSize(int *width, int *height) void TCOD_sys_get_char_size(int *width, int *height) sys_get_char_size() # returns w,h # static void TCODSystem::getCharSize(out int w, out int h); width,height contains a character size when the function returns

void updateChar(int asciiCode, int fontx, int fonty, const TCODImage *img, int x, int y)

system_misc Dynamically updating the font bitmap You can dynamically change the bitmap of a character in the font.

All cells using this ascii code will be updated at next flush call. static void TCODSystem::updateChar(int asciiCode, int fontx, int fonty,const TCODImage *img,int x,int y) void TCOD_sys_update_char(int asciiCode, int fontx, int fonty, TCOD_image_t img, int x, int y) sys_update_char(asciiCode,fontx,fonty,img,x,y) asciiCode ascii code corresponding to the character to update fontx,fonty coordinate of the character in the bitmap font (in characters, not pixels) img image containing the new character bitmap x,y position in pixels of the top-left corner of the character in the image

void setRenderer(TCOD_renderer_t renderer)

system_misc Dynamically change libtcod’s internal renderer As of 1.5.1, libtcod contains 3 different renderers : SDL : historic libtcod renderer.

Should work and be pretty fast everywhere OpenGL : requires OpenGL compatible video card. Might be much faster or much slower than SDL, depending on the drivers GLSDL : requires OpenGL 1.4 compatible video card with GL_ARB_shader_objects extension. Blazing fast if you have the proper hardware and drivers. This function switches the current renderer dynamically. static void TCODSystem::setRenderer(TCOD_renderer_t renderer) void TCOD_sys_set_renderer(TCOD_renderer_t renderer) sys_set_renderer(renderer) # static void TCODSystem::setRenderer(TCODRendererType renderer); renderer Either TCOD_RENDERER_GLSL, TCOD_RENDERER_OPENGL or TCOD_RENDERER_SDL

TCOD_renderer_t getRenderer()

system_misc Get the current internal renderer static TCOD_renderer_t TCODSystem::getRenderer() TCOD_renderer_t TCOD_sys_get_renderer() sys_get_renderer() # static TCODRendererType TCODSystem::getRenderer();

bool setClipboard(const char *value)

system_clipboard Clipboard integration With these functions, you can copy data in your operating system’s clipboard from the game or retrieve data from the clipboard.

system Set current clipboard contents Takes UTF-8 text and copies it into the system clipboard. On Linux, because an application cannot access the system clipboard unless a window is open, if no window is open the call will do nothing. static bool TCODSystem::setClipboard(const char *value) bool TCOD_sys_clipboard_set(const char *value) sys_clipboard_set(value) value UTF-8 text to copy into the clipboard

char *getClipboard()

system_clipboard Get current clipboard contents Returns the UTF-8 text currently in the system clipboard.

On Linux, because an application cannot access the system clipboard unless a window is open, if no window is open an empty string will be returned. For C and C++, note that the pointer is borrowed, and libtcod will take care of freeing the memory. static char *TCODSystem::getClipboard() char *TCOD_sys_clipboard_get() sys_clipboard_get() # Returns UTF-8 string