我按如下方式定义SDL2窗口:
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); // red: 8 bits
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); // green: 8 bits
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); // blue: 8 bits
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); // alpha: 8 bits
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32); // r + g + b + a = 32 bits
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
graphWindow = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
然后,使用以下结构,我创建一个像素数组,并使用该数组作为传递给SDL的缓冲区:
typedef struct {
float r;
float g;
float b;
float a;
} Pixel;
// ...
void main(void) {
Pixel *pixels;
// ...
pixels = malloc(width * height * sizeof(Pixel));
// ...
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
while (graphNotClosed) {
glDrawPixels(width, height, GL_RGBA, GL_FLOAT, pixels);
// do some number cruching and change some elements of array "pixels"
// ...
SDL_GL_SwapWindow(graphWindow);
}
}
困扰我的是,虽然代码有时会在while
循环中更改一个且只有一个像素,但我仍然继续传递整个数组(每次迭代时使用width * height
)到SDL(通过glDrawPixels
)。
我的问题:这是将整个数组传递给SDL的必要(也是唯一的方法),还是可以通知SDL只是从先前状态改变的像素,名称为加速图形处理。