gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Timer + vBlanks (kind of related to countdown timer thread)

#17860 - darknet - Tue Mar 16, 2004 4:20 am

Alright, I have a dilemma (As usual right?), I was weeding through the threads and came to the countdown thread. While the info there really helped me, it also opened up a number of questions. Here goes:

My project consists of a number of timed mini-games. For example, a person will have 30 seconds to complete a mini-game. If 30 seconds go by, a screen pops up showing that time is up. Think WarioWare MicroGames.

In any case, if the person does finish the game, I do not want the timer stuff to appear at all obviously. Otherwise, when time runs out, display the message.

The way I have it set up right now, If i run my timer function, nothing else is allowed to run concurrently, i.e. the system just waits till time is up and displays the message, not allowing for any other interaction.

How should this kind of thing be structured / accomplished? I read the countdown timer thread and came up with this function,

Code:

void setAndRunFor(int amountOfTime) {

   //set up a frame variable (=60)
   unsigned short frame = 60;
   
   //Every VBlank, decrement a 'frame' counter.
   while(amountOfTime!=0) {

      for(int i = frame; i>=0; i--) {
         vBlank();
      }
      amountOfTime--;
      frame = 60;
   }
 
  //draw the time is up graphic here
  setMode(MODE_3 | BG2_ENABLE);
  drawMode3Background(videoBuffer, timesUp_Bitmap);
}


where vBlank() is defined as so:

Code:

void vBlank() { while(REG_VCOUNT != 160); }


For testing purposes, I am just calling that function in random places throughout different mini-games, however nothing allows the game control and the timer to coexist. In addition, how would I give control back to the main program once the timer is up and the screen is displayed?

Sorry for the long winded post, thanks for reading + any answer is appreciated.

#17861 - tepples - Tue Mar 16, 2004 4:53 am

Each minigame will look like the following pseudocode:
Code:

void minigame_shoot_the_hamsters(void)
{
  /* omitted: init minigame state and graphics */
  u16 time_left = 1800; /* typical for Mario Party; WarioWare uses 192 */
  s8 done = 0;

  while(!done)
  {
    vBlank();
    /* omitted: display code */
    /* omitted: one cycle of the game loop, setting done to FAILURE or
       SUCCESS, where FAILURE and SUCCESS are non-zero constants */
    time_left--;
    if(time_left == 0)
      done = FAILURE; /* or SUCCESS if goal is "stay alive for 30 seconds" */
  }
}

In other words, run one cycle of the game loop after each 'vBlank()' in your code.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#17870 - darknet - Tue Mar 16, 2004 7:02 am

you're the best, that works beautifully, and now i can scrap that over complicated timer class!