渲染对象时的性能(2d) [英] Performance when rendering object(2d)

查看:45
本文介绍了渲染对象时的性能(2d)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在小型游戏中注意到,当屏幕上有多个对象时,我会非常快地获得非常低的fps.

妨碍表演的两个对象是敌人,是玩家还是敌人发射的射击.

遍历向量并移动所有敌人后,我再次循环向量并渲染每个敌人.

I''ve noticed in my small game that I get very low fps very quickly when multiple objects are on the screen.

The two objects that hogs performance are enemies and shots fired by either the player or the enemy.

After looping through a vector and moving all enemies I loop the vector again and renders each enemy.

void renderEnemy(){
for (int i = 0;i<enemyVector.size();i++){
enemyVector[i].drawEnemy();
}
};





void enemy::drawEnemy(){
const string playerFilename       = "SmallMine.tif";
const int    DROPS_X_FRAME_COUNT = 1;
const int    DROPS_Y_FRAME_COUNT = 1;
VGCImage     player               = VGCDisplay::openImage(
    playerFilename,
    DROPS_X_FRAME_COUNT,
    DROPS_Y_FRAME_COUNT
);
const VGCVector     frameIndex = VGCVector(0, 0);
const VGCVector     position   = VGCVector(xPos, yPos);
const VGCAdjustment adjustment = VGCAdjustment(0.0, 0.0);
VGCDisplay::renderImage(player, frameIndex, position, adjustment);
VGCDisplay::closeImage(player);



};

如您所见,drawEnemy被调用很多,这减慢了我的游戏的速度,但是我不确定如何避免多次调用此函数.



};

As you can see drawEnemy gets called quite a lot which slows down my game but I''m not sure on how to avoid this function getting called so many times.

推荐答案

问题不在于您经常调用此函数,而是每次您一次又一次地加载文件时.从磁盘读取文件是一个非常慢的操作,应该只执行一次:您应该在程序开始时(或在加载级别时)加载文件并保留在内存中.

您正在使用哪个库?它不是DirectX也不是OpenGL.
The problem is not that you are calling this function very often but rather that each time you are loading the file again and again. Reading a file from disk is a very slow operation and should be performed only once: you should load your file at the beginning of your program (or when your level is loaded) and keep in memory.

Which library are you using ? It''s not DirectX nor OpenGL.


这篇关于渲染对象时的性能(2d)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