//#ifdef __cplusplus
#include <cstdlib>
#define o(S) printf("%s\n",S);
#define os(S) printf("%s\n",S);
#define oi(S) printf("%d\n",S);
#define od(S) printf("%d\n",S);
#define of(S) printf("%f\n",S);
#define oc(S) printf("%c\n",S);
//#else
// #include <stdlib.h>
//#endif
//#ifdef __APPLE__
//#include <SDL/SDL.h>
//#else
#include <SDL.h>
//#endif
SDL_Surface *screen;
struct rec
{
int x,
y,
w,
h;
int color;
};
void fillRec(rec r, int color)
{
SDL_Rect rect = {r.x, r.y, r.w, r.h};
SDL_FillRect(screen, &rect, color);
}
int W,H;
class Rec
{
public:
Rec()
{
o("Rec.ctor");
r.x = 17 * rand() % 400;
r.y = 17 * rand() % 200;
dx=rand()%3;
dy=rand()%3;
r.w = 16;
r.h = 16;
r.color = rand() ;
}
void draw()
{
fillRec(r, r.color);
}
void updt()
{
r.x+=dx;
r.y+=dy;
if(r.x > W || r.x<16)
{
dx *= -1;
}
if(r.y > H || r.x<16)
{
dy*=-1;
}
if(rand()%1024==0){
dx+=rand()%4-2;dy+=rand()%4-2;
if(W<800 && rand()%256==0){W++;}
if(H<600&&rand()%256==0){H++;}
}
}
private:
rec r;
int dx,dy;
};
int main(int argc, char **argv)
{
W=H=400;
// initialize SDL video
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("Unable to init SDL: %s\n", SDL_GetError());
return 1;
}
// make sure SDL cleans up before exit
atexit(SDL_Quit);
// create a new window
screen = SDL_SetVideoMode(800, 600, 16,
SDL_HWSURFACE | SDL_DOUBLEBUF);
if(!screen)
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
return 1;
}
// load an image
// SDL_Surface* bmp = SDL_LoadBMP("cb.bmp");
// if (!bmp)
// {
// printf("Unable to load bitmap: %s\n", SDL_GetError());
// return 1;
// }
// centre the bitmap on screen
// SDL_Rect dstrect;
// dstrect.x = (screen->w - bmp->w) / 2;
// dstrect.y = (screen->h - bmp->h) / 2;
Rec rA[300];
// program main loop
bool done = false;
int oldTicks,newTicks,longestTick=0;
oldTicks=newTicks=longestTick=1;
int sec=1000;
int frames=0;
while(!done)
{
int oldTicks = SDL_GetTicks();
frames++;
//if(frames%sec==0){frames=0;}
// message processing loop
SDL_Event event;
while(SDL_PollEvent(&event))
{
// check for messages
switch(event.type)
{
// exit if the window is closed
case SDL_QUIT:
{
done = true;
break;
}
// check for keypresses
case SDL_KEYDOWN:
{
// exit if ESCAPE is pressed
if(event.key.keysym.sym == SDLK_ESCAPE)
done = true;
break;
}
} // end switch
} // end of message processing
// DRAWING STARTS HERE
// clear screen
//SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
// draw bitmap
// SDL_BlitSurface(bmp, 0, screen, &dstrect);
for(int i=0;i<300;i++){rA[i].updt();}
//r1.draw();
for(int i=0;i<300;i++){rA[i].draw();}
// DRAWING ENDS HERE
// finally, update the screen :)
//SDL_Flip(screen);
newTicks = SDL_GetTicks();
SDL_UpdateRect(screen,0,0,0,0);
} // end main loop
// free loaded bitmap
// SDL_FreeSurface(bmp);
// all is well ;)
printf("Exited cleanly\n");
return 0;
}
Here is how it starts out:
And here again after some time has passed. See that some have broken lose.
But who uses screensavers anymore?

