indent tornado.c

This commit is contained in:
Bill Kendrick 2017-10-15 11:51:13 -07:00
parent cf3173690d
commit 011f7f27c1

View file

@ -40,20 +40,22 @@
/* Our globals: */
enum { SIDE_LEFT, SIDE_RIGHT };
enum { LEAFSIDE_RIGHT_DOWN,
LEAFSIDE_LEFT_DOWN,
LEAFSIDE_RIGHT_UP,
LEAFSIDE_LEFT_UP };
enum
{ SIDE_LEFT, SIDE_RIGHT };
enum
{ LEAFSIDE_RIGHT_DOWN,
LEAFSIDE_LEFT_DOWN,
LEAFSIDE_RIGHT_UP,
LEAFSIDE_LEFT_UP
};
static Mix_Chunk /* * tornado_click_snd, */ * tornado_release_snd;
static Mix_Chunk /* * tornado_click_snd, */ * tornado_release_snd;
static Uint8 tornado_r, tornado_g, tornado_b;
static int tornado_min_x, tornado_max_x, tornado_bottom_x, tornado_bottom_y;
static int tornado_side_first;
static int tornado_side_decided;
static SDL_Surface * tornado_base, * tornado_cloud,
* tornado_cloud_colorized;
static int top_w;
static SDL_Surface *tornado_base, *tornado_cloud, *tornado_cloud_colorized;
static int top_w;
/* Local function prototypes: */
@ -62,24 +64,22 @@ typedef struct
float x, y;
} Point2D;
static void tornado_predrag(magic_api * api, SDL_Surface * canvas,
SDL_Surface * last, int ox, int oy, int x, int y);
static void tornado_predrag(magic_api * api, SDL_Surface * canvas, SDL_Surface * last, int ox, int oy, int x, int y);
static void tornado_drawbase(magic_api * api, SDL_Surface * canvas);
static void tornado_drawstalk(magic_api * api, SDL_Surface * canvas, SDL_Surface * last,
int top_x, int top_y, int minx, int maxx,
int bottom_x, int bottom_y, int final);
int top_x, int top_y, int minx, int maxx, int bottom_x, int bottom_y, int final);
static void tornado_drawtornado(magic_api * api, SDL_Surface * canvas, int x, int y);
static Point2D tornado_PointOnCubicBezier(Point2D* cp, float t);
static void tornado_ComputeBezier(Point2D* cp, int numberOfPoints, Point2D* curve);
static Point2D tornado_PointOnCubicBezier(Point2D * cp, float t);
static void tornado_ComputeBezier(Point2D * cp, int numberOfPoints, Point2D * curve);
static void tornado_colorize_cloud(magic_api * api);
static Uint32 tornado_mess(Uint32 pixel, SDL_Surface * canvas);
Uint32 tornado_api_version(void);
int tornado_init(magic_api * api);
int tornado_get_tool_count(magic_api * api);
SDL_Surface * tornado_get_icon(magic_api * api, int which);
char * tornado_get_name(magic_api * api, int which);
SDL_Surface *tornado_get_icon(magic_api * api, int which);
char *tornado_get_name(magic_api * api, int which);
char * tornado_get_description(magic_api * api, int which, int mode);
char *tornado_get_description(magic_api * api, int which, int mode);
@ -87,14 +87,11 @@ char * tornado_get_description(magic_api * api, int which, int mode);
void tornado_drag(magic_api * api, int which, SDL_Surface * canvas,
SDL_Surface * last, int ox, int oy, int x, int y,
SDL_Rect * update_rect);
SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect);
void tornado_click(magic_api * api, int which, int mode,
SDL_Surface * canvas, SDL_Surface * last,
int x, int y, SDL_Rect * update_rect);
SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect);
void tornado_release(magic_api * api, int which,
SDL_Surface * canvas, SDL_Surface * last,
int x, int y, SDL_Rect * update_rect);
SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect);
void tornado_shutdown(magic_api * api);
void tornado_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b);
@ -105,7 +102,10 @@ int tornado_modes(magic_api * api, int which);
Uint32 tornado_api_version(void) { return(TP_MAGIC_API_VERSION); }
Uint32 tornado_api_version(void)
{
return (TP_MAGIC_API_VERSION);
}
// No setup required:
@ -119,53 +119,49 @@ int tornado_init(magic_api * api)
tornado_click_snd = Mix_LoadWAV(fname);
*/
snprintf(fname, sizeof(fname), "%s/sounds/magic/tornado_release.ogg",
api->data_directory);
snprintf(fname, sizeof(fname), "%s/sounds/magic/tornado_release.ogg", api->data_directory);
tornado_release_snd = Mix_LoadWAV(fname);
snprintf(fname, sizeof(fname), "%s/images/magic/tornado_base.png",
api->data_directory);
snprintf(fname, sizeof(fname), "%s/images/magic/tornado_base.png", api->data_directory);
tornado_base = IMG_Load(fname);
snprintf(fname, sizeof(fname), "%s/images/magic/tornado_cloud.png",
api->data_directory);
snprintf(fname, sizeof(fname), "%s/images/magic/tornado_cloud.png", api->data_directory);
tornado_cloud = IMG_Load(fname);
return(1);
return (1);
}
// We have multiple tools:
int tornado_get_tool_count(magic_api * api ATTRIBUTE_UNUSED)
{
return(1);
return (1);
}
// Load our icons:
SDL_Surface * tornado_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED)
SDL_Surface *tornado_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED)
{
char fname[1024];
snprintf(fname, sizeof(fname), "%s/images/magic/tornado.png",
api->data_directory);
snprintf(fname, sizeof(fname), "%s/images/magic/tornado.png", api->data_directory);
return(IMG_Load(fname));
return (IMG_Load(fname));
}
// Return our names, localized:
char * tornado_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
char *tornado_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
{
return(strdup(gettext_noop("Tornado")));
return (strdup(gettext_noop("Tornado")));
}
// Return our descriptions, localized:
char * tornado_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED)
char *tornado_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED)
{
return(strdup(gettext_noop("Click and drag to draw a tornado funnel on your picture.")));
return (strdup(gettext_noop("Click and drag to draw a tornado funnel on your picture.")));
}
// Affect the canvas on drag:
static void tornado_predrag(magic_api * api ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED,
SDL_Surface * last ATTRIBUTE_UNUSED, int ox, int oy, int x, int y)
SDL_Surface * last ATTRIBUTE_UNUSED, int ox, int oy, int x, int y)
{
if (x < tornado_min_x)
tornado_min_x = x;
@ -184,23 +180,22 @@ static void tornado_predrag(magic_api * api ATTRIBUTE_UNUSED, SDL_Surface * canv
// Determine which way to bend first:
//
if (tornado_side_decided == 0)
{
if (x < tornado_bottom_x - 10)
{
tornado_side_first = SIDE_LEFT;
tornado_side_decided = 1;
if (x < tornado_bottom_x - 10)
{
tornado_side_first = SIDE_LEFT;
tornado_side_decided = 1;
}
else if (x > tornado_bottom_x + 10)
{
tornado_side_first = SIDE_RIGHT;
tornado_side_decided = 1;
}
}
else if (x > tornado_bottom_x + 10)
{
tornado_side_first = SIDE_RIGHT;
tornado_side_decided = 1;
}
}
}
void tornado_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas,
SDL_Surface * last, int ox, int oy, int x, int y,
SDL_Rect * update_rect)
SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect)
{
tornado_predrag(api, canvas, last, ox, oy, x, y);
@ -213,32 +208,30 @@ void tornado_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * can
/* Draw the base and the stalk (low-quality) for now: */
tornado_drawstalk(api, canvas, last,
x, y, tornado_min_x, tornado_max_x,
tornado_bottom_x, tornado_bottom_y, !(api->button_down()));
x, y, tornado_min_x, tornado_max_x, tornado_bottom_x, tornado_bottom_y, !(api->button_down()));
tornado_drawbase(api, canvas);
update_rect->x = 0;
update_rect->y = 0;
update_rect->w = canvas->w;
update_rect->w = canvas->w;
update_rect->h = canvas->h;
}
// Affect the canvas on click:
void tornado_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED,
SDL_Surface * canvas, SDL_Surface * last,
int x, int y, SDL_Rect * update_rect)
SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect)
{
tornado_min_x = x;
tornado_max_x = x;
tornado_bottom_x = x;
tornado_bottom_y = y;// - tornado_base->h;
tornado_bottom_y = y; // - tornado_base->h;
tornado_side_decided = 0;
tornado_side_first = SIDE_LEFT;
tornado_drag(api, which, canvas, last, x, y, x, y, update_rect);
/*
api->playsound(tornado_click_snd, (x * 255) / canvas->w, 255);
*/
@ -246,8 +239,7 @@ void tornado_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED,
// Affect the canvas on release:
void tornado_release(magic_api * api, int which ATTRIBUTE_UNUSED,
SDL_Surface * canvas, SDL_Surface * last,
int x, int y, SDL_Rect * update_rect)
SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect)
{
/* Don't let tornado be too low compared to base: */
@ -259,7 +251,7 @@ void tornado_release(magic_api * api, int which ATTRIBUTE_UNUSED,
tornado_predrag(api, canvas, last, x, y, x, y);
/* Erase any old stuff: */
SDL_BlitSurface(last, NULL, canvas, NULL);
@ -267,15 +259,13 @@ void tornado_release(magic_api * api, int which ATTRIBUTE_UNUSED,
/* Draw high-quality stalk, and tornado: */
tornado_drawstalk(api, canvas, last,
x, y, tornado_min_x, tornado_max_x,
tornado_bottom_x, tornado_bottom_y, 1);
tornado_drawstalk(api, canvas, last, x, y, tornado_min_x, tornado_max_x, tornado_bottom_x, tornado_bottom_y, 1);
tornado_drawtornado(api, canvas, x, y);
tornado_drawtornado(api, canvas, x, y);
tornado_drawbase(api, canvas);
tornado_drawbase(api, canvas);
update_rect->x = 0;
update_rect->y = 0;
update_rect->w = canvas->w;
@ -287,10 +277,10 @@ void tornado_release(magic_api * api, int which ATTRIBUTE_UNUSED,
static void tornado_drawtornado(magic_api * api, SDL_Surface * canvas, int x, int y)
{
SDL_Surface * aux_surf;
SDL_Surface *aux_surf;
SDL_Rect dest;
aux_surf = api->scale(tornado_cloud_colorized, top_w *2, top_w,0);
aux_surf = api->scale(tornado_cloud_colorized, top_w * 2, top_w, 0);
dest.x = x - (aux_surf->w / 2);
dest.y = y - (aux_surf->h / 2);
@ -311,21 +301,19 @@ static void tornado_drawbase(magic_api * api ATTRIBUTE_UNUSED, SDL_Surface * can
static Uint32 tornado_mess(Uint32 pixel, SDL_Surface * canvas)
{
Uint8 r, g, b, a;
float f = (float)rand()*255/RAND_MAX;
float f = (float)rand() * 255 / RAND_MAX;
SDL_GetRGBA(pixel, canvas->format, &r, &g, &b, &a);
return (SDL_MapRGBA(canvas->format,
(tornado_r + r + (Uint8)f * 2) / 4,
(tornado_g + g + (Uint8)f * 2) / 4,
(tornado_b + b + (Uint8)f * 2) / 4,
a));
(tornado_r + r + (Uint8) f * 2) / 4,
(tornado_g + g + (Uint8) f * 2) / 4, (tornado_b + b + (Uint8) f * 2) / 4, a));
}
static void tornado_drawstalk(magic_api * api, SDL_Surface * canvas, SDL_Surface * last,
int top_x, int top_y, int minx, int maxx,
int bottom_x, int bottom_y, int final)
int top_x, int top_y, int minx, int maxx, int bottom_x, int bottom_y, int final)
{
Point2D control_points[4];
Point2D * curve;
Point2D *curve;
int i, n_points;
int left, right;
SDL_Rect dest;
@ -340,22 +328,22 @@ static void tornado_drawstalk(magic_api * api, SDL_Surface * canvas, SDL_Surface
control_points[0].y = top_y;
if (tornado_side_first == SIDE_LEFT)
{
control_points[1].x = minx;
control_points[2].x = maxx;
}
{
control_points[1].x = minx;
control_points[2].x = maxx;
}
else
{
control_points[1].x = maxx;
control_points[2].x = minx;
}
{
control_points[1].x = maxx;
control_points[2].x = minx;
}
control_points[1].y = ((bottom_y - top_y) / 3) + top_y;
control_points[2].y = (((bottom_y - top_y) / 3) * 2) + top_y;
control_points[3].x = bottom_x;
control_points[3].y = bottom_y;
if (final == 0)
n_points = 8;
else
@ -370,58 +358,61 @@ static void tornado_drawstalk(magic_api * api, SDL_Surface * canvas, SDL_Surface
top_w = max(32, n_points * n_points / 1000);
/* Draw the curve: */
for (i = 0; i < n_points - 1; i++)
{
if (final == 0)
{
dest.x = curve[i].x;
dest.y = curve[i].y;
dest.w = 2;
dest.h = 2;
SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, 0, 0, 0));
}
else
{
ii = n_points - i;
/* min 10 pixels then ii^2 / 2000 or 4 * ii^2 / canvas->w,
don't let the top of funnel be wider than the half of canvas */
if (n_points * n_points / 2000 > canvas->w / 4)
ww = 4 * n_points * n_points / canvas->w;
if (final == 0)
{
dest.x = curve[i].x;
dest.y = curve[i].y;
dest.w = 2;
dest.h = 2;
SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, 0, 0, 0));
}
else
ww = 2000;
{
ii = n_points - i;
/* min 10 pixels then ii^2 / 2000 or 4 * ii^2 / canvas->w,
don't let the top of funnel be wider than the half of canvas */
if (n_points * n_points / 2000 > canvas->w / 4)
ww = 4 * n_points * n_points / canvas->w;
else
ww = 2000;
left = min(curve[i].x, curve[i + 1].x)-5-ii*ii/ww;
right = max(curve[i].x, curve[i + 1].x)+5+ii*ii/ww;
left = min(curve[i].x, curve[i + 1].x) - 5 - ii * ii / ww;
right = max(curve[i].x, curve[i + 1].x) + 5 + ii * ii / ww;
dest.x = left;
dest.y = curve[i].y;
dest.w = right - left + 1;
dest.h = 2;
dest.x = left;
dest.y = curve[i].y;
dest.w = right - left + 1;
dest.h = 2;
}
rotation += 3;
/* The body of the tornado: 3x 1y rotation + some random particles */
for (p = dest.x; p < dest.x + dest.w; p++)
{
if ((float)rand() * 100 / RAND_MAX > 10)
{
api->putpixel(canvas, p, dest.y, api->getpixel(last, dest.x + (p - dest.x + rotation) % dest.w, dest.y));
}
else
{
api->putpixel(canvas, p, dest.y,
tornado_mess(api->getpixel(last, dest.x + (p - dest.x + rotation) % dest.w, dest.y),
canvas));
}
}
/* Some random particles flying around the tornado */
for (p = dest.x - dest.w * 20 / 100; p < dest.x + dest.w + dest.w * 20 / 100; p++)
{
if ((float)rand() * 100 / RAND_MAX < 5 && ((p < dest.x) || (p > dest.w)))
api->putpixel(canvas, p, dest.y,
tornado_mess(api->getpixel(last, dest.x + (p - dest.x + rotation) % dest.w, dest.y), canvas));
}
}
rotation +=3;
/* The body of the tornado: 3x 1y rotation + some random particles */
for (p = dest.x; p < dest.x + dest.w; p++)
{
if ((float)rand() * 100 / RAND_MAX > 10 )
{
api->putpixel(canvas, p, dest.y, api->getpixel(last, dest.x + (p - dest.x + rotation) % dest.w , dest.y));
}
else
{
api->putpixel(canvas, p, dest.y, tornado_mess(api->getpixel(last, dest.x + (p - dest.x + rotation) % dest.w , dest.y), canvas));
}
}
/* Some random particles flying around the tornado */
for (p = dest.x - dest.w * 20 / 100; p < dest.x + dest.w + dest.w * 20 / 100; p++)
{
if ((float)rand() * 100 / RAND_MAX < 5 && ((p < dest.x) || (p > dest.w)))
api->putpixel(canvas, p, dest.y, tornado_mess(api->getpixel(last, dest.x + (p - dest.x + rotation) % dest.w , dest.y), canvas));
}
}
free(curve);
}
@ -474,32 +465,32 @@ cp[3] is the end point, or P3 in the above diagram
t is the parameter value, 0 <= t <= 1
*/
static Point2D tornado_PointOnCubicBezier( Point2D* cp, float t )
static Point2D tornado_PointOnCubicBezier(Point2D * cp, float t)
{
float ax, bx, cx;
float ay, by, cy;
float tSquared, tCubed;
Point2D result;
float ax, bx, cx;
float ay, by, cy;
float tSquared, tCubed;
Point2D result;
/* calculate the polynomial coefficients */
/* calculate the polynomial coefficients */
cx = 3.0 * (cp[1].x - cp[0].x);
bx = 3.0 * (cp[2].x - cp[1].x) - cx;
ax = cp[3].x - cp[0].x - cx - bx;
cy = 3.0 * (cp[1].y - cp[0].y);
by = 3.0 * (cp[2].y - cp[1].y) - cy;
ay = cp[3].y - cp[0].y - cy - by;
/* calculate the curve point at parameter value t */
tSquared = t * t;
tCubed = tSquared * t;
result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
return result;
cx = 3.0 * (cp[1].x - cp[0].x);
bx = 3.0 * (cp[2].x - cp[1].x) - cx;
ax = cp[3].x - cp[0].x - cx - bx;
cy = 3.0 * (cp[1].y - cp[0].y);
by = 3.0 * (cp[2].y - cp[1].y) - cy;
ay = cp[3].y - cp[0].y - cy - by;
/* calculate the curve point at parameter value t */
tSquared = t * t;
tCubed = tSquared * t;
result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
return result;
}
/*
@ -509,15 +500,15 @@ static Point2D tornado_PointOnCubicBezier( Point2D* cp, float t )
<sizeof(Point2D) numberOfPoints>
*/
static void tornado_ComputeBezier( Point2D* cp, int numberOfPoints, Point2D* curve )
static void tornado_ComputeBezier(Point2D * cp, int numberOfPoints, Point2D * curve)
{
float dt;
int i;
float dt;
int i;
dt = 1.0 / ( numberOfPoints - 1 );
dt = 1.0 / (numberOfPoints - 1);
for( i = 0; i < numberOfPoints; i++)
curve[i] = tornado_PointOnCubicBezier( cp, i*dt );
for (i = 0; i < numberOfPoints; i++)
curve[i] = tornado_PointOnCubicBezier(cp, i * dt);
}
@ -532,9 +523,7 @@ static void tornado_colorize_cloud(magic_api * api)
/* Create a surface to render into: */
amask = ~(tornado_cloud->format->Rmask |
tornado_cloud->format->Gmask |
tornado_cloud->format->Bmask);
amask = ~(tornado_cloud->format->Rmask | tornado_cloud->format->Gmask | tornado_cloud->format->Bmask);
tornado_cloud_colorized =
SDL_CreateRGBSurface(SDL_SWSURFACE,
@ -542,8 +531,7 @@ static void tornado_colorize_cloud(magic_api * api)
tornado_cloud->h,
tornado_cloud->format->BitsPerPixel,
tornado_cloud->format->Rmask,
tornado_cloud->format->Gmask,
tornado_cloud->format->Bmask, amask);
tornado_cloud->format->Gmask, tornado_cloud->format->Bmask, amask);
/* Render the new cloud: */
@ -551,32 +539,33 @@ static void tornado_colorize_cloud(magic_api * api)
SDL_LockSurface(tornado_cloud_colorized);
for (y = 0; y < tornado_cloud->h; y++)
{
for (x = 0; x < tornado_cloud->w; x++)
{
SDL_GetRGBA(api->getpixel(tornado_cloud, x, y),
tornado_cloud->format, &r, &g, &b, &a);
for (x = 0; x < tornado_cloud->w; x++)
{
SDL_GetRGBA(api->getpixel(tornado_cloud, x, y), tornado_cloud->format, &r, &g, &b, &a);
api->putpixel(tornado_cloud_colorized, x, y,
SDL_MapRGBA(tornado_cloud_colorized->format,
(tornado_r + r * 2) / 3, (tornado_g + g * 2) / 3, (tornado_b + b * 2) / 3, a));
api->putpixel(tornado_cloud_colorized, x, y,
SDL_MapRGBA(tornado_cloud_colorized->format,
(tornado_r + r * 2) / 3, (tornado_g + g * 2) / 3, (tornado_b + b * 2) / 3, a));
}
}
}
SDL_UnlockSurface(tornado_cloud_colorized);
SDL_UnlockSurface(tornado_cloud);
}
void tornado_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED)
void tornado_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED,
SDL_Surface * canvas ATTRIBUTE_UNUSED)
{
}
void tornado_switchout(magic_api * api, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED)
void tornado_switchout(magic_api * api, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED,
SDL_Surface * canvas ATTRIBUTE_UNUSED)
{
api->stopsound();
}
int tornado_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
{
return(MODE_PAINT_WITH_PREVIEW);
return (MODE_PAINT_WITH_PREVIEW);
}