HOME       >>       Graphics, Design & Animation

Sdl:why Does The Rectangle Move So Slowly?


I begin to study sdl.I find it is not easy to do this without any knowledge about
graphic programing.

Now I have a problem in making a simple animation: move rectange.The problem is that the rectangel move slowly even though I haven't do any time delay!

Is there any way to speed up the movement of rectange? Some beginner links or documentation about speeding up the output of videocard is helpful ,too.I haven't find useful information in sdl's homepage and maillist for too little knowledge in this field.

Appreciate for your any help!
1.Here are some system information:
CPU: Athlon XP 1800
Video Card:GeForce4 MX 440 AGP 8x (only 4x is enable for the capability of mainboard)
OS: gentoo linux
2.Here is the simple code I change from the example in dev lib of SDL:

#include <stdlib.h>#include <unistd.h>#include <SDL/SDL.h>/* * Return the pixel value at (x, y) * NOTE: The surface must be locked before calling this! */Uint32 getpixel(SDL_Surface *surface, int x, int y){    int bpp = surface->format->BytesPerPixel;    /* Here p is the address to the pixel we want to retrieve */    Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;    switch(bpp) {    case 1:        return *p;    case 2:        return *(Uint16 *)p;    case 3:        if(SDL_BYTEORDER == SDL_BIG_ENDIAN)            return p[0] << 16 | p[1] << 8 | p[2];        else            return p[0] | p[1] << 8 | p[2] << 16;    case 4:        return *(Uint32 *)p;    default:        return 0;       /* shouldn't happen, but avoids warnings */    }}main(int argc, char *argv[]){   SDL_Surface *screen;   int W = 640,H = 480,bpp = 8;   int x,y;   SDL_Rect dstrect;   Uint32 color;            /* Initialize the SDL library */    if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {        fprintf(stderr,                "Couldn't initialize SDL: %s\n", SDL_GetError());        exit(1);    }      /* Clean up on exit */    atexit(SDL_Quit);          /*     * Initialize the display in a 640x480 bpp-bit palettized mode,     * requesting a software surface     */    /* Have a preference for bpp-bit, but accept any depth */    screen = SDL_SetVideoMode(W, H, bpp,            SDL_HWSURFACE|SDL_ANYFORMAT|SDL_DOUBLEBUF);    if ( screen == NULL ) {        fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n",                        SDL_GetError());        exit(1);    }    for(x = 0,y = 0;(x<W) && (y<H);x++){        dstrect.x = x;        dstrect.y = y;        dstrect.w = 100;        dstrect.h = 100;        color=getpixel(screen,x,y);        SDL_FillRect(screen,&dstrect,0xfa9d31);        //SDL_UpdateRect(screen,x,y,100,100);        //SDL_UpdateRects(screen,1,&dstrect);        SDL_Flip(screen);        //usleep(1e2);        SDL_FillRect(screen,&dstrect,color);        //SDL_UpdateRect(screen,x,y,100,100);        //SDL_Flip(screen);        y ++;    }}


hmm...I remember coming across this, but it's been so long since I've looked at SDL. I remembered that if the window that displayed the rectangle wasn't active, the speed would be faster. I just can't remember what I did to improve this.I'll see if I can figure this one out.MC


Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.