Slow C++ DirectX 2D Game
c++, directx, graphics, optimization, sprite
Solution
Try replacing all occurrences of at() with the [] operator. 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.
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;
}
Problem
I'm new to C++ and DirectX, I come from XNA. I have developed a game like Fly The Copter. What i've done is created a class named 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. In XNA the game just runs fast and in C++ really slow. Here's 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 X value by 4. In the Draw method I call 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.