gpt4 book ai didi

c++ - 游戏运行几分钟后 SDL_Surface 消失?

转载 作者:太空宇宙 更新时间:2023-11-04 14:08:48 26 4
gpt4 key购买 nike

运行我的小行星游戏几分钟后,代表我的飞船的 SDL_Surface 消失了。以某种方式保存船的旋转图像(rot)的表面被设置为 NULL。我不知道为什么!在我添加 if(rot != NULL && image != NULL) 行之前,程序会在运行大约一分三十秒后崩溃,但现在图像就消失了。我似乎无法弄清楚出了什么问题!提前致谢!

涉及旋转和渲染的 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);

最佳答案

您似乎没有释放最后旋转的图像。检查是否为 null,并在创建新表面之前释放表面:

void Sprite::Rotate(double angleAdd)
{
if(rot != NULL)
SDL_FreeSurface(rot);

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);
}

关于c++ - 游戏运行几分钟后 SDL_Surface 消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15726313/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com