简体   繁体   English

慢速C ++ DirectX 2D游戏

[英]Slow C++ DirectX 2D Game

I'm new to C++ and DirectX, I come from XNA. 我是C ++和DirectX的新手,我来自XNA。 I have developed a game like Fly The Copter . 我开发了像Fly The Copter这样的游戏。 What i've done is created a class named Wall. 我所做的是创建了一个名为Wall的类。 While the game is running I draw all the walls. 在比赛开始的时候,我画了所有的墙。 In XNA I stored the walls in a ArrayList and in C++ I've used vector. 在XNA中,我将墙存储在ArrayList中,而在C ++中我使用了vector。 In XNA the game just runs fast and in C++ really slow. 在XNA中,游戏运行速度很快,在C ++中运行速度非常慢。 Here's the C++ code: 这是C ++代码:

void GameScreen::Update()
{
    //Update Walls
    int len = walls.size();
    for(int i = wallsPassed; i < len; i++)
    {
        walls.at(i).Update();
        if (walls.at(i).pos.x <= -40)
            wallsPassed += 2;
    }
}

void GameScreen::Draw()
{
    //Draw Walls
    int len = walls.size();
    for(int i = wallsPassed; i < len; i++)
    {
        if (walls.at(i).pos.x < 1280)
            walls.at(i).Draw();
        else
            break;
    }
}

In the Update method I decrease the X value by 4. In the Draw method I call sprite->Draw (Direct3DXSprite). 在Update方法中,我将X值减小4.在Draw方法中,我调用sprite-> Draw(Direct3DXSprite)。 That the only codes that runs in the game loop. 这是游戏循环中唯一运行的代码。 I know this is a bad code, if you have an idea to improve it please help. 我知道这是一个糟糕的代码,如果你有想法改进它,请帮忙。 Thanks and sorry about my english. 谢谢,抱歉我的英语。

Try replacing all occurrences of at() with the [] operator. 尝试使用[]运算符替换所有出现的at()。 For example: 例如:

 walls[i].Draw();

and then turn on all optimisations. 然后打开所有优化。 Both [] and at() are function calls - to get the maximum performance you need to make sure that they are inlined, which is what upping the optimisation level will do. []和at()都是函数调用 - 以获得确保它们被内联所需的最大性能,这是优化级别的优势。

You can also do some minimal caching of a wall object - for example: 您还可以对墙对象进行一些最小的缓存 - 例如:

 for(int i = wallsPassed; i < len; i++)
 {
    Wall & w = walls[i]; 
    w.Update();
    if (w.pos.x <= -40)
        wallsPassed += 2;
 }

Try to narrow the cause of the performance problem (also termed profiling). 尝试缩小性能问题的原因(也称为性能分析)。 I would try drawing only one object while continue updating all the objects. 我会尝试只绘制一个对象,同时继续更新所有对象。 If its suddenly faster, then its a DirectX drawing problem. 如果它突然更快,那么它的DirectX绘图问题。

Otherwise try drawing all the objects, but updating only one wall. 否则尝试绘制所有对象,但只更新一个墙。 If its faster then your update() function may be too expensive. 如果它更快,那么你的update()函数可能太贵了。

  • How fast is 'fast'? “快”有多快?
  • How slow is'really slow'? 慢得多慢?
  • How many sprites are you drawing? 你画了多少精灵?
  • How big is each one as an image file, and in pixels drawn on-screen? 每个人作为图像文件有多大,以及在屏幕上绘制的像素?
  • How does performance scale (in XNA/C++) as you change the number of sprites drawn? 当您更改绘制的精灵数量时,性能如何扩展(在XNA / C ++中)?
  • What difference do you get if you draw without updating, or vice versa 如果你在没有更新的情况下绘制,你会得到什么区别,反之亦然

