Created
February 12, 2011 19:21
-
-
Save stevenlr/824019 to your computer and use it in GitHub Desktop.
plasma effect demoscene in C width SDL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SDL/SDL.h> | |
#include <math.h> | |
typedef unsigned char uchar; | |
void ppixel(SDL_Surface *screen, int x, int y, uchar r, uchar g, uchar b); | |
int main(int argc, char *argv[]) | |
{ | |
SDL_Event event; | |
Uint32 last_time; | |
int cont = 1, x, y; | |
uchar r[256], g[256], b[256]; | |
double f = 0; | |
SDL_Surface *screen; | |
SDL_Init(SDL_INIT_VIDEO); | |
screen = SDL_SetVideoMode(320, 200, 32, SDL_HWSURFACE); | |
for(x = 0; x < 256; x++) | |
{ | |
r[x] = 255 - ceil((sin(3.14 * 2 * x / 255) + 1) * 127); | |
g[x] = ceil((sin(3.14 * 2 * x / 127.0) + 1) * 64); | |
b[x] = 255 - r[x]; | |
} | |
last_time = SDL_GetTicks(); | |
while(cont == 1) | |
{ | |
while(SDL_PollEvent(&event)) | |
{ | |
switch(event.type) | |
{ | |
case SDL_QUIT: | |
cont = 0; | |
break; | |
} | |
} | |
for(y = 0; y < 200; y++) | |
{ | |
for(x = 0; x < 320; x++) | |
{ | |
float c1 = sin(x / 50.0 + f + y / 200.0); | |
float c2 = sqrt((sin(0.8 * f) * 160 - x + 160) * (sin(0.8 * f) * 160 - x + 160) + (cos(1.2 * f) * 100 - y + 100) * (cos(1.2 * f) * 100 - y + 100)); | |
c2 = sin(c2 / 50.0); | |
float c3 = (c1 + c2) / 2; | |
int res = ceil((c3 + 1) * 127); | |
ppixel(screen, x, y, r[res], g[res], b[res]); | |
} | |
} | |
SDL_Flip(screen); | |
f += 0.1; | |
if(SDL_GetTicks() - last_time < 40) | |
{ | |
SDL_Delay(40 - (SDL_GetTicks() - last_time)); | |
} | |
last_time = SDL_GetTicks(); | |
} | |
SDL_Quit(); | |
return 0; | |
} | |
void ppixel(SDL_Surface *screen, int x, int y, uchar r, uchar g, uchar b) | |
{ | |
unsigned long color = SDL_MapRGB(screen->format, r, g, b); | |
unsigned long *bufp; | |
bufp = (unsigned long *) screen->pixels + y * screen->pitch / 4 + x; | |
*bufp = color; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment