Core

Graphics and OpenGL Integration

NAppGUI splits graphics into a portable 2D drawing layer, draw2d, and a separate OpenGL helper layer, ogl3d. The first supports fonts, images, and draw contexts; the second creates native OpenGL contexts for views.

Relevant Source Files

draw2d Scope

The public draw2d layer is intentionally compact at the top level: start/finish plus a preferred monospace family hint. The real surface expands through the related headers: dctx.h for drawing contexts, font.h for font creation and measurement, and image.h for raster image loading, conversion, scaling, trimming, encoding, and native-handle access.

DCtx can draw into an offscreen bitmap via dctx_bitmap(...), then materialize the result as an Image. That offscreen path is used heavily by owner-drawn widgets and platform control caching.

2D Backend Implementations

Platform Main Stack
macOS Core Graphics, Cocoa fonts/images
Windows GDI/GDI+
GTK Cairo plus Pango text layout

This backend-specific code is hidden behind the portable draw/image/font APIs, which is why owner-drawn widgets in gui can be shared across all three desktop platforms.

Fonts and Images as First-Class Types

Fonts can be created by family, system default, or monospace default, then copied or transformed with new style/width/x-scale. The API exposes family, size, height, width, ascent/descent, installed families, and the native backend handle.

Images can come from raw pixels, pixbufs, files, memory data, or resource packs. They support copy, crop, rotate, scale, stream I/O, frame counts for animated formats, codec inspection, pixel extraction, and an attached user data pointer.

ogl3d Lifecycle and Context Model

ogl3d is deliberately separate from draw2d. Its public API manages OpenGL context lifetime with ogl3d_start(), ogl3d_context(...), ogl3d_begin_draw(), ogl3d_end_draw(), ogl3d_set_size(), and ogl3d_destroy().

The layer tracks context allocations/deallocations and asserts on shutdown if a context was leaked. It also validates the requested OpenGL version through GLEW before declaring a context usable. The dedicated OGL3D Internals page goes deeper into the backend-specific details and the glhello integration pattern.

Platform OpenGL Implementations

  • macOS: creates NSOpenGLContext with profile attributes chosen from OGLProps and attaches it to an NSView.
  • Windows: uses wgl with a native window device context.
  • GTK: uses EGL on top of GTK/X11 and maps the requested API/profile into EGL context attributes.

The public API stays the same, but the native handle passed as view is platform-specific by design.