Why is this so slow?

hi, i have a problem with this code. when i run it, the response to a
input action is very slow. e.g printing the coordinates of left_click is
delayed around 2 seconds from actual click. the delay depends on how
many events the WaitEvent() must process before it can get to the
desired event (when I run with mouse accross the screen and then click,
it takes A LOT of time to get it propagate. but in my other codes this
loop works without such delay.
please, what can i do to make it run faster?
thank you-------------------------

int main(int argc, char *argv[]) {

if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf(“Unable to initialize SDL: %s\n”, SDL_GetError());
return 1;
}

atexit(SDL_Quit);

screen = SDL_SetVideoMode(1024, 768, 32, SDL_HWSURFACE | SDL_DOUBLEBUF |
SDL_ANYFORMAT);
if (screen == NULL) {
printf(“Unable to set video mode: %s\n”, SDL_GetError());
return 1;
}

tmp = IMG_Load("./EIN/BG/pyramid.png");
BG = SDL_DisplayFormat(tmp);
SDL_FreeSurface(tmp);
if (BG == NULL) {
printf(“Unable to load background.\n”);
return 1;
}

while (SDL_WaitEvent(&event)) {
if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button==1) {
_x=event.button.x;
_y=event.button.y;
printf("%d,%d\n", _x,_y);
}

SDL_BlitSurface(BG, NULL, screen, NULL);
SDL_UpdateRect(screen, 0, 0, 0, 0);
SDL_Flip(screen);

}

}

I think… That you are doing the whole :

SDL_BlitSurface(BG, NULL, screen, NULL);
SDL_UpdateRect(screen, 0, 0, 0, 0);
SDL_Flip(screen);

So many times, in a second, and that’s why it takes son long.
You don’t really need to draw anything that hasn?t moved!!
Check your loops… Good Luck.

Juan Ignacio Carniglia - Programmer - Buenos Aires, Argentina.> -----Original Message-----

From: sdl-admin at libsdl.org [mailto:sdl-admin at libsdl.org] On
Behalf Of 515
Sent: Monday, August 25, 2003 5:49 PM
To: sdl at libsdl.org
Subject: [SDL] why is this so slow?

hi, i have a problem with this code. when i run it, the response to a
input action is very slow. e.g printing the coordinates of
left_click is
delayed around 2 seconds from actual click. the delay depends on how
many events the WaitEvent() must process before it can get to the
desired event (when I run with mouse accross the screen and
then click,
it takes A LOT of time to get it propagate. but in my other
codes this
loop works without such delay.
please, what can i do to make it run faster?
thank you


int main(int argc, char *argv[]) {

if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf(“Unable to initialize SDL: %s\n”, SDL_GetError()); return 1; }

atexit(SDL_Quit);

screen = SDL_SetVideoMode(1024, 768, 32, SDL_HWSURFACE |
SDL_DOUBLEBUF | SDL_ANYFORMAT); if (screen == NULL) {
printf(“Unable to set video mode: %s\n”, SDL_GetError());
return 1;
}

tmp = IMG_Load("./EIN/BG/pyramid.png");
BG = SDL_DisplayFormat(tmp);
SDL_FreeSurface(tmp);
if (BG == NULL) {
printf(“Unable to load background.\n”);
return 1;
}

while (SDL_WaitEvent(&event)) {
if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button==1) {
_x=event.button.x;
_y=event.button.y;
printf("%d,%d\n", _x,_y);
}

SDL_BlitSurface(BG, NULL, screen, NULL);
SDL_UpdateRect(screen, 0, 0, 0, 0);
SDL_Flip(screen);

}

}


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

There is a function called SDL_UpdateRects which can be used for updating
areas of the screen that have changed. Corse you have to track them. See the
testsprite.c sample that comes with SDL for an example of how this is used

Also why are you calling SDL_Flip… Assuming the screen is your display
screen you shouldn’t need to call SDL_Flip after SDL_UpdateRect. The only
reason for using SDL_Flip is if your using a double buffering method… which
doesn’t appear to be the case in the sample code provided.

See testwin.c for an example different between SDL_Flip and SDL_UpdateRect.
Using both will slow things down, as your updating the screen twice each
time, when it only needs it once.

Thanks
V

Quoting Juan Ignacio Carniglia :> I think… That you are doing the whole :

SDL_BlitSurface(BG, NULL, screen, NULL);
SDL_UpdateRect(screen, 0, 0, 0, 0);
SDL_Flip(screen);

So many times, in a second, and that’s why it takes son long.
You don’t really need to draw anything that hasn?t moved!!
Check your loops… Good Luck.

Juan Ignacio Carniglia - Programmer - Buenos Aires, Argentina.

