Skip to main content
gamedev.net
🔒 Locked

SDL slow loading

Started by skwee Jan 18, 2007 at 5:58 AM 4 replies 1k views
Original Post
skwee
skwee
Hello I am C/C++ programmer and I started with SDL. I am programing now Tetris game. When i run my game it takes some time load the windows and at the task bar in not appears or appears after 10-15sec. When I am trying to move the window is very slow. Its suppose to be like this? Or I have problem?
I would love to change the world, but they won’t give me the source code.
rip-off
rip-off
That sounds very unusual. If your code isnt to long you could post it, if its quite long try posting the bits that are called during loading.
skwee
skwee
Ok i'll try to make it shorter as i can

Functions:
void init_graph()
{
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, BPP, SDL_SWSURFACE);
SDL_WM_SetCaption("Tetris", NULL);
}

SDL_Surface* load_pic(char *pic)
{
SDL_Surface *picture = NULL;
cube = SDL_LoadBMP(pic);
picture = SDL_DisplayFormat(cube);
SDL_FreeSurface(cube);
return picture;
}

void init_fig(Fig *fig)
{
fig->x = GSCREEN_WIDTH/2;
fig->y = 0;
fig->pic = load_pic(cube_pic);
}

void show(Fig fig, SDL_Surface *screen)
{
SDL_Rect offset;
offset.x = fig.x;
offset.y = fig.y;
SDL_BlitSurface(fig.pic, NULL, screen, &offset);
}

int main(int argc, char **argv)
{
Fig cube;
bool quit = false;
init_graph();
init_fig(&cube);
show(cube, screen);
while(quit == false)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
//case SDLK_UP: move(&cube, 0); break;
case SDLK_DOWN: move_y(&cube, 2); break;
case SDLK_RIGHT: move_x(&cube, 1); break;
case SDLK_LEFT: move_x(&cube, 2); break;
}
}
if(event.type == SDL_QUIT)
quit = true;
}
SDL_Flip(screen);
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00));
move_y(&cube, 0);
show(cube, screen);
SDL_Delay(500);
}
end_graph(cube.pic);
return 0;
}

This is tne main code parts
I would love to change the world, but they won’t give me the source code.
rip-off
rip-off
SDL_Delay(500), you know this pauses your program for half a second? Why so long?

What is Fig? Does it have a constructor and what are its contents?

Why does show take a Fig by value rather than by reference?

Do you have lots of globals elsewhere that have constructors that need running?

If it takes 10- 15 seconds to load your program then it means that Figs costructor and any global constructors are taking that order of time before init_graph() gets called. If you are using C++ put init_graph() as the first line of code in main()
skwee
skwee
SDL_Delay(500) pauses for half sec i know it and i need it becuase tetris must have pauses between each move of block.
Fig is typedef struct{ int x, y; SDL_Surface *pic)Fig;
"Why does show take a Fig by value rather than by reference?" What do you mean? Can you give an example?
Globals:
const int SCREEN_HEIGHT = 320;
const int SCREEN_WIDTH = 160;
const int GSCREEN_HEIGHT = 320;
const int GSCREEN_WIDTH = 160;
const int BPP = 32;
const int HEIGHT = 8;
const int WIDTH = 8;
const int STEP = 8;
char *cube_pic = "cube.bmp";
SDL_Surface *screen = NULL;
SDL_Surface *cube = NULL;
SDL_Event event;

Ok i did some experiments now:
1. Tried to put init_graph(); as the fisr line of the program, the same: the program loads, but it appear at the taskbar after 10-15 sec and when I am moving the window of the program it move slowly.
2. Removed SDL_Delay(500); at all, runs great no "lags" appear immediately window moving good, BUT the Fig (that must be the block that fall down at tetris game) already down i mean i can't see the process of it falling down its already fall down.
So from this seems that my problem cause of SDL_Delay(); but without it i cant make a proper game tetris. So i want also to ask can i use Timing instead of SDL_Delay()?
Any other suggestions?
I would love to change the world, but they won’t give me the source code.
evillive2
evillive2
You can use timing to "delay" the game loop but in most cases you would instead want to use a time-difference (look up time based movement) between frames to figure out how much to move the pieces. This way on 2 different systems the piece should fall at the same rate while the faster one will just show more frames and possibly look smoother. For example in the US a cars speed is measured in miles per hour. You could measure a game objects speed in pixels per second and use SDL_GetTicks to figure out how many milliseconds have passed per frame to move the pieces.

Delaying the game loop or limiting the frame rate as you are asking about is usually a little easier at first but as the game progresses in complexity it can become choppy as your actual frame rate approaches the requested frame rate. It is performed however by using something similar to the below example in pseudo code:
// the number of milliseconds in a second / the desired number of frames per secondUint32 requested_time_per_frame = 1000/30;Uint32 last_time, now_time, diff_time;// initialize last_timelast_time = SDL_GetTicks();// the game loopwhile(1){    // do game logic    ...    // do whatever else needs to be done    ...    // do drawing    ...    now_time = SDL_GetTicks();    diff_time = now_time - last_time;    // delay only the difference left for each frame    SDL_Delay( requested_time_per_frame - diff_time );    // set the last_time to now_time    last_time = now_time;}


The primary issue with this is that SDL_GetTicks is only accurate to about 10ms depending on the environment and so is SDL_Delay which could add up to be a 20ms flux in either direction. Secondary issues include integer math rounding (slightly fixed by using floats) that again adds to the inaccuracy of the whole equation leading to hiccups in the way the game looks.

A small game like tetris will most likely get by without problems by limiting the frame rate however both solutions are very similar in concept and the choice is ultimately up to you.
Evillive2

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.