SDL系统内存在我的游戏中增长得非常非常快
SDL system memory rising very very fast on my game
这是我的代码:
void apply_start_screen()
{
apply_surface( 0, 0, startingScreenBackground, screen );
apply_surface( ( SCREEN_WIDTH - startButton->w ) /2, 200, startButton, screen);
apply_surface( ( SCREEN_WIDTH - infoButton ->w ) /2, 450, infoButton, screen );
message = TTF_RenderText_Solid( font, "In The Jungle!", green );
apply_surface( ( SCREEN_WIDTH - message->w ) / 2, 25, message, screen );
SDL_Flip( screen );
}
主代码。。。
Mouse start( ( SCREEN_WIDTH - startButton->w ) /2, 200, 200, 200);
这个类只使用sdl-rect创建一个按钮,并检查它是否被按下
以及这里的问题:
while( quit == false )
{
apply_start_screen();
if( start.handle_event() == true )
{
currentState = 1;
}
}
当前状态只是转到下一个屏幕,不用担心。问题是apply_start_screen()每次使用都会使系统内存增加。我不明白。如果你多次渲染相同的图像/文本,会占用更多的内存吗?为什么?sdl曲面是动态分配的,但我仍然渲染相同的曲面,这意味着我不会分配更多的内存?有什么建议吗?谢谢你抽出时间。
解决方案:由于TTF_RenderText_Solid返回一个曲面,而SDL_surface是一个动态分配像素的结构,所以问题是在的每一帧上,apply_start_screen函数都在动态分配内存。有两种方法可以解决此问题:首先创建一个全局SDL_surface*消息,并执行以下操作:
void apply_start_screen()
{
apply_surface( 0, 0, startingScreenBackground, screen );
apply_surface( ( SCREEN_WIDTH - startButton->w ) /2, 200, startButton, screen);
apply_surface( ( SCREEN_WIDTH - infoButton ->w ) /2, 450, infoButton, screen );
message = TTF_RenderText_Solid( font, "In The Jungle!", green );
apply_surface( ( SCREEN_WIDTH - message->w ) / 2, 25, message, screen );
SDL_FreeSurface( message );
message = NULL;
}
或者简单地删除TTF_RendeText_Solid,这样就不会再创建/返回曲面,如下所示:
void apply_start_screen()
{
apply_surface( 0, 0, startingScreenBackground, screen );
apply_surface( ( SCREEN_WIDTH - startButton->w ) /2, 200, startButton, screen);
apply_surface( ( SCREEN_WIDTH - infoButton ->w ) /2, 450, infoButton, screen );
apply_surface( ( SCREEN_WIDTH - message->w ) / 2, 25, message, screen );
}
main...
message = TTF_RenderText_Solid( font, "In The Jungle!", green );
rest of program...
希望我能帮上忙:)
相关文章:
- OpenGL大的3D纹理(>2GB)非常慢
- 为什么需要复制构造函数,在哪些情况下它们非常有用
- 在C++游戏中与库存系统作斗争
- 换位表导致测试失败(但在游戏中运行良好)
- 为什么std::互斥需要很长的、非常不规则的时间来共享
- Craps游戏问题,忽略if语句
- 矢量下标超出SFML游戏中的范围
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- G++ C++17 类模板参数推导在非常特殊的情况下不起作用
- 使用浮点数和双精度数的非常小数字的数学
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 游戏体验和升级
- 我的代码运行良好,但在游戏循环中中断
- 如何使用 SFML 在贪吃蛇游戏中定义游戏结束?
- 我可以使用任何好的逻辑来阻止计算机将 O 放在井字游戏中的现有 X 上
- 将鼠标和键盘输入发送到 unity3d 游戏 (Rust)
- 寻找非常精确的delta时间计数器.想要限制闪电战游戏中的帧数
- SDL系统内存在我的游戏中增长得非常非常快
- 我的简单游戏的计时器非常关闭...C++