3.4. 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 my_renderer(sdl_surface) :
... draw something on sdl_surface using pygame
libtcod.sys_register_SDL_renderer(my_renderer)
Parameter | Description |
---|---|
callback | The renderer to call before swapping the screen buffer. If NULL, custom rendering is disabled |
Example:
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);
Managing screen redraw
libtcod is not aware of the part of the screen your SDL renderer has updated. If no change occured 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)
Parameter | Description |
---|---|
x,y,w,h | Part of the root console you want to redraw even if nothing has changed in the console back/fore/char. |