1
運行我的小行星遊戲幾分鐘後,代表我的船的SDL_Surface消失。不知怎的,保存船的旋轉圖像(腐蝕)的表面被設置爲NULL。我無法弄清楚爲什麼!在我添加行if(rot != NULL && image != NULL)
之前,程序在運行約三十秒後會崩潰,但現在圖像就消失了。我似乎無法弄清楚什麼是錯的!提前致謝!遊戲運行幾分鐘後,SDL_Surface會消失嗎?
Sprite類涉及旋轉和渲染的函數。
void Sprite::Rotate(double angleAdd)
{
rotated = true;
double temp = 0;
angle += angleAdd;
temp = angleAdd + angle;
if (temp >= 360)
angleAdd = temp - 360;
if (temp <= 360)
angleAdd = temp + 360;
rot = rotozoomSurface(image, angle , 1.0, 1);
}
void Sprite::Draw(SDL_Surface* screen)
{
if(image == NULL)
SDL_FillRect(screen, &rect, color);
else
{
if (rotated)
{
rotRect.x = x;
rotRect.y = y;
//Center the image
if(rot != NULL && image != NULL){
rotRect.x -= ((rot->w/2) - (image->w/2));
rotRect.y -= ((rot->h/2) - (image->h/2));
}
SDL_BlitSurface(rot, NULL, screen, &rotRect);
}
else
SDL_BlitSurface(image, NULL, screen, &rect);
}
}
bool Sprite::SetImage(std::string fileName)
{
imageFileName = fileName;
SDL_Surface* loadedImage = NULL;
loadedImage = SDL_LoadBMP(fileName.c_str());
if(loadedImage == NULL)
{
if (debugEnabled)
printf("Failed to load image: %s\n", fileName.c_str());
SDL_FreeSurface(loadedImage);
return false;
}
else
{
image = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
return true;
}
}
主營:
if (arrowPressed[0])
{
ship.SetImage(shipFor.GetImage());
if(!phase)
ship.Accel(-moveSpeed);
else
ship.Accel(-moveSpeed * 2);
}
if (arrowPressed[1])
{
ship.Rotate(turnSpeed);
ship.SetImage(shipRig.GetImage());
}
if (arrowPressed[3])
{
ship.Rotate(-turnSpeed);
ship.SetImage(shipLef.GetImage());
}
if (arrowPressed[0] && arrowPressed[1])
ship.SetImage(shipForRig.GetImage());
if (arrowPressed[0] && arrowPressed[3])
ship.SetImage(shipForLef.GetImage());
if (!arrowPressed[0] && !arrowPressed[1] && !arrowPressed[3])
ship.SetImage(shipNor.GetImage());
if (ship.GetCenterX() >= RESX - 40)
ship.SetPosCentered(50, ship.GetCenterY());
if (ship.GetCenterX() <= 40)
ship.SetPosCentered(RESX - 50, ship.GetCenterY());
if (ship.GetCenterY() >= RESY - 40)
ship.SetPosCentered(ship.GetCenterX(), 50);
if (ship.GetCenterY() <= 40)
ship.SetPosCentered(ship.GetCenterX(), RESY - 150);
程序使用多少內存?我懷疑這是內存泄漏。 –