5

As you probably know, C++ has no standard graphics library. Most games use DirectX or OpenGL.

But how do those libraries actually work? In other words, how can the third-party libraries draw graphics if there's no mechanism for it in C++? Are they just written in another language?

Maxpm
  • 24,113
  • 33
  • 111
  • 170

3 Answers3

7

Specifically DirectX and OpenGL work by calling the operating system and/or the video hardware driver. The driver, in turn, does the actual drawing by interacting with the graphical device. The exact details of interaction vary from one video card to another.

Back in the DOS days, C++ programmers could work with the hardware directly. This was accomplished in two ways. First, by writing to/reading from a special memory block ("framebuffer") where the pixels or text were stored. It was a span of memory at a known address, so to work with it you had to cast an integer constant to a pointer and work with that pointer. Purely C++ mechanism. The other way of interaction was reading from/writing to I/O ports. Now, this is a mechanism that is not directly supported by C, unless you count inline assembly or compiler intrinsics. There were two library functions that would wrap these two operations (literally, wrappers around two CPU commands - INP and OUTP), but most programmers would just use a one-line inline assembly snippet instead.

Even now, most video hardware interaction boils down to these two pathways - framebuffer and I/O ports (DMA and interrupts are typically not used for graphics). But we application-level programmers don't get to see those. This is driver stuff.

One modern caveat has to do with protected mode; in protected mode, the C pointers are not the same as underlying physical addresses. Simply typecasting 0xA0000 to a pointer won't get you to a framebuffer, even in kernel mode. However, kernel-level code (i. e. a driver) can request that the memory manager give it a pointer that corresponds to a specific physical address.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • Are you sure about DMA not being used? ISTR something about running out of graphics memory and paging textures from/to main memory. – MSalters Feb 04 '11 at 09:28
  • DMA is usually for stream devices who emit bytes one by one - hard drives and network cards. But anyway, "using" DMA from the software side boils down to port I/O, specifying a memory address to read from/write into in some of them. The rest is done by the device itself. – Seva Alekseyev Jan 25 '13 at 14:17
  • 1
    @SevaAlekseyev: Modern GPUs usually have a whole bunch of DMA engines with scatter/gather capabilites. – datenwolf Jun 04 '20 at 20:38
  • Still, setting up DMA hardware boils down to I/O port operations. – Seva Alekseyev Oct 01 '21 at 14:25
6

They will transfer their calls to the driver which will send them to the graphic card.

Nekresh
  • 2,948
  • 23
  • 28
  • But the driver itself is also written in C or C++. Then how those driver could work if the language they are written in does not have graphic library? – MOON Jun 27 '22 at 22:33
1

DirectX and OpenGL opperate on a pretty low level. That is you say "draw a triangle, now draw a square". Normally programmers then wrap these calls into C++ classes that provide higher level functionality, such as "Draw a model, draw a tree". This is known as an engine.

I'd recommend taking a look at the NeHe tutorials for more info: http://nehe.gamedev.net/


So for example a call to draw a triangle in OpenGL looks like this:

GLBegin(BEGIN_POLYGONS);
GLVector3(0,0,0);
GLVector3(10,10,0);
GLVector3(-10,10,0);
GLEnd();

And like I said, see the above link to the NeHe tutorials, they are all in C/C++

Timothy Baldridge
  • 10,455
  • 1
  • 44
  • 80
  • An engine is usually an even higher level concept that sits on top of something like a scene graph. – Eric Feb 03 '11 at 14:24
  • Unless your engine includes you screen graph. Ogre3D, Crystal Space, XNA, and Irrlicht are all engines, and all include screen graphs – Timothy Baldridge Feb 03 '11 at 14:31
  • But how do they say "draw a triangle?" – Maxpm Feb 03 '11 at 14:37
  • 1
    By calling standard C functions...See above ^^ – Timothy Baldridge Feb 03 '11 at 14:40
  • A bit late, but just because an engine includes a scene graph doesn't mean it's not a conceptually separate concept. A scene graph is a data structure to manage rendering 3D data. An engine manages the scene graph content, among other things. – Eric Jul 27 '11 at 15:04