c++ - SDL C++ TTF 一段时间后随机消失(并且不会回来)

标签 c++ text sdl truetype

我正在使用 SDL 在 C++ 中制作类似 Yahtzee 的游戏(带有计分功能的骰子游戏)。这是我自己开发的第一个 SDL 项目。

问题可以描述为:当我启动游戏时,文本渲染良好。我可以记分并继续游戏。 但是,如果我等待几分钟(无论我是否还在玩),文本最终会停止渲染。我可以继续游戏,但不会显示任何文字,并且之前显示的文字不再显示。

我对这个问题完全困惑。直到现在才发生这种情况,因为之前的游戏都没有足够长的时间让我注意到。

注意:我偶然发现的一件事是软件和硬件渲染。我不知道为什么,但我尝试用硬件进行渲染,但这并没有解决问题。

另一个注意事项:代码的文本渲染部分接近结尾,在 for 循环中。

我迷路了,谁能帮帮我?我将发布代码,但它又长又荒谬,而且我认为这不是问题所在。

int GUI(State& GameState)
{       
apply_surface( 0, 0, background, screen ); //background
apply_surface( SCORE_X, SCORE_Y, scoreImage, screen ); //score card
apply_surface( KEPT_TRAY_X, KEPT_TRAY_Y, keptTray, screen ); //kept tray
apply_surface( TEXT_BOX_X, TEXT_BOX_Y, textBox, screen ); //text box

//Blatantly handles only the next event on the queue per frame
if( SDL_PollEvent( &event ) )
    eventHandler(GameState);

//place button
if(GameState.placeButtonClicked)
{
    apply_surface( GameState.buttonV[GameState.placeButtonClicked].x, GameState.buttonV[GameState.placeButtonClicked].y, clickedButton, screen );
}

//dice[1]
apply_surface( *GameState.diceV[1].pointX, *GameState.diceV[1].pointY, *GameState.diceV[1].surfaceValue, screen );

//dice[2]
apply_surface( *GameState.diceV[2].pointX, *GameState.diceV[2].pointY, *GameState.diceV[2].surfaceValue, screen );

//dice[3]
apply_surface( *GameState.diceV[3].pointX, *GameState.diceV[3].pointY, *GameState.diceV[3].surfaceValue, screen );

//dice[4]
apply_surface( *GameState.diceV[4].pointX, *GameState.diceV[4].pointY, *GameState.diceV[4].surfaceValue, screen );

//dice[5]
apply_surface( *GameState.diceV[5].pointX, *GameState.diceV[5].pointY, *GameState.diceV[5].surfaceValue, screen );

//rollButton
if(GameState.navV[1].clicked)
{
    apply_surface( GameState.navV[1].x, GameState.navV[1].y, rollButtonClicked, screen);
}
else
{
    apply_surface( GameState.navV[1].x, GameState.navV[1].y, rollButton, screen );
}

//endTurnButton
if(GameState.navV[2].clicked)
{
    apply_surface( GameState.navV[2].x, GameState.navV[2].y, endTurnButtonClicked, screen );
}
else
{
    apply_surface( GameState.navV[2].x, GameState.navV[2].y, endTurnButton, screen );
}

//displaying the clickedButton if a score is clicked
if(GameState.placeButtonClicked != 0)
{
    apply_surface( GameState.buttonV[GameState.placeButtonClicked].x, GameState.buttonV[GameState.placeButtonClicked].y, clickedButton, screen );
}

//displaying the scores for each "button" (each score segment)
for(int i = 1; i <= (GameState.buttonV.size() - 1); i++)
{
    if(GameState.buttonV[i].value >= 0)
    {
        message = TTF_RenderText_Solid( scoreFont, GameState.buttonV[i].valueString, scoreFontColor );
        apply_surface( GameState.buttonV[i].x + 10, GameState.buttonV[i].y + 2, message, screen );
    }
}

if( SDL_Flip( screen ) == -1 )
{
    return 1;
}

decGUICounters(GameState);

return 0;

}

最佳答案

我在浏览这个网站时找到了答案(花了一段时间!)。希望我能用我的发现帮助其他人:

问题是我反复将新文本设置为消息:

message = TTF_RenderText_Solid(etc);

当您调用TTF_RenderText_Solid()时,它会向表面指针(消息)返回一个新表面。但是,它不会释放旧表面包含的 RAM。

在将新内存分配给表面指针(消息)之前,您所要做的就是:SDL_FreeSurface(message);

这可以防止内存泄漏,进而防止您的内存被 CPU 随机删除。

TL;DR:

SDL_FreeSurface(message);

之前:

message = TTF_RenderText_Solid();

关于c++ - SDL C++ TTF 一段时间后随机消失(并且不会回来),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11232926/

相关文章:

c++ - 为什么 bind() 应该被弃用?

css - 定位多行文本中的特定行?

java - 导出具有多个值的变量?

c++ - "Iterator not incrementable"从 std::vector 删除项目时

ubuntu - SDL.h : No such file or directory

c++ - 在嵌套类型中保留 volatile

c++ - 为什么在此处返回 malloc 指针会导致 "HEAP CORRUPTION"空闲?

c++模板成员函数,根据模板参数更改参数

css - 图像旁边的多行垂直文本居中

c - 为什么尝试编译 SDL 时链接器出错?