[英]Simple Cairo/Quartz C++ example
I need to create and run a simple interactive C++ Cairo (graphics) app on MacOS, which presumably means using the Quartz backend. 我需要在MacOS上创建并运行一个简单的交互式C ++ Cairo(图形)应用程序,这大概意味着要使用Quartz后端。
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.
一切都使用可可(ObjectiveC)或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. 开罗不了解Windows或事件循环。 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 Windows HDC
, or an X11 Drawable
. 它可以创建各种与系统无关的表面,例如内存中的位图,PDF和PostScript文件,并且可以将表面包裹在一些与系统相关的图形目标周围,例如Quartz
CGContext
,Windows HDC
或X11 Drawable
。
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_t
and use Cairo to draw. 然后,您可以将系统的屏幕绘制目标包装在
cairo_surface_t
并使用Cairo进行绘制。
So, you can use the native macOS Cocoa frameworks for interaction, and use cairo_quartz_surface_create_for_cg_context
to wrap a cairo_surface_t
around a macOS-native CGContext
. 所以,你可以使用原生的MacOS Cocoa框架的互动,并使用
cairo_quartz_surface_create_for_cg_context
包装一个cairo_surface_t
围绕MacOS的原生CGContext
。 Cocoa applications tend to provide the best user experience on macOS. 可可应用程序倾向于在macOS上提供最佳的用户体验。
Or you can use the cross-platform Qt system for interaction, and use cairo_qt_surface_create
to wrap a cairo_surface_t
around a QPainter
. 或者你可以使用跨平台的Qt系统互动,并使用
cairo_qt_surface_create
包装一个cairo_surface_t
周围QPainter
。 Qt is a C++ system, so this might be your easiest solution. Qt是C ++系统,因此这可能是您最简单的解决方案。
Or you can use SDL with the cairosdl
helpers. 或者,您可以将SDL与
cairosdl
帮助程序一起使用。
Or, for any framework that provides access to simple RGB bitmaps, you can use cairo_image_surface_create_for_data
to wrap a cairo_surface_t
around the bitmap. 或者,对于提供访问简单RGB位图的任何框架,您可以使用
cairo_image_surface_create_for_data
将cairo_image_surface_create_for_data
包裹cairo_surface_t
图周围。
Or, for any framework that can import a simple RGB bitmap, you can use cairo_image_surface_create
to create a bitmap, and then hand it off to the system using the system's API. 或者,对于可以导入简单RGB位图的任何框架,都可以使用
cairo_image_surface_create
创建位图,然后使用系统的API将其交给系统。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.