使用复制构造函数后不渲染SDL纹理



我正在制作一个使用SDL渲染图形的基本程序。我有两个类处理渲染:

一个Texture类(加载并渲染SDL_textures)

//Texture warpper class
class LTexture
{
private:
    //The actual texture
    SDL_Texture* mTexture;
    //Image demensions
    int mWidth;
    int mHeight;
public:
    //Initializes/Deallocates variables
    LTexture();
    ~LTexture();
    LTexture(const LTexture &rhs);
    //Loads image at specified path
    bool loadFromFile(std::string path);
    //Deallocates texture
    void free();
    //Renders texture at given point
    void render(int x, int y);
    //Gets image dimensions
    int getWidth();
    int getHeight();

};

    LTexture::LTexture()
{
    //Initialize
    mTexture = NULL;
    mWidth = 0;
    mHeight = 0;
}
LTexture::~LTexture()
{
    //Deallocate
    free();
}
LTexture::LTexture(const LTexture &rhs)
{
    mTexture = rhs.mTexture;
    mWidth = rhs.mWidth;
    mHeight = rhs.mHeight;
}
bool LTexture::loadFromFile(std::string path)
{
    //Get rid of preexisting texture
    free();
    //The final texture
    SDL_Texture* newTexture = NULL;
    //Load image at specified path
    SDL_Surface* loadedSurface = IMG_Load(path.c_str());
    if (loadedSurface == NULL)
    {
        printf("Unable to load image %s! SDL_image error: %sn", path.c_str(), IMG_GetError());
    }
    else
    {
        //Create texture from surface pixels
        newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
        if (newTexture == NULL)
        {
            printf("Unable to create texture from %s! SDL Error: %sn", path.c_str(), SDL_GetError());
        }
        else
        {
            //Get image dimensions
            mWidth = loadedSurface->w;
            mHeight = loadedSurface->h;
        }
        //Get rid of old loaded surface
        SDL_FreeSurface(loadedSurface);
    }
    //Return success
    mTexture = newTexture;
    return mTexture != NULL;
}
void LTexture::free()
{
    //Free Texture if it exists
    if (mTexture != NULL)
    {
        SDL_DestroyTexture(mTexture);
        mTexture = NULL;
        mWidth = 0;
        mHeight = 0;
    }
}
void LTexture::render(int x, int y)
{
    //Set rendering space and render to screen
    SDL_Rect renderQuad = { x, y, mWidth, mHeight };
    SDL_RenderCopy(gRenderer, mTexture, NULL, &renderQuad);
    printf("Rendering...n");
    if (mTexture == NULL)
    {
        printf("No texture loaded!n");
    }
    else
    {
        printf("Texture loaded! %sn", mTexture);
    }
}
int LTexture::getWidth()
{
    return mWidth;
}
int LTexture::getHeight()
{
    return mHeight;
}

和一个Button类(这应该使它更容易在与按钮状态相关的不同纹理之间切换。每个对象应该在按钮中有3个纹理对象)。

声明:

        //Class for buttons and chips
    class Button
    {
    public:
        //Initializes internal variables
        Button();
        //Handles mouse events
        void handleEvent(SDL_Event* e);
        //Render buttons
        void render();
        //Sets top left position
        void setPosition(int x, int y);
        //Gets image dimensions
        void setWidth(int w);
        void setHeight(int h);
        //Set button status
        void setButtonAction(buttonAction action);
        //Get button status
        buttonStatus getButtonStatus();
        //Perform button action !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!FIXME::HAVEN'T DEFINED!!!!!!!!!!!!!!!!!!!!!!!!!!!!1!!
        void activateButton(buttonAction action);
        void setTextures(LTexture out, LTexture over, LTexture down);

    private:
        //Top left position
        SDL_Point mPosition;
        //Currently used global image
        buttonStatus mButtonStatus;
        //What happens if button is pressed
        buttonAction mButtonAction;
        //Width and height
        int mWidth;
        int mHeight;
        //Textures
        LTexture mOut;
        LTexture mOver;
        LTexture mDown;
    };
Button::Button()
{
    mPosition.x = 0;
    mPosition.y = 0;
    mWidth = 0;
    mHeight = 0;
    mButtonStatus = MOUSE_OUT;
}
void Button::setPosition(int x, int y)
{
    mPosition.x = x;
    mPosition.y = y;
}
void Button::handleEvent(SDL_Event* e)
{
    bool mInside = true;
    //If mouse event happened
    if (e->type == SDL_MOUSEBUTTONDOWN || e->type == SDL_MOUSEMOTION)
    {
        //Get mouse position
        int x, y;
        SDL_GetMouseState(&x, &y);
        //Mouse is left of the button
        if (x < mPosition.x)
        {
            mInside = false;
        }
        //Mouse is right of button
        else if (x > mPosition.x + mWidth)
        {
            mInside = false;
        }
        //Mouse is above button
        else if (y < mPosition.y)
        {
            mInside = false;
        }
        //Mouse is below button
        else if (y > mPosition.y + mHeight)
        {
            mInside = false;
        }
        //Logic\
        //Mouse is outside of button
        if (!mInside)
        {
            mButtonStatus = MOUSE_OUT;
        }
        //Mouse is inside of button
        else
        {
            switch (e->type)
            {
            case SDL_MOUSEMOTION:
                mButtonStatus = MOUSE_OVER;
                break;
            case SDL_MOUSEBUTTONDOWN:
                mButtonStatus = MOUSE_BUTTON_DOWN;
                break;
            }
        }
    }
}
void Button::render()
{
    switch (mButtonStatus)
    {
    case MOUSE_OUT:
        mOut.render(mPosition.x, mPosition.y);
        printf("Out renderedn");
        break;
    case MOUSE_OVER:
        mOver.render(mPosition.x, mPosition.y);
        printf("Over renderedn");
        break;
    case MOUSE_BUTTON_DOWN:
        mDown.render(mPosition.x, mPosition.y);
        printf("Down renderedn");
        break;
    }
}

void Button::setWidth(int w)
{
    mWidth = w;
}
void Button::setHeight(int h)
{
    mHeight = h;
}
void Button::setButtonAction(buttonAction action)
{
    mButtonAction = action;
}
buttonStatus Button::getButtonStatus()
{
    return mButtonStatus;
}
void Button::setTextures(LTexture out, LTexture over, LTexture down)
{
    mOut = out;
    mOver = over;
    mDown = down;
}

不幸的是,当我尝试使用复制构造函数将SDL_Texture值从原始纹理传递到按钮私有纹理时,它不传递任何值,但仍然认为它不是"NULL"

我发现这段代码有两个问题。首先,在LTexture复制构造函数中,您只将指针复制到SDL_Texture。这被称为浅拷贝。然后,在LTexture的析构函数中调用free(),删除SDL_Texture。这很糟糕,因为这意味着LTexture的任何副本现在都有指向已被删除的纹理的指针,所以它们将不再工作。这里最好的两个解决方案是使用一个名为shared_ptr的c++11类来存储纹理,这将防止它被删除,或者实际上复制纹理并指向新的纹理(深度复制)。对于测试,您可以尝试在free()中注释对SDL_DestroyTexture的调用。

另外,您忘了实现赋值操作符。目前它的行为就像复制构造函数一样(因为你所做的只是一个浅复制),但如果你让它成为一个深复制,你也必须实现它。

最新更新