Game loop

Hi

I have this as my game loop (WIn32 VC++5), but the performance is horribly
slow. Do I need to use the events, as I (ultimately!) want a nice tight
main loop which blits, checks the keys and checks the mouse. Am I doing
something wrong?

while ( ! done )
{

for (j=0; j<=SCROLL_HEIGHT; j++)
for (i=0; i<=SCROLL_WIDTH; i++)
{
dest.x = 64i;
dest.y = 64
j;
dest.w = 64;
dest.h = 64;
SDL_BlitSurface(tiles[map[x1+i][y1+j]], NULL, screen, &dest);
}

SDL_Flip(screen);

keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_ESCAPE] == SDL_PRESSED)
{
done = 1;
}

if (keys[SDLK_RIGHT] == SDL_PRESSED)
{
if (xpos+1<MAP_WIDTH)
{
xpos ++;
}

if (x2+1<MAP_WIDTH)
{
x1++;
x2++;
}
}

/* Handle events */
while ( SDL_PollEvent(&event) )
{
switch (event.type)
{
case SDL_QUIT:
done = 1;
break;

 }

}
}
}

cheers
Mike

I have this as my game loop (WIn32 VC++5), but the performance is horribly
slow. Do I need to use the events, as I (ultimately!) want a nice tight
main loop which blits, checks the keys and checks the mouse. Am I doing
something wrong?

I have to go eat, but in general this is going to be a relatively slow loop,
because you are doing a triple table lookup and are probably blitting in
software.
Here are some general optimization tricks that might help this particular
loop:

for (j=0; j<=SCROLL_HEIGHT; j++)
for (i=0; i<=SCROLL_WIDTH; i++)
{
dest.x = 64i;
dest.y = 64
j;
dest.w = 64;
dest.h = 64;
SDL_BlitSurface(tiles[map[x1+i][y1+j]], NULL, screen, &dest);
}

y = y1;
dest.x = 0;
dest.y = 0;
dest.w = 64;
dest.h = 64;
for (j=0; j<=SCROLL_HEIGHT; j++) {
      o = y1*SCROLL_WIDTH+x1;
      // Since you know SCROLL_WIDTH is divisible by four, unroll... 
      // (A good optimizing compiler might already do this)
      for (i=0; i<=SCROLL_WIDTH/4; i++) {
           // Since you know you won't clip the edges, use MiddleBlit()
           SDL_MiddleBlit(tiles[map[o++]], NULL, screen, &dest);
           dest.x += 64;
           SDL_MiddleBlit(tiles[map[o++]], NULL, screen, &dest);
           dest.x += 64;
           SDL_MiddleBlit(tiles[map[o++]], NULL, screen, &dest);
           dest.x += 64;
           SDL_MiddleBlit(tiles[map[o++]], NULL, screen, &dest);
           dest.x += 64;
      }
      dest.w += 64;
}

SDL_Flip(screen);

For further optimization, make tiles[] into a single large surface
and blit from an offset within it.
This should help a bit. Anybody? Gotta run! :slight_smile:

See ya!
-Sam Lantinga (slouken at devolution.com)–
Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

I have this as my game loop (WIn32 VC++5), but the performance is horribly
slow. Do I need to use the events, as I (ultimately!) want a nice tight
main loop which blits, checks the keys and checks the mouse. Am I doing
something wrong?

I have to go eat, but in general this is going to be a relatively slow loop,
because you are doing a triple table lookup and are probably blitting in
software.
Here are some general optimization tricks that might help this particular
loop:

thanks, this helps me a lot too :slight_smile: … although my K6-300 under Linux can
probably optimize it itself with -O6 -mamdk6… egcs automatically unrolls
loops and all.

I’ll have Richard send you a 3.0.1 boxed set when it comes out. It’s got
Maelstrom and SDL on it, plus egcs and gcc 2.7.2.3, plus a bunch of other
neat stuff.

You’re still in Pleasanton, CA, right? I’ll probably be back for good
next month or the month after, and I’ll be living in that area. We should
get together and have lunch or something.On Tue, 24 Nov 1998, Sam Lantinga wrote:


