Created
November 24, 2011 18:43
-
-
Save neuro-sys/1391991 to your computer and use it in GitHub Desktop.
oldschool twister effect
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 <stdio.h> | |
#include <math.h> | |
#include <SDL/SDL.h> | |
#define LOCK_SURFACE(A) if (SDL_MUSTLOCK(A)) if (SDL_LockSurface(A) < 0) { printf("%s\n", SDL_GetError()); exit(1); } | |
#define UNLOCK_SURFACE(A) if (SDL_MUSTLOCK(A)) SDL_UnlockSurface(A); | |
#define ANGLE(A) (A) * M_PI / 180. | |
#define W 320 | |
#define H 200 | |
#define BPP 24 | |
#define BAR_W 100 | |
#define BAR_X W / 2 | |
float aa = 1; | |
SDL_Surface* video; | |
Uint8* keys; | |
void inline put_pixel(SDL_Surface *s, int x, int y, Uint32 col) | |
{ | |
LOCK_SURFACE(s); | |
Uint8 *p = (Uint8 *) s->pixels + (s->pitch * y) + x * s->format->BytesPerPixel; | |
*(Uint32 *)p = col; | |
UNLOCK_SURFACE(s); | |
} | |
Uint32 inline get_pixel(SDL_Surface *s, int x, int y) | |
{ | |
Uint8 *p = (Uint8 *) s->pixels + (s->pitch * y) + x * s->format->BytesPerPixel; | |
return *(Uint32 *)p; | |
} | |
void inline draw_hline(SDL_Surface *s, int x, int y, int len, Uint32 col) | |
{ | |
LOCK_SURFACE(s); | |
Uint8 *p = (Uint8 *) s->pixels + (s->pitch * y) + x * s->format->BytesPerPixel; | |
int i; | |
for (i = 0; i < len; i++) { | |
Uint8 r, g, b; | |
*(Uint32 *)p = col; | |
p += s->format->BytesPerPixel; | |
} | |
UNLOCK_SURFACE(s); | |
} | |
void draw_twister() | |
{ | |
int x, y; | |
float amp = sin(aa / 60.) * M_PI + M_PI * 2; /* near the zero, higher frequency. (we don't want too much of that) */ | |
float ang = sin(aa / 40.) * M_PI + M_PI ; /* [0 -> M_PI * 2] */ | |
for (y = 0; y < video->h; y++) | |
{ | |
int x1, x2, x3, x4; | |
x1 = BAR_W / 2 + BAR_W / 2 * sin(ANGLE(y / amp) + ang); | |
x2 = BAR_W / 2 + BAR_W / 2 * sin(ANGLE(y / amp) + ang + M_PI / 2); | |
x3 = BAR_W / 2 + BAR_W / 2 * sin(ANGLE(y / amp) + ang + M_PI); | |
x4 = BAR_W / 2 + BAR_W / 2 * sin(ANGLE(y / amp) + ang + M_PI + M_PI / 2); | |
if (x1 < x2) draw_hline(video, BAR_X + x1, y, x2 - x1, 0xff); | |
if (x2 < x3) draw_hline(video, BAR_X + x2, y, x3 - x2, 0xff << 8); | |
if (x3 < x4) draw_hline(video, BAR_X + x3, y, x4 - x3, 0xff << 16); | |
if (x4 < x1) draw_hline(video, BAR_X + x4, y, x1 - x4, 0xffff00); | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { printf("%s\n", SDL_GetError()); exit(1); } | |
if ((video = SDL_SetVideoMode(W, H, BPP, SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL)) == NULL) { printf("%s\n", SDL_GetError()); exit(1); } | |
keys = SDL_GetKeyState(NULL); | |
while(!keys[SDLK_ESCAPE]) | |
{ | |
SDL_FillRect(video, NULL, 0); | |
SDL_PumpEvents(); | |
draw_twister(); | |
SDL_Flip(video); | |
aa++; | |
SDL_Delay(1000/30.); | |
} | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment