I'm curious about any steps I could take to to increase the efficiency and speed of my game.
Currently I have a vector for each set of objects I'd like to draw on the screen. For example, coins, enemies, blocks, etc. Here's an example for how I have things setup:
void WorldLevel::draw()
{
for (int bi = 0; bi < blocks.size(); bi++)
{
if (InCameraView(blocks[bi]))
{
App.Draw(blocks[bi]);
}
}
for (int ei = 0; ei < enemies.size(); ei++)
{
if (InCameraView(enemies[ei]))
{
enemies[ei].draw();
}
}
}
void WorldLevel::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[ei].update();
enemies[ei].TouchGround(blocks[bi]);
}
}
}
}
I think the biggest sacrifice is looping through the blocks vector, because it's usually pretty large. I also understand that it's probably bad practice to use nested for loops, but I'm not exactly sure how else to achieve what I'm trying to do.
I was thinking about making a second vector that contains all of the blocks that are on screen (blocks_drawable), and loop through that vector anytime I need the objects to interact with the blocks. However, I assume I would still need to loop through the blocks to determine the blocks on screen, so I'm not too sure if there would be a noticeable difference in performance.
What could I do to make things run better? The game feels very smooth whenever the blocks vector isn't too large, but things start to slow down whenever the vector becomes larger.
Thanks for any help!