All pastes #745262 Raw Edit

Miscellany

public c v1 · immutable
·published 2007-10-22 07:06 UTC
rendered paste body
/* * Creation date: 21 Oct 2007 * Author: ZaidaZadkiel * Purpose: None, really. I think it looks pretty. */#include "SDL/SDL.h"#include "SDL/SDL_gfxPrimitives.h"#include <stdio.h>#define UPDATES 10typedef enum{    true = 1,    false = 0} boxes_boolean_t;typedef struct{    SDL_Surface *screen;    int xtimes, ytimes;    int seed;    boxes_boolean_t print;    Uint8 colors[30*30];    Uint8 newcolors[30*30];    Sint8 deltas[30*30];} boxes_data_t;int do_events(boxes_data_t *data){	SDL_Event e;	SDLKey k;	while(SDL_PollEvent(&e)){		switch(e.type){			case SDL_KEYDOWN:				k = e.key.keysym.sym;				if(k == SDLK_ESCAPE){					printf("escape pressed\n");					return 1;				}/*if*/				data.print   = (k == SDLK_p);				if(k == SDLK_RIGHT   ) data->xtimes += (data->xtimes < 30);				if(k == SDLK_LEFT    ) data->xtimes -= (data->xtimes > 1);				if(k == SDLK_UP      ) data->ytimes -= (data->ytimes > 1);				if(k == SDLK_DOWN    ) data->ytimes += (data->ytimes < 30);				if(k == SDLK_HOME    ) {data->seed   =  5000; srand(data->seed);}				if(k == SDLK_END     ) {data->seed   = 10000; srand(data->seed);}				if(k == SDLK_PAGEDOWN) {data->seed  += (data->seed < 10000) * 100; srand(data->seed);}				if(k == SDLK_PAGEUP) {data->seed  -= (data->seed >  5000) * 100; srand(data->seed);}				break;			default:				break;		}/*switch*/	}/*if*/	return 0;}Uint32 get_color(int i, int len){/*	Uint8 d = ((float)i/(float)len)*255.0;*/	Uint8 *c, *nc;	Sint8 *d;	c  = &colors[i];	nc = &newcolors[i];	d  = &deltas[i];	if(*c > (*nc - *d)){// && *c < (*nc + *d)){		if(i == 3)			printf(">> c %3d, nc %3d, d %3d\n", *c, *nc, *d);	}else{	//	printf("oldcolor: c=%3d, nc=%3d\n", *c, *nc);		*nc = rand() % 256;		//*d  = (*nc - *c) / UPDATES;		if(*c > *nc){			*d = (*nc - *c) / UPDATES;		}else{			*d = (*c - *nc) / UPDATES;		}		if(*d == 0) *d = (*c > *nc ? -1 : 1);		//printf("nc %3d, c %3d, d %3d\n", *nc, *c, *d);	//	printf("newcolor: i=%3d, c=%3d, nc=%3d, d=%3d\n", i, *c, *nc, *d);			}	*c += *d;//	Uint8 d = rand() % 256; //((double)rand() / (double)RAND_MAX+(double)1) * 512;	return SDL_MapRGB(		screen->format,		*c, 0, 0	);}void paint_boxes(int nw, int nh){	SDL_Rect r;	int boxlen;	float cx, cy; /*correction x and y*/	r.x = 0;	r.y = 0;	r.w = screen->w / nw+1;	r.h = screen->h / nh+1;	cx = 1-(float)(r.w * nw) / screen->w;	cy = 1-(float)(r.h * nh) / screen->h;	boxlen = nw*nh;	if(print){		print = false;		printf("-------\n");		printf("seed = %d\n", seed);		printf("xtimes = %2d ytimes = %2d\n", nw, nh);		printf("r = {%3d,%3d,%3d,%3d}\n", 			r.x, r.y, r.w, r.h		);		printf("boxlen = %3d\n", boxlen);		printf("cx = %3f cy = %3f\n", cx, cy);		printf("test %d : %f\n", 			((boxlen/2) % nw) * r.w,			(((boxlen/2) % nw) * r.w) +			((((boxlen/2) % nw) * r.w) * cx)		);	}/*if(print)*/	int i;	for(i = 1; i <= boxlen; i++){		SDL_FillRect(screen, &r, get_color(i, boxlen));				r.x  = (i % nw) * r.w;		r.y  = (i / nw) * r.h;		r.x += (r.x * cx);		r.y += r.y * cy;	}}int main(int argc, char argv[]){putenv("SDL_VIDEO_WINDOW_POS");putenv("SDL_VIDEO_CENTERED=1");	atexit(SDL_Quit);	/* Initialize the SDL library */	if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {		fprintf(stderr,"Couldn't initialize SDL: %s\n", SDL_GetError());		exit(1);	}	screen = SDL_SetVideoMode(800, 600, 16, SDL_DOUBLEBUF | SDL_SWSURFACE | SDL_NOFRAME );	if ( screen == NULL ) {		fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n",			SDL_GetError());		exit(1);	}	xtimes = 30;	ytimes = 30;	int i;	for(i = 0; i != 30*30; i++){		colors[i] = 0;		newcolors[i] = 255;		deltas[i] = 255/UPDATES;	}	while(do_events() == false){		SDL_FillRect(screen, NULL, 0);		paint_boxes(xtimes,ytimes);		SDL_Flip(screen);		SDL_Delay(1000/UPDATES);	}	return 0;}