All pastes #25894 Raw Edit

Miscellany

public text v1 · immutable
#25894 ·published 2005-10-18 22:09 UTC
rendered paste body
#include <stdlib.h>
#include "SDL/SDL.h"
#include <math.h>

//constants
const int horizontalRez = 1400;
const int verticalRez = 1050;
const int maxStars = 1500;
const int updateTime = 30;
const int centerX = horizontalRez/2;
const int centerY = verticalRez/2; 
const int Scale = 900;

//star object
struct star { 
	int x,y,z;
};

//array of Stars
star starField[maxStars];

// Function prototypes
void initStars(void);
void putpixel(SDL_Surface *S, int x, int y, int color);
void drawStar(SDL_Surface *S);
void updateStars(void);

void initStars(void){
	int i;
	for (i=0;i<maxStars;i++){
		starField[i].x=rand()%horizontalRez - centerX;
		starField[i].y=rand()%verticalRez - centerY;
		starField[i].z=i+1;
	}
}

void putpixel(SDL_Surface *S, int x, int y, int color){
	unsigned int *pixels = (unsigned int*)S->pixels;
	pixels[(S->pitch * y / 4) + x] = color;
}

void drawStar(SDL_Surface *S){
	int i;
	int xcoord;
	int ycoord;
	int b;
	int c;
	SDL_FillRect(S, NULL, 0x000000);
	for (i=0;i<maxStars;i++){
		xcoord = (int)((starField[i].x*Scale)/starField[i].z) + centerX;
		ycoord = (int)((starField[i].y*Scale)/starField[i].z) + centerY;
		c = maxStars/starField[i].z
		if (xcoord < horizontalRez && ycoord < verticalRez && xcoord > 0 && ycoord > 0)
			putpixel(S, xcoord, ycoord, SDL_MapRGB(S->format, 255, 255, 255));
	}
}

void updateStars(void){
	int i;
	for (i=0;i<maxStars;i++){
		// move star to the right - depending on layer
		// [plus one - since layer zero would result in no motion otherwize]
		starField[i].z-=5;
		// check for wrap around
		if (starField[i].z < 1){
			starField[i].y=rand()%verticalRez - centerY;
			starField[i].x=rand()%horizontalRez - centerX;
			starField[i].z+=maxStars;
		}
	}
}

int main(int argc, char *argv[]){
	//SDL screen pointer
	SDL_Surface *screen;
	
	int ticks;
	int timeDifference;
	
	// 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);
    SDL_ShowCursor(SDL_DISABLE);
	screen = SDL_SetVideoMode(horizontalRez, verticalRez, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
	// 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 timer
		ticks = SDL_GetTicks();
		//Update Star Field
		updateStars();
		SDL_LockSurface(screen);
		drawStar(screen);
		SDL_UnlockSurface(screen);
		SDL_Flip(screen);
		//slow down if necessary
		timeDifference = SDL_GetTicks() - ticks;
		if(timeDifference < updateTime) 
			SDL_Delay(updateTime-timeDifference);
		
		// 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;
}