【发布时间】:2016-02-11 06:01:33
【问题描述】:
我正在开发一个简单的滚动空间射击游戏。我有一个由实体类对象继承的 Sprite 类。我从这些对象中得到内存泄漏。我用这个游戏的主菜单对此进行了测试,我相信问题出在 sprite 类中。
内存的问题似乎是我不是 SDL_Destroy.. 正在处理纹理和渲染器。然而,通过破坏纹理,我得到了一个纹理被另一个纹理替代的狂野结果,或者根本没有纹理。如果我销毁渲染器,则不会加载任何图像。如果我都没有销毁,那么一切都会正常加载,但是会出现内存泄漏。
我相信野生纹理与我处理对象矢量的方式有关,但我不知道处理它们的另一种方式..
Sprite 类:
Sprite::Sprite()
{
}
Sprite::Sprite(SDL_Renderer* pRenderer, std::string filePath, int x, int y, int w, int h)
{
_renderer = pRenderer;
_filePath = filePath;
_image = IMG_LoadTexture(_renderer, filePath.c_str());
_rect.x = x;
_rect.y = y;
_rect.w = w;
_rect.h = h;
}
Sprite::~Sprite()
{
std::cout<<_filePath<<" destroyed"<<std::endl;
SDL_DestroyTexture(_image);
//SDL_DestroyRenderer(_renderer);
}
void Sprite::draw()
{
SDL_RenderCopy(_renderer, _image, NULL, &_rect);
}
我有几个产生子弹、小行星等的向量。它们都使用 Sprite 类。当不再需要它们时,我使用与此类似的代码来处理它们的删除:
if (!update)
{
vector[i] = vector.back;
vector.pop_back;
}
Asteroid 类只是随机更改图像的类之一。它继承了 Entity,它继承了 Sprite。不确定它是否也会泄漏内存。很难用其他所有东西进行测试..
这里是小行星类:
Asteroid::Asteroid(SDL_Renderer* pRenderer) :
_health(50)
{
_renderer = pRenderer;
_posX = getRandom(800);
_posY = -50;
_width = 30;
_height = 30;
_velocityX = getRandom(2);
_velocityY = getRandom(2);
initSprite(pRenderer, "Asteroid.png", _posX, _posY, _width, _height);
}
Asteroid::~Asteroid()
{
//EMPTY
}
bool Asteroid::update(std::vector<Bullet>& bullets,
std::vector<Block>& blocks)
{
//COLLISION BULLET
for (int i = 0; i < bullets.size(); i++)
{
if (collisionCheck(bullets[i].getRect()))
{
//CHANGE VELOCITY & POSITION
_velocityY -= 0.2f;
_posY -= 3;
//TAKE DAMAGE
_health -= 10;
}
}
if(_posY >= 500)
{
return false;
}
//IF DEAD
if (_health <= 0)
{
return false;
}
//MOVEMENT
drift();
return true;
}
void Asteroid::draw()
{
Entity::draw();
}
void Asteroid::drift()
{
//move position
_posX += _velocityX;
_posY += _velocityY;
//check boundaries
if (_posY > 0) {
if (_posX < 0)
{
_velocityX *= -1;
}
if (_posX > 800 - _width)
{
_velocityX *= -1;
}
if (_posY < 0)
{
_velocityY *= -1;
}
if (_posY > 600 - _height)
{
_velocityY *= -1;
}
}
}
bool Asteroid::collisionCheck(SDL_Rect rect2)
{
return !(_rect.x > rect2.x + rect2.w || _rect.x + _rect.w < rect2.x ||
_rect.y > rect2.y + rect2.h || _rect.y + _rect.h < rect2.y );
}
int Asteroid::getRandom(int threshold)
{
_randomNumber = rand() % threshold + 1;
return _randomNumber;}
我是 C++ 新手,但学得很快。如果有人能指出我正确的方向,我将不胜感激。
谢谢
【问题讨论】: