【发布时间】:2014-09-02 02:35:05
【问题描述】:
我有一个带有几个继承者的抽象基类,我创建了一个抽象基类的指针数组,指向继承者:
抽象基类:
class Tile
{
public:
bool isPassable;
void setTileSet( SDL_Texture *Texture );
SDL_Rect* clip;
};
继承人:
class Floor: public Tile
{
public:
bool isPassable = true;
SDL_Rect clip = { 20, 0, 20, 20 };
};
指针数组:
Tile * dungeon[ z ][ x ][ y ] = {{{ nullptr }}};
当我尝试通过指针数组访问成员 SDL_Rect 剪辑以对图块进行 blit 时,没有任何反应。程序编译并运行,但屏幕保持默认黑色:
dungeon[ 0 ][ 0 ][ 0 ] = &floor;
Draw::renderTexture( TILESET, REN, 0, 0, dungeon[ 0 ][ 0 ][ 0 ]->clip );
但是,当我只使用类 Floor 访问剪辑时,图像会很好地闪烁:
Draw::renderTexture( TILESET, REN, 0, 0, floor.clip );
我以为指针抽象基类可能没有加载,但是当我试图将代码清除时:
SDL_Rect error = { 0, 0, 20, 20 };
if( dungeon[ 0 ][ 0 ][ 0 ] != nullptr )
{
Draw::renderTexture( TILESET, REN, 0, 0, dungeon[ 0 ][ 0 ][ 0 ]->clip );
}
else
{
Draw::renderTexture( TILESET, REN, 0, 0, &error );
}
屏幕保持黑色。
这是 Draw::renderTexture 函数,虽然我怀疑这是问题所在:
void Draw::renderTexture(SDL_Texture *texture, SDL_Renderer *render, int x, int y, SDL_Rect *clipping )
{
SDL_Rect destination;
destination.x = x;
destination.y = y;
destination.w = clipping->w;
destination.h = clipping->h;
SDL_RenderCopy( render, texture, clipping, &destination );
【问题讨论】:
标签: c++ polymorphism sdl