作爲替代上面的回答,如果因任何原因,你確實需要動態地創建location
,你需要做的是這樣的:
while(event.type != SDL_QUIT) //The game loop that does everything
{
SDL_Rect *location;
location = new SDL_Rect(600,400,0,0); //add new operator
SDL_PollEvent(&event); //This "polls" the event
//Drawing stuff goes here
SDL_BlitSurface(zombie, NULL, buffer, location);
SDL_Flip(buffer); //Draw
delete location; //IMPORTANT: deallocate memory
}
請注意,因爲在循環的每次迭代中都會創建一個額外的SDL_Rect
,並且在下一次迭代中將不再是指向它的指針,所以有必要在循環結束之前將其刪除(換句話說,在每次迭代結束之前刪除)。否則,你創建一個內存泄漏。
作爲另一種替代方案,如果您需要更改爲location
以從循環的一次迭代持續到下一次循環,或者如果根本不需要在循環內更改,但是您想清理尾波一點點,你可以做這樣的事情:
SDL_Rect *location = new SDL_Rect(600,400,0,0);
while(event.type != SDL_QUIT) //The game loop that does everything
{
SDL_PollEvent(&event); //This "polls" the event
//Drawing stuff goes here
SDL_BlitSurface(zombie, NULL, buffer, location);
SDL_Flip(buffer); //Draw
}
delete location;
我不知道它是用C寫的,謝謝你告訴我,並給我一個合法的答案! :D – Lemmons 2010-10-14 07:38:02
順便說一下,由於C++ 11的第一個例子可以改寫爲「SDL_Rect location {0,0,600,400};' – HolyBlackCat 2016-02-13 08:56:57