简单的2D图形编程
多年前,我使用 C 和 C++ 中的 DirectDraw 来绘制一些简单的 2D 图形。我习惯了创建表面、使用指针写入表面、翻转后台缓冲区、在屏幕外表面上存储精灵等步骤。那么今天如果我想用C或C++编写一些2D图形程序,该怎么做呢?
- 同样的编程方法是否仍然适用,或者我是否必须对视频硬件抽象有不同的理解?
- Windows 和 Linux 上有哪些库和工具可用?
I used DirectDraw in C and C++ years back to draw some simple 2D graphics. I was used to the steps of creating a surface, writing to it using pointers, flipping the back-buffer, storing sprites on off-screen surfaces, and so on. So today if I want write some 2D graphics programs in C or C++, what is the way to go?
- Will this same method of programming still apply or do I have to have a different understanding of the video hardware abstraction?
- What libraries and tools are available on Windows and Linux?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SDL,OpenGL 和 Qt 4 (它是 gui 库,但速度很快/对于 2D 渲染来说足够灵活)
通常,您不会每帧“使用指针”将数据写入表面,而是使用 API 提供的方法来操作/绘制它们。这是因为与每帧将数据从系统内存传输到视频内存相比,驱动程序使用视频内存的速度更快。如果必须的话,您仍然可以将数据写入硬件表面/纹理(即使在每一帧期间),但这些表面可能需要以特殊方式处理才能获得最佳性能。例如,在 DirectX 中,您需要告诉驱动程序表面数据将频繁更改,并且您只会将数据写入表面,而不会读回。另外,在面向 3D 的 API (openGL/DirectX) 中,在另一个表面上渲染表面是有点“特殊情况”,您可能需要使用“渲染目标”(DirectX) 或“帧缓冲对象”(OpenGL)。这与 DirectDraw 不同(据我所知,您可以将任何内容块传输到任何内容上)。好处是,通过 3D api,您可以获得极其灵活的处理表面/纹理的方式 - 拉伸、旋转、用颜色着色、将它们混合在一起、使用着色器处理它们都可以在硬件上完成。
另一件事是,具有硬件支持的现代 3D api 通常不会在 8 位调色板纹理上运行,而更喜欢 ARGB 图像。需要时,可以模拟带有调色板的 8 位表面,并且 2D 低级 API(SDL、DirectDraw)提供它们。您还可以使用片段/像素着色器在硬件上模拟 8 位纹理。
无论如何,如果您想要使用表面的“老式”跨平台方式(即“使用指针每帧写入数据” - 即您需要软件渲染器或其他东西),SDL 可以轻松实现。如果您想要更高级别、更灵活的操作 - Qt 4 和 OpenGL 适合您。
SDL, OpenGL, and Qt 4 (it is gui library, but it is fast/flexible enough for 2D rendering)
Normally you don't write data into surface "using pointers" every frame, and instead manipulate/draw them using methods provided by API. This is because the driver will work faster with video memory than if you transfer data from system memory into video memory every frame. You still can write data into hardware surface/texture (even during every frame), if you have to, but those surfaces may need to be treated in special way to get optimal performance. For example, in DirectX you would need to tell the driver that surface data is going to change frequently and that you're going only to write data into surface, never reading it back. Also, in 3D-oriented APIs (openGL/DirectX) rendering surface on the other surface is a somewhat "special case", and you may need to use "Render Targets"(DirectX) or "Framebuffer Objects"(OpenGL). Which is different from DirectDraw (where, AFAIK, you could blit anything onto anything). The good thing is that with 3D api you get incredibly flexible way of dealing with surfaces/textures - stretching, rotating, tinting them with color, blending them together, processing them using shaders can be done on hardware.
Another thing is that modern 3D apis with hardware support frequently don't operate on 8bit palleted textures, and prefers ARGB images. 8 bit surfaces with palette may be emulated, when needed, and 2D low-level apis (SDL, DirectDraw) provide them. Also you can emulate 8bit texture on hardware using fragment/pixel shaders.
Anyway, if you want "old school" cross-platform way of using surfaces (i.e. "write data every frame using pointers" - i.e. you need software renderer or something), SDL easily allows that. If you want higher-level, more flexible operations - Qt 4 and OpenGL are for you.
在Linux上你可以使用OpenGL,它不仅用于3D支持,还支持2D。
On Linux you could use OpenGL, it is not only used for 3D support but also supports 2D.
SDL 也非常易于使用,开箱即用。它也是跨平台的,并且包含(并且有很多)可用于满足您的需求的插件。如果您需要 3D 支持,它也可以与 openGL 很好地交互。
SDL is also pretty easy to use, out of the box. It is also cross-platform, and includes (and has a lot of) plugins available to handle your needs. It interfaces nicely with openGL as well should you need 3D support.
Windows 上的 Direct2D。
EGLOutput/EGLDevice 或 GEM 取决于 Linux 的 GPU 驱动程序。
Direct2D on Windows.
EGLOutput/EGLDevice or GEM depending on the GPU driver for Linux.