diff --git a/magic/src/calligraphy.c b/magic/src/calligraphy.c index c73d1f21e..ae2a455d3 100644 --- a/magic/src/calligraphy.c +++ b/magic/src/calligraphy.c @@ -41,32 +41,29 @@ typedef struct float x, y; } Point2D; -static Mix_Chunk * calligraphy_snd; +static Mix_Chunk *calligraphy_snd; static Point2D calligraphy_control_points[4]; static int calligraphy_r, calligraphy_g, calligraphy_b; static int calligraphy_old_thick; static Uint32 calligraphy_last_time; -static SDL_Surface * calligraphy_brush, * calligraphy_colored_brush; +static SDL_Surface *calligraphy_brush, *calligraphy_colored_brush; /* Local Function Prototypes */ -static Point2D calligraphy_PointOnCubicBezier(Point2D* cp, float t); -static void calligraphy_ComputeBezier(Point2D* cp, int numberOfPoints, Point2D* curve); +static Point2D calligraphy_PointOnCubicBezier(Point2D * cp, float t); +static void calligraphy_ComputeBezier(Point2D * cp, int numberOfPoints, Point2D * curve); static float calligraphy_dist(float x1, float y1, float x2, float y2); int calligraphy_init(magic_api * api); Uint32 calligraphy_api_version(void); int calligraphy_get_tool_count(magic_api * api); -SDL_Surface * calligraphy_get_icon(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); +SDL_Surface *calligraphy_get_icon(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); void calligraphy_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 calligraphy_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 calligraphy_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 calligraphy_shutdown(magic_api * api); void calligraphy_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int calligraphy_requires_colors(magic_api * api, int which); @@ -79,19 +76,17 @@ int calligraphy_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/calligraphy.ogg", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/calligraphy.ogg", api->data_directory); calligraphy_snd = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%s/images/magic/calligraphy_brush.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/calligraphy_brush.png", api->data_directory); calligraphy_brush = IMG_Load(fname); calligraphy_colored_brush = NULL; if (calligraphy_brush == NULL) - return(0); + return (0); calligraphy_last_time = 0; @@ -100,53 +95,54 @@ int calligraphy_init(magic_api * api) calligraphy_g = -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: int calligraphy_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(1); + return (1); } // 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]; - snprintf(fname, sizeof(fname), "%s/images/magic/calligraphy.png", - api->data_directory); - return(IMG_Load(fname)); + snprintf(fname, sizeof(fname), "%s/images/magic/calligraphy.png", api->data_directory); + return (IMG_Load(fname)); } // 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: -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( - gettext_noop("Click and drag the mouse around to draw in calligraphy."))); + return (strdup(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, - SDL_Surface * last ATTRIBUTE_UNUSED, int ox, int oy, int x, int y, - SDL_Rect * update_rect) + SDL_Surface * last ATTRIBUTE_UNUSED, int ox, int oy, int x, int y, SDL_Rect * update_rect) { - Point2D * curve; + Point2D *curve; int i, n_points, thick, new_thick; Uint32 colr; SDL_Rect src, dest; // if (SDL_GetTicks() < calligraphy_last_time + 5) // return; - + calligraphy_control_points[0].x = calligraphy_control_points[1].x; calligraphy_control_points[0].y = calligraphy_control_points[1].y; 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, - calligraphy_control_points[0].y, - calligraphy_control_points[1].x, - calligraphy_control_points[1].y) + - calligraphy_dist(calligraphy_control_points[1].x, - calligraphy_control_points[1].y, - calligraphy_control_points[2].x, - calligraphy_control_points[2].y) + - calligraphy_dist(calligraphy_control_points[2].x, - calligraphy_control_points[2].y, - calligraphy_control_points[3].x, - calligraphy_control_points[3].y); + calligraphy_control_points[0].y, + calligraphy_control_points[1].x, + calligraphy_control_points[1].y) + + calligraphy_dist(calligraphy_control_points[1].x, + calligraphy_control_points[1].y, + calligraphy_control_points[2].x, + calligraphy_control_points[2].y) + + calligraphy_dist(calligraphy_control_points[2].x, + calligraphy_control_points[2].y, calligraphy_control_points[3].x, calligraphy_control_points[3].y); 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); calligraphy_ComputeBezier(calligraphy_control_points, n_points, curve); - colr = SDL_MapRGB(canvas->format, - calligraphy_r, - calligraphy_g, - calligraphy_b); + colr = SDL_MapRGB(canvas->format, 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++) - { - 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; - y = curve[i].y - (j / 2); // 30 degrees + thick = ((new_thick * i) + (calligraphy_old_thick * (n_points - i))) / n_points; - 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); + + /* 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; + 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; @@ -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 (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - 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, - SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, - int x, int y, SDL_Rect * update_rect ATTRIBUTE_UNUSED) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x, int y, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { calligraphy_old_thick = 8; 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, - SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect 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) { } @@ -313,9 +315,7 @@ void calligraphy_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b) Uint8 a; Uint32 amask; - if (calligraphy_r == r && - calligraphy_g == g && - calligraphy_b == b) + if (calligraphy_r == r && calligraphy_g == g && calligraphy_b == b) return; 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) SDL_FreeSurface(calligraphy_colored_brush); - amask = ~(calligraphy_brush->format->Rmask | - calligraphy_brush->format->Gmask | - calligraphy_brush->format->Bmask); + amask = ~(calligraphy_brush->format->Rmask | calligraphy_brush->format->Gmask | calligraphy_brush->format->Bmask); calligraphy_colored_brush = 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->format->BitsPerPixel, calligraphy_brush->format->Rmask, - calligraphy_brush->format->Gmask, - calligraphy_brush->format->Bmask, amask); + calligraphy_brush->format->Gmask, calligraphy_brush->format->Bmask, amask); if (calligraphy_colored_brush == NULL) - return; // FIXME: Error! + return; // FIXME: Error! SDL_LockSurface(calligraphy_brush); SDL_LockSurface(calligraphy_colored_brush); for (y = 0; y < calligraphy_brush->h; y++) - { - for (x = 0; x < calligraphy_brush->w; x++) { - SDL_GetRGBA(api->getpixel(calligraphy_brush, x, y), - calligraphy_brush->format, &r, &g, &b, &a); + for (x = 0; x < calligraphy_brush->w; x++) + { + SDL_GetRGBA(api->getpixel(calligraphy_brush, x, y), calligraphy_brush->format, &r, &g, &b, &a); - api->putpixel(calligraphy_colored_brush, x, y, - SDL_MapRGBA(calligraphy_colored_brush->format, - calligraphy_r, - calligraphy_g, - calligraphy_b, a)); + api->putpixel(calligraphy_colored_brush, x, y, + SDL_MapRGBA(calligraphy_colored_brush->format, calligraphy_r, calligraphy_g, calligraphy_b, a)); + } } - } SDL_UnlockSurface(calligraphy_colored_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 */ -static Point2D calligraphy_PointOnCubicBezier( Point2D* cp, float t ) +static Point2D calligraphy_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; + 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; + 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 */ + /* calculate the curve point at parameter value t */ - tSquared = t * t; - tCubed = tSquared * 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; + result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x; + 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 ) */ -static void calligraphy_ComputeBezier(Point2D* cp, int numberOfPoints, Point2D* curve) +static void calligraphy_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] = calligraphy_PointOnCubicBezier( cp, i*dt ); + for (i = 0; i < numberOfPoints; i++) + curve[i] = calligraphy_PointOnCubicBezier(cp, i * dt); } static float calligraphy_dist(float x1, float y1, float x2, float y2) { float d; + d = (sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))); 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) { - return(MODE_PAINT); + return (MODE_PAINT); }