Scott M. Stone <sstone at pht.com, sstone at pht.co.jp>
Head of TurboLinux Development/Systems Administrator
Pacific HiTech, Inc (USA) / Pacific HiTech, KK (Japan)

You might want to move the SDL_PollEvent() loop before the keystate check
because the SDL_PollEvent() function updates the current keystate.
Your application will have better keyboard response that way. :slight_smile:

keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_ESCAPE] == SDL_PRESSED)
{
done = 1;
}

/* Handle events */
while ( SDL_PollEvent(&event) )
{
}

See ya!
-Sam Lantinga (slouken at devolution.com)–
Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

Fixing typos… I haven’t tested this code

y = y1;

[delete ^^^^]

dest.x = 0;
dest.y = 0;
dest.w = 64;
dest.h = 64;
for (j=0; j<=SCROLL_HEIGHT; j++) {
      o = y1*SCROLL_WIDTH+x1;
      // Since you know SCROLL_WIDTH is divisible by four, unroll... 
      // (A good optimizing compiler might already do this)
      for (i=0; i<=SCROLL_WIDTH/4; i++) {
           // Since you know you won't clip the edges, use MiddleBlit()
           SDL_MiddleBlit(tiles[map[o++]], NULL, screen, &dest);
           dest.x += 64;
           SDL_MiddleBlit(tiles[map[o++]], NULL, screen, &dest);
           dest.x += 64;
           SDL_MiddleBlit(tiles[map[o++]], NULL, screen, &dest);
           dest.x += 64;
           SDL_MiddleBlit(tiles[map[o++]], NULL, screen, &dest);
           dest.x += 64;
      }
      dest.w += 64;

[Oops dest.y += 64;]

}

SDL_Flip(screen);

For further optimization, make tiles[] into a single large surface
and blit from an offset within it.
This should help a bit. Anybody? Gotta run! :slight_smile:

See ya!
-Sam Lantinga (slouken at devolution.com)–
Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

Good day,
Developers. Sorry for this off topic question, but this is so new to
me (sdl, game developing, … ):

I have a crazy idea to put 3 games in 1. So each have it's own game 

loop. Now, I have a menu trough witch user can start game1, game2 or
game3 with its own loop. So I think that i should call menu loop from
main loop, and when user selects one of the games, I should call game
loop. Correct, or am I brain dead?

// this is in main - int main( int argc, char *argv[] ) func (main 

loop), calls menu loop

int done = 0; // exit from app when done == 10; 1 for game1, 2 for 

game2, 3 for game3, 10 for exit

while ( done != 10 )
{
   if ( done == 0 )
      done = showMainMenu(); // main menu loop returns 1, 2 or 3 if 

games are selected, or 10 for exit

   if ( done == 1 )
      done = startGame1(); // game1 loop returns 0 when loop exits

   if ( done == 2 )
      done = startGame2(); // game2 loop returns 0 when loop exits

   if ( done == 3 )
      done == startGame3(); // game3 loop returns 0 when loop exits
}

// main menu loop

int showMainMenu()
{
   // start while loop for menu and then...
   // if user selected game1...
   return 1;

   // if user selected game2...
   return 2;

   // if user selected game3...
   return 3;

   // if user selected exit...
   return 10;
}

// game1 loop

int game1()
{
   // start while loop for game1 and then...
   // if user selected exit...
   return 0;
}

... is this OK?

Thanks for help,
Vladimir.-- 

"This is it… This is where I belong… "

Vladimir Djokic wrote:

     done = startGame2(); // game2 loop returns 0 when loop exits

  if ( done == 3 )
     done == startGame3(); // game3 loop returns 0 when loop exits

Watch for this typo, it’ll still call startGame3(), but wont set done to the
value returned; it’d do a comparison as you know, and go back to running the
same game again.

Cheers,
John
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 250 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20040217/ab64da71/attachment.pgp

John Drinkwater wrote:

Vladimir Djokic wrote:

     done = startGame2(); // game2 loop returns 0 when loop exits

  if ( done == 3 )
     done == startGame3(); // game3 loop returns 0 when loop exits

Watch for this typo, it’ll still call startGame3(), but wont set done
to the value returned; it’d do a comparison as you know, and go back
to running the same game again.

Cheers,
John

Thanks John:)–
"This is it… This is where I belong… "