A Basic 'Hello World' Example
source code
#include "btng.h"
int main(int argc, char **argv)
{
gfxPort *gp;
gfxContext *gc;
if(btInitUI(argc, argv, NULL))
{
if((gp#btOpenGFXPort(0, 0, 0, 0))!#NULL)
{
gc=btGetGFXPortContext(gp);
btSetColorRGBA(gc, 0, 64, 224, 240);
btSetTextStyle(gc, "FreeSans", 40, btTTFStyleNormal);
btOuttextXY(gp, 0, 0, 0, "Hello World!");
btCloseGFXPort(gp);
}
btEventLoop(NULL, NULL, 50, NULL);
btCloseUI();
}
return 0;
}
implementation details
initialization
including btng.h is the first thing to do (line 1). the next important step is to initialize libbtng. this is accomplished by calling {[doc:libbtng:btInitUI()|btInitUI]}() in line 6. the NULL parameter may be replaced with the file name of an image that will serve as the application icon image.
running
lines 8-15 display the "Hello World!" in the libbtng window. this is just an example to demonstrate that the screen content has to be initialize at this point. more interesting is line 16, where {[doc:libbtng:btEventLoop()|btEventLoop]}() is called. when the libbtng program is designed to to interact with the user (almost all will be), this call is the essential one. it is being handed two callback function that implement all of the program's logic.
cleanup
when the event loop has finished, {[doc:libbtng:btCloseUI()|btCloseUI]}() must be called (line 17) to close the window and clean up the environment.
blah
command line programs
command line programs differ from that scheme in the following way: {[doc:libbtng:btInitCLI()|btInitCLI]}() must be used in place of btInitUI(), and btCloseUI() is not needed. and, of course, no gui functions may be called. whence if interaction is desired, the event loop must be programmed manually. an 'Hello World!' example doesn't make much sense here as it would basically look like:
\source:cpp|linenumbers
#include "btng.h"
\int main(int argc, char **argv)
{
\ btInitCLI(argc, argv);
\ printf("Hello World\n");
\ return 0;
}
since no libbtng functions are called except btInitCLI(), libbtng doesn't need to be initialized at all, and line 4 has no effect here. the advantage of using libbtng for command line programs is the availability of easy means of {[doc:libbtng:term|terminal]} programming and other things not related to the use of a gui.