SDL_Texture自身变为NULL

时间:2015-07-20 12:27:38

标签: c++ pointers textures sdl

我遇到SDL问题。 我有一个用于文本对象的CLASS_Text类,它为渲染文本保存SDL_Texture *。它使用外部CLASS_Font通过SDL_Texture *&渲染纹理。当我尝试在构造函数中使用纹理时工作正常,但是当我尝试将文本呈现在它应该不起作用时,_Texture由于某种原因变为NULL而我无法绕过它。它不是局部变量,而是在构造函数中加载到CLASS_Text中的私有指针。 我在其他纹理的类中使用了类似的设计,它似乎在那里工作正常,所以我无法弄清楚为什么它不会在这种情况下。

在我声明像这样的对象之后渲染文本纹理时

text.Render();
SDL_RenderPresent( getRenderer() );
SDL_Delay( 2000 );

有效!这是在构造函数之外。但是当我稍后在Window的渲染函数中执行完全相同的操作时它不起作用,RenderCopy是否会以某种方式使纹理指针变为NULL?

这是Text类的'render function:

//Render the texture to its position on the screen
void CLASS_Text::Render(){
if( _isLoaded && (_Texture != NULL) ){
    SDL_Rect dest_rec{ _X, _Y, _Width, _Height };
    SDL_RenderCopy( getRenderer(), _Texture, NULL, &dest_rec );
} else{
    std::cout << DEBUG_TEXT << "Error rendering text object, it is not loaded properly! \n";
}
}

CLASS_Text

class CLASS_Text {

//Declare font classes friends so they can render to texture
friend class CLASS_Font;
friend class CLASS_BitMapFont;

//Hardware texture
SDL_Texture * _Texture;

//Text
std::string _Text;

//Rendering type
TEXT_RENDERTYPE _RenderType;

//Pointer to truetype font
CLASS_Font * _Font;
//Pointer to bitmap font
CLASS_BitMapFont * _BMFont;

//If flag is true then font is truetype, otherwise bitmap font
bool _isTrueType;

//Text color
SDL_Color _Color;
//Text backgroundcolor
SDL_Color _BackgroundColor;

//Flag indicates whether text has a solid color background
bool _hasBackGround;

//Coordinates
int _X;
int _Y;
//Size in pixels
int _Width;
int _Height;

//Misc variables
int _WordCount;
int _Length;

//Is the texture loaded
bool _isLoaded;

public:

//Constructor and destructor
CLASS_Text( std::string text, int x, int y, Abstract_Font * font, SDL_Color textcolor = {255,255,255}, std::string name = "Unnamed", TEXT_RENDERTYPE rendertype = TEXT_SOLID,
            bool background = false, SDL_Color bgcolor = {0,0,0} );
CLASS_Text( std::string text, int x, int y, STYLE_Text& style, std::string name = "Unnamed" ); //Using text style
~CLASS_Text();
//Free texture
void Free();

void ChangePosition( int x, int y );
void ChangeText( std::string newtext, bool rerender = false );
void ChangeFont( Abstract_Font * newfont );
void ChangeFont( std::string fontname );
void ChangeRenderType( TEXT_RENDERTYPE type );

//Sets the text color, requires regeneration
void SetColor( Uint8 red, Uint8 green, Uint8 blue );
//Set text bg on/off
void SetBackground( bool on_off );
void SetBgColor( Uint8 red, Uint8 green, Uint8 blue );

//Delete the previous texture and render new
void Load();

//Render the texture
void Render();
//Another render, which ignores the text objects'
//coordinates and renders to given location instead
void Render( int x, int y );

};

这是Load()函数

//Delete the previous texture and render new
void CLASS_Text::Load(){

//Free previous texture
Free();

//False until rendered succesfully
_isLoaded = false;

//Set texture to NULL
_Texture = NULL;

if( _isTrueType ){
    //If it is truetype the rendering happens with CLASS_Font's functions
    switch( _RenderType ){
    case TEXT_SOLID:
        if( TextDebug )
            std::cout << DEBUG_TEXT << "Rendering SOLID TrueType text \n";
        _Font->RenderText( _Texture, _Text, _Color, _Width, _Height, TEXT_SOLID );
        _isLoaded = true;
        break;
    case TEXT_SHADED:
        if( TextDebug )
            std::cout << DEBUG_TEXT << "Rendering SHADED TrueType text \n";
        _Font->RenderText_Shaded( _Texture, _Text, _Color, _BackgroundColor, _Width, _Height );
        _isLoaded = true;
        break;
    case TEXT_BLENDED:
        if( TextDebug )
            std::cout << DEBUG_TEXT << "Rendering BLENDED TrueType text \n";
        _Font->RenderText_Blended( _Texture, _Text, _Color, _Width, _Height );
        _isLoaded = true;
        break;
    default:
        std::cout << DEBUG_TEXT << "Invalid rendering type " << _RenderType << "\n";
    }
} else{
    if( TextDebug )
        std::cout << DEBUG_TEXT << "Rendering bitmap text \n";
    //Else it's bitmap and is rendered as such
}

if( TextDebug )
    std::cout << DEBUG_TEXT << "Rendered text object \n";

}

这是Font的文字渲染功能

void CLASS_Font::RenderText( SDL_Texture*& texture, std::string text, SDL_Color textcolor, int& width, int& height, TEXT_RENDERTYPE type, SDL_Color bgcolor ){
//If there's a texture already remove it
if( texture != NULL ){
    SDL_DestroyTexture( texture );
}
SDL_Surface * textSurface;
//Render text surface
switch( type ){
case TEXT_SOLID: //SOLID
    textSurface = TTF_RenderText_Solid( _Font, text.c_str(), textcolor );
    break;
case TEXT_SHADED: //SHADED
    textSurface = TTF_RenderText_Shaded( _Font, text.c_str(), textcolor, bgcolor );
    break;
case TEXT_BLENDED: //BLENDED
    textSurface = TTF_RenderText_Blended( _Font, text.c_str(), textcolor );
    break;
default:
    std::cout << DEBUG_FONT << "Error! Invalid rendering type \n";
    return;
}

//Check if the rendering was successfull
if( textSurface == NULL )
{
    std::cout << DEBUG_FONT << "Rendering text surface failed \n";
    return;
}

//Convert surface to display format
SDL_Surface * formattedSurface = SDL_ConvertSurfaceFormat( textSurface, Current_Window->getWindowPixelFormat(), NULL);

//Check the converted surface
if( formattedSurface == NULL )
{
    std::cout << DEBUG_FONT << "Converting text surface failed \n";
    return;
}

//Create texture from surface pixels
texture = SDL_CreateTextureFromSurface( getRenderer(), formattedSurface );
if( texture == NULL )
{
    std::cout << DEBUG_FONT << "Error creating text texture \n";
}
else
{
    width = textSurface->w;
    height = textSurface->h;

    if( TextDebug )
        std::cout << DEBUG_FONT << "Text surface successfully rendered and converted to texture \n";
}

//Free the temporary surfaces
SDL_FreeSurface( textSurface );
SDL_FreeSurface( formattedSurface );

return;
}

1 个答案:

答案 0 :(得分:0)

我认为你应该发布CLASS_Text的内容,因为理解为什么渲染不能从那里起作用是至关重要的。

但是,渲染函数可能会使用一些类成员,这些成员不会在您调用此函数的位置(例如坐标_X和_Y)中的确切位置进行初始化。

尝试在c&#39; tor结束时调用render。

相关问题