问题描述
你可能知道,C ++没有标准的图形库。大多数游戏使用DirectX或OpenGL。
As you probably know, C++ has no standard graphics library. Most games use DirectX or OpenGL.
但是这些库实际上如何工作?换句话说,如果C ++中没有机制,第三方库如何绘制图形?
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?
推荐答案
具体来说DirectX和OpenGL通过调用操作系统和/或视频硬件驱动程序来工作。驱动程序又通过与图形设备交互来完成实际绘图。互动的确切细节因视频卡而异。
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.
回到DOS日,C ++程序员可以直接使用硬件。这是以两种方式完成的。首先,通过写入/读取存储像素或文本的特殊存储器块(帧缓冲器)。它是一个已知地址的一段内存,所以要使用它,你必须将一个整数常量转换为指针并使用该指针。纯C ++机制。另一种交互方式是从I / O端口读取/写入I / O端口。现在,这是一个不直接由C支持的机制,除非你想计入内联汇编或编译器内在函数。有两个库函数会封装这两个操作(从字面上看,它们围绕着两个CPU命令 - INP和OUTP),但是大多数程序员只使用一行内联汇编代码片段。
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 wanna 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.
即使现在,大多数视频硬件交互归结为这两种途径 - 帧缓冲区和I / O端口(DMA和中断通常不用于图形)。但是我们的应用程序级程序员不会看到那些。这是驱动程序的东西。
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.
一个现代警告与保护模式有关;在保护模式中,C指针不与底层物理地址相同。简单地类型传递0xA0000到指针不会让你到帧缓冲区,即使在内核模式。然而,内核级代码(即驱动器)可以请求存储器管理器给予它对应于特定物理地址的指针。
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.
这篇关于C ++图形库的内部工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!