#include #include #include "SDL/SDL.h" #include #define XRES 640 #define YRES 480 #define BALLSIZE 40 #define NUMCOL 3 #define PERCOL 4 #define PAGES (NUMCOL*PERCOL) SDL_Surface *screen; SDL_Surface *page[PAGES]; SDL_Surface *ball[NUMCOL]; int bcols[][3] = { { 0x68, 0x68, 0xff }, { 0xff, 0x68, 0x68 }, { 0x68, 0xff, 0x68 } }; int cpage=0; void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel) { int bpp = surface->format->BytesPerPixel; /* Here p is the address to the pixel we want to set */ Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; switch(bpp) { case 1: *p = pixel; break; case 2: *(Uint16 *)p = pixel; break; case 3: if(SDL_BYTEORDER == SDL_BIG_ENDIAN) { p[0] = (pixel >> 16) & 0xff; p[1] = (pixel >> 8) & 0xff; p[2] = pixel & 0xff; } else { p[0] = pixel & 0xff; p[1] = (pixel >> 8) & 0xff; p[2] = (pixel >> 16) & 0xff; } break; case 4: *(Uint32 *)p = pixel; break; } } void drawball(int xofs, int yofs, int a) { SDL_Rect drect = { xofs, yofs, ball[a]->w, ball[a]->h }; SDL_BlitSurface(ball[a], NULL, page[cpage], &drect); } int main() { int size=BALLSIZE; int hsize=size/2; if(SDL_Init(SDL_INIT_VIDEO)<0) { fprintf(stderr, "failed to initialise sdl: %s\n", SDL_GetError()); exit(1); }; atexit(SDL_Quit); screen = SDL_SetVideoMode(XRES, YRES, 16, SDL_DOUBLEBUF|SDL_HWSURFACE); if(!screen) { fprintf(stderr, "failed to set video mode: %s\n", SDL_GetError()); exit(1); } { int n; for(n=0; nw, screen->h, 16, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, 0); } { Uint32 col; int x, y, n; double dist, bright; for(n=0; n hsize) continue; bright=1-(dist/size); col=SDL_MapRGBA(ball[n]->format, bcols[n][0]*bright, bcols[n][1]*bright, bcols[n][2]*bright, 0xd0*((1.0+(dist/hsize))/2.0)); putpixel(ball[n], x, y, col); } } } } /* Lock the screen for direct access to the pixels */ if(SDL_MUSTLOCK(screen)) { if(SDL_LockSurface(screen) < 0) { fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError()); exit(1); } } { SDL_Event event; float f=0.0; int i=-10; int pause=0, dir=1; int x,y; int quit=0; while(!quit) { if(i>=10) dir=-1; else if(i<=-10) dir=1; while(f>=1.0) { i+=dir; f-=1.0; } { int j; for(j=0; jw-size)); y=(int)((((sin(f*2*M_PI)/2)*(i/10.0))+0.5)*(screen->h-size)); drawball(x, y, j); cpage=(cpage+PERCOL)%(PAGES); } } cpage=(cpage+1)%(PAGES); f+=0.02/PERCOL; SDL_BlitSurface(page[cpage], NULL, screen, NULL); SDL_Flip(screen); do { // usleep(1000000); for(x=0; x<750000; x++) y=x; while(SDL_PollEvent(&event)) if(event.type == SDL_KEYDOWN) if(event.key.keysym.sym == SDLK_ESCAPE) quit=1; else if(event.key.keysym.sym == SDLK_SPACE) pause=!pause; } while(pause && !quit); } } if(SDL_MUSTLOCK(screen)) { SDL_UnlockSurface(screen); } SDL_Quit(); exit(0); }