Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SDL/SDL.h"
- #include <math.h>
- #ifndef PI
- #define PI 3.14159265358979323846
- #endif
- #define VIDEOX 320
- #define VIDEOY 240
- #define TABLEX VIDEOX*2
- #define TABLEY VIDEOY*2
- #define TONES 256
- double r[3];
- double R[6];
- unsigned char *t;
- int rands[512];
- int lastRandI = 0;
- static void do_plasma(SDL_Surface*surface,
- double x1,
- double y1,
- double x2,
- double y2,
- double x3,
- double y3,
- unsigned char*t)
- {
- unsigned int
- X1=x1*(TABLEX/2),
- Y1=y1*(TABLEY/2),
- X2=x2*(TABLEX/2),
- Y2=y2*(TABLEY/2),
- X3=x3*(TABLEX/2),
- Y3=y3*(TABLEY/2);
- unsigned int y;
- unsigned char *t1=t+X1+Y1*TABLEX, *t2=t+X2+Y2*TABLEX, *t3=t+X3+Y3*TABLEX;
- for (y=0; y<VIDEOY; y++) {
- unsigned x;
- unsigned char*tmp= ((Uint8*)surface->pixels) + y * surface->pitch;
- unsigned int t=y*TABLEX, tmax=t+VIDEOX;
- for (x=0; t<tmax; t++, tmp++) {
- tmp[0]=t1[t]+t2[t]+t3[t];
- }
- }
- }
- int main ()
- {
- SDL_Surface *screen;
- SDL_Color colors[TONES*2];
- int starttime, endtime, realtime;
- int i, state=0;
- /* randomized stuff */
- startcrand(SDL_GetTicks(), 99999999);
- {
- int c;
- for (c=0; c<3; c++)
- r[c]=((double)(crand()%1000+1))/300000;
- for (c=0; c<6; c++)
- R[c]=((double)(crand()%1000+1))/5000;
- }
- /* Inizialize the SDL library */
- if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
- fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
- return 1;
- }
- screen=SDL_SetVideoMode(VIDEOX, VIDEOY, 8, SDL_SWSURFACE);
- if ( screen == NULL ) {
- fprintf(stderr, "Couldn't initialize video mode: %s\n", SDL_GetError());
- return 1;
- }
- SDL_nFont *font = SDL_nLoadFont(NSP_FONT_THIN, SDL_MapRGB(screen->format, 255,0,60),NSP_FONT_DEFAULT);
- SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 100));
- SDL_nDrawString(screen, font, 120, 110, "Loading...");
- SDL_Flip(screen);
- precalculateTable();
- SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
- SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
- /* Main loop */
- while (SDL_PollEvent(NULL)==0) {
- starttime=SDL_GetTicks();
- state++;
- {
- int c;
- for (c=0 ; c<TONES; c++) {
- colors[c].r=(sin(((double)c)/TONES*6*PI+r[0]*PI*state*PI)+1)*127;
- colors[c].g=(sin(((double)c)/TONES*6*PI+r[1]*state*PI)+1)*127;
- colors[c].b=(sin(((double)c)/TONES*6*PI+r[2]*state*PI)+1)*127;
- }
- SDL_SetColors(screen, colors, 0, TONES);
- }
- if (SDL_LockSurface(screen) <0 ) {
- continue;
- }
- do_plasma(screen,
- (sin(((double)state)*R[0])+1)/2,
- (sin(((double)state)*R[1])+1)/2,
- (sin(((double)state)*R[2])+1)/2,
- (sin(((double)state)*R[3])+1)/2,
- (sin(((double)state)*R[4])+1)/2,
- (sin(((double)state)*R[5])+1)/2, t);
- SDL_UnlockSurface(screen);
- SDL_Flip(screen);
- endtime = SDL_GetTicks();
- //limit frame draw time to a minimum of 40ms (25fps)
- if((endtime-starttime)<40){
- sleep(40-(endtime-starttime));
- realtime = SDL_GetTicks()-starttime;
- }
- }
- printf("Time to draw last frame was %d ms (%d fps)\n",endtime-starttime,1000/(endtime-starttime));
- if((endtime-starttime)<40)
- printf("But was forced to %d ms (%d fps)\n",realtime,1000/realtime);
- SDL_Quit();
- return 0;
- }
- void precalculateTable()
- {
- printf("Precalculating table...\n");
- t = (unsigned char *)malloc(TABLEY*TABLEX);
- if ( t == NULL ) {
- fprintf(stderr, "Out of memory\n");
- exit(1);
- }
- {
- int y;
- for (y=0 ; y<TABLEY ; y++) {
- int x;
- for (x=0 ; x<TABLEX ; x++) {
- double tmp=(((double)((x-(TABLEX/2))*(x-(TABLEX/2))+(y-(TABLEX/2))*(y-(TABLEX/2))))
- *(PI/(TABLEX*TABLEX+TABLEY*TABLEY)));
- t[y*TABLEX+x]=(sin(sqrt(tmp)*12)+1)*TONES/6;
- }
- }
- }
- }
- //RAND functions
- void startcrand(int seed, int max){
- printf("Starting pseudorandom number generator...\n");
- if(seed>max){
- printf("Oops: seed is bigger than max\n");
- exit(-1);
- }
- if(seed == 0){
- printf("Oops: seed is 0 - setting it to 1337\n");
- seed = 1337;
- }
- printf("Seed is %d\n",seed);
- int a = 5; //multiplier
- int c = 3; //increment
- int i; //iterator
- printf("Generating random numbers.. \n");
- for(i=0; i<500; i++)
- {
- seed = (a * seed + c) % max;
- rands[i] = seed;
- }
- printf("Done!\n");
- }
- int crand(){
- if(lastRandI > 490)
- lastRandI = 2;
- lastRandI++;
- return rands[lastRandI];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement