I need to create and run a simple interactive C++ Cairo (graphics) app on MacOS, which presumably means using the Quartz backend.
I feel really silly asking this, but I cannot seem to locate a simple example on the Web. Everything uses Cocoa (which is ObjectiveC) or Swift.
Here's what I have so far, and it links properly but does nothing... but it's because I'm missing some stuff I'm sure:
#include <cairo/cairo-quartz.h>
#include <cairo/cairo.h>
int main()
{
cairo_surface_t* surface = cairo_quartz_surface_create (
CAIRO_FORMAT_RGB24
, 640
, 480
);
cairo_t* cr = cairo_create(surface);
// What next? How do I get the window to show up?
// What do I use for my event loop?
}
Command to build:
clang++ $(pkg-config --cflags --libs cairo) main.cpp
(Obviously this assumes you have correctly installed Cairo)
Cairo doesn't know about windows or event loops. It just knows how to draw into “surfaces”, where a surface is somewhat circularly defined as “something Cairo can draw into”. It can create various system-independent surfaces, like in-memory bitmaps, PDFs, and PostScript files, and it can wrap a surface around some system-dependent drawing targets, like a Quartz
CGContext, a WindowsHDC, or an X11Drawable.If you want to create an interactive application, you need to use some other system to create an on-screen drawing target and to respond to user input. Then you can wrap the system's on-screen drawing target in a
cairo_surface_tand use Cairo to draw.So, you can use the native macOS Cocoa frameworks for interaction, and use
cairo_quartz_surface_create_for_cg_contextto wrap acairo_surface_taround a macOS-nativeCGContext. Cocoa applications tend to provide the best user experience on macOS.Or you can use the cross-platform Qt system for interaction, and use
cairo_qt_surface_createto wrap acairo_surface_taround aQPainter. Qt is a C++ system, so this might be your easiest solution.Or you can use SDL with the
cairosdlhelpers.Or, for any framework that provides access to simple RGB bitmaps, you can use
cairo_image_surface_create_for_datato wrap acairo_surface_taround the bitmap.Or, for any framework that can import a simple RGB bitmap, you can use
cairo_image_surface_createto create a bitmap, and then hand it off to the system using the system's API.