Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple 2D graphics programming

Tags:

graphics

2d

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?

  1. Will this same method of programming still apply or do I have to have a different understanding of the video hardware abstraction?
  2. What libraries and tools are available on Windows and Linux?
like image 420
rup Avatar asked Jul 12 '10 07:07

rup


2 Answers

What libraries and tools are available on Windows and Linux?

SDL, OpenGL, and Qt 4 (it is gui library, but it is fast/flexible enough for 2D rendering)

Will this same method of programming still apply or do I have to have a different understanding of the video hardware abstraction?

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.

like image 189
SigTerm Avatar answered Oct 24 '22 14:10

SigTerm


On Linux you could use OpenGL, it is not only used for 3D support but also supports 2D.

like image 22
UnixShadow Avatar answered Oct 24 '22 13:10

UnixShadow