Handling Events

Events are sent to views in situations that might need handling, such as user input being available or the graphics needing to be drawn. This is the main way that application code interacts with the user and window system.

Events are sent as a PuglEvent to the callback function set with puglSetEventFunc(). The type field defines the type of the event and which field is active. There are many event types, but visible applications must handle at least PUGL_CONFIGURE and PUGL_EXPOSE.

For example, a basic event handler might look something like this:

static PuglStatus
onEvent(PuglView* view, const PuglEvent* event)
{
  MyApp* app = (MyApp*)puglGetHandle(view);

  switch (event->type) {
  case PUGL_REALIZE:
    return setupGraphics(app);
  case PUGL_UNREALIZE:
    return teardownGraphics(app);
  case PUGL_CONFIGURE:
    return resize(app, event->configure.width, event->configure.height);
  case PUGL_EXPOSE:
    return draw(app, view);
  case PUGL_CLOSE:
    return quit(app);
  case PUGL_BUTTON_PRESS:
     return onButtonPress(app, view, event->button);
  default:
    break;
  }

  return PUGL_SUCCESS;
}

Drawing

Drawing is only performed in response to an exposure (PuglExposeEvent), allowing portable code to make efficient use of the graphics system. The graphics context is only available for this and perhaps a few other events, depending on the backend in use.

Cairo

A Cairo context (cairo_t) is created for each exposure, and can be accessed with puglGetContext() during its handling. The returned pointer is only valid until the end of the exposure.

OpenGL

The OpenGL context is only active while handling realization (PUGL_REALIZE, PUGL_UNREALIZE) and exposure (PUGL_BLANK, PUGL_EXPOSE), and can only be used for drawing during exposure. Note that it’s not active while handling configuration (PUGL_CONFIGURE), so the application must store the configuration and apply it to the graphics context on the next exposure. The OpenGL context is implicit state, so puglGetContext() always returns null.

Vulkan

With Vulkan, the graphics context is managed by the application, so puglGetContext() always returns null. The application must render and submit a frame when handling an exposure.

Requesting Redrawing

A view or a region of it can be scheduled for redrawing with puglObscureView() or puglObscureRegion(). This is usually done during an update (PUGL_UPDATE), but a view can be obscured at any time except during exposure.

After a view is obscured, an exposure (PuglExposeEvent) will be dispatched when it’s time to draw the updated contents. Note however that exposures are always mediated by the window system, there’s no direct correspondence between them and any earlier obscure calls.

To redraw continuously, set the PUGL_CONTINUOUS hint, and synchronized exposures will automatically be dispatched for every frame without needing to explicitly obscure the view.