Class TCODSystem

Class Documentation

class TCODSystem

Public Static Functions

static void setFps(int val)

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

These are functions specifically aimed at real time game development.

Parameters

val – Maximum number of frames per second. 0 means unlimited frame rate.

static int getFps()

The value returned by this function is updated every second.

static float getLastFrameLength()

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.

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’)

static void sleepMilli(uint32_t val)

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

Parameters

val – number of milliseconds before the function returns

static uint32_t getElapsedMilli()

This function returns the number of milliseconds since the program has started.

static float getElapsedSeconds()

This function returns the number of seconds since the program has started.

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

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.

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_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 … }

Parameters
  • 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

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

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.

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_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 … }

Parameters
  • 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

static void saveScreenshot(const char *filename)

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.

Parameters

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…).

static bool createDirectory(const char *path)

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.

Parameters

path – Directory path. The immediate father directory (<path>/..) must exist and be writable.

static bool deleteDirectory(const char *path)
Parameters

path – directory path. This directory must exist, be writable and empty

static bool deleteFile(const char *path)
Parameters

path – File path. This file must exist and be writable.

static bool isDirectory(const char *path)
Parameters

path – a path to check

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

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.

Parameters
  • 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

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

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

Useful for detecting errors caused by missing files.

if (!TCODSystem::fileExists(“myfile.%s”,”txt”)) { fprintf(stderr,”no such file!”); }

if (!TCOD_sys_file_exists(“myfile.%s”,”txt”)) { fprintf(stderr,”no such file!”); }

Parameters
  • filename – the file name, using printf-like formatting

  • ... – optional arguments for filename formatting

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

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).

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); }

Parameters
  • filename – the file name

  • buf – a buffer to be allocated and filled with the file content

  • size – the size of the allocated buffer.

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

This is a portable function to write some data to a file.

TCODSystem::writeFile(“myfile.dat”,buf,size));

TCOD_sys_write_file(“myfile.dat”,buf,size));

Parameters
  • filename – the file name

  • buf – a buffer containing the data to write

  • size – the number of bytes to write.

static void registerSDLRenderer(ITCODSDLRenderer *renderer)

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 )

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.

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)

Parameters

callback – The renderer to call before swapping the screen buffer. If NULL, custom rendering is disabled

static void forceFullscreenResolution(int width, int height)

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

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.

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) &#8212; use 800x600 in fullscreen instead of 640x400 tcod.console.initRoot(80,50,””,true) &#8212; 80x50 console with 8x8 char => 640x400 default resolution

Parameters
  • x, y, w, h – Part of the root console you want to redraw even if nothing has changed in the console back/fore/char.

  • 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

static void getCurrentResolution(int *width, int *height)

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.

Parameters

width, height – contains current resolution when the function returns

static void getFullscreenOffsets(int *offset_x, int *offset_y)

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.

Parameters

offset_x, offset_y – contains the position of the console on the screen when using fullscreen mode.

static void getCharSize(int *w, int *h)

You can get the size of the characters in the font.

Parameters

width, height – contains a character size when the function returns

static void updateChar(int asciiCode, int font_x, int font_y, const TCODImage *img, int x, int y)

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.

Parameters
  • asciiCode – ascii code corresponding to the character to update

  • font_x, font_y – 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

static void setRenderer(TCOD_renderer_t 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.

Parameters

renderer – Either TCOD_RENDERER_GLSL, TCOD_RENDERER_OPENGL or TCOD_RENDERER_SDL

static TCOD_renderer_t getRenderer()
static bool setClipboard(const char *value)

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.

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

Parameters

value – UTF-8 text to copy into the clipboard

static char *getClipboard()

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 int getNumCores()
static TCOD_thread_t newThread(int (*func)(void*), void *data)
static void deleteThread(TCOD_thread_t th)
static void waitThread(TCOD_thread_t th)
static TCOD_mutex_t newMutex()
static void mutexIn(TCOD_mutex_t mut)
static void mutexOut(TCOD_mutex_t mut)
static void deleteMutex(TCOD_mutex_t mut)
static TCOD_semaphore_t newSemaphore(int initVal)
static void lockSemaphore(TCOD_semaphore_t sem)
static void unlockSemaphore(TCOD_semaphore_t sem)
static void deleteSemaphore(TCOD_semaphore_t sem)
static TCOD_cond_t newCondition()
static void signalCondition(TCOD_cond_t sem)
static void broadcastCondition(TCOD_cond_t sem)
static void waitCondition(TCOD_cond_t sem, TCOD_mutex_t mut)
static void deleteCondition(TCOD_cond_t sem)