#include #include "SDL/SDL.h" #include #include //SDL screen pointer SDL_Surface *screen; //computer specific constants const int horizontalRez = 800; const int verticalRez = 600; //star constants const int maxStars = 500; const int layers = 3; //star object struct star { int x,y; int layer; }; //array of Stars star starField[maxStars]; void initStars(void){ int i; for (i=0;ipixels; int lineoffset = y * (screen->pitch / 4); ptr[lineoffset + x] = color; } int layerColor(int layer){ if (layer == 0){ return 0xff0000; } if (layer == 1){ return 0x00ff00; } if (layer == 2){ return 0x0000ff; } return 0xffffff; } void drawStar(void){ // Declare a couple of variables int i, j, yofs, ofs; // Draw to screen yofs = 0; for (i = 0; i < verticalRez; i++){ for (j = 0, ofs = yofs; j < horizontalRez; j++, ofs++){ ((unsigned int*)screen->pixels)[ofs] = 0; } yofs += screen->pitch / 4; } int s; for (s=0;s(horizontalRez - 1)){ starField[i].y=rand()%verticalRez; starField[i].x=0; starField[i].layer=rand()%layers; } } } // BEGIN PROGRAM EXECUTION int main(int argc, char *argv[]){ // Initialize SDL's subsystems - in this case, only video. if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); exit(1); } // Register SDL_Quit to be called at exit; makes sure things are // cleaned up when we quit. atexit(SDL_Quit); screen = SDL_SetVideoMode(horizontalRez, verticalRez, 32, SDL_SWSURFACE); // If we fail, return error. if ( screen == NULL ) { fprintf(stderr, "Unable to set video: %s\n", SDL_GetError()); exit(1); } initStars(); // Main loop: loop forever. while (1){ //Update Star Field updateStars(); drawStar(); // Poll for events, and handle the ones we care about. SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: break; case SDL_KEYUP: // If escape is pressed, return (and thus, quit) if (event.key.keysym.sym == SDLK_ESCAPE) return 0; break; case SDL_QUIT: return(0); } } } return 0; }