Tutorial 1. Minimal "Hello world"

We will implement a minimal "Hello world" application with libagar.

If libagar is not yet installed on your system, refer to the Installation Guide for your particular platform. Compilation of a minimal "Hello world" is demonstrated at the end of each Installation Guide.

*** FIXME how to set monospace font in patreon text posts?

#include <agar/core.h>
#include <agar/gui.h>
 
 int
 main(int argc, char *argv)
 {
         AG_Window *win;
         
         AG_InitCore(NULL, 0);
         AG_InitGraphics(0);

         win = AG_WindowNew(0);
         AG_LabelNew(win, 0, "Hello, world!");
         AG_WindowShow(win);

         AG_EventLoop();
         return (0);
 }

We must initialize both core and gui libraries by calling AG_InitCore() followed by AG_InitGraphics(). We omit error checking in this example, but in a proper application both calls should be checked for a return value of -1, in which case the error string could be retrieved using AG_GetError().

The AG_WindowNew() call creates a new window, returning a pointer to the newly instantiated AG_Window object.

Create an AG_Label widget by calling AG_LabelNew(), passing the AG_Window object as the parent argument. Another possibility for the parent argument would have been a different widget (for example an AG_Box) acting as a container widget, but here we attach the label to the window directly.

Newly created windows are not visible by default. To make a window visible we must call AG_WindowShow().

Finally, enter AG_EventLoop(). This is agar's standard event loop routine which waits on low-level events (such as activity on sockets / file descriptors, timer expirations and kernel-event notifications), and processes them.