indent calligraphy.c

This commit is contained in:
Bill Kendrick 2017-10-15 11:39:45 -07:00
parent 1ee28d1abd
commit 0a167dbc71

View file

@ -41,32 +41,29 @@ typedef struct
float x, y; float x, y;
} Point2D; } Point2D;
static Mix_Chunk * calligraphy_snd; static Mix_Chunk *calligraphy_snd;
static Point2D calligraphy_control_points[4]; static Point2D calligraphy_control_points[4];
static int calligraphy_r, calligraphy_g, calligraphy_b; static int calligraphy_r, calligraphy_g, calligraphy_b;
static int calligraphy_old_thick; static int calligraphy_old_thick;
static Uint32 calligraphy_last_time; static Uint32 calligraphy_last_time;
static SDL_Surface * calligraphy_brush, * calligraphy_colored_brush; static SDL_Surface *calligraphy_brush, *calligraphy_colored_brush;
/* Local Function Prototypes */ /* Local Function Prototypes */
static Point2D calligraphy_PointOnCubicBezier(Point2D* cp, float t); static Point2D calligraphy_PointOnCubicBezier(Point2D * cp, float t);
static void calligraphy_ComputeBezier(Point2D* cp, int numberOfPoints, Point2D* curve); static void calligraphy_ComputeBezier(Point2D * cp, int numberOfPoints, Point2D * curve);
static float calligraphy_dist(float x1, float y1, float x2, float y2); static float calligraphy_dist(float x1, float y1, float x2, float y2);
int calligraphy_init(magic_api * api); int calligraphy_init(magic_api * api);
Uint32 calligraphy_api_version(void); Uint32 calligraphy_api_version(void);
int calligraphy_get_tool_count(magic_api * api); int calligraphy_get_tool_count(magic_api * api);
SDL_Surface * calligraphy_get_icon(magic_api * api, int which); SDL_Surface *calligraphy_get_icon(magic_api * api, int which);
char * calligraphy_get_name(magic_api * api, int which); char *calligraphy_get_name(magic_api * api, int which);
char * calligraphy_get_description(magic_api * api, int which, int mode); char *calligraphy_get_description(magic_api * api, int which, int mode);
void calligraphy_drag(magic_api * api, int which, SDL_Surface * canvas, void calligraphy_drag(magic_api * api, int which, SDL_Surface * canvas,
SDL_Surface * last, int ox, int oy, int x, int y, SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect);
SDL_Rect * update_rect);
void calligraphy_click(magic_api * api, int which, int mode, void calligraphy_click(magic_api * api, int which, int mode,
SDL_Surface * canvas, SDL_Surface * last, SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect);
int x, int y, SDL_Rect * update_rect);
void calligraphy_release(magic_api * api, int which, void calligraphy_release(magic_api * api, int which,
SDL_Surface * canvas, SDL_Surface * last, SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect);
int x, int y, SDL_Rect * update_rect);
void calligraphy_shutdown(magic_api * api); void calligraphy_shutdown(magic_api * api);
void calligraphy_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); void calligraphy_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b);
int calligraphy_requires_colors(magic_api * api, int which); int calligraphy_requires_colors(magic_api * api, int which);
@ -79,19 +76,17 @@ int calligraphy_init(magic_api * api)
{ {
char fname[1024]; char fname[1024];
snprintf(fname, sizeof(fname), "%s/sounds/magic/calligraphy.ogg", snprintf(fname, sizeof(fname), "%s/sounds/magic/calligraphy.ogg", api->data_directory);
api->data_directory);
calligraphy_snd = Mix_LoadWAV(fname); calligraphy_snd = Mix_LoadWAV(fname);
snprintf(fname, sizeof(fname), "%s/images/magic/calligraphy_brush.png", snprintf(fname, sizeof(fname), "%s/images/magic/calligraphy_brush.png", api->data_directory);
api->data_directory);
calligraphy_brush = IMG_Load(fname); calligraphy_brush = IMG_Load(fname);
calligraphy_colored_brush = NULL; calligraphy_colored_brush = NULL;
if (calligraphy_brush == NULL) if (calligraphy_brush == NULL)
return(0); return (0);
calligraphy_last_time = 0; calligraphy_last_time = 0;
@ -100,46 +95,47 @@ int calligraphy_init(magic_api * api)
calligraphy_g = -1; calligraphy_g = -1;
calligraphy_b = -1; calligraphy_b = -1;
return(1); return (1);
} }
Uint32 calligraphy_api_version(void) { return(TP_MAGIC_API_VERSION); } Uint32 calligraphy_api_version(void)
{
return (TP_MAGIC_API_VERSION);
}
// Only one tool: // Only one tool:
int calligraphy_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) int calligraphy_get_tool_count(magic_api * api ATTRIBUTE_UNUSED)
{ {
return(1); return (1);
} }
// Load our icon: // Load our icon:
SDL_Surface * calligraphy_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) SDL_Surface *calligraphy_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED)
{ {
char fname[1024]; char fname[1024];
snprintf(fname, sizeof(fname), "%s/images/magic/calligraphy.png", snprintf(fname, sizeof(fname), "%s/images/magic/calligraphy.png", api->data_directory);
api->data_directory); return (IMG_Load(fname));
return(IMG_Load(fname));
} }
// Return our name, localized: // Return our name, localized:
char * calligraphy_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) char *calligraphy_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
{ {
return(strdup(gettext_noop("Calligraphy"))); return (strdup(gettext_noop("Calligraphy")));
} }
// Return our description, localized: // Return our description, localized:
char * calligraphy_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) char *calligraphy_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED,
int mode ATTRIBUTE_UNUSED)
{ {
return(strdup( return (strdup(gettext_noop("Click and drag the mouse around to draw in calligraphy.")));
gettext_noop("Click and drag the mouse around to draw in calligraphy.")));
} }
void calligraphy_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, void calligraphy_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas,
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, SDL_Rect * update_rect)
SDL_Rect * update_rect)
{ {
Point2D * curve; Point2D *curve;
int i, n_points, thick, new_thick; int i, n_points, thick, new_thick;
Uint32 colr; Uint32 colr;
SDL_Rect src, dest; SDL_Rect src, dest;
@ -178,9 +174,7 @@ void calligraphy_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface *
calligraphy_control_points[2].x, calligraphy_control_points[2].x,
calligraphy_control_points[2].y) + calligraphy_control_points[2].y) +
calligraphy_dist(calligraphy_control_points[2].x, calligraphy_dist(calligraphy_control_points[2].x,
calligraphy_control_points[2].y, calligraphy_control_points[2].y, calligraphy_control_points[3].x, calligraphy_control_points[3].y);
calligraphy_control_points[3].x,
calligraphy_control_points[3].y);
if (n_points == 0) if (n_points == 0)
return; // No-op; not any points to plot return; // No-op; not any points to plot
@ -190,17 +184,13 @@ void calligraphy_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface *
calligraphy_ComputeBezier(calligraphy_control_points, n_points, curve); calligraphy_ComputeBezier(calligraphy_control_points, n_points, curve);
colr = SDL_MapRGB(canvas->format, colr = SDL_MapRGB(canvas->format, calligraphy_r, calligraphy_g, calligraphy_b);
calligraphy_r,
calligraphy_g,
calligraphy_b);
new_thick = 40 - min((n_points /* / 2 */), 32); new_thick = 40 - min((n_points /* / 2 */ ), 32);
for (i = 0; i < n_points - 1; i++) for (i = 0; i < n_points - 1; i++)
{ {
thick = ((new_thick * i) + thick = ((new_thick * i) + (calligraphy_old_thick * (n_points - i))) / n_points;
(calligraphy_old_thick * (n_points - i))) / n_points;
/* The new way, using an antialiased brush bitmap */ /* The new way, using an antialiased brush bitmap */
@ -253,8 +243,20 @@ void calligraphy_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface *
if (ox > x) { int tmp = ox; ox = x; x = tmp; } if (ox > x)
if (oy > y) { int tmp = oy; oy = y; y = tmp; } {
int tmp = ox;
ox = x;
x = tmp;
}
if (oy > y)
{
int tmp = oy;
oy = y;
y = tmp;
}
update_rect->x = ox - 16; update_rect->x = ox - 16;
update_rect->y = oy - 16; update_rect->y = oy - 16;
@ -313,9 +315,7 @@ void calligraphy_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b)
Uint8 a; Uint8 a;
Uint32 amask; Uint32 amask;
if (calligraphy_r == r && if (calligraphy_r == r && calligraphy_g == g && calligraphy_b == b)
calligraphy_g == g &&
calligraphy_b == b)
return; return;
calligraphy_r = r; calligraphy_r = r;
@ -325,9 +325,7 @@ void calligraphy_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b)
if (calligraphy_colored_brush != NULL) if (calligraphy_colored_brush != NULL)
SDL_FreeSurface(calligraphy_colored_brush); SDL_FreeSurface(calligraphy_colored_brush);
amask = ~(calligraphy_brush->format->Rmask | amask = ~(calligraphy_brush->format->Rmask | calligraphy_brush->format->Gmask | calligraphy_brush->format->Bmask);
calligraphy_brush->format->Gmask |
calligraphy_brush->format->Bmask);
calligraphy_colored_brush = calligraphy_colored_brush =
SDL_CreateRGBSurface(SDL_SWSURFACE, SDL_CreateRGBSurface(SDL_SWSURFACE,
@ -335,8 +333,7 @@ void calligraphy_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b)
calligraphy_brush->h, calligraphy_brush->h,
calligraphy_brush->format->BitsPerPixel, calligraphy_brush->format->BitsPerPixel,
calligraphy_brush->format->Rmask, calligraphy_brush->format->Rmask,
calligraphy_brush->format->Gmask, calligraphy_brush->format->Gmask, calligraphy_brush->format->Bmask, amask);
calligraphy_brush->format->Bmask, amask);
if (calligraphy_colored_brush == NULL) if (calligraphy_colored_brush == NULL)
return; // FIXME: Error! return; // FIXME: Error!
@ -349,14 +346,10 @@ void calligraphy_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b)
{ {
for (x = 0; x < calligraphy_brush->w; x++) for (x = 0; x < calligraphy_brush->w; x++)
{ {
SDL_GetRGBA(api->getpixel(calligraphy_brush, x, y), SDL_GetRGBA(api->getpixel(calligraphy_brush, x, y), calligraphy_brush->format, &r, &g, &b, &a);
calligraphy_brush->format, &r, &g, &b, &a);
api->putpixel(calligraphy_colored_brush, x, y, api->putpixel(calligraphy_colored_brush, x, y,
SDL_MapRGBA(calligraphy_colored_brush->format, SDL_MapRGBA(calligraphy_colored_brush->format, calligraphy_r, calligraphy_g, calligraphy_b, a));
calligraphy_r,
calligraphy_g,
calligraphy_b, a));
} }
} }
@ -384,7 +377,7 @@ cp[3] is the end point, or P3 in the above diagram
t is the parameter value, 0 <= t <= 1 t is the parameter value, 0 <= t <= 1
*/ */
static Point2D calligraphy_PointOnCubicBezier( Point2D* cp, float t ) static Point2D calligraphy_PointOnCubicBezier(Point2D * cp, float t)
{ {
float ax, bx, cx; float ax, bx, cx;
float ay, by, cy; float ay, by, cy;
@ -420,34 +413,37 @@ static Point2D calligraphy_PointOnCubicBezier( Point2D* cp, float t )
<sizeof(Point2D) numberOfPoints> <sizeof(Point2D) numberOfPoints>
*/ */
static void calligraphy_ComputeBezier(Point2D* cp, int numberOfPoints, Point2D* curve) static void calligraphy_ComputeBezier(Point2D * cp, int numberOfPoints, Point2D * curve)
{ {
float dt; float dt;
int i; int i;
dt = 1.0 / ( numberOfPoints - 1 ); dt = 1.0 / (numberOfPoints - 1);
for( i = 0; i < numberOfPoints; i++) for (i = 0; i < numberOfPoints; i++)
curve[i] = calligraphy_PointOnCubicBezier( cp, i*dt ); curve[i] = calligraphy_PointOnCubicBezier(cp, i * dt);
} }
static float calligraphy_dist(float x1, float y1, float x2, float y2) static float calligraphy_dist(float x1, float y1, float x2, float y2)
{ {
float d; float d;
d = (sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))); d = (sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)));
return d; return d;
} }
void calligraphy_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) void calligraphy_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED,
SDL_Surface * canvas ATTRIBUTE_UNUSED)
{ {
} }
void calligraphy_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) void calligraphy_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED,
SDL_Surface * canvas ATTRIBUTE_UNUSED)
{ {
} }
int calligraphy_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) int calligraphy_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
{ {
return(MODE_PAINT); return (MODE_PAINT);
} }