From cc05925d9e03155193cc58c1719ccdb6367da2b4 Mon Sep 17 00:00:00 2001 From: Bill Kendrick Date: Thu, 15 Sep 2022 00:11:16 -0700 Subject: [PATCH] Re-ran `indent` on all .c & .h source code files Like so -- find . -name "*.c" -or -name "*.h" -exec indent -nbfda -npcs -npsl -bli0 --no-tabs {} \; The `indent` invocation differs from the last one noted in CHANGES.txt (from 2006!?), in that I've added "--no-tabs", to ensure indents are all space-based. --- docs/CHANGES.txt | 5 +- dummy.c | 2 +- magic/docs/tp_magic_example.c | 127 +- magic/src/alien.c | 165 +- magic/src/blind.c | 297 +- magic/src/blocks_chalk_drip.c | 383 +- magic/src/blur.c | 185 +- magic/src/bricks.c | 217 +- magic/src/calligraphy.c | 169 +- magic/src/cartoon.c | 224 +- magic/src/checkerboard.c | 121 +- magic/src/clone.c | 180 +- magic/src/confetti.c | 121 +- magic/src/distortion.c | 113 +- magic/src/emboss.c | 122 +- magic/src/fade_darken.c | 194 +- magic/src/fisheye.c | 199 +- magic/src/flower.c | 423 +- magic/src/foam.c | 429 +- magic/src/fold.c | 601 +- magic/src/fretwork.c | 491 +- magic/src/glasstile.c | 204 +- magic/src/grass.c | 188 +- magic/src/halftone.c | 301 +- magic/src/kalidescope.c | 202 +- magic/src/light.c | 173 +- magic/src/lightning.c | 211 +- magic/src/metalpaint.c | 114 +- magic/src/mirror_flip.c | 115 +- magic/src/mosaic.c | 261 +- magic/src/mosaic_shaped.c | 833 +- magic/src/negative.c | 200 +- magic/src/noise.c | 164 +- magic/src/perspective.c | 815 +- magic/src/pixels.c | 89 +- magic/src/puzzle.c | 164 +- magic/src/rails.c | 407 +- magic/src/rain.c | 188 +- magic/src/rainbow.c | 130 +- magic/src/realrainbow.c | 272 +- magic/src/reflection.c | 380 +- magic/src/ripples.c | 109 +- magic/src/rosette.c | 124 +- magic/src/sharpen.c | 217 +- magic/src/shift.c | 234 +- magic/src/smudge.c | 167 +- magic/src/snow.c | 172 +- magic/src/stretch.c | 257 +- magic/src/string.c | 418 +- magic/src/tint.c | 219 +- magic/src/toothpaste.c | 145 +- magic/src/tornado.c | 284 +- magic/src/tv.c | 162 +- magic/src/waves.c | 124 +- magic/src/xor.c | 127 +- src/android_assets.c | 75 +- src/android_assets.h | 52 +- src/android_mbstowcs.c | 109 +- src/android_mbstowcs.h | 2 +- src/android_print.c | 104 +- src/android_print.h | 20 +- src/compiler.h | 5 +- src/cursor.c | 8 +- src/dirwalk.c | 598 +- src/dirwalk.h | 21 +- src/fill.c | 687 +- src/fill.h | 31 +- src/fill_tools.h | 10 +- src/fonts.c | 1665 +- src/fonts.h | 15 +- src/get_fname.c | 29 +- src/gifenc.c | 506 +- src/gifenc.h | 29 +- src/i18n.c | 477 +- src/i18n.h | 3 +- src/im.c | 1987 +- src/ios_print.h | 4 +- src/onscreen_keyboard.c | 2075 +- src/onscreen_keyboard.h | 24 +- src/parse.h | 4 +- src/pixels.c | 142 +- src/pixels.h | 2 +- src/platform.h | 22 +- src/playsound.c | 114 +- src/playsound.h | 3 +- src/postscript_print.c | 237 +- src/postscript_print.h | 3 +- src/progressbar.c | 53 +- src/progressbar.h | 3 +- src/rgblinear.c | 12 +- src/rgblinear.h | 3 +- src/shapes.h | 25 +- src/test-png.c | 67 +- src/tuxpaint.c | 36155 +++++++++++++++++--------------- src/win32_dirent.c | 54 +- src/win32_dirent.h | 7 +- src/win32_print.c | 361 +- src/win32_print.h | 3 +- src/win32_trash.c | 17 +- 99 files changed, 31659 insertions(+), 27102 deletions(-) diff --git a/docs/CHANGES.txt b/docs/CHANGES.txt index 82d18a0a5..349ca245c 100644 --- a/docs/CHANGES.txt +++ b/docs/CHANGES.txt @@ -7,7 +7,7 @@ Various contributors (see below, and AUTHORS.txt) http://www.tuxpaint.org/ -2022.September.14 (0.9.29) +2022.September.15 (0.9.29) * Improvements to "Stamp" tool: ----------------------------- * Stamps may now be rotated. @@ -131,6 +131,9 @@ http://www.tuxpaint.org/ Tux Paint Config. source updated to make the script's job easier. Bill Kendrick + * Cleaned up .c and .h source files using `indent`: + find . -name "*.c" -or -name "*.h" -exec indent -nbfda -npcs -npsl -bli0 --no-tabs {} \; + * Documentation updates: --------------------- * Update macOS build instructions for SDL2.0. diff --git a/dummy.c b/dummy.c index 311857d57..70d0c852a 100644 --- a/dummy.c +++ b/dummy.c @@ -11,7 +11,7 @@ static TYPE x UNUSED; #endif #ifdef SYMBOL -static int exists UNUSED = ! !SYMBOL; +static int exists UNUSED = !!SYMBOL; #endif int main(int argc UNUSED, char *argv[]UNUSED) diff --git a/magic/docs/tp_magic_example.c b/magic/docs/tp_magic_example.c index 8d0e615c6..3642c848a 100644 --- a/magic/docs/tp_magic_example.c +++ b/magic/docs/tp_magic_example.c @@ -93,9 +93,11 @@ Uint8 example_r, example_g, example_b; // that are declared _before_ them. void example_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect); + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect); -void example_line_callback(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y); +void example_line_callback(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y); /* Setup Functions: */ @@ -136,22 +138,23 @@ int example_init(magic_api * api) char fname[1024]; for (i = 0; i < NUM_TOOLS; i++) - { - // Assemble the filename from the "snd_filenames[]" array into - // a full path to a real file. - // - // Use "api->data_directory" to figure out where our sounds should be. - // (The "tp-magic-config --dataprefix" command would have told us when - // we installed our plugin and its data.) + { + // Assemble the filename from the "snd_filenames[]" array into + // a full path to a real file. + // + // Use "api->data_directory" to figure out where our sounds should be. + // (The "tp-magic-config --dataprefix" command would have told us when + // we installed our plugin and its data.) - snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, snd_filenames[i]); + snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, + snd_filenames[i]); - printf("Trying to load %s sound file\n", fname); + printf("Trying to load %s sound file\n", fname); - // Try to load the file! + // Try to load the file! - snd_effect[i] = Mix_LoadWAV(fname); - } + snd_effect[i] = Mix_LoadWAV(fname); + } return (1); } @@ -191,7 +194,8 @@ SDL_Surface *example_get_icon(magic_api * api, int which) // We use 'which' (which of our tools Tux Paint is asking about) // as an index into the array. - snprintf(fname, sizeof(fname), "%s/images/magic/%s.png", api->data_directory, icon_filenames[which]); + snprintf(fname, sizeof(fname), "%s/images/magic/%s.png", + api->data_directory, icon_filenames[which]); // Try to load the image, and return the results to Tux Paint: @@ -330,7 +334,8 @@ void example_shutdown(magic_api * api) void example_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect) + SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect) { // In our case, a single click (which is also the start of a drag!) // is identical to what dragging does, but just at one point, rather @@ -347,7 +352,8 @@ example_click(magic_api * api, int which, int mode, void example_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect) + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect) { // Call Tux Paint's "line()" function. // @@ -358,7 +364,8 @@ example_drag(magic_api * api, int which, SDL_Surface * canvas, // useful things (which of our "Magic" tools is being used and // the current and snapshot canvases). - api->line((void *)api, which, canvas, snapshot, ox, oy, x, y, 1, example_line_callback); + api->line((void *) api, which, canvas, snapshot, ox, oy, x, y, 1, + example_line_callback); // If we need to, swap the X and/or Y values, so that @@ -366,19 +373,19 @@ example_drag(magic_api * api, int which, SDL_Surface * canvas, // so the values we put inside "update_rect" make sense: if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } // Fill in the elements of the "update_rect" SDL_Rect structure @@ -409,7 +416,8 @@ example_drag(magic_api * api, int which, SDL_Surface * canvas, void example_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect) + SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect) { // Neither of our effects do anything special when the mouse is released // from a click or click-and-drag, so there's no code here... @@ -453,7 +461,8 @@ void example_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b) // It pays attention to 'which' to determine which of our plugin's tools // is currently selected. -void example_line_callback(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y) +void example_line_callback(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y) { // For technical reasons, we can't accept a pointer to the "magic_api" // struct, like the other functions do. @@ -473,37 +482,41 @@ void example_line_callback(void *ptr, int which, SDL_Surface * canvas, SDL_Surfa // Tux Paint sends to us with the values we enumerated above. if (which == TOOL_ONE) - { - // Tool number 1 simply draws a single pixel at the (x,y) location. - // It's a 1x1 pixel brush + { + // Tool number 1 simply draws a single pixel at the (x,y) location. + // It's a 1x1 pixel brush - api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, example_r, example_g, example_b)); + api->putpixel(canvas, x, y, + SDL_MapRGB(canvas->format, example_r, example_g, + example_b)); - // We use "SDL_MapRGB()" to convert the RGB value we receive from Tux Paint - // for the user's current color selection to a 'Uint32' pixel value - // we can send to Tux Paint's "putpixel()" function. - } + // We use "SDL_MapRGB()" to convert the RGB value we receive from Tux Paint + // for the user's current color selection to a 'Uint32' pixel value + // we can send to Tux Paint's "putpixel()" function. + } else if (which == TOOL_TWO) + { + // Tool number 2 copies an 8x8 square of pixels from the opposite side + // of the canvas and puts it under the cursor + + for (yy = -4; yy < 4; yy++) { - // Tool number 2 copies an 8x8 square of pixels from the opposite side - // of the canvas and puts it under the cursor + for (xx = -4; xx < 4; xx++) + { + api->putpixel(canvas, x + xx, y + yy, + api->getpixel(snapshot, canvas->w - x - xx, + canvas->h - y - yy)); - for (yy = -4; yy < 4; yy++) - { - for (xx = -4; xx < 4; xx++) - { - api->putpixel(canvas, x + xx, y + yy, api->getpixel(snapshot, canvas->w - x - xx, canvas->h - y - yy)); + // We simply use Tux Paint's "getpixel()" routine to pull pixel + // values from the 'snapshot', and then "putpixel()" to draw them + // right into the 'canvas'. - // We simply use Tux Paint's "getpixel()" routine to pull pixel - // values from the 'snapshot', and then "putpixel()" to draw them - // right into the 'canvas'. - - // Note: putpixel() and getpixel() are safe to use, even if your - // X,Y values are outside of the SDL surface (e.g., negative, or - // greater than the surface's width or height). - } - } + // Note: putpixel() and getpixel() are safe to use, even if your + // X,Y values are outside of the SDL surface (e.g., negative, or + // greater than the surface's width or height). + } } + } } // Switch-In event @@ -522,7 +535,8 @@ void example_line_callback(void *ptr, int which, SDL_Surface * canvas, SDL_Surfa // Our example doesn't do anything when we switch to, or away from, our // Magic tools, so we just do nothing here. -void example_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas) +void example_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas) { } @@ -542,6 +556,7 @@ void example_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas // Our example doesn't do anything when we switch to, or away from, our // Magic tools, so we just do nothing here. -void example_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas) +void example_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas) { } diff --git a/magic/src/alien.c b/magic/src/alien.c index edc6ca5b2..16fe868b3 100644 --- a/magic/src/alien.c +++ b/magic/src/alien.c @@ -73,7 +73,8 @@ const int alien_groups[alien_NUM_TOOLS] = { }; const char *alien_descs[alien_NUM_TOOLS][2] = { - {gettext_noop("Click and drag the mouse to change the colors in parts of your picture."), + {gettext_noop + ("Click and drag the mouse to change the colors in parts of your picture."), gettext_noop("Click to change the colors in your entire picture."),}, }; @@ -86,17 +87,21 @@ char *alien_get_name(magic_api * api, int which); int alien_get_group(magic_api * api, int which); char *alien_get_description(magic_api * api, int which, int mode); void alien_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); Mix_Chunk *magic_loadsound(char *file); void alien_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void alien_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 alien_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); void alien_shutdown(magic_api * api); void alien_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int alien_requires_colors(magic_api * api, int which); -void alien_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void alien_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void alien_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void alien_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int alien_modes(magic_api * api, int which); @@ -114,10 +119,11 @@ int alien_init(magic_api * api) srand(time(0)); for (i = 0; i < alien_NUM_TOOLS; i++) - { - snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, alien_snd_filenames[i]); - alien_snd_effect[i] = Mix_LoadWAV(fname); - } + { + snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, + alien_snd_filenames[i]); + alien_snd_effect[i] = Mix_LoadWAV(fname); + } return (1); } @@ -131,7 +137,8 @@ SDL_Surface *alien_get_icon(magic_api * api, int which) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, alien_icon_filenames[which]); + snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, + alien_icon_filenames[which]); return (IMG_Load(fname)); } @@ -147,14 +154,16 @@ int alien_get_group(magic_api * api ATTRIBUTE_UNUSED, int which) } // Return our descriptions, localized: -char *alien_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode) +char *alien_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, + int mode) { return (strdup(gettext_noop(alien_descs[which][mode - 1]))); } //Do the effect for one pixel static void do_alien_pixel(void *ptr, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) + SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { magic_api *api = (magic_api *) ptr; @@ -162,74 +171,82 @@ static void do_alien_pixel(void *ptr, int which ATTRIBUTE_UNUSED, double temp2[3]; int k; - SDL_GetRGB(api->getpixel(canvas, x, y), canvas->format, &temp[0], &temp[1], &temp[2]); + SDL_GetRGB(api->getpixel(canvas, x, y), canvas->format, &temp[0], &temp[1], + &temp[2]); for (k = 0; k < 3; k++) - { + { //EP temp2[k] = clamp(0,127.5 * (1.0 + sin (((temp[k] / 127.5 - 1.0) * alien_FREQUENCY[k] + alien_ANGLE[k] / 180.0) * M_PI)),255); - temp2[k] = clamp(0.0, - 127.5 * (1.0 + - sin(((temp[k] / 127.5 - 1.0) * alien_FREQUENCY[k] + alien_ANGLE[k] / 180.0) * M_PI)), - 255.0); - } - api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, temp2[0], temp2[1], temp2[2])); + temp2[k] = clamp(0.0, + 127.5 * (1.0 + + sin(((temp[k] / 127.5 - + 1.0) * alien_FREQUENCY[k] + + alien_ANGLE[k] / 180.0) * M_PI)), 255.0); + } + api->putpixel(canvas, x, y, + SDL_MapRGB(canvas->format, temp2[0], temp2[1], temp2[2])); } // Do the effect for the full image -static void do_alien_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int which) +static void do_alien_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, + int which) { int x, y; for (y = 0; y < last->h; y++) + { + for (x = 0; x < last->w; x++) { - for (x = 0; x < last->w; x++) - { - do_alien_pixel(ptr, which, canvas, last, x, y); - } + do_alien_pixel(ptr, which, canvas, last, x, y); } + } } //do the effect for the brush -static void do_alien_brush(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void do_alien_brush(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y) { int xx, yy; magic_api *api = (magic_api *) ptr; for (yy = y - alien_RADIUS; yy < y + alien_RADIUS; yy++) + { + for (xx = x - alien_RADIUS; xx < x + alien_RADIUS; xx++) { - for (xx = x - alien_RADIUS; xx < x + alien_RADIUS; xx++) - { - if (api->in_circle(xx - x, yy - y, alien_RADIUS) && !api->touched(xx, yy)) - { - do_alien_pixel(api, which, canvas, last, xx, yy); - } - } + if (api->in_circle(xx - x, yy - y, alien_RADIUS) + && !api->touched(xx, yy)) + { + do_alien_pixel(api, which, canvas, last, xx, yy); + } } + } } // Affect the canvas on drag: void alien_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) { - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_alien_brush); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, + do_alien_brush); api->playsound(alien_snd_effect[which], (x * 255) / canvas->w, 255); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = ox - alien_RADIUS; update_rect->y = oy - alien_RADIUS; @@ -244,34 +261,38 @@ Mix_Chunk *magic_loadsound(char *file) Mix_Chunk *temp; if (!use_sound) - { - return (Mix_Chunk *) - 1; - } + { + return (Mix_Chunk *) - 1; + } temp = Mix_LoadWAV(file); return temp; } // Affect the canvas on click: void alien_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) { if (mode == MODE_PAINT) alien_drag(api, which, canvas, last, x, y, x, y, update_rect); else - { - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; - do_alien_full(api, canvas, last, which); - api->playsound(alien_snd_effect[which], 128, 255); - } + { + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; + do_alien_full(api, canvas, last, which); + api->playsound(alien_snd_effect[which], 128, 255); + } } // Affect the canvas on release: -void alien_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) +void alien_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) { } @@ -282,32 +303,36 @@ void alien_shutdown(magic_api * api ATTRIBUTE_UNUSED) int i; for (i = 0; i < alien_NUM_TOOLS; i++) + { + if (alien_snd_effect[i] != NULL) { - if (alien_snd_effect[i] != NULL) - { - Mix_FreeChunk(alien_snd_effect[i]); - } + Mix_FreeChunk(alien_snd_effect[i]); } + } } // Record the color from Tux Paint: -void alien_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, +void alien_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) { } // Use colors: -int alien_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int alien_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void alien_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void alien_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void alien_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void alien_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } diff --git a/magic/src/blind.c b/magic/src/blind.c index a64678ac5..96dfa7c5b 100644 --- a/magic/src/blind.c +++ b/magic/src/blind.c @@ -62,15 +62,20 @@ int blind_get_group(magic_api * api, int which); char *blind_get_description(magic_api * api, int which, int mode); int blind_requires_colors(magic_api * api, int which); void blind_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); + SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect); void blind_shutdown(magic_api * api); -void blind_paint_blind(void *ptr_to_api, int which_tool, SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y); +void blind_paint_blind(void *ptr_to_api, int which_tool, SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y); void blind_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void blind_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void blind_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void blind_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void blind_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); +void blind_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void blind_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int blind_modes(magic_api * api, int which); // Housekeeping functions @@ -91,7 +96,8 @@ int blind_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/blind.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/blind.ogg", + api->data_directory); blind_snd = Mix_LoadWAV(fname); return (1); @@ -106,36 +112,45 @@ SDL_Surface *blind_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/blind.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/blind.png", + api->data_directory); return (IMG_Load(fname)); } -char *blind_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *blind_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return strdup(gettext_noop("Blind")); } -int blind_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int blind_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PICTURE_DECORATIONS; } -char *blind_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *blind_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED) { return strdup(gettext_noop ("Click towards the edge of your picture to pull window blinds over it. Move perpendicularly to open or close the blinds.")); } -int blind_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int blind_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 1; } -void blind_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * snapshot ATTRIBUTE_UNUSED, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) +void blind_release(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED, + SDL_Surface * snapshot ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, + SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -147,12 +162,14 @@ void blind_shutdown(magic_api * api ATTRIBUTE_UNUSED) // Interactivity functions void blind_paint_blind(void *ptr_to_api, int which_tool ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * snapshot ATTRIBUTE_UNUSED, int x, int y) + SDL_Surface * canvas, + SDL_Surface * snapshot ATTRIBUTE_UNUSED, int x, int y) { magic_api *api = (magic_api *) ptr_to_api; api->putpixel(canvas, x, y, - SDL_MapRGB(canvas->format, (blind_r + blind_light) / 2, (blind_g + blind_light) / 2, + SDL_MapRGB(canvas->format, (blind_r + blind_light) / 2, + (blind_g + blind_light) / 2, (blind_b + blind_light) / 2)); } @@ -167,145 +184,157 @@ void blind_paint_blind(void *ptr_to_api, int which_tool ATTRIBUTE_UNUSED, */ void blind_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect) + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect) { int opaque; SDL_BlitSurface(snapshot, NULL, canvas, NULL); switch (blind_side) + { + int i, j; + + case BLIND_SIDE_TOP: + opaque = max((x * BLIND_THICKNESS) / canvas->w + 2, 2); + for (i = y; i >= 0; i -= BLIND_THICKNESS) { - int i, j; - - case BLIND_SIDE_TOP: - opaque = max((x * BLIND_THICKNESS) / canvas->w + 2, 2); - for (i = y; i >= 0; i -= BLIND_THICKNESS) - { - blind_light = 255; - for (j = i; j > i - opaque / 2; j--) - { - api->line(api, which, canvas, snapshot, 0, j, canvas->w, j, 1, blind_paint_blind); - blind_light -= 20; - } - for (j = i - opaque / 2; j > i - opaque; j--) - { - api->line(api, which, canvas, snapshot, 0, j, canvas->w, j, 1, blind_paint_blind); - blind_light += 20; - } - } - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = max(oy, y); - api->playsound(blind_snd, 128, 255); - break; - - case BLIND_SIDE_BOTTOM: - opaque = max((x * BLIND_THICKNESS) / canvas->w + 2, 2); - for (i = y; i <= canvas->h; i += BLIND_THICKNESS) - { - blind_light = 255; - for (j = i; j < i + opaque / 2; j++) - { - api->line(api, which, canvas, snapshot, 0, j, canvas->w, j, 1, blind_paint_blind); - blind_light -= 20; - } - for (j = i + opaque / 2; j < i + opaque; j++) - { - api->line(api, which, canvas, snapshot, 0, j, canvas->w, j, 1, blind_paint_blind); - blind_light += 20; - } - } - - update_rect->x = 0; - update_rect->y = min(oy, y); - update_rect->w = canvas->w; - update_rect->h = canvas->h - update_rect->y; - api->playsound(blind_snd, 128, 255); - break; - - case BLIND_SIDE_RIGHT: - opaque = max((y * BLIND_THICKNESS) / canvas->h + 2, 2); - for (i = x; i <= canvas->w; i += BLIND_THICKNESS) - { - blind_light = 255; - for (j = i; j < i + opaque / 2; j++) - { - api->line(api, which, canvas, snapshot, j, 0, j, canvas->h, 1, blind_paint_blind); - blind_light -= 20; - } - for (j = i + opaque / 2; j < i + opaque; j++) - { - api->line(api, which, canvas, snapshot, j, 0, j, canvas->h, 1, blind_paint_blind); - blind_light += 20; - } - } - - update_rect->x = min(ox, x); - update_rect->y = 0; - update_rect->w = canvas->w - update_rect->x; - update_rect->h = canvas->h; - api->playsound(blind_snd, (x * 255) / canvas->w, 255); - break; - - case BLIND_SIDE_LEFT: - opaque = max((y * BLIND_THICKNESS) / canvas->h + 2, 2); - for (i = x; i >= 0; i -= BLIND_THICKNESS) - { - blind_light = 255; - for (j = i; j > i - opaque / 2; j--) - { - api->line(api, which, canvas, snapshot, j, 0, j, canvas->h, 1, blind_paint_blind); - blind_light -= 20; - } - for (j = i - opaque / 2; j > i - opaque; j--) - { - api->line(api, which, canvas, snapshot, j, 0, j, canvas->h, 1, blind_paint_blind); - blind_light += 20; - } - } - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = max(ox, x); - update_rect->h = canvas->h; - api->playsound(blind_snd, (x * 255) / canvas->w, 255); - break; - + blind_light = 255; + for (j = i; j > i - opaque / 2; j--) + { + api->line(api, which, canvas, snapshot, 0, j, canvas->w, j, 1, + blind_paint_blind); + blind_light -= 20; + } + for (j = i - opaque / 2; j > i - opaque; j--) + { + api->line(api, which, canvas, snapshot, 0, j, canvas->w, j, 1, + blind_paint_blind); + blind_light += 20; + } } + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = max(oy, y); + api->playsound(blind_snd, 128, 255); + break; + + case BLIND_SIDE_BOTTOM: + opaque = max((x * BLIND_THICKNESS) / canvas->w + 2, 2); + for (i = y; i <= canvas->h; i += BLIND_THICKNESS) + { + blind_light = 255; + for (j = i; j < i + opaque / 2; j++) + { + api->line(api, which, canvas, snapshot, 0, j, canvas->w, j, 1, + blind_paint_blind); + blind_light -= 20; + } + for (j = i + opaque / 2; j < i + opaque; j++) + { + api->line(api, which, canvas, snapshot, 0, j, canvas->w, j, 1, + blind_paint_blind); + blind_light += 20; + } + } + + update_rect->x = 0; + update_rect->y = min(oy, y); + update_rect->w = canvas->w; + update_rect->h = canvas->h - update_rect->y; + api->playsound(blind_snd, 128, 255); + break; + + case BLIND_SIDE_RIGHT: + opaque = max((y * BLIND_THICKNESS) / canvas->h + 2, 2); + for (i = x; i <= canvas->w; i += BLIND_THICKNESS) + { + blind_light = 255; + for (j = i; j < i + opaque / 2; j++) + { + api->line(api, which, canvas, snapshot, j, 0, j, canvas->h, 1, + blind_paint_blind); + blind_light -= 20; + } + for (j = i + opaque / 2; j < i + opaque; j++) + { + api->line(api, which, canvas, snapshot, j, 0, j, canvas->h, 1, + blind_paint_blind); + blind_light += 20; + } + } + + update_rect->x = min(ox, x); + update_rect->y = 0; + update_rect->w = canvas->w - update_rect->x; + update_rect->h = canvas->h; + api->playsound(blind_snd, (x * 255) / canvas->w, 255); + break; + + case BLIND_SIDE_LEFT: + opaque = max((y * BLIND_THICKNESS) / canvas->h + 2, 2); + for (i = x; i >= 0; i -= BLIND_THICKNESS) + { + blind_light = 255; + for (j = i; j > i - opaque / 2; j--) + { + api->line(api, which, canvas, snapshot, j, 0, j, canvas->h, 1, + blind_paint_blind); + blind_light -= 20; + } + for (j = i - opaque / 2; j > i - opaque; j--) + { + api->line(api, which, canvas, snapshot, j, 0, j, canvas->h, 1, + blind_paint_blind); + blind_light += 20; + } + } + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = max(ox, x); + update_rect->h = canvas->h; + api->playsound(blind_snd, (x * 255) / canvas->w, 255); + break; + + } } void blind_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) { if (y < canvas->h / 2) - { - if (x < y) - blind_side = BLIND_SIDE_LEFT; - else if (canvas->w - x < y) - blind_side = BLIND_SIDE_RIGHT; - else - blind_side = BLIND_SIDE_TOP; - } + { + if (x < y) + blind_side = BLIND_SIDE_LEFT; + else if (canvas->w - x < y) + blind_side = BLIND_SIDE_RIGHT; + else + blind_side = BLIND_SIDE_TOP; + } else - { - if (x < canvas->h - y) - blind_side = BLIND_SIDE_LEFT; - else if (canvas->w - x < canvas->h - y) - blind_side = BLIND_SIDE_RIGHT; - else - blind_side = BLIND_SIDE_BOTTOM; - } + { + if (x < canvas->h - y) + blind_side = BLIND_SIDE_LEFT; + else if (canvas->w - x < canvas->h - y) + blind_side = BLIND_SIDE_RIGHT; + else + blind_side = BLIND_SIDE_BOTTOM; + } blind_drag(api, which, canvas, last, x, y, x, y, update_rect); } -void blind_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void blind_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void blind_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void blind_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { diff --git a/magic/src/blocks_chalk_drip.c b/magic/src/blocks_chalk_drip.c index 2fed94b8c..cc340d0d0 100644 --- a/magic/src/blocks_chalk_drip.c +++ b/magic/src/blocks_chalk_drip.c @@ -60,18 +60,25 @@ SDL_Surface *blocks_chalk_drip_get_icon(magic_api * api, int which); char *blocks_chalk_drip_get_name(magic_api * api, int which); int blocks_chalk_drip_get_group(magic_api * api, int which); char *blocks_chalk_drip_get_description(magic_api * api, int which, int mode); -static void blocks_chalk_drip_linecb(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +static void blocks_chalk_drip_linecb(void *ptr, int which, + SDL_Surface * canvas, SDL_Surface * last, + int x, int y); void blocks_chalk_drip_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 blocks_chalk_drip_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 blocks_chalk_drip_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 blocks_chalk_drip_shutdown(magic_api * api); void blocks_chalk_drip_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int blocks_chalk_drip_requires_colors(magic_api * api, int which); -void blocks_chalk_drip_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void blocks_chalk_drip_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void blocks_chalk_drip_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void blocks_chalk_drip_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int blocks_chalk_drip_modes(magic_api * api, int which); @@ -80,13 +87,16 @@ int blocks_chalk_drip_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/blocks.wav", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/blocks.wav", + api->data_directory); snd_effect[0] = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%ssounds/magic/chalk.wav", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/chalk.wav", + api->data_directory); snd_effect[1] = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%ssounds/magic/drip.wav", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/drip.wav", + api->data_directory); snd_effect[2] = Mix_LoadWAV(fname); return (1); @@ -110,17 +120,20 @@ SDL_Surface *blocks_chalk_drip_get_icon(magic_api * api, int which) char fname[1024]; if (which == TOOL_BLOCKS) - { - snprintf(fname, sizeof(fname), "%simages/magic/blocks.png", api->data_directory); - } + { + snprintf(fname, sizeof(fname), "%simages/magic/blocks.png", + api->data_directory); + } else if (which == TOOL_CHALK) - { - snprintf(fname, sizeof(fname), "%simages/magic/chalk.png", api->data_directory); - } + { + snprintf(fname, sizeof(fname), "%simages/magic/chalk.png", + api->data_directory); + } else if (which == TOOL_DRIP) - { - snprintf(fname, sizeof(fname), "%simages/magic/drip.png", api->data_directory); - } + { + snprintf(fname, sizeof(fname), "%simages/magic/drip.png", + api->data_directory); + } return (IMG_Load(fname)); } @@ -139,54 +152,67 @@ char *blocks_chalk_drip_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) } // Return our group (all the same): -int blocks_chalk_drip_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int blocks_chalk_drip_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_DISTORTS; } // Return our descriptions, localized: -char *blocks_chalk_drip_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode) +char *blocks_chalk_drip_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which, int mode) { if (which == TOOL_BLOCKS) + { + if (mode == MODE_PAINT) { - if (mode == MODE_PAINT) - { - return (strdup(gettext_noop("Click and drag the mouse around to make the picture blocky."))); - } - else - { - return (strdup(gettext_noop("Click to make the entire picture blocky."))); - } + return (strdup + (gettext_noop + ("Click and drag the mouse around to make the picture blocky."))); } + else + { + return (strdup + (gettext_noop("Click to make the entire picture blocky."))); + } + } else if (which == TOOL_CHALK) + { + if (mode == MODE_PAINT) { - if (mode == MODE_PAINT) - { - return (strdup(gettext_noop("Click and drag the mouse around to turn the picture into a chalk drawing."))); - } - else - { - return (strdup(gettext_noop("Click to turn the entire picture into a chalk drawing."))); - } + return (strdup + (gettext_noop + ("Click and drag the mouse around to turn the picture into a chalk drawing."))); } + else + { + return (strdup + (gettext_noop + ("Click to turn the entire picture into a chalk drawing."))); + } + } else if (which == TOOL_DRIP) + { + if (mode == MODE_PAINT) { - if (mode == MODE_PAINT) - { - return (strdup(gettext_noop("Click and drag the mouse around to make the picture drip."))); - } - else - { - return (strdup(gettext_noop("Click to make the entire picture drip."))); - } + return (strdup + (gettext_noop + ("Click and drag the mouse around to make the picture drip."))); } + else + { + return (strdup(gettext_noop("Click to make the entire picture drip."))); + } + } return (NULL); } // Do the effect: -static void blocks_chalk_drip_linecb(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void blocks_chalk_drip_linecb(void *ptr, int which, + SDL_Surface * canvas, SDL_Surface * last, + int x, int y) { magic_api *api = (magic_api *) ptr; int xx, yy; @@ -196,125 +222,136 @@ static void blocks_chalk_drip_linecb(void *ptr, int which, SDL_Surface * canvas, Uint32 colr; if (which == TOOL_BLOCKS) + { + /* Put x/y on exact grid points: */ + + x = (x / EFFECT_REZ) * EFFECT_REZ; + y = (y / EFFECT_REZ) * EFFECT_REZ; + + if (!api->touched(x, y)) { - /* Put x/y on exact grid points: */ - - x = (x / EFFECT_REZ) * EFFECT_REZ; - y = (y / EFFECT_REZ) * EFFECT_REZ; - - if (!api->touched(x, y)) + for (yy = y - (EFFECT_REZ * 2); yy < y + (EFFECT_REZ * 2); + yy = yy + EFFECT_REZ) + { + for (xx = x - (EFFECT_REZ * 2); xx < x + (EFFECT_REZ * 2); + xx = xx + EFFECT_REZ) { - for (yy = y - (EFFECT_REZ * 2); yy < y + (EFFECT_REZ * 2); yy = yy + EFFECT_REZ) + Uint32 pix[(EFFECT_REZ * EFFECT_REZ)]; + Uint32 p_or = 0; + Uint32 p_and = ~0; + unsigned i = (EFFECT_REZ * EFFECT_REZ); + + while (i--) + { + Uint32 p_tmp; + + p_tmp = api->getpixel(last, xx + (i >> 2), yy + (i & 3)); + p_or |= p_tmp; + p_and &= p_tmp; + pix[i] = p_tmp; + } + if (p_or == p_and) // if all pixels the same already + { + SDL_GetRGB(p_or, last->format, &r, &g, &b); + } + else // nope, must average them + { + double r_sum = 0.0; + double g_sum = 0.0; + double b_sum = 0.0; + + i = (EFFECT_REZ * EFFECT_REZ); + while (i--) { - for (xx = x - (EFFECT_REZ * 2); xx < x + (EFFECT_REZ * 2); xx = xx + EFFECT_REZ) - { - Uint32 pix[(EFFECT_REZ * EFFECT_REZ)]; - Uint32 p_or = 0; - Uint32 p_and = ~0; - unsigned i = (EFFECT_REZ * EFFECT_REZ); - - while (i--) - { - Uint32 p_tmp; - - p_tmp = api->getpixel(last, xx + (i >> 2), yy + (i & 3)); - p_or |= p_tmp; - p_and &= p_tmp; - pix[i] = p_tmp; - } - if (p_or == p_and) // if all pixels the same already - { - SDL_GetRGB(p_or, last->format, &r, &g, &b); - } - else // nope, must average them - { - double r_sum = 0.0; - double g_sum = 0.0; - double b_sum = 0.0; - - i = (EFFECT_REZ * EFFECT_REZ); - while (i--) - { - SDL_GetRGB(pix[i], last->format, &r, &g, &b); - r_sum += api->sRGB_to_linear(r); - g_sum += api->sRGB_to_linear(g); - b_sum += api->sRGB_to_linear(b); - } - r = api->linear_to_sRGB(r_sum / (float) (EFFECT_REZ * EFFECT_REZ)); - g = api->linear_to_sRGB(g_sum / (float) (EFFECT_REZ * EFFECT_REZ)); - b = api->linear_to_sRGB(b_sum / (float) (EFFECT_REZ * EFFECT_REZ)); - } - - /* Draw block: */ - - dest.x = xx; - dest.y = yy; - dest.w = EFFECT_REZ; - dest.h = EFFECT_REZ; - - SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, r, g, b)); - } + SDL_GetRGB(pix[i], last->format, &r, &g, &b); + r_sum += api->sRGB_to_linear(r); + g_sum += api->sRGB_to_linear(g); + b_sum += api->sRGB_to_linear(b); } + r = + api->linear_to_sRGB(r_sum / (float) (EFFECT_REZ * EFFECT_REZ)); + g = + api->linear_to_sRGB(g_sum / (float) (EFFECT_REZ * EFFECT_REZ)); + b = + api->linear_to_sRGB(b_sum / (float) (EFFECT_REZ * EFFECT_REZ)); + } + + /* Draw block: */ + + dest.x = xx; + dest.y = yy; + dest.w = EFFECT_REZ; + dest.h = EFFECT_REZ; + + SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, r, g, b)); } + } } + } else if (which == TOOL_CHALK) + { + for (yy = y - (EFFECT_REZ * 2); yy <= y + (EFFECT_REZ * 2); + yy = yy + EFFECT_REZ) { - for (yy = y - (EFFECT_REZ * 2); yy <= y + (EFFECT_REZ * 2); yy = yy + EFFECT_REZ) - { - for (xx = x - (EFFECT_REZ * 2); xx <= x + (EFFECT_REZ * 2); xx = xx + EFFECT_REZ) - { - dest.x = xx + ((rand() % (EFFECT_REZ + 1)) - (EFFECT_REZ / 2)); - dest.y = yy + ((rand() % (EFFECT_REZ + 1)) - (EFFECT_REZ / 2)); - dest.w = (rand() % EFFECT_REZ) + (EFFECT_REZ / 2); - dest.h = (rand() % EFFECT_REZ) + (EFFECT_REZ / 2); + for (xx = x - (EFFECT_REZ * 2); xx <= x + (EFFECT_REZ * 2); + xx = xx + EFFECT_REZ) + { + dest.x = xx + ((rand() % (EFFECT_REZ + 1)) - (EFFECT_REZ / 2)); + dest.y = yy + ((rand() % (EFFECT_REZ + 1)) - (EFFECT_REZ / 2)); + dest.w = (rand() % EFFECT_REZ) + (EFFECT_REZ / 2); + dest.h = (rand() % EFFECT_REZ) + (EFFECT_REZ / 2); - colr = api->getpixel(last, clamp(0, xx, canvas->w - 1), clamp(0, yy, canvas->h - 1)); - SDL_FillRect(canvas, &dest, colr); - } - } + colr = + api->getpixel(last, clamp(0, xx, canvas->w - 1), + clamp(0, yy, canvas->h - 1)); + SDL_FillRect(canvas, &dest, colr); + } } + } else if (which == TOOL_DRIP) + { + for (xx = x - 8; xx <= x + 8; xx++) { - for (xx = x - 8; xx <= x + 8; xx++) - { - h = (rand() % 8) + 8; + h = (rand() % 8) + 8; - for (yy = y; yy <= y + h; yy++) - { - src.x = xx; - src.y = y; - src.w = 1; - src.h = 16; + for (yy = y; yy <= y + h; yy++) + { + src.x = xx; + src.y = y; + src.w = 1; + src.h = 16; - dest.x = xx; - dest.y = yy; + dest.x = xx; + dest.y = yy; - SDL_BlitSurface(last, &src, canvas, &dest); - } - } + SDL_BlitSurface(last, &src, canvas, &dest); + } } + } } // Affect the canvas on drag: void blocks_chalk_drip_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) { - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, blocks_chalk_drip_linecb); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, + blocks_chalk_drip_linecb); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = ox - 16; update_rect->y = oy - 16; @@ -326,28 +363,41 @@ void blocks_chalk_drip_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void blocks_chalk_drip_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) { - if (mode == MODE_PAINT) { + if (mode == MODE_PAINT) + { blocks_chalk_drip_drag(api, which, canvas, last, x, y, x, y, update_rect); - } else /* MODE_FULLSCREEN */ { - if (which != TOOL_DRIP) { - for (y = 0; y < canvas->h; y += EFFECT_REZ) { - if (y % 10 == 0) { + } + else /* MODE_FULLSCREEN */ + { + if (which != TOOL_DRIP) + { + for (y = 0; y < canvas->h; y += EFFECT_REZ) + { + if (y % 10 == 0) + { api->update_progress_bar(); } - for (x = 0; x < canvas->w; x += EFFECT_REZ) { + for (x = 0; x < canvas->w; x += EFFECT_REZ) + { blocks_chalk_drip_linecb(api, which, canvas, last, x, y); } } - } else { + } + else + { /* Drip (works from bottom-to-top) */ - int p = (canvas->h -1) % 10; - for (y = canvas->h -1; y >= 0; y -= EFFECT_REZ) { - if ((y + p) % 10 == 0) { + int p = (canvas->h - 1) % 10; + for (y = canvas->h - 1; y >= 0; y -= EFFECT_REZ) + { + if ((y + p) % 10 == 0) + { api->update_progress_bar(); } - for (x = 0; x < canvas->w; x += EFFECT_REZ) { + for (x = 0; x < canvas->w; x += EFFECT_REZ) + { blocks_chalk_drip_linecb(api, which, canvas, last, x, y); } } @@ -362,9 +412,12 @@ void blocks_chalk_drip_click(magic_api * api, int which, int mode, } // Affect the canvas on release: -void blocks_chalk_drip_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) +void blocks_chalk_drip_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) { } @@ -380,31 +433,41 @@ void blocks_chalk_drip_shutdown(magic_api * api ATTRIBUTE_UNUSED) // Record the color from Tux Paint: void blocks_chalk_drip_set_color(magic_api * api ATTRIBUTE_UNUSED, - Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) + Uint8 r ATTRIBUTE_UNUSED, + Uint8 g ATTRIBUTE_UNUSED, + Uint8 b ATTRIBUTE_UNUSED) { } // Use colors: -int blocks_chalk_drip_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int blocks_chalk_drip_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void blocks_chalk_drip_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void blocks_chalk_drip_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void blocks_chalk_drip_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void blocks_chalk_drip_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } int blocks_chalk_drip_modes(magic_api * api ATTRIBUTE_UNUSED, int which) { - if (which == TOOL_BLOCKS || TOOL_CHALK) { + if (which == TOOL_BLOCKS || TOOL_CHALK) + { return (MODE_PAINT | MODE_FULLSCREEN); - } else /* TOOL_DRIP */ { + } + else /* TOOL_DRIP */ + { return (MODE_PAINT); } } diff --git a/magic/src/blur.c b/magic/src/blur.c index 369588966..a353cf009 100644 --- a/magic/src/blur.c +++ b/magic/src/blur.c @@ -47,16 +47,19 @@ char *blur_get_name(magic_api * api, int which); int blur_get_group(magic_api * api, int which); char *blur_get_description(magic_api * api, int which, int mode); void blur_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void blur_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void blur_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void blur_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); +void blur_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); void blur_shutdown(magic_api * api); void blur_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int blur_requires_colors(magic_api * api, int which); -void blur_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void blur_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void blur_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void blur_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int blur_modes(magic_api * api, int which); enum @@ -103,10 +106,11 @@ int blur_init(magic_api * api) char fname[1024]; for (i = 0; i < blur_NUM_TOOLS; i++) - { - snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, blur_snd_filenames[i]); - blur_snd_effect[i] = Mix_LoadWAV(fname); - } + { + snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, + blur_snd_filenames[i]); + blur_snd_effect[i] = Mix_LoadWAV(fname); + } return (1); } @@ -120,7 +124,8 @@ SDL_Surface *blur_get_icon(magic_api * api, int which) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, blur_icon_filenames[which]); + snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, + blur_icon_filenames[which]); return (IMG_Load(fname)); } @@ -137,13 +142,16 @@ int blur_get_group(magic_api * api ATTRIBUTE_UNUSED, int which) } // Return our descriptions, localized: -char *blur_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode) +char *blur_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, + int mode) { return (strdup(gettext_noop(blur_descs[which][mode - 1]))); } //Do the effect for one pixel -static void do_blur_pixel(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void do_blur_pixel(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * last, int x, + int y) { magic_api *api = (magic_api *) ptr; int i, j, k; @@ -159,89 +167,98 @@ static void do_blur_pixel(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * c }; for (k = 0; k < 3; k++) - { - blurValue[k] = 0; - } + { + blurValue[k] = 0; + } for (i = -2; i < 3; i++) + { + for (j = -2; j < 3; j++) { - for (j = -2; j < 3; j++) - { - //Add the pixels around the current one wieghted - SDL_GetRGB(api->getpixel(last, x + i, y + j), last->format, &temp[0], &temp[1], &temp[2]); - for (k = 0; k < 3; k++) - { - blurValue[k] += temp[k] * weight[i + 2][j + 2]; - } - } + //Add the pixels around the current one wieghted + SDL_GetRGB(api->getpixel(last, x + i, y + j), last->format, &temp[0], + &temp[1], &temp[2]); + for (k = 0; k < 3; k++) + { + blurValue[k] += temp[k] * weight[i + 2][j + 2]; + } } + } for (k = 0; k < 3; k++) - { - blurValue[k] /= 273; - } - api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, blurValue[0], blurValue[1], blurValue[2])); + { + blurValue[k] /= 273; + } + api->putpixel(canvas, x, y, + SDL_MapRGB(canvas->format, blurValue[0], blurValue[1], + blurValue[2])); } // Do the effect for the full image -static void do_blur_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int which) +static void do_blur_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, + int which) { - magic_api * api = (magic_api *) ptr; + magic_api *api = (magic_api *) ptr; int x, y; for (y = 0; y < last->h; y++) + { + if (y % 10 == 0) { - if (y % 10 == 0) { - api->update_progress_bar(); - } - - for (x = 0; x < last->w; x++) - { - do_blur_pixel(api, which, canvas, last, x, y); - } + api->update_progress_bar(); } + + for (x = 0; x < last->w; x++) + { + do_blur_pixel(api, which, canvas, last, x, y); + } + } } //do the effect for the brush -static void do_blur_brush(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void do_blur_brush(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y) { int xx, yy; magic_api *api = (magic_api *) ptr; for (yy = y - blur_RADIUS; yy < y + blur_RADIUS; yy++) + { + for (xx = x - blur_RADIUS; xx < x + blur_RADIUS; xx++) { - for (xx = x - blur_RADIUS; xx < x + blur_RADIUS; xx++) - { - if (api->in_circle(xx - x, yy - y, blur_RADIUS) && !api->touched(xx, yy)) - { - do_blur_pixel(api, which, canvas, last, xx, yy); - } - } + if (api->in_circle(xx - x, yy - y, blur_RADIUS) + && !api->touched(xx, yy)) + { + do_blur_pixel(api, which, canvas, last, xx, yy); + } } + } } // Affect the canvas on drag: void blur_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) { - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_blur_brush); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, + do_blur_brush); api->playsound(blur_snd_effect[which], (x * 255) / canvas->w, 255); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = ox - blur_RADIUS; update_rect->y = oy - blur_RADIUS; @@ -251,25 +268,29 @@ void blur_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void blur_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) { if (mode == MODE_PAINT) blur_drag(api, which, canvas, last, x, y, x, y, update_rect); else - { - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; - do_blur_full(api, canvas, last, which); - api->playsound(blur_snd_effect[which], 128, 255); - } + { + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; + do_blur_full(api, canvas, last, which); + api->playsound(blur_snd_effect[which], 128, 255); + } } // Affect the canvas on release: -void blur_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) +void blur_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) { } @@ -280,32 +301,36 @@ void blur_shutdown(magic_api * api ATTRIBUTE_UNUSED) int i; for (i = 0; i < blur_NUM_TOOLS; i++) + { + if (blur_snd_effect[i] != NULL) { - if (blur_snd_effect[i] != NULL) - { - Mix_FreeChunk(blur_snd_effect[i]); - } + Mix_FreeChunk(blur_snd_effect[i]); } + } } // Record the color from Tux Paint: -void blur_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, +void blur_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) { } // Use colors: -int blur_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int blur_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void blur_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void blur_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void blur_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void blur_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } diff --git a/magic/src/bricks.c b/magic/src/bricks.c index b921a18f0..7836f61e9 100644 --- a/magic/src/bricks.c +++ b/magic/src/bricks.c @@ -54,7 +54,8 @@ static Uint8 bricks_r, bricks_g, bricks_b; /* Local function prototype: */ -static void do_brick(magic_api * api, SDL_Surface * canvas, int x, int y, int w, int h); +static void do_brick(magic_api * api, SDL_Surface * canvas, int x, int y, + int w, int h); int bricks_init(magic_api * api); Uint32 bricks_api_version(void); int bricks_get_tool_count(magic_api * api); @@ -63,15 +64,18 @@ char *bricks_get_name(magic_api * api, int which); int bricks_get_group(magic_api * api, int which); char *bricks_get_description(magic_api * api, int which, int mode); void bricks_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void bricks_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void bricks_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); void bricks_release(magic_api * api, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); //An empty function. Is there a purpose to this? Ask moderator. void bricks_shutdown(magic_api * api); void bricks_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int bricks_requires_colors(magic_api * api, int which); -void bricks_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void bricks_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void bricks_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void bricks_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int bricks_modes(magic_api * api, int which); // No setup required: @@ -79,7 +83,8 @@ int bricks_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/brick.wav", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/brick.wav", + api->data_directory); brick_snd = Mix_LoadWAV(fname); return (1); @@ -102,19 +107,22 @@ SDL_Surface *bricks_get_icon(magic_api * api, int which) char fname[1024]; if (which == TOOL_LARGEBRICKS) - { - snprintf(fname, sizeof(fname), "%simages/magic/largebrick.png", api->data_directory); - } + { + snprintf(fname, sizeof(fname), "%simages/magic/largebrick.png", + api->data_directory); + } else if (which == TOOL_SMALLBRICKS) - { - snprintf(fname, sizeof(fname), "%simages/magic/smallbrick.png", api->data_directory); - } + { + snprintf(fname, sizeof(fname), "%simages/magic/smallbrick.png", + api->data_directory); + } return (IMG_Load(fname)); } // Return our names, localized: -char *bricks_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *bricks_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { /* Both are named "Bricks", at the moment: */ @@ -122,13 +130,15 @@ char *bricks_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUS } // Return our group (both the same): -int bricks_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int bricks_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PAINTING; } // Return our descriptions, localized: -char *bricks_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode ATTRIBUTE_UNUSED) +char *bricks_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, + int mode ATTRIBUTE_UNUSED) { if (which == TOOL_LARGEBRICKS) return (strdup(gettext_noop("Click and drag to draw large bricks."))); @@ -140,7 +150,8 @@ char *bricks_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mo // Do the effect: -static void do_bricks(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) +static void do_bricks(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { magic_api *api = (magic_api *) ptr; @@ -160,12 +171,12 @@ static void do_bricks(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * unsigned char *mybrick; if (which == TOOL_LARGEBRICKS) - { - vertical_joint = 4; // between a brick and the one above/below - horizontal_joint = 4; // between a brick and the one to the side - nominal_width = 36; - nominal_height = 24; // 11 to 14, for joints of 2 - } + { + vertical_joint = 4; // between a brick and the one above/below + horizontal_joint = 4; // between a brick and the one to the side + nominal_width = 36; + nominal_height = 24; // 11 to 14, for joints of 2 + } nominal_length = 2 * nominal_width; specified_width = nominal_width - horizontal_joint; @@ -173,79 +184,82 @@ static void do_bricks(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * specified_length = nominal_length - horizontal_joint; if (!api->button_down()) - { - if (map) - free(map); - // the "+ 3" allows for both ends and misalignment - x_count = (canvas->w + nominal_width - 1) / nominal_width + 3; - y_count = (canvas->h + nominal_height - 1) / nominal_height + 3; - map = calloc(x_count, y_count); - } + { + if (map) + free(map); + // the "+ 3" allows for both ends and misalignment + x_count = (canvas->w + nominal_width - 1) / nominal_width + 3; + y_count = (canvas->h + nominal_height - 1) / nominal_height + 3; + map = calloc(x_count, y_count); + } brick_x = x / nominal_width; brick_y = y / nominal_height; mybrick = map + brick_x + 1 + (brick_y + 1) * x_count; - if ((unsigned)x < (unsigned)canvas->w && (unsigned)y < (unsigned)canvas->h && !*mybrick) + if ((unsigned) x < (unsigned) canvas->w + && (unsigned) y < (unsigned) canvas->h && !*mybrick) + { + int my_x = brick_x * nominal_width; + int my_w = specified_width; + + *mybrick = 1; + + + // FIXME: + //SDL_LockSurface(canvas); + + if ((brick_y ^ brick_x) & 1) { - int my_x = brick_x * nominal_width; - int my_w = specified_width; - - *mybrick = 1; - - - // FIXME: - //SDL_LockSurface(canvas); - - if ((brick_y ^ brick_x) & 1) - { - if (mybrick[1]) - my_w = specified_length; - } - else if (mybrick[-1]) - { - my_x -= nominal_width; - my_w = specified_length; - } - do_brick(api, canvas, my_x, brick_y * nominal_height, my_w, specified_height); - - - // FIXME: - // SDL_UnlockSurface(canvas); - - // upper left corner and lower right corner - - // FIXME - /* - update_canvas(brick_x * nominal_width - nominal_width, - brick_y * nominal_height - vertical_joint, - brick_x * nominal_width + specified_length, - (brick_y + 1) * nominal_height); - */ + if (mybrick[1]) + my_w = specified_length; } + else if (mybrick[-1]) + { + my_x -= nominal_width; + my_w = specified_length; + } + do_brick(api, canvas, my_x, brick_y * nominal_height, my_w, + specified_height); + + + // FIXME: + // SDL_UnlockSurface(canvas); + + // upper left corner and lower right corner + + // FIXME + /* + update_canvas(brick_x * nominal_width - nominal_width, + brick_y * nominal_height - vertical_joint, + brick_x * nominal_width + specified_length, + (brick_y + 1) * nominal_height); + */ + } } // Affect the canvas on drag: void bricks_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) { - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_bricks); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, do_bricks); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = x - 64; update_rect->y = y - 64; @@ -257,14 +271,18 @@ void bricks_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void bricks_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) { bricks_drag(api, which, canvas, last, x, y, x, y, update_rect); } -void bricks_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) +void bricks_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) { } @@ -276,7 +294,8 @@ void bricks_shutdown(magic_api * api ATTRIBUTE_UNUSED) } // Record the color from Tux Paint: -void bricks_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) +void bricks_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, + Uint8 b) { bricks_r = r; bricks_g = g; @@ -284,21 +303,29 @@ void bricks_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 } // Use colors: -int bricks_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int bricks_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 1; } -static void do_brick(magic_api * api, SDL_Surface * canvas, int x, int y, int w, int h) +static void do_brick(magic_api * api, SDL_Surface * canvas, int x, int y, + int w, int h) { SDL_Rect dest; // brick color: 127,76,73 - double ran_r = rand() / (double)RAND_MAX; - double ran_g = rand() / (double)RAND_MAX; - double base_r = api->sRGB_to_linear(bricks_r) * 1.5 + api->sRGB_to_linear(127) * 5.0 + ran_r; - double base_g = api->sRGB_to_linear(bricks_g) * 1.5 + api->sRGB_to_linear(76) * 5.0 + ran_g; - double base_b = api->sRGB_to_linear(bricks_b) * 1.5 + api->sRGB_to_linear(73) * 5.0 + (ran_r + ran_g * 2.0) / 3.0; + double ran_r = rand() / (double) RAND_MAX; + double ran_g = rand() / (double) RAND_MAX; + double base_r = + api->sRGB_to_linear(bricks_r) * 1.5 + api->sRGB_to_linear(127) * 5.0 + + ran_r; + double base_g = + api->sRGB_to_linear(bricks_g) * 1.5 + api->sRGB_to_linear(76) * 5.0 + + ran_g; + double base_b = + api->sRGB_to_linear(bricks_b) * 1.5 + api->sRGB_to_linear(73) * 5.0 + + (ran_r + ran_g * 2.0) / 3.0; Uint8 r = api->linear_to_sRGB(base_r / 7.5); Uint8 g = api->linear_to_sRGB(base_g / 7.5); @@ -318,13 +345,15 @@ static void do_brick(magic_api * api, SDL_Surface * canvas, int x, int y, int w, api->playsound(brick_snd, (x * 255) / canvas->w, 255); } -void bricks_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void bricks_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void bricks_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void bricks_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } diff --git a/magic/src/calligraphy.c b/magic/src/calligraphy.c index 6d96492e3..78af803b0 100644 --- a/magic/src/calligraphy.c +++ b/magic/src/calligraphy.c @@ -50,7 +50,8 @@ 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 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); @@ -60,16 +61,21 @@ char *calligraphy_get_name(magic_api * api, int which); int calligraphy_get_group(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); -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_release(magic_api * api, int which, 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); -void calligraphy_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void calligraphy_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void calligraphy_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void calligraphy_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int calligraphy_modes(magic_api * api, int which); // No setup required: @@ -77,11 +83,13 @@ int calligraphy_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/calligraphy.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/calligraphy.ogg", + api->data_directory); calligraphy_snd = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%simages/magic/calligraphy_brush.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/calligraphy_brush.png", + api->data_directory); calligraphy_brush = IMG_Load(fname); calligraphy_colored_brush = NULL; @@ -115,32 +123,40 @@ SDL_Surface *calligraphy_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/calligraphy.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/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 our group -int calligraphy_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int calligraphy_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PAINTING; } // Return our description, localized: -char *calligraphy_get_description(magic_api * api ATTRIBUTE_UNUSED, int which 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) +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) { Point2D *curve; int i, n_points, thick, new_thick; @@ -180,7 +196,9 @@ void calligraphy_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * 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[2].y, + calligraphy_control_points[3].x, + calligraphy_control_points[3].y); if (n_points == 0) return; // No-op; not any points to plot @@ -193,36 +211,37 @@ void calligraphy_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * 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; + { + thick = + ((new_thick * i) + (calligraphy_old_thick * (n_points - i))) / n_points; - /* The new way, using an antialiased brush bitmap */ + /* The new way, using an antialiased brush bitmap */ - x = curve[i].x; - y = curve[i].y; + 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; + 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; + dest.x = x - thick / 4; + dest.y = y - thick / 4; - SDL_BlitSurface(calligraphy_colored_brush, &src, canvas, &dest); + 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; + 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; + dest.x = x - thick / 2; + dest.y = y; - SDL_BlitSurface(calligraphy_colored_brush, &src, canvas, &dest); - } + SDL_BlitSurface(calligraphy_colored_brush, &src, canvas, &dest); + } calligraphy_old_thick = (calligraphy_old_thick + new_thick) / 2; @@ -231,19 +250,19 @@ void calligraphy_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = ox - 16; update_rect->y = oy - 16; @@ -260,9 +279,11 @@ void calligraphy_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * api->playsound(calligraphy_snd, (x * 255) / canvas->w, 255); } -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) +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) { calligraphy_old_thick = 8; calligraphy_last_time = 0; @@ -278,9 +299,12 @@ 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) +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) { } @@ -312,7 +336,9 @@ 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, @@ -320,7 +346,8 @@ 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! @@ -330,22 +357,26 @@ void calligraphy_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b) for (y = 0; y < calligraphy_brush->h; y++) + { + for (x = 0; x < calligraphy_brush->w; x++) { - for (x = 0; x < calligraphy_brush->w; x++) - { - SDL_GetRGBA(api->getpixel(calligraphy_brush, x, y), 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, - 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); } // We don't use colors -int calligraphy_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int calligraphy_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 1; } @@ -400,7 +431,8 @@ 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; @@ -420,17 +452,22 @@ static float calligraphy_dist(float x1, float y1, float x2, float y2) return d; } -void calligraphy_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode 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, +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); } diff --git a/magic/src/cartoon.c b/magic/src/cartoon.c index 29a3c8d72..3a0987cb1 100644 --- a/magic/src/cartoon.c +++ b/magic/src/cartoon.c @@ -39,7 +39,7 @@ /* Our globals: */ static Mix_Chunk *cartoon_snd; -SDL_Surface * result_surf; +SDL_Surface *result_surf; #define OUTLINE_THRESH 48 @@ -51,20 +51,26 @@ SDL_Surface *cartoon_get_icon(magic_api * api, int which); char *cartoon_get_name(magic_api * api, int which); int cartoon_get_group(magic_api * api, int which); char *cartoon_get_description(magic_api * api, int which, int mode); -void cartoon_apply_colors(magic_api * api, SDL_Surface * surf, int xx, int yy); +void cartoon_apply_colors(magic_api * api, SDL_Surface * surf, int xx, + int yy); void cartoon_apply_outline(magic_api * api, int xx, int yy); -static void do_cartoon(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +static void do_cartoon(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); void cartoon_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void cartoon_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void cartoon_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void cartoon_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); +void cartoon_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, + SDL_Rect * update_rect); void cartoon_shutdown(magic_api * api); void cartoon_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int cartoon_requires_colors(magic_api * api, int which); -void cartoon_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void cartoon_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void cartoon_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void cartoon_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int cartoon_modes(magic_api * api, int which); @@ -74,7 +80,8 @@ int cartoon_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/cartoon.wav", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/cartoon.wav", + api->data_directory); cartoon_snd = Mix_LoadWAV(fname); return (1); @@ -96,39 +103,48 @@ SDL_Surface *cartoon_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/cartoon.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/cartoon.png", + api->data_directory); return (IMG_Load(fname)); } // Return our names, localized: -char *cartoon_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *cartoon_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (strdup(gettext_noop("Cartoon"))); } // Return our groups -int cartoon_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int cartoon_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_COLOR_FILTERS; } // Return our descriptions, localized: -char *cartoon_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode) +char *cartoon_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode) { if (mode == MODE_PAINT) - { - return (strdup(gettext_noop("Click and drag the mouse around to turn the picture into a cartoon."))); - } + { + return (strdup + (gettext_noop + ("Click and drag the mouse around to turn the picture into a cartoon."))); + } else - { - return (strdup(gettext_noop("Click to turn the entire picture into a cartoon."))); - } + { + return (strdup + (gettext_noop + ("Click to turn the entire picture into a cartoon."))); + } } // Do the effect: -void cartoon_apply_colors(magic_api * api, SDL_Surface * surf, int xx, int yy) { +void cartoon_apply_colors(magic_api * api, SDL_Surface * surf, int xx, int yy) +{ Uint8 r, g, b; float hue, sat, val; @@ -149,69 +165,81 @@ void cartoon_apply_colors(magic_api * api, SDL_Surface * surf, int xx, int yy) { sat = floor(sat * 4) / 4; api->hsvtorgb(hue, sat, val, &r, &g, &b); - api->putpixel(result_surf, xx, yy, SDL_MapRGB(result_surf->format, r, g, b)); + api->putpixel(result_surf, xx, yy, + SDL_MapRGB(result_surf->format, r, g, b)); } -void cartoon_apply_outline(magic_api * api, int xx, int yy) { +void cartoon_apply_outline(magic_api * api, int xx, int yy) +{ Uint8 r, g, b; Uint8 r1, g1, b1, r2, g2, b2; - SDL_GetRGB(api->getpixel(result_surf, xx, yy), result_surf->format, &r, &g, &b); - SDL_GetRGB(api->getpixel(result_surf, xx + 1, yy), result_surf->format, &r1, &g1, &b1); - SDL_GetRGB(api->getpixel(result_surf, xx + 1, yy + 1), result_surf->format, &r2, &g2, &b2); + SDL_GetRGB(api->getpixel(result_surf, xx, yy), result_surf->format, &r, &g, + &b); + SDL_GetRGB(api->getpixel(result_surf, xx + 1, yy), result_surf->format, &r1, + &g1, &b1); + SDL_GetRGB(api->getpixel(result_surf, xx + 1, yy + 1), result_surf->format, + &r2, &g2, &b2); if (abs(((r + g + b) / 3) - (r1 + g1 + b1) / 3) > OUTLINE_THRESH || abs(((r + g + b) / 3) - (r2 + g2 + b2) / 3) > OUTLINE_THRESH || abs(r - r1) > OUTLINE_THRESH || abs(g - g1) > OUTLINE_THRESH || abs(b - b1) > OUTLINE_THRESH - || abs(r - r2) > OUTLINE_THRESH || abs(g - g2) > OUTLINE_THRESH || abs(b - b2) > OUTLINE_THRESH) - { - api->putpixel(result_surf, xx - 1, yy, SDL_MapRGB(result_surf->format, 0, 0, 0)); - api->putpixel(result_surf, xx, yy - 1, SDL_MapRGB(result_surf->format, 0, 0, 0)); - api->putpixel(result_surf, xx - 1, yy - 1, SDL_MapRGB(result_surf->format, 0, 0, 0)); - } + || abs(r - r2) > OUTLINE_THRESH || abs(g - g2) > OUTLINE_THRESH + || abs(b - b2) > OUTLINE_THRESH) + { + api->putpixel(result_surf, xx - 1, yy, + SDL_MapRGB(result_surf->format, 0, 0, 0)); + api->putpixel(result_surf, xx, yy - 1, + SDL_MapRGB(result_surf->format, 0, 0, 0)); + api->putpixel(result_surf, xx - 1, yy - 1, + SDL_MapRGB(result_surf->format, 0, 0, 0)); + } } -static void do_cartoon(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) +static void do_cartoon(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { magic_api *api = (magic_api *) ptr; int xx, yy; for (yy = y - 16; yy < y + 16; yy = yy + 1) + { + for (xx = x - 16; xx < x + 16; xx = xx + 1) { - for (xx = x - 16; xx < x + 16; xx = xx + 1) - { - if (api->in_circle(xx - x, yy - y, 16)) - { - api->putpixel(canvas, xx, yy, api->getpixel(result_surf, xx, yy)); - } - } + if (api->in_circle(xx - x, yy - y, 16)) + { + api->putpixel(canvas, xx, yy, api->getpixel(result_surf, xx, yy)); + } } + } } // Affect the canvas on drag: void cartoon_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) { if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_cartoon); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, do_cartoon); update_rect->x = ox - 16; update_rect->y = oy - 16; @@ -223,51 +251,57 @@ void cartoon_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void cartoon_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) { for (y = 0; y < canvas->h; y++) + { + if (y % 10 == 0) { - if (y % 10 == 0) { - api->update_progress_bar(); - } - - for (x = 0; x < canvas->w; x++) - { - cartoon_apply_colors(api, last, x, y); - } + api->update_progress_bar(); } + + for (x = 0; x < canvas->w; x++) + { + cartoon_apply_colors(api, last, x, y); + } + } for (y = 0; y < canvas->h; y++) + { + if (y % 10 == 0) { - if (y % 10 == 0) { - api->update_progress_bar(); - } - - for (x = 0; x < canvas->w; x++) - { - cartoon_apply_outline(api, x, y); - } + api->update_progress_bar(); } + for (x = 0; x < canvas->w; x++) + { + cartoon_apply_outline(api, x, y); + } + } + if (mode == MODE_PAINT) - { - cartoon_drag(api, which, canvas, last, x, y, x, y, update_rect); - } + { + cartoon_drag(api, which, canvas, last, x, y, x, y, update_rect); + } else - { - api->playsound(cartoon_snd, 128, 255); + { + api->playsound(cartoon_snd, 128, 255); - SDL_BlitSurface(result_surf, NULL, canvas, NULL); - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; - } + SDL_BlitSurface(result_surf, NULL, canvas, NULL); + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; + } } // Affect the canvas on release: -void cartoon_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) +void cartoon_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) { } @@ -279,39 +313,47 @@ void cartoon_shutdown(magic_api * api ATTRIBUTE_UNUSED) } // Record the color from Tux Paint: -void cartoon_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, +void cartoon_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) { } // Use colors: -int cartoon_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int cartoon_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void cartoon_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void cartoon_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas) { Uint32 amask; - amask = ~(canvas->format->Rmask | canvas->format->Gmask | canvas->format->Bmask); + amask = + ~(canvas->format->Rmask | canvas->format->Gmask | canvas->format->Bmask); result_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->w, canvas->h, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, amask); + canvas->format->Rmask, + canvas->format->Gmask, + canvas->format->Bmask, amask); } -void cartoon_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void cartoon_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { if (result_surf != NULL) SDL_FreeSurface(result_surf); } -int cartoon_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int cartoon_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_PAINT | MODE_FULLSCREEN); } diff --git a/magic/src/checkerboard.c b/magic/src/checkerboard.c index c07d9debc..67a2b5a57 100644 --- a/magic/src/checkerboard.c +++ b/magic/src/checkerboard.c @@ -46,15 +46,22 @@ int checkerboard_get_group(magic_api * api, int which); char *checkerboard_get_description(magic_api * api, int which, int mode); int checkerboard_requires_colors(magic_api * api, int which); void checkerboard_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); + SDL_Surface * canvas, SDL_Surface * snapshot, int x, + int y, SDL_Rect * update_rect); void checkerboard_shutdown(magic_api * api); -void checkerboard_paint_checkerboard(void *ptr_to_api, int which_tool, SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y); +void checkerboard_paint_checkerboard(void *ptr_to_api, int which_tool, + SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y); void checkerboard_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect); + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect); void checkerboard_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void checkerboard_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void checkerboard_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); + SDL_Surface * canvas, SDL_Surface * last, int x, + int y, SDL_Rect * update_rect); +void checkerboard_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void checkerboard_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int checkerboard_modes(magic_api * api, int which); // Housekeeping functions @@ -64,7 +71,7 @@ Uint32 checkerboard_api_version(void) return (TP_MAGIC_API_VERSION); } -void checkerboard_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) //get the colors from API and store it in structure +void checkerboard_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) //get the colors from API and store it in structure { checkerboard_r = r; checkerboard_g = g; @@ -75,7 +82,8 @@ int checkerboard_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/checkerboard.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/checkerboard.ogg", + api->data_directory); checkerboard_snd = Mix_LoadWAV(fname); return (1); @@ -86,40 +94,50 @@ int checkerboard_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) return 1; } -SDL_Surface *checkerboard_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *checkerboard_get_icon(magic_api * api, + int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/checkerboard.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/checkerboard.png", + api->data_directory); return (IMG_Load(fname)); } -char *checkerboard_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *checkerboard_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return strdup(gettext_noop("Checkerboard")); } -int checkerboard_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int checkerboard_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PICTURE_DECORATIONS; } -char *checkerboard_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *checkerboard_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED) { return strdup(gettext_noop ("Click and drag to fill the canvas with a checkerboard pattern.")); } -int checkerboard_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int checkerboard_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 1; } -void checkerboard_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * snapshot ATTRIBUTE_UNUSED, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) +void checkerboard_release(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED, + SDL_Surface * snapshot ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, + SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -130,10 +148,10 @@ void checkerboard_shutdown(magic_api * api ATTRIBUTE_UNUSED) // Interactivity functions -void checkerboard_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, - SDL_Surface * snapshot, - int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, - int x, int y, SDL_Rect * update_rect) +void checkerboard_drag(magic_api * api, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * snapshot, + int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, + int x, int y, SDL_Rect * update_rect) { int sz, xx, yy; Uint8 draw_start, draw_row, draw_cell; @@ -142,9 +160,13 @@ void checkerboard_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface SDL_BlitSurface(snapshot, NULL, canvas, NULL); - sz = max(10, max(abs(x - checkerboard_start_x), abs(y - checkerboard_start_y))); + sz = + max(10, + max(abs(x - checkerboard_start_x), abs(y - checkerboard_start_y))); - colr = SDL_MapRGB(canvas->format, checkerboard_r, checkerboard_g, checkerboard_b); + colr = + SDL_MapRGB(canvas->format, checkerboard_r, checkerboard_g, + checkerboard_b); draw_start = 1; if (x < checkerboard_start_x) @@ -154,11 +176,14 @@ void checkerboard_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface /* From the mouse Y position down... */ draw_row = draw_start; - for (yy = checkerboard_start_y; yy <= canvas->h; yy += sz) { + for (yy = checkerboard_start_y; yy <= canvas->h; yy += sz) + { /* From the mouse X position right... */ draw_cell = draw_row; - for (xx = checkerboard_start_x; xx <= canvas->w; xx += sz) { - if (draw_cell) { + for (xx = checkerboard_start_x; xx <= canvas->w; xx += sz) + { + if (draw_cell) + { dest.x = xx; dest.y = yy; dest.w = sz; @@ -170,8 +195,10 @@ void checkerboard_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface /* From the mouse X position left... */ draw_cell = !draw_row; - for (xx = checkerboard_start_x - sz; xx > -sz; xx -= sz) { - if (draw_cell) { + for (xx = checkerboard_start_x - sz; xx > -sz; xx -= sz) + { + if (draw_cell) + { dest.x = xx; dest.y = yy; dest.w = sz; @@ -186,11 +213,14 @@ void checkerboard_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface /* From the mouse Y position up... */ draw_row = !draw_start; - for (yy = checkerboard_start_y - sz; yy > -sz; yy -= sz) { + for (yy = checkerboard_start_y - sz; yy > -sz; yy -= sz) + { /* From the mouse X position right... */ draw_cell = draw_row; - for (xx = checkerboard_start_x; xx <= canvas->w; xx += sz) { - if (draw_cell) { + for (xx = checkerboard_start_x; xx <= canvas->w; xx += sz) + { + if (draw_cell) + { dest.x = xx; dest.y = yy; dest.w = sz; @@ -202,8 +232,10 @@ void checkerboard_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface /* From the mouse X position left... */ draw_cell = !draw_row; - for (xx = checkerboard_start_x - sz; xx > -sz; xx -= sz) { - if (draw_cell) { + for (xx = checkerboard_start_x - sz; xx > -sz; xx -= sz) + { + if (draw_cell) + { dest.x = xx; dest.y = yy; dest.w = sz; @@ -225,27 +257,32 @@ void checkerboard_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface api->playsound(checkerboard_snd, 128, 255); } -void checkerboard_click(magic_api * api, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * last, - int x, int y, - SDL_Rect * update_rect) +void checkerboard_click(magic_api * api, int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, + SDL_Rect * update_rect) { checkerboard_start_x = x; checkerboard_start_y = y; checkerboard_drag(api, which, canvas, last, x, y, x, y, update_rect); } -void checkerboard_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, - SDL_Surface * canvas ATTRIBUTE_UNUSED) +void checkerboard_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void checkerboard_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, - SDL_Surface * canvas ATTRIBUTE_UNUSED) +void checkerboard_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -int checkerboard_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int checkerboard_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_PAINT); } diff --git a/magic/src/clone.c b/magic/src/clone.c index 43af0b710..7dd9ac88d 100644 --- a/magic/src/clone.c +++ b/magic/src/clone.c @@ -59,7 +59,7 @@ static Mix_Chunk *clone_start_snd, *clone_snd; int clone_state; int clone_src_x, clone_src_y; int clone_drag_start_x, clone_drag_start_y; -SDL_Surface * clone_last; +SDL_Surface *clone_last; int clone_crosshair_visible; @@ -73,31 +73,38 @@ char *clone_get_name(magic_api * api, int which); int clone_get_group(magic_api * api, int which); char *clone_get_description(magic_api * api, int which, int mode); void clone_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 clone_doit(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect, - int crosshairs); -void clone_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void clone_release(magic_api * api, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect, int crosshairs); +void clone_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); +void clone_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); void clone_shutdown(magic_api * api); void clone_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int clone_requires_colors(magic_api * api, int which); -void clone_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void clone_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void clone_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void clone_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int clone_modes(magic_api * api, int which); void clone_crosshairs(magic_api * api, SDL_Surface * canvas, int x, int y); -void done_cloning(magic_api * api, SDL_Surface * canvas, SDL_Rect * update_rect); +void done_cloning(magic_api * api, SDL_Surface * canvas, + SDL_Rect * update_rect); // No setup required: int clone_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/clone_start.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/clone_start.ogg", + api->data_directory); clone_start_snd = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%ssounds/magic/clone.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/clone.ogg", + api->data_directory); clone_snd = Mix_LoadWAV(fname); clone_state = CLONE_READY_TO_START; @@ -122,34 +129,42 @@ SDL_Surface *clone_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/clone.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/clone.png", + api->data_directory); return (IMG_Load(fname)); } // Return our names, localized: -char *clone_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *clone_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (strdup(gettext_noop("Clone"))); } // Return our groups: -int clone_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int clone_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_DISTORTS; } // Return our descriptions, localized: -char *clone_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *clone_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED) { - return (strdup(gettext_noop("Click once to pick a spot to begin cloning. Click again and drag to clone that part of the picture."))); + return (strdup + (gettext_noop + ("Click once to pick a spot to begin cloning. Click again and drag to clone that part of the picture."))); return (NULL); } // Do the effect: -static void do_clone(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void do_clone(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * last, int x, int y) { magic_api *api = (magic_api *) ptr; Uint8 r, g, b; @@ -161,25 +176,27 @@ static void do_clone(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas srcy = clone_src_y + (y - clone_drag_start_y); if (!api->touched(x, y)) + { + for (yy = -16; yy < 16; yy++) { - for (yy = -16; yy < 16; yy++) + for (xx = -16; xx < 16; xx++) + { + if (api->in_circle(xx, yy, 16)) { - for (xx = -16; xx < 16; xx++) - { - if (api->in_circle(xx, yy, 16)) - { - SDL_GetRGB(api->getpixel(last, srcx + xx, srcy + yy), last->format, &r, &g, &b); - pixel = SDL_MapRGB(canvas->format, r, g, b); - api->putpixel(canvas, x + xx, y + yy, pixel); - } - } + SDL_GetRGB(api->getpixel(last, srcx + xx, srcy + yy), last->format, + &r, &g, &b); + pixel = SDL_MapRGB(canvas->format, r, g, b); + api->putpixel(canvas, x + xx, y + yy, pixel); } + } } + } } // Affect the canvas on drag: void clone_drag(magic_api * api, int which, 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) { /* Step 3 - Actively cloning (moving the mouse) */ @@ -192,8 +209,8 @@ void clone_drag(magic_api * api, int which, SDL_Surface * canvas, } void clone_doit(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect, - int crosshairs) + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect, int crosshairs) { if (clone_state != CLONE_CLONING) return; @@ -201,28 +218,29 @@ void clone_doit(magic_api * api, int which, SDL_Surface * canvas, clone_drag_start_x = ox; clone_drag_start_y = oy; - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_clone); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, do_clone); /* Move source position relative to mouse motion */ clone_src_x += (x - ox); clone_src_y += (y - oy); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } - if (crosshairs) { + if (crosshairs) + { clone_crosshairs(api, canvas, clone_src_x, clone_src_y); /* FIXME be more clever */ update_rect->x = 0; @@ -230,7 +248,9 @@ void clone_doit(magic_api * api, int which, SDL_Surface * canvas, update_rect->w = canvas->w; update_rect->h = canvas->h; clone_crosshair_visible = 1; - } else { + } + else + { update_rect->x = x - 64; update_rect->y = y - 64; update_rect->w = (ox + 128) - update_rect->x; @@ -242,9 +262,11 @@ void clone_doit(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void clone_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) { - if (clone_state == CLONE_READY_TO_START) { + if (clone_state == CLONE_READY_TO_START) + { /* Step 1 - Picking a source for the clone */ clone_src_x = x; clone_src_y = y; @@ -260,47 +282,57 @@ void clone_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, update_rect->y = y - 15; update_rect->w = 32; update_rect->h = 32; - } else if (clone_state == CLONE_CLONING) { + } + else if (clone_state == CLONE_CLONING) + { /* Step 2 - Starting a clone (hopefully holding mouse down here) */ clone_doit(api, which, canvas, clone_last, x, y, x, y, update_rect, 0); } } void clone_release(magic_api * api, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect) + SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, + SDL_Rect * update_rect) { - if (clone_state == CLONE_STARTING) { + if (clone_state == CLONE_STARTING) + { /* Release of the initial click (to pick initial source position); now ready for second click (to begin cloning) */ clone_state = CLONE_CLONING; - } else { + } + else + { done_cloning(api, canvas, update_rect); } } -void done_cloning(magic_api * api, SDL_Surface * canvas, SDL_Rect * update_rect) { +void done_cloning(magic_api * api, SDL_Surface * canvas, + SDL_Rect * update_rect) +{ /* Done cloning! */ /* Erase crosshairs from source position, now that we're all done */ if (clone_crosshair_visible) - { - clone_crosshairs(api, canvas, clone_src_x, clone_src_y); - update_rect->x = clone_src_x - 15; - update_rect->y = clone_src_y - 15; - update_rect->w = 32; - update_rect->h = 32; - clone_crosshair_visible = 0; - } + { + clone_crosshairs(api, canvas, clone_src_x, clone_src_y); + update_rect->x = clone_src_x - 15; + update_rect->y = clone_src_y - 15; + update_rect->w = 32; + update_rect->h = 32; + clone_crosshair_visible = 0; + } clone_state = CLONE_READY_TO_START; api->stopsound(); } -void clone_crosshairs(magic_api * api, SDL_Surface * canvas, int x, int y) { +void clone_crosshairs(magic_api * api, SDL_Surface * canvas, int x, int y) +{ int i; - for (i = -15; i < 16; i++) { + for (i = -15; i < 16; i++) + { api->xorpixel(canvas, x + i, y); api->xorpixel(canvas, x, y + i); } @@ -314,29 +346,35 @@ void clone_shutdown(magic_api * api ATTRIBUTE_UNUSED) Mix_FreeChunk(clone_start_snd); } -void clone_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 ATTRIBUTE_UNUSED r, Uint8 ATTRIBUTE_UNUSED g, Uint8 ATTRIBUTE_UNUSED b) +void clone_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 ATTRIBUTE_UNUSED r, Uint8 ATTRIBUTE_UNUSED g, + Uint8 ATTRIBUTE_UNUSED b) { } -int clone_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int clone_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void clone_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void clone_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { - clone_last = SDL_CreateRGBSurface(0, canvas->w, canvas->h, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, - canvas->format->Amask); + clone_last = + SDL_CreateRGBSurface(0, canvas->w, canvas->h, + canvas->format->BitsPerPixel, canvas->format->Rmask, + canvas->format->Gmask, canvas->format->Bmask, + canvas->format->Amask); clone_state = CLONE_READY_TO_START; } void clone_switchout(magic_api * api, int which ATTRIBUTE_UNUSED, - int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas) + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas) { - SDL_Rect update_rect; /* Needed to satisfy done_cloning() :-( */ + SDL_Rect update_rect; /* Needed to satisfy done_cloning() :-( */ done_cloning(api, canvas, &update_rect); diff --git a/magic/src/confetti.c b/magic/src/confetti.c index 01406aee7..084ee5dc8 100644 --- a/magic/src/confetti.c +++ b/magic/src/confetti.c @@ -33,21 +33,26 @@ int confetti_get_group(magic_api * api, int which); char *confetti_get_description(magic_api * api, int which, int mode); int confetti_requires_colors(magic_api * api, int which); void confetti_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); + SDL_Surface * canvas, SDL_Surface * snapshot, int x, + int y, SDL_Rect * update_rect); void confetti_shutdown(magic_api * api); inline char confetti_get_greater(const char what1, const char what2); inline char confetti_get_lesser(const char what1, const char what2); Uint32 confetti_get_new_color(void *ptr, SDL_Surface * canvas); void confetti_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void confetti_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void confetti_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); + SDL_Surface * canvas, SDL_Surface * last, int x, int y, + SDL_Rect * update_rect); +void confetti_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void confetti_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int confetti_modes(magic_api * api, int which); // Housekeeping functions void confetti_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect); + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect); Uint32 confetti_api_version(void) { @@ -65,7 +70,8 @@ int confetti_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/confetti.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/confetti.ogg", + api->data_directory); confetti_snd = Mix_LoadWAV(fname); return (1); @@ -80,34 +86,43 @@ SDL_Surface *confetti_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/confetti.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/confetti.png", + api->data_directory); return (IMG_Load(fname)); } -char *confetti_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *confetti_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return strdup(gettext_noop("Confetti")); } -int confetti_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int confetti_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PAINTING; } -char *confetti_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *confetti_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED) { return strdup(gettext_noop("Click to throw confetti!")); } -int confetti_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int confetti_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 1; } -void confetti_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * snapshot ATTRIBUTE_UNUSED, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) +void confetti_release(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED, + SDL_Surface * snapshot ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, + SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -160,48 +175,52 @@ Uint32 confetti_get_new_color(void *ptr, SDL_Surface * canvas) //this function static void confetti_circle(void *ptr, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) + SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { magic_api *api = (magic_api *) ptr; int xx, yy; Uint32 color = confetti_get_new_color(api, canvas); - for (yy = y - CONFETTI_BRUSH_SIZE / 2; yy < y + CONFETTI_BRUSH_SIZE / 2; yy++) + for (yy = y - CONFETTI_BRUSH_SIZE / 2; yy < y + CONFETTI_BRUSH_SIZE / 2; + yy++) - for (xx = x - CONFETTI_BRUSH_SIZE / 2; xx < x + CONFETTI_BRUSH_SIZE / 2; xx++) + for (xx = x - CONFETTI_BRUSH_SIZE / 2; xx < x + CONFETTI_BRUSH_SIZE / 2; + xx++) if (api->in_circle(xx - x, yy - y, CONFETTI_BRUSH_SIZE / 2)) api->putpixel(canvas, xx, yy, color); } void confetti_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) { unsigned char i; char min_x = 0, max_x = 0, min_y = 0, max_y = 0; char dx = 0, dy = 0; for (i = 0; i < CONFETTI_QUANTITY; i++) - { - srand((dx + dy) / 2 + time(0)); //to get a unique seed even if dx and dy aren't defined - dx = (rand() % 100) - 50; //generate a value between <-50; +50> - dy = (rand() % 100) - 50; //to spread confetti around the cursor position + { + srand((dx + dy) / 2 + time(0)); //to get a unique seed even if dx and dy aren't defined + dx = (rand() % 100) - 50; //generate a value between <-50; +50> + dy = (rand() % 100) - 50; //to spread confetti around the cursor position - if (!i) - { - min_x = max_x = dx; - min_y = max_y = dy; - } - else - { - min_x = confetti_get_lesser(min_x, dx); //any candidates to new min/max values? Hands up please... - max_x = confetti_get_greater(max_x, dx); - min_y = confetti_get_lesser(min_y, dy); - max_y = confetti_get_greater(max_y, dy); - } - confetti_circle((void *)api, which, canvas, last, x + dx, y + dy); + if (!i) + { + min_x = max_x = dx; + min_y = max_y = dy; } + else + { + min_x = confetti_get_lesser(min_x, dx); //any candidates to new min/max values? Hands up please... + max_x = confetti_get_greater(max_x, dx); + min_y = confetti_get_lesser(min_y, dy); + max_y = confetti_get_greater(max_y, dy); + } + confetti_circle((void *) api, which, canvas, last, x + dx, y + dy); + } update_rect->x = x + min_x - CONFETTI_BRUSH_SIZE / 2; update_rect->y = y + min_y - CONFETTI_BRUSH_SIZE / 2; @@ -212,37 +231,41 @@ void confetti_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, } void confetti_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect) + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect) { int temp; if (ox > x) - { - temp = x; - x = ox; - ox = temp; - } + { + temp = x; + x = ox; + ox = temp; + } if (oy > y) - { - temp = y; - y = oy; - oy = temp; - } + { + temp = y; + y = oy; + oy = temp; + } confetti_click(api, which, MODE_PAINT, canvas, snapshot, x, y, update_rect); } -void confetti_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void confetti_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void confetti_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void confetti_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -int confetti_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int confetti_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_PAINT); } diff --git a/magic/src/distortion.c b/magic/src/distortion.c index 5b39f0329..2ec688a55 100644 --- a/magic/src/distortion.c +++ b/magic/src/distortion.c @@ -65,19 +65,26 @@ int distortion_requires_colors(magic_api * api, int which); void distortion_shutdown(magic_api * api); void distortion_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); -void distortion_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); + SDL_Surface * canvas, SDL_Surface * snapshot, int x, + int y, SDL_Rect * update_rect); +void distortion_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect); void distortion_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); -void distortion_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void distortion_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void distortion_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void distortion_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int distortion_modes(magic_api * api, int which); void distortion_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect); + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect); -static void distortion_line_callback(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y); +static void distortion_line_callback(void *ptr, int which, + SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y); /* Setup Functions: */ @@ -95,7 +102,8 @@ int distortion_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/distortion.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/distortion.ogg", + api->data_directory); // Try to load the file! @@ -119,7 +127,8 @@ SDL_Surface *distortion_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/distortion.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/distortion.png", + api->data_directory); // Try to load the image, and return the results to Tux Paint: @@ -130,7 +139,8 @@ SDL_Surface *distortion_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) // Report our "Magic" tool names -char *distortion_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *distortion_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (strdup(gettext_noop("Distortion"))); } @@ -138,7 +148,8 @@ char *distortion_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_ // Report our "Magic" tool groups -int distortion_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int distortion_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_DISTORTS; } @@ -146,15 +157,19 @@ int distortion_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_U // Report our "Magic" tool descriptions -char *distortion_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, +char *distortion_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) { - return (strdup(gettext_noop("Click and drag the mouse to cause distortion in your picture."))); + return (strdup + (gettext_noop + ("Click and drag the mouse to cause distortion in your picture."))); } // Report whether we accept colors -int distortion_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int distortion_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } @@ -174,7 +189,8 @@ void distortion_shutdown(magic_api * api ATTRIBUTE_UNUSED) // Affect the canvas on click: void distortion_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect) + SDL_Surface * canvas, SDL_Surface * snapshot, int x, + int y, SDL_Rect * update_rect) { distortion_drag(api, which, canvas, snapshot, x, y, x, y, update_rect); } @@ -183,25 +199,27 @@ void distortion_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, // Affect the canvas on drag: void distortion_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect) + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect) { - api->line((void *)api, which, canvas, snapshot, ox, oy, x, y, 1, distortion_line_callback); + api->line((void *) api, which, canvas, snapshot, ox, oy, x, y, 1, + distortion_line_callback); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = ox - 8; @@ -217,14 +235,18 @@ void distortion_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on release: -void distortion_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * snapshot ATTRIBUTE_UNUSED, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) +void distortion_release(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED, + SDL_Surface * snapshot ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, + SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } -void distortion_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, +void distortion_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) { } @@ -233,7 +255,8 @@ void distortion_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UN // Our "callback" function static void distortion_line_callback(void *ptr, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y) + SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y) { magic_api *api = (magic_api *) ptr; int xx, yy; @@ -244,28 +267,34 @@ static void distortion_line_callback(void *ptr, int which ATTRIBUTE_UNUSED, // Tux Paint sends to us with the values we enumerated above. for (yy = -8; yy < 8; yy++) + { + for (xx = -8; xx < 8; xx++) { - for (xx = -8; xx < 8; xx++) - { - if (api->in_circle(xx, yy, 8)) - { - api->putpixel(canvas, x + xx, y + yy, api->getpixel(snapshot, x + xx / 2, y + yy)); - } - } + if (api->in_circle(xx, yy, 8)) + { + api->putpixel(canvas, x + xx, y + yy, + api->getpixel(snapshot, x + xx / 2, y + yy)); + } } + } } -void distortion_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void distortion_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void distortion_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void distortion_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -int distortion_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int distortion_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_PAINT); } diff --git a/magic/src/emboss.c b/magic/src/emboss.c index b9792a0e6..aa687de0a 100644 --- a/magic/src/emboss.c +++ b/magic/src/emboss.c @@ -47,19 +47,24 @@ int emboss_get_group(magic_api * api, int which); char *emboss_get_description(magic_api * api, int which, int mode); void emboss_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 emboss_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 emboss_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 emboss_shutdown(magic_api * api); void emboss_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int emboss_requires_colors(magic_api * api, int which); -void emboss_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void emboss_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void emboss_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void emboss_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int emboss_modes(magic_api * api, int which); @@ -74,7 +79,8 @@ int emboss_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/emboss.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/emboss.ogg", + api->data_directory); emboss_snd = Mix_LoadWAV(fname); return (1); @@ -91,28 +97,34 @@ SDL_Surface *emboss_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/emboss.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/emboss.png", + api->data_directory); return (IMG_Load(fname)); } // Return our names, localized: -char *emboss_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *emboss_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (strdup(gettext_noop("Emboss"))); } // Return our groups: -int emboss_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int emboss_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_DISTORTS; } // Return our descriptions, localized: -char *emboss_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode) +char *emboss_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode) { if (mode == MODE_PAINT) - return (strdup(gettext_noop("Click and drag the mouse to emboss the picture."))); + return (strdup + (gettext_noop + ("Click and drag the mouse to emboss the picture."))); else return (strdup(gettext_noop("Click to emboss the entire picture."))); } @@ -120,7 +132,9 @@ char *emboss_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBU // Do the effect (single pixel; used by do_emboss() (painted circle) // and emboss_click() when in fullscreen mode): -static void emboss_pixel(void * ptr, SDL_Surface * last, int x, int y, SDL_Surface * canvas) { +static void emboss_pixel(void *ptr, SDL_Surface * last, int x, int y, + SDL_Surface * canvas) +{ magic_api *api = (magic_api *) ptr; Uint8 r1, g1, b1, r2, g2, b2; int r; @@ -150,46 +164,48 @@ static void emboss_pixel(void * ptr, SDL_Surface * last, int x, int y, SDL_Surfa // Do the effect (a circle around a touch point): -static void do_emboss(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void do_emboss(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * last, int x, int y) { magic_api *api = (magic_api *) ptr; int xx, yy; for (yy = -16; yy < 16; yy++) + { + for (xx = -16; xx < 16; xx++) { - for (xx = -16; xx < 16; xx++) + if (api->in_circle(xx, yy, 16)) + { + if (!api->touched(x + xx, y + yy)) { - if (api->in_circle(xx, yy, 16)) - { - if (!api->touched(x + xx, y + yy)) - { - emboss_pixel(api, last, x + xx, y + yy, canvas); - } - } + emboss_pixel(api, last, x + xx, y + yy, canvas); } + } } + } } // Affect the canvas on drag: void emboss_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) { - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_emboss); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, do_emboss); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = ox - 16; update_rect->y = oy - 16; @@ -201,17 +217,24 @@ void emboss_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void emboss_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) { - if (mode == MODE_PAINT) { + if (mode == MODE_PAINT) + { emboss_drag(api, which, canvas, last, x, y, x, y, update_rect); - } else { - for (y = 0; y < canvas->h; y++) { - if (y % 10 == 0) { + } + else + { + for (y = 0; y < canvas->h; y++) + { + if (y % 10 == 0) + { api->update_progress_bar(); } - for (x = 0; x < canvas->w; x++) { + for (x = 0; x < canvas->w; x++) + { emboss_pixel(api, last, x, y, canvas); } } @@ -224,9 +247,12 @@ void emboss_click(magic_api * api, int which, int mode, } // Affect the canvas on release: -void emboss_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) +void emboss_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) { } @@ -238,23 +264,27 @@ void emboss_shutdown(magic_api * api ATTRIBUTE_UNUSED) } // Record the color from Tux Paint: -void emboss_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, +void emboss_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) { } // Use colors: -int emboss_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int emboss_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void emboss_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void emboss_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void emboss_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void emboss_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } diff --git a/magic/src/fade_darken.c b/magic/src/fade_darken.c index b4449b409..8388b307e 100644 --- a/magic/src/fade_darken.c +++ b/magic/src/fade_darken.c @@ -52,29 +52,38 @@ SDL_Surface *fade_darken_get_icon(magic_api * api, int which); int fade_darken_get_group(magic_api * api, int which); char *fade_darken_get_name(magic_api * api, int which); char *fade_darken_get_description(magic_api * api, int which, int mode); -static void do_fade_darken(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); -static void do_fade_darken_paint(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +static void do_fade_darken(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); +static void do_fade_darken_paint(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); void fade_darken_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 fade_darken_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void fade_darken_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 fade_darken_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, + SDL_Rect * update_rect); void fade_darken_shutdown(magic_api * api); void fade_darken_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int fade_darken_requires_colors(magic_api * api, int which); -void fade_darken_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void fade_darken_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void fade_darken_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void fade_darken_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int fade_darken_modes(magic_api * api, int which); int fade_darken_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/fade.wav", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/fade.wav", + api->data_directory); snd_effects[TOOL_FADE] = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%ssounds/magic/darken.wav", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/darken.wav", + api->data_directory); snd_effects[TOOL_DARKEN] = Mix_LoadWAV(fname); return (1); @@ -97,13 +106,15 @@ SDL_Surface *fade_darken_get_icon(magic_api * api, int which) char fname[1024]; if (which == TOOL_FADE) - { - snprintf(fname, sizeof(fname), "%simages/magic/fade.png", api->data_directory); - } + { + snprintf(fname, sizeof(fname), "%simages/magic/fade.png", + api->data_directory); + } else if (which == TOOL_DARKEN) - { - snprintf(fname, sizeof(fname), "%simages/magic/darken.png", api->data_directory); - } + { + snprintf(fname, sizeof(fname), "%simages/magic/darken.png", + api->data_directory); + } return (IMG_Load(fname)); } @@ -120,33 +131,40 @@ char *fade_darken_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) } // Return our group (all the same): -int fade_darken_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int fade_darken_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_COLOR_FILTERS; } // Return our description, localized: -char *fade_darken_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode) +char *fade_darken_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, + int mode) { if (which == TOOL_FADE) - { - if (mode == MODE_PAINT) - return (strdup(gettext_noop("Click and drag the mouse to lighten parts of your picture."))); - else if (mode == MODE_FULLSCREEN) - return (strdup(gettext_noop("Click to lighten your entire picture."))); - } + { + if (mode == MODE_PAINT) + return (strdup + (gettext_noop + ("Click and drag the mouse to lighten parts of your picture."))); + else if (mode == MODE_FULLSCREEN) + return (strdup(gettext_noop("Click to lighten your entire picture."))); + } else if (which == TOOL_DARKEN) - { - if (mode == MODE_PAINT) - return (strdup(gettext_noop("Click and drag the mouse to darken parts of your picture."))); - else if (mode == MODE_FULLSCREEN) - return (strdup(gettext_noop("Click to darken your entire picture."))); - } + { + if (mode == MODE_PAINT) + return (strdup + (gettext_noop + ("Click and drag the mouse to darken parts of your picture."))); + else if (mode == MODE_FULLSCREEN) + return (strdup(gettext_noop("Click to darken your entire picture."))); + } return (NULL); } -static void do_fade_darken(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void do_fade_darken(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y) { Uint8 r, g, b; magic_api *api = (magic_api *) ptr; @@ -154,47 +172,50 @@ static void do_fade_darken(void *ptr, int which, SDL_Surface * canvas, SDL_Surfa SDL_GetRGB(api->getpixel(last, x, y), last->format, &r, &g, &b); if (which == TOOL_FADE) - { - r = min(r + 48, 255); - g = min(g + 48, 255); - b = min(b + 48, 255); - } + { + r = min(r + 48, 255); + g = min(g + 48, 255); + b = min(b + 48, 255); + } else if (which == TOOL_DARKEN) - { - r = max(r - 48, 0); - g = max(g - 48, 0); - b = max(b - 48, 0); - } + { + r = max(r - 48, 0); + g = max(g - 48, 0); + b = max(b - 48, 0); + } api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, r, g, b)); } // Callback that does the fade_darken color effect on a circle centered around x,y -static void do_fade_darken_paint(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void do_fade_darken_paint(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y) { int xx, yy; magic_api *api = (magic_api *) ptr; for (yy = y - 16; yy < y + 16; yy++) + { + for (xx = x - 16; xx < x + 16; xx++) { - for (xx = x - 16; xx < x + 16; xx++) - { - if (api->in_circle(xx - x, yy - y, 16) && !api->touched(xx, yy)) - { - do_fade_darken(api, which, canvas, last, xx, yy); - } - } + if (api->in_circle(xx - x, yy - y, 16) && !api->touched(xx, yy)) + { + do_fade_darken(api, which, canvas, last, xx, yy); + } } + } } // Ask Tux Paint to call our 'do_fade_darken_paint()' callback over a line void fade_darken_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) { SDL_LockSurface(last); SDL_LockSurface(canvas); - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_fade_darken_paint); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, + do_fade_darken_paint); SDL_UnlockSurface(canvas); SDL_UnlockSurface(last); @@ -202,19 +223,19 @@ void fade_darken_drag(magic_api * api, int which, SDL_Surface * canvas, api->playsound(snd_effects[which], (x * 255) / canvas->w, 255); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = ox - 16; update_rect->y = oy - 16; @@ -225,31 +246,35 @@ void fade_darken_drag(magic_api * api, int which, SDL_Surface * canvas, // Ask Tux Paint to call our 'do_fade_darken_paint()' callback at a single point, // or 'do_fade_darken()' on the entire image void fade_darken_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) { if (mode == MODE_PAINT) fade_darken_drag(api, which, canvas, last, x, y, x, y, update_rect); else - { - int xx, yy; + { + int xx, yy; - for (yy = 0; yy < canvas->h; yy++) - for (xx = 0; xx < canvas->w; xx++) - do_fade_darken(api, which, canvas, last, xx, yy); + for (yy = 0; yy < canvas->h; yy++) + for (xx = 0; xx < canvas->w; xx++) + do_fade_darken(api, which, canvas, last, xx, yy); - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; - /* FIXME: Play sfx */ - } + /* FIXME: Play sfx */ + } } // Release -void fade_darken_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) +void fade_darken_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) { } @@ -265,27 +290,34 @@ void fade_darken_shutdown(magic_api * api ATTRIBUTE_UNUSED) // We don't use colors void fade_darken_set_color(magic_api * api ATTRIBUTE_UNUSED, - Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, + Uint8 b ATTRIBUTE_UNUSED) { } // We don't use colors -int fade_darken_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int fade_darken_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void fade_darken_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void fade_darken_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void fade_darken_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void fade_darken_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -int fade_darken_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int fade_darken_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_PAINT | MODE_FULLSCREEN); } diff --git a/magic/src/fisheye.c b/magic/src/fisheye.c index 54208d834..192cf67f5 100644 --- a/magic/src/fisheye.c +++ b/magic/src/fisheye.c @@ -45,29 +45,36 @@ int fisheye_get_group(magic_api * api, int which); char *fisheye_get_description(magic_api * api, int which, int mode); int fisheye_requires_colors(magic_api * api, int which); void fisheye_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); + SDL_Surface * canvas, SDL_Surface * snapshot, int x, + int y, SDL_Rect * update_rect); void fisheye_shutdown(magic_api * api); -void fisheye_draw(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +void fisheye_draw(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); void fisheye_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void fisheye_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void fisheye_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void fisheye_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void fisheye_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); +void fisheye_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void fisheye_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int fisheye_modes(magic_api * api, int which); // Housekeeping functions void fisheye_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect); + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect); Uint32 fisheye_api_version(void) { return (TP_MAGIC_API_VERSION); } -void fisheye_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, +void fisheye_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) { @@ -77,7 +84,8 @@ int fisheye_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/fisheye.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/fisheye.ogg", + api->data_directory); fisheye_snd = Mix_LoadWAV(fname); return (1); @@ -92,34 +100,45 @@ SDL_Surface *fisheye_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/fisheye.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/fisheye.png", + api->data_directory); return (IMG_Load(fname)); } -char *fisheye_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *fisheye_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return strdup(gettext_noop("Fisheye")); } -int fisheye_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int fisheye_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_DISTORTS; } -char *fisheye_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *fisheye_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED) { - return strdup(gettext_noop("Click on part of your picture to create a fisheye effect.")); + return + strdup(gettext_noop + ("Click on part of your picture to create a fisheye effect.")); } -int fisheye_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int fisheye_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void fisheye_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * snapshot ATTRIBUTE_UNUSED, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) +void fisheye_release(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED, + SDL_Surface * snapshot ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, + SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -131,8 +150,8 @@ void fisheye_shutdown(magic_api * api ATTRIBUTE_UNUSED) // do-fisheye -void fisheye_draw(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, - int x, int y) +void fisheye_draw(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { magic_api *api = (magic_api *) ptr; @@ -147,13 +166,15 @@ void fisheye_draw(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, S last_x = x; last_y = y; - oryg = SDL_CreateRGBSurface(SDL_SWSURFACE, 80, 80, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, - canvas->format->Amask); + oryg = + SDL_CreateRGBSurface(SDL_SWSURFACE, 80, 80, canvas->format->BitsPerPixel, + canvas->format->Rmask, canvas->format->Gmask, + canvas->format->Bmask, canvas->format->Amask); - output = SDL_CreateRGBSurface(SDL_SWSURFACE, 80, 80, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, - canvas->format->Amask); + output = + SDL_CreateRGBSurface(SDL_SWSURFACE, 80, 80, canvas->format->BitsPerPixel, + canvas->format->Rmask, canvas->format->Gmask, + canvas->format->Bmask, canvas->format->Amask); rect.x = x - 40; rect.y = y - 40; @@ -163,79 +184,85 @@ void fisheye_draw(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, S //do vertical fisheye for (i = 0; i < 40; i++) - { - temp_src = SDL_CreateRGBSurface(SDL_SWSURFACE, 1, 80, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, - canvas->format->Amask); + { + temp_src = + SDL_CreateRGBSurface(SDL_SWSURFACE, 1, 80, canvas->format->BitsPerPixel, + canvas->format->Rmask, canvas->format->Gmask, + canvas->format->Bmask, canvas->format->Amask); - //let's take a smooth bar of scaled bitmap and copy it to temp - //left side first - rect.x = i; - rect.y = 0; - rect.w = 1; + //let's take a smooth bar of scaled bitmap and copy it to temp + //left side first + rect.x = i; + rect.y = 0; + rect.w = 1; - SDL_BlitSurface(oryg, &rect, temp_src, NULL); //this bar is copied to temp_src + SDL_BlitSurface(oryg, &rect, temp_src, NULL); //this bar is copied to temp_src - temp_dest = SDL_CreateRGBSurface(SDL_SWSURFACE, 1, 80 + 2 * i, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, - canvas->format->Amask); + temp_dest = + SDL_CreateRGBSurface(SDL_SWSURFACE, 1, 80 + 2 * i, + canvas->format->BitsPerPixel, + canvas->format->Rmask, canvas->format->Gmask, + canvas->format->Bmask, canvas->format->Amask); - temp_dest = api->scale(temp_src, 1, 80 + 2 * i, 0); //temp_dest stores scaled temp_src + temp_dest = api->scale(temp_src, 1, 80 + 2 * i, 0); //temp_dest stores scaled temp_src - temp_rect.x = 0; - temp_rect.y = i; - temp_rect.w = 1; - temp_rect.h = 80; + temp_rect.x = 0; + temp_rect.y = i; + temp_rect.w = 1; + temp_rect.h = 80; - SDL_BlitSurface(temp_dest, &temp_rect, output, &rect); //let's copy it to output + SDL_BlitSurface(temp_dest, &temp_rect, output, &rect); //let's copy it to output - //right side then + //right side then - rect.x = 79 - i; + rect.x = 79 - i; - SDL_BlitSurface(oryg, &rect, temp_src, NULL); //this bar is copied to temp_src //OK + SDL_BlitSurface(oryg, &rect, temp_src, NULL); //this bar is copied to temp_src //OK - temp_dest = api->scale(temp_src, 1, 80 + 2 * i, 0); //temp_dest stores scaled temp_src + temp_dest = api->scale(temp_src, 1, 80 + 2 * i, 0); //temp_dest stores scaled temp_src - SDL_BlitSurface(temp_dest, &temp_rect, output, &rect); //let's copy it to output - } + SDL_BlitSurface(temp_dest, &temp_rect, output, &rect); //let's copy it to output + } //do horizontal fisheye for (i = 0; i < 40; i++) - { - temp_src = SDL_CreateRGBSurface(SDL_SWSURFACE, 80, 1, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, - canvas->format->Amask); + { + temp_src = + SDL_CreateRGBSurface(SDL_SWSURFACE, 80, 1, canvas->format->BitsPerPixel, + canvas->format->Rmask, canvas->format->Gmask, + canvas->format->Bmask, canvas->format->Amask); - temp_dest = SDL_CreateRGBSurface(SDL_SWSURFACE, 80 + 2 * i, 1, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, - canvas->format->Amask); + temp_dest = + SDL_CreateRGBSurface(SDL_SWSURFACE, 80 + 2 * i, 1, + canvas->format->BitsPerPixel, + canvas->format->Rmask, canvas->format->Gmask, + canvas->format->Bmask, canvas->format->Amask); - //upper side first - rect.x = 0; - rect.y = i; - rect.w = 80; - rect.h = 1; + //upper side first + rect.x = 0; + rect.y = i; + rect.w = 80; + rect.h = 1; - temp_rect.x = i; - temp_rect.y = 0; - temp_rect.w = 80; - temp_rect.h = 1; + temp_rect.x = i; + temp_rect.y = 0; + temp_rect.w = 80; + temp_rect.h = 1; - SDL_BlitSurface(output, &rect, temp_src, NULL); + SDL_BlitSurface(output, &rect, temp_src, NULL); - temp_dest = api->scale(temp_src, 80 + 2 * i, 1, 0); + temp_dest = api->scale(temp_src, 80 + 2 * i, 1, 0); - SDL_BlitSurface(temp_dest, &temp_rect, output, &rect); + SDL_BlitSurface(temp_dest, &temp_rect, output, &rect); - //lower side then + //lower side then - rect.y = 79 - i; - SDL_BlitSurface(output, &rect, temp_src, NULL); + rect.y = 79 - i; + SDL_BlitSurface(output, &rect, temp_src, NULL); - temp_dest = api->scale(temp_src, 80 + 2 * i, 1, 0); - SDL_BlitSurface(temp_dest, &temp_rect, output, &rect); - } + temp_dest = api->scale(temp_src, 80 + 2 * i, 1, 0); + SDL_BlitSurface(temp_dest, &temp_rect, output, &rect); + } rect.x = x - 40; rect.y = y - 40; @@ -247,7 +274,8 @@ void fisheye_draw(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, S for (xx = x - 40; xx < x + 40; xx++) if (api->in_circle(xx - x, yy - y, 40)) - api->putpixel(canvas, xx, yy, api->getpixel(output, xx + 40 - x, yy + 40 - y)); + api->putpixel(canvas, xx, yy, + api->getpixel(output, xx + 40 - x, yy + 40 - y)); SDL_FreeSurface(oryg); @@ -259,7 +287,8 @@ void fisheye_draw(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, S } void fisheye_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect) + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect) { api->line(api, which, canvas, snapshot, ox, oy, x, y, 1, fisheye_draw); @@ -270,26 +299,30 @@ void fisheye_drag(magic_api * api, int which, SDL_Surface * canvas, } void fisheye_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) { last_x = -80; /* A value that will be beyond any clicked position */ last_y = -80; fisheye_drag(api, which, canvas, last, x, y, x, y, update_rect); } -void fisheye_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void fisheye_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void fisheye_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void fisheye_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -int fisheye_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int fisheye_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_PAINT); } diff --git a/magic/src/flower.c b/magic/src/flower.c index 7f06fae9f..44b8602b0 100644 --- a/magic/src/flower.c +++ b/magic/src/flower.c @@ -51,7 +51,8 @@ static Uint8 flower_r, flower_g, flower_b; static int flower_min_x, flower_max_x, flower_bottom_x, flower_bottom_y; static int flower_side_first; static int flower_side_decided; -static SDL_Surface *flower_base, *flower_leaf, *flower_petals, *flower_petals_colorized; +static SDL_Surface *flower_base, *flower_leaf, *flower_petals, + *flower_petals_colorized; /* Local function prototypes: */ @@ -61,9 +62,11 @@ typedef struct } Point2D; static void flower_drawbase(magic_api * api, SDL_Surface * canvas); -static void flower_drawflower(magic_api * api, SDL_Surface * canvas, int x, int y); +static void flower_drawflower(magic_api * api, SDL_Surface * canvas, int x, + int y); static Point2D flower_PointOnCubicBezier(Point2D * cp, float t); -static void flower_ComputeBezier(Point2D * cp, int numberOfPoints, Point2D * curve); +static void flower_ComputeBezier(Point2D * cp, int numberOfPoints, + Point2D * curve); static void flower_colorize_petals(magic_api * api); Uint32 flower_api_version(void); int flower_init(magic_api * api); @@ -72,25 +75,32 @@ SDL_Surface *flower_get_icon(magic_api * api, int which); char *flower_get_name(magic_api * api, int which); int flower_get_group(magic_api * api, int which); char *flower_get_description(magic_api * api, int which, int mode); -static void flower_predrag(magic_api * api, SDL_Surface * canvas, SDL_Surface * last, int ox, int oy, int x, int y); +static void flower_predrag(magic_api * api, SDL_Surface * canvas, + SDL_Surface * last, int ox, int oy, int x, int y); void flower_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void flower_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void flower_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -static void flower_drawflower(magic_api * api, SDL_Surface * canvas, int x, int y); + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void flower_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); +void flower_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); +static void flower_drawflower(magic_api * api, SDL_Surface * canvas, int x, + int y); static void flower_drawbase(magic_api * api, SDL_Surface * canvas); static void flower_drawstalk(magic_api * api, SDL_Surface * canvas, - 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); void flower_shutdown(magic_api * api); void flower_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int flower_requires_colors(magic_api * api, int which); static Point2D flower_PointOnCubicBezier(Point2D * cp, float t); -static void flower_ComputeBezier(Point2D * cp, int numberOfPoints, Point2D * curve); +static void flower_ComputeBezier(Point2D * cp, int numberOfPoints, + Point2D * curve); static void flower_colorize_petals(magic_api * api); -void flower_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void flower_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void flower_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void flower_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int flower_modes(magic_api * api, int which); @@ -106,19 +116,24 @@ int flower_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/flower_click.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/flower_click.ogg", + api->data_directory); flower_click_snd = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%ssounds/magic/flower_release.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/flower_release.ogg", + api->data_directory); flower_release_snd = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%simages/magic/flower_base.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/flower_base.png", + api->data_directory); flower_base = IMG_Load(fname); - snprintf(fname, sizeof(fname), "%simages/magic/flower_leaf.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/flower_leaf.png", + api->data_directory); flower_leaf = IMG_Load(fname); - snprintf(fname, sizeof(fname), "%simages/magic/flower_petals.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/flower_petals.png", + api->data_directory); flower_petals = IMG_Load(fname); return (1); @@ -135,32 +150,41 @@ SDL_Surface *flower_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/flower.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/flower.png", + api->data_directory); return (IMG_Load(fname)); } // Return our names, localized: -char *flower_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *flower_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (strdup(gettext_noop("Flower"))); } // Return our groups: -int flower_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int flower_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_ARTISTIC; } // Return our descriptions, localized: -char *flower_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *flower_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 flower stalk. Let go to finish the flower."))); + return (strdup + (gettext_noop + ("Click and drag to draw a flower stalk. Let go to finish the flower."))); } // Affect the canvas on drag: -static void flower_predrag(magic_api * api ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED, - SDL_Surface * last ATTRIBUTE_UNUSED, int ox, int oy, int x, int y) +static void flower_predrag(magic_api * api ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED, + SDL_Surface * last ATTRIBUTE_UNUSED, int ox, + int oy, int x, int y) { if (x < flower_min_x) flower_min_x = x; @@ -179,22 +203,23 @@ static void flower_predrag(magic_api * api ATTRIBUTE_UNUSED, SDL_Surface * canva // Determine which way to bend first: // if (flower_side_decided == 0) + { + if (x < flower_bottom_x - 10) { - if (x < flower_bottom_x - 10) - { - flower_side_first = SIDE_LEFT; - flower_side_decided = 1; - } - else if (x > flower_bottom_x + 10) - { - flower_side_first = SIDE_RIGHT; - flower_side_decided = 1; - } + flower_side_first = SIDE_LEFT; + flower_side_decided = 1; } + else if (x > flower_bottom_x + 10) + { + flower_side_first = SIDE_RIGHT; + flower_side_decided = 1; + } + } } -void flower_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) +void flower_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) { flower_predrag(api, canvas, last, ox, oy, x, y); @@ -207,7 +232,8 @@ void flower_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * canv /* Draw the base and the stalk (low-quality) for now: */ flower_drawstalk(api, canvas, - x, y, flower_min_x, flower_max_x, flower_bottom_x, flower_bottom_y, !(api->button_down())); + x, y, flower_min_x, flower_max_x, flower_bottom_x, + flower_bottom_y, !(api->button_down())); flower_drawbase(api, canvas); @@ -219,7 +245,8 @@ void flower_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * canv // Affect the canvas on click: void flower_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) { flower_min_x = x; flower_max_x = x; @@ -236,7 +263,8 @@ void flower_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, // Affect the canvas on release: void flower_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 flower be too low compared to base: */ @@ -256,7 +284,8 @@ void flower_release(magic_api * api, int which ATTRIBUTE_UNUSED, /* Draw high-quality stalk, and flower: */ - flower_drawstalk(api, canvas, x, y, flower_min_x, flower_max_x, flower_bottom_x, flower_bottom_y, 1); + flower_drawstalk(api, canvas, x, y, flower_min_x, flower_max_x, + flower_bottom_x, flower_bottom_y, 1); flower_drawflower(api, canvas, x, y); @@ -272,7 +301,8 @@ void flower_release(magic_api * api, int which ATTRIBUTE_UNUSED, } -static void flower_drawflower(magic_api * api ATTRIBUTE_UNUSED, SDL_Surface * canvas, int x, int y) +static void flower_drawflower(magic_api * api ATTRIBUTE_UNUSED, + SDL_Surface * canvas, int x, int y) { SDL_Rect dest; @@ -282,7 +312,8 @@ static void flower_drawflower(magic_api * api ATTRIBUTE_UNUSED, SDL_Surface * ca SDL_BlitSurface(flower_petals_colorized, NULL, canvas, &dest); } -static void flower_drawbase(magic_api * api ATTRIBUTE_UNUSED, SDL_Surface * canvas) +static void flower_drawbase(magic_api * api ATTRIBUTE_UNUSED, + SDL_Surface * canvas) { SDL_Rect dest; @@ -292,8 +323,10 @@ static void flower_drawbase(magic_api * api ATTRIBUTE_UNUSED, SDL_Surface * canv SDL_BlitSurface(flower_base, NULL, canvas, &dest); } -static void flower_drawstalk(magic_api * api ATTRIBUTE_UNUSED, SDL_Surface * canvas, - int top_x, int top_y, int minx, int maxx, int bottom_x, int bottom_y, int final) +static void flower_drawstalk(magic_api * api ATTRIBUTE_UNUSED, + SDL_Surface * canvas, int top_x, int top_y, + int minx, int maxx, int bottom_x, int bottom_y, + int final) { Point2D control_points[4]; Point2D *curve; @@ -310,15 +343,15 @@ static void flower_drawstalk(magic_api * api ATTRIBUTE_UNUSED, SDL_Surface * can control_points[0].y = top_y; if (flower_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; @@ -339,126 +372,127 @@ static void flower_drawstalk(magic_api * api ATTRIBUTE_UNUSED, SDL_Surface * can /* Draw the curve: */ for (i = 0; i < n_points - 1; i++) + { + if (final == 0) { - if (final == 0) - { - dest.x = curve[i].x; - dest.y = curve[i].y; - dest.w = 2; - dest.h = 2; - } - else - { - left = min(curve[i].x, curve[i + 1].x); - right = max(curve[i].x, curve[i + 1].x); - - dest.x = left; - dest.y = curve[i].y; - dest.w = right - left + 1; - dest.h = 2; - } - - SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, 0, 128, 0)); - - - /* When we're done (final render), we can add some random leaves: */ - - if (final && i > 32 && i < n_points - 32 && (i % 16) == 0 && (rand() % 5) > 0) - { - /* Check for hard left/right angles: */ - - side = -1; - - if (curve[i - 2].x - curve[i + 2].x > 5) - { - /* Hard lower-left-to-upper-right (/), - stick either a left-upward or right-downward facing leaf */ - - if (rand() % 10 < 5) - side = LEAFSIDE_LEFT_UP; - else - side = LEAFSIDE_RIGHT_DOWN; - } - else if (curve[i - 2].x - curve[i + 2].x < -5) - { - /* Hard lower-right-to-upper-left (\) - stick either a right-upward or left-downward facing leaf */ - - if (rand() % 10 < 5) - side = LEAFSIDE_LEFT_DOWN; - else - side = LEAFSIDE_RIGHT_UP; - } - else if (fabs(curve[i - 2].x - curve[i + 2].x) < 5) - { - /* Mostly up; stick left- or right-downward: */ - - if (rand() % 10 < 5) - side = LEAFSIDE_LEFT_DOWN; - else - side = LEAFSIDE_RIGHT_DOWN; - } - - - /* Draw the appropriately-oriented leaf, if any: */ - - if (side == LEAFSIDE_RIGHT_DOWN) - { - dest.x = curve[i].x; - dest.y = curve[i].y; - - SDL_BlitSurface(flower_leaf, NULL, canvas, &dest); - } - else if (side == LEAFSIDE_LEFT_DOWN) - { - for (xx = 0; xx < flower_leaf->w; xx++) - { - src.x = xx; - src.y = 0; - src.w = 1; - src.h = flower_leaf->h; - - dest.x = curve[i].x - xx; - dest.y = curve[i].y; - - SDL_BlitSurface(flower_leaf, &src, canvas, &dest); - } - } - else if (side == LEAFSIDE_RIGHT_UP) - { - for (yy = 0; yy < flower_leaf->h; yy++) - { - src.x = 0; - src.y = yy; - src.w = flower_leaf->w; - src.h = 1; - - dest.x = curve[i].x; - dest.y = curve[i].y - yy; - - SDL_BlitSurface(flower_leaf, &src, canvas, &dest); - } - } - else if (side == LEAFSIDE_LEFT_UP) - { - for (xx = 0; xx < flower_leaf->w; xx++) - { - for (yy = 0; yy < flower_leaf->h; yy++) - { - src.x = xx; - src.y = yy; - src.w = 1; - src.h = 1; - - dest.x = curve[i].x - xx; - dest.y = curve[i].y - yy; - - SDL_BlitSurface(flower_leaf, &src, canvas, &dest); - } - } - } - } + dest.x = curve[i].x; + dest.y = curve[i].y; + dest.w = 2; + dest.h = 2; } + else + { + left = min(curve[i].x, curve[i + 1].x); + right = max(curve[i].x, curve[i + 1].x); + + dest.x = left; + dest.y = curve[i].y; + dest.w = right - left + 1; + dest.h = 2; + } + + SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, 0, 128, 0)); + + + /* When we're done (final render), we can add some random leaves: */ + + if (final && i > 32 && i < n_points - 32 && (i % 16) == 0 + && (rand() % 5) > 0) + { + /* Check for hard left/right angles: */ + + side = -1; + + if (curve[i - 2].x - curve[i + 2].x > 5) + { + /* Hard lower-left-to-upper-right (/), + stick either a left-upward or right-downward facing leaf */ + + if (rand() % 10 < 5) + side = LEAFSIDE_LEFT_UP; + else + side = LEAFSIDE_RIGHT_DOWN; + } + else if (curve[i - 2].x - curve[i + 2].x < -5) + { + /* Hard lower-right-to-upper-left (\) + stick either a right-upward or left-downward facing leaf */ + + if (rand() % 10 < 5) + side = LEAFSIDE_LEFT_DOWN; + else + side = LEAFSIDE_RIGHT_UP; + } + else if (fabs(curve[i - 2].x - curve[i + 2].x) < 5) + { + /* Mostly up; stick left- or right-downward: */ + + if (rand() % 10 < 5) + side = LEAFSIDE_LEFT_DOWN; + else + side = LEAFSIDE_RIGHT_DOWN; + } + + + /* Draw the appropriately-oriented leaf, if any: */ + + if (side == LEAFSIDE_RIGHT_DOWN) + { + dest.x = curve[i].x; + dest.y = curve[i].y; + + SDL_BlitSurface(flower_leaf, NULL, canvas, &dest); + } + else if (side == LEAFSIDE_LEFT_DOWN) + { + for (xx = 0; xx < flower_leaf->w; xx++) + { + src.x = xx; + src.y = 0; + src.w = 1; + src.h = flower_leaf->h; + + dest.x = curve[i].x - xx; + dest.y = curve[i].y; + + SDL_BlitSurface(flower_leaf, &src, canvas, &dest); + } + } + else if (side == LEAFSIDE_RIGHT_UP) + { + for (yy = 0; yy < flower_leaf->h; yy++) + { + src.x = 0; + src.y = yy; + src.w = flower_leaf->w; + src.h = 1; + + dest.x = curve[i].x; + dest.y = curve[i].y - yy; + + SDL_BlitSurface(flower_leaf, &src, canvas, &dest); + } + } + else if (side == LEAFSIDE_LEFT_UP) + { + for (xx = 0; xx < flower_leaf->w; xx++) + { + for (yy = 0; yy < flower_leaf->h; yy++) + { + src.x = xx; + src.y = yy; + src.w = 1; + src.h = 1; + + dest.x = curve[i].x - xx; + dest.y = curve[i].y - yy; + + SDL_BlitSurface(flower_leaf, &src, canvas, &dest); + } + } + } + } + } free(curve); } @@ -492,7 +526,8 @@ void flower_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b) } // Use colors: -int flower_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int flower_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 1; } @@ -547,7 +582,8 @@ static Point2D flower_PointOnCubicBezier(Point2D * cp, float t) */ -static void flower_ComputeBezier(Point2D * cp, int numberOfPoints, Point2D * curve) +static void flower_ComputeBezier(Point2D * cp, int numberOfPoints, + Point2D * curve) { float dt; int i; @@ -570,7 +606,9 @@ static void flower_colorize_petals(magic_api * api) /* Create a surface to render into: */ - amask = ~(flower_petals->format->Rmask | flower_petals->format->Gmask | flower_petals->format->Bmask); + amask = + ~(flower_petals->format->Rmask | flower_petals->format-> + Gmask | flower_petals->format->Bmask); flower_petals_colorized = SDL_CreateRGBSurface(SDL_SWSURFACE, @@ -578,7 +616,8 @@ static void flower_colorize_petals(magic_api * api) flower_petals->h, flower_petals->format->BitsPerPixel, flower_petals->format->Rmask, - flower_petals->format->Gmask, flower_petals->format->Bmask, amask); + flower_petals->format->Gmask, + flower_petals->format->Bmask, amask); /* Render the new petals: */ @@ -586,32 +625,38 @@ static void flower_colorize_petals(magic_api * api) SDL_LockSurface(flower_petals_colorized); for (y = 0; y < flower_petals->h; y++) + { + for (x = 0; x < flower_petals->w; x++) { - for (x = 0; x < flower_petals->w; x++) - { - SDL_GetRGBA(api->getpixel(flower_petals, x, y), flower_petals->format, &r, &g, &b, &a); + SDL_GetRGBA(api->getpixel(flower_petals, x, y), flower_petals->format, + &r, &g, &b, &a); - api->putpixel(flower_petals_colorized, x, y, - SDL_MapRGBA(flower_petals_colorized->format, flower_r, flower_g, flower_b, a)); + api->putpixel(flower_petals_colorized, x, y, + SDL_MapRGBA(flower_petals_colorized->format, flower_r, + flower_g, flower_b, a)); - if (api->in_circle((x - flower_petals->w / 2), (y - flower_petals->h / 2), 8)) - { - api->putpixel(flower_petals_colorized, x, y, - SDL_MapRGBA(flower_petals_colorized->format, 0xFF, 0xFF, 0x00, a)); - } - } + if (api->in_circle + ((x - flower_petals->w / 2), (y - flower_petals->h / 2), 8)) + { + api->putpixel(flower_petals_colorized, x, y, + SDL_MapRGBA(flower_petals_colorized->format, 0xFF, 0xFF, + 0x00, a)); + } } + } SDL_UnlockSurface(flower_petals_colorized); SDL_UnlockSurface(flower_petals); } -void flower_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void flower_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void flower_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void flower_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } diff --git a/magic/src/foam.c b/magic/src/foam.c index 7b032bcd9..883553102 100644 --- a/magic/src/foam.c +++ b/magic/src/foam.c @@ -46,16 +46,22 @@ Uint32 foam_api_version(void); int foam_init(magic_api * api); char *foam_get_description(magic_api * api, int which, int mode); void foam_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 foam_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void foam_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void foam_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); SDL_Surface *foam_get_icon(magic_api * api, int which); -char *foam_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED); -int foam_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED); -void foam_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void foam_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +char *foam_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED); +int foam_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED); +void foam_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void foam_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); void foam_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); void foam_shutdown(magic_api * api); int foam_get_tool_count(magic_api * api); @@ -77,16 +83,26 @@ int foam_init(magic_api * api) char fname[1024]; SDL_Surface *foam_data; - snprintf(fname, sizeof(fname), "%ssounds/magic/foam.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/foam.ogg", + api->data_directory); foam_snd = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%simages/magic/foam_data.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/foam_data.png", + api->data_directory); foam_data = IMG_Load(fname); - foam_7 = api->scale(foam_data, ((api->canvas_w / FOAM_PROP) * 4) / 4, ((api->canvas_h / FOAM_PROP) * 4) / 4, 0); - foam_5 = api->scale(foam_data, ((api->canvas_w / FOAM_PROP) * 3) / 4, ((api->canvas_h / FOAM_PROP) * 3) / 4, 0); - foam_3 = api->scale(foam_data, ((api->canvas_w / FOAM_PROP) * 2) / 4, ((api->canvas_h / FOAM_PROP) * 2) / 4, 0); - foam_1 = api->scale(foam_data, ((api->canvas_w / FOAM_PROP) * 1) / 4, ((api->canvas_h / FOAM_PROP) * 1) / 4, 0); + foam_7 = + api->scale(foam_data, ((api->canvas_w / FOAM_PROP) * 4) / 4, + ((api->canvas_h / FOAM_PROP) * 4) / 4, 0); + foam_5 = + api->scale(foam_data, ((api->canvas_w / FOAM_PROP) * 3) / 4, + ((api->canvas_h / FOAM_PROP) * 3) / 4, 0); + foam_3 = + api->scale(foam_data, ((api->canvas_w / FOAM_PROP) * 2) / 4, + ((api->canvas_h / FOAM_PROP) * 2) / 4, 0); + foam_1 = + api->scale(foam_data, ((api->canvas_w / FOAM_PROP) * 1) / 4, + ((api->canvas_h / FOAM_PROP) * 1) / 4, 0); SDL_FreeSurface(foam_data); @@ -104,32 +120,40 @@ SDL_Surface *foam_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/foam.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/foam.png", + api->data_directory); return (IMG_Load(fname)); } // Return our names, localized: -char *foam_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *foam_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (strdup(gettext_noop("Foam"))); } // Return our groups -int foam_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int foam_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PAINTING; } // Return our descriptions, localized: -char *foam_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *foam_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED) { - return (strdup(gettext_noop("Click and drag the mouse to cover an area with foamy bubbles."))); + return (strdup + (gettext_noop + ("Click and drag the mouse to cover an area with foamy bubbles."))); } // Do the effect: -static void do_foam(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED, +static void do_foam(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { magic_api *api = (magic_api *) ptr; @@ -138,46 +162,47 @@ static void do_foam(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas /* SDL_Rect dest; */ for (yy = -FOAM_RADIUS; yy < FOAM_RADIUS; yy++) + { + for (xx = -FOAM_RADIUS; xx < FOAM_RADIUS; xx++) { - for (xx = -FOAM_RADIUS; xx < FOAM_RADIUS; xx++) - { - if (api->in_circle(xx, yy, FOAM_RADIUS)) - { - nx = (x / FOAM_PROP) + xx; - ny = (y / FOAM_PROP) + yy; + if (api->in_circle(xx, yy, FOAM_RADIUS)) + { + nx = (x / FOAM_PROP) + xx; + ny = (y / FOAM_PROP) + yy; - if (nx >= 0 && ny >= 0 && nx < foam_mask_w && ny < foam_mask_h) - { - foam_mask[ny * foam_mask_w + nx] = 1; - } - } + if (nx >= 0 && ny >= 0 && nx < foam_mask_w && ny < foam_mask_h) + { + foam_mask[ny * foam_mask_w + nx] = 1; } + } } + } } // Affect the canvas on drag: void foam_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) { - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_foam); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, do_foam); foam_release(api, which, canvas, last, x, y, update_rect); /* FIXME */ if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = 0; update_rect->y = 0; @@ -188,20 +213,21 @@ void foam_drag(magic_api * api, int which, SDL_Surface * canvas, } // Affect the canvas on click: -void foam_click(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * last, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect) +void foam_click(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas, + SDL_Surface * last, int x ATTRIBUTE_UNUSED, + int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect) { int i; if (foam_mask == NULL) - { - foam_mask_w = canvas->w / FOAM_PROP; - foam_mask_h = canvas->h / FOAM_PROP; + { + foam_mask_w = canvas->w / FOAM_PROP; + foam_mask_h = canvas->h / FOAM_PROP; - foam_mask = (int *)malloc(sizeof(int) * (foam_mask_w * foam_mask_h)); - foam_mask_tmp = (int *)malloc(sizeof(int) * (foam_mask_w * foam_mask_h)); - } + foam_mask = (int *) malloc(sizeof(int) * (foam_mask_w * foam_mask_h)); + foam_mask_tmp = (int *) malloc(sizeof(int) * (foam_mask_w * foam_mask_h)); + } for (i = 0; i < foam_mask_w * foam_mask_h; i++) foam_mask[i] = 0; @@ -217,24 +243,25 @@ static int foam_mask_test(int r, int x, int y) tot = 0; for (yy = 0; yy < r; yy++) + { + for (xx = 0; xx < r; xx++) { - for (xx = 0; xx < r; xx++) - { - if (x + xx < foam_mask_w && y + yy < foam_mask_h) - { - bub_r = foam_mask[((y + yy) * foam_mask_w) + (x + xx)]; - tot = tot + bub_r; - } - } + if (x + xx < foam_mask_w && y + yy < foam_mask_h) + { + bub_r = foam_mask[((y + yy) * foam_mask_w) + (x + xx)]; + tot = tot + bub_r; + } } + } return (tot); } // Affect the canvas on release: -void foam_release(magic_api * api ATTRIBUTE_UNUSED ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * last, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect) +void foam_release(magic_api * api ATTRIBUTE_UNUSED ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, + SDL_Surface * last, int x ATTRIBUTE_UNUSED, + int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect) { int xx, yy; int changes, max_iters; @@ -244,165 +271,167 @@ void foam_release(magic_api * api ATTRIBUTE_UNUSED ATTRIBUTE_UNUSED, int which A SDL_BlitSurface(last, NULL, canvas, NULL); - memcpy(foam_mask_tmp, foam_mask, (sizeof(int) * (foam_mask_w * foam_mask_h))); + memcpy(foam_mask_tmp, foam_mask, + (sizeof(int) * (foam_mask_w * foam_mask_h))); max_iters = 2; do + { + changes = 0; + max_iters--; + + for (yy = 0; yy < foam_mask_h - 7; yy++) { - changes = 0; - max_iters--; - - for (yy = 0; yy < foam_mask_h - 7; yy++) + for (xx = 0; xx < foam_mask_w - 7; xx++) + { + if (foam_mask_test(7, xx, yy) >= 40) { - for (xx = 0; xx < foam_mask_w - 7; xx++) - { - if (foam_mask_test(7, xx, yy) >= 40) - { - foam_mask[((yy + 0) * foam_mask_w) + (xx + 0)] = 7; - foam_mask[((yy + 0) * foam_mask_w) + (xx + 1)] = 0; - foam_mask[((yy + 0) * foam_mask_w) + (xx + 2)] = 1; - foam_mask[((yy + 0) * foam_mask_w) + (xx + 3)] = 0; - foam_mask[((yy + 0) * foam_mask_w) + (xx + 4)] = 1; - foam_mask[((yy + 0) * foam_mask_w) + (xx + 5)] = 2; - foam_mask[((yy + 0) * foam_mask_w) + (xx + 6)] = 0; + foam_mask[((yy + 0) * foam_mask_w) + (xx + 0)] = 7; + foam_mask[((yy + 0) * foam_mask_w) + (xx + 1)] = 0; + foam_mask[((yy + 0) * foam_mask_w) + (xx + 2)] = 1; + foam_mask[((yy + 0) * foam_mask_w) + (xx + 3)] = 0; + foam_mask[((yy + 0) * foam_mask_w) + (xx + 4)] = 1; + foam_mask[((yy + 0) * foam_mask_w) + (xx + 5)] = 2; + foam_mask[((yy + 0) * foam_mask_w) + (xx + 6)] = 0; - foam_mask[((yy + 1) * foam_mask_w) + (xx + 0)] = 0; - foam_mask[((yy + 1) * foam_mask_w) + (xx + 1)] = 1; - foam_mask[((yy + 1) * foam_mask_w) + (xx + 2)] = 0; - foam_mask[((yy + 1) * foam_mask_w) + (xx + 3)] = 0; - foam_mask[((yy + 1) * foam_mask_w) + (xx + 4)] = 0; - foam_mask[((yy + 1) * foam_mask_w) + (xx + 5)] = 2; - foam_mask[((yy + 1) * foam_mask_w) + (xx + 6)] = 0; + foam_mask[((yy + 1) * foam_mask_w) + (xx + 0)] = 0; + foam_mask[((yy + 1) * foam_mask_w) + (xx + 1)] = 1; + foam_mask[((yy + 1) * foam_mask_w) + (xx + 2)] = 0; + foam_mask[((yy + 1) * foam_mask_w) + (xx + 3)] = 0; + foam_mask[((yy + 1) * foam_mask_w) + (xx + 4)] = 0; + foam_mask[((yy + 1) * foam_mask_w) + (xx + 5)] = 2; + foam_mask[((yy + 1) * foam_mask_w) + (xx + 6)] = 0; - foam_mask[((yy + 2) * foam_mask_w) + (xx + 0)] = 1; - foam_mask[((yy + 2) * foam_mask_w) + (xx + 1)] = 0; - foam_mask[((yy + 2) * foam_mask_w) + (xx + 2)] = 0; - foam_mask[((yy + 2) * foam_mask_w) + (xx + 3)] = 0; - foam_mask[((yy + 2) * foam_mask_w) + (xx + 4)] = 0; - foam_mask[((yy + 2) * foam_mask_w) + (xx + 5)] = 0; - foam_mask[((yy + 2) * foam_mask_w) + (xx + 6)] = 1; + foam_mask[((yy + 2) * foam_mask_w) + (xx + 0)] = 1; + foam_mask[((yy + 2) * foam_mask_w) + (xx + 1)] = 0; + foam_mask[((yy + 2) * foam_mask_w) + (xx + 2)] = 0; + foam_mask[((yy + 2) * foam_mask_w) + (xx + 3)] = 0; + foam_mask[((yy + 2) * foam_mask_w) + (xx + 4)] = 0; + foam_mask[((yy + 2) * foam_mask_w) + (xx + 5)] = 0; + foam_mask[((yy + 2) * foam_mask_w) + (xx + 6)] = 1; - foam_mask[((yy + 3) * foam_mask_w) + (xx + 0)] = 0; - foam_mask[((yy + 3) * foam_mask_w) + (xx + 1)] = 1; - foam_mask[((yy + 3) * foam_mask_w) + (xx + 2)] = 0; - foam_mask[((yy + 3) * foam_mask_w) + (xx + 3)] = 0; - foam_mask[((yy + 3) * foam_mask_w) + (xx + 4)] = 0; - foam_mask[((yy + 3) * foam_mask_w) + (xx + 5)] = 0; - foam_mask[((yy + 3) * foam_mask_w) + (xx + 6)] = 0; + foam_mask[((yy + 3) * foam_mask_w) + (xx + 0)] = 0; + foam_mask[((yy + 3) * foam_mask_w) + (xx + 1)] = 1; + foam_mask[((yy + 3) * foam_mask_w) + (xx + 2)] = 0; + foam_mask[((yy + 3) * foam_mask_w) + (xx + 3)] = 0; + foam_mask[((yy + 3) * foam_mask_w) + (xx + 4)] = 0; + foam_mask[((yy + 3) * foam_mask_w) + (xx + 5)] = 0; + foam_mask[((yy + 3) * foam_mask_w) + (xx + 6)] = 0; - foam_mask[((yy + 4) * foam_mask_w) + (xx + 0)] = 1; - foam_mask[((yy + 4) * foam_mask_w) + (xx + 1)] = 0; - foam_mask[((yy + 4) * foam_mask_w) + (xx + 2)] = 0; - foam_mask[((yy + 4) * foam_mask_w) + (xx + 3)] = 0; - foam_mask[((yy + 4) * foam_mask_w) + (xx + 4)] = 0; - foam_mask[((yy + 4) * foam_mask_w) + (xx + 5)] = 0; - foam_mask[((yy + 4) * foam_mask_w) + (xx + 6)] = 1; + foam_mask[((yy + 4) * foam_mask_w) + (xx + 0)] = 1; + foam_mask[((yy + 4) * foam_mask_w) + (xx + 1)] = 0; + foam_mask[((yy + 4) * foam_mask_w) + (xx + 2)] = 0; + foam_mask[((yy + 4) * foam_mask_w) + (xx + 3)] = 0; + foam_mask[((yy + 4) * foam_mask_w) + (xx + 4)] = 0; + foam_mask[((yy + 4) * foam_mask_w) + (xx + 5)] = 0; + foam_mask[((yy + 4) * foam_mask_w) + (xx + 6)] = 1; - foam_mask[((yy + 5) * foam_mask_w) + (xx + 0)] = 2; - foam_mask[((yy + 5) * foam_mask_w) + (xx + 1)] = 0; - foam_mask[((yy + 5) * foam_mask_w) + (xx + 2)] = 0; - foam_mask[((yy + 5) * foam_mask_w) + (xx + 3)] = 7; - foam_mask[((yy + 5) * foam_mask_w) + (xx + 4)] = 0; - foam_mask[((yy + 5) * foam_mask_w) + (xx + 5)] = 3; - foam_mask[((yy + 5) * foam_mask_w) + (xx + 6)] = 0; + foam_mask[((yy + 5) * foam_mask_w) + (xx + 0)] = 2; + foam_mask[((yy + 5) * foam_mask_w) + (xx + 1)] = 0; + foam_mask[((yy + 5) * foam_mask_w) + (xx + 2)] = 0; + foam_mask[((yy + 5) * foam_mask_w) + (xx + 3)] = 7; + foam_mask[((yy + 5) * foam_mask_w) + (xx + 4)] = 0; + foam_mask[((yy + 5) * foam_mask_w) + (xx + 5)] = 3; + foam_mask[((yy + 5) * foam_mask_w) + (xx + 6)] = 0; - foam_mask[((yy + 6) * foam_mask_w) + (xx + 0)] = 0; - foam_mask[((yy + 6) * foam_mask_w) + (xx + 1)] = 0; - foam_mask[((yy + 6) * foam_mask_w) + (xx + 2)] = 1; - foam_mask[((yy + 6) * foam_mask_w) + (xx + 3)] = 0; - foam_mask[((yy + 6) * foam_mask_w) + (xx + 4)] = 1; - foam_mask[((yy + 6) * foam_mask_w) + (xx + 5)] = 0; - foam_mask[((yy + 6) * foam_mask_w) + (xx + 6)] = 2; + foam_mask[((yy + 6) * foam_mask_w) + (xx + 0)] = 0; + foam_mask[((yy + 6) * foam_mask_w) + (xx + 1)] = 0; + foam_mask[((yy + 6) * foam_mask_w) + (xx + 2)] = 1; + foam_mask[((yy + 6) * foam_mask_w) + (xx + 3)] = 0; + foam_mask[((yy + 6) * foam_mask_w) + (xx + 4)] = 1; + foam_mask[((yy + 6) * foam_mask_w) + (xx + 5)] = 0; + foam_mask[((yy + 6) * foam_mask_w) + (xx + 6)] = 2; - changes = 1; - } - else if (foam_mask_test(5, xx, yy) >= 30) - { - foam_mask[((yy + 0) * foam_mask_w) + (xx + 0)] = 2; - foam_mask[((yy + 0) * foam_mask_w) + (xx + 1)] = 1; - foam_mask[((yy + 0) * foam_mask_w) + (xx + 2)] = 0; - foam_mask[((yy + 0) * foam_mask_w) + (xx + 3)] = 1; - foam_mask[((yy + 0) * foam_mask_w) + (xx + 4)] = 2; - - foam_mask[((yy + 1) * foam_mask_w) + (xx + 0)] = 1; - foam_mask[((yy + 1) * foam_mask_w) + (xx + 1)] = 0; - foam_mask[((yy + 1) * foam_mask_w) + (xx + 2)] = 0; - foam_mask[((yy + 1) * foam_mask_w) + (xx + 3)] = 0; - foam_mask[((yy + 1) * foam_mask_w) + (xx + 4)] = 1; - - foam_mask[((yy + 2) * foam_mask_w) + (xx + 0)] = 0; - foam_mask[((yy + 2) * foam_mask_w) + (xx + 1)] = 0; - foam_mask[((yy + 2) * foam_mask_w) + (xx + 2)] = 5; - foam_mask[((yy + 2) * foam_mask_w) + (xx + 3)] = 0; - foam_mask[((yy + 2) * foam_mask_w) + (xx + 4)] = 0; - - foam_mask[((yy + 3) * foam_mask_w) + (xx + 0)] = 2; - foam_mask[((yy + 3) * foam_mask_w) + (xx + 1)] = 0; - foam_mask[((yy + 3) * foam_mask_w) + (xx + 2)] = 1; - foam_mask[((yy + 3) * foam_mask_w) + (xx + 3)] = 2; - foam_mask[((yy + 3) * foam_mask_w) + (xx + 4)] = 0; - - foam_mask[((yy + 4) * foam_mask_w) + (xx + 0)] = 0; - foam_mask[((yy + 4) * foam_mask_w) + (xx + 1)] = 1; - foam_mask[((yy + 4) * foam_mask_w) + (xx + 2)] = 0; - foam_mask[((yy + 4) * foam_mask_w) + (xx + 3)] = 1; - foam_mask[((yy + 4) * foam_mask_w) + (xx + 4)] = 0; - - changes = 1; - } - else if (foam_mask_test(3, xx, yy) >= 8) - { - foam_mask[((yy + 0) * foam_mask_w) + (xx + 0)] = 1; - foam_mask[((yy + 0) * foam_mask_w) + (xx + 1)] = 0; - foam_mask[((yy + 0) * foam_mask_w) + (xx + 2)] = 1; - - foam_mask[((yy + 1) * foam_mask_w) + (xx + 0)] = 0; - foam_mask[((yy + 1) * foam_mask_w) + (xx + 1)] = 3; - foam_mask[((yy + 1) * foam_mask_w) + (xx + 2)] = 0; - - foam_mask[((yy + 2) * foam_mask_w) + (xx + 0)] = 1; - foam_mask[((yy + 2) * foam_mask_w) + (xx + 1)] = 0; - foam_mask[((yy + 2) * foam_mask_w) + (xx + 2)] = 1; - - changes = 1; - } - } + changes = 1; } + else if (foam_mask_test(5, xx, yy) >= 30) + { + foam_mask[((yy + 0) * foam_mask_w) + (xx + 0)] = 2; + foam_mask[((yy + 0) * foam_mask_w) + (xx + 1)] = 1; + foam_mask[((yy + 0) * foam_mask_w) + (xx + 2)] = 0; + foam_mask[((yy + 0) * foam_mask_w) + (xx + 3)] = 1; + foam_mask[((yy + 0) * foam_mask_w) + (xx + 4)] = 2; + + foam_mask[((yy + 1) * foam_mask_w) + (xx + 0)] = 1; + foam_mask[((yy + 1) * foam_mask_w) + (xx + 1)] = 0; + foam_mask[((yy + 1) * foam_mask_w) + (xx + 2)] = 0; + foam_mask[((yy + 1) * foam_mask_w) + (xx + 3)] = 0; + foam_mask[((yy + 1) * foam_mask_w) + (xx + 4)] = 1; + + foam_mask[((yy + 2) * foam_mask_w) + (xx + 0)] = 0; + foam_mask[((yy + 2) * foam_mask_w) + (xx + 1)] = 0; + foam_mask[((yy + 2) * foam_mask_w) + (xx + 2)] = 5; + foam_mask[((yy + 2) * foam_mask_w) + (xx + 3)] = 0; + foam_mask[((yy + 2) * foam_mask_w) + (xx + 4)] = 0; + + foam_mask[((yy + 3) * foam_mask_w) + (xx + 0)] = 2; + foam_mask[((yy + 3) * foam_mask_w) + (xx + 1)] = 0; + foam_mask[((yy + 3) * foam_mask_w) + (xx + 2)] = 1; + foam_mask[((yy + 3) * foam_mask_w) + (xx + 3)] = 2; + foam_mask[((yy + 3) * foam_mask_w) + (xx + 4)] = 0; + + foam_mask[((yy + 4) * foam_mask_w) + (xx + 0)] = 0; + foam_mask[((yy + 4) * foam_mask_w) + (xx + 1)] = 1; + foam_mask[((yy + 4) * foam_mask_w) + (xx + 2)] = 0; + foam_mask[((yy + 4) * foam_mask_w) + (xx + 3)] = 1; + foam_mask[((yy + 4) * foam_mask_w) + (xx + 4)] = 0; + + changes = 1; + } + else if (foam_mask_test(3, xx, yy) >= 8) + { + foam_mask[((yy + 0) * foam_mask_w) + (xx + 0)] = 1; + foam_mask[((yy + 0) * foam_mask_w) + (xx + 1)] = 0; + foam_mask[((yy + 0) * foam_mask_w) + (xx + 2)] = 1; + + foam_mask[((yy + 1) * foam_mask_w) + (xx + 0)] = 0; + foam_mask[((yy + 1) * foam_mask_w) + (xx + 1)] = 3; + foam_mask[((yy + 1) * foam_mask_w) + (xx + 2)] = 0; + + foam_mask[((yy + 2) * foam_mask_w) + (xx + 0)] = 1; + foam_mask[((yy + 2) * foam_mask_w) + (xx + 1)] = 0; + foam_mask[((yy + 2) * foam_mask_w) + (xx + 2)] = 1; + + changes = 1; + } + } } + } while (changes && max_iters > 0); for (yy = 0; yy < foam_mask_h; yy++) + { + for (xx = 0; xx < foam_mask_w; xx++) { - for (xx = 0; xx < foam_mask_w; xx++) - { - n = foam_mask[yy * foam_mask_w + xx]; + n = foam_mask[yy * foam_mask_w + xx]; - img = NULL; + img = NULL; - if (n == 1) - img = foam_1; - else if (n == 3) - img = foam_3; - else if (n == 5) - img = foam_5; - else if (n == 7) - img = foam_7; + if (n == 1) + img = foam_1; + else if (n == 3) + img = foam_3; + else if (n == 5) + img = foam_5; + else if (n == 7) + img = foam_7; - if (img != NULL) - { - dest.x = (xx * FOAM_PROP) - (img->w / 2) + ((rand() % 15) - 7); - dest.y = (yy * FOAM_PROP) - (img->h / 2) + ((rand() % 15) - 7); + if (img != NULL) + { + dest.x = (xx * FOAM_PROP) - (img->w / 2) + ((rand() % 15) - 7); + dest.y = (yy * FOAM_PROP) - (img->h / 2) + ((rand() % 15) - 7); - SDL_BlitSurface(img, NULL, canvas, &dest); - } - } + SDL_BlitSurface(img, NULL, canvas, &dest); + } } + } - memcpy(foam_mask, foam_mask_tmp, (sizeof(int) * (foam_mask_w * foam_mask_h))); + memcpy(foam_mask, foam_mask_tmp, + (sizeof(int) * (foam_mask_w * foam_mask_h))); update_rect->x = 0; @@ -431,7 +460,8 @@ void foam_shutdown(magic_api * api ATTRIBUTE_UNUSED) } // Record the color from Tux Paint: -void foam_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) +void foam_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, + Uint8 b) { foam_r = r; foam_g = g; @@ -439,17 +469,20 @@ void foam_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) } // Use colors: -int foam_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int foam_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; /* FIXME: Would be nice to tint the bubbles */ } -void foam_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void foam_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void foam_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void foam_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } diff --git a/magic/src/fold.c b/magic/src/fold.c index 544ac9145..0e8ed31f9 100644 --- a/magic/src/fold.c +++ b/magic/src/fold.c @@ -32,15 +32,21 @@ SDL_Surface *fold_surface_src, *fold_surface_dst; void fold_draw(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); -static void fold_erase(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); + SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect); +static void fold_erase(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); void translate_coords(SDL_Surface * canvas, int angle); SDL_Surface *rotate(magic_api * api, SDL_Surface * canvas, int angle); void fold_draw(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); -static void fold_print_line(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); -static void fold_print_dark_line(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); -void translate_xy(SDL_Surface * canvas, int x, int y, int *a, int *b, int rotation); + SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect); +static void fold_print_line(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); +static void fold_print_dark_line(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); +void translate_xy(SDL_Surface * canvas, int x, int y, int *a, int *b, + int rotation); Uint32 fold_api_version(void); void fold_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int fold_init(magic_api * api); @@ -51,21 +57,27 @@ int fold_get_group(magic_api * api, int which); char *fold_get_description(magic_api * api, int which, int mode); int fold_requires_colors(magic_api * api, int which); void fold_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); + SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect); void fold_shutdown(magic_api * api); void fold_click(magic_api * ptr, int which, int mode, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); + SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect); void fold_preview(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect); + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect); int fold_modes(magic_api * api, int which); // Housekeeping functions void fold_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void fold_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void fold_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); inline Uint8 fold_what_corner(int x, int y, SDL_Surface * canvas); -void fold_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void fold_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); Uint32 fold_api_version(void) { @@ -83,7 +95,8 @@ int fold_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/fold.wav", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/fold.wav", + api->data_directory); fold_snd = Mix_LoadWAV(fname); return (1); @@ -94,50 +107,65 @@ int fold_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) return 1; } -SDL_Surface *fold_get_icon(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +SDL_Surface *fold_get_icon(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/fold.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/fold.png", + api->data_directory); return (IMG_Load(fname)); } -char *fold_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *fold_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (gettext_noop("Fold")); } -int fold_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int fold_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PICTURE_WARPS; } -char *fold_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *fold_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED) { - return strdup(gettext_noop("Choose a background color and click to turn the corner of the page over.")); + return + strdup(gettext_noop + ("Choose a background color and click to turn the corner of the page over.")); } -int fold_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int fold_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 1; } //selected color will be a "backpage" color -static void fold_shadow(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * temp, int x, int y) +static void fold_shadow(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * temp, int x, + int y) { magic_api *api = (magic_api *) ptr; Uint8 r, g, b, a; SDL_GetRGBA(api->getpixel(temp, x, y), temp->format, &r, &g, &b, &a); api->putpixel(canvas, x, y, SDL_MapRGBA(canvas->format, - max(r - 160 + fold_shadow_value * 4, 0), max(g - 160 + fold_shadow_value * 4, - 0), - max(b - 160 + fold_shadow_value * 4, 0), a)); + max(r - 160 + fold_shadow_value * 4, + 0), + max(g - 160 + fold_shadow_value * 4, + 0), + max(b - 160 + fold_shadow_value * 4, + 0), a)); } void fold_draw(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect ATTRIBUTE_UNUSED) + SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect ATTRIBUTE_UNUSED) { float right_step_x, right_step_y, left_step_x, left_step_y; float dist_x, dist_y; @@ -145,86 +173,103 @@ void fold_draw(magic_api * api, int which, float w, h; SDL_Surface *temp; - temp = SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->w, canvas->h, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, - canvas->format->Amask); + temp = + SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->w, canvas->h, + canvas->format->BitsPerPixel, canvas->format->Rmask, + canvas->format->Gmask, canvas->format->Bmask, + canvas->format->Amask); SDL_BlitSurface(canvas, 0, temp, 0); - right_step_x = (float)(x - left_arm_x) / (float)(left_arm_x - fold_ox); - right_step_y = (float)(y - left_arm_y) / (float)(left_arm_x - fold_ox); - left_step_x = (float)(x - right_arm_x) / (float)(right_arm_y - fold_oy); - left_step_y = (float)(y - right_arm_y) / (float)(right_arm_y - fold_oy); + right_step_x = (float) (x - left_arm_x) / (float) (left_arm_x - fold_ox); + right_step_y = (float) (y - left_arm_y) / (float) (left_arm_x - fold_ox); + left_step_x = (float) (x - right_arm_x) / (float) (right_arm_y - fold_oy); + left_step_y = (float) (y - right_arm_y) / (float) (right_arm_y - fold_oy); - left_y = (float)right_arm_y / left_arm_x * (left_arm_x - canvas->w); - right_x = (float)left_arm_x / right_arm_y * (right_arm_y - canvas->h); + left_y = (float) right_arm_y / left_arm_x * (left_arm_x - canvas->w); + right_x = (float) left_arm_x / right_arm_y * (right_arm_y - canvas->h); for (w = 0; w < canvas->w; w += 0.5) + { + for (h = 0; h < canvas->h; h += 0.5) { - for (h = 0; h < canvas->h; h += 0.5) - { - dist_x = right_step_x * w + left_step_x * h; - dist_y = right_step_y * w + left_step_y * h; - api->putpixel(canvas, x - dist_x, y - dist_y, api->getpixel(temp, w, h)); - } + dist_x = right_step_x * w + left_step_x * h; + dist_y = right_step_y * w + left_step_y * h; + api->putpixel(canvas, x - dist_x, y - dist_y, + api->getpixel(temp, w, h)); } + } // Erasing the triangle. // The 1 pixel in plus is a workaround for api-line not getting the end in some lines. if (left_arm_x > canvas->w) - { - for (h = 0; h <= right_arm_y; h++) - api->line((void *)api, which, canvas, snapshot, canvas->w, left_y - h, -1, right_arm_y - h, 1, fold_erase); - } + { + for (h = 0; h <= right_arm_y; h++) + api->line((void *) api, which, canvas, snapshot, canvas->w, left_y - h, + -1, right_arm_y - h, 1, fold_erase); + } else if (right_arm_y > canvas->h) - { - for (w = 0; w <= left_arm_x; w++) - api->line((void *)api, which, canvas, snapshot, left_arm_x - w, 0, right_x - w, canvas->h + 1, 1, fold_erase); - } + { + for (w = 0; w <= left_arm_x; w++) + api->line((void *) api, which, canvas, snapshot, left_arm_x - w, 0, + right_x - w, canvas->h + 1, 1, fold_erase); + } else for (w = 0; w <= min(left_arm_x, right_arm_y); w++) // The -1 values are because api->line - api->line((void *)api, which, canvas, snapshot, left_arm_x - w, 0, -1, right_arm_y - w, 1, fold_erase); + api->line((void *) api, which, canvas, snapshot, left_arm_x - w, 0, -1, + right_arm_y - w, 1, fold_erase); SDL_BlitSurface(canvas, 0, temp, 0); // Shadows if (left_arm_x > canvas->w) - { - for (fold_shadow_value = 0; fold_shadow_value < 40; fold_shadow_value += 1) - api->line((void *)api, which, canvas, temp, canvas->w, left_y - fold_shadow_value, 0, - right_arm_y - fold_shadow_value, 1, fold_shadow); + { + for (fold_shadow_value = 0; fold_shadow_value < 40; + fold_shadow_value += 1) + api->line((void *) api, which, canvas, temp, canvas->w, + left_y - fold_shadow_value, 0, + right_arm_y - fold_shadow_value, 1, fold_shadow); - } + } else if (right_arm_y > canvas->h) - { - for (fold_shadow_value = 0; fold_shadow_value < 40; fold_shadow_value += 1) - api->line((void *)api, which, canvas, temp, left_arm_x - fold_shadow_value, 0, right_x - fold_shadow_value, - canvas->h, 1, fold_shadow); + { + for (fold_shadow_value = 0; fold_shadow_value < 40; + fold_shadow_value += 1) + api->line((void *) api, which, canvas, temp, + left_arm_x - fold_shadow_value, 0, + right_x - fold_shadow_value, canvas->h, 1, fold_shadow); - } + } else - for (fold_shadow_value = 0; fold_shadow_value < 40; fold_shadow_value += 1) - api->line((void *)api, which, canvas, temp, left_arm_x - fold_shadow_value, 0, 0, right_arm_y - fold_shadow_value, - 1, fold_shadow); + for (fold_shadow_value = 0; fold_shadow_value < 40; + fold_shadow_value += 1) + api->line((void *) api, which, canvas, temp, + left_arm_x - fold_shadow_value, 0, 0, + right_arm_y - fold_shadow_value, 1, fold_shadow); SDL_BlitSurface(canvas, 0, temp, 0); for (fold_shadow_value = 0; fold_shadow_value < 40; fold_shadow_value += 1) - { - if (fold_shadow_value * left_step_x > x || fold_shadow_value * right_step_y > y) - break; + { + if (fold_shadow_value * left_step_x > x + || fold_shadow_value * right_step_y > y) + break; - dist_x = fold_shadow_value * (right_step_x + left_step_x); - dist_y = fold_shadow_value * (right_step_y + left_step_y); - api->line((void *)api, which, canvas, temp, left_arm_x + fold_shadow_value * right_step_x, - fold_shadow_value * right_step_y, fold_shadow_value * left_step_x, - right_arm_y + fold_shadow_value * left_step_y, 1, fold_shadow); - } + dist_x = fold_shadow_value * (right_step_x + left_step_x); + dist_y = fold_shadow_value * (right_step_y + left_step_y); + api->line((void *) api, which, canvas, temp, + left_arm_x + fold_shadow_value * right_step_x, + fold_shadow_value * right_step_y, + fold_shadow_value * left_step_x, + right_arm_y + fold_shadow_value * left_step_y, 1, fold_shadow); + } - api->line((void *)api, which, canvas, snapshot, x, y, right_arm_x, right_arm_y, 1, fold_print_line); - api->line((void *)api, which, canvas, snapshot, x, y, left_arm_x, left_arm_y, 1, fold_print_line); - api->line((void *)api, which, canvas, snapshot, left_arm_x, left_arm_y, right_arm_x, right_arm_y, 1, - fold_print_dark_line); + api->line((void *) api, which, canvas, snapshot, x, y, right_arm_x, + right_arm_y, 1, fold_print_line); + api->line((void *) api, which, canvas, snapshot, x, y, left_arm_x, + left_arm_y, 1, fold_print_line); + api->line((void *) api, which, canvas, snapshot, left_arm_x, left_arm_y, + right_arm_x, right_arm_y, 1, fold_print_dark_line); } @@ -235,44 +280,48 @@ SDL_Surface *rotate(magic_api * api, SDL_Surface * canvas, int angle) int a, b; if (angle == 180) - temp = SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->w, canvas->h, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, - canvas->format->Amask); + temp = + SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->w, canvas->h, + canvas->format->BitsPerPixel, + canvas->format->Rmask, canvas->format->Gmask, + canvas->format->Bmask, canvas->format->Amask); else - temp = SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->h, canvas->w, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, - canvas->format->Amask); + temp = + SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->h, canvas->w, + canvas->format->BitsPerPixel, + canvas->format->Rmask, canvas->format->Gmask, + canvas->format->Bmask, canvas->format->Amask); switch (angle) - { - case 90: - for (x = 0; x < canvas->w; x++) - for (y = 0; y < canvas->h; y++) - { - translate_xy(canvas, x, y, &a, &b, 90); - api->putpixel(temp, a, b, api->getpixel(canvas, x, y)); - } - break; + { + case 90: + for (x = 0; x < canvas->w; x++) + for (y = 0; y < canvas->h; y++) + { + translate_xy(canvas, x, y, &a, &b, 90); + api->putpixel(temp, a, b, api->getpixel(canvas, x, y)); + } + break; - case 180: - // printf("%i, %i\n",temp,canvas); - for (x = 0; x < canvas->w; x++) - for (y = 0; y < canvas->h; y++) - { - translate_xy(canvas, x, y, &a, &b, 180); - api->putpixel(temp, a, b, api->getpixel(canvas, x, y)); - } - break; + case 180: + // printf("%i, %i\n",temp,canvas); + for (x = 0; x < canvas->w; x++) + for (y = 0; y < canvas->h; y++) + { + translate_xy(canvas, x, y, &a, &b, 180); + api->putpixel(temp, a, b, api->getpixel(canvas, x, y)); + } + break; - case 270: - for (x = 0; x < canvas->w; x++) - for (y = 0; y < canvas->h; y++) - { - translate_xy(canvas, x, y, &a, &b, 270); - api->putpixel(temp, a, b, api->getpixel(canvas, x, y)); - } - break; - } + case 270: + for (x = 0; x < canvas->w; x++) + for (y = 0; y < canvas->h; y++) + { + translate_xy(canvas, x, y, &a, &b, 270); + api->putpixel(temp, a, b, api->getpixel(canvas, x, y)); + } + break; + } return temp; } @@ -281,54 +330,56 @@ void translate_coords(SDL_Surface * canvas, int angle) int a, b; switch (angle) - { - case 90: - translate_xy(canvas, right_arm_x, right_arm_y, &a, &b, 90); - right_arm_x = a; - right_arm_y = b; - translate_xy(canvas, left_arm_x, left_arm_y, &a, &b, 90); - left_arm_x = a; - left_arm_y = b; + { + case 90: + translate_xy(canvas, right_arm_x, right_arm_y, &a, &b, 90); + right_arm_x = a; + right_arm_y = b; + translate_xy(canvas, left_arm_x, left_arm_y, &a, &b, 90); + left_arm_x = a; + left_arm_y = b; - break; - case 180: - right_arm_x = canvas->w - 1 - right_arm_x; - right_arm_y = canvas->h - 1 - right_arm_y; - left_arm_x = canvas->w - 1 - left_arm_x; - left_arm_y = canvas->h - 1 - left_arm_y; - break; - case 270: - translate_xy(canvas, right_arm_x, right_arm_y, &a, &b, 270); - right_arm_x = a; - right_arm_y = b; - translate_xy(canvas, left_arm_x, left_arm_y, &a, &b, 270); - left_arm_x = a; - left_arm_y = b; - break; - } + break; + case 180: + right_arm_x = canvas->w - 1 - right_arm_x; + right_arm_y = canvas->h - 1 - right_arm_y; + left_arm_x = canvas->w - 1 - left_arm_x; + left_arm_y = canvas->h - 1 - left_arm_y; + break; + case 270: + translate_xy(canvas, right_arm_x, right_arm_y, &a, &b, 270); + right_arm_x = a; + right_arm_y = b; + translate_xy(canvas, left_arm_x, left_arm_y, &a, &b, 270); + left_arm_x = a; + left_arm_y = b; + break; + } } -void translate_xy(SDL_Surface * canvas, int x, int y, int *a, int *b, int rotation) +void translate_xy(SDL_Surface * canvas, int x, int y, int *a, int *b, + int rotation) { switch (rotation) - { - case 90: - *a = y; - *b = canvas->w - 1 - x; - break; - case 180: - *a = canvas->w - 1 - x; - *b = canvas->h - 1 - y; - break; - case 270: - *a = canvas->h - 1 - y; - *b = x; - break; - } + { + case 90: + *a = y; + *b = canvas->w - 1 - x; + break; + case 180: + *a = canvas->w - 1 - x; + *b = canvas->h - 1 - y; + break; + case 270: + *a = canvas->h - 1 - y; + *b = x; + break; + } } void fold_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect) + SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect) { int a, b; SDL_Surface *temp, *temp2; @@ -338,44 +389,44 @@ void fold_release(magic_api * api, int which, fold_ox = fold_oy = 0; SDL_BlitSurface(snapshot, 0, canvas, 0); switch (corner) - { - case 1: - translate_xy(canvas, x, y, &a, &b, 90); - translate_coords(canvas, 90); - temp = rotate(api, canvas, 90); - fold_draw(api, which, temp, snapshot, a, b, update_rect); - temp2 = rotate(api, temp, 270); - SDL_BlitSurface(temp2, 0, canvas, 0); - SDL_FreeSurface(temp); - SDL_FreeSurface(temp2); - break; + { + case 1: + translate_xy(canvas, x, y, &a, &b, 90); + translate_coords(canvas, 90); + temp = rotate(api, canvas, 90); + fold_draw(api, which, temp, snapshot, a, b, update_rect); + temp2 = rotate(api, temp, 270); + SDL_BlitSurface(temp2, 0, canvas, 0); + SDL_FreeSurface(temp); + SDL_FreeSurface(temp2); + break; - case 2: - fold_draw(api, which, canvas, snapshot, x, y, update_rect); - break; + case 2: + fold_draw(api, which, canvas, snapshot, x, y, update_rect); + break; - case 3: - translate_xy(canvas, x, y, &a, &b, 270); - translate_coords(canvas, 270); - temp = rotate(api, canvas, 270); - fold_draw(api, which, temp, snapshot, a, b, update_rect); - temp2 = rotate(api, temp, 90); - SDL_BlitSurface(temp2, 0, canvas, 0); - SDL_FreeSurface(temp); - SDL_FreeSurface(temp2); - break; + case 3: + translate_xy(canvas, x, y, &a, &b, 270); + translate_coords(canvas, 270); + temp = rotate(api, canvas, 270); + fold_draw(api, which, temp, snapshot, a, b, update_rect); + temp2 = rotate(api, temp, 90); + SDL_BlitSurface(temp2, 0, canvas, 0); + SDL_FreeSurface(temp); + SDL_FreeSurface(temp2); + break; - case 4: - translate_xy(canvas, x, y, &a, &b, 180); - translate_coords(canvas, 180); - temp = rotate(api, canvas, 180); - fold_draw(api, which, temp, snapshot, a, b, update_rect); - temp2 = rotate(api, temp, 180); - SDL_BlitSurface(temp2, 0, canvas, 0); - SDL_FreeSurface(temp); - SDL_FreeSurface(temp2); - break; - } + case 4: + translate_xy(canvas, x, y, &a, &b, 180); + translate_coords(canvas, 180); + temp = rotate(api, canvas, 180); + fold_draw(api, which, temp, snapshot, a, b, update_rect); + temp2 = rotate(api, temp, 180); + SDL_BlitSurface(temp2, 0, canvas, 0); + SDL_FreeSurface(temp); + SDL_FreeSurface(temp2); + break; + } update_rect->x = update_rect->y = 0; update_rect->w = canvas->w; @@ -395,77 +446,84 @@ void fold_shutdown(magic_api * api ATTRIBUTE_UNUSED) inline Uint8 fold_what_corner(int x, int y, SDL_Surface * canvas) { if (x >= canvas->w / 2) - { - if (y >= canvas->h / 2) - return 4; - else - return 1; - } + { + if (y >= canvas->h / 2) + return 4; + else + return 1; + } else - { - if (y >= canvas->h / 2) - return 3; - else - return 2; - } + { + if (y >= canvas->h / 2) + return 3; + else + return 2; + } } -static void fold_print_line(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * last, - int x, int y) +static void fold_print_line(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * last, int x, + int y) { magic_api *api = (magic_api *) ptr; api->putpixel(canvas, x, y, SDL_MapRGB(last->format, 222, 222, 222)); //Middle gray. Color have been set arbitrary. } -static void fold_print_dark_line(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, - SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) +static void fold_print_dark_line(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, int x, + int y) { magic_api *api = (magic_api *) ptr; api->putpixel(canvas, x, y, SDL_MapRGB(last->format, 90, 90, 90)); //It should not look too black nor too white with shadowed colors. } -static void fold_erase(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, - int x, int y) +static void fold_erase(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { magic_api *api = (magic_api *) ptr; - api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, fold_r, fold_g, fold_b)); + api->putpixel(canvas, x, y, + SDL_MapRGB(canvas->format, fold_r, fold_g, fold_b)); } void fold_click(magic_api * ptr, int which, int mode ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect) + SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect) { magic_api *api = (magic_api *) ptr; corner = fold_what_corner(x, y, snapshot); switch (corner) - { - case 1: - fold_ox = canvas->w - 1; - fold_oy = 0; - break; - case 2: - fold_ox = fold_oy = 0; - break; - case 3: - fold_ox = 0; - fold_oy = canvas->h - 1; - break; - case 4: - fold_ox = canvas->w - 1; - fold_oy = canvas->h - 1; - break; - } + { + case 1: + fold_ox = canvas->w - 1; + fold_oy = 0; + break; + case 2: + fold_ox = fold_oy = 0; + break; + case 3: + fold_ox = 0; + fold_oy = canvas->h - 1; + break; + case 4: + fold_ox = canvas->w - 1; + fold_oy = canvas->h - 1; + break; + } fold_drag(api, which, canvas, snapshot, x, y, x, y, update_rect); } void fold_preview(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, int x, int y, + SDL_Surface * snapshot, int ox ATTRIBUTE_UNUSED, + int oy ATTRIBUTE_UNUSED, int x, int y, SDL_Rect * update_rect) { int middle_point_x; @@ -479,51 +537,69 @@ void fold_preview(magic_api * api, int which, SDL_Surface * canvas, middle_point_y = (fold_oy + y) / 2; switch (corner) - { - case 1: //Right Upper - right_arm_x = fold_ox - (fold_ox - middle_point_x) - middle_point_y * middle_point_y / (fold_ox - middle_point_x); - right_arm_y = fold_oy; + { + case 1: //Right Upper + right_arm_x = + fold_ox - (fold_ox - middle_point_x) - + middle_point_y * middle_point_y / (fold_ox - middle_point_x); + right_arm_y = fold_oy; - left_arm_x = fold_ox; - left_arm_y = - fold_oy - (fold_oy - middle_point_y) - (fold_ox - middle_point_x) * (fold_ox - middle_point_x) / (fold_oy - - middle_point_y); - break; + left_arm_x = fold_ox; + left_arm_y = + fold_oy - (fold_oy - middle_point_y) - (fold_ox - + middle_point_x) * (fold_ox - + middle_point_x) + / (fold_oy - middle_point_y); + break; - case 2: //LU - right_arm_x = fold_ox; - right_arm_y = middle_point_y + middle_point_x * middle_point_x / middle_point_y; + case 2: //LU + right_arm_x = fold_ox; + right_arm_y = + middle_point_y + middle_point_x * middle_point_x / middle_point_y; - left_arm_x = middle_point_x + middle_point_y * middle_point_y / middle_point_x; - left_arm_y = fold_oy; - break; + left_arm_x = + middle_point_x + middle_point_y * middle_point_y / middle_point_x; + left_arm_y = fold_oy; + break; - case 3: //LL - right_arm_x = middle_point_x + (fold_oy - middle_point_y) * (fold_oy - middle_point_y) / middle_point_x; - right_arm_y = fold_oy; + case 3: //LL + right_arm_x = + middle_point_x + (fold_oy - middle_point_y) * (fold_oy - + middle_point_y) / + middle_point_x; + right_arm_y = fold_oy; - left_arm_x = fold_ox; - left_arm_y = - fold_oy - (fold_oy - middle_point_y) - (fold_ox - middle_point_x) * (fold_ox - middle_point_x) / (fold_oy - - middle_point_y); - break; + left_arm_x = fold_ox; + left_arm_y = + fold_oy - (fold_oy - middle_point_y) - (fold_ox - + middle_point_x) * (fold_ox - + middle_point_x) + / (fold_oy - middle_point_y); + break; - case 4: //RL - right_arm_x = fold_ox; - right_arm_y = - fold_oy - (fold_oy - middle_point_y) - (fold_ox - middle_point_x) * (fold_ox - middle_point_x) / (fold_oy - - middle_point_y); + case 4: //RL + right_arm_x = fold_ox; + right_arm_y = + fold_oy - (fold_oy - middle_point_y) - (fold_ox - + middle_point_x) * (fold_ox - + middle_point_x) + / (fold_oy - middle_point_y); - left_arm_x = - fold_ox - (fold_ox - middle_point_x) - (fold_oy - middle_point_y) * (fold_oy - middle_point_y) / (fold_ox - - middle_point_x); - left_arm_y = fold_oy; - break; - } + left_arm_x = + fold_ox - (fold_ox - middle_point_x) - (fold_oy - + middle_point_y) * (fold_oy - + middle_point_y) + / (fold_ox - middle_point_x); + left_arm_y = fold_oy; + break; + } - api->line((void *)api, which, canvas, snapshot, x, y, right_arm_x, right_arm_y, 1, fold_print_line); - api->line((void *)api, which, canvas, snapshot, x, y, left_arm_x, left_arm_y, 1, fold_print_line); - api->line((void *)api, which, canvas, snapshot, left_arm_x, left_arm_y, right_arm_x, right_arm_y, 1, fold_print_line); + api->line((void *) api, which, canvas, snapshot, x, y, right_arm_x, + right_arm_y, 1, fold_print_line); + api->line((void *) api, which, canvas, snapshot, x, y, left_arm_x, + left_arm_y, 1, fold_print_line); + api->line((void *) api, which, canvas, snapshot, left_arm_x, left_arm_y, + right_arm_x, right_arm_y, 1, fold_print_line); update_rect->x = update_rect->y = 0; update_rect->w = canvas->w; @@ -531,7 +607,8 @@ void fold_preview(magic_api * api, int which, SDL_Surface * canvas, } void fold_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect) + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect) { // Avoid division by zero when calculating the preview x = clamp(2, x, canvas->w - 2); @@ -539,12 +616,14 @@ void fold_drag(magic_api * api, int which, SDL_Surface * canvas, fold_preview(api, which, canvas, snapshot, ox, oy, x, y, update_rect); } -void fold_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void fold_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void fold_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void fold_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } diff --git a/magic/src/fretwork.c b/magic/src/fretwork.c index 4b2231520..4bb1c9a93 100644 --- a/magic/src/fretwork.c +++ b/magic/src/fretwork.c @@ -49,7 +49,8 @@ static unsigned int fretwork_update_rectangle_width; //the width of the updat static unsigned int fretwork_update_rectangle_height; //the height of the update_rectangle static SDL_Rect modification_rect; static SDL_Surface *canvas_backup; -static SDL_Surface *fretwork_one_back, *fretwork_three_back, *fretwork_four_back, *fretwork_corner_back; +static SDL_Surface *fretwork_one_back, *fretwork_three_back, + *fretwork_four_back, *fretwork_corner_back; // Housekeeping functions @@ -57,7 +58,8 @@ static SDL_Surface *fretwork_one_back, *fretwork_three_back, *fretwork_four_back Uint32 fretwork_api_version(void); int fretwork_modes(magic_api * api, int which); void fretwork_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); -static void fretwork_colorize(magic_api * api, SDL_Surface * dest, SDL_Surface * src); +static void fretwork_colorize(magic_api * api, SDL_Surface * dest, + SDL_Surface * src); int fretwork_init(magic_api * api); int fretwork_get_tool_count(magic_api * api); SDL_Surface *fretwork_get_icon(magic_api * api, int which); @@ -66,20 +68,28 @@ int fretwork_get_group(magic_api * api, int which); char *fretwork_get_description(magic_api * api, int which, int mode); int fretwork_requires_colors(magic_api * api, int which); void fretwork_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); + SDL_Surface * canvas, SDL_Surface * snapshot, int x, + int y, SDL_Rect * update_rect); void fretwork_shutdown(magic_api * api); -void fretwork_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas, SDL_Surface * snapshot); -void fretwork_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas, SDL_Surface * snapshot); +void fretwork_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas, SDL_Surface * snapshot); +void fretwork_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas, SDL_Surface * snapshot); #define POINT_TYPE typeof(((SDL_Rect *)NULL)->x) -inline void fretwork_extract_coords_from_segment(unsigned int segment, POINT_TYPE * x, POINT_TYPE * y); +inline void fretwork_extract_coords_from_segment(unsigned int segment, + POINT_TYPE * x, + POINT_TYPE * y); void fretwork_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); + SDL_Surface * canvas, SDL_Surface * snapshot, int x, + int y, SDL_Rect * update_rect); void fretwork_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect); -static void fretwork_draw_wrapper(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +static void fretwork_draw_wrapper(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); inline unsigned int fretwork_get_segment(int x, int y); @@ -90,7 +100,8 @@ Uint32 fretwork_api_version(void) return (TP_MAGIC_API_VERSION); } -int fretwork_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int fretwork_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_PAINT | MODE_FULLSCREEN); } @@ -107,7 +118,8 @@ void fretwork_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b) } /* Adapted from flower.c */ -static void fretwork_colorize(magic_api * api, SDL_Surface * dest, SDL_Surface * src) +static void fretwork_colorize(magic_api * api, SDL_Surface * dest, + SDL_Surface * src) { int x, y; Uint8 r, g, b, a; @@ -116,14 +128,16 @@ static void fretwork_colorize(magic_api * api, SDL_Surface * dest, SDL_Surface * SDL_LockSurface(dest); for (y = 0; y < src->h; y++) + { + for (x = 0; x < src->w; x++) { - for (x = 0; x < src->w; x++) - { - SDL_GetRGBA(api->getpixel(src, x, y), src->format, &r, &g, &b, &a); + SDL_GetRGBA(api->getpixel(src, x, y), src->format, &r, &g, &b, &a); - api->putpixel(dest, x, y, SDL_MapRGBA(dest->format, fretwork_r, fretwork_g, fretwork_b, a)); - } + api->putpixel(dest, x, y, + SDL_MapRGBA(dest->format, fretwork_r, fretwork_g, + fretwork_b, a)); } + } SDL_UnlockSurface(src); SDL_UnlockSurface(dest); @@ -135,15 +149,19 @@ int fretwork_init(magic_api * api) char fname[1024]; Uint8 i; //is always < 4, so Uint8 seems to be a good idea - fretwork_images = (char **)malloc(sizeof(char *) * 4); + fretwork_images = (char **) malloc(sizeof(char *) * 4); for (i = 0; i < 4; i++) - fretwork_images[i] = (char *)malloc(sizeof(char) * 1024); + fretwork_images[i] = (char *) malloc(sizeof(char) * 1024); - snprintf(fretwork_images[0], 1024 * sizeof(char), "%simages/magic/fretwork_one.png", api->data_directory); - snprintf(fretwork_images[1], 1024 * sizeof(char), "%simages/magic/fretwork_three.png", api->data_directory); - snprintf(fretwork_images[2], 1024 * sizeof(char), "%simages/magic/fretwork_four.png", api->data_directory); - snprintf(fretwork_images[3], 1024 * sizeof(char), "%simages/magic/fretwork_corner.png", api->data_directory); + snprintf(fretwork_images[0], 1024 * sizeof(char), + "%simages/magic/fretwork_one.png", api->data_directory); + snprintf(fretwork_images[1], 1024 * sizeof(char), + "%simages/magic/fretwork_three.png", api->data_directory); + snprintf(fretwork_images[2], 1024 * sizeof(char), + "%simages/magic/fretwork_four.png", api->data_directory); + snprintf(fretwork_images[3], 1024 * sizeof(char), + "%simages/magic/fretwork_corner.png", api->data_directory); fretwork_one = IMG_Load(fretwork_images[0]); fretwork_three = IMG_Load(fretwork_images[1]); @@ -157,7 +175,8 @@ int fretwork_init(magic_api * api) img_w = fretwork_one->w; img_h = fretwork_one->h; - snprintf(fname, sizeof(fname), "%ssounds/magic/fretwork.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/fretwork.ogg", + api->data_directory); fretwork_snd = Mix_LoadWAV(fname); return (1); @@ -172,37 +191,48 @@ SDL_Surface *fretwork_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/fretwork.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/fretwork.png", + api->data_directory); return (IMG_Load(fname)); } -int fretwork_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int fretwork_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PAINTING; } -char *fretwork_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *fretwork_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return strdup(gettext_noop("Fretwork")); } -char *fretwork_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode) +char *fretwork_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode) { if (mode == MODE_PAINT) - return strdup(gettext_noop("Click and drag to draw repetitive patterns.")); + return + strdup(gettext_noop("Click and drag to draw repetitive patterns.")); else - return strdup(gettext_noop("Click to surround your picture with repetitive patterns.")); + return + strdup(gettext_noop + ("Click to surround your picture with repetitive patterns.")); } -int fretwork_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int fretwork_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 1; } -void fretwork_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * snapshot ATTRIBUTE_UNUSED, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) +void fretwork_release(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED, + SDL_Surface * snapshot ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, + SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -229,14 +259,18 @@ void fretwork_shutdown(magic_api * api ATTRIBUTE_UNUSED) free(fretwork_status_of_segments); } -void fretwork_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * snapshot ATTRIBUTE_UNUSED) +void fretwork_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas, + SDL_Surface * snapshot ATTRIBUTE_UNUSED) { //we've to compute the quantity of segments in each direction - canvas_backup = SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->w, canvas->h, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, - canvas->format->Amask); + canvas_backup = + SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->w, canvas->h, + canvas->format->BitsPerPixel, canvas->format->Rmask, + canvas->format->Gmask, canvas->format->Bmask, + canvas->format->Amask); SDL_BlitSurface(canvas, NULL, canvas_backup, NULL); fretwork_segments_x = fretwork_math_ceil(canvas->w, img_w); @@ -246,8 +280,10 @@ void fretwork_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNU } -void fretwork_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, - SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * snapshot ATTRIBUTE_UNUSED) +void fretwork_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED, + SDL_Surface * snapshot ATTRIBUTE_UNUSED) { free(fretwork_status_of_segments); fretwork_status_of_segments = NULL; @@ -260,7 +296,7 @@ inline int fretwork_math_ceil(int x, int y) { int temp; - temp = (int)x / y; + temp = (int) x / y; if (x % y) return temp + 1; else @@ -279,10 +315,12 @@ inline unsigned int fretwork_get_segment(int x, int y) return (yy - 1) * fretwork_segments_x + xx; } -inline void fretwork_extract_coords_from_segment(unsigned int segment, POINT_TYPE * x, POINT_TYPE * y) +inline void fretwork_extract_coords_from_segment(unsigned int segment, + POINT_TYPE * x, + POINT_TYPE * y) { *x = ((segment % fretwork_segments_x) - 1) * img_w; //useful to set update_rect as small as possible - *y = (int)(segment / fretwork_segments_x) * img_h; + *y = (int) (segment / fretwork_segments_x) * img_h; } /* static void fretwork_flip(void * ptr, SDL_Surface * dest, SDL_Surface * src) */ @@ -296,81 +334,87 @@ inline void fretwork_extract_coords_from_segment(unsigned int segment, POINT_TYP /* api->putpixel(dest, x, y, api->getpixel(src, x, src->h-y)); */ /* } */ -static void fretwork_flip_flop(void *ptr, SDL_Surface * dest, SDL_Surface * src) +static void fretwork_flip_flop(void *ptr, SDL_Surface * dest, + SDL_Surface * src) { magic_api *api = (magic_api *) ptr; POINT_TYPE x, y; for (x = 0; x < dest->w; x++) for (y = 0; y < dest->h; y++) - api->putpixel(dest, dest->w - 1 - x, dest->h - 1 - y, api->getpixel(src, x, y)); + api->putpixel(dest, dest->w - 1 - x, dest->h - 1 - y, + api->getpixel(src, x, y)); } -static void fretwork_rotate(void *ptr, SDL_Surface * dest, SDL_Surface * src, _Bool direction) +static void fretwork_rotate(void *ptr, SDL_Surface * dest, SDL_Surface * src, + _Bool direction) //src and dest must have same size { magic_api *api = (magic_api *) ptr; POINT_TYPE x, y; if (direction) //rotate -90 degs - { - for (x = 0; x < dest->w; x++) - for (y = 0; y < dest->h; y++) - api->putpixel(dest, x, y, api->getpixel(src, y, src->h - 1 - x)); - } + { + for (x = 0; x < dest->w; x++) + for (y = 0; y < dest->h; y++) + api->putpixel(dest, x, y, api->getpixel(src, y, src->h - 1 - x)); + } else //rotate +90 degs - { - for (x = 0; x < dest->w; x++) - for (y = 0; y < dest->h; y++) - api->putpixel(dest, x, y, api->getpixel(src, src->h - y - 1, x)); - } + { + for (x = 0; x < dest->w; x++) + for (y = 0; y < dest->h; y++) + api->putpixel(dest, x, y, api->getpixel(src, src->h - y - 1, x)); + } } void fretwork_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect) + SDL_Surface * canvas, SDL_Surface * snapshot, int x, + int y, SDL_Rect * update_rect) { int left_x, right_x, top_y, bottom_y; fretwork_segment_modified_last = 0; if (mode == MODE_PAINT) - { - fretwork_segment_last_clicked = fretwork_get_segment(x, y); - fretwork_drag(api, which, canvas, snapshot, x, y, x, y, update_rect); - } + { + fretwork_segment_last_clicked = fretwork_get_segment(x, y); + fretwork_drag(api, which, canvas, snapshot, x, y, x, y, update_rect); + } else + { + if (fretwork_full_runs <= + min(fretwork_segments_x, fretwork_segments_y) / 2) { - if (fretwork_full_runs <= min(fretwork_segments_x, fretwork_segments_y) / 2) - { - left_x = img_w * fretwork_full_runs; - right_x = img_w * fretwork_segments_x - img_w * fretwork_full_runs; - top_y = img_h * fretwork_full_runs; - bottom_y = img_h * fretwork_segments_y - img_h * (fretwork_full_runs - 1); + left_x = img_w * fretwork_full_runs; + right_x = img_w * fretwork_segments_x - img_w * fretwork_full_runs; + top_y = img_h * fretwork_full_runs; + bottom_y = + img_h * fretwork_segments_y - img_h * (fretwork_full_runs - 1); - //left line - api->line((void *)api, which, canvas, snapshot, left_x, top_y, left_x, bottom_y, img_w / 2, - fretwork_draw_wrapper); + //left line + api->line((void *) api, which, canvas, snapshot, left_x, top_y, left_x, + bottom_y, img_w / 2, fretwork_draw_wrapper); - //top line - api->line((void *)api, which, canvas, snapshot, left_x, top_y, right_x, top_y, img_w / 2, - fretwork_draw_wrapper); + //top line + api->line((void *) api, which, canvas, snapshot, left_x, top_y, right_x, + top_y, img_w / 2, fretwork_draw_wrapper); - //bottom line - api->line((void *)api, which, canvas, snapshot, left_x, bottom_y, right_x, bottom_y, img_w / 2, - fretwork_draw_wrapper); + //bottom line + api->line((void *) api, which, canvas, snapshot, left_x, bottom_y, + right_x, bottom_y, img_w / 2, fretwork_draw_wrapper); - //right line - api->line((void *)api, which, canvas, snapshot, right_x, top_y, right_x, bottom_y, img_w / 2, - fretwork_draw_wrapper); + //right line + api->line((void *) api, which, canvas, snapshot, right_x, top_y, + right_x, bottom_y, img_w / 2, fretwork_draw_wrapper); - fretwork_full_runs += 1; - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; - } + fretwork_full_runs += 1; + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; } + } } static Uint8 fretwork_select_image(Uint16 segment) @@ -378,44 +422,49 @@ static Uint8 fretwork_select_image(Uint16 segment) int take_up, take_down; int val_up, val_down, val_left, val_right; int from_top = 0, from_bottom = 0, from_left = 0, from_right = 0; - int from_top_right = 0, from_top_left = 0, from_bottom_right = 0, from_bottom_left = 0; + int from_top_right = 0, from_top_left = 0, from_bottom_right = + 0, from_bottom_left = 0; int TOP = 0, BOTTOM = 0, LEFT = 0, RIGHT = 0; //Checking from were we come... if (fretwork_segment_modified_last > 0) - { - if (segment == fretwork_segment_modified_last + 1) - from_left = 1; - else if (segment == fretwork_segment_modified_last - 1) - from_right = 1; - else if (segment == fretwork_segment_modified_last - fretwork_segments_x) - from_bottom = 1; - else if (segment == fretwork_segment_modified_last + fretwork_segments_x) - from_top = 1; + { + if (segment == fretwork_segment_modified_last + 1) + from_left = 1; + else if (segment == fretwork_segment_modified_last - 1) + from_right = 1; + else if (segment == fretwork_segment_modified_last - fretwork_segments_x) + from_bottom = 1; + else if (segment == fretwork_segment_modified_last + fretwork_segments_x) + from_top = 1; - // Very very few cases will reach this, segments are joining by the corner - // We need to add a new segment to join by side, adding clockwise - else if (segment == fretwork_segment_modified_last + fretwork_segments_x + 1) - { - from_top_left = 1; - fretwork_segment_to_add = segment - fretwork_segments_x; - } - else if (segment == fretwork_segment_modified_last + fretwork_segments_x - 1) - { - from_top_right = 1; - fretwork_segment_to_add = segment + 1; - } - else if (segment == fretwork_segment_modified_last - fretwork_segments_x - 1) - { - from_bottom_right = 1; - fretwork_segment_to_add = segment + fretwork_segments_x; - } - else if (segment == fretwork_segment_modified_last - fretwork_segments_x + 1) - { - from_bottom_left = 1; - fretwork_segment_to_add = segment - 1; - } + // Very very few cases will reach this, segments are joining by the corner + // We need to add a new segment to join by side, adding clockwise + else if (segment == + fretwork_segment_modified_last + fretwork_segments_x + 1) + { + from_top_left = 1; + fretwork_segment_to_add = segment - fretwork_segments_x; } + else if (segment == + fretwork_segment_modified_last + fretwork_segments_x - 1) + { + from_top_right = 1; + fretwork_segment_to_add = segment + 1; + } + else if (segment == + fretwork_segment_modified_last - fretwork_segments_x - 1) + { + from_bottom_right = 1; + fretwork_segment_to_add = segment + fretwork_segments_x; + } + else if (segment == + fretwork_segment_modified_last - fretwork_segments_x + 1) + { + from_bottom_left = 1; + fretwork_segment_to_add = segment - 1; + } + } take_up = segment - fretwork_segments_x; if (take_up <= 0) @@ -424,7 +473,7 @@ static Uint8 fretwork_select_image(Uint16 segment) val_up = fretwork_status_of_segments[take_up]; take_down = segment + fretwork_segments_x; - if (take_down > (signed)(fretwork_segments_x * fretwork_segments_y)) + if (take_down > (signed) (fretwork_segments_x * fretwork_segments_y)) val_down = SEG_NONE; else val_down = fretwork_status_of_segments[take_down]; @@ -440,9 +489,9 @@ static Uint8 fretwork_select_image(Uint16 segment) val_right = fretwork_status_of_segments[segment + 1]; if (from_left || (val_left & SEG_RIGHT) || from_bottom_left) - { - LEFT = 1; - } + { + LEFT = 1; + } if (from_right || (val_right & SEG_LEFT) || from_top_right) RIGHT = 1; if (from_top || (val_up & SEG_BOTTOM) || from_top_left) @@ -480,8 +529,10 @@ static Uint8 fretwork_select_image(Uint16 segment) } -static void fretwork_draw(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, - SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y ATTRIBUTE_UNUSED, unsigned int segment) +static void fretwork_draw(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, int x, + int y ATTRIBUTE_UNUSED, unsigned int segment) { magic_api *api = (magic_api *) ptr; SDL_Surface *result, *temp; @@ -491,7 +542,8 @@ static void fretwork_draw(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * c use_temp = 0; if ((segment < 1) | (segment > fretwork_segments_x * fretwork_segments_y)) return; - fretwork_extract_coords_from_segment(segment, &modification_rect.x, &modification_rect.y); + fretwork_extract_coords_from_segment(segment, &modification_rect.x, + &modification_rect.y); modification_rect.h = img_w; modification_rect.w = img_h; @@ -502,73 +554,81 @@ static void fretwork_draw(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * c fretwork_status_of_segments[segment] = image; //and write it to global table - result = SDL_CreateRGBSurface(SDL_SWSURFACE, img_w, img_h, fretwork_one->format->BitsPerPixel, - fretwork_one->format->Rmask, fretwork_one->format->Gmask, fretwork_one->format->Bmask, - fretwork_one->format->Amask); + result = + SDL_CreateRGBSurface(SDL_SWSURFACE, img_w, img_h, + fretwork_one->format->BitsPerPixel, + fretwork_one->format->Rmask, + fretwork_one->format->Gmask, + fretwork_one->format->Bmask, + fretwork_one->format->Amask); - temp = SDL_CreateRGBSurface(SDL_SWSURFACE, img_w, img_h, fretwork_one->format->BitsPerPixel, - fretwork_one->format->Rmask, fretwork_one->format->Gmask, fretwork_one->format->Bmask, - fretwork_one->format->Amask); + temp = + SDL_CreateRGBSurface(SDL_SWSURFACE, img_w, img_h, + fretwork_one->format->BitsPerPixel, + fretwork_one->format->Rmask, + fretwork_one->format->Gmask, + fretwork_one->format->Bmask, + fretwork_one->format->Amask); SDL_BlitSurface(canvas_backup, &modification_rect, result, NULL); switch (image) - { - case 0: - case SEG_TOP_BOTTOM: - SDL_BlitSurface(canvas_backup, &modification_rect, result, NULL); - SDL_BlitSurface(fretwork_one, NULL, result, NULL); - break; + { + case 0: + case SEG_TOP_BOTTOM: + SDL_BlitSurface(canvas_backup, &modification_rect, result, NULL); + SDL_BlitSurface(fretwork_one, NULL, result, NULL); + break; - case SEG_LEFT_RIGHT: - SDL_BlitSurface(canvas_backup, &modification_rect, result, NULL); - fretwork_rotate(api, temp, fretwork_one, 1); - use_temp = 1; - break; + case SEG_LEFT_RIGHT: + SDL_BlitSurface(canvas_backup, &modification_rect, result, NULL); + fretwork_rotate(api, temp, fretwork_one, 1); + use_temp = 1; + break; - case SEG_LEFT_RIGHT_TOP_BOTTOM: - SDL_BlitSurface(canvas_backup, &modification_rect, result, NULL); - SDL_BlitSurface(fretwork_four, NULL, result, NULL); - break; + case SEG_LEFT_RIGHT_TOP_BOTTOM: + SDL_BlitSurface(canvas_backup, &modification_rect, result, NULL); + SDL_BlitSurface(fretwork_four, NULL, result, NULL); + break; - case SEG_LEFT_RIGHT_TOP: - SDL_BlitSurface(fretwork_three, NULL, result, NULL); - break; + case SEG_LEFT_RIGHT_TOP: + SDL_BlitSurface(fretwork_three, NULL, result, NULL); + break; - case SEG_LEFT_RIGHT_BOTTOM: - fretwork_flip_flop(api, temp, fretwork_three); - use_temp = 1; - break; + case SEG_LEFT_RIGHT_BOTTOM: + fretwork_flip_flop(api, temp, fretwork_three); + use_temp = 1; + break; - case SEG_LEFT_TOP_BOTTOM: - fretwork_rotate(api, temp, fretwork_three, 0); - use_temp = 1; - break; + case SEG_LEFT_TOP_BOTTOM: + fretwork_rotate(api, temp, fretwork_three, 0); + use_temp = 1; + break; - case SEG_RIGHT_TOP_BOTTOM: - fretwork_rotate(api, temp, fretwork_three, 1); - use_temp = 1; - break; + case SEG_RIGHT_TOP_BOTTOM: + fretwork_rotate(api, temp, fretwork_three, 1); + use_temp = 1; + break; - case SEG_RIGHT_TOP: - SDL_BlitSurface(fretwork_corner, NULL, result, NULL); - break; + case SEG_RIGHT_TOP: + SDL_BlitSurface(fretwork_corner, NULL, result, NULL); + break; - case SEG_RIGHT_BOTTOM: - fretwork_rotate(api, temp, fretwork_corner, 1); - use_temp = 1; - break; + case SEG_RIGHT_BOTTOM: + fretwork_rotate(api, temp, fretwork_corner, 1); + use_temp = 1; + break; - case SEG_LEFT_TOP: - fretwork_rotate(api, temp, fretwork_corner, 0); - use_temp = 1; - break; + case SEG_LEFT_TOP: + fretwork_rotate(api, temp, fretwork_corner, 0); + use_temp = 1; + break; - case SEG_LEFT_BOTTOM: - fretwork_flip_flop(api, temp, fretwork_corner); - use_temp = 1; - break; - } + case SEG_LEFT_BOTTOM: + fretwork_flip_flop(api, temp, fretwork_corner); + use_temp = 1; + break; + } if (use_temp) SDL_BlitSurface(temp, NULL, result, NULL); @@ -580,58 +640,67 @@ static void fretwork_draw(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * c } -static void fretwork_draw_wrapper(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void fretwork_draw_wrapper(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y) { fretwork_segment_modified = fretwork_get_segment(x, y); - fretwork_draw((void *)ptr, which, canvas, last, x, y, fretwork_segment_modified); + fretwork_draw((void *) ptr, which, canvas, last, x, y, + fretwork_segment_modified); if (fretwork_segment_modified_last > 0) - { - fretwork_draw((void *)ptr, which, canvas, last, x, y, fretwork_segment_modified_last); - fretwork_extract_coords_from_segment(fretwork_segment_start_rectangle, &modification_rect.x, - &modification_rect.y); - modification_rect.w = fretwork_update_rectangle_width * img_w; - modification_rect.h = fretwork_update_rectangle_height * img_h; - } + { + fretwork_draw((void *) ptr, which, canvas, last, x, y, + fretwork_segment_modified_last); + fretwork_extract_coords_from_segment(fretwork_segment_start_rectangle, + &modification_rect.x, + &modification_rect.y); + modification_rect.w = fretwork_update_rectangle_width * img_w; + modification_rect.h = fretwork_update_rectangle_height * img_h; + } if (fretwork_segment_to_add > 0) - { - fretwork_draw((void *)ptr, which, canvas, last, x, y, fretwork_segment_to_add); - fretwork_draw((void *)ptr, which, canvas, last, x, y, fretwork_segment_modified_last); - fretwork_segment_to_add = 0; - } + { + fretwork_draw((void *) ptr, which, canvas, last, x, y, + fretwork_segment_to_add); + fretwork_draw((void *) ptr, which, canvas, last, x, y, + fretwork_segment_modified_last); + fretwork_segment_to_add = 0; + } fretwork_segment_modified_last = fretwork_segment_modified; } void fretwork_drag(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect) + SDL_Surface * canvas, SDL_Surface * snapshot, int ox, + int oy, int x, int y, SDL_Rect * update_rect) { int start_x, end_x, start_y, end_y, segment_start, segment_end, w, h; - if ((x < canvas->w) && (y < canvas->h) && (ox < canvas->w) && (oy < canvas->h) && ((signed)x > 0) && ((signed)y > 0) - && ((signed)ox > 0) && ((signed)oy > 0)) - { - api->line((void *)api, which, canvas, snapshot, ox, oy, x, y, img_w / 2, fretwork_draw_wrapper); - // This should be improved, maybe passed to fretwork_draw() - start_x = min(ox, x); - end_x = max(ox, x); - start_y = min(oy, y); - end_y = max(oy, y); - segment_start = fretwork_get_segment(start_x - img_w, start_y - img_h); - segment_end = fretwork_get_segment(end_x + img_w, end_y + img_h); + if ((x < canvas->w) && (y < canvas->h) && (ox < canvas->w) + && (oy < canvas->h) && ((signed) x > 0) && ((signed) y > 0) + && ((signed) ox > 0) && ((signed) oy > 0)) + { + api->line((void *) api, which, canvas, snapshot, ox, oy, x, y, img_w / 2, + fretwork_draw_wrapper); + // This should be improved, maybe passed to fretwork_draw() + start_x = min(ox, x); + end_x = max(ox, x); + start_y = min(oy, y); + end_y = max(oy, y); + segment_start = fretwork_get_segment(start_x - img_w, start_y - img_h); + segment_end = fretwork_get_segment(end_x + img_w, end_y + img_h); - x = ((segment_start % fretwork_segments_x) - 1) * img_w; - y = (int)(segment_start / fretwork_segments_x) * img_h; - w = ((segment_end % fretwork_segments_x) - 1) * img_w - x + img_w; - h = (int)(segment_end / fretwork_segments_x) * img_h - y + img_h; + x = ((segment_start % fretwork_segments_x) - 1) * img_w; + y = (int) (segment_start / fretwork_segments_x) * img_h; + w = ((segment_end % fretwork_segments_x) - 1) * img_w - x + img_w; + h = (int) (segment_end / fretwork_segments_x) * img_h - y + img_h; - update_rect->x = x; - update_rect->y = y; - update_rect->w = w; - update_rect->h = h; - } + update_rect->x = x; + update_rect->y = y; + update_rect->w = w; + update_rect->h = h; + } } diff --git a/magic/src/glasstile.c b/magic/src/glasstile.c index e400d09f6..b89eb6df0 100644 --- a/magic/src/glasstile.c +++ b/magic/src/glasstile.c @@ -46,18 +46,24 @@ SDL_Surface *glasstile_get_icon(magic_api * api, int which); char *glasstile_get_name(magic_api * api, int which); int glasstile_get_group(magic_api * api, int which); char *glasstile_get_description(magic_api * api, int which, int mode); -static void do_glasstile(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +static void do_glasstile(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); void glasstile_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 glasstile_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void glasstile_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 glasstile_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, + SDL_Rect * update_rect); void glasstile_shutdown(magic_api * api); void glasstile_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int glasstile_requires_colors(magic_api * api, int which); -void glasstile_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void glasstile_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void glasstile_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void glasstile_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int glasstile_modes(magic_api * api, int which); Uint32 glasstile_api_version(void) @@ -74,7 +80,8 @@ int glasstile_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/glasstile.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/glasstile.ogg", + api->data_directory); glasstile_snd = Mix_LoadWAV(fname); glasstile_hit = NULL; @@ -94,35 +101,45 @@ SDL_Surface *glasstile_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/glasstile.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/glasstile.png", + api->data_directory); return (IMG_Load(fname)); } // Return our names, localized: -char *glasstile_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *glasstile_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (strdup(gettext_noop("Glass Tile"))); } // Return our groups -int glasstile_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int glasstile_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_DISTORTS; } // Return our descriptions, localized: -char *glasstile_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode) +char *glasstile_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode) { if (mode == MODE_PAINT) - return (strdup(gettext_noop("Click and drag the mouse to put glass tile over your picture."))); + return (strdup + (gettext_noop + ("Click and drag the mouse to put glass tile over your picture."))); else - return (strdup(gettext_noop("Click to cover your entire picture in glass tiles."))); + return (strdup + (gettext_noop + ("Click to cover your entire picture in glass tiles."))); } // Do the effect: -static void do_glasstile(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void do_glasstile(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * last, int x, + int y) { magic_api *api = (magic_api *) ptr; int xx, yy, xl, xr, yt, yb; @@ -153,61 +170,66 @@ static void do_glasstile(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * ca /* Apply the effect: */ for (yy = -GT_SIZE; yy < GT_SIZE; yy = yy + 2) + { + for (xx = -GT_SIZE; xx < GT_SIZE; xx = xx + 2) { - for (xx = -GT_SIZE; xx < GT_SIZE; xx = xx + 2) - { - SDL_GetRGB(api->getpixel(last, x + xx, y + yy), last->format, &r1, &g1, &b1); - SDL_GetRGB(api->getpixel(last, x + xx + 1, y + yy), last->format, &r2, &g2, &b2); - SDL_GetRGB(api->getpixel(last, x + xx, y + yy + 1), last->format, &r3, &g3, &b3); - SDL_GetRGB(api->getpixel(last, x + xx + 1, y + yy + 1), last->format, &r4, &g4, &b4); + SDL_GetRGB(api->getpixel(last, x + xx, y + yy), last->format, &r1, &g1, + &b1); + SDL_GetRGB(api->getpixel(last, x + xx + 1, y + yy), last->format, &r2, + &g2, &b2); + SDL_GetRGB(api->getpixel(last, x + xx, y + yy + 1), last->format, &r3, + &g3, &b3); + SDL_GetRGB(api->getpixel(last, x + xx + 1, y + yy + 1), last->format, + &r4, &g4, &b4); - r = (r1 + r2 + r3 + r4) >> 2; - g = (g1 + g2 + g3 + g4) >> 2; - b = (b1 + b2 + b3 + b4) >> 2; + r = (r1 + r2 + r3 + r4) >> 2; + g = (g1 + g2 + g3 + g4) >> 2; + b = (b1 + b2 + b3 + b4) >> 2; - if (xx <= -GT_SIZE + 2 || yy == -GT_SIZE + 2) - { - r = min(255, r + 64); - g = min(255, g + 64); - b = min(255, b + 64); - } - else if (xx >= GT_SIZE - 3 || yy >= GT_SIZE - 3) - { - r = max(0, r - 64); - g = max(0, g - 64); - b = max(0, b - 64); - } + if (xx <= -GT_SIZE + 2 || yy == -GT_SIZE + 2) + { + r = min(255, r + 64); + g = min(255, g + 64); + b = min(255, b + 64); + } + else if (xx >= GT_SIZE - 3 || yy >= GT_SIZE - 3) + { + r = max(0, r - 64); + g = max(0, g - 64); + b = max(0, b - 64); + } - rgb = SDL_MapRGB(canvas->format, r, g, b); + rgb = SDL_MapRGB(canvas->format, r, g, b); - xl = (xx / 3) - GT_SIZE + (GT_SIZE / 3); - xr = (xx / 3) + (GT_SIZE * 2) / 3; - yt = (yy / 3) - GT_SIZE + (GT_SIZE / 3); - yb = (yy / 3) + (GT_SIZE * 2) / 3; + xl = (xx / 3) - GT_SIZE + (GT_SIZE / 3); + xr = (xx / 3) + (GT_SIZE * 2) / 3; + yt = (yy / 3) - GT_SIZE + (GT_SIZE / 3); + yb = (yy / 3) + (GT_SIZE * 2) / 3; - api->putpixel(canvas, x + xl, y + yt, rgb); - api->putpixel(canvas, x + xx / 2, y + yt, rgb); - api->putpixel(canvas, x + xr, y + yt, rgb); + api->putpixel(canvas, x + xl, y + yt, rgb); + api->putpixel(canvas, x + xx / 2, y + yt, rgb); + api->putpixel(canvas, x + xr, y + yt, rgb); - api->putpixel(canvas, x + xl, y + yy / 2, rgb); - api->putpixel(canvas, x + xr, y + yy / 2, rgb); + api->putpixel(canvas, x + xl, y + yy / 2, rgb); + api->putpixel(canvas, x + xr, y + yy / 2, rgb); - api->putpixel(canvas, x + xl, y + yb, rgb); - api->putpixel(canvas, x + xx / 2, y + yb, rgb); - api->putpixel(canvas, x + xr, y + yb, rgb); + api->putpixel(canvas, x + xl, y + yb, rgb); + api->putpixel(canvas, x + xx / 2, y + yb, rgb); + api->putpixel(canvas, x + xr, y + yb, rgb); - /* Center */ + /* Center */ - api->putpixel(canvas, x + xx / 2, y + yy / 2, rgb); - } + api->putpixel(canvas, x + xx / 2, y + yy / 2, rgb); } + } } // Affect the canvas on drag: void glasstile_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) { - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_glasstile); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, do_glasstile); update_rect->x = 0; update_rect->y = 0; @@ -240,20 +262,21 @@ void glasstile_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void glasstile_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) { int xx, yy; if (glasstile_hit == NULL) - { - glasstile_hit_ysize = (canvas->h / GT_SIZE) + 1; - glasstile_hit_xsize = (canvas->w / GT_SIZE) + 1; + { + glasstile_hit_ysize = (canvas->h / GT_SIZE) + 1; + glasstile_hit_xsize = (canvas->w / GT_SIZE) + 1; - glasstile_hit = (int * *)malloc(sizeof(int *) * glasstile_hit_ysize); + glasstile_hit = (int * *) malloc(sizeof(int *) * glasstile_hit_ysize); - for (yy = 0; yy < glasstile_hit_ysize; yy++) - glasstile_hit[yy] = (int *)malloc(sizeof(int) * glasstile_hit_xsize); - } + for (yy = 0; yy < glasstile_hit_ysize; yy++) + glasstile_hit[yy] = (int *) malloc(sizeof(int) * glasstile_hit_xsize); + } for (yy = 0; yy < glasstile_hit_ysize; yy++) for (xx = 0; xx < glasstile_hit_xsize; xx++) @@ -262,24 +285,27 @@ void glasstile_click(magic_api * api, int which, int mode, if (mode == MODE_PAINT) glasstile_drag(api, which, canvas, last, x, y, x, y, update_rect); else if (mode == MODE_FULLSCREEN) - { - for (y = 0; y < canvas->h; y = y + GT_SIZE) - for (x = 0; x < canvas->w; x = x + GT_SIZE) - do_glasstile(api, which, canvas, last, x, y); + { + for (y = 0; y < canvas->h; y = y + GT_SIZE) + for (x = 0; x < canvas->w; x = x + GT_SIZE) + do_glasstile(api, which, canvas, last, x, y); - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; - api->playsound(glasstile_snd, 128, 255); - } + api->playsound(glasstile_snd, 128, 255); + } } // Affect the canvas on release: -void glasstile_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) +void glasstile_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) { } @@ -292,39 +318,45 @@ void glasstile_shutdown(magic_api * api ATTRIBUTE_UNUSED) Mix_FreeChunk(glasstile_snd); if (glasstile_hit != NULL) + { + for (y = 0; y < glasstile_hit_ysize; y++) { - for (y = 0; y < glasstile_hit_ysize; y++) - { - if (glasstile_hit[y] != NULL) - free(glasstile_hit[y]); - } - free(glasstile_hit); + if (glasstile_hit[y] != NULL) + free(glasstile_hit[y]); } + free(glasstile_hit); + } } // Record the color from Tux Paint: -void glasstile_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, +void glasstile_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) { } // Use colors: -int glasstile_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int glasstile_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void glasstile_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void glasstile_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void glasstile_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void glasstile_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -int glasstile_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int glasstile_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_PAINT | MODE_FULLSCREEN); } diff --git a/magic/src/grass.c b/magic/src/grass.c index 22071ac3d..bd079f81b 100644 --- a/magic/src/grass.c +++ b/magic/src/grass.c @@ -50,18 +50,22 @@ char *grass_get_name(magic_api * api, int which); int grass_get_group(magic_api * api, int which); char *grass_get_description(magic_api * api, int which, int mode); void grass_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void grass_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void grass_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void grass_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); +void grass_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); void grass_shutdown(magic_api * api); void grass_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int grass_requires_colors(magic_api * api, int which); -static void do_grass(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +static void do_grass(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); static int log2int(int x); -void grass_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void grass_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void grass_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void grass_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int grass_modes(magic_api * api, int which); @@ -70,10 +74,12 @@ int grass_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/grass.wav", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/grass.wav", + api->data_directory); grass_snd = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%simages/magic/grass_data.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/grass_data.png", + api->data_directory); img_grass = IMG_Load(fname); return (1); @@ -97,50 +103,58 @@ SDL_Surface *grass_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/grass.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/grass.png", + api->data_directory); return (IMG_Load(fname)); } // Return our names, localized: -char *grass_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *grass_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (strdup(gettext_noop("Grass"))); } // Return our groups: -int grass_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int grass_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PAINTING; } // Return our descriptions, localized: -char *grass_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *grass_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED) { - return (strdup(gettext_noop("Click and drag to draw grass. Don’t forget the dirt!"))); + return (strdup + (gettext_noop + ("Click and drag to draw grass. Don’t forget the dirt!"))); } // Affect the canvas on drag: void grass_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) { - api->line((void *)api, which, canvas, last, ox, oy, x, y, 4, do_grass); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 4, do_grass); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = ox - 64; update_rect->y = oy - 64; @@ -152,14 +166,18 @@ void grass_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void grass_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) { grass_drag(api, which, canvas, last, x, y, x, y, update_rect); } -void grass_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) +void grass_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) { } @@ -171,7 +189,8 @@ void grass_shutdown(magic_api * api ATTRIBUTE_UNUSED) } // Record the color from Tux Paint: -void grass_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) +void grass_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, + Uint8 b) { grass_r = r; grass_g = g; @@ -179,13 +198,15 @@ void grass_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b } // Use colors: -int grass_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int grass_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 1; } static void do_grass(void *ptr, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) + SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { magic_api *api = (magic_api *) ptr; int xx, yy; @@ -198,50 +219,63 @@ static void do_grass(void *ptr, int which ATTRIBUTE_UNUSED, if (!api->button_down()) bucket = 0; - bucket += (3.5 + (rand() / (double)RAND_MAX)) * 7.0; + bucket += (3.5 + (rand() / (double) RAND_MAX)) * 7.0; while (bucket >= 0) + { + int rank = + log2int(((double) y / canvas->h) * + (0.99 + (rand() / (double) RAND_MAX)) * 64); + int ah = 1 << rank; + + bucket -= ah; + src.x = (rand() % 4) * 64; + src.y = ah; + src.w = 64; + src.h = ah; + + dest.x = x - 32; + dest.y = y - 30 + (int) ((rand() / (double) RAND_MAX) * 30); + + tmp_red = + api->sRGB_to_linear(grass_r) * 2.0 + (rand() / (double) RAND_MAX); + tmp_green = + api->sRGB_to_linear(grass_g) * 2.0 + (rand() / (double) RAND_MAX); + tmp_blue = api->sRGB_to_linear(grass_b) * 2.0 + api->sRGB_to_linear(17); + + for (yy = 0; yy < ah; yy++) { - int rank = log2int(((double)y / canvas->h) * (0.99 + (rand() / (double)RAND_MAX)) * 64); - int ah = 1 << rank; + for (xx = 0; xx < 64; xx++) + { + double rd, gd, bd; - bucket -= ah; - src.x = (rand() % 4) * 64; - src.y = ah; - src.w = 64; - src.h = ah; + SDL_GetRGBA(api->getpixel(img_grass, xx + src.x, yy + src.y), + img_grass->format, &r, &g, &b, &a); - dest.x = x - 32; - dest.y = y - 30 + (int)((rand() / (double)RAND_MAX) * 30); + rd = api->sRGB_to_linear(r) * 8.0 + tmp_red; + rd = rd * (a / 255.0) / 11.0; + gd = api->sRGB_to_linear(g) * 8.0 + tmp_green; + gd = gd * (a / 255.0) / 11.0; + bd = api->sRGB_to_linear(b) * 8.0 + tmp_blue; + bd = bd * (a / 255.0) / 11.0; - tmp_red = api->sRGB_to_linear(grass_r) * 2.0 + (rand() / (double)RAND_MAX); - tmp_green = api->sRGB_to_linear(grass_g) * 2.0 + (rand() / (double)RAND_MAX); - tmp_blue = api->sRGB_to_linear(grass_b) * 2.0 + api->sRGB_to_linear(17); + SDL_GetRGB(api->getpixel(canvas, xx + dest.x, yy + dest.y), + canvas->format, &r, &g, &b); - for (yy = 0; yy < ah; yy++) - { - for (xx = 0; xx < 64; xx++) - { - double rd, gd, bd; + r = + api->linear_to_sRGB(api->sRGB_to_linear(r) * (1.0 - a / 255.0) + + rd); + g = + api->linear_to_sRGB(api->sRGB_to_linear(g) * (1.0 - a / 255.0) + + gd); + b = + api->linear_to_sRGB(api->sRGB_to_linear(b) * (1.0 - a / 255.0) + + bd); - SDL_GetRGBA(api->getpixel(img_grass, xx + src.x, yy + src.y), img_grass->format, &r, &g, &b, &a); - - rd = api->sRGB_to_linear(r) * 8.0 + tmp_red; - rd = rd * (a / 255.0) / 11.0; - gd = api->sRGB_to_linear(g) * 8.0 + tmp_green; - gd = gd * (a / 255.0) / 11.0; - bd = api->sRGB_to_linear(b) * 8.0 + tmp_blue; - bd = bd * (a / 255.0) / 11.0; - - SDL_GetRGB(api->getpixel(canvas, xx + dest.x, yy + dest.y), canvas->format, &r, &g, &b); - - r = api->linear_to_sRGB(api->sRGB_to_linear(r) * (1.0 - a / 255.0) + rd); - g = api->linear_to_sRGB(api->sRGB_to_linear(g) * (1.0 - a / 255.0) + gd); - b = api->linear_to_sRGB(api->sRGB_to_linear(b) * (1.0 - a / 255.0) + bd); - - api->putpixel(canvas, xx + dest.x, yy + dest.y, SDL_MapRGB(canvas->format, r, g, b)); - } - } + api->putpixel(canvas, xx + dest.x, yy + dest.y, + SDL_MapRGB(canvas->format, r, g, b)); + } } + } } // this one rounds down @@ -253,19 +287,21 @@ static int log2int(int x) return 0; x >>= 1; while (x) - { - x >>= 1; - y++; - } + { + x >>= 1; + y++; + } return y; } -void grass_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void grass_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void grass_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void grass_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } diff --git a/magic/src/halftone.c b/magic/src/halftone.c index 801c279cf..b82bc71dc 100644 --- a/magic/src/halftone.c +++ b/magic/src/halftone.c @@ -19,8 +19,8 @@ #define deg_cos(x) cos((x) * M_PI / 180.0) #define deg_sin(x) sin((x) * M_PI / 180.0) -#define GRID_SIZE 16 /* Size of the grid, and hence max size of the circle (it may fill more, into a square shape) */ -#define OFFSET_RADIUS 2 /* Radius for when offsetting C, M, Y, and K colors by their angles (see `chan_angles[]`) */ +#define GRID_SIZE 16 /* Size of the grid, and hence max size of the circle (it may fill more, into a square shape) */ +#define OFFSET_RADIUS 2 /* Radius for when offsetting C, M, Y, and K colors by their angles (see `chan_angles[]`) */ enum { @@ -47,9 +47,9 @@ const int groups[NUM_TOOLS] = { const char *descs[NUM_TOOLS][2] = { { - gettext_noop("Click and drag to turn your drawing into a newspaper."), - gettext_noop("Click to turn your drawing into a newspaper."), - }, + gettext_noop("Click and drag to turn your drawing into a newspaper."), + gettext_noop("Click to turn your drawing into a newspaper."), + }, }; Mix_Chunk *snd_effect[NUM_TOOLS]; @@ -59,8 +59,10 @@ static SDL_Surface *canvas_backup, *square; /* Function Prototypes: */ void halftone_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void halftone_line_callback(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y); + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void halftone_line_callback(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y); Uint32 halftone_api_version(void); int halftone_init(magic_api * api); int halftone_get_tool_count(magic_api * api); @@ -72,12 +74,16 @@ int halftone_requires_colors(magic_api * api, int which); int halftone_modes(magic_api * api, int which); void halftone_shutdown(magic_api * api); void halftone_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); -void halftone_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); + SDL_Surface * canvas, SDL_Surface * snapshot, int x, + int y, SDL_Rect * update_rect); +void halftone_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect); void halftone_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); -void halftone_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void halftone_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void halftone_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void halftone_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); void halftone_rgb2cmyk(Uint8 r, Uint8 g, Uint8 b, float cmyk[]); Uint32 halftone_api_version(void) @@ -94,11 +100,12 @@ int halftone_init(magic_api * api) square = NULL; for (i = 0; i < NUM_TOOLS; i++) - { - snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, snd_filenames[i]); + { + snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, + snd_filenames[i]); - snd_effect[i] = Mix_LoadWAV(fname); - } + snd_effect[i] = Mix_LoadWAV(fname); + } return (1); @@ -113,7 +120,8 @@ SDL_Surface *halftone_get_icon(magic_api * api, int which) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, icon_filenames[which]); + snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, + icon_filenames[which]); return (IMG_Load(fname)); } @@ -134,7 +142,8 @@ int halftone_get_group(magic_api * api ATTRIBUTE_UNUSED, int which) return groups[which]; } -char *halftone_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode) +char *halftone_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, + int mode) { const char *our_desc_english; const char *our_desc_localized; @@ -145,12 +154,14 @@ char *halftone_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int return (strdup(our_desc_localized)); } -int halftone_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int halftone_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -int halftone_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int halftone_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_PAINT | MODE_FULLSCREEN); } @@ -159,8 +170,10 @@ void halftone_shutdown(magic_api * api ATTRIBUTE_UNUSED) { int i; - for (i = 0; i < NUM_TOOLS; i++) { - if (snd_effect[i] != NULL) { + for (i = 0; i < NUM_TOOLS; i++) + { + if (snd_effect[i] != NULL) + { Mix_FreeChunk(snd_effect[i]); } } @@ -170,50 +183,53 @@ void halftone_shutdown(magic_api * api ATTRIBUTE_UNUSED) } void halftone_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect) + SDL_Surface * canvas, SDL_Surface * snapshot, int x, + int y, SDL_Rect * update_rect) { int full_x, full_y; if (mode == MODE_PAINT) - { - halftone_drag(api, which, canvas, snapshot, x, y, x, y, update_rect); - } + { + halftone_drag(api, which, canvas, snapshot, x, y, x, y, update_rect); + } else + { + for (full_y = 0; full_y < canvas->h; full_y += GRID_SIZE) { - for (full_y = 0; full_y < canvas->h; full_y += GRID_SIZE) - { - for (full_x = 0; full_x < canvas->w; full_x += GRID_SIZE) - { - halftone_line_callback(api, which, canvas, snapshot, full_x, full_y); - } - } - api->playsound(snd_effect[which], 128, 255); - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; + for (full_x = 0; full_x < canvas->w; full_x += GRID_SIZE) + { + halftone_line_callback(api, which, canvas, snapshot, full_x, full_y); + } } + api->playsound(snd_effect[which], 128, 255); + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; + } } void halftone_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect) + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect) { - api->line((void *)api, which, canvas, snapshot, ox, oy, x, y, 4, halftone_line_callback); + api->line((void *) api, which, canvas, snapshot, ox, oy, x, y, 4, + halftone_line_callback); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } ox = (ox / GRID_SIZE) * GRID_SIZE + (GRID_SIZE / 2); oy = (oy / GRID_SIZE) * GRID_SIZE + (GRID_SIZE / 2); @@ -239,33 +255,39 @@ enum }; Uint8 chan_colors[NUM_CHANS][3] = { - {0, 255, 255}, /* Cyan */ - {255, 0, 255}, /* Magenta */ - {255, 255, 0}, /* Yellow */ - {0, 0, 0} /* Black */ + {0, 255, 255}, /* Cyan */ + {255, 0, 255}, /* Magenta */ + {255, 255, 0}, /* Yellow */ + {0, 0, 0} /* Black */ }; int chan_angles[NUM_CHANS] = { - 75, /* Cyan */ - 15, /* Magenta */ - 90, /* Yellow */ - 45 /* Black */ + 75, /* Cyan */ + 15, /* Magenta */ + 90, /* Yellow */ + 45 /* Black */ }; -void halftone_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * snapshot ATTRIBUTE_UNUSED, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) +void halftone_release(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED, + SDL_Surface * snapshot ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, + SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } -void halftone_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, - Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) +void halftone_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, + Uint8 b ATTRIBUTE_UNUSED) { } void halftone_line_callback(void *ptr, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * snapshot ATTRIBUTE_UNUSED, int x, int y) + SDL_Surface * canvas, + SDL_Surface * snapshot ATTRIBUTE_UNUSED, int x, + int y) { Uint8 r, g, b, or, og, ob; Uint32 total_r, total_g, total_b; @@ -285,24 +307,25 @@ void halftone_line_callback(void *ptr, int which ATTRIBUTE_UNUSED, y = (y / GRID_SIZE) * GRID_SIZE + (GRID_SIZE / 2); if (api->touched(x, y)) - { - return; - } + { + return; + } /* Get the average color around the mouse */ total_r = total_g = total_b = 0; px_cnt = 0; for (xxx = -(GRID_SIZE / 2); xxx < (GRID_SIZE / 2); xxx++) + { + for (yyy = -(GRID_SIZE / 2); yyy < (GRID_SIZE / 2); yyy++) { - for (yyy = -(GRID_SIZE / 2); yyy < (GRID_SIZE / 2); yyy++) - { - SDL_GetRGB(api->getpixel(canvas_backup, x + xxx, y + yyy), canvas_backup->format, &r, &g, &b); - total_r += r; - total_g += g; - total_b += b; - px_cnt++; - } + SDL_GetRGB(api->getpixel(canvas_backup, x + xxx, y + yyy), + canvas_backup->format, &r, &g, &b); + total_r += r; + total_g += g; + total_b += b; + px_cnt++; } + } total_r /= px_cnt; total_g /= px_cnt; @@ -314,42 +337,42 @@ void halftone_line_callback(void *ptr, int which ATTRIBUTE_UNUSED, /* Draw C, M, Y and K blobs into our 'square' surface */ for (channel = 0; channel < NUM_CHANS; channel++) + { + for (xxx = -(GRID_SIZE / 2) - 1; xxx < (GRID_SIZE / 2) + 1; xxx++) { - for (xxx = -(GRID_SIZE / 2) - 1; xxx < (GRID_SIZE / 2) + 1; xxx++) + for (yyy = -(GRID_SIZE / 2) - 1; yyy < (GRID_SIZE / 2) + 1; yyy++) + { + /* A circle blob, radius based upon channel (C, M, Y or K) strength for this color */ + + ox = xxx + deg_cos(chan_angles[channel]) * OFFSET_RADIUS; + oy = yyy + deg_sin(chan_angles[channel]) * OFFSET_RADIUS; + + sqx = ((GRID_SIZE / 2) + ox) % GRID_SIZE; + sqy = ((GRID_SIZE / 2) + oy) % GRID_SIZE; + + /* Use intensity of the CMKY channel in question to decide + how big of a circle to paint */ + if (api->in_circle(xxx, yyy, cmyk[channel] * GRID_SIZE)) { - for (yyy = -(GRID_SIZE / 2) - 1; yyy < (GRID_SIZE / 2) + 1; yyy++) - { - /* A circle blob, radius based upon channel (C, M, Y or K) strength for this color */ + /* Use the pure C, Y, M, or K color to paint with */ + r = chan_colors[channel][0]; + g = chan_colors[channel][1]; + b = chan_colors[channel][2]; - ox = xxx + deg_cos(chan_angles[channel]) * OFFSET_RADIUS; - oy = yyy + deg_sin(chan_angles[channel]) * OFFSET_RADIUS; - - sqx = ((GRID_SIZE / 2) + ox) % GRID_SIZE; - sqy = ((GRID_SIZE / 2) + oy) % GRID_SIZE; - - /* Use intensity of the CMKY channel in question to decide - how big of a circle to paint */ - if (api->in_circle(xxx, yyy, cmyk[channel] * GRID_SIZE)) - { - /* Use the pure C, Y, M, or K color to paint with */ - r = chan_colors[channel][0]; - g = chan_colors[channel][1]; - b = chan_colors[channel][2]; - - /* Additively blend with whatever we have in the - 'square' buffer (which starts as white) - (since the target is RGB, we use `min()`) */ - SDL_GetRGB(api->getpixel(square, sqx, sqy), square->format, &or, &og, &ob); - pixel = SDL_MapRGB(square->format, - min((Uint8) (r * 2.0), or), - min((Uint8) (g * 2.0), og), - min((Uint8) (b * 2.0), ob) - ); - api->putpixel(square, sqx, sqy, pixel); - } - } + /* Additively blend with whatever we have in the + 'square' buffer (which starts as white) + (since the target is RGB, we use `min()`) */ + SDL_GetRGB(api->getpixel(square, sqx, sqy), square->format, &or, + &og, &ob); + pixel = + SDL_MapRGB(square->format, min((Uint8) (r * 2.0), or), + min((Uint8) (g * 2.0), og), min((Uint8) (b * 2.0), + ob)); + api->putpixel(square, sqx, sqy, pixel); } + } } + } /* Copy the results to the canvas */ dest.x = x - GRID_SIZE / 2; @@ -360,27 +383,33 @@ void halftone_line_callback(void *ptr, int which ATTRIBUTE_UNUSED, SDL_BlitSurface(square, NULL, canvas, &dest); } -void halftone_switchin(magic_api * api, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas) +void halftone_switchin(magic_api * api, int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas) { if (canvas_backup == NULL) - { - canvas_backup = - SDL_CreateRGBSurface(SDL_SWSURFACE, api->canvas_w, api->canvas_h, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, canvas->format->Amask); - } + { + canvas_backup = + SDL_CreateRGBSurface(SDL_SWSURFACE, api->canvas_w, api->canvas_h, + canvas->format->BitsPerPixel, + canvas->format->Rmask, canvas->format->Gmask, + canvas->format->Bmask, canvas->format->Amask); + } if (square == NULL) - { - square = - SDL_CreateRGBSurface(SDL_SWSURFACE, GRID_SIZE, GRID_SIZE, canvas->format->BitsPerPixel, canvas->format->Rmask, - canvas->format->Gmask, canvas->format->Bmask, canvas->format->Amask); - } + { + square = + SDL_CreateRGBSurface(SDL_SWSURFACE, GRID_SIZE, GRID_SIZE, + canvas->format->BitsPerPixel, + canvas->format->Rmask, canvas->format->Gmask, + canvas->format->Bmask, canvas->format->Amask); + } SDL_BlitSurface(canvas, NULL, canvas_backup, NULL); } -void halftone_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void halftone_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } @@ -393,25 +422,25 @@ void halftone_rgb2cmyk(Uint8 r, Uint8 g, Uint8 b, float cmyk[]) by Alexei Kourbatov */ if (r == 0 && g == 0 && b == 0) - { - /* Black */ - c = 0.0; - m = 0.0; - y = 0.0; - k = 1.0; - } + { + /* Black */ + c = 0.0; + m = 0.0; + y = 0.0; + k = 1.0; + } else - { - c = 1.0 - (((float)r) / 255.0); - m = 1.0 - (((float)g) / 255.0); - y = 1.0 - (((float)b) / 255.0); + { + c = 1.0 - (((float) r) / 255.0); + m = 1.0 - (((float) g) / 255.0); + y = 1.0 - (((float) b) / 255.0); - mincmy = min(c, min(m, y)); - c = (c - mincmy) / (1.0 - mincmy); - m = (m - mincmy) / (1.0 - mincmy); - y = (y - mincmy) / (1.0 - mincmy); - k = mincmy; - } + mincmy = min(c, min(m, y)); + c = (c - mincmy) / (1.0 - mincmy); + m = (m - mincmy) / (1.0 - mincmy); + y = (y - mincmy) / (1.0 - mincmy); + k = mincmy; + } cmyk[0] = c; cmyk[1] = m; diff --git a/magic/src/kalidescope.c b/magic/src/kalidescope.c index 0cac7a8db..c420cf3c1 100644 --- a/magic/src/kalidescope.c +++ b/magic/src/kalidescope.c @@ -67,18 +67,24 @@ SDL_Surface *kalidescope_get_icon(magic_api * api, int which); char *kalidescope_get_name(magic_api * api, int which); int kalidescope_get_group(magic_api * api, int which); char *kalidescope_get_description(magic_api * api, int which, int mode); -static void do_kalidescope(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +static void do_kalidescope(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); void kalidescope_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 kalidescope_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void kalidescope_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 kalidescope_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, + SDL_Rect * update_rect); void kalidescope_shutdown(magic_api * api); int kalidescope_requires_colors(magic_api * api, int which); void kalidescope_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); -void kalidescope_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void kalidescope_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void kalidescope_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void kalidescope_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int kalidescope_modes(magic_api * api, int which); Uint32 kalidescope_api_version(void) @@ -91,7 +97,8 @@ int kalidescope_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/kaleidoscope.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/kaleidoscope.ogg", + api->data_directory); kalidescope_snd = Mix_LoadWAV(fname); return (1); @@ -107,7 +114,8 @@ SDL_Surface *kalidescope_get_icon(magic_api * api, int which) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, kal_icon_names[which]); + snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, + kal_icon_names[which]); return (IMG_Load(fname)); } @@ -116,61 +124,69 @@ SDL_Surface *kalidescope_get_icon(magic_api * api, int which) char *kalidescope_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) { if (which == KAL_LR) - { - return (strdup(gettext_noop("Symmetric Left/Right"))); - } + { + return (strdup(gettext_noop("Symmetric Left/Right"))); + } else if (which == KAL_UD) - { - return (strdup(gettext_noop("Symmetric Up/Down"))); - } + { + return (strdup(gettext_noop("Symmetric Up/Down"))); + } else if (which == KAL_PATTERN) - { - return (strdup(gettext_noop("Pattern"))); - } + { + return (strdup(gettext_noop("Pattern"))); + } else if (which == KAL_TILES) - { - return (strdup(gettext_noop("Tiles"))); - } + { + return (strdup(gettext_noop("Tiles"))); + } else - { /* KAL_BOTH */ - return (strdup(gettext_noop("Kaleidoscope"))); - } + { /* KAL_BOTH */ + return (strdup(gettext_noop("Kaleidoscope"))); + } } // Return our group (all the same): -int kalidescope_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int kalidescope_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PATTERN_PAINTING; } // Return our descriptions, localized: -char *kalidescope_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode ATTRIBUTE_UNUSED) +char *kalidescope_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, + int mode ATTRIBUTE_UNUSED) { if (which == KAL_LR) - { - return (strdup - (gettext_noop - ("Click and drag the mouse to draw with two brushes that are symmetric across the left and right of your picture."))); - } + { + return (strdup + (gettext_noop + ("Click and drag the mouse to draw with two brushes that are symmetric across the left and right of your picture."))); + } else if (which == KAL_UD) - { - return (strdup - (gettext_noop - ("Click and drag the mouse to draw with two brushes that are symmetric across the top and bottom of your picture."))); - } + { + return (strdup + (gettext_noop + ("Click and drag the mouse to draw with two brushes that are symmetric across the top and bottom of your picture."))); + } else if (which == KAL_PATTERN) - { - return (strdup(gettext_noop("Click and drag the mouse to draw a pattern across the picture."))); - } + { + return (strdup + (gettext_noop + ("Click and drag the mouse to draw a pattern across the picture."))); + } else if (which == KAL_TILES) - { - return (strdup(gettext_noop("Click and drag the mouse to draw a pattern that is symmetric across the picture."))); - } + { + return (strdup + (gettext_noop + ("Click and drag the mouse to draw a pattern that is symmetric across the picture."))); + } else - { /* KAL_BOTH */ - return (strdup(gettext_noop("Click and drag the mouse to draw with symmetric brushes (a kaleidoscope)."))); - } + { /* KAL_BOTH */ + return (strdup + (gettext_noop + ("Click and drag the mouse to draw with symmetric brushes (a kaleidoscope)."))); + } } // Do the effect: @@ -183,49 +199,55 @@ static void do_kalidescope(void *ptr, int which, SDL_Surface * canvas, int i, j; Uint32 colr; - colr = SDL_MapRGB(canvas->format, kalidescope_r, kalidescope_g, kalidescope_b); + colr = + SDL_MapRGB(canvas->format, kalidescope_r, kalidescope_g, kalidescope_b); for (yy = -8; yy < 8; yy++) + { + for (xx = -8; xx < 8; xx++) { - for (xx = -8; xx < 8; xx++) + if (api->in_circle(xx, yy, 8)) + { + api->putpixel(canvas, x + xx, y + yy, colr); + + if (which == KAL_LR || which == KAL_BOTH) { - if (api->in_circle(xx, yy, 8)) + api->putpixel(canvas, canvas->w - 1 - x + xx, y + yy, colr); + + if (which == KAL_BOTH) + { + api->putpixel(canvas, canvas->w - 1 - x + xx, + canvas->h - 1 - y + yy, colr); + } + } + if (which == KAL_UD || which == KAL_BOTH) + { + api->putpixel(canvas, x + xx, canvas->h - 1 - y + yy, colr); + } + if (which == KAL_PATTERN || which == KAL_TILES) + { + for (i = 0; i <= canvas->w; i += square_size) + for (j = 0; j <= canvas->h; j += square_size) { - api->putpixel(canvas, x + xx, y + yy, colr); - - if (which == KAL_LR || which == KAL_BOTH) - { - api->putpixel(canvas, canvas->w - 1 - x + xx, y + yy, colr); - - if (which == KAL_BOTH) - { - api->putpixel(canvas, canvas->w - 1 - x + xx, canvas->h - 1 - y + yy, colr); - } - } - if (which == KAL_UD || which == KAL_BOTH) - { - api->putpixel(canvas, x + xx, canvas->h - 1 - y + yy, colr); - } - if (which == KAL_PATTERN || which == KAL_TILES) - { - for (i = 0; i <= canvas->w; i += square_size) - for (j = 0; j <= canvas->h; j += square_size) - { - api->putpixel(canvas, i + xx + x % square_size, j + yy + y % square_size, colr); - if (which == KAL_TILES) - api->putpixel(canvas, i + yy + y % square_size, j + xx + x % square_size, colr); - } - } + api->putpixel(canvas, i + xx + x % square_size, + j + yy + y % square_size, colr); + if (which == KAL_TILES) + api->putpixel(canvas, i + yy + y % square_size, + j + xx + x % square_size, colr); } } + } } + } } // Affect the canvas on drag: void kalidescope_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) { - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_kalidescope); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, + do_kalidescope); update_rect->x = 0; update_rect->y = 0; @@ -237,15 +259,18 @@ void kalidescope_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void kalidescope_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) { kalidescope_drag(api, which, canvas, last, x, y, x, y, update_rect); } // Affect the canvas on release: void kalidescope_release(magic_api * api, 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) { api->stopsound(); } @@ -258,7 +283,8 @@ void kalidescope_shutdown(magic_api * api ATTRIBUTE_UNUSED) } // Record the color from Tux Paint: -void kalidescope_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) +void kalidescope_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, + Uint8 b) { kalidescope_r = r; kalidescope_g = g; @@ -266,22 +292,28 @@ void kalidescope_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, U } // Use colors: -int kalidescope_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int kalidescope_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 1; } -void kalidescope_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void kalidescope_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void kalidescope_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void kalidescope_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -int kalidescope_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int kalidescope_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_PAINT); } diff --git a/magic/src/light.c b/magic/src/light.c index e4d77938e..f1515a8d3 100644 --- a/magic/src/light.c +++ b/magic/src/light.c @@ -47,18 +47,22 @@ SDL_Surface *light_get_icon(magic_api * api, int which); char *light_get_name(magic_api * api, int which); int light_get_group(magic_api * api, int which); char *light_get_description(magic_api * api, int which, int mode); -static void do_light(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +static void do_light(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); void light_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void light_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void light_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void light_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); +void light_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); void light_shutdown(magic_api * api); void light_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int light_requires_colors(magic_api * api, int which); -void light_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void light_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void light_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void light_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int light_modes(magic_api * api, int which); @@ -73,10 +77,12 @@ int light_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/light1.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/light1.ogg", + api->data_directory); light1_snd = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%ssounds/magic/light2.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/light2.ogg", + api->data_directory); light2_snd = Mix_LoadWAV(fname); return (1); @@ -93,33 +99,41 @@ SDL_Surface *light_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/light.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/light.png", + api->data_directory); return (IMG_Load(fname)); } // Return our names, localized: -char *light_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *light_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (strdup(gettext_noop("Light"))); } // Return our groups: -int light_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int light_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PAINTING; } // Return our descriptions, localized: -char *light_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *light_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 beam of light on your picture."))); + return (strdup + (gettext_noop + ("Click and drag to draw a beam of light on your picture."))); } // Do the effect: -static void do_light(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, - int x, int y) +static void do_light(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { magic_api *api = (magic_api *) ptr; int xx, yy; @@ -129,74 +143,76 @@ static void do_light(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas float adj; for (yy = -8; yy < 8; yy++) + { + for (xx = -8; xx < 8; xx++) { - for (xx = -8; xx < 8; xx++) + if (api->in_circle(xx, yy, 8)) + { + pix = api->getpixel(canvas, x + xx, y + yy); + + SDL_GetRGB(pix, canvas->format, &r, &g, &b); + + adj = (7.99 - sqrt(abs(xx * yy))) / 128.0; + + api->rgbtohsv(r, g, b, &h, &s, &v); + + v = min((float) 1.0, v + adj); + + if (light_h == -1 && h == -1) { - if (api->in_circle(xx, yy, 8)) - { - pix = api->getpixel(canvas, x + xx, y + yy); - - SDL_GetRGB(pix, canvas->format, &r, &g, &b); - - adj = (7.99 - sqrt(abs(xx * yy))) / 128.0; - - api->rgbtohsv(r, g, b, &h, &s, &v); - - v = min((float)1.0, v + adj); - - if (light_h == -1 && h == -1) - { - new_h = -1; - new_s = 0; - new_v = v; - } - else if (light_h == -1) - { - new_h = h; - new_s = max(0.0, s - adj / 2.0); - new_v = v; - } - else if (h == -1) - { - new_h = light_h; - new_s = max(0.0, light_s - adj / 2.0); - new_v = v; - } - else - { - new_h = (light_h + h) / 2; - new_s = max(0.0, s - adj / 2.0); - new_v = v; - } - - api->hsvtorgb(new_h, new_s, new_v, &r, &g, &b); - - api->putpixel(canvas, x + xx, y + yy, SDL_MapRGB(canvas->format, r, g, b)); - } + new_h = -1; + new_s = 0; + new_v = v; } + else if (light_h == -1) + { + new_h = h; + new_s = max(0.0, s - adj / 2.0); + new_v = v; + } + else if (h == -1) + { + new_h = light_h; + new_s = max(0.0, light_s - adj / 2.0); + new_v = v; + } + else + { + new_h = (light_h + h) / 2; + new_s = max(0.0, s - adj / 2.0); + new_v = v; + } + + api->hsvtorgb(new_h, new_s, new_v, &r, &g, &b); + + api->putpixel(canvas, x + xx, y + yy, + SDL_MapRGB(canvas->format, r, g, b)); + } } + } } // Affect the canvas on drag: void light_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) { - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_light); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, do_light); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = ox - 8; update_rect->y = oy - 8; @@ -208,7 +224,8 @@ void light_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void light_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) { light_drag(api, which, canvas, last, x, y, x, y, update_rect); } @@ -216,7 +233,8 @@ void light_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, // Affect the canvas on release: void light_release(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, - int x, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) + int x, int y ATTRIBUTE_UNUSED, + SDL_Rect * update_rect ATTRIBUTE_UNUSED) { api->playsound(light2_snd, (x * 255) / canvas->w, 255); } @@ -237,17 +255,20 @@ void light_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b) } // Use colors: -int light_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int light_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 1; } -void light_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void light_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void light_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void light_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } diff --git a/magic/src/lightning.c b/magic/src/lightning.c index 6c49be853..688324ad0 100644 --- a/magic/src/lightning.c +++ b/magic/src/lightning.c @@ -31,16 +31,24 @@ int lightning_requires_colors(magic_api * api, int which); int lightning_modes(magic_api * api, int which); void lightning_shutdown(magic_api * api); void lightning_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); + SDL_Surface * canvas, SDL_Surface * snapshot, int x, + int y, SDL_Rect * update_rect); void lightning_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); void lightning_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void lightning_line_callback_drag(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y); -void lightning_draw_bolt(void * ptr, SDL_Surface * canvas, SDL_Surface * snapshot, float sx, float sy, float angle, float len, int thickness); -void lightning_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); -void lightning_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void lightning_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void lightning_line_callback_drag(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y); +void lightning_draw_bolt(void *ptr, SDL_Surface * canvas, + SDL_Surface * snapshot, float sx, float sy, + float angle, float len, int thickness); +void lightning_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect); +void lightning_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void lightning_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); Uint32 lightning_api_version(void) @@ -52,7 +60,8 @@ int lightning_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/lightning.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/lightning.ogg", + api->data_directory); snd_effect = Mix_LoadWAV(fname); return (1); @@ -68,32 +77,41 @@ SDL_Surface *lightning_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/lightning.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/lightning.png", + api->data_directory); return (IMG_Load(fname)); } -char *lightning_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *lightning_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return strdup(gettext("Lightning")); } -int lightning_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int lightning_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_ARTISTIC; } -char *lightning_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *lightning_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED) { - return strdup(gettext("Click, drag, and release to draw a lightning bolt between two points.")); + return + strdup(gettext + ("Click, drag, and release to draw a lightning bolt between two points.")); } -int lightning_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int lightning_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 1; } -int lightning_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int lightning_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MODE_PAINT; } @@ -107,7 +125,8 @@ void lightning_shutdown(magic_api * api ATTRIBUTE_UNUSED) void lightning_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect) + SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect) { sx = x; sy = y; @@ -117,8 +136,8 @@ lightning_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, void lightning_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, - int x, int y, SDL_Rect * update_rect) + SDL_Surface * snapshot, int ox ATTRIBUTE_UNUSED, + int oy ATTRIBUTE_UNUSED, int x, int y, SDL_Rect * update_rect) { /* FIXME: This could be made more efficient (only blit and update between (sx,sy) and (x,y), though @@ -131,13 +150,15 @@ lightning_drag(magic_api * api, int which, SDL_Surface * canvas, SDL_BlitSurface(snapshot, update_rect, canvas, update_rect); - api->line((void *)api, which, canvas, snapshot, sx, sy, x, y, 1, lightning_line_callback_drag); + api->line((void *) api, which, canvas, snapshot, sx, sy, x, y, 1, + lightning_line_callback_drag); } void lightning_release(magic_api * api, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect) + SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect) { float a, b, len, angle; int thickness; @@ -168,8 +189,9 @@ lightning_release(magic_api * api, int which ATTRIBUTE_UNUSED, angle = -angle; #ifdef DEBUG - printf("(%d,%d)->(%d,%d) => a = %.2f, b = %.2f, c (len) = %.2f; angle = %.2f degrees\n", - sx, sy, x, y, a, b, len, angle); + printf + ("(%d,%d)->(%d,%d) => a = %.2f, b = %.2f, c (len) = %.2f; angle = %.2f degrees\n", + sx, sy, x, y, a, b, len, angle); fflush(stdout); #endif @@ -177,10 +199,13 @@ lightning_release(magic_api * api, int which ATTRIBUTE_UNUSED, if (thickness < 4) thickness = 4; - lightning_draw_bolt((void *) api, canvas, snapshot, (float) sx, (float) sy, angle, len, thickness); + lightning_draw_bolt((void *) api, canvas, snapshot, (float) sx, (float) sy, + angle, len, thickness); } -void lightning_draw_bolt(void * ptr, SDL_Surface * canvas, SDL_Surface * snapshot, float sx, float sy, float angle, float len, int thickness) +void lightning_draw_bolt(void *ptr, SDL_Surface * canvas, + SDL_Surface * snapshot, float sx, float sy, + float angle, float len, int thickness) { magic_api *api = (magic_api *) ptr; float i; @@ -199,77 +224,79 @@ void lightning_draw_bolt(void * ptr, SDL_Surface * canvas, SDL_Surface * snapsho t = 1; for (i = 0; i < len; i++) + { + x = x + cos(angle * M_PI / 180.0); + y = y + sin(angle * M_PI / 180.0); + + angle = angle + ((float) (rand() % 15) - 7.5); + if (angle < orig_angle - 10.0) + angle = orig_angle - 10.0; + else if (angle > orig_angle + 10.0) + angle = orig_angle + 10.0; + + for (yy = -t; yy <= t; yy++) { - x = x + cos(angle * M_PI / 180.0); - y = y + sin(angle * M_PI / 180.0); - - angle = angle + ((float) (rand() % 15) - 7.5); - if (angle < orig_angle - 10.0) - angle = orig_angle - 10.0; - else if (angle > orig_angle + 10.0) - angle = orig_angle + 10.0; - - for (yy = -t; yy <= t; yy++) + for (xx = -t; xx <= t; xx++) + { + if (api->in_circle(xx, yy, t)) { - for (xx = -t; xx <= t; xx++) - { - if (api->in_circle(xx, yy, t)) - { - float light_h, light_s; + float light_h, light_s; - light_h = lightning_h; - light_s = lightning_s; + light_h = lightning_h; + light_s = lightning_s; - SDL_GetRGB(api->getpixel(canvas, x + xx, y + yy), canvas->format, &r, &g, &b); - api->rgbtohsv(r, g, b, &h, &s, &v); + SDL_GetRGB(api->getpixel(canvas, x + xx, y + yy), canvas->format, + &r, &g, &b); + api->rgbtohsv(r, g, b, &h, &s, &v); - adj = 1.0 - (sqrt((xx * xx) + (yy * yy)) / t); + adj = 1.0 - (sqrt((xx * xx) + (yy * yy)) / t); - new_v = v + adj; - if (new_v > 1.0) - { - light_s = light_s / (new_v * 2); - new_v = 1.0; - } + new_v = v + adj; + if (new_v > 1.0) + { + light_s = light_s / (new_v * 2); + new_v = 1.0; + } - if (light_h == -1) - { - new_h = h; - new_s = (s * 25) / 100; - } - else - { - new_h = ((light_h * 75) + (h * 25)) / 100; - new_s = ((light_s * 75) + (s * 25)) / 100; - } - - api->hsvtorgb(new_h, new_s, new_v, &r, &g, &b); - - api->putpixel(canvas, x + xx, y + yy, SDL_MapRGB(canvas->format, r, g, b)); - } - } - } - - if (((rand() % 50) == 0 || (int) i == (int) (len / 2)) && thickness > 1 && len >= 4) - { - float new_angle; - - if ((rand() % 10) == 0) - { - new_angle = angle + ((float) (rand() % 180) - 90.0); - } + if (light_h == -1) + { + new_h = h; + new_s = (s * 25) / 100; + } else - { - new_angle = angle + ((float) (rand() % 90) - 45.0); - } + { + new_h = ((light_h * 75) + (h * 25)) / 100; + new_s = ((light_s * 75) + (s * 25)) / 100; + } - lightning_draw_bolt((void *) api, canvas, snapshot, x, y, - new_angle, - ((len / 8) + (rand() % (int) (len / 4))), - thickness - 1 - ); + api->hsvtorgb(new_h, new_s, new_v, &r, &g, &b); + + api->putpixel(canvas, x + xx, y + yy, + SDL_MapRGB(canvas->format, r, g, b)); } + } } + + if (((rand() % 50) == 0 || (int) i == (int) (len / 2)) && thickness > 1 + && len >= 4) + { + float new_angle; + + if ((rand() % 10) == 0) + { + new_angle = angle + ((float) (rand() % 180) - 90.0); + } + else + { + new_angle = angle + ((float) (rand() % 90) - 45.0); + } + + lightning_draw_bolt((void *) api, canvas, snapshot, x, y, + new_angle, + ((len / 8) + (rand() % (int) (len / 4))), + thickness - 1); + } + } } @@ -279,17 +306,25 @@ void lightning_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b) } -void lightning_line_callback_drag(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * snapshot ATTRIBUTE_UNUSED, int x, int y) +void lightning_line_callback_drag(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, + SDL_Surface * snapshot ATTRIBUTE_UNUSED, + int x, int y) { magic_api *api = (magic_api *) ptr; api->xorpixel(canvas, x, y); } -void lightning_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void lightning_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void lightning_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void lightning_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } diff --git a/magic/src/metalpaint.c b/magic/src/metalpaint.c index 974171e98..ef79871c2 100644 --- a/magic/src/metalpaint.c +++ b/magic/src/metalpaint.c @@ -45,18 +45,24 @@ SDL_Surface *metalpaint_get_icon(magic_api * api, int which); char *metalpaint_get_name(magic_api * api, int which); int metalpaint_get_group(magic_api * api, int which); char *metalpaint_get_description(magic_api * api, int which, int mode); -static void do_metalpaint(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +static void do_metalpaint(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); void metalpaint_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 metalpaint_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void metalpaint_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 metalpaint_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, + SDL_Rect * update_rect); void metalpaint_shutdown(magic_api * api); void metalpaint_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int metalpaint_requires_colors(magic_api * api, int which); -void metalpaint_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void metalpaint_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void metalpaint_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void metalpaint_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int metalpaint_modes(magic_api * api, int which); @@ -71,7 +77,8 @@ int metalpaint_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/metalpaint.wav", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/metalpaint.wav", + api->data_directory); metalpaint_snd = Mix_LoadWAV(fname); return (1); @@ -88,28 +95,34 @@ SDL_Surface *metalpaint_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/metalpaint.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/metalpaint.png", + api->data_directory); return (IMG_Load(fname)); } // Return our names, localized: -char *metalpaint_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *metalpaint_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (strdup(gettext_noop("Metal Paint"))); } // Return our groups: -int metalpaint_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int metalpaint_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PAINTING; } // Return our descriptions, localized: -char *metalpaint_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, +char *metalpaint_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) { - return (strdup(gettext_noop("Click and drag the mouse to paint with a metallic color."))); + return (strdup + (gettext_noop + ("Click and drag the mouse to paint with a metallic color."))); } #define METALPAINT_CYCLE 32 @@ -125,7 +138,8 @@ static int metalpaint_gradient[METALPAINT_CYCLE] = { // Do the effect: -static void do_metalpaint(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, +static void do_metalpaint(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { magic_api *api = (magic_api *) ptr; @@ -134,40 +148,43 @@ static void do_metalpaint(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * c Uint8 r, g, b; for (yy = -8; yy < 8; yy++) + { + for (xx = -8; xx < 8; xx++) { - for (xx = -8; xx < 8; xx++) - { - n = metalpaint_gradient[((x + xx + y + yy) / 4) % METALPAINT_CYCLE]; + n = metalpaint_gradient[((x + xx + y + yy) / 4) % METALPAINT_CYCLE]; - r = (metalpaint_r * n) / 255; - g = (metalpaint_g * n) / 255; - b = (metalpaint_b * n) / 255; + r = (metalpaint_r * n) / 255; + g = (metalpaint_g * n) / 255; + b = (metalpaint_b * n) / 255; - api->putpixel(canvas, x + xx, y + yy, SDL_MapRGB(canvas->format, r, g, b)); - } + api->putpixel(canvas, x + xx, y + yy, + SDL_MapRGB(canvas->format, r, g, b)); } + } } // Affect the canvas on drag: void metalpaint_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) { - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_metalpaint); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, + do_metalpaint); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = ox - 8; update_rect->y = oy - 8; @@ -179,15 +196,19 @@ void metalpaint_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void metalpaint_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) { metalpaint_drag(api, which, canvas, last, x, y, x, y, update_rect); } // Affect the canvas on release: -void metalpaint_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) +void metalpaint_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) { } @@ -199,7 +220,8 @@ void metalpaint_shutdown(magic_api * api ATTRIBUTE_UNUSED) } // Record the color from Tux Paint: -void metalpaint_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) +void metalpaint_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, + Uint8 b) { metalpaint_r = min(255, r + 64); metalpaint_g = min(255, g + 64); @@ -207,22 +229,28 @@ void metalpaint_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Ui } // Use colors: -int metalpaint_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int metalpaint_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 1; } -void metalpaint_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void metalpaint_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void metalpaint_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void metalpaint_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -int metalpaint_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int metalpaint_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_PAINT); } diff --git a/magic/src/mirror_flip.c b/magic/src/mirror_flip.c index e1c83cb58..e1631ea80 100644 --- a/magic/src/mirror_flip.c +++ b/magic/src/mirror_flip.c @@ -52,9 +52,12 @@ SDL_Surface *mirror_flip_get_icon(magic_api *, int); char *mirror_flip_get_name(magic_api *, int); int mirror_flip_get_group(magic_api *, int); char *mirror_flip_get_description(magic_api *, int, int); -void mirror_flip_drag(magic_api *, int, SDL_Surface *, SDL_Surface *, int, int, int, int, SDL_Rect *); -void mirror_flip_release(magic_api *, int, SDL_Surface *, SDL_Surface *, int, int, int, int, SDL_Rect *); -void mirror_flip_click(magic_api *, int, int, SDL_Surface *, SDL_Surface *, int, int, SDL_Rect *); +void mirror_flip_drag(magic_api *, int, SDL_Surface *, SDL_Surface *, int, + int, int, int, SDL_Rect *); +void mirror_flip_release(magic_api *, int, SDL_Surface *, SDL_Surface *, int, + int, int, int, SDL_Rect *); +void mirror_flip_click(magic_api *, int, int, SDL_Surface *, SDL_Surface *, + int, int, SDL_Rect *); void mirror_flip_shutdown(magic_api *); void mirror_flip_set_color(magic_api *, Uint8, Uint8, Uint8); int mirror_flip_requires_colors(magic_api *, int); @@ -67,10 +70,12 @@ int mirror_flip_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/mirror.wav", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/mirror.wav", + api->data_directory); snd_effects[TOOL_MIRROR] = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%ssounds/magic/flip.wav", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/flip.wav", + api->data_directory); snd_effects[TOOL_FLIP] = Mix_LoadWAV(fname); return (1); @@ -93,13 +98,15 @@ SDL_Surface *mirror_flip_get_icon(magic_api * api, int which) char fname[1024]; if (which == TOOL_MIRROR) - { - snprintf(fname, sizeof(fname), "%simages/magic/mirror.png", api->data_directory); - } + { + snprintf(fname, sizeof(fname), "%simages/magic/mirror.png", + api->data_directory); + } else if (which == TOOL_FLIP) - { - snprintf(fname, sizeof(fname), "%simages/magic/flip.png", api->data_directory); - } + { + snprintf(fname, sizeof(fname), "%simages/magic/flip.png", + api->data_directory); + } return (IMG_Load(fname)); } @@ -116,13 +123,15 @@ char *mirror_flip_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) } // Return our group (the same): -int mirror_flip_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int mirror_flip_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PICTURE_WARPS; } // Return our descriptions, localized: -char *mirror_flip_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode ATTRIBUTE_UNUSED) +char *mirror_flip_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, + int mode ATTRIBUTE_UNUSED) { if (which == TOOL_MIRROR) return (strdup(gettext_noop("Click to make a mirror image."))); @@ -134,19 +143,23 @@ char *mirror_flip_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, i // We affect the whole canvas, so only do things on click, not drag: void mirror_flip_drag(magic_api * api ATTRIBUTE_UNUSED, - int which ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, int ox ATTRIBUTE_UNUSED, int oy 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) { // No-op } void mirror_flip_release(magic_api * api ATTRIBUTE_UNUSED, - int which ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, int ox ATTRIBUTE_UNUSED, int oy 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) { // No-op } @@ -154,46 +167,47 @@ void mirror_flip_release(magic_api * api ATTRIBUTE_UNUSED, // Affect the canvas on click: void mirror_flip_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * last, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect) + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, + SDL_Rect * update_rect) { int xx, yy; SDL_Rect src, dest; if (which == TOOL_MIRROR) + { + for (xx = 0; xx < canvas->w; xx++) { - for (xx = 0; xx < canvas->w; xx++) - { - src.x = xx; - src.y = 0; - src.w = 1; - src.h = canvas->h; + src.x = xx; + src.y = 0; + src.w = 1; + src.h = canvas->h; - dest.x = canvas->w - xx - 1; - dest.y = 0; + dest.x = canvas->w - xx - 1; + dest.y = 0; - SDL_BlitSurface(last, &src, canvas, &dest); - } - - api->special_notify(SPECIAL_MIRROR); + SDL_BlitSurface(last, &src, canvas, &dest); } + + api->special_notify(SPECIAL_MIRROR); + } else if (which == TOOL_FLIP) + { + for (yy = 0; yy < canvas->h; yy++) { - for (yy = 0; yy < canvas->h; yy++) - { - src.x = 0; - src.y = yy; - src.w = canvas->w; - src.h = 1; + src.x = 0; + src.y = yy; + src.w = canvas->w; + src.h = 1; - dest.x = 0; - dest.y = canvas->h - yy - 1; + dest.x = 0; + dest.y = canvas->h - yy - 1; - SDL_BlitSurface(last, &src, canvas, &dest); - } - - api->special_notify(SPECIAL_FLIP); + SDL_BlitSurface(last, &src, canvas, &dest); } + api->special_notify(SPECIAL_FLIP); + } + update_rect->x = 0; update_rect->y = 0; update_rect->w = canvas->w; @@ -213,27 +227,34 @@ void mirror_flip_shutdown(magic_api * api ATTRIBUTE_UNUSED) // We don't use colors: void mirror_flip_set_color(magic_api * api ATTRIBUTE_UNUSED, - Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, + Uint8 b ATTRIBUTE_UNUSED) { } // We don't use colors: -int mirror_flip_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int mirror_flip_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } void mirror_flip_switchin(magic_api * api ATTRIBUTE_UNUSED, - int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } void mirror_flip_switchout(magic_api * api ATTRIBUTE_UNUSED, - int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -int mirror_flip_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int mirror_flip_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_FULLSCREEN); } diff --git a/magic/src/mosaic.c b/magic/src/mosaic.c index 7422c5b57..6eee90589 100644 --- a/magic/src/mosaic.c +++ b/magic/src/mosaic.c @@ -45,9 +45,12 @@ #define gettext_noop(String) String #endif -static void mosaic_noise_pixel(void *ptr, SDL_Surface * canvas, int noise_AMOUNT, int x, int y); -static void mosaic_blur_pixel(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int x, int y); -static void mosaic_sharpen_pixel(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +static void mosaic_noise_pixel(void *ptr, SDL_Surface * canvas, + int noise_AMOUNT, int x, int y); +static void mosaic_blur_pixel(void *ptr, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); +static void mosaic_sharpen_pixel(void *ptr, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); static void reset_mosaic_blured(SDL_Surface * canvas); /* Prototypes */ @@ -59,9 +62,12 @@ char *mosaic_get_name(magic_api *, int); int mosaic_get_group(magic_api *, int); char *mosaic_get_description(magic_api *, int, int); void mosaic_paint(void *, int, SDL_Surface *, SDL_Surface *, int, int); -void mosaic_drag(magic_api *, int, SDL_Surface *, SDL_Surface *, int, int, int, int, SDL_Rect *); -void mosaic_click(magic_api *, int, int, SDL_Surface *, SDL_Surface *, int, int, SDL_Rect *); -void mosaic_release(magic_api *, int, SDL_Surface *, SDL_Surface *, int, int, SDL_Rect *); +void mosaic_drag(magic_api *, int, SDL_Surface *, SDL_Surface *, int, int, + int, int, SDL_Rect *); +void mosaic_click(magic_api *, int, int, SDL_Surface *, SDL_Surface *, int, + int, SDL_Rect *); +void mosaic_release(magic_api *, int, SDL_Surface *, SDL_Surface *, int, int, + SDL_Rect *); void mosaic_shutdown(magic_api *); void mosaic_set_color(magic_api *, Uint8, Uint8, Uint8); int mosaic_requires_colors(magic_api *, int); @@ -102,7 +108,8 @@ const int mosaic_groups[mosaic_NUM_TOOLS] = { }; const char *mosaic_descs[mosaic_NUM_TOOLS][2] = { - {gettext_noop("Click and drag the mouse to add a mosaic effect to parts of your picture."), + {gettext_noop + ("Click and drag the mouse to add a mosaic effect to parts of your picture."), gettext_noop("Click to add a mosaic effect to your entire picture."),}, }; @@ -119,10 +126,11 @@ int mosaic_init(magic_api * api) char fname[1024]; for (i = 0; i < mosaic_NUM_TOOLS; i++) - { - snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, mosaic_snd_filenames[i]); - mosaic_snd_effect[i] = Mix_LoadWAV(fname); - } + { + snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, + mosaic_snd_filenames[i]); + mosaic_snd_effect[i] = Mix_LoadWAV(fname); + } return (1); } @@ -137,7 +145,8 @@ SDL_Surface *mosaic_get_icon(magic_api * api, int which) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, mosaic_icon_filenames[which]); + snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, + mosaic_icon_filenames[which]); return (IMG_Load(fname)); } @@ -154,7 +163,8 @@ int mosaic_get_group(magic_api * api ATTRIBUTE_UNUSED, int which) } // Return our descriptions, localized: -char *mosaic_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode) +char *mosaic_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, + int mode) { return (strdup(gettext_noop(mosaic_descs[which][mode - 1]))); } @@ -167,80 +177,89 @@ static int mosaic_grey(Uint8 r1, Uint8 g1, Uint8 b1) // Do the effect for the full image static void do_mosaic_full(void *ptr, SDL_Surface * canvas, - SDL_Surface * last ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) + SDL_Surface * last ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { magic_api *api = (magic_api *) ptr; int x, y; - Uint32 amask = ~(canvas->format->Rmask | canvas->format->Gmask | canvas->format->Bmask); + Uint32 amask = + ~(canvas->format->Rmask | canvas->format->Gmask | canvas->format->Bmask); SDL_Surface *mosaic_temp = SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->w, canvas->h, - canvas->format->BitsPerPixel, + canvas->format-> + BitsPerPixel, canvas->format->Rmask, canvas->format->Gmask, - canvas->format->Bmask, amask); + canvas->format->Bmask, + amask); api->update_progress_bar(); for (y = 0; y < canvas->h; y++) - { + { - for (x = 0; x < canvas->w; x++) - { - mosaic_blur_pixel(api, mosaic_temp, canvas_noise, x, y); - } + for (x = 0; x < canvas->w; x++) + { + mosaic_blur_pixel(api, mosaic_temp, canvas_noise, x, y); } + } api->update_progress_bar(); for (y = 0; y < canvas->h; y++) + { + for (x = 0; x < canvas->w; x++) { - for (x = 0; x < canvas->w; x++) - { - mosaic_sharpen_pixel(api, canvas, mosaic_temp, x, y); - } + mosaic_sharpen_pixel(api, canvas, mosaic_temp, x, y); } + } SDL_FreeSurface(mosaic_temp); } /* Paint the brush, noise is yet done at switchin, blurs 2 pixels around the brush in order to get sharpen well done.*/ void mosaic_paint(void *ptr_to_api, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) + SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, + int x, int y) { int i, j, pix_row_pos; magic_api *api = (magic_api *) ptr_to_api; - for (j = max(0, y - mosaic_RADIUS - 2); j < min(canvas->h, y + mosaic_RADIUS + 2); j++) - { - pix_row_pos = j * canvas->w; - for (i = max(0, x - mosaic_RADIUS - 2); i < min(canvas->w, x + mosaic_RADIUS + 2); i++) - if (!mosaic_blured[pix_row_pos + i] && api->in_circle(i - x, j - y, mosaic_RADIUS + 2)) - { - mosaic_blur_pixel(api, canvas_blur, canvas_noise, i, j); - mosaic_blured[pix_row_pos + i] = 1; /* Track what are yet blured */ - } - } + for (j = max(0, y - mosaic_RADIUS - 2); + j < min(canvas->h, y + mosaic_RADIUS + 2); j++) + { + pix_row_pos = j * canvas->w; + for (i = max(0, x - mosaic_RADIUS - 2); + i < min(canvas->w, x + mosaic_RADIUS + 2); i++) + if (!mosaic_blured[pix_row_pos + i] + && api->in_circle(i - x, j - y, mosaic_RADIUS + 2)) + { + mosaic_blur_pixel(api, canvas_blur, canvas_noise, i, j); + mosaic_blured[pix_row_pos + i] = 1; /* Track what are yet blured */ + } + } for (i = x - mosaic_RADIUS; i < x + mosaic_RADIUS; i++) for (j = y - mosaic_RADIUS; j < y + mosaic_RADIUS; j++) if (api->in_circle(i - x, j - y, mosaic_RADIUS)) if (!api->touched(i, j)) - { - mosaic_sharpen_pixel(api, canvas_sharp, canvas_blur, i, j); - api->putpixel(canvas, i, j, api->getpixel(canvas_sharp, i, j)); - } + { + mosaic_sharpen_pixel(api, canvas_sharp, canvas_blur, i, j); + api->putpixel(canvas, i, j, api->getpixel(canvas_sharp, i, j)); + } } // Affect the canvas on drag: void mosaic_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) { api->line(api, which, canvas, last, ox, oy, x, y, 1, mosaic_paint); @@ -254,20 +273,21 @@ void mosaic_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void mosaic_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) { if (mode == MODE_FULLSCREEN) - { - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; + { + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; - do_mosaic_full(api, canvas_noise, last, which); - SDL_BlitSurface(canvas_noise, NULL, canvas, NULL); - api->playsound(mosaic_snd_effect[which], 128, 255); - } + do_mosaic_full(api, canvas_noise, last, which); + SDL_BlitSurface(canvas_noise, NULL, canvas, NULL); + api->playsound(mosaic_snd_effect[which], 128, 255); + } else mosaic_drag(api, which, canvas, last, x, y, x, y, update_rect); @@ -278,7 +298,8 @@ void mosaic_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) + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, + SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -289,28 +310,31 @@ void mosaic_shutdown(magic_api * api ATTRIBUTE_UNUSED) int i; for (i = 0; i < mosaic_NUM_TOOLS; i++) + { + if (mosaic_snd_effect[i] != NULL) { - if (mosaic_snd_effect[i] != NULL) - { - Mix_FreeChunk(mosaic_snd_effect[i]); - } + Mix_FreeChunk(mosaic_snd_effect[i]); } + } } // Record the color from Tux Paint: void mosaic_set_color(magic_api * api ATTRIBUTE_UNUSED, - Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, + Uint8 b ATTRIBUTE_UNUSED) { } // Use colors: -int mosaic_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int mosaic_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } //Add noise to a pixel -static void mosaic_noise_pixel(void *ptr, SDL_Surface * canvas, int noise_AMOUNT, int x, int y) +static void mosaic_noise_pixel(void *ptr, SDL_Surface * canvas, + int noise_AMOUNT, int x, int y) { magic_api *api = (magic_api *) ptr; @@ -318,16 +342,21 @@ static void mosaic_noise_pixel(void *ptr, SDL_Surface * canvas, int noise_AMOUNT double temp2[3]; int k; - SDL_GetRGB(api->getpixel(canvas, x, y), canvas->format, &temp[0], &temp[1], &temp[2]); + SDL_GetRGB(api->getpixel(canvas, x, y), canvas->format, &temp[0], &temp[1], + &temp[2]); for (k = 0; k < 3; k++) - { - temp2[k] = clamp(0.0, (int)temp[k] - (rand() % noise_AMOUNT) + noise_AMOUNT / 2.0, 255.0); - } - api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, temp2[0], temp2[1], temp2[2])); + { + temp2[k] = + clamp(0.0, (int) temp[k] - (rand() % noise_AMOUNT) + noise_AMOUNT / 2.0, + 255.0); + } + api->putpixel(canvas, x, y, + SDL_MapRGB(canvas->format, temp2[0], temp2[1], temp2[2])); } //Blur a pixel -static void mosaic_blur_pixel(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void mosaic_blur_pixel(void *ptr, SDL_Surface * canvas, + SDL_Surface * last, int x, int y) { magic_api *api = (magic_api *) ptr; int i, j, k; @@ -343,31 +372,35 @@ static void mosaic_blur_pixel(void *ptr, SDL_Surface * canvas, SDL_Surface * las }; for (k = 0; k < 3; k++) - { - blurValue[k] = 0; - } + { + blurValue[k] = 0; + } for (i = -2; i < 3; i++) + { + for (j = -2; j < 3; j++) { - for (j = -2; j < 3; j++) - { - //Add the pixels around the current one wieghted - SDL_GetRGB(api->getpixel(last, x + i, y + j), last->format, &temp[0], &temp[1], &temp[2]); - for (k = 0; k < 3; k++) - { - blurValue[k] += temp[k] * weight[i + 2][j + 2]; - } - } + //Add the pixels around the current one wieghted + SDL_GetRGB(api->getpixel(last, x + i, y + j), last->format, &temp[0], + &temp[1], &temp[2]); + for (k = 0; k < 3; k++) + { + blurValue[k] += temp[k] * weight[i + 2][j + 2]; + } } + } for (k = 0; k < 3; k++) - { - blurValue[k] /= 273; - } - api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, blurValue[0], blurValue[1], blurValue[2])); + { + blurValue[k] /= 273; + } + api->putpixel(canvas, x, y, + SDL_MapRGB(canvas->format, blurValue[0], blurValue[1], + blurValue[2])); } //Sharpen a pixel -static void mosaic_sharpen_pixel(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void mosaic_sharpen_pixel(void *ptr, SDL_Surface * canvas, + SDL_Surface * last, int x, int y) { magic_api *api = (magic_api *) ptr; @@ -391,73 +424,85 @@ static void mosaic_sharpen_pixel(void *ptr, SDL_Surface * canvas, SDL_Surface * sobel_1 = 0; sobel_2 = 0; for (i = -1; i < 2; i++) + { + for (j = -1; j < 2; j++) { - for (j = -1; j < 2; j++) - { - //No need to check if inside canvas, getpixel does it for us. - SDL_GetRGB(api->getpixel(last, x + i, y + j), last->format, &r1, &g1, &b1); - grey = mosaic_grey(r1, g1, b1); - sobel_1 += grey * sobel_weights_1[i + 1][j + 1]; - sobel_2 += grey * sobel_weights_2[i + 1][j + 1]; - } + //No need to check if inside canvas, getpixel does it for us. + SDL_GetRGB(api->getpixel(last, x + i, y + j), last->format, &r1, &g1, + &b1); + grey = mosaic_grey(r1, g1, b1); + sobel_1 += grey * sobel_weights_1[i + 1][j + 1]; + sobel_2 += grey * sobel_weights_2[i + 1][j + 1]; } + } temp = sqrt(sobel_1 * sobel_1 + sobel_2 * sobel_2); temp = (temp / 1443) * 255.0; SDL_GetRGB(api->getpixel(last, x, y), last->format, &r1, &g1, &b1); - api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, clamp(0.0, r1 + mosaic_SHARPEN * temp, 255.0), - clamp(0.0, g1 + mosaic_SHARPEN * temp, 255.0), - clamp(0.0, b1 + mosaic_SHARPEN * temp, 255.0))); + api->putpixel(canvas, x, y, + SDL_MapRGB(canvas->format, + clamp(0.0, r1 + mosaic_SHARPEN * temp, 255.0), + clamp(0.0, g1 + mosaic_SHARPEN * temp, 255.0), + clamp(0.0, b1 + mosaic_SHARPEN * temp, 255.0))); } -void mosaic_switchin(magic_api * api, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas) +void mosaic_switchin(magic_api * api, int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas) { int y, x; Uint32 amask; mosaic_blured = (Uint8 *) malloc(sizeof(Uint8) * (canvas->w * canvas->h)); if (mosaic_blured == NULL) - { - fprintf(stderr, "\nError: Can't build drawing touch mask!\n"); - exit(1); - } + { + fprintf(stderr, "\nError: Can't build drawing touch mask!\n"); + exit(1); + } - amask = ~(canvas->format->Rmask | canvas->format->Gmask | canvas->format->Bmask); + amask = + ~(canvas->format->Rmask | canvas->format->Gmask | canvas->format->Bmask); canvas_noise = SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->w, canvas->h, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, amask); + canvas->format->Rmask, + canvas->format->Gmask, + canvas->format->Bmask, amask); SDL_BlitSurface(canvas, NULL, canvas_noise, NULL); for (y = 0; y < canvas->h; y++) + { + for (x = 0; x < canvas->w; x++) { - for (x = 0; x < canvas->w; x++) - { - mosaic_noise_pixel(api, canvas_noise, mosaic_AMOUNT, x, y); - } + mosaic_noise_pixel(api, canvas_noise, mosaic_AMOUNT, x, y); } + } canvas_blur = SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->w, canvas->h, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, amask); + canvas->format->Rmask, + canvas->format->Gmask, + canvas->format->Bmask, amask); canvas_sharp = SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->w, canvas->h, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, amask); + canvas->format->Rmask, + canvas->format->Gmask, + canvas->format->Bmask, amask); reset_mosaic_blured(canvas); } void mosaic_switchout(magic_api * api ATTRIBUTE_UNUSED, - int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { SDL_FreeSurface(canvas_noise); SDL_FreeSurface(canvas_blur); diff --git a/magic/src/mosaic_shaped.c b/magic/src/mosaic_shaped.c index d4cd4c96f..731316d87 100644 --- a/magic/src/mosaic_shaped.c +++ b/magic/src/mosaic_shaped.c @@ -49,14 +49,20 @@ #define gettext_noop(String) String #endif -static void mosaic_shaped_sharpen_pixel(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +static void mosaic_shaped_sharpen_pixel(void *ptr, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); static void reset_counter(SDL_Surface * canvas, Uint8 * counter); -static void fill_square(magic_api * api, SDL_Surface * canvas, int x, int y, int size, Uint32 color); +static void fill_square(magic_api * api, SDL_Surface * canvas, int x, int y, + int size, Uint32 color); static void deform(magic_api * api, SDL_Surface * srfc); -static void do_mosaic_shaped_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int which, +static void do_mosaic_shaped_full(void *ptr, SDL_Surface * canvas, + SDL_Surface * last, int which, SDL_Rect * update_rect); -static void mosaic_shaped_fill(void *ptr_to_api, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); -static void mosaic_shaped_paint(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +static void mosaic_shaped_fill(void *ptr_to_api, int which, + SDL_Surface * canvas, SDL_Surface * last, + int x, int y); +static void mosaic_shaped_paint(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); Uint32 mosaic_shaped_api_version(void); int mosaic_shaped_init(magic_api * api); @@ -68,13 +74,16 @@ int mosaic_shaped_get_group(magic_api * api, int which); char *mosaic_shaped_get_description(magic_api * api, int which, int mode); void mosaic_shaped_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 mosaic_shaped_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 mosaic_shaped_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 mosaic_shaped_shutdown(magic_api * api); @@ -82,20 +91,24 @@ void mosaic_shaped_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int mosaic_shaped_requires_colors(magic_api * api, int which); -void mosaic_shaped_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); +void mosaic_shaped_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); -void mosaic_shaped_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void mosaic_shaped_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int mosaic_shaped_modes(magic_api * api, int which); -int scan_fill(magic_api * api, SDL_Surface * canvas, SDL_Surface * srfc, int x, int y, int fill_edge, int fill_tile, - int size, Uint32 color); +int scan_fill(magic_api * api, SDL_Surface * canvas, SDL_Surface * srfc, + int x, int y, int fill_edge, int fill_tile, int size, + Uint32 color); Uint8 *mosaic_shaped_counted; Uint8 *mosaic_shaped_done; Uint8 mosaic_shaped_r, mosaic_shaped_g, mosaic_shaped_b; -int mosaic_shaped_average_r, mosaic_shaped_average_g, mosaic_shaped_average_b, mosaic_shaped_average_count; +int mosaic_shaped_average_r, mosaic_shaped_average_g, mosaic_shaped_average_b, + mosaic_shaped_average_count; Uint32 pixel_average, black, white; enum @@ -138,17 +151,20 @@ const char *mosaic_shaped_names[mosaic_shaped_NUM_TOOLS] = { const char *mosaic_shaped_descs[mosaic_shaped_NUM_TOOLS][2] = { { - gettext_noop("Click and drag the mouse to add a square mosaic to parts of your picture."), + gettext_noop + ("Click and drag the mouse to add a square mosaic to parts of your picture."), gettext_noop("Click to add a square mosaic to your entire picture."), }, { - gettext_noop("Click and drag the mouse to add a hexagonal mosaic to parts of your picture."), + gettext_noop + ("Click and drag the mouse to add a hexagonal mosaic to parts of your picture."), gettext_noop("Click to add a hexagonal mosaic to your entire picture."), }, { - gettext_noop("Click and drag the mouse to add an irregular mosaic to parts of your picture."), + gettext_noop + ("Click and drag the mouse to add an irregular mosaic to parts of your picture."), gettext_noop("Click to add an irregular mosaic to your entire picture."), }, }; @@ -167,10 +183,11 @@ int mosaic_shaped_init(magic_api * api) mosaic_shaped_pattern = NULL; for (i = 0; i < mosaic_shaped_NUM_TOOLS; i++) - { - snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, mosaic_shaped_snd_filenames[i]); - mosaic_shaped_snd_effect[i] = Mix_LoadWAV(fname); - } + { + snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, + mosaic_shaped_snd_filenames[i]); + mosaic_shaped_snd_effect[i] = Mix_LoadWAV(fname); + } return (1); } @@ -185,7 +202,8 @@ SDL_Surface *mosaic_shaped_get_icon(magic_api * api, int which) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, mosaic_shaped_icon_filenames[which]); + snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, + mosaic_shaped_icon_filenames[which]); return (IMG_Load(fname)); } @@ -196,13 +214,15 @@ char *mosaic_shaped_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) } // Return our groups (all the same!) -int mosaic_shaped_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int mosaic_shaped_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_DISTORTS; } // Return our descriptions, localized: -char *mosaic_shaped_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode) +char *mosaic_shaped_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which, int mode) { return (strdup(gettext_noop(mosaic_shaped_descs[which][mode - 1]))); } @@ -214,50 +234,62 @@ static int mosaic_shaped_grey(Uint8 r1, Uint8 g1, Uint8 b1) } // Do the effect for the full image -static void do_mosaic_shaped_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, - int which ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) +static void do_mosaic_shaped_full(void *ptr, SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + SDL_Rect * update_rect ATTRIBUTE_UNUSED) { int i, j, size; Uint32 mosaic_shaped_color; magic_api *api = (magic_api *) ptr; - mosaic_shaped_color = SDL_MapRGBA(canvas->format, mosaic_shaped_r, mosaic_shaped_g, mosaic_shaped_b, 0); + mosaic_shaped_color = + SDL_MapRGBA(canvas->format, mosaic_shaped_r, mosaic_shaped_g, + mosaic_shaped_b, 0); for (i = 3; i < canvas->w - 3; i += 2) + { + api->playsound(mosaic_shaped_snd_effect[which], 128, 255); + api->update_progress_bar(); + + for (j = 3; j < canvas->h - 3; j += 2) { - api->playsound(mosaic_shaped_snd_effect[which], 128, 255); - api->update_progress_bar(); + if (!mosaic_shaped_done[canvas->w * j + i]) + if (!mosaic_shaped_counted[canvas->w * j + i]) + if (api->getpixel(canvas_shaped, i, j) != black) + { + mosaic_shaped_average_r = 0; + mosaic_shaped_average_g = 0; + mosaic_shaped_average_b = 0; + mosaic_shaped_average_count = 0; + scan_fill(api, canvas, canvas_shaped, i, j, 1, 0, 1, + mosaic_shaped_color); - for (j = 3; j < canvas->h - 3; j += 2) - { - if (!mosaic_shaped_done[canvas->w * j + i]) - if (!mosaic_shaped_counted[canvas->w * j + i]) - if (api->getpixel(canvas_shaped, i, j) != black) - { - mosaic_shaped_average_r = 0; - mosaic_shaped_average_g = 0; - mosaic_shaped_average_b = 0; - mosaic_shaped_average_count = 0; - scan_fill(api, canvas, canvas_shaped, i, j, 1, 0, 1, mosaic_shaped_color); - - if (mosaic_shaped_average_count > 0) - { - reset_counter(canvas, mosaic_shaped_counted); - size = 0; - pixel_average = - SDL_MapRGB(canvas->format, mosaic_shaped_average_r / mosaic_shaped_average_count, - mosaic_shaped_average_g / mosaic_shaped_average_count, - mosaic_shaped_average_b / mosaic_shaped_average_count); - scan_fill(api, canvas, canvas_shaped, i, j, 0, 1, size, pixel_average); - } - } - } + if (mosaic_shaped_average_count > 0) + { + reset_counter(canvas, mosaic_shaped_counted); + size = 0; + pixel_average = + SDL_MapRGB(canvas->format, + mosaic_shaped_average_r / + mosaic_shaped_average_count, + mosaic_shaped_average_g / + mosaic_shaped_average_count, + mosaic_shaped_average_b / + mosaic_shaped_average_count); + scan_fill(api, canvas, canvas_shaped, i, j, 0, 1, size, + pixel_average); + } + } } + } } /* Fills a tesera */ -static void mosaic_shaped_fill(void *ptr_to_api, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, - SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) +static void mosaic_shaped_fill(void *ptr_to_api, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, int x, + int y) { Uint32 mosaic_shaped_color; int size; @@ -265,34 +297,38 @@ static void mosaic_shaped_fill(void *ptr_to_api, int which ATTRIBUTE_UNUSED, SDL x = clamp(0, x, canvas->w - 1); y = clamp(0, y, canvas->h - 1); - mosaic_shaped_color = SDL_MapRGBA(canvas->format, mosaic_shaped_r, mosaic_shaped_g, mosaic_shaped_b, 0); + mosaic_shaped_color = + SDL_MapRGBA(canvas->format, mosaic_shaped_r, mosaic_shaped_g, + mosaic_shaped_b, 0); mosaic_shaped_average_r = 0; mosaic_shaped_average_g = 0; mosaic_shaped_average_b = 0; mosaic_shaped_average_count = 0; if (api->getpixel(canvas_shaped, x, y) == black) - { - return; - } + { + return; + } scan_fill(api, canvas, canvas_shaped, x, y, 1, 0, 1, mosaic_shaped_color); if (mosaic_shaped_average_count > 0) - { - size = 0; - pixel_average = - SDL_MapRGB(canvas->format, mosaic_shaped_average_r / mosaic_shaped_average_count, - mosaic_shaped_average_g / mosaic_shaped_average_count, - mosaic_shaped_average_b / mosaic_shaped_average_count); - reset_counter(canvas, mosaic_shaped_counted); - scan_fill(api, canvas, canvas_shaped, x, y, 0, 1, size, pixel_average); - } + { + size = 0; + pixel_average = + SDL_MapRGB(canvas->format, + mosaic_shaped_average_r / mosaic_shaped_average_count, + mosaic_shaped_average_g / mosaic_shaped_average_count, + mosaic_shaped_average_b / mosaic_shaped_average_count); + reset_counter(canvas, mosaic_shaped_counted); + scan_fill(api, canvas, canvas_shaped, x, y, 0, 1, size, pixel_average); + } } // Affect the canvas on drag: void mosaic_shaped_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) { api->line(api, which, canvas, last, ox, oy, x, y, 1, mosaic_shaped_fill); update_rect->x = min(ox, x) - mosaic_shaped_pattern->w; @@ -304,27 +340,31 @@ void mosaic_shaped_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void mosaic_shaped_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) { if (mode == MODE_FULLSCREEN) - { - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; - do_mosaic_shaped_full(api, canvas, last, which, update_rect); - api->playsound(mosaic_shaped_snd_effect[which], 128, 255); - } + { + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; + do_mosaic_shaped_full(api, canvas, last, which, update_rect); + api->playsound(mosaic_shaped_snd_effect[which], 128, 255); + } else - { - mosaic_shaped_drag(api, which, canvas, last, x, y, x, y, update_rect); - } + { + mosaic_shaped_drag(api, which, canvas, last, x, y, x, y, update_rect); + } } // Affect the canvas on release: -void mosaic_shaped_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) +void mosaic_shaped_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) { } @@ -335,16 +375,17 @@ void mosaic_shaped_shutdown(magic_api * api ATTRIBUTE_UNUSED) int i; for (i = 0; i < mosaic_shaped_NUM_TOOLS; i++) + { + if (mosaic_shaped_snd_effect[i] != NULL) { - if (mosaic_shaped_snd_effect[i] != NULL) - { - Mix_FreeChunk(mosaic_shaped_snd_effect[i]); - } + Mix_FreeChunk(mosaic_shaped_snd_effect[i]); } + } } // Record the color from Tux Paint: -void mosaic_shaped_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) +void mosaic_shaped_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, + Uint8 g, Uint8 b) { mosaic_shaped_r = r; mosaic_shaped_g = g; @@ -352,14 +393,16 @@ void mosaic_shaped_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, } // Use colors: -int mosaic_shaped_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int mosaic_shaped_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 1; } //Sharpen a pixel static void mosaic_shaped_sharpen_pixel(void *ptr, - SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last, int x, int y) + SDL_Surface * canvas ATTRIBUTE_UNUSED, + SDL_Surface * last, int x, int y) { magic_api *api = (magic_api *) ptr; Uint8 r1, g1, b1; @@ -381,29 +424,32 @@ static void mosaic_shaped_sharpen_pixel(void *ptr, sobel_2 = 0; for (i = -1; i < 2; i++) + { + for (j = -1; j < 2; j++) { - for (j = -1; j < 2; j++) - { - //No need to check if inside canvas, getpixel does it for us. - SDL_GetRGB(api->getpixel(last, x + i, y + j), last->format, &r1, &g1, &b1); - grey = mosaic_shaped_grey(r1, g1, b1); - sobel_1 += grey * sobel_weights_1[i + 1][j + 1]; - sobel_2 += grey * sobel_weights_2[i + 1][j + 1]; - } + //No need to check if inside canvas, getpixel does it for us. + SDL_GetRGB(api->getpixel(last, x + i, y + j), last->format, &r1, &g1, + &b1); + grey = mosaic_shaped_grey(r1, g1, b1); + sobel_1 += grey * sobel_weights_1[i + 1][j + 1]; + sobel_2 += grey * sobel_weights_2[i + 1][j + 1]; } + } temp = sqrt(sobel_1 * sobel_1 + sobel_2 * sobel_2); temp = (temp / 1443) * 255.0; if (temp > 25) - { - api->putpixel(canvas_shaped, x, y, SDL_MapRGBA(canvas_shaped->format, 0, 0, 0, 0)); - } + { + api->putpixel(canvas_shaped, x, y, + SDL_MapRGBA(canvas_shaped->format, 0, 0, 0, 0)); + } } -void mosaic_shaped_switchin(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas) +void mosaic_shaped_switchin(magic_api * api, int which, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas) { int y, x; int i, j; @@ -411,29 +457,33 @@ void mosaic_shaped_switchin(magic_api * api, int which, int mode ATTRIBUTE_UNUSE SDL_Surface *surf_aux, *tmp, *tmp2; Uint32 amask; - mosaic_shaped_counted = (Uint8 *) malloc(sizeof(Uint8) * (canvas->w * canvas->h)); + mosaic_shaped_counted = + (Uint8 *) malloc(sizeof(Uint8) * (canvas->w * canvas->h)); if (mosaic_shaped_counted == NULL) - { - fprintf(stderr, "\nError: Can't build drawing touch mask!\n"); - exit(1); - } + { + fprintf(stderr, "\nError: Can't build drawing touch mask!\n"); + exit(1); + } - mosaic_shaped_done = (Uint8 *) malloc(sizeof(Uint8) * (canvas->w * canvas->h)); + mosaic_shaped_done = + (Uint8 *) malloc(sizeof(Uint8) * (canvas->w * canvas->h)); if (mosaic_shaped_done == NULL) - { - fprintf(stderr, "\nError: Can't build drawing touch mask!\n"); - exit(1); - } + { + fprintf(stderr, "\nError: Can't build drawing touch mask!\n"); + exit(1); + } - amask = ~(canvas->format->Rmask | canvas->format->Gmask | canvas->format->Bmask); + amask = + ~(canvas->format->Rmask | canvas->format->Gmask | canvas->format->Bmask); tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->w, canvas->h, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, amask); + canvas->format->Rmask, canvas->format->Gmask, + canvas->format->Bmask, amask); canvas_shaped = SDL_ConvertSurfaceFormat(tmp, SDL_PIXELFORMAT_RGB888, 0); SDL_FreeSurface(tmp); @@ -441,130 +491,192 @@ void mosaic_shaped_switchin(magic_api * api, int which, int mode ATTRIBUTE_UNUSE canvas->w + 10, canvas->h + 10, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, amask); + canvas->format->Rmask, canvas->format->Gmask, + canvas->format->Bmask, amask); surf_aux = SDL_ConvertSurfaceFormat(tmp2, SDL_PIXELFORMAT_RGB888, 0); SDL_FreeSurface(tmp2); /* Generation of patterns now in the program, solves #210 */ if (which == TOOL_SQUARE) + { + mosaic_shaped_pattern = SDL_CreateRGBSurface(SDL_SWSURFACE, + 16, + 16, + canvas->format->BitsPerPixel, + canvas->format->Rmask, + canvas->format->Gmask, + canvas->format->Bmask, + amask); + SDL_FillRect(mosaic_shaped_pattern, NULL, + SDL_MapRGBA(mosaic_shaped_pattern->format, 255, 255, 255, + SDL_ALPHA_OPAQUE)); + /* Shape */ + for (i = 0; i < mosaic_shaped_pattern->w; i++) { - mosaic_shaped_pattern = SDL_CreateRGBSurface(SDL_SWSURFACE, - 16, - 16, - canvas->format->BitsPerPixel, - canvas->format->Rmask, - canvas->format->Gmask, canvas->format->Bmask, amask); - SDL_FillRect(mosaic_shaped_pattern, NULL, - SDL_MapRGBA(mosaic_shaped_pattern->format, 255, 255, 255, SDL_ALPHA_OPAQUE)); - /* Shape */ - for (i = 0; i < mosaic_shaped_pattern->w; i++) - { - api->putpixel(mosaic_shaped_pattern, 0, i, - SDL_MapRGBA(mosaic_shaped_pattern->format, 0, 0, 0, SDL_ALPHA_OPAQUE)); - api->putpixel(mosaic_shaped_pattern, mosaic_shaped_pattern->w - 1, i, - SDL_MapRGBA(mosaic_shaped_pattern->format, 0, 0, 0, SDL_ALPHA_OPAQUE)); - api->putpixel(mosaic_shaped_pattern, i, 0, - SDL_MapRGBA(mosaic_shaped_pattern->format, 0, 0, 0, SDL_ALPHA_OPAQUE)); - api->putpixel(mosaic_shaped_pattern, i, mosaic_shaped_pattern->w - 1, - SDL_MapRGBA(mosaic_shaped_pattern->format, 0, 0, 0, SDL_ALPHA_OPAQUE)); - } - /* Shadow */ - for (i = 1; i < mosaic_shaped_pattern->w - 1; i++) - { - api->putpixel(mosaic_shaped_pattern, 1, i, - SDL_MapRGBA(mosaic_shaped_pattern->format, 128, 128, 128, SDL_ALPHA_OPAQUE)); - api->putpixel(mosaic_shaped_pattern, mosaic_shaped_pattern->w - 2, i, - SDL_MapRGBA(mosaic_shaped_pattern->format, 128, 128, 128, SDL_ALPHA_OPAQUE)); - api->putpixel(mosaic_shaped_pattern, i, 1, - SDL_MapRGBA(mosaic_shaped_pattern->format, 128, 128, 128, SDL_ALPHA_OPAQUE)); - api->putpixel(mosaic_shaped_pattern, i, mosaic_shaped_pattern->w - 2, - SDL_MapRGBA(mosaic_shaped_pattern->format, 128, 128, 128, SDL_ALPHA_OPAQUE)); - - } - api->putpixel(mosaic_shaped_pattern, 2, 2, - SDL_MapRGBA(mosaic_shaped_pattern->format, 152, 152, 152, SDL_ALPHA_OPAQUE)); - api->putpixel(mosaic_shaped_pattern, 2, mosaic_shaped_pattern->h - 3, - SDL_MapRGBA(mosaic_shaped_pattern->format, 152, 152, 152, SDL_ALPHA_OPAQUE)); - api->putpixel(mosaic_shaped_pattern, mosaic_shaped_pattern->w - 3, 2, - SDL_MapRGBA(mosaic_shaped_pattern->format, 152, 152, 152, SDL_ALPHA_OPAQUE)); - api->putpixel(mosaic_shaped_pattern, mosaic_shaped_pattern->w - 3, mosaic_shaped_pattern->h - 3, - SDL_MapRGBA(mosaic_shaped_pattern->format, 152, 152, 152, SDL_ALPHA_OPAQUE)); + api->putpixel(mosaic_shaped_pattern, 0, i, + SDL_MapRGBA(mosaic_shaped_pattern->format, 0, 0, 0, + SDL_ALPHA_OPAQUE)); + api->putpixel(mosaic_shaped_pattern, mosaic_shaped_pattern->w - 1, i, + SDL_MapRGBA(mosaic_shaped_pattern->format, 0, 0, 0, + SDL_ALPHA_OPAQUE)); + api->putpixel(mosaic_shaped_pattern, i, 0, + SDL_MapRGBA(mosaic_shaped_pattern->format, 0, 0, 0, + SDL_ALPHA_OPAQUE)); + api->putpixel(mosaic_shaped_pattern, i, mosaic_shaped_pattern->w - 1, + SDL_MapRGBA(mosaic_shaped_pattern->format, 0, 0, 0, + SDL_ALPHA_OPAQUE)); } + /* Shadow */ + for (i = 1; i < mosaic_shaped_pattern->w - 1; i++) + { + api->putpixel(mosaic_shaped_pattern, 1, i, + SDL_MapRGBA(mosaic_shaped_pattern->format, 128, 128, 128, + SDL_ALPHA_OPAQUE)); + api->putpixel(mosaic_shaped_pattern, mosaic_shaped_pattern->w - 2, i, + SDL_MapRGBA(mosaic_shaped_pattern->format, 128, 128, 128, + SDL_ALPHA_OPAQUE)); + api->putpixel(mosaic_shaped_pattern, i, 1, + SDL_MapRGBA(mosaic_shaped_pattern->format, 128, 128, 128, + SDL_ALPHA_OPAQUE)); + api->putpixel(mosaic_shaped_pattern, i, mosaic_shaped_pattern->w - 2, + SDL_MapRGBA(mosaic_shaped_pattern->format, 128, 128, 128, + SDL_ALPHA_OPAQUE)); + + } + api->putpixel(mosaic_shaped_pattern, 2, 2, + SDL_MapRGBA(mosaic_shaped_pattern->format, 152, 152, 152, + SDL_ALPHA_OPAQUE)); + api->putpixel(mosaic_shaped_pattern, 2, mosaic_shaped_pattern->h - 3, + SDL_MapRGBA(mosaic_shaped_pattern->format, 152, 152, 152, + SDL_ALPHA_OPAQUE)); + api->putpixel(mosaic_shaped_pattern, mosaic_shaped_pattern->w - 3, 2, + SDL_MapRGBA(mosaic_shaped_pattern->format, 152, 152, 152, + SDL_ALPHA_OPAQUE)); + api->putpixel(mosaic_shaped_pattern, mosaic_shaped_pattern->w - 3, + mosaic_shaped_pattern->h - 3, + SDL_MapRGBA(mosaic_shaped_pattern->format, 152, 152, 152, + SDL_ALPHA_OPAQUE)); + } else if (which == TOOL_IRREGULAR) - { - mosaic_shaped_pattern = SDL_CreateRGBSurface(SDL_SWSURFACE, - 64, - 64, - canvas->format->BitsPerPixel, - canvas->format->Rmask, - canvas->format->Gmask, canvas->format->Bmask, amask); - SDL_FillRect(mosaic_shaped_pattern, NULL, - SDL_MapRGBA(mosaic_shaped_pattern->format, 255, 255, 255, SDL_ALPHA_OPAQUE)); + { + mosaic_shaped_pattern = SDL_CreateRGBSurface(SDL_SWSURFACE, + 64, + 64, + canvas->format->BitsPerPixel, + canvas->format->Rmask, + canvas->format->Gmask, + canvas->format->Bmask, + amask); + SDL_FillRect(mosaic_shaped_pattern, NULL, + SDL_MapRGBA(mosaic_shaped_pattern->format, 255, 255, 255, + SDL_ALPHA_OPAQUE)); - /* Start/end of lines taken from the original mosaic_shaped_irregular_pattern.png */ - api->line(api, which, mosaic_shaped_pattern, NULL, 0, 8, 36, 23, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 0, 43, 36, 23, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 0, 26, 28, 53, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 0, 54, 10, 63, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 55, 0, 36, 23, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 63, 43, 28, 53, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 24, 63, 28, 53, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 24, 0, 27, 19, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 63, 8, 50, 6, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 10, 0, 4, 10, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 10, 0, 25, 7, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 41, 0, 26, 12, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 41, 63, 28, 53, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 41, 63, 56, 58, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 63, 53, 55, 45, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 55, 63, 59, 49, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 10, 63, 20, 45, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 63, 26, 40, 18, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 4, 30, 14, 14, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 18, 33, 21, 17, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 23, 48, 29, 27, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 37, 50, 36, 23, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 44, 13, 37, 3, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 59, 24, 55, 7, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 49, 47, 54, 23, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 36, 35, 51, 37, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 61, 44, 52, 31, 1, mosaic_shaped_paint); - } + /* Start/end of lines taken from the original mosaic_shaped_irregular_pattern.png */ + api->line(api, which, mosaic_shaped_pattern, NULL, 0, 8, 36, 23, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 0, 43, 36, 23, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 0, 26, 28, 53, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 0, 54, 10, 63, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 55, 0, 36, 23, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 63, 43, 28, 53, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 24, 63, 28, 53, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 24, 0, 27, 19, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 63, 8, 50, 6, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 10, 0, 4, 10, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 10, 0, 25, 7, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 41, 0, 26, 12, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 41, 63, 28, 53, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 41, 63, 56, 58, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 63, 53, 55, 45, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 55, 63, 59, 49, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 10, 63, 20, 45, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 63, 26, 40, 18, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 4, 30, 14, 14, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 18, 33, 21, 17, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 23, 48, 29, 27, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 37, 50, 36, 23, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 44, 13, 37, 3, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 59, 24, 55, 7, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 49, 47, 54, 23, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 36, 35, 51, 37, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 61, 44, 52, 31, 1, + mosaic_shaped_paint); + } else if (which == TOOL_HEX) - { - mosaic_shaped_pattern = SDL_CreateRGBSurface(SDL_SWSURFACE, - 48, - 28, - canvas->format->BitsPerPixel, - canvas->format->Rmask, - canvas->format->Gmask, canvas->format->Bmask, amask); - SDL_FillRect(mosaic_shaped_pattern, NULL, - SDL_MapRGBA(mosaic_shaped_pattern->format, 255, 255, 255, SDL_ALPHA_OPAQUE)); + { + mosaic_shaped_pattern = SDL_CreateRGBSurface(SDL_SWSURFACE, + 48, + 28, + canvas->format->BitsPerPixel, + canvas->format->Rmask, + canvas->format->Gmask, + canvas->format->Bmask, + amask); + SDL_FillRect(mosaic_shaped_pattern, NULL, + SDL_MapRGBA(mosaic_shaped_pattern->format, 255, 255, 255, + SDL_ALPHA_OPAQUE)); - api->line(api, which, mosaic_shaped_pattern, NULL, 0, 16, 8, 0, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 8, 0, 26, 0, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 26, 0, 32, 14, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 32, 14, 26, 27, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 32, 14, 47, 14, 1, mosaic_shaped_paint); - api->line(api, which, mosaic_shaped_pattern, NULL, 0, 13, 8, 27, 1, mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 0, 16, 8, 0, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 8, 0, 26, 0, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 26, 0, 32, 14, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 32, 14, 26, 27, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 32, 14, 47, 14, 1, + mosaic_shaped_paint); + api->line(api, which, mosaic_shaped_pattern, NULL, 0, 13, 8, 27, 1, + mosaic_shaped_paint); - //make pattern more accurate - api->putpixel(mosaic_shaped_pattern, 9, 27, - SDL_MapRGBA(mosaic_shaped_pattern->format, 0, 0, 0, SDL_ALPHA_OPAQUE)); - api->putpixel(mosaic_shaped_pattern, 9, 26, - SDL_MapRGBA(mosaic_shaped_pattern->format, 0, 0, 0, SDL_ALPHA_OPAQUE)); - api->putpixel(mosaic_shaped_pattern, 26, 27, - SDL_MapRGBA(mosaic_shaped_pattern->format, 0, 0, 0, SDL_ALPHA_OPAQUE)); - api->putpixel(mosaic_shaped_pattern, 26, 26, - SDL_MapRGBA(mosaic_shaped_pattern->format, 0, 0, 0, SDL_ALPHA_OPAQUE)); - api->putpixel(mosaic_shaped_pattern, 26, 25, - SDL_MapRGBA(mosaic_shaped_pattern->format, 0, 0, 0, SDL_ALPHA_OPAQUE)); - api->putpixel(mosaic_shaped_pattern, 25, 27, - SDL_MapRGBA(mosaic_shaped_pattern->format, 0, 0, 0, SDL_ALPHA_OPAQUE)); + //make pattern more accurate + api->putpixel(mosaic_shaped_pattern, 9, 27, + SDL_MapRGBA(mosaic_shaped_pattern->format, 0, 0, 0, + SDL_ALPHA_OPAQUE)); + api->putpixel(mosaic_shaped_pattern, 9, 26, + SDL_MapRGBA(mosaic_shaped_pattern->format, 0, 0, 0, + SDL_ALPHA_OPAQUE)); + api->putpixel(mosaic_shaped_pattern, 26, 27, + SDL_MapRGBA(mosaic_shaped_pattern->format, 0, 0, 0, + SDL_ALPHA_OPAQUE)); + api->putpixel(mosaic_shaped_pattern, 26, 26, + SDL_MapRGBA(mosaic_shaped_pattern->format, 0, 0, 0, + SDL_ALPHA_OPAQUE)); + api->putpixel(mosaic_shaped_pattern, 26, 25, + SDL_MapRGBA(mosaic_shaped_pattern->format, 0, 0, 0, + SDL_ALPHA_OPAQUE)); + api->putpixel(mosaic_shaped_pattern, 25, 27, + SDL_MapRGBA(mosaic_shaped_pattern->format, 0, 0, 0, + SDL_ALPHA_OPAQUE)); - } + } SDL_SetSurfaceBlendMode(mosaic_shaped_pattern, SDL_BLENDMODE_NONE); @@ -573,16 +685,16 @@ void mosaic_shaped_switchin(magic_api * api, int which, int mode ATTRIBUTE_UNUSE for (i = 0; i < surf_aux->w; i += mosaic_shaped_pattern->w) for (j = 0; j < surf_aux->h; j += mosaic_shaped_pattern->h) - { - rect.x = i; - rect.y = j; - SDL_BlitSurface(mosaic_shaped_pattern, NULL, surf_aux, &rect); - } + { + rect.x = i; + rect.y = j; + SDL_BlitSurface(mosaic_shaped_pattern, NULL, surf_aux, &rect); + } if (which == TOOL_IRREGULAR) - { - deform(api, surf_aux); - } + { + deform(api, surf_aux); + } SDL_SetSurfaceBlendMode(surf_aux, SDL_BLENDMODE_NONE); SDL_BlitSurface(surf_aux, NULL, canvas_shaped, NULL); @@ -592,43 +704,47 @@ void mosaic_shaped_switchin(magic_api * api, int which, int mode ATTRIBUTE_UNUSE /* Two black lines at the edges */ for (i = 0; i < canvas->w; i++) - { - api->putpixel(canvas_shaped, i, 0, black); - api->putpixel(canvas_shaped, i, 1, black); - api->putpixel(canvas_shaped, i, canvas->h - 1, black); - api->putpixel(canvas_shaped, i, canvas->h - 2, black); - } + { + api->putpixel(canvas_shaped, i, 0, black); + api->putpixel(canvas_shaped, i, 1, black); + api->putpixel(canvas_shaped, i, canvas->h - 1, black); + api->putpixel(canvas_shaped, i, canvas->h - 2, black); + } for (j = 0; j < canvas->h; j++) - { - api->putpixel(canvas_shaped, 0, j, black); - api->putpixel(canvas_shaped, 1, j, black); - api->putpixel(canvas_shaped, canvas->w - 1, j, black); - api->putpixel(canvas_shaped, canvas->w - 2, j, black); - } + { + api->putpixel(canvas_shaped, 0, j, black); + api->putpixel(canvas_shaped, 1, j, black); + api->putpixel(canvas_shaped, canvas->w - 1, j, black); + api->putpixel(canvas_shaped, canvas->w - 2, j, black); + } /* A copy of canvas at switchin, will be used to draw from it as snapshot changes at each click */ canvas_back = SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->w, canvas->h, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, amask); + canvas->format->Rmask, + canvas->format->Gmask, + canvas->format->Bmask, amask); SDL_BlitSurface(canvas, NULL, canvas_back, NULL); if (which != TOOL_SQUARE) /* The pattern for square is small enouth to not need an additional shape */ for (y = 0; y < canvas->h; y++) + { + for (x = 0; x < canvas->w; x++) { - for (x = 0; x < canvas->w; x++) - { - mosaic_shaped_sharpen_pixel(api, canvas_shaped, canvas, x, y); - } + mosaic_shaped_sharpen_pixel(api, canvas_shaped, canvas, x, y); } + } reset_counter(canvas, mosaic_shaped_counted); reset_counter(canvas, mosaic_shaped_done); } -void mosaic_shaped_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void mosaic_shaped_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { SDL_FreeSurface(canvas_shaped); @@ -637,7 +753,8 @@ void mosaic_shaped_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBU free(mosaic_shaped_counted); } -int mosaic_shaped_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int mosaic_shaped_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_PAINT | MODE_FULLSCREEN); } @@ -648,16 +765,17 @@ void reset_counter(SDL_Surface * canvas, Uint8 * counter) for (j = 0; j < canvas->h; j++) for (i = 0; i < canvas->w; i++) - { - counter[j * canvas->w + i] = 0; - } + { + counter[j * canvas->w + i] = 0; + } } int scan_fill_count; -int scan_fill(magic_api * api, SDL_Surface * canvas, SDL_Surface * srfc, int x, int y, int fill_edge, int fill_tile, - int size, Uint32 color) +int scan_fill(magic_api * api, SDL_Surface * canvas, SDL_Surface * srfc, + int x, int y, int fill_edge, int fill_tile, int size, + Uint32 color) { int leftx, rightx; Uint8 r, g, b, a; @@ -669,92 +787,100 @@ int scan_fill(magic_api * api, SDL_Surface * canvas, SDL_Surface * srfc, int x, /* Abort, if we recurse too deep! -bjk 2014.08.05 */ scan_fill_count++; if (scan_fill_count > 500) - { - scan_fill_count--; - return (0); - } + { + scan_fill_count--; + return (0); + } if (mosaic_shaped_counted[y * canvas->w + x] == 1) - { - scan_fill_count--; - return (0); - } + { + scan_fill_count--; + return (0); + } if (api->getpixel(srfc, x, y) == black) + { + if (fill_edge == 1) { - if (fill_edge == 1) - { - fill_square(api, canvas, x, y, size, color); - } - - scan_fill_count--; - return (0); /* No need to check more */ + fill_square(api, canvas, x, y, size, color); } + scan_fill_count--; + return (0); /* No need to check more */ + } + if (fill_tile == 1) - { - Uint32 shadow; - Uint8 shr, shg, shb, sha; - Uint8 cnvsr, cnvsg, cnvsb, cnvsa; + { + Uint32 shadow; + Uint8 shr, shg, shb, sha; + Uint8 cnvsr, cnvsg, cnvsb, cnvsa; - shadow = api->getpixel(srfc, x, y); - SDL_GetRGBA(shadow, srfc->format, &shr, &shg, &shb, &sha); - SDL_GetRGBA(pixel_average, srfc->format, &cnvsr, &cnvsg, &cnvsb, &cnvsa); - shadow = SDL_MapRGBA(canvas->format, (shr * cnvsr) / 255, (shg * cnvsg) / 255, (shb * cnvsb) / 255, 0); //(shr + cnvsr) /2, ; - api->putpixel(canvas, x, y, shadow); - mosaic_shaped_counted[y * canvas->w + x] = 1; - mosaic_shaped_done[y * canvas->w + x] = 1; - } + shadow = api->getpixel(srfc, x, y); + SDL_GetRGBA(shadow, srfc->format, &shr, &shg, &shb, &sha); + SDL_GetRGBA(pixel_average, srfc->format, &cnvsr, &cnvsg, &cnvsb, &cnvsa); + shadow = SDL_MapRGBA(canvas->format, (shr * cnvsr) / 255, (shg * cnvsg) / 255, (shb * cnvsb) / 255, 0); //(shr + cnvsr) /2, ; + api->putpixel(canvas, x, y, shadow); + mosaic_shaped_counted[y * canvas->w + x] = 1; + mosaic_shaped_done[y * canvas->w + x] = 1; + } else - { - SDL_GetRGBA(api->getpixel(canvas_back, x, y), canvas_back->format, &r, &g, &b, &a); - mosaic_shaped_average_r += r; - mosaic_shaped_average_g += g; - mosaic_shaped_average_b += b; - mosaic_shaped_average_count += 1; - mosaic_shaped_counted[y * canvas->w + x] = 1; - } + { + SDL_GetRGBA(api->getpixel(canvas_back, x, y), canvas_back->format, &r, &g, + &b, &a); + mosaic_shaped_average_r += r; + mosaic_shaped_average_g += g; + mosaic_shaped_average_b += b; + mosaic_shaped_average_count += 1; + mosaic_shaped_counted[y * canvas->w + x] = 1; + } /* Search right */ - while (scan_fill(api, canvas, srfc, rightx, y, fill_edge, fill_tile, size, color) && (rightx < canvas->w)) - { - rightx++; - } + while (scan_fill + (api, canvas, srfc, rightx, y, fill_edge, fill_tile, size, color) + && (rightx < canvas->w)) + { + rightx++; + } /* Search left */ - while (scan_fill(api, canvas, srfc, leftx, y, fill_edge, fill_tile, size, color) && (leftx >= 0)) - { - leftx--; - } + while (scan_fill + (api, canvas, srfc, leftx, y, fill_edge, fill_tile, size, color) + && (leftx >= 0)) + { + leftx--; + } /* Top / bottom */ for (i = leftx; i <= rightx; i++) + { + if (y > 0) { - if (y > 0) - { - scan_fill(api, canvas, srfc, i, y - 1, fill_edge, fill_tile, size, color); - } - - if (y + 1 < canvas->w) - { - scan_fill(api, canvas, srfc, i, y + 1, fill_edge, fill_tile, size, color); - } + scan_fill(api, canvas, srfc, i, y - 1, fill_edge, fill_tile, size, + color); } + if (y + 1 < canvas->w) + { + scan_fill(api, canvas, srfc, i, y + 1, fill_edge, fill_tile, size, + color); + } + } + scan_fill_count--; return (1); } -void fill_square(magic_api * api, SDL_Surface * canvas, int x, int y, int size, Uint32 color) +void fill_square(magic_api * api, SDL_Surface * canvas, int x, int y, + int size, Uint32 color) { int i, j; for (i = (x - size); i < (x + 1 + size); i++) for (j = (y - size); j < (y + 1 + size); j++) - { - api->putpixel(canvas, i, j, color); - } + { + api->putpixel(canvas, i, j, color); + } } void deform(magic_api * api, SDL_Surface * srfc) @@ -763,20 +889,24 @@ void deform(magic_api * api, SDL_Surface * srfc) for (j = 0; j < srfc->h; j++) for (i = 0; i < srfc->w; i++) - { - api->putpixel(srfc, i, j, api->getpixel(srfc, i + sin(j * M_PI / 90) * 10 + 10, j)); - } + { + api->putpixel(srfc, i, j, + api->getpixel(srfc, i + sin(j * M_PI / 90) * 10 + 10, j)); + } for (i = 0; i < srfc->w; i++) for (j = 0; j < srfc->h; j++) - { - api->putpixel(srfc, i, j, api->getpixel(srfc, i, j + sin(i * M_PI / 90) * 10 + 10)); - } + { + api->putpixel(srfc, i, j, + api->getpixel(srfc, i, j + sin(i * M_PI / 90) * 10 + 10)); + } } /* Paints a 2 pixel square with black and shadows around 3 more pixels */ -static void mosaic_shaped_paint(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, - SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) +static void mosaic_shaped_paint(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, int x, + int y) { magic_api *api = (magic_api *) ptr; int radius, shadow; @@ -791,41 +921,42 @@ static void mosaic_shaped_paint(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surfa for (i = -(radius + shadow); i < (radius + shadow); i++) for (j = -(radius + shadow); j < (radius + shadow); j++) + { + /* Ensure effects on the edges reaches the opposite side if necessary */ + ii = x + i; + if (ii < 0) + ii += canvas->w; + if (ii >= canvas->w) + ii -= canvas->w; + jj = y + j; + if (jj < 0) + jj += canvas->h; + if (jj >= canvas->h) + ii -= canvas->h; + + /* Shadow_tone is also used as a marker, anything already painted on black must finally be black */ + shadow_tone = api->getpixel(canvas, ii, jj); + + // if (abs(i) <= radius && abs(j) <= radius) + if (0 <= i && i <= 1 && 0 <= j && j <= 1) + api->putpixel(canvas, ii, jj, black); + + else if (api->in_circle(i, j, radius + shadow) && shadow_tone != black) { - /* Ensure effects on the edges reaches the opposite side if necessary */ - ii = x + i; - if (ii < 0) - ii += canvas->w; - if (ii >= canvas->w) - ii -= canvas->w; - jj = y + j; - if (jj < 0) - jj += canvas->h; - if (jj >= canvas->h) - ii -= canvas->h; + SDL_GetRGBA(shadow_tone, canvas->format, &r, &g, &b, &a); - /* Shadow_tone is also used as a marker, anything already painted on black must finally be black */ - shadow_tone = api->getpixel(canvas, ii, jj); + /* Shadows should be shadows, not black */ + if (r > 10) + r -= 9; + if (g > 10) + g -= 9; + if (b > 10) + b -= 9; - // if (abs(i) <= radius && abs(j) <= radius) - if (0 <= i && i <= 1 && 0 <= j && j <= 1) - api->putpixel(canvas, ii, jj, black); - - else if (api->in_circle(i, j, radius + shadow) && shadow_tone != black) - { - SDL_GetRGBA(shadow_tone, canvas->format, &r, &g, &b, &a); - - /* Shadows should be shadows, not black */ - if (r > 10) - r -= 9; - if (g > 10) - g -= 9; - if (b > 10) - b -= 9; - - api->putpixel(canvas, ii, jj, SDL_MapRGBA(canvas->format, r, g, b, SDL_ALPHA_OPAQUE)); - - } + api->putpixel(canvas, ii, jj, + SDL_MapRGBA(canvas->format, r, g, b, SDL_ALPHA_OPAQUE)); } + + } } diff --git a/magic/src/negative.c b/magic/src/negative.c index 239a3423e..2fafc93c6 100644 --- a/magic/src/negative.c +++ b/magic/src/negative.c @@ -42,19 +42,25 @@ SDL_Surface *negative_get_icon(magic_api * api, int which); char *negative_get_name(magic_api * api, int which); int negative_get_group(magic_api * api, int which); char *negative_get_description(magic_api * api, int which, int mode); -static void do_negative(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +static void do_negative(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); void negative_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 negative_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void negative_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 negative_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, + SDL_Rect * update_rect); void negative_shutdown(magic_api * api); void negative_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int negative_requires_colors(magic_api * api, int which); -void negative_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void negative_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void negative_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void negative_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int negative_modes(magic_api * api, int which); enum @@ -76,13 +82,14 @@ const char *negative_names[negative_NUM_TOOLS] = { const char *negative_descs[negative_NUM_TOOLS][2] = { { - gettext_noop("Click and drag the mouse around to make your painting negative."), - gettext_noop("Click to turn your painting into its negative.") - }, + gettext_noop + ("Click and drag the mouse around to make your painting negative."), + gettext_noop("Click to turn your painting into its negative.")}, { - gettext_noop("Click and drag the mouse around to change colors to their opposites -- their complementary colors."), - gettext_noop("Click to turn all colors in your painting into their opposites -- their complementary colors.") - }, + gettext_noop + ("Click and drag the mouse around to change colors to their opposites -- their complementary colors."), + gettext_noop + ("Click to turn all colors in your painting into their opposites -- their complementary colors.")}, }; @@ -90,7 +97,8 @@ int negative_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/negative.wav", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/negative.wav", + api->data_directory); negative_snd = Mix_LoadWAV(fname); @@ -112,7 +120,8 @@ SDL_Surface *negative_get_icon(magic_api * api, int which) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, negative_icon_filenames[which]); + snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, + negative_icon_filenames[which]); return (IMG_Load(fname)); } @@ -123,93 +132,105 @@ char *negative_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) } // Return our group (both the same): -int negative_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int negative_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_COLOR_FILTERS; } // Return our description, localized: -char *negative_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode) +char *negative_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, + int mode) { int mode_idx; - if (mode == MODE_PAINT) { + if (mode == MODE_PAINT) + { mode_idx = 0; - } else if (mode == MODE_FULLSCREEN) { + } + else if (mode == MODE_FULLSCREEN) + { mode_idx = 1; - } else { + } + else + { return NULL; } - return(strdup(gettext_noop(negative_descs[which][mode_idx]))); + return (strdup(gettext_noop(negative_descs[which][mode_idx]))); } -static void negative_calc(void *ptr, int which, Uint8 r, Uint8 g, Uint8 b, Uint8 * new_r, Uint8 * new_g, Uint8 * new_b) { +static void negative_calc(void *ptr, int which, Uint8 r, Uint8 g, Uint8 b, + Uint8 * new_r, Uint8 * new_g, Uint8 * new_b) +{ float h, s, v, new_h; magic_api *api = (magic_api *) ptr; if (which == TOOL_NEGATIVE) - { - *new_r = 0xFF - r; - *new_g = 0xFF - g; - *new_b = 0xFF - b; - } + { + *new_r = 0xFF - r; + *new_g = 0xFF - g; + *new_b = 0xFF - b; + } else + { + api->rgbtohsv(r, g, b, &h, &s, &v); + new_h = h + 180.0; + if (new_h >= 360.0) { - api->rgbtohsv(r, g, b, &h, &s, &v); - new_h = h + 180.0; - if (new_h >= 360.0) - { - new_h = new_h - 360.0; - } - api->hsvtorgb(new_h, s, v, new_r, new_g, new_b); + new_h = new_h - 360.0; } + api->hsvtorgb(new_h, s, v, new_r, new_g, new_b); + } } // Callback that does the negative color effect on a circle centered around x,y -static void do_negative(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void do_negative(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y) { int xx, yy; Uint8 r, g, b, new_r, new_g, new_b; magic_api *api = (magic_api *) ptr; for (yy = y - 16; yy < y + 16; yy++) + { + for (xx = x - 16; xx < x + 16; xx++) { - for (xx = x - 16; xx < x + 16; xx++) - { - if (api->in_circle(xx - x, yy - y, 16)) - { - SDL_GetRGB(api->getpixel(last, xx, yy), last->format, &r, &g, &b); - negative_calc(api, which, r, g, b, &new_r, &new_g, &new_b); - api->putpixel(canvas, xx, yy, SDL_MapRGB(canvas->format, new_r, new_g, new_b)); - } - } + if (api->in_circle(xx - x, yy - y, 16)) + { + SDL_GetRGB(api->getpixel(last, xx, yy), last->format, &r, &g, &b); + negative_calc(api, which, r, g, b, &new_r, &new_g, &new_b); + api->putpixel(canvas, xx, yy, + SDL_MapRGB(canvas->format, new_r, new_g, new_b)); + } } + } } // Ask Tux Paint to call our 'do_negative()' callback over a line void negative_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) { SDL_LockSurface(last); SDL_LockSurface(canvas); - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_negative); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, do_negative); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = ox - 16; update_rect->y = oy - 16; @@ -225,38 +246,43 @@ void negative_drag(magic_api * api, int which, SDL_Surface * canvas, // Ask Tux Paint to call our 'do_negative()' callback at a single point void negative_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) { if (mode == MODE_PAINT) negative_drag(api, which, canvas, last, x, y, x, y, update_rect); else + { + int xx, yy; + Uint8 r, g, b, new_r, new_g, new_b; + + for (yy = 0; yy < canvas->h; yy++) { - int xx, yy; - Uint8 r, g, b, new_r, new_g, new_b; - - for (yy = 0; yy < canvas->h; yy++) - { - for (xx = 0; xx < canvas->w; xx++) - { - SDL_GetRGB(api->getpixel(last, xx, yy), last->format, &r, &g, &b); - negative_calc(api, which, r, g, b, &new_r, &new_g, &new_b); - api->putpixel(canvas, xx, yy, SDL_MapRGB(canvas->format, new_r, new_g, new_b)); - } - } - - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; - - api->playsound(negative_snd, (x * 255) / canvas->w, 255); + for (xx = 0; xx < canvas->w; xx++) + { + SDL_GetRGB(api->getpixel(last, xx, yy), last->format, &r, &g, &b); + negative_calc(api, which, r, g, b, &new_r, &new_g, &new_b); + api->putpixel(canvas, xx, yy, + SDL_MapRGB(canvas->format, new_r, new_g, new_b)); + } } + + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; + + api->playsound(negative_snd, (x * 255) / canvas->w, 255); + } } -void negative_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) +void negative_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) { } @@ -268,29 +294,33 @@ void negative_shutdown(magic_api * api ATTRIBUTE_UNUSED) } // We don't use colors -void negative_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, +void negative_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) { } // We don't use colors -int negative_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int negative_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void negative_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void negative_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void negative_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void negative_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -int negative_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int negative_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_PAINT | MODE_FULLSCREEN); } - diff --git a/magic/src/noise.c b/magic/src/noise.c index 3b8fbd745..7c2280800 100644 --- a/magic/src/noise.c +++ b/magic/src/noise.c @@ -71,7 +71,8 @@ const int noise_groups[noise_NUM_TOOLS] = { }; const char *noise_descs[noise_NUM_TOOLS][2] = { - {gettext_noop("Click and drag the mouse to add noise to parts of your picture."), + {gettext_noop + ("Click and drag the mouse to add noise to parts of your picture."), gettext_noop("Click to add noise to your entire picture."),}, }; @@ -81,20 +82,26 @@ SDL_Surface *noise_get_icon(magic_api * api, int which); char *noise_get_name(magic_api * api, int which); int noise_get_group(magic_api * api, int which); char *noise_get_description(magic_api * api, int which, int mode); -static void do_noise_pixel(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); -static void do_noise_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int which); -static void do_noise_brush(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +static void do_noise_pixel(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); +static void do_noise_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, + int which); +static void do_noise_brush(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); void noise_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void noise_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void noise_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void noise_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); +void noise_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); void noise_shutdown(magic_api * api); void noise_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int noise_requires_colors(magic_api * api, int which); -void noise_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void noise_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void noise_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void noise_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int noise_modes(magic_api * api, int which); int noise_get_tool_count(magic_api * api ATTRIBUTE_UNUSED); @@ -112,10 +119,11 @@ int noise_init(magic_api * api) srand(time(0)); for (i = 0; i < noise_NUM_TOOLS; i++) - { - snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, noise_snd_filenames[i]); - noise_snd_effect[i] = Mix_LoadWAV(fname); - } + { + snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, + noise_snd_filenames[i]); + noise_snd_effect[i] = Mix_LoadWAV(fname); + } return (1); } @@ -129,7 +137,8 @@ SDL_Surface *noise_get_icon(magic_api * api, int which) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, noise_icon_filenames[which]); + snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, + noise_icon_filenames[which]); return (IMG_Load(fname)); } @@ -146,14 +155,16 @@ int noise_get_group(magic_api * api ATTRIBUTE_UNUSED, int which) } // Return our descriptions, localized: -char *noise_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode) +char *noise_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, + int mode) { return (strdup(gettext_noop(noise_descs[which][mode - 1]))); } //Do the effect for one pixel static void do_noise_pixel(void *ptr, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) + SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { magic_api *api = (magic_api *) ptr; @@ -161,70 +172,79 @@ static void do_noise_pixel(void *ptr, int which ATTRIBUTE_UNUSED, double temp2[3]; int k; - SDL_GetRGB(api->getpixel(canvas, x, y), canvas->format, &temp[0], &temp[1], &temp[2]); + SDL_GetRGB(api->getpixel(canvas, x, y), canvas->format, &temp[0], &temp[1], + &temp[2]); for (k = 0; k < 3; k++) - { - temp2[k] = clamp(0.0, (int)temp[k] - (rand() % noise_AMOUNT) + noise_AMOUNT / 2.0, 255.0); - } - api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, temp2[0], temp2[1], temp2[2])); + { + temp2[k] = + clamp(0.0, (int) temp[k] - (rand() % noise_AMOUNT) + noise_AMOUNT / 2.0, + 255.0); + } + api->putpixel(canvas, x, y, + SDL_MapRGB(canvas->format, temp2[0], temp2[1], temp2[2])); } // Do the effect for the full image -static void do_noise_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int which) +static void do_noise_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, + int which) { int x, y; for (y = 0; y < last->h; y++) + { + for (x = 0; x < last->w; x++) { - for (x = 0; x < last->w; x++) - { - do_noise_pixel(ptr, which, canvas, last, x, y); - } + do_noise_pixel(ptr, which, canvas, last, x, y); } + } } //do the effect for the brush -static void do_noise_brush(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void do_noise_brush(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y) { int xx, yy; magic_api *api = (magic_api *) ptr; for (yy = y - noise_RADIUS; yy < y + noise_RADIUS; yy++) + { + for (xx = x - noise_RADIUS; xx < x + noise_RADIUS; xx++) { - for (xx = x - noise_RADIUS; xx < x + noise_RADIUS; xx++) - { - if (api->in_circle(xx - x, yy - y, noise_RADIUS) && !api->touched(xx, yy)) - { - do_noise_pixel(api, which, canvas, last, xx, yy); - } - } + if (api->in_circle(xx - x, yy - y, noise_RADIUS) + && !api->touched(xx, yy)) + { + do_noise_pixel(api, which, canvas, last, xx, yy); + } } + } } // Affect the canvas on drag: void noise_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) { - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_noise_brush); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, + do_noise_brush); api->playsound(noise_snd_effect[which], (x * 255) / canvas->w, 255); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = ox - noise_RADIUS; update_rect->y = oy - noise_RADIUS; @@ -234,25 +254,29 @@ void noise_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void noise_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) { if (mode == MODE_PAINT) noise_drag(api, which, canvas, last, x, y, x, y, update_rect); else - { - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; - do_noise_full(api, canvas, last, which); - api->playsound(noise_snd_effect[which], 128, 255); - } + { + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; + do_noise_full(api, canvas, last, which); + api->playsound(noise_snd_effect[which], 128, 255); + } } // Affect the canvas on release: -void noise_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) +void noise_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) { } @@ -263,32 +287,36 @@ void noise_shutdown(magic_api * api ATTRIBUTE_UNUSED) int i; for (i = 0; i < noise_NUM_TOOLS; i++) + { + if (noise_snd_effect[i] != NULL) { - if (noise_snd_effect[i] != NULL) - { - Mix_FreeChunk(noise_snd_effect[i]); - } + Mix_FreeChunk(noise_snd_effect[i]); } + } } // Record the color from Tux Paint: -void noise_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, +void noise_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) { } // Use colors: -int noise_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int noise_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void noise_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void noise_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void noise_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void noise_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } diff --git a/magic/src/perspective.c b/magic/src/perspective.c index a66a56378..e41dbe1ca 100644 --- a/magic/src/perspective.c +++ b/magic/src/perspective.c @@ -53,7 +53,8 @@ static void perspective_preview(magic_api * api, int which, SDL_Surface * canvas, SDL_Surface * last, - int x, int y, SDL_Rect * update_rect, float step); + int x, int y, SDL_Rect * update_rect, + float step); Uint32 perspective_api_version(void); int perspective_init(magic_api * api); int perspective_get_tool_count(magic_api * api); @@ -64,13 +65,16 @@ int perspective_get_group(magic_api * api, int which); char *perspective_get_description(magic_api * api, int which, int mode); void perspective_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 perspective_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 perspective_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 perspective_shutdown(magic_api * api); @@ -78,16 +82,20 @@ void perspective_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int perspective_requires_colors(magic_api * api, int which); -void perspective_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); +void perspective_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); -void perspective_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void perspective_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int perspective_modes(magic_api * api, int which); -int scan_fill(magic_api * api, SDL_Surface * canvas, SDL_Surface * srfc, int x, int y, int fill_edge, int fill_tile, - int size, Uint32 color); +int scan_fill(magic_api * api, SDL_Surface * canvas, SDL_Surface * srfc, + int x, int y, int fill_edge, int fill_tile, int size, + Uint32 color); -void perspective_line(void *ptr_to_api, int which, SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y); +void perspective_line(void *ptr_to_api, int which, SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y); @@ -104,7 +112,8 @@ int dash; int click_x, click_y; int new_w, new_h, old_h, sound_h; -int perspective_average_r, perspective_average_g, perspective_average_b, perspective_average_count; +int perspective_average_r, perspective_average_g, perspective_average_b, + perspective_average_count; Uint32 pixel_average, black, white; int otop_left_x, otop_left_y, otop_right_x, otop_right_y; @@ -143,10 +152,10 @@ static Mix_Chunk *perspective_snd_effect[perspective_NUM_TOOLS + 1]; const char *perspective_snd_filenames[perspective_NUM_TOOLS + 1] = { "perspective.ogg", - "zoom_down.ogg", /* TODO: Could use a different sound */ + "zoom_down.ogg", /* TODO: Could use a different sound */ "zoom_up.ogg", "zoom_down.ogg", - "zoom_down.ogg", /* TODO: Could use a different sound */ + "zoom_down.ogg", /* TODO: Could use a different sound */ }; const char *perspective_icon_filenames[perspective_NUM_TOOLS] = { @@ -166,15 +175,19 @@ const char *perspective_names[perspective_NUM_TOOLS] = { }; const char *perspective_descs[perspective_NUM_TOOLS] = { - gettext_noop("Click on the corners and drag where you want to stretch the picture."), + gettext_noop + ("Click on the corners and drag where you want to stretch the picture."), gettext_noop("Click to turn your picture into 2-by-2 panels."), - gettext_noop("Click and drag up to zoom in the picture. Drag down to zoom out and tile the picture."), + gettext_noop + ("Click and drag up to zoom in the picture. Drag down to zoom out and tile the picture."), - gettext_noop("Click and drag up to zoom in or drag down to zoom out the picture."), + gettext_noop + ("Click and drag up to zoom in or drag down to zoom out the picture."), - gettext_noop("Click and drag up to rush in or drag down to rush out the picture."), + gettext_noop + ("Click and drag up to rush in or drag down to rush out the picture."), }; Uint32 perspective_api_version(void) @@ -189,10 +202,11 @@ int perspective_init(magic_api * api) char fname[1024]; for (i = 0; i <= perspective_NUM_TOOLS; i++) - { - snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, perspective_snd_filenames[i]); - perspective_snd_effect[i] = Mix_LoadWAV(fname); - } + { + snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, + perspective_snd_filenames[i]); + perspective_snd_effect[i] = Mix_LoadWAV(fname); + } return (1); } @@ -206,7 +220,8 @@ SDL_Surface *perspective_get_icon(magic_api * api, int which) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, perspective_icon_filenames[which]); + snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, + perspective_icon_filenames[which]); return (IMG_Load(fname)); } @@ -217,13 +232,15 @@ char *perspective_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) } // Return our group (the same): -int perspective_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int perspective_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PICTURE_WARPS; } // Return our descriptions, localized: -char *perspective_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode ATTRIBUTE_UNUSED) +char *perspective_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, + int mode ATTRIBUTE_UNUSED) { return (strdup(gettext_noop(perspective_descs[which]))); } @@ -231,114 +248,121 @@ char *perspective_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, i // Affect the canvas on drag: void perspective_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, int x, int y, + SDL_Surface * last, int ox ATTRIBUTE_UNUSED, + int oy ATTRIBUTE_UNUSED, int x, int y, SDL_Rect * update_rect) { switch (which) + { + case TOOL_PERSPECTIVE: { - case TOOL_PERSPECTIVE: + switch (corner) { - switch (corner) - { - case TOP_LEFT: - { - top_left_x = x; - top_left_y = y; - } - break; + case TOP_LEFT: + { + top_left_x = x; + top_left_y = y; + } + break; - case TOP_RIGHT: - { - top_right_x = x; - top_right_y = y; - } - break; + case TOP_RIGHT: + { + top_right_x = x; + top_right_y = y; + } + break; - case BOTTOM_LEFT: - { - bottom_left_x = x; - bottom_left_y = y; - } - break; + case BOTTOM_LEFT: + { + bottom_left_x = x; + bottom_left_y = y; + } + break; - case BOTTOM_RIGHT: - { - bottom_right_x = x; - bottom_right_y = y; - } - break; - } - - SDL_BlitSurface(canvas_back, NULL, canvas, NULL); - - perspective_preview(api, which, canvas, last, x, y, update_rect, 2.0); - - /* Draw a square and the current shape relative to it as a visual reference */ - /* square */ - api->line(api, which, canvas, last, otop_left_x, otop_left_y, otop_right_x, otop_right_y, 1, perspective_line); - api->line(api, which, canvas, last, otop_left_x, otop_left_y, obottom_left_x, obottom_left_y, 1, - perspective_line); - api->line(api, which, canvas, last, obottom_left_x, obottom_left_y, obottom_right_x, obottom_right_y, 1, - perspective_line); - api->line(api, which, canvas, last, obottom_right_x, obottom_right_y, otop_right_x, otop_right_y, 1, - perspective_line); - - /* shape */ - api->line(api, which, canvas, last, top_left_x, top_left_y, top_right_x, top_right_y, 1, perspective_line); - api->line(api, which, canvas, last, top_left_x, top_left_y, bottom_left_x, bottom_left_y, 1, perspective_line); - api->line(api, which, canvas, last, bottom_left_x, bottom_left_y, bottom_right_x, bottom_right_y, 1, - perspective_line); - api->line(api, which, canvas, last, bottom_right_x, bottom_right_y, top_right_x, top_right_y, 1, - perspective_line); - - - - - api->playsound(perspective_snd_effect[which], (x * 255) / canvas->w, 255); + case BOTTOM_RIGHT: + { + bottom_right_x = x; + bottom_right_y = y; + } + break; } - break; - case TOOL_ZOOM: - case TOOL_RUSH: - case TOOL_TILEZOOM: + + SDL_BlitSurface(canvas_back, NULL, canvas, NULL); + + perspective_preview(api, which, canvas, last, x, y, update_rect, 2.0); + + /* Draw a square and the current shape relative to it as a visual reference */ + /* square */ + api->line(api, which, canvas, last, otop_left_x, otop_left_y, + otop_right_x, otop_right_y, 1, perspective_line); + api->line(api, which, canvas, last, otop_left_x, otop_left_y, + obottom_left_x, obottom_left_y, 1, perspective_line); + api->line(api, which, canvas, last, obottom_left_x, obottom_left_y, + obottom_right_x, obottom_right_y, 1, perspective_line); + api->line(api, which, canvas, last, obottom_right_x, obottom_right_y, + otop_right_x, otop_right_y, 1, perspective_line); + + /* shape */ + api->line(api, which, canvas, last, top_left_x, top_left_y, top_right_x, + top_right_y, 1, perspective_line); + api->line(api, which, canvas, last, top_left_x, top_left_y, + bottom_left_x, bottom_left_y, 1, perspective_line); + api->line(api, which, canvas, last, bottom_left_x, bottom_left_y, + bottom_right_x, bottom_right_y, 1, perspective_line); + api->line(api, which, canvas, last, bottom_right_x, bottom_right_y, + top_right_x, top_right_y, 1, perspective_line); + + + + + api->playsound(perspective_snd_effect[which], (x * 255) / canvas->w, + 255); + } + break; + case TOOL_ZOOM: + case TOOL_RUSH: + case TOOL_TILEZOOM: + { + int x_distance, y_distance; + + if (which == TOOL_ZOOM || which == TOOL_RUSH) { - int x_distance, y_distance; - - if (which == TOOL_ZOOM || which == TOOL_RUSH) - { - update_rect->x = update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; - - SDL_FillRect(canvas, update_rect, SDL_MapRGB(canvas->format, perspective_r, perspective_g, perspective_b)); - } - - new_h = max(1, old_h + click_y - y); - new_w = canvas->w * new_h / canvas->h; - if (new_h >= sound_h) - api->playsound(perspective_snd_effect[which], 127, 255); - else - api->playsound(perspective_snd_effect[which + 1], 127, 255); - sound_h = new_h; - - x_distance = (otop_right_x - otop_left_x) * new_w / canvas->w; - top_left_x = bottom_left_x = canvas->w / 2 - x_distance / 2; - top_right_x = bottom_right_x = canvas->w / 2 + x_distance / 2; - - y_distance = (obottom_left_y - otop_left_y) * new_w / canvas->w; - top_left_y = top_right_y = canvas->h / 2 - y_distance / 2; - bottom_left_y = bottom_right_y = canvas->h / 2 + y_distance / 2; - - perspective_preview(api, which, canvas, last, x, y, update_rect, 2.0); - update_rect->x = update_rect->y = 0; update_rect->w = canvas->w; update_rect->h = canvas->h; + + SDL_FillRect(canvas, update_rect, + SDL_MapRGB(canvas->format, perspective_r, perspective_g, + perspective_b)); } - break; + new_h = max(1, old_h + click_y - y); + new_w = canvas->w * new_h / canvas->h; + if (new_h >= sound_h) + api->playsound(perspective_snd_effect[which], 127, 255); + else + api->playsound(perspective_snd_effect[which + 1], 127, 255); + sound_h = new_h; + x_distance = (otop_right_x - otop_left_x) * new_w / canvas->w; + top_left_x = bottom_left_x = canvas->w / 2 - x_distance / 2; + top_right_x = bottom_right_x = canvas->w / 2 + x_distance / 2; + y_distance = (obottom_left_y - otop_left_y) * new_w / canvas->w; + top_left_y = top_right_y = canvas->h / 2 - y_distance / 2; + bottom_left_y = bottom_right_y = canvas->h / 2 + y_distance / 2; + + perspective_preview(api, which, canvas, last, x, y, update_rect, 2.0); + + update_rect->x = update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; } + break; + + + + } update_rect->x = 0; update_rect->y = 0; update_rect->w = canvas->w; @@ -348,104 +372,106 @@ void perspective_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void perspective_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) { switch (which) + { + case TOOL_PERSPECTIVE: { - case TOOL_PERSPECTIVE: - { - if (x < canvas->w / 2) - { - if (y < canvas->h / 2) - { - corner = TOP_LEFT; - } - else - { - corner = BOTTOM_LEFT; - } - } + if (x < canvas->w / 2) + { + if (y < canvas->h / 2) + { + corner = TOP_LEFT; + } else - { - if (y < canvas->h / 2) - { - corner = TOP_RIGHT; - } - else - { - corner = BOTTOM_RIGHT; - } - } - - + { + corner = BOTTOM_LEFT; + } } - break; - case TOOL_RUSH: - case TOOL_ZOOM: - case TOOL_TILEZOOM: + else { - click_x = x; - click_y = y; - old_h = new_h; + if (y < canvas->h / 2) + { + corner = TOP_RIGHT; + } + else + { + corner = BOTTOM_RIGHT; + } } - break; - case TOOL_PANELS: - { - SDL_Surface *scaled_surf; - scaled_surf = api->scale(canvas, canvas->w / 2, canvas->h / 2, 0); - /* Top left */ - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = scaled_surf->w; - update_rect->h = scaled_surf->h; - SDL_BlitSurface(scaled_surf, NULL, canvas, update_rect); - - /* Top right */ - update_rect->x = scaled_surf->w; - update_rect->y = 0; - update_rect->w = scaled_surf->w; - update_rect->h = scaled_surf->h; - SDL_BlitSurface(scaled_surf, NULL, canvas, update_rect); - - /* Bottom left */ - update_rect->x = 0; - update_rect->y = scaled_surf->h; - update_rect->w = scaled_surf->w; - update_rect->h = scaled_surf->h; - SDL_BlitSurface(scaled_surf, NULL, canvas, update_rect); - - /* Bottom right */ - update_rect->x = scaled_surf->w; - update_rect->y = scaled_surf->h; - update_rect->w = scaled_surf->w; - update_rect->h = scaled_surf->h; - SDL_BlitSurface(scaled_surf, NULL, canvas, update_rect); - - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; - - SDL_FreeSurface(scaled_surf); - - api->playsound(perspective_snd_effect[which], 127, 255); - } - break; } + break; + case TOOL_RUSH: + case TOOL_ZOOM: + case TOOL_TILEZOOM: + { + click_x = x; + click_y = y; + old_h = new_h; + } + break; + case TOOL_PANELS: + { + SDL_Surface *scaled_surf; + + scaled_surf = api->scale(canvas, canvas->w / 2, canvas->h / 2, 0); + + /* Top left */ + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = scaled_surf->w; + update_rect->h = scaled_surf->h; + SDL_BlitSurface(scaled_surf, NULL, canvas, update_rect); + + /* Top right */ + update_rect->x = scaled_surf->w; + update_rect->y = 0; + update_rect->w = scaled_surf->w; + update_rect->h = scaled_surf->h; + SDL_BlitSurface(scaled_surf, NULL, canvas, update_rect); + + /* Bottom left */ + update_rect->x = 0; + update_rect->y = scaled_surf->h; + update_rect->w = scaled_surf->w; + update_rect->h = scaled_surf->h; + SDL_BlitSurface(scaled_surf, NULL, canvas, update_rect); + + /* Bottom right */ + update_rect->x = scaled_surf->w; + update_rect->y = scaled_surf->h; + update_rect->w = scaled_surf->w; + update_rect->h = scaled_surf->h; + SDL_BlitSurface(scaled_surf, NULL, canvas, update_rect); + + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; + + SDL_FreeSurface(scaled_surf); + + api->playsound(perspective_snd_effect[which], 127, 255); + } + break; + } if (which != TOOL_PANELS) - { - perspective_drag(api, which, canvas, last, x, y, x, y, update_rect); - } + { + perspective_drag(api, which, canvas, last, x, y, x, y, update_rect); + } } // Affect the canvas on release: void perspective_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) { if (which == TOOL_PANELS) return; @@ -455,161 +481,178 @@ void perspective_release(magic_api * api, int which, update_rect->h = canvas->h; if (which == TOOL_ZOOM || which == TOOL_PERSPECTIVE) - SDL_FillRect(canvas, update_rect, SDL_MapRGB(canvas->format, perspective_r, perspective_g, perspective_b)); + SDL_FillRect(canvas, update_rect, + SDL_MapRGB(canvas->format, perspective_r, perspective_g, + perspective_b)); if (which == TOOL_PERSPECTIVE) - { - perspective_preview(api, which, canvas, last, x, y, update_rect, 0.5); - } + { + perspective_preview(api, which, canvas, last, x, y, update_rect, 0.5); + } else if (which == TOOL_RUSH) + { + int h1, h2, w, h, hh, x, y, dx1, dy1; + Uint8 r1, g1, b1, r2, g2, b2; + Uint32 r, g, b; + float pct; + SDL_Surface *scaled_surf; + + if (new_h == canvas->h || new_h == 0) { - int h1, h2, w, h, hh, x, y, dx1, dy1; - Uint8 r1, g1, b1, r2, g2, b2; - Uint32 r, g, b; - float pct; - SDL_Surface *scaled_surf; - - if (new_h == canvas->h || new_h == 0) - { - SDL_BlitSurface(last, NULL, canvas, NULL); - return; /* Do nothing */ - } - - if (new_h > canvas->h * 1.5) - new_h = canvas->h * 1.5; - - if (new_h < canvas->h) - { - h1 = new_h; - h2 = canvas->h; - } - else - { - h1 = canvas->h; - h2 = new_h; - } - SDL_BlitSurface(last, NULL, canvas, NULL); + return; /* Do nothing */ + } - for (h = 0; h < (h2 - h1); h++) - { - if ((h / 2) % 10 == 0) - api->update_progress_bar(); + if (new_h > canvas->h * 1.5) + new_h = canvas->h * 1.5; + + if (new_h < canvas->h) + { + h1 = new_h; + h2 = canvas->h; + } + else + { + h1 = canvas->h; + h2 = new_h; + } + + SDL_BlitSurface(last, NULL, canvas, NULL); + + for (h = 0; h < (h2 - h1); h++) + { + if ((h / 2) % 10 == 0) + api->update_progress_bar(); // if (new_h < canvas->h) - hh = h2 - h; + hh = h2 - h; // else // hh = new_h + h; - w = canvas->w * hh / canvas->h; - scaled_surf = api->scale(last, w, hh, 0); + w = canvas->w * hh / canvas->h; + scaled_surf = api->scale(last, w, hh, 0); - dx1 = (canvas->w - scaled_surf->w) / 2; - dy1 = (canvas->h - scaled_surf->h) / 2; + dx1 = (canvas->w - scaled_surf->w) / 2; + dy1 = (canvas->h - scaled_surf->h) / 2; - for (y = 0; y < scaled_surf->h; y++) + for (y = 0; y < scaled_surf->h; y++) + { + if (dy1 + y >= 0 && dy1 + y < canvas->h) + { + for (x = 0; x < scaled_surf->w; x++) + { + if (dx1 + x >= 0 && dx1 + x < canvas->w) { - if (dy1 + y >= 0 && dy1 + y < canvas->h) - { - for (x = 0; x < scaled_surf->w; x++) - { - if (dx1 + x >= 0 && dx1 + x < canvas->w) - { - SDL_GetRGB(api->getpixel(scaled_surf, x, y), scaled_surf->format, &r1, &g1, &b1); - SDL_GetRGB(api->getpixel(canvas, dx1 + x, dy1 + y), canvas->format, &r2, &g2, &b2); - pct = (float) ((float) h / ((float) h2 - (float) h1)); + SDL_GetRGB(api->getpixel(scaled_surf, x, y), + scaled_surf->format, &r1, &g1, &b1); + SDL_GetRGB(api->getpixel(canvas, dx1 + x, dy1 + y), + canvas->format, &r2, &g2, &b2); + pct = (float) ((float) h / ((float) h2 - (float) h1)); // if (new_h > canvas->h) // pct = 1.0 - pct; - r = api->linear_to_sRGB((api->sRGB_to_linear(r1) * (1.0 - pct)) + (api->sRGB_to_linear(r2) * (pct))); - g = api->linear_to_sRGB((api->sRGB_to_linear(g1) * (1.0 - pct)) + (api->sRGB_to_linear(g2) * (pct))); - b = api->linear_to_sRGB((api->sRGB_to_linear(b1) * (1.0 - pct)) + (api->sRGB_to_linear(b2) * (pct))); + r = + api->linear_to_sRGB((api->sRGB_to_linear(r1) * (1.0 - pct)) + + (api->sRGB_to_linear(r2) * (pct))); + g = + api->linear_to_sRGB((api->sRGB_to_linear(g1) * (1.0 - pct)) + + (api->sRGB_to_linear(g2) * (pct))); + b = + api->linear_to_sRGB((api->sRGB_to_linear(b1) * (1.0 - pct)) + + (api->sRGB_to_linear(b2) * (pct))); - api->putpixel(canvas, dx1 + x, dy1 + y, SDL_MapRGB(canvas->format, r, g, b)); - } - } - } + api->putpixel(canvas, dx1 + x, dy1 + y, + SDL_MapRGB(canvas->format, r, g, b)); } + } } - - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; + } } - else /* ZOOM & TILEZOOM */ + + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; + } + else /* ZOOM & TILEZOOM */ + { + SDL_Surface *aux_surf; + SDL_Surface *scaled_surf; + + update_rect->x = update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; + + if (which == TOOL_ZOOM) + SDL_FillRect(canvas, update_rect, + SDL_MapRGB(canvas->format, perspective_r, perspective_g, + perspective_b)); + + + if (new_h < canvas->h) { - SDL_Surface *aux_surf; - SDL_Surface *scaled_surf; + int xx, yy, x_span, y_span; - update_rect->x = update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; + scaled_surf = api->scale(canvas_back, new_w, new_h, 0); - if (which == TOOL_ZOOM) - SDL_FillRect(canvas, update_rect, SDL_MapRGB(canvas->format, perspective_r, perspective_g, perspective_b)); - - - if (new_h < canvas->h) - { - int xx, yy, x_span, y_span; - - scaled_surf = api->scale(canvas_back, new_w, new_h, 0); - - if (which == TOOL_TILEZOOM && (new_w < canvas->w || new_h < canvas->h)) - { - x_span = ceil(canvas->w / new_w); - y_span = ceil(canvas->h / new_h); - } - else - x_span = y_span = 0; - - for (yy = -y_span; yy <= y_span; yy++) - { - for (xx = -x_span; xx <= x_span; xx++) - { - update_rect->x = ((canvas->w - new_w) / 2) + (new_w * xx); - update_rect->y = ((canvas->h - new_h) / 2) + (new_h * yy); - update_rect->w = new_w; - update_rect->h = new_h; - SDL_BlitSurface(scaled_surf, NULL, canvas, update_rect); - } - } - } + if (which == TOOL_TILEZOOM && (new_w < canvas->w || new_h < canvas->h)) + { + x_span = ceil(canvas->w / new_w); + y_span = ceil(canvas->h / new_h); + } else + x_span = y_span = 0; + + for (yy = -y_span; yy <= y_span; yy++) + { + for (xx = -x_span; xx <= x_span; xx++) { - int aux_h, aux_w; - - aux_h = canvas->h * canvas->h / new_h; - aux_w = canvas->w * aux_h / canvas->h; - - update_rect->x = canvas->w / 2 - aux_w / 2; - update_rect->y = canvas->h / 2 - aux_h / 2; - update_rect->w = aux_w; - update_rect->h = aux_h; - - aux_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, - aux_w, - aux_h, - canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, 0); - - SDL_BlitSurface(canvas_back, update_rect, aux_surf, NULL); - scaled_surf = api->scale(aux_surf, canvas->w, canvas->h, 0); - SDL_BlitSurface(scaled_surf, NULL, canvas, NULL); - SDL_FreeSurface(aux_surf); + update_rect->x = ((canvas->w - new_w) / 2) + (new_w * xx); + update_rect->y = ((canvas->h - new_h) / 2) + (new_h * yy); + update_rect->w = new_w; + update_rect->h = new_h; + SDL_BlitSurface(scaled_surf, NULL, canvas, update_rect); } - SDL_FreeSurface(scaled_surf); - - update_rect->x = update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; - + } } + else + { + int aux_h, aux_w; + + aux_h = canvas->h * canvas->h / new_h; + aux_w = canvas->w * aux_h / canvas->h; + + update_rect->x = canvas->w / 2 - aux_w / 2; + update_rect->y = canvas->h / 2 - aux_h / 2; + update_rect->w = aux_w; + update_rect->h = aux_h; + + aux_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, + aux_w, + aux_h, + canvas->format->BitsPerPixel, + canvas->format->Rmask, + canvas->format->Gmask, + canvas->format->Bmask, 0); + + SDL_BlitSurface(canvas_back, update_rect, aux_surf, NULL); + scaled_surf = api->scale(aux_surf, canvas->w, canvas->h, 0); + SDL_BlitSurface(scaled_surf, NULL, canvas, NULL); + SDL_FreeSurface(aux_surf); + } + SDL_FreeSurface(scaled_surf); + + update_rect->x = update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; + + } } void perspective_preview(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect, float step) + SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, + SDL_Rect * update_rect, float step) { float i, j; float ax, ay, bx, by, dx, dy; @@ -622,73 +665,78 @@ void perspective_preview(magic_api * api, int which, update_rect->h = canvas->h; if (which == TOOL_ZOOM) - SDL_FillRect(canvas, update_rect, SDL_MapRGB(canvas->format, perspective_r, perspective_g, perspective_b)); + SDL_FillRect(canvas, update_rect, + SDL_MapRGB(canvas->format, perspective_r, perspective_g, + perspective_b)); else if (which == TOOL_TILEZOOM || which == TOOL_RUSH) - SDL_FillRect(canvas, update_rect, SDL_MapRGB(canvas->format, 128, 128, 128)); + SDL_FillRect(canvas, update_rect, + SDL_MapRGB(canvas->format, 128, 128, 128)); ox_distance = otop_right_x - otop_left_x; oy_distance = obottom_left_y - otop_left_y; - top_advc_x = (float)(top_right_x - top_left_x) / ox_distance; - top_advc_y = (float)(top_right_y - top_left_y) / ox_distance; + top_advc_x = (float) (top_right_x - top_left_x) / ox_distance; + top_advc_y = (float) (top_right_y - top_left_y) / ox_distance; - left_advc_x = (float)(bottom_left_x - top_left_x) / oy_distance; - left_advc_y = (float)(bottom_left_y - top_left_y) / oy_distance; + left_advc_x = (float) (bottom_left_x - top_left_x) / oy_distance; + left_advc_y = (float) (bottom_left_y - top_left_y) / oy_distance; - right_advc_x = (float)(bottom_right_x - top_right_x) / oy_distance; - right_advc_y = (float)(bottom_right_y - top_right_y) / oy_distance; + right_advc_x = (float) (bottom_right_x - top_right_x) / oy_distance; + right_advc_y = (float) (bottom_right_y - top_right_y) / oy_distance; - bottom_advc_x = (float)(bottom_right_x - bottom_left_x) / ox_distance; - bottom_advc_y = (float)(bottom_right_y - bottom_left_y) / ox_distance; + bottom_advc_x = (float) (bottom_right_x - bottom_left_x) / ox_distance; + bottom_advc_y = (float) (bottom_right_y - bottom_left_y) / ox_distance; center_ofset_x = (otop_left_x - top_left_x) * 2; center_ofset_y = (otop_left_y - top_left_y) * 2; for (i = 0; i < canvas->w; i += step) + { + ax = (float) top_advc_x *i; + ay = (float) top_advc_y *i; + bx = (float) bottom_advc_x *i + (bottom_left_x - top_left_x) * 2; + by = (float) bottom_advc_y *i + (bottom_left_y - top_left_y) * 2; + + for (j = 0; j < canvas->h; j += step) { - ax = (float)top_advc_x *i; - ay = (float)top_advc_y *i; - bx = (float)bottom_advc_x *i + (bottom_left_x - top_left_x) * 2; - by = (float)bottom_advc_y *i + (bottom_left_y - top_left_y) * 2; + dx = (float) (bx - ax) / canvas->h * j; + dy = (float) (by - ay) / canvas->h * j; - for (j = 0; j < canvas->h; j += step) - { - dx = (float)(bx - ax) / canvas->h * j; - dy = (float)(by - ay) / canvas->h * j; - - api->putpixel(canvas, ax + dx - center_ofset_x, ay + dy - center_ofset_y, api->getpixel(canvas_back, i, j)); - } + api->putpixel(canvas, ax + dx - center_ofset_x, + ay + dy - center_ofset_y, api->getpixel(canvas_back, i, + j)); } + } if (which == TOOL_TILEZOOM && new_w >= 2 && new_h >= 2) + { + int xx, yy, x_span, y_span; + SDL_Rect src_rect, dest_rect; + + x_span = ceil(canvas->w / new_w); + y_span = ceil(canvas->h / new_h); + + src_rect.x = -center_ofset_x; + src_rect.y = -center_ofset_y; + src_rect.w = new_w; + src_rect.h = new_h; + + for (yy = -y_span; yy <= y_span; yy++) { - int xx, yy, x_span, y_span; - SDL_Rect src_rect, dest_rect; - - x_span = ceil(canvas->w / new_w); - y_span = ceil(canvas->h / new_h); - - src_rect.x = -center_ofset_x; - src_rect.y = -center_ofset_y; - src_rect.w = new_w; - src_rect.h = new_h; - - for (yy = -y_span; yy <= y_span; yy++) + for (xx = -x_span; xx <= x_span; xx++) + { + if (xx != 0 || yy != 0) { - for (xx = -x_span; xx <= x_span; xx++) - { - if (xx != 0 || yy != 0) - { - dest_rect.x = ((canvas->w - new_w) / 2) + (new_w * xx); - dest_rect.y = ((canvas->h - new_h) / 2) + (new_h * yy); - dest_rect.w = new_w; - dest_rect.h = new_h; - SDL_BlitSurface(canvas, &src_rect, canvas, &dest_rect); - } - } + dest_rect.x = ((canvas->w - new_w) / 2) + (new_w * xx); + dest_rect.y = ((canvas->h - new_h) / 2) + (new_h * yy); + dest_rect.w = new_w; + dest_rect.h = new_h; + SDL_BlitSurface(canvas, &src_rect, canvas, &dest_rect); } + } } + } } void perspective_shutdown(magic_api * api ATTRIBUTE_UNUSED) @@ -697,16 +745,17 @@ void perspective_shutdown(magic_api * api ATTRIBUTE_UNUSED) int i; for (i = 0; i < perspective_NUM_TOOLS + 1; i++) + { + if (perspective_snd_effect[i] != NULL) { - if (perspective_snd_effect[i] != NULL) - { - Mix_FreeChunk(perspective_snd_effect[i]); - } + Mix_FreeChunk(perspective_snd_effect[i]); } + } } // Record the color from Tux Paint: -void perspective_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) +void perspective_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, + Uint8 b) { perspective_r = r; perspective_g = g; @@ -721,8 +770,9 @@ int perspective_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which) return 1; } -void perspective_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, - SDL_Surface * canvas) +void perspective_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas) { Uint32 amask; @@ -732,25 +782,32 @@ void perspective_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_ top_left_x = otop_left_x = bottom_left_x = obottom_left_x = canvas->w / 4; top_left_y = otop_left_y = top_right_y = otop_right_y = canvas->h / 4; - top_right_x = otop_right_x = bottom_right_x = obottom_right_x = canvas->w - otop_left_x; + top_right_x = otop_right_x = bottom_right_x = obottom_right_x = + canvas->w - otop_left_x; - bottom_left_y = obottom_left_y = bottom_right_y = obottom_right_y = canvas->h - otop_left_y; + bottom_left_y = obottom_left_y = bottom_right_y = obottom_right_y = + canvas->h - otop_left_y; black = SDL_MapRGBA(canvas->format, 0, 0, 0, 0); white = SDL_MapRGBA(canvas->format, 255, 255, 255, 0); - amask = ~(canvas->format->Rmask | canvas->format->Gmask | canvas->format->Bmask); + amask = + ~(canvas->format->Rmask | canvas->format->Gmask | canvas->format->Bmask); canvas_back = SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->w, canvas->h, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, amask); + canvas->format->Rmask, + canvas->format->Gmask, + canvas->format->Bmask, amask); SDL_BlitSurface(canvas, NULL, canvas_back, NULL); } -void perspective_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void perspective_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { SDL_FreeSurface(canvas_back); @@ -758,14 +815,18 @@ void perspective_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE int perspective_modes(magic_api * api ATTRIBUTE_UNUSED, int which) { - if (which == TOOL_PANELS) { + if (which == TOOL_PANELS) + { return (MODE_FULLSCREEN); - } else { + } + else + { return (MODE_PAINT_WITH_PREVIEW); } } -void perspective_line(void *ptr_to_api, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, +void perspective_line(void *ptr_to_api, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * snapshot ATTRIBUTE_UNUSED, int x, int y) { magic_api *api = (magic_api *) ptr_to_api; diff --git a/magic/src/pixels.c b/magic/src/pixels.c index ae0296bbc..2a4765c98 100644 --- a/magic/src/pixels.c +++ b/magic/src/pixels.c @@ -58,15 +58,19 @@ char *pixels_get_name(magic_api * api, int which); int pixels_get_group(magic_api * api, int which); char *pixels_get_description(magic_api * api, int which, int mode); void pixels_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void pixels_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void pixels_release(magic_api * api, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void pixels_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); +void pixels_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); void pixels_shutdown(magic_api * api); void pixels_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int pixels_requires_colors(magic_api * api, int which); -void pixels_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void pixels_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void pixels_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void pixels_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int pixels_modes(magic_api * api, int which); // No setup required: @@ -74,7 +78,8 @@ int pixels_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/pixels.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/pixels.ogg", + api->data_directory); pixel_snd = Mix_LoadWAV(fname); return (1); @@ -96,25 +101,30 @@ SDL_Surface *pixels_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/pixels.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/pixels.png", + api->data_directory); return (IMG_Load(fname)); } // Return our names, localized: -char *pixels_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *pixels_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (strdup(gettext_noop("Pixels"))); } // Return our group (both the same): -int pixels_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int pixels_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PAINTING; } // Return our descriptions, localized: -char *pixels_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *pixels_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED) { return (strdup(gettext_noop("Click and drag to draw large pixels."))); @@ -123,7 +133,9 @@ char *pixels_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBU // Do the effect: -static void do_pixels(void *ptr ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) +static void do_pixels(void *ptr ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { SDL_Rect dest; int pixel_size; @@ -135,29 +147,31 @@ static void do_pixels(void *ptr ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, SD dest.w = pixel_size; dest.h = pixel_size; - SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, pixels_r, pixels_g, pixels_b)); + SDL_FillRect(canvas, &dest, + SDL_MapRGB(canvas->format, pixels_r, pixels_g, pixels_b)); } // Affect the canvas on drag: void pixels_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) { - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_pixels); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, do_pixels); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = x - 64; update_rect->y = y - 64; @@ -169,14 +183,17 @@ void pixels_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void pixels_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) { pixels_drag(api, which, canvas, last, x, y, x, y, update_rect); } void pixels_release(magic_api * api, 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) { api->stopsound(); } @@ -189,7 +206,8 @@ void pixels_shutdown(magic_api * api ATTRIBUTE_UNUSED) } // Record the color from Tux Paint: -void pixels_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) +void pixels_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, + Uint8 b) { pixels_r = r; pixels_g = g; @@ -197,18 +215,21 @@ void pixels_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 } // Use colors: -int pixels_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int pixels_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 1; } -void pixels_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void pixels_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void pixels_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void pixels_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } diff --git a/magic/src/puzzle.c b/magic/src/puzzle.c index 029681711..c0cb9c048 100644 --- a/magic/src/puzzle.c +++ b/magic/src/puzzle.c @@ -57,19 +57,25 @@ char *puzzle_get_name(magic_api * api, int which); int puzzle_get_group(magic_api * api, int which); char *puzzle_get_description(magic_api * api, int which, int mode); void puzzle_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 puzzle_shutdown(magic_api * api); void puzzle_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int puzzle_requires_colors(magic_api * api, int which); -void puzzle_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void puzzle_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void puzzle_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void puzzle_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int puzzle_modes(magic_api * api, int which); -static void puzzle_draw(void *ptr, int which_tool, SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y); +static void puzzle_draw(void *ptr, int which_tool, SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y); void puzzle_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 puzzle_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); int gcd(int a, int b); Uint32 puzzle_api_version(void) @@ -81,7 +87,8 @@ int puzzle_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/puzzle.wav", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/puzzle.wav", + api->data_directory); puzzle_snd = Mix_LoadWAV(fname); return 1; @@ -96,32 +103,41 @@ SDL_Surface *puzzle_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/puzzle.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/puzzle.png", + api->data_directory); return (IMG_Load(fname)); } -char *puzzle_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *puzzle_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (strdup(gettext_noop("Puzzle"))); } -int puzzle_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int puzzle_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_DISTORTS; } -char *puzzle_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode) +char *puzzle_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode) { if (mode == MODE_PAINT) - return strdup(gettext_noop("Click the part of your picture where would you like a puzzle.")); + return + strdup(gettext_noop + ("Click the part of your picture where would you like a puzzle.")); return strdup(gettext_noop("Click to make a puzzle in fullscreen mode.")); } -void puzzle_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) +void puzzle_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) { } @@ -131,12 +147,14 @@ void puzzle_shutdown(magic_api * api ATTRIBUTE_UNUSED) Mix_FreeChunk(puzzle_snd); } -void puzzle_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, +void puzzle_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) { } -int puzzle_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int puzzle_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } @@ -148,18 +166,22 @@ int gcd(int a, int b) //greatest common divisor return gcd(b, a % b); } -void puzzle_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void puzzle_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas) { puzzle_gcd = RATIO * gcd(canvas->w, canvas->h); - rects_w = (unsigned int)canvas->w / puzzle_gcd; - rects_h = (unsigned int)canvas->h / puzzle_gcd; + rects_w = (unsigned int) canvas->w / puzzle_gcd; + rects_h = (unsigned int) canvas->h / puzzle_gcd; canvas_backup = - SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->w, canvas->h, canvas->format->BitsPerPixel, canvas->format->Rmask, - canvas->format->Gmask, canvas->format->Bmask, canvas->format->Amask); + SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->w, canvas->h, + canvas->format->BitsPerPixel, canvas->format->Rmask, + canvas->format->Gmask, canvas->format->Bmask, + canvas->format->Amask); } -void puzzle_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void puzzle_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { SDL_FreeSurface(canvas_backup); @@ -172,7 +194,8 @@ int puzzle_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) } static void puzzle_draw(void *ptr, int which_tool ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * snapshot ATTRIBUTE_UNUSED, int x, int y) + SDL_Surface * canvas, + SDL_Surface * snapshot ATTRIBUTE_UNUSED, int x, int y) { @@ -187,59 +210,65 @@ static void puzzle_draw(void *ptr, int which_tool ATTRIBUTE_UNUSED, y = (y / puzzle_gcd) * puzzle_gcd; if (!api->touched(x, y)) + { + srand(rand()); + + r = rand() % 4; + + rect_that.x = x; + rect_that.y = y; + + switch (r) { - srand(rand()); + case 0: //upper + if (y > puzzle_gcd) + rect_that.y = y - puzzle_gcd; - r = rand() % 4; + break; + case 1: //right + if (x < canvas->w - puzzle_gcd) + rect_that.x = x - puzzle_gcd; - rect_that.x = x; - rect_that.y = y; + break; + case 2: //lower + if (y < canvas->h - puzzle_gcd) + rect_that.y = y - puzzle_gcd; - switch (r) - { - case 0: //upper - if (y > puzzle_gcd) - rect_that.y = y - puzzle_gcd; - - break; - case 1: //right - if (x < canvas->w - puzzle_gcd) - rect_that.x = x - puzzle_gcd; - - break; - case 2: //lower - if (y < canvas->h - puzzle_gcd) - rect_that.y = y - puzzle_gcd; - - break; - case 3: //left - if (x > puzzle_gcd) - rect_that.x = x - puzzle_gcd; - break; - } - - rect_this.x = x; - rect_this.y = y; - rect_this.h = rect_this.w = puzzle_gcd; - rect_that.h = rect_that.w = puzzle_gcd; - - - SDL_BlitSurface(canvas, &rect_this, canvas, &rect_that); - SDL_BlitSurface(canvas_backup, &rect_that, canvas, &rect_this); - api->playsound(puzzle_snd, (x * 255) / canvas->w, 255); + break; + case 3: //left + if (x > puzzle_gcd) + rect_that.x = x - puzzle_gcd; + break; } + + rect_this.x = x; + rect_this.y = y; + rect_this.h = rect_this.w = puzzle_gcd; + rect_that.h = rect_that.w = puzzle_gcd; + + + SDL_BlitSurface(canvas, &rect_this, canvas, &rect_that); + SDL_BlitSurface(canvas_backup, &rect_that, canvas, &rect_this); + api->playsound(puzzle_snd, (x * 255) / canvas->w, 255); + } } void puzzle_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, int x, int y, + SDL_Surface * last, int ox ATTRIBUTE_UNUSED, + int oy ATTRIBUTE_UNUSED, int x, int y, SDL_Rect * update_rect) { - puzzle_draw(api, which, canvas, last, x - puzzle_gcd / 2, y - puzzle_gcd / 2); + puzzle_draw(api, which, canvas, last, x - puzzle_gcd / 2, + y - puzzle_gcd / 2); - puzzle_draw(api, which, canvas, last, x - 1.5 * puzzle_gcd / 2, y - puzzle_gcd / 2); - puzzle_draw(api, which, canvas, last, x + 0.5 * puzzle_gcd, y - puzzle_gcd / 2); - puzzle_draw(api, which, canvas, last, x - puzzle_gcd / 2, y - 1.5 * puzzle_gcd); - puzzle_draw(api, which, canvas, last, x - puzzle_gcd / 2, y + 0.5 * puzzle_gcd); + puzzle_draw(api, which, canvas, last, x - 1.5 * puzzle_gcd / 2, + y - puzzle_gcd / 2); + puzzle_draw(api, which, canvas, last, x + 0.5 * puzzle_gcd, + y - puzzle_gcd / 2); + puzzle_draw(api, which, canvas, last, x - puzzle_gcd / 2, + y - 1.5 * puzzle_gcd); + puzzle_draw(api, which, canvas, last, x - puzzle_gcd / 2, + y + 0.5 * puzzle_gcd); update_rect->x = 0; update_rect->y = 0; @@ -248,7 +277,8 @@ void puzzle_drag(magic_api * api, int which, SDL_Surface * canvas, } void puzzle_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) { puzzle_drag(api, which, canvas, last, x, y, x, y, update_rect); } diff --git a/magic/src/rails.c b/magic/src/rails.c index d9287aa68..a2d2b10f9 100644 --- a/magic/src/rails.c +++ b/magic/src/rails.c @@ -53,25 +53,34 @@ int rails_get_group(magic_api * api, int which); char *rails_get_description(magic_api * api, int which, int mode); int rails_requires_colors(magic_api * api, int which); void rails_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); + SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect); void rails_shutdown(magic_api * api); -void rails_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void rails_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void rails_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void rails_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); inline unsigned int rails_get_segment(int x, int y); #define POINT_TYPE typeof(((SDL_Rect *)NULL)->x) -inline void rails_extract_coords_from_segment(unsigned int segment, POINT_TYPE * x, POINT_TYPE * y); +inline void rails_extract_coords_from_segment(unsigned int segment, + POINT_TYPE * x, POINT_TYPE * y); static void rails_flip(void *ptr, SDL_Surface * dest, SDL_Surface * src); static void rails_flip_flop(void *ptr, SDL_Surface * dest, SDL_Surface * src); -static void rails_rotate(void *ptr, SDL_Surface * dest, SDL_Surface * src, unsigned int direction); -void rails_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); +static void rails_rotate(void *ptr, SDL_Surface * dest, SDL_Surface * src, + unsigned int direction); +void rails_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect); static Uint8 rails_select_image(Uint16 segment); -static void rails_draw(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, - int x, int y, unsigned int segment); +static void rails_draw(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, + unsigned int segment); -static void rails_draw_wrapper(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +static void rails_draw_wrapper(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); void rails_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect); + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect); Uint32 rails_api_version(void) { @@ -83,7 +92,8 @@ int rails_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) return (MODE_PAINT); } -void rails_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, +void rails_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) { } @@ -93,15 +103,19 @@ int rails_init(magic_api * api) char fname[1024]; Uint8 i; //is always < 3, so Uint8 seems to be a good idea - rails_images = (char **)malloc(sizeof(char *) * 4); + rails_images = (char **) malloc(sizeof(char *) * 4); for (i = 0; i < 4; i++) - rails_images[i] = (char *)malloc(sizeof(char) * 1024); + rails_images[i] = (char *) malloc(sizeof(char) * 1024); - snprintf(rails_images[0], 1024 * sizeof(char), "%simages/magic/rails_one.png", api->data_directory); - snprintf(rails_images[1], 1024 * sizeof(char), "%simages/magic/rails_three.png", api->data_directory); - snprintf(rails_images[2], 1024 * sizeof(char), "%simages/magic/rails_four.png", api->data_directory); - snprintf(rails_images[3], 1024 * sizeof(char), "%simages/magic/rails_corner.png", api->data_directory); + snprintf(rails_images[0], 1024 * sizeof(char), + "%simages/magic/rails_one.png", api->data_directory); + snprintf(rails_images[1], 1024 * sizeof(char), + "%simages/magic/rails_three.png", api->data_directory); + snprintf(rails_images[2], 1024 * sizeof(char), + "%simages/magic/rails_four.png", api->data_directory); + snprintf(rails_images[3], 1024 * sizeof(char), + "%simages/magic/rails_corner.png", api->data_directory); rails_one = IMG_Load(rails_images[0]); rails_three = IMG_Load(rails_images[1]); @@ -111,7 +125,8 @@ int rails_init(magic_api * api) img_w = rails_one->w; img_h = rails_one->h; - snprintf(fname, sizeof(fname), "%ssounds/magic/rails.wav", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/rails.wav", + api->data_directory); rails_snd = Mix_LoadWAV(fname); return (1); @@ -126,34 +141,45 @@ SDL_Surface *rails_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/rails.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/rails.png", + api->data_directory); return (IMG_Load(fname)); } -char *rails_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *rails_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return strdup(gettext_noop("Rails")); } -int rails_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int rails_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PAINTING; } -char *rails_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *rails_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED) { - return strdup(gettext_noop("Click and drag to draw train track rails on your picture.")); + return + strdup(gettext_noop + ("Click and drag to draw train track rails on your picture.")); } -int rails_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int rails_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void rails_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * snapshot ATTRIBUTE_UNUSED, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) +void rails_release(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED, + SDL_Surface * snapshot ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, + SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -176,30 +202,35 @@ void rails_shutdown(magic_api * api ATTRIBUTE_UNUSED) free(rails_status_of_segments); } -void rails_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void rails_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas) { //we've to compute the quantity of segments in each direction - canvas_backup = SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->w, canvas->h, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, - canvas->format->Amask); + canvas_backup = + SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->w, canvas->h, + canvas->format->BitsPerPixel, canvas->format->Rmask, + canvas->format->Gmask, canvas->format->Bmask, + canvas->format->Amask); SDL_BlitSurface(canvas, NULL, canvas_backup, NULL); rails_segments_x = rails_math_ceil(canvas->w, img_w); rails_segments_y = rails_math_ceil(canvas->h, img_h); //status_of_segments[0] will not be used, we write in rails_status_of_segments[1 to segments_x*segments_y] - rails_status_of_segments = (Uint8 *) calloc(rails_segments_x * rails_segments_y + 1, sizeof(Uint8)); + rails_status_of_segments = + (Uint8 *) calloc(rails_segments_x * rails_segments_y + 1, sizeof(Uint8)); } -void rails_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void rails_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { if (rails_status_of_segments != NULL) - { - free(rails_status_of_segments); - rails_status_of_segments = NULL; - } + { + free(rails_status_of_segments); + rails_status_of_segments = NULL; + } } // Interactivity functions @@ -208,7 +239,7 @@ inline int rails_math_ceil(int x, int y) { int temp; - temp = (int)x / y; + temp = (int) x / y; if (x % y) return temp + 1; @@ -229,10 +260,11 @@ inline unsigned int rails_get_segment(int x, int y) } -inline void rails_extract_coords_from_segment(unsigned int segment, POINT_TYPE * x, POINT_TYPE * y) +inline void rails_extract_coords_from_segment(unsigned int segment, + POINT_TYPE * x, POINT_TYPE * y) { //extracts the coords of the beginning and the segment *x = ((segment % rails_segments_x) - 1) * img_w; //useful to set update_rect as small as possible - *y = (int)(segment / rails_segments_x) * img_h; + *y = (int) (segment / rails_segments_x) * img_h; } static void rails_flip(void *ptr, SDL_Surface * dest, SDL_Surface * src) @@ -255,29 +287,31 @@ static void rails_flip_flop(void *ptr, SDL_Surface * dest, SDL_Surface * src) api->putpixel(dest, x, y, api->getpixel(src, y, x)); } -static void rails_rotate(void *ptr, SDL_Surface * dest, SDL_Surface * src, unsigned int direction) +static void rails_rotate(void *ptr, SDL_Surface * dest, SDL_Surface * src, + unsigned int direction) //src and dest must have same size { magic_api *api = (magic_api *) ptr; POINT_TYPE x, y; if (direction) //rotate -90 degs - { - for (x = 0; x < dest->w; x++) - for (y = 0; y < dest->h; y++) - api->putpixel(dest, x, y, api->getpixel(src, y, src->w - x - 1)); - } + { + for (x = 0; x < dest->w; x++) + for (y = 0; y < dest->h; y++) + api->putpixel(dest, x, y, api->getpixel(src, y, src->w - x - 1)); + } else //rotate +90 degs - { - for (x = 0; x < dest->w; x++) - for (y = 0; y < dest->h; y++) - api->putpixel(dest, x, y, api->getpixel(src, src->h - y - 1, x)); - } + { + for (x = 0; x < dest->w; x++) + for (y = 0; y < dest->h; y++) + api->putpixel(dest, x, y, api->getpixel(src, src->h - y - 1, x)); + } } void rails_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect) + SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect) { rails_segment_modified_last = 0; rails_drag(api, which, canvas, snapshot, x, y, x, y, update_rect); @@ -288,50 +322,51 @@ static Uint8 rails_select_image(Uint16 segment) int take_up, take_down; int val_up, val_down, val_left, val_right; int from_top = 0, from_bottom = 0, from_left = 0, from_right = 0; - int from_top_right = 0, from_top_left = 0, from_bottom_right = 0, from_bottom_left = 0; + int from_top_right = 0, from_top_left = 0, from_bottom_right = + 0, from_bottom_left = 0; int TOP = 0, BOTTOM = 0, LEFT = 0, RIGHT = 0; //Checking from were we come... if (rails_segment_modified_last > 0) + { + if (segment == rails_segment_modified_last + 1) + from_left = 1; + + if (segment == rails_segment_modified_last - 1) + from_right = 1; + + if (segment == rails_segment_modified_last - rails_segments_x) + from_bottom = 1; + + if (segment == rails_segment_modified_last + rails_segments_x) + from_top = 1; + + // Segments are joining by the corner + // We need to add a new segment to join by side, adding clockwise + if (segment == rails_segment_modified_last + rails_segments_x + 1) { - if (segment == rails_segment_modified_last + 1) - from_left = 1; - - if (segment == rails_segment_modified_last - 1) - from_right = 1; - - if (segment == rails_segment_modified_last - rails_segments_x) - from_bottom = 1; - - if (segment == rails_segment_modified_last + rails_segments_x) - from_top = 1; - - // Segments are joining by the corner - // We need to add a new segment to join by side, adding clockwise - if (segment == rails_segment_modified_last + rails_segments_x + 1) - { - from_top_left = 1; - rails_segment_to_add = segment - rails_segments_x; - } - - if (segment == rails_segment_modified_last + rails_segments_x - 1) - { - from_top_right = 1; - rails_segment_to_add = segment + 1; - } - - if (segment == rails_segment_modified_last - rails_segments_x - 1) - { - from_bottom_right = 1; - rails_segment_to_add = segment + rails_segments_x; - } - if (segment == rails_segment_modified_last - rails_segments_x + 1) - { - from_bottom_left = 1; - rails_segment_to_add = segment - 1; - } + from_top_left = 1; + rails_segment_to_add = segment - rails_segments_x; } + if (segment == rails_segment_modified_last + rails_segments_x - 1) + { + from_top_right = 1; + rails_segment_to_add = segment + 1; + } + + if (segment == rails_segment_modified_last - rails_segments_x - 1) + { + from_bottom_right = 1; + rails_segment_to_add = segment + rails_segments_x; + } + if (segment == rails_segment_modified_last - rails_segments_x + 1) + { + from_bottom_left = 1; + rails_segment_to_add = segment - 1; + } + } + @@ -343,7 +378,7 @@ static Uint8 rails_select_image(Uint16 segment) val_up = rails_status_of_segments[take_up]; take_down = segment + rails_segments_x; - if (take_down > (signed)(rails_segments_x * rails_segments_y)) + if (take_down > (signed) (rails_segments_x * rails_segments_y)) val_down = SEG_NONE; else val_down = rails_status_of_segments[take_down]; @@ -359,9 +394,9 @@ static Uint8 rails_select_image(Uint16 segment) val_right = rails_status_of_segments[segment + 1]; if (from_left || (val_left & SEG_RIGHT) || from_bottom_left) - { - LEFT = 1; - } + { + LEFT = 1; + } if (from_right || (val_right & SEG_LEFT) || from_top_right) RIGHT = 1; if (from_top || (val_up & SEG_BOTTOM) || from_top_left) @@ -400,8 +435,10 @@ static Uint8 rails_select_image(Uint16 segment) } -static void rails_draw(void *ptr, int which ATTRIBUTE_UNUSED, ATTRIBUTE_UNUSED SDL_Surface * canvas, - SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y ATTRIBUTE_UNUSED, unsigned int segment) +static void rails_draw(void *ptr, int which ATTRIBUTE_UNUSED, + ATTRIBUTE_UNUSED SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, int x, + int y ATTRIBUTE_UNUSED, unsigned int segment) { magic_api *api = (magic_api *) ptr; SDL_Surface *result, *temp; @@ -412,7 +449,8 @@ static void rails_draw(void *ptr, int which ATTRIBUTE_UNUSED, ATTRIBUTE_UNUSED S if (segment > rails_segments_x * rails_segments_y) return; //modification_rect.x and modification_rect.y are set by function - rails_extract_coords_from_segment(segment, &modification_rect.x, &modification_rect.y); + rails_extract_coords_from_segment(segment, &modification_rect.x, + &modification_rect.y); modification_rect.h = img_w; modification_rect.w = img_h; @@ -424,74 +462,78 @@ static void rails_draw(void *ptr, int which ATTRIBUTE_UNUSED, ATTRIBUTE_UNUSED S rails_status_of_segments[segment] = image; //and write it to global table - result = SDL_CreateRGBSurface(SDL_SWSURFACE, img_w, img_h, rails_one->format->BitsPerPixel, - rails_one->format->Rmask, rails_one->format->Gmask, rails_one->format->Bmask, - rails_one->format->Amask); + result = + SDL_CreateRGBSurface(SDL_SWSURFACE, img_w, img_h, + rails_one->format->BitsPerPixel, + rails_one->format->Rmask, rails_one->format->Gmask, + rails_one->format->Bmask, rails_one->format->Amask); - temp = SDL_CreateRGBSurface(SDL_SWSURFACE, img_w, img_h, rails_one->format->BitsPerPixel, - rails_one->format->Rmask, rails_one->format->Gmask, rails_one->format->Bmask, - rails_one->format->Amask); + temp = + SDL_CreateRGBSurface(SDL_SWSURFACE, img_w, img_h, + rails_one->format->BitsPerPixel, + rails_one->format->Rmask, rails_one->format->Gmask, + rails_one->format->Bmask, rails_one->format->Amask); SDL_BlitSurface(canvas_backup, &modification_rect, result, NULL); switch (image) - { - case 0: - case SEG_TOP_BOTTOM: - SDL_BlitSurface(canvas_backup, &modification_rect, result, NULL); - SDL_BlitSurface(rails_one, NULL, result, NULL); - break; + { + case 0: + case SEG_TOP_BOTTOM: + SDL_BlitSurface(canvas_backup, &modification_rect, result, NULL); + SDL_BlitSurface(rails_one, NULL, result, NULL); + break; - case SEG_LEFT_RIGHT: - SDL_BlitSurface(canvas_backup, &modification_rect, result, NULL); - rails_rotate(api, temp, rails_one, 1); - use_temp = 1; - break; + case SEG_LEFT_RIGHT: + SDL_BlitSurface(canvas_backup, &modification_rect, result, NULL); + rails_rotate(api, temp, rails_one, 1); + use_temp = 1; + break; - case SEG_LEFT_RIGHT_TOP_BOTTOM: - SDL_BlitSurface(canvas_backup, &modification_rect, result, NULL); - SDL_BlitSurface(rails_four, NULL, result, NULL); - break; + case SEG_LEFT_RIGHT_TOP_BOTTOM: + SDL_BlitSurface(canvas_backup, &modification_rect, result, NULL); + SDL_BlitSurface(rails_four, NULL, result, NULL); + break; - case SEG_LEFT_RIGHT_TOP: - SDL_BlitSurface(rails_three, NULL, result, NULL); - break; + case SEG_LEFT_RIGHT_TOP: + SDL_BlitSurface(rails_three, NULL, result, NULL); + break; - case SEG_LEFT_RIGHT_BOTTOM: - rails_flip(api, temp, rails_three); - use_temp = 1; - break; + case SEG_LEFT_RIGHT_BOTTOM: + rails_flip(api, temp, rails_three); + use_temp = 1; + break; - case SEG_LEFT_TOP_BOTTOM: - rails_rotate(api, temp, rails_three, 0); - use_temp = 1; - break; + case SEG_LEFT_TOP_BOTTOM: + rails_rotate(api, temp, rails_three, 0); + use_temp = 1; + break; - case SEG_RIGHT_TOP_BOTTOM: - rails_rotate(api, temp, rails_three, 1); - use_temp = 1; - break; + case SEG_RIGHT_TOP_BOTTOM: + rails_rotate(api, temp, rails_three, 1); + use_temp = 1; + break; - case SEG_RIGHT_TOP: - SDL_BlitSurface(rails_corner, NULL, result, NULL); - break; + case SEG_RIGHT_TOP: + SDL_BlitSurface(rails_corner, NULL, result, NULL); + break; - case SEG_RIGHT_BOTTOM: - rails_flip(api, temp, rails_corner); - use_temp = 1; - break; + case SEG_RIGHT_BOTTOM: + rails_flip(api, temp, rails_corner); + use_temp = 1; + break; - case SEG_LEFT_TOP: - rails_rotate(api, temp, rails_corner, 0); - use_temp = 1; - break; + case SEG_LEFT_TOP: + rails_rotate(api, temp, rails_corner, 0); + use_temp = 1; + break; - case SEG_LEFT_BOTTOM: - rails_flip_flop(api, temp, rails_corner); - use_temp = 1; - break; - } + case SEG_LEFT_BOTTOM: + rails_flip_flop(api, temp, rails_corner); + use_temp = 1; + break; + } if (use_temp) SDL_BlitSurface(temp, NULL, result, NULL); @@ -502,7 +544,8 @@ static void rails_draw(void *ptr, int which ATTRIBUTE_UNUSED, ATTRIBUTE_UNUSED S api->playsound(rails_snd, (x * 255) / canvas->w, 255); } -static void rails_draw_wrapper(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void rails_draw_wrapper(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y) { rails_segment_modified = rails_get_segment(x, y); @@ -510,50 +553,56 @@ static void rails_draw_wrapper(void *ptr, int which, SDL_Surface * canvas, SDL_S if ((rails_segment_modified == rails_segment_modified_last)) return; if (rails_segment_modified > 0) - { - rails_draw((void *)ptr, which, canvas, last, x, y, rails_segment_modified); - } + { + rails_draw((void *) ptr, which, canvas, last, x, y, + rails_segment_modified); + } if (rails_segment_modified_last > 0) - rails_draw((void *)ptr, which, canvas, last, x, y, rails_segment_modified_last); + rails_draw((void *) ptr, which, canvas, last, x, y, + rails_segment_modified_last); if (rails_segment_to_add > 0) - { - rails_draw((void *)ptr, which, canvas, last, x, y, rails_segment_to_add); - rails_draw((void *)ptr, which, canvas, last, x, y, rails_segment_modified_last); - rails_segment_to_add = 0; - } + { + rails_draw((void *) ptr, which, canvas, last, x, y, rails_segment_to_add); + rails_draw((void *) ptr, which, canvas, last, x, y, + rails_segment_modified_last); + rails_segment_to_add = 0; + } if (rails_segment_modified > 0) rails_segment_modified_last = rails_segment_modified; } void rails_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect) + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect) { int start_x, end_x, start_y, end_y, segment_start, segment_end, w, h; // avoiding to write out of the canvas - if ((x < canvas->w) && (y < canvas->h) && (ox < canvas->w) && (oy < canvas->h) && ((signed)x > 0) && ((signed)y > 0) - && ((signed)ox > 0) && ((signed)oy > 0)) - { - api->line((void *)api, which, canvas, snapshot, ox, oy, x, y, img_w / 2, rails_draw_wrapper); + if ((x < canvas->w) && (y < canvas->h) && (ox < canvas->w) + && (oy < canvas->h) && ((signed) x > 0) && ((signed) y > 0) + && ((signed) ox > 0) && ((signed) oy > 0)) + { + api->line((void *) api, which, canvas, snapshot, ox, oy, x, y, img_w / 2, + rails_draw_wrapper); - start_x = min(ox, x); - end_x = max(ox, x); - start_y = min(oy, y); - end_y = max(oy, y); + start_x = min(ox, x); + end_x = max(ox, x); + start_y = min(oy, y); + end_y = max(oy, y); - segment_start = rails_get_segment(start_x - img_w, start_y - img_h); - segment_end = rails_get_segment(end_x + img_w, end_y + img_h); + segment_start = rails_get_segment(start_x - img_w, start_y - img_h); + segment_end = rails_get_segment(end_x + img_w, end_y + img_h); - x = ((segment_start % rails_segments_x) - 1) * img_w; - y = (int)(segment_start / rails_segments_x) * img_h; - w = ((segment_end % rails_segments_x) - 1) * img_w - x + img_w; - h = (int)(segment_end / rails_segments_x) * img_h - y + img_h; + x = ((segment_start % rails_segments_x) - 1) * img_w; + y = (int) (segment_start / rails_segments_x) * img_h; + w = ((segment_end % rails_segments_x) - 1) * img_w - x + img_w; + h = (int) (segment_end / rails_segments_x) * img_h - y + img_h; - update_rect->x = x; - update_rect->y = y; - update_rect->w = w; - update_rect->h = h; - } + update_rect->x = x; + update_rect->y = y; + update_rect->w = w; + update_rect->h = h; + } } diff --git a/magic/src/rain.c b/magic/src/rain.c index d8c7ead19..bbfb495f0 100644 --- a/magic/src/rain.c +++ b/magic/src/rain.c @@ -43,7 +43,8 @@ #define gettext_noop(String) String #endif -void rain_click(magic_api *, int, int, SDL_Surface *, SDL_Surface *, int, int, SDL_Rect *); +void rain_click(magic_api *, int, int, SDL_Surface *, SDL_Surface *, int, int, + SDL_Rect *); static const int rain_SIZE = 30; static const int rain_AMOUNT = 200; @@ -84,19 +85,24 @@ SDL_Surface *rain_get_icon(magic_api * api, int which); char *rain_get_name(magic_api * api, int which); int rain_get_group(magic_api * api, int which); char *rain_get_description(magic_api * api, int which, int mode); -static void do_rain_drop(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); -static void rain_linecb(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +static void do_rain_drop(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); +static void rain_linecb(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); void rain_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void rain_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void rain_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void rain_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); +void rain_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); void rain_shutdown(magic_api * api); void rain_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int rain_requires_colors(magic_api * api, int which); -void rain_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void rain_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void rain_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void rain_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int rain_modes(magic_api * api, int which); Uint32 rain_api_version(void) @@ -108,9 +114,9 @@ Uint32 rain_api_version(void) static int rain_inRainShape(double x, double y, double r) { if (sqrt(x * x + y * y) < (r * pow(cos(atan2(x, y)), 10.0))) - { - return 1; - } + { + return 1; + } return 0; } @@ -122,10 +128,11 @@ int rain_init(magic_api * api) //Load sounds for (i = 0; i < rain_NUM_TOOLS; i++) - { - snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, rain_snd_filenames[i]); - rain_snd_effect[i] = Mix_LoadWAV(fname); - } + { + snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, + rain_snd_filenames[i]); + rain_snd_effect[i] = Mix_LoadWAV(fname); + } return (1); } @@ -140,7 +147,8 @@ SDL_Surface *rain_get_icon(magic_api * api, int which) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, rain_icon_filenames[which]); + snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, + rain_icon_filenames[which]); return (IMG_Load(fname)); } @@ -157,13 +165,15 @@ int rain_get_group(magic_api * api ATTRIBUTE_UNUSED, int which) } // Return our descriptions, localized: -char *rain_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode) +char *rain_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, + int mode) { return (strdup(gettext_noop(rain_descs[which][mode - 1]))); } // Do the effect: -static void do_rain_drop(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, +static void do_rain_drop(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { magic_api *api = (magic_api *) ptr; @@ -172,54 +182,59 @@ static void do_rain_drop(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * ca Uint8 r, g, b; for (yy = y - rain_SIZE / 2; yy < y + rain_SIZE / 2; yy++) + { + for (xx = x - rain_SIZE; xx < x + rain_SIZE; xx++) { - for (xx = x - rain_SIZE; xx < x + rain_SIZE; xx++) - { - if (rain_inRainShape(xx - x, yy - y + rain_SIZE / 2, rain_SIZE)) - { - //api->rgbtohsv(rain_r, rain_g, rain_b, &h, &s, &v); - //api->hsvtorgb(h, s, rain_weights[(yy-y)*((rain_SIZE*2) -1)+(xx-x)], &r, &g, &b); - SDL_GetRGB(api->getpixel(canvas, xx, yy), canvas->format, &r, &g, &b); - api->putpixel(canvas, xx, yy, SDL_MapRGB(canvas->format, clamp(0, r - 50, 255), - clamp(0, g - 50, 255), clamp(0, b + 200, 255))); - } - } + if (rain_inRainShape(xx - x, yy - y + rain_SIZE / 2, rain_SIZE)) + { + //api->rgbtohsv(rain_r, rain_g, rain_b, &h, &s, &v); + //api->hsvtorgb(h, s, rain_weights[(yy-y)*((rain_SIZE*2) -1)+(xx-x)], &r, &g, &b); + SDL_GetRGB(api->getpixel(canvas, xx, yy), canvas->format, &r, &g, &b); + api->putpixel(canvas, xx, yy, + SDL_MapRGB(canvas->format, clamp(0, r - 50, 255), + clamp(0, g - 50, 255), clamp(0, b + 200, + 255))); + } } + } } -static void rain_linecb(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void rain_linecb(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y) { magic_api *api = (magic_api *) ptr; SDL_Rect rect; if (rand() % 10 == 0) - { - rain_click(api, which, MODE_PAINT, canvas, last, - x + (rand() % rain_SIZE * 2) - rain_SIZE, y + (rand() % rain_SIZE * 2) - rain_SIZE, &rect); - } + { + rain_click(api, which, MODE_PAINT, canvas, last, + x + (rand() % rain_SIZE * 2) - rain_SIZE, + y + (rand() % rain_SIZE * 2) - rain_SIZE, &rect); + } } // Affect the canvas on drag: void rain_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) { - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, rain_linecb); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, rain_linecb); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = ox - rain_SIZE * 2; update_rect->y = oy - rain_SIZE * 2; @@ -229,43 +244,48 @@ void rain_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void rain_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) { if (mode == MODE_PAINT) - { - do_rain_drop(api, which, canvas, last, x, y); + { + do_rain_drop(api, which, canvas, last, x, y); - update_rect->x = x - rain_SIZE; - update_rect->y = y - rain_SIZE; - update_rect->w = rain_SIZE * 2; - update_rect->h = rain_SIZE * 2; + update_rect->x = x - rain_SIZE; + update_rect->y = y - rain_SIZE; + update_rect->w = rain_SIZE * 2; + update_rect->h = rain_SIZE * 2; - api->playsound(rain_snd_effect[which], (x * 255) / canvas->w, 255); - } + api->playsound(rain_snd_effect[which], (x * 255) / canvas->w, 255); + } else + { + + int i; + + for (i = 0; i < rain_AMOUNT; i++) { - - int i; - - for (i = 0; i < rain_AMOUNT; i++) - { - do_rain_drop(api, which, canvas, last, rand() % canvas->w, rand() % canvas->h); - } - - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; - - api->playsound(rain_snd_effect[which], 128, 255); + do_rain_drop(api, which, canvas, last, rand() % canvas->w, + rand() % canvas->h); } + + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; + + api->playsound(rain_snd_effect[which], 128, 255); + } } // Affect the canvas on release: -void rain_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) +void rain_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) { } @@ -276,33 +296,37 @@ void rain_shutdown(magic_api * api ATTRIBUTE_UNUSED) int i; for (i = 0; i < rain_NUM_TOOLS; i++) + { + if (rain_snd_effect[i] != NULL) { - if (rain_snd_effect[i] != NULL) - { - Mix_FreeChunk(rain_snd_effect[i]); - } + Mix_FreeChunk(rain_snd_effect[i]); } + } } // Record the color from Tux Paint: -void rain_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, +void rain_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) { } // Use colors: -int rain_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int rain_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void rain_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void rain_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void rain_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void rain_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } diff --git a/magic/src/rainbow.c b/magic/src/rainbow.c index 11f99426f..86ca1a594 100644 --- a/magic/src/rainbow.c +++ b/magic/src/rainbow.c @@ -76,23 +76,29 @@ SDL_Surface *rainbow_get_icon(magic_api * api, int which); char *rainbow_get_name(magic_api * api, int which); int rainbow_get_group(magic_api * api, int which); char *rainbow_get_description(magic_api * api, int which, int mode); -static void rainbow_linecb(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +static void rainbow_linecb(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); void rainbow_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 rainbow_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 rainbow_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 rainbow_shutdown(magic_api * api); void rainbow_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int rainbow_requires_colors(magic_api * api, int which); -void rainbow_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void rainbow_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void rainbow_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void rainbow_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int rainbow_modes(magic_api * api, int which); Uint32 rainbow_api_version(void) @@ -109,7 +115,8 @@ int rainbow_init(magic_api * api) rainbow_color = 0; rainbow_mix = 0; - snprintf(fname, sizeof(fname), "%ssounds/magic/rainbow.wav", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/rainbow.wav", + api->data_directory); rainbow_snd = Mix_LoadWAV(fname); return (1); @@ -126,29 +133,37 @@ SDL_Surface *rainbow_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/rainbow.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/rainbow.png", + api->data_directory); return (IMG_Load(fname)); } // Return our names, localized: -char *rainbow_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *rainbow_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { - if (which == 0) { + if (which == 0) + { return (strdup(gettext_noop("Rainbow"))); - } else { + } + else + { return (strdup(gettext_noop("Smooth Rainbow"))); } } // Return our group: -int rainbow_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int rainbow_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PAINTING; } // Return our descriptions, localized: -char *rainbow_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *rainbow_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED) { return (strdup(gettext_noop("You can draw in rainbow colors!"))); } @@ -156,37 +171,43 @@ char *rainbow_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIB // Do the effect: static void rainbow_linecb(void *ptr, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) + SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { magic_api *api = (magic_api *) ptr; int xx, yy; for (yy = y - 16; yy < y + 16; yy++) + { + for (xx = x - 16; xx < x + 16; xx++) { - for (xx = x - 16; xx < x + 16; xx++) - { - if (api->in_circle(xx - x, yy - y, 16)) - { - api->putpixel(canvas, xx, yy, rainbow_rgb); - } - } + if (api->in_circle(xx - x, yy - y, 16)) + { + api->putpixel(canvas, xx, yy, rainbow_rgb); + } } + } } // Affect the canvas on drag: void rainbow_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) { Uint8 r1, g1, b1, r2, g2, b2; int rc_tmp; - if (which == 1) { + if (which == 1) + { rainbow_mix += 1; - if (rainbow_mix > MIX_MAX) { + if (rainbow_mix > MIX_MAX) + { rainbow_mix = 0; rainbow_color = (rainbow_color + 1) % NUM_RAINBOW_COLORS; } - } else { + } + else + { rainbow_mix = 0; rainbow_color = (rainbow_color + 1) % NUM_RAINBOW_COLORS; } @@ -201,26 +222,30 @@ void rainbow_drag(magic_api * api, int which, SDL_Surface * canvas, b2 = rainbow_hexes[rc_tmp][2]; rainbow_rgb = SDL_MapRGB(canvas->format, - ((r1 * (MIX_MAX - rainbow_mix)) + (r2 * rainbow_mix)) / MIX_MAX, - ((g1 * (MIX_MAX - rainbow_mix)) + (g2 * rainbow_mix)) / MIX_MAX, - ((b1 * (MIX_MAX - rainbow_mix)) + (b2 * rainbow_mix)) / MIX_MAX); + ((r1 * (MIX_MAX - rainbow_mix)) + + (r2 * rainbow_mix)) / MIX_MAX, + ((g1 * (MIX_MAX - rainbow_mix)) + + (g2 * rainbow_mix)) / MIX_MAX, + ((b1 * (MIX_MAX - rainbow_mix)) + + (b2 * rainbow_mix)) / MIX_MAX); - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, rainbow_linecb); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, + rainbow_linecb); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = ox - 16; update_rect->y = oy - 16; @@ -232,14 +257,18 @@ void rainbow_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void rainbow_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) { rainbow_drag(api, which, canvas, last, x, y, x, y, update_rect); } -void rainbow_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) +void rainbow_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) { } @@ -251,28 +280,33 @@ void rainbow_shutdown(magic_api * api ATTRIBUTE_UNUSED) } // Record the color from Tux Paint: -void rainbow_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, +void rainbow_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) { } // Use colors: -int rainbow_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int rainbow_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void rainbow_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void rainbow_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void rainbow_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void rainbow_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -int rainbow_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int rainbow_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_PAINT); } diff --git a/magic/src/realrainbow.c b/magic/src/realrainbow.c index 0943f59cc..f3675ece6 100644 --- a/magic/src/realrainbow.c +++ b/magic/src/realrainbow.c @@ -24,12 +24,15 @@ Mix_Chunk *realrainbow_snd; int realrainbow_x1, realrainbow_y1, realrainbow_x2, realrainbow_y2; SDL_Rect realrainbow_rect; SDL_Surface *realrainbow_colors[2]; -Uint8 realrainbow_blendr, realrainbow_blendg, realrainbow_blendb, realrainbow_blenda; +Uint8 realrainbow_blendr, realrainbow_blendg, realrainbow_blendb, + realrainbow_blenda; -void realrainbow_arc(magic_api * api, int which, SDL_Surface * canvas, SDL_Surface * last, - int x1, int y1, int x2, int y2, int fulldraw, SDL_Rect * update_rect); -static void realrainbow_linecb(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +void realrainbow_arc(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x1, int y1, int x2, int y2, + int fulldraw, SDL_Rect * update_rect); +static void realrainbow_linecb(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); Uint32 realrainbow_api_version(void); int realrainbow_init(magic_api * api); int realrainbow_get_tool_count(magic_api * api); @@ -42,13 +45,18 @@ int realrainbow_requires_colors(magic_api * api, int which); void realrainbow_shutdown(magic_api * api); void realrainbow_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); void realrainbow_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void realrainbow_drag(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void realrainbow_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void realrainbow_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void realrainbow_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); + SDL_Surface * canvas, SDL_Surface * last, int x, int y, + SDL_Rect * update_rect); +void realrainbow_drag(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void realrainbow_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, + SDL_Rect * update_rect); +void realrainbow_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void realrainbow_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); Uint32 realrainbow_api_version(void) @@ -60,17 +68,21 @@ int realrainbow_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/realrainbow-colors.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/realrainbow-colors.png", + api->data_directory); realrainbow_colors[0] = IMG_Load(fname); if (realrainbow_colors[0] == NULL) return (0); - snprintf(fname, sizeof(fname), "%simages/magic/realrainbow-roygbiv-colors.png", api->data_directory); + snprintf(fname, sizeof(fname), + "%simages/magic/realrainbow-roygbiv-colors.png", + api->data_directory); realrainbow_colors[1] = IMG_Load(fname); if (realrainbow_colors[1] == NULL) return (0); - snprintf(fname, sizeof(fname), "%ssounds/magic/realrainbow.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/realrainbow.ogg", + api->data_directory); realrainbow_snd = Mix_LoadWAV(fname); return (1); @@ -86,9 +98,11 @@ SDL_Surface *realrainbow_get_icon(magic_api * api, int which) char fname[1024]; if (which == 0) - snprintf(fname, sizeof(fname), "%simages/magic/realrainbow.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/realrainbow.png", + api->data_directory); else - snprintf(fname, sizeof(fname), "%simages/magic/realrainbow-roygbiv.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/realrainbow-roygbiv.png", + api->data_directory); return (IMG_Load(fname)); } @@ -101,12 +115,14 @@ char *realrainbow_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) return (strdup(gettext_noop("ROYGBIV Rainbow"))); } -int realrainbow_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int realrainbow_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_ARTISTIC; } -char *realrainbow_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, +char *realrainbow_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) { return (strdup @@ -114,12 +130,14 @@ char *realrainbow_get_description(magic_api * api ATTRIBUTE_UNUSED, int which AT ("Click where you want your rainbow to start, drag to where you want it to end, and then let go to draw a rainbow."))); } -int realrainbow_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int realrainbow_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_PAINT_WITH_PREVIEW); } -int realrainbow_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int realrainbow_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (0); } @@ -134,14 +152,17 @@ void realrainbow_shutdown(magic_api * api ATTRIBUTE_UNUSED) Mix_FreeChunk(realrainbow_snd); } -void realrainbow_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, +void realrainbow_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) { } -void realrainbow_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) +void realrainbow_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) { realrainbow_x1 = x; realrainbow_y1 = y; @@ -154,7 +175,8 @@ void realrainbow_click(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNU void realrainbow_drag(magic_api * api, int which, SDL_Surface * canvas, SDL_Surface * last, - int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, int x, int y, SDL_Rect * update_rect) + int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, int x, + int y, SDL_Rect * update_rect) { int rx1, ry1, rx2, ry2; SDL_Rect rect; @@ -164,8 +186,8 @@ void realrainbow_drag(magic_api * api, int which, SDL_BlitSurface(last, &realrainbow_rect, canvas, &realrainbow_rect); - realrainbow_arc(api, which, canvas, last, realrainbow_x1, realrainbow_y1, realrainbow_x2, realrainbow_y2, 0, - update_rect); + realrainbow_arc(api, which, canvas, last, realrainbow_x1, realrainbow_y1, + realrainbow_x2, realrainbow_y2, 0, update_rect); memcpy(&rect, &realrainbow_rect, sizeof(SDL_Rect)); memcpy(&realrainbow_rect, update_rect, sizeof(SDL_Rect)); @@ -191,7 +213,8 @@ void realrainbow_drag(magic_api * api, int which, } void realrainbow_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) { int rx1, ry1, rx2, ry2; SDL_Rect rect; @@ -201,8 +224,8 @@ void realrainbow_release(magic_api * api, int which, SDL_BlitSurface(last, &realrainbow_rect, canvas, &realrainbow_rect); - realrainbow_arc(api, which, canvas, last, realrainbow_x1, realrainbow_y1, realrainbow_x2, realrainbow_y2, 1, - update_rect); + realrainbow_arc(api, which, canvas, last, realrainbow_x1, realrainbow_y1, + realrainbow_x2, realrainbow_y2, 1, update_rect); memcpy(&rect, &realrainbow_rect, sizeof(SDL_Rect)); memcpy(&realrainbow_rect, update_rect, sizeof(SDL_Rect)); @@ -229,19 +252,24 @@ void realrainbow_release(magic_api * api, int which, api->playsound(realrainbow_snd, 128, 255); } -void realrainbow_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void realrainbow_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void realrainbow_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void realrainbow_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void realrainbow_arc(magic_api * api, int which, SDL_Surface * canvas, SDL_Surface * last, int x1, int y1, int x2, - int y2, int fulldraw, SDL_Rect * update_rect) +void realrainbow_arc(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x1, int y1, int x2, int y2, + int fulldraw, SDL_Rect * update_rect) { int lowx, lowy, hix, hiy, xm, ym, xc, yc, r, a1, atan2_a, atan2_b; int a, oa, ox, oy, nx, ny, step, thick, rr, done; @@ -249,71 +277,71 @@ void realrainbow_arc(magic_api * api, int which, SDL_Surface * canvas, SDL_Surfa int colorindex; if (abs(x2 - x1) < 50) - { - if (x2 > x1) - x2 = x1 + 50; - else - x2 = x1 - 50; - } + { + if (x2 > x1) + x2 = x1 + 50; + else + x2 = x1 - 50; + } if (y1 == y2) - { - xc = x1 + (x2 - x1) / 2; - yc = y1; - r = abs(xc - x1); + { + xc = x1 + (x2 - x1) / 2; + yc = y1; + r = abs(xc - x1); - a1 = 0; - theta = -180; - } + a1 = 0; + theta = -180; + } else + { + if (y1 > y2) { - if (y1 > y2) - { - lowx = x1; - lowy = y1; - hix = x2; - hiy = y2; - } - else - { - lowx = x2; - lowy = y2; - hix = x1; - hiy = y1; - } - - xm = (lowx + hix) / 2; - ym = (lowy + hiy) / 2; - - if (hix == lowx) - return; - - slope = (float)(hiy - lowy) / (float)(hix - lowx); - - yc = lowy; - xc = slope * (ym - yc) + xm; - - r = abs(xc - lowx); - atan2_b = hix - xc; - atan2_a = hiy - yc; - theta = atan2(atan2_a, atan2_b) * (180.0 / M_PI); - - if (slope > 0) - a1 = 0; - else - a1 = -180; + lowx = x1; + lowy = y1; + hix = x2; + hiy = y2; } + else + { + lowx = x2; + lowy = y2; + hix = x1; + hiy = y1; + } + + xm = (lowx + hix) / 2; + ym = (lowy + hiy) / 2; + + if (hix == lowx) + return; + + slope = (float) (hiy - lowy) / (float) (hix - lowx); + + yc = lowy; + xc = slope * (ym - yc) + xm; + + r = abs(xc - lowx); + atan2_b = hix - xc; + atan2_a = hiy - yc; + theta = atan2(atan2_a, atan2_b) * (180.0 / M_PI); + + if (slope > 0) + a1 = 0; + else + a1 = -180; + } if (fulldraw) - { - step = 1; - /* thick = (r / 5); */ - } + { + step = 1; + /* thick = (r / 5); */ + } else - { - step = 30; - /* thick = 1; */ - } + { + step = 30; + /* thick = 1; */ + } thick = (r / 5); if (theta < a1) @@ -323,37 +351,40 @@ void realrainbow_arc(magic_api * api, int which, SDL_Surface * canvas, SDL_Surfa oa = a1; for (a = (a1 + step); done < 2; a = a + step) + { + for (rr = r - (thick / 2); rr <= r + (thick / 2); rr++) { - for (rr = r - (thick / 2); rr <= r + (thick / 2); rr++) - { - ox = (rr * cos(oa * M_PI / 180.0)) + xc; - oy = (rr * sin(oa * M_PI / 180.0)) + yc; + ox = (rr * cos(oa * M_PI / 180.0)) + xc; + oy = (rr * sin(oa * M_PI / 180.0)) + yc; - nx = (rr * cos(a * M_PI / 180.0)) + xc; - ny = (rr * sin(a * M_PI / 180.0)) + yc; + nx = (rr * cos(a * M_PI / 180.0)) + xc; + ny = (rr * sin(a * M_PI / 180.0)) + yc; - colorindex = - realrainbow_colors[which]->h - 1 - (((rr - r + (thick / 2)) * realrainbow_colors[which]->h) / thick); + colorindex = + realrainbow_colors[which]->h - 1 - + (((rr - r + (thick / 2)) * realrainbow_colors[which]->h) / thick); - SDL_GetRGBA(api->getpixel(realrainbow_colors[which], 0, colorindex), - realrainbow_colors[which]->format, &realrainbow_blendr, &realrainbow_blendg, &realrainbow_blendb, - &realrainbow_blenda); + SDL_GetRGBA(api->getpixel(realrainbow_colors[which], 0, colorindex), + realrainbow_colors[which]->format, &realrainbow_blendr, + &realrainbow_blendg, &realrainbow_blendb, + &realrainbow_blenda); - if (!fulldraw) - realrainbow_blenda = 255; + if (!fulldraw) + realrainbow_blenda = 255; - api->line((void *)api, 0, canvas, last, ox, oy, nx, ny, 1, realrainbow_linecb); - } - - oa = a; - - if ((step > 0 && a + step > theta) || (step < 0 && a + step < theta)) - { - done++; - a = theta - step; - } + api->line((void *) api, 0, canvas, last, ox, oy, nx, ny, 1, + realrainbow_linecb); } + oa = a; + + if ((step > 0 && a + step > theta) || (step < 0 && a + step < theta)) + { + done++; + a = theta - step; + } + } + update_rect->y = yc - r - thick - 2; update_rect->h = r + thick * 2 + 4; update_rect->x = xc - r - thick; @@ -361,7 +392,8 @@ void realrainbow_arc(magic_api * api, int which, SDL_Surface * canvas, SDL_Surfa } static void realrainbow_linecb(void *ptr, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * last, int x, int y) + SDL_Surface * canvas, SDL_Surface * last, + int x, int y) { magic_api *api = (magic_api *) ptr; Uint8 origr, origg, origb; @@ -369,9 +401,15 @@ static void realrainbow_linecb(void *ptr, int which ATTRIBUTE_UNUSED, SDL_GetRGB(api->getpixel(last, x, y), last->format, &origr, &origg, &origb); - newr = ((realrainbow_blendr * realrainbow_blenda) / 255) + ((origr * (255 - realrainbow_blenda)) / 255); - newg = ((realrainbow_blendg * realrainbow_blenda) / 255) + ((origg * (255 - realrainbow_blenda)) / 255); - newb = ((realrainbow_blendb * realrainbow_blenda) / 255) + ((origb * (255 - realrainbow_blenda)) / 255); + newr = + ((realrainbow_blendr * realrainbow_blenda) / 255) + + ((origr * (255 - realrainbow_blenda)) / 255); + newg = + ((realrainbow_blendg * realrainbow_blenda) / 255) + + ((origg * (255 - realrainbow_blenda)) / 255); + newb = + ((realrainbow_blendb * realrainbow_blenda) / 255) + + ((origb * (255 - realrainbow_blenda)) / 255); api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, newr, newg, newb)); } diff --git a/magic/src/reflection.c b/magic/src/reflection.c index 4b6ac6cff..50113797c 100644 --- a/magic/src/reflection.c +++ b/magic/src/reflection.c @@ -60,18 +60,23 @@ char *reflection_get_name(magic_api * api, int which); int reflection_get_group(magic_api * api, int which); char *reflection_get_description(magic_api * api, int which, int mode); void reflection_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void do_reflection(magic_api * api, SDL_Surface * canvas, - SDL_Surface * last, int x, int y, SDL_Rect * update_rect, int show_origin); + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void do_reflection(magic_api * api, SDL_Surface * canvas, SDL_Surface * last, + int x, int y, SDL_Rect * update_rect, int show_origin); void reflection_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void reflection_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 reflection_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, + SDL_Rect * update_rect); void reflection_shutdown(magic_api * api); void reflection_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int reflection_requires_colors(magic_api * api, int which); -void reflection_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void reflection_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void reflection_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void reflection_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int reflection_modes(magic_api * api, int which); @@ -80,7 +85,8 @@ int reflection_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/reflection.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/reflection.ogg", + api->data_directory); reflection_snd = Mix_LoadWAV(fname); return (1); @@ -105,30 +111,38 @@ SDL_Surface *reflection_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) return (IMG_Load(fname)); } -char *reflection_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *reflection_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (strdup(gettext_noop("Reflection"))); } -int reflection_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int reflection_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PICTURE_WARPS; } -char *reflection_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *reflection_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 add a reflection to your picture."))); + return (strdup + (gettext_noop + ("Click and drag the mouse around to add a reflection to your picture."))); } -void reflection_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, - SDL_Surface * last, int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, int x, int y, - SDL_Rect * update_rect) +void reflection_drag(magic_api * api, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * last, + int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, int x, + int y, SDL_Rect * update_rect) { do_reflection(api, canvas, last, x, y, update_rect, 1); } void do_reflection(magic_api * api, SDL_Surface * canvas, - SDL_Surface * last, int x, int y, SDL_Rect * update_rect, int show_origin) + SDL_Surface * last, int x, int y, SDL_Rect * update_rect, + int show_origin) { float scale; int xx, yy; @@ -149,29 +163,29 @@ void do_reflection(magic_api * api, SDL_Surface * canvas, /* Determine what direction to go */ if (abs(x - reflection_x1) < 32) - { - /* +/-32 pixels of wiggle room before we switch from vertical to horizontal */ - if (y > reflection_y1) - reflection_side = REFLECTION_SIDE_BOTTOM; - else - reflection_side = REFLECTION_SIDE_TOP; - } + { + /* +/-32 pixels of wiggle room before we switch from vertical to horizontal */ + if (y > reflection_y1) + reflection_side = REFLECTION_SIDE_BOTTOM; + else + reflection_side = REFLECTION_SIDE_TOP; + } else - { - if (x < reflection_x1) - reflection_side = REFLECTION_SIDE_LEFT; - else - reflection_side = REFLECTION_SIDE_RIGHT; - } + { + if (x < reflection_x1) + reflection_side = REFLECTION_SIDE_LEFT; + else + reflection_side = REFLECTION_SIDE_RIGHT; + } /* If we've changed direction, reset the canvas back to the snapshot before proceeding */ if (reflection_side != reflection_side_old) - { - SDL_BlitSurface(last, NULL, canvas, NULL); - reflection_side_old = reflection_side; - update_all = 1; - } + { + SDL_BlitSurface(last, NULL, canvas, NULL); + reflection_side_old = reflection_side; + update_all = 1; + } /* Note: This isn't very good, and I basically @@ -179,173 +193,179 @@ void do_reflection(magic_api * api, SDL_Surface * canvas, well enough. There's a ton of room for improvement! -bjk 2021.11.05 */ if (reflection_side == REFLECTION_SIDE_BOTTOM) + { + /* Starting from `reflection_y1` and moving down, + we'll copy from `reflection_y1` and moving up */ + + scale = (float) reflection_y1 / (float) y; + + for (yy = reflection_y1; yy < canvas->h; yy++) { - /* Starting from `reflection_y1` and moving down, - we'll copy from `reflection_y1` and moving up */ + dest.x = 0; + dest.y = yy; + dest.w = canvas->w; + dest.h = 1; - scale = (float) reflection_y1 / (float) y; + src.x = 0; + src.y = (reflection_y1 * scale) + ((y - yy) * scale); + src.w = canvas->w; + src.h = 1; - for (yy = reflection_y1; yy < canvas->h; yy++) - { - dest.x = 0; - dest.y = yy; - dest.w = canvas->w; - dest.h = 1; + if (src.y < 0) + { + src.y = yy; + } - src.x = 0; - src.y = (reflection_y1 * scale) + ((y - yy) * scale); - src.w = canvas->w; - src.h = 1; - - if (src.y < 0) - { - src.y = yy; - } - - SDL_BlitSurface(last, &src, canvas, &dest); - } - - update_rect->x = 0; - update_rect->y = reflection_y1; - update_rect->w = canvas->w; - update_rect->h = canvas->h - reflection_y1 + 1; + SDL_BlitSurface(last, &src, canvas, &dest); } + + update_rect->x = 0; + update_rect->y = reflection_y1; + update_rect->w = canvas->w; + update_rect->h = canvas->h - reflection_y1 + 1; + } else if (reflection_side == REFLECTION_SIDE_TOP) + { + /* Starting from `reflection_y1` and moving up, + we'll copy from `reflection_y1` and moving down */ + + scale = ((float) reflection_y1 / (float) y); + + for (yy = reflection_y1; yy >= 0; yy--) { - /* Starting from `reflection_y1` and moving up, - we'll copy from `reflection_y1` and moving down */ + dest.x = 0; + dest.y = yy; + dest.w = canvas->w; + dest.h = 1; - scale = ((float) reflection_y1 / (float) y); + src.x = 0; + src.y = (reflection_y1 / scale) + (y * scale) - (yy / scale); + src.w = canvas->w; + src.h = 1; - for (yy = reflection_y1; yy >= 0; yy--) - { - dest.x = 0; - dest.y = yy; - dest.w = canvas->w; - dest.h = 1; + if (src.y >= canvas->h) + { + src.y = yy; + } - src.x = 0; - src.y = (reflection_y1 / scale) + (y * scale) - (yy / scale); - src.w = canvas->w; - src.h = 1; - - if (src.y >= canvas->h) - { - src.y = yy; - } - - SDL_BlitSurface(last, &src, canvas, &dest); - } - - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = reflection_y1; + SDL_BlitSurface(last, &src, canvas, &dest); } + + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = reflection_y1; + } else if (reflection_side == REFLECTION_SIDE_RIGHT) + { + /* Starting from `reflection_x1` and moving right, + we'll copy from `reflection_x1` and moving left */ + + scale = (float) reflection_x1 / (float) x; + + for (xx = reflection_x1; xx < canvas->w; xx++) { - /* Starting from `reflection_x1` and moving right, - we'll copy from `reflection_x1` and moving left */ + dest.x = xx; + dest.y = 0; + dest.w = 1; + dest.h = canvas->h; - scale = (float) reflection_x1 / (float) x; + src.x = (reflection_x1 * scale) + ((x - xx) * scale); + src.y = 0; + src.w = 1; + src.h = canvas->h; - for (xx = reflection_x1; xx < canvas->w; xx++) - { - dest.x = xx; - dest.y = 0; - dest.w = 1; - dest.h = canvas->h; + if (src.x < 0) + { + src.x = xx; + } - src.x = (reflection_x1 * scale) + ((x - xx) * scale); - src.y = 0; - src.w = 1; - src.h = canvas->h; - - if (src.x < 0) - { - src.x = xx; - } - - SDL_BlitSurface(last, &src, canvas, &dest); - } - - update_rect->x = reflection_x1; - update_rect->y = 0; - update_rect->w = canvas->w - reflection_x1 + 1; - update_rect->h = canvas->h; + SDL_BlitSurface(last, &src, canvas, &dest); } + + update_rect->x = reflection_x1; + update_rect->y = 0; + update_rect->w = canvas->w - reflection_x1 + 1; + update_rect->h = canvas->h; + } else if (reflection_side == REFLECTION_SIDE_LEFT) + { + /* Starting from `reflection_x1` and left up, + we'll copy from `reflection_x1` and right down */ + + scale = (float) reflection_x1 / (float) x; + + for (xx = reflection_x1; xx >= 0; xx--) { - /* Starting from `reflection_x1` and left up, - we'll copy from `reflection_x1` and right down */ + dest.x = xx; + dest.y = 0; + dest.w = 1; + dest.h = canvas->h; - scale = (float) reflection_x1 / (float) x; + src.x = (reflection_x1 / scale) + (x * scale) - (xx / scale); + src.y = 0; + src.w = 1; + src.h = canvas->h; - for (xx = reflection_x1; xx >= 0; xx--) - { - dest.x = xx; - dest.y = 0; - dest.w = 1; - dest.h = canvas->h; + if (src.x >= canvas->w) + { + src.x = xx; + } - src.x = (reflection_x1 / scale) + (x * scale) - (xx / scale); - src.y = 0; - src.w = 1; - src.h = canvas->h; - - if (src.x >= canvas->w) - { - src.x = xx; - } - - SDL_BlitSurface(last, &src, canvas, &dest); - } - - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = reflection_x1; - update_rect->h = canvas->h; + SDL_BlitSurface(last, &src, canvas, &dest); } + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = reflection_x1; + update_rect->h = canvas->h; + } + /* TODO: Support reflecting at arbitrary angles! (a la linear gradient fill tool) */ if (update_all) - { - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; - } + { + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; + } else + { + for (yy = reflection_y1 - REFLECTION_XOR_SIZE; + yy < reflection_y1 + REFLECTION_XOR_SIZE; yy++) { - for (yy = reflection_y1 - REFLECTION_XOR_SIZE; yy < reflection_y1 + REFLECTION_XOR_SIZE; yy++) - { - if (show_origin) - api->xorpixel(canvas, reflection_x1, yy); - else - api->putpixel(canvas, reflection_x1, yy, api->getpixel(last, reflection_x1, yy)); - } - - for (xx = reflection_x1 - REFLECTION_XOR_SIZE; xx < reflection_x1 + REFLECTION_XOR_SIZE; xx++) - { - if (show_origin) - api->xorpixel(canvas, xx, reflection_y1); - else - api->putpixel(canvas, xx, reflection_y1, api->getpixel(last, xx, reflection_y1)); - } - - update_rect->x -= REFLECTION_XOR_SIZE; - update_rect->w += (REFLECTION_XOR_SIZE * 2); - update_rect->y -= REFLECTION_XOR_SIZE; - update_rect->h += (REFLECTION_XOR_SIZE * 2); + if (show_origin) + api->xorpixel(canvas, reflection_x1, yy); + else + api->putpixel(canvas, reflection_x1, yy, + api->getpixel(last, reflection_x1, yy)); } - api->playsound(reflection_snd, (x * 255) / canvas->w, (y * 255) / canvas->h); + for (xx = reflection_x1 - REFLECTION_XOR_SIZE; + xx < reflection_x1 + REFLECTION_XOR_SIZE; xx++) + { + if (show_origin) + api->xorpixel(canvas, xx, reflection_y1); + else + api->putpixel(canvas, xx, reflection_y1, + api->getpixel(last, xx, reflection_y1)); + } + + update_rect->x -= REFLECTION_XOR_SIZE; + update_rect->w += (REFLECTION_XOR_SIZE * 2); + update_rect->y -= REFLECTION_XOR_SIZE; + update_rect->h += (REFLECTION_XOR_SIZE * 2); + } + + api->playsound(reflection_snd, (x * 255) / canvas->w, + (y * 255) / canvas->h); } void reflection_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) { if (x <= 0) x = 1; @@ -365,8 +385,8 @@ void reflection_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, } void reflection_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) { do_reflection(api, canvas, last, x, y, update_rect, 0); } @@ -377,29 +397,35 @@ void reflection_shutdown(magic_api * api ATTRIBUTE_UNUSED) Mix_FreeChunk(reflection_snd); } -void reflection_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, - Uint8 b ATTRIBUTE_UNUSED) +void reflection_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, + Uint8 b ATTRIBUTE_UNUSED) { } -int reflection_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int reflection_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void reflection_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, - SDL_Surface * canvas) +void reflection_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas) { reflection_x1 = canvas->w / 2; reflection_y1 = canvas->h / 2; } -void reflection_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, - SDL_Surface * canvas ATTRIBUTE_UNUSED) +void reflection_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -int reflection_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int reflection_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MODE_PAINT; } diff --git a/magic/src/ripples.c b/magic/src/ripples.c index 7c779067e..2ee41b9d2 100644 --- a/magic/src/ripples.c +++ b/magic/src/ripples.c @@ -49,17 +49,22 @@ char *ripples_get_name(magic_api * api, int which); int ripples_get_group(magic_api * api, int which); char *ripples_get_description(magic_api * api, int which, int mode); void ripples_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); -static void ripples_linecb(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); -void ripples_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void ripples_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +static void ripples_linecb(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); +void ripples_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); +void ripples_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, + SDL_Rect * update_rect); void ripples_shutdown(magic_api * api); void ripples_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int ripples_requires_colors(magic_api * api, int which); -void ripples_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void ripples_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void ripples_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void ripples_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int ripples_modes(magic_api * api, int which); Uint32 ripples_api_version(void) @@ -75,7 +80,8 @@ int ripples_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/ripples.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/ripples.ogg", + api->data_directory); ripples_snd = Mix_LoadWAV(fname); return (1); @@ -92,38 +98,49 @@ SDL_Surface *ripples_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/ripples.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/ripples.png", + api->data_directory); return (IMG_Load(fname)); } // Return our names, localized: -char *ripples_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *ripples_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (strdup(gettext_noop("Ripples"))); } // Return our groups: -int ripples_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int ripples_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_DISTORTS; } // Return our descriptions, localized: -char *ripples_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *ripples_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED) { - return (strdup(gettext_noop("Click to make ripples appear over your picture."))); + return (strdup + (gettext_noop("Click to make ripples appear over your picture."))); } // Affect the canvas on drag: -void ripples_drag(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED, - SDL_Surface * last ATTRIBUTE_UNUSED, int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) +void ripples_drag(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED, + SDL_Surface * last ATTRIBUTE_UNUSED, + int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, + SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } static void ripples_linecb(void *ptr, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * last, int x, int y) + SDL_Surface * canvas, SDL_Surface * last, int x, + int y) { magic_api *api = (magic_api *) ptr; Uint8 r, g, b; @@ -141,7 +158,8 @@ static void ripples_linecb(void *ptr, int which ATTRIBUTE_UNUSED, // Affect the canvas on click: void ripples_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) { float radius; float fli; @@ -150,25 +168,26 @@ void ripples_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, radius = 100; for (fli = 0; fli < radius; fli = fli + .25) + { + ripples_z = (10 * deg_sin(((50 * 50) / (fli + 4)) * 10)); + + ox = fli * deg_cos(0) + x; + oy = -fli * deg_sin(0) + y; + + for (d = 0; d <= 360 + (360 / (fli + 1)); d = d + 360 / (fli + 1)) { - ripples_z = (10 * deg_sin(((50 * 50) / (fli + 4)) * 10)); + nx = fli * deg_cos(d) + x; + ny = -fli * deg_sin(d) + y; - ox = fli * deg_cos(0) + x; - oy = -fli * deg_sin(0) + y; + ripples_brite = (ripples_z * 20 * deg_sin(d + 45)) / ((fli / 4) + 1); - for (d = 0; d <= 360 + (360 / (fli + 1)); d = d + 360 / (fli + 1)) - { - nx = fli * deg_cos(d) + x; - ny = -fli * deg_sin(d) + y; + api->line((void *) api, which, canvas, last, ox, oy, nx, ny, 1, + ripples_linecb); - ripples_brite = (ripples_z * 20 * deg_sin(d + 45)) / ((fli / 4) + 1); - - api->line((void *)api, which, canvas, last, ox, oy, nx, ny, 1, ripples_linecb); - - ox = nx; - oy = ny; - } + ox = nx; + oy = ny; } + } update_rect->x = x - 100; update_rect->y = y - 100; @@ -179,9 +198,12 @@ void ripples_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, } // Affect the canvas on release: -void ripples_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) +void ripples_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) { } @@ -193,28 +215,33 @@ void ripples_shutdown(magic_api * api ATTRIBUTE_UNUSED) } // Record the color from Tux Paint: -void ripples_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, +void ripples_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) { } // Use colors: -int ripples_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int ripples_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void ripples_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void ripples_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void ripples_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void ripples_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -int ripples_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int ripples_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_ONECLICK); } diff --git a/magic/src/rosette.c b/magic/src/rosette.c index 103159bb4..4030c2d84 100644 --- a/magic/src/rosette.c +++ b/magic/src/rosette.c @@ -60,18 +60,25 @@ int rosette_get_group(magic_api * api, int which); char *rosette_get_description(magic_api * api, int which, int mode); int rosette_requires_colors(magic_api * api, int which); void rosette_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); + SDL_Surface * canvas, SDL_Surface * snapshot, int x, + int y, SDL_Rect * update_rect); void rosette_shutdown(magic_api * api); -void rosette_draw(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y); +void rosette_draw(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y); void rosette_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect); + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect); void rosette_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void rosette_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void rosette_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); + SDL_Surface * canvas, SDL_Surface * last, int x, int y, + SDL_Rect * update_rect); +void rosette_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void rosette_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int rosette_modes(magic_api * api, int which); -void rosette_circle(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y); +void rosette_circle(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y); Uint32 rosette_api_version(void) { @@ -89,7 +96,8 @@ int rosette_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/picasso.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/picasso.ogg", + api->data_directory); rosette_snd = Mix_LoadWAV(fname); return (1); @@ -105,9 +113,11 @@ SDL_Surface *rosette_get_icon(magic_api * api, int which) char fname[1024]; if (!which) - snprintf(fname, sizeof(fname), "%simages/magic/rosette.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/rosette.png", + api->data_directory); else - snprintf(fname, sizeof(fname), "%simages/magic/picasso.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/picasso.png", + api->data_directory); return (IMG_Load(fname)); } @@ -120,12 +130,14 @@ char *rosette_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) return strdup(gettext_noop("Picasso")); } -int rosette_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int rosette_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PATTERN_PAINTING; } -char *rosette_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode ATTRIBUTE_UNUSED) +char *rosette_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, + int mode ATTRIBUTE_UNUSED) { if (!which) return strdup(gettext_noop("Click and start drawing your rosette.")); //just k'scope with 3 bits? @@ -133,14 +145,18 @@ char *rosette_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int m return strdup(gettext_noop("You can draw just like Picasso!")); //what is this actually doing? } -int rosette_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int rosette_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 1; } -void rosette_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * snapshot ATTRIBUTE_UNUSED, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) +void rosette_release(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED, + SDL_Surface * snapshot ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, + SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -152,7 +168,8 @@ void rosette_shutdown(magic_api * api ATTRIBUTE_UNUSED) // Interactivity functions void rosette_circle(void *ptr, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * snapshot ATTRIBUTE_UNUSED, int x, int y) + SDL_Surface * canvas, + SDL_Surface * snapshot ATTRIBUTE_UNUSED, int x, int y) { magic_api *api = (magic_api *) ptr; @@ -161,11 +178,14 @@ void rosette_circle(void *ptr, int which ATTRIBUTE_UNUSED, for (yy = y - ROSETTE_R; yy < y + ROSETTE_R; yy++) for (xx = x - ROSETTE_R; xx < x + ROSETTE_R; xx++) if (api->in_circle(xx - x, yy - y, ROSETTE_R / 2)) - api->putpixel(canvas, xx, yy, SDL_MapRGB(canvas->format, rosette_colors.r, rosette_colors.g, rosette_colors.b)); + api->putpixel(canvas, xx, yy, + SDL_MapRGB(canvas->format, rosette_colors.r, + rosette_colors.g, rosette_colors.b)); } -void rosette_draw(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y) +void rosette_draw(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y) { magic_api *api = (magic_api *) ptr; @@ -173,41 +193,41 @@ void rosette_draw(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * snap double xx, yy; //distance to the center of the image int x1, y1, x2, y2; - xx = (double)(xmid - x); - yy = (double)(y - ymid); + xx = (double) (xmid - x); + yy = (double) (y - ymid); if (which == 0) - { - angle = 2 * M_PI / 3; //an angle between brushes + { + angle = 2 * M_PI / 3; //an angle between brushes - x1 = (int)(xx * cos(angle) - yy * sin(angle)); - y1 = (int)(xx * sin(angle) + yy * cos(angle)); + x1 = (int) (xx * cos(angle) - yy * sin(angle)); + y1 = (int) (xx * sin(angle) + yy * cos(angle)); - x2 = (int)(xx * cos(2 * angle) - yy * sin(2 * angle)); - y2 = (int)(xx * sin(2 * angle) + yy * cos(2 * angle)); - } + x2 = (int) (xx * cos(2 * angle) - yy * sin(2 * angle)); + y2 = (int) (xx * sin(2 * angle) + yy * cos(2 * angle)); + } else - { - angle = atan(yy / xx); + { + angle = atan(yy / xx); - if ((xx < 0) && (yy > 0)) - angle += M_PI; + if ((xx < 0) && (yy > 0)) + angle += M_PI; - if ((xx < 0) && (yy < 0)) - angle += M_PI; + if ((xx < 0) && (yy < 0)) + angle += M_PI; - if ((xx > 0) && (yy < 0)) - angle += 2 * M_PI; + if ((xx > 0) && (yy < 0)) + angle += 2 * M_PI; - if ((y == ymid) && (xx < 0)) - angle = M_PI; + if ((y == ymid) && (xx < 0)) + angle = M_PI; - x1 = (int)(xx * cos(2 * angle) - yy * sin(2 * angle)); - y1 = (int)(xx * sin(2 * angle) - yy * cos(angle)); + x1 = (int) (xx * cos(2 * angle) - yy * sin(2 * angle)); + y1 = (int) (xx * sin(2 * angle) - yy * cos(angle)); - x2 = (int)(xx * cos(2 * angle) - yy * sin(2 * angle)); - y2 = (int)(xx * sin(2 * angle) + yy * cos(2 * angle)); - } + x2 = (int) (xx * cos(2 * angle) - yy * sin(2 * angle)); + y2 = (int) (xx * sin(2 * angle) + yy * cos(2 * angle)); + } rosette_circle(api, which, canvas, snapshot, x, y); rosette_circle(api, which, canvas, snapshot, (-1) * (x1 - xmid), y1 + ymid); @@ -215,9 +235,11 @@ void rosette_draw(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * snap } void rosette_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect) + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect) { - api->line((void *)api, which, canvas, snapshot, ox, oy, x, y, 1, rosette_draw); + api->line((void *) api, which, canvas, snapshot, ox, oy, x, y, 1, + rosette_draw); api->playsound(rosette_snd, (x * 255) / canvas->w, 255); update_rect->x = update_rect->y = 0; @@ -226,25 +248,29 @@ void rosette_drag(magic_api * api, int which, SDL_Surface * canvas, } void rosette_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) { rosette_drag(api, which, canvas, last, x, y, x, y, update_rect); } -void rosette_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void rosette_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { xmid = canvas->w / 2; ymid = canvas->h / 2; } -void rosette_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void rosette_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -int rosette_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int rosette_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_PAINT); } diff --git a/magic/src/sharpen.c b/magic/src/sharpen.c index b58e75045..16ed81370 100644 --- a/magic/src/sharpen.c +++ b/magic/src/sharpen.c @@ -79,12 +79,15 @@ const char *sharpen_names[sharpen_NUM_TOOLS] = { }; const char *sharpen_descs[sharpen_NUM_TOOLS][2] = { - {gettext_noop("Click and drag the mouse to trace edges in parts of your picture."), + {gettext_noop + ("Click and drag the mouse to trace edges in parts of your picture."), gettext_noop("Click to trace edges in your entire picture."),}, {gettext_noop("Click and drag the mouse to sharpen parts of your picture."), gettext_noop("Click to sharpen the entire picture."),}, - {gettext_noop("Click and drag the mouse to create a black and white silhouette."), - gettext_noop("Click to create a black and white silhouette of your entire picture.")}, + {gettext_noop + ("Click and drag the mouse to create a black and white silhouette."), + gettext_noop + ("Click to create a black and white silhouette of your entire picture.")}, }; Uint32 sharpen_api_version(void); @@ -95,22 +98,29 @@ char *sharpen_get_name(magic_api * api, int which); int sharpen_get_group(magic_api * api, int which); char *sharpen_get_description(magic_api * api, int which, int mode); static int sharpen_grey(Uint8 r1, Uint8 g1, Uint8 b1); -static void do_sharpen_pixel(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); -static void do_sharpen_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int which); -static void do_sharpen_brush(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +static void do_sharpen_pixel(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); +static void do_sharpen_full(void *ptr, SDL_Surface * canvas, + SDL_Surface * last, int which); +static void do_sharpen_brush(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); void sharpen_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void sharpen_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void sharpen_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); void sharpen_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 sharpen_shutdown(magic_api * api); void sharpen_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int sharpen_requires_colors(magic_api * api, int which); -void sharpen_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void sharpen_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void sharpen_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void sharpen_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int sharpen_modes(magic_api * api, int which); Uint32 sharpen_api_version(void) @@ -127,10 +137,11 @@ int sharpen_init(magic_api * api) char fname[1024]; for (i = 0; i < sharpen_NUM_TOOLS; i++) - { - snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, sharpen_snd_filenames[i]); - sharpen_snd_effect[i] = Mix_LoadWAV(fname); - } + { + snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, + sharpen_snd_filenames[i]); + sharpen_snd_effect[i] = Mix_LoadWAV(fname); + } return (1); } @@ -146,7 +157,8 @@ SDL_Surface *sharpen_get_icon(magic_api * api, int which) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, sharpen_icon_filenames[which]); + snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, + sharpen_icon_filenames[which]); return (IMG_Load(fname)); } @@ -157,13 +169,15 @@ char *sharpen_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) } // Return our group (all the same): -int sharpen_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int sharpen_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_DISTORTS; } // Return our descriptions, localized: -char *sharpen_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode) +char *sharpen_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, + int mode) { return (strdup(gettext_noop(sharpen_descs[which][mode - 1]))); } @@ -175,7 +189,8 @@ static int sharpen_grey(Uint8 r1, Uint8 g1, Uint8 b1) } // Do the effect: -static void do_sharpen_pixel(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void do_sharpen_pixel(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y) { magic_api *api = (magic_api *) ptr; @@ -198,104 +213,113 @@ static void do_sharpen_pixel(void *ptr, int which, SDL_Surface * canvas, SDL_Sur sobel_1 = 0; sobel_2 = 0; for (i = -1; i < 2; i++) + { + for (j = -1; j < 2; j++) { - for (j = -1; j < 2; j++) - { - //No need to check if inside canvas, getpixel does it for us. - SDL_GetRGB(api->getpixel(last, x + i, y + j), last->format, &r1, &g1, &b1); - grey = sharpen_grey(r1, g1, b1); - sobel_1 += grey * sobel_weights_1[i + 1][j + 1]; - sobel_2 += grey * sobel_weights_2[i + 1][j + 1]; - } + //No need to check if inside canvas, getpixel does it for us. + SDL_GetRGB(api->getpixel(last, x + i, y + j), last->format, &r1, &g1, + &b1); + grey = sharpen_grey(r1, g1, b1); + sobel_1 += grey * sobel_weights_1[i + 1][j + 1]; + sobel_2 += grey * sobel_weights_2[i + 1][j + 1]; } + } temp = sqrt(sobel_1 * sobel_1 + sobel_2 * sobel_2); temp = (temp / 1443) * 255.0; // set image to white where edge value is below THRESHOLD if (which == TOOL_TRACE) + { + if (temp < THRESHOLD) { - if (temp < THRESHOLD) - { - api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, 255, 255, 255)); - } + api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, 255, 255, 255)); } + } //Simply display the edge values - provides a nice black and white silhouette image else if (which == TOOL_SILHOUETTE) - { - api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, temp, temp, temp)); - } + { + api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, temp, temp, temp)); + } //Add the edge values to the original image, creating a more distinct jump in contrast at edges else if (which == TOOL_SHARPEN) - { - SDL_GetRGB(api->getpixel(last, x, y), last->format, &r1, &g1, &b1); - api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, clamp(0.0, r1 + SHARPEN * temp, 255.0), - clamp(0.0, g1 + SHARPEN * temp, 255.0), - clamp(0.0, b1 + SHARPEN * temp, 255.0))); - } + { + SDL_GetRGB(api->getpixel(last, x, y), last->format, &r1, &g1, &b1); + api->putpixel(canvas, x, y, + SDL_MapRGB(canvas->format, + clamp(0.0, r1 + SHARPEN * temp, 255.0), + clamp(0.0, g1 + SHARPEN * temp, 255.0), + clamp(0.0, b1 + SHARPEN * temp, 255.0))); + } } // Do the effect for the full image -static void do_sharpen_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int which) +static void do_sharpen_full(void *ptr, SDL_Surface * canvas, + SDL_Surface * last, int which) { - magic_api * api = (magic_api *) ptr; + magic_api *api = (magic_api *) ptr; int x, y; for (y = 0; y < last->h; y++) + { + if (y % 10 == 0) { - if (y % 10 == 0) { - api->update_progress_bar(); - } - - for (x = 0; x < last->w; x++) - { - do_sharpen_pixel(api, which, canvas, last, x, y); - } + api->update_progress_bar(); } + + for (x = 0; x < last->w; x++) + { + do_sharpen_pixel(api, which, canvas, last, x, y); + } + } } //do the effect for the brush -static void do_sharpen_brush(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void do_sharpen_brush(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y) { int xx, yy; magic_api *api = (magic_api *) ptr; for (yy = y - sharpen_RADIUS; yy < y + sharpen_RADIUS; yy++) + { + for (xx = x - sharpen_RADIUS; xx < x + sharpen_RADIUS; xx++) { - for (xx = x - sharpen_RADIUS; xx < x + sharpen_RADIUS; xx++) - { - if (api->in_circle(xx - x, yy - y, sharpen_RADIUS) && !api->touched(xx, yy)) - { - do_sharpen_pixel(api, which, canvas, last, xx, yy); - } - } + if (api->in_circle(xx - x, yy - y, sharpen_RADIUS) + && !api->touched(xx, yy)) + { + do_sharpen_pixel(api, which, canvas, last, xx, yy); + } } + } } // Affect the canvas on drag: void sharpen_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) { - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_sharpen_brush); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, + do_sharpen_brush); api->playsound(sharpen_snd_effect[which], (x * 255) / canvas->w, 255); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = ox - sharpen_RADIUS; update_rect->y = oy - sharpen_RADIUS; @@ -305,25 +329,29 @@ void sharpen_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void sharpen_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) { if (mode == MODE_PAINT) sharpen_drag(api, which, canvas, last, x, y, x, y, update_rect); else - { - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; - do_sharpen_full(api, canvas, last, which); - api->playsound(sharpen_snd_effect[which], 128, 255); - } + { + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; + do_sharpen_full(api, canvas, last, which); + api->playsound(sharpen_snd_effect[which], 128, 255); + } } // Affect the canvas on release: -void sharpen_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) +void sharpen_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) { } @@ -334,37 +362,42 @@ void sharpen_shutdown(magic_api * api ATTRIBUTE_UNUSED) int i; for (i = 0; i < sharpen_NUM_TOOLS; i++) + { + if (sharpen_snd_effect[i] != NULL) { - if (sharpen_snd_effect[i] != NULL) - { - Mix_FreeChunk(sharpen_snd_effect[i]); - } + Mix_FreeChunk(sharpen_snd_effect[i]); } + } } // Record the color from Tux Paint: -void sharpen_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, +void sharpen_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) { } // Use colors: -int sharpen_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int sharpen_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void sharpen_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void sharpen_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void sharpen_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void sharpen_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -int sharpen_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int sharpen_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_FULLSCREEN | MODE_PAINT); } diff --git a/magic/src/shift.c b/magic/src/shift.c index 73b3b2ac6..2b829e899 100644 --- a/magic/src/shift.c +++ b/magic/src/shift.c @@ -44,7 +44,8 @@ static Mix_Chunk *shift_snd; /* Local function prototypes: */ static void shift_doit(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect, int crosshairs); + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect, int crosshairs); Uint32 shift_api_version(void); int shift_init(magic_api * api); int shift_get_tool_count(magic_api * api); @@ -53,17 +54,20 @@ char *shift_get_name(magic_api * api, int which); int shift_get_group(magic_api * api, int which); char *shift_get_description(magic_api * api, int which, int mode); void shift_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void shift_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void shift_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void shift_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); +void shift_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); void shift_shutdown(magic_api * api); void shift_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int shift_requires_colors(magic_api * api, int which); -void shift_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void shift_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void shift_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void shift_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int shift_modes(magic_api * api, int which); @@ -79,7 +83,8 @@ int shift_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/shift.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/shift.ogg", + api->data_directory); shift_snd = Mix_LoadWAV(fname); return (1); @@ -92,37 +97,46 @@ int shift_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) } // Load our icons: -SDL_Surface *shift_get_icon(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +SDL_Surface *shift_get_icon(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/shift.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/shift.png", + api->data_directory); return (IMG_Load(fname)); } // Return our names, localized: -char *shift_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *shift_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (strdup(gettext_noop("Shift"))); } // Return our group -int shift_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int shift_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PICTURE_WARPS; } // Return our descriptions, localized: -char *shift_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *shift_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED) { - return (strdup(gettext_noop("Click and drag to shift your picture around on the canvas."))); + return (strdup + (gettext_noop + ("Click and drag to shift your picture around on the canvas."))); } // Affect the canvas on drag: void shift_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) { if (ox == x && oy == y) return; /* No-op */ @@ -130,8 +144,10 @@ void shift_drag(magic_api * api, int which, SDL_Surface * canvas, shift_doit(api, which, canvas, last, ox, oy, x, y, update_rect, 1); } -static void shift_doit(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, - SDL_Surface * last, int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, int x, int y, +static void shift_doit(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, + SDL_Surface * last, int ox ATTRIBUTE_UNUSED, + int oy ATTRIBUTE_UNUSED, int x, int y, SDL_Rect * update_rect, int crosshairs) { SDL_Rect dest; @@ -162,123 +178,123 @@ static void shift_doit(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNU if (dy > 0) + { + if (dx > 0) { - if (dx > 0) - { - /* Top Left */ + /* Top Left */ - dest.x = dx - canvas->w; - dest.y = dy - canvas->h; - - SDL_BlitSurface(last, NULL, canvas, &dest); - } - - - /* Top */ - - dest.x = dx; + dest.x = dx - canvas->w; dest.y = dy - canvas->h; SDL_BlitSurface(last, NULL, canvas, &dest); - - - if (dx < 0) - { - /* Top Right */ - - dest.x = dx + canvas->w; - dest.y = dy - canvas->h; - - SDL_BlitSurface(last, NULL, canvas, &dest); - } } + /* Top */ + + dest.x = dx; + dest.y = dy - canvas->h; + + SDL_BlitSurface(last, NULL, canvas, &dest); + + + if (dx < 0) + { + /* Top Right */ + + dest.x = dx + canvas->w; + dest.y = dy - canvas->h; + + SDL_BlitSurface(last, NULL, canvas, &dest); + } + } + + if (dx > 0) - { - /* Left */ + { + /* Left */ - dest.x = dx - canvas->w; - dest.y = dy; + dest.x = dx - canvas->w; + dest.y = dy; - SDL_BlitSurface(last, NULL, canvas, &dest); - } + SDL_BlitSurface(last, NULL, canvas, &dest); + } if (dx < 0) - { - /* Right */ + { + /* Right */ - dest.x = dx + canvas->w; - dest.y = dy; + dest.x = dx + canvas->w; + dest.y = dy; - SDL_BlitSurface(last, NULL, canvas, &dest); - } + SDL_BlitSurface(last, NULL, canvas, &dest); + } if (dy < 0) + { + if (dx > 0) { - if (dx > 0) - { - /* Bottom Left */ + /* Bottom Left */ - dest.x = dx - canvas->w; - dest.y = dy + canvas->h; - - SDL_BlitSurface(last, NULL, canvas, &dest); - } - - - /* Bottom */ - - dest.x = dx; + dest.x = dx - canvas->w; dest.y = dy + canvas->h; SDL_BlitSurface(last, NULL, canvas, &dest); - - - if (dx < 0) - { - /* Bottom Right */ - - dest.x = dx + canvas->w; - dest.y = dy + canvas->h; - - SDL_BlitSurface(last, NULL, canvas, &dest); - } } + /* Bottom */ + + dest.x = dx; + dest.y = dy + canvas->h; + + SDL_BlitSurface(last, NULL, canvas, &dest); + + + if (dx < 0) + { + /* Bottom Right */ + + dest.x = dx + canvas->w; + dest.y = dy + canvas->h; + + SDL_BlitSurface(last, NULL, canvas, &dest); + } + } + + if (crosshairs) - { - dest.x = (canvas->w / 2) - 1; - dest.y = 0; - dest.w = 3; - dest.h = canvas->h; + { + dest.x = (canvas->w / 2) - 1; + dest.y = 0; + dest.w = 3; + dest.h = canvas->h; - SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, 255, 255, 255)); + SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, 255, 255, 255)); - dest.x = 0; - dest.y = (canvas->h / 2) - 1; - dest.w = canvas->w; - dest.h = 3; + dest.x = 0; + dest.y = (canvas->h / 2) - 1; + dest.w = canvas->w; + dest.h = 3; - SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, 255, 255, 255)); + SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, 255, 255, 255)); - dest.x = canvas->w / 2; - dest.y = 0; - dest.w = 1; - dest.h = canvas->h; + dest.x = canvas->w / 2; + dest.y = 0; + dest.w = 1; + dest.h = canvas->h; - SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, 0, 0, 0)); + SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, 0, 0, 0)); - dest.x = 0; - dest.y = canvas->h / 2; - dest.w = canvas->w; - dest.h = 1; + dest.x = 0; + dest.y = canvas->h / 2; + dest.w = canvas->w; + dest.h = 1; - SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, 0, 0, 0)); - } + SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, 0, 0, 0)); + } /* Update everything! */ @@ -293,7 +309,8 @@ static void shift_doit(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNU // Affect the canvas on click: void shift_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) { shift_x = x; shift_y = y; @@ -303,7 +320,8 @@ void shift_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, // Affect the canvas on release: void shift_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) { shift_doit(api, which, canvas, last, x, y, x, y, update_rect, 0); api->stopsound(); @@ -319,22 +337,26 @@ void shift_shutdown(magic_api * api ATTRIBUTE_UNUSED) // Record the color from Tux Paint: void shift_set_color(magic_api * api ATTRIBUTE_UNUSED, - Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, + Uint8 b ATTRIBUTE_UNUSED) { } // Use colors: -int shift_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int shift_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void shift_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void shift_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void shift_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void shift_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } diff --git a/magic/src/smudge.c b/magic/src/smudge.c index 178be4160..eb6c3e0c9 100644 --- a/magic/src/smudge.c +++ b/magic/src/smudge.c @@ -46,18 +46,22 @@ SDL_Surface *smudge_get_icon(magic_api * api, int which); char *smudge_get_name(magic_api * api, int which); int smudge_get_group(magic_api * api, int which); char *smudge_get_description(magic_api * api, int which, int mode); -static void do_smudge(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +static void do_smudge(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); void smudge_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void smudge_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void smudge_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void smudge_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); +void smudge_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); void smudge_shutdown(magic_api * api); void smudge_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int smudge_requires_colors(magic_api * api, int which); -void smudge_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void smudge_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void smudge_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void smudge_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int smudge_modes(magic_api * api, int which); int smudge_get_tool_count(magic_api * api); @@ -66,7 +70,8 @@ int smudge_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/smudge.wav", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/smudge.wav", + api->data_directory); smudge_snd = Mix_LoadWAV(fname); return (1); @@ -89,9 +94,11 @@ SDL_Surface *smudge_get_icon(magic_api * api, int which) char fname[1024]; if (which == 0) - snprintf(fname, sizeof(fname), "%simages/magic/smudge.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/smudge.png", + api->data_directory); else /* if (which == 1) */ - snprintf(fname, sizeof(fname), "%simages/magic/wetpaint.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/wetpaint.png", + api->data_directory); return (IMG_Load(fname)); } @@ -115,17 +122,23 @@ int smudge_get_group(magic_api * api ATTRIBUTE_UNUSED, int which) } // Return our descriptions, localized: -char *smudge_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode ATTRIBUTE_UNUSED) +char *smudge_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, + int mode ATTRIBUTE_UNUSED) { if (which == 0) - return (strdup(gettext_noop("Click and drag the mouse around to smudge the picture."))); + return (strdup + (gettext_noop + ("Click and drag the mouse around to smudge the picture."))); else /* if (which == 1) */ - return (strdup(gettext_noop("Click and drag the mouse around to draw with wet, smudgy paint."))); + return (strdup + (gettext_noop + ("Click and drag the mouse around to draw with wet, smudgy paint."))); } // Do the effect: -static void do_smudge(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void do_smudge(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y) { magic_api *api = (magic_api *) ptr; static double state[32][32][3]; @@ -135,66 +148,80 @@ static void do_smudge(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * int xx, yy, strength; if (which == 1) - { - /* Wet paint */ - for (yy = -8; yy < 8; yy++) - for (xx = -8; xx < 8; xx++) - if (api->in_circle(xx, yy, 8)) - { - SDL_GetRGB(api->getpixel(last, x + xx, y + yy), last->format, &r, &g, &b); - //strength = (abs(xx * yy) / 8) + 6; - strength = (abs(xx * yy) / 8) + 1; - api->putpixel(canvas, x + xx, y + yy, SDL_MapRGB(canvas->format, - (smudge_r + r * strength) / (strength + 1), - (smudge_g + g * strength) / (strength + 1), - (smudge_b + b * strength) / (strength + 1))); - } - } + { + /* Wet paint */ + for (yy = -8; yy < 8; yy++) + for (xx = -8; xx < 8; xx++) + if (api->in_circle(xx, yy, 8)) + { + SDL_GetRGB(api->getpixel(last, x + xx, y + yy), last->format, &r, + &g, &b); + //strength = (abs(xx * yy) / 8) + 6; + strength = (abs(xx * yy) / 8) + 1; + api->putpixel(canvas, x + xx, y + yy, SDL_MapRGB(canvas->format, + (smudge_r + + r * strength) / + (strength + 1), + (smudge_g + + g * strength) / + (strength + 1), + (smudge_b + + b * strength) / + (strength + 1))); + } + } while (i--) - { - int iy = i >> 5; - int ix = i & 0x1f; + { + int iy = i >> 5; + int ix = i & 0x1f; - // is it not on the circle of radius sqrt(120) at location 16,16? - if ((ix - 16) * (ix - 16) + (iy - 16) * (iy - 16) > 120) - continue; - // it is on the circle, so grab it + // is it not on the circle of radius sqrt(120) at location 16,16? + if ((ix - 16) * (ix - 16) + (iy - 16) * (iy - 16) > 120) + continue; + // it is on the circle, so grab it - SDL_GetRGB(api->getpixel(canvas, x + ix - 16, y + iy - 16), last->format, &r, &g, &b); - state[ix][iy][0] = rate * state[ix][iy][0] + (1.0 - rate) * api->sRGB_to_linear(r); - state[ix][iy][1] = rate * state[ix][iy][1] + (1.0 - rate) * api->sRGB_to_linear(g); - state[ix][iy][2] = rate * state[ix][iy][2] + (1.0 - rate) * api->sRGB_to_linear(b); + SDL_GetRGB(api->getpixel(canvas, x + ix - 16, y + iy - 16), last->format, + &r, &g, &b); + state[ix][iy][0] = + rate * state[ix][iy][0] + (1.0 - rate) * api->sRGB_to_linear(r); + state[ix][iy][1] = + rate * state[ix][iy][1] + (1.0 - rate) * api->sRGB_to_linear(g); + state[ix][iy][2] = + rate * state[ix][iy][2] + (1.0 - rate) * api->sRGB_to_linear(b); - // opacity 100% --> new data not blended w/ existing data - api->putpixel(canvas, x + ix - 16, y + iy - 16, - SDL_MapRGB(canvas->format, api->linear_to_sRGB(state[ix][iy][0]), - api->linear_to_sRGB(state[ix][iy][1]), api->linear_to_sRGB(state[ix][iy][2]))); - } + // opacity 100% --> new data not blended w/ existing data + api->putpixel(canvas, x + ix - 16, y + iy - 16, + SDL_MapRGB(canvas->format, + api->linear_to_sRGB(state[ix][iy][0]), + api->linear_to_sRGB(state[ix][iy][1]), + api->linear_to_sRGB(state[ix][iy][2]))); + } } // Affect the canvas on drag: void smudge_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) { - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_smudge); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, do_smudge); api->playsound(smudge_snd, (x * 255) / canvas->w, 255); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = ox - 16; update_rect->y = oy - 16; @@ -204,15 +231,19 @@ void smudge_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void smudge_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) { smudge_drag(api, which, canvas, last, x, y, x, y, update_rect); } // Affect the canvas on click: -void smudge_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) +void smudge_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) { } @@ -224,7 +255,8 @@ void smudge_shutdown(magic_api * api ATTRIBUTE_UNUSED) } // Record the color from Tux Paint: -void smudge_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) +void smudge_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, + Uint8 b) { smudge_r = r; smudge_g = g; @@ -232,7 +264,8 @@ void smudge_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 } // Use colors: -int smudge_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int smudge_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { if (which == 0) return 0; @@ -240,12 +273,14 @@ int smudge_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE return 1; } -void smudge_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void smudge_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void smudge_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void smudge_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } diff --git a/magic/src/snow.c b/magic/src/snow.c index 31941edc6..decac94ac 100644 --- a/magic/src/snow.c +++ b/magic/src/snow.c @@ -84,19 +84,24 @@ SDL_Surface *snow_get_icon(magic_api * api, int which); char *snow_get_name(magic_api * api, int which); int snow_get_group(magic_api * api, int which); char *snow_get_description(magic_api * api, int which); -static void do_snow(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int which, int snowAmount); +static void do_snow(void *ptr, SDL_Surface * canvas, SDL_Surface * last, + int which, int snowAmount); void snow_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void snow_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void snow_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); void snow_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 snow_shutdown(magic_api * api); void snow_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int snow_requires_colors(magic_api * api, int which); -void snow_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void snow_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void snow_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void snow_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int snow_modes(magic_api * api, int which); Uint32 snow_api_version(void) { @@ -112,29 +117,32 @@ int snow_init(magic_api * api) srand(time(0)); - snprintf(fname, sizeof(fname), "%simages/magic/Snow_flake4.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/Snow_flake4.png", + api->data_directory); snow_flake1 = IMG_Load(fname); if (snow_flake1 == NULL) - { - return (0); - } + { + return (0); + } - snprintf(fname, sizeof(fname), "%simages/magic/Snow_flake5.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/Snow_flake5.png", + api->data_directory); snow_flake2 = IMG_Load(fname); if (snow_flake2 == NULL) - { - return (0); - } + { + return (0); + } if (snow_flake2 == NULL) - { - printf("meh\n"); - } + { + printf("meh\n"); + } for (i = 0; i < snow_NUM_TOOLS; i++) - { - snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, snow_snd_filenames[i]); - snow_snd_effect[i] = Mix_LoadWAV(fname); - } + { + snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, + snow_snd_filenames[i]); + snow_snd_effect[i] = Mix_LoadWAV(fname); + } return (1); } @@ -148,7 +156,8 @@ SDL_Surface *snow_get_icon(magic_api * api, int which) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, snow_icon_filenames[which]); + snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, + snow_icon_filenames[which]); return (IMG_Load(fname)); } @@ -158,9 +167,10 @@ char *snow_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) return (strdup(gettext_noop(snow_names[which]))); } -int snow_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int snow_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { - return MAGIC_TYPE_PICTURE_DECORATIONS; /* Because we affect the whole image, and not just around the mouse */ + return MAGIC_TYPE_PICTURE_DECORATIONS; /* Because we affect the whole image, and not just around the mouse */ } // Return our descriptions, localized: @@ -170,7 +180,8 @@ char *snow_get_description(magic_api * api ATTRIBUTE_UNUSED, int which) } // Do the effect: -static void do_snow(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int which, int snowAmount) +static void do_snow(void *ptr, SDL_Surface * canvas, SDL_Surface * last, + int which, int snowAmount) { magic_api *api = (magic_api *) ptr; @@ -179,43 +190,48 @@ static void do_snow(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int whi SDL_Rect dest; for (i = 0; i < snowAmount; i++) + { + centre_x = rand() % canvas->w; + centre_y = rand() % canvas->h; + if (which == TOOL_SNOWBALL) { - centre_x = rand() % canvas->w; - centre_y = rand() % canvas->h; - if (which == TOOL_SNOWBALL) + for (y = -snow_RADIUS; y < snow_RADIUS; y++) + { + for (x = -snow_RADIUS; x < snow_RADIUS; x++) { - for (y = -snow_RADIUS; y < snow_RADIUS; y++) - { - for (x = -snow_RADIUS; x < snow_RADIUS; x++) - { - if (api->in_circle(x, y, snow_RADIUS)) - { - SDL_GetRGB(api->getpixel(last, centre_x + x, centre_y + y), last->format, &r, &g, &b); - api->putpixel(canvas, centre_x + x, centre_y + y, SDL_MapRGB(canvas->format, 255, 255, 255)); - } - } - } - } - if (which == TOOL_SNOWFLAKE) - { - dest.x = centre_x; - dest.y = centre_y; - if (rand() % 2 == 0) - { - SDL_BlitSurface(snow_flake1, NULL, canvas, &dest); - } - else - { - SDL_BlitSurface(snow_flake2, NULL, canvas, &dest); - } + if (api->in_circle(x, y, snow_RADIUS)) + { + SDL_GetRGB(api->getpixel(last, centre_x + x, centre_y + y), + last->format, &r, &g, &b); + api->putpixel(canvas, centre_x + x, centre_y + y, + SDL_MapRGB(canvas->format, 255, 255, 255)); + } } + } } + if (which == TOOL_SNOWFLAKE) + { + dest.x = centre_x; + dest.y = centre_y; + if (rand() % 2 == 0) + { + SDL_BlitSurface(snow_flake1, NULL, canvas, &dest); + } + else + { + SDL_BlitSurface(snow_flake2, NULL, canvas, &dest); + } + } + } } // Affect the canvas on drag: -void snow_drag(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED, - SDL_Surface * last ATTRIBUTE_UNUSED, int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) +void snow_drag(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED, + SDL_Surface * last ATTRIBUTE_UNUSED, int ox ATTRIBUTE_UNUSED, + int oy ATTRIBUTE_UNUSED, int x ATTRIBUTE_UNUSED, + int y ATTRIBUTE_UNUSED, + SDL_Rect * update_rect ATTRIBUTE_UNUSED) { // No-op } @@ -223,7 +239,8 @@ void snow_drag(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, SDL // Affect the canvas on click: void snow_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * last, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect) + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, + SDL_Rect * update_rect) { update_rect->x = 0; update_rect->y = 0; @@ -235,9 +252,12 @@ void snow_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, } // Affect the canvas on release: -void snow_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) +void snow_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) { } @@ -248,40 +268,44 @@ void snow_shutdown(magic_api * api ATTRIBUTE_UNUSED) int i; for (i = 0; i < snow_NUM_TOOLS; i++) + { + if (snow_snd_effect[i] != NULL) { - if (snow_snd_effect[i] != NULL) - { - Mix_FreeChunk(snow_snd_effect[i]); - } + Mix_FreeChunk(snow_snd_effect[i]); } + } if (snow_flake1 != NULL) - { - SDL_FreeSurface(snow_flake1); - } + { + SDL_FreeSurface(snow_flake1); + } if (snow_flake2 != NULL) - { - SDL_FreeSurface(snow_flake2); - } + { + SDL_FreeSurface(snow_flake2); + } } // Record the color from Tux Paint: -void snow_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, +void snow_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) { } // Use colors: -int snow_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int snow_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void snow_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void snow_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void snow_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void snow_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } diff --git a/magic/src/stretch.c b/magic/src/stretch.c index f9b7f2389..08c8a3903 100644 --- a/magic/src/stretch.c +++ b/magic/src/stretch.c @@ -57,15 +57,21 @@ int stretch_get_group(magic_api * api, int which); char *stretch_get_description(magic_api * api, int which, int mode); int stretch_requires_colors(magic_api * api, int which); void stretch_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); + SDL_Surface * canvas, SDL_Surface * snapshot, int x, + int y, SDL_Rect * update_rect); void stretch_shutdown(magic_api * api); -void stretch_paint_stretch(void *ptr_to_api, int which_tool, SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y); +void stretch_paint_stretch(void *ptr_to_api, int which_tool, + SDL_Surface * canvas, SDL_Surface * snapshot, + int x, int y); void stretch_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void stretch_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void stretch_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void stretch_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void stretch_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); +void stretch_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void stretch_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int stretch_modes(magic_api * api, int which); // Housekeeping functions @@ -74,7 +80,9 @@ Uint32 stretch_api_version(void) return (TP_MAGIC_API_VERSION); } -void stretch_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) +void stretch_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, + Uint8 b ATTRIBUTE_UNUSED) { } @@ -82,7 +90,8 @@ int stretch_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/stretch.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/stretch.ogg", + api->data_directory); stretch_snd = Mix_LoadWAV(fname); return (1); @@ -97,36 +106,45 @@ SDL_Surface *stretch_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/stretch.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/stretch.png", + api->data_directory); return (IMG_Load(fname)); } -char *stretch_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *stretch_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return strdup(gettext_noop("Stretch")); } -int stretch_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int stretch_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PICTURE_WARPS; } -char *stretch_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *stretch_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED) { return strdup(gettext_noop ("Click and drag to stretch part of your picture vertically or horizontally.")); } -int stretch_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int stretch_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void stretch_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * snapshot ATTRIBUTE_UNUSED, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) +void stretch_release(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED, + SDL_Surface * snapshot ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, + SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -137,9 +155,10 @@ void stretch_shutdown(magic_api * api ATTRIBUTE_UNUSED) // Interactivity functions -void stretch_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, - int x, int y, SDL_Rect * update_rect) +void stretch_drag(magic_api * api, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * snapshot, + int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, int x, + int y, SDL_Rect * update_rect) { SDL_Rect src, dest; float xx, yy; @@ -148,92 +167,94 @@ void stretch_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * can SDL_BlitSurface(snapshot, NULL, canvas, NULL); switch (stretch_side) + { + case STRETCH_DIRECTION_VERT: { - case STRETCH_DIRECTION_VERT: + if (y != stretch_start_y) { - if (y != stretch_start_y) - { - divisor1 = (float) y / (float) stretch_start_y; - divisor2 = (float) (canvas->h - y) / (float) (canvas->h - stretch_start_y); + divisor1 = (float) y / (float) stretch_start_y; + divisor2 = + (float) (canvas->h - y) / (float) (canvas->h - stretch_start_y); - for (yy = 0; yy < y; yy++) - { - src.x = 0; - src.y = (yy / divisor1); - src.w = canvas->w; - src.h = 1; + for (yy = 0; yy < y; yy++) + { + src.x = 0; + src.y = (yy / divisor1); + src.w = canvas->w; + src.h = 1; - dest.x = 0; - dest.y = yy; - dest.w = canvas->w; - dest.h = 1; + dest.x = 0; + dest.y = yy; + dest.w = canvas->w; + dest.h = 1; - SDL_BlitSurface(snapshot, &src, canvas, &dest); - } + SDL_BlitSurface(snapshot, &src, canvas, &dest); + } - for (yy = y; yy < canvas->h; yy++) - { - src.x = 0; - src.y = stretch_start_y + ((yy - y) / divisor2); - src.w = canvas->w; - src.h = 1; + for (yy = y; yy < canvas->h; yy++) + { + src.x = 0; + src.y = stretch_start_y + ((yy - y) / divisor2); + src.w = canvas->w; + src.h = 1; - dest.x = 0; - dest.y = yy; - dest.w = canvas->w; - dest.h = 1; + dest.x = 0; + dest.y = yy; + dest.w = canvas->w; + dest.h = 1; - SDL_BlitSurface(snapshot, &src, canvas, &dest); - } + SDL_BlitSurface(snapshot, &src, canvas, &dest); + } - api->playsound(stretch_snd, 128, 255); - } - break; - } - - case STRETCH_DIRECTION_HORIZ: - { - if (x != stretch_start_x) - { - divisor1 = (float) x / (float) stretch_start_x; - divisor2 = (float) (canvas->w - x) / (float) (canvas->w - stretch_start_x); - - for (xx = 0; xx < x; xx++) - { - src.x = (xx / divisor1); - src.y = 0; - src.w = 1; - src.h = canvas->h; - - dest.x = xx; - dest.y = 0; - dest.w = 1; - dest.h = canvas->h; - - SDL_BlitSurface(snapshot, &src, canvas, &dest); - } - - for (xx = x; xx < canvas->w; xx++) - { - src.x = stretch_start_x + ((xx - x) / divisor2); - src.y = 0; - src.w = 1; - src.h = canvas->h; - - dest.x = xx; - dest.y = 0; - dest.w = 1; - dest.h = canvas->h; - - SDL_BlitSurface(snapshot, &src, canvas, &dest); - } - api->playsound(stretch_snd, (x * 255) / canvas->w, 255); - } - - break; + api->playsound(stretch_snd, 128, 255); } + break; } + case STRETCH_DIRECTION_HORIZ: + { + if (x != stretch_start_x) + { + divisor1 = (float) x / (float) stretch_start_x; + divisor2 = + (float) (canvas->w - x) / (float) (canvas->w - stretch_start_x); + + for (xx = 0; xx < x; xx++) + { + src.x = (xx / divisor1); + src.y = 0; + src.w = 1; + src.h = canvas->h; + + dest.x = xx; + dest.y = 0; + dest.w = 1; + dest.h = canvas->h; + + SDL_BlitSurface(snapshot, &src, canvas, &dest); + } + + for (xx = x; xx < canvas->w; xx++) + { + src.x = stretch_start_x + ((xx - x) / divisor2); + src.y = 0; + src.w = 1; + src.h = canvas->h; + + dest.x = xx; + dest.y = 0; + dest.w = 1; + dest.h = canvas->h; + + SDL_BlitSurface(snapshot, &src, canvas, &dest); + } + api->playsound(stretch_snd, (x * 255) / canvas->w, 255); + } + + break; + } + } + update_rect->x = 0; update_rect->y = 0; update_rect->w = canvas->w; @@ -241,26 +262,27 @@ void stretch_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * can } void stretch_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) { if (y < canvas->h / 2) - { - if (x < y) - stretch_side = STRETCH_DIRECTION_HORIZ; - else if (canvas->w - x < y) - stretch_side = STRETCH_DIRECTION_HORIZ; - else - stretch_side = STRETCH_DIRECTION_VERT; - } + { + if (x < y) + stretch_side = STRETCH_DIRECTION_HORIZ; + else if (canvas->w - x < y) + stretch_side = STRETCH_DIRECTION_HORIZ; + else + stretch_side = STRETCH_DIRECTION_VERT; + } else - { - if (x < canvas->h - y) - stretch_side = STRETCH_DIRECTION_HORIZ; - else if (canvas->w - x < canvas->h - y) - stretch_side = STRETCH_DIRECTION_HORIZ; - else - stretch_side = STRETCH_DIRECTION_VERT; - } + { + if (x < canvas->h - y) + stretch_side = STRETCH_DIRECTION_HORIZ; + else if (canvas->w - x < canvas->h - y) + stretch_side = STRETCH_DIRECTION_HORIZ; + else + stretch_side = STRETCH_DIRECTION_VERT; + } stretch_start_x = x; stretch_start_y = y; @@ -268,19 +290,22 @@ void stretch_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, stretch_drag(api, which, canvas, last, x, y, x, y, update_rect); } -void stretch_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, - SDL_Surface * canvas ATTRIBUTE_UNUSED) +void stretch_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void stretch_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, - SDL_Surface * canvas ATTRIBUTE_UNUSED) +void stretch_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -int stretch_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int stretch_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_PAINT); } diff --git a/magic/src/string.c b/magic/src/string.c index 2bad6bfc5..4e878df7f 100644 --- a/magic/src/string.c +++ b/magic/src/string.c @@ -10,7 +10,8 @@ unsigned int img_w, img_h; static Uint8 string_r, string_g, string_b; static int string_ox, string_oy; -static int string_vertex_x, string_vertex_y, string_vertex_done, string_vertex_distance; +static int string_vertex_x, string_vertex_y, string_vertex_done, + string_vertex_distance; static SDL_Surface *canvas_backup; enum string_tools { @@ -24,25 +25,28 @@ Mix_Chunk *string_snd[STRING_NUMTOOLS]; // Custom function declarations -void string_callback(void *ptr, int which_tool, SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y); -void string_draw_triangle(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int ox, int oy, int x, int y, - SDL_Rect * update_rect); -void string_draw_angle(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int ox, int oy, int x, int y, +void string_callback(void *ptr, int which_tool, SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y); +void string_draw_triangle(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * snapshot, int ox, int oy, int x, + int y, SDL_Rect * update_rect); +void string_draw_angle(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect); void string_draw_triangle_preview(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int ox, int oy, int x, int y, - SDL_Rect * update_rect); + SDL_Surface * canvas, + SDL_Surface * snapshot, int ox, int oy, + int x, int y, SDL_Rect * update_rect); void string_draw_angle_preview(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Surface * canvas, SDL_Surface * snapshot, + int ox, int oy, int x, int y, SDL_Rect * update_rect); void scale_xcoord(int *xcoord); void scale_ycoord(int *ycoord); void scale_coords(int *ox, int *oy, int *x, int *y); void string_draw_wrapper(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int ox, int oy, int x, int y, - SDL_Rect * update_rect); + SDL_Surface * canvas, SDL_Surface * snapshot, int ox, + int oy, int x, int y, SDL_Rect * update_rect); void string_set_vertex(int x, int y); void compute_middle(int start_point, int end_point, int vertex, int *middle); @@ -50,7 +54,8 @@ void compute_middle(int start_point, int end_point, int vertex, int *middle); // Prototypes for required functions void string_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect); + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect); Uint32 string_api_version(void); @@ -63,13 +68,17 @@ int string_get_group(magic_api * api, int which); char *string_get_description(magic_api * api, int which, int mode); int string_requires_colors(magic_api * api, int which); void string_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); + SDL_Surface * canvas, SDL_Surface * snapshot, int x, + int y, SDL_Rect * update_rect); int string_init(magic_api * api); void string_shutdown(magic_api * api); -void string_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas, SDL_Surface * snapshot); -void string_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas, SDL_Surface * snapshot); -void string_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); +void string_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas, SDL_Surface * snapshot); +void string_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas, SDL_Surface * snapshot); +void string_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect); // Required functions @@ -86,7 +95,8 @@ int string_modes(magic_api * api ATTRIBUTE_UNUSED, int which) return (MODE_PAINT_WITH_PREVIEW); } -void string_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) +void string_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, + Uint8 b) { string_r = r; string_g = g; @@ -105,95 +115,111 @@ SDL_Surface *string_get_icon(magic_api * api, int which) char fname[1024]; switch (which) - { - case STRING_TOOL_FULL_BY_OFFSET: - snprintf(fname, sizeof(fname), "%simages/magic/string_art_full_by_offset.png", api->data_directory); - break; - case STRING_TOOL_TRIANGLE: - snprintf(fname, sizeof(fname), "%simages/magic/string_art_triangles.png", api->data_directory); - break; - case STRING_TOOL_ANGLE: - snprintf(fname, sizeof(fname), "%simages/magic/string_art_angles.png", api->data_directory); - break; - } + { + case STRING_TOOL_FULL_BY_OFFSET: + snprintf(fname, sizeof(fname), + "%simages/magic/string_art_full_by_offset.png", + api->data_directory); + break; + case STRING_TOOL_TRIANGLE: + snprintf(fname, sizeof(fname), "%simages/magic/string_art_triangles.png", + api->data_directory); + break; + case STRING_TOOL_ANGLE: + snprintf(fname, sizeof(fname), "%simages/magic/string_art_angles.png", + api->data_directory); + break; + } return (IMG_Load(fname)); } -char *string_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *string_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { switch (which) - { - case STRING_TOOL_FULL_BY_OFFSET: - return strdup(gettext_noop("String edges")); - break; - case STRING_TOOL_TRIANGLE: - return strdup(gettext_noop("String corner")); - break; - default: - return strdup(gettext_noop("String 'V'")); - } + { + case STRING_TOOL_FULL_BY_OFFSET: + return strdup(gettext_noop("String edges")); + break; + case STRING_TOOL_TRIANGLE: + return strdup(gettext_noop("String corner")); + break; + default: + return strdup(gettext_noop("String 'V'")); + } } -int string_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int string_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_ARTISTIC; } -char *string_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode ATTRIBUTE_UNUSED) +char *string_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, + int mode ATTRIBUTE_UNUSED) { switch (which) - { - case STRING_TOOL_FULL_BY_OFFSET: - return - strdup(gettext_noop - ("Click and drag to draw string art. Drag top-bottom to draw less or more lines, left or right to make a bigger hole.")); - break; - case STRING_TOOL_TRIANGLE: - return strdup(gettext_noop("Click and drag to draw arrows made of string art.")); - break; - default: - return strdup(gettext_noop("Draw string art arrows with free angles.")); - } + { + case STRING_TOOL_FULL_BY_OFFSET: + return + strdup(gettext_noop + ("Click and drag to draw string art. Drag top-bottom to draw less or more lines, left or right to make a bigger hole.")); + break; + case STRING_TOOL_TRIANGLE: + return + strdup(gettext_noop + ("Click and drag to draw arrows made of string art.")); + break; + default: + return strdup(gettext_noop("Draw string art arrows with free angles.")); + } } -int string_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int string_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 1; } void string_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect) + SDL_Surface * canvas, SDL_Surface * snapshot, int x, + int y, SDL_Rect * update_rect) { int dx, dy; if (which == STRING_TOOL_TRIANGLE) - string_draw_triangle((void *)api, which, canvas, snapshot, string_ox, string_oy, x, y, update_rect); + string_draw_triangle((void *) api, which, canvas, snapshot, string_ox, + string_oy, x, y, update_rect); if (which == STRING_TOOL_ANGLE) + { + if (!string_vertex_done) // maybe we face small children, draw square angles aligned to the drag { - if (!string_vertex_done) // maybe we face small children, draw square angles aligned to the drag - { - dx = string_ox - x; - dy = string_oy - y; - y = y + dx; - x = x - dy; - } - string_draw_angle((void *)api, which, canvas, snapshot, string_ox, string_oy, x, y, update_rect); + dx = string_ox - x; + dy = string_oy - y; + y = y + dx; + x = x - dy; } + string_draw_angle((void *) api, which, canvas, snapshot, string_ox, + string_oy, x, y, update_rect); + } } int string_init(magic_api * api ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/string.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/string.ogg", + api->data_directory); string_snd[STRING_TOOL_FULL_BY_OFFSET] = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%ssounds/magic/string2.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/string2.ogg", + api->data_directory); string_snd[STRING_TOOL_TRIANGLE] = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%ssounds/magic/string3.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/string3.ogg", + api->data_directory); string_snd[STRING_TOOL_ANGLE] = Mix_LoadWAV(fname); return (1); @@ -207,21 +233,29 @@ void string_shutdown(magic_api * api ATTRIBUTE_UNUSED) SDL_FreeSurface(canvas_backup); while (i < STRING_NUMTOOLS) - { - if (string_snd[i] != NULL) - Mix_FreeChunk(string_snd[i]); - i++; - } + { + if (string_snd[i] != NULL) + Mix_FreeChunk(string_snd[i]); + i++; + } } -void string_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * snapshot ATTRIBUTE_UNUSED) +void string_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas, + SDL_Surface * snapshot ATTRIBUTE_UNUSED) { - canvas_backup = SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->w, canvas->h, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, - canvas->format->Amask); + canvas_backup = + SDL_CreateRGBSurface(SDL_SWSURFACE, canvas->w, canvas->h, + canvas->format->BitsPerPixel, canvas->format->Rmask, + canvas->format->Gmask, canvas->format->Bmask, + canvas->format->Amask); } -void string_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * snapshot ATTRIBUTE_UNUSED) +void string_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED, + SDL_Surface * snapshot ATTRIBUTE_UNUSED) { SDL_FreeSurface(canvas_backup); canvas_backup = NULL; @@ -230,15 +264,21 @@ void string_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUS // Interactivity functions -void string_callback(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * snapshot ATTRIBUTE_UNUSED, int x, int y) +void string_callback(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, + SDL_Surface * snapshot ATTRIBUTE_UNUSED, int x, int y) { magic_api *api = (magic_api *) ptr; - api->putpixel(canvas, x, y, SDL_MapRGBA(canvas->format, string_r, string_g, string_b, 255)); + api->putpixel(canvas, x, y, + SDL_MapRGBA(canvas->format, string_r, string_g, string_b, + 255)); } -void string_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect) +void string_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect) { SDL_BlitSurface(canvas, NULL, canvas_backup, NULL); @@ -249,8 +289,11 @@ void string_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, SDL_Sur string_drag(api, which, canvas, snapshot, x, y, x, y, update_rect); } -static void string_draw_full_by_offset(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * snapshot ATTRIBUTE_UNUSED, - int x, int y, SDL_Rect * update_rect) +static void string_draw_full_by_offset(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, + SDL_Surface * + snapshot ATTRIBUTE_UNUSED, int x, + int y, SDL_Rect * update_rect) { magic_api *api = (magic_api *) ptr; int u; @@ -260,56 +303,57 @@ static void string_draw_full_by_offset(void *ptr, int which ATTRIBUTE_UNUSED, SD // int n=y/5; int **a; float step_w, step_h, aux; - int side = (int)(y / 3); + int side = (int) (y / 3); SDL_BlitSurface(snapshot, 0, canvas, 0); if (side < 3) side = 3; - o = (int)(side * 4 * x / canvas->w); - step_w = canvas->w / (float)side; - step_h = canvas->h / (float)side; + o = (int) (side * 4 * x / canvas->w); + step_w = canvas->w / (float) side; + step_h = canvas->h / (float) side; a = malloc(sizeof(int *) * side * 4 * 2); for (i = 0; i < side * 4; i++) + { + a[i] = malloc(sizeof(int *) * 2); + if (i < side) { - a[i] = malloc(sizeof(int *) * 2); - if (i < side) - { - a[i][0] = 0; - aux = step_h * (float)i; - a[i][1] = (int)aux; - } - else if (i < (side * 2)) - { - a[i][0] = (int)((float)(i % side) * step_w); - a[i][1] = canvas->h; - } - else if (i < (int)(side * 3)) - { - a[i][0] = canvas->w; - a[i][1] = (int)(canvas->h - (float)((i % side) * step_h)); - } - else if (i < (int)(side * 4)) - { - a[i][0] = (int)(canvas->w - ((float)((i % side) * step_w))); - a[i][1] = 0; - } + a[i][0] = 0; + aux = step_h * (float) i; + a[i][1] = (int) aux; } + else if (i < (side * 2)) + { + a[i][0] = (int) ((float) (i % side) * step_w); + a[i][1] = canvas->h; + } + else if (i < (int) (side * 3)) + { + a[i][0] = canvas->w; + a[i][1] = (int) (canvas->h - (float) ((i % side) * step_h)); + } + else if (i < (int) (side * 4)) + { + a[i][0] = (int) (canvas->w - ((float) ((i % side) * step_w))); + a[i][1] = 0; + } + } for (i = 0; i < side * 4; i++) - { - u = (i + o) % (side * 4); - api->line((void *)api, which, canvas, snapshot, a[i][0], a[i][1], a[u][0], a[u][1], 1, string_callback); - } + { + u = (i + o) % (side * 4); + api->line((void *) api, which, canvas, snapshot, a[i][0], a[i][1], + a[u][0], a[u][1], 1, string_callback); + } for (i = 0; i < side * 4; i++) - { - free(a[i]); - } + { + free(a[i]); + } free(a); update_rect->x = 0; @@ -344,13 +388,18 @@ void scale_coords(int *ox, int *oy, int *x, int *y) void compute_middle(int start_point, int end_point, int vertex, int *middle) { - *middle = min(start_point, end_point) + (max(start_point, end_point) - min(start_point, end_point)) / 2; - *middle = min(*middle, vertex) + (max(*middle, vertex) - min(*middle, vertex)) / 2; + *middle = + min(start_point, + end_point) + (max(start_point, end_point) - min(start_point, + end_point)) / 2; + *middle = + min(*middle, vertex) + (max(*middle, vertex) - min(*middle, vertex)) / 2; } void string_draw_triangle_preview(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int ox, int oy, int x, int y, - SDL_Rect * update_rect) + SDL_Surface * canvas, + SDL_Surface * snapshot, int ox, int oy, + int x, int y, SDL_Rect * update_rect) { int middle_x, middle_y; @@ -365,14 +414,21 @@ void string_draw_triangle_preview(magic_api * api, int which, compute_middle(x, string_ox, string_ox, &middle_x); compute_middle(y, string_oy, string_oy, &middle_y); - api->line((void *)api, which, canvas, snapshot, string_ox, string_oy, string_ox, y, 1, string_callback); - api->line((void *)api, which, canvas, snapshot, string_ox, string_oy, x, string_oy, 1, string_callback); - api->line((void *)api, which, canvas, snapshot, middle_x, middle_y, x, string_oy, 1, string_callback); - api->line((void *)api, which, canvas, snapshot, string_ox, y, middle_x, middle_y, 1, string_callback); + api->line((void *) api, which, canvas, snapshot, string_ox, string_oy, + string_ox, y, 1, string_callback); + api->line((void *) api, which, canvas, snapshot, string_ox, string_oy, x, + string_oy, 1, string_callback); + api->line((void *) api, which, canvas, snapshot, middle_x, middle_y, x, + string_oy, 1, string_callback); + api->line((void *) api, which, canvas, snapshot, string_ox, y, middle_x, + middle_y, 1, string_callback); } void string_draw_angle_preview(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, int x, int y, SDL_Rect * update_rect) + SDL_Surface * canvas, SDL_Surface * snapshot, + int ox ATTRIBUTE_UNUSED, + int oy ATTRIBUTE_UNUSED, int x, int y, + SDL_Rect * update_rect) { int middle_x, middle_y; int dx, dy; @@ -383,31 +439,38 @@ void string_draw_angle_preview(magic_api * api, int which, update_rect->h = canvas->h; SDL_BlitSurface(canvas_backup, update_rect, canvas, update_rect); - api->line((void *)api, which, canvas, snapshot, string_ox, string_oy, string_vertex_x, string_vertex_y, 1, - string_callback); + api->line((void *) api, which, canvas, snapshot, string_ox, string_oy, + string_vertex_x, string_vertex_y, 1, string_callback); if (!string_vertex_done) - { - // if(!string_vertex_done) // maybe we face small children, draw square angles aligned to the drag - //{ - dx = string_ox - x; - dy = string_oy - y; - y = y + dx; - x = x - dy; - } + { + // if(!string_vertex_done) // maybe we face small children, draw square angles aligned to the drag + //{ + dx = string_ox - x; + dy = string_oy - y; + y = y + dx; + x = x - dy; + } compute_middle(string_ox, x, string_vertex_x, &middle_x); compute_middle(string_oy, y, string_vertex_y, &middle_y); - api->line((void *)api, which, canvas, snapshot, string_vertex_x, string_vertex_y, x, y, 1, string_callback); - api->line((void *)api, which, canvas, snapshot, string_ox, string_oy, middle_x, middle_y, 1, string_callback); - api->line((void *)api, which, canvas, snapshot, x, y, middle_x, middle_y, 1, string_callback); + api->line((void *) api, which, canvas, snapshot, string_vertex_x, + string_vertex_y, x, y, 1, string_callback); + api->line((void *) api, which, canvas, snapshot, string_ox, string_oy, + middle_x, middle_y, 1, string_callback); + api->line((void *) api, which, canvas, snapshot, x, y, middle_x, middle_y, + 1, string_callback); } void string_draw_angle(magic_api * api, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * snapshot ATTRIBUTE_UNUSED, int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, int x, int y, SDL_Rect * update_rect) + SDL_Surface * canvas, + SDL_Surface * snapshot ATTRIBUTE_UNUSED, + int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, + int x, int y, SDL_Rect * update_rect) { - float first_arm_step_x, first_arm_step_y, second_arm_step_x, second_arm_step_y; + float first_arm_step_x, first_arm_step_y, second_arm_step_x, + second_arm_step_y; int i; int max_wh, steps; int max_separation = 10; @@ -419,24 +482,30 @@ void string_draw_angle(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_BlitSurface(canvas_backup, update_rect, canvas, update_rect); max_wh = - max(max(max(string_ox, string_vertex_x), x) - min(min(string_vertex_x, x), string_ox), - max(max(string_oy, string_vertex_y), y) - min(min(string_vertex_y, y), string_oy)); + max(max(max(string_ox, string_vertex_x), x) - + min(min(string_vertex_x, x), string_ox), + max(max(string_oy, string_vertex_y), y) - min(min(string_vertex_y, y), + string_oy)); steps = max_wh / max_separation; - first_arm_step_x = (float)(string_ox - string_vertex_x) / (float)steps; - first_arm_step_y = (float)(string_oy - string_vertex_y) / (float)steps; - second_arm_step_x = (float)(string_vertex_x - x) / (float)steps; - second_arm_step_y = (float)(string_vertex_y - y) / (float)steps; + first_arm_step_x = (float) (string_ox - string_vertex_x) / (float) steps; + first_arm_step_y = (float) (string_oy - string_vertex_y) / (float) steps; + second_arm_step_x = (float) (string_vertex_x - x) / (float) steps; + second_arm_step_y = (float) (string_vertex_y - y) / (float) steps; for (i = 0; i <= steps; i++) - { - api->line((void *)api, 0, canvas, snapshot, string_ox - first_arm_step_x * i, string_oy - first_arm_step_y * i, - string_vertex_x - second_arm_step_x * i, string_vertex_y - second_arm_step_y * i, 1, string_callback); - } + { + api->line((void *) api, 0, canvas, snapshot, + string_ox - first_arm_step_x * i, + string_oy - first_arm_step_y * i, + string_vertex_x - second_arm_step_x * i, + string_vertex_y - second_arm_step_y * i, 1, string_callback); + } } void string_draw_triangle(magic_api * api, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Surface * canvas, SDL_Surface * snapshot, + int ox, int oy, int x, int y, SDL_Rect * update_rect) { SDL_BlitSurface(canvas_backup, 0, canvas, 0); @@ -448,19 +517,23 @@ void string_draw_triangle(magic_api * api, int which ATTRIBUTE_UNUSED, string_oy = y; y = string_vertex_y; - string_draw_angle((void *)api, which, canvas, snapshot, string_ox, string_oy, x, y, update_rect); + string_draw_angle((void *) api, which, canvas, snapshot, string_ox, + string_oy, x, y, update_rect); } void string_draw_wrapper(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int ox, int oy, int x, int y, - SDL_Rect * update_rect) + SDL_Surface * canvas, SDL_Surface * snapshot, int ox, + int oy, int x, int y, SDL_Rect * update_rect) { if (which == STRING_TOOL_FULL_BY_OFFSET) - string_draw_full_by_offset((void *)api, which, canvas, snapshot, x, y, update_rect); + string_draw_full_by_offset((void *) api, which, canvas, snapshot, x, y, + update_rect); else if (which == STRING_TOOL_TRIANGLE) - string_draw_triangle_preview((void *)api, which, canvas, snapshot, ox, oy, x, y, update_rect); + string_draw_triangle_preview((void *) api, which, canvas, snapshot, ox, + oy, x, y, update_rect); else if (which == STRING_TOOL_ANGLE) - string_draw_angle_preview((void *)api, which, canvas, snapshot, ox, oy, x, y, update_rect); + string_draw_angle_preview((void *) api, which, canvas, snapshot, ox, oy, + x, y, update_rect); } void string_set_vertex(int x, int y) @@ -472,24 +545,27 @@ void string_set_vertex(int x, int y) dx = max(string_ox, x) - min(string_ox, x); dy = max(string_oy, y) - min(string_oy, y); if (dx + dy > string_vertex_distance) - { - string_vertex_distance = dx + dy; - string_vertex_x = x; - string_vertex_y = y; - } + { + string_vertex_distance = dx + dy; + string_vertex_x = x; + string_vertex_y = y; + } if (dx + dy + 30 < string_vertex_distance) string_vertex_done = 1; } void string_drag(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect) + SDL_Surface * canvas, SDL_Surface * snapshot, int ox, int oy, + int x, int y, SDL_Rect * update_rect) { - if ((x < canvas->w) && (y < canvas->h) && (ox < canvas->w) && (oy < canvas->h) && ((signed)x > 0) && ((signed)y > 0) - && ((signed)ox > 0) && ((signed)oy > 0)) - { - string_set_vertex(x, y); - string_draw_wrapper((void *)api, which, canvas, snapshot, ox, oy, x, y, update_rect); - api->playsound(string_snd[which], (x * 255) / canvas->w, 255); + if ((x < canvas->w) && (y < canvas->h) && (ox < canvas->w) + && (oy < canvas->h) && ((signed) x > 0) && ((signed) y > 0) + && ((signed) ox > 0) && ((signed) oy > 0)) + { + string_set_vertex(x, y); + string_draw_wrapper((void *) api, which, canvas, snapshot, ox, oy, x, y, + update_rect); + api->playsound(string_snd[which], (x * 255) / canvas->w, 255); - } + } } diff --git a/magic/src/tint.c b/magic/src/tint.c index 927ba7616..ee3df9db3 100644 --- a/magic/src/tint.c +++ b/magic/src/tint.c @@ -76,10 +76,13 @@ const char *tint_names[tint_NUM_TOOLS] = { }; const char *tint_descs[tint_NUM_TOOLS][2] = { - {gettext_noop("Click and drag the mouse around to change the color of parts of your picture."), + {gettext_noop + ("Click and drag the mouse around to change the color of parts of your picture."), gettext_noop("Click to change the color of your entire picture."),}, - {gettext_noop("Click and drag the mouse around to turn parts of your picture into white and a color you choose."), - gettext_noop("Click to turn your entire picture into white and a color you choose.")} + {gettext_noop + ("Click and drag the mouse around to turn parts of your picture into white and a color you choose."), + gettext_noop + ("Click to turn your entire picture into white and a color you choose.")} }; int tint_init(magic_api * api); @@ -90,20 +93,26 @@ char *tint_get_name(magic_api * api, int which); int tint_get_group(magic_api * api, int which); char *tint_get_description(magic_api * api, int which, int mode); static int tint_grey(Uint8 r1, Uint8 g1, Uint8 b1); -static void do_tint_pixel(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); -static void do_tint_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int which); -static void do_tint_brush(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +static void do_tint_pixel(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); +static void do_tint_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, + int which); +static void do_tint_brush(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); void tint_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void tint_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void tint_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void tint_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); +void tint_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); void tint_shutdown(magic_api * api); void tint_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int tint_requires_colors(magic_api * api, int which); -void tint_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void tint_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void tint_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void tint_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int tint_modes(magic_api * api, int which); Uint32 tint_api_version(void) @@ -118,10 +127,11 @@ int tint_init(magic_api * api) char fname[1024]; for (i = 0; i < tint_NUM_TOOLS; i++) - { - snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, tint_snd_filenames[i]); - tint_snd_effect[i] = Mix_LoadWAV(fname); - } + { + snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, + tint_snd_filenames[i]); + tint_snd_effect[i] = Mix_LoadWAV(fname); + } return (1); } @@ -135,7 +145,8 @@ SDL_Surface *tint_get_icon(magic_api * api, int which) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, tint_icon_filenames[which]); + snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, + tint_icon_filenames[which]); return (IMG_Load(fname)); } @@ -146,13 +157,15 @@ char *tint_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) } // Return our group (both the same): -int tint_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int tint_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_COLOR_FILTERS; } // Return our descriptions, localized: -char *tint_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode) +char *tint_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, + int mode) { return (strdup(gettext_noop(tint_descs[which][mode - 1]))); } @@ -163,7 +176,8 @@ static int tint_grey(Uint8 r1, Uint8 g1, Uint8 b1) return 0.3 * r1 + .59 * g1 + 0.11 * b1; } -static void do_tint_pixel(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void do_tint_pixel(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y) { magic_api *api = (magic_api *) ptr; @@ -176,81 +190,88 @@ static void do_tint_pixel(void *ptr, int which, SDL_Surface * canvas, SDL_Surfac int greyValue = tint_grey(r, g, b); if (which == TOOL_TINT) - { - api->rgbtohsv(tint_r, tint_g, tint_b, &h, &s, &v); - api->hsvtorgb(h, s, greyValue / 255.0, &r, &g, &b); - api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, r, g, b)); - } + { + api->rgbtohsv(tint_r, tint_g, tint_b, &h, &s, &v); + api->hsvtorgb(h, s, greyValue / 255.0, &r, &g, &b); + api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, r, g, b)); + } else if (which == TOOL_THRESHOLD) - { - int thresholdValue = (tint_max - tint_min) / 2; + { + int thresholdValue = (tint_max - tint_min) / 2; - if (greyValue < thresholdValue) - { - api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, tint_r, tint_g, tint_b)); - } - else - { - api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, 255, 255, 255)); - } + if (greyValue < thresholdValue) + { + api->putpixel(canvas, x, y, + SDL_MapRGB(canvas->format, tint_r, tint_g, tint_b)); } + else + { + api->putpixel(canvas, x, y, + SDL_MapRGB(canvas->format, 255, 255, 255)); + } + } } } // Do the effect: -static void do_tint_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int which) +static void do_tint_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, + int which) { int x, y; for (y = 0; y < last->h; y++) + { + for (x = 0; x < last->w; x++) { - for (x = 0; x < last->w; x++) - { - do_tint_pixel(ptr, which, canvas, last, x, y); - } + do_tint_pixel(ptr, which, canvas, last, x, y); } + } } -static void do_tint_brush(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y) +static void do_tint_brush(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y) { int xx, yy; magic_api *api = (magic_api *) ptr; for (yy = y - tint_RADIUS; yy < y + tint_RADIUS; yy++) + { + for (xx = x - tint_RADIUS; xx < x + tint_RADIUS; xx++) { - for (xx = x - tint_RADIUS; xx < x + tint_RADIUS; xx++) - { - if (api->in_circle(xx - x, yy - y, tint_RADIUS) && !api->touched(xx, yy)) - { - do_tint_pixel(api, which, canvas, last, xx, yy); - } - } + if (api->in_circle(xx - x, yy - y, tint_RADIUS) + && !api->touched(xx, yy)) + { + do_tint_pixel(api, which, canvas, last, xx, yy); + } } + } } // Affect the canvas on drag: void tint_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) { - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_tint_brush); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, + do_tint_brush); api->playsound(tint_snd_effect[which], (x * 255) / canvas->w, 255); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = ox - tint_RADIUS; update_rect->y = oy - tint_RADIUS; @@ -260,25 +281,29 @@ void tint_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void tint_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) { if (mode == MODE_PAINT) tint_drag(api, which, canvas, last, x, y, x, y, update_rect); else - { - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; - do_tint_full(api, canvas, last, which); - api->playsound(tint_snd_effect[which], 128, 255); - } + { + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; + do_tint_full(api, canvas, last, which); + api->playsound(tint_snd_effect[which], 128, 255); + } } // Affect the canvas on release: -void tint_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) +void tint_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) { } @@ -289,16 +314,17 @@ void tint_shutdown(magic_api * api ATTRIBUTE_UNUSED) int i; for (i = 0; i < tint_NUM_TOOLS; i++) + { + if (tint_snd_effect[i] != NULL) { - if (tint_snd_effect[i] != NULL) - { - Mix_FreeChunk(tint_snd_effect[i]); - } + Mix_FreeChunk(tint_snd_effect[i]); } + } } // Record the color from Tux Paint: -void tint_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) +void tint_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, + Uint8 b) { tint_r = r; tint_g = g; @@ -306,39 +332,42 @@ void tint_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) } // Use colors: -int tint_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int tint_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 1; } -void tint_switchin(magic_api * api, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas) +void tint_switchin(magic_api * api, int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas) { int x, y; Uint8 r1, g1, b1; for (y = 0; y < canvas->h; y++) + { + for (x = 0; x < canvas->w; x++) { - for (x = 0; x < canvas->w; x++) - { - SDL_GetRGB(api->getpixel(canvas, x, y), canvas->format, &r1, &g1, &b1); - { - int greyValue = tint_grey(r1, g1, b1); + SDL_GetRGB(api->getpixel(canvas, x, y), canvas->format, &r1, &g1, &b1); + { + int greyValue = tint_grey(r1, g1, b1); - if (greyValue < tint_min) - { - tint_min = greyValue; - } - if (greyValue > tint_max) - { - tint_max = greyValue; - } - } + if (greyValue < tint_min) + { + tint_min = greyValue; } + if (greyValue > tint_max) + { + tint_max = greyValue; + } + } } + } } -void tint_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void tint_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } diff --git a/magic/src/toothpaste.c b/magic/src/toothpaste.c index a86438110..23dd66d64 100644 --- a/magic/src/toothpaste.c +++ b/magic/src/toothpaste.c @@ -84,18 +84,24 @@ SDL_Surface *toothpaste_get_icon(magic_api * api, int which); char *toothpaste_get_name(magic_api * api, int which); int toothpaste_get_group(magic_api * api, int which); char *toothpaste_get_description(magic_api * api, int which, int mode); -static void do_toothpaste(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); +static void do_toothpaste(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y); void toothpaste_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 toothpaste_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void toothpaste_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 toothpaste_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, + SDL_Rect * update_rect); void toothpaste_shutdown(magic_api * api); void toothpaste_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int toothpaste_requires_colors(magic_api * api, int which); -void toothpaste_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void toothpaste_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void toothpaste_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void toothpaste_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int toothpaste_modes(magic_api * api, int which); Uint32 toothpaste_api_version(void) @@ -113,30 +119,36 @@ int toothpaste_init(magic_api * api) //Load sounds for (i = 0; i < toothpaste_NUM_TOOLS; i++) - { - snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, toothpaste_snd_filenames[i]); - toothpaste_snd_effect[i] = Mix_LoadWAV(fname); - } + { + snprintf(fname, sizeof(fname), "%ssounds/magic/%s", api->data_directory, + toothpaste_snd_filenames[i]); + toothpaste_snd_effect[i] = Mix_LoadWAV(fname); + } //Set up weights pi = acos(0.0) * 2; - toothpaste_weights = (double *)malloc(toothpaste_RADIUS * 2 * toothpaste_RADIUS * 2 * sizeof(double)); + toothpaste_weights = + (double *) malloc(toothpaste_RADIUS * 2 * toothpaste_RADIUS * 2 * + sizeof(double)); if (toothpaste_weights == NULL) - { - return (0); - } + { + return (0); + } for (k = -toothpaste_RADIUS; k < +toothpaste_RADIUS; k++) + { + for (j = -toothpaste_RADIUS; j < +toothpaste_RADIUS; j++) { - for (j = -toothpaste_RADIUS; j < +toothpaste_RADIUS; j++) - { - if (api->in_circle(j, k, toothpaste_RADIUS)) - { - toothpaste_weights[(k + toothpaste_RADIUS) * ((toothpaste_RADIUS * 2) - 1) + (j + toothpaste_RADIUS)] = - ((fabs(atan2((double)(j), (double)(k)))) / pi); - } - } + if (api->in_circle(j, k, toothpaste_RADIUS)) + { + toothpaste_weights[(k + + toothpaste_RADIUS) * ((toothpaste_RADIUS * 2) - + 1) + (j + + toothpaste_RADIUS)] = + ((fabs(atan2((double) (j), (double) (k)))) / pi); + } } + } return (1); } @@ -151,7 +163,8 @@ SDL_Surface *toothpaste_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, toothpaste_icon_filenames[which]); + snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, + toothpaste_icon_filenames[which]); return (IMG_Load(fname)); } @@ -168,13 +181,15 @@ int toothpaste_get_group(magic_api * api ATTRIBUTE_UNUSED, int which) } // Return our descriptions, localized: -char *toothpaste_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode ATTRIBUTE_UNUSED) +char *toothpaste_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, + int mode ATTRIBUTE_UNUSED) { return (strdup(gettext_noop(toothpaste_descs[which]))); } // Do the effect: -static void do_toothpaste(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, +static void do_toothpaste(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { magic_api *api = (magic_api *) ptr; @@ -186,30 +201,37 @@ static void do_toothpaste(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * c Uint8 r, g, b; for (yy = y - toothpaste_RADIUS; yy < y + toothpaste_RADIUS; yy++) + { + for (xx = x - toothpaste_RADIUS; xx < x + toothpaste_RADIUS; xx++) { - for (xx = x - toothpaste_RADIUS; xx < x + toothpaste_RADIUS; xx++) - { - if (api->in_circle(xx - x, yy - y, toothpaste_RADIUS) && !api->touched(xx, yy)) - { + if (api->in_circle(xx - x, yy - y, toothpaste_RADIUS) + && !api->touched(xx, yy)) + { - api->rgbtohsv(toothpaste_r, toothpaste_g, toothpaste_b, &h, &s, &v); - api->hsvtorgb(h, s, - toothpaste_weights[(yy - y + toothpaste_RADIUS) * ((toothpaste_RADIUS * 2) - 1) + - (xx - x + toothpaste_RADIUS)], &r, &g, &b); - api->putpixel(canvas, xx, yy, SDL_MapRGB(canvas->format, r, g, b)); + api->rgbtohsv(toothpaste_r, toothpaste_g, toothpaste_b, &h, &s, &v); + api->hsvtorgb(h, s, + toothpaste_weights[(yy - y + + toothpaste_RADIUS) * + ((toothpaste_RADIUS * 2) - 1) + (xx - + x + + toothpaste_RADIUS)], + &r, &g, &b); + api->putpixel(canvas, xx, yy, SDL_MapRGB(canvas->format, r, g, b)); - } - } + } } + } } // Affect the canvas on drag: void toothpaste_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) { - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_toothpaste); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, + do_toothpaste); api->playsound(toothpaste_snd_effect[which], (x * 255) / canvas->w, 255); @@ -222,16 +244,20 @@ void toothpaste_drag(magic_api * api, int which, SDL_Surface * canvas, // Affect the canvas on click: void toothpaste_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) { toothpaste_drag(api, which, canvas, last, x, y, x, y, update_rect); } // Affect the canvas on release: -void toothpaste_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) +void toothpaste_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) { } @@ -242,21 +268,22 @@ void toothpaste_shutdown(magic_api * api ATTRIBUTE_UNUSED) int i; for (i = 0; i < toothpaste_NUM_TOOLS; i++) + { + if (toothpaste_snd_effect[i] != NULL) { - if (toothpaste_snd_effect[i] != NULL) - { - Mix_FreeChunk(toothpaste_snd_effect[i]); - } + Mix_FreeChunk(toothpaste_snd_effect[i]); } + } if (toothpaste_weights != NULL) - { - free(toothpaste_weights); - toothpaste_weights = NULL; - } + { + free(toothpaste_weights); + toothpaste_weights = NULL; + } } // Record the color from Tux Paint: -void toothpaste_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) +void toothpaste_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, + Uint8 b) { toothpaste_r = r; toothpaste_g = g; @@ -264,23 +291,29 @@ void toothpaste_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Ui } // Use colors: -int toothpaste_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int toothpaste_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 1; } -void toothpaste_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void toothpaste_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void toothpaste_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void toothpaste_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -int toothpaste_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int toothpaste_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_PAINT); } diff --git a/magic/src/tornado.c b/magic/src/tornado.c index 53458740e..4f8d96b19 100644 --- a/magic/src/tornado.c +++ b/magic/src/tornado.c @@ -64,13 +64,18 @@ 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); -static void tornado_drawtornado(magic_api * api, SDL_Surface * canvas, int x, int y); +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); +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 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); @@ -88,17 +93,21 @@ 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); -void tornado_click(magic_api * api, int which, int mode, - 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 * 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); +void tornado_release(magic_api * api, int which, 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); int tornado_requires_colors(magic_api * api, int which); -void tornado_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void tornado_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void tornado_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void tornado_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int tornado_modes(magic_api * api, int which); @@ -120,13 +129,16 @@ int tornado_init(magic_api * api) tornado_click_snd = Mix_LoadWAV(fname); */ - snprintf(fname, sizeof(fname), "%ssounds/magic/tornado_release.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/tornado_release.ogg", + api->data_directory); tornado_release_snd = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%simages/magic/tornado_base.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/tornado_base.png", + api->data_directory); tornado_base = IMG_Load(fname); - snprintf(fname, sizeof(fname), "%simages/magic/tornado_cloud.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/tornado_cloud.png", + api->data_directory); tornado_cloud = IMG_Load(fname); return (1); @@ -143,32 +155,41 @@ SDL_Surface *tornado_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/tornado.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/tornado.png", + api->data_directory); 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 our groups: -int tornado_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int tornado_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_ARTISTIC; } // 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) +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) { if (x < tornado_min_x) tornado_min_x = x; @@ -187,22 +208,23 @@ 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) { - 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; - } + 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; + } + } } -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) +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) { tornado_predrag(api, canvas, last, ox, oy, x, y); @@ -215,7 +237,8 @@ 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); @@ -227,7 +250,8 @@ void tornado_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * can // 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; @@ -246,7 +270,8 @@ 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: */ @@ -266,7 +291,8 @@ 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); @@ -282,7 +308,8 @@ void tornado_release(magic_api * api, int which ATTRIBUTE_UNUSED, } -static void tornado_drawtornado(magic_api * api, SDL_Surface * canvas, int x, int y) +static void tornado_drawtornado(magic_api * api, SDL_Surface * canvas, int x, + int y) { SDL_Surface *aux_surf; SDL_Rect dest; @@ -295,7 +322,8 @@ static void tornado_drawtornado(magic_api * api, SDL_Surface * canvas, int x, in SDL_FreeSurface(aux_surf); } -static void tornado_drawbase(magic_api * api ATTRIBUTE_UNUSED, SDL_Surface * canvas) +static void tornado_drawbase(magic_api * api ATTRIBUTE_UNUSED, + SDL_Surface * canvas) { SDL_Rect dest; @@ -308,16 +336,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_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) +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) { Point2D control_points[4]; Point2D *curve; @@ -335,15 +366,15 @@ 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; @@ -367,58 +398,68 @@ static void tornado_drawstalk(magic_api * api, SDL_Surface * canvas, SDL_Surface /* Draw the curve: */ for (i = 0; i < n_points - 1; i++) + { + if (final == 0) { - 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; - 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; - - 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)); - } + 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; + 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; + + 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)); + } + } free(curve); } @@ -452,7 +493,8 @@ void tornado_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b) } // Use colors: -int tornado_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int tornado_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 1; } @@ -507,7 +549,8 @@ static Point2D tornado_PointOnCubicBezier(Point2D * cp, float t) */ -static void tornado_ComputeBezier(Point2D * cp, int numberOfPoints, Point2D * curve) +static void tornado_ComputeBezier(Point2D * cp, int numberOfPoints, + Point2D * curve) { float dt; int i; @@ -530,7 +573,9 @@ 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, @@ -538,7 +583,8 @@ 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: */ @@ -546,33 +592,39 @@ 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++) { - for (x = 0; x < tornado_cloud->w; x++) - { - SDL_GetRGBA(api->getpixel(tornado_cloud, x, y), tornado_cloud->format, &r, &g, &b, &a); + 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, +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, +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) +int tornado_modes(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (MODE_PAINT_WITH_PREVIEW); } diff --git a/magic/src/tv.c b/magic/src/tv.c index cf8c6a07a..35670144c 100644 --- a/magic/src/tv.c +++ b/magic/src/tv.c @@ -46,14 +46,18 @@ int tv_get_group(magic_api * api, int which); char *tv_get_description(magic_api * api, int which, int mode); int tv_requires_colors(magic_api * api, int which); void tv_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect); + SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, + SDL_Rect * update_rect); void tv_shutdown(magic_api * api); -void tv_paint_tv(void *ptr_to_api, int which_tool, SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y); -void tv_do_tv(void *ptr_to_api, int which_tool, SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y); +void tv_paint_tv(void *ptr_to_api, int which_tool, SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y); +void tv_do_tv(void *ptr_to_api, int which_tool, SDL_Surface * canvas, + SDL_Surface * snapshot, int x, int y); void tv_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void tv_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void tv_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); void tv_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); void tv_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); int tv_modes(magic_api * api, int which); @@ -74,7 +78,8 @@ int tv_init(magic_api * api ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/tv.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/tv.ogg", + api->data_directory); tv_snd = Mix_LoadWAV(fname); return (1); @@ -89,12 +94,14 @@ SDL_Surface *tv_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/tv.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/tv.png", + api->data_directory); return (IMG_Load(fname)); } -char *tv_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *tv_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return strdup(gettext_noop("TV")); } @@ -104,24 +111,32 @@ int tv_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) return MAGIC_TYPE_DISTORTS; } -char *tv_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode) +char *tv_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode) { if (mode == MODE_PAINT) - return strdup(gettext_noop("Click and drag to make parts of your picture look like they are on television.")); + return + strdup(gettext_noop + ("Click and drag to make parts of your picture look like they are on television.")); else - return strdup(gettext_noop("Click to make your picture look like it's on television.")); + return + strdup(gettext_noop + ("Click to make your picture look like it's on television.")); } -int tv_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int tv_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } void tv_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * snapshot ATTRIBUTE_UNUSED, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) + SDL_Surface * canvas ATTRIBUTE_UNUSED, + SDL_Surface * snapshot ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, + SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -133,64 +148,68 @@ void tv_shutdown(magic_api * api ATTRIBUTE_UNUSED) // Interactivity functions void tv_do_tv(void *ptr_to_api, int which_tool ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * snapshot ATTRIBUTE_UNUSED, int x, int y) + SDL_Surface * canvas, SDL_Surface * snapshot ATTRIBUTE_UNUSED, + int x, int y) { magic_api *api = (magic_api *) ptr_to_api; Uint8 r, g, b, i; for (i = 0; i < 2; i++) + { + /* Convert the line below to their red/green/blue elements */ + SDL_GetRGB(api->getpixel(snapshot, x, y + i), snapshot->format, &r, &g, + &b); + if (x % 3 == 0) { - /* Convert the line below to their red/green/blue elements */ - SDL_GetRGB(api->getpixel(snapshot, x, y + i), snapshot->format, &r, &g, &b); - if (x % 3 == 0) - { - /* Red */ - g = 0; - b = 0; - } - else if (x % 3 == 1) - { - /* Green */ - r = 0; - b = 0; - } - else - { - /* Blue */ - r = 0; - g = 0; - } - - r = r / (i + 1); - g = g / (i + 1); - b = b / (i + 1); - - api->putpixel(canvas, x, y + i, SDL_MapRGB(canvas->format, r, g, b)); + /* Red */ + g = 0; + b = 0; } + else if (x % 3 == 1) + { + /* Green */ + r = 0; + b = 0; + } + else + { + /* Blue */ + r = 0; + g = 0; + } + + r = r / (i + 1); + g = g / (i + 1); + b = b / (i + 1); + + api->putpixel(canvas, x, y + i, SDL_MapRGB(canvas->format, r, g, b)); + } } void tv_paint_tv(void *ptr_to_api, int which_tool ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * snapshot ATTRIBUTE_UNUSED, int x, int y) + SDL_Surface * canvas, + SDL_Surface * snapshot ATTRIBUTE_UNUSED, int x, int y) { int i, j; magic_api *api = (magic_api *) ptr_to_api; - y = (y - (y % 2)); + y = (y - (y % 2)); for (i = x - RADIUS; i < x + RADIUS; i++) + { + for (j = y - RADIUS; j < y + RADIUS; j += 2) { - for (j = y - RADIUS; j < y + RADIUS; j += 2) - { - if (api->in_circle(i - x, j - y, RADIUS) && !api->touched(i, j)) - { - tv_do_tv(api, 0, canvas, snapshot, i, j); - } - } + if (api->in_circle(i - x, j - y, RADIUS) && !api->touched(i, j)) + { + tv_do_tv(api, 0, canvas, snapshot, i, j); + } } + } } void tv_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect) + SDL_Surface * snapshot, int ox, int oy, int x, int y, + SDL_Rect * update_rect) { api->line(api, which, canvas, snapshot, ox, oy, x, y, 1, tv_paint_tv); @@ -202,36 +221,39 @@ void tv_drag(magic_api * api, int which, SDL_Surface * canvas, } void tv_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) { if (mode == MODE_FULLSCREEN) + { + for (y = 0; y < canvas->h; y += 2) { - for (y = 0; y < canvas->h; y += 2) - { - for (x = 0; x < canvas->w; x++) - { - tv_do_tv(api, which, canvas, last, x, y); - } - } + for (x = 0; x < canvas->w; x++) + { + tv_do_tv(api, which, canvas, last, x, y); + } + } - update_rect->w = canvas->w; - update_rect->h = canvas->h; - update_rect->x = update_rect->y = 0; - api->playsound(tv_snd, 128, 255); - } + update_rect->w = canvas->w; + update_rect->h = canvas->h; + update_rect->x = update_rect->y = 0; + api->playsound(tv_snd, 128, 255); + } else - { - tv_drag(api, which, canvas, last, x, y, x, y, update_rect); - } + { + tv_drag(api, which, canvas, last, x, y, x, y, update_rect); + } } -void tv_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void tv_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void tv_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void tv_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { diff --git a/magic/src/waves.c b/magic/src/waves.c index 91d50b795..209baba8d 100644 --- a/magic/src/waves.c +++ b/magic/src/waves.c @@ -46,16 +46,19 @@ char *waves_get_name(magic_api * api, int which); int waves_get_group(magic_api * api, int which); char *waves_get_description(magic_api * api, int which, int mode); void waves_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); -void waves_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); -void waves_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); + SDL_Surface * last, int ox, int oy, int x, int y, + SDL_Rect * update_rect); +void waves_click(magic_api * api, int which, int mode, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); +void waves_release(magic_api * api, int which, SDL_Surface * canvas, + SDL_Surface * last, int x, int y, SDL_Rect * update_rect); void waves_shutdown(magic_api * api); void waves_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int waves_requires_colors(magic_api * api, int which); -void waves_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void waves_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void waves_switchin(magic_api * api, int which, int mode, + SDL_Surface * canvas); +void waves_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int waves_modes(magic_api * api, int which); Uint32 waves_api_version(void) @@ -69,10 +72,12 @@ int waves_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/waves.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/waves.ogg", + api->data_directory); waves_snd[0] = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%ssounds/magic/wavelet.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/wavelet.ogg", + api->data_directory); waves_snd[1] = Mix_LoadWAV(fname); @@ -91,15 +96,18 @@ SDL_Surface *waves_get_icon(magic_api * api, int which) char fname[1024]; if (!which) - snprintf(fname, sizeof(fname), "%simages/magic/waves.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/waves.png", + api->data_directory); else - snprintf(fname, sizeof(fname), "%simages/magic/wavelet.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/wavelet.png", + api->data_directory); return (IMG_Load(fname)); } // Return our group (both the same): -int waves_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int waves_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_PICTURE_WARPS; } @@ -114,7 +122,8 @@ char *waves_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) } // Return our descriptions, localized: -char *waves_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode ATTRIBUTE_UNUSED) +char *waves_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, + int mode ATTRIBUTE_UNUSED) { if (!which) return (strdup @@ -126,9 +135,10 @@ char *waves_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mod } -void waves_drag(magic_api * api ATTRIBUTE_UNUSED, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, int x, int y, - SDL_Rect * update_rect) +void waves_drag(magic_api * api ATTRIBUTE_UNUSED, int which, + SDL_Surface * canvas, SDL_Surface * last, + int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, int x, + int y, SDL_Rect * update_rect) { int xx, yy; SDL_Rect src, dest; @@ -138,46 +148,46 @@ void waves_drag(magic_api * api ATTRIBUTE_UNUSED, int which, SDL_Surface * canva SDL_BlitSurface(last, NULL, canvas, NULL); if (which == 0) + { + //waves effect + width = ((x * 10) / canvas->w) + 10; + height = ((canvas->h - y) / 10) + 1; + + for (yy = 0; yy < canvas->h; yy++) { - //waves effect - width = ((x * 10) / canvas->w) + 10; - height = ((canvas->h - y) / 10) + 1; + xx = sin((yy * height) * M_PI / 180.0) * width; - for (yy = 0; yy < canvas->h; yy++) - { - xx = sin((yy * height) * M_PI / 180.0) * width; + src.x = 0; + src.y = yy; + src.w = canvas->w; + src.h = 1; - src.x = 0; - src.y = yy; - src.w = canvas->w; - src.h = 1; + dest.x = xx; + dest.y = yy; - dest.x = xx; - dest.y = yy; - - SDL_BlitSurface(last, &src, canvas, &dest); - } + SDL_BlitSurface(last, &src, canvas, &dest); } + } else + { + width = ((x * 10) / canvas->w) + 10; + height = ((canvas->h - y) / 10) + 1; + + for (xx = 0; xx < canvas->w; xx++) { - width = ((x * 10) / canvas->w) + 10; - height = ((canvas->h - y) / 10) + 1; + yy = sin((xx * height) * M_PI / 180.0) * width; - for (xx = 0; xx < canvas->w; xx++) - { - yy = sin((xx * height) * M_PI / 180.0) * width; + src.x = xx; + src.y = 0; + src.w = 1; + src.h = canvas->h; - src.x = xx; - src.y = 0; - src.w = 1; - src.h = canvas->h; + dest.x = xx; + dest.y = yy; - dest.x = xx; - dest.y = yy; - - SDL_BlitSurface(last, &src, canvas, &dest); - } + SDL_BlitSurface(last, &src, canvas, &dest); } + } update_rect->x = 0; update_rect->y = 0; update_rect->w = canvas->w; @@ -186,16 +196,20 @@ void waves_drag(magic_api * api ATTRIBUTE_UNUSED, int which, SDL_Surface * canva // Affect the canvas on click: void waves_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) { waves_drag(api, which, canvas, last, x, y, x, y, update_rect); api->playsound(waves_snd[which], 128, 255); } // Affect the canvas on release: -void waves_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) +void waves_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) { } @@ -209,23 +223,27 @@ void waves_shutdown(magic_api * api ATTRIBUTE_UNUSED) } // Record the color from Tux Paint: -void waves_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, +void waves_set_color(magic_api * api ATTRIBUTE_UNUSED, + Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) { } // Use colors: -int waves_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int waves_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void waves_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void waves_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void waves_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void waves_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } diff --git a/magic/src/xor.c b/magic/src/xor.c index 03d795e35..3a3b2b6ce 100644 --- a/magic/src/xor.c +++ b/magic/src/xor.c @@ -43,19 +43,23 @@ int xor_get_group(magic_api * api, int which); char *xor_get_description(magic_api * api, int which, int mode); void xor_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 xor_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 xor_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 xor_shutdown(magic_api * api); void xor_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int xor_requires_colors(magic_api * api, int which); void xor_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); -void xor_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); +void xor_switchout(magic_api * api, int which, int mode, + SDL_Surface * canvas); int xor_modes(magic_api * api, int which); Uint32 xor_api_version(void) @@ -67,7 +71,8 @@ int xor_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%ssounds/magic/xor.ogg", api->data_directory); + snprintf(fname, sizeof(fname), "%ssounds/magic/xor.ogg", + api->data_directory); xor_snd = Mix_LoadWAV(fname); return (1); @@ -82,31 +87,38 @@ SDL_Surface *xor_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%simages/magic/xor.png", api->data_directory); + snprintf(fname, sizeof(fname), "%simages/magic/xor.png", + api->data_directory); return (IMG_Load(fname)); } -char *xor_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *xor_get_name(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return (strdup(gettext_noop("Xor Colors"))); } -int xor_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int xor_get_group(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return MAGIC_TYPE_COLOR_FILTERS; } -char *xor_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode) +char *xor_get_description(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode) { if (mode == MODE_PAINT) return (strdup(gettext_noop("Click and drag to draw a XOR effect"))); else - return (strdup(gettext_noop("Click to draw a XOR effect on the whole picture"))); + return (strdup + (gettext_noop + ("Click to draw a XOR effect on the whole picture"))); } static void do_xor(void *ptr, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) + SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, + int x, int y) { magic_api *api = (magic_api *) ptr; Uint8 r, g, b, xor; @@ -116,52 +128,55 @@ static void do_xor(void *ptr, int which ATTRIBUTE_UNUSED, SDL_GetRGB(api->getpixel(canvas, x, y), canvas->format, &r, &g, &b); api->rgbtohsv(r, g, b, &hue, &sat, &val); if (sat == 0) - xor = (2 * (int)hue + (x ^ y)) % 360; + xor = (2 * (int) hue + (x ^ y)) % 360; else - xor = ((int)hue + (x ^ y)) % 360; + xor = ((int) hue + (x ^ y)) % 360; api->hsvtorgb(xor, 1, 1, &r, &g, &b); pixel = SDL_MapRGB(canvas->format, r, g, b); api->putpixel(canvas, x, y, pixel); } static void do_xor_circle(void *ptr, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) + SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { magic_api *api = (magic_api *) ptr; int xx, yy; for (yy = -16; yy < 16; yy++) + { + for (xx = -16; xx < 16; xx++) { - for (xx = -16; xx < 16; xx++) - { - if (api->in_circle(xx, yy, 16)) - { - if (!api->touched(xx + x, yy + y)) - do_xor(api, which, canvas, last, x + xx, y + yy); - } - } + if (api->in_circle(xx, yy, 16)) + { + if (!api->touched(xx + x, yy + y)) + do_xor(api, which, canvas, last, x + xx, y + yy); + } } + } } void xor_drag(magic_api * api, int which, 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) { - api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_xor_circle); + api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, + do_xor_circle); if (ox > x) - { - int tmp = ox; + { + int tmp = ox; - ox = x; - x = tmp; - } + ox = x; + x = tmp; + } if (oy > y) - { - int tmp = oy; + { + int tmp = oy; - oy = y; - y = tmp; - } + oy = y; + y = tmp; + } update_rect->x = ox - 16; update_rect->y = oy - 16; @@ -172,29 +187,32 @@ void xor_drag(magic_api * api, int which, SDL_Surface * canvas, } void xor_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y, SDL_Rect * update_rect) + SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, + int x, int y, SDL_Rect * update_rect) { if (mode == MODE_PAINT) xor_drag(api, which, canvas, last, x, y, x, y, update_rect); else - { - int xx, yy; + { + int xx, yy; - for (yy = 0; yy < canvas->h; yy++) - for (xx = 0; xx < canvas->w; xx++) - do_xor(api, which, canvas, last, xx, yy); + for (yy = 0; yy < canvas->h; yy++) + for (xx = 0; xx < canvas->w; xx++) + do_xor(api, which, canvas, last, xx, yy); - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; - api->playsound(xor_snd, (x * 255) / canvas->w, 255); - } + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; + api->playsound(xor_snd, (x * 255) / canvas->w, 255); + } } void xor_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) { } @@ -204,22 +222,25 @@ void xor_shutdown(magic_api * api ATTRIBUTE_UNUSED) Mix_FreeChunk(xor_snd); } -void xor_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, - Uint8 b ATTRIBUTE_UNUSED) +void xor_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, + Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) { } -int xor_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +int xor_requires_colors(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED) { return 0; } -void xor_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void xor_switchin(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void xor_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, +void xor_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } diff --git a/src/android_assets.c b/src/android_assets.c index 9e216bf67..0d2f0caaf 100644 --- a/src/android_assets.c +++ b/src/android_assets.c @@ -19,25 +19,26 @@ #include #include "android_assets.h" -AAssetManager * asset_manager = NULL; -char* nativelibdir = NULL; +AAssetManager *asset_manager = NULL; +char *nativelibdir = NULL; -AAssetDir * open_asset_dir(char * dirname) +AAssetDir *open_asset_dir(char *dirname) { return AAssetManager_openDir(asset_manager, dirname); } -char* get_nativelibdir() +char *get_nativelibdir() { - return nativelibdir; + return nativelibdir; } -void load_assets_dir(char * dirname, tp_ftw_str ** ffilenames, unsigned * num_file_names) +void load_assets_dir(char *dirname, tp_ftw_str ** ffilenames, + unsigned *num_file_names) { - AAssetDir* assetDir = AAssetManager_openDir(asset_manager, dirname); - const char* filename = (const char*)NULL; - tp_ftw_str * filenames = NULL; - + AAssetDir *assetDir = AAssetManager_openDir(asset_manager, dirname); + const char *filename = (const char *) NULL; + tp_ftw_str *filenames = NULL; + unsigned max_file_names = 0; int fulllen = 0; *num_file_names = 0; @@ -46,7 +47,7 @@ void load_assets_dir(char * dirname, tp_ftw_str ** ffilenames, unsigned * num_f while ((filename = AAssetDir_getNextFileName(assetDir)) != NULL) { char *cp; - + fulllen = strlen(filename) + 1; if (*num_file_names == max_file_names) @@ -66,7 +67,12 @@ void load_assets_dir(char * dirname, tp_ftw_str ** ffilenames, unsigned * num_f *ffilenames = filenames; } -JNIEXPORT jboolean Java_org_tuxpaint_tuxpaintActivity_managertojni(JNIEnv * env, jclass clazz, jobject mgr) +JNIEXPORT jboolean Java_org_tuxpaint_tuxpaintActivity_managertojni(JNIEnv * + env, + jclass + clazz, + jobject + mgr) { asset_manager = AAssetManager_fromJava(env, mgr); @@ -76,7 +82,12 @@ JNIEXPORT jboolean Java_org_tuxpaint_tuxpaintActivity_managertojni(JNIEnv * env, return 1; } -JNIEXPORT void Java_org_tuxpaint_tuxpaintActivity_setnativelibdir(JNIEnv * env, jclass clazz, jstring path) +JNIEXPORT void Java_org_tuxpaint_tuxpaintActivity_setnativelibdir(JNIEnv * + env, + jclass + clazz, + jstring + path) { const char *cpath = (*env)->GetStringUTFChars(env, path, NULL); nativelibdir = strdup(cpath); @@ -84,21 +95,24 @@ JNIEXPORT void Java_org_tuxpaint_tuxpaintActivity_setnativelibdir(JNIEnv * env, } -void load_brushes_from_assets(SDL_Surface * screen, SDL_Texture *texture, SDL_Renderer *renderer, const char * dirname, void (*fn) (SDL_Surface * screen, - SDL_Texture * texture, - SDL_Renderer * renderer, - const char *restrict const dir, - unsigned dirlen, tp_ftw_str * files, - unsigned count, const char *restrict const locale)) +void load_brushes_from_assets(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer, const char *dirname, + void (*fn)(SDL_Surface * screen, + SDL_Texture * texture, + SDL_Renderer * renderer, + const char *restrict const dir, + unsigned dirlen, tp_ftw_str * files, + unsigned count, + const char *restrict const locale)) { unsigned num_file_names = 0; - char * dir = "data/brushes"; + char *dir = "data/brushes"; char buf[TP_FTW_PATHSIZE]; unsigned dirlen = strlen(dirname); memcpy(buf, dir, dirlen); - tp_ftw_str * filenames = NULL; + tp_ftw_str *filenames = NULL; load_assets_dir(dirname, &filenames, &num_file_names); fn(screen, texture, renderer, dir, dirlen, filenames, num_file_names, NULL); @@ -107,12 +121,14 @@ void load_brushes_from_assets(SDL_Surface * screen, SDL_Texture *texture, SDL_Re -void load_from_assets(SDL_Surface * screen, SDL_Texture *texture, SDL_Renderer *renderer, const char * dirname, void (*fn) (SDL_Surface * screen, - SDL_Texture * texture, - SDL_Renderer * renderer, - const char *restrict const dir, - unsigned dirlen, tp_ftw_str * files, - unsigned count, const char *restrict const locale)) +void load_from_assets(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer, const char *dirname, + void (*fn)(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer, + const char *restrict const dir, + unsigned dirlen, tp_ftw_str * files, + unsigned count, + const char *restrict const locale)) { unsigned num_file_names = 0; // char * dir = "data/stamps/cartoon/tux"; @@ -121,8 +137,9 @@ void load_from_assets(SDL_Surface * screen, SDL_Texture *texture, SDL_Renderer * memcpy(buf, dirname, dirlen); - tp_ftw_str * filenames = NULL; + tp_ftw_str *filenames = NULL; load_assets_dir(dirname, &filenames, &num_file_names); - fn(screen, texture, renderer, dirname, dirlen, filenames, num_file_names, NULL); + fn(screen, texture, renderer, dirname, dirlen, filenames, num_file_names, + NULL); } diff --git a/src/android_assets.h b/src/android_assets.h index b71a52259..97d722f7c 100644 --- a/src/android_assets.h +++ b/src/android_assets.h @@ -18,7 +18,7 @@ #define TP_ANDROID_ASSETS #include -#include "debug.h" +#include "debug.h" #include "dirwalk.h" #include "progressbar.h" #include @@ -28,27 +28,43 @@ #define ASSETS_BRUSHES_DIR "data/brushes" #define ASSETS_STAMPS_DIR "stamps/cartoon/tux" -AAssetDir * open_asset_dir(char * dirname); -char* get_nativelibdir(); +AAssetDir *open_asset_dir(char *dirname); +char *get_nativelibdir(); -void load_brushes_from_assets(SDL_Surface * screen, SDL_Texture *texture, SDL_Renderer *renderer, const char * dirname, void (*fn) (SDL_Surface * screen, - SDL_Texture * texture, - SDL_Renderer * renderer, - const char *restrict const dir, - unsigned dirlen, tp_ftw_str * files, - unsigned count, const char *restrict const locale) ); +void load_brushes_from_assets(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer, const char *dirname, + void (*fn)(SDL_Surface * screen, + SDL_Texture * texture, + SDL_Renderer * renderer, + const char *restrict const dir, + unsigned dirlen, tp_ftw_str * files, + unsigned count, + const char *restrict const locale)); -void load_from_assets(SDL_Surface * screen, SDL_Texture *texture, SDL_Renderer *renderer, const char * dirname, void (*fn) (SDL_Surface * screen, - SDL_Texture * texture, - SDL_Renderer * renderer, - const char *restrict const dir, - unsigned dirlen, tp_ftw_str * files, - unsigned count, const char *restrict const locale) ); +void load_from_assets(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer, const char *dirname, + void (*fn)(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer, + const char *restrict const dir, + unsigned dirlen, tp_ftw_str * files, + unsigned count, + const char *restrict const locale)); -void load_assets_dir(char * dirname, tp_ftw_str ** ffilenames, unsigned * num_file_names); +void load_assets_dir(char *dirname, tp_ftw_str ** ffilenames, + unsigned *num_file_names); -JNIEXPORT jboolean Java_org_tuxpaint_tuxpaintActivity_managertojni(JNIEnv * env, jclass clazz, jobject mgr); +JNIEXPORT jboolean Java_org_tuxpaint_tuxpaintActivity_managertojni(JNIEnv * + env, + jclass + clazz, + jobject + mgr); -JNIEXPORT void Java_org_tuxpaint_tuxpaintActivity_setnativelibdir(JNIEnv * env, jclass clazz, jstring path); +JNIEXPORT void Java_org_tuxpaint_tuxpaintActivity_setnativelibdir(JNIEnv * + env, + jclass + clazz, + jstring + path); #endif diff --git a/src/android_mbstowcs.c b/src/android_mbstowcs.c index 0aea8edcd..fcba44fe4 100644 --- a/src/android_mbstowcs.c +++ b/src/android_mbstowcs.c @@ -30,58 +30,67 @@ #include // This implementation may be simple, but can work fine for all of Android devices -size_t mbstowcs(wchar_t * __restrict pwcs, const char * __restrict s, size_t n){ - int length = strnlen (s, n); - // w is the index of pwcs, s is the index of s - int w = 0, c = 0; +size_t mbstowcs(wchar_t *__restrict pwcs, const char *__restrict s, size_t n) +{ + int length = strnlen(s, n); + // w is the index of pwcs, s is the index of s + int w = 0, c = 0; - while (1) { - pwcs[w] = '\0'; - char first = s[c]; - int len = 0; - if ((first & 0x80) == 0) { - pwcs[w] = (wchar_t)s[c]; - len = 1; - } - else if ((first & 0xe0) == 0xc0) { - pwcs[w] |= first & 0x1f; - pwcs[w] <<= 6; - pwcs[w] |= s[c+1] & 0x3f; - len = 2; - } - else if ((first & 0xf0) == 0xe0) { - pwcs[w] |= first & 0x0f; - pwcs[w] <<= 6; - pwcs[w] |= s[c+1] & 0x3f; - pwcs[w] <<= 6; - pwcs[w] |= s[c+2] & 0x3f; - len = 3; - } - else if ((first & 0xf8) == 0xf0) { - pwcs[w] |= first & 0x07; - pwcs[w] <<= 6; - pwcs[w] |= s[c+1] & 0x3f; - pwcs[w] <<= 6; - pwcs[w] |= s[c+2] & 0x3f; - pwcs[w] <<= 6; - pwcs[w] |= s[c+3] & 0x3f; - len = 4; - } - else { - return -1; - } + while (1) + { + pwcs[w] = '\0'; + char first = s[c]; + int len = 0; + if ((first & 0x80) == 0) + { + pwcs[w] = (wchar_t) s[c]; + len = 1; + } + else if ((first & 0xe0) == 0xc0) + { + pwcs[w] |= first & 0x1f; + pwcs[w] <<= 6; + pwcs[w] |= s[c + 1] & 0x3f; + len = 2; + } + else if ((first & 0xf0) == 0xe0) + { + pwcs[w] |= first & 0x0f; + pwcs[w] <<= 6; + pwcs[w] |= s[c + 1] & 0x3f; + pwcs[w] <<= 6; + pwcs[w] |= s[c + 2] & 0x3f; + len = 3; + } + else if ((first & 0xf8) == 0xf0) + { + pwcs[w] |= first & 0x07; + pwcs[w] <<= 6; + pwcs[w] |= s[c + 1] & 0x3f; + pwcs[w] <<= 6; + pwcs[w] |= s[c + 2] & 0x3f; + pwcs[w] <<= 6; + pwcs[w] |= s[c + 3] & 0x3f; + len = 4; + } + else + { + return -1; + } - c += len; - w++; + c += len; + w++; - if (c > length){ - pwcs[w] = '\0'; - return -1; - } - if (c == length){ - pwcs[w] = '\0'; - return w; - } - } + if (c > length) + { + pwcs[w] = '\0'; + return -1; + } + if (c == length) + { + pwcs[w] = '\0'; + return w; + } + } } diff --git a/src/android_mbstowcs.h b/src/android_mbstowcs.h index 976e4caa1..5df3e2484 100644 --- a/src/android_mbstowcs.h +++ b/src/android_mbstowcs.h @@ -31,6 +31,6 @@ Anyway, using our own implementation of "mbstowcs" can fix this problem. #undef mbsrtowcs // redefine mbstowcs function -size_t mbstowcs(wchar_t * __restrict pwcs, const char * __restrict s, size_t n); +size_t mbstowcs(wchar_t *__restrict pwcs, const char *__restrict s, size_t n); #endif diff --git a/src/android_print.c b/src/android_print.c index c904893bf..f367a1b5c 100644 --- a/src/android_print.c +++ b/src/android_print.c @@ -19,7 +19,7 @@ (See COPYING.txt) */ - + // Based on JNI and PrintHelper class // https://developer.android.com/reference/android/support/v4/print/PrintHelper.html // https://developer.android.com/training/printing/photos.html @@ -31,58 +31,80 @@ /* FIXME: Is this check still needed after the change from android/support/v4 to AndroidX and being Android 5 the minimum version we support? */ // Since Print work is based on Java PrintHelper class, which may not be supported on some old versions -int IsPrinterAvailable( void ) +int IsPrinterAvailable(void) { - JNIEnv *mEnv = Android_JNI_GetEnv(); - jclass mPrintHelperClass = (*mEnv)->FindClass(mEnv, "androidx/print/PrintHelper"); + JNIEnv *mEnv = Android_JNI_GetEnv(); + jclass mPrintHelperClass = + (*mEnv)->FindClass(mEnv, "androidx/print/PrintHelper"); - if (mPrintHelperClass == NULL) - return 0; + if (mPrintHelperClass == NULL) + return 0; - jmethodID mSupportMethod = (*mEnv)->GetStaticMethodID(mEnv, mPrintHelperClass, "systemSupportsPrint", "()Z"); - jboolean support = (*mEnv)->CallStaticBooleanMethod(mEnv, mPrintHelperClass, mSupportMethod); + jmethodID mSupportMethod = + (*mEnv)->GetStaticMethodID(mEnv, mPrintHelperClass, "systemSupportsPrint", + "()Z"); + jboolean support = + (*mEnv)->CallStaticBooleanMethod(mEnv, mPrintHelperClass, mSupportMethod); - return support ? 1 : 0; + return support ? 1 : 0; } // This function is based on // (1) convert surface to Java BitMap object // (2) call Java PrintHelper to do print job. -const char *SurfacePrint(SDL_Surface *surface) +const char *SurfacePrint(SDL_Surface * surface) { - JNIEnv *mEnv = Android_JNI_GetEnv(); - jclass mBitmapClass = (*mEnv)->FindClass(mEnv, "android/graphics/Bitmap"); - jmethodID mCreateMethod = (*mEnv)->GetStaticMethodID(mEnv, mBitmapClass, "createBitmap", "([IIILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;"); - jintArray mSurfaceArray = (*mEnv)->NewIntArray(mEnv, surface->w * surface->h); - (*mEnv)->SetIntArrayRegion(mEnv,mSurfaceArray, 0, surface->w * surface->h, surface->pixels); - jclass mConfigClass = (*mEnv)->FindClass(mEnv, "android/graphics/Bitmap$Config"); - jfieldID mConfigField = (*mEnv)->GetStaticFieldID(mEnv, mConfigClass , "ARGB_8888", "Landroid/graphics/Bitmap$Config;"); - jobject mConfig = (*mEnv)->GetStaticObjectField(mEnv, mConfigClass, mConfigField); - jobject mBitMap = (*mEnv)->CallStaticObjectMethod(mEnv, mBitmapClass, mCreateMethod, mSurfaceArray, surface->w, surface->h, mConfig); + JNIEnv *mEnv = Android_JNI_GetEnv(); + jclass mBitmapClass = (*mEnv)->FindClass(mEnv, "android/graphics/Bitmap"); + jmethodID mCreateMethod = + (*mEnv)->GetStaticMethodID(mEnv, mBitmapClass, "createBitmap", + "([IIILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;"); + jintArray mSurfaceArray = + (*mEnv)->NewIntArray(mEnv, surface->w * surface->h); + (*mEnv)->SetIntArrayRegion(mEnv, mSurfaceArray, 0, surface->w * surface->h, + surface->pixels); + jclass mConfigClass = + (*mEnv)->FindClass(mEnv, "android/graphics/Bitmap$Config"); + jfieldID mConfigField = + (*mEnv)->GetStaticFieldID(mEnv, mConfigClass, "ARGB_8888", + "Landroid/graphics/Bitmap$Config;"); + jobject mConfig = + (*mEnv)->GetStaticObjectField(mEnv, mConfigClass, mConfigField); + jobject mBitMap = + (*mEnv)->CallStaticObjectMethod(mEnv, mBitmapClass, mCreateMethod, + mSurfaceArray, surface->w, surface->h, + mConfig); - jobject mContext = (jobject)SDL_AndroidGetActivity(); - jclass mPrintClass = (*mEnv)->FindClass(mEnv, "androidx/print/PrintHelper"); - // sometimes android v4 support library may be not ready - if (mPrintClass == NULL) - return "There is no androidX support library."; - jmethodID mInitMethod = (*mEnv)->GetMethodID(mEnv, mPrintClass, "", "(Landroid/content/Context;)V"); - jobject mPrint = (*mEnv)->NewObject(mEnv, mPrintClass, mInitMethod, mContext); - jmethodID mPrintMethod = (*mEnv)->GetMethodID(mEnv, mPrintClass, "printBitmap", "(Ljava/lang/String;Landroid/graphics/Bitmap;)V"); + jobject mContext = (jobject) SDL_AndroidGetActivity(); + jclass mPrintClass = (*mEnv)->FindClass(mEnv, "androidx/print/PrintHelper"); + // sometimes android v4 support library may be not ready + if (mPrintClass == NULL) + return "There is no androidX support library."; + jmethodID mInitMethod = (*mEnv)->GetMethodID(mEnv, mPrintClass, "", + "(Landroid/content/Context;)V"); + jobject mPrint = + (*mEnv)->NewObject(mEnv, mPrintClass, mInitMethod, mContext); + jmethodID mPrintMethod = + (*mEnv)->GetMethodID(mEnv, mPrintClass, "printBitmap", + "(Ljava/lang/String;Landroid/graphics/Bitmap;)V"); - /* Thanks to n.collins for the explaination on the int signature - on https://stackoverflow.com/questions/13468041/android-how-to-call-java-method-from-jni-with-int-and-int-parameters --Pere */ - jmethodID msetScaleMode = (*mEnv)->GetMethodID(mEnv, mPrintClass, "setScaleMode", "(I)V"); - jfieldID mScaleModeField = (*mEnv)->GetStaticFieldID(mEnv, mPrintClass, "SCALE_MODE_FIT", "I"); - jint mScaleModeInt = (*mEnv)->GetStaticIntField(mEnv, mPrintClass, mScaleModeField); - (*mEnv)->CallVoidMethod(mEnv, mPrint, msetScaleMode, mScaleModeInt); + /* Thanks to n.collins for the explaination on the int signature + on https://stackoverflow.com/questions/13468041/android-how-to-call-java-method-from-jni-with-int-and-int-parameters --Pere */ + jmethodID msetScaleMode = + (*mEnv)->GetMethodID(mEnv, mPrintClass, "setScaleMode", "(I)V"); + jfieldID mScaleModeField = + (*mEnv)->GetStaticFieldID(mEnv, mPrintClass, "SCALE_MODE_FIT", "I"); + jint mScaleModeInt = + (*mEnv)->GetStaticIntField(mEnv, mPrintClass, mScaleModeField); + (*mEnv)->CallVoidMethod(mEnv, mPrint, msetScaleMode, mScaleModeInt); - jstring mString = (*mEnv)->NewStringUTF(mEnv, "TuxPaint"); - (*mEnv)->CallVoidMethod(mEnv, mPrint, mPrintMethod, mString, mBitMap); + jstring mString = (*mEnv)->NewStringUTF(mEnv, "TuxPaint"); + (*mEnv)->CallVoidMethod(mEnv, mPrint, mPrintMethod, mString, mBitMap); - // clean up - (*mEnv)->DeleteLocalRef(mEnv, mSurfaceArray); - (*mEnv)->DeleteLocalRef(mEnv, mConfig); - (*mEnv)->DeleteLocalRef(mEnv, mPrint); - (*mEnv)->DeleteLocalRef(mEnv, mString); - return NULL; + // clean up + (*mEnv)->DeleteLocalRef(mEnv, mSurfaceArray); + (*mEnv)->DeleteLocalRef(mEnv, mConfig); + (*mEnv)->DeleteLocalRef(mEnv, mPrint); + (*mEnv)->DeleteLocalRef(mEnv, mString); + return NULL; } diff --git a/src/android_print.h b/src/android_print.h index 0d122430d..01b0785f0 100644 --- a/src/android_print.h +++ b/src/android_print.h @@ -1,7 +1,7 @@ -/* android_print.h */ - -/* printing support for Tux Paint */ - +/* android_print.h */ + +/* printing support for Tux Paint */ + /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -17,15 +17,15 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA (See COPYING.txt) -*/ - +*/ + #ifndef __ANDROID_PRINT_H__ #define __ANDROID_PRINT_H__ - + #include "SDL.h" -extern const char *SurfacePrint(SDL_Surface *surface); +extern const char *SurfacePrint(SDL_Surface * surface); extern int IsPrinterAvailable(void); - -#endif /* __ANDROID_PRINT__ */ + +#endif /* __ANDROID_PRINT__ */ diff --git a/src/compiler.h b/src/compiler.h index 61e6f3998..152d3c43a 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -129,9 +129,8 @@ /* h/t https://tutel.me/c/programming/questions/45349079/how+to+use+__attribute__fallthrough+correctly+in+gcc */ #ifndef FALLTHROUGH #if defined(__GNUC__) && __GNUC__ >= 7 - #define FALL_THROUGH __attribute__ ((fallthrough)) +#define FALL_THROUGH __attribute__ ((fallthrough)) #else - #define FALL_THROUGH ((void)0) +#define FALL_THROUGH ((void)0) #endif /* __GNUC__ >= 7 */ #endif - diff --git a/src/cursor.c b/src/cursor.c index ecbc6300c..e6748a0bc 100644 --- a/src/cursor.c +++ b/src/cursor.c @@ -97,8 +97,8 @@ void do_setcursor(SDL_Cursor * c) void free_cursor(SDL_Cursor ** cursor) { if (*cursor) - { - SDL_FreeCursor(*cursor); - *cursor = NULL; - } + { + SDL_FreeCursor(*cursor); + *cursor = NULL; + } } diff --git a/src/dirwalk.c b/src/dirwalk.c index 7c251d702..7b612caa3 100644 --- a/src/dirwalk.c +++ b/src/dirwalk.c @@ -79,8 +79,10 @@ extern char *strcasestr(const char *haystack, const char *needle); * @param screen Screen surface, for animating progress bar. * FIXME */ -void loadfont_callback(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, - const char *restrict const dir, unsigned dirlen, tp_ftw_str * files, unsigned i, +void loadfont_callback(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer, + const char *restrict const dir, unsigned dirlen, + tp_ftw_str * files, unsigned i, const char *restrict const locale) { dirlen = dirlen; @@ -90,188 +92,209 @@ void loadfont_callback(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer #else while (i--) #endif + { + int loadable = 0; + const char *restrict const cp = strchr(files[i].str, '.'); + + show_progress_bar_(screen, texture, renderer); + if (cp) { - int loadable = 0; - const char *restrict const cp = strchr(files[i].str, '.'); + // need gcc 3.4 for the restrict in this location + const char * /*restrict */ const suffixes[] = + { "ttc", "dfont", "pfa", "pfb", "otf", "ttf", }; + int j = sizeof suffixes / sizeof suffixes[0]; - show_progress_bar_(screen, texture, renderer); - if (cp) + while (j--) + { + // only check part, because of potential .gz or .bz2 suffix + if (!strncasecmp(cp + 1, suffixes[j], strlen(suffixes[j]))) { - // need gcc 3.4 for the restrict in this location - const char * /*restrict */ const suffixes[] = - { "ttc", "dfont", "pfa", "pfb", "otf", "ttf", }; - int j = sizeof suffixes / sizeof suffixes[0]; - - while (j--) - { - // only check part, because of potential .gz or .bz2 suffix - if (!strncasecmp(cp + 1, suffixes[j], strlen(suffixes[j]))) - { - loadable = 1; - break; - } - } + loadable = 1; + break; } - if (!loadable) + } + } + if (!loadable) + { + if (strcasestr(files[i].str, "/rsrc")) + loadable = 1; + } + // Loadable: TrueType (.ttf), OpenType (.otf), Type1 (.pfa and .pfb), + // and various useless bitmap fonts. Compressed files (with .gz or .bz2) + // should also work. A *.dfont is pretty much a Mac resource fork in a normal + // file, and may load with some library versions. + if (loadable) + { + char fname[512]; + TuxPaint_Font *font; + + snprintf(fname, sizeof fname, "%s/%s", dir, files[i].str); +#ifdef DEBUG + printf("%s:%d - Loading font: %s (locale is: %s)\n", __FILE__, __LINE__, fname, (locale ? locale : "NULL")); //EP + fflush(stdout); +#endif + if (locale && strstr(fname, "locale") && !all_locale_fonts) + { + char fname_check[512]; + + /* We're (probably) loading from our locale fonts folder; ONLY load our locale's font */ + snprintf(fname_check, sizeof fname_check, "%s/%s.ttf", dir, locale); +#ifdef DEBUG + printf("%s:%d - checking \"%s\" vs \"%s\"\n", __FILE__, __LINE__, fname_check, fname); //EP +#endif + if (strcmp(fname, fname_check) == 0) + font = TuxPaint_Font_OpenFont("", fname, text_sizes[text_size]); + else + font = NULL; + } + else + { + font = TuxPaint_Font_OpenFont("", fname, text_sizes[text_size]); + } + if (font) + { + const char *restrict const family = + TuxPaint_Font_FontFaceFamilyName(font); + const char *restrict const style = + TuxPaint_Font_FontFaceStyleName(font); + + +#ifdef DEBUG + if (font->typ == FONT_TYPE_TTF) { - if (strcasestr(files[i].str, "/rsrc")) - loadable = 1; + int numfaces = TTF_FontFaces(font->ttf_font); + + if (numfaces != 1) + printf("%s:%d - Found %d faces in %s, %s, %s\n", __FILE__, + __LINE__, numfaces, files[i].str, family, style); + + printf("%s:%d - success: tpf: 0x%x tpf->ttf_font: 0x%x\n", __FILE__, __LINE__, (unsigned int) (intptr_t) font, (unsigned int) (intptr_t) font->ttf_font); //EP added (intptr_t) to avoid warning on x64 } - // Loadable: TrueType (.ttf), OpenType (.otf), Type1 (.pfa and .pfb), - // and various useless bitmap fonts. Compressed files (with .gz or .bz2) - // should also work. A *.dfont is pretty much a Mac resource fork in a normal - // file, and may load with some library versions. - if (loadable) - { - char fname[512]; - TuxPaint_Font *font; - - snprintf(fname, sizeof fname, "%s/%s", dir, files[i].str); -#ifdef DEBUG - printf("%s:%d - Loading font: %s (locale is: %s)\n", __FILE__, __LINE__, fname, (locale ? locale : "NULL")); //EP -fflush(stdout); -#endif - if (locale && strstr(fname, "locale") && !all_locale_fonts) - { - char fname_check[512]; - - /* We're (probably) loading from our locale fonts folder; ONLY load our locale's font */ - snprintf(fname_check, sizeof fname_check, "%s/%s.ttf", dir, locale); -#ifdef DEBUG - printf("%s:%d - checking \"%s\" vs \"%s\"\n", __FILE__, __LINE__, fname_check, fname); //EP -#endif - if (strcmp(fname, fname_check) == 0) - font = TuxPaint_Font_OpenFont("", fname, text_sizes[text_size]); - else - font = NULL; - } - else - { - font = TuxPaint_Font_OpenFont("", fname, text_sizes[text_size]); - } - if (font) - { - const char *restrict const family = TuxPaint_Font_FontFaceFamilyName(font); - const char *restrict const style = TuxPaint_Font_FontFaceStyleName(font); - - -#ifdef DEBUG - if (font->typ == FONT_TYPE_TTF) - { - int numfaces = TTF_FontFaces(font->ttf_font); - - if (numfaces != 1) - printf("%s:%d - Found %d faces in %s, %s, %s\n", __FILE__, __LINE__, numfaces, files[i].str, family, style); - - printf("%s:%d - success: tpf: 0x%x tpf->ttf_font: 0x%x\n", __FILE__, __LINE__, (unsigned int)(intptr_t) font, (unsigned int)(intptr_t) font->ttf_font); //EP added (intptr_t) to avoid warning on x64 - } #ifndef NO_SDLPANGO - else - printf("%s:%d - success: tpf: 0x%x tpf->pango_context: 0x%x\n", __FILE__, __LINE__, (unsigned int)(intptr_t) font, (unsigned int)(intptr_t) font->pango_context); + else + printf("%s:%d - success: tpf: 0x%x tpf->pango_context: 0x%x\n", + __FILE__, __LINE__, (unsigned int) (intptr_t) font, + (unsigned int) (intptr_t) font->pango_context); #endif #endif - // These fonts crash Tux Paint via a library bug. - int blacklisted = !strcmp("Zapfino", family) || !strcmp("Elvish Ring NFI", family); + // These fonts crash Tux Paint via a library bug. + int blacklisted = !strcmp("Zapfino", family) + || !strcmp("Elvish Ring NFI", family); - // First, the blacklist. We list font families that can crash Tux Paint - // via bugs in the SDL_ttf library. We also test fonts to be sure that - // they have both uppercase and lowercase letters. Note that we do not - // test for "Aa", because it is OK if uppercase and lowercase are the - // same (but not nice -- such fonts get a low score later). - // - // Most locales leave the blacklist strings alone: "QX" and "qx" - // (it is less destructive to use the scoring strings instead) - // - // Locales that absolutely require all fonts to have some - // extra characters should use "QX..." and "qx...", where "..." - // are some characters you absolutely require in all fonts. - // - // Locales with absolutely NO use for ASCII may use "..." and "...", - // where "..." are some characters you absolutely require in - // all fonts. This would be the case for a locale in which it is - // impossible for a user to type ASCII letters. - // - // Most translators should use scoring instead. - if (!charset_works(font, gettext("qx")) || !charset_works(font, gettext("QX"))) - blacklisted = 1; + // First, the blacklist. We list font families that can crash Tux Paint + // via bugs in the SDL_ttf library. We also test fonts to be sure that + // they have both uppercase and lowercase letters. Note that we do not + // test for "Aa", because it is OK if uppercase and lowercase are the + // same (but not nice -- such fonts get a low score later). + // + // Most locales leave the blacklist strings alone: "QX" and "qx" + // (it is less destructive to use the scoring strings instead) + // + // Locales that absolutely require all fonts to have some + // extra characters should use "QX..." and "qx...", where "..." + // are some characters you absolutely require in all fonts. + // + // Locales with absolutely NO use for ASCII may use "..." and "...", + // where "..." are some characters you absolutely require in + // all fonts. This would be the case for a locale in which it is + // impossible for a user to type ASCII letters. + // + // Most translators should use scoring instead. + if (!charset_works(font, gettext("qx")) + || !charset_works(font, gettext("QX"))) + blacklisted = 1; - if (!blacklisted) - { - if (num_font_styles == num_font_styles_max) - { - num_font_styles_max = num_font_styles_max * 5 / 4 + 30; - user_font_styles = realloc(user_font_styles, num_font_styles_max * sizeof *user_font_styles); - } - user_font_styles[num_font_styles] = malloc(sizeof *user_font_styles[num_font_styles]); - user_font_styles[num_font_styles]->directory = strdup(dir); - user_font_styles[num_font_styles]->filename = files[i].str; // steal it (mark NULL below) - user_font_styles[num_font_styles]->family = strdup(family); - user_font_styles[num_font_styles]->style = strdup(style); - user_font_styles[num_font_styles]->score = 0; + if (!blacklisted) + { + if (num_font_styles == num_font_styles_max) + { + num_font_styles_max = num_font_styles_max * 5 / 4 + 30; + user_font_styles = + realloc(user_font_styles, + num_font_styles_max * sizeof *user_font_styles); + } + user_font_styles[num_font_styles] = + malloc(sizeof *user_font_styles[num_font_styles]); + user_font_styles[num_font_styles]->directory = strdup(dir); + user_font_styles[num_font_styles]->filename = files[i].str; // steal it (mark NULL below) + user_font_styles[num_font_styles]->family = strdup(family); + user_font_styles[num_font_styles]->style = strdup(style); + user_font_styles[num_font_styles]->score = 0; - // TODO: weight specification + // TODO: weight specification - // Now we score fonts to ensure that the best ones will be placed at - // the top of the list. The user will see them first. This sorting is - // especially important for users who have scroll buttons disabled. - // Translators should do whatever is needed to put crummy fonts last. + // Now we score fonts to ensure that the best ones will be placed at + // the top of the list. The user will see them first. This sorting is + // especially important for users who have scroll buttons disabled. + // Translators should do whatever is needed to put crummy fonts last. - // distinct uppercase and lowercase (e.g., 'o' vs. 'O') - user_font_styles[num_font_styles]->score += charset_works(font, gettext("oO")); + // distinct uppercase and lowercase (e.g., 'o' vs. 'O') + user_font_styles[num_font_styles]->score += + charset_works(font, gettext("oO")); - // common punctuation (e.g., '?', '!', '.', ',', etc.) - user_font_styles[num_font_styles]->score += charset_works(font, gettext(",.?!")); + // common punctuation (e.g., '?', '!', '.', ',', etc.) + user_font_styles[num_font_styles]->score += + charset_works(font, gettext(",.?!")); - // uncommon punctuation (e.g., '@', '#', '*', etc.) - user_font_styles[num_font_styles]->score += charset_works(font, gettext("`\%_@$~#{<(^&*")); + // uncommon punctuation (e.g., '@', '#', '*', etc.) + user_font_styles[num_font_styles]->score += + charset_works(font, gettext("`\%_@$~#{<(^&*")); - // digits (e.g., '0', '1' and '7') - user_font_styles[num_font_styles]->score += charset_works(font, gettext("017")); + // digits (e.g., '0', '1' and '7') + user_font_styles[num_font_styles]->score += + charset_works(font, gettext("017")); - // distinct circle-like characters (e.g., 'O' (capital oh) vs. '0' (zero)) - user_font_styles[num_font_styles]->score += charset_works(font, gettext("O0")); + // distinct circle-like characters (e.g., 'O' (capital oh) vs. '0' (zero)) + user_font_styles[num_font_styles]->score += + charset_works(font, gettext("O0")); - // distinct line-like characters (e.g., 'l' (lowercase elle) vs. '1' (one) vs. 'I' (capital aye)) - user_font_styles[num_font_styles]->score += charset_works(font, gettext("1Il|")); + // distinct line-like characters (e.g., 'l' (lowercase elle) vs. '1' (one) vs. 'I' (capital aye)) + user_font_styles[num_font_styles]->score += + charset_works(font, gettext("1Il|")); - // translation spares -- design not finalized + // translation spares -- design not finalized #if 0 - user_font_styles[num_font_styles]->score += charset_works(font, gettext("<1>spare-1a")); - user_font_styles[num_font_styles]->score += charset_works(font, gettext("<1>spare-1b")); - user_font_styles[num_font_styles]->score += charset_works(font, gettext("<9>spare-9a")) * 9; - user_font_styles[num_font_styles]->score += charset_works(font, gettext("<9>spare-9b")) * 9; + user_font_styles[num_font_styles]->score += + charset_works(font, gettext("<1>spare-1a")); + user_font_styles[num_font_styles]->score += + charset_works(font, gettext("<1>spare-1b")); + user_font_styles[num_font_styles]->score += + charset_works(font, gettext("<9>spare-9a")) * 9; + user_font_styles[num_font_styles]->score += + charset_works(font, gettext("<9>spare-9b")) * 9; #endif // this really should be dynamic, avoiding the need for a special build #ifdef OLPC_XO - // Maybe German adds a "\xc2\xb7" (middle dot) and colon here? The key wouldn't change though. - user_font_styles[num_font_styles]->score += charset_works(font, "\xc3\x97\xc3\xb7"); // multiply and divide + // Maybe German adds a "\xc2\xb7" (middle dot) and colon here? The key wouldn't change though. + user_font_styles[num_font_styles]->score += charset_works(font, "\xc3\x97\xc3\xb7"); // multiply and divide #endif - // FIXME: add topology tests ('A' has one hole, 'B' has two holes, etc.) + // FIXME: add topology tests ('A' has one hole, 'B' has two holes, etc.) - num_font_styles++; + num_font_styles++; //printf("Accepted: %s, %s, %s, score(%d)\n", files[i].str, family, style, user_font_styles[num_font_styles]->score); - files[i].str = NULL; // so free() won't crash -- we stole the memory - } - else - { -#ifdef DEBUG - fprintf(stderr, "Font is too defective: %s, %s, %s\n", files[i].str, family, style); -#endif - } - TuxPaint_Font_CloseFont(font); - } - else - { -#ifdef DEBUG - fprintf(stderr, "could not open %s\n", files[i].str); -#endif - } + files[i].str = NULL; // so free() won't crash -- we stole the memory } - free(files[i].str); + else + { +#ifdef DEBUG + fprintf(stderr, "Font is too defective: %s, %s, %s\n", files[i].str, + family, style); +#endif + } + TuxPaint_Font_CloseFont(font); + } + else + { +#ifdef DEBUG + fprintf(stderr, "could not open %s\n", files[i].str); +#endif + } } + free(files[i].str); + } free(files); } @@ -305,10 +328,17 @@ int compare_ftw_str(const void *v1, const void *v2) * @param fn Callback function to invoke * @param locale Locale, to pass to callback function when applicable (i.e., for fonts), else NULL */ -void tp_ftw(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, char *restrict const dir, - unsigned dirlen, int rsrc, void (*fn) (SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, - const char *restrict const dir, unsigned dirlen, tp_ftw_str * files, - unsigned count, const char *restrict const locale), +void tp_ftw(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer, char *restrict const dir, + unsigned dirlen, int rsrc, void (*fn)(SDL_Surface * screen, + SDL_Texture * texture, + SDL_Renderer * renderer, + const char *restrict const + dir, unsigned dirlen, + tp_ftw_str * files, + unsigned count, + const char *restrict const + locale), const char *restrict const locale) { DIR *d; @@ -335,173 +365,177 @@ void tp_ftw(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer AAssetDir *adir; if (!d) /* Fallback into assets */ - { - /* Remove the trailing '/' */ - dlen = strlen(dir) - 1; - di = strndup(dir, dlen); - adir = open_asset_dir(di); - if (!adir) - return; - } + { + /* Remove the trailing '/' */ + dlen = strlen(dir) - 1; + di = strndup(dir, dlen); + adir = open_asset_dir(di); + if (!adir) + return; + } #else if (!d) return; #endif for (;;) - { - struct dirent *f; + { + struct dirent *f; - if (d) - f = readdir(d); - int filetype = TP_FTW_UNKNOWN; + if (d) + f = readdir(d); + int filetype = TP_FTW_UNKNOWN; #ifdef __ANDROID__ - char *afilename; + char *afilename; - if (!d) - { - afilename = AAssetDir_getNextFileName(adir); - if (afilename) - { - f = malloc(sizeof(struct dirent)); - strncpy(f->d_name, afilename, sizeof(f->d_name)); - f->d_type = DT_REG; + if (!d) + { + afilename = AAssetDir_getNextFileName(adir); + if (afilename) + { + f = malloc(sizeof(struct dirent)); + strncpy(f->d_name, afilename, sizeof(f->d_name)); + f->d_type = DT_REG; - /* There is not _DIRENT_HAVE_D_NAMLEN currently on Android 4.3, but who knows in the future... */ + /* There is not _DIRENT_HAVE_D_NAMLEN currently on Android 4.3, but who knows in the future... */ #if defined(_DIRENT_HAVE_D_NAMLEN) - f->d_namlen = strlen(f->d_name); + f->d_namlen = strlen(f->d_name); #endif - /* AAssetDir_getNextFileName() only lists files, not (sub)dirs, - and we don't put any device or special file inside assets, - so it is a regular file */ - filetype = TP_FTW_NORMAL; - } - else - break; - } -#endif - - if (!f) + /* AAssetDir_getNextFileName() only lists files, not (sub)dirs, + and we don't put any device or special file inside assets, + so it is a regular file */ + filetype = TP_FTW_NORMAL; + } + else break; - if (f->d_name[0] == '.') - continue; + } +#endif + + if (!f) + break; + if (f->d_name[0] == '.') + continue; // Linux and BSD can often provide file type info w/o the stat() call #ifdef DT_UNKNOWN - switch (f->d_type) - { - default: - continue; - case DT_REG: - if (!rsrc) // if maybe opening resource files, need st_size - filetype = TP_FTW_NORMAL; - break; - case DT_DIR: - filetype = TP_FTW_DIRECTORY; - break; - case DT_UNKNOWN: - case DT_LNK: - ; - } + switch (f->d_type) + { + default: + continue; + case DT_REG: + if (!rsrc) // if maybe opening resource files, need st_size + filetype = TP_FTW_NORMAL; + break; + case DT_DIR: + filetype = TP_FTW_DIRECTORY; + break; + case DT_UNKNOWN: + case DT_LNK: + ; + } #else #warning Failed to see DT_UNKNOWN #endif #if defined(_DIRENT_HAVE_D_NAMLEN) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) - d_namlen = f->d_namlen; + d_namlen = f->d_namlen; #else - d_namlen = strlen(f->d_name); + d_namlen = strlen(f->d_name); #endif - add_rsrc = 0; + add_rsrc = 0; - if (filetype == TP_FTW_UNKNOWN) - { - struct stat sbuf; + if (filetype == TP_FTW_UNKNOWN) + { + struct stat sbuf; - memcpy(dir + dirlen, f->d_name, d_namlen + 1); - if (stat(dir, &sbuf)) - continue; // oh well... try the next one - if (S_ISDIR(sbuf.st_mode)) - filetype = TP_FTW_DIRECTORY; - else if (S_ISREG(sbuf.st_mode)) - { - filetype = TP_FTW_NORMAL; - if (rsrc && !sbuf.st_size) - add_rsrc = 5; // 5 is length of "/rsrc" - } - else - continue; // was a device file or somesuch - } - if (filetype == TP_FTW_NORMAL) - { - char *cp; - - if (num_file_names == max_file_names) - { - max_file_names = max_file_names * 5 / 4 + 30; - file_names = realloc(file_names, max_file_names * sizeof *file_names); - } - cp = malloc(d_namlen + add_rsrc + 1); - memcpy(cp, f->d_name, d_namlen); - if (add_rsrc) - memcpy(cp + d_namlen, "/rsrc", 6); - else - cp[d_namlen] = '\0'; - file_names[num_file_names].str = cp; - file_names[num_file_names].len = d_namlen; - num_file_names++; - } - if (filetype == TP_FTW_DIRECTORY) - { - char *cp; - - if (num_dir_names == max_dir_names) - { - max_dir_names = max_dir_names * 5 / 4 + 3; - dir_names = realloc(dir_names, max_dir_names * sizeof *dir_names); - } - cp = malloc(d_namlen + 1); - memcpy(cp, f->d_name, d_namlen + 1); - dir_names[num_dir_names].str = cp; - dir_names[num_dir_names].len = d_namlen; - num_dir_names++; - } + memcpy(dir + dirlen, f->d_name, d_namlen + 1); + if (stat(dir, &sbuf)) + continue; // oh well... try the next one + if (S_ISDIR(sbuf.st_mode)) + filetype = TP_FTW_DIRECTORY; + else if (S_ISREG(sbuf.st_mode)) + { + filetype = TP_FTW_NORMAL; + if (rsrc && !sbuf.st_size) + add_rsrc = 5; // 5 is length of "/rsrc" + } + else + continue; // was a device file or somesuch } + if (filetype == TP_FTW_NORMAL) + { + char *cp; + + if (num_file_names == max_file_names) + { + max_file_names = max_file_names * 5 / 4 + 30; + file_names = realloc(file_names, max_file_names * sizeof *file_names); + } + cp = malloc(d_namlen + add_rsrc + 1); + memcpy(cp, f->d_name, d_namlen); + if (add_rsrc) + memcpy(cp + d_namlen, "/rsrc", 6); + else + cp[d_namlen] = '\0'; + file_names[num_file_names].str = cp; + file_names[num_file_names].len = d_namlen; + num_file_names++; + } + if (filetype == TP_FTW_DIRECTORY) + { + char *cp; + + if (num_dir_names == max_dir_names) + { + max_dir_names = max_dir_names * 5 / 4 + 3; + dir_names = realloc(dir_names, max_dir_names * sizeof *dir_names); + } + cp = malloc(d_namlen + 1); + memcpy(cp, f->d_name, d_namlen + 1); + dir_names[num_dir_names].str = cp; + dir_names[num_dir_names].len = d_namlen; + num_dir_names++; + } + } closedir(d); show_progress_bar_(screen, texture, renderer); dir[dirlen] = '\0'; // repair it (clobbered for stat() call above) if (1 || file_names) // Now ALWAYS calling callback function, so stamp loader can notice top-level directories (even if there are only subdirs, and no files, inside) -bjk 2007.05.16 - { + { // let callee sort and keep the string #if 0 - qsort(file_names, num_file_names, sizeof *file_names, compare_ftw_str); - while (num_file_names--) - { - free(file_names[num_file_names].str); - } - free(file_names); + qsort(file_names, num_file_names, sizeof *file_names, compare_ftw_str); + while (num_file_names--) + { + free(file_names[num_file_names].str); + } + free(file_names); #else #ifdef __ANDROID__ - if (dlen != dirlen) /* First case only happens in Android files coming from assets */ - fn(screen, texture, renderer, di, dlen, file_names, num_file_names, locale); - else + if (dlen != dirlen) /* First case only happens in Android files coming from assets */ + fn(screen, texture, renderer, di, dlen, file_names, num_file_names, + locale); + else #endif - fn(screen, texture, renderer, dir, dirlen, file_names, num_file_names, locale); + fn(screen, texture, renderer, dir, dirlen, file_names, num_file_names, + locale); #endif - } + } if (dir_names) + { + qsort(dir_names, num_dir_names, sizeof *dir_names, compare_ftw_str); + while (num_dir_names--) { - qsort(dir_names, num_dir_names, sizeof *dir_names, compare_ftw_str); - while (num_dir_names--) - { - memcpy(dir + dirlen, dir_names[num_dir_names].str, dir_names[num_dir_names].len + 1); - tp_ftw(screen, texture, renderer, dir, dirlen + dir_names[num_dir_names].len, rsrc, fn, locale); - free(dir_names[num_dir_names].str); - } - free(dir_names); + memcpy(dir + dirlen, dir_names[num_dir_names].str, + dir_names[num_dir_names].len + 1); + tp_ftw(screen, texture, renderer, dir, + dirlen + dir_names[num_dir_names].len, rsrc, fn, locale); + free(dir_names[num_dir_names].str); } + free(dir_names); + } } diff --git a/src/dirwalk.h b/src/dirwalk.h index 8dd092c2b..3355ab78f 100644 --- a/src/dirwalk.h +++ b/src/dirwalk.h @@ -45,14 +45,23 @@ typedef struct tp_ftw_str } tp_ftw_str; -void loadfont_callback(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, - const char *restrict const dir, unsigned dirlen, tp_ftw_str * files, unsigned i, +void loadfont_callback(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer, + const char *restrict const dir, unsigned dirlen, + tp_ftw_str * files, unsigned i, const char *restrict const locale); int compare_ftw_str(const void *v1, const void *v2); -void tp_ftw(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, char *restrict const dir, - unsigned dirlen, int rsrc, void (*fn) (SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, - const char *restrict const dir, unsigned dirlen, tp_ftw_str * files, - unsigned count, const char *restrict const locale), +void tp_ftw(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer, char *restrict const dir, + unsigned dirlen, int rsrc, void (*fn)(SDL_Surface * screen, + SDL_Texture * texture, + SDL_Renderer * renderer, + const char *restrict const + dir, unsigned dirlen, + tp_ftw_str * files, + unsigned count, + const char *restrict const + locale), const char *restrict const locale); #endif diff --git a/src/fill.c b/src/fill.c index 0ff7ece6f..22f33b12f 100644 --- a/src/fill.c +++ b/src/fill.c @@ -64,63 +64,73 @@ #define QUEUE_SIZE_CHUNK 1024 -typedef struct queue_s { +typedef struct queue_s +{ int x, y, y_outside; } queue_t; -queue_t * queue; +queue_t *queue; int queue_size = 0, queue_end = 0; /* Local function prototypes: */ -SDL_Surface * global_screen, * global_last, * global_canvas; +SDL_Surface *global_screen, *global_last, *global_canvas; Uint32 global_old_colr, global_cur_colr; -Uint8 * global_touched; +Uint8 *global_touched; int global_extent_x1, global_extent_y1, global_extent_x2, global_extent_y2; int global_prog_anim; double colors_close(SDL_Surface * canvas, Uint32 c1, Uint32 c2); -Uint32 blend(SDL_Surface * canvas, Uint32 draw_colr, Uint32 old_colr, double pct); -void simulate_flood_fill_outside_check(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, int x, int y, int y_outside); -void draw_brush_fill_single(SDL_Surface * canvas, int x, int y, Uint32 draw_color, Uint8 * touched); +Uint32 blend(SDL_Surface * canvas, Uint32 draw_colr, Uint32 old_colr, + double pct); +void simulate_flood_fill_outside_check(SDL_Surface * screen, + SDL_Texture * texture, + SDL_Renderer * renderer, int x, int y, + int y_outside); +void draw_brush_fill_single(SDL_Surface * canvas, int x, int y, + Uint32 draw_color, Uint8 * touched); void init_queue(void); void add_to_queue(int x, int y, int y_outside); -int remove_from_queue(int * x, int * y, int * y_outside); +int remove_from_queue(int *x, int *y, int *y_outside); void cleanup_queue(void); -void init_queue(void) { +void init_queue(void) +{ queue_size = 0; queue_end = 0; queue = (queue_t *) malloc(sizeof(queue_t) * QUEUE_SIZE_CHUNK); if (queue == NULL) - { - fprintf(stderr, "Fill queue cannot be malloc()'d\n"); - return; - } + { + fprintf(stderr, "Fill queue cannot be malloc()'d\n"); + return; + } queue_size = QUEUE_SIZE_CHUNK; } -void add_to_queue(int x, int y, int y_outside) { +void add_to_queue(int x, int y, int y_outside) +{ /* Reallocate if we need more space */ if (queue_end + 1 > queue_size) + { + queue_t *tmp; + tmp = + (queue_t *) realloc(queue, + sizeof(queue_t) * (queue_size + QUEUE_SIZE_CHUNK)); + if (tmp == NULL) { - queue_t * tmp; - tmp = (queue_t *) realloc(queue, sizeof(queue_t) * (queue_size + QUEUE_SIZE_CHUNK)); - if (tmp == NULL) - { - fprintf(stderr, "Fill queue cannot be realloc()'d\n"); - return; - } - queue_size += QUEUE_SIZE_CHUNK; -#ifdef DEBUG - printf("queue_size = %d\n", queue_size); - fflush(stdout); -#endif - queue = tmp; + fprintf(stderr, "Fill queue cannot be realloc()'d\n"); + return; } + queue_size += QUEUE_SIZE_CHUNK; +#ifdef DEBUG + printf("queue_size = %d\n", queue_size); + fflush(stdout); +#endif + queue = tmp; + } queue[queue_end].x = x; queue[queue_end].y = y; @@ -130,14 +140,15 @@ void add_to_queue(int x, int y, int y_outside) { #ifdef DEBUG if (queue_end % 100 == 0) - { - printf("queue_end = %d\n", queue_end); - fflush(stdout); - } + { + printf("queue_end = %d\n", queue_end); + fflush(stdout); + } #endif } -int remove_from_queue(int * x, int * y, int * y_outside) { +int remove_from_queue(int *x, int *y, int *y_outside) +{ if (queue_end == 0) return 0; @@ -149,16 +160,17 @@ int remove_from_queue(int * x, int * y, int * y_outside) { #ifdef DEBUG if (queue_end % 100 == 0) - { - printf("queue_end = %d\n", queue_end); - fflush(stdout); - } + { + printf("queue_end = %d\n", queue_end); + fflush(stdout); + } #endif return 1; } -void cleanup_queue(void) { +void cleanup_queue(void) +{ if (queue != NULL) free(queue); @@ -177,51 +189,60 @@ double colors_close(SDL_Surface * canvas, Uint32 c1, Uint32 c2) Uint8 r1, g1, b1, r2, g2, b2; if (c1 == c2) - { - /* Get it over with quick, if possible! */ + { + /* Get it over with quick, if possible! */ - return 0.0; - } + return 0.0; + } else - { - double r, g, b; + { + double r, g, b; - SDL_GetRGB(c1, canvas->format, &r1, &g1, &b1); - SDL_GetRGB(c2, canvas->format, &r2, &g2, &b2); + SDL_GetRGB(c1, canvas->format, &r1, &g1, &b1); + SDL_GetRGB(c2, canvas->format, &r2, &g2, &b2); - // use distance in linear RGB space - r = sRGB_to_linear_table[r1] - sRGB_to_linear_table[r2]; - r *= r; - g = sRGB_to_linear_table[g1] - sRGB_to_linear_table[g2]; - g *= g; - b = sRGB_to_linear_table[b1] - sRGB_to_linear_table[b2]; - b *= b; + // use distance in linear RGB space + r = sRGB_to_linear_table[r1] - sRGB_to_linear_table[r2]; + r *= r; + g = sRGB_to_linear_table[g1] - sRGB_to_linear_table[g2]; + g *= g; + b = sRGB_to_linear_table[b1] - sRGB_to_linear_table[b2]; + b *= b; - // easy to confuse: - // dark grey, brown, purple - // light grey, tan - // red, orange - return (r + g + b); - } + // easy to confuse: + // dark grey, brown, purple + // light grey, tan + // red, orange + return (r + g + b); + } } int would_flood_fill(SDL_Surface * canvas, Uint32 cur_colr, Uint32 old_colr) { if (colors_close(canvas, cur_colr, old_colr) < COLOR_MATCH_NARROW) - { - return 0; - } else { - return 1; - } + { + return 0; + } + else + { + return 1; + } } -void do_flood_fill(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, SDL_Surface * last, SDL_Surface * canvas, int x, int y, Uint32 cur_colr, Uint32 old_colr, int * x1, int * y1, int * x2, int * y2, Uint8 * touched) +void do_flood_fill(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer, SDL_Surface * last, + SDL_Surface * canvas, int x, int y, Uint32 cur_colr, + Uint32 old_colr, int *x1, int *y1, int *x2, int *y2, + Uint8 * touched) { - simulate_flood_fill(screen, texture, renderer, last, canvas, x, y, cur_colr, old_colr, x1, y1, x2, y2, touched); + simulate_flood_fill(screen, texture, renderer, last, canvas, x, y, cur_colr, + old_colr, x1, y1, x2, y2, touched); } -Uint32 blend(SDL_Surface * canvas, Uint32 draw_colr, Uint32 old_colr, double pct) { +Uint32 blend(SDL_Surface * canvas, Uint32 draw_colr, Uint32 old_colr, + double pct) +{ Uint8 old_r, old_g, old_b, draw_r, draw_g, draw_b, new_r, new_g, new_b; SDL_GetRGB(draw_colr, canvas->format, &draw_r, &draw_g, &draw_b); @@ -235,7 +256,12 @@ Uint32 blend(SDL_Surface * canvas, Uint32 draw_colr, Uint32 old_colr, double pct return SDL_MapRGB(canvas->format, new_r, new_g, new_b); } -void simulate_flood_fill(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, SDL_Surface * last, SDL_Surface * canvas, int x, int y, Uint32 cur_colr, Uint32 old_colr, int * extent_x1, int * extent_y1, int * extent_x2, int * extent_y2, Uint8 * touched) { +void simulate_flood_fill(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer, SDL_Surface * last, + SDL_Surface * canvas, int x, int y, Uint32 cur_colr, + Uint32 old_colr, int *extent_x1, int *extent_y1, + int *extent_x2, int *extent_y2, Uint8 * touched) +{ int y_outside; /* Get ready */ @@ -257,9 +283,10 @@ void simulate_flood_fill(SDL_Surface * screen, SDL_Texture * texture, SDL_Render /* Do the work (possibly queuing more, as we go) */ while (remove_from_queue(&x, &y, &y_outside)) - { - simulate_flood_fill_outside_check(screen, texture, renderer, x, y, y_outside); - } + { + simulate_flood_fill_outside_check(screen, texture, renderer, x, y, + y_outside); + } cleanup_queue(); *extent_x1 = global_extent_x1; @@ -268,7 +295,10 @@ void simulate_flood_fill(SDL_Surface * screen, SDL_Texture * texture, SDL_Render *extent_y2 = global_extent_y2; } -void simulate_flood_fill_outside_check(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, int x, int y, int y_outside) +void simulate_flood_fill_outside_check(SDL_Surface * screen, + SDL_Texture * texture, + SDL_Renderer * renderer, int x, int y, + int y_outside) { int fillL, fillR, narrowFillL, narrowFillR, i, outside, just_queued; double in_line, closeness; @@ -287,13 +317,13 @@ void simulate_flood_fill_outside_check(SDL_Surface * screen, SDL_Texture * textu return; if (y < global_extent_y1) - { - global_extent_y1 = y; - } + { + global_extent_y1 = y; + } if (y > global_extent_y2) - { - global_extent_y2 = y; - } + { + global_extent_y2 = y; + } fillL = x; @@ -303,139 +333,172 @@ void simulate_flood_fill_outside_check(SDL_Surface * screen, SDL_Texture * textu global_prog_anim++; if ((global_prog_anim % 8) == 0) - { - show_progress_bar_(screen, texture, renderer); + { + show_progress_bar_(screen, texture, renderer); - } + } - if ((global_prog_anim % 800) == 1) /* Always lay sound _once_ */ + if ((global_prog_anim % 800) == 1) /* Always lay sound _once_ */ playsound(global_canvas, 1, SND_FILL, 1, x, SNDDIST_NEAR); #ifdef DEBUG_WATCH if (global_prog_anim % 100 == 0) - { - SDL_BlitSurface(global_canvas, NULL, global_screen, NULL); - SDL_Flip(global_screen); - } + { + SDL_BlitSurface(global_canvas, NULL, global_screen, NULL); + SDL_Flip(global_screen); + } #endif /* Find left side, filling along the way */ - px_colr = getpixels[global_last->format->BytesPerPixel] (global_last, fillL /* - 1 */, y); + px_colr = + getpixels[global_last->format->BytesPerPixel] (global_last, + fillL /* - 1 */ , y); in_line = colors_close(global_canvas, px_colr, global_old_colr); outside = 0; while (in_line < COLOR_MATCH_WIDE && outside < WIDE_MATCH_THRESHOLD) + { + if (in_line > COLOR_MATCH_NARROW) { - if (in_line > COLOR_MATCH_NARROW) { - outside++; - } else { - narrowFillL = fillL; - } - - if (global_touched != NULL) { - touch_byt = (255 - ((Uint8) (in_line * 85))); - if (touch_byt == 0) - touch_byt = 1; - - global_touched[(y * global_canvas->w) + fillL] = touch_byt; - } - - px_colr = getpixels[global_last->format->BytesPerPixel] (global_last, fillL, y); - putpixels[global_canvas->format->BytesPerPixel] (global_canvas, fillL, y, blend(global_canvas, global_cur_colr, px_colr, (1.0 - in_line))); - fillL--; - - px_colr = getpixels[global_last->format->BytesPerPixel] (global_last, fillL, y); - - if (fillL >= 0) - { - in_line = colors_close(global_canvas, px_colr, global_old_colr); - } - else - { - in_line = 3.0; - } + outside++; } + else + { + narrowFillL = fillL; + } + + if (global_touched != NULL) + { + touch_byt = (255 - ((Uint8) (in_line * 85))); + if (touch_byt == 0) + touch_byt = 1; + + global_touched[(y * global_canvas->w) + fillL] = touch_byt; + } + + px_colr = + getpixels[global_last->format->BytesPerPixel] (global_last, fillL, y); + putpixels[global_canvas->format->BytesPerPixel] (global_canvas, fillL, y, + blend(global_canvas, + global_cur_colr, + px_colr, + (1.0 - in_line))); + fillL--; + + px_colr = + getpixels[global_last->format->BytesPerPixel] (global_last, fillL, y); + + if (fillL >= 0) + { + in_line = colors_close(global_canvas, px_colr, global_old_colr); + } + else + { + in_line = 3.0; + } + } if (fillL >= 0) + { + if (global_touched != NULL) { - if (global_touched != NULL) - { - touch_byt = (255 - ((Uint8) (in_line * 85))); - if (touch_byt == 0) - touch_byt = 1; + touch_byt = (255 - ((Uint8) (in_line * 85))); + if (touch_byt == 0) + touch_byt = 1; - global_touched[(y * global_canvas->w) + fillL] = touch_byt; - } - - px_colr = getpixels[global_last->format->BytesPerPixel] (global_last, fillL, y); - putpixels[global_canvas->format->BytesPerPixel] (global_canvas, fillL, y, blend(global_canvas, global_cur_colr, px_colr, (1.0 - in_line))); + global_touched[(y * global_canvas->w) + fillL] = touch_byt; } + px_colr = + getpixels[global_last->format->BytesPerPixel] (global_last, fillL, y); + putpixels[global_canvas->format->BytesPerPixel] (global_canvas, fillL, y, + blend(global_canvas, + global_cur_colr, + px_colr, + (1.0 - in_line))); + } + if (fillL < global_extent_x1) - { - global_extent_x1 = fillL; - } + { + global_extent_x1 = fillL; + } fillL++; /* Find right side, filling along the way */ - px_colr = getpixels[global_last->format->BytesPerPixel] (global_last, fillR + 1, y); + px_colr = + getpixels[global_last->format->BytesPerPixel] (global_last, fillR + 1, y); in_line = colors_close(global_canvas, px_colr, global_old_colr); outside = 0; while (in_line < COLOR_MATCH_WIDE && outside < WIDE_MATCH_THRESHOLD) + { + if (in_line > COLOR_MATCH_NARROW) { - if (in_line > COLOR_MATCH_NARROW) { - outside++; - } else { - narrowFillR = fillR; - } - - if (global_touched != NULL) { - touch_byt = (255 - ((Uint8) (in_line * 85))); - if (touch_byt == 0) - touch_byt = 1; - - global_touched[(y * global_canvas->w) + fillR] = touch_byt; - } - - px_colr = getpixels[global_last->format->BytesPerPixel] (global_last, fillR, y); - putpixels[global_canvas->format->BytesPerPixel] (global_canvas, fillR, y, blend(global_canvas, global_cur_colr, px_colr, (1.0 - in_line))); - fillR++; - - px_colr = getpixels[global_last->format->BytesPerPixel] (global_last, fillR, y); - - if (fillR < global_canvas->w) - { - in_line = colors_close(global_canvas, px_colr, global_old_colr); - } - else - { - in_line = 3.0; - } + outside++; } + else + { + narrowFillR = fillR; + } + + if (global_touched != NULL) + { + touch_byt = (255 - ((Uint8) (in_line * 85))); + if (touch_byt == 0) + touch_byt = 1; + + global_touched[(y * global_canvas->w) + fillR] = touch_byt; + } + + px_colr = + getpixels[global_last->format->BytesPerPixel] (global_last, fillR, y); + putpixels[global_canvas->format->BytesPerPixel] (global_canvas, fillR, y, + blend(global_canvas, + global_cur_colr, + px_colr, + (1.0 - in_line))); + fillR++; + + px_colr = + getpixels[global_last->format->BytesPerPixel] (global_last, fillR, y); + + if (fillR < global_canvas->w) + { + in_line = colors_close(global_canvas, px_colr, global_old_colr); + } + else + { + in_line = 3.0; + } + } if (fillR < global_canvas->w) + { + if (global_touched != NULL) { - if (global_touched != NULL) - { - touch_byt = (255 - ((Uint8) (in_line * 85))); - if (touch_byt == 0) - touch_byt = 1; + touch_byt = (255 - ((Uint8) (in_line * 85))); + if (touch_byt == 0) + touch_byt = 1; - global_touched[(y * global_canvas->w) + fillR] = touch_byt; - } - - px_colr = getpixels[global_last->format->BytesPerPixel] (global_last, fillR, y); - putpixels[global_canvas->format->BytesPerPixel] (global_canvas, fillR, y, blend(global_canvas, global_cur_colr, px_colr, (1.0 - in_line))); + global_touched[(y * global_canvas->w) + fillR] = touch_byt; } + px_colr = + getpixels[global_last->format->BytesPerPixel] (global_last, fillR, y); + putpixels[global_canvas->format->BytesPerPixel] (global_canvas, fillR, y, + blend(global_canvas, + global_cur_colr, + px_colr, + (1.0 - in_line))); + } + if (fillR > global_extent_x2) - { - global_extent_x2 = fillR; - } + { + global_extent_x2 = fillR; + } fillR--; @@ -444,69 +507,74 @@ void simulate_flood_fill_outside_check(SDL_Surface * screen, SDL_Texture * textu just_queued = 0; if (y > 0) + { + for (i = narrowFillL; i <= narrowFillR; i++) { - for (i = narrowFillL; i <= narrowFillR; i++) + px_colr = + getpixels[global_last->format->BytesPerPixel] (global_last, i, y - 1); + closeness = colors_close(global_canvas, px_colr, global_old_colr); + if (closeness < COLOR_MATCH_NARROW || + (closeness < COLOR_MATCH_WIDE && y_outside < WIDE_MATCH_THRESHOLD)) + { + if (!just_queued + && (global_touched == NULL + || !global_touched[((y - 1) * global_canvas->w) + i])) { - px_colr = getpixels[global_last->format->BytesPerPixel] (global_last, i, y - 1); - closeness = colors_close(global_canvas, px_colr, global_old_colr); - if (closeness < COLOR_MATCH_NARROW || - (closeness < COLOR_MATCH_WIDE && y_outside < WIDE_MATCH_THRESHOLD) - ) - { - if (!just_queued && (global_touched == NULL || !global_touched[((y - 1) * global_canvas->w) + i])) - { - add_to_queue(i, y - 1, y_outside + 1); - just_queued = 1; - } - else - { - just_queued = 0; - } - } - else - { - just_queued = 0; - } + add_to_queue(i, y - 1, y_outside + 1); + just_queued = 1; } + else + { + just_queued = 0; + } + } + else + { + just_queued = 0; + } } + } /* Continue filling downwards from this scanline */ just_queued = 0; if (y < global_canvas->h - 1) + { + for (i = narrowFillL; i <= narrowFillR; i++) { - for (i = narrowFillL; i <= narrowFillR; i++) + px_colr = + getpixels[global_last->format->BytesPerPixel] (global_last, i, y + 1); + closeness = colors_close(global_canvas, px_colr, global_old_colr); + if (closeness < COLOR_MATCH_NARROW || + (closeness < COLOR_MATCH_WIDE && y_outside < WIDE_MATCH_THRESHOLD)) + { + if (!just_queued + && (global_touched == NULL + || !global_touched[((y + 1) * global_canvas->w) + i])) { - px_colr = getpixels[global_last->format->BytesPerPixel] (global_last, i, y + 1); - closeness = colors_close(global_canvas, px_colr, global_old_colr); - if (closeness < COLOR_MATCH_NARROW || - (closeness < COLOR_MATCH_WIDE && y_outside < WIDE_MATCH_THRESHOLD) - ) - { - if (!just_queued && (global_touched == NULL || !global_touched[((y + 1) * global_canvas->w) + i])) - { - add_to_queue(i, y + 1, y_outside + 1); - just_queued = 1; - } - else - { - just_queued = 0; - } - } - else - { - just_queued = 0; - } + add_to_queue(i, y + 1, y_outside + 1); + just_queued = 1; } + else + { + just_queued = 0; + } + } + else + { + just_queued = 0; + } } + } } void draw_linear_gradient(SDL_Surface * canvas, SDL_Surface * last, - int x_left, int y_top, int x_right, int y_bottom, - int x1, int y1, int x2, int y2, Uint32 draw_color, Uint8 * touched -) { + int x_left, int y_top, int x_right, int y_bottom, + int x1, int y1, int x2, int y2, Uint32 draw_color, + Uint8 * touched) +{ Uint32 old_colr, new_colr; int xx, yy; Uint8 draw_r, draw_g, draw_b, old_r, old_g, old_b, new_r, new_g, new_b; @@ -521,9 +589,12 @@ void draw_linear_gradient(SDL_Surface * canvas, SDL_Surface * last, C2 = (A * x2) + (B * y2); /* FIXME: C2 should be larger than C1? */ - for (yy = y_top; yy <= y_bottom; yy++) { - for (xx = x_left; xx <= x_right; xx++) { - if (touched[(yy * canvas->w) + xx]) { + for (yy = y_top; yy <= y_bottom; yy++) + { + for (xx = x_left; xx <= x_right; xx++) + { + if (touched[(yy * canvas->w) + xx]) + { /* Get the old color, and blend it (with a distance-based ratio) with the target color */ old_colr = getpixels[last->format->BytesPerPixel] (last, xx, yy); SDL_GetRGB(old_colr, last->format, &old_r, &old_g, &old_b); @@ -532,13 +603,18 @@ void draw_linear_gradient(SDL_Surface * canvas, SDL_Surface * last, https://stackoverflow.com/questions/521493/creating-a-linear-gradient-in-2d-array) */ C = (A * xx) + (B * yy); - if (C < C1) { + if (C < C1) + { /* At/beyond the click spot (opposite direction of mouse); solid color */ ratio = 0.0; - } else if (C >= C2) { + } + else if (C >= C2) + { /* At/beyond the mouse; completely faded out */ ratio = 1.0; - } else { + } + else + { /* The actual gradient... */ ratio = (C - C1) / (C2 - C1); } @@ -546,9 +622,15 @@ void draw_linear_gradient(SDL_Surface * canvas, SDL_Surface * last, /* Apply fuzziness at any antialiased edges we detected */ ratio = (ratio * ((float) touched[yy * canvas->w + xx] / 255.0)); - new_r = (Uint8) (((float) old_r) * ratio + ((float) draw_r * (1.0 - ratio))); - new_g = (Uint8) (((float) old_g) * ratio + ((float) draw_g * (1.0 - ratio))); - new_b = (Uint8) (((float) old_b) * ratio + ((float) draw_b * (1.0 - ratio))); + new_r = + (Uint8) (((float) old_r) * ratio + + ((float) draw_r * (1.0 - ratio))); + new_g = + (Uint8) (((float) old_g) * ratio + + ((float) draw_g * (1.0 - ratio))); + new_b = + (Uint8) (((float) old_b) * ratio + + ((float) draw_b * (1.0 - ratio))); new_colr = SDL_MapRGB(canvas->format, new_r, new_g, new_b); putpixels[canvas->format->BytesPerPixel] (canvas, xx, yy, new_colr); @@ -557,27 +639,32 @@ void draw_linear_gradient(SDL_Surface * canvas, SDL_Surface * last, } } -void draw_brush_fill_single(SDL_Surface * canvas, int x, int y, Uint32 draw_color, Uint8 * touched) { +void draw_brush_fill_single(SDL_Surface * canvas, int x, int y, + Uint32 draw_color, Uint8 * touched) +{ int xx, yy; for (yy = -16; yy < 16; yy++) + { + for (xx = -16; xx < 16; xx++) { - for (xx = -16; xx < 16; xx++) - { - if ((xx * xx) + (yy * yy) < (16 * 16) && - touched[((y + yy) * canvas->w) + (x + xx)]) - { - putpixels[canvas->format->BytesPerPixel] (canvas, x + xx, y + yy, draw_color); - } - } + if ((xx * xx) + (yy * yy) < (16 * 16) && + touched[((y + yy) * canvas->w) + (x + xx)]) + { + putpixels[canvas->format->BytesPerPixel] (canvas, x + xx, y + yy, + draw_color); + } } + } } void draw_brush_fill(SDL_Surface * canvas, - int x_left ATTRIBUTE_UNUSED, int y_top ATTRIBUTE_UNUSED, int x_right ATTRIBUTE_UNUSED, int y_bottom ATTRIBUTE_UNUSED, - int x1, int y1, int x2, int y2, Uint32 draw_color, Uint8 * touched, - int * up_x1, int * up_y1, int * up_x2, int * up_y2 -) { + int x_left ATTRIBUTE_UNUSED, int y_top ATTRIBUTE_UNUSED, + int x_right ATTRIBUTE_UNUSED, + int y_bottom ATTRIBUTE_UNUSED, int x1, int y1, int x2, + int y2, Uint32 draw_color, Uint8 * touched, int *up_x1, + int *up_y1, int *up_x2, int *up_y2) +{ int dx, dy; int y; int orig_x1, orig_y1, orig_x2, orig_y2, tmp; @@ -593,60 +680,60 @@ void draw_brush_fill(SDL_Surface * canvas, dy = y2 - y1; if (dx != 0) + { + m = ((float) dy) / ((float) dx); + b = y1 - m * x1; + + if (x2 >= x1) + dx = 1; + else + dx = -1; + + while (x1 != x2) { - m = ((float)dy) / ((float)dx); - b = y1 - m * x1; + y1 = m * x1 + b; + y2 = m * (x1 + dx) + b; - if (x2 >= x1) - dx = 1; - else - dx = -1; - - while (x1 != x2) - { - y1 = m * x1 + b; - y2 = m * (x1 + dx) + b; - - if (y1 > y2) - { - for (y = y1; y >= y2; y--) - draw_brush_fill_single(canvas, x1, y, draw_color, touched); - } - else - { - for (y = y1; y <= y2; y++) - draw_brush_fill_single(canvas, x1, y, draw_color, touched); - } - - x1 = x1 + dx; - } - } - else - { if (y1 > y2) - { - y = y1; - y1 = y2; - y2 = y; - } + { + for (y = y1; y >= y2; y--) + draw_brush_fill_single(canvas, x1, y, draw_color, touched); + } + else + { + for (y = y1; y <= y2; y++) + draw_brush_fill_single(canvas, x1, y, draw_color, touched); + } - for (y = y1; y <= y2; y++) - draw_brush_fill_single(canvas, x1, y, draw_color, touched); + x1 = x1 + dx; } + } + else + { + if (y1 > y2) + { + y = y1; + y1 = y2; + y2 = y; + } + + for (y = y1; y <= y2; y++) + draw_brush_fill_single(canvas, x1, y, draw_color, touched); + } if (orig_x1 > orig_x2) - { - tmp = orig_x1; - orig_x1 = orig_x2; - orig_x2 = tmp; - } + { + tmp = orig_x1; + orig_x1 = orig_x2; + orig_x2 = tmp; + } if (orig_y1 > orig_y2) - { - tmp = orig_y1; - orig_y1 = orig_y2; - orig_y2 = tmp; - } + { + tmp = orig_y1; + orig_y1 = orig_y2; + orig_y2 = tmp; + } *up_x1 = orig_x1 - 16; *up_y1 = orig_y1 - 16; @@ -654,9 +741,10 @@ void draw_brush_fill(SDL_Surface * canvas, *up_y2 = orig_y2 + 16; } -void draw_radial_gradient(SDL_Surface * canvas, int x_left, int y_top, int x_right, int y_bottom, - int x, int y, Uint32 draw_color, Uint8 * touched -) { +void draw_radial_gradient(SDL_Surface * canvas, int x_left, int y_top, + int x_right, int y_bottom, int x, int y, + Uint32 draw_color, Uint8 * touched) +{ Uint32 old_colr, new_colr; int xx, yy; float xd, yd, dist, rad, ratio; @@ -666,7 +754,8 @@ void draw_radial_gradient(SDL_Surface * canvas, int x_left, int y_top, int x_rig xd = max(abs(x - x_right), abs(x - x_left)); yd = max(abs(y - y_bottom), abs(y - y_top)); rad = sqrt(xd * xd + yd * yd); - if (rad == 0) { + if (rad == 0) + { return; } @@ -674,27 +763,38 @@ void draw_radial_gradient(SDL_Surface * canvas, int x_left, int y_top, int x_rig SDL_GetRGB(draw_color, canvas->format, &draw_r, &draw_g, &draw_b); /* Traverse the flood-filled zone */ - for (yy = y_top; yy <= y_bottom; yy++) { - for (xx = x_left; xx <= x_right; xx++) { + for (yy = y_top; yy <= y_bottom; yy++) + { + for (xx = x_left; xx <= x_right; xx++) + { /* Only alter the pixels within the flood itself */ - if (touched[(yy * canvas->w) + xx]) { + if (touched[(yy * canvas->w) + xx]) + { /* Determine the distance from the click point */ xd = fabs((float) (xx - x)); yd = fabs((float) (yy - y)); dist = sqrt(xd * xd + yd * yd); - if (dist < rad) { + if (dist < rad) + { ratio = (dist / rad); /* Get the old color, and blend it (with a distance-based ratio) with the target color */ - old_colr = getpixels[canvas->format->BytesPerPixel] (canvas, xx, yy); + old_colr = + getpixels[canvas->format->BytesPerPixel] (canvas, xx, yy); SDL_GetRGB(old_colr, canvas->format, &old_r, &old_g, &old_b); /* Apply fuzziness at any antialiased edges we detected */ ratio = (ratio * ((float) touched[yy * canvas->w + xx] / 255.0)); - new_r = (Uint8) (((float) old_r) * ratio + ((float) draw_r * (1.00 - ratio))); - new_g = (Uint8) (((float) old_g) * ratio + ((float) draw_g * (1.00 - ratio))); - new_b = (Uint8) (((float) old_b) * ratio + ((float) draw_b * (1.00 - ratio))); + new_r = + (Uint8) (((float) old_r) * ratio + + ((float) draw_r * (1.00 - ratio))); + new_g = + (Uint8) (((float) old_g) * ratio + + ((float) draw_g * (1.00 - ratio))); + new_b = + (Uint8) (((float) old_b) * ratio + + ((float) draw_b * (1.00 - ratio))); new_colr = SDL_MapRGB(canvas->format, new_r, new_g, new_b); putpixels[canvas->format->BytesPerPixel] (canvas, xx, yy, new_colr); @@ -703,4 +803,3 @@ void draw_radial_gradient(SDL_Surface * canvas, int x_left, int y_top, int x_rig } } } - diff --git a/src/fill.h b/src/fill.h index 83467aa5a..dbb4a6e1d 100644 --- a/src/fill.h +++ b/src/fill.h @@ -37,17 +37,26 @@ #include "SDL.h" int would_flood_fill(SDL_Surface * canvas, Uint32 cur_colr, Uint32 old_colr); -void do_flood_fill(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, SDL_Surface * last, SDL_Surface * canvas, int x, int y, Uint32 cur_colr, Uint32 old_colr, int * x1, int * y1, int * x2, int * y2, Uint8 * touched); -void simulate_flood_fill(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, SDL_Surface * last, SDL_Surface * canvas, int x, int y, Uint32 cur_colr, Uint32 old_colr, int * x1, int * y1, int * x2, int * y2, Uint8 * touched); +void do_flood_fill(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer, SDL_Surface * last, + SDL_Surface * canvas, int x, int y, Uint32 cur_colr, + Uint32 old_colr, int *x1, int *y1, int *x2, int *y2, + Uint8 * touched); +void simulate_flood_fill(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer, SDL_Surface * last, + SDL_Surface * canvas, int x, int y, Uint32 cur_colr, + Uint32 old_colr, int *x1, int *y1, int *x2, int *y2, + Uint8 * touched); void draw_linear_gradient(SDL_Surface * canvas, SDL_Surface * last, - int x_left, int y_top, int x_right, int y_bottom, - int x1, int y1, int x2, int y2, Uint32 draw_color, Uint8 * touched); -void draw_radial_gradient(SDL_Surface * canvas, int x_left, int y_top, int x_right, int y_bottom, - int x, int y, Uint32 draw_color, Uint8 * touched); -void draw_brush_fill(SDL_Surface * canvas, - int x_left, int y_top, int x_right, int y_bottom, - int x1, int y1, int x2, int y2, Uint32 draw_color, Uint8 * touched, - int * up_x1, int * up_y1, int * up_x2, int * up_y2); + int x_left, int y_top, int x_right, int y_bottom, + int x1, int y1, int x2, int y2, Uint32 draw_color, + Uint8 * touched); +void draw_radial_gradient(SDL_Surface * canvas, int x_left, int y_top, + int x_right, int y_bottom, int x, int y, + Uint32 draw_color, Uint8 * touched); +void draw_brush_fill(SDL_Surface * canvas, int x_left, int y_top, int x_right, + int y_bottom, int x1, int y1, int x2, int y2, + Uint32 draw_color, Uint8 * touched, int *up_x1, + int *up_y1, int *up_x2, int *up_y2); #endif - diff --git a/src/fill_tools.h b/src/fill_tools.h index 45362c620..70c1bbb8b 100644 --- a/src/fill_tools.h +++ b/src/fill_tools.h @@ -38,7 +38,8 @@ #define gettext_noop(String) String #endif -enum { +enum +{ FILL_FLOOD, FILL_BRUSH, FILL_GRADIENT_LINEAR, @@ -56,8 +57,10 @@ const char *const fill_names[NUM_FILLS] = { const char *const fill_tips[NUM_FILLS] = { gettext_noop("Click to fill an area with a solid color."), gettext_noop("Click and drag to fill an area by hand, using a brush."), - gettext_noop("Click and drag to fill an area with a linear gradient (from the chosen color to transparent)."), - gettext_noop("Click to fill an area with a radial gradient (from the chosen color to transparent).") + gettext_noop + ("Click and drag to fill an area with a linear gradient (from the chosen color to transparent)."), + gettext_noop + ("Click to fill an area with a radial gradient (from the chosen color to transparent).") }; const char *const fill_img_fnames[NUM_FILLS] = { @@ -68,4 +71,3 @@ const char *const fill_img_fnames[NUM_FILLS] = { }; #endif - diff --git a/src/fonts.c b/src/fonts.c index ebf0b3de4..10d187787 100644 --- a/src/fonts.c +++ b/src/fonts.c @@ -173,39 +173,40 @@ TuxPaint_Font *load_locale_font(TuxPaint_Font * fallback, int size) TuxPaint_Font *ret = NULL; if (!need_own_font) - { - return fallback; - } + { + return fallback; + } else - { - char str[128]; + { + char str[128]; - snprintf(str, sizeof(str), "%sfonts/locale/%s.ttf", DATA_PREFIX, lang_prefix); + snprintf(str, sizeof(str), "%sfonts/locale/%s.ttf", DATA_PREFIX, + lang_prefix); - ret = TuxPaint_Font_OpenFont("", str, size); + ret = TuxPaint_Font_OpenFont("", str, size); #ifdef __APPLE__ - if (!ret) - { - snprintf(str, sizeof(str), "%sfonts/%s.ttf", DATA_PREFIX, lang_prefix); - ret = TuxPaint_Font_OpenFont("", str, size); - } + if (!ret) + { + snprintf(str, sizeof(str), "%sfonts/%s.ttf", DATA_PREFIX, lang_prefix); + ret = TuxPaint_Font_OpenFont("", str, size); + } - if (!ret) - { - snprintf(str, sizeof(str), "/Library/Fonts/%s.ttf", lang_prefix); - ret = TuxPaint_Font_OpenFont("", str, size); - } + if (!ret) + { + snprintf(str, sizeof(str), "/Library/Fonts/%s.ttf", lang_prefix); + ret = TuxPaint_Font_OpenFont("", str, size); + } - if (!ret) - { - snprintf(str, sizeof(str), "%s/%s.ttf", apple_fontsPath(), lang_prefix); - ret = TuxPaint_Font_OpenFont("", str, size); - } + if (!ret) + { + snprintf(str, sizeof(str), "%s/%s.ttf", apple_fontsPath(), lang_prefix); + ret = TuxPaint_Font_OpenFont("", str, size); + } #endif - return ret ? ret : fallback; - } + return ret ? ret : fallback; + } } #endif // ifdef NO_SDLPANGO @@ -213,50 +214,54 @@ TuxPaint_Font *load_locale_font(TuxPaint_Font * fallback, int size) void TuxPaint_Font_CloseFont(TuxPaint_Font * tpf) { #ifdef DEBUG - printf("%s:%d - TuxPaint_Font_CloseFont step 1 (%p)\n", __FILE__, __LINE__, tpf); + printf("%s:%d - TuxPaint_Font_CloseFont step 1 (%p)\n", __FILE__, __LINE__, + tpf); #endif if (!tpf) return; #ifndef NO_SDLPANGO #ifdef DEBUG - printf("%s:%d - TuxPaint_Font_CloseFont step 2 (%p, %d)\n", __FILE__, __LINE__, tpf->pango_context, tpf->typ); + printf("%s:%d - TuxPaint_Font_CloseFont step 2 (%p, %d)\n", __FILE__, + __LINE__, tpf->pango_context, tpf->typ); #endif if (tpf->typ == FONT_TYPE_PANGO) if (tpf->pango_context) - { -#ifndef __APPLE__ //EP added ifdef because SDLPango_FreeContext sometimes crashed with "pointer being freed was not allocated" - SDLPango_FreeContext(tpf->pango_context); + { +#ifndef __APPLE__ //EP added ifdef because SDLPango_FreeContext sometimes crashed with "pointer being freed was not allocated" + SDLPango_FreeContext(tpf->pango_context); #endif - tpf->pango_context = NULL; - } + tpf->pango_context = NULL; + } #endif #ifdef DEBUG - printf("%s:%d - TuxPaint_Font_CloseFont step 3 (%p, %d)\n", __FILE__, __LINE__, tpf->ttf_font, tpf->typ); + printf("%s:%d - TuxPaint_Font_CloseFont step 3 (%p, %d)\n", __FILE__, + __LINE__, tpf->ttf_font, tpf->typ); fflush(stdout); #endif if (tpf->typ == FONT_TYPE_TTF) if (tpf->ttf_font) - { - TTF_CloseFont(tpf->ttf_font); - tpf->ttf_font = NULL; - } + { + TTF_CloseFont(tpf->ttf_font); + tpf->ttf_font = NULL; + } if (tpf->desc != NULL) - { - free(tpf->desc); - tpf->desc = NULL; - } + { + free(tpf->desc); + tpf->desc = NULL; + } free(tpf); } -TuxPaint_Font *TuxPaint_Font_OpenFont(const char *pangodesc, const char *ttffilename, int size) +TuxPaint_Font *TuxPaint_Font_OpenFont(const char *pangodesc, + const char *ttffilename, int size) { TTF_Font *ttf_font; TuxPaint_Font *tpf = NULL; - char * familyname; + char *familyname; int i; #ifndef NO_SDLPANGO @@ -264,130 +269,147 @@ TuxPaint_Font *TuxPaint_Font_OpenFont(const char *pangodesc, const char *ttffile #endif #ifdef DEBUG - printf("%s:%d - OpenFont(pango:\"%s\", ttf:\"%s\")\n", __FILE__, __LINE__, pangodesc, ttffilename); + printf("%s:%d - OpenFont(pango:\"%s\", ttf:\"%s\")\n", __FILE__, __LINE__, + pangodesc, ttffilename); #endif #ifndef NO_SDLPANGO if (pangodesc != NULL && pangodesc[0] != '\0') + { + tpf = (TuxPaint_Font *) malloc(sizeof(TuxPaint_Font)); + tpf->typ = FONT_TYPE_PANGO; + snprintf(desc, sizeof(desc), "%s %d", pangodesc, (size * 3) / 4); + tpf->desc = strdup(desc); + +#ifdef DEBUG + printf("%s:%d - Creating Pango context: \"%s\"\n", __FILE__, __LINE__, + desc); +#endif + + tpf->pango_context = SDLPango_CreateContext_GivenFontDesc(desc); + if (tpf->pango_context == NULL) { - tpf = (TuxPaint_Font *) malloc(sizeof(TuxPaint_Font)); - tpf->typ = FONT_TYPE_PANGO; - snprintf(desc, sizeof(desc), "%s %d", pangodesc, (size * 3) / 4); - tpf->desc = strdup(desc); - #ifdef DEBUG - printf("%s:%d - Creating Pango context: \"%s\"\n", __FILE__, __LINE__, desc); + printf("%s:%d - Failed to load %s\n", __FILE__, __LINE__, desc); #endif - - tpf->pango_context = SDLPango_CreateContext_GivenFontDesc(desc); - if (tpf->pango_context == NULL) - { -#ifdef DEBUG - printf("%s:%d - Failed to load %s\n", __FILE__, __LINE__, desc); -#endif - free(tpf); - tpf = NULL; - } - else - tpf->height = size; /* FIXME: Is this accurate!? -bjk 2007.07.12 */ - -#ifdef DEBUG - printf("%s:%d - TuxPaint_Font_OpenFont(\"%s\", \"%s\", %d) done (SDL_Pango)\n\n", __FILE__, __LINE__, pangodesc, ttffilename, size); - fflush(stdout); -#endif - - return (tpf); + free(tpf); + tpf = NULL; } + else + tpf->height = size; /* FIXME: Is this accurate!? -bjk 2007.07.12 */ + +#ifdef DEBUG + printf + ("%s:%d - TuxPaint_Font_OpenFont(\"%s\", \"%s\", %d) done (SDL_Pango)\n\n", + __FILE__, __LINE__, pangodesc, ttffilename, size); + fflush(stdout); +#endif + + return (tpf); + } #endif // #ifndef NO_SDLPANGO /* -- Did not, at this point, load the font using SDL_Pango -- */ if (ttffilename != NULL && ttffilename[0] != '\0') + { +#ifdef DEBUG + printf("%s:%d - Considering loading TTF \"%s\"\n", __FILE__, __LINE__, + ttffilename); + fflush(stdout); +#endif + + i = 0; + while (problemFonts[i] != NULL) { + if (!strcmp(ttffilename, problemFonts[i++])) + { #ifdef DEBUG - printf("%s:%d - Considering loading TTF \"%s\"\n", __FILE__, __LINE__, ttffilename); - fflush(stdout); + fprintf(stderr, "Notice: Skipping problematic font: \"%s\"\n", + ttffilename); #endif - - i = 0; - while (problemFonts[i] != NULL) - { - if (!strcmp(ttffilename, problemFonts[i++])) - { -#ifdef DEBUG - fprintf(stderr, "Notice: Skipping problematic font: \"%s\"\n", ttffilename); -#endif - return NULL; /* bail on known problematic fonts that cause TTF_OpenFont to crash */ - } - } - - i = 0; - while (problemFontExtensions[i] != NULL) - { - if (strstr(ttffilename, problemFontExtensions[i++])) - { -#ifdef DEBUG - fprintf(stderr, "Notice: Skipping font with problematic extension: \"%s\"\n", ttffilename); -#endif - return NULL; /* bail on known problematic font types that cause TTF_OpenFont to crash */ - } - } - - ttf_font = TTF_OpenFont(ttffilename, size); - - if (ttf_font == NULL) { - fprintf(stderr, "Cannot open TTF font '%s' (size %d)\n", ttffilename, size); - return NULL; + return NULL; /* bail on known problematic fonts that cause TTF_OpenFont to crash */ } + } - familyname = TTF_FontFaceFamilyName(ttf_font); /* N.B.: I don't believe we're supposed to free() this... -bjk 2021.10.26 */ - (void) familyname; // avoid compiler complaints if NO_SDLPANGO is set, or ALWAYS_LOAD_FONT_WITH_PANGO is not set, and DEBUG is not set + i = 0; + while (problemFontExtensions[i] != NULL) + { + if (strstr(ttffilename, problemFontExtensions[i++])) + { +#ifdef DEBUG + fprintf(stderr, + "Notice: Skipping font with problematic extension: \"%s\"\n", + ttffilename); +#endif + return NULL; /* bail on known problematic font types that cause TTF_OpenFont to crash */ + } + } + + ttf_font = TTF_OpenFont(ttffilename, size); + + if (ttf_font == NULL) + { + fprintf(stderr, "Cannot open TTF font '%s' (size %d)\n", ttffilename, + size); + return NULL; + } + + familyname = TTF_FontFaceFamilyName(ttf_font); /* N.B.: I don't believe we're supposed to free() this... -bjk 2021.10.26 */ + (void) familyname; // avoid compiler complaints if NO_SDLPANGO is set, or ALWAYS_LOAD_FONT_WITH_PANGO is not set, and DEBUG is not set #ifdef DEBUG - printf("%s:%d - Loaded %s (\"%s\")\n", __FILE__, __LINE__, ttffilename, (familyname != NULL ? familyname : "")); - fflush(stdout); + printf("%s:%d - Loaded %s (\"%s\")\n", __FILE__, __LINE__, ttffilename, + (familyname != NULL ? familyname : "")); + fflush(stdout); #endif /* This is currently disabled, as it is under construction (and may be moot under SDL2) -bjk 2021.10.28 */ #ifdef ALWAYS_LOAD_FONT_WITH_PANGO #ifndef NO_SDLPANGO - /* -- Try loading the font with Pango, instead! */ - tpf = TuxPaint_Font_OpenFont(familyname, "", size); - if (tpf != NULL) { - /* Success! Clean up and return the TuxPaint_Font that we got back */ + /* -- Try loading the font with Pango, instead! */ + tpf = TuxPaint_Font_OpenFont(familyname, "", size); + if (tpf != NULL) + { + /* Success! Clean up and return the TuxPaint_Font that we got back */ #ifdef DEBUG - printf("%s:%d - Loaded via SDL_Pango!\n", __FILE__, __LINE__); - printf("%s:%d - TuxPaint_Font_OpenFont(\"%s\", \"%s\", %d) done (SDL_ttf -> SDL_Pango)\n\n", __FILE__, __LINE__, pangodesc, ttffilename, size); - fflush(stdout); + printf("%s:%d - Loaded via SDL_Pango!\n", __FILE__, __LINE__); + printf + ("%s:%d - TuxPaint_Font_OpenFont(\"%s\", \"%s\", %d) done (SDL_ttf -> SDL_Pango)\n\n", + __FILE__, __LINE__, pangodesc, ttffilename, size); + fflush(stdout); #endif - TTF_CloseFont(ttf_font); - return(tpf); - } + TTF_CloseFont(ttf_font); + return (tpf); + } #endif // #ifndef NO_SDLPANGO #endif - /* -- Proceed with loading the TTF font file using SDL_ttf */ + /* -- Proceed with loading the TTF font file using SDL_ttf */ - tpf = (TuxPaint_Font *) malloc(sizeof(TuxPaint_Font)); - tpf->typ = FONT_TYPE_TTF; - tpf->ttf_font = ttf_font; - tpf->desc = strdup(ttffilename); + tpf = (TuxPaint_Font *) malloc(sizeof(TuxPaint_Font)); + tpf->typ = FONT_TYPE_TTF; + tpf->ttf_font = ttf_font; + tpf->desc = strdup(ttffilename); #ifdef DEBUG - printf("%s:%d - Succeeded loading %s via SDL_ttf\n", __FILE__, __LINE__, ttffilename); + printf("%s:%d - Succeeded loading %s via SDL_ttf\n", __FILE__, __LINE__, + ttffilename); #endif - tpf->height = TTF_FontHeight(tpf->ttf_font); + tpf->height = TTF_FontHeight(tpf->ttf_font); #ifdef DEBUG - printf("%s:%d - TuxPaint_Font_OpenFont(\"%s\", \"%s\", %d) done (SDL_ttf)\n\n", __FILE__, __LINE__, pangodesc, ttffilename, size); - fflush(stdout); + printf + ("%s:%d - TuxPaint_Font_OpenFont(\"%s\", \"%s\", %d) done (SDL_ttf)\n\n", + __FILE__, __LINE__, pangodesc, ttffilename, size); + fflush(stdout); #endif - return (tpf); - } + return (tpf); + } fprintf(stderr, "TuxPaint_Font_OpenFont() called with no loadable font\n"); return NULL; @@ -401,30 +423,31 @@ void reliable_write(int fd, const void *buf, size_t count) struct pollfd p; do - { - ssize_t rc = write(fd, buf, count); + { + ssize_t rc = write(fd, buf, count); - if (rc == -1) + if (rc == -1) + { + switch (errno) + { + default: + return; + case EAGAIN: + case ENOSPC: + ; // satisfy a C syntax abomination + p = (struct pollfd) { - switch (errno) - { - default: - return; - case EAGAIN: - case ENOSPC: - ; // satisfy a C syntax abomination - p = (struct pollfd) - { - fd, POLLOUT, 0}; - poll(&p, 1, -1); // try not to burn CPU time - // FALL THROUGH - case EINTR: - continue; - } - } - buf += rc; - count -= rc; + fd, POLLOUT, 0 + }; + poll(&p, 1, -1); // try not to burn CPU time + // FALL THROUGH + case EINTR: + continue; + } } + buf += rc; + count -= rc; + } while (count); } @@ -434,31 +457,32 @@ static void reliable_read(int fd, void *buf, size_t count) struct pollfd p; do - { - ssize_t rc = read(fd, buf, count); + { + ssize_t rc = read(fd, buf, count); - if (rc == -1) + if (rc == -1) + { + switch (errno) + { + default: + return; + case EAGAIN: + ; // satisfy a C syntax abomination + p = (struct pollfd) { - switch (errno) - { - default: - return; - case EAGAIN: - ; // satisfy a C syntax abomination - p = (struct pollfd) - { - fd, POLLIN, 0}; - poll(&p, 1, -1); // try not to burn CPU time - // FALL THROUGH - case EINTR: - continue; - } - } - if (rc == 0) - break; // EOF. Better not happen before the end! - buf += rc; - count -= rc; + fd, POLLIN, 0 + }; + poll(&p, 1, -1); // try not to burn CPU time + // FALL THROUGH + case EINTR: + continue; + } } + if (rc == 0) + break; // EOF. Better not happen before the end! + buf += rc; + count -= rc; + } while (count); } @@ -478,14 +502,14 @@ static void groupfonts_range(style_info ** base, int count) #if 0 // THREADED_FONTS if (count < 1 || count > 4) + { + printf("\n::::::: %d styles in %s:\n", count, base[0]->family); + i = count; + while (i--) { - printf("\n::::::: %d styles in %s:\n", count, base[0]->family); - i = count; - while (i--) - { - printf(" %s\n", base[i]->style); - } + printf(" %s\n", base[i]->style); } + } #endif i = count; @@ -498,90 +522,92 @@ static void groupfonts_range(style_info ** base, int count) i = 4; while (i--) - { - if (!boldcounts[i]) - continue; - if (i > boldmax) - boldmax = i; - if (i < boldmin) - boldmin = i; - bolduse++; - } + { + if (!boldcounts[i]) + continue; + if (i > boldmax) + boldmax = i; + if (i < boldmin) + boldmin = i; + bolduse++; + } if (likely(bolduse <= 2)) - { - // in case they are same, we want non-bold, - // so that setting goes second - boldmap[boldmax] = 1; - boldmap[boldmin] = 0; - } + { + // in case they are same, we want non-bold, + // so that setting goes second + boldmap[boldmax] = 1; + boldmap[boldmin] = 0; + } else if (count == 3) + { + int boldmid; + int zmin = 0, zmid = 0, zmax = 0; + + boldmap[boldmax] = 1; + boldmap[boldmin] = 0; + boldmid = boldcounts[boldmin + 1] ? boldmin + 1 : boldmin + 2; + + i = 3; + while (i--) { - int boldmid; - int zmin = 0, zmid = 0, zmax = 0; - - boldmap[boldmax] = 1; - boldmap[boldmin] = 0; - boldmid = boldcounts[boldmin + 1] ? boldmin + 1 : boldmin + 2; - - i = 3; - while (i--) - { - if (base[i]->boldness == boldmin) - zmin = base[i]->italic; - if (base[i]->boldness == boldmid) - zmid = base[i]->italic; - if (base[i]->boldness == boldmax) - zmax = base[i]->italic; - } - if (zmin != zmid) - boldmap[boldmid] = 0; - else if (zmid != zmax) - boldmap[boldmid] = 1; - else if (boldmin == 0 && boldmid == 1) - { - boldmap[0] = -1; - boldmap[1] = 0; - } + if (base[i]->boldness == boldmin) + zmin = base[i]->italic; + if (base[i]->boldness == boldmid) + zmid = base[i]->italic; + if (base[i]->boldness == boldmax) + zmax = base[i]->italic; } - else + if (zmin != zmid) + boldmap[boldmid] = 0; + else if (zmid != zmax) + boldmap[boldmid] = 1; + else if (boldmin == 0 && boldmid == 1) { - int claimed_bold = boldcounts[3]; - int claimed_norm = boldcounts[1]; - - // 3 or 4 boldness levels, 4 or more styles! - // This is going to be random hacks and hopes. - - // bold is bold - boldmap[3] = 1; - - // norm is norm + boldmap[0] = -1; boldmap[1] = 0; - - // classify demi-bold or medium - if (claimed_bold < 2) - { - boldmap[2] = 1; - claimed_bold += boldcounts[2]; - } - else if (claimed_norm < 2) - { - boldmap[2] = 0; - claimed_norm += boldcounts[2]; - } - - // classify lightface - if (claimed_norm < 2) - { - boldmap[0] = 0; - //claimed_norm += boldcounts[0]; - } } + } + else + { + int claimed_bold = boldcounts[3]; + int claimed_norm = boldcounts[1]; + + // 3 or 4 boldness levels, 4 or more styles! + // This is going to be random hacks and hopes. + + // bold is bold + boldmap[3] = 1; + + // norm is norm + boldmap[1] = 0; + + // classify demi-bold or medium + if (claimed_bold < 2) + { + boldmap[2] = 1; + claimed_bold += boldcounts[2]; + } + else if (claimed_norm < 2) + { + boldmap[2] = 0; + claimed_norm += boldcounts[2]; + } + + // classify lightface + if (claimed_norm < 2) + { + boldmap[0] = 0; + //claimed_norm += boldcounts[0]; + } + } if (num_font_families == num_font_families_max) - { - num_font_families_max = num_font_families_max * 5 / 4 + 30; - user_font_families = realloc(user_font_families, num_font_families_max * sizeof *user_font_families); - } + { + num_font_families_max = num_font_families_max * 5 / 4 + 30; + user_font_families = + realloc(user_font_families, + num_font_families_max * sizeof *user_font_families); + } fi = calloc(1, sizeof *fi); user_font_families[num_font_families++] = fi; @@ -590,50 +616,53 @@ static void groupfonts_range(style_info ** base, int count) fi->score = base[0]->truetype + base[0]->score; i = count; while (i--) - { - int b = boldmap[base[i]->boldness]; + { + int b = boldmap[base[i]->boldness]; - if (b == -1) - { + if (b == -1) + { #if 0 // THREADED_FONTS - printf("too many boldness levels, discarding: %s, %s\n", base[i]->family, base[i]->style); + printf("too many boldness levels, discarding: %s, %s\n", + base[i]->family, base[i]->style); #endif - continue; - } - spot = b ? TTF_STYLE_BOLD : 0; - spot += base[i]->italic ? TTF_STYLE_ITALIC : 0; - if (fi->filename[spot]) - { -#if 0 -// THREADED_FONTS - printf("duplicates, discarding: %s, %s\n", base[i]->family, base[i]->style); - printf("b %d, spot %d\n", b, spot); - printf("occupancy %p %p %p %p\n", fi->filename[0], fi->filename[1], fi->filename[2], fi->filename[3]); -#endif - continue; - } - fi->filename[spot] = strdup(base[i]->filename); - fi->score += 2; + continue; } + spot = b ? TTF_STYLE_BOLD : 0; + spot += base[i]->italic ? TTF_STYLE_ITALIC : 0; + if (fi->filename[spot]) + { +#if 0 +// THREADED_FONTS + printf("duplicates, discarding: %s, %s\n", base[i]->family, + base[i]->style); + printf("b %d, spot %d\n", b, spot); + printf("occupancy %p %p %p %p\n", fi->filename[0], fi->filename[1], + fi->filename[2], fi->filename[3]); +#endif + continue; + } + fi->filename[spot] = strdup(base[i]->filename); + fi->score += 2; + } if (!fi->filename[0] && !fi->filename[1]) - { - fi->filename[0] = fi->filename[2]; - fi->filename[2] = NULL; - fi->filename[1] = fi->filename[3]; - fi->filename[3] = NULL; - } + { + fi->filename[0] = fi->filename[2]; + fi->filename[2] = NULL; + fi->filename[1] = fi->filename[3]; + fi->filename[3] = NULL; + } if (!fi->filename[0] && !fi->filename[2]) - { - fi->filename[0] = fi->filename[1]; - fi->filename[1] = NULL; - fi->filename[2] = fi->filename[3]; - fi->filename[3] = NULL; - } + { + fi->filename[0] = fi->filename[1]; + fi->filename[1] = NULL; + fi->filename[2] = fi->filename[3]; + fi->filename[3] = NULL; + } if (!fi->filename[0]) - { - fi->filename[0] = strdup(fi->filename[TTF_STYLE_BOLD]); - } + { + fi->filename[0] = strdup(fi->filename[TTF_STYLE_BOLD]); + } } @@ -706,121 +735,121 @@ static void parse_font_style(style_info * si) while (*sp) + { + if (*sp == ' ') { - if (*sp == ' ') - { - sp++; - continue; - } - if (!strncasecmp(sp, "Bold", strlen("Bold"))) - { - sp += strlen("Bold"); - have_bold = 1; - continue; - } - if (!strncasecmp(sp, "Regular", strlen("Regular"))) - { - sp += strlen("Regular"); - continue; - } - if (!strncasecmp(sp, "Italic", strlen("Italic"))) - { - sp += strlen("Italic"); - si->italic = 1; - continue; - } - if (!strncasecmp(sp, "Oblique", strlen("Oblique"))) - { - sp += strlen("Oblique"); - si->italic = 1; - continue; - } - // move " Condensed" from style to family - if (!strncasecmp(sp, "Condensed", strlen("Condensed"))) - { - size_t len = strlen(si->family); - char *name = malloc(len + strlen(" Condensed") + 1); + sp++; + continue; + } + if (!strncasecmp(sp, "Bold", strlen("Bold"))) + { + sp += strlen("Bold"); + have_bold = 1; + continue; + } + if (!strncasecmp(sp, "Regular", strlen("Regular"))) + { + sp += strlen("Regular"); + continue; + } + if (!strncasecmp(sp, "Italic", strlen("Italic"))) + { + sp += strlen("Italic"); + si->italic = 1; + continue; + } + if (!strncasecmp(sp, "Oblique", strlen("Oblique"))) + { + sp += strlen("Oblique"); + si->italic = 1; + continue; + } + // move " Condensed" from style to family + if (!strncasecmp(sp, "Condensed", strlen("Condensed"))) + { + size_t len = strlen(si->family); + char *name = malloc(len + strlen(" Condensed") + 1); - sp += strlen("Condensed"); - memcpy(name, si->family, len); - strcpy(name + len, " Condensed"); - free(si->family); - si->family = name; - continue; - } - if (!strncasecmp(sp, "Light", strlen("Light"))) - { - sp += strlen("Light"); - have_light = 1; - continue; - } - if (!strncasecmp(sp, "Medium", strlen("Medium"))) - { - sp += strlen("Medium"); - have_medium = 1; - continue; - } - if (!strncasecmp(sp, "Demi", strlen("Demi"))) - { - sp += strlen("Demi"); - have_demi = 1; - continue; - } - if (!strncasecmp(sp, "Heavy", strlen("Heavy"))) - { - sp += strlen("Heavy"); - have_heavy = 1; - continue; - } - if (!strncasecmp(sp, "Normal", strlen("Normal"))) - { - sp += strlen("Normal"); - continue; - } - if (!strncasecmp(sp, "Black", strlen("Black"))) - { - sp += strlen("Black"); - have_black = 1; - continue; - } - if (!strncasecmp(sp, "Roman", strlen("Roman"))) - { - sp += strlen("Roman"); - continue; - } - if (!strncasecmp(sp, "Book", strlen("Book"))) - { - sp += strlen("Book"); - continue; - } - if (!strncasecmp(sp, "Chancery", strlen("Chancery"))) - { - sp += strlen("Chancery"); - si->italic = 1; - continue; - } - if (!strncasecmp(sp, "Thin", strlen("Thin"))) - { - sp += strlen("Thin"); - have_light = 1; - continue; - } - if (!strncmp(sp, "LR", strlen("LR"))) - { - sp += strlen("LR"); - continue; - } + sp += strlen("Condensed"); + memcpy(name, si->family, len); + strcpy(name + len, " Condensed"); + free(si->family); + si->family = name; + continue; + } + if (!strncasecmp(sp, "Light", strlen("Light"))) + { + sp += strlen("Light"); + have_light = 1; + continue; + } + if (!strncasecmp(sp, "Medium", strlen("Medium"))) + { + sp += strlen("Medium"); + have_medium = 1; + continue; + } + if (!strncasecmp(sp, "Demi", strlen("Demi"))) + { + sp += strlen("Demi"); + have_demi = 1; + continue; + } + if (!strncasecmp(sp, "Heavy", strlen("Heavy"))) + { + sp += strlen("Heavy"); + have_heavy = 1; + continue; + } + if (!strncasecmp(sp, "Normal", strlen("Normal"))) + { + sp += strlen("Normal"); + continue; + } + if (!strncasecmp(sp, "Black", strlen("Black"))) + { + sp += strlen("Black"); + have_black = 1; + continue; + } + if (!strncasecmp(sp, "Roman", strlen("Roman"))) + { + sp += strlen("Roman"); + continue; + } + if (!strncasecmp(sp, "Book", strlen("Book"))) + { + sp += strlen("Book"); + continue; + } + if (!strncasecmp(sp, "Chancery", strlen("Chancery"))) + { + sp += strlen("Chancery"); + si->italic = 1; + continue; + } + if (!strncasecmp(sp, "Thin", strlen("Thin"))) + { + sp += strlen("Thin"); + have_light = 1; + continue; + } + if (!strncmp(sp, "LR", strlen("LR"))) + { + sp += strlen("LR"); + continue; + } - if (!stumped) - { - stumped = 1; + if (!stumped) + { + stumped = 1; #if 0 // THREADED_FONTS - printf("Font style parser stumped by \"%s\".\n", si->style); + printf("Font style parser stumped by \"%s\".\n", si->style); #endif - } - sp++; // bad: an unknown character } + sp++; // bad: an unknown character + } if (have_demi || have_medium) @@ -833,7 +862,8 @@ static void parse_font_style(style_info * si) si->boldness = 1; // we'll count both TrueType and OpenType - si->truetype = ! !strcasestr(si->filename, ".ttf") || ! !strcasestr(si->filename, ".otf"); + si->truetype = !!strcasestr(si->filename, ".ttf") + || !!strcasestr(si->filename, ".otf"); } @@ -844,21 +874,21 @@ static void dupe_markdown_range(family_info ** base, int count) int i = count; while (i--) - { - int score = base[i]->score; + { + int score = base[i]->score; - if (score <= bestscore) - continue; - bestscore = score; - bestslot = i; - } + if (score <= bestscore) + continue; + bestscore = score; + bestslot = i; + } i = count; while (i--) - { - if (i == bestslot) - continue; - base[i]->score = -999; - } + { + if (i == bestslot) + continue; + base[i]->score = -999; + } } @@ -871,58 +901,62 @@ static void groupfonts(void) while (i--) parse_font_style(user_font_styles[i]); - qsort(user_font_styles, num_font_styles, sizeof user_font_styles[0], compar_fontgroup); + qsort(user_font_styles, num_font_styles, sizeof user_font_styles[0], + compar_fontgroup); //printf("groupfonts() qsort(user_font_styles...)\n"); //fflush(stdout); for (;;) - { - int high = low; + { + int high = low; - if (low >= num_font_styles) + if (low >= num_font_styles) + break; + for (;;) + { + if (++high >= num_font_styles) + break; + if (compar_fontgroup(user_font_styles + low, user_font_styles + high)) break; - for (;;) - { - if (++high >= num_font_styles) - break; - if (compar_fontgroup(user_font_styles + low, user_font_styles + high)) - break; - } - groupfonts_range(user_font_styles + low, high - low); - low = high; } + groupfonts_range(user_font_styles + low, high - low); + low = high; + } i = num_font_styles; while (i--) - { - free(user_font_styles[i]->filename); - free(user_font_styles[i]->directory); - free(user_font_styles[i]->family); - free(user_font_styles[i]->style); - free(user_font_styles[i]); - } + { + free(user_font_styles[i]->filename); + free(user_font_styles[i]->directory); + free(user_font_styles[i]->family); + free(user_font_styles[i]->style); + free(user_font_styles[i]); + } free(user_font_styles); user_font_styles = NULL; // just to catch bugs - qsort(user_font_families, num_font_families, sizeof user_font_families[0], compar_fontkiller); + qsort(user_font_families, num_font_families, sizeof user_font_families[0], + compar_fontkiller); low = 0; for (;;) - { - int high = low; + { + int high = low; - if (low >= num_font_families) + if (low >= num_font_families) + break; + for (;;) + { + if (++high >= num_font_families) + break; + if (strcmp + (user_font_families[low]->family, user_font_families[high]->family)) break; - for (;;) - { - if (++high >= num_font_families) - break; - if (strcmp(user_font_families[low]->family, user_font_families[high]->family)) - break; - } - dupe_markdown_range(user_font_families + low, high - low); - low = high; } - qsort(user_font_families, num_font_families, sizeof user_font_families[0], compar_fontscore); + dupe_markdown_range(user_font_families + low, high - low); + low = high; + } + qsort(user_font_families, num_font_families, sizeof user_font_families[0], + compar_fontscore); //printf("groupfonts() qsort(user_font_families 2...)\n"); //fflush(stdout); if (num_font_families > 0 && user_font_families[0]->score < 0) @@ -931,23 +965,24 @@ static void groupfonts(void) // THREADED_FONTS printf("Trim starting with %d families\n", num_font_families); #endif - while (num_font_families > 1 && user_font_families[num_font_families - 1]->score < 0) - { - i = --num_font_families; - free(user_font_families[i]->directory); - free(user_font_families[i]->family); - cpp = user_font_families[i]->filename; - if (cpp[0]) - free(cpp[0]); - if (cpp[1]) - free(cpp[1]); - if (cpp[2]) - free(cpp[2]); - if (cpp[3]) - free(cpp[3]); - free(user_font_families[i]); - user_font_families[i] = NULL; - } + while (num_font_families > 1 + && user_font_families[num_font_families - 1]->score < 0) + { + i = --num_font_families; + free(user_font_families[i]->directory); + free(user_font_families[i]->family); + cpp = user_font_families[i]->filename; + if (cpp[0]) + free(cpp[0]); + if (cpp[1]) + free(cpp[1]); + if (cpp[2]) + free(cpp[2]); + if (cpp[3]) + free(cpp[3]); + free(user_font_families[i]); + user_font_families[i] = NULL; + } #if 0 // THREADED_FONTS printf("Trim ending with %d families\n", num_font_families); @@ -955,8 +990,11 @@ static void groupfonts(void) } -static void loadfonts_locale_filter(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, - const char *const dir, const char *restrict const locale) +static void loadfonts_locale_filter(SDL_Surface * screen, + SDL_Texture * texture, + SDL_Renderer * renderer, + const char *const dir, + const char *restrict const locale) { char buf[TP_FTW_PATHSIZE]; unsigned dirlen = strlen(dir); @@ -964,72 +1002,84 @@ static void loadfonts_locale_filter(SDL_Surface * screen, SDL_Texture * texture, DEBUG_PRINTF("Loading fonts from [%s]\n", dir); memcpy(buf, dir, dirlen); - tp_ftw(screen, texture, renderer, buf, dirlen, 1, loadfont_callback, locale); + tp_ftw(screen, texture, renderer, buf, dirlen, 1, loadfont_callback, + locale); } -static void loadfonts(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, const char *const dir) +static void loadfonts(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer, const char *const dir) { loadfonts_locale_filter(screen, texture, renderer, dir, NULL); } -/* static */ int load_user_fonts(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, void *vp, +/* static */ int load_user_fonts(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer, void *vp, const char *restrict const locale) { char *homedirdir; - (void)vp; // junk passed by threading library + (void) vp; // junk passed by threading library - loadfonts_locale_filter(screen, texture, renderer, DATA_PREFIX "fonts", locale); + loadfonts_locale_filter(screen, texture, renderer, DATA_PREFIX "fonts", + locale); if (!no_system_fonts) - { + { #ifdef WIN32 - homedirdir = GetSystemFontDir(); - loadfonts(screen, texture, renderer, homedirdir); - free(homedirdir); + homedirdir = GetSystemFontDir(); + loadfonts(screen, texture, renderer, homedirdir); + free(homedirdir); #elif defined(__BEOS__) - loadfonts(screen, texture, renderer, "/boot/home/config/font/ttffonts"); - loadfonts(screen, texture, renderer, "/usr/share/fonts"); - loadfonts(screen, texture, renderer, "/usr/X11R6/lib/X11/fonts"); + loadfonts(screen, texture, renderer, "/boot/home/config/font/ttffonts"); + loadfonts(screen, texture, renderer, "/usr/share/fonts"); + loadfonts(screen, texture, renderer, "/usr/X11R6/lib/X11/fonts"); #elif defined(__HAIKU__) - dev_t volume = dev_for_path("/boot"); - char buffer[B_PATH_NAME_LENGTH + B_FILE_NAME_LENGTH]; - status_t result; + dev_t volume = dev_for_path("/boot"); + char buffer[B_PATH_NAME_LENGTH + B_FILE_NAME_LENGTH]; + status_t result; - result = find_directory(B_SYSTEM_FONTS_DIRECTORY, volume, false, buffer, sizeof(buffer)); - loadfonts(screen, texture, renderer, buffer); - result = find_directory(B_SYSTEM_NONPACKAGED_FONTS_DIRECTORY, volume, false, buffer, sizeof(buffer)); - loadfonts(screen, texture, renderer, buffer); - result = find_directory(B_USER_FONTS_DIRECTORY, volume, false, buffer, sizeof(buffer)); - loadfonts(screen, texture, renderer, buffer); - result = find_directory(B_USER_NONPACKAGED_FONTS_DIRECTORY, volume, false, buffer, sizeof(buffer)); - loadfonts(screen, texture, renderer, buffer); + result = + find_directory(B_SYSTEM_FONTS_DIRECTORY, volume, false, buffer, + sizeof(buffer)); + loadfonts(screen, texture, renderer, buffer); + result = + find_directory(B_SYSTEM_NONPACKAGED_FONTS_DIRECTORY, volume, false, + buffer, sizeof(buffer)); + loadfonts(screen, texture, renderer, buffer); + result = + find_directory(B_USER_FONTS_DIRECTORY, volume, false, buffer, + sizeof(buffer)); + loadfonts(screen, texture, renderer, buffer); + result = + find_directory(B_USER_NONPACKAGED_FONTS_DIRECTORY, volume, false, + buffer, sizeof(buffer)); + loadfonts(screen, texture, renderer, buffer); #elif defined(__APPLE__) - loadfonts(screen, texture, renderer, "/System/Library/Fonts"); - loadfonts(screen, texture, renderer, "/Library/Fonts"); - loadfonts(screen, texture, renderer, apple_fontsPath()); - loadfonts(screen, texture, renderer, "/usr/share/fonts"); - loadfonts(screen, texture, renderer, "/usr/X11R6/lib/X11/fonts"); + loadfonts(screen, texture, renderer, "/System/Library/Fonts"); + loadfonts(screen, texture, renderer, "/Library/Fonts"); + loadfonts(screen, texture, renderer, apple_fontsPath()); + loadfonts(screen, texture, renderer, "/usr/share/fonts"); + loadfonts(screen, texture, renderer, "/usr/X11R6/lib/X11/fonts"); #elif defined(__ANDROID__) - loadfonts(screen, texture, renderer, "data/fonts"); - loadfonts(screen, texture, renderer, "/system/fonts"); + loadfonts(screen, texture, renderer, "data/fonts"); + loadfonts(screen, texture, renderer, "/system/fonts"); #elif defined(__sun__) - loadfonts(screen, texture, renderer, "/usr/openwin/lib/X11/fonts"); - loadfonts(screen, texture, renderer, "/usr/share/fonts"); - loadfonts(screen, texture, renderer, "/usr/X11R6/lib/X11/fonts"); + loadfonts(screen, texture, renderer, "/usr/openwin/lib/X11/fonts"); + loadfonts(screen, texture, renderer, "/usr/share/fonts"); + loadfonts(screen, texture, renderer, "/usr/X11R6/lib/X11/fonts"); #else - loadfonts(screen, texture, renderer, "/usr/share/feh/fonts"); - loadfonts(screen, texture, renderer, "/usr/share/fonts"); - loadfonts(screen, texture, renderer, "/usr/X11R6/lib/X11/fonts"); - loadfonts(screen, texture, renderer, "/usr/share/texmf/fonts"); - loadfonts(screen, texture, renderer, "/usr/share/grace/fonts/type1"); - loadfonts(screen, texture, renderer, "/usr/share/hatman/fonts"); - loadfonts(screen, texture, renderer, "/usr/share/icewm/themes/jim-mac"); - loadfonts(screen, texture, renderer, "/usr/share/vlc/skins2/fonts"); - loadfonts(screen, texture, renderer, "/usr/share/xplanet/fonts"); + loadfonts(screen, texture, renderer, "/usr/share/feh/fonts"); + loadfonts(screen, texture, renderer, "/usr/share/fonts"); + loadfonts(screen, texture, renderer, "/usr/X11R6/lib/X11/fonts"); + loadfonts(screen, texture, renderer, "/usr/share/texmf/fonts"); + loadfonts(screen, texture, renderer, "/usr/share/grace/fonts/type1"); + loadfonts(screen, texture, renderer, "/usr/share/hatman/fonts"); + loadfonts(screen, texture, renderer, "/usr/share/icewm/themes/jim-mac"); + loadfonts(screen, texture, renderer, "/usr/share/vlc/skins2/fonts"); + loadfonts(screen, texture, renderer, "/usr/share/xplanet/fonts"); #endif - } + } homedirdir = get_fname("fonts", DIR_DATA); loadfonts(screen, texture, renderer, homedirdir); @@ -1042,8 +1092,10 @@ static void loadfonts(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer #endif #ifdef __APPLE__ - homedirdir = malloc(snprintf(NULL, 0, "%s/fonts", apple_globalPreferencesPath()) + 1); - if(homedirdir) { + homedirdir = + malloc(snprintf(NULL, 0, "%s/fonts", apple_globalPreferencesPath()) + 1); + if (homedirdir) + { sprintf(homedirdir, "%s/fonts", apple_globalPreferencesPath()); loadfonts(screen, texture, renderer, homedirdir); free(homedirdir); @@ -1051,7 +1103,8 @@ static void loadfonts(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer #endif #ifdef DEBUG - printf("%s:%d - Grouping %d fonts...\n", __FILE__, __LINE__, num_font_styles); + printf("%s:%d - Grouping %d fonts...\n", __FILE__, __LINE__, + num_font_styles); fflush(stdout); #endif @@ -1071,7 +1124,8 @@ static void loadfonts(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer #ifdef FORKED_FONTS -void run_font_scanner(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, +void run_font_scanner(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer, const char *restrict const locale) { int sv[2]; @@ -1082,18 +1136,18 @@ void run_font_scanner(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer exit(42); font_scanner_pid = fork(); if (font_scanner_pid) - { - // parent (or error -- but we're screwed in that case) - font_socket_fd = sv[0]; - close(sv[1]); - return; - } + { + // parent (or error -- but we're screwed in that case) + font_socket_fd = sv[0]; + close(sv[1]); + return; + } #ifndef __HAIKU__ /* be nice, letting the main thread get the CPU */ if (nice(42) == -1) - { - fprintf(stderr, "Font scanner thread can't nice() itself\n"); - } + { + fprintf(stderr, "Font scanner thread can't nice() itself\n"); + } #endif sched_yield(); // try to let the parent run right now prctl(PR_SET_PDEATHSIG, 9); // get killed if parent exits @@ -1111,103 +1165,105 @@ void run_font_scanner(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer size = 0; i = num_font_families; while (i--) - { - char *s; + { + char *s; - s = user_font_families[i]->directory; - if (s) - size += strlen(s); - s = user_font_families[i]->family; - if (s) - size += strlen(s); - s = user_font_families[i]->filename[0]; - if (s) - size += strlen(s); - s = user_font_families[i]->filename[1]; - if (s) - size += strlen(s); - s = user_font_families[i]->filename[2]; - if (s) - size += strlen(s); - s = user_font_families[i]->filename[3]; - if (s) - size += strlen(s); - size += 6; // for '\0' on each of the above - } + s = user_font_families[i]->directory; + if (s) + size += strlen(s); + s = user_font_families[i]->family; + if (s) + size += strlen(s); + s = user_font_families[i]->filename[0]; + if (s) + size += strlen(s); + s = user_font_families[i]->filename[1]; + if (s) + size += strlen(s); + s = user_font_families[i]->filename[2]; + if (s) + size += strlen(s); + s = user_font_families[i]->filename[3]; + if (s) + size += strlen(s); + size += 6; // for '\0' on each of the above + } size += 2; // for 2-byte font count buf = malloc(size); walk = buf; #ifdef DEBUG - printf("%s:%d - Sending %u bytes with %u families.\n", __FILE__, __LINE__, size, num_font_families); + printf("%s:%d - Sending %u bytes with %u families.\n", __FILE__, __LINE__, + size, num_font_families); #endif *walk++ = num_font_families & 0xffu; *walk++ = num_font_families >> 8u; i = num_font_families; while (i--) + { + int len; + char *s; + + s = user_font_families[i]->directory; + if (s) { - int len; - char *s; - - s = user_font_families[i]->directory; - if (s) - { - len = strlen(s); - memcpy(walk, s, len); - walk += len; - } - *walk++ = '\0'; - - s = user_font_families[i]->family; - if (s) - { - len = strlen(s); - memcpy(walk, s, len); - walk += len; - } - *walk++ = '\0'; - - s = user_font_families[i]->filename[0]; - if (s) - { - len = strlen(s); - memcpy(walk, s, len); - walk += len; - } - *walk++ = '\0'; - - s = user_font_families[i]->filename[1]; - if (s) - { - len = strlen(s); - memcpy(walk, s, len); - walk += len; - } - *walk++ = '\0'; - - s = user_font_families[i]->filename[2]; - if (s) - { - len = strlen(s); - memcpy(walk, s, len); - walk += len; - } - *walk++ = '\0'; - - s = user_font_families[i]->filename[3]; - if (s) - { - len = strlen(s); - memcpy(walk, s, len); - walk += len; - } - *walk++ = '\0'; + len = strlen(s); + memcpy(walk, s, len); + walk += len; } + *walk++ = '\0'; + + s = user_font_families[i]->family; + if (s) + { + len = strlen(s); + memcpy(walk, s, len); + walk += len; + } + *walk++ = '\0'; + + s = user_font_families[i]->filename[0]; + if (s) + { + len = strlen(s); + memcpy(walk, s, len); + walk += len; + } + *walk++ = '\0'; + + s = user_font_families[i]->filename[1]; + if (s) + { + len = strlen(s); + memcpy(walk, s, len); + walk += len; + } + *walk++ = '\0'; + + s = user_font_families[i]->filename[2]; + if (s) + { + len = strlen(s); + memcpy(walk, s, len); + walk += len; + } + *walk++ = '\0'; + + s = user_font_families[i]->filename[3]; + if (s) + { + len = strlen(s); + memcpy(walk, s, len); + walk += len; + } + *walk++ = '\0'; + } reliable_write(font_socket_fd, buf, size); _exit(0); } -void receive_some_font_info(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer) +void receive_some_font_info(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer) { char *buf = NULL; unsigned buf_size = 0; @@ -1221,60 +1277,63 @@ void receive_some_font_info(SDL_Surface * screen, SDL_Texture * texture, SDL_Ren fcntl(font_socket_fd, F_SETFL, O_NONBLOCK); for (;;) + { + if (buf_size <= buf_fill * 9 / 8 + 128) { - if (buf_size <= buf_fill * 9 / 8 + 128) - { - buf_size = buf_size * 5 / 4 + 256; + buf_size = buf_size * 5 / 4 + 256; - // FIXME: Valgrind says this leaks -bjk 2007.07.19 - buf = realloc(buf, buf_size); - } - rc = read(font_socket_fd, buf + buf_fill, buf_size - buf_fill); + // FIXME: Valgrind says this leaks -bjk 2007.07.19 + buf = realloc(buf, buf_size); + } + rc = read(font_socket_fd, buf + buf_fill, buf_size - buf_fill); #ifdef DEBUG - printf("%s:%d - read: fd=%d buf_fill=%u buf_size=%u rc=%ld\n", __FILE__, __LINE__, font_socket_fd, buf_fill, buf_size, (long int)rc); + printf("%s:%d - read: fd=%d buf_fill=%u buf_size=%u rc=%ld\n", __FILE__, + __LINE__, font_socket_fd, buf_fill, buf_size, (long int) rc); #endif - if (rc == -1) + if (rc == -1) + { + switch (errno) + { + default: + return; + case EAGAIN: + ; // satisfy a C syntax abomination + p = (struct pollfd) { - switch (errno) - { - default: - return; - case EAGAIN: - ; // satisfy a C syntax abomination - p = (struct pollfd) - { - font_socket_fd, POLLIN, 0}; - show_progress_bar_(screen, texture, renderer); - poll(&p, 1, 29); // try not to burn CPU time - continue; - case EINTR: - continue; - } - } - buf_fill += rc; - if (!rc || font_thread_aborted) - break; + font_socket_fd, POLLIN, 0 + }; + show_progress_bar_(screen, texture, renderer); + poll(&p, 1, 29); // try not to burn CPU time + continue; + case EINTR: + continue; + } } + buf_fill += rc; + if (!rc || font_thread_aborted) + break; + } close(font_socket_fd); waitpid(font_scanner_pid, &status, 0); if (WIFSIGNALED(status) || font_thread_aborted) - { - fprintf(stderr, "child killed by signal %u\n", WTERMSIG(status)); - user_font_families = NULL; - num_font_families = 0; - font_thread_done = 1; + { + fprintf(stderr, "child killed by signal %u\n", WTERMSIG(status)); + user_font_families = NULL; + num_font_families = 0; + font_thread_done = 1; - return; - } + return; + } show_progress_bar_(screen, texture, renderer); walk = buf; - num_font_families = *(unsigned char *)walk++; - num_font_families += *(unsigned char *)walk++ << 8u; + num_font_families = *(unsigned char *) walk++; + num_font_families += *(unsigned char *) walk++ << 8u; #ifdef DEBUG - printf("%s:%d - Got %u bytes with %u families.\n", __FILE__, __LINE__, buf_fill, num_font_families); + printf("%s:%d - Got %u bytes with %u families.\n", __FILE__, __LINE__, + buf_fill, num_font_families); #endif user_font_families = malloc(num_font_families * sizeof *user_font_families); @@ -1283,39 +1342,39 @@ void receive_some_font_info(SDL_Surface * screen, SDL_Texture * texture, SDL_Ren i = num_font_families; while (i--) - { - unsigned len; + { + unsigned len; - user_font_families[i] = fip + i; + user_font_families[i] = fip + i; - len = strlen(walk); - user_font_families[i]->directory = len ? walk : NULL; - walk += len + 1; + len = strlen(walk); + user_font_families[i]->directory = len ? walk : NULL; + walk += len + 1; - len = strlen(walk); - user_font_families[i]->family = len ? walk : NULL; - walk += len + 1; + len = strlen(walk); + user_font_families[i]->family = len ? walk : NULL; + walk += len + 1; - len = strlen(walk); - user_font_families[i]->filename[0] = len ? walk : NULL; - walk += len + 1; + len = strlen(walk); + user_font_families[i]->filename[0] = len ? walk : NULL; + walk += len + 1; - len = strlen(walk); - user_font_families[i]->filename[1] = len ? walk : NULL; - walk += len + 1; + len = strlen(walk); + user_font_families[i]->filename[1] = len ? walk : NULL; + walk += len + 1; - len = strlen(walk); - user_font_families[i]->filename[2] = len ? walk : NULL; - walk += len + 1; + len = strlen(walk); + user_font_families[i]->filename[2] = len ? walk : NULL; + walk += len + 1; - len = strlen(walk); - user_font_families[i]->filename[3] = len ? walk : NULL; - walk += len + 1; + len = strlen(walk); + user_font_families[i]->filename[3] = len ? walk : NULL; + walk += len + 1; - user_font_families[i]->handle = NULL; + user_font_families[i]->handle = NULL; - // score left uninitialized - } + // score left uninitialized + } font_thread_done = 1; } @@ -1338,47 +1397,50 @@ TuxPaint_Font *getfonthandle(int desire) #endif if (fi == NULL) - { + { #ifdef DEBUG - printf("%s:%d - getfonthandle(%d) points to a NULL family\n", __FILE__, __LINE__, desire); - fflush(stdout); + printf("%s:%d - getfonthandle(%d) points to a NULL family\n", __FILE__, + __LINE__, desire); + fflush(stdout); #endif - return NULL; - } + return NULL; + } if (fi->filename != NULL) - { + { #ifdef DEBUG - printf("%s:%d - Setting 'name' to fi->filename[%d (0x%x)]\n", __FILE__, __LINE__, (int)text_state, (int)text_state); - fflush(stdout); + printf("%s:%d - Setting 'name' to fi->filename[%d (0x%x)]\n", __FILE__, + __LINE__, (int) text_state, (int) text_state); + fflush(stdout); #endif - name = fi->filename[text_state]; + name = fi->filename[text_state]; #ifdef DEBUG - printf("%s:%d - Which is: %s\n", __FILE__, __LINE__, name); - fflush(stdout); + printf("%s:%d - Which is: %s\n", __FILE__, __LINE__, name); + fflush(stdout); #endif - } + } else - { + { #ifdef DEBUG - printf("%s:%d - fi->filename is NULL\n", __FILE__, __LINE__); - fflush(stdout); + printf("%s:%d - fi->filename is NULL\n", __FILE__, __LINE__); + fflush(stdout); #endif - name = NULL; - } + name = NULL; + } if (fi->handle) - { + { #ifdef DEBUG - printf("%s:%d - fi->handle was set (0x%x)\n", __FILE__, __LINE__, (int)(intptr_t) fi->handle); + printf("%s:%d - fi->handle was set (0x%x)\n", __FILE__, __LINE__, + (int) (intptr_t) fi->handle); - fflush(stdout); + fflush(stdout); #endif - return fi->handle; - } + return fi->handle; + } #ifdef DEBUG printf("%s:%d - fi->handle was not yet set\n", __FILE__, __LINE__); @@ -1410,28 +1472,28 @@ TuxPaint_Font *getfonthandle(int desire) */ { if (!name) - { - name = fi->filename[text_state ^ TTF_STYLE_ITALIC]; - missing = text_state & TTF_STYLE_ITALIC; - } + { + name = fi->filename[text_state ^ TTF_STYLE_ITALIC]; + missing = text_state & TTF_STYLE_ITALIC; + } if (!name) - { - name = fi->filename[text_state ^ TTF_STYLE_BOLD]; - missing = text_state & TTF_STYLE_BOLD; - } + { + name = fi->filename[text_state ^ TTF_STYLE_BOLD]; + missing = text_state & TTF_STYLE_BOLD; + } if (!name) - { - name = fi->filename[text_state ^ (TTF_STYLE_ITALIC | TTF_STYLE_BOLD)]; - missing = text_state & (TTF_STYLE_ITALIC | TTF_STYLE_BOLD); - } + { + name = fi->filename[text_state ^ (TTF_STYLE_ITALIC | TTF_STYLE_BOLD)]; + missing = text_state & (TTF_STYLE_ITALIC | TTF_STYLE_BOLD); + } if (!name) - { + { #ifdef DEBUG - printf("%s:%d - name is still NULL\n", __FILE__, __LINE__); - fflush(stdout); + printf("%s:%d - name is still NULL\n", __FILE__, __LINE__); + fflush(stdout); #endif - return (NULL); - } + return (NULL); + } pathname = alloca(strlen(fi->directory) + 1 + strlen(name) + 1); sprintf(pathname, "%s/%s", fi->directory, name); @@ -1439,36 +1501,38 @@ TuxPaint_Font *getfonthandle(int desire) strcpy(description, ""); } - fi->handle = TuxPaint_Font_OpenFont(description, pathname, text_sizes[text_size]); + fi->handle = + TuxPaint_Font_OpenFont(description, pathname, text_sizes[text_size]); // if the font doesn't load, we die -- it did load OK before though if (fi->handle == NULL) + { +#ifdef DEBUG + printf("%s:%d - fi->handle is NULL!\n", __FILE__, __LINE__); + fflush(stdout); +#endif + return (NULL); + } + + if (fi->handle->typ == FONT_TYPE_TTF) + { + if (fi->handle->ttf_font == NULL) { #ifdef DEBUG - printf("%s:%d - fi->handle is NULL!\n", __FILE__, __LINE__); + printf("%s:%d - fi->handle->ttf_font is NULL!\n", __FILE__, __LINE__); fflush(stdout); #endif return (NULL); } - if (fi->handle->typ == FONT_TYPE_TTF) - { - if (fi->handle->ttf_font == NULL) - { #ifdef DEBUG - printf("%s:%d - fi->handle->ttf_font is NULL!\n", __FILE__, __LINE__); - fflush(stdout); -#endif - return (NULL); - } - -#ifdef DEBUG - printf("%s:%d - calling TTF_SetFontStyle(0x%x)\n", __FILE__, __LINE__, missing); - fflush(stdout); + printf("%s:%d - calling TTF_SetFontStyle(0x%x)\n", __FILE__, __LINE__, + missing); + fflush(stdout); #endif - TTF_SetFontStyle(fi->handle->ttf_font, missing); - } + TTF_SetFontStyle(fi->handle->ttf_font, missing); + } #ifndef NO_SDLPANGO if (fi->handle->typ == FONT_TYPE_PANGO) @@ -1483,7 +1547,8 @@ TuxPaint_Font *getfonthandle(int desire) static int was_bad_font; // see if two font surfaces are the same -static int do_surfcmp(const SDL_Surface * const *const v1, const SDL_Surface * const *const v2) +static int do_surfcmp(const SDL_Surface * const *const v1, + const SDL_Surface * const *const v2) { const SDL_Surface *const s1 = *v1; const SDL_Surface *const s2 = *v2; @@ -1492,21 +1557,22 @@ static int do_surfcmp(const SDL_Surface * const *const v1, const SDL_Surface * c int i; if (s1 == s2) - { - fprintf(stderr, "s1==s2?\n"); - return 0; - } - if (!s1 || !s2 || !s1->w || !s2->w || !s1->h || !s2->h || !s1->format || !s2->format) - { - was_bad_font = 1; - return 0; - } + { + fprintf(stderr, "s1==s2?\n"); + return 0; + } + if (!s1 || !s2 || !s1->w || !s2->w || !s1->h || !s2->h || !s1->format + || !s2->format) + { + was_bad_font = 1; + return 0; + } if (s1->format->BytesPerPixel != s2->format->BytesPerPixel) - { - // something really strange and bad happened - was_bad_font = 1; - return s1->format->BytesPerPixel - s2->format->BytesPerPixel; - } + { + // something really strange and bad happened + was_bad_font = 1; + return s1->format->BytesPerPixel - s2->format->BytesPerPixel; + } if (s1->w != s2->w) @@ -1515,8 +1581,8 @@ static int do_surfcmp(const SDL_Surface * const *const v1, const SDL_Surface * c return s1->h - s2->h; { - const char *const c1 = (char *const)s1->pixels; - const char *const c2 = (char *const)s2->pixels; + const char *const c1 = (char *const) s1->pixels; + const char *const c2 = (char *const) s2->pixels; width = s1->format->BytesPerPixel * s1->w; if (width == s1->pitch) @@ -1524,11 +1590,11 @@ static int do_surfcmp(const SDL_Surface * const *const v1, const SDL_Surface * c cmp = 0; i = s1->h; while (i--) - { - cmp = memcmp(c1 + i * s1->pitch, c2 + i * s2->pitch, width); - if (cmp) - break; - } + { + cmp = memcmp(c1 + i * s1->pitch, c2 + i * s2->pitch, width); + if (cmp) + break; + } } return cmp; @@ -1560,56 +1626,56 @@ int charset_works(TuxPaint_Font * font, const char *s) #else while (*s) #endif - { - char c[8]; - unsigned offset = 0; - SDL_Surface *tmp_surf = NULL; + { + char c[8]; + unsigned offset = 0; + SDL_Surface *tmp_surf = NULL; - do - c[offset++] = *s++; - while ((*s & 0xc0u) == 0x80u); // assume safe input - c[offset++] = '\0'; + do + c[offset++] = *s++; + while ((*s & 0xc0u) == 0x80u); // assume safe input + c[offset++] = '\0'; #ifndef NO_SDLPANGO - if (font->typ == FONT_TYPE_PANGO) - { - sdl_color_to_pango_color(black, &pango_color); - SDLPango_SetDefaultColor(font->pango_context, &pango_color); - SDLPango_SetText(font->pango_context, c, -1); - tmp_surf = SDLPango_CreateSurfaceDraw(font->pango_context); - } + if (font->typ == FONT_TYPE_PANGO) + { + sdl_color_to_pango_color(black, &pango_color); + SDLPango_SetDefaultColor(font->pango_context, &pango_color); + SDLPango_SetText(font->pango_context, c, -1); + tmp_surf = SDLPango_CreateSurfaceDraw(font->pango_context); + } #endif /* FIXME: Should the following be in an "#else" block!? -bjk 2009.06.01 */ - if (font->typ == FONT_TYPE_TTF) - { - tmp_surf = TTF_RenderUTF8_Blended(font->ttf_font, c, black); - } + if (font->typ == FONT_TYPE_TTF) + { + tmp_surf = TTF_RenderUTF8_Blended(font->ttf_font, c, black); + } - if (!tmp_surf) - { + if (!tmp_surf) + { #if 0 // THREADED_FONTS - printf("could not render \"%s\" font\n", TTF_FontFaceFamilyName(font)); + printf("could not render \"%s\" font\n", TTF_FontFaceFamilyName(font)); #endif - goto out; - } - surfs[count++] = tmp_surf; + goto out; } + surfs[count++] = tmp_surf; + } was_bad_font = 0; qsort(surfs, count, sizeof surfs[0], surfcmp); ret = !was_bad_font; out: while (count--) + { + if (surfs[count] == NULL) + fprintf(stderr, "TRYING TO RE-FREE!"); + else { - if (surfs[count] == NULL) - fprintf(stderr, "TRYING TO RE-FREE!"); - else - { - SDL_FreeSurface(surfs[count]); - surfs[count] = NULL; - } + SDL_FreeSurface(surfs[count]); + surfs[count] = NULL; } + } free(surfs); return ret; } @@ -1617,13 +1683,14 @@ out: int TuxPaint_Font_FontHeight(TuxPaint_Font * tpf) { if (tpf == NULL) - { + { #ifdef DEBUG - printf("%s:%d - TuxPaint_Font_FontHeight() received NULL\n", __FILE__, __LINE__); - fflush(stdout); + printf("%s:%d - TuxPaint_Font_FontHeight() received NULL\n", __FILE__, + __LINE__); + fflush(stdout); #endif - return (1); - } + return (1); + } return (tpf->height); } @@ -1631,29 +1698,31 @@ int TuxPaint_Font_FontHeight(TuxPaint_Font * tpf) const char *TuxPaint_Font_FontFaceFamilyName(TuxPaint_Font * tpf) { if (tpf == NULL) - { + { #ifdef DEBUG - printf("%s:%d - TuxPaint_Font_FontFaceFamilyName() received NULL\n", __FILE__, __LINE__); - fflush(stdout); + printf("%s:%d - TuxPaint_Font_FontFaceFamilyName() received NULL\n", + __FILE__, __LINE__); + fflush(stdout); #endif - return (""); - } + return (""); + } #ifndef NO_SDLPANGO if (tpf->typ == FONT_TYPE_PANGO) - { - (void)(tpf); - /* FIXME */ + { + (void) (tpf); + /* FIXME */ - return (""); - } + return (""); + } #endif if (tpf->typ == FONT_TYPE_TTF) return (TTF_FontFaceFamilyName(tpf->ttf_font)); #ifdef DEBUG - printf("%s:%d - TuxPaint_Font_FontFaceFamilyName() is confused\n", __FILE__, __LINE__); + printf("%s:%d - TuxPaint_Font_FontFaceFamilyName() is confused\n", __FILE__, + __LINE__); #endif return (""); @@ -1662,29 +1731,31 @@ const char *TuxPaint_Font_FontFaceFamilyName(TuxPaint_Font * tpf) const char *TuxPaint_Font_FontFaceStyleName(TuxPaint_Font * tpf) { if (tpf == NULL) - { + { #ifdef DEBUG - printf("%s:%d - TuxPaint_Font_FontFaceStyleName() received NULL\n", __FILE__, __LINE__); - fflush(stdout); + printf("%s:%d - TuxPaint_Font_FontFaceStyleName() received NULL\n", + __FILE__, __LINE__); + fflush(stdout); #endif - return (""); - } + return (""); + } #ifndef NO_SDLPANGO if (tpf->typ == FONT_TYPE_PANGO) - { - (void)(tpf); - /* FIXME */ + { + (void) (tpf); + /* FIXME */ - return (""); - } + return (""); + } #endif if (tpf->typ == FONT_TYPE_TTF) return (TTF_FontFaceStyleName(tpf->ttf_font)); #ifdef DEBUG - printf("%s:%d - TuxPaint_Font_FontFaceStyleName() is confused\n", __FILE__, __LINE__); + printf("%s:%d - TuxPaint_Font_FontFaceStyleName() is confused\n", __FILE__, + __LINE__); #endif return (""); @@ -1693,7 +1764,8 @@ const char *TuxPaint_Font_FontFaceStyleName(TuxPaint_Font * tpf) #ifndef NO_SDLPANGO -void ssdl_color_to_pango_color(SDL_Color sdl_color, SDLPango_Matrix * pango_color) +void ssdl_color_to_pango_color(SDL_Color sdl_color, + SDLPango_Matrix * pango_color) { Uint8 pc[4][4]; @@ -1720,7 +1792,8 @@ void ssdl_color_to_pango_color(SDL_Color sdl_color, SDLPango_Matrix * pango_colo memcpy(pango_color, pc, 16); } -void sdl_color_to_pango_color(SDL_Color sdl_color, SDLPango_Matrix * pango_color) +void sdl_color_to_pango_color(SDL_Color sdl_color, + SDLPango_Matrix * pango_color) { Uint8 pc[4][4]; diff --git a/src/fonts.h b/src/fonts.h index 91d64de9c..bab1f6cc9 100644 --- a/src/fonts.h +++ b/src/fonts.h @@ -121,9 +121,11 @@ int TuxPaint_Font_FontHeight(TuxPaint_Font * tpf); #ifdef FORKED_FONTS void reliable_write(int fd, const void *buf, size_t count); -void run_font_scanner(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, +void run_font_scanner(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer, const char *restrict const locale); -void receive_some_font_info(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer); +void receive_some_font_info(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer); #endif ////////////////////////////////////////////////////////////////////// @@ -191,7 +193,8 @@ TuxPaint_Font *getfonthandle(int desire); int charset_works(TuxPaint_Font * font, const char *s); -TuxPaint_Font *TuxPaint_Font_OpenFont(const char *pangodesc, const char *ttffilename, int size); +TuxPaint_Font *TuxPaint_Font_OpenFont(const char *pangodesc, + const char *ttffilename, int size); void TuxPaint_Font_CloseFont(TuxPaint_Font * tpf); const char *TuxPaint_Font_FontFaceFamilyName(TuxPaint_Font * tpf); const char *TuxPaint_Font_FontFaceStyleName(TuxPaint_Font * tpf); @@ -199,10 +202,12 @@ const char *TuxPaint_Font_FontFaceStyleName(TuxPaint_Font * tpf); #ifdef NO_SDLPANGO TuxPaint_Font *load_locale_font(TuxPaint_Font * fallback, int size); #else -void sdl_color_to_pango_color(SDL_Color sdl_color, SDLPango_Matrix * pango_color); +void sdl_color_to_pango_color(SDL_Color sdl_color, + SDLPango_Matrix * pango_color); #endif -int load_user_fonts(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, void *vp, +int load_user_fonts(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer, void *vp, const char *restrict const locale); #endif diff --git a/src/get_fname.c b/src/get_fname.c index 85bd64bd3..73f6e713f 100644 --- a/src/get_fname.c +++ b/src/get_fname.c @@ -87,35 +87,40 @@ char *get_fname(const char *const name, int kind) { char f[512]; // const char *restrict const dir; - const char * dir; + const char *dir; - if (kind == DIR_SAVE) { + if (kind == DIR_SAVE) + { dir = savedir; - } else if (kind == DIR_DATA) { + } + else if (kind == DIR_DATA) + { dir = datadir; - } else if (kind == DIR_EXPORT || kind == DIR_EXPORT_PARENT) { + } + else if (kind == DIR_EXPORT || kind == DIR_EXPORT_PARENT) + { dir = exportdir; } - snprintf(f, sizeof(f), - "%s%c%s", - dir, (*name) ? '/' : '\0', /* Some mkdir()'s don't like trailing slashes */ - name); + snprintf(f, sizeof(f), "%s%c%s", dir, (*name) ? '/' : '\0', /* Some mkdir()'s don't like trailing slashes */ + name); - if (kind == DIR_EXPORT_PARENT) { + if (kind == DIR_EXPORT_PARENT) + { int len, i, stop; stop = -1; len = strlen(f); - for (i = len - 1; i >= 0 && stop == -1; i--) { + for (i = len - 1; i >= 0 && stop == -1; i--) + { if (f[i] == '/') stop = i; } - if (stop != -1) { + if (stop != -1) + { f[stop] = '\0'; } } return strdup(f); } - diff --git a/src/gifenc.c b/src/gifenc.c index f2ccc6f53..994ef11d9 100644 --- a/src/gifenc.c +++ b/src/gifenc.c @@ -16,52 +16,50 @@ #define write_num(fd, n) write((fd), (uint8_t []) {(n) & 0xFF, (n) >> 8}, 2) static uint8_t vga[0x30] = { - 0x00, 0x00, 0x00, - 0xAA, 0x00, 0x00, - 0x00, 0xAA, 0x00, - 0xAA, 0x55, 0x00, - 0x00, 0x00, 0xAA, - 0xAA, 0x00, 0xAA, - 0x00, 0xAA, 0xAA, - 0xAA, 0xAA, 0xAA, - 0x55, 0x55, 0x55, - 0xFF, 0x55, 0x55, - 0x55, 0xFF, 0x55, - 0xFF, 0xFF, 0x55, - 0x55, 0x55, 0xFF, - 0xFF, 0x55, 0xFF, - 0x55, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, + 0x00, 0x00, 0x00, + 0xAA, 0x00, 0x00, + 0x00, 0xAA, 0x00, + 0xAA, 0x55, 0x00, + 0x00, 0x00, 0xAA, + 0xAA, 0x00, 0xAA, + 0x00, 0xAA, 0xAA, + 0xAA, 0xAA, 0xAA, + 0x55, 0x55, 0x55, + 0xFF, 0x55, 0x55, + 0x55, 0xFF, 0x55, + 0xFF, 0xFF, 0x55, + 0x55, 0x55, 0xFF, + 0xFF, 0x55, 0xFF, + 0x55, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, }; -struct Node { - uint16_t key; - struct Node *children[]; +struct Node +{ + uint16_t key; + struct Node *children[]; }; typedef struct Node Node; -static Node * -new_node(uint16_t key, int degree) +static Node *new_node(uint16_t key, int degree) { - Node *node = calloc(1, sizeof(*node) + degree * sizeof(Node *)); - if (node) - node->key = key; - return node; + Node *node = calloc(1, sizeof(*node) + degree * sizeof(Node *)); + if (node) + node->key = key; + return node; } -static Node * -new_trie(int degree, int *nkeys) +static Node *new_trie(int degree, int *nkeys) { - Node *root = new_node(0, degree); - /* Create nodes for single pixels. */ - for (*nkeys = 0; *nkeys < degree; (*nkeys)++) - root->children[*nkeys] = new_node(*nkeys, degree); - *nkeys += 2; /* skip clear code and stop code */ - return root; + Node *root = new_node(0, degree); + /* Create nodes for single pixels. */ + for (*nkeys = 0; *nkeys < degree; (*nkeys)++) + root->children[*nkeys] = new_node(*nkeys, degree); + *nkeys += 2; /* skip clear code and stop code */ + return root; } -static void -del_trie(Node *root, int degree) +static void del_trie(Node * root, int degree) { int i; @@ -72,191 +70,261 @@ del_trie(Node *root, int degree) free(root); } -static void put_loop(ge_GIF *gif, uint16_t loop); +static void put_loop(ge_GIF * gif, uint16_t loop); #define OR_ABORT if (res == -1) { fprintf(stderr, "Cannot write to GIF\n"); return(NULL); } #define OR_ABORT2 if (res == -1) { fprintf(stderr, "Cannot write to GIF\n"); return; } -ge_GIF * -ge_new_gif( - const char *fname, uint16_t width, uint16_t height, - uint8_t *palette, int depth, int loop -) +ge_GIF *ge_new_gif(const char *fname, uint16_t width, uint16_t height, + uint8_t * palette, int depth, int loop) { - int i, r, g, b, v; - ssize_t res; - ge_GIF *gif = calloc(1, sizeof(*gif) + 2*width*height); - if (!gif) - goto no_gif; - gif->w = width; gif->h = height; - gif->depth = depth > 1 ? depth : 2; - gif->frame = (uint8_t *) &gif[1]; - gif->back = &gif->frame[width*height]; - gif->fd = creat(fname, 0666); - if (gif->fd == -1) - goto no_fd; + int i, r, g, b, v; + ssize_t res; + ge_GIF *gif = calloc(1, sizeof(*gif) + 2 * width * height); + if (!gif) + goto no_gif; + gif->w = width; + gif->h = height; + gif->depth = depth > 1 ? depth : 2; + gif->frame = (uint8_t *) & gif[1]; + gif->back = &gif->frame[width * height]; + gif->fd = creat(fname, 0666); + if (gif->fd == -1) + goto no_fd; #ifdef _WIN32 - setmode(gif->fd, O_BINARY); + setmode(gif->fd, O_BINARY); #endif - res = write(gif->fd, "GIF89a", 6); OR_ABORT; - res = write_num(gif->fd, width); OR_ABORT; - res = write_num(gif->fd, height); OR_ABORT; - res = write(gif->fd, (uint8_t []) {0xF0 | (depth-1), 0x00, 0x00}, 3); OR_ABORT; - if (palette) { - res = write(gif->fd, palette, 3 << depth); OR_ABORT; - } else if (depth <= 4) { - res = write(gif->fd, vga, 3 << depth); OR_ABORT; - } else { - res = write(gif->fd, vga, sizeof(vga)); OR_ABORT; - i = 0x10; - for (r = 0; r < 6; r++) { - for (g = 0; g < 6; g++) { - for (b = 0; b < 6; b++) { - res = write(gif->fd, (uint8_t []) {r*51, g*51, b*51}, 3); OR_ABORT; - if (++i == 1 << depth) - goto done_gct; - } - } - } - for (i = 1; i <= 24; i++) { - v = i * 0xFF / 25; - res = write(gif->fd, (uint8_t []) {v, v, v}, 3); OR_ABORT; + res = write(gif->fd, "GIF89a", 6); + OR_ABORT; + res = write_num(gif->fd, width); + OR_ABORT; + res = write_num(gif->fd, height); + OR_ABORT; + res = write(gif->fd, (uint8_t[]) + { + 0xF0 | (depth - 1), 0x00, 0x00} + , 3); + OR_ABORT; + if (palette) + { + res = write(gif->fd, palette, 3 << depth); + OR_ABORT; + } + else if (depth <= 4) + { + res = write(gif->fd, vga, 3 << depth); + OR_ABORT; + } + else + { + res = write(gif->fd, vga, sizeof(vga)); + OR_ABORT; + i = 0x10; + for (r = 0; r < 6; r++) + { + for (g = 0; g < 6; g++) + { + for (b = 0; b < 6; b++) + { + res = write(gif->fd, (uint8_t[]) + { + r * 51, g * 51, b * 51} + , 3); + OR_ABORT; + if (++i == 1 << depth) + goto done_gct; } + } } + for (i = 1; i <= 24; i++) + { + v = i * 0xFF / 25; + res = write(gif->fd, (uint8_t[]) + { + v, v, v} + , 3); + OR_ABORT; + } + } done_gct: - if (loop >= 0 && loop <= 0xFFFF) - put_loop(gif, (uint16_t) loop); - return gif; + if (loop >= 0 && loop <= 0xFFFF) + put_loop(gif, (uint16_t) loop); + return gif; no_fd: - free(gif); + free(gif); no_gif: - return NULL; + return NULL; } -static void -put_loop(ge_GIF *gif, uint16_t loop) +static void put_loop(ge_GIF * gif, uint16_t loop) { - ssize_t res; + ssize_t res; - res = write(gif->fd, (uint8_t []) {'!', 0xFF, 0x0B}, 3); OR_ABORT2; - res = write(gif->fd, "NETSCAPE2.0", 11); OR_ABORT2; - res = write(gif->fd, (uint8_t []) {0x03, 0x01}, 2); OR_ABORT2; - res = write_num(gif->fd, loop); OR_ABORT2; - res = write(gif->fd, "\0", 1); OR_ABORT2; + res = write(gif->fd, (uint8_t[]) + { + '!', 0xFF, 0x0B}, 3); + OR_ABORT2; + res = write(gif->fd, "NETSCAPE2.0", 11); + OR_ABORT2; + res = write(gif->fd, (uint8_t[]) + { + 0x03, 0x01}, 2); + OR_ABORT2; + res = write_num(gif->fd, loop); + OR_ABORT2; + res = write(gif->fd, "\0", 1); + OR_ABORT2; } /* Add packed key to buffer, updating offset and partial. * gif->offset holds position to put next *bit* * gif->partial holds bits to include in next byte */ -static void -put_key(ge_GIF *gif, uint16_t key, int key_size) +static void put_key(ge_GIF * gif, uint16_t key, int key_size) { - int byte_offset, bit_offset, bits_to_write; - ssize_t res; + int byte_offset, bit_offset, bits_to_write; + ssize_t res; - byte_offset = gif->offset / 8; - bit_offset = gif->offset % 8; - gif->partial |= ((uint32_t) key) << bit_offset; - bits_to_write = bit_offset + key_size; - while (bits_to_write >= 8) { - gif->buffer[byte_offset++] = gif->partial & 0xFF; - if (byte_offset == 0xFF) { - res = write(gif->fd, "\xFF", 1); OR_ABORT2; - res = write(gif->fd, gif->buffer, 0xFF); OR_ABORT2; - byte_offset = 0; - } - gif->partial >>= 8; - bits_to_write -= 8; + byte_offset = gif->offset / 8; + bit_offset = gif->offset % 8; + gif->partial |= ((uint32_t) key) << bit_offset; + bits_to_write = bit_offset + key_size; + while (bits_to_write >= 8) + { + gif->buffer[byte_offset++] = gif->partial & 0xFF; + if (byte_offset == 0xFF) + { + res = write(gif->fd, "\xFF", 1); + OR_ABORT2; + res = write(gif->fd, gif->buffer, 0xFF); + OR_ABORT2; + byte_offset = 0; } - gif->offset = (gif->offset + key_size) % (0xFF * 8); + gif->partial >>= 8; + bits_to_write -= 8; + } + gif->offset = (gif->offset + key_size) % (0xFF * 8); +} + +static void end_key(ge_GIF * gif) +{ + int byte_offset; + ssize_t res; + + byte_offset = gif->offset / 8; + if (gif->offset % 8) + gif->buffer[byte_offset++] = gif->partial & 0xFF; + res = write(gif->fd, (uint8_t[]) + { + byte_offset} + , 1); + OR_ABORT2; + res = write(gif->fd, gif->buffer, byte_offset); + OR_ABORT2; + res = write(gif->fd, "\0", 1); + OR_ABORT2; + gif->offset = gif->partial = 0; } static void -end_key(ge_GIF *gif) +put_image(ge_GIF * gif, uint16_t w, uint16_t h, uint16_t x, uint16_t y) { - int byte_offset; - ssize_t res; + int nkeys, key_size, i, j; + Node *node, *child, *root; + ssize_t res; + int degree = 1 << gif->depth; - byte_offset = gif->offset / 8; - if (gif->offset % 8) - gif->buffer[byte_offset++] = gif->partial & 0xFF; - res = write(gif->fd, (uint8_t []) {byte_offset}, 1); OR_ABORT2; - res = write(gif->fd, gif->buffer, byte_offset); OR_ABORT2; - res = write(gif->fd, "\0", 1); OR_ABORT2; - gif->offset = gif->partial = 0; -} - -static void -put_image(ge_GIF *gif, uint16_t w, uint16_t h, uint16_t x, uint16_t y) -{ - int nkeys, key_size, i, j; - Node *node, *child, *root; - ssize_t res; - int degree = 1 << gif->depth; - - res = write(gif->fd, ",", 1); OR_ABORT2; - res = write_num(gif->fd, x); OR_ABORT2; - res = write_num(gif->fd, y); OR_ABORT2; - res = write_num(gif->fd, w); OR_ABORT2; - res = write_num(gif->fd, h); OR_ABORT2; - res = write(gif->fd, (uint8_t []) {0x00, gif->depth}, 2); OR_ABORT2; - root = node = new_trie(degree, &nkeys); - key_size = gif->depth + 1; - put_key(gif, degree, key_size); /* clear code */ - for (i = y; i < y+h; i++) { - for (j = x; j < x+w; j++) { - uint8_t pixel = gif->frame[i*gif->w+j] & (degree - 1); - child = node->children[pixel]; - if (child) { - node = child; - } else { - put_key(gif, node->key, key_size); - if (nkeys < 0x1000) { - if (nkeys == (1 << key_size)) - key_size++; - node->children[pixel] = new_node(nkeys++, degree); - } else { - put_key(gif, degree, key_size); /* clear code */ - del_trie(root, degree); - root = node = new_trie(degree, &nkeys); - key_size = gif->depth + 1; - } - node = root->children[pixel]; - } + res = write(gif->fd, ",", 1); + OR_ABORT2; + res = write_num(gif->fd, x); + OR_ABORT2; + res = write_num(gif->fd, y); + OR_ABORT2; + res = write_num(gif->fd, w); + OR_ABORT2; + res = write_num(gif->fd, h); + OR_ABORT2; + res = write(gif->fd, (uint8_t[]) + { + 0x00, gif->depth}, 2); + OR_ABORT2; + root = node = new_trie(degree, &nkeys); + key_size = gif->depth + 1; + put_key(gif, degree, key_size); /* clear code */ + for (i = y; i < y + h; i++) + { + for (j = x; j < x + w; j++) + { + uint8_t pixel = gif->frame[i * gif->w + j] & (degree - 1); + child = node->children[pixel]; + if (child) + { + node = child; + } + else + { + put_key(gif, node->key, key_size); + if (nkeys < 0x1000) + { + if (nkeys == (1 << key_size)) + key_size++; + node->children[pixel] = new_node(nkeys++, degree); } + else + { + put_key(gif, degree, key_size); /* clear code */ + del_trie(root, degree); + root = node = new_trie(degree, &nkeys); + key_size = gif->depth + 1; + } + node = root->children[pixel]; + } } - put_key(gif, node->key, key_size); - put_key(gif, degree + 1, key_size); /* stop code */ - end_key(gif); - del_trie(root, degree); + } + put_key(gif, node->key, key_size); + put_key(gif, degree + 1, key_size); /* stop code */ + end_key(gif); + del_trie(root, degree); } static int -get_bbox(ge_GIF *gif, uint16_t *w, uint16_t *h, uint16_t *x, uint16_t *y) +get_bbox(ge_GIF * gif, uint16_t * w, uint16_t * h, uint16_t * x, uint16_t * y) { - int i, j, k; - int left, right, top, bottom; - left = gif->w; right = 0; - top = gif->h; bottom = 0; - k = 0; - for (i = 0; i < gif->h; i++) { - for (j = 0; j < gif->w; j++, k++) { - if (gif->frame[k] != gif->back[k]) { - if (j < left) left = j; - if (j > right) right = j; - if (i < top) top = i; - if (i > bottom) bottom = i; - } - } - } - if (left != gif->w && top != gif->h) { - *x = left; *y = top; - *w = right - left + 1; - *h = bottom - top + 1; - return 1; - } else { - return 0; + int i, j, k; + int left, right, top, bottom; + left = gif->w; + right = 0; + top = gif->h; + bottom = 0; + k = 0; + for (i = 0; i < gif->h; i++) + { + for (j = 0; j < gif->w; j++, k++) + { + if (gif->frame[k] != gif->back[k]) + { + if (j < left) + left = j; + if (j > right) + right = j; + if (i < top) + top = i; + if (i > bottom) + bottom = i; + } } + } + if (left != gif->w && top != gif->h) + { + *x = left; + *y = top; + *w = right - left + 1; + *h = bottom - top + 1; + return 1; + } + else + { + return 0; + } } /* (From the docs) @@ -267,46 +335,52 @@ get_bbox(ge_GIF *gif, uint16_t *w, uint16_t *h, uint16_t *x, uint16_t *y) * a minimum of `delay` == 6. If `delay` == 0, no delay information will be stored * for the frame. This can be used when creating still (single-frame) GIF images. */ -static void -set_delay(ge_GIF *gif, uint16_t d) +static void set_delay(ge_GIF * gif, uint16_t d) { - ssize_t res; + ssize_t res; - res = write(gif->fd, (uint8_t []) {'!', 0xF9, 0x04, 0x04}, 4); OR_ABORT2; - res = write_num(gif->fd, d); OR_ABORT2; - res = write(gif->fd, "\0\0", 2); OR_ABORT2; + res = write(gif->fd, (uint8_t[]) + { + '!', 0xF9, 0x04, 0x04}, 4); + OR_ABORT2; + res = write_num(gif->fd, d); + OR_ABORT2; + res = write(gif->fd, "\0\0", 2); + OR_ABORT2; } -void -ge_add_frame(ge_GIF *gif, uint16_t delay) +void ge_add_frame(ge_GIF * gif, uint16_t delay) { - uint16_t w, h, x, y; - uint8_t *tmp; + uint16_t w, h, x, y; + uint8_t *tmp; - if (delay) - set_delay(gif, delay); - if (gif->nframes == 0) { - w = gif->w; - h = gif->h; - x = y = 0; - } else if (!get_bbox(gif, &w, &h, &x, &y)) { - /* image's not changed; save one pixel just to add delay */ - w = h = 1; - x = y = 0; - } - put_image(gif, w, h, x, y); - gif->nframes++; - tmp = gif->back; - gif->back = gif->frame; - gif->frame = tmp; + if (delay) + set_delay(gif, delay); + if (gif->nframes == 0) + { + w = gif->w; + h = gif->h; + x = y = 0; + } + else if (!get_bbox(gif, &w, &h, &x, &y)) + { + /* image's not changed; save one pixel just to add delay */ + w = h = 1; + x = y = 0; + } + put_image(gif, w, h, x, y); + gif->nframes++; + tmp = gif->back; + gif->back = gif->frame; + gif->frame = tmp; } -void -ge_close_gif(ge_GIF* gif) +void ge_close_gif(ge_GIF * gif) { - ssize_t res; + ssize_t res; - res = write(gif->fd, ";", 1); OR_ABORT2; - close(gif->fd); - free(gif); + res = write(gif->fd, ";", 1); + OR_ABORT2; + close(gif->fd); + free(gif); } diff --git a/src/gifenc.h b/src/gifenc.h index b8504e0c2..a54304cd5 100644 --- a/src/gifenc.h +++ b/src/gifenc.h @@ -3,22 +3,21 @@ #include -typedef struct ge_GIF { - uint16_t w, h; - int depth; - int fd; - int offset; - int nframes; - uint8_t *frame, *back; - uint32_t partial; - uint8_t buffer[0xFF]; +typedef struct ge_GIF +{ + uint16_t w, h; + int depth; + int fd; + int offset; + int nframes; + uint8_t *frame, *back; + uint32_t partial; + uint8_t buffer[0xFF]; } ge_GIF; -ge_GIF *ge_new_gif( - const char *fname, uint16_t width, uint16_t height, - uint8_t *palette, int depth, int loop -); -void ge_add_frame(ge_GIF *gif, uint16_t delay); -void ge_close_gif(ge_GIF* gif); +ge_GIF *ge_new_gif(const char *fname, uint16_t width, uint16_t height, + uint8_t * palette, int depth, int loop); +void ge_add_frame(ge_GIF * gif, uint16_t delay); +void ge_close_gif(ge_GIF * gif); #endif /* GIFENC_H */ diff --git a/src/i18n.c b/src/i18n.c index f19e21fb0..39992572e 100644 --- a/src/i18n.c +++ b/src/i18n.c @@ -60,10 +60,16 @@ static char *android_locale() static char android_locale_buf[32]; JNIEnv *mEnv = Android_JNI_GetEnv(); jclass mLocaleClass = (*mEnv)->FindClass(mEnv, "java/util/Locale"); - jmethodID mGetDefaultMethod = (*mEnv)->GetStaticMethodID(mEnv, mLocaleClass, "getDefault", "()Ljava/util/Locale;"); - jobject mLocaleObject = (*mEnv)->CallStaticObjectMethod(mEnv, mLocaleClass, mGetDefaultMethod); - jmethodID mToStringMethod = (*mEnv)->GetMethodID(mEnv, mLocaleClass, "toString", "()Ljava/lang/String;"); - jstring mLocaleString = (*mEnv)->CallObjectMethod(mEnv, mLocaleObject, mToStringMethod); + jmethodID mGetDefaultMethod = + (*mEnv)->GetStaticMethodID(mEnv, mLocaleClass, "getDefault", + "()Ljava/util/Locale;"); + jobject mLocaleObject = + (*mEnv)->CallStaticObjectMethod(mEnv, mLocaleClass, mGetDefaultMethod); + jmethodID mToStringMethod = + (*mEnv)->GetMethodID(mEnv, mLocaleClass, "toString", + "()Ljava/lang/String;"); + jstring mLocaleString = + (*mEnv)->CallObjectMethod(mEnv, mLocaleObject, mToStringMethod); const char *locale = (*mEnv)->GetStringUTFChars(mEnv, mLocaleString, 0); strcpy(android_locale_buf, locale); @@ -484,7 +490,9 @@ static void show_lang_usage(int exitcode) const char *const prg = "tuxpaint"; /* FIXME: All this should REALLY be array-based!!! */ - fprintf(f, "\n" "Usage: %s [--lang LANGUAGE]\n" "\n" "LANGUAGE may be one of:\n" + fprintf(f, + "\n" "Usage: %s [--lang LANGUAGE]\n" "\n" + "LANGUAGE may be one of:\n" /* C */ " english american-english\n" /* ach */ " acholi acoli\n" /* af */ " afrikaans\n" @@ -505,7 +513,8 @@ static void show_lang_usage(int exitcode) /* brx */ " bodo\n" /* nb */ " bokmal\n" /* bs */ " bosnian\n" -/* pt_BR */ " brazilian brazilian-portuguese portugues-brazilian\n" +/* pt_BR */ + " brazilian brazilian-portuguese portugues-brazilian\n" /* br */ " breton brezhoneg\n" /* en_GB */ " british british-english\n" /* bg_BG */ " bulgarian\n" @@ -562,7 +571,8 @@ static void show_lang_usage(int exitcode) /* mni */ " manipuri-bengali\n" /* mni@meiteimayek */ " manipuri-meitei-mayek\n" /* nr */ " marathi\n" -/* es_MX */ " mexican mexican-spanish espanol-mejicano\n" +/* es_MX */ + " mexican mexican-spanish espanol-mejicano\n" /* mn */ " mongolian\n" /* nr */ " ndebele\n" /* ne */ " nepali\n" @@ -760,7 +770,8 @@ static void show_locale_usage(FILE * f, const char *const prg) " wa_BE (Walloon)\n" " wo_SN (Wolof)\n" " cy_GB (Welsh Cymraeg)\n" - " xh_ZA (Xhosa)\n" " zam (Zapoteco-Miahuatlan)\n" " zu_ZA (Zulu)\n" "\n", prg); + " xh_ZA (Xhosa)\n" " zam (Zapoteco-Miahuatlan)\n" + " zu_ZA (Zulu)\n" "\n", prg); } /** @@ -785,10 +796,10 @@ static int search_int_array(int l, int *array) int i; for (i = 0; array[i] != -1; i++) - { - if (array[i] == l) - return 1; - } + { + if (array[i] == l) + return 1; + } return 0; } @@ -804,18 +815,19 @@ static void ctype_utf8(void) #ifndef _WIN32 /* FIXME: should this iterate over more locales? A zapotec speaker may have es_MX.UTF-8 available but not have en_US.UTF-8 for example */ - const char *names[] = { "en_US.UTF8", "en_US.UTF-8", "UTF8", "UTF-8", "C.UTF-8" }; + const char *names[] = + { "en_US.UTF8", "en_US.UTF-8", "UTF8", "UTF-8", "C.UTF-8" }; int i = sizeof(names) / sizeof(names[0]); for (;;) - { - if (iswprint((wchar_t) 0xf7)) // division symbol -- which is in Latin-1 :-/ - return; - if (--i < 0) - break; - setlocale(LC_CTYPE, names[i]); - setlocale(LC_MESSAGES, names[i]); - } + { + if (iswprint((wchar_t) 0xf7)) // division symbol -- which is in Latin-1 :-/ + return; + if (--i < 0) + break; + setlocale(LC_CTYPE, names[i]); + setlocale(LC_MESSAGES, names[i]); + } fprintf(stderr, "Failed to find a locale with iswprint() working!\n"); #endif } @@ -828,13 +840,14 @@ static void ctype_utf8(void) */ static const char *language_to_locale(const char *langstr) { - int i = sizeof language_to_locale_array / sizeof language_to_locale_array[0]; + int i = + sizeof language_to_locale_array / sizeof language_to_locale_array[0]; while (i--) - { - if (!strcmp(langstr, language_to_locale_array[i].language)) - return language_to_locale_array[i].locale; - } + { + if (!strcmp(langstr, language_to_locale_array[i].language)) + return language_to_locale_array[i].locale; + } if (strcmp(langstr, "help") == 0 || strcmp(langstr, "list") == 0) show_lang_usage(0); fprintf(stderr, "%s is an invalid language\n", langstr); @@ -854,34 +867,37 @@ static const char *language_to_locale(const char *langstr) */ static const char *locale_to_closest_locale(const char *inlocale) { - const int numlocale = sizeof(language_to_locale_array) / sizeof(language_to_locale_array[0]); - const char* outlocale = NULL; + const int numlocale = + sizeof(language_to_locale_array) / sizeof(language_to_locale_array[0]); + const char *outlocale = NULL; int outlocale_score = 0; int i = 0; int j = 0; /* find the locale with the longest string match */ for (i = 0; i < numlocale; i++) + { + const char *candidate = language_to_locale_array[i].locale; + + for (j = 0; j < (int) strlen(inlocale) && j < (int) strlen(candidate); + j++) { - const char* candidate = language_to_locale_array[i].locale; - - for (j = 0; j < (int) strlen(inlocale) && j < (int) strlen(candidate); j++) - { - if(inlocale[j] != candidate[j]) break; - } - - if (j > outlocale_score) - { - outlocale = candidate; - outlocale_score = j; - } + if (inlocale[j] != candidate[j]) + break; } + if (j > outlocale_score) + { + outlocale = candidate; + outlocale_score = j; + } + } + /* locale must match at least two characters */ if (outlocale_score < 2) - { - outlocale = ""; - } + { + outlocale = ""; + } return outlocale; } @@ -925,68 +941,70 @@ static void set_langint_from_locale_string(const char *restrict loc) *dot = '\0'; if (cntrycode) - { - ccodeaux = strdup(cntrycode); - *cntrycode = '\0'; - } + { + ccodeaux = strdup(cntrycode); + *cntrycode = '\0'; + } if (at) + { + ataux = strdup(at); + *at = '\0'; + + if (cntrycode) { - ataux = strdup(at); - *at = '\0'; - - if (cntrycode) - { - /* ll_CC@variant */ - //if (found == 0) printf("ll_CC@variant check\n"); - snprintf(straux, 255, "%s%s%s", baseloc, ccodeaux, ataux); - len_baseloc = strlen(straux); - for (i = 0; i < NUM_LANGS && found == 0; i++) - { - // Case-insensitive (both "pt_BR" and "pt_br" work, etc.) - if (len_baseloc == strlen(lang_prefixes[i]) && !strncasecmp(straux, lang_prefixes[i], len_baseloc)) - { - langint = i; - found = 1; - } - } - } - - /* ll@variant */ - //if (found == 0) printf("ll@variant check\n"); - snprintf(straux, 255, "%s%s", baseloc, ataux); + /* ll_CC@variant */ + //if (found == 0) printf("ll_CC@variant check\n"); + snprintf(straux, 255, "%s%s%s", baseloc, ccodeaux, ataux); len_baseloc = strlen(straux); for (i = 0; i < NUM_LANGS && found == 0; i++) + { + // Case-insensitive (both "pt_BR" and "pt_br" work, etc.) + if (len_baseloc == strlen(lang_prefixes[i]) + && !strncasecmp(straux, lang_prefixes[i], len_baseloc)) { - // Case-insensitive (both "pt_BR" and "pt_br" work, etc.) - if (len_baseloc == strlen(lang_prefixes[i]) && !strncasecmp(straux, lang_prefixes[i], len_baseloc)) - { - langint = i; - found = 1; - } + langint = i; + found = 1; } + } } + /* ll@variant */ + //if (found == 0) printf("ll@variant check\n"); + snprintf(straux, 255, "%s%s", baseloc, ataux); + len_baseloc = strlen(straux); + for (i = 0; i < NUM_LANGS && found == 0; i++) + { + // Case-insensitive (both "pt_BR" and "pt_br" work, etc.) + if (len_baseloc == strlen(lang_prefixes[i]) + && !strncasecmp(straux, lang_prefixes[i], len_baseloc)) + { + langint = i; + found = 1; + } + } + } + if (cntrycode) + { + /* ll_CC */ + //if (found == 0) printf("ll_CC check\n"); + snprintf(straux, 255, "%s%s", baseloc, ccodeaux); + len_baseloc = strlen(straux); + + /* Which, if any, of the locales is it? */ + + for (i = 0; i < NUM_LANGS && found == 0; i++) { - /* ll_CC */ - //if (found == 0) printf("ll_CC check\n"); - snprintf(straux, 255, "%s%s", baseloc, ccodeaux); - len_baseloc = strlen(straux); - - /* Which, if any, of the locales is it? */ - - for (i = 0; i < NUM_LANGS && found == 0; i++) - { - // Case-insensitive (both "pt_BR" and "pt_br" work, etc.) - if (len_baseloc == strlen(lang_prefixes[i]) && - !strncasecmp(straux, lang_prefixes[i], strlen(lang_prefixes[i]))) - { - langint = i; - found = 1; - } - } + // Case-insensitive (both "pt_BR" and "pt_br" work, etc.) + if (len_baseloc == strlen(lang_prefixes[i]) && + !strncasecmp(straux, lang_prefixes[i], strlen(lang_prefixes[i]))) + { + langint = i; + found = 1; + } } + } /* ll */ // if (found == 0) printf("ll check\n"); @@ -994,14 +1012,15 @@ static void set_langint_from_locale_string(const char *restrict loc) /* Which, if any, of the locales is it? */ for (i = 0; i < NUM_LANGS && found == 0; i++) + { + // Case-insensitive (both "pt_BR" and "pt_br" work, etc.) + if (len_baseloc == strlen(lang_prefixes[i]) + && !strncasecmp(baseloc, lang_prefixes[i], strlen(lang_prefixes[i]))) { - // Case-insensitive (both "pt_BR" and "pt_br" work, etc.) - if (len_baseloc == strlen(lang_prefixes[i]) && !strncasecmp(baseloc, lang_prefixes[i], strlen(lang_prefixes[i]))) - { - langint = i; - found = 1; - } + langint = i; + found = 1; } + } /* Last resort, we should never arrive here, this check depends on the right order in lang_prefixes[] @@ -1011,14 +1030,14 @@ static void set_langint_from_locale_string(const char *restrict loc) // printf("Language still not found: loc= %s Trying reverse check as last resource...\n", loc); for (i = 0; i < NUM_LANGS && found == 0; i++) + { + // Case-insensitive (both "pt_BR" and "pt_br" work, etc.) + if (!strncasecmp(loc, lang_prefixes[i], strlen(lang_prefixes[i]))) { - // Case-insensitive (both "pt_BR" and "pt_br" work, etc.) - if (!strncasecmp(loc, lang_prefixes[i], strlen(lang_prefixes[i]))) - { - langint = i; - found = 1; - } + langint = i; + found = 1; } + } // printf("langint %i, lang_ext %s\n", langint, lang_prefixes[langint]); free(baseloc); @@ -1048,7 +1067,8 @@ static void mysetenv(const char *name, const char *value) char *str; #endif - if (name != NULL && value != NULL) { + if (name != NULL && value != NULL) + { #ifdef HAVE_SETENV setenv(name, value, 1); #else @@ -1058,11 +1078,12 @@ static void mysetenv(const char *name, const char *value) sprintf(str, "%s=%s", name, value); putenv(str); #endif - } else { - fprintf(stderr, "WARNING: mysetenv() received a null pointer. name=%s, value=%s\n", - (name == NULL ? "NULL" : name), - (value == NULL ? "NULL" : value) - ); + } + else + { + fprintf(stderr, + "WARNING: mysetenv() received a null pointer. name=%s, value=%s\n", + (name == NULL ? "NULL" : name), (value == NULL ? "NULL" : value)); } } @@ -1074,7 +1095,8 @@ static void mysetenv(const char *name, const char *value) * @return The Y-nudge value for font rendering in the language. */ -static int set_current_language(const char *restrict loc, int * ptr_num_wished_langs) +static int set_current_language(const char *restrict loc, + int *ptr_num_wished_langs) { int i; int j = 0; @@ -1087,66 +1109,67 @@ static int set_current_language(const char *restrict loc, int * ptr_num_wished_l *ptr_num_wished_langs = 0; if (strlen(loc) > 0) - { - /* Got command line or config file language */ - DEBUG_PRINTF("Language via config: %s\n", loc); - mysetenv("LANGUAGE", loc); - } + { + /* Got command line or config file language */ + DEBUG_PRINTF("Language via config: %s\n", loc); + mysetenv("LANGUAGE", loc); + } else - { - DEBUG_PRINTF("Language NOT set via config\n"); + { + DEBUG_PRINTF("Language NOT set via config\n"); - /* Find what language to use from env vars */ - env = getenv("LANGUAGE"); - if (env == NULL || env[0] == '\0') - { - env = getenv("LC_ALL"); - if (env != NULL && env[0] != '\0') - { - DEBUG_PRINTF("Language via LC_ALL: %s\n", getenv("LC_ALL")); - mysetenv("LANGUAGE", getenv("LC_ALL")); - } - else - { - env = getenv("LC_MESSAGES"); - if (env != NULL && env[0] != '\0') - { - DEBUG_PRINTF("Language via LC_MESSAGES: %s\n", getenv("LC_MESSAGES")); - mysetenv("LANGUAGE", getenv("LC_MESSAGES")); - } - else - { - env = getenv("LANG"); - if (env != NULL && env[0] != '\0') - { - DEBUG_PRINTF("Language via LANG: %s\n", getenv("LANG")); - mysetenv("LANGUAGE", getenv("LANG")); - } - else - { - DEBUG_PRINTF("No language set!\n"); - } - } - } - } + /* Find what language to use from env vars */ + env = getenv("LANGUAGE"); + if (env == NULL || env[0] == '\0') + { + env = getenv("LC_ALL"); + if (env != NULL && env[0] != '\0') + { + DEBUG_PRINTF("Language via LC_ALL: %s\n", getenv("LC_ALL")); + mysetenv("LANGUAGE", getenv("LC_ALL")); + } else + { + env = getenv("LC_MESSAGES"); + if (env != NULL && env[0] != '\0') { - DEBUG_PRINTF("Language was set to '%s'\n", getenv("LANGUAGE")); - } + DEBUG_PRINTF("Language via LC_MESSAGES: %s\n", + getenv("LC_MESSAGES")); + mysetenv("LANGUAGE", getenv("LC_MESSAGES")); + } + else + { + env = getenv("LANG"); + if (env != NULL && env[0] != '\0') + { + DEBUG_PRINTF("Language via LANG: %s\n", getenv("LANG")); + mysetenv("LANGUAGE", getenv("LANG")); + } + else + { + DEBUG_PRINTF("No language set!\n"); + } + } + } } + else + { + DEBUG_PRINTF("Language was set to '%s'\n", getenv("LANGUAGE")); + } + } oldloc = strdup(loc); /* First set the locale according to the environment, then try to overwrite with loc, after that, ctype_utf8() call will test the compatibility with utf8 and try to load a different locale if the resulting one is not compatible. */ - DEBUG_PRINTF("Locale BEFORE is: %s\n", setlocale(LC_ALL, NULL)); //EP + DEBUG_PRINTF("Locale BEFORE is: %s\n", setlocale(LC_ALL, NULL)); //EP setlocale(LC_ALL, ""); setlocale(LC_ALL, loc); ctype_utf8(); - DEBUG_PRINTF("Locale AFTER is: %s\n", setlocale(LC_ALL, NULL)); //EP + DEBUG_PRINTF("Locale AFTER is: %s\n", setlocale(LC_ALL, NULL)); //EP #ifdef BDIST_WIN32 // FIXME: After the update of MinGW/MSYS2 in January 2022, gettext() no longer find @@ -1177,77 +1200,80 @@ static int set_current_language(const char *restrict loc, int * ptr_num_wished_l loc = setlocale(LC_MESSAGES, NULL); if (oldloc && loc && strcmp(oldloc, "") != 0 && strcmp(loc, oldloc) != 0) - { - /* System doesn't recognize that locale! Hack, per Albert C., is to set LC_ALL to a valid UTF-8 locale, then set LANGUAGE to the locale we want to force -bjk 2010.10.05 */ + { + /* System doesn't recognize that locale! Hack, per Albert C., is to set LC_ALL to a valid UTF-8 locale, then set LANGUAGE to the locale we want to force -bjk 2010.10.05 */ - /* Albert's comments from December 2009: - gettext() won't even bother to look up messages unless it - is totally satisfied that you are using one of the locales that - it ships with! Make gettext() unhappy, and it'll switch to the - lobotomized 7-bit Linux "C" locale just to spite you. + /* Albert's comments from December 2009: + gettext() won't even bother to look up messages unless it + is totally satisfied that you are using one of the locales that + it ships with! Make gettext() unhappy, and it'll switch to the + lobotomized 7-bit Linux "C" locale just to spite you. - http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/localedata/SUPPORTED?content-type=text/x-cvsweb-markup&cvsroot=glibc + http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/localedata/SUPPORTED?content-type=text/x-cvsweb-markup&cvsroot=glibc - You can confuse gettext() into mostly behaving. For example, a - user could pick a random UTF-8 locale and change the messages. - In that case, Tux Paint thinks it's in the other locale but the - messages come out right. Like so: LANGUAGE=zam LC_ALL=fr_FR.UTF-8 - It doesn't work to leave LC_ALL unset, set it to "zam", set it to "C", - or set it to random nonsense. Yeah, Tux Paint will think it's in - a French locale, but the messages will be Zapotec nonetheless. + You can confuse gettext() into mostly behaving. For example, a + user could pick a random UTF-8 locale and change the messages. + In that case, Tux Paint thinks it's in the other locale but the + messages come out right. Like so: LANGUAGE=zam LC_ALL=fr_FR.UTF-8 + It doesn't work to leave LC_ALL unset, set it to "zam", set it to "C", + or set it to random nonsense. Yeah, Tux Paint will think it's in + a French locale, but the messages will be Zapotec nonetheless. - Maybe it's time to give up on gettext(). + Maybe it's time to give up on gettext(). - [see also: https://sourceforge.net/mailarchive/message.php?msg_name=787b0d920912222352i5ab22834x92686283b565016b%40mail.gmail.com ] - */ + [see also: https://sourceforge.net/mailarchive/message.php?msg_name=787b0d920912222352i5ab22834x92686283b565016b%40mail.gmail.com ] + */ - /* Unneeded here, this has yet been done as part of ctype_utf8() call before, iterating over a list of locales */ - // setlocale(LC_ALL, "en_US.UTF-8"); /* Is it dumb to assume "en_US" is pretty close to "C" locale? */ + /* Unneeded here, this has yet been done as part of ctype_utf8() call before, iterating over a list of locales */ + // setlocale(LC_ALL, "en_US.UTF-8"); /* Is it dumb to assume "en_US" is pretty close to "C" locale? */ - mysetenv("LANGUAGE", oldloc); - set_langint_from_locale_string(oldloc); - } + mysetenv("LANGUAGE", oldloc); + set_langint_from_locale_string(oldloc); + } else - { + { #ifdef _WIN32 - if (getenv("LANGUAGE") == NULL) - mysetenv("LANGUAGE", loc); + if (getenv("LANGUAGE") == NULL) + mysetenv("LANGUAGE", loc); #endif - if (getenv("LANGUAGE") == NULL) - mysetenv("LANGUAGE", "C"); - } + if (getenv("LANGUAGE") == NULL) + mysetenv("LANGUAGE", "C"); + } env_language = strdup(getenv("LANGUAGE")); if (*env_language) + { + env_language_lang = strtok(env_language, ":"); + while (env_language_lang != NULL) { - env_language_lang = strtok(env_language, ":"); - while (env_language_lang != NULL) + num_wished_langs++; + set_langint_from_locale_string(env_language_lang); + wished_langs[j].langint = langint; + wished_langs[j].lang_prefix = lang_prefixes[langint]; + wished_langs[j].need_own_font = + search_int_array(langint, lang_use_own_font); + wished_langs[j].need_right_to_left = + search_int_array(langint, lang_use_right_to_left); + wished_langs[j].need_right_to_left_word = + search_int_array(langint, lang_use_right_to_left_word); + for (i = 0; lang_y_nudge[i][0] != -1; i++) + { + // printf("lang_y_nudge[%d][0] = %d\n", i, lang_y_nudge[i][0]); + if (lang_y_nudge[i][0] == langint) { - num_wished_langs++; - set_langint_from_locale_string(env_language_lang); - wished_langs[j].langint = langint; - wished_langs[j].lang_prefix = lang_prefixes[langint]; - wished_langs[j].need_own_font = search_int_array(langint, lang_use_own_font); - wished_langs[j].need_right_to_left = search_int_array(langint, lang_use_right_to_left); - wished_langs[j].need_right_to_left_word = search_int_array(langint, lang_use_right_to_left_word); - for (i = 0; lang_y_nudge[i][0] != -1; i++) - { - // printf("lang_y_nudge[%d][0] = %d\n", i, lang_y_nudge[i][0]); - if (lang_y_nudge[i][0] == langint) - { - wished_langs[j].lang_y_nudge = lang_y_nudge[i][1]; - break; - } - } - - - j++; - env_language_lang = strtok(NULL, ":"); + wished_langs[j].lang_y_nudge = lang_y_nudge[i][1]; + break; } - if (*env_language) - free(env_language); + } + + + j++; + env_language_lang = strtok(NULL, ":"); } + if (*env_language) + free(env_language); + } // set_langint_from_locale_string(loc); @@ -1264,13 +1290,15 @@ static int set_current_language(const char *restrict loc, int * ptr_num_wished_l #ifdef DEBUG fprintf(stderr, "DEBUG: Language is %s (%d) %s/%s\n", - lang_prefix, langint, need_right_to_left ? "(RTL)" : "", need_right_to_left_word ? "(RTL words)" : ""); + lang_prefix, langint, need_right_to_left ? "(RTL)" : "", + need_right_to_left_word ? "(RTL words)" : ""); fflush(stderr); #endif free(oldloc); - DEBUG_PRINTF("lang_prefixes[%d] is \"%s\"\n", get_current_language(), lang_prefixes[get_current_language()]); + DEBUG_PRINTF("lang_prefixes[%d] is \"%s\"\n", get_current_language(), + lang_prefixes[get_current_language()]); *ptr_num_wished_langs = num_wished_langs; @@ -1289,27 +1317,28 @@ static int set_current_language(const char *restrict loc, int * ptr_num_wished_l * @param int * a place to return the number of languages we want to use, when scanning stamp descriptions * @return Y-nudge */ -int setup_i18n(const char *restrict lang, const char *restrict locale, int * num_wished_langs) +int setup_i18n(const char *restrict lang, const char *restrict locale, + int *num_wished_langs) { DEBUG_PRINTF("lang %p, locale %p\n", lang, locale); DEBUG_PRINTF("lang \"%s\", locale \"%s\"\n", lang, locale); if (locale) + { + if (!strcmp(locale, "help")) { - if (!strcmp(locale, "help")) - { - show_locale_usage(stdout, "tuxpaint"); - exit(0); - } + show_locale_usage(stdout, "tuxpaint"); + exit(0); } + } else - { - #if defined(__APPLE__) - locale = locale_to_closest_locale(apple_locale()); - #else - locale = ""; - #endif - } + { +#if defined(__APPLE__) + locale = locale_to_closest_locale(apple_locale()); +#else + locale = ""; +#endif + } if (lang) locale = language_to_locale(lang); diff --git a/src/i18n.h b/src/i18n.h index 8a0ef346c..5775d7276 100644 --- a/src/i18n.h +++ b/src/i18n.h @@ -206,7 +206,8 @@ extern w_langs wished_langs[255]; /* Function prototypes: */ int get_current_language(void); -int setup_i18n(const char *restrict lang, const char *restrict locale, int * ptr_num_wished_languages) MUST_CHECK; +int setup_i18n(const char *restrict lang, const char *restrict locale, + int *ptr_num_wished_languages) MUST_CHECK; #ifdef NO_SDLPANGO int smash_i18n(void) MUST_CHECK; diff --git a/src/im.c b/src/im.c index ff8d9ce20..ad20bc16a 100644 --- a/src/im.c +++ b/src/im.c @@ -153,7 +153,7 @@ enum /** * All im_event_*() functions have this type. */ -typedef int (*IM_EVENT_FN) (IM_DATA *, SDL_Event); /* IM_EVENT_FN type */ +typedef int (*IM_EVENT_FN)(IM_DATA *, SDL_Event); /* IM_EVENT_FN type */ /** @@ -243,7 +243,7 @@ static IM_EVENT_FN im_event_fns[NUM_LANGS]; #define ARRAYLEN(a) ( sizeof(a)/sizeof(*(a)) ) -static void wcs_lshift(wchar_t * s, size_t count) +static void wcs_lshift(wchar_t *s, size_t count) { wchar_t *dest = s; wchar_t *src = s + count; @@ -256,9 +256,9 @@ static void wcs_lshift(wchar_t * s, size_t count) /** * Pull out "count" characters from the back. */ -static void wcs_pull(wchar_t * s, size_t count) +static void wcs_pull(wchar_t *s, size_t count) { - int peg = (int)wcslen(s) - (int)count; + int peg = (int) wcslen(s) - (int) count; if (peg < 0) peg = 0; @@ -292,10 +292,10 @@ static int sm_init(STATE_MACHINE * sm) sm->next = calloc(INITIAL_SMSIZE, sizeof(SM_WITH_KEY)); if (!sm->next) - { - perror("sm_init"); - return 1; - } + { + perror("sm_init"); + return 1; + } sm->next_maxsize = INITIAL_SMSIZE; return 0; @@ -308,22 +308,22 @@ static int sm_init(STATE_MACHINE * sm) static void sm_free(STATE_MACHINE * sm) { if (sm->next) + { + int i = 0; + + for (i = 0; i < (int) sm->next_maxsize; i++) { - int i = 0; + STATE_MACHINE *next_state = sm->next[i].state; - for (i = 0; i < (int)sm->next_maxsize; i++) - { - STATE_MACHINE *next_state = sm->next[i].state; - - if (next_state) - sm_free(next_state); - sm->next[i].state = NULL; - } - - free(sm->next); - sm->next = NULL; + if (next_state) + sm_free(next_state); + sm->next[i].state = NULL; } + free(sm->next); + sm->next = NULL; + } + memset(sm, 0, sizeof(STATE_MACHINE)); } @@ -337,10 +337,10 @@ static int sm_dblspace(STATE_MACHINE * sm) SM_WITH_KEY *next = realloc(sm->next, sizeof(SM_WITH_KEY) * newsize); if (next == NULL) - { - perror("sm_dblspace"); - return 1; - } + { + perror("sm_dblspace"); + return 1; + } sm->next = next; sm->next_maxsize = newsize; @@ -358,7 +358,8 @@ static STATE_MACHINE *sm_search_shallow(STATE_MACHINE * sm, char key) SM_WITH_KEY smk = { key, NULL }; SM_WITH_KEY *smk_found; - smk_found = bsearch(&smk, sm->next, sm->next_size, sizeof(SM_WITH_KEY), swk_compare); + smk_found = + bsearch(&smk, sm->next, sm->next_size, sizeof(SM_WITH_KEY), swk_compare); if (!smk_found) return NULL; @@ -379,20 +380,21 @@ static STATE_MACHINE *sm_search_shallow(STATE_MACHINE * sm, char key) * * @return Found unicode character sequence output of the last state. */ -static const wchar_t *sm_search(STATE_MACHINE * start, wchar_t * key, int *matched, STATE_MACHINE ** penult, +static const wchar_t *sm_search(STATE_MACHINE * start, wchar_t *key, + int *matched, STATE_MACHINE ** penult, STATE_MACHINE ** end) { - STATE_MACHINE *sm = sm_search_shallow(start, (char)*key); + STATE_MACHINE *sm = sm_search_shallow(start, (char) *key); const wchar_t *unicode; /* No match - stop recursion */ if (!sm) - { - *matched = 0; - *end = start; + { + *matched = 0; + *end = start; - return start->output; - } + return start->output; + } /* Match - recurse */ *penult = start; @@ -416,57 +418,58 @@ static void sm_sort_shallow(STATE_MACHINE * sm) /** * Add a single sequence-to-unicode path to the state machine. */ -static int sm_add(STATE_MACHINE * sm, char *seq, const wchar_t * unicode, char flag) +static int sm_add(STATE_MACHINE * sm, char *seq, const wchar_t *unicode, + char flag) { STATE_MACHINE *sm_found = sm_search_shallow(sm, seq[0]); /* Empty sequence */ if (seq[0] == '\0') + { + if (wcslen(sm->output)) { - if (wcslen(sm->output)) - { - size_t i; + size_t i; - fprintf(stderr, "Unicode sequence "); - for (i = 0; i < wcslen(sm->output); i++) - fprintf(stderr, "%04X ", (int)sm->output[i]); - fprintf(stderr, " already defined, overriding with "); - for (i = 0; i < wcslen(unicode); i++) - fprintf(stderr, "%04X ", (int)unicode[i]); - fprintf(stderr, "\n"); - } - wcscpy(sm->output, unicode); - sm->flag = flag; - return 0; + fprintf(stderr, "Unicode sequence "); + for (i = 0; i < wcslen(sm->output); i++) + fprintf(stderr, "%04X ", (int) sm->output[i]); + fprintf(stderr, " already defined, overriding with "); + for (i = 0; i < wcslen(unicode); i++) + fprintf(stderr, "%04X ", (int) unicode[i]); + fprintf(stderr, "\n"); } + wcscpy(sm->output, unicode); + sm->flag = flag; + return 0; + } /* The key doesn't exist yet */ if (!sm_found) + { + int index = (int) sm->next_size; + SM_WITH_KEY *next = &sm->next[index]; + + /* Add the key */ + next->key = seq[0]; + next->state = malloc(sizeof(STATE_MACHINE)); + if (!next->state) { - int index = (int)sm->next_size; - SM_WITH_KEY *next = &sm->next[index]; - - /* Add the key */ - next->key = seq[0]; - next->state = malloc(sizeof(STATE_MACHINE)); - if (!next->state) - { - perror("sm_add"); - return 1; - } - sm_init(next->state); - sm_found = next->state; - - /* Increase store for next time, if necessary */ - if (++(sm->next_size) >= sm->next_maxsize) - { - if (sm_dblspace(sm)) - { - fprintf(stderr, "Memory expansion failure\n"); - return 1; - } - } + perror("sm_add"); + return 1; } + sm_init(next->state); + sm_found = next->state; + + /* Increase store for next time, if necessary */ + if (++(sm->next_size) >= sm->next_maxsize) + { + if (sm_dblspace(sm)) + { + fprintf(stderr, "Memory expansion failure\n"); + return 1; + } + } + } /* Recurse */ sm_add(sm_found, seq + 1, unicode, flag); @@ -494,9 +497,9 @@ static int charmap_init(CHARMAP * cm) memset(cm, 0, sizeof(CHARMAP)); for (i = 0; i < MAX_SECTIONS; i++) - { - error_code += sm_init(&cm->sections[i]); - } + { + error_code += sm_init(&cm->sections[i]); + } return error_code; } @@ -513,19 +516,20 @@ static int charmap_init(CHARMAP * cm) * * @return 0 if no error, 1 if error. */ -static int charmap_add(CHARMAP * cm, int section, char *seq, const wchar_t * unicode, char *flag) +static int charmap_add(CHARMAP * cm, int section, char *seq, + const wchar_t *unicode, char *flag) { if (section >= MAX_SECTIONS) - { - fprintf(stderr, "Section count exceeded\n"); - return 1; - } + { + fprintf(stderr, "Section count exceeded\n"); + return 1; + } /* For now, we only utilize one-character flags */ if (strlen(flag) > 1) - { - fprintf(stderr, "%04X: Multi-character flag, truncated.\n", (int)(intptr_t) unicode); //EP added (intptr_t) to avoid warning on x64 - } + { + fprintf(stderr, "%04X: Multi-character flag, truncated.\n", (int) (intptr_t) unicode); //EP added (intptr_t) to avoid warning on x64 + } return sm_add(&cm->sections[section], seq, unicode, flag[0]); } @@ -547,99 +551,99 @@ static int charmap_load(CHARMAP * cm, const char *path) /* Open */ is = fopen(path, "rt"); if (!is) - { - perror("path"); - return 1; - } + { + perror("path"); + return 1; + } /* Load */ while (!feof(is)) + { + wchar_t unicode[MAX_UNICODE_SEQ]; + int ulen = 0; + + char buf[256]; + char flag[256]; + + int scanned = 0; + + /* Scan a single token first */ + scanned = fscanf(is, "%255s", buf); + if (scanned < 0) + break; + if (scanned == 0) { - wchar_t unicode[MAX_UNICODE_SEQ]; - int ulen = 0; + fprintf(stderr, "%s: Character map syntax error\n", path); + return 1; + } - char buf[256]; - char flag[256]; + /* Handle the first argument */ + if (strcmp(buf, "section") == 0) + { /* Section division */ + section++; + continue; + } + else if (buf[0] == '#') + { /* Comment */ + scanned = fscanf(is, "%*[^\n]"); + continue; + } + else + { + char *bp = buf; + int u; - int scanned = 0; - - /* Scan a single token first */ - scanned = fscanf(is, "%255s", buf); - if (scanned < 0) - break; - if (scanned == 0) + do + { + if (sscanf(bp, "%x", &u) == 1) + { /* Unicode */ + unicode[ulen++] = u; + } + else { - fprintf(stderr, "%s: Character map syntax error\n", path); + fprintf(stderr, "%s: Syntax error at '%s'\n", path, buf); return 1; } - /* Handle the first argument */ - if (strcmp(buf, "section") == 0) - { /* Section division */ - section++; - continue; - } - else if (buf[0] == '#') - { /* Comment */ - scanned = fscanf(is, "%*[^\n]"); - continue; - } - else - { - char *bp = buf; - int u; + bp = strchr(bp, ':'); + if (bp) + bp++; + } + while (bp && ulen < MAX_UNICODE_SEQ - 1); + unicode[ulen] = L'\0'; + } - do - { - if (sscanf(bp, "%x", &u) == 1) - { /* Unicode */ - unicode[ulen++] = u; - } - else - { - fprintf(stderr, "%s: Syntax error at '%s'\n", path, buf); - return 1; - } + /* Scan some more */ + scanned = fscanf(is, "%255s\t%255s", buf, flag); + if (scanned < 0) + break; - bp = strchr(bp, ':'); - if (bp) - bp++; - } - while (bp && ulen < MAX_UNICODE_SEQ - 1); - unicode[ulen] = L'\0'; - } + /* Input count checking */ + switch (scanned) + { + case 0: + case 1: + fprintf(stderr, "%s: Character map syntax error\n", path); + return 1; - /* Scan some more */ - scanned = fscanf(is, "%255s\t%255s", buf, flag); - if (scanned < 0) - break; - - /* Input count checking */ - switch (scanned) - { - case 0: - case 1: - fprintf(stderr, "%s: Character map syntax error\n", path); - return 1; - - default: - if (charmap_add(cm, section, buf, unicode, flag)) - { + default: + if (charmap_add(cm, section, buf, unicode, flag)) + { #ifndef __BEOS__ #if defined __GLIBC__ && __GLIBC__ == 2 && __GLIBC_MINOR__ >=2 || __GLIBC__ > 2 || __APPLE__ - size_t i = 0; + size_t i = 0; - fwprintf(stderr, L"Unable to add sequence '%ls', unicode ", buf); - for (i = 0; i < wcslen(unicode); i++) - fwprintf(stderr, L"%04X ", (int)unicode[i]); - fwprintf(stderr, L"in section %d\n", section); + fwprintf(stderr, L"Unable to add sequence '%ls', unicode ", buf); + for (i = 0; i < wcslen(unicode); i++) + fwprintf(stderr, L"%04X ", (int) unicode[i]); + fwprintf(stderr, L"in section %d\n", section); #endif #endif - error_code = 1; - } - } + error_code = 1; + } } + } /* Close */ fclose(is); @@ -656,9 +660,9 @@ static void charmap_free(CHARMAP * cm) int i; for (i = 0; i < MAX_SECTIONS; i++) - { - sm_free(&cm->sections[i]); - } + { + sm_free(&cm->sections[i]); + } memset(cm, 0, sizeof(CHARMAP)); } @@ -667,7 +671,7 @@ static void charmap_free(CHARMAP * cm) /** * Search for a matching character string in the character map. */ -static const wchar_t *charmap_search(CHARMAP * cm, wchar_t * s) +static const wchar_t *charmap_search(CHARMAP * cm, wchar_t *s) { STATE_MACHINE *start; const wchar_t *unicode; @@ -675,13 +679,15 @@ static const wchar_t *charmap_search(CHARMAP * cm, wchar_t * s) /* Determine the starting state based on the charmap's active section */ section = cm->section; - if (!IN_RANGE(0, section, (int)ARRAYLEN(cm->sections))) + if (!IN_RANGE(0, section, (int) ARRAYLEN(cm->sections))) section = 0; start = &cm->sections[section]; cm->match_state = NULL; cm->match_state_prev = NULL; - unicode = sm_search(start, s, &cm->match_count, &cm->match_state_prev, &cm->match_state); + unicode = + sm_search(start, s, &cm->match_count, &cm->match_state_prev, + &cm->match_state); /** * Determine whether the match is final. A match is considered to be final @@ -695,22 +701,22 @@ static const wchar_t *charmap_search(CHARMAP * cm, wchar_t * s) * final state we possibly can. */ cm->match_is_final = 0; - if (cm->match_count < (int)wcslen(s)) - { - cm->match_is_final = 1; - } + if (cm->match_count < (int) wcslen(s)) + { + cm->match_is_final = 1; + } /* Statistics */ cm->match_stats = MATCH_STAT_NONE; if (cm->match_state->next_size == 0) - { - cm->match_is_final = 1; - cm->match_stats |= MATCH_STAT_NOMOSTATES; - } - if (cm->match_count == (int)wcslen(s)) - { - cm->match_stats |= MATCH_STAT_NOMOBUF; - } + { + cm->match_is_final = 1; + cm->match_stats |= MATCH_STAT_NOMOSTATES; + } + if (cm->match_count == (int) wcslen(s)) + { + cm->match_stats |= MATCH_STAT_NOMOBUF; + } return unicode; } @@ -736,23 +742,23 @@ static int im_event_c(IM_DATA * im, SDL_Event event) /* Handle key stroke */ switch (ks.sym) - { - case SDLK_BACKSPACE: - im->s[0] = L'\b'; - im->s[1] = L'\0'; - break; - case SDLK_TAB: - im->s[0] = L'\t'; - im->s[1] = L'\0'; - break; - case SDLK_RETURN: - im->s[0] = L'\r'; - im->s[1] = L'\0'; - break; - default: - mbstowcs(im->s, event.text.text, 16); - //default: wcsncpy(im->s , event.text.text, 16); - } + { + case SDLK_BACKSPACE: + im->s[0] = L'\b'; + im->s[1] = L'\0'; + break; + case SDLK_TAB: + im->s[0] = L'\t'; + im->s[1] = L'\0'; + break; + case SDLK_RETURN: + im->s[0] = L'\r'; + im->s[1] = L'\0'; + break; + default: + mbstowcs(im->s, event.text.text, 16); + //default: wcsncpy(im->s , event.text.text, 16); + } //im->s[1] = L'\0'; #ifdef IM_DEBUG @@ -792,27 +798,27 @@ int im_read(IM_DATA * im, SDL_Event event) Right procedure would be to modify the different im_event_LL() functions, but for now letting as this as it is what the functions expect. */ switch (event.key.keysym.sym) - { - case SDLK_BACKSPACE: - event.text.text[0] = L'\b'; - event.text.text[1] = L'\0'; - break; - case SDLK_RETURN: - event.text.text[0] = L'\r'; - event.text.text[1] = L'\0'; - break; - case SDLK_TAB: - event.text.text[0] = L'\t'; - event.text.text[1] = L'\0'; - break; - } + { + case SDLK_BACKSPACE: + event.text.text[0] = L'\b'; + event.text.text[1] = L'\0'; + break; + case SDLK_RETURN: + event.text.text[0] = L'\r'; + event.text.text[1] = L'\0'; + break; + case SDLK_TAB: + event.text.text[0] = L'\t'; + event.text.text[1] = L'\0'; + break; + } /* Sanity check */ if (im->lang < 0 || im->lang >= NUM_LANGS) - { - fprintf(stderr, "im->lang out of range (%d), using default\n", im->lang); - im->lang = LANG_DEFAULT; - } + { + fprintf(stderr, "im->lang out of range (%d), using default\n", im->lang); + im->lang = LANG_DEFAULT; + } /* Function pointer to the language-specific im_event_* function */ im_event_fp = im_event_fns[im->lang]; @@ -824,7 +830,8 @@ int im_read(IM_DATA * im, SDL_Event event) redraw = im_event_c(im, event); #ifdef IM_DEBUG - wprintf(L"* [%8ls] [%8ls] %2d %2d (%2d)\n", im->s, im->buf, wcslen(im->s), wcslen(im->buf), im->redraw); + wprintf(L"* [%8ls] [%8ls] %2d %2d (%2d)\n", im->s, im->buf, wcslen(im->s), + wcslen(im->buf), im->redraw); #endif return redraw; @@ -948,207 +955,210 @@ static int im_event_zh_tw(IM_DATA * im, SDL_Event event) /* Handle event requests */ switch (im->request) + { + case 0: + break; + + case IM_REQ_FREE: /* Free allocated resources */ + charmap_free(&cm); + FALL_THROUGH; /* go onto full reset */ + + case IM_REQ_RESET_FULL: /* Full reset */ + cm.section = SEC_ENGLISH; + im->tip_text = im_tip_text[IM_TIP_ENGLISH]; + FALL_THROUGH; /* go onto soft reset */ + + case IM_REQ_RESET_SOFT: /* Soft reset */ + im->s[0] = L'\0'; + im->buf[0] = L'\0'; + im->redraw = 0; + cm.match_count = 0; + cm.match_is_final = 0; + cm.match_state = &cm.sections[cm.section]; + cm.match_state_prev = &cm.sections[cm.section]; + break; + + case IM_REQ_INIT: /* Initialization */ + charmap_init(&cm); + + if (charmap_load(&cm, lang_file)) { - case 0: - break; + fprintf(stderr, "Unable to load %s, defaulting to im_event_c\n", + lang_file); + im->lang = LANG_DEFAULT; + return im_event_c(im, event); + } - case IM_REQ_FREE: /* Free allocated resources */ - charmap_free(&cm); - FALL_THROUGH; /* go onto full reset */ - - case IM_REQ_RESET_FULL: /* Full reset */ - cm.section = SEC_ENGLISH; - im->tip_text = im_tip_text[IM_TIP_ENGLISH]; - FALL_THROUGH; /* go onto soft reset */ - - case IM_REQ_RESET_SOFT: /* Soft reset */ - im->s[0] = L'\0'; - im->buf[0] = L'\0'; - im->redraw = 0; - cm.match_count = 0; - cm.match_is_final = 0; - cm.match_state = &cm.sections[cm.section]; - cm.match_state_prev = &cm.sections[cm.section]; - break; - - case IM_REQ_INIT: /* Initialization */ - charmap_init(&cm); - - if (charmap_load(&cm, lang_file)) - { - fprintf(stderr, "Unable to load %s, defaulting to im_event_c\n", lang_file); - im->lang = LANG_DEFAULT; - return im_event_c(im, event); - } - - im_fullreset(im); + im_fullreset(im); #ifdef DEBUG - printf("IM: Loaded '%s'\n", lang_file); + printf("IM: Loaded '%s'\n", lang_file); #endif - break; - } + break; + } if (im->request != IM_REQ_TRANSLATE) return 0; /* Discard redraw characters, so they can be redrawn */ - if ((int)wcslen(im->s) < im->redraw) + if ((int) wcslen(im->s) < im->redraw) im->redraw = wcslen(im->s); wcs_lshift(im->s, (wcslen(im->s) - im->redraw)); /* Handle keys */ switch (ks.sym) + { + /* Keys to ignore */ + case SDLK_NUMLOCKCLEAR: + case SDLK_CAPSLOCK: + case SDLK_SCROLLLOCK: + case SDLK_LSHIFT: + case SDLK_RSHIFT: + case SDLK_LCTRL: + case SDLK_RCTRL: + case SDLK_LGUI: + case SDLK_RGUI: + case SDLK_MENU: + case SDLK_MODE: + case SDLK_APPLICATION: + break; + + /* Left-Alt & Right-Alt mapped to mode-switch */ + case SDLK_RALT: + case SDLK_LALT: + cm.section = ((cm.section + 1) % SEC_TOTAL); /* Change section */ + im_softreset(im); /* Soft reset */ + + /* Set tip text */ + switch (cm.section) { - /* Keys to ignore */ - case SDLK_NUMLOCKCLEAR: - case SDLK_CAPSLOCK: - case SDLK_SCROLLLOCK: - case SDLK_LSHIFT: - case SDLK_RSHIFT: - case SDLK_LCTRL: - case SDLK_RCTRL: - case SDLK_LGUI: - case SDLK_RGUI: - case SDLK_MENU: - case SDLK_MODE: - case SDLK_APPLICATION: + case SEC_ENGLISH: + im->tip_text = im_tip_text[IM_TIP_ENGLISH]; break; - - /* Left-Alt & Right-Alt mapped to mode-switch */ - case SDLK_RALT: - case SDLK_LALT: - cm.section = ((cm.section + 1) % SEC_TOTAL); /* Change section */ - im_softreset(im); /* Soft reset */ - - /* Set tip text */ - switch (cm.section) - { - case SEC_ENGLISH: - im->tip_text = im_tip_text[IM_TIP_ENGLISH]; - break; - case SEC_ZH_TW: - im->tip_text = im_tip_text[IM_TIP_ZH_TW]; - break; - } + case SEC_ZH_TW: + im->tip_text = im_tip_text[IM_TIP_ZH_TW]; break; + } + break; - /* Enter finalizes previous redraw */ - case SDLK_RETURN: - if (im->redraw <= 0) - { - im->s[0] = L'\r'; - im->s[1] = L'\0'; - } - im->buf[0] = L'\0'; - im->redraw = 0; - break; + /* Enter finalizes previous redraw */ + case SDLK_RETURN: + if (im->redraw <= 0) + { + im->s[0] = L'\r'; + im->s[1] = L'\0'; + } + im->buf[0] = L'\0'; + im->redraw = 0; + break; - /* Actual character processing */ - default: - if (event.type == SDL_TEXTINPUT || ks.sym == SDLK_BACKSPACE || ks.sym == SDLK_RETURN || ks.sym == SDLK_TAB) - { - /* English mode */ - if (cm.section == SEC_ENGLISH) - { - mbstowcs(im->s, event.text.text, 16); + /* Actual character processing */ + default: + if (event.type == SDL_TEXTINPUT || ks.sym == SDLK_BACKSPACE + || ks.sym == SDLK_RETURN || ks.sym == SDLK_TAB) + { + /* English mode */ + if (cm.section == SEC_ENGLISH) + { + mbstowcs(im->s, event.text.text, 16); // im->s[0] = event.text.text[0]; // im->s[1] = L'\0'; - im->buf[0] = L'\0'; - } - /* ZH_TW mode */ - else + im->buf[0] = L'\0'; + } + /* ZH_TW mode */ + else + { + wchar_t u = event.text.text[0]; + + im->s[0] = L'\0'; /* Zero-out output string */ + wcsncat(im->buf, &u, 1); /* Copy new character */ + + /* Translate the characters */ + im->redraw = 0; + while (1) + { + const wchar_t *us = charmap_search(&cm, im->buf); + +#ifdef IM_DEBUG + wprintf(L" [%8ls] [%8ls] %2d %2d\n", im->s, im->buf, wcslen(im->s), + wcslen(im->buf)); +#endif + + /* Match was found? */ + if (us && wcslen(us)) + { +#ifdef IM_DEBUG + wprintf(L" 1\n"); +#endif + + wcscat(im->s, us); + + /* Final match */ + if (cm.match_is_final) { - wchar_t u = event.text.text[0]; - - im->s[0] = L'\0'; /* Zero-out output string */ - wcsncat(im->buf, &u, 1); /* Copy new character */ - - /* Translate the characters */ - im->redraw = 0; - while (1) - { - const wchar_t *us = charmap_search(&cm, im->buf); - -#ifdef IM_DEBUG - wprintf(L" [%8ls] [%8ls] %2d %2d\n", im->s, im->buf, wcslen(im->s), wcslen(im->buf)); -#endif - - /* Match was found? */ - if (us && wcslen(us)) - { -#ifdef IM_DEBUG - wprintf(L" 1\n"); -#endif - - wcscat(im->s, us); - - /* Final match */ - if (cm.match_is_final) - { - wcs_lshift(im->buf, cm.match_count); - cm.match_count = 0; - cm.match_is_final = 0; - } - /* May need to be overwritten next time */ - else - { - im->redraw += wcslen(us); - break; - } - } - /* No match, but more data is in the buffer */ - else if (wcslen(im->buf) > 0) - { - /* If the input character has no state, it's its own state */ - if (cm.match_count == 0) - { -#ifdef IM_DEBUG - wprintf(L" 2a\n"); -#endif - wcsncat(im->s, im->buf, 1); - wcs_lshift(im->buf, 1); - cm.match_is_final = 0; - } - /* If the matched characters didn't consume all, it's own state */ - else if ((size_t) cm.match_count != wcslen(im->buf)) - { -#ifdef IM_DEBUG - wprintf(L" 2b (%2d)\n", cm.match_count); -#endif - wcsncat(im->s, im->buf, 1); - wcs_lshift(im->buf, 1); - cm.match_is_final = 0; - } - /* Otherwise it's just a part of a future input */ - else - { -#ifdef IM_DEBUG - wprintf(L" 2c (%2d)\n", cm.match_count); -#endif - wcscat(im->s, im->buf); - cm.match_is_final = 0; - im->redraw += wcslen(im->buf); - break; - } - } - /* No match and no more data in the buffer */ - else - { -#ifdef IM_DEBUG - wprintf(L" 3\n"); -#endif - break; - } - - /* Is this the end? */ - if (cm.match_is_final) - break; - } + wcs_lshift(im->buf, cm.match_count); + cm.match_count = 0; + cm.match_is_final = 0; } + /* May need to be overwritten next time */ + else + { + im->redraw += wcslen(us); + break; + } + } + /* No match, but more data is in the buffer */ + else if (wcslen(im->buf) > 0) + { + /* If the input character has no state, it's its own state */ + if (cm.match_count == 0) + { +#ifdef IM_DEBUG + wprintf(L" 2a\n"); +#endif + wcsncat(im->s, im->buf, 1); + wcs_lshift(im->buf, 1); + cm.match_is_final = 0; + } + /* If the matched characters didn't consume all, it's own state */ + else if ((size_t) cm.match_count != wcslen(im->buf)) + { +#ifdef IM_DEBUG + wprintf(L" 2b (%2d)\n", cm.match_count); +#endif + wcsncat(im->s, im->buf, 1); + wcs_lshift(im->buf, 1); + cm.match_is_final = 0; + } + /* Otherwise it's just a part of a future input */ + else + { +#ifdef IM_DEBUG + wprintf(L" 2c (%2d)\n", cm.match_count); +#endif + wcscat(im->s, im->buf); + cm.match_is_final = 0; + im->redraw += wcslen(im->buf); + break; + } + } + /* No match and no more data in the buffer */ + else + { +#ifdef IM_DEBUG + wprintf(L" 3\n"); +#endif + break; + } + + /* Is this the end? */ + if (cm.match_is_final) + break; } + } } + } return im->redraw; } @@ -1171,204 +1181,206 @@ static int im_event_th(IM_DATA * im, SDL_Event event) /* Handle event requests */ switch (im->request) + { + case 0: + break; + + case IM_REQ_FREE: /* Free allocated resources */ + charmap_free(&cm); + FALL_THROUGH; /* go onto full reset */ + + case IM_REQ_RESET_FULL: /* Full reset */ + cm.section = SEC_ENGLISH; + im->tip_text = im_tip_text[IM_TIP_ENGLISH]; + FALL_THROUGH; /* go onto soft reset */ + + case IM_REQ_RESET_SOFT: /* Soft reset */ + im->s[0] = L'\0'; + im->buf[0] = L'\0'; + im->redraw = 0; + cm.match_count = 0; + cm.match_is_final = 0; + cm.match_state = &cm.sections[cm.section]; + cm.match_state_prev = &cm.sections[cm.section]; + break; + + case IM_REQ_INIT: /* Initialization */ + charmap_init(&cm); + + if (charmap_load(&cm, lang_file)) { - case 0: - break; + fprintf(stderr, "Unable to load %s, defaulting to im_event_c\n", + lang_file); + im->lang = LANG_DEFAULT; + return im_event_c(im, event); + } - case IM_REQ_FREE: /* Free allocated resources */ - charmap_free(&cm); - FALL_THROUGH; /* go onto full reset */ - - case IM_REQ_RESET_FULL: /* Full reset */ - cm.section = SEC_ENGLISH; - im->tip_text = im_tip_text[IM_TIP_ENGLISH]; - FALL_THROUGH; /* go onto soft reset */ - - case IM_REQ_RESET_SOFT: /* Soft reset */ - im->s[0] = L'\0'; - im->buf[0] = L'\0'; - im->redraw = 0; - cm.match_count = 0; - cm.match_is_final = 0; - cm.match_state = &cm.sections[cm.section]; - cm.match_state_prev = &cm.sections[cm.section]; - break; - - case IM_REQ_INIT: /* Initialization */ - charmap_init(&cm); - - if (charmap_load(&cm, lang_file)) - { - fprintf(stderr, "Unable to load %s, defaulting to im_event_c\n", lang_file); - im->lang = LANG_DEFAULT; - return im_event_c(im, event); - } - - im_fullreset(im); + im_fullreset(im); #ifdef DEBUG - printf("IM: Loaded '%s'\n", lang_file); + printf("IM: Loaded '%s'\n", lang_file); #endif - break; - } + break; + } if (im->request != IM_REQ_TRANSLATE) return 0; /* Discard redraw characters, so they can be redrawn */ - if ((int)wcslen(im->s) < im->redraw) + if ((int) wcslen(im->s) < im->redraw) im->redraw = wcslen(im->s); wcs_lshift(im->s, (wcslen(im->s) - im->redraw)); /* Handle keys */ switch (ks.sym) + { + /* Keys to ignore */ + case SDLK_NUMLOCKCLEAR: + case SDLK_CAPSLOCK: + case SDLK_SCROLLLOCK: + case SDLK_LSHIFT: + case SDLK_RSHIFT: + case SDLK_LCTRL: + case SDLK_RCTRL: + case SDLK_LALT: + case SDLK_LGUI: + case SDLK_RGUI: + case SDLK_MENU: + case SDLK_MODE: + case SDLK_APPLICATION: + break; + + /* Right-Alt mapped to mode-switch */ + case SDLK_RALT: + cm.section = ((cm.section + 1) % SEC_TOTAL); /* Change section */ + im_softreset(im); /* Soft reset */ + + /* Set tip text */ + switch (cm.section) { - /* Keys to ignore */ - case SDLK_NUMLOCKCLEAR: - case SDLK_CAPSLOCK: - case SDLK_SCROLLLOCK: - case SDLK_LSHIFT: - case SDLK_RSHIFT: - case SDLK_LCTRL: - case SDLK_RCTRL: - case SDLK_LALT: - case SDLK_LGUI: - case SDLK_RGUI: - case SDLK_MENU: - case SDLK_MODE: - case SDLK_APPLICATION: + case SEC_ENGLISH: + im->tip_text = im_tip_text[IM_TIP_ENGLISH]; break; - - /* Right-Alt mapped to mode-switch */ - case SDLK_RALT: - cm.section = ((cm.section + 1) % SEC_TOTAL); /* Change section */ - im_softreset(im); /* Soft reset */ - - /* Set tip text */ - switch (cm.section) - { - case SEC_ENGLISH: - im->tip_text = im_tip_text[IM_TIP_ENGLISH]; - break; - case SEC_THAI: - im->tip_text = im_tip_text[IM_TIP_THAI]; - break; - } + case SEC_THAI: + im->tip_text = im_tip_text[IM_TIP_THAI]; break; - - /* Enter finalizes previous redraw */ - case SDLK_RETURN: - if (im->redraw <= 0) - { - im->s[0] = L'\r'; - im->s[1] = L'\0'; - } - im->buf[0] = L'\0'; - im->redraw = 0; - break; - - /* Actual character processing */ - default: - /* English mode */ - if (cm.section == SEC_ENGLISH) - { - // im->s[0] = event.text.text[0]; - mbstowcs(im->s, event.text.text, 16); - //im->s[1] = L'\0'; - im->buf[0] = L'\0'; - } - /* Thai mode */ - else - { - wchar_t u = event.text.text[0]; - - im->s[0] = L'\0'; /* Zero-out output string */ - wcsncat(im->buf, &u, 1); /* Copy new character */ - - /* Translate the characters */ - im->redraw = 0; - while (1) - { - const wchar_t *us = charmap_search(&cm, im->buf); - -#ifdef IM_DEBUG - wprintf(L" [%8ls] [%8ls] %2d %2d\n", im->s, im->buf, wcslen(im->s), wcslen(im->buf)); -#endif - - /* Match was found? */ - if (us && wcslen(us)) - { -#ifdef IM_DEBUG - wprintf(L" 1\n"); -#endif - - wcscat(im->s, us); - - /* Final match */ - if (cm.match_is_final) - { - wcs_lshift(im->buf, cm.match_count); - cm.match_count = 0; - cm.match_is_final = 0; - } - /* May need to be overwritten next time */ - else - { - im->redraw += wcslen(us); - break; - } - } - /* No match, but more data is in the buffer */ - else if (wcslen(im->buf) > 0) - { - /* If the input character has no state, it's its own state */ - if (cm.match_count == 0) - { -#ifdef IM_DEBUG - wprintf(L" 2a\n"); -#endif - wcsncat(im->s, im->buf, 1); - wcs_lshift(im->buf, 1); - cm.match_is_final = 0; - } - /* If the matched characters didn't consume all, it's own state */ - else if ((size_t) cm.match_count != wcslen(im->buf)) - { -#ifdef IM_DEBUG - wprintf(L" 2b (%2d)\n", cm.match_count); -#endif - wcsncat(im->s, im->buf, 1); - wcs_lshift(im->buf, 1); - cm.match_is_final = 0; - } - /* Otherwise it's just a part of a future input */ - else - { -#ifdef IM_DEBUG - wprintf(L" 2c (%2d)\n", cm.match_count); -#endif - wcscat(im->s, im->buf); - cm.match_is_final = 0; - im->redraw += wcslen(im->buf); - break; - } - } - /* No match and no more data in the buffer */ - else - { -#ifdef IM_DEBUG - wprintf(L" 3\n"); -#endif - break; - } - - /* Is this the end? */ - if (cm.match_is_final) - break; - } - } } + break; + + /* Enter finalizes previous redraw */ + case SDLK_RETURN: + if (im->redraw <= 0) + { + im->s[0] = L'\r'; + im->s[1] = L'\0'; + } + im->buf[0] = L'\0'; + im->redraw = 0; + break; + + /* Actual character processing */ + default: + /* English mode */ + if (cm.section == SEC_ENGLISH) + { + // im->s[0] = event.text.text[0]; + mbstowcs(im->s, event.text.text, 16); + //im->s[1] = L'\0'; + im->buf[0] = L'\0'; + } + /* Thai mode */ + else + { + wchar_t u = event.text.text[0]; + + im->s[0] = L'\0'; /* Zero-out output string */ + wcsncat(im->buf, &u, 1); /* Copy new character */ + + /* Translate the characters */ + im->redraw = 0; + while (1) + { + const wchar_t *us = charmap_search(&cm, im->buf); + +#ifdef IM_DEBUG + wprintf(L" [%8ls] [%8ls] %2d %2d\n", im->s, im->buf, wcslen(im->s), + wcslen(im->buf)); +#endif + + /* Match was found? */ + if (us && wcslen(us)) + { +#ifdef IM_DEBUG + wprintf(L" 1\n"); +#endif + + wcscat(im->s, us); + + /* Final match */ + if (cm.match_is_final) + { + wcs_lshift(im->buf, cm.match_count); + cm.match_count = 0; + cm.match_is_final = 0; + } + /* May need to be overwritten next time */ + else + { + im->redraw += wcslen(us); + break; + } + } + /* No match, but more data is in the buffer */ + else if (wcslen(im->buf) > 0) + { + /* If the input character has no state, it's its own state */ + if (cm.match_count == 0) + { +#ifdef IM_DEBUG + wprintf(L" 2a\n"); +#endif + wcsncat(im->s, im->buf, 1); + wcs_lshift(im->buf, 1); + cm.match_is_final = 0; + } + /* If the matched characters didn't consume all, it's own state */ + else if ((size_t) cm.match_count != wcslen(im->buf)) + { +#ifdef IM_DEBUG + wprintf(L" 2b (%2d)\n", cm.match_count); +#endif + wcsncat(im->s, im->buf, 1); + wcs_lshift(im->buf, 1); + cm.match_is_final = 0; + } + /* Otherwise it's just a part of a future input */ + else + { +#ifdef IM_DEBUG + wprintf(L" 2c (%2d)\n", cm.match_count); +#endif + wcscat(im->s, im->buf); + cm.match_is_final = 0; + im->redraw += wcslen(im->buf); + break; + } + } + /* No match and no more data in the buffer */ + else + { +#ifdef IM_DEBUG + wprintf(L" 3\n"); +#endif + break; + } + + /* Is this the end? */ + if (cm.match_is_final) + break; + } + } + } return im->redraw; } @@ -1391,210 +1403,213 @@ static int im_event_ja(IM_DATA * im, SDL_Event event) /* Handle event requests */ switch (im->request) + { + case 0: + break; + + case IM_REQ_FREE: /* Free allocated resources */ + charmap_free(&cm); + FALL_THROUGH; /* go onto full reset */ + + case IM_REQ_RESET_FULL: /* Full reset */ + cm.section = SEC_ENGLISH; + im->tip_text = im_tip_text[IM_TIP_ENGLISH]; + FALL_THROUGH; /* go onto soft reset */ + + case IM_REQ_RESET_SOFT: /* Soft reset */ + im->s[0] = L'\0'; + im->buf[0] = L'\0'; + im->redraw = 0; + cm.match_count = 0; + cm.match_is_final = 0; + cm.match_state = &cm.sections[cm.section]; + cm.match_state_prev = &cm.sections[cm.section]; + break; + + case IM_REQ_INIT: /* Initialization */ + charmap_init(&cm); + + if (charmap_load(&cm, lang_file)) { - case 0: - break; + fprintf(stderr, "Unable to load %s, defaulting to im_event_c\n", + lang_file); + im->lang = LANG_DEFAULT; + return im_event_c(im, event); + } - case IM_REQ_FREE: /* Free allocated resources */ - charmap_free(&cm); - FALL_THROUGH; /* go onto full reset */ - - case IM_REQ_RESET_FULL: /* Full reset */ - cm.section = SEC_ENGLISH; - im->tip_text = im_tip_text[IM_TIP_ENGLISH]; - FALL_THROUGH; /* go onto soft reset */ - - case IM_REQ_RESET_SOFT: /* Soft reset */ - im->s[0] = L'\0'; - im->buf[0] = L'\0'; - im->redraw = 0; - cm.match_count = 0; - cm.match_is_final = 0; - cm.match_state = &cm.sections[cm.section]; - cm.match_state_prev = &cm.sections[cm.section]; - break; - - case IM_REQ_INIT: /* Initialization */ - charmap_init(&cm); - - if (charmap_load(&cm, lang_file)) - { - fprintf(stderr, "Unable to load %s, defaulting to im_event_c\n", lang_file); - im->lang = LANG_DEFAULT; - return im_event_c(im, event); - } - - im_fullreset(im); + im_fullreset(im); #ifdef DEBUG - printf("IM: Loaded '%s'\n", lang_file); + printf("IM: Loaded '%s'\n", lang_file); #endif - break; - } + break; + } if (im->request != IM_REQ_TRANSLATE) return 0; /* Discard redraw characters, so they can be redrawn */ - if ((int)wcslen(im->s) < im->redraw) + if ((int) wcslen(im->s) < im->redraw) im->redraw = wcslen(im->s); wcs_lshift(im->s, (wcslen(im->s) - im->redraw)); /* Handle keys */ switch (ks.sym) + { + /* Keys to ignore */ + case SDLK_NUMLOCKCLEAR: + case SDLK_CAPSLOCK: + case SDLK_SCROLLLOCK: + case SDLK_LSHIFT: + case SDLK_RSHIFT: + case SDLK_LCTRL: + case SDLK_RCTRL: + case SDLK_LGUI: + case SDLK_RGUI: + case SDLK_MENU: + case SDLK_MODE: + case SDLK_APPLICATION: + break; + + /* Left-Alt & Right-Alt mapped to mode-switch */ + case SDLK_RALT: + case SDLK_LALT: + cm.section = ((cm.section + 1) % SEC_TOTAL); /* Change section */ + im_softreset(im); /* Soft reset */ + + /* Set tip text */ + switch (cm.section) { - /* Keys to ignore */ - case SDLK_NUMLOCKCLEAR: - case SDLK_CAPSLOCK: - case SDLK_SCROLLLOCK: - case SDLK_LSHIFT: - case SDLK_RSHIFT: - case SDLK_LCTRL: - case SDLK_RCTRL: - case SDLK_LGUI: - case SDLK_RGUI: - case SDLK_MENU: - case SDLK_MODE: - case SDLK_APPLICATION: + case SEC_ENGLISH: + im->tip_text = im_tip_text[IM_TIP_ENGLISH]; break; - - /* Left-Alt & Right-Alt mapped to mode-switch */ - case SDLK_RALT: - case SDLK_LALT: - cm.section = ((cm.section + 1) % SEC_TOTAL); /* Change section */ - im_softreset(im); /* Soft reset */ - - /* Set tip text */ - switch (cm.section) - { - case SEC_ENGLISH: - im->tip_text = im_tip_text[IM_TIP_ENGLISH]; - break; - case SEC_HIRAGANA: - im->tip_text = im_tip_text[IM_TIP_HIRAGANA]; - break; - case SEC_KATAKANA: - im->tip_text = im_tip_text[IM_TIP_KATAKANA]; - break; - } + case SEC_HIRAGANA: + im->tip_text = im_tip_text[IM_TIP_HIRAGANA]; break; - - /* Enter finalizes previous redraw */ - case SDLK_RETURN: - if (im->redraw <= 0) - { - im->s[0] = L'\r'; - im->s[1] = L'\0'; - } - im->buf[0] = L'\0'; - im->redraw = 0; + case SEC_KATAKANA: + im->tip_text = im_tip_text[IM_TIP_KATAKANA]; break; + } + break; - /* Actual character processing */ - default: - if (event.type == SDL_TEXTINPUT || ks.sym == SDLK_BACKSPACE || ks.sym == SDLK_RETURN || ks.sym == SDLK_TAB) - { - /* English mode */ - if (cm.section == SEC_ENGLISH) - { - mbstowcs(im->s, event.text.text, 16); + /* Enter finalizes previous redraw */ + case SDLK_RETURN: + if (im->redraw <= 0) + { + im->s[0] = L'\r'; + im->s[1] = L'\0'; + } + im->buf[0] = L'\0'; + im->redraw = 0; + break; + + /* Actual character processing */ + default: + if (event.type == SDL_TEXTINPUT || ks.sym == SDLK_BACKSPACE + || ks.sym == SDLK_RETURN || ks.sym == SDLK_TAB) + { + /* English mode */ + if (cm.section == SEC_ENGLISH) + { + mbstowcs(im->s, event.text.text, 16); // im->s[0] = event.text.text[0]; // im->s[1] = L'\0'; - im->buf[0] = L'\0'; - } - /* Hiragana and Katakana modes */ - else + im->buf[0] = L'\0'; + } + /* Hiragana and Katakana modes */ + else + { + wchar_t u = event.text.text[0]; + + im->s[0] = L'\0'; /* Zero-out output string */ + wcsncat(im->buf, &u, 1); /* Copy new character */ + + /* Translate the characters */ + im->redraw = 0; + while (1) + { + const wchar_t *us = charmap_search(&cm, im->buf); + +#ifdef IM_DEBUG + wprintf(L" [%8ls] [%8ls] %2d %2d\n", im->s, im->buf, wcslen(im->s), + wcslen(im->buf)); +#endif + + /* Match was found? */ + if (us && wcslen(us)) + { +#ifdef IM_DEBUG + wprintf(L" 1\n"); +#endif + + wcscat(im->s, us); + + /* Final match */ + if (cm.match_is_final) { - wchar_t u = event.text.text[0]; - - im->s[0] = L'\0'; /* Zero-out output string */ - wcsncat(im->buf, &u, 1); /* Copy new character */ - - /* Translate the characters */ - im->redraw = 0; - while (1) - { - const wchar_t *us = charmap_search(&cm, im->buf); - -#ifdef IM_DEBUG - wprintf(L" [%8ls] [%8ls] %2d %2d\n", im->s, im->buf, wcslen(im->s), wcslen(im->buf)); -#endif - - /* Match was found? */ - if (us && wcslen(us)) - { -#ifdef IM_DEBUG - wprintf(L" 1\n"); -#endif - - wcscat(im->s, us); - - /* Final match */ - if (cm.match_is_final) - { - wcs_lshift(im->buf, cm.match_count); - cm.match_count = 0; - cm.match_is_final = 0; - } - /* May need to be overwritten next time */ - else - { - im->redraw += wcslen(us); - break; - } - } - /* No match, but more data is in the buffer */ - else if (wcslen(im->buf) > 0) - { - /* If the input character has no state, it's its own state */ - if (cm.match_count == 0) - { -#ifdef IM_DEBUG - wprintf(L" 2a\n"); -#endif - wcsncat(im->s, im->buf, 1); - wcs_lshift(im->buf, 1); - cm.match_is_final = 0; - } - /* If the matched characters didn't consume all, it's own state */ - else if ((size_t) cm.match_count != wcslen(im->buf)) - { -#ifdef IM_DEBUG - wprintf(L" 2b (%2d)\n", cm.match_count); -#endif - wcsncat(im->s, im->buf, 1); - wcs_lshift(im->buf, 1); - cm.match_is_final = 0; - } - /* Otherwise it's just a part of a future input */ - else - { -#ifdef IM_DEBUG - wprintf(L" 2c (%2d)\n", cm.match_count); -#endif - wcscat(im->s, im->buf); - cm.match_is_final = 0; - im->redraw += wcslen(im->buf); - break; - } - } - /* No match and no more data in the buffer */ - else - { -#ifdef IM_DEBUG - wprintf(L" 3\n"); -#endif - break; - } - - /* Is this the end? */ - if (cm.match_is_final) - break; - } + wcs_lshift(im->buf, cm.match_count); + cm.match_count = 0; + cm.match_is_final = 0; } + /* May need to be overwritten next time */ + else + { + im->redraw += wcslen(us); + break; + } + } + /* No match, but more data is in the buffer */ + else if (wcslen(im->buf) > 0) + { + /* If the input character has no state, it's its own state */ + if (cm.match_count == 0) + { +#ifdef IM_DEBUG + wprintf(L" 2a\n"); +#endif + wcsncat(im->s, im->buf, 1); + wcs_lshift(im->buf, 1); + cm.match_is_final = 0; + } + /* If the matched characters didn't consume all, it's own state */ + else if ((size_t) cm.match_count != wcslen(im->buf)) + { +#ifdef IM_DEBUG + wprintf(L" 2b (%2d)\n", cm.match_count); +#endif + wcsncat(im->s, im->buf, 1); + wcs_lshift(im->buf, 1); + cm.match_is_final = 0; + } + /* Otherwise it's just a part of a future input */ + else + { +#ifdef IM_DEBUG + wprintf(L" 2c (%2d)\n", cm.match_count); +#endif + wcscat(im->s, im->buf); + cm.match_is_final = 0; + im->redraw += wcslen(im->buf); + break; + } + } + /* No match and no more data in the buffer */ + else + { +#ifdef IM_DEBUG + wprintf(L" 3\n"); +#endif + break; + } + + /* Is this the end? */ + if (cm.match_is_final) + break; } + } } + } return im->redraw; } @@ -1613,14 +1628,15 @@ static int im_event_ko_isvowel(CHARMAP * cm, wchar_t c) /* Determine the starting state based on the charmap's active section */ section = cm->section; - if (!IN_RANGE(0, section, (int)ARRAYLEN(cm->sections))) + if (!IN_RANGE(0, section, (int) ARRAYLEN(cm->sections))) section = 0; start = &cm->sections[section]; - next = sm_search_shallow(start, (char)c); + next = sm_search_shallow(start, (char) c); unicode = next ? next->output : NULL; - return (unicode && wcslen(unicode) == 1 && 0x314F <= unicode[0] && unicode[0] <= 0x3163); + return (unicode && wcslen(unicode) == 1 && 0x314F <= unicode[0] + && unicode[0] <= 0x3163); } @@ -1641,259 +1657,262 @@ static int im_event_ko(IM_DATA * im, SDL_Event event) /* Handle event requests */ switch (im->request) + { + case 0: + break; + + case IM_REQ_FREE: /* Free allocated resources */ + charmap_free(&cm); + FALL_THROUGH; /* go onto full reset */ + + case IM_REQ_RESET_FULL: /* Full reset */ + cm.section = SEC_ENGLISH; + im->tip_text = im_tip_text[IM_TIP_ENGLISH]; + FALL_THROUGH; /* go onto soft reset */ + + case IM_REQ_RESET_SOFT: /* Soft reset */ + im->s[0] = L'\0'; + im->buf[0] = L'\0'; + im->redraw = 0; + cm.match_count = 0; + cm.match_is_final = 0; + cm.match_state = &cm.sections[cm.section]; + cm.match_state_prev = &cm.sections[cm.section]; + break; + + case IM_REQ_INIT: /* Initialization */ + charmap_init(&cm); + + if (charmap_load(&cm, lang_file)) { - case 0: - break; + fprintf(stderr, "Unable to load %s, defaulting to im_event_c\n", + lang_file); + im->lang = LANG_DEFAULT; + return im_event_c(im, event); + } - case IM_REQ_FREE: /* Free allocated resources */ - charmap_free(&cm); - FALL_THROUGH; /* go onto full reset */ - - case IM_REQ_RESET_FULL: /* Full reset */ - cm.section = SEC_ENGLISH; - im->tip_text = im_tip_text[IM_TIP_ENGLISH]; - FALL_THROUGH; /* go onto soft reset */ - - case IM_REQ_RESET_SOFT: /* Soft reset */ - im->s[0] = L'\0'; - im->buf[0] = L'\0'; - im->redraw = 0; - cm.match_count = 0; - cm.match_is_final = 0; - cm.match_state = &cm.sections[cm.section]; - cm.match_state_prev = &cm.sections[cm.section]; - break; - - case IM_REQ_INIT: /* Initialization */ - charmap_init(&cm); - - if (charmap_load(&cm, lang_file)) - { - fprintf(stderr, "Unable to load %s, defaulting to im_event_c\n", lang_file); - im->lang = LANG_DEFAULT; - return im_event_c(im, event); - } - - im_fullreset(im); + im_fullreset(im); #ifdef DEBUG - printf("IM: Loaded '%s'\n", lang_file); + printf("IM: Loaded '%s'\n", lang_file); #endif - break; - } + break; + } if (im->request != IM_REQ_TRANSLATE) return 0; /* Discard redraw characters, so they can be redrawn */ - if ((int)wcslen(im->s) < im->redraw) + if ((int) wcslen(im->s) < im->redraw) im->redraw = wcslen(im->s); wcs_lshift(im->s, (wcslen(im->s) - im->redraw)); /* Handle keys */ switch (ks.sym) + { + /* Keys to ignore */ + case SDLK_NUMLOCKCLEAR: + case SDLK_CAPSLOCK: + case SDLK_SCROLLLOCK: + case SDLK_LSHIFT: + case SDLK_RSHIFT: + case SDLK_LCTRL: + case SDLK_RCTRL: + case SDLK_LGUI: + case SDLK_RGUI: + case SDLK_MENU: + case SDLK_MODE: + case SDLK_APPLICATION: + break; + + /* Left-Alt & Right-Alt mapped to mode-switch */ + case SDLK_LALT: + case SDLK_RALT: + cm.section = ((cm.section + 1) % SEC_TOTAL); /* Change section */ + im_softreset(im); /* Soft reset */ + + /* Set tip text */ + switch (cm.section) { - /* Keys to ignore */ - case SDLK_NUMLOCKCLEAR: - case SDLK_CAPSLOCK: - case SDLK_SCROLLLOCK: - case SDLK_LSHIFT: - case SDLK_RSHIFT: - case SDLK_LCTRL: - case SDLK_RCTRL: - case SDLK_LGUI: - case SDLK_RGUI: - case SDLK_MENU: - case SDLK_MODE: - case SDLK_APPLICATION: + case SEC_ENGLISH: + im->tip_text = im_tip_text[IM_TIP_ENGLISH]; break; - - /* Left-Alt & Right-Alt mapped to mode-switch */ - case SDLK_LALT: - case SDLK_RALT: - cm.section = ((cm.section + 1) % SEC_TOTAL); /* Change section */ - im_softreset(im); /* Soft reset */ - - /* Set tip text */ - switch (cm.section) - { - case SEC_ENGLISH: - im->tip_text = im_tip_text[IM_TIP_ENGLISH]; - break; - case SEC_HANGUL: - im->tip_text = im_tip_text[IM_TIP_HANGUL]; - break; - } + case SEC_HANGUL: + im->tip_text = im_tip_text[IM_TIP_HANGUL]; break; - - /* Backspace removes only a single buffered character */ - case SDLK_BACKSPACE: - /* Delete one buffered character */ - if (wcslen(im->buf) > 0) - { - wcs_pull(im->buf, 1); - if (im->redraw > 0) - im->redraw--; - event.text.text[0] = L'\0'; - } - FALL_THROUGH; /* continue processing: */ - - /* Actual character processing */ - default: - if (event.type == SDL_TEXTINPUT || ks.sym == SDLK_BACKSPACE || ks.sym == SDLK_RETURN || ks.sym == SDLK_TAB) - { - /* English mode */ - if (cm.section == SEC_ENGLISH) - { - mbstowcs(im->s, event.text.text, 16); - im->buf[0] = L'\0'; - } - /* Hangul mode */ - else - { - wchar_t u = event.text.text[0]; - wchar_t *bp = im->buf; - - im->s[0] = L'\0'; /* Zero-out output string */ - wcsncat(bp, &u, 1); /* Copy new character */ - - /* Translate the characters */ - im->redraw = 0; - while (1) - { - const wchar_t *us = charmap_search(&cm, bp); - -#ifdef IM_DEBUG - wprintf(L" [%8ls] [%8ls] %2d %2d\n", im->s, im->buf, wcslen(im->s), wcslen(im->buf)); -#endif - - /* Match was found? */ - if (us && wcslen(us)) - { - /* Final match */ - if (cm.match_is_final) - { - /* Batchim may carry over to the next character */ - if (cm.match_state->flag == 'b') - { - wchar_t next_char = bp[cm.match_count]; - - /* If there is no more buffer, output it */ - if (cm.match_stats & MATCH_STAT_NOMOBUF) - { -#ifdef IM_DEBUG - wprintf(L" 1a\n"); -#endif - - wcscat(im->s, us); /* Output */ - im->redraw += wcslen(us); /* May need to re-eval next time */ - bp += cm.match_count; /* Keep buffer data for re-eval */ - cm.match_count = 0; - cm.match_is_final = 0; - } - /* If there is buffer data but it's not vowel, finalize it */ - else if (!im_event_ko_isvowel(&cm, next_char)) - { -#ifdef IM_DEBUG - wprintf(L" 1b\n"); -#endif - - wcscat(im->s, us); /* Output */ - wcs_lshift(bp, cm.match_count); - cm.match_count = 0; - cm.match_is_final = 0; - } - /* If there is buffer and it's vowel, re-eval */ - else - { -#ifdef IM_DEBUG - wprintf(L" 1c\n"); -#endif - - us = cm.match_state_prev->output; - wcscat(im->s, us); /* Output */ - cm.match_count--; /* Matched all but one */ - cm.match_is_final = 0; - wcs_lshift(bp, cm.match_count); - } - } - /* No batchim - this is final */ - else - { -#ifdef IM_DEBUG - wprintf(L" 1d\n"); -#endif - - wcscat(im->s, us); - wcs_lshift(bp, cm.match_count); - cm.match_count = 0; - cm.match_is_final = 0; - } - } - /* May need to be overwritten next time */ - else - { -#ifdef IM_DEBUG - wprintf(L" 1e\n"); -#endif - - wcscat(im->s, us); - im->redraw += wcslen(us); - break; - } - } - /* No match, but more data is in the buffer */ - else if (wcslen(bp) > 0) - { - /* If the input character has no state, it's its own state */ - if (cm.match_count == 0) - { -#ifdef IM_DEBUG - wprintf(L" 2a\n"); -#endif - wcsncat(im->s, bp, 1); - wcs_lshift(bp, 1); - cm.match_is_final = 0; - } - /* If the matched characters didn't consume all, it's own state */ - else if ((size_t) cm.match_count != wcslen(bp)) - { -#ifdef IM_DEBUG - wprintf(L" 2b (%2d)\n", cm.match_count); -#endif - wcsncat(im->s, bp, 1); - wcs_lshift(bp, 1); - cm.match_is_final = 0; - } - /* Otherwise it's just a part of a future input */ - else - { -#ifdef IM_DEBUG - wprintf(L" 2c (%2d)\n", cm.match_count); -#endif - wcscat(im->s, bp); - cm.match_is_final = 0; - im->redraw += wcslen(bp); - break; - } - } - /* No match and no more data in the buffer */ - else - { -#ifdef IM_DEBUG - wprintf(L" 3\n"); -#endif - break; - } - - /* Is this the end? */ - if (cm.match_is_final) - break; - } - } - } } + break; + + /* Backspace removes only a single buffered character */ + case SDLK_BACKSPACE: + /* Delete one buffered character */ + if (wcslen(im->buf) > 0) + { + wcs_pull(im->buf, 1); + if (im->redraw > 0) + im->redraw--; + event.text.text[0] = L'\0'; + } + FALL_THROUGH; /* continue processing: */ + + /* Actual character processing */ + default: + if (event.type == SDL_TEXTINPUT || ks.sym == SDLK_BACKSPACE + || ks.sym == SDLK_RETURN || ks.sym == SDLK_TAB) + { + /* English mode */ + if (cm.section == SEC_ENGLISH) + { + mbstowcs(im->s, event.text.text, 16); + im->buf[0] = L'\0'; + } + /* Hangul mode */ + else + { + wchar_t u = event.text.text[0]; + wchar_t *bp = im->buf; + + im->s[0] = L'\0'; /* Zero-out output string */ + wcsncat(bp, &u, 1); /* Copy new character */ + + /* Translate the characters */ + im->redraw = 0; + while (1) + { + const wchar_t *us = charmap_search(&cm, bp); + +#ifdef IM_DEBUG + wprintf(L" [%8ls] [%8ls] %2d %2d\n", im->s, im->buf, wcslen(im->s), + wcslen(im->buf)); +#endif + + /* Match was found? */ + if (us && wcslen(us)) + { + /* Final match */ + if (cm.match_is_final) + { + /* Batchim may carry over to the next character */ + if (cm.match_state->flag == 'b') + { + wchar_t next_char = bp[cm.match_count]; + + /* If there is no more buffer, output it */ + if (cm.match_stats & MATCH_STAT_NOMOBUF) + { +#ifdef IM_DEBUG + wprintf(L" 1a\n"); +#endif + + wcscat(im->s, us); /* Output */ + im->redraw += wcslen(us); /* May need to re-eval next time */ + bp += cm.match_count; /* Keep buffer data for re-eval */ + cm.match_count = 0; + cm.match_is_final = 0; + } + /* If there is buffer data but it's not vowel, finalize it */ + else if (!im_event_ko_isvowel(&cm, next_char)) + { +#ifdef IM_DEBUG + wprintf(L" 1b\n"); +#endif + + wcscat(im->s, us); /* Output */ + wcs_lshift(bp, cm.match_count); + cm.match_count = 0; + cm.match_is_final = 0; + } + /* If there is buffer and it's vowel, re-eval */ + else + { +#ifdef IM_DEBUG + wprintf(L" 1c\n"); +#endif + + us = cm.match_state_prev->output; + wcscat(im->s, us); /* Output */ + cm.match_count--; /* Matched all but one */ + cm.match_is_final = 0; + wcs_lshift(bp, cm.match_count); + } + } + /* No batchim - this is final */ + else + { +#ifdef IM_DEBUG + wprintf(L" 1d\n"); +#endif + + wcscat(im->s, us); + wcs_lshift(bp, cm.match_count); + cm.match_count = 0; + cm.match_is_final = 0; + } + } + /* May need to be overwritten next time */ + else + { +#ifdef IM_DEBUG + wprintf(L" 1e\n"); +#endif + + wcscat(im->s, us); + im->redraw += wcslen(us); + break; + } + } + /* No match, but more data is in the buffer */ + else if (wcslen(bp) > 0) + { + /* If the input character has no state, it's its own state */ + if (cm.match_count == 0) + { +#ifdef IM_DEBUG + wprintf(L" 2a\n"); +#endif + wcsncat(im->s, bp, 1); + wcs_lshift(bp, 1); + cm.match_is_final = 0; + } + /* If the matched characters didn't consume all, it's own state */ + else if ((size_t) cm.match_count != wcslen(bp)) + { +#ifdef IM_DEBUG + wprintf(L" 2b (%2d)\n", cm.match_count); +#endif + wcsncat(im->s, bp, 1); + wcs_lshift(bp, 1); + cm.match_is_final = 0; + } + /* Otherwise it's just a part of a future input */ + else + { +#ifdef IM_DEBUG + wprintf(L" 2c (%2d)\n", cm.match_count); +#endif + wcscat(im->s, bp); + cm.match_is_final = 0; + im->redraw += wcslen(bp); + break; + } + } + /* No match and no more data in the buffer */ + else + { +#ifdef IM_DEBUG + wprintf(L" 3\n"); +#endif + break; + } + + /* Is this the end? */ + if (cm.match_is_final) + break; + } + } + } + } return im->redraw; } @@ -1913,9 +1932,9 @@ void im_init(IM_DATA * im, int lang) { /* Free already allocated resources if initialized before */ if (im_initialized) - { - im_free(im); - } + { + im_free(im); + } /* Initialize */ memset(im, 0, sizeof(IM_DATA)); @@ -1923,15 +1942,15 @@ void im_init(IM_DATA * im, int lang) /* Setup static globals */ if (!im_initialized) - { - /* ADD NEW LANGUAGE SUPPORT HERE */ - im_event_fns[LANG_JA] = &im_event_ja; - im_event_fns[LANG_KO] = &im_event_ko; - im_event_fns[LANG_TH] = &im_event_th; - im_event_fns[LANG_ZH_TW] = &im_event_zh_tw; + { + /* ADD NEW LANGUAGE SUPPORT HERE */ + im_event_fns[LANG_JA] = &im_event_ja; + im_event_fns[LANG_KO] = &im_event_ko; + im_event_fns[LANG_TH] = &im_event_th; + im_event_fns[LANG_ZH_TW] = &im_event_zh_tw; - im_initialized = 1; - } + im_initialized = 1; + } #ifdef DEBUG assert(0 <= im->lang && im->lang < NUM_LANGS); diff --git a/src/ios_print.h b/src/ios_print.h index 340679cf4..277f94d4e 100644 --- a/src/ios_print.h +++ b/src/ios_print.h @@ -25,8 +25,8 @@ #include "SDL.h" -int DisplayPageSetup(const SDL_Surface* surface); -const char* SurfacePrint(const SDL_Surface* surface, int showDialog); +int DisplayPageSetup(const SDL_Surface * surface); +const char *SurfacePrint(const SDL_Surface * surface, int showDialog); #endif /* __IOS_PRINT_H__ */ diff --git a/src/onscreen_keyboard.c b/src/onscreen_keyboard.c index 98f023a2f..41207ddf5 100644 --- a/src/onscreen_keyboard.c +++ b/src/onscreen_keyboard.c @@ -59,7 +59,8 @@ static void draw_keyboard(on_screen_keyboard * keyboard); static osk_key *find_key(on_screen_keyboard * keyboard, int x, int y); static void set_key(osk_key * orig, osk_key * dest, int firsttime); static void load_keysymdefs(osk_layout * layout, char *keysymdefs_name); -static struct osk_layout *load_layout(on_screen_keyboard * keyboard, char *layout_name); +static struct osk_layout *load_layout(on_screen_keyboard * keyboard, + char *layout_name); #ifdef DEBUG_OSK_COMPOSEMAP static void print_composemap(osk_composenode * composemap, char *sp); @@ -79,22 +80,27 @@ static SDL_Surface *SDL_DisplayFormatAlpha(SDL_Surface * surface) return (tmp); } -struct osk_keyboard *osk_create(char * layout_name, SDL_Surface * canvas, - SDL_Surface * BLANK_button_up, SDL_Surface * BLANK_button_down, - SDL_Surface * BLANK_button_off, SDL_Surface * BLANK_button_nav, +struct osk_keyboard *osk_create(char *layout_name, SDL_Surface * canvas, + SDL_Surface * BLANK_button_up, + SDL_Surface * BLANK_button_down, + SDL_Surface * BLANK_button_off, + SDL_Surface * BLANK_button_nav, SDL_Surface * BLANK_button_hold, - SDL_Surface * BLANK_oskdel, SDL_Surface * BLANK_osktab, SDL_Surface * BLANK_oskenter, - SDL_Surface * BLANK_oskcapslock, SDL_Surface * BLANK_oskshift, + SDL_Surface * BLANK_oskdel, + SDL_Surface * BLANK_osktab, + SDL_Surface * BLANK_oskenter, + SDL_Surface * BLANK_oskcapslock, + SDL_Surface * BLANK_oskshift, int disable_change) { - SDL_Surface * surface; - SDL_Surface * button_up, * button_down; - SDL_Surface * button_off, * button_nav; - SDL_Surface * button_hold; - SDL_Surface * oskdel, * osktab, * oskenter; - SDL_Surface * oskcapslock, * oskshift; - osk_layout * layout; - on_screen_keyboard * keyboard; + SDL_Surface *surface; + SDL_Surface *button_up, *button_down; + SDL_Surface *button_off, *button_nav; + SDL_Surface *button_hold; + SDL_Surface *oskdel, *osktab, *oskenter; + SDL_Surface *oskcapslock, *oskshift; + osk_layout *layout; + on_screen_keyboard *keyboard; int layout_avail_width, layout_avail_height; keyboard = malloc(sizeof(on_screen_keyboard)); @@ -104,16 +110,17 @@ struct osk_keyboard *osk_create(char * layout_name, SDL_Surface * canvas, keyboard->disable_change = disable_change; layout = load_layout(keyboard, layout_name); if (!layout) + { + fprintf(stderr, "Error trying to load the required layout %s\n", + layout_name); + layout = load_layout(keyboard, strdup("default.layout")); + if (!layout) { - fprintf(stderr, "Error trying to load the required layout %s\n", layout_name); - layout = load_layout(keyboard, strdup("default.layout")); - if (!layout) - { - fprintf(stderr, "Error trying to load the default layout\n"); - return NULL; - } - fprintf(stderr, "Loaded the default layout instead.\n"); + fprintf(stderr, "Error trying to load the default layout\n"); + return NULL; } + fprintf(stderr, "Loaded the default layout instead.\n"); + } #ifdef OSK_DEBUG printf("w %i, h %i\n", layout->width, layout->height); @@ -122,75 +129,78 @@ struct osk_keyboard *osk_create(char * layout_name, SDL_Surface * canvas, layout_avail_width = (canvas->w * 0.9); layout_avail_height = (canvas->h * 0.5); - if (layout->width * BLANK_button_up->w >= layout_avail_width || /* Don't allow it to be > 90% of the width of the canvas */ - layout->height * BLANK_button_up->h >= layout_avail_height /* Don't allow it to be > 50% of the height of the canvas */) { - /* Full-size buttons too large, resize to fit */ - float max_w, max_h; - float scale_w, scale_h; + if (layout->width * BLANK_button_up->w >= layout_avail_width || /* Don't allow it to be > 90% of the width of the canvas */ + layout->height * BLANK_button_up->h >= layout_avail_height + /* Don't allow it to be > 50% of the height of the canvas */ ) + { + /* Full-size buttons too large, resize to fit */ + float max_w, max_h; + float scale_w, scale_h; #ifdef OSK_DEBUG - printf("%d x %d layout of %d x %d buttons won't fit within %d x %d pixel area...\n", - layout->width, layout->height, - BLANK_button_up->w, BLANK_button_up->h, - layout_avail_width, layout_avail_height); + printf + ("%d x %d layout of %d x %d buttons won't fit within %d x %d pixel area...\n", + layout->width, layout->height, BLANK_button_up->w, BLANK_button_up->h, + layout_avail_width, layout_avail_height); #endif - max_w = (float) layout_avail_width / (float) layout->width; - max_h = (float) layout_avail_height / (float) layout->height; + max_w = (float) layout_avail_width / (float) layout->width; + max_h = (float) layout_avail_height / (float) layout->height; #ifdef OSK_DEBUG - printf("...want (%d / %d) x (%d x %d) = %.2f x %.2f buttons...\n", - layout_avail_width, layout->width, - layout_avail_height, layout->height, - max_w, max_h); + printf("...want (%d / %d) x (%d x %d) = %.2f x %.2f buttons...\n", + layout_avail_width, layout->width, + layout_avail_height, layout->height, max_w, max_h); #endif - if (max_w > max_h) - max_w = max_h; - if (max_h > max_w) - max_h = max_w; + if (max_w > max_h) + max_w = max_h; + if (max_h > max_w) + max_h = max_w; - scale_w = (float) max_w / (float) BLANK_button_up->w; - scale_h = (float) max_h / (float) BLANK_button_up->h; + scale_w = (float) max_w / (float) BLANK_button_up->w; + scale_h = (float) max_h / (float) BLANK_button_up->h; #ifdef OSK_DEBUG - printf("...so scaling by w=%.2f & h=%.2f\n", - scale_w, scale_h); + printf("...so scaling by w=%.2f & h=%.2f\n", scale_w, scale_h); #endif - button_up = zoomSurface(BLANK_button_up, scale_w, scale_h, 1); - button_down = zoomSurface(BLANK_button_down, scale_w, scale_h, 1); - button_off = zoomSurface(BLANK_button_off, scale_w, scale_h, 1); - button_nav = zoomSurface(BLANK_button_nav, scale_w, scale_h, 1); - button_hold = zoomSurface(BLANK_button_hold, scale_w, scale_h, 1); - oskdel = zoomSurface(BLANK_oskdel, scale_w, scale_h, 1); - osktab = zoomSurface(BLANK_osktab, scale_w, scale_h, 1); - oskenter = zoomSurface(BLANK_oskenter, scale_w, scale_h, 1); - oskcapslock = zoomSurface(BLANK_oskcapslock, scale_w, scale_h, 1); - oskshift = zoomSurface(BLANK_oskshift, scale_w, scale_h, 1); - } else { - button_up = SDL_DisplayFormatAlpha(BLANK_button_up); - button_down = SDL_DisplayFormatAlpha(BLANK_button_down); - button_off = SDL_DisplayFormatAlpha(BLANK_button_off); - button_nav = SDL_DisplayFormatAlpha(BLANK_button_nav); - button_hold = SDL_DisplayFormatAlpha(BLANK_button_hold); - oskdel = SDL_DisplayFormatAlpha(BLANK_oskdel); - osktab = SDL_DisplayFormatAlpha(BLANK_osktab); - oskenter = SDL_DisplayFormatAlpha(BLANK_oskenter); - oskcapslock = SDL_DisplayFormatAlpha(BLANK_oskcapslock); - oskshift = SDL_DisplayFormatAlpha(BLANK_oskshift); + button_up = zoomSurface(BLANK_button_up, scale_w, scale_h, 1); + button_down = zoomSurface(BLANK_button_down, scale_w, scale_h, 1); + button_off = zoomSurface(BLANK_button_off, scale_w, scale_h, 1); + button_nav = zoomSurface(BLANK_button_nav, scale_w, scale_h, 1); + button_hold = zoomSurface(BLANK_button_hold, scale_w, scale_h, 1); + oskdel = zoomSurface(BLANK_oskdel, scale_w, scale_h, 1); + osktab = zoomSurface(BLANK_osktab, scale_w, scale_h, 1); + oskenter = zoomSurface(BLANK_oskenter, scale_w, scale_h, 1); + oskcapslock = zoomSurface(BLANK_oskcapslock, scale_w, scale_h, 1); + oskshift = zoomSurface(BLANK_oskshift, scale_w, scale_h, 1); + } + else + { + button_up = SDL_DisplayFormatAlpha(BLANK_button_up); + button_down = SDL_DisplayFormatAlpha(BLANK_button_down); + button_off = SDL_DisplayFormatAlpha(BLANK_button_off); + button_nav = SDL_DisplayFormatAlpha(BLANK_button_nav); + button_hold = SDL_DisplayFormatAlpha(BLANK_button_hold); + oskdel = SDL_DisplayFormatAlpha(BLANK_oskdel); + osktab = SDL_DisplayFormatAlpha(BLANK_osktab); + oskenter = SDL_DisplayFormatAlpha(BLANK_oskenter); + oskcapslock = SDL_DisplayFormatAlpha(BLANK_oskcapslock); + oskshift = SDL_DisplayFormatAlpha(BLANK_oskshift); } surface = SDL_CreateRGBSurface(canvas->flags, layout->width * button_up->w, layout->height * button_up->h, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, 0); + canvas->format->Rmask, canvas->format->Gmask, + canvas->format->Bmask, 0); if (!surface) - { - fprintf(stderr, "Error creating the onscreen keyboard surface\n"); - return NULL; - } + { + fprintf(stderr, "Error creating the onscreen keyboard surface\n"); + return NULL; + } // keyboard->name = layout_name; keyboard->canvas_ptr = canvas; keyboard->layout = layout; @@ -238,7 +248,8 @@ struct osk_keyboard *osk_create(char * layout_name, SDL_Surface * canvas, keyboard->BLANK_oskshift = BLANK_oskshift; SDL_FillRect(surface, NULL, - SDL_MapRGB(surface->format, keyboard->layout->bgcolor.r, keyboard->layout->bgcolor.g, + SDL_MapRGB(surface->format, keyboard->layout->bgcolor.r, + keyboard->layout->bgcolor.g, keyboard->layout->bgcolor.b)); keybd_prepare(keyboard); @@ -247,7 +258,8 @@ struct osk_keyboard *osk_create(char * layout_name, SDL_Surface * canvas, return keyboard; } -static struct osk_layout *load_layout(on_screen_keyboard * keyboard, char *layout_name) +static struct osk_layout *load_layout(on_screen_keyboard * keyboard, + char *layout_name) { FILE *fi; int hlayout_loaded; @@ -270,46 +282,47 @@ static struct osk_layout *load_layout(on_screen_keyboard * keyboard, char *layou layout->sizeofkeysymdefs = 0; layout->bgcolor = def_bgcolor; layout->fgcolor = def_fgcolor; - + hlayout_loaded = 0; #ifdef OSK_DEBUG printf("load_layout %s\n", layout_name); #endif filename = malloc(sizeof(char) * 255); if (layout_name != NULL) + { + keyboard->name = strdup(layout_name); + /* Try full path */ + fi = fopen(layout_name, "r"); + if (fi == NULL) { - keyboard->name = strdup(layout_name); - /* Try full path */ - fi = fopen(layout_name, "r"); - if (fi == NULL) - { - /* Try with DATA_PREFIX */ + /* Try with DATA_PREFIX */ - snprintf(filename, 255, "%sosk/%s", DATA_PREFIX, layout_name); - fi = fopen(filename, "r"); - if (fi == NULL) - { - fprintf(stderr, "Can't open either %s nor %s\n", layout_name, filename); - /* Fallback to default */ - snprintf(filename, 255, "%sosk/default.layout", DATA_PREFIX); - fi = fopen(filename, "r"); - keyboard->name = strdup("default.layout"); - } - } - } - else - { - snprintf(filename, 255, "%sosk/default.layout", DATA_PREFIX); + snprintf(filename, 255, "%sosk/%s", DATA_PREFIX, layout_name); fi = fopen(filename, "r"); - keyboard->name = strdup("default.layout"); + if (fi == NULL) + { + fprintf(stderr, "Can't open either %s nor %s\n", layout_name, + filename); + /* Fallback to default */ + snprintf(filename, 255, "%sosk/default.layout", DATA_PREFIX); + fi = fopen(filename, "r"); + keyboard->name = strdup("default.layout"); + } } + } + else + { + snprintf(filename, 255, "%sosk/default.layout", DATA_PREFIX); + fi = fopen(filename, "r"); + keyboard->name = strdup("default.layout"); + } free(filename); if (fi == NULL) - { - fprintf(stderr, "Can't load the on screen keyboard layout\n"); - return NULL; - } + { + fprintf(stderr, "Can't load the on screen keyboard layout\n"); + return NULL; + } line = malloc(sizeof(char) * 1024); @@ -317,52 +330,52 @@ static struct osk_layout *load_layout(on_screen_keyboard * keyboard, char *layou value = malloc(sizeof(char) * 255); while (!feof(fi)) + { + tmp_ptr = fgets(line, 1023, fi); + + if (is_blank_or_comment(line)) + continue; + + sscanf(line, "%s %s", key, value); + if (strcmp("layout", key) == 0 && !hlayout_loaded) { - tmp_ptr = fgets(line, 1023, fi); - - if (is_blank_or_comment(line)) - continue; - - sscanf(line, "%s %s", key, value); - if (strcmp("layout", key) == 0 && !hlayout_loaded) - { #ifdef OSK_DEBUG - printf("layout found: %s\n", value); + printf("layout found: %s\n", value); #endif - load_hlayout(layout, value); - hlayout_loaded = 1; - } - else if (strncmp("keymap", key, 6) == 0) - { -#ifdef OSK_DEBUG - printf("keymap found: %s\n", value); -#endif - load_keymap(layout, value); - } - else if (strncmp("composemap", key, 10) == 0) - { -#ifdef OSK_DEBUG - printf("composemap found: %s\n", value); -#endif - load_composemap(layout, value); - } - else if (strncmp("keysymdefs", key, 10) == 0) - { - load_keysymdefs(layout, value); - } - else if (strncmp("keyboardlist", key, 12) == 0) - { - strcpy(value, &line[13]); - keyboard->keyboard_list = strdup(value); - } - -#ifdef OSK_DEBUG - printf("key %s, value %s\n", key, value); -#endif - key[0] = '\0'; - value[0] = '\0'; + load_hlayout(layout, value); + hlayout_loaded = 1; } + else if (strncmp("keymap", key, 6) == 0) + { +#ifdef OSK_DEBUG + printf("keymap found: %s\n", value); +#endif + load_keymap(layout, value); + } + else if (strncmp("composemap", key, 10) == 0) + { +#ifdef OSK_DEBUG + printf("composemap found: %s\n", value); +#endif + load_composemap(layout, value); + } + else if (strncmp("keysymdefs", key, 10) == 0) + { + load_keysymdefs(layout, value); + } + else if (strncmp("keyboardlist", key, 12) == 0) + { + strcpy(value, &line[13]); + keyboard->keyboard_list = strdup(value); + } + +#ifdef OSK_DEBUG + printf("key %s, value %s\n", key, value); +#endif + key[0] = '\0'; + value[0] = '\0'; + } @@ -402,19 +415,20 @@ void load_hlayout(osk_layout * layout, char *hlayout_name) /* Try full path */ fi = fopen(hlayout_name, "r"); if (fi == NULL) - { - /* Try with DATA_PREFIX */ + { + /* Try with DATA_PREFIX */ - snprintf(filename, 255, "%sosk/%s", DATA_PREFIX, hlayout_name); - fi = fopen(filename, "r"); - if (fi == NULL) - { - fprintf(stderr, "Can't open either %s nor %s\n", hlayout_name, filename); - layout->keys = NULL; - free(filename); - return; - } + snprintf(filename, 255, "%sosk/%s", DATA_PREFIX, hlayout_name); + fi = fopen(filename, "r"); + if (fi == NULL) + { + fprintf(stderr, "Can't open either %s nor %s\n", hlayout_name, + filename); + layout->keys = NULL; + free(filename); + return; } + } free(filename); @@ -433,123 +447,127 @@ void load_hlayout(osk_layout * layout, char *hlayout_name) while (!feof(fi)) + { + if (width && height && !allocated) { - if (width && height && !allocated) - { - layout->keys = malloc(height * sizeof(osk_key *)); - layout->keys[0] = malloc(width * sizeof(osk_key)); + layout->keys = malloc(height * sizeof(osk_key *)); + layout->keys[0] = malloc(width * sizeof(osk_key)); - for (i = 0; i < width; i++) - { - layout->keys[0][i].width = 0; - layout->keys[0][i].plain_label = NULL; - layout->keys[line_number][i].top_label = NULL; - layout->keys[line_number][i].altgr_label = NULL; - layout->keys[line_number][i].shift_altgr_label = NULL; - } - layout->width = width; - layout->height = height; + for (i = 0; i < width; i++) + { + layout->keys[0][i].width = 0; + layout->keys[0][i].plain_label = NULL; + layout->keys[line_number][i].top_label = NULL; + layout->keys[line_number][i].altgr_label = NULL; + layout->keys[line_number][i].shift_altgr_label = NULL; + } + layout->width = width; + layout->height = height; #ifdef OSK_DEBUG - printf("w %i, h %i\n", layout->width, layout->height); + printf("w %i, h %i\n", layout->width, layout->height); #endif - allocated = 1; - } - - tmp_ptr = fgets(line, 1023, fi); - - if (is_blank_or_comment(line)) - continue; - - if (strncmp(line, "WIDTH", 5) == 0) - sscanf(line, "%s %i", key, &width); - - else if (strncmp(line, "HEIGHT", 5) == 0) - sscanf(line, "%s %i", key, &height); - - else if (strncmp(line, "FONTPATH", 8) == 0) - { -#ifdef OSK_DEBUG - printf("linefont %s\n", line); -#endif - sscanf(line, "%s %s", key, fontpath); - if (!is_blank_or_comment(fontpath)) - have_fontpath = 1; - } - else if (strncmp(line, "FGCOLOR", 5) == 0) - { -#ifdef OSK_DEBUG - printf("linefont %s\n", line); -#endif - sscanf(line, "%s %i %i %i", key, &r, &g, &b); - if (r > 0 && r < 256 && g > 0 && g < 256 && b > 0 && b < 256) - { - layout->fgcolor.r = r; - layout->fgcolor.g = g; - layout->fgcolor.b = b; - r = g = b = 256; - } - } - else if (strncmp(line, "BGCOLOR", 5) == 0) - { -#ifdef OSK_DEBUG - printf("linefont %s\n", line); -#endif - sscanf(line, "%s %i %i %i", key, &r, &g, &b); - if (r > 0 && r < 256 && g > 0 && g < 256 && b > 0 && b < 256) - { - layout->bgcolor.r = r; - layout->bgcolor.g = g; - layout->bgcolor.b = b; - r = g = b = 256; - } - } - else if (strncmp(line, "NEWLINE", 7) == 0) - { - line_number++; - key_number = 0; - layout->keys[line_number] = malloc(width * sizeof(osk_key)); - for (i = 0; i < width; i++) - { - layout->keys[line_number][i].width = 0; - layout->keys[line_number][i].plain_label = NULL; - layout->keys[line_number][i].top_label = NULL; - layout->keys[line_number][i].altgr_label = NULL; - layout->keys[line_number][i].shift_altgr_label = NULL; - } - } - else if (width && height && allocated && strncmp(line, "KEY ", 4) == 0 && key_number < width) - { - plain_label = malloc(sizeof(char) * 64); - top_label = malloc(sizeof(char) * 64); - altgr_label = malloc(sizeof(char) * 64); - shift_altgr_label = malloc(sizeof(char) * 64); - - sscanf(line, - "%s %i %i.%i %s %s %s %s %i", - key, - &keycode, - &key_width, &key_width_decimal, plain_label, top_label, altgr_label, shift_altgr_label, &shiftcaps); - layout->keys[line_number][key_number].keycode = keycode; - layout->keys[line_number][key_number].width = (float)0.1 *key_width_decimal + key_width; - - layout->keys[line_number][key_number].plain_label = plain_label; - layout->keys[line_number][key_number].top_label = top_label; - layout->keys[line_number][key_number].altgr_label = altgr_label; - layout->keys[line_number][key_number].shift_altgr_label = shift_altgr_label; - layout->keys[line_number][key_number].shiftcaps = shiftcaps; - layout->keys[line_number][key_number].stick = 0; - key_number++; - } + allocated = 1; } + tmp_ptr = fgets(line, 1023, fi); + + if (is_blank_or_comment(line)) + continue; + + if (strncmp(line, "WIDTH", 5) == 0) + sscanf(line, "%s %i", key, &width); + + else if (strncmp(line, "HEIGHT", 5) == 0) + sscanf(line, "%s %i", key, &height); + + else if (strncmp(line, "FONTPATH", 8) == 0) + { +#ifdef OSK_DEBUG + printf("linefont %s\n", line); +#endif + sscanf(line, "%s %s", key, fontpath); + if (!is_blank_or_comment(fontpath)) + have_fontpath = 1; + } + else if (strncmp(line, "FGCOLOR", 5) == 0) + { +#ifdef OSK_DEBUG + printf("linefont %s\n", line); +#endif + sscanf(line, "%s %i %i %i", key, &r, &g, &b); + if (r > 0 && r < 256 && g > 0 && g < 256 && b > 0 && b < 256) + { + layout->fgcolor.r = r; + layout->fgcolor.g = g; + layout->fgcolor.b = b; + r = g = b = 256; + } + } + else if (strncmp(line, "BGCOLOR", 5) == 0) + { +#ifdef OSK_DEBUG + printf("linefont %s\n", line); +#endif + sscanf(line, "%s %i %i %i", key, &r, &g, &b); + if (r > 0 && r < 256 && g > 0 && g < 256 && b > 0 && b < 256) + { + layout->bgcolor.r = r; + layout->bgcolor.g = g; + layout->bgcolor.b = b; + r = g = b = 256; + } + } + else if (strncmp(line, "NEWLINE", 7) == 0) + { + line_number++; + key_number = 0; + layout->keys[line_number] = malloc(width * sizeof(osk_key)); + for (i = 0; i < width; i++) + { + layout->keys[line_number][i].width = 0; + layout->keys[line_number][i].plain_label = NULL; + layout->keys[line_number][i].top_label = NULL; + layout->keys[line_number][i].altgr_label = NULL; + layout->keys[line_number][i].shift_altgr_label = NULL; + } + } + else if (width && height && allocated && strncmp(line, "KEY ", 4) == 0 + && key_number < width) + { + plain_label = malloc(sizeof(char) * 64); + top_label = malloc(sizeof(char) * 64); + altgr_label = malloc(sizeof(char) * 64); + shift_altgr_label = malloc(sizeof(char) * 64); + + sscanf(line, + "%s %i %i.%i %s %s %s %s %i", + key, + &keycode, + &key_width, &key_width_decimal, plain_label, top_label, + altgr_label, shift_altgr_label, &shiftcaps); + layout->keys[line_number][key_number].keycode = keycode; + layout->keys[line_number][key_number].width = + (float) 0.1 *key_width_decimal + key_width; + + layout->keys[line_number][key_number].plain_label = plain_label; + layout->keys[line_number][key_number].top_label = top_label; + layout->keys[line_number][key_number].altgr_label = altgr_label; + layout->keys[line_number][key_number].shift_altgr_label = + shift_altgr_label; + layout->keys[line_number][key_number].shiftcaps = shiftcaps; + layout->keys[line_number][key_number].stick = 0; + key_number++; + } + } + if (have_fontpath) layout->fontpath = fontpath; else - { - free(fontpath); - layout->fontpath = NULL; - } + { + free(fontpath); + layout->fontpath = NULL; + } free(line); free(key); @@ -587,19 +605,19 @@ void load_keymap(osk_layout * layout, char *keymap_name) /* Try full path */ fi = fopen(keymap_name, "r"); if (fi == NULL) - { - /* Try with DATA_PREFIX */ + { + /* Try with DATA_PREFIX */ - snprintf(filename, 255, "%sosk/%s", DATA_PREFIX, keymap_name); - fi = fopen(filename, "r"); - if (fi == NULL) - { - fprintf(stderr, "Can't open either %s nor %s\n", keymap_name, filename); - layout->keys = NULL; - free(filename); - return; - } + snprintf(filename, 255, "%sosk/%s", DATA_PREFIX, keymap_name); + fi = fopen(filename, "r"); + if (fi == NULL) + { + fprintf(stderr, "Can't open either %s nor %s\n", keymap_name, filename); + layout->keys = NULL; + free(filename); + return; } + } free(filename); @@ -607,53 +625,55 @@ void load_keymap(osk_layout * layout, char *keymap_name) layout->keymap = malloc(256 * sizeof(osk_keymap)); for (i = 0; i < 256; i++) - { - layout->keymap[i].plain = NULL; - layout->keymap[i].caps = NULL; - layout->keymap[i].altgr = NULL; - layout->keymap[i].shiftaltgr = NULL; - } + { + layout->keymap[i].plain = NULL; + layout->keymap[i].caps = NULL; + layout->keymap[i].altgr = NULL; + layout->keymap[i].shiftaltgr = NULL; + } while (!feof(fi)) + { + tmp_ptr = fgets(line, 1023, fi); + + if (is_blank_or_comment(line)) + continue; + + ksname1 = malloc(sizeof(char) * 64); + ksname2 = malloc(sizeof(char) * 64); + ksname3 = malloc(sizeof(char) * 64); + ksname4 = malloc(sizeof(char) * 64); + ksname1[0] = '\0'; + ksname2[0] = '\0'; + ksname3[0] = '\0'; + ksname4[0] = '\0'; + + /* FIXME: Why is the us-intl keymap duplicating the two first entries of every keycode? */ + /* And why is the arabic keymap using the 5th and 6th entries as plain/shifted keys? */ + readed = + sscanf(line, "keycode %i = %s %s %s %s", &keycode, ksname1, ksname2, + ksname3, ksname4); + + if (readed == 5 && keycode > 8 && keycode < 256) { - tmp_ptr = fgets(line, 1023, fi); - - if (is_blank_or_comment(line)) - continue; - - ksname1 = malloc(sizeof(char) * 64); - ksname2 = malloc(sizeof(char) * 64); - ksname3 = malloc(sizeof(char) * 64); - ksname4 = malloc(sizeof(char) * 64); - ksname1[0] = '\0'; - ksname2[0] = '\0'; - ksname3[0] = '\0'; - ksname4[0] = '\0'; - - /* FIXME: Why is the us-intl keymap duplicating the two first entries of every keycode? */ - /* And why is the arabic keymap using the 5th and 6th entries as plain/shifted keys? */ - readed = sscanf(line, "keycode %i = %s %s %s %s", &keycode, ksname1, ksname2, ksname3, ksname4); - - if (readed == 5 && keycode > 8 && keycode < 256) - { - layout->keymap[keycode].plain = ksname1; - layout->keymap[keycode].caps = ksname2; - layout->keymap[keycode].altgr = ksname3; - layout->keymap[keycode].shiftaltgr = ksname4; - } - else - { - free(ksname1); - free(ksname2); - free(ksname3); - free(ksname4); - layout->keymap[keycode].plain = NULL; - layout->keymap[keycode].caps = NULL; - layout->keymap[keycode].altgr = NULL; - layout->keymap[keycode].shiftaltgr = NULL; - } + layout->keymap[keycode].plain = ksname1; + layout->keymap[keycode].caps = ksname2; + layout->keymap[keycode].altgr = ksname3; + layout->keymap[keycode].shiftaltgr = ksname4; } + else + { + free(ksname1); + free(ksname2); + free(ksname3); + free(ksname4); + layout->keymap[keycode].plain = NULL; + layout->keymap[keycode].caps = NULL; + layout->keymap[keycode].altgr = NULL; + layout->keymap[keycode].shiftaltgr = NULL; + } + } free(line); fclose(fi); @@ -673,7 +693,8 @@ void load_keymap(osk_layout * layout, char *keymap_name) } /* Scans a line of keysyms and result and classifies them. */ -static void gettokens(char *line, char *delim, char **pointer, osk_composenode * composenode, osk_layout * layout) +static void gettokens(char *line, char *delim, char **pointer, + osk_composenode * composenode, osk_layout * layout) { int i; char *tok; @@ -688,73 +709,76 @@ static void gettokens(char *line, char *delim, char **pointer, osk_composenode * return; if (tok[0] == ':') /* End of precompose keysyms, next will be the result in UTF-8. */ + { + free(tok); + tok = strdup(strtok_r(line, ": \"\t", pointer)); + + mbstowcs(wtok, tok, 255); + + result = wcsdup(wtok); + /* printf("->%ls<-\n", wtok); */ + free(wtok); + free(tok); + composenode->result = result; + return; + } + else + { + if (composenode->size == 0) { - free(tok); - tok = strdup(strtok_r(line, ": \"\t", pointer)); + composenode->size = 1; + auxnode = malloc(sizeof(osk_composenode)); + composenode->childs = malloc(sizeof(osk_composenode *)); + composenode->childs[0] = auxnode; + mbstowcs(wtok, tok, 254); /* <<< CRASH */ + composenode->childs[0]->keysym = wcsdup(wtok); + composenode->childs[0]->result = NULL; + composenode->childs[0]->size = 0; - mbstowcs(wtok, tok, 255); + /* printf("size %d, keysym %ls => ", composenode->size, composenode->childs[0]->keysym); */ - result = wcsdup(wtok); - /* printf("->%ls<-\n", wtok); */ + gettokens(NULL, delim, pointer, composenode->childs[0], layout); free(wtok); free(tok); - composenode->result = result; return; } - else + else { - if (composenode->size == 0) + for (i = 0; i < composenode->size; i++) + { + mbstowcs(wtok, tok, 255); + if (wcscmp(composenode->childs[i]->keysym, wtok) == 0) { - composenode->size = 1; - auxnode = malloc(sizeof(osk_composenode)); - composenode->childs = malloc(sizeof(osk_composenode *)); - composenode->childs[0] = auxnode; - mbstowcs(wtok, tok, 254); /* <<< CRASH */ - composenode->childs[0]->keysym = wcsdup(wtok); - composenode->childs[0]->result = NULL; - composenode->childs[0]->size = 0; - /* printf("size %d, keysym %ls => ", composenode->size, composenode->childs[0]->keysym); */ + /* printf("Size %d, keysym %ls =>", composenode->size, composenode->childs[i]->keysym); */ - gettokens(NULL, delim, pointer, composenode->childs[0], layout); - free(wtok); + gettokens(NULL, delim, pointer, composenode->childs[i], layout); free(tok); + free(wtok); return; } - else - { - for (i = 0; i < composenode->size; i++) - { - mbstowcs(wtok, tok, 255); - if (wcscmp(composenode->childs[i]->keysym, wtok) == 0) - { - - /* printf("Size %d, keysym %ls =>", composenode->size, composenode->childs[i]->keysym); */ - - gettokens(NULL, delim, pointer, composenode->childs[i], layout); - free(tok); - free(wtok); - return; - } - } - } - - composenode->size = composenode->size + 1; - composenode->childs = realloc(composenode->childs, composenode->size * sizeof(osk_composenode *)); - - mbstowcs(wtok, tok, 255); - auxnode = malloc(sizeof(osk_composenode)); - composenode->childs[composenode->size - 1] = auxnode; //malloc(sizeof(osk_composenode)); - composenode->childs[composenode->size - 1]->keysym = wtok; - composenode->childs[composenode->size - 1]->result = NULL; - composenode->childs[composenode->size - 1]->size = 0; - - /* printf("size %d, keysym %ls =>", composenode->size, composenode->childs[composenode->size - 1]->keysym); */ - - gettokens(NULL, delim, pointer, composenode->childs[composenode->size - 1], layout); - free(tok); - return; + } } + + composenode->size = composenode->size + 1; + composenode->childs = + realloc(composenode->childs, + composenode->size * sizeof(osk_composenode *)); + + mbstowcs(wtok, tok, 255); + auxnode = malloc(sizeof(osk_composenode)); + composenode->childs[composenode->size - 1] = auxnode; //malloc(sizeof(osk_composenode)); + composenode->childs[composenode->size - 1]->keysym = wtok; + composenode->childs[composenode->size - 1]->result = NULL; + composenode->childs[composenode->size - 1]->size = 0; + + /* printf("size %d, keysym %ls =>", composenode->size, composenode->childs[composenode->size - 1]->keysym); */ + + gettokens(NULL, delim, pointer, + composenode->childs[composenode->size - 1], layout); + free(tok); + return; + } } @@ -775,19 +799,20 @@ static void load_composemap(osk_layout * layout, char *composemap_name) /* Try full path */ fi = fopen(composemap_name, "r"); if (fi == NULL) - { - /* Try with DATA_PREFIX */ + { + /* Try with DATA_PREFIX */ - snprintf(filename, 255, "%sosk/%s", DATA_PREFIX, composemap_name); - fi = fopen(filename, "r"); - if (fi == NULL) - { - fprintf(stderr, "Can't open either %s nor %s\n", composemap_name, filename); - layout->keys = NULL; - free(filename); - return; - } + snprintf(filename, 255, "%sosk/%s", DATA_PREFIX, composemap_name); + fi = fopen(filename, "r"); + if (fi == NULL) + { + fprintf(stderr, "Can't open either %s nor %s\n", composemap_name, + filename); + layout->keys = NULL; + free(filename); + return; } + } free(filename); @@ -799,14 +824,14 @@ static void load_composemap(osk_layout * layout, char *composemap_name) line = malloc(1024 * sizeof(char)); while (!feof(fi)) - { - tmp_ptr = fgets(line, 1023, fi); + { + tmp_ptr = fgets(line, 1023, fi); - if (is_blank_or_comment(line)) - continue; + if (is_blank_or_comment(line)) + continue; - gettokens(line, (char *)">< \t", pointer, layout->composemap, layout); - } + gettokens(line, (char *) ">< \t", pointer, layout->composemap, layout); + } fclose(fi); free(line); @@ -829,29 +854,29 @@ static void print_composemap(osk_composenode * composemap, char *sp) printf("%d ==> ", composemap->size); #endif if (composemap->size == 0) - { + { #ifdef OSK_DEBUG - printf("result %ls\n", composemap->result); + printf("result %ls\n", composemap->result); #endif - return; - } + return; + } if (sp) - { - sprintf(space, "%s\t", sp); - } + { + sprintf(space, "%s\t", sp); + } else - { - sprintf(space, " "); - } + { + sprintf(space, " "); + } #ifdef OSK_DEBUG printf("%s", space); #endif for (i = 0; i < composemap->size; i++) - { - print_composemap(composemap->childs[i], space); - // free(space); - } + { + print_composemap(composemap->childs[i], space); + // free(space); + } /* for (i = 0; i < composemap->size; i++) */ /* { */ /* printf("aaa %ls, ", composemap->keysym); */ @@ -883,19 +908,20 @@ static void load_keysymdefs(osk_layout * layout, char *keysymdefs_name) /* Try full path */ fi = fopen(keysymdefs_name, "r"); if (fi == NULL) - { - /* Try with DATA_PREFIX */ + { + /* Try with DATA_PREFIX */ - snprintf(filename, 255, "%sosk/%s", DATA_PREFIX, keysymdefs_name); - fi = fopen(filename, "r"); - if (fi == NULL) - { - fprintf(stderr, "Can't open either %s nor %s\n", keysymdefs_name, filename); - layout->keysymdefs = NULL; - free(filename); - return; - } + snprintf(filename, 255, "%sosk/%s", DATA_PREFIX, keysymdefs_name); + fi = fopen(filename, "r"); + if (fi == NULL) + { + fprintf(stderr, "Can't open either %s nor %s\n", keysymdefs_name, + filename); + layout->keysymdefs = NULL; + free(filename); + return; } + } free(filename); @@ -905,21 +931,23 @@ static void load_keysymdefs(osk_layout * layout, char *keysymdefs_name) line = malloc(1024 * sizeof(wchar_t)); while (!feof(fi)) - { - tmp_ptr = fgets(line, 1023, fi); - if (strncmp("#define XK_", line, 11) != 0) - continue; + { + tmp_ptr = fgets(line, 1023, fi); + if (strncmp("#define XK_", line, 11) != 0) + continue; - layout->sizeofkeysymdefs = i; - layout->keysymdefs = realloc(layout->keysymdefs, sizeof(keysymdefs) * (i + 1)); + layout->sizeofkeysymdefs = i; + layout->keysymdefs = + realloc(layout->keysymdefs, sizeof(keysymdefs) * (i + 1)); - /* Some keysyms doesn't correspond to any unicode value, ej. BackSpace */ - layout->keysymdefs[i].unicode = 0; - layout->keysymdefs[i].mnemo = malloc(sizeof(char) * 128); - sscanf(line, "#define XK_%s %x /* U+%x", - layout->keysymdefs[i].mnemo, &layout->keysymdefs[i].keysym, &layout->keysymdefs[i].unicode); - i++; - } + /* Some keysyms doesn't correspond to any unicode value, ej. BackSpace */ + layout->keysymdefs[i].unicode = 0; + layout->keysymdefs[i].mnemo = malloc(sizeof(char) * 128); + sscanf(line, "#define XK_%s %x /* U+%x", + layout->keysymdefs[i].mnemo, &layout->keysymdefs[i].keysym, + &layout->keysymdefs[i].unicode); + i++; + } fclose(fi); free(line); @@ -943,10 +971,10 @@ static int mnemo2keysym(char *mnemo, on_screen_keyboard * keyboard) unsigned int i; for (i = 0; i < keyboard->layout->sizeofkeysymdefs; i++) - { - if (strcmp(mnemo, keyboard->layout->keysymdefs[i].mnemo) == 0) - return (keyboard->layout->keysymdefs[i].keysym); - } + { + if (strcmp(mnemo, keyboard->layout->keysymdefs[i].mnemo) == 0) + return (keyboard->layout->keysymdefs[i].keysym); + } i = 0; /* Perhaps the mnemo is in UXXXX format? */ @@ -974,7 +1002,8 @@ static int keysym2unicode(int keysym, on_screen_keyboard * keyboard) * This software is in the public domain. Share and enjoy! */ /* first check for Latin-1 characters (1:1 mapping) */ - if ((keysym >= 0x0020 && keysym <= 0x007e) || (keysym >= 0x00a0 && keysym <= 0x00ff)) + if ((keysym >= 0x0020 && keysym <= 0x007e) + || (keysym >= 0x00a0 && keysym <= 0x00ff)) return keysym; /* also check for directly encoded 24-bit UCS characters */ @@ -992,65 +1021,67 @@ static int keysym2unicode(int keysym, on_screen_keyboard * keyboard) /* Searches in the tree for composing stuff */ -static void get_composed_keysym(on_screen_keyboard * keyboard, osk_composenode * composenode, wchar_t * keysym) +static void get_composed_keysym(on_screen_keyboard * keyboard, + osk_composenode * composenode, + wchar_t *keysym) { int i; /* If there is not a compose table return the keysym */ if (!composenode) - { - if (keyboard->composed) - free(keyboard->composed); - keyboard->composed = wcsdup(keysym); - keyboard->composed_type = 0; - return; - } + { + if (keyboard->composed) + free(keyboard->composed); + keyboard->composed = wcsdup(keysym); + keyboard->composed_type = 0; + return; + } /* If there is a compose table, lookup for matches */ for (i = 0; i < composenode->size; i++) + { + /* If matches, set either the result or the next node */ + if (wcscmp(composenode->childs[i]->keysym, keysym) == 0) { - /* If matches, set either the result or the next node */ - if (wcscmp(composenode->childs[i]->keysym, keysym) == 0) - { - if (composenode->childs[i]->result) - { - if (keyboard->composed) - free(keyboard->composed); - keyboard->composed = wcsdup(composenode->childs[i]->result); - keyboard->composing = keyboard->layout->composemap; - /* The result in the Compose files from xorg is yet in unicode */ - keyboard->composed_type = 1; - return; - } - else - { - if (keyboard->composed) - free(keyboard->composed); - keyboard->composed = NULL; - keyboard->composing = composenode->childs[i]; - return; - } - } + if (composenode->childs[i]->result) + { + if (keyboard->composed) + free(keyboard->composed); + keyboard->composed = wcsdup(composenode->childs[i]->result); + keyboard->composing = keyboard->layout->composemap; + /* The result in the Compose files from xorg is yet in unicode */ + keyboard->composed_type = 1; + return; + } + else + { + if (keyboard->composed) + free(keyboard->composed); + keyboard->composed = NULL; + keyboard->composing = composenode->childs[i]; + return; + } } + } /* No matches found, if we were in the middle of a sequence, reset the compose stuff, if we were in the beginning node, set the keysym */ if (keyboard->layout->composemap == composenode) - { - if (keyboard->composed) - free(keyboard->composed); - keyboard->composed = wcsdup(keysym); - keyboard->composed_type = 0; - } + { + if (keyboard->composed) + free(keyboard->composed); + keyboard->composed = wcsdup(keysym); + keyboard->composed_type = 0; + } else /* reset */ - { - keyboard->composing = keyboard->layout->composemap; - if (keyboard->composed) - free(keyboard->composed); - keyboard->composed = NULL; - keyboard->composed_type = 0; - } + { + keyboard->composing = keyboard->layout->composemap; + if (keyboard->composed) + free(keyboard->composed); + keyboard->composed = NULL; + keyboard->composed_type = 0; + } } @@ -1063,14 +1094,14 @@ static int is_blank_or_comment(char *line) if (strlen(line) == 0) return 0; while (line[i] != '\n') - { - if (line[i] == '#') - return 1; - else if (line[i] == ' ' || line[i] == '\t') - i++; - else - return 0; - } + { + if (line[i] == '#') + return 1; + else if (line[i] == ' ' || line[i] == '\t') + i++; + else + return 0; + } return 1; } @@ -1101,60 +1132,66 @@ static void keybd_prepare(on_screen_keyboard * keyboard) { char *fontname; int font_height; - + /* Pick a height (e.g., 16pt for small (24x24), 32pt for large (48x48) buttons) */ font_height = ((keyboard->button_up->h * 2) / 3); - + fontname = malloc(sizeof(char) * 255); if (keyboard->osk_fonty == NULL) + { + + if (keyboard->layout->fontpath) { - - if (keyboard->layout->fontpath) - { - /* First try if it is an absolute path */ - keyboard->osk_fonty = TTF_OpenFont(keyboard->layout->fontpath, font_height); - if (keyboard->osk_fonty == NULL) - { - /* Now trying if it is relative to DATA_PREFIX/fonts/ */ - snprintf(fontname, 255, "%s/fonts/%s", DATA_PREFIX, keyboard->layout->fontpath); - - keyboard->osk_fonty = TTF_OpenFont(fontname, font_height); - if (keyboard->osk_fonty == NULL) - { - /* Perhaps it is relative to DATA_PREFIX only? */ - snprintf(fontname, 255, "%s/%s", DATA_PREFIX, keyboard->layout->fontpath); - keyboard->osk_fonty = TTF_OpenFont(fontname, font_height); - if (keyboard->osk_fonty == NULL) - { - /* Or to DATA_PREFIX/fonts/locale/ ? */ - snprintf(fontname, 255, "%s/fonts/locale/%s", DATA_PREFIX, keyboard->layout->fontpath); - keyboard->osk_fonty = TTF_OpenFont(fontname, font_height); - } - } - } - } - + /* First try if it is an absolute path */ + keyboard->osk_fonty = + TTF_OpenFont(keyboard->layout->fontpath, font_height); if (keyboard->osk_fonty == NULL) + { + /* Now trying if it is relative to DATA_PREFIX/fonts/ */ + snprintf(fontname, 255, "%s/fonts/%s", DATA_PREFIX, + keyboard->layout->fontpath); + + keyboard->osk_fonty = TTF_OpenFont(fontname, font_height); + if (keyboard->osk_fonty == NULL) { - /* Going with the default */ - sprintf(fontname, "%s/fonts/FreeSansBold.ttf", DATA_PREFIX); + /* Perhaps it is relative to DATA_PREFIX only? */ + snprintf(fontname, 255, "%s/%s", DATA_PREFIX, + keyboard->layout->fontpath); keyboard->osk_fonty = TTF_OpenFont(fontname, font_height); + if (keyboard->osk_fonty == NULL) + { + /* Or to DATA_PREFIX/fonts/locale/ ? */ + snprintf(fontname, 255, "%s/fonts/locale/%s", DATA_PREFIX, + keyboard->layout->fontpath); + keyboard->osk_fonty = TTF_OpenFont(fontname, font_height); + } } - - if (keyboard->osk_fonty == NULL) - { - fprintf(stderr, "\nError: Can't open the font!\n" - "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", SDL_GetError()); - free(fontname); - exit(1); - } - - free(fontname); + } } + + if (keyboard->osk_fonty == NULL) + { + /* Going with the default */ + sprintf(fontname, "%s/fonts/FreeSansBold.ttf", DATA_PREFIX); + keyboard->osk_fonty = TTF_OpenFont(fontname, font_height); + } + + if (keyboard->osk_fonty == NULL) + { + fprintf(stderr, "\nError: Can't open the font!\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", SDL_GetError()); + free(fontname); + exit(1); + } + + free(fontname); + } } -static void apply_surface(int x, int y, SDL_Surface * source, SDL_Surface * destination, SDL_Rect * clip) +static void apply_surface(int x, int y, SDL_Surface * source, + SDL_Surface * destination, SDL_Rect * clip) { SDL_Rect offset; @@ -1206,42 +1243,43 @@ static SDL_Surface *stretch_surface(SDL_Surface * orig, int width) width, orig->h, orig->format->BitsPerPixel, - orig->format->Rmask, orig->format->Gmask, orig->format->Bmask, 0); + orig->format->Rmask, orig->format->Gmask, + orig->format->Bmask, 0); SDL_BlitSurface(orig, NULL, dest, NULL); rect.y = 0; if (width > orig->w) - { - rect.x = width - orig->w; - rect.h = orig->h; - rect.w = orig->w; - SDL_BlitSurface(orig, NULL, dest, &rect); + { + rect.x = width - orig->w; + rect.h = orig->h; + rect.w = orig->w; + SDL_BlitSurface(orig, NULL, dest, &rect); - rect.w = 1; - for (i = orig->w / 2; i < width - orig->w / 2; i++) - { - rect.x = i; - SDL_BlitSurface(orig, &orig_rect, dest, &rect); - } + rect.w = 1; + for (i = orig->w / 2; i < width - orig->w / 2; i++) + { + rect.x = i; + SDL_BlitSurface(orig, &orig_rect, dest, &rect); } + } else if (width < orig->w) + { + rect.y = 0; + rect.w = 1; + rect.h = dest->h; + + orig_rect.y = 0; + orig_rect.w = 1; + orig_rect.h = orig->h; + + for (i = 0; i <= width / 2; i++) { - rect.y = 0; - rect.w = 1; - rect.h = dest->h; - - orig_rect.y = 0; - orig_rect.w = 1; - orig_rect.h = orig->h; - - for (i = 0; i <= width / 2; i++) - { - rect.x = dest->w - i; - orig_rect.x = orig->w - i; - SDL_BlitSurface(orig, &orig_rect, dest, &rect); - } + rect.x = dest->w - i; + orig_rect.x = orig->w - i; + SDL_BlitSurface(orig, &orig_rect, dest, &rect); } + } return dest; } @@ -1259,23 +1297,23 @@ static void draw_keyboard(on_screen_keyboard * keyboard) accumulated_height = 0; for (j = 0; j < keyboard->layout->height; j++) + { + accumulated_width = 0; + for (i = 0; i < keyboard->layout->width; i++) { - accumulated_width = 0; - for (i = 0; i < keyboard->layout->width; i++) - { - if (keyboard->layout->keys[j][i].width) - { + if (keyboard->layout->keys[j][i].width) + { - keyboard->layout->keys[j][i].row = j; - keyboard->layout->keys[j][i].x = accumulated_width; - keyboard->layout->keys[j][i].y = accumulated_height; + keyboard->layout->keys[j][i].row = j; + keyboard->layout->keys[j][i].x = accumulated_width; + keyboard->layout->keys[j][i].y = accumulated_height; - draw_key(keyboard->layout->keys[j][i], keyboard, 0); - } - accumulated_width += (keyboard->layout->keys[j][i].width * key_width); - } - accumulated_height += key_height; + draw_key(keyboard->layout->keys[j][i], keyboard, 0); + } + accumulated_width += (keyboard->layout->keys[j][i].width * key_width); } + accumulated_height += key_height; + } /* draw_key(keyboard->keymodifiers.shift, keyboard, 0); */ /* draw_key(keyboard->keymodifiers.altgr, keyboard, 0); */ @@ -1296,28 +1334,40 @@ static void draw_key(osk_key key, on_screen_keyboard * keyboard, int hot) snprintf(text, 6, "%s", key.plain_label); if (strncmp("NULL", text, 4) != 0 && key.keycode != 0) + { + if (hot) + skey = + stretch_surface(keyboard->button_down, + key.width * keyboard->button_down->w); + + else if (key.stick) + skey = + stretch_surface(keyboard->button_hold, + key.width * keyboard->button_hold->w); + + else { - if (hot) - skey = stretch_surface(keyboard->button_down, key.width * keyboard->button_down->w); - - else if (key.stick) - skey = stretch_surface(keyboard->button_hold, key.width * keyboard->button_hold->w); - + if (key.keycode == 1 || key.keycode == 2) + { + if (keyboard->disable_change) + skey = + stretch_surface(keyboard->button_off, + key.width * keyboard->button_off->w); + else + skey = + stretch_surface(keyboard->button_nav, + key.width * keyboard->button_nav->w); + } else - { - if (key.keycode == 1 || key.keycode == 2) - { - if (keyboard->disable_change) - skey = stretch_surface(keyboard->button_off, key.width * keyboard->button_off->w); - else - skey = stretch_surface(keyboard->button_nav, key.width * keyboard->button_nav->w); - } - else - skey = stretch_surface(keyboard->button_up, key.width * keyboard->button_up->w); - } + skey = + stretch_surface(keyboard->button_up, + key.width * keyboard->button_up->w); } + } else - skey = stretch_surface(keyboard->button_off, key.width * keyboard->button_off->w); + skey = + stretch_surface(keyboard->button_off, + key.width * keyboard->button_off->w); apply_surface(key.x, key.y, skey, keyboard->surface, NULL); @@ -1345,88 +1395,92 @@ static void label_key(osk_key key, on_screen_keyboard * keyboard) text = strdup(key.plain_label); else if (modstate == KMOD_SHIFT) - { - text = strdup(key.top_label); - } + { + text = strdup(key.top_label); + } else if (modstate == KMOD_RALT) - { - text = strdup(key.altgr_label); - } + { + text = strdup(key.altgr_label); + } else if (modstate == KMOD_CAPS) - { - if (key.shiftcaps == 1) - text = strdup(key.top_label); + { + if (key.shiftcaps == 1) + text = strdup(key.top_label); - else - text = strdup(key.plain_label); - } + else + text = strdup(key.plain_label); + } /* Now the combined ones */ else if (modstate & KMOD_RALT && modstate & KMOD_SHIFT) - { - if (modstate & KMOD_CAPS) - { - if (key.shiftcaps) - text = strdup(key.altgr_label); - else - text = strdup(key.shift_altgr_label); - } - else - { - text = strdup(key.shift_altgr_label); - } - } - - else if (modstate & KMOD_RALT && modstate & KMOD_CAPS && !(modstate & KMOD_SHIFT)) + { + if (modstate & KMOD_CAPS) { if (key.shiftcaps) - text = strdup(key.shift_altgr_label); - else text = strdup(key.altgr_label); + else + text = strdup(key.shift_altgr_label); } + else + { + text = strdup(key.shift_altgr_label); + } + } + + else if (modstate & KMOD_RALT && modstate & KMOD_CAPS + && !(modstate & KMOD_SHIFT)) + { + if (key.shiftcaps) + text = strdup(key.shift_altgr_label); + else + text = strdup(key.altgr_label); + } else if (modstate & KMOD_SHIFT && modstate & KMOD_CAPS) - { - if (key.shiftcaps == 1) - text = strdup(key.plain_label); - else - text = strdup(key.top_label); - } + { + if (key.shiftcaps == 1) + text = strdup(key.plain_label); + else + text = strdup(key.top_label); + } if (strncmp("DELETE", text, 6) == 0) - { - apply_surface(key.x, key.y, keyboard->oskdel, keyboard->surface, NULL); - } + { + apply_surface(key.x, key.y, keyboard->oskdel, keyboard->surface, NULL); + } else if (strncmp("TAB", text, 3) == 0) - { - apply_surface(key.x, key.y, keyboard->osktab, keyboard->surface, NULL); - } + { + apply_surface(key.x, key.y, keyboard->osktab, keyboard->surface, NULL); + } else if (strncmp("ENTER", text, 5) == 0) - { - apply_surface(key.x, key.y, keyboard->oskenter, keyboard->surface, NULL); - } + { + apply_surface(key.x, key.y, keyboard->oskenter, keyboard->surface, NULL); + } else if (strncmp("CAPSLOCK", text, 8) == 0) - { - apply_surface(key.x, key.y, keyboard->oskcapslock, keyboard->surface, NULL); - } + { + apply_surface(key.x, key.y, keyboard->oskcapslock, keyboard->surface, + NULL); + } else if (strncmp("SHIFT", text, 5) == 0) - { - apply_surface(key.x, key.y, keyboard->oskshift, keyboard->surface, NULL); - } + { + apply_surface(key.x, key.y, keyboard->oskshift, keyboard->surface, NULL); + } else if (strncmp("SPACE", text, 5) != 0 && strncmp("NULL", text, 4) != 0) - { - messager = TTF_RenderUTF8_Blended(keyboard->osk_fonty, text, keyboard->layout->fgcolor); + { + messager = + TTF_RenderUTF8_Blended(keyboard->osk_fonty, text, + keyboard->layout->fgcolor); - apply_surface(key.x + 5, key.y, messager, keyboard->surface, NULL); - SDL_FreeSurface(messager); - } + apply_surface(key.x + 5, key.y, messager, keyboard->surface, NULL); + SDL_FreeSurface(messager); + } free(text); } @@ -1438,16 +1492,18 @@ static osk_key *find_key(on_screen_keyboard * keyboard, int x, int y) key = NULL; for (j = 0; j < keyboard->layout->height; j++) - { - if (keyboard->layout->keys[j][0].y < y && keyboard->layout->keys[j][0].y + keyboard->button_up->h > y) - for (i = 0; i < keyboard->layout->width; i++) - if (keyboard->layout->keys[j][i].x < x && - keyboard->layout->keys[j][i].x + keyboard->layout->keys[j][i].width * keyboard->button_up->w > x) - { - key = &keyboard->layout->keys[j][i]; - return key; - } - } + { + if (keyboard->layout->keys[j][0].y < y + && keyboard->layout->keys[j][0].y + keyboard->button_up->h > y) + for (i = 0; i < keyboard->layout->width; i++) + if (keyboard->layout->keys[j][i].x < x && + keyboard->layout->keys[j][i].x + + keyboard->layout->keys[j][i].width * keyboard->button_up->w > x) + { + key = &keyboard->layout->keys[j][i]; + return key; + } + } return NULL; } @@ -1457,45 +1513,45 @@ static osk_key *find_key(on_screen_keyboard * keyboard, int x, int y) static void set_key(osk_key * orig, osk_key * dest, int firsttime) { if (orig == NULL) - { - dest->keycode = 0; - dest->row = 0; - dest->x = 0; - dest->y = 0; - dest->width = 0; - if (!firsttime && dest->plain_label != NULL) - free(dest->plain_label); - dest->plain_label = NULL; - if (!firsttime && dest->top_label != NULL) - free(dest->top_label); - dest->top_label = NULL; - if (!firsttime && dest->altgr_label != NULL) - free(dest->altgr_label); - dest->altgr_label = NULL; - dest->shiftcaps = 0; - } + { + dest->keycode = 0; + dest->row = 0; + dest->x = 0; + dest->y = 0; + dest->width = 0; + if (!firsttime && dest->plain_label != NULL) + free(dest->plain_label); + dest->plain_label = NULL; + if (!firsttime && dest->top_label != NULL) + free(dest->top_label); + dest->top_label = NULL; + if (!firsttime && dest->altgr_label != NULL) + free(dest->altgr_label); + dest->altgr_label = NULL; + dest->shiftcaps = 0; + } else - { - dest->keycode = orig->keycode; - dest->row = orig->row; - dest->x = orig->x; - dest->y = orig->y; - dest->width = orig->width; + { + dest->keycode = orig->keycode; + dest->row = orig->row; + dest->x = orig->x; + dest->y = orig->y; + dest->width = orig->width; - if (dest->plain_label != NULL) - free(dest->plain_label); - dest->plain_label = strdup(orig->plain_label); + if (dest->plain_label != NULL) + free(dest->plain_label); + dest->plain_label = strdup(orig->plain_label); - if (dest->top_label != NULL) - free(dest->top_label); - dest->top_label = strdup(orig->top_label); + if (dest->top_label != NULL) + free(dest->top_label); + dest->top_label = strdup(orig->top_label); - if (dest->altgr_label != NULL) - free(dest->altgr_label); - dest->altgr_label = strdup(orig->altgr_label); + if (dest->altgr_label != NULL) + free(dest->altgr_label); + dest->altgr_label = strdup(orig->altgr_label); - dest->shiftcaps = orig->shiftcaps; - } + dest->shiftcaps = orig->shiftcaps; + } } static char *find_keysym(osk_key key, on_screen_keyboard * keyboard) @@ -1517,60 +1573,62 @@ static char *find_keysym(osk_key key, on_screen_keyboard * keyboard) keysym = keysyms.plain; else if (modstate == KMOD_SHIFT) - { - keysym = keysyms.caps; - } + { + keysym = keysyms.caps; + } else if (modstate == KMOD_RALT) - { - keysym = keysyms.altgr; - } + { + keysym = keysyms.altgr; + } else if (modstate == KMOD_CAPS) - { - if (key.shiftcaps == 1) - keysym = keysyms.caps; - else - keysym = keysyms.plain; - } + { + if (key.shiftcaps == 1) + keysym = keysyms.caps; + else + keysym = keysyms.plain; + } /* Now the combined ones */ else if (modstate & KMOD_RALT && modstate & KMOD_SHIFT) - { - if (modstate & KMOD_CAPS) - { - if (key.shiftcaps) - keysym = keysyms.altgr; - else - keysym = keysyms.shiftaltgr; - } - else - { - keysym = keysyms.shiftaltgr; - } - } - - else if (modstate & KMOD_RALT && modstate & KMOD_CAPS && !(modstate & KMOD_SHIFT)) + { + if (modstate & KMOD_CAPS) { if (key.shiftcaps) - keysym = keysyms.shiftaltgr; - else keysym = keysyms.altgr; + else + keysym = keysyms.shiftaltgr; } + else + { + keysym = keysyms.shiftaltgr; + } + } + + else if (modstate & KMOD_RALT && modstate & KMOD_CAPS + && !(modstate & KMOD_SHIFT)) + { + if (key.shiftcaps) + keysym = keysyms.shiftaltgr; + else + keysym = keysyms.altgr; + } else if (modstate & KMOD_SHIFT && modstate & KMOD_CAPS) - { - if (key.shiftcaps == 1) - keysym = keysyms.plain; - else - keysym = keysyms.caps; - } + { + if (key.shiftcaps == 1) + keysym = keysyms.plain; + else + keysym = keysyms.caps; + } return (keysym); } /* We lose the SDL ModState by leaving and entering the tuxpaint window, so using a custom state */ -static int handle_keymods(char *keysym, osk_key * key, on_screen_keyboard * keyboard) +static int handle_keymods(char *keysym, osk_key * key, + on_screen_keyboard * keyboard) { SDL_Keymod mod; SDL_Event ev; @@ -1578,74 +1636,75 @@ static int handle_keymods(char *keysym, osk_key * key, on_screen_keyboard * keyb mod = keyboard->modifiers; if (strncmp("Shift", keysym, 5) == 0) + { + if (mod & KMOD_SHIFT) { - if (mod & KMOD_SHIFT) - { - keyboard->modifiers = mod & 0xFFF0; - key->stick = 0; - keyboard->kmdf.shift->stick = 0; - } - else - { - keyboard->modifiers = mod | KMOD_SHIFT; - key->stick = 1; - keyboard->kmdf.shift = key; - } - return 1; + keyboard->modifiers = mod & 0xFFF0; + key->stick = 0; + keyboard->kmdf.shift->stick = 0; } + else + { + keyboard->modifiers = mod | KMOD_SHIFT; + key->stick = 1; + keyboard->kmdf.shift = key; + } + return 1; + } else if (strncmp("Alt_L", keysym, 5) == 0) - { - ev.key.keysym.sym = SDLK_LALT; - ev.text.text[0] = 0; // FIXME is 0 the right value here? - ev.type = SDL_KEYDOWN; - SDL_PushEvent(&ev); - ev.type = SDL_KEYUP; - SDL_PushEvent(&ev); + { + ev.key.keysym.sym = SDLK_LALT; + ev.text.text[0] = 0; // FIXME is 0 the right value here? + ev.type = SDL_KEYDOWN; + SDL_PushEvent(&ev); + ev.type = SDL_KEYUP; + SDL_PushEvent(&ev); - return 1; - } + return 1; + } /* Seems ISO_Level3_Shift and ISO_Next_Group are used too for right Alt */ else if (strncmp("ISO_Level3_Shift", keysym, 16) == 0 || - strncmp("ISO_Next_Group", keysym, 14) == 0 || strncmp("ALT_R", keysym, 5) == 0) + strncmp("ISO_Next_Group", keysym, 14) == 0 + || strncmp("ALT_R", keysym, 5) == 0) + { + if (mod & KMOD_RALT) { - if (mod & KMOD_RALT) - { - keyboard->modifiers = mod & 0xF0FF; - keyboard->kmdf.altgr->stick = 0; - } - else - { - keyboard->modifiers = mod | KMOD_RALT; - key->stick = 1; - keyboard->kmdf.altgr = key; - - return 1; - } - return 0; + keyboard->modifiers = mod & 0xF0FF; + keyboard->kmdf.altgr->stick = 0; } - - else if (strncmp("Caps_Lock", keysym, 9) == 0) + else { - if (mod & KMOD_CAPS) - { - keyboard->modifiers = mod & 0x0FFF; - key->stick = 0; - } - else - { - keyboard->modifiers = mod | KMOD_CAPS; - key->stick = 1; - } - + keyboard->modifiers = mod | KMOD_RALT; + key->stick = 1; + keyboard->kmdf.altgr = key; return 1; } + return 0; + } + + else if (strncmp("Caps_Lock", keysym, 9) == 0) + { + if (mod & KMOD_CAPS) + { + keyboard->modifiers = mod & 0x0FFF; + key->stick = 0; + } + else + { + keyboard->modifiers = mod | KMOD_CAPS; + key->stick = 1; + } + + + return 1; + } if (mod & KMOD_CAPS) - { - keyboard->modifiers = KMOD_CAPS; - } + { + keyboard->modifiers = KMOD_CAPS; + } else keyboard->modifiers = KMOD_NONE; @@ -1675,25 +1734,25 @@ static void set_dead_sticks(osk_key * key, on_screen_keyboard * keyboard) static void clear_dead_sticks(on_screen_keyboard * keyboard) { if (keyboard->kmdf.dead) - { - keyboard->kmdf.dead->stick = 0; - keyboard->kmdf.dead = NULL; - } + { + keyboard->kmdf.dead->stick = 0; + keyboard->kmdf.dead = NULL; + } if (keyboard->kmdf.dead2) - { - keyboard->kmdf.dead2->stick = 0; - keyboard->kmdf.dead2 = NULL; - } + { + keyboard->kmdf.dead2->stick = 0; + keyboard->kmdf.dead2 = NULL; + } if (keyboard->kmdf.dead3) - { - keyboard->kmdf.dead3->stick = 0; - keyboard->kmdf.dead3 = NULL; - } + { + keyboard->kmdf.dead3->stick = 0; + keyboard->kmdf.dead3 = NULL; + } if (keyboard->kmdf.dead4) - { - keyboard->kmdf.dead4->stick = 0; - keyboard->kmdf.dead4 = NULL; - } + { + keyboard->kmdf.dead4->stick = 0; + keyboard->kmdf.dead4 = NULL; + } } struct osk_keyboard *osk_clicked(on_screen_keyboard * keyboard, int x, int y) @@ -1718,216 +1777,216 @@ struct osk_keyboard *osk_clicked(on_screen_keyboard * keyboard, int x, int y) key = find_key(keyboard, x, y); if (key) + { + /* First the reserved keycodes */ + /* Select next or previous keyboard */ + if (key->keycode == 1 || key->keycode == 2) { - /* First the reserved keycodes */ - /* Select next or previous keyboard */ - if (key->keycode == 1 || key->keycode == 2) + if (keyboard->disable_change) + { + // free(key); + return (keyboard); + } + + aux_list = strdup(keyboard->keyboard_list); + aux_list_ptr = aux_list; + +#ifdef OSK_DEBUG + printf("auxlist: %s\n", aux_list); + printf("kn %s\n", keyboard->name); +#endif + + if (key->keycode == 1) + { + for (i = 0;; i++, aux_list = NULL) { - if (keyboard->disable_change) - { - // free(key); - return (keyboard); - } + name = strtok(aux_list, " \n\r\t"); - aux_list = strdup(keyboard->keyboard_list); - aux_list_ptr = aux_list; + if (i == 0) + aux_name = name; -#ifdef OSK_DEBUG - printf("auxlist: %s\n", aux_list); - printf("kn %s\n", keyboard->name); -#endif - - if (key->keycode == 1) - { - for (i = 0;; i++, aux_list = NULL) - { - name = strtok(aux_list, " \n\r\t"); - - if (i == 0) - aux_name = name; - - if (strcmp(name, keyboard->name) == 0) - { - name = strtok(NULL, " \n\r\t"); - if (name == NULL) - name = aux_name; - break; - } - } - } - else - { - aux_name = NULL; - for (i = 0;; i++, aux_list = NULL) - { - name = strtok(aux_list, " \n\r\t"); - - if (name == NULL) - { - name = aux_name; - break; - } - - if (strstr(name, keyboard->name)) - { - name = aux_name; - if (name != NULL) - break; - } - - aux_name = name; - } - } - - - new_keyboard = - osk_create(name, keyboard->canvas_ptr, - keyboard->BLANK_button_up, keyboard->BLANK_button_down, - keyboard->BLANK_button_off, keyboard->BLANK_button_nav, - keyboard->BLANK_button_hold, - keyboard->BLANK_oskdel, keyboard->BLANK_osktab, - keyboard->BLANK_oskenter, keyboard->BLANK_oskcapslock, - keyboard->BLANK_oskshift, - keyboard->disable_change); - - free(aux_list_ptr); - - if (new_keyboard == NULL) - { - // free(key); - return (keyboard); /* Don't break here, at least the old keyboard should work */ - } - else - { - free(new_keyboard->keyboard_list); - new_keyboard->keyboard_list = strdup(keyboard->keyboard_list); - // free(key); - osk_free(keyboard); - return (new_keyboard); - } - } - - - keysym = find_keysym(*key, keyboard); - if (!keysym) - { - return (keyboard); - } - - draw_key(*key, keyboard, 1); - - if (handle_keymods(keysym, key, keyboard)) - { - return (keyboard); /* no more processing is needed */ - } - - wkeysym = malloc(sizeof(wchar_t) * (strlen(keysym) + 1)); - - mbsrtowcs(wkeysym, (const char **)&keysym, strlen(keysym) + 1, NULL); - -#ifdef OSK_DEBUG - printf("wkeysym %ls %i\n\n", wkeysym, (int)wcslen(wkeysym)); -#endif - - - get_composed_keysym(keyboard, keyboard->composing, wkeysym); - - if (keyboard->composed) - { - keyboard->last_key_pressed = key; - set_key(NULL, &keyboard->keymodifiers.compose, 0); - ks = keyboard->composed; - -#ifdef OSK_DEBUG - printf("keysym found %ls\n", ks); -#endif - - mnemo = malloc(sizeof(char) * 32); - snprintf(mnemo, 31, "%ls", ks); - - if (wcsncmp(L"Return", ks, 6) == 0) - { - event.key.keysym.sym = SDLK_RETURN; - event.text.text[0] = '\r'; - event.text.text[1] = '\0'; - } - else if (wcsncmp(L"Tab", ks, 3) == 0 || wcsncmp(L"ISO_Left_Tab", ks, 12) == 0) - { - event.key.keysym.sym = SDLK_TAB; - event.text.text[0] = '\t'; - event.text.text[1] = '\0'; - } - else if (wcsncmp(L"BackSpace", ks, 9) == 0) - { - event.key.keysym.sym = SDLK_BACKSPACE; - event.text.text[0] = '\b'; - event.text.text[1] = '\0'; - } - else if (wcsncmp(L"NoSymbol", ks, 8) == 0) - { - return (keyboard); - } - else - { - int len; - - if (keyboard->composed_type == 1) - { -#ifdef OSK_DEBUG - printf("Composed_type = 1: \"%ls\"\n", keyboard->composed); -#endif - len = wcstombs(event.text.text, keyboard->composed, 16); - } - else - { - int iwc; - wchar_t buf[2]; - - iwc = keysym2unicode(mnemo2keysym(mnemo, keyboard), keyboard); - buf[0] = (wchar_t) iwc; - buf[1] = L'\0'; - -#ifdef OSK_DEBUG - printf("iwc as buf = \"%ls\"\n", buf); -#endif - len = wcstombs(event.text.text, buf, 16); - } - -#ifdef OSK_DEBUG - printf("len = %d\n", len); - printf("event.text.text = \"%s\"\n", event.text.text); -#endif - } - - clear_dead_sticks(keyboard); - event.type = SDL_TEXTINPUT; - SDL_PushEvent(&event); - free(mnemo); + if (strcmp(name, keyboard->name) == 0) + { + name = strtok(NULL, " \n\r\t"); + if (name == NULL) + name = aux_name; + break; + } } + } else + { + aux_name = NULL; + for (i = 0;; i++, aux_list = NULL) { - if (keyboard->composing == keyboard->layout->composemap) - { -#ifdef OSK_DEBUG - printf("compose sequence resetted\n"); -#endif - set_key(NULL, &keyboard->keymodifiers.compose, 0); - keyboard->last_key_pressed = key; - clear_dead_sticks(keyboard); - } - else - { - set_key(key, &keyboard->keymodifiers.compose, 0); -#ifdef OSK_DEBUG - printf("still composing\n"); -#endif - set_dead_sticks(key, keyboard); - /* Fixme: Would be nice if we can highlight next available-to-compose keys, but how? */ - } + name = strtok(aux_list, " \n\r\t"); + + if (name == NULL) + { + name = aux_name; + break; + } + + if (strstr(name, keyboard->name)) + { + name = aux_name; + if (name != NULL) + break; + } + + aux_name = name; } - free(wkeysym); + } + + + new_keyboard = + osk_create(name, keyboard->canvas_ptr, + keyboard->BLANK_button_up, keyboard->BLANK_button_down, + keyboard->BLANK_button_off, keyboard->BLANK_button_nav, + keyboard->BLANK_button_hold, + keyboard->BLANK_oskdel, keyboard->BLANK_osktab, + keyboard->BLANK_oskenter, keyboard->BLANK_oskcapslock, + keyboard->BLANK_oskshift, keyboard->disable_change); + + free(aux_list_ptr); + + if (new_keyboard == NULL) + { + // free(key); + return (keyboard); /* Don't break here, at least the old keyboard should work */ + } + else + { + free(new_keyboard->keyboard_list); + new_keyboard->keyboard_list = strdup(keyboard->keyboard_list); + // free(key); + osk_free(keyboard); + return (new_keyboard); + } } + + keysym = find_keysym(*key, keyboard); + if (!keysym) + { + return (keyboard); + } + + draw_key(*key, keyboard, 1); + + if (handle_keymods(keysym, key, keyboard)) + { + return (keyboard); /* no more processing is needed */ + } + + wkeysym = malloc(sizeof(wchar_t) * (strlen(keysym) + 1)); + + mbsrtowcs(wkeysym, (const char **) &keysym, strlen(keysym) + 1, NULL); + +#ifdef OSK_DEBUG + printf("wkeysym %ls %i\n\n", wkeysym, (int) wcslen(wkeysym)); +#endif + + + get_composed_keysym(keyboard, keyboard->composing, wkeysym); + + if (keyboard->composed) + { + keyboard->last_key_pressed = key; + set_key(NULL, &keyboard->keymodifiers.compose, 0); + ks = keyboard->composed; + +#ifdef OSK_DEBUG + printf("keysym found %ls\n", ks); +#endif + + mnemo = malloc(sizeof(char) * 32); + snprintf(mnemo, 31, "%ls", ks); + + if (wcsncmp(L"Return", ks, 6) == 0) + { + event.key.keysym.sym = SDLK_RETURN; + event.text.text[0] = '\r'; + event.text.text[1] = '\0'; + } + else if (wcsncmp(L"Tab", ks, 3) == 0 + || wcsncmp(L"ISO_Left_Tab", ks, 12) == 0) + { + event.key.keysym.sym = SDLK_TAB; + event.text.text[0] = '\t'; + event.text.text[1] = '\0'; + } + else if (wcsncmp(L"BackSpace", ks, 9) == 0) + { + event.key.keysym.sym = SDLK_BACKSPACE; + event.text.text[0] = '\b'; + event.text.text[1] = '\0'; + } + else if (wcsncmp(L"NoSymbol", ks, 8) == 0) + { + return (keyboard); + } + else + { + int len; + + if (keyboard->composed_type == 1) + { +#ifdef OSK_DEBUG + printf("Composed_type = 1: \"%ls\"\n", keyboard->composed); +#endif + len = wcstombs(event.text.text, keyboard->composed, 16); + } + else + { + int iwc; + wchar_t buf[2]; + + iwc = keysym2unicode(mnemo2keysym(mnemo, keyboard), keyboard); + buf[0] = (wchar_t) iwc; + buf[1] = L'\0'; + +#ifdef OSK_DEBUG + printf("iwc as buf = \"%ls\"\n", buf); +#endif + len = wcstombs(event.text.text, buf, 16); + } + +#ifdef OSK_DEBUG + printf("len = %d\n", len); + printf("event.text.text = \"%s\"\n", event.text.text); +#endif + } + + clear_dead_sticks(keyboard); + event.type = SDL_TEXTINPUT; + SDL_PushEvent(&event); + free(mnemo); + } + else + { + if (keyboard->composing == keyboard->layout->composemap) + { +#ifdef OSK_DEBUG + printf("compose sequence resetted\n"); +#endif + set_key(NULL, &keyboard->keymodifiers.compose, 0); + keyboard->last_key_pressed = key; + clear_dead_sticks(keyboard); + } + else + { + set_key(key, &keyboard->keymodifiers.compose, 0); +#ifdef OSK_DEBUG + printf("still composing\n"); +#endif + set_dead_sticks(key, keyboard); + /* Fixme: Would be nice if we can highlight next available-to-compose keys, but how? */ + } + } + free(wkeysym); + } + return (keyboard); } @@ -1937,10 +1996,10 @@ void osk_released(on_screen_keyboard * keyboard) key = keyboard->last_key_pressed; if (key) - { - draw_key(*key, keyboard, 0); - // free(key); - } + { + draw_key(*key, keyboard, 0); + // free(key); + } keyboard->last_key_pressed = NULL; draw_keyboard(keyboard); } @@ -1951,16 +2010,16 @@ static void free_keymap(osk_keymap * keymap) int i; for (i = 0; i < 256; i++) - { - if (keymap[i].plain) - free(keymap[i].plain); - if (keymap[i].caps) - free(keymap[i].caps); - if (keymap[i].altgr) - free(keymap[i].altgr); - if (keymap[i].shiftaltgr) - free(keymap[i].shiftaltgr); - } + { + if (keymap[i].plain) + free(keymap[i].plain); + if (keymap[i].caps) + free(keymap[i].caps); + if (keymap[i].altgr) + free(keymap[i].altgr); + if (keymap[i].shiftaltgr) + free(keymap[i].shiftaltgr); + } free(keymap); } @@ -1969,10 +2028,10 @@ static void free_composemap(osk_composenode * composenode) int i; for (i = 0; i < composenode->size; i++) - { - free_composemap(composenode->childs[i]); - free(composenode->childs[i]); - } + { + free_composemap(composenode->childs[i]); + free(composenode->childs[i]); + } if (composenode->result) free(composenode->result); else @@ -1995,21 +2054,21 @@ static void free_keys(osk_layout * layout) int i, j; for (j = 0; j < layout->height; j++) + { + for (i = 0; i < layout->width; i++) { - for (i = 0; i < layout->width; i++) - { - if (layout->keys[j][i].plain_label) - free(layout->keys[j][i].plain_label); - if (layout->keys[j][i].top_label) - free(layout->keys[j][i].top_label); - if (layout->keys[j][i].altgr_label) - free(layout->keys[j][i].altgr_label); - if (layout->keys[j][i].shift_altgr_label) - free(layout->keys[j][i].shift_altgr_label); + if (layout->keys[j][i].plain_label) + free(layout->keys[j][i].plain_label); + if (layout->keys[j][i].top_label) + free(layout->keys[j][i].top_label); + if (layout->keys[j][i].altgr_label) + free(layout->keys[j][i].altgr_label); + if (layout->keys[j][i].shift_altgr_label) + free(layout->keys[j][i].shift_altgr_label); - } - free(layout->keys[j]); } + free(layout->keys[j]); + } free(layout->keys); } diff --git a/src/onscreen_keyboard.h b/src/onscreen_keyboard.h index b40f2b425..1db4f99f0 100644 --- a/src/onscreen_keyboard.h +++ b/src/onscreen_keyboard.h @@ -105,13 +105,13 @@ typedef struct osk_keyboard char *name; /* The name of the keyboard */ char *keyboard_list; /* The names of the keyboards allowed from this one */ SDL_Surface *surface; /* The surface containing the current layout's keyboard */ - /* The surfaces containing the current layout's button backgrounds*/ + /* The surfaces containing the current layout's button backgrounds */ SDL_Surface *button_up; SDL_Surface *button_down; SDL_Surface *button_off; SDL_Surface *button_nav; SDL_Surface *button_hold; - /* The surfaces containing some symbols for the current layout's buttons */ + /* The surfaces containing some symbols for the current layout's buttons */ SDL_Surface *oskdel; /* delete arrow */ SDL_Surface *osktab; /* Tab arrows */ SDL_Surface *oskenter; /* Return hook/arrow */ @@ -133,7 +133,7 @@ typedef struct osk_keyboard int composed_type; /* 1 if the value stored in composed is yet the unicode value */ osk_composenode *composing; /* The node in the middle of a compose sequence */ osk_key *last_key_pressed; /* The last key pressed */ - SDL_Surface * canvas_ptr; /* Canvas drawing surface, for bpp and sizing needs when cycling through keyboard layouts */ + SDL_Surface *canvas_ptr; /* Canvas drawing surface, for bpp and sizing needs when cycling through keyboard layouts */ /* Large and small buttons, to pass back to osk_create() when cycling through keyboard layouts */ SDL_Surface *BLANK_button_up; SDL_Surface *BLANK_button_down; @@ -147,17 +147,23 @@ typedef struct osk_keyboard SDL_Surface *BLANK_oskshift; } on_screen_keyboard; -struct osk_keyboard *osk_create(char * layout_name, SDL_Surface * canvas, - SDL_Surface * BLANK_button_up, SDL_Surface * BLANK_button_down, - SDL_Surface * BLANK_button_off, SDL_Surface * BLANK_button_nav, +struct osk_keyboard *osk_create(char *layout_name, SDL_Surface * canvas, + SDL_Surface * BLANK_button_up, + SDL_Surface * BLANK_button_down, + SDL_Surface * BLANK_button_off, + SDL_Surface * BLANK_button_nav, SDL_Surface * BLANK_button_hold, - SDL_Surface * BLANK_oskdel, SDL_Surface * BLANK_osktab, SDL_Surface * BLANK_oskenter, - SDL_Surface * BLANK_oskcapslock, SDL_Surface * BLANK_oskshift, + SDL_Surface * BLANK_oskdel, + SDL_Surface * BLANK_osktab, + SDL_Surface * BLANK_oskenter, + SDL_Surface * BLANK_oskcapslock, + SDL_Surface * BLANK_oskshift, int disable_change); struct osk_layout *osk_load_layout(char *layout_name); -void osk_get_layout_data(char *layout_name, int *layout_w, int *layout_h, char *layout_buttons, char *layout_labels, +void osk_get_layout_data(char *layout_name, int *layout_w, int *layout_h, + char *layout_buttons, char *layout_labels, char *layout_keycodes); void osk_reset(on_screen_keyboard * osk); struct osk_keyboard *osk_clicked(on_screen_keyboard * keyboard, int x, int y); diff --git a/src/parse.h b/src/parse.h index a5034befe..d3f39cb8f 100644 --- a/src/parse.h +++ b/src/parse.h @@ -95,5 +95,5 @@ struct cfginfo #define CFGINFO_MAXOFFSET (sizeof(struct cfginfo)) -extern void parse_one_option(struct cfginfo *restrict tmpcfg, const char *str, const char *opt, - const char *restrict src); +extern void parse_one_option(struct cfginfo *restrict tmpcfg, const char *str, + const char *opt, const char *restrict src); diff --git a/src/pixels.c b/src/pixels.c index 5602b2fd7..49e1a549f 100644 --- a/src/pixels.c +++ b/src/pixels.c @@ -37,19 +37,21 @@ static void putpixel8(SDL_Surface * surface, int x, int y, Uint32 pixel) Uint8 *p; /* Assuming the X/Y values are within the bounds of this surface... */ - if (likely(likely((unsigned)x < (unsigned)surface->w) && likely((unsigned)y < (unsigned)surface->h))) - { - // Set a pointer to the exact location in memory of the pixel - p = (Uint8 *) (((Uint8 *) surface->pixels) + /* Start: beginning of RAM */ - (y * surface->pitch) + /* Go down Y lines */ - x); /* Go in X pixels */ + if (likely + (likely((unsigned) x < (unsigned) surface->w) + && likely((unsigned) y < (unsigned) surface->h))) + { + // Set a pointer to the exact location in memory of the pixel + p = (Uint8 *) (((Uint8 *) surface->pixels) + /* Start: beginning of RAM */ + (y * surface->pitch) + /* Go down Y lines */ + x); /* Go in X pixels */ - /* Set the (correctly-sized) piece of data in the surface's RAM - * to the pixel value sent in: */ + /* Set the (correctly-sized) piece of data in the surface's RAM + * to the pixel value sent in: */ - *p = pixel; - } + *p = pixel; + } } /* Draw a single pixel into the surface: */ @@ -58,19 +60,21 @@ static void putpixel16(SDL_Surface * surface, int x, int y, Uint32 pixel) Uint8 *p; /* Assuming the X/Y values are within the bounds of this surface... */ - if (likely(likely((unsigned)x < (unsigned)surface->w) && likely((unsigned)y < (unsigned)surface->h))) - { - // Set a pointer to the exact location in memory of the pixel - p = (Uint8 *) (((Uint8 *) surface->pixels) + /* Start: beginning of RAM */ - (y * surface->pitch) + /* Go down Y lines */ - (x * 2)); /* Go in X pixels */ + if (likely + (likely((unsigned) x < (unsigned) surface->w) + && likely((unsigned) y < (unsigned) surface->h))) + { + // Set a pointer to the exact location in memory of the pixel + p = (Uint8 *) (((Uint8 *) surface->pixels) + /* Start: beginning of RAM */ + (y * surface->pitch) + /* Go down Y lines */ + (x * 2)); /* Go in X pixels */ - /* Set the (correctly-sized) piece of data in the surface's RAM - * to the pixel value sent in: */ + /* Set the (correctly-sized) piece of data in the surface's RAM + * to the pixel value sent in: */ - *(Uint16 *) p = pixel; - } + *(Uint16 *) p = pixel; + } } /* Draw a single pixel into the surface: */ @@ -79,31 +83,33 @@ static void putpixel24(SDL_Surface * surface, int x, int y, Uint32 pixel) Uint8 *p; /* Assuming the X/Y values are within the bounds of this surface... */ - if (likely(likely((unsigned)x < (unsigned)surface->w) && likely((unsigned)y < (unsigned)surface->h))) + if (likely + (likely((unsigned) x < (unsigned) surface->w) + && likely((unsigned) y < (unsigned) surface->h))) + { + // Set a pointer to the exact location in memory of the pixel + p = (Uint8 *) (((Uint8 *) surface->pixels) + /* Start: beginning of RAM */ + (y * surface->pitch) + /* Go down Y lines */ + (x * 3)); /* Go in X pixels */ + + + /* Set the (correctly-sized) piece of data in the surface's RAM + * to the pixel value sent in: */ + + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { - // Set a pointer to the exact location in memory of the pixel - p = (Uint8 *) (((Uint8 *) surface->pixels) + /* Start: beginning of RAM */ - (y * surface->pitch) + /* Go down Y lines */ - (x * 3)); /* Go in X pixels */ - - - /* Set the (correctly-sized) piece of data in the surface's RAM - * to the pixel value sent in: */ - - if (SDL_BYTEORDER == SDL_BIG_ENDIAN) - { - p[0] = (pixel >> 16) & 0xff; - p[1] = (pixel >> 8) & 0xff; - p[2] = pixel & 0xff; - } - else - { - p[0] = pixel & 0xff; - p[1] = (pixel >> 8) & 0xff; - p[2] = (pixel >> 16) & 0xff; - } - + p[0] = (pixel >> 16) & 0xff; + p[1] = (pixel >> 8) & 0xff; + p[2] = pixel & 0xff; } + else + { + p[0] = pixel & 0xff; + p[1] = (pixel >> 8) & 0xff; + p[2] = (pixel >> 16) & 0xff; + } + + } } /* Draw a single pixel into the surface: */ @@ -112,19 +118,21 @@ static void putpixel32(SDL_Surface * surface, int x, int y, Uint32 pixel) Uint8 *p; /* Assuming the X/Y values are within the bounds of this surface... */ - if (likely(likely((unsigned)x < (unsigned)surface->w) && likely((unsigned)y < (unsigned)surface->h))) - { - // Set a pointer to the exact location in memory of the pixel - p = (Uint8 *) (((Uint8 *) surface->pixels) + /* Start: beginning of RAM */ - (y * surface->pitch) + /* Go down Y lines */ - (x * 4)); /* Go in X pixels */ + if (likely + (likely((unsigned) x < (unsigned) surface->w) + && likely((unsigned) y < (unsigned) surface->h))) + { + // Set a pointer to the exact location in memory of the pixel + p = (Uint8 *) (((Uint8 *) surface->pixels) + /* Start: beginning of RAM */ + (y * surface->pitch) + /* Go down Y lines */ + (x * 4)); /* Go in X pixels */ - /* Set the (correctly-sized) piece of data in the surface's RAM - * to the pixel value sent in: */ + /* Set the (correctly-sized) piece of data in the surface's RAM + * to the pixel value sent in: */ - *(Uint32 *) p = pixel; // 32-bit display - } + *(Uint32 *) p = pixel; // 32-bit display + } } /* Get a pixel: */ @@ -133,9 +141,9 @@ static Uint32 getpixel8(SDL_Surface * surface, int x, int y) Uint8 *p; /* get the X/Y values within the bounds of this surface */ - if (unlikely((unsigned)x > (unsigned)surface->w - 1u)) + if (unlikely((unsigned) x > (unsigned) surface->w - 1u)) x = (x < 0) ? 0 : surface->w - 1; - if (unlikely((unsigned)y > (unsigned)surface->h - 1u)) + if (unlikely((unsigned) y > (unsigned) surface->h - 1u)) y = (y < 0) ? 0 : surface->h - 1; /* Set a pointer to the exact location in memory of the pixel @@ -159,9 +167,9 @@ static Uint32 getpixel16(SDL_Surface * surface, int x, int y) Uint8 *p; /* get the X/Y values within the bounds of this surface */ - if (unlikely((unsigned)x > (unsigned)surface->w - 1u)) + if (unlikely((unsigned) x > (unsigned) surface->w - 1u)) x = (x < 0) ? 0 : surface->w - 1; - if (unlikely((unsigned)y > (unsigned)surface->h - 1u)) + if (unlikely((unsigned) y > (unsigned) surface->h - 1u)) y = (y < 0) ? 0 : surface->h - 1; /* Set a pointer to the exact location in memory of the pixel @@ -186,9 +194,9 @@ static Uint32 getpixel24(SDL_Surface * surface, int x, int y) Uint32 pixel; /* get the X/Y values within the bounds of this surface */ - if (unlikely((unsigned)x > (unsigned)surface->w - 1u)) + if (unlikely((unsigned) x > (unsigned) surface->w - 1u)) x = (x < 0) ? 0 : surface->w - 1; - if (unlikely((unsigned)y > (unsigned)surface->h - 1u)) + if (unlikely((unsigned) y > (unsigned) surface->h - 1u)) y = (y < 0) ? 0 : surface->h - 1; /* Set a pointer to the exact location in memory of the pixel @@ -219,9 +227,9 @@ static Uint32 getpixel32(SDL_Surface * surface, int x, int y) Uint8 *p; /* get the X/Y values within the bounds of this surface */ - if (unlikely((unsigned)x > (unsigned)surface->w - 1u)) + if (unlikely((unsigned) x > (unsigned) surface->w - 1u)) x = (x < 0) ? 0 : surface->w - 1; - if (unlikely((unsigned)y > (unsigned)surface->h - 1u)) + if (unlikely((unsigned) y > (unsigned) surface->h - 1u)) y = (y < 0) ? 0 : surface->h - 1; /* Set a pointer to the exact location in memory of the pixel @@ -239,11 +247,11 @@ static Uint32 getpixel32(SDL_Surface * surface, int x, int y) return *(Uint32 *) p; // 32-bit display } -void (*putpixels[]) (SDL_Surface *, int, int, Uint32) = -{ -putpixel8, putpixel8, putpixel16, putpixel24, putpixel32}; +void (*putpixels[])(SDL_Surface *, int, int, Uint32) = { + putpixel8, putpixel8, putpixel16, putpixel24, putpixel32 +}; -Uint32(*getpixels[])(SDL_Surface *, int, int) = -{ -getpixel8, getpixel8, getpixel16, getpixel24, getpixel32}; +Uint32(*getpixels[])(SDL_Surface *, int, int) = { + getpixel8, getpixel8, getpixel16, getpixel24, getpixel32 +}; diff --git a/src/pixels.h b/src/pixels.h index 853c5d2a5..a2cee79fe 100644 --- a/src/pixels.h +++ b/src/pixels.h @@ -32,7 +32,7 @@ #include "SDL.h" -extern void (*putpixels[]) (SDL_Surface *, int, int, Uint32); +extern void (*putpixels[])(SDL_Surface *, int, int, Uint32); extern Uint32(*getpixels[]) (SDL_Surface *, int, int); #endif diff --git a/src/platform.h b/src/platform.h index 92e445b10..5152aaa24 100644 --- a/src/platform.h +++ b/src/platform.h @@ -24,21 +24,21 @@ #if defined(__APPLE__) - #include +#include /* - * MAC test must be last because it tests true even on iOS / tvOS / watchOS. - */ + * MAC test must be last because it tests true even on iOS / tvOS / watchOS. + */ - #if TARGET_OS_IOS || TARGET_OS_IPHONE || TARGET_OS_SIMULATOR || TARGET_IPHONE_SIMULATOR || TARGET_OS_EMBEDDED - #define __IOS__ 1 - #elif TARGET_OS_OSX || TARGET_OS_MAC - #define __MACOS__ 1 - #else - #define __OTHER_APPLE__ 1 +#if TARGET_OS_IOS || TARGET_OS_IPHONE || TARGET_OS_SIMULATOR || TARGET_IPHONE_SIMULATOR || TARGET_OS_EMBEDDED +#define __IOS__ 1 +#elif TARGET_OS_OSX || TARGET_OS_MAC +#define __MACOS__ 1 +#else +#define __OTHER_APPLE__ 1 - #warning "Unsupported Apple platform, will build on a best-effort basis" - #endif +#warning "Unsupported Apple platform, will build on a best-effort basis" +#endif #endif /* __APPLE__ */ diff --git a/src/playsound.c b/src/playsound.c index 82d9ec929..babe09c1d 100644 --- a/src/playsound.c +++ b/src/playsound.c @@ -48,70 +48,72 @@ static int old_sound[4] = { -1, -1, -1, -1 }; * (low values, near the top of the window, are quieter), or * SNDDIST_NEAR for full volume */ -void playsound(SDL_Surface * screen, int chan, int s, int override, int x, int y) +void playsound(SDL_Surface * screen, int chan, int s, int override, int x, + int y) { #ifndef NOSOUND int left, dist; if (!mute && use_sound && s != SND_NONE) - { + { #ifdef DEBUG - printf("playsound #%d in channel %d, pos (%d,%d), %soverride, ptr=%p\n", s, chan, x, y, override ? "" : "no ", - sounds[s]); + printf("playsound #%d in channel %d, pos (%d,%d), %soverride, ptr=%p\n", + s, chan, x, y, override ? "" : "no ", sounds[s]); + fflush(stdout); +#endif + if (override || !Mix_Playing(chan)) + { + Mix_PlayChannel(chan, sounds[s], 0); + + old_sound[chan] = s; + } + + if (old_sound[chan] == s) + { + if (y == SNDDIST_NEAR) + dist = 0; + else + { + if (y < 0) + y = 0; + else if (y >= screen->h - 1) + y = screen->h - 1; + + dist = (255 * ((screen->h - 1) - y)) / (screen->h - 1); + } + + + if (use_stereo) + { + if (x == SNDPOS_LEFT) + left = 255 - dist; + else if (x == SNDPOS_CENTER) + left = (255 - dist) / 2; + else if (x == SNDPOS_RIGHT) + left = 0; + else + { + if (x < 0) + x = 0; + else if (x >= screen->w) + x = screen->w - 1; + + left = ((255 - dist) * ((screen->w - 1) - x)) / (screen->w - 1); + } + } + else + { + /* Stereo disabled; treat everything like a SNDPOS_CENTER + (equal amount in each of the left/right channels) */ + left = (255 - dist) / 2; + } +#ifdef DEBUG + printf("Panning of sound #%d in channel %d, left=%d, right=%d\n", s, + chan, left, (255 - dist) - left); fflush(stdout); #endif - if (override || !Mix_Playing(chan)) - { - Mix_PlayChannel(chan, sounds[s], 0); - - old_sound[chan] = s; - } - - if (old_sound[chan] == s) - { - if (y == SNDDIST_NEAR) - dist = 0; - else - { - if (y < 0) - y = 0; - else if (y >= screen->h - 1) - y = screen->h - 1; - - dist = (255 * ((screen->h - 1) - y)) / (screen->h - 1); - } - - - if (use_stereo) - { - if (x == SNDPOS_LEFT) - left = 255 - dist; - else if (x == SNDPOS_CENTER) - left = (255 - dist) / 2; - else if (x == SNDPOS_RIGHT) - left = 0; - else - { - if (x < 0) - x = 0; - else if (x >= screen->w) - x = screen->w - 1; - - left = ((255 - dist) * ((screen->w - 1) - x)) / (screen->w - 1); - } - } - else - { - /* Stereo disabled; treat everything like a SNDPOS_CENTER - (equal amount in each of the left/right channels) */ - left = (255 - dist) / 2; - } -#ifdef DEBUG - printf("Panning of sound #%d in channel %d, left=%d, right=%d\n", s, chan, left, (255 - dist) - left); - fflush(stdout); -#endif - Mix_SetPanning(chan, left, (255 - dist) - left); - } + Mix_SetPanning(chan, left, (255 - dist) - left); } + } #endif } diff --git a/src/playsound.h b/src/playsound.h index c0d9097f0..ec3347321 100644 --- a/src/playsound.h +++ b/src/playsound.h @@ -37,6 +37,7 @@ extern Mix_Chunk *sounds[NUM_SOUNDS]; extern int mute, use_sound, use_stereo; -void playsound(SDL_Surface * screen, int chan, int s, int override, int x, int y); +void playsound(SDL_Surface * screen, int chan, int s, int override, int x, + int y); #endif diff --git a/src/postscript_print.c b/src/postscript_print.c index 802e5e944..e91f31a8d 100644 --- a/src/postscript_print.c +++ b/src/postscript_print.c @@ -69,17 +69,18 @@ static int f2int(float f) { - return ((int)f); + return ((int) f); } static int f2dec(float f) { - return (int)((f - f2int(f)) * 100); + return (int) ((f - f2int(f)) * 100); } /* Actually save the PostScript data to the file stream: */ int do_ps_save(FILE * fi, - const char *restrict const fname, SDL_Surface * surf, const char *restrict pprsize, int is_pipe) + const char *restrict const fname, SDL_Surface * surf, + const char *restrict pprsize, int is_pipe) { const struct paper *ppr; int img_w = surf->w; @@ -93,7 +94,8 @@ int do_ps_save(FILE * fi, Uint8 r, g, b; char buf[256]; - Uint32(*getpixel) (SDL_Surface *, int, int) = getpixels[surf->format->BytesPerPixel]; + Uint32(*getpixel) (SDL_Surface *, int, int) = + getpixels[surf->format->BytesPerPixel]; int printed_img_w, printed_img_h; time_t t = time(NULL); int rotate; @@ -103,35 +105,35 @@ int do_ps_save(FILE * fi, /* Determine paper size: */ if (pprsize == NULL) + { + /* User did not request a specific paper size (on command-line or + in config file), ask the system. It will return either their + $PAPER env. var., the value from /etc/papersize, or NULL: */ + + pprsize = systempapername(); + + if (pprsize == NULL) { - /* User did not request a specific paper size (on command-line or - in config file), ask the system. It will return either their - $PAPER env. var., the value from /etc/papersize, or NULL: */ + /* No setting, env. var. or /etc/ file; use the default! */ - pprsize = systempapername(); - - if (pprsize == NULL) - { - /* No setting, env. var. or /etc/ file; use the default! */ - - pprsize = defaultpapername(); + pprsize = defaultpapername(); #ifdef DEBUG - printf("Using default paper\n"); -#endif - } -#ifdef DEBUG - else - { - printf("Using system paper\n"); - } + printf("Using default paper\n"); #endif } +#ifdef DEBUG + else + { + printf("Using system paper\n"); + } +#endif + } #ifdef DEBUG else - { - printf("Using user paper\n"); - } + { + printf("Using user paper\n"); + } #endif #ifdef DEBUG @@ -147,7 +149,8 @@ int do_ps_save(FILE * fi, ppr_h = paperpsheight(ppr); #ifdef DEBUG - printf("Paper is %d x %d (%.2f\" x %.2f\")\n", ppr_w, ppr_h, (float)ppr_w / 72.0, (float)ppr_h / 72.0); + printf("Paper is %d x %d (%.2f\" x %.2f\")\n", ppr_w, ppr_h, + (float) ppr_w / 72.0, (float) ppr_h / 72.0); #endif paperdone(); // FIXME: Should we do this at quit? -bjk 2007.06.25 @@ -155,18 +158,19 @@ int do_ps_save(FILE * fi, /* Determine whether it's best to rotate the image: */ - if ((ppr_w >= ppr_h && img_w >= img_h) || (ppr_w <= ppr_h && img_w <= img_h)) - { - rotate = 0; - r_img_w = img_w; - r_img_h = img_h; - } + if ((ppr_w >= ppr_h && img_w >= img_h) + || (ppr_w <= ppr_h && img_w <= img_h)) + { + rotate = 0; + r_img_w = img_w; + r_img_h = img_h; + } else - { - rotate = 1; - r_img_w = img_h; - r_img_h = img_w; - } + { + rotate = 1; + r_img_w = img_h; + r_img_h = img_w; + } #ifdef DEBUG printf("Image is %d x %d\n", img_w, img_h); @@ -177,13 +181,16 @@ int do_ps_save(FILE * fi, /* Determine scale: */ - scale = my_min(((float)(ppr_w - (MARGIN * 2)) / (float)r_img_w), ((float)(ppr_h - (MARGIN * 2)) / (float)r_img_h)); + scale = + my_min(((float) (ppr_w - (MARGIN * 2)) / (float) r_img_w), + ((float) (ppr_h - (MARGIN * 2)) / (float) r_img_h)); printed_img_w = r_img_w * scale; printed_img_h = r_img_h * scale; #ifdef DEBUG - printf("Scaling image by %.2f (to %d x %d)\n", scale, printed_img_w, printed_img_h); + printf("Scaling image by %.2f (to %d x %d)\n", scale, printed_img_w, + printed_img_h); #endif @@ -209,7 +216,8 @@ int do_ps_save(FILE * fi, fprintf(fi, "%%%%Pages: 1\n"); - fprintf(fi, "%%%%BoundingBox: 0 0 %d %d\n", (int)(ppr_w + 0.5), (int)(ppr_h + 0.5)); + fprintf(fi, "%%%%BoundingBox: 0 0 %d %d\n", (int) (ppr_w + 0.5), + (int) (ppr_h + 0.5)); fprintf(fi, "%%%%EndComments\n"); @@ -228,20 +236,23 @@ int do_ps_save(FILE * fi, fprintf(fi, "%%%%Page: 1 1\n"); - fprintf(fi, "<< /PageSize [ %d %d ] /ImagingBBox null >> setpagedevice\n", ppr_w, ppr_h); + fprintf(fi, "<< /PageSize [ %d %d ] /ImagingBBox null >> setpagedevice\n", + ppr_w, ppr_h); fprintf(fi, "gsave\n"); /* 'translate' moves the user space origin to a new position with respect to the current page, leaving the orientation of the axes and the unit lengths unchanged. */ - fprintf(fi, "%d.%02d %d.%02d translate\n", f2int(tlate_x), f2dec(tlate_x), f2int(tlate_y), f2dec(tlate_y)); + fprintf(fi, "%d.%02d %d.%02d translate\n", f2int(tlate_x), f2dec(tlate_x), + f2int(tlate_y), f2dec(tlate_y)); /* 'scale' modifies the unit lengths independently along the current x and y axes, leaving the origin location and the orientation of the axes unchanged. */ fprintf(fi, "%d.%02d %d.%02d scale\n", - f2int(printed_img_w), f2dec(printed_img_w), f2int(printed_img_h), f2dec(printed_img_h)); + f2int(printed_img_w), f2dec(printed_img_w), f2int(printed_img_h), + f2dec(printed_img_h)); /* Rotate the image */ if (rotate) @@ -261,23 +272,23 @@ int do_ps_save(FILE * fi, cur_line_len = 0; for (y = 0; y < img_h; y++) + { + for (plane = 0; plane < 3; plane++) { - for (plane = 0; plane < 3; plane++) - { - for (x = 0; x < img_w; x++) - { - SDL_GetRGB(getpixel(surf, x, y), surf->format, &r, &g, &b); - fprintf(fi, "%02x", (plane == 0 ? r : (plane == 1 ? g : b))); + for (x = 0; x < img_w; x++) + { + SDL_GetRGB(getpixel(surf, x, y), surf->format, &r, &g, &b); + fprintf(fi, "%02x", (plane == 0 ? r : (plane == 1 ? g : b))); - cur_line_len++; - if (cur_line_len >= 30) - { - fprintf(fi, "\n"); - cur_line_len = 0; - } - } + cur_line_len++; + if (cur_line_len >= 30) + { + fprintf(fi, "\n"); + cur_line_len = 0; } + } } + } fprintf(fi, "\n"); fprintf(fi, "grestore\n"); @@ -286,74 +297,74 @@ int do_ps_save(FILE * fi, fprintf(fi, "%%%%EOF\n"); if (!is_pipe) - { - fclose(fi); - return 1; - } + { + fclose(fi); + return 1; + } else - { - pid_t child_pid, w; - int status; + { + pid_t child_pid, w; + int status; #ifdef __APPLE__ - /* macOS does not always reset errno so Tux Paint thinks print never - * succeeds - let's reset before calling pclose() on macOS */ - errno = 0; + /* macOS does not always reset errno so Tux Paint thinks print never + * succeeds - let's reset before calling pclose() on macOS */ + errno = 0; #endif - child_pid = pclose(fi); + child_pid = pclose(fi); #ifdef DEBUG - printf("pclose returned %d\n", child_pid); - fflush(stdout); - printf("errno = %d\n", errno); - fflush(stdout); + printf("pclose returned %d\n", child_pid); + fflush(stdout); + printf("errno = %d\n", errno); + fflush(stdout); #endif - if (child_pid < 0 || (errno != 0 && errno != EAGAIN)) - { /* FIXME: This right? */ - return 0; - } - else if (child_pid == 0) - { - return 1; - } - - do - { - w = waitpid(child_pid, &status, 0); - -#ifdef DEBUG - if (w == -1) - { - perror("waitpid"); - exit(EXIT_FAILURE); - } - if (WIFEXITED(status)) - { - printf("exited, status=%d\n", WEXITSTATUS(status)); - } - else if (WIFSIGNALED(status)) - { - printf("killed by signal %d\n", WTERMSIG(status)); - } - else if (WIFSTOPPED(status)) - { - printf("stopped by signal %d\n", WSTOPSIG(status)); - } - else if (WIFCONTINUED(status)) - { - printf("continued\n"); - } -#endif - } - while (w != -1 && !WIFEXITED(status) && !WIFSIGNALED(status)); - - if (WIFEXITED(status) && WEXITSTATUS(status) != 0) /* Not happy exit */ - return 0; - + if (child_pid < 0 || (errno != 0 && errno != EAGAIN)) + { /* FIXME: This right? */ + return 0; + } + else if (child_pid == 0) + { return 1; } + + do + { + w = waitpid(child_pid, &status, 0); + +#ifdef DEBUG + if (w == -1) + { + perror("waitpid"); + exit(EXIT_FAILURE); + } + if (WIFEXITED(status)) + { + printf("exited, status=%d\n", WEXITSTATUS(status)); + } + else if (WIFSIGNALED(status)) + { + printf("killed by signal %d\n", WTERMSIG(status)); + } + else if (WIFSTOPPED(status)) + { + printf("stopped by signal %d\n", WSTOPSIG(status)); + } + else if (WIFCONTINUED(status)) + { + printf("continued\n"); + } +#endif + } + while (w != -1 && !WIFEXITED(status) && !WIFSIGNALED(status)); + + if (WIFEXITED(status) && WEXITSTATUS(status) != 0) /* Not happy exit */ + return 0; + + return 1; + } } #endif diff --git a/src/postscript_print.h b/src/postscript_print.h index 8bf2ecc82..1f882abd6 100644 --- a/src/postscript_print.h +++ b/src/postscript_print.h @@ -79,7 +79,8 @@ #ifdef PRINTMETHOD_PS int do_ps_save(FILE * fi, - const char *restrict const fname, SDL_Surface * surf, const char *restrict pprsize, int is_pipe); + const char *restrict const fname, SDL_Surface * surf, + const char *restrict pprsize, int is_pipe); #endif diff --git a/src/progressbar.c b/src/progressbar.c index 43f5c1ef5..461c9644a 100644 --- a/src/progressbar.c +++ b/src/progressbar.c @@ -39,7 +39,8 @@ int progress_bar_disabled, prog_bar_ctr; * * @param screen Screen surface */ -void show_progress_bar_(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer) +void show_progress_bar_(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer) { SDL_Rect dest, src, r; int x; @@ -51,34 +52,36 @@ void show_progress_bar_(SDL_Surface * screen, SDL_Texture * texture, SDL_Rendere newtime = SDL_GetTicks(); if (newtime > oldtime + 15) /* trying not to eat some serious CPU time! */ + { + for (x = 0; x < screen->w; x = x + 65) { - for (x = 0; x < screen->w; x = x + 65) - { - src.x = 65 - (prog_bar_ctr % 65); - src.y = 0; - src.w = 65; - src.h = 24; + src.x = 65 - (prog_bar_ctr % 65); + src.y = 0; + src.w = 65; + src.h = 24; - dest.x = x; - dest.y = screen->h - 24; + dest.x = x; + dest.y = screen->h - 24; - SDL_BlitSurface(img_progress, &src, screen, &dest); - } - - prog_bar_ctr++; - - r.x = 0; - r.y = screen->h - 24; - r.w = screen->w; - r.h = 24; - - SDL_UpdateTexture(texture, &r, screen->pixels + ((screen->h - 24) * screen->pitch), screen->pitch); - - /* Docs says one should clear the renderer, even if this means a refresh of the whole thing. */ - SDL_RenderClear(renderer); - SDL_RenderCopy(renderer, texture, NULL, NULL); - SDL_RenderPresent(renderer); + SDL_BlitSurface(img_progress, &src, screen, &dest); } + + prog_bar_ctr++; + + r.x = 0; + r.y = screen->h - 24; + r.w = screen->w; + r.h = 24; + + SDL_UpdateTexture(texture, &r, + screen->pixels + ((screen->h - 24) * screen->pitch), + screen->pitch); + + /* Docs says one should clear the renderer, even if this means a refresh of the whole thing. */ + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, texture, NULL, NULL); + SDL_RenderPresent(renderer); + } oldtime = newtime; diff --git a/src/progressbar.h b/src/progressbar.h index 75df115e9..9061fef7a 100644 --- a/src/progressbar.h +++ b/src/progressbar.h @@ -36,6 +36,7 @@ extern SDL_Surface *img_progress; extern int progress_bar_disabled, prog_bar_ctr; -void show_progress_bar_(SDL_Surface * screen, SDL_Texture *texture, SDL_Renderer *renderer); +void show_progress_bar_(SDL_Surface * screen, SDL_Texture * texture, + SDL_Renderer * renderer); #endif diff --git a/src/rgblinear.c b/src/rgblinear.c index e3b5d18c9..88172d63d 100644 --- a/src/rgblinear.c +++ b/src/rgblinear.c @@ -43,11 +43,11 @@ unsigned char linear_to_sRGB(float linear) slot = linear * 4096.0 + 0.5; if (slot > 4095) - { - if (linear > 0.5) - slot = 4095; - else - slot = 0; - } + { + if (linear > 0.5) + slot = 4095; + else + slot = 0; + } return linear_to_sRGB_table[slot]; } diff --git a/src/rgblinear.h b/src/rgblinear.h index 7fe31ec9e..7bca5f64f 100644 --- a/src/rgblinear.h +++ b/src/rgblinear.h @@ -305,7 +305,8 @@ static const unsigned char linear_to_sRGB_table[4096] = "\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd" "\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe" "\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe" - "\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff" "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"; + "\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"; unsigned char linear_to_sRGB(float linear) FUNCTION; diff --git a/src/shapes.h b/src/shapes.h index 2c71ccb44..614b05cdd 100644 --- a/src/shapes.h +++ b/src/shapes.h @@ -118,8 +118,8 @@ const int shape_locked[NUM_SHAPES] = { 0, /* Hexagon */ 0, /* Heptagon */ 0, /* Heptagon */ - 1, /* Octagon */ /* FIXME: Consider unlocking? -bjk 2022.01.21 */ - 1, /* Octagon */ /* FIXME: Consider unlocking? -bjk 2022.01.21 */ + 1, /* Octagon *//* FIXME: Consider unlocking? -bjk 2022.01.21 */ + 1, /* Octagon *//* FIXME: Consider unlocking? -bjk 2022.01.21 */ 0, /* Rhombus */ 0, /* Rhombus */ 0, /* 3 points star */ @@ -334,8 +334,10 @@ const char *const shape_tips[NUM_SHAPES] = { gettext_noop("A rectangle has four sides and four right angles."), // Description of a circle - gettext_noop("A circle is a curve where all points have the same distance from the center."), - gettext_noop("A circle is a curve where all points have the same distance from the center."), + gettext_noop + ("A circle is a curve where all points have the same distance from the center."), + gettext_noop + ("A circle is a curve where all points have the same distance from the center."), // Description of an ellipse gettext_noop("An ellipse is a stretched circle."), @@ -362,8 +364,10 @@ const char *const shape_tips[NUM_SHAPES] = { gettext_noop("An octagon has eight equal sides."), // Description of a rhombus - gettext_noop("A rhombus has four equal sides, and opposite sides are parallel."), - gettext_noop("A rhombus has four equal sides, and opposite sides are parallel."), + gettext_noop + ("A rhombus has four equal sides, and opposite sides are parallel."), + gettext_noop + ("A rhombus has four equal sides, and opposite sides are parallel."), gettext_noop("A star with 3 points."), gettext_noop("A star with 3 points."), @@ -423,15 +427,18 @@ const char *const shapemode_img_fnames[NUM_SHAPEMODES] = { /* String shown when Shapes tool is selected; one version for normal ("complex shapes"), the other for simplified mode ("simple shapes") */ -enum { +enum +{ SHAPE_COMPLEXITY_NORMAL, SHAPE_COMPLEXITY_SIMPLE, NUM_SHAPE_COMPLEXITIES }; const char *const shape_tool_tips[NUM_SHAPE_COMPLEXITIES] = { - gettext_noop("Pick a shape. Click to start drawing, drag, and let go when it is the size and shape you want. Move around to rotate it, and click again to draw it."), - gettext_noop("Pick a shape. Click to start drawing, drag, and let go when it is the size and shape you want.") + gettext_noop + ("Pick a shape. Click to start drawing, drag, and let go when it is the size and shape you want. Move around to rotate it, and click again to draw it."), + gettext_noop + ("Pick a shape. Click to start drawing, drag, and let go when it is the size and shape you want.") }; /* Strings shown when switching between "from center" diff --git a/src/test-png.c b/src/test-png.c index 03c89764c..3acbaee07 100644 --- a/src/test-png.c +++ b/src/test-png.c @@ -29,17 +29,19 @@ #include #include -int main(int argc, char * argv[]) { +int main(int argc, char *argv[]) +{ int i, w, h, y; - FILE * fi; + FILE *fi; png_structp png; png_infop info; png_byte ctype, depth; - png_bytep * rows; + png_bytep *rows; /* Usage output */ - if (argc == 1 || strcmp(argv[1], "--help") == 0) { + if (argc == 1 || strcmp(argv[1], "--help") == 0) + { fprintf(stderr, "Usage: %s file.png [file.png ...]\n", argv[0]); exit(1); } @@ -53,30 +55,39 @@ int main(int argc, char * argv[]) { /* Open each PNG image!... */ - for (i = 1; i < argc; i++) { - printf("%5d ------------------------------------------------------------------\n", i); + for (i = 1; i < argc; i++) + { + printf + ("%5d ------------------------------------------------------------------\n", + i); printf("%s\n", argv[i]); fflush(stdout); /* Open the file */ fi = fopen(argv[i], "rb"); - if (fi == NULL) { + if (fi == NULL) + { printf("Cannot open\n"); - } else { + } + else + { /* Prepare PNG library stuff... */ png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - if (!png) { + if (!png) + { fprintf(stderr, "Cannot png_create_read_struct()!\n"); exit(1); } info = png_create_info_struct(png); - if (!info) { + if (!info) + { fprintf(stderr, "Cannot png_create_info_struct()!\n"); exit(1); } - if (setjmp(png_jmpbuf(png))) { + if (setjmp(png_jmpbuf(png))) + { fprintf(stderr, "Cannot setjmp(png_jmpbuf(png)))!\n"); exit(1); } @@ -95,39 +106,43 @@ int main(int argc, char * argv[]) { depth = png_get_bit_depth(png, info); /* If 16-bit, strip to 8-bit */ - if (depth == 16) { + if (depth == 16) + { printf("test-png warning: 16-bit\n"); png_set_strip_16(png); } /* Switch palette to RGB */ - if (ctype == PNG_COLOR_TYPE_PALETTE) { + if (ctype == PNG_COLOR_TYPE_PALETTE) + { printf("test-png warning: paletted\n"); png_set_palette_to_rgb(png); } /* Expand low-depth greyscale up to 8-bit */ - if (ctype == PNG_COLOR_TYPE_GRAY && depth < 8) { + if (ctype == PNG_COLOR_TYPE_GRAY && depth < 8) + { printf("test-png warning: greyscale with only %d-bit depth\n", depth); png_set_expand_gray_1_2_4_to_8(png); } /* Expand tRNS chunks into alpha */ - if (png_get_valid(png, info, PNG_INFO_tRNS)) { + if (png_get_valid(png, info, PNG_INFO_tRNS)) + { printf("test-png warning: contains tRNS chunk\n"); png_set_tRNS_to_alpha(png); } /* Fill alpha channel if there is none */ if (ctype == PNG_COLOR_TYPE_RGB || - ctype == PNG_COLOR_TYPE_GRAY || - ctype == PNG_COLOR_TYPE_PALETTE) { + ctype == PNG_COLOR_TYPE_GRAY || ctype == PNG_COLOR_TYPE_PALETTE) + { png_set_filler(png, 0xFF, PNG_FILLER_AFTER); } /* Expand grey to color */ - if (ctype == PNG_COLOR_TYPE_GRAY || - ctype == PNG_COLOR_TYPE_GRAY_ALPHA) { + if (ctype == PNG_COLOR_TYPE_GRAY || ctype == PNG_COLOR_TYPE_GRAY_ALPHA) + { printf("test-png warning: greyscale\n"); png_set_gray_to_rgb(png); } @@ -137,15 +152,19 @@ int main(int argc, char * argv[]) { /* Allocate space */ rows = (png_bytep *) malloc(sizeof(png_bytep) * h); - if (!rows) { + if (!rows) + { fprintf(stderr, "Failed to malloc() space for image data rows!\n"); exit(1); } - for (y = 0; y < h; y++) { + for (y = 0; y < h; y++) + { rows[y] = (png_byte *) malloc(png_get_rowbytes(png, info)); - if (!rows[y]) { - fprintf(stderr, "Failed to malloc() space for image data row #%d!\n", y); + if (!rows[y]) + { + fprintf(stderr, + "Failed to malloc() space for image data row #%d!\n", y); exit(1); } } @@ -170,5 +189,5 @@ int main(int argc, char * argv[]) { fflush(stdout); } - return(0); + return (0); } diff --git a/src/tuxpaint.c b/src/tuxpaint.c index dea9c4934..64d2dcdcd 100644 --- a/src/tuxpaint.c +++ b/src/tuxpaint.c @@ -224,7 +224,7 @@ char *strcasestr(const char *haystack, const char *needle) result = strstr(uphaystack, upneedle); if (result != NULL) - return (result - uphaystack + (char *)haystack); + return (result - uphaystack + (char *) haystack); else return NULL; } @@ -556,15 +556,16 @@ int iswprint(wchar_t wc) (anything with smaller resolution will round up or down) */ #define SLOPPY_FRAC_MIN (float) 1 #define SLOPPY_FRAC_MAX (float) 10 -static void sloppy_frac(float f, int * numer, int * denom); +static void sloppy_frac(float f, int *numer, int *denom); -static void select_label_node(int * old_x, int * old_y); +static void select_label_node(int *old_x, int *old_y); static void apply_label_node(int old_x, int old_y); static void reposition_onscreen_keyboard(int y); -static void reset_stamps(int * stamp_xored_rt, int * stamp_place_x, int * stamp_place_y, int * stamp_tool_mode); +static void reset_stamps(int *stamp_xored_rt, int *stamp_place_x, + int *stamp_place_y, int *stamp_tool_mode); /* EP added #ifndef __APPLE__ because macros are buggy (shifted by 1 byte), plus the function exists in SDL */ #ifndef __APPLE__ @@ -604,9 +605,9 @@ static void reset_stamps(int * stamp_xored_rt, int * stamp_place_x, int * stamp_ #endif #if (SDL_MAJOR_VERSION < 2) - #define EVENT_FILTER_EVENT_TYPE const SDL_Event +#define EVENT_FILTER_EVENT_TYPE const SDL_Event #else - #define EVENT_FILTER_EVENT_TYPE union SDL_Event +#define EVENT_FILTER_EVENT_TYPE union SDL_Event #endif int TP_EventFilter(void *data, EVENT_FILTER_EVENT_TYPE * event); @@ -649,16 +650,16 @@ FILE *my_fmemopen(unsigned char *data, size_t size, const char *mode) fi = fopen(fname, "wb"); if (fi == NULL) - { - free(fname); - return (NULL); - } + { + free(fname); + return (NULL); + } for (i = 0; i < size; i++) - { - fwrite(data, 1, 1, fi); - data++; - } + { + fwrite(data, 1, 1, fi); + data++; + } fclose(fi); fi = fopen(fname, mode); @@ -696,9 +697,9 @@ enum /* Modes of the "Label" tool */ enum { - LABEL_LABEL, /* Adding new label(s) */ - LABEL_SELECT, /* "Select" button clicked; user is selecting a label to edit */ - LABEL_APPLY /* "Apply" button clicked; user is selecting a label to apply permanently to the canvas */ + LABEL_LABEL, /* Adding new label(s) */ + LABEL_SELECT, /* "Select" button clicked; user is selecting a label to edit */ + LABEL_APPLY /* "Apply" button clicked; user is selecting a label to apply permanently to the canvas */ }; @@ -711,8 +712,8 @@ static char **color_names; /* Special color options (from left-to-right (very last entry)) */ #define COLOR_SELECTOR (NUM_COLORS - 3) /* Pick a color from the canvas */ -#define COLOR_PICKER (NUM_COLORS - 2) /* Pick a color from a palette */ -#define COLOR_MIXER (NUM_COLORS - 1) /* Mix colors together */ +#define COLOR_PICKER (NUM_COLORS - 2) /* Pick a color from a palette */ +#define COLOR_MIXER (NUM_COLORS - 1) /* Mix colors together */ /* Show debugging stuff: */ @@ -724,7 +725,7 @@ static char **color_names; static void debug(const char *const str) { #ifndef DEBUG - (void)str; + (void) str; #else fprintf(stderr, "DEBUG: %s\n", str); fflush(stderr); @@ -766,7 +767,7 @@ static float render_scale; /* Scale factor for the render */ static int button_w; /* was 48 */ static int button_h; /* was 48 */ -static int button_size_auto = 0; /* if the size of buttons should be autocalculated */ +static int button_size_auto = 0; /* if the size of buttons should be autocalculated */ static float button_scale; /* scale factor to be applied to the size of buttons */ static int color_button_w; /* was 32 */ static int color_button_h; /* was 48 */ @@ -818,13 +819,16 @@ static void set_max_buttonscale(void) float max_w, max_h; /* WINDOW_WIDTH / original size of tools columnss + 9 buttons + tooloption columns */ - max_w = (float)WINDOW_WIDTH / (gd_tools.cols * 48 + 9 * 48 + gd_toolopt.cols * 48); + max_w = + (float) WINDOW_WIDTH / (gd_tools.cols * 48 + 9 * 48 + + gd_toolopt.cols * 48); /* WINDOW_HEIGHT / original size of r_ttools.h + 5 buttons + colors rows + tux area */ - max_h = (float)WINDOW_HEIGHT / (40 + 5 * 48 + gd_colors.rows * 48 + 56); + max_h = (float) WINDOW_HEIGHT / (40 + 5 * 48 + gd_colors.rows * 48 + 56); button_scale = min(max_w, max_h); - fprintf(stderr, "Will use a button size of %d\n", (int)(button_scale * ORIGINAL_BUTTON_SIZE)); + fprintf(stderr, "Will use a button size of %d\n", + (int) (button_scale * ORIGINAL_BUTTON_SIZE)); } /** @@ -876,17 +880,23 @@ static void setup_normal_screen_layout(void) r_tuxarea.w = WINDOW_WIDTH; /* need 56 minimum for the Tux area */ - buttons_tall = (WINDOW_HEIGHT - r_ttoolopt.h - 56 * button_scale - r_colors.h) / button_h; - if (buttons_tall < 5) { - fprintf(stderr, "Button size '%d' with window size '%dx%d' is not reasonable (not tall enough).\n", - button_w, WINDOW_WIDTH, WINDOW_HEIGHT); + buttons_tall = + (WINDOW_HEIGHT - r_ttoolopt.h - 56 * button_scale - + r_colors.h) / button_h; + if (buttons_tall < 5) + { + fprintf(stderr, + "Button size '%d' with window size '%dx%d' is not reasonable (not tall enough).\n", + button_w, WINDOW_WIDTH, WINDOW_HEIGHT); set_max_buttonscale(); setup_normal_screen_layout(); } - if (r_canvas.w < button_w * 9) { - fprintf(stderr, "Button size '%d' with window size '%dx%d' is not reasonable (not wide enough).\n", - button_w, WINDOW_WIDTH, WINDOW_HEIGHT); + if (r_canvas.w < button_w * 9) + { + fprintf(stderr, + "Button size '%d' with window size '%dx%d' is not reasonable (not wide enough).\n", + button_w, WINDOW_WIDTH, WINDOW_HEIGHT); set_max_buttonscale(); setup_normal_screen_layout(); } @@ -1043,7 +1053,8 @@ static void SDL_Flip(SDL_Surface * screen) SDL_RenderPresent(renderer); } -static void SDL_UpdateRect(SDL_Surface * screen, Sint32 x, Sint32 y, Sint32 w, Sint32 h) +static void SDL_UpdateRect(SDL_Surface * screen, Sint32 x, Sint32 y, Sint32 w, + Sint32 h) { SDL_Rect r; @@ -1052,7 +1063,8 @@ static void SDL_UpdateRect(SDL_Surface * screen, Sint32 x, Sint32 y, Sint32 w, S r.w = w; r.h = h; - SDL_UpdateTexture(texture, &r, screen->pixels + (y * screen->pitch + x * 4), screen->pitch); + SDL_UpdateTexture(texture, &r, screen->pixels + (y * screen->pitch + x * 4), + screen->pitch); // Docs says one must clear the renderer, even if this means a refresh of the whole thing. SDL_RenderClear(renderer); @@ -1083,18 +1095,18 @@ static void update_screen(int x1, int y1, int x2, int y2) int tmp; if (x1 > x2) - { - tmp = x1; - x1 = x2; - x2 = tmp; - } + { + tmp = x1; + x1 = x2; + x2 = tmp; + } if (y1 > y2) - { - tmp = y1; - y1 = y2; - y2 = tmp; - } + { + tmp = y1; + y1 = y2; + y2 = tmp; + } x1 = x1 - 1; x2 = x2 + 1; @@ -1147,7 +1159,8 @@ static void update_screen_rect(SDL_Rect * r) static int hit_test(const SDL_Rect * const r, unsigned x, unsigned y) { /* note the use of unsigned math: no need to check for negative */ - return (x - (unsigned) r->x < (unsigned) r->w) && (y - (unsigned) r->y < (unsigned) r->h); + return (x - (unsigned) r->x < (unsigned) r->w) + && (y - (unsigned) r->y < (unsigned) r->h); } #define HIT(r) hit_test(&(r), event.button.x, event.button.y) @@ -1162,7 +1175,8 @@ static int hit_test(const SDL_Rect * const r, unsigned x, unsigned y) * @param gd The grid of items * @returns The item clicked, or -1 if click was outside the grid. */ -static int grid_hit_gd(const SDL_Rect * const r, unsigned x, unsigned y, grid_dims * gd) +static int grid_hit_gd(const SDL_Rect * const r, unsigned x, unsigned y, + grid_dims * gd) { unsigned item_w = r->w / gd->cols; unsigned item_h = r->h / gd->rows; @@ -1218,12 +1232,12 @@ static void update_canvas_ex_r(int x1, int y1, int x2, int y2, int screen_too) dest.h = src.h; if (img_starter != NULL) - { - /* If there was a starter, cover this part of the drawing with - the corresponding part of the starter's foreground! */ + { + /* If there was a starter, cover this part of the drawing with + the corresponding part of the starter's foreground! */ - SDL_BlitSurface(img_starter, &dest, canvas, &dest); - } + SDL_BlitSurface(img_starter, &dest, canvas, &dest); + } dest.x = x1 + r_ttools.w; @@ -1256,22 +1270,22 @@ static void update_canvas_ex(int x1, int y1, int x2, int y2, int screen_too) SDL_Rect src, dest; if (img_starter != NULL) - { - /* If there was a starter, cover this part of the drawing with - the corresponding part of the starter's foreground! */ + { + /* If there was a starter, cover this part of the drawing with + the corresponding part of the starter's foreground! */ - src.x = x1; - src.y = y1; - src.w = x2 - x1 + 1; - src.h = y2 - y1 + 1; + src.x = x1; + src.y = y1; + src.w = x2 - x1 + 1; + src.h = y2 - y1 + 1; - dest.x = x1; - dest.y = y1; - dest.w = src.w; - dest.h = src.h; + dest.x = x1; + dest.y = y1; + dest.w = src.w; + dest.h = src.h; - SDL_BlitSurface(img_starter, &dest, canvas, &dest); - } + SDL_BlitSurface(img_starter, &dest, canvas, &dest); + } SDL_BlitSurface(canvas, NULL, screen, &r_canvas); @@ -1357,7 +1371,8 @@ static int button_down; static int scrolling_selector, scrolling_tool, scrolling_dialog; static int promptless_save = SAVE_OVER_UNSET; -static int _promptless_save_over, _promptless_save_over_ask, _promptless_save_over_new; +static int _promptless_save_over, _promptless_save_over_ask, + _promptless_save_over_new; static int disable_quit; static int noshortcuts; @@ -1447,10 +1462,12 @@ static unsigned select_text_size; static int coming_from_undo_or_redo = FALSE; -static void add_label_node(int, int, Uint16, Uint16, SDL_Surface * label_node_surface); +static void add_label_node(int, int, Uint16, Uint16, + SDL_Surface * label_node_surface); static void load_info_about_label_surface(FILE * lfi); -static struct label_node *search_label_list(struct label_node **, Uint16, Uint16, int hover); +static struct label_node *search_label_list(struct label_node **, Uint16, + Uint16, int hover); static void highlight_label_nodes(void); static void cycle_highlighted_label_node(void); static int are_labels(void); @@ -1466,22 +1483,28 @@ static void derender_node(struct label_node **); static void delete_label_list(struct label_node **); -static void myblit(SDL_Surface * src_surf, SDL_Rect * src_rect, SDL_Surface * dest_surf, SDL_Rect * dest_rect); +static void myblit(SDL_Surface * src_surf, SDL_Rect * src_rect, + SDL_Surface * dest_surf, SDL_Rect * dest_rect); static void set_label_fonts(void); static void tmp_apply_uncommited_text(void); static void undo_tmp_applied_text(void); -static void handle_joyaxismotion(SDL_Event event, int *motioner, int *val_x, int *val_y); -static void handle_joyhatmotion(SDL_Event event, int oldpos_x, int oldpos_y, int *valhat_x, int *valhat_y, +static void handle_joyaxismotion(SDL_Event event, int *motioner, int *val_x, + int *val_y); +static void handle_joyhatmotion(SDL_Event event, int oldpos_x, int oldpos_y, + int *valhat_x, int *valhat_y, int *hat_motioner, Uint32 * old_hat_ticks); static void handle_joyballmotion(SDL_Event event, int oldpos_x, int oldpos_y); -static void handle_joybuttonupdown(SDL_Event event, int oldpos_x, int oldpos_y); -static void handle_motioners(int oldpos_x, int oldpos_y, int motioner, int hatmotioner, int old_hat_ticks, int val_x, +static void handle_joybuttonupdown(SDL_Event event, int oldpos_x, + int oldpos_y); +static void handle_motioners(int oldpos_x, int oldpos_y, int motioner, + int hatmotioner, int old_hat_ticks, int val_x, int val_y, int valhat_x, int valhat_y); -static void handle_joybuttonupdownscl(SDL_Event event, int oldpos_x, int oldpos_y, SDL_Rect real_r_tools); +static void handle_joybuttonupdownscl(SDL_Event event, int oldpos_x, + int oldpos_y, SDL_Rect real_r_tools); #ifdef __ANDROID__ static void start_motion_convert(SDL_Event event); @@ -1489,9 +1512,9 @@ static void convert_motion_to_wheel(SDL_Event event); static void stop_motion_convert(SDL_Event event); #endif -char * get_xdg_user_dir(const char * dir_type, const char * fallback); +char *get_xdg_user_dir(const char *dir_type, const char *fallback); #ifdef WIN32 -extern char * GetUserImageDir(void); +extern char *GetUserImageDir(void); #endif /* Magic tools API and tool handles: */ @@ -1503,22 +1526,25 @@ static void special_notify(int flags); typedef struct magic_funcs_s { - int (*get_tool_count) (magic_api *); - int (*get_group) (magic_api *, int); - char *(*get_name) (magic_api *, int); - SDL_Surface *(*get_icon) (magic_api *, int); - char *(*get_description) (magic_api *, int, int); - int (*requires_colors) (magic_api *, int); - int (*modes) (magic_api *, int); - void (*set_color) (magic_api *, Uint8, Uint8, Uint8); - int (*init) (magic_api *); - Uint32(*api_version) (void); - void (*shutdown) (magic_api *); - void (*click) (magic_api *, int, int, SDL_Surface *, SDL_Surface *, int, int, SDL_Rect *); - void (*drag) (magic_api *, int, SDL_Surface *, SDL_Surface *, int, int, int, int, SDL_Rect *); - void (*release) (magic_api *, int, SDL_Surface *, SDL_Surface *, int, int, SDL_Rect *); - void (*switchin) (magic_api *, int, int, SDL_Surface *, SDL_Surface *); - void (*switchout) (magic_api *, int, int, SDL_Surface *, SDL_Surface *); + int (*get_tool_count)(magic_api *); + int (*get_group)(magic_api *, int); + char *(*get_name)(magic_api *, int); + SDL_Surface *(*get_icon)(magic_api *, int); + char *(*get_description)(magic_api *, int, int); + int (*requires_colors)(magic_api *, int); + int (*modes)(magic_api *, int); + void (*set_color)(magic_api *, Uint8, Uint8, Uint8); + int (*init)(magic_api *); + Uint32(*api_version) (void); + void (*shutdown)(magic_api *); + void (*click)(magic_api *, int, int, SDL_Surface *, SDL_Surface *, int, int, + SDL_Rect *); + void (*drag)(magic_api *, int, SDL_Surface *, SDL_Surface *, int, int, int, + int, SDL_Rect *); + void (*release)(magic_api *, int, SDL_Surface *, SDL_Surface *, int, int, + SDL_Rect *); + void (*switchin)(magic_api *, int, int, SDL_Surface *, SDL_Surface *); + void (*switchout)(magic_api *, int, int, SDL_Surface *, SDL_Surface *); } magic_funcs_t; @@ -1542,11 +1568,11 @@ typedef struct magic_s #define MAX_MAGICS_PER_GROUP 128 static int num_plugin_files; /* How many shared object files we went through */ -static void *magic_handle[MAX_MAGIC_GROUPS * MAX_MAGICS_PER_GROUP]; /* Handle to shared object (to be unloaded later) *//* FIXME: Unload them! */ -static magic_funcs_t magic_funcs[MAX_MAGIC_GROUPS * MAX_MAGICS_PER_GROUP]; /* Pointer to shared objects' functions */ +static void *magic_handle[MAX_MAGIC_GROUPS * MAX_MAGICS_PER_GROUP]; /* Handle to shared object (to be unloaded later) *//* FIXME: Unload them! */ +static magic_funcs_t magic_funcs[MAX_MAGIC_GROUPS * MAX_MAGICS_PER_GROUP]; /* Pointer to shared objects' functions */ static magic_t magics[MAX_MAGIC_GROUPS][MAX_MAGICS_PER_GROUP]; -static int num_magics[MAX_MAGIC_GROUPS]; /* How many magic tools were loaded (note: shared objs may report more than 1 tool) */ +static int num_magics[MAX_MAGIC_GROUPS]; /* How many magic tools were loaded (note: shared objs may report more than 1 tool) */ static int num_magics_total; enum @@ -1606,7 +1632,8 @@ static int have_to_rec_label_node; static int have_to_rec_label_node_back; static SDL_Surface *img_title, *img_title_credits, *img_title_tuxpaint; static SDL_Surface *img_btn_up, *img_btn_down, *img_btn_off, *img_btn_hold; -static SDL_Surface *img_btnsm_up, *img_btnsm_off, *img_btnsm_down, *img_btnsm_hold; +static SDL_Surface *img_btnsm_up, *img_btnsm_off, *img_btnsm_down, + *img_btnsm_hold; static SDL_Surface *img_btn_nav, *img_btnsm_nav; static SDL_Surface *img_brush_anim, *img_brush_dir; static SDL_Surface *img_prev, *img_next; @@ -1615,8 +1642,10 @@ static SDL_Surface *img_dead40x40; static SDL_Surface *img_black, *img_grey; static SDL_Surface *img_yes, *img_no; static SDL_Surface *img_sfx, *img_speak; -static SDL_Surface *img_open, *img_erase, *img_back, *img_trash, *img_pict_export; -static SDL_Surface *img_slideshow, *img_play, *img_gif_export, *img_select_digits; +static SDL_Surface *img_open, *img_erase, *img_back, *img_trash, + *img_pict_export; +static SDL_Surface *img_slideshow, *img_play, *img_gif_export, + *img_select_digits; static SDL_Surface *img_printer, *img_printer_wait; static SDL_Surface *img_save_over, *img_popup_arrow; static SDL_Surface *img_cursor_up, *img_cursor_down; @@ -1628,18 +1657,23 @@ static SDL_Surface *img_magic_paint, *img_magic_fullscreen; static SDL_Surface *img_shapes_corner, *img_shapes_center; static SDL_Surface *img_bold, *img_italic; static SDL_Surface *img_label_select, *img_label_apply; -static SDL_Surface *img_color_picker, *img_color_picker_thumb, *img_color_picker_val; +static SDL_Surface *img_color_picker, *img_color_picker_thumb, + *img_color_picker_val; static SDL_Surface *img_paintwell, *img_color_sel, *img_color_mix; static int color_picker_x, color_picker_y, color_picker_v; static int color_mixer_reset; -static SDL_Surface *img_title_on, *img_title_off, *img_title_large_on, *img_title_large_off; +static SDL_Surface *img_title_on, *img_title_off, *img_title_large_on, + *img_title_large_off; static SDL_Surface *img_title_names[NUM_TITLES]; static SDL_Surface *img_tools[NUM_TOOLS], *img_tool_names[NUM_TOOLS]; -static SDL_Surface *img_oskdel, *img_osktab, *img_oskenter, *img_oskcapslock, *img_oskshift; -static SDL_Surface *thumbnail(SDL_Surface * src, int max_x, int max_y, int keep_aspect); -static SDL_Surface *thumbnail2(SDL_Surface * src, int max_x, int max_y, int keep_aspect, int keep_alpha); +static SDL_Surface *img_oskdel, *img_osktab, *img_oskenter, *img_oskcapslock, + *img_oskshift; +static SDL_Surface *thumbnail(SDL_Surface * src, int max_x, int max_y, + int keep_aspect); +static SDL_Surface *thumbnail2(SDL_Surface * src, int max_x, int max_y, + int keep_aspect, int keep_alpha); #ifndef NO_BILINEAR static SDL_Surface *zoom(SDL_Surface * src, int new_x, int new_y); @@ -1654,7 +1688,8 @@ static SDL_Surface *zoom(SDL_Surface * src, int new_x, int new_y); * @param color The color to draw it in * @return A new surface, containing the rendered text */ -static SDL_Surface *render_text(TuxPaint_Font * restrict font, const char *restrict str, SDL_Color color) +static SDL_Surface *render_text(TuxPaint_Font * restrict font, + const char *restrict str, SDL_Color color) { SDL_Surface *ret = NULL; int height; @@ -1664,43 +1699,43 @@ static SDL_Surface *render_text(TuxPaint_Font * restrict font, const char *restr #endif if (font == NULL) - { - fprintf(stderr, "render_text() received a NULL font!\n"); - fflush(stdout); - return NULL; - } + { + fprintf(stderr, "render_text() received a NULL font!\n"); + fflush(stdout); + return NULL; + } #ifndef NO_SDLPANGO if (font->typ == FONT_TYPE_PANGO) - { - sdl_color_to_pango_color(color, &pango_color); + { + sdl_color_to_pango_color(color, &pango_color); #ifdef DEBUG - printf("Calling SDLPango_SetText(\"%s\")\n", str); - fflush(stdout); + printf("Calling SDLPango_SetText(\"%s\")\n", str); + fflush(stdout); #endif #ifdef __ANDROID__ - /* FIXME This extrange workaround helps in getting the translations working - on 4.3 4.4 */ - SDLPango_SetLanguage(font->pango_context, "ca"); + /* FIXME This extrange workaround helps in getting the translations working + on 4.3 4.4 */ + SDLPango_SetLanguage(font->pango_context, "ca"); #endif - SDLPango_SetDefaultColor(font->pango_context, &pango_color); - SDLPango_SetText(font->pango_context, str, -1); - ret = SDLPango_CreateSurfaceDraw(font->pango_context); - } + SDLPango_SetDefaultColor(font->pango_context, &pango_color); + SDLPango_SetText(font->pango_context, str, -1); + ret = SDLPango_CreateSurfaceDraw(font->pango_context); + } #endif if (font->typ == FONT_TYPE_TTF) - { + { #ifdef DEBUG - printf("Calling TTF_RenderUTF8_Blended(\"%s\")\n", str); - fflush(stdout); + printf("Calling TTF_RenderUTF8_Blended(\"%s\")\n", str); + fflush(stdout); #endif - ret = TTF_RenderUTF8_Blended(font->ttf_font, str, color); - } + ret = TTF_RenderUTF8_Blended(font->ttf_font, str, color); + } if (ret) return ret; @@ -1726,20 +1761,20 @@ static SDL_Surface *render_text(TuxPaint_Font * restrict font, const char *restr * @param str The wide-character string * @return The string, as Uint16 characters. */ -static Uint16 *wcstou16(const wchar_t * str) +static Uint16 *wcstou16(const wchar_t *str) { unsigned int i, len = wcslen(str); Uint16 *res = malloc((len + 1) * sizeof(Uint16)); for (i = 0; i < len + 1; ++i) - { - /* This is a bodge, but it seems unlikely that a case-conversion - will cause a change from one utf16 character into two.... - (though at least UTF-8 suffers from this problem) */ + { + /* This is a bodge, but it seems unlikely that a case-conversion + will cause a change from one utf16 character into two.... + (though at least UTF-8 suffers from this problem) */ - /* FIXME: mangles non-BMP characters rather than using UTF-16 surrogates! */ - res[i] = (Uint16) str[i]; - } + /* FIXME: mangles non-BMP characters rather than using UTF-16 surrogates! */ + res[i] = (Uint16) str[i]; + } return res; } @@ -1753,7 +1788,9 @@ static Uint16 *wcstou16(const wchar_t * str) * @param color The color to draw it in * @return A new surface, containing the rendered text */ -static SDL_Surface *render_text_w(TuxPaint_Font * restrict font, const wchar_t * restrict str, SDL_Color color) +static SDL_Surface *render_text_w(TuxPaint_Font * restrict font, + const wchar_t *restrict str, + SDL_Color color) { SDL_Surface *ret = NULL; int height; @@ -1768,29 +1805,29 @@ static SDL_Surface *render_text_w(TuxPaint_Font * restrict font, const wchar_t * #ifndef NO_SDLPANGO if (font->typ == FONT_TYPE_PANGO) - { - sdl_color_to_pango_color(color, &pango_color); + { + sdl_color_to_pango_color(color, &pango_color); - SDLPango_SetDefaultColor(font->pango_context, &pango_color); + SDLPango_SetDefaultColor(font->pango_context, &pango_color); /* Convert from 16-bit UNICODE to UTF-8 encoded for SDL_Pango: */ -utfstr_max = (sizeof(char) * 4 * (wcslen(str) + 1)); -utfstr = (char *)malloc(utfstr_max); + utfstr_max = (sizeof(char) * 4 * (wcslen(str) + 1)); + utfstr = (char *) malloc(utfstr_max); -wcstombs(utfstr, str, utfstr_max); + wcstombs(utfstr, str, utfstr_max); - SDLPango_SetText(font->pango_context, utfstr, -1); - ret = SDLPango_CreateSurfaceDraw(font->pango_context); - } + SDLPango_SetText(font->pango_context, utfstr, -1); + ret = SDLPango_CreateSurfaceDraw(font->pango_context); + } #endif if (font->typ == FONT_TYPE_TTF) - { - ustr = wcstou16(str); - ret = TTF_RenderUNICODE_Blended(font->ttf_font, ustr, color); - free(ustr); - } + { + ustr = wcstou16(str); + ret = TTF_RenderUNICODE_Blended(font->ttf_font, ustr, color); + free(ustr); + } if (ret) return ret; @@ -1860,7 +1897,7 @@ static int max_stamps[MAX_STAMP_GROUPS]; static stamp_type **stamp_data[MAX_STAMP_GROUPS]; static SDL_Surface *active_stamp; -static SDL_Surface * current_stamp_cached = NULL; +static SDL_Surface *current_stamp_cached = NULL; /** @@ -1898,8 +1935,8 @@ static SDL_Surface *img_shapes[NUM_SHAPES], *img_shape_names[NUM_SHAPES]; static SDL_Surface *img_fills[NUM_FILLS], *img_fill_names[NUM_FILLS]; static SDL_Surface *img_openlabels_open, *img_openlabels_erase, *img_openlabels_slideshow, *img_openlabels_back, *img_openlabels_play, - *img_openlabels_gif_export, *img_openlabels_pict_export, *img_openlabels_next, - *img_mixerlabel_clear; + *img_openlabels_gif_export, *img_openlabels_pict_export, + *img_openlabels_next, *img_mixerlabel_clear; static SDL_Surface *img_tux[NUM_TIP_TUX]; @@ -1943,9 +1980,9 @@ static int brush_counter, brush_frame; #define BRUSH_SPACING_SIZES 12 /* How many brush spacing options to provide (max will represent BRUSH_SPACING_MAX_MULTIPLIER times the max dimension of the brush; min will represent 1 pixel) */ -#define BRUSH_SPACING_MAX_MULTIPLIER 5 /* How far apart (in terms of a multiplier of a - brush's dimensions) the max brush spacing setting - represents */ +#define BRUSH_SPACING_MAX_MULTIPLIER 5 /* How far apart (in terms of a multiplier of a + brush's dimensions) the max brush spacing setting + represents */ static unsigned cur_color; static int cur_tool, cur_brush, old_tool; @@ -2001,7 +2038,8 @@ typedef enum #define NUM_EDGES 4 -static SDL_Event scrolltimer_selector_event, scrolltimer_tool_event, scrolltimer_dialog_event; +static SDL_Event scrolltimer_selector_event, scrolltimer_tool_event, + scrolltimer_dialog_event; int non_left_click_count = 0; @@ -2018,7 +2056,8 @@ SDL_Joystick *joystick; static void mainloop(void); static void brush_draw(int x1, int y1, int x2, int y2, int update); -static void blit_brush(int x, int y, int direction, double rotation, int * w, int * h); +static void blit_brush(int x, int y, int direction, double rotation, int *w, + int *h); static void stamp_draw(int x, int y, int stamp_angle_rotation); static void rec_undo_buffer(void); @@ -2027,7 +2066,8 @@ void show_usage(int exitcode); static char *progname; static SDL_Cursor *get_cursor(unsigned char *bits, unsigned char *mask_bits, - unsigned int w, unsigned int h, unsigned int x, unsigned int y); + unsigned int w, unsigned int h, unsigned int x, + unsigned int y); static void seticon(void); static SDL_Surface *loadimage(const char *const fname); static SDL_Surface *do_loadimage(const char *const fname, int abort_on_error); @@ -2080,12 +2120,16 @@ static void reset_avail_tools(void); static int compare_dirent2s(struct dirent2 *f1, struct dirent2 *f2); static int compare_dirent2s_invert(struct dirent2 *f1, struct dirent2 *f2); static void redraw_tux_text(void); -static void draw_tux_text(int which_tux, const char *const str, int want_right_to_left); -static void draw_tux_text_ex(int which_tux, const char *const str, int want_right_to_left, Uint8 locale_text); +static void draw_tux_text(int which_tux, const char *const str, + int want_right_to_left); +static void draw_tux_text_ex(int which_tux, const char *const str, + int want_right_to_left, Uint8 locale_text); static void draw_cur_tool_tip(void); -static void wordwrap_text(const char *const str, SDL_Color color, int left, int top, int right, int want_right_to_left); -static void wordwrap_text_ex(const char *const str, SDL_Color color, - int left, int top, int right, int want_right_to_left, Uint8 locale_text); +static void wordwrap_text(const char *const str, SDL_Color color, int left, + int top, int right, int want_right_to_left); +static void wordwrap_text_ex(const char *const str, SDL_Color color, int left, + int top, int right, int want_right_to_left, + Uint8 locale_text); static char *loaddesc(const char *const fname, Uint8 * locale_text); static double loadinfo(const char *const fname, stamp_type * inf); @@ -2100,20 +2144,25 @@ static void save_current(void); static int do_prompt_image_flash(const char *const text, const char *const btn_yes, const char *const btn_no, SDL_Surface * img1, - SDL_Surface * img2, SDL_Surface * img3, int animate, int ox, int oy); + SDL_Surface * img2, SDL_Surface * img3, + int animate, int ox, int oy); static int do_prompt_image_flash_snd(const char *const text, const char *const btn_yes, const char *const btn_no, SDL_Surface * img1, SDL_Surface * img2, - SDL_Surface * img3, int animate, int snd, int ox, int oy); + SDL_Surface * img3, int animate, int snd, + int ox, int oy); static int do_prompt_image(const char *const text, const char *const btn_yes, const char *const btn_no, SDL_Surface * img1, - SDL_Surface * img2, SDL_Surface * img3, int ox, int oy); + SDL_Surface * img2, SDL_Surface * img3, int ox, + int oy); static int do_prompt_image_snd(const char *const text, const char *const btn_yes, const char *const btn_no, SDL_Surface * img1, - SDL_Surface * img2, SDL_Surface * img3, int snd, int ox, int oy); -static int do_prompt(const char *const text, const char *const btn_yes, const char *const btn_no, int ox, int oy); + SDL_Surface * img2, SDL_Surface * img3, + int snd, int ox, int oy); +static int do_prompt(const char *const text, const char *const btn_yes, + const char *const btn_no, int ox, int oy); static int do_prompt_snd(const char *const text, const char *const btn_yes, const char *const btn_no, int snd, int ox, int oy); static void cleanup(void); @@ -2126,44 +2175,59 @@ static void do_shape(int sx, int sy, int nx, int ny, int rotn, int use_brush); static int shape_rotation(int ctr_x, int ctr_y, int ox, int oy); static int brush_rotation(int ctr_x, int ctr_y, int ox, int oy); static int do_save(int tool, int dont_show_success_results, int autosave); -static int do_png_save(FILE * fi, const char *const fname, SDL_Surface * surf, int embed); +static int do_png_save(FILE * fi, const char *const fname, SDL_Surface * surf, + int embed); static void load_embedded_data(char *fname, SDL_Surface * org_surf); static int chunk_is_valid(const char *chunk_name, png_unknown_chunk unknown); Bytef *get_chunk_data(FILE * fp, char *fname, png_structp png_ptr, - png_infop info_ptr, const char *chunk_name, png_unknown_chunk unknown, int *unc_size); + png_infop info_ptr, const char *chunk_name, + png_unknown_chunk unknown, int *unc_size); static void get_new_file_id(void); static int do_quit(int tool); static int do_open(void); static int do_new_dialog(void); -static int do_new_dialog_add_colors(SDL_Surface * *thumbs, int num_files, int *d_places, char * *d_names, +static int do_new_dialog_add_colors(SDL_Surface * *thumbs, int num_files, + int *d_places, char * *d_names, char * *d_exts, int *white_in_palette); static int do_color_picker(void); -static void draw_color_picker_crosshairs(int color_picker_left, int color_picker_top, int color_picker_val_left, int color_picker_val_top); +static void draw_color_picker_crosshairs(int color_picker_left, + int color_picker_top, + int color_picker_val_left, + int color_picker_val_top); static void set_color_picker_crosshair_size(void); static void draw_color_picker_values(int l, int t); -static void draw_color_picker_palette_and_values(int color_picker_left, int color_picker_top, int color_picker_val_left, int color_picker_val_top); +static void draw_color_picker_palette_and_values(int color_picker_left, + int color_picker_top, + int color_picker_val_left, + int color_picker_val_top); static void render_color_picker_palette(void); static int do_color_sel(int temp_mode); static int do_color_mix(void); static void draw_color_mixer_blank_example(void); -static void calc_color_mixer_average(float * out_h, float * out_s, float * out_v); +static void calc_color_mixer_average(float *out_h, float *out_s, + float *out_v); static void draw_color_mixer_tooltip(void); static void draw_color_mix_undo_redo(void); -static void render_color_button(int the_color, SDL_Surface * decoration, SDL_Surface * icon); +static void render_color_button(int the_color, SDL_Surface * decoration, + SDL_Surface * icon); static void handle_color_changed(void); static int do_slideshow(void); -static void play_slideshow(int *selected, int num_selected, char *dirname, char **d_names, char **d_exts, int speed); +static void play_slideshow(int *selected, int num_selected, char *dirname, + char **d_names, char **d_exts, int speed); static void draw_selection_digits(int right, int bottom, int n); -static int export_gif(int *selected, int num_selected, char *dirname, char **d_names, char **d_exts, int speed); +static int export_gif(int *selected, int num_selected, char *dirname, + char **d_names, char **d_exts, int speed); int export_gif_monitor_events(void); -static int export_pict(char * fname); -static char * get_export_filepath(const char * ext); +static int export_pict(char *fname); +static char *get_export_filepath(const char *ext); static void wait_for_sfx(void); -static void rgbtohsv(Uint8 r8, Uint8 g8, Uint8 b8, float *h, float *s, float *v); -static void hsvtorgb(float h, float s, float v, Uint8 * r8, Uint8 * g8, Uint8 * b8); +static void rgbtohsv(Uint8 r8, Uint8 g8, Uint8 b8, float *h, float *s, + float *v); +static void hsvtorgb(float h, float s, float v, Uint8 * r8, Uint8 * g8, + Uint8 * b8); static SDL_Surface *flip_surface(SDL_Surface * s); static SDL_Surface *mirror_surface(SDL_Surface * s); @@ -2173,7 +2237,7 @@ static void do_print(void); static void strip_trailing_whitespace(char *buf); static void do_render_cur_text(int do_blit); static char *uppercase(const char *restrict const str); -static wchar_t *uppercase_w(const wchar_t * restrict const str); +static wchar_t *uppercase_w(const wchar_t *restrict const str); static char *textdir(const char *const str); static SDL_Surface *do_render_button_label(const char *const label); static void create_button_labels(void); @@ -2181,16 +2245,20 @@ static Uint32 scrolltimer_selector_callback(Uint32 interval, void *param); static Uint32 scrolltimer_tool_callback(Uint32 interval, void *param); static Uint32 scrolltimer_dialog_callback(Uint32 interval, void *param); static Uint32 drawtext_callback(Uint32 interval, void *param); -static void control_drawtext_timer(Uint32 interval, const char *const text, Uint8 locale_text); +static void control_drawtext_timer(Uint32 interval, const char *const text, + Uint8 locale_text); static const char *great_str(void); static void draw_image_title(int t, SDL_Rect dest); -static void handle_keymouse(SDLKey key, Uint32 updown, int steps, SDL_Rect * area1, SDL_Rect * area2); -static void handle_keymouse_buttons(SDLKey key, int *whicht, int *whichc, SDL_Rect real_r_tools); +static void handle_keymouse(SDLKey key, Uint32 updown, int steps, + SDL_Rect * area1, SDL_Rect * area2); +static void handle_keymouse_buttons(SDLKey key, int *whicht, int *whichc, + SDL_Rect real_r_tools); static void handle_active(SDL_Event * event); /*static char *replace_tilde(const char* const path);*/ #ifdef NO_SDLPANGO -static void anti_carriage_return(int left, int right, int cur_top, int new_top, int cur_bot, int line_width); +static void anti_carriage_return(int left, int right, int cur_top, + int new_top, int cur_bot, int line_width); #endif static void load_starter_id(char *saved_id, FILE * fil); static void load_starter(char *img_id); @@ -2208,9 +2276,12 @@ Mix_Chunk *magic_current_snd_ptr; static void magic_playsound(Mix_Chunk * snd, int left_right, int up_down); static void magic_stopsound(void); static void magic_line_func(void *mapi, - int which, SDL_Surface * canvas, SDL_Surface * last, - int x1, int y1, int x2, int y2, int step, - void (*cb) (void *, int, SDL_Surface *, SDL_Surface *, int, int)); + int which, SDL_Surface * canvas, + SDL_Surface * last, int x1, int y1, int x2, + int y2, int step, void (*cb)(void *, int, + SDL_Surface *, + SDL_Surface *, int, + int)); static Uint8 magic_linear_to_sRGB(float lin); static float magic_sRGB_to_linear(Uint8 srgb); @@ -2232,7 +2303,8 @@ static SDL_Surface *load_kpx(const char *file); #ifndef NOSVG static SDL_Surface *load_svg(const char *file); -static float pick_best_scape(unsigned int orig_w, unsigned int orig_h, unsigned int max_w, unsigned int max_h); +static float pick_best_scape(unsigned int orig_w, unsigned int orig_h, + unsigned int max_w, unsigned int max_h); #endif static SDL_Surface *myIMG_Load_RWops(const char *file); static SDL_Surface *myIMG_Load(const char *file); @@ -2241,8 +2313,8 @@ int file_exists(char *path); int generate_fontconfig_cache_spinner(SDL_Surface * screen); -char * safe_strncat(char *dest, const char *src, size_t n); -char * safe_strncpy(char *dest, const char *src, size_t n); +char *safe_strncat(char *dest, const char *src, size_t n); +char *safe_strncpy(char *dest, const char *src, size_t n); int safe_snprintf(char *str, size_t size, const char *format, ...); #define MAX_UTF8_CHAR_LENGTH 6 @@ -2259,7 +2331,8 @@ static int bypass_splash_wait; #define NUM_CONFETTI 100 -typedef struct { +typedef struct +{ float x, y, xm, ym, ymm; Uint32 color; } confetti_t; @@ -2280,7 +2353,7 @@ static void do_wait(int counter) int done; #ifdef ANNIVERSARY int i; - SDL_Surface * back_surf; + SDL_Surface *back_surf; SDL_Rect r; #endif @@ -2290,7 +2363,8 @@ static void do_wait(int counter) done = 0; #ifdef ANNIVERSARY - for (i = 0; i < NUM_CONFETTI; i++) { + for (i = 0; i < NUM_CONFETTI; i++) + { confetti[i].x = rand() % screen->w; confetti[i].y = rand() % screen->h; confetti[i].xm = (rand() % 9) - 4; @@ -2298,8 +2372,7 @@ static void do_wait(int counter) confetti[i].ymm = ((rand() % 10) / 20) + 0.1; confetti[i].color = SDL_MapRGB(screen->format, (rand() % 128) + 96, - (rand() % 128) + 96, - (rand() % 128) + 96); + (rand() % 128) + 96, (rand() % 128) + 96); } back_surf = SDL_DisplayFormat(screen); @@ -2307,60 +2380,63 @@ static void do_wait(int counter) do + { + while (SDL_PollEvent(&event)) { - while (SDL_PollEvent(&event)) - { - if (event.type == SDL_QUIT) - { - done = 1; + if (event.type == SDL_QUIT) + { + done = 1; - /* FIXME: Handle SDL_Quit better */ - } - else if (event.type == SDL_WINDOWEVENT) - { - handle_active(&event); - } - else if (event.type == SDL_KEYDOWN) - { - done = 1; - } - else if (event.type == SDL_MOUSEBUTTONDOWN && valid_click(event.button.button)) - { - done = 1; - } - } + /* FIXME: Handle SDL_Quit better */ + } + else if (event.type == SDL_WINDOWEVENT) + { + handle_active(&event); + } + else if (event.type == SDL_KEYDOWN) + { + done = 1; + } + else if (event.type == SDL_MOUSEBUTTONDOWN + && valid_click(event.button.button)) + { + done = 1; + } + } #ifdef ANNIVERSARY - for (i = 0; i < NUM_CONFETTI; i++) { - r.x = confetti[i].x; - r.y = confetti[i].y; - r.w = 8; - r.h = 8; + for (i = 0; i < NUM_CONFETTI; i++) + { + r.x = confetti[i].x; + r.y = confetti[i].y; + r.w = 8; + r.h = 8; - SDL_BlitSurface(back_surf, &r, screen, &r); - } + SDL_BlitSurface(back_surf, &r, screen, &r); + } - for (i = 0; i < NUM_CONFETTI; i++) { - confetti[i].x += confetti[i].xm; - confetti[i].xm += (((rand() % 5) - 2) / 10); - confetti[i].y += confetti[i].ym; - confetti[i].ym += confetti[i].ymm; + for (i = 0; i < NUM_CONFETTI; i++) + { + confetti[i].x += confetti[i].xm; + confetti[i].xm += (((rand() % 5) - 2) / 10); + confetti[i].y += confetti[i].ym; + confetti[i].ym += confetti[i].ymm; - r.x = confetti[i].x; - r.y = confetti[i].y; - r.w = 6 + (rand() % 2); - r.h = (r.y % 8) + 1; + r.x = confetti[i].x; + r.y = confetti[i].y; + r.w = 6 + (rand() % 2); + r.h = (r.y % 8) + 1; - SDL_FillRect(screen, &r, confetti[i].color); - } + SDL_FillRect(screen, &r, confetti[i].color); + } - SDL_Flip(screen); + SDL_Flip(screen); #endif - counter--; - SDL_Delay(100); - } + counter--; + SDL_Delay(100); + } while (!done && counter > 0); #ifdef ANNIVERSARY @@ -2459,15 +2535,16 @@ on_screen_keyboard *new_kbd; SDL_Rect kbd_rect; #if (SDL_MAJOR_VERSION < 2) - #define TIMERID_NONE NULL +#define TIMERID_NONE NULL #else - #define TIMERID_NONE 0 +#define TIMERID_NONE 0 #endif -int brushflag, xnew, ynew, eraflag, lineflag, magicflag, keybd_flag, keybd_position, keyglobal, initial_y, gen_key_flag, - ide, activeflag, old_x, old_y; +int brushflag, xnew, ynew, eraflag, lineflag, magicflag, keybd_flag, + keybd_position, keyglobal, initial_y, gen_key_flag, ide, activeflag, old_x, + old_y; int cur_thing; -SDL_TimerID scrolltimer_dialog = TIMERID_NONE; /* Used by Open, Open->Slideshow, and New dialogs */ +SDL_TimerID scrolltimer_dialog = TIMERID_NONE; /* Used by Open, Open->Slideshow, and New dialogs */ Uint32 TP_SDL_MOUSEBUTTONSCROLL; /** @@ -2476,7 +2553,8 @@ Uint32 TP_SDL_MOUSEBUTTONSCROLL; static void mainloop(void) { int done, val_x, val_y, valhat_x, valhat_y, new_x, new_y, - shape_tool_mode, shape_start_x, shape_start_y, shape_current_x, shape_current_y, old_stamp_group, which; + shape_tool_mode, shape_start_x, shape_start_y, shape_current_x, + shape_current_y, old_stamp_group, which; int num_things; int *thing_scroll; int do_draw; @@ -2501,11 +2579,13 @@ static void mainloop(void) #endif TP_SDL_MOUSEBUTTONSCROLL = SDL_RegisterEvents(1); - SDL_TimerID scrolltimer_selector = TIMERID_NONE, scrolltimer_tool = TIMERID_NONE; + SDL_TimerID scrolltimer_selector = TIMERID_NONE, scrolltimer_tool = + TIMERID_NONE; SDL_Event event; SDLKey key; SDLMod mod; - Uint32 last_cursor_blink, cur_cursor_blink, pre_event_time, current_event_time; + Uint32 last_cursor_blink, cur_cursor_blink, pre_event_time, + current_event_time; SDL_Rect update_rect; SDL_Rect real_r_tools = r_tools; @@ -2515,8 +2595,8 @@ static void mainloop(void) #endif float angle; - char angle_tool_text[256]; // FIXME Consider malloc'ing - char stretch_tool_text[256]; // FIXME Consider malloc'ing + char angle_tool_text[256]; // FIXME Consider malloc'ing + char stretch_tool_text[256]; // FIXME Consider malloc'ing num_things = num_brushes; thing_scroll = &brush_scroll; @@ -2552,489 +2632,514 @@ static void mainloop(void) r_tir.h = 0; if (NUM_TOOLS > buttons_tall * gd_tools.cols) - { - real_r_tools.h = r_tools.h - button_h; - real_r_tools.y = r_tools.y + button_h / 2; - } + { + real_r_tools.h = r_tools.h - button_h; + real_r_tools.y = r_tools.y + button_h / 2; + } do + { + ignoring_motion = 0; + + pre_event_time = SDL_GetTicks(); + + + while (SDL_PollEvent(&event)) { - ignoring_motion = 0; + current_event_time = SDL_GetTicks(); - pre_event_time = SDL_GetTicks(); - - - while (SDL_PollEvent(&event)) + /* To avoid getting stuck in a 'catching up with mouse motion' interface lock-up */ + /* FIXME: Another thing we could do here is peek into events, and 'skip' to the last motion...? Or something... -bjk 2011.04.26 */ + if (current_event_time > pre_event_time + 500 + && event.type == SDL_MOUSEMOTION) + { + if (cur_tool == TOOL_STAMP + && stamp_tool_mode == STAMP_TOOL_MODE_ROTATE) + /* Discarding old stamp XORs, don't need to keep any outdated mouse motion event */ { - current_event_time = SDL_GetTicks(); + int rest = + SDL_PeepEvents(NULL, 1000, SDL_PEEKEVENT, SDL_MOUSEMOTION, + SDL_MOUSEMOTION); + for (int i = 0; i < rest; i++) + { + SDL_PollEvent(&event); + if (event.type != SDL_MOUSEMOTION) /* But keep any other events that could be there */ + break; + } + } + else + ignoring_motion = (ignoring_motion + 1) % 3; /* Ignore every couple of motion events, to keep things moving quickly (but avoid, e.g., attempts to draw "O" from looking like "D") */ + } - /* To avoid getting stuck in a 'catching up with mouse motion' interface lock-up */ - /* FIXME: Another thing we could do here is peek into events, and 'skip' to the last motion...? Or something... -bjk 2011.04.26 */ - if (current_event_time > pre_event_time + 500 && event.type == SDL_MOUSEMOTION) + if (event.type == SDL_QUIT) + { + magic_switchout(canvas); + done = do_quit(cur_tool); + if (!done) + { + magic_switchin(canvas); + + if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + { + if (onscreen_keyboard && kbd) { - if (cur_tool == TOOL_STAMP && stamp_tool_mode == STAMP_TOOL_MODE_ROTATE) - /* Discarding old stamp XORs, don't need to keep any outdated mouse motion event */ - { - int rest = SDL_PeepEvents(NULL, 1000, SDL_PEEKEVENT, SDL_MOUSEMOTION, SDL_MOUSEMOTION); - for (int i = 0; i < rest; i++) - { - SDL_PollEvent(&event); - if (event.type != SDL_MOUSEMOTION) /* But keep any other events that could be there */ - break; - } - } - else - ignoring_motion = (ignoring_motion + 1) % 3; /* Ignore every couple of motion events, to keep things moving quickly (but avoid, e.g., attempts to draw "O" from looking like "D") */ + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); + update_screen_rect(&kbd_rect); } - if (event.type == SDL_QUIT) + if (onscreen_keyboard && !kbd) { - magic_switchout(canvas); - done = do_quit(cur_tool); - if (!done) - { - magic_switchin(canvas); - - if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - { - if (onscreen_keyboard && kbd) - { - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); - } - - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput(); - } - } - } + SDL_StartTextInput(); } + } + } + } #ifdef AUTOSAVE_GOING_BACKGROUND - else if (event.type == SDL_APP_DIDENTERBACKGROUND) - { - /* Triggers a save to a temporary file in case the app is killed later. - That file should be deleted if tuxpaint continues without being killed but it contains unsaved data - so it must be used as the next start drawing if tuxpaint is killed. - Also a reference to the actual file being drawn should be kept and restored. */ - if (!been_saved) - { - do_save(cur_tool, 0, 1); - save_current(); - } - } - else if (event.type == SDL_APP_DIDENTERFOREGROUND) - { - /* Discard the temp file saved before as the user takes again control */ - snprintf(tmp, sizeof(tmp), "saved/%s%s", AUTOSAVED_NAME, FNAME_EXTENSION); - fname = get_fname(tmp, DIR_SAVE); - fi = fopen(fname, "wb"); - if (fi != NULL) - { - fclose(fi); - unlink(fname); - free(fname); - } - redraw_tux_text(); - } + else if (event.type == SDL_APP_DIDENTERBACKGROUND) + { + /* Triggers a save to a temporary file in case the app is killed later. + That file should be deleted if tuxpaint continues without being killed but it contains unsaved data + so it must be used as the next start drawing if tuxpaint is killed. + Also a reference to the actual file being drawn should be kept and restored. */ + if (!been_saved) + { + do_save(cur_tool, 0, 1); + save_current(); + } + } + else if (event.type == SDL_APP_DIDENTERFOREGROUND) + { + /* Discard the temp file saved before as the user takes again control */ + snprintf(tmp, sizeof(tmp), "saved/%s%s", AUTOSAVED_NAME, + FNAME_EXTENSION); + fname = get_fname(tmp, DIR_SAVE); + fi = fopen(fname, "wb"); + if (fi != NULL) + { + fclose(fi); + unlink(fname); + free(fname); + } + redraw_tux_text(); + } #endif - else if (event.type == SDL_WINDOWEVENT) - { - /* Reset Shapes tool and clean the canvas if we lose focus */ - if (mouseaccessibility && emulate_button_pressed && - ((cur_tool == TOOL_SHAPES && shape_tool_mode != SHAPE_TOOL_MODE_DONE) || cur_tool == TOOL_LINES) && - event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) - /* event.active.state & (SDL_APPINPUTFOCUS|SDL_APPACTIVE) && - event.active.gain == 0) */ - { - do_undo(); - tool_avail[TOOL_REDO] = 0; /* Don't let them 'redo' to get preview back */ - draw_toolbar(); - update_screen_rect(&r_tools); - shape_tool_mode = SHAPE_TOOL_MODE_DONE; - } - handle_active(&event); - } - else if (event.type == SDL_KEYUP) - { - key = event.key.keysym.sym; - handle_keymouse(key, SDL_KEYUP, 16, NULL, NULL); - } - else if (event.type == SDL_KEYDOWN || event.type == SDL_TEXTINPUT) - { - key = event.key.keysym.sym; - mod = event.key.keysym.mod; + else if (event.type == SDL_WINDOWEVENT) + { + /* Reset Shapes tool and clean the canvas if we lose focus */ + if (mouseaccessibility && emulate_button_pressed && + ((cur_tool == TOOL_SHAPES + && shape_tool_mode != SHAPE_TOOL_MODE_DONE) + || cur_tool == TOOL_LINES) + && event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) + /* event.active.state & (SDL_APPINPUTFOCUS|SDL_APPACTIVE) && + event.active.gain == 0) */ + { + do_undo(); + tool_avail[TOOL_REDO] = 0; /* Don't let them 'redo' to get preview back */ + draw_toolbar(); + update_screen_rect(&r_tools); + shape_tool_mode = SHAPE_TOOL_MODE_DONE; + } + handle_active(&event); + } + else if (event.type == SDL_KEYUP) + { + key = event.key.keysym.sym; + handle_keymouse(key, SDL_KEYUP, 16, NULL, NULL); + } + else if (event.type == SDL_KEYDOWN || event.type == SDL_TEXTINPUT) + { + key = event.key.keysym.sym; + mod = event.key.keysym.mod; #ifdef DEBUG /* FIXME: debug junk */ - fprintf(stderr, - "key 0x%04x mod 0x%04x character 0x%04x %d <%c> is %sprintable, key_down 0x%x\n", - (unsigned)key, - (unsigned)mod, - (unsigned)event.text.text, - (int)event.text.text, - (key_unicode > ' ' && key_unicode < 127) ? (char)event.text.text : ' ', - iswprint(key_unicode) ? "" : "not ", (unsigned)key_down); + fprintf(stderr, + "key 0x%04x mod 0x%04x character 0x%04x %d <%c> is %sprintable, key_down 0x%x\n", + (unsigned) key, + (unsigned) mod, + (unsigned) event.text.text, + (int) event.text.text, + (key_unicode > ' ' + && key_unicode < 127) ? (char) event.text.text : ' ', + iswprint(key_unicode) ? "" : "not ", (unsigned) key_down); #endif - if (cur_tool == TOOL_STAMP) - { - SDL_Rect r_stamps_sizesel; + if (cur_tool == TOOL_STAMP) + { + SDL_Rect r_stamps_sizesel; - r_stamps_sizesel.x = r_canvas.x + r_canvas.w; - r_stamps_sizesel.y = r_canvas.h - img_btn_up->h; - r_stamps_sizesel.w = img_btn_up->w * 2; - r_stamps_sizesel.h = img_btn_up->h; - handle_keymouse(key, SDL_KEYDOWN, 16, &r_canvas, &r_stamps_sizesel); - } - else - handle_keymouse(key, SDL_KEYDOWN, 16, &r_canvas, NULL); + r_stamps_sizesel.x = r_canvas.x + r_canvas.w; + r_stamps_sizesel.y = r_canvas.h - img_btn_up->h; + r_stamps_sizesel.w = img_btn_up->w * 2; + r_stamps_sizesel.h = img_btn_up->h; + handle_keymouse(key, SDL_KEYDOWN, 16, &r_canvas, &r_stamps_sizesel); + } + else + handle_keymouse(key, SDL_KEYDOWN, 16, &r_canvas, NULL); - /* handle_keymouse_buttons will move one button at a time */ - handle_keymouse_buttons(key, &whicht, &whichc, real_r_tools); + /* handle_keymouse_buttons will move one button at a time */ + handle_keymouse_buttons(key, &whicht, &whichc, real_r_tools); - if ((key == SDLK_ESCAPE || key == SDLK_AC_BACK) && !disable_quit) - { - magic_switchout(canvas); - done = do_quit(cur_tool); - if (!done) - { - magic_switchin(canvas); - reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, &stamp_tool_mode); + if ((key == SDLK_ESCAPE || key == SDLK_AC_BACK) && !disable_quit) + { + magic_switchout(canvas); + done = do_quit(cur_tool); + if (!done) + { + magic_switchin(canvas); + reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, + &stamp_tool_mode); - if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - { - if (onscreen_keyboard && kbd) - { - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); - } + if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + { + if (onscreen_keyboard && kbd) + { + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); + update_screen_rect(&kbd_rect); + } - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput(); - } - } - } - } - else if (key == SDLK_s && (mod & KMOD_ALT)) - { + if (onscreen_keyboard && !kbd) + { + SDL_StartTextInput(); + } + } + } + } + else if (key == SDLK_s && (mod & KMOD_ALT)) + { #ifndef NOSOUND - if (use_sound) - { + if (use_sound) + { #ifdef DEBUG - printf("modstate at mainloop %d, mod %d\n", SDL_GetModState(), mod); + printf("modstate at mainloop %d, mod %d\n", SDL_GetModState(), + mod); #endif - mute = !mute; - Mix_HaltChannel(-1); + mute = !mute; + Mix_HaltChannel(-1); - if (mute) - { - /* Sound has been muted (silenced) via keyboard shortcut */ - draw_tux_text(TUX_BORED, gettext("Sound muted."), 0); - } - else - { - /* Sound has been unmuted (unsilenced) via keyboard shortcut */ - draw_tux_text(TUX_BORED, gettext("Sound unmuted."), 0); - } - } + if (mute) + { + /* Sound has been muted (silenced) via keyboard shortcut */ + draw_tux_text(TUX_BORED, gettext("Sound muted."), 0); + } + else + { + /* Sound has been unmuted (unsilenced) via keyboard shortcut */ + draw_tux_text(TUX_BORED, gettext("Sound unmuted."), 0); + } + } #endif - } - else if ((key == SDLK_ESCAPE || key == SDLK_AC_BACK) && (mod & KMOD_SHIFT) && (mod & KMOD_CTRL)) - { - magic_switchout(canvas); - done = do_quit(cur_tool); - if (!done) - magic_switchin(canvas); - } + } + else if ((key == SDLK_ESCAPE || key == SDLK_AC_BACK) + && (mod & KMOD_SHIFT) && (mod & KMOD_CTRL)) + { + magic_switchout(canvas); + done = do_quit(cur_tool); + if (!done) + magic_switchin(canvas); + } #ifdef WIN32 - else if (key == SDLK_F4 && (mod & KMOD_ALT)) - { - magic_switchout(canvas); - done = do_quit(cur_tool); - if (!done) - magic_switchin(canvas); - } + else if (key == SDLK_F4 && (mod & KMOD_ALT)) + { + magic_switchout(canvas); + done = do_quit(cur_tool); + if (!done) + magic_switchin(canvas); + } #endif - else if (key == SDLK_z && (mod & KMOD_CTRL) && !noshortcuts) - { - /* Ctrl-Z - Undo */ + else if (key == SDLK_z && (mod & KMOD_CTRL) && !noshortcuts) + { + /* Ctrl-Z - Undo */ - magic_switchout(canvas); + magic_switchout(canvas); - if (tool_avail[TOOL_UNDO]) - { - if (cursor_x != -1 && cursor_y != -1) - { - hide_blinking_cursor(); - if (texttool_len > 0) - { - rec_undo_buffer(); - do_render_cur_text(1); - texttool_len = 0; - cursor_textwidth = 0; - label_node_to_edit = NULL; - } - else if (cur_tool == TOOL_LABEL && label_node_to_edit) - { - rec_undo_buffer(); - have_to_rec_label_node = TRUE; - add_label_node(0, 0, 0, 0, NULL); - derender_node(&label_node_to_edit); - label_node_to_edit = NULL; - } - } - if (cur_tool == TOOL_STAMP) - reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, &stamp_tool_mode); + if (tool_avail[TOOL_UNDO]) + { + if (cursor_x != -1 && cursor_y != -1) + { + hide_blinking_cursor(); + if (texttool_len > 0) + { + rec_undo_buffer(); + do_render_cur_text(1); + texttool_len = 0; + cursor_textwidth = 0; + label_node_to_edit = NULL; + } + else if (cur_tool == TOOL_LABEL && label_node_to_edit) + { + rec_undo_buffer(); + have_to_rec_label_node = TRUE; + add_label_node(0, 0, 0, 0, NULL); + derender_node(&label_node_to_edit); + label_node_to_edit = NULL; + } + } + if (cur_tool == TOOL_STAMP) + reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, + &stamp_tool_mode); - if (cur_undo == newest_undo) - { - rec_undo_buffer(); - do_undo(); - } - do_undo(); - update_screen_rect(&r_tools); - shape_tool_mode = SHAPE_TOOL_MODE_DONE; - } + if (cur_undo == newest_undo) + { + rec_undo_buffer(); + do_undo(); + } + do_undo(); + update_screen_rect(&r_tools); + shape_tool_mode = SHAPE_TOOL_MODE_DONE; + } - magic_switchin(canvas); - } - else if (key == SDLK_r && (mod & KMOD_CTRL) && !noshortcuts) - { - /* Ctrl-R - Redo */ + magic_switchin(canvas); + } + else if (key == SDLK_r && (mod & KMOD_CTRL) && !noshortcuts) + { + /* Ctrl-R - Redo */ - magic_switchout(canvas); + magic_switchout(canvas); - if (tool_avail[TOOL_REDO]) - { - if (cur_tool == TOOL_STAMP) - reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, &stamp_tool_mode); - hide_blinking_cursor(); - do_redo(); - update_screen_rect(&r_tools); - shape_tool_mode = SHAPE_TOOL_MODE_DONE; - } + if (tool_avail[TOOL_REDO]) + { + if (cur_tool == TOOL_STAMP) + reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, + &stamp_tool_mode); + hide_blinking_cursor(); + do_redo(); + update_screen_rect(&r_tools); + shape_tool_mode = SHAPE_TOOL_MODE_DONE; + } - magic_switchin(canvas); - } - else if (key == SDLK_o && (mod & KMOD_CTRL) && !noshortcuts) - { - /* Ctrl-O - Open */ + magic_switchin(canvas); + } + else if (key == SDLK_o && (mod & KMOD_CTRL) && !noshortcuts) + { + /* Ctrl-O - Open */ - magic_switchout(canvas); - if (cur_tool == TOOL_STAMP) - reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, &stamp_tool_mode); + magic_switchout(canvas); + if (cur_tool == TOOL_STAMP) + reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, + &stamp_tool_mode); - disable_avail_tools(); - draw_toolbar(); - draw_colors(COLORSEL_CLOBBER_WIPE); - draw_none(); + disable_avail_tools(); + draw_toolbar(); + draw_colors(COLORSEL_CLOBBER_WIPE); + draw_none(); - if (do_open() == 0) - { - if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - do_render_cur_text(0); - } + if (do_open() == 0) + { + if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + do_render_cur_text(0); + } - enable_avail_tools(); + enable_avail_tools(); - draw_toolbar(); - update_screen_rect(&r_tools); - draw_colors(COLORSEL_REFRESH); + draw_toolbar(); + update_screen_rect(&r_tools); + draw_colors(COLORSEL_REFRESH); - if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) - draw_brushes(); - else if (cur_tool == TOOL_MAGIC) - draw_magic(); - else if (cur_tool == TOOL_STAMP) - draw_stamps(); - else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - { - draw_fonts(); - if (onscreen_keyboard && kbd) - { - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); - } + if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) + draw_brushes(); + else if (cur_tool == TOOL_MAGIC) + draw_magic(); + else if (cur_tool == TOOL_STAMP) + draw_stamps(); + else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + { + draw_fonts(); + if (onscreen_keyboard && kbd) + { + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); + update_screen_rect(&kbd_rect); + } - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput(); - } - } - else if (cur_tool == TOOL_SHAPES) - draw_shapes(); - else if (cur_tool == TOOL_ERASER) - draw_erasers(); - else if (cur_tool == TOOL_FILL) - draw_fills(); + if (onscreen_keyboard && !kbd) + { + SDL_StartTextInput(); + } + } + else if (cur_tool == TOOL_SHAPES) + draw_shapes(); + else if (cur_tool == TOOL_ERASER) + draw_erasers(); + else if (cur_tool == TOOL_FILL) + draw_fills(); - draw_cur_tool_tip(); + draw_cur_tool_tip(); - /* FIXME: Make delay configurable: */ - control_drawtext_timer(1000, tool_tips[cur_tool], 0); - /* FIXME: May need to use draw_cur_tool_tip() here? -bjk 2021.09.06 */ + /* FIXME: Make delay configurable: */ + control_drawtext_timer(1000, tool_tips[cur_tool], 0); + /* FIXME: May need to use draw_cur_tool_tip() here? -bjk 2021.09.06 */ - magic_switchin(canvas); - } - else if ((key == SDLK_n && (mod & KMOD_CTRL)) && !noshortcuts) - { - /* Ctrl-N - New */ + magic_switchin(canvas); + } + else if ((key == SDLK_n && (mod & KMOD_CTRL)) && !noshortcuts) + { + /* Ctrl-N - New */ - magic_switchout(canvas); - if (cur_tool == TOOL_STAMP) - reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, &stamp_tool_mode); + magic_switchout(canvas); + if (cur_tool == TOOL_STAMP) + reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, + &stamp_tool_mode); - hide_blinking_cursor(); - shape_tool_mode = SHAPE_TOOL_MODE_DONE; + hide_blinking_cursor(); + shape_tool_mode = SHAPE_TOOL_MODE_DONE; - disable_avail_tools(); - draw_toolbar(); - draw_colors(COLORSEL_CLOBBER_WIPE); - draw_none(); + disable_avail_tools(); + draw_toolbar(); + draw_colors(COLORSEL_CLOBBER_WIPE); + draw_none(); - if (do_new_dialog() == 0) - { - draw_tux_text(tool_tux[TUX_DEFAULT], TIP_NEW_ABORT, 1); + if (do_new_dialog() == 0) + { + draw_tux_text(tool_tux[TUX_DEFAULT], TIP_NEW_ABORT, 1); - if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - do_render_cur_text(0); - } + if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + do_render_cur_text(0); + } - enable_avail_tools(); + enable_avail_tools(); - draw_toolbar(); - update_screen_rect(&r_tools); - draw_colors(COLORSEL_REFRESH); + draw_toolbar(); + update_screen_rect(&r_tools); + draw_colors(COLORSEL_REFRESH); - if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) - draw_brushes(); - else if (cur_tool == TOOL_MAGIC) - draw_magic(); - else if (cur_tool == TOOL_STAMP) - draw_stamps(); - else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - { - draw_fonts(); - if (onscreen_keyboard && kbd) - { - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); - } + if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) + draw_brushes(); + else if (cur_tool == TOOL_MAGIC) + draw_magic(); + else if (cur_tool == TOOL_STAMP) + draw_stamps(); + else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + { + draw_fonts(); + if (onscreen_keyboard && kbd) + { + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); + update_screen_rect(&kbd_rect); + } - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput(); - } - } - else if (cur_tool == TOOL_SHAPES) - draw_shapes(); - else if (cur_tool == TOOL_ERASER) - draw_erasers(); - else if (cur_tool == TOOL_FILL) - draw_fills(); + if (onscreen_keyboard && !kbd) + { + SDL_StartTextInput(); + } + } + else if (cur_tool == TOOL_SHAPES) + draw_shapes(); + else if (cur_tool == TOOL_ERASER) + draw_erasers(); + else if (cur_tool == TOOL_FILL) + draw_fills(); - update_screen_rect(&r_toolopt); - update_screen_rect(&r_ttoolopt); - magic_switchin(canvas); - } - else if (key == SDLK_s && (mod & KMOD_CTRL) && !noshortcuts) - { - /* Ctrl-S - Save */ + update_screen_rect(&r_toolopt); + update_screen_rect(&r_ttoolopt); + magic_switchin(canvas); + } + else if (key == SDLK_s && (mod & KMOD_CTRL) && !noshortcuts) + { + /* Ctrl-S - Save */ - magic_switchout(canvas); - hide_blinking_cursor(); + magic_switchout(canvas); + hide_blinking_cursor(); - /* Only reset stamp XORs if there will be prompt */ - if (cur_tool == TOOL_STAMP && promptless_save == SAVE_OVER_PROMPT && file_id[0] != '\0') - reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, &stamp_tool_mode); + /* Only reset stamp XORs if there will be prompt */ + if (cur_tool == TOOL_STAMP && promptless_save == SAVE_OVER_PROMPT + && file_id[0] != '\0') + reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, + &stamp_tool_mode); - if (do_save(cur_tool, 0, 0)) - { - /* Only think it's been saved if it HAS been saved :^) */ + if (do_save(cur_tool, 0, 0)) + { + /* Only think it's been saved if it HAS been saved :^) */ - been_saved = 1; - tool_avail[TOOL_SAVE] = 0; - } + been_saved = 1; + tool_avail[TOOL_SAVE] = 0; + } - draw_toolbar(); - update_screen_rect(&r_tools); - if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - { - if (onscreen_keyboard && kbd) - { - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); - } + draw_toolbar(); + update_screen_rect(&r_tools); + if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + { + if (onscreen_keyboard && kbd) + { + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); + update_screen_rect(&kbd_rect); + } - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput(); - } - } + if (onscreen_keyboard && !kbd) + { + SDL_StartTextInput(); + } + } - magic_switchin(canvas); - } + magic_switchin(canvas); + } #ifdef __APPLE__ - else if (key == SDLK_p && (mod & KMOD_CTRL) && (mod & KMOD_SHIFT) && !noshortcuts) - { - /* Ctrl-Shft-P - Page Setup */ - if (!disable_print) - DisplayPageSetup(canvas); - } + else if (key == SDLK_p && (mod & KMOD_CTRL) && (mod & KMOD_SHIFT) + && !noshortcuts) + { + /* Ctrl-Shft-P - Page Setup */ + if (!disable_print) + DisplayPageSetup(canvas); + } #endif - else if (key == SDLK_p && (mod & KMOD_CTRL) && !noshortcuts) - { - /* Ctrl-P - Print */ + else if (key == SDLK_p && (mod & KMOD_CTRL) && !noshortcuts) + { + /* Ctrl-P - Print */ - if (!disable_print) - { - magic_switchout(canvas); - if (cur_tool == TOOL_STAMP) - reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, &stamp_tool_mode); + if (!disable_print) + { + magic_switchout(canvas); + if (cur_tool == TOOL_STAMP) + reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, + &stamp_tool_mode); - /* If they haven't hit [Enter], but clicked 'Print', add their text now -bjk 2007.10.25 */ + /* If they haven't hit [Enter], but clicked 'Print', add their text now -bjk 2007.10.25 */ - tmp_apply_uncommited_text(); - print_image(); - undo_tmp_applied_text(); - magic_switchin(canvas); + tmp_apply_uncommited_text(); + print_image(); + undo_tmp_applied_text(); + magic_switchin(canvas); - if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - { - if (onscreen_keyboard && kbd) - { - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); - } + if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + { + if (onscreen_keyboard && kbd) + { + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); + update_screen_rect(&kbd_rect); + } - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput(); - } - } + if (onscreen_keyboard && !kbd) + { + SDL_StartTextInput(); + } + } - draw_toolbar(); - draw_tux_text(TUX_BORED, "", 0); - update_screen_rect(&r_tools); - } - } - else if (event.type == SDL_TEXTINPUT || - (event.type == SDL_KEYDOWN && - (event.key.keysym.sym == SDLK_BACKSPACE || - event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_TAB || - event.key.keysym.sym == SDLK_LALT || event.key.keysym.sym == SDLK_RALT))) - { - /* Handle key in text tool: */ + draw_toolbar(); + draw_tux_text(TUX_BORED, "", 0); + update_screen_rect(&r_tools); + } + } + else if (event.type == SDL_TEXTINPUT || + (event.type == SDL_KEYDOWN && + (event.key.keysym.sym == SDLK_BACKSPACE || + event.key.keysym.sym == SDLK_RETURN + || event.key.keysym.sym == SDLK_TAB + || event.key.keysym.sym == SDLK_LALT + || event.key.keysym.sym == SDLK_RALT))) + { + /* Handle key in text tool: */ - if (((cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) && cursor_x != -1 && cursor_y != -1) || - (cur_tool == TOOL_LABEL && (cur_label == LABEL_SELECT || cur_label == LABEL_APPLY))) - { - static int redraw = 0; - wchar_t *im_cp = im_data.s; + if (((cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + && cursor_x != -1 && cursor_y != -1) || (cur_tool == TOOL_LABEL + && (cur_label == + LABEL_SELECT + || cur_label == + LABEL_APPLY))) + { + static int redraw = 0; + wchar_t *im_cp = im_data.s; #ifdef DEBUG // key_down = key; @@ -3049,3680 +3154,4012 @@ static void mainloop(void) // (unsigned)key_down // ); #if 0 - /* this doesn't work for some reason */ - wprintf(L"character 0x%04x %d <%lc> is %d pixels, %lsprintable, key_down 0x%x\n", - event.key.keysym.unicode, - event.key.keysym.unicode, - (key_unicode > L' ') ? event.key.keysym.unicode : L' ', - charsize(event.key.keysym.unicode), iswprint(key_unicode) ? L"" : L"not ", key_down); + /* this doesn't work for some reason */ + wprintf + (L"character 0x%04x %d <%lc> is %d pixels, %lsprintable, key_down 0x%x\n", + event.key.keysym.unicode, event.key.keysym.unicode, + (key_unicode > L' ') ? event.key.keysym.unicode : L' ', + charsize(event.key.keysym.unicode), + iswprint(key_unicode) ? L"" : L"not ", key_down); #endif #endif - /* Set the text input rectangle for system onscreen keyboards */ - if (onscreen_keyboard && !kbd) - { - r_tir.y = (float)cursor_y / render_scale; - r_tir.x = (float)cursor_x / render_scale; - SDL_SetTextInputRect(&r_tir); - SDL_StartTextInput(); - } + /* Set the text input rectangle for system onscreen keyboards */ + if (onscreen_keyboard && !kbd) + { + r_tir.y = (float) cursor_y / render_scale; + r_tir.x = (float) cursor_x / render_scale; + SDL_SetTextInputRect(&r_tir); + SDL_StartTextInput(); + } - /* Discard previous # of redraw characters */ - if ((int)texttool_len <= redraw) - texttool_len = 0; - else - texttool_len -= redraw; - texttool_str[texttool_len] = L'\0'; + /* Discard previous # of redraw characters */ + if ((int) texttool_len <= redraw) + texttool_len = 0; + else + texttool_len -= redraw; + texttool_str[texttool_len] = L'\0'; - /* Read IM, remember how many to redraw next iteration */ - redraw = im_read(&im_data, event); + /* Read IM, remember how many to redraw next iteration */ + redraw = im_read(&im_data, event); - /* Korean Hangul needs this to refresh when buffered chars gets emptied */ - if (!*im_cp) - do_render_cur_text(0); + /* Korean Hangul needs this to refresh when buffered chars gets emptied */ + if (!*im_cp) + do_render_cur_text(0); - /* Queue each character to be displayed */ - while (*im_cp) - { + /* Queue each character to be displayed */ + while (*im_cp) + { #ifdef __APPLE__ - /* Apple uses DEL for BACKSPACE */ - if (*im_cp == SDLK_DELETE) *im_cp = L'\b'; + /* Apple uses DEL for BACKSPACE */ + if (*im_cp == SDLK_DELETE) + *im_cp = L'\b'; #endif - if (*im_cp == L'\b') - { - /* [Backspace] */ - hide_blinking_cursor(); - if (texttool_len > 0) - { - texttool_len--; - texttool_str[texttool_len] = L'\0'; - playsound(screen, 0, SND_KEYCLICK, 0, SNDPOS_CENTER, SNDDIST_NEAR); + if (*im_cp == L'\b') + { + /* [Backspace] */ + hide_blinking_cursor(); + if (texttool_len > 0) + { + texttool_len--; + texttool_str[texttool_len] = L'\0'; + playsound(screen, 0, SND_KEYCLICK, 0, SNDPOS_CENTER, + SNDDIST_NEAR); - do_render_cur_text(0); + do_render_cur_text(0); - if (been_saved) - { - been_saved = 0; + if (been_saved) + { + been_saved = 0; - if (!disable_save) - tool_avail[TOOL_SAVE] = 1; + if (!disable_save) + tool_avail[TOOL_SAVE] = 1; - draw_toolbar(); - update_screen_rect(&r_tools); - } + draw_toolbar(); + update_screen_rect(&r_tools); + } - } - } - else if (*im_cp == L'\r') - { - /* [Enter]... */ + } + } + else if (*im_cp == L'\r') + { + /* [Enter]... */ - int font_height; + int font_height; - font_height = TuxPaint_Font_FontHeight(getfonthandle(cur_font)); + font_height = + TuxPaint_Font_FontHeight(getfonthandle(cur_font)); - hide_blinking_cursor(); - if (texttool_len > 0) - { - /* [Enter] to finish entering text */ + hide_blinking_cursor(); + if (texttool_len > 0) + { + /* [Enter] to finish entering text */ - rec_undo_buffer(); - do_render_cur_text(1); - label_node_to_edit = NULL; - texttool_len = 0; - cursor_textwidth = 0; - if (cur_tool == TOOL_LABEL) - { - draw_fonts(); - update_screen_rect(&r_toolopt); - } + rec_undo_buffer(); + do_render_cur_text(1); + label_node_to_edit = NULL; + texttool_len = 0; + cursor_textwidth = 0; + if (cur_tool == TOOL_LABEL) + { + draw_fonts(); + update_screen_rect(&r_toolopt); + } - if (been_saved) - { - been_saved = 0; + if (been_saved) + { + been_saved = 0; - if (!disable_save) - tool_avail[TOOL_SAVE] = 1; + if (!disable_save) + tool_avail[TOOL_SAVE] = 1; - draw_toolbar(); - update_screen_rect(&r_tools); - } + draw_toolbar(); + update_screen_rect(&r_tools); + } - cursor_x = cursor_left; - cursor_y = min(cursor_y + font_height, canvas->h - font_height); + cursor_x = cursor_left; + cursor_y = + min(cursor_y + font_height, canvas->h - font_height); - /* Reposition the on-screen keyboard if we begin typing over it */ - update_canvas_ex(kbd_rect.x, kbd_rect.y, kbd_rect.x + kbd_rect.w, - kbd_rect.y + kbd_rect.h, 0); - update_screen_rect(&kbd_rect); - reposition_onscreen_keyboard(cursor_y); + /* Reposition the on-screen keyboard if we begin typing over it */ + update_canvas_ex(kbd_rect.x, kbd_rect.y, + kbd_rect.x + kbd_rect.w, + kbd_rect.y + kbd_rect.h, 0); + update_screen_rect(&kbd_rect); + reposition_onscreen_keyboard(cursor_y); - playsound(screen, 0, SND_RETURN, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - } - else if (cur_tool == TOOL_LABEL && label_node_to_edit) - { - /* [Enter] to finish erasing text from a pre-existing Label */ + playsound(screen, 0, SND_RETURN, 1, SNDPOS_RIGHT, + SNDDIST_NEAR); + } + else if (cur_tool == TOOL_LABEL && label_node_to_edit) + { + /* [Enter] to finish erasing text from a pre-existing Label */ - rec_undo_buffer(); - have_to_rec_label_node = TRUE; - add_label_node(0, 0, 0, 0, NULL); - derender_node(&label_node_to_edit); - label_node_to_edit = NULL; + rec_undo_buffer(); + have_to_rec_label_node = TRUE; + add_label_node(0, 0, 0, 0, NULL); + derender_node(&label_node_to_edit); + label_node_to_edit = NULL; - playsound(screen, 0, SND_LINE_END, 0, SNDPOS_CENTER, SNDDIST_NEAR); + playsound(screen, 0, SND_LINE_END, 0, SNDPOS_CENTER, + SNDDIST_NEAR); - if (been_saved) - { - been_saved = 0; + if (been_saved) + { + been_saved = 0; - if (!disable_save) - tool_avail[TOOL_SAVE] = 1; + if (!disable_save) + tool_avail[TOOL_SAVE] = 1; - draw_toolbar(); - update_screen_rect(&r_tools); - } - } + draw_toolbar(); + update_screen_rect(&r_tools); + } + } - else if (cur_tool == TOOL_LABEL && cur_label == LABEL_SELECT) - { - /* [Enter] to select a node to edit */ + else if (cur_tool == TOOL_LABEL && cur_label == LABEL_SELECT) + { + /* [Enter] to select a node to edit */ - DEBUG_PRINTF("Searching for label @ (%d+3,%d+3)\n", - highlighted_label_node->save_x, - highlighted_label_node->save_y); + DEBUG_PRINTF("Searching for label @ (%d+3,%d+3)\n", + highlighted_label_node->save_x, + highlighted_label_node->save_y); - label_node_to_edit = - search_label_list(&highlighted_label_node, highlighted_label_node->save_x + 3, - highlighted_label_node->save_y + 3, 0); + label_node_to_edit = + search_label_list(&highlighted_label_node, + highlighted_label_node->save_x + 3, + highlighted_label_node->save_y + 3, 0); - if (label_node_to_edit) - { - select_label_node(&old_x, &old_y); - DEBUG_PRINTF("Got a label: \"%ls\" @ (%d,%d)\n", - label_node_to_edit->save_texttool_str, old_x, old_y); - DEBUG_PRINTF("Cursor now @ (%d,%d); width = %d\n", - cursor_x, cursor_y, cursor_textwidth); - cursor_x = label_node_to_edit->save_x; - cursor_y = label_node_to_edit->save_y; - cursor_left = cursor_x; - DEBUG_PRINTF("Cursor now @ (%d,%d)\n", cursor_x, cursor_y); - } + if (label_node_to_edit) + { + select_label_node(&old_x, &old_y); + DEBUG_PRINTF("Got a label: \"%ls\" @ (%d,%d)\n", + label_node_to_edit->save_texttool_str, old_x, + old_y); + DEBUG_PRINTF("Cursor now @ (%d,%d); width = %d\n", + cursor_x, cursor_y, cursor_textwidth); + cursor_x = label_node_to_edit->save_x; + cursor_y = label_node_to_edit->save_y; + cursor_left = cursor_x; + DEBUG_PRINTF("Cursor now @ (%d,%d)\n", cursor_x, + cursor_y); + } - do_render_cur_text(0); - } + do_render_cur_text(0); + } - else if (cur_tool == TOOL_LABEL && cur_label == LABEL_APPLY) - { - /* [Enter] to select a node to apply it to the canvas */ + else if (cur_tool == TOOL_LABEL && cur_label == LABEL_APPLY) + { + /* [Enter] to select a node to apply it to the canvas */ - label_node_to_edit = - search_label_list(&highlighted_label_node, highlighted_label_node->save_x + 3, - highlighted_label_node->save_y + 3, 0); + label_node_to_edit = + search_label_list(&highlighted_label_node, + highlighted_label_node->save_x + 3, + highlighted_label_node->save_y + 3, 0); - if (label_node_to_edit) - { - reposition_onscreen_keyboard(old_y); - apply_label_node(highlighted_label_node->save_x, highlighted_label_node->save_y); - do_render_cur_text(0); - } - } - else - { - /* [Enter] with no text; just move insertion cursor down to the next 'line' */ + if (label_node_to_edit) + { + reposition_onscreen_keyboard(old_y); + apply_label_node(highlighted_label_node->save_x, + highlighted_label_node->save_y); + do_render_cur_text(0); + } + } + else + { + /* [Enter] with no text; just move insertion cursor down to the next 'line' */ - cursor_x = cursor_left; - cursor_y = min(cursor_y + font_height, canvas->h - font_height); + cursor_x = cursor_left; + cursor_y = + min(cursor_y + font_height, canvas->h - font_height); - /* Reposition the on-screen keyboard if we begin typing over it */ - update_canvas_ex(kbd_rect.x, kbd_rect.y, kbd_rect.x + kbd_rect.w, - kbd_rect.y + kbd_rect.h, 0); - update_screen_rect(&kbd_rect); - reposition_onscreen_keyboard(cursor_y); + /* Reposition the on-screen keyboard if we begin typing over it */ + update_canvas_ex(kbd_rect.x, kbd_rect.y, + kbd_rect.x + kbd_rect.w, + kbd_rect.y + kbd_rect.h, 0); + update_screen_rect(&kbd_rect); + reposition_onscreen_keyboard(cursor_y); - playsound(screen, 0, SND_RETURN, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - } + playsound(screen, 0, SND_RETURN, 1, SNDPOS_RIGHT, + SNDDIST_NEAR); + } #ifdef SPEECH #ifdef __APPLE__ - if (use_sound) - speak_string(texttool_str); + if (use_sound) + speak_string(texttool_str); #endif #endif - im_softreset(&im_data); - } - else if (*im_cp == L'\t') - { - /* [Tab]... */ + im_softreset(&im_data); + } + else if (*im_cp == L'\t') + { + /* [Tab]... */ - if (texttool_len > 0) - { - /* [Tab] to finish entering text */ + if (texttool_len > 0) + { + /* [Tab] to finish entering text */ - rec_undo_buffer(); - do_render_cur_text(1); - label_node_to_edit = NULL; - cursor_x = min(cursor_x + cursor_textwidth, canvas->w); - texttool_len = 0; - cursor_textwidth = 0; - if (cur_tool == TOOL_LABEL) - { - draw_fonts(); - update_screen_rect(&r_toolopt); - } + rec_undo_buffer(); + do_render_cur_text(1); + label_node_to_edit = NULL; + cursor_x = min(cursor_x + cursor_textwidth, canvas->w); + texttool_len = 0; + cursor_textwidth = 0; + if (cur_tool == TOOL_LABEL) + { + draw_fonts(); + update_screen_rect(&r_toolopt); + } - if (been_saved) - { - been_saved = 0; + if (been_saved) + { + been_saved = 0; - if (!disable_save) - tool_avail[TOOL_SAVE] = 1; + if (!disable_save) + tool_avail[TOOL_SAVE] = 1; - draw_toolbar(); - update_screen_rect(&r_tools); - } + draw_toolbar(); + update_screen_rect(&r_tools); + } - playsound(screen, 0, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - } - else if (cur_tool == TOOL_LABEL && label_node_to_edit) - { - /* [Tab] to finish erasing text from a pre-existing Label */ + playsound(screen, 0, SND_CLICK, 1, SNDPOS_RIGHT, + SNDDIST_NEAR); + } + else if (cur_tool == TOOL_LABEL && label_node_to_edit) + { + /* [Tab] to finish erasing text from a pre-existing Label */ - rec_undo_buffer(); - have_to_rec_label_node = TRUE; - add_label_node(0, 0, 0, 0, NULL); - derender_node(&label_node_to_edit); - label_node_to_edit = NULL; + rec_undo_buffer(); + have_to_rec_label_node = TRUE; + add_label_node(0, 0, 0, 0, NULL); + derender_node(&label_node_to_edit); + label_node_to_edit = NULL; - playsound(screen, 0, SND_LINE_END, 0, SNDPOS_CENTER, SNDDIST_NEAR); + playsound(screen, 0, SND_LINE_END, 0, SNDPOS_CENTER, + SNDDIST_NEAR); - if (been_saved) - { - been_saved = 0; + if (been_saved) + { + been_saved = 0; - if (!disable_save) - tool_avail[TOOL_SAVE] = 1; + if (!disable_save) + tool_avail[TOOL_SAVE] = 1; - draw_toolbar(); - update_screen_rect(&r_tools); - } - } - else if (cur_tool == TOOL_LABEL && (cur_label == LABEL_SELECT || cur_label == LABEL_APPLY)) - { - /* [Tab] to cycle between the Labels (nodes) */ + draw_toolbar(); + update_screen_rect(&r_tools); + } + } + else if (cur_tool == TOOL_LABEL + && (cur_label == LABEL_SELECT + || cur_label == LABEL_APPLY)) + { + /* [Tab] to cycle between the Labels (nodes) */ - cycle_highlighted_label_node(); - highlight_label_nodes(); - } + cycle_highlighted_label_node(); + highlight_label_nodes(); + } #ifdef SPEECH #ifdef __APPLE__ - if (use_sound) - speak_string(texttool_str); + if (use_sound) + speak_string(texttool_str); #endif #endif - im_softreset(&im_data); - } - else if (cur_tool == TOOL_TEXT || cur_label == LABEL_LABEL) - { - // iswprintf seems not supported well in Android + im_softreset(&im_data); + } + else if (cur_tool == TOOL_TEXT || cur_label == LABEL_LABEL) + { + // iswprintf seems not supported well in Android #ifndef __ANDROID__ - if (!iswprint(*im_cp)) - break; + if (!iswprint(*im_cp)) + break; #endif - /* Printable characters... */ + /* Printable characters... */ - if (texttool_len < (sizeof(texttool_str) / sizeof(wchar_t)) - 1) - { - int old_cursor_textwidth = cursor_textwidth; + if (texttool_len < + (sizeof(texttool_str) / sizeof(wchar_t)) - 1) + { + int old_cursor_textwidth = cursor_textwidth; #ifdef DEBUG // wprintf(L" key = <%c>\nunicode = <%lc> 0x%04x %d\n\n", // key_down, key_unicode, key_unicode, key_unicode); #endif - texttool_str[texttool_len++] = *im_cp; - texttool_str[texttool_len] = 0; + texttool_str[texttool_len++] = *im_cp; + texttool_str[texttool_len] = 0; - do_render_cur_text(0); + do_render_cur_text(0); - if (been_saved) - { - been_saved = 0; + if (been_saved) + { + been_saved = 0; - if (!disable_save) - tool_avail[TOOL_SAVE] = 1; + if (!disable_save) + tool_avail[TOOL_SAVE] = 1; - draw_toolbar(); - update_screen_rect(&r_tools); - } + draw_toolbar(); + update_screen_rect(&r_tools); + } - if (cursor_x + old_cursor_textwidth <= canvas->w - 50 && - cursor_x + cursor_textwidth > canvas->w - 50) - { - playsound(screen, 0, SND_KEYCLICKRING, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - } - else - { - /* FIXME: Might be fun to position the - sound based on keyboard layout...? */ + if (cursor_x + old_cursor_textwidth <= canvas->w - 50 && + cursor_x + cursor_textwidth > canvas->w - 50) + { + playsound(screen, 0, SND_KEYCLICKRING, 1, SNDPOS_RIGHT, + SNDDIST_NEAR); + } + else + { + /* FIXME: Might be fun to position the + sound based on keyboard layout...? */ - playsound(screen, 0, SND_KEYCLICK, 0, SNDPOS_CENTER, SNDDIST_NEAR); - } - } - } - - im_cp++; - } /* while(*im_cp) */ - - /* Show IM tip text */ - if (im_data.tip_text) - { - draw_tux_text(TUX_DEFAULT, im_data.tip_text, 1); - } - - } + playsound(screen, 0, SND_KEYCLICK, 0, SNDPOS_CENTER, + SNDDIST_NEAR); + } } - } - else if (event.type == SDL_JOYAXISMOTION) - { - handle_joyaxismotion(event, &motioner, &val_x, &val_y); - } - else if (event.type == SDL_JOYHATMOTION) - { - handle_joyhatmotion(event, oldpos_x, oldpos_y, &valhat_x, &valhat_y, &hatmotioner, &old_hat_ticks); - } - else if (event.type == SDL_JOYBALLMOTION) - { - handle_joyballmotion(event, oldpos_x, oldpos_y); - } - else if (event.type == SDL_JOYBUTTONDOWN || event.type == SDL_JOYBUTTONUP) - { - handle_joybuttonupdownscl(event, oldpos_x, oldpos_y, real_r_tools); - } - else if (event.type == SDL_MOUSEBUTTONDOWN && - event.button.button >= 2 && - event.button.button <= 3 && - (no_button_distinction == 0 && !(HIT(r_tools) && GRIDHIT_GD(r_tools, gd_tools) == TOOL_PRINT))) - { - /* They're using the middle or right mouse buttons! */ + } - non_left_click_count++; + im_cp++; + } /* while(*im_cp) */ + + /* Show IM tip text */ + if (im_data.tip_text) + { + draw_tux_text(TUX_DEFAULT, im_data.tip_text, 1); + } + + } + } + } + else if (event.type == SDL_JOYAXISMOTION) + { + handle_joyaxismotion(event, &motioner, &val_x, &val_y); + } + else if (event.type == SDL_JOYHATMOTION) + { + handle_joyhatmotion(event, oldpos_x, oldpos_y, &valhat_x, &valhat_y, + &hatmotioner, &old_hat_ticks); + } + else if (event.type == SDL_JOYBALLMOTION) + { + handle_joyballmotion(event, oldpos_x, oldpos_y); + } + else if (event.type == SDL_JOYBUTTONDOWN + || event.type == SDL_JOYBUTTONUP) + { + handle_joybuttonupdownscl(event, oldpos_x, oldpos_y, real_r_tools); + } + else if (event.type == SDL_MOUSEBUTTONDOWN && + event.button.button >= 2 && + event.button.button <= 3 && + (no_button_distinction == 0 + && !(HIT(r_tools) + && GRIDHIT_GD(r_tools, gd_tools) == TOOL_PRINT))) + { + /* They're using the middle or right mouse buttons! */ + + non_left_click_count++; - if (non_left_click_count == 10 || non_left_click_count == 20 || (non_left_click_count % 50) == 0) + if (non_left_click_count == 10 || non_left_click_count == 20 + || (non_left_click_count % 50) == 0) + { + /* Pop up an informative animation: */ + + hide_blinking_cursor(); + do_prompt_image_flash(PROMPT_TIP_LEFTCLICK_TXT, + PROMPT_TIP_LEFTCLICK_YES, + "", img_mouse, img_mouse_click, NULL, 1, + event.button.x, event.button.y); + if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + { + if (onscreen_keyboard && !kbd) + { + r_tir.y = (float) event.button.y / render_scale; + SDL_SetTextInputRect(&r_tir); + SDL_StartTextInput(); + } + do_render_cur_text(0); + } + draw_tux_text(TUX_BORED, "", 0); + } + } + else if ((event.type == SDL_MOUSEBUTTONDOWN || + event.type == TP_SDL_MOUSEBUTTONSCROLL) + && event.button.button <= 3) + { + if (HIT(r_tools)) + { + if (HIT(real_r_tools)) + { + /* A tool on the left has been pressed! */ + brushflag = 0; + magicflag = 0; + magic_switchout(canvas); + whicht = tool_scroll + GRIDHIT_GD(real_r_tools, gd_tools); + + if (whicht < NUM_TOOLS && tool_avail[whicht] && + (valid_click(event.button.button) || whicht == TOOL_PRINT)) + { + /* Allow middle/right-click on "Print", since [Alt]+click + on Mac OS X changes it from left click to middle! */ + + /* Render any current text, if switching to a different + drawing tool: */ + + if ((cur_tool == TOOL_TEXT && whicht != TOOL_TEXT && + whicht != TOOL_NEW && whicht != TOOL_OPEN && + whicht != TOOL_SAVE && whicht != TOOL_PRINT && + whicht != TOOL_QUIT) || + (cur_tool == TOOL_LABEL && whicht != TOOL_LABEL && + whicht != TOOL_NEW && whicht != TOOL_OPEN && + whicht != TOOL_SAVE && whicht != TOOL_PRINT + && whicht != TOOL_QUIT)) + { + if (cursor_x != -1 && cursor_y != -1) { - /* Pop up an informative animation: */ - hide_blinking_cursor(); - do_prompt_image_flash(PROMPT_TIP_LEFTCLICK_TXT, - PROMPT_TIP_LEFTCLICK_YES, - "", img_mouse, img_mouse_click, NULL, 1, event.button.x, event.button.y); - if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - { - if (onscreen_keyboard && !kbd) - { - r_tir.y = (float)event.button.y / render_scale; - SDL_SetTextInputRect(&r_tir); - SDL_StartTextInput(); - } - do_render_cur_text(0); - } - draw_tux_text(TUX_BORED, "", 0); + if (texttool_len > 0) + { + rec_undo_buffer(); + do_render_cur_text(1); + texttool_len = 0; + cursor_textwidth = 0; + label_node_to_edit = NULL; + } + else if (cur_tool == TOOL_LABEL && label_node_to_edit) + { + rec_undo_buffer(); + have_to_rec_label_node = TRUE; + add_label_node(0, 0, 0, 0, NULL); + derender_node(&label_node_to_edit); + label_node_to_edit = NULL; + } } - } - else if ((event.type == SDL_MOUSEBUTTONDOWN || - event.type == TP_SDL_MOUSEBUTTONSCROLL) && event.button.button <= 3) - { - if (HIT(r_tools)) + } + update_canvas(0, 0, WINDOW_WIDTH - r_ttoolopt.w, + (button_h * buttons_tall) + r_ttools.h); + + old_tool = cur_tool; + cur_tool = whicht; + draw_toolbar(); + update_screen_rect(&r_tools); + printf("screenrectr_tools %d, %d, %d, %d\n", r_tools.x, + r_tools.y, r_tools.w, r_tools.h); + playsound(screen, 1, SND_CLICK, 0, SNDPOS_LEFT, SNDDIST_NEAR); + + /* FIXME: this "if" is just plain gross */ + if (cur_tool != TOOL_TEXT) + draw_cur_tool_tip(); + + /* Draw items for this tool: */ + + if (cur_tool == TOOL_BRUSH) + { + keybd_flag = 0; + cur_thing = cur_brush; + num_things = num_brushes; + thing_scroll = &brush_scroll; + draw_brushes(); + draw_colors(COLORSEL_ENABLE); + } + else if (cur_tool == TOOL_STAMP) + { + keybd_flag = 0; + cur_thing = cur_stamp[stamp_group]; + num_things = num_stamps[stamp_group]; + thing_scroll = &(stamp_scroll[stamp_group]); + draw_stamps(); + draw_colors(stamp_colorable(cur_stamp[stamp_group]) || + stamp_tintable(cur_stamp[stamp_group])); + reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, + &stamp_tool_mode); + set_active_stamp(); + update_stamp_xor(0); + } + else if (cur_tool == TOOL_LINES) + { + keybd_flag = 0; + cur_thing = cur_brush; + num_things = num_brushes; + thing_scroll = &brush_scroll; + draw_brushes(); + draw_colors(COLORSEL_ENABLE); + } + else if (cur_tool == TOOL_FILL) + { + keybd_flag = 0; + cur_thing = cur_fill; + num_things = NUM_FILLS; + thing_scroll = &fill_scroll; + draw_fills(); + draw_colors(COLORSEL_ENABLE); + } + else if (cur_tool == TOOL_SHAPES) + { + keybd_flag = 0; + cur_thing = cur_shape; + num_things = NUM_SHAPES; + thing_scroll = &shape_scroll; + draw_shapes(); + draw_colors(COLORSEL_ENABLE); + shape_tool_mode = SHAPE_TOOL_MODE_DONE; + } + else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + { + if (onscreen_keyboard && kbd) { - if (HIT(real_r_tools)) - { - /* A tool on the left has been pressed! */ - brushflag = 0; - magicflag = 0; - magic_switchout(canvas); - whicht = tool_scroll + GRIDHIT_GD(real_r_tools, gd_tools); + if (kbd == NULL) + { + if (onscreen_keyboard_layout) + kbd = + osk_create(onscreen_keyboard_layout, canvas, + img_btn_up, img_btn_down, img_btn_off, + img_btn_nav, img_btn_hold, + img_oskdel, img_osktab, img_oskenter, + img_oskcapslock, img_oskshift, + onscreen_keyboard_disable_change); + else + kbd = + osk_create(strdup("default.layout"), canvas, + img_btn_up, img_btn_down, img_btn_off, + img_btn_nav, img_btn_hold, + img_oskdel, img_osktab, img_oskenter, + img_oskcapslock, img_oskshift, + onscreen_keyboard_disable_change); + } - if (whicht < NUM_TOOLS && tool_avail[whicht] && - (valid_click(event.button.button) || whicht == TOOL_PRINT)) - { - /* Allow middle/right-click on "Print", since [Alt]+click - on Mac OS X changes it from left click to middle! */ + if (kbd == NULL) + fprintf(stderr, "kbd = NULL\n"); + else + reposition_onscreen_keyboard(0); + } + if (!font_thread_done) + { + draw_colors(COLORSEL_DISABLE); + draw_none(); + update_screen_rect(&r_toolopt); + update_screen_rect(&r_ttoolopt); + do_setcursor(cursor_watch); - /* Render any current text, if switching to a different - drawing tool: */ + /* Wait while Text tool finishes loading fonts */ + draw_tux_text(TUX_WAIT, gettext("Please wait…"), 1); - if ((cur_tool == TOOL_TEXT && whicht != TOOL_TEXT && - whicht != TOOL_NEW && whicht != TOOL_OPEN && - whicht != TOOL_SAVE && whicht != TOOL_PRINT && - whicht != TOOL_QUIT) || - (cur_tool == TOOL_LABEL && whicht != TOOL_LABEL && - whicht != TOOL_NEW && whicht != TOOL_OPEN && - whicht != TOOL_SAVE && whicht != TOOL_PRINT && whicht != TOOL_QUIT)) - { - if (cursor_x != -1 && cursor_y != -1) - { - hide_blinking_cursor(); - if (texttool_len > 0) - { - rec_undo_buffer(); - do_render_cur_text(1); - texttool_len = 0; - cursor_textwidth = 0; - label_node_to_edit = NULL; - } - else if (cur_tool == TOOL_LABEL && label_node_to_edit) - { - rec_undo_buffer(); - have_to_rec_label_node = TRUE; - add_label_node(0, 0, 0, 0, NULL); - derender_node(&label_node_to_edit); - label_node_to_edit = NULL; - } - } - } - update_canvas(0, 0, WINDOW_WIDTH - r_ttoolopt.w, (button_h * buttons_tall) + r_ttools.h); - - old_tool = cur_tool; - cur_tool = whicht; - draw_toolbar(); - update_screen_rect(&r_tools); - printf("screenrectr_tools %d, %d, %d, %d\n", r_tools.x, r_tools.y, r_tools.w, r_tools.h); - playsound(screen, 1, SND_CLICK, 0, SNDPOS_LEFT, SNDDIST_NEAR); - - /* FIXME: this "if" is just plain gross */ - if (cur_tool != TOOL_TEXT) - draw_cur_tool_tip(); - - /* Draw items for this tool: */ - - if (cur_tool == TOOL_BRUSH) - { - keybd_flag = 0; - cur_thing = cur_brush; - num_things = num_brushes; - thing_scroll = &brush_scroll; - draw_brushes(); - draw_colors(COLORSEL_ENABLE); - } - else if (cur_tool == TOOL_STAMP) - { - keybd_flag = 0; - cur_thing = cur_stamp[stamp_group]; - num_things = num_stamps[stamp_group]; - thing_scroll = &(stamp_scroll[stamp_group]); - draw_stamps(); - draw_colors(stamp_colorable(cur_stamp[stamp_group]) || - stamp_tintable(cur_stamp[stamp_group])); - reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, &stamp_tool_mode); - set_active_stamp(); - update_stamp_xor(0); - } - else if (cur_tool == TOOL_LINES) - { - keybd_flag = 0; - cur_thing = cur_brush; - num_things = num_brushes; - thing_scroll = &brush_scroll; - draw_brushes(); - draw_colors(COLORSEL_ENABLE); - } - else if (cur_tool == TOOL_FILL) - { - keybd_flag = 0; - cur_thing = cur_fill; - num_things = NUM_FILLS; - thing_scroll = &fill_scroll; - draw_fills(); - draw_colors(COLORSEL_ENABLE); - } - else if (cur_tool == TOOL_SHAPES) - { - keybd_flag = 0; - cur_thing = cur_shape; - num_things = NUM_SHAPES; - thing_scroll = &shape_scroll; - draw_shapes(); - draw_colors(COLORSEL_ENABLE); - shape_tool_mode = SHAPE_TOOL_MODE_DONE; - } - else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - { - if (onscreen_keyboard && kbd) - { - if (kbd == NULL) - { - if (onscreen_keyboard_layout) - kbd = - osk_create(onscreen_keyboard_layout, canvas, - img_btn_up, img_btn_down, img_btn_off, - img_btn_nav, img_btn_hold, - img_oskdel, img_osktab, img_oskenter, - img_oskcapslock, img_oskshift, - onscreen_keyboard_disable_change); - else - kbd = - osk_create(strdup("default.layout"), canvas, - img_btn_up, img_btn_down, img_btn_off, - img_btn_nav, img_btn_hold, - img_oskdel, img_osktab, img_oskenter, - img_oskcapslock, img_oskshift, - onscreen_keyboard_disable_change); - } - - if (kbd == NULL) - fprintf(stderr, "kbd = NULL\n"); - else - reposition_onscreen_keyboard(0); - } - if (!font_thread_done) - { - draw_colors(COLORSEL_DISABLE); - draw_none(); - update_screen_rect(&r_toolopt); - update_screen_rect(&r_ttoolopt); - do_setcursor(cursor_watch); - - /* Wait while Text tool finishes loading fonts */ - draw_tux_text(TUX_WAIT, gettext("Please wait…"), 1); - - waiting_for_fonts = 1; + waiting_for_fonts = 1; #ifdef FORKED_FONTS - receive_some_font_info(screen, texture, renderer); + receive_some_font_info(screen, texture, renderer); #else - while (!font_thread_done && !font_thread_aborted) - { - /* FIXME: should have a read-depends memory barrier around here */ - show_progress_bar(screen); - SDL_Delay(20); - } - /* FIXME: should kill this in any case */ - SDL_WaitThread(font_thread, NULL); + while (!font_thread_done && !font_thread_aborted) + { + /* FIXME: should have a read-depends memory barrier around here */ + show_progress_bar(screen); + SDL_Delay(20); + } + /* FIXME: should kill this in any case */ + SDL_WaitThread(font_thread, NULL); #endif - set_label_fonts(); - do_setcursor(cursor_arrow); - } - if (onscreen_keyboard && !kbd) - { - r_tir.y = (float)event.button.y / render_scale; - SDL_SetTextInputRect(&r_tir); - SDL_StartTextInput(); - } - draw_cur_tool_tip(); + set_label_fonts(); + do_setcursor(cursor_arrow); + } + if (onscreen_keyboard && !kbd) + { + r_tir.y = (float) event.button.y / render_scale; + SDL_SetTextInputRect(&r_tir); + SDL_StartTextInput(); + } + draw_cur_tool_tip(); - if (num_font_families > 0) - { - cur_thing = cur_font; - num_things = num_font_families; - thing_scroll = &font_scroll; - cur_label = LABEL_LABEL; + if (num_font_families > 0) + { + cur_thing = cur_font; + num_things = num_font_families; + thing_scroll = &font_scroll; + cur_label = LABEL_LABEL; - draw_fonts(); - draw_colors(COLORSEL_ENABLE); - } - else - { - /* Problem using fonts! */ + draw_fonts(); + draw_colors(COLORSEL_ENABLE); + } + else + { + /* Problem using fonts! */ - cur_tool = old_tool; - draw_toolbar(); - update_screen_rect(&r_tools); - } - } - else if (cur_tool == TOOL_MAGIC) - { - keybd_flag = 0; - cur_thing = cur_magic[magic_group]; - num_things = num_magics[magic_group]; - thing_scroll = &(magic_scroll[magic_group]); - magic_current_snd_ptr = NULL; - draw_magic(); - draw_colors(magics[magic_group][cur_thing].colors); + cur_tool = old_tool; + draw_toolbar(); + update_screen_rect(&r_tools); + } + } + else if (cur_tool == TOOL_MAGIC) + { + keybd_flag = 0; + cur_thing = cur_magic[magic_group]; + num_things = num_magics[magic_group]; + thing_scroll = &(magic_scroll[magic_group]); + magic_current_snd_ptr = NULL; + draw_magic(); + draw_colors(magics[magic_group][cur_thing].colors); - if (magics[magic_group][cur_thing].colors) - magic_funcs[magics[magic_group][cur_thing].handle_idx].set_color(magic_api_struct, - color_hexes[cur_color][0], - color_hexes[cur_color][1], - color_hexes[cur_color][2]); - } - else if (cur_tool == TOOL_ERASER) - { - keybd_flag = 0; - cur_thing = cur_eraser; - num_things = NUM_ERASERS; - thing_scroll = &eraser_scroll; - draw_erasers(); - draw_colors(COLORSEL_DISABLE); - } - else if (cur_tool == TOOL_UNDO) - { - if (cur_undo == newest_undo) - { - rec_undo_buffer(); - do_undo(); - } - do_undo(); + if (magics[magic_group][cur_thing].colors) + magic_funcs[magics[magic_group][cur_thing].handle_idx]. + set_color(magic_api_struct, color_hexes[cur_color][0], + color_hexes[cur_color][1], + color_hexes[cur_color][2]); + } + else if (cur_tool == TOOL_ERASER) + { + keybd_flag = 0; + cur_thing = cur_eraser; + num_things = NUM_ERASERS; + thing_scroll = &eraser_scroll; + draw_erasers(); + draw_colors(COLORSEL_DISABLE); + } + else if (cur_tool == TOOL_UNDO) + { + if (cur_undo == newest_undo) + { + rec_undo_buffer(); + do_undo(); + } + do_undo(); - been_saved = 0; + been_saved = 0; - if (!disable_save) - tool_avail[TOOL_SAVE] = 1; + if (!disable_save) + tool_avail[TOOL_SAVE] = 1; - cur_tool = old_tool; - draw_toolbar(); - update_screen_rect(&r_tools); - shape_tool_mode = SHAPE_TOOL_MODE_DONE; - reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, &stamp_tool_mode); - } - else if (cur_tool == TOOL_REDO) - { - do_redo(); + cur_tool = old_tool; + draw_toolbar(); + update_screen_rect(&r_tools); + shape_tool_mode = SHAPE_TOOL_MODE_DONE; + reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, + &stamp_tool_mode); + } + else if (cur_tool == TOOL_REDO) + { + do_redo(); - been_saved = 0; + been_saved = 0; - if (!disable_save) - tool_avail[TOOL_SAVE] = 1; + if (!disable_save) + tool_avail[TOOL_SAVE] = 1; - cur_tool = old_tool; - draw_toolbar(); - update_screen_rect(&r_tools); - shape_tool_mode = SHAPE_TOOL_MODE_DONE; - reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, &stamp_tool_mode); - } - else if (cur_tool == TOOL_OPEN) - { - disable_avail_tools(); - draw_toolbar(); - draw_colors(COLORSEL_CLOBBER_WIPE); - draw_none(); + cur_tool = old_tool; + draw_toolbar(); + update_screen_rect(&r_tools); + shape_tool_mode = SHAPE_TOOL_MODE_DONE; + reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, + &stamp_tool_mode); + } + else if (cur_tool == TOOL_OPEN) + { + disable_avail_tools(); + draw_toolbar(); + draw_colors(COLORSEL_CLOBBER_WIPE); + draw_none(); - if (do_open() == 0) - { - if (old_tool == TOOL_TEXT || old_tool == TOOL_LABEL) - do_render_cur_text(0); - } - - enable_avail_tools(); - - cur_tool = old_tool; - draw_toolbar(); - update_screen_rect(&r_tools); - - draw_cur_tool_tip(); - - draw_colors(COLORSEL_REFRESH); - - if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) - draw_brushes(); - else if (cur_tool == TOOL_MAGIC) - draw_magic(); - else if (cur_tool == TOOL_STAMP) - { - draw_stamps(); - reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, &stamp_tool_mode); - } - else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - { - draw_fonts(); - if (onscreen_keyboard && kbd) - { - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); - } - - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput(); - } - } - else if (cur_tool == TOOL_SHAPES) - draw_shapes(); - else if (cur_tool == TOOL_ERASER) - draw_erasers(); - else if (cur_tool == TOOL_FILL) - draw_fills(); - } - else if (cur_tool == TOOL_SAVE) - { - if (do_save(old_tool, 0, 0)) - { - been_saved = 1; - tool_avail[TOOL_SAVE] = 0; - } - - if (old_tool == TOOL_TEXT || old_tool == TOOL_LABEL) - { - if (onscreen_keyboard && kbd) - { - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); - } - - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput(); - } - } - else if (old_tool == TOOL_STAMP) - reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, &stamp_tool_mode); - - cur_tool = old_tool; - draw_toolbar(); - update_screen_rect(&r_tools); - } - else if (cur_tool == TOOL_NEW) - { - shape_tool_mode = SHAPE_TOOL_MODE_DONE; - - disable_avail_tools(); - draw_toolbar(); - draw_colors(COLORSEL_CLOBBER_WIPE); - draw_none(); - - if (do_new_dialog() == 0) - { - cur_tool = old_tool; - - draw_tux_text(tool_tux[TUX_DEFAULT], TIP_NEW_ABORT, 1); - - if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - do_render_cur_text(0); - } - - cur_tool = old_tool; - - enable_avail_tools(); - - draw_toolbar(); - update_screen_rect(&r_tools); - draw_colors(COLORSEL_REFRESH); - - if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) - draw_brushes(); - else if (cur_tool == TOOL_MAGIC) - draw_magic(); - else if (cur_tool == TOOL_STAMP) - { - draw_stamps(); - reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, &stamp_tool_mode); - } - else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - { - draw_fonts(); - if (onscreen_keyboard && kbd) - { - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); - } - - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput(); - - } - } - else if (cur_tool == TOOL_SHAPES) - draw_shapes(); - else if (cur_tool == TOOL_ERASER) - draw_erasers(); - else if (cur_tool == TOOL_FILL) - draw_fills(); - } - else if (cur_tool == TOOL_PRINT) - { - /* If they haven't hit [Enter], but clicked 'Print', add their text now -bjk 2007.10.25 */ - tmp_apply_uncommited_text(); - /* original print code was here */ - print_image(); - undo_tmp_applied_text(); - - if (old_tool == TOOL_TEXT || old_tool == TOOL_LABEL) - { - if (onscreen_keyboard && kbd) - { - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); - } - - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput(); - } - } - else if (old_tool == TOOL_STAMP) - reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, &stamp_tool_mode); - - cur_tool = old_tool; - draw_toolbar(); - draw_tux_text(TUX_BORED, "", 0); - update_screen_rect(&r_tools); - } - else if (cur_tool == TOOL_QUIT) - { - done = do_quit(old_tool); - - if (old_tool == TOOL_TEXT || old_tool == TOOL_LABEL) - { - if (onscreen_keyboard && kbd) - { - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); - } - - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput(); - - } - } - else if (old_tool == TOOL_STAMP) - reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, &stamp_tool_mode); - - cur_tool = old_tool; - draw_toolbar(); - update_screen_rect(&r_tools); - } - update_screen_rect(&r_toolopt); - update_screen_rect(&r_ttoolopt); - } - - if (!done) - magic_switchin(canvas); - } - else if ( - ((event.button.y < r_tools.y + button_h / 2) && tool_scroll > 0) || - ((event.button.y > real_r_tools.y + real_r_tools.h) - && (tool_scroll < NUM_TOOLS - buttons_tall * gd_tools.cols + gd_tools.cols)) - ) { - /* Tool up or down scroll buttons */ - - if (event.button.y < r_tools.y + button_h / 2) - { - /* Tool up scroll button */ - tool_scroll -= gd_tools.cols; - playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, SNDDIST_NEAR); - - draw_toolbar(); - update_screen_rect(&r_tools); - } - else - { - /* Tool down scroll button */ - tool_scroll += gd_tools.cols; - draw_toolbar(); - playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, SNDDIST_NEAR); - - update_screen_rect(&r_tools); - } - - if (!scrolling_tool && event.type == SDL_MOUSEBUTTONDOWN) - { - DEBUG_PRINTF("Starting scrolling\n"); - memcpy(&scrolltimer_tool_event, &event, sizeof(SDL_Event)); - scrolltimer_tool_event.type = TP_SDL_MOUSEBUTTONSCROLL; - - /* - * We enable the timer subsystem only when needed (e.g., to use SDL_AddTimer() needed - * for scrolling) then disable it immediately after (e.g., after the timer has fired or - * after SDL_RemoveTimer()) because enabling the timer subsystem in SDL1 has a high - * energy impact on the Mac. - */ - - scrolling_tool = 1; - SDL_InitSubSystem(SDL_INIT_TIMER); - scrolltimer_tool = - SDL_AddTimer(REPEAT_SPEED, scrolltimer_tool_callback, (void *)&scrolltimer_tool_event); - } - else - { - DEBUG_PRINTF("Continuing scrolling\n"); - scrolltimer_tool = - SDL_AddTimer(REPEAT_SPEED / 3, scrolltimer_tool_callback, (void *)&scrolltimer_tool_event); - } - } + if (do_open() == 0) + { + if (old_tool == TOOL_TEXT || old_tool == TOOL_LABEL) + do_render_cur_text(0); } - else if (HIT(r_toolopt) && valid_click(event.button.button)) + enable_avail_tools(); + + cur_tool = old_tool; + draw_toolbar(); + update_screen_rect(&r_tools); + + draw_cur_tool_tip(); + + draw_colors(COLORSEL_REFRESH); + + if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) + draw_brushes(); + else if (cur_tool == TOOL_MAGIC) + draw_magic(); + else if (cur_tool == TOOL_STAMP) { - /* Options on the right - WARNING: this must be kept in sync with the mouse-move - code (for cursor changes) and mouse-scroll code. */ + draw_stamps(); + reset_stamps(&stamp_xored_rt, &stamp_place_x, + &stamp_place_y, &stamp_tool_mode); + } + else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + { + draw_fonts(); + if (onscreen_keyboard && kbd) + { + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, + &kbd_rect); + update_screen_rect(&kbd_rect); + } - if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_STAMP || - cur_tool == TOOL_SHAPES || cur_tool == TOOL_LINES || - cur_tool == TOOL_MAGIC || cur_tool == TOOL_TEXT || - cur_tool == TOOL_ERASER || cur_tool == TOOL_LABEL || - cur_tool == TOOL_FILL) - { - int num_rows_needed; - SDL_Rect r_controls; - SDL_Rect r_notcontrols; - SDL_Rect r_items; /* = r_notcontrols; */ - int toolopt_changed; - int select_changed = 0; - grid_dims gd_controls; /* might become 2-by-2 */ - grid_dims gd_items; /* generally becoming 2-by-whatever */ + if (onscreen_keyboard && !kbd) + { + SDL_StartTextInput(); + } + } + else if (cur_tool == TOOL_SHAPES) + draw_shapes(); + else if (cur_tool == TOOL_ERASER) + draw_erasers(); + else if (cur_tool == TOOL_FILL) + draw_fills(); + } + else if (cur_tool == TOOL_SAVE) + { + if (do_save(old_tool, 0, 0)) + { + been_saved = 1; + tool_avail[TOOL_SAVE] = 0; + } - gd_controls.rows = 0; - gd_controls.cols = 0; - gd_items.rows = 2; - gd_items.cols = 2; + if (old_tool == TOOL_TEXT || old_tool == TOOL_LABEL) + { + if (onscreen_keyboard && kbd) + { + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, + &kbd_rect); + update_screen_rect(&kbd_rect); + } - /* Note set of things we're dealing with */ - /* (stamps, brushes, etc.) */ + if (onscreen_keyboard && !kbd) + { + SDL_StartTextInput(); + } + } + else if (old_tool == TOOL_STAMP) + reset_stamps(&stamp_xored_rt, &stamp_place_x, + &stamp_place_y, &stamp_tool_mode); - if (cur_tool == TOOL_STAMP) - { - if (!disable_stamp_controls) - { - /* Account for stamp controls and group changing (left/right) buttons */ - gd_controls.rows = 3; - gd_controls.cols = 2; - } - else - { - /* Stamp controls are disabled; account for group changing (left/right) buttons */ - gd_controls.rows = 1; - gd_controls.cols = 2; - } - } - else if (cur_tool == TOOL_TEXT) - { - if (!disable_stamp_controls) - { - /* Account for text controls */ - gd_controls.rows = 2; - gd_controls.cols = 2; - } - } - else if (cur_tool == TOOL_LABEL) - { - if (!disable_stamp_controls) - { - /* Account for text controls and label selection button */ - gd_controls.rows = 3; - gd_controls.cols = 2; - } - else - { - /* Text controls disabled; only account for label selection button */ - gd_controls.rows = 1; - gd_controls.cols = 2; - } - } + cur_tool = old_tool; + draw_toolbar(); + update_screen_rect(&r_tools); + } + else if (cur_tool == TOOL_NEW) + { + shape_tool_mode = SHAPE_TOOL_MODE_DONE; - else if (cur_tool == TOOL_MAGIC) - { - if (!disable_magic_controls) - { - /* Account for magic controls and group changing (left/right) buttons */ - gd_controls.rows = 2; - gd_controls.cols = 2; - } - else - { - /* Magic controls are disabled; account for group changing (left/right) buttons */ - gd_controls.rows = 1; - gd_controls.cols = 2; - } - } + disable_avail_tools(); + draw_toolbar(); + draw_colors(COLORSEL_CLOBBER_WIPE); + draw_none(); - else if (cur_tool == TOOL_SHAPES) - { - if (!disable_shape_controls) - { - /* Account for shape controls (corner- vs center-based expansion) */ - gd_controls.rows = 1; - gd_controls.cols = 2; - } - } + if (do_new_dialog() == 0) + { + cur_tool = old_tool; - else if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) - { - if (!disable_brushspacing) - { - /* Account for brush spacing */ - gd_controls.rows = 1; - gd_controls.cols = 2; - } - } + draw_tux_text(tool_tux[TUX_DEFAULT], TIP_NEW_ABORT, 1); - /* number of whole or partial rows that will be needed - (can make this per-tool if variable columns needed) */ - num_rows_needed = (num_things + gd_items.cols - 1) / gd_items.cols; + if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + do_render_cur_text(0); + } - do_draw = 0; + cur_tool = old_tool; - r_controls.w = r_toolopt.w; - r_controls.h = gd_controls.rows * button_h; - r_controls.x = r_toolopt.x; - r_controls.y = r_toolopt.y + r_toolopt.h - r_controls.h; + enable_avail_tools(); - r_notcontrols.w = r_toolopt.w; - r_notcontrols.h = r_toolopt.h - r_controls.h; - r_notcontrols.x = r_toolopt.x; - r_notcontrols.y = r_toolopt.y; + draw_toolbar(); + update_screen_rect(&r_tools); + draw_colors(COLORSEL_REFRESH); - r_items.x = r_notcontrols.x; - r_items.y = r_notcontrols.y; - r_items.w = r_notcontrols.w; - r_items.h = r_notcontrols.h; + if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) + draw_brushes(); + else if (cur_tool == TOOL_MAGIC) + draw_magic(); + else if (cur_tool == TOOL_STAMP) + { + draw_stamps(); + reset_stamps(&stamp_xored_rt, &stamp_place_x, + &stamp_place_y, &stamp_tool_mode); + } + else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + { + draw_fonts(); + if (onscreen_keyboard && kbd) + { + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, + &kbd_rect); + update_screen_rect(&kbd_rect); + } - if (num_rows_needed * button_h > r_items.h) - { - /* too many; we'll need scroll buttons */ - r_items.h -= button_h; - r_items.y += button_h / 2; - } - gd_items.rows = r_items.h / button_h; + if (onscreen_keyboard && !kbd) + { + SDL_StartTextInput(); - toolopt_changed = 0; + } + } + else if (cur_tool == TOOL_SHAPES) + draw_shapes(); + else if (cur_tool == TOOL_ERASER) + draw_erasers(); + else if (cur_tool == TOOL_FILL) + draw_fills(); + } + else if (cur_tool == TOOL_PRINT) + { + /* If they haven't hit [Enter], but clicked 'Print', add their text now -bjk 2007.10.25 */ + tmp_apply_uncommited_text(); + /* original print code was here */ + print_image(); + undo_tmp_applied_text(); - if (HIT(r_items)) - { - /* A selection button was clicked... */ - which = GRIDHIT_GD(r_items, gd_items) + *thing_scroll; + if (old_tool == TOOL_TEXT || old_tool == TOOL_LABEL) + { + if (onscreen_keyboard && kbd) + { + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, + &kbd_rect); + update_screen_rect(&kbd_rect); + } - if (which < num_things) - { - /* ...and there was something there to click */ - toolopt_changed = 1; + if (onscreen_keyboard && !kbd) + { + SDL_StartTextInput(); + } + } + else if (old_tool == TOOL_STAMP) + reset_stamps(&stamp_xored_rt, &stamp_place_x, + &stamp_place_y, &stamp_tool_mode); + + cur_tool = old_tool; + draw_toolbar(); + draw_tux_text(TUX_BORED, "", 0); + update_screen_rect(&r_tools); + } + else if (cur_tool == TOOL_QUIT) + { + done = do_quit(old_tool); + + if (old_tool == TOOL_TEXT || old_tool == TOOL_LABEL) + { + if (onscreen_keyboard && kbd) + { + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, + &kbd_rect); + update_screen_rect(&kbd_rect); + } + + if (onscreen_keyboard && !kbd) + { + SDL_StartTextInput(); + + } + } + else if (old_tool == TOOL_STAMP) + reset_stamps(&stamp_xored_rt, &stamp_place_x, + &stamp_place_y, &stamp_tool_mode); + + cur_tool = old_tool; + draw_toolbar(); + update_screen_rect(&r_tools); + } + update_screen_rect(&r_toolopt); + update_screen_rect(&r_ttoolopt); + } + + if (!done) + magic_switchin(canvas); + } + else if (((event.button.y < r_tools.y + button_h / 2) + && tool_scroll > 0) + || ((event.button.y > real_r_tools.y + real_r_tools.h) + && (tool_scroll < + NUM_TOOLS - buttons_tall * gd_tools.cols + + gd_tools.cols))) + { + /* Tool up or down scroll buttons */ + + if (event.button.y < r_tools.y + button_h / 2) + { + /* Tool up scroll button */ + tool_scroll -= gd_tools.cols; + playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, + SNDDIST_NEAR); + + draw_toolbar(); + update_screen_rect(&r_tools); + } + else + { + /* Tool down scroll button */ + tool_scroll += gd_tools.cols; + draw_toolbar(); + playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, + SNDDIST_NEAR); + + update_screen_rect(&r_tools); + } + + if (!scrolling_tool && event.type == SDL_MOUSEBUTTONDOWN) + { + DEBUG_PRINTF("Starting scrolling\n"); + memcpy(&scrolltimer_tool_event, &event, sizeof(SDL_Event)); + scrolltimer_tool_event.type = TP_SDL_MOUSEBUTTONSCROLL; + + /* + * We enable the timer subsystem only when needed (e.g., to use SDL_AddTimer() needed + * for scrolling) then disable it immediately after (e.g., after the timer has fired or + * after SDL_RemoveTimer()) because enabling the timer subsystem in SDL1 has a high + * energy impact on the Mac. + */ + + scrolling_tool = 1; + SDL_InitSubSystem(SDL_INIT_TIMER); + scrolltimer_tool = + SDL_AddTimer(REPEAT_SPEED, scrolltimer_tool_callback, + (void *) &scrolltimer_tool_event); + } + else + { + DEBUG_PRINTF("Continuing scrolling\n"); + scrolltimer_tool = + SDL_AddTimer(REPEAT_SPEED / 3, scrolltimer_tool_callback, + (void *) &scrolltimer_tool_event); + } + } + } + + else if (HIT(r_toolopt) && valid_click(event.button.button)) + { + /* Options on the right + WARNING: this must be kept in sync with the mouse-move + code (for cursor changes) and mouse-scroll code. */ + + if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_STAMP || + cur_tool == TOOL_SHAPES || cur_tool == TOOL_LINES || + cur_tool == TOOL_MAGIC || cur_tool == TOOL_TEXT || + cur_tool == TOOL_ERASER || cur_tool == TOOL_LABEL || + cur_tool == TOOL_FILL) + { + int num_rows_needed; + SDL_Rect r_controls; + SDL_Rect r_notcontrols; + SDL_Rect r_items; /* = r_notcontrols; */ + int toolopt_changed; + int select_changed = 0; + grid_dims gd_controls; /* might become 2-by-2 */ + grid_dims gd_items; /* generally becoming 2-by-whatever */ + + gd_controls.rows = 0; + gd_controls.cols = 0; + gd_items.rows = 2; + gd_items.cols = 2; + + /* Note set of things we're dealing with */ + /* (stamps, brushes, etc.) */ + + if (cur_tool == TOOL_STAMP) + { + if (!disable_stamp_controls) + { + /* Account for stamp controls and group changing (left/right) buttons */ + gd_controls.rows = 3; + gd_controls.cols = 2; + } + else + { + /* Stamp controls are disabled; account for group changing (left/right) buttons */ + gd_controls.rows = 1; + gd_controls.cols = 2; + } + } + else if (cur_tool == TOOL_TEXT) + { + if (!disable_stamp_controls) + { + /* Account for text controls */ + gd_controls.rows = 2; + gd_controls.cols = 2; + } + } + else if (cur_tool == TOOL_LABEL) + { + if (!disable_stamp_controls) + { + /* Account for text controls and label selection button */ + gd_controls.rows = 3; + gd_controls.cols = 2; + } + else + { + /* Text controls disabled; only account for label selection button */ + gd_controls.rows = 1; + gd_controls.cols = 2; + } + } + + else if (cur_tool == TOOL_MAGIC) + { + if (!disable_magic_controls) + { + /* Account for magic controls and group changing (left/right) buttons */ + gd_controls.rows = 2; + gd_controls.cols = 2; + } + else + { + /* Magic controls are disabled; account for group changing (left/right) buttons */ + gd_controls.rows = 1; + gd_controls.cols = 2; + } + } + + else if (cur_tool == TOOL_SHAPES) + { + if (!disable_shape_controls) + { + /* Account for shape controls (corner- vs center-based expansion) */ + gd_controls.rows = 1; + gd_controls.cols = 2; + } + } + + else if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) + { + if (!disable_brushspacing) + { + /* Account for brush spacing */ + gd_controls.rows = 1; + gd_controls.cols = 2; + } + } + + /* number of whole or partial rows that will be needed + (can make this per-tool if variable columns needed) */ + num_rows_needed = + (num_things + gd_items.cols - 1) / gd_items.cols; + + do_draw = 0; + + r_controls.w = r_toolopt.w; + r_controls.h = gd_controls.rows * button_h; + r_controls.x = r_toolopt.x; + r_controls.y = r_toolopt.y + r_toolopt.h - r_controls.h; + + r_notcontrols.w = r_toolopt.w; + r_notcontrols.h = r_toolopt.h - r_controls.h; + r_notcontrols.x = r_toolopt.x; + r_notcontrols.y = r_toolopt.y; + + r_items.x = r_notcontrols.x; + r_items.y = r_notcontrols.y; + r_items.w = r_notcontrols.w; + r_items.h = r_notcontrols.h; + + if (num_rows_needed * button_h > r_items.h) + { + /* too many; we'll need scroll buttons */ + r_items.h -= button_h; + r_items.y += button_h / 2; + } + gd_items.rows = r_items.h / button_h; + + toolopt_changed = 0; + + if (HIT(r_items)) + { + /* A selection button was clicked... */ + which = GRIDHIT_GD(r_items, gd_items) + *thing_scroll; + + if (which < num_things) + { + /* ...and there was something there to click */ + toolopt_changed = 1; #ifndef NOSOUND - if (cur_tool != TOOL_STAMP || stamp_data[stamp_group][which]->ssnd == NULL) - { - playsound(screen, 1, SND_BLEEP, 0, SNDPOS_RIGHT, SNDDIST_NEAR); - } + if (cur_tool != TOOL_STAMP + || stamp_data[stamp_group][which]->ssnd == NULL) + { + playsound(screen, 1, SND_BLEEP, 0, SNDPOS_RIGHT, + SNDDIST_NEAR); + } #endif - cur_thing = which; - do_draw = 1; - } - } - else if (HIT(r_controls)) - { - /* Controls were clicked */ + cur_thing = which; + do_draw = 1; + } + } + else if (HIT(r_controls)) + { + /* Controls were clicked */ - which = GRIDHIT_GD(r_controls, gd_controls); + which = GRIDHIT_GD(r_controls, gd_controls); - if (cur_tool == TOOL_STAMP) - { - if (stamp_tool_mode == STAMP_TOOL_MODE_ROTATE) - { - stamp_xor(stamp_place_x, stamp_place_y); - reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, &stamp_tool_mode); - } + if (cur_tool == TOOL_STAMP) + { + if (stamp_tool_mode == STAMP_TOOL_MODE_ROTATE) + { + stamp_xor(stamp_place_x, stamp_place_y); + reset_stamps(&stamp_xored_rt, &stamp_place_x, + &stamp_place_y, &stamp_tool_mode); + } - /* Stamp controls! */ - int control_sound = -1; + /* Stamp controls! */ + int control_sound = -1; - if (which == 4 || which == 5) - { - /* Grow/Shrink Controls: */ + if (which == 4 || which == 5) + { + /* Grow/Shrink Controls: */ #ifdef OLD_STAMP_GROW_SHRINK - if (which == 5) - { - /* Bottom right button: Grow: */ - if (stamp_data[stamp_group][cur_stamp[stamp_group]]->size < MAX_STAMP_SIZE) - { - stamp_data[stamp_group][cur_stamp[stamp_group]]->size++; - control_sound = SND_GROW; - } - } - else - { - /* Bottom left button: Shrink: */ - if (stamp_data[stamp_group][cur_stamp[stamp_group]]->size > MIN_STAMP_SIZE) - { - stamp_data[stamp_group][cur_stamp[stamp_group]]->size--; - control_sound = SND_SHRINK; - } - } + if (which == 5) + { + /* Bottom right button: Grow: */ + if (stamp_data[stamp_group][cur_stamp[stamp_group]]->size + < MAX_STAMP_SIZE) + { + stamp_data[stamp_group][cur_stamp[stamp_group]]->size++; + control_sound = SND_GROW; + } + } + else + { + /* Bottom left button: Shrink: */ + if (stamp_data[stamp_group][cur_stamp[stamp_group]]->size + > MIN_STAMP_SIZE) + { + stamp_data[stamp_group][cur_stamp[stamp_group]]->size--; + control_sound = SND_SHRINK; + } + } #else - int old_size; + int old_size; #ifdef DEBUG - float choice; + float choice; #endif - old_size = stamp_data[stamp_group][cur_stamp[stamp_group]]->size; + old_size = + stamp_data[stamp_group][cur_stamp[stamp_group]]->size; - stamp_data[stamp_group][cur_stamp[stamp_group]]->size = - (((MAX_STAMP_SIZE - MIN_STAMP_SIZE + 1 - /* +1 to address lack of ability to get back to max default stamp size (SF Bug #1668235 -bjk 2011.01.08) */ - ) * (event.button.x - (WINDOW_WIDTH - r_ttoolopt.w))) / r_ttoolopt.w) + MIN_STAMP_SIZE; + stamp_data[stamp_group][cur_stamp[stamp_group]]->size = + (((MAX_STAMP_SIZE - MIN_STAMP_SIZE + 1 + /* +1 to address lack of ability to get back to max default stamp size (SF Bug #1668235 -bjk 2011.01.08) */ + ) * (event.button.x - + (WINDOW_WIDTH - r_ttoolopt.w))) / r_ttoolopt.w) + + MIN_STAMP_SIZE; #ifdef DEBUG - printf("Old size = %d, Chose %0.4f, New size =%d\n", old_size, choice, - stamp_data[stamp_group][cur_stamp[stamp_group]]->size); + printf("Old size = %d, Chose %0.4f, New size =%d\n", + old_size, choice, + stamp_data[stamp_group][cur_stamp + [stamp_group]]->size); #endif - if (stamp_data[stamp_group][cur_stamp[stamp_group]]->size < old_size) - control_sound = SND_SHRINK; - else if (stamp_data[stamp_group][cur_stamp[stamp_group]]->size > old_size) - control_sound = SND_GROW; -#endif - } - else if (which == 2 || which == 3) - { - /* Mirror/Flip Controls: */ - if (which == 3) - { - /* Top right button: Flip: */ - if (stamp_data[stamp_group][cur_stamp[stamp_group]]->flipable) - { - stamp_data[stamp_group][cur_stamp[stamp_group]]->flipped = - !stamp_data[stamp_group][cur_stamp[stamp_group]]->flipped; - control_sound = SND_FLIP; - } - } - else - { - /* Top left button: Mirror: */ - if (stamp_data[stamp_group][cur_stamp[stamp_group]]->mirrorable) - { - stamp_data[stamp_group][cur_stamp[stamp_group]]->mirrored = - !stamp_data[stamp_group][cur_stamp[stamp_group]]->mirrored; - control_sound = SND_MIRROR; - } - } - } - else - { - /* Prev/Next Controls: */ - - old_stamp_group = stamp_group; - - if (which == 1) - { - /* Next group */ - stamp_group++; - if (stamp_group >= num_stamp_groups) - stamp_group = 0; - control_sound = SND_CLICK; - } - else - { - /* Prev group */ - stamp_group--; - if (stamp_group < 0) - stamp_group = num_stamp_groups - 1; - control_sound = SND_CLICK; - } - - if (stamp_group == old_stamp_group) - control_sound = -1; - else - { - cur_thing = cur_stamp[stamp_group]; - num_things = num_stamps[stamp_group]; - thing_scroll = &(stamp_scroll[stamp_group]); - } - } - - if (control_sound != -1) - { - playsound(screen, 0, control_sound, 0, SNDPOS_CENTER, SNDDIST_NEAR); - draw_stamps(); - update_screen_rect(&r_toolopt); - set_active_stamp(); - update_stamp_xor(0); - stamp_tool_mode = STAMP_TOOL_MODE_PLACE; - } - } - else if (cur_tool == TOOL_MAGIC) - { - /* Magic controls */ - - int grp; - int cur; - - grp = magic_group; - cur = cur_magic[grp]; - - if (which == 0 || which == 1) - { - int tries = 0; - - magic_switchout(canvas); - - /* Magic pagination */ - do - { - tries++; - - if (which == 0) - { - magic_group--; - if (magic_group < 0) - magic_group = MAX_MAGIC_GROUPS - 1; - } - else if (which == 1) - { - magic_group++; - if (magic_group >= MAX_MAGIC_GROUPS) - magic_group = 0; - } - } - while (num_magics[magic_group] == 0 && tries < MAX_MAGIC_GROUPS); - - keybd_flag = 0; - cur_thing = cur_magic[magic_group]; - num_things = num_magics[magic_group]; - thing_scroll = &(magic_scroll[magic_group]); - magic_current_snd_ptr = NULL; - - draw_magic(); - update_screen_rect(&r_toolopt); - - draw_colors(magics[magic_group][cur_thing].colors); - - if (magics[magic_group][cur_thing].colors) - magic_funcs[magics[magic_group][cur_thing].handle_idx].set_color(magic_api_struct, - color_hexes[cur_color][0], - color_hexes[cur_color][1], - color_hexes[cur_color][2]); - - magic_switchin(canvas); - - playsound(screen, 0, SND_CLICK, 0, SNDPOS_CENTER, SNDDIST_NEAR); - } - else - { - /* Magic controls! */ - if (which == 3 && magics[grp][cur].avail_modes & MODE_FULLSCREEN) - { - magic_switchout(canvas); - magics[grp][cur].mode = MODE_FULLSCREEN; - magic_switchin(canvas); - draw_magic(); - update_screen_rect(&r_toolopt); - } - else if (which == 2 && magics[grp][cur].avail_modes & MODE_PAINT) - { - magic_switchout(canvas); - magics[grp][cur].mode = MODE_PAINT; - magic_switchin(canvas); - draw_magic(); - update_screen_rect(&r_toolopt); - } - else if (which == 2 && magics[grp][cur].avail_modes & MODE_PAINT_WITH_PREVIEW) - { - magic_switchout(canvas); - magics[grp][cur].mode = MODE_PAINT_WITH_PREVIEW; - magic_switchin(canvas); - draw_magic(); - update_screen_rect(&r_toolopt); - } - else if (which == 2 && magics[grp][cur].avail_modes & MODE_ONECLICK) - { - magic_switchout(canvas); - magics[grp][cur].mode = MODE_ONECLICK; - magic_switchin(canvas); - draw_magic(); - update_screen_rect(&r_toolopt); - } - /* FIXME: Sfx */ - } - } - else if (cur_tool == TOOL_SHAPES) - { - /* Shape controls! */ - shape_mode = which; - draw_shapes(); - update_screen_rect(&r_toolopt); - draw_tux_text(TUX_GREAT, shapemode_tips[shape_mode], 1); - playsound(screen, 0, SND_CLICK, 0, SNDPOS_RIGHT, SNDDIST_NEAR); - update_screen_rect(&r_tuxarea); - toolopt_changed = 0; - } - else if (cur_tool == TOOL_TEXT) - { - /* Text controls! */ - int control_sound = -1; - - if (which & 2) - { - /* One of the bottom buttons: */ - if (which & 1) - { - /* Bottom right button: Grow: */ - if (text_size < MAX_TEXT_SIZE) - { - text_size++; - control_sound = SND_GROW; - toolopt_changed = 1; - } - } - else - { - /* Bottom left button: Shrink: */ - if (text_size > MIN_TEXT_SIZE) - { - text_size--; - control_sound = SND_SHRINK; - toolopt_changed = 1; - } - } - } - else - { - /* One of the top buttons: */ - if (which & 1) - { - /* Top right button: Italic: */ - if (text_state & TTF_STYLE_ITALIC) - { - text_state &= ~TTF_STYLE_ITALIC; - control_sound = SND_ITALIC_ON; - } - else - { - text_state |= TTF_STYLE_ITALIC; - control_sound = SND_ITALIC_OFF; - } - } - else - { - /* Top left button: Bold: */ - if (text_state & TTF_STYLE_BOLD) - { - text_state &= ~TTF_STYLE_BOLD; - control_sound = SND_THIN; - } - else - { - text_state |= TTF_STYLE_BOLD; - control_sound = SND_THICK; - } - } - toolopt_changed = 1; - } - if (control_sound != -1) - { - playsound(screen, 0, control_sound, 0, SNDPOS_CENTER, SNDDIST_NEAR); - - - if (cur_tool == TOOL_TEXT) /* Huh? It had better be! */ - { - /* need to invalidate all the cached user fonts, causing reload on demand */ - - int i; - - for (i = 0; i < num_font_families; i++) - { - if (user_font_families[i] && user_font_families[i]->handle) - { - TuxPaint_Font_CloseFont(user_font_families[i]->handle); - user_font_families[i]->handle = NULL; - } - } - draw_fonts(); - update_screen_rect(&r_toolopt); - } - } - } - else if (cur_tool == TOOL_LABEL) - { - /* Label controls! */ - int control_sound = -1; - - if (which & 4) - { - /* One of the bottom buttons: */ - if (which & 1) - { - /* Bottom right button: Grow: */ - if (text_size < MAX_TEXT_SIZE) - { - text_size++; - control_sound = SND_GROW; - toolopt_changed = 1; - } - } - else - { - /* Bottom left button: Shrink: */ - if (text_size > MIN_TEXT_SIZE) - { - text_size--; - control_sound = SND_SHRINK; - toolopt_changed = 1; - } - } - } - else - { - if (which & 2) - { - /* One of the middle buttons: */ - if (which & 1) - { - /* right button: Italic: */ - if (text_state & TTF_STYLE_ITALIC) - { - text_state &= ~TTF_STYLE_ITALIC; - control_sound = SND_ITALIC_ON; - } - else - { - text_state |= TTF_STYLE_ITALIC; - control_sound = SND_ITALIC_OFF; - } - } - else - { - /* middle left button: Bold: */ - if (text_state & TTF_STYLE_BOLD) - { - text_state &= ~TTF_STYLE_BOLD; - control_sound = SND_THIN; - } - else - { - text_state |= TTF_STYLE_BOLD; - control_sound = SND_THICK; - } - } - toolopt_changed = 1; - } - - else - { - /* One of the top buttons: */ - if (which & 1) - { - /* Top right: Select button: */ - if (cur_label == LABEL_SELECT) - { - /* Already in label select mode; turn it off */ - cur_label = LABEL_LABEL; - update_canvas(0, 0, WINDOW_WIDTH - r_ttoolopt.w, (button_h * buttons_tall) + r_ttoolopt.h); - if (onscreen_keyboard) - { - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); - } - - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput(); - - } - draw_tux_text(TUX_GREAT, tool_tips[TOOL_LABEL], 1); - playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - } - else - { - /* Want to select a label */ - if (are_labels()) - { - update_canvas_ex_r(kbd_rect.x - r_ttools.w, kbd_rect.y, - kbd_rect.x + kbd_rect.w, kbd_rect.y + kbd_rect.h, - 1); - if (texttool_len > 0) - { - rec_undo_buffer(); - do_render_cur_text(1); - texttool_len = 0; - cursor_textwidth = 0; - label_node_to_edit = NULL; - } - else if (label_node_to_edit) - { - rec_undo_buffer(); - have_to_rec_label_node = TRUE; - add_label_node(0, 0, 0, 0, NULL); - label_node_to_edit = NULL; - } - - cur_label = LABEL_SELECT; - highlight_label_nodes(); - - draw_tux_text(TUX_GREAT, TIP_LABEL_SELECTOR_ENABLED, 1); - playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - } - } - toolopt_changed = 1; - } - else - { - /* Top left: "Apply" label */ - if (cur_label == LABEL_APPLY) - { - /* Already in label apply mode; turn it off */ - cur_label = LABEL_LABEL; - update_canvas(0, 0, WINDOW_WIDTH - r_ttoolopt.w, (button_h * buttons_tall) + r_ttoolopt.h); - if (onscreen_keyboard) - { - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); - } - - draw_tux_text(TUX_GREAT, tool_tips[TOOL_LABEL], 1); - playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - } - else - { - /* Want to apply a label */ - if (are_labels()) - { - update_canvas_ex_r(kbd_rect.x - r_ttools.w, kbd_rect.y, - kbd_rect.x + kbd_rect.w, kbd_rect.y + kbd_rect.h, - 1); - if (texttool_len > 0) - { - rec_undo_buffer(); - do_render_cur_text(1); - texttool_len = 0; - cursor_textwidth = 0; - label_node_to_edit = NULL; - } - else if (label_node_to_edit) - { - rec_undo_buffer(); - have_to_rec_label_node = TRUE; - add_label_node(0, 0, 0, 0, NULL); - label_node_to_edit = NULL; - } - - cur_label = LABEL_APPLY; - highlight_label_nodes(); - - draw_tux_text(TUX_GREAT, TIP_LABEL_APPLIER_ENABLED, 1); - playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - } - } - - toolopt_changed = 1; - } - } - } - - if (control_sound != -1) - { - playsound(screen, 0, control_sound, 0, SNDPOS_CENTER, SNDDIST_NEAR); - - - if (cur_tool == TOOL_LABEL) /* Huh? It had better be! */ - { - /* need to invalidate all the cached user fonts, causing reload on demand */ - - int i; - - for (i = 0; i < num_font_families; i++) - { - if (user_font_families[i] && user_font_families[i]->handle) - { - TuxPaint_Font_CloseFont(user_font_families[i]->handle); - user_font_families[i]->handle = NULL; - } - } - draw_fonts(); - update_screen_rect(&r_toolopt); - } - } - draw_fonts(); - update_screen_rect(&r_toolopt); - } - else if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) - { - /* Brush spacing */ - - int prev_size, chosen, new_size, frame_w, w, h, control_sound; - - prev_size = brushes_spacing[cur_brush]; - chosen = ((BRUSH_SPACING_SIZES + 1) * (event.button.x - r_ttoolopt.x)) / r_ttoolopt.w; - - frame_w = img_brushes[cur_brush]->w / abs(brushes_frames[cur_brush]); - w = frame_w / (brushes_directional[cur_brush] ? 3 : 1); - h = img_brushes[cur_brush]->h / (brushes_directional[cur_brush] ? 3 : 1); - - /* Spacing ranges from 0px to "N x the max dimension of the brush" - (so a 48x48 brush would have a spacing of 48 if the center option is chosen) */ - new_size = (chosen * max(w, h) * BRUSH_SPACING_MAX_MULTIPLIER) / BRUSH_SPACING_SIZES; - - if (new_size != prev_size) - { - char tmp_tip[256]; - int numer, denom; - - brushes_spacing[cur_brush] = new_size; - draw_brushes_spacing(); - update_screen_rect(&r_toolopt); - - if (new_size < prev_size) - control_sound = SND_SHRINK; - else - control_sound = SND_GROW; - - if (new_size == 0) - { - draw_tux_text(TUX_GREAT, TIP_BRUSH_SPACING_ZERO, 1); - } - else if (new_size / max(w, h) == 1) - { - draw_tux_text(TUX_GREAT, TIP_BRUSH_SPACING_SAME, 1); - } - else if (new_size > max(w, h)) - { - double ratio, i, f; - - ratio = (float) new_size / (float) max(w, h); - f = modf(ratio, &i); - - if (f > (SLOPPY_FRAC_MAX - SLOPPY_FRAC_MIN) / SLOPPY_FRAC_MAX) - { - i++; - f = 0.0; - } - else if (f < SLOPPY_FRAC_MIN / SLOPPY_FRAC_MAX) - { - f = 0.0; - } - - if (f == 0.0) - { - snprintf(tmp_tip, sizeof(tmp_tip), gettext(TIP_BRUSH_SPACING_MORE), (int) i); - } - else - { - sloppy_frac(f, &numer, &denom); - - snprintf(tmp_tip, sizeof(tmp_tip), gettext(TIP_BRUSH_SPACING_MORE_FRAC), (int) i, numer, denom); - } - - draw_tux_text(TUX_GREAT, tmp_tip, 1); - } - else if (new_size < max(w, h)) - { - sloppy_frac((float) new_size / (float) max(w, h), &numer, &denom); - snprintf(tmp_tip, sizeof(tmp_tip), gettext(TIP_BRUSH_SPACING_LESS), numer, denom); - draw_tux_text(TUX_GREAT, tmp_tip, 1); - } - - playsound(screen, 0, control_sound, 0, SNDPOS_CENTER, SNDDIST_NEAR); - } - } - } - else - { - /* Scroll buttons */ - int is_upper = event.button.y < r_toolopt.y + button_h / 2; - - if ((is_upper && *thing_scroll > 0) /* upper arrow */ - || (!is_upper && *thing_scroll / gd_items.cols < num_rows_needed - gd_items.rows) /* lower arrow */ - ) - { - *thing_scroll += is_upper ? -gd_items.cols : gd_items.cols; - do_draw = 1; - playsound(screen, 1, SND_SCROLL, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - - if (scrolltimer_selector != TIMERID_NONE) - { - SDL_RemoveTimer(scrolltimer_selector); - scrolltimer_selector = TIMERID_NONE; - } - - if (!scrolling_selector && event.type == SDL_MOUSEBUTTONDOWN) - { - DEBUG_PRINTF("Starting scrolling\n"); - memcpy(&scrolltimer_selector_event, &event, sizeof(SDL_Event)); - scrolltimer_selector_event.type = TP_SDL_MOUSEBUTTONSCROLL; - - /* - * We enable the timer subsystem only when needed (e.g., to use SDL_AddTimer() needed - * for scrolling) then disable it immediately after (e.g., after the timer has fired or - * after SDL_RemoveTimer()) because enabling the timer subsystem in SDL1 has a high - * energy impact on the Mac. - */ - - scrolling_selector = 1; - SDL_InitSubSystem(SDL_INIT_TIMER); - scrolltimer_selector = - SDL_AddTimer(REPEAT_SPEED, scrolltimer_selector_callback, (void *)&scrolltimer_selector_event); - } - else - { - DEBUG_PRINTF("Continuing scrolling\n"); - scrolltimer_selector = - SDL_AddTimer(REPEAT_SPEED / 3, scrolltimer_selector_callback, (void *)&scrolltimer_selector_event); - } - - if (*thing_scroll == 0 || *thing_scroll / gd_items.cols == num_rows_needed - gd_items.rows) - { - do_setcursor(cursor_arrow); - if (scrolling_selector) - { - if (scrolltimer_selector != TIMERID_NONE) - { - SDL_RemoveTimer(scrolltimer_selector); - scrolltimer_selector = TIMERID_NONE; - } - scrolling_selector = 0; - SDL_QuitSubSystem(SDL_INIT_TIMER); - } - } - } - } - - - /* Assign the change(s), if any / redraw, if needed: */ - - if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) - { - cur_brush = cur_thing; - render_brush(); - - if (do_draw) - { - draw_brushes(); - show_brush_tip(); - } - } - else if (cur_tool == TOOL_ERASER) - { - cur_eraser = cur_thing; - - if (do_draw) - draw_erasers(); - } - else if (cur_tool == TOOL_FILL) - { - cur_fill = cur_thing; - draw_tux_text(TUX_GREAT, fill_tips[cur_fill], 1); - - if (do_draw) - draw_fills(); - } - else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - { - /* FIXME */ - /* char font_tux_text[512]; */ - - cur_font = cur_thing; - - /* FIXME */ - /* - safe_snprintf(font_tux_text, sizeof font_tux_text, "%s (%s).", - TTF_FontFaceFamilyName(getfonthandle(cur_font)), - TTF_FontFaceStyleName(getfonthandle(cur_font))); - draw_tux_text(TUX_GREAT, font_tux_text, 1); - */ - - if (do_draw) - draw_fonts(); - - - /* Only rerender when picking a different font */ - if (toolopt_changed) - { - draw_fonts(); - if (select_changed) - { - rec_undo_buffer(); - do_render_cur_text(1); - texttool_len = 0; - } - else - { - do_render_cur_text(0); - } - } - } - else if (cur_tool == TOOL_STAMP) - { -#ifndef NOSOUND - /* Only play when picking a different stamp */ - if (toolopt_changed && !mute) - { - /* If the sound hasn't been loaded yet, do it now */ - - if (!stamp_data[stamp_group][cur_thing]->sound_processed) - { - get_stamp_thumb(stamp_data[stamp_group][cur_thing], 1); - } - - /* If there's an SFX, play it! */ - - if (stamp_data[stamp_group][cur_thing]->ssnd != NULL) - { - Mix_ChannelFinished(NULL); /* Prevents multiple clicks from toggling between SFX and desc sound, rather than always playing SFX first, then desc sound... */ - - Mix_PlayChannel(2, stamp_data[stamp_group][cur_thing]->ssnd, 0); - - /* If there's a description sound, play it after the SFX! */ - - if (stamp_data[stamp_group][cur_thing]->sdesc != NULL) - { - Mix_ChannelFinished(playstampdesc); - } - } - else - { - /* No SFX? If there's a description sound, play it now! */ - - if (stamp_data[stamp_group][cur_thing]->sdesc != NULL) - { - Mix_PlayChannel(2, stamp_data[stamp_group][cur_thing]->sdesc, 0); - } - } - } -#endif - - if (cur_thing != cur_stamp[stamp_group]) - { - if (stamp_tool_mode == STAMP_TOOL_MODE_ROTATE) - { - stamp_xor(stamp_place_x, stamp_place_y); - reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, &stamp_tool_mode); - } - cur_stamp[stamp_group] = cur_thing; - set_active_stamp(); - update_stamp_xor(0); - stamp_tool_mode = STAMP_TOOL_MODE_PLACE; - } - - if (do_draw) - draw_stamps(); - - if (stamp_data[stamp_group][cur_stamp[stamp_group]]->stxt != NULL) - { -#ifdef DEBUG - printf("stamp_data[stamp_group][cur_stamp[stamp_group]]->stxt = %s\n", - stamp_data[stamp_group][cur_stamp[stamp_group]]->stxt); -#endif - - draw_tux_text_ex(TUX_GREAT, stamp_data[stamp_group][cur_stamp[stamp_group]]->stxt, 1, - stamp_data[stamp_group][cur_stamp[stamp_group]]->locale_text); - } - else - draw_tux_text(TUX_GREAT, "", 0); - - /* Enable or disable color selector: */ - draw_colors(stamp_colorable(cur_stamp[stamp_group]) - || stamp_tintable(cur_stamp[stamp_group])); - if (!scrolling_selector) - { - stamp_xor(canvas->w / 2, canvas->h / 2); - stamp_xored = 1; - stamp_size_selector_clicked = 1; - update_screen(canvas->w / 2 - (CUR_STAMP_W + 1) / 2 + r_canvas.x, - canvas->h / 2 - (CUR_STAMP_H + 1) / 2 + r_canvas.y, - canvas->w / 2 + (CUR_STAMP_W + 1) / 2 + r_canvas.x, - canvas->h / 2 + (CUR_STAMP_H + 1) / 2 + r_canvas.y); - } - } - else if (cur_tool == TOOL_SHAPES) - { - cur_shape = cur_thing; - - /* Remove ghost previews an reset the tool */ - if (shape_tool_mode != SHAPE_TOOL_MODE_DONE) - { - shape_tool_mode = SHAPE_TOOL_MODE_DONE; - do_undo(); - tool_avail[TOOL_REDO] = 0; /* Don't let them 'redo' to get preview back */ - draw_toolbar(); - update_screen_rect(&r_tools); - update_canvas(0, 0, canvas->w, canvas->h); - } - - if (toolopt_changed) - draw_tux_text(TUX_GREAT, shape_tips[cur_shape], 1); - - if (do_draw) - draw_shapes(); - } - else if (cur_tool == TOOL_MAGIC) - { - int grp; - int cur; - - grp = magic_group; - cur = cur_magic[grp]; - - if (cur_thing != cur) - { - cur = cur_thing; - magic_switchout(canvas); - - cur_magic[grp] = cur_thing; - draw_colors(magics[grp][cur].colors); - - if (magics[grp][cur].colors) - magic_funcs[magics[grp][cur].handle_idx].set_color(magic_api_struct, - color_hexes[cur_color][0], - color_hexes[cur_color][1], - color_hexes[cur_color][2]); - - magic_switchin(canvas); - } - - draw_tux_text(TUX_GREAT, magics[grp][cur].tip[magic_modeint(magics[grp][cur].mode)], 1); - - if (do_draw) - draw_magic(); - } - - /* Update the screen: */ - if (do_draw) - update_screen_rect(&r_toolopt); - } - } - else if (HIT(r_colors) && colors_are_selectable) - { - /* Color! */ - whichc = GRIDHIT_GD(r_colors, gd_colors); - - if (valid_click(event.button.button)) - { - // magic_switchout(canvas); - - if (whichc >= 0 && whichc < NUM_COLORS) - { - int old_color; - - old_color = cur_color; - cur_color = whichc; - draw_tux_text(TUX_KISS, color_names[cur_color], 1); - - if (cur_color == (unsigned) COLOR_PICKER || cur_color == (unsigned) COLOR_SELECTOR || cur_color == (unsigned) COLOR_MIXER) - { - int chose_color; - - disable_avail_tools(); - draw_toolbar(); - draw_colors(COLORSEL_CLOBBER_WIPE); - draw_none(); - - chose_color = 0; - if (cur_color == (unsigned) COLOR_PICKER) - chose_color = do_color_picker(); - else if (cur_color == (unsigned) COLOR_SELECTOR) - chose_color = do_color_sel(0); - else if (cur_color == (unsigned) COLOR_MIXER) - { - chose_color = do_color_mix(); - if (!chose_color) - cur_color = old_color; - } - - if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - { - if (onscreen_keyboard && kbd) - { - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); - } - - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput(); - } - } - - enable_avail_tools(); - draw_toolbar(); - update_screen_rect(&r_tools); - - draw_cur_tool_tip(); - - draw_colors(COLORSEL_FORCE_REDRAW); - - if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) - draw_brushes(); - else if (cur_tool == TOOL_MAGIC) - draw_magic(); - else if (cur_tool == TOOL_STAMP) - { - if (stamp_tool_mode == STAMP_TOOL_MODE_ROTATE) - stamp_xor(stamp_place_x, stamp_place_y); - else if (stamp_xored) - { - stamp_xor(canvas->w / 2, canvas->h / 2); - stamp_xored = 0; - update_screen(canvas->w / 2 - (CUR_STAMP_W + 1) / 2 + r_canvas.x, - canvas->h / 2 - (CUR_STAMP_H + 1) / 2 + r_canvas.y, - canvas->w / 2 + (CUR_STAMP_W + 1) / 2 + r_canvas.x, - canvas->h / 2 + (CUR_STAMP_H + 1) / 2 + r_canvas.y); - } - draw_stamps(); - } - else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - draw_fonts(); - else if (cur_tool == TOOL_SHAPES) - draw_shapes(); - else if (cur_tool == TOOL_ERASER) - draw_erasers(); - else if (cur_tool == TOOL_FILL) - draw_fills(); - - if (chose_color) - playsound(screen, 1, SND_BUBBLE, 1, SNDPOS_CENTER, SNDDIST_NEAR); - else - playsound(screen, 1, SND_CLICK, 1, SNDPOS_CENTER, SNDDIST_NEAR); - - SDL_Flip(screen); - } - else - { - draw_colors(COLORSEL_REFRESH); - - playsound(screen, 1, SND_BUBBLE, 1, event.button.x, SNDDIST_NEAR); - } - - handle_color_changed(); - } - } - } - else if (HIT(r_canvas) && valid_click(event.button.button) && keyglobal == 0) - { - const Uint8 * kbd_state; - - kbd_state = SDL_GetKeyboardState(NULL); - - if ((kbd_state[SDL_SCANCODE_LCTRL] || kbd_state[SDL_SCANCODE_RCTRL]) && colors_are_selectable) - { - int chose_color; - - /* Holding [Ctrl] while clicking; switch to temp-mode color selector! */ - chose_color = do_color_sel(1); - - draw_cur_tool_tip(); - draw_colors(COLORSEL_FORCE_REDRAW); - - if (chose_color) - { - playsound(screen, 1, SND_BUBBLE, 1, SNDPOS_CENTER, SNDDIST_NEAR); - cur_color = COLOR_SELECTOR; - handle_color_changed(); - } - else - playsound(screen, 1, SND_CLICK, 1, SNDPOS_CENTER, SNDDIST_NEAR); - - SDL_Flip(screen); - } + if (stamp_data[stamp_group][cur_stamp[stamp_group]]->size < + old_size) + control_sound = SND_SHRINK; else + if (stamp_data[stamp_group][cur_stamp[stamp_group]]->size + > old_size) + control_sound = SND_GROW; +#endif + } + else if (which == 2 || which == 3) + { + /* Mirror/Flip Controls: */ + if (which == 3) + { + /* Top right button: Flip: */ + if (stamp_data[stamp_group] + [cur_stamp[stamp_group]]->flipable) { - /* Draw something! */ - old_x = event.button.x - r_canvas.x; - old_y = event.button.y - r_canvas.y; - /* if (old_y < r_canvas.h/2) */ - /* { */ - /* keybd_position = 0; */ - /* } */ - /* else */ - /* { */ - /* keybd_position = 1; */ - /* } */ - - if (been_saved) - { - been_saved = 0; - - if (!disable_save) - tool_avail[TOOL_SAVE] = 1; - - draw_toolbar(); - update_screen_rect(&r_tools); - } - - if (cur_tool == TOOL_BRUSH) - { - /* Start painting! */ - if (!emulate_button_pressed) - rec_undo_buffer(); - - /* (Arbitrarily large, so we draw once now) */ - reset_brush_counter(); - - /* brush_draw(old_x, old_y, old_x, old_y, 1); fixes SF #1934883? */ - playsound(screen, 0, paintsound(img_cur_brush_w), 1, event.button.x, SNDDIST_NEAR); - - if (mouseaccessibility) - emulate_button_pressed = !emulate_button_pressed; - } - else if (cur_tool == TOOL_LINES) - { - /* Start a line! */ - - if (!emulate_button_pressed) - { - rec_undo_buffer(); - - line_start_x = old_x; - line_start_y = old_y; - - /* (Arbitrarily large, so we draw once now) */ - reset_brush_counter(); - - /* brush_draw(old_x, old_y, old_x, old_y, 1); fixes sf #1934883? */ - - playsound(screen, 1, SND_LINE_START, 1, event.button.x, SNDDIST_NEAR); - draw_tux_text(TUX_BORED, TIP_LINE_START, 1); - } - if (mouseaccessibility) - emulate_button_pressed = !emulate_button_pressed; - } - else if (cur_tool == TOOL_SHAPES) - { - if (shape_tool_mode == SHAPE_TOOL_MODE_DONE) - { - /* Start drawing a shape! */ - - rec_undo_buffer(); - - shape_start_x = old_x; - shape_start_y = old_y; - - shape_tool_mode = SHAPE_TOOL_MODE_STRETCH; - - playsound(screen, 1, SND_LINE_START, 1, event.button.x, SNDDIST_NEAR); - draw_tux_text(TUX_BORED, TIP_SHAPE_START, 1); - if (mouseaccessibility) - emulate_button_pressed = 1; - } - else if (shape_tool_mode == SHAPE_TOOL_MODE_ROTATE) - { - /* Draw the shape with the brush! */ - - /* Only draw here in mouse accessibility mode as there IS a mouse */ - /* See #300881 for the reasons that this is deplaced to draw in mouse release */ - if (mouseaccessibility) - { - /* (Arbitrarily large...) */ - reset_brush_counter(); - - playsound(screen, 1, SND_LINE_END, 1, event.button.x, SNDDIST_NEAR); - do_shape(shape_start_x, shape_start_y, shape_current_x, shape_current_y, - shape_rotation(shape_start_x, shape_start_y, - event.button.x - r_canvas.x, event.button.y - r_canvas.y), 1); - - shape_tool_mode = SHAPE_TOOL_MODE_DONE; - draw_tux_text(TUX_GREAT, shape_tool_tips[simple_shapes ? SHAPE_COMPLEXITY_SIMPLE : SHAPE_COMPLEXITY_NORMAL], 1); - } - } - else if (shape_tool_mode == SHAPE_TOOL_MODE_STRETCH) - /* Only reached in accessibility mode */ - emulate_button_pressed = 0; - } - else if (cur_tool == TOOL_MAGIC) - { - int grp; - int cur; - - grp = magic_group; - cur = cur_magic[grp]; - - if (!emulate_button_pressed) - { - int undo_ctr; - SDL_Surface *last; - - /* Start doing magic! */ - - /* These switchout/in are here for Magic tools that abuse the canvas - by drawing widgets on them; you don't want the widgets recorded as part - of the canvas in the undo buffer! - HOWEVER, as Pere noted in 2010.March, this causes many 'normal' Magic - tools to not work right, because they lose their record of the 'original' - canvas, before the user started using the tool (e.g., Rails, Perspective, Zoom). - FIXME: Some in-between solution is needed (a 'clean up the canvas'/'dirty the canvas' - pair of functions for the widgety abusers?) -bjk 2010.03.22 */ - - /* magic_switchout(canvas); *//* <-- FIXME: I dislike this -bjk 2009.10.13 */ - rec_undo_buffer(); - /* magic_switchin(canvas); *//* <-- FIXME: I dislike this -bjk 2009.10.13 */ - - if (cur_undo > 0) - undo_ctr = cur_undo - 1; - else - undo_ctr = NUM_UNDO_BUFS - 1; - - last = undo_bufs[undo_ctr]; - - update_rect.x = 0; - update_rect.y = 0; - update_rect.w = 0; - update_rect.h = 0; - - reset_touched(); - - magic_funcs[magics[grp][cur].handle_idx].click(magic_api_struct, - magics[grp][cur].idx, - magics[grp][cur].mode, - canvas, last, old_x, old_y, &update_rect); - - draw_tux_text(TUX_GREAT, magics[grp][cur].tip[magic_modeint(magics[grp][cur].mode)], 1); - - update_canvas(update_rect.x, update_rect.y, - update_rect.x + update_rect.w, update_rect.y + update_rect.h); - } - - if (mouseaccessibility) - { - if (magics[grp][cur].mode != MODE_FULLSCREEN && magics[grp][cur].mode != MODE_ONECLICK) /* Note: some non-fullscreen tools are also click-only (not click-and-drag) -bjk 2011.04.26 */ - emulate_button_pressed = !emulate_button_pressed; - } - } - else if (cur_tool == TOOL_ERASER) - { - /* Erase! */ - if (!emulate_button_pressed) - rec_undo_buffer(); - - do_eraser(old_x, old_y, 1); - - if (mouseaccessibility) - emulate_button_pressed = !emulate_button_pressed; - } - else if (cur_tool == TOOL_FILL) - { - Uint32 draw_color, canv_color; - - /* Fill */ - - draw_color = SDL_MapRGB(canvas->format, - color_hexes[cur_color][0], - color_hexes[cur_color][1], - color_hexes[cur_color][2]); - canv_color = getpixels[canvas->format->BytesPerPixel] (canvas, old_x, old_y); - - fill_x = old_x; - fill_y = old_y; - - if (would_flood_fill(canvas, draw_color, canv_color)) - { - int x1, y1, x2, y2; - SDL_Surface * last; - int undo_ctr; - - /* We only bother recording an undo buffer - (which may kill our redos) if we're about - to actually change the picture */ - rec_undo_buffer(); - x1 = x2 = old_x; - y1 = y2 = old_y; - - if (cur_undo > 0) - undo_ctr = cur_undo - 1; - else - undo_ctr = NUM_UNDO_BUFS - 1; - - last = undo_bufs[undo_ctr]; - - - for (y1 = 0; y1 < canvas->h; y1++) { - for (x1 = 0; x1 < canvas->w; x1++) { - sim_flood_touched[(y1 * canvas->w) + x1] = 0; - } - } - - if (cur_fill == FILL_FLOOD) - { - /* Flood fill a solid color */ - do_flood_fill(screen, texture, renderer, last, canvas, old_x, old_y, draw_color, canv_color, &x1, &y1, &x2, &y2, sim_flood_touched); - - update_canvas(x1, y1, x2, y2); - } - else - { - SDL_Surface * tmp_canvas; - - tmp_canvas = SDL_CreateRGBSurface(canvas->flags, - canvas->w, canvas->h, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, canvas->format->Amask); - SDL_BlitSurface(canvas, NULL, tmp_canvas, NULL); - - simulate_flood_fill(screen, texture, renderer, last, tmp_canvas, old_x, old_y, draw_color, canv_color, &x1, &y1, &x2, &y2, sim_flood_touched); - SDL_FreeSurface(tmp_canvas); - - sim_flood_x1 = x1; - sim_flood_y1 = y1; - sim_flood_x2 = x2; - sim_flood_y2 = y2; - - if (cur_fill == FILL_GRADIENT_RADIAL) - { - /* Radial gradient */ - draw_radial_gradient(canvas, sim_flood_x1, sim_flood_y1, sim_flood_x2, sim_flood_y2, - old_x, old_y, draw_color, sim_flood_touched); - } - else if (cur_fill == FILL_GRADIENT_LINEAR) - { - /* Start a linear gradient */ - draw_linear_gradient(canvas, canvas, sim_flood_x1, sim_flood_y1, sim_flood_x2, sim_flood_y2, - fill_x, fill_y, old_x, old_y + 1, draw_color, sim_flood_touched); - fill_drag_started = 1; - } - else if (cur_fill == FILL_BRUSH) - { - /* Start painting within the fill area */ - draw_brush_fill(canvas, sim_flood_x1, sim_flood_y1, sim_flood_x2, sim_flood_y2, - fill_x, fill_y, old_x, old_y, draw_color, sim_flood_touched, &x1, &y1, &x2, &y2); - } - - update_canvas(x1, y1, x2, y2); - } - - draw_tux_text(TUX_GREAT, fill_tips[cur_fill], 1); - } - } - - else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + stamp_data[stamp_group][cur_stamp[stamp_group]]->flipped + = + !stamp_data[stamp_group][cur_stamp + [stamp_group]]->flipped; + control_sound = SND_FLIP; + } + } + else + { + /* Top left button: Mirror: */ + if (stamp_data[stamp_group] + [cur_stamp[stamp_group]]->mirrorable) { - if (onscreen_keyboard && !kbd) + stamp_data[stamp_group][cur_stamp + [stamp_group]]->mirrored = + !stamp_data[stamp_group][cur_stamp + [stamp_group]]->mirrored; + control_sound = SND_MIRROR; + } + } + } + else + { + /* Prev/Next Controls: */ + + old_stamp_group = stamp_group; + + if (which == 1) + { + /* Next group */ + stamp_group++; + if (stamp_group >= num_stamp_groups) + stamp_group = 0; + control_sound = SND_CLICK; + } + else + { + /* Prev group */ + stamp_group--; + if (stamp_group < 0) + stamp_group = num_stamp_groups - 1; + control_sound = SND_CLICK; + } + + if (stamp_group == old_stamp_group) + control_sound = -1; + else + { + cur_thing = cur_stamp[stamp_group]; + num_things = num_stamps[stamp_group]; + thing_scroll = &(stamp_scroll[stamp_group]); + } + } + + if (control_sound != -1) + { + playsound(screen, 0, control_sound, 0, SNDPOS_CENTER, + SNDDIST_NEAR); + draw_stamps(); + update_screen_rect(&r_toolopt); + set_active_stamp(); + update_stamp_xor(0); + stamp_tool_mode = STAMP_TOOL_MODE_PLACE; + } + } + else if (cur_tool == TOOL_MAGIC) + { + /* Magic controls */ + + int grp; + int cur; + + grp = magic_group; + cur = cur_magic[grp]; + + if (which == 0 || which == 1) + { + int tries = 0; + + magic_switchout(canvas); + + /* Magic pagination */ + do + { + tries++; + + if (which == 0) + { + magic_group--; + if (magic_group < 0) + magic_group = MAX_MAGIC_GROUPS - 1; + } + else if (which == 1) + { + magic_group++; + if (magic_group >= MAX_MAGIC_GROUPS) + magic_group = 0; + } + } + while (num_magics[magic_group] == 0 + && tries < MAX_MAGIC_GROUPS); + + keybd_flag = 0; + cur_thing = cur_magic[magic_group]; + num_things = num_magics[magic_group]; + thing_scroll = &(magic_scroll[magic_group]); + magic_current_snd_ptr = NULL; + + draw_magic(); + update_screen_rect(&r_toolopt); + + draw_colors(magics[magic_group][cur_thing].colors); + + if (magics[magic_group][cur_thing].colors) + magic_funcs[magics[magic_group][cur_thing].handle_idx]. + set_color(magic_api_struct, color_hexes[cur_color][0], + color_hexes[cur_color][1], + color_hexes[cur_color][2]); + + magic_switchin(canvas); + + playsound(screen, 0, SND_CLICK, 0, SNDPOS_CENTER, + SNDDIST_NEAR); + } + else + { + /* Magic controls! */ + if (which == 3 + && magics[grp][cur].avail_modes & MODE_FULLSCREEN) + { + magic_switchout(canvas); + magics[grp][cur].mode = MODE_FULLSCREEN; + magic_switchin(canvas); + draw_magic(); + update_screen_rect(&r_toolopt); + } + else if (which == 2 + && magics[grp][cur].avail_modes & MODE_PAINT) + { + magic_switchout(canvas); + magics[grp][cur].mode = MODE_PAINT; + magic_switchin(canvas); + draw_magic(); + update_screen_rect(&r_toolopt); + } + else if (which == 2 + && magics[grp][cur].avail_modes & + MODE_PAINT_WITH_PREVIEW) + { + magic_switchout(canvas); + magics[grp][cur].mode = MODE_PAINT_WITH_PREVIEW; + magic_switchin(canvas); + draw_magic(); + update_screen_rect(&r_toolopt); + } + else if (which == 2 + && magics[grp][cur].avail_modes & MODE_ONECLICK) + { + magic_switchout(canvas); + magics[grp][cur].mode = MODE_ONECLICK; + magic_switchin(canvas); + draw_magic(); + update_screen_rect(&r_toolopt); + } + /* FIXME: Sfx */ + } + } + else if (cur_tool == TOOL_SHAPES) + { + /* Shape controls! */ + shape_mode = which; + draw_shapes(); + update_screen_rect(&r_toolopt); + draw_tux_text(TUX_GREAT, shapemode_tips[shape_mode], 1); + playsound(screen, 0, SND_CLICK, 0, SNDPOS_RIGHT, + SNDDIST_NEAR); + update_screen_rect(&r_tuxarea); + toolopt_changed = 0; + } + else if (cur_tool == TOOL_TEXT) + { + /* Text controls! */ + int control_sound = -1; + + if (which & 2) + { + /* One of the bottom buttons: */ + if (which & 1) + { + /* Bottom right button: Grow: */ + if (text_size < MAX_TEXT_SIZE) + { + text_size++; + control_sound = SND_GROW; + toolopt_changed = 1; + } + } + else + { + /* Bottom left button: Shrink: */ + if (text_size > MIN_TEXT_SIZE) + { + text_size--; + control_sound = SND_SHRINK; + toolopt_changed = 1; + } + } + } + else + { + /* One of the top buttons: */ + if (which & 1) + { + /* Top right button: Italic: */ + if (text_state & TTF_STYLE_ITALIC) + { + text_state &= ~TTF_STYLE_ITALIC; + control_sound = SND_ITALIC_ON; + } + else + { + text_state |= TTF_STYLE_ITALIC; + control_sound = SND_ITALIC_OFF; + } + } + else + { + /* Top left button: Bold: */ + if (text_state & TTF_STYLE_BOLD) + { + text_state &= ~TTF_STYLE_BOLD; + control_sound = SND_THIN; + } + else + { + text_state |= TTF_STYLE_BOLD; + control_sound = SND_THICK; + } + } + toolopt_changed = 1; + } + if (control_sound != -1) + { + playsound(screen, 0, control_sound, 0, SNDPOS_CENTER, + SNDDIST_NEAR); + + + if (cur_tool == TOOL_TEXT) /* Huh? It had better be! */ + { + /* need to invalidate all the cached user fonts, causing reload on demand */ + + int i; + + for (i = 0; i < num_font_families; i++) + { + if (user_font_families[i] + && user_font_families[i]->handle) + { + TuxPaint_Font_CloseFont(user_font_families + [i]->handle); + user_font_families[i]->handle = NULL; + } + } + draw_fonts(); + update_screen_rect(&r_toolopt); + } + } + } + else if (cur_tool == TOOL_LABEL) + { + /* Label controls! */ + int control_sound = -1; + + if (which & 4) + { + /* One of the bottom buttons: */ + if (which & 1) + { + /* Bottom right button: Grow: */ + if (text_size < MAX_TEXT_SIZE) + { + text_size++; + control_sound = SND_GROW; + toolopt_changed = 1; + } + } + else + { + /* Bottom left button: Shrink: */ + if (text_size > MIN_TEXT_SIZE) + { + text_size--; + control_sound = SND_SHRINK; + toolopt_changed = 1; + } + } + } + else + { + if (which & 2) + { + /* One of the middle buttons: */ + if (which & 1) + { + /* right button: Italic: */ + if (text_state & TTF_STYLE_ITALIC) + { + text_state &= ~TTF_STYLE_ITALIC; + control_sound = SND_ITALIC_ON; + } + else + { + text_state |= TTF_STYLE_ITALIC; + control_sound = SND_ITALIC_OFF; + } + } + else + { + /* middle left button: Bold: */ + if (text_state & TTF_STYLE_BOLD) + { + text_state &= ~TTF_STYLE_BOLD; + control_sound = SND_THIN; + } + else + { + text_state |= TTF_STYLE_BOLD; + control_sound = SND_THICK; + } + } + toolopt_changed = 1; + } + + else + { + /* One of the top buttons: */ + if (which & 1) + { + /* Top right: Select button: */ + if (cur_label == LABEL_SELECT) + { + /* Already in label select mode; turn it off */ + cur_label = LABEL_LABEL; + update_canvas(0, 0, WINDOW_WIDTH - r_ttoolopt.w, + (button_h * buttons_tall) + + r_ttoolopt.h); + if (onscreen_keyboard) + { + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, + &kbd_rect); + update_screen_rect(&kbd_rect); + } + + if (onscreen_keyboard && !kbd) { - r_tir.y = (float)old_y / render_scale; - SDL_SetTextInputRect(&r_tir); SDL_StartTextInput(); - } - /* Text and Label Tools! */ - if (cur_tool == TOOL_LABEL && cur_label == LABEL_SELECT) + } + draw_tux_text(TUX_GREAT, tool_tips[TOOL_LABEL], 1); + playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, + SNDDIST_NEAR); + } + else + { + /* Want to select a label */ + if (are_labels()) { - /* Click to select a node to edit */ + update_canvas_ex_r(kbd_rect.x - r_ttools.w, + kbd_rect.y, + kbd_rect.x + kbd_rect.w, + kbd_rect.y + kbd_rect.h, 1); + if (texttool_len > 0) + { + rec_undo_buffer(); + do_render_cur_text(1); + texttool_len = 0; + cursor_textwidth = 0; + label_node_to_edit = NULL; + } + else if (label_node_to_edit) + { + rec_undo_buffer(); + have_to_rec_label_node = TRUE; + add_label_node(0, 0, 0, 0, NULL); + label_node_to_edit = NULL; + } - DEBUG_PRINTF("Searching for label @ (%d,%d)\n", old_x, old_y); + cur_label = LABEL_SELECT; + highlight_label_nodes(); - label_node_to_edit = search_label_list(&highlighted_label_node, old_x, old_y, 0); - - if (label_node_to_edit) - { - DEBUG_PRINTF("Got a label: \"%ls\" @ (%d,%d)\n", - label_node_to_edit->save_texttool_str, old_x, old_y); - DEBUG_PRINTF("Cursor now @ (%d,%d); width = %d\n", - cursor_x, cursor_y, cursor_textwidth); - select_label_node(&old_x, &old_y); - } - } - else if (cur_tool == TOOL_LABEL && cur_label == LABEL_APPLY) - { - /* Click to select a node to apply it to the canvas */ - - label_node_to_edit = search_label_list(&highlighted_label_node, old_x, old_y, 0); - if (label_node_to_edit) - apply_label_node(old_x, old_y); - } - else - hide_blinking_cursor(); - - - - if (cursor_x != -1 && cursor_y != -1) - { - /* - if (texttool_len > 0) - { - rec_undo_buffer(); - do_render_cur_text(1); - texttool_len = 0; - } - */ - } - if (onscreen_keyboard && kbd && HIT(kbd_rect) && !(cur_tool == TOOL_LABEL && (cur_label == LABEL_SELECT || cur_label == LABEL_APPLY))) - { - new_kbd = osk_clicked(kbd, old_x - kbd_rect.x + r_canvas.x, old_y - kbd_rect.y + r_canvas.y); - /* keyboard has changed, erase the old, note that the old kbd has yet been freed. */ - if (new_kbd != kbd) - { - kbd = new_kbd; - update_canvas_ex(kbd_rect.x, kbd_rect.y, kbd_rect.x + kbd_rect.w, kbd_rect.y + kbd_rect.h, - 0); - /* set kbd_rect dimensions according to the new keyboard */ - reposition_onscreen_keyboard(-1); - } - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); - } - else - { - cursor_x = old_x; - cursor_y = old_y; - cursor_left = old_x; - - if (onscreen_keyboard && !(cur_tool == TOOL_LABEL && (cur_label == LABEL_SELECT || cur_label == LABEL_APPLY))) - { - update_canvas_ex(kbd_rect.x, kbd_rect.y, kbd_rect.x + kbd_rect.w, - kbd_rect.y + kbd_rect.h, 0); - update_screen_rect(&kbd_rect); - reposition_onscreen_keyboard(old_y); - } - } - - if (onscreen_keyboard && !kbd) - { - r_tir.y = (float)cursor_y / render_scale; - SDL_SetTextInputRect(&r_tir); - SDL_StartTextInput(); - } - - - do_render_cur_text(0); + draw_tux_text(TUX_GREAT, TIP_LABEL_SELECTOR_ENABLED, + 1); + playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, + SNDDIST_NEAR); } - - button_down = 1; - } - } - else if (HIT(r_sfx) && valid_click(event.button.button)) - { - /* A sound player button on the lower left has been pressed! */ -#ifndef NOSOUND - if (cur_tool == TOOL_STAMP && use_sound && !mute) - { - which = GRIDHIT_GD(r_sfx, gd_sfx); - - if (which == 0 && !stamp_data[stamp_group][cur_stamp[stamp_group]]->no_sound) - { - /* Re-play sound effect: */ - - Mix_ChannelFinished(NULL); - Mix_PlayChannel(2, stamp_data[stamp_group][cur_thing]->ssnd, 0); - } - else if (which == 1 && !stamp_data[stamp_group][cur_stamp[stamp_group]]->no_descsound) - { - Mix_ChannelFinished(NULL); - Mix_PlayChannel(2, stamp_data[stamp_group][cur_thing]->sdesc, 0); - } - - magic_switchout(canvas); + } + toolopt_changed = 1; } -#endif + else + { + /* Top left: "Apply" label */ + if (cur_label == LABEL_APPLY) + { + /* Already in label apply mode; turn it off */ + cur_label = LABEL_LABEL; + update_canvas(0, 0, WINDOW_WIDTH - r_ttoolopt.w, + (button_h * buttons_tall) + + r_ttoolopt.h); + if (onscreen_keyboard) + { + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, + &kbd_rect); + update_screen_rect(&kbd_rect); + } + + draw_tux_text(TUX_GREAT, tool_tips[TOOL_LABEL], 1); + playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, + SNDDIST_NEAR); + } + else + { + /* Want to apply a label */ + if (are_labels()) + { + update_canvas_ex_r(kbd_rect.x - r_ttools.w, + kbd_rect.y, + kbd_rect.x + kbd_rect.w, + kbd_rect.y + kbd_rect.h, 1); + if (texttool_len > 0) + { + rec_undo_buffer(); + do_render_cur_text(1); + texttool_len = 0; + cursor_textwidth = 0; + label_node_to_edit = NULL; + } + else if (label_node_to_edit) + { + rec_undo_buffer(); + have_to_rec_label_node = TRUE; + add_label_node(0, 0, 0, 0, NULL); + label_node_to_edit = NULL; + } + + cur_label = LABEL_APPLY; + highlight_label_nodes(); + + draw_tux_text(TUX_GREAT, TIP_LABEL_APPLIER_ENABLED, + 1); + playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, + SNDDIST_NEAR); + } + } + + toolopt_changed = 1; + } + } } -#ifdef __ANDROID__ - start_motion_convert(event); -#endif - - } - else if (event.type == SDL_MOUSEWHEEL && wheely) - { - int most = 14; - int num_rows_needed; - int xpos, ypos; - SDL_Rect r_controls; - SDL_Rect r_notcontrols; - SDL_Rect r_items; /* = r_notcontrols; */ - - SDL_GetMouseState(&xpos, &ypos); - - /* Scroll wheel code. - WARNING: this must be kept in sync with the mouse-move - code (for cursor changes) and mouse-click code. */ - - if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_STAMP || - cur_tool == TOOL_SHAPES || cur_tool == TOOL_LINES || - cur_tool == TOOL_MAGIC || cur_tool == TOOL_TEXT || - cur_tool == TOOL_ERASER || cur_tool == TOOL_LABEL || - cur_tool == TOOL_FILL) + if (control_sound != -1) { + playsound(screen, 0, control_sound, 0, SNDPOS_CENTER, + SNDDIST_NEAR); - /* Left tools scroll (via scroll wheel) */ - if (hit_test(&r_tools, xpos, ypos) && NUM_TOOLS > most + TOOLOFFSET) + + if (cur_tool == TOOL_LABEL) /* Huh? It had better be! */ + { + /* need to invalidate all the cached user fonts, causing reload on demand */ + + int i; + + for (i = 0; i < num_font_families; i++) { - int is_upper = (event.wheel.y > 0); - - if (is_upper && tool_scroll > 0) - { - tool_scroll -= gd_tools.cols; - playsound(screen, 1, SND_SCROLL, 1, event.button.x, SNDDIST_NEAR); - draw_toolbar(); - } - else if (!is_upper && tool_scroll < NUM_TOOLS - 12 - TOOLOFFSET) - { - tool_scroll += gd_tools.cols; - playsound(screen, 1, SND_SCROLL, 1, event.button.x, SNDDIST_NEAR); - draw_toolbar(); - } - - if (event.button.y < r_tools.y + button_h / 2) // cursor on the upper button - { - if (tool_scroll == 0) - do_setcursor(cursor_arrow); - else - do_setcursor(cursor_up); - } - - else if (event.button.y > r_tools.y + r_tools.h - button_h / 2) // cursor on the lower button - { - if (tool_scroll < NUM_TOOLS - 12 - TOOLOFFSET) - do_setcursor(cursor_down); - else - do_setcursor(cursor_arrow); - } - - else if (tool_avail[((event.button.x - r_tools.x) / button_w) + - ((event.button.y - - r_tools.y - button_h / 2) / button_h) * gd_tools.cols + tool_scroll]) - { - do_setcursor(cursor_hand); - } - else - { - do_setcursor(cursor_arrow); - } - update_screen_rect(&r_tools); + if (user_font_families[i] + && user_font_families[i]->handle) + { + TuxPaint_Font_CloseFont(user_font_families + [i]->handle); + user_font_families[i]->handle = NULL; + } } + draw_fonts(); + update_screen_rect(&r_toolopt); + } + } + draw_fonts(); + update_screen_rect(&r_toolopt); + } + else if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) + { + /* Brush spacing */ - /* Right tool options scroll (via scroll wheel) */ + int prev_size, chosen, new_size, frame_w, w, h, control_sound; + + prev_size = brushes_spacing[cur_brush]; + chosen = + ((BRUSH_SPACING_SIZES + 1) * (event.button.x - + r_ttoolopt.x)) / r_ttoolopt.w; + + frame_w = + img_brushes[cur_brush]->w / abs(brushes_frames[cur_brush]); + w = frame_w / (brushes_directional[cur_brush] ? 3 : 1); + h = + img_brushes[cur_brush]->h / + (brushes_directional[cur_brush] ? 3 : 1); + + /* Spacing ranges from 0px to "N x the max dimension of the brush" + (so a 48x48 brush would have a spacing of 48 if the center option is chosen) */ + new_size = + (chosen * max(w, h) * BRUSH_SPACING_MAX_MULTIPLIER) / + BRUSH_SPACING_SIZES; + + if (new_size != prev_size) + { + char tmp_tip[256]; + int numer, denom; + + brushes_spacing[cur_brush] = new_size; + draw_brushes_spacing(); + update_screen_rect(&r_toolopt); + + if (new_size < prev_size) + control_sound = SND_SHRINK; else + control_sound = SND_GROW; + + if (new_size == 0) + { + draw_tux_text(TUX_GREAT, TIP_BRUSH_SPACING_ZERO, 1); + } + else if (new_size / max(w, h) == 1) + { + draw_tux_text(TUX_GREAT, TIP_BRUSH_SPACING_SAME, 1); + } + else if (new_size > max(w, h)) + { + double ratio, i, f; + + ratio = (float) new_size / (float) max(w, h); + f = modf(ratio, &i); + + if (f > + (SLOPPY_FRAC_MAX - SLOPPY_FRAC_MIN) / SLOPPY_FRAC_MAX) { - grid_dims gd_controls; /* might become 2-by-2 */ - grid_dims gd_items; /* generally becoming 2-by-whatever */ - - gd_controls.rows = 0; - gd_controls.cols = 0; - gd_items.rows = 2; - gd_items.cols = 2; - - /* Note set of things we're dealing with */ - /* (stamps, brushes, etc.) */ - - if (cur_tool == TOOL_STAMP) - { - if (!disable_stamp_controls) - { - /* was 2,2 before adding left/right stamp group buttons -bjk 2007.05.15 */ - gd_controls.rows = 3; - gd_controls.cols = 2; - } - else - { - /* was left 0,0 before adding left/right stamp group buttons -bjk 2007.05.03 */ - gd_controls.rows = 1; - gd_controls.cols = 2; - } - } - else if (cur_tool == TOOL_TEXT) - { - if (!disable_stamp_controls) - { - gd_controls.rows = 2; - gd_controls.cols = 2; - } - } - else if (cur_tool == TOOL_LABEL) - { - if (!disable_stamp_controls) - { - gd_controls.rows = 3; - gd_controls.cols = 2; - } - else - { - gd_controls.rows = 1; - gd_controls.cols = 2; - } - } - else if (cur_tool == TOOL_MAGIC) - { - if (!disable_magic_controls) - { - gd_controls.rows = 2; - gd_controls.cols = 2; - } - else - { - gd_controls.rows = 1; - gd_controls.cols = 2; - } - } - else if (cur_tool == TOOL_SHAPES) - { - if (!disable_shape_controls) - { - gd_controls.rows = 1; - gd_controls.cols = 2; - } - } - else if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) - { - if (!disable_brushspacing) - { - gd_controls.rows = 1; - gd_controls.cols = 2; - } - } - - /* number of whole or partial rows that will be needed - (can make this per-tool if variable columns needed) */ - num_rows_needed = (num_things + gd_items.cols - 1) / gd_items.cols; - - do_draw = 0; - - r_controls.w = r_toolopt.w; - r_controls.h = gd_controls.rows * button_h; - r_controls.x = r_toolopt.x; - r_controls.y = r_toolopt.y + r_toolopt.h - r_controls.h; - - r_notcontrols.w = r_toolopt.w; - r_notcontrols.h = r_toolopt.h - r_controls.h; - r_notcontrols.x = r_toolopt.x; - r_notcontrols.y = r_toolopt.y; - - r_items.x = r_notcontrols.x; - r_items.y = r_notcontrols.y; - r_items.w = r_notcontrols.w; - r_items.h = r_notcontrols.h; - - if (num_rows_needed * button_h > r_items.h) - { - /* too many; we'll need scroll buttons */ - r_items.h -= button_h; - r_items.y += button_h / 2; - } - gd_items.rows = r_items.h / button_h; - - if (0) - { - } - else - { - /* scroll button */ - int is_upper = (event.wheel.y > (Sint32) 0); - - if ((is_upper && *thing_scroll > 0) /* upper arrow */ - || (!is_upper && *thing_scroll / gd_items.cols < num_rows_needed - gd_items.rows) /* lower arrow */ - ) - { - *thing_scroll += is_upper ? -gd_items.cols : gd_items.cols; - do_draw = 1; - playsound(screen, 1, SND_SCROLL, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - if (*thing_scroll == 0) - { - do_setcursor(cursor_arrow); - } - } - } - - - /* Assign the change(s), if any / redraw, if needed: */ - - if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) - { - if (do_draw) - draw_brushes(); - } - else if (cur_tool == TOOL_ERASER) - { - if (do_draw) - draw_erasers(); - } - else if (cur_tool == TOOL_FILL) - { - if (do_draw) - draw_fills(); - } - else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - { - if (do_draw) - draw_fonts(); - } - else if (cur_tool == TOOL_STAMP) - { - if (do_draw) - draw_stamps(); - } - else if (cur_tool == TOOL_SHAPES) - { - if (do_draw) - draw_shapes(); - } - else if (cur_tool == TOOL_MAGIC) - { - if (do_draw) - draw_magic(); - } - - /* Update the screen: */ - if (do_draw) - update_screen_rect(&r_toolopt); - + i++; + f = 0.0; } + else if (f < SLOPPY_FRAC_MIN / SLOPPY_FRAC_MAX) + { + f = 0.0; + } + + if (f == 0.0) + { + snprintf(tmp_tip, sizeof(tmp_tip), + gettext(TIP_BRUSH_SPACING_MORE), (int) i); + } + else + { + sloppy_frac(f, &numer, &denom); + + snprintf(tmp_tip, sizeof(tmp_tip), + gettext(TIP_BRUSH_SPACING_MORE_FRAC), (int) i, + numer, denom); + } + + draw_tux_text(TUX_GREAT, tmp_tip, 1); + } + else if (new_size < max(w, h)) + { + sloppy_frac((float) new_size / (float) max(w, h), &numer, + &denom); + snprintf(tmp_tip, sizeof(tmp_tip), + gettext(TIP_BRUSH_SPACING_LESS), numer, denom); + draw_tux_text(TUX_GREAT, tmp_tip, 1); + } + + playsound(screen, 0, control_sound, 0, SNDPOS_CENTER, + SNDDIST_NEAR); } + } } - else if (event.type == SDL_USEREVENT) + else { - if (event.user.code == USEREVENT_TEXT_UPDATE) - { - /* Time to replace "Great!" with old tip text: */ + /* Scroll buttons */ + int is_upper = event.button.y < r_toolopt.y + button_h / 2; - if (event.user.data1 != NULL) - { - if (((unsigned char *)event.user.data1)[0] == '=') - { - draw_tux_text_ex(TUX_GREAT, (char *)event.user.data1 + 1, 1, (int)(intptr_t) event.user.data2); //EP added (intptr_t) to avoid warning on x64 - } - else - { - draw_tux_text_ex(TUX_GREAT, (char *)event.user.data1, 0, (int)(intptr_t) event.user.data2); //EP added (intptr_t) to avoid warning on x64 - } - } - else - draw_tux_text(TUX_GREAT, "", 1); + if ((is_upper && *thing_scroll > 0) /* upper arrow */ + || (!is_upper && *thing_scroll / gd_items.cols < num_rows_needed - gd_items.rows) /* lower arrow */ + ) + { + *thing_scroll += is_upper ? -gd_items.cols : gd_items.cols; + do_draw = 1; + playsound(screen, 1, SND_SCROLL, 1, SNDPOS_RIGHT, + SNDDIST_NEAR); + + if (scrolltimer_selector != TIMERID_NONE) + { + SDL_RemoveTimer(scrolltimer_selector); + scrolltimer_selector = TIMERID_NONE; } - else if (event.user.code == USEREVENT_PLAYDESCSOUND) + + if (!scrolling_selector && event.type == SDL_MOUSEBUTTONDOWN) { - /* Play a stamp's spoken description (because the sound effect just finished) */ - /* (This event is pushed into the queue by playstampdesc(), which - is a callback from Mix_ChannelFinished() when playing a stamp SFX) */ + DEBUG_PRINTF("Starting scrolling\n"); + memcpy(&scrolltimer_selector_event, &event, + sizeof(SDL_Event)); + scrolltimer_selector_event.type = TP_SDL_MOUSEBUTTONSCROLL; - debug("Playing description sound..."); + /* + * We enable the timer subsystem only when needed (e.g., to use SDL_AddTimer() needed + * for scrolling) then disable it immediately after (e.g., after the timer has fired or + * after SDL_RemoveTimer()) because enabling the timer subsystem in SDL1 has a high + * energy impact on the Mac. + */ -#ifndef NOSOUND - Mix_ChannelFinished(NULL); /* Kill the callback, so we don't get stuck in a loop! */ - - if (event.user.data1 != NULL) - { - if ((int)(intptr_t) event.user.data1 == cur_stamp[stamp_group]) /* Don't play old stamp's sound... *///EP added (intptr_t) to avoid warning on x64 - { - if (!mute && stamp_data[stamp_group][(int)(intptr_t) event.user.data1]->sdesc != NULL) //EP added (intptr_t) to avoid warning on x64 - Mix_PlayChannel(2, stamp_data[stamp_group][(int)(intptr_t) event.user.data1]->sdesc, //EP added (intptr_t) to avoid warning on x64 - 0); - } - } -#endif + scrolling_selector = 1; + SDL_InitSubSystem(SDL_INIT_TIMER); + scrolltimer_selector = + SDL_AddTimer(REPEAT_SPEED, scrolltimer_selector_callback, + (void *) &scrolltimer_selector_event); } - } - else if (event.type == SDL_MOUSEBUTTONUP) - { - if (scrolling_selector) + else { - if (scrolltimer_selector != TIMERID_NONE) + DEBUG_PRINTF("Continuing scrolling\n"); + scrolltimer_selector = + SDL_AddTimer(REPEAT_SPEED / 3, + scrolltimer_selector_callback, + (void *) &scrolltimer_selector_event); + } + + if (*thing_scroll == 0 + || *thing_scroll / gd_items.cols == + num_rows_needed - gd_items.rows) + { + do_setcursor(cursor_arrow); + if (scrolling_selector) + { + if (scrolltimer_selector != TIMERID_NONE) { SDL_RemoveTimer(scrolltimer_selector); scrolltimer_selector = TIMERID_NONE; } - scrolling_selector = 0; - SDL_QuitSubSystem(SDL_INIT_TIMER); - - DEBUG_PRINTF("Killing selector scrolling\n"); + scrolling_selector = 0; + SDL_QuitSubSystem(SDL_INIT_TIMER); + } } - - else if (scrolling_tool) - { - if (scrolltimer_tool != TIMERID_NONE) - { - SDL_RemoveTimer(scrolltimer_tool); - scrolltimer_tool = TIMERID_NONE; - } - scrolling_tool = 0; - SDL_QuitSubSystem(SDL_INIT_TIMER); - - DEBUG_PRINTF("Killing tool scrolling\n"); - } - - /* Erase the xor drawed at click */ - else if (cur_tool == TOOL_STAMP && stamp_xored && event.button.button < 4) - { - stamp_xor(canvas->w / 2, canvas->h / 2); - stamp_xored = 0; - stamp_size_selector_clicked = 0; - update_screen(canvas->w / 2 - (CUR_STAMP_W + 1) / 2 + r_canvas.x, - canvas->h / 2 - (CUR_STAMP_H + 1) / 2 + r_canvas.y, - canvas->w / 2 + (CUR_STAMP_W + 1) / 2 + r_canvas.x, - canvas->h / 2 + (CUR_STAMP_H + 1) / 2 + r_canvas.y); - } - - - if (button_down || emulate_button_pressed) - { - if (cur_tool == TOOL_BRUSH) - { - /* (Drawing on mouse release to fix single click issue) */ - brush_draw(old_x, old_y, old_x, old_y, 1); - } - else if (cur_tool == TOOL_STAMP) - { - if (stamp_tool_mode == STAMP_TOOL_MODE_PLACE) - { - if (old_x >= 0 && old_y >= 0 && old_x <= r_canvas.w && old_y <= r_canvas.h) - { - if (!no_stamp_rotation) - { - int mouse_warp_x; - - /* Going through stamp rotation step, first */ - - /* Warp mouse to the far right of the stamp, - where we'll start at 0-degrees of rotation - (keep it within the canvas, though!) */ - mouse_warp_x = r_tools.w + old_x + (CUR_STAMP_W / 2); - if (mouse_warp_x >= WINDOW_WIDTH - r_ttoolopt.w) - mouse_warp_x = WINDOW_WIDTH - r_ttoolopt.w - 1; - - SDL_WarpMouse(mouse_warp_x, old_y); - do_setcursor(cursor_rotate); - - stamp_tool_mode = STAMP_TOOL_MODE_ROTATE; - stamp_place_x = old_x; - stamp_place_y = old_y; - draw_tux_text(TUX_GREAT, TIP_STAMPS_ROTATING, 1); - } - else - { - /* Draw a stamp! */ - rec_undo_buffer(); - playsound(screen, 1, SND_STAMP, 1, event.button.x, SNDDIST_NEAR); - stamp_draw(old_x, old_y, 0); - reset_stamps(&stamp_xored_rt, &old_x, &old_y, &stamp_tool_mode); - - draw_tux_text(TUX_GREAT, great_str(), 1); - - /* FIXME: Make delay configurable: */ - - control_drawtext_timer(1000, stamp_data[stamp_group][cur_stamp[stamp_group]]->stxt, - stamp_data[stamp_group][cur_stamp[stamp_group]]->locale_text); - } - } - } - else if (stamp_tool_mode == STAMP_TOOL_MODE_ROTATE) - { - /* Draw a stamp (finishing rotation step)! */ - rec_undo_buffer(); - playsound(screen, 1, SND_STAMP, 1, stamp_place_x, SNDDIST_NEAR); - int stamp_angle_rotation = 360 - brush_rotation(stamp_place_x, stamp_place_y, old_x, old_y); - - stamp_draw(stamp_place_x, stamp_place_y, stamp_angle_rotation); - draw_tux_text(TUX_GREAT, great_str(), 1); - reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, &stamp_tool_mode); - - /* FIXME: Make delay configurable: */ - - control_drawtext_timer(1000, stamp_data[stamp_group][cur_stamp[stamp_group]]->stxt, - stamp_data[stamp_group][cur_stamp[stamp_group]]->locale_text); - - } - } - - else if (cur_tool == TOOL_LINES) - { - if (!mouseaccessibility || (mouseaccessibility && !emulate_button_pressed)) - { - /* (Arbitrarily large, so we draw once now) */ - reset_brush_counter(); - - brush_draw(line_start_x, line_start_y, - event.button.x - r_canvas.x, event.button.y - r_canvas.y, 1); - brush_draw(event.button.x - r_canvas.x, - event.button.y - r_canvas.y, - event.button.x - r_canvas.x, event.button.y - r_canvas.y, 1); - - playsound(screen, 1, SND_LINE_END, 1, event.button.x, SNDDIST_NEAR); - draw_tux_text(TUX_GREAT, tool_tips[TOOL_LINES], 1); - } - } - - else if (cur_tool == TOOL_SHAPES) - { - if (!mouseaccessibility || (mouseaccessibility && !emulate_button_pressed)) - { - if (shape_tool_mode == SHAPE_TOOL_MODE_STRETCH) - { - /* Now we can rotate the shape... */ - - shape_current_x = event.button.x - r_canvas.x; - shape_current_y = event.button.y - r_canvas.y; - - if (!simple_shapes && !shape_no_rotate[cur_shape]) - { - shape_tool_mode = SHAPE_TOOL_MODE_ROTATE; - - shape_radius = - sqrt((shape_start_x - shape_current_x) * (shape_start_x - shape_current_x) + - (shape_start_y - shape_current_y) * (shape_start_y - shape_current_y)); - - SDL_WarpMouse(shape_current_x + r_ttools.w, shape_start_y); - do_setcursor(cursor_rotate); - - - /* Erase stretchy XOR: */ - - if (abs(shape_start_x - shape_current_x) > 15 || abs(shape_start_y - shape_current_y) > 15) - do_shape(shape_start_x, shape_start_y, old_x, old_y, 0, 0); - - /* Make an initial rotation XOR to be erased: */ - - do_shape(shape_start_x, shape_start_y, - shape_current_x, shape_current_y, - shape_rotation(shape_start_x, shape_start_y, shape_current_x, shape_current_y), 0); - - playsound(screen, 1, SND_LINE_START, 1, event.button.x, SNDDIST_NEAR); - draw_tux_text(TUX_BORED, TIP_SHAPE_NEXT, 1); - - - /* FIXME: Do something less intensive! */ - - SDL_Flip(screen); - } - else - { - reset_brush_counter(); - - - playsound(screen, 1, SND_LINE_END, 1, event.button.x, SNDDIST_NEAR); - do_shape(shape_start_x, shape_start_y, shape_current_x, shape_current_y, 0, 1); - - SDL_Flip(screen); - - shape_tool_mode = SHAPE_TOOL_MODE_DONE; - draw_tux_text(TUX_GREAT, shape_tool_tips[simple_shapes ? SHAPE_COMPLEXITY_SIMPLE : SHAPE_COMPLEXITY_NORMAL], 1); - } - } - else if (shape_tool_mode == SHAPE_TOOL_MODE_ROTATE) - { - reset_brush_counter(); - - playsound(screen, 1, SND_LINE_END, 1, event.button.x, SNDDIST_NEAR); - do_shape(shape_start_x, shape_start_y, shape_current_x, shape_current_y, - shape_rotation(shape_start_x, shape_start_y, - event.button.x - r_canvas.x, event.button.y - r_canvas.y), 1); - - shape_tool_mode = SHAPE_TOOL_MODE_DONE; - draw_tux_text(TUX_GREAT, shape_tool_tips[simple_shapes ? SHAPE_COMPLEXITY_SIMPLE : SHAPE_COMPLEXITY_NORMAL], 1); - - /* FIXME: Do something less intensive! */ - - SDL_Flip(screen); - } - } - } - else if (cur_tool == TOOL_MAGIC - && (magics[magic_group][cur_magic[magic_group]].mode == MODE_PAINT - || magics[magic_group][cur_magic[magic_group]].mode == MODE_ONECLICK - || magics[magic_group][cur_magic[magic_group]].mode == MODE_PAINT_WITH_PREVIEW)) - { - int grp; - int cur; - - grp = magic_group; - cur = cur_magic[grp]; - - if (!mouseaccessibility || (mouseaccessibility && !emulate_button_pressed)) - { - int undo_ctr; - SDL_Surface *last; - - /* Releasing button: Finish the magic: */ - - if (cur_undo > 0) - undo_ctr = cur_undo - 1; - else - undo_ctr = NUM_UNDO_BUFS - 1; - - last = undo_bufs[undo_ctr]; - - update_rect.x = 0; - update_rect.y = 0; - update_rect.w = 0; - update_rect.h = 0; - - magic_funcs[magics[grp][cur].handle_idx].release(magic_api_struct, - magics[grp][cur].idx, - canvas, last, old_x, old_y, &update_rect); - - draw_tux_text(TUX_GREAT, magics[grp][cur].tip[magic_modeint(magics[grp][cur].mode)], 1); - - update_canvas(update_rect.x, update_rect.y, - update_rect.x + update_rect.w, update_rect.y + update_rect.h); - } - } - else if (onscreen_keyboard && - (cur_tool == TOOL_TEXT || (cur_tool == TOOL_LABEL && cur_label != LABEL_SELECT && cur_label != LABEL_APPLY))) - { - if (onscreen_keyboard && kbd) - { - osk_released(kbd); - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); - // SDL_Flip(screen); - } - - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput(); - } - } - else if (cur_tool == TOOL_FILL) - { - fill_drag_started = 0; - } - } - button_down = 0; - -#ifdef __ANDROID__ - stop_motion_convert(event); -#endif + } } - else if (event.type == SDL_MOUSEMOTION && !ignoring_motion) - { - new_x = event.button.x - r_canvas.x; - new_y = event.button.y - r_canvas.y; -#ifdef __ANDROID__ - convert_motion_to_wheel(event); + + /* Assign the change(s), if any / redraw, if needed: */ + + if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) + { + cur_brush = cur_thing; + render_brush(); + + if (do_draw) + { + draw_brushes(); + show_brush_tip(); + } + } + else if (cur_tool == TOOL_ERASER) + { + cur_eraser = cur_thing; + + if (do_draw) + draw_erasers(); + } + else if (cur_tool == TOOL_FILL) + { + cur_fill = cur_thing; + draw_tux_text(TUX_GREAT, fill_tips[cur_fill], 1); + + if (do_draw) + draw_fills(); + } + else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + { + /* FIXME */ + /* char font_tux_text[512]; */ + + cur_font = cur_thing; + + /* FIXME */ + /* + safe_snprintf(font_tux_text, sizeof font_tux_text, "%s (%s).", + TTF_FontFaceFamilyName(getfonthandle(cur_font)), + TTF_FontFaceStyleName(getfonthandle(cur_font))); + draw_tux_text(TUX_GREAT, font_tux_text, 1); + */ + + if (do_draw) + draw_fonts(); + + + /* Only rerender when picking a different font */ + if (toolopt_changed) + { + draw_fonts(); + if (select_changed) + { + rec_undo_buffer(); + do_render_cur_text(1); + texttool_len = 0; + } + else + { + do_render_cur_text(0); + } + } + } + else if (cur_tool == TOOL_STAMP) + { +#ifndef NOSOUND + /* Only play when picking a different stamp */ + if (toolopt_changed && !mute) + { + /* If the sound hasn't been loaded yet, do it now */ + + if (!stamp_data[stamp_group][cur_thing]->sound_processed) + { + get_stamp_thumb(stamp_data[stamp_group][cur_thing], 1); + } + + /* If there's an SFX, play it! */ + + if (stamp_data[stamp_group][cur_thing]->ssnd != NULL) + { + Mix_ChannelFinished(NULL); /* Prevents multiple clicks from toggling between SFX and desc sound, rather than always playing SFX first, then desc sound... */ + + Mix_PlayChannel(2, stamp_data[stamp_group][cur_thing]->ssnd, + 0); + + /* If there's a description sound, play it after the SFX! */ + + if (stamp_data[stamp_group][cur_thing]->sdesc != NULL) + { + Mix_ChannelFinished(playstampdesc); + } + } + else + { + /* No SFX? If there's a description sound, play it now! */ + + if (stamp_data[stamp_group][cur_thing]->sdesc != NULL) + { + Mix_PlayChannel(2, + stamp_data[stamp_group][cur_thing]->sdesc, + 0); + } + } + } #endif - oldpos_x = event.motion.x; - oldpos_y = event.motion.y; - - - /* FIXME: Is doing this every event too intensive? */ - /* Should I check current cursor first? */ - - if (HIT(r_tools)) + if (cur_thing != cur_stamp[stamp_group]) + { + if (stamp_tool_mode == STAMP_TOOL_MODE_ROTATE) { - int most = buttons_tall * gd_tools.cols; - - /* Tools: */ - - if (NUM_TOOLS > most) - { - if (event.button.y < r_tools.y + button_h / 2) - { - if (tool_scroll > 0) - do_setcursor(cursor_up); - else - do_setcursor(cursor_arrow); - } - else if (event.button.y > r_tools.y + r_tools.h - button_h / 2) - { - if (tool_scroll < NUM_TOOLS - buttons_tall * gd_tools.cols + gd_tools.cols) - do_setcursor(cursor_down); - else - do_setcursor(cursor_arrow); - } - - else if (tool_avail[((event.button.x - r_tools.x) / button_w) + - ((event.button.y - - r_tools.y - button_h / 2) / button_h) * gd_tools.cols + tool_scroll]) - { - do_setcursor(cursor_hand); - } - else - { - do_setcursor(cursor_arrow); - } - - } - - else - { - if (tool_avail[((event.button.x - r_tools.x) / button_w) + - ((event.button.y - r_tools.y) / button_h) * gd_tools.cols]) - { - do_setcursor(cursor_hand); - } - else - { - do_setcursor(cursor_arrow); - } - } + stamp_xor(stamp_place_x, stamp_place_y); + reset_stamps(&stamp_xored_rt, &stamp_place_x, + &stamp_place_y, &stamp_tool_mode); } - else if (HIT(r_sfx)) - { - /* Sound player buttons: */ + cur_stamp[stamp_group] = cur_thing; + set_active_stamp(); + update_stamp_xor(0); + stamp_tool_mode = STAMP_TOOL_MODE_PLACE; + } - if (cur_tool == TOOL_STAMP && use_sound && !mute && - ((GRIDHIT_GD(r_sfx, gd_sfx) == 0 && - !stamp_data[stamp_group][cur_stamp[stamp_group]]->no_sound) || - (GRIDHIT_GD(r_sfx, gd_sfx) == 1 && - !stamp_data[stamp_group][cur_stamp[stamp_group]]->no_descsound))) - { - do_setcursor(cursor_hand); - } - else - { - do_setcursor(cursor_arrow); - } - } - else if (HIT(r_colors)) - { - /* Color picker: */ - if (colors_are_selectable) - do_setcursor(cursor_hand); - else - do_setcursor(cursor_arrow); - } - else if (HIT(r_toolopt)) - { - int control_rows = 0, num_places; + if (do_draw) + draw_stamps(); - /* mouse cursor code - WARNING: this must be kept in sync with the mouse-click - and mouse-click code. (it isn't, currently!) */ + if (stamp_data[stamp_group][cur_stamp[stamp_group]]->stxt != + NULL) + { +#ifdef DEBUG + printf + ("stamp_data[stamp_group][cur_stamp[stamp_group]]->stxt = %s\n", + stamp_data[stamp_group][cur_stamp[stamp_group]]->stxt); +#endif - /* Note set of things we're dealing with */ - /* (stamps, brushes, etc.) */ - if (cur_tool == TOOL_STAMP) - { - } - else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - { - } - - /* This if/if/if block is awful -bjk 2022.01.19 */ - if (cur_tool == TOOL_STAMP && !disable_stamp_controls) - control_rows = 3; - - if (cur_tool == TOOL_LABEL) - { - control_rows = 1; - if (!disable_stamp_controls) - control_rows = 3; - } - - if (cur_tool == TOOL_TEXT && !disable_stamp_controls) - control_rows = 2; - if (cur_tool == TOOL_MAGIC) - { - control_rows = 1; - if (!disable_magic_controls) - control_rows = 2; - } - if (cur_tool == TOOL_SHAPES && !disable_shape_controls) - control_rows = 1; - if ((cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) && !disable_brushspacing) - control_rows = 1; - - num_places = buttons_tall * gd_toolopt.cols - control_rows * gd_toolopt.cols; - - if (num_things > num_places) - { - /* Are there scroll buttons? */ - num_places = num_places - gd_toolopt.cols; /* Two scroll buttons occupy one row */ - if (event.button.y < r_ttoolopt.h + img_scroll_up->h) - { - /* Up button; is it available? */ - - if (*thing_scroll > 0) - do_setcursor(cursor_up); - else - do_setcursor(cursor_arrow); - } - else if (event.button.y > - (button_h * (num_places / gd_toolopt.cols) + r_ttoolopt.h + img_scroll_up->h) - && event.button.y <= (button_h * (num_places / gd_toolopt.cols) + r_ttoolopt.h + img_scroll_up->h + img_scroll_up->h)) - { - /* Down button; is it available? */ - - if (*thing_scroll < num_things - num_places) - do_setcursor(cursor_down); - else - do_setcursor(cursor_arrow); - } - else - { - /* One of the selectors: */ - - /* FIXME: Also show "cursor_hand" when touching controls (stamp size, brush spacing, etc.!) (See below) -bjk 2022.01.19 */ - - which = ((event.button.y - r_ttoolopt.h - img_scroll_up->h) / button_h) * 2 + (event.button.x - (WINDOW_WIDTH - r_ttoolopt.w)) / button_w; - - if (which < num_things) - do_setcursor(cursor_hand); - else - do_setcursor(cursor_arrow); - } - } - else - { - /* No scroll buttons - must be a selector: */ - - /* FIXME: Also show "cursor_hand" when touching controls (stamp size, brush spacing, etc.!) (See above) -bjk 2022.01.19 */ - - which = ((event.button.y - r_ttoolopt.h) / button_h) * 2 + (event.button.x - (WINDOW_WIDTH - r_ttoolopt.w)) / button_w; - - if (which < num_things) - do_setcursor(cursor_hand); - else - do_setcursor(cursor_arrow); - } - } - else if (HIT(r_canvas) && keyglobal == 0) - { - /* Canvas: */ - - if (cur_tool == TOOL_BRUSH) - do_setcursor(cursor_brush); - else if (cur_tool == TOOL_STAMP) - { - if (stamp_tool_mode != STAMP_TOOL_MODE_ROTATE) - do_setcursor(cursor_tiny); - else - do_setcursor(cursor_rotate); - } - else if (cur_tool == TOOL_LINES || cur_tool == TOOL_FILL) - do_setcursor(cursor_crosshair); - else if (cur_tool == TOOL_SHAPES) - { - if (shape_tool_mode != SHAPE_TOOL_MODE_ROTATE) - do_setcursor(cursor_crosshair); - else - do_setcursor(cursor_rotate); - } - else if (cur_tool == TOOL_TEXT) - { - if (onscreen_keyboard && HIT(kbd_rect)) - do_setcursor(cursor_hand); - else - do_setcursor(cursor_insertion); - } - else if (cur_tool == TOOL_LABEL) - { - if (cur_label == LABEL_LABEL) - { - if (onscreen_keyboard && HIT(kbd_rect)) - do_setcursor(cursor_hand); - else - do_setcursor(cursor_insertion); - } - else if (cur_label == LABEL_SELECT || cur_label == LABEL_APPLY) - { - if (search_label_list(¤t_label_node, event.button.x - r_ttools.w, event.button.y, 1)) - do_setcursor(cursor_hand); - else - do_setcursor(cursor_arrow); - } - } - else if (cur_tool == TOOL_MAGIC) - do_setcursor(cursor_wand); - else if (cur_tool == TOOL_ERASER) - do_setcursor(cursor_tiny); - } + draw_tux_text_ex(TUX_GREAT, + stamp_data[stamp_group][cur_stamp + [stamp_group]]->stxt, + 1, + stamp_data[stamp_group][cur_stamp + [stamp_group]]-> + locale_text); + } else + draw_tux_text(TUX_GREAT, "", 0); + + /* Enable or disable color selector: */ + draw_colors(stamp_colorable(cur_stamp[stamp_group]) + || stamp_tintable(cur_stamp[stamp_group])); + if (!scrolling_selector) + { + stamp_xor(canvas->w / 2, canvas->h / 2); + stamp_xored = 1; + stamp_size_selector_clicked = 1; + update_screen(canvas->w / 2 - (CUR_STAMP_W + 1) / 2 + + r_canvas.x, + canvas->h / 2 - (CUR_STAMP_H + 1) / 2 + + r_canvas.y, + canvas->w / 2 + (CUR_STAMP_W + 1) / 2 + + r_canvas.x, + canvas->h / 2 + (CUR_STAMP_H + 1) / 2 + + r_canvas.y); + } + } + else if (cur_tool == TOOL_SHAPES) + { + cur_shape = cur_thing; + + /* Remove ghost previews an reset the tool */ + if (shape_tool_mode != SHAPE_TOOL_MODE_DONE) + { + shape_tool_mode = SHAPE_TOOL_MODE_DONE; + do_undo(); + tool_avail[TOOL_REDO] = 0; /* Don't let them 'redo' to get preview back */ + draw_toolbar(); + update_screen_rect(&r_tools); + update_canvas(0, 0, canvas->w, canvas->h); + } + + if (toolopt_changed) + draw_tux_text(TUX_GREAT, shape_tips[cur_shape], 1); + + if (do_draw) + draw_shapes(); + } + else if (cur_tool == TOOL_MAGIC) + { + int grp; + int cur; + + grp = magic_group; + cur = cur_magic[grp]; + + if (cur_thing != cur) + { + cur = cur_thing; + magic_switchout(canvas); + + cur_magic[grp] = cur_thing; + draw_colors(magics[grp][cur].colors); + + if (magics[grp][cur].colors) + magic_funcs[magics[grp][cur].handle_idx]. + set_color(magic_api_struct, color_hexes[cur_color][0], + color_hexes[cur_color][1], + color_hexes[cur_color][2]); + + magic_switchin(canvas); + } + + draw_tux_text(TUX_GREAT, + magics[grp][cur].tip[magic_modeint + (magics[grp][cur].mode)], 1); + + if (do_draw) + draw_magic(); + } + + /* Update the screen: */ + if (do_draw) + update_screen_rect(&r_toolopt); + } + } + else if (HIT(r_colors) && colors_are_selectable) + { + /* Color! */ + whichc = GRIDHIT_GD(r_colors, gd_colors); + + if (valid_click(event.button.button)) + { + // magic_switchout(canvas); + + if (whichc >= 0 && whichc < NUM_COLORS) + { + int old_color; + + old_color = cur_color; + cur_color = whichc; + draw_tux_text(TUX_KISS, color_names[cur_color], 1); + + if (cur_color == (unsigned) COLOR_PICKER + || cur_color == (unsigned) COLOR_SELECTOR + || cur_color == (unsigned) COLOR_MIXER) + { + int chose_color; + + disable_avail_tools(); + draw_toolbar(); + draw_colors(COLORSEL_CLOBBER_WIPE); + draw_none(); + + chose_color = 0; + if (cur_color == (unsigned) COLOR_PICKER) + chose_color = do_color_picker(); + else if (cur_color == (unsigned) COLOR_SELECTOR) + chose_color = do_color_sel(0); + else if (cur_color == (unsigned) COLOR_MIXER) + { + chose_color = do_color_mix(); + if (!chose_color) + cur_color = old_color; + } + + if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + { + if (onscreen_keyboard && kbd) + { + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, + &kbd_rect); + update_screen_rect(&kbd_rect); + } + + if (onscreen_keyboard && !kbd) + { + SDL_StartTextInput(); + } + } + + enable_avail_tools(); + draw_toolbar(); + update_screen_rect(&r_tools); + + draw_cur_tool_tip(); + + draw_colors(COLORSEL_FORCE_REDRAW); + + if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) + draw_brushes(); + else if (cur_tool == TOOL_MAGIC) + draw_magic(); + else if (cur_tool == TOOL_STAMP) + { + if (stamp_tool_mode == STAMP_TOOL_MODE_ROTATE) + stamp_xor(stamp_place_x, stamp_place_y); + else if (stamp_xored) + { + stamp_xor(canvas->w / 2, canvas->h / 2); + stamp_xored = 0; + update_screen(canvas->w / 2 - (CUR_STAMP_W + 1) / 2 + + r_canvas.x, + canvas->h / 2 - (CUR_STAMP_H + 1) / 2 + + r_canvas.y, + canvas->w / 2 + (CUR_STAMP_W + 1) / 2 + + r_canvas.x, + canvas->h / 2 + (CUR_STAMP_H + 1) / 2 + + r_canvas.y); + } + draw_stamps(); + } + else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + draw_fonts(); + else if (cur_tool == TOOL_SHAPES) + draw_shapes(); + else if (cur_tool == TOOL_ERASER) + draw_erasers(); + else if (cur_tool == TOOL_FILL) + draw_fills(); + + if (chose_color) + playsound(screen, 1, SND_BUBBLE, 1, SNDPOS_CENTER, + SNDDIST_NEAR); + else + playsound(screen, 1, SND_CLICK, 1, SNDPOS_CENTER, + SNDDIST_NEAR); + + SDL_Flip(screen); + } + else + { + draw_colors(COLORSEL_REFRESH); + + playsound(screen, 1, SND_BUBBLE, 1, event.button.x, + SNDDIST_NEAR); + } + + handle_color_changed(); + } + } + } + else if (HIT(r_canvas) && valid_click(event.button.button) + && keyglobal == 0) + { + const Uint8 *kbd_state; + + kbd_state = SDL_GetKeyboardState(NULL); + + if ((kbd_state[SDL_SCANCODE_LCTRL] || kbd_state[SDL_SCANCODE_RCTRL]) + && colors_are_selectable) + { + int chose_color; + + /* Holding [Ctrl] while clicking; switch to temp-mode color selector! */ + chose_color = do_color_sel(1); + + draw_cur_tool_tip(); + draw_colors(COLORSEL_FORCE_REDRAW); + + if (chose_color) + { + playsound(screen, 1, SND_BUBBLE, 1, SNDPOS_CENTER, + SNDDIST_NEAR); + cur_color = COLOR_SELECTOR; + handle_color_changed(); + } + else + playsound(screen, 1, SND_CLICK, 1, SNDPOS_CENTER, SNDDIST_NEAR); + + SDL_Flip(screen); + } + else + { + /* Draw something! */ + old_x = event.button.x - r_canvas.x; + old_y = event.button.y - r_canvas.y; + /* if (old_y < r_canvas.h/2) */ + /* { */ + /* keybd_position = 0; */ + /* } */ + /* else */ + /* { */ + /* keybd_position = 1; */ + /* } */ + + if (been_saved) + { + been_saved = 0; + + if (!disable_save) + tool_avail[TOOL_SAVE] = 1; + + draw_toolbar(); + update_screen_rect(&r_tools); + } + + if (cur_tool == TOOL_BRUSH) + { + /* Start painting! */ + if (!emulate_button_pressed) + rec_undo_buffer(); + + /* (Arbitrarily large, so we draw once now) */ + reset_brush_counter(); + + /* brush_draw(old_x, old_y, old_x, old_y, 1); fixes SF #1934883? */ + playsound(screen, 0, paintsound(img_cur_brush_w), 1, + event.button.x, SNDDIST_NEAR); + + if (mouseaccessibility) + emulate_button_pressed = !emulate_button_pressed; + } + else if (cur_tool == TOOL_LINES) + { + /* Start a line! */ + + if (!emulate_button_pressed) + { + rec_undo_buffer(); + + line_start_x = old_x; + line_start_y = old_y; + + /* (Arbitrarily large, so we draw once now) */ + reset_brush_counter(); + + /* brush_draw(old_x, old_y, old_x, old_y, 1); fixes sf #1934883? */ + + playsound(screen, 1, SND_LINE_START, 1, event.button.x, + SNDDIST_NEAR); + draw_tux_text(TUX_BORED, TIP_LINE_START, 1); + } + if (mouseaccessibility) + emulate_button_pressed = !emulate_button_pressed; + } + else if (cur_tool == TOOL_SHAPES) + { + if (shape_tool_mode == SHAPE_TOOL_MODE_DONE) + { + /* Start drawing a shape! */ + + rec_undo_buffer(); + + shape_start_x = old_x; + shape_start_y = old_y; + + shape_tool_mode = SHAPE_TOOL_MODE_STRETCH; + + playsound(screen, 1, SND_LINE_START, 1, event.button.x, + SNDDIST_NEAR); + draw_tux_text(TUX_BORED, TIP_SHAPE_START, 1); + if (mouseaccessibility) + emulate_button_pressed = 1; + } + else if (shape_tool_mode == SHAPE_TOOL_MODE_ROTATE) + { + /* Draw the shape with the brush! */ + + /* Only draw here in mouse accessibility mode as there IS a mouse */ + /* See #300881 for the reasons that this is deplaced to draw in mouse release */ + if (mouseaccessibility) + { + /* (Arbitrarily large...) */ + reset_brush_counter(); + + playsound(screen, 1, SND_LINE_END, 1, event.button.x, + SNDDIST_NEAR); + do_shape(shape_start_x, shape_start_y, shape_current_x, + shape_current_y, shape_rotation(shape_start_x, + shape_start_y, + event.button.x - + r_canvas.x, + event.button.y - + r_canvas.y), 1); + + shape_tool_mode = SHAPE_TOOL_MODE_DONE; + draw_tux_text(TUX_GREAT, + shape_tool_tips[simple_shapes ? + SHAPE_COMPLEXITY_SIMPLE : + SHAPE_COMPLEXITY_NORMAL], 1); + } + } + else if (shape_tool_mode == SHAPE_TOOL_MODE_STRETCH) + /* Only reached in accessibility mode */ + emulate_button_pressed = 0; + } + else if (cur_tool == TOOL_MAGIC) + { + int grp; + int cur; + + grp = magic_group; + cur = cur_magic[grp]; + + if (!emulate_button_pressed) + { + int undo_ctr; + SDL_Surface *last; + + /* Start doing magic! */ + + /* These switchout/in are here for Magic tools that abuse the canvas + by drawing widgets on them; you don't want the widgets recorded as part + of the canvas in the undo buffer! + HOWEVER, as Pere noted in 2010.March, this causes many 'normal' Magic + tools to not work right, because they lose their record of the 'original' + canvas, before the user started using the tool (e.g., Rails, Perspective, Zoom). + FIXME: Some in-between solution is needed (a 'clean up the canvas'/'dirty the canvas' + pair of functions for the widgety abusers?) -bjk 2010.03.22 */ + + /* magic_switchout(canvas); *//* <-- FIXME: I dislike this -bjk 2009.10.13 */ + rec_undo_buffer(); + /* magic_switchin(canvas); *//* <-- FIXME: I dislike this -bjk 2009.10.13 */ + + if (cur_undo > 0) + undo_ctr = cur_undo - 1; + else + undo_ctr = NUM_UNDO_BUFS - 1; + + last = undo_bufs[undo_ctr]; + + update_rect.x = 0; + update_rect.y = 0; + update_rect.w = 0; + update_rect.h = 0; + + reset_touched(); + + magic_funcs[magics[grp][cur].handle_idx]. + click(magic_api_struct, magics[grp][cur].idx, + magics[grp][cur].mode, canvas, last, old_x, old_y, + &update_rect); + + draw_tux_text(TUX_GREAT, + magics[grp][cur].tip[magic_modeint + (magics[grp][cur].mode)], + 1); + + update_canvas(update_rect.x, update_rect.y, + update_rect.x + update_rect.w, + update_rect.y + update_rect.h); + } + + if (mouseaccessibility) + { + if (magics[grp][cur].mode != MODE_FULLSCREEN && magics[grp][cur].mode != MODE_ONECLICK) /* Note: some non-fullscreen tools are also click-only (not click-and-drag) -bjk 2011.04.26 */ + emulate_button_pressed = !emulate_button_pressed; + } + } + else if (cur_tool == TOOL_ERASER) + { + /* Erase! */ + if (!emulate_button_pressed) + rec_undo_buffer(); + + do_eraser(old_x, old_y, 1); + + if (mouseaccessibility) + emulate_button_pressed = !emulate_button_pressed; + } + else if (cur_tool == TOOL_FILL) + { + Uint32 draw_color, canv_color; + + /* Fill */ + + draw_color = SDL_MapRGB(canvas->format, + color_hexes[cur_color][0], + color_hexes[cur_color][1], + color_hexes[cur_color][2]); + canv_color = + getpixels[canvas->format->BytesPerPixel] (canvas, old_x, + old_y); + + fill_x = old_x; + fill_y = old_y; + + if (would_flood_fill(canvas, draw_color, canv_color)) + { + int x1, y1, x2, y2; + SDL_Surface *last; + int undo_ctr; + + /* We only bother recording an undo buffer + (which may kill our redos) if we're about + to actually change the picture */ + rec_undo_buffer(); + x1 = x2 = old_x; + y1 = y2 = old_y; + + if (cur_undo > 0) + undo_ctr = cur_undo - 1; + else + undo_ctr = NUM_UNDO_BUFS - 1; + + last = undo_bufs[undo_ctr]; + + + for (y1 = 0; y1 < canvas->h; y1++) + { + for (x1 = 0; x1 < canvas->w; x1++) + { + sim_flood_touched[(y1 * canvas->w) + x1] = 0; + } + } + + if (cur_fill == FILL_FLOOD) + { + /* Flood fill a solid color */ + do_flood_fill(screen, texture, renderer, last, canvas, + old_x, old_y, draw_color, canv_color, &x1, + &y1, &x2, &y2, sim_flood_touched); + + update_canvas(x1, y1, x2, y2); + } + else + { + SDL_Surface *tmp_canvas; + + tmp_canvas = SDL_CreateRGBSurface(canvas->flags, + canvas->w, canvas->h, + canvas->format-> + BitsPerPixel, + canvas->format->Rmask, + canvas->format->Gmask, + canvas->format->Bmask, + canvas->format->Amask); + SDL_BlitSurface(canvas, NULL, tmp_canvas, NULL); + + simulate_flood_fill(screen, texture, renderer, last, + tmp_canvas, old_x, old_y, draw_color, + canv_color, &x1, &y1, &x2, &y2, + sim_flood_touched); + SDL_FreeSurface(tmp_canvas); + + sim_flood_x1 = x1; + sim_flood_y1 = y1; + sim_flood_x2 = x2; + sim_flood_y2 = y2; + + if (cur_fill == FILL_GRADIENT_RADIAL) + { + /* Radial gradient */ + draw_radial_gradient(canvas, sim_flood_x1, sim_flood_y1, + sim_flood_x2, sim_flood_y2, old_x, + old_y, draw_color, + sim_flood_touched); + } + else if (cur_fill == FILL_GRADIENT_LINEAR) + { + /* Start a linear gradient */ + draw_linear_gradient(canvas, canvas, sim_flood_x1, + sim_flood_y1, sim_flood_x2, + sim_flood_y2, fill_x, fill_y, old_x, + old_y + 1, draw_color, + sim_flood_touched); + fill_drag_started = 1; + } + else if (cur_fill == FILL_BRUSH) + { + /* Start painting within the fill area */ + draw_brush_fill(canvas, sim_flood_x1, sim_flood_y1, + sim_flood_x2, sim_flood_y2, fill_x, + fill_y, old_x, old_y, draw_color, + sim_flood_touched, &x1, &y1, &x2, &y2); + } + + update_canvas(x1, y1, x2, y2); + } + + draw_tux_text(TUX_GREAT, fill_tips[cur_fill], 1); + } + } + + else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + { + if (onscreen_keyboard && !kbd) + { + r_tir.y = (float) old_y / render_scale; + SDL_SetTextInputRect(&r_tir); + SDL_StartTextInput(); + } + + /* Text and Label Tools! */ + if (cur_tool == TOOL_LABEL && cur_label == LABEL_SELECT) + { + /* Click to select a node to edit */ + + DEBUG_PRINTF("Searching for label @ (%d,%d)\n", old_x, old_y); + + label_node_to_edit = + search_label_list(&highlighted_label_node, old_x, old_y, 0); + + if (label_node_to_edit) + { + DEBUG_PRINTF("Got a label: \"%ls\" @ (%d,%d)\n", + label_node_to_edit->save_texttool_str, old_x, + old_y); + DEBUG_PRINTF("Cursor now @ (%d,%d); width = %d\n", cursor_x, + cursor_y, cursor_textwidth); + select_label_node(&old_x, &old_y); + } + } + else if (cur_tool == TOOL_LABEL && cur_label == LABEL_APPLY) + { + /* Click to select a node to apply it to the canvas */ + + label_node_to_edit = + search_label_list(&highlighted_label_node, old_x, old_y, 0); + if (label_node_to_edit) + apply_label_node(old_x, old_y); + } + else + hide_blinking_cursor(); + + + + if (cursor_x != -1 && cursor_y != -1) + { + /* + if (texttool_len > 0) + { + rec_undo_buffer(); + do_render_cur_text(1); + texttool_len = 0; + } + */ + } + if (onscreen_keyboard && kbd && HIT(kbd_rect) + && !(cur_tool == TOOL_LABEL + && (cur_label == LABEL_SELECT + || cur_label == LABEL_APPLY))) + { + new_kbd = + osk_clicked(kbd, old_x - kbd_rect.x + r_canvas.x, + old_y - kbd_rect.y + r_canvas.y); + /* keyboard has changed, erase the old, note that the old kbd has yet been freed. */ + if (new_kbd != kbd) + { + kbd = new_kbd; + update_canvas_ex(kbd_rect.x, kbd_rect.y, + kbd_rect.x + kbd_rect.w, + kbd_rect.y + kbd_rect.h, 0); + /* set kbd_rect dimensions according to the new keyboard */ + reposition_onscreen_keyboard(-1); + } + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); + update_screen_rect(&kbd_rect); + } + else + { + cursor_x = old_x; + cursor_y = old_y; + cursor_left = old_x; + + if (onscreen_keyboard + && !(cur_tool == TOOL_LABEL + && (cur_label == LABEL_SELECT + || cur_label == LABEL_APPLY))) + { + update_canvas_ex(kbd_rect.x, kbd_rect.y, + kbd_rect.x + kbd_rect.w, + kbd_rect.y + kbd_rect.h, 0); + update_screen_rect(&kbd_rect); + reposition_onscreen_keyboard(old_y); + } + } + + if (onscreen_keyboard && !kbd) + { + r_tir.y = (float) cursor_y / render_scale; + SDL_SetTextInputRect(&r_tir); + SDL_StartTextInput(); + } + + + do_render_cur_text(0); + } + + button_down = 1; + } + } + else if (HIT(r_sfx) && valid_click(event.button.button)) + { + /* A sound player button on the lower left has been pressed! */ +#ifndef NOSOUND + if (cur_tool == TOOL_STAMP && use_sound && !mute) + { + which = GRIDHIT_GD(r_sfx, gd_sfx); + + if (which == 0 + && !stamp_data[stamp_group][cur_stamp[stamp_group]]->no_sound) + { + /* Re-play sound effect: */ + + Mix_ChannelFinished(NULL); + Mix_PlayChannel(2, stamp_data[stamp_group][cur_thing]->ssnd, 0); + } + else if (which == 1 + && + !stamp_data[stamp_group][cur_stamp + [stamp_group]]->no_descsound) + { + Mix_ChannelFinished(NULL); + Mix_PlayChannel(2, stamp_data[stamp_group][cur_thing]->sdesc, + 0); + } + + magic_switchout(canvas); + } +#endif + } + +#ifdef __ANDROID__ + start_motion_convert(event); +#endif + + } + else if (event.type == SDL_MOUSEWHEEL && wheely) + { + int most = 14; + int num_rows_needed; + int xpos, ypos; + SDL_Rect r_controls; + SDL_Rect r_notcontrols; + SDL_Rect r_items; /* = r_notcontrols; */ + + SDL_GetMouseState(&xpos, &ypos); + + /* Scroll wheel code. + WARNING: this must be kept in sync with the mouse-move + code (for cursor changes) and mouse-click code. */ + + if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_STAMP || + cur_tool == TOOL_SHAPES || cur_tool == TOOL_LINES || + cur_tool == TOOL_MAGIC || cur_tool == TOOL_TEXT || + cur_tool == TOOL_ERASER || cur_tool == TOOL_LABEL || + cur_tool == TOOL_FILL) + { + + /* Left tools scroll (via scroll wheel) */ + if (hit_test(&r_tools, xpos, ypos) && NUM_TOOLS > most + TOOLOFFSET) + { + int is_upper = (event.wheel.y > 0); + + if (is_upper && tool_scroll > 0) + { + tool_scroll -= gd_tools.cols; + playsound(screen, 1, SND_SCROLL, 1, event.button.x, + SNDDIST_NEAR); + draw_toolbar(); + } + else if (!is_upper && tool_scroll < NUM_TOOLS - 12 - TOOLOFFSET) + { + tool_scroll += gd_tools.cols; + playsound(screen, 1, SND_SCROLL, 1, event.button.x, + SNDDIST_NEAR); + draw_toolbar(); + } + + if (event.button.y < r_tools.y + button_h / 2) // cursor on the upper button + { + if (tool_scroll == 0) + do_setcursor(cursor_arrow); + else + do_setcursor(cursor_up); + } + + else if (event.button.y > r_tools.y + r_tools.h - button_h / 2) // cursor on the lower button + { + if (tool_scroll < NUM_TOOLS - 12 - TOOLOFFSET) + do_setcursor(cursor_down); + else + do_setcursor(cursor_arrow); + } + + else if (tool_avail[((event.button.x - r_tools.x) / button_w) + + ((event.button.y - + r_tools.y - + button_h / 2) / button_h) * gd_tools.cols + + tool_scroll]) + { + do_setcursor(cursor_hand); + } + else + { + do_setcursor(cursor_arrow); + } + update_screen_rect(&r_tools); + } + + /* Right tool options scroll (via scroll wheel) */ + else + { + grid_dims gd_controls; /* might become 2-by-2 */ + grid_dims gd_items; /* generally becoming 2-by-whatever */ + + gd_controls.rows = 0; + gd_controls.cols = 0; + gd_items.rows = 2; + gd_items.cols = 2; + + /* Note set of things we're dealing with */ + /* (stamps, brushes, etc.) */ + + if (cur_tool == TOOL_STAMP) + { + if (!disable_stamp_controls) + { + /* was 2,2 before adding left/right stamp group buttons -bjk 2007.05.15 */ + gd_controls.rows = 3; + gd_controls.cols = 2; + } + else + { + /* was left 0,0 before adding left/right stamp group buttons -bjk 2007.05.03 */ + gd_controls.rows = 1; + gd_controls.cols = 2; + } + } + else if (cur_tool == TOOL_TEXT) + { + if (!disable_stamp_controls) + { + gd_controls.rows = 2; + gd_controls.cols = 2; + } + } + else if (cur_tool == TOOL_LABEL) + { + if (!disable_stamp_controls) + { + gd_controls.rows = 3; + gd_controls.cols = 2; + } + else + { + gd_controls.rows = 1; + gd_controls.cols = 2; + } + } + else if (cur_tool == TOOL_MAGIC) + { + if (!disable_magic_controls) + { + gd_controls.rows = 2; + gd_controls.cols = 2; + } + else + { + gd_controls.rows = 1; + gd_controls.cols = 2; + } + } + else if (cur_tool == TOOL_SHAPES) + { + if (!disable_shape_controls) + { + gd_controls.rows = 1; + gd_controls.cols = 2; + } + } + else if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) + { + if (!disable_brushspacing) + { + gd_controls.rows = 1; + gd_controls.cols = 2; + } + } + + /* number of whole or partial rows that will be needed + (can make this per-tool if variable columns needed) */ + num_rows_needed = + (num_things + gd_items.cols - 1) / gd_items.cols; + + do_draw = 0; + + r_controls.w = r_toolopt.w; + r_controls.h = gd_controls.rows * button_h; + r_controls.x = r_toolopt.x; + r_controls.y = r_toolopt.y + r_toolopt.h - r_controls.h; + + r_notcontrols.w = r_toolopt.w; + r_notcontrols.h = r_toolopt.h - r_controls.h; + r_notcontrols.x = r_toolopt.x; + r_notcontrols.y = r_toolopt.y; + + r_items.x = r_notcontrols.x; + r_items.y = r_notcontrols.y; + r_items.w = r_notcontrols.w; + r_items.h = r_notcontrols.h; + + if (num_rows_needed * button_h > r_items.h) + { + /* too many; we'll need scroll buttons */ + r_items.h -= button_h; + r_items.y += button_h / 2; + } + gd_items.rows = r_items.h / button_h; + + if (0) + { + } + else + { + /* scroll button */ + int is_upper = (event.wheel.y > (Sint32) 0); + + if ((is_upper && *thing_scroll > 0) /* upper arrow */ + || (!is_upper && *thing_scroll / gd_items.cols < num_rows_needed - gd_items.rows) /* lower arrow */ + ) + { + *thing_scroll += is_upper ? -gd_items.cols : gd_items.cols; + do_draw = 1; + playsound(screen, 1, SND_SCROLL, 1, SNDPOS_RIGHT, + SNDDIST_NEAR); + if (*thing_scroll == 0) { do_setcursor(cursor_arrow); } + } + } - if (button_down || emulate_button_pressed) + /* Assign the change(s), if any / redraw, if needed: */ + + if (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) + { + if (do_draw) + draw_brushes(); + } + else if (cur_tool == TOOL_ERASER) + { + if (do_draw) + draw_erasers(); + } + else if (cur_tool == TOOL_FILL) + { + if (do_draw) + draw_fills(); + } + else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + { + if (do_draw) + draw_fonts(); + } + else if (cur_tool == TOOL_STAMP) + { + if (do_draw) + draw_stamps(); + } + else if (cur_tool == TOOL_SHAPES) + { + if (do_draw) + draw_shapes(); + } + else if (cur_tool == TOOL_MAGIC) + { + if (do_draw) + draw_magic(); + } + + /* Update the screen: */ + if (do_draw) + update_screen_rect(&r_toolopt); + + } + } + } + else if (event.type == SDL_USEREVENT) + { + if (event.user.code == USEREVENT_TEXT_UPDATE) + { + /* Time to replace "Great!" with old tip text: */ + + if (event.user.data1 != NULL) + { + if (((unsigned char *) event.user.data1)[0] == '=') + { + draw_tux_text_ex(TUX_GREAT, (char *) event.user.data1 + 1, 1, (int) (intptr_t) event.user.data2); //EP added (intptr_t) to avoid warning on x64 + } + else + { + draw_tux_text_ex(TUX_GREAT, (char *) event.user.data1, 0, (int) (intptr_t) event.user.data2); //EP added (intptr_t) to avoid warning on x64 + } + } + else + draw_tux_text(TUX_GREAT, "", 1); + } + else if (event.user.code == USEREVENT_PLAYDESCSOUND) + { + /* Play a stamp's spoken description (because the sound effect just finished) */ + /* (This event is pushed into the queue by playstampdesc(), which + is a callback from Mix_ChannelFinished() when playing a stamp SFX) */ + + debug("Playing description sound..."); + +#ifndef NOSOUND + Mix_ChannelFinished(NULL); /* Kill the callback, so we don't get stuck in a loop! */ + + if (event.user.data1 != NULL) + { + if ((int) (intptr_t) event.user.data1 == cur_stamp[stamp_group]) /* Don't play old stamp's sound... *///EP added (intptr_t) to avoid warning on x64 + { + if (!mute && stamp_data[stamp_group][(int) (intptr_t) event.user.data1]->sdesc != NULL) //EP added (intptr_t) to avoid warning on x64 + Mix_PlayChannel(2, stamp_data[stamp_group][(int) (intptr_t) event.user.data1]->sdesc, //EP added (intptr_t) to avoid warning on x64 + 0); + } + } +#endif + } + } + else if (event.type == SDL_MOUSEBUTTONUP) + { + if (scrolling_selector) + { + if (scrolltimer_selector != TIMERID_NONE) + { + SDL_RemoveTimer(scrolltimer_selector); + scrolltimer_selector = TIMERID_NONE; + } + scrolling_selector = 0; + SDL_QuitSubSystem(SDL_INIT_TIMER); + + DEBUG_PRINTF("Killing selector scrolling\n"); + } + + else if (scrolling_tool) + { + if (scrolltimer_tool != TIMERID_NONE) + { + SDL_RemoveTimer(scrolltimer_tool); + scrolltimer_tool = TIMERID_NONE; + } + scrolling_tool = 0; + SDL_QuitSubSystem(SDL_INIT_TIMER); + + DEBUG_PRINTF("Killing tool scrolling\n"); + } + + /* Erase the xor drawed at click */ + else if (cur_tool == TOOL_STAMP && stamp_xored + && event.button.button < 4) + { + stamp_xor(canvas->w / 2, canvas->h / 2); + stamp_xored = 0; + stamp_size_selector_clicked = 0; + update_screen(canvas->w / 2 - (CUR_STAMP_W + 1) / 2 + r_canvas.x, + canvas->h / 2 - (CUR_STAMP_H + 1) / 2 + r_canvas.y, + canvas->w / 2 + (CUR_STAMP_W + 1) / 2 + r_canvas.x, + canvas->h / 2 + (CUR_STAMP_H + 1) / 2 + r_canvas.y); + } + + + if (button_down || emulate_button_pressed) + { + if (cur_tool == TOOL_BRUSH) + { + /* (Drawing on mouse release to fix single click issue) */ + brush_draw(old_x, old_y, old_x, old_y, 1); + } + else if (cur_tool == TOOL_STAMP) + { + if (stamp_tool_mode == STAMP_TOOL_MODE_PLACE) + { + if (old_x >= 0 && old_y >= 0 && old_x <= r_canvas.w + && old_y <= r_canvas.h) + { + if (!no_stamp_rotation) { - if (cur_tool == TOOL_BRUSH) - { - /* Pushing button and moving: Draw with the brush: */ + int mouse_warp_x; - brush_draw(old_x, old_y, new_x, new_y, 1); + /* Going through stamp rotation step, first */ - playsound(screen, 0, paintsound(img_cur_brush_w), 0, event.button.x, SNDDIST_NEAR); - } - else if (cur_tool == TOOL_LINES) - { - /* Still pushing button, while moving: - Draw XOR where line will go: */ + /* Warp mouse to the far right of the stamp, + where we'll start at 0-degrees of rotation + (keep it within the canvas, though!) */ + mouse_warp_x = r_tools.w + old_x + (CUR_STAMP_W / 2); + if (mouse_warp_x >= WINDOW_WIDTH - r_ttoolopt.w) + mouse_warp_x = WINDOW_WIDTH - r_ttoolopt.w - 1; - line_xor(line_start_x, line_start_y, old_x, old_y); + SDL_WarpMouse(mouse_warp_x, old_y); + do_setcursor(cursor_rotate); - line_xor(line_start_x, line_start_y, new_x, new_y); + stamp_tool_mode = STAMP_TOOL_MODE_ROTATE; + stamp_place_x = old_x; + stamp_place_y = old_y; + draw_tux_text(TUX_GREAT, TIP_STAMPS_ROTATING, 1); + } + else + { + /* Draw a stamp! */ + rec_undo_buffer(); + playsound(screen, 1, SND_STAMP, 1, event.button.x, + SNDDIST_NEAR); + stamp_draw(old_x, old_y, 0); + reset_stamps(&stamp_xored_rt, &old_x, &old_y, + &stamp_tool_mode); - if (new_y != line_start_y) - angle = (atan2f((new_x - line_start_x), (new_y - line_start_y)) * 180 / M_PI) - 90.0; // we want straight line to the right to be 0 degrees - else if (new_x >= line_start_x) - angle = 0.0; - else - angle = 180.0; + draw_tux_text(TUX_GREAT, great_str(), 1); - if (angle < 0.0) - angle += 360.0; + /* FIXME: Make delay configurable: */ + + control_drawtext_timer(1000, + stamp_data[stamp_group][cur_stamp + [stamp_group]]-> + stxt, + stamp_data[stamp_group][cur_stamp + [stamp_group]]-> + locale_text); + } + } + } + else if (stamp_tool_mode == STAMP_TOOL_MODE_ROTATE) + { + /* Draw a stamp (finishing rotation step)! */ + rec_undo_buffer(); + playsound(screen, 1, SND_STAMP, 1, stamp_place_x, SNDDIST_NEAR); + int stamp_angle_rotation = + 360 - brush_rotation(stamp_place_x, stamp_place_y, old_x, + old_y); + + stamp_draw(stamp_place_x, stamp_place_y, stamp_angle_rotation); + draw_tux_text(TUX_GREAT, great_str(), 1); + reset_stamps(&stamp_xored_rt, &stamp_place_x, &stamp_place_y, + &stamp_tool_mode); + + /* FIXME: Make delay configurable: */ + + control_drawtext_timer(1000, + stamp_data[stamp_group][cur_stamp + [stamp_group]]-> + stxt, + stamp_data[stamp_group][cur_stamp + [stamp_group]]-> + locale_text); + + } + } + + else if (cur_tool == TOOL_LINES) + { + if (!mouseaccessibility + || (mouseaccessibility && !emulate_button_pressed)) + { + /* (Arbitrarily large, so we draw once now) */ + reset_brush_counter(); + + brush_draw(line_start_x, line_start_y, + event.button.x - r_canvas.x, + event.button.y - r_canvas.y, 1); + brush_draw(event.button.x - r_canvas.x, + event.button.y - r_canvas.y, + event.button.x - r_canvas.x, + event.button.y - r_canvas.y, 1); + + playsound(screen, 1, SND_LINE_END, 1, event.button.x, + SNDDIST_NEAR); + draw_tux_text(TUX_GREAT, tool_tips[TOOL_LINES], 1); + } + } + + else if (cur_tool == TOOL_SHAPES) + { + if (!mouseaccessibility + || (mouseaccessibility && !emulate_button_pressed)) + { + if (shape_tool_mode == SHAPE_TOOL_MODE_STRETCH) + { + /* Now we can rotate the shape... */ + + shape_current_x = event.button.x - r_canvas.x; + shape_current_y = event.button.y - r_canvas.y; + + if (!simple_shapes && !shape_no_rotate[cur_shape]) + { + shape_tool_mode = SHAPE_TOOL_MODE_ROTATE; + + shape_radius = + sqrt((shape_start_x - shape_current_x) * (shape_start_x - + shape_current_x) + + (shape_start_y - + shape_current_y) * (shape_start_y - + shape_current_y)); + + SDL_WarpMouse(shape_current_x + r_ttools.w, shape_start_y); + do_setcursor(cursor_rotate); + + + /* Erase stretchy XOR: */ + + if (abs(shape_start_x - shape_current_x) > 15 + || abs(shape_start_y - shape_current_y) > 15) + do_shape(shape_start_x, shape_start_y, old_x, old_y, 0, + 0); + + /* Make an initial rotation XOR to be erased: */ + + do_shape(shape_start_x, shape_start_y, + shape_current_x, shape_current_y, + shape_rotation(shape_start_x, shape_start_y, + shape_current_x, shape_current_y), + 0); + + playsound(screen, 1, SND_LINE_START, 1, event.button.x, + SNDDIST_NEAR); + draw_tux_text(TUX_BORED, TIP_SHAPE_NEXT, 1); + + + /* FIXME: Do something less intensive! */ + + SDL_Flip(screen); + } + else + { + reset_brush_counter(); + + + playsound(screen, 1, SND_LINE_END, 1, event.button.x, + SNDDIST_NEAR); + do_shape(shape_start_x, shape_start_y, shape_current_x, + shape_current_y, 0, 1); + + SDL_Flip(screen); + + shape_tool_mode = SHAPE_TOOL_MODE_DONE; + draw_tux_text(TUX_GREAT, + shape_tool_tips[simple_shapes ? + SHAPE_COMPLEXITY_SIMPLE : + SHAPE_COMPLEXITY_NORMAL], 1); + } + } + else if (shape_tool_mode == SHAPE_TOOL_MODE_ROTATE) + { + reset_brush_counter(); + + playsound(screen, 1, SND_LINE_END, 1, event.button.x, + SNDDIST_NEAR); + do_shape(shape_start_x, shape_start_y, shape_current_x, + shape_current_y, shape_rotation(shape_start_x, + shape_start_y, + event.button.x - + r_canvas.x, + event.button.y - + r_canvas.y), 1); + + shape_tool_mode = SHAPE_TOOL_MODE_DONE; + draw_tux_text(TUX_GREAT, + shape_tool_tips[simple_shapes ? + SHAPE_COMPLEXITY_SIMPLE : + SHAPE_COMPLEXITY_NORMAL], 1); + + /* FIXME: Do something less intensive! */ + + SDL_Flip(screen); + } + } + } + else if (cur_tool == TOOL_MAGIC + && (magics[magic_group][cur_magic[magic_group]].mode == + MODE_PAINT + || magics[magic_group][cur_magic[magic_group]].mode == + MODE_ONECLICK + || magics[magic_group][cur_magic[magic_group]].mode == + MODE_PAINT_WITH_PREVIEW)) + { + int grp; + int cur; + + grp = magic_group; + cur = cur_magic[grp]; + + if (!mouseaccessibility + || (mouseaccessibility && !emulate_button_pressed)) + { + int undo_ctr; + SDL_Surface *last; + + /* Releasing button: Finish the magic: */ + + if (cur_undo > 0) + undo_ctr = cur_undo - 1; + else + undo_ctr = NUM_UNDO_BUFS - 1; + + last = undo_bufs[undo_ctr]; + + update_rect.x = 0; + update_rect.y = 0; + update_rect.w = 0; + update_rect.h = 0; + + magic_funcs[magics[grp][cur].handle_idx]. + release(magic_api_struct, magics[grp][cur].idx, canvas, last, + old_x, old_y, &update_rect); + + draw_tux_text(TUX_GREAT, + magics[grp][cur].tip[magic_modeint + (magics[grp][cur].mode)], 1); + + update_canvas(update_rect.x, update_rect.y, + update_rect.x + update_rect.w, + update_rect.y + update_rect.h); + } + } + else if (onscreen_keyboard && + (cur_tool == TOOL_TEXT + || (cur_tool == TOOL_LABEL && cur_label != LABEL_SELECT + && cur_label != LABEL_APPLY))) + { + if (onscreen_keyboard && kbd) + { + osk_released(kbd); + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); + update_screen_rect(&kbd_rect); + // SDL_Flip(screen); + } + + if (onscreen_keyboard && !kbd) + { + SDL_StartTextInput(); + } + } + else if (cur_tool == TOOL_FILL) + { + fill_drag_started = 0; + } + } + button_down = 0; + +#ifdef __ANDROID__ + stop_motion_convert(event); +#endif + } + else if (event.type == SDL_MOUSEMOTION && !ignoring_motion) + { + new_x = event.button.x - r_canvas.x; + new_y = event.button.y - r_canvas.y; + +#ifdef __ANDROID__ + convert_motion_to_wheel(event); +#endif + + oldpos_x = event.motion.x; + oldpos_y = event.motion.y; + + + /* FIXME: Is doing this every event too intensive? */ + /* Should I check current cursor first? */ + + if (HIT(r_tools)) + { + int most = buttons_tall * gd_tools.cols; + + /* Tools: */ + + if (NUM_TOOLS > most) + { + if (event.button.y < r_tools.y + button_h / 2) + { + if (tool_scroll > 0) + do_setcursor(cursor_up); + else + do_setcursor(cursor_arrow); + } + else if (event.button.y > r_tools.y + r_tools.h - button_h / 2) + { + if (tool_scroll < + NUM_TOOLS - buttons_tall * gd_tools.cols + gd_tools.cols) + do_setcursor(cursor_down); + else + do_setcursor(cursor_arrow); + } + + else if (tool_avail[((event.button.x - r_tools.x) / button_w) + + ((event.button.y - + r_tools.y - + button_h / 2) / button_h) * gd_tools.cols + + tool_scroll]) + { + do_setcursor(cursor_hand); + } + else + { + do_setcursor(cursor_arrow); + } + + } + + else + { + if (tool_avail[((event.button.x - r_tools.x) / button_w) + + ((event.button.y - + r_tools.y) / button_h) * gd_tools.cols]) + { + do_setcursor(cursor_hand); + } + else + { + do_setcursor(cursor_arrow); + } + } + } + else if (HIT(r_sfx)) + { + /* Sound player buttons: */ + + if (cur_tool == TOOL_STAMP && use_sound && !mute && + ((GRIDHIT_GD(r_sfx, gd_sfx) == 0 && + !stamp_data[stamp_group][cur_stamp[stamp_group]]->no_sound) || + (GRIDHIT_GD(r_sfx, gd_sfx) == 1 && + !stamp_data[stamp_group][cur_stamp + [stamp_group]]->no_descsound))) + { + do_setcursor(cursor_hand); + } + else + { + do_setcursor(cursor_arrow); + } + } + else if (HIT(r_colors)) + { + /* Color picker: */ + if (colors_are_selectable) + do_setcursor(cursor_hand); + else + do_setcursor(cursor_arrow); + } + else if (HIT(r_toolopt)) + { + int control_rows = 0, num_places; + + /* mouse cursor code + WARNING: this must be kept in sync with the mouse-click + and mouse-click code. (it isn't, currently!) */ + + /* Note set of things we're dealing with */ + /* (stamps, brushes, etc.) */ + if (cur_tool == TOOL_STAMP) + { + } + else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + { + } + + /* This if/if/if block is awful -bjk 2022.01.19 */ + if (cur_tool == TOOL_STAMP && !disable_stamp_controls) + control_rows = 3; + + if (cur_tool == TOOL_LABEL) + { + control_rows = 1; + if (!disable_stamp_controls) + control_rows = 3; + } + + if (cur_tool == TOOL_TEXT && !disable_stamp_controls) + control_rows = 2; + if (cur_tool == TOOL_MAGIC) + { + control_rows = 1; + if (!disable_magic_controls) + control_rows = 2; + } + if (cur_tool == TOOL_SHAPES && !disable_shape_controls) + control_rows = 1; + if ((cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) + && !disable_brushspacing) + control_rows = 1; + + num_places = + buttons_tall * gd_toolopt.cols - control_rows * gd_toolopt.cols; + + if (num_things > num_places) + { + /* Are there scroll buttons? */ + num_places = num_places - gd_toolopt.cols; /* Two scroll buttons occupy one row */ + if (event.button.y < r_ttoolopt.h + img_scroll_up->h) + { + /* Up button; is it available? */ + + if (*thing_scroll > 0) + do_setcursor(cursor_up); + else + do_setcursor(cursor_arrow); + } + else if (event.button.y > + (button_h * (num_places / gd_toolopt.cols) + + r_ttoolopt.h + img_scroll_up->h) + && event.button.y <= + (button_h * (num_places / gd_toolopt.cols) + + r_ttoolopt.h + img_scroll_up->h + img_scroll_up->h)) + { + /* Down button; is it available? */ + + if (*thing_scroll < num_things - num_places) + do_setcursor(cursor_down); + else + do_setcursor(cursor_arrow); + } + else + { + /* One of the selectors: */ + + /* FIXME: Also show "cursor_hand" when touching controls (stamp size, brush spacing, etc.!) (See below) -bjk 2022.01.19 */ + + which = + ((event.button.y - r_ttoolopt.h - + img_scroll_up->h) / button_h) * 2 + (event.button.x - + (WINDOW_WIDTH - + r_ttoolopt.w)) / + button_w; + + if (which < num_things) + do_setcursor(cursor_hand); + else + do_setcursor(cursor_arrow); + } + } + else + { + /* No scroll buttons - must be a selector: */ + + /* FIXME: Also show "cursor_hand" when touching controls (stamp size, brush spacing, etc.!) (See above) -bjk 2022.01.19 */ + + which = + ((event.button.y - r_ttoolopt.h) / button_h) * 2 + + (event.button.x - (WINDOW_WIDTH - r_ttoolopt.w)) / button_w; + + if (which < num_things) + do_setcursor(cursor_hand); + else + do_setcursor(cursor_arrow); + } + } + else if (HIT(r_canvas) && keyglobal == 0) + { + /* Canvas: */ + + if (cur_tool == TOOL_BRUSH) + do_setcursor(cursor_brush); + else if (cur_tool == TOOL_STAMP) + { + if (stamp_tool_mode != STAMP_TOOL_MODE_ROTATE) + do_setcursor(cursor_tiny); + else + do_setcursor(cursor_rotate); + } + else if (cur_tool == TOOL_LINES || cur_tool == TOOL_FILL) + do_setcursor(cursor_crosshair); + else if (cur_tool == TOOL_SHAPES) + { + if (shape_tool_mode != SHAPE_TOOL_MODE_ROTATE) + do_setcursor(cursor_crosshair); + else + do_setcursor(cursor_rotate); + } + else if (cur_tool == TOOL_TEXT) + { + if (onscreen_keyboard && HIT(kbd_rect)) + do_setcursor(cursor_hand); + else + do_setcursor(cursor_insertion); + } + else if (cur_tool == TOOL_LABEL) + { + if (cur_label == LABEL_LABEL) + { + if (onscreen_keyboard && HIT(kbd_rect)) + do_setcursor(cursor_hand); + else + do_setcursor(cursor_insertion); + } + else if (cur_label == LABEL_SELECT || cur_label == LABEL_APPLY) + { + if (search_label_list + (¤t_label_node, event.button.x - r_ttools.w, + event.button.y, 1)) + do_setcursor(cursor_hand); + else + do_setcursor(cursor_arrow); + } + } + else if (cur_tool == TOOL_MAGIC) + do_setcursor(cursor_wand); + else if (cur_tool == TOOL_ERASER) + do_setcursor(cursor_tiny); + } + else + { + do_setcursor(cursor_arrow); + } + + + if (button_down || emulate_button_pressed) + { + if (cur_tool == TOOL_BRUSH) + { + /* Pushing button and moving: Draw with the brush: */ + + brush_draw(old_x, old_y, new_x, new_y, 1); + + playsound(screen, 0, paintsound(img_cur_brush_w), 0, + event.button.x, SNDDIST_NEAR); + } + else if (cur_tool == TOOL_LINES) + { + /* Still pushing button, while moving: + Draw XOR where line will go: */ + + line_xor(line_start_x, line_start_y, old_x, old_y); + + line_xor(line_start_x, line_start_y, new_x, new_y); + + if (new_y != line_start_y) + angle = (atan2f((new_x - line_start_x), (new_y - line_start_y)) * 180 / M_PI) - 90.0; // we want straight line to the right to be 0 degrees + else if (new_x >= line_start_x) + angle = 0.0; + else + angle = 180.0; + + if (angle < 0.0) + angle += 360.0; #ifndef __ANDROID__ - update_screen(line_start_x + r_canvas.x, - line_start_y + r_canvas.y, old_x + r_canvas.x, old_y + r_canvas.y); - update_screen(line_start_x + r_canvas.x, - line_start_y + r_canvas.y, new_x + r_canvas.x, new_y + r_canvas.y); - update_screen(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); + update_screen(line_start_x + r_canvas.x, + line_start_y + r_canvas.y, old_x + r_canvas.x, + old_y + r_canvas.y); + update_screen(line_start_x + r_canvas.x, + line_start_y + r_canvas.y, new_x + r_canvas.x, + new_y + r_canvas.y); + update_screen(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); #else - /* Anyway SDL_UpdateRect() backward compatibility function refreshes all the screen on Android */ - SDL_UpdateRect(screen, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); + /* Anyway SDL_UpdateRect() backward compatibility function refreshes all the screen on Android */ + SDL_UpdateRect(screen, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); #endif - snprintf(angle_tool_text, sizeof(angle_tool_text), gettext(TIP_LINE_MOVING), floor(angle)); - draw_tux_text(TUX_BORED, angle_tool_text, 1); - } - else if (cur_tool == TOOL_SHAPES) - { - /* Still pushing button, while moving: - Draw XOR where shape will go: */ + snprintf(angle_tool_text, sizeof(angle_tool_text), + gettext(TIP_LINE_MOVING), floor(angle)); + draw_tux_text(TUX_BORED, angle_tool_text, 1); + } + else if (cur_tool == TOOL_SHAPES) + { + /* Still pushing button, while moving: + Draw XOR where shape will go: */ - if (shape_tool_mode == SHAPE_TOOL_MODE_STRETCH) - { - do_shape(shape_start_x, shape_start_y, old_x, old_y, 0, 0); + if (shape_tool_mode == SHAPE_TOOL_MODE_STRETCH) + { + do_shape(shape_start_x, shape_start_y, old_x, old_y, 0, 0); - do_shape(shape_start_x, shape_start_y, new_x, new_y, 0, 0); + do_shape(shape_start_x, shape_start_y, new_x, new_y, 0, 0); - shape_reverse = (new_x < shape_start_x); + shape_reverse = (new_x < shape_start_x); - /* FIXME: Fix update shape function! */ + /* FIXME: Fix update shape function! */ - /* update_shape(shape_start_x, old_x, new_x, - shape_start_y, old_y, new_y, - shape_locked[cur_shape]); */ + /* update_shape(shape_start_x, old_x, new_x, + shape_start_y, old_y, new_y, + shape_locked[cur_shape]); */ - SDL_Flip(screen); - } - } - else if (cur_tool == TOOL_MAGIC - && (magics[magic_group][cur_magic[magic_group]].mode == MODE_PAINT - || magics[magic_group][cur_magic[magic_group]].mode == MODE_ONECLICK - || magics[magic_group][cur_magic[magic_group]].mode == MODE_PAINT_WITH_PREVIEW)) - { - int undo_ctr; - SDL_Surface *last; + SDL_Flip(screen); + } + } + else if (cur_tool == TOOL_MAGIC + && (magics[magic_group][cur_magic[magic_group]].mode == + MODE_PAINT + || magics[magic_group][cur_magic[magic_group]].mode == + MODE_ONECLICK + || magics[magic_group][cur_magic[magic_group]].mode == + MODE_PAINT_WITH_PREVIEW)) + { + int undo_ctr; + SDL_Surface *last; - /* Pushing button and moving: Continue doing the magic: */ + /* Pushing button and moving: Continue doing the magic: */ - if (cur_undo > 0) - undo_ctr = cur_undo - 1; - else - undo_ctr = NUM_UNDO_BUFS - 1; + if (cur_undo > 0) + undo_ctr = cur_undo - 1; + else + undo_ctr = NUM_UNDO_BUFS - 1; - last = undo_bufs[undo_ctr]; + last = undo_bufs[undo_ctr]; - update_rect.x = 0; - update_rect.y = 0; - update_rect.w = 0; - update_rect.h = 0; + update_rect.x = 0; + update_rect.y = 0; + update_rect.w = 0; + update_rect.h = 0; - magic_funcs[magics[magic_group][cur_magic[magic_group]].handle_idx].drag( - magic_api_struct, - magics[magic_group][cur_magic[magic_group]].idx, - canvas, last, - old_x, old_y, new_x, new_y, &update_rect); + magic_funcs[magics[magic_group] + [cur_magic[magic_group]]. + handle_idx].drag(magic_api_struct, + magics[magic_group][cur_magic + [magic_group]]. + idx, canvas, last, old_x, old_y, + new_x, new_y, &update_rect); - update_canvas(update_rect.x, update_rect.y, - update_rect.x + update_rect.w, update_rect.y + update_rect.h); - } - else if (cur_tool == TOOL_ERASER) - { - int sz; + update_canvas(update_rect.x, update_rect.y, + update_rect.x + update_rect.w, + update_rect.y + update_rect.h); + } + else if (cur_tool == TOOL_ERASER) + { + int sz; - /* Still pushing, and moving - Erase! */ + /* Still pushing, and moving - Erase! */ - eraser_draw(old_x, old_y, new_x, new_y); + eraser_draw(old_x, old_y, new_x, new_y); - sz = calc_eraser_size(cur_eraser); - if (cur_eraser >= NUM_ERASERS / 2) - { - /* Circle eraser */ - circle_xor(new_x, new_y, sz / 2); - } - else - { - /* Square eraser */ - rect_xor(new_x - sz / 2, new_y - sz / 2, new_x + sz / 2, new_y + sz / 2); - } - } - else if (cur_tool == TOOL_FILL && cur_fill == FILL_GRADIENT_LINEAR && fill_drag_started) - { - Uint32 draw_color; - int undo_ctr; - SDL_Surface * last; + sz = calc_eraser_size(cur_eraser); + if (cur_eraser >= NUM_ERASERS / 2) + { + /* Circle eraser */ + circle_xor(new_x, new_y, sz / 2); + } + else + { + /* Square eraser */ + rect_xor(new_x - sz / 2, new_y - sz / 2, new_x + sz / 2, + new_y + sz / 2); + } + } + else if (cur_tool == TOOL_FILL && cur_fill == FILL_GRADIENT_LINEAR + && fill_drag_started) + { + Uint32 draw_color; + int undo_ctr; + SDL_Surface *last; - if (cur_undo > 0) - undo_ctr = cur_undo - 1; - else - undo_ctr = NUM_UNDO_BUFS - 1; + if (cur_undo > 0) + undo_ctr = cur_undo - 1; + else + undo_ctr = NUM_UNDO_BUFS - 1; - last = undo_bufs[undo_ctr]; + last = undo_bufs[undo_ctr]; - /* Pushing button and moving: Update the gradient: */ + /* Pushing button and moving: Update the gradient: */ - draw_color = SDL_MapRGB(canvas->format, - color_hexes[cur_color][0], - color_hexes[cur_color][1], - color_hexes[cur_color][2]); - draw_linear_gradient(canvas, last, sim_flood_x1, sim_flood_y1, sim_flood_x2, sim_flood_y2, - fill_x, fill_y, new_x, new_y, draw_color, sim_flood_touched); + draw_color = SDL_MapRGB(canvas->format, + color_hexes[cur_color][0], + color_hexes[cur_color][1], + color_hexes[cur_color][2]); + draw_linear_gradient(canvas, last, sim_flood_x1, sim_flood_y1, + sim_flood_x2, sim_flood_y2, fill_x, fill_y, + new_x, new_y, draw_color, sim_flood_touched); - update_canvas(sim_flood_x1, sim_flood_y1, sim_flood_x2, sim_flood_y2); - } - else if (cur_tool == TOOL_FILL && cur_fill == FILL_BRUSH) - { - Uint32 draw_color; - int x1, y1, x2, y2; + update_canvas(sim_flood_x1, sim_flood_y1, sim_flood_x2, + sim_flood_y2); + } + else if (cur_tool == TOOL_FILL && cur_fill == FILL_BRUSH) + { + Uint32 draw_color; + int x1, y1, x2, y2; - /* Pushing button and moving: Paint more within the fill area: */ + /* Pushing button and moving: Paint more within the fill area: */ - draw_color = SDL_MapRGB(canvas->format, - color_hexes[cur_color][0], - color_hexes[cur_color][1], - color_hexes[cur_color][2]); + draw_color = SDL_MapRGB(canvas->format, + color_hexes[cur_color][0], + color_hexes[cur_color][1], + color_hexes[cur_color][2]); - draw_brush_fill(canvas, sim_flood_x1, sim_flood_y1, sim_flood_x2, sim_flood_y2, - old_x, old_y, new_x, new_y, draw_color, sim_flood_touched, &x1, &y1, &x2, &y2); + draw_brush_fill(canvas, sim_flood_x1, sim_flood_y1, sim_flood_x2, + sim_flood_y2, old_x, old_y, new_x, new_y, + draw_color, sim_flood_touched, &x1, &y1, &x2, + &y2); - update_canvas(x1, y1, x2, y2); - } - } + update_canvas(x1, y1, x2, y2); + } + } - if (cur_tool == TOOL_STAMP || - ((cur_tool == TOOL_ERASER && !button_down) && - (!mouseaccessibility || (mouseaccessibility && !emulate_button_pressed)))) - { - int w = 0; - int h = 0; + if (cur_tool == TOOL_STAMP || + ((cur_tool == TOOL_ERASER && !button_down) && + (!mouseaccessibility + || (mouseaccessibility && !emulate_button_pressed)))) + { + int w = 0; + int h = 0; - /* Moving: Draw XOR where stamp/eraser will apply: */ + /* Moving: Draw XOR where stamp/eraser will apply: */ - if (cur_tool == TOOL_STAMP) - { - w = active_stamp->w; - h = active_stamp->h; - } - else - { - if (cur_eraser < NUM_ERASERS / 2) - { - w = (ERASER_MIN + - (((NUM_ERASERS / 2) - cur_eraser - 1) * - ((ERASER_MAX - ERASER_MIN) / ((NUM_ERASERS / 2) - 1)))); - } - else - { - w = (ERASER_MIN + - (((NUM_ERASERS / 2) - (cur_eraser - NUM_ERASERS / 2) - 1) * - ((ERASER_MAX - ERASER_MIN) / ((NUM_ERASERS / 2) - 1)))); - } + if (cur_tool == TOOL_STAMP) + { + w = active_stamp->w; + h = active_stamp->h; + } + else + { + if (cur_eraser < NUM_ERASERS / 2) + { + w = (ERASER_MIN + + (((NUM_ERASERS / 2) - cur_eraser - 1) * + ((ERASER_MAX - ERASER_MIN) / ((NUM_ERASERS / 2) - 1)))); + } + else + { + w = (ERASER_MIN + + (((NUM_ERASERS / 2) - (cur_eraser - NUM_ERASERS / 2) - 1) * + ((ERASER_MAX - ERASER_MIN) / ((NUM_ERASERS / 2) - 1)))); + } - h = w; - } + h = w; + } - if (old_x >= 0 && old_x < r_canvas.w && old_y >= 0 && old_y < r_canvas.h) - { - if (cur_tool == TOOL_STAMP) - { - if (stamp_tool_mode == STAMP_TOOL_MODE_ROTATE) - { - stamp_xor(stamp_place_x, stamp_place_y); - update_stamp_xor((360 - brush_rotation(stamp_place_x, stamp_place_y, new_x, new_y)) % 360); - stamp_xor(stamp_place_x, stamp_place_y); + if (old_x >= 0 && old_x < r_canvas.w && old_y >= 0 + && old_y < r_canvas.h) + { + if (cur_tool == TOOL_STAMP) + { + if (stamp_tool_mode == STAMP_TOOL_MODE_ROTATE) + { + stamp_xor(stamp_place_x, stamp_place_y); + update_stamp_xor((360 - + brush_rotation(stamp_place_x, stamp_place_y, + new_x, new_y)) % 360); + stamp_xor(stamp_place_x, stamp_place_y); - /* The half of maximum size the stamp could have when rotating. */ - int half_bigbox = sqrt((CUR_STAMP_W + 1) * (CUR_STAMP_W + 1) + (CUR_STAMP_H + 1) * (CUR_STAMP_H + 1)) / 2; - update_screen(stamp_place_x - half_bigbox + r_canvas.x, - stamp_place_y - half_bigbox + r_canvas.y, - stamp_place_x + half_bigbox + r_canvas.x, - stamp_place_y + half_bigbox + r_canvas.y); - } - else if (stamp_xored_rt) - { - /* Stamp */ - stamp_xor(old_x, old_y); - stamp_xored_rt = !stamp_xored_rt; + /* The half of maximum size the stamp could have when rotating. */ + int half_bigbox = + sqrt((CUR_STAMP_W + 1) * (CUR_STAMP_W + 1) + + (CUR_STAMP_H + 1) * (CUR_STAMP_H + 1)) / 2; + update_screen(stamp_place_x - half_bigbox + r_canvas.x, + stamp_place_y - half_bigbox + r_canvas.y, + stamp_place_x + half_bigbox + r_canvas.x, + stamp_place_y + half_bigbox + r_canvas.y); + } + else if (stamp_xored_rt) + { + /* Stamp */ + stamp_xor(old_x, old_y); + stamp_xored_rt = !stamp_xored_rt; - update_screen(old_x - (CUR_STAMP_W + 1) / 2 + r_canvas.x, - old_y - (CUR_STAMP_H + 1) / 2 + r_canvas.y, - old_x + (CUR_STAMP_W + 1) / 2 + r_canvas.x, - old_y + (CUR_STAMP_H + 1) / 2 + r_canvas.y); - } - } - else - { - if (cur_tool == TOOL_ERASER && cur_eraser >= NUM_ERASERS / 2) - { - /* Circle eraser */ - circle_xor(old_x, old_y, calc_eraser_size(cur_eraser) / 2); - } - else - { - /* Otherwise (square eraser) */ - rect_xor(old_x - w / 2, old_y - h / 2, old_x + w / 2, old_y + h / 2); - } + update_screen(old_x - (CUR_STAMP_W + 1) / 2 + r_canvas.x, + old_y - (CUR_STAMP_H + 1) / 2 + r_canvas.y, + old_x + (CUR_STAMP_W + 1) / 2 + r_canvas.x, + old_y + (CUR_STAMP_H + 1) / 2 + r_canvas.y); + } + } + else + { + if (cur_tool == TOOL_ERASER && cur_eraser >= NUM_ERASERS / 2) + { + /* Circle eraser */ + circle_xor(old_x, old_y, calc_eraser_size(cur_eraser) / 2); + } + else + { + /* Otherwise (square eraser) */ + rect_xor(old_x - w / 2, old_y - h / 2, old_x + w / 2, + old_y + h / 2); + } - update_screen(old_x - w / 2 + r_canvas.x, - old_y - h / 2 + r_canvas.y, - old_x + w / 2 + r_canvas.x, old_y + h / 2 + r_canvas.y); - } - } - if (new_x >= 0 && new_x < r_canvas.w && new_y >= 0 && new_y < r_canvas.h) - { - if (cur_tool == TOOL_STAMP) - { - /* Stamp */ - if (stamp_tool_mode != STAMP_TOOL_MODE_ROTATE){ - stamp_xor(new_x, new_y); - stamp_xored_rt = 1; - } - update_screen(old_x - (CUR_STAMP_W + 1) / 2 + r_canvas.x, - old_y - (CUR_STAMP_H + 1) / 2 + r_canvas.y, - old_x + (CUR_STAMP_W + 1) / 2 + r_canvas.x, - old_y + (CUR_STAMP_H + 1) / 2 + r_canvas.y); - } - else - { - if (cur_tool == TOOL_ERASER && cur_eraser >= NUM_ERASERS / 2) - { - /* Circle eraser */ - circle_xor(new_x, new_y, calc_eraser_size(cur_eraser) / 2); - } - else - { - /* Otherwise (square eraser) */ - rect_xor(new_x - w / 2, new_y - h / 2, new_x + w / 2, new_y + h / 2); - } + update_screen(old_x - w / 2 + r_canvas.x, + old_y - h / 2 + r_canvas.y, + old_x + w / 2 + r_canvas.x, + old_y + h / 2 + r_canvas.y); + } + } + if (new_x >= 0 && new_x < r_canvas.w && new_y >= 0 + && new_y < r_canvas.h) + { + if (cur_tool == TOOL_STAMP) + { + /* Stamp */ + if (stamp_tool_mode != STAMP_TOOL_MODE_ROTATE) + { + stamp_xor(new_x, new_y); + stamp_xored_rt = 1; + } + update_screen(old_x - (CUR_STAMP_W + 1) / 2 + r_canvas.x, + old_y - (CUR_STAMP_H + 1) / 2 + r_canvas.y, + old_x + (CUR_STAMP_W + 1) / 2 + r_canvas.x, + old_y + (CUR_STAMP_H + 1) / 2 + r_canvas.y); + } + else + { + if (cur_tool == TOOL_ERASER && cur_eraser >= NUM_ERASERS / 2) + { + /* Circle eraser */ + circle_xor(new_x, new_y, calc_eraser_size(cur_eraser) / 2); + } + else + { + /* Otherwise (square eraser) */ + rect_xor(new_x - w / 2, new_y - h / 2, new_x + w / 2, + new_y + h / 2); + } - update_screen(new_x - w / 2 + r_canvas.x, - new_y - h / 2 + r_canvas.y, - new_x + w / 2 + r_canvas.x, new_y + h / 2 + r_canvas.y); - } - } + update_screen(new_x - w / 2 + r_canvas.x, + new_y - h / 2 + r_canvas.y, + new_x + w / 2 + r_canvas.x, + new_y + h / 2 + r_canvas.y); + } + } - if (cur_tool == TOOL_STAMP && HIT(r_toolopt) && event.motion.y > r_toolopt.h - && event.motion.state == SDL_PRESSED && stamp_size_selector_clicked) - { - int control_sound = -1; - int w, h; - int old_size; + if (cur_tool == TOOL_STAMP && HIT(r_toolopt) + && event.motion.y > r_toolopt.h + && event.motion.state == SDL_PRESSED + && stamp_size_selector_clicked) + { + int control_sound = -1; + int w, h; + int old_size; #ifdef DEBUG - float choice; + float choice; #endif - old_size = stamp_data[stamp_group][cur_stamp[stamp_group]]->size; - w = CUR_STAMP_W; - h = CUR_STAMP_H; + old_size = stamp_data[stamp_group][cur_stamp[stamp_group]]->size; + w = CUR_STAMP_W; + h = CUR_STAMP_H; - stamp_data[stamp_group][cur_stamp[stamp_group]]->size = (((MAX_STAMP_SIZE - MIN_STAMP_SIZE + 1 - /* +1 to address lack of ability to get back to max default stamp size (SF Bug #1668235 -bjk 2011.01.08) */ - ) * (event.button.x - - (WINDOW_WIDTH - r_ttoolopt.w))) / r_toolopt.w) + - MIN_STAMP_SIZE; + stamp_data[stamp_group][cur_stamp[stamp_group]]->size = + (((MAX_STAMP_SIZE - MIN_STAMP_SIZE + 1 + /* +1 to address lack of ability to get back to max default stamp size (SF Bug #1668235 -bjk 2011.01.08) */ + ) * (event.button.x - + (WINDOW_WIDTH - r_ttoolopt.w))) / r_toolopt.w) + + MIN_STAMP_SIZE; #ifdef DEBUG - printf("Old size = %d, Chose %0.4f, New size =%d\n", old_size, choice, - stamp_data[stamp_group][cur_stamp[stamp_group]]->size); + printf("Old size = %d, Chose %0.4f, New size =%d\n", old_size, + choice, + stamp_data[stamp_group][cur_stamp[stamp_group]]->size); #endif - if (stamp_data[stamp_group][cur_stamp[stamp_group]]->size != old_size) - { - if (stamp_xored) - { - stamp_xor(canvas->w / 2, canvas->h / 2); - stamp_xored = 0; - - update_screen(canvas->w / 2 - (w + 1) / 2 + r_canvas.x, - canvas->h / 2 - (h + 1) / 2 + r_canvas.y, - canvas->w / 2 + (w + 1) / 2 + r_canvas.x, - canvas->h / 2 + (h + 1) / 2 + r_canvas.y); - } - - update_stamp_xor(0); - stamp_xor(canvas->w / 2, canvas->h / 2); - stamp_xored = 1; - update_screen(canvas->w / 2 - (CUR_STAMP_W + 1) / 2 + r_canvas.x, - canvas->h / 2 - (CUR_STAMP_H + 1) / 2 + r_canvas.y, - canvas->w / 2 + (CUR_STAMP_W + 1) / 2 + r_canvas.x, - canvas->h / 2 + (CUR_STAMP_H + 1) / 2 + r_canvas.y); - } - - if (stamp_data[stamp_group][cur_stamp[stamp_group]]->size < old_size) - control_sound = SND_SHRINK; - else if (stamp_data[stamp_group][cur_stamp[stamp_group]]->size > old_size) - control_sound = SND_GROW; - - if (control_sound) - { - playsound(screen, 0, control_sound, 0, SNDPOS_CENTER, SNDDIST_NEAR); - draw_stamps(); - update_screen_rect(&r_toolopt); - } - } - } - else if (cur_tool == TOOL_SHAPES) - { - if (shape_tool_mode == SHAPE_TOOL_MODE_STRETCH && !shape_locked[cur_shape]) - { - float aspect; - int w, h; - - w = abs(shape_start_x - new_x); - h = abs(shape_start_y - new_y); - - - if (w < 2 || h < 2) - aspect = 0; - else if (w > h) - aspect = (float) w / (float) h; - else - aspect = (float) h / (float) w; - - if (aspect == 0 || aspect >= 100) - { - draw_tux_text(TUX_BORED, TIP_SHAPE_START, 1); - } - else - { - snprintf(stretch_tool_text, sizeof(stretch_tool_text), gettext(TIP_SHAPE_STRETCHING_UNLOCKED), aspect); - draw_tux_text(TUX_BORED, stretch_tool_text, 1); - } - } - else if (shape_tool_mode == SHAPE_TOOL_MODE_ROTATE) - { - int deg; - - deg = shape_rotation(shape_start_x, shape_start_y, old_x, old_y); - do_shape(shape_start_x, shape_start_y, shape_current_x, shape_current_y, deg, 0); - - deg = shape_rotation(shape_start_x, shape_start_y, new_x, new_y); - do_shape(shape_start_x, shape_start_y, shape_current_x, shape_current_y, deg, 0); - - deg = -deg; - if (deg < 0) - deg += 360; - - snprintf(angle_tool_text, sizeof(angle_tool_text), gettext(TIP_SHAPE_ROTATING), deg); - draw_tux_text(TUX_BORED, angle_tool_text, 1); - - /* FIXME: Do something less intensive! */ - SDL_Flip(screen); - } - } - - old_x = new_x; - old_y = new_y; - oldpos_x = event.button.x; - oldpos_y = event.button.y; - } - } - - if (cur_tool == TOOL_TEXT || (cur_tool == TOOL_LABEL && cur_label != LABEL_SELECT && cur_label != LABEL_APPLY)) - { - /* if (onscreen_keyboard) */ - /* osk_clicked(kbd, old_x, old_y); */ - /* on_screen_keyboardd(); */ - cur_cursor_blink = SDL_GetTicks(); - - if (cursor_x != -1 && cursor_y != -1 && cur_cursor_blink > last_cursor_blink + CURSOR_BLINK_SPEED) + if (stamp_data[stamp_group][cur_stamp[stamp_group]]->size != + old_size) { - last_cursor_blink = SDL_GetTicks(); - draw_blinking_cursor(); - } - } + if (stamp_xored) + { + stamp_xor(canvas->w / 2, canvas->h / 2); + stamp_xored = 0; - if (cur_tool != TOOL_TEXT && cur_tool != TOOL_LABEL) - { - if (onscreen_keyboard && !kbd) + update_screen(canvas->w / 2 - (w + 1) / 2 + r_canvas.x, + canvas->h / 2 - (h + 1) / 2 + r_canvas.y, + canvas->w / 2 + (w + 1) / 2 + r_canvas.x, + canvas->h / 2 + (h + 1) / 2 + r_canvas.y); + } + + update_stamp_xor(0); + stamp_xor(canvas->w / 2, canvas->h / 2); + stamp_xored = 1; + update_screen(canvas->w / 2 - (CUR_STAMP_W + 1) / 2 + + r_canvas.x, + canvas->h / 2 - (CUR_STAMP_H + 1) / 2 + + r_canvas.y, + canvas->w / 2 + (CUR_STAMP_W + 1) / 2 + + r_canvas.x, + canvas->h / 2 + (CUR_STAMP_H + 1) / 2 + + r_canvas.y); + } + + if (stamp_data[stamp_group][cur_stamp[stamp_group]]->size < + old_size) + control_sound = SND_SHRINK; + else if (stamp_data[stamp_group][cur_stamp[stamp_group]]->size > + old_size) + control_sound = SND_GROW; + + if (control_sound) { - SDL_StopTextInput(); + playsound(screen, 0, control_sound, 0, SNDPOS_CENTER, + SNDDIST_NEAR); + draw_stamps(); + update_screen_rect(&r_toolopt); } + } + } + else if (cur_tool == TOOL_SHAPES) + { + if (shape_tool_mode == SHAPE_TOOL_MODE_STRETCH + && !shape_locked[cur_shape]) + { + float aspect; + int w, h; + + w = abs(shape_start_x - new_x); + h = abs(shape_start_y - new_y); + + + if (w < 2 || h < 2) + aspect = 0; + else if (w > h) + aspect = (float) w / (float) h; + else + aspect = (float) h / (float) w; + + if (aspect == 0 || aspect >= 100) + { + draw_tux_text(TUX_BORED, TIP_SHAPE_START, 1); + } + else + { + snprintf(stretch_tool_text, sizeof(stretch_tool_text), + gettext(TIP_SHAPE_STRETCHING_UNLOCKED), aspect); + draw_tux_text(TUX_BORED, stretch_tool_text, 1); + } + } + else if (shape_tool_mode == SHAPE_TOOL_MODE_ROTATE) + { + int deg; + + deg = shape_rotation(shape_start_x, shape_start_y, old_x, old_y); + do_shape(shape_start_x, shape_start_y, shape_current_x, + shape_current_y, deg, 0); + + deg = shape_rotation(shape_start_x, shape_start_y, new_x, new_y); + do_shape(shape_start_x, shape_start_y, shape_current_x, + shape_current_y, deg, 0); + + deg = -deg; + if (deg < 0) + deg += 360; + + snprintf(angle_tool_text, sizeof(angle_tool_text), + gettext(TIP_SHAPE_ROTATING), deg); + draw_tux_text(TUX_BORED, angle_tool_text, 1); + + /* FIXME: Do something less intensive! */ + SDL_Flip(screen); + } } - if (motioner | hatmotioner) - handle_motioners(oldpos_x, oldpos_y, motioner, hatmotioner, old_hat_ticks, val_x, val_y, valhat_x, valhat_y); - - - SDL_Delay(10); + old_x = new_x; + old_y = new_y; + oldpos_x = event.button.x; + oldpos_y = event.button.y; + } } + + if (cur_tool == TOOL_TEXT + || (cur_tool == TOOL_LABEL && cur_label != LABEL_SELECT + && cur_label != LABEL_APPLY)) + { + /* if (onscreen_keyboard) */ + /* osk_clicked(kbd, old_x, old_y); */ + /* on_screen_keyboardd(); */ + cur_cursor_blink = SDL_GetTicks(); + + if (cursor_x != -1 && cursor_y != -1 + && cur_cursor_blink > last_cursor_blink + CURSOR_BLINK_SPEED) + { + last_cursor_blink = SDL_GetTicks(); + draw_blinking_cursor(); + } + } + + if (cur_tool != TOOL_TEXT && cur_tool != TOOL_LABEL) + { + if (onscreen_keyboard && !kbd) + { + SDL_StopTextInput(); + } + } + + if (motioner | hatmotioner) + handle_motioners(oldpos_x, oldpos_y, motioner, hatmotioner, + old_hat_ticks, val_x, val_y, valhat_x, valhat_y); + + + SDL_Delay(10); + } while (!done); #if defined (__ANDROID__) /* Closing Tux Paint before the end of font scanning resulted in crashes in the Android port */ /* This is an abuse of font_thread_aborted, maybe it is better to use a new, more descriptive marker? */ - if(!font_thread_done) + if (!font_thread_done) font_thread_aborted = 1; #endif } @@ -6734,9 +7171,9 @@ static void mainloop(void) static void hide_blinking_cursor(void) { if (cur_toggle_count & 1) - { - draw_blinking_cursor(); - } + { + draw_blinking_cursor(); + } } /** @@ -6749,12 +7186,14 @@ static void draw_blinking_cursor(void) cur_toggle_count++; line_xor(cursor_x + cursor_textwidth, cursor_y, - cursor_x + cursor_textwidth, cursor_y + TuxPaint_Font_FontHeight(getfonthandle(cur_font))); + cursor_x + cursor_textwidth, + cursor_y + TuxPaint_Font_FontHeight(getfonthandle(cur_font))); update_screen(cursor_x + r_canvas.x + cursor_textwidth, cursor_y + r_canvas.y, cursor_x + r_canvas.x + cursor_textwidth, - cursor_y + r_canvas.y + TuxPaint_Font_FontHeight(getfonthandle(cur_font))); + cursor_y + r_canvas.y + + TuxPaint_Font_FontHeight(getfonthandle(cur_font))); } /** @@ -6795,90 +7234,90 @@ static void brush_draw(int x1, int y1, int x2, int y2, int update) direction = BRUSH_DIRECTION_NONE; r = -1.0; if (brushes_directional[cur_brush] || brushes_rotate[cur_brush]) - { - r = brush_rotation(x1, y1, x2, y2); + { + r = brush_rotation(x1, y1, x2, y2); - if (brushes_directional[cur_brush]) - { - r = r + 22.0; - if (r < 0.0) - r = r + 360.0; - if (x1 != x2 || y1 != y2) - direction = (r / 45.0); - } - else - { - r = 270.0 - r; - } + if (brushes_directional[cur_brush]) + { + r = r + 22.0; + if (r < 0.0) + r = r + 360.0; + if (x1 != x2 || y1 != y2) + direction = (r / 45.0); } + else + { + r = 270.0 - r; + } + } dx = x2 - x1; dy = y2 - y1; if (dx != 0) + { + m = ((float) dy) / ((float) dx); + b = y1 - m * x1; + + if (x2 >= x1) + dx = 1; + else + dx = -1; + + + while (x1 != x2) { - m = ((float)dy) / ((float)dx); - b = y1 - m * x1; + y1 = m * x1 + b; + y2 = m * (x1 + dx) + b; - if (x2 >= x1) - dx = 1; - else - dx = -1; - - - while (x1 != x2) - { - y1 = m * x1 + b; - y2 = m * (x1 + dx) + b; - - if (y1 > y2) - { - for (y = y1; y >= y2; y--) - blit_brush(x1, y, direction, r, &w, &h); - } - else - { - for (y = y1; y <= y2; y++) - blit_brush(x1, y, direction, r, &w, &h); - } - - x1 = x1 + dx; - } - } - else - { if (y1 > y2) - { - y = y1; - y1 = y2; - y2 = y; - } + { + for (y = y1; y >= y2; y--) + blit_brush(x1, y, direction, r, &w, &h); + } + else + { + for (y = y1; y <= y2; y++) + blit_brush(x1, y, direction, r, &w, &h); + } - for (y = y1; y <= y2; y++) - blit_brush(x1, y, direction, r, &w, &h); + x1 = x1 + dx; } + } + else + { + if (y1 > y2) + { + y = y1; + y1 = y2; + y2 = y; + } + + for (y = y1; y <= y2; y++) + blit_brush(x1, y, direction, r, &w, &h); + } if (orig_x1 > orig_x2) - { - tmp = orig_x1; - orig_x1 = orig_x2; - orig_x2 = tmp; - } + { + tmp = orig_x1; + orig_x1 = orig_x2; + orig_x2 = tmp; + } if (orig_y1 > orig_y2) - { - tmp = orig_y1; - orig_y1 = orig_y2; - orig_y2 = tmp; - } + { + tmp = orig_y1; + orig_y1 = orig_y2; + orig_y2 = tmp; + } if (update) - { - sz = max(w,h); - update_canvas(orig_x1 - sz, orig_y1 - sz, orig_x2 + sz, orig_y2 + sz); - } + { + sz = max(w, h); + update_canvas(orig_x1 - sz, orig_y1 - sz, orig_x2 + sz, orig_y2 + sz); + } } @@ -6910,137 +7349,148 @@ void reset_brush_counter(void) * @param direction BRUSH_DIRECTION_... being drawn (for compass direction brushes) * @param rotation angle being drawn (for brushes which may rotate at any angle (0-360 degrees)) */ -static void blit_brush(int x, int y, int direction, double rotation, int * w, int * h) +static void blit_brush(int x, int y, int direction, double rotation, int *w, + int *h) { SDL_Rect src, dest; brush_counter++; if (brush_counter >= img_cur_brush_spacing) + { + brush_counter = 0; + + if (img_cur_brush_frames >= 0) { - brush_counter = 0; + brush_frame++; + if (brush_frame >= img_cur_brush_frames) + brush_frame = 0; + } + else + { + int old_brush_frame = brush_frame; - if (img_cur_brush_frames >= 0) - { - brush_frame++; - if (brush_frame >= img_cur_brush_frames) - brush_frame = 0; - } - else - { - int old_brush_frame = brush_frame; - - do - { - brush_frame = rand() % abs(img_cur_brush_frames); - } - while (brush_frame == old_brush_frame); - } - - dest.x = x; - dest.y = y; - - if (img_cur_brush_directional) - { - if (direction == BRUSH_DIRECTION_UP_LEFT || - direction == BRUSH_DIRECTION_UP || direction == BRUSH_DIRECTION_UP_RIGHT) - { - src.y = 0; - } - else if (direction == BRUSH_DIRECTION_LEFT || - direction == BRUSH_DIRECTION_NONE || direction == BRUSH_DIRECTION_RIGHT) - { - src.y = img_cur_brush_h; - } - else if (direction == BRUSH_DIRECTION_DOWN_LEFT || - direction == BRUSH_DIRECTION_DOWN || direction == BRUSH_DIRECTION_DOWN_RIGHT) - { - src.y = img_cur_brush_h << 1; - } - - if (direction == BRUSH_DIRECTION_UP_LEFT || - direction == BRUSH_DIRECTION_LEFT || direction == BRUSH_DIRECTION_DOWN_LEFT) - { - src.x = brush_frame * img_cur_brush_frame_w; - } - else if (direction == BRUSH_DIRECTION_UP || - direction == BRUSH_DIRECTION_NONE || direction == BRUSH_DIRECTION_DOWN) - { - src.x = brush_frame * img_cur_brush_frame_w + img_cur_brush_w; - } - else if (direction == BRUSH_DIRECTION_UP_RIGHT || - direction == BRUSH_DIRECTION_RIGHT || direction == BRUSH_DIRECTION_DOWN_RIGHT) - { - src.x = brush_frame * img_cur_brush_frame_w + (img_cur_brush_w << 1); - } - } - else - { - src.x = brush_frame * img_cur_brush_w; - src.y = 0; - } - - src.w = img_cur_brush_w; - src.h = img_cur_brush_h; - - if (img_cur_brush_rotate && rotation != -1.0 /* only if we're moving */) - { - SDL_Surface * rotated_brush; - - /* TODO: Cache these; discard them when the user changes the brush or alters its color */ - - rotated_brush = NULL; - - if (img_cur_brush_frames != 1) - { - SDL_Surface * brush_frame_surf; - - brush_frame_surf = - SDL_CreateRGBSurface(img_cur_brush->flags, - src.w, - src.h, - img_cur_brush->format->BitsPerPixel, - img_cur_brush->format->Rmask, img_cur_brush->format->Gmask, img_cur_brush->format->Bmask, - img_cur_brush->format->Amask); - if (brush_frame_surf != NULL) - { - /* 2021/09/28 SDL(2)_gfxBlitRGBA() is not available in the SDL2_gfx library, using plain SDL_BlitSurface() instead. Pere - SDL_gfxBlitRGBA(img_cur_brush, &src, brush_frame_surf, NULL); */ - SDL_BlitSurface(img_cur_brush, &src, brush_frame_surf, NULL); - rotated_brush = rotozoomSurface(brush_frame_surf, rotation, 1.0, SMOOTHING_ON); - SDL_FreeSurface(brush_frame_surf); - } - } - else - { - rotated_brush = rotozoomSurface(img_cur_brush, rotation, 1.0, SMOOTHING_ON); - } - - if (rotated_brush != NULL) - { - src.x = 0; - src.y = 0; - src.w = rotated_brush->w; - src.h = rotated_brush->h; - - dest.x = dest.x - (img_cur_brush_w >> 1) + (rotated_brush->w >> 1); - dest.y = dest.y - (img_cur_brush_h >> 1) + (rotated_brush->h >> 1); - dest.w = rotated_brush->w; - dest.h = rotated_brush->h; - - SDL_BlitSurface(rotated_brush, &src, canvas, &dest); - - SDL_FreeSurface(rotated_brush); - } - } - else - { - SDL_BlitSurface(img_cur_brush, &src, canvas, &dest); - } + do + { + brush_frame = rand() % abs(img_cur_brush_frames); + } + while (brush_frame == old_brush_frame); } - *w = src.w; - *h = src.h; + dest.x = x; + dest.y = y; + + if (img_cur_brush_directional) + { + if (direction == BRUSH_DIRECTION_UP_LEFT || + direction == BRUSH_DIRECTION_UP + || direction == BRUSH_DIRECTION_UP_RIGHT) + { + src.y = 0; + } + else if (direction == BRUSH_DIRECTION_LEFT || + direction == BRUSH_DIRECTION_NONE + || direction == BRUSH_DIRECTION_RIGHT) + { + src.y = img_cur_brush_h; + } + else if (direction == BRUSH_DIRECTION_DOWN_LEFT || + direction == BRUSH_DIRECTION_DOWN + || direction == BRUSH_DIRECTION_DOWN_RIGHT) + { + src.y = img_cur_brush_h << 1; + } + + if (direction == BRUSH_DIRECTION_UP_LEFT || + direction == BRUSH_DIRECTION_LEFT + || direction == BRUSH_DIRECTION_DOWN_LEFT) + { + src.x = brush_frame * img_cur_brush_frame_w; + } + else if (direction == BRUSH_DIRECTION_UP || + direction == BRUSH_DIRECTION_NONE + || direction == BRUSH_DIRECTION_DOWN) + { + src.x = brush_frame * img_cur_brush_frame_w + img_cur_brush_w; + } + else if (direction == BRUSH_DIRECTION_UP_RIGHT || + direction == BRUSH_DIRECTION_RIGHT + || direction == BRUSH_DIRECTION_DOWN_RIGHT) + { + src.x = brush_frame * img_cur_brush_frame_w + (img_cur_brush_w << 1); + } + } + else + { + src.x = brush_frame * img_cur_brush_w; + src.y = 0; + } + + src.w = img_cur_brush_w; + src.h = img_cur_brush_h; + + if (img_cur_brush_rotate && rotation != -1.0 /* only if we're moving */ ) + { + SDL_Surface *rotated_brush; + + /* TODO: Cache these; discard them when the user changes the brush or alters its color */ + + rotated_brush = NULL; + + if (img_cur_brush_frames != 1) + { + SDL_Surface *brush_frame_surf; + + brush_frame_surf = + SDL_CreateRGBSurface(img_cur_brush->flags, + src.w, + src.h, + img_cur_brush->format->BitsPerPixel, + img_cur_brush->format->Rmask, + img_cur_brush->format->Gmask, + img_cur_brush->format->Bmask, + img_cur_brush->format->Amask); + if (brush_frame_surf != NULL) + { + /* 2021/09/28 SDL(2)_gfxBlitRGBA() is not available in the SDL2_gfx library, using plain SDL_BlitSurface() instead. Pere + SDL_gfxBlitRGBA(img_cur_brush, &src, brush_frame_surf, NULL); */ + SDL_BlitSurface(img_cur_brush, &src, brush_frame_surf, NULL); + rotated_brush = + rotozoomSurface(brush_frame_surf, rotation, 1.0, SMOOTHING_ON); + SDL_FreeSurface(brush_frame_surf); + } + } + else + { + rotated_brush = + rotozoomSurface(img_cur_brush, rotation, 1.0, SMOOTHING_ON); + } + + if (rotated_brush != NULL) + { + src.x = 0; + src.y = 0; + src.w = rotated_brush->w; + src.h = rotated_brush->h; + + dest.x = dest.x - (img_cur_brush_w >> 1) + (rotated_brush->w >> 1); + dest.y = dest.y - (img_cur_brush_h >> 1) + (rotated_brush->h >> 1); + dest.w = rotated_brush->w; + dest.h = rotated_brush->h; + + SDL_BlitSurface(rotated_brush, &src, canvas, &dest); + + SDL_FreeSurface(rotated_brush); + } + } + else + { + SDL_BlitSurface(img_cur_brush, &src, canvas, &dest); + } + } + + *w = src.w; + *h = src.h; } @@ -7088,7 +7538,9 @@ static void fill_multichan(multichan * mc, double *up, double *vp) fract = 1.0 / (X + 15.0 * Y + 3.0 * Z); u_prime = 4.0 * X * fract; v_prime = 9.0 * Y * fract; - mc->L = (Y_norm > 0.008856) ? 116.0 * pow(Y_norm, 1.0 / 3.0) - 16.0 : 903.3 * Y_norm; + mc->L = + (Y_norm > 0.008856) ? 116.0 * pow(Y_norm, + 1.0 / 3.0) - 16.0 : 903.3 * Y_norm; u = 13.0 * mc->L * (u_prime - u0_prime); v = 13.0 * mc->L * (v_prime - v0_prime); @@ -7111,34 +7563,37 @@ static double tint_part_1(multichan * work, SDL_Surface * in) double v_total = 0; double u, v; - Uint32(*getpixel) (SDL_Surface *, int, int) = getpixels[in->format->BytesPerPixel]; + Uint32(*getpixel) (SDL_Surface *, int, int) = + getpixels[in->format->BytesPerPixel]; SDL_LockSurface(in); for (yy = 0; yy < in->h; yy++) + { + for (xx = 0; xx < in->w; xx++) { - for (xx = 0; xx < in->w; xx++) - { - multichan *mc = work + yy * in->w + xx; + multichan *mc = work + yy * in->w + xx; - /* put pixels into a more tolerable form */ - SDL_GetRGBA(getpixel(in, xx, yy), in->format, &mc->or, &mc->og, &mc->ob, &mc->alpha); + /* put pixels into a more tolerable form */ + SDL_GetRGBA(getpixel(in, xx, yy), in->format, &mc->or, &mc->og, &mc->ob, + &mc->alpha); - fill_multichan(mc, &u, &v); + fill_multichan(mc, &u, &v); - /* average out u and v, giving more weight to opaque - high-saturation pixels - (this is to take an initial guess at the primary hue) */ + /* average out u and v, giving more weight to opaque + high-saturation pixels + (this is to take an initial guess at the primary hue) */ - u_total += mc->alpha * u * mc->sat; - v_total += mc->alpha * v * mc->sat; + u_total += mc->alpha * u * mc->sat; + v_total += mc->alpha * v * mc->sat; - } } + } SDL_UnlockSurface(in); #ifdef DEBUG - fprintf(stderr, "u_total=%f\nv_total=%f\natan2()=%f\n", u_total, v_total, atan2(u_total, v_total)); + fprintf(stderr, "u_total=%f\nv_total=%f\natan2()=%f\n", u_total, v_total, + atan2(u_total, v_total)); #endif return atan2(u_total, v_total); @@ -7148,14 +7603,15 @@ static double tint_part_1(multichan * work, SDL_Surface * in) /** * FIXME */ -static void change_colors(SDL_Surface * out, multichan * work, double hue_range, multichan * key_color_ptr) +static void change_colors(SDL_Surface * out, multichan * work, + double hue_range, multichan * key_color_ptr) { double lower_hue_1, upper_hue_1, lower_hue_2, upper_hue_2; int xx, yy; multichan dst; double satratio; double slope; - void (*putpixel) (SDL_Surface *, int, int, Uint32); + void (*putpixel)(SDL_Surface *, int, int, Uint32); double old_sat; double newsat; double L; @@ -7174,15 +7630,15 @@ static void change_colors(SDL_Surface * out, multichan * work, double hue_range, lower_hue_1 = key_color.hue - hue_range; upper_hue_1 = key_color.hue + hue_range; if (lower_hue_1 < -M_PI) - { - lower_hue_2 = lower_hue_1 + 2 * M_PI; - upper_hue_2 = upper_hue_1 + 2 * M_PI; - } + { + lower_hue_2 = lower_hue_1 + 2 * M_PI; + upper_hue_2 = upper_hue_1 + 2 * M_PI; + } else - { - lower_hue_2 = lower_hue_1 - 2 * M_PI; - upper_hue_2 = upper_hue_1 - 2 * M_PI; - } + { + lower_hue_2 = lower_hue_1 - 2 * M_PI; + upper_hue_2 = upper_hue_1 - 2 * M_PI; + } /* get the destination color set up */ dst.or = color_hexes[cur_color][0]; @@ -7196,59 +7652,65 @@ static void change_colors(SDL_Surface * out, multichan * work, double hue_range, SDL_LockSurface(out); for (yy = 0; yy < out->h; yy++) + { + for (xx = 0; xx < out->w; xx++) { - for (xx = 0; xx < out->w; xx++) - { - multichan *mc = work + yy * out->w + xx; - double oldhue = mc->hue; + multichan *mc = work + yy * out->w + xx; + double oldhue = mc->hue; - /* if not in the first range, and not in the second range, skip this one - (really should alpha-blend as a function of hue angle difference) */ - if ((oldhue < lower_hue_1 || oldhue > upper_hue_1) && (oldhue < lower_hue_2 || oldhue > upper_hue_2)) - { - putpixel(out, xx, yy, SDL_MapRGBA(out->format, mc->or, mc->og, mc->ob, mc->alpha)); - continue; - } + /* if not in the first range, and not in the second range, skip this one + (really should alpha-blend as a function of hue angle difference) */ + if ((oldhue < lower_hue_1 || oldhue > upper_hue_1) + && (oldhue < lower_hue_2 || oldhue > upper_hue_2)) + { + putpixel(out, xx, yy, + SDL_MapRGBA(out->format, mc->or, mc->og, mc->ob, mc->alpha)); + continue; + } - /* Modify the pixel */ - old_sat = mc->sat; - newsat = old_sat * satratio; - L = mc->L; - if (dst.sat > 0) - L += newsat * slope; /* not greyscale destination */ - else - L += old_sat * (dst.L - key_color.L) / key_color.sat; + /* Modify the pixel */ + old_sat = mc->sat; + newsat = old_sat * satratio; + L = mc->L; + if (dst.sat > 0) + L += newsat * slope; /* not greyscale destination */ + else + L += old_sat * (dst.L - key_color.L) / key_color.sat; - /* convert from L,u,v all the way back to sRGB with 8-bit channels */ - tries = 3; - trysat:; - u = newsat * sin(dst.hue); - v = newsat * cos(dst.hue); + /* convert from L,u,v all the way back to sRGB with 8-bit channels */ + tries = 3; + trysat:; + u = newsat * sin(dst.hue); + v = newsat * cos(dst.hue); - /* Luv to XYZ */ - u_prime = u / (13.0 * L) + u0_prime; - v_prime = v / (13.0 * L) + v0_prime; - Y = (L > 7.99959199307) ? Y0 * pow((L + 16.0) / 116.0, 3.0) : Y0 * L / 903.3; - X = 2.25 * Y * u_prime / v_prime; - Z = (3.0 * Y - 0.75 * Y * u_prime) / v_prime - 5.0 * Y; + /* Luv to XYZ */ + u_prime = u / (13.0 * L) + u0_prime; + v_prime = v / (13.0 * L) + v0_prime; + Y = + (L > 7.99959199307) ? Y0 * pow((L + 16.0) / 116.0, + 3.0) : Y0 * L / 903.3; + X = 2.25 * Y * u_prime / v_prime; + Z = (3.0 * Y - 0.75 * Y * u_prime) / v_prime - 5.0 * Y; - /* coordinate change: XYZ to RGB */ - r = 3.2410 * X + -1.5374 * Y + -0.4986 * Z; - g = -0.9692 * X + 1.8760 * Y + 0.0416 * Z; - b = 0.0556 * X + -0.2040 * Y + 1.0570 * Z; + /* coordinate change: XYZ to RGB */ + r = 3.2410 * X + -1.5374 * Y + -0.4986 * Z; + g = -0.9692 * X + 1.8760 * Y + 0.0416 * Z; + b = 0.0556 * X + -0.2040 * Y + 1.0570 * Z; - /* If it is out of gamut, try to de-saturate it a few times before truncating. - (the linear_to_sRGB function will truncate) */ - if ((r <= -0.5 || g <= -0.5 || b <= -0.5 || r >= 255.5 || g >= 255.5 || b >= 255.5) && tries--) - { - newsat *= 0.8; - goto trysat; - } + /* If it is out of gamut, try to de-saturate it a few times before truncating. + (the linear_to_sRGB function will truncate) */ + if ((r <= -0.5 || g <= -0.5 || b <= -0.5 || r >= 255.5 || g >= 255.5 + || b >= 255.5) && tries--) + { + newsat *= 0.8; + goto trysat; + } - putpixel(out, xx, yy, - SDL_MapRGBA(out->format, linear_to_sRGB(r), linear_to_sRGB(g), linear_to_sRGB(b), mc->alpha)); - } + putpixel(out, xx, yy, + SDL_MapRGBA(out->format, linear_to_sRGB(r), linear_to_sRGB(g), + linear_to_sRGB(b), mc->alpha)); } + } SDL_UnlockSurface(out); } @@ -7256,7 +7718,8 @@ static void change_colors(SDL_Surface * out, multichan * work, double hue_range, /** * FIXME */ -static multichan *find_most_saturated(double initial_hue, multichan * work, unsigned num, double *hue_range_ptr) +static multichan *find_most_saturated(double initial_hue, multichan * work, + unsigned num, double *hue_range_ptr) { /* find the most saturated pixel near the initial hue guess */ multichan *key_color_ptr = NULL; @@ -7270,18 +7733,18 @@ static multichan *find_most_saturated(double initial_hue, multichan * work, unsi multichan *mc; switch (stamp_data[stamp_group][cur_stamp[stamp_group]]->tinter) - { - default: - case TINTER_NORMAL: - hue_range = 18 * M_PI / 180.0; /* plus or minus 18 degrees search, 27 replace */ - break; - case TINTER_NARROW: - hue_range = 6 * M_PI / 180.0; /* plus or minus 6 degrees search, 9 replace */ - break; - case TINTER_ANYHUE: - hue_range = M_PI; /* plus or minus 180 degrees */ - break; - } + { + default: + case TINTER_NORMAL: + hue_range = 18 * M_PI / 180.0; /* plus or minus 18 degrees search, 27 replace */ + break; + case TINTER_NARROW: + hue_range = 6 * M_PI / 180.0; /* plus or minus 6 degrees search, 9 replace */ + break; + case TINTER_ANYHUE: + hue_range = M_PI; /* plus or minus 180 degrees */ + break; + } hue_range_retry:; @@ -7290,39 +7753,40 @@ hue_range_retry:; upper_hue_1 = initial_hue + hue_range; if (lower_hue_1 < -M_PI) - { - lower_hue_2 = lower_hue_1 + 2 * M_PI; - upper_hue_2 = upper_hue_1 + 2 * M_PI; - } + { + lower_hue_2 = lower_hue_1 + 2 * M_PI; + upper_hue_2 = upper_hue_1 + 2 * M_PI; + } else - { - lower_hue_2 = lower_hue_1 - 2 * M_PI; - upper_hue_2 = upper_hue_1 - 2 * M_PI; - } + { + lower_hue_2 = lower_hue_1 - 2 * M_PI; + upper_hue_2 = upper_hue_1 - 2 * M_PI; + } i = num; while (i--) + { + mc = work + i; + + /* if not in the first range, and not in the second range, skip this one */ + if ((mc->hue < lower_hue_1 || mc->hue > upper_hue_1) + && (mc->hue < lower_hue_2 || mc->hue > upper_hue_2)) + continue; + + if (mc->sat > max_sat) { - mc = work + i; - - /* if not in the first range, and not in the second range, skip this one */ - if ((mc->hue < lower_hue_1 || mc->hue > upper_hue_1) && (mc->hue < lower_hue_2 || mc->hue > upper_hue_2)) - continue; - - if (mc->sat > max_sat) - { - max_sat = mc->sat; - key_color_ptr = mc; - } + max_sat = mc->sat; + key_color_ptr = mc; } + } if (!key_color_ptr) - { - hue_range *= 1.5; + { + hue_range *= 1.5; - if (hue_range < M_PI) - goto hue_range_retry; - } + if (hue_range < M_PI) + goto hue_range_retry; + } *hue_range_ptr = hue_range; @@ -7337,8 +7801,10 @@ static void vector_tint_surface(SDL_Surface * out, SDL_Surface * in) { int xx, yy; - Uint32(*getpixel) (SDL_Surface *, int, int) = getpixels[in->format->BytesPerPixel]; - void (*putpixel) (SDL_Surface *, int, int, Uint32) = putpixels[out->format->BytesPerPixel]; + Uint32(*getpixel) (SDL_Surface *, int, int) = + getpixels[in->format->BytesPerPixel]; + void (*putpixel)(SDL_Surface *, int, int, Uint32) = + putpixels[out->format->BytesPerPixel]; double r = sRGB_to_linear_table[color_hexes[cur_color][0]]; double g = sRGB_to_linear_table[color_hexes[cur_color][1]]; @@ -7346,22 +7812,24 @@ static void vector_tint_surface(SDL_Surface * out, SDL_Surface * in) SDL_LockSurface(in); for (yy = 0; yy < in->h; yy++) + { + for (xx = 0; xx < in->w; xx++) { - for (xx = 0; xx < in->w; xx++) - { - unsigned char r8, g8, b8, a8; - double old; + unsigned char r8, g8, b8, a8; + double old; - SDL_GetRGBA(getpixel(in, xx, yy), in->format, &r8, &g8, &b8, &a8); - /* get the linear greyscale value */ - old = - sRGB_to_linear_table[r8] * 0.2126 + sRGB_to_linear_table[g8] * 0.7152 + sRGB_to_linear_table[b8] * 0.0722; + SDL_GetRGBA(getpixel(in, xx, yy), in->format, &r8, &g8, &b8, &a8); + /* get the linear greyscale value */ + old = + sRGB_to_linear_table[r8] * 0.2126 + + sRGB_to_linear_table[g8] * 0.7152 + sRGB_to_linear_table[b8] * 0.0722; - putpixel(out, xx, yy, - SDL_MapRGBA(out->format, linear_to_sRGB(r * old), - linear_to_sRGB(g * old), linear_to_sRGB(b * old), a8)); - } + putpixel(out, xx, yy, + SDL_MapRGBA(out->format, linear_to_sRGB(r * old), + linear_to_sRGB(g * old), linear_to_sRGB(b * old), + a8)); } + } SDL_UnlockSurface(in); } @@ -7385,42 +7853,44 @@ static void tint_surface(SDL_Surface * tmp_surf, SDL_Surface * surf_ptr) work = malloc(sizeof(multichan) * width * height); if (work) + { + initial_hue = tint_part_1(work, surf_ptr); + +#ifdef DEBUG + /* FIXME: To stderr, not stdout? */ + printf("initial_hue = %f\n", initial_hue); +#endif + + key_color_ptr = + find_most_saturated(initial_hue, work, width * height, &hue_range); + +#ifdef DEBUG + /* FIXME: To stderr, not stdout? */ + printf("key_color_ptr = %d\n", (int) (intptr_t) key_color_ptr); //EP added (intptr_t) to avoid warning on x64 +#endif + + if (key_color_ptr) { - initial_hue = tint_part_1(work, surf_ptr); + /* wider for processing than for searching */ + hue_range *= 1.5; -#ifdef DEBUG - /* FIXME: To stderr, not stdout? */ - printf("initial_hue = %f\n", initial_hue); -#endif - - key_color_ptr = find_most_saturated(initial_hue, work, width * height, &hue_range); - -#ifdef DEBUG - /* FIXME: To stderr, not stdout? */ - printf("key_color_ptr = %d\n", (int)(intptr_t) key_color_ptr); //EP added (intptr_t) to avoid warning on x64 -#endif - - if (key_color_ptr) - { - /* wider for processing than for searching */ - hue_range *= 1.5; - - change_colors(tmp_surf, work, hue_range, key_color_ptr); - - free(work); - return; - } - else - { - fprintf(stderr, "find_most_saturated() failed\n"); - } + change_colors(tmp_surf, work, hue_range, key_color_ptr); free(work); + return; } + else + { + fprintf(stderr, "find_most_saturated() failed\n"); + } + + free(work); + } /* Failed! Fall back: */ - fprintf(stderr, "Falling back to tinter=vector, this should be in the *.dat file\n"); + fprintf(stderr, + "Falling back to tinter=vector, this should be in the *.dat file\n"); vector_tint_surface(tmp_surf, surf_ptr); } @@ -7441,111 +7911,121 @@ static void stamp_draw(int x, int y, int stamp_angle_rotation) int dont_free_tmp_surf, dont_free_scaled_surf; if (current_stamp_cached == NULL) + { + Uint32(*getpixel) (SDL_Surface *, int, int); + void (*putpixel)(SDL_Surface *, int, int, Uint32); + + /* Shrink or grow it! */ + scaled_surf = thumbnail(active_stamp, CUR_STAMP_W, CUR_STAMP_H, 0); + dont_free_scaled_surf = 0; + + + surf_ptr = scaled_surf; + + getpixel = getpixels[surf_ptr->format->BytesPerPixel]; + + /* Create a temp surface to play with: */ + if (stamp_colorable(cur_stamp[stamp_group]) + || stamp_tintable(cur_stamp[stamp_group])) { - Uint32(*getpixel) (SDL_Surface *, int, int); - void (*putpixel) (SDL_Surface *, int, int, Uint32); + amask = + ~(surf_ptr->format->Rmask | surf_ptr->format->Gmask | surf_ptr-> + format->Bmask); - /* Shrink or grow it! */ - scaled_surf = thumbnail(active_stamp, CUR_STAMP_W, CUR_STAMP_H, 0); - dont_free_scaled_surf = 0; + tmp_surf = + SDL_CreateRGBSurface(SDL_SWSURFACE, + surf_ptr->w, + surf_ptr->h, + surf_ptr->format->BitsPerPixel, + surf_ptr->format->Rmask, surf_ptr->format->Gmask, + surf_ptr->format->Bmask, amask); + if (tmp_surf == NULL) + { + fprintf(stderr, "\nError: Can't render the colored stamp!\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", SDL_GetError()); - surf_ptr = scaled_surf; + cleanup(); + exit(1); + } - getpixel = getpixels[surf_ptr->format->BytesPerPixel]; - - /* Create a temp surface to play with: */ - if (stamp_colorable(cur_stamp[stamp_group]) || stamp_tintable(cur_stamp[stamp_group])) - { - amask = ~(surf_ptr->format->Rmask | surf_ptr->format->Gmask | surf_ptr->format->Bmask); - - tmp_surf = - SDL_CreateRGBSurface(SDL_SWSURFACE, - surf_ptr->w, - surf_ptr->h, - surf_ptr->format->BitsPerPixel, - surf_ptr->format->Rmask, surf_ptr->format->Gmask, surf_ptr->format->Bmask, amask); - - if (tmp_surf == NULL) - { - fprintf(stderr, "\nError: Can't render the colored stamp!\n" - "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", SDL_GetError()); - - cleanup(); - exit(1); - } - - dont_free_tmp_surf = 0; - } - else - { - /* Not altering color; no need to create temp surf if we don't use it! */ - - tmp_surf = NULL; - dont_free_tmp_surf = 1; - } - - if (tmp_surf != NULL) - putpixel = putpixels[tmp_surf->format->BytesPerPixel]; - else - putpixel = NULL; - - - /* Alter the stamp's color, if needed: */ - - if (stamp_colorable(cur_stamp[stamp_group]) && tmp_surf != NULL) - { - /* Render the stamp in the chosen color: */ - - /* FIXME: It sucks to render this EVERY TIME. Why not just when - they pick the color, or pick the stamp, like with brushes? */ - - /* Render the stamp: */ - - SDL_LockSurface(surf_ptr); - SDL_LockSurface(tmp_surf); - - for (yy = 0; yy < surf_ptr->h; yy++) - { - for (xx = 0; xx < surf_ptr->w; xx++) - { - SDL_GetRGBA(getpixel(surf_ptr, xx, yy), surf_ptr->format, &r, &g, &b, &a); - - putpixel(tmp_surf, xx, yy, - SDL_MapRGBA(tmp_surf->format, - color_hexes[cur_color][0], color_hexes[cur_color][1], color_hexes[cur_color][2], a)); - } - } - - SDL_UnlockSurface(tmp_surf); - SDL_UnlockSurface(surf_ptr); - } - else if (stamp_tintable(cur_stamp[stamp_group])) - { - /* Tintable */ - if (stamp_data[stamp_group][cur_stamp[stamp_group]]->tinter == TINTER_VECTOR) - vector_tint_surface(tmp_surf, surf_ptr); - else - tint_surface(tmp_surf, surf_ptr); - } - else - { - /* No color change, just use it! */ - tmp_surf = surf_ptr; - } - - current_stamp_cached = SDL_DisplayFormatAlpha(tmp_surf); + dont_free_tmp_surf = 0; } - else + else { - tmp_surf = current_stamp_cached; + /* Not altering color; no need to create temp surf if we don't use it! */ + + tmp_surf = NULL; dont_free_tmp_surf = 1; - dont_free_scaled_surf = 1; } + if (tmp_surf != NULL) + putpixel = putpixels[tmp_surf->format->BytesPerPixel]; + else + putpixel = NULL; + + + /* Alter the stamp's color, if needed: */ + + if (stamp_colorable(cur_stamp[stamp_group]) && tmp_surf != NULL) + { + /* Render the stamp in the chosen color: */ + + /* FIXME: It sucks to render this EVERY TIME. Why not just when + they pick the color, or pick the stamp, like with brushes? */ + + /* Render the stamp: */ + + SDL_LockSurface(surf_ptr); + SDL_LockSurface(tmp_surf); + + for (yy = 0; yy < surf_ptr->h; yy++) + { + for (xx = 0; xx < surf_ptr->w; xx++) + { + SDL_GetRGBA(getpixel(surf_ptr, xx, yy), surf_ptr->format, &r, &g, + &b, &a); + + putpixel(tmp_surf, xx, yy, + SDL_MapRGBA(tmp_surf->format, + color_hexes[cur_color][0], + color_hexes[cur_color][1], + color_hexes[cur_color][2], a)); + } + } + + SDL_UnlockSurface(tmp_surf); + SDL_UnlockSurface(surf_ptr); + } + else if (stamp_tintable(cur_stamp[stamp_group])) + { + /* Tintable */ + if (stamp_data[stamp_group][cur_stamp[stamp_group]]->tinter == + TINTER_VECTOR) + vector_tint_surface(tmp_surf, surf_ptr); + else + tint_surface(tmp_surf, surf_ptr); + } + else + { + /* No color change, just use it! */ + tmp_surf = surf_ptr; + } + + current_stamp_cached = SDL_DisplayFormatAlpha(tmp_surf); + } + else + { + tmp_surf = current_stamp_cached; + dont_free_tmp_surf = 1; + dont_free_scaled_surf = 1; + } + /* Rotate the stamp (if no_stamp_rotation is not configured) */ if (stamp_angle_rotation) - tmp_surf = rotozoomSurface(tmp_surf, stamp_angle_rotation, 1.0, SMOOTHING_ON); + tmp_surf = + rotozoomSurface(tmp_surf, stamp_angle_rotation, 1.0, SMOOTHING_ON); /* Where it will go? */ base_x = x - (tmp_surf->w + 1) / 2; @@ -7555,11 +8035,12 @@ static void stamp_draw(int x, int y, int stamp_angle_rotation) /* And blit it! */ dest.x = base_x; dest.y = base_y; - SDL_BlitSurface(tmp_surf, NULL, canvas, &dest); /* FIXME: Conditional jump or move depends on uninitialised value(s) */ + SDL_BlitSurface(tmp_surf, NULL, canvas, &dest); /* FIXME: Conditional jump or move depends on uninitialised value(s) */ update_canvas(x - (tmp_surf->w + 1) / 2, - y - (tmp_surf->h + 1) / 2, x + (tmp_surf->w + 1) / 2, y + (tmp_surf->h + 1) / 2); + y - (tmp_surf->h + 1) / 2, x + (tmp_surf->w + 1) / 2, + y + (tmp_surf->h + 1) / 2); /* Free the temporary surfaces */ @@ -7594,29 +8075,30 @@ static void rec_undo_buffer(void) #ifdef DEBUG /* FIXME: Stderr instead of stdout? */ - printf("DRAW: Current=%d Oldest=%d Newest=%d\n", cur_undo, oldest_undo, newest_undo); + printf("DRAW: Current=%d Oldest=%d Newest=%d\n", cur_undo, oldest_undo, + newest_undo); #endif /* Update toolbar buttons, if needed: */ if (tool_avail[TOOL_UNDO] == 0) - { - tool_avail[TOOL_UNDO] = 1; - wanna_update_toolbar = 1; - } + { + tool_avail[TOOL_UNDO] = 1; + wanna_update_toolbar = 1; + } if (tool_avail[TOOL_REDO]) - { - tool_avail[TOOL_REDO] = 0; - wanna_update_toolbar = 1; - } + { + tool_avail[TOOL_REDO] = 0; + wanna_update_toolbar = 1; + } if (wanna_update_toolbar) - { - draw_toolbar(); - update_screen_rect(&r_tools); - } + { + draw_toolbar(); + update_screen_rect(&r_tools); + } } @@ -7638,7 +8120,8 @@ void show_version(int details) printf("\nBuilt with these options:\n"); - printf(" SDL version %d.%d.%d\n", SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL); + printf(" SDL version %d.%d.%d\n", SDL_MAJOR_VERSION, SDL_MINOR_VERSION, + SDL_PATCHLEVEL); /* Quality reductions: */ @@ -7647,11 +8130,13 @@ void show_version(int details) #endif #ifdef LOW_QUALITY_COLOR_SELECTOR - printf(" Low Quality Color Selector enabled (LOW_QUALITY_COLOR_SELECTOR)\n"); + printf + (" Low Quality Color Selector enabled (LOW_QUALITY_COLOR_SELECTOR)\n"); #endif #ifdef LOW_QUALITY_STAMP_OUTLINE - printf(" Low Quality Stamp Outline enabled (LOW_QUALITY_STAMP_OUTLINE)\n"); + printf + (" Low Quality Stamp Outline enabled (LOW_QUALITY_STAMP_OUTLINE)\n"); #endif #ifdef NO_PROMPT_SHADOWS @@ -7892,14 +8377,19 @@ static unsigned compute_default_scale_factor(double ratio) unsigned defsize = HARD_MAX_STAMP_SIZE; while (defsize > 0) - { - double this_err = good_log - log(scaletable[defsize].numer / (double)scaletable[defsize].denom); - double next_err = good_log - log(scaletable[defsize - 1].numer / (double)scaletable[defsize - 1].denom); + { + double this_err = + good_log - + log(scaletable[defsize].numer / (double) scaletable[defsize].denom); + double next_err = + good_log - + log(scaletable[defsize - 1].numer / + (double) scaletable[defsize - 1].denom); - if (fabs(next_err) > fabs(this_err)) - break; - defsize--; - } + if (fabs(next_err) > fabs(this_err)) + break; + defsize--; + } return defsize; } @@ -7916,9 +8406,10 @@ static unsigned compute_default_scale_factor(double ratio) */ static void loadbrush_callback(SDL_Surface * screen, __attribute__((unused)) SDL_Texture * texture, - __attribute__((unused)) SDL_Renderer * renderer, - const char *restrict const dir, - unsigned dirlen, tp_ftw_str * files, unsigned i, const char *restrict const locale) + __attribute__((unused)) SDL_Renderer * + renderer, const char *restrict const dir, + unsigned dirlen, tp_ftw_str * files, + unsigned i, const char *restrict const locale) { FILE *fi; char buf[64]; @@ -7926,109 +8417,122 @@ static void loadbrush_callback(SDL_Surface * screen, int brush_w, brush_h; float scale; - (void)dirlen; - (void)locale; + (void) dirlen; + (void) locale; qsort(files, i, sizeof *files, compare_ftw_str); while (i--) + { + show_progress_bar(screen); + if (strcasestr(files[i].str, ".png")) { - show_progress_bar(screen); - if (strcasestr(files[i].str, ".png")) + char fname[512]; + + if (strcasecmp(files[i].str, SHAPE_BRUSH_NAME) == 0) + shape_brush = num_brushes; + + safe_snprintf(fname, sizeof fname, "%s/%s", dir, files[i].str); + if (num_brushes == num_brushes_max) + { + num_brushes_max = num_brushes_max * 5 / 4 + 4; + img_brushes = + realloc(img_brushes, num_brushes_max * sizeof *img_brushes); + img_brushes_thumbs = + realloc(img_brushes_thumbs, + num_brushes_max * sizeof *img_brushes_thumbs); + brushes_frames = + realloc(brushes_frames, num_brushes_max * sizeof(int)); + brushes_directional = + realloc(brushes_directional, num_brushes_max * sizeof(short)); + brushes_rotate = + realloc(brushes_rotate, num_brushes_max * sizeof(short)); + brushes_spacing = + realloc(brushes_spacing, num_brushes_max * sizeof(int)); + } + img_brushes[num_brushes] = loadimage(fname); + + /* Load brush metadata, if any: */ + + brushes_frames[num_brushes] = 1; + brushes_directional[num_brushes] = 0; + brushes_rotate[num_brushes] = 0; + brushes_spacing[num_brushes] = img_brushes[num_brushes]->h / 4; + + strcpy(strcasestr(fname, ".png"), ".dat"); /* FIXME: Use strncpy (ugh, complicated) */ + fi = fopen(fname, "r"); + + want_rand = 0; + + if (fi != NULL) + { + do { - char fname[512]; - - if (strcasecmp(files[i].str, SHAPE_BRUSH_NAME) == 0) - shape_brush = num_brushes; - - safe_snprintf(fname, sizeof fname, "%s/%s", dir, files[i].str); - if (num_brushes == num_brushes_max) + if (fgets(buf, sizeof(buf), fi)) + { + if (strstr(buf, "frames=") != NULL) { - num_brushes_max = num_brushes_max * 5 / 4 + 4; - img_brushes = realloc(img_brushes, num_brushes_max * sizeof *img_brushes); - img_brushes_thumbs = realloc(img_brushes_thumbs, num_brushes_max * sizeof *img_brushes_thumbs); - brushes_frames = realloc(brushes_frames, num_brushes_max * sizeof(int)); - brushes_directional = realloc(brushes_directional, num_brushes_max * sizeof(short)); - brushes_rotate = realloc(brushes_rotate, num_brushes_max * sizeof(short)); - brushes_spacing = realloc(brushes_spacing, num_brushes_max * sizeof(int)); + brushes_frames[num_brushes] = atoi(strstr(buf, "frames=") + 7); } - img_brushes[num_brushes] = loadimage(fname); - - /* Load brush metadata, if any: */ - - brushes_frames[num_brushes] = 1; - brushes_directional[num_brushes] = 0; - brushes_rotate[num_brushes] = 0; - brushes_spacing[num_brushes] = img_brushes[num_brushes]->h / 4; - - strcpy(strcasestr(fname, ".png"), ".dat"); /* FIXME: Use strncpy (ugh, complicated) */ - fi = fopen(fname, "r"); - - want_rand = 0; - - if (fi != NULL) + else if (strstr(buf, "spacing=") != NULL) { - do - { - if (fgets(buf, sizeof(buf), fi)) - { - if (strstr(buf, "frames=") != NULL) - { - brushes_frames[num_brushes] = atoi(strstr(buf, "frames=") + 7); - } - else if (strstr(buf, "spacing=") != NULL) - { - brushes_spacing[num_brushes] = atoi(strstr(buf, "spacing=") + 8); - } - else if (strstr(buf, "directional") != NULL) - { - brushes_directional[num_brushes] = 1; - } - else if (strstr(buf, "rotate") != NULL) - { - brushes_rotate[num_brushes] = 1; - } - else if (strstr(buf, "random") != NULL) - { - want_rand = 1; - } - } - } - while (!feof(fi)); - fclose(fi); - - if (want_rand) - brushes_frames[num_brushes] *= -1; + brushes_spacing[num_brushes] = + atoi(strstr(buf, "spacing=") + 8); } - - /* Generate thumbnail */ - brush_w = ((img_brushes[num_brushes]->w / abs(brushes_frames[num_brushes])) / (brushes_directional[num_brushes] ? 3 : 1)); - brush_h = (img_brushes[num_brushes]->h / (brushes_directional[num_brushes] ? 3 : 1)); - - if (brush_w <= button_w && brush_h <= button_h - ) { - img_brushes_thumbs[num_brushes] = duplicate_surface(img_brushes[num_brushes]); - } else { - if (brush_w > brush_h) { - scale = (float) ((float)button_w / (float)brush_w); - } else { - scale = (float) ((float)button_h / (float)brush_h); + else if (strstr(buf, "directional") != NULL) + { + brushes_directional[num_brushes] = 1; + } + else if (strstr(buf, "rotate") != NULL) + { + brushes_rotate[num_brushes] = 1; + } + else if (strstr(buf, "random") != NULL) + { + want_rand = 1; } - - img_brushes_thumbs[num_brushes] = - thumbnail2( - img_brushes[num_brushes], - img_brushes[num_brushes]->w * scale, - img_brushes[num_brushes]->h * scale, - 0, /* no need to ask to keep aspect; already kept */ - 1 /* keep alpha */ - ); } - - num_brushes++; } - free(files[i].str); + while (!feof(fi)); + fclose(fi); + + if (want_rand) + brushes_frames[num_brushes] *= -1; + } + + /* Generate thumbnail */ + brush_w = + ((img_brushes[num_brushes]->w / abs(brushes_frames[num_brushes])) / + (brushes_directional[num_brushes] ? 3 : 1)); + brush_h = + (img_brushes[num_brushes]->h / + (brushes_directional[num_brushes] ? 3 : 1)); + + if (brush_w <= button_w && brush_h <= button_h) + { + img_brushes_thumbs[num_brushes] = + duplicate_surface(img_brushes[num_brushes]); + } + else + { + if (brush_w > brush_h) + { + scale = (float) ((float) button_w / (float) brush_w); + } + else + { + scale = (float) ((float) button_h / (float) brush_h); + } + + img_brushes_thumbs[num_brushes] = thumbnail2(img_brushes[num_brushes], img_brushes[num_brushes]->w * scale, img_brushes[num_brushes]->h * scale, 0, /* no need to ask to keep aspect; already kept */ + 1 /* keep alpha */ + ); + } + + num_brushes++; } + free(files[i].str); + } free(files); } @@ -8036,7 +8540,8 @@ static void loadbrush_callback(SDL_Surface * screen, /** * FIXME */ -static void load_brush_dir(SDL_Surface * screen, const char *restrict const dir) +static void load_brush_dir(SDL_Surface * screen, + const char *restrict const dir) { char buf[TP_FTW_PATHSIZE]; unsigned dirlen = strlen(dir); @@ -8061,28 +8566,28 @@ SDL_Surface *mirror_surface(SDL_Surface * s) SDL_SetSurfaceBlendMode(s, SDL_BLENDMODE_NONE); if (new_surf != NULL) + { + for (x = 0; x < s->w; x++) { - for (x = 0; x < s->w; x++) - { - src.x = x; - src.y = 0; - src.w = 1; - src.h = s->h; + src.x = x; + src.y = 0; + src.w = 1; + src.h = s->h; - dest.x = s->w - x - 1; - dest.y = 0; + dest.x = s->w - x - 1; + dest.y = 0; - SDL_BlitSurface(s, &src, new_surf, &dest); - } - - SDL_FreeSurface(s); - - return (new_surf); + SDL_BlitSurface(s, &src, new_surf, &dest); } + + SDL_FreeSurface(s); + + return (new_surf); + } else - { - return (s); - } + { + return (s); + } } /** @@ -8101,28 +8606,28 @@ SDL_Surface *flip_surface(SDL_Surface * s) SDL_SetSurfaceBlendMode(s, SDL_BLENDMODE_NONE); if (new_surf != NULL) + { + for (y = 0; y < s->h; y++) { - for (y = 0; y < s->h; y++) - { - src.x = 0; - src.y = y; - src.w = s->w; - src.h = 1; + src.x = 0; + src.y = y; + src.w = s->w; + src.h = 1; - dest.x = 0; - dest.y = s->h - y - 1; + dest.x = 0; + dest.y = s->h - y - 1; - SDL_BlitSurface(s, &src, new_surf, &dest); - } - - SDL_FreeSurface(s); - - return (new_surf); + SDL_BlitSurface(s, &src, new_surf, &dest); } + + SDL_FreeSurface(s); + + return (new_surf); + } else - { - return (s); - } + { + return (s); + } } static unsigned default_stamp_size; @@ -8130,7 +8635,8 @@ static unsigned default_stamp_size; /** * FIXME */ -static void loadstamp_finisher(stamp_type * sd, unsigned w, unsigned h, double ratio) +static void loadstamp_finisher(stamp_type * sd, unsigned w, unsigned h, + double ratio) { unsigned int upper = HARD_MAX_STAMP_SIZE; unsigned int underscanned_upper = HARD_MAX_STAMP_SIZE; @@ -8138,7 +8644,8 @@ static void loadstamp_finisher(stamp_type * sd, unsigned w, unsigned h, double r unsigned mid; #ifdef DEBUG - printf("Finishing %s for %dx%d (ratio=%0.4f)\n", sd->stampname, w, h, ratio); + printf("Finishing %s for %dx%d (ratio=%0.4f)\n", sd->stampname, w, h, + ratio); #endif /* If Tux Paint is in mirror-image-by-default mode, mirror, if we can: */ @@ -8146,85 +8653,85 @@ static void loadstamp_finisher(stamp_type * sd, unsigned w, unsigned h, double r sd->mirrored = 1; do - { - scaleparams *s = &scaletable[upper]; - int pw, ph; /* proposed width and height */ + { + scaleparams *s = &scaletable[upper]; + int pw, ph; /* proposed width and height */ - pw = (w * s->numer + s->denom - 1) / s->denom; - ph = (h * s->numer + s->denom - 1) / s->denom; + pw = (w * s->numer + s->denom - 1) / s->denom; + ph = (h * s->numer + s->denom - 1) / s->denom; #ifdef ALLOW_STAMP_OVERSCAN - /* OK to let a stamp stick off the sides in one direction, not two */ - /* By default, Tux Paint allowed stamps to be, at max, 2x as wide OR 2x as tall as canvas; scaled that back to 1.5 -bjk 2011.01.08 */ - if (pw < canvas->w * 1.5 && ph < canvas->h * 1) - { + /* OK to let a stamp stick off the sides in one direction, not two */ + /* By default, Tux Paint allowed stamps to be, at max, 2x as wide OR 2x as tall as canvas; scaled that back to 1.5 -bjk 2011.01.08 */ + if (pw < canvas->w * 1.5 && ph < canvas->h * 1) + { #ifdef DEBUG - printf("Upper at %d with proposed size %dx%d (wide)\n", upper, pw, ph); -#endif - if (pw > canvas->w) - { - underscanned_upper = upper - 1; - } - else - { - underscanned_upper = upper; - } - break; - } - if (pw < canvas->w * 1 && ph < canvas->h * 1.5) - { -#ifdef DEBUG - printf("Upper at %d with proposed size %dx%d (tall)\n", upper, pw, ph); -#endif - if (ph > canvas->h) - { - underscanned_upper = upper - 1; - } - else - { - underscanned_upper = upper; - } - break; - } -#else - if (pw <= canvas->w * 1 && ph <= canvas->h * 1) - { -#ifdef DEBUG - printf("Upper at %d with proposed size %dx%d\n", upper, pw, ph); -#endif - underscanned_upper = upper; - break; - } + printf("Upper at %d with proposed size %dx%d (wide)\n", upper, pw, ph); #endif + if (pw > canvas->w) + { + underscanned_upper = upper - 1; + } + else + { + underscanned_upper = upper; + } + break; } + if (pw < canvas->w * 1 && ph < canvas->h * 1.5) + { +#ifdef DEBUG + printf("Upper at %d with proposed size %dx%d (tall)\n", upper, pw, ph); +#endif + if (ph > canvas->h) + { + underscanned_upper = upper - 1; + } + else + { + underscanned_upper = upper; + } + break; + } +#else + if (pw <= canvas->w * 1 && ph <= canvas->h * 1) + { +#ifdef DEBUG + printf("Upper at %d with proposed size %dx%d\n", upper, pw, ph); +#endif + underscanned_upper = upper; + break; + } +#endif + } while (--upper); do + { + scaleparams *s = &scaletable[lower]; + int pw, ph; /* proposed width and height */ + + pw = (w * s->numer + s->denom - 1) / s->denom; + ph = (h * s->numer + s->denom - 1) / s->denom; + + if (pw * ph > 20) { - scaleparams *s = &scaletable[lower]; - int pw, ph; /* proposed width and height */ - - pw = (w * s->numer + s->denom - 1) / s->denom; - ph = (h * s->numer + s->denom - 1) / s->denom; - - if (pw * ph > 20) - { #ifdef DEBUG - printf("Lower at %d with proposed size %dx%d\n", lower, pw, ph); + printf("Lower at %d with proposed size %dx%d\n", lower, pw, ph); #endif - break; - } + break; } + } while (++lower < HARD_MAX_STAMP_SIZE); if (upper < lower) - { - /* this, if it ever happens, is very bad */ - upper = (upper + lower) / 2; - lower = upper; - } + { + /* this, if it ever happens, is very bad */ + upper = (upper + lower) / 2; + lower = upper; + } mid = default_stamp_size; if (ratio != 1.0) @@ -8232,9 +8739,9 @@ static void loadstamp_finisher(stamp_type * sd, unsigned w, unsigned h, double r /* Ratio override for SVGs! */ if (ratio == 1.0 && sd->is_svg) - { - mid = compute_default_scale_factor(0.2); - } + { + mid = compute_default_scale_factor(0.2); + } if (mid > upper) mid = upper; @@ -8254,12 +8761,12 @@ static void loadstamp_finisher(stamp_type * sd, unsigned w, unsigned h, double r #endif if (stamp_size_override != -1) - { - sd->size = (((upper - lower) * stamp_size_override) / 10) + lower; + { + sd->size = (((upper - lower) * stamp_size_override) / 10) + lower; #ifdef DEBUG - printf("...but adjusting size to %d\n", sd->size); + printf("...but adjusting size to %d\n", sd->size); #endif - } + } #ifdef DEBUG printf("\n"); #endif @@ -8277,10 +8784,10 @@ static void loadstamp_finisher(stamp_type * sd, unsigned w, unsigned h, double r static void clear_cached_stamp(void) { if (current_stamp_cached != NULL) - { - SDL_FreeSurface(current_stamp_cached); - current_stamp_cached = NULL; - } + { + SDL_FreeSurface(current_stamp_cached); + current_stamp_cached = NULL; + } } /** @@ -8310,196 +8817,196 @@ static void set_active_stamp(void) needs_flip = sd->flipped; if (sd->mirrored && sd->flipped) + { + /* Want mirrored and flipped, both */ + +#ifdef DEBUG + printf("want both mirrored & flipped\n"); +#endif + + if (!sd->no_premirrorflip) { - /* Want mirrored and flipped, both */ - -#ifdef DEBUG - printf("want both mirrored & flipped\n"); -#endif - - if (!sd->no_premirrorflip) - { #ifndef NOSVG - memcpy(buf + len, "_mirror_flip.svg", 17); - active_stamp = do_loadimage(buf, 0); -#endif - - if (active_stamp == NULL) - { - memcpy(buf + len, "_mirror_flip.png", 17); - active_stamp = do_loadimage(buf, 0); - } - } - - - if (active_stamp != NULL) - { -#ifdef DEBUG - printf("found a _mirror_flip!\n"); -#endif - - needs_mirror = 0; - needs_flip = 0; - } - else - { - /* Couldn't get one that was both, look for _mirror then _flip and - flip or mirror it: */ - -#ifdef DEBUG - printf("didn't find a _mirror_flip\n"); -#endif - - if (!sd->no_premirror) - { -#ifndef NOSVG - memcpy(buf + len, "_mirror.svg", 12); - active_stamp = do_loadimage(buf, 0); -#endif - - if (active_stamp == NULL) - { - memcpy(buf + len, "_mirror.png", 12); - active_stamp = do_loadimage(buf, 0); - } - } - - if (active_stamp != NULL) - { -#ifdef DEBUG - printf("found a _mirror!\n"); -#endif - needs_mirror = 0; - } - else - { - /* Couldn't get one that was just pre-mirrored, look for a - pre-flipped */ - -#ifdef DEBUG - printf("didn't find a _mirror, either\n"); -#endif - - if (!sd->no_preflip) - { -#ifndef NOSVG - memcpy(buf + len, "_flip.svg", 10); - active_stamp = do_loadimage(buf, 0); -#endif - - if (active_stamp == NULL) - { - memcpy(buf + len, "_flip.png", 10); - active_stamp = do_loadimage(buf, 0); - } - } - - if (active_stamp != NULL) - { -#ifdef DEBUG - printf("found a _flip!\n"); -#endif - needs_flip = 0; - } - else - { -#ifdef DEBUG - printf("didn't find a _flip, either\n"); -#endif - } - } - } - } - else if (sd->flipped && !sd->no_preflip) - { - /* Want flipped only */ - -#ifdef DEBUG - printf("want flipped only\n"); -#endif - -#ifndef NOSVG - memcpy(buf + len, "_flip.svg", 10); + memcpy(buf + len, "_mirror_flip.svg", 17); active_stamp = do_loadimage(buf, 0); #endif if (active_stamp == NULL) + { + memcpy(buf + len, "_mirror_flip.png", 17); + active_stamp = do_loadimage(buf, 0); + } + } + + + if (active_stamp != NULL) + { +#ifdef DEBUG + printf("found a _mirror_flip!\n"); +#endif + + needs_mirror = 0; + needs_flip = 0; + } + else + { + /* Couldn't get one that was both, look for _mirror then _flip and + flip or mirror it: */ + +#ifdef DEBUG + printf("didn't find a _mirror_flip\n"); +#endif + + if (!sd->no_premirror) + { +#ifndef NOSVG + memcpy(buf + len, "_mirror.svg", 12); + active_stamp = do_loadimage(buf, 0); +#endif + + if (active_stamp == NULL) { - memcpy(buf + len, "_flip.png", 10); + memcpy(buf + len, "_mirror.png", 12); active_stamp = do_loadimage(buf, 0); } + } if (active_stamp != NULL) + { +#ifdef DEBUG + printf("found a _mirror!\n"); +#endif + needs_mirror = 0; + } + else + { + /* Couldn't get one that was just pre-mirrored, look for a + pre-flipped */ + +#ifdef DEBUG + printf("didn't find a _mirror, either\n"); +#endif + + if (!sd->no_preflip) + { +#ifndef NOSVG + memcpy(buf + len, "_flip.svg", 10); + active_stamp = do_loadimage(buf, 0); +#endif + + if (active_stamp == NULL) + { + memcpy(buf + len, "_flip.png", 10); + active_stamp = do_loadimage(buf, 0); + } + } + + if (active_stamp != NULL) { #ifdef DEBUG printf("found a _flip!\n"); #endif needs_flip = 0; } - else + else { #ifdef DEBUG - printf("didn't find a _flip\n"); + printf("didn't find a _flip, either\n"); #endif } + } } - else if (sd->mirrored && !sd->no_premirror) - { - /* Want mirrored only */ + } + else if (sd->flipped && !sd->no_preflip) + { + /* Want flipped only */ #ifdef DEBUG - printf("want mirrored only\n"); + printf("want flipped only\n"); #endif #ifndef NOSVG - memcpy(buf + len, "_mirror.svg", 12); + memcpy(buf + len, "_flip.svg", 10); + active_stamp = do_loadimage(buf, 0); +#endif + + if (active_stamp == NULL) + { + memcpy(buf + len, "_flip.png", 10); active_stamp = do_loadimage(buf, 0); -#endif - - if (active_stamp == NULL) - { - memcpy(buf + len, "_mirror.png", 12); - active_stamp = do_loadimage(buf, 0); - } - - if (active_stamp != NULL) - { -#ifdef DEBUG - printf("found a _mirror!\n"); -#endif - needs_mirror = 0; - } - else - { -#ifdef DEBUG - printf("didn't find a _mirror\n"); -#endif - } } + if (active_stamp != NULL) + { +#ifdef DEBUG + printf("found a _flip!\n"); +#endif + needs_flip = 0; + } + else + { +#ifdef DEBUG + printf("didn't find a _flip\n"); +#endif + } + } + else if (sd->mirrored && !sd->no_premirror) + { + /* Want mirrored only */ + +#ifdef DEBUG + printf("want mirrored only\n"); +#endif + +#ifndef NOSVG + memcpy(buf + len, "_mirror.svg", 12); + active_stamp = do_loadimage(buf, 0); +#endif + + if (active_stamp == NULL) + { + memcpy(buf + len, "_mirror.png", 12); + active_stamp = do_loadimage(buf, 0); + } + + if (active_stamp != NULL) + { +#ifdef DEBUG + printf("found a _mirror!\n"); +#endif + needs_mirror = 0; + } + else + { +#ifdef DEBUG + printf("didn't find a _mirror\n"); +#endif + } + } + /* Didn't want mirrored, or flipped, or couldn't load anything that was pre-rendered: */ if (!active_stamp) - { + { #ifdef DEBUG - printf("loading normal\n"); + printf("loading normal\n"); #endif #ifndef NOSVG - memcpy(buf + len, ".svg", 5); - active_stamp = do_loadimage(buf, 0); + memcpy(buf + len, ".svg", 5); + active_stamp = do_loadimage(buf, 0); #endif - if (active_stamp == NULL) - { - memcpy(buf + len, ".png", 5); - active_stamp = do_loadimage(buf, 0); - } - + if (active_stamp == NULL) + { + memcpy(buf + len, ".png", 5); + active_stamp = do_loadimage(buf, 0); } + } + /* Never allow a NULL image! */ if (!active_stamp) @@ -8510,20 +9017,20 @@ static void set_active_stamp(void) do it to the image we did load: */ if (needs_mirror) - { + { #ifdef DEBUG - printf("mirroring\n"); + printf("mirroring\n"); #endif - active_stamp = mirror_surface(active_stamp); - } + active_stamp = mirror_surface(active_stamp); + } if (needs_flip) - { + { #ifdef DEBUG - printf("flipping\n"); + printf("flipping\n"); #endif - active_stamp = flip_surface(active_stamp); - } + active_stamp = flip_surface(active_stamp); + } #ifdef DEBUG printf("\n\n"); @@ -8552,84 +9059,85 @@ static void get_stamp_thumb(stamp_type * sd, int process_sound) memcpy(buf, sd->stampname, len); if (!sd->processed) - { - memcpy(buf + len, ".dat", 5); - ratio = loadinfo(buf, sd); - } + { + memcpy(buf + len, ".dat", 5); + ratio = loadinfo(buf, sd); + } else - { - /* So here, unless an SVG stamp has a .dat file with a 'scale', - the Stamp ends up defaulting to 100% (ratio=1.0). - Since we render the SVG as large as possible, for quality reasons, - we almost never want the _default_ size to be 100%. + { + /* So here, unless an SVG stamp has a .dat file with a 'scale', + the Stamp ends up defaulting to 100% (ratio=1.0). + Since we render the SVG as large as possible, for quality reasons, + we almost never want the _default_ size to be 100%. - So we need to either (a) keep track of the SVG's own pixel size - and try to set the default size to something close to that, - or (b) pick a universal initial size that we can apply to _all_ SVGs - where the initial size is left unspecified (which means knowing when - they're SVGs). + So we need to either (a) keep track of the SVG's own pixel size + and try to set the default size to something close to that, + or (b) pick a universal initial size that we can apply to _all_ SVGs + where the initial size is left unspecified (which means knowing when + they're SVGs). - So far, I'm doing (b), in loadstamp_finisher... + So far, I'm doing (b), in loadstamp_finisher... - -bjk 2009.09.29 */ + -bjk 2009.09.29 */ - ratio = 1.0; - } + ratio = 1.0; + } if (!sd->no_txt && !sd->stxt) - { - /* damn thing wants a .png extension; give it one */ - memcpy(buf + len, ".png", 5); - sd->stxt = loaddesc(buf, &(sd->locale_text)); - sd->no_txt = !sd->stxt; - } + { + /* damn thing wants a .png extension; give it one */ + memcpy(buf + len, ".png", 5); + sd->stxt = loaddesc(buf, &(sd->locale_text)); + sd->no_txt = !sd->stxt; + } #ifndef NOSOUND if (!sd->sound_processed && process_sound) + { + /* good time to load the sound */ + if (!sd->no_sound && !sd->ssnd && use_sound) { - /* good time to load the sound */ - if (!sd->no_sound && !sd->ssnd && use_sound) - { - /* damn thing wants a .png extension; give it one */ - memcpy(buf + len, ".png", 5); - sd->ssnd = loadsound(buf); - sd->no_sound = !sd->ssnd; - } - - /* ...and the description */ - if (!sd->no_descsound && !sd->sdesc && use_sound) - { - /* damn thing wants a .png extension; give it one */ - memcpy(buf + len, ".png", 5); - sd->sdesc = loaddescsound(buf); - sd->no_descsound = !sd->sdesc; - } - - sd->sound_processed = 1; + /* damn thing wants a .png extension; give it one */ + memcpy(buf + len, ".png", 5); + sd->ssnd = loadsound(buf); + sd->no_sound = !sd->ssnd; } + + /* ...and the description */ + if (!sd->no_descsound && !sd->sdesc && use_sound) + { + /* damn thing wants a .png extension; give it one */ + memcpy(buf + len, ".png", 5); + sd->sdesc = loaddescsound(buf); + sd->no_descsound = !sd->sdesc; + } + + sd->sound_processed = 1; + } #endif /* first see if we can re-use an existing thumbnail */ if (sd->thumbnail) + { +#ifdef DEBUG + printf("have an sd->thumbnail\n"); +#endif + + if (sd->thumb_mirrored_flipped == sd->flipped && + sd->thumb_mirrored_flipped == sd->mirrored && + sd->mirrored == sd->thumb_mirrored + && sd->flipped == sd->thumb_flipped) { -#ifdef DEBUG - printf("have an sd->thumbnail\n"); -#endif - - if (sd->thumb_mirrored_flipped == sd->flipped && - sd->thumb_mirrored_flipped == sd->mirrored && - sd->mirrored == sd->thumb_mirrored && sd->flipped == sd->thumb_flipped) - { - /* It's already the way we want */ + /* It's already the way we want */ #ifdef DEBUG - printf("mirrored == flipped == thumb_mirrored_flipped [bye]\n"); + printf("mirrored == flipped == thumb_mirrored_flipped [bye]\n"); #endif - return; - } + return; } + } /* nope, see if there's a pre-rendered one we can use */ @@ -8641,179 +9149,179 @@ static void get_stamp_thumb(stamp_type * sd, int process_sound) bigimg = NULL; if (sd->mirrored && sd->flipped) + { +#ifdef DEBUG + printf("want mirrored & flipped\n"); +#endif + + if (!sd->no_premirrorflip) { -#ifdef DEBUG - printf("want mirrored & flipped\n"); -#endif - - if (!sd->no_premirrorflip) - { - memcpy(buf + len, "_mirror_flip.png", 17); - bigimg = do_loadimage(buf, 0); - -#ifndef NOSVG - if (bigimg == NULL) - { - memcpy(buf + len, "_mirror_flip.svg", 17); - bigimg = do_loadimage(buf, 0); - } -#endif - } - - if (bigimg) - { -#ifdef DEBUG - printf("found a _mirror_flip!\n"); -#endif - - need_mirror = 0; - need_flip = 0; - } - else - { -#ifdef DEBUG - printf("didn't find a mirror_flip\n"); -#endif - sd->no_premirrorflip = 1; - - if (!sd->no_premirror) - { - memcpy(buf + len, "_mirror.png", 12); - bigimg = do_loadimage(buf, 0); - -#ifndef NOSVG - if (bigimg == NULL) - { - memcpy(buf + len, "_mirror.svg", 12); - bigimg = do_loadimage(buf, 0); - } -#endif - } - - if (bigimg) - { -#ifdef DEBUG - printf("found a _mirror\n"); -#endif - - need_mirror = 0; - } - else - { -#ifdef DEBUG - printf("didn't find a mirror\n"); -#endif - - if (!sd->no_preflip) - { - memcpy(buf + len, "_flip.png", 10); - bigimg = do_loadimage(buf, 0); - -#ifndef NOSVG - if (bigimg == NULL) - { - memcpy(buf + len, "_flip.svg", 10); - bigimg = do_loadimage(buf, 0); - } -#endif - } - - if (bigimg) - { -#ifdef DEBUG - printf("found a _flip\n"); -#endif - - need_flip = 0; - } - } - } - } - else if (sd->mirrored && !sd->no_premirror) - { -#ifdef DEBUG - printf("want mirrored only\n"); -#endif - - memcpy(buf + len, "_mirror.png", 12); + memcpy(buf + len, "_mirror_flip.png", 17); bigimg = do_loadimage(buf, 0); #ifndef NOSVG if (bigimg == NULL) + { + memcpy(buf + len, "_mirror_flip.svg", 17); + bigimg = do_loadimage(buf, 0); + } +#endif + } + + if (bigimg) + { +#ifdef DEBUG + printf("found a _mirror_flip!\n"); +#endif + + need_mirror = 0; + need_flip = 0; + } + else + { +#ifdef DEBUG + printf("didn't find a mirror_flip\n"); +#endif + sd->no_premirrorflip = 1; + + if (!sd->no_premirror) + { + memcpy(buf + len, "_mirror.png", 12); + bigimg = do_loadimage(buf, 0); + +#ifndef NOSVG + if (bigimg == NULL) { memcpy(buf + len, "_mirror.svg", 12); bigimg = do_loadimage(buf, 0); } #endif + } if (bigimg) - { + { #ifdef DEBUG - printf("found a _mirror!\n"); -#endif - need_mirror = 0; - } - else - { -#ifdef DEBUG - printf("didn't find a mirror\n"); -#endif - sd->no_premirror = 1; - } - } - else if (sd->flipped && !sd->no_preflip) - { -#ifdef DEBUG - printf("want flipped only\n"); + printf("found a _mirror\n"); #endif - memcpy(buf + len, "_flip.png", 10); - bigimg = do_loadimage(buf, 0); + need_mirror = 0; + } + else + { +#ifdef DEBUG + printf("didn't find a mirror\n"); +#endif + + if (!sd->no_preflip) + { + memcpy(buf + len, "_flip.png", 10); + bigimg = do_loadimage(buf, 0); #ifndef NOSVG - if (bigimg == NULL) - { - memcpy(buf + len, "_flip.svg", 10); - bigimg = do_loadimage(buf, 0); + if (bigimg == NULL) + { + memcpy(buf + len, "_flip.svg", 10); + bigimg = do_loadimage(buf, 0); + } +#endif } + + if (bigimg) + { +#ifdef DEBUG + printf("found a _flip\n"); #endif - if (bigimg) - { -#ifdef DEBUG - printf("found a _flip!\n"); -#endif need_flip = 0; } - else - { -#ifdef DEBUG - printf("didn't find a flip\n"); -#endif - sd->no_preflip = 1; - } + } } + } + else if (sd->mirrored && !sd->no_premirror) + { +#ifdef DEBUG + printf("want mirrored only\n"); +#endif + + memcpy(buf + len, "_mirror.png", 12); + bigimg = do_loadimage(buf, 0); + +#ifndef NOSVG + if (bigimg == NULL) + { + memcpy(buf + len, "_mirror.svg", 12); + bigimg = do_loadimage(buf, 0); + } +#endif + + if (bigimg) + { +#ifdef DEBUG + printf("found a _mirror!\n"); +#endif + need_mirror = 0; + } + else + { +#ifdef DEBUG + printf("didn't find a mirror\n"); +#endif + sd->no_premirror = 1; + } + } + else if (sd->flipped && !sd->no_preflip) + { +#ifdef DEBUG + printf("want flipped only\n"); +#endif + + memcpy(buf + len, "_flip.png", 10); + bigimg = do_loadimage(buf, 0); + +#ifndef NOSVG + if (bigimg == NULL) + { + memcpy(buf + len, "_flip.svg", 10); + bigimg = do_loadimage(buf, 0); + } +#endif + + if (bigimg) + { +#ifdef DEBUG + printf("found a _flip!\n"); +#endif + need_flip = 0; + } + else + { +#ifdef DEBUG + printf("didn't find a flip\n"); +#endif + sd->no_preflip = 1; + } + } /* If we didn't load a pre-rendered, load the normal one: */ if (!bigimg) - { + { #ifdef DEBUG - printf("loading normal...\n"); + printf("loading normal...\n"); #endif - memcpy(buf + len, ".png", 5); - bigimg = do_loadimage(buf, 0); + memcpy(buf + len, ".png", 5); + bigimg = do_loadimage(buf, 0); #ifndef NOSVG - if (bigimg == NULL) - { - memcpy(buf + len, ".svg", 5); - bigimg = do_loadimage(buf, 0); - } -#endif + if (bigimg == NULL) + { + memcpy(buf + len, ".svg", 5); + bigimg = do_loadimage(buf, 0); } +#endif + } /* Scale the stamp down to its thumbnail size: */ @@ -8823,18 +9331,18 @@ static void get_stamp_thumb(stamp_type * sd, int process_sound) int ww = (40 * button_h) / ORIGINAL_BUTTON_SIZE; int hh = (40 * button_h) / ORIGINAL_BUTTON_SIZE; if (bigimg) - { - w = bigimg->w; - h = bigimg->h; - } + { + w = bigimg->w; + h = bigimg->h; + } if (!bigimg) sd->thumbnail = thumbnail(img_dead40x40, ww, hh, 1); /* copy */ else if (bigimg->w > 40 || bigimg->h > 40) - { - sd->thumbnail = thumbnail(bigimg, ww, hh, 1); - SDL_FreeSurface(bigimg); - } + { + sd->thumbnail = thumbnail(bigimg, ww, hh, 1); + SDL_FreeSurface(bigimg); + } else sd->thumbnail = bigimg; @@ -8842,20 +9350,20 @@ static void get_stamp_thumb(stamp_type * sd, int process_sound) /* Mirror and/or flip the thumbnail, if we still need to do so: */ if (need_mirror) - { + { #ifdef DEBUG - printf("mirroring\n"); + printf("mirroring\n"); #endif - sd->thumbnail = mirror_surface(sd->thumbnail); - } + sd->thumbnail = mirror_surface(sd->thumbnail); + } if (need_flip) - { + { #ifdef DEBUG - printf("flipping\n"); + printf("flipping\n"); #endif - sd->thumbnail = flip_surface(sd->thumbnail); - } + sd->thumbnail = flip_surface(sd->thumbnail); + } /* Note the fact that the thumbnail's mirror/flip is the same as the main @@ -8896,137 +9404,145 @@ static void get_stamp_thumb(stamp_type * sd, int process_sound) */ static void loadstamp_callback(SDL_Surface * screen, __attribute__((unused)) SDL_Texture * texture, - __attribute__((unused)) SDL_Renderer * renderer, - const char *restrict const dir, - unsigned dirlen, tp_ftw_str * files, unsigned i, const char *restrict const locale) + __attribute__((unused)) SDL_Renderer * + renderer, const char *restrict const dir, + unsigned dirlen, tp_ftw_str * files, + unsigned i, const char *restrict const locale) { - (void)locale; + (void) locale; #ifdef DEBUG /* FIXME: Stderr instead of stdout? */ printf("loadstamp_callback (%d): %s\n", i, dir); #endif if (num_stamps[stamp_group] > 0) + { + /* If previous group had any stamps... */ + + unsigned int i, slashcount; + + + /* See if the current directory is shallow enough to be + important for making a new stamp group: */ + + slashcount = 0; + + for (i = strlen(load_stamp_basedir) + 1; i < strlen(dir); i++) { - /* If previous group had any stamps... */ - - unsigned int i, slashcount; - - - /* See if the current directory is shallow enough to be - important for making a new stamp group: */ - - slashcount = 0; - - for (i = strlen(load_stamp_basedir) + 1; i < strlen(dir); i++) - { - if (dir[i] == '/' || dir[i] == '\\') - slashcount++; - } - - if (slashcount <= stamp_group_dir_depth) - { - stamp_group++; -#ifdef DEBUG - /* FIXME: Stderr instead of stdout? */ - printf("\n...counts as a new group! now: %d\n", stamp_group); -#endif - } - else - { -#ifdef DEBUG - /* FIXME: Stderr instead of stdout? */ - printf("...is still part of group %d\n", stamp_group); -#endif - } + if (dir[i] == '/' || dir[i] == '\\') + slashcount++; } + if (slashcount <= stamp_group_dir_depth) + { + stamp_group++; +#ifdef DEBUG + /* FIXME: Stderr instead of stdout? */ + printf("\n...counts as a new group! now: %d\n", stamp_group); +#endif + } + else + { +#ifdef DEBUG + /* FIXME: Stderr instead of stdout? */ + printf("...is still part of group %d\n", stamp_group); +#endif + } + } + /* Sort and iterate the file list: */ qsort(files, i, sizeof *files, compare_ftw_str); while (i--) - { - char fname[512]; - const char *dotext, *ext, *mirror_ext, *flip_ext, *mirrorflip_ext; + { + char fname[512]; + const char *dotext, *ext, *mirror_ext, *flip_ext, *mirrorflip_ext; - ext = ".png"; - mirror_ext = "_mirror.png"; - flip_ext = "_flip.png"; - mirrorflip_ext = "_mirror_flip.png"; - dotext = (char *)strcasestr(files[i].str, ext); + ext = ".png"; + mirror_ext = "_mirror.png"; + flip_ext = "_flip.png"; + mirrorflip_ext = "_mirror_flip.png"; + dotext = (char *) strcasestr(files[i].str, ext); #ifndef NOSVG - if (dotext == NULL) - { - ext = ".svg"; - mirror_ext = "_mirror.svg"; - flip_ext = "_flip.svg"; - mirrorflip_ext = "_mirror_flip.svg"; - dotext = (char *)strcasestr(files[i].str, ext); - } - else - { - /* Found PNG, but we support SVG; let's see if there's an SVG - * version too, before loading the PNG */ + if (dotext == NULL) + { + ext = ".svg"; + mirror_ext = "_mirror.svg"; + flip_ext = "_flip.svg"; + mirrorflip_ext = "_mirror_flip.svg"; + dotext = (char *) strcasestr(files[i].str, ext); + } + else + { + /* Found PNG, but we support SVG; let's see if there's an SVG + * version too, before loading the PNG */ - char svgname[512]; - FILE *fi; + char svgname[512]; + FILE *fi; - safe_snprintf(svgname, sizeof(svgname), "%s/%s", dir, files[i].str); - strcpy(strcasestr(svgname, ".png"), ".svg"); /* FIXME: Use strncpy (ugh, complicated) */ + safe_snprintf(svgname, sizeof(svgname), "%s/%s", dir, files[i].str); + strcpy(strcasestr(svgname, ".png"), ".svg"); /* FIXME: Use strncpy (ugh, complicated) */ - fi = fopen(svgname, "r"); - if (fi != NULL) - { - debug("Found SVG version of "); - debug(files[i].str); - debug("\n"); + fi = fopen(svgname, "r"); + if (fi != NULL) + { + debug("Found SVG version of "); + debug(files[i].str); + debug("\n"); - fclose(fi); - continue; /* ugh, i hate continues */ - } - } + fclose(fi); + continue; /* ugh, i hate continues */ + } + } #endif - /* - * Showing the progress bar across the screen can be CPU-intensive, so - * update infrequently. - */ - if ((i % 32) == 0) - show_progress_bar(screen); + /* + * Showing the progress bar across the screen can be CPU-intensive, so + * update infrequently. + */ + if ((i % 32) == 0) + show_progress_bar(screen); - if (dotext > files[i].str && !strcasecmp(dotext, ext) - && (dotext - files[i].str + 1 + dirlen < (int)(sizeof fname)) - && !strcasestr(files[i].str, mirror_ext) - && !strcasestr(files[i].str, flip_ext) && !strcasestr(files[i].str, mirrorflip_ext)) - { - safe_snprintf(fname, sizeof fname, "%s/%s", dir, files[i].str); - if (num_stamps[stamp_group] == max_stamps[stamp_group]) - { - max_stamps[stamp_group] = max_stamps[stamp_group] * 5 / 4 + 15; - stamp_data[stamp_group] = realloc(stamp_data[stamp_group], - max_stamps[stamp_group] * sizeof(*stamp_data[stamp_group])); - } - stamp_data[stamp_group][num_stamps[stamp_group]] = - calloc(1, sizeof *stamp_data[stamp_group][num_stamps[stamp_group]]); - stamp_data[stamp_group][num_stamps[stamp_group]]->stampname = malloc(dotext - files[i].str + 1 + dirlen + 1); - memcpy(stamp_data[stamp_group][num_stamps[stamp_group]]->stampname, fname, - dotext - files[i].str + 1 + dirlen); - stamp_data[stamp_group][num_stamps[stamp_group]]->stampname[dotext - files[i].str + 1 + dirlen] = '\0'; + if (dotext > files[i].str && !strcasecmp(dotext, ext) + && (dotext - files[i].str + 1 + dirlen < (int) (sizeof fname)) + && !strcasestr(files[i].str, mirror_ext) + && !strcasestr(files[i].str, flip_ext) + && !strcasestr(files[i].str, mirrorflip_ext)) + { + safe_snprintf(fname, sizeof fname, "%s/%s", dir, files[i].str); + if (num_stamps[stamp_group] == max_stamps[stamp_group]) + { + max_stamps[stamp_group] = max_stamps[stamp_group] * 5 / 4 + 15; + stamp_data[stamp_group] = realloc(stamp_data[stamp_group], + max_stamps[stamp_group] * + sizeof(*stamp_data[stamp_group])); + } + stamp_data[stamp_group][num_stamps[stamp_group]] = + calloc(1, sizeof *stamp_data[stamp_group][num_stamps[stamp_group]]); + stamp_data[stamp_group][num_stamps[stamp_group]]->stampname = + malloc(dotext - files[i].str + 1 + dirlen + 1); + memcpy(stamp_data[stamp_group][num_stamps[stamp_group]]->stampname, + fname, dotext - files[i].str + 1 + dirlen); + stamp_data[stamp_group][num_stamps[stamp_group]]->stampname[dotext - + files[i].str + + 1 + + dirlen] = + '\0'; - if (strcmp(ext, ".svg") == 0) - { - stamp_data[stamp_group][num_stamps[stamp_group]]->is_svg = 1; - } - else - { - stamp_data[stamp_group][num_stamps[stamp_group]]->is_svg = 0; - } + if (strcmp(ext, ".svg") == 0) + { + stamp_data[stamp_group][num_stamps[stamp_group]]->is_svg = 1; + } + else + { + stamp_data[stamp_group][num_stamps[stamp_group]]->is_svg = 0; + } - num_stamps[stamp_group]++; - } - free(files[i].str); + num_stamps[stamp_group]++; } + free(files[i].str); + } free(files); } @@ -9072,9 +9588,11 @@ static void load_stamps(SDL_Surface * screen) #endif if (num_stamps[0] == 0) - { - fprintf(stderr, "\nWarning: No stamps found in " DATA_PREFIX "stamps/\n" "or %s\n\n", homedirdir); - } + { + fprintf(stderr, + "\nWarning: No stamps found in " DATA_PREFIX "stamps/\n" + "or %s\n\n", homedirdir); + } num_stamp_groups = stamp_group + 1; @@ -9102,23 +9620,24 @@ int generate_fontconfig_cache_spinner(SDL_Surface * screen) SDL_Event event; while (fontconfig_thread_done == 0) - { - show_progress_bar(screen); - SDL_Flip(screen); - SDL_Delay(20); + { + show_progress_bar(screen); + SDL_Flip(screen); + SDL_Delay(20); - while (SDL_PollEvent(&event) > 0) - { - if (event.type == SDL_QUIT || - (event.type == SDL_KEYDOWN - && (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_AC_BACK))) - { - fprintf(stderr, "Aborting!\n"); - fflush(stdout); - return (1); - } - } + while (SDL_PollEvent(&event) > 0) + { + if (event.type == SDL_QUIT || + (event.type == SDL_KEYDOWN + && (event.key.keysym.sym == SDLK_ESCAPE + || event.key.keysym.sym == SDLK_AC_BACK))) + { + fprintf(stderr, "Aborting!\n"); + fflush(stdout); + return (1); + } } + } return (0); } @@ -9132,43 +9651,44 @@ static int generate_fontconfig_cache_real(void) SDL_Color black = { 0, 0, 0, 0 }; #ifdef DEBUG - printf("-- Hello from generate_fontconfig_cache() (thread # %d)\n", SDL_ThreadID()); + printf("-- Hello from generate_fontconfig_cache() (thread # %d)\n", + SDL_ThreadID()); fflush(stdout); #endif tmp_font = TuxPaint_Font_OpenFont(PANGO_DEFAULT_FONT, NULL, 12); if (tmp_font != NULL) + { +#ifdef DEBUG + printf("-- Generated a font.\n"); + fflush(stdout); +#endif + tmp_surf = render_text(tmp_font, "Test", black); + if (tmp_surf != NULL) { #ifdef DEBUG - printf("-- Generated a font.\n"); + printf("-- Generated a surface\n"); fflush(stdout); #endif - tmp_surf = render_text(tmp_font, "Test", black); - if (tmp_surf != NULL) - { -#ifdef DEBUG - printf("-- Generated a surface\n"); - fflush(stdout); -#endif - SDL_FreeSurface(tmp_surf); - } - else - { -#ifdef DEBUG - printf("-- Failed to make a surface!\n"); - fflush(stdout); -#endif - } - TuxPaint_Font_CloseFont(tmp_font); + SDL_FreeSurface(tmp_surf); } + else + { +#ifdef DEBUG + printf("-- Failed to make a surface!\n"); + fflush(stdout); +#endif + } + TuxPaint_Font_CloseFont(tmp_font); + } else - { + { #ifdef DEBUG - printf("-- Failed to generate a font!\n"); - fflush(stdout); + printf("-- Failed to generate a font!\n"); + fflush(stdout); #endif - } + } fontconfig_thread_done = 1; @@ -9182,7 +9702,8 @@ static int generate_fontconfig_cache_real(void) /** * FIXME */ -static int generate_fontconfig_cache(__attribute__((unused)) void *vp) +static int generate_fontconfig_cache( __attribute__((unused)) + void *vp) { return generate_fontconfig_cache_real(); } @@ -9197,19 +9718,19 @@ static void signal_handler(int sig) { // It is not legal to call printf or most other functions here! if (sig == SIGUSR1 || sig == SIGUSR2) + { + autosave_on_quit = 1; + no_prompt_on_quit = 1; + if (sig == SIGUSR1) { - autosave_on_quit = 1; - no_prompt_on_quit = 1; - if (sig == SIGUSR1) - { - promptless_save = SAVE_OVER_NO; - } - else - { - promptless_save = SAVE_OVER_ALWAYS; - } - raise(SIGTERM); + promptless_save = SAVE_OVER_NO; } + else + { + promptless_save = SAVE_OVER_ALWAYS; + } + raise(SIGTERM); + } } #endif @@ -9239,7 +9760,10 @@ static SDL_Surface *do_render_button_label(const char *const label) tmp_surf = render_text(myfont, upstr, black); free(upstr); - surf = thumbnail(tmp_surf, min(button_w, tmp_surf->w), min((int) (18 * button_scale + button_label_y_nudge), tmp_surf->h), 0); + surf = + thumbnail(tmp_surf, min(button_w, tmp_surf->w), + min((int) (18 * button_scale + button_label_y_nudge), + tmp_surf->h), 0); SDL_FreeSurface(tmp_surf); return surf; @@ -9258,12 +9782,12 @@ static void create_button_labels(void) /* Magic Tools */ for (i = 0; i < MAX_MAGIC_GROUPS; i++) + { + for (j = 0; j < num_magics[i]; j++) { - for (j = 0; j < num_magics[i]; j++) - { - magics[i][j].img_name = do_render_button_label(magics[i][j].name); - } + magics[i][j].img_name = do_render_button_label(magics[i][j].name); } + } /* Shapes for Shape Tool */ for (i = 0; i < NUM_SHAPES; i++) @@ -9294,7 +9818,8 @@ static void create_button_labels(void) img_openlabels_play = do_render_button_label(gettext_noop("Play")); /* Slideshow: 'GIF Export' button, to create an animated GIF */ - img_openlabels_gif_export = do_render_button_label(gettext_noop("GIF Export")); + img_openlabels_gif_export = + do_render_button_label(gettext_noop("GIF Export")); /* Slideshow: 'Next' button, to load next slide (image) */ img_openlabels_next = do_render_button_label(gettext_noop("Next")); @@ -9322,13 +9847,13 @@ static void seticon(void) #endif if (icon == NULL) - { - fprintf(stderr, - "\nWarning: I could not load the icon image: %s\n" - "The Simple DirectMedia error that occurred was:\n" - "%s\n\n", DATA_PREFIX "images/icon.png", SDL_GetError()); - return; - } + { + fprintf(stderr, + "\nWarning: I could not load the icon image: %s\n" + "The Simple DirectMedia error that occurred was:\n" + "%s\n\n", DATA_PREFIX "images/icon.png", SDL_GetError()); + return; + } /* Create mask: */ @@ -9347,10 +9872,10 @@ static void seticon(void) /* Grab keyboard and mouse, if requested: */ if (grab_input) - { - debug("Grabbing input!"); - SDL_SetWindowGrab(window_screen, SDL_TRUE); - } + { + debug("Grabbing input!"); + SDL_SetWindowGrab(window_screen, SDL_TRUE); + } } @@ -9359,7 +9884,8 @@ static void seticon(void) */ /* Load a mouse pointer (cursor) shape: */ static SDL_Cursor *get_cursor(unsigned char *bits, unsigned char *mask_bits, - unsigned int width, unsigned int height, unsigned int x, unsigned int y) + unsigned int width, unsigned int height, + unsigned int x, unsigned int y) { Uint8 b; Uint8 temp_bitmap[128], temp_bitmask[128]; @@ -9367,30 +9893,36 @@ static SDL_Cursor *get_cursor(unsigned char *bits, unsigned char *mask_bits, if (((width + 7) / 8) * height > 128) - { - fprintf(stderr, "Error: Cursor is too large!\n"); - cleanup(); - exit(1); - } + { + fprintf(stderr, "Error: Cursor is too large!\n"); + cleanup(); + exit(1); + } for (i = 0; i < ((width + 7) / 8) * height; i++) - { - b = bits[i]; + { + b = bits[i]; - temp_bitmap[i] = (((b & 0x01) << 7) | - ((b & 0x02) << 5) | - ((b & 0x04) << 3) | - ((b & 0x08) << 1) | - ((b & 0x10) >> 1) | ((b & 0x20) >> 3) | ((b & 0x40) >> 5) | ((b & 0x80) >> 7)); + temp_bitmap[i] = (((b & 0x01) << 7) | + ((b & 0x02) << 5) | + ((b & 0x04) << 3) | + ((b & 0x08) << 1) | + ((b & 0x10) >> 1) | ((b & 0x20) >> 3) | ((b & 0x40) >> + 5) | ((b & + 0x80) >> + 7)); - b = mask_bits[i]; + b = mask_bits[i]; - temp_bitmask[i] = (((b & 0x01) << 7) | - ((b & 0x02) << 5) | - ((b & 0x04) << 3) | - ((b & 0x08) << 1) | - ((b & 0x10) >> 1) | ((b & 0x20) >> 3) | ((b & 0x40) >> 5) | ((b & 0x80) >> 7)); - } + temp_bitmask[i] = (((b & 0x01) << 7) | + ((b & 0x02) << 5) | + ((b & 0x04) << 3) | + ((b & 0x08) << 1) | + ((b & 0x10) >> 1) | ((b & 0x20) >> 3) | ((b & 0x40) >> + 5) | ((b & + 0x80) + >> 7)); + } return (SDL_CreateCursor(temp_bitmap, temp_bitmask, width, height, x, y)); } @@ -9402,25 +9934,25 @@ static SDL_Surface *loadimagerb(const char *const fname) { /* For the vaste majority of users return as soon as possible and touch the image as less as we can. */ if (button_h == ORIGINAL_BUTTON_SIZE) - return(loadimage(fname)); + return (loadimage(fname)); /* Going to resize the button */ - int w,h; + int w, h; SDL_Surface *aux_surf; SDL_Surface *aux2_surf; aux_surf = loadimage(fname); if (aux_surf) - { - w = (aux_surf->w * button_w) / ORIGINAL_BUTTON_SIZE; - h = (aux_surf->h * button_h) / ORIGINAL_BUTTON_SIZE; - aux2_surf = thumbnail(aux_surf, w, h, 0); - } + { + w = (aux_surf->w * button_w) / ORIGINAL_BUTTON_SIZE; + h = (aux_surf->h * button_h) / ORIGINAL_BUTTON_SIZE; + aux2_surf = thumbnail(aux_surf, w, h, 0); + } else return (NULL); SDL_FreeSurface(aux_surf); - return(aux2_surf); + return (aux2_surf); } /** @@ -9444,46 +9976,48 @@ static SDL_Surface *do_loadimage(const char *const fname, int abort_on_error) /* Load the image file: */ - s = myIMG_Load((char *)fname); + s = myIMG_Load((char *) fname); if (s == NULL) + { + if (abort_on_error) { - if (abort_on_error) - { - fprintf(stderr, - "\nError: I couldn't load a graphics file:\n" - "%s\n" "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", fname, SDL_GetError()); + fprintf(stderr, + "\nError: I couldn't load a graphics file:\n" + "%s\n" "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", fname, SDL_GetError()); - cleanup(); - exit(1); - } - else - { - return (NULL); - } + cleanup(); + exit(1); } + else + { + return (NULL); + } + } /* Convert to the display format: */ disp_fmt_s = SDL_DisplayFormatAlpha(s); if (disp_fmt_s == NULL) + { + if (abort_on_error) { - if (abort_on_error) - { - fprintf(stderr, - "\nError: I couldn't convert a graphics file:\n" - "%s\n" "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", fname, SDL_GetError()); + fprintf(stderr, + "\nError: I couldn't convert a graphics file:\n" + "%s\n" "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", fname, SDL_GetError()); - SDL_FreeSurface(s); - cleanup(); - exit(1); - } - else - { - SDL_FreeSurface(s); - return (NULL); - } + SDL_FreeSurface(s); + cleanup(); + exit(1); } + else + { + SDL_FreeSurface(s); + return (NULL); + } + } /* Free the temp. surface & return the converted one: */ @@ -9514,93 +10048,102 @@ static void draw_toolbar(void) /* Do we need scrollbars? */ if (NUM_TOOLS > most + TOOLOFFSET) + { + off_y = img_scroll_up->h; + max = most - gd_tools.cols + TOOLOFFSET; + gd_tools.rows = max / gd_tools.cols; + + dest.x = 0; + dest.y = r_ttools.h; + + if (tool_scroll > 0) { - off_y = img_scroll_up->h; - max = most - gd_tools.cols + TOOLOFFSET; - gd_tools.rows = max / gd_tools.cols; - - dest.x = 0; - dest.y = r_ttools.h; - - if (tool_scroll > 0) - { - SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); - } - - dest.x = 0; - dest.y = r_ttools.h + off_y + ((most - gd_tools.cols + TOOLOFFSET) / gd_tools.cols * button_h); - - - - if (tool_scroll < NUM_TOOLS - (most - gd_tools.cols) - TOOLOFFSET) - { - SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); - } + SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); } + else + { + SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); + } + + dest.x = 0; + dest.y = + r_ttools.h + off_y + + ((most - gd_tools.cols + TOOLOFFSET) / gd_tools.cols * button_h); + + + + if (tool_scroll < NUM_TOOLS - (most - gd_tools.cols) - TOOLOFFSET) + { + SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); + } + } else - { - off_y = 0; - max = most + TOOLOFFSET; - } + { + off_y = 0; + max = most + TOOLOFFSET; + } for (tool = tool_scroll; tool < tool_scroll + max; tool++) + { + i = tool - tool_scroll; + dest.x = ((i % 2) * button_w); + dest.y = ((i / 2) * button_h) + r_ttools.h + off_y; + + + if (tool < NUM_TOOLS) { - i = tool - tool_scroll; - dest.x = ((i % 2) * button_w); - dest.y = ((i / 2) * button_h) + r_ttools.h + off_y; + SDL_Surface *button_color; + SDL_Surface *button_body; - - if (tool < NUM_TOOLS) - { - SDL_Surface *button_color; - SDL_Surface *button_body; - - if (tool_scroll + i == cur_tool) - { - button_body = img_btn_down; - button_color = img_black; - } - else if (tool_avail[tool]) - { - button_body = img_btn_up; - button_color = img_black; - } - else - { - button_body = img_btn_off; - button_color = img_grey; - } - SDL_BlitSurface(button_body, NULL, screen, &dest); - SDL_BlitSurface(button_color, NULL, img_tools[tool], NULL); - SDL_BlitSurface(button_color, NULL, img_tool_names[tool], NULL); - - dest.x = ((i % 2) * button_w) + 4; - dest.y = ((i / 2) * button_h) + r_ttools.h + 2 + off_y; - - SDL_BlitSurface(img_tools[tool], NULL, screen, &dest); - - - dest.x = ((i % 2) * button_w) + (4 * button_w) / ORIGINAL_BUTTON_SIZE + ((40 * button_w) / ORIGINAL_BUTTON_SIZE - img_tool_names[tool]->w) / 2; - dest.y = ((i / 2) * button_h) + r_ttools.h + (2 * button_w) / ORIGINAL_BUTTON_SIZE + (((44 + button_label_y_nudge) * button_w) / ORIGINAL_BUTTON_SIZE - img_tool_names[tool]->h) + off_y; - - SDL_BlitSurface(img_tool_names[tool], NULL, screen, &dest); - } + if (tool_scroll + i == cur_tool) + { + button_body = img_btn_down; + button_color = img_black; + } + else if (tool_avail[tool]) + { + button_body = img_btn_up; + button_color = img_black; + } else - { - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - } + { + button_body = img_btn_off; + button_color = img_grey; + } + SDL_BlitSurface(button_body, NULL, screen, &dest); + SDL_BlitSurface(button_color, NULL, img_tools[tool], NULL); + SDL_BlitSurface(button_color, NULL, img_tool_names[tool], NULL); + + dest.x = ((i % 2) * button_w) + 4; + dest.y = ((i / 2) * button_h) + r_ttools.h + 2 + off_y; + + SDL_BlitSurface(img_tools[tool], NULL, screen, &dest); + + + dest.x = + ((i % 2) * button_w) + (4 * button_w) / ORIGINAL_BUTTON_SIZE + + ((40 * button_w) / ORIGINAL_BUTTON_SIZE - + img_tool_names[tool]->w) / 2; + dest.y = + ((i / 2) * button_h) + r_ttools.h + + (2 * button_w) / ORIGINAL_BUTTON_SIZE + + (((44 + button_label_y_nudge) * button_w) / ORIGINAL_BUTTON_SIZE - + img_tool_names[tool]->h) + off_y; + + SDL_BlitSurface(img_tool_names[tool], NULL, screen, &dest); } + else + { + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + } + } } @@ -9625,75 +10168,89 @@ static void draw_magic(void) most = most + gd_toolopt.cols; if (num_magics[magic_group] > most + TOOLOFFSET) + { + off_y = img_scroll_down->h; + max = (most - 2) + TOOLOFFSET; + + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = r_ttoolopt.h; + + if (magic_scroll[magic_group] > 0) { - off_y = img_scroll_down->h; - max = (most - 2) + TOOLOFFSET; - - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = r_ttoolopt.h; - - if (magic_scroll[magic_group] > 0) - { - SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); - } - - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = r_ttoolopt.h + img_scroll_down->h + ((((most - 2) / 2) + TOOLOFFSET / 2) * button_h); - - if (magic_scroll[magic_group] < num_magics[magic_group] - (most - 2) - TOOLOFFSET) - { - SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); - } + SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); } + else + { + SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); + } + + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = + r_ttoolopt.h + img_scroll_down->h + + ((((most - 2) / 2) + TOOLOFFSET / 2) * button_h); + + if (magic_scroll[magic_group] < + num_magics[magic_group] - (most - 2) - TOOLOFFSET) + { + SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); + } + } else + { + off_y = 0; + max = most + TOOLOFFSET; + } + + + for (magic = magic_scroll[magic_group]; + magic < magic_scroll[magic_group] + max; magic++) + { + i = magic - magic_scroll[magic_group]; + + dest.x = ((i % 2) * button_w) + (WINDOW_WIDTH - r_ttoolopt.w); + dest.y = ((i / 2) * button_h) + r_ttoolopt.h + off_y; + + if (magic < num_magics[magic_group]) { - off_y = 0; - max = most + TOOLOFFSET; - } - - - for (magic = magic_scroll[magic_group]; magic < magic_scroll[magic_group] + max; magic++) - { - i = magic - magic_scroll[magic_group]; - - dest.x = ((i % 2) * button_w) + (WINDOW_WIDTH - r_ttoolopt.w); - dest.y = ((i / 2) * button_h) + r_ttoolopt.h + off_y; - - if (magic < num_magics[magic_group]) - { - if (magic == cur_magic[magic_group]) - { - SDL_BlitSurface(img_btn_down, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); - } - - dest.x = WINDOW_WIDTH - r_ttoolopt.w + ((i % 2) * button_w) + 4; - dest.y = ((i / 2) * button_h) + r_ttoolopt.h + 4 + off_y; - - SDL_BlitSurface(magics[magic_group][magic].img_icon, NULL, screen, &dest); - - - dest.x = WINDOW_WIDTH - r_ttoolopt.w + ((i % 2) * button_w) + (4 * button_w) / ORIGINAL_BUTTON_SIZE + ((40 * button_w) / ORIGINAL_BUTTON_SIZE - magics[magic_group][magic].img_name->w) / 2; - dest.y = (((i / 2) * button_h) + r_ttoolopt.h + (4 * button_h) / ORIGINAL_BUTTON_SIZE + ((44 * button_h) / ORIGINAL_BUTTON_SIZE - magics[magic_group][magic].img_name->h) + off_y); - - SDL_BlitSurface(magics[magic_group][magic].img_name, NULL, screen, &dest); - } + if (magic == cur_magic[magic_group]) + { + SDL_BlitSurface(img_btn_down, NULL, screen, &dest); + } else - { - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - } + { + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + } + + dest.x = WINDOW_WIDTH - r_ttoolopt.w + ((i % 2) * button_w) + 4; + dest.y = ((i / 2) * button_h) + r_ttoolopt.h + 4 + off_y; + + SDL_BlitSurface(magics[magic_group][magic].img_icon, NULL, screen, + &dest); + + + dest.x = + WINDOW_WIDTH - r_ttoolopt.w + ((i % 2) * button_w) + + (4 * button_w) / ORIGINAL_BUTTON_SIZE + + ((40 * button_w) / ORIGINAL_BUTTON_SIZE - + magics[magic_group][magic].img_name->w) / 2; + dest.y = + (((i / 2) * button_h) + r_ttoolopt.h + + (4 * button_h) / ORIGINAL_BUTTON_SIZE + + ((44 * button_h) / ORIGINAL_BUTTON_SIZE - + magics[magic_group][magic].img_name->h) + off_y); + + SDL_BlitSurface(magics[magic_group][magic].img_name, NULL, screen, + &dest); } + else + { + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + } + } /* Draw group pagination buttons: */ @@ -9709,7 +10266,9 @@ static void draw_magic(void) SDL_BlitSurface(button_body, NULL, screen, &dest); dest.x = WINDOW_WIDTH - r_ttoolopt.w + (button_w - img_prev->w) / 2; - dest.y = (r_ttoolopt.h + (((most + TOOLOFFSET) / 2) * button_h) + (button_h - img_prev->h) / 2); + dest.y = + (r_ttoolopt.h + (((most + TOOLOFFSET) / 2) * button_h) + + (button_h - img_prev->h) / 2); SDL_BlitSurface(button_color, NULL, img_prev, NULL); SDL_BlitSurface(img_prev, NULL, screen, &dest); @@ -9720,12 +10279,15 @@ static void draw_magic(void) button_body = img_btn_nav; dest.x = WINDOW_WIDTH - button_w; - dest.y = r_ttoolopt.h + (((most + TOOLOFFSET) / gd_toolopt.cols) * button_h); + dest.y = + r_ttoolopt.h + (((most + TOOLOFFSET) / gd_toolopt.cols) * button_h); SDL_BlitSurface(button_body, NULL, screen, &dest); dest.x = WINDOW_WIDTH - button_w + (button_w - img_next->w) / 2; - dest.y = (r_ttoolopt.h + (((most + TOOLOFFSET) / gd_toolopt.cols) * button_h) + (button_h - img_next->h) / 2); + dest.y = + (r_ttoolopt.h + (((most + TOOLOFFSET) / gd_toolopt.cols) * button_h) + + (button_h - img_next->h) / 2); SDL_BlitSurface(button_color, NULL, img_next, NULL); SDL_BlitSurface(img_next, NULL, screen, &dest); @@ -9734,55 +10296,75 @@ static void draw_magic(void) /* Draw magic controls: */ if (!disable_magic_controls) - { - SDL_Surface *button_color; - int grp, cur; + { + SDL_Surface *button_color; + int grp, cur; - grp = magic_group; - cur = cur_magic[magic_group]; + grp = magic_group; + cur = cur_magic[magic_group]; - /* Show paint button: */ + /* Show paint button: */ - if (magics[grp][cur].mode == MODE_PAINT || magics[grp][cur].mode == MODE_ONECLICK - || magics[grp][cur].mode == MODE_PAINT_WITH_PREVIEW) - button_color = img_btn_down; /* Active */ - else if (magics[grp][cur].avail_modes & MODE_PAINT || magics[grp][cur].avail_modes & MODE_ONECLICK - || magics[grp][cur].avail_modes & MODE_PAINT_WITH_PREVIEW) - button_color = img_btn_up; /* Available, but not active */ - else - button_color = img_btn_off; /* Unavailable */ + if (magics[grp][cur].mode == MODE_PAINT + || magics[grp][cur].mode == MODE_ONECLICK + || magics[grp][cur].mode == MODE_PAINT_WITH_PREVIEW) + button_color = img_btn_down; /* Active */ + else if (magics[grp][cur].avail_modes & MODE_PAINT + || magics[grp][cur].avail_modes & MODE_ONECLICK + || magics[grp][cur].avail_modes & MODE_PAINT_WITH_PREVIEW) + button_color = img_btn_up; /* Available, but not active */ + else + button_color = img_btn_off; /* Unavailable */ - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = r_ttoolopt.h + ((most / gd_toolopt.cols + (TOOLOFFSET + 2) / gd_toolopt.cols) * button_h); + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = + r_ttoolopt.h + + ((most / gd_toolopt.cols + + (TOOLOFFSET + 2) / gd_toolopt.cols) * button_h); - SDL_BlitSurface(button_color, NULL, screen, &dest); + SDL_BlitSurface(button_color, NULL, screen, &dest); - dest.x = WINDOW_WIDTH - r_ttoolopt.w + (button_w - img_magic_paint->w) / 2; - dest.y = (r_ttoolopt.h + ((most / gd_toolopt.cols + (TOOLOFFSET + 2) / gd_toolopt.cols) * button_h) + (button_h - img_magic_paint->h) / 2); + dest.x = + WINDOW_WIDTH - r_ttoolopt.w + (button_w - img_magic_paint->w) / 2; + dest.y = + (r_ttoolopt.h + + ((most / gd_toolopt.cols + + (TOOLOFFSET + 2) / gd_toolopt.cols) * button_h) + (button_h - + img_magic_paint-> + h) / 2); - SDL_BlitSurface(img_magic_paint, NULL, screen, &dest); + SDL_BlitSurface(img_magic_paint, NULL, screen, &dest); - /* Show fullscreen button: */ + /* Show fullscreen button: */ - if (magics[grp][cur].mode == MODE_FULLSCREEN) - button_color = img_btn_down; /* Active */ - else if (magics[grp][cur].avail_modes & MODE_FULLSCREEN) - button_color = img_btn_up; /* Available, but not active */ - else - button_color = img_btn_off; /* Unavailable */ + if (magics[grp][cur].mode == MODE_FULLSCREEN) + button_color = img_btn_down; /* Active */ + else if (magics[grp][cur].avail_modes & MODE_FULLSCREEN) + button_color = img_btn_up; /* Available, but not active */ + else + button_color = img_btn_off; /* Unavailable */ - dest.x = WINDOW_WIDTH - button_w; - dest.y = r_ttoolopt.h + ((most / gd_toolopt.cols + (TOOLOFFSET + 2) / gd_toolopt.cols) * button_h); + dest.x = WINDOW_WIDTH - button_w; + dest.y = + r_ttoolopt.h + + ((most / gd_toolopt.cols + + (TOOLOFFSET + 2) / gd_toolopt.cols) * button_h); - SDL_BlitSurface(button_color, NULL, screen, &dest); + SDL_BlitSurface(button_color, NULL, screen, &dest); - dest.x = WINDOW_WIDTH - button_w + (button_w - img_magic_fullscreen->w) / 2; - dest.y = (r_ttoolopt.h + ((most / gd_toolopt.cols + (TOOLOFFSET + 2) / gd_toolopt.cols) * button_h) + (button_h - img_magic_fullscreen->h) / 2); + dest.x = + WINDOW_WIDTH - button_w + (button_w - img_magic_fullscreen->w) / 2; + dest.y = + (r_ttoolopt.h + + ((most / gd_toolopt.cols + + (TOOLOFFSET + 2) / gd_toolopt.cols) * button_h) + (button_h - + img_magic_fullscreen-> + h) / 2); - SDL_BlitSurface(img_magic_fullscreen, NULL, screen, &dest); - } + SDL_BlitSurface(img_magic_fullscreen, NULL, screen, &dest); + } } @@ -9821,30 +10403,35 @@ static unsigned draw_colors(unsigned action) old_color = cur_color; - for (i = 0; i < (unsigned int)NUM_COLORS; i++) - { - dest.x = r_colors.x + i % gd_colors.cols * color_button_w; - dest.y = r_colors.y + i / gd_colors.cols * color_button_h; + for (i = 0; i < (unsigned int) NUM_COLORS; i++) + { + dest.x = r_colors.x + i % gd_colors.cols * color_button_w; + dest.y = r_colors.y + i / gd_colors.cols * color_button_h; #ifndef LOW_QUALITY_COLOR_SELECTOR - SDL_BlitSurface((colors_state == COLORSEL_ENABLE) - ? img_color_btns[i + (i == cur_color) * NUM_COLORS] : img_color_btn_off, NULL, screen, &dest); + SDL_BlitSurface((colors_state == COLORSEL_ENABLE) + ? img_color_btns[i + + (i == + cur_color) * + NUM_COLORS] : img_color_btn_off, NULL, + screen, &dest); #else - dest.w = color_button_w; - dest.h = color_button_h; - if (colors_state == COLORSEL_ENABLE) - SDL_FillRect(screen, &dest, - SDL_MapRGB(screen->format, color_hexes[i][0], color_hexes[i][1], color_hexes[i][2])); - else - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 240, 240, 240)); + dest.w = color_button_w; + dest.h = color_button_h; + if (colors_state == COLORSEL_ENABLE) + SDL_FillRect(screen, &dest, + SDL_MapRGB(screen->format, color_hexes[i][0], + color_hexes[i][1], color_hexes[i][2])); + else + SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 240, 240, 240)); - if (i == cur_color && colors_state == COLORSEL_ENABLE) - { - dest.y += 4; - SDL_BlitSurface(img_paintcan, NULL, screen, &dest); - } + if (i == cur_color && colors_state == COLORSEL_ENABLE) + { + dest.y += 4; + SDL_BlitSurface(img_paintcan, NULL, screen, &dest); + } #endif - } + } update_screen_rect(&r_colors); /* if only the color changed, no need to draw the title */ @@ -9853,20 +10440,23 @@ static unsigned draw_colors(unsigned action) /* If more than one colors rows, fill the parts of the r_tcolors not covered by the title. */ if (gd_colors.rows > 1) - SDL_FillRect(screen, &r_tcolors, SDL_MapRGBA(screen->format, 255,255,255,255)); + SDL_FillRect(screen, &r_tcolors, + SDL_MapRGBA(screen->format, 255, 255, 255, 255)); if (colors_state == COLORSEL_ENABLE) - { - SDL_BlitSurface(img_title_large_on, NULL, screen, &r_tcolors); + { + SDL_BlitSurface(img_title_large_on, NULL, screen, &r_tcolors); - dest.x = r_tcolors.x + (r_tcolors.w - img_title_names[TITLE_COLORS]->w) / 2; - dest.y = r_tcolors.y + (r_tcolors.h - img_title_names[TITLE_COLORS]->h) / 2; - SDL_BlitSurface(img_title_names[TITLE_COLORS], NULL, screen, &dest); - } + dest.x = + r_tcolors.x + (r_tcolors.w - img_title_names[TITLE_COLORS]->w) / 2; + dest.y = + r_tcolors.y + (r_tcolors.h - img_title_names[TITLE_COLORS]->h) / 2; + SDL_BlitSurface(img_title_names[TITLE_COLORS], NULL, screen, &dest); + } else - { - SDL_BlitSurface(img_title_large_off, NULL, screen, &r_tcolors); - } + { + SDL_BlitSurface(img_title_large_off, NULL, screen, &r_tcolors); + } update_screen_rect(&r_tcolors); @@ -9890,109 +10480,116 @@ static void draw_brushes(void) /* Space for buttons, was 14 */ most = (buttons_tall * gd_toolopt.cols) - TOOLOFFSET; if (!disable_brushspacing) - { - /* Make room for spacing controls */ - most -= 2; - } + { + /* Make room for spacing controls */ + most -= 2; + } /* Do we need scrollbars? */ if (num_brushes > most + TOOLOFFSET) + { + most = most - gd_toolopt.cols; /* was 12 */ + off_y = img_scroll_up->h; + max = most + TOOLOFFSET; + + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = r_ttoolopt.h; + + if (brush_scroll > 0) { - most = most - gd_toolopt.cols; /* was 12 */ - off_y = img_scroll_up->h; - max = most + TOOLOFFSET; - - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = r_ttoolopt.h; - - if (brush_scroll > 0) - { - SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); - } - - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = r_ttoolopt.h + img_scroll_up->h + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); - - if (brush_scroll < num_brushes - most - TOOLOFFSET) - { - SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); - } + SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); } + else + { + SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); + } + + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = + r_ttoolopt.h + img_scroll_up->h + + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); + + if (brush_scroll < num_brushes - most - TOOLOFFSET) + { + SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); + } + } else - { - off_y = 0; - max = most + TOOLOFFSET; - } + { + off_y = 0; + max = most + TOOLOFFSET; + } /* Draw each of the shown brushes: */ for (brush = brush_scroll; brush < brush_scroll + max; brush++) + { + i = brush - brush_scroll; + + + dest.x = ((i % 2) * button_w) + (WINDOW_WIDTH - r_ttoolopt.w); + dest.y = ((i / 2) * button_h) + r_ttoolopt.h + off_y; + + if (brush == cur_brush) { - i = brush - brush_scroll; - - - dest.x = ((i % 2) * button_w) + (WINDOW_WIDTH - r_ttoolopt.w); - dest.y = ((i / 2) * button_h) + r_ttoolopt.h + off_y; - - if (brush == cur_brush) - { - SDL_BlitSurface(img_btn_down, NULL, screen, &dest); - } - else if (brush < num_brushes) - { - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - } - - if (brush < num_brushes) - { - int ui_btn_x, ui_btn_y; - - if (brushes_directional[brush]) - src.x = (img_brushes_thumbs[brush]->w / abs(brushes_frames[brush])) / 3; - else - src.x = 0; - - src.y = brushes_directional[brush] ? (img_brushes_thumbs[brush]->h / 3) : 0; - - src.w = (img_brushes_thumbs[brush]->w / abs(brushes_frames[brush])) / (brushes_directional[brush] ? 3 : 1); - src.h = (img_brushes_thumbs[brush]->h / (brushes_directional[brush] ? 3 : 1)); - - ui_btn_x = ((i % 2) * button_w) + (WINDOW_WIDTH - r_ttoolopt.w); - ui_btn_y = ((i / 2) * button_h) + r_ttoolopt.h + off_y; - - dest.x = ui_btn_x + ((button_w - src.w) >> 1); - dest.y = ui_btn_y + ((button_h - src.h) >> 1); - - SDL_BlitSurface(img_brushes_thumbs[brush], &src, screen, &dest); - - if (brushes_directional[brush] || brushes_rotate[brush]) - { - dest.x = ui_btn_x + button_w - img_brush_dir->w; - dest.y = ui_btn_y + button_h - img_brush_dir->h; - SDL_BlitSurface(img_brush_dir, NULL, screen, &dest); - } - if (brushes_frames[brush] != 1) - { - dest.x = ui_btn_x; - dest.y = ui_btn_y + button_h - img_brush_anim->h; - SDL_BlitSurface(img_brush_anim, NULL, screen, &dest); - } - } + SDL_BlitSurface(img_btn_down, NULL, screen, &dest); } + else if (brush < num_brushes) + { + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + } + + if (brush < num_brushes) + { + int ui_btn_x, ui_btn_y; + + if (brushes_directional[brush]) + src.x = + (img_brushes_thumbs[brush]->w / abs(brushes_frames[brush])) / 3; + else + src.x = 0; + + src.y = + brushes_directional[brush] ? (img_brushes_thumbs[brush]->h / 3) : 0; + + src.w = + (img_brushes_thumbs[brush]->w / abs(brushes_frames[brush])) / + (brushes_directional[brush] ? 3 : 1); + src.h = + (img_brushes_thumbs[brush]->h / (brushes_directional[brush] ? 3 : 1)); + + ui_btn_x = ((i % 2) * button_w) + (WINDOW_WIDTH - r_ttoolopt.w); + ui_btn_y = ((i / 2) * button_h) + r_ttoolopt.h + off_y; + + dest.x = ui_btn_x + ((button_w - src.w) >> 1); + dest.y = ui_btn_y + ((button_h - src.h) >> 1); + + SDL_BlitSurface(img_brushes_thumbs[brush], &src, screen, &dest); + + if (brushes_directional[brush] || brushes_rotate[brush]) + { + dest.x = ui_btn_x + button_w - img_brush_dir->w; + dest.y = ui_btn_y + button_h - img_brush_dir->h; + SDL_BlitSurface(img_brush_dir, NULL, screen, &dest); + } + if (brushes_frames[brush] != 1) + { + dest.x = ui_btn_x; + dest.y = ui_btn_y + button_h - img_brush_anim->h; + SDL_BlitSurface(img_brush_anim, NULL, screen, &dest); + } + } + } if (!disable_brushspacing) draw_brushes_spacing(); @@ -10012,37 +10609,39 @@ static void draw_brushes_spacing(void) /* Spacing ranges from 0px to "N x the max dimension of the brush" (so a 48x48 brush would have a spacing of 48 if the center option is chosen) */ - size_at = (BRUSH_SPACING_SIZES * brushes_spacing[cur_brush]) / (max(w, h) * BRUSH_SPACING_MAX_MULTIPLIER); + size_at = + (BRUSH_SPACING_SIZES * brushes_spacing[cur_brush]) / (max(w, h) * + BRUSH_SPACING_MAX_MULTIPLIER); - x_per = (float)r_ttoolopt.w / BRUSH_SPACING_SIZES; - y_per = (float)button_h / BRUSH_SPACING_SIZES; + x_per = (float) r_ttoolopt.w / BRUSH_SPACING_SIZES; + y_per = (float) button_h / BRUSH_SPACING_SIZES; for (i = 0; i < BRUSH_SPACING_SIZES; i++) - { - xx = ceil(x_per); - yy = ceil(y_per * i); + { + xx = ceil(x_per); + yy = ceil(y_per * i); - if (i <= size_at) - btn = thumbnail(img_btn_down, xx, yy, 0); - else - btn = thumbnail(img_btn_up, xx, yy, 0); + if (i <= size_at) + btn = thumbnail(img_btn_down, xx, yy, 0); + else + btn = thumbnail(img_btn_up, xx, yy, 0); - blnk = thumbnail(img_btn_off, xx, button_h - yy, 0); + blnk = thumbnail(img_btn_off, xx, button_h - yy, 0); - /* FIXME: Check for NULL! */ + /* FIXME: Check for NULL! */ - dest.x = (WINDOW_WIDTH - r_ttoolopt.w) + (i * x_per); - dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; - SDL_BlitSurface(blnk, NULL, screen, &dest); + dest.x = (WINDOW_WIDTH - r_ttoolopt.w) + (i * x_per); + dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; + SDL_BlitSurface(blnk, NULL, screen, &dest); - dest.x = (WINDOW_WIDTH - r_ttoolopt.w) + (i * x_per); - dest.y = (button_h * buttons_tall + r_ttools.h) - (y_per * i); - SDL_BlitSurface(btn, NULL, screen, &dest); + dest.x = (WINDOW_WIDTH - r_ttoolopt.w) + (i * x_per); + dest.y = (button_h * buttons_tall + r_ttools.h) - (y_per * i); + SDL_BlitSurface(btn, NULL, screen, &dest); - SDL_FreeSurface(btn); - SDL_FreeSurface(blnk); - } + SDL_FreeSurface(btn); + SDL_FreeSurface(blnk); + } } /** @@ -10065,17 +10664,17 @@ static void draw_fonts(void) /* How many can we show? */ if (cur_tool == TOOL_LABEL) - { - most = most - gd_toolopt.cols - gd_toolopt.cols - gd_toolopt.cols; - if (disable_stamp_controls) - most = most + gd_toolopt.cols + gd_toolopt.cols; - } + { + most = most - gd_toolopt.cols - gd_toolopt.cols - gd_toolopt.cols; + if (disable_stamp_controls) + most = most + gd_toolopt.cols + gd_toolopt.cols; + } else - { - most = most - gd_toolopt.cols - gd_toolopt.cols; - if (disable_stamp_controls) - most = most + gd_toolopt.cols + gd_toolopt.cols /* Ugly! */; - } + { + most = most - gd_toolopt.cols - gd_toolopt.cols; + if (disable_stamp_controls) + most = most + gd_toolopt.cols + gd_toolopt.cols /* Ugly! */ ; + } #ifdef DEBUG printf("there are %d font families\n", num_font_families); @@ -10085,39 +10684,43 @@ static void draw_fonts(void) /* Do we need scrollbars? */ if (num_font_families > most + TOOLOFFSET) + { + off_y = img_scroll_up->h; + max = most - gd_toolopt.cols + TOOLOFFSET; + + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = r_ttoolopt.h; + + if (font_scroll > 0) { - off_y = img_scroll_up->h; - max = most - gd_toolopt.cols + TOOLOFFSET; - - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = r_ttoolopt.h; - - if (font_scroll > 0) - { - SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); - } - - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = r_ttoolopt.h + off_y + (((most - gd_toolopt.cols) / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); - - if (font_scroll < num_font_families - (most - gd_toolopt.cols) - TOOLOFFSET) - { - SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); - } + SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); } + else + { + SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); + } + + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = + r_ttoolopt.h + off_y + + (((most - gd_toolopt.cols) / gd_toolopt.cols + + TOOLOFFSET / gd_toolopt.cols) * button_h); + + if (font_scroll < + num_font_families - (most - gd_toolopt.cols) - TOOLOFFSET) + { + SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); + } + } else - { - off_y = 0; - max = most + TOOLOFFSET; - } + { + off_y = 0; + max = most + TOOLOFFSET; + } /* Draw each of the shown fonts: */ if (!num_font_families % 2) @@ -10126,77 +10729,77 @@ static void draw_fonts(void) font_scroll = min(font_scroll, max(0, num_font_families + 1 - max)); /* FIXAM COMENTARI */ for (font = font_scroll; font < font_scroll + max; font++) - { - i = font - font_scroll; + { + i = font - font_scroll; + dest.x = ((i % 2) * button_w) + (WINDOW_WIDTH - r_ttoolopt.w); + dest.y = ((i / 2) * button_h) + r_ttoolopt.h + off_y; + + if (font == cur_font) + { + SDL_BlitSurface(img_btn_down, NULL, screen, &dest); + } + else if (font < num_font_families) + { + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + } + + if (font < num_font_families) + { + SDL_Surface *tmp_surf_1; + + /* Label for 'Letters' buttons (font selector, down the right when the Text tool is being used); used to show the difference between font faces */ + tmp_surf_1 = render_text(getfonthandle(font), gettext("Aa"), black); + + if (tmp_surf_1 == NULL) + { + fprintf(stderr, "render_text() returned NULL!\n"); + return; + } + + if (tmp_surf_1->w > button_w || tmp_surf_1->h > button_h) + { + tmp_surf = thumbnail(tmp_surf_1, button_w, button_h, 1); + SDL_FreeSurface(tmp_surf_1); + } + else + tmp_surf = tmp_surf_1; + + src.x = (tmp_surf->w - button_w) / 2; + src.y = (tmp_surf->h - button_h) / 2; + src.w = button_w; + src.h = button_h; + + if (src.x < 0) + src.x = 0; + if (src.y < 0) + src.y = 0; + dest.x = ((i % 2) * button_w) + (WINDOW_WIDTH - r_ttoolopt.w); + if (src.w > tmp_surf->w) + { + src.w = tmp_surf->w; + dest.x = dest.x + ((button_w - (tmp_surf->w)) / 2); + } + + dest.y = ((i / 2) * button_h) + r_ttoolopt.h + off_y; + if (src.h > tmp_surf->h) + { + src.h = tmp_surf->h; + dest.y = dest.y + ((button_h - (tmp_surf->h)) / 2); + } - if (font == cur_font) - { - SDL_BlitSurface(img_btn_down, NULL, screen, &dest); - } - else if (font < num_font_families) - { - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - } + SDL_BlitSurface(tmp_surf, &src, screen, &dest); - if (font < num_font_families) - { - SDL_Surface *tmp_surf_1; - - /* Label for 'Letters' buttons (font selector, down the right when the Text tool is being used); used to show the difference between font faces */ - tmp_surf_1 = render_text(getfonthandle(font), gettext("Aa"), black); - - if (tmp_surf_1 == NULL) - { - fprintf(stderr, "render_text() returned NULL!\n"); - return; - } - - if (tmp_surf_1->w > button_w || tmp_surf_1->h > button_h) - { - tmp_surf = thumbnail(tmp_surf_1, button_w, button_h, 1); - SDL_FreeSurface(tmp_surf_1); - } - else - tmp_surf = tmp_surf_1; - - src.x = (tmp_surf->w - button_w) / 2; - src.y = (tmp_surf->h - button_h) / 2; - src.w = button_w; - src.h = button_h; - - if (src.x < 0) - src.x = 0; - if (src.y < 0) - src.y = 0; - - dest.x = ((i % 2) * button_w) + (WINDOW_WIDTH - r_ttoolopt.w); - if (src.w > tmp_surf->w) - { - src.w = tmp_surf->w; - dest.x = dest.x + ((button_w - (tmp_surf->w)) / 2); - } - - - dest.y = ((i / 2) * button_h) + r_ttoolopt.h + off_y; - if (src.h > tmp_surf->h) - { - src.h = tmp_surf->h; - dest.y = dest.y + ((button_h - (tmp_surf->h)) / 2); - } - - SDL_BlitSurface(tmp_surf, &src, screen, &dest); - - SDL_FreeSurface(tmp_surf); - } + SDL_FreeSurface(tmp_surf); } + } /* Draw text controls: */ @@ -10204,139 +10807,170 @@ static void draw_fonts(void) /* Label controls, if in label tool (always!) */ if (cur_tool == TOOL_LABEL) + { + /* "Apply Label" button */ + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = + r_ttoolopt.h + + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); + if (cur_label == LABEL_APPLY) + SDL_BlitSurface(img_btn_down, NULL, screen, &dest); + else { - /* "Apply Label" button */ - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = r_ttoolopt.h + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); - if (cur_label == LABEL_APPLY) - SDL_BlitSurface(img_btn_down, NULL, screen, &dest); + if (are_labels()) + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); else - { - if (are_labels()) - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); - else - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - } - - dest.x = WINDOW_WIDTH - r_ttoolopt.w + (button_w - img_label_apply->w) / 2; - dest.y = (r_ttoolopt.h + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h) + (button_h - img_label_apply->h) / 2); - - SDL_BlitSurface(img_label_apply, NULL, screen, &dest); - - - /* "Select Label" button */ - dest.x = WINDOW_WIDTH - button_w; - dest.y = r_ttoolopt.h + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); - - if (cur_label == LABEL_SELECT) - SDL_BlitSurface(img_btn_down, NULL, screen, &dest); - else - { - if (are_labels()) - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); - else - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - } - - - dest.x = WINDOW_WIDTH - button_w + (button_w - img_label_select->w) / 2; - dest.y = (r_ttoolopt.h + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h) + (button_h - img_label_select->h) / 2); - - SDL_BlitSurface(img_label_select, NULL, screen, &dest); - - most = most + gd_toolopt.cols; + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); } + dest.x = + WINDOW_WIDTH - r_ttoolopt.w + (button_w - img_label_apply->w) / 2; + dest.y = + (r_ttoolopt.h + + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h) + + (button_h - img_label_apply->h) / 2); + + SDL_BlitSurface(img_label_apply, NULL, screen, &dest); + + + /* "Select Label" button */ + dest.x = WINDOW_WIDTH - button_w; + dest.y = + r_ttoolopt.h + + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); + + if (cur_label == LABEL_SELECT) + SDL_BlitSurface(img_btn_down, NULL, screen, &dest); + else + { + if (are_labels()) + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + else + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + } + + + dest.x = WINDOW_WIDTH - button_w + (button_w - img_label_select->w) / 2; + dest.y = + (r_ttoolopt.h + + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h) + + (button_h - img_label_select->h) / 2); + + SDL_BlitSurface(img_label_select, NULL, screen, &dest); + + most = most + gd_toolopt.cols; + } + /* Size, italic, and bold, only appear when not UI is not being simplified */ if (!disable_stamp_controls) + { + SDL_Surface *button_color; + SDL_Surface *button_body; + + // label_ctrl_y = r_ttoolopt.h + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); + + /* Show bold button: */ + + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = + r_ttoolopt.h + + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); + + if (text_state & TTF_STYLE_BOLD) + SDL_BlitSurface(img_btn_down, NULL, screen, &dest); + else + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + + dest.x = WINDOW_WIDTH - r_ttoolopt.w + (button_w - img_bold->w) / 2; + dest.y = + (r_ttoolopt.h + + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h) + + (button_h - img_bold->h) / 2); + + SDL_BlitSurface(img_bold, NULL, screen, &dest); + + + /* Show italic button: */ + + dest.x = WINDOW_WIDTH - button_w; + dest.y = + r_ttoolopt.h + + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); + + if (text_state & TTF_STYLE_ITALIC) + SDL_BlitSurface(img_btn_down, NULL, screen, &dest); + else + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + + dest.x = WINDOW_WIDTH - button_w + (button_w - img_italic->w) / 2; + dest.y = + (r_ttoolopt.h + + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h) + + (button_h - img_italic->h) / 2); + + SDL_BlitSurface(img_italic, NULL, screen, &dest); + + most = most + gd_toolopt.cols; + + + /* Show shrink button: */ + + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = + r_ttoolopt.h + + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); + + if (text_size > MIN_TEXT_SIZE) { - SDL_Surface *button_color; - SDL_Surface *button_body; - - // label_ctrl_y = r_ttoolopt.h + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); - - /* Show bold button: */ - - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = r_ttoolopt.h + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); - - if (text_state & TTF_STYLE_BOLD) - SDL_BlitSurface(img_btn_down, NULL, screen, &dest); - else - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); - - dest.x = WINDOW_WIDTH - r_ttoolopt.w + (button_w - img_bold->w) / 2; - dest.y = (r_ttoolopt.h + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h) + (button_h - img_bold->h) / 2); - - SDL_BlitSurface(img_bold, NULL, screen, &dest); - - - /* Show italic button: */ - - dest.x = WINDOW_WIDTH - button_w; - dest.y = r_ttoolopt.h + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); - - if (text_state & TTF_STYLE_ITALIC) - SDL_BlitSurface(img_btn_down, NULL, screen, &dest); - else - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); - - dest.x = WINDOW_WIDTH - button_w + (button_w - img_italic->w) / 2; - dest.y = (r_ttoolopt.h + ((most /gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h) + (button_h - img_italic->h) / 2); - - SDL_BlitSurface(img_italic, NULL, screen, &dest); - - most = most + gd_toolopt.cols; - - - /* Show shrink button: */ - - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = r_ttoolopt.h + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); - - if (text_size > MIN_TEXT_SIZE) - { - button_color = img_black; - button_body = img_btn_up; - } - else - { - button_color = img_grey; - button_body = img_btn_off; - } - SDL_BlitSurface(button_body, NULL, screen, &dest); - - dest.x = WINDOW_WIDTH - r_ttoolopt.w + (button_w - img_shrink->w) / 2; - dest.y = (r_ttoolopt.h + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h) + (button_h - img_shrink->h) / 2); - - SDL_BlitSurface(button_color, NULL, img_shrink, NULL); - SDL_BlitSurface(img_shrink, NULL, screen, &dest); - - - /* Show grow button: */ - - dest.x = WINDOW_WIDTH - button_w; - dest.y = r_ttoolopt.h + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); - - if (text_size < MAX_TEXT_SIZE) - { - button_color = img_black; - button_body = img_btn_up; - } - else - { - button_color = img_grey; - button_body = img_btn_off; - } - SDL_BlitSurface(button_body, NULL, screen, &dest); - - dest.x = WINDOW_WIDTH - button_w + (button_w - img_grow->w) / 2; - dest.y = (r_ttoolopt.h + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h) + (button_h - img_grow->h) / 2); - - SDL_BlitSurface(button_color, NULL, img_grow, NULL); - SDL_BlitSurface(img_grow, NULL, screen, &dest); + button_color = img_black; + button_body = img_btn_up; } + else + { + button_color = img_grey; + button_body = img_btn_off; + } + SDL_BlitSurface(button_body, NULL, screen, &dest); + + dest.x = WINDOW_WIDTH - r_ttoolopt.w + (button_w - img_shrink->w) / 2; + dest.y = + (r_ttoolopt.h + + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h) + + (button_h - img_shrink->h) / 2); + + SDL_BlitSurface(button_color, NULL, img_shrink, NULL); + SDL_BlitSurface(img_shrink, NULL, screen, &dest); + + + /* Show grow button: */ + + dest.x = WINDOW_WIDTH - button_w; + dest.y = + r_ttoolopt.h + + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); + + if (text_size < MAX_TEXT_SIZE) + { + button_color = img_black; + button_body = img_btn_up; + } + else + { + button_color = img_grey; + button_body = img_btn_off; + } + SDL_BlitSurface(button_body, NULL, screen, &dest); + + dest.x = WINDOW_WIDTH - button_w + (button_w - img_grow->w) / 2; + dest.y = + (r_ttoolopt.h + + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h) + + (button_h - img_grow->h) / 2); + + SDL_BlitSurface(button_color, NULL, img_grow, NULL); + SDL_BlitSurface(img_grow, NULL, screen, &dest); + } } @@ -10364,7 +10998,7 @@ static void draw_stamps(void) /* How many can we show? */ - most = (buttons_tall * gd_toolopt.cols) - gd_toolopt.cols - gd_toolopt.cols - gd_toolopt.cols - TOOLOFFSET; /* was 10 and 14, before left/right controls -bjk 2007.05.03 */ + most = (buttons_tall * gd_toolopt.cols) - gd_toolopt.cols - gd_toolopt.cols - gd_toolopt.cols - TOOLOFFSET; /* was 10 and 14, before left/right controls -bjk 2007.05.03 */ if (disable_stamp_controls) most = (buttons_tall * gd_toolopt.cols) - gd_toolopt.cols - TOOLOFFSET; @@ -10372,83 +11006,90 @@ static void draw_stamps(void) /* Do we need scrollbars? */ if (num_stamps[stamp_group] > most + TOOLOFFSET) + { + off_y = img_scroll_up->h; + max = (most - gd_toolopt.cols) + TOOLOFFSET; + + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = r_ttoolopt.h; + + if (stamp_scroll[stamp_group] > 0) { - off_y = img_scroll_up->h; - max = (most - gd_toolopt.cols) + TOOLOFFSET; - - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = r_ttoolopt.h; - - if (stamp_scroll[stamp_group] > 0) - { - SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); - } - - - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = r_ttoolopt.h + off_y + (((most + 2) / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); /* was 6, before left/right controls -bjk 2007.05.03 */ - - if (!disable_stamp_controls) - dest.y = dest.y - (button_h * 2); - - if (stamp_scroll[stamp_group] < num_stamps[stamp_group] - (most - 2) - TOOLOFFSET) - { - SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); - } + SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); } + else + { + SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); + } + + + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = r_ttoolopt.h + off_y + (((most + 2) / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); /* was 6, before left/right controls -bjk 2007.05.03 */ + + if (!disable_stamp_controls) + dest.y = dest.y - (button_h * 2); + + if (stamp_scroll[stamp_group] < + num_stamps[stamp_group] - (most - 2) - TOOLOFFSET) + { + SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); + } + } else - { - off_y = 0; - max = most + TOOLOFFSET; - } + { + off_y = 0; + max = most + TOOLOFFSET; + } /* Draw each of the shown stamps: */ - for (stamp = stamp_scroll[stamp_group]; stamp < stamp_scroll[stamp_group] + max; stamp++) + for (stamp = stamp_scroll[stamp_group]; + stamp < stamp_scroll[stamp_group] + max; stamp++) + { + i = stamp - stamp_scroll[stamp_group]; + + dest.x = ((i % 2) * button_w) + (WINDOW_WIDTH - r_ttoolopt.w); + dest.y = ((i / 2) * button_h) + r_ttoolopt.h + off_y; + + if (stamp == cur_stamp[stamp_group]) { - i = stamp - stamp_scroll[stamp_group]; - - dest.x = ((i % 2) * button_w) + (WINDOW_WIDTH - r_ttoolopt.w); - dest.y = ((i / 2) * button_h) + r_ttoolopt.h + off_y; - - if (stamp == cur_stamp[stamp_group]) - { - SDL_BlitSurface(img_btn_down, NULL, screen, &dest); - } - else if (stamp < num_stamps[stamp_group]) - { - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - } - - if (stamp < num_stamps[stamp_group]) - { - /* Loads the thumbnail and sounds, the sounds just if this is the current stamp, increasing responsivity for low powered devices */ - get_stamp_thumb(stamp_data[stamp_group][stamp], stamp == cur_stamp[stamp_group] ? 1 : 0); - img = stamp_data[stamp_group][stamp]->thumbnail; - - base_x = ((i % 2) * button_w) + (WINDOW_WIDTH - r_ttoolopt.w) + ((button_w - (img->w)) / 2); - - base_y = ((i / 2) * button_h) + r_ttoolopt.h + ((button_h - (img->h)) / 2) + off_y; - - dest.x = base_x; - dest.y = base_y; - - SDL_BlitSurface(img, NULL, screen, &dest); - } + SDL_BlitSurface(img_btn_down, NULL, screen, &dest); } + else if (stamp < num_stamps[stamp_group]) + { + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + } + + if (stamp < num_stamps[stamp_group]) + { + /* Loads the thumbnail and sounds, the sounds just if this is the current stamp, increasing responsivity for low powered devices */ + get_stamp_thumb(stamp_data[stamp_group][stamp], + stamp == cur_stamp[stamp_group] ? 1 : 0); + img = stamp_data[stamp_group][stamp]->thumbnail; + + base_x = + ((i % 2) * button_w) + (WINDOW_WIDTH - r_ttoolopt.w) + + ((button_w - (img->w)) / 2); + + base_y = + ((i / 2) * button_h) + r_ttoolopt.h + ((button_h - (img->h)) / 2) + + off_y; + + dest.x = base_x; + dest.y = base_y; + + SDL_BlitSurface(img, NULL, screen, &dest); + } + } /* Draw stamp group buttons (prev/next): */ @@ -10465,7 +11106,9 @@ static void draw_stamps(void) SDL_BlitSurface(button_body, NULL, screen, &dest); dest.x = WINDOW_WIDTH - r_ttoolopt.w + (button_w - img_prev->w) / 2; - dest.y = (r_ttoolopt.h + (((most + TOOLOFFSET) / 2) * button_h) + (button_h - img_prev->h) / 2); + dest.y = + (r_ttoolopt.h + (((most + TOOLOFFSET) / 2) * button_h) + + (button_h - img_prev->h) / 2); SDL_BlitSurface(button_color, NULL, img_prev, NULL); SDL_BlitSurface(img_prev, NULL, screen, &dest); @@ -10476,12 +11119,15 @@ static void draw_stamps(void) button_body = img_btn_nav; dest.x = WINDOW_WIDTH - button_w; - dest.y = r_ttoolopt.h + (((most + TOOLOFFSET) / gd_toolopt.cols) * button_h); + dest.y = + r_ttoolopt.h + (((most + TOOLOFFSET) / gd_toolopt.cols) * button_h); SDL_BlitSurface(button_body, NULL, screen, &dest); dest.x = WINDOW_WIDTH - button_w + (button_w - img_next->w) / 2; - dest.y = (r_ttoolopt.h + (((most + TOOLOFFSET) / gd_toolopt.cols) * button_h) + (button_h - img_next->h) / 2); + dest.y = + (r_ttoolopt.h + (((most + TOOLOFFSET) / gd_toolopt.cols) * button_h) + + (button_h - img_next->h) / 2); SDL_BlitSurface(button_color, NULL, img_next, NULL); SDL_BlitSurface(img_next, NULL, screen, &dest); @@ -10490,154 +11136,176 @@ static void draw_stamps(void) /* Draw stamp controls: */ if (!disable_stamp_controls) + { + /* Show mirror button: */ + + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = + r_ttoolopt.h + + ((most + gd_toolopt.cols + TOOLOFFSET) / gd_toolopt.cols * button_h); + + if (stamp_data[stamp_group][cur_stamp[stamp_group]]->mirrorable) { - /* Show mirror button: */ - - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = r_ttoolopt.h + ((most + gd_toolopt.cols+ TOOLOFFSET) / gd_toolopt.cols * button_h); - - if (stamp_data[stamp_group][cur_stamp[stamp_group]]->mirrorable) - { - if (stamp_data[stamp_group][cur_stamp[stamp_group]]->mirrored) - { - button_color = img_black; - button_body = img_btn_down; - } - else - { - button_color = img_black; - button_body = img_btn_up; - } - } + if (stamp_data[stamp_group][cur_stamp[stamp_group]]->mirrored) + { + button_color = img_black; + button_body = img_btn_down; + } else - { - button_color = img_grey; - button_body = img_btn_off; - } - SDL_BlitSurface(button_body, NULL, screen, &dest); + { + button_color = img_black; + button_body = img_btn_up; + } + } + else + { + button_color = img_grey; + button_body = img_btn_off; + } + SDL_BlitSurface(button_body, NULL, screen, &dest); - dest.x = WINDOW_WIDTH - r_ttoolopt.w + (button_w - img_mirror->w) / 2; - dest.y = (r_ttoolopt.h + ((most + gd_toolopt.cols + TOOLOFFSET) / gd_toolopt.cols * button_h) + (button_h - img_mirror->h) / 2); + dest.x = WINDOW_WIDTH - r_ttoolopt.w + (button_w - img_mirror->w) / 2; + dest.y = + (r_ttoolopt.h + + ((most + gd_toolopt.cols + TOOLOFFSET) / gd_toolopt.cols * button_h) + + (button_h - img_mirror->h) / 2); - SDL_BlitSurface(button_color, NULL, img_mirror, NULL); - SDL_BlitSurface(img_mirror, NULL, screen, &dest); + SDL_BlitSurface(button_color, NULL, img_mirror, NULL); + SDL_BlitSurface(img_mirror, NULL, screen, &dest); - /* Show flip button: */ + /* Show flip button: */ - dest.x = WINDOW_WIDTH - button_w; - dest.y = r_ttoolopt.h + ((most + gd_toolopt.cols + TOOLOFFSET) / gd_toolopt.cols * button_h); + dest.x = WINDOW_WIDTH - button_w; + dest.y = + r_ttoolopt.h + + ((most + gd_toolopt.cols + TOOLOFFSET) / gd_toolopt.cols * button_h); - if (stamp_data[stamp_group][cur_stamp[stamp_group]]->flipable) - { - if (stamp_data[stamp_group][cur_stamp[stamp_group]]->flipped) - { - button_color = img_black; - button_body = img_btn_down; - } - else - { - button_color = img_black; - button_body = img_btn_up; - } - } + if (stamp_data[stamp_group][cur_stamp[stamp_group]]->flipable) + { + if (stamp_data[stamp_group][cur_stamp[stamp_group]]->flipped) + { + button_color = img_black; + button_body = img_btn_down; + } else - { - button_color = img_grey; - button_body = img_btn_off; - } - SDL_BlitSurface(button_body, NULL, screen, &dest); + { + button_color = img_black; + button_body = img_btn_up; + } + } + else + { + button_color = img_grey; + button_body = img_btn_off; + } + SDL_BlitSurface(button_body, NULL, screen, &dest); - dest.x = WINDOW_WIDTH - button_w + (button_w - img_flip->w) / 2; - dest.y = (r_ttoolopt.h + ((most + gd_toolopt.cols + TOOLOFFSET) / gd_toolopt.cols * button_h) + (button_h - img_flip->h) / 2); + dest.x = WINDOW_WIDTH - button_w + (button_w - img_flip->w) / 2; + dest.y = + (r_ttoolopt.h + + ((most + gd_toolopt.cols + TOOLOFFSET) / gd_toolopt.cols * button_h) + + (button_h - img_flip->h) / 2); - SDL_BlitSurface(button_color, NULL, img_flip, NULL); - SDL_BlitSurface(img_flip, NULL, screen, &dest); + SDL_BlitSurface(button_color, NULL, img_flip, NULL); + SDL_BlitSurface(img_flip, NULL, screen, &dest); #ifdef OLD_STAMP_GROW_SHRINK - /* Show shrink button: */ + /* Show shrink button: */ - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = 40 + ((6 + TOOLOFFSET / 2) * button_h); + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = 40 + ((6 + TOOLOFFSET / 2) * button_h); - if (stamp_data[stamp_group][cur_stamp[stamp_group]]->size > MIN_STAMP_SIZE) - { - button_color = img_black; - button_body = img_btn_up; - } - else - { - button_color = img_grey; - button_body = img_btn_off; - } - SDL_BlitSurface(button_body, NULL, screen, &dest); + if (stamp_data[stamp_group][cur_stamp[stamp_group]]->size > + MIN_STAMP_SIZE) + { + button_color = img_black; + button_body = img_btn_up; + } + else + { + button_color = img_grey; + button_body = img_btn_off; + } + SDL_BlitSurface(button_body, NULL, screen, &dest); - dest.x = WINDOW_WIDTH - r_ttoolopt.w + (button_w - img_shrink->w) / 2; - dest.y = (40 + ((6 + TOOLOFFSET / 2) * button_h) + (button_h - img_shrink->h) / 2); + dest.x = WINDOW_WIDTH - r_ttoolopt.w + (button_w - img_shrink->w) / 2; + dest.y = + (40 + ((6 + TOOLOFFSET / 2) * button_h) + + (button_h - img_shrink->h) / 2); - SDL_BlitSurface(button_color, NULL, img_shrink, NULL); - SDL_BlitSurface(img_shrink, NULL, screen, &dest); + SDL_BlitSurface(button_color, NULL, img_shrink, NULL); + SDL_BlitSurface(img_shrink, NULL, screen, &dest); - /* Show grow button: */ + /* Show grow button: */ - dest.x = WINDOW_WIDTH - button_w; - dest.y = 40 + ((6 + TOOLOFFSET / 2) * button_h); + dest.x = WINDOW_WIDTH - button_w; + dest.y = 40 + ((6 + TOOLOFFSET / 2) * button_h); - if (stamp_data[stamp_group][cur_stamp[stamp_group]]->size < MAX_STAMP_SIZE) - { - button_color = img_black; - button_body = img_btn_up; - } - else - { - button_color = img_grey; - button_body = img_btn_off; - } - SDL_BlitSurface(button_body, NULL, screen, &dest); + if (stamp_data[stamp_group][cur_stamp[stamp_group]]->size < + MAX_STAMP_SIZE) + { + button_color = img_black; + button_body = img_btn_up; + } + else + { + button_color = img_grey; + button_body = img_btn_off; + } + SDL_BlitSurface(button_body, NULL, screen, &dest); - dest.x = WINDOW_WIDTH - button_w + (button_w - img_grow->w) / 2; - dest.y = (40 + ((6 + TOOLOFFSET / 2) * button_h) + (button_h - img_grow->h) / 2); + dest.x = WINDOW_WIDTH - button_w + (button_w - img_grow->w) / 2; + dest.y = + (40 + ((6 + TOOLOFFSET / 2) * button_h) + (button_h - img_grow->h) / 2); - SDL_BlitSurface(button_color, NULL, img_grow, NULL); - SDL_BlitSurface(img_grow, NULL, screen, &dest); + SDL_BlitSurface(button_color, NULL, img_grow, NULL); + SDL_BlitSurface(img_grow, NULL, screen, &dest); #else - sizes = MAX_STAMP_SIZE - MIN_STAMP_SIZE + 1; /* +1 for SF Bug #1668235 -bjk 2011.01.08 */ - size_at = (stamp_data[stamp_group][cur_stamp[stamp_group]]->size - MIN_STAMP_SIZE); + sizes = MAX_STAMP_SIZE - MIN_STAMP_SIZE + 1; /* +1 for SF Bug #1668235 -bjk 2011.01.08 */ + size_at = + (stamp_data[stamp_group][cur_stamp[stamp_group]]->size - + MIN_STAMP_SIZE); - x_per = (float)r_ttoolopt.w / sizes; - y_per = (float)button_h / sizes; + x_per = (float) r_ttoolopt.w / sizes; + y_per = (float) button_h / sizes; - for (i = 0; i < sizes; i++) - { - xx = ceil(x_per); - yy = ceil(y_per * i); + for (i = 0; i < sizes; i++) + { + xx = ceil(x_per); + yy = ceil(y_per * i); - if (i <= size_at) - btn = thumbnail(img_btn_down, xx, yy, 0); - else - btn = thumbnail(img_btn_up, xx, yy, 0); + if (i <= size_at) + btn = thumbnail(img_btn_down, xx, yy, 0); + else + btn = thumbnail(img_btn_up, xx, yy, 0); - blnk = thumbnail(img_btn_off, xx, button_h - yy, 0); + blnk = thumbnail(img_btn_off, xx, button_h - yy, 0); - /* FIXME: Check for NULL! */ + /* FIXME: Check for NULL! */ - dest.x = (WINDOW_WIDTH - r_ttoolopt.w) + (i * x_per); - dest.y = (((most + gd_toolopt.cols + gd_toolopt.cols + gd_toolopt.cols + TOOLOFFSET) / gd_toolopt.cols * button_h)) - 8 * button_scale; - SDL_BlitSurface(blnk, NULL, screen, &dest); + dest.x = (WINDOW_WIDTH - r_ttoolopt.w) + (i * x_per); + dest.y = + (((most + gd_toolopt.cols + gd_toolopt.cols + gd_toolopt.cols + + TOOLOFFSET) / gd_toolopt.cols * button_h)) - 8 * button_scale; + SDL_BlitSurface(blnk, NULL, screen, &dest); - dest.x = (WINDOW_WIDTH - r_ttoolopt.w) + (i * x_per); - dest.y = (((most + gd_toolopt.cols + gd_toolopt.cols + gd_toolopt.cols + gd_toolopt.cols + TOOLOFFSET) / gd_toolopt.cols * button_h)) - 8 * button_scale - (y_per * i); - SDL_BlitSurface(btn, NULL, screen, &dest); + dest.x = (WINDOW_WIDTH - r_ttoolopt.w) + (i * x_per); + dest.y = + (((most + gd_toolopt.cols + gd_toolopt.cols + gd_toolopt.cols + + gd_toolopt.cols + TOOLOFFSET) / gd_toolopt.cols * button_h)) - + 8 * button_scale - (y_per * i); + SDL_BlitSurface(btn, NULL, screen, &dest); - SDL_FreeSurface(btn); - SDL_FreeSurface(blnk); - } -#endif + SDL_FreeSurface(btn); + SDL_FreeSurface(blnk); } +#endif + } - redraw_tux_text(); + redraw_tux_text(); } @@ -10659,116 +11327,137 @@ static void draw_shapes(void) most = (buttons_tall * gd_toolopt.cols) - gd_toolopt.cols - TOOLOFFSET; if (NUM_SHAPES > most + TOOLOFFSET) + { + off_y = img_scroll_up->h; + max = (most - 2) + TOOLOFFSET; + + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = r_ttoolopt.h; + + if (shape_scroll > 0) { - off_y = img_scroll_up->h; - max = (most - 2) + TOOLOFFSET; - - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = r_ttoolopt.h; - - if (shape_scroll > 0) - { - SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); - } - - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = r_ttoolopt.h + img_scroll_up->h + ((((most - 2) / 2) + TOOLOFFSET / 2) * button_h); - - if (shape_scroll < NUM_SHAPES - (most - 2) - TOOLOFFSET) - { - SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); - } + SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); } + else + { + SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); + } + + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = + r_ttoolopt.h + img_scroll_up->h + + ((((most - 2) / 2) + TOOLOFFSET / 2) * button_h); + + if (shape_scroll < NUM_SHAPES - (most - 2) - TOOLOFFSET) + { + SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); + } + } else - { - off_y = 0; - max = most + TOOLOFFSET; - } + { + off_y = 0; + max = most + TOOLOFFSET; + } for (shape = shape_scroll; shape < shape_scroll + max; shape++) + { + i = shape - shape_scroll; + + dest.x = ((i % 2) * button_w) + WINDOW_WIDTH - r_ttoolopt.w; + dest.y = ((i / 2) * button_h) + r_ttoolopt.h + off_y; + + if (shape == cur_shape) { - i = shape - shape_scroll; - - dest.x = ((i % 2) * button_w) + WINDOW_WIDTH - r_ttoolopt.w; - dest.y = ((i / 2) * button_h) + r_ttoolopt.h + off_y; - - if (shape == cur_shape) - { - SDL_BlitSurface(img_btn_down, NULL, screen, &dest); - } - else if (shape < NUM_SHAPES) - { - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - } - - - if (shape < NUM_SHAPES) - { - dest.x = ((i % 2) * button_w) + (4 * button_w) / ORIGINAL_BUTTON_SIZE + WINDOW_WIDTH - r_ttoolopt.w; - dest.y = ((i / 2) * button_h) + r_ttoolopt.h + (4 * button_h) / ORIGINAL_BUTTON_SIZE + off_y; - - SDL_BlitSurface(img_shapes[shape], NULL, screen, &dest); - - dest.x = ((i % 2) * button_w) + (4 * button_w) / ORIGINAL_BUTTON_SIZE + WINDOW_WIDTH - r_ttoolopt.w + ((40 * button_w) / ORIGINAL_BUTTON_SIZE - img_shape_names[shape]->w) / 2; - dest.y = ((i / 2) * button_h) + r_ttoolopt.h + (4 * button_h) / ORIGINAL_BUTTON_SIZE + ((44 * button_h) / ORIGINAL_BUTTON_SIZE - img_shape_names[shape]->h) + off_y; - - SDL_BlitSurface(img_shape_names[shape], NULL, screen, &dest); - } + SDL_BlitSurface(img_btn_down, NULL, screen, &dest); } + else if (shape < NUM_SHAPES) + { + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + } + + + if (shape < NUM_SHAPES) + { + dest.x = + ((i % 2) * button_w) + (4 * button_w) / ORIGINAL_BUTTON_SIZE + + WINDOW_WIDTH - r_ttoolopt.w; + dest.y = + ((i / 2) * button_h) + r_ttoolopt.h + + (4 * button_h) / ORIGINAL_BUTTON_SIZE + off_y; + + SDL_BlitSurface(img_shapes[shape], NULL, screen, &dest); + + dest.x = + ((i % 2) * button_w) + (4 * button_w) / ORIGINAL_BUTTON_SIZE + + WINDOW_WIDTH - r_ttoolopt.w + + ((40 * button_w) / ORIGINAL_BUTTON_SIZE - + img_shape_names[shape]->w) / 2; + dest.y = + ((i / 2) * button_h) + r_ttoolopt.h + + (4 * button_h) / ORIGINAL_BUTTON_SIZE + + ((44 * button_h) / ORIGINAL_BUTTON_SIZE - img_shape_names[shape]->h) + + off_y; + + SDL_BlitSurface(img_shape_names[shape], NULL, screen, &dest); + } + } /* Draw shape tool controls: */ if (!disable_shape_controls) - { - SDL_Surface *button_color; + { + SDL_Surface *button_color; - /* Show shape-from-center button: */ + /* Show shape-from-center button: */ - if (shape_mode == SHAPEMODE_CENTER) - button_color = img_btn_down; - else - button_color = img_btn_up; + if (shape_mode == SHAPEMODE_CENTER) + button_color = img_btn_down; + else + button_color = img_btn_up; - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = r_ttoolopt.h + ((most + TOOLOFFSET) / gd_toolopt.cols * button_h); + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = + r_ttoolopt.h + ((most + TOOLOFFSET) / gd_toolopt.cols * button_h); - SDL_BlitSurface(button_color, NULL, screen, &dest); + SDL_BlitSurface(button_color, NULL, screen, &dest); - dest.x = WINDOW_WIDTH - r_ttoolopt.w + (button_w - img_shapes_center->w) / 2; - dest.y = (r_ttoolopt.h + ((most + TOOLOFFSET) / gd_toolopt.cols * button_h) + (button_h - img_shapes_center->h) / 2); + dest.x = + WINDOW_WIDTH - r_ttoolopt.w + (button_w - img_shapes_center->w) / 2; + dest.y = + (r_ttoolopt.h + ((most + TOOLOFFSET) / gd_toolopt.cols * button_h) + + (button_h - img_shapes_center->h) / 2); - SDL_BlitSurface(img_shapes_center, NULL, screen, &dest); + SDL_BlitSurface(img_shapes_center, NULL, screen, &dest); - /* Show shape-from-corner button: */ + /* Show shape-from-corner button: */ - if (shape_mode == SHAPEMODE_CORNER) - button_color = img_btn_down; - else - button_color = img_btn_up; + if (shape_mode == SHAPEMODE_CORNER) + button_color = img_btn_down; + else + button_color = img_btn_up; - dest.x = WINDOW_WIDTH - button_w; - dest.y = r_ttoolopt.h + ((most + TOOLOFFSET) / gd_toolopt.cols * button_h); + dest.x = WINDOW_WIDTH - button_w; + dest.y = + r_ttoolopt.h + ((most + TOOLOFFSET) / gd_toolopt.cols * button_h); - SDL_BlitSurface(button_color, NULL, screen, &dest); + SDL_BlitSurface(button_color, NULL, screen, &dest); - dest.x = WINDOW_WIDTH - button_w + (button_w - img_shapes_corner->w) / 2; - dest.y = (r_ttoolopt.h + ((most + TOOLOFFSET) / gd_toolopt.cols * button_h) + (button_h - img_shapes_corner->h) / 2); + dest.x = WINDOW_WIDTH - button_w + (button_w - img_shapes_corner->w) / 2; + dest.y = + (r_ttoolopt.h + ((most + TOOLOFFSET) / gd_toolopt.cols * button_h) + + (button_h - img_shapes_corner->h) / 2); - SDL_BlitSurface(img_shapes_corner, NULL, screen, &dest); - } + SDL_BlitSurface(img_shapes_corner, NULL, screen, &dest); + } } @@ -10780,7 +11469,7 @@ static void draw_erasers(void) { int i, j, x, y, sz; int xx, yy, n; - void (*putpixel) (SDL_Surface *, int, int, Uint32); + void (*putpixel)(SDL_Surface *, int, int, Uint32); SDL_Rect dest; int most, off_y; @@ -10794,130 +11483,151 @@ static void draw_erasers(void) /* Do we need scrollbars? */ if (NUM_FILLS > most + TOOLOFFSET) + { + most = most - gd_toolopt.cols; /* was 12 */ + off_y = img_scroll_up->h; + + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = r_ttoolopt.h; + + if (eraser_scroll > 0) { - most = most - gd_toolopt.cols; /* was 12 */ - off_y = img_scroll_up->h; - - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = r_ttoolopt.h; - - if (eraser_scroll > 0) - { - SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); - } - - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = r_ttoolopt.h + img_scroll_up->h + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); - - if (eraser_scroll < NUM_ERASERS - most - TOOLOFFSET) - { - SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); - } + SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); } + else + { + SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); + } + + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = + r_ttoolopt.h + img_scroll_up->h + + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); + + if (eraser_scroll < NUM_ERASERS - most - TOOLOFFSET) + { + SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); + } + } else - { - off_y = 0; - } + { + off_y = 0; + } for (j = 0; j < most + TOOLOFFSET; j++) + { + i = j; + dest.x = ((i % 2) * button_w) + WINDOW_WIDTH - r_ttoolopt.w; + dest.y = ((i / 2) * button_h) + r_ttoolopt.h + off_y; + + i = j + eraser_scroll; + + if (i == cur_eraser) { - i = j; - dest.x = ((i % 2) * button_w) + WINDOW_WIDTH - r_ttoolopt.w; - dest.y = ((i / 2) * button_h) + r_ttoolopt.h + off_y; - - i = j + eraser_scroll; - - if (i == cur_eraser) - { - SDL_BlitSurface(img_btn_down, NULL, screen, &dest); - } - else if (i < NUM_ERASERS) - { - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - } - - if (i < NUM_ERASERS) - { - if (i < NUM_ERASERS / 2) - { - /* Square */ - - sz = (2 + (((NUM_ERASERS / 2) - 1 - i) * (38 / ((NUM_ERASERS / 2) - 1)))) * button_scale; - - x = ((i % 2) * button_w) + WINDOW_WIDTH - r_ttoolopt.w + 24 * button_scale - sz / 2; - y = ((j / 2) * button_h) + r_ttoolopt.h + 24 * button_scale - sz / 2 + off_y; - - dest.x = x; - dest.y = y; - dest.w = sz; - dest.h = 2; - - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 0, 0, 0)); - - dest.x = x; - dest.y = y + sz - 2; - dest.w = sz; - dest.h = 2; - - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 0, 0, 0)); - - dest.x = x; - dest.y = y; - dest.w = 2; - dest.h = sz; - - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 0, 0, 0)); - - dest.x = x + sz - 2; - dest.y = y; - dest.w = 2; - dest.h = sz; - - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 0, 0, 0)); - } - else - { - /* Circle */ - - sz = (2 + (((NUM_ERASERS / 2) - 1 - (i - NUM_ERASERS / 2)) * (38 / ((NUM_ERASERS / 2) - 1)))) * button_scale; - - x = ((i % 2) * button_w) + WINDOW_WIDTH - r_ttoolopt.w + 24 * button_scale- sz / 2; - y = ((j / 2) * button_h) + 40 * button_scale + 24 * button_scale - sz / 2 + off_y; - - for (yy = 0; yy <= sz; yy++) - { - for (xx = 0; xx <= sz; xx++) - { - n = (xx * xx) + (yy * yy) - ((sz / 2) * (sz / 2)); - - if (n >= -sz && n <= sz) - { - putpixel(screen, (x + sz / 2) + xx, (y + sz / 2) + yy, SDL_MapRGB(screen->format, 0, 0, 0)); - - putpixel(screen, (x + sz / 2) - xx, (y + sz / 2) + yy, SDL_MapRGB(screen->format, 0, 0, 0)); - - putpixel(screen, (x + sz / 2) + xx, (y + sz / 2) - yy, SDL_MapRGB(screen->format, 0, 0, 0)); - - putpixel(screen, (x + sz / 2) - xx, (y + sz / 2) - yy, SDL_MapRGB(screen->format, 0, 0, 0)); - - } - } - } - } - } + SDL_BlitSurface(img_btn_down, NULL, screen, &dest); } + else if (i < NUM_ERASERS) + { + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + } + + if (i < NUM_ERASERS) + { + if (i < NUM_ERASERS / 2) + { + /* Square */ + + sz = + (2 + + (((NUM_ERASERS / 2) - 1 - + i) * (38 / ((NUM_ERASERS / 2) - 1)))) * button_scale; + + x = + ((i % 2) * button_w) + WINDOW_WIDTH - r_ttoolopt.w + + 24 * button_scale - sz / 2; + y = + ((j / 2) * button_h) + r_ttoolopt.h + 24 * button_scale - sz / 2 + + off_y; + + dest.x = x; + dest.y = y; + dest.w = sz; + dest.h = 2; + + SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 0, 0, 0)); + + dest.x = x; + dest.y = y + sz - 2; + dest.w = sz; + dest.h = 2; + + SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 0, 0, 0)); + + dest.x = x; + dest.y = y; + dest.w = 2; + dest.h = sz; + + SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 0, 0, 0)); + + dest.x = x + sz - 2; + dest.y = y; + dest.w = 2; + dest.h = sz; + + SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 0, 0, 0)); + } + else + { + /* Circle */ + + sz = + (2 + + (((NUM_ERASERS / 2) - 1 - + (i - NUM_ERASERS / 2)) * (38 / ((NUM_ERASERS / 2) - + 1)))) * button_scale; + + x = + ((i % 2) * button_w) + WINDOW_WIDTH - r_ttoolopt.w + + 24 * button_scale - sz / 2; + y = + ((j / 2) * button_h) + 40 * button_scale + 24 * button_scale - + sz / 2 + off_y; + + for (yy = 0; yy <= sz; yy++) + { + for (xx = 0; xx <= sz; xx++) + { + n = (xx * xx) + (yy * yy) - ((sz / 2) * (sz / 2)); + + if (n >= -sz && n <= sz) + { + putpixel(screen, (x + sz / 2) + xx, (y + sz / 2) + yy, + SDL_MapRGB(screen->format, 0, 0, 0)); + + putpixel(screen, (x + sz / 2) - xx, (y + sz / 2) + yy, + SDL_MapRGB(screen->format, 0, 0, 0)); + + putpixel(screen, (x + sz / 2) + xx, (y + sz / 2) - yy, + SDL_MapRGB(screen->format, 0, 0, 0)); + + putpixel(screen, (x + sz / 2) - xx, (y + sz / 2) - yy, + SDL_MapRGB(screen->format, 0, 0, 0)); + + } + } + } + } + } + } } @@ -10935,12 +11645,12 @@ static void draw_none(void) SDL_BlitSurface(img_title_off, NULL, screen, &dest); for (i = 0; i < buttons_tall * gd_toolopt.cols; i++) - { - dest.x = ((i % 2) * button_w) + WINDOW_WIDTH - r_ttoolopt.w; - dest.y = ((i / 2) * button_h) + r_ttoolopt.h; + { + dest.x = ((i % 2) * button_w) + WINDOW_WIDTH - r_ttoolopt.w; + dest.y = ((i / 2) * button_h) + r_ttoolopt.h; - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - } + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + } } /* Draw Fill sub-tools */ @@ -10959,73 +11669,86 @@ static void draw_fills(void) /* Do we need scrollbars? */ if (NUM_FILLS > most + TOOLOFFSET) + { + most = most - gd_toolopt.cols; /* was 12 */ + off_y = img_scroll_up->h; + + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = r_ttoolopt.h; + + if (fill_scroll > 0) { - most = most - gd_toolopt.cols; /* was 12 */ - off_y = img_scroll_up->h; - - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = r_ttoolopt.h; - - if (fill_scroll > 0) - { - SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); - } - - dest.x = WINDOW_WIDTH - r_ttoolopt.w; - dest.y = r_ttoolopt.h + img_scroll_up->h + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); - - if (fill_scroll < NUM_FILLS - most - TOOLOFFSET) - { - SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); - } + SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); } + else + { + SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); + } + + dest.x = WINDOW_WIDTH - r_ttoolopt.w; + dest.y = + r_ttoolopt.h + img_scroll_up->h + + ((most / gd_toolopt.cols + TOOLOFFSET / gd_toolopt.cols) * button_h); + + if (fill_scroll < NUM_FILLS - most - TOOLOFFSET) + { + SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); + } + } else - { - off_y = 0; - } + { + off_y = 0; + } for (j = 0; j < most + TOOLOFFSET; j++) + { + i = j; + dest.x = ((i % 2) * button_w) + WINDOW_WIDTH - r_ttoolopt.w; + dest.y = ((i / 2) * button_h) + r_ttoolopt.h + off_y; + + i = j + fill_scroll; + + if (i == cur_fill) { - i = j; - dest.x = ((i % 2) * button_w) + WINDOW_WIDTH - r_ttoolopt.w; - dest.y = ((i / 2) * button_h) + r_ttoolopt.h + off_y; - - i = j + fill_scroll; - - if (i == cur_fill) - { - SDL_BlitSurface(img_btn_down, NULL, screen, &dest); - } - else if (i < NUM_FILLS) - { - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - } - - if (i < NUM_FILLS) - { - dest.x = ((i % 2) * button_w) + (4 * button_w) / ORIGINAL_BUTTON_SIZE + WINDOW_WIDTH - r_ttoolopt.w; - dest.y = ((i / 2) * button_h) + r_ttoolopt.h + (4 * button_h) / ORIGINAL_BUTTON_SIZE + off_y; - - SDL_BlitSurface(img_fills[i], NULL, screen, &dest); - - dest.x = ((i % 2) * button_w) + (4 * button_w) / ORIGINAL_BUTTON_SIZE + WINDOW_WIDTH - r_ttoolopt.w + ((40 * button_w) / ORIGINAL_BUTTON_SIZE - img_fill_names[i]->w) / 2; - dest.y = ((i / 2) * button_h) + r_ttoolopt.h + (4 * button_h) / ORIGINAL_BUTTON_SIZE + ((44 * button_h) / ORIGINAL_BUTTON_SIZE - img_fill_names[i]->h) + off_y; - - SDL_BlitSurface(img_fill_names[i], NULL, screen, &dest); - } + SDL_BlitSurface(img_btn_down, NULL, screen, &dest); } + else if (i < NUM_FILLS) + { + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + } + + if (i < NUM_FILLS) + { + dest.x = + ((i % 2) * button_w) + (4 * button_w) / ORIGINAL_BUTTON_SIZE + + WINDOW_WIDTH - r_ttoolopt.w; + dest.y = + ((i / 2) * button_h) + r_ttoolopt.h + + (4 * button_h) / ORIGINAL_BUTTON_SIZE + off_y; + + SDL_BlitSurface(img_fills[i], NULL, screen, &dest); + + dest.x = + ((i % 2) * button_w) + (4 * button_w) / ORIGINAL_BUTTON_SIZE + + WINDOW_WIDTH - r_ttoolopt.w + + ((40 * button_w) / ORIGINAL_BUTTON_SIZE - img_fill_names[i]->w) / 2; + dest.y = + ((i / 2) * button_h) + r_ttoolopt.h + + (4 * button_h) / ORIGINAL_BUTTON_SIZE + + ((44 * button_h) / ORIGINAL_BUTTON_SIZE - img_fill_names[i]->h) + + off_y; + + SDL_BlitSurface(img_fill_names[i], NULL, screen, &dest); + } + } } @@ -11033,7 +11756,8 @@ static void draw_fills(void) * FIXME */ /* Create a thumbnail: */ -static SDL_Surface *thumbnail(SDL_Surface * src, int max_x, int max_y, int keep_aspect) +static SDL_Surface *thumbnail(SDL_Surface * src, int max_x, int max_y, + int keep_aspect) { return (thumbnail2(src, max_x, max_y, keep_aspect, 1)); } @@ -11041,7 +11765,8 @@ static SDL_Surface *thumbnail(SDL_Surface * src, int max_x, int max_y, int keep_ /** * FIXME */ -static SDL_Surface *thumbnail2(SDL_Surface * src, int max_x, int max_y, int keep_aspect, int keep_alpha) +static SDL_Surface *thumbnail2(SDL_Surface * src, int max_x, int max_y, + int keep_aspect, int keep_alpha) { int x, y; float src_x, src_y, off_x, off_y; @@ -11055,38 +11780,39 @@ static SDL_Surface *thumbnail2(SDL_Surface * src, int max_x, int max_y, int keep Uint8 r, g, b, a; float xscale, yscale; int tmp; - void (*putpixel) (SDL_Surface *, int, int, Uint32); + void (*putpixel)(SDL_Surface *, int, int, Uint32); - Uint32(*getpixel) (SDL_Surface *, int, int) = getpixels[src->format->BytesPerPixel]; + Uint32(*getpixel) (SDL_Surface *, int, int) = + getpixels[src->format->BytesPerPixel]; /* Determine scale and centering offsets: */ if (!keep_aspect) - { - yscale = (float)((float)src->h / (float)max_y); - xscale = (float)((float)src->w / (float)max_x); + { + yscale = (float) ((float) src->h / (float) max_y); + xscale = (float) ((float) src->w / (float) max_x); - off_x = 0; + off_x = 0; + off_y = 0; + } + else + { + if (src->h > src->w) + { + yscale = (float) ((float) src->h / (float) max_y); + xscale = yscale; + + off_x = ((src->h - src->w) / xscale) / 2; off_y = 0; } - else + else { - if (src->h > src->w) - { - yscale = (float)((float)src->h / (float)max_y); - xscale = yscale; + xscale = (float) ((float) src->w / (float) max_x); + yscale = xscale; - off_x = ((src->h - src->w) / xscale) / 2; - off_y = 0; - } - else - { - xscale = (float)((float)src->w / (float)max_x); - yscale = xscale; - - off_x = 0; - off_y = ((src->w - src->h) / xscale) / 2; - } + off_x = 0; + off_y = ((src->w - src->h) / xscale) / 2; } + } #ifndef NO_BILINEAR @@ -11098,18 +11824,20 @@ static SDL_Surface *thumbnail2(SDL_Surface * src, int max_x, int max_y, int keep /* Create surface for thumbnail: */ s = SDL_CreateRGBSurface(src->flags, /* SDL_SWSURFACE, */ - max_x, max_y, src->format->BitsPerPixel, src->format->Rmask, src->format->Gmask, + max_x, max_y, src->format->BitsPerPixel, + src->format->Rmask, src->format->Gmask, src->format->Bmask, src->format->Amask); if (s == NULL) - { - fprintf(stderr, "\nError: Can't build stamp thumbnails\n" - "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", SDL_GetError()); + { + fprintf(stderr, "\nError: Can't build stamp thumbnails\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", SDL_GetError()); - cleanup(); - exit(1); - } + cleanup(); + exit(1); + } putpixel = putpixels[s->format->BytesPerPixel]; @@ -11119,75 +11847,83 @@ static SDL_Surface *thumbnail2(SDL_Surface * src, int max_x, int max_y, int keep SDL_LockSurface(s); for (y = 0; y < max_y; y++) + { + for (x = 0; x < max_x; x++) { - for (x = 0; x < max_x; x++) - { #ifndef LOW_QUALITY_THUMBNAILS - tr = 0; - tg = 0; - tb = 0; - ta = 0; + tr = 0; + tg = 0; + tb = 0; + ta = 0; - tmp = 0; + tmp = 0; - for (src_y = y * yscale; src_y < y * yscale + yscale && src_y < src->h; src_y++) - { - for (src_x = x * xscale; src_x < x * xscale + xscale && src_x < src->w; src_x++) - { - SDL_GetRGBA(getpixel(src, src_x, src_y), src->format, &r, &g, &b, &a); + for (src_y = y * yscale; src_y < y * yscale + yscale && src_y < src->h; + src_y++) + { + for (src_x = x * xscale; + src_x < x * xscale + xscale && src_x < src->w; src_x++) + { + SDL_GetRGBA(getpixel(src, src_x, src_y), src->format, &r, &g, &b, + &a); #ifdef GAMMA_CORRECTED_THUMBNAILS - /* per: http://www.4p8.com/eric.brasseur/gamma.html */ + /* per: http://www.4p8.com/eric.brasseur/gamma.html */ - tr = tr + sRGB_to_linear_table[r]; - tg = tg + sRGB_to_linear_table[g]; - tb = tb + sRGB_to_linear_table[b]; + tr = tr + sRGB_to_linear_table[r]; + tg = tg + sRGB_to_linear_table[g]; + tb = tb + sRGB_to_linear_table[b]; #else - tr = tr + r; - tb = tb + b; - tg = tg + g; + tr = tr + r; + tb = tb + b; + tg = tg + g; #endif - ta = ta + a; + ta = ta + a; - tmp++; - } - } - - if (tmp != 0) - { - tr = tr / tmp; - tb = tb / tmp; - tg = tg / tmp; - ta = ta / tmp; - -#ifdef GAMMA_CORRECTED_THUMBNAILS - tr = linear_to_sRGB(tr); - tg = linear_to_sRGB(tg); - tb = linear_to_sRGB(tb); -#endif - - if (keep_alpha == 0 && s->format->Amask != 0) - { - tr = ((ta * tr) / 255) + (255 - ta); - tg = ((ta * tg) / 255) + (255 - ta); - tb = ((ta * tb) / 255) + (255 - ta); - - putpixel(s, x + off_x, y + off_y, SDL_MapRGBA(s->format, (Uint8) tr, (Uint8) tg, (Uint8) tb, 0xff)); - } - else - { - putpixel(s, x + off_x, y + off_y, SDL_MapRGBA(s->format, - (Uint8) tr, (Uint8) tg, (Uint8) tb, (Uint8) ta)); - } - } -#else - src_x = x * xscale; - src_y = y * yscale; - - putpixel(s, x + off_x, y + off_y, getpixel(src, src_x, src_y)); -#endif + tmp++; } + } + + if (tmp != 0) + { + tr = tr / tmp; + tb = tb / tmp; + tg = tg / tmp; + ta = ta / tmp; + +#ifdef GAMMA_CORRECTED_THUMBNAILS + tr = linear_to_sRGB(tr); + tg = linear_to_sRGB(tg); + tb = linear_to_sRGB(tb); +#endif + + if (keep_alpha == 0 && s->format->Amask != 0) + { + tr = ((ta * tr) / 255) + (255 - ta); + tg = ((ta * tg) / 255) + (255 - ta); + tb = ((ta * tb) / 255) + (255 - ta); + + putpixel(s, x + off_x, y + off_y, + SDL_MapRGBA(s->format, (Uint8) tr, (Uint8) tg, (Uint8) tb, + 0xff)); + } + else + { + putpixel(s, x + off_x, y + off_y, SDL_MapRGBA(s->format, + (Uint8) tr, + (Uint8) tg, + (Uint8) tb, + (Uint8) ta)); + } + } +#else + src_x = x * xscale; + src_y = y * yscale; + + putpixel(s, x + off_x, y + off_y, getpixel(src, src_x, src_y)); +#endif } + } SDL_UnlockSurface(s); SDL_UnlockSurface(src); @@ -11206,12 +11942,14 @@ static SDL_Surface *thumbnail2(SDL_Surface * src, int max_x, int max_y, int keep static SDL_Surface *zoom(SDL_Surface * src, int new_w, int new_h) { SDL_Surface *s; - void (*putpixel) (SDL_Surface *, int, int, Uint32); + void (*putpixel)(SDL_Surface *, int, int, Uint32); - Uint32(*getpixel) (SDL_Surface *, int, int) = getpixels[src->format->BytesPerPixel]; + Uint32(*getpixel) (SDL_Surface *, int, int) = + getpixels[src->format->BytesPerPixel]; float xscale, yscale; int x, y; - float floor_x, ceil_x, floor_y, ceil_y, fraction_x, fraction_y, one_minus_x, one_minus_y; + float floor_x, ceil_x, floor_y, ceil_y, fraction_x, fraction_y, one_minus_x, + one_minus_y; float n1, n2; float r1, g1, b1, a1; float r2, g2, b2, a2; @@ -11224,17 +11962,19 @@ static SDL_Surface *zoom(SDL_Surface * src, int new_w, int new_h) s = SDL_CreateRGBSurface(src->flags, /* SDL_SWSURFACE, */ new_w, new_h, src->format->BitsPerPixel, - src->format->Rmask, src->format->Gmask, src->format->Bmask, src->format->Amask); + src->format->Rmask, src->format->Gmask, + src->format->Bmask, src->format->Amask); if (s == NULL) - { - fprintf(stderr, "\nError: Can't build zoom surface\n" - "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", SDL_GetError()); + { + fprintf(stderr, "\nError: Can't build zoom surface\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", SDL_GetError()); - cleanup(); - exit(1); - } + cleanup(); + exit(1); + } putpixel = putpixels[s->format->BytesPerPixel]; @@ -11242,115 +11982,123 @@ static SDL_Surface *zoom(SDL_Surface * src, int new_w, int new_h) SDL_LockSurface(src); SDL_LockSurface(s); - xscale = (float)src->w / (float)new_w; - yscale = (float)src->h / (float)new_h; + xscale = (float) src->w / (float) new_w; + yscale = (float) src->h / (float) new_h; for (x = 0; x < new_w; x++) + { + for (y = 0; y < new_h; y++) { - for (y = 0; y < new_h; y++) - { - floor_x = floor((float)x * xscale); - ceil_x = floor_x + 1; - if (ceil_x >= src->w) - ceil_x = floor_x; + floor_x = floor((float) x * xscale); + ceil_x = floor_x + 1; + if (ceil_x >= src->w) + ceil_x = floor_x; - floor_y = floor((float)y * yscale); - ceil_y = floor_y + 1; - if (ceil_y >= src->h) - ceil_y = floor_y; + floor_y = floor((float) y * yscale); + ceil_y = floor_y + 1; + if (ceil_y >= src->h) + ceil_y = floor_y; - fraction_x = x * xscale - floor_x; - fraction_y = y * yscale - floor_y; + fraction_x = x * xscale - floor_x; + fraction_y = y * yscale - floor_y; - one_minus_x = 1.0 - fraction_x; - one_minus_y = 1.0 - fraction_y; + one_minus_x = 1.0 - fraction_x; + one_minus_y = 1.0 - fraction_y; #if VIDEO_BPP==32 - { //EP added local block to avoid warning "Passing arg 3 from incompatible pointer type" of section below block - Uint8 r, g, b, a; + { //EP added local block to avoid warning "Passing arg 3 from incompatible pointer type" of section below block + Uint8 r, g, b, a; - SDL_GetRGBA(getpixel(src, floor_x, floor_y), src->format, &r, &g, &b, &a); - r1 = (float)r; - g1 = (float)g; - b1 = (float)b; - a1 = (float)a; - SDL_GetRGBA(getpixel(src, ceil_x, floor_y), src->format, &r, &g, &b, &a); - r2 = (float)r; - g2 = (float)g; - b2 = (float)b; - a2 = (float)a; - SDL_GetRGBA(getpixel(src, floor_x, ceil_y), src->format, &r, &g, &b, &a); - r3 = (float)r; - g3 = (float)g; - b3 = (float)b; - a3 = (float)a; - SDL_GetRGBA(getpixel(src, ceil_x, ceil_y), src->format, &r, &g, &b, &a); - r4 = (float)r; - g4 = (float)g; - b4 = (float)b; - a4 = (float)a; - } - /* - SDL_GetRGBA(getpixel(src, floor_x, floor_y), src->format, - &r1, &g1, &b1, &a1); - SDL_GetRGBA(getpixel(src, ceil_x, floor_y), src->format, - &r2, &g2, &b2, &a2); - SDL_GetRGBA(getpixel(src, floor_x, ceil_y), src->format, - &r3, &g3, &b3, &a3); - SDL_GetRGBA(getpixel(src, ceil_x, ceil_y), src->format, - &r4, &g4, &b4, &a4); - */ + SDL_GetRGBA(getpixel(src, floor_x, floor_y), src->format, &r, &g, &b, + &a); + r1 = (float) r; + g1 = (float) g; + b1 = (float) b; + a1 = (float) a; + SDL_GetRGBA(getpixel(src, ceil_x, floor_y), src->format, &r, &g, &b, + &a); + r2 = (float) r; + g2 = (float) g; + b2 = (float) b; + a2 = (float) a; + SDL_GetRGBA(getpixel(src, floor_x, ceil_y), src->format, &r, &g, &b, + &a); + r3 = (float) r; + g3 = (float) g; + b3 = (float) b; + a3 = (float) a; + SDL_GetRGBA(getpixel(src, ceil_x, ceil_y), src->format, &r, &g, &b, + &a); + r4 = (float) r; + g4 = (float) g; + b4 = (float) b; + a4 = (float) a; + } + /* + SDL_GetRGBA(getpixel(src, floor_x, floor_y), src->format, + &r1, &g1, &b1, &a1); + SDL_GetRGBA(getpixel(src, ceil_x, floor_y), src->format, + &r2, &g2, &b2, &a2); + SDL_GetRGBA(getpixel(src, floor_x, ceil_y), src->format, + &r3, &g3, &b3, &a3); + SDL_GetRGBA(getpixel(src, ceil_x, ceil_y), src->format, + &r4, &g4, &b4, &a4); + */ #else - { - Uint8 r, g, b, a; + { + Uint8 r, g, b, a; - r = g = b = a = 0; /* Unused, bah! */ + r = g = b = a = 0; /* Unused, bah! */ - SDL_GetRGBA(getpixel(src, floor_x, floor_y), src->format, &r, &g, &b, &a); - r1 = (float)r; - g1 = (float)g; - b1 = (float)b; - a1 = (float)a; + SDL_GetRGBA(getpixel(src, floor_x, floor_y), src->format, &r, &g, &b, + &a); + r1 = (float) r; + g1 = (float) g; + b1 = (float) b; + a1 = (float) a; - SDL_GetRGBA(getpixel(src, ceil_x, floor_y), src->format, &r, &g, &b, &a); - r2 = (float)r; - g2 = (float)g; - b2 = (float)b; - a2 = (float)a; + SDL_GetRGBA(getpixel(src, ceil_x, floor_y), src->format, &r, &g, &b, + &a); + r2 = (float) r; + g2 = (float) g; + b2 = (float) b; + a2 = (float) a; - SDL_GetRGBA(getpixel(src, floor_x, ceil_y), src->format, &r, &g, &b, &a); - r3 = (float)r; - g3 = (float)g; - b3 = (float)b; - a3 = (float)a; + SDL_GetRGBA(getpixel(src, floor_x, ceil_y), src->format, &r, &g, &b, + &a); + r3 = (float) r; + g3 = (float) g; + b3 = (float) b; + a3 = (float) a; - SDL_GetRGBA(getpixel(src, ceil_x, ceil_y), src->format, &r, &g, &b, &a); - r4 = (float)r; - g4 = (float)g; - b4 = (float)b; - a4 = (float)a; - } + SDL_GetRGBA(getpixel(src, ceil_x, ceil_y), src->format, &r, &g, &b, + &a); + r4 = (float) r; + g4 = (float) g; + b4 = (float) b; + a4 = (float) a; + } #endif - n1 = (one_minus_x * r1 + fraction_x * r2); - n2 = (one_minus_x * r3 + fraction_x * r4); - r = (one_minus_y * n1 + fraction_y * n2); + n1 = (one_minus_x * r1 + fraction_x * r2); + n2 = (one_minus_x * r3 + fraction_x * r4); + r = (one_minus_y * n1 + fraction_y * n2); - n1 = (one_minus_x * g1 + fraction_x * g2); - n2 = (one_minus_x * g3 + fraction_x * g4); - g = (one_minus_y * n1 + fraction_y * n2); + n1 = (one_minus_x * g1 + fraction_x * g2); + n2 = (one_minus_x * g3 + fraction_x * g4); + g = (one_minus_y * n1 + fraction_y * n2); - n1 = (one_minus_x * b1 + fraction_x * b2); - n2 = (one_minus_x * b3 + fraction_x * b4); - b = (one_minus_y * n1 + fraction_y * n2); + n1 = (one_minus_x * b1 + fraction_x * b2); + n2 = (one_minus_x * b3 + fraction_x * b4); + b = (one_minus_y * n1 + fraction_y * n2); - n1 = (one_minus_x * a1 + fraction_x * a2); - n2 = (one_minus_x * a3 + fraction_x * a4); - a = (one_minus_y * n1 + fraction_y * n2); + n1 = (one_minus_x * a1 + fraction_x * a2); + n2 = (one_minus_x * a3 + fraction_x * a4); + a = (one_minus_y * n1 + fraction_y * n2); - putpixel(s, x, y, SDL_MapRGBA(s->format, r, g, b, a)); - } + putpixel(s, x, y, SDL_MapRGBA(s->format, r, g, b, a)); } + } SDL_UnlockSurface(s); SDL_UnlockSurface(src); @@ -11376,8 +12124,8 @@ static void _xorpixel(SDL_Surface * surf, int x, int y) BytesPerPixel = surf->format->BytesPerPixel; /* Set a pointer to the exact location in memory of the pixel */ - p = (Uint8 *) (((Uint8 *) surf->pixels) + /* Start: beginning of RAM */ - (y * surf->pitch) + /* Go down Y lines */ + p = (Uint8 *) (((Uint8 *) surf->pixels) + /* Start: beginning of RAM */ + (y * surf->pitch) + /* Go down Y lines */ (x * BytesPerPixel)); /* Go in X pixels */ /* XOR the (correctly-sized) piece of data in the surface's RAM */ @@ -11388,19 +12136,21 @@ static void _xorpixel(SDL_Surface * surf, int x, int y) else if (BytesPerPixel == 2) *(Uint16 *) p ^= 0xd6d6; else if (BytesPerPixel == 3) - { - p[0] ^= 0x80; - p[1] ^= 0x80; - p[2] ^= 0x80; - } + { + p[0] ^= 0x80; + p[1] ^= 0x80; + p[2] ^= 0x80; + } } /** * FIXME */ -static void xorpixel(int x, int y) { +static void xorpixel(int x, int y) +{ /* if outside the canvas, return */ - if ((unsigned)x >= (unsigned)canvas->w || (unsigned)y >= (unsigned)canvas->h) + if ((unsigned) x >= (unsigned) canvas->w + || (unsigned) y >= (unsigned) canvas->h) return; /* now switch to screen coordinates */ @@ -11422,60 +12172,62 @@ static void do_undo(void) wanna_update_toolbar = 0; if (cur_undo != oldest_undo) - { - cur_undo--; + { + cur_undo--; - do_undo_label_node(); + do_undo_label_node(); - if (cur_undo < 0) - cur_undo = NUM_UNDO_BUFS - 1; + if (cur_undo < 0) + cur_undo = NUM_UNDO_BUFS - 1; #ifdef DEBUG - printf("BLITTING: %d\n", cur_undo); + printf("BLITTING: %d\n", cur_undo); #endif - SDL_BlitSurface(undo_bufs[cur_undo], NULL, canvas, NULL); + SDL_BlitSurface(undo_bufs[cur_undo], NULL, canvas, NULL); - if (img_starter != NULL) - { - if (undo_starters[cur_undo] == UNDO_STARTER_MIRRORED) - { - starter_mirrored = !starter_mirrored; - mirror_starter(); - } - else if (undo_starters[cur_undo] == UNDO_STARTER_FLIPPED) - { - starter_flipped = !starter_flipped; - flip_starter(); - } - } - - update_canvas(0, 0, (WINDOW_WIDTH - r_ttoolopt.w), (button_h * 7) + 40 + HEIGHTOFFSET); - - - if (cur_undo == oldest_undo) - { - tool_avail[TOOL_UNDO] = 0; - wanna_update_toolbar = 1; - } - - if (tool_avail[TOOL_REDO] == 0) - { - tool_avail[TOOL_REDO] = 1; - wanna_update_toolbar = 1; - } - - if (wanna_update_toolbar) - { - draw_toolbar(); - update_screen_rect(&r_tools); - } - - been_saved = 0; + if (img_starter != NULL) + { + if (undo_starters[cur_undo] == UNDO_STARTER_MIRRORED) + { + starter_mirrored = !starter_mirrored; + mirror_starter(); + } + else if (undo_starters[cur_undo] == UNDO_STARTER_FLIPPED) + { + starter_flipped = !starter_flipped; + flip_starter(); + } } + update_canvas(0, 0, (WINDOW_WIDTH - r_ttoolopt.w), + (button_h * 7) + 40 + HEIGHTOFFSET); + + + if (cur_undo == oldest_undo) + { + tool_avail[TOOL_UNDO] = 0; + wanna_update_toolbar = 1; + } + + if (tool_avail[TOOL_REDO] == 0) + { + tool_avail[TOOL_REDO] = 1; + wanna_update_toolbar = 1; + } + + if (wanna_update_toolbar) + { + draw_toolbar(); + update_screen_rect(&r_tools); + } + + been_saved = 0; + } + #ifdef DEBUG - printf("UNDO: Current=%d Oldest=%d Newest=%d\n", cur_undo, oldest_undo, newest_undo); + printf("UNDO: Current=%d Oldest=%d Newest=%d\n", cur_undo, oldest_undo, + newest_undo); #endif } @@ -11487,43 +12239,45 @@ static void do_undo(void) static void do_redo(void) { if (cur_undo != newest_undo) + { + if (img_starter != NULL) { - if (img_starter != NULL) - { - if (undo_starters[cur_undo] == UNDO_STARTER_MIRRORED) - { - starter_mirrored = !starter_mirrored; - mirror_starter(); - } - else if (undo_starters[cur_undo] == UNDO_STARTER_FLIPPED) - { - starter_flipped = !starter_flipped; - flip_starter(); - } - } - - cur_undo = (cur_undo + 1) % NUM_UNDO_BUFS; - -#ifdef DEBUG - printf("BLITTING: %d\n", cur_undo); -#endif - do_redo_label_node(); - SDL_BlitSurface(undo_bufs[cur_undo], NULL, canvas, NULL); - - update_canvas(0, 0, (WINDOW_WIDTH - r_ttoolopt.w), (button_h * 7) + 40 + HEIGHTOFFSET); - - been_saved = 0; + if (undo_starters[cur_undo] == UNDO_STARTER_MIRRORED) + { + starter_mirrored = !starter_mirrored; + mirror_starter(); + } + else if (undo_starters[cur_undo] == UNDO_STARTER_FLIPPED) + { + starter_flipped = !starter_flipped; + flip_starter(); + } } + cur_undo = (cur_undo + 1) % NUM_UNDO_BUFS; + #ifdef DEBUG - printf("REDO: Current=%d Oldest=%d Newest=%d\n", cur_undo, oldest_undo, newest_undo); + printf("BLITTING: %d\n", cur_undo); +#endif + do_redo_label_node(); + SDL_BlitSurface(undo_bufs[cur_undo], NULL, canvas, NULL); + + update_canvas(0, 0, (WINDOW_WIDTH - r_ttoolopt.w), + (button_h * 7) + 40 + HEIGHTOFFSET); + + been_saved = 0; + } + +#ifdef DEBUG + printf("REDO: Current=%d Oldest=%d Newest=%d\n", cur_undo, oldest_undo, + newest_undo); #endif if (((cur_undo + 1) % NUM_UNDO_BUFS) == newest_undo) - { - tool_avail[TOOL_REDO] = 0; - } + { + tool_avail[TOOL_REDO] = 0; + } tool_avail[TOOL_UNDO] = 1; @@ -11542,8 +12296,10 @@ static void render_brush(void) int x, y; Uint8 r, g, b, a; - Uint32(*getpixel_brush) (SDL_Surface *, int, int) = getpixels[img_brushes[cur_brush]->format->BytesPerPixel]; - void (*putpixel_brush) (SDL_Surface *, int, int, Uint32) = putpixels[img_brushes[cur_brush]->format->BytesPerPixel]; + Uint32(*getpixel_brush) (SDL_Surface *, int, int) = + getpixels[img_brushes[cur_brush]->format->BytesPerPixel]; + void (*putpixel_brush)(SDL_Surface *, int, int, Uint32) = + putpixels[img_brushes[cur_brush]->format->BytesPerPixel]; /* Kludge; not sure why cur_brush would become greater! */ @@ -11555,15 +12311,16 @@ static void render_brush(void) /* Free the old rendered brush (if any): */ if (img_cur_brush != NULL) - { - SDL_FreeSurface(img_cur_brush); - } + { + SDL_FreeSurface(img_cur_brush); + } /* Create a surface to render into: */ amask = ~(img_brushes[cur_brush]->format->Rmask | - img_brushes[cur_brush]->format->Gmask | img_brushes[cur_brush]->format->Bmask); + img_brushes[cur_brush]->format->Gmask | img_brushes[cur_brush]-> + format->Bmask); img_cur_brush = SDL_CreateRGBSurface(SDL_SWSURFACE, @@ -11571,16 +12328,18 @@ static void render_brush(void) img_brushes[cur_brush]->h, img_brushes[cur_brush]->format->BitsPerPixel, img_brushes[cur_brush]->format->Rmask, - img_brushes[cur_brush]->format->Gmask, img_brushes[cur_brush]->format->Bmask, amask); + img_brushes[cur_brush]->format->Gmask, + img_brushes[cur_brush]->format->Bmask, amask); if (img_cur_brush == NULL) - { - fprintf(stderr, "\nError: Can't render a brush!\n" - "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", SDL_GetError()); + { + fprintf(stderr, "\nError: Can't render a brush!\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", SDL_GetError()); - cleanup(); - exit(1); - } + cleanup(); + exit(1); + } /* Render the new brush: */ @@ -11589,35 +12348,39 @@ static void render_brush(void) SDL_LockSurface(img_cur_brush); for (y = 0; y < img_brushes[cur_brush]->h; y++) + { + for (x = 0; x < img_brushes[cur_brush]->w; x++) { - for (x = 0; x < img_brushes[cur_brush]->w; x++) - { - SDL_GetRGBA(getpixel_brush(img_brushes[cur_brush], x, y), img_brushes[cur_brush]->format, &r, &g, &b, &a); + SDL_GetRGBA(getpixel_brush(img_brushes[cur_brush], x, y), + img_brushes[cur_brush]->format, &r, &g, &b, &a); - if (r == g && g == b) - { - putpixel_brush(img_cur_brush, x, y, - SDL_MapRGBA(img_cur_brush->format, - color_hexes[cur_color][0], - color_hexes[cur_color][1], color_hexes[cur_color][2], a)); - } - else - { - putpixel_brush(img_cur_brush, x, y, - SDL_MapRGBA(img_cur_brush->format, - (r + color_hexes[cur_color][0]) >> 1, - (g + color_hexes[cur_color][1]) >> 1, - (b + color_hexes[cur_color][2]) >> 1, a)); - } - } + if (r == g && g == b) + { + putpixel_brush(img_cur_brush, x, y, + SDL_MapRGBA(img_cur_brush->format, + color_hexes[cur_color][0], + color_hexes[cur_color][1], + color_hexes[cur_color][2], a)); + } + else + { + putpixel_brush(img_cur_brush, x, y, + SDL_MapRGBA(img_cur_brush->format, + (r + color_hexes[cur_color][0]) >> 1, + (g + color_hexes[cur_color][1]) >> 1, + (b + color_hexes[cur_color][2]) >> 1, a)); + } } + } SDL_UnlockSurface(img_cur_brush); SDL_UnlockSurface(img_brushes[cur_brush]); img_cur_brush_frame_w = img_cur_brush->w / abs(brushes_frames[cur_brush]); - img_cur_brush_w = img_cur_brush_frame_w / (brushes_directional[cur_brush] ? 3 : 1); - img_cur_brush_h = img_cur_brush->h / (brushes_directional[cur_brush] ? 3 : 1); + img_cur_brush_w = + img_cur_brush_frame_w / (brushes_directional[cur_brush] ? 3 : 1); + img_cur_brush_h = + img_cur_brush->h / (brushes_directional[cur_brush] ? 3 : 1); img_cur_brush_frames = brushes_frames[cur_brush]; img_cur_brush_directional = brushes_directional[cur_brush]; img_cur_brush_rotate = brushes_rotate[cur_brush]; @@ -11630,26 +12393,27 @@ static void render_brush(void) /** * Show a tool tip, for when the brush choice has changed */ -static void show_brush_tip(void) { +static void show_brush_tip(void) +{ if (img_cur_brush_rotate || img_cur_brush_directional) + { + if (abs(img_cur_brush_frames) > 1) { - if (abs(img_cur_brush_frames) > 1) - { - draw_tux_text(TUX_GREAT, TIP_BRUSH_CHOICE_ANM_DIR, 1); - } - else - { - draw_tux_text(TUX_GREAT, TIP_BRUSH_CHOICE_DIR, 1); - } + draw_tux_text(TUX_GREAT, TIP_BRUSH_CHOICE_ANM_DIR, 1); } + else + { + draw_tux_text(TUX_GREAT, TIP_BRUSH_CHOICE_DIR, 1); + } + } else if (abs(img_cur_brush_frames) > 1) - { - draw_tux_text(TUX_GREAT, TIP_BRUSH_CHOICE_ANM, 1); - } + { + draw_tux_text(TUX_GREAT, TIP_BRUSH_CHOICE_ANM, 1); + } else - { - draw_tux_text(TUX_GREAT, tool_tips[cur_tool], 1); - } + { + draw_tux_text(TUX_GREAT, tool_tips[cur_tool], 1); + } } @@ -11673,61 +12437,61 @@ static void line_xor(int x1, int y1, int x2, int y2) num_drawn = 0; if (dx != 0) + { + m = ((float) dy) / ((float) dx); + b = y1 - m * x1; + + if (x2 >= x1) + dx = 1; + else + dx = -1; + + + while (x1 != x2) { - m = ((float)dy) / ((float)dx); - b = y1 - m * x1; + y1 = m * x1 + b; + y2 = m * (x1 + dx) + b; - if (x2 >= x1) - dx = 1; - else - dx = -1; - - - while (x1 != x2) - { - y1 = m * x1 + b; - y2 = m * (x1 + dx) + b; - - if (y1 > y2) - { - y = y1; - y1 = y2; - y2 = y; - } - - for (y = y1; y <= y2; y++) - { - num_drawn++; - if (num_drawn < 10 || dont_do_xor == 0) - xorpixel(x1, y); - } - - x1 = x1 + dx; - } - } - else - { if (y1 > y2) - { - for (y = y1; y >= y2; y--) - { - num_drawn++; + { + y = y1; + y1 = y2; + y2 = y; + } - if (num_drawn < 10 || dont_do_xor == 0) - xorpixel(x1, y); - } - } - else - { - for (y = y1; y <= y2; y++) - { - num_drawn++; + for (y = y1; y <= y2; y++) + { + num_drawn++; + if (num_drawn < 10 || dont_do_xor == 0) + xorpixel(x1, y); + } - if (num_drawn < 10 || dont_do_xor == 0) - xorpixel(x1, y); - } - } + x1 = x1 + dx; } + } + else + { + if (y1 > y2) + { + for (y = y1; y >= y2; y--) + { + num_drawn++; + + if (num_drawn < 10 || dont_do_xor == 0) + xorpixel(x1, y); + } + } + else + { + for (y = y1; y <= y2; y++) + { + num_drawn++; + + if (num_drawn < 10 || dont_do_xor == 0) + xorpixel(x1, y); + } + } + } /* SDL_UnlockSurface(screen); */ } @@ -11790,7 +12554,8 @@ static void circle_xor(int x, int y, int sz) xorpixel(x, y + sz); xorpixel(x, y - sz); - for (xx = 1; xx < sz; xx++) { + for (xx = 1; xx < sz; xx++) + { yy = sqrt(sz2 - (xx * xx) + 0.5); xorpixel(x + xx, y + yy); xorpixel(x + xx, y - yy); @@ -11806,7 +12571,9 @@ static int calc_eraser_size(int which_eraser) if (which_eraser >= NUM_SIZES) which_eraser -= NUM_SIZES; - return(((NUM_SIZES - 1 - which_eraser) * ((ERASER_MAX - ERASER_MIN) / (NUM_SIZES - 1))) + ERASER_MIN); + return (((NUM_SIZES - 1 - + which_eraser) * ((ERASER_MAX - ERASER_MIN) / (NUM_SIZES - 1))) + + ERASER_MIN); } /** @@ -11822,95 +12589,101 @@ static void do_eraser(int x, int y, int update) sz = calc_eraser_size(cur_eraser); if (cur_eraser < NUM_SIZES) - { - /* Square eraser: */ + { + /* Square eraser: */ - dest.x = x - (sz / 2); - dest.y = y - (sz / 2); - dest.w = sz; - dest.h = sz; + dest.x = x - (sz / 2); + dest.y = y - (sz / 2); + dest.w = sz; + dest.h = sz; - if (img_starter_bkgd == NULL) - SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, canvas_color_r, canvas_color_g, canvas_color_b)); - else - SDL_BlitSurface(img_starter_bkgd, &dest, canvas, &dest); - } + if (img_starter_bkgd == NULL) + SDL_FillRect(canvas, &dest, + SDL_MapRGB(canvas->format, canvas_color_r, canvas_color_g, + canvas_color_b)); + else + SDL_BlitSurface(img_starter_bkgd, &dest, canvas, &dest); + } else + { + /* Round eraser: */ + + for (yy = 0; yy <= sz; yy++) { - /* Round eraser: */ + hit = 0; + for (xx = 0; xx <= sz && hit == 0; xx++) + { + n = (xx * xx) + (yy * yy) - ((sz * sz) / 4); - for (yy = 0; yy <= sz; yy++) + if (n >= -sz && n <= sz) + hit = 1; + + if (hit) { - hit = 0; - for (xx = 0; xx <= sz && hit == 0; xx++) - { - n = (xx * xx) + (yy * yy) - ((sz * sz) / 4); + dest.x = x - xx; + dest.y = y - yy; + dest.w = xx * 2; + dest.h = 1; - if (n >= -sz && n <= sz) - hit = 1; - - if (hit) - { - dest.x = x - xx; - dest.y = y - yy; - dest.w = xx * 2; - dest.h = 1; - - if (img_starter_bkgd == NULL) - SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, - canvas_color_r, canvas_color_g, canvas_color_b)); - else - SDL_BlitSurface(img_starter_bkgd, &dest, canvas, &dest); + if (img_starter_bkgd == NULL) + SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, + canvas_color_r, + canvas_color_g, + canvas_color_b)); + else + SDL_BlitSurface(img_starter_bkgd, &dest, canvas, &dest); - dest.x = x - xx; - dest.y = y + yy; - dest.w = xx * 2; - dest.h = 1; + dest.x = x - xx; + dest.y = y + yy; + dest.w = xx * 2; + dest.h = 1; - if (img_starter_bkgd == NULL) - SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, - canvas_color_r, canvas_color_g, canvas_color_b)); - else - SDL_BlitSurface(img_starter_bkgd, &dest, canvas, &dest); - } - } + if (img_starter_bkgd == NULL) + SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, + canvas_color_r, + canvas_color_g, + canvas_color_b)); + else + SDL_BlitSurface(img_starter_bkgd, &dest, canvas, &dest); } + } } + } #ifndef NOSOUND if (!mute && use_sound) + { + if (!Mix_Playing(0)) { - if (!Mix_Playing(0)) - { - eraser_sound = (eraser_sound + 1) % 2; + eraser_sound = (eraser_sound + 1) % 2; - playsound(screen, 0, SND_ERASER1 + eraser_sound, 0, x, SNDDIST_NEAR); - } + playsound(screen, 0, SND_ERASER1 + eraser_sound, 0, x, SNDDIST_NEAR); } + } #endif if (update) - { - update_canvas(x - sz / 2, y - sz / 2, x + sz / 2, y + sz / 2); + { + update_canvas(x - sz / 2, y - sz / 2, x + sz / 2, y + sz / 2); - if (cur_eraser >= NUM_ERASERS / 2) - { - /* Circle eraser */ - circle_xor(x, y, sz / 2); - } - else - { - /* Square eraser */ - rect_xor(x - sz / 2, y - sz / 2, x + sz / 2, y + sz / 2); - } + if (cur_eraser >= NUM_ERASERS / 2) + { + /* Circle eraser */ + circle_xor(x, y, sz / 2); + } + else + { + /* Square eraser */ + rect_xor(x - sz / 2, y - sz / 2, x + sz / 2, y + sz / 2); + } #ifdef __APPLE__ - /* Prevent ghosted eraser outlines from remaining on the screen in windowed mode */ - update_screen(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); + /* Prevent ghosted eraser outlines from remaining on the screen in windowed mode */ + update_screen(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); #endif - } + } } /** @@ -11938,65 +12711,66 @@ static void eraser_draw(int x1, int y1, int x2, int y2) dy = y2 - y1; if (dx != 0) + { + m = ((float) dy) / ((float) dx); + b = y1 - m * x1; + + if (x2 >= x1) + dx = 1; + else + dx = -1; + + + while (x1 != x2) { - m = ((float)dy) / ((float)dx); - b = y1 - m * x1; + y1 = m * x1 + b; + y2 = m * (x1 + dx) + b; - if (x2 >= x1) - dx = 1; - else - dx = -1; - - - while (x1 != x2) - { - y1 = m * x1 + b; - y2 = m * (x1 + dx) + b; - - if (y1 > y2) - { - for (y = y1; y >= y2; y--) - do_eraser(x1, y, 0); - } - else - { - for (y = y1; y <= y2; y++) - do_eraser(x1, y, 0); - } - - x1 = x1 + dx; - } - } - else - { if (y1 > y2) - { - y = y1; - y1 = y2; - y2 = y; - } + { + for (y = y1; y >= y2; y--) + do_eraser(x1, y, 0); + } + else + { + for (y = y1; y <= y2; y++) + do_eraser(x1, y, 0); + } - for (y = y1; y <= y2; y++) - do_eraser(x1, y, 0); + x1 = x1 + dx; } + } + else + { + if (y1 > y2) + { + y = y1; + y1 = y2; + y2 = y; + } + + for (y = y1; y <= y2; y++) + do_eraser(x1, y, 0); + } if (orig_x1 > orig_x2) - { - tmp = orig_x1; - orig_x1 = orig_x2; - orig_x2 = tmp; - } + { + tmp = orig_x1; + orig_x1 = orig_x2; + orig_x2 = tmp; + } if (orig_y1 > orig_y2) - { - tmp = orig_y1; - orig_y1 = orig_y2; - orig_y2 = tmp; - } + { + tmp = orig_y1; + orig_y1 = orig_y2; + orig_y2 = tmp; + } length = (calc_eraser_size(cur_eraser) >> 1) + 1; - update_canvas(orig_x1 - length, orig_y1 - length, orig_x2 + length, orig_y2 + length); + update_canvas(orig_x1 - length, orig_y1 - length, orig_x2 + length, + orig_y2 + length); } /** @@ -12009,9 +12783,9 @@ static void reset_avail_tools(void) int disallow_print = disable_print; /* set to 1 later if printer unavail */ for (i = 0; i < NUM_TOOLS; i++) - { - tool_avail[i] = 1; - } + { + tool_avail[i] = 1; + } /* Unavailable at the beginning of a new canvas: */ @@ -12089,10 +12863,10 @@ static void disable_avail_tools(void) hide_blinking_cursor(); for (i = 0; i < NUM_TOOLS; i++) - { - tool_avail_bak[i] = tool_avail[i]; - tool_avail[i] = 0; - } + { + tool_avail_bak[i] = tool_avail[i]; + tool_avail[i] = 0; + } } /** @@ -12104,9 +12878,9 @@ static void enable_avail_tools(void) int i; for (i = 0; i < NUM_TOOLS; i++) - { - tool_avail[i] = tool_avail_bak[i]; - } + { + tool_avail[i] = tool_avail_bak[i]; + } } @@ -12141,7 +12915,8 @@ static int compare_dirent2s_invert(struct dirent2 *f1, struct dirent2 *f2) * FIXME */ /* Draw tux's text on the screen: */ -static void draw_tux_text(int which_tux, const char *const str, int want_right_to_left) +static void draw_tux_text(int which_tux, const char *const str, + int want_right_to_left) { draw_tux_text_ex(which_tux, str, want_right_to_left, 0); } @@ -12156,13 +12931,15 @@ static Uint8 latest_locale_text; */ static void redraw_tux_text(void) { - draw_tux_text_ex(latest_tux, latest_tux_text, latest_r2l, latest_locale_text); + draw_tux_text_ex(latest_tux, latest_tux_text, latest_r2l, + latest_locale_text); } /** * FIXME */ -static void draw_tux_text_ex(int which_tux, const char *const str, int want_right_to_left, Uint8 locale_text) +static void draw_tux_text_ex(int which_tux, const char *const str, + int want_right_to_left, Uint8 locale_text) { SDL_Rect dest; SDL_Color black = { 0, 0, 0, 0 }; @@ -12191,58 +12968,59 @@ static void draw_tux_text_ex(int which_tux, const char *const str, int want_righ /* Don't let sfx and speak buttons cover the top of Tux, either: */ if (cur_tool == TOOL_STAMP && use_sound && !mute) - { - if (dest.y < r_sfx.y + r_sfx.h) - dest.y = r_sfx.y + r_sfx.h; - } + { + if (dest.y < r_sfx.y + r_sfx.h) + dest.y = r_sfx.y + r_sfx.h; + } SDL_BlitSurface(img_tux[which_tux], NULL, screen, &dest); /* Wide enough for Tux, or two stamp sound buttons (whichever's wider) */ w = max(img_tux[which_tux]->w, img_btnsm_up->w * 2) + 5; - wordwrap_text_ex(str, black, w, r_tuxarea.y, r_tuxarea.w, want_right_to_left, locale_text); + wordwrap_text_ex(str, black, w, r_tuxarea.y, r_tuxarea.w, + want_right_to_left, locale_text); /* Draw 'sound effect' and 'speak' buttons, if we're in the Stamp tool */ if (cur_tool == TOOL_STAMP && use_sound && !mute) - { - /* Sound effect: */ + { + /* Sound effect: */ - if (stamp_data[stamp_group][cur_stamp[stamp_group]]->no_sound) - btn = img_btnsm_off; - else - btn = img_btnsm_up; + if (stamp_data[stamp_group][cur_stamp[stamp_group]]->no_sound) + btn = img_btnsm_off; + else + btn = img_btnsm_up; - dest.x = 0; - dest.y = r_tuxarea.y; + dest.x = 0; + dest.y = r_tuxarea.y; - SDL_BlitSurface(btn, NULL, screen, &dest); + SDL_BlitSurface(btn, NULL, screen, &dest); - dest.x = (img_btnsm_up->w - img_sfx->w) / 2; - dest.y = r_tuxarea.y + ((img_btnsm_up->h - img_sfx->h) / 2); + dest.x = (img_btnsm_up->w - img_sfx->w) / 2; + dest.y = r_tuxarea.y + ((img_btnsm_up->h - img_sfx->h) / 2); - SDL_BlitSurface(img_sfx, NULL, screen, &dest); + SDL_BlitSurface(img_sfx, NULL, screen, &dest); - /* Speak: */ + /* Speak: */ - if (stamp_data[stamp_group][cur_stamp[stamp_group]]->no_descsound) - btn = img_btnsm_off; - else - btn = img_btnsm_up; + if (stamp_data[stamp_group][cur_stamp[stamp_group]]->no_descsound) + btn = img_btnsm_off; + else + btn = img_btnsm_up; - dest.x = img_btnsm_up->w; - dest.y = r_tuxarea.y; + dest.x = img_btnsm_up->w; + dest.y = r_tuxarea.y; - SDL_BlitSurface(btn, NULL, screen, &dest); + SDL_BlitSurface(btn, NULL, screen, &dest); - dest.x = img_btnsm_up->w + ((img_btnsm_up->w - img_speak->w) / 2); - dest.y = r_tuxarea.y + ((img_btnsm_up->h - img_speak->h) / 2); + dest.x = img_btnsm_up->w + ((img_btnsm_up->w - img_speak->w) / 2); + dest.y = r_tuxarea.y + ((img_btnsm_up->h - img_speak->h) / 2); - SDL_BlitSurface(img_speak, NULL, screen, &dest); - } + SDL_BlitSurface(img_speak, NULL, screen, &dest); + } update_screen_rect(&r_tuxarea); } @@ -12259,23 +13037,26 @@ static void draw_tux_text_ex(int which_tux, const char *const str, int want_righ static void draw_cur_tool_tip(void) { if (cur_tool == TOOL_FILL) - { - draw_tux_text(tool_tux[cur_tool], fill_tips[cur_fill], 1); - } + { + draw_tux_text(tool_tux[cur_tool], fill_tips[cur_fill], 1); + } else if (cur_tool == TOOL_SHAPES) - { - draw_tux_text(tool_tux[cur_tool], shape_tool_tips[simple_shapes ? SHAPE_COMPLEXITY_SIMPLE : SHAPE_COMPLEXITY_NORMAL], 1); - } + { + draw_tux_text(tool_tux[cur_tool], + shape_tool_tips[simple_shapes ? SHAPE_COMPLEXITY_SIMPLE : + SHAPE_COMPLEXITY_NORMAL], 1); + } else - { - draw_tux_text(tool_tux[cur_tool], tool_tips[cur_tool], 1); - } + { + draw_tux_text(tool_tux[cur_tool], tool_tips[cur_tool], 1); + } } /** * FIXME */ -static void wordwrap_text(const char *const str, SDL_Color color, int left, int top, int right, int want_right_to_left) +static void wordwrap_text(const char *const str, SDL_Color color, int left, + int top, int right, int want_right_to_left) { wordwrap_text_ex(str, color, left, top, right, want_right_to_left, 0); } @@ -12284,7 +13065,8 @@ static void wordwrap_text(const char *const str, SDL_Color color, int left, int * FIXME */ static void wordwrap_text_ex(const char *const str, SDL_Color color, - int left, int top, int right, int want_right_to_left, Uint8 locale_text) + int left, int top, int right, + int want_right_to_left, Uint8 locale_text) { SDL_Surface *text; TuxPaint_Font *myfont = medium_font; @@ -12313,10 +13095,10 @@ static void wordwrap_text_ex(const char *const str, SDL_Color color, myfont = locale_font; if (strcmp(str, gettext(str)) == 0) - { - /* String isn't translated! Don't write right-to-left, even if our locale is an RTOL language: */ - want_right_to_left = 0; - } + { + /* String isn't translated! Don't write right-to-left, even if our locale is an RTOL language: */ + want_right_to_left = 0; + } #ifndef NO_SDLPANGO @@ -12325,43 +13107,50 @@ static void wordwrap_text_ex(const char *const str, SDL_Color color, sdl_color_to_pango_color(color, &pango_color); SDLPango_SetDefaultColor(myfont->pango_context, &pango_color); - SDLPango_SetMinimumSize(myfont->pango_context, right - left, canvas->h - top); + SDLPango_SetMinimumSize(myfont->pango_context, right - left, + canvas->h - top); if (want_right_to_left && need_right_to_left) + { + SDLPango_SetBaseDirection(locale_font->pango_context, + SDLPANGO_DIRECTION_RTL); + if (only_uppercase) { - SDLPango_SetBaseDirection(locale_font->pango_context, SDLPANGO_DIRECTION_RTL); - if (only_uppercase) - { - char *upper_str = uppercase(gettext(str)); + char *upper_str = uppercase(gettext(str)); - SDLPango_SetText_GivenAlignment(myfont->pango_context, upper_str, -1, SDLPANGO_ALIGN_RIGHT); - free(upper_str); - } - else - SDLPango_SetText_GivenAlignment(myfont->pango_context, gettext(str), -1, SDLPANGO_ALIGN_RIGHT); + SDLPango_SetText_GivenAlignment(myfont->pango_context, upper_str, -1, + SDLPANGO_ALIGN_RIGHT); + free(upper_str); } + else + SDLPango_SetText_GivenAlignment(myfont->pango_context, gettext(str), -1, + SDLPANGO_ALIGN_RIGHT); + } else + { + SDLPango_SetBaseDirection(locale_font->pango_context, + SDLPANGO_DIRECTION_LTR); + if (only_uppercase) { - SDLPango_SetBaseDirection(locale_font->pango_context, SDLPANGO_DIRECTION_LTR); - if (only_uppercase) - { - char *upper_str = uppercase(gettext(str)); + char *upper_str = uppercase(gettext(str)); - SDLPango_SetText_GivenAlignment(myfont->pango_context, upper_str, -1, SDLPANGO_ALIGN_LEFT); - free(upper_str); - } - else - SDLPango_SetText_GivenAlignment(myfont->pango_context, gettext(str), -1, SDLPANGO_ALIGN_LEFT); + SDLPango_SetText_GivenAlignment(myfont->pango_context, upper_str, -1, + SDLPANGO_ALIGN_LEFT); + free(upper_str); } + else + SDLPango_SetText_GivenAlignment(myfont->pango_context, gettext(str), -1, + SDLPANGO_ALIGN_LEFT); + } text = SDLPango_CreateSurfaceDraw(myfont->pango_context); dest.x = left; dest.y = top; if (text != NULL) - { - SDL_BlitSurface(text, NULL, screen, &dest); - SDL_FreeSurface(text); - } + { + SDL_BlitSurface(text, NULL, screen, &dest); + SDL_FreeSurface(text); + } #else /* Cursor starting position: */ @@ -12376,314 +13165,318 @@ static void wordwrap_text_ex(const char *const str, SDL_Color color, debug("..."); if (strcmp(str, "") != 0) + { + if (want_right_to_left == 0) + locale_str = (unsigned char *) strdup(gettext(str)); + else + locale_str = (unsigned char *) textdir(gettext(str)); + + + /* For each UTF8 character: */ + + utf8_str_len = 0; + utf8_str[0] = '\0'; + + for (i = 0; i <= strlen((char *) locale_str); i++) { - if (want_right_to_left == 0) - locale_str = (unsigned char *)strdup(gettext(str)); - else - locale_str = (unsigned char *)textdir(gettext(str)); - - - /* For each UTF8 character: */ - - utf8_str_len = 0; - utf8_str[0] = '\0'; - - for (i = 0; i <= strlen((char *)locale_str); i++) - { - if (locale_str[i] < 128) - { - utf8_str[utf8_str_len++] = locale_str[i]; - utf8_str[utf8_str_len] = '\0'; - - - /* Space? Blit the word! (Word-wrap if necessary) */ - - if (locale_str[i] == ' ' || locale_str[i] == '\0') - { - if (only_uppercase) - { - char *upper_utf8_str = uppercase((char *)utf8_str); - - text = render_text(myfont, (char *)upper_utf8_str, color); - free(upper_utf8_str); - } - else - text = render_text(myfont, (char *)utf8_str, color); - - if (!text) - continue; /* Didn't render anything... */ - - /* ----------- */ - if (text->w > right - left) - { - /* Move left and down (if not already at left!) */ - - if (x > left) - { - if (need_right_to_left && want_right_to_left) - anti_carriage_return(left, right, top, top + text->h, y + text->h, x - left); - - x = left; - y = y + text->h; - } - - - /* Junk the blitted word; it's too long! */ - - last_text_height = text->h; - SDL_FreeSurface(text); - - - /* For each UTF8 character: */ - - for (j = 0; j < utf8_str_len; j++) - { - /* How many bytes does this character need? */ - - if (utf8_str[j] < 128) /* 0xxx xxxx - 1 byte */ - { - utf8_char[0] = utf8_str[j]; - utf8_char[1] = '\0'; - } - else if ((utf8_str[j] & 0xE0) == 0xC0) /* 110x xxxx - 2 bytes */ - { - utf8_char[0] = utf8_str[j]; - utf8_char[1] = utf8_str[j + 1]; - utf8_char[2] = '\0'; - j = j + 1; - } - else if ((utf8_str[j] & 0xF0) == 0xE0) /* 1110 xxxx - 3 bytes */ - { - utf8_char[0] = utf8_str[j]; - utf8_char[1] = utf8_str[j + 1]; - utf8_char[2] = utf8_str[j + 2]; - utf8_char[3] = '\0'; - j = j + 2; - } - else /* 1111 0xxx - 4 bytes */ - { - utf8_char[0] = utf8_str[j]; - utf8_char[1] = utf8_str[j + 1]; - utf8_char[2] = utf8_str[j + 2]; - utf8_char[3] = utf8_str[j + 3]; - utf8_char[4] = '\0'; - j = j + 3; - } - - - if (utf8_char[0] != '\0') - { - text = render_text(myfont, (char *)utf8_char, color); - if (text != NULL) - { - if (x + text->w > right) - { - if (need_right_to_left && want_right_to_left) - anti_carriage_return(left, right, top, top + text->h, y + text->h, x - left); - - x = left; - y = y + text->h; - } - - dest.x = x; - - if (need_right_to_left && want_right_to_left) - dest.y = top; - else - dest.y = y; - - SDL_BlitSurface(text, NULL, screen, &dest); - - last_text_height = text->h; - - x = x + text->w; - - SDL_FreeSurface(text); - } - } - } - } - else - { - /* Not too wide for one line... */ - - if (x + text->w > right) - { - /* This word needs to move down? */ - - if (need_right_to_left && want_right_to_left) - anti_carriage_return(left, right, top, top + text->h, y + text->h, x - left); - - x = left; - y = y + text->h; - } - - dest.x = x; - - if (need_right_to_left && want_right_to_left) - dest.y = top; - else - dest.y = y; - - SDL_BlitSurface(text, NULL, screen, &dest); - - last_text_height = text->h; - x = x + text->w; - - SDL_FreeSurface(text); - } - - - utf8_str_len = 0; - utf8_str[0] = '\0'; - } - } - else if ((locale_str[i] & 0xE0) == 0xC0) - { - utf8_str[utf8_str_len++] = locale_str[i]; - utf8_str[utf8_str_len++] = locale_str[i + 1]; - utf8_str[utf8_str_len] = '\0'; - i++; - } - else if ((locale_str[i] & 0xF0) == 0xE0) - { - utf8_str[utf8_str_len++] = locale_str[i]; - utf8_str[utf8_str_len++] = locale_str[i + 1]; - utf8_str[utf8_str_len++] = locale_str[i + 2]; - utf8_str[utf8_str_len] = '\0'; - i = i + 2; - } - else - { - utf8_str[utf8_str_len++] = locale_str[i]; - utf8_str[utf8_str_len++] = locale_str[i + 1]; - utf8_str[utf8_str_len++] = locale_str[i + 2]; - utf8_str[utf8_str_len++] = locale_str[i + 3]; - utf8_str[utf8_str_len] = '\0'; - i = i + 3; - } - } - - free(locale_str); - } - else if (strlen(str) != 0) - { - /* Truncate if too big! (sorry!) */ - + if (locale_str[i] < 128) { - char *s1 = gettext(str); - - if (want_right_to_left) - { - char *freeme = s1; - - s1 = textdir(s1); - free(freeme); - } - tstr = uppercase(s1); - free(s1); - } - - if (strlen(tstr) > sizeof(substr) - 1) - tstr[sizeof(substr) - 1] = '\0'; + utf8_str[utf8_str_len++] = locale_str[i]; + utf8_str[utf8_str_len] = '\0'; - /* For each word... */ + /* Space? Blit the word! (Word-wrap if necessary) */ - for (i = 0; i < strlen(tstr); i++) + if (locale_str[i] == ' ' || locale_str[i] == '\0') { - /* Figure out the word... */ - - len = 0; - - for (j = i; tstr[j] != ' ' && tstr[j] != '\0'; j++) - { - substr[len++] = tstr[j]; - } - - substr[len++] = ' '; - substr[len] = '\0'; - - - /* Render the word for display... */ - - if (only_uppercase) - { - char *uppercase_substr = uppercase(substr); + { + char *upper_utf8_str = uppercase((char *) utf8_str); - text = render_text(myfont, uppercase_substr, color); - free(uppercase_substr); - } + text = render_text(myfont, (char *) upper_utf8_str, color); + free(upper_utf8_str); + } else - text = render_text(myfont, substr, color); + text = render_text(myfont, (char *) utf8_str, color); + if (!text) + continue; /* Didn't render anything... */ - /* If it won't fit on this line, move to the next! */ + /* ----------- */ + if (text->w > right - left) + { + /* Move left and down (if not already at left!) */ - if (x + text->w > right) /* Correct? */ + if (x > left) { if (need_right_to_left && want_right_to_left) - anti_carriage_return(left, right, top, top + text->h, y + text->h, x - left); + anti_carriage_return(left, right, top, top + text->h, + y + text->h, x - left); x = left; y = y + text->h; } - /* Draw the word: */ + /* Junk the blitted word; it's too long! */ - dest.x = x; + last_text_height = text->h; + SDL_FreeSurface(text); - if (need_right_to_left && want_right_to_left) - dest.y = top; + + /* For each UTF8 character: */ + + for (j = 0; j < utf8_str_len; j++) + { + /* How many bytes does this character need? */ + + if (utf8_str[j] < 128) /* 0xxx xxxx - 1 byte */ + { + utf8_char[0] = utf8_str[j]; + utf8_char[1] = '\0'; + } + else if ((utf8_str[j] & 0xE0) == 0xC0) /* 110x xxxx - 2 bytes */ + { + utf8_char[0] = utf8_str[j]; + utf8_char[1] = utf8_str[j + 1]; + utf8_char[2] = '\0'; + j = j + 1; + } + else if ((utf8_str[j] & 0xF0) == 0xE0) /* 1110 xxxx - 3 bytes */ + { + utf8_char[0] = utf8_str[j]; + utf8_char[1] = utf8_str[j + 1]; + utf8_char[2] = utf8_str[j + 2]; + utf8_char[3] = '\0'; + j = j + 2; + } + else /* 1111 0xxx - 4 bytes */ + { + utf8_char[0] = utf8_str[j]; + utf8_char[1] = utf8_str[j + 1]; + utf8_char[2] = utf8_str[j + 2]; + utf8_char[3] = utf8_str[j + 3]; + utf8_char[4] = '\0'; + j = j + 3; + } + + + if (utf8_char[0] != '\0') + { + text = render_text(myfont, (char *) utf8_char, color); + if (text != NULL) + { + if (x + text->w > right) + { + if (need_right_to_left && want_right_to_left) + anti_carriage_return(left, right, top, top + text->h, + y + text->h, x - left); + + x = left; + y = y + text->h; + } + + dest.x = x; + + if (need_right_to_left && want_right_to_left) + dest.y = top; + else + dest.y = y; + + SDL_BlitSurface(text, NULL, screen, &dest); + + last_text_height = text->h; + + x = x + text->w; + + SDL_FreeSurface(text); + } + } + } + } else - dest.y = y; + { + /* Not too wide for one line... */ - SDL_BlitSurface(text, NULL, screen, &dest); + if (x + text->w > right) + { + /* This word needs to move down? */ + + if (need_right_to_left && want_right_to_left) + anti_carriage_return(left, right, top, top + text->h, + y + text->h, x - left); + + x = left; + y = y + text->h; + } + + dest.x = x; + + if (need_right_to_left && want_right_to_left) + dest.y = top; + else + dest.y = y; + + SDL_BlitSurface(text, NULL, screen, &dest); + + last_text_height = text->h; + x = x + text->w; + + SDL_FreeSurface(text); + } - /* Move the cursor one word's worth: */ - - x = x + text->w; - - - /* Free the temp. surface: */ - - last_text_height = text->h; - SDL_FreeSurface(text); - - - /* Now on to the next word... */ - - i = j; + utf8_str_len = 0; + utf8_str[0] = '\0'; } - - free(tstr); + } + else if ((locale_str[i] & 0xE0) == 0xC0) + { + utf8_str[utf8_str_len++] = locale_str[i]; + utf8_str[utf8_str_len++] = locale_str[i + 1]; + utf8_str[utf8_str_len] = '\0'; + i++; + } + else if ((locale_str[i] & 0xF0) == 0xE0) + { + utf8_str[utf8_str_len++] = locale_str[i]; + utf8_str[utf8_str_len++] = locale_str[i + 1]; + utf8_str[utf8_str_len++] = locale_str[i + 2]; + utf8_str[utf8_str_len] = '\0'; + i = i + 2; + } + else + { + utf8_str[utf8_str_len++] = locale_str[i]; + utf8_str[utf8_str_len++] = locale_str[i + 1]; + utf8_str[utf8_str_len++] = locale_str[i + 2]; + utf8_str[utf8_str_len++] = locale_str[i + 3]; + utf8_str[utf8_str_len] = '\0'; + i = i + 3; + } } + free(locale_str); + } + else if (strlen(str) != 0) + { + /* Truncate if too big! (sorry!) */ + + { + char *s1 = gettext(str); + + if (want_right_to_left) + { + char *freeme = s1; + + s1 = textdir(s1); + free(freeme); + } + tstr = uppercase(s1); + free(s1); + } + + if (strlen(tstr) > sizeof(substr) - 1) + tstr[sizeof(substr) - 1] = '\0'; + + + /* For each word... */ + + for (i = 0; i < strlen(tstr); i++) + { + /* Figure out the word... */ + + len = 0; + + for (j = i; tstr[j] != ' ' && tstr[j] != '\0'; j++) + { + substr[len++] = tstr[j]; + } + + substr[len++] = ' '; + substr[len] = '\0'; + + + /* Render the word for display... */ + + + if (only_uppercase) + { + char *uppercase_substr = uppercase(substr); + + text = render_text(myfont, uppercase_substr, color); + free(uppercase_substr); + } + else + text = render_text(myfont, substr, color); + + + /* If it won't fit on this line, move to the next! */ + + if (x + text->w > right) /* Correct? */ + { + if (need_right_to_left && want_right_to_left) + anti_carriage_return(left, right, top, top + text->h, y + text->h, + x - left); + + x = left; + y = y + text->h; + } + + + /* Draw the word: */ + + dest.x = x; + + if (need_right_to_left && want_right_to_left) + dest.y = top; + else + dest.y = y; + + SDL_BlitSurface(text, NULL, screen, &dest); + + + /* Move the cursor one word's worth: */ + + x = x + text->w; + + + /* Free the temp. surface: */ + + last_text_height = text->h; + SDL_FreeSurface(text); + + + /* Now on to the next word... */ + + i = j; + } + + free(tstr); + } + /* Right-justify the final line of text, in right-to-left mode: */ if (need_right_to_left && want_right_to_left && last_text_height > 0) - { - src.x = left; - src.y = top; - src.w = x - left; - src.h = last_text_height; + { + src.x = left; + src.y = top; + src.w = x - left; + src.h = last_text_height; - dest.x = right - src.w; - dest.y = top; + dest.x = right - src.w; + dest.y = top; - SDL_BlitSurface(screen, &src, screen, &dest); + SDL_BlitSurface(screen, &src, screen, &dest); - dest.x = left; - dest.y = top; - dest.w = (right - left - src.w); - dest.h = last_text_height; + dest.x = left; + dest.y = top; + dest.w = (right - left - src.w); + dest.h = last_text_height; - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 255, 255, 255)); - } + SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 255, 255, 255)); + } #endif } @@ -12698,15 +13491,15 @@ static void playstampdesc(int chan) static SDL_Event playsound_event; if (chan == 2) /* Only do this when the channel playing the stamp sfx has ended! */ - { - debug("Stamp SFX ended. Pushing event to play description sound..."); + { + debug("Stamp SFX ended. Pushing event to play description sound..."); - playsound_event.type = SDL_USEREVENT; - playsound_event.user.code = USEREVENT_PLAYDESCSOUND; - playsound_event.user.data1 = (void *)(intptr_t) cur_stamp[stamp_group]; //EP added (intptr_t) to avoid warning on x64 + playsound_event.type = SDL_USEREVENT; + playsound_event.user.code = USEREVENT_PLAYDESCSOUND; + playsound_event.user.data1 = (void *) (intptr_t) cur_stamp[stamp_group]; //EP added (intptr_t) to avoid warning on x64 - SDL_PushEvent(&playsound_event); - } + SDL_PushEvent(&playsound_event); + } } #endif @@ -12727,87 +13520,89 @@ static Mix_Chunk *loadsound_extra(const char *const fname, const char *extra) if (strcasestr(fname, ".png") != NULL) - { - strcpy(ext, ".png"); /* char[5] size is sufficient */ - } + { + strcpy(ext, ".png"); /* char[5] size is sufficient */ + } else - { - /* Sorry, we only handle images */ + { + /* Sorry, we only handle images */ - return (NULL); - } + return (NULL); + } /* First, check for localized version of sound: */ snd_fname = malloc(strlen(fname) + strlen(lang_prefix) + 16); - strcpy(snd_fname, fname); /* malloc'd size should be sufficient */ + strcpy(snd_fname, fname); /* malloc'd size should be sufficient */ safe_snprintf(tmp_str, sizeof(tmp_str), "%s_%s.ogg", extra, lang_prefix); - strcpy((char *)strcasestr(snd_fname, ext), tmp_str); /* FIXME: Use strncpy() (ugh, complicated) */ + strcpy((char *) strcasestr(snd_fname, ext), tmp_str); /* FIXME: Use strncpy() (ugh, complicated) */ debug(snd_fname); tmp_snd = Mix_LoadWAV(snd_fname); if (tmp_snd == NULL) + { + debug("...No local version of sound (OGG)!"); + + strcpy(snd_fname, fname); /* malloc'd size should be sufficient */ + safe_snprintf(tmp_str, sizeof(tmp_str), "%s_%s.wav", extra, lang_prefix); + strcpy((char *) strcasestr(snd_fname, ext), tmp_str); /* FIXME: Use strncpy() (ugh, complicated) */ + debug(snd_fname); + tmp_snd = Mix_LoadWAV(snd_fname); + + if (tmp_snd == NULL) { - debug("...No local version of sound (OGG)!"); + debug("...No local version of sound (WAV)!"); + + /* Check for non-country-code locale */ strcpy(snd_fname, fname); /* malloc'd size should be sufficient */ - safe_snprintf(tmp_str, sizeof(tmp_str), "%s_%s.wav", extra, lang_prefix); - strcpy((char *)strcasestr(snd_fname, ext), tmp_str); /* FIXME: Use strncpy() (ugh, complicated) */ + safe_snprintf(tmp_str, sizeof(tmp_str), "%s_%s.ogg", extra, + short_lang_prefix); + strcpy((char *) strcasestr(snd_fname, ext), tmp_str); /* FIXME: Use strncpy() (ugh, complicated) */ debug(snd_fname); tmp_snd = Mix_LoadWAV(snd_fname); if (tmp_snd == NULL) + { + debug("...No short local version of sound (OGG)!"); + + strcpy(snd_fname, fname); /* malloc'd size should be sufficient */ + safe_snprintf(tmp_str, sizeof(tmp_str), "%s_%s.wav", extra, + short_lang_prefix); + strcpy((char *) strcasestr(snd_fname, ext), tmp_str); /* FIXME: Use strncpy() (ugh, complicated) */ + debug(snd_fname); + tmp_snd = Mix_LoadWAV(snd_fname); + + if (tmp_snd == NULL) { - debug("...No local version of sound (WAV)!"); + /* Now, check for default sound: */ - /* Check for non-country-code locale */ + debug("...No short local version of sound (WAV)!"); - strcpy(snd_fname, fname); /* malloc'd size should be sufficient */ - safe_snprintf(tmp_str, sizeof(tmp_str), "%s_%s.ogg", extra, short_lang_prefix); - strcpy((char *)strcasestr(snd_fname, ext), tmp_str); /* FIXME: Use strncpy() (ugh, complicated) */ + strcpy(snd_fname, fname); /* malloc'd size should be sufficient */ + safe_snprintf(tmp_str, sizeof(tmp_str), "%s.ogg", extra); + strcpy((char *) strcasestr(snd_fname, ext), tmp_str); /* FIXME: Use strncpy() (ugh, complicated) */ debug(snd_fname); tmp_snd = Mix_LoadWAV(snd_fname); if (tmp_snd == NULL) - { - debug("...No short local version of sound (OGG)!"); + { + debug("...No default version of sound (OGG)!"); - strcpy(snd_fname, fname); /* malloc'd size should be sufficient */ - safe_snprintf(tmp_str, sizeof(tmp_str), "%s_%s.wav", extra, short_lang_prefix); - strcpy((char *)strcasestr(snd_fname, ext), tmp_str); /* FIXME: Use strncpy() (ugh, complicated) */ - debug(snd_fname); - tmp_snd = Mix_LoadWAV(snd_fname); + strcpy(snd_fname, fname); /* malloc'd size should be sufficient */ + safe_snprintf(tmp_str, sizeof(tmp_str), "%s.wav", extra); + strcpy((char *) strcasestr(snd_fname, ext), tmp_str); /* FIXME: Use strncpy() (ugh, complicated) */ + debug(snd_fname); + tmp_snd = Mix_LoadWAV(snd_fname); - if (tmp_snd == NULL) - { - /* Now, check for default sound: */ - - debug("...No short local version of sound (WAV)!"); - - strcpy(snd_fname, fname); /* malloc'd size should be sufficient */ - safe_snprintf(tmp_str, sizeof(tmp_str), "%s.ogg", extra); - strcpy((char *)strcasestr(snd_fname, ext), tmp_str); /* FIXME: Use strncpy() (ugh, complicated) */ - debug(snd_fname); - tmp_snd = Mix_LoadWAV(snd_fname); - - if (tmp_snd == NULL) - { - debug("...No default version of sound (OGG)!"); - - strcpy(snd_fname, fname); /* malloc'd size should be sufficient */ - safe_snprintf(tmp_str, sizeof(tmp_str), "%s.wav", extra); - strcpy((char *)strcasestr(snd_fname, ext), tmp_str); /* FIXME: Use strncpy() (ugh, complicated) */ - debug(snd_fname); - tmp_snd = Mix_LoadWAV(snd_fname); - - if (tmp_snd == NULL) - debug("...No default version of sound (WAV)!"); - } - } - } + if (tmp_snd == NULL) + debug("...No default version of sound (WAV)!"); + } } + } } + } free(snd_fname); return (tmp_snd); @@ -12843,11 +13638,11 @@ static void strip_trailing_whitespace(char *buf) unsigned i = strlen(buf); while (i--) - { - if (!isspace(buf[i])) - break; - buf[i] = '\0'; - } + { + if (!isspace(buf[i])) + break; + buf[i] = '\0'; + } } @@ -12875,95 +13670,99 @@ static char *loaddesc(const char *const fname, Uint8 * locale_text) #endif if (extptr != NULL) + { + found = 0; + strcpy(def_buf, ""); /* In case of total inability to find anything */ + + /* Set the first available language */ + for (i = 0; i < num_wished_langs && !found; i++) { - found = 0; - strcpy(def_buf, ""); /* In case of total inability to find anything */ + strcpy((char *) extptr, ".txt"); /* safe; pointing into a safe spot within an existing string (txt_fname) */ + fi = fopen(txt_fname, "r"); + if (!fi) + return NULL; - /* Set the first available language */ - for (i = 0; i < num_wished_langs && !found; i++) + got_first = 0; + strcpy(def_buf, ""); + + do + { + if (fgets(buf, sizeof(buf), fi)) { - strcpy((char *)extptr, ".txt"); /* safe; pointing into a safe spot within an existing string (txt_fname) */ - fi = fopen(txt_fname, "r"); - if (!fi) - return NULL; + if (!feof(fi)) + { + strip_trailing_whitespace(buf); - got_first = 0; - strcpy(def_buf, ""); - - do + if (!got_first) { - if (fgets(buf, sizeof(buf), fi)) - { - if (!feof(fi)) - { - strip_trailing_whitespace(buf); + /* First one is the default: */ - if (!got_first) - { - /* First one is the default: */ - - strcpy(def_buf, buf); /* safe; both the same size */ - got_first = 1; - } - - debug(buf); - - - // lang_prefix = lang_prefixes[langint]; - /* See if it's the one for this locale... */ - - if ((char *)strcasestr(buf, wished_langs[i].lang_prefix) == buf) - { - - debug(buf + strlen(wished_langs[i].lang_prefix)); - if ((char *)strcasestr(buf + strlen(wished_langs[i].lang_prefix), ".utf8=") == - buf + strlen(wished_langs[i].lang_prefix)) - { - lang_prefix = wished_langs[i].lang_prefix; - short_lang_prefix = strdup(lang_prefix); - /* When in doubt, cut off country code */ - if (strchr(short_lang_prefix, '_')) - *strchr(short_lang_prefix, '_') = '\0'; - - need_own_font = wished_langs[i].need_own_font; - need_right_to_left = wished_langs[i].need_right_to_left; - need_right_to_left_word = wished_langs[i].need_right_to_left_word; - - found = 1; - - debug("...FOUND!"); - } - } - } - } + strcpy(def_buf, buf); /* safe; both the same size */ + got_first = 1; } - while (!feof(fi) && !found); - fclose(fi); + debug(buf); + + // lang_prefix = lang_prefixes[langint]; + /* See if it's the one for this locale... */ + + if ((char *) strcasestr(buf, wished_langs[i].lang_prefix) == buf) + { + + debug(buf + strlen(wished_langs[i].lang_prefix)); + if ((char *) + strcasestr(buf + strlen(wished_langs[i].lang_prefix), + ".utf8=") == + buf + strlen(wished_langs[i].lang_prefix)) + { + lang_prefix = wished_langs[i].lang_prefix; + short_lang_prefix = strdup(lang_prefix); + /* When in doubt, cut off country code */ + if (strchr(short_lang_prefix, '_')) + *strchr(short_lang_prefix, '_') = '\0'; + + need_own_font = wished_langs[i].need_own_font; + need_right_to_left = wished_langs[i].need_right_to_left; + need_right_to_left_word = + wished_langs[i].need_right_to_left_word; + + found = 1; + + debug("...FOUND!"); + } + } + } } + } + while (!feof(fi) && !found); - free(txt_fname); + fclose(fi); - - /* Return the string: */ - - if (found) - { - *locale_text = 1; - return (strdup(buf + (strlen(lang_prefix)) + 6)); - } - else - { - /* No locale-specific translation; use the default (English) */ - return (strdup(def_buf)); - } } - else + + free(txt_fname); + + + /* Return the string: */ + + if (found) { - fprintf(stderr, "Somehow, '%s' doesn't have a filename extension!?\n", fname); - return NULL; + *locale_text = 1; + return (strdup(buf + (strlen(lang_prefix)) + 6)); } + else + { + /* No locale-specific translation; use the default (English) */ + return (strdup(def_buf)); + } + } + else + { + fprintf(stderr, "Somehow, '%s' doesn't have a filename extension!?\n", + fname); + return NULL; + } } @@ -12988,91 +13787,95 @@ static double loadinfo(const char *const fname, stamp_type * inf) return ratio; do + { + if (fgets(buf, sizeof buf, fi)) { - if (fgets(buf, sizeof buf, fi)) + if (!feof(fi)) + { + strip_trailing_whitespace(buf); + + if (strcmp(buf, "colorable") == 0) + inf->colorable = 1; + else if (strcmp(buf, "tintable") == 0) + inf->tintable = 1; + else if (!memcmp(buf, "scale", 5) + && (isspace(buf[5]) || buf[5] == '=')) { - if (!feof(fi)) - { - strip_trailing_whitespace(buf); + double tmp, tmp2; + char *cp = buf + 6; - if (strcmp(buf, "colorable") == 0) - inf->colorable = 1; - else if (strcmp(buf, "tintable") == 0) - inf->tintable = 1; - else if (!memcmp(buf, "scale", 5) && (isspace(buf[5]) || buf[5] == '=')) - { - double tmp, tmp2; - char *cp = buf + 6; - - while (isspace(*cp) || *cp == '=') - cp++; - if (strchr(cp, '%')) - { - tmp = strtod(cp, NULL) / 100.0; - if (tmp > 0.0001 && tmp < 10000.0) - ratio = tmp; - } - else if (strchr(cp, '/')) - { - tmp = strtod(cp, &cp); - while (*cp && !isdigit(*cp)) - cp++; - tmp2 = strtod(cp, NULL); - if (tmp > 0.0001 && tmp < 10000.0 && tmp2 > 0.0001 && tmp2 < 10000.0 - && tmp / tmp2 > 0.0001 && tmp / tmp2 < 10000.0) - ratio = tmp / tmp2; - } - else if (strchr(cp, ':')) - { - tmp = strtod(cp, &cp); - while (*cp && !isdigit(*cp)) - cp++; - tmp2 = strtod(cp, NULL); - if (tmp > 0.0001 && tmp < 10000.0 && - tmp2 > 0.0001 && tmp2 < 10000.0 && tmp2 / tmp > 0.0001 && tmp2 / tmp < 10000.0) - ratio = tmp2 / tmp; - } - else - { - tmp = strtod(cp, NULL); - if (tmp > 0.0001 && tmp < 10000.0) - ratio = 1.0 / tmp; - } - } - else if (!memcmp(buf, "tinter", 6) && (isspace(buf[6]) || buf[6] == '=')) - { - char *cp = buf + 7; - - while (isspace(*cp) || *cp == '=') - cp++; - if (!strcmp(cp, "anyhue")) - { - inf->tinter = TINTER_ANYHUE; - } - else if (!strcmp(cp, "narrow")) - { - inf->tinter = TINTER_NARROW; - } - else if (!strcmp(cp, "normal")) - { - inf->tinter = TINTER_NORMAL; - } - else if (!strcmp(cp, "vector")) - { - inf->tinter = TINTER_VECTOR; - } - else - { - debug(cp); - } - } - else if (strcmp(buf, "nomirror") == 0) - inf->mirrorable = 0; - else if (strcmp(buf, "noflip") == 0) - inf->flipable = 0; - } + while (isspace(*cp) || *cp == '=') + cp++; + if (strchr(cp, '%')) + { + tmp = strtod(cp, NULL) / 100.0; + if (tmp > 0.0001 && tmp < 10000.0) + ratio = tmp; + } + else if (strchr(cp, '/')) + { + tmp = strtod(cp, &cp); + while (*cp && !isdigit(*cp)) + cp++; + tmp2 = strtod(cp, NULL); + if (tmp > 0.0001 && tmp < 10000.0 && tmp2 > 0.0001 + && tmp2 < 10000.0 && tmp / tmp2 > 0.0001 + && tmp / tmp2 < 10000.0) + ratio = tmp / tmp2; + } + else if (strchr(cp, ':')) + { + tmp = strtod(cp, &cp); + while (*cp && !isdigit(*cp)) + cp++; + tmp2 = strtod(cp, NULL); + if (tmp > 0.0001 && tmp < 10000.0 && + tmp2 > 0.0001 && tmp2 < 10000.0 && tmp2 / tmp > 0.0001 + && tmp2 / tmp < 10000.0) + ratio = tmp2 / tmp; + } + else + { + tmp = strtod(cp, NULL); + if (tmp > 0.0001 && tmp < 10000.0) + ratio = 1.0 / tmp; + } } + else if (!memcmp(buf, "tinter", 6) + && (isspace(buf[6]) || buf[6] == '=')) + { + char *cp = buf + 7; + + while (isspace(*cp) || *cp == '=') + cp++; + if (!strcmp(cp, "anyhue")) + { + inf->tinter = TINTER_ANYHUE; + } + else if (!strcmp(cp, "narrow")) + { + inf->tinter = TINTER_NARROW; + } + else if (!strcmp(cp, "normal")) + { + inf->tinter = TINTER_NORMAL; + } + else if (!strcmp(cp, "vector")) + { + inf->tinter = TINTER_VECTOR; + } + else + { + debug(cp); + } + } + else if (strcmp(buf, "nomirror") == 0) + inf->mirrorable = 0; + else if (strcmp(buf, "noflip") == 0) + inf->flipable = 0; + } } + } while (!feof(fi)); fclose(fi); @@ -13083,7 +13886,8 @@ static double loadinfo(const char *const fname, stamp_type * inf) /** * FIXME */ -static int SDLCALL NondefectiveBlit(SDL_Surface * src, SDL_Rect * srcrect, SDL_Surface * dst, SDL_Rect * dstrect) +static int SDLCALL NondefectiveBlit(SDL_Surface * src, SDL_Rect * srcrect, + SDL_Surface * dst, SDL_Rect * dstrect) { int dstx = 0; int dsty = 0; @@ -13092,54 +13896,57 @@ static int SDLCALL NondefectiveBlit(SDL_Surface * src, SDL_Rect * srcrect, SDL_S int srcw = src->w; int srch = src->h; - Uint32(*getpixel) (SDL_Surface *, int, int) = getpixels[src->format->BytesPerPixel]; - void (*putpixel) (SDL_Surface *, int, int, Uint32) = putpixels[dst->format->BytesPerPixel]; + Uint32(*getpixel) (SDL_Surface *, int, int) = + getpixels[src->format->BytesPerPixel]; + void (*putpixel)(SDL_Surface *, int, int, Uint32) = + putpixels[dst->format->BytesPerPixel]; if (srcrect) - { - srcx = srcrect->x; - srcy = srcrect->y; - srcw = srcrect->w; - srch = srcrect->h; - } + { + srcx = srcrect->x; + srcy = srcrect->y; + srcw = srcrect->w; + srch = srcrect->h; + } if (dstrect) - { - dstx = dstrect->x; - dsty = dstrect->y; - } + { + dstx = dstrect->x; + dsty = dstrect->y; + } if (dsty < 0) - { - srcy += -dsty; - srch -= -dsty; - dsty = 0; - } + { + srcy += -dsty; + srch -= -dsty; + dsty = 0; + } if (dstx < 0) - { - srcx += -dstx; - srcw -= -dstx; - dstx = 0; - } + { + srcx += -dstx; + srcw -= -dstx; + dstx = 0; + } if (dstx + srcw > dst->w - 1) - { - srcw -= (dstx + srcw) - (dst->w - 1); - } + { + srcw -= (dstx + srcw) - (dst->w - 1); + } if (dsty + srch > dst->h - 1) - { - srch -= (dsty + srch) - (dst->h - 1); - } + { + srch -= (dsty + srch) - (dst->h - 1); + } if (srcw < 1 || srch < 1) return -1; /* no idea what to return if nothing done */ srch++; /* "++" is a tweak to get to edges -bjk 2009.10.11 */ while (srch--) - { - int i = srcw + 1; /* "+ 1" is a tweak to get to edges -bjk 2009.10.11 */ + { + int i = srcw + 1; /* "+ 1" is a tweak to get to edges -bjk 2009.10.11 */ - while (i--) - { - putpixel(dst, i + dstx, srch + dsty, getpixel(src, i + srcx, srch + srcy)); - } + while (i--) + { + putpixel(dst, i + dstx, srch + dsty, + getpixel(src, i + srcx, srch + srcy)); } + } return (0); } @@ -13155,7 +13962,9 @@ static int SDLCALL NondefectiveBlit(SDL_Surface * src, SDL_Rect * srcrect, SDL_S */ static void autoscale_copy_smear_free(SDL_Surface * src, SDL_Surface * dst, int SDLCALL(*blit) (SDL_Surface * src, - SDL_Rect * srcrect, SDL_Surface * dst, SDL_Rect * dstrect)) + SDL_Rect * srcrect, + SDL_Surface * dst, + SDL_Rect * dstrect)) { SDL_Surface *src1; SDL_Rect dest; @@ -13165,66 +13974,66 @@ static void autoscale_copy_smear_free(SDL_Surface * src, SDL_Surface * dst, be scaled too. Keep the pixels square though, filling in the gaps via a smear. */ if (src->w != dst->w || src->h != dst->h) - { - if (src->w / (float)dst->w > src->h / (float)dst->h) - src1 = thumbnail(src, dst->w, src->h * dst->w / src->w, 0); - else - src1 = thumbnail(src, src->w * dst->h / src->h, dst->h, 0); - SDL_FreeSurface(src); - src = src1; - } + { + if (src->w / (float) dst->w > src->h / (float) dst->h) + src1 = thumbnail(src, dst->w, src->h * dst->w / src->w, 0); + else + src1 = thumbnail(src, src->w * dst->h / src->h, dst->h, 0); + SDL_FreeSurface(src); + src = src1; + } dest.x = (dst->w - src->w) / 2; dest.y = (dst->h - src->h) / 2; blit(src, NULL, dst, &dest); if (src->w != dst->w) - { - /* we know that the heights match, and src is narrower */ - SDL_Rect sour; - int i = (dst->w - src->w) / 2; + { + /* we know that the heights match, and src is narrower */ + SDL_Rect sour; + int i = (dst->w - src->w) / 2; - sour.w = 1; - sour.x = 0; - sour.h = src->h; - sour.y = 0; - while (i-- > 0) - { - dest.x = i; - blit(src, &sour, dst, &dest); - } - sour.x = src->w - 1; - i = (dst->w - src->w) / 2 + src->w - 1; - while (++i < dst->w) - { - dest.x = i; - blit(src, &sour, dst, &dest); - } + sour.w = 1; + sour.x = 0; + sour.h = src->h; + sour.y = 0; + while (i-- > 0) + { + dest.x = i; + blit(src, &sour, dst, &dest); } + sour.x = src->w - 1; + i = (dst->w - src->w) / 2 + src->w - 1; + while (++i < dst->w) + { + dest.x = i; + blit(src, &sour, dst, &dest); + } + } if (src->h != dst->h) - { - /* we know that the widths match, and src is shorter */ - SDL_Rect sour; - int i = (dst->h - src->h) / 2; + { + /* we know that the widths match, and src is shorter */ + SDL_Rect sour; + int i = (dst->h - src->h) / 2; - sour.w = src->w; - sour.x = 0; - sour.h = 1; - sour.y = 0; - while (i-- > 0) - { - dest.y = i; - blit(src, &sour, dst, &dest); - } - sour.y = src->h - 1; - i = (dst->h - src->h) / 2 + src->h - 1; - while (++i < dst->h) - { - dest.y = i; - blit(src, &sour, dst, &dest); - } + sour.w = src->w; + sour.x = 0; + sour.h = 1; + sour.y = 0; + while (i-- > 0) + { + dest.y = i; + blit(src, &sour, dst, &dest); } + sour.y = src->h - 1; + i = (dst->h - src->h) / 2 + src->h - 1; + while (++i < dst->h) + { + dest.y = i; + blit(src, &sour, dst, &dest); + } + } SDL_FreeSurface(src); } @@ -13239,18 +14048,18 @@ static void load_starter_id(char *saved_id, FILE * fil) char fname[FILENAME_MAX]; FILE *fi; char color_tag; - int r, g, b, __attribute__((unused))tmp; + int r, g, b, __attribute__((unused)) tmp; char * __attribute__((unused)) tmp_ptr; rname = NULL; if (saved_id != NULL) - { - safe_snprintf(fname, sizeof(fname), "saved/%s.dat", saved_id); - rname = get_fname(fname, DIR_SAVE); + { + safe_snprintf(fname, sizeof(fname), "saved/%s.dat", saved_id); + rname = get_fname(fname, DIR_SAVE); - fi = fopen(rname, "r"); - } + fi = fopen(rname, "r"); + } else fi = fil; @@ -13258,66 +14067,67 @@ static void load_starter_id(char *saved_id, FILE * fil) template_id[0] = '\0'; if (fi != NULL) + { + if (fgets(starter_id, sizeof(starter_id), fi)) { - if (fgets(starter_id, sizeof(starter_id), fi)) - { - starter_id[strlen(starter_id) - 1] = '\0'; + starter_id[strlen(starter_id) - 1] = '\0'; - tmp = fscanf(fi, "%d", &starter_mirrored); - tmp = fscanf(fi, "%d", &starter_flipped); - tmp = fscanf(fi, "%d", &starter_personal); + tmp = fscanf(fi, "%d", &starter_mirrored); + tmp = fscanf(fi, "%d", &starter_flipped); + tmp = fscanf(fi, "%d", &starter_personal); - do - { - color_tag = fgetc(fi); - } - while ((color_tag == '\n' || color_tag == '\r') && !feof(fi)); + do + { + color_tag = fgetc(fi); + } + while ((color_tag == '\n' || color_tag == '\r') && !feof(fi)); - if (!feof(fi) && color_tag == 'c') - { - tmp = fscanf(fi, "%d", &r); - tmp = fscanf(fi, "%d", &g); - tmp = fscanf(fi, "%d", &b); + if (!feof(fi) && color_tag == 'c') + { + tmp = fscanf(fi, "%d", &r); + tmp = fscanf(fi, "%d", &g); + tmp = fscanf(fi, "%d", &b); - canvas_color_r = (Uint8) r; - canvas_color_g = (Uint8) g; - canvas_color_b = (Uint8) b; - } - else - { - canvas_color_r = 255; - canvas_color_g = 255; - canvas_color_b = 255; - } + canvas_color_r = (Uint8) r; + canvas_color_g = (Uint8) g; + canvas_color_b = (Uint8) b; + } + else + { + canvas_color_r = 255; + canvas_color_g = 255; + canvas_color_b = 255; + } - do - { - color_tag = fgetc(fi); - } - while ((color_tag == '\n' || color_tag == '\r') && !feof(fi)); + do + { + color_tag = fgetc(fi); + } + while ((color_tag == '\n' || color_tag == '\r') && !feof(fi)); - if (!feof(fi) && color_tag == 'T') - { - tmp_ptr = fgets(template_id, sizeof(template_id), fi); - template_id[strlen(template_id) - 1] = '\0'; - tmp = fscanf(fi, "%d", &template_personal); + if (!feof(fi) && color_tag == 'T') + { + tmp_ptr = fgets(template_id, sizeof(template_id), fi); + template_id[strlen(template_id) - 1] = '\0'; + tmp = fscanf(fi, "%d", &template_personal); #ifdef DEBUG - printf("template = %s\n (Personal=%d)", template_id, template_personal); + printf("template = %s\n (Personal=%d)", template_id, + template_personal); #endif - } - if (!feof(fi) && color_tag == 'M') - { - starter_modified = fgetc(fi); - } - } - fclose(fi); + } + if (!feof(fi) && color_tag == 'M') + { + starter_modified = fgetc(fi); + } } + fclose(fi); + } else - { - canvas_color_r = 255; - canvas_color_g = 255; - canvas_color_b = 255; - } + { + canvas_color_r = 255; + canvas_color_g = 255; + canvas_color_b = 255; + } if (saved_id != NULL) free(rname); @@ -13327,7 +14137,10 @@ static void load_starter_id(char *saved_id, FILE * fil) /** * FIXME */ -static SDL_Surface *load_starter_helper(char *path_and_basename, const char *extension, SDL_Surface * (*load_func) (const char *)) +static SDL_Surface *load_starter_helper(char *path_and_basename, + const char *extension, + SDL_Surface * + (*load_func) (const char *)) { char *ext; char fname[256]; @@ -13340,14 +14153,14 @@ static SDL_Surface *load_starter_helper(char *path_and_basename, const char *ext surf = (*load_func) (fname); if (surf == NULL) + { + for (i = 0; i < strlen(ext); i++) { - for (i = 0; i < strlen(ext); i++) - { - ext[i] = toupper(ext[i]); - } - safe_snprintf(fname, sizeof(fname), "%s.%s", path_and_basename, ext); - surf = (*load_func) (fname); + ext[i] = toupper(ext[i]); } + safe_snprintf(fname, sizeof(fname), "%s.%s", path_and_basename, ext); + surf = (*load_func) (fname); + } free(ext); @@ -13380,36 +14193,36 @@ static void load_starter(char *img_id) #ifndef NOSVG if (tmp_surf == NULL) - { - /* Try loading an SVG */ + { + /* Try loading an SVG */ - safe_snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); - tmp_surf = load_starter_helper(fname, "svg", &load_svg); - } + safe_snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); + tmp_surf = load_starter_helper(fname, "svg", &load_svg); + } #endif if (tmp_surf == NULL) - { - safe_snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); - tmp_surf = load_starter_helper(fname, "png", &IMG_Load); - } + { + safe_snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); + tmp_surf = load_starter_helper(fname, "png", &IMG_Load); + } if (tmp_surf == NULL) - { - /* Try loading a KPX */ - safe_snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); - tmp_surf = load_starter_helper(fname, "kpx", &myIMG_Load); - } + { + /* Try loading a KPX */ + safe_snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); + tmp_surf = load_starter_helper(fname, "kpx", &myIMG_Load); + } if (tmp_surf != NULL) - { - img_starter = SDL_DisplayFormatAlpha(tmp_surf); - //SDL_SetSurfaceAlphaMod(img_starter, SDL_ALPHA_OPAQUE); - //SDL_SetSurfaceBlendMode(img_starter, SDL_BLENDMODE_BLEND); - printf("QQQQ\n"); - SDL_FreeSurface(tmp_surf); - } + { + img_starter = SDL_DisplayFormatAlpha(tmp_surf); + //SDL_SetSurfaceAlphaMod(img_starter, SDL_ALPHA_OPAQUE); + //SDL_SetSurfaceBlendMode(img_starter, SDL_BLENDMODE_BLEND); + printf("QQQQ\n"); + SDL_FreeSurface(tmp_surf); + } /* Try to load the a background image: */ @@ -13418,38 +14231,38 @@ static void load_starter(char *img_id) #ifndef NOSVG /* (Try SVG) */ if (tmp_surf == NULL) - { - safe_snprintf(fname, sizeof(fname), "%s/%s-back", dirname, img_id); - tmp_surf = load_starter_helper(fname, "svg", &load_svg); - } + { + safe_snprintf(fname, sizeof(fname), "%s/%s-back", dirname, img_id); + tmp_surf = load_starter_helper(fname, "svg", &load_svg); + } #endif /* (JPEG) */ if (tmp_surf == NULL) - { - safe_snprintf(fname, sizeof(fname), "%s/%s-back", dirname, img_id); - tmp_surf = load_starter_helper(fname, "jpeg", &IMG_Load); - } + { + safe_snprintf(fname, sizeof(fname), "%s/%s-back", dirname, img_id); + tmp_surf = load_starter_helper(fname, "jpeg", &IMG_Load); + } if (tmp_surf == NULL) - { - /* (Then just JPG) */ - safe_snprintf(fname, sizeof(fname), "%s/%s-back", dirname, img_id); - tmp_surf = load_starter_helper(fname, "jpg", &IMG_Load); - } + { + /* (Then just JPG) */ + safe_snprintf(fname, sizeof(fname), "%s/%s-back", dirname, img_id); + tmp_surf = load_starter_helper(fname, "jpg", &IMG_Load); + } /* (Failed? Try PNG next) */ if (tmp_surf == NULL) - { - safe_snprintf(fname, sizeof(fname), "%s/%s-back", dirname, img_id); - tmp_surf = load_starter_helper(fname, "png", &IMG_Load); - } + { + safe_snprintf(fname, sizeof(fname), "%s/%s-back", dirname, img_id); + tmp_surf = load_starter_helper(fname, "png", &IMG_Load); + } if (tmp_surf != NULL) - { - img_starter_bkgd = SDL_DisplayFormat(tmp_surf); - SDL_FreeSurface(tmp_surf); - } + { + img_starter_bkgd = SDL_DisplayFormat(tmp_surf); + SDL_FreeSurface(tmp_surf); + } /* If no background, let's try to remove all white @@ -13457,88 +14270,97 @@ static void load_starter(char *img_id) transparency, if they're simple black-and-white outlines */ if (img_starter != NULL && img_starter_bkgd == NULL) + { + int x, y; + + Uint32(*getpixel) (SDL_Surface *, int, int) = + getpixels[img_starter->format->BytesPerPixel]; + void (*putpixel)(SDL_Surface *, int, int, Uint32) = + putpixels[img_starter->format->BytesPerPixel]; + Uint32 p; + Uint8 r, g, b, a; + int any_transparency; + + any_transparency = 0; + + for (y = 0; y < img_starter->h && !any_transparency; y++) { - int x, y; + for (x = 0; x < img_starter->w && !any_transparency; x++) + { + p = getpixel(img_starter, x, y); + SDL_GetRGBA(p, img_starter->format, &r, &g, &b, &a); - Uint32(*getpixel) (SDL_Surface *, int, int) = getpixels[img_starter->format->BytesPerPixel]; - void (*putpixel) (SDL_Surface *, int, int, Uint32) = putpixels[img_starter->format->BytesPerPixel]; - Uint32 p; - Uint8 r, g, b, a; - int any_transparency; - - any_transparency = 0; - - for (y = 0; y < img_starter->h && !any_transparency; y++) - { - for (x = 0; x < img_starter->w && !any_transparency; x++) - { - p = getpixel(img_starter, x, y); - SDL_GetRGBA(p, img_starter->format, &r, &g, &b, &a); - - if (a < 255) - any_transparency = 1; - } - } - - if (!any_transparency) - { - /* No transparency found! We MUST check for white pixels to save - the day! */ - - for (y = 0; y < img_starter->h; y++) - { - for (x = 0; x < img_starter->w; x++) - { - p = getpixel(img_starter, x, y); - SDL_GetRGBA(p, img_starter->format, &r, &g, &b, &a); - - if (abs(r - g) < 16 && abs(r - b) < 16 && abs(b - g) < 16) - { - a = 255 - ((r + g + b) / 3); - } - - p = SDL_MapRGBA(img_starter->format, r, g, b, a); - putpixel(img_starter, x, y, p); - } - } - } + if (a < 255) + any_transparency = 1; + } } + if (!any_transparency) + { + /* No transparency found! We MUST check for white pixels to save + the day! */ + + for (y = 0; y < img_starter->h; y++) + { + for (x = 0; x < img_starter->w; x++) + { + p = getpixel(img_starter, x, y); + SDL_GetRGBA(p, img_starter->format, &r, &g, &b, &a); + + if (abs(r - g) < 16 && abs(r - b) < 16 && abs(b - g) < 16) + { + a = 255 - ((r + g + b) / 3); + } + + p = SDL_MapRGBA(img_starter->format, r, g, b, a); + putpixel(img_starter, x, y, p); + } + } + } + } + /* Scale if needed... */ - if (img_starter != NULL && (img_starter->w != canvas->w || img_starter->h != canvas->h)) - { - tmp_surf = img_starter; + if (img_starter != NULL + && (img_starter->w != canvas->w || img_starter->h != canvas->h)) + { + tmp_surf = img_starter; - img_starter = SDL_CreateRGBSurface(canvas->flags, - canvas->w, canvas->h, - tmp_surf->format->BitsPerPixel, - tmp_surf->format->Rmask, - tmp_surf->format->Gmask, tmp_surf->format->Bmask, tmp_surf->format->Amask); + img_starter = SDL_CreateRGBSurface(canvas->flags, + canvas->w, canvas->h, + tmp_surf->format->BitsPerPixel, + tmp_surf->format->Rmask, + tmp_surf->format->Gmask, + tmp_surf->format->Bmask, + tmp_surf->format->Amask); - /* 3rd arg ignored for RGBA surfaces */ - // SDL_SetAlpha(tmp_surf, SDL_RLEACCEL, SDL_ALPHA_OPAQUE); - //SDL_SetSurfaceBlendMode(tmp_surf, SDL_BLENDMODE_BLEND); + /* 3rd arg ignored for RGBA surfaces */ + // SDL_SetAlpha(tmp_surf, SDL_RLEACCEL, SDL_ALPHA_OPAQUE); + //SDL_SetSurfaceBlendMode(tmp_surf, SDL_BLENDMODE_BLEND); - autoscale_copy_smear_free(tmp_surf, img_starter, NondefectiveBlit); - // SDL_SetAlpha(img_starter, SDL_RLEACCEL, SDL_ALPHA_OPAQUE); - SDL_SetSurfaceBlendMode(img_starter, SDL_BLENDMODE_BLEND); + autoscale_copy_smear_free(tmp_surf, img_starter, NondefectiveBlit); + // SDL_SetAlpha(img_starter, SDL_RLEACCEL, SDL_ALPHA_OPAQUE); + SDL_SetSurfaceBlendMode(img_starter, SDL_BLENDMODE_BLEND); - } + } - if (img_starter_bkgd != NULL && (img_starter_bkgd->w != canvas->w || img_starter_bkgd->h != canvas->h)) - { - tmp_surf = img_starter_bkgd; + if (img_starter_bkgd != NULL + && (img_starter_bkgd->w != canvas->w + || img_starter_bkgd->h != canvas->h)) + { + tmp_surf = img_starter_bkgd; - img_starter_bkgd = SDL_CreateRGBSurface(SDL_SWSURFACE, - canvas->w, canvas->h, - canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, 0); + img_starter_bkgd = SDL_CreateRGBSurface(SDL_SWSURFACE, + canvas->w, canvas->h, + canvas->format->BitsPerPixel, + canvas->format->Rmask, + canvas->format->Gmask, + canvas->format->Bmask, 0); - autoscale_copy_smear_free(tmp_surf, img_starter_bkgd, SDL_BlitSurface); - } + autoscale_copy_smear_free(tmp_surf, img_starter_bkgd, SDL_BlitSurface); + } free(dirname); } @@ -13571,52 +14393,56 @@ static void load_template(char *img_id) #ifndef NOSVG /* (Failed? Try SVG next) */ if (tmp_surf == NULL) - { - safe_snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); - tmp_surf = load_starter_helper(fname, "svg", &load_svg); - } + { + safe_snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); + tmp_surf = load_starter_helper(fname, "svg", &load_svg); + } #endif /* (JPEG) */ if (tmp_surf == NULL) - { - safe_snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); - tmp_surf = load_starter_helper(fname, "jpeg", &IMG_Load); - } + { + safe_snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); + tmp_surf = load_starter_helper(fname, "jpeg", &IMG_Load); + } if (tmp_surf == NULL) - { - /* (Then just JPG) */ - safe_snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); - tmp_surf = load_starter_helper(fname, "jpg", &IMG_Load); - } + { + /* (Then just JPG) */ + safe_snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); + tmp_surf = load_starter_helper(fname, "jpg", &IMG_Load); + } /* (Failed? Try PNG next) */ if (tmp_surf == NULL) - { - safe_snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); - tmp_surf = load_starter_helper(fname, "png", &IMG_Load); - } + { + safe_snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); + tmp_surf = load_starter_helper(fname, "png", &IMG_Load); + } if (tmp_surf != NULL) - { - img_starter_bkgd = SDL_DisplayFormat(tmp_surf); - SDL_FreeSurface(tmp_surf); - } + { + img_starter_bkgd = SDL_DisplayFormat(tmp_surf); + SDL_FreeSurface(tmp_surf); + } /* Scale if needed... */ - if (img_starter_bkgd != NULL && (img_starter_bkgd->w != canvas->w || img_starter_bkgd->h != canvas->h)) - { - tmp_surf = img_starter_bkgd; + if (img_starter_bkgd != NULL + && (img_starter_bkgd->w != canvas->w + || img_starter_bkgd->h != canvas->h)) + { + tmp_surf = img_starter_bkgd; - img_starter_bkgd = SDL_CreateRGBSurface(SDL_SWSURFACE, - canvas->w, canvas->h, - canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, 0); + img_starter_bkgd = SDL_CreateRGBSurface(SDL_SWSURFACE, + canvas->w, canvas->h, + canvas->format->BitsPerPixel, + canvas->format->Rmask, + canvas->format->Gmask, + canvas->format->Bmask, 0); - autoscale_copy_smear_free(tmp_surf, img_starter_bkgd, SDL_BlitSurface); - } + autoscale_copy_smear_free(tmp_surf, img_starter_bkgd, SDL_BlitSurface); + } free(dirname); } @@ -13630,25 +14456,26 @@ static void determine_id(void) fi = fopen(fname, "r"); if (fi == NULL) - { - fprintf(stderr, - "\nWarning: Couldn't determine the current image's ID\n" - "%s\n" "The system error that occurred was:\n" "%s\n\n", fname, strerror(errno)); - file_id[0] = '\0'; - starter_id[0] = '\0'; - template_id[0] = '\0'; - } + { + fprintf(stderr, + "\nWarning: Couldn't determine the current image's ID\n" + "%s\n" "The system error that occurred was:\n" "%s\n\n", fname, + strerror(errno)); + file_id[0] = '\0'; + starter_id[0] = '\0'; + template_id[0] = '\0'; + } else + { + if (fgets(file_id, sizeof(file_id), fi)) { - if (fgets(file_id, sizeof(file_id), fi)) - { - if (strlen(file_id) > 0) - { - file_id[strlen(file_id) - 1] = '\0'; - } - } - fclose(fi); + if (strlen(file_id) > 0) + { + file_id[strlen(file_id) - 1] = '\0'; + } } + fclose(fi); + } free(fname); } @@ -13677,13 +14504,13 @@ static void load_current(void) fi = fopen(fname, "r"); if (fi != NULL) - { - snprintf(file_id, sizeof(file_id), "%s", AUTOSAVED_NAME); - file_id[strlen(file_id)] = '\0'; - found_autosaved = 1; - fclose(fi); - free(fname); - } + { + snprintf(file_id, sizeof(file_id), "%s", AUTOSAVED_NAME); + file_id[strlen(file_id)] = '\0'; + found_autosaved = 1; + fclose(fi); + free(fname); + } #endif if (!found_autosaved) @@ -13692,77 +14519,79 @@ static void load_current(void) /* Load that image: */ if (file_id[0] != '\0') + { + + start_label_node = NULL; + current_label_node = NULL; + first_label_node_in_redo_stack = NULL; + highlighted_label_node = NULL; + label_node_to_edit = NULL; + have_to_rec_label_node = FALSE; + + safe_snprintf(ftmp, sizeof(ftmp), "saved/%s%s", file_id, FNAME_EXTENSION); + fname = get_fname(ftmp, DIR_SAVE); + + tmp = myIMG_Load_RWops(fname); + + if (tmp == NULL) { + fprintf(stderr, + "\nWarning: Couldn't load any current image.\n" + "%s\n" "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", fname, SDL_GetError()); - start_label_node = NULL; - current_label_node = NULL; - first_label_node_in_redo_stack = NULL; - highlighted_label_node = NULL; - label_node_to_edit = NULL; - have_to_rec_label_node = FALSE; - - safe_snprintf(ftmp, sizeof(ftmp), "saved/%s%s", file_id, FNAME_EXTENSION); - fname = get_fname(ftmp, DIR_SAVE); - - tmp = myIMG_Load_RWops(fname); - - if (tmp == NULL) - { - fprintf(stderr, - "\nWarning: Couldn't load any current image.\n" - "%s\n" "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", fname, SDL_GetError()); - - file_id[0] = '\0'; - starter_id[0] = '\0'; - template_id[0] = '\0'; - } - else - { - org_surf = SDL_DisplayFormat(tmp); - autoscale_copy_smear_free(tmp, canvas, SDL_BlitSurface); - - /* First we run this for compatibility, then we will chek if - there are data embedded in the png file */ - load_starter_id(file_id, NULL); - if (starter_id[0] != '\0') - { - load_starter(starter_id); - - if (starter_mirrored) - mirror_starter(); - - if (starter_flipped) - flip_starter(); - } - else if (template_id[0] != '\0') - { - load_template(template_id); - } - - load_embedded_data(fname, org_surf); - } - - free(fname); + file_id[0] = '\0'; + starter_id[0] = '\0'; + template_id[0] = '\0'; } + else + { + org_surf = SDL_DisplayFormat(tmp); + autoscale_copy_smear_free(tmp, canvas, SDL_BlitSurface); + + /* First we run this for compatibility, then we will chek if + there are data embedded in the png file */ + load_starter_id(file_id, NULL); + if (starter_id[0] != '\0') + { + load_starter(starter_id); + + if (starter_mirrored) + mirror_starter(); + + if (starter_flipped) + flip_starter(); + } + else if (template_id[0] != '\0') + { + load_template(template_id); + } + + load_embedded_data(fname, org_surf); + } + + free(fname); + } if (!found_autosaved) - { - been_saved = 1; - tool_avail[TOOL_SAVE] = 0; - } + { + been_saved = 1; + tool_avail[TOOL_SAVE] = 0; + } else - { - /* Set file_id to the draw that were edited when the autosave triggered */ - determine_id(); - snprintf(ftmp, sizeof(ftmp), "saved/%s%s", AUTOSAVED_NAME, FNAME_EXTENSION); - fname = get_fname(ftmp, DIR_SAVE); - unlink(fname); - free(fname); + { + /* Set file_id to the draw that were edited when the autosave triggered */ + determine_id(); + snprintf(ftmp, sizeof(ftmp), "saved/%s%s", AUTOSAVED_NAME, + FNAME_EXTENSION); + fname = get_fname(ftmp, DIR_SAVE); + unlink(fname); + free(fname); - /* The autosaved file comes from work that was not explicitely saved */ - been_saved = 0; - tool_avail[TOOL_SAVE] = 1; - } + /* The autosaved file comes from work that was not explicitely saved */ + been_saved = 0; + tool_avail[TOOL_SAVE] = 1; + } } @@ -13778,12 +14607,13 @@ static int make_directory(int dir_type, const char *path, const char *errmsg) fname = get_fname(path, dir_type); res = mkdir(fname, 0755); if (res != 0 && errno != EEXIST) - { - fprintf(stderr, - "\nError: %s:\n" "%s\n" "The error that occurred was:\n" "%s\n\n", errmsg, fname, strerror(errno)); - free(fname); - return 0; - } + { + fprintf(stderr, + "\nError: %s:\n" "%s\n" "The error that occurred was:\n" "%s\n\n", + errmsg, fname, strerror(errno)); + free(fname); + return 0; + } free(fname); return 1; } @@ -13797,28 +14627,30 @@ static void save_current(void) char *fname; FILE *fi; - if (!make_directory(DIR_SAVE, "", "Can't create user data directory (E001)")) - { - draw_tux_text(TUX_OOPS, strerror(errno), 0); - return; - } + if (!make_directory + (DIR_SAVE, "", "Can't create user data directory (E001)")) + { + draw_tux_text(TUX_OOPS, strerror(errno), 0); + return; + } fname = get_fname("current_id.txt", DIR_SAVE); fi = fopen(fname, "w"); if (fi == NULL) - { - fprintf(stderr, - "\nError: Can't keep track of current image.\n" - "%s\n" "The error that occurred was:\n" "%s\n\n", fname, strerror(errno)); + { + fprintf(stderr, + "\nError: Can't keep track of current image.\n" + "%s\n" "The error that occurred was:\n" "%s\n\n", fname, + strerror(errno)); - draw_tux_text(TUX_OOPS, strerror(errno), 0); - } + draw_tux_text(TUX_OOPS, strerror(errno), 0); + } else - { - fprintf(fi, "%s\n", file_id); - fclose(fi); - } + { + fprintf(fi, "%s\n", file_id); + fclose(fi); + } free(fname); } @@ -13828,7 +14660,8 @@ static void save_current(void) * FIXME */ /* Prompt the user with a yes/no question: */ -static int do_prompt(const char *const text, const char *const btn_yes, const char *const btn_no, int ox, int oy) +static int do_prompt(const char *const text, const char *const btn_yes, + const char *const btn_no, int ox, int oy) { return (do_prompt_image(text, btn_yes, btn_no, NULL, NULL, NULL, ox, oy)); } @@ -13839,7 +14672,8 @@ static int do_prompt(const char *const text, const char *const btn_yes, const ch static int do_prompt_snd(const char *const text, const char *const btn_yes, const char *const btn_no, int snd, int ox, int oy) { - return (do_prompt_image_flash_snd(text, btn_yes, btn_no, NULL, NULL, NULL, 0, snd, ox, oy)); + return (do_prompt_image_flash_snd + (text, btn_yes, btn_no, NULL, NULL, NULL, 0, snd, ox, oy)); } /** @@ -13847,9 +14681,11 @@ static int do_prompt_snd(const char *const text, const char *const btn_yes, */ static int do_prompt_image(const char *const text, const char *const btn_yes, const char *const btn_no, SDL_Surface * img1, - SDL_Surface * img2, SDL_Surface * img3, int ox, int oy) + SDL_Surface * img2, SDL_Surface * img3, int ox, + int oy) { - return (do_prompt_image_snd(text, btn_yes, btn_no, img1, img2, img3, SND_NONE, ox, oy)); + return (do_prompt_image_snd + (text, btn_yes, btn_no, img1, img2, img3, SND_NONE, ox, oy)); } /** @@ -13858,9 +14694,11 @@ static int do_prompt_image(const char *const text, const char *const btn_yes, static int do_prompt_image_snd(const char *const text, const char *const btn_yes, const char *const btn_no, SDL_Surface * img1, - SDL_Surface * img2, SDL_Surface * img3, int snd, int ox, int oy) + SDL_Surface * img2, SDL_Surface * img3, + int snd, int ox, int oy) { - return (do_prompt_image_flash_snd(text, btn_yes, btn_no, img1, img2, img3, 0, snd, ox, oy)); + return (do_prompt_image_flash_snd + (text, btn_yes, btn_no, img1, img2, img3, 0, snd, ox, oy)); } /** @@ -13869,9 +14707,12 @@ static int do_prompt_image_snd(const char *const text, static int do_prompt_image_flash(const char *const text, const char *const btn_yes, const char *const btn_no, SDL_Surface * img1, - SDL_Surface * img2, SDL_Surface * img3, int animate, int ox, int oy) + SDL_Surface * img2, SDL_Surface * img3, + int animate, int ox, int oy) { - return (do_prompt_image_flash_snd(text, btn_yes, btn_no, img1, img2, img3, animate, SND_NONE, ox, oy)); + return (do_prompt_image_flash_snd + (text, btn_yes, btn_no, img1, img2, img3, animate, SND_NONE, ox, + oy)); } #define PROMPT_W (min(canvas->w, ((int) (440 * button_scale)))) @@ -13884,7 +14725,8 @@ static int do_prompt_image_flash_snd(const char *const text, const char *const btn_yes, const char *const btn_no, SDL_Surface * img1, SDL_Surface * img2, - SDL_Surface * img3, int animate, int snd, int ox, int oy) + SDL_Surface * img3, int animate, int snd, + int ox, int oy) { int oox, ooy, nx, ny; SDL_Event event; @@ -13909,15 +14751,15 @@ static int do_prompt_image_flash_snd(const char *const text, #ifdef DEBUG if (snd >= 0) - { - printf("Prompt and play sound #%d: %s\n", snd, sound_fnames[snd]); - fflush(stdout); - } + { + printf("Prompt and play sound #%d: %s\n", snd, sound_fnames[snd]); + fflush(stdout); + } else - { - printf("Prompt without sound\n"); - fflush(stdout); - } + { + printf("Prompt without sound\n"); + fflush(stdout); + } #endif val_x = val_y = motioner = 0; @@ -13947,33 +14789,37 @@ static int do_prompt_image_flash_snd(const char *const text, backup = SDL_CreateRGBSurface(screen->flags, screen->w, screen->h, screen->format->BitsPerPixel, screen->format->Rmask, - screen->format->Gmask, screen->format->Bmask, screen->format->Amask); + screen->format->Gmask, screen->format->Bmask, + screen->format->Amask); SDL_FillRect(backup, NULL, SDL_MapRGBA(backup->format, 255, 255, 255, 255)); SDL_BlitSurface(screen, NULL, backup, NULL); for (w = 0; w <= r_ttools.w; w = w + 2) - { - oox = ox - w; - ooy = oy - w; + { + oox = ox - w; + ooy = oy - w; - nx = PROMPT_LEFT + r_ttools.w - w + PROMPTOFFSETX; - ny = 2 + canvas->h / 2 - w; + nx = PROMPT_LEFT + r_ttools.w - w + PROMPTOFFSETX; + ny = 2 + canvas->h / 2 - w; - dest.x = ((nx * w) + (oox * (r_ttools.w - w))) / r_ttools.w; - dest.y = ((ny * w) + (ooy * (r_ttools.w - w))) / r_ttools.w; - dest.w = (PROMPT_W - r_ttools.w * 2) + w * 2; - dest.h = w * 2; - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 224 - (int)(w / button_scale), 224 - (int)(w / button_scale), 244 - (int)(w / button_scale))); + dest.x = ((nx * w) + (oox * (r_ttools.w - w))) / r_ttools.w; + dest.y = ((ny * w) + (ooy * (r_ttools.w - w))) / r_ttools.w; + dest.w = (PROMPT_W - r_ttools.w * 2) + w * 2; + dest.h = w * 2; + SDL_FillRect(screen, &dest, + SDL_MapRGB(screen->format, 224 - (int) (w / button_scale), + 224 - (int) (w / button_scale), + 244 - (int) (w / button_scale))); - SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); + SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); - if ((w % 8) == 0) - SDL_Delay(1); + if ((w % 8) == 0) + SDL_Delay(1); - if (w == r_ttools.w - 2) - SDL_BlitSurface(backup, NULL, screen, NULL); - } + if (w == r_ttools.w - 2) + SDL_BlitSurface(backup, NULL, screen, NULL); + } SDL_FreeSurface(backup); @@ -13986,30 +14832,35 @@ static int do_prompt_image_flash_snd(const char *const text, (w - 4) * 2, screen->format->BitsPerPixel, screen->format->Rmask, - screen->format->Gmask, screen->format->Bmask, screen->format->Amask); + screen->format->Gmask, + screen->format->Bmask, + screen->format->Amask); if (alpha_surf != NULL) + { + SDL_FillRect(alpha_surf, NULL, + SDL_MapRGBA(alpha_surf->format, 0, 0, 0, 64)); + + for (i = 8; i > 0; i = i - 2) { - SDL_FillRect(alpha_surf, NULL, SDL_MapRGBA(alpha_surf->format, 0, 0, 0, 64)); - - for (i = 8; i > 0; i = i - 2) - { - dest.x = PROMPT_LEFT + r_ttools.w - (w - 4) + i + PROMPTOFFSETX; - dest.y = 94 / button_scale + r_ttools.w / button_scale - (w - 4) + i + PROMPTOFFSETY; - dest.w = (PROMPT_W - r_ttools.w * 2) + (w - 4) * 2; - dest.h = (w - 4) * 2; - dest.y = canvas->h / 2 - dest.h / 2 + i + 2; - SDL_BlitSurface(alpha_surf, NULL, screen, &dest); - } - - SDL_FreeSurface(alpha_surf); + dest.x = PROMPT_LEFT + r_ttools.w - (w - 4) + i + PROMPTOFFSETX; + dest.y = + 94 / button_scale + r_ttools.w / button_scale - (w - 4) + i + + PROMPTOFFSETY; + dest.w = (PROMPT_W - r_ttools.w * 2) + (w - 4) * 2; + dest.h = (w - 4) * 2; + dest.y = canvas->h / 2 - dest.h / 2 + i + 2; + SDL_BlitSurface(alpha_surf, NULL, screen, &dest); } + + SDL_FreeSurface(alpha_surf); + } #endif w = w - 6; - dest_back.x = dest.x = PROMPT_LEFT + r_ttools.w - w + PROMPTOFFSETX; + dest_back.x = dest.x = PROMPT_LEFT + r_ttools.w - w + PROMPTOFFSETX; dest_back.w = dest.w = (PROMPT_W - r_ttools.w * 2) + w * 2; dest_back.h = dest.h = w * 2; dest_back.y = dest.y = canvas->h / 2 - dest.h / 2 + 2; @@ -14023,17 +14874,18 @@ static int do_prompt_image_flash_snd(const char *const text, img1b = NULL; if (img1 != NULL) + { + if (img1->h > 64 * button_scale + && img2 != NULL /* Only scale if it matters */ ) { - if (img1->h > 64 * button_scale && img2 != NULL /* Only scale if it matters */ ) - { - img1b = thumbnail(img1, 80 * button_scale, 64 * button_scale, 1); - free_img1b = 1; - } - else - { - img1b = img1; - } + img1b = thumbnail(img1, 80 * button_scale, 64 * button_scale, 1); + free_img1b = 1; } + else + { + img1b = img1; + } + } /* If we're showing any images on the right, determine the widest width @@ -14057,65 +14909,65 @@ static int do_prompt_image_flash_snd(const char *const text, /* Draw the question: */ if (need_right_to_left == 0) - { - txt_left = (PROMPT_LEFT + 6) + PROMPTOFFSETX; - txt_right = (PROMPT_LEFT + PROMPT_W - 5) + PROMPTOFFSETX - offset; - img_left = (PROMPT_LEFT + PROMPT_W - 5) + PROMPTOFFSETX - max_img_w - 4; - btn_left = (PROMPT_LEFT + 6) + PROMPTOFFSETX; - txt_btn_left = txt_left + img_yes->w + 4; - txt_btn_right = txt_right; - } + { + txt_left = (PROMPT_LEFT + 6) + PROMPTOFFSETX; + txt_right = (PROMPT_LEFT + PROMPT_W - 5) + PROMPTOFFSETX - offset; + img_left = (PROMPT_LEFT + PROMPT_W - 5) + PROMPTOFFSETX - max_img_w - 4; + btn_left = (PROMPT_LEFT + 6) + PROMPTOFFSETX; + txt_btn_left = txt_left + img_yes->w + 4; + txt_btn_right = txt_right; + } else - { - txt_left = (PROMPT_LEFT + 6) + PROMPTOFFSETX + offset; - txt_right = (PROMPT_LEFT + PROMPT_W - 5) + PROMPTOFFSETX; - img_left = (PROMPT_LEFT + 6) + PROMPTOFFSETX + 4; - btn_left = (PROMPT_LEFT + PROMPT_W - 5) + PROMPTOFFSETX - img_yes->w - 4; - txt_btn_left = txt_left; - txt_btn_right = btn_left; - } + { + txt_left = (PROMPT_LEFT + 6) + PROMPTOFFSETX + offset; + txt_right = (PROMPT_LEFT + PROMPT_W - 5) + PROMPTOFFSETX; + img_left = (PROMPT_LEFT + 6) + PROMPTOFFSETX + 4; + btn_left = (PROMPT_LEFT + PROMPT_W - 5) + PROMPTOFFSETX - img_yes->w - 4; + txt_btn_left = txt_left; + txt_btn_right = btn_left; + } - wordwrap_text(text, black, txt_left, dest.y + 2 , txt_right, 1); + wordwrap_text(text, black, txt_left, dest.y + 2, txt_right, 1); /* Draw the images (if any, and if not animated): */ - img_y = dest_back.y + 6; + img_y = dest_back.y + 6; if (img1b != NULL) - { - dest.x = img_left + (max_img_w - img1b->w) / 2; - dest.y = img_y; + { + dest.x = img_left + (max_img_w - img1b->w) / 2; + dest.y = img_y; - SDL_BlitSurface(img1b, NULL, screen, &dest); + SDL_BlitSurface(img1b, NULL, screen, &dest); - if (!animate) - img_y = img_y + img1b->h + 4; - } + if (!animate) + img_y = img_y + img1b->h + 4; + } if (!animate) + { + if (img2 != NULL) { - if (img2 != NULL) - { - dest.x = img_left + (max_img_w - img2->w) / 2; - dest.y = img_y; + dest.x = img_left + (max_img_w - img2->w) / 2; + dest.y = img_y; - SDL_BlitSurface(img2, NULL, screen, &dest); + SDL_BlitSurface(img2, NULL, screen, &dest); - img_y = img_y + img2->h + 4; - } - - if (img3 != NULL) - { - dest.x = img_left + (max_img_w - img3->w) / 2; - dest.y = img_y; - - SDL_BlitSurface(img3, NULL, screen, &dest); - - img_y = img_y + img3->h + 4; /* unnecessary */ - } + img_y = img_y + img2->h + 4; } + if (img3 != NULL) + { + dest.x = img_left + (max_img_w - img3->w) / 2; + dest.y = img_y; + + SDL_BlitSurface(img3, NULL, screen, &dest); + + img_y = img_y + img3->h + 4; /* unnecessary */ + } + } + /* Draw yes button: */ @@ -14132,13 +14984,13 @@ static int do_prompt_image_flash_snd(const char *const text, /* Draw no button: */ if (strlen(btn_no) != 0) - { - dest.x = btn_left; - dest.y = dest.y + button_h + 4; - SDL_BlitSurface(img_no, NULL, screen, &dest); + { + dest.x = btn_left; + dest.y = dest.y + button_h + 4; + SDL_BlitSurface(img_no, NULL, screen, &dest); - wordwrap_text(btn_no, black, txt_btn_left, dest.y + 5, txt_btn_right, 1); - } + wordwrap_text(btn_no, black, txt_btn_left, dest.y + 5, txt_btn_right, 1); + } /* Draw Tux, waiting... */ @@ -14152,159 +15004,172 @@ static int do_prompt_image_flash_snd(const char *const text, ans = 0; do + { + while (SDL_PollEvent(&event)) { - while (SDL_PollEvent(&event)) + if (event.type == SDL_QUIT) + { + ans = 0; + done = 1; + } + else if (event.type == SDL_WINDOWEVENT) + { + handle_active(&event); + } + else if (event.type == SDL_KEYUP) + { + key = event.key.keysym.sym; + + handle_keymouse(key, SDL_KEYUP, 24, NULL, NULL); + } + else if (event.type == SDL_KEYDOWN) + { + key = event.key.keysym.sym; + + handle_keymouse(key, SDL_KEYDOWN, 24, NULL, NULL); + + + /* FIXME: Should use SDLK_{c} instead of '{c}'? How? */ + + if (key == key_y || key == SDLK_RETURN) { - if (event.type == SDL_QUIT) + /* Y or ENTER - Yes! */ + + ans = 1; + done = 1; + } + else if (key == key_n || key == SDLK_ESCAPE || key == SDLK_AC_BACK) + { + /* N or ESCAPE - No! */ + + if (strlen(btn_no) != 0) + { + ans = 0; + done = 1; + } + else + { + if (key == SDLK_ESCAPE || key == SDLK_AC_BACK) { - ans = 0; + /* ESCAPE also simply dismisses if there's no Yes/No + choice: */ + + ans = 1; done = 1; } - else if (event.type == SDL_WINDOWEVENT) - { - handle_active(&event); - } - else if (event.type == SDL_KEYUP) - { - key = event.key.keysym.sym; - - handle_keymouse(key, SDL_KEYUP, 24, NULL, NULL); - } - else if (event.type == SDL_KEYDOWN) - { - key = event.key.keysym.sym; - - handle_keymouse(key, SDL_KEYDOWN, 24, NULL, NULL); - - - /* FIXME: Should use SDLK_{c} instead of '{c}'? How? */ - - if (key == key_y || key == SDLK_RETURN) - { - /* Y or ENTER - Yes! */ - - ans = 1; - done = 1; - } - else if (key == key_n || key == SDLK_ESCAPE || key == SDLK_AC_BACK) - { - /* N or ESCAPE - No! */ - - if (strlen(btn_no) != 0) - { - ans = 0; - done = 1; - } - else - { - if (key == SDLK_ESCAPE || key == SDLK_AC_BACK) - { - /* ESCAPE also simply dismisses if there's no Yes/No - choice: */ - - ans = 1; - done = 1; - } - } - } - } - else if (event.type == SDL_MOUSEBUTTONDOWN && valid_click(event.button.button)) - { - if (event.button.x >= btn_left && event.button.x < btn_left + img_yes->w) - { - if (event.button.y >= dest_back.y + dest_back.h - 4 - button_h - 4 - button_h && - event.button.y < dest_back.y + dest_back.h - 4 - button_h - 4 - button_h + img_yes->h) - { - ans = 1; - done = 1; - } - else if (strlen(btn_no) != 0 && - event.button.y >= dest_back.y + dest_back.h - 4 - button_h && - event.button.y < dest_back.y + dest_back.h - 4 - button_h + img_no->h) - { - ans = 0; - done = 1; - } - } - } - else if (event.type == SDL_MOUSEMOTION) - { - if (event.button.x >= btn_left && - event.button.x < btn_left + img_yes->w && - ((event.button.y >= dest_back.y + dest_back.h - 4 - button_h - 4 - button_h && - event.button.y < dest_back.y + dest_back.h - 4 - button_h - 4 - button_h + img_yes->h) || - (strlen(btn_no) != 0 && - event.button.y >= dest_back.y + dest_back.h - 4 - button_h && - event.button.y < dest_back.y + dest_back.h - 4 - button_h + img_no->h))) - { - do_setcursor(cursor_hand); - } - else - { - do_setcursor(cursor_arrow); - } - oldpos_x = event.button.x; - oldpos_y = event.button.y; - } - - else if (event.type == SDL_JOYAXISMOTION) - handle_joyaxismotion(event, &motioner, &val_x, &val_y); - - else if (event.type == SDL_JOYHATMOTION) - handle_joyhatmotion(event, oldpos_x, oldpos_y, &valhat_x, &valhat_y, &hatmotioner, &old_hat_ticks); - - - else if (event.type == SDL_JOYBALLMOTION) - handle_joyballmotion(event, oldpos_x, oldpos_y); - - else if (event.type == SDL_JOYBUTTONDOWN) - handle_joybuttonupdown(event, oldpos_x, oldpos_y); + } } - - if (motioner | hatmotioner) - handle_motioners(oldpos_x, oldpos_y, motioner, hatmotioner, old_hat_ticks, val_x, val_y, valhat_x, valhat_y); - - - SDL_Delay(10); - - if (animate) + } + else if (event.type == SDL_MOUSEBUTTONDOWN + && valid_click(event.button.button)) + { + if (event.button.x >= btn_left + && event.button.x < btn_left + img_yes->w) { - counter++; - - if (counter == 5) - { - dest.x = img_left + (max_img_w - img2->w) / 2; - dest.y = img_y; - - SDL_BlitSurface(img2, NULL, screen, &dest); - SDL_Flip(screen); - } - else if (counter == 10) - { - if (img3 != NULL) - { - dest.x = img_left + (max_img_w - img3->w) / 2; - dest.y = img_y; - - SDL_BlitSurface(img3, NULL, screen, &dest); - SDL_Flip(screen); - } - else - counter = 15; - } - - if (counter == 15) - { - dest.x = img_left + (max_img_w - img1b->w) / 2; - dest.y = img_y; - - SDL_BlitSurface(img1b, NULL, screen, &dest); - SDL_Flip(screen); - - counter = 0; - } + if (event.button.y >= + dest_back.y + dest_back.h - 4 - button_h - 4 - button_h + && event.button.y < + dest_back.y + dest_back.h - 4 - button_h - 4 - button_h + + img_yes->h) + { + ans = 1; + done = 1; + } + else if (strlen(btn_no) != 0 && + event.button.y >= dest_back.y + dest_back.h - 4 - button_h + && event.button.y < + dest_back.y + dest_back.h - 4 - button_h + img_no->h) + { + ans = 0; + done = 1; + } } + } + else if (event.type == SDL_MOUSEMOTION) + { + if (event.button.x >= btn_left && + event.button.x < btn_left + img_yes->w && + ((event.button.y >= + dest_back.y + dest_back.h - 4 - button_h - 4 - button_h + && event.button.y < + dest_back.y + dest_back.h - 4 - button_h - 4 - button_h + + img_yes->h) || (strlen(btn_no) != 0 + && event.button.y >= + dest_back.y + dest_back.h - 4 - button_h + && event.button.y < + dest_back.y + dest_back.h - 4 - button_h + + img_no->h))) + { + do_setcursor(cursor_hand); + } + else + { + do_setcursor(cursor_arrow); + } + oldpos_x = event.button.x; + oldpos_y = event.button.y; + } + + else if (event.type == SDL_JOYAXISMOTION) + handle_joyaxismotion(event, &motioner, &val_x, &val_y); + + else if (event.type == SDL_JOYHATMOTION) + handle_joyhatmotion(event, oldpos_x, oldpos_y, &valhat_x, &valhat_y, + &hatmotioner, &old_hat_ticks); + + + else if (event.type == SDL_JOYBALLMOTION) + handle_joyballmotion(event, oldpos_x, oldpos_y); + + else if (event.type == SDL_JOYBUTTONDOWN) + handle_joybuttonupdown(event, oldpos_x, oldpos_y); } + + if (motioner | hatmotioner) + handle_motioners(oldpos_x, oldpos_y, motioner, hatmotioner, + old_hat_ticks, val_x, val_y, valhat_x, valhat_y); + + + SDL_Delay(10); + + if (animate) + { + counter++; + + if (counter == 5) + { + dest.x = img_left + (max_img_w - img2->w) / 2; + dest.y = img_y; + + SDL_BlitSurface(img2, NULL, screen, &dest); + SDL_Flip(screen); + } + else if (counter == 10) + { + if (img3 != NULL) + { + dest.x = img_left + (max_img_w - img3->w) / 2; + dest.y = img_y; + + SDL_BlitSurface(img3, NULL, screen, &dest); + SDL_Flip(screen); + } + else + counter = 15; + } + + if (counter == 15) + { + dest.x = img_left + (max_img_w - img1b->w) / 2; + dest.y = img_y; + + SDL_BlitSurface(img1b, NULL, screen, &dest); + SDL_Flip(screen); + + counter = 0; + } + } + } while (!done); @@ -14332,34 +15197,34 @@ static void cleanup(void) int i, j; for (j = 0; j < num_stamp_groups; j++) + { + for (i = 0; i < num_stamps[j]; i++) { - for (i = 0; i < num_stamps[j]; i++) - { #ifndef NOSOUND - if (stamp_data[j][i]->ssnd) - { - Mix_FreeChunk(stamp_data[j][i]->ssnd); - stamp_data[j][i]->ssnd = NULL; - } - if (stamp_data[j][i]->sdesc) - { - Mix_FreeChunk(stamp_data[j][i]->sdesc); - stamp_data[j][i]->sdesc = NULL; - } + if (stamp_data[j][i]->ssnd) + { + Mix_FreeChunk(stamp_data[j][i]->ssnd); + stamp_data[j][i]->ssnd = NULL; + } + if (stamp_data[j][i]->sdesc) + { + Mix_FreeChunk(stamp_data[j][i]->sdesc); + stamp_data[j][i]->sdesc = NULL; + } #endif - if (stamp_data[j][i]->stxt) - { - free(stamp_data[j][i]->stxt); - stamp_data[j][i]->stxt = NULL; - } - free_surface(&stamp_data[j][i]->thumbnail); + if (stamp_data[j][i]->stxt) + { + free(stamp_data[j][i]->stxt); + stamp_data[j][i]->stxt = NULL; + } + free_surface(&stamp_data[j][i]->thumbnail); - free(stamp_data[j][i]->stampname); - free(stamp_data[j][i]); - stamp_data[j][i] = NULL; - } - free(stamp_data[j]); + free(stamp_data[j][i]->stampname); + free(stamp_data[j][i]); + stamp_data[j][i] = NULL; } + free(stamp_data[j]); + } free_surface(&active_stamp); free_surface(¤t_stamp_cached); @@ -14373,13 +15238,13 @@ static void cleanup(void) free_surface_array(img_tool_names, NUM_TOOLS); free_surface_array(img_title_names, NUM_TITLES); for (i = 0; i < MAX_MAGIC_GROUPS; i++) + { + for (j = 0; j < num_magics[i]; j++) { - for (j = 0; j < num_magics[i]; j++) - { - free_surface(&(magics[i][j].img_icon)); - free_surface(&(magics[i][j].img_name)); - } + free_surface(&(magics[i][j].img_icon)); + free_surface(&(magics[i][j].img_name)); } + } free_surface_array(img_shapes, NUM_SHAPES); free_surface_array(img_shape_names, NUM_SHAPES); free_surface_array(img_fills, NUM_FILLS); @@ -14485,18 +15350,18 @@ static void cleanup(void) #endif if (onscreen_keyboard) - { - free_surface(&img_oskdel); - free_surface(&img_osktab); - free_surface(&img_oskenter); - free_surface(&img_oskcapslock); - free_surface(&img_oskshift); + { + free_surface(&img_oskdel); + free_surface(&img_osktab); + free_surface(&img_oskenter); + free_surface(&img_oskcapslock); + free_surface(&img_oskshift); - if (kbd) - osk_free(kbd); - else - SDL_StopTextInput(); - } + if (kbd) + osk_free(kbd); + else + SDL_StopTextInput(); + } free_surface(&screen); free_surface(&img_starter); @@ -14506,85 +15371,85 @@ static void cleanup(void) free_surface(&img_cur_brush); if (touched != NULL) - { - free(touched); - touched = NULL; - } + { + free(touched); + touched = NULL; + } if (sim_flood_touched != NULL) - { - free(sim_flood_touched); - sim_flood_touched = NULL; - } + { + free(sim_flood_touched); + sim_flood_touched = NULL; + } if (medium_font != NULL) - { + { #ifdef DEBUG - printf("cleanup: medium font\n"); //EP + printf("cleanup: medium font\n"); //EP #endif - TuxPaint_Font_CloseFont(medium_font); - medium_font = NULL; - } + TuxPaint_Font_CloseFont(medium_font); + medium_font = NULL; + } if (small_font != NULL) - { + { #ifdef DEBUG - printf("cleanup: small font\n"); //EP + printf("cleanup: small font\n"); //EP #endif - TuxPaint_Font_CloseFont(small_font); - small_font = NULL; - } + TuxPaint_Font_CloseFont(small_font); + small_font = NULL; + } if (large_font != NULL) - { + { #ifdef DEBUG - printf("cleanup: large font\n"); //EP + printf("cleanup: large font\n"); //EP #endif - TuxPaint_Font_CloseFont(large_font); - large_font = NULL; - } + TuxPaint_Font_CloseFont(large_font); + large_font = NULL; + } #ifdef FORKED_FONTS free(user_font_families); /* we'll leak the bodies... oh well */ #else for (i = 0; i < num_font_families; i++) + { + if (user_font_families[i]) { - if (user_font_families[i]) - { - char **cpp = user_font_families[i]->filename - 1; + char **cpp = user_font_families[i]->filename - 1; - if (*++cpp) - free(*cpp); - if (*++cpp) - free(*cpp); - if (*++cpp) - free(*cpp); - if (*++cpp) - free(*cpp); - if (user_font_families[i]->handle) - TuxPaint_Font_CloseFont(user_font_families[i]->handle); - free(user_font_families[i]->directory); - free(user_font_families[i]->family); - free(user_font_families[i]); - user_font_families[i] = NULL; - } + if (*++cpp) + free(*cpp); + if (*++cpp) + free(*cpp); + if (*++cpp) + free(*cpp); + if (*++cpp) + free(*cpp); + if (user_font_families[i]->handle) + TuxPaint_Font_CloseFont(user_font_families[i]->handle); + free(user_font_families[i]->directory); + free(user_font_families[i]->family); + free(user_font_families[i]); + user_font_families[i] = NULL; } + } #endif #ifndef NOSOUND if (use_sound) + { + for (i = 0; i < NUM_SOUNDS; i++) { - for (i = 0; i < NUM_SOUNDS; i++) - { - if (sounds[i]) - { - Mix_FreeChunk(sounds[i]); - sounds[i] = NULL; - } - } - - Mix_CloseAudio(); + if (sounds[i]) + { + Mix_FreeChunk(sounds[i]); + sounds[i] = NULL; + } } + + Mix_CloseAudio(); + } #endif for (i = 0; i < num_plugin_files; i++) @@ -14604,10 +15469,10 @@ static void cleanup(void) free_cursor(&cursor_pipette); for (i = 0; i < NUM_COLORS; i++) - { - free(color_hexes[i]); - free(color_names[i]); - } + { + free(color_hexes[i]); + free(color_names[i]); + } free(color_hexes); free(color_names); @@ -14622,36 +15487,37 @@ static void cleanup(void) (so Tux Paint can be launched again soon, if the user wants to!) */ if (ok_to_use_lockfile) - { - char *lock_fname; - time_t zero_time; - FILE *fi; + { + char *lock_fname; + time_t zero_time; + FILE *fi; #ifndef WIN32 - lock_fname = get_fname("lockfile.dat", DIR_SAVE); + lock_fname = get_fname("lockfile.dat", DIR_SAVE); #else - lock_fname = get_temp_fname("lockfile.dat"); + lock_fname = get_temp_fname("lockfile.dat"); #endif - zero_time = (time_t) 0; + zero_time = (time_t) 0; - fi = fopen(lock_fname, "w"); - if (fi != NULL) - { - /* If we can write to it, do so! */ + fi = fopen(lock_fname, "w"); + if (fi != NULL) + { + /* If we can write to it, do so! */ - fwrite(&zero_time, sizeof(time_t), 1, fi); - fclose(fi); - } - else - { - fprintf(stderr, - "\nWarning: I couldn't create the lockfile (%s)\n" - "The error that occurred was:\n" "%s\n\n", lock_fname, strerror(errno)); - } - - free(lock_fname); + fwrite(&zero_time, sizeof(time_t), 1, fi); + fclose(fi); } + else + { + fprintf(stderr, + "\nWarning: I couldn't create the lockfile (%s)\n" + "The error that occurred was:\n" "%s\n\n", lock_fname, + strerror(errno)); + } + + free(lock_fname); + } #if !defined(WIN32) && !defined(__APPLE__) && !defined(__BEOS__) && !defined(__ANDROID__) // if (papersize != NULL) @@ -14690,10 +15556,10 @@ static void free_surface(SDL_Surface ** surface_array) { if (surface_array) //EP added this line to avoid app crash if (*surface_array) - { - SDL_FreeSurface(*surface_array); - *surface_array = NULL; - } + { + SDL_FreeSurface(*surface_array); + *surface_array = NULL; + } } @@ -14706,9 +15572,9 @@ static void free_surface_array(SDL_Surface * surface_array[], int count) if (surface_array) //EP added this line to avoid app crash for (i = 0; i < count; ++i) - { - free_surface(&surface_array[i]); - } + { + free_surface(&surface_array[i]); + } } @@ -14730,29 +15596,29 @@ static void do_shape(int sx, int sy, int nx, int ny, int rotn, int use_brush) rx = abs(nx - sx); ry = abs(ny - sy); if (shape_mode == SHAPEMODE_CORNER) - { - rx = sqrt(rx * rx) / 2; - ry = sqrt(ry * ry) / 2; - } + { + rx = sqrt(rx * rx) / 2; + ry = sqrt(ry * ry) / 2; + } /* If the shape has a 1:1 ("locked") aspect ratio, use the larger radius: */ if (shape_locked[cur_shape]) - { - if (rx > ry) - ry = rx; - else - rx = ry; - } + { + if (rx > ry) + ry = rx; + else + rx = ry; + } /* Is the shape tiny? Make it SOME size, first! */ if (rx < 15 && ry < 15) - { - rx = 15; - ry = 15; - } + { + rx = 15; + ry = 15; + } if (rx < 2) rx = 2; @@ -14763,11 +15629,11 @@ static void do_shape(int sx, int sy, int nx, int ny, int rotn, int use_brush) /* Render a default brush: */ if (use_brush) - { - old_brush = cur_brush; - cur_brush = shape_brush; /* Now only semi-kludgy! */ - render_brush(); - } + { + old_brush = cur_brush; + cur_brush = shape_brush; /* Now only semi-kludgy! */ + render_brush(); + } /* Draw the shape: */ @@ -14778,93 +15644,201 @@ static void do_shape(int sx, int sy, int nx, int ny, int rotn, int use_brush) if (shape_mode == SHAPEMODE_CORNER) + { + /* Get extent of shape based on it's vertices, + and scale up if we need to + (e.g., square's points are at 45, 135, 225 & 315 degrees, + which do not extend to the full radius). + + This works well for square and rectangle; it mostly + works for triangle and 5-pointed star, but it seems + sufficient. -bjk 2020.08.15 */ + max_x = 0; + max_y = 0; + for (side = 0; side < shape_sides[cur_shape]; side++) { - /* Get extent of shape based on it's vertices, - and scale up if we need to - (e.g., square's points are at 45, 135, 225 & 315 degrees, - which do not extend to the full radius). + a1 = (angle_skip * side + init_ang) * M_PI / 180.0; + a2 = (angle_skip * (side + 1) + init_ang) * M_PI / 180.0; + x1 = (int) (cos(a1) * rx); + y1 = (int) (-sin(a1) * ry); - This works well for square and rectangle; it mostly - works for triangle and 5-pointed star, but it seems - sufficient. -bjk 2020.08.15 */ - max_x = 0; - max_y = 0; - for (side = 0; side < shape_sides[cur_shape]; side++) - { - a1 = (angle_skip * side + init_ang) * M_PI / 180.0; - a2 = (angle_skip * (side + 1) + init_ang) * M_PI / 180.0; - x1 = (int)(cos(a1) * rx); - y1 = (int)(-sin(a1) * ry); - - if (abs(x1) > max_x) - max_x = abs(x1); - if (abs(y1) > max_y) - max_y = abs(y1); - } - - if (max_x < rx) - rx = (rx * rx) / max_x; - if (max_y < ry) - ry = (ry * ry) / max_y; + if (abs(x1) > max_x) + max_x = abs(x1); + if (abs(y1) > max_y) + max_y = abs(y1); } + if (max_x < rx) + rx = (rx * rx) / max_x; + if (max_y < ry) + ry = (ry * ry) / max_y; + } + step = 1; if (dont_do_xor && !use_brush) - { - /* If we're in light outline mode & not drawing the shape with the brush, - if it has lots of sides (like a circle), reduce the number of sides: */ + { + /* If we're in light outline mode & not drawing the shape with the brush, + if it has lots of sides (like a circle), reduce the number of sides: */ - if (shape_sides[cur_shape] > 5) - step = (shape_sides[cur_shape] / 8); - } + if (shape_sides[cur_shape] > 5) + step = (shape_sides[cur_shape] / 8); + } /* Where is the object? */ - if (shape_mode == SHAPEMODE_CENTER) { + if (shape_mode == SHAPEMODE_CENTER) + { offx = 0; offy = 0; - } else { + } + else + { offx = (nx - sx) / 2; offy = (ny - sy) / 2; if (shape_locked[cur_shape]) + { + if (abs(offx) > abs(offy)) { - if (abs(offx) > abs(offy)) - { - if (offy > 0) - offy = abs(offx); - else - offy = -abs(offx); - } + if (offy > 0) + offy = abs(offx); else - { - if (offx > 0) - offx = abs(offy); - else - offx = -abs(offy); - } + offy = -abs(offx); } + else + { + if (offx > 0) + offx = abs(offy); + else + offx = -abs(offy); + } + } } for (side = 0; side < shape_sides[cur_shape]; side = side + step) + { + a1 = (angle_skip * side + init_ang) * M_PI / 180; + a2 = (angle_skip * (side + 1) + init_ang) * M_PI / 180; + + x1 = (int) (cos(a1) * rx); + y1 = (int) (-sin(a1) * ry); + + x2 = (int) (cos(a2) * rx); + y2 = (int) (-sin(a2) * ry); + + xv = (int) (cos((a1 + a2) / 2) * rx * shape_valley[cur_shape] / 100); + yv = (int) (-sin((a1 + a2) / 2) * ry * shape_valley[cur_shape] / 100); + + /* Rotate the line: */ + + if (rotn != 0) { - a1 = (angle_skip * side + init_ang) * M_PI / 180; - a2 = (angle_skip * (side + 1) + init_ang) * M_PI / 180; + rotn_rad = rotn * M_PI / 180; - x1 = (int)(cos(a1) * rx); - y1 = (int)(-sin(a1) * ry); + xp = (x1 + offx) * cos(rotn_rad) - (y1 + offy) * sin(rotn_rad); + yp = (x1 + offx) * sin(rotn_rad) + (y1 + offy) * cos(rotn_rad); - x2 = (int)(cos(a2) * rx); - y2 = (int)(-sin(a2) * ry); + x1 = xp - offx; + y1 = yp - offy; - xv = (int)(cos((a1 + a2) / 2) * rx * shape_valley[cur_shape] / 100); - yv = (int)(-sin((a1 + a2) / 2) * ry * shape_valley[cur_shape] / 100); + xp = (x2 + offx) * cos(rotn_rad) - (y2 + offy) * sin(rotn_rad); + yp = (x2 + offx) * sin(rotn_rad) + (y2 + offy) * cos(rotn_rad); - /* Rotate the line: */ + x2 = xp - offx; + y2 = yp - offy; - if (rotn != 0) + xp = (xv + offx) * cos(rotn_rad) - (yv + offy) * sin(rotn_rad); + yp = (xv + offx) * sin(rotn_rad) + (yv + offy) * cos(rotn_rad); + + xv = xp - offx; + yv = yp - offy; + } + + + /* Center the line around the center of the shape: */ + + x1 = x1 + sx + offx; + y1 = y1 + sy + offy; + x2 = x2 + sx + offx; + y2 = y2 + sy + offy; + xv = xv + sx + offx; + yv = yv + sy + offy; + + + /* Draw: */ + + if (!use_brush) + { + /* (XOR) */ + if (shape_valley[cur_shape] == 100) + line_xor(x1, y1, x2, y2); + else + { + line_xor(x1, y1, xv, yv); + line_xor(xv, yv, x2, y2); + } + } + else + { + if (shape_valley[cur_shape] == 100) + /* Brush */ + + brush_draw(x1, y1, x2, y2, 0); + else + /* Stars */ + { + brush_draw(x1, y1, xv, yv, 0); + brush_draw(xv, yv, x2, y2, 0); + } + } + } + + + if (use_brush && shape_filled[cur_shape] && rx > 0 && ry > 0) + { + /* FIXME: In the meantime, we'll do this lame radius-based fill: */ + + for (xx = max(abs(rx), abs(ry)); xx > 0; xx--) + { + yy = min(xx * rx / ry, xx * ry / rx); + + for (side = 0; side < shape_sides[cur_shape]; side++) + { + a1 = (angle_skip * side + init_ang) * M_PI / 180; + a2 = (angle_skip * (side + 1) + init_ang) * M_PI / 180; + + if (yy == xx * ry / rx) + { + x1 = (int) (cos(a1) * xx); + y1 = (int) (-sin(a1) * yy); + + x2 = (int) (cos(a2) * xx); + y2 = (int) (-sin(a2) * yy); + + xv = + (int) (cos((a1 + a2) / 2) * xx * shape_valley[cur_shape] / 100); + yv = + (int) (-sin((a1 + a2) / 2) * yy * shape_valley[cur_shape] / 100); + } + else + { + x1 = (int) (cos(a1) * yy); + y1 = (int) (-sin(a1) * xx); + + x2 = (int) (cos(a2) * yy); + y2 = (int) (-sin(a2) * xx); + + xv = + (int) (cos((a1 + a2) / 2) * yy * shape_valley[cur_shape] / 100); + yv = + (int) (-sin((a1 + a2) / 2) * xx * shape_valley[cur_shape] / 100); + } + + /* Rotate the line: */ + + if (rotn != 0) { rotn_rad = rotn * M_PI / 180; @@ -14888,155 +15862,56 @@ static void do_shape(int sx, int sy, int nx, int ny, int rotn, int use_brush) } - /* Center the line around the center of the shape: */ + /* Center the line around the center of the shape: */ - x1 = x1 + sx + offx; - y1 = y1 + sy + offy; - x2 = x2 + sx + offx; - y2 = y2 + sy + offy; - xv = xv + sx + offx; - yv = yv + sy + offy; + x1 = x1 + sx + offx; + y1 = y1 + sy + offy; + x2 = x2 + sx + offx; + y2 = y2 + sy + offy; + xv = xv + sx + offx; + yv = yv + sy + offy; - /* Draw: */ - - if (!use_brush) + /* Draw: */ + if (shape_valley[cur_shape] == 100) + brush_draw(x1, y1, x2, y2, 0); + else + /* Stars */ { - /* (XOR) */ - if (shape_valley[cur_shape] == 100) - line_xor(x1, y1, x2, y2); - else - { - line_xor(x1, y1, xv, yv); - line_xor(xv, yv, x2, y2); - } + brush_draw(x1, y1, xv, yv, 0); + brush_draw(xv, yv, x2, y2, 0); } - else - { - if (shape_valley[cur_shape] == 100) - /* Brush */ + } - brush_draw(x1, y1, x2, y2, 0); - else - /* Stars */ - { - brush_draw(x1, y1, xv, yv, 0); - brush_draw(xv, yv, x2, y2, 0); - } - } - } - - - if (use_brush && shape_filled[cur_shape] && rx > 0 && ry > 0) - { - /* FIXME: In the meantime, we'll do this lame radius-based fill: */ - - for (xx = max(abs(rx), abs(ry)); xx > 0; xx--) - { - yy = min(xx * rx / ry, xx * ry / rx); - - for (side = 0; side < shape_sides[cur_shape]; side++) - { - a1 = (angle_skip * side + init_ang) * M_PI / 180; - a2 = (angle_skip * (side + 1) + init_ang) * M_PI / 180; - - if (yy == xx * ry / rx) - { - x1 = (int)(cos(a1) * xx); - y1 = (int)(-sin(a1) * yy); - - x2 = (int)(cos(a2) * xx); - y2 = (int)(-sin(a2) * yy); - - xv = (int)(cos((a1 + a2) / 2) * xx * shape_valley[cur_shape] / 100); - yv = (int)(-sin((a1 + a2) / 2) * yy * shape_valley[cur_shape] / 100); - } - else - { - x1 = (int)(cos(a1) * yy); - y1 = (int)(-sin(a1) * xx); - - x2 = (int)(cos(a2) * yy); - y2 = (int)(-sin(a2) * xx); - - xv = (int)(cos((a1 + a2) / 2) * yy * shape_valley[cur_shape] / 100); - yv = (int)(-sin((a1 + a2) / 2) * xx * shape_valley[cur_shape] / 100); - } - - /* Rotate the line: */ - - if (rotn != 0) - { - rotn_rad = rotn * M_PI / 180; - - xp = (x1 + offx) * cos(rotn_rad) - (y1 + offy) * sin(rotn_rad); - yp = (x1 + offx) * sin(rotn_rad) + (y1 + offy) * cos(rotn_rad); - - x1 = xp - offx; - y1 = yp - offy; - - xp = (x2 + offx) * cos(rotn_rad) - (y2 + offy) * sin(rotn_rad); - yp = (x2 + offx) * sin(rotn_rad) + (y2 + offy) * cos(rotn_rad); - - x2 = xp - offx; - y2 = yp - offy; - - xp = (xv + offx) * cos(rotn_rad) - (yv + offy) * sin(rotn_rad); - yp = (xv + offx) * sin(rotn_rad) + (yv + offy) * cos(rotn_rad); - - xv = xp - offx; - yv = yp - offy; - } - - - /* Center the line around the center of the shape: */ - - x1 = x1 + sx + offx; - y1 = y1 + sy + offy; - x2 = x2 + sx + offx; - y2 = y2 + sy + offy; - xv = xv + sx + offx; - yv = yv + sy + offy; - - - /* Draw: */ - if (shape_valley[cur_shape] == 100) - brush_draw(x1, y1, x2, y2, 0); - else - /* Stars */ - { - brush_draw(x1, y1, xv, yv, 0); - brush_draw(xv, yv, x2, y2, 0); - } - } - - if (xx % 10 == 0) - update_canvas(0, 0, WINDOW_WIDTH - r_ttoolopt.w, (button_h * 7) + 40 + HEIGHTOFFSET); - } + if (xx % 10 == 0) + update_canvas(0, 0, WINDOW_WIDTH - r_ttoolopt.w, + (button_h * 7) + 40 + HEIGHTOFFSET); } + } /* Update it! */ if (use_brush) - { - if (abs(rx) > abs(ry)) - rmax = abs(rx) + 20; - else - rmax = abs(ry) + 20; + { + if (abs(rx) > abs(ry)) + rmax = abs(rx) + 20; + else + rmax = abs(ry) + 20; - update_canvas(sx - rmax + offx, sy - rmax + offy, sx + rmax + offx, sy + rmax + offy); - } + update_canvas(sx - rmax + offx, sy - rmax + offy, sx + rmax + offx, + sy + rmax + offy); + } /* Return to normal brush (for paint brush and line tools): */ if (use_brush) - { - cur_brush = old_brush; - render_brush(); - show_brush_tip(); - } + { + cur_brush = old_brush; + render_brush(); + show_brush_tip(); + } } @@ -15050,7 +15925,8 @@ static int shape_rotation(int ctr_x, int ctr_y, int ox, int oy) deg = (atan2(oy - ctr_y, ox - ctr_x) * 180 / M_PI); - if (shape_reverse) { + if (shape_reverse) + { deg = (deg + 180) % 360; } @@ -15108,7 +15984,6 @@ static int brush_rotation(int ctr_x, int ctr_y, int ox, int oy) /* Save the current image: */ static int do_save(int tool, int dont_show_success_results, int autosave) - { int scroll; char *fname; @@ -15120,116 +15995,127 @@ static int do_save(int tool, int dont_show_success_results, int autosave) if (disable_save && !autosave) return 0; - scroll = (NUM_TOOLS > buttons_tall * gd_tools.cols) ? img_scroll_down->h : 0; + scroll = + (NUM_TOOLS > buttons_tall * gd_tools.cols) ? img_scroll_down->h : 0; tmp_apply_uncommited_text(); SDL_BlitSurface(canvas, NULL, save_canvas, NULL); SDL_BlitSurface(label, NULL, save_canvas, NULL); if (autosave) - { - /* No prompts, no progressbar, always save to autosave.png */ - } + { + /* No prompts, no progressbar, always save to autosave.png */ + } else if (promptless_save == SAVE_OVER_NO) + { + /* Never save over - _always_ save a new file! */ + + get_new_file_id(); + } + else if (promptless_save == SAVE_OVER_PROMPT) + { + /* Saving the same picture? */ + + if (file_id[0] != '\0') { - /* Never save over - _always_ save a new file! */ + /* We sure we want to do that? */ + + if (do_prompt_image_snd(PROMPT_SAVE_OVER_TXT, + PROMPT_SAVE_OVER_YES, + PROMPT_SAVE_OVER_NO, + img_save_over, NULL, NULL, SND_AREYOUSURE, + (TOOL_SAVE % 2) * button_w + button_w / 2, + (TOOL_SAVE / 2) * button_h + r_ttools.h + + button_h / 2 - + tool_scroll * button_h / gd_tools.cols + + scroll) == 0) + { + /* No - Let's save a new picture! */ + + get_new_file_id(); + } + if (tool == TOOL_TEXT || tool == TOOL_LABEL) + do_render_cur_text(0); + } + else + { + /* Saving a new picture: */ get_new_file_id(); } - else if (promptless_save == SAVE_OVER_PROMPT) - { - /* Saving the same picture? */ - - if (file_id[0] != '\0') - { - /* We sure we want to do that? */ - - if (do_prompt_image_snd(PROMPT_SAVE_OVER_TXT, - PROMPT_SAVE_OVER_YES, - PROMPT_SAVE_OVER_NO, - img_save_over, NULL, NULL, SND_AREYOUSURE, - (TOOL_SAVE % 2) * button_w + button_w / 2, - (TOOL_SAVE / 2) * button_h + r_ttools.h + button_h / 2 - tool_scroll * button_h / gd_tools.cols + scroll) == 0) - { - /* No - Let's save a new picture! */ - - get_new_file_id(); - } - if (tool == TOOL_TEXT || tool == TOOL_LABEL) - do_render_cur_text(0); - } - else - { - /* Saving a new picture: */ - - get_new_file_id(); - } - } + } else if (promptless_save == SAVE_OVER_ALWAYS) - { - if (file_id[0] == '\0') - get_new_file_id(); - } + { + if (file_id[0] == '\0') + get_new_file_id(); + } /* Make sure we have a ~/.tuxpaint directory: */ if (!autosave) - { - show_progress_bar(screen); - do_setcursor(cursor_watch); - } + { + show_progress_bar(screen); + do_setcursor(cursor_watch); + } - if (!make_directory(DIR_SAVE, "", "Can't create user data directory (E002)")) - { - fprintf(stderr, "Cannot save the any pictures! SORRY!\n\n"); - draw_tux_text(TUX_OOPS, strerror(errno), 0); - return 0; - } + if (!make_directory + (DIR_SAVE, "", "Can't create user data directory (E002)")) + { + fprintf(stderr, "Cannot save the any pictures! SORRY!\n\n"); + draw_tux_text(TUX_OOPS, strerror(errno), 0); + return 0; + } if (!autosave) - { - show_progress_bar(screen); - } + { + show_progress_bar(screen); + } /* Make sure we have a ~/.tuxpaint/saved directory: */ - if (!make_directory(DIR_SAVE, "saved", "Can't create user data directory (for saved drawings) (E003)")) - { - fprintf(stderr, "Cannot save any pictures! SORRY!\n\n"); - draw_tux_text(TUX_OOPS, strerror(errno), 0); - return 0; - } + if (!make_directory + (DIR_SAVE, "saved", + "Can't create user data directory (for saved drawings) (E003)")) + { + fprintf(stderr, "Cannot save any pictures! SORRY!\n\n"); + draw_tux_text(TUX_OOPS, strerror(errno), 0); + return 0; + } if (!autosave) - { - show_progress_bar(screen); - } + { + show_progress_bar(screen); + } /* Make sure we have a ~/.tuxpaint/saved/.thumbs/ directory: */ - if (!make_directory(DIR_SAVE, "saved/.thumbs", "Can't create user data thumbnail directory (for saved drawings' thumbnails) (E004)")) - { - fprintf(stderr, "Cannot save any pictures! SORRY!\n\n"); - draw_tux_text(TUX_OOPS, strerror(errno), 0); - return 0; - } + if (!make_directory + (DIR_SAVE, "saved/.thumbs", + "Can't create user data thumbnail directory (for saved drawings' thumbnails) (E004)")) + { + fprintf(stderr, "Cannot save any pictures! SORRY!\n\n"); + draw_tux_text(TUX_OOPS, strerror(errno), 0); + return 0; + } - if (!make_directory(DIR_SAVE, "saved/.label", "Can't create label information directory (E005)")) - { - fprintf(stderr, "Cannot save label information! SORRY!\n\n"); - draw_tux_text(TUX_OOPS, strerror(errno), 0); - return 0; - } - + if (!make_directory + (DIR_SAVE, "saved/.label", + "Can't create label information directory (E005)")) + { + fprintf(stderr, "Cannot save label information! SORRY!\n\n"); + draw_tux_text(TUX_OOPS, strerror(errno), 0); + return 0; + } + if (!autosave) - { - show_progress_bar(screen); - } + { + show_progress_bar(screen); + } /* Save the file: */ @@ -15243,21 +16129,22 @@ static int do_save(int tool, int dont_show_success_results, int autosave) fi = fopen(fname, "wb"); if (fi == NULL) - { - fprintf(stderr, - "\nError: Couldn't save the current image!\n" - "%s\n" "The system error that occurred was:\n" "%s\n\n", fname, strerror(errno)); + { + fprintf(stderr, + "\nError: Couldn't save the current image!\n" + "%s\n" "The system error that occurred was:\n" "%s\n\n", fname, + strerror(errno)); - draw_tux_text(TUX_OOPS, strerror(errno), 0); - } + draw_tux_text(TUX_OOPS, strerror(errno), 0); + } else + { + if (!do_png_save(fi, fname, save_canvas, 1)) { - if (!do_png_save(fi, fname, save_canvas, 1)) - { - free(fname); - return 0; - } + free(fname); + return 0; } + } free(fname); @@ -15277,17 +16164,18 @@ static int do_save(int tool, int dont_show_success_results, int autosave) fname = get_fname(tmp, DIR_SAVE); fi = fopen(fname, "r"); if (fi != NULL) - { - fclose(fi); - } + { + fclose(fi); + } else - { - /* No old thumbnail! Save this image's thumbnail in the new place, - under ".thumbs" */ + { + /* No old thumbnail! Save this image's thumbnail in the new place, + under ".thumbs" */ - safe_snprintf(tmp, sizeof(tmp), "saved/.thumbs/%s-t%s", file_id, FNAME_EXTENSION); - fname = get_fname(tmp, DIR_SAVE); - } + safe_snprintf(tmp, sizeof(tmp), "saved/.thumbs/%s-t%s", file_id, + FNAME_EXTENSION); + fname = get_fname(tmp, DIR_SAVE); + } debug(fname); @@ -15295,14 +16183,15 @@ static int do_save(int tool, int dont_show_success_results, int autosave) fi = fopen(fname, "wb"); if (fi == NULL) - { - fprintf(stderr, "\nError: Couldn't save thumbnail of image!\n" - "%s\n" "The system error that occurred was:\n" "%s\n\n", fname, strerror(errno)); - } + { + fprintf(stderr, "\nError: Couldn't save thumbnail of image!\n" + "%s\n" "The system error that occurred was:\n" "%s\n\n", fname, + strerror(errno)); + } else - { - do_png_save(fi, fname, thm, 0); - } + { + do_png_save(fi, fname, thm, 0); + } SDL_FreeSurface(thm); free(fname); @@ -15311,23 +16200,26 @@ static int do_save(int tool, int dont_show_success_results, int autosave) /* Write 'starter' and/or canvas color info, if it's useful to: */ if (starter_id[0] != '\0' || - template_id[0] != '\0' || canvas_color_r != 255 || canvas_color_g != 255 || canvas_color_b != 255) + template_id[0] != '\0' || canvas_color_r != 255 || canvas_color_g != 255 + || canvas_color_b != 255) + { + safe_snprintf(tmp, sizeof(tmp), "saved/%s.dat", file_id); + fname = get_fname(tmp, DIR_SAVE); + fi = fopen(fname, "w"); + if (fi != NULL) { - safe_snprintf(tmp, sizeof(tmp), "saved/%s.dat", file_id); - fname = get_fname(tmp, DIR_SAVE); - fi = fopen(fname, "w"); - if (fi != NULL) - { - fprintf(fi, "%s\n", starter_id); - fprintf(fi, "%d %d %d\n", starter_mirrored, starter_flipped, starter_personal); - fprintf(fi, "c%d %d %d\n", canvas_color_r, canvas_color_g, canvas_color_b); - fprintf(fi, "T%s\n", template_id); - fprintf(fi, "%d\n", template_personal); - fclose(fi); - } - - free(fname); + fprintf(fi, "%s\n", starter_id); + fprintf(fi, "%d %d %d\n", starter_mirrored, starter_flipped, + starter_personal); + fprintf(fi, "c%d %d %d\n", canvas_color_r, canvas_color_g, + canvas_color_b); + fprintf(fi, "T%s\n", template_id); + fprintf(fi, "%d\n", template_personal); + fclose(fi); } + + free(fname); + } #endif /* All happy! */ @@ -15335,10 +16227,10 @@ static int do_save(int tool, int dont_show_success_results, int autosave) playsound(screen, 0, SND_SAVE, 1, SNDPOS_CENTER, SNDDIST_NEAR); if (!dont_show_success_results) - { - draw_tux_text(TUX_DEFAULT, tool_tips[TOOL_SAVE], 1); - do_setcursor(cursor_arrow); - } + { + draw_tux_text(TUX_DEFAULT, tool_tips[TOOL_SAVE], 1); + do_setcursor(cursor_arrow); + } undo_tmp_applied_text(); @@ -15348,7 +16240,8 @@ static int do_save(int tool, int dont_show_success_results, int autosave) /** * FIXME */ -static void set_chunk_data(unsigned char **chunk_data, size_t * chunk_data_len, size_t uncompressed_size, Bytef * data, +static void set_chunk_data(unsigned char **chunk_data, size_t *chunk_data_len, + size_t uncompressed_size, Bytef * data, size_t dataLen) { int headersLen; @@ -15373,11 +16266,11 @@ static void set_chunk_data(unsigned char **chunk_data, size_t * chunk_data_len, *chunk_data_len = headersLen + dataLen; cdata = calloc(*chunk_data_len, sizeof(unsigned char *)); - strcat(cdata, headers); /* FIXME: Use strncat() */ + strcat(cdata, headers); /* FIXME: Use strncat() */ for (i = 0; i < dataLen; i++) cdata[headersLen + i] = data[i]; - *chunk_data = (unsigned char *)cdata; + *chunk_data = (unsigned char *) cdata; free(line); free(headers); @@ -15444,372 +16337,408 @@ static void do_png_embed_data(png_structp png_ptr) /* Starter foreground */ if (img_starter) - { + { #ifdef DEBUG - printf("Saving starter... %d\n", (int)(intptr_t) img_starter); //EP added (intptr_t) to avoid warning on x64 + printf("Saving starter... %d\n", (int) (intptr_t) img_starter); //EP added (intptr_t) to avoid warning on x64 #endif - sbk_pixs = malloc(img_starter->h * img_starter->w * 4); - compressedLen = compressBound(img_starter->h * img_starter->w * 4); + sbk_pixs = malloc(img_starter->h * img_starter->w * 4); + compressedLen = compressBound(img_starter->h * img_starter->w * 4); - compressed_data = malloc(compressedLen * sizeof(Bytef *)); + compressed_data = malloc(compressedLen * sizeof(Bytef *)); - if (SDL_MUSTLOCK(img_starter)) - SDL_LockSurface(img_starter); + if (SDL_MUSTLOCK(img_starter)) + SDL_LockSurface(img_starter); - for (y = 0; y < img_starter->h; y++) - for (x = 0; x < img_starter->w; x++) - { - SDL_GetRGBA(getpixels[img_starter->format->BytesPerPixel] (img_starter, x, y), img_starter->format, &r, &g, - &b, &a); + for (y = 0; y < img_starter->h; y++) + for (x = 0; x < img_starter->w; x++) + { + SDL_GetRGBA(getpixels[img_starter->format->BytesPerPixel] + (img_starter, x, y), img_starter->format, &r, &g, &b, &a); /* clear the transparent pixels assigning the same r g and b values */ - if (a == SDL_ALPHA_TRANSPARENT) - { - sbk_pixs[4 * (y * img_starter->w + x)] = SDL_ALPHA_TRANSPARENT; - sbk_pixs[4 * (y * img_starter->w + x) + 1] = SDL_ALPHA_TRANSPARENT; - sbk_pixs[4 * (y * img_starter->w + x) + 2] = SDL_ALPHA_TRANSPARENT; - sbk_pixs[4 * (y * img_starter->w + x) + 3] = SDL_ALPHA_TRANSPARENT; - } - else - { - sbk_pixs[4 * (y * img_starter->w + x)] = r; - sbk_pixs[4 * (y * img_starter->w + x) + 1] = g; - sbk_pixs[4 * (y * img_starter->w + x) + 2] = b; - sbk_pixs[4 * (y * img_starter->w + x) + 3] = a; - } - } + if (a == SDL_ALPHA_TRANSPARENT) + { + sbk_pixs[4 * (y * img_starter->w + x)] = SDL_ALPHA_TRANSPARENT; + sbk_pixs[4 * (y * img_starter->w + x) + 1] = SDL_ALPHA_TRANSPARENT; + sbk_pixs[4 * (y * img_starter->w + x) + 2] = SDL_ALPHA_TRANSPARENT; + sbk_pixs[4 * (y * img_starter->w + x) + 3] = SDL_ALPHA_TRANSPARENT; + } + else + { + sbk_pixs[4 * (y * img_starter->w + x)] = r; + sbk_pixs[4 * (y * img_starter->w + x) + 1] = g; + sbk_pixs[4 * (y * img_starter->w + x) + 2] = b; + sbk_pixs[4 * (y * img_starter->w + x) + 3] = a; + } + } - if (SDL_MUSTLOCK(img_starter)) - SDL_UnlockSurface(img_starter); + if (SDL_MUSTLOCK(img_starter)) + SDL_UnlockSurface(img_starter); - compress(compressed_data, &compressedLen, sbk_pixs, img_starter->h * img_starter->w * 4); - set_chunk_data(&chunk_data, &chunk_data_len, img_starter->w * img_starter->h * 4, compressed_data, compressedLen); + compress(compressed_data, &compressedLen, sbk_pixs, + img_starter->h * img_starter->w * 4); + set_chunk_data(&chunk_data, &chunk_data_len, + img_starter->w * img_starter->h * 4, compressed_data, + compressedLen); - tuxpaint_chunks[1].data = (png_byte *) chunk_data; - tuxpaint_chunks[1].size = chunk_data_len; - tuxpaint_chunks[1].location = PNG_HAVE_IHDR; - tuxpaint_chunks[1].name[0] = 't'; - tuxpaint_chunks[1].name[1] = 'p'; - tuxpaint_chunks[1].name[2] = 'F'; - tuxpaint_chunks[1].name[3] = 'G'; - tuxpaint_chunks[1].name[4] = '\0'; - png_write_chunk(png_ptr, tuxpaint_chunks[1].name, tuxpaint_chunks[1].data, tuxpaint_chunks[1].size); + tuxpaint_chunks[1].data = (png_byte *) chunk_data; + tuxpaint_chunks[1].size = chunk_data_len; + tuxpaint_chunks[1].location = PNG_HAVE_IHDR; + tuxpaint_chunks[1].name[0] = 't'; + tuxpaint_chunks[1].name[1] = 'p'; + tuxpaint_chunks[1].name[2] = 'F'; + tuxpaint_chunks[1].name[3] = 'G'; + tuxpaint_chunks[1].name[4] = '\0'; + png_write_chunk(png_ptr, tuxpaint_chunks[1].name, tuxpaint_chunks[1].data, + tuxpaint_chunks[1].size); - free(compressed_data); - free(chunk_data); - free(sbk_pixs); - } + free(compressed_data); + free(chunk_data); + free(sbk_pixs); + } /* Starter background */ if (img_starter_bkgd) + { + sbk_pixs = malloc(img_starter_bkgd->w * img_starter_bkgd->h * 3); + compressedLen = + compressBound(img_starter_bkgd->h * img_starter_bkgd->w * 3); + + compressed_data = malloc(compressedLen * sizeof(Bytef *)); + + if (SDL_MUSTLOCK(img_starter_bkgd)) + SDL_LockSurface(img_starter_bkgd); + + for (y = 0; y < img_starter_bkgd->h; y++) + for (x = 0; x < img_starter_bkgd->w; x++) + { + SDL_GetRGB(getpixels[img_starter_bkgd->format->BytesPerPixel] + (img_starter_bkgd, x, y), img_starter_bkgd->format, &r, &g, + &b); + + sbk_pixs[3 * (y * img_starter_bkgd->w + x)] = r; + sbk_pixs[3 * (y * img_starter_bkgd->w + x) + 1] = g; + sbk_pixs[3 * (y * img_starter_bkgd->w + x) + 2] = b; + } + + /* Clear the parts covered by the foreground */ + if (img_starter) { - sbk_pixs = malloc(img_starter_bkgd->w * img_starter_bkgd->h * 3); - compressedLen = compressBound(img_starter_bkgd->h * img_starter_bkgd->w * 3); - - compressed_data = malloc(compressedLen * sizeof(Bytef *)); - - if (SDL_MUSTLOCK(img_starter_bkgd)) - SDL_LockSurface(img_starter_bkgd); - + if (SDL_MUSTLOCK(img_starter)) + SDL_LockSurface(img_starter); for (y = 0; y < img_starter_bkgd->h; y++) for (x = 0; x < img_starter_bkgd->w; x++) - { - SDL_GetRGB(getpixels[img_starter_bkgd->format->BytesPerPixel] (img_starter_bkgd, x, y), - img_starter_bkgd->format, &r, &g, &b); - - sbk_pixs[3 * (y * img_starter_bkgd->w + x)] = r; - sbk_pixs[3 * (y * img_starter_bkgd->w + x) + 1] = g; - sbk_pixs[3 * (y * img_starter_bkgd->w + x) + 2] = b; - } - - /* Clear the parts covered by the foreground */ - if (img_starter) { - if (SDL_MUSTLOCK(img_starter)) - SDL_LockSurface(img_starter); - for (y = 0; y < img_starter_bkgd->h; y++) - for (x = 0; x < img_starter_bkgd->w; x++) - { - SDL_GetRGBA(getpixels[img_starter->format->BytesPerPixel] (img_starter, x, y), img_starter->format, &r, - &g, &b, &a); + SDL_GetRGBA(getpixels[img_starter->format->BytesPerPixel] + (img_starter, x, y), img_starter->format, &r, &g, &b, + &a); - if (a == SDL_ALPHA_OPAQUE) - { - sbk_pixs[3 * (y * img_starter_bkgd->w + x)] = SDL_ALPHA_TRANSPARENT; - sbk_pixs[3 * (y * img_starter_bkgd->w + x) + 1] = SDL_ALPHA_TRANSPARENT; - sbk_pixs[3 * (y * img_starter_bkgd->w + x) + 2] = SDL_ALPHA_TRANSPARENT; - } - } - if (SDL_MUSTLOCK(img_starter)) - SDL_UnlockSurface(img_starter); + if (a == SDL_ALPHA_OPAQUE) + { + sbk_pixs[3 * (y * img_starter_bkgd->w + x)] = + SDL_ALPHA_TRANSPARENT; + sbk_pixs[3 * (y * img_starter_bkgd->w + x) + 1] = + SDL_ALPHA_TRANSPARENT; + sbk_pixs[3 * (y * img_starter_bkgd->w + x) + 2] = + SDL_ALPHA_TRANSPARENT; + } } - - if (SDL_MUSTLOCK(img_starter_bkgd)) - SDL_UnlockSurface(img_starter_bkgd); - -#ifdef DEBUG - printf("%d \n", (int)compressedLen); -#endif - - compress(compressed_data, &compressedLen, sbk_pixs, img_starter_bkgd->h * img_starter_bkgd->w * 3); - - set_chunk_data(&chunk_data, &chunk_data_len, img_starter_bkgd->w * img_starter_bkgd->h * 3, compressed_data, - compressedLen); -#ifdef DEBUG - printf("%d \n", (int)compressedLen); -#endif - - - tuxpaint_chunks[2].data = (png_byte *) chunk_data; - tuxpaint_chunks[2].size = chunk_data_len; - tuxpaint_chunks[2].location = PNG_HAVE_IHDR; - tuxpaint_chunks[2].name[0] = 't'; - tuxpaint_chunks[2].name[1] = 'p'; - tuxpaint_chunks[2].name[2] = 'B'; - tuxpaint_chunks[2].name[3] = 'G'; - tuxpaint_chunks[2].name[4] = '\0'; - png_write_chunk(png_ptr, tuxpaint_chunks[2].name, tuxpaint_chunks[2].data, tuxpaint_chunks[2].size); - - free(compressed_data); - free(chunk_data); - free(sbk_pixs); + if (SDL_MUSTLOCK(img_starter)) + SDL_UnlockSurface(img_starter); } + if (SDL_MUSTLOCK(img_starter_bkgd)) + SDL_UnlockSurface(img_starter_bkgd); + +#ifdef DEBUG + printf("%d \n", (int) compressedLen); +#endif + + compress(compressed_data, &compressedLen, sbk_pixs, + img_starter_bkgd->h * img_starter_bkgd->w * 3); + + set_chunk_data(&chunk_data, &chunk_data_len, + img_starter_bkgd->w * img_starter_bkgd->h * 3, + compressed_data, compressedLen); +#ifdef DEBUG + printf("%d \n", (int) compressedLen); +#endif + + + tuxpaint_chunks[2].data = (png_byte *) chunk_data; + tuxpaint_chunks[2].size = chunk_data_len; + tuxpaint_chunks[2].location = PNG_HAVE_IHDR; + tuxpaint_chunks[2].name[0] = 't'; + tuxpaint_chunks[2].name[1] = 'p'; + tuxpaint_chunks[2].name[2] = 'B'; + tuxpaint_chunks[2].name[3] = 'G'; + tuxpaint_chunks[2].name[4] = '\0'; + png_write_chunk(png_ptr, tuxpaint_chunks[2].name, tuxpaint_chunks[2].data, + tuxpaint_chunks[2].size); + + free(compressed_data); + free(chunk_data); + free(sbk_pixs); + } + /* Label: diff from label surface to canvas surface */ if (label && are_labels()) + { + sbk_pixs = malloc(label->h * label->w * 4); + compressedLen = (uLongf) compressBound(label->h * label->w * 4); + compressed_data = malloc(compressedLen * sizeof(Bytef *)); + + if (SDL_MUSTLOCK(label)) + SDL_LockSurface(label); + if (SDL_MUSTLOCK(canvas)) + SDL_LockSurface(canvas); + + for (y = 0; y < label->h; y++) { - sbk_pixs = malloc(label->h * label->w * 4); - compressedLen = (uLongf) compressBound(label->h * label->w * 4); - compressed_data = malloc(compressedLen * sizeof(Bytef *)); - - if (SDL_MUSTLOCK(label)) - SDL_LockSurface(label); - if (SDL_MUSTLOCK(canvas)) - SDL_LockSurface(canvas); - - for (y = 0; y < label->h; y++) + for (x = 0; x < label->w; x++) + { + SDL_GetRGBA(getpixels[label->format->BytesPerPixel] (label, x, y), + label->format, &r, &g, &b, &a); + if (a != SDL_ALPHA_TRANSPARENT) { - for (x = 0; x < label->w; x++) - { - SDL_GetRGBA(getpixels[label->format->BytesPerPixel] (label, x, y), label->format, &r, &g, &b, &a); - if (a != SDL_ALPHA_TRANSPARENT) - { - SDL_GetRGB(getpixels[canvas->format->BytesPerPixel] (canvas, x, y), canvas->format, &r, &g, &b); + SDL_GetRGB(getpixels[canvas->format->BytesPerPixel] (canvas, x, y), + canvas->format, &r, &g, &b); - sbk_pixs[4 * (y * label->w + x)] = r; - sbk_pixs[4 * (y * label->w + x) + 1] = g; - sbk_pixs[4 * (y * label->w + x) + 2] = b; - sbk_pixs[4 * (y * label->w + x) + 3] = SDL_ALPHA_OPAQUE; - } - else - { - sbk_pixs[4 * (y * label->w + x)] = SDL_ALPHA_TRANSPARENT; - sbk_pixs[4 * (y * label->w + x) + 1] = SDL_ALPHA_TRANSPARENT; - sbk_pixs[4 * (y * label->w + x) + 2] = SDL_ALPHA_TRANSPARENT; - sbk_pixs[4 * (y * label->w + x) + 3] = SDL_ALPHA_TRANSPARENT; - } - } + sbk_pixs[4 * (y * label->w + x)] = r; + sbk_pixs[4 * (y * label->w + x) + 1] = g; + sbk_pixs[4 * (y * label->w + x) + 2] = b; + sbk_pixs[4 * (y * label->w + x) + 3] = SDL_ALPHA_OPAQUE; } + else + { + sbk_pixs[4 * (y * label->w + x)] = SDL_ALPHA_TRANSPARENT; + sbk_pixs[4 * (y * label->w + x) + 1] = SDL_ALPHA_TRANSPARENT; + sbk_pixs[4 * (y * label->w + x) + 2] = SDL_ALPHA_TRANSPARENT; + sbk_pixs[4 * (y * label->w + x) + 3] = SDL_ALPHA_TRANSPARENT; + } + } + } - if (SDL_MUSTLOCK(label)) - SDL_UnlockSurface(label); - if (SDL_MUSTLOCK(canvas)) - SDL_UnlockSurface(canvas); + if (SDL_MUSTLOCK(label)) + SDL_UnlockSurface(label); + if (SDL_MUSTLOCK(canvas)) + SDL_UnlockSurface(canvas); - compress(compressed_data, &compressedLen, sbk_pixs, canvas->h * canvas->w * 4); - set_chunk_data(&chunk_data, &chunk_data_len, canvas->w * canvas->h * 4, compressed_data, compressedLen); + compress(compressed_data, &compressedLen, sbk_pixs, + canvas->h * canvas->w * 4); + set_chunk_data(&chunk_data, &chunk_data_len, canvas->w * canvas->h * 4, + compressed_data, compressedLen); - tuxpaint_chunks[3].data = chunk_data; - tuxpaint_chunks[3].size = chunk_data_len; - tuxpaint_chunks[3].location = PNG_HAVE_IHDR; - tuxpaint_chunks[3].name[0] = 't'; - tuxpaint_chunks[3].name[1] = 'p'; - tuxpaint_chunks[3].name[2] = 'L'; - tuxpaint_chunks[3].name[3] = 'D'; - tuxpaint_chunks[3].name[4] = '\0'; + tuxpaint_chunks[3].data = chunk_data; + tuxpaint_chunks[3].size = chunk_data_len; + tuxpaint_chunks[3].location = PNG_HAVE_IHDR; + tuxpaint_chunks[3].name[0] = 't'; + tuxpaint_chunks[3].name[1] = 'p'; + tuxpaint_chunks[3].name[2] = 'L'; + tuxpaint_chunks[3].name[3] = 'D'; + tuxpaint_chunks[3].name[4] = '\0'; - png_write_chunk(png_ptr, tuxpaint_chunks[3].name, tuxpaint_chunks[3].data, tuxpaint_chunks[3].size); - free(compressed_data); - free(chunk_data); - free(sbk_pixs); + png_write_chunk(png_ptr, tuxpaint_chunks[3].name, tuxpaint_chunks[3].data, + tuxpaint_chunks[3].size); + free(compressed_data); + free(chunk_data); + free(sbk_pixs); - /* Label data */ + /* Label data */ #ifndef fmemopen_alternative - lfi = open_memstream(&ldata, &size_of_uncompressed_label_data); + lfi = open_memstream(&ldata, &size_of_uncompressed_label_data); #else #ifndef WIN32 - fname = get_fname("tmpfile", DIR_SAVE); + fname = get_fname("tmpfile", DIR_SAVE); #else - fname = get_temp_fname("tmpfile"); + fname = get_temp_fname("tmpfile"); #endif - lfi = fopen(fname, "wb+"); + lfi = fopen(fname, "wb+"); #endif - current_node = current_label_node; - while (current_node != NULL) + current_node = current_label_node; + while (current_node != NULL) + { + if (current_node->is_enabled && current_node->save_texttool_len > 0) + list_ctr = list_ctr + 1; + current_node = current_node->next_to_down_label_node; + } + + fprintf(lfi, "%d\n", list_ctr); + fprintf(lfi, "%d\n", r_canvas.w); + fprintf(lfi, "%d\n\n", r_canvas.h); + + current_node = start_label_node; + while (current_node && current_node != first_label_node_in_redo_stack) + { + if (current_node->is_enabled == TRUE + && current_node->save_texttool_len > 0) + { + fprintf(lfi, "%u\n", current_node->save_texttool_len); + for (i = 0; i < current_node->save_texttool_len; i++) { - if (current_node->is_enabled && current_node->save_texttool_len > 0) - list_ctr = list_ctr + 1; - current_node = current_node->next_to_down_label_node; +#ifdef WIN32 + wtmpchar = current_node->save_texttool_str[i]; + nbtmpstr = + WideCharToMultiByte(CP_UTF8, 0, &wtmpchar, 1, tmpstr, 16, NULL, + NULL); + tmpstr[nbtmpstr] = '\0'; + fprintf(lfi, "%s", tmpstr); +#elif defined(__ANDROID__) + fprintf(lfi, "%d ", (int) current_node->save_texttool_str[i]); +#else + fprintf(lfi, "%lc", (wint_t) current_node->save_texttool_str[i]); +#endif + } + fprintf(lfi, "\n"); + + fprintf(lfi, "%u\n", current_node->save_color.r); + fprintf(lfi, "%u\n", current_node->save_color.g); + fprintf(lfi, "%u\n", current_node->save_color.b); + fprintf(lfi, "%d\n", current_node->save_width); + fprintf(lfi, "%d\n", current_node->save_height); + fprintf(lfi, "%u\n", current_node->save_x); + fprintf(lfi, "%u\n", current_node->save_y); + + if (current_node->save_font_type == NULL) /* Fonts yet setted */ + { + fprintf(lfi, "%d\n", current_node->save_cur_font); + fprintf(lfi, "%s\n", + TTF_FontFaceFamilyName(getfonthandle + (current_node->save_cur_font)-> + ttf_font)); + } + else + { + fprintf(lfi, "%d\n", 0); + fprintf(lfi, "%s\n", current_node->save_font_type); } - fprintf(lfi, "%d\n", list_ctr); - fprintf(lfi, "%d\n", r_canvas.w); - fprintf(lfi, "%d\n\n", r_canvas.h); + fprintf(lfi, "%d\n", current_node->save_text_state); + fprintf(lfi, "%u\n", current_node->save_text_size); - current_node = start_label_node; - while (current_node && current_node != first_label_node_in_redo_stack) - { - if (current_node->is_enabled == TRUE && current_node->save_texttool_len > 0) - { - fprintf(lfi, "%u\n", current_node->save_texttool_len); - for (i = 0; i < current_node->save_texttool_len; i++) - { -#ifdef WIN32 - wtmpchar = current_node->save_texttool_str[i]; - nbtmpstr = WideCharToMultiByte(CP_UTF8, 0, &wtmpchar, 1, tmpstr, 16, NULL, NULL); - tmpstr[nbtmpstr] = '\0'; - fprintf(lfi, "%s", tmpstr); -#elif defined(__ANDROID__) - fprintf(lfi, "%d ", (int)current_node->save_texttool_str[i]); -#else - fprintf(lfi, "%lc", (wint_t) current_node->save_texttool_str[i]); -#endif - } - fprintf(lfi, "\n"); - - fprintf(lfi, "%u\n", current_node->save_color.r); - fprintf(lfi, "%u\n", current_node->save_color.g); - fprintf(lfi, "%u\n", current_node->save_color.b); - fprintf(lfi, "%d\n", current_node->save_width); - fprintf(lfi, "%d\n", current_node->save_height); - fprintf(lfi, "%u\n", current_node->save_x); - fprintf(lfi, "%u\n", current_node->save_y); - - if (current_node->save_font_type == NULL) /* Fonts yet setted */ - { - fprintf(lfi, "%d\n", current_node->save_cur_font); - fprintf(lfi, "%s\n", TTF_FontFaceFamilyName(getfonthandle(current_node->save_cur_font)->ttf_font)); - } - else - { - fprintf(lfi, "%d\n", 0); - fprintf(lfi, "%s\n", current_node->save_font_type); - } - - fprintf(lfi, "%d\n", current_node->save_text_state); - fprintf(lfi, "%u\n", current_node->save_text_size); - - SDL_LockSurface(current_node->label_node_surface); - alpha_size = sizeof(Uint8); - for (x = 0; x < current_node->save_width; x++) - for (y = 0; y < current_node->save_height; y++) - { + SDL_LockSurface(current_node->label_node_surface); + alpha_size = sizeof(Uint8); + for (x = 0; x < current_node->save_width; x++) + for (y = 0; y < current_node->save_height; y++) + { /* *INDENT-OFF* */ pix = getpixels[current_node->label_node_surface->format->BytesPerPixel](current_node->label_node_surface, x, y); /* *INDENT-ON* */ - SDL_GetRGBA(pix, current_label_node->label_node_surface->format, &r, &g, &b, &a); - fwrite(&a, alpha_size, 1, lfi); - } - SDL_UnlockSurface(current_node->label_node_surface); - fprintf(lfi, "\n\n"); - } - current_node = current_node->next_to_up_label_node; + SDL_GetRGBA(pix, current_label_node->label_node_surface->format, + &r, &g, &b, &a); + fwrite(&a, alpha_size, 1, lfi); + } + SDL_UnlockSurface(current_node->label_node_surface); + fprintf(lfi, "\n\n"); + } + current_node = current_node->next_to_up_label_node; #ifdef DEBUG - printf("cur %p, red %p\n", current_node, first_label_node_in_redo_stack); + printf("cur %p, red %p\n", current_node, + first_label_node_in_redo_stack); #endif - } + } #ifdef fmemopen_alternative - size_of_uncompressed_label_data = ftell(lfi); - rewind(lfi); - ldata = malloc(size_of_uncompressed_label_data); - for (i = 0; i < size_of_uncompressed_label_data; i++) - fread(&ldata[i], 1, 1, lfi); + size_of_uncompressed_label_data = ftell(lfi); + rewind(lfi); + ldata = malloc(size_of_uncompressed_label_data); + for (i = 0; i < size_of_uncompressed_label_data; i++) + fread(&ldata[i], 1, 1, lfi); #endif - fclose(lfi); + fclose(lfi); - compressedLen = compressBound(size_of_uncompressed_label_data); - compressed_data = malloc(compressedLen * sizeof(Bytef *)); - compress((Bytef *) compressed_data, &compressedLen, (unsigned char *)ldata, size_of_uncompressed_label_data); - set_chunk_data(&chunk_data, &chunk_data_len, size_of_uncompressed_label_data, compressed_data, compressedLen); + compressedLen = compressBound(size_of_uncompressed_label_data); + compressed_data = malloc(compressedLen * sizeof(Bytef *)); + compress((Bytef *) compressed_data, &compressedLen, + (unsigned char *) ldata, size_of_uncompressed_label_data); + set_chunk_data(&chunk_data, &chunk_data_len, + size_of_uncompressed_label_data, compressed_data, + compressedLen); - tuxpaint_chunks[4].data = chunk_data; - tuxpaint_chunks[4].size = chunk_data_len; - tuxpaint_chunks[4].location = PNG_HAVE_IHDR; - tuxpaint_chunks[4].name[0] = 't'; - tuxpaint_chunks[4].name[1] = 'p'; - tuxpaint_chunks[4].name[2] = 'L'; - tuxpaint_chunks[4].name[3] = 'L'; - tuxpaint_chunks[4].name[4] = '\0'; + tuxpaint_chunks[4].data = chunk_data; + tuxpaint_chunks[4].size = chunk_data_len; + tuxpaint_chunks[4].location = PNG_HAVE_IHDR; + tuxpaint_chunks[4].name[0] = 't'; + tuxpaint_chunks[4].name[1] = 'p'; + tuxpaint_chunks[4].name[2] = 'L'; + tuxpaint_chunks[4].name[3] = 'L'; + tuxpaint_chunks[4].name[4] = '\0'; - png_write_chunk(png_ptr, tuxpaint_chunks[4].name, tuxpaint_chunks[4].data, tuxpaint_chunks[4].size); + png_write_chunk(png_ptr, tuxpaint_chunks[4].name, tuxpaint_chunks[4].data, + tuxpaint_chunks[4].size); - free(compressed_data); - free(chunk_data); - } + free(compressed_data); + free(chunk_data); + } /* Write 'starter' and/or canvas color info, if it's useful to: */ if (starter_id[0] != '\0' || - template_id[0] != '\0' || canvas_color_r != 255 || canvas_color_g != 255 || canvas_color_b != 255) - { - /* Usually the .dat data are less than 100 bytes, hope this keeps line and char_stream in the safe side */ - line_sz = 256; - line = calloc(line_sz, 1); - char_stream_sz = 256 + sizeof(starter_id) + sizeof(template_id), + template_id[0] != '\0' || canvas_color_r != 255 || canvas_color_g != 255 + || canvas_color_b != 255) + { + /* Usually the .dat data are less than 100 bytes, hope this keeps line and char_stream in the safe side */ + line_sz = 256; + line = calloc(line_sz, 1); + char_stream_sz = 256 + sizeof(starter_id) + sizeof(template_id), char_stream = calloc(char_stream_sz, 1); - safe_snprintf(char_stream, char_stream_sz, "%s\n", starter_id); + safe_snprintf(char_stream, char_stream_sz, "%s\n", starter_id); - safe_snprintf(line, line_sz, "%d %d %d\n", starter_mirrored, starter_flipped, starter_personal); - safe_strncat(char_stream, line, char_stream_sz); + safe_snprintf(line, line_sz, "%d %d %d\n", starter_mirrored, + starter_flipped, starter_personal); + safe_strncat(char_stream, line, char_stream_sz); - safe_snprintf(line, line_sz, "c%d %d %d\n", canvas_color_r, canvas_color_g, canvas_color_b); - safe_strncat(char_stream, line, char_stream_sz); + safe_snprintf(line, line_sz, "c%d %d %d\n", canvas_color_r, + canvas_color_g, canvas_color_b); + safe_strncat(char_stream, line, char_stream_sz); - safe_snprintf(line, line_sz, "T%s\n", template_id); - safe_strncat(char_stream, line, char_stream_sz); + safe_snprintf(line, line_sz, "T%s\n", template_id); + safe_strncat(char_stream, line, char_stream_sz); - safe_snprintf(line, line_sz, "%d\n", template_personal); - safe_strncat(char_stream, line, char_stream_sz); + safe_snprintf(line, line_sz, "%d\n", template_personal); + safe_strncat(char_stream, line, char_stream_sz); - safe_snprintf(line, line_sz, "M%d\n", starter_modified); - safe_strncat(char_stream, line, char_stream_sz); + safe_snprintf(line, line_sz, "M%d\n", starter_modified); + safe_strncat(char_stream, line, char_stream_sz); - dat_size = strlen(char_stream); + dat_size = strlen(char_stream); - set_chunk_data(&chunk_data, &chunk_data_len, dat_size, (Bytef *) char_stream, dat_size); + set_chunk_data(&chunk_data, &chunk_data_len, dat_size, + (Bytef *) char_stream, dat_size); - tuxpaint_chunks[4].data = chunk_data; - tuxpaint_chunks[4].size = chunk_data_len; - tuxpaint_chunks[4].location = PNG_HAVE_IHDR; - tuxpaint_chunks[4].name[0] = 't'; - tuxpaint_chunks[4].name[1] = 'p'; - tuxpaint_chunks[4].name[2] = 'D'; - tuxpaint_chunks[4].name[3] = 'T'; - tuxpaint_chunks[4].name[4] = '\0'; + tuxpaint_chunks[4].data = chunk_data; + tuxpaint_chunks[4].size = chunk_data_len; + tuxpaint_chunks[4].location = PNG_HAVE_IHDR; + tuxpaint_chunks[4].name[0] = 't'; + tuxpaint_chunks[4].name[1] = 'p'; + tuxpaint_chunks[4].name[2] = 'D'; + tuxpaint_chunks[4].name[3] = 'T'; + tuxpaint_chunks[4].name[4] = '\0'; - png_write_chunk(png_ptr, tuxpaint_chunks[4].name, tuxpaint_chunks[4].data, tuxpaint_chunks[4].size); + png_write_chunk(png_ptr, tuxpaint_chunks[4].name, tuxpaint_chunks[4].data, + tuxpaint_chunks[4].size); - free(char_stream); - free(line); - free(chunk_data); - } + free(char_stream); + free(line); + free(chunk_data); + } } /** * FIXME */ /* Actually save the PNG data to the file stream: */ -static int do_png_save(FILE * fi, const char *const fname, SDL_Surface * surf, int embed) +static int do_png_save(FILE * fi, const char *const fname, SDL_Surface * surf, + int embed) { png_structp png_ptr; png_infop info_ptr; @@ -15818,11 +16747,23 @@ static int do_png_save(FILE * fi, const char *const fname, SDL_Surface * surf, i Uint8 r, g, b; int x, y, count; - Uint32(*getpixel) (SDL_Surface *, int, int) = getpixels[surf->format->BytesPerPixel]; + Uint32(*getpixel) (SDL_Surface *, int, int) = + getpixels[surf->format->BytesPerPixel]; png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (png_ptr == NULL) + { + fclose(fi); + png_destroy_write_struct(&png_ptr, (png_infopp) NULL); + + fprintf(stderr, "\nError: Couldn't save the image!\n%s\n\n", fname); + draw_tux_text(TUX_OOPS, strerror(errno), 0); + } + else + { + info_ptr = png_create_info_struct(png_ptr); + if (info_ptr == NULL) { fclose(fi); png_destroy_write_struct(&png_ptr, (png_infopp) NULL); @@ -15830,99 +16771,91 @@ static int do_png_save(FILE * fi, const char *const fname, SDL_Surface * surf, i fprintf(stderr, "\nError: Couldn't save the image!\n%s\n\n", fname); draw_tux_text(TUX_OOPS, strerror(errno), 0); } - else + else { - info_ptr = png_create_info_struct(png_ptr); - if (info_ptr == NULL) - { - fclose(fi); - png_destroy_write_struct(&png_ptr, (png_infopp) NULL); + if (setjmp(png_jmpbuf(png_ptr))) + { + fclose(fi); + png_destroy_write_struct(&png_ptr, (png_infopp) NULL); - fprintf(stderr, "\nError: Couldn't save the image!\n%s\n\n", fname); - draw_tux_text(TUX_OOPS, strerror(errno), 0); - } + fprintf(stderr, "\nError: Couldn't save the image!\n%s\n\n", fname); + draw_tux_text(TUX_OOPS, strerror(errno), 0); + + return 0; + } else + { + png_init_io(png_ptr, fi); + + png_set_IHDR(png_ptr, info_ptr, surf->w, surf->h, 8, + PNG_COLOR_TYPE_RGB, 1, PNG_COMPRESSION_TYPE_BASE, + PNG_FILTER_TYPE_BASE); + + png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, + PNG_sRGB_INTENT_PERCEPTUAL); + + /* Set headers */ + + count = 0; + + /* + if (title != NULL && strlen(title) > 0) + { + text_ptr[count].key = "Title"; + text_ptr[count].text = title; + text_ptr[count].compression = PNG_TEXT_COMPRESSION_NONE; + count++; + } + */ + + text_ptr[count].key = (png_charp) "Software"; + text_ptr[count].text = + (png_charp) "Tux Paint " VER_VERSION " (" VER_DATE ")"; + text_ptr[count].compression = PNG_TEXT_COMPRESSION_NONE; + count++; + + + png_set_text(png_ptr, info_ptr, text_ptr, count); + + png_write_info(png_ptr, info_ptr); + + if (embed) + do_png_embed_data(png_ptr); + + /* Save the picture: */ + + png_rows = malloc(sizeof(char *) * surf->h); + + for (y = 0; y < surf->h; y++) { - if (setjmp(png_jmpbuf(png_ptr))) - { - fclose(fi); - png_destroy_write_struct(&png_ptr, (png_infopp) NULL); + png_rows[y] = malloc(sizeof(char) * 3 * surf->w); - fprintf(stderr, "\nError: Couldn't save the image!\n%s\n\n", fname); - draw_tux_text(TUX_OOPS, strerror(errno), 0); + for (x = 0; x < surf->w; x++) + { + SDL_GetRGB(getpixel(surf, x, y), surf->format, &r, &g, &b); - return 0; - } - else - { - png_init_io(png_ptr, fi); - - png_set_IHDR(png_ptr, info_ptr, surf->w, surf->h, 8, PNG_COLOR_TYPE_RGB, 1, PNG_COMPRESSION_TYPE_BASE, - PNG_FILTER_TYPE_BASE); - - png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL); - - /* Set headers */ - - count = 0; - - /* - if (title != NULL && strlen(title) > 0) - { - text_ptr[count].key = "Title"; - text_ptr[count].text = title; - text_ptr[count].compression = PNG_TEXT_COMPRESSION_NONE; - count++; - } - */ - - text_ptr[count].key = (png_charp) "Software"; - text_ptr[count].text = (png_charp) "Tux Paint " VER_VERSION " (" VER_DATE ")"; - text_ptr[count].compression = PNG_TEXT_COMPRESSION_NONE; - count++; - - - png_set_text(png_ptr, info_ptr, text_ptr, count); - - png_write_info(png_ptr, info_ptr); - - if (embed) - do_png_embed_data(png_ptr); - - /* Save the picture: */ - - png_rows = malloc(sizeof(char *) * surf->h); - - for (y = 0; y < surf->h; y++) - { - png_rows[y] = malloc(sizeof(char) * 3 * surf->w); - - for (x = 0; x < surf->w; x++) - { - SDL_GetRGB(getpixel(surf, x, y), surf->format, &r, &g, &b); - - png_rows[y][x * 3 + 0] = r; - png_rows[y][x * 3 + 1] = g; - png_rows[y][x * 3 + 2] = b; - } - } - - png_write_image(png_ptr, png_rows); - - for (y = 0; y < surf->h; y++) - free(png_rows[y]); - - free(png_rows); - - - png_write_end(png_ptr, NULL); - png_destroy_write_struct(&png_ptr, &info_ptr); - fclose(fi); - - return 1; - } + png_rows[y][x * 3 + 0] = r; + png_rows[y][x * 3 + 1] = g; + png_rows[y][x * 3 + 2] = b; + } } + + png_write_image(png_ptr, png_rows); + + for (y = 0; y < surf->h; y++) + free(png_rows[y]); + + free(png_rows); + + + png_write_end(png_ptr, NULL); + png_destroy_write_struct(&png_ptr, &info_ptr); + fclose(fi); + + return 1; + } } + } return 0; } @@ -15954,49 +16887,52 @@ static int do_quit(int tool) int done, tmp_tool, scroll; if (!no_prompt_on_quit) - { - scroll = (NUM_TOOLS > buttons_tall * gd_tools.cols) ? img_scroll_down->h : 0; - done = do_prompt_snd(PROMPT_QUIT_TXT, - PROMPT_QUIT_YES, PROMPT_QUIT_NO, SND_AREYOUSURE, - (TOOL_QUIT % 2) * button_w + button_w / 2, - (TOOL_QUIT / 2) * button_h + r_ttools.h + button_h / 2 - tool_scroll * button_h / gd_tools.cols + scroll); - } + { + scroll = + (NUM_TOOLS > buttons_tall * gd_tools.cols) ? img_scroll_down->h : 0; + done = + do_prompt_snd(PROMPT_QUIT_TXT, PROMPT_QUIT_YES, PROMPT_QUIT_NO, + SND_AREYOUSURE, (TOOL_QUIT % 2) * button_w + button_w / 2, + (TOOL_QUIT / 2) * button_h + r_ttools.h + button_h / 2 - + tool_scroll * button_h / gd_tools.cols + scroll); + } else - { - done = 1; - } + { + done = 1; + } if (done && !been_saved && !disable_save) + { + if (autosave_on_quit || + do_prompt(PROMPT_QUIT_SAVE_TXT, PROMPT_QUIT_SAVE_YES, + PROMPT_QUIT_SAVE_NO, screen->w / 2, screen->h / 2)) { - if (autosave_on_quit || - do_prompt(PROMPT_QUIT_SAVE_TXT, PROMPT_QUIT_SAVE_YES, PROMPT_QUIT_SAVE_NO, screen->w / 2, screen->h / 2)) - { - if (do_save(tool, 1, 0)) - { - /* Don't bug user about successful save when quitting -bjk 2007.05.15 */ - /* do_prompt(tool_tips[TOOL_SAVE], "OK", ""); */ - } - else - { - /* Couldn't save! Abort quit! */ + if (do_save(tool, 1, 0)) + { + /* Don't bug user about successful save when quitting -bjk 2007.05.15 */ + /* do_prompt(tool_tips[TOOL_SAVE], "OK", ""); */ + } + else + { + /* Couldn't save! Abort quit! */ - done = 0; - } - } + done = 0; + } } + } else - { - if (tool == TOOL_TEXT || tool == TOOL_LABEL) - do_render_cur_text(0); + { + if (tool == TOOL_TEXT || tool == TOOL_LABEL) + do_render_cur_text(0); - /* Bring back stamp sound effects and speak buttons, if we were in - Stamps tool: */ + /* Bring back stamp sound effects and speak buttons, if we were in + Stamps tool: */ - tmp_tool = cur_tool; - cur_tool = tool; - draw_tux_text(TUX_BORED, "", 0); - cur_tool = tmp_tool; - } + tmp_tool = cur_tool; + cur_tool = tool; + draw_tux_text(TUX_BORED, "", 0); + cur_tool = tmp_tool; + } if (done) SDL_JoystickClose(joystick); return (done); @@ -16052,1156 +16988,1289 @@ static int do_open(void) opened_something = 0; do + { + do_setcursor(cursor_watch); + + /* Allocate some space: */ + + things_alloced = 32; + + fs = (struct dirent2 *) malloc(sizeof(struct dirent2) * things_alloced); + + num_files = 0; + cur = 0; + which = 0; + slideshow = 0; + num_files_in_dirs = 0; + any_saved_files = 0; + + + /* Open directories of images: */ + + for (places_to_look = 0; places_to_look < NUM_PLACES_TO_LOOK; + places_to_look++) { - do_setcursor(cursor_watch); + if (places_to_look == PLACE_SAVED_DIR) + { + /* Saved-images: */ - /* Allocate some space: */ - - things_alloced = 32; - - fs = (struct dirent2 *)malloc(sizeof(struct dirent2) * things_alloced); - - num_files = 0; - cur = 0; - which = 0; - slideshow = 0; - num_files_in_dirs = 0; - any_saved_files = 0; - - - /* Open directories of images: */ - - for (places_to_look = 0; places_to_look < NUM_PLACES_TO_LOOK; places_to_look++) - { - if (places_to_look == PLACE_SAVED_DIR) - { - /* Saved-images: */ - - dirname[places_to_look] = get_fname("saved", DIR_SAVE); - } - else if (places_to_look == PLACE_PERSONAL_STARTERS_DIR) - { - /* Starters handled by New dialog... */ - dirname[places_to_look] = NULL; - continue; - } - else if (places_to_look == PLACE_STARTERS_DIR) - { - /* Starters handled by New dialog... */ - dirname[places_to_look] = NULL; - continue; - } - else if (places_to_look == PLACE_PERSONAL_TEMPLATES_DIR) - { - /* Templates handled by New dialog... */ - dirname[places_to_look] = NULL; - continue; - } - else if (places_to_look == PLACE_TEMPLATES_DIR) - { - /* Templates handled by New dialog... */ - dirname[places_to_look] = NULL; - continue; - } - - - /* Read directory of images and build thumbnails: */ - - d = opendir(dirname[places_to_look]); - - if (d != NULL) - { - /* Gather list of files (for sorting): */ - - do - { - f = readdir(d); - - if (f != NULL) - { - memcpy(&(fs[num_files_in_dirs].f), f, sizeof(struct dirent)); - fs[num_files_in_dirs].place = places_to_look; - - num_files_in_dirs++; - - if (places_to_look == PLACE_SAVED_DIR) - any_saved_files = 1; - - if (num_files_in_dirs >= things_alloced) - { - things_alloced = things_alloced + 32; - - /* FIXME: Valgrind says this is leaked -bjk 2007.07.19 */ - fs = (struct dirent2 *)realloc(fs, sizeof(struct dirent2) * things_alloced); - } - } - } - while (f != NULL); - - closedir(d); - } - } - - - /* (Re)allocate space for the information about these files: */ - - thumbs = (SDL_Surface * *)malloc(sizeof(SDL_Surface *) * num_files_in_dirs); - d_places = (int *)malloc(sizeof(int) * num_files_in_dirs); - d_names = (char **)malloc(sizeof(char *) * num_files_in_dirs); - d_exts = (char **)malloc(sizeof(char *) * num_files_in_dirs); - - - /* Sort: */ - - if (!reversesort) - qsort(fs, num_files_in_dirs, sizeof(struct dirent2), (int (*)(const void *, const void *))compare_dirent2s); - else - qsort(fs, num_files_in_dirs, sizeof(struct dirent2), (int (*)(const void *, const void *))compare_dirent2s_invert); + dirname[places_to_look] = get_fname("saved", DIR_SAVE); + } + else if (places_to_look == PLACE_PERSONAL_STARTERS_DIR) + { + /* Starters handled by New dialog... */ + dirname[places_to_look] = NULL; + continue; + } + else if (places_to_look == PLACE_STARTERS_DIR) + { + /* Starters handled by New dialog... */ + dirname[places_to_look] = NULL; + continue; + } + else if (places_to_look == PLACE_PERSONAL_TEMPLATES_DIR) + { + /* Templates handled by New dialog... */ + dirname[places_to_look] = NULL; + continue; + } + else if (places_to_look == PLACE_TEMPLATES_DIR) + { + /* Templates handled by New dialog... */ + dirname[places_to_look] = NULL; + continue; + } /* Read directory of images and build thumbnails: */ - for (j = 0; j < num_files_in_dirs; j++) - { - f = &(fs[j].f); - place = fs[j].place; + d = opendir(dirname[places_to_look]); - show_progress_bar(screen); + if (d != NULL) + { + /* Gather list of files (for sorting): */ + + do + { + f = readdir(d); if (f != NULL) + { + memcpy(&(fs[num_files_in_dirs].f), f, sizeof(struct dirent)); + fs[num_files_in_dirs].place = places_to_look; + + num_files_in_dirs++; + + if (places_to_look == PLACE_SAVED_DIR) + any_saved_files = 1; + + if (num_files_in_dirs >= things_alloced) { - debug(f->d_name); + things_alloced = things_alloced + 32; - if (strcasestr(f->d_name, "-t.") == NULL && strcasestr(f->d_name, "-back.") == NULL) - { - if (strcasestr(f->d_name, FNAME_EXTENSION) != NULL - /* Support legacy BMP files for load: */ - || strcasestr(f->d_name, ".bmp") != NULL) - { - safe_strncpy(fname, f->d_name, sizeof(fname)); - if (strcasestr(fname, FNAME_EXTENSION) != NULL) - { - d_exts[num_files] = strdup(strcasestr(fname, FNAME_EXTENSION)); - strcpy((char *)strcasestr(fname, FNAME_EXTENSION), ""); /* Safe; truncating */ - } - - if (strcasestr(fname, ".bmp") != NULL) - { - d_exts[num_files] = strdup(strcasestr(fname, ".bmp")); - strcpy((char *)strcasestr(fname, ".bmp"), ""); /* Safe; truncating */ - } - - d_names[num_files] = strdup(fname); - d_places[num_files] = place; - - - /* Is it the 'current' file we just loaded? - We'll make it the current selection! */ - - if (strcmp(d_names[num_files], file_id) == 0) - { - which = num_files; - cur = (which / 4) * 4; - - /* Center the cursor (useful for when the last item is - selected first!) */ - - if (cur - 8 >= 0) - cur = cur - 8; - else if (cur - 4 >= 0) - cur = cur - 4; - } - - - /* Try to load thumbnail first: */ - - safe_snprintf(fname, sizeof(fname), "%s/.thumbs/%s-t.png", - dirname[d_places[num_files]], d_names[num_files]); - debug(fname); - img = IMG_Load(fname); - - if (img == NULL) - { - /* No thumbnail in the new location ("saved/.thumbs"), - try the old locatin ("saved/"): */ - - safe_snprintf(fname, sizeof(fname), "%s/%s-t.png", - dirname[d_places[num_files]], d_names[num_files]); - debug(fname); - - img = IMG_Load(fname); - } - - if (img != NULL) - { - /* Loaded the thumbnail from one or the other location */ - show_progress_bar(screen); - - img1 = SDL_DisplayFormat(img); - SDL_FreeSurface(img); - - /* if too big, or too small in both dimensions, rescale it - (for now: using old thumbnail as source for high speed, low quality) */ - if (img1->w > THUMB_W - 20 || img1->h > THUMB_H - 20 - || (img1->w < THUMB_W - 20 && img1->h < THUMB_H - 20)) - { - img2 = thumbnail(img1, THUMB_W - 20, THUMB_H - 20, 0); - SDL_FreeSurface(img1); - img1 = img2; - } - - thumbs[num_files] = img1; - - if (thumbs[num_files] == NULL) - { - fprintf(stderr, - "\nError: Couldn't create a thumbnail of " "saved image!\n" "%s\n", fname); - } - - num_files++; - } - else - { - /* No thumbnail - load original: */ - - /* Make sure we have a ~/.tuxpaint/saved directory: */ - if (make_directory(DIR_SAVE, "saved", "Can't create user data directory (for saved drawings) (E006)")) - { - /* (Make sure we have a .../saved/.thumbs/ directory:) */ - make_directory(DIR_SAVE, "saved/.thumbs", "Can't create user data thumbnail directory (for saved drawings' thumbnails) (E007)"); - } - - - if (img == NULL) - { - safe_snprintf(fname, sizeof(fname), "%s/%s", dirname[d_places[num_files]], f->d_name); - debug(fname); - img = myIMG_Load(fname); - } - - - show_progress_bar(screen); - - if (img == NULL) - { - fprintf(stderr, - "\nWarning: I can't open one of the saved files!\n" - "%s\n" - "The Simple DirectMedia Layer error that " - "occurred was:\n" "%s\n\n", fname, SDL_GetError()); - - free(d_names[num_files]); - free(d_exts[num_files]); - } - else - { - /* Turn it into a thumbnail: */ - - img1 = SDL_DisplayFormatAlpha(img); - img2 = thumbnail2(img1, THUMB_W - 20, THUMB_H - 20, 0, 0); - SDL_FreeSurface(img1); - - show_progress_bar(screen); - - thumbs[num_files] = SDL_DisplayFormat(img2); - SDL_FreeSurface(img2); - if (thumbs[num_files] == NULL) - { - fprintf(stderr, - "\nError: Couldn't create a thumbnail of " "saved image!\n" "%s\n", fname); - } - - SDL_FreeSurface(img); - - show_progress_bar(screen); - - - /* Let's save this thumbnail, so we don't have to - create it again next time 'Open' is called: */ - - if (d_places[num_files] == PLACE_SAVED_DIR) - { - debug("Saving thumbnail for this one!"); - - safe_snprintf(fname, sizeof(fname), "%s/.thumbs/%s-t.png", - dirname[d_places[num_files]], d_names[num_files]); - - fi = fopen(fname, "wb"); - if (fi == NULL) - { - fprintf(stderr, - "\nError: Couldn't save thumbnail of " - "saved image!\n" - "%s\n" "The error that occurred was:\n" "%s\n\n", fname, strerror(errno)); - } - else - { - do_png_save(fi, fname, thumbs[num_files], 0); - } - - show_progress_bar(screen); - } - - - num_files++; - } - } - } - } - else - { - /* It was a thumbnail file ("...-t.png") */ - } + /* FIXME: Valgrind says this is leaked -bjk 2007.07.19 */ + fs = + (struct dirent2 *) realloc(fs, + sizeof(struct dirent2) * + things_alloced); } + } } + while (f != NULL); + + closedir(d); + } + } + + + /* (Re)allocate space for the information about these files: */ + + thumbs = + (SDL_Surface * *)malloc(sizeof(SDL_Surface *) * num_files_in_dirs); + d_places = (int *) malloc(sizeof(int) * num_files_in_dirs); + d_names = (char **) malloc(sizeof(char *) * num_files_in_dirs); + d_exts = (char **) malloc(sizeof(char *) * num_files_in_dirs); + + + /* Sort: */ + + if (!reversesort) + qsort(fs, num_files_in_dirs, sizeof(struct dirent2), + (int (*)(const void *, const void *)) compare_dirent2s); + else + qsort(fs, num_files_in_dirs, sizeof(struct dirent2), + (int (*)(const void *, const void *)) compare_dirent2s_invert); + + + /* Read directory of images and build thumbnails: */ + + for (j = 0; j < num_files_in_dirs; j++) + { + f = &(fs[j].f); + place = fs[j].place; + + show_progress_bar(screen); + + if (f != NULL) + { + debug(f->d_name); + + if (strcasestr(f->d_name, "-t.") == NULL + && strcasestr(f->d_name, "-back.") == NULL) + { + if (strcasestr(f->d_name, FNAME_EXTENSION) != NULL + /* Support legacy BMP files for load: */ + || strcasestr(f->d_name, ".bmp") != NULL) + { + safe_strncpy(fname, f->d_name, sizeof(fname)); + if (strcasestr(fname, FNAME_EXTENSION) != NULL) + { + d_exts[num_files] = strdup(strcasestr(fname, FNAME_EXTENSION)); + strcpy((char *) strcasestr(fname, FNAME_EXTENSION), ""); /* Safe; truncating */ + } + + if (strcasestr(fname, ".bmp") != NULL) + { + d_exts[num_files] = strdup(strcasestr(fname, ".bmp")); + strcpy((char *) strcasestr(fname, ".bmp"), ""); /* Safe; truncating */ + } + + d_names[num_files] = strdup(fname); + d_places[num_files] = place; + + + /* Is it the 'current' file we just loaded? + We'll make it the current selection! */ + + if (strcmp(d_names[num_files], file_id) == 0) + { + which = num_files; + cur = (which / 4) * 4; + + /* Center the cursor (useful for when the last item is + selected first!) */ + + if (cur - 8 >= 0) + cur = cur - 8; + else if (cur - 4 >= 0) + cur = cur - 4; + } + + + /* Try to load thumbnail first: */ + + safe_snprintf(fname, sizeof(fname), "%s/.thumbs/%s-t.png", + dirname[d_places[num_files]], d_names[num_files]); + debug(fname); + img = IMG_Load(fname); + + if (img == NULL) + { + /* No thumbnail in the new location ("saved/.thumbs"), + try the old locatin ("saved/"): */ + + safe_snprintf(fname, sizeof(fname), "%s/%s-t.png", + dirname[d_places[num_files]], d_names[num_files]); + debug(fname); + + img = IMG_Load(fname); + } + + if (img != NULL) + { + /* Loaded the thumbnail from one or the other location */ + show_progress_bar(screen); + + img1 = SDL_DisplayFormat(img); + SDL_FreeSurface(img); + + /* if too big, or too small in both dimensions, rescale it + (for now: using old thumbnail as source for high speed, low quality) */ + if (img1->w > THUMB_W - 20 || img1->h > THUMB_H - 20 + || (img1->w < THUMB_W - 20 && img1->h < THUMB_H - 20)) + { + img2 = thumbnail(img1, THUMB_W - 20, THUMB_H - 20, 0); + SDL_FreeSurface(img1); + img1 = img2; + } + + thumbs[num_files] = img1; + + if (thumbs[num_files] == NULL) + { + fprintf(stderr, + "\nError: Couldn't create a thumbnail of " + "saved image!\n" "%s\n", fname); + } + + num_files++; + } + else + { + /* No thumbnail - load original: */ + + /* Make sure we have a ~/.tuxpaint/saved directory: */ + if (make_directory + (DIR_SAVE, "saved", + "Can't create user data directory (for saved drawings) (E006)")) + { + /* (Make sure we have a .../saved/.thumbs/ directory:) */ + make_directory(DIR_SAVE, "saved/.thumbs", + "Can't create user data thumbnail directory (for saved drawings' thumbnails) (E007)"); + } + + + if (img == NULL) + { + safe_snprintf(fname, sizeof(fname), "%s/%s", + dirname[d_places[num_files]], f->d_name); + debug(fname); + img = myIMG_Load(fname); + } + + + show_progress_bar(screen); + + if (img == NULL) + { + fprintf(stderr, + "\nWarning: I can't open one of the saved files!\n" + "%s\n" + "The Simple DirectMedia Layer error that " + "occurred was:\n" "%s\n\n", fname, SDL_GetError()); + + free(d_names[num_files]); + free(d_exts[num_files]); + } + else + { + /* Turn it into a thumbnail: */ + + img1 = SDL_DisplayFormatAlpha(img); + img2 = thumbnail2(img1, THUMB_W - 20, THUMB_H - 20, 0, 0); + SDL_FreeSurface(img1); + + show_progress_bar(screen); + + thumbs[num_files] = SDL_DisplayFormat(img2); + SDL_FreeSurface(img2); + if (thumbs[num_files] == NULL) + { + fprintf(stderr, + "\nError: Couldn't create a thumbnail of " + "saved image!\n" "%s\n", fname); + } + + SDL_FreeSurface(img); + + show_progress_bar(screen); + + + /* Let's save this thumbnail, so we don't have to + create it again next time 'Open' is called: */ + + if (d_places[num_files] == PLACE_SAVED_DIR) + { + debug("Saving thumbnail for this one!"); + + safe_snprintf(fname, sizeof(fname), "%s/.thumbs/%s-t.png", + dirname[d_places[num_files]], + d_names[num_files]); + + fi = fopen(fname, "wb"); + if (fi == NULL) + { + fprintf(stderr, + "\nError: Couldn't save thumbnail of " + "saved image!\n" + "%s\n" "The error that occurred was:\n" "%s\n\n", + fname, strerror(errno)); + } + else + { + do_png_save(fi, fname, thumbs[num_files], 0); + } + + show_progress_bar(screen); + } + + + num_files++; + } + } + } + } + else + { + /* It was a thumbnail file ("...-t.png") */ + } + } + } #ifdef DEBUG - printf("%d saved files were found!\n", num_files); + printf("%d saved files were found!\n", num_files); #endif - if (num_files == 0) + if (num_files == 0) + { + do_prompt_snd(PROMPT_OPEN_NOFILES_TXT, PROMPT_OPEN_NOFILES_YES, "", + SND_YOUCANNOT, + (TOOL_OPEN % 2) * button_w + button_w / 2, + (TOOL_OPEN / 2) * button_h + r_ttools.h + button_h / 2 - + tool_scroll * button_h / gd_tools.cols); + } + else + { + /* Let user choose an image: */ + + /* Instructions for 'Open' file dialog */ + char *instructions = + textdir(gettext_noop + ("Choose the picture you want, then click “Open”.")); + draw_tux_text(TUX_BORED, instructions, 1); + + /* NOTE: cur is now set above; if file_id'th file is found, it's + set to that file's index; otherwise, we default to '0' */ + + update_list = 1; + want_erase = 0; + want_export = 0; + + done = 0; + slideshow = 0; + + last_click_which = -1; + last_click_time = 0; + last_click_button = -1; + + + do_setcursor(cursor_arrow); + + + do + { + /* Update screen: */ + + if (update_list) { - do_prompt_snd(PROMPT_OPEN_NOFILES_TXT, PROMPT_OPEN_NOFILES_YES, "", - SND_YOUCANNOT, - (TOOL_OPEN % 2) * button_w + button_w / 2, - (TOOL_OPEN / 2) * button_h + r_ttools.h + button_h / 2 - tool_scroll * button_h / gd_tools.cols); - } - else - { - /* Let user choose an image: */ + /* Erase screen: */ - /* Instructions for 'Open' file dialog */ - char *instructions = textdir(gettext_noop("Choose the picture you want, then click “Open”.")); - draw_tux_text(TUX_BORED, instructions, 1); + dest.x = r_ttools.w; + dest.y = 0; + dest.w = WINDOW_WIDTH - r_ttoolopt.w - r_ttools.w; + dest.h = button_h * buttons_tall + r_ttools.h; - /* NOTE: cur is now set above; if file_id'th file is found, it's - set to that file's index; otherwise, we default to '0' */ - - update_list = 1; - want_erase = 0; - want_export = 0; - - done = 0; - slideshow = 0; - - last_click_which = -1; - last_click_time = 0; - last_click_button = -1; + SDL_FillRect(screen, &dest, + SDL_MapRGB(screen->format, 255, 255, 255)); - do_setcursor(cursor_arrow); + /* Draw icons: */ + for (i = cur; i < cur + 16 && i < num_files; i++) + { + /* Draw cursor: */ - do + dest.x = THUMB_W * ((i - cur) % 4) + r_ttools.w; + dest.y = THUMB_H * ((i - cur) / 4) + img_scroll_up->h; + + if (i == which) { - /* Update screen: */ + SDL_BlitSurface(img_cursor_down, NULL, screen, &dest); + debug(d_names[i]); + } + else + SDL_BlitSurface(img_cursor_up, NULL, screen, &dest); - if (update_list) + + + dest.x = + THUMB_W * ((i - cur) % 4) + r_ttools.w + 10 + (THUMB_W - 20 - + thumbs[i]->w) / + 2; + dest.y = + THUMB_H * ((i - cur) / 4) + img_scroll_up->h + 10 + (THUMB_H - + 20 - + thumbs + [i]->h) / + 2; + + if (thumbs[i] != NULL) + SDL_BlitSurface(thumbs[i], NULL, screen, &dest); + } + + + /* Draw arrows: */ + + dest.x = (WINDOW_WIDTH - img_scroll_up->w) / 2; + dest.y = 0; + + if (cur > 0) + SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); + else + SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); + + dest.x = (WINDOW_WIDTH - img_scroll_up->w) / 2; + dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; + + if (cur < num_files - 16) + SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + else + SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); + + + /* "Open" button: */ + + dest.x = r_ttools.w; + dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; + SDL_BlitSurface(img_open, NULL, screen, &dest); + + dest.x = r_ttools.w + (button_w - img_openlabels_open->w) / 2; + dest.y = + (button_h * buttons_tall + r_ttools.h) - img_openlabels_open->h; + SDL_BlitSurface(img_openlabels_open, NULL, screen, &dest); + + + /* "Slideshow" button: */ + + dest.x = r_ttools.w + button_w; + dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; + if (any_saved_files) + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + else + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + + dest.x = r_ttools.w + button_w; + dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; + SDL_BlitSurface(img_slideshow, NULL, screen, &dest); + + dest.x = + r_ttools.w + button_w + (button_w - + img_openlabels_slideshow->w) / 2; + dest.y = + (button_h * buttons_tall + r_ttools.h) - + img_openlabels_slideshow->h; + SDL_BlitSurface(img_openlabels_slideshow, NULL, screen, &dest); + + + /* "Back" button: */ + + dest.x = WINDOW_WIDTH - r_ttoolopt.w - button_w; + dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; + SDL_BlitSurface(img_back, NULL, screen, &dest); + + dest.x = + WINDOW_WIDTH - r_ttoolopt.w - button_w + (button_w - + img_openlabels_back->w) + / 2; + dest.y = + (button_h * buttons_tall + r_ttools.h) - img_openlabels_back->h; + SDL_BlitSurface(img_openlabels_back, NULL, screen, &dest); + + + /* "Export" button: */ + + dest.x = + WINDOW_WIDTH - r_ttoolopt.w - button_w - button_w - button_w; + dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; + + if (d_places[which] != PLACE_STARTERS_DIR + && d_places[which] != PLACE_PERSONAL_STARTERS_DIR) + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + else + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + + dest.x = + WINDOW_WIDTH - r_ttoolopt.w - button_w - button_w - button_w + + (button_w - img_pict_export->w) / 2; + dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; + SDL_BlitSurface(img_pict_export, NULL, screen, &dest); + + dest.x = + WINDOW_WIDTH - r_ttoolopt.w - button_w - button_w - button_w + + (button_w - img_openlabels_pict_export->w) / 2; + dest.y = + (button_h * buttons_tall + r_ttools.h) - + img_openlabels_pict_export->h; + SDL_BlitSurface(img_openlabels_pict_export, NULL, screen, &dest); + + + /* "Erase" button: */ + + dest.x = WINDOW_WIDTH - r_ttoolopt.w - button_w - button_w; + dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; + + if (d_places[which] != PLACE_STARTERS_DIR + && d_places[which] != PLACE_PERSONAL_STARTERS_DIR) + SDL_BlitSurface(img_erase, NULL, screen, &dest); + else + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + + dest.x = + WINDOW_WIDTH - r_ttoolopt.w - button_w - button_w + (button_w - + img_openlabels_erase-> + w) / 2; + dest.y = + (button_h * buttons_tall + r_ttools.h) - img_openlabels_erase->h; + SDL_BlitSurface(img_openlabels_erase, NULL, screen, &dest); + + + SDL_Flip(screen); + + update_list = 0; + } + + while (SDL_PollEvent(&event)) + { + if (event.type == SDL_QUIT) + { + done = 1; + + /* FIXME: Handle SDL_Quit better */ + } + else if (event.type == SDL_WINDOWEVENT) + { + handle_active(&event); + } + else if (event.type == SDL_KEYUP) + { + key = event.key.keysym.sym; + + handle_keymouse(key, SDL_KEYUP, 24, NULL, NULL); + } + else if (event.type == SDL_KEYDOWN) + { + key = event.key.keysym.sym; + + handle_keymouse(key, SDL_KEYDOWN, 24, NULL, NULL); + + /* This was interfering with handle_keymouse above, + remapping from LEFT RIGHT UP DOWN to F11 F12 F8 F7 */ + if (key == SDLK_F11) + { + if (which > 0) + { + which--; + + if (which < cur) + cur = cur - 4; + + update_list = 1; + } + } + else if (key == SDLK_F12) + { + if (which < num_files - 1) + { + which++; + + if (which >= cur + 16) + cur = cur + 4; + + update_list = 1; + } + } + else if (key == SDLK_F8) + { + if (which >= 0) + { + which = which - 4; + + if (which < 0) + which = 0; + + if (which < cur) + cur = cur - 4; + + update_list = 1; + } + } + else if (key == SDLK_F7) + { + if (which < num_files) + { + which = which + 4; + + if (which >= num_files) + which = num_files - 1; + + if (which >= cur + 16) + cur = cur + 4; + + update_list = 1; + } + } + else if (key == SDLK_RETURN) /* space also conflicts with handle_keymouse || key == SDLK_SPACE) */ + { + /* Open */ + + done = 1; + playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); + } + else if (key == SDLK_ESCAPE || key == SDLK_AC_BACK) + { + /* Go back: */ + + which = -1; + done = 1; + playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); + } + else if (key == SDLK_d && + (event.key.keysym.mod & KMOD_CTRL) && + d_places[which] != PLACE_STARTERS_DIR && + d_places[which] != PLACE_PERSONAL_STARTERS_DIR + && !noshortcuts) + { + /* Delete! */ + + want_erase = 1; + } + } + else + if ((event.type == SDL_MOUSEBUTTONDOWN + && valid_click(event.button.button)) + || event.type == TP_SDL_MOUSEBUTTONSCROLL) + { + if (event.button.x >= r_ttools.w + && event.button.x < WINDOW_WIDTH - r_ttoolopt.w + && event.button.y >= img_scroll_up->h + && event.button.y < + (button_h * buttons_tall + r_ttools.h) - button_h) + { + /* Picked an icon! */ + + int old_which = which; + + which = + ((event.button.x - r_ttools.w) / (THUMB_W) + + (((event.button.y - img_scroll_up->h) / THUMB_H) * 4)) + cur; + + if (which < num_files) + { + playsound(screen, 1, SND_BLEEP, 1, event.button.x, + SNDDIST_NEAR); + update_list = 1; + + + if (which == last_click_which && + SDL_GetTicks() < last_click_time + 1000 + && event.button.button == last_click_button) { - /* Erase screen: */ + /* Double-click! */ - dest.x = r_ttools.w; - dest.y = 0; - dest.w = WINDOW_WIDTH - r_ttoolopt.w - r_ttools.w; - dest.h = button_h * buttons_tall + r_ttools.h; + done = 1; + } - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 255, 255, 255)); + last_click_which = which; + last_click_time = SDL_GetTicks(); + last_click_button = event.button.button; + } + else + which = old_which; + } + else if (event.button.x >= (WINDOW_WIDTH - img_scroll_up->w) / 2 + && event.button.x <= + (WINDOW_WIDTH + img_scroll_up->w) / 2) + { + if (event.button.y < img_scroll_up->h || + (event.button.y >= + (button_h * buttons_tall + r_ttools.h) - button_h + && event.button.y < + (button_h * buttons_tall + r_ttools.h) - img_scroll_up->h)) + { + /* Up or down scroll button in Open dialog: */ - - /* Draw icons: */ - - for (i = cur; i < cur + 16 && i < num_files; i++) - { - /* Draw cursor: */ - - dest.x = THUMB_W * ((i - cur) % 4) + r_ttools.w; - dest.y = THUMB_H * ((i - cur) / 4) + img_scroll_up->h; - - if (i == which) - { - SDL_BlitSurface(img_cursor_down, NULL, screen, &dest); - debug(d_names[i]); - } - else - SDL_BlitSurface(img_cursor_up, NULL, screen, &dest); - - - - dest.x = THUMB_W * ((i - cur) % 4) + r_ttools.w + 10 + (THUMB_W - 20 - thumbs[i]->w) / 2; - dest.y = THUMB_H * ((i - cur) / 4) + img_scroll_up->h + 10 + (THUMB_H - 20 - thumbs[i]->h) / 2; - - if (thumbs[i] != NULL) - SDL_BlitSurface(thumbs[i], NULL, screen, &dest); - } - - - /* Draw arrows: */ - - dest.x = (WINDOW_WIDTH - img_scroll_up->w) / 2; - dest.y = 0; + if (event.button.y < img_scroll_up->h) + { + /* Up scroll button in Open dialog: */ if (cur > 0) - SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); - else - SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); + { + cur = cur - 4; + update_list = 1; + playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, + SNDDIST_NEAR); - dest.x = (WINDOW_WIDTH - img_scroll_up->w) / 2; - dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; + if (cur == 0) + do_setcursor(cursor_arrow); + } + + if (which >= cur + 16) + which = which - 4; + } + else if (event.button.y >= + (button_h * buttons_tall + r_ttools.h) - button_h + && event.button.y < + (button_h * buttons_tall + r_ttools.h) - + img_scroll_up->h) + { + /* Down scroll button in Open dialog: */ if (cur < num_files - 16) - SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); - else - SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); + { + cur = cur + 4; + update_list = 1; + playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, + SNDDIST_NEAR); + if (cur >= num_files - 16) + do_setcursor(cursor_arrow); + } - /* "Open" button: */ - - dest.x = r_ttools.w; - dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; - SDL_BlitSurface(img_open, NULL, screen, &dest); - - dest.x = r_ttools.w + (button_w - img_openlabels_open->w) / 2; - dest.y = (button_h * buttons_tall + r_ttools.h) - img_openlabels_open->h; - SDL_BlitSurface(img_openlabels_open, NULL, screen, &dest); - - - /* "Slideshow" button: */ - - dest.x = r_ttools.w + button_w; - dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; - if (any_saved_files) - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); - else - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - - dest.x = r_ttools.w + button_w; - dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; - SDL_BlitSurface(img_slideshow, NULL, screen, &dest); - - dest.x = r_ttools.w + button_w + (button_w - img_openlabels_slideshow->w) / 2; - dest.y = (button_h * buttons_tall + r_ttools.h) - img_openlabels_slideshow->h; - SDL_BlitSurface(img_openlabels_slideshow, NULL, screen, &dest); - - - /* "Back" button: */ - - dest.x = WINDOW_WIDTH - r_ttoolopt.w - button_w; - dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; - SDL_BlitSurface(img_back, NULL, screen, &dest); - - dest.x = WINDOW_WIDTH - r_ttoolopt.w - button_w + (button_w - img_openlabels_back->w) / 2; - dest.y = (button_h * buttons_tall + r_ttools.h) - img_openlabels_back->h; - SDL_BlitSurface(img_openlabels_back, NULL, screen, &dest); - - - /* "Export" button: */ - - dest.x = WINDOW_WIDTH - r_ttoolopt.w - button_w - button_w - button_w; - dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; - - if (d_places[which] != PLACE_STARTERS_DIR && d_places[which] != PLACE_PERSONAL_STARTERS_DIR) - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); - else - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - - dest.x = WINDOW_WIDTH - r_ttoolopt.w - button_w - button_w - button_w + (button_w - img_pict_export->w) / 2; - dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; - SDL_BlitSurface(img_pict_export, NULL, screen, &dest); - - dest.x = WINDOW_WIDTH - r_ttoolopt.w - button_w - button_w - button_w + (button_w - img_openlabels_pict_export->w) / 2; - dest.y = (button_h * buttons_tall + r_ttools.h) - img_openlabels_pict_export->h; - SDL_BlitSurface(img_openlabels_pict_export, NULL, screen, &dest); - - - /* "Erase" button: */ - - dest.x = WINDOW_WIDTH - r_ttoolopt.w - button_w - button_w; - dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; - - if (d_places[which] != PLACE_STARTERS_DIR && d_places[which] != PLACE_PERSONAL_STARTERS_DIR) - SDL_BlitSurface(img_erase, NULL, screen, &dest); - else - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - - dest.x = WINDOW_WIDTH - r_ttoolopt.w - button_w - button_w + (button_w - img_openlabels_erase->w) / 2; - dest.y = (button_h * buttons_tall + r_ttools.h) - img_openlabels_erase->h; - SDL_BlitSurface(img_openlabels_erase, NULL, screen, &dest); - - - SDL_Flip(screen); - - update_list = 0; + if (which < cur) + which = which + 4; } - while (SDL_PollEvent(&event)) + if (scrolltimer_dialog != TIMERID_NONE) { - if (event.type == SDL_QUIT) - { - done = 1; - - /* FIXME: Handle SDL_Quit better */ - } - else if (event.type == SDL_WINDOWEVENT) - { - handle_active(&event); - } - else if (event.type == SDL_KEYUP) - { - key = event.key.keysym.sym; - - handle_keymouse(key, SDL_KEYUP, 24, NULL, NULL); - } - else if (event.type == SDL_KEYDOWN) - { - key = event.key.keysym.sym; - - handle_keymouse(key, SDL_KEYDOWN, 24, NULL, NULL); - - /* This was interfering with handle_keymouse above, - remapping from LEFT RIGHT UP DOWN to F11 F12 F8 F7 */ - if (key == SDLK_F11) - { - if (which > 0) - { - which--; - - if (which < cur) - cur = cur - 4; - - update_list = 1; - } - } - else if (key == SDLK_F12) - { - if (which < num_files - 1) - { - which++; - - if (which >= cur + 16) - cur = cur + 4; - - update_list = 1; - } - } - else if (key == SDLK_F8) - { - if (which >= 0) - { - which = which - 4; - - if (which < 0) - which = 0; - - if (which < cur) - cur = cur - 4; - - update_list = 1; - } - } - else if (key == SDLK_F7) - { - if (which < num_files) - { - which = which + 4; - - if (which >= num_files) - which = num_files - 1; - - if (which >= cur + 16) - cur = cur + 4; - - update_list = 1; - } - } - else if (key == SDLK_RETURN) /* space also conflicts with handle_keymouse || key == SDLK_SPACE) */ - { - /* Open */ - - done = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); - } - else if (key == SDLK_ESCAPE || key == SDLK_AC_BACK) - { - /* Go back: */ - - which = -1; - done = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - } - else if (key == SDLK_d && - (event.key.keysym.mod & KMOD_CTRL) && - d_places[which] != PLACE_STARTERS_DIR && - d_places[which] != PLACE_PERSONAL_STARTERS_DIR && !noshortcuts) - { - /* Delete! */ - - want_erase = 1; - } - } - else if ((event.type == SDL_MOUSEBUTTONDOWN && valid_click(event.button.button)) || - event.type == TP_SDL_MOUSEBUTTONSCROLL) - { - if (event.button.x >= r_ttools.w && event.button.x < WINDOW_WIDTH - r_ttoolopt.w && - event.button.y >= img_scroll_up->h && event.button.y < (button_h * buttons_tall + r_ttools.h) - button_h) - { - /* Picked an icon! */ - - int old_which = which; - - which = ((event.button.x - r_ttools.w) / (THUMB_W) + (((event.button.y - img_scroll_up->h) / THUMB_H) * 4)) + cur; - - if (which < num_files) - { - playsound(screen, 1, SND_BLEEP, 1, event.button.x, SNDDIST_NEAR); - update_list = 1; - - - if (which == last_click_which && - SDL_GetTicks() < last_click_time + 1000 && event.button.button == last_click_button) - { - /* Double-click! */ - - done = 1; - } - - last_click_which = which; - last_click_time = SDL_GetTicks(); - last_click_button = event.button.button; - } - else - which = old_which; - } - else if (event.button.x >= (WINDOW_WIDTH - img_scroll_up->w) / 2 && - event.button.x <= (WINDOW_WIDTH + img_scroll_up->w) / 2) - { - if (event.button.y < img_scroll_up->h || - (event.button.y >= (button_h * buttons_tall + r_ttools.h) - button_h && - event.button.y < (button_h * buttons_tall + r_ttools.h) - img_scroll_up->h)) - { - /* Up or down scroll button in Open dialog: */ - - if (event.button.y < img_scroll_up->h) - { - /* Up scroll button in Open dialog: */ - - if (cur > 0) - { - cur = cur - 4; - update_list = 1; - playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, SNDDIST_NEAR); - - if (cur == 0) - do_setcursor(cursor_arrow); - } - - if (which >= cur + 16) - which = which - 4; - } - else if (event.button.y >= (button_h * buttons_tall + r_ttools.h) - button_h && - event.button.y < (button_h * buttons_tall + r_ttools.h) - img_scroll_up->h) - { - /* Down scroll button in Open dialog: */ - - if (cur < num_files - 16) - { - cur = cur + 4; - update_list = 1; - playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, SNDDIST_NEAR); - - if (cur >= num_files - 16) - do_setcursor(cursor_arrow); - } - - if (which < cur) - which = which + 4; - } - - if (scrolltimer_dialog != TIMERID_NONE) - { - SDL_RemoveTimer(scrolltimer_dialog); - scrolltimer_dialog = TIMERID_NONE; - } - - if (!scrolling_dialog && event.type == SDL_MOUSEBUTTONDOWN) - { - DEBUG_PRINTF("Starting scrolling\n"); - memcpy(&scrolltimer_dialog_event, &event, sizeof(SDL_Event)); - scrolltimer_dialog_event.type = TP_SDL_MOUSEBUTTONSCROLL; - - /* - * We enable the timer subsystem only when needed (e.g., to use SDL_AddTimer() needed - * for scrolling) then disable it immediately after (e.g., after the timer has fired or - * after SDL_RemoveTimer()) because enabling the timer subsystem in SDL1 has a high - * energy impact on the Mac. - */ - - scrolling_dialog = 1; - SDL_InitSubSystem(SDL_INIT_TIMER); - scrolltimer_dialog = - SDL_AddTimer(REPEAT_SPEED, scrolltimer_dialog_callback, (void *)&scrolltimer_dialog_event); - } - else - { - DEBUG_PRINTF("Continuing scrolling\n"); - scrolltimer_dialog = - SDL_AddTimer(REPEAT_SPEED / 3, scrolltimer_dialog_callback, (void *)&scrolltimer_dialog_event); - } - } - } - else if (event.button.x >= r_ttools.w && event.button.x < r_ttools.w + button_w && - event.button.y >= (button_h * buttons_tall + r_ttools.h) - button_h && - event.button.y < (button_h * buttons_tall + r_ttools.h)) - { - /* Open */ - - done = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); - } - else if (event.button.x >= r_ttools.w + button_w && event.button.x < r_ttools.w + button_w + button_w && - event.button.y >= (button_h * buttons_tall + r_ttools.h) - button_h && - event.button.y < (button_h * buttons_tall + r_ttools.h) && any_saved_files == 1) - { - /* Slideshow */ - - done = 1; - slideshow = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); - } - else if (event.button.x >= (WINDOW_WIDTH - r_ttoolopt.w - button_w) && - event.button.x < (WINDOW_WIDTH - r_ttoolopt.w) && - event.button.y >= (button_h * buttons_tall + r_ttools.h) - button_h && - event.button.y < (button_h * buttons_tall + r_ttools.h)) - { - /* Back */ - - which = -1; - done = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - } - else if (event.button.x >= (WINDOW_WIDTH - r_ttoolopt.w - button_w - button_w) && - event.button.x < (WINDOW_WIDTH - button_w - r_ttoolopt.w) && - event.button.y >= (button_h * buttons_tall + r_ttools.h) - button_h && - event.button.y < (button_h * buttons_tall + r_ttools.h) && - d_places[which] != PLACE_STARTERS_DIR && d_places[which] != PLACE_PERSONAL_STARTERS_DIR) - { - /* Erase */ - - want_erase = 1; - } - else if (event.button.x >= (WINDOW_WIDTH - r_ttoolopt.w - button_w - button_w - button_w) && - event.button.x < (WINDOW_WIDTH - button_w - button_w - r_ttoolopt.w) && - event.button.y >= (button_h * buttons_tall + r_ttools.h) - button_h && - event.button.y < (button_h * buttons_tall + r_ttools.h) && - d_places[which] != PLACE_STARTERS_DIR && d_places[which] != PLACE_PERSONAL_STARTERS_DIR) - { - /* Export */ - - want_export = 1; - } -#ifdef __ANDROID__ - start_motion_convert(event); -#endif - } - else if (event.type == SDL_MOUSEWHEEL && wheely) - { - /* Scroll wheel! */ - - if (event.wheel.y > 0 && cur > 0) - { - cur = cur - 4; - update_list = 1; - playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, SNDDIST_NEAR); - - if (cur == 0) - do_setcursor(cursor_arrow); - - if (which >= cur + 16) - which = which - 4; - } - else if (event.wheel.y < 0 && cur < num_files - 16) - { - cur = cur + 4; - update_list = 1; - playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, SNDDIST_NEAR); - - if (cur >= num_files - 16) - do_setcursor(cursor_arrow); - - if (which < cur) - which = which + 4; - } - } - else if (event.type == SDL_MOUSEMOTION) - { - /* Deal with mouse pointer shape! */ - - if (event.button.y < img_scroll_up->h && - event.button.x >= (WINDOW_WIDTH - img_scroll_up->w) / 2 && - event.button.x <= (WINDOW_WIDTH + img_scroll_up->w) / 2 && cur > 0) - { - /* Scroll up button: */ - - do_setcursor(cursor_up); - } - else if (event.button.y >= (button_h * buttons_tall + r_ttools.h) - button_h && - event.button.y < (button_h * buttons_tall + r_ttools.h - img_scroll_up->h) && - event.button.x >= (WINDOW_WIDTH - img_scroll_up->w) / 2 && - event.button.x <= (WINDOW_WIDTH + img_scroll_up->w) / 2 && cur < num_files - 16) - { - /* Scroll down button: */ - - do_setcursor(cursor_down); - } - else if (((event.button.x >= r_ttools.w && event.button.x < r_ttools.w + button_w + button_w) || - (event.button.x >= (WINDOW_WIDTH - r_ttoolopt.w - button_w) && - event.button.x < (WINDOW_WIDTH - r_ttoolopt.w)) || - (event.button.x >= (WINDOW_WIDTH - r_ttoolopt.w - button_w - button_w - button_w) && - event.button.x < (WINDOW_WIDTH - button_w - r_ttoolopt.w) && - /* Both "Erase" and "Export" only work on our own files... */ - d_places[which] != PLACE_STARTERS_DIR && - d_places[which] != PLACE_PERSONAL_STARTERS_DIR)) && - event.button.y >= (button_h * buttons_tall + r_ttools.h) - button_h && - event.button.y < (button_h * buttons_tall + r_ttools.h)) - { - /* One of the command buttons: */ - - do_setcursor(cursor_hand); - } - else if (event.button.x >= r_ttools.w && event.button.x < WINDOW_WIDTH - r_ttoolopt.w && - event.button.y > img_scroll_up->h && - event.button.y < (button_h * buttons_tall + r_ttools.h) - button_h && - ((((event.button.x - r_ttools.w) / (THUMB_W) + - (((event.button.y - img_scroll_up->h) / THUMB_H) * 4)) + cur) < num_files)) - { - /* One of the thumbnails: */ - - do_setcursor(cursor_hand); - } - else - { - /* Unclickable... */ - - do_setcursor(cursor_arrow); - } - -#ifdef __ANDROID__ - convert_motion_to_wheel(event); -#endif - - oldpos_x = event.button.x; - oldpos_y = event.button.y; - } - - else if (event.type == SDL_MOUSEBUTTONUP) - { -#ifdef __ANDROID__ - stop_motion_convert(event); -#endif - if (scrolling_dialog) - { - if (scrolltimer_dialog != TIMERID_NONE) - { - SDL_RemoveTimer(scrolltimer_dialog); - scrolltimer_dialog = TIMERID_NONE; - } - scrolling_dialog = 0; - SDL_QuitSubSystem(SDL_INIT_TIMER); - DEBUG_PRINTF("Killing dialog scrolling\n"); - } - } - - else if (event.type == SDL_JOYAXISMOTION) - handle_joyaxismotion(event, &motioner, &val_x, &val_y); - - else if (event.type == SDL_JOYHATMOTION) - handle_joyhatmotion(event, oldpos_x, oldpos_y, &valhat_x, &valhat_y, &hatmotioner, &old_hat_ticks); - - else if (event.type == SDL_JOYBALLMOTION) - handle_joyballmotion(event, oldpos_x, oldpos_y); - - else if (event.type == SDL_JOYBUTTONDOWN || event.type == SDL_JOYBUTTONUP) - handle_joybuttonupdown(event, oldpos_x, oldpos_y); - } /* while (SDL_PollEvent(&event)) */ - - if (motioner | hatmotioner) - handle_motioners(oldpos_x, oldpos_y, motioner, hatmotioner, old_hat_ticks, val_x, val_y, valhat_x, - valhat_y); - - - SDL_Delay(10); - - if (want_erase) - { - want_erase = 0; - - if (do_prompt_image_snd(PROMPT_ERASE_TXT, - PROMPT_ERASE_YES, PROMPT_ERASE_NO, - thumbs[which], - img_popup_arrow, img_trash, SND_AREYOUSURE, - WINDOW_WIDTH - r_ttoolopt.w - button_w - button_w + 24, button_h * buttons_tall + r_ttools.h - button_h + img_scroll_up->h)) - { - safe_snprintf(fname, sizeof(fname), "saved/%s%s", d_names[which], d_exts[which]); - - rfname = get_fname(fname, DIR_SAVE); - - if (trash(rfname) == 0) - { - update_list = 1; - - - /* Delete the thumbnail, too: */ - - safe_snprintf(fname, sizeof(fname), "saved/.thumbs/%s-t.png", d_names[which]); - - free(rfname); - rfname = get_fname(fname, DIR_SAVE); - - unlink(rfname); - - - /* Try deleting old-style thumbnail, too: */ - - safe_snprintf(fname, sizeof(fname), "saved/%s-t.png", d_names[which]); - - free(rfname); - rfname = get_fname(fname, DIR_SAVE); - - unlink(rfname); - - - /* Delete .dat file, if any: */ - - safe_snprintf(fname, sizeof(fname), "saved/%s.dat", d_names[which]); - - free(rfname); - rfname = get_fname(fname, DIR_SAVE); - - trash(rfname); - - - - /* Move all other files up a notch: */ - - free(d_names[which]); - free(d_exts[which]); - free_surface(&thumbs[which]); - - thumbs[which] = NULL; - - for (i = which; i < num_files - 1; i++) - { - d_names[i] = d_names[i + 1]; - d_exts[i] = d_exts[i + 1]; - thumbs[i] = thumbs[i + 1]; - d_places[i] = d_places[i + 1]; - } - - num_files--; - - - /* Make sure the cursor doesn't go off the end! */ - - if (which >= num_files) - which = num_files - 1; - - - /* Scroll up if the cursor goes off top of screen! */ - - if (which < cur && cur >= 4) - { - cur = cur - 4; - update_list = 1; - } - - - /* No files to open now? */ - - if (which < 0) - { - do_prompt_snd(PROMPT_OPEN_NOFILES_TXT, - PROMPT_OPEN_NOFILES_YES, "", SND_YOUCANNOT, screen->w / 2, screen->h / 2); - done = 1; - } - } - else - { - perror(rfname); - - do_prompt_snd("CAN'T", "OK", "", SND_YOUCANNOT, 0, 0); - update_list = 1; - } - - free(rfname); - } - else - { - update_list = 1; - } + SDL_RemoveTimer(scrolltimer_dialog); + scrolltimer_dialog = TIMERID_NONE; } - if (want_export) + if (!scrolling_dialog && event.type == SDL_MOUSEBUTTONDOWN) { - want_export = 0; + DEBUG_PRINTF("Starting scrolling\n"); + memcpy(&scrolltimer_dialog_event, &event, + sizeof(SDL_Event)); + scrolltimer_dialog_event.type = TP_SDL_MOUSEBUTTONSCROLL; - safe_snprintf(fname, sizeof(fname), "saved/%s%s", d_names[which], d_exts[which]); - rfname = get_fname(fname, DIR_SAVE); - if (export_pict(rfname)) - do_prompt_snd(PROMPT_PICT_EXPORT_TXT, PROMPT_EXPORT_YES, "", SND_TUXOK, screen->w / 2, screen->h / 2); - else - do_prompt_snd(PROMPT_PICT_EXPORT_FAILED_TXT, PROMPT_EXPORT_YES, "", SND_YOUCANNOT, screen->w / 2, screen->h / 2); + /* + * We enable the timer subsystem only when needed (e.g., to use SDL_AddTimer() needed + * for scrolling) then disable it immediately after (e.g., after the timer has fired or + * after SDL_RemoveTimer()) because enabling the timer subsystem in SDL1 has a high + * energy impact on the Mac. + */ - draw_tux_text(TUX_BORED, instructions, 1); - update_list = 1; + scrolling_dialog = 1; + SDL_InitSubSystem(SDL_INIT_TIMER); + scrolltimer_dialog = + SDL_AddTimer(REPEAT_SPEED, scrolltimer_dialog_callback, + (void *) &scrolltimer_dialog_event); } + else + { + DEBUG_PRINTF("Continuing scrolling\n"); + scrolltimer_dialog = + SDL_AddTimer(REPEAT_SPEED / 3, + scrolltimer_dialog_callback, + (void *) &scrolltimer_dialog_event); + } + } } - while (!done); - - - if (!slideshow) + else if (event.button.x >= r_ttools.w + && event.button.x < r_ttools.w + button_w + && event.button.y >= + (button_h * buttons_tall + r_ttools.h) - button_h + && event.button.y < + (button_h * buttons_tall + r_ttools.h)) { - /* Load the chosen picture: */ + /* Open */ - if (which != -1) - { - /* Save old one first? */ + done = 1; + playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); + } + else if (event.button.x >= r_ttools.w + button_w + && event.button.x < r_ttools.w + button_w + button_w + && event.button.y >= + (button_h * buttons_tall + r_ttools.h) - button_h + && event.button.y < + (button_h * buttons_tall + r_ttools.h) + && any_saved_files == 1) + { + /* Slideshow */ - if (!been_saved && !disable_save) - { - if (do_prompt_image_snd(PROMPT_OPEN_SAVE_TXT, - PROMPT_OPEN_SAVE_YES, - PROMPT_OPEN_SAVE_NO, - img_tools[TOOL_SAVE], NULL, NULL, - SND_AREYOUSURE, screen->w / 2, screen->h / 2)) - { - do_save(TOOL_OPEN, 1, 0); - } - } + done = 1; + slideshow = 1; + playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); + } + else if (event.button.x >= + (WINDOW_WIDTH - r_ttoolopt.w - button_w) + && event.button.x < (WINDOW_WIDTH - r_ttoolopt.w) + && event.button.y >= + (button_h * buttons_tall + r_ttools.h) - button_h + && event.button.y < + (button_h * buttons_tall + r_ttools.h)) + { + /* Back */ - /* Clean the label stuff */ - delete_label_list(&start_label_node); - start_label_node = current_label_node = first_label_node_in_redo_stack = highlighted_label_node = - label_node_to_edit = NULL; - have_to_rec_label_node = FALSE; + which = -1; + done = 1; + playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); + } + else if (event.button.x >= + (WINDOW_WIDTH - r_ttoolopt.w - button_w - button_w) + && event.button.x < + (WINDOW_WIDTH - button_w - r_ttoolopt.w) + && event.button.y >= + (button_h * buttons_tall + r_ttools.h) - button_h + && event.button.y < + (button_h * buttons_tall + r_ttools.h) + && d_places[which] != PLACE_STARTERS_DIR + && d_places[which] != PLACE_PERSONAL_STARTERS_DIR) + { + /* Erase */ - SDL_FillRect(label, NULL, SDL_MapRGBA(label->format, 0, 0, 0, 0)); + want_erase = 1; + } + else if (event.button.x >= + (WINDOW_WIDTH - r_ttoolopt.w - button_w - button_w - + button_w) + && event.button.x < + (WINDOW_WIDTH - button_w - button_w - r_ttoolopt.w) + && event.button.y >= + (button_h * buttons_tall + r_ttools.h) - button_h + && event.button.y < + (button_h * buttons_tall + r_ttools.h) + && d_places[which] != PLACE_STARTERS_DIR + && d_places[which] != PLACE_PERSONAL_STARTERS_DIR) + { + /* Export */ - /* Figure out filename: */ + want_export = 1; + } +#ifdef __ANDROID__ + start_motion_convert(event); +#endif + } + else if (event.type == SDL_MOUSEWHEEL && wheely) + { + /* Scroll wheel! */ - safe_snprintf(fname, sizeof(fname), "%s/%s%s", dirname[d_places[which]], d_names[which], d_exts[which]); - fi = fopen(fname, "r"); - if (fi == NULL) - { - fprintf(stderr, - "\nWarning: Couldn't load the saved image! (1)\n" - "%s\n" "The file is missing.\n\n\n", fname); - do_prompt(PROMPT_OPEN_UNOPENABLE_TXT, PROMPT_OPEN_UNOPENABLE_YES, "", 0, 0); - } - fclose(fi); + if (event.wheel.y > 0 && cur > 0) + { + cur = cur - 4; + update_list = 1; + playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, + SNDDIST_NEAR); - img = myIMG_Load(fname); + if (cur == 0) + do_setcursor(cursor_arrow); - if (img == NULL) - { - fprintf(stderr, - "\nWarning: Couldn't load the saved image! (2)\n" - "%s\n" - "The Simple DirectMedia Layer error that occurred " - "was:\n" "%s\n\n", fname, SDL_GetError()); + if (which >= cur + 16) + which = which - 4; + } + else if (event.wheel.y < 0 && cur < num_files - 16) + { + cur = cur + 4; + update_list = 1; + playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, + SNDDIST_NEAR); - do_prompt(PROMPT_OPEN_UNOPENABLE_TXT, PROMPT_OPEN_UNOPENABLE_YES, "", 0, 0); - } - else - { - free_surface(&img_starter); - free_surface(&img_starter_bkgd); - starter_mirrored = 0; - starter_flipped = 0; - starter_personal = 0; + if (cur >= num_files - 16) + do_setcursor(cursor_arrow); - org_surf = SDL_DisplayFormat(img); /* Keep a copy of the original image - unscaled to send to load_embedded_data */ - autoscale_copy_smear_free(img, canvas, SDL_BlitSurface); + if (which < cur) + which = which + 4; + } + } + else if (event.type == SDL_MOUSEMOTION) + { + /* Deal with mouse pointer shape! */ - cur_undo = 0; - oldest_undo = 0; - newest_undo = 0; + if (event.button.y < img_scroll_up->h && + event.button.x >= (WINDOW_WIDTH - img_scroll_up->w) / 2 && + event.button.x <= (WINDOW_WIDTH + img_scroll_up->w) / 2 + && cur > 0) + { + /* Scroll up button: */ - /* Saved image: */ + do_setcursor(cursor_up); + } + else if (event.button.y >= + (button_h * buttons_tall + r_ttools.h) - button_h + && event.button.y < + (button_h * buttons_tall + r_ttools.h - img_scroll_up->h) + && event.button.x >= + (WINDOW_WIDTH - img_scroll_up->w) / 2 + && event.button.x <= + (WINDOW_WIDTH + img_scroll_up->w) / 2 + && cur < num_files - 16) + { + /* Scroll down button: */ - been_saved = 1; + do_setcursor(cursor_down); + } + else + if (((event.button.x >= r_ttools.w + && event.button.x < r_ttools.w + button_w + button_w) + || (event.button.x >= + (WINDOW_WIDTH - r_ttoolopt.w - button_w) + && event.button.x < (WINDOW_WIDTH - r_ttoolopt.w)) + || (event.button.x >= + (WINDOW_WIDTH - r_ttoolopt.w - button_w - button_w - + button_w) + && event.button.x < + (WINDOW_WIDTH - button_w - r_ttoolopt.w) && + /* Both "Erase" and "Export" only work on our own files... */ + d_places[which] != PLACE_STARTERS_DIR && + d_places[which] != PLACE_PERSONAL_STARTERS_DIR)) && + event.button.y >= + (button_h * buttons_tall + r_ttools.h) - button_h + && event.button.y < (button_h * buttons_tall + r_ttools.h)) + { + /* One of the command buttons: */ - safe_strncpy(file_id, d_names[which], sizeof(file_id)); - starter_id[0] = '\0'; - template_id[0] = '\0'; + do_setcursor(cursor_hand); + } + else if (event.button.x >= r_ttools.w + && event.button.x < WINDOW_WIDTH - r_ttoolopt.w + && event.button.y > img_scroll_up->h + && event.button.y < + (button_h * buttons_tall + r_ttools.h) - button_h + && + ((((event.button.x - r_ttools.w) / (THUMB_W) + + (((event.button.y - + img_scroll_up->h) / THUMB_H) * 4)) + cur) < + num_files)) + { + /* One of the thumbnails: */ + do_setcursor(cursor_hand); + } + else + { + /* Unclickable... */ - /* Keep this for compatibility */ - /* See if this saved image was based on a 'starter' */ - - load_starter_id(d_names[which], NULL); - - if (starter_id[0] != '\0') - { - load_starter(starter_id); - - if (starter_mirrored) - mirror_starter(); - - if (starter_flipped) - flip_starter(); - } - else if (template_id[0] != '\0') - load_template(template_id); - - load_embedded_data(fname, org_surf); - - reset_avail_tools(); - - tool_avail_bak[TOOL_UNDO] = 0; - tool_avail_bak[TOOL_REDO] = 0; - - opened_something = 1; - } - } + do_setcursor(cursor_arrow); } +#ifdef __ANDROID__ + convert_motion_to_wheel(event); +#endif - update_canvas(0, 0, WINDOW_WIDTH - r_ttoolopt.w - r_ttools.w, button_h * buttons_tall + r_ttools.h); + oldpos_x = event.button.x; + oldpos_y = event.button.y; + } - free(instructions); - } + else if (event.type == SDL_MOUSEBUTTONUP) + { +#ifdef __ANDROID__ + stop_motion_convert(event); +#endif + if (scrolling_dialog) + { + if (scrolltimer_dialog != TIMERID_NONE) + { + SDL_RemoveTimer(scrolltimer_dialog); + scrolltimer_dialog = TIMERID_NONE; + } + scrolling_dialog = 0; + SDL_QuitSubSystem(SDL_INIT_TIMER); + DEBUG_PRINTF("Killing dialog scrolling\n"); + } + } + + else if (event.type == SDL_JOYAXISMOTION) + handle_joyaxismotion(event, &motioner, &val_x, &val_y); + + else if (event.type == SDL_JOYHATMOTION) + handle_joyhatmotion(event, oldpos_x, oldpos_y, &valhat_x, + &valhat_y, &hatmotioner, &old_hat_ticks); + + else if (event.type == SDL_JOYBALLMOTION) + handle_joyballmotion(event, oldpos_x, oldpos_y); + + else if (event.type == SDL_JOYBUTTONDOWN + || event.type == SDL_JOYBUTTONUP) + handle_joybuttonupdown(event, oldpos_x, oldpos_y); + } /* while (SDL_PollEvent(&event)) */ + + if (motioner | hatmotioner) + handle_motioners(oldpos_x, oldpos_y, motioner, hatmotioner, + old_hat_ticks, val_x, val_y, valhat_x, valhat_y); - /* Clean up: */ + SDL_Delay(10); - free_surface_array(thumbs, num_files); - - free(thumbs); - - for (i = 0; i < num_files; i++) + if (want_erase) { - free(d_names[i]); - free(d_exts[i]); + want_erase = 0; + + if (do_prompt_image_snd(PROMPT_ERASE_TXT, + PROMPT_ERASE_YES, PROMPT_ERASE_NO, + thumbs[which], + img_popup_arrow, img_trash, SND_AREYOUSURE, + WINDOW_WIDTH - r_ttoolopt.w - button_w - + button_w + 24, + button_h * buttons_tall + r_ttools.h - + button_h + img_scroll_up->h)) + { + safe_snprintf(fname, sizeof(fname), "saved/%s%s", d_names[which], + d_exts[which]); + + rfname = get_fname(fname, DIR_SAVE); + + if (trash(rfname) == 0) + { + update_list = 1; + + + /* Delete the thumbnail, too: */ + + safe_snprintf(fname, sizeof(fname), "saved/.thumbs/%s-t.png", + d_names[which]); + + free(rfname); + rfname = get_fname(fname, DIR_SAVE); + + unlink(rfname); + + + /* Try deleting old-style thumbnail, too: */ + + safe_snprintf(fname, sizeof(fname), "saved/%s-t.png", + d_names[which]); + + free(rfname); + rfname = get_fname(fname, DIR_SAVE); + + unlink(rfname); + + + /* Delete .dat file, if any: */ + + safe_snprintf(fname, sizeof(fname), "saved/%s.dat", + d_names[which]); + + free(rfname); + rfname = get_fname(fname, DIR_SAVE); + + trash(rfname); + + + + /* Move all other files up a notch: */ + + free(d_names[which]); + free(d_exts[which]); + free_surface(&thumbs[which]); + + thumbs[which] = NULL; + + for (i = which; i < num_files - 1; i++) + { + d_names[i] = d_names[i + 1]; + d_exts[i] = d_exts[i + 1]; + thumbs[i] = thumbs[i + 1]; + d_places[i] = d_places[i + 1]; + } + + num_files--; + + + /* Make sure the cursor doesn't go off the end! */ + + if (which >= num_files) + which = num_files - 1; + + + /* Scroll up if the cursor goes off top of screen! */ + + if (which < cur && cur >= 4) + { + cur = cur - 4; + update_list = 1; + } + + + /* No files to open now? */ + + if (which < 0) + { + do_prompt_snd(PROMPT_OPEN_NOFILES_TXT, + PROMPT_OPEN_NOFILES_YES, "", SND_YOUCANNOT, + screen->w / 2, screen->h / 2); + done = 1; + } + } + else + { + perror(rfname); + + do_prompt_snd("CAN'T", "OK", "", SND_YOUCANNOT, 0, 0); + update_list = 1; + } + + free(rfname); + } + else + { + update_list = 1; + } } - for (i = 0; i < NUM_PLACES_TO_LOOK; i++) - if (dirname[i] != NULL) - free(dirname[i]); - - free(d_names); - free(d_exts); - free(d_places); - - - if (slideshow) + if (want_export) { - slideshow = do_slideshow(); + want_export = 0; + + safe_snprintf(fname, sizeof(fname), "saved/%s%s", d_names[which], + d_exts[which]); + rfname = get_fname(fname, DIR_SAVE); + if (export_pict(rfname)) + do_prompt_snd(PROMPT_PICT_EXPORT_TXT, PROMPT_EXPORT_YES, "", + SND_TUXOK, screen->w / 2, screen->h / 2); + else + do_prompt_snd(PROMPT_PICT_EXPORT_FAILED_TXT, PROMPT_EXPORT_YES, + "", SND_YOUCANNOT, screen->w / 2, screen->h / 2); + + draw_tux_text(TUX_BORED, instructions, 1); + update_list = 1; } + } + while (!done); + + + if (!slideshow) + { + /* Load the chosen picture: */ + + if (which != -1) + { + /* Save old one first? */ + + if (!been_saved && !disable_save) + { + if (do_prompt_image_snd(PROMPT_OPEN_SAVE_TXT, + PROMPT_OPEN_SAVE_YES, + PROMPT_OPEN_SAVE_NO, + img_tools[TOOL_SAVE], NULL, NULL, + SND_AREYOUSURE, screen->w / 2, + screen->h / 2)) + { + do_save(TOOL_OPEN, 1, 0); + } + } + + /* Clean the label stuff */ + delete_label_list(&start_label_node); + start_label_node = current_label_node = + first_label_node_in_redo_stack = highlighted_label_node = + label_node_to_edit = NULL; + have_to_rec_label_node = FALSE; + + SDL_FillRect(label, NULL, SDL_MapRGBA(label->format, 0, 0, 0, 0)); + + /* Figure out filename: */ + + safe_snprintf(fname, sizeof(fname), "%s/%s%s", + dirname[d_places[which]], d_names[which], + d_exts[which]); + fi = fopen(fname, "r"); + if (fi == NULL) + { + fprintf(stderr, + "\nWarning: Couldn't load the saved image! (1)\n" + "%s\n" "The file is missing.\n\n\n", fname); + do_prompt(PROMPT_OPEN_UNOPENABLE_TXT, PROMPT_OPEN_UNOPENABLE_YES, + "", 0, 0); + } + fclose(fi); + + img = myIMG_Load(fname); + + if (img == NULL) + { + fprintf(stderr, + "\nWarning: Couldn't load the saved image! (2)\n" + "%s\n" + "The Simple DirectMedia Layer error that occurred " + "was:\n" "%s\n\n", fname, SDL_GetError()); + + do_prompt(PROMPT_OPEN_UNOPENABLE_TXT, PROMPT_OPEN_UNOPENABLE_YES, + "", 0, 0); + } + else + { + free_surface(&img_starter); + free_surface(&img_starter_bkgd); + starter_mirrored = 0; + starter_flipped = 0; + starter_personal = 0; + + org_surf = SDL_DisplayFormat(img); /* Keep a copy of the original image + unscaled to send to load_embedded_data */ + autoscale_copy_smear_free(img, canvas, SDL_BlitSurface); + + cur_undo = 0; + oldest_undo = 0; + newest_undo = 0; + + /* Saved image: */ + + been_saved = 1; + + safe_strncpy(file_id, d_names[which], sizeof(file_id)); + starter_id[0] = '\0'; + template_id[0] = '\0'; + + + /* Keep this for compatibility */ + /* See if this saved image was based on a 'starter' */ + + load_starter_id(d_names[which], NULL); + + if (starter_id[0] != '\0') + { + load_starter(starter_id); + + if (starter_mirrored) + mirror_starter(); + + if (starter_flipped) + flip_starter(); + } + else if (template_id[0] != '\0') + load_template(template_id); + + load_embedded_data(fname, org_surf); + + reset_avail_tools(); + + tool_avail_bak[TOOL_UNDO] = 0; + tool_avail_bak[TOOL_REDO] = 0; + + opened_something = 1; + } + } + } + + + update_canvas(0, 0, WINDOW_WIDTH - r_ttoolopt.w - r_ttools.w, + button_h * buttons_tall + r_ttools.h); + + free(instructions); } + + + /* Clean up: */ + + free_surface_array(thumbs, num_files); + + free(thumbs); + + for (i = 0; i < num_files; i++) + { + free(d_names[i]); + free(d_exts[i]); + } + + for (i = 0; i < NUM_PLACES_TO_LOOK; i++) + if (dirname[i] != NULL) + free(dirname[i]); + + free(d_names); + free(d_exts); + free(d_places); + + + if (slideshow) + { + slideshow = do_slideshow(); + } + } while (slideshow); return (opened_something); @@ -17228,7 +18297,8 @@ static int do_slideshow(void) int num_selected; FILE *fi; char fname[1024]; - int num_files, num_files_in_dir, i, done, update_list, cur, which, j, go_back, found, speed; + int num_files, num_files_in_dir, i, done, update_list, cur, which, j, + go_back, found, speed; SDL_Rect dest; SDL_Event event; SDLKey key; @@ -17249,7 +18319,7 @@ static int do_slideshow(void) things_alloced = 32; - fs = (struct dirent2 *)malloc(sizeof(struct dirent2) * things_alloced); + fs = (struct dirent2 *) malloc(sizeof(struct dirent2) * things_alloced); num_files_in_dir = 0; num_files = 0; @@ -17267,217 +18337,234 @@ static int do_slideshow(void) d = opendir(dirname); if (d != NULL) + { + /* Gather list of files (for sorting): */ + + do { - /* Gather list of files (for sorting): */ + f = readdir(d); - do + if (f != NULL) + { + memcpy(&(fs[num_files_in_dir].f), f, sizeof(struct dirent)); + fs[num_files_in_dir].place = PLACE_SAVED_DIR; + + num_files_in_dir++; + + if (num_files_in_dir >= things_alloced) { - f = readdir(d); - - if (f != NULL) - { - memcpy(&(fs[num_files_in_dir].f), f, sizeof(struct dirent)); - fs[num_files_in_dir].place = PLACE_SAVED_DIR; - - num_files_in_dir++; - - if (num_files_in_dir >= things_alloced) - { - things_alloced = things_alloced + 32; - fs = (struct dirent2 *)realloc(fs, sizeof(struct dirent2) * things_alloced); - } - } + things_alloced = things_alloced + 32; + fs = + (struct dirent2 *) realloc(fs, + sizeof(struct dirent2) * + things_alloced); } - while (f != NULL); - - closedir(d); + } } + while (f != NULL); + + closedir(d); + } /* (Re)allocate space for the information about these files: */ thumbs = (SDL_Surface * *)malloc(sizeof(SDL_Surface *) * num_files_in_dir); - d_names = (char **)malloc(sizeof(char *) * num_files_in_dir); - d_exts = (char **)malloc(sizeof(char *) * num_files_in_dir); - selected = (int *)malloc(sizeof(int) * num_files_in_dir); + d_names = (char **) malloc(sizeof(char *) * num_files_in_dir); + d_exts = (char **) malloc(sizeof(char *) * num_files_in_dir); + selected = (int *) malloc(sizeof(int) * num_files_in_dir); /* Sort: */ if (!reversesort) - qsort(fs, num_files_in_dir, sizeof(struct dirent2), (int (*)(const void *, const void *))compare_dirent2s); + qsort(fs, num_files_in_dir, sizeof(struct dirent2), + (int (*)(const void *, const void *)) compare_dirent2s); else - qsort(fs, num_files_in_dir, sizeof(struct dirent2), (int (*)(const void *, const void *))compare_dirent2s_invert); + qsort(fs, num_files_in_dir, sizeof(struct dirent2), + (int (*)(const void *, const void *)) compare_dirent2s_invert); /* Read directory of images and build thumbnails: */ for (j = 0; j < num_files_in_dir; j++) + { + f = &(fs[j].f); + + show_progress_bar(screen); + + if (f != NULL) { - f = &(fs[j].f); + debug(f->d_name); - show_progress_bar(screen); - - if (f != NULL) + if (strcasestr(f->d_name, "-t.") == NULL + && strcasestr(f->d_name, "-back.") == NULL) + { + if (strcasestr(f->d_name, FNAME_EXTENSION) != NULL + /* Support legacy BMP files for load: */ + || strcasestr(f->d_name, ".bmp") != NULL) { - debug(f->d_name); + safe_strncpy(fname, f->d_name, sizeof(fname)); + if (strcasestr(fname, FNAME_EXTENSION) != NULL) + { + d_exts[num_files] = strdup(strcasestr(fname, FNAME_EXTENSION)); + strcpy((char *) strcasestr(fname, FNAME_EXTENSION), ""); /* FIXME: Use strncpy() (ugh, complicated) */ + } - if (strcasestr(f->d_name, "-t.") == NULL && strcasestr(f->d_name, "-back.") == NULL) + if (strcasestr(fname, ".bmp") != NULL) + { + d_exts[num_files] = strdup(strcasestr(fname, ".bmp")); + strcpy((char *) strcasestr(fname, ".bmp"), ""); /* Safe; truncating */ + } + + d_names[num_files] = strdup(fname); + + + /* FIXME: Try to center list on whatever was selected + in do_open() when the slideshow button was clicked. */ + + /* Try to load thumbnail first: */ + + safe_snprintf(fname, sizeof(fname), "%s/.thumbs/%s-t.png", dirname, + d_names[num_files]); + debug("Loading thumbnail..."); + debug(fname); + img = IMG_Load(fname); + if (img == NULL) + { + /* No thumbnail in the new location ("saved/.thumbs"), + try the old locatin ("saved/"): */ + + safe_snprintf(fname, sizeof(fname), "%s/%s-t.png", dirname, + d_names[num_files]); + debug(fname); + + img = IMG_Load(fname); + } + + + if (img != NULL) + { + /* Loaded the thumbnail from one or the other location */ + + debug("Thumbnail loaded, scaling"); + show_progress_bar(screen); + + img1 = SDL_DisplayFormat(img); + SDL_FreeSurface(img); + + if (img1 != NULL) { - if (strcasestr(f->d_name, FNAME_EXTENSION) != NULL - /* Support legacy BMP files for load: */ - || strcasestr(f->d_name, ".bmp") != NULL) - { - safe_strncpy(fname, f->d_name, sizeof(fname)); - if (strcasestr(fname, FNAME_EXTENSION) != NULL) - { - d_exts[num_files] = strdup(strcasestr(fname, FNAME_EXTENSION)); - strcpy((char *)strcasestr(fname, FNAME_EXTENSION), ""); /* FIXME: Use strncpy() (ugh, complicated) */ - } + /* if too big, or too small in both dimensions, rescale it + (for now: using old thumbnail as source for high speed, low quality) */ + if (img1->w > THUMB_W - 20 || img1->h > THUMB_H - 20 + || (img1->w < THUMB_W - 20 && img1->h < THUMB_H - 20)) + { + img2 = thumbnail(img1, THUMB_W - 20, THUMB_H - 20, 0); + SDL_FreeSurface(img1); + img1 = img2; + } - if (strcasestr(fname, ".bmp") != NULL) - { - d_exts[num_files] = strdup(strcasestr(fname, ".bmp")); - strcpy((char *)strcasestr(fname, ".bmp"), ""); /* Safe; truncating */ - } + thumbs[num_files] = img1; - d_names[num_files] = strdup(fname); - - - /* FIXME: Try to center list on whatever was selected - in do_open() when the slideshow button was clicked. */ - - /* Try to load thumbnail first: */ - - safe_snprintf(fname, sizeof(fname), "%s/.thumbs/%s-t.png", dirname, d_names[num_files]); - debug("Loading thumbnail..."); - debug(fname); - img = IMG_Load(fname); - if (img == NULL) - { - /* No thumbnail in the new location ("saved/.thumbs"), - try the old locatin ("saved/"): */ - - safe_snprintf(fname, sizeof(fname), "%s/%s-t.png", dirname, d_names[num_files]); - debug(fname); - - img = IMG_Load(fname); - } - - - if (img != NULL) - { - /* Loaded the thumbnail from one or the other location */ - - debug("Thumbnail loaded, scaling"); - show_progress_bar(screen); - - img1 = SDL_DisplayFormat(img); - SDL_FreeSurface(img); - - if (img1 != NULL) - { - /* if too big, or too small in both dimensions, rescale it - (for now: using old thumbnail as source for high speed, low quality) */ - if (img1->w > THUMB_W - 20 || img1->h > THUMB_H - 20 - || (img1->w < THUMB_W - 20 && img1->h < THUMB_H - 20)) - { - img2 = thumbnail(img1, THUMB_W - 20, THUMB_H - 20, 0); - SDL_FreeSurface(img1); - img1 = img2; - } - - thumbs[num_files] = img1; - - if (thumbs[num_files] == NULL) - { - fprintf(stderr, "\nError: Couldn't create a thumbnail of saved image!\n" "%s\n", fname); - } - else - num_files++; - } - } - else - { - /* No thumbnail - load original: */ - - /* Make sure we have a ~/.tuxpaint/saved directory: */ - if (make_directory(DIR_SAVE, "saved", "Can't create user data directory (for saved drawings) (E008)")) - { - /* (Make sure we have a .../saved/.thumbs/ directory:) */ - make_directory(DIR_SAVE, "saved/.thumbs", "Can't create user data thumbnail directory (for saved drawings' thumbnails) (E009)"); - } - - safe_snprintf(fname, sizeof(fname), "%s/%s", dirname, f->d_name); - - debug("Loading original, to make thumbnail"); - debug(fname); - img = myIMG_Load(fname); - - - show_progress_bar(screen); - - - if (img == NULL) - { - fprintf(stderr, - "\nWarning: I can't open one of the saved files!\n" - "%s\n" - "The Simple DirectMedia Layer error that " - "occurred was:\n" "%s\n\n", fname, SDL_GetError()); - } - else - { - /* Turn it into a thumbnail: */ - - img1 = SDL_DisplayFormatAlpha(img); - img2 = thumbnail2(img1, THUMB_W - 20, THUMB_H - 20, 0, 0); - SDL_FreeSurface(img1); - - show_progress_bar(screen); - - thumbs[num_files] = SDL_DisplayFormat(img2); - SDL_FreeSurface(img2); - - SDL_FreeSurface(img); - - if (thumbs[num_files] == NULL) - { - fprintf(stderr, "\nError: Couldn't create a thumbnail of saved image!\n" "%s\n", fname); - } - else - { - show_progress_bar(screen); - - /* Let's save this thumbnail, so we don't have to - create it again next time 'Open' is called: */ - - debug("Saving thumbnail for this one!"); - - safe_snprintf(fname, sizeof(fname), "%s/.thumbs/%s-t.png", dirname, d_names[num_files]); - - fi = fopen(fname, "wb"); - if (fi == NULL) - { - fprintf(stderr, - "\nError: Couldn't save thumbnail of saved image!\n" - "%s\n" "The error that occurred was:\n" "%s\n\n", fname, strerror(errno)); - } - else - { - do_png_save(fi, fname, thumbs[num_files], 0); - } - - show_progress_bar(screen); - - num_files++; - } - } - } - } + if (thumbs[num_files] == NULL) + { + fprintf(stderr, + "\nError: Couldn't create a thumbnail of saved image!\n" + "%s\n", fname); + } + else + num_files++; } + } + else + { + /* No thumbnail - load original: */ + + /* Make sure we have a ~/.tuxpaint/saved directory: */ + if (make_directory + (DIR_SAVE, "saved", + "Can't create user data directory (for saved drawings) (E008)")) + { + /* (Make sure we have a .../saved/.thumbs/ directory:) */ + make_directory(DIR_SAVE, "saved/.thumbs", + "Can't create user data thumbnail directory (for saved drawings' thumbnails) (E009)"); + } + + safe_snprintf(fname, sizeof(fname), "%s/%s", dirname, f->d_name); + + debug("Loading original, to make thumbnail"); + debug(fname); + img = myIMG_Load(fname); + + + show_progress_bar(screen); + + + if (img == NULL) + { + fprintf(stderr, + "\nWarning: I can't open one of the saved files!\n" + "%s\n" + "The Simple DirectMedia Layer error that " + "occurred was:\n" "%s\n\n", fname, SDL_GetError()); + } + else + { + /* Turn it into a thumbnail: */ + + img1 = SDL_DisplayFormatAlpha(img); + img2 = thumbnail2(img1, THUMB_W - 20, THUMB_H - 20, 0, 0); + SDL_FreeSurface(img1); + + show_progress_bar(screen); + + thumbs[num_files] = SDL_DisplayFormat(img2); + SDL_FreeSurface(img2); + + SDL_FreeSurface(img); + + if (thumbs[num_files] == NULL) + { + fprintf(stderr, + "\nError: Couldn't create a thumbnail of saved image!\n" + "%s\n", fname); + } + else + { + show_progress_bar(screen); + + /* Let's save this thumbnail, so we don't have to + create it again next time 'Open' is called: */ + + debug("Saving thumbnail for this one!"); + + safe_snprintf(fname, sizeof(fname), "%s/.thumbs/%s-t.png", + dirname, d_names[num_files]); + + fi = fopen(fname, "wb"); + if (fi == NULL) + { + fprintf(stderr, + "\nError: Couldn't save thumbnail of saved image!\n" + "%s\n" "The error that occurred was:\n" "%s\n\n", + fname, strerror(errno)); + } + else + { + do_png_save(fi, fname, thumbs[num_files], 0); + } + + show_progress_bar(screen); + + num_files++; + } + } + } } + } } + } #ifdef DEBUG printf("%d saved files were found!\n", num_files); @@ -17492,20 +18579,20 @@ static int do_slideshow(void) user wants to make a slideshow out of more recent images (vs. very old ones) */ if (!reversesort) - { - /* Default sort puts newest at the top, oldest at the bottom, - so start at the end */ - which = num_files - 1; - cur = ((num_files - 16) / 4) * 4; - if (cur < 0) - cur = 0; - } - else - { - /* "reversesort" option puts oldest at the top, so start there */ - which = 0; + { + /* Default sort puts newest at the top, oldest at the bottom, + so start at the end */ + which = num_files - 1; + cur = ((num_files - 16) / 4) * 4; + if (cur < 0) cur = 0; - } + } + else + { + /* "reversesort" option puts oldest at the top, so start there */ + which = 0; + cur = 0; + } update_list = 1; @@ -17519,590 +18606,657 @@ static int do_slideshow(void) do_setcursor(cursor_arrow); do + { + /* Update screen: */ + + if (update_list) { - /* Update screen: */ + /* Erase screen: */ - if (update_list) + dest.x = r_ttools.w; + dest.y = 0; + dest.w = WINDOW_WIDTH - r_ttoolopt.w - r_ttools.w; + dest.h = button_h * buttons_tall + r_ttools.h; + + SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 255, 255, 255)); + + + /* Draw icons: */ + + for (i = cur; i < cur + 16 && i < num_files; i++) + { + /* Draw cursor: */ + + dest.x = THUMB_W * ((i - cur) % 4) + r_ttools.w; + dest.y = THUMB_H * ((i - cur) / 4) + img_scroll_up->h; + + if (i == which) { - /* Erase screen: */ + SDL_BlitSurface(img_cursor_down, NULL, screen, &dest); + debug(d_names[i]); + } + else + SDL_BlitSurface(img_cursor_up, NULL, screen, &dest); - dest.x = r_ttools.w; - dest.y = 0; - dest.w = WINDOW_WIDTH - r_ttoolopt.w - r_ttools.w; - dest.h = button_h * buttons_tall + r_ttools.h; + if (thumbs[i] != NULL) + { + dest.x = + THUMB_W * ((i - cur) % 4) + r_ttools.w + 10 + (THUMB_W - 20 - + thumbs[i]->w) / 2; + dest.y = + THUMB_H * ((i - cur) / 4) + img_scroll_up->h + 10 + (THUMB_H - + 20 - + thumbs[i]->h) + / 2; - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 255, 255, 255)); + SDL_BlitSurface(thumbs[i], NULL, screen, &dest); + } + + found = -1; + + for (j = 0; j < num_selected && found == -1; j++) + { + if (selected[j] == i) + found = j; + } + + if (found != -1) + { + dest.x = + (THUMB_W * ((i - cur) % 4) + r_ttools.h + 10 + + (THUMB_W - 20 - thumbs[i]->w) / 2) + thumbs[i]->w; + dest.y = + (THUMB_H * ((i - cur) / 4) + img_scroll_up->h + 10 + + (THUMB_H - 20 - thumbs[i]->h) / 2) + thumbs[i]->h; + + draw_selection_digits(dest.x, dest.y, found + 1); + } + } - /* Draw icons: */ + /* Draw arrows: */ - for (i = cur; i < cur + 16 && i < num_files; i++) + dest.x = (WINDOW_WIDTH - img_scroll_up->w) / 2; + dest.y = 0; + + if (cur > 0) + SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); + else + SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); + + dest.x = (WINDOW_WIDTH - img_scroll_up->w) / 2; + dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; + + if (cur < num_files - 16) + SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + else + SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); + + + /* "Play" button: */ + + dest.x = r_ttools.w; + dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; + SDL_BlitSurface(img_play, NULL, screen, &dest); + + dest.x = r_ttools.w + (button_w - img_openlabels_play->w) / 2; + dest.y = + (button_h * buttons_tall + r_ttools.h) - img_openlabels_play->h; + SDL_BlitSurface(img_openlabels_play, NULL, screen, &dest); + + + /* "GIF Export" button: */ + + dest.x = WINDOW_WIDTH - r_ttoolopt.w - button_w * 2; + dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + + dest.x = + WINDOW_WIDTH - r_ttoolopt.w - button_w * 2 + (button_w - + img_gif_export->w) / 2; + dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; + SDL_BlitSurface(img_gif_export, NULL, screen, &dest); + + dest.x = + WINDOW_WIDTH - r_ttoolopt.w - button_w * 2 + (button_w - + img_openlabels_gif_export-> + w) / 2; + dest.y = + (button_h * buttons_tall + r_ttools.h) - img_openlabels_gif_export->h; + SDL_BlitSurface(img_openlabels_gif_export, NULL, screen, &dest); + + + /* "Back" button: */ + + dest.x = WINDOW_WIDTH - r_ttoolopt.w - button_w; + dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; + SDL_BlitSurface(img_back, NULL, screen, &dest); + + dest.x = + WINDOW_WIDTH - r_ttoolopt.w - button_w + (button_w - + img_openlabels_back->w) / 2; + dest.y = + (button_h * buttons_tall + r_ttools.h) - img_openlabels_back->h; + SDL_BlitSurface(img_openlabels_back, NULL, screen, &dest); + + + /* Speed control: */ + + speeds = 10; + x_per = (float) r_ttools.w / speeds; + y_per = (float) button_h / speeds; + + for (i = 0; i < speeds; i++) + { + xx = ceil(x_per); + yy = ceil(y_per * i); + + if (i <= speed) + btn = thumbnail(img_btn_down, xx, yy, 0); + else + btn = thumbnail(img_btn_up, xx, yy, 0); + + blnk = thumbnail(img_btn_off, xx, button_h - yy, 0); + + /* FIXME: Check for NULL! */ + + dest.x = r_ttools.w + button_w + (i * x_per); + dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; + SDL_BlitSurface(blnk, NULL, screen, &dest); + + dest.x = r_ttools.w + button_w + (i * x_per); + dest.y = (button_h * buttons_tall + r_ttools.h) - (y_per * i); + SDL_BlitSurface(btn, NULL, screen, &dest); + + SDL_FreeSurface(btn); + SDL_FreeSurface(blnk); + } + + SDL_Flip(screen); + + update_list = 0; + } + + /* Was a call to SDL_WaitEvent(&event); before, + changed to this while loop in order to get joystick working */ + while (SDL_PollEvent(&event)) + { + if (event.type == SDL_QUIT) + { + done = 1; + + /* FIXME: Handle SDL_Quit better */ + } + else if (event.type == SDL_WINDOWEVENT) + { + handle_active(&event); + } + else if (event.type == SDL_KEYUP) + { + key = event.key.keysym.sym; + + handle_keymouse(key, SDL_KEYUP, 24, NULL, NULL); + } + else if (event.type == SDL_KEYDOWN) + { + key = event.key.keysym.sym; + + + dest.x = button_w * 3; + dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; + dest.w = button_w * 2; + dest.h = button_h; + + handle_keymouse(key, SDL_KEYDOWN, 24, &dest, NULL); + + if (key == SDLK_RETURN) + { + /* Play */ + + // done = 1; + // playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); + event.type = SDL_MOUSEBUTTONDOWN; + event.button.x = button_w * 2 + 5; + event.button.y = + (button_h * buttons_tall + r_ttools.h) - button_h + 5; + event.button.button = 1; + SDL_PushEvent(&event); + + + } + else if (key == SDLK_ESCAPE || key == SDLK_AC_BACK) + { + /* Go back: */ + + go_back = 1; + done = 1; + playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); + } + } + else + if ((event.type == SDL_MOUSEBUTTONDOWN + && valid_click(event.button.button)) + || event.type == TP_SDL_MOUSEBUTTONSCROLL) + { + if (event.button.x >= r_ttools.w + && event.button.x < WINDOW_WIDTH - r_ttoolopt.w + && event.button.y >= img_scroll_up->h + && event.button.y < + (button_h * buttons_tall + r_ttools.h - button_h)) + { + /* Picked an icon! */ + + which = + ((event.button.x - r_ttools.w) / (THUMB_W) + + (((event.button.y - img_scroll_up->h) / THUMB_H) * 4)) + cur; + + if (which < num_files) + { + playsound(screen, 1, SND_BLEEP, 1, event.button.x, SNDDIST_NEAR); + + /* Is it selected already? */ + + found = -1; + for (i = 0; i < num_selected && found == -1; i++) { - /* Draw cursor: */ - - dest.x = THUMB_W * ((i - cur) % 4) + r_ttools.w; - dest.y = THUMB_H * ((i - cur) / 4) + img_scroll_up->h; - - if (i == which) - { - SDL_BlitSurface(img_cursor_down, NULL, screen, &dest); - debug(d_names[i]); - } - else - SDL_BlitSurface(img_cursor_up, NULL, screen, &dest); - - if (thumbs[i] != NULL) - { - dest.x = THUMB_W * ((i - cur) % 4) + r_ttools.w + 10 + (THUMB_W - 20 - thumbs[i]->w) / 2; - dest.y = THUMB_H * ((i - cur) / 4) + img_scroll_up->h + 10 + (THUMB_H - 20 - thumbs[i]->h) / 2; - - SDL_BlitSurface(thumbs[i], NULL, screen, &dest); - } - - found = -1; - - for (j = 0; j < num_selected && found == -1; j++) - { - if (selected[j] == i) - found = j; - } - - if (found != -1) - { - dest.x = (THUMB_W * ((i - cur) % 4) + r_ttools.h + 10 + (THUMB_W - 20 - thumbs[i]->w) / 2) + thumbs[i]->w; - dest.y = (THUMB_H * ((i - cur) / 4) + img_scroll_up->h + 10 + (THUMB_H - 20 - thumbs[i]->h) / 2) + thumbs[i]->h; - - draw_selection_digits(dest.x, dest.y, found + 1); - } + if (selected[i] == which) + found = i; } - - /* Draw arrows: */ - - dest.x = (WINDOW_WIDTH - img_scroll_up->w) / 2; - dest.y = 0; - - if (cur > 0) - SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); - else - SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); - - dest.x = (WINDOW_WIDTH - img_scroll_up->w) / 2; - dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; - - if (cur < num_files - 16) - SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); - else - SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); - - - /* "Play" button: */ - - dest.x = r_ttools.w; - dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; - SDL_BlitSurface(img_play, NULL, screen, &dest); - - dest.x = r_ttools.w + (button_w - img_openlabels_play->w) / 2; - dest.y = (button_h * buttons_tall + r_ttools.h) - img_openlabels_play->h; - SDL_BlitSurface(img_openlabels_play, NULL, screen, &dest); - - - /* "GIF Export" button: */ - - dest.x = WINDOW_WIDTH - r_ttoolopt.w - button_w * 2; - dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); - - dest.x = WINDOW_WIDTH - r_ttoolopt.w - button_w * 2 + (button_w - img_gif_export->w) / 2; - dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; - SDL_BlitSurface(img_gif_export, NULL, screen, &dest); - - dest.x = WINDOW_WIDTH - r_ttoolopt.w - button_w * 2 + (button_w - img_openlabels_gif_export->w) / 2; - dest.y = (button_h * buttons_tall + r_ttools.h) - img_openlabels_gif_export->h; - SDL_BlitSurface(img_openlabels_gif_export, NULL, screen, &dest); - - - /* "Back" button: */ - - dest.x = WINDOW_WIDTH - r_ttoolopt.w - button_w; - dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; - SDL_BlitSurface(img_back, NULL, screen, &dest); - - dest.x = WINDOW_WIDTH - r_ttoolopt.w - button_w + (button_w - img_openlabels_back->w) / 2; - dest.y = (button_h * buttons_tall + r_ttools.h) - img_openlabels_back->h; - SDL_BlitSurface(img_openlabels_back, NULL, screen, &dest); - - - /* Speed control: */ - - speeds = 10; - x_per = (float)r_ttools.w / speeds; - y_per = (float)button_h / speeds; - - for (i = 0; i < speeds; i++) + if (found == -1) { - xx = ceil(x_per); - yy = ceil(y_per * i); + /* No! Select it! */ - if (i <= speed) - btn = thumbnail(img_btn_down, xx, yy, 0); - else - btn = thumbnail(img_btn_up, xx, yy, 0); - - blnk = thumbnail(img_btn_off, xx, button_h - yy, 0); - - /* FIXME: Check for NULL! */ - - dest.x = r_ttools.w + button_w + (i * x_per); - dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; - SDL_BlitSurface(blnk, NULL, screen, &dest); - - dest.x = r_ttools.w + button_w + (i * x_per); - dest.y = (button_h * buttons_tall + r_ttools.h) - (y_per * i); - SDL_BlitSurface(btn, NULL, screen, &dest); - - SDL_FreeSurface(btn); - SDL_FreeSurface(blnk); + selected[num_selected++] = which; } + else + { + /* Yes! Unselect it! */ + + for (i = found; i < num_selected - 1; i++) + selected[i] = selected[i + 1]; + + num_selected--; + } + + update_list = 1; + } + } + else if (event.button.x >= (WINDOW_WIDTH - img_scroll_up->w) / 2 && + event.button.x <= (WINDOW_WIDTH + img_scroll_up->w) / 2) + { + if (event.button.y < img_scroll_up->h || + (event.button.y >= + (button_h * buttons_tall + r_ttools.h - button_h) + && event.button.y < + (button_h * buttons_tall + r_ttools.h - img_scroll_down->h))) + { + /* Up or Down scroll button in Slideshow dialog: */ + + if (event.button.y < img_scroll_up->h) + { + /* Up scroll button: */ + + if (cur > 0) + { + cur = cur - 4; + update_list = 1; + playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, + SNDDIST_NEAR); + + if (cur == 0) + do_setcursor(cursor_arrow); + } + + if (which >= cur + 16) + which = which - 4; + } + else if (event.button.y >= + (button_h * buttons_tall + r_ttools.h - button_h) + && event.button.y < + (button_h * buttons_tall + r_ttools.h - + img_scroll_down->h)) + { + /* Down scroll button: */ + + if (cur < num_files - 16) + { + cur = cur + 4; + update_list = 1; + playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, + SNDDIST_NEAR); + + if (cur >= num_files - 16) + do_setcursor(cursor_arrow); + } + + if (which < cur) + which = which + 4; + } + } + + if (scrolltimer_dialog != TIMERID_NONE) + { + SDL_RemoveTimer(scrolltimer_dialog); + scrolltimer_dialog = TIMERID_NONE; + } + + if (!scrolling_dialog && event.type == SDL_MOUSEBUTTONDOWN) + { + DEBUG_PRINTF("Starting scrolling\n"); + memcpy(&scrolltimer_dialog_event, &event, sizeof(SDL_Event)); + scrolltimer_dialog_event.type = TP_SDL_MOUSEBUTTONSCROLL; + + /* + * We enable the timer subsystem only when needed (e.g., to use SDL_AddTimer() needed + * for scrolling) then disable it immediately after (e.g., after the timer has fired or + * after SDL_RemoveTimer()) because enabling the timer subsystem in SDL1 has a high + * energy impact on the Mac. + */ + + scrolling_dialog = 1; + SDL_InitSubSystem(SDL_INIT_TIMER); + scrolltimer_dialog = + SDL_AddTimer(REPEAT_SPEED, scrolltimer_dialog_callback, + (void *) &scrolltimer_dialog_event); + } + else + { + DEBUG_PRINTF("Continuing scrolling\n"); + scrolltimer_dialog = + SDL_AddTimer(REPEAT_SPEED / 3, scrolltimer_dialog_callback, + (void *) &scrolltimer_dialog_event); + } + } + else if (event.button.x >= r_ttools.w + && event.button.x < r_ttools.w + button_w + && event.button.y >= + (button_h * buttons_tall + r_ttools.h) - button_h + && event.button.y < (button_h * buttons_tall + r_ttools.h)) + { + /* Play */ + + playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); + + + /* If none selected, select all, in order! */ + + if (num_selected == 0) + { + for (i = 0; i < num_files; i++) + selected[i] = i; + num_selected = num_files; + } + + play_slideshow(selected, num_selected, dirname, d_names, d_exts, + speed); + + + /* Redraw entire screen, after playback: */ + + SDL_FillRect(screen, NULL, + SDL_MapRGB(canvas->format, 255, 255, 255)); + draw_toolbar(); + draw_colors(COLORSEL_CLOBBER_WIPE); + draw_none(); + + /* Instructions for Slideshow file dialog */ + freeme = textdir(TUX_TIP_SLIDESHOW); + draw_tux_text(TUX_BORED, freeme, 1); + free(freeme); SDL_Flip(screen); - update_list = 0; + update_list = 1; } - - /* Was a call to SDL_WaitEvent(&event); before, - changed to this while loop in order to get joystick working */ - while (SDL_PollEvent(&event)) + else if (event.button.x >= r_ttools.w + button_w + && event.button.x < r_ttools.w + button_w + r_ttools.w + && event.button.y >= + (button_h * buttons_tall + r_ttools.h) - button_h + && event.button.y < (button_h * buttons_tall + r_ttools.h)) { - if (event.type == SDL_QUIT) - { - done = 1; + /* Speed slider */ - /* FIXME: Handle SDL_Quit better */ - } - else if (event.type == SDL_WINDOWEVENT) - { - handle_active(&event); - } - else if (event.type == SDL_KEYUP) - { - key = event.key.keysym.sym; + int old_speed, control_sound, click_x; - handle_keymouse(key, SDL_KEYUP, 24, NULL, NULL); - } - else if (event.type == SDL_KEYDOWN) - { - key = event.key.keysym.sym; + old_speed = speed; + click_x = event.button.x - r_ttools.w - button_w; + speed = ((10 * click_x) / r_ttools.w); - dest.x = button_w * 3; - dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; - dest.w = button_w * 2; - dest.h = button_h; + control_sound = -1; - handle_keymouse(key, SDL_KEYDOWN, 24, &dest, NULL); + if (speed < old_speed) + control_sound = SND_SHRINK; + else if (speed > old_speed) + control_sound = SND_GROW; - if (key == SDLK_RETURN) - { - /* Play */ + if (control_sound != -1) + { + playsound(screen, 0, control_sound, 0, SNDPOS_CENTER, + SNDDIST_NEAR); - // done = 1; - // playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); - event.type = SDL_MOUSEBUTTONDOWN; - event.button.x = button_w * 2 + 5; - event.button.y = (button_h * buttons_tall + r_ttools.h) - button_h + 5; - event.button.button = 1; - SDL_PushEvent(&event); + update_list = 1; + } + } + else if (event.button.x >= + (WINDOW_WIDTH - r_ttoolopt.w - button_w - button_w) + && event.button.x < (WINDOW_WIDTH - r_ttoolopt.w - button_w) + && event.button.y >= + (button_h * buttons_tall + r_ttools.h) - button_h + && event.button.y < (button_h * buttons_tall + r_ttools.h)) + { + /* GIF Export */ + playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - } - else if (key == SDLK_ESCAPE || key == SDLK_AC_BACK) - { - /* Go back: */ + if (num_selected < 2) + { + /* None selected? Too dangerous to automatically select all (like we do for slideshow playback). + Only 1 selected? No point in saving as GIF. + */ + freeme = + textdir(gettext_noop + ("Select 2 or more drawings to turn into an animated GIF.")); + draw_tux_text(TUX_BORED, freeme, 1); + free(freeme); - go_back = 1; - done = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - } - } - else if ((event.type == SDL_MOUSEBUTTONDOWN && valid_click(event.button.button)) || - event.type == TP_SDL_MOUSEBUTTONSCROLL) - { - if (event.button.x >= r_ttools.w && event.button.x < WINDOW_WIDTH - r_ttoolopt.w && - event.button.y >= img_scroll_up->h && event.button.y < (button_h * buttons_tall + r_ttools.h - button_h)) - { - /* Picked an icon! */ + control_drawtext_timer(2000, instructions, 0); /* N.B. It will draw instructions, regardless */ + } + else + { + export_successful = + export_gif(selected, num_selected, dirname, d_names, d_exts, + speed); - which = ((event.button.x - r_ttools.w) / (THUMB_W) + (((event.button.y - img_scroll_up->h) / THUMB_H) * 4)) + cur; + /* Redraw entire screen, after export: */ + SDL_FillRect(screen, NULL, + SDL_MapRGB(canvas->format, 255, 255, 255)); + draw_toolbar(); + draw_colors(COLORSEL_CLOBBER_WIPE); + draw_none(); - if (which < num_files) - { - playsound(screen, 1, SND_BLEEP, 1, event.button.x, SNDDIST_NEAR); + /* Show a message depending on success */ + if (export_successful) + do_prompt_snd(PROMPT_GIF_EXPORT_TXT, PROMPT_EXPORT_YES, "", + SND_TUXOK, screen->w / 2, screen->h / 2); + else + do_prompt_snd(PROMPT_GIF_EXPORT_FAILED_TXT, PROMPT_EXPORT_YES, + "", SND_YOUCANNOT, screen->w / 2, screen->h / 2); - /* Is it selected already? */ + freeme = textdir(TUX_TIP_SLIDESHOW); + draw_tux_text(TUX_BORED, freeme, 1); + free(freeme); - found = -1; - for (i = 0; i < num_selected && found == -1; i++) - { - if (selected[i] == which) - found = i; - } + SDL_Flip(screen); - if (found == -1) - { - /* No! Select it! */ + update_list = 1; + } + } + else if (event.button.x >= (WINDOW_WIDTH - r_ttoolopt.w - button_w) && + event.button.x < (WINDOW_WIDTH - r_ttoolopt.w) && + event.button.y >= + (button_h * buttons_tall + r_ttools.h) - button_h + && event.button.y < (button_h * buttons_tall + r_ttools.h)) + { + /* Back */ - selected[num_selected++] = which; - } - else - { - /* Yes! Unselect it! */ - - for (i = found; i < num_selected - 1; i++) - selected[i] = selected[i + 1]; - - num_selected--; - } - - update_list = 1; - } - } - else if (event.button.x >= (WINDOW_WIDTH - img_scroll_up->w) / 2 && - event.button.x <= (WINDOW_WIDTH + img_scroll_up->w) / 2) - { - if (event.button.y < img_scroll_up->h || - (event.button.y >= (button_h * buttons_tall + r_ttools.h - button_h) && - event.button.y < (button_h * buttons_tall + r_ttools.h - img_scroll_down->h)) - ) - { - /* Up or Down scroll button in Slideshow dialog: */ - - if (event.button.y < img_scroll_up->h) - { - /* Up scroll button: */ - - if (cur > 0) - { - cur = cur - 4; - update_list = 1; - playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, SNDDIST_NEAR); - - if (cur == 0) - do_setcursor(cursor_arrow); - } - - if (which >= cur + 16) - which = which - 4; - } - else if (event.button.y >= (button_h * buttons_tall + r_ttools.h - button_h) && - event.button.y < (button_h * buttons_tall + r_ttools.h - img_scroll_down->h)) - { - /* Down scroll button: */ - - if (cur < num_files - 16) - { - cur = cur + 4; - update_list = 1; - playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, SNDDIST_NEAR); - - if (cur >= num_files - 16) - do_setcursor(cursor_arrow); - } - - if (which < cur) - which = which + 4; - } - } - - if (scrolltimer_dialog != TIMERID_NONE) - { - SDL_RemoveTimer(scrolltimer_dialog); - scrolltimer_dialog = TIMERID_NONE; - } - - if (!scrolling_dialog && event.type == SDL_MOUSEBUTTONDOWN) - { - DEBUG_PRINTF("Starting scrolling\n"); - memcpy(&scrolltimer_dialog_event, &event, sizeof(SDL_Event)); - scrolltimer_dialog_event.type = TP_SDL_MOUSEBUTTONSCROLL; - - /* - * We enable the timer subsystem only when needed (e.g., to use SDL_AddTimer() needed - * for scrolling) then disable it immediately after (e.g., after the timer has fired or - * after SDL_RemoveTimer()) because enabling the timer subsystem in SDL1 has a high - * energy impact on the Mac. - */ - - scrolling_dialog = 1; - SDL_InitSubSystem(SDL_INIT_TIMER); - scrolltimer_dialog = - SDL_AddTimer(REPEAT_SPEED, scrolltimer_dialog_callback, (void *)&scrolltimer_dialog_event); - } - else - { - DEBUG_PRINTF("Continuing scrolling\n"); - scrolltimer_dialog = - SDL_AddTimer(REPEAT_SPEED / 3, scrolltimer_dialog_callback, (void *)&scrolltimer_dialog_event); - } - } - else if (event.button.x >= r_ttools.w && event.button.x < r_ttools.w + button_w && - event.button.y >= (button_h * buttons_tall + r_ttools.h) - button_h && - event.button.y < (button_h * buttons_tall + r_ttools.h)) - { - /* Play */ - - playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); - - - /* If none selected, select all, in order! */ - - if (num_selected == 0) - { - for (i = 0; i < num_files; i++) - selected[i] = i; - num_selected = num_files; - } - - play_slideshow(selected, num_selected, dirname, d_names, d_exts, speed); - - - /* Redraw entire screen, after playback: */ - - SDL_FillRect(screen, NULL, SDL_MapRGB(canvas->format, 255, 255, 255)); - draw_toolbar(); - draw_colors(COLORSEL_CLOBBER_WIPE); - draw_none(); - - /* Instructions for Slideshow file dialog */ - freeme = textdir(TUX_TIP_SLIDESHOW); - draw_tux_text(TUX_BORED, freeme, 1); - free(freeme); - - SDL_Flip(screen); - - update_list = 1; - } - else if (event.button.x >= r_ttools.w + button_w && event.button.x < r_ttools.w + button_w + r_ttools.w && - event.button.y >= (button_h * buttons_tall + r_ttools.h) - button_h && - event.button.y < (button_h * buttons_tall + r_ttools.h)) - { - /* Speed slider */ - - int old_speed, control_sound, click_x; - - old_speed = speed; - - click_x = event.button.x - r_ttools.w - button_w; - speed = ((10 * click_x) / r_ttools.w); - - control_sound = -1; - - if (speed < old_speed) - control_sound = SND_SHRINK; - else if (speed > old_speed) - control_sound = SND_GROW; - - if (control_sound != -1) - { - playsound(screen, 0, control_sound, 0, SNDPOS_CENTER, SNDDIST_NEAR); - - update_list = 1; - } - } - else if (event.button.x >= (WINDOW_WIDTH - r_ttoolopt.w - button_w - button_w) && - event.button.x < (WINDOW_WIDTH - r_ttoolopt.w - button_w) && - event.button.y >= (button_h * buttons_tall + r_ttools.h) - button_h && - event.button.y < (button_h * buttons_tall + r_ttools.h)) - { - /* GIF Export */ - - playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - - if (num_selected < 2) - { - /* None selected? Too dangerous to automatically select all (like we do for slideshow playback). - Only 1 selected? No point in saving as GIF. - */ - freeme = textdir(gettext_noop("Select 2 or more drawings to turn into an animated GIF.")); - draw_tux_text(TUX_BORED, freeme, 1); - free(freeme); - - control_drawtext_timer(2000, instructions, 0); /* N.B. It will draw instructions, regardless */ - } - else - { - export_successful = export_gif(selected, num_selected, dirname, d_names, d_exts, speed); - - /* Redraw entire screen, after export: */ - SDL_FillRect(screen, NULL, SDL_MapRGB(canvas->format, 255, 255, 255)); - draw_toolbar(); - draw_colors(COLORSEL_CLOBBER_WIPE); - draw_none(); - - /* Show a message depending on success */ - if (export_successful) - do_prompt_snd(PROMPT_GIF_EXPORT_TXT, PROMPT_EXPORT_YES, "", SND_TUXOK, screen->w / 2, screen->h / 2); - else - do_prompt_snd(PROMPT_GIF_EXPORT_FAILED_TXT, PROMPT_EXPORT_YES, "", SND_YOUCANNOT, screen->w / 2, screen->h / 2); - - freeme = textdir(TUX_TIP_SLIDESHOW); - draw_tux_text(TUX_BORED, freeme, 1); - free(freeme); - - SDL_Flip(screen); - - update_list = 1; - } - } - else if (event.button.x >= (WINDOW_WIDTH - r_ttoolopt.w - button_w) && - event.button.x < (WINDOW_WIDTH - r_ttoolopt.w) && - event.button.y >= (button_h * buttons_tall + r_ttools.h) - button_h && - event.button.y < (button_h * buttons_tall + r_ttools.h)) - { - /* Back */ - - go_back = 1; - done = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - } + go_back = 1; + done = 1; + playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); + } #ifdef __ANDROID__ - start_motion_convert(event); + start_motion_convert(event); #endif - } - else if (event.type == SDL_MOUSEWHEEL && wheely) - { - /* Scroll wheel! */ + } + else if (event.type == SDL_MOUSEWHEEL && wheely) + { + /* Scroll wheel! */ - if (event.wheel.y > 0 && cur > 0) - { - cur = cur - 4; - update_list = 1; - playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, SNDDIST_NEAR); + if (event.wheel.y > 0 && cur > 0) + { + cur = cur - 4; + update_list = 1; + playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, SNDDIST_NEAR); - if (cur == 0) - do_setcursor(cursor_arrow); + if (cur == 0) + do_setcursor(cursor_arrow); - if (which >= cur + 16) - which = which - 4; - } - else if (event.wheel.y < 0 && cur < num_files - 16) - { - cur = cur + 4; - update_list = 1; - playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, SNDDIST_NEAR); + if (which >= cur + 16) + which = which - 4; + } + else if (event.wheel.y < 0 && cur < num_files - 16) + { + cur = cur + 4; + update_list = 1; + playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, SNDDIST_NEAR); - if (cur >= num_files - 16) - do_setcursor(cursor_arrow); + if (cur >= num_files - 16) + do_setcursor(cursor_arrow); - if (which < cur) - which = which + 4; - } - } - else if (event.type == SDL_MOUSEMOTION) - { - /* Deal with mouse pointer shape! */ + if (which < cur) + which = which + 4; + } + } + else if (event.type == SDL_MOUSEMOTION) + { + /* Deal with mouse pointer shape! */ - if (event.button.y < img_scroll_up->h && - event.button.x >= (WINDOW_WIDTH - img_scroll_up->w) / 2 && - event.button.x <= (WINDOW_WIDTH + img_scroll_up->w) / 2 && cur > 0) - { - /* Scroll up button: */ + if (event.button.y < img_scroll_up->h && + event.button.x >= (WINDOW_WIDTH - img_scroll_up->w) / 2 && + event.button.x <= (WINDOW_WIDTH + img_scroll_up->w) / 2 + && cur > 0) + { + /* Scroll up button: */ - do_setcursor(cursor_up); - } - else if (event.button.y >= (button_h * buttons_tall + r_ttools.h - button_h) && - event.button.y < (button_h * buttons_tall + r_ttools.h - img_scroll_up->h) && - event.button.x >= (WINDOW_WIDTH - img_scroll_up->w) / 2 && - event.button.x <= (WINDOW_WIDTH + img_scroll_up->w) / 2 && cur < num_files - 16) - { - /* Scroll down button: */ + do_setcursor(cursor_up); + } + else if (event.button.y >= + (button_h * buttons_tall + r_ttools.h - button_h) + && event.button.y < + (button_h * buttons_tall + r_ttools.h - img_scroll_up->h) + && event.button.x >= (WINDOW_WIDTH - img_scroll_up->w) / 2 + && event.button.x <= (WINDOW_WIDTH + img_scroll_up->w) / 2 + && cur < num_files - 16) + { + /* Scroll down button: */ - do_setcursor(cursor_down); - } - else if (((event.button.x >= r_ttools.w && event.button.x < r_ttools.w + button_w + r_ttools.w) || - (event.button.x >= (WINDOW_WIDTH - r_ttoolopt.w - button_w * 2) && - event.button.x < (WINDOW_WIDTH - r_ttoolopt.w))) && - event.button.y >= (button_h * buttons_tall + r_ttools.h) - button_h && - event.button.y < (button_h * buttons_tall + r_ttools.h)) - { - /* One of the command buttons: */ + do_setcursor(cursor_down); + } + else + if (((event.button.x >= r_ttools.w + && event.button.x < r_ttools.w + button_w + r_ttools.w) + || (event.button.x >= + (WINDOW_WIDTH - r_ttoolopt.w - button_w * 2) + && event.button.x < (WINDOW_WIDTH - r_ttoolopt.w))) + && event.button.y >= + (button_h * buttons_tall + r_ttools.h) - button_h + && event.button.y < (button_h * buttons_tall + r_ttools.h)) + { + /* One of the command buttons: */ - do_setcursor(cursor_hand); - } - else if (event.button.x >= r_ttools.w && event.button.x < WINDOW_WIDTH - r_ttoolopt.w - && event.button.y > img_scroll_up->h - && event.button.y < (button_h * buttons_tall + r_ttools.h) - button_h - && - ((((event.button.x - r_ttools.w) / (THUMB_W) + - (((event.button.y - img_scroll_up->h) / THUMB_H) * 4)) + cur) < num_files)) - { - /* One of the thumbnails: */ + do_setcursor(cursor_hand); + } + else if (event.button.x >= r_ttools.w + && event.button.x < WINDOW_WIDTH - r_ttoolopt.w + && event.button.y > img_scroll_up->h + && event.button.y < + (button_h * buttons_tall + r_ttools.h) - button_h + && + ((((event.button.x - r_ttools.w) / (THUMB_W) + + (((event.button.y - img_scroll_up->h) / THUMB_H) * 4)) + + cur) < num_files)) + { + /* One of the thumbnails: */ - do_setcursor(cursor_hand); - } - else - { - /* Unclickable... */ + do_setcursor(cursor_hand); + } + else + { + /* Unclickable... */ - do_setcursor(cursor_arrow); - } - -#ifdef __ANDROID__ - convert_motion_to_wheel(event); -#endif - - oldpos_x = event.button.x; - oldpos_y = event.button.y; - } - - else if (event.type == SDL_MOUSEBUTTONUP) - { -#ifdef __ANDROID__ - stop_motion_convert(event); -#endif - - if (scrolling_dialog) - { - if (scrolltimer_dialog != TIMERID_NONE) - { - SDL_RemoveTimer(scrolltimer_dialog); - scrolltimer_dialog = TIMERID_NONE; - } - scrolling_dialog = 0; - SDL_QuitSubSystem(SDL_INIT_TIMER); - DEBUG_PRINTF("Killing dialog scrolling\n"); - } - } - - else if (event.type == SDL_JOYAXISMOTION) - handle_joyaxismotion(event, &motioner, &val_x, &val_y); - - else if (event.type == SDL_JOYHATMOTION) - handle_joyhatmotion(event, oldpos_x, oldpos_y, &valhat_x, &valhat_y, &hatmotioner, &old_hat_ticks); - - else if (event.type == SDL_JOYBALLMOTION) - handle_joyballmotion(event, oldpos_x, oldpos_y); - - else if (event.type == SDL_JOYBUTTONDOWN || event.type == SDL_JOYBUTTONUP) - handle_joybuttonupdown(event, oldpos_x, oldpos_y); - - else if (event.type == SDL_USEREVENT) - { - if (event.user.code == USEREVENT_TEXT_UPDATE) - { - if (event.user.data1 != NULL) - { - draw_tux_text(TUX_BORED, instructions, 1); - } - } - } + do_setcursor(cursor_arrow); } - if (motioner | hatmotioner) - handle_motioners(oldpos_x, oldpos_y, motioner, hatmotioner, old_hat_ticks, val_x, val_y, valhat_x, valhat_y); +#ifdef __ANDROID__ + convert_motion_to_wheel(event); +#endif - SDL_Delay(10); + oldpos_x = event.button.x; + oldpos_y = event.button.y; + } + + else if (event.type == SDL_MOUSEBUTTONUP) + { +#ifdef __ANDROID__ + stop_motion_convert(event); +#endif + + if (scrolling_dialog) + { + if (scrolltimer_dialog != TIMERID_NONE) + { + SDL_RemoveTimer(scrolltimer_dialog); + scrolltimer_dialog = TIMERID_NONE; + } + scrolling_dialog = 0; + SDL_QuitSubSystem(SDL_INIT_TIMER); + DEBUG_PRINTF("Killing dialog scrolling\n"); + } + } + + else if (event.type == SDL_JOYAXISMOTION) + handle_joyaxismotion(event, &motioner, &val_x, &val_y); + + else if (event.type == SDL_JOYHATMOTION) + handle_joyhatmotion(event, oldpos_x, oldpos_y, &valhat_x, &valhat_y, + &hatmotioner, &old_hat_ticks); + + else if (event.type == SDL_JOYBALLMOTION) + handle_joyballmotion(event, oldpos_x, oldpos_y); + + else if (event.type == SDL_JOYBUTTONDOWN + || event.type == SDL_JOYBUTTONUP) + handle_joybuttonupdown(event, oldpos_x, oldpos_y); + + else if (event.type == SDL_USEREVENT) + { + if (event.user.code == USEREVENT_TEXT_UPDATE) + { + if (event.user.data1 != NULL) + { + draw_tux_text(TUX_BORED, instructions, 1); + } + } + } } + + if (motioner | hatmotioner) + handle_motioners(oldpos_x, oldpos_y, motioner, hatmotioner, + old_hat_ticks, val_x, val_y, valhat_x, valhat_y); + + SDL_Delay(10); + } while (!done); @@ -18113,10 +19267,10 @@ static int do_slideshow(void) free(thumbs); for (i = 0; i < num_files; i++) - { - free(d_names[i]); - free(d_exts[i]); - } + { + free(d_names[i]); + free(d_exts[i]); + } free(dirname); @@ -18143,7 +19297,8 @@ static int do_slideshow(void) * @char ** d_ext -- array of file exentions of the images to be played * @int speed -- how fast to play the slideshow (0 = no automatic advance, 1 = slowest, 10 = as fast as possible) */ -static void play_slideshow(int *selected, int num_selected, char *dirname, char **d_names, char **d_exts, int speed) +static void play_slideshow(int *selected, int num_selected, char *dirname, + char **d_names, char **d_exts, int speed) { int i, which, next, done; int val_x, val_y, motioner; @@ -18177,202 +19332,209 @@ static void play_slideshow(int *selected, int num_selected, char *dirname, char done = 0; do + { + for (i = 0; i < num_selected && !done; i++) { - for (i = 0; i < num_selected && !done; i++) - { - which = selected[i]; - show_progress_bar(screen); + which = selected[i]; + show_progress_bar(screen); - /* Figure out filename: */ + /* Figure out filename: */ - safe_snprintf(fname, sizeof(fname), "%s/%s%s", dirname, d_names[which], d_exts[which]); + safe_snprintf(fname, sizeof(fname), "%s/%s%s", dirname, d_names[which], + d_exts[which]); - img = myIMG_Load(fname); + img = myIMG_Load(fname); - if (img != NULL) - { - autoscale_copy_smear_free(img, screen, SDL_BlitSurface); + if (img != NULL) + { + autoscale_copy_smear_free(img, screen, SDL_BlitSurface); - safe_strncpy(file_id, d_names[which], sizeof(file_id)); + safe_strncpy(file_id, d_names[which], sizeof(file_id)); /* FIXME: is the starter even used??? -bjk 2009.10.16 */ - /* See if this saved image was based on a 'starter' */ + /* See if this saved image was based on a 'starter' */ - load_starter_id(d_names[which], NULL); + load_starter_id(d_names[which], NULL); - if (starter_id[0] != '\0') - { - load_starter(starter_id); + if (starter_id[0] != '\0') + { + load_starter(starter_id); - if (starter_mirrored) - mirror_starter(); + if (starter_mirrored) + mirror_starter(); - if (starter_flipped) - flip_starter(); - } - else - load_template(template_id); - } - - /* "Back" button: */ - - dest.x = screen->w - button_w; - dest.y = screen->h - button_h; - SDL_BlitSurface(img_back, NULL, screen, &dest); - - dest.x = screen->w - button_w + (button_w - img_openlabels_back->w) / 2; - dest.y = screen->h - img_openlabels_back->h; - SDL_BlitSurface(img_openlabels_back, NULL, screen, &dest); - - /* "Next" button: */ - - dest.x = 0; - dest.y = screen->h - button_h; - SDL_BlitSurface(img_play, NULL, screen, &dest); - - dest.x = (button_w - img_openlabels_next->w) / 2; - dest.y = screen->h - img_openlabels_next->h; - SDL_BlitSurface(img_openlabels_next, NULL, screen, &dest); - - - SDL_Flip(screen); - - - /* Handle any events, and otherwise wait for time to count down: */ - - next = 0; - last_ticks = SDL_GetTicks(); - - do - { - while (SDL_PollEvent(&event)) - { - if (event.type == SDL_QUIT) - { - /* FIXME: Handle SDL_QUIT better! */ - - next = 1; - done = 1; - } - else if (event.type == SDL_WINDOWEVENT) - { - handle_active(&event); - } - else if (event.type == SDL_KEYDOWN) - { - key = event.key.keysym.sym; - - handle_keymouse(key, SDL_KEYDOWN, 24, NULL, NULL); - - if (key == SDLK_RETURN || key == SDLK_SPACE || key == SDLK_PAGEDOWN) - { - /* RETURN, SPACE or PAGEDOWN: Skip to next right away! */ - - next = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); - } - else if (key == SDLK_PAGEUP) - { - /* LEFT: Go back one! */ - - i = i - 2; - - if (i < -1) - i = num_selected - 2; - - next = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); - } - else if (key == SDLK_ESCAPE || key == SDLK_AC_BACK) - { - /* Go back: */ - - next = 1; - done = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - } - } - else if (event.type == SDL_MOUSEBUTTONDOWN) - { - /* Mouse click! */ - - if (event.button.x >= screen->w - button_w && event.button.y >= screen->h - button_h) - { - /* Back button */ - - next = 1; - done = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - } - else - { - /* Otherwise, skip to next image right away! */ - - next = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); - } - } - else if (event.type == SDL_MOUSEMOTION) - { - /* Deal with mouse pointer shape! */ - - if ((event.button.x >= screen->w - button_w || event.button.x < button_w) && event.button.y >= screen->h - button_h) - { - /* Back or Next buttons */ - - do_setcursor(cursor_hand); - } - else - { - /* Otherwise, minimal cursor... */ - - do_setcursor(cursor_tiny); - } - oldpos_x = event.button.x; - oldpos_y = event.button.y; - } - - else if (event.type == SDL_JOYAXISMOTION) - handle_joyaxismotion(event, &motioner, &val_x, &val_y); - - else if (event.type == SDL_JOYHATMOTION) - handle_joyhatmotion(event, oldpos_x, oldpos_y, &valhat_x, &valhat_y, &hatmotioner, &old_hat_ticks); - - else if (event.type == SDL_JOYBALLMOTION) - handle_joyballmotion(event, oldpos_x, oldpos_y); - - else if (event.type == SDL_JOYBUTTONDOWN || event.type == SDL_JOYBUTTONUP) - handle_joybuttonupdown(event, oldpos_x, oldpos_y); - - } - - if (motioner | hatmotioner) - handle_motioners(oldpos_x, oldpos_y, motioner, hatmotioner, old_hat_ticks, val_x, val_y, valhat_x, - valhat_y); - - SDL_Delay(10); - - - /* Automatically skip to the next one after time expires: */ - - if (speed != 0) - { - if (SDL_GetTicks() >= last_ticks + (10 - speed) * 500) - next = 1; - } - } - while (!next); + if (starter_flipped) + flip_starter(); } + else + load_template(template_id); + } + + /* "Back" button: */ + + dest.x = screen->w - button_w; + dest.y = screen->h - button_h; + SDL_BlitSurface(img_back, NULL, screen, &dest); + + dest.x = screen->w - button_w + (button_w - img_openlabels_back->w) / 2; + dest.y = screen->h - img_openlabels_back->h; + SDL_BlitSurface(img_openlabels_back, NULL, screen, &dest); + + /* "Next" button: */ + + dest.x = 0; + dest.y = screen->h - button_h; + SDL_BlitSurface(img_play, NULL, screen, &dest); + + dest.x = (button_w - img_openlabels_next->w) / 2; + dest.y = screen->h - img_openlabels_next->h; + SDL_BlitSurface(img_openlabels_next, NULL, screen, &dest); + + + SDL_Flip(screen); + + + /* Handle any events, and otherwise wait for time to count down: */ + + next = 0; + last_ticks = SDL_GetTicks(); + + do + { + while (SDL_PollEvent(&event)) + { + if (event.type == SDL_QUIT) + { + /* FIXME: Handle SDL_QUIT better! */ + + next = 1; + done = 1; + } + else if (event.type == SDL_WINDOWEVENT) + { + handle_active(&event); + } + else if (event.type == SDL_KEYDOWN) + { + key = event.key.keysym.sym; + + handle_keymouse(key, SDL_KEYDOWN, 24, NULL, NULL); + + if (key == SDLK_RETURN || key == SDLK_SPACE + || key == SDLK_PAGEDOWN) + { + /* RETURN, SPACE or PAGEDOWN: Skip to next right away! */ + + next = 1; + playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); + } + else if (key == SDLK_PAGEUP) + { + /* LEFT: Go back one! */ + + i = i - 2; + + if (i < -1) + i = num_selected - 2; + + next = 1; + playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); + } + else if (key == SDLK_ESCAPE || key == SDLK_AC_BACK) + { + /* Go back: */ + + next = 1; + done = 1; + playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); + } + } + else if (event.type == SDL_MOUSEBUTTONDOWN) + { + /* Mouse click! */ + + if (event.button.x >= screen->w - button_w + && event.button.y >= screen->h - button_h) + { + /* Back button */ + + next = 1; + done = 1; + playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); + } + else + { + /* Otherwise, skip to next image right away! */ + + next = 1; + playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); + } + } + else if (event.type == SDL_MOUSEMOTION) + { + /* Deal with mouse pointer shape! */ + + if ((event.button.x >= screen->w - button_w + || event.button.x < button_w) + && event.button.y >= screen->h - button_h) + { + /* Back or Next buttons */ + + do_setcursor(cursor_hand); + } + else + { + /* Otherwise, minimal cursor... */ + + do_setcursor(cursor_tiny); + } + oldpos_x = event.button.x; + oldpos_y = event.button.y; + } + + else if (event.type == SDL_JOYAXISMOTION) + handle_joyaxismotion(event, &motioner, &val_x, &val_y); + + else if (event.type == SDL_JOYHATMOTION) + handle_joyhatmotion(event, oldpos_x, oldpos_y, &valhat_x, + &valhat_y, &hatmotioner, &old_hat_ticks); + + else if (event.type == SDL_JOYBALLMOTION) + handle_joyballmotion(event, oldpos_x, oldpos_y); + + else if (event.type == SDL_JOYBUTTONDOWN + || event.type == SDL_JOYBUTTONUP) + handle_joybuttonupdown(event, oldpos_x, oldpos_y); + + } + + if (motioner | hatmotioner) + handle_motioners(oldpos_x, oldpos_y, motioner, hatmotioner, + old_hat_ticks, val_x, val_y, valhat_x, valhat_y); + + SDL_Delay(10); + + + /* Automatically skip to the next one after time expires: */ + + if (speed != 0) + { + if (SDL_GetTicks() >= last_ticks + (10 - speed) * 500) + next = 1; + } + } + while (!next); } + } while (!done); /* Restore everything about the currently-active image that got clobbered above */ - strcpy(starter_id, tmp_starter_id); /* safe; originally strdup()'d from the dest. */ + strcpy(starter_id, tmp_starter_id); /* safe; originally strdup()'d from the dest. */ free(tmp_starter_id); strcpy(template_id, tmp_template_id); /* safe; originally strdup()'d from the dest. */ @@ -18402,41 +19564,41 @@ static void draw_selection_digits(int right, int bottom, int n) digit_h = img_select_digits->h; if (n > 99) - { - len = 3; - place = 100; - } + { + len = 3; + place = 100; + } else if (n > 9) - { - len = 2; - place = 10; - } + { + len = 2; + place = 10; + } else - { - len = 1; - place = 1; - } + { + len = 1; + place = 1; + } x = right - digit_w * len; y = bottom - digit_h; for (i = 0; i < len; i++) - { - v = (n / place) % (place * 10); + { + v = (n / place) % (place * 10); - src.x = digit_w * v; - src.y = 0; - src.w = digit_w; - src.h = digit_h; + src.x = digit_w * v; + src.y = 0; + src.w = digit_w; + src.h = digit_h; - dest.x = x; - dest.y = y; + dest.x = x; + dest.y = y; - SDL_BlitSurface(img_select_digits, &src, screen, &dest); + SDL_BlitSurface(img_select_digits, &src, screen, &dest); - x = x + digit_w; - place = place / 10; - } + x = x + digit_w; + place = place / 10; + } } @@ -18448,10 +19610,10 @@ static void wait_for_sfx(void) { #ifndef NOSOUND if (use_sound) - { - while (Mix_Playing(-1)) - SDL_Delay(10); - } + { + while (Mix_Playing(-1)) + SDL_Delay(10); + } #endif } @@ -18482,26 +19644,30 @@ static char stiple[] = "008088080000" "808000000808" "000080880800" - "088080000008" "000000808808" "080880800000" "080000008088" "000808808000" "880800000080" "000008088080"; + "088080000008" "000000808808" "080880800000" "080000008088" "000808808000" + "880800000080" "000008088080"; #endif static unsigned char *stamp_outline_data; static int stamp_outline_w, stamp_outline_h; -static void reset_stamps(int * stamp_xored_rt, int * stamp_place_x, int * stamp_place_y, int * stamp_tool_mode) +static void reset_stamps(int *stamp_xored_rt, int *stamp_place_x, + int *stamp_place_y, int *stamp_tool_mode) { - if (!active_stamp) - return; + if (!active_stamp) + return; - *stamp_xored_rt = 0; - *stamp_tool_mode = STAMP_TOOL_MODE_PLACE; - int half_bigbox = sqrt((CUR_STAMP_W + 1) * (CUR_STAMP_W + 1) + (CUR_STAMP_H + 1) * (CUR_STAMP_H + 1)) / 2; - update_screen(*stamp_place_x - half_bigbox + r_canvas.x, - *stamp_place_y - half_bigbox + r_canvas.y, - *stamp_place_x + half_bigbox + r_canvas.x, - *stamp_place_y + half_bigbox + r_canvas.y); - update_stamp_xor(0); + *stamp_xored_rt = 0; + *stamp_tool_mode = STAMP_TOOL_MODE_PLACE; + int half_bigbox = + sqrt((CUR_STAMP_W + 1) * (CUR_STAMP_W + 1) + + (CUR_STAMP_H + 1) * (CUR_STAMP_H + 1)) / 2; + update_screen(*stamp_place_x - half_bigbox + r_canvas.x, + *stamp_place_y - half_bigbox + r_canvas.y, + *stamp_place_x + half_bigbox + r_canvas.x, + *stamp_place_y + half_bigbox + r_canvas.y); + update_stamp_xor(0); } /** @@ -18525,27 +19691,28 @@ static void update_stamp_xor(int stamp_angle_rotation) /* start by scaling */ src = thumbnail(src, CUR_STAMP_W, CUR_STAMP_H, 0); - if(stamp_angle_rotation) - { - SDL_Surface * aux_surf = src; - src = rotozoomSurface(aux_surf, stamp_angle_rotation, 1.0, SMOOTHING_ON); - SDL_FreeSurface(aux_surf); - } + if (stamp_angle_rotation) + { + SDL_Surface *aux_surf = src; + src = rotozoomSurface(aux_surf, stamp_angle_rotation, 1.0, SMOOTHING_ON); + SDL_FreeSurface(aux_surf); + } getpixel = getpixels[src->format->BytesPerPixel]; alphabits = calloc(src->w + 4, src->h + 4); SDL_LockSurface(src); for (yy = 0; yy < src->h; yy++) + { + ry = yy; + for (xx = 0; xx < src->w; xx++) { - ry = yy; - for (xx = 0; xx < src->w; xx++) - { - rx = xx; - SDL_GetRGBA(getpixel(src, rx, ry), - src->format, &dummy, &dummy, &dummy, alphabits + xx + 2 + (yy + 2) * (src->w + 4)); - } + rx = xx; + SDL_GetRGBA(getpixel(src, rx, ry), + src->format, &dummy, &dummy, &dummy, + alphabits + xx + 2 + (yy + 2) * (src->w + 4)); } + } SDL_UnlockSurface(src); new_w = src->w + 4; @@ -18554,46 +19721,46 @@ static void update_stamp_xor(int stamp_angle_rotation) outline = calloc(new_w, new_h); for (yy = 1; yy < new_h - 1; yy++) + { + for (xx = 1; xx < new_w - 1; xx++) { - for (xx = 1; xx < new_w - 1; xx++) - { - unsigned char above = 0; - unsigned char below = 0xff; - unsigned char tmp; + unsigned char above = 0; + unsigned char below = 0xff; + unsigned char tmp; - tmp = alphabits[(xx - 1) + (yy - 1) * new_w]; - above |= tmp; - below &= tmp; - tmp = alphabits[(xx + 1) + (yy - 1) * new_w]; - above |= tmp; - below &= tmp; + tmp = alphabits[(xx - 1) + (yy - 1) * new_w]; + above |= tmp; + below &= tmp; + tmp = alphabits[(xx + 1) + (yy - 1) * new_w]; + above |= tmp; + below &= tmp; - tmp = alphabits[(xx + 0) + (yy - 1) * new_w]; - above |= tmp; - below &= tmp; - tmp = alphabits[(xx + 0) + (yy + 0) * new_w]; - above |= tmp; - below &= tmp; - tmp = alphabits[(xx + 1) + (yy + 0) * new_w]; - above |= tmp; - below &= tmp; - tmp = alphabits[(xx - 1) + (yy + 0) * new_w]; - above |= tmp; - below &= tmp; - tmp = alphabits[(xx + 0) + (yy + 1) * new_w]; - above |= tmp; - below &= tmp; + tmp = alphabits[(xx + 0) + (yy - 1) * new_w]; + above |= tmp; + below &= tmp; + tmp = alphabits[(xx + 0) + (yy + 0) * new_w]; + above |= tmp; + below &= tmp; + tmp = alphabits[(xx + 1) + (yy + 0) * new_w]; + above |= tmp; + below &= tmp; + tmp = alphabits[(xx - 1) + (yy + 0) * new_w]; + above |= tmp; + below &= tmp; + tmp = alphabits[(xx + 0) + (yy + 1) * new_w]; + above |= tmp; + below &= tmp; - tmp = alphabits[(xx - 1) + (yy + 1) * new_w]; - above |= tmp; - below &= tmp; - tmp = alphabits[(xx + 1) + (yy + 1) * new_w]; - above |= tmp; - below &= tmp; + tmp = alphabits[(xx - 1) + (yy + 1) * new_w]; + above |= tmp; + below &= tmp; + tmp = alphabits[(xx + 1) + (yy + 1) * new_w]; + above |= tmp; + below &= tmp; - outline[xx + yy * new_w] = (above ^ below) >> 7; - } + outline[xx + yy * new_w] = (above ^ below) >> 7; } + } old_outline_data = stamp_outline_data; stamp_outline_data = outline; @@ -18613,18 +19780,18 @@ static void stamp_xor(int x, int y) SDL_LockSurface(screen); for (yy = 0; yy < stamp_outline_h; yy++) + { + for (xx = 0; xx < stamp_outline_w; xx++) { - for (xx = 0; xx < stamp_outline_w; xx++) - { - if (!stamp_outline_data[xx + yy * stamp_outline_w]) /* FIXME: Conditional jump or move depends on uninitialised value(s) */ - continue; - sx = x + xx - stamp_outline_w / 2; - sy = y + yy - stamp_outline_h / 2; - if (stiple[sx % STIPLE_W + sy % STIPLE_H * STIPLE_W] != '8') - continue; - xorpixel(sx, sy); - } + if (!stamp_outline_data[xx + yy * stamp_outline_w]) /* FIXME: Conditional jump or move depends on uninitialised value(s) */ + continue; + sx = x + xx - stamp_outline_w / 2; + sy = y + yy - stamp_outline_h / 2; + if (stiple[sx % STIPLE_W + sy % STIPLE_H * STIPLE_W] != '8') + continue; + xorpixel(sx, sy); } + } SDL_UnlockSurface(screen); } @@ -18633,7 +19800,8 @@ static void stamp_xor(int x, int y) /** * FIXME */ -static void rgbtohsv(Uint8 r8, Uint8 g8, Uint8 b8, float *h, float *s, float *v) +static void rgbtohsv(Uint8 r8, Uint8 g8, Uint8 b8, float *h, float *s, + float *v) { float rgb_min, rgb_max, delta, r, g, b; @@ -18648,93 +19816,94 @@ static void rgbtohsv(Uint8 r8, Uint8 g8, Uint8 b8, float *h, float *s, float *v) delta = rgb_max - rgb_min; if (rgb_max == 0) - { - /* Black */ + { + /* Black */ - *s = 0; - *h = -1; - } + *s = 0; + *h = -1; + } else - { - *s = delta / rgb_max; + { + *s = delta / rgb_max; - if (r == rgb_max) - *h = (g - b) / delta; - else if (g == rgb_max) - *h = 2 + (b - r) / delta; /* between cyan & yellow */ - else - *h = 4 + (r - g) / delta; /* between magenta & cyan */ + if (r == rgb_max) + *h = (g - b) / delta; + else if (g == rgb_max) + *h = 2 + (b - r) / delta; /* between cyan & yellow */ + else + *h = 4 + (r - g) / delta; /* between magenta & cyan */ - *h = (*h * 60); /* degrees */ + *h = (*h * 60); /* degrees */ - if (*h < 0) - *h = (*h + 360); - } + if (*h < 0) + *h = (*h + 360); + } } /** * FIXME */ -static void hsvtorgb(float h, float s, float v, Uint8 * r8, Uint8 * g8, Uint8 * b8) +static void hsvtorgb(float h, float s, float v, Uint8 * r8, Uint8 * g8, + Uint8 * b8) { int i; float f, p, q, t, r, g, b; if (s == 0) - { - /* Achromatic (grey) */ + { + /* Achromatic (grey) */ + r = v; + g = v; + b = v; + } + else + { + h = h / 60; + i = floor(h); + f = h - i; + p = v * (1 - s); + q = v * (1 - s * f); + t = v * (1 - s * (1 - f)); + + if (i == 0) + { r = v; + g = t; + b = p; + } + else if (i == 1) + { + r = q; g = v; + b = p; + } + else if (i == 2) + { + r = p; + g = v; + b = t; + } + else if (i == 3) + { + r = p; + g = q; b = v; } - else + else if (i == 4) { - h = h / 60; - i = floor(h); - f = h - i; - p = v * (1 - s); - q = v * (1 - s * f); - t = v * (1 - s * (1 - f)); - - if (i == 0) - { - r = v; - g = t; - b = p; - } - else if (i == 1) - { - r = q; - g = v; - b = p; - } - else if (i == 2) - { - r = p; - g = v; - b = t; - } - else if (i == 3) - { - r = p; - g = q; - b = v; - } - else if (i == 4) - { - r = t; - g = p; - b = v; - } - else - { - r = v; - g = p; - b = q; - } + r = t; + g = p; + b = v; } + else + { + r = v; + g = p; + b = q; + } + } *r8 = (Uint8) (r * 255); @@ -18750,38 +19919,42 @@ static void print_image(void) int cur_time, scroll; cur_time = SDL_GetTicks() / 1000; - scroll = (NUM_TOOLS > buttons_tall * gd_tools.cols) ? img_scroll_down->h : 0; + scroll = + (NUM_TOOLS > buttons_tall * gd_tools.cols) ? img_scroll_down->h : 0; #ifdef DEBUG printf("Current time = %d\n", cur_time); #endif if (cur_time >= last_print_time + print_delay) + { + if (alt_print_command_default == ALTPRINT_ALWAYS) + want_alt_printcommand = 1; + else if (alt_print_command_default == ALTPRINT_NEVER) + want_alt_printcommand = 0; + else /* ALTPRINT_MOD */ + want_alt_printcommand = (SDL_GetModState() & KMOD_ALT); + + if (do_prompt_image_snd(PROMPT_PRINT_NOW_TXT, + PROMPT_PRINT_NOW_YES, + PROMPT_PRINT_NOW_NO, + img_printer, NULL, NULL, SND_AREYOUSURE, + (TOOL_PRINT % 2) * button_w + button_w / 2, + (TOOL_PRINT / 2) * button_h + r_ttools.h + + button_h / 2 - + tool_scroll * button_h / gd_tools.cols + scroll)) { - if (alt_print_command_default == ALTPRINT_ALWAYS) - want_alt_printcommand = 1; - else if (alt_print_command_default == ALTPRINT_NEVER) - want_alt_printcommand = 0; - else /* ALTPRINT_MOD */ - want_alt_printcommand = (SDL_GetModState() & KMOD_ALT); + do_print(); - if (do_prompt_image_snd(PROMPT_PRINT_NOW_TXT, - PROMPT_PRINT_NOW_YES, - PROMPT_PRINT_NOW_NO, - img_printer, NULL, NULL, SND_AREYOUSURE, - (TOOL_PRINT % 2) * button_w + button_w / 2, - (TOOL_PRINT / 2) * button_h + r_ttools.h + button_h / 2 - tool_scroll * button_h / gd_tools.cols + scroll)) - { - do_print(); - - last_print_time = cur_time; - } + last_print_time = cur_time; } + } else - { - do_prompt_image_snd(PROMPT_PRINT_TOO_SOON_TXT, - PROMPT_PRINT_TOO_SOON_YES, "", img_printer_wait, NULL, NULL, SND_YOUCANNOT, 0, screen->h); - } + { + do_prompt_image_snd(PROMPT_PRINT_TOO_SOON_TXT, + PROMPT_PRINT_TOO_SOON_YES, "", img_printer_wait, NULL, + NULL, SND_YOUCANNOT, 0, screen->h); + } } /** @@ -18807,25 +19980,28 @@ void do_print(void) pi = popen(pcmd, "w"); if (pi == NULL) - { - perror(pcmd); - } + { + perror(pcmd); + } else - { + { #ifdef PRINTMETHOD_PNG_PNM_PS - if (do_png_save(pi, pcmd, save_canvas, 0)) - do_prompt_snd(PROMPT_PRINT_TXT, PROMPT_PRINT_YES, "", SND_TUXOK, screen->w / 2, screen->h / 2); + if (do_png_save(pi, pcmd, save_canvas, 0)) + do_prompt_snd(PROMPT_PRINT_TXT, PROMPT_PRINT_YES, "", SND_TUXOK, + screen->w / 2, screen->h / 2); #elif defined(PRINTMETHOD_PNM_PS) - /* nothing here */ + /* nothing here */ #elif defined(PRINTMETHOD_PS) - if (do_ps_save(pi, pcmd, save_canvas, papersize, 1)) - do_prompt_snd(PROMPT_PRINT_TXT, PROMPT_PRINT_YES, "", SND_TUXOK, screen->w / 2, screen->h / 2); - else - do_prompt_snd(PROMPT_PRINT_FAILED_TXT, PROMPT_PRINT_YES, "", SND_YOUCANNOT, screen->w / 2, screen->h / 2); + if (do_ps_save(pi, pcmd, save_canvas, papersize, 1)) + do_prompt_snd(PROMPT_PRINT_TXT, PROMPT_PRINT_YES, "", SND_TUXOK, + screen->w / 2, screen->h / 2); + else + do_prompt_snd(PROMPT_PRINT_FAILED_TXT, PROMPT_PRINT_YES, "", + SND_YOUCANNOT, screen->w / 2, screen->h / 2); #else #error No print method defined! #endif - } + } #else #ifdef WIN32 /* Win32 */ @@ -18833,10 +20009,12 @@ void do_print(void) char f[512]; int show = want_alt_printcommand; - safe_snprintf(f, sizeof(f), "%s/%s", savedir, "print.cfg"); /* FIXME */ + safe_snprintf(f, sizeof(f), "%s/%s", savedir, "print.cfg"); /* FIXME */ { - const char *error = SurfacePrint(window_screen, save_canvas, use_print_config ? f : NULL, show); + const char *error = + SurfacePrint(window_screen, save_canvas, use_print_config ? f : NULL, + show); if (error) fprintf(stderr, "%s\n", error); @@ -18850,12 +20028,12 @@ void do_print(void) int show = (want_alt_printcommand && !fullscreen); const char *error = SurfacePrint(save_canvas, show); - + if (error) - { - fprintf(stderr, "Cannot print: %s\n", error); - do_prompt_snd(error, PROMPT_PRINT_YES, "", SND_TUXOK, 0, 0); - } + { + fprintf(stderr, "Cannot print: %s\n", error); + do_prompt_snd(error, PROMPT_PRINT_YES, "", SND_TUXOK, 0, 0); + } #elif defined(__ANDROID__) @@ -18863,31 +20041,39 @@ void do_print(void) Uint8 src_r, src_g, src_b, src_a; SDL_Surface *save_canvas_and = SDL_CreateRGBSurface(0, WINDOW_WIDTH - (96 * 2), - (48 * 7) + 40 + HEIGHTOFFSET, - screen->format->BitsPerPixel, + (48 * 7) + 40 + + HEIGHTOFFSET, + screen->format-> + BitsPerPixel, screen->format->Rmask, screen->format->Gmask, - screen->format->Bmask, 0); + screen->format->Bmask, + 0); for (x = 0; x < save_canvas->w; x++) for (y = 0; y < save_canvas->h; y++) - { - SDL_GetRGBA(getpixels[save_canvas->format->BytesPerPixel] (save_canvas, x, y), - save_canvas->format, &src_r, &src_g, &src_b, &src_a); + { + SDL_GetRGBA(getpixels[save_canvas->format->BytesPerPixel] + (save_canvas, x, y), save_canvas->format, &src_r, &src_g, + &src_b, &src_a); - putpixels[save_canvas_and->format->BytesPerPixel] (save_canvas_and, x, y, - SDL_MapRGBA(save_canvas_and->format, src_r, src_g, src_b, - SDL_ALPHA_OPAQUE)); - } + putpixels[save_canvas_and->format->BytesPerPixel] (save_canvas_and, x, + y, + SDL_MapRGBA + (save_canvas_and-> + format, src_r, + src_g, src_b, + SDL_ALPHA_OPAQUE)); + } const char *error = SurfacePrint(save_canvas_and); if (error) - { - fprintf(stderr, "Cannot print: %s\n", error); - do_prompt_snd(error, PROMPT_PRINT_YES, "", SND_TUXOK, 0, 0); - } + { + fprintf(stderr, "Cannot print: %s\n", error); + do_prompt_snd(error, PROMPT_PRINT_YES, "", SND_TUXOK, 0, 0); + } SDL_FreeSurface(save_canvas_and); #endif @@ -18918,191 +20104,206 @@ static void do_render_cur_text(int do_blit) /* Keep cursor on the screen! */ - if (cursor_y > ((button_h * buttons_tall + r_ttools.h) - TuxPaint_Font_FontHeight(getfonthandle(cur_font)))) - { - cursor_y = ((button_h * buttons_tall + r_ttools.h) - TuxPaint_Font_FontHeight(getfonthandle(cur_font))); - } + if (cursor_y > + ((button_h * buttons_tall + r_ttools.h) - + TuxPaint_Font_FontHeight(getfonthandle(cur_font)))) + { + cursor_y = + ((button_h * buttons_tall + r_ttools.h) - + TuxPaint_Font_FontHeight(getfonthandle(cur_font))); + } /* Render the text: */ if (texttool_len > 0) - { + { #if defined(_FRIBIDI_H) || defined(FRIBIDI_H) - //FriBidiCharType baseDir = FRIBIDI_TYPE_LTR; + //FriBidiCharType baseDir = FRIBIDI_TYPE_LTR; #if defined (__MINGW32__) && (__GNUC__ <= 4 ) - FriBidiCharType baseDir = FRIBIDI_TYPE_WL; /* Per: Shai Ayal , 2009-01-14 */ + FriBidiCharType baseDir = FRIBIDI_TYPE_WL; /* Per: Shai Ayal , 2009-01-14 */ #else - FriBidiParType baseDir = FRIBIDI_TYPE_WL; //EP to avoid warning on types in now commented line above + FriBidiParType baseDir = FRIBIDI_TYPE_WL; //EP to avoid warning on types in now commented line above #endif - FriBidiChar *unicodeIn, *unicodeOut; - unsigned int i; + FriBidiChar *unicodeIn, *unicodeOut; + unsigned int i; - unicodeIn = (FriBidiChar *) malloc(sizeof(FriBidiChar) * (texttool_len + 1)); - unicodeOut = (FriBidiChar *) malloc(sizeof(FriBidiChar) * (texttool_len + 1)); + unicodeIn = + (FriBidiChar *) malloc(sizeof(FriBidiChar) * (texttool_len + 1)); + unicodeOut = + (FriBidiChar *) malloc(sizeof(FriBidiChar) * (texttool_len + 1)); - str = (wchar_t *) malloc(sizeof(wchar_t) * (texttool_len + 1)); + str = (wchar_t *) malloc(sizeof(wchar_t) * (texttool_len + 1)); - for (i = 0; i < texttool_len; i++) - unicodeIn[i] = (FriBidiChar) texttool_str[i]; + for (i = 0; i < texttool_len; i++) + unicodeIn[i] = (FriBidiChar) texttool_str[i]; - fribidi_log2vis(unicodeIn, texttool_len, &baseDir, unicodeOut, 0, 0, 0); + fribidi_log2vis(unicodeIn, texttool_len, &baseDir, unicodeOut, 0, 0, 0); - /* FIXME: If we determine that some new text was RtoL, we should - reposition the text */ + /* FIXME: If we determine that some new text was RtoL, we should + reposition the text */ - for (i = 0; i < texttool_len; i++) - str[i] = (long)unicodeOut[i]; + for (i = 0; i < texttool_len; i++) + str[i] = (long) unicodeOut[i]; - str[texttool_len] = L'\0'; + str[texttool_len] = L'\0'; - free(unicodeIn); - free(unicodeOut); + free(unicodeIn); + free(unicodeOut); #else - str = uppercase_w(texttool_str); + str = uppercase_w(texttool_str); #endif - tmp_surf = render_text_w(getfonthandle(cur_font), str, color); + tmp_surf = render_text_w(getfonthandle(cur_font), str, color); - w = tmp_surf->w; - h = tmp_surf->h; - r_tir.h = (float)tmp_surf->h / render_scale; - r_tir.w = (float)tmp_surf->w / render_scale; + w = tmp_surf->w; + h = tmp_surf->h; + r_tir.h = (float) tmp_surf->h / render_scale; + r_tir.w = (float) tmp_surf->w / render_scale; - cursor_textwidth = w; - } + cursor_textwidth = w; + } else /* Erase the stalle letter . */ + { + if (cur_label != LABEL_SELECT && cur_label != LABEL_APPLY) { - if (cur_label != LABEL_SELECT && cur_label != LABEL_APPLY) - { - update_canvas_ex_r(old_dest.x - r_ttools.w, old_dest.y, old_dest.x + old_dest.w, old_dest.y + old_dest.h, 0); - old_dest.x = old_dest.y = old_dest.w = old_dest.h = 0; + update_canvas_ex_r(old_dest.x - r_ttools.w, old_dest.y, + old_dest.x + old_dest.w, old_dest.y + old_dest.h, 0); + old_dest.x = old_dest.y = old_dest.w = old_dest.h = 0; - update_canvas_ex_r(old_cursor_x - 1, - old_cursor_y - 1, - old_cursor_x + 1, old_cursor_y + 1 + TuxPaint_Font_FontHeight(getfonthandle(cur_font)), 0); + update_canvas_ex_r(old_cursor_x - 1, + old_cursor_y - 1, + old_cursor_x + 1, + old_cursor_y + 1 + + TuxPaint_Font_FontHeight(getfonthandle(cur_font)), + 0); - /* FIXME: Do less flickery updating here (use update_canvas_ex() above, then SDL_Flip() or SDL_UpdateRect() here -bjk 2010.02.10 */ - - old_cursor_x = cursor_x; - old_cursor_y = cursor_y; - cursor_textwidth = 0; - } - /* FIXME: Is this SDL_Flip() still needed? Pere 2011.06.28 */ - SDL_Flip(screen); - return; + /* FIXME: Do less flickery updating here (use update_canvas_ex() above, then SDL_Flip() or SDL_UpdateRect() here -bjk 2010.02.10 */ + old_cursor_x = cursor_x; + old_cursor_y = cursor_y; + cursor_textwidth = 0; } + /* FIXME: Is this SDL_Flip() still needed? Pere 2011.06.28 */ + SDL_Flip(screen); + return; + + } if (!do_blit) + { + update_canvas_ex_r(old_dest.x - r_ttools.w, old_dest.y, + old_dest.x + old_dest.w, old_dest.y + old_dest.h, 0); + + /* update_canvas_ex_r(cursor_x - 1, */ + /* cursor_y - 1, */ + /* cursor_x + 1 + TuxPaint_Font_FontHeight(getfonthandle(cur_font)) * 3, */ + /* cursor_y + 1 + TuxPaint_Font_FontHeight(getfonthandle(cur_font)), 0); */ + + + /* Draw outline around text: */ + + dest.x = cursor_x - 2 + r_ttools.w; + dest.y = cursor_y - 2; + dest.w = w + 4; + dest.h = h + 4; + + if (dest.x + dest.w > WINDOW_WIDTH - r_ttoolopt.w) + dest.w = WINDOW_WIDTH - r_ttoolopt.w - dest.x; + if (dest.y + dest.h > (button_h * buttons_tall + r_ttools.h)) + dest.h = (button_h * buttons_tall + r_ttools.h) - dest.y; + + SDL_FillRect(screen, &dest, SDL_MapRGB(canvas->format, 0, 0, 0)); + + old_dest.x = dest.x; + old_dest.y = dest.y; + old_dest.w = dest.w; + old_dest.h = dest.h; + + /* FIXME: This would be nice if it were alpha-blended: */ + + dest.x = cursor_x + r_ttools.w; + dest.y = cursor_y; + dest.w = w; + dest.h = h; + + if (dest.x + dest.w > WINDOW_WIDTH - r_ttoolopt.w) + dest.w = WINDOW_WIDTH - r_ttoolopt.w - dest.x; + if (dest.y + dest.h > (button_h * buttons_tall + r_ttools.h)) + dest.h = (button_h * buttons_tall + r_ttools.h) - dest.y; + + if ((color_hexes[cur_color][0] + color_hexes[cur_color][1] + + color_hexes[cur_color][2]) >= 384) { - update_canvas_ex_r(old_dest.x - r_ttools.w, old_dest.y, old_dest.x + old_dest.w, old_dest.y + old_dest.h, 0); + /* Grey background if blit is white!... */ - /* update_canvas_ex_r(cursor_x - 1, */ - /* cursor_y - 1, */ - /* cursor_x + 1 + TuxPaint_Font_FontHeight(getfonthandle(cur_font)) * 3, */ - /* cursor_y + 1 + TuxPaint_Font_FontHeight(getfonthandle(cur_font)), 0); */ - - - /* Draw outline around text: */ - - dest.x = cursor_x - 2 + r_ttools.w; - dest.y = cursor_y - 2; - dest.w = w + 4; - dest.h = h + 4; - - if (dest.x + dest.w > WINDOW_WIDTH - r_ttoolopt.w) - dest.w = WINDOW_WIDTH - r_ttoolopt.w - dest.x; - if (dest.y + dest.h > (button_h * buttons_tall + r_ttools.h)) - dest.h = (button_h * buttons_tall + r_ttools.h) - dest.y; - - SDL_FillRect(screen, &dest, SDL_MapRGB(canvas->format, 0, 0, 0)); - - old_dest.x = dest.x; - old_dest.y = dest.y; - old_dest.w = dest.w; - old_dest.h = dest.h; - - /* FIXME: This would be nice if it were alpha-blended: */ - - dest.x = cursor_x + r_ttools.w; - dest.y = cursor_y; - dest.w = w; - dest.h = h; - - if (dest.x + dest.w > WINDOW_WIDTH - r_ttoolopt.w) - dest.w = WINDOW_WIDTH - r_ttoolopt.w - dest.x; - if (dest.y + dest.h > (button_h * buttons_tall + r_ttools.h)) - dest.h = (button_h * buttons_tall + r_ttools.h) - dest.y; - - if ((color_hexes[cur_color][0] + color_hexes[cur_color][1] + color_hexes[cur_color][2]) >= 384) - { - /* Grey background if blit is white!... */ - - SDL_FillRect(screen, &dest, SDL_MapRGB(canvas->format, 64, 64, 64)); - } - else - { - /* White background, normally... */ - - SDL_FillRect(screen, &dest, SDL_MapRGB(canvas->format, 255, 255, 255)); - } + SDL_FillRect(screen, &dest, SDL_MapRGB(canvas->format, 64, 64, 64)); } + else + { + /* White background, normally... */ + + SDL_FillRect(screen, &dest, SDL_MapRGB(canvas->format, 255, 255, 255)); + } + } /* Draw the text itself! */ if (tmp_surf != NULL) + { + dest.x = cursor_x; + dest.y = cursor_y; + + src.x = 0; + src.y = 0; + src.w = tmp_surf->w; + src.h = tmp_surf->h; + + if (dest.x + src.w > WINDOW_WIDTH - r_ttoolopt.w - r_ttools.w) + src.w = WINDOW_WIDTH - r_ttoolopt.w - r_ttools.w - dest.x; + if (dest.y + src.h > (button_h * buttons_tall + r_ttools.h)) + src.h = (button_h * buttons_tall + r_ttools.h) - dest.y; + + if (do_blit) { - dest.x = cursor_x; - dest.y = cursor_y; + if ((cur_tool == TOOL_LABEL && label_node_to_edit) || + ((old_tool == TOOL_LABEL && label_node_to_edit) && + (cur_tool == TOOL_PRINT || + cur_tool == TOOL_SAVE || cur_tool == TOOL_OPEN + || cur_tool == TOOL_NEW || cur_tool == TOOL_QUIT))) + { + have_to_rec_label_node = TRUE; + add_label_node(src.w, src.h, dest.x, dest.y, tmp_surf); + simply_render_node(current_label_node); + } + else if (cur_tool == TOOL_LABEL || + (old_tool == TOOL_LABEL && + (cur_tool == TOOL_PRINT || + cur_tool == TOOL_SAVE || cur_tool == TOOL_OPEN + || cur_tool == TOOL_NEW || cur_tool == TOOL_QUIT))) + { + myblit(tmp_surf, &src, label, &dest); - src.x = 0; - src.y = 0; - src.w = tmp_surf->w; - src.h = tmp_surf->h; + have_to_rec_label_node = TRUE; + add_label_node(src.w, src.h, dest.x, dest.y, tmp_surf); - if (dest.x + src.w > WINDOW_WIDTH - r_ttoolopt.w - r_ttools.w) - src.w = WINDOW_WIDTH - r_ttoolopt.w - r_ttools.w - dest.x; - if (dest.y + src.h > (button_h * buttons_tall + r_ttools.h)) - src.h = (button_h * buttons_tall + r_ttools.h) - dest.y; - - if (do_blit) - { - if ((cur_tool == TOOL_LABEL && label_node_to_edit) || - ((old_tool == TOOL_LABEL && label_node_to_edit) && - (cur_tool == TOOL_PRINT || - cur_tool == TOOL_SAVE || cur_tool == TOOL_OPEN || cur_tool == TOOL_NEW || cur_tool == TOOL_QUIT))) - { - have_to_rec_label_node = TRUE; - add_label_node(src.w, src.h, dest.x, dest.y, tmp_surf); - simply_render_node(current_label_node); - } - else if (cur_tool == TOOL_LABEL || - (old_tool == TOOL_LABEL && - (cur_tool == TOOL_PRINT || - cur_tool == TOOL_SAVE || cur_tool == TOOL_OPEN || cur_tool == TOOL_NEW || cur_tool == TOOL_QUIT))) - { - myblit(tmp_surf, &src, label, &dest); - - have_to_rec_label_node = TRUE; - add_label_node(src.w, src.h, dest.x, dest.y, tmp_surf); - - } - else - { - SDL_BlitSurface(tmp_surf, &src, canvas, &dest); - } - update_canvas_ex_r(dest.x - 2, dest.y - 2, dest.x + tmp_surf->w + 4, dest.y + tmp_surf->h + 4, 0); - } + } else - { - dest.x = dest.x + r_ttools.w; - SDL_BlitSurface(tmp_surf, &src, screen, &dest); - } + { + SDL_BlitSurface(tmp_surf, &src, canvas, &dest); + } + update_canvas_ex_r(dest.x - 2, dest.y - 2, dest.x + tmp_surf->w + 4, + dest.y + tmp_surf->h + 4, 0); } + else + { + dest.x = dest.x + r_ttools.w; + SDL_BlitSurface(tmp_surf, &src, screen, &dest); + } + } /* FIXME: Only update what's changed! */ @@ -19142,11 +20343,11 @@ static char *uppercase(const char *restrict const str) mbstowcs(dest, str, sizeof(wchar_t) * n); /* at most n wchar_t written */ i = 0; do - { - dest[i] = towupper(dest[i]); - } + { + dest[i] = towupper(dest[i]); + } while (dest[i++]); - wcstombs(ustr, dest, n * 2); /* at most n * 2 bytes written */ + wcstombs(ustr, dest, n * 2); /* at most n * 2 bytes written */ #ifdef DEBUG printf(" ORIGINAL: %s\n" "UPPERCASE: %s\n\n", str, ustr); @@ -19157,7 +20358,7 @@ static char *uppercase(const char *restrict const str) /** * FIXME */ -static wchar_t *uppercase_w(const wchar_t * restrict const str) +static wchar_t *uppercase_w(const wchar_t *restrict const str) { unsigned n = wcslen(str) + 1; wchar_t *ustr = malloc(sizeof(wchar_t) * n); @@ -19165,15 +20366,15 @@ static wchar_t *uppercase_w(const wchar_t * restrict const str) memcpy(ustr, str, sizeof(wchar_t) * n); if (only_uppercase) - { - unsigned i = 0; + { + unsigned i = 0; - do - { - ustr[i] = towupper(ustr[i]); - } - while (ustr[i++]); + do + { + ustr[i] = towupper(ustr[i]); } + while (ustr[i++]); + } return ustr; } @@ -19196,55 +20397,55 @@ static char *textdir(const char *const str) dstr = malloc(strlen(str) + 5); if (need_right_to_left_word) + { + dstr[strlen(str)] = '\0'; + + for (i = 0; i < strlen(str); i++) { - dstr[strlen(str)] = '\0'; + j = (strlen(str) - i - 1); - for (i = 0; i < strlen(str); i++) - { - j = (strlen(str) - i - 1); + c1 = (unsigned char) str[i + 0]; + c2 = (unsigned char) str[i + 1]; + c3 = (unsigned char) str[i + 2]; + c4 = (unsigned char) str[i + 3]; - c1 = (unsigned char)str[i + 0]; - c2 = (unsigned char)str[i + 1]; - c3 = (unsigned char)str[i + 2]; - c4 = (unsigned char)str[i + 3]; - - if (c1 < 128) /* 0xxx xxxx - 1 byte */ - { - dstr[j] = str[i]; - } - else if ((c1 & 0xE0) == 0xC0) /* 110x xxxx - 2 bytes */ - { - dstr[j - 1] = c1; - dstr[j - 0] = c2; - i = i + 1; - } - else if ((c1 & 0xF0) == 0xE0) /* 1110 xxxx - 3 bytes */ - { - dstr[j - 2] = c1; - dstr[j - 1] = c2; - dstr[j - 0] = c3; - i = i + 2; - } - else /* 1111 0xxx - 4 bytes */ - { - dstr[j - 3] = c1; - dstr[j - 2] = c2; - dstr[j - 1] = c3; - dstr[j - 0] = c4; - i = i + 3; - } - } + if (c1 < 128) /* 0xxx xxxx - 1 byte */ + { + dstr[j] = str[i]; + } + else if ((c1 & 0xE0) == 0xC0) /* 110x xxxx - 2 bytes */ + { + dstr[j - 1] = c1; + dstr[j - 0] = c2; + i = i + 1; + } + else if ((c1 & 0xF0) == 0xE0) /* 1110 xxxx - 3 bytes */ + { + dstr[j - 2] = c1; + dstr[j - 1] = c2; + dstr[j - 0] = c3; + i = i + 2; + } + else /* 1111 0xxx - 4 bytes */ + { + dstr[j - 3] = c1; + dstr[j - 2] = c2; + dstr[j - 1] = c3; + dstr[j - 0] = c4; + i = i + 3; + } } + } else - { - strcpy((char *)dstr, str); /* safe; malloc'd to a sufficient size */ - } + { + strcpy((char *) dstr, str); /* safe; malloc'd to a sufficient size */ + } #ifdef DEBUG printf("L2R_DIR: %s\n", dstr); #endif - return ((char *)dstr); + return ((char *) dstr); } @@ -19256,16 +20457,16 @@ static Uint32 scrolltimer_selector_callback(Uint32 interval, void *param) { /* printf("scrolltimer_selector_callback(%d) -- ", interval); */ if (scrolling_selector) - { - DEBUG_PRINTF("(Still scrolling selector)\n"); - SDL_PushEvent((SDL_Event *) param); - return interval; - } + { + DEBUG_PRINTF("(Still scrolling selector)\n"); + SDL_PushEvent((SDL_Event *) param); + return interval; + } else - { - DEBUG_PRINTF("(all done scrolling selector)\n"); - return 0; - } + { + DEBUG_PRINTF("(all done scrolling selector)\n"); + return 0; + } } /** @@ -19276,16 +20477,16 @@ static Uint32 scrolltimer_tool_callback(Uint32 interval, void *param) { /* printf("scrolltimer_tool_callback(%d)\n", interval); */ if (scrolling_tool) - { - DEBUG_PRINTF("(Still scrolling tool)\n"); - SDL_PushEvent((SDL_Event *) param); - return interval; - } + { + DEBUG_PRINTF("(Still scrolling tool)\n"); + SDL_PushEvent((SDL_Event *) param); + return interval; + } else - { - DEBUG_PRINTF("(all done scrolling tool)\n"); - return 0; - } + { + DEBUG_PRINTF("(all done scrolling tool)\n"); + return 0; + } } @@ -19296,16 +20497,16 @@ static Uint32 scrolltimer_tool_callback(Uint32 interval, void *param) static Uint32 scrolltimer_dialog_callback(Uint32 interval, void *param) { if (scrolling_dialog) - { - DEBUG_PRINTF("(Still scrolling dialog)\n"); - SDL_PushEvent((SDL_Event *) param); - return interval; - } + { + DEBUG_PRINTF("(Still scrolling dialog)\n"); + SDL_PushEvent((SDL_Event *) param); + return interval; + } else - { - DEBUG_PRINTF("(all done scrolling dialog)\n"); - return 0; - } + { + DEBUG_PRINTF("(all done scrolling dialog)\n"); + return 0; + } } @@ -19313,7 +20514,8 @@ static Uint32 scrolltimer_dialog_callback(Uint32 interval, void *param) * FIXME */ /* Controls the Text-Timer - interval == 0 removes the timer */ -static void control_drawtext_timer(Uint32 interval, const char *const text, Uint8 locale_text) +static void control_drawtext_timer(Uint32 interval, const char *const text, + Uint8 locale_text) { static int activated = 0; static SDL_TimerID TimerID = 0; @@ -19323,24 +20525,25 @@ static void control_drawtext_timer(Uint32 interval, const char *const text, Uint /* Remove old timer if any is running */ if (activated) - { - SDL_RemoveTimer(TimerID); - activated = 0; - TimerID = 0; - } + { + SDL_RemoveTimer(TimerID); + activated = 0; + TimerID = 0; + } if (interval == 0) return; drawtext_event.type = SDL_USEREVENT; drawtext_event.user.code = USEREVENT_TEXT_UPDATE; - drawtext_event.user.data1 = (void *)text; - drawtext_event.user.data2 = (void *)(intptr_t) ((int)locale_text); //EP added (intptr_t) to avoid warning on x64 + drawtext_event.user.data1 = (void *) text; + drawtext_event.user.data2 = (void *) (intptr_t) ((int) locale_text); //EP added (intptr_t) to avoid warning on x64 /* Add new timer */ - TimerID = SDL_AddTimer(interval, drawtext_callback, (void *)&drawtext_event); + TimerID = + SDL_AddTimer(interval, drawtext_callback, (void *) &drawtext_event); activated = 1; } @@ -19351,7 +20554,7 @@ static void control_drawtext_timer(Uint32 interval, const char *const text, Uint /* Drawtext Timer */ static Uint32 drawtext_callback(Uint32 interval, void *param) { - (void)interval; + (void) interval; SDL_PushEvent((SDL_Event *) param); return 0; /* Remove timer */ @@ -19365,11 +20568,11 @@ static Uint32 drawtext_callback(Uint32 interval, void *param) static char *debug_gettext(const char *str) { if (strcmp(str, dgettext(NULL, str)) == 0) - { - printf("NOTRANS: %s\n", str); - printf("..TRANS: %s\n", dgettext(NULL, str)); - fflush(stdout); - } + { + printf("NOTRANS: %s\n", str); + printf("..TRANS: %s\n", dgettext(NULL, str)); + fflush(stdout); + } return (dgettext(NULL, str)); } @@ -19422,294 +20625,312 @@ static void draw_image_title(int t, SDL_Rect dest) /* Handle keyboard events to control the mouse: */ /* Move as many pixels as bigsteps outside the areas, in the areas and 5 pixels around, move 1 pixel at a time */ -static void handle_keymouse(SDLKey key, Uint32 updown, int steps, SDL_Rect * area1, SDL_Rect * area2) +static void handle_keymouse(SDLKey key, Uint32 updown, int steps, + SDL_Rect * area1, SDL_Rect * area2) { int left, right, up, bottom; SDL_Event event; SDL_Rect r1, r2; if (keymouse) + { + /* make the compiler happy */ + r1.x = r1.y = r1.w = r1.h = 0; + r2.x = r2.y = r2.w = r2.h = 0; + + if (area1) { - /* make the compiler happy */ - r1.x = r1.y = r1.w = r1.h = 0; - r2.x = r2.y = r2.w = r2.h = 0; + r1.x = max(0, area1->x - 5); + r1.y = max(0, area1->y - 5); + r1.w = area1->x - r1.x + area1->w + 5; + r1.h = area1->y - r1.y + area1->h + 5; + } - if (area1) + if (area2) + { + r2.x = max(0, area2->x - 5); + r2.y = max(0, area2->y - 5); + r2.w = area2->x - r2.x + area2->w + 5; + r2.h = area2->y - r2.y + area2->h + 5; + } + + /* The defaults */ + left = max(0, oldpos_x - steps); + right = min(screen->w, oldpos_x + steps); + up = max(0, oldpos_y - steps); + bottom = min(screen->h, oldpos_y + steps); + + /* If Shift is pressed, go with the defaults */ + if (!(SDL_GetModState() & KMOD_SHIFT)) + { + /* 1 pixel if in one of the areas */ + if ((area1 && oldpos_x > r1.x && oldpos_x - r1.x < r1.w + && oldpos_y > r1.y && oldpos_y - r1.y < r1.h) || (area2 + && oldpos_x > + r2.x + && oldpos_x - + r2.x < r2.w + && oldpos_y > + r2.y + && oldpos_y - + r2.y < r2.h)) + { + left = max(0, oldpos_x - 1); + right = min(screen->w, oldpos_x + 1); + up = max(0, oldpos_y - 1); + bottom = min(screen->h, oldpos_y + 1); + } + + /* Not enter too deep in the areas at once */ + else if (area1 || area2) + { + if (area1) + if (oldpos_y - r1.y < r1.h && oldpos_y > r1.y) + { + if (oldpos_x - r1.x < steps) + right = min(oldpos_x + steps, r1.x + 1); + else if (oldpos_x - r1.x - r1.w < steps) + left = max(r1.x + r1.w - 1, oldpos_x - steps); + } + + if (oldpos_x - r1.x < r1.w && oldpos_x > r1.x) { - r1.x = max(0, area1->x - 5); - r1.y = max(0, area1->y - 5); - r1.w = area1->x - r1.x + area1->w + 5; - r1.h = area1->y - r1.y + area1->h + 5; + if (oldpos_y - r1.y < steps) + bottom = min(r1.y + 1, oldpos_y + steps); + else if (oldpos_y - r1.y - r1.h < steps) + up = max(r1.y + r1.h - 1, oldpos_y - steps); } - if (area2) + if (area2) + if (oldpos_y - r2.y < r2.h && oldpos_y > r2.y) + { + if (oldpos_x - r2.x < steps) + right = min(oldpos_x + steps, r2.x + 1); + else if (oldpos_x - r2.x - r2.w < steps) + left = max(r2.x + r2.w - 1, oldpos_x - steps); + } + + if (oldpos_x - r2.x < r2.w && oldpos_x > r2.x) { - r2.x = max(0, area2->x - 5); - r2.y = max(0, area2->y - 5); - r2.w = area2->x - r2.x + area2->w + 5; - r2.h = area2->y - r2.y + area2->h + 5; + if (oldpos_y - r2.y < steps) + bottom = min(r2.y + 1, oldpos_y + steps); + else if (oldpos_y - r2.y - r2.h < steps) + up = max(r2.y + r2.h - 1, oldpos_y - steps); + } + } + } + + if (updown == SDL_KEYUP) + { + if (key == SDLK_INSERT || key == SDLK_F5 || + ((cur_tool != TOOL_TEXT && cur_tool != TOOL_LABEL) && + (key == SDLK_SPACE || key == SDLK_5 || key == SDLK_KP_5))) + { + event.type = SDL_MOUSEBUTTONUP; + event.button.x = oldpos_x; + event.button.y = oldpos_y; + event.button.button = 1; + SDL_PushEvent(&event); + } + } + + else + { + if (key == SDLK_LEFT) + SDL_WarpMouse(left, oldpos_y); + + else if (key == SDLK_RIGHT) + SDL_WarpMouse(right, oldpos_y); + + else if (key == SDLK_UP) + SDL_WarpMouse(oldpos_x, up); + + else if (key == SDLK_DOWN) + SDL_WarpMouse(oldpos_x, bottom); + + else if ((key == SDLK_INSERT || key == SDLK_F5) && !button_down) + { + event.type = SDL_MOUSEBUTTONDOWN; + event.button.x = oldpos_x; + event.button.y = oldpos_y; + event.button.button = 1; + SDL_PushEvent(&event); + } + + else if (cur_tool != TOOL_TEXT && cur_tool != TOOL_LABEL) + { + if (!button_down + && (key == SDLK_SPACE || key == SDLK_5 || key == SDLK_KP_5)) + { + event.type = SDL_MOUSEBUTTONDOWN; + event.button.x = oldpos_x; + event.button.y = oldpos_y; + event.button.button = 1; + SDL_PushEvent(&event); } - /* The defaults */ - left = max(0, oldpos_x - steps); - right = min(screen->w, oldpos_x + steps); - up = max(0, oldpos_y - steps); - bottom = min(screen->h, oldpos_y + steps); + else if (key == SDLK_1 || key == SDLK_KP_1) + SDL_WarpMouse(left, bottom); - /* If Shift is pressed, go with the defaults */ - if (!(SDL_GetModState() & KMOD_SHIFT)) - { - /* 1 pixel if in one of the areas */ - if ((area1 && oldpos_x > r1.x && oldpos_x - r1.x < r1.w && oldpos_y > r1.y && oldpos_y - r1.y < r1.h) || - (area2 && oldpos_x > r2.x && oldpos_x - r2.x < r2.w && oldpos_y > r2.y && oldpos_y - r2.y < r2.h)) - { - left = max(0, oldpos_x - 1); - right = min(screen->w, oldpos_x + 1); - up = max(0, oldpos_y - 1); - bottom = min(screen->h, oldpos_y + 1); - } + else if (key == SDLK_3 || key == SDLK_KP_3) + SDL_WarpMouse(right, bottom); - /* Not enter too deep in the areas at once */ - else if (area1 || area2) - { - if (area1) - if (oldpos_y - r1.y < r1.h && oldpos_y > r1.y) - { - if (oldpos_x - r1.x < steps) - right = min(oldpos_x + steps, r1.x + 1); - else if (oldpos_x - r1.x - r1.w < steps) - left = max(r1.x + r1.w - 1, oldpos_x - steps); - } + else if (key == SDLK_7 || key == SDLK_KP_7) + SDL_WarpMouse(left, up); - if (oldpos_x - r1.x < r1.w && oldpos_x > r1.x) - { - if (oldpos_y - r1.y < steps) - bottom = min(r1.y + 1, oldpos_y + steps); - else if (oldpos_y - r1.y - r1.h < steps) - up = max(r1.y + r1.h - 1, oldpos_y - steps); - } + else if (key == SDLK_9 || key == SDLK_KP_9) + SDL_WarpMouse(right, up); - if (area2) - if (oldpos_y - r2.y < r2.h && oldpos_y > r2.y) - { - if (oldpos_x - r2.x < steps) - right = min(oldpos_x + steps, r2.x + 1); - else if (oldpos_x - r2.x - r2.w < steps) - left = max(r2.x + r2.w - 1, oldpos_x - steps); - } + else if (key == SDLK_2 || key == SDLK_KP_2) + SDL_WarpMouse(oldpos_x, bottom); - if (oldpos_x - r2.x < r2.w && oldpos_x > r2.x) - { - if (oldpos_y - r2.y < steps) - bottom = min(r2.y + 1, oldpos_y + steps); - else if (oldpos_y - r2.y - r2.h < steps) - up = max(r2.y + r2.h - 1, oldpos_y - steps); - } - } - } + else if (key == SDLK_8 || key == SDLK_KP_8) + SDL_WarpMouse(oldpos_x, up); - if (updown == SDL_KEYUP) - { - if (key == SDLK_INSERT || key == SDLK_F5 || - ((cur_tool != TOOL_TEXT && cur_tool != TOOL_LABEL) && - (key == SDLK_SPACE || key == SDLK_5 || key == SDLK_KP_5))) - { - event.type = SDL_MOUSEBUTTONUP; - event.button.x = oldpos_x; - event.button.y = oldpos_y; - event.button.button = 1; - SDL_PushEvent(&event); - } - } + else if (key == SDLK_6 || key == SDLK_KP_6) + SDL_WarpMouse(right, oldpos_y); - else - { - if (key == SDLK_LEFT) - SDL_WarpMouse(left, oldpos_y); + else if (key == SDLK_4 || key == SDLK_KP_4) + SDL_WarpMouse(left, oldpos_y); - else if (key == SDLK_RIGHT) - SDL_WarpMouse(right, oldpos_y); - - else if (key == SDLK_UP) - SDL_WarpMouse(oldpos_x, up); - - else if (key == SDLK_DOWN) - SDL_WarpMouse(oldpos_x, bottom); - - else if ((key == SDLK_INSERT || key == SDLK_F5) && !button_down) - { - event.type = SDL_MOUSEBUTTONDOWN; - event.button.x = oldpos_x; - event.button.y = oldpos_y; - event.button.button = 1; - SDL_PushEvent(&event); - } - - else if (cur_tool != TOOL_TEXT && cur_tool != TOOL_LABEL) - { - if (!button_down && (key == SDLK_SPACE || key == SDLK_5 || key == SDLK_KP_5)) - { - event.type = SDL_MOUSEBUTTONDOWN; - event.button.x = oldpos_x; - event.button.y = oldpos_y; - event.button.button = 1; - SDL_PushEvent(&event); - } - - else if (key == SDLK_1 || key == SDLK_KP_1) - SDL_WarpMouse(left, bottom); - - else if (key == SDLK_3 || key == SDLK_KP_3) - SDL_WarpMouse(right, bottom); - - else if (key == SDLK_7 || key == SDLK_KP_7) - SDL_WarpMouse(left, up); - - else if (key == SDLK_9 || key == SDLK_KP_9) - SDL_WarpMouse(right, up); - - else if (key == SDLK_2 || key == SDLK_KP_2) - SDL_WarpMouse(oldpos_x, bottom); - - else if (key == SDLK_8 || key == SDLK_KP_8) - SDL_WarpMouse(oldpos_x, up); - - else if (key == SDLK_6 || key == SDLK_KP_6) - SDL_WarpMouse(right, oldpos_y); - - else if (key == SDLK_4 || key == SDLK_KP_4) - SDL_WarpMouse(left, oldpos_y); - - /* FIXME: This is qwerty centric and interferes with gettexted reponses for yes/no, - so disabling until either is removed or is configurable. */ + /* FIXME: This is qwerty centric and interferes with gettexted reponses for yes/no, + so disabling until either is removed or is configurable. */ #if 0 - else if (key == SDLK_s) - SDL_WarpMouse(oldpos_x, bottom); + else if (key == SDLK_s) + SDL_WarpMouse(oldpos_x, bottom); - else if (key == SDLK_w) - SDL_WarpMouse(oldpos_x, up); + else if (key == SDLK_w) + SDL_WarpMouse(oldpos_x, up); - else if (key == SDLK_d) - SDL_WarpMouse(right, oldpos_y); + else if (key == SDLK_d) + SDL_WarpMouse(right, oldpos_y); - else if (key == SDLK_a) - SDL_WarpMouse(left, oldpos_y); + else if (key == SDLK_a) + SDL_WarpMouse(left, oldpos_y); #endif - } - } + } } + } } /** * FIXME */ /* A subset of keys that will move one button at a time and jump between r_canvas<->r_tools<->r_colors */ -static void handle_keymouse_buttons(SDLKey key, int *whicht, int *whichc, SDL_Rect real_r_tools) +static void handle_keymouse_buttons(SDLKey key, int *whicht, int *whichc, + SDL_Rect real_r_tools) { if (hit_test(&real_r_tools, oldpos_x, oldpos_y) && - (key == SDLK_F7 || key == SDLK_F8 || key == SDLK_F11 || key == SDLK_F12)) + (key == SDLK_F7 || key == SDLK_F8 || key == SDLK_F11 + || key == SDLK_F12)) + { + *whicht = + tool_scroll + grid_hit_gd(&real_r_tools, oldpos_x, oldpos_y, &gd_tools); + + if (key == SDLK_F7 && hit_test(&real_r_tools, oldpos_x, oldpos_y)) { - *whicht = tool_scroll + grid_hit_gd(&real_r_tools, oldpos_x, oldpos_y, &gd_tools); - - if (key == SDLK_F7 && hit_test(&real_r_tools, oldpos_x, oldpos_y)) - { - *whicht += 2; - *whicht = *whicht % NUM_TOOLS; - while (!tool_avail[*whicht]) - { - *whicht += 2; - *whicht = *whicht % NUM_TOOLS; - } - } - - else if (key == SDLK_F8 && hit_test(&real_r_tools, oldpos_x, oldpos_y)) - { - *whicht -= 2; - if (*whicht < 0) - *whicht += NUM_TOOLS; - while (!tool_avail[*whicht]) - { - *whicht -= 2; - if (*whicht < 0) - *whicht += NUM_TOOLS; - } - } - - else if (key == SDLK_F12 && hit_test(&real_r_tools, oldpos_x, oldpos_y)) - { - *whicht = *whicht + 1; - *whicht = *whicht % NUM_TOOLS; - while (!tool_avail[*whicht]) - { - *whicht += 1; - *whicht = *whicht % NUM_TOOLS; - } - } - - else if (key == SDLK_F11 && hit_test(&real_r_tools, oldpos_x, oldpos_y)) - { - *whicht = tool_scroll + grid_hit_gd(&real_r_tools, oldpos_x, oldpos_y, &gd_tools); - *whicht = *whicht - 1; - if (*whicht < 0) - *whicht += NUM_TOOLS; - while (!tool_avail[*whicht]) - { - *whicht -= 1; - if (*whicht < 0) - *whicht += NUM_TOOLS; - } - } - - while (*whicht >= tool_scroll + 2 * real_r_tools.h / button_h) - { - tool_scroll += 2; - draw_toolbar(); - update_screen_rect(&r_tools); - } - while (*whicht < tool_scroll) - { - tool_scroll -= 2; - draw_toolbar(); - update_screen_rect(&r_tools); - } - - SDL_WarpMouse(button_w / 2 + *whicht % 2 * button_w, - real_r_tools.y - *whicht % 2 * button_w / 2 + *whicht * button_h / 2 + 10 - - tool_scroll * button_h / 2); + *whicht += 2; + *whicht = *whicht % NUM_TOOLS; + while (!tool_avail[*whicht]) + { + *whicht += 2; + *whicht = *whicht % NUM_TOOLS; + } } + else if (key == SDLK_F8 && hit_test(&real_r_tools, oldpos_x, oldpos_y)) + { + *whicht -= 2; + if (*whicht < 0) + *whicht += NUM_TOOLS; + while (!tool_avail[*whicht]) + { + *whicht -= 2; + if (*whicht < 0) + *whicht += NUM_TOOLS; + } + } + + else if (key == SDLK_F12 && hit_test(&real_r_tools, oldpos_x, oldpos_y)) + { + *whicht = *whicht + 1; + *whicht = *whicht % NUM_TOOLS; + while (!tool_avail[*whicht]) + { + *whicht += 1; + *whicht = *whicht % NUM_TOOLS; + } + } + + else if (key == SDLK_F11 && hit_test(&real_r_tools, oldpos_x, oldpos_y)) + { + *whicht = + tool_scroll + grid_hit_gd(&real_r_tools, oldpos_x, oldpos_y, + &gd_tools); + *whicht = *whicht - 1; + if (*whicht < 0) + *whicht += NUM_TOOLS; + while (!tool_avail[*whicht]) + { + *whicht -= 1; + if (*whicht < 0) + *whicht += NUM_TOOLS; + } + } + + while (*whicht >= tool_scroll + 2 * real_r_tools.h / button_h) + { + tool_scroll += 2; + draw_toolbar(); + update_screen_rect(&r_tools); + } + while (*whicht < tool_scroll) + { + tool_scroll -= 2; + draw_toolbar(); + update_screen_rect(&r_tools); + } + + SDL_WarpMouse(button_w / 2 + *whicht % 2 * button_w, + real_r_tools.y - *whicht % 2 * button_w / 2 + + *whicht * button_h / 2 + 10 - tool_scroll * button_h / 2); + } + else if (key == SDLK_F11 && hit_test(&r_colors, oldpos_x, oldpos_y)) - { - *whichc = grid_hit_gd(&r_colors, oldpos_x, oldpos_y, &gd_colors); - *whichc = *whichc - 1; - if (*whichc < 0) - *whichc += NUM_COLORS; - SDL_WarpMouse(button_w * 2 + *whichc * color_button_w + 12, r_canvas.h + (r_colors.h / 2)); - } + { + *whichc = grid_hit_gd(&r_colors, oldpos_x, oldpos_y, &gd_colors); + *whichc = *whichc - 1; + if (*whichc < 0) + *whichc += NUM_COLORS; + SDL_WarpMouse(button_w * 2 + *whichc * color_button_w + 12, + r_canvas.h + (r_colors.h / 2)); + } else if (key == SDLK_F12 && hit_test(&r_colors, oldpos_x, oldpos_y)) - { - *whichc = grid_hit_gd(&r_colors, oldpos_x, oldpos_y, &gd_colors); - *whichc = *whichc + 1; - *whichc = *whichc % NUM_COLORS; - SDL_WarpMouse(button_w * 2 + *whichc * color_button_w + 12, r_canvas.h + (r_colors.h / 2)); - } + { + *whichc = grid_hit_gd(&r_colors, oldpos_x, oldpos_y, &gd_colors); + *whichc = *whichc + 1; + *whichc = *whichc % NUM_COLORS; + SDL_WarpMouse(button_w * 2 + *whichc * color_button_w + 12, + r_canvas.h + (r_colors.h / 2)); + } else if (key == SDLK_F4) - { - if (hit_test(&r_tools, oldpos_x, oldpos_y)) - SDL_WarpMouse(button_w * 2 + *whichc * color_button_w + 12, r_canvas.h + (r_colors.h / 2)); - else if (hit_test(&r_colors, oldpos_x, oldpos_y)) - SDL_WarpMouse(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2); - else - SDL_WarpMouse(button_w / 2 + *whicht % 2 * button_w, - real_r_tools.y - *whicht % 2 * button_w / 2 + *whicht * button_h / 2 + 10 - - tool_scroll * button_h / 2); + { + if (hit_test(&r_tools, oldpos_x, oldpos_y)) + SDL_WarpMouse(button_w * 2 + *whichc * color_button_w + 12, + r_canvas.h + (r_colors.h / 2)); + else if (hit_test(&r_colors, oldpos_x, oldpos_y)) + SDL_WarpMouse(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2); + else + SDL_WarpMouse(button_w / 2 + *whicht % 2 * button_w, + real_r_tools.y - *whicht % 2 * button_w / 2 + + *whicht * button_h / 2 + 10 - tool_scroll * button_h / 2); - /* Play a sound here as there is a big jump */ - playsound(screen, 1, SND_CLICK, 0, SNDPOS_LEFT, SNDDIST_NEAR); - } + /* Play a sound here as there is a big jump */ + playsound(screen, 1, SND_CLICK, 0, SNDPOS_LEFT, SNDDIST_NEAR); + } } /** @@ -19718,26 +20939,27 @@ static void handle_keymouse_buttons(SDLKey key, int *whicht, int *whichc, SDL_Re /* Unblank screen in fullscreen mode, if needed: */ static void handle_active(SDL_Event * event) { - if (event->window.event == SDL_WINDOWEVENT_EXPOSED || SDL_WINDOWEVENT_RESTORED) - { - // if (fullscreen) - SDL_Flip(screen); - } + if (event->window.event == SDL_WINDOWEVENT_EXPOSED + || SDL_WINDOWEVENT_RESTORED) + { + // if (fullscreen) + SDL_Flip(screen); + } if (event->window.event == SDL_WINDOWEVENT_FOCUS_GAINED) + { + if (mouseaccessibility) { - if (mouseaccessibility) - { - magic_switchin(canvas); - } - else if (mouseaccessibility && emulate_button_pressed) - { - magic_switchout(canvas); - } + magic_switchin(canvas); + } + else if (mouseaccessibility && emulate_button_pressed) + { + magic_switchout(canvas); + } #ifdef _WIN32 - SetActivationState(1); + SetActivationState(1); #endif - } + } } @@ -19747,7 +20969,8 @@ static void handle_active(SDL_Event * event) /* For right-to-left languages, when word-wrapping, we need to make sure the text doesn't end up going from bottom-to-top, too! */ #ifdef NO_SDLPANGO -static void anti_carriage_return(int left, int right, int cur_top, int new_top, int cur_bot, int line_width) +static void anti_carriage_return(int left, int right, int cur_top, + int new_top, int cur_bot, int line_width) { SDL_Rect src, dest; @@ -19817,57 +21040,57 @@ static void mirror_starter(void) img_starter = duplicate_surface(orig); if (img_starter != NULL) + { + for (x = 0; x < orig->w; x++) { - for (x = 0; x < orig->w; x++) - { - src.x = x; - src.y = 0; - src.w = 1; - src.h = orig->h; + src.x = x; + src.y = 0; + src.w = 1; + src.h = orig->h; - dest.x = orig->w - x - 1; - dest.y = 0; + dest.x = orig->w - x - 1; + dest.y = 0; - SDL_BlitSurface(orig, &src, img_starter, &dest); - } - - SDL_FreeSurface(orig); + SDL_BlitSurface(orig, &src, img_starter, &dest); } + + SDL_FreeSurface(orig); + } else - { - img_starter = orig; - } + { + img_starter = orig; + } /* Mirror background: */ if (img_starter_bkgd != NULL) + { + orig = img_starter_bkgd; + img_starter_bkgd = duplicate_surface(orig); + + if (img_starter_bkgd != NULL) { - orig = img_starter_bkgd; - img_starter_bkgd = duplicate_surface(orig); + for (x = 0; x < orig->w; x++) + { + src.x = x; + src.y = 0; + src.w = 1; + src.h = orig->h; - if (img_starter_bkgd != NULL) - { - for (x = 0; x < orig->w; x++) - { - src.x = x; - src.y = 0; - src.w = 1; - src.h = orig->h; + dest.x = orig->w - x - 1; + dest.y = 0; - dest.x = orig->w - x - 1; - dest.y = 0; + SDL_BlitSurface(orig, &src, img_starter_bkgd, &dest); + } - SDL_BlitSurface(orig, &src, img_starter_bkgd, &dest); - } - - SDL_FreeSurface(orig); - } - else - { - img_starter_bkgd = orig; - } + SDL_FreeSurface(orig); } + else + { + img_starter_bkgd = orig; + } + } } @@ -19887,57 +21110,57 @@ static void flip_starter(void) img_starter = duplicate_surface(orig); if (img_starter != NULL) + { + for (y = 0; y < orig->h; y++) { - for (y = 0; y < orig->h; y++) - { - src.x = 0; - src.y = y; - src.w = orig->w; - src.h = 1; + src.x = 0; + src.y = y; + src.w = orig->w; + src.h = 1; - dest.x = 0; - dest.y = orig->h - y - 1; + dest.x = 0; + dest.y = orig->h - y - 1; - SDL_BlitSurface(orig, &src, img_starter, &dest); - } - - SDL_FreeSurface(orig); + SDL_BlitSurface(orig, &src, img_starter, &dest); } + + SDL_FreeSurface(orig); + } else - { - img_starter = orig; - } + { + img_starter = orig; + } /* Flip background: */ if (img_starter_bkgd != NULL) + { + orig = img_starter_bkgd; + img_starter_bkgd = duplicate_surface(orig); + + if (img_starter_bkgd != NULL) { - orig = img_starter_bkgd; - img_starter_bkgd = duplicate_surface(orig); + for (y = 0; y < orig->h; y++) + { + src.x = 0; + src.y = y; + src.w = orig->w; + src.h = 1; - if (img_starter_bkgd != NULL) - { - for (y = 0; y < orig->h; y++) - { - src.x = 0; - src.y = y; - src.w = orig->w; - src.h = 1; + dest.x = 0; + dest.y = orig->h - y - 1; - dest.x = 0; - dest.y = orig->h - y - 1; + SDL_BlitSurface(orig, &src, img_starter_bkgd, &dest); + } - SDL_BlitSurface(orig, &src, img_starter_bkgd, &dest); - } - - SDL_FreeSurface(orig); - } - else - { - img_starter_bkgd = orig; - } + SDL_FreeSurface(orig); } + else + { + img_starter_bkgd = orig; + } + } } @@ -20008,22 +21231,22 @@ static SDL_Surface *load_svg(const char *file) /* Create the SVG cairo stuff: */ if (svg_cairo_create(&scr) != SVG_CAIRO_STATUS_SUCCESS) - { + { #ifdef DEBUG - printf("svg_cairo_create() failed\n"); + printf("svg_cairo_create() failed\n"); #endif - return (NULL); - } + return (NULL); + } /* Parse the SVG file: */ if (svg_cairo_parse(scr, file) != SVG_CAIRO_STATUS_SUCCESS) - { - svg_cairo_destroy(scr); + { + svg_cairo_destroy(scr); #ifdef DEBUG - printf("svg_cairo_parse(%s) failed\n", file); + printf("svg_cairo_parse(%s) failed\n", file); #endif - return (NULL); - } + return (NULL); + } /* Get the natural size of the SVG */ svg_cairo_get_size(scr, &rwidth, &rheight); @@ -20032,13 +21255,13 @@ static SDL_Surface *load_svg(const char *file) #endif if (rwidth == 0 || rheight == 0) - { - svg_cairo_destroy(scr); + { + svg_cairo_destroy(scr); #ifdef DEBUG - printf("SVG %s had 0 width or height!\n", file); + printf("SVG %s had 0 width or height!\n", file); #endif - return (NULL); - } + return (NULL); + } /* We will create a CAIRO_FORMAT_ARGB32 surface. We don't need to match the screen SDL format, but we are interested in the alpha bit... */ @@ -20051,8 +21274,8 @@ static SDL_Surface *load_svg(const char *file) scale = pick_best_scape(rwidth, rheight, r_canvas.w, r_canvas.h); - width = ((float)rwidth * scale); - height = ((float)rheight * scale); + width = ((float) rwidth * scale); + height = ((float) rheight * scale); #ifdef DEBUG printf("scaling to %d x %d (%f scale)\n", width, height, scale); @@ -20065,22 +21288,25 @@ static SDL_Surface *load_svg(const char *file) image = calloc(stride * height, 1); #ifdef DEBUG - printf("calling cairo_image_surface_create_for_data(..., CAIRO_FORMAT_ARGB32, %d(w), %d(h), %d(stride))\n", width, - height, stride); + printf + ("calling cairo_image_surface_create_for_data(..., CAIRO_FORMAT_ARGB32, %d(w), %d(h), %d(stride))\n", + width, height, stride); #endif /* Create the cairo surface with the adjusted width and height */ - cairo_surface = cairo_image_surface_create_for_data(image, CAIRO_FORMAT_ARGB32, width, height, stride); + cairo_surface = + cairo_image_surface_create_for_data(image, CAIRO_FORMAT_ARGB32, width, + height, stride); cr = cairo_create(cairo_surface); if (cr == NULL) - { - svg_cairo_destroy(scr); + { + svg_cairo_destroy(scr); #ifdef DEBUG - printf("cairo_create() failed\n"); + printf("cairo_create() failed\n"); #endif - return (NULL); - } + return (NULL); + } /* Scale it (proportionally) */ cairo_scale(cr, scale, scale); /* no return value :( */ @@ -20094,12 +21320,12 @@ static SDL_Surface *load_svg(const char *file) svg_cairo_destroy(scr); if (res != SVG_CAIRO_STATUS_SUCCESS) - { + { #ifdef DEBUG - printf("svg_cairo_render() failed\n"); + printf("svg_cairo_render() failed\n"); #endif - return (NULL); - } + return (NULL); + } /* Adjust the SDL surface to match the cairo surface created @@ -20110,15 +21336,17 @@ static SDL_Surface *load_svg(const char *file) amask = 0xff000000; /* Create the SDL surface using the pixel data stored: */ - sdl_surface_tmp = SDL_CreateRGBSurfaceFrom((void *)image, width, height, bpp, stride, rmask, gmask, bmask, amask); + sdl_surface_tmp = + SDL_CreateRGBSurfaceFrom((void *) image, width, height, bpp, stride, + rmask, gmask, bmask, amask); if (sdl_surface_tmp == NULL) - { + { #ifdef DEBUG - printf("SDL_CreateRGBSurfaceFrom() failed\n"); + printf("SDL_CreateRGBSurfaceFrom() failed\n"); #endif - return (NULL); - } + return (NULL); + } /* Convert the SDL surface to the display format, for faster blitting: */ @@ -20126,15 +21354,16 @@ static SDL_Surface *load_svg(const char *file) SDL_FreeSurface(sdl_surface_tmp); if (sdl_surface == NULL) - { + { #ifdef DEBUG - printf("SDL_DisplayFormatAlpha() failed\n"); + printf("SDL_DisplayFormatAlpha() failed\n"); #endif - return (NULL); - } + return (NULL); + } #ifdef DEBUG - printf("SDL surface from %d x %d SVG is %d x %d\n", rwidth, rheight, sdl_surface->w, sdl_surface->h); + printf("SDL surface from %d x %d SVG is %d x %d\n", rwidth, rheight, + sdl_surface->w, sdl_surface->h); #endif return (sdl_surface); @@ -20171,12 +21400,12 @@ static SDL_Surface *_load_svg(const char *file) rsvg_handle = rsvg_handle_new_from_file(file, &gerr); if (rsvg_handle == NULL) - { + { #ifdef DEBUG - fprintf(stderr, "rsvg_handle_new_from_file() failed\n"); + fprintf(stderr, "rsvg_handle_new_from_file() failed\n"); #endif - return (NULL); - } + return (NULL); + } rsvg_handle_get_dimensions(rsvg_handle, &dimensions); rwidth = dimensions.width; @@ -20195,8 +21424,8 @@ static SDL_Surface *_load_svg(const char *file) printf("best scale is %.4f\n", scale); #endif - width = ((float)rwidth * scale); - height = ((float)rheight * scale); + width = ((float) rwidth * scale); + height = ((float) rheight * scale); #ifdef DEBUG printf("scaling to %d x %d (%f scale)\n", width, height, scale); @@ -20208,43 +21437,45 @@ static SDL_Surface *_load_svg(const char *file) /* Allocate space for an image: */ image = calloc(stride * height, 1); if (image == NULL) - { + { #ifdef DEBUG - fprintf(stderr, "Unable to allocate image buffer\n"); + fprintf(stderr, "Unable to allocate image buffer\n"); #endif - rsvg_handle_close(rsvg_handle, &gerr); - return (NULL); - } + rsvg_handle_close(rsvg_handle, &gerr); + return (NULL); + } /* Create a surface for Cairo to draw into: */ - cairo_surf = cairo_image_surface_create_for_data(image, CAIRO_FORMAT_ARGB32, width, height, stride); + cairo_surf = + cairo_image_surface_create_for_data(image, CAIRO_FORMAT_ARGB32, width, + height, stride); if (cairo_surface_status(cairo_surf) != CAIRO_STATUS_SUCCESS) - { + { #ifdef DEBUG - fprintf(stderr, "cairo_image_surface_create() failed\n"); + fprintf(stderr, "cairo_image_surface_create() failed\n"); #endif - rsvg_handle_close(rsvg_handle, &gerr); - free(image); - return (NULL); - } + rsvg_handle_close(rsvg_handle, &gerr); + free(image); + return (NULL); + } /* Create a new Cairo object: */ cr = cairo_create(cairo_surf); if (cairo_status(cr) != CAIRO_STATUS_SUCCESS) - { + { #ifdef DEBUG - fprintf(stderr, "cairo_create() failed\n"); + fprintf(stderr, "cairo_create() failed\n"); #endif - rsvg_handle_close(rsvg_handle, &gerr); - cairo_surface_destroy(cairo_surf); - free(image); - return (NULL); - } + rsvg_handle_close(rsvg_handle, &gerr); + cairo_surface_destroy(cairo_surf); + free(image); + return (NULL); + } /* Ask RSVG to render the SVG into the Cairo object: */ @@ -20267,39 +21498,42 @@ static SDL_Surface *_load_svg(const char *file) amask = 0xff000000; /* Create the SDL surface using the pixel data stored: */ - sdl_surface_tmp = SDL_CreateRGBSurfaceFrom((void *)image, width, height, bpp, stride, rmask, gmask, bmask, amask); + sdl_surface_tmp = + SDL_CreateRGBSurfaceFrom((void *) image, width, height, bpp, stride, + rmask, gmask, bmask, amask); if (sdl_surface_tmp == NULL) - { + { #ifdef DEBUG - fprintf(stderr, "SDL_CreateRGBSurfaceFrom() failed\n"); + fprintf(stderr, "SDL_CreateRGBSurfaceFrom() failed\n"); #endif - rsvg_handle_close(rsvg_handle, &gerr); - cairo_surface_destroy(cairo_surf); - free(image); - cairo_destroy(cr); - return (NULL); - } + rsvg_handle_close(rsvg_handle, &gerr); + cairo_surface_destroy(cairo_surf); + free(image); + cairo_destroy(cr); + return (NULL); + } /* Convert the SDL surface to the display format, for faster blitting: */ sdl_surface = SDL_DisplayFormatAlpha(sdl_surface_tmp); SDL_FreeSurface(sdl_surface_tmp); if (sdl_surface == NULL) - { + { #ifdef DEBUG - fprintf(stderr, "SDL_DisplayFormatAlpha() failed\n"); + fprintf(stderr, "SDL_DisplayFormatAlpha() failed\n"); #endif - rsvg_handle_close(rsvg_handle, &gerr); - cairo_surface_destroy(cairo_surf); - free(image); - cairo_destroy(cr); - return (NULL); - } + rsvg_handle_close(rsvg_handle, &gerr); + cairo_surface_destroy(cairo_surf); + free(image); + cairo_destroy(cr); + return (NULL); + } #ifdef DEBUG - printf("SDL surface from %d x %d SVG is %d x %d\n", rwidth, rheight, sdl_surface->w, sdl_surface->h); + printf("SDL surface from %d x %d SVG is %d x %d\n", rwidth, rheight, + sdl_surface->w, sdl_surface->h); #endif @@ -20321,7 +21555,7 @@ static SDL_Surface *load_svg(const char *file) sdl_surface = _load_svg(file); if (sdl_surface == NULL) sdl_surface = IMG_Load(file); - return(sdl_surface); + return (sdl_surface); } #endif @@ -20330,18 +21564,20 @@ static SDL_Surface *load_svg(const char *file) /** * FIXME */ -static float pick_best_scape(unsigned int orig_w, unsigned int orig_h, unsigned int max_w, unsigned int max_h) +static float pick_best_scape(unsigned int orig_w, unsigned int orig_h, + unsigned int max_w, unsigned int max_h) { float aspect, scale, wscale, hscale; - aspect = (float)orig_w / (float)orig_h; + aspect = (float) orig_w / (float) orig_h; #ifdef DEBUG - printf("trying to fit %d x %d (aspect: %.4f) into %d x %d\n", orig_w, orig_h, aspect, max_w, max_h); + printf("trying to fit %d x %d (aspect: %.4f) into %d x %d\n", orig_w, + orig_h, aspect, max_w, max_h); #endif - wscale = (float)max_w / (float)orig_w; - hscale = (float)max_h / (float)orig_h; + wscale = (float) max_w / (float) orig_w; + hscale = (float) max_h / (float) orig_h; #ifdef DEBUG printf("max_w / orig_w = wscale: %.4f\n", wscale); @@ -20350,47 +21586,51 @@ static float pick_best_scape(unsigned int orig_w, unsigned int orig_h, unsigned #endif if (aspect >= 1) - { - /* Image is wider-than-tall (or square) */ + { + /* Image is wider-than-tall (or square) */ - scale = wscale; + scale = wscale; #ifdef DEBUG - printf("Wider-than-tall. Using wscale.\n"); - printf("new size would be: %d x %d\n", (int)((float)orig_w * scale), (int)((float)orig_h * scale)); + printf("Wider-than-tall. Using wscale.\n"); + printf("new size would be: %d x %d\n", (int) ((float) orig_w * scale), + (int) ((float) orig_h * scale)); #endif - if ((float)orig_h * scale > (float)max_h) - { - scale = hscale; - -#ifdef DEBUG - printf("Too tall! Using hscale!\n"); - printf("new size would be: %d x %d\n", (int)((float)orig_w * scale), (int)((float)orig_h * scale)); -#endif - } - } - else + if ((float) orig_h * scale > (float) max_h) { - /* Taller-than-wide */ - scale = hscale; #ifdef DEBUG - printf("Taller-than-wide. Using hscale.\n"); - printf("new size would be: %d x %d\n", (int)((float)orig_w * scale), (int)((float)orig_h * scale)); + printf("Too tall! Using hscale!\n"); + printf("new size would be: %d x %d\n", (int) ((float) orig_w * scale), + (int) ((float) orig_h * scale)); #endif + } + } + else + { + /* Taller-than-wide */ - if ((float)orig_w * scale > (float)max_w) - { - scale = wscale; + scale = hscale; #ifdef DEBUG - printf("Too wide! Using wscale!\n"); - printf("new size would be: %d x %d\n", (int)((float)orig_w * scale), (int)((float)orig_h * scale)); + printf("Taller-than-wide. Using hscale.\n"); + printf("new size would be: %d x %d\n", (int) ((float) orig_w * scale), + (int) ((float) orig_h * scale)); +#endif + + if ((float) orig_w * scale > (float) max_w) + { + scale = wscale; + +#ifdef DEBUG + printf("Too wide! Using wscale!\n"); + printf("new size would be: %d x %d\n", (int) ((float) orig_w * scale), + (int) ((float) orig_h * scale)); #endif - } } + } #ifdef DEBUG @@ -20445,19 +21685,20 @@ static SDL_Surface *myIMG_Load_RWops(const char *file) static SDL_Surface *myIMG_Load(const char *file) { if (strlen(file) > 4 && strcasecmp(file + strlen(file) - 4, ".kpx") == 0) - { - return (load_kpx(file)); + { + return (load_kpx(file)); #ifndef NOSVG - } - else if (strlen(file) > 4 && strcasecmp(file + strlen(file) - 4, ".svg") == 0) - { - return (load_svg(file)); + } + else if (strlen(file) > 4 + && strcasecmp(file + strlen(file) - 4, ".svg") == 0) + { + return (load_svg(file)); #endif - } + } else - { - return (myIMG_Load_RWops(file)); - } + { + return (myIMG_Load_RWops(file)); + } } /** @@ -20511,420 +21752,498 @@ static void load_magic_plugins(void) num_magics_total = 0; for (plc = 0; plc < NUM_MAGIC_PLACES; plc++) + { + if (plc == MAGIC_PLACE_GLOBAL) { - if (plc == MAGIC_PLACE_GLOBAL) - { #if defined (__ANDROID__) - /* Need this at runtime as Android installs on different locations depending on the user */ + /* Need this at runtime as Android installs on different locations depending on the user */ place = strdup(get_nativelibdir()); #else - place = strdup(MAGIC_PREFIX); + place = strdup(MAGIC_PREFIX); #endif - } + } + else if (plc == MAGIC_PLACE_LOCAL) + place = get_fname("plugins/", DIR_DATA); +#ifdef __APPLE__ + else if (plc == MAGIC_PLACE_ALLUSERS) + place = strdup("/Library/Application Support/TuxPaint/plugins/"); +#endif + else + continue; /* Huh? */ + +#ifdef DEBUG + printf("\n"); + printf("Loading magic plug-ins from %s\n", place); + fflush(stdout); +#endif + + /* Gather list of files (for sorting): */ + + d = opendir(place); + + if (d != NULL) + { + /* Set magic API hooks: */ + + magic_api_struct = (magic_api *) malloc(sizeof(magic_api)); + magic_api_struct->tp_version = strdup(VER_VERSION); + + if (plc == MAGIC_PLACE_GLOBAL) + magic_api_struct->data_directory = strdup(DATA_PREFIX); else if (plc == MAGIC_PLACE_LOCAL) - place = get_fname("plugins/", DIR_DATA); + magic_api_struct->data_directory = + get_fname("plugins/data/", DIR_DATA); #ifdef __APPLE__ else if (plc == MAGIC_PLACE_ALLUSERS) - place = strdup("/Library/Application Support/TuxPaint/plugins/"); + magic_api_struct->data_directory = + strdup("/Library/Application Support/TuxPaint/plugins/data"); #endif else - continue; /* Huh? */ + magic_api_struct->data_directory = strdup("./"); -#ifdef DEBUG - printf("\n"); - printf("Loading magic plug-ins from %s\n", place); - fflush(stdout); -#endif + magic_api_struct->update_progress_bar = update_progress_bar; + magic_api_struct->sRGB_to_linear = magic_sRGB_to_linear; + magic_api_struct->linear_to_sRGB = magic_linear_to_sRGB; + magic_api_struct->in_circle = in_circle_rad; + magic_api_struct->getpixel = magic_getpixel; + magic_api_struct->putpixel = magic_putpixel; + magic_api_struct->xorpixel = magic_xorpixel; + magic_api_struct->line = magic_line_func; + magic_api_struct->playsound = magic_playsound; + magic_api_struct->stopsound = magic_stopsound; + magic_api_struct->special_notify = special_notify; + magic_api_struct->button_down = magic_button_down; + magic_api_struct->rgbtohsv = rgbtohsv; + magic_api_struct->hsvtorgb = hsvtorgb; + magic_api_struct->canvas_w = canvas->w; + magic_api_struct->canvas_h = canvas->h; + magic_api_struct->scale = magic_scale; + magic_api_struct->touched = magic_touched; - /* Gather list of files (for sorting): */ - d = opendir(place); + do + { + f = readdir(d); - if (d != NULL) + if (f != NULL) { - /* Set magic API hooks: */ + struct stat sbuf; - magic_api_struct = (magic_api *) malloc(sizeof(magic_api)); - magic_api_struct->tp_version = strdup(VER_VERSION); + safe_snprintf(fname, sizeof(fname), "%s%s", place, f->d_name); + if (!stat(fname, &sbuf) && S_ISREG(sbuf.st_mode)) + { + /* Get just the name of the object (e.g., "negative"), w/o filename + extension: */ - if (plc == MAGIC_PLACE_GLOBAL) - magic_api_struct->data_directory = strdup(DATA_PREFIX); - else if (plc == MAGIC_PLACE_LOCAL) - magic_api_struct->data_directory = get_fname("plugins/data/", DIR_DATA); -#ifdef __APPLE__ - else if (plc == MAGIC_PLACE_ALLUSERS) - magic_api_struct->data_directory = strdup("/Library/Application Support/TuxPaint/plugins/data"); -#endif - else - magic_api_struct->data_directory = strdup("./"); - - magic_api_struct->update_progress_bar = update_progress_bar; - magic_api_struct->sRGB_to_linear = magic_sRGB_to_linear; - magic_api_struct->linear_to_sRGB = magic_linear_to_sRGB; - magic_api_struct->in_circle = in_circle_rad; - magic_api_struct->getpixel = magic_getpixel; - magic_api_struct->putpixel = magic_putpixel; - magic_api_struct->xorpixel = magic_xorpixel; - magic_api_struct->line = magic_line_func; - magic_api_struct->playsound = magic_playsound; - magic_api_struct->stopsound = magic_stopsound; - magic_api_struct->special_notify = special_notify; - magic_api_struct->button_down = magic_button_down; - magic_api_struct->rgbtohsv = rgbtohsv; - magic_api_struct->hsvtorgb = hsvtorgb; - magic_api_struct->canvas_w = canvas->w; - magic_api_struct->canvas_h = canvas->h; - magic_api_struct->scale = magic_scale; - magic_api_struct->touched = magic_touched; - - - do - { - f = readdir(d); - - if (f != NULL) - { - struct stat sbuf; - - safe_snprintf(fname, sizeof(fname), "%s%s", place, f->d_name); - if (!stat(fname, &sbuf) && S_ISREG(sbuf.st_mode)) - { - /* Get just the name of the object (e.g., "negative"), w/o filename - extension: */ - - safe_strncpy(objname, f->d_name, sizeof(objname)); - strcpy(strchr(objname, '.'), ""); /* safe; truncating */ + safe_strncpy(objname, f->d_name, sizeof(objname)); + strcpy(strchr(objname, '.'), ""); /* safe; truncating */ #if defined(__ANDROID__) - // since Android compiles magic tools with name like "libxxx.so", here we shall exclude the prefix "lib". - strcpy(objname, objname + 3); + // since Android compiles magic tools with name like "libxxx.so", here we shall exclude the prefix "lib". + strcpy(objname, objname + 3); #endif - magic_handle[num_plugin_files] = SDL_LoadObject(fname); + magic_handle[num_plugin_files] = SDL_LoadObject(fname); - if (magic_handle[num_plugin_files] != NULL) - { + if (magic_handle[num_plugin_files] != NULL) + { #ifdef DEBUG - printf("loading: %s\n", fname); - fflush(stdout); + printf("loading: %s\n", fname); + fflush(stdout); #endif - safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, "get_tool_count"); - magic_funcs[num_plugin_files].get_tool_count = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, + "get_tool_count"); + magic_funcs[num_plugin_files].get_tool_count = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, "get_group"); - magic_funcs[num_plugin_files].get_group = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, + "get_group"); + magic_funcs[num_plugin_files].get_group = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, "get_name"); - magic_funcs[num_plugin_files].get_name = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, + "get_name"); + magic_funcs[num_plugin_files].get_name = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, "get_icon"); - magic_funcs[num_plugin_files].get_icon = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, + "get_icon"); + magic_funcs[num_plugin_files].get_icon = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, "get_description"); - magic_funcs[num_plugin_files].get_description = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, + "get_description"); + magic_funcs[num_plugin_files].get_description = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, "requires_colors"); - magic_funcs[num_plugin_files].requires_colors = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, + "requires_colors"); + magic_funcs[num_plugin_files].requires_colors = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, "modes"); - magic_funcs[num_plugin_files].modes = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, + "modes"); + magic_funcs[num_plugin_files].modes = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, "set_color"); - magic_funcs[num_plugin_files].set_color = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, + "set_color"); + magic_funcs[num_plugin_files].set_color = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, "init"); - magic_funcs[num_plugin_files].init = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, + "init"); + magic_funcs[num_plugin_files].init = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, "api_version"); - magic_funcs[num_plugin_files].api_version = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, + "api_version"); + magic_funcs[num_plugin_files].api_version = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, "shutdown"); - magic_funcs[num_plugin_files].shutdown = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, + "shutdown"); + magic_funcs[num_plugin_files].shutdown = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, "click"); - magic_funcs[num_plugin_files].click = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, + "click"); + magic_funcs[num_plugin_files].click = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, "drag"); - magic_funcs[num_plugin_files].drag = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, + "drag"); + magic_funcs[num_plugin_files].drag = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, "release"); - magic_funcs[num_plugin_files].release = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, + "release"); + magic_funcs[num_plugin_files].release = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, "switchin"); - magic_funcs[num_plugin_files].switchin = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, + "switchin"); + magic_funcs[num_plugin_files].switchin = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, "switchout"); - magic_funcs[num_plugin_files].switchout = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + safe_snprintf(funcname, sizeof(funcname), "%s_%s", objname, + "switchout"); + magic_funcs[num_plugin_files].switchout = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); #ifdef DEBUG - //EP added (intptr_t) to avoid warning on x64 on all lines below - printf("get_tool_count = 0x%x\n", - (int)(intptr_t) magic_funcs[num_plugin_files].get_tool_count); - printf("get_group = 0x%x\n", (int)(intptr_t) magic_funcs[num_plugin_files].get_group); - printf("get_name = 0x%x\n", (int)(intptr_t) magic_funcs[num_plugin_files].get_name); - printf("get_icon = 0x%x\n", (int)(intptr_t) magic_funcs[num_plugin_files].get_icon); - printf("get_description = 0x%x\n", - (int)(intptr_t) magic_funcs[num_plugin_files].get_description); - printf("requires_colors = 0x%x\n", - (int)(intptr_t) magic_funcs[num_plugin_files].requires_colors); - printf("modes = 0x%x\n", (int)(intptr_t) magic_funcs[num_plugin_files].modes); - printf("set_color = 0x%x\n", (int)(intptr_t) magic_funcs[num_plugin_files].set_color); - printf("init = 0x%x\n", (int)(intptr_t) magic_funcs[num_plugin_files].init); - printf("api_version = 0x%x\n", (int)(intptr_t) magic_funcs[num_plugin_files].api_version); - printf("shutdown = 0x%x\n", (int)(intptr_t) magic_funcs[num_plugin_files].shutdown); - printf("click = 0x%x\n", (int)(intptr_t) magic_funcs[num_plugin_files].click); - printf("drag = 0x%x\n", (int)(intptr_t) magic_funcs[num_plugin_files].drag); - printf("release = 0x%x\n", (int)(intptr_t) magic_funcs[num_plugin_files].release); - printf("switchin = 0x%x\n", (int)(intptr_t) magic_funcs[num_plugin_files].switchin); - printf("switchout = 0x%x\n", (int)(intptr_t) magic_funcs[num_plugin_files].switchout); + //EP added (intptr_t) to avoid warning on x64 on all lines below + printf("get_tool_count = 0x%x\n", + (int) (intptr_t) + magic_funcs[num_plugin_files].get_tool_count); + printf("get_group = 0x%x\n", + (int) (intptr_t) + magic_funcs[num_plugin_files].get_group); + printf("get_name = 0x%x\n", + (int) (intptr_t) magic_funcs[num_plugin_files].get_name); + printf("get_icon = 0x%x\n", + (int) (intptr_t) magic_funcs[num_plugin_files].get_icon); + printf("get_description = 0x%x\n", + (int) (intptr_t) + magic_funcs[num_plugin_files].get_description); + printf("requires_colors = 0x%x\n", + (int) (intptr_t) + magic_funcs[num_plugin_files].requires_colors); + printf("modes = 0x%x\n", + (int) (intptr_t) magic_funcs[num_plugin_files].modes); + printf("set_color = 0x%x\n", + (int) (intptr_t) + magic_funcs[num_plugin_files].set_color); + printf("init = 0x%x\n", + (int) (intptr_t) magic_funcs[num_plugin_files].init); + printf("api_version = 0x%x\n", + (int) (intptr_t) + magic_funcs[num_plugin_files].api_version); + printf("shutdown = 0x%x\n", + (int) (intptr_t) magic_funcs[num_plugin_files].shutdown); + printf("click = 0x%x\n", + (int) (intptr_t) magic_funcs[num_plugin_files].click); + printf("drag = 0x%x\n", + (int) (intptr_t) magic_funcs[num_plugin_files].drag); + printf("release = 0x%x\n", + (int) (intptr_t) magic_funcs[num_plugin_files].release); + printf("switchin = 0x%x\n", + (int) (intptr_t) magic_funcs[num_plugin_files].switchin); + printf("switchout = 0x%x\n", + (int) (intptr_t) + magic_funcs[num_plugin_files].switchout); #endif - err = 0; + err = 0; - if (magic_funcs[num_plugin_files].get_tool_count == NULL) - { - fprintf(stderr, "Error: plugin %s is missing get_tool_count\n", fname); - err = 1; - } - if (magic_funcs[num_plugin_files].get_group == NULL) - { - fprintf(stderr, "Error: plugin %s is missing get_group\n", fname); - err = 1; - } - if (magic_funcs[num_plugin_files].get_name == NULL) - { - fprintf(stderr, "Error: plugin %s is missing get_name\n", fname); - err = 1; - } - if (magic_funcs[num_plugin_files].get_icon == NULL) - { - fprintf(stderr, "Error: plugin %s is missing get_icon\n", fname); - err = 1; - } - if (magic_funcs[num_plugin_files].get_description == NULL) - { - fprintf(stderr, "Error: plugin %s is missing get_description\n", fname); - err = 1; - } - if (magic_funcs[num_plugin_files].requires_colors == NULL) - { - fprintf(stderr, "Error: plugin %s is missing requires_colors\n", fname); - err = 1; - } - if (magic_funcs[num_plugin_files].modes == NULL) - { - fprintf(stderr, "Error: plugin %s is missing modes\n", fname); - err = 1; - } - if (magic_funcs[num_plugin_files].set_color == NULL) - { - fprintf(stderr, "Error: plugin %s is missing set_color\n", fname); - err = 1; - } - if (magic_funcs[num_plugin_files].init == NULL) - { - fprintf(stderr, "Error: plugin %s is missing init\n", fname); - err = 1; - } - if (magic_funcs[num_plugin_files].shutdown == NULL) - { - fprintf(stderr, "Error: plugin %s is missing shutdown\n", fname); - err = 1; - } - if (magic_funcs[num_plugin_files].click == NULL) - { - fprintf(stderr, "Error: plugin %s is missing click\n", fname); - err = 1; - } - if (magic_funcs[num_plugin_files].release == NULL) - { - fprintf(stderr, "Error: plugin %s is missing release\n", fname); - err = 1; - } - if (magic_funcs[num_plugin_files].switchin == NULL) - { - fprintf(stderr, "Error: plugin %s is missing switchin\n", fname); - err = 1; - } - if (magic_funcs[num_plugin_files].switchout == NULL) - { - fprintf(stderr, "Error: plugin %s is missing switchout\n", fname); - err = 1; - } - if (magic_funcs[num_plugin_files].drag == NULL) - { - fprintf(stderr, "Error: plugin %s is missing drag\n", fname); - err = 1; - } + if (magic_funcs[num_plugin_files].get_tool_count == NULL) + { + fprintf(stderr, + "Error: plugin %s is missing get_tool_count\n", + fname); + err = 1; + } + if (magic_funcs[num_plugin_files].get_group == NULL) + { + fprintf(stderr, "Error: plugin %s is missing get_group\n", + fname); + err = 1; + } + if (magic_funcs[num_plugin_files].get_name == NULL) + { + fprintf(stderr, "Error: plugin %s is missing get_name\n", + fname); + err = 1; + } + if (magic_funcs[num_plugin_files].get_icon == NULL) + { + fprintf(stderr, "Error: plugin %s is missing get_icon\n", + fname); + err = 1; + } + if (magic_funcs[num_plugin_files].get_description == NULL) + { + fprintf(stderr, + "Error: plugin %s is missing get_description\n", + fname); + err = 1; + } + if (magic_funcs[num_plugin_files].requires_colors == NULL) + { + fprintf(stderr, + "Error: plugin %s is missing requires_colors\n", + fname); + err = 1; + } + if (magic_funcs[num_plugin_files].modes == NULL) + { + fprintf(stderr, "Error: plugin %s is missing modes\n", fname); + err = 1; + } + if (magic_funcs[num_plugin_files].set_color == NULL) + { + fprintf(stderr, "Error: plugin %s is missing set_color\n", + fname); + err = 1; + } + if (magic_funcs[num_plugin_files].init == NULL) + { + fprintf(stderr, "Error: plugin %s is missing init\n", fname); + err = 1; + } + if (magic_funcs[num_plugin_files].shutdown == NULL) + { + fprintf(stderr, "Error: plugin %s is missing shutdown\n", + fname); + err = 1; + } + if (magic_funcs[num_plugin_files].click == NULL) + { + fprintf(stderr, "Error: plugin %s is missing click\n", fname); + err = 1; + } + if (magic_funcs[num_plugin_files].release == NULL) + { + fprintf(stderr, "Error: plugin %s is missing release\n", + fname); + err = 1; + } + if (magic_funcs[num_plugin_files].switchin == NULL) + { + fprintf(stderr, "Error: plugin %s is missing switchin\n", + fname); + err = 1; + } + if (magic_funcs[num_plugin_files].switchout == NULL) + { + fprintf(stderr, "Error: plugin %s is missing switchout\n", + fname); + err = 1; + } + if (magic_funcs[num_plugin_files].drag == NULL) + { + fprintf(stderr, "Error: plugin %s is missing drag\n", fname); + err = 1; + } - if (magic_funcs[num_plugin_files].api_version == NULL) - { - fprintf(stderr, "Error: plugin %s is missing api_version\n", fname); - err = 1; - } - else if (magic_funcs[num_plugin_files].api_version() != TP_MAGIC_API_VERSION) - { - fprintf(stderr, - "Warning: plugin %s uses Tux Paint 'Magic' tool API version %x,\nbut Tux Paint needs version %x.\n", - fname, magic_funcs[num_plugin_files].api_version(), TP_MAGIC_API_VERSION); - err = 1; - } + if (magic_funcs[num_plugin_files].api_version == NULL) + { + fprintf(stderr, "Error: plugin %s is missing api_version\n", + fname); + err = 1; + } + else if (magic_funcs[num_plugin_files].api_version() != + TP_MAGIC_API_VERSION) + { + fprintf(stderr, + "Warning: plugin %s uses Tux Paint 'Magic' tool API version %x,\nbut Tux Paint needs version %x.\n", + fname, magic_funcs[num_plugin_files].api_version(), + TP_MAGIC_API_VERSION); + err = 1; + } - if (err) - { - SDL_UnloadObject(magic_handle[num_plugin_files]); - } - else - { - res = magic_funcs[num_plugin_files].init(magic_api_struct); + if (err) + { + SDL_UnloadObject(magic_handle[num_plugin_files]); + } + else + { + res = magic_funcs[num_plugin_files].init(magic_api_struct); - if (res != 0) - n = magic_funcs[num_plugin_files].get_tool_count(magic_api_struct); - else - { - magic_funcs[num_plugin_files].shutdown(magic_api_struct); - n = 0; - } - - - if (n == 0) - { - fprintf(stderr, "Error: plugin %s failed to startup or reported 0 magic tools\n", - fname); - fflush(stderr); - SDL_UnloadObject(magic_handle[num_plugin_files]); - } - else - { - int j, group, idx; - SDL_Surface * icon_tmp; - - for (i = 0; i < n; i++) - { - group = magic_funcs[num_plugin_files].get_group(magic_api_struct, i); - if (group < MAX_MAGIC_GROUPS) - { - idx = num_magics[group]; - - magics[group][idx].idx = i; - magics[group][idx].place = plc; - magics[group][idx].handle_idx = num_plugin_files; - magics[group][idx].group = group; - magics[group][idx].name = - magic_funcs[num_plugin_files].get_name(magic_api_struct, i); - - magics[group][idx].avail_modes = - magic_funcs[num_plugin_files].modes(magic_api_struct, i); - - for (j = 0; j < MAX_MODES; j++) - { - magics[group][idx].tip[j] = NULL; - if (j) - { - if (magics[group][idx].avail_modes & MODE_FULLSCREEN) - magics[group][idx].tip[j] = - magic_funcs[num_plugin_files].get_description(magic_api_struct, i, - MODE_FULLSCREEN); - } - else - { - if (magics[group][idx].avail_modes & MODE_PAINT) - magics[group][idx].tip[j] = - magic_funcs[num_plugin_files].get_description(magic_api_struct, i, - MODE_PAINT); - else if (magics[group][idx].avail_modes & MODE_ONECLICK) - magics[group][idx].tip[j] = - magic_funcs[num_plugin_files].get_description(magic_api_struct, i, - MODE_ONECLICK); - else if (magics[group][idx].avail_modes & MODE_PAINT_WITH_PREVIEW) - magics[group][idx].tip[j] = - magic_funcs[num_plugin_files].get_description(magic_api_struct, i, - MODE_PAINT_WITH_PREVIEW); - } - } - - magics[group][idx].colors = - magic_funcs[num_plugin_files].requires_colors(magic_api_struct, i); - if (magics[group][idx].avail_modes & MODE_PAINT) - magics[group][idx].mode = MODE_PAINT; - else if (magics[group][idx].avail_modes & MODE_ONECLICK) - magics[group][idx].mode = MODE_ONECLICK; - else if (magics[group][idx].avail_modes & MODE_PAINT_WITH_PREVIEW) - magics[group][idx].mode = MODE_PAINT_WITH_PREVIEW; - else - magics[group][idx].mode = MODE_FULLSCREEN; - - icon_tmp = magic_funcs[num_plugin_files].get_icon(magic_api_struct, i); - if (icon_tmp != NULL) - { - magics[group][idx].img_icon = thumbnail(icon_tmp, 40 * button_w / ORIGINAL_BUTTON_SIZE, 30 * button_h / ORIGINAL_BUTTON_SIZE, 1); - SDL_FreeSurface(icon_tmp); - -#ifdef DEBUG - printf("-- %s\n", magics[group][idx].name); - printf("avail_modes = %d\n", magics[group][idx].avail_modes); -#endif - - num_magics[group]++; - num_magics_total++; - } - else - { - fprintf(stderr, "Error: plugin %s mode # %d failed to load an icon\n", - fname, i); - fflush(stderr); - } - } - else - { - fprintf(stderr, "Error: plugin %s mode # %d reported group %d (higher than %d)\n", - fname, i, group, MAX_MAGIC_GROUPS - 1); - fflush(stderr); - } - } - - num_plugin_files++; - } - } - } - else - { - fprintf(stderr, "Warning: Failed to load object %s: %s\n", fname, SDL_GetError()); - fflush(stderr); - } - } + if (res != 0) + n = + magic_funcs[num_plugin_files].get_tool_count + (magic_api_struct); + else + { + magic_funcs[num_plugin_files].shutdown(magic_api_struct); + n = 0; } + + + if (n == 0) + { + fprintf(stderr, + "Error: plugin %s failed to startup or reported 0 magic tools\n", + fname); + fflush(stderr); + SDL_UnloadObject(magic_handle[num_plugin_files]); + } + else + { + int j, group, idx; + SDL_Surface *icon_tmp; + + for (i = 0; i < n; i++) + { + group = + magic_funcs[num_plugin_files].get_group + (magic_api_struct, i); + if (group < MAX_MAGIC_GROUPS) + { + idx = num_magics[group]; + + magics[group][idx].idx = i; + magics[group][idx].place = plc; + magics[group][idx].handle_idx = num_plugin_files; + magics[group][idx].group = group; + magics[group][idx].name = + magic_funcs[num_plugin_files].get_name + (magic_api_struct, i); + + magics[group][idx].avail_modes = + magic_funcs[num_plugin_files].modes(magic_api_struct, + i); + + for (j = 0; j < MAX_MODES; j++) + { + magics[group][idx].tip[j] = NULL; + if (j) + { + if (magics[group][idx].avail_modes & + MODE_FULLSCREEN) + magics[group][idx].tip[j] = + magic_funcs[num_plugin_files].get_description + (magic_api_struct, i, MODE_FULLSCREEN); + } + else + { + if (magics[group][idx].avail_modes & MODE_PAINT) + magics[group][idx].tip[j] = + magic_funcs[num_plugin_files].get_description + (magic_api_struct, i, MODE_PAINT); + else if (magics[group][idx].avail_modes & + MODE_ONECLICK) + magics[group][idx].tip[j] = + magic_funcs[num_plugin_files].get_description + (magic_api_struct, i, MODE_ONECLICK); + else if (magics[group][idx].avail_modes & + MODE_PAINT_WITH_PREVIEW) + magics[group][idx].tip[j] = + magic_funcs[num_plugin_files].get_description + (magic_api_struct, i, MODE_PAINT_WITH_PREVIEW); + } + } + + magics[group][idx].colors = + magic_funcs[num_plugin_files].requires_colors + (magic_api_struct, i); + if (magics[group][idx].avail_modes & MODE_PAINT) + magics[group][idx].mode = MODE_PAINT; + else if (magics[group][idx].avail_modes & MODE_ONECLICK) + magics[group][idx].mode = MODE_ONECLICK; + else if (magics[group][idx].avail_modes & + MODE_PAINT_WITH_PREVIEW) + magics[group][idx].mode = MODE_PAINT_WITH_PREVIEW; + else + magics[group][idx].mode = MODE_FULLSCREEN; + + icon_tmp = + magic_funcs[num_plugin_files].get_icon + (magic_api_struct, i); + if (icon_tmp != NULL) + { + magics[group][idx].img_icon = + thumbnail(icon_tmp, + 40 * button_w / ORIGINAL_BUTTON_SIZE, + 30 * button_h / ORIGINAL_BUTTON_SIZE, 1); + SDL_FreeSurface(icon_tmp); + +#ifdef DEBUG + printf("-- %s\n", magics[group][idx].name); + printf("avail_modes = %d\n", + magics[group][idx].avail_modes); +#endif + + num_magics[group]++; + num_magics_total++; + } + else + { + fprintf(stderr, + "Error: plugin %s mode # %d failed to load an icon\n", + fname, i); + fflush(stderr); + } + } + else + { + fprintf(stderr, + "Error: plugin %s mode # %d reported group %d (higher than %d)\n", + fname, i, group, MAX_MAGIC_GROUPS - 1); + fflush(stderr); + } + } + + num_plugin_files++; + } + } } - while (f != NULL); - - closedir(d); + else + { + fprintf(stderr, "Warning: Failed to load object %s: %s\n", + fname, SDL_GetError()); + fflush(stderr); + } + } } + } + while (f != NULL); + + closedir(d); } + } - for (i = 0; i < MAX_MAGIC_GROUPS; i++) { + for (i = 0; i < MAX_MAGIC_GROUPS; i++) + { qsort(magics[i], num_magics[i], sizeof(magic_t), magic_sort); } #ifdef DEBUG - printf("Loaded %d magic tools from %d plug-in files\n", num_magics_total, num_plugin_files); + printf("Loaded %d magic tools from %d plug-in files\n", num_magics_total, + num_plugin_files); printf("\n"); fflush(stdout); #endif @@ -20956,9 +22275,12 @@ static void update_progress_bar(void) * FIXME */ static void magic_line_func(void *mapi, - int which, SDL_Surface * canvas, SDL_Surface * last, - int x1, int y1, int x2, int y2, int step, - void (*cb) (void *, int, SDL_Surface *, SDL_Surface *, int, int)) + int which, SDL_Surface * canvas, + SDL_Surface * last, int x1, int y1, int x2, + int y2, int step, void (*cb)(void *, int, + SDL_Surface *, + SDL_Surface *, int, + int)) { int dx, dy, y; float m, b; @@ -20970,64 +22292,64 @@ static void magic_line_func(void *mapi, cnt = step - 1; if (dx != 0) + { + m = ((float) dy) / ((float) dx); + b = y1 - m * x1; + + if (x2 >= x1) + dx = 1; + else + dx = -1; + + + while (x1 != x2) { - m = ((float)dy) / ((float)dx); - b = y1 - m * x1; + y1 = m * x1 + b; + y2 = m * (x1 + dx) + b; - if (x2 >= x1) - dx = 1; - else - dx = -1; - - - while (x1 != x2) - { - y1 = m * x1 + b; - y2 = m * (x1 + dx) + b; - - if (y1 > y2) - { - for (y = y1; y >= y2; y--) - { - cnt = (cnt + 1) % step; - if (cnt == 0) - cb((void *)mapi, which, canvas, last, x1, y); - } - } - else - { - for (y = y1; y <= y2; y++) - { - cnt = (cnt + 1) % step; - if (cnt == 0) - cb((void *)mapi, which, canvas, last, x1, y); - } - } - - x1 = x1 + dx; - } - } - else - { if (y1 > y2) + { + for (y = y1; y >= y2; y--) { - for (y = y1; y >= y2; y--) - { - cnt = (cnt + 1) % step; - if (cnt == 0) - cb((void *)mapi, which, canvas, last, x1, y); - } + cnt = (cnt + 1) % step; + if (cnt == 0) + cb((void *) mapi, which, canvas, last, x1, y); } + } else + { + for (y = y1; y <= y2; y++) { - for (y = y1; y <= y2; y++) - { - cnt = (cnt + 1) % step; - if (cnt == 0) - cb((void *)mapi, which, canvas, last, x1, y); - } + cnt = (cnt + 1) % step; + if (cnt == 0) + cb((void *) mapi, which, canvas, last, x1, y); } + } + + x1 = x1 + dx; } + } + else + { + if (y1 > y2) + { + for (y = y1; y >= y2; y--) + { + cnt = (cnt + 1) % step; + if (cnt == 0) + cb((void *) mapi, which, canvas, last, x1, y); + } + } + else + { + for (y = y1; y <= y2; y++) + { + cnt = (cnt + 1) % step; + if (cnt == 0) + cb((void *) mapi, which, canvas, last, x1, y); + } + } + } /* FIXME: Set and return an update rect? */ } @@ -21047,28 +22369,28 @@ static void special_notify(int flags) tmp_int = NUM_UNDO_BUFS - 1; if (flags & SPECIAL_MIRROR) - { - /* Mirror starter, too! */ + { + /* Mirror starter, too! */ - starter_mirrored = !starter_mirrored; + starter_mirrored = !starter_mirrored; - if (img_starter != NULL) - mirror_starter(); + if (img_starter != NULL) + mirror_starter(); - undo_starters[tmp_int] = UNDO_STARTER_MIRRORED; - } + undo_starters[tmp_int] = UNDO_STARTER_MIRRORED; + } if (flags & SPECIAL_FLIP) - { - /* Flip starter, too! */ + { + /* Flip starter, too! */ - starter_flipped = !starter_flipped; + starter_flipped = !starter_flipped; - if (img_starter != NULL) - flip_starter(); + if (img_starter != NULL) + flip_starter(); - undo_starters[tmp_int] = UNDO_STARTER_FLIPPED; - } + undo_starters[tmp_int] = UNDO_STARTER_FLIPPED; + } } /** @@ -21124,14 +22446,14 @@ static void magic_playsound(Mix_Chunk * snd, int left_right, int up_down) left_right = 255; if (use_stereo) - { - left = ((255 - dist) * (255 - left_right)) / 255; - } + { + left = ((255 - dist) * (255 - left_right)) / 255; + } else - { - /* Stereo disabled; no panning (see playsound.c) */ - left = (255 - dist) / 2; - } + { + /* Stereo disabled; no panning (see playsound.c) */ + left = (255 - dist) / 2; + } Mix_SetPanning(0, left, (255 - dist) - left); #endif @@ -21211,7 +22533,7 @@ static int do_new_dialog(void) things_alloced = 32; - fs = (struct dirent2 *)malloc(sizeof(struct dirent2) * things_alloced); + fs = (struct dirent2 *) malloc(sizeof(struct dirent2) * things_alloced); num_files = 0; cur = 0; @@ -21221,104 +22543,112 @@ static int do_new_dialog(void) /* Open directories of images: */ - for (places_to_look = 0; places_to_look < NUM_PLACES_TO_LOOK; places_to_look++) + for (places_to_look = 0; places_to_look < NUM_PLACES_TO_LOOK; + places_to_look++) + { + if (places_to_look == PLACE_SAVED_DIR) { - if (places_to_look == PLACE_SAVED_DIR) - { - /* Skip saved images; only want starters! */ - dirname[places_to_look] = NULL; - continue; /* ugh */ - } - else if (places_to_look == PLACE_PERSONAL_STARTERS_DIR) - { - /* Check for coloring-book style 'starter' images in our folder: */ - - dirname[places_to_look] = get_fname("starters", DIR_DATA); - } - else if (places_to_look == PLACE_STARTERS_DIR) - { - /* Finally, check for system-wide coloring-book style - 'starter' images: */ - - dirname[places_to_look] = strdup(DATA_PREFIX "starters"); - } - else if (places_to_look == PLACE_PERSONAL_TEMPLATES_DIR) - { - /* Check for 'template' images in our folder: */ - - dirname[places_to_look] = get_fname("templates", DIR_DATA); - } - else if (places_to_look == PLACE_TEMPLATES_DIR) - { - /* Finally, check for system-wide 'template' images: */ - - dirname[places_to_look] = strdup(DATA_PREFIX "templates"); - } - - - /* Read directory of images and build thumbnails: */ - - d = opendir(dirname[places_to_look]); - - if (d != NULL) - { - /* Gather list of files (for sorting): */ - - do - { - f = readdir(d); - - if (f !=NULL) - { - safe_snprintf(fname, sizeof(fname), "%s/%s", dirname[places_to_look], f->d_name); - if (!stat(fname, &sbuf) && S_ISREG(sbuf.st_mode)) - { - memcpy(&(fs[num_files_in_dirs].f), f, sizeof(struct dirent)); - fs[num_files_in_dirs].place = places_to_look; - - num_files_in_dirs++; - - if (num_files_in_dirs >= things_alloced) - { - things_alloced = things_alloced + 32; - - fs = (struct dirent2 *)realloc(fs, sizeof(struct dirent2) * things_alloced); - } - } - } - } - while (f != NULL); - - closedir(d); - } -#ifdef __ANDROID__ - else if (dirname[places_to_look][0] != "/") - { - /* Try inside android assets only if it is a relative path */ - - AAssetDir *ad = open_asset_dir(dirname[places_to_look]); - const char *afilename = (const char *)NULL; - - while ((afilename = AAssetDir_getNextFileName(ad)) != NULL) - { - f = malloc(sizeof(struct dirent)); - strncpy(f->d_name, afilename, sizeof(f->d_name)); - - memcpy(&(fs[num_files_in_dirs].f), f, sizeof(struct dirent)); - fs[num_files_in_dirs].place = places_to_look; - free(f); - num_files_in_dirs++; - - if (num_files_in_dirs >= things_alloced) - { - things_alloced = things_alloced + 32; - - fs = (struct dirent2 *)realloc(fs, sizeof(struct dirent2) * things_alloced); - } - } - } -#endif + /* Skip saved images; only want starters! */ + dirname[places_to_look] = NULL; + continue; /* ugh */ } + else if (places_to_look == PLACE_PERSONAL_STARTERS_DIR) + { + /* Check for coloring-book style 'starter' images in our folder: */ + + dirname[places_to_look] = get_fname("starters", DIR_DATA); + } + else if (places_to_look == PLACE_STARTERS_DIR) + { + /* Finally, check for system-wide coloring-book style + 'starter' images: */ + + dirname[places_to_look] = strdup(DATA_PREFIX "starters"); + } + else if (places_to_look == PLACE_PERSONAL_TEMPLATES_DIR) + { + /* Check for 'template' images in our folder: */ + + dirname[places_to_look] = get_fname("templates", DIR_DATA); + } + else if (places_to_look == PLACE_TEMPLATES_DIR) + { + /* Finally, check for system-wide 'template' images: */ + + dirname[places_to_look] = strdup(DATA_PREFIX "templates"); + } + + + /* Read directory of images and build thumbnails: */ + + d = opendir(dirname[places_to_look]); + + if (d != NULL) + { + /* Gather list of files (for sorting): */ + + do + { + f = readdir(d); + + if (f != NULL) + { + safe_snprintf(fname, sizeof(fname), "%s/%s", + dirname[places_to_look], f->d_name); + if (!stat(fname, &sbuf) && S_ISREG(sbuf.st_mode)) + { + memcpy(&(fs[num_files_in_dirs].f), f, sizeof(struct dirent)); + fs[num_files_in_dirs].place = places_to_look; + + num_files_in_dirs++; + + if (num_files_in_dirs >= things_alloced) + { + things_alloced = things_alloced + 32; + + fs = + (struct dirent2 *) realloc(fs, + sizeof(struct dirent2) * + things_alloced); + } + } + } + } + while (f != NULL); + + closedir(d); + } +#ifdef __ANDROID__ + else if (dirname[places_to_look][0] != "/") + { + /* Try inside android assets only if it is a relative path */ + + AAssetDir *ad = open_asset_dir(dirname[places_to_look]); + const char *afilename = (const char *) NULL; + + while ((afilename = AAssetDir_getNextFileName(ad)) != NULL) + { + f = malloc(sizeof(struct dirent)); + strncpy(f->d_name, afilename, sizeof(f->d_name)); + + memcpy(&(fs[num_files_in_dirs].f), f, sizeof(struct dirent)); + fs[num_files_in_dirs].place = places_to_look; + free(f); + num_files_in_dirs++; + + if (num_files_in_dirs >= things_alloced) + { + things_alloced = things_alloced + 32; + + fs = + (struct dirent2 *) realloc(fs, + sizeof(struct dirent2) * + things_alloced); + } + } + } +#endif + } /* (Re)allocate space for the information about these files: */ @@ -21328,15 +22658,16 @@ static int do_new_dialog(void) tot += NUM_COLORS; thumbs = (SDL_Surface * *)malloc(sizeof(SDL_Surface *) * tot); - d_places = (int *)malloc(sizeof(int) * tot); - d_names = (char **)malloc(sizeof(char *) * tot); - d_exts = (char **)malloc(sizeof(char *) * tot); + d_places = (int *) malloc(sizeof(int) * tot); + d_names = (char **) malloc(sizeof(char *) * tot); + d_exts = (char **) malloc(sizeof(char *) * tot); /* Sort: */ /* (N.B. "New" dialog not affected by 'reversesort' option) */ - qsort(fs, num_files_in_dirs, sizeof(struct dirent2), (int (*)(const void *, const void *))compare_dirent2s); + qsort(fs, num_files_in_dirs, sizeof(struct dirent2), + (int (*)(const void *, const void *)) compare_dirent2s); /* Throw the color palette at the beginning (default): */ @@ -21344,10 +22675,12 @@ static int do_new_dialog(void) white_in_palette = -1; if (!new_colors_last) - { - first_color = 0; - num_files = do_new_dialog_add_colors(thumbs, num_files, d_places, d_names, d_exts, &white_in_palette); - } + { + first_color = 0; + num_files = + do_new_dialog_add_colors(thumbs, num_files, d_places, d_names, d_exts, + &white_in_palette); + } first_starter = num_files; first_template = -1; /* In case there are none... */ @@ -21356,312 +22689,337 @@ static int do_new_dialog(void) /* Read directory of images and build thumbnails: */ for (j = 0; j < num_files_in_dirs; j++) + { + f = &(fs[j].f); + place = fs[j].place; + + if ((place == PLACE_PERSONAL_TEMPLATES_DIR + || place == PLACE_TEMPLATES_DIR) && first_template == -1) + first_template = num_files; + + show_progress_bar(screen); + + if (f != NULL) { - f = &(fs[j].f); - place = fs[j].place; + debug(f->d_name); - if ((place == PLACE_PERSONAL_TEMPLATES_DIR || place == PLACE_TEMPLATES_DIR) && first_template == -1) - first_template = num_files; - - show_progress_bar(screen); - - if (f != NULL) + if (strcasestr(f->d_name, "-t.") == NULL + && strcasestr(f->d_name, "-back.") == NULL) + { + if (strcasestr(f->d_name, FNAME_EXTENSION) != NULL + /* Support legacy BMP files for load: */ + || strcasestr(f->d_name, ".bmp") != NULL + /* Support for KPX (Kid Pix templates; just a JPEG with resource fork header): */ + || strcasestr(f->d_name, ".kpx") != NULL + || strcasestr(f->d_name, ".jpg") != NULL +#ifndef NOSVG + || strcasestr(f->d_name, ".svg") != NULL +#endif + ) { - debug(f->d_name); + safe_strncpy(fname, f->d_name, sizeof(fname)); + skip = 0; - if (strcasestr(f->d_name, "-t.") == NULL && strcasestr(f->d_name, "-back.") == NULL) + if (strcasestr(fname, FNAME_EXTENSION) != NULL) + { + d_exts[num_files] = strdup(strcasestr(fname, FNAME_EXTENSION)); + strcpy((char *) strcasestr(fname, FNAME_EXTENSION), ""); /* safe; truncating */ + } + + if (strcasestr(fname, ".bmp") != NULL) + { + d_exts[num_files] = strdup(strcasestr(fname, ".bmp")); + strcpy((char *) strcasestr(fname, ".bmp"), ""); /* safe; truncating */ + } + +#ifndef NOSVG + if (strcasestr(fname, ".svg") != NULL) + { + d_exts[num_files] = strdup(strcasestr(fname, ".svg")); + strcpy((char *) strcasestr(fname, ".svg"), ""); /* safe; truncating */ + } +#endif + + if (strcasestr(fname, ".kpx") != NULL) + { + d_exts[num_files] = strdup(strcasestr(fname, ".kpx")); + strcpy((char *) strcasestr(fname, ".kpx"), ""); /* safe; truncating */ + } + + if (strcasestr(fname, ".jpg") != NULL) + { + d_exts[num_files] = strdup(strcasestr(fname, ".jpg")); + strcpy((char *) strcasestr(fname, ".jpg"), ""); /* safe; truncating */ + } + +#ifndef NOSVG + /* If identically-named SVG exists, skip this version */ + for (k = 0; k < num_files_in_dirs; k++) + { + if (k != j) { - if (strcasestr(f->d_name, FNAME_EXTENSION) != NULL - /* Support legacy BMP files for load: */ - || strcasestr(f->d_name, ".bmp") != NULL - /* Support for KPX (Kid Pix templates; just a JPEG with resource fork header): */ - || strcasestr(f->d_name, ".kpx") != NULL - || strcasestr(f->d_name, ".jpg") != NULL -#ifndef NOSVG - || strcasestr(f->d_name, ".svg") != NULL -#endif - ) - { - safe_strncpy(fname, f->d_name, sizeof(fname)); - skip = 0; + struct dirent *f2; + char fname2[1024]; - if (strcasestr(fname, FNAME_EXTENSION) != NULL) - { - d_exts[num_files] = strdup(strcasestr(fname, FNAME_EXTENSION)); - strcpy((char *)strcasestr(fname, FNAME_EXTENSION), ""); /* safe; truncating */ - } + f2 = &(fs[k].f); + safe_strncpy(fname2, f2->d_name, sizeof(fname2)); - if (strcasestr(fname, ".bmp") != NULL) - { - d_exts[num_files] = strdup(strcasestr(fname, ".bmp")); - strcpy((char *)strcasestr(fname, ".bmp"), ""); /* safe; truncating */ - } - -#ifndef NOSVG - if (strcasestr(fname, ".svg") != NULL) - { - d_exts[num_files] = strdup(strcasestr(fname, ".svg")); - strcpy((char *)strcasestr(fname, ".svg"), ""); /* safe; truncating */ - } -#endif - - if (strcasestr(fname, ".kpx") != NULL) - { - d_exts[num_files] = strdup(strcasestr(fname, ".kpx")); - strcpy((char *)strcasestr(fname, ".kpx"), ""); /* safe; truncating */ - } - - if (strcasestr(fname, ".jpg") != NULL) - { - d_exts[num_files] = strdup(strcasestr(fname, ".jpg")); - strcpy((char *)strcasestr(fname, ".jpg"), ""); /* safe; truncating */ - } - -#ifndef NOSVG - /* If identically-named SVG exists, skip this version */ - for (k = 0; k < num_files_in_dirs; k++) - { - if (k != j) - { - struct dirent *f2; - char fname2[1024]; - - f2 = &(fs[k].f); - safe_strncpy(fname2, f2->d_name, sizeof(fname2)); - - if (strstr(fname2, fname) == fname2 && strlen(fname) == strlen(fname2)- strlen(".svg") && strcasestr(fname2, ".svg") != NULL) - { - /* SVG of this bitmap exists; we'll skip it */ - skip = 1; - } - } - } -#endif - if (skip) - { - free(d_exts[num_files]); - } - else - { - d_names[num_files] = strdup(fname); - d_places[num_files] = place; - - - /* Try to load thumbnail first: */ - - safe_snprintf(fname, sizeof(fname), "%s/.thumbs/%s-t.png", - dirname[d_places[num_files]], d_names[num_files]); - debug(fname); - img = IMG_Load(fname); - - if (img == NULL) - { - /* No thumbnail in the new location ("saved/.thumbs"), - try the old location ("saved/"): */ - - safe_snprintf(fname, sizeof(fname), "%s/%s-t.png", - dirname[d_places[num_files]], d_names[num_files]); - debug(fname); - - img = IMG_Load(fname); - } - - if (img != NULL) - { - /* Loaded the thumbnail from one or the other location */ - show_progress_bar(screen); - - img1 = SDL_DisplayFormat(img); - SDL_FreeSurface(img); - - /* if too big, or too small in both dimensions, rescale it - (for now: using old thumbnail as source for high speed, - low quality) */ - if (img1->w > THUMB_W - 20 || img1->h > THUMB_H - 20 - || (img1->w < THUMB_W - 20 && img1->h < THUMB_H - 20)) - { - img2 = thumbnail(img1, THUMB_W - 20, THUMB_H - 20, 0); - SDL_FreeSurface(img1); - img1 = img2; - } - - thumbs[num_files] = img1; - - if (thumbs[num_files] == NULL) - { - fprintf(stderr, - "\nError: Couldn't create a thumbnail of saved image!\n" "%s\n", fname); - } - - num_files++; - } - else - { - /* No thumbnail - load original: */ - - if (d_places[num_files] == PLACE_PERSONAL_TEMPLATES_DIR || - d_places[num_files] == PLACE_PERSONAL_STARTERS_DIR) - { - /* Make sure we have a ~/.tuxpaint/[starters|templates] directory: */ - if (make_directory(DIR_SAVE, dirname[d_places[num_files]], "Can't create user data directory (for starters/templates) (E010)")) - { - /* (Make sure we have a .../[starters|templates]/.thumbs/ directory:) */ - safe_snprintf(fname, sizeof(fname), "%s/.thumbs", dirname[d_places[num_files]]); - make_directory(DIR_SAVE, fname, "Can't create user data thumbnail directory (for starters/templates) (E011)"); - } - } - - img = NULL; - - if (d_places[num_files] == PLACE_STARTERS_DIR || - d_places[num_files] == PLACE_PERSONAL_STARTERS_DIR) - { - /* Try to load a starter's background image, first! - If it exists, it should give a better idea of what the - starter looks like, compared to the overlay image... */ - - /* (Try JPEG first) */ - safe_snprintf(fname, sizeof(fname), "%s/%s-back", - dirname[d_places[num_files]], d_names[num_files]); - img = load_starter_helper(fname, "jpeg", &IMG_Load); - if (img == NULL) - { - safe_snprintf(fname, sizeof(fname), "%s/%s-back", - dirname[d_places[num_files]], d_names[num_files]); - img = load_starter_helper(fname, "jpg", &IMG_Load); - } - -#ifndef NOSVG - if (img == NULL) - { - /* (Try SVG next) */ - safe_snprintf(fname, sizeof(fname), "%s/%s-back", - dirname[d_places[num_files]], d_names[num_files]); - img = load_starter_helper(fname, "svg", &load_svg); - } -#endif - - if (img == NULL) - { - /* (Try PNG next) */ - safe_snprintf(fname, sizeof(fname), "%s/%s-back", - dirname[d_places[num_files]], d_names[num_files]); - img = load_starter_helper(fname, "png", &IMG_Load); - } - } - - if (img == NULL) - { - /* Didn't load a starter background (or didn't try!), - try loading the actual image... */ - - safe_snprintf(fname, sizeof(fname), "%s/%s", dirname[d_places[num_files]], f->d_name); - debug(fname); - img = myIMG_Load(fname); - } - - - show_progress_bar(screen); - - if (img == NULL) - { - fprintf(stderr, - "\nWarning: I can't open one of the saved files!\n" - "%s\n" - "The Simple DirectMedia Layer error that " - "occurred was:\n" "%s\n\n", fname, SDL_GetError()); - - free(d_names[num_files]); - free(d_exts[num_files]); - } - else - { - /* Turn it into a thumbnail: */ - - img1 = SDL_DisplayFormatAlpha(img); - img2 = thumbnail2(img1, THUMB_W - 20, THUMB_H - 20, 0, 0); - SDL_FreeSurface(img1); - - show_progress_bar(screen); - - thumbs[num_files] = SDL_DisplayFormat(img2); - SDL_FreeSurface(img2); - if (thumbs[num_files] == NULL) - { - fprintf(stderr, - "\nError: Couldn't create a thumbnail of saved image!\n" "%s\n", fname); - } - - SDL_FreeSurface(img); - - show_progress_bar(screen); - - - /* Let's save this thumbnail, so we don't have to - create it again next time 'Open' is called: */ - /* if (d_places[num_files] == PLACE_SAVED_DIR) *//* <-- FIXME: This test should probably go...? -bjk 2009.10.15 */ - - if (d_places[num_files] == PLACE_PERSONAL_STARTERS_DIR || /* We must check to not try to write to system wide dirs Pere 2010.3.25 */ - d_places[num_files] == PLACE_PERSONAL_TEMPLATES_DIR) - { - debug("Saving thumbnail for this one!"); - - safe_snprintf(fname, sizeof(fname), "%s/.thumbs/%s-t.png", - dirname[d_places[num_files]], d_names[num_files]); - - if (!make_directory(DIR_SAVE, "starters", "Can't create user data directory (for starters) (E012)") || - !make_directory(DIR_SAVE, "templates", "Can't create user data directory (for templates) (E013)") || - !make_directory(DIR_SAVE, "starters/.thumbs", "Can't create user data directory (for starters) (E014)") || - !make_directory(DIR_SAVE, "templates/.thumbs", "Can't create user data directory (for templates) (E015)")) - fprintf(stderr, "Cannot save any pictures! SORRY!\n\n"); - else - { - fi = fopen(fname, "wb"); - if (fi == NULL) - { - fprintf(stderr, - "\nError: Couldn't save thumbnail of " - "saved image!\n" - "%s\n" - "The error that occurred was:\n" "%s\n\n", fname, strerror(errno)); - } - else - { - do_png_save(fi, fname, thumbs[num_files], 0); - } - } - - show_progress_bar(screen); - } - - - num_files++; - } - } - } - } + if (strstr(fname2, fname) == fname2 + && strlen(fname) == strlen(fname2) - strlen(".svg") + && strcasestr(fname2, ".svg") != NULL) + { + /* SVG of this bitmap exists; we'll skip it */ + skip = 1; + } } + } +#endif + if (skip) + { + free(d_exts[num_files]); + } else + { + d_names[num_files] = strdup(fname); + d_places[num_files] = place; + + + /* Try to load thumbnail first: */ + + safe_snprintf(fname, sizeof(fname), "%s/.thumbs/%s-t.png", + dirname[d_places[num_files]], d_names[num_files]); + debug(fname); + img = IMG_Load(fname); + + if (img == NULL) { - /* It was a thumbnail file ("...-t.png") or immutable scene starter's - overlay layer ("...-front.png") */ + /* No thumbnail in the new location ("saved/.thumbs"), + try the old location ("saved/"): */ + + safe_snprintf(fname, sizeof(fname), "%s/%s-t.png", + dirname[d_places[num_files]], d_names[num_files]); + debug(fname); + + img = IMG_Load(fname); } + + if (img != NULL) + { + /* Loaded the thumbnail from one or the other location */ + show_progress_bar(screen); + + img1 = SDL_DisplayFormat(img); + SDL_FreeSurface(img); + + /* if too big, or too small in both dimensions, rescale it + (for now: using old thumbnail as source for high speed, + low quality) */ + if (img1->w > THUMB_W - 20 || img1->h > THUMB_H - 20 + || (img1->w < THUMB_W - 20 && img1->h < THUMB_H - 20)) + { + img2 = thumbnail(img1, THUMB_W - 20, THUMB_H - 20, 0); + SDL_FreeSurface(img1); + img1 = img2; + } + + thumbs[num_files] = img1; + + if (thumbs[num_files] == NULL) + { + fprintf(stderr, + "\nError: Couldn't create a thumbnail of saved image!\n" + "%s\n", fname); + } + + num_files++; + } + else + { + /* No thumbnail - load original: */ + + if (d_places[num_files] == PLACE_PERSONAL_TEMPLATES_DIR || + d_places[num_files] == PLACE_PERSONAL_STARTERS_DIR) + { + /* Make sure we have a ~/.tuxpaint/[starters|templates] directory: */ + if (make_directory + (DIR_SAVE, dirname[d_places[num_files]], + "Can't create user data directory (for starters/templates) (E010)")) + { + /* (Make sure we have a .../[starters|templates]/.thumbs/ directory:) */ + safe_snprintf(fname, sizeof(fname), "%s/.thumbs", + dirname[d_places[num_files]]); + make_directory(DIR_SAVE, fname, + "Can't create user data thumbnail directory (for starters/templates) (E011)"); + } + } + + img = NULL; + + if (d_places[num_files] == PLACE_STARTERS_DIR || + d_places[num_files] == PLACE_PERSONAL_STARTERS_DIR) + { + /* Try to load a starter's background image, first! + If it exists, it should give a better idea of what the + starter looks like, compared to the overlay image... */ + + /* (Try JPEG first) */ + safe_snprintf(fname, sizeof(fname), "%s/%s-back", + dirname[d_places[num_files]], + d_names[num_files]); + img = load_starter_helper(fname, "jpeg", &IMG_Load); + if (img == NULL) + { + safe_snprintf(fname, sizeof(fname), "%s/%s-back", + dirname[d_places[num_files]], + d_names[num_files]); + img = load_starter_helper(fname, "jpg", &IMG_Load); + } + +#ifndef NOSVG + if (img == NULL) + { + /* (Try SVG next) */ + safe_snprintf(fname, sizeof(fname), "%s/%s-back", + dirname[d_places[num_files]], + d_names[num_files]); + img = load_starter_helper(fname, "svg", &load_svg); + } +#endif + + if (img == NULL) + { + /* (Try PNG next) */ + safe_snprintf(fname, sizeof(fname), "%s/%s-back", + dirname[d_places[num_files]], + d_names[num_files]); + img = load_starter_helper(fname, "png", &IMG_Load); + } + } + + if (img == NULL) + { + /* Didn't load a starter background (or didn't try!), + try loading the actual image... */ + + safe_snprintf(fname, sizeof(fname), "%s/%s", + dirname[d_places[num_files]], f->d_name); + debug(fname); + img = myIMG_Load(fname); + } + + + show_progress_bar(screen); + + if (img == NULL) + { + fprintf(stderr, + "\nWarning: I can't open one of the saved files!\n" + "%s\n" + "The Simple DirectMedia Layer error that " + "occurred was:\n" "%s\n\n", fname, SDL_GetError()); + + free(d_names[num_files]); + free(d_exts[num_files]); + } + else + { + /* Turn it into a thumbnail: */ + + img1 = SDL_DisplayFormatAlpha(img); + img2 = thumbnail2(img1, THUMB_W - 20, THUMB_H - 20, 0, 0); + SDL_FreeSurface(img1); + + show_progress_bar(screen); + + thumbs[num_files] = SDL_DisplayFormat(img2); + SDL_FreeSurface(img2); + if (thumbs[num_files] == NULL) + { + fprintf(stderr, + "\nError: Couldn't create a thumbnail of saved image!\n" + "%s\n", fname); + } + + SDL_FreeSurface(img); + + show_progress_bar(screen); + + + /* Let's save this thumbnail, so we don't have to + create it again next time 'Open' is called: */ + /* if (d_places[num_files] == PLACE_SAVED_DIR) *//* <-- FIXME: This test should probably go...? -bjk 2009.10.15 */ + + if (d_places[num_files] == PLACE_PERSONAL_STARTERS_DIR || /* We must check to not try to write to system wide dirs Pere 2010.3.25 */ + d_places[num_files] == PLACE_PERSONAL_TEMPLATES_DIR) + { + debug("Saving thumbnail for this one!"); + + safe_snprintf(fname, sizeof(fname), "%s/.thumbs/%s-t.png", + dirname[d_places[num_files]], + d_names[num_files]); + + if (!make_directory + (DIR_SAVE, "starters", + "Can't create user data directory (for starters) (E012)") + || !make_directory(DIR_SAVE, "templates", + "Can't create user data directory (for templates) (E013)") + || !make_directory(DIR_SAVE, "starters/.thumbs", + "Can't create user data directory (for starters) (E014)") + || !make_directory(DIR_SAVE, "templates/.thumbs", + "Can't create user data directory (for templates) (E015)")) + fprintf(stderr, "Cannot save any pictures! SORRY!\n\n"); + else + { + fi = fopen(fname, "wb"); + if (fi == NULL) + { + fprintf(stderr, + "\nError: Couldn't save thumbnail of " + "saved image!\n" + "%s\n" + "The error that occurred was:\n" "%s\n\n", + fname, strerror(errno)); + } + else + { + do_png_save(fi, fname, thumbs[num_files], 0); + } + } + + show_progress_bar(screen); + } + + + num_files++; + } + } + } } + } + else + { + /* It was a thumbnail file ("...-t.png") or immutable scene starter's + overlay layer ("...-front.png") */ + } } + } /* Throw the color palette at the end (alternative option): */ if (new_colors_last) - { - first_color = num_files; - num_files = do_new_dialog_add_colors(thumbs, num_files, d_places, d_names, d_exts, &white_in_palette); - } + { + first_color = num_files; + num_files = + do_new_dialog_add_colors(thumbs, num_files, d_places, d_names, d_exts, + &white_in_palette); + } #ifdef DEBUG printf("%d files and colors were found!\n", num_files); - printf("first_color = %d\nfirst_starter = %d\nfirst_template = %d\nnum_files = %d\n\n", first_color, first_starter, - first_template, num_files); + printf + ("first_color = %d\nfirst_starter = %d\nfirst_template = %d\nnum_files = %d\n\n", + first_color, first_starter, first_template, num_files); #endif @@ -21686,671 +23044,725 @@ static int do_new_dialog(void) do + { + /* Update screen: */ + + if (update_list) { - /* Update screen: */ + /* Erase screen: */ - if (update_list) + dest.x = r_ttools.w; + dest.y = 0; + dest.w = WINDOW_WIDTH - r_ttoolopt.w - r_ttools.w; + dest.h = button_h * buttons_tall + r_ttools.h; + + SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 255, 255, 255)); + + + /* Draw icons: */ + + for (i = cur; i < cur + 16 && i < num_files; i++) + { + /* Draw cursor: */ + + dest.x = THUMB_W * ((i - cur) % 4) + r_ttools.w; + dest.y = THUMB_H * ((i - cur) / 4) + img_scroll_up->h; + + if (d_places[i] == PLACE_SAVED_DIR) { - /* Erase screen: */ - - dest.x = r_ttools.w; - dest.y = 0; - dest.w = WINDOW_WIDTH - r_ttoolopt.w - r_ttools.w; - dest.h = button_h * buttons_tall + r_ttools.h; - - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 255, 255, 255)); - - - /* Draw icons: */ - - for (i = cur; i < cur + 16 && i < num_files; i++) - { - /* Draw cursor: */ - - dest.x = THUMB_W * ((i - cur) % 4) + r_ttools.w; - dest.y = THUMB_H * ((i - cur) / 4) + img_scroll_up->h; - - if (d_places[i] == PLACE_SAVED_DIR) - { - if (i == which) - { - SDL_BlitSurface(img_cursor_down, NULL, screen, &dest); - debug(d_names[i]); - } - else - SDL_BlitSurface(img_cursor_up, NULL, screen, &dest); - } - else - { - if (i == which) - { - SDL_BlitSurface(img_cursor_starter_down, NULL, screen, &dest); - debug(d_names[i]); - } - else - SDL_BlitSurface(img_cursor_starter_up, NULL, screen, &dest); - } - - - - dest.x = THUMB_W * ((i - cur) % 4) + r_ttools.w + 10 + (THUMB_W - 20 - thumbs[i]->w) / 2; - dest.y = THUMB_H * ((i - cur) / 4) + img_scroll_up->h + 10 + (THUMB_H - 20 - thumbs[i]->h) / 2; - - if (thumbs[i] != NULL) - SDL_BlitSurface(thumbs[i], NULL, screen, &dest); - } - - - /* Draw arrows: */ - - dest.x = (WINDOW_WIDTH - img_scroll_up->w) / 2; - dest.y = 0; - - if (cur > 0) - SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); + if (i == which) + { + SDL_BlitSurface(img_cursor_down, NULL, screen, &dest); + debug(d_names[i]); + } else - SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); - - dest.x = (WINDOW_WIDTH - img_scroll_up->w) / 2; - dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; - - if (cur < num_files - 16) - SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + SDL_BlitSurface(img_cursor_up, NULL, screen, &dest); + } + else + { + if (i == which) + { + SDL_BlitSurface(img_cursor_starter_down, NULL, screen, &dest); + debug(d_names[i]); + } else - SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); - - - /* "Open" button: */ - - dest.x = r_ttools.w; - dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; - SDL_BlitSurface(img_open, NULL, screen, &dest); - - dest.x = r_ttools.w + (button_w - img_openlabels_open->w) / 2; - dest.y = (button_h * buttons_tall + r_ttools.h) - img_openlabels_open->h; - SDL_BlitSurface(img_openlabels_open, NULL, screen, &dest); - - - /* "Back" button: */ - - dest.x = WINDOW_WIDTH - r_ttoolopt.w - button_w; - dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; - SDL_BlitSurface(img_back, NULL, screen, &dest); - - dest.x = WINDOW_WIDTH - r_ttoolopt.w - button_w + (button_w - img_openlabels_back->w) / 2; - dest.y = (button_h * buttons_tall + r_ttools.h) - img_openlabels_back->h; - SDL_BlitSurface(img_openlabels_back, NULL, screen, &dest); - - - SDL_Flip(screen); - - update_list = 0; + SDL_BlitSurface(img_cursor_starter_up, NULL, screen, &dest); } - /* Was a call to SDL_WaitEvent(&event); before, - changed to this while loop in order to get joystick working */ - while (SDL_PollEvent(&event)) - { - if (event.type == SDL_QUIT) - { - done = 1; - - /* FIXME: Handle SDL_Quit better */ - } - else if (event.type == SDL_WINDOWEVENT) - { - handle_active(&event); - } - else if (event.type == SDL_KEYUP) - { - key = event.key.keysym.sym; - - handle_keymouse(key, SDL_KEYUP, 24, NULL, NULL); - } - else if (event.type == SDL_KEYDOWN) - { - key = event.key.keysym.sym; - - handle_keymouse(key, SDL_KEYDOWN, 24, NULL, NULL); - - /* Moved from LEFT RIGHT UP DOWN to F11 F12 F8 F7 */ - - if (key == SDLK_F11) - { - if (which > 0) - { - which--; - - if (which < cur) - cur = cur - 4; - - update_list = 1; - } - } - else if (key == SDLK_F12) - { - if (which < num_files - 1) - { - which++; - - if (which >= cur + 16) - cur = cur + 4; - - update_list = 1; - } - } - else if (key == SDLK_F8) - { - if (which >= 0) - { - which = which - 4; - - if (which < 0) - which = 0; - - if (which < cur) - cur = cur - 4; - - update_list = 1; - } - } - else if (key == SDLK_F7) - { - if (which < num_files) - { - which = which + 4; - - if (which >= num_files) - which = num_files - 1; - - if (which >= cur + 16) - cur = cur + 4; - - update_list = 1; - } - } - else if (key == SDLK_RETURN) - { - /* Open */ - - done = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); - } - else if (key == SDLK_ESCAPE || key == SDLK_AC_BACK) - { - /* Go back: */ - - which = -1; - done = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - } - } - else if ((event.type == SDL_MOUSEBUTTONDOWN && valid_click(event.button.button)) || - event.type == TP_SDL_MOUSEBUTTONSCROLL) - { - if (event.button.x >= r_ttools.w && event.button.x < WINDOW_WIDTH - r_ttoolopt.w && - event.button.y >= img_scroll_up->h && event.button.y < (button_h * buttons_tall + r_ttools.h - button_h)) - { - /* Picked an icon! */ - - which = ((event.button.x - r_ttools.w) / (THUMB_W) + (((event.button.y - img_scroll_up->h) / THUMB_H) * 4)) + cur; - - if (which < num_files) - { - playsound(screen, 1, SND_BLEEP, 1, event.button.x, SNDDIST_NEAR); - update_list = 1; - if (which == last_click_which && - SDL_GetTicks() < last_click_time + 1000 && event.button.button == last_click_button) - { - /* Double-click! */ + dest.x = + THUMB_W * ((i - cur) % 4) + r_ttools.w + 10 + (THUMB_W - 20 - + thumbs[i]->w) / 2; + dest.y = + THUMB_H * ((i - cur) / 4) + img_scroll_up->h + 10 + (THUMB_H - 20 - + thumbs[i]->h) / + 2; - done = 1; - } + if (thumbs[i] != NULL) + SDL_BlitSurface(thumbs[i], NULL, screen, &dest); + } - last_click_which = which; - last_click_time = SDL_GetTicks(); - last_click_button = event.button.button; - } - } - else if (event.button.x >= (WINDOW_WIDTH - img_scroll_up->w) / 2 && - event.button.x <= (WINDOW_WIDTH + img_scroll_up->w) / 2) - { - if (event.button.y < img_scroll_up->h || - (event.button.y >= (button_h * buttons_tall + r_ttools.h - button_h) && - event.button.y < (button_h * buttons_tall + r_ttools.h - img_scroll_up->h)) - ) - { - /* Up or Down scroll button in New dialog: */ - if (event.button.y < img_scroll_up->h) - { - /* Up scroll button: */ + /* Draw arrows: */ - if (cur > 0) - { - cur = cur - 4; - update_list = 1; - playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, SNDDIST_NEAR); + dest.x = (WINDOW_WIDTH - img_scroll_up->w) / 2; + dest.y = 0; - if (cur == 0) - do_setcursor(cursor_arrow); - } + if (cur > 0) + SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); + else + SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); - if (which >= cur + 16) - which = which - 4; - } - else if (event.button.y >= (button_h * buttons_tall + r_ttools.h - button_h) && - event.button.y < (button_h * buttons_tall + r_ttools.h - img_scroll_up->h)) - { - /* Down scroll button: */ + dest.x = (WINDOW_WIDTH - img_scroll_up->w) / 2; + dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; - if (cur < num_files - 16) - { - cur = cur + 4; - update_list = 1; - playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, SNDDIST_NEAR); + if (cur < num_files - 16) + SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + else + SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); - if (cur >= num_files - 16) - do_setcursor(cursor_arrow); - } - if (which < cur) - which = which + 4; - } + /* "Open" button: */ - if (scrolltimer_dialog != TIMERID_NONE) - { - SDL_RemoveTimer(scrolltimer_dialog); - scrolltimer_dialog = TIMERID_NONE; - } + dest.x = r_ttools.w; + dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; + SDL_BlitSurface(img_open, NULL, screen, &dest); - if (!scrolling_dialog && event.type == SDL_MOUSEBUTTONDOWN) - { - DEBUG_PRINTF("Starting scrolling\n"); - memcpy(&scrolltimer_dialog_event, &event, sizeof(SDL_Event)); - scrolltimer_dialog_event.type = TP_SDL_MOUSEBUTTONSCROLL; + dest.x = r_ttools.w + (button_w - img_openlabels_open->w) / 2; + dest.y = + (button_h * buttons_tall + r_ttools.h) - img_openlabels_open->h; + SDL_BlitSurface(img_openlabels_open, NULL, screen, &dest); - /* - * We enable the timer subsystem only when needed (e.g., to use SDL_AddTimer() needed - * for scrolling) then disable it immediately after (e.g., after the timer has fired or - * after SDL_RemoveTimer()) because enabling the timer subsystem in SDL1 has a high - * energy impact on the Mac. - */ - scrolling_dialog = 1; - SDL_InitSubSystem(SDL_INIT_TIMER); - scrolltimer_dialog = - SDL_AddTimer(REPEAT_SPEED, scrolltimer_dialog_callback, (void *)&scrolltimer_dialog_event); - } - else - { - DEBUG_PRINTF("Continuing scrolling\n"); - scrolltimer_dialog = - SDL_AddTimer(REPEAT_SPEED / 3, scrolltimer_dialog_callback, (void *)&scrolltimer_dialog_event); - } - } - } - else if (event.button.x >= r_ttools.w && event.button.x < r_ttools.w + button_w && - event.button.y >= (button_h * buttons_tall + r_ttools.h) - button_h && - event.button.y < (button_h * buttons_tall + r_ttools.h)) - { - /* Open */ + /* "Back" button: */ - done = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); - } - else if (event.button.x >= (WINDOW_WIDTH - r_ttoolopt.w - button_w) && - event.button.x < (WINDOW_WIDTH - r_ttoolopt.w) && - event.button.y >= (button_h * buttons_tall + r_ttools.h) - button_h && - event.button.y < (button_h * buttons_tall + r_ttools.h)) - { - /* Back */ + dest.x = WINDOW_WIDTH - r_ttoolopt.w - button_w; + dest.y = (button_h * buttons_tall + r_ttools.h) - button_h; + SDL_BlitSurface(img_back, NULL, screen, &dest); - which = -1; - done = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - } -#ifdef __ANDROID__ - start_motion_convert(event); -#endif - } - else if (event.type == SDL_MOUSEWHEEL && wheely) - { - /* Scroll wheel! */ + dest.x = + WINDOW_WIDTH - r_ttoolopt.w - button_w + (button_w - + img_openlabels_back->w) / 2; + dest.y = + (button_h * buttons_tall + r_ttools.h) - img_openlabels_back->h; + SDL_BlitSurface(img_openlabels_back, NULL, screen, &dest); - if (event.wheel.y > 0 && cur > 0) - { - cur = cur - 4; - update_list = 1; - playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, SNDDIST_NEAR); - if (cur == 0) - do_setcursor(cursor_arrow); + SDL_Flip(screen); - if (which >= cur + 16) - which = which - 4; - } - else if (event.wheel.y < 0 && cur < num_files - 16) - { - cur = cur + 4; - update_list = 1; - playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, SNDDIST_NEAR); - - if (cur >= num_files - 16) - do_setcursor(cursor_arrow); - - if (which < cur) - which = which + 4; - } - } - else if (event.type == SDL_MOUSEMOTION) - { - /* Deal with mouse pointer shape! */ - - if (event.button.y < img_scroll_up->h && - event.button.x >= (WINDOW_WIDTH - img_scroll_up->w) / 2 && - event.button.x <= (WINDOW_WIDTH + img_scroll_up->w) / 2 && cur > 0) - { - /* Scroll up button: */ - - do_setcursor(cursor_up); - } - else if (event.button.y >= (button_h * buttons_tall + r_ttools.h - button_h) && - event.button.y < (button_h * buttons_tall + r_ttools.h - img_scroll_up->h) && - event.button.x >= (WINDOW_WIDTH - img_scroll_up->w) / 2 && - event.button.x <= (WINDOW_WIDTH + img_scroll_up->w) / 2 && cur < num_files - 16) - { - /* Scroll down button: */ - - do_setcursor(cursor_down); - } - else if (((event.button.x >= r_ttools.w && event.button.x < r_ttools.w + button_w) || - (event.button.x >= (WINDOW_WIDTH - r_ttoolopt.w - button_w) && - event.button.x < (WINDOW_WIDTH - r_ttoolopt.w) && - d_places[which] != PLACE_STARTERS_DIR && - d_places[which] != PLACE_PERSONAL_STARTERS_DIR)) && - event.button.y >= (button_h * buttons_tall + r_ttools.h) - button_h && - event.button.y < (button_h * buttons_tall + r_ttools.h)) - { - /* One of the command buttons: */ - - do_setcursor(cursor_hand); - } - else if (event.button.x >= r_ttools.w && event.button.x < WINDOW_WIDTH - r_ttoolopt.w && - event.button.y > img_scroll_up->h && - event.button.y < (button_h * buttons_tall + r_ttools.h) - button_h && - ((((event.button.x - r_ttools.w) / (THUMB_W) + - (((event.button.y - img_scroll_up->h) / THUMB_H) * 4)) + cur) < num_files)) - { - /* One of the thumbnails: */ - - do_setcursor(cursor_hand); - } - else - { - /* Unclickable... */ - - do_setcursor(cursor_arrow); - } - -#ifdef __ANDROID__ - convert_motion_to_wheel(event); -#endif - - oldpos_x = event.button.x; - oldpos_y = event.button.y; - } - - else if (event.type == SDL_MOUSEBUTTONUP) - { -#ifdef __ANDROID__ - stop_motion_convert(event); -#endif - - if (scrolling_dialog) - { - if (scrolltimer_dialog != TIMERID_NONE) - { - SDL_RemoveTimer(scrolltimer_dialog); - scrolltimer_dialog = TIMERID_NONE; - } - scrolling_dialog = 0; - SDL_QuitSubSystem(SDL_INIT_TIMER); - DEBUG_PRINTF("Killing dialog scrolling\n"); - } - } - - else if (event.type == SDL_JOYAXISMOTION) - handle_joyaxismotion(event, &motioner, &val_x, &val_y); - - else if (event.type == SDL_JOYHATMOTION) - handle_joyhatmotion(event, oldpos_x, oldpos_y, &valhat_x, &valhat_y, &hatmotioner, &old_hat_ticks); - - else if (event.type == SDL_JOYBALLMOTION) - handle_joyballmotion(event, oldpos_x, oldpos_y); - - else if (event.type == SDL_JOYBUTTONDOWN || event.type == SDL_JOYBUTTONUP) - handle_joybuttonupdown(event, oldpos_x, oldpos_y); - } - - if (motioner | hatmotioner) - handle_motioners(oldpos_x, oldpos_y, motioner, hatmotioner, old_hat_ticks, val_x, val_y, valhat_x, valhat_y); - - SDL_Delay(10); + update_list = 0; } + + /* Was a call to SDL_WaitEvent(&event); before, + changed to this while loop in order to get joystick working */ + while (SDL_PollEvent(&event)) + { + if (event.type == SDL_QUIT) + { + done = 1; + + /* FIXME: Handle SDL_Quit better */ + } + else if (event.type == SDL_WINDOWEVENT) + { + handle_active(&event); + } + else if (event.type == SDL_KEYUP) + { + key = event.key.keysym.sym; + + handle_keymouse(key, SDL_KEYUP, 24, NULL, NULL); + } + else if (event.type == SDL_KEYDOWN) + { + key = event.key.keysym.sym; + + handle_keymouse(key, SDL_KEYDOWN, 24, NULL, NULL); + + /* Moved from LEFT RIGHT UP DOWN to F11 F12 F8 F7 */ + + if (key == SDLK_F11) + { + if (which > 0) + { + which--; + + if (which < cur) + cur = cur - 4; + + update_list = 1; + } + } + else if (key == SDLK_F12) + { + if (which < num_files - 1) + { + which++; + + if (which >= cur + 16) + cur = cur + 4; + + update_list = 1; + } + } + else if (key == SDLK_F8) + { + if (which >= 0) + { + which = which - 4; + + if (which < 0) + which = 0; + + if (which < cur) + cur = cur - 4; + + update_list = 1; + } + } + else if (key == SDLK_F7) + { + if (which < num_files) + { + which = which + 4; + + if (which >= num_files) + which = num_files - 1; + + if (which >= cur + 16) + cur = cur + 4; + + update_list = 1; + } + } + else if (key == SDLK_RETURN) + { + /* Open */ + + done = 1; + playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); + } + else if (key == SDLK_ESCAPE || key == SDLK_AC_BACK) + { + /* Go back: */ + + which = -1; + done = 1; + playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); + } + } + else + if ((event.type == SDL_MOUSEBUTTONDOWN + && valid_click(event.button.button)) + || event.type == TP_SDL_MOUSEBUTTONSCROLL) + { + if (event.button.x >= r_ttools.w + && event.button.x < WINDOW_WIDTH - r_ttoolopt.w + && event.button.y >= img_scroll_up->h + && event.button.y < + (button_h * buttons_tall + r_ttools.h - button_h)) + { + /* Picked an icon! */ + + which = + ((event.button.x - r_ttools.w) / (THUMB_W) + + (((event.button.y - img_scroll_up->h) / THUMB_H) * 4)) + cur; + + if (which < num_files) + { + playsound(screen, 1, SND_BLEEP, 1, event.button.x, SNDDIST_NEAR); + update_list = 1; + + + if (which == last_click_which && + SDL_GetTicks() < last_click_time + 1000 + && event.button.button == last_click_button) + { + /* Double-click! */ + + done = 1; + } + + last_click_which = which; + last_click_time = SDL_GetTicks(); + last_click_button = event.button.button; + } + } + else if (event.button.x >= (WINDOW_WIDTH - img_scroll_up->w) / 2 && + event.button.x <= (WINDOW_WIDTH + img_scroll_up->w) / 2) + { + if (event.button.y < img_scroll_up->h || + (event.button.y >= + (button_h * buttons_tall + r_ttools.h - button_h) + && event.button.y < + (button_h * buttons_tall + r_ttools.h - img_scroll_up->h))) + { + /* Up or Down scroll button in New dialog: */ + + if (event.button.y < img_scroll_up->h) + { + /* Up scroll button: */ + + if (cur > 0) + { + cur = cur - 4; + update_list = 1; + playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, + SNDDIST_NEAR); + + if (cur == 0) + do_setcursor(cursor_arrow); + } + + if (which >= cur + 16) + which = which - 4; + } + else if (event.button.y >= + (button_h * buttons_tall + r_ttools.h - button_h) + && event.button.y < + (button_h * buttons_tall + r_ttools.h - + img_scroll_up->h)) + { + /* Down scroll button: */ + + if (cur < num_files - 16) + { + cur = cur + 4; + update_list = 1; + playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, + SNDDIST_NEAR); + + if (cur >= num_files - 16) + do_setcursor(cursor_arrow); + } + + if (which < cur) + which = which + 4; + } + + if (scrolltimer_dialog != TIMERID_NONE) + { + SDL_RemoveTimer(scrolltimer_dialog); + scrolltimer_dialog = TIMERID_NONE; + } + + if (!scrolling_dialog && event.type == SDL_MOUSEBUTTONDOWN) + { + DEBUG_PRINTF("Starting scrolling\n"); + memcpy(&scrolltimer_dialog_event, &event, sizeof(SDL_Event)); + scrolltimer_dialog_event.type = TP_SDL_MOUSEBUTTONSCROLL; + + /* + * We enable the timer subsystem only when needed (e.g., to use SDL_AddTimer() needed + * for scrolling) then disable it immediately after (e.g., after the timer has fired or + * after SDL_RemoveTimer()) because enabling the timer subsystem in SDL1 has a high + * energy impact on the Mac. + */ + + scrolling_dialog = 1; + SDL_InitSubSystem(SDL_INIT_TIMER); + scrolltimer_dialog = + SDL_AddTimer(REPEAT_SPEED, scrolltimer_dialog_callback, + (void *) &scrolltimer_dialog_event); + } + else + { + DEBUG_PRINTF("Continuing scrolling\n"); + scrolltimer_dialog = + SDL_AddTimer(REPEAT_SPEED / 3, scrolltimer_dialog_callback, + (void *) &scrolltimer_dialog_event); + } + } + } + else if (event.button.x >= r_ttools.w + && event.button.x < r_ttools.w + button_w + && event.button.y >= + (button_h * buttons_tall + r_ttools.h) - button_h + && event.button.y < (button_h * buttons_tall + r_ttools.h)) + { + /* Open */ + + done = 1; + playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); + } + else if (event.button.x >= (WINDOW_WIDTH - r_ttoolopt.w - button_w) && + event.button.x < (WINDOW_WIDTH - r_ttoolopt.w) && + event.button.y >= + (button_h * buttons_tall + r_ttools.h) - button_h + && event.button.y < (button_h * buttons_tall + r_ttools.h)) + { + /* Back */ + + which = -1; + done = 1; + playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); + } +#ifdef __ANDROID__ + start_motion_convert(event); +#endif + } + else if (event.type == SDL_MOUSEWHEEL && wheely) + { + /* Scroll wheel! */ + + if (event.wheel.y > 0 && cur > 0) + { + cur = cur - 4; + update_list = 1; + playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, SNDDIST_NEAR); + + if (cur == 0) + do_setcursor(cursor_arrow); + + if (which >= cur + 16) + which = which - 4; + } + else if (event.wheel.y < 0 && cur < num_files - 16) + { + cur = cur + 4; + update_list = 1; + playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, SNDDIST_NEAR); + + if (cur >= num_files - 16) + do_setcursor(cursor_arrow); + + if (which < cur) + which = which + 4; + } + } + else if (event.type == SDL_MOUSEMOTION) + { + /* Deal with mouse pointer shape! */ + + if (event.button.y < img_scroll_up->h && + event.button.x >= (WINDOW_WIDTH - img_scroll_up->w) / 2 && + event.button.x <= (WINDOW_WIDTH + img_scroll_up->w) / 2 + && cur > 0) + { + /* Scroll up button: */ + + do_setcursor(cursor_up); + } + else if (event.button.y >= + (button_h * buttons_tall + r_ttools.h - button_h) + && event.button.y < + (button_h * buttons_tall + r_ttools.h - img_scroll_up->h) + && event.button.x >= (WINDOW_WIDTH - img_scroll_up->w) / 2 + && event.button.x <= (WINDOW_WIDTH + img_scroll_up->w) / 2 + && cur < num_files - 16) + { + /* Scroll down button: */ + + do_setcursor(cursor_down); + } + else + if (((event.button.x >= r_ttools.w + && event.button.x < r_ttools.w + button_w) + || (event.button.x >= (WINDOW_WIDTH - r_ttoolopt.w - button_w) + && event.button.x < (WINDOW_WIDTH - r_ttoolopt.w) + && d_places[which] != PLACE_STARTERS_DIR + && d_places[which] != PLACE_PERSONAL_STARTERS_DIR)) + && event.button.y >= + (button_h * buttons_tall + r_ttools.h) - button_h + && event.button.y < (button_h * buttons_tall + r_ttools.h)) + { + /* One of the command buttons: */ + + do_setcursor(cursor_hand); + } + else if (event.button.x >= r_ttools.w + && event.button.x < WINDOW_WIDTH - r_ttoolopt.w + && event.button.y > img_scroll_up->h + && event.button.y < + (button_h * buttons_tall + r_ttools.h) - button_h + && + ((((event.button.x - r_ttools.w) / (THUMB_W) + + (((event.button.y - img_scroll_up->h) / THUMB_H) * 4)) + + cur) < num_files)) + { + /* One of the thumbnails: */ + + do_setcursor(cursor_hand); + } + else + { + /* Unclickable... */ + + do_setcursor(cursor_arrow); + } + +#ifdef __ANDROID__ + convert_motion_to_wheel(event); +#endif + + oldpos_x = event.button.x; + oldpos_y = event.button.y; + } + + else if (event.type == SDL_MOUSEBUTTONUP) + { +#ifdef __ANDROID__ + stop_motion_convert(event); +#endif + + if (scrolling_dialog) + { + if (scrolltimer_dialog != TIMERID_NONE) + { + SDL_RemoveTimer(scrolltimer_dialog); + scrolltimer_dialog = TIMERID_NONE; + } + scrolling_dialog = 0; + SDL_QuitSubSystem(SDL_INIT_TIMER); + DEBUG_PRINTF("Killing dialog scrolling\n"); + } + } + + else if (event.type == SDL_JOYAXISMOTION) + handle_joyaxismotion(event, &motioner, &val_x, &val_y); + + else if (event.type == SDL_JOYHATMOTION) + handle_joyhatmotion(event, oldpos_x, oldpos_y, &valhat_x, &valhat_y, + &hatmotioner, &old_hat_ticks); + + else if (event.type == SDL_JOYBALLMOTION) + handle_joyballmotion(event, oldpos_x, oldpos_y); + + else if (event.type == SDL_JOYBUTTONDOWN + || event.type == SDL_JOYBUTTONUP) + handle_joybuttonupdown(event, oldpos_x, oldpos_y); + } + + if (motioner | hatmotioner) + handle_motioners(oldpos_x, oldpos_y, motioner, hatmotioner, + old_hat_ticks, val_x, val_y, valhat_x, valhat_y); + + SDL_Delay(10); + } while (!done); /* Load the chosen starter, or start with a blank solid color: */ if (which != -1) + { + /* Save old one first? */ + + if (!been_saved && !disable_save) { - /* Save old one first? */ - - if (!been_saved && !disable_save) - { - if (do_prompt_image_snd(PROMPT_OPEN_SAVE_TXT, - PROMPT_OPEN_SAVE_YES, - PROMPT_OPEN_SAVE_NO, - img_tools[TOOL_SAVE], NULL, NULL, SND_AREYOUSURE, screen->w / 2, screen->h / 2)) - { - do_save(TOOL_NEW, 1, 0); - } - } - - /* Clear label surface */ - - SDL_FillRect(label, NULL, SDL_MapRGBA(label->format, 0, 0, 0, 0)); - - /* Clear all info related to label surface */ - - delete_label_list(&start_label_node); - start_label_node = current_label_node = first_label_node_in_redo_stack = highlighted_label_node = - label_node_to_edit = NULL; - have_to_rec_label_node = FALSE; - - if (which >= first_starter && (first_template == -1 || which < first_template) - && (!new_colors_last || which < first_color)) - { - /* Load a starter: */ - - /* Figure out filename: */ - - safe_snprintf(fname, sizeof(fname), "%s/%s%s", dirname[d_places[which]], d_names[which], d_exts[which]); - - img = myIMG_Load(fname); - - if (img == NULL) - { - fprintf(stderr, - "\nWarning: Couldn't load the saved image! (3)\n" - "%s\n" - "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", fname, SDL_GetError()); - - do_prompt(PROMPT_OPEN_UNOPENABLE_TXT, PROMPT_OPEN_UNOPENABLE_YES, "", 0, 0); - } - else - { - free_surface(&img_starter); - free_surface(&img_starter_bkgd); - starter_mirrored = 0; - starter_flipped = 0; - starter_personal = 0; - starter_modified = 0; - - autoscale_copy_smear_free(img, canvas, SDL_BlitSurface); - - cur_undo = 0; - oldest_undo = 0; - newest_undo = 0; - - /* Immutable 'starter' image; - we'll need to save a new image when saving...: */ - - been_saved = 1; - - file_id[0] = '\0'; - safe_strncpy(starter_id, d_names[which], sizeof(starter_id)); - template_id[0] = '\0'; - - if (d_places[which] == PLACE_PERSONAL_STARTERS_DIR) - starter_personal = 1; - else - starter_personal = 0; - - load_starter(starter_id); - - canvas_color_r = 255; - canvas_color_g = 255; - canvas_color_b = 255; - - SDL_FillRect(canvas, NULL, SDL_MapRGB(canvas->format, 255, 255, 255)); - SDL_BlitSurface(img_starter_bkgd, NULL, canvas, NULL); - SDL_BlitSurface(img_starter, NULL, canvas, NULL); - } - } - else if (first_template != -1 && which >= first_template && (!new_colors_last || which < first_color)) - { - /* Load a template: */ - - /* Figure out filename: */ - - safe_snprintf(fname, sizeof(fname), "%s/%s%s", dirname[d_places[which]], d_names[which], d_exts[which]); - img = myIMG_Load(fname); - - if (img == NULL) - { - fprintf(stderr, - "\nWarning: Couldn't load the saved image! (4)\n" - "%s\n" - "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", fname, SDL_GetError()); - - do_prompt(PROMPT_OPEN_UNOPENABLE_TXT, PROMPT_OPEN_UNOPENABLE_YES, "", 0, 0); - } - else - { - free_surface(&img_starter); - free_surface(&img_starter_bkgd); - template_personal = 0; - - autoscale_copy_smear_free(img, canvas, SDL_BlitSurface); - - cur_undo = 0; - oldest_undo = 0; - newest_undo = 0; - - /* Immutable 'template' image; - we'll need to save a new image when saving...: */ - - been_saved = 1; - - file_id[0] = '\0'; - safe_strncpy(template_id, d_names[which], sizeof(template_id)); - starter_id[0] = '\0'; - - if (d_places[which] == PLACE_PERSONAL_TEMPLATES_DIR) - template_personal = 1; - else - template_personal = 0; - - load_template(template_id); - - canvas_color_r = 255; - canvas_color_g = 255; - canvas_color_b = 255; - - SDL_FillRect(canvas, NULL, SDL_MapRGB(canvas->format, 255, 255, 255)); - SDL_BlitSurface(img_starter_bkgd, NULL, canvas, NULL); - } - } - else - { - /* A color! */ - - free_surface(&img_starter); - free_surface(&img_starter_bkgd); - starter_mirrored = 0; - starter_flipped = 0; - starter_personal = 0; - starter_modified = 0; - - which = which - first_color; - - /* Launch color picker if they chose that: */ - - if (which == COLOR_PICKER) - { - if (do_color_picker() == 0) - return (0); - } - else if (which == COLOR_MIXER) - { - if (do_color_mix() == 0) - return (0); - } - - /* FIXME: Don't do anything and go back to Open dialog if they - hit BACK in color picker! */ - - if (which == 0) /* White */ - { - canvas_color_r = canvas_color_g = canvas_color_b = 255; - } - else if (which <= white_in_palette) /* One of the colors before white in the pallete */ - { - canvas_color_r = color_hexes[which - 1][0]; - canvas_color_g = color_hexes[which - 1][1]; - canvas_color_b = color_hexes[which - 1][2]; - } - else - { - canvas_color_r = color_hexes[which][0]; - canvas_color_g = color_hexes[which][1]; - canvas_color_b = color_hexes[which][2]; - } - - SDL_FillRect(canvas, NULL, SDL_MapRGB(canvas->format, canvas_color_r, canvas_color_g, canvas_color_b)); - - cur_undo = 0; - oldest_undo = 0; - newest_undo = 0; - - been_saved = 1; - reset_avail_tools(); - - tool_avail_bak[TOOL_UNDO] = 0; - tool_avail_bak[TOOL_REDO] = 0; - - file_id[0] = '\0'; - starter_id[0] = '\0'; - - playsound(screen, 1, SND_HARP, 1, SNDPOS_CENTER, SNDDIST_NEAR); - } + if (do_prompt_image_snd(PROMPT_OPEN_SAVE_TXT, + PROMPT_OPEN_SAVE_YES, + PROMPT_OPEN_SAVE_NO, + img_tools[TOOL_SAVE], NULL, NULL, + SND_AREYOUSURE, screen->w / 2, screen->h / 2)) + { + do_save(TOOL_NEW, 1, 0); + } } - update_canvas(0, 0, WINDOW_WIDTH - r_ttoolopt.w - r_ttools.w, button_h * buttons_tall + r_ttools.h); + /* Clear label surface */ + + SDL_FillRect(label, NULL, SDL_MapRGBA(label->format, 0, 0, 0, 0)); + + /* Clear all info related to label surface */ + + delete_label_list(&start_label_node); + start_label_node = current_label_node = first_label_node_in_redo_stack = + highlighted_label_node = label_node_to_edit = NULL; + have_to_rec_label_node = FALSE; + + if (which >= first_starter + && (first_template == -1 || which < first_template) + && (!new_colors_last || which < first_color)) + { + /* Load a starter: */ + + /* Figure out filename: */ + + safe_snprintf(fname, sizeof(fname), "%s/%s%s", dirname[d_places[which]], + d_names[which], d_exts[which]); + + img = myIMG_Load(fname); + + if (img == NULL) + { + fprintf(stderr, + "\nWarning: Couldn't load the saved image! (3)\n" + "%s\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", fname, SDL_GetError()); + + do_prompt(PROMPT_OPEN_UNOPENABLE_TXT, PROMPT_OPEN_UNOPENABLE_YES, "", + 0, 0); + } + else + { + free_surface(&img_starter); + free_surface(&img_starter_bkgd); + starter_mirrored = 0; + starter_flipped = 0; + starter_personal = 0; + starter_modified = 0; + + autoscale_copy_smear_free(img, canvas, SDL_BlitSurface); + + cur_undo = 0; + oldest_undo = 0; + newest_undo = 0; + + /* Immutable 'starter' image; + we'll need to save a new image when saving...: */ + + been_saved = 1; + + file_id[0] = '\0'; + safe_strncpy(starter_id, d_names[which], sizeof(starter_id)); + template_id[0] = '\0'; + + if (d_places[which] == PLACE_PERSONAL_STARTERS_DIR) + starter_personal = 1; + else + starter_personal = 0; + + load_starter(starter_id); + + canvas_color_r = 255; + canvas_color_g = 255; + canvas_color_b = 255; + + SDL_FillRect(canvas, NULL, SDL_MapRGB(canvas->format, 255, 255, 255)); + SDL_BlitSurface(img_starter_bkgd, NULL, canvas, NULL); + SDL_BlitSurface(img_starter, NULL, canvas, NULL); + } + } + else if (first_template != -1 && which >= first_template + && (!new_colors_last || which < first_color)) + { + /* Load a template: */ + + /* Figure out filename: */ + + safe_snprintf(fname, sizeof(fname), "%s/%s%s", dirname[d_places[which]], + d_names[which], d_exts[which]); + img = myIMG_Load(fname); + + if (img == NULL) + { + fprintf(stderr, + "\nWarning: Couldn't load the saved image! (4)\n" + "%s\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", fname, SDL_GetError()); + + do_prompt(PROMPT_OPEN_UNOPENABLE_TXT, PROMPT_OPEN_UNOPENABLE_YES, "", + 0, 0); + } + else + { + free_surface(&img_starter); + free_surface(&img_starter_bkgd); + template_personal = 0; + + autoscale_copy_smear_free(img, canvas, SDL_BlitSurface); + + cur_undo = 0; + oldest_undo = 0; + newest_undo = 0; + + /* Immutable 'template' image; + we'll need to save a new image when saving...: */ + + been_saved = 1; + + file_id[0] = '\0'; + safe_strncpy(template_id, d_names[which], sizeof(template_id)); + starter_id[0] = '\0'; + + if (d_places[which] == PLACE_PERSONAL_TEMPLATES_DIR) + template_personal = 1; + else + template_personal = 0; + + load_template(template_id); + + canvas_color_r = 255; + canvas_color_g = 255; + canvas_color_b = 255; + + SDL_FillRect(canvas, NULL, SDL_MapRGB(canvas->format, 255, 255, 255)); + SDL_BlitSurface(img_starter_bkgd, NULL, canvas, NULL); + } + } + else + { + /* A color! */ + + free_surface(&img_starter); + free_surface(&img_starter_bkgd); + starter_mirrored = 0; + starter_flipped = 0; + starter_personal = 0; + starter_modified = 0; + + which = which - first_color; + + /* Launch color picker if they chose that: */ + + if (which == COLOR_PICKER) + { + if (do_color_picker() == 0) + return (0); + } + else if (which == COLOR_MIXER) + { + if (do_color_mix() == 0) + return (0); + } + + /* FIXME: Don't do anything and go back to Open dialog if they + hit BACK in color picker! */ + + if (which == 0) /* White */ + { + canvas_color_r = canvas_color_g = canvas_color_b = 255; + } + else if (which <= white_in_palette) /* One of the colors before white in the pallete */ + { + canvas_color_r = color_hexes[which - 1][0]; + canvas_color_g = color_hexes[which - 1][1]; + canvas_color_b = color_hexes[which - 1][2]; + } + else + { + canvas_color_r = color_hexes[which][0]; + canvas_color_g = color_hexes[which][1]; + canvas_color_b = color_hexes[which][2]; + } + + SDL_FillRect(canvas, NULL, + SDL_MapRGB(canvas->format, canvas_color_r, canvas_color_g, + canvas_color_b)); + + cur_undo = 0; + oldest_undo = 0; + newest_undo = 0; + + been_saved = 1; + reset_avail_tools(); + + tool_avail_bak[TOOL_UNDO] = 0; + tool_avail_bak[TOOL_REDO] = 0; + + file_id[0] = '\0'; + starter_id[0] = '\0'; + + playsound(screen, 1, SND_HARP, 1, SNDPOS_CENTER, SNDDIST_NEAR); + } + } + + update_canvas(0, 0, WINDOW_WIDTH - r_ttoolopt.w - r_ttools.w, + button_h * buttons_tall + r_ttools.h); /* Clean up: */ @@ -22360,12 +23772,12 @@ static int do_new_dialog(void) free(thumbs); for (i = 0; i < num_files; i++) - { - if (d_names[i] != NULL) - free(d_names[i]); - if (d_exts[i] != NULL) - free(d_exts[i]); - } + { + if (d_names[i] != NULL) + free(d_names[i]); + if (d_exts[i] != NULL) + free(d_exts[i]); + } for (i = 0; i < NUM_PLACES_TO_LOOK; i++) if (dirname[i] != NULL) @@ -22382,7 +23794,8 @@ static int do_new_dialog(void) normally appears at the beginning (above Starts & Templates), but may be placed at the end with the "--newcolorslast" option. */ -static int do_new_dialog_add_colors(SDL_Surface * *thumbs, int num_files, int *d_places, char * *d_names, +static int do_new_dialog_add_colors(SDL_Surface * *thumbs, int num_files, + int *d_places, char * *d_names, char * *d_exts, int *white_in_palette) { int j; @@ -22390,68 +23803,72 @@ static int do_new_dialog_add_colors(SDL_Surface * *thumbs, int num_files, int *d Uint8 r, g, b; for (j = -1; j < NUM_COLORS; j++) + { + added = 0; + + if (j < COLOR_PICKER) { - added = 0; + if (j == -1 || /* (short circuit) */ + color_hexes[j][0] != 255 || /* Ignore white, we'll have already added it */ + color_hexes[j][1] != 255 || color_hexes[j][2] != 255) + { + /* Palette colors: */ - if (j < COLOR_PICKER) + thumbs[num_files] = SDL_CreateRGBSurface(screen->flags, + THUMB_W - 20, THUMB_H - 20, + screen->format->BitsPerPixel, + screen->format->Rmask, + screen->format->Gmask, + screen->format->Bmask, 0); + + if (thumbs[num_files] != NULL) { - if (j == -1 || /* (short circuit) */ - color_hexes[j][0] != 255 || /* Ignore white, we'll have already added it */ - color_hexes[j][1] != 255 || color_hexes[j][2] != 255) - { - /* Palette colors: */ - - thumbs[num_files] = SDL_CreateRGBSurface(screen->flags, - THUMB_W - 20, THUMB_H - 20, - screen->format->BitsPerPixel, - screen->format->Rmask, - screen->format->Gmask, screen->format->Bmask, 0); - - if (thumbs[num_files] != NULL) - { - if (j == -1) - { - r = g = b = 255; /* White */ - } - else - { - r = color_hexes[j][0]; - g = color_hexes[j][1]; - b = color_hexes[j][2]; - } - SDL_FillRect(thumbs[num_files], NULL, SDL_MapRGB(thumbs[num_files]->format, r, g, b)); - added = 1; - } - } + if (j == -1) + { + r = g = b = 255; /* White */ + } else - { - *white_in_palette = j; - } - } - else if (j == COLOR_PICKER) - { - /* Color picker: */ - - thumbs[num_files] = thumbnail(img_color_picker, THUMB_W - 20, THUMB_H - 20, 0); + { + r = color_hexes[j][0]; + g = color_hexes[j][1]; + b = color_hexes[j][2]; + } + SDL_FillRect(thumbs[num_files], NULL, + SDL_MapRGB(thumbs[num_files]->format, r, g, b)); added = 1; } - else if (j == COLOR_MIXER) - { - /* Color mixer: */ - - thumbs[num_files] = thumbnail(img_color_mix, THUMB_W - 20, THUMB_H - 20, 0); - added = 1; - } - - if (added) - { - d_places[num_files] = PLACE_COLOR_PALETTE; - d_names[num_files] = NULL; - d_exts[num_files] = NULL; - - num_files++; - } + } + else + { + *white_in_palette = j; + } } + else if (j == COLOR_PICKER) + { + /* Color picker: */ + + thumbs[num_files] = + thumbnail(img_color_picker, THUMB_W - 20, THUMB_H - 20, 0); + added = 1; + } + else if (j == COLOR_MIXER) + { + /* Color mixer: */ + + thumbs[num_files] = + thumbnail(img_color_mix, THUMB_W - 20, THUMB_H - 20, 0); + added = 1; + } + + if (added) + { + d_places[num_files] = PLACE_COLOR_PALETTE; + d_names[num_files] = NULL; + d_exts[num_files] = NULL; + + num_files++; + } + } return num_files; } @@ -22466,12 +23883,12 @@ static void reset_touched(void) int x, y; for (y = 0; y < canvas->h; y++) + { + for (x = 0; x < canvas->w; x++) { - for (x = 0; x < canvas->w; x++) - { - touched[(y * canvas->w) + x] = 0; - } + touched[(y * canvas->w) + x] = 0; } + } } /** @@ -22531,13 +23948,13 @@ static int do_color_sel(int temp_mode) Uint32(*getpixel_img_color_picker) (SDL_Surface *, int, int); if (!temp_mode) - { - want_animated_popups = 1; - } + { + want_animated_popups = 1; + } else - { - want_animated_popups = 0; - } + { + want_animated_popups = 0; + } val_x = val_y = motioner = 0; valhat_x = valhat_y = hatmotioner = 0; @@ -22555,7 +23972,8 @@ static int do_color_sel(int temp_mode) backup = SDL_CreateRGBSurface(screen->flags, screen->w, screen->h, screen->format->BitsPerPixel, screen->format->Rmask, - screen->format->Gmask, screen->format->Bmask, screen->format->Amask); + screen->format->Gmask, screen->format->Bmask, + screen->format->Amask); SDL_BlitSurface(screen, NULL, backup, NULL); @@ -22566,37 +23984,38 @@ static int do_color_sel(int temp_mode) if (want_animated_popups) + { + /* center of button */ + ox = WINDOW_WIDTH - color_button_w - color_button_w / 2; + oy = r_colors.y + r_colors.h / 2; + dx = 0; + dy = 0; + w = 0; + stepx = (r_color_sel.x - ox) / number_of_steps; + stepy = (r_color_sel.y - oy) / number_of_steps; + + for (i = 0; i < number_of_steps; i++) { - /* center of button */ - ox = WINDOW_WIDTH - color_button_w - color_button_w / 2; - oy = r_colors.y + r_colors.h / 2; - dx = 0; - dy = 0; - w = 0; - stepx = (r_color_sel.x - ox) / number_of_steps; - stepy = (r_color_sel.y - oy) / number_of_steps; - - for (i = 0; i < number_of_steps; i++) - { - w = w + (128 + 6 + 4) / number_of_steps; - dx = i * stepx; - dy = i * stepy; + w = w + (128 + 6 + 4) / number_of_steps; + dx = i * stepx; + dy = i * stepy; - dest.x = ox + dx; - dest.y = oy + dy; + dest.x = ox + dx; + dest.y = oy + dy; - dest.w = i * r_color_sel.w / number_of_steps; - dest.h = i * r_color_sel.h / number_of_steps; + dest.w = i * r_color_sel.w / number_of_steps; + dest.h = i * r_color_sel.h / number_of_steps; - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 255 - w, 255 - w, 255 - w)); - SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); - SDL_Delay(2); - } - - SDL_BlitSurface(backup, NULL, screen, NULL); + SDL_FillRect(screen, &dest, + SDL_MapRGB(screen->format, 255 - w, 255 - w, 255 - w)); + SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); + SDL_Delay(2); } + SDL_BlitSurface(backup, NULL, screen, NULL); + } + #ifndef NO_PROMPT_SHADOWS alpha_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, @@ -22604,30 +24023,34 @@ static int do_color_sel(int temp_mode) r_color_sel.h, screen->format->BitsPerPixel, screen->format->Rmask, - screen->format->Gmask, screen->format->Bmask, screen->format->Amask); + screen->format->Gmask, + screen->format->Bmask, + screen->format->Amask); if (alpha_surf != NULL) + { + SDL_FillRect(alpha_surf, NULL, + SDL_MapRGBA(alpha_surf->format, 0, 0, 0, 64)); + + for (i = 8; i > 0; i = i - 2) { - SDL_FillRect(alpha_surf, NULL, SDL_MapRGBA(alpha_surf->format, 0, 0, 0, 64)); + dest.x = r_color_sel.x + i; + dest.y = r_color_sel.y + i; + dest.w = r_color_sel.w; + dest.h = r_color_sel.h; - for (i = 8; i > 0; i = i - 2) - { - dest.x = r_color_sel.x + i; - dest.y = r_color_sel.y + i; - dest.w = r_color_sel.w; - dest.h = r_color_sel.h; - - SDL_BlitSurface(alpha_surf, NULL, screen, &dest); - } - - SDL_FreeSurface(alpha_surf); + SDL_BlitSurface(alpha_surf, NULL, screen, &dest); } + + SDL_FreeSurface(alpha_surf); + } #endif /* Draw prompt box: */ - SDL_FillRect(screen, &r_color_sel, SDL_MapRGB(screen->format, 255, 255, 255)); + SDL_FillRect(screen, &r_color_sel, + SDL_MapRGB(screen->format, 255, 255, 255)); @@ -22638,14 +24061,16 @@ static int do_color_sel(int temp_mode) color_example_dest.w = r_color_sel.w - button_w - 8; color_example_dest.h = r_color_sel.h - 4; - SDL_FillRect(screen, &color_example_dest, SDL_MapRGB(screen->format, 0, 0, 0)); + SDL_FillRect(screen, &color_example_dest, + SDL_MapRGB(screen->format, 0, 0, 0)); color_example_dest.x += 2; color_example_dest.y += 2; color_example_dest.w -= 4; color_example_dest.h -= 4; - SDL_FillRect(screen, &color_example_dest, SDL_MapRGB(screen->format, 255, 255, 255)); + SDL_FillRect(screen, &color_example_dest, + SDL_MapRGB(screen->format, 255, 255, 255)); color_example_dest.x += 2; color_example_dest.y += 2; @@ -22655,41 +24080,41 @@ static int do_color_sel(int temp_mode) if (!temp_mode) - { - /* Draw current color picker color: */ + { + /* Draw current color picker color: */ - SDL_FillRect(screen, &color_example_dest, - SDL_MapRGB(screen->format, - color_hexes[COLOR_SELECTOR][0], - color_hexes[COLOR_SELECTOR][1], - color_hexes[COLOR_SELECTOR][2])); + SDL_FillRect(screen, &color_example_dest, + SDL_MapRGB(screen->format, + color_hexes[COLOR_SELECTOR][0], + color_hexes[COLOR_SELECTOR][1], + color_hexes[COLOR_SELECTOR][2])); - /* Show "Back" button */ - back_left = r_color_sel.x + r_color_sel.w - button_w - 4; - back_top = r_color_sel.y; + /* Show "Back" button */ + back_left = r_color_sel.x + r_color_sel.w - button_w - 4; + back_top = r_color_sel.y; - dest.x = back_left; - dest.y = back_top; + dest.x = back_left; + dest.y = back_top; - SDL_BlitSurface(img_back, NULL, screen, &dest); + SDL_BlitSurface(img_back, NULL, screen, &dest); - dest.x = back_left + (img_back->w - img_openlabels_back->w) / 2; - dest.y = back_top + img_back->h - img_openlabels_back->h; - SDL_BlitSurface(img_openlabels_back, NULL, screen, &dest); - } + dest.x = back_left + (img_back->w - img_openlabels_back->w) / 2; + dest.y = back_top + img_back->h - img_openlabels_back->h; + SDL_BlitSurface(img_openlabels_back, NULL, screen, &dest); + } else - { - int mx, my; + { + int mx, my; - /* Temp mode: Grab color from canvas under the mouse immediately - (let the motion-handling code in the loop below do it for us!) */ - SDL_GetMouseState(&mx, &my); - SDL_WarpMouse(mx - 1, my); /* Need to move to a different spot, or no events occur */ - SDL_WarpMouse(mx, my); + /* Temp mode: Grab color from canvas under the mouse immediately + (let the motion-handling code in the loop below do it for us!) */ + SDL_GetMouseState(&mx, &my); + SDL_WarpMouse(mx - 1, my); /* Need to move to a different spot, or no events occur */ + SDL_WarpMouse(mx, my); - /* These will be unused in this mode: */ - back_left = back_top = 0; - } + /* These will be unused in this mode: */ + back_left = back_top = 0; + } /* Blit canvas on screen */ @@ -22708,174 +24133,189 @@ static int do_color_sel(int temp_mode) Needs testing on other operating sistems with touchscreen. */ if (!temp_mode) - SDL_WarpMouse(r_color_sel.x + r_color_sel.w / 2, r_color_sel.y + r_color_sel.h / 2); + SDL_WarpMouse(r_color_sel.x + r_color_sel.w / 2, + r_color_sel.y + r_color_sel.h / 2); #endif do + { + while (SDL_PollEvent(&event)) { - while (SDL_PollEvent(&event)) + if (event.type == SDL_QUIT) + { + chose = 0; + done = 1; + } + else if (event.type == SDL_WINDOWEVENT) + { + handle_active(&event); + } + else if (event.type == SDL_KEYUP) + { + key = event.key.keysym.sym; + + handle_keymouse(key, SDL_KEYUP, 24, NULL, NULL); + } + else if (event.type == SDL_KEYDOWN) + { + key = event.key.keysym.sym; + + handle_keymouse(key, SDL_KEYDOWN, 24, &r_color_picker, NULL); + + if (key == SDLK_ESCAPE) { - if (event.type == SDL_QUIT) + /* Hit [Escape]; abort (in either full UI or temporary mode) */ + chose = 0; + done = 1; + } + } + else if (event.type == SDL_MOUSEBUTTONUP + && valid_click(event.button.button)) + { + if (event.button.x >= r_canvas.x && + event.button.x < r_canvas.x + r_canvas.w && + event.button.y >= r_canvas.y + && event.button.y < r_canvas.y + r_canvas.h) + { + /* Picked a color in the canvas, and released! */ + + chose = 1; + done = 1; + + x = event.button.x - r_canvas.x; + y = event.button.y - r_canvas.y; + + color_sel_x = x; + color_sel_y = y; + } + else + { + if (!temp_mode) + { + if (event.button.x >= back_left && + event.button.x < back_left + img_back->w && + event.button.y >= back_top + && event.button.y < back_top + img_back->h) { + /* Full UI mode: Decided to go Back; abort */ + chose = 0; done = 1; } - else if (event.type == SDL_WINDOWEVENT) - { - handle_active(&event); - } - else if (event.type == SDL_KEYUP) - { - key = event.key.keysym.sym; + } + else + { + /* Temp mode: Released outside of canvas; abort */ + chose = 0; + done = 1; + } + } + } + else if (event.type == SDL_MOUSEMOTION) + { + if (event.button.x >= r_canvas.x && + event.button.x < r_canvas.x + r_canvas.w && + event.button.y >= r_canvas.y + && event.button.y < r_canvas.y + r_canvas.h) + { + /* Hovering over the canvas! */ - handle_keymouse(key, SDL_KEYUP, 24, NULL, NULL); - } - else if (event.type == SDL_KEYDOWN) - { - key = event.key.keysym.sym; - - handle_keymouse(key, SDL_KEYDOWN, 24, &r_color_picker, NULL); - - if (key == SDLK_ESCAPE) - { - /* Hit [Escape]; abort (in either full UI or temporary mode) */ - chose = 0; - done = 1; - } - } - else if (event.type == SDL_MOUSEBUTTONUP && valid_click(event.button.button)) - { - if (event.button.x >= r_canvas.x && - event.button.x < r_canvas.x + r_canvas.w && - event.button.y >= r_canvas.y && event.button.y < r_canvas.y + r_canvas.h) - { - /* Picked a color in the canvas, and released! */ - - chose = 1; - done = 1; - - x = event.button.x - r_canvas.x; - y = event.button.y - r_canvas.y; - - color_sel_x = x; - color_sel_y = y; - } - else - { - if (!temp_mode) - { - if (event.button.x >= back_left && - event.button.x < back_left + img_back->w && - event.button.y >= back_top && event.button.y < back_top + img_back->h) - { - /* Full UI mode: Decided to go Back; abort */ - - chose = 0; - done = 1; - } - } - else - { - /* Temp mode: Released outside of canvas; abort */ - chose = 0; - done = 1; - } - } - } - else if (event.type == SDL_MOUSEMOTION) - { - if (event.button.x >= r_canvas.x && - event.button.x < r_canvas.x + r_canvas.w && - event.button.y >= r_canvas.y && event.button.y < r_canvas.y + r_canvas.h) - { - /* Hovering over the canvas! */ - - do_setcursor(cursor_pipette); + do_setcursor(cursor_pipette); - /* Show a big solid example of the color: */ + /* Show a big solid example of the color: */ - x = event.button.x - r_canvas.x; - y = event.button.y - r_canvas.y; + x = event.button.x - r_canvas.x; + y = event.button.y - r_canvas.y; - getpixel_img_color_picker = getpixels[canvas->format->BytesPerPixel]; - SDL_GetRGB(getpixel_img_color_picker(canvas, x, y), canvas->format, &r, &g, &b); + getpixel_img_color_picker = + getpixels[canvas->format->BytesPerPixel]; + SDL_GetRGB(getpixel_img_color_picker(canvas, x, y), canvas->format, + &r, &g, &b); - SDL_FillRect(screen, &color_example_dest, SDL_MapRGB(screen->format, r, g, b)); + SDL_FillRect(screen, &color_example_dest, + SDL_MapRGB(screen->format, r, g, b)); - SDL_UpdateRect(screen, - color_example_dest.x, - color_example_dest.y, color_example_dest.w, color_example_dest.h); - } - else - { - /* Outside the canvas... */ + SDL_UpdateRect(screen, + color_example_dest.x, + color_example_dest.y, color_example_dest.w, + color_example_dest.h); + } + else + { + /* Outside the canvas... */ - if (!temp_mode) - { - /* Revert to current color picker color, so we know what it was, - and what we'll get if we go Back: */ + if (!temp_mode) + { + /* Revert to current color picker color, so we know what it was, + and what we'll get if we go Back: */ - SDL_FillRect(screen, &color_example_dest, - SDL_MapRGB(screen->format, - color_hexes[COLOR_SELECTOR][0], - color_hexes[COLOR_SELECTOR][1], - color_hexes[COLOR_SELECTOR][2])); + SDL_FillRect(screen, &color_example_dest, + SDL_MapRGB(screen->format, + color_hexes[COLOR_SELECTOR][0], + color_hexes[COLOR_SELECTOR][1], + color_hexes[COLOR_SELECTOR][2])); - SDL_UpdateRect(screen, - color_example_dest.x, - color_example_dest.y, color_example_dest.w, color_example_dest.h); + SDL_UpdateRect(screen, + color_example_dest.x, + color_example_dest.y, color_example_dest.w, + color_example_dest.h); - /* Change cursor to arrow (or hand, if over Back): */ + /* Change cursor to arrow (or hand, if over Back): */ - if (event.button.x >= back_left && - event.button.x < back_left + img_back->w && - event.button.y >= back_top && event.button.y < back_top + img_back->h) - do_setcursor(cursor_hand); - else - do_setcursor(cursor_arrow); - } - } - - oldpos_x = event.motion.x; - oldpos_y = event.motion.y; - } - else if (event.type == SDL_JOYAXISMOTION) - handle_joyaxismotion(event, &motioner, &val_x, &val_y); - - else if (event.type == SDL_JOYHATMOTION) - handle_joyhatmotion(event, oldpos_x, oldpos_y, &valhat_x, &valhat_y, &hatmotioner, &old_hat_ticks); - - else if (event.type == SDL_JOYBALLMOTION) - handle_joyballmotion(event, oldpos_x, oldpos_y); - - else if (event.type == SDL_JOYBUTTONDOWN || event.type == SDL_JOYBUTTONUP) - handle_joybuttonupdown(event, oldpos_x, oldpos_y); + if (event.button.x >= back_left && + event.button.x < back_left + img_back->w && + event.button.y >= back_top + && event.button.y < back_top + img_back->h) + do_setcursor(cursor_hand); + else + do_setcursor(cursor_arrow); + } } - if (motioner | hatmotioner) - handle_motioners(oldpos_x, oldpos_y, motioner, hatmotioner, old_hat_ticks, val_x, val_y, valhat_x, valhat_y); + oldpos_x = event.motion.x; + oldpos_y = event.motion.y; + } + else if (event.type == SDL_JOYAXISMOTION) + handle_joyaxismotion(event, &motioner, &val_x, &val_y); - SDL_Delay(10); + else if (event.type == SDL_JOYHATMOTION) + handle_joyhatmotion(event, oldpos_x, oldpos_y, &valhat_x, &valhat_y, + &hatmotioner, &old_hat_ticks); + + else if (event.type == SDL_JOYBALLMOTION) + handle_joyballmotion(event, oldpos_x, oldpos_y); + + else if (event.type == SDL_JOYBUTTONDOWN + || event.type == SDL_JOYBUTTONUP) + handle_joybuttonupdown(event, oldpos_x, oldpos_y); } + + if (motioner | hatmotioner) + handle_motioners(oldpos_x, oldpos_y, motioner, hatmotioner, + old_hat_ticks, val_x, val_y, valhat_x, valhat_y); + + SDL_Delay(10); + } while (!done); /* Set the new color: */ if (chose) - { - getpixel_img_color_picker = getpixels[canvas->format->BytesPerPixel]; - SDL_GetRGB(getpixel_img_color_picker(canvas, color_sel_x, color_sel_y), canvas->format, &r, &g, &b); + { + getpixel_img_color_picker = getpixels[canvas->format->BytesPerPixel]; + SDL_GetRGB(getpixel_img_color_picker(canvas, color_sel_x, color_sel_y), + canvas->format, &r, &g, &b); - color_hexes[COLOR_SELECTOR][0] = r; - color_hexes[COLOR_SELECTOR][1] = g; - color_hexes[COLOR_SELECTOR][2] = b; + color_hexes[COLOR_SELECTOR][0] = r; + color_hexes[COLOR_SELECTOR][1] = g; + color_hexes[COLOR_SELECTOR][2] = b; - /* Re-render color selector to show the current color it contains: */ - render_color_button(COLOR_SELECTOR, NULL, img_color_sel); - } + /* Re-render color selector to show the current color it contains: */ + render_color_button(COLOR_SELECTOR, NULL, img_color_sel); + } return (chose); } @@ -22922,7 +24362,8 @@ static int do_color_picker(void) do_setcursor(cursor_hand); - getpixel_img_color_picker = getpixels[img_color_picker->format->BytesPerPixel]; + getpixel_img_color_picker = + getpixels[img_color_picker->format->BytesPerPixel]; /* Draw button box: */ @@ -22932,7 +24373,8 @@ static int do_color_picker(void) backup = SDL_CreateRGBSurface(screen->flags, screen->w, screen->h, screen->format->BitsPerPixel, screen->format->Rmask, - screen->format->Gmask, screen->format->Bmask, screen->format->Amask); + screen->format->Gmask, screen->format->Bmask, + screen->format->Amask); SDL_BlitSurface(screen, NULL, backup, NULL); @@ -22947,18 +24389,21 @@ static int do_color_picker(void) stop = r_final.h / 2 + 6 + 4; for (w = 0; w <= stop; w = w + 4) - { - dest.x = ox - ((ox -r_final.x) * w) / stop; - dest.y = oy - ((oy -r_final.y) * w) / stop; - dest.w = w * 4; - dest.h = w * 2; + { + dest.x = ox - ((ox - r_final.x) * w) / stop; + dest.y = oy - ((oy - r_final.y) * w) / stop; + dest.w = w * 4; + dest.h = w * 2; - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 255 - (int)(w / button_scale) , 255 -(int)( w / button_scale), 255 - (int)(w / button_scale))); + SDL_FillRect(screen, &dest, + SDL_MapRGB(screen->format, 255 - (int) (w / button_scale), + 255 - (int) (w / button_scale), + 255 - (int) (w / button_scale))); - SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); - if (w % 16 == 0) - SDL_Delay(1); - } + SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); + if (w % 16 == 0) + SDL_Delay(1); + } SDL_BlitSurface(backup, NULL, screen, NULL); @@ -22968,24 +24413,27 @@ static int do_color_picker(void) r_final.h + 16, screen->format->BitsPerPixel, screen->format->Rmask, - screen->format->Gmask, screen->format->Bmask, screen->format->Amask); + screen->format->Gmask, + screen->format->Bmask, + screen->format->Amask); if (alpha_surf != NULL) + { + SDL_FillRect(alpha_surf, NULL, + SDL_MapRGBA(alpha_surf->format, 0, 0, 0, 64)); + + for (i = 8; i > 0; i = i - 2) { - SDL_FillRect(alpha_surf, NULL, SDL_MapRGBA(alpha_surf->format, 0, 0, 0, 64)); + dest.x = r_final.x + i - 4; + dest.y = r_final.y + i - 4; + dest.w = r_final.w + 8; + dest.h = r_final.h + 16; - for (i = 8; i > 0; i = i - 2) - { - dest.x = r_final.x + i - 4; - dest.y = r_final.y + i - 4; - dest.w = r_final.w + 8; - dest.h = r_final.h + 16; - - SDL_BlitSurface(alpha_surf, NULL, screen, &dest); - } - - SDL_FreeSurface(alpha_surf); + SDL_BlitSurface(alpha_surf, NULL, screen, &dest); } + + SDL_FreeSurface(alpha_surf); + } #endif @@ -23027,20 +24475,23 @@ static int do_color_picker(void) /* Determine spot for example color: */ - color_example_dest.x = color_picker_left + img_color_picker->w + 2 + img_back->w + 2; + color_example_dest.x = + color_picker_left + img_color_picker->w + 2 + img_back->w + 2; color_example_dest.y = color_picker_top + 2; color_example_dest.w = r_final.w / 2 - 2 - img_back->w - 2; color_example_dest.h = r_final.h / 2 - 4; - SDL_FillRect(screen, &color_example_dest, SDL_MapRGB(screen->format, 0, 0, 0)); + SDL_FillRect(screen, &color_example_dest, + SDL_MapRGB(screen->format, 0, 0, 0)); color_example_dest.x += 2; color_example_dest.y += 2; color_example_dest.w -= 4; color_example_dest.h -= 4; - SDL_FillRect(screen, &color_example_dest, SDL_MapRGB(screen->format, 255, 255, 255)); + SDL_FillRect(screen, &color_example_dest, + SDL_MapRGB(screen->format, 255, 255, 255)); color_example_dest.x += 2; color_example_dest.y += 2; @@ -23091,7 +24542,8 @@ static int do_color_picker(void) so we avoid updating parts of the screen where the crosshairs extend past the rainbow rectangle or value slider, since this function does not (yet) do any clipping) */ - draw_color_picker_crosshairs(color_picker_left, color_picker_top, color_picker_val_left, color_picker_val_top); + draw_color_picker_crosshairs(color_picker_left, color_picker_top, + color_picker_val_left, color_picker_val_top); dest.x = color_picker_left; dest.y = color_picker_top; @@ -23114,262 +24566,295 @@ static int do_color_picker(void) last_motion_within_val_slider = 0; do + { + while (SDL_PollEvent(&event)) { - while (SDL_PollEvent(&event)) + if (event.type == SDL_QUIT) + { + chose = 0; + done = 1; + } + else if (event.type == SDL_WINDOWEVENT) + { + handle_active(&event); + } + else if (event.type == SDL_KEYUP) + { + key = event.key.keysym.sym; + + handle_keymouse(key, SDL_KEYUP, 24, NULL, NULL); + } + else if (event.type == SDL_KEYDOWN) + { + key = event.key.keysym.sym; + + handle_keymouse(key, SDL_KEYDOWN, 24, &r_color_picker, NULL); + + if (key == SDLK_ESCAPE || key == SDLK_AC_BACK) { - if (event.type == SDL_QUIT) - { - chose = 0; - done = 1; - } - else if (event.type == SDL_WINDOWEVENT) - { - handle_active(&event); - } - else if (event.type == SDL_KEYUP) - { - key = event.key.keysym.sym; + chose = 0; + done = 1; + } + } + else if (event.type == SDL_MOUSEBUTTONUP + && valid_click(event.button.button)) + { + if (event.button.x >= color_picker_left && + event.button.x < color_picker_left + img_color_picker->w && + event.button.y >= color_picker_top && + event.button.y < color_picker_top + img_color_picker->h) + { + /* Picked a color! */ - handle_keymouse(key, SDL_KEYUP, 24, NULL, NULL); - } - else if (event.type == SDL_KEYDOWN) - { - key = event.key.keysym.sym; + x = event.button.x - color_picker_left; + y = event.button.y - color_picker_top; - handle_keymouse(key, SDL_KEYDOWN, 24, &r_color_picker, NULL); + color_picker_x = x; + color_picker_y = y; - if (key == SDLK_ESCAPE || key == SDLK_AC_BACK) - { - chose = 0; - done = 1; - } - } - else if (event.type == SDL_MOUSEBUTTONUP && valid_click(event.button.button)) - { - if (event.button.x >= color_picker_left && - event.button.x < color_picker_left + img_color_picker->w && - event.button.y >= color_picker_top && - event.button.y < color_picker_top + img_color_picker->h) - { - /* Picked a color! */ + /* Update (entire) color box */ + SDL_GetRGB(getpixel_img_color_picker(img_color_picker, x, y), + img_color_picker->format, &r, &g, &b); - x = event.button.x - color_picker_left; - y = event.button.y - color_picker_top; + SDL_FillRect(screen, &color_example_dest, + SDL_MapRGB(screen->format, r, g, b)); - color_picker_x = x; - color_picker_y = y; - - /* Update (entire) color box */ - SDL_GetRGB(getpixel_img_color_picker(img_color_picker, x, y), img_color_picker->format, &r, &g, &b); - - SDL_FillRect(screen, &color_example_dest, SDL_MapRGB(screen->format, r, g, b)); - - SDL_UpdateRect(screen, - color_example_dest.x, color_example_dest.y, - color_example_dest.w, color_example_dest.h); + SDL_UpdateRect(screen, + color_example_dest.x, color_example_dest.y, + color_example_dest.w, color_example_dest.h); - /* Reposition hue/sat crosshair */ - dest.x = color_picker_left; - dest.y = color_picker_top; - SDL_BlitSurface(img_color_picker, NULL, screen, &dest); - draw_color_picker_crosshairs(color_picker_left, color_picker_top, color_picker_val_left, color_picker_val_top); - SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); - } - else if (event.button.x >= color_picker_val_left && - event.button.y >= color_picker_val_top && - event.button.x <= color_picker_val_left + img_back->w && - event.button.y <= color_picker_val_top + img_color_picker_val->h) - { - /* Picked a value from the slider */ + /* Reposition hue/sat crosshair */ + dest.x = color_picker_left; + dest.y = color_picker_top; + SDL_BlitSurface(img_color_picker, NULL, screen, &dest); + draw_color_picker_crosshairs(color_picker_left, color_picker_top, + color_picker_val_left, + color_picker_val_top); + SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); + } + else if (event.button.x >= color_picker_val_left && + event.button.y >= color_picker_val_top && + event.button.x <= color_picker_val_left + img_back->w && + event.button.y <= + color_picker_val_top + img_color_picker_val->h) + { + /* Picked a value from the slider */ - y = event.button.y - color_picker_val_top; - color_picker_v = y; + y = event.button.y - color_picker_val_top; + color_picker_v = y; - /* Re-render the palette with the new value */ - render_color_picker_palette(); + /* Re-render the palette with the new value */ + render_color_picker_palette(); - /* Update (entire) color box */ - SDL_GetRGB(getpixel_img_color_picker(img_color_picker, color_picker_x, color_picker_y), img_color_picker->format, &r, &g, &b); + /* Update (entire) color box */ + SDL_GetRGB(getpixel_img_color_picker + (img_color_picker, color_picker_x, color_picker_y), + img_color_picker->format, &r, &g, &b); - SDL_FillRect(screen, &color_example_dest, SDL_MapRGB(screen->format, r, g, b)); + SDL_FillRect(screen, &color_example_dest, + SDL_MapRGB(screen->format, r, g, b)); - SDL_UpdateRect(screen, - color_example_dest.x, color_example_dest.y, - color_example_dest.w, color_example_dest.h); + SDL_UpdateRect(screen, + color_example_dest.x, color_example_dest.y, + color_example_dest.w, color_example_dest.h); - /* Redraw hue/sat palette, and val slider, and redraw crosshairs */ - draw_color_picker_palette_and_values(color_picker_left, color_picker_top, color_picker_val_left, color_picker_val_top); - } - else if (event.button.x >= done_left && - event.button.x < done_left + img_yes->w && - event.button.y >= done_top && event.button.y < done_top + img_yes->h) - { - /* Accepting color */ + /* Redraw hue/sat palette, and val slider, and redraw crosshairs */ + draw_color_picker_palette_and_values(color_picker_left, + color_picker_top, + color_picker_val_left, + color_picker_val_top); + } + else if (event.button.x >= done_left && + event.button.x < done_left + img_yes->w && + event.button.y >= done_top + && event.button.y < done_top + img_yes->h) + { + /* Accepting color */ - chose = 1; - done = 1; - } - else if (event.button.x >= back_left && - event.button.x < back_left + img_back->w && - event.button.y >= back_top && event.button.y < back_top + img_back->h) - { - /* Decided to go Back */ + chose = 1; + done = 1; + } + else if (event.button.x >= back_left && + event.button.x < back_left + img_back->w && + event.button.y >= back_top + && event.button.y < back_top + img_back->h) + { + /* Decided to go Back */ - chose = 0; - done = 1; - } - } - else if (event.type == SDL_MOUSEMOTION) - { - if (event.button.x >= color_picker_val_left && - event.button.y >= color_picker_val_top && - event.button.x <= color_picker_val_left + img_back->w && - event.button.y <= color_picker_val_top + img_color_picker_val->h) - { - int tmp_color_picker_v; + chose = 0; + done = 1; + } + } + else if (event.type == SDL_MOUSEMOTION) + { + if (event.button.x >= color_picker_val_left && + event.button.y >= color_picker_val_top && + event.button.x <= color_picker_val_left + img_back->w && + event.button.y <= color_picker_val_top + img_color_picker_val->h) + { + int tmp_color_picker_v; - /* Hovering over a value from the slider */ + /* Hovering over a value from the slider */ - do_setcursor(cursor_hand); + do_setcursor(cursor_hand); - y = event.button.y - color_picker_val_top; - tmp_color_picker_v = color_picker_v; - color_picker_v = y; + y = event.button.y - color_picker_val_top; + tmp_color_picker_v = color_picker_v; + color_picker_v = y; - /* Re-render the palette with the new value */ - render_color_picker_palette(); - color_picker_v = tmp_color_picker_v; + /* Re-render the palette with the new value */ + render_color_picker_palette(); + color_picker_v = tmp_color_picker_v; - /* Show a big solid example of the color: */ + /* Show a big solid example of the color: */ - x = event.button.x - color_picker_left; - y = event.button.y - color_picker_top; + x = event.button.x - color_picker_left; + y = event.button.y - color_picker_top; - SDL_GetRGB(getpixel_img_color_picker(img_color_picker, color_picker_x, color_picker_y), img_color_picker->format, &r, &g, &b); + SDL_GetRGB(getpixel_img_color_picker + (img_color_picker, color_picker_x, color_picker_y), + img_color_picker->format, &r, &g, &b); - dest.x = color_example_dest.x + color_example_dest.w / 4; - dest.y = color_example_dest.y + color_example_dest.h / 4; - dest.w = color_example_dest.w / 2; - dest.h = color_example_dest.h / 2; + dest.x = color_example_dest.x + color_example_dest.w / 4; + dest.y = color_example_dest.y + color_example_dest.h / 4; + dest.w = color_example_dest.w / 2; + dest.h = color_example_dest.h / 2; - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, r, g, b)); + SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, r, g, b)); - SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); + SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); - /* Redraw hue/sat palette, and val slider, and redraw crosshairs */ - draw_color_picker_palette_and_values(color_picker_left, color_picker_top, color_picker_val_left, color_picker_val_top); + /* Redraw hue/sat palette, and val slider, and redraw crosshairs */ + draw_color_picker_palette_and_values(color_picker_left, + color_picker_top, + color_picker_val_left, + color_picker_val_top); - last_motion_within_val_slider = 1; - } - else - { - if (last_motion_within_val_slider) - { - render_color_picker_palette(); - draw_color_picker_palette_and_values(color_picker_left, color_picker_top, color_picker_val_left, color_picker_val_top); - last_motion_within_val_slider = 0; - } + last_motion_within_val_slider = 1; + } + else + { + if (last_motion_within_val_slider) + { + render_color_picker_palette(); + draw_color_picker_palette_and_values(color_picker_left, + color_picker_top, + color_picker_val_left, + color_picker_val_top); + last_motion_within_val_slider = 0; + } - if (event.button.x >= color_picker_left && - event.button.x < color_picker_left + img_color_picker->w && - event.button.y >= color_picker_top && event.button.y < color_picker_top + img_color_picker->h) - { - /* Hovering over the colors! */ + if (event.button.x >= color_picker_left && + event.button.x < color_picker_left + img_color_picker->w && + event.button.y >= color_picker_top + && event.button.y < color_picker_top + img_color_picker->h) + { + /* Hovering over the colors! */ - do_setcursor(cursor_pipette); + do_setcursor(cursor_pipette); - /* Show a big solid example of the color: */ + /* Show a big solid example of the color: */ - x = event.button.x - color_picker_left; - y = event.button.y - color_picker_top; + x = event.button.x - color_picker_left; + y = event.button.y - color_picker_top; - SDL_GetRGB(getpixel_img_color_picker(img_color_picker, x, y), img_color_picker->format, &r, &g, &b); + SDL_GetRGB(getpixel_img_color_picker(img_color_picker, x, y), + img_color_picker->format, &r, &g, &b); - dest.x = color_example_dest.x + color_example_dest.w / 4; - dest.y = color_example_dest.y + color_example_dest.h / 4; - dest.w = color_example_dest.w / 2; - dest.h = color_example_dest.h / 2; + dest.x = color_example_dest.x + color_example_dest.w / 4; + dest.y = color_example_dest.y + color_example_dest.h / 4; + dest.w = color_example_dest.w / 2; + dest.h = color_example_dest.h / 2; - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, r, g, b)); + SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, r, g, b)); - SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); - } - else - { - /* Revert to current color picker color */ + SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); + } + else + { + /* Revert to current color picker color */ - SDL_GetRGB(getpixel_img_color_picker(img_color_picker, color_picker_x, color_picker_y), - img_color_picker->format, &r, &g, &b); + SDL_GetRGB(getpixel_img_color_picker + (img_color_picker, color_picker_x, color_picker_y), + img_color_picker->format, &r, &g, &b); - SDL_FillRect(screen, &color_example_dest, SDL_MapRGB(screen->format, r, g, b)); + SDL_FillRect(screen, &color_example_dest, + SDL_MapRGB(screen->format, r, g, b)); - SDL_UpdateRect(screen, - color_example_dest.x, color_example_dest.y, - color_example_dest.w, color_example_dest.h); + SDL_UpdateRect(screen, + color_example_dest.x, color_example_dest.y, + color_example_dest.w, color_example_dest.h); - /* Change cursor to arrow (or hand, if over Back or Done): */ + /* Change cursor to arrow (or hand, if over Back or Done): */ - if (event.button.x >= back_left && - event.button.x < back_left + img_back->w && - event.button.y >= back_top && event.button.y < back_top + img_back->h) - do_setcursor(cursor_hand); - else if (event.button.x >= done_left && - event.button.x < done_left + img_yes->w && - event.button.y >= done_top && event.button.y < done_top + img_yes->h) - do_setcursor(cursor_hand); - else - do_setcursor(cursor_arrow); - } - } - - oldpos_x = event.motion.x; - oldpos_y = event.motion.y; - } - else if (event.type == SDL_JOYAXISMOTION) - handle_joyaxismotion(event, &motioner, &val_x, &val_y); - - else if (event.type == SDL_JOYHATMOTION) - handle_joyhatmotion(event, oldpos_x, oldpos_y, &valhat_x, &valhat_y, &hatmotioner, &old_hat_ticks); - - else if (event.type == SDL_JOYBALLMOTION) - handle_joyballmotion(event, oldpos_x, oldpos_y); - - else if (event.type == SDL_JOYBUTTONDOWN || event.type == SDL_JOYBUTTONUP) - handle_joybuttonupdown(event, oldpos_x, oldpos_y); + if (event.button.x >= back_left && + event.button.x < back_left + img_back->w && + event.button.y >= back_top + && event.button.y < back_top + img_back->h) + do_setcursor(cursor_hand); + else if (event.button.x >= done_left && + event.button.x < done_left + img_yes->w && + event.button.y >= done_top + && event.button.y < done_top + img_yes->h) + do_setcursor(cursor_hand); + else + do_setcursor(cursor_arrow); + } } - if (motioner | hatmotioner) - handle_motioners(oldpos_x, oldpos_y, motioner, hatmotioner, old_hat_ticks, val_x, val_y, valhat_x, valhat_y); + oldpos_x = event.motion.x; + oldpos_y = event.motion.y; + } + else if (event.type == SDL_JOYAXISMOTION) + handle_joyaxismotion(event, &motioner, &val_x, &val_y); - SDL_Delay(10); + else if (event.type == SDL_JOYHATMOTION) + handle_joyhatmotion(event, oldpos_x, oldpos_y, &valhat_x, &valhat_y, + &hatmotioner, &old_hat_ticks); + + else if (event.type == SDL_JOYBALLMOTION) + handle_joyballmotion(event, oldpos_x, oldpos_y); + + else if (event.type == SDL_JOYBUTTONDOWN + || event.type == SDL_JOYBUTTONUP) + handle_joybuttonupdown(event, oldpos_x, oldpos_y); } + + if (motioner | hatmotioner) + handle_motioners(oldpos_x, oldpos_y, motioner, hatmotioner, + old_hat_ticks, val_x, val_y, valhat_x, valhat_y); + + SDL_Delay(10); + } while (!done); if (chose) - { - /* Set the new color: */ - SDL_GetRGB(getpixel_img_color_picker(img_color_picker, color_picker_x, color_picker_y), img_color_picker->format, &r, &g, &b); + { + /* Set the new color: */ + SDL_GetRGB(getpixel_img_color_picker + (img_color_picker, color_picker_x, color_picker_y), + img_color_picker->format, &r, &g, &b); - color_hexes[COLOR_PICKER][0] = r; - color_hexes[COLOR_PICKER][1] = g; - color_hexes[COLOR_PICKER][2] = b; + color_hexes[COLOR_PICKER][0] = r; + color_hexes[COLOR_PICKER][1] = g; + color_hexes[COLOR_PICKER][2] = b; - /* Re-render color picker to show the current color it contains: */ - render_color_button(COLOR_PICKER, img_color_picker_thumb, NULL); - } + /* Re-render color picker to show the current color it contains: */ + render_color_button(COLOR_PICKER, img_color_picker_thumb, NULL); + } else - { - /* Set crosshairs to the existing color */ - color_picker_x = old_cp_x; - color_picker_y = old_cp_y; - color_picker_v = old_cp_v; - } + { + /* Set crosshairs to the existing color */ + color_picker_x = old_cp_x; + color_picker_y = old_cp_y; + color_picker_v = old_cp_v; + } /* Remove the prompt: */ @@ -23381,7 +24866,11 @@ static int do_color_picker(void) } -static void draw_color_picker_palette_and_values(int color_picker_left, int color_picker_top, int color_picker_val_left, int color_picker_val_top) { +static void draw_color_picker_palette_and_values(int color_picker_left, + int color_picker_top, + int color_picker_val_left, + int color_picker_val_top) +{ SDL_Rect dest; draw_color_picker_values(color_picker_val_left, color_picker_val_top); @@ -23391,7 +24880,8 @@ static void draw_color_picker_palette_and_values(int color_picker_left, int colo SDL_BlitSurface(img_color_picker, NULL, screen, &dest); SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); - draw_color_picker_crosshairs(color_picker_left, color_picker_top, color_picker_val_left, color_picker_val_top); + draw_color_picker_crosshairs(color_picker_left, color_picker_top, + color_picker_val_left, color_picker_val_top); dest.x = color_picker_val_left; dest.y = color_picker_val_top; @@ -23410,29 +24900,30 @@ static void render_color_picker_palette(void) { int x, y; Uint8 r, g, b; - void (*putpixel) (SDL_Surface *, int, int, Uint32); + void (*putpixel)(SDL_Surface *, int, int, Uint32); SDL_Event event; putpixel = putpixels[img_color_picker->format->BytesPerPixel]; for (y = 0; y < img_color_picker->h; y++) + { + for (x = 0; x < img_color_picker->w; x++) { - for (x = 0; x < img_color_picker->w; x++) - { - hsvtorgb((((float) y * 360.0) / ((float) img_color_picker->h)), - ((float) x / ((float) img_color_picker->w)), - 1.0 - (((float) color_picker_v) / ((float) img_color_picker_val->h)), - &r, &g, &b); - putpixel(img_color_picker, x, y, - SDL_MapRGBA(img_color_picker->format, r, g, b, 255)); - } + hsvtorgb((((float) y * 360.0) / ((float) img_color_picker->h)), + ((float) x / ((float) img_color_picker->w)), + 1.0 - + (((float) color_picker_v) / ((float) img_color_picker_val->h)), + &r, &g, &b); + putpixel(img_color_picker, x, y, + SDL_MapRGBA(img_color_picker->format, r, g, b, 255)); } + } while (SDL_PollEvent(&event) && event.type == SDL_MOUSEMOTION) - { - /* Eat further motion events that may've been generated while - we re-rendered the palette (so we don't spin tons of cycles - rendering it more than we really need) */ - } + { + /* Eat further motion events that may've been generated while + we re-rendered the palette (so we don't spin tons of cycles + rendering it more than we really need) */ + } SDL_PushEvent(&event); } @@ -23442,7 +24933,8 @@ static void render_color_picker_palette(void) center of the crosshair is positioned precisely */ int CROSSHAIR_LENGTH, CROSSHAIR_THICKNESS, CROSSHAIR_BORDER; -static void set_color_picker_crosshair_size(void) { +static void set_color_picker_crosshair_size(void) +{ CROSSHAIR_LENGTH = (int) (11 * button_scale); CROSSHAIR_LENGTH /= 2; CROSSHAIR_LENGTH *= 2; @@ -23464,26 +24956,38 @@ static void set_color_picker_crosshair_size(void) { if (CROSSHAIR_BORDER < 1) CROSSHAIR_BORDER = 1; - DEBUG_PRINTF("Crosshair will be %d in size, with %d thickness, and a %d border\n", - CROSSHAIR_LENGTH, CROSSHAIR_THICKNESS, CROSSHAIR_BORDER); + DEBUG_PRINTF + ("Crosshair will be %d in size, with %d thickness, and a %d border\n", + CROSSHAIR_LENGTH, CROSSHAIR_THICKNESS, CROSSHAIR_BORDER); } -static void draw_color_picker_crosshairs(int color_picker_left, int color_picker_top, int color_picker_val_left, int color_picker_val_top) +static void draw_color_picker_crosshairs(int color_picker_left, + int color_picker_top, + int color_picker_val_left, + int color_picker_val_top) { SDL_Rect dest; int ctr_x; /* Hue/Saturation (the big rectangle) */ - dest.x = color_picker_x + color_picker_left - (CROSSHAIR_LENGTH - 1) / 2 - CROSSHAIR_BORDER; - dest.y = color_picker_y + color_picker_top - (CROSSHAIR_THICKNESS - 1) / 2 - CROSSHAIR_BORDER; + dest.x = + color_picker_x + color_picker_left - (CROSSHAIR_LENGTH - 1) / 2 - + CROSSHAIR_BORDER; + dest.y = + color_picker_y + color_picker_top - (CROSSHAIR_THICKNESS - 1) / 2 - + CROSSHAIR_BORDER; dest.w = CROSSHAIR_LENGTH + CROSSHAIR_BORDER * 2; dest.h = CROSSHAIR_THICKNESS + CROSSHAIR_BORDER * 2; SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 0, 0, 0)); - dest.x = color_picker_x + color_picker_left - (CROSSHAIR_THICKNESS - 1) / 2 - CROSSHAIR_BORDER; - dest.y = color_picker_y + color_picker_top - (CROSSHAIR_LENGTH - 1) / 2 - CROSSHAIR_BORDER; + dest.x = + color_picker_x + color_picker_left - (CROSSHAIR_THICKNESS - 1) / 2 - + CROSSHAIR_BORDER; + dest.y = + color_picker_y + color_picker_top - (CROSSHAIR_LENGTH - 1) / 2 - + CROSSHAIR_BORDER; dest.w = CROSSHAIR_THICKNESS + CROSSHAIR_BORDER * 2; dest.h = CROSSHAIR_LENGTH + CROSSHAIR_BORDER * 2; @@ -23511,14 +25015,18 @@ static void draw_color_picker_crosshairs(int color_picker_left, int color_picker ctr_x = color_picker_val_left + img_back->w / 2; dest.x = ctr_x - (CROSSHAIR_LENGTH - 1) / 2 - CROSSHAIR_BORDER; - dest.y = color_picker_v + color_picker_val_top - (CROSSHAIR_THICKNESS - 1) / 2 - CROSSHAIR_BORDER; + dest.y = + color_picker_v + color_picker_val_top - (CROSSHAIR_THICKNESS - 1) / 2 - + CROSSHAIR_BORDER; dest.w = CROSSHAIR_LENGTH + CROSSHAIR_BORDER * 2; dest.h = CROSSHAIR_THICKNESS + CROSSHAIR_BORDER * 2; SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 0, 0, 0)); dest.x = ctr_x - (CROSSHAIR_THICKNESS - 1) / 2 - CROSSHAIR_BORDER; - dest.y = color_picker_v + color_picker_val_top - (CROSSHAIR_LENGTH - 1) / 2 - CROSSHAIR_BORDER; + dest.y = + color_picker_v + color_picker_val_top - (CROSSHAIR_LENGTH - 1) / 2 - + CROSSHAIR_BORDER; dest.w = CROSSHAIR_THICKNESS + CROSSHAIR_BORDER * 2; dest.h = CROSSHAIR_LENGTH + CROSSHAIR_BORDER * 2; @@ -23526,7 +25034,8 @@ static void draw_color_picker_crosshairs(int color_picker_left, int color_picker dest.x = ctr_x - (CROSSHAIR_LENGTH - 1) / 2; - dest.y = color_picker_v + color_picker_val_top - (CROSSHAIR_THICKNESS - 1) / 2; + dest.y = + color_picker_v + color_picker_val_top - (CROSSHAIR_THICKNESS - 1) / 2; dest.w = CROSSHAIR_LENGTH; dest.h = CROSSHAIR_THICKNESS; @@ -23555,7 +25064,8 @@ static void draw_color_picker_values(int l, int t) } -enum { +enum +{ COLOR_MIXER_BTN_RED, COLOR_MIXER_BTN_YELLOW, COLOR_MIXER_BTN_BLUE, @@ -23572,20 +25082,21 @@ enum { }; SDL_Rect color_example_dest; -int color_mix_btn_lefts[NUM_COLOR_MIXER_BTNS], color_mix_btn_tops[NUM_COLOR_MIXER_BTNS]; +int color_mix_btn_lefts[NUM_COLOR_MIXER_BTNS], + color_mix_btn_tops[NUM_COLOR_MIXER_BTNS]; /* Hue (degrees 0-360, or -1 for N/A), Saturation (0.0-1.0), Value (0.0-1.0) */ float mixer_hsv[NUM_MIXER_COLORS][3] = { - { 330.0, 1.0, 0.9 }, /* Red (Magenta-ish) */ - { 60.0, 1.0, 0.9 }, /* Yellow */ - { 210.0, 1.0, 0.9 }, /* Blue (Cyan-ish) */ - { -1, 0.0, 1.0 }, /* White */ - { -1, 0.0, 0.5 }, /* Grey */ - { -1, 0.0, 0.0 } /* Black */ + {330.0, 1.0, 0.9}, /* Red (Magenta-ish) */ + {60.0, 1.0, 0.9}, /* Yellow */ + {210.0, 1.0, 0.9}, /* Blue (Cyan-ish) */ + {-1, 0.0, 1.0}, /* White */ + {-1, 0.0, 0.5}, /* Grey */ + {-1, 0.0, 0.0} /* Black */ }; -const char * color_mixer_color_names[NUM_MIXER_COLORS] = { +const char *color_mixer_color_names[NUM_MIXER_COLORS] = { /* Descriptions (names) of the color mixer tool's primary colors and shades */ gettext_noop("red"), gettext_noop("yellow"), @@ -23595,14 +25106,17 @@ const char * color_mixer_color_names[NUM_MIXER_COLORS] = { gettext_noop("black") }; -const char * color_mixer_color_tips[] = { +const char *color_mixer_color_tips[] = { /* Tool tip text describing a mixed color (e.g., "1/3 red and 1/2 yellow", or "1/3 blue and 2/3 white", etc.) */ gettext_noop("Your color is %1$s %2$s."), gettext_noop("Your color is %1$s %2$s and %3$s %4$s."), gettext_noop("Your color is %1$s %2$s, %3$s %4$s, and %5$s %6$s."), - gettext_noop("Your color is %1$s %2$s, %3$s %4$s, %5$s %6$s, and %7$s %8$s."), - gettext_noop("Your color is %1$s %2$s, %3$s %4$s, %5$s %6$s, %7$s %8$s, and %9$s %10$s."), - gettext_noop("Your color is %1$s %2$s, %3$s %4$s, %5$s %6$s, %7$s %8$s, %9$s %10$s, and %11$s %12$s.") + gettext_noop + ("Your color is %1$s %2$s, %3$s %4$s, %5$s %6$s, and %7$s %8$s."), + gettext_noop + ("Your color is %1$s %2$s, %3$s %4$s, %5$s %6$s, %7$s %8$s, and %9$s %10$s."), + gettext_noop + ("Your color is %1$s %2$s, %3$s %4$s, %5$s %6$s, %7$s %8$s, %9$s %10$s, and %11$s %12$s.") }; @@ -23652,7 +25166,8 @@ static int do_color_mix(void) backup = SDL_CreateRGBSurface(screen->flags, screen->w, screen->h, screen->format->BitsPerPixel, screen->format->Rmask, - screen->format->Gmask, screen->format->Bmask, screen->format->Amask); + screen->format->Gmask, screen->format->Bmask, + screen->format->Amask); SDL_BlitSurface(screen, NULL, backup, NULL); @@ -23671,18 +25186,21 @@ static int do_color_mix(void) stop = r_final.h / 2 + 6 + 4; for (w = 0; w <= stop; w = w + 4) - { - dest.x = ox - ((ox -r_final.x) * w) / stop; - dest.y = oy - ((oy -r_final.y) * w) / stop; - dest.w = w * 4; - dest.h = w * 2; + { + dest.x = ox - ((ox - r_final.x) * w) / stop; + dest.y = oy - ((oy - r_final.y) * w) / stop; + dest.w = w * 4; + dest.h = w * 2; - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 255 - (int)(w / button_scale) , 255 -(int)( w / button_scale), 255 - (int)(w / button_scale))); + SDL_FillRect(screen, &dest, + SDL_MapRGB(screen->format, 255 - (int) (w / button_scale), + 255 - (int) (w / button_scale), + 255 - (int) (w / button_scale))); - SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); - if (w % 16 == 0) - SDL_Delay(1); - } + SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); + if (w % 16 == 0) + SDL_Delay(1); + } SDL_BlitSurface(backup, NULL, screen, NULL); @@ -23692,25 +25210,28 @@ static int do_color_mix(void) r_final.h + 16, screen->format->BitsPerPixel, screen->format->Rmask, - screen->format->Gmask, screen->format->Bmask, screen->format->Amask); + screen->format->Gmask, + screen->format->Bmask, + screen->format->Amask); if (alpha_surf != NULL) + { + SDL_FillRect(alpha_surf, NULL, + SDL_MapRGBA(alpha_surf->format, 0, 0, 0, 64)); + + for (i = 8; i > 0; i = i - 2) { - SDL_FillRect(alpha_surf, NULL, SDL_MapRGBA(alpha_surf->format, 0, 0, 0, 64)); + dest.x = r_final.x + i - 4; + dest.y = r_final.y + i - 4; + dest.w = r_final.w + 8; + dest.h = r_final.h + 16; - for (i = 8; i > 0; i = i - 2) - { - dest.x = r_final.x + i - 4; - dest.y = r_final.y + i - 4; - dest.w = r_final.w + 8; - dest.h = r_final.h + 16; - - SDL_BlitSurface(alpha_surf, NULL, screen, &dest); - } - - - SDL_FreeSurface(alpha_surf); + SDL_BlitSurface(alpha_surf, NULL, screen, &dest); } + + + SDL_FreeSurface(alpha_surf); + } #endif @@ -23731,14 +25252,16 @@ static int do_color_mix(void) color_example_dest.w = cell_w * 2; color_example_dest.h = cell_h * 2; - SDL_FillRect(screen, &color_example_dest, SDL_MapRGB(screen->format, 0, 0, 0)); + SDL_FillRect(screen, &color_example_dest, + SDL_MapRGB(screen->format, 0, 0, 0)); color_example_dest.x += 2; color_example_dest.y += 2; color_example_dest.w -= 4; color_example_dest.h -= 4; - SDL_FillRect(screen, &color_example_dest, SDL_MapRGB(screen->format, 255, 255, 255)); + SDL_FillRect(screen, &color_example_dest, + SDL_MapRGB(screen->format, 255, 255, 255)); color_example_dest.x += 2; color_example_dest.y += 2; @@ -23748,45 +25271,46 @@ static int do_color_mix(void) /* Draw current color mixer color: */ if (color_mixer_reset) - { - draw_color_mixer_blank_example(); - } + { + draw_color_mixer_blank_example(); + } else - { - SDL_FillRect(screen, &color_example_dest, - SDL_MapRGB(screen->format, - color_hexes[COLOR_MIXER][0], - color_hexes[COLOR_MIXER][1], - color_hexes[COLOR_MIXER][2])); - } + { + SDL_FillRect(screen, &color_example_dest, + SDL_MapRGB(screen->format, + color_hexes[COLOR_MIXER][0], + color_hexes[COLOR_MIXER][1], + color_hexes[COLOR_MIXER][2])); + } - rgbtohsv(color_hexes[COLOR_MIXER][0], color_hexes[COLOR_MIXER][1], color_hexes[COLOR_MIXER][2], &h, &s, &v); + rgbtohsv(color_hexes[COLOR_MIXER][0], color_hexes[COLOR_MIXER][1], + color_hexes[COLOR_MIXER][2], &h, &s, &v); if (s == 0) - { - /* Current color is totally greyscale; set hue to "N/A" */ - h = -1; - } + { + /* Current color is totally greyscale; set hue to "N/A" */ + h = -1; + } /* Draw colors */ for (i = 0; i < NUM_MIXER_COLORS; i++) - { - float tmp_v; + { + float tmp_v; - color_mix_btn_lefts[i] = r_final.x + ((i % 3) * cell_w) + 2; - color_mix_btn_tops[i] = r_final.y + ((i / 3) * cell_h) + 2; + color_mix_btn_lefts[i] = r_final.x + ((i % 3) * cell_w) + 2; + color_mix_btn_tops[i] = r_final.y + ((i / 3) * cell_h) + 2; - dest.x = color_mix_btn_lefts[i]; - dest.y = color_mix_btn_tops[i]; - dest.w = cell_w - 2; - dest.h = cell_h - 2; + dest.x = color_mix_btn_lefts[i]; + dest.y = color_mix_btn_tops[i]; + dest.w = cell_w - 2; + dest.h = cell_h - 2; - tmp_v = mixer_hsv[i][2]; - if (tmp_v >= 0.05) - tmp_v -= 0.05; + tmp_v = mixer_hsv[i][2]; + if (tmp_v >= 0.05) + tmp_v -= 0.05; - hsvtorgb(mixer_hsv[i][0], mixer_hsv[i][1], tmp_v, &r, &g, &b); - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, r, g, b)); - } + hsvtorgb(mixer_hsv[i][0], mixer_hsv[i][1], tmp_v, &r, &g, &b); + SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, r, g, b)); + } /* Draw "Undo" & "Redo" buttons */ @@ -23809,8 +25333,13 @@ static int do_color_mix(void) dest.y = color_mix_btn_tops[COLOR_MIXER_BTN_CLEAR]; SDL_BlitSurface(img_erase, NULL, screen, &dest); - dest.x = color_mix_btn_lefts[COLOR_MIXER_BTN_CLEAR] + (img_back->w - img_mixerlabel_clear->w) / 2; - dest.y = color_mix_btn_tops[COLOR_MIXER_BTN_CLEAR] + img_back->h - img_mixerlabel_clear->h; + dest.x = + color_mix_btn_lefts[COLOR_MIXER_BTN_CLEAR] + (img_back->w - + img_mixerlabel_clear->w) / + 2; + dest.y = + color_mix_btn_tops[COLOR_MIXER_BTN_CLEAR] + img_back->h - + img_mixerlabel_clear->h; SDL_BlitSurface(img_mixerlabel_clear, NULL, screen, &dest); @@ -23823,8 +25352,12 @@ static int do_color_mix(void) dest.y = color_mix_btn_tops[COLOR_MIXER_BTN_BACK]; SDL_BlitSurface(img_back, NULL, screen, &dest); - dest.x = color_mix_btn_lefts[COLOR_MIXER_BTN_BACK] + (img_back->w - img_openlabels_back->w) / 2; - dest.y = color_mix_btn_tops[COLOR_MIXER_BTN_BACK] + img_back->h - img_openlabels_back->h; + dest.x = + color_mix_btn_lefts[COLOR_MIXER_BTN_BACK] + (img_back->w - + img_openlabels_back->w) / 2; + dest.y = + color_mix_btn_tops[COLOR_MIXER_BTN_BACK] + img_back->h - + img_openlabels_back->h; SDL_BlitSurface(img_openlabels_back, NULL, screen, &dest); /* Show "OK" button */ @@ -23833,26 +25366,26 @@ static int do_color_mix(void) color_mix_btn_tops[COLOR_MIXER_BTN_USE] = r_final.y + (cell_h * 3) + 2; if (!color_mixer_reset) - { - /* Only draw "OK" button when we can accept! */ - dest.x = color_mix_btn_lefts[COLOR_MIXER_BTN_USE]; - dest.y = color_mix_btn_tops[COLOR_MIXER_BTN_USE]; - SDL_BlitSurface(img_yes, NULL, screen, &dest); - } + { + /* Only draw "OK" button when we can accept! */ + dest.x = color_mix_btn_lefts[COLOR_MIXER_BTN_USE]; + dest.y = color_mix_btn_tops[COLOR_MIXER_BTN_USE]; + SDL_BlitSurface(img_yes, NULL, screen, &dest); + } SDL_Flip(screen); if (color_mixer_reset) - { - for (i = 0; i < NUM_MIXER_COLORS; i++) - color_mixer_color_counts[i] = 0; - } + { + for (i = 0; i < NUM_MIXER_COLORS; i++) + color_mixer_color_counts[i] = 0; + } else - { - new_r = color_hexes[COLOR_MIXER][0]; - new_g = color_hexes[COLOR_MIXER][1]; - new_b = color_hexes[COLOR_MIXER][2]; - } + { + new_r = color_hexes[COLOR_MIXER][0]; + new_g = color_hexes[COLOR_MIXER][1]; + new_b = color_hexes[COLOR_MIXER][2]; + } @@ -23864,310 +25397,330 @@ static int do_color_mix(void) /* Let the user pick a color, or go back: */ do + { + while (SDL_PollEvent(&event)) { - while (SDL_PollEvent(&event)) + if (event.type == SDL_QUIT) + { + chose = 0; + done = 1; + } + else if (event.type == SDL_WINDOWEVENT) + { + handle_active(&event); + } + else if (event.type == SDL_KEYUP) + { + key = event.key.keysym.sym; + + handle_keymouse(key, SDL_KEYUP, 24, NULL, NULL); + } + else if (event.type == SDL_KEYDOWN) + { + key = event.key.keysym.sym; + + handle_keymouse(key, SDL_KEYDOWN, 24, NULL, NULL); + + if (key == SDLK_ESCAPE) { - if (event.type == SDL_QUIT) - { - chose = 0; - done = 1; - } - else if (event.type == SDL_WINDOWEVENT) - { - handle_active(&event); - } - else if (event.type == SDL_KEYUP) - { - key = event.key.keysym.sym; + chose = 0; + done = 1; + } + } + else if (event.type == SDL_MOUSEMOTION) + { + /* Motion; change mouse pointer shape based on active UI buttons */ - handle_keymouse(key, SDL_KEYUP, 24, NULL, NULL); - } - else if (event.type == SDL_KEYDOWN) - { - key = event.key.keysym.sym; - - handle_keymouse(key, SDL_KEYDOWN, 24, NULL, NULL); - - if (key == SDLK_ESCAPE) - { - chose = 0; - done = 1; - } - } - else if (event.type == SDL_MOUSEMOTION) - { - /* Motion; change mouse pointer shape based on active UI buttons */ - - btn_clicked = -1; - for (i = 0; i < NUM_COLOR_MIXER_BTNS && btn_clicked == -1; i++) - { - if (event.button.x >= color_mix_btn_lefts[i] && - event.button.x < color_mix_btn_lefts[i] + img_back->w && - event.button.y >= color_mix_btn_tops[i] && - event.button.y < color_mix_btn_tops[i] + img_back->h) - { - btn_clicked = i; - } - } - - if ((btn_clicked >= 0 && btn_clicked < NUM_MIXER_COLORS) || - btn_clicked == COLOR_MIXER_BTN_CLEAR || - (btn_clicked == COLOR_MIXER_BTN_USE && !color_mixer_reset) || - btn_clicked == COLOR_MIXER_BTN_BACK || - (btn_clicked == COLOR_MIXER_BTN_UNDO && color_mix_cur_undo != color_mix_oldest_undo) || - (btn_clicked == COLOR_MIXER_BTN_REDO && color_mix_cur_undo != color_mix_newest_undo) - ) - { - do_setcursor(cursor_hand); - } - else - { - do_setcursor(cursor_arrow); - } - } - else if (event.type == SDL_MOUSEBUTTONUP && valid_click(event.button.button)) - { - /* Released a click, determine what action to take! */ - - /* Did they click any of the actual buttons? */ - btn_clicked = -1; - for (i = 0; i < NUM_COLOR_MIXER_BTNS && btn_clicked == -1; i++) - { - if (event.button.x >= color_mix_btn_lefts[i] && - event.button.x < color_mix_btn_lefts[i] + img_back->w && - event.button.y >= color_mix_btn_tops[i] && - event.button.y < color_mix_btn_tops[i] + img_back->h) - { - btn_clicked = i; - } - } - - if (btn_clicked >= 0 && btn_clicked < NUM_MIXER_COLORS) - { - /* Clicked a color! */ - - if (color_mixer_reset) - { - /* Starting fresh; add the chosen paint 100% */ - h = mixer_hsv[btn_clicked][0]; - s = mixer_hsv[btn_clicked][1]; - v = mixer_hsv[btn_clicked][2]; - - color_mixer_reset = 0; - - /* We can draw the "OK" button now! */ - dest.x = color_mix_btn_lefts[COLOR_MIXER_BTN_USE]; - dest.y = color_mix_btn_tops[COLOR_MIXER_BTN_USE]; - dest.w = cell_w; - dest.h = cell_h; - SDL_BlitSurface(img_yes, NULL, screen, &dest); - SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); - - color_mixer_color_counts[btn_clicked]++; - } - else - { - /* Blending in some color */ - - color_mixer_color_counts[btn_clicked]++; - - calc_color_mixer_average(&h, &s, &v); - } - - /* Record into undo buffer */ - - mixer_undo_buf[color_mix_cur_undo] = btn_clicked; - color_mix_cur_undo = (color_mix_cur_undo + 1) % NUM_COLOR_MIX_UNDO_BUFS; - if (color_mix_cur_undo == color_mix_oldest_undo) - color_mix_oldest_undo = (color_mix_oldest_undo + 1) % NUM_COLOR_MIX_UNDO_BUFS; - color_mix_newest_undo = color_mix_cur_undo; - - draw_color_mix_undo_redo(); - - - /* Show the new color */ - - hsvtorgb(h, s, v, &new_r, &new_g, &new_b); - - SDL_FillRect(screen, &color_example_dest, SDL_MapRGB(screen->format, new_r, new_g, new_b)); - SDL_UpdateRect(screen, color_example_dest.x, color_example_dest.y, color_example_dest.w, color_example_dest.h); - - /* Draw the tooltip and play a sound */ - draw_color_mixer_tooltip(); - - playsound(screen, 1, SND_BUBBLE, 1, SNDPOS_CENTER, SNDDIST_NEAR); - } - else if (btn_clicked == COLOR_MIXER_BTN_BACK) - { - /* Decided to go Back */ - - chose = 0; - done = 1; - } - else if (btn_clicked == COLOR_MIXER_BTN_USE && !color_mixer_reset) - { - /* Decided to use this color */ - chose = 1; - done = 1; - } - else if (btn_clicked == COLOR_MIXER_BTN_CLEAR) - { - /* Decided to clear the color choice & start over */ - - color_mixer_reset = 1; - - /* Wipe undo buffer */ - color_mix_cur_undo = color_mix_oldest_undo = color_mix_newest_undo = 0; - draw_color_mix_undo_redo(); - - /* Clear color usage counts */ - for (i = 0; i < NUM_MIXER_COLORS; i++) - color_mixer_color_counts[i] = 0; - - draw_tux_text(TUX_BORED, color_names[COLOR_MIXER], 1); - - /* Erase the "OK" button! */ - dest.x = color_mix_btn_lefts[COLOR_MIXER_BTN_USE]; - dest.y = color_mix_btn_tops[COLOR_MIXER_BTN_USE]; - dest.w = cell_w; - dest.h = cell_h; - - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 255, 255, 255)); - SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); - - draw_color_mixer_blank_example(); - -#ifndef NOSOUND - if (!mute && use_sound) - { - if (!Mix_Playing(0)) - { - eraser_sound = (eraser_sound + 1) % 2; - - playsound(screen, 0, SND_ERASER1 + eraser_sound, 0, SNDPOS_CENTER, SNDDIST_NEAR); - } - } -#endif - } - else if (btn_clicked == COLOR_MIXER_BTN_UNDO && color_mix_cur_undo != color_mix_oldest_undo) - { - int tot_count; - - /* Undo! */ - color_mix_cur_undo--; - if (color_mix_cur_undo < 0) - color_mix_cur_undo = NUM_COLOR_MIX_UNDO_BUFS - 1; - - color_mixer_color_counts[mixer_undo_buf[color_mix_cur_undo]]--; - - tot_count = 0; - for (i = 0; i < NUM_MIXER_COLORS; i++) - tot_count += color_mixer_color_counts[i]; - - if (tot_count > 0) - { - /* Still have some paint on there */ - calc_color_mixer_average(&h, &s, &v); - - hsvtorgb(h, s, v, &new_r, &new_g, &new_b); - - SDL_FillRect(screen, &color_example_dest, SDL_MapRGB(screen->format, new_r, new_g, new_b)); - SDL_UpdateRect(screen, color_example_dest.x, color_example_dest.y, color_example_dest.w, color_example_dest.h); - - draw_color_mixer_tooltip(); - } - else - { - /* Back to the very beginning; show blank */ - color_mixer_reset = 1; - draw_color_mixer_blank_example(); - - /* Erase the "OK" button! */ - dest.x = color_mix_btn_lefts[COLOR_MIXER_BTN_USE]; - dest.y = color_mix_btn_tops[COLOR_MIXER_BTN_USE]; - dest.w = cell_w; - dest.h = cell_h; - - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 255, 255, 255)); - SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); - - draw_tux_text(TUX_BORED, color_names[COLOR_MIXER], 1); - } - - playsound(screen, 1, SND_CLICK, 1, SNDPOS_CENTER, SNDDIST_NEAR); - draw_color_mix_undo_redo(); - } - else if (btn_clicked == COLOR_MIXER_BTN_REDO && color_mix_cur_undo != color_mix_newest_undo) - { - /* Redo! */ - color_mixer_color_counts[mixer_undo_buf[color_mix_cur_undo]]++; - - calc_color_mixer_average(&h, &s, &v); - - hsvtorgb(h, s, v, &new_r, &new_g, &new_b); - - SDL_FillRect(screen, &color_example_dest, SDL_MapRGB(screen->format, new_r, new_g, new_b)); - SDL_UpdateRect(screen, color_example_dest.x, color_example_dest.y, color_example_dest.w, color_example_dest.h); - - if (color_mixer_reset == 1) - { - /* Bringing back the first color */ - color_mixer_reset = 0; - - /* Draw "OK" */ - dest.x = color_mix_btn_lefts[COLOR_MIXER_BTN_USE]; - dest.y = color_mix_btn_tops[COLOR_MIXER_BTN_USE]; - dest.w = cell_w; - dest.h = cell_h; - SDL_BlitSurface(img_yes, NULL, screen, &dest); - SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); - } - - color_mix_cur_undo = (color_mix_cur_undo + 1) % NUM_COLOR_MIX_UNDO_BUFS; - - playsound(screen, 1, SND_CLICK, 1, SNDPOS_CENTER, SNDDIST_NEAR); - draw_color_mix_undo_redo(); - - draw_color_mixer_tooltip(); - } - } - else if (event.type == SDL_JOYAXISMOTION) - handle_joyaxismotion(event, &motioner, &val_x, &val_y); - - else if (event.type == SDL_JOYHATMOTION) - handle_joyhatmotion(event, oldpos_x, oldpos_y, &valhat_x, &valhat_y, &hatmotioner, &old_hat_ticks); - - else if (event.type == SDL_JOYBALLMOTION) - handle_joyballmotion(event, oldpos_x, oldpos_y); - - else if (event.type == SDL_JOYBUTTONDOWN || event.type == SDL_JOYBUTTONUP) - handle_joybuttonupdown(event, oldpos_x, oldpos_y); + btn_clicked = -1; + for (i = 0; i < NUM_COLOR_MIXER_BTNS && btn_clicked == -1; i++) + { + if (event.button.x >= color_mix_btn_lefts[i] && + event.button.x < color_mix_btn_lefts[i] + img_back->w && + event.button.y >= color_mix_btn_tops[i] && + event.button.y < color_mix_btn_tops[i] + img_back->h) + { + btn_clicked = i; + } } - if (motioner | hatmotioner) - handle_motioners(oldpos_x, oldpos_y, motioner, hatmotioner, old_hat_ticks, val_x, val_y, valhat_x, valhat_y); + if ((btn_clicked >= 0 && btn_clicked < NUM_MIXER_COLORS) || + btn_clicked == COLOR_MIXER_BTN_CLEAR || + (btn_clicked == COLOR_MIXER_BTN_USE && !color_mixer_reset) || + btn_clicked == COLOR_MIXER_BTN_BACK || + (btn_clicked == COLOR_MIXER_BTN_UNDO + && color_mix_cur_undo != color_mix_oldest_undo) + || (btn_clicked == COLOR_MIXER_BTN_REDO + && color_mix_cur_undo != color_mix_newest_undo)) + { + do_setcursor(cursor_hand); + } + else + { + do_setcursor(cursor_arrow); + } + } + else if (event.type == SDL_MOUSEBUTTONUP + && valid_click(event.button.button)) + { + /* Released a click, determine what action to take! */ - SDL_Delay(10); + /* Did they click any of the actual buttons? */ + btn_clicked = -1; + for (i = 0; i < NUM_COLOR_MIXER_BTNS && btn_clicked == -1; i++) + { + if (event.button.x >= color_mix_btn_lefts[i] && + event.button.x < color_mix_btn_lefts[i] + img_back->w && + event.button.y >= color_mix_btn_tops[i] && + event.button.y < color_mix_btn_tops[i] + img_back->h) + { + btn_clicked = i; + } + } + + if (btn_clicked >= 0 && btn_clicked < NUM_MIXER_COLORS) + { + /* Clicked a color! */ + + if (color_mixer_reset) + { + /* Starting fresh; add the chosen paint 100% */ + h = mixer_hsv[btn_clicked][0]; + s = mixer_hsv[btn_clicked][1]; + v = mixer_hsv[btn_clicked][2]; + + color_mixer_reset = 0; + + /* We can draw the "OK" button now! */ + dest.x = color_mix_btn_lefts[COLOR_MIXER_BTN_USE]; + dest.y = color_mix_btn_tops[COLOR_MIXER_BTN_USE]; + dest.w = cell_w; + dest.h = cell_h; + SDL_BlitSurface(img_yes, NULL, screen, &dest); + SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); + + color_mixer_color_counts[btn_clicked]++; + } + else + { + /* Blending in some color */ + + color_mixer_color_counts[btn_clicked]++; + + calc_color_mixer_average(&h, &s, &v); + } + + /* Record into undo buffer */ + + mixer_undo_buf[color_mix_cur_undo] = btn_clicked; + color_mix_cur_undo = + (color_mix_cur_undo + 1) % NUM_COLOR_MIX_UNDO_BUFS; + if (color_mix_cur_undo == color_mix_oldest_undo) + color_mix_oldest_undo = + (color_mix_oldest_undo + 1) % NUM_COLOR_MIX_UNDO_BUFS; + color_mix_newest_undo = color_mix_cur_undo; + + draw_color_mix_undo_redo(); + + + /* Show the new color */ + + hsvtorgb(h, s, v, &new_r, &new_g, &new_b); + + SDL_FillRect(screen, &color_example_dest, + SDL_MapRGB(screen->format, new_r, new_g, new_b)); + SDL_UpdateRect(screen, color_example_dest.x, color_example_dest.y, + color_example_dest.w, color_example_dest.h); + + /* Draw the tooltip and play a sound */ + draw_color_mixer_tooltip(); + + playsound(screen, 1, SND_BUBBLE, 1, SNDPOS_CENTER, SNDDIST_NEAR); + } + else if (btn_clicked == COLOR_MIXER_BTN_BACK) + { + /* Decided to go Back */ + + chose = 0; + done = 1; + } + else if (btn_clicked == COLOR_MIXER_BTN_USE && !color_mixer_reset) + { + /* Decided to use this color */ + chose = 1; + done = 1; + } + else if (btn_clicked == COLOR_MIXER_BTN_CLEAR) + { + /* Decided to clear the color choice & start over */ + + color_mixer_reset = 1; + + /* Wipe undo buffer */ + color_mix_cur_undo = color_mix_oldest_undo = color_mix_newest_undo = + 0; + draw_color_mix_undo_redo(); + + /* Clear color usage counts */ + for (i = 0; i < NUM_MIXER_COLORS; i++) + color_mixer_color_counts[i] = 0; + + draw_tux_text(TUX_BORED, color_names[COLOR_MIXER], 1); + + /* Erase the "OK" button! */ + dest.x = color_mix_btn_lefts[COLOR_MIXER_BTN_USE]; + dest.y = color_mix_btn_tops[COLOR_MIXER_BTN_USE]; + dest.w = cell_w; + dest.h = cell_h; + + SDL_FillRect(screen, &dest, + SDL_MapRGB(screen->format, 255, 255, 255)); + SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); + + draw_color_mixer_blank_example(); + +#ifndef NOSOUND + if (!mute && use_sound) + { + if (!Mix_Playing(0)) + { + eraser_sound = (eraser_sound + 1) % 2; + + playsound(screen, 0, SND_ERASER1 + eraser_sound, 0, + SNDPOS_CENTER, SNDDIST_NEAR); + } + } +#endif + } + else if (btn_clicked == COLOR_MIXER_BTN_UNDO + && color_mix_cur_undo != color_mix_oldest_undo) + { + int tot_count; + + /* Undo! */ + color_mix_cur_undo--; + if (color_mix_cur_undo < 0) + color_mix_cur_undo = NUM_COLOR_MIX_UNDO_BUFS - 1; + + color_mixer_color_counts[mixer_undo_buf[color_mix_cur_undo]]--; + + tot_count = 0; + for (i = 0; i < NUM_MIXER_COLORS; i++) + tot_count += color_mixer_color_counts[i]; + + if (tot_count > 0) + { + /* Still have some paint on there */ + calc_color_mixer_average(&h, &s, &v); + + hsvtorgb(h, s, v, &new_r, &new_g, &new_b); + + SDL_FillRect(screen, &color_example_dest, + SDL_MapRGB(screen->format, new_r, new_g, new_b)); + SDL_UpdateRect(screen, color_example_dest.x, color_example_dest.y, + color_example_dest.w, color_example_dest.h); + + draw_color_mixer_tooltip(); + } + else + { + /* Back to the very beginning; show blank */ + color_mixer_reset = 1; + draw_color_mixer_blank_example(); + + /* Erase the "OK" button! */ + dest.x = color_mix_btn_lefts[COLOR_MIXER_BTN_USE]; + dest.y = color_mix_btn_tops[COLOR_MIXER_BTN_USE]; + dest.w = cell_w; + dest.h = cell_h; + + SDL_FillRect(screen, &dest, + SDL_MapRGB(screen->format, 255, 255, 255)); + SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); + + draw_tux_text(TUX_BORED, color_names[COLOR_MIXER], 1); + } + + playsound(screen, 1, SND_CLICK, 1, SNDPOS_CENTER, SNDDIST_NEAR); + draw_color_mix_undo_redo(); + } + else if (btn_clicked == COLOR_MIXER_BTN_REDO + && color_mix_cur_undo != color_mix_newest_undo) + { + /* Redo! */ + color_mixer_color_counts[mixer_undo_buf[color_mix_cur_undo]]++; + + calc_color_mixer_average(&h, &s, &v); + + hsvtorgb(h, s, v, &new_r, &new_g, &new_b); + + SDL_FillRect(screen, &color_example_dest, + SDL_MapRGB(screen->format, new_r, new_g, new_b)); + SDL_UpdateRect(screen, color_example_dest.x, color_example_dest.y, + color_example_dest.w, color_example_dest.h); + + if (color_mixer_reset == 1) + { + /* Bringing back the first color */ + color_mixer_reset = 0; + + /* Draw "OK" */ + dest.x = color_mix_btn_lefts[COLOR_MIXER_BTN_USE]; + dest.y = color_mix_btn_tops[COLOR_MIXER_BTN_USE]; + dest.w = cell_w; + dest.h = cell_h; + SDL_BlitSurface(img_yes, NULL, screen, &dest); + SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); + } + + color_mix_cur_undo = + (color_mix_cur_undo + 1) % NUM_COLOR_MIX_UNDO_BUFS; + + playsound(screen, 1, SND_CLICK, 1, SNDPOS_CENTER, SNDDIST_NEAR); + draw_color_mix_undo_redo(); + + draw_color_mixer_tooltip(); + } + } + else if (event.type == SDL_JOYAXISMOTION) + handle_joyaxismotion(event, &motioner, &val_x, &val_y); + + else if (event.type == SDL_JOYHATMOTION) + handle_joyhatmotion(event, oldpos_x, oldpos_y, &valhat_x, &valhat_y, + &hatmotioner, &old_hat_ticks); + + else if (event.type == SDL_JOYBALLMOTION) + handle_joyballmotion(event, oldpos_x, oldpos_y); + + else if (event.type == SDL_JOYBUTTONDOWN + || event.type == SDL_JOYBUTTONUP) + handle_joybuttonupdown(event, oldpos_x, oldpos_y); } + + if (motioner | hatmotioner) + handle_motioners(oldpos_x, oldpos_y, motioner, hatmotioner, + old_hat_ticks, val_x, val_y, valhat_x, valhat_y); + + SDL_Delay(10); + } while (!done); /* Set the new color: */ if (chose) - { - color_hexes[COLOR_MIXER][0] = new_r; - color_hexes[COLOR_MIXER][1] = new_g; - color_hexes[COLOR_MIXER][2] = new_b; + { + color_hexes[COLOR_MIXER][0] = new_r; + color_hexes[COLOR_MIXER][1] = new_g; + color_hexes[COLOR_MIXER][2] = new_b; - /* Re-render color mixer to show the current color it contains: */ - render_color_button(COLOR_MIXER, NULL, img_color_mix); - } + /* Re-render color mixer to show the current color it contains: */ + render_color_button(COLOR_MIXER, NULL, img_color_mix); + } else - { - color_mixer_reset = old_color_mixer_reset; - } + { + color_mixer_reset = old_color_mixer_reset; + } /* Remove the prompt: */ @@ -24184,7 +25737,8 @@ static int do_color_mix(void) * Draw a pattern over the color mixer sample, * for when no color has been chosen. */ -static void draw_color_mixer_blank_example(void) { +static void draw_color_mixer_blank_example(void) +{ int w; SDL_Rect dest; @@ -24192,17 +25746,17 @@ static void draw_color_mixer_blank_example(void) { SDL_MapRGB(screen->format, 192, 192, 192)); for (w = 0; w < color_example_dest.w; w += 4) - { - dest.x = color_example_dest.x + w; - dest.y = color_example_dest.y; - dest.w = 2; - dest.h = color_example_dest.h; + { + dest.x = color_example_dest.x + w; + dest.y = color_example_dest.y; + dest.w = 2; + dest.h = color_example_dest.h; - SDL_FillRect(screen, &dest, - SDL_MapRGB(screen->format, 128, 128, 128)); - } + SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 128, 128, 128)); + } - SDL_UpdateRect(screen, color_example_dest.x, color_example_dest.y, color_example_dest.w, color_example_dest.h); + SDL_UpdateRect(screen, color_example_dest.x, color_example_dest.y, + color_example_dest.w, color_example_dest.h); } @@ -24211,7 +25765,8 @@ static void draw_color_mixer_blank_example(void) { * proportions, and calculate an HSV value for the * final color */ -static void calc_color_mixer_average(float * out_h, float * out_s, float * out_v) { +static void calc_color_mixer_average(float *out_h, float *out_s, float *out_v) +{ float h, s, v; float circ_mean_avg_sin, circ_mean_avg_cos; int i, tot_count, tot_count_hue; @@ -24222,38 +25777,40 @@ static void calc_color_mixer_average(float * out_h, float * out_s, float * out_v sat = val = 0.0; for (i = 0; i < NUM_MIXER_COLORS; i++) + { + tot_count += color_mixer_color_counts[i]; + + if (mixer_hsv[i][0] != -1) { - tot_count += color_mixer_color_counts[i]; + tot_count_hue += color_mixer_color_counts[i]; - if (mixer_hsv[i][0] != -1) - { - tot_count_hue += color_mixer_color_counts[i]; - - circ_mean_avg_sin += (sin(mixer_hsv[i][0] * M_PI / 180.0) * color_mixer_color_counts[i]); - circ_mean_avg_cos += (cos(mixer_hsv[i][0] * M_PI / 180.0) * color_mixer_color_counts[i]); - } - - sat += mixer_hsv[i][1] * (color_mixer_color_counts[i]); - val += mixer_hsv[i][2] * (color_mixer_color_counts[i]); + circ_mean_avg_sin += + (sin(mixer_hsv[i][0] * M_PI / 180.0) * color_mixer_color_counts[i]); + circ_mean_avg_cos += + (cos(mixer_hsv[i][0] * M_PI / 180.0) * color_mixer_color_counts[i]); } + sat += mixer_hsv[i][1] * (color_mixer_color_counts[i]); + val += mixer_hsv[i][2] * (color_mixer_color_counts[i]); + } + if (tot_count_hue == 0) - { - /* None of the colors we mixed has any hue! */ - h = -1; - } + { + /* None of the colors we mixed has any hue! */ + h = -1; + } else - { - /* Average all the hues we have */ - circ_mean_avg_sin /= tot_count_hue; - circ_mean_avg_cos /= tot_count_hue; + { + /* Average all the hues we have */ + circ_mean_avg_sin /= tot_count_hue; + circ_mean_avg_cos /= tot_count_hue; - h = atan2(circ_mean_avg_sin, circ_mean_avg_cos) * 180.0 / M_PI; - if (h < 0.0) - h += 360.0; - else if (h >= 360.0) - h -= 360.0; - } + h = atan2(circ_mean_avg_sin, circ_mean_avg_cos) * 180.0 / M_PI; + if (h < 0.0) + h += 360.0; + else if (h >= 360.0) + h -= 360.0; + } /* Average the saturation and values */ @@ -24270,9 +25827,10 @@ static void calc_color_mixer_average(float * out_h, float * out_s, float * out_v * making the buttons appear clickable ("up") or not ("off"), * depending on the state of the color mixer's undo buffer */ -static void draw_color_mix_undo_redo(void) { +static void draw_color_mix_undo_redo(void) +{ SDL_Rect dest; - SDL_Surface * icon_label_color, * tmp_surf; + SDL_Surface *icon_label_color, *tmp_surf; /* Show "Undo" button */ @@ -24280,17 +25838,19 @@ static void draw_color_mix_undo_redo(void) { dest.y = color_mix_btn_tops[COLOR_MIXER_BTN_UNDO]; if (color_mix_cur_undo != color_mix_oldest_undo) - { - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); - icon_label_color = img_black; - } + { + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + icon_label_color = img_black; + } else - { - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - icon_label_color = img_grey; - } + { + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + icon_label_color = img_grey; + } - dest.x = color_mix_btn_lefts[COLOR_MIXER_BTN_UNDO] + (img_back->w - img_tools[TOOL_UNDO]->w) / 2; + dest.x = + color_mix_btn_lefts[COLOR_MIXER_BTN_UNDO] + (img_back->w - + img_tools[TOOL_UNDO]->w) / 2; dest.y = color_mix_btn_tops[COLOR_MIXER_BTN_UNDO]; tmp_surf = SDL_DisplayFormatAlpha(img_tools[TOOL_UNDO]); @@ -24298,15 +25858,22 @@ static void draw_color_mix_undo_redo(void) { SDL_BlitSurface(tmp_surf, NULL, screen, &dest); SDL_FreeSurface(tmp_surf); - dest.x = color_mix_btn_lefts[COLOR_MIXER_BTN_UNDO] + (img_back->w - img_tool_names[TOOL_UNDO]->w) / 2; - dest.y = color_mix_btn_tops[COLOR_MIXER_BTN_UNDO] + img_back->h - img_tool_names[TOOL_UNDO]->h; + dest.x = + color_mix_btn_lefts[COLOR_MIXER_BTN_UNDO] + (img_back->w - + img_tool_names[TOOL_UNDO]->w) + / 2; + dest.y = + color_mix_btn_tops[COLOR_MIXER_BTN_UNDO] + img_back->h - + img_tool_names[TOOL_UNDO]->h; tmp_surf = SDL_DisplayFormatAlpha(img_tool_names[TOOL_UNDO]); SDL_BlitSurface(icon_label_color, NULL, tmp_surf, NULL); SDL_BlitSurface(tmp_surf, NULL, screen, &dest); SDL_FreeSurface(tmp_surf); - SDL_UpdateRect(screen, color_mix_btn_lefts[COLOR_MIXER_BTN_UNDO], color_mix_btn_tops[COLOR_MIXER_BTN_UNDO], img_back->w, img_back->h); + SDL_UpdateRect(screen, color_mix_btn_lefts[COLOR_MIXER_BTN_UNDO], + color_mix_btn_tops[COLOR_MIXER_BTN_UNDO], img_back->w, + img_back->h); /* Show "Redo" button */ @@ -24315,17 +25882,19 @@ static void draw_color_mix_undo_redo(void) { dest.y = color_mix_btn_tops[COLOR_MIXER_BTN_REDO]; if (color_mix_cur_undo != color_mix_newest_undo) - { - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); - icon_label_color = img_black; - } + { + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + icon_label_color = img_black; + } else - { - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - icon_label_color = img_grey; - } + { + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + icon_label_color = img_grey; + } - dest.x = color_mix_btn_lefts[COLOR_MIXER_BTN_REDO] + (img_back->w - img_tools[TOOL_REDO]->w) / 2; + dest.x = + color_mix_btn_lefts[COLOR_MIXER_BTN_REDO] + (img_back->w - + img_tools[TOOL_REDO]->w) / 2; dest.y = color_mix_btn_tops[COLOR_MIXER_BTN_REDO]; tmp_surf = SDL_DisplayFormatAlpha(img_tools[TOOL_REDO]); @@ -24333,131 +25902,168 @@ static void draw_color_mix_undo_redo(void) { SDL_BlitSurface(tmp_surf, NULL, screen, &dest); SDL_FreeSurface(tmp_surf); - dest.x = color_mix_btn_lefts[COLOR_MIXER_BTN_REDO] + (img_back->w - img_tool_names[TOOL_REDO]->w) / 2; - dest.y = color_mix_btn_tops[COLOR_MIXER_BTN_REDO] + img_back->h - img_tool_names[TOOL_REDO]->h; + dest.x = + color_mix_btn_lefts[COLOR_MIXER_BTN_REDO] + (img_back->w - + img_tool_names[TOOL_REDO]->w) + / 2; + dest.y = + color_mix_btn_tops[COLOR_MIXER_BTN_REDO] + img_back->h - + img_tool_names[TOOL_REDO]->h; tmp_surf = SDL_DisplayFormatAlpha(img_tool_names[TOOL_REDO]); SDL_BlitSurface(icon_label_color, NULL, tmp_surf, NULL); SDL_BlitSurface(tmp_surf, NULL, screen, &dest); SDL_FreeSurface(tmp_surf); - SDL_UpdateRect(screen, color_mix_btn_lefts[COLOR_MIXER_BTN_REDO], color_mix_btn_tops[COLOR_MIXER_BTN_REDO], img_back->w, img_back->h); + SDL_UpdateRect(screen, color_mix_btn_lefts[COLOR_MIXER_BTN_REDO], + color_mix_btn_tops[COLOR_MIXER_BTN_REDO], img_back->w, + img_back->h); } /** * Show a tooltip describing the color the user has mixed */ -static void draw_color_mixer_tooltip(void) { +static void draw_color_mixer_tooltip(void) +{ int i, num_colors_used, tot_count; char tip_txt[1024]; char tip_txt_proportions[NUM_MIXER_COLORS][64]; - int used_colors_color[NUM_MIXER_COLORS], used_colors_amount[NUM_MIXER_COLORS]; + int used_colors_color[NUM_MIXER_COLORS], + used_colors_amount[NUM_MIXER_COLORS]; num_colors_used = 0; tot_count = 0; for (i = 0; i < NUM_MIXER_COLORS; i++) + { + if (color_mixer_color_counts[i]) { - if (color_mixer_color_counts[i]) - { - used_colors_color[num_colors_used] = i; - used_colors_amount[num_colors_used] = color_mixer_color_counts[i]; - num_colors_used++; + used_colors_color[num_colors_used] = i; + used_colors_amount[num_colors_used] = color_mixer_color_counts[i]; + num_colors_used++; - tot_count += color_mixer_color_counts[i]; - } + tot_count += color_mixer_color_counts[i]; } + } if (num_colors_used == 1) + { + if (used_colors_amount[0] == 1) { - if (used_colors_amount[0] == 1) - { - snprintf(tip_txt, sizeof(tip_txt), gettext(color_mixer_color_tips[0]), - /* Color mixer; e.g., "Your color is entirely grey." */ gettext("entirely"), - gettext(color_mixer_color_names[used_colors_color[0]])); - } - else - { - snprintf(tip_txt_proportions[0], sizeof(tip_txt_proportions[0]), - "%1$s (%2$d/%3$d)", - gettext("entirely"), - used_colors_amount[0], used_colors_amount[0]); - - snprintf(tip_txt, sizeof(tip_txt), gettext(color_mixer_color_tips[0]), - tip_txt_proportions[0], - gettext(color_mixer_color_names[used_colors_color[0]])); - } + snprintf(tip_txt, sizeof(tip_txt), gettext(color_mixer_color_tips[0]), + /* Color mixer; e.g., "Your color is entirely grey." */ + gettext("entirely"), + gettext(color_mixer_color_names[used_colors_color[0]])); } + else + { + snprintf(tip_txt_proportions[0], sizeof(tip_txt_proportions[0]), + "%1$s (%2$d/%3$d)", + gettext("entirely"), + used_colors_amount[0], used_colors_amount[0]); + + snprintf(tip_txt, sizeof(tip_txt), gettext(color_mixer_color_tips[0]), + tip_txt_proportions[0], + gettext(color_mixer_color_names[used_colors_color[0]])); + } + } else + { + for (i = 0; i < num_colors_used; i++) { - for (i = 0; i < num_colors_used; i++) - { - int factor, best_factor; + int factor, best_factor; - best_factor = 0; - for (factor = 2; factor <= used_colors_amount[i]; factor++) - { - if ((used_colors_amount[i] % factor) == 0 && (tot_count % factor) == 0) - best_factor = factor; - } + best_factor = 0; + for (factor = 2; factor <= used_colors_amount[i]; factor++) + { + if ((used_colors_amount[i] % factor) == 0 + && (tot_count % factor) == 0) + best_factor = factor; + } - if (best_factor) - { - snprintf(tip_txt_proportions[i], sizeof(tip_txt_proportions[i]), - "%d/%d (%d/%d)", - used_colors_amount[i], tot_count, - (int) (used_colors_amount[i] / best_factor), (int) (tot_count / best_factor)); - } - else - { - snprintf(tip_txt_proportions[i], sizeof(tip_txt_proportions[i]), - "%d/%d", used_colors_amount[i], tot_count); - } - } - - tip_txt[0] = '\0'; /* Just in case! */ - if (num_colors_used == 2) - { - snprintf(tip_txt, sizeof(tip_txt), gettext(color_mixer_color_tips[num_colors_used - 1]), - tip_txt_proportions[0], gettext(color_mixer_color_names[used_colors_color[0]]), - tip_txt_proportions[1], gettext(color_mixer_color_names[used_colors_color[1]])); - } - else if (num_colors_used == 3) - { - snprintf(tip_txt, sizeof(tip_txt), gettext(color_mixer_color_tips[num_colors_used - 1]), - tip_txt_proportions[0], gettext(color_mixer_color_names[used_colors_color[0]]), - tip_txt_proportions[1], gettext(color_mixer_color_names[used_colors_color[1]]), - tip_txt_proportions[2], gettext(color_mixer_color_names[used_colors_color[2]])); - } - else if (num_colors_used == 4) - { - snprintf(tip_txt, sizeof(tip_txt), gettext(color_mixer_color_tips[num_colors_used - 1]), - tip_txt_proportions[0], gettext(color_mixer_color_names[used_colors_color[0]]), - tip_txt_proportions[1], gettext(color_mixer_color_names[used_colors_color[1]]), - tip_txt_proportions[2], gettext(color_mixer_color_names[used_colors_color[2]]), - tip_txt_proportions[3], gettext(color_mixer_color_names[used_colors_color[3]])); - } - else if (num_colors_used == 5) - { - snprintf(tip_txt, sizeof(tip_txt), gettext(color_mixer_color_tips[num_colors_used - 1]), - tip_txt_proportions[0], gettext(color_mixer_color_names[used_colors_color[0]]), - tip_txt_proportions[1], gettext(color_mixer_color_names[used_colors_color[1]]), - tip_txt_proportions[2], gettext(color_mixer_color_names[used_colors_color[2]]), - tip_txt_proportions[3], gettext(color_mixer_color_names[used_colors_color[3]]), - tip_txt_proportions[4], gettext(color_mixer_color_names[used_colors_color[4]])); - } - else if (num_colors_used == 6) - { - snprintf(tip_txt, sizeof(tip_txt), gettext(color_mixer_color_tips[num_colors_used - 1]), - tip_txt_proportions[0], gettext(color_mixer_color_names[used_colors_color[0]]), - tip_txt_proportions[1], gettext(color_mixer_color_names[used_colors_color[1]]), - tip_txt_proportions[2], gettext(color_mixer_color_names[used_colors_color[2]]), - tip_txt_proportions[3], gettext(color_mixer_color_names[used_colors_color[3]]), - tip_txt_proportions[4], gettext(color_mixer_color_names[used_colors_color[4]]), - tip_txt_proportions[5], gettext(color_mixer_color_names[used_colors_color[5]])); - } + if (best_factor) + { + snprintf(tip_txt_proportions[i], sizeof(tip_txt_proportions[i]), + "%d/%d (%d/%d)", + used_colors_amount[i], tot_count, + (int) (used_colors_amount[i] / best_factor), + (int) (tot_count / best_factor)); + } + else + { + snprintf(tip_txt_proportions[i], sizeof(tip_txt_proportions[i]), + "%d/%d", used_colors_amount[i], tot_count); + } } + tip_txt[0] = '\0'; /* Just in case! */ + if (num_colors_used == 2) + { + snprintf(tip_txt, sizeof(tip_txt), + gettext(color_mixer_color_tips[num_colors_used - 1]), + tip_txt_proportions[0], + gettext(color_mixer_color_names[used_colors_color[0]]), + tip_txt_proportions[1], + gettext(color_mixer_color_names[used_colors_color[1]])); + } + else if (num_colors_used == 3) + { + snprintf(tip_txt, sizeof(tip_txt), + gettext(color_mixer_color_tips[num_colors_used - 1]), + tip_txt_proportions[0], + gettext(color_mixer_color_names[used_colors_color[0]]), + tip_txt_proportions[1], + gettext(color_mixer_color_names[used_colors_color[1]]), + tip_txt_proportions[2], + gettext(color_mixer_color_names[used_colors_color[2]])); + } + else if (num_colors_used == 4) + { + snprintf(tip_txt, sizeof(tip_txt), + gettext(color_mixer_color_tips[num_colors_used - 1]), + tip_txt_proportions[0], + gettext(color_mixer_color_names[used_colors_color[0]]), + tip_txt_proportions[1], + gettext(color_mixer_color_names[used_colors_color[1]]), + tip_txt_proportions[2], + gettext(color_mixer_color_names[used_colors_color[2]]), + tip_txt_proportions[3], + gettext(color_mixer_color_names[used_colors_color[3]])); + } + else if (num_colors_used == 5) + { + snprintf(tip_txt, sizeof(tip_txt), + gettext(color_mixer_color_tips[num_colors_used - 1]), + tip_txt_proportions[0], + gettext(color_mixer_color_names[used_colors_color[0]]), + tip_txt_proportions[1], + gettext(color_mixer_color_names[used_colors_color[1]]), + tip_txt_proportions[2], + gettext(color_mixer_color_names[used_colors_color[2]]), + tip_txt_proportions[3], + gettext(color_mixer_color_names[used_colors_color[3]]), + tip_txt_proportions[4], + gettext(color_mixer_color_names[used_colors_color[4]])); + } + else if (num_colors_used == 6) + { + snprintf(tip_txt, sizeof(tip_txt), + gettext(color_mixer_color_tips[num_colors_used - 1]), + tip_txt_proportions[0], + gettext(color_mixer_color_names[used_colors_color[0]]), + tip_txt_proportions[1], + gettext(color_mixer_color_names[used_colors_color[1]]), + tip_txt_proportions[2], + gettext(color_mixer_color_names[used_colors_color[2]]), + tip_txt_proportions[3], + gettext(color_mixer_color_names[used_colors_color[3]]), + tip_txt_proportions[4], + gettext(color_mixer_color_names[used_colors_color[4]]), + tip_txt_proportions[5], + gettext(color_mixer_color_names[used_colors_color[5]])); + } + } + draw_tux_text(TUX_GREAT, tip_txt, 1); } @@ -24468,7 +26074,9 @@ static void draw_color_mixer_tooltip(void) { * @param int the_color - the color within the palette (e.g., COLOR_PICKER) (its RGB values will be grabbed via global color_hexes[], and the new button will be rendered into the appropriate img_color_btns[]) * @param SDL_Surface * decoration - a decoration bitmap to be applied to the button (or NULL if none) (e.g., the color picker rainbow that appears around the color picker button's paintwell) */ -static void render_color_button(int the_color, SDL_Surface * decoration, SDL_Surface * icon) { +static void render_color_button(int the_color, SDL_Surface * decoration, + SDL_Surface * icon) +{ SDL_Surface *tmp_btn_up, *tmp_btn_down; SDL_Rect dest; double rh, gh, bh; @@ -24479,7 +26087,7 @@ static void render_color_button(int the_color, SDL_Surface * decoration, SDL_Sur tmp_btn_up = thumbnail(img_btn_up, color_button_w, color_button_h, 0); tmp_btn_down = thumbnail(img_btn_down, color_button_w, color_button_h, 0); - img_color_btn_off = thumbnail(img_btn_off, color_button_w, color_button_h, 0); /* FIXME: Need to free? */ + img_color_btn_off = thumbnail(img_btn_off, color_button_w, color_button_h, 0); /* FIXME: Need to free? */ getpixel_tmp_btn_up = getpixels[tmp_btn_up->format->BytesPerPixel]; getpixel_tmp_btn_down = getpixels[tmp_btn_down->format->BytesPerPixel]; @@ -24493,52 +26101,66 @@ static void render_color_button(int the_color, SDL_Surface * decoration, SDL_Sur SDL_LockSurface(img_color_btns[the_color + NUM_COLORS]); for (y = 0; y < tmp_btn_up->h; y++) + { + for (x = 0; x < tmp_btn_up->w; x++) { - for (x = 0; x < tmp_btn_up->w; x++) - { - double ru, gu, bu, rd, gd, bd, aa; - Uint8 a, r, g, b; + double ru, gu, bu, rd, gd, bd, aa; + Uint8 a, r, g, b; - SDL_GetRGB(getpixel_tmp_btn_up(tmp_btn_up, x, y), tmp_btn_up->format, &r, &g, &b); + SDL_GetRGB(getpixel_tmp_btn_up(tmp_btn_up, x, y), tmp_btn_up->format, + &r, &g, &b); - ru = sRGB_to_linear_table[r]; - gu = sRGB_to_linear_table[g]; - bu = sRGB_to_linear_table[b]; - SDL_GetRGB(getpixel_tmp_btn_down(tmp_btn_down, x, y), tmp_btn_down->format, &r, &g, &b); + ru = sRGB_to_linear_table[r]; + gu = sRGB_to_linear_table[g]; + bu = sRGB_to_linear_table[b]; + SDL_GetRGB(getpixel_tmp_btn_down(tmp_btn_down, x, y), + tmp_btn_down->format, &r, &g, &b); - rd = sRGB_to_linear_table[r]; - gd = sRGB_to_linear_table[g]; - bd = sRGB_to_linear_table[b]; - SDL_GetRGBA(getpixel_img_paintwell(img_paintwell, x, y), img_paintwell->format, &r, &g, &b, &a); + rd = sRGB_to_linear_table[r]; + gd = sRGB_to_linear_table[g]; + bd = sRGB_to_linear_table[b]; + SDL_GetRGBA(getpixel_img_paintwell(img_paintwell, x, y), + img_paintwell->format, &r, &g, &b, &a); - aa = a / 255.0; + aa = a / 255.0; - if (decoration != NULL) - { - putpixels[img_color_btns[the_color]->format->BytesPerPixel] - (img_color_btns[the_color], x, y, - getpixels[decoration->format->BytesPerPixel] (decoration, x, y)); - putpixels[img_color_btns[the_color + NUM_COLORS]->format->BytesPerPixel] - (img_color_btns[the_color + NUM_COLORS], x, y, - getpixels[decoration->format->BytesPerPixel] (decoration, x, y)); - } + if (decoration != NULL) + { + putpixels[img_color_btns[the_color]->format->BytesPerPixel] + (img_color_btns[the_color], x, y, + getpixels[decoration->format->BytesPerPixel] (decoration, x, y)); + putpixels[img_color_btns[the_color + NUM_COLORS]->format-> + BytesPerPixel] (img_color_btns[the_color + NUM_COLORS], x, + y, + getpixels[decoration->format-> + BytesPerPixel] (decoration, x, + y)); + } - if (a == 255) - { - putpixels[img_color_btns[the_color]->format->BytesPerPixel] - (img_color_btns[the_color], x, y, - SDL_MapRGB(img_color_btns[the_color]->format, - linear_to_sRGB(rh * aa + ru * (1.0 - aa)), - linear_to_sRGB(gh * aa + gu * (1.0 - aa)), linear_to_sRGB(bh * aa + bu * (1.0 - aa)))); + if (a == 255) + { + putpixels[img_color_btns[the_color]->format->BytesPerPixel] + (img_color_btns[the_color], x, y, + SDL_MapRGB(img_color_btns[the_color]->format, + linear_to_sRGB(rh * aa + ru * (1.0 - aa)), + linear_to_sRGB(gh * aa + gu * (1.0 - aa)), + linear_to_sRGB(bh * aa + bu * (1.0 - aa)))); - putpixels[img_color_btns[the_color + NUM_COLORS]->format->BytesPerPixel] - (img_color_btns[the_color + NUM_COLORS], x, y, - SDL_MapRGB(img_color_btns[the_color + NUM_COLORS]->format, - linear_to_sRGB(rh * aa + rd * (1.0 - aa)), - linear_to_sRGB(gh * aa + gd * (1.0 - aa)), linear_to_sRGB(bh * aa + bd * (1.0 - aa)))); - } - } + putpixels[img_color_btns[the_color + NUM_COLORS]->format-> + BytesPerPixel] (img_color_btns[the_color + NUM_COLORS], x, + y, + SDL_MapRGB(img_color_btns + [the_color + NUM_COLORS]->format, + linear_to_sRGB(rh * aa + + rd * (1.0 - aa)), + linear_to_sRGB(gh * aa + + gd * (1.0 - aa)), + linear_to_sRGB(bh * aa + + bd * (1.0 - + aa)))); + } } + } SDL_UnlockSurface(img_color_btns[the_color]); SDL_UnlockSurface(img_color_btns[the_color + NUM_COLORS]); @@ -24547,18 +26169,19 @@ static void render_color_button(int the_color, SDL_Surface * decoration, SDL_Sur SDL_FreeSurface(tmp_btn_down); if (icon != NULL) - { - /* Apply pipette to color selector entries */ - dest.x = (img_color_btns[the_color]->w - icon->w) / 2; - dest.y = (img_color_btns[the_color]->h - icon->h) / 2; - dest.w = icon->w; - dest.h = icon->h; - SDL_BlitSurface(icon, NULL, img_color_btns[the_color], &dest); + { + /* Apply pipette to color selector entries */ + dest.x = (img_color_btns[the_color]->w - icon->w) / 2; + dest.y = (img_color_btns[the_color]->h - icon->h) / 2; + dest.w = icon->w; + dest.h = icon->h; + SDL_BlitSurface(icon, NULL, img_color_btns[the_color], &dest); - dest.x = (img_color_btns[the_color + NUM_COLORS]->w - icon->w) / 2; - dest.y = (img_color_btns[the_color + NUM_COLORS]->h - icon->h) / 2; - SDL_BlitSurface(icon, NULL, img_color_btns[the_color + NUM_COLORS], &dest); - } + dest.x = (img_color_btns[the_color + NUM_COLORS]->w - icon->w) / 2; + dest.y = (img_color_btns[the_color + NUM_COLORS]->h - icon->h) / 2; + SDL_BlitSurface(icon, NULL, img_color_btns[the_color + NUM_COLORS], + &dest); + } } @@ -24568,18 +26191,17 @@ static void render_color_button(int the_color, SDL_Surface * decoration, SDL_Sur * using the color picker (palette), or using the shortcut key to * use color selector in temp-mode. */ -static void handle_color_changed(void) { +static void handle_color_changed(void) +{ render_brush(); if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) do_render_cur_text(0); else if (cur_tool == TOOL_MAGIC) - magic_funcs[magics[magic_group][cur_magic[magic_group]].handle_idx].set_color( - magic_api_struct, - color_hexes[cur_color][0], - color_hexes[cur_color][1], - color_hexes[cur_color][2]); + magic_funcs[magics[magic_group][cur_magic[magic_group]].handle_idx]. + set_color(magic_api_struct, color_hexes[cur_color][0], + color_hexes[cur_color][1], color_hexes[cur_color][2]); else if (cur_tool == TOOL_STAMP) clear_cached_stamp(); } @@ -24617,33 +26239,35 @@ static void magic_switchout(SDL_Surface * last) int was_clicking = 0; if (mouseaccessibility && emulate_button_pressed) - { - /* We were 'clicking' in mouse accessibility mode; stop clicking now */ - /* (EVEN if we weren't in magic tool) */ - emulate_button_pressed = 0; - was_clicking = 1; - } + { + /* We were 'clicking' in mouse accessibility mode; stop clicking now */ + /* (EVEN if we weren't in magic tool) */ + emulate_button_pressed = 0; + was_clicking = 1; + } if (cur_tool == TOOL_MAGIC) + { + int grp, cur; + + grp = magic_group; + cur = cur_magic[magic_group]; + + magic_funcs[magics[grp][cur].handle_idx].switchout(magic_api_struct, + magics[grp][cur].idx, + magics[grp][cur].mode, + canvas, last); + update_canvas(0, 0, canvas->w, canvas->h); + + if (was_clicking && magics[grp][cur].mode == MODE_PAINT_WITH_PREVIEW) { - int grp, cur; - - grp = magic_group; - cur = cur_magic[magic_group]; - - magic_funcs[magics[grp][cur].handle_idx].switchout(magic_api_struct, - magics[grp][cur].idx, magics[grp][cur].mode, canvas, last); - update_canvas(0, 0, canvas->w, canvas->h); - - if (was_clicking && magics[grp][cur].mode == MODE_PAINT_WITH_PREVIEW) - { - /* Clean up preview! */ - do_undo(); - tool_avail[TOOL_REDO] = 0; /* Don't let them 'redo' to get preview back */ - draw_toolbar(); - update_screen_rect(&r_tools); - } + /* Clean up preview! */ + do_undo(); + tool_avail[TOOL_REDO] = 0; /* Don't let them 'redo' to get preview back */ + draw_toolbar(); + update_screen_rect(&r_tools); } + } } /** @@ -24652,22 +26276,24 @@ static void magic_switchout(SDL_Surface * last) static void magic_switchin(SDL_Surface * last) { if (cur_tool == TOOL_MAGIC) - { - int grp, cur; + { + int grp, cur; - grp = magic_group; - cur = cur_magic[magic_group]; + grp = magic_group; + cur = cur_magic[magic_group]; - magic_funcs[magics[grp][cur].handle_idx].switchin(magic_api_struct, - magics[grp][cur].idx, magics[grp][cur].mode, canvas, last); + magic_funcs[magics[grp][cur].handle_idx].switchin(magic_api_struct, + magics[grp][cur].idx, + magics[grp][cur].mode, + canvas, last); - /* In case the Magic tool's switchin() called update_progress_bar(), - let's put the old Tux text back: */ + /* In case the Magic tool's switchin() called update_progress_bar(), + let's put the old Tux text back: */ - redraw_tux_text(); + redraw_tux_text(); - update_canvas(0, 0, canvas->w, canvas->h); - } + update_canvas(0, 0, canvas->w, canvas->h); + } } /** @@ -24675,7 +26301,8 @@ static void magic_switchin(SDL_Surface * last) */ static int magic_modeint(int mode) { - if (mode == MODE_PAINT || mode == MODE_ONECLICK || mode == MODE_PAINT_WITH_PREVIEW) + if (mode == MODE_PAINT || mode == MODE_ONECLICK + || mode == MODE_PAINT_WITH_PREVIEW) return 0; else if (mode == MODE_FULLSCREEN) return 1; @@ -24686,7 +26313,8 @@ static int magic_modeint(int mode) /** * FIXME */ -static void add_label_node(int w, int h, Uint16 x, Uint16 y, SDL_Surface * label_node_surface) +static void add_label_node(int w, int h, Uint16 x, Uint16 y, + SDL_Surface * label_node_surface) { struct label_node *new_node = malloc(sizeof(struct label_node)); struct label_node *aux_node; @@ -24695,10 +26323,10 @@ static void add_label_node(int w, int h, Uint16 x, Uint16 y, SDL_Surface * label new_node->save_texttool_len = texttool_len; while (i < texttool_len) - { - new_node->save_texttool_str[i] = texttool_str[i]; - i = i + 1; - } + { + new_node->save_texttool_str[i] = texttool_str[i]; + i = i + 1; + } new_node->save_texttool_str[i] = L'\0'; // printf("New node's text is: \"%ls\"\n", new_node->save_texttool_str); @@ -24715,28 +26343,28 @@ static void add_label_node(int w, int h, Uint16 x, Uint16 y, SDL_Surface * label new_node->save_undoid = 255; if (texttool_len > 0) - { - new_node->is_enabled = TRUE; - } + { + new_node->is_enabled = TRUE; + } else - { - new_node->is_enabled = FALSE; - } + { + new_node->is_enabled = FALSE; + } new_node->save_font_type = NULL; if (label_node_to_edit) - { - new_node->disables = label_node_to_edit; - } + { + new_node->disables = label_node_to_edit; + } else new_node->disables = NULL; if (label_node_surface != NULL) - { - new_node->label_node_surface = label_node_surface; - new_node->label_node_surface->refcount++; - } + { + new_node->label_node_surface = label_node_surface; + new_node->label_node_surface->refcount++; + } else new_node->label_node_surface = NULL; @@ -24745,10 +26373,10 @@ static void add_label_node(int w, int h, Uint16 x, Uint16 y, SDL_Surface * label new_node->next_to_down_label_node = current_label_node; if (current_label_node) - { - aux_node = current_label_node; - aux_node->next_to_up_label_node = new_node; - } + { + aux_node = current_label_node; + aux_node->next_to_up_label_node = new_node; + } current_label_node = new_node; @@ -24764,7 +26392,8 @@ static void add_label_node(int w, int h, Uint16 x, Uint16 y, SDL_Surface * label /** * FIXME */ -static struct label_node *search_label_list(struct label_node **ref_head, Uint16 x, Uint16 y, int hover) +static struct label_node *search_label_list(struct label_node **ref_head, + Uint16 x, Uint16 y, int hover) { struct label_node *current_node; struct label_node *tmp_node = NULL; @@ -24780,103 +26409,110 @@ static struct label_node *search_label_list(struct label_node **ref_head, Uint16 current_node = *ref_head; while (done != TRUE) + { + if (x >= current_node->save_x) { - if (x >= current_node->save_x) + if (y >= current_node->save_y) + { + if (x <= (current_node->save_x) + (current_node->save_width)) { - if (y >= current_node->save_y) + if (y <= (current_node->save_y) + (current_node->save_height)) + { + if (current_node->is_enabled == TRUE) { - if (x <= (current_node->save_x) + (current_node->save_width)) - { - if (y <= (current_node->save_y) + (current_node->save_height)) - { - if (current_node->is_enabled == TRUE) - { - if (hover == 1) - return (current_node); - tmp_node = current_node; - done = TRUE; - } - } - } + if (hover == 1) + return (current_node); + tmp_node = current_node; + done = TRUE; } + } } - current_node = current_node->next_to_down_label_node; - if (current_node == NULL) - current_node = current_label_node; - if (current_node == *ref_head) - done = TRUE; + } } + current_node = current_node->next_to_down_label_node; + if (current_node == NULL) + current_node = current_label_node; + if (current_node == *ref_head) + done = TRUE; + } if (tmp_node != NULL) + { + select_texttool_len = tmp_node->save_texttool_len; + + u = 0; + while (u < select_texttool_len) { - select_texttool_len = tmp_node->save_texttool_len; - - u = 0; - while (u < select_texttool_len) - { - select_texttool_str[u] = tmp_node->save_texttool_str[u]; - u = u + 1; - } - select_texttool_str[u] = L'\0'; - - for (k = 0; k < NUM_COLORS; k++) - { - if ((color_hexes[k][0] == tmp_node->save_color.r) && - (color_hexes[k][1] == tmp_node->save_color.g) && - (color_hexes[k][2] == tmp_node->save_color.b) && (k < COLOR_PICKER)) - { - select_color = k; - cur_color = k; - break; - } - - if (k == COLOR_PICKER) - { - cur_color = COLOR_PICKER; - select_color = COLOR_PICKER; - color_hexes[select_color][0] = tmp_node->save_color.r; - color_hexes[select_color][1] = tmp_node->save_color.g; - color_hexes[select_color][2] = tmp_node->save_color.b; - SDL_LockSurface(img_color_btns[COLOR_PICKER]); - SDL_LockSurface(img_color_btns[COLOR_PICKER + NUM_COLORS]); - - for (j = 0; j < 48 /* 48 */ ; j++) - { - for (i = 0; i < 48; i++) - { - SDL_GetRGBA(getpixels[img_paintwell->format->BytesPerPixel] (img_paintwell, i, j), - img_paintwell->format, &r, &g, &b, &a); - if (a == 255) - { - putpixels[img_color_btns[COLOR_PICKER]->format->BytesPerPixel] - (img_color_btns[COLOR_PICKER], i, j, - SDL_MapRGB(img_color_btns[COLOR_PICKER]->format, - tmp_node->save_color.r, tmp_node->save_color.g, tmp_node->save_color.b)); - putpixels[img_color_btns[COLOR_PICKER + NUM_COLORS]->format->BytesPerPixel] - (img_color_btns[COLOR_PICKER + NUM_COLORS], i, j, - SDL_MapRGB(img_color_btns[COLOR_PICKER + NUM_COLORS]->format, - tmp_node->save_color.r, tmp_node->save_color.g, tmp_node->save_color.b)); - } - } - } - SDL_UnlockSurface(img_color_btns[COLOR_PICKER]); - SDL_UnlockSurface(img_color_btns[COLOR_PICKER + NUM_COLORS]); - - draw_colors(COLORSEL_CLOBBER); - render_brush(); /* FIXME: render_brush should be called at the start of Brush and Line tools? */ - } - } - - select_width = tmp_node->save_width; - select_height = tmp_node->save_height; - select_x = tmp_node->save_x; - select_y = tmp_node->save_y; - select_cur_font = tmp_node->save_cur_font; - select_text_state = tmp_node->save_text_state; - select_text_size = tmp_node->save_text_size; - - return tmp_node; + select_texttool_str[u] = tmp_node->save_texttool_str[u]; + u = u + 1; } + select_texttool_str[u] = L'\0'; + + for (k = 0; k < NUM_COLORS; k++) + { + if ((color_hexes[k][0] == tmp_node->save_color.r) && + (color_hexes[k][1] == tmp_node->save_color.g) && + (color_hexes[k][2] == tmp_node->save_color.b) && (k < COLOR_PICKER)) + { + select_color = k; + cur_color = k; + break; + } + + if (k == COLOR_PICKER) + { + cur_color = COLOR_PICKER; + select_color = COLOR_PICKER; + color_hexes[select_color][0] = tmp_node->save_color.r; + color_hexes[select_color][1] = tmp_node->save_color.g; + color_hexes[select_color][2] = tmp_node->save_color.b; + SDL_LockSurface(img_color_btns[COLOR_PICKER]); + SDL_LockSurface(img_color_btns[COLOR_PICKER + NUM_COLORS]); + + for (j = 0; j < 48 /* 48 */ ; j++) + { + for (i = 0; i < 48; i++) + { + SDL_GetRGBA(getpixels[img_paintwell->format->BytesPerPixel] + (img_paintwell, i, j), img_paintwell->format, &r, &g, + &b, &a); + if (a == 255) + { + putpixels[img_color_btns[COLOR_PICKER]->format->BytesPerPixel] + (img_color_btns[COLOR_PICKER], i, j, + SDL_MapRGB(img_color_btns[COLOR_PICKER]->format, + tmp_node->save_color.r, tmp_node->save_color.g, + tmp_node->save_color.b)); + putpixels[img_color_btns[COLOR_PICKER + NUM_COLORS]->format-> + BytesPerPixel] (img_color_btns[COLOR_PICKER + + NUM_COLORS], i, j, + SDL_MapRGB(img_color_btns + [COLOR_PICKER + + NUM_COLORS]->format, + tmp_node->save_color.r, + tmp_node->save_color.g, + tmp_node->save_color.b)); + } + } + } + SDL_UnlockSurface(img_color_btns[COLOR_PICKER]); + SDL_UnlockSurface(img_color_btns[COLOR_PICKER + NUM_COLORS]); + + draw_colors(COLORSEL_CLOBBER); + render_brush(); /* FIXME: render_brush should be called at the start of Brush and Line tools? */ + } + } + + select_width = tmp_node->save_width; + select_height = tmp_node->save_height; + select_x = tmp_node->save_x; + select_y = tmp_node->save_y; + select_cur_font = tmp_node->save_cur_font; + select_text_state = tmp_node->save_text_state; + select_text_size = tmp_node->save_text_size; + + return tmp_node; + } return NULL; } @@ -24887,16 +26523,16 @@ static struct label_node *search_label_list(struct label_node **ref_head, Uint16 static void rec_undo_label(void) { if (first_label_node_in_redo_stack != NULL) - { - delete_label_list(&first_label_node_in_redo_stack); - first_label_node_in_redo_stack = NULL; - } + { + delete_label_list(&first_label_node_in_redo_stack); + first_label_node_in_redo_stack = NULL; + } if (coming_from_undo_or_redo) // yet recorded, avoiding to write text_undo - { - coming_from_undo_or_redo = FALSE; - return; - } + { + coming_from_undo_or_redo = FALSE; + return; + } // FIXME: // It's all wrong to have a separate undo stack anyway. We need a way @@ -24907,19 +26543,20 @@ static void rec_undo_label(void) // stops being current or for when the level becomes current again. if (have_to_rec_label_node) - { - current_label_node->save_undoid = cur_undo; - text_undo[cur_undo] = 1; - have_to_rec_label_node = FALSE; - } + { + current_label_node->save_undoid = cur_undo; + text_undo[cur_undo] = 1; + have_to_rec_label_node = FALSE; + } else - { - text_undo[cur_undo] = 0; + { + text_undo[cur_undo] = 0; - /* Have we cycled around NUM_UNDO_BUFS? */ - if (current_label_node != NULL && current_label_node->save_undoid == (cur_undo + 1) % NUM_UNDO_BUFS) - current_label_node->save_undoid = 255; - } + /* Have we cycled around NUM_UNDO_BUFS? */ + if (current_label_node != NULL + && current_label_node->save_undoid == (cur_undo + 1) % NUM_UNDO_BUFS) + current_label_node->save_undoid = 255; + } } /** @@ -24929,23 +26566,23 @@ static void do_undo_label_node() { if (text_undo[(cur_undo + 1) % NUM_UNDO_BUFS] == 1) if (current_label_node != NULL) + { + if (current_label_node->save_undoid == (cur_undo + 1) % NUM_UNDO_BUFS) { - if (current_label_node->save_undoid == (cur_undo + 1) % NUM_UNDO_BUFS) - { - if (current_label_node->disables != NULL) /* If current node is an editing of an older one, reenable it. */ - current_label_node->disables->is_enabled = TRUE; + if (current_label_node->disables != NULL) /* If current node is an editing of an older one, reenable it. */ + current_label_node->disables->is_enabled = TRUE; - first_label_node_in_redo_stack = current_label_node; - current_label_node = current_label_node->next_to_down_label_node; + first_label_node_in_redo_stack = current_label_node; + current_label_node = current_label_node->next_to_down_label_node; - if (current_label_node == NULL) - start_label_node = current_label_node; + if (current_label_node == NULL) + start_label_node = current_label_node; - highlighted_label_node = current_label_node; - derender_node(&first_label_node_in_redo_stack); - coming_from_undo_or_redo = TRUE; - } + highlighted_label_node = current_label_node; + derender_node(&first_label_node_in_redo_stack); + coming_from_undo_or_redo = TRUE; } + } highlighted_label_node = current_label_node; } @@ -24955,27 +26592,28 @@ static void do_undo_label_node() static void do_redo_label_node() { if ((text_undo[cur_undo] == 1) && (first_label_node_in_redo_stack != NULL)) + { + if (first_label_node_in_redo_stack->save_undoid == cur_undo) { - if (first_label_node_in_redo_stack->save_undoid == cur_undo) - { - current_label_node = first_label_node_in_redo_stack; - first_label_node_in_redo_stack = current_label_node->next_to_up_label_node; + current_label_node = first_label_node_in_redo_stack; + first_label_node_in_redo_stack = + current_label_node->next_to_up_label_node; - if (start_label_node == NULL) - start_label_node = current_label_node; + if (start_label_node == NULL) + start_label_node = current_label_node; - highlighted_label_node = current_label_node; - if (current_label_node->disables != NULL) /* If this is a redo of an editing, redisable the old node. */ - { - current_label_node->disables->is_enabled = FALSE; - derender_node(¤t_label_node->disables); - } - else - simply_render_node(current_label_node); + highlighted_label_node = current_label_node; + if (current_label_node->disables != NULL) /* If this is a redo of an editing, redisable the old node. */ + { + current_label_node->disables->is_enabled = FALSE; + derender_node(¤t_label_node->disables); + } + else + simply_render_node(current_label_node); - coming_from_undo_or_redo = TRUE; - } + coming_from_undo_or_redo = TRUE; } + } } @@ -24992,70 +26630,71 @@ static void simply_render_node(struct label_node *node) unsigned i; if (node->label_node_surface == NULL) + { + /* Render the text: */ + + SDL_Color color = node->save_color; + + text_state = node->save_text_state; + text_size = node->save_text_size; + + i = 0; + while (i < node->save_texttool_len) { - /* Render the text: */ - - SDL_Color color = node->save_color; - - text_state = node->save_text_state; - text_size = node->save_text_size; - - i = 0; - while (i < node->save_texttool_len) - { - tmp_str[i] = node->save_texttool_str[i]; - i = i + 1; - } - tmp_str[i] = L'\0'; - - str = uppercase_w(tmp_str); - - text_state = node->save_text_state; - text_size = node->save_text_size; - - for (j = 0; j < num_font_families; j++) - { - if (user_font_families[j] && user_font_families[j]->handle) - { - TuxPaint_Font_CloseFont(user_font_families[j]->handle); - user_font_families[j]->handle = NULL; - } - } - - tmp_surf = render_text_w(getfonthandle(node->save_cur_font), str, color); - if (tmp_surf != NULL) - - node->label_node_surface = tmp_surf; + tmp_str[i] = node->save_texttool_str[i]; + i = i + 1; } + tmp_str[i] = L'\0'; + + str = uppercase_w(tmp_str); + + text_state = node->save_text_state; + text_size = node->save_text_size; + + for (j = 0; j < num_font_families; j++) + { + if (user_font_families[j] && user_font_families[j]->handle) + { + TuxPaint_Font_CloseFont(user_font_families[j]->handle); + user_font_families[j]->handle = NULL; + } + } + + tmp_surf = render_text_w(getfonthandle(node->save_cur_font), str, color); + if (tmp_surf != NULL) + + node->label_node_surface = tmp_surf; + } if (node->label_node_surface != NULL) - { - w = node->label_node_surface->w; + { + w = node->label_node_surface->w; - cursor_textwidth = w; - /* Draw the text itself! */ + cursor_textwidth = w; + /* Draw the text itself! */ - dest.x = node->save_x; - dest.y = node->save_y; + dest.x = node->save_x; + dest.y = node->save_y; - src.x = 0; - src.y = 0; - src.w = node->label_node_surface->w; - src.h = node->label_node_surface->h; + src.x = 0; + src.y = 0; + src.w = node->label_node_surface->w; + src.h = node->label_node_surface->h; - if (dest.x + src.w > WINDOW_WIDTH - r_ttoolopt.w - r_ttools.w) - src.w = WINDOW_WIDTH - r_ttoolopt.w - r_ttools.w - dest.x; - if (dest.y + src.h > (button_h * buttons_tall + r_ttools.h)) - src.h = (button_h * buttons_tall + r_ttools.h) - dest.y; + if (dest.x + src.w > WINDOW_WIDTH - r_ttoolopt.w - r_ttools.w) + src.w = WINDOW_WIDTH - r_ttoolopt.w - r_ttools.w - dest.x; + if (dest.y + src.h > (button_h * buttons_tall + r_ttools.h)) + src.h = (button_h * buttons_tall + r_ttools.h) - dest.y; - myblit(node->label_node_surface, &src, label, &dest); + myblit(node->label_node_surface, &src, label, &dest); - update_canvas(dest.x, dest.y, dest.x + node->label_node_surface->w, dest.y + node->label_node_surface->h); + update_canvas(dest.x, dest.y, dest.x + node->label_node_surface->w, + dest.y + node->label_node_surface->h); - /* Setting the sizes correctly */ - node->save_width = node->label_node_surface->w; - node->save_height = node->label_node_surface->h; - } + /* Setting the sizes correctly */ + node->save_width = node->label_node_surface->w; + node->save_height = node->label_node_surface->h; + } } /** @@ -25066,26 +26705,27 @@ static void render_all_nodes_starting_at(struct label_node **node) struct label_node *current_node; if (*node != NULL) + { + current_node = *node; + while (current_node != first_label_node_in_redo_stack) { - current_node = *node; - while (current_node != first_label_node_in_redo_stack) - { - if (current_node->is_enabled == TRUE) - { - simply_render_node(current_node); - } - if (current_node->next_to_up_label_node == NULL) - return; - current_node = current_node->next_to_up_label_node; - } + if (current_node->is_enabled == TRUE) + { + simply_render_node(current_node); + } + if (current_node->next_to_up_label_node == NULL) + return; + current_node = current_node->next_to_up_label_node; } + } } /** * FIXME */ /* FIXME: This should search for the top-down of the overlaping labels and only re-render from it */ -static void derender_node(__attribute__((unused)) struct label_node **ref_head) +static void derender_node( __attribute__((unused)) + struct label_node **ref_head) { SDL_Rect r_tmp_derender; @@ -25094,7 +26734,8 @@ static void derender_node(__attribute__((unused)) struct label_node **ref_head) r_tmp_derender.x = 0; r_tmp_derender.y = 0; - SDL_FillRect(label, &r_tmp_derender, SDL_MapRGBA(label->format, 0, 0, 0, 0)); + SDL_FillRect(label, &r_tmp_derender, + SDL_MapRGBA(label->format, 0, 0, 0, 0)); render_all_nodes_starting_at(&start_label_node); } @@ -25108,15 +26749,15 @@ static void delete_label_list(struct label_node **ref_head) struct label_node *next; while (current != NULL) - { - fflush(stdout); + { + fflush(stdout); - next = current->next_to_up_label_node; - if (current->label_node_surface) - SDL_FreeSurface(current->label_node_surface); - free(current); - current = next; - } + next = current->next_to_up_label_node; + if (current->label_node_surface) + SDL_FreeSurface(current->label_node_surface); + free(current); + current = next; + } *ref_head = NULL; } @@ -25126,7 +26767,8 @@ static void delete_label_list(struct label_node **ref_head) */ /* A custom bliter that allows to put two transparent layers toghether without having to deal with colorkeys or SDL_SRCALPHA I am always reinventing the wheel. Hope this one is not squared. Pere */ -static void myblit(SDL_Surface * src_surf, SDL_Rect * src_rect, SDL_Surface * dest_surf, SDL_Rect * dest_rect) +static void myblit(SDL_Surface * src_surf, SDL_Rect * src_rect, + SDL_Surface * dest_surf, SDL_Rect * dest_rect) { int x, y; Uint8 src_r, src_g, src_b, src_a; @@ -25134,35 +26776,56 @@ static void myblit(SDL_Surface * src_surf, SDL_Rect * src_rect, SDL_Surface * de for (x = src_rect->x; x < src_rect->w + src_rect->x; x++) for (y = src_rect->y; y < src_rect->h + src_rect->y; y++) + { + SDL_GetRGBA(getpixels[src_surf->format->BytesPerPixel] + (src_surf, x - src_rect->x, y - src_rect->y), + src_surf->format, &src_r, &src_g, &src_b, &src_a); + if (src_a != SDL_ALPHA_TRANSPARENT) { - SDL_GetRGBA(getpixels[src_surf->format->BytesPerPixel] (src_surf, x - src_rect->x, y - src_rect->y), - src_surf->format, &src_r, &src_g, &src_b, &src_a); - if (src_a != SDL_ALPHA_TRANSPARENT) + if (src_a == SDL_ALPHA_OPAQUE) + putpixels[dest_surf->format->BytesPerPixel] (dest_surf, + x + dest_rect->x, + y + dest_rect->y, + SDL_MapRGBA + (dest_surf->format, + src_r, src_g, src_b, + src_a)); + else + { + SDL_GetRGBA(getpixels[dest_surf->format->BytesPerPixel] + (dest_surf, x + dest_rect->x, y + dest_rect->y), + src_surf->format, &dest_r, &dest_g, &dest_b, &dest_a); + if (dest_a == SDL_ALPHA_TRANSPARENT) + putpixels[dest_surf->format->BytesPerPixel] (dest_surf, + x + dest_rect->x, + y + dest_rect->y, + SDL_MapRGBA + (dest_surf->format, + src_r, src_g, src_b, + src_a)); + else { - if (src_a == SDL_ALPHA_OPAQUE) - putpixels[dest_surf->format->BytesPerPixel] (dest_surf, x + dest_rect->x, y + dest_rect->y, - SDL_MapRGBA(dest_surf->format, src_r, src_g, src_b, src_a)); - else - { - SDL_GetRGBA(getpixels[dest_surf->format->BytesPerPixel] (dest_surf, x + dest_rect->x, y + dest_rect->y), - src_surf->format, &dest_r, &dest_g, &dest_b, &dest_a); - if (dest_a == SDL_ALPHA_TRANSPARENT) - putpixels[dest_surf->format->BytesPerPixel] (dest_surf, x + dest_rect->x, y + dest_rect->y, - SDL_MapRGBA(dest_surf->format, src_r, src_g, src_b, - src_a)); - else - { - dest_r = src_r * src_a / 255 + dest_r * dest_a * (255 - src_a) / 255 / 255; - dest_g = src_g * src_a / 255 + dest_g * dest_a * (255 - src_a) / 255 / 255; - dest_b = src_b * src_a / 255 + dest_b * dest_a * (255 - src_a) / 255 / 255; - dest_a = src_a + dest_a * (255 - src_a) / 255; - putpixels[dest_surf->format->BytesPerPixel] (dest_surf, x + dest_rect->x, y + dest_rect->y, - SDL_MapRGBA(dest_surf->format, dest_r, dest_g, dest_b, - dest_a)); - } - } + dest_r = + src_r * src_a / 255 + dest_r * dest_a * (255 - + src_a) / 255 / 255; + dest_g = + src_g * src_a / 255 + dest_g * dest_a * (255 - + src_a) / 255 / 255; + dest_b = + src_b * src_a / 255 + dest_b * dest_a * (255 - + src_a) / 255 / 255; + dest_a = src_a + dest_a * (255 - src_a) / 255; + putpixels[dest_surf->format->BytesPerPixel] (dest_surf, + x + dest_rect->x, + y + dest_rect->y, + SDL_MapRGBA + (dest_surf->format, + dest_r, dest_g, + dest_b, dest_a)); } + } } + } } /** @@ -25205,8 +26868,8 @@ static void load_info_about_label_surface(FILE * lfi) /* Clear all info related to label surface */ delete_label_list(&start_label_node); - start_label_node = current_label_node = first_label_node_in_redo_stack = highlighted_label_node = label_node_to_edit = - NULL; + start_label_node = current_label_node = first_label_node_in_redo_stack = + highlighted_label_node = label_node_to_edit = NULL; have_to_rec_label_node = FALSE; @@ -25218,37 +26881,39 @@ static void load_info_about_label_surface(FILE * lfi) tmp_fscanf_return = fscanf(lfi, "%d\n", &list_ctr); if (list_ctr <= 0) - { - fprintf(stderr, "Unexpected! Count of label notes is <= 0 (%d)!\n", list_ctr); - fclose(lfi); - return; - } + { + fprintf(stderr, "Unexpected! Count of label notes is <= 0 (%d)!\n", + list_ctr); + fclose(lfi); + return; + } /* Read saved canvas width/height, so we can scale to the current canvas (in case it changed due to window size / fullscreen resolution changes, larger UI button size, etc. */ tmp_fscanf_return = fscanf(lfi, "%d\n", &tmp_scale_w); tmp_fscanf_return = fscanf(lfi, "%d\n\n", &tmp_scale_h); - (void)tmp_fscanf_return; + (void) tmp_fscanf_return; if (tmp_scale_w <= 0 || tmp_scale_h <= 0) - { - fprintf(stderr, "Unexpected! Saved canvas dimensions %d x %d!\n", tmp_scale_w, tmp_scale_h); - fclose(lfi); - return; - } + { + fprintf(stderr, "Unexpected! Saved canvas dimensions %d x %d!\n", + tmp_scale_w, tmp_scale_h); + fclose(lfi); + return; + } /* Calculate canvas aspect ratios & such */ old_width = tmp_scale_w; old_height = tmp_scale_h; new_width = r_canvas.w; new_height = r_canvas.h; - new_ratio = (float)new_width / new_height; - old_ratio = (float)old_width / old_height; + new_ratio = (float) new_width / new_height; + old_ratio = (float) old_width / old_height; if (new_ratio < old_ratio) - new_to_old_ratio = (float)new_width / old_width; + new_to_old_ratio = (float) new_width / old_width; else - new_to_old_ratio = (float)new_height / old_height; + new_to_old_ratio = (float) new_height / old_height; #ifdef WIN32 wtmpstr = malloc(1024); @@ -25258,183 +26923,197 @@ static void load_info_about_label_surface(FILE * lfi) /* Read the labels' text: */ for (k = 0; k < list_ctr; k++) - { - new_node = malloc(sizeof(struct label_node)); + { + new_node = malloc(sizeof(struct label_node)); - new_node->save_texttool_len = atoi(fgets(tmpstr, 5, lfi)); + new_node->save_texttool_len = atoi(fgets(tmpstr, 5, lfi)); #ifdef DEBUG - printf("Reading %d wide chars\n", new_node->save_texttool_len); fflush(stdout); + printf("Reading %d wide chars\n", new_node->save_texttool_len); + fflush(stdout); #endif - if (new_node->save_texttool_len >= 1024) - { - fprintf(stderr, "Unexpected! Saved text length is >= 1024 (%u!)\n", new_node->save_texttool_len); - free(new_node); + if (new_node->save_texttool_len >= 1024) + { + fprintf(stderr, "Unexpected! Saved text length is >= 1024 (%u!)\n", + new_node->save_texttool_len); + free(new_node); #ifdef WIN32 - free(wtmpstr); + free(wtmpstr); #endif - free(tmpstr); - fclose(lfi); - return; - } - else - { + free(tmpstr); + fclose(lfi); + return; + } + else + { #ifdef WIN32 - /* Using fancy "%[]" operator to scan until the end of a line */ - tmp_fscanf_return = fscanf(lfi, "%[^\n]\n", tmpstr); - mbstowcs(wtmpstr, tmpstr, 1024); - for (l = 0; l < new_node->save_texttool_len; l++) - new_node->save_texttool_str[l] = wtmpstr[l]; - new_node->save_texttool_str[l] = L'\0'; + /* Using fancy "%[]" operator to scan until the end of a line */ + tmp_fscanf_return = fscanf(lfi, "%[^\n]\n", tmpstr); + mbstowcs(wtmpstr, tmpstr, 1024); + for (l = 0; l < new_node->save_texttool_len; l++) + new_node->save_texttool_str[l] = wtmpstr[l]; + new_node->save_texttool_str[l] = L'\0'; #elif defined(__ANDROID__) wchar_t tmp_char; for (l = 0; l < new_node->save_texttool_len; l++) - { - fscanf(lfi, "%d ", &tmp_char); - new_node->save_texttool_str[l] = tmp_char; - } + { + fscanf(lfi, "%d ", &tmp_char); + new_node->save_texttool_str[l] = tmp_char; + } fscanf(lfi, "\n"); #else - /* Using fancy "%[]" operator to scan until the end of a line */ - tmp_fscanf_return = fscanf(lfi, "%l[^\n]\n", new_node->save_texttool_str); + /* Using fancy "%[]" operator to scan until the end of a line */ + tmp_fscanf_return = + fscanf(lfi, "%l[^\n]\n", new_node->save_texttool_str); #endif #ifdef DEBUG - printf("Read: \"%ls\"\n", new_node->save_texttool_str); fflush(stdout); + printf("Read: \"%ls\"\n", new_node->save_texttool_str); + fflush(stdout); #endif - /* Read the label's color (RGB) */ - tmp_fscanf_return = fscanf(lfi, "%u\n", &l); - new_node->save_color.r = (Uint8) l; - tmp_fscanf_return = fscanf(lfi, "%u\n", &l); - new_node->save_color.g = (Uint8) l; - tmp_fscanf_return = fscanf(lfi, "%u\n", &l); - new_node->save_color.b = (Uint8) l; + /* Read the label's color (RGB) */ + tmp_fscanf_return = fscanf(lfi, "%u\n", &l); + new_node->save_color.r = (Uint8) l; + tmp_fscanf_return = fscanf(lfi, "%u\n", &l); + new_node->save_color.g = (Uint8) l; + tmp_fscanf_return = fscanf(lfi, "%u\n", &l); + new_node->save_color.b = (Uint8) l; - /* Read the label's position */ - tmp_fscanf_return = fscanf(lfi, "%d\n", &new_node->save_width); - tmp_fscanf_return = fscanf(lfi, "%d\n", &new_node->save_height); - tmp_fscanf_return = fscanf(lfi, "%d\n", &tmp_pos); - old_pos = (int)tmp_pos; + /* Read the label's position */ + tmp_fscanf_return = fscanf(lfi, "%d\n", &new_node->save_width); + tmp_fscanf_return = fscanf(lfi, "%d\n", &new_node->save_height); + tmp_fscanf_return = fscanf(lfi, "%d\n", &tmp_pos); + old_pos = (int) tmp_pos; - if (new_ratio < old_ratio) - { - new_pos = (old_pos * new_to_old_ratio); - tmp_pos = new_pos; - new_node->save_x = tmp_pos; - tmp_fscanf_return = fscanf(lfi, "%d\n", &tmp_pos); - old_pos = (int)tmp_pos; - new_pos = old_pos * new_to_old_ratio + (new_height - old_height * new_to_old_ratio) / 2; - tmp_pos = new_pos; - new_node->save_y = tmp_pos; - } - else - { - new_pos = (old_pos * new_to_old_ratio) + (new_width - old_width * new_to_old_ratio) / 2; - tmp_pos = new_pos; - new_node->save_x = tmp_pos; - tmp_fscanf_return = fscanf(lfi, "%d\n", &tmp_pos); - old_pos = (int)tmp_pos; - new_pos = (old_pos * new_to_old_ratio); - tmp_pos = new_pos; - new_node->save_y = tmp_pos; - } + if (new_ratio < old_ratio) + { + new_pos = (old_pos * new_to_old_ratio); + tmp_pos = new_pos; + new_node->save_x = tmp_pos; + tmp_fscanf_return = fscanf(lfi, "%d\n", &tmp_pos); + old_pos = (int) tmp_pos; + new_pos = + old_pos * new_to_old_ratio + (new_height - + old_height * new_to_old_ratio) / 2; + tmp_pos = new_pos; + new_node->save_y = tmp_pos; + } + else + { + new_pos = + (old_pos * new_to_old_ratio) + (new_width - + old_width * new_to_old_ratio) / 2; + tmp_pos = new_pos; + new_node->save_x = tmp_pos; + tmp_fscanf_return = fscanf(lfi, "%d\n", &tmp_pos); + old_pos = (int) tmp_pos; + new_pos = (old_pos * new_to_old_ratio); + tmp_pos = new_pos; + new_node->save_y = tmp_pos; + } #ifdef DEBUG - printf("Original label size %dx%d\n", new_node->save_width, new_node->save_height); + printf("Original label size %dx%d\n", new_node->save_width, + new_node->save_height); #endif - if (new_node->save_width > 8192 || new_node->save_height > 8192) - { - fprintf(stderr, "Unexpected! Save dimensions are (%u x %u!)\n", new_node->save_width, new_node->save_height); - free(new_node); - free(tmpstr); + if (new_node->save_width > 8192 || new_node->save_height > 8192) + { + fprintf(stderr, "Unexpected! Save dimensions are (%u x %u!)\n", + new_node->save_width, new_node->save_height); + free(new_node); + free(tmpstr); #ifdef WIN32 - free(wtmpstr); + free(wtmpstr); #endif - fclose(lfi); - return; - } + fclose(lfi); + return; + } - /* Read the label's font */ - tmp_fscanf_return = fscanf(lfi, "%d\n", &new_node->save_cur_font); - /* FIXME: This seems wrong! -bjk 2022.04.02 */ - new_node->save_cur_font = 0; + /* Read the label's font */ + tmp_fscanf_return = fscanf(lfi, "%d\n", &new_node->save_cur_font); + /* FIXME: This seems wrong! -bjk 2022.04.02 */ + new_node->save_cur_font = 0; - new_node->save_font_type = malloc(64); - tmp_fgets_return = fgets(new_node->save_font_type, 64, lfi); - (void)tmp_fgets_return; + new_node->save_font_type = malloc(64); + tmp_fgets_return = fgets(new_node->save_font_type, 64, lfi); + (void) tmp_fgets_return; - /* Read the label's state (italic &/or bold), and size */ - tmp_fscanf_return = fscanf(lfi, "%d\n", &new_node->save_text_state); - tmp_fscanf_return = fscanf(lfi, "%u\n", &new_node->save_text_size); + /* Read the label's state (italic &/or bold), and size */ + tmp_fscanf_return = fscanf(lfi, "%d\n", &new_node->save_text_state); + tmp_fscanf_return = fscanf(lfi, "%u\n", &new_node->save_text_size); - /* Read the bitmap data stored under the label */ - /* (The final PNG, when saved, includes the labels, as applied - to the canvas. But we need to be able to edit/move/remove them, - so we need to know what went _behind_ them) */ - label_node_surface = SDL_CreateRGBSurface(screen->flags, - new_node->save_width, - new_node->save_height, - screen->format->BitsPerPixel, - screen->format->Rmask, - screen->format->Gmask, screen->format->Bmask, TPAINT_AMASK); + /* Read the bitmap data stored under the label */ + /* (The final PNG, when saved, includes the labels, as applied + to the canvas. But we need to be able to edit/move/remove them, + so we need to know what went _behind_ them) */ + label_node_surface = SDL_CreateRGBSurface(screen->flags, + new_node->save_width, + new_node->save_height, + screen->format->BitsPerPixel, + screen->format->Rmask, + screen->format->Gmask, + screen->format->Bmask, + TPAINT_AMASK); - SDL_LockSurface(label_node_surface); - for (x = 0; x < new_node->save_width; x++) - for (y = 0; y < new_node->save_height; y++) - { - a = fgetc(lfi); - putpixels[label_node_surface->format->BytesPerPixel] (label_node_surface, x, y, - SDL_MapRGBA(label_node_surface->format, - new_node->save_color.r, - new_node->save_color.g, - new_node->save_color.b, a)); - } - SDL_UnlockSurface(label_node_surface); - - /* Set the label's size, in proportion to any canvas size differences */ - new_text_size = (float)new_node->save_text_size * new_to_old_ratio; - - /* Scale the backbuffer, in proportion... */ - label_node_surface_aux = - zoom(label_node_surface, label_node_surface->w * new_to_old_ratio, label_node_surface->h * new_to_old_ratio); - SDL_FreeSurface(label_node_surface); - new_node->label_node_surface = label_node_surface_aux; - new_node->label_node_surface->refcount++; - SDL_FreeSurface(label_node_surface_aux); - - if ((unsigned)new_text_size > MAX_TEXT_SIZE) /* Here we reach the limits when scaling the font size */ - new_node->save_text_size = MAX_TEXT_SIZE; - else if ((unsigned)new_text_size > MIN_TEXT_SIZE) - new_node->save_text_size = floor(new_text_size + 0.5); - else - new_node->save_text_size = MIN_TEXT_SIZE; - - new_node->save_undoid = 255; /* A value that cur_undo will likely never reach */ - new_node->is_enabled = TRUE; - new_node->disables = NULL; - new_node->next_to_down_label_node = NULL; - new_node->next_to_up_label_node = NULL; - tmp_fscanf_return = fscanf(lfi, "\n"); - - /* Link the labels together, for navigating between them */ - if (current_label_node == NULL) - { - current_label_node = new_node; - start_label_node = current_label_node; - } - else - { - new_node->next_to_down_label_node = current_label_node; - current_label_node->next_to_up_label_node = new_node; - current_label_node = new_node; - } - - highlighted_label_node = current_label_node; - simply_render_node(current_label_node); + SDL_LockSurface(label_node_surface); + for (x = 0; x < new_node->save_width; x++) + for (y = 0; y < new_node->save_height; y++) + { + a = fgetc(lfi); + putpixels[label_node_surface->format-> + BytesPerPixel] (label_node_surface, x, y, + SDL_MapRGBA(label_node_surface->format, + new_node->save_color.r, + new_node->save_color.g, + new_node->save_color.b, a)); } + SDL_UnlockSurface(label_node_surface); + + /* Set the label's size, in proportion to any canvas size differences */ + new_text_size = (float) new_node->save_text_size * new_to_old_ratio; + + /* Scale the backbuffer, in proportion... */ + label_node_surface_aux = + zoom(label_node_surface, label_node_surface->w * new_to_old_ratio, + label_node_surface->h * new_to_old_ratio); + SDL_FreeSurface(label_node_surface); + new_node->label_node_surface = label_node_surface_aux; + new_node->label_node_surface->refcount++; + SDL_FreeSurface(label_node_surface_aux); + + if ((unsigned) new_text_size > MAX_TEXT_SIZE) /* Here we reach the limits when scaling the font size */ + new_node->save_text_size = MAX_TEXT_SIZE; + else if ((unsigned) new_text_size > MIN_TEXT_SIZE) + new_node->save_text_size = floor(new_text_size + 0.5); + else + new_node->save_text_size = MIN_TEXT_SIZE; + + new_node->save_undoid = 255; /* A value that cur_undo will likely never reach */ + new_node->is_enabled = TRUE; + new_node->disables = NULL; + new_node->next_to_down_label_node = NULL; + new_node->next_to_up_label_node = NULL; + tmp_fscanf_return = fscanf(lfi, "\n"); + + /* Link the labels together, for navigating between them */ + if (current_label_node == NULL) + { + current_label_node = new_node; + start_label_node = current_label_node; + } + else + { + new_node->next_to_down_label_node = current_label_node; + current_label_node->next_to_up_label_node = new_node; + current_label_node = new_node; + } + + highlighted_label_node = current_label_node; + simply_render_node(current_label_node); } + } first_label_node_in_redo_stack = NULL; fclose(lfi); @@ -25460,52 +27139,54 @@ static void set_label_fonts() node = current_label_node; while (node != NULL) + { + for (i = 0; i < num_font_families; i++) { - for (i = 0; i < num_font_families; i++) - { - Uint32 c; + Uint32 c; - /* FIXME: 2009/09/13 TTF_FontFaceFamilyName() appends random "\n" at the end - of the returned string. Should investigate why, and when corrected, - remove the code that deals whith the ending "\n"s in ttffont */ - ttffont = TTF_FontFaceFamilyName(getfonthandle(i)->ttf_font); - for (c = 0; c < strlen(ttffont); c++) - if (ttffont[c] == '\n') - ttffont[c] = '\0'; - for (c = 0; c < strlen(node->save_font_type); c++) - if (node->save_font_type[c] == '\n') - node->save_font_type[c] = '\0'; + /* FIXME: 2009/09/13 TTF_FontFaceFamilyName() appends random "\n" at the end + of the returned string. Should investigate why, and when corrected, + remove the code that deals whith the ending "\n"s in ttffont */ + ttffont = TTF_FontFaceFamilyName(getfonthandle(i)->ttf_font); + for (c = 0; c < strlen(ttffont); c++) + if (ttffont[c] == '\n') + ttffont[c] = '\0'; + for (c = 0; c < strlen(node->save_font_type); c++) + if (node->save_font_type[c] == '\n') + node->save_font_type[c] = '\0'; #ifdef DEBUG - printf("ttffont A%sA\n", ttffont); - printf("font_type B%sB\n", node->save_font_type); + printf("ttffont A%sA\n", ttffont); + printf("font_type B%sB\n", node->save_font_type); #endif - if (strcmp(node->save_font_type, ttffont) == 0) + if (strcmp(node->save_font_type, ttffont) == 0) - { + { #ifdef DEBUG - printf("Font matched %s !!!\n", ttffont); + printf("Font matched %s !!!\n", ttffont); #endif - node->save_cur_font = i; - break; - } - else if (strstr(ttffont, node->save_font_type) || strstr(node->save_font_type, ttffont)) - { + node->save_cur_font = i; + break; + } + else if (strstr(ttffont, node->save_font_type) + || strstr(node->save_font_type, ttffont)) + { #ifdef DEBUG - printf("setting %s as replacement", TTF_FontFaceFamilyName(getfonthandle(i)->ttf_font)); + printf("setting %s as replacement", + TTF_FontFaceFamilyName(getfonthandle(i)->ttf_font)); #endif - node->save_cur_font = i; - } - } - - if (node->save_cur_font > num_font_families) /* This should never happens, setting default font. */ - node->save_cur_font = 0; - - free(node->save_font_type); /* Not needed anymore */ - node->save_font_type = NULL; - node = node->next_to_down_label_node; + node->save_cur_font = i; + } } + + if (node->save_cur_font > num_font_families) /* This should never happens, setting default font. */ + node->save_cur_font = 0; + + free(node->save_font_type); /* Not needed anymore */ + node->save_font_type = NULL; + node = node->next_to_down_label_node; + } } @@ -25517,41 +27198,46 @@ static void tmp_apply_uncommited_text() have_to_rec_label_node_back = have_to_rec_label_node; if (texttool_len > 0) + { + if (cur_tool == TOOL_TEXT || + (old_tool == TOOL_TEXT && + (cur_tool == TOOL_PRINT || + cur_tool == TOOL_SAVE || cur_tool == TOOL_OPEN + || cur_tool == TOOL_NEW || cur_tool == TOOL_QUIT))) { - if (cur_tool == TOOL_TEXT || - (old_tool == TOOL_TEXT && - (cur_tool == TOOL_PRINT || - cur_tool == TOOL_SAVE || cur_tool == TOOL_OPEN || cur_tool == TOOL_NEW || cur_tool == TOOL_QUIT))) - { - canvas_back = SDL_CreateRGBSurface(canvas->flags, - canvas->w, - canvas->h, - canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, 0); - SDL_BlitSurface(canvas, NULL, canvas_back, NULL); - do_render_cur_text(1); - } - - else if (cur_tool == TOOL_LABEL || - (old_tool == TOOL_LABEL && - (cur_tool == TOOL_PRINT || - cur_tool == TOOL_SAVE || cur_tool == TOOL_OPEN || cur_tool == TOOL_NEW || cur_tool == TOOL_QUIT))) - { - do_render_cur_text(1); - current_label_node->save_undoid = 253; - } + canvas_back = SDL_CreateRGBSurface(canvas->flags, + canvas->w, + canvas->h, + canvas->format->BitsPerPixel, + canvas->format->Rmask, + canvas->format->Gmask, + canvas->format->Bmask, 0); + SDL_BlitSurface(canvas, NULL, canvas_back, NULL); + do_render_cur_text(1); } + + else if (cur_tool == TOOL_LABEL || + (old_tool == TOOL_LABEL && + (cur_tool == TOOL_PRINT || + cur_tool == TOOL_SAVE || cur_tool == TOOL_OPEN + || cur_tool == TOOL_NEW || cur_tool == TOOL_QUIT))) + { + do_render_cur_text(1); + current_label_node->save_undoid = 253; + } + } else if ((cur_tool == TOOL_LABEL && label_node_to_edit) || ((old_tool == TOOL_LABEL && label_node_to_edit) && (cur_tool == TOOL_PRINT || - cur_tool == TOOL_SAVE || cur_tool == TOOL_OPEN || cur_tool == TOOL_NEW || cur_tool == TOOL_QUIT))) - { - add_label_node(0, 0, 0, 0, NULL); - current_label_node->is_enabled = FALSE; - current_label_node->save_undoid = 253; + cur_tool == TOOL_SAVE || cur_tool == TOOL_OPEN + || cur_tool == TOOL_NEW || cur_tool == TOOL_QUIT))) + { + add_label_node(0, 0, 0, 0, NULL); + current_label_node->is_enabled = FALSE; + current_label_node->save_undoid = 253; - derender_node(&label_node_to_edit); - } + derender_node(&label_node_to_edit); + } } /** @@ -25562,33 +27248,35 @@ static void undo_tmp_applied_text() struct label_node *aux_label_node; if (texttool_len > 0) + { + if (cur_tool == TOOL_TEXT || + (cur_tool == TOOL_PRINT && old_tool == TOOL_TEXT) || + (cur_tool == TOOL_SAVE && old_tool == TOOL_TEXT) || + (cur_tool == TOOL_OPEN && old_tool == TOOL_TEXT) || + (cur_tool == TOOL_NEW && old_tool == TOOL_TEXT) + || (cur_tool == TOOL_QUIT && old_tool == TOOL_TEXT)) { - if (cur_tool == TOOL_TEXT || - (cur_tool == TOOL_PRINT && old_tool == TOOL_TEXT) || - (cur_tool == TOOL_SAVE && old_tool == TOOL_TEXT) || - (cur_tool == TOOL_OPEN && old_tool == TOOL_TEXT) || - (cur_tool == TOOL_NEW && old_tool == TOOL_TEXT) || (cur_tool == TOOL_QUIT && old_tool == TOOL_TEXT)) - { - SDL_BlitSurface(canvas_back, NULL, canvas, NULL); - SDL_FreeSurface(canvas_back); - do_render_cur_text(0); - } - } - if (current_label_node != NULL && current_label_node->save_undoid == 253) - { - aux_label_node = current_label_node; - current_label_node = current_label_node->next_to_down_label_node; - - if (current_label_node == NULL) - start_label_node = NULL; - else - current_label_node->next_to_up_label_node = first_label_node_in_redo_stack; - - derender_node(&aux_label_node); - delete_label_list(&aux_label_node); - have_to_rec_label_node = have_to_rec_label_node_back; + SDL_BlitSurface(canvas_back, NULL, canvas, NULL); + SDL_FreeSurface(canvas_back); do_render_cur_text(0); } + } + if (current_label_node != NULL && current_label_node->save_undoid == 253) + { + aux_label_node = current_label_node; + current_label_node = current_label_node->next_to_down_label_node; + + if (current_label_node == NULL) + start_label_node = NULL; + else + current_label_node->next_to_up_label_node = + first_label_node_in_redo_stack; + + derender_node(&aux_label_node); + delete_label_list(&aux_label_node); + have_to_rec_label_node = have_to_rec_label_node_back; + do_render_cur_text(0); + } } @@ -25603,56 +27291,26 @@ static void highlight_label_nodes() struct label_node *aux_node; if (highlighted_label_node != NULL) + { + aux_node = highlighted_label_node->next_to_up_label_node; + if (aux_node == first_label_node_in_redo_stack) + aux_node = start_label_node; + + + while (aux_node != highlighted_label_node) { - aux_node = highlighted_label_node->next_to_up_label_node; - if (aux_node == first_label_node_in_redo_stack) - aux_node = start_label_node; + if (aux_node->is_enabled) + { + rect.x = aux_node->save_x + button_w * 2; + rect.y = aux_node->save_y; + rect.w = aux_node->save_width; + rect.h = aux_node->save_height; + SDL_FillRect(screen, &rect, + SDL_MapRGBA(screen->format, 0, 0, 0, + SDL_ALPHA_TRANSPARENT)); - while (aux_node != highlighted_label_node) - { - if (aux_node->is_enabled) - { - rect.x = aux_node->save_x + button_w * 2; - rect.y = aux_node->save_y; - rect.w = aux_node->save_width; - rect.h = aux_node->save_height; - - SDL_FillRect(screen, &rect, SDL_MapRGBA(screen->format, 0, 0, 0, SDL_ALPHA_TRANSPARENT)); - - for (j = 2; j < aux_node->save_height / 4; j++) - { - rect1.x = rect.x + j; - rect1.y = rect.y + j; - rect1.w = rect.w - 2 * j; - if (rect1.w < 2) - break; - rect1.h = rect.h - 2 * j; - SDL_FillRect(screen, - &rect1, - SDL_MapRGBA(screen->format, - 4 * j * 200 / aux_node->save_height, - 4 * j * 200 / aux_node->save_height, - 4 * j * 200 / aux_node->save_height, SDL_ALPHA_OPAQUE)); - - SDL_BlitSurface(aux_node->label_node_surface, NULL, screen, &rect); - } - - } - - aux_node = aux_node->next_to_up_label_node; - if (aux_node == first_label_node_in_redo_stack) - aux_node = start_label_node; - } - - aux_node = highlighted_label_node; - rect.x = aux_node->save_x + button_w * 2; - rect.y = aux_node->save_y; - rect.w = aux_node->save_width; - rect.h = aux_node->save_height; - SDL_FillRect(screen, &rect, SDL_MapRGBA(screen->format, 255, 0, 0, SDL_ALPHA_OPAQUE)); - - for (j = 2; j < aux_node->save_height / 4; j++) + for (j = 2; j < aux_node->save_height / 4; j++) { rect1.x = rect.x + j; rect1.y = rect.y + j; @@ -25662,13 +27320,49 @@ static void highlight_label_nodes() rect1.h = rect.h - 2 * j; SDL_FillRect(screen, &rect1, - SDL_MapRGBA(screen->format, 255, 4 * j * 225 / aux_node->save_height, 0, SDL_ALPHA_OPAQUE)); + SDL_MapRGBA(screen->format, + 4 * j * 200 / aux_node->save_height, + 4 * j * 200 / aux_node->save_height, + 4 * j * 200 / aux_node->save_height, + SDL_ALPHA_OPAQUE)); SDL_BlitSurface(aux_node->label_node_surface, NULL, screen, &rect); } - SDL_Flip(screen); + } + + aux_node = aux_node->next_to_up_label_node; + if (aux_node == first_label_node_in_redo_stack) + aux_node = start_label_node; } + + aux_node = highlighted_label_node; + rect.x = aux_node->save_x + button_w * 2; + rect.y = aux_node->save_y; + rect.w = aux_node->save_width; + rect.h = aux_node->save_height; + SDL_FillRect(screen, &rect, + SDL_MapRGBA(screen->format, 255, 0, 0, SDL_ALPHA_OPAQUE)); + + for (j = 2; j < aux_node->save_height / 4; j++) + { + rect1.x = rect.x + j; + rect1.y = rect.y + j; + rect1.w = rect.w - 2 * j; + if (rect1.w < 2) + break; + rect1.h = rect.h - 2 * j; + SDL_FillRect(screen, + &rect1, + SDL_MapRGBA(screen->format, 255, + 4 * j * 225 / aux_node->save_height, 0, + SDL_ALPHA_OPAQUE)); + + SDL_BlitSurface(aux_node->label_node_surface, NULL, screen, &rect); + } + + SDL_Flip(screen); + } } /** @@ -25679,29 +27373,30 @@ static void cycle_highlighted_label_node() struct label_node *aux_node; if (highlighted_label_node) + { + aux_node = highlighted_label_node->next_to_down_label_node; + if (aux_node == NULL) { - aux_node = highlighted_label_node->next_to_down_label_node; - if (aux_node == NULL) - { - aux_node = current_label_node; - } - - if (aux_node->is_enabled) - { - highlighted_label_node = aux_node; - } - else - { - while (aux_node->is_enabled == FALSE && aux_node != highlighted_label_node) - { - aux_node = aux_node->next_to_down_label_node; - if (aux_node == NULL) - aux_node = current_label_node; - if (aux_node->is_enabled) - highlighted_label_node = aux_node; - } - } + aux_node = current_label_node; } + + if (aux_node->is_enabled) + { + highlighted_label_node = aux_node; + } + else + { + while (aux_node->is_enabled == FALSE + && aux_node != highlighted_label_node) + { + aux_node = aux_node->next_to_down_label_node; + if (aux_node == NULL) + aux_node = current_label_node; + if (aux_node->is_enabled) + highlighted_label_node = aux_node; + } + } + } } /** @@ -25712,15 +27407,15 @@ static int are_labels() struct label_node *aux_node; if (current_label_node) + { + aux_node = current_label_node; + while (aux_node) { - aux_node = current_label_node; - while (aux_node) - { - if (aux_node->is_enabled) - return (TRUE); - aux_node = aux_node->next_to_down_label_node; - } + if (aux_node->is_enabled) + return (TRUE); + aux_node = aux_node->next_to_down_label_node; } + } return (FALSE); } @@ -25744,53 +27439,56 @@ int chunk_is_valid(const char *chunk_name, png_unknown_chunk unknown) 'x' == unknown.data[2] && 'p' == unknown.data[3] && 'a' == unknown.data[4] && - 'i' == unknown.data[5] && 'n' == unknown.data[6] && 't' == unknown.data[7] && '\n' == unknown.data[8]) + 'i' == unknown.data[5] && 'n' == unknown.data[6] + && 't' == unknown.data[7] && '\n' == unknown.data[8]) + { + /* Passed the first test, now checking if there are at least + 4 fields in the first 50 bytes of the chunk data */ + count = 9; + fields = 1; + new_field = 1; + while (count < 50) { - /* Passed the first test, now checking if there are at least - 4 fields in the first 50 bytes of the chunk data */ - count = 9; - fields = 1; - new_field = 1; - while (count < 50) - { - if (unknown.data[count] == '\n') - { - if (new_field == 1) - return (FALSE); /* Avoid empty fields */ - fields++; - if (fields == 4) - { /* Last check, see if the sizes match */ - control = malloc(50); - softwr = malloc(50); - sscanf((char *)unknown.data, "%s\n%s\n%d\n%d\n", control, softwr, &unc_size, &comp); - free(control); - free(softwr); - if (count + comp + 1 == unknown.size) - return (TRUE); - else - return (FALSE); - } - new_field = 1; - } + if (unknown.data[count] == '\n') + { + if (new_field == 1) + return (FALSE); /* Avoid empty fields */ + fields++; + if (fields == 4) + { /* Last check, see if the sizes match */ + control = malloc(50); + softwr = malloc(50); + sscanf((char *) unknown.data, "%s\n%s\n%d\n%d\n", control, softwr, + &unc_size, &comp); + free(control); + free(softwr); + if (count + comp + 1 == unknown.size) + return (TRUE); else - { - /* Check if there are decimal values here */ - if ((fields < 4 && fields > 1) && - !((unknown.data[count] == '0') || - (unknown.data[count] == '1') || - (unknown.data[count] == '2') || - (unknown.data[count] == '3') || - (unknown.data[count] == '4') || - (unknown.data[count] == '5') || - (unknown.data[count] == '6') || - (unknown.data[count] == '7') || (unknown.data[count] == '8') || (unknown.data[count] == '9'))) - return (FALSE); - - new_field = 0; - } - count++; + return (FALSE); } + new_field = 1; + } + else + { + /* Check if there are decimal values here */ + if ((fields < 4 && fields > 1) && + !((unknown.data[count] == '0') || + (unknown.data[count] == '1') || + (unknown.data[count] == '2') || + (unknown.data[count] == '3') || + (unknown.data[count] == '4') || + (unknown.data[count] == '5') || + (unknown.data[count] == '6') || + (unknown.data[count] == '7') || (unknown.data[count] == '8') + || (unknown.data[count] == '9'))) + return (FALSE); + + new_field = 0; + } + count++; } + } return (FALSE); } @@ -25799,7 +27497,8 @@ int chunk_is_valid(const char *chunk_name, png_unknown_chunk unknown) * FIXME */ Bytef *get_chunk_data(FILE * fp, char *fname, png_structp png_ptr, - png_infop info_ptr, const char *chunk_name, png_unknown_chunk unknown, int *unc_size) + png_infop info_ptr, const char *chunk_name, + png_unknown_chunk unknown, int *unc_size) { unsigned int i; @@ -25811,55 +27510,56 @@ Bytef *get_chunk_data(FILE * fp, char *fname, png_structp png_ptr, control = malloc(50); softwr = malloc(50); - sscanf((char *)unknown.data, "%s\n%s\n%d\n%d\n", control, softwr, unc_size, &comp); + sscanf((char *) unknown.data, "%s\n%s\n%d\n%d\n", control, softwr, unc_size, + &comp); free(control); free(softwr); comp_buff = malloc(comp * sizeof(Bytef)); if (comp_buff == NULL) - { - fclose(fp); + { + fclose(fp); - png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); + png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); - fprintf(stderr, - "\nError: Couldn't recover the embedded data in %s\n\nUnable to allocate memory for the compressed buffer for %s\n\n", - fname, chunk_name); - draw_tux_text(TUX_OOPS, strerror(errno), 0); - return (NULL); - } + fprintf(stderr, + "\nError: Couldn't recover the embedded data in %s\n\nUnable to allocate memory for the compressed buffer for %s\n\n", + fname, chunk_name); + draw_tux_text(TUX_OOPS, strerror(errno), 0); + return (NULL); + } f = 0; count = 0; for (i = 0; i < unknown.size; i++) + { + if (f > 3) { - if (f > 3) - { - comp_buff[i - count] = unknown.data[i]; - // printf("%d, %d, %d ",i-count, comp_buff[i - count], unknown.data[i]); - } - - if (unknown.data[i] == '\n' && f < 4) - { - f++; - count = i + 1; - } + comp_buff[i - count] = unknown.data[i]; + // printf("%d, %d, %d ",i-count, comp_buff[i - count], unknown.data[i]); } + if (unknown.data[i] == '\n' && f < 4) + { + f++; + count = i + 1; + } + } + unc_buff = malloc(*unc_size * sizeof(Bytef)); if (unc_buff == NULL) - { - fclose(fp); + { + fclose(fp); - png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); + png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); - fprintf(stderr, - "\nError: Couldn't recover the embedded data in %s\n\nUnable to allocate memory for the compressed buffer for %s\n\n", - fname, chunk_name); - draw_tux_text(TUX_OOPS, strerror(errno), 0); - return (NULL); - } + fprintf(stderr, + "\nError: Couldn't recover the embedded data in %s\n\nUnable to allocate memory for the compressed buffer for %s\n\n", + fname, chunk_name); + draw_tux_text(TUX_OOPS, strerror(errno), 0); + return (NULL); + } /* Seems that uncompress() has problems in 64bits systems, so using inflate() Pere 2012/03/28 */ /* unc_err = uncompress(unc_buff, (uLongf *) unc_size, comp_buff, comp); */ @@ -25881,17 +27581,20 @@ Bytef *get_chunk_data(FILE * fp, char *fname, png_structp png_ptr, inflateEnd(zstp); if (unc_err != Z_STREAM_END) - { - fprintf(stderr, "\n error %d, unc %d, comp %d\n", unc_err, *unc_size, comp); - fclose(fp); - png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); - free(comp_buff); - free(unc_buff); + { + fprintf(stderr, "\n error %d, unc %d, comp %d\n", unc_err, *unc_size, + comp); + fclose(fp); + png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); + free(comp_buff); + free(unc_buff); - fprintf(stderr, "Can't recover the embedded data in %s, error in uncompressing data from %s\n\n", fname, chunk_name); - draw_tux_text(TUX_OOPS, strerror(errno), 0); - return (NULL); - } + fprintf(stderr, + "Can't recover the embedded data in %s, error in uncompressing data from %s\n\n", + fname, chunk_name); + draw_tux_text(TUX_OOPS, strerror(errno), 0); + return (NULL); + } free(comp_buff); return (unc_buff); @@ -25929,346 +27632,412 @@ void load_embedded_data(char *fname, SDL_Surface * org_surf) fp = fopen(fname, "rb"); if (!fp) - { - SDL_FreeSurface(org_surf); - return; - } + { + SDL_FreeSurface(org_surf); + return; + } png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (png_ptr == NULL) + { + fclose(fp); + png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL); + + fprintf(stderr, "\nError: Couldn't open the image!\n%s\n\n", fname); + draw_tux_text(TUX_OOPS, strerror(errno), 0); + SDL_FreeSurface(org_surf); + return; + } + else + { +#ifdef DEBUG + printf("%s\n", fname); +#endif + + info_ptr = png_create_info_struct(png_ptr); + if (info_ptr == NULL) { fclose(fp); - png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL); + png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); fprintf(stderr, "\nError: Couldn't open the image!\n%s\n\n", fname); draw_tux_text(TUX_OOPS, strerror(errno), 0); SDL_FreeSurface(org_surf); return; } - else + + png_init_io(png_ptr, fp); + + png_set_keep_unknown_chunks(png_ptr, 3, NULL, 0); + + png_read_info(png_ptr, info_ptr); + + ww = png_get_image_width(png_ptr, info_ptr); + hh = png_get_image_height(png_ptr, info_ptr); + + num_unknowns = (int) png_get_unknown_chunks(png_ptr, info_ptr, &unknowns); + +#ifdef DEBUG + printf("num_unknowns %i\n", num_unknowns); +#endif + if (num_unknowns) { + have_label_delta = have_label_data = have_background = have_foreground = + FALSE; + ldelta = ldata = fgnd = bgnd = FALSE; + + /* Need to get things in order, as we can't enforce any order in custom chunks, + we need to go around them 3 times */ + + /* First we search for the things that usually were in the .dat file, so if a starter or a + template is found and if it is not modified, we can load it clean (i.e. not rebluring a + blured when scaled one) */ + for (u = 0; u < num_unknowns; u++) + { #ifdef DEBUG - printf("%s\n", fname); + printf("%s, %d\n", unknowns[u].name, (int) unknowns[u].size); #endif - info_ptr = png_create_info_struct(png_ptr); - if (info_ptr == NULL) + if (chunk_is_valid("tpDT", unknowns[u])) { - fclose(fp); - png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); - - fprintf(stderr, "\nError: Couldn't open the image!\n%s\n\n", fname); - draw_tux_text(TUX_OOPS, strerror(errno), 0); - SDL_FreeSurface(org_surf); - return; - } - - png_init_io(png_ptr, fp); - - png_set_keep_unknown_chunks(png_ptr, 3, NULL, 0); - - png_read_info(png_ptr, info_ptr); - - ww = png_get_image_width(png_ptr, info_ptr); - hh = png_get_image_height(png_ptr, info_ptr); - - num_unknowns = (int)png_get_unknown_chunks(png_ptr, info_ptr, &unknowns); - #ifdef DEBUG - printf("num_unknowns %i\n", num_unknowns); + printf("Valid tpDT\n"); #endif - if (num_unknowns) - { - have_label_delta = have_label_data = have_background = have_foreground = FALSE; - ldelta = ldata = fgnd = bgnd = FALSE; + fi = fmemopen(unknowns[u].data, unknowns[u].size, "r"); + if (fi == NULL) + { + fclose(fp); + png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); - /* Need to get things in order, as we can't enforce any order in custom chunks, - we need to go around them 3 times */ - - /* First we search for the things that usually were in the .dat file, so if a starter or a - template is found and if it is not modified, we can load it clean (i.e. not rebluring a - blured when scaled one) */ - for (u = 0; u < num_unknowns; u++) - { -#ifdef DEBUG - printf("%s, %d\n", unknowns[u].name, (int)unknowns[u].size); -#endif - - if (chunk_is_valid("tpDT", unknowns[u])) - { -#ifdef DEBUG - printf("Valid tpDT\n"); -#endif - fi = fmemopen(unknowns[u].data, unknowns[u].size, "r"); - if (fi == NULL) - { - fclose(fp); - png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); - - fprintf(stderr, "\nError: Couldn't load the data embedded in %s\n\n", fname); - draw_tux_text(TUX_OOPS, strerror(errno), 0); - SDL_FreeSurface(org_surf); - return; /* Refusing to go further with the other chunks */ - } - - /* Put fi position at the right place after the chunk headers */ - control = malloc(50); - CHAR_PTR_TMP = fgets(control, 49, fi); - CHAR_PTR_TMP = fgets(control, 49, fi); - CHAR_PTR_TMP = fgets(control, 49, fi); - CHAR_PTR_TMP = fgets(control, 49, fi); - (void)CHAR_PTR_TMP; - free(control); - - /* fi will be closed in load_starter_id() */ - load_starter_id(NULL, fi); - if (!starter_modified) - { - /* Code adapted from load_current() */ - if (starter_id[0] != '\0') - { - load_starter(starter_id); - - if (starter_mirrored && img_starter) - mirror_starter(); - - if (starter_flipped && img_starter) - flip_starter(); - } - else if (template_id[0] != '\0') - { - load_template(template_id); - } - } - } - /* Also check what we have there */ - if (chunk_is_valid("tpBK", unknowns[u])) - have_background = TRUE; - if (chunk_is_valid("tpFG", unknowns[u])) - have_foreground = TRUE; - if (chunk_is_valid("tpLD", unknowns[u])) - have_label_delta = TRUE; - if (chunk_is_valid("tpLL", unknowns[u])) - have_label_data = TRUE; - } - - /* Recover the labels and apply the diff from label to canvas. */ - if (!disable_label && have_label_delta && have_label_data) - { - for (u = 0; u < num_unknowns; u++) - { - if (chunk_is_valid("tpLD", unknowns[u])) - { -#ifdef DEBUG - printf("Valid tpLD\n"); -#endif - - unc_buff = get_chunk_data(fp, fname, png_ptr, info_ptr, "tpLD", unknowns[u], &unc_size); - if (unc_buff == NULL) - { - if (are_labels()) - { - delete_label_list(&start_label_node); - start_label_node = current_label_node = NULL; - } - - SDL_FreeSurface(org_surf); - return; - } - else - { - SDL_LockSurface(org_surf); - for (j = 0; j < hh; j++) - for (i = 0; i < ww; i++) - { - if ((Uint8) unc_buff[4 * j * ww + 4 * i + 3] == SDL_ALPHA_OPAQUE) - putpixels[org_surf->format->BytesPerPixel] (org_surf, i, j, - SDL_MapRGB(org_surf->format, - unc_buff[4 * (j * ww + i)], - unc_buff[4 * (j * ww + i) + 1], - unc_buff[4 * (j * ww + i) + - 2])); - } - } - - SDL_UnlockSurface(org_surf); - - free(unc_buff); - ldelta = TRUE; - } - - /* Label Data */ - if (!disable_label && chunk_is_valid("tpLL", unknowns[u])) - { -#ifdef DEBUG - printf("Valid tpLL\n"); -#endif - - unc_buff = get_chunk_data(fp, fname, png_ptr, info_ptr, "tpLL", unknowns[u], &unc_size); - if (unc_buff == NULL) - { - SDL_FreeSurface(org_surf); - return; - } - else - { - fi = fmemopen(unc_buff, unc_size, "rb"); - if (fi == NULL) - { - fprintf(stderr, "Can't recover the label data embedded in %s, error in create file stream\n\n", - fname); - fclose(fp); - png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); - free(unc_buff); - SDL_FreeSurface(org_surf); - - draw_tux_text(TUX_OOPS, strerror(errno), 0); - return; - } - else - load_info_about_label_surface(fi); - } - - free(unc_buff); - ldata = TRUE; -#ifdef DEBUG - printf("Out of label data\n"); -#endif - } - } - } - /* Apply the original canvas */ - if (ldelta && ldata) - autoscale_copy_smear_free(org_surf, canvas, SDL_BlitSurface); - else + fprintf(stderr, + "\nError: Couldn't load the data embedded in %s\n\n", + fname); + draw_tux_text(TUX_OOPS, strerror(errno), 0); SDL_FreeSurface(org_surf); + return; /* Refusing to go further with the other chunks */ + } - /* Third run, back and foreground */ - if (have_background || have_foreground) + /* Put fi position at the right place after the chunk headers */ + control = malloc(50); + CHAR_PTR_TMP = fgets(control, 49, fi); + CHAR_PTR_TMP = fgets(control, 49, fi); + CHAR_PTR_TMP = fgets(control, 49, fi); + CHAR_PTR_TMP = fgets(control, 49, fi); + (void) CHAR_PTR_TMP; + free(control); + + /* fi will be closed in load_starter_id() */ + load_starter_id(NULL, fi); + if (!starter_modified) + { + /* Code adapted from load_current() */ + if (starter_id[0] != '\0') { - for (u = 0; u < num_unknowns; u++) + load_starter(starter_id); + + if (starter_mirrored && img_starter) + mirror_starter(); + + if (starter_flipped && img_starter) + flip_starter(); + } + else if (template_id[0] != '\0') + { + load_template(template_id); + } + } + } + /* Also check what we have there */ + if (chunk_is_valid("tpBK", unknowns[u])) + have_background = TRUE; + if (chunk_is_valid("tpFG", unknowns[u])) + have_foreground = TRUE; + if (chunk_is_valid("tpLD", unknowns[u])) + have_label_delta = TRUE; + if (chunk_is_valid("tpLL", unknowns[u])) + have_label_data = TRUE; + } + + /* Recover the labels and apply the diff from label to canvas. */ + if (!disable_label && have_label_delta && have_label_data) + { + for (u = 0; u < num_unknowns; u++) + { + if (chunk_is_valid("tpLD", unknowns[u])) + { +#ifdef DEBUG + printf("Valid tpLD\n"); +#endif + + unc_buff = + get_chunk_data(fp, fname, png_ptr, info_ptr, "tpLD", + unknowns[u], &unc_size); + if (unc_buff == NULL) + { + if (are_labels()) + { + delete_label_list(&start_label_node); + start_label_node = current_label_node = NULL; + } + + SDL_FreeSurface(org_surf); + return; + } + else + { + SDL_LockSurface(org_surf); + for (j = 0; j < hh; j++) + for (i = 0; i < ww; i++) { - if ((starter_modified || !img_starter_bkgd) && chunk_is_valid("tpBG", unknowns[u])) - { - unc_buff = get_chunk_data(fp, fname, png_ptr, info_ptr, "tpBG", unknowns[u], &unc_size); - if (unc_buff == NULL) - return; - aux_surf = - SDL_CreateRGBSurface(0, ww, hh, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Gmask, 0); - if (aux_surf == NULL) - { -#ifdef DEBUG - fprintf(stderr, "Can't recover the background data embedded in %s, error in create aux image\n\n", - fname); -#endif - fclose(fp); - png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); - free(unc_buff); - - draw_tux_text(TUX_OOPS, strerror(errno), 0); - - free(unc_buff); - return; - } - SDL_LockSurface(aux_surf); - -#ifdef DEBUG - printf("Bkgd!!!\n"); -#endif - for (j = 0; j < hh; j++) - for (i = 0; i < ww; i++) - putpixels[aux_surf->format->BytesPerPixel] (aux_surf, i, j, - SDL_MapRGB - (aux_surf->format, - unc_buff[3 * j * ww + 3 * i], - unc_buff[3 * j * ww + 3 * i + 1], - unc_buff[3 * j * ww + 3 * i + 2])); - SDL_UnlockSurface(aux_surf); - - if (img_starter_bkgd) - SDL_FreeSurface(img_starter_bkgd); - - if (aux_surf->w != canvas->w || aux_surf->h != canvas->h) - { - img_starter_bkgd = SDL_CreateRGBSurface(SDL_SWSURFACE, - canvas->w, - canvas->h, - canvas->format->BitsPerPixel, - canvas->format->Rmask, - canvas->format->Gmask, canvas->format->Bmask, 0); - - autoscale_copy_smear_free(aux_surf, img_starter_bkgd, SDL_BlitSurface); - } - free(unc_buff); - } - - if ((starter_modified || !img_starter) && chunk_is_valid("tpFG", unknowns[u])) - { -#ifdef DEBUG - printf("Frgd!!!\n"); -#endif - - unc_buff = get_chunk_data(fp, fname, png_ptr, info_ptr, "tpFG", unknowns[u], &unc_size); - if (unc_buff == NULL) - return; - - aux_surf = SDL_CreateRGBSurface(canvas->flags, ww, hh, - canvas->format->BitsPerPixel, - canvas->format->Rmask, - canvas->format->Gmask, canvas->format->Gmask, TPAINT_AMASK); - if (aux_surf == NULL) - { - fprintf(stderr, "Can't recover the foreground data embedded in %s, error in create aux image\n\n", - fname); - fclose(fp); - png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); - free(unc_buff); - - draw_tux_text(TUX_OOPS, strerror(errno), 0); - - free(unc_buff); - return; - } - - SDL_LockSurface(aux_surf); - for (j = 0; j < hh; j++) - for (i = 0; i < ww; i++) - { - putpixels[aux_surf->format->BytesPerPixel] (aux_surf, i, j, - SDL_MapRGBA - (aux_surf->format, - unc_buff[4 * j * ww + 4 * i], - unc_buff[4 * j * ww + 4 * i + 1], - unc_buff[4 * j * ww + 4 * i + 2], - unc_buff[4 * j * ww + 4 * i + 3])); - } - SDL_UnlockSurface(aux_surf); - - if (img_starter) - SDL_FreeSurface(img_starter); - - - /* Code adapted from load_starter */ - img_starter = SDL_CreateRGBSurface(canvas->flags, - canvas->w, canvas->h, - canvas->format->BitsPerPixel, - canvas->format->Rmask, - canvas->format->Gmask, canvas->format->Bmask, TPAINT_AMASK); - - /* 3rd arg ignored for RGBA surfaces */ - // SDL_SetAlpha(aux_surf, SDL_RLEACCEL, SDL_ALPHA_OPAQUE); - SDL_SetSurfaceBlendMode(aux_surf, SDL_BLENDMODE_NONE); - autoscale_copy_smear_free(aux_surf, img_starter, NondefectiveBlit); - // SDL_SetAlpha(img_starter, SDL_ALPHA_OPAQUE); - SDL_SetSurfaceBlendMode(img_starter, SDL_BLENDMODE_NONE); - - free(unc_buff); - } + if ((Uint8) unc_buff[4 * j * ww + 4 * i + 3] == + SDL_ALPHA_OPAQUE) + putpixels[org_surf->format->BytesPerPixel] (org_surf, i, + j, + SDL_MapRGB + (org_surf-> + format, + unc_buff[4 * + (j * + ww + + + i)], + unc_buff[4 * + (j * + ww + + + i) + + + 1], + unc_buff[4 * + (j * + ww + + + i) + + + 2])); } } + + SDL_UnlockSurface(org_surf); + + free(unc_buff); + ldelta = TRUE; + } + + /* Label Data */ + if (!disable_label && chunk_is_valid("tpLL", unknowns[u])) + { +#ifdef DEBUG + printf("Valid tpLL\n"); +#endif + + unc_buff = + get_chunk_data(fp, fname, png_ptr, info_ptr, "tpLL", + unknowns[u], &unc_size); + if (unc_buff == NULL) + { + SDL_FreeSurface(org_surf); + return; + } + else + { + fi = fmemopen(unc_buff, unc_size, "rb"); + if (fi == NULL) + { + fprintf(stderr, + "Can't recover the label data embedded in %s, error in create file stream\n\n", + fname); + fclose(fp); + png_destroy_read_struct(&png_ptr, &info_ptr, + (png_infopp) NULL); + free(unc_buff); + SDL_FreeSurface(org_surf); + + draw_tux_text(TUX_OOPS, strerror(errno), 0); + return; + } + else + load_info_about_label_surface(fi); + } + + free(unc_buff); + ldata = TRUE; +#ifdef DEBUG + printf("Out of label data\n"); +#endif + } } + } + /* Apply the original canvas */ + if (ldelta && ldata) + autoscale_copy_smear_free(org_surf, canvas, SDL_BlitSurface); + else + SDL_FreeSurface(org_surf); + + /* Third run, back and foreground */ + if (have_background || have_foreground) + { + for (u = 0; u < num_unknowns; u++) + { + if ((starter_modified || !img_starter_bkgd) + && chunk_is_valid("tpBG", unknowns[u])) + { + unc_buff = + get_chunk_data(fp, fname, png_ptr, info_ptr, "tpBG", + unknowns[u], &unc_size); + if (unc_buff == NULL) + return; + aux_surf = + SDL_CreateRGBSurface(0, ww, hh, canvas->format->BitsPerPixel, + canvas->format->Rmask, + canvas->format->Gmask, + canvas->format->Gmask, 0); + if (aux_surf == NULL) + { +#ifdef DEBUG + fprintf(stderr, + "Can't recover the background data embedded in %s, error in create aux image\n\n", + fname); +#endif + fclose(fp); + png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); + free(unc_buff); + + draw_tux_text(TUX_OOPS, strerror(errno), 0); + + free(unc_buff); + return; + } + SDL_LockSurface(aux_surf); + +#ifdef DEBUG + printf("Bkgd!!!\n"); +#endif + for (j = 0; j < hh; j++) + for (i = 0; i < ww; i++) + putpixels[aux_surf->format->BytesPerPixel] (aux_surf, i, j, + SDL_MapRGB + (aux_surf->format, + unc_buff[3 * j * + ww + + 3 * i], + unc_buff[3 * j * + ww + + 3 * i + + 1], + unc_buff[3 * j * + ww + + 3 * i + + 2])); + SDL_UnlockSurface(aux_surf); + + if (img_starter_bkgd) + SDL_FreeSurface(img_starter_bkgd); + + if (aux_surf->w != canvas->w || aux_surf->h != canvas->h) + { + img_starter_bkgd = SDL_CreateRGBSurface(SDL_SWSURFACE, + canvas->w, + canvas->h, + canvas->format-> + BitsPerPixel, + canvas->format->Rmask, + canvas->format->Gmask, + canvas->format->Bmask, + 0); + + autoscale_copy_smear_free(aux_surf, img_starter_bkgd, + SDL_BlitSurface); + } + free(unc_buff); + } + + if ((starter_modified || !img_starter) + && chunk_is_valid("tpFG", unknowns[u])) + { +#ifdef DEBUG + printf("Frgd!!!\n"); +#endif + + unc_buff = + get_chunk_data(fp, fname, png_ptr, info_ptr, "tpFG", + unknowns[u], &unc_size); + if (unc_buff == NULL) + return; + + aux_surf = SDL_CreateRGBSurface(canvas->flags, ww, hh, + canvas->format->BitsPerPixel, + canvas->format->Rmask, + canvas->format->Gmask, + canvas->format->Gmask, + TPAINT_AMASK); + if (aux_surf == NULL) + { + fprintf(stderr, + "Can't recover the foreground data embedded in %s, error in create aux image\n\n", + fname); + fclose(fp); + png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); + free(unc_buff); + + draw_tux_text(TUX_OOPS, strerror(errno), 0); + + free(unc_buff); + return; + } + + SDL_LockSurface(aux_surf); + for (j = 0; j < hh; j++) + for (i = 0; i < ww; i++) + { + putpixels[aux_surf->format->BytesPerPixel] (aux_surf, i, j, + SDL_MapRGBA + (aux_surf->format, + unc_buff[4 * j * + ww + + 4 * i], + unc_buff[4 * j * + ww + + 4 * i + + 1], + unc_buff[4 * j * + ww + + 4 * i + + 2], + unc_buff[4 * j * + ww + + 4 * i + + 3])); + } + SDL_UnlockSurface(aux_surf); + + if (img_starter) + SDL_FreeSurface(img_starter); + + + /* Code adapted from load_starter */ + img_starter = SDL_CreateRGBSurface(canvas->flags, + canvas->w, canvas->h, + canvas->format->BitsPerPixel, + canvas->format->Rmask, + canvas->format->Gmask, + canvas->format->Bmask, + TPAINT_AMASK); + + /* 3rd arg ignored for RGBA surfaces */ + // SDL_SetAlpha(aux_surf, SDL_RLEACCEL, SDL_ALPHA_OPAQUE); + SDL_SetSurfaceBlendMode(aux_surf, SDL_BLENDMODE_NONE); + autoscale_copy_smear_free(aux_surf, img_starter, + NondefectiveBlit); + // SDL_SetAlpha(img_starter, SDL_ALPHA_OPAQUE); + SDL_SetSurfaceBlendMode(img_starter, SDL_BLENDMODE_NONE); + + free(unc_buff); + } + } + } } + } png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); fclose(fp); @@ -26295,18 +28064,18 @@ static void show_available_papersizes(int exitcode) cnt = 0; while (ppr != NULL) + { + fprintf(fi, "\t%s", papername(ppr)); + cnt++; + if (cnt == 5) { - fprintf(fi, "\t%s", papername(ppr)); - cnt++; - if (cnt == 5) - { - cnt = 0; - fprintf(fi, "\n"); - } - - ppr = papernext(ppr); + cnt = 0; + fprintf(fi, "\n"); } + ppr = papernext(ppr); + } + fprintf(fi, "\n"); if (cnt != 0) fprintf(fi, "\n"); @@ -26318,7 +28087,8 @@ static void show_available_papersizes(int exitcode) /** * FIXME */ -static void parse_file_options(struct cfginfo *restrict tmpcfg, const char *filename) +static void parse_file_options(struct cfginfo *restrict tmpcfg, + const char *filename) { char str[256]; char *arg; @@ -26331,59 +28101,61 @@ static void parse_file_options(struct cfginfo *restrict tmpcfg, const char *file line = 0; while (fgets(str, sizeof(str), fi)) + { + line++; + + strip_trailing_whitespace(str); + + /* Comments and blank lines can be safely ignored */ + if (str[0] == '#' || str[0] == '\0') + continue; + + /* Expecting alphanumeric at the beginning of a line; ignore (and complain about) the rest */ + if (!isalnum(*str)) { - line++; - - strip_trailing_whitespace(str); - - /* Comments and blank lines can be safely ignored */ - if (str[0] == '#' || str[0] == '\0') - continue; - - /* Expecting alphanumeric at the beginning of a line; ignore (and complain about) the rest */ - if (!isalnum(*str)) - { - fprintf(stderr, - "Warning: do not understand '%s' on line %d of '%s'\n", str, line, filename); - continue; - } - - /* Expecting to be in the form of OPTION=ARG; ignore (and complain about) the rest */ - arg = strchr(str, '='); - if (arg) - { - *arg++ = '\0'; - } - else - { - fprintf(stderr, - "Warning: do not understand '%s' on line %d of '%s'\n", str, line, filename); - continue; - } - -#ifdef __linux__ -#ifndef __ANDROID__ - /* Perform shell expansion */ - wordexp_t result; - - wordexp(arg, &result, 0); - arg = strdup(result.we_wordv[0]); - wordfree(&result); -#endif -#endif - - /* FIXME: leaking mem here, but the trouble is that these - strings get mixed in with ones from .data and .rodata - and free() isn't smart about the situation -- also some - of the strings end up being kept around */ - parse_one_option(tmpcfg, str, strdup(arg), filename); - -#ifdef __linux__ -#ifndef __ANDROID__ - free(arg); -#endif -#endif + fprintf(stderr, + "Warning: do not understand '%s' on line %d of '%s'\n", str, + line, filename); + continue; } + + /* Expecting to be in the form of OPTION=ARG; ignore (and complain about) the rest */ + arg = strchr(str, '='); + if (arg) + { + *arg++ = '\0'; + } + else + { + fprintf(stderr, + "Warning: do not understand '%s' on line %d of '%s'\n", str, + line, filename); + continue; + } + +#ifdef __linux__ +#ifndef __ANDROID__ + /* Perform shell expansion */ + wordexp_t result; + + wordexp(arg, &result, 0); + arg = strdup(result.we_wordv[0]); + wordfree(&result); +#endif +#endif + + /* FIXME: leaking mem here, but the trouble is that these + strings get mixed in with ones from .data and .rodata + and free() isn't smart about the situation -- also some + of the strings end up being kept around */ + parse_one_option(tmpcfg, str, strdup(arg), filename); + +#ifdef __linux__ +#ifndef __ANDROID__ + free(arg); +#endif +#endif + } fclose(fi); /* These interact in horrid ways. */ @@ -26424,50 +28196,50 @@ static void parse_argv_options(struct cfginfo *restrict tmpcfg, char *argv[]) }; while ((str = *++argv)) + { + if (str[0] == '-' && str[1] != '-' && str[1] != '\0') { - if (str[0] == '-' && str[1] != '-' && str[1] != '\0') - { - int i, found = 0; + int i, found = 0; - for (i = 0; short_synonyms[i][0] != NULL && !found; i++) - { - if (strcmp(short_synonyms[i][0], str) == 0) - { - if (argv[1] && argv[1][0] != '-') - arg = *++argv; - else - arg = NULL; - parse_one_option(tmpcfg, short_synonyms[i][1], arg, NULL); - found = 1; - } - } - if (found) - continue; - } - else if (str[0] == '-' && str[1] == '-' && str[2]) + for (i = 0; short_synonyms[i][0] != NULL && !found; i++) + { + if (strcmp(short_synonyms[i][0], str) == 0) { - str += 2; - arg = strchr(str, '='); - if (arg) - *arg++ = '\0'; - else if (argv[1] && argv[1][0] != '-') + if (argv[1] && argv[1][0] != '-') arg = *++argv; - parse_one_option(tmpcfg, str, arg, NULL); - continue; + else + arg = NULL; + parse_one_option(tmpcfg, short_synonyms[i][1], arg, NULL); + found = 1; } - fprintf(stderr, "%s is not understood\n", *argv); - show_usage(63); - exit(1); + } + if (found) + continue; } + else if (str[0] == '-' && str[1] == '-' && str[2]) + { + str += 2; + arg = strchr(str, '='); + if (arg) + *arg++ = '\0'; + else if (argv[1] && argv[1][0] != '-') + arg = *++argv; + parse_one_option(tmpcfg, str, arg, NULL); + continue; + } + fprintf(stderr, "%s is not understood\n", *argv); + show_usage(63); + exit(1); + } /* These interact in horrid ways. */ if (tmpcfg->parsertmp_lang && tmpcfg->parsertmp_locale) - { - fprintf(stderr, - "Error: command line option '--lang=%s' overrides option '--locale=%s'\n", - tmpcfg->parsertmp_lang, tmpcfg->parsertmp_locale); - exit(92); - } + { + fprintf(stderr, + "Error: command line option '--lang=%s' overrides option '--locale=%s'\n", + tmpcfg->parsertmp_lang, tmpcfg->parsertmp_locale); + exit(92); + } if (tmpcfg->parsertmp_lang) tmpcfg->parsertmp_locale = PARSE_CLOBBER; else if (tmpcfg->parsertmp_locale) @@ -26483,14 +28255,16 @@ static void tmpcfg_merge(struct cfginfo *loser, const struct cfginfo *winner) int i = CFGINFO_MAXOFFSET / sizeof(char *); while (i--) - { - const char *cfgitem; + { + const char *cfgitem; - memcpy(&cfgitem, i * sizeof(const char *) + (const char *)winner, sizeof cfgitem); - if (!cfgitem) - continue; - memcpy(i * sizeof(const char *) + (char *)loser, &cfgitem, sizeof cfgitem); - } + memcpy(&cfgitem, i * sizeof(const char *) + (const char *) winner, + sizeof cfgitem); + if (!cfgitem) + continue; + memcpy(i * sizeof(const char *) + (char *) loser, &cfgitem, + sizeof cfgitem); + } } /** @@ -26499,7 +28273,7 @@ static void tmpcfg_merge(struct cfginfo *loser, const struct cfginfo *winner) static void setup_config(char *argv[]) { char str[128]; - char * picturesdir; + char *picturesdir; #if !defined(_WIN32) && !defined(__ANDROID__) const char *home = getenv("HOME"); @@ -26530,88 +28304,92 @@ static void setup_config(char *argv[]) #if !defined(_WIN32) && !defined(__ANDROID__) if (!home) - { - /* Woah, don't know where $HOME is? */ - fprintf(stderr, "Error: You have no $HOME environment variable!\n"); - exit(1); - } + { + /* Woah, don't know where $HOME is? */ + fprintf(stderr, "Error: You have no $HOME environment variable!\n"); + exit(1); + } #endif /* == SAVEDIR == */ if (tmpcfg_cmd.savedir) savedir = strdup(tmpcfg_cmd.savedir); else - { + { #ifdef _WIN32 - savedir = GetDefaultSaveDir("TuxPaint"); + savedir = GetDefaultSaveDir("TuxPaint"); #elif __BEOS__ - savedir = strdup("./tuxpaint"); + savedir = strdup("./tuxpaint"); #elif __HAIKU__ - /* Haiku: Make use of find_directory() */ - dev_t volume = dev_for_path("/boot"); - char buffer[B_PATH_NAME_LENGTH + B_FILE_NAME_LENGTH]; - status_t result; + /* Haiku: Make use of find_directory() */ + dev_t volume = dev_for_path("/boot"); + char buffer[B_PATH_NAME_LENGTH + B_FILE_NAME_LENGTH]; + status_t result; - result = find_directory(B_USER_SETTINGS_DIRECTORY, volume, false, buffer, sizeof(buffer)); - asprintf((char **)&savedir, "%s/%s", buffer, "TuxPaint"); + result = + find_directory(B_USER_SETTINGS_DIRECTORY, volume, false, buffer, + sizeof(buffer)); + asprintf((char **) &savedir, "%s/%s", buffer, "TuxPaint"); #elif __APPLE__ - savedir = strdup(apple_preferencesPath()); + savedir = strdup(apple_preferencesPath()); #elif __ANDROID__ - savedir = SDL_AndroidGetExternalStoragePath(); + savedir = SDL_AndroidGetExternalStoragePath(); #else - int tmp; + int tmp; - tmp = asprintf((char **)&savedir, "%s/%s", home, ".tuxpaint"); - if (tmp < 0) - { - fprintf(stderr, "Can't set savedir\n"); - exit(91); - } -#endif + tmp = asprintf((char **) &savedir, "%s/%s", home, ".tuxpaint"); + if (tmp < 0) + { + fprintf(stderr, "Can't set savedir\n"); + exit(91); } +#endif + } /* == EXPORTDIR == */ if (tmpcfg_cmd.exportdir) exportdir = strdup(tmpcfg_cmd.exportdir); else - { - /* FIXME: Need assist for: - * __BEOS__ - * __HAIKU__ - */ + { + /* FIXME: Need assist for: + * __BEOS__ + * __HAIKU__ + */ #ifdef WIN32 - picturesdir = GetUserImageDir(); + picturesdir = GetUserImageDir(); #elif __APPLE__ - picturesdir = strdup(apple_picturesPath()); + picturesdir = strdup(apple_picturesPath()); #elif __ANDROID__ - picturesdir = strdup(SDL_AndroidGetExternalStoragePath()); - char* substring = strstr(picturesdir, "/Android"); - if (substring != NULL) { - strcpy(substring, "/Pictures"); - } -#else - picturesdir = get_xdg_user_dir("PICTURES", "Pictures"); -#endif - safe_snprintf(str, sizeof(str), "%s/TuxPaint", picturesdir); - free(picturesdir); - exportdir = strdup(str); + picturesdir = strdup(SDL_AndroidGetExternalStoragePath()); + char *substring = strstr(picturesdir, "/Android"); + if (substring != NULL) + { + strcpy(substring, "/Pictures"); } +#else + picturesdir = get_xdg_user_dir("PICTURES", "Pictures"); +#endif + safe_snprintf(str, sizeof(str), "%s/TuxPaint", picturesdir); + free(picturesdir); + exportdir = strdup(str); + } /* Load options from user's own configuration (".rc" / ".cfg") file: */ #if defined(_WIN32) /* Default local config file in users savedir directory on Windows */ - safe_snprintf(str, sizeof(str), "%s/tuxpaint.cfg", savedir); /* FIXME */ + safe_snprintf(str, sizeof(str), "%s/tuxpaint.cfg", savedir); /* FIXME */ #elif defined(__BEOS__) || defined(__HAIKU__) /* BeOS: Use a "tuxpaint.cfg" file: */ - strcpy(str, "tuxpaint.cfg"); /* safe; sufficient size */ + strcpy(str, "tuxpaint.cfg"); /* safe; sufficient size */ #elif defined(__APPLE__) /* macOS, iOS: Use a "tuxpaint.cfg" file in the Tux Paint application support folder */ safe_snprintf(str, sizeof(str), "%s/tuxpaint.cfg", apple_preferencesPath()); #elif defined(__ANDROID__) /* Try to find the user's config file */ /* This file is writed by the tuxpaint config activity when the user runs it */ - safe_snprintf(str, sizeof(str), "%s/tuxpaint.cfg", SDL_AndroidGetExternalStoragePath()); + safe_snprintf(str, sizeof(str), "%s/tuxpaint.cfg", + SDL_AndroidGetExternalStoragePath()); #else @@ -26631,38 +28409,39 @@ static void setup_config(char *argv[]) #endif if (tmpcfg_usr.parsertmp_sysconfig != PARSE_NO) - { - struct cfginfo tmpcfg_sys; + { + struct cfginfo tmpcfg_sys; - memset(&tmpcfg_sys, '\0', sizeof tmpcfg_sys); + memset(&tmpcfg_sys, '\0', sizeof tmpcfg_sys); #ifdef _WIN32 - /* global config file in the application directory */ - parse_file_options(&tmpcfg_sys, "tuxpaint.cfg"); + /* global config file in the application directory */ + parse_file_options(&tmpcfg_sys, "tuxpaint.cfg"); #elif defined(__APPLE__) - /* EP added this conditional section for Mac to fix - folder & extension inconsistency with Tux Paint Config application) */ - /* macOS, iOS: Use a "tuxpaint.cfg" file in the *global* Tux Paint - application support folder */ - safe_snprintf(str, sizeof(str), "%s/tuxpaint.cfg", apple_globalPreferencesPath()); - parse_file_options(&tmpcfg_sys, str); + /* EP added this conditional section for Mac to fix + folder & extension inconsistency with Tux Paint Config application) */ + /* macOS, iOS: Use a "tuxpaint.cfg" file in the *global* Tux Paint + application support folder */ + safe_snprintf(str, sizeof(str), "%s/tuxpaint.cfg", + apple_globalPreferencesPath()); + parse_file_options(&tmpcfg_sys, str); #elif defined(__ANDROID__) - /* Load the config file we provide in assets/etc/tuxpaint.cfg */ - snprintf(str, sizeof(str), "etc/tuxpaint.cfg"); + /* Load the config file we provide in assets/etc/tuxpaint.cfg */ + snprintf(str, sizeof(str), "etc/tuxpaint.cfg"); - parse_file_options(&tmpcfg_sys, str); + parse_file_options(&tmpcfg_sys, str); #else - /* normally /etc/tuxpaint/tuxpaint.conf */ - parse_file_options(&tmpcfg_sys, CONFDIR "tuxpaint.conf"); + /* normally /etc/tuxpaint/tuxpaint.conf */ + parse_file_options(&tmpcfg_sys, CONFDIR "tuxpaint.conf"); #endif - tmpcfg_merge(&tmpcfg, &tmpcfg_sys); - } + tmpcfg_merge(&tmpcfg, &tmpcfg_sys); + } tmpcfg_merge(&tmpcfg, &tmpcfg_usr); if (tmpcfg.savedir) - { - free((char *)savedir); - savedir = tmpcfg.savedir; - } + { + free((char *) savedir); + savedir = tmpcfg.savedir; + } datadir = tmpcfg.datadir ? tmpcfg.datadir : savedir; @@ -26672,7 +28451,9 @@ static void setup_config(char *argv[]) tmpcfg.parsertmp_lang = NULL; if (tmpcfg.parsertmp_locale == PARSE_CLOBBER) tmpcfg.parsertmp_locale = NULL; - button_label_y_nudge = setup_i18n(tmpcfg.parsertmp_lang, tmpcfg.parsertmp_locale, &num_wished_langs); + button_label_y_nudge = + setup_i18n(tmpcfg.parsertmp_lang, tmpcfg.parsertmp_locale, + &num_wished_langs); /* FIXME: most of this is not required before starting the font scanner */ @@ -26728,81 +28509,88 @@ static void setup_config(char *argv[]) #undef SETBOOL if (tmpcfg.parsertmp_windowsize) - { - char *endp1; - char *endp2; - int w = strtoul(tmpcfg.parsertmp_windowsize, &endp1, 10); - int h = strtoul(endp1 + 1, &endp2, 10); + { + char *endp1; + char *endp2; + int w = strtoul(tmpcfg.parsertmp_windowsize, &endp1, 10); + int h = strtoul(endp1 + 1, &endp2, 10); - if (tmpcfg.parsertmp_windowsize == endp1 || endp1 + 1 == endp2 || *endp1 != 'x' || *endp2) - { - fprintf(stderr, "Window size '%s' is not understood.\n", tmpcfg.parsertmp_windowsize); - exit(97); - } - if (w < 500 || w > 32000 || h < 480 || h > 32000 || h > w * 3 || w > h * 4) - { - fprintf(stderr, "Window size '%s' is not reasonable.\n", tmpcfg.parsertmp_windowsize); - exit(93); - } - WINDOW_WIDTH = w; - WINDOW_HEIGHT = h; + if (tmpcfg.parsertmp_windowsize == endp1 || endp1 + 1 == endp2 + || *endp1 != 'x' || *endp2) + { + fprintf(stderr, "Window size '%s' is not understood.\n", + tmpcfg.parsertmp_windowsize); + exit(97); } + if (w < 500 || w > 32000 || h < 480 || h > 32000 || h > w * 3 + || w > h * 4) + { + fprintf(stderr, "Window size '%s' is not reasonable.\n", + tmpcfg.parsertmp_windowsize); + exit(93); + } + WINDOW_WIDTH = w; + WINDOW_HEIGHT = h; + } if (tmpcfg.parsertmp_fullscreen_native) - { - /* should conflict with other fullscreen/native_screensize setting? */ - if (!strcmp(tmpcfg.parsertmp_fullscreen_native, "native")) - native_screensize = 1; - fullscreen = strcmp(tmpcfg.parsertmp_fullscreen_native, "no"); - } + { + /* should conflict with other fullscreen/native_screensize setting? */ + if (!strcmp(tmpcfg.parsertmp_fullscreen_native, "native")) + native_screensize = 1; + fullscreen = strcmp(tmpcfg.parsertmp_fullscreen_native, "no"); + } if (tmpcfg.button_size) + { + if (strstr(tmpcfg.button_size, "auto")) + button_size_auto = 1; + else { - if (strstr(tmpcfg.button_size, "auto")) - button_size_auto = 1; - else - { - if (strtof(tmpcfg.button_size, NULL) < 24 || strtof(tmpcfg.button_size, NULL) > 192) - { - fprintf(stderr, "Button size (now %s) must be between 24 and 192.\n", tmpcfg.button_size); - exit(1); - } - button_scale = strtof(tmpcfg.button_size, NULL) / ORIGINAL_BUTTON_SIZE; - } + if (strtof(tmpcfg.button_size, NULL) < 24 + || strtof(tmpcfg.button_size, NULL) > 192) + { + fprintf(stderr, "Button size (now %s) must be between 24 and 192.\n", + tmpcfg.button_size); + exit(1); + } + button_scale = strtof(tmpcfg.button_size, NULL) / ORIGINAL_BUTTON_SIZE; } + } else button_scale = 1; if (tmpcfg.colors_rows) + { + if (strtof(tmpcfg.colors_rows, NULL) > 3) { - if (strtof(tmpcfg.colors_rows, NULL) > 3) - { - fprintf(stderr, "Color rows (now %s) must be between 1 and 3.\n", tmpcfg.colors_rows); - exit(1); - } - colors_rows = strtof(tmpcfg.colors_rows, NULL); + fprintf(stderr, "Color rows (now %s) must be between 1 and 3.\n", + tmpcfg.colors_rows); + exit(1); } + colors_rows = strtof(tmpcfg.colors_rows, NULL); + } else colors_rows = 1; if (tmpcfg.stamp_size_override) + { + if (!strcmp(tmpcfg.stamp_size_override, "default")) + stamp_size_override = -1; + else { - if (!strcmp(tmpcfg.stamp_size_override, "default")) - stamp_size_override = -1; - else - { - /* FIXME: needs to be a scaling factor */ - stamp_size_override = atoi(tmpcfg.stamp_size_override); - if (stamp_size_override > 10) - stamp_size_override = 10; - } + /* FIXME: needs to be a scaling factor */ + stamp_size_override = atoi(tmpcfg.stamp_size_override); + if (stamp_size_override > 10) + stamp_size_override = 10; } + } /* FIXME: make this dynamic (accelerometer or OLPC XO-1 rotation button) */ if (tmpcfg.rotate_orientation) rotate_orientation = !strcmp(tmpcfg.rotate_orientation, "portrait"); /* alternative is "landscape" */ if (tmpcfg.colorfile) safe_strncpy(colorfile, tmpcfg.colorfile, sizeof(colorfile)); if (tmpcfg.print_delay) - { - print_delay = atoi(tmpcfg.print_delay); - last_print_time = -print_delay; - } + { + print_delay = atoi(tmpcfg.print_delay); + last_print_time = -print_delay; + } #ifdef PAPER_H if (tmpcfg.printcommand) printcommand = tmpcfg.printcommand; @@ -26810,308 +28598,368 @@ static void setup_config(char *argv[]) altprintcommand = tmpcfg.altprintcommand; #endif if (tmpcfg.alt_print_command_default) - { - /* FIXME: probably need extra variables */ - if (!strcmp(tmpcfg.alt_print_command_default, "always")) - alt_print_command_default = ALTPRINT_ALWAYS; - else if (!strcmp(tmpcfg.alt_print_command_default, "never")) - alt_print_command_default = ALTPRINT_NEVER; - else - alt_print_command_default = ALTPRINT_MOD; /* default ("mod") */ - } + { + /* FIXME: probably need extra variables */ + if (!strcmp(tmpcfg.alt_print_command_default, "always")) + alt_print_command_default = ALTPRINT_ALWAYS; + else if (!strcmp(tmpcfg.alt_print_command_default, "never")) + alt_print_command_default = ALTPRINT_NEVER; + else + alt_print_command_default = ALTPRINT_MOD; /* default ("mod") */ + } #ifdef PAPER_H if (tmpcfg.papersize) papersize = tmpcfg.papersize; #endif if (tmpcfg.joystick_dev) + { + if (strcmp(tmpcfg.joystick_dev, "list") == 0) { - if (strcmp(tmpcfg.joystick_dev, "list") == 0) - { - joystick_dev = -1; - } - else - { - if (strtof(tmpcfg.joystick_dev, NULL) < 0 || strtof(tmpcfg.joystick_dev, NULL) > 100) - { - fprintf(stderr, "Joystick dev (now %s) must be between 0 and 100.\n", tmpcfg.joystick_dev); - exit(1); - } - joystick_dev = strtof(tmpcfg.joystick_dev, NULL); - } + joystick_dev = -1; } + else + { + if (strtof(tmpcfg.joystick_dev, NULL) < 0 + || strtof(tmpcfg.joystick_dev, NULL) > 100) + { + fprintf(stderr, "Joystick dev (now %s) must be between 0 and 100.\n", + tmpcfg.joystick_dev); + exit(1); + } + joystick_dev = strtof(tmpcfg.joystick_dev, NULL); + } + } if (tmpcfg.joystick_slowness) + { + if (strtof(tmpcfg.joystick_slowness, NULL) < 0 + || strtof(tmpcfg.joystick_slowness, NULL) > 500) { - if (strtof(tmpcfg.joystick_slowness, NULL) < 0 || strtof(tmpcfg.joystick_slowness, NULL) > 500) - { - fprintf(stderr, "Joystick slowness (now %s) must be between 0 and 500.\n", tmpcfg.joystick_slowness); - exit(1); - } - joystick_slowness = strtof(tmpcfg.joystick_slowness, NULL); + fprintf(stderr, + "Joystick slowness (now %s) must be between 0 and 500.\n", + tmpcfg.joystick_slowness); + exit(1); } + joystick_slowness = strtof(tmpcfg.joystick_slowness, NULL); + } if (tmpcfg.joystick_lowthreshold) + { + if (strtof(tmpcfg.joystick_lowthreshold, NULL) < 0 + || strtof(tmpcfg.joystick_lowthreshold, NULL) > 32766) { - if (strtof(tmpcfg.joystick_lowthreshold, NULL) < 0 || strtof(tmpcfg.joystick_lowthreshold, NULL) > 32766) - { - /* FIXME: Find better exit code */ - fprintf(stderr, "Joystick lower threshold (now %s) must be between 0 and 32766", tmpcfg.joystick_lowthreshold); - exit(1); - } - joystick_low_threshold = strtof(tmpcfg.joystick_lowthreshold, NULL); + /* FIXME: Find better exit code */ + fprintf(stderr, + "Joystick lower threshold (now %s) must be between 0 and 32766", + tmpcfg.joystick_lowthreshold); + exit(1); } + joystick_low_threshold = strtof(tmpcfg.joystick_lowthreshold, NULL); + } if (tmpcfg.joystick_maxsteps) + { + if (strtof(tmpcfg.joystick_maxsteps, NULL) < 1 + || strtof(tmpcfg.joystick_maxsteps, NULL) > 7) { - if (strtof(tmpcfg.joystick_maxsteps, NULL) < 1 || strtof(tmpcfg.joystick_maxsteps, NULL) > 7) - { - /* FIXME: Find better exit code */ - fprintf(stderr, "Joystick max steps (now %s) must be between 1 and 7", tmpcfg.joystick_maxsteps); - exit(1); - } - joystick_maxsteps = strtof(tmpcfg.joystick_maxsteps, NULL); + /* FIXME: Find better exit code */ + fprintf(stderr, "Joystick max steps (now %s) must be between 1 and 7", + tmpcfg.joystick_maxsteps); + exit(1); } + joystick_maxsteps = strtof(tmpcfg.joystick_maxsteps, NULL); + } if (tmpcfg.joystick_hat_slowness) + { + if (strtof(tmpcfg.joystick_hat_slowness, NULL) < 0 + || strtof(tmpcfg.joystick_hat_slowness, NULL) > 500) { - if (strtof(tmpcfg.joystick_hat_slowness, NULL) < 0 || strtof(tmpcfg.joystick_hat_slowness, NULL) > 500) - { - fprintf(stderr, "Joystick hat slowness (now %s) must be between 0 and 500.\n", tmpcfg.joystick_hat_slowness); - exit(1); - } - joystick_hat_slowness = strtof(tmpcfg.joystick_hat_slowness, NULL); + fprintf(stderr, + "Joystick hat slowness (now %s) must be between 0 and 500.\n", + tmpcfg.joystick_hat_slowness); + exit(1); } + joystick_hat_slowness = strtof(tmpcfg.joystick_hat_slowness, NULL); + } if (tmpcfg.joystick_hat_timeout) + { + if (strtof(tmpcfg.joystick_hat_timeout, NULL) < 0 + || strtof(tmpcfg.joystick_hat_timeout, NULL) > 3000) { - if (strtof(tmpcfg.joystick_hat_timeout, NULL) < 0 || strtof(tmpcfg.joystick_hat_timeout, NULL) > 3000) - { - /* FIXME: Find better exit code */ - fprintf(stderr, "Joystick hat timeout (now %s) must be between 0 and 3000", tmpcfg.joystick_hat_timeout); - exit(1); - } - joystick_hat_timeout = strtof(tmpcfg.joystick_hat_timeout, NULL); + /* FIXME: Find better exit code */ + fprintf(stderr, + "Joystick hat timeout (now %s) must be between 0 and 3000", + tmpcfg.joystick_hat_timeout); + exit(1); } + joystick_hat_timeout = strtof(tmpcfg.joystick_hat_timeout, NULL); + } if (tmpcfg.joystick_button_escape) + { + if (strtof(tmpcfg.joystick_button_escape, NULL) < 0 + || strtof(tmpcfg.joystick_button_escape, NULL) > 254) { - if (strtof(tmpcfg.joystick_button_escape, NULL) < 0 || strtof(tmpcfg.joystick_button_escape, NULL) > 254) - { - /* FIXME: Find better exit code */ - fprintf(stderr, "Joystick button escape shortcurt (now %s) must be between 0 and 254", tmpcfg.joystick_button_escape); - exit(1); - } - joystick_button_escape = strtof(tmpcfg.joystick_button_escape, NULL); + /* FIXME: Find better exit code */ + fprintf(stderr, + "Joystick button escape shortcurt (now %s) must be between 0 and 254", + tmpcfg.joystick_button_escape); + exit(1); } + joystick_button_escape = strtof(tmpcfg.joystick_button_escape, NULL); + } if (tmpcfg.joystick_button_selectbrushtool) + { + if (strtof(tmpcfg.joystick_button_selectbrushtool, NULL) < 0 + || strtof(tmpcfg.joystick_button_selectbrushtool, NULL) > 254) { - if (strtof(tmpcfg.joystick_button_selectbrushtool, NULL) < 0 - || strtof(tmpcfg.joystick_button_selectbrushtool, NULL) > 254) - { - /* FIXME: Find better exit code */ - fprintf(stderr, "Joystick button brush tool shortcurt (now %s) must be between 0 and 254", - tmpcfg.joystick_button_selectbrushtool); - exit(1); - } - joystick_button_selectbrushtool = strtof(tmpcfg.joystick_button_selectbrushtool, NULL); + /* FIXME: Find better exit code */ + fprintf(stderr, + "Joystick button brush tool shortcurt (now %s) must be between 0 and 254", + tmpcfg.joystick_button_selectbrushtool); + exit(1); } + joystick_button_selectbrushtool = + strtof(tmpcfg.joystick_button_selectbrushtool, NULL); + } if (tmpcfg.joystick_button_selectstamptool) + { + if (strtof(tmpcfg.joystick_button_selectstamptool, NULL) < 0 + || strtof(tmpcfg.joystick_button_selectstamptool, NULL) > 254) { - if (strtof(tmpcfg.joystick_button_selectstamptool, NULL) < 0 - || strtof(tmpcfg.joystick_button_selectstamptool, NULL) > 254) - { - /* FIXME: Find better exit code */ - fprintf(stderr, "Joystick button stamp tool shortcurt (now %s) must be between 0 and 254", - tmpcfg.joystick_button_selectstamptool); - exit(1); - } - joystick_button_selectstamptool = strtof(tmpcfg.joystick_button_selectstamptool, NULL); + /* FIXME: Find better exit code */ + fprintf(stderr, + "Joystick button stamp tool shortcurt (now %s) must be between 0 and 254", + tmpcfg.joystick_button_selectstamptool); + exit(1); } + joystick_button_selectstamptool = + strtof(tmpcfg.joystick_button_selectstamptool, NULL); + } if (tmpcfg.joystick_button_selectlinestool) + { + if (strtof(tmpcfg.joystick_button_selectlinestool, NULL) < 0 + || strtof(tmpcfg.joystick_button_selectlinestool, NULL) > 254) { - if (strtof(tmpcfg.joystick_button_selectlinestool, NULL) < 0 - || strtof(tmpcfg.joystick_button_selectlinestool, NULL) > 254) - { - /* FIXME: Find better exit code */ - fprintf(stderr, "Joystick button lines tool shortcurt (now %s) must be between 0 and 254", - tmpcfg.joystick_button_selectlinestool); - exit(1); - } - joystick_button_selectlinestool = strtof(tmpcfg.joystick_button_selectlinestool, NULL); + /* FIXME: Find better exit code */ + fprintf(stderr, + "Joystick button lines tool shortcurt (now %s) must be between 0 and 254", + tmpcfg.joystick_button_selectlinestool); + exit(1); } + joystick_button_selectlinestool = + strtof(tmpcfg.joystick_button_selectlinestool, NULL); + } if (tmpcfg.joystick_button_selectshapestool) + { + if (strtof(tmpcfg.joystick_button_selectshapestool, NULL) < 0 + || strtof(tmpcfg.joystick_button_selectshapestool, NULL) > 254) { - if (strtof(tmpcfg.joystick_button_selectshapestool, NULL) < 0 - || strtof(tmpcfg.joystick_button_selectshapestool, NULL) > 254) - { - /* FIXME: Find better exit code */ - fprintf(stderr, "Joystick button shapes tool shortcurt (now %s) must be between 0 and 254", - tmpcfg.joystick_button_selectshapestool); - exit(1); - } - joystick_button_selectshapestool = strtof(tmpcfg.joystick_button_selectshapestool, NULL); + /* FIXME: Find better exit code */ + fprintf(stderr, + "Joystick button shapes tool shortcurt (now %s) must be between 0 and 254", + tmpcfg.joystick_button_selectshapestool); + exit(1); } + joystick_button_selectshapestool = + strtof(tmpcfg.joystick_button_selectshapestool, NULL); + } if (tmpcfg.joystick_button_selecttexttool) + { + if (strtof(tmpcfg.joystick_button_selecttexttool, NULL) < 0 + || strtof(tmpcfg.joystick_button_selecttexttool, NULL) > 254) { - if (strtof(tmpcfg.joystick_button_selecttexttool, NULL) < 0 - || strtof(tmpcfg.joystick_button_selecttexttool, NULL) > 254) - { - /* FIXME: Find better exit code */ - fprintf(stderr, "Joystick button text tool shortcurt (now %s) must be between 0 and 254", - tmpcfg.joystick_button_selecttexttool); - exit(1); - } - joystick_button_selecttexttool = strtof(tmpcfg.joystick_button_selecttexttool, NULL); + /* FIXME: Find better exit code */ + fprintf(stderr, + "Joystick button text tool shortcurt (now %s) must be between 0 and 254", + tmpcfg.joystick_button_selecttexttool); + exit(1); } + joystick_button_selecttexttool = + strtof(tmpcfg.joystick_button_selecttexttool, NULL); + } if (tmpcfg.joystick_button_selectlabeltool) + { + if (strtof(tmpcfg.joystick_button_selectlabeltool, NULL) < 0 + || strtof(tmpcfg.joystick_button_selectlabeltool, NULL) > 254) { - if (strtof(tmpcfg.joystick_button_selectlabeltool, NULL) < 0 - || strtof(tmpcfg.joystick_button_selectlabeltool, NULL) > 254) - { - /* FIXME: Find better exit code */ - fprintf(stderr, "Joystick button label tool shortcurt (now %s) must be between 0 and 254", - tmpcfg.joystick_button_selectlabeltool); - exit(1); - } - joystick_button_selectlabeltool = strtof(tmpcfg.joystick_button_selectlabeltool, NULL); + /* FIXME: Find better exit code */ + fprintf(stderr, + "Joystick button label tool shortcurt (now %s) must be between 0 and 254", + tmpcfg.joystick_button_selectlabeltool); + exit(1); } + joystick_button_selectlabeltool = + strtof(tmpcfg.joystick_button_selectlabeltool, NULL); + } if (tmpcfg.joystick_button_selectmagictool) + { + if (strtof(tmpcfg.joystick_button_selectmagictool, NULL) < 0 + || strtof(tmpcfg.joystick_button_selectmagictool, NULL) > 254) { - if (strtof(tmpcfg.joystick_button_selectmagictool, NULL) < 0 - || strtof(tmpcfg.joystick_button_selectmagictool, NULL) > 254) - { - /* FIXME: Find better exit code */ - fprintf(stderr, "Joystick button magic tool shortcurt (now %s) must be between 0 and 254", - tmpcfg.joystick_button_selectmagictool); - exit(1); - } - joystick_button_selectmagictool = strtof(tmpcfg.joystick_button_selectmagictool, NULL); + /* FIXME: Find better exit code */ + fprintf(stderr, + "Joystick button magic tool shortcurt (now %s) must be between 0 and 254", + tmpcfg.joystick_button_selectmagictool); + exit(1); } + joystick_button_selectmagictool = + strtof(tmpcfg.joystick_button_selectmagictool, NULL); + } if (tmpcfg.joystick_button_undo) + { + if (strtof(tmpcfg.joystick_button_undo, NULL) < 0 + || strtof(tmpcfg.joystick_button_undo, NULL) > 254) { - if (strtof(tmpcfg.joystick_button_undo, NULL) < 0 || strtof(tmpcfg.joystick_button_undo, NULL) > 254) - { - /* FIXME: Find better exit code */ - fprintf(stderr, "Joystick button undo shortcurt (now %s) must be between 0 and 254", tmpcfg.joystick_button_undo); - exit(1); - } - joystick_button_undo = strtof(tmpcfg.joystick_button_undo, NULL); + /* FIXME: Find better exit code */ + fprintf(stderr, + "Joystick button undo shortcurt (now %s) must be between 0 and 254", + tmpcfg.joystick_button_undo); + exit(1); } + joystick_button_undo = strtof(tmpcfg.joystick_button_undo, NULL); + } if (tmpcfg.joystick_button_redo) + { + if (strtof(tmpcfg.joystick_button_redo, NULL) < 0 + || strtof(tmpcfg.joystick_button_redo, NULL) > 254) { - if (strtof(tmpcfg.joystick_button_redo, NULL) < 0 || strtof(tmpcfg.joystick_button_redo, NULL) > 254) - { - /* FIXME: Find better exit code */ - fprintf(stderr, "Joystick button redo shortcurt (now %s) must be between 0 and 254", tmpcfg.joystick_button_redo); - exit(1); - } - joystick_button_redo = strtof(tmpcfg.joystick_button_redo, NULL); + /* FIXME: Find better exit code */ + fprintf(stderr, + "Joystick button redo shortcurt (now %s) must be between 0 and 254", + tmpcfg.joystick_button_redo); + exit(1); } + joystick_button_redo = strtof(tmpcfg.joystick_button_redo, NULL); + } if (tmpcfg.joystick_button_selecterasertool) + { + if (strtof(tmpcfg.joystick_button_selecterasertool, NULL) < 0 + || strtof(tmpcfg.joystick_button_selecterasertool, NULL) > 254) { - if (strtof(tmpcfg.joystick_button_selecterasertool, NULL) < 0 - || strtof(tmpcfg.joystick_button_selecterasertool, NULL) > 254) - { - /* FIXME: Find better exit code */ - fprintf(stderr, "Joystick button eraser tool shortcurt (now %s) must be between 0 and 254", - tmpcfg.joystick_button_selecterasertool); - exit(1); - } - joystick_button_selecterasertool = strtof(tmpcfg.joystick_button_selecterasertool, NULL); + /* FIXME: Find better exit code */ + fprintf(stderr, + "Joystick button eraser tool shortcurt (now %s) must be between 0 and 254", + tmpcfg.joystick_button_selecterasertool); + exit(1); } + joystick_button_selecterasertool = + strtof(tmpcfg.joystick_button_selecterasertool, NULL); + } if (tmpcfg.joystick_button_new) + { + if (strtof(tmpcfg.joystick_button_new, NULL) < 0 + || strtof(tmpcfg.joystick_button_new, NULL) > 254) { - if (strtof(tmpcfg.joystick_button_new, NULL) < 0 || strtof(tmpcfg.joystick_button_new, NULL) > 254) - { - /* FIXME: Find better exit code */ - fprintf(stderr, "Joystick button new shortcurt (now %s) must be between 0 and 254", tmpcfg.joystick_button_new); - exit(1); - } - joystick_button_new = strtof(tmpcfg.joystick_button_new, NULL); + /* FIXME: Find better exit code */ + fprintf(stderr, + "Joystick button new shortcurt (now %s) must be between 0 and 254", + tmpcfg.joystick_button_new); + exit(1); } + joystick_button_new = strtof(tmpcfg.joystick_button_new, NULL); + } if (tmpcfg.joystick_button_open) + { + if (strtof(tmpcfg.joystick_button_open, NULL) < 0 + || strtof(tmpcfg.joystick_button_open, NULL) > 254) { - if (strtof(tmpcfg.joystick_button_open, NULL) < 0 || strtof(tmpcfg.joystick_button_open, NULL) > 254) - { - /* FIXME: Find better exit code */ - fprintf(stderr, "Joystick button open shortcurt (now %s) must be between 0 and 254", tmpcfg.joystick_button_open); - exit(1); - } - joystick_button_open = strtof(tmpcfg.joystick_button_open, NULL); + /* FIXME: Find better exit code */ + fprintf(stderr, + "Joystick button open shortcurt (now %s) must be between 0 and 254", + tmpcfg.joystick_button_open); + exit(1); } + joystick_button_open = strtof(tmpcfg.joystick_button_open, NULL); + } if (tmpcfg.joystick_button_save) + { + if (strtof(tmpcfg.joystick_button_save, NULL) < 0 + || strtof(tmpcfg.joystick_button_save, NULL) > 254) { - if (strtof(tmpcfg.joystick_button_save, NULL) < 0 || strtof(tmpcfg.joystick_button_save, NULL) > 254) - { - /* FIXME: Find better exit code */ - fprintf(stderr, "Joystick button save shortcurt (now %s) must be between 0 and 254", tmpcfg.joystick_button_save); - exit(1); - } - joystick_button_save = strtof(tmpcfg.joystick_button_save, NULL); + /* FIXME: Find better exit code */ + fprintf(stderr, + "Joystick button save shortcurt (now %s) must be between 0 and 254", + tmpcfg.joystick_button_save); + exit(1); } + joystick_button_save = strtof(tmpcfg.joystick_button_save, NULL); + } if (tmpcfg.joystick_button_pagesetup) + { + if (strtof(tmpcfg.joystick_button_pagesetup, NULL) < 0 + || strtof(tmpcfg.joystick_button_pagesetup, NULL) > 254) { - if (strtof(tmpcfg.joystick_button_pagesetup, NULL) < 0 || strtof(tmpcfg.joystick_button_pagesetup, NULL) > 254) - { - /* FIXME: Find better exit code */ - fprintf(stderr, "Joystick button page setup shortcurt (now %s) must be between 0 and 254", - tmpcfg.joystick_button_pagesetup); - exit(1); - } - joystick_button_pagesetup = strtof(tmpcfg.joystick_button_pagesetup, NULL); + /* FIXME: Find better exit code */ + fprintf(stderr, + "Joystick button page setup shortcurt (now %s) must be between 0 and 254", + tmpcfg.joystick_button_pagesetup); + exit(1); } + joystick_button_pagesetup = + strtof(tmpcfg.joystick_button_pagesetup, NULL); + } if (tmpcfg.joystick_button_print) + { + if (strtof(tmpcfg.joystick_button_print, NULL) < 0 + || strtof(tmpcfg.joystick_button_print, NULL) > 254) { - if (strtof(tmpcfg.joystick_button_print, NULL) < 0 || strtof(tmpcfg.joystick_button_print, NULL) > 254) - { - /* FIXME: Find better exit code */ - fprintf(stderr, "Joystick button print shortcurt (now %s) must be between 0 and 254", tmpcfg.joystick_button_print); - exit(1); - } - joystick_button_print = strtof(tmpcfg.joystick_button_print, NULL); + /* FIXME: Find better exit code */ + fprintf(stderr, + "Joystick button print shortcurt (now %s) must be between 0 and 254", + tmpcfg.joystick_button_print); + exit(1); } + joystick_button_print = strtof(tmpcfg.joystick_button_print, NULL); + } if (tmpcfg.joystick_buttons_ignore) - { - char *token; + { + char *token; - token = strtok((char *) tmpcfg.joystick_buttons_ignore, ","); - while (token != NULL) - { - if (strtof(token, NULL) < 0 || strtof(token, NULL) > 254) - { - /* FIXME: Find better exit code */ - fprintf(stderr, "Joystick buttons must be between 0 and 254 (don't like %s)", tmpcfg.joystick_buttons_ignore); - exit(1); - } - joystick_buttons_ignore[joystick_buttons_ignore_len++] = strtof(token, NULL); - token = strtok(NULL, ","); - } + token = strtok((char *) tmpcfg.joystick_buttons_ignore, ","); + while (token != NULL) + { + if (strtof(token, NULL) < 0 || strtof(token, NULL) > 254) + { + /* FIXME: Find better exit code */ + fprintf(stderr, + "Joystick buttons must be between 0 and 254 (don't like %s)", + tmpcfg.joystick_buttons_ignore); + exit(1); + } + joystick_buttons_ignore[joystick_buttons_ignore_len++] = + strtof(token, NULL); + token = strtok(NULL, ","); } + } /* having any of theese implies having onscreen keyboard setted */ if (tmpcfg.onscreen_keyboard_layout) - { - onscreen_keyboard_layout = strdup(tmpcfg.onscreen_keyboard_layout); - onscreen_keyboard = TRUE; - } + { + onscreen_keyboard_layout = strdup(tmpcfg.onscreen_keyboard_layout); + onscreen_keyboard = TRUE; + } if (tmpcfg.onscreen_keyboard_disable_change) - { - onscreen_keyboard = TRUE; - } + { + onscreen_keyboard = TRUE; + } #ifdef DEBUG - printf("\n\nPromptless save:\nask: %d\nnew: %d\nover: %d\n\n", _promptless_save_over_ask, _promptless_save_over_new, + printf("\n\nPromptless save:\nask: %d\nnew: %d\nover: %d\n\n", + _promptless_save_over_ask, _promptless_save_over_new, _promptless_save_over); #endif if (_promptless_save_over_ask) - { - promptless_save = SAVE_OVER_PROMPT; - } + { + promptless_save = SAVE_OVER_PROMPT; + } else if (_promptless_save_over_new) - { - promptless_save = SAVE_OVER_NO; - } + { + promptless_save = SAVE_OVER_NO; + } else if (_promptless_save_over) - { - promptless_save = SAVE_OVER_ALWAYS; - } + { + promptless_save = SAVE_OVER_ALWAYS; + } } @@ -27143,41 +28991,41 @@ static void chdir_to_binary(char *argv0) with MINGW/MSYS easier */ if (argv0) - { - char *app_path = strdup(argv0); - char *slash = strrchr(app_path, '/'); + { + char *app_path = strdup(argv0); + char *slash = strrchr(app_path, '/'); #if defined(__MACOS__) - // On macOS, execution is deep inside the app bundle. - // E.g., "/Applications/TuxPaint.app/Contents/MacOS/tuxpaint" - // But we want to point somewhere higher up, say to "Contents", so we can access - // the resources in Resources folder. So move up one level. - int levels = 1; /* we need to back up 1 level */ + // On macOS, execution is deep inside the app bundle. + // E.g., "/Applications/TuxPaint.app/Contents/MacOS/tuxpaint" + // But we want to point somewhere higher up, say to "Contents", so we can access + // the resources in Resources folder. So move up one level. + int levels = 1; /* we need to back up 1 level */ - while ((levels-- > 0) && (slash)) - { - *slash = '\0'; /* this overwrites the \0 at end of string */ - slash = strrchr(app_path, '/'); /* so we can carry on our back-pedaling... */ - } + while ((levels-- > 0) && (slash)) + { + *slash = '\0'; /* this overwrites the \0 at end of string */ + slash = strrchr(app_path, '/'); /* so we can carry on our back-pedaling... */ + } #endif - if (!slash) - { - slash = strrchr(app_path, '\\'); - } - if (slash) - { - *(slash + 1) = '\0'; - chdir(app_path); - } - free(app_path); - /* - getcwd(curdir, sizeof(curdir)); - printf("New current directory for runtime: %s\n", curdir); - */ + if (!slash) + { + slash = strrchr(app_path, '\\'); } + if (slash) + { + *(slash + 1) = '\0'; + chdir(app_path); + } + free(app_path); + /* + getcwd(curdir, sizeof(curdir)); + printf("New current directory for runtime: %s\n", curdir); + */ + } #else - (void)argv0; + (void) argv0; #endif } @@ -27194,136 +29042,151 @@ static void setup_colors(void) /* Load colors, or use default ones: */ if (colorfile[0] != '\0') + { + fi = fopen(colorfile, "r"); + if (fi == NULL) { - fi = fopen(colorfile, "r"); - if (fi == NULL) - { - fprintf(stderr, "\nWarning, could not open color file. Using defaults.\n"); - perror(colorfile); - colorfile[0] = '\0'; - } - else - { - int max = 0, per = 5; - char str[80], tmp_str[80]; - int count; - - NUM_COLORS = 0; - - do - { - if (fgets(str, sizeof(str), fi)) - { - if (!feof(fi)) - { - if (NUM_COLORS + 1 > max) - { - color_hexes = realloc(color_hexes, sizeof(Uint8 *) * (max + per)); - color_names = realloc(color_names, sizeof(char *) * (max + per)); - - for (i = max; i < max + per; i++) - color_hexes[i] = malloc(sizeof(Uint8) * 3); - - max = max + per; - } - - while (str[strlen(str) - 1] == '\n' || str[strlen(str) - 1] == '\r') - str[strlen(str) - 1] = '\0'; - - if (str[0] == '#') - { - /* Hex form */ - - sscanf(str + 1, "%s %n", tmp_str, &count); - - if (strlen(tmp_str) == 6) - { - /* Byte (#rrggbb) form */ - - color_hexes[NUM_COLORS][0] = (hex2dec(tmp_str[0]) << 4) + hex2dec(tmp_str[1]); - color_hexes[NUM_COLORS][1] = (hex2dec(tmp_str[2]) << 4) + hex2dec(tmp_str[3]); - color_hexes[NUM_COLORS][2] = (hex2dec(tmp_str[4]) << 4) + hex2dec(tmp_str[5]); - - color_names[NUM_COLORS] = strdup(str + count); - NUM_COLORS++; - } - else if (strlen(tmp_str) == 3) - { - /* Nybble (#rgb) form */ - - color_hexes[NUM_COLORS][0] = (hex2dec(tmp_str[0]) << 4) + hex2dec(tmp_str[0]); - color_hexes[NUM_COLORS][1] = (hex2dec(tmp_str[1]) << 4) + hex2dec(tmp_str[1]); - color_hexes[NUM_COLORS][2] = (hex2dec(tmp_str[2]) << 4) + hex2dec(tmp_str[2]); - - color_names[NUM_COLORS] = strdup(str + count); - NUM_COLORS++; - } - } - else - { - /* Assume int form */ - - if (sscanf(str, "%hu %hu %hu %n", - (short unsigned int *)&(color_hexes[NUM_COLORS][0]), - (short unsigned int *)&(color_hexes[NUM_COLORS][1]), - (short unsigned int *)&(color_hexes[NUM_COLORS][2]), &count) >= 3) - { - color_names[NUM_COLORS] = strdup(str + count); - NUM_COLORS++; - } - } - } - } - } - while (!feof(fi)); - - if (NUM_COLORS < 2) - { - fprintf(stderr, "\nWarning, not enough colors in color file. Using defaults.\n"); - fprintf(stderr, "%s\n", colorfile); - colorfile[0] = '\0'; - - for (i = 0; i < NUM_COLORS; i++) - { - free(color_names[i]); - free(color_hexes[i]); - } - - free(color_names); - free(color_hexes); - } - } + fprintf(stderr, + "\nWarning, could not open color file. Using defaults.\n"); + perror(colorfile); + colorfile[0] = '\0'; } + else + { + int max = 0, per = 5; + char str[80], tmp_str[80]; + int count; + + NUM_COLORS = 0; + + do + { + if (fgets(str, sizeof(str), fi)) + { + if (!feof(fi)) + { + if (NUM_COLORS + 1 > max) + { + color_hexes = + realloc(color_hexes, sizeof(Uint8 *) * (max + per)); + color_names = + realloc(color_names, sizeof(char *) * (max + per)); + + for (i = max; i < max + per; i++) + color_hexes[i] = malloc(sizeof(Uint8) * 3); + + max = max + per; + } + + while (str[strlen(str) - 1] == '\n' + || str[strlen(str) - 1] == '\r') + str[strlen(str) - 1] = '\0'; + + if (str[0] == '#') + { + /* Hex form */ + + sscanf(str + 1, "%s %n", tmp_str, &count); + + if (strlen(tmp_str) == 6) + { + /* Byte (#rrggbb) form */ + + color_hexes[NUM_COLORS][0] = + (hex2dec(tmp_str[0]) << 4) + hex2dec(tmp_str[1]); + color_hexes[NUM_COLORS][1] = + (hex2dec(tmp_str[2]) << 4) + hex2dec(tmp_str[3]); + color_hexes[NUM_COLORS][2] = + (hex2dec(tmp_str[4]) << 4) + hex2dec(tmp_str[5]); + + color_names[NUM_COLORS] = strdup(str + count); + NUM_COLORS++; + } + else if (strlen(tmp_str) == 3) + { + /* Nybble (#rgb) form */ + + color_hexes[NUM_COLORS][0] = + (hex2dec(tmp_str[0]) << 4) + hex2dec(tmp_str[0]); + color_hexes[NUM_COLORS][1] = + (hex2dec(tmp_str[1]) << 4) + hex2dec(tmp_str[1]); + color_hexes[NUM_COLORS][2] = + (hex2dec(tmp_str[2]) << 4) + hex2dec(tmp_str[2]); + + color_names[NUM_COLORS] = strdup(str + count); + NUM_COLORS++; + } + } + else + { + /* Assume int form */ + + if (sscanf(str, "%hu %hu %hu %n", + (short unsigned int *) &(color_hexes[NUM_COLORS][0]), + (short unsigned int *) &(color_hexes[NUM_COLORS][1]), + (short unsigned int *) &(color_hexes[NUM_COLORS][2]), + &count) >= 3) + { + color_names[NUM_COLORS] = strdup(str + count); + NUM_COLORS++; + } + } + } + } + } + while (!feof(fi)); + + if (NUM_COLORS < 2) + { + fprintf(stderr, + "\nWarning, not enough colors in color file. Using defaults.\n"); + fprintf(stderr, "%s\n", colorfile); + colorfile[0] = '\0'; + + for (i = 0; i < NUM_COLORS; i++) + { + free(color_names[i]); + free(color_hexes[i]); + } + + free(color_names); + free(color_hexes); + } + } + } /* Use default, if no file specified (or trouble opening it) */ if (colorfile[0] == '\0') + { + NUM_COLORS = NUM_DEFAULT_COLORS; + + color_hexes = malloc(sizeof(Uint8 *) * NUM_COLORS); + color_names = malloc(sizeof(char *) * NUM_COLORS); + + for (i = 0; i < NUM_COLORS; i++) { - NUM_COLORS = NUM_DEFAULT_COLORS; + color_hexes[i] = malloc(sizeof(Uint8 *) * 3); - color_hexes = malloc(sizeof(Uint8 *) * NUM_COLORS); - color_names = malloc(sizeof(char *) * NUM_COLORS); + for (j = 0; j < 3; j++) + color_hexes[i][j] = default_color_hexes[i][j]; - for (i = 0; i < NUM_COLORS; i++) - { - color_hexes[i] = malloc(sizeof(Uint8 *) * 3); - - for (j = 0; j < 3; j++) - color_hexes[i][j] = default_color_hexes[i][j]; - - color_names[i] = strdup(default_color_names[i]); - } + color_names[i] = strdup(default_color_names[i]); } + } /* Add room for dynamic color options at the end of the list: */ - color_hexes = (Uint8 **) realloc(color_hexes, sizeof(Uint8 *) * (NUM_COLORS + 3)); - color_names = (char **)realloc(color_names, sizeof(char *) * (NUM_COLORS + 3)); + color_hexes = + (Uint8 **) realloc(color_hexes, sizeof(Uint8 *) * (NUM_COLORS + 3)); + color_names = + (char **) realloc(color_names, sizeof(char *) * (NUM_COLORS + 3)); /* Add "Color Select" color: */ - color_names[NUM_COLORS] = strdup(gettext("Select a color from your drawing.")); + color_names[NUM_COLORS] = + strdup(gettext("Select a color from your drawing.")); color_hexes[NUM_COLORS] = (Uint8 *) malloc(sizeof(Uint8) * 3); color_hexes[NUM_COLORS][0] = 0; color_hexes[NUM_COLORS][1] = 0; @@ -27332,19 +29195,23 @@ static void setup_colors(void) /* Add "Color Picker" color: */ /* (This is an attempt to describe an HSV color picker in extremely basic terms!) */ - color_names[NUM_COLORS] = strdup(gettext("Pick a color. Hues go top to bottom. Saturation/intensity goes left (pale) to right (pure). Value (lightness/darkness): grey bar.")); + color_names[NUM_COLORS] = + strdup(gettext + ("Pick a color. Hues go top to bottom. Saturation/intensity goes left (pale) to right (pure). Value (lightness/darkness): grey bar.")); color_hexes[NUM_COLORS] = (Uint8 *) malloc(sizeof(Uint8) * 3); color_hexes[NUM_COLORS][0] = 255; color_hexes[NUM_COLORS][1] = 255; color_hexes[NUM_COLORS][2] = 255; - color_picker_x = 0; /* Saturation */ - color_picker_y = 0; /* Hue */ - color_picker_v = 0; /* Value */ + color_picker_x = 0; /* Saturation */ + color_picker_y = 0; /* Hue */ + color_picker_v = 0; /* Value */ NUM_COLORS++; /* Add "Color Mixer" color: */ /* (The terms 'tint', 'tone', and 'shade' relate to combining white, grey, or black paint (respectively) to another color) */ - color_names[NUM_COLORS] = strdup(gettext("Click the primary colors (red, yellow, and blue), white (to tint), grey (to tone), and black (to shade), to mix together a new color.")); + color_names[NUM_COLORS] = + strdup(gettext + ("Click the primary colors (red, yellow, and blue), white (to tint), grey (to tone), and black (to shade), to mix together a new color.")); color_hexes[NUM_COLORS] = (Uint8 *) malloc(sizeof(Uint8) * 3); color_hexes[NUM_COLORS][0] = 255; color_hexes[NUM_COLORS][1] = 255; @@ -27384,32 +29251,33 @@ static void do_lock_file(void) fi = fopen(lock_fname, "r"); if (fi != NULL) + { + /* If it exists, read its contents: */ + + if (fread(&time_lock, sizeof(time_t), 1, fi) > 0) { - /* If it exists, read its contents: */ + /* Has it not been 30 seconds yet? */ - if (fread(&time_lock, sizeof(time_t), 1, fi) > 0) - { - /* Has it not been 30 seconds yet? */ + if (time_now < time_lock + 30) + { + /* FIXME: Wrap in gettext() */ + printf + ("You have already started tuxpaint less than 30 seconds ago.\n" + "To prevent multiple executions by mistake, TuxPaint will not run\n" + "before 30 seconds have elapsed since it was last started.\n" + "\n" + "You can also use the --nolockfile argument, see tuxpaint(1).\n\n"); - if (time_now < time_lock + 30) - { - /* FIXME: Wrap in gettext() */ - printf - ("You have already started tuxpaint less than 30 seconds ago.\n" - "To prevent multiple executions by mistake, TuxPaint will not run\n" - "before 30 seconds have elapsed since it was last started.\n" - "\n" "You can also use the --nolockfile argument, see tuxpaint(1).\n\n"); + free(lock_fname); - free(lock_fname); - - fclose(fi); - exit(0); - } - } - - fclose(fi); + fclose(fi); + exit(0); + } } + fclose(fi); + } + /* Okay to run; create/update the lockfile */ @@ -27421,23 +29289,25 @@ static void do_lock_file(void) fi = fopen(lock_fname, "w"); if (fi != NULL) - { - /* If we can write to it, do so! */ + { + /* If we can write to it, do so! */ - fwrite(&time_now, sizeof(time_t), 1, fi); - fclose(fi); - } + fwrite(&time_now, sizeof(time_t), 1, fi); + fclose(fi); + } else - { - fprintf(stderr, - "\nWarning: I couldn't create the lockfile (%s)\n" - "The error that occurred was:\n" "%s\n\n", lock_fname, strerror(errno)); - } + { + fprintf(stderr, + "\nWarning: I couldn't create the lockfile (%s)\n" + "The error that occurred was:\n" "%s\n\n", lock_fname, + strerror(errno)); + } free(lock_fname); } -int TP_EventFilter(__attribute__((unused)) void *data, EVENT_FILTER_EVENT_TYPE * event) +int TP_EventFilter( __attribute__((unused)) + void *data, EVENT_FILTER_EVENT_TYPE * event) /** * FIXME */ @@ -27460,7 +29330,8 @@ int TP_EventFilter(__attribute__((unused)) void *data, EVENT_FILTER_EVENT_TYPE * event->type == SDL_TEXTINPUT || event->type == SDL_APP_WILLENTERBACKGROUND || event->type == SDL_APP_WILLENTERFOREGROUND || - event->type == SDL_APP_DIDENTERBACKGROUND || event->type == SDL_APP_DIDENTERFOREGROUND) + event->type == SDL_APP_DIDENTERBACKGROUND + || event->type == SDL_APP_DIDENTERFOREGROUND) return 1; return 0; @@ -27508,10 +29379,10 @@ static void setup(void) #ifdef _WIN32 if (fullscreen) - { - InstallKeyboardHook(); - SetActivationState(1); - } + { + InstallKeyboardHook(); + SetActivationState(1); + } #endif im_init(&im_data, get_current_language()); @@ -27521,17 +29392,20 @@ static void setup(void) #endif #ifndef WIN32 - putenv((char *)"SDL_VIDEO_X11_WMCLASS=TuxPaint.TuxPaint"); + putenv((char *) "SDL_VIDEO_X11_WMCLASS=TuxPaint.TuxPaint"); #endif if (disable_screensaver == 0) + { + putenv((char *) "SDL_VIDEO_ALLOW_SCREENSAVER=1"); + if (SDL_MAJOR_VERSION < 2 + || (SDL_MAJOR_VERSION == 2 && SDL_MINOR_VERSION == 0 + && SDL_PATCHLEVEL < 2)) { - putenv((char *)"SDL_VIDEO_ALLOW_SCREENSAVER=1"); - if (SDL_MAJOR_VERSION < 2 || (SDL_MAJOR_VERSION == 2 && SDL_MINOR_VERSION == 0 && SDL_PATCHLEVEL < 2)) - { - fprintf(stderr, "Note: 'allowscreensaver' requires SDL 1.2.12 or higher\n"); - } + fprintf(stderr, + "Note: 'allowscreensaver' requires SDL 1.2.12 or higher\n"); } + } if (joystick_dev != -1) do_lock_file(); @@ -27544,29 +29418,31 @@ static void setup(void) /* Init SDL */ if (SDL_Init(init_flags) < 0) - { + { #ifndef NOSOUND - char *olderr = strdup(SDL_GetError()); + char *olderr = strdup(SDL_GetError()); - use_sound = 0; - init_flags &= ~SDL_INIT_AUDIO; - if (SDL_Init(init_flags) >= 0) - { - /* worked, w/o sound */ - fprintf(stderr, - "\nWarning: I could not initialize audio!\n" - "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", olderr); - free(olderr); - } - else -#endif - { - fprintf(stderr, - "\nError: I could not initialize video and/or the timer!\n" - "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", SDL_GetError()); - exit(1); - } + use_sound = 0; + init_flags &= ~SDL_INIT_AUDIO; + if (SDL_Init(init_flags) >= 0) + { + /* worked, w/o sound */ + fprintf(stderr, + "\nWarning: I could not initialize audio!\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", olderr); + free(olderr); } + else +#endif + { + fprintf(stderr, + "\nError: I could not initialize video and/or the timer!\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", SDL_GetError()); + exit(1); + } + } /* Set up event filter */ @@ -27576,35 +29452,36 @@ static void setup(void) /* Set up joystick */ if (joystick_dev == -1) + { + fprintf(stderr, "%i joystick(s) were found:\n", SDL_NumJoysticks()); + + for (i = 0; i < SDL_NumJoysticks(); i++) { - fprintf(stderr, "%i joystick(s) were found:\n", SDL_NumJoysticks()); + SDL_Joystick *joystick = SDL_JoystickOpen(i); - for (i = 0; i < SDL_NumJoysticks(); i++) - { - SDL_Joystick *joystick = SDL_JoystickOpen(i); - - fprintf(stderr, " %d: %s\n", i, SDL_JoystickName(joystick)); - } - - SDL_Quit(); - exit(0); + fprintf(stderr, " %d: %s\n", i, SDL_JoystickName(joystick)); } + SDL_Quit(); + exit(0); + } + joystick = SDL_JoystickOpen(joystick_dev); if (joystick == NULL) - { - fprintf(stderr, "Could not open joystick device %d: %s\n", joystick_dev, SDL_GetError()); - } + { + fprintf(stderr, "Could not open joystick device %d: %s\n", joystick_dev, + SDL_GetError()); + } else - { - SDL_JoystickEventState(SDL_ENABLE); + { + SDL_JoystickEventState(SDL_ENABLE); #ifdef DEBUG - printf("Number of Axes: %d\n", SDL_JoystickNumAxes(joystick)); - printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(joystick)); - printf("Number of Balls: %d\n", SDL_JoystickNumBalls(joystick)); - printf("Number of Hats: %d\n", SDL_JoystickNumHats(joystick)); + printf("Number of Axes: %d\n", SDL_JoystickNumAxes(joystick)); + printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(joystick)); + printf("Number of Balls: %d\n", SDL_JoystickNumBalls(joystick)); + printf("Number of Hats: %d\n", SDL_JoystickNumHats(joystick)); #endif - } + } #ifndef NOSOUND @@ -27617,26 +29494,29 @@ static void setup(void) #else if (use_sound && Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 2048) < 0) #endif - { - fprintf(stderr, - "\nWarning: I could not set up audio for 44100 Hz " - "16-bit stereo.\n" "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", SDL_GetError()); - use_sound = 0; - } + { + fprintf(stderr, + "\nWarning: I could not set up audio for 44100 Hz " + "16-bit stereo.\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", SDL_GetError()); + use_sound = 0; + } i = NUM_SOUNDS; while (use_sound && i--) - { - sounds[i] = Mix_LoadWAV(sound_fnames[i]); + { + sounds[i] = Mix_LoadWAV(sound_fnames[i]); - if (sounds[i] == NULL) - { - fprintf(stderr, - "\nWarning: I couldn't open a sound file:\n%s\n" - "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", sound_fnames[i], SDL_GetError()); - use_sound = 0; - } + if (sounds[i] == NULL) + { + fprintf(stderr, + "\nWarning: I couldn't open a sound file:\n%s\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", sound_fnames[i], SDL_GetError()); + use_sound = 0; } + } #endif @@ -27653,14 +29533,15 @@ static void setup(void) #endif /* Init TTF stuff: */ if (TTF_Init() < 0) - { - fprintf(stderr, - "\nError: I could not initialize the font (TTF) library!\n" - "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", SDL_GetError()); + { + fprintf(stderr, + "\nError: I could not initialize the font (TTF) library!\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", SDL_GetError()); - SDL_Quit(); - exit(1); - } + SDL_Quit(); + exit(1); + } #ifdef DEBUG @@ -27674,225 +29555,243 @@ static void setup(void) #if defined __ANDROID__ SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeRight"); #endif - - if (rotate_orientation) - { - if (native_screensize && fullscreen) - { -#if defined __ANDROID__ - SDL_SetHint(SDL_HINT_ORIENTATIONS, "Portrait"); -#else - fprintf(stderr, "Warning: Asking for native screen size overrides request to rotate orientation.\n"); -#endif - } - else - { - int tmp; - tmp = WINDOW_WIDTH; - WINDOW_WIDTH = WINDOW_HEIGHT; - WINDOW_HEIGHT = tmp; - } + if (rotate_orientation) + { + if (native_screensize && fullscreen) + { +#if defined __ANDROID__ + SDL_SetHint(SDL_HINT_ORIENTATIONS, "Portrait"); +#else + fprintf(stderr, + "Warning: Asking for native screen size overrides request to rotate orientation.\n"); +#endif } + else + { + int tmp; + + tmp = WINDOW_WIDTH; + WINDOW_WIDTH = WINDOW_HEIGHT; + WINDOW_HEIGHT = tmp; + } + } /* Deal with 'native' screen size option */ if (native_screensize) + { + if (!fullscreen) { - if (!fullscreen) - { - fprintf(stderr, "Warning: Asking for native screensize in a window. Ignoring.\n"); - } - else - { - WINDOW_WIDTH = 0; - WINDOW_HEIGHT = 0; - } + fprintf(stderr, + "Warning: Asking for native screensize in a window. Ignoring.\n"); } + else + { + WINDOW_WIDTH = 0; + WINDOW_HEIGHT = 0; + } + } /* Open Window: */ if (fullscreen) - { + { #ifdef USE_HWSURFACE - /* screen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, - VIDEO_BPP, SDL_FULLSCREEN | SDL_HWSURFACE); */ - window_screen = SDL_CreateWindow("Tux Paint", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, - //0,0, - WINDOW_WIDTH, WINDOW_HEIGHT, - SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_HWSURFACE); - printf("1\n"); - if (window_screen == NULL) - printf("window_screen = NULL 1\n"); + /* screen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, + VIDEO_BPP, SDL_FULLSCREEN | SDL_HWSURFACE); */ + window_screen = + SDL_CreateWindow("Tux Paint", SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + //0,0, + WINDOW_WIDTH, WINDOW_HEIGHT, + SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_HWSURFACE); + printf("1\n"); + if (window_screen == NULL) + printf("window_screen = NULL 1\n"); #else /* screen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, VIDEO_BPP, SDL_FULLSCREEN | SDL_SWSURFACE);*/ - window_screen = SDL_CreateWindow(NULL, - //"Tux Paint", - SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, - //0, 0, - WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_FULLSCREEN_DESKTOP); - printf("2\n"); - if (window_screen == NULL) - printf("window_screen = NULL 2\n"); + window_screen = SDL_CreateWindow(NULL, + //"Tux Paint", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + //0, 0, + WINDOW_WIDTH, WINDOW_HEIGHT, + SDL_WINDOW_FULLSCREEN_DESKTOP); + printf("2\n"); + if (window_screen == NULL) + printf("window_screen = NULL 2\n"); #endif - renderer = SDL_CreateRenderer(window_screen, -1, 0); - SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); + renderer = SDL_CreateRenderer(window_screen, -1, 0); + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); + + if (native_screensize) + { + SDL_GL_GetDrawableSize(window_screen, &ww, &hh); + + /* Tuxpaint goes wrong under 500x480. + Scale it using SDL2 features */ + if (ww < 500 || hh < 480) + { + float window_scale_w = 1.; + float window_scale_h = 1.; + + window_scale_w = 501.f / ww; + window_scale_h = 481.f / hh; + + if (window_scale_w > window_scale_h) + { + /* Keep things squared */ + ww = window_scale_w * ww; + hh = window_scale_w * hh; + render_scale = window_scale_w; + + SDL_RenderSetScale(renderer, window_scale_w, window_scale_w); + } + else + { + ww = window_scale_h * ww; + hh = window_scale_h * hh; + render_scale = window_scale_h; + + SDL_RenderSetScale(renderer, window_scale_h, window_scale_h); + } + } +#if defined __ANDROID__ + /* Deal with rotate orientation in native screens */ + if (rotate_orientation) + { + if (ww > hh) + { + int tmp; + tmp = ww; + ww = hh; + hh = tmp; + } + } + else + { + if (hh > ww) + { + int tmp; + tmp = ww; + ww = hh; + hh = tmp; + } + } +#endif + + } + else + { + ww = WINDOW_WIDTH; + hh = WINDOW_HEIGHT; + } + + texture = + SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888, + SDL_TEXTUREACCESS_STATIC, ww, hh); + + + screen = + SDL_CreateRGBSurface(0, ww, hh, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, + 0xFF000000); + if (screen == NULL) + { + fprintf(stderr, + "\nWarning: I could not open the display in fullscreen mode.\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", SDL_GetError()); + + fullscreen = 0; + } + else + { + /* Get resolution if we asked for native: */ if (native_screensize) - { - SDL_GL_GetDrawableSize(window_screen, &ww, &hh); - - /* Tuxpaint goes wrong under 500x480. - Scale it using SDL2 features */ - if (ww < 500 || hh < 480) - { - float window_scale_w = 1.; - float window_scale_h = 1.; - - window_scale_w = 501.f / ww; - window_scale_h = 481.f / hh; - - if (window_scale_w > window_scale_h) - { - /* Keep things squared */ - ww = window_scale_w * ww; - hh = window_scale_w * hh; - render_scale = window_scale_w; - - SDL_RenderSetScale(renderer, window_scale_w, window_scale_w); - } - else - { - ww = window_scale_h * ww; - hh = window_scale_h * hh; - render_scale = window_scale_h; - - SDL_RenderSetScale(renderer, window_scale_h, window_scale_h); - } - } -#if defined __ANDROID__ - /* Deal with rotate orientation in native screens */ - if (rotate_orientation) - { - if (ww > hh) - { - int tmp; - tmp = ww; - ww = hh; - hh = tmp; - } - } - else - { - if (hh > ww) - { - int tmp; - tmp = ww; - ww = hh; - hh = tmp; - } - } -#endif - - } - else - { - ww = WINDOW_WIDTH; - hh = WINDOW_HEIGHT; - } - - texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STATIC, ww, hh); - - - screen = SDL_CreateRGBSurface(0, ww, hh, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); - if (screen == NULL) - { - fprintf(stderr, - "\nWarning: I could not open the display in fullscreen mode.\n" - "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", SDL_GetError()); - - fullscreen = 0; - } - else - { - /* Get resolution if we asked for native: */ - - if (native_screensize) - { - WINDOW_WIDTH = screen->w; - WINDOW_HEIGHT = screen->h; - } - } + { + WINDOW_WIDTH = screen->w; + WINDOW_HEIGHT = screen->h; + } } + } if (!fullscreen) - { - int set_window_pos = 0; + { + int set_window_pos = 0; - if (getenv((char *)"SDL_VIDEO_WINDOW_POS") == NULL) - { - set_window_pos = 1; - putenv((char *)"SDL_VIDEO_WINDOW_POS=center"); - } + if (getenv((char *) "SDL_VIDEO_WINDOW_POS") == NULL) + { + set_window_pos = 1; + putenv((char *) "SDL_VIDEO_WINDOW_POS=center"); + } #ifdef USE_HWSURFACE - /* screen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, - VIDEO_BPP, SDL_HWSURFACE); */ - window_screen = SDL_CreateWindow("Tux Paint", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_HWSURFACE); - printf("3\n"); - if (window_screen == NULL) - printf("window_screen = NULL 3\n"); + /* screen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, + VIDEO_BPP, SDL_HWSURFACE); */ + window_screen = SDL_CreateWindow("Tux Paint", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, + WINDOW_HEIGHT, SDL_WINDOW_HWSURFACE); + printf("3\n"); + if (window_screen == NULL) + printf("window_screen = NULL 3\n"); #else - window_screen = SDL_CreateWindow("Tux Paint", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, 0 /* no flags */); - /* screen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, - s - VIDEO_BPP, SDL_SWSURFACE); */ - printf("4\n"); - if (window_screen == NULL) - printf("window_screen = NULL 4\n"); + window_screen = SDL_CreateWindow("Tux Paint", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, + WINDOW_HEIGHT, 0 /* no flags */ ); + /* screen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, + s + VIDEO_BPP, SDL_SWSURFACE); */ + printf("4\n"); + if (window_screen == NULL) + printf("window_screen = NULL 4\n"); #endif - if (set_window_pos) - putenv((char *)"SDL_VIDEO_WINDOW_POS=nopref"); + if (set_window_pos) + putenv((char *) "SDL_VIDEO_WINDOW_POS=nopref"); - /* Note: Seems that this depends on the compliance by the window manager - currently this doesn't works under Fvwm */ - SDL_SetWindowMinimumSize(window_screen, WINDOW_WIDTH, WINDOW_HEIGHT); - SDL_SetWindowMaximumSize(window_screen, WINDOW_WIDTH, WINDOW_HEIGHT); + /* Note: Seems that this depends on the compliance by the window manager + currently this doesn't works under Fvwm */ + SDL_SetWindowMinimumSize(window_screen, WINDOW_WIDTH, WINDOW_HEIGHT); + SDL_SetWindowMaximumSize(window_screen, WINDOW_WIDTH, WINDOW_HEIGHT); - renderer = SDL_CreateRenderer(window_screen, -1, 0); - SDL_GL_GetDrawableSize(window_screen, &ww, &hh); - texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STATIC, ww, hh); + renderer = SDL_CreateRenderer(window_screen, -1, 0); + SDL_GL_GetDrawableSize(window_screen, &ww, &hh); + texture = + SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888, + SDL_TEXTUREACCESS_STATIC, ww, hh); - screen = SDL_CreateRGBSurface(0, ww, hh, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); + screen = + SDL_CreateRGBSurface(0, ww, hh, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, + 0xFF000000); - //screen = SDL_GetWindowSurface(window_screen); + //screen = SDL_GetWindowSurface(window_screen); - if (screen == NULL) - { - fprintf(stderr, - "\nError: 1 I could not open the display.\n" - "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", SDL_GetError()); + if (screen == NULL) + { + fprintf(stderr, + "\nError: 1 I could not open the display.\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", SDL_GetError()); - cleanup(); - exit(1); - } + cleanup(); + exit(1); } + } SDL_RenderSetLogicalSize(renderer, WINDOW_WIDTH, WINDOW_HEIGHT); @@ -27900,8 +29799,10 @@ static void setup(void) /* (Need to do this after native screen resolution is handled) */ - if (button_size_auto) /* Automatic size of buttons, see https://sourceforge.net/p/tuxpaint/feature-requests/218/ */ - button_scale = (float) min((48 * screen->w) / 800, (48 * screen->h) / 600) / ORIGINAL_BUTTON_SIZE; + if (button_size_auto) /* Automatic size of buttons, see https://sourceforge.net/p/tuxpaint/feature-requests/218/ */ + button_scale = + (float) min((48 * screen->w) / 800, + (48 * screen->h) / 600) / ORIGINAL_BUTTON_SIZE; setup_screen_layout(); set_color_picker_crosshair_size(); @@ -27928,13 +29829,16 @@ static void setup(void) if (big_title) - img_title_tuxpaint = loadimage(DATA_PREFIX "images/title-tuxpaint-2x.png"); + img_title_tuxpaint = + loadimage(DATA_PREFIX "images/title-tuxpaint-2x.png"); else img_title_tuxpaint = loadimage(DATA_PREFIX "images/title-tuxpaint.png"); SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255)); - dest.x = ((WINDOW_WIDTH - img_title->w - (img_title_tuxpaint->w / 2)) / 2) + (img_title_tuxpaint->w / 2) + 20; + dest.x = + ((WINDOW_WIDTH - img_title->w - (img_title_tuxpaint->w / 2)) / 2) + + (img_title_tuxpaint->w / 2) + 20; dest.y = (WINDOW_HEIGHT - img_title->h); SDL_BlitSurface(img_title, NULL, screen, &dest); @@ -27960,12 +29864,13 @@ static void setup(void) #if defined(WIN32) && defined(LARGE_CURSOR_FULLSCREEN_BUG) if (fullscreen && no_fancy_cursors == 0) - { - fprintf(stderr, "Warning: An SDL bug causes the fancy cursors to leave\n" - "trails in fullscreen mode. Disabling fancy cursors.\n" - "(You can do this yourself with 'nofancycursors' option,\n" "to avoid this warning in the future.)\n"); - no_fancy_cursors = 1; - } + { + fprintf(stderr, "Warning: An SDL bug causes the fancy cursors to leave\n" + "trails in fullscreen mode. Disabling fancy cursors.\n" + "(You can do this yourself with 'nofancycursors' option,\n" + "to avoid this warning in the future.)\n"); + no_fancy_cursors = 1; + } #endif @@ -27982,7 +29887,9 @@ static void setup(void) #endif /* this one first, because we need it yesterday */ - cursor_watch = get_cursor(watch_bits, watch_mask_bits, watch_width, watch_height, 14 / scale, 14 / scale); + cursor_watch = + get_cursor(watch_bits, watch_mask_bits, watch_width, watch_height, + 14 / scale, 14 / scale); do_setcursor(cursor_watch); show_progress_bar(screen); @@ -28000,32 +29907,34 @@ static void setup(void) fflush(stdout); #endif - fontconfig_thread = SDL_CreateThread(generate_fontconfig_cache, "fontconfig_thread", NULL); + fontconfig_thread = + SDL_CreateThread(generate_fontconfig_cache, "fontconfig_thread", NULL); if (fontconfig_thread == NULL) - { - fprintf(stderr, "Failed to create Pango setup thread: %s\n", SDL_GetError()); - } + { + fprintf(stderr, "Failed to create Pango setup thread: %s\n", + SDL_GetError()); + } else + { +#ifdef DEBUG + printf("Thread spawned\n"); + fflush(stdout); +#endif + if (generate_fontconfig_cache_spinner(screen)) /* returns 1 if aborted */ { -#ifdef DEBUG - printf("Thread spawned\n"); + printf("Pango thread aborted!\n"); fflush(stdout); -#endif - if (generate_fontconfig_cache_spinner(screen)) /* returns 1 if aborted */ - { - printf("Pango thread aborted!\n"); - fflush(stdout); - // FIXME SDL2 - // SDL_KillThread(fontconfig_thread); - SDL_Quit(); - exit(0); - /* FIXME: Let's be more graceful about exiting (e.g., clean up lockfile!) -bjk 2010.04.27 */ - } -#ifdef DEBUG - printf("Done generating cache\n"); - fflush(stdout); -#endif + // FIXME SDL2 + // SDL_KillThread(fontconfig_thread); + SDL_Quit(); + exit(0); + /* FIXME: Let's be more graceful about exiting (e.g., clean up lockfile!) -bjk 2010.04.27 */ } +#ifdef DEBUG + printf("Done generating cache\n"); + fflush(stdout); +#endif + } #ifdef FORKED_FONTS @@ -28034,26 +29943,31 @@ static void setup(void) printf("Now running font scanner\n"); fflush(stdout); #endif - run_font_scanner(screen, texture, renderer, lang_prefixes[get_current_language()]); + run_font_scanner(screen, texture, renderer, + lang_prefixes[get_current_language()]); #endif #endif medium_font = TuxPaint_Font_OpenFont(PANGO_DEFAULT_FONT, - DATA_PREFIX "fonts/default_font.ttf", (18 - (only_uppercase * 3)) * button_scale); + DATA_PREFIX "fonts/default_font.ttf", + (18 - + (only_uppercase * 3)) * button_scale); if (medium_font == NULL) - { - fprintf(stderr, - "\nError: Can't load font file: " - DATA_PREFIX "fonts/default_font.ttf\n" - "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", SDL_GetError()); + { + fprintf(stderr, + "\nError: Can't load font file: " + DATA_PREFIX "fonts/default_font.ttf\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", SDL_GetError()); - cleanup(); - exit(1); - } + cleanup(); + exit(1); + } - safe_snprintf(tmp_str, sizeof(tmp_str), "Version: %s – %s", VER_VERSION, VER_DATE); + safe_snprintf(tmp_str, sizeof(tmp_str), "Version: %s – %s", VER_VERSION, + VER_DATE); tmp_surf = render_text(medium_font, tmp_str, black); dest.x = 10; @@ -28065,7 +29979,8 @@ static void setup(void) printf("%s\n", tmp_str); #endif - safe_snprintf(tmp_str, sizeof(tmp_str), "© 2002–2021 Bill Kendrick et al."); + safe_snprintf(tmp_str, sizeof(tmp_str), + "© 2002–2021 Bill Kendrick et al."); tmp_surf = render_text(medium_font, tmp_str, black); dest.x = 10; dest.y = WINDOW_HEIGHT - img_progress->h - (tmp_surf->h * 2); @@ -28085,28 +30000,45 @@ static void setup(void) #ifndef __APPLE__ - cursor_arrow = get_cursor(arrow_bits, arrow_mask_bits, arrow_width, arrow_height, 0, 0); + cursor_arrow = + get_cursor(arrow_bits, arrow_mask_bits, arrow_width, arrow_height, 0, 0); #endif - cursor_hand = get_cursor(hand_bits, hand_mask_bits, hand_width, hand_height, 12 / scale, 1 / scale); + cursor_hand = + get_cursor(hand_bits, hand_mask_bits, hand_width, hand_height, 12 / scale, + 1 / scale); - cursor_pipette = get_cursor(pipette_bits, pipette_mask_bits, pipette_width, pipette_height, 2 / scale, 20 / scale); + cursor_pipette = + get_cursor(pipette_bits, pipette_mask_bits, pipette_width, pipette_height, + 2 / scale, 20 / scale); - cursor_wand = get_cursor(wand_bits, wand_mask_bits, wand_width, wand_height, 4 / scale, 4 / scale); + cursor_wand = + get_cursor(wand_bits, wand_mask_bits, wand_width, wand_height, 4 / scale, + 4 / scale); cursor_insertion = get_cursor(insertion_bits, insertion_mask_bits, - insertion_width, insertion_height, 7 / scale, 4 / scale); + insertion_width, insertion_height, 7 / scale, + 4 / scale); - cursor_brush = get_cursor(brush_bits, brush_mask_bits, brush_width, brush_height, 4 / scale, 28 / scale); + cursor_brush = + get_cursor(brush_bits, brush_mask_bits, brush_width, brush_height, + 4 / scale, 28 / scale); cursor_crosshair = get_cursor(crosshair_bits, crosshair_mask_bits, - crosshair_width, crosshair_height, 15 / scale, 15 / scale); + crosshair_width, crosshair_height, 15 / scale, + 15 / scale); - cursor_rotate = get_cursor(rotate_bits, rotate_mask_bits, rotate_width, rotate_height, 15 / scale, 15 / scale); + cursor_rotate = + get_cursor(rotate_bits, rotate_mask_bits, rotate_width, rotate_height, + 15 / scale, 15 / scale); - cursor_up = get_cursor(up_bits, up_mask_bits, up_width, up_height, 15 / scale, 1 / scale); + cursor_up = + get_cursor(up_bits, up_mask_bits, up_width, up_height, 15 / scale, + 1 / scale); - cursor_down = get_cursor(down_bits, down_mask_bits, down_width, down_height, 15 / scale, 30 / scale); + cursor_down = + get_cursor(down_bits, down_mask_bits, down_width, down_height, 15 / scale, + 30 / scale); cursor_tiny = get_cursor(tiny_bits, tiny_mask_bits, tiny_width, tiny_height, 3, 3); /* Exactly the same in SMALL (16x16) size! */ @@ -28121,14 +30053,14 @@ static void setup(void) #endif /* Per https://wiki.libsdl.org/SDL_CreateRGBSurface, - * the flags are unused and should be set to 0 - * Using zeros for the RGB masks sets a default value, based on the depth. - */ + * the flags are unused and should be set to 0 + * Using zeros for the RGB masks sets a default value, based on the depth. + */ canvas = SDL_CreateRGBSurface(0, canvas_width, canvas_height, 24, 0, 0, 0, 0); save_canvas = SDL_CreateRGBSurface(0, canvas_width, canvas_height, - 24, 0, 0, 0, 0); + 24, 0, 0, 0, 0); img_starter = NULL; @@ -28139,31 +30071,33 @@ static void setup(void) starter_modified = 0; if (canvas == NULL) - { - fprintf(stderr, "\nError: Can't build drawing canvas!\n" - "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", SDL_GetError()); + { + fprintf(stderr, "\nError: Can't build drawing canvas!\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", SDL_GetError()); - cleanup(); - exit(1); - } + cleanup(); + exit(1); + } touched = (Uint8 *) malloc(sizeof(Uint8) * (canvas->w * canvas->h)); if (touched == NULL) - { - fprintf(stderr, "\nError: Can't build drawing touch mask for Magic!\n"); + { + fprintf(stderr, "\nError: Can't build drawing touch mask for Magic!\n"); - cleanup(); - exit(1); - } + cleanup(); + exit(1); + } - sim_flood_touched = (Uint8 *) malloc(sizeof(Uint8) * (canvas->w * canvas->h)); + sim_flood_touched = + (Uint8 *) malloc(sizeof(Uint8) * (canvas->w * canvas->h)); if (sim_flood_touched == NULL) - { - fprintf(stderr, "\nError: Can't build drawing touch mask for Fill!\n"); + { + fprintf(stderr, "\nError: Can't build drawing touch mask for Fill!\n"); - cleanup(); - exit(1); - } + cleanup(); + exit(1); + } canvas_color_r = 255; canvas_color_g = 255; @@ -28177,7 +30111,8 @@ static void setup(void) WINDOW_WIDTH - (r_ttools.w * 2), (button_h * 7) + 40 + HEIGHTOFFSET, screen->format->BitsPerPixel, - screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, TPAINT_AMASK); + screen->format->Rmask, screen->format->Gmask, + screen->format->Bmask, TPAINT_AMASK); /* making the label layer transparent */ SDL_FillRect(label, NULL, SDL_MapRGBA(label->format, 0, 0, 0, 0)); @@ -28185,25 +30120,27 @@ static void setup(void) /* Create undo buffer space: */ for (i = 0; i < NUM_UNDO_BUFS; i++) + { + undo_bufs[i] = + SDL_CreateRGBSurface(screen->flags, canvas_width, canvas_height, + screen->format->BitsPerPixel, + screen->format->Rmask, screen->format->Gmask, + screen->format->Bmask, 0); + + + if (undo_bufs[i] == NULL) { - undo_bufs[i] = SDL_CreateRGBSurface(screen->flags, canvas_width, canvas_height, - screen->format->BitsPerPixel, - screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, 0); + fprintf(stderr, "\nError: Can't build undo buffer! (%d of %d)\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", i + 1, NUM_UNDO_BUFS, SDL_GetError()); - - if (undo_bufs[i] == NULL) - { - fprintf(stderr, "\nError: Can't build undo buffer! (%d of %d)\n" - "The Simple DirectMedia Layer error that occurred was:\n" - "%s\n\n", i + 1, NUM_UNDO_BUFS, SDL_GetError()); - - cleanup(); - exit(1); - } - - undo_starters[i] = UNDO_STARTER_NONE; + cleanup(); + exit(1); } + undo_starters[i] = UNDO_STARTER_NONE; + } + /* Load other images: */ @@ -28214,7 +30151,8 @@ static void setup(void) img_title_on = loadimagerb(DATA_PREFIX "images/ui/title.png"); img_title_large_on = loadimagerb(DATA_PREFIX "images/ui/title_large.png"); img_title_off = loadimagerb(DATA_PREFIX "images/ui/no_title.png"); - img_title_large_off = loadimagerb(DATA_PREFIX "images/ui/no_title_large.png"); + img_title_large_off = + loadimagerb(DATA_PREFIX "images/ui/no_title_large.png"); img_btn_up = loadimagerb(DATA_PREFIX "images/ui/btn_up.png"); img_btn_down = loadimagerb(DATA_PREFIX "images/ui/btn_down.png"); @@ -28239,7 +30177,9 @@ static void setup(void) img_btn_off->w, img_btn_off->h, img_btn_off->format->BitsPerPixel, img_btn_off->format->Rmask, - img_btn_off->format->Gmask, img_btn_off->format->Bmask, img_btn_off->format->Amask); + img_btn_off->format->Gmask, + img_btn_off->format->Bmask, + img_btn_off->format->Amask); SDL_SetSurfaceAlphaMod(img_black, SDL_ALPHA_TRANSPARENT); SDL_FillRect(img_black, NULL, SDL_MapRGBA(screen->format, 0, 0, 0, 255)); @@ -28248,10 +30188,13 @@ static void setup(void) img_btn_off->w, img_btn_off->h, img_btn_off->format->BitsPerPixel, img_btn_off->format->Rmask, - img_btn_off->format->Gmask, img_btn_off->format->Bmask, img_btn_off->format->Amask); + img_btn_off->format->Gmask, + img_btn_off->format->Bmask, + img_btn_off->format->Amask); SDL_SetSurfaceAlphaMod(img_grey, SDL_ALPHA_TRANSPARENT); - SDL_FillRect(img_grey, NULL, SDL_MapRGBA(screen->format, 0x88, 0x88, 0x88, 255)); + SDL_FillRect(img_grey, NULL, + SDL_MapRGBA(screen->format, 0x88, 0x88, 0x88, 255)); show_progress_bar(screen); @@ -28288,7 +30231,8 @@ static void setup(void) img_shrink = loadimagerb(DATA_PREFIX "images/ui/shrink.png"); img_magic_paint = loadimagerb(DATA_PREFIX "images/ui/magic_paint.png"); - img_magic_fullscreen = loadimagerb(DATA_PREFIX "images/ui/magic_fullscreen.png"); + img_magic_fullscreen = + loadimagerb(DATA_PREFIX "images/ui/magic_fullscreen.png"); img_shapes_center = loadimagerb(DATA_PREFIX "images/ui/shapes_center.png"); img_shapes_corner = loadimagerb(DATA_PREFIX "images/ui/shapes_corner.png"); @@ -28319,7 +30263,8 @@ static void setup(void) img_scroll_down = loadimagerb(DATA_PREFIX "images/ui/scroll_down.png"); img_scroll_up_off = loadimagerb(DATA_PREFIX "images/ui/scroll_up_off.png"); - img_scroll_down_off = loadimagerb(DATA_PREFIX "images/ui/scroll_down_off.png"); + img_scroll_down_off = + loadimagerb(DATA_PREFIX "images/ui/scroll_down_off.png"); img_color_sel = loadimagerb(DATA_PREFIX "images/ui/csel.png"); img_color_mix = loadimagerb(DATA_PREFIX "images/ui/cmix.png"); @@ -28328,38 +30273,38 @@ static void setup(void) #endif if (onscreen_keyboard) - { - img_oskdel = loadimagerb(DATA_PREFIX "images/ui/osk_delete.png"); - img_osktab = loadimagerb(DATA_PREFIX "images/ui/osk_tab.png"); - img_oskenter = loadimagerb(DATA_PREFIX "images/ui/osk_enter.png"); - img_oskcapslock = loadimagerb(DATA_PREFIX "images/ui/osk_capslock.png"); - img_oskshift = loadimagerb(DATA_PREFIX "images/ui/osk_shift.png"); + { + img_oskdel = loadimagerb(DATA_PREFIX "images/ui/osk_delete.png"); + img_osktab = loadimagerb(DATA_PREFIX "images/ui/osk_tab.png"); + img_oskenter = loadimagerb(DATA_PREFIX "images/ui/osk_enter.png"); + img_oskcapslock = loadimagerb(DATA_PREFIX "images/ui/osk_capslock.png"); + img_oskshift = loadimagerb(DATA_PREFIX "images/ui/osk_shift.png"); - if (onscreen_keyboard_layout) - { - // use platform system onscreen keybord or tuxpaint onscreen keybord - if (strcmp(onscreen_keyboard_layout, "SYSTEM") == 0) - kbd = NULL; - else - kbd = - osk_create(onscreen_keyboard_layout, canvas, - img_btn_up, img_btn_down, img_btn_off, - img_btn_nav, img_btn_hold, - img_oskdel, img_osktab, img_oskenter, - img_oskcapslock, img_oskshift, - onscreen_keyboard_disable_change); - } + if (onscreen_keyboard_layout) + { + // use platform system onscreen keybord or tuxpaint onscreen keybord + if (strcmp(onscreen_keyboard_layout, "SYSTEM") == 0) + kbd = NULL; else - { - kbd = - osk_create(strdup("default.layout"), canvas, - img_btn_up, img_btn_down, img_btn_off, - img_btn_nav, img_btn_hold, - img_oskdel, img_osktab, img_oskenter, - img_oskcapslock, img_oskshift, - onscreen_keyboard_disable_change); - } + kbd = + osk_create(onscreen_keyboard_layout, canvas, + img_btn_up, img_btn_down, img_btn_off, + img_btn_nav, img_btn_hold, + img_oskdel, img_osktab, img_oskenter, + img_oskcapslock, img_oskshift, + onscreen_keyboard_disable_change); } + else + { + kbd = + osk_create(strdup("default.layout"), canvas, + img_btn_up, img_btn_down, img_btn_off, + img_btn_nav, img_btn_hold, + img_oskdel, img_osktab, img_oskenter, + img_oskcapslock, img_oskshift, + onscreen_keyboard_disable_change); + } + } show_progress_bar(screen); @@ -28375,11 +30320,13 @@ static void setup(void) #endif if (num_brushes == 0) - { - fprintf(stderr, "\nError: No brushes found in " DATA_PREFIX "brushes/\n" "or %s\n\n", homedirdir); - cleanup(); - exit(1); - } + { + fprintf(stderr, + "\nError: No brushes found in " DATA_PREFIX "brushes/\n" + "or %s\n\n", homedirdir); + cleanup(); + exit(1); + } free(homedirdir); @@ -28387,38 +30334,44 @@ static void setup(void) /* Load system fonts: */ large_font = TuxPaint_Font_OpenFont(PANGO_DEFAULT_FONT, - DATA_PREFIX "fonts/default_font.ttf", (30 - (only_uppercase * 3)) * button_scale); + DATA_PREFIX "fonts/default_font.ttf", + (30 - + (only_uppercase * 3)) * button_scale); if (large_font == NULL) - { - fprintf(stderr, - "\nError: Can't load font file: " - DATA_PREFIX "fonts/default_font.ttf\n" - "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", SDL_GetError()); + { + fprintf(stderr, + "\nError: Can't load font file: " + DATA_PREFIX "fonts/default_font.ttf\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", SDL_GetError()); - cleanup(); - exit(1); - } + cleanup(); + exit(1); + } - small_font = TuxPaint_Font_OpenFont(PANGO_DEFAULT_FONT, DATA_PREFIX "fonts/default_font.ttf", + small_font = + TuxPaint_Font_OpenFont(PANGO_DEFAULT_FONT, + DATA_PREFIX "fonts/default_font.ttf", #ifdef __APPLE__ - (12 - (only_uppercase * 2)) * button_scale + (12 - (only_uppercase * 2)) * button_scale #else - (13 - (only_uppercase * 2)) * button_scale + (13 - (only_uppercase * 2)) * button_scale #endif - ); + ); if (small_font == NULL) - { - fprintf(stderr, - "\nError: Can't load font file: " - DATA_PREFIX "fonts/default_font.ttf\n" - "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", SDL_GetError()); + { + fprintf(stderr, + "\nError: Can't load font file: " + DATA_PREFIX "fonts/default_font.ttf\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", SDL_GetError()); - cleanup(); - exit(1); - } + cleanup(); + exit(1); + } #ifdef NO_SDLPANGO @@ -28446,21 +30399,25 @@ static void setup(void) /* Load shape icons: */ for (i = 0; i < NUM_SHAPES; i++) - { - SDL_Surface *aux_surf = loadimage(shape_img_fnames[i]); - img_shapes[i] = thumbnail2(aux_surf, (aux_surf->w * button_w) / ORIGINAL_BUTTON_SIZE, (aux_surf->h * button_h) / ORIGINAL_BUTTON_SIZE, 0, 1); - SDL_FreeSurface(aux_surf); - } + { + SDL_Surface *aux_surf = loadimage(shape_img_fnames[i]); + img_shapes[i] = + thumbnail2(aux_surf, (aux_surf->w * button_w) / ORIGINAL_BUTTON_SIZE, + (aux_surf->h * button_h) / ORIGINAL_BUTTON_SIZE, 0, 1); + SDL_FreeSurface(aux_surf); + } show_progress_bar(screen); /* Load fill sub-tool icons: */ for (i = 0; i < NUM_FILLS; i++) - { - SDL_Surface *aux_surf = loadimage(fill_img_fnames[i]); - img_fills[i] = thumbnail2(aux_surf, (aux_surf->w * button_w) / ORIGINAL_BUTTON_SIZE, (aux_surf->h * button_h) / ORIGINAL_BUTTON_SIZE, 0, 1); - SDL_FreeSurface(aux_surf); - } + { + SDL_Surface *aux_surf = loadimage(fill_img_fnames[i]); + img_fills[i] = + thumbnail2(aux_surf, (aux_surf->w * button_w) / ORIGINAL_BUTTON_SIZE, + (aux_surf->h * button_h) / ORIGINAL_BUTTON_SIZE, 0, 1); + SDL_FreeSurface(aux_surf); + } show_progress_bar(screen); @@ -28476,31 +30433,34 @@ static void setup(void) show_progress_bar(screen); img_color_picker = loadimagerb(DATA_PREFIX "images/ui/color_picker.png"); - img_color_picker_val = loadimagerb(DATA_PREFIX "images/ui/color_picker_val.png"); + img_color_picker_val = + loadimagerb(DATA_PREFIX "images/ui/color_picker_val.png"); /* Create toolbox and selector labels: */ for (i = 0; i < NUM_TITLES; i++) + { + if (strlen(title_names[i]) > 0) { - if (strlen(title_names[i]) > 0) - { - TuxPaint_Font *myfont = large_font; - char *td_str = textdir(gettext(title_names[i])); + TuxPaint_Font *myfont = large_font; + char *td_str = textdir(gettext(title_names[i])); - if (need_own_font && strcmp(gettext(title_names[i]), title_names[i])) - myfont = locale_font; - upstr = uppercase(td_str); - free(td_str); - tmp_surf = render_text(myfont, upstr, black); - free(upstr); - img_title_names[i] = thumbnail(tmp_surf, min((int) (84 * button_scale), tmp_surf->w), tmp_surf->h, 0); - SDL_FreeSurface(tmp_surf); - } - else - { - img_title_names[i] = NULL; - } + if (need_own_font && strcmp(gettext(title_names[i]), title_names[i])) + myfont = locale_font; + upstr = uppercase(td_str); + free(td_str); + tmp_surf = render_text(myfont, upstr, black); + free(upstr); + img_title_names[i] = + thumbnail(tmp_surf, min((int) (84 * button_scale), tmp_surf->w), + tmp_surf->h, 0); + SDL_FreeSurface(tmp_surf); } + else + { + img_title_names[i] = NULL; + } + } @@ -28513,35 +30473,40 @@ static void setup(void) img_paintwell = thumbnail(img1, color_button_w, color_button_h, 0); tmp_btn_up = thumbnail(img_btn_up, color_button_w, color_button_h, 0); tmp_btn_down = thumbnail(img_btn_down, color_button_w, color_button_h, 0); - img_color_btn_off = thumbnail(img_btn_off, color_button_w, color_button_h, 0); + img_color_btn_off = + thumbnail(img_btn_off, color_button_w, color_button_h, 0); SDL_FreeSurface(img1); - img_color_picker_thumb = thumbnail(img_color_picker, color_button_w, color_button_h, 0); + img_color_picker_thumb = + thumbnail(img_color_picker, color_button_w, color_button_h, 0); /* Create surfaces to draw them into: */ img_color_btns = malloc(sizeof(SDL_Surface *) * NUM_COLORS * 2); for (i = 0; i < NUM_COLORS * 2; i++) + { + img_color_btns[i] = SDL_CreateRGBSurface(screen->flags, + /* (WINDOW_WIDTH - r_ttoolopt.w) / NUM_COLORS, 48, */ + tmp_btn_up->w, tmp_btn_up->h, + screen->format->BitsPerPixel, + screen->format->Rmask, + screen->format->Gmask, + screen->format->Bmask, 0); + + if (img_color_btns[i] == NULL) { - img_color_btns[i] = SDL_CreateRGBSurface(screen->flags, - /* (WINDOW_WIDTH - r_ttoolopt.w) / NUM_COLORS, 48, */ - tmp_btn_up->w, tmp_btn_up->h, - screen->format->BitsPerPixel, - screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, 0); + fprintf(stderr, "\nError: Can't build color button!\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n", SDL_GetError()); - if (img_color_btns[i] == NULL) - { - fprintf(stderr, "\nError: Can't build color button!\n" - "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", SDL_GetError()); - - cleanup(); - exit(1); - } - - SDL_LockSurface(img_color_btns[i]); + cleanup(); + exit(1); } + SDL_LockSurface(img_color_btns[i]); + } + /* Generate the buttons based on the thumbnails: */ @@ -28554,80 +30519,96 @@ static void setup(void) for (y = 0; y < tmp_btn_up->h /* 48 */ ; y++) + { + for (x = 0; + x < tmp_btn_up->w /* (WINDOW_WIDTH - r_ttoolopt.w) / NUM_COLORS */ ; + x++) { - for (x = 0; x < tmp_btn_up->w /* (WINDOW_WIDTH - r_ttoolopt.w) / NUM_COLORS */ ; - x++) + double ru, gu, bu, rd, gd, bd, aa; + Uint8 a; + + SDL_GetRGB(getpixel_tmp_btn_up(tmp_btn_up, x, y), tmp_btn_up->format, + &r, &g, &b); + + ru = sRGB_to_linear_table[r]; + gu = sRGB_to_linear_table[g]; + bu = sRGB_to_linear_table[b]; + SDL_GetRGB(getpixel_tmp_btn_down(tmp_btn_down, x, y), + tmp_btn_down->format, &r, &g, &b); + rd = sRGB_to_linear_table[r]; + gd = sRGB_to_linear_table[g]; + bd = sRGB_to_linear_table[b]; + SDL_GetRGBA(getpixel_img_paintwell(img_paintwell, x, y), + img_paintwell->format, &r, &g, &b, &a); + aa = a / 255.0; + + for (i = 0; i < NUM_COLORS; i++) + { + double rh = sRGB_to_linear_table[color_hexes[i][0]]; + double gh = sRGB_to_linear_table[color_hexes[i][1]]; + double bh = sRGB_to_linear_table[color_hexes[i][2]]; + + if (i == COLOR_PICKER) { - double ru, gu, bu, rd, gd, bd, aa; - Uint8 a; - - SDL_GetRGB(getpixel_tmp_btn_up(tmp_btn_up, x, y), tmp_btn_up->format, &r, &g, &b); - - ru = sRGB_to_linear_table[r]; - gu = sRGB_to_linear_table[g]; - bu = sRGB_to_linear_table[b]; - SDL_GetRGB(getpixel_tmp_btn_down(tmp_btn_down, x, y), tmp_btn_down->format, &r, &g, &b); - rd = sRGB_to_linear_table[r]; - gd = sRGB_to_linear_table[g]; - bd = sRGB_to_linear_table[b]; - SDL_GetRGBA(getpixel_img_paintwell(img_paintwell, x, y), img_paintwell->format, &r, &g, &b, &a); - aa = a / 255.0; - - for (i = 0; i < NUM_COLORS; i++) - { - double rh = sRGB_to_linear_table[color_hexes[i][0]]; - double gh = sRGB_to_linear_table[color_hexes[i][1]]; - double bh = sRGB_to_linear_table[color_hexes[i][2]]; - - if (i == COLOR_PICKER) - { - putpixels[img_color_btns[i]->format->BytesPerPixel] - (img_color_btns[i], x, y, - getpixels[img_color_picker_thumb->format->BytesPerPixel] (img_color_picker_thumb, x, y)); - putpixels[img_color_btns[i + NUM_COLORS]->format->BytesPerPixel] - (img_color_btns[i + NUM_COLORS], x, y, - getpixels[img_color_picker_thumb->format->BytesPerPixel] (img_color_picker_thumb, x, y)); - } - - if (i < COLOR_PICKER || a == 255) - { - putpixels[img_color_btns[i]->format->BytesPerPixel] - (img_color_btns[i], x, y, - SDL_MapRGB(img_color_btns[i]->format, - linear_to_sRGB(rh * aa + ru * (1.0 - aa)), - linear_to_sRGB(gh * aa + gu * (1.0 - aa)), linear_to_sRGB(bh * aa + bu * (1.0 - aa)))); - putpixels[img_color_btns[i + NUM_COLORS]->format->BytesPerPixel] - (img_color_btns[i + NUM_COLORS], x, y, - SDL_MapRGB(img_color_btns[i + NUM_COLORS]->format, - linear_to_sRGB(rh * aa + rd * (1.0 - aa)), - linear_to_sRGB(gh * aa + gd * (1.0 - aa)), linear_to_sRGB(bh * aa + bd * (1.0 - aa)))); - } - } + putpixels[img_color_btns[i]->format->BytesPerPixel] + (img_color_btns[i], x, y, + getpixels[img_color_picker_thumb->format-> + BytesPerPixel] (img_color_picker_thumb, x, y)); + putpixels[img_color_btns[i + NUM_COLORS]->format-> + BytesPerPixel] (img_color_btns[i + NUM_COLORS], x, y, + getpixels[img_color_picker_thumb->format-> + BytesPerPixel] + (img_color_picker_thumb, x, y)); } + + if (i < COLOR_PICKER || a == 255) + { + putpixels[img_color_btns[i]->format->BytesPerPixel] + (img_color_btns[i], x, y, + SDL_MapRGB(img_color_btns[i]->format, + linear_to_sRGB(rh * aa + ru * (1.0 - aa)), + linear_to_sRGB(gh * aa + gu * (1.0 - aa)), + linear_to_sRGB(bh * aa + bu * (1.0 - aa)))); + putpixels[img_color_btns[i + NUM_COLORS]->format-> + BytesPerPixel] (img_color_btns[i + NUM_COLORS], x, y, + SDL_MapRGB(img_color_btns + [i + NUM_COLORS]->format, + linear_to_sRGB(rh * aa + + rd * (1.0 - + aa)), + linear_to_sRGB(gh * aa + + gd * (1.0 - + aa)), + linear_to_sRGB(bh * aa + + bd * (1.0 - + aa)))); + } + } } + } for (i = 0; i < NUM_COLORS * 2; i++) + { + SDL_UnlockSurface(img_color_btns[i]); + if (i == COLOR_SELECTOR || i == COLOR_SELECTOR + NUM_COLORS) { - SDL_UnlockSurface(img_color_btns[i]); - if (i == COLOR_SELECTOR || i == COLOR_SELECTOR + NUM_COLORS) - { - /* Color selector; draw pipette */ - dest.x = (img_color_btns[i]->w - img_color_sel->w) / 2; - dest.y = (img_color_btns[i]->h - img_color_sel->h) / 2; - dest.w = img_color_sel->w; - dest.h = img_color_sel->h; - SDL_BlitSurface(img_color_sel, NULL, img_color_btns[i], &dest); - } - else if (i == COLOR_MIXER || i == COLOR_MIXER + NUM_COLORS) - { - /* Color mixer; draw palette */ - dest.x = (img_color_btns[i]->w - img_color_mix->w) / 2; - dest.y = (img_color_btns[i]->h - img_color_mix->h) / 2; - dest.w = img_color_mix->w; - dest.h = img_color_mix->h; - SDL_BlitSurface(img_color_mix, NULL, img_color_btns[i], &dest); - } + /* Color selector; draw pipette */ + dest.x = (img_color_btns[i]->w - img_color_sel->w) / 2; + dest.y = (img_color_btns[i]->h - img_color_sel->h) / 2; + dest.w = img_color_sel->w; + dest.h = img_color_sel->h; + SDL_BlitSurface(img_color_sel, NULL, img_color_btns[i], &dest); } + else if (i == COLOR_MIXER || i == COLOR_MIXER + NUM_COLORS) + { + /* Color mixer; draw palette */ + dest.x = (img_color_btns[i]->w - img_color_mix->w) / 2; + dest.y = (img_color_btns[i]->h - img_color_mix->h) / 2; + dest.w = img_color_mix->w; + dest.h = img_color_mix->h; + SDL_BlitSurface(img_color_mix, NULL, img_color_btns[i], &dest); + } + } SDL_UnlockSurface(tmp_btn_up); SDL_UnlockSurface(tmp_btn_down); @@ -28687,7 +30668,9 @@ static void claim_to_be_ready(void) src.w = img_title->w; src.x = 0; src.y = img_title->h - img_progress->h; - dest.x = ((WINDOW_WIDTH - img_title->w - (img_title_tuxpaint->w / 2)) / 2) + (img_title_tuxpaint->w / 2) + 20; + dest.x = + ((WINDOW_WIDTH - img_title->w - (img_title_tuxpaint->w / 2)) / 2) + + (img_title_tuxpaint->w / 2) + 20; SDL_BlitSurface(img_title, &src, screen, &dest); SDL_FreeSurface(img_title); @@ -28742,16 +30725,16 @@ static void claim_to_be_ready(void) brush_scroll = 0; for (i = 0; i < MAX_STAMP_GROUPS; i++) - { - stamp_scroll[i] = 0; - } + { + stamp_scroll[i] = 0; + } stamp_group = 0; /* reset! */ for (i = 0; i < MAX_MAGIC_GROUPS; i++) - { - magic_scroll[i] = 0; - cur_magic[i] = 0; - } + { + magic_scroll[i] = 0; + cur_magic[i] = 0; + } font_scroll = 0; tool_scroll = 0; eraser_scroll = 0; @@ -28773,7 +30756,8 @@ static void claim_to_be_ready(void) draw_toolbar(); draw_colors(COLORSEL_FORCE_REDRAW); draw_brushes(); - update_canvas(0, 0, WINDOW_WIDTH - r_ttoolopt.w, (48 * 7) + 40 + HEIGHTOFFSET); + update_canvas(0, 0, WINDOW_WIDTH - r_ttoolopt.w, + (48 * 7) + 40 + HEIGHTOFFSET); SDL_Flip(screen); @@ -28792,7 +30776,7 @@ int main(int argc, char *argv[]) CLOCK_TYPE time2; #endif - (void)argc; + (void) argc; #ifdef DEBUG CLOCK_ASM(time1); @@ -28827,8 +30811,8 @@ int main(int argc, char *argv[]) char stdout_win32[255], stderr_win32[255]; safe_snprintf(stdout_win32, 255, "%s/stdout.txt", savedir); safe_snprintf(stderr_win32, 255, "%s/stderr.txt", savedir); - freopen(stdout_win32, "w", stdout); /* redirect stdout to a file */ - freopen(stderr_win32, "w", stderr); /* redirect stderr to a file */ + freopen(stdout_win32, "w", stdout); /* redirect stdout to a file */ + freopen(stderr_win32, "w", stderr); /* redirect stderr to a file */ #endif #endif @@ -28841,7 +30825,7 @@ int main(int argc, char *argv[]) * file may not exist on the runtime system, however, so we copy the file * into our app bundle at compile time, and tell Fontconfig here to look for * the file within the app bundle. */ - putenv((char *)"FONTCONFIG_PATH=Resources/etc"); + putenv((char *) "FONTCONFIG_PATH=Resources/etc"); #endif #ifdef FORKED_FONTS @@ -28852,7 +30836,8 @@ int main(int argc, char *argv[]) printf("Running font scanner\n"); fflush(stdout); #endif - run_font_scanner(screen, texture, renderer, lang_prefixes[get_current_language()]); + run_font_scanner(screen, texture, renderer, + lang_prefixes[get_current_language()]); #else #ifdef DEBUG printf("NOT running font scanner\n"); @@ -28863,25 +30848,28 @@ int main(int argc, char *argv[]) /* Warnings to satisfy SF.net Bug #3327493 -bjk 2011.06.24 */ if (disable_save && autosave_on_quit) - { - fprintf(stderr, "Warning: Autosave requested, but saving is disabled.\n"); - } + { + fprintf(stderr, "Warning: Autosave requested, but saving is disabled.\n"); + } if (disable_save && (promptless_save != SAVE_OVER_UNSET)) - { - fprintf(stderr, "Warning: Save-over option specified, but saving is disabled.\n"); - } + { + fprintf(stderr, + "Warning: Save-over option specified, but saving is disabled.\n"); + } if (promptless_save == SAVE_OVER_UNSET) - { - promptless_save = SAVE_OVER_PROMPT; - } + { + promptless_save = SAVE_OVER_PROMPT; + } /* Set up! */ setup(); #ifdef DEBUG - printf("Seconds in early start-up: %.3f\n", (double)(time2 - time1) / CLOCK_SPEED); - printf("Seconds in late start-up: %.3f\n", (double)(time2 - time1) / CLOCK_SPEED); + printf("Seconds in early start-up: %.3f\n", + (double) (time2 - time1) / CLOCK_SPEED); + printf("Seconds in late start-up: %.3f\n", + (double) (time2 - time1) / CLOCK_SPEED); #endif @@ -28912,7 +30900,8 @@ static int trash(char *path) #elif defined(__APPLE__) return apple_trash(path); #else - char fname[MAX_PATH], trashpath[MAX_PATH], dest[MAX_PATH], infoname[MAX_PATH], bname[MAX_PATH], ext[MAX_PATH]; + char fname[MAX_PATH], trashpath[MAX_PATH], dest[MAX_PATH], + infoname[MAX_PATH], bname[MAX_PATH], ext[MAX_PATH]; char deldate[32]; struct tm tim; time_t now; @@ -28927,10 +30916,10 @@ static int trash(char *path) /* FIXME: This is Freedesktop.org-centric -bjk 2011.04.16 */ if (basename(path) == NULL) - { - debug("Can't get basename! Deleting instead."); - return (unlink(path)); - } + { + debug("Can't get basename! Deleting instead."); + return (unlink(path)); + } #ifdef DEBUG printf("trash: basename=%s", basename(path)); /* EP */ @@ -28938,28 +30927,30 @@ static int trash(char *path) safe_strncpy(fname, basename(path), sizeof(fname)); if (!file_exists(path)) - { - debug("Does't exist anyway, so skipping"); - return (1); - } + { + debug("Does't exist anyway, so skipping"); + return (1); + } /* Move file into Trash folder */ /* FIXME: Use xdg function */ if (getenv("XDG_DATA_HOME") != NULL) - { - safe_snprintf(trashpath, sizeof(trashpath), "%s/Trash", getenv("XDG_DATA_HOME")); - } + { + safe_snprintf(trashpath, sizeof(trashpath), "%s/Trash", + getenv("XDG_DATA_HOME")); + } else if (getenv("HOME") != NULL) - { - safe_snprintf(trashpath, sizeof(trashpath), "%s/.local/share/Trash", getenv("HOME")); - } + { + safe_snprintf(trashpath, sizeof(trashpath), "%s/.local/share/Trash", + getenv("HOME")); + } else - { - debug("Can't move to trash! Deleting instead."); - return (unlink(path)); - } + { + debug("Can't move to trash! Deleting instead."); + return (unlink(path)); + } mkdir(trashpath, 0x777); safe_snprintf(dest, sizeof(dest), "%s/files", trashpath); @@ -28971,74 +30962,76 @@ static int trash(char *path) safe_strncpy(bname, fname, sizeof(bname)); if (strstr(bname, ".") != NULL) - { - strcpy(strstr(bname, "."), "\0"); /* FIXME: Use strncpy() (ugh, complicated) */ - safe_strncpy(ext, strstr(fname, ".") + 1, sizeof(ext)); - } + { + strcpy(strstr(bname, "."), "\0"); /* FIXME: Use strncpy() (ugh, complicated) */ + safe_strncpy(ext, strstr(fname, ".") + 1, sizeof(ext)); + } else - { - debug("Filename format unfamiliar! Deleting instead."); - return (unlink(path)); - } + { + debug("Filename format unfamiliar! Deleting instead."); + return (unlink(path)); + } - safe_snprintf(infoname, sizeof(infoname), "%s/info/%s.trashinfo", trashpath, fname); + safe_snprintf(infoname, sizeof(infoname), "%s/info/%s.trashinfo", trashpath, + fname); cnt = 1; while (file_exists(dest) && cnt < 100) - { - safe_snprintf(fname, sizeof(fname), "%s_%d.%s", bname, cnt, ext); + { + safe_snprintf(fname, sizeof(fname), "%s_%d.%s", bname, cnt, ext); - safe_snprintf(dest, sizeof(dest), "%s/files/%s", trashpath, fname); - safe_snprintf(infoname, sizeof(infoname), "%s/info/%s.trashinfo", trashpath, fname); - cnt++; - } + safe_snprintf(dest, sizeof(dest), "%s/files/%s", trashpath, fname); + safe_snprintf(infoname, sizeof(infoname), "%s/info/%s.trashinfo", + trashpath, fname); + cnt++; + } if (cnt >= 100) - { - debug("Too many identically-named files! Deleting instead."); - return (unlink(path)); - } + { + debug("Too many identically-named files! Deleting instead."); + return (unlink(path)); + } debug(dest); if (rename(path, dest) == -1) + { + debug("Could not move to trash. Trying to copy, instead."); + + fi = fopen(path, "r"); + if (fi == NULL) { - debug("Could not move to trash. Trying to copy, instead."); - - fi = fopen(path, "r"); - if (fi == NULL) - { - debug("Could not open source file for copy. Deleting instead."); - return (unlink(path)); - } - fo = fopen(dest, "w"); - if (fo == NULL) - { - debug("Could not open dest. file for copy. Deleting instead."); - fclose(fi); - return (unlink(path)); - } - while (!feof(fi)) - { - len = fread(buf, sizeof(unsigned char), sizeof(buf), fi); - if (len > 0) - { - fwrite(buf, sizeof(unsigned char), sizeof(buf), fo); - } - } - fclose(fi); - fclose(fo); - - unlink(path); + debug("Could not open source file for copy. Deleting instead."); + return (unlink(path)); } + fo = fopen(dest, "w"); + if (fo == NULL) + { + debug("Could not open dest. file for copy. Deleting instead."); + fclose(fi); + return (unlink(path)); + } + while (!feof(fi)) + { + len = fread(buf, sizeof(unsigned char), sizeof(buf), fi); + if (len > 0) + { + fwrite(buf, sizeof(unsigned char), sizeof(buf), fo); + } + } + fclose(fi); + fclose(fo); + + unlink(path); + } /* Create info file */ fo = fopen(infoname, "w"); if (fo == NULL) - { - debug("Error: Couldn't create info file!"); - return (1); - } + { + debug("Error: Couldn't create info file!"); + return (1); + } now = time(NULL); tim = *(localtime(&now)); @@ -29060,7 +31053,7 @@ static int trash(char *path) /* FIXME: Is this sufficient to find 'dbus-send' (rely on system to use $PATH?) -bjk 2011.04.18 */ tmp = system("dbus-send / org.kde.KDirNotify.FilesAdded string:trash:/"); - (void)tmp; + (void) tmp; /* Note: GNOME figures out when things change because it asks the Kernel @@ -29093,7 +31086,8 @@ int file_exists(char *path) */ /* Don't move the mouse here as this is only called when an event triggers it and the joystick can be holded withouth sending any event. */ -static void handle_joyaxismotion(SDL_Event event, int *motioner, int *val_x, int *val_y) +static void handle_joyaxismotion(SDL_Event event, int *motioner, int *val_x, + int *val_y) { int i, j, step; @@ -29104,84 +31098,89 @@ static void handle_joyaxismotion(SDL_Event event, int *motioner, int *val_x, int j = SDL_JoystickGetAxis(joystick, 1); step = 5000; if (abs(i) < joystick_low_threshold && abs(j) < joystick_low_threshold) - { - *motioner = FALSE; - *val_x = 0; - *val_y = 0; - } + { + *motioner = FALSE; + *val_x = 0; + *val_y = 0; + } else + { + if (i > joystick_low_threshold) + *val_x = + min((i - joystick_low_threshold) / step + 1, joystick_maxsteps); + else if (i < -joystick_low_threshold) + *val_x = + max((i + joystick_low_threshold) / step - 1, -joystick_maxsteps); + else + *val_x = 0; + + if (j > joystick_low_threshold) + *val_y = + min((j - joystick_low_threshold) / step + 1, joystick_maxsteps); + else if (j < -joystick_low_threshold) + *val_y = + max((j + joystick_low_threshold) / step - 1, -joystick_maxsteps); + else + *val_y = 0; + + if (*val_x || *val_y) { - if (i > joystick_low_threshold) - *val_x = min((i - joystick_low_threshold) / step + 1, joystick_maxsteps); - else if (i < -joystick_low_threshold) - *val_x = max((i + joystick_low_threshold) / step - 1, -joystick_maxsteps); - else - *val_x = 0; - - if (j > joystick_low_threshold) - *val_y = min((j - joystick_low_threshold) / step + 1, joystick_maxsteps); - else if (j < -joystick_low_threshold) - *val_y = max((j + joystick_low_threshold) / step - 1, -joystick_maxsteps); - else - *val_y = 0; - - if (*val_x || *val_y) - { - *motioner = TRUE; - } - else - *motioner = FALSE; + *motioner = TRUE; } + else + *motioner = FALSE; + } } /** * FIXME */ -static void handle_joyhatmotion(SDL_Event event, int oldpos_x, int oldpos_y, int *valhat_x, int *valhat_y, +static void handle_joyhatmotion(SDL_Event event, int oldpos_x, int oldpos_y, + int *valhat_x, int *valhat_y, int *hatmotioner, Uint32 * old_hat_ticks) { *hatmotioner = 1; switch (event.jhat.value) - { - case SDL_HAT_CENTERED: - *valhat_x = 0; - *valhat_y = 0; - *hatmotioner = 0; - break; - case SDL_HAT_UP: - *valhat_x = 0; - *valhat_y = -1; - break; - case SDL_HAT_RIGHTUP: - *valhat_x = 1; - *valhat_y = -1; - break; - case SDL_HAT_RIGHT: - *valhat_x = 1; - *valhat_y = 0; - break; - case SDL_HAT_RIGHTDOWN: - *valhat_x = 1; - *valhat_y = 1; - break; - case SDL_HAT_DOWN: - *valhat_x = 0; - *valhat_y = 1; - break; - case SDL_HAT_LEFTDOWN: - *valhat_x = -1; - *valhat_y = 1; - break; - case SDL_HAT_LEFT: - *valhat_x = -1; - *valhat_y = 0; - break; - case SDL_HAT_LEFTUP: - *valhat_x = -1; - *valhat_y = -1; - break; - } + { + case SDL_HAT_CENTERED: + *valhat_x = 0; + *valhat_y = 0; + *hatmotioner = 0; + break; + case SDL_HAT_UP: + *valhat_x = 0; + *valhat_y = -1; + break; + case SDL_HAT_RIGHTUP: + *valhat_x = 1; + *valhat_y = -1; + break; + case SDL_HAT_RIGHT: + *valhat_x = 1; + *valhat_y = 0; + break; + case SDL_HAT_RIGHTDOWN: + *valhat_x = 1; + *valhat_y = 1; + break; + case SDL_HAT_DOWN: + *valhat_x = 0; + *valhat_y = 1; + break; + case SDL_HAT_LEFTDOWN: + *valhat_x = -1; + *valhat_y = 1; + break; + case SDL_HAT_LEFT: + *valhat_x = -1; + *valhat_y = 0; + break; + case SDL_HAT_LEFTUP: + *valhat_x = -1; + *valhat_y = -1; + break; + } if (*valhat_x || *valhat_y) SDL_WarpMouse(oldpos_x + *valhat_x, oldpos_y + *valhat_y); @@ -29206,7 +31205,8 @@ static void handle_joyballmotion(SDL_Event event, int oldpos_x, int oldpos_y) /** * FIXME */ -static void handle_motioners(int oldpos_x, int oldpos_y, int motioner, int hatmotioner, int old_hat_ticks, int val_x, +static void handle_motioners(int oldpos_x, int oldpos_y, int motioner, + int hatmotioner, int old_hat_ticks, int val_x, int val_y, int valhat_x, int valhat_y) { int vx, vy; @@ -29220,10 +31220,10 @@ static void handle_motioners(int oldpos_x, int oldpos_y, int motioner, int hatmo if (ticks - old_hat_ticks > joystick_hat_timeout) - { - vx += valhat_x; - vy += valhat_y; - } + { + vx += valhat_x; + vy += valhat_y; + } SDL_WarpMouse(vx, vy); if (motioner && joystick_slowness) @@ -29237,7 +31237,8 @@ static void handle_motioners(int oldpos_x, int oldpos_y, int motioner, int hatmo /** * FIXME */ -static void handle_joybuttonupdown(SDL_Event event, int oldpos_x, int oldpos_y) +static void handle_joybuttonupdown(SDL_Event event, int oldpos_x, + int oldpos_y) { handle_joybuttonupdownscl(event, oldpos_x, oldpos_y, r_tools); } @@ -29245,7 +31246,8 @@ static void handle_joybuttonupdown(SDL_Event event, int oldpos_x, int oldpos_y) /** * FIXME */ -static void handle_joybuttonupdownscl(SDL_Event event, int oldpos_x, int oldpos_y, SDL_Rect real_r_tools) +static void handle_joybuttonupdownscl(SDL_Event event, int oldpos_x, + int oldpos_y, SDL_Rect real_r_tools) { int i, ignore = 0; int eby, ts; @@ -29258,152 +31260,160 @@ static void handle_joybuttonupdownscl(SDL_Event event, int oldpos_x, int oldpos_ ev.button.state = SDL_PRESSED; if (event.type == SDL_JOYBUTTONDOWN) + { + /* First the actions that can be reached via keyboard shortcurts. */ + /* Escape is usefull to dismiss dialogs */ + if (event.button.button == joystick_button_escape) { - /* First the actions that can be reached via keyboard shortcurts. */ - /* Escape is usefull to dismiss dialogs */ - if (event.button.button == joystick_button_escape) - { - ev.type = SDL_KEYDOWN; - ev.key.keysym.sym = SDLK_ESCAPE; - ev.key.keysym.mod = KMOD_CTRL; - } - else if (event.button.button == joystick_button_pagesetup) - { - ev.type = SDL_KEYDOWN; - ev.key.keysym.sym = SDLK_p; - ev.key.keysym.mod = KMOD_CTRL | KMOD_SHIFT; - } - - /* Those could be reached too via clicks on the buttons. */ - else if (event.button.button == joystick_button_undo) - { - ev.type = SDL_KEYDOWN; - ev.key.keysym.sym = SDLK_z; - ev.key.keysym.mod = KMOD_CTRL; - } - else if (event.button.button == joystick_button_redo) - { - ev.type = SDL_KEYDOWN; - ev.key.keysym.sym = SDLK_r; - ev.key.keysym.mod = KMOD_CTRL; - } - else if (event.button.button == joystick_button_open) - { - ev.type = SDL_KEYDOWN; - ev.key.keysym.sym = SDLK_o; - ev.key.keysym.mod = KMOD_CTRL; - } - else if (event.button.button == joystick_button_new) - { - ev.type = SDL_KEYDOWN; - ev.key.keysym.sym = SDLK_n; - ev.key.keysym.mod = KMOD_CTRL; - } - else if (event.button.button == joystick_button_save) - { - ev.type = SDL_KEYDOWN; - ev.key.keysym.sym = SDLK_s; - ev.key.keysym.mod = KMOD_CTRL; - } - else if (event.button.button == joystick_button_print) - { - ev.type = SDL_KEYDOWN; - ev.key.keysym.sym = SDLK_p; - ev.key.keysym.mod = KMOD_CTRL; - } - - - /* Now the clicks on the tool buttons. */ - /* Note that at small window sizes there are scroll buttons in the tools rectangle */ - /* and some tools are hiden. */ - /* As any click outside of real_r_tools will not select the desired tool, */ - /* the workaround I came up with is to click on the scroll buttons to reveal the button, */ - /* then click on it. */ - else if (event.button.button == joystick_button_selectbrushtool || - event.button.button == joystick_button_selectstamptool || - event.button.button == joystick_button_selectlinestool || - event.button.button == joystick_button_selectshapestool || - event.button.button == joystick_button_selecttexttool || - event.button.button == joystick_button_selectlabeltool || - event.button.button == joystick_button_selectmagictool || - event.button.button == joystick_button_selecterasertool) - - { - if (event.button.button == joystick_button_selectbrushtool) - { - ev.button.x = (TOOL_BRUSH % 2) * button_w + button_w / 2; - ev.button.y = real_r_tools.y + TOOL_BRUSH / 2 * button_h + button_h / 2; - } - - else if (event.button.button == joystick_button_selectstamptool) - { - ev.button.x = (TOOL_STAMP % 2) * button_w + button_w / 2; - ev.button.y = real_r_tools.y + TOOL_STAMP / 2 * button_h + button_h / 2; - } - - else if (event.button.button == joystick_button_selectlinestool) - { - ev.button.x = (TOOL_LINES % 2) * button_w + button_w / 2; - ev.button.y = real_r_tools.y + TOOL_LINES / 2 * button_h + button_h / 2; - } - - else if (event.button.button == joystick_button_selectshapestool) - { - ev.button.x = (TOOL_SHAPES % 2) * button_w + button_w / 2; - ev.button.y = real_r_tools.y + TOOL_SHAPES / 2 * button_h + button_h / 2; - } - - else if (event.button.button == joystick_button_selecttexttool) - { - ev.button.x = (TOOL_TEXT % 2) * button_w + button_w / 2; - ev.button.y = real_r_tools.y + TOOL_TEXT / 2 * button_h + button_h / 2; - } - - else if (event.button.button == joystick_button_selectlabeltool) - { - ev.button.x = (TOOL_LABEL % 2) * button_w + button_w / 2; - ev.button.y = real_r_tools.y + TOOL_LABEL / 2 * button_h + button_h / 2; - } - - else if (event.button.button == joystick_button_selectmagictool) - { - ev.button.x = (TOOL_MAGIC % 2) * button_w + button_w / 2; - ev.button.y = real_r_tools.y + TOOL_MAGIC / 2 * button_h + button_h / 2; - } - - else if (event.button.button == joystick_button_selecterasertool) - { - ev.button.x = (TOOL_ERASER % 2) * button_w + button_w / 2; - ev.button.y = real_r_tools.y + TOOL_ERASER / 2 * button_h + button_h / 2; - } - - /* Deal with scroll to reveal the button that should be clicked */ - eby = ev.button.y; - ts = tool_scroll; - - while (eby < real_r_tools.y + ts / 2 * button_h) - { - ev.button.y = real_r_tools.y - 1; - SDL_PushEvent(&ev); - ts -= 2; - } - - /* We don't need this ATM, but better left it ready in case the number of tools grows enough */ - while (eby > real_r_tools.y + real_r_tools.h + ts / 2 * button_h) - { - ev.button.y = real_r_tools.y + real_r_tools.h + 1; - SDL_PushEvent(&ev); - ts += 2; - } - - ev.button.y = eby - ts / 2 * button_h; - } + ev.type = SDL_KEYDOWN; + ev.key.keysym.sym = SDLK_ESCAPE; + ev.key.keysym.mod = KMOD_CTRL; } + else if (event.button.button == joystick_button_pagesetup) + { + ev.type = SDL_KEYDOWN; + ev.key.keysym.sym = SDLK_p; + ev.key.keysym.mod = KMOD_CTRL | KMOD_SHIFT; + } + + /* Those could be reached too via clicks on the buttons. */ + else if (event.button.button == joystick_button_undo) + { + ev.type = SDL_KEYDOWN; + ev.key.keysym.sym = SDLK_z; + ev.key.keysym.mod = KMOD_CTRL; + } + else if (event.button.button == joystick_button_redo) + { + ev.type = SDL_KEYDOWN; + ev.key.keysym.sym = SDLK_r; + ev.key.keysym.mod = KMOD_CTRL; + } + else if (event.button.button == joystick_button_open) + { + ev.type = SDL_KEYDOWN; + ev.key.keysym.sym = SDLK_o; + ev.key.keysym.mod = KMOD_CTRL; + } + else if (event.button.button == joystick_button_new) + { + ev.type = SDL_KEYDOWN; + ev.key.keysym.sym = SDLK_n; + ev.key.keysym.mod = KMOD_CTRL; + } + else if (event.button.button == joystick_button_save) + { + ev.type = SDL_KEYDOWN; + ev.key.keysym.sym = SDLK_s; + ev.key.keysym.mod = KMOD_CTRL; + } + else if (event.button.button == joystick_button_print) + { + ev.type = SDL_KEYDOWN; + ev.key.keysym.sym = SDLK_p; + ev.key.keysym.mod = KMOD_CTRL; + } + + + /* Now the clicks on the tool buttons. */ + /* Note that at small window sizes there are scroll buttons in the tools rectangle */ + /* and some tools are hiden. */ + /* As any click outside of real_r_tools will not select the desired tool, */ + /* the workaround I came up with is to click on the scroll buttons to reveal the button, */ + /* then click on it. */ + else if (event.button.button == joystick_button_selectbrushtool || + event.button.button == joystick_button_selectstamptool || + event.button.button == joystick_button_selectlinestool || + event.button.button == joystick_button_selectshapestool || + event.button.button == joystick_button_selecttexttool || + event.button.button == joystick_button_selectlabeltool || + event.button.button == joystick_button_selectmagictool || + event.button.button == joystick_button_selecterasertool) + + { + if (event.button.button == joystick_button_selectbrushtool) + { + ev.button.x = (TOOL_BRUSH % 2) * button_w + button_w / 2; + ev.button.y = + real_r_tools.y + TOOL_BRUSH / 2 * button_h + button_h / 2; + } + + else if (event.button.button == joystick_button_selectstamptool) + { + ev.button.x = (TOOL_STAMP % 2) * button_w + button_w / 2; + ev.button.y = + real_r_tools.y + TOOL_STAMP / 2 * button_h + button_h / 2; + } + + else if (event.button.button == joystick_button_selectlinestool) + { + ev.button.x = (TOOL_LINES % 2) * button_w + button_w / 2; + ev.button.y = + real_r_tools.y + TOOL_LINES / 2 * button_h + button_h / 2; + } + + else if (event.button.button == joystick_button_selectshapestool) + { + ev.button.x = (TOOL_SHAPES % 2) * button_w + button_w / 2; + ev.button.y = + real_r_tools.y + TOOL_SHAPES / 2 * button_h + button_h / 2; + } + + else if (event.button.button == joystick_button_selecttexttool) + { + ev.button.x = (TOOL_TEXT % 2) * button_w + button_w / 2; + ev.button.y = + real_r_tools.y + TOOL_TEXT / 2 * button_h + button_h / 2; + } + + else if (event.button.button == joystick_button_selectlabeltool) + { + ev.button.x = (TOOL_LABEL % 2) * button_w + button_w / 2; + ev.button.y = + real_r_tools.y + TOOL_LABEL / 2 * button_h + button_h / 2; + } + + else if (event.button.button == joystick_button_selectmagictool) + { + ev.button.x = (TOOL_MAGIC % 2) * button_w + button_w / 2; + ev.button.y = + real_r_tools.y + TOOL_MAGIC / 2 * button_h + button_h / 2; + } + + else if (event.button.button == joystick_button_selecterasertool) + { + ev.button.x = (TOOL_ERASER % 2) * button_w + button_w / 2; + ev.button.y = + real_r_tools.y + TOOL_ERASER / 2 * button_h + button_h / 2; + } + + /* Deal with scroll to reveal the button that should be clicked */ + eby = ev.button.y; + ts = tool_scroll; + + while (eby < real_r_tools.y + ts / 2 * button_h) + { + ev.button.y = real_r_tools.y - 1; + SDL_PushEvent(&ev); + ts -= 2; + } + + /* We don't need this ATM, but better left it ready in case the number of tools grows enough */ + while (eby > real_r_tools.y + real_r_tools.h + ts / 2 * button_h) + { + ev.button.y = real_r_tools.y + real_r_tools.h + 1; + SDL_PushEvent(&ev); + ts += 2; + } + + ev.button.y = eby - ts / 2 * button_h; + } + } else - { - ev.button.type = SDL_MOUSEBUTTONUP; - ev.button.state = SDL_RELEASED; - } + { + ev.button.type = SDL_MOUSEBUTTONUP; + ev.button.state = SDL_RELEASED; + } #ifdef DEBUG printf("result %d %d\n", ev.button.x, ev.button.y); @@ -29412,12 +31422,12 @@ static void handle_joybuttonupdownscl(SDL_Event event, int oldpos_x, int oldpos_ /* See if it's a button we ignore */ for (i = 0; i < joystick_buttons_ignore_len && !ignore; i++) + { + if (event.button.button == joystick_buttons_ignore[i]) { - if (event.button.button == joystick_buttons_ignore[i]) - { - ignore = 1; - } + ignore = 1; } + } if (!ignore) SDL_PushEvent(&ev); @@ -29442,9 +31452,11 @@ static void start_motion_convert(SDL_Event event) if (HIT(r_tools)) scroll = 1; - else if (HIT(r_toolopt) && (cur_tool == TOOL_BRUSH || cur_tool == TOOL_STAMP || - cur_tool == TOOL_LINES || cur_tool == TOOL_SHAPES || cur_tool == TOOL_TEXT || - cur_tool == TOOL_LABEL || cur_tool == TOOL_MAGIC)) + else if (HIT(r_toolopt) + && (cur_tool == TOOL_BRUSH || cur_tool == TOOL_STAMP + || cur_tool == TOOL_LINES || cur_tool == TOOL_SHAPES + || cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL + || cur_tool == TOOL_MAGIC)) scroll = 1; else if ((cur_tool == TOOL_OPEN) && HIT(r_canvas)) scroll = 1; @@ -29479,27 +31491,29 @@ static void convert_motion_to_wheel(SDL_Event event) int high = 0; if (HIT(r_tools)) - { - scroll = 1; - high = 48; - } - else if (HIT(r_toolopt) && (cur_tool == TOOL_BRUSH || cur_tool == TOOL_STAMP || - cur_tool == TOOL_LINES || cur_tool == TOOL_SHAPES || cur_tool == TOOL_TEXT || - cur_tool == TOOL_LABEL || cur_tool == TOOL_MAGIC)) - { - scroll = 1; - high = 48; - } + { + scroll = 1; + high = 48; + } + else if (HIT(r_toolopt) + && (cur_tool == TOOL_BRUSH || cur_tool == TOOL_STAMP + || cur_tool == TOOL_LINES || cur_tool == TOOL_SHAPES + || cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL + || cur_tool == TOOL_MAGIC)) + { + scroll = 1; + high = 48; + } else if ((cur_tool == TOOL_OPEN) && HIT(r_canvas)) - { - scroll = 1; - high = THUMB_H; - } + { + scroll = 1; + high = THUMB_H; + } else if ((cur_tool == TOOL_NEW) && HIT(r_canvas)) - { - scroll = 1; - high = THUMB_H; - } + { + scroll = 1; + high = THUMB_H; + } if (scroll != 1) return; @@ -29508,21 +31522,21 @@ static void convert_motion_to_wheel(SDL_Event event) motion_dy += event.button.y - oldpos_y; if (motion_dy > 0) + { + while (motion_dy - high > 0) { - while (motion_dy - high > 0) - { - SDL_SendMouseWheel(NULL, event.motion.which, 0, 1); - motion_dy -= high; - } + SDL_SendMouseWheel(NULL, event.motion.which, 0, 1); + motion_dy -= high; } + } else + { + while (motion_dy + high < 0) { - while (motion_dy + high < 0) - { - SDL_SendMouseWheel(NULL, event.motion.which, 0, -1); - motion_dy += high; - } + SDL_SendMouseWheel(NULL, event.motion.which, 0, -1); + motion_dy += high; } + } } #endif @@ -29534,33 +31548,43 @@ static void convert_motion_to_wheel(SDL_Event event) * @param char * fallback -- path, under $HOME, to use instead (e.g., "Pictures") * @return char * path (caller is expected to free() it) */ -char * get_xdg_user_dir(const char * dir_type, const char * fallback) { - FILE * fi; - char * config_home, * found; - char tmp_path[MAX_PATH], config_path[MAX_PATH], line[MAX_PATH], search[MAX_PATH], return_path[MAX_PATH]; +char *get_xdg_user_dir(const char *dir_type, const char *fallback) +{ + FILE *fi; + char *config_home, *found; + char tmp_path[MAX_PATH], config_path[MAX_PATH], line[MAX_PATH], + search[MAX_PATH], return_path[MAX_PATH]; int found_it; found_it = FALSE; /* Figure out where XDG user-dirs config exists, and use it if possible */ - if (getenv("XDG_CONFIG_HOME") != NULL) { + if (getenv("XDG_CONFIG_HOME") != NULL) + { config_home = strdup(getenv("XDG_CONFIG_HOME")); - } else { + } + else + { #ifdef DEBUG fprintf(stderr, "XDG_CONFIG_HOME not set, checking $HOME/.config/\n"); #endif - if (getenv("HOME") != NULL) { + if (getenv("HOME") != NULL) + { safe_snprintf(tmp_path, MAX_PATH, "%s/.config", getenv("HOME")); config_home = strdup(tmp_path); - } else { + } + else + { #ifdef DEBUG - fprintf(stderr, "No HOME, either?! Returing fallback in current directory\n"); + fprintf(stderr, + "No HOME, either?! Returing fallback in current directory\n"); #endif return strdup(fallback); } } - if (config_home[strlen(config_home) - 1] == '/') { + if (config_home[strlen(config_home) - 1] == '/') + { config_home[strlen(config_home) - 1] = '\0'; } safe_snprintf(config_path, MAX_PATH, "%s/user-dirs.dirs", config_home); @@ -29574,34 +31598,42 @@ char * get_xdg_user_dir(const char * dir_type, const char * fallback) { /* Read the config to find the path we want */ fi = fopen(config_path, "r"); - if (fi != NULL) { + if (fi != NULL) + { /* Search for a line in the form of either either XDG_PICTURES_DIR="$HOME/Pictures" or XDG_PICTURES_DIR="/Path/To/Pictures" - */ + */ #ifdef DEBUG fprintf(stderr, "Searching it for: %s\n", search); #endif - while (fgets(line, MAX_PATH, fi) && !found_it) { + while (fgets(line, MAX_PATH, fi) && !found_it) + { /* Trim trailing CR/LF */ - if (line[strlen(line) - 1] == '\n' || - line[strlen(line) - 1] == '\r') { - line[strlen(line) - 1] = '\0'; - } + if (line[strlen(line) - 1] == '\n' || line[strlen(line) - 1] == '\r') + { + line[strlen(line) - 1] = '\0'; + } - if (strstr(line, search) == line) { + if (strstr(line, search) == line) + { found = line + strlen(search); #ifdef DEBUG fprintf(stderr, "Found it: %s\n", found); #endif - if (strstr(found, "$HOME/") == found) { - safe_snprintf(return_path, MAX_PATH, "%s/%s", getenv("HOME"), found + 6 /* skip '$HOME/' */); - } else { + if (strstr(found, "$HOME/") == found) + { + safe_snprintf(return_path, MAX_PATH, "%s/%s", getenv("HOME"), + found + 6 /* skip '$HOME/' */ ); + } + else + { safe_strncpy(return_path, found, MAX_PATH); } /* Trim trailing " */ - if (return_path[strlen(return_path) - 1] == '\"') { + if (return_path[strlen(return_path) - 1] == '\"') + { return_path[strlen(return_path) - 1] = '\0'; } @@ -29611,12 +31643,15 @@ char * get_xdg_user_dir(const char * dir_type, const char * fallback) { fclose(fi); #ifdef DEBUG - } else { + } + else + { fprintf(stderr, "%s doesn't exist\n", config_path); #endif } - if (!found_it) { + if (!found_it) + { #ifdef DEBUG fprintf(stderr, "Using fallback of $HOME/%s\n", fallback); #endif @@ -29639,19 +31674,21 @@ char * get_xdg_user_dir(const char * dir_type, const char * fallback) { * @param int speed -- how fast to play the slideshow (0 and 1 both = slowest, 10 = fasted) * @return int -- 0 if export failed or was aborted, 1 if successful */ -static int export_gif(int *selected, int num_selected, char *dirname, char **d_names, char **d_exts, int speed) { +static int export_gif(int *selected, int num_selected, char *dirname, + char **d_names, char **d_exts, int speed) +{ char *tmp_starter_id, *tmp_template_id, *tmp_file_id; int tmp_starter_mirrored, tmp_starter_flipped, tmp_starter_personal; - char * gif_fname; + char *gif_fname; char fname[MAX_PATH]; int i, j, done, which, x, y; SDL_Surface *img; int overall_w, overall_h, overall_area; - Uint8 * bitmap; + Uint8 *bitmap; Uint8 r, g, b, a; size_t pixels_size; unsigned char *raw_8bit_pixels; - uint8_t gif_palette[768]; /* 256 x 3 */ + uint8_t gif_palette[768]; /* 256 x 3 */ liq_attr *liq_handle; liq_image *input_image; liq_result *quantization_result; @@ -29675,10 +31712,10 @@ static int export_gif(int *selected, int num_selected, char *dirname, char **d_n gif_fname = get_export_filepath("gif"); if (gif_fname == NULL) - { - /* Can't create export dir! Abort! */ - return FALSE; - } + { + /* Can't create export dir! Abort! */ + return FALSE; + } /* For now, always saving GIF using the size of Tux Paint's window, which is how images appear in the slide show */ @@ -29687,147 +31724,161 @@ static int export_gif(int *selected, int num_selected, char *dirname, char **d_n overall_area = overall_w * overall_h; if (speed == 0) - { - gif_speed = 1; - } + { + gif_speed = 1; + } gif_speed = (10 - speed) * 50; bitmap = malloc(num_selected * overall_area * 4); if (bitmap != NULL) + { + done = 0; + + for (i = 0; i < num_selected && !done; i++) { - done = 0; + which = selected[i]; + show_progress_bar(screen); - for (i = 0; i < num_selected && !done; i++) + + /* Figure out filename: */ + safe_snprintf(fname, sizeof(fname), "%s/%s%s", dirname, d_names[which], + d_exts[which]); + + /* Load and scale the image */ + img = myIMG_Load(fname); + + if (img != NULL) + { + autoscale_copy_smear_free(img, screen, SDL_BlitSurface); + + safe_strncpy(file_id, d_names[which], sizeof(file_id)); + + /* See if this saved image was based on a 'starter' */ + load_starter_id(d_names[which], NULL); + if (starter_id[0] != '\0') { - which = selected[i]; - show_progress_bar(screen); + load_starter(starter_id); + if (starter_mirrored) + mirror_starter(); - /* Figure out filename: */ - safe_snprintf(fname, sizeof(fname), "%s/%s%s", dirname, d_names[which], d_exts[which]); - - /* Load and scale the image */ - img = myIMG_Load(fname); - - if (img != NULL) - { - autoscale_copy_smear_free(img, screen, SDL_BlitSurface); - - safe_strncpy(file_id, d_names[which], sizeof(file_id)); - - /* See if this saved image was based on a 'starter' */ - load_starter_id(d_names[which], NULL); - if (starter_id[0] != '\0') - { - load_starter(starter_id); - - if (starter_mirrored) - mirror_starter(); - - if (starter_flipped) - flip_starter(); - } - else - load_template(template_id); - } else { - /* Error loading !*/ - fprintf(stderr, "Error loading %s!\n", fname); - /* FIXME Abort? */ - } - - /* Record the raw RGB into a big strip, to be quantized and sliced later */ - for (y = 0; y < overall_h; y++) { - for (x = 0; x < overall_w; x++) { - SDL_GetRGBA(getpixels[screen->format->BytesPerPixel](screen, x, y), screen->format, &r, &g, &b, &a); - - bitmap[((i * overall_area) + (y * overall_w) + x) * 4 + 0] = r; - bitmap[((i * overall_area) + (y * overall_w) + x) * 4 + 1] = g; - bitmap[((i * overall_area) + (y * overall_w) + x) * 4 + 2] = b; - bitmap[((i * overall_area) + (y * overall_w) + x) * 4 + 3] = 255; - } - } - - SDL_Flip(screen); - done = export_gif_monitor_events(); + if (starter_flipped) + flip_starter(); } + else + load_template(template_id); + } + else + { + /* Error loading ! */ + fprintf(stderr, "Error loading %s!\n", fname); + /* FIXME Abort? */ + } - - if (!done) + /* Record the raw RGB into a big strip, to be quantized and sliced later */ + for (y = 0; y < overall_h; y++) + { + for (x = 0; x < overall_w; x++) { - /* Quantize to max 256 (8bpp) colors and generate a suitable palette */ - liq_handle = liq_attr_create(); - input_image = liq_image_create_rgba(liq_handle, bitmap, overall_w, num_selected * overall_h, 0); - liq_set_max_colors(liq_handle, 256); + SDL_GetRGBA(getpixels[screen->format->BytesPerPixel] (screen, x, y), + screen->format, &r, &g, &b, &a); - show_progress_bar(screen); - done = export_gif_monitor_events(); + bitmap[((i * overall_area) + (y * overall_w) + x) * 4 + 0] = r; + bitmap[((i * overall_area) + (y * overall_w) + x) * 4 + 1] = g; + bitmap[((i * overall_area) + (y * overall_w) + x) * 4 + 2] = b; + bitmap[((i * overall_area) + (y * overall_w) + x) * 4 + 3] = 255; + } + } + + SDL_Flip(screen); + done = export_gif_monitor_events(); + } + + + if (!done) + { + /* Quantize to max 256 (8bpp) colors and generate a suitable palette */ + liq_handle = liq_attr_create(); + input_image = + liq_image_create_rgba(liq_handle, bitmap, overall_w, + num_selected * overall_h, 0); + liq_set_max_colors(liq_handle, 256); + + show_progress_bar(screen); + done = export_gif_monitor_events(); #if LIQ_VERSION >= 20800 - qtiz_status = liq_image_quantize(input_image, liq_handle, &quantization_result); - done = (qtiz_status != LIQ_OK); + qtiz_status = + liq_image_quantize(input_image, liq_handle, &quantization_result); + done = (qtiz_status != LIQ_OK); #else - quantization_result = liq_quantize_image(liq_handle, input_image); - done = (quantization_result == NULL); + quantization_result = liq_quantize_image(liq_handle, input_image); + done = (quantization_result == NULL); #endif - if (!done) { - // Use libimagequant to make new image pixels from the palette - pixels_size = num_selected * overall_area; - raw_8bit_pixels = malloc(pixels_size); - liq_set_dithering_level(quantization_result, 1.0); + if (!done) + { + // Use libimagequant to make new image pixels from the palette + pixels_size = num_selected * overall_area; + raw_8bit_pixels = malloc(pixels_size); + liq_set_dithering_level(quantization_result, 1.0); - liq_write_remapped_image(quantization_result, input_image, raw_8bit_pixels, pixels_size); - palette = liq_get_palette(quantization_result); - free(bitmap); + liq_write_remapped_image(quantization_result, input_image, + raw_8bit_pixels, pixels_size); + palette = liq_get_palette(quantization_result); + free(bitmap); - for (j = 0; j < (int) palette->count; j++) { - gif_palette[j * 3 + 0] = palette->entries[j].r; - gif_palette[j * 3 + 1] = palette->entries[j].g; - gif_palette[j * 3 + 2] = palette->entries[j].b; - } - - /* Open GIF */ - ge_GIF *gif = ge_new_gif( - gif_fname, - overall_w, overall_h, - gif_palette, - 8, /* 256 colors */ - 0 /* infinite loop */ - ); - - /* Export each frame */ - for (i = 0; i < num_selected && !done; i++) - { - memcpy(gif->frame, raw_8bit_pixels + i * overall_area, overall_area); - ge_add_frame(gif, gif_speed); - - show_progress_bar(screen); - done = export_gif_monitor_events(); - } - - /* Close the GIF */ - ge_close_gif(gif); - } else { - fprintf(stderr, "Quantization failed\n"); - done = 1; - } - - if (done) - { - /* Aborted; discard the partially-saved GIF */ - unlink(gif_fname); - } + for (j = 0; j < (int) palette->count; j++) + { + gif_palette[j * 3 + 0] = palette->entries[j].r; + gif_palette[j * 3 + 1] = palette->entries[j].g; + gif_palette[j * 3 + 2] = palette->entries[j].b; } + + /* Open GIF */ + ge_GIF *gif = ge_new_gif(gif_fname, + overall_w, overall_h, + gif_palette, + 8, /* 256 colors */ + 0 /* infinite loop */ + ); + + /* Export each frame */ + for (i = 0; i < num_selected && !done; i++) + { + memcpy(gif->frame, raw_8bit_pixels + i * overall_area, + overall_area); + ge_add_frame(gif, gif_speed); + + show_progress_bar(screen); + done = export_gif_monitor_events(); + } + + /* Close the GIF */ + ge_close_gif(gif); + } + else + { + fprintf(stderr, "Quantization failed\n"); + done = 1; + } + + if (done) + { + /* Aborted; discard the partially-saved GIF */ + unlink(gif_fname); + } } + } else - { - /* Out of memory! */ - done = 1; - } + { + /* Out of memory! */ + done = 1; + } /* Restore everything about the currently-active image that got clobbered above */ - strcpy(starter_id, tmp_starter_id); /* safe; originally strdup()'d from the dest. */ + strcpy(starter_id, tmp_starter_id); /* safe; originally strdup()'d from the dest. */ free(tmp_starter_id); strcpy(template_id, tmp_template_id); /* safe; originally strdup()'d from the dest. */ @@ -29844,7 +31895,7 @@ static int export_gif(int *selected, int num_selected, char *dirname, char **d_n free(gif_fname); /* Success if we didn't have an error, and user didn't abort */ - return(!done); + return (!done); } /** @@ -29855,26 +31906,28 @@ static int export_gif(int *selected, int num_selected, char *dirname, char **d_n * * @return int 0 = keep going, 1 = abort */ -int export_gif_monitor_events(void) { +int export_gif_monitor_events(void) +{ int done; SDL_Event event; SDLKey key; done = 0; while (SDL_PollEvent(&event)) + { + if (event.type == SDL_QUIT) { - if (event.type == SDL_QUIT) - { - done = 1; - } - else if (event.type == SDL_KEYDOWN) - { - key = event.key.keysym.sym; - if (key == SDLK_ESCAPE) { - done = 1; - } - } + done = 1; } + else if (event.type == SDL_KEYDOWN) + { + key = event.key.keysym.sym; + if (key == SDLK_ESCAPE) + { + done = 1; + } + } + } SDL_Delay(10); return done; } @@ -29889,11 +31942,12 @@ int export_gif_monitor_events(void) { * @param char * fname -- full path to the image to export * @return int 1 = success, 0 = failed */ -static int export_pict(char * fname) { - FILE * fi, * fo; +static int export_pict(char *fname) +{ + FILE *fi, *fo; size_t len; unsigned char buf[1024]; - char * pict_fname; + char *pict_fname; Uint32 time_before, time_after; do_setcursor(cursor_watch); @@ -29901,36 +31955,40 @@ static int export_pict(char * fname) { fi = fopen(fname, "rb"); if (fi == NULL) - { - fprintf(stderr, "Cannot export from saved Tux Paint file '%s'\nThe error that occurred was:\n%s\n\n", fname, strerror(errno)); - return FALSE; - } + { + fprintf(stderr, + "Cannot export from saved Tux Paint file '%s'\nThe error that occurred was:\n%s\n\n", + fname, strerror(errno)); + return FALSE; + } time_before = SDL_GetTicks(); pict_fname = get_export_filepath("png"); if (pict_fname == NULL) - { - fclose(fi); - return FALSE; - } + { + fclose(fi); + return FALSE; + } fo = fopen(pict_fname, "wb"); if (fo == NULL) - { - fprintf(stderr, "Cannot export to new file '%s'\nThe error that occurred was:\n%s\n\n", pict_fname, strerror(errno)); - free(pict_fname); - fclose(fi); - return FALSE; - } + { + fprintf(stderr, + "Cannot export to new file '%s'\nThe error that occurred was:\n%s\n\n", + pict_fname, strerror(errno)); + free(pict_fname); + fclose(fi); + return FALSE; + } while (!feof(fi)) + { + len = fread(buf, sizeof(unsigned char), sizeof(buf), fi); + if (len > 0) { - len = fread(buf, sizeof(unsigned char), sizeof(buf), fi); - if (len > 0) - { - fwrite(buf, sizeof(unsigned char), sizeof(buf), fo); - } + fwrite(buf, sizeof(unsigned char), sizeof(buf), fo); } + } /* FIXME: Probably good to check for errors here and respond accordingly -bjk 2020.07.26 */ @@ -29943,10 +32001,10 @@ static int export_pict(char * fname) { so ensure at least one second has elapsed */ time_after = SDL_GetTicks(); if (time_after - time_before < 1000) - { - show_progress_bar(screen); - SDL_Delay(time_after + 1000 - time_before); - } + { + show_progress_bar(screen); + SDL_Delay(time_after + 1000 - time_before); + } return TRUE; } @@ -29966,7 +32024,8 @@ static int export_pict(char * fname) { * (e.g., /home/username/Pictures/TuxPaint/2020072620110100.gif") * Or NULL if the directory cannot be created. */ -static char * get_export_filepath(const char * ext) { +static char *get_export_filepath(const char *ext) +{ char *rname; char fname[FILENAME_MAX]; char timestamp[16]; @@ -29974,17 +32033,26 @@ static char * get_export_filepath(const char * ext) { /* Make sure the export dir exists */ - if (!make_directory(DIR_EXPORT, "", "Can't create export directory; will try to make its parent (E016)")) + if (!make_directory + (DIR_EXPORT, "", + "Can't create export directory; will try to make its parent (E016)")) + { + /* See if perhaps we need to try and make its parent directory first? */ + if (make_directory + (DIR_EXPORT_PARENT, "", + "Can't create export directory parent (E016b)")) { - /* See if perhaps we need to try and make its parent directory first? */ - if (make_directory(DIR_EXPORT_PARENT, "", "Can't create export directory parent (E016b)")) { - if (!make_directory(DIR_EXPORT, "", "Can't create export directory (E016c)")) { - return NULL; - } - } else { + if (!make_directory + (DIR_EXPORT, "", "Can't create export directory (E016c)")) + { return NULL; } } + else + { + return NULL; + } + } /* Create a unique filename, within that dir */ t = time(NULL); @@ -29993,24 +32061,27 @@ static char * get_export_filepath(const char * ext) { rname = get_fname(fname, DIR_EXPORT); debug(rname); - return(rname); + return (rname); } -char * safe_strncat(char *dest, const char *src, size_t n) { - char * ptr; +char *safe_strncat(char *dest, const char *src, size_t n) +{ + char *ptr; ptr = strncat(dest, src, n - 1); dest[n - 1] = '\0'; return ptr; } -char * safe_strncpy(char *dest, const char *src, size_t n) { - char * ptr; - ptr = strncpy(dest, src, n - 1); /* FIXME: Clean up, and keep safe, to avoid compiler warning (e.g., "output may be truncated copying 255 bytes from a string of length 255") */ +char *safe_strncpy(char *dest, const char *src, size_t n) +{ + char *ptr; + ptr = strncpy(dest, src, n - 1); /* FIXME: Clean up, and keep safe, to avoid compiler warning (e.g., "output may be truncated copying 255 bytes from a string of length 255") */ dest[n - 1] = '\0'; return ptr; } -int safe_snprintf(char *str, size_t size, const char *format, ...) { +int safe_snprintf(char *str, size_t size, const char *format, ...) +{ int r; va_list ap; @@ -30022,7 +32093,8 @@ int safe_snprintf(char *str, size_t size, const char *format, ...) { return r; } -static void sloppy_frac(float f, int * numer, int * denom) { +static void sloppy_frac(float f, int *numer, int *denom) +{ int n, d, fr, i, gcd, n_over_d_100; fr = round(f * 100.0); @@ -30031,25 +32103,25 @@ static void sloppy_frac(float f, int * numer, int * denom) { *denom = 1; for (d = SLOPPY_FRAC_MIN; d <= SLOPPY_FRAC_MAX; d++) + { + for (n = 1; n < d; n++) { - for (n = 1; n < d; n++) - { - n_over_d_100 = ((n * 100) / d); - if (n_over_d_100 >= fr && - n_over_d_100 < (fr + ((SLOPPY_FRAC_MIN * 100) / SLOPPY_FRAC_MAX))) - { - *numer = n; - *denom = d; - } - } + n_over_d_100 = ((n * 100) / d); + if (n_over_d_100 >= fr && + n_over_d_100 < (fr + ((SLOPPY_FRAC_MIN * 100) / SLOPPY_FRAC_MAX))) + { + *numer = n; + *denom = d; + } } + } gcd = 1; for (i = 1; i <= *numer && i <= *denom; i++) - { - if ((*numer % i) == 0 && (*denom % i) == 0) - gcd = i; - } + { + if ((*numer % i) == 0 && (*denom % i) == 0) + gcd = i; + } *numer /= gcd; *denom /= gcd; @@ -30061,7 +32133,8 @@ static void sloppy_frac(float f, int * numer, int * denom) { * * @param int * old_x, old_y -- Pointers to feed the position of the chosen label */ -static void select_label_node(int * old_x, int * old_y) { +static void select_label_node(int *old_x, int *old_y) +{ unsigned int i; int j; @@ -30079,10 +32152,10 @@ static void select_label_node(int * old_x, int * old_y) { i = 0; texttool_len = select_texttool_len; while (i < texttool_len) - { - texttool_str[i] = select_texttool_str[i]; - i = i + 1; - } + { + texttool_str[i] = select_texttool_str[i]; + i = i + 1; + } texttool_str[i] = L'\0'; cur_color = select_color; @@ -30098,13 +32171,13 @@ static void select_label_node(int * old_x, int * old_y) { /* ??? */ for (j = 0; j < num_font_families; j++) + { + if (user_font_families[j] && user_font_families[j]->handle) { - if (user_font_families[j] && user_font_families[j]->handle) - { - TuxPaint_Font_CloseFont(user_font_families[j]->handle); - user_font_families[j]->handle = NULL; - } + TuxPaint_Font_CloseFont(user_font_families[j]->handle); + user_font_families[j]->handle = NULL; } + } update_screen_rect(&r_toolopt); @@ -30142,7 +32215,8 @@ static void select_label_node(int * old_x, int * old_y) { * * Sets insertion point cursor location (`cursor_x`, `cursor_y`) * * Displays Label tool's main instructions (Tux tip) */ -static void apply_label_node(int old_x, int old_y) { +static void apply_label_node(int old_x, int old_y) +{ cursor_x = old_x; cursor_y = old_y; cursor_left = old_x; @@ -30169,7 +32243,8 @@ static void apply_label_node(int old_x, int old_y) { rect.w = label_node_to_edit->save_width; rect.h = label_node_to_edit->save_height; - SDL_BlitSurface(label_node_to_edit->label_node_surface, NULL, canvas, &rect); + SDL_BlitSurface(label_node_to_edit->label_node_surface, NULL, canvas, + &rect); label_node_to_edit->is_enabled = FALSE; /* [Best way to explain this?] */ @@ -30183,15 +32258,15 @@ static void apply_label_node(int old_x, int old_y) { /* Make "Save" button available after this change (if appropriate) */ if (been_saved) - { - been_saved = 0; + { + been_saved = 0; - if (!disable_save) - tool_avail[TOOL_SAVE] = 1; + if (!disable_save) + tool_avail[TOOL_SAVE] = 1; - draw_toolbar(); - update_screen_rect(&r_tools); - } + draw_toolbar(); + update_screen_rect(&r_tools); + } /* Back to normal "how to use Label tool" tip; play sound */ update_canvas_ex_r(rect.x, rect.y, rect.w, rect.h, 1); @@ -30207,24 +32282,24 @@ static void apply_label_node(int old_x, int old_y) { * @param int y -- If -1, don't reposition vertically; otherwise, if y * is in top half of canvas, put keyboard on bottom & vice-versa */ -static void reposition_onscreen_keyboard(int y) { +static void reposition_onscreen_keyboard(int y) +{ if (onscreen_keyboard && kbd) + { + kbd_rect.x = button_w * 2 + (canvas->w - kbd->surface->w) / 2; + + if (y != -1) { - kbd_rect.x = button_w * 2 + (canvas->w - kbd->surface->w) / 2; - - if (y != -1) - { - if (y < r_canvas.h / 2) - kbd_rect.y = r_canvas.h - kbd->surface->h; - else - kbd_rect.y = 0; - } - - kbd_rect.w = kbd->surface->w; - kbd_rect.h = kbd->surface->h; - - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); + if (y < r_canvas.h / 2) + kbd_rect.y = r_canvas.h - kbd->surface->h; + else + kbd_rect.y = 0; } -} + kbd_rect.w = kbd->surface->w; + kbd_rect.h = kbd->surface->h; + + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); + update_screen_rect(&kbd_rect); + } +} diff --git a/src/win32_dirent.c b/src/win32_dirent.c index 461a5cdb4..0f05d56f4 100644 --- a/src/win32_dirent.c +++ b/src/win32_dirent.c @@ -54,10 +54,10 @@ DIR *opendir(const char *pSpec) strcat(pathname, "/*"); pDir->hFind = FindFirstFile(pathname, &pDir->wfd); if (pDir->hFind == INVALID_HANDLE_VALUE) - { - free(pDir); - pDir = NULL; - } + { + free(pDir); + pDir = NULL; + } return pDir; } @@ -83,16 +83,16 @@ struct dirent *readdir(struct DIR *pDir) assert(pDir != NULL); if (pDir->hFind) - { - strcpy(pDir->de.d_name, (const char *)pDir->wfd.cFileName); - if (!FindNextFile(pDir->hFind, &pDir->wfd)) + { + strcpy(pDir->de.d_name, (const char *) pDir->wfd.cFileName); + if (!FindNextFile(pDir->hFind, &pDir->wfd)) - { - FindClose(pDir->hFind); - pDir->hFind = NULL; - } - return &pDir->de; + { + FindClose(pDir->hFind); + pDir->hFind = NULL; } + return &pDir->de; + } return NULL; } @@ -107,7 +107,9 @@ struct dirent *readdir(struct DIR *pDir) */ int alphasort(const void *a, const void *b) { - return (strcmp((*(const struct dirent **)a)->d_name, (*(const struct dirent **)b)->d_name)); + return (strcmp + ((*(const struct dirent **) a)->d_name, + (*(const struct dirent **) b)->d_name)); } /** @@ -124,11 +126,15 @@ static int addToList(int i, struct dirent ***namelist, struct dirent *entry) int size; struct dirent *block; - *namelist = (struct dirent **)realloc((void *)(*namelist), (size_t) ((i + 1) * sizeof(struct dirent *))); + *namelist = + (struct dirent **) realloc((void *) (*namelist), + (size_t) ((i + 1) * sizeof(struct dirent *))); if (*namelist == NULL) return -1; - size = (((char *)&entry->d_name) - ((char *)entry)) + strlen(entry->d_name) + 1; - block = (struct dirent *)malloc(size); + size = + (((char *) &entry->d_name) - ((char *) entry)) + strlen(entry->d_name) + + 1; + block = (struct dirent *) malloc(size); if (block == NULL) return -1; (*namelist)[i] = block; @@ -145,7 +151,8 @@ static int addToList(int i, struct dirent ***namelist, struct dirent *entry) * @param compar Callback for sorting items in the list (via qsort()). * @return Count of items, or -1 on error. */ -int scandir(const char *dir, struct dirent ***namelist, selectCB select, comparCB compar) +int scandir(const char *dir, struct dirent ***namelist, selectCB select, + comparCB compar) { DIR *pDir; int count; @@ -157,15 +164,16 @@ int scandir(const char *dir, struct dirent ***namelist, selectCB select, comparC return -1; count = 0; while ((entry = readdir(pDir)) != NULL) - { - if (select == NULL || (select != NULL && select(entry))) - if ((count = addToList(count, namelist, entry)) < 0) - break; - } + { + if (select == NULL || (select != NULL && select(entry))) + if ((count = addToList(count, namelist, entry)) < 0) + break; + } closedir(pDir); if (count <= 0) return -1; if (compar != NULL) - qsort((void *)(*namelist), (size_t) count, sizeof(struct dirent *), compar); + qsort((void *) (*namelist), (size_t) count, sizeof(struct dirent *), + compar); return count; } diff --git a/src/win32_dirent.h b/src/win32_dirent.h index e04827828..cc77bb829 100644 --- a/src/win32_dirent.h +++ b/src/win32_dirent.h @@ -68,7 +68,8 @@ typedef struct extern DIR *opendir(const char *pSpec); extern void closedir(DIR * pDir); extern struct dirent *readdir(struct DIR *pDir); -typedef int (*selectCB) (const struct dirent *); -typedef int (*comparCB) (const void *, const void *); +typedef int (*selectCB)(const struct dirent *); +typedef int (*comparCB)(const void *, const void *); extern int alphasort(const void *a, const void *b); -extern int scandir(const char *dir, struct dirent ***namelist, selectCB select, comparCB compar); +extern int scandir(const char *dir, struct dirent ***namelist, + selectCB select, comparCB compar); diff --git a/src/win32_print.c b/src/win32_print.c index d9098931d..95fea9336 100644 --- a/src/win32_print.c +++ b/src/win32_print.c @@ -68,17 +68,18 @@ static SDL_Surface *make24bitDIB(SDL_Surface * surf) surf24 = SDL_ConvertSurface(surf, &pixfmt, SDL_SWSURFACE); surfDIB = SDL_CreateRGBSurface(SDL_SWSURFACE, surf24->w, surf24->h, 24, - pixfmt.Rmask, pixfmt.Gmask, pixfmt.Bmask, pixfmt.Amask); + pixfmt.Rmask, pixfmt.Gmask, pixfmt.Bmask, + pixfmt.Amask); linesize = surf24->w * 3; // Flip top2bottom dst = surfDIB->pixels; src = ((Uint8 *) surf24->pixels) + ((surf24->h - 1) * surf24->pitch); for (i = 0; i < surf24->h; ++i) - { - memcpy(dst, src, linesize); - src -= surf24->pitch; - dst += surfDIB->pitch; - } + { + memcpy(dst, src, linesize); + src -= surf24->pitch; + dst += surfDIB->pitch; + } SDL_FreeSurface(surf24); // Free temp surface @@ -104,16 +105,17 @@ static int GetDefaultPrinterStrings(char *device, char *driver, char *output) return 0; if (((dev = strtok(buff, ",")) != NULL) && - ((drv = strtok(NULL, ", ")) != NULL) && ((out = strtok(NULL, ", ")) != NULL)) - { - if (device) - strcpy(device, dev); - if (driver) - strcpy(driver, drv); - if (output) - strcpy(output, out); - return 1; - } + ((drv = strtok(NULL, ", ")) != NULL) + && ((out = strtok(NULL, ", ")) != NULL)) + { + if (device) + strcpy(device, dev); + if (driver) + strcpy(driver, drv); + if (output) + strcpy(output, out); + return 1; + } return 0; } @@ -143,7 +145,8 @@ static HANDLE LoadCustomPrinterHDEVMODE(HWND hWnd, const char *filepath) if (!OpenPrinter(device, &hPrinter, NULL)) goto err_exit; - sizeof_devmode = (int)DocumentProperties(hWnd, hPrinter, device, NULL, NULL, 0); + sizeof_devmode = + (int) DocumentProperties(hWnd, hPrinter, device, NULL, NULL, 0); if (!sizeof_devmode) goto err_exit; @@ -156,7 +159,8 @@ static HANDLE LoadCustomPrinterHDEVMODE(HWND hWnd, const char *filepath) if (!devmode) goto err_exit; - res = DocumentProperties(hWnd, hPrinter, device, devmode, NULL, DM_OUT_BUFFER); + res = + DocumentProperties(hWnd, hPrinter, device, devmode, NULL, DM_OUT_BUFFER); if (res != IDOK) goto err_exit; @@ -166,7 +170,9 @@ static HANDLE LoadCustomPrinterHDEVMODE(HWND hWnd, const char *filepath) goto err_exit; fclose(fp); - res = DocumentProperties(hWnd, hPrinter, device, devmode, devmode, DM_IN_BUFFER | DM_OUT_BUFFER); + res = + DocumentProperties(hWnd, hPrinter, device, devmode, devmode, + DM_IN_BUFFER | DM_OUT_BUFFER); if (res != IDOK) goto err_exit; @@ -189,25 +195,26 @@ err_exit: /** * FIXME */ -static int SaveCustomPrinterHDEVMODE(HWND hWnd, const char *filepath, HANDLE hDevMode) +static int SaveCustomPrinterHDEVMODE(HWND hWnd, const char *filepath, + HANDLE hDevMode) { FILE *fp = NULL; NOREF(hWnd); if ((fp = fopen(filepath, "wb")) != NULL) - { - DEVMODE *devmode = (DEVMODE *) GlobalLock(hDevMode); - int block_size = devmode->dmSize + devmode->dmDriverExtra; - int block_written; - char devname[dmDeviceNameSize]; + { + DEVMODE *devmode = (DEVMODE *) GlobalLock(hDevMode); + int block_size = devmode->dmSize + devmode->dmDriverExtra; + int block_written; + char devname[dmDeviceNameSize]; - strcpy(devname, (const char *)devmode->dmDeviceName); - fwrite(devname, 1, sizeof(devname), fp); - block_written = fwrite(devmode, 1, block_size, fp); - GlobalUnlock(hDevMode); - fclose(fp); - return block_size == block_written; - } + strcpy(devname, (const char *) devmode->dmDeviceName); + fwrite(devname, 1, sizeof(devname), fp); + block_written = fwrite(devmode, 1, block_size, fp); + GlobalUnlock(hDevMode); + fclose(fp); + return block_size == block_written; + } return 0; } @@ -222,10 +229,10 @@ static int FileExists(const char *filepath) FILE *fp; if ((fp = fopen(filepath, "rb")) != NULL) - { - fclose(fp); - return 1; - } + { + fclose(fp); + return 1; + } return 0; } @@ -250,22 +257,23 @@ static int GetCustomPrinterDC(HWND hWnd, const char *printcfg, int show) pd.hDevMode = LoadCustomPrinterHDEVMODE(hWnd, printcfg); if (show || !FileExists(printcfg)) + { + if (PrintDlg(&pd)) { - if (PrintDlg(&pd)) - { - hDCprinter = pd.hDC; - SaveCustomPrinterHDEVMODE(hWnd, printcfg, pd.hDevMode); - GlobalFree(pd.hDevMode); - return 1; - } + hDCprinter = pd.hDC; + SaveCustomPrinterHDEVMODE(hWnd, printcfg, pd.hDevMode); GlobalFree(pd.hDevMode); - return 0; + return 1; } + GlobalFree(pd.hDevMode); + return 0; + } { DEVMODE *devmode = (DEVMODE *) GlobalLock(pd.hDevMode); - hDCprinter = CreateDC(NULL, (const char *)devmode->dmDeviceName, NULL, devmode); + hDCprinter = + CreateDC(NULL, (const char *) devmode->dmDeviceName, NULL, devmode); GlobalUnlock(pd.hDevMode); GlobalFree(pd.hDevMode); } @@ -295,9 +303,9 @@ static int GetPrinterDC(HWND hWnd, const char *printcfg, int show) hDCprinter = NULL; if (printcfg) - { - return GetCustomPrinterDC(hWnd, printcfg, show); - } + { + return GetCustomPrinterDC(hWnd, printcfg, show); + } hDCprinter = GetDefaultPrinterDC(); return 1; } @@ -318,7 +326,8 @@ int IsPrinterAvailable(void) /** * FIXME */ -const char *SurfacePrint(SDL_Window * window, SDL_Surface * surf, const char *printcfg, int showdialog) +const char *SurfacePrint(SDL_Window * window, SDL_Surface * surf, + const char *printcfg, int showdialog) { const char *res = NULL; HWND hWnd; @@ -342,10 +351,10 @@ const char *SurfacePrint(SDL_Window * window, SDL_Surface * surf, const char *pr hWnd = wminfo.info.win.window; if (!GetPrinterDC(hWnd, printcfg, showdialog)) - { - ShowWindow(hWnd, SW_SHOWNORMAL); - return NULL; - } + { + ShowWindow(hWnd, SW_SHOWNORMAL); + return NULL; + } if (!hDCprinter) return "win32_print: GetPrinterDC() failed."; @@ -360,26 +369,26 @@ const char *SurfacePrint(SDL_Window * window, SDL_Surface * surf, const char *pr nError = StartDoc(hDCprinter, &di); if (nError == SP_ERROR) - { - res = "win32_print: StartDoc() failed."; - goto error; - } + { + res = "win32_print: StartDoc() failed."; + goto error; + } nError = StartPage(hDCprinter); if (nError <= 0) - { - res = "win32_print: StartPage() failed."; - goto error; - } + { + res = "win32_print: StartPage() failed."; + goto error; + } ////////////////////////////////////////////////////////////////////////////////////// surf24 = make24bitDIB(surf); if (!surf24) - { - res = "win32_print: make24bitDIB() failed."; - goto error; - } + { + res = "win32_print: make24bitDIB() failed."; + goto error; + } memset(&bmih, 0, sizeof(bmih)); bmih.biSize = sizeof(bmih); @@ -395,100 +404,100 @@ const char *SurfacePrint(SDL_Window * window, SDL_Surface * surf, const char *pr sY = GetDeviceCaps(hDCprinter, LOGPIXELSY); switch (scaling) + { + case STRETCH_TO_FIT: { - case STRETCH_TO_FIT: - { - /* stretches x and y dimensions independently to fit the page */ - /* doesn't preserve image aspect-ratio */ - rcDst.top = 0; - rcDst.left = 0; - rcDst.bottom = pageHeight; - rcDst.right = pageWidth; - break; - } - case SCALE_TO_FIT: - { - /* maximises image size on the page */ - /* preserves aspect-ratio, alignment is top and center */ - int width = bmih.biWidth; - int height = bmih.biHeight; - - if (width < pageWidth && height < pageHeight) - { - float dW = (float)pageWidth / width; - float dH = (float)pageHeight / height; - - if (dW < dH) - { - width = pageWidth; - height = (int)((height * dW * (sY / sX)) + 0.5f); - } - else - { - width = (int)((width * dH * (sX / sY)) + 0.5f); - height = pageHeight; - } - } - if (width > pageWidth) - { - height = height * width / pageWidth; - width = pageWidth; - } - if (height > pageHeight) - { - width = width * height / pageHeight; - height = pageHeight; - } - - rcDst.top = 0; - rcDst.left = (pageWidth - width) / 2; - rcDst.bottom = rcDst.top + height; - rcDst.right = rcDst.left + width; - break; - } - default: - res = "win32_print: invalid scaling option."; - goto error; + /* stretches x and y dimensions independently to fit the page */ + /* doesn't preserve image aspect-ratio */ + rcDst.top = 0; + rcDst.left = 0; + rcDst.bottom = pageHeight; + rcDst.right = pageWidth; + break; } + case SCALE_TO_FIT: + { + /* maximises image size on the page */ + /* preserves aspect-ratio, alignment is top and center */ + int width = bmih.biWidth; + int height = bmih.biHeight; + + if (width < pageWidth && height < pageHeight) + { + float dW = (float) pageWidth / width; + float dH = (float) pageHeight / height; + + if (dW < dH) + { + width = pageWidth; + height = (int) ((height * dW * (sY / sX)) + 0.5f); + } + else + { + width = (int) ((width * dH * (sX / sY)) + 0.5f); + height = pageHeight; + } + } + if (width > pageWidth) + { + height = height * width / pageWidth; + width = pageWidth; + } + if (height > pageHeight) + { + width = width * height / pageHeight; + height = pageHeight; + } + + rcDst.top = 0; + rcDst.left = (pageWidth - width) / 2; + rcDst.bottom = rcDst.top + height; + rcDst.right = rcDst.left + width; + break; + } + default: + res = "win32_print: invalid scaling option."; + goto error; + } hDCCaps = GetDeviceCaps(hDCprinter, RASTERCAPS); if (hDCCaps & RC_PALETTE) - { - res = "win32_print: printer context requires palette."; - goto error; - } + { + res = "win32_print: printer context requires palette."; + goto error; + } if (hDCCaps & RC_STRETCHDIB) - { - SetStretchBltMode(hDCprinter, COLORONCOLOR); + { + SetStretchBltMode(hDCprinter, COLORONCOLOR); - bmi.bmiHeader = bmih; - nError = StretchDIBits(hDCprinter, rcDst.left, rcDst.top, - rcDst.right - rcDst.left, - rcDst.bottom - rcDst.top, - 0, 0, bmih.biWidth, bmih.biHeight, - surf24->pixels, &bmi, DIB_RGB_COLORS, SRCCOPY); - if (nError == (int) GDI_ERROR) - { - res = "win32_print: StretchDIBits() failed."; - goto error; - } - } - else + bmi.bmiHeader = bmih; + nError = StretchDIBits(hDCprinter, rcDst.left, rcDst.top, + rcDst.right - rcDst.left, + rcDst.bottom - rcDst.top, + 0, 0, bmih.biWidth, bmih.biHeight, + surf24->pixels, &bmi, DIB_RGB_COLORS, SRCCOPY); + if (nError == (int) GDI_ERROR) { - res = "win32_print: StretchDIBits() not available."; + res = "win32_print: StretchDIBits() failed."; goto error; } + } + else + { + res = "win32_print: StretchDIBits() not available."; + goto error; + } ////////////////////////////////////////////////////////////////////////////////////// nError = EndPage(hDCprinter); if (nError <= 0) - { - res = "win32_print: EndPage() failed."; - goto error; - } + { + res = "win32_print: EndPage() failed."; + goto error; + } EndDoc(hDCprinter); @@ -514,7 +523,8 @@ error: /* Read access to Windows Registry */ -static HRESULT ReadRegistry(const char *key, const char *option, char *value, int size) +static HRESULT ReadRegistry(const char *key, const char *option, char *value, + int size) { LONG res; HKEY hKey = NULL; @@ -563,17 +573,18 @@ char *GetDefaultSaveDir(const char *suffix) { char prefix[MAX_PATH]; char path[2 * MAX_PATH]; - const char *key = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"; + const char *key = + "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"; const char *option = "AppData"; HRESULT hr = S_OK; if (SUCCEEDED(hr = ReadRegistry(key, option, prefix, sizeof(prefix)))) - { - remove_slash(prefix); - snprintf(path, sizeof(path), "%s/%s", prefix, suffix); - _mkdir(path); - return strdup(path); - } + { + remove_slash(prefix); + snprintf(path, sizeof(path), "%s/%s", prefix, suffix); + _mkdir(path); + return strdup(path); + } return strdup("userdata"); } @@ -587,15 +598,16 @@ char *GetDefaultSaveDir(const char *suffix) char *GetSystemFontDir(void) { char path[MAX_PATH]; - const char *key = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"; + const char *key = + "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"; const char *option = "Fonts"; HRESULT hr = S_OK; if (SUCCEEDED(hr = ReadRegistry(key, option, path, sizeof(path)))) - { - remove_slash(path); - return strdup(path); - } + { + remove_slash(path); + return strdup(path); + } return strdup("C:\\WINDOWS\\FONTS"); } @@ -609,15 +621,16 @@ char *GetUserImageDir(void); char *GetUserImageDir(void) { char path[MAX_PATH]; - const char *key = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"; + const char *key = + "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"; const char *option = "My Pictures"; HRESULT hr = S_OK; - + if (SUCCEEDED(hr = ReadRegistry(key, option, path, sizeof(path)))) - { - remove_slash(path); - return strdup(path); - } + { + remove_slash(path); + return strdup(path); + } return strdup("C:\\Pictures"); } @@ -630,9 +643,9 @@ static char *GetUserTempDir(void) const char *temp = getenv("TEMP"); if (!temp) - { - temp = "userdata"; - } + { + temp = "userdata"; + } return strdup(temp); } @@ -662,7 +675,8 @@ static int g_bWindowActive = 0; /** * FIXME */ -LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam); +LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, + LPARAM lParam); LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { int bEatKeystroke = 0; @@ -672,14 +686,15 @@ LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) return CallNextHookEx(g_hKeyboardHook, nCode, wParam, lParam); switch (wParam) + { + case WM_KEYDOWN: + case WM_KEYUP: { - case WM_KEYDOWN: - case WM_KEYUP: - { - bEatKeystroke = g_bWindowActive && ((p->vkCode == VK_LWIN) || (p->vkCode == VK_RWIN)); - break; - } + bEatKeystroke = g_bWindowActive && ((p->vkCode == VK_LWIN) + || (p->vkCode == VK_RWIN)); + break; } + } if (bEatKeystroke) return 1; @@ -693,7 +708,9 @@ int InstallKeyboardHook(void) { if (g_hKeyboardHook) return -1; - g_hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandle(NULL), 0); + g_hKeyboardHook = + SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, + GetModuleHandle(NULL), 0); return g_hKeyboardHook ? 0 : -2; } diff --git a/src/win32_print.h b/src/win32_print.h index 5d1dd57a6..f24fd935c 100644 --- a/src/win32_print.h +++ b/src/win32_print.h @@ -15,7 +15,8 @@ #endif /* if printcfg is NULL, uses the default printer */ -extern const char *SurfacePrint(SDL_Window * window, SDL_Surface * surf, const char *printcfg, int showdialog); +extern const char *SurfacePrint(SDL_Window * window, SDL_Surface * surf, + const char *printcfg, int showdialog); extern int IsPrinterAvailable(void); /* additional windows functions requiring */ diff --git a/src/win32_trash.c b/src/win32_trash.c index 46e15cbd6..5655a0011 100644 --- a/src/win32_trash.c +++ b/src/win32_trash.c @@ -1,23 +1,26 @@ #include #include -int MoveFileToRecycleBin(const TCHAR *fullPathName); +int MoveFileToRecycleBin(const TCHAR * fullPathName); int win32_trash(const char *path); -int MoveFileToRecycleBin(const TCHAR *fullPathName) +int MoveFileToRecycleBin(const TCHAR * fullPathName) { SHFILEOPSTRUCT fileOp; const TCHAR *src = fullPathName; TCHAR *dest; fileOp.pFrom = dest = alloca(sizeof(*dest) * (_tcslen(fullPathName) + 2)); - while((*dest++ = *src++) != _T('\0')) {} + while ((*dest++ = *src++) != _T('\0')) + { + } *dest = _T('\0'); fileOp.hwnd = NULL; fileOp.wFunc = FO_DELETE; fileOp.pTo = NULL; - fileOp.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT | FOF_ALLOWUNDO; + fileOp.fFlags = + FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT | FOF_ALLOWUNDO; return SHFileOperation(&fileOp); } @@ -26,8 +29,10 @@ int win32_trash(const char *path) char *p, *src; src = p = strdup(path); - while(*p != '\0'){ - if (*p == '/') *p = '\\'; + while (*p != '\0') + { + if (*p == '/') + *p = '\\'; p++; } return MoveFileToRecycleBin(src);