Click to See Complete Forum and Search --> : SDL Timer problems


seventhirty
December 24th, 2008, 08:26 AM
Hi code gurus!

I'm currentlu trying to make a tetris game, using MS Visual C++ 6.0 and SDL. My OS is Windows XP. I'm reading "Accelerated C++ " by Koeing and "Focus on SDL" by Pazera (both really helpful and good-written books)

My idea is to use an array - int board[10][18] for the game board.
I've written a function drawBlocks, which goes through the array and draws the appropriate block(30X30 pixels):
// 0 - empty, no block there
// 1 - moving block
// 2 - fallen block

Here's how it looks like:

void drawBlocks (int board[][18])
{
SDL_Surface* empty = NULL;
empty = SDL_LoadBMP("empty.bmp");

SDL_Surface* full = NULL;
full = SDL_LoadBMP("full.bmp");

SDL_Surface* fallen = NULL;
fallen = SDL_LoadBMP("fallen.bmp");


SDL_Rect frame;
frame.h=30;
frame.w=30;

for (int i =0; i!=300; i+=30)
for (int j =0; j!=540; j+=30)
{
frame.x=i;
frame.y=j;

if ( board[i/30][j/30]==0 ) SDL_BlitSurface ( empty, NULL, g_pMainSurface, &frame);
if ( board[i/30][j/30]==1 ) SDL_BlitSurface ( full, NULL, g_pMainSurface, &frame);
if ( board[i/30][j/30]==2 ) SDL_BlitSurface ( fallen, NULL, g_pMainSurface, &frame);
}

}

Here's how the main function looks like:

int main(int argc, char* argv[])
{
if (SDL_Init(SDL_INIT_VIDEO || SDL_INIT_TIMER)==-1)
{
fprintf(stderr,"Could not initialize SDL!\n");
exit(1);
}

else
{
fprintf(stdout, "SDL is initialized properly!\n");

atexit(SDL_Quit);

}

g_pMainSurface = SDL_SetVideoMode(300, 540, 0, SDL_ANYFORMAT);
if(!g_pMainSurface)
{
fprintf(stderr,"Could not create main surface!\n");
exit(1);
}

//initialize game board as empty

int board[10][18];

for (int i =0; i!=10;i++)
for (int j =0; j!=18; j++)
board[i][j]=0;

drawBlocks ( board );

for(;;)
{
if(SDL_WaitEvent(&g_Event)==0)
{
fprintf(stderr,"Error while waiting for an event!\n");
exit(1);
}
if(g_Event.type==SDL_QUIT)
{
fprintf(stdout,"Quit Event has occured!\n");
break;
}
}
fprintf(stdout, "Terminating program normally.\n");
return(0);
}



Everything is fine when I run this. Here are the include directives and event and surface initialization:


#include "sdl.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

SDL_Surface* g_pMainSurface = NULL;
SDL_Event g_Event;



Here is the problem:
After the initialization of the game board and drawing on the screen I try to wait 1 sec. and then to draw a block on the board by adding this code:


/*...*/
int board[10][18];

for (int i =0; i!=10;i++)
for (int j =0; j!=18; j++)
board[i][j]=0;

drawBlocks ( board );

//here's what i add:
SDL_Delay(1000)
board[6][6]=1;
drawBlcoks (board);


When I execute(no errors, no warnings..) this I get a black board( I suppose drawBlocks() does not do anything) for 1 second and then a board whith the added block drawn (alfter the SDL_Delay() ). If I wait another sec and add another block, the board stays black for two seconds and then draws the 2 blocks (while it is supposed to draw an empty surface(not black), wait 1 sec, draw a block on it, wait a sec and draw another block)

I alse tried to use the SDL Timer and a windows timer - It's the absolutely same story and I'm pretty sure that I use the SDL_Delay and windows delay functions as supposed to. I think the problem lies somewhere else, but I don't know where. I lost 2 days on it trying to figure it out, now I ask you guys. Please help me! : )

Feel free to ask any questions..
Sorry if my post is long and if my code is hard to read...

zerver
December 30th, 2008, 10:08 AM
Hi!

Perhaps it is double buffered and you have to update the screen, e.g.

SDL_GL_SwapBuffers();

seventhirty
January 7th, 2009, 06:22 PM
I found it after reading more carefully.. (please excuse me for not doing it before posting...)

It's not double buffering (it is a concern only for applications in full screen mode), its the lack of SDL_UpdateRect, which is used for updating the display.. The timer actually runs ok.