Version 4, last updated by poliklosio at September 10, 2010 17:18 UTC
Quick start
Below there is the code of a minimal application using PKgui. It is an empty window than may be closed by hitting the ESCAPE key or clicking the exit button of the window.
#include <iostream>
#include "gui/PKgui.h" //assumes that PKgui has been downloaded into "gui" subdirectory of your project
using namespace std;
using namespace PKgui;
class main_loop_listener: public PKgui_listener
{
public:
virtual void on_event(const SDL_Event& _ev,bool& _continue)
{
if(_ev.type==SDL_KEYDOWN && _ev.key.keysym.sym==SDLK_ESCAPE)
_continue=!_continue;
if(_ev.type==SDL_QUIT)
_continue=!_continue;
}
virtual void before_events(bool&){}
virtual void after_events(bool&){}
virtual void before_draw(PKdisplay_target&,bool&){}
virtual void after_draw(PKdisplay_target&,bool&){}
};
int everything()
{
//essential call to set up libraries that PKgui needs to work;
if(!get_PKGUI().initialize_SDL_and_TTF())
{exit(1);}
//this sets up a way of displaying things on the screen (as opposed to
//displaying onto surfaces in main memory); currently there are:
//- PKgraphics_provider_opengl
//- PKgraphics_provider_sdl
//to chose from;
PKgraphics_provider* prov=
new PKgraphics_provider_opengl;
get_PKgraphical_data().set_provider(*prov);
const int screen_width=800,screen_height=600;
//this creates a window on the screen and sets some properties of
//display; calls SDL_SetVideoMode;
if(!get_PKGUI().initialize_graphics(
get_PKgraphical_data().get_provider(),
PKsize(screen_width,screen_height)))
{
get_PKgraphical_data().get_provider().quit();
exit(1);
}
PKdisplay_target_screen scr;
main_loop_listener listener;
get_PKGUI().set_main_loop_listener(&listener);
get_PKGUI().main_loop(scr);
//if PKgui has been compiled with the PKGUI_NO_SDL_TTF_DEPENDANCY option,
//remove the following line to avoid compilation error;
TTF_Quit();
get_PKgraphical_data().get_provider().quit();
return 0;
}
int main(int argc,char** argv)
{
try
{
return everything();
}
catch(PKexception e)
{
cerr<<e.what()<<endl;
}
return 1;
}
This piece of code is exactly like above, except it adds a caption and an edition box to the window. Note that since PKgui is completely independent of the operating system GUI, the application has to load some graphics and font at the beginning of execution. The code copied from above is shown in grey.
#include <iostream>
#include "gui/PKgui.h"
using namespace std;
using namespace PKgui;
struct app_resources
{
FONT_PTR main_font;
PKsurfaces_ids surfaces_ids;
app_resources()
{
//load font; assumes that the file guilook/GenAR102.TTF exists;
//also, it assumes that PKgui has been compiled WITHOUT the
//PKGUI_NO_SDL_TTF_DEPENDANCY option;
// (to test the code, download any free .TTF file from the internet
// put it to 'guilook' and substitute the file name here);
main_font=new PKttf_font("guilook/GenAR102.TTF",20);
PKsurface_manager& sfc_mngr=get_PKGUI().get_surface_manager();
//load graphics for different kinds of widgets
//the files styl*.bmp are located in the 'example_app_resources'
//folder in the PKgui repository;
sfc_mngr.load_bitmap_set("guilook/stylreal.bmp");
sfc_mngr.load_bitmap_set("guilook/stylrealheld.bmp");
sfc_mngr.load_bitmap_set("guilook/stylrealtextdisplay.bmp");
sfc_mngr.load_bitmap_set("guilook/stylrealtextedition.bmp");
surfaces_ids.set_no_action_surface(
sfc_mngr.get_id_for_filename("guilook/stylreal.bmp"));
surfaces_ids.set_action_surface(
sfc_mngr.get_id_for_filename("guilook/stylrealheld.bmp"));
surfaces_ids.set_non_clickable_surface(
sfc_mngr.get_id_for_filename("guilook/stylrealtextdisplay.bmp"));
surfaces_ids.set_text_display_surface(
sfc_mngr.get_id_for_filename("guilook/stylrealtextdisplay.bmp"));
surfaces_ids.set_text_edition_surface(
sfc_mngr.get_id_for_filename("guilook/stylrealtextedition.bmp"));
}
};
class app_menu
{
app_resources res;
PKextended_widget_factory factory;
W_CAPTION_PTR caption;
W_EDITBOX_PTR editbox;
PKlayer layer;
public:
app_menu():
res(),
factory(0,res.main_font,
res.surfaces_ids,
0,
create_SDL_Color(0,0,0))
{
get_PKGUI().set_active_layer(&layer);
caption=factory.create_caption(create_SDL_Rect(100,100,400,30),
"Dear user! Type something here, please:");
editbox=factory.create_editbox(create_SDL_Rect(100,140,400,30),"");
layer.add_widget(&*caption);
layer.add_widget(&*editbox);
}
};
class main_loop_listener: public PKgui_listener
{
public:
virtual void on_event(const SDL_Event& _ev,bool& _continue)
{
if(_ev.type==SDL_KEYDOWN && _ev.key.keysym.sym==SDLK_ESCAPE)
_continue=!_continue;
if(_ev.type==SDL_QUIT)
_continue=!_continue;
}
virtual void before_events(bool&){}
virtual void after_events(bool&){}
virtual void before_draw(PKdisplay_target&,bool&){}
virtual void after_draw(PKdisplay_target&,bool&){}
};
int everything()
{
if(!get_PKGUI().initialize_SDL_and_TTF())
{exit(1);}
PKgraphics_provider* prov=
new PKgraphics_provider_opengl;
get_PKgraphical_data().set_provider(*prov);
const int screen_width=800,screen_height=600;
if(!get_PKGUI().initialize_graphics(
get_PKgraphical_data().get_provider(),
PKsize(screen_width,screen_height)))
{
get_PKgraphical_data().get_provider().quit();
exit(1);
}
app_menu menu;
PKdisplay_target_screen scr;
main_loop_listener listener;
get_PKGUI().set_main_loop_listener(&listener);
get_PKGUI().main_loop(scr);
TTF_Quit();
get_PKgraphical_data().get_provider().quit();
return 0;
}
int main(int argc,char** argv)
{
try
{
return everything();
}
catch(PKexception e)
{
cerr<<e.what()<<endl;
}
return 1;
}