Should logic and drawing be split even if it doubles the loops?


#1

Recently I was calculating the points on a circle to draw, and then later drawing those points. It occurred to me that I was running one loop to create the points, and then the same sized loop to draw them. I thought that if I merged the two operations together into one loop, then I could eliminate one loop entirely.

My problem with this is that I was taught to separate parts, like logic first and then drawing, but in cases like this is seems to make sense to do both together to save execution time, which I imagine could matter in large programs.

Would SDL operations for game loops benefit from merging the logic and drawing loops in cases where they required the same loop count, or is it better to keep the sections separate? I simply want to know so that I develop good programming habits, but at the same time don’t bog down SDL and get a low frame rate.


#2

First of all you are probably talking in the 0.001’s of milliseconds to loop 1000+ times and you will have16.66ms for every update if your running 60FPS.

I would personally keep the update and draw logic separate otherwise things may get complicated when you add more logic like collisions etc.

Go for Readability first and then optimise if you have problems


#3

That sounds like good advice. The way I have it set up now I think it defaults to 60 FPS. I’m glad you said that I can keep the parts separate, because combining things was really starting to make it hard to read.

I’ll remember to stay separate unless there are performance problems. Thanks!


#4

Separating the game logic can help, by calculating everything first, you ensure you are always rendering a full frame. However, it is not necessary. Technically you can put everything in a single loop, but it gets difficult to manage with a large project. As far as performance of a separate loop, that is not enough to matter.


#5

What do you mean about how separating will ensure the rendering of a full frame? Sorry, I’m still rather new to SDL and programming. I thought the frame was rendered in a single command. Here are two compressed code examples (separate and not), please explain using them and maybe it will make more sense to me. Thanks!

// Non-separated code
while (!quit)
{
  Point p[1000];
  SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  SDL_RenderClear(renderer);

  SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  for (int i = 0; i < 1000; i++)
  {
    p[i] = {i, i};
    SDL_RenderDrawPoint(renderer, p[i].x, p[i].y);
  }

  SDL_RenderPresent(renderer);
}

// Separated Code
while (!quit)
{
  Point p[1000];
  for (int i = 0; i < 1000; i++)
    p[i] = {i, i};

  SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  SDL_RenderClear(renderer);
  SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  for (int i = 0; i < 1000; i++)
    SDL_RenderDrawPoint(renderer, p[i].x, p[i].y);
  SDL_RenderPresent(renderer);
}

#6

You are splitting hairs in these examples. Just keep the majority of your logic and drawing separate. If you have to have a for loop in your rendering it’s not going to blow anything up though.


#7

I was just trying to make the example as simple as possible to illustrate my point. I realize that the example I posted probably wouldn’t see a major performance shift, but I’m asking if the concept would make a difference in a full game where such loops might be much larger and more plentiful.


#8

As someone said before, just keep them separated within reason, but if it makes it a lot more difficult or unreadable, then a little bit of processing mixed with rendering is fine and unavoidable. There’s no way for me to illustrate where the line is, so experience will make it obvious but I will say you have nothing to worry about.


#9

Good, I just wanted to see if it were going to be a problem in general. I didn’t want to start out on the wrong foot and end up having to change all my code later down the road.


#10

Looking at the example you provided I would suggest not creating the points everytime.
Also in your example you only ever need to loop to draw as your not doing anything else with the points.

If you were to update the positions of the points then yes you should do this seperately.

Example
Point p[1000];
for (int i = 0; i < 1000; i++)
{
p[i] = {i, i};
}

while (!quit)
{
// Draw
}


#11

Sorry, I forgot to include that the points will be changed, or at least will possibly be changed each time. Speaking of which, should I add a boolean to mark if any points change, and only draw if they do? Or is it fine to simply draw every frame, regardless?


#12

In your case you will need to redraw points as clearing the renderer will remove previous draw frame.

Message me a copy of what you have so far and I can take a look. I would like to get an idea of what your at.