我決定做我的第一個遊戲,它很簡單,但我想用C++,而且我選擇了SDL來學習。所以我的問題是關於在編寫代碼時如何處理「緩衝區」。我會在底部發布我的相關代碼。使用SDL進行硬件緩衝,關於它是如何工作的問題
好的,基本上我的理解是,SDL負責實際將哪個緩衝區繪製到屏幕上。當我寫入緩衝區時,它始終是我正在寫入的後緩衝區,或者當前未在屏幕上繪製的緩衝區。所以,當我調用SDL_Flip(screen)時,它會將我的屏幕表面「blits」到backbuffer上,然後將指向哪個緩衝區的指針移動到該緩衝區,該緩衝區曾經是backbuffer,這是我一直在研究的緩衝區,現在顯示的舊緩衝區成爲後緩衝區。此時如果我調用SDL_FillRect(參數),它將在現在的後臺緩衝區上執行?
我要發表我的學習的遊戲我的整個「心跳」,因爲它可能有助於澄清我的問題:
//While the user hasn't quit
while(quit == false)
{
//If there's an event to handle
if(SDL_PollEvent(&event))
{
//If a key was pressed
if(event.type == SDL_KEYDOWN)
{
//Set the proper message surface
switch(event.key.keysym.sym)
{
case SDLK_UP: message = upMessage; break;
case SDLK_DOWN: message = downMessage; break;
case SDLK_LEFT: message = leftMessage; break;
case SDLK_RIGHT: message = rightMessage; break;
}
}
else if(event.type == SDL_QUIT) //if the user clicks the little X in the upper right corner.
{
quit = true;
}
}
//If a message needs to be displayed
if(message != NULL)
{
// Clear the back buffer.
SDL_FillRect(SDL_GetVideoSurface(), NULL, 0);
//Draw the backgroudn to the back buffer.
apply_surface(0, 0, background, screen);
// Draw the "message" to the back buffer.
apply_surface((SCREEN_WIDTH - message->w)/2, (SCREEN_HEIGHT - message->h)/2, message, screen);
//Null the surface pointer
message = NULL;
}
//Swap the current and back buffer.
if(SDL_Flip(screen) == -1)
{
return 1;
}
}
您的陳述似乎基本正確,那麼您的問題是什麼? – Francis 2009-05-02 08:59:04