Maybe you just have forgotten to turn on release mode :) I had some problems with it in the past - I thought my code was very slow because of debug mode. 也许你只是忘了打开发布模式:)我以前遇到过一些问题 - 我认为我的代码因为调试模式而非常慢。 If it's not it, you can have a problem with rendering part, or with huge count of objects. 如果不是它,您可能会遇到渲染零件或大量对象的问题。 The code you provided looks good... 你提供的代码看起来不错......

Have you tried multiple buffers (aka Double Buffering ) for the bitmaps? 您是否为位图尝试了多个缓冲区(又称双缓冲 )?

The typical scenario is to draw in one buffer, then while the first buffer is copied to the screen, draw in a second buffer. 典型的情况是在一个缓冲区中绘制,然后在将第一个缓冲区复制到屏幕时,在第二个缓冲区中绘制。

Another technique is to have a huge "logical" screen in memory. 另一种技术是在内存中拥有一个巨大的“逻辑”屏幕。 The portion draw in the physical display is a viewport or view into a small area in the logical screen. 在物理显示中绘制的部分是视口视图逻辑屏幕中的小区域。 Moving the background (or screen) just requires a copy on the part of the graphics processor. 移动背景(或屏幕)只需要图形处理器的副本。

You can aid batching of sprite draw calls. 您可以帮助批量精灵绘制调用。 Presumably Your draw call calls your only instance of ID3DXSprite::Draw with the relevant parameters. 假设您的绘图调用使用相关参数调用您唯一的ID3DXSprite :: Draw实例。

You can get much improved performance by doing a call to ID3DXSprite::Begin (with the D3DXSPRITE_SORT_TEXTURE flag set) and then calling ID3DXSprite::End when you've done all your rendering. 通过调用ID3DXSprite :: Begin(设置了D3DXSPRITE_SORT_TEXTURE标志),然后在完成所有渲染后调用ID3DXSprite :: End,可以大大提高性能。 ID3DXSprite will then sort all your sprite calls by texture to decrease the number of texture switches and batch the relevant calls together. 然后,ID3DXSprite将按纹理对所有精灵调用进行排序,以减少纹理开关的数量并将相关调用一起批处理。 This will improve performance massively. 这将大大提高性能。

Its difficult to say more, however, without seeing the internals of your Update and Draw calls. 但是,如果没有看到Update和Draw调用的内部结构,很难说更多。 The above is only a guess ... 以上只是一个猜测......

To draw every single wall with a different draw call is a bad idea. 用不同的绘制调用来绘制每一面墙都是一个坏主意。 Try to batch the data into a single vertex buffer/index buffer and send them into a single draw. 尝试将数据批处理到单个顶点缓冲区/索引缓冲区中,并将它们发送到单个绘图中。 That's a more sane idea. 这是一个更明智的想法。

Anyway for getting an idea of WHY it goes slowly try with some CPU and GPU (PerfHud, Intel GPA, etc...) to know first of all WHAT's the bottleneck (if the CPU or the GPU). 无论如何,要想知道为什么它会慢慢尝试使用一些CPU和GPU(PerfHud,英特尔GPA等等)首先要知道什么是瓶颈(如果是CPU或GPU)。 And then you can fight to alleviate the problem. 然后你可以战斗来缓解这个问题。

The lookups into your list of walls are unlikely to be the source of your slowdown. 查找墙列表不太可能成为您减速的源头。 The cost of drawing objects in 3D will typically be the limiting factor. 以3D形式绘制对象的成本通常是限制因素。

The important parts are your draw code, the flags you used to create the DirectX device, and the flags you use to create your textures. 重要的部分是您的绘图代码,用于创建DirectX设备的标志以及用于创建纹理的标志。 My stab in the dark... check that you initialize the device as HAL (hardware 3d) rather than REF (software 3d). 我在黑暗中刺伤...检查你将设备初始化为HAL(硬件3d)而不是REF(软件3d)。

Also, how many sprites are you drawing? 还有,你画了多少精灵? Each draw call has a fair amount of overhead. 每次抽奖都有相当大的开销。 If you make more than couple-hundred per frame, that will be your limiting factor. 如果你每帧超过几百,这将是你的限制因素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM