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,53 +95,54 @@ 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;
// if (SDL_GetTicks() < calligraphy_last_time + 5) // if (SDL_GetTicks() < calligraphy_last_time + 5)
// return; // return;
calligraphy_control_points[0].x = calligraphy_control_points[1].x; calligraphy_control_points[0].x = calligraphy_control_points[1].x;
calligraphy_control_points[0].y = calligraphy_control_points[1].y; calligraphy_control_points[0].y = calligraphy_control_points[1].y;
calligraphy_control_points[1].x = calligraphy_control_points[2].x; calligraphy_control_points[1].x = calligraphy_control_points[2].x;
@ -170,82 +166,76 @@ void calligraphy_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface *
*/ */
n_points = calligraphy_dist(calligraphy_control_points[0].x, n_points = calligraphy_dist(calligraphy_control_points[0].x,
calligraphy_control_points[0].y, calligraphy_control_points[0].y,
calligraphy_control_points[1].x, calligraphy_control_points[1].x,
calligraphy_control_points[1].y) + calligraphy_control_points[1].y) +
calligraphy_dist(calligraphy_control_points[1].x, calligraphy_dist(calligraphy_control_points[1].x,
calligraphy_control_points[1].y, calligraphy_control_points[1].y,
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
curve = (Point2D *) malloc(sizeof(Point2D) * n_points); curve = (Point2D *) malloc(sizeof(Point2D) * n_points);
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) +
(calligraphy_old_thick * (n_points - i))) / n_points;
/* The new way, using an antialiased brush bitmap */
x = curve[i].x;
y = curve[i].y;
src.x = calligraphy_brush->w - thick / 2 - thick / 4;
src.w = thick / 2 + thick / 4;
src.y = 0;
src.h = thick / 4;
dest.x = x - thick / 4;
dest.y = y - thick / 4;
SDL_BlitSurface(calligraphy_colored_brush, &src, canvas, &dest);
src.x = 0;
src.w = thick / 2 + thick / 4;
src.y = calligraphy_brush->h - thick / 4;
src.h = thick / 4;
dest.x = x - thick / 2;
dest.y = y;
SDL_BlitSurface(calligraphy_colored_brush, &src, canvas, &dest);
/* Old way; using putpixel:
SDL_LockSurface(canvas);
for (j = -(thick / 2); j < (thick / 2) + 1; j++)
{ {
x = curve[i].x + j; thick = ((new_thick * i) + (calligraphy_old_thick * (n_points - i))) / n_points;
y = curve[i].y - (j / 2); // 30 degrees
api->putpixel(canvas, x, y, colr);
api->putpixel(canvas, x + 1, y, colr); /* The new way, using an antialiased brush bitmap */
api->putpixel(canvas, x, y + 1, colr);
api->putpixel(canvas, x + 1, y + 1, colr); x = curve[i].x;
y = curve[i].y;
src.x = calligraphy_brush->w - thick / 2 - thick / 4;
src.w = thick / 2 + thick / 4;
src.y = 0;
src.h = thick / 4;
dest.x = x - thick / 4;
dest.y = y - thick / 4;
SDL_BlitSurface(calligraphy_colored_brush, &src, canvas, &dest);
src.x = 0;
src.w = thick / 2 + thick / 4;
src.y = calligraphy_brush->h - thick / 4;
src.h = thick / 4;
dest.x = x - thick / 2;
dest.y = y;
SDL_BlitSurface(calligraphy_colored_brush, &src, canvas, &dest);
/* Old way; using putpixel:
SDL_LockSurface(canvas);
for (j = -(thick / 2); j < (thick / 2) + 1; j++)
{
x = curve[i].x + j;
y = curve[i].y - (j / 2); // 30 degrees
api->putpixel(canvas, x, y, colr);
api->putpixel(canvas, x + 1, y, colr);
api->putpixel(canvas, x, y + 1, colr);
api->putpixel(canvas, x + 1, y + 1, colr);
}
SDL_UnlockSurface(canvas);
*/
} }
SDL_UnlockSurface(canvas);
*/
}
calligraphy_old_thick = (calligraphy_old_thick + new_thick) / 2; calligraphy_old_thick = (calligraphy_old_thick + new_thick) / 2;
@ -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;
@ -272,8 +274,8 @@ void calligraphy_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface *
} }
void calligraphy_click(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, void calligraphy_click(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED,
SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED,
int x, int y, SDL_Rect * update_rect ATTRIBUTE_UNUSED) int x, int y, SDL_Rect * update_rect ATTRIBUTE_UNUSED)
{ {
calligraphy_old_thick = 8; calligraphy_old_thick = 8;
calligraphy_last_time = 0; calligraphy_last_time = 0;
@ -290,8 +292,8 @@ void calligraphy_click(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNU
void calligraphy_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, void calligraphy_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED,
SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED,
int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED)
{ {
} }
@ -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,30 +333,25 @@ 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!
SDL_LockSurface(calligraphy_brush); SDL_LockSurface(calligraphy_brush);
SDL_LockSurface(calligraphy_colored_brush); SDL_LockSurface(calligraphy_colored_brush);
for (y = 0; y < calligraphy_brush->h; y++) for (y = 0; y < calligraphy_brush->h; y++)
{
for (x = 0; x < calligraphy_brush->w; x++)
{ {
SDL_GetRGBA(api->getpixel(calligraphy_brush, x, y), for (x = 0; x < calligraphy_brush->w; x++)
calligraphy_brush->format, &r, &g, &b, &a); {
SDL_GetRGBA(api->getpixel(calligraphy_brush, x, y), 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));
} }
}
SDL_UnlockSurface(calligraphy_colored_brush); SDL_UnlockSurface(calligraphy_colored_brush);
SDL_UnlockSurface(calligraphy_brush); SDL_UnlockSurface(calligraphy_brush);
@ -384,32 +377,32 @@ 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;
float tSquared, tCubed; float tSquared, tCubed;
Point2D result; Point2D result;
/* calculate the polynomial coefficients */ /* calculate the polynomial coefficients */
cx = 3.0 * (cp[1].x - cp[0].x); cx = 3.0 * (cp[1].x - cp[0].x);
bx = 3.0 * (cp[2].x - cp[1].x) - cx; bx = 3.0 * (cp[2].x - cp[1].x) - cx;
ax = cp[3].x - cp[0].x - cx - bx; ax = cp[3].x - cp[0].x - cx - bx;
cy = 3.0 * (cp[1].y - cp[0].y); cy = 3.0 * (cp[1].y - cp[0].y);
by = 3.0 * (cp[2].y - cp[1].y) - cy; by = 3.0 * (cp[2].y - cp[1].y) - cy;
ay = cp[3].y - cp[0].y - cy - by; ay = cp[3].y - cp[0].y - cy - by;
/* calculate the curve point at parameter value t */ /* calculate the curve point at parameter value t */
tSquared = t * t; tSquared = t * t;
tCubed = tSquared * t; tCubed = tSquared * t;
result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x; result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y; result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
return result; return result;
} }
@ -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);
} }