Name: Anonymous 2011-11-12 18:05
Hey there /prog/, let's talk about optimization or different things we can do to speed up our applications.
Currently I'm writing a game, and it usually takes up around ~50% of my CPU usage on the bigger maps. I have a vector that contains all of my blocks, enemies, and other things that should be drawn on the screen.
In my draw thread, it could look like this:
player.draw();
for (int bi = 0; bi < blocks.size(); bi++)
{
if (InCameraView(blocks[bi]))
{
draw(blocks[bi]);
}
}
In my update thread, it looks like:
player.update();
for (int bi = 0; bi < blocks.size(); bi++)
{
if (InCameraView(blocks[bi]))
{
player.TouchGround(blocks[bi]);
for (int ei = 0; ei < enemies.size(); ei++)
{
enemies.update();
enemies[ei].TouchGround(blocks[bi]);
}
}
}
Obviously only an example, and I typically have lots of more objects that are currently being drawn on the screen. I also understand that it's usually very bad to have nested for loops, but I'm not exactly sure how else I could handle this.
Also, for rendering I have a lot of "switch" blocks to determine what I should render or update.
switch (CurrentWorld)
{
case Worlds::MainMenu:
worldMainMenu.draw();
break
case Worlds::Level:
worldLevel.draw();
break;
}
Also, for updating thread:
while (true)
{
while (resetTime >= gameTime)
{
resetTime -= gameTime;
switch (CurrentWorld)
{
case Worlds::MainMenu:
mainMenu.update();
break;
case Worlds::Level:
worldLevel.update();
break;
}
}
}
How do you guys like to optimize your applications to run better and use less processing power?
Currently I'm writing a game, and it usually takes up around ~50% of my CPU usage on the bigger maps. I have a vector that contains all of my blocks, enemies, and other things that should be drawn on the screen.
In my draw thread, it could look like this:
player.draw();
for (int bi = 0; bi < blocks.size(); bi++)
{
if (InCameraView(blocks[bi]))
{
draw(blocks[bi]);
}
}
In my update thread, it looks like:
player.update();
for (int bi = 0; bi < blocks.size(); bi++)
{
if (InCameraView(blocks[bi]))
{
player.TouchGround(blocks[bi]);
for (int ei = 0; ei < enemies.size(); ei++)
{
enemies.update();
enemies[ei].TouchGround(blocks[bi]);
}
}
}
Obviously only an example, and I typically have lots of more objects that are currently being drawn on the screen. I also understand that it's usually very bad to have nested for loops, but I'm not exactly sure how else I could handle this.
Also, for rendering I have a lot of "switch" blocks to determine what I should render or update.
switch (CurrentWorld)
{
case Worlds::MainMenu:
worldMainMenu.draw();
break
case Worlds::Level:
worldLevel.draw();
break;
}
Also, for updating thread:
while (true)
{
while (resetTime >= gameTime)
{
resetTime -= gameTime;
switch (CurrentWorld)
{
case Worlds::MainMenu:
mainMenu.update();
break;
case Worlds::Level:
worldLevel.update();
break;
}
}
}
How do you guys like to optimize your applications to run better and use less processing power?