-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Welcome to the guisan wiki!
Guisan is an updated version of Guichan for SDL2.
Once you have cloned the repository, build the library and its examples by typing
scons
Note: this will not build the demo.
To install, type
sudo scons install
which will install the library, its header files and the pkg-config file at the appropriate places. The authors of Guichan have made an amazing work in documenting their code, but to generate a more user-friendly documentation in HTML, type
doxygen Doxyfile
As is the case with most libraries, make sure you link your program with the -lguisan
option. Because guisan requires SDL2, SDL2_image and SDL2_ttf, guisan needs to appear before them in the compilation command. It may also be necessary to indicate the correct directory where the headers are installed.
If your project uses autotools, you can check for the presence of guisan on the system with pkg-config using the PKG_CHECK_MODULES
macro in your configure.ac
file.
If instead you prefer CMake, thanks to the use of pkg-config, you can use the PKG_SEARCH_MODULE
macro to look for guisan
.
Note: this part builds upon the knowledge gained from reading the examples provided.
The first step is to add the necessary headers in your source file:
#include <guisan.hpp> /* mandatory */
#include <guisan/sdl.hpp> /* needed for SDL-specific implementations */
Pay attention to the fact that in the case of errors, guisan uses C++ exceptions instead of the integer return codes and SDL_{Set/Get}Error
combo used by other SDL libraries. Make sure you catch them!
All the widgets and classes provided by guisan are located in the namespace gcn
.
Several objects need to be created to be able to use guisan in your project. According to the examples provided, the required parts are:
- a
Graphics
object, like anSDLGraphics
to handle widget drawing - an
Input
object to manage events - an
ImageLoader
, which is used internally to... load images, for example to createIcon
s orImageButton
s - a
GUI
object - a
Font
object to draw texts such asLabel
s or captions. Guisan provides ImageFont out of the box, but if you prefer to use TrueType fonts, SDLTrueTypeFont is also available (imported from Guichan, justifies the SDL2_ttf dependency). - a
Container
object. Indeed, the GUI object can only contain one widget, so if you plan to use more than one at the same time, the Container is the only way to go.
Please note: guisan is not compatible (yet) with the new SDL2 Render API! See SDL2Graphics for an attempt to fix this.
In the meantime, the workaround I found consists of:
- creating a dedicated SDL_Surface with the dimensions of your window. Fill it with an opaque color (I recommend magenta), then set this color as the colorkey for this surface. Set its blend mode to
SDL_BLENDMODE_NONE
. - from this SDL_Surface, create a SDL_Texture using
SDL_CreateTextureFromSurface
. Set the blend mode of this texture toSDL_BLENDMODE_BLEND
. - Set the target surface for the Graphics object to your dedicated surface.
The next step is to include guisan in your game event loop.
- At the end of your event processing code, add a call to the
pushInput(SDL_Event event)
method of thegcn::SDLInput
object to make guisan aware of this event. - Then, we would like the GUI to process this event. To do so, call the
logic()
method of theGui
object. No parameter is needed. - If you use SDLGraphics with the workaround explained above: now is the time to clear your dedicated surface using
SDL_FillRect()
and a transparent color (alpha value of 0). - Next, it is time to draw the updates to the target surface, using the
draw()
method of theGui
object. Again, no parameter is needed. - If you use SDLGraphics with the workaround explained above: update your dedicated texture with the contents of the dedicated GUI surface using
SDL_UpdateTexture
, then doSDL_RenderCopy
to copy the texture contents to the renderer. - Finally, we tell SDL to display the changes on the screen with a call to
SDL_UpdateWindowSurface()
if you use the window surface, orSDL_RenderPresent()
if you use a renderer.
Please see the examples sdlwidgets.cpp and sdl2widgets.cpp for the 2 possible solutions (SDLGraphics + GetWindowSurface or SDL2Graphics + renderer).