Created
May 3, 2011 15:53
-
-
Save neuro-sys/953599 to your computer and use it in GitHub Desktop.
old school plasma effect f'd
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 <allegro.h> | |
#include <math.h> | |
#include <time.h> | |
#define WIDTH 320 | |
#define HEIGHT 240 | |
int progress = 0; | |
int plasma1Thickness = 10, plasma2Thickness = 10, plasma3Thickness = 10; | |
int getUserInput() { | |
if (!keypressed()) | |
return 0; | |
printf("plasma1 = %d\n", plasma1Thickness); | |
printf("plasma2 = %d\n", plasma2Thickness); | |
printf("plasma3 = %d\n", plasma3Thickness); | |
if (key[KEY_A]) | |
plasma1Thickness++; | |
else if (key[KEY_Z]) | |
plasma1Thickness--; | |
else if (key[KEY_S]) | |
plasma2Thickness++; | |
else if (key[KEY_X]) | |
plasma2Thickness--; | |
else if (key[KEY_D]) | |
plasma3Thickness++; | |
else if (key[KEY_C]) | |
plasma3Thickness--; | |
else if (key[KEY_Q]) { | |
printf("EXIT...\n"); | |
return -1; | |
} | |
clear_keybuf(); | |
return 0; | |
} | |
void preCalcPlasmoid(int **p1, int **p2, int **p3) | |
{ | |
int i, j; | |
for (i = 0; i < HEIGHT * 2; i++) { | |
for (j = 0; j < WIDTH * 2; j++) { | |
double d1 = WIDTH / 5 + (WIDTH / 5 * (sin(hypot(j, i) / (WIDTH * 2 / plasma1Thickness)))) ; | |
double d2 = WIDTH / 10 + (WIDTH / 10 * (sin(hypot(WIDTH * 2 - i, HEIGHT * 2 - j) / (WIDTH * 2 / plasma2Thickness)))) ; | |
double d3 = WIDTH / 10 + (WIDTH / 10 * (sin(hypot(WIDTH * 2 + i, HEIGHT * 2 - j) / (WIDTH * 2 / plasma3Thickness)))) ; | |
p1[i][j] = (int) d1; | |
p2[i][j] = (int) d2; | |
p3[i][j] = (int) d3; | |
} | |
if (HEIGHT % 40 == 0) | |
textout_centre_ex(screen, font, "#", progress++, SCREEN_H / 2 + 15, makecol(0, 255, 0), 0); | |
} | |
} | |
int main() | |
{ | |
BITMAP *buffer; | |
BITMAP *buffer2; | |
int **plasma1; | |
int **plasma2; | |
int **plasma3; | |
int i, j; | |
long startTime = clock(); | |
int elapsed; | |
int FPS = 0; | |
int foo; | |
allegro_init(); | |
install_keyboard(); | |
set_color_depth(32); | |
set_gfx_mode(GFX_AUTODETECT_WINDOWED, WIDTH, HEIGHT, 0, 0); | |
textout_centre_ex(screen, font, "LOADING...", SCREEN_W / 2, SCREEN_H / 2, makecol(0, 255, 0), 0); | |
plasma1 = malloc(HEIGHT * 2 * sizeof(int)); | |
plasma2 = malloc(HEIGHT * 2 * sizeof(int)); | |
plasma3 = malloc(HEIGHT * 2 * sizeof(int)); | |
for (i = 0; i < HEIGHT * 2; i++) { | |
plasma1[i] = malloc(WIDTH * 2 * sizeof(int)); | |
if (plasma1[i] == NULL) { | |
printf("OUT OF MEMMORY\n"); | |
return 0; | |
} | |
plasma2[i] = malloc(WIDTH * 2 * sizeof(int)); | |
if (plasma2[i] == NULL) { | |
printf("OUT OF MEMMORY\n"); | |
return 0; | |
} | |
plasma3[i] = malloc(WIDTH * 2 * sizeof(int)); | |
if (plasma3[i] == NULL) { | |
printf("OUT OF MEMMORY\n"); | |
return 0; | |
} | |
} | |
preCalcPlasmoid(plasma1, plasma2, plasma3); | |
buffer = create_bitmap(WIDTH, HEIGHT); | |
buffer2 = create_bitmap(WIDTH, HEIGHT); | |
elapsed = clock(); | |
while (getUserInput() != -1) { | |
FPS++; | |
int x1, y1, x2, y2, x3, y3; | |
int src1, src2, src3; | |
int currentTime = (int) (startTime - clock()) >> 2; | |
x1 = WIDTH / 4 + (int)(WIDTH / 4 * cos( (double) currentTime /57 )); | |
x2 = WIDTH / 4 + (int)(WIDTH / 4 * sin( (double)-currentTime /94 )); | |
x3 = WIDTH / 4 + (int)(WIDTH / 4 * sin( (double)-currentTime /87 )); | |
for (i = 0; i < HEIGHT; i++) { | |
for (j = 0; j < WIDTH; j++) { | |
int sum = plasma1[j + x2][i + x3] + plasma2[j + x3][i + x1] + plasma3[j + x1][i + x2]; | |
int col = makecol(sum + plasma1Thickness , sum + plasma2Thickness, sum + plasma3Thickness); | |
/* | |
int filteredPix = (buffer[i][j - 1]) + (buffer[i][j] + 1) + | |
(buffer[i + 1][j - 1]) + (buffer[i + 1][j]) + (buffer[i + 1][j + 1]) + | |
(buffer[i + 2][j - 1]) + (buffer[i + 2][j]) + (buffer[i + 2][j + 1]); | |
*/ | |
putpixel(buffer, j, i, col); | |
} | |
} | |
// Apply filter... | |
BITMAP *temp = buffer2; | |
buffer2 = buffer; | |
buffer = temp; | |
if (clock() - elapsed >= CLOCKS_PER_SEC) { | |
foo = FPS; | |
FPS = 0; | |
elapsed = clock(); | |
} | |
textprintf_ex(buffer, font, 0, 10, makecol(255, 255, 255), 0, "FPS: %d", foo); | |
blit(buffer, screen, 0, 0, 0, 0, WIDTH, HEIGHT); | |
} | |
textout_centre_ex(screen, font, "EXITING...", SCREEN_W / 2, SCREEN_H / 2, makecol(0, 255, 0), 0); | |
destroy_bitmap(buffer); | |
for (i = 0; i < HEIGHT; i++) { | |
free(plasma1[i]); | |
free(plasma2[i]); | |
} | |
free(plasma1); | |
free(plasma2); | |
return 0; | |
} | |
END_OF_MAIN(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment