Slow game C ++ DirectX 2D

I am new to C ++ and DirectX, I came from XNA. I developed a game like Fly The Copter . What I did was created by the Wall class. While the game is running, I draw all the walls. In XNA, I saved the walls in ArrayList, and in C ++ I used a vector. In XNA, the game is fast and very slow in C ++. Here is the C ++ code:

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 value of X by 4. In the Draw method, I call sprite-> Draw (Direct3DXSprite). These are the only codes that run in the game loop. I know this is bad code, if you have an idea to improve it, please help. Thanks and sorry for my english.

+4
source share
8 answers

Try replacing all occurrences of at () with the [] operator. For instance:

  walls[i].Draw(); 

and then turn on all optimizations. Both [] and at () - function calls - to get the maximum performance necessary to ensure that they are built-in, which will help increase the level of optimization.

You can also perform 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; } 
+8
source

Try narrowing down the cause of a performance problem (also called profiling). I would try to draw only one object while continuing to update all objects. If it suddenly accelerates, then this is a problem with the DirectX pattern.

Otherwise, try to draw all the objects, but update only one wall. If it is faster, then the update () function may be too expensive.

+2
source
  • How fast is "fast"?
  • How slowly works slower?
  • How many sprites do you draw?
  • How large are each of them as an image file and in pixels on the screen?
  • How does the performance scale (in XNA / C ++) change when the number of drawn sprites changes?
  • What difference does it make if you draw without updating, or vice versa
+1
source

Perhaps you just forgot to turn on release mode :) I had some problems with it in the past - I thought my code was very slow due to debug mode. If this is not the case, you may have a problem with part of the rendering or with a huge number of objects. The code you provided looks good ...

+1
source

Have you tried several buffers (aka Double Buffering) for bitmaps?

A typical scenario is to draw in one buffer, then when the first buffer is copied to the screen, draw a second buffer.

Another method is to have a huge β€œlogical” screen in memory. The part displayed on the physical display is a viewport or a presentation in a small area on the logical screen. To move the background (or screen) requires only a copy from the side of the GPU.

0
source

You can help finalize your sprite drawing calls. Presumably, your draw call invokes your only instance of ID3DXSprite :: Draw with the appropriate parameters.

You can get much improved performance by calling ID3DXSprite :: Begin (with the D3DXSPRITE_SORT_TEXTURE flag set) and then calling ID3DXSprite :: End when you have done all your rendering. ID3DXSprite then sorts all of your sprite calls by texture to reduce the number of texture switches and batch matching calls. This will greatly improve performance.

It is hard to say more, however, without seeing the internal calls of your Update and Draw calls. Above is just a hunch ...

0
source

To paint each wall with a different call to paint is a bad idea. Try to combine the data into one buffer of the vertex buffer / index and send them in one draw. This is a more reasonable idea.

In any case, in order to get an idea of ​​WHY, there is a slow attempt with some processors and GPUs (PerfHud, Intel GPA, etc.), in the first place to know THAT this is a bottleneck (if the processor or GPU) . And then you can fight to alleviate the problem.

0
source

Searching your list of walls is unlikely to be the source of your slowdown. The cost of drawing objects in 3D will usually be a limiting factor.

The important parts are the drawing code, the flags that you used to create the DirectX device, and the flags that you use to create your textures. My bump in the dark ... check that you initialize the device as HAL (hardware 3D) and not REF (3d software).

Also, how many sprites do you draw? Each call to draw has a fair amount of overhead. If you make more than a couple hundred per frame, this will be your limiting factor.

0
source

Source: https://habr.com/ru/post/1299689/


All Articles