-----Original Message-----
From: sdl-admin at libsdl.org [mailto:sdl-admin at libsdl.org] On
Behalf Of 515
Sent: Monday, August 25, 2003 5:49 PM
To: sdl at libsdl.org
Subject: [SDL] why is this so slow?

hi, i have a problem with this code. when i run it, the response to a
input action is very slow. e.g printing the coordinates of
left_click is
delayed around 2 seconds from actual click. the delay depends on how
many events the WaitEvent() must process before it can get to the
desired event (when I run with mouse accross the screen and
then click,
it takes A LOT of time to get it propagate. but in my other
codes this
loop works without such delay.
please, what can i do to make it run faster?
thank you


int main(int argc, char *argv[]) {

if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf(“Unable to initialize SDL: %s\n”, SDL_GetError()); return 1; }

atexit(SDL_Quit);

screen = SDL_SetVideoMode(1024, 768, 32, SDL_HWSURFACE |
SDL_DOUBLEBUF | SDL_ANYFORMAT); if (screen == NULL) {
printf(“Unable to set video mode: %s\n”, SDL_GetError());
return 1;
}

tmp = IMG_Load("./EIN/BG/pyramid.png");
BG = SDL_DisplayFormat(tmp);
SDL_FreeSurface(tmp);
if (BG == NULL) {
printf(“Unable to load background.\n”);
return 1;
}

while (SDL_WaitEvent(&event)) {
if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button==1) {
_x=event.button.x;
_y=event.button.y;
printf("%d,%d\n", _x,_y);
}

SDL_BlitSurface(BG, NULL, screen, NULL);
SDL_UpdateRect(screen, 0, 0, 0, 0);
SDL_Flip(screen);

}

}


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


This mail sent through IMP: http://horde.org/imp/

Varmint wrote:

Also why are you calling SDL_Flip… Assuming the screen is your display
screen you shouldn’t need to call SDL_Flip after SDL_UpdateRect. The only
reason for using SDL_Flip is if your using a double buffering method… which
doesn’t appear to be the case in the sample code provided.

why doesn’t this code seem to be using doublebuffer? i passed
SDL_HWSURFACE | SDL_DOUBLEBUF to SDL_SetVideoMode() and I use SDL_FLip()
The SDL_UpdateRect(screen, 0, 0, 0, 0); was of course redundant because
of my stupidity :{

thx
515>>>-----Original Message-----

From: sdl-admin at libsdl.org [mailto:sdl-admin at libsdl.org] On
Behalf Of 515
Sent: Monday, August 25, 2003 5:49 PM
To: sdl at libsdl.org
Subject: [SDL] why is this so slow?

hi, i have a problem with this code. when i run it, the response to a
input action is very slow. e.g printing the coordinates of
left_click is
delayed around 2 seconds from actual click. the delay depends on how
many events the WaitEvent() must process before it can get to the
desired event (when I run with mouse accross the screen and
then click,
it takes A LOT of time to get it propagate. but in my other
codes this
loop works without such delay.
please, what can i do to make it run faster?
thank you


int main(int argc, char *argv[]) {

if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf(“Unable to initialize SDL: %s\n”, SDL_GetError()); return 1; }

atexit(SDL_Quit);

screen = SDL_SetVideoMode(1024, 768, 32, SDL_HWSURFACE |
SDL_DOUBLEBUF | SDL_ANYFORMAT); if (screen == NULL) {
printf(“Unable to set video mode: %s\n”, SDL_GetError());
return 1;
}

tmp = IMG_Load("./EIN/BG/pyramid.png");
BG = SDL_DisplayFormat(tmp);
SDL_FreeSurface(tmp);
if (BG == NULL) {
printf(“Unable to load background.\n”);
return 1;
}

while (SDL_WaitEvent(&event)) {
if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button==1) {
_x=event.button.x;
_y=event.button.y;
printf("%d,%d\n", _x,_y);
}

SDL_BlitSurface(BG, NULL, screen, NULL);
SDL_UpdateRect(screen, 0, 0, 0, 0);
SDL_Flip(screen);

}

}

why doesn’t this code seem to be using doublebuffer? i passed
SDL_HWSURFACE | SDL_DOUBLEBUF to SDL_SetVideoMode() and I use SDL_FLip()
The SDL_UpdateRect(screen, 0, 0, 0, 0); was of course redundant because
of my stupidity :{

http://www.libsdl.org/faq.php?action=listentries&category=2#67

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

515 wrote:

SDL_UpdateRect(screen, 0, 0, 0, 0);
SDL_Flip(screen);

SDL_UpdateRect or SDL_Flip. Pick one. Don’t use the other.–
Rainer Deyke - rainerd at eldwood.com - http://eldwood.com