Time

Hi, I am trying to draw something on screen every 2 minutes AFTER it is picked up. So, when my character picks this item up, it will draw it in a different random position 2 minuets later. But I am having a trouble. I am using SDL and this is how I am try to do it.

1
2
3
4
5
6
7
8
9
10
Uint32 oldTime = SDL_GetTicks();
while(quit==false)//main game loop
{
   if(SDL_GetTicks() - oldTime > 120000)
  {
     drawthing();
     oldTime = SDL_GetTicks();
  } 

}


Now, obviously this does not work as I want to, because it will only draw each loop, and it will only draw if that condition is true, so it will only be true for a split second I thnk?...anyway, I was thinking, would it be good to have something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Uint32 oldTime = SDL_GetTicks();
while(quit==false)//main game loop
{
   if(drawthing)
   {
     drawIt(); 
   }
   if(SDL_GetTicks() - oldTime > 120000)
  {
    drawthing=true;
    oldTime = SDL_GetTicks()
  } 

  if(player.colllides(thingtoodraw))
   {
    drawthing=false;
   }

}


Is my second way a good idea?
Last edited on
Two parts of your revised code that I don't like are:
1
2
3
4
if(drawthing)
   {
     drawIt(); 
   }

Imagine having one of those for every object on your screen. Unless of course this isn't literally what you are writing and is only an illustration, in which case it looks good.
And
This "thing" should be an object at this point of complexity.

As a personal preference I would make Lines 8 through 12 into a function template. But that's because I assume you'll be using it more then just this once.
This was only an illustration yes, I am going to have an array of size 4. Each cell corresponds to a type of item. And I will draw a specific item from this array depending on which powerup is needed at the time(armour,health,lives,weapons) every 2 minutes. What would be the benefit of having it as a function template here? Why would using it more than once merit doing that? Thanks.
After you turn "thing" into an object, having this section of code as a function template would allow you to pass it, or any other object in and have the function return a boolean value. Anytime you find yourself about to copy and paste a section of code to reuse it in another part of the same program you should consider instead how you would make that section of code into a function, this makes for cleaner looking code so it is easier to trouble shoot and\or read (less lines to dig through) and IMHO adds to the fun of the project.

This is all predicated on the idea that this game will be expanded and that you're using this for more then one object. Otherwise a member funtion for "thing"s class would be just fine.
Makes sense, thanks for the help.
Topic archived. No new replies allowed.