# Integrate LibRocket With SDL And OpenGL

I been currently involved in a game project and we reach the point we're want to added the GUI for menus and other stuff but the time that we maybe spent developing our own system could cost us a bit of time, and this is a part of the system that we don't want to lose too much time.

So, we're reading about some UI Libraries like GUIchan, CEGUI and recently LibRocket which apparently is very flexible and useful, so my question is if Librocket could be integrate with a project develop in SDL/OpenGL in C++? and if you know where to find information about that, because we're unable to find something like that.

Yes, you absolutely can use libRocket with nearly any rendering system (There is libRocket apps in most Desktop systems, iOS and Android. )

libRocket uses an abstract render interface internally. libRocket is a library that is ready to parse text into internal structures, and then push its geometry into that abstract renderer.

So, nothing is rendered at all if you don't implement the renderer yourself. You simply inherit Rocket::Core::RenderInterface if im not mistaken, and implement a few functions yourself.

Documentation on how to do this can be found here: http://librocket.com/wiki/documentation/C%2B%2BManual/Interfaces

Then, when you assign your custom renderer, it will be used for pushing the geometry. At this point, you have full control of what to do with it.

There are samples around for OpenGL renderers, but you can adapt one to SDL, if you see that suits you !

Hope it helps.

• Thanks for the comment, that will help me a lot in the process. – oscar.rpr Oct 10 '11 at 4:17
• @oscar.rpr It should be noted also that libRocket provides a sample render interface which provides no optimisations that you can use in the samples/shell/include/ShellRenderInterface.h and samples/shell/src/ShellRenderInterface.cpp files as is explained here. In addition, the last time I used it, the library didn't enable GL_VERTEX_ARRAYS when it rendered, so I had to do it myself or I couldn't see anything. – Martin Foot Oct 10 '11 at 8:10
• Thanks for the reply, I going to work with that these days, I tell you my progress. – oscar.rpr Oct 12 '11 at 0:35

I guess you have to make some changes in libRocket code to make it pass Window creation. I'm not sure if there is another way but I'm sure this piece of code will work! and I assume you are developing for a windows system, if you are targeting another platform there is a similar steps but with different variables and function changes!

first of all you need to get a handler to the window SDL creates. this piece of code extracts required HWND value (according to here:

#include <SDL_syswm.h>

struct SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);

if(-1 == SDL_GetWMInfo(&wmInfo))
throw AVException(SDL_GetError());

HWND hwndSDL = wmInfo.window;


now we need to make sure libRocket uses same window for it's rendering, after a little search in it's files I've found a file hg/shell/src/win32/ShellWin32.cpp (address is based on googlecode) which contains window creation functions. there you'll see a function named bool Shell::OpenWindow(const char* name, bool attach_opengl). that is the main function responsible for creating a window. to make libRocket reuse our hwndSDL variable you have to remove/comment all the lines in that function except these :

if (attach_opengl)
{
opengl_attached = AttachOpenGL();
if (!opengl_attached)
return false;
}


and just before these lines add this piece of code:

window_handle = hwndSDL;


I know it's probably not the best way to do job but I'm sure it'll work. and if you are targeting anything other than windows you just have to find equivalents for HWND value and createwindow function. they sure are somewhere in codes since almost all window systems either windows or non-windows share same logic.

• Sorry, do you use libRocket often? I'm nearly sure libRocket doesnt come to that low-level kind of code.. Without disregarding your answer :) I will try an answer anyway :) – Grimshaw Oct 9 '11 at 22:53
• Thanks for the answer, I will check out your idea for future game projects. – oscar.rpr Oct 10 '11 at 4:18
• @Devil no I just found out such a library exist! as I stated in my answer, I'm not sure if this was the best method for libRocket, but it should work. I've done same thing to connect some other graphical libraries. for example to initialize a gameengine inside Qt SDK. – Ali1S232 Oct 10 '11 at 8:39