diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..5347b89c7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +*.o +*.so +tuxpaint +obj/parse.c +obj/parse_step1.c +src/tp_magic_api.h +tp-magic-config +trans/ +*~ diff --git a/docs/CHANGES.txt b/docs/CHANGES.txt index c2748b2ed..016638f90 100644 --- a/docs/CHANGES.txt +++ b/docs/CHANGES.txt @@ -10,6 +10,7 @@ $Id$ 2017.xxx.x (0.9.23) * New tools + --------- * Color selector Pere Pujal i Carabantes @@ -102,6 +103,11 @@ $Id$ the program will crash (SF.net Bug #210) + * Other Improvements: + ------------------- + * Reformatted source code via GNU Indent tool; should + greatly increase code readability and formatting consistency. + 2014.August.5 (0.9.22) * New Tools: diff --git a/dummy.c b/dummy.c index 564814cf4..311857d57 100644 --- a/dummy.c +++ b/dummy.c @@ -11,9 +11,10 @@ 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){ - return 0; +int main(int argc UNUSED, char *argv[]UNUSED) +{ + return 0; } diff --git a/macosx/.indent.pro b/macosx/.indent.pro new file mode 120000 index 000000000..c1779574c --- /dev/null +++ b/macosx/.indent.pro @@ -0,0 +1 @@ +../src/.indent.pro \ No newline at end of file diff --git a/macosx/SDLMain.h b/macosx/SDLMain.h index 2cfb43ac4..025fbc8f8 100755 --- a/macosx/SDLMain.h +++ b/macosx/SDLMain.h @@ -12,34 +12,37 @@ #import #import "TransparentTextView.h" -@interface SDLMain : NSObject +@ interface SDLMain:NSObject { - IBOutlet NSWindow *messagePanel; - IBOutlet NSTextField *messageText; - IBOutlet NSTextField *messageStatus; - IBOutlet NSProgressIndicator *messageProgress; - IBOutlet NSWindow *aboutWindow; - IBOutlet NSTextField *appnameText; - IBOutlet NSTextField *versionText; - IBOutlet TransparentTextView *acknowledgmentsText; + IBOutlet NSWindow *messagePanel; + IBOutlet NSTextField *messageText; + IBOutlet NSTextField *messageStatus; + IBOutlet NSProgressIndicator *messageProgress; + IBOutlet NSWindow *aboutWindow; + IBOutlet NSTextField *appnameText; + IBOutlet NSTextField *versionText; + IBOutlet TransparentTextView *acknowledgmentsText; } -- (IBAction)onAbout:(id)sender; -- (IBAction)onNew:(id)sender; -- (IBAction)onOpen:(id)sender; -- (IBAction)onSave:(id)sender; -- (IBAction)onPrint:(id)sender; -- (IBAction)onPageSetup:(id)sender; -- (IBAction)onUndo:(id)sender; -- (IBAction)onRedo:(id)sender; -- (IBAction)onHelp:(id)sender; -- (IBAction)onQuit:(id)sender; +-(IBAction) onAbout:(id) sender; +-(IBAction) onNew:(id) sender; +-(IBAction) onOpen:(id) sender; +-(IBAction) onSave:(id) sender; +-(IBAction) onPrint:(id) sender; +-(IBAction) onPageSetup:(id) sender; +-(IBAction) onUndo:(id) sender; +-(IBAction) onRedo:(id) sender; +-(IBAction) onHelp:(id) sender; +-(IBAction) onQuit:(id) sender; -- (void) sendSDLControlKeystroke:(int)key; -- (void) sendSDLControlShiftKeystroke:(int)key; -- (void) setupBridge; +-(void)sendSDLControlKeystroke:(int)key; +-(void)sendSDLControlShiftKeystroke:(int)key; +-(void)setupBridge; -- (void) displayMessage:(NSString*)message andStatus:(NSString*)status withProgressIndicator:(BOOL)progress; -- (void) hideMessage; +-(void)displayMessage:(NSString *) + message andStatus:(NSString *) + status withProgressIndicator:(BOOL) progress; + +-(void)hideMessage; @end diff --git a/macosx/message.h b/macosx/message.h index 4c0142b5e..687fc0369 100644 --- a/macosx/message.h +++ b/macosx/message.h @@ -23,5 +23,5 @@ #define MSG_FONT_CACHE 1 -void displayMessage( int msgId ); +void displayMessage(int msgId); void hideMessage(); diff --git a/macosx/patch.c b/macosx/patch.c index 3ff1498c5..86256628b 100644 --- a/macosx/patch.c +++ b/macosx/patch.c @@ -17,42 +17,42 @@ // missing from wchar.h on Mac -wchar_t* wcsdup(const wchar_t* ws) +wchar_t *wcsdup(const wchar_t * ws) { - wchar_t *ret; - size_t len; - - len = wcslen (ws); - ret = malloc ((len + 1) * sizeof (wchar_t)); - if (ret == 0) - return ret; - return (wcscpy (ret, ws)); + wchar_t *ret; + size_t len; + + len = wcslen(ws); + ret = malloc((len + 1) * sizeof(wchar_t)); + if (ret == 0) + return ret; + return (wcscpy(ret, ws)); } // missing, needed by __nl_find_msg in libintl.a(dcigettext.o) // http://forums.macrumors.com/showthread.php?t=1284479 #undef iconv_t -typedef void* iconv_t; -extern size_t libiconv(iconv_t cd, char* * inbuf, size_t *inbytesleft, char* * outbuf, size_t *outbytesleft); -extern iconv_t libiconv_open(const char* tocode, const char* fromcode); +typedef void *iconv_t; +extern size_t libiconv(iconv_t cd, char * *inbuf, size_t * inbytesleft, char * *outbuf, size_t * outbytesleft); +extern iconv_t libiconv_open(const char *tocode, const char *fromcode); -size_t iconv(iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft) +size_t iconv(iconv_t cd, char **inbuf, size_t * inbytesleft, char **outbuf, size_t * outbytesleft) { - return libiconv(cd, inbuf, inbytesleft, outbuf, outbytesleft); + return libiconv(cd, inbuf, inbytesleft, outbuf, outbytesleft); } iconv_t iconv_open(const char *tocode, const char *fromcode) { - return libiconv_open(tocode, fromcode); + return libiconv_open(tocode, fromcode); } // to fix ineffective setlocale() in i18n.c or force language to Inuktitut // must be called prior to setup_i18n() -patch_i18n(const char* locale) +patch_i18n(const char *locale) { - setenv("LANG", locale, 1); // takes language passed as an argument + setenv("LANG", locale, 1); // takes language passed as an argument // setenv("LANG", "iu_CA.UTF-8", 1); // forces language to Inuktitut } @@ -62,282 +62,283 @@ patch_i18n(const char* locale) // http://www.publicsource.apple.com/source/clang/clang-137/src/projects/compiler-rt/lib/int_lib.h #include typedef unsigned su_int; -typedef long long di_int; +typedef long long di_int; typedef unsigned long long du_int; + #ifdef __i386__ -typedef int ti_int __attribute__ ((mode (DI))); -typedef unsigned tu_int __attribute__ ((mode (DI))); +typedef int ti_int __attribute__ ((mode(DI))); +typedef unsigned tu_int __attribute__ ((mode(DI))); #else -typedef int ti_int __attribute__ ((mode (TI))); -typedef unsigned tu_int __attribute__ ((mode (TI))); +typedef int ti_int __attribute__ ((mode(TI))); +typedef unsigned tu_int __attribute__ ((mode(TI))); #endif typedef union { - tu_int all; - struct - { + tu_int all; + struct + { #if _YUGA_LITTLE_ENDIAN - du_int low; - du_int high; + du_int low; + du_int high; #else - du_int high; - du_int low; -#endif /* _YUGA_LITTLE_ENDIAN */ - }s; + du_int high; + du_int low; +#endif /* _YUGA_LITTLE_ENDIAN */ + } s; } utwords; // missing, needed by __umodti3 and __udivti3 // http://www.publicsource.apple.com/source/clang/clang-137/src/projects/compiler-rt/lib/udivmodti4.c -tu_int __udivmodti4(tu_int a, tu_int b, tu_int* rem) +tu_int __udivmodti4(tu_int a, tu_int b, tu_int * rem) { - const unsigned n_udword_bits = sizeof(du_int) * CHAR_BIT; - const unsigned n_utword_bits = sizeof(tu_int) * CHAR_BIT; - utwords n; - n.all = a; - utwords d; - d.all = b; - utwords q; - utwords r; - unsigned sr; - /* special cases, X is unknown, K != 0 */ - if (n.s.high == 0) + const unsigned n_udword_bits = sizeof(du_int) * CHAR_BIT; + const unsigned n_utword_bits = sizeof(tu_int) * CHAR_BIT; + utwords n; + + n.all = a; + utwords d; + + d.all = b; + utwords q; + utwords r; + unsigned sr; + + /* special cases, X is unknown, K != 0 */ + if (n.s.high == 0) { - if (d.s.high == 0) + if (d.s.high == 0) { - /* 0 X - * --- - * 0 X - */ - if (rem) - *rem = n.s.low % d.s.low; - return n.s.low / d.s.low; + /* 0 X + * --- + * 0 X + */ + if (rem) + *rem = n.s.low % d.s.low; + return n.s.low / d.s.low; } - /* 0 X - * --- - * K X - */ - if (rem) - *rem = n.s.low; - return 0; + /* 0 X + * --- + * K X + */ + if (rem) + *rem = n.s.low; + return 0; } - /* n.s.high != 0 */ - if (d.s.low == 0) + /* n.s.high != 0 */ + if (d.s.low == 0) { - if (d.s.high == 0) + if (d.s.high == 0) { - /* K X - * --- - * 0 0 - */ - if (rem) - *rem = n.s.high % d.s.low; - return n.s.high / d.s.low; + /* K X + * --- + * 0 0 + */ + if (rem) + *rem = n.s.high % d.s.low; + return n.s.high / d.s.low; } - /* d.s.high != 0 */ - if (n.s.low == 0) + /* d.s.high != 0 */ + if (n.s.low == 0) { - /* K 0 - * --- - * K 0 - */ - if (rem) + /* K 0 + * --- + * K 0 + */ + if (rem) { - r.s.high = n.s.high % d.s.high; - r.s.low = 0; - *rem = r.all; + r.s.high = n.s.high % d.s.high; + r.s.low = 0; + *rem = r.all; } - return n.s.high / d.s.high; + return n.s.high / d.s.high; } - /* K K - * --- - * K 0 - */ - if ((d.s.high & (d.s.high - 1)) == 0) /* if d is a power of 2 */ + /* K K + * --- + * K 0 + */ + if ((d.s.high & (d.s.high - 1)) == 0) /* if d is a power of 2 */ { - if (rem) + if (rem) { - r.s.low = n.s.low; - r.s.high = n.s.high & (d.s.high - 1); - *rem = r.all; + r.s.low = n.s.low; + r.s.high = n.s.high & (d.s.high - 1); + *rem = r.all; } - return n.s.high >> __builtin_ctzll(d.s.high); + return n.s.high >> __builtin_ctzll(d.s.high); } - /* K K - * --- - * K 0 - */ - sr = __builtin_clzll(d.s.high) - __builtin_clzll(n.s.high); - /* 0 <= sr <= n_udword_bits - 2 or sr large */ - if (sr > n_udword_bits - 2) + /* K K + * --- + * K 0 + */ + sr = __builtin_clzll(d.s.high) - __builtin_clzll(n.s.high); + /* 0 <= sr <= n_udword_bits - 2 or sr large */ + if (sr > n_udword_bits - 2) { - if (rem) + if (rem) + *rem = n.all; + return 0; + } + ++sr; + /* 1 <= sr <= n_udword_bits - 1 */ + /* q.all = n.all << (n_utword_bits - sr); */ + q.s.low = 0; + q.s.high = n.s.low << (n_udword_bits - sr); + /* r.all = n.all >> sr; */ + r.s.high = n.s.high >> sr; + r.s.low = (n.s.high << (n_udword_bits - sr)) | (n.s.low >> sr); + } + else /* d.s.low != 0 */ + { + if (d.s.high == 0) + { + /* K X + * --- + * 0 K + */ + if ((d.s.low & (d.s.low - 1)) == 0) /* if d is a power of 2 */ + { + if (rem) + *rem = n.s.low & (d.s.low - 1); + if (d.s.low == 1) + return n.all; + unsigned sr = __builtin_ctzll(d.s.low); + + q.s.high = n.s.high >> sr; + q.s.low = (n.s.high << (n_udword_bits - sr)) | (n.s.low >> sr); + return q.all; + } + /* K X + * --- + * 0 K + */ + sr = 1 + n_udword_bits + __builtin_clzll(d.s.low) - __builtin_clzll(n.s.high); + /* 2 <= sr <= n_utword_bits - 1 + * q.all = n.all << (n_utword_bits - sr); + * r.all = n.all >> sr; + * if (sr == n_udword_bits) + * { + * q.s.low = 0; + * q.s.high = n.s.low; + * r.s.high = 0; + * r.s.low = n.s.high; + * } + * else if (sr < n_udword_bits) // 2 <= sr <= n_udword_bits - 1 + * { + * q.s.low = 0; + * q.s.high = n.s.low << (n_udword_bits - sr); + * r.s.high = n.s.high >> sr; + * r.s.low = (n.s.high << (n_udword_bits - sr)) | (n.s.low >> sr); + * } + * else // n_udword_bits + 1 <= sr <= n_utword_bits - 1 + * { + * q.s.low = n.s.low << (n_utword_bits - sr); + * q.s.high = (n.s.high << (n_utword_bits - sr)) | + * (n.s.low >> (sr - n_udword_bits)); + * r.s.high = 0; + * r.s.low = n.s.high >> (sr - n_udword_bits); + * } + */ + q.s.low = (n.s.low << (n_utword_bits - sr)) & ((di_int) (int)(n_udword_bits - sr) >> (n_udword_bits - 1)); + q.s.high = ((n.s.low << (n_udword_bits - sr)) & + ((di_int) (int)(sr - n_udword_bits - 1) >> (n_udword_bits - 1))) | + (((n.s.high << (n_utword_bits - sr)) | + (n.s.low >> (sr - n_udword_bits))) & ((di_int) (int)(n_udword_bits - sr) >> (n_udword_bits - 1))); + r.s.high = (n.s.high >> sr) & ((di_int) (int)(sr - n_udword_bits) >> (n_udword_bits - 1)); + r.s.low = ((n.s.high >> (sr - n_udword_bits)) & + ((di_int) (int)(n_udword_bits - sr - 1) >> (n_udword_bits - 1))) | + (((n.s.high << (n_udword_bits - sr)) | + (n.s.low >> sr)) & ((di_int) (int)(sr - n_udword_bits) >> (n_udword_bits - 1))); + } + else + { + /* K X + * --- + * K K + */ + sr = __builtin_clzll(d.s.high) - __builtin_clzll(n.s.high); + /*0 <= sr <= n_udword_bits - 1 or sr large */ + if (sr > n_udword_bits - 1) + { + if (rem) *rem = n.all; - return 0; - } - ++sr; - /* 1 <= sr <= n_udword_bits - 1 */ - /* q.all = n.all << (n_utword_bits - sr); */ - q.s.low = 0; - q.s.high = n.s.low << (n_udword_bits - sr); - /* r.all = n.all >> sr; */ - r.s.high = n.s.high >> sr; - r.s.low = (n.s.high << (n_udword_bits - sr)) | (n.s.low >> sr); - } - else /* d.s.low != 0 */ - { - if (d.s.high == 0) - { - /* K X - * --- - * 0 K - */ - if ((d.s.low & (d.s.low - 1)) == 0) /* if d is a power of 2 */ - { - if (rem) - *rem = n.s.low & (d.s.low - 1); - if (d.s.low == 1) - return n.all; - unsigned sr = __builtin_ctzll(d.s.low); - q.s.high = n.s.high >> sr; - q.s.low = (n.s.high << (n_udword_bits - sr)) | (n.s.low >> sr); - return q.all; + return 0; } - /* K X - * --- - * 0 K - */ - sr = 1 + n_udword_bits + __builtin_clzll(d.s.low) - - __builtin_clzll(n.s.high); - /* 2 <= sr <= n_utword_bits - 1 - * q.all = n.all << (n_utword_bits - sr); - * r.all = n.all >> sr; - * if (sr == n_udword_bits) - * { - * q.s.low = 0; - * q.s.high = n.s.low; - * r.s.high = 0; - * r.s.low = n.s.high; - * } - * else if (sr < n_udword_bits) // 2 <= sr <= n_udword_bits - 1 - * { - * q.s.low = 0; - * q.s.high = n.s.low << (n_udword_bits - sr); - * r.s.high = n.s.high >> sr; - * r.s.low = (n.s.high << (n_udword_bits - sr)) | (n.s.low >> sr); - * } - * else // n_udword_bits + 1 <= sr <= n_utword_bits - 1 - * { - * q.s.low = n.s.low << (n_utword_bits - sr); - * q.s.high = (n.s.high << (n_utword_bits - sr)) | - * (n.s.low >> (sr - n_udword_bits)); - * r.s.high = 0; - * r.s.low = n.s.high >> (sr - n_udword_bits); - * } - */ - q.s.low = (n.s.low << (n_utword_bits - sr)) & - ((di_int)(int)(n_udword_bits - sr) >> (n_udword_bits-1)); - q.s.high = ((n.s.low << ( n_udword_bits - sr)) & - ((di_int)(int)(sr - n_udword_bits - 1) >> (n_udword_bits-1))) | - (((n.s.high << (n_utword_bits - sr)) | - (n.s.low >> (sr - n_udword_bits))) & - ((di_int)(int)(n_udword_bits - sr) >> (n_udword_bits-1))); - r.s.high = (n.s.high >> sr) & - ((di_int)(int)(sr - n_udword_bits) >> (n_udword_bits-1)); - r.s.low = ((n.s.high >> (sr - n_udword_bits)) & - ((di_int)(int)(n_udword_bits - sr - 1) >> (n_udword_bits-1))) | - (((n.s.high << (n_udword_bits - sr)) | - (n.s.low >> sr)) & - ((di_int)(int)(sr - n_udword_bits) >> (n_udword_bits-1))); - } - else - { - /* K X - * --- - * K K - */ - sr = __builtin_clzll(d.s.high) - __builtin_clzll(n.s.high); - /*0 <= sr <= n_udword_bits - 1 or sr large */ - if (sr > n_udword_bits - 1) - { - if (rem) - *rem = n.all; - return 0; - } - ++sr; - /* 1 <= sr <= n_udword_bits */ - /* q.all = n.all << (n_utword_bits - sr); */ - q.s.low = 0; - q.s.high = n.s.low << (n_udword_bits - sr); - /* r.all = n.all >> sr; - * if (sr < n_udword_bits) - * { - * r.s.high = n.s.high >> sr; - * r.s.low = (n.s.high << (n_udword_bits - sr)) | (n.s.low >> sr); - * } - * else - * { - * r.s.high = 0; - * r.s.low = n.s.high; - * } - */ - r.s.high = (n.s.high >> sr) & - ((di_int)(int)(sr - n_udword_bits) >> (n_udword_bits-1)); - r.s.low = (n.s.high << (n_udword_bits - sr)) | - ((n.s.low >> sr) & - ((di_int)(int)(sr - n_udword_bits) >> (n_udword_bits-1))); + ++sr; + /* 1 <= sr <= n_udword_bits */ + /* q.all = n.all << (n_utword_bits - sr); */ + q.s.low = 0; + q.s.high = n.s.low << (n_udword_bits - sr); + /* r.all = n.all >> sr; + * if (sr < n_udword_bits) + * { + * r.s.high = n.s.high >> sr; + * r.s.low = (n.s.high << (n_udword_bits - sr)) | (n.s.low >> sr); + * } + * else + * { + * r.s.high = 0; + * r.s.low = n.s.high; + * } + */ + r.s.high = (n.s.high >> sr) & ((di_int) (int)(sr - n_udword_bits) >> (n_udword_bits - 1)); + r.s.low = (n.s.high << (n_udword_bits - sr)) | + ((n.s.low >> sr) & ((di_int) (int)(sr - n_udword_bits) >> (n_udword_bits - 1))); } } - /* Not a special case - * q and r are initialized with: - * q.all = n.all << (n_utword_bits - sr); - * r.all = n.all >> sr; - * 1 <= sr <= n_utword_bits - 1 - */ - su_int carry = 0; - for (; sr > 0; --sr) + /* Not a special case + * q and r are initialized with: + * q.all = n.all << (n_utword_bits - sr); + * r.all = n.all >> sr; + * 1 <= sr <= n_utword_bits - 1 + */ + su_int carry = 0; + + for (; sr > 0; --sr) { - /* r:q = ((r:q) << 1) | carry */ - r.s.high = (r.s.high << 1) | (r.s.low >> (n_udword_bits - 1)); - r.s.low = (r.s.low << 1) | (q.s.high >> (n_udword_bits - 1)); - q.s.high = (q.s.high << 1) | (q.s.low >> (n_udword_bits - 1)); - q.s.low = (q.s.low << 1) | carry; - /* carry = 0; - * if (r.all >= d.all) - * { - * r.all -= d.all; - * carry = 1; - * } - */ - const ti_int s = (ti_int)(d.all - r.all - 1) >> (n_utword_bits - 1); - carry = s & 1; - r.all -= d.all & s; + /* r:q = ((r:q) << 1) | carry */ + r.s.high = (r.s.high << 1) | (r.s.low >> (n_udword_bits - 1)); + r.s.low = (r.s.low << 1) | (q.s.high >> (n_udword_bits - 1)); + q.s.high = (q.s.high << 1) | (q.s.low >> (n_udword_bits - 1)); + q.s.low = (q.s.low << 1) | carry; + /* carry = 0; + * if (r.all >= d.all) + * { + * r.all -= d.all; + * carry = 1; + * } + */ + const ti_int s = (ti_int) (d.all - r.all - 1) >> (n_utword_bits - 1); + + carry = s & 1; + r.all -= d.all & s; } - q.all = (q.all << 1) | carry; - if (rem) - *rem = r.all; - return q.all; + q.all = (q.all << 1) | carry; + if (rem) + *rem = r.all; + return q.all; } // missing, needed by __cairo_uint128_divrem in libcairo.a(cairo-wideint.o) // http://www.publicsource.apple.com/source/clang/clang-137/src/projects/compiler-rt/lib/umodti3.c tu_int __umodti3(tu_int a, tu_int b) { - tu_int r; - __udivmodti4(a, b, &r); - return r; + tu_int r; + + __udivmodti4(a, b, &r); + return r; } // missing, needed by __cairo_uint128_divrem in libcairo.a(cairo-wideint.o) // http://www.publicsource.apple.com/source/clang/clang-137/src/projects/compiler-rt/lib/udivti3.c tu_int __udivti3(tu_int a, tu_int b) { - return __udivmodti4(a, b, 0); + return __udivmodti4(a, b, 0); } // missing, needed by __nl_log_untranslated in libintl.a(log.o) for 10.5 -FILE* fopen$DARWIN_EXTSN(const char *filename, const char *mode) +FILE *fopen$DARWIN_EXTSN(const char *filename, const char *mode) { - return fopen(filename, mode); + return fopen(filename, mode); } // missing, needed by _slab_allocator_alloc_chunk in libglib-2.0.a(libglib_2_0_la-gslice.o) for 10.5 @@ -345,21 +346,20 @@ FILE* fopen$DARWIN_EXTSN(const char *filename, const char *mode) // http://www.publicsource.apple.com/source/clang/clang-137/src/tools/clang/lib/Headers/mm_malloc.h int posix_memalign(void **ptr, size_t align, size_t size) { - if (ptr) - { - *ptr = _mm_malloc (size, align); - return 0; - } + if (ptr) + { + *ptr = _mm_malloc(size, align); + return 0; + } - return ENOMEM; + return ENOMEM; } -#endif // PATCH_10_5 +#endif // PATCH_10_5 #ifdef PATCH_LIBPNG_EARLIER_THAN_1_5 // missing in libpng<1.5, needed by _Load_SBit_Png in libfreetype.a(sfnt.o), _error_callback in libfreetype.a(sfnt.o) // http://stackoverflow.com/questions/5190554/unresolved-external-png-set-longjmp-fn-in-libpng // http://cpansearch.perl.org/src/JTPALMER/Alien-SDL-1.439_1/patches/SDL_image-1.2.10-libpng15.patch -typedef jmp_buf* (*png_set_longjmp_fnPtr)(png_structp, void*, size_t); -png_set_longjmp_fnPtr png_set_longjmp_fn = 0; +typedef jmp_buf *(*png_set_longjmp_fnPtr) (png_structp, void *, size_t); +png_set_longjmp_fnPtr png_set_longjmp_fn = 0; #endif - diff --git a/macosx/patch.h b/macosx/patch.h index c2762e9a2..b172899f1 100644 --- a/macosx/patch.h +++ b/macosx/patch.h @@ -11,4 +11,4 @@ #include -wchar_t* wcsdup(const wchar_t*); \ No newline at end of file +wchar_t *wcsdup(const wchar_t *); diff --git a/macosx/speech.h b/macosx/speech.h index 64e6e6164..3798e09c8 100644 --- a/macosx/speech.h +++ b/macosx/speech.h @@ -21,5 +21,4 @@ // (See COPYING.txt) // -void speak_string(const wchar_t *str); - +void speak_string(const wchar_t * str); diff --git a/macosx/wrapperdata.h b/macosx/wrapperdata.h index c9050f2d3..b833c4930 100644 --- a/macosx/wrapperdata.h +++ b/macosx/wrapperdata.h @@ -14,15 +14,15 @@ struct WrapperDataStruct { - char dataPath[2048]; // path to data folder inside Tux Paint application bundle - char preferencesPath[2048]; // path to the user's Tux Paint preferences folder - char globalPreferencesPath[2048]; // path to all users' Tux Paint preferences folder - char fontsPath[2048]; // path to the user's fonts folder - int foundSDL; // was SDL.framework found? - int foundSDL_image; // was SDL_image.framework found? - int foundSDL_mixer; // was SDL_mixer.framework found? - int cocoaKeystrokes; // should keystrokes be intercepted by Cocoa wrapper? - int menuAction; // was the action initiated by a Mac OS X menu selection? + char dataPath[2048]; // path to data folder inside Tux Paint application bundle + char preferencesPath[2048]; // path to the user's Tux Paint preferences folder + char globalPreferencesPath[2048]; // path to all users' Tux Paint preferences folder + char fontsPath[2048]; // path to the user's fonts folder + int foundSDL; // was SDL.framework found? + int foundSDL_image; // was SDL_image.framework found? + int foundSDL_mixer; // was SDL_mixer.framework found? + int cocoaKeystrokes; // should keystrokes be intercepted by Cocoa wrapper? + int menuAction; // was the action initiated by a Mac OS X menu selection? }; typedef struct WrapperDataStruct WrapperData; diff --git a/magic/docs/tp_magic_example.c b/magic/docs/tp_magic_example.c index 415b9693a..44bcacae4 100644 --- a/magic/docs/tp_magic_example.c +++ b/magic/docs/tp_magic_example.c @@ -9,12 +9,12 @@ /* -------------------------- */ #include -#include // For "strdup()" -#include // For "gettext()" +#include // For "strdup()" +#include // For "gettext()" -#include "tp_magic_api.h" // Tux Paint "Magic" tool API header -#include "SDL_image.h" // For IMG_Load(), to load our PNG icon -#include "SDL_mixer.h" // For Mix_LoadWAV(), to load our sound effects +#include "tp_magic_api.h" // Tux Paint "Magic" tool API header +#include "SDL_image.h" // For IMG_Load(), to load our PNG icon +#include "SDL_mixer.h" // For Mix_LoadWAV(), to load our sound effects /* Tool Enumerations: */ @@ -22,21 +22,22 @@ /* What tools we contain: */ -enum { - TOOL_ONE, // Becomes '0' - TOOL_TWO, // Becomes '1' - NUM_TOOLS // Becomes '2' +enum +{ + TOOL_ONE, // Becomes '0' + TOOL_TWO, // Becomes '1' + NUM_TOOLS // Becomes '2' }; /* A list of filenames for sounds and icons to load at startup: */ -const char * snd_filenames[NUM_TOOLS] = { +const char *snd_filenames[NUM_TOOLS] = { "one.wav", "two.wav" }; -const char * icon_filenames[NUM_TOOLS] = { +const char *icon_filenames[NUM_TOOLS] = { "one.png", "two.png" }; @@ -49,7 +50,7 @@ const char * icon_filenames[NUM_TOOLS] = { /* A list of names for the tools */ -const char * names[NUM_TOOLS] = { +const char *names[NUM_TOOLS] = { gettext_noop("A tool"), gettext_noop("Another tool") }; @@ -57,7 +58,7 @@ const char * names[NUM_TOOLS] = { /* A list of descriptions of the tools */ -const char * descs[NUM_TOOLS] = { +const char *descs[NUM_TOOLS] = { gettext_noop("This is example tool number 1."), gettext_noop("This is example tool number 2.") }; @@ -68,7 +69,7 @@ const char * descs[NUM_TOOLS] = { /* --------------------- */ /* Sound effects: */ -Mix_Chunk * snd_effect[NUM_TOOLS]; +Mix_Chunk *snd_effect[NUM_TOOLS]; /* The current color (an "RGB" value) the user has selected in Tux Paint: */ Uint8 example_r, example_g, example_b; @@ -83,12 +84,9 @@ 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: */ @@ -105,7 +103,7 @@ void example_line_callback(void * ptr, int which, Uint32 example_api_version(void) { - return(TP_MAGIC_API_VERSION); + return (TP_MAGIC_API_VERSION); } @@ -129,26 +127,24 @@ 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); + return (1); } @@ -163,7 +159,7 @@ int example_init(magic_api * api) int example_get_tool_count(magic_api * api) { - return(NUM_TOOLS); + return (NUM_TOOLS); } @@ -172,7 +168,7 @@ int example_get_tool_count(magic_api * api) // When Tux Paint is starting up and loading plugins, it asks us to // provide icons for the "Magic" tool buttons. -SDL_Surface * example_get_icon(magic_api * api, int which) +SDL_Surface *example_get_icon(magic_api * api, int which) { char fname[1024]; @@ -186,13 +182,12 @@ 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: - return(IMG_Load(fname)); + return (IMG_Load(fname)); } @@ -201,10 +196,10 @@ SDL_Surface * example_get_icon(magic_api * api, int which) // When Tux Paint is starting up and loading plugins, it asks us to // provide names (labels) for the "Magic" tool buttons. -char * example_get_name(magic_api * api, int which) +char *example_get_name(magic_api * api, int which) { - const char * our_name_english; - const char * our_name_localized; + const char *our_name_english; + const char *our_name_localized; // Get our name from the "names[]" array. // @@ -226,7 +221,7 @@ char * example_get_name(magic_api * api, int which) // send it to Tux Paint. (Tux Paint keeps track of the string and // will free it for us, so we have one less thing to keep track of.) - return(strdup(our_name_localized)); + return (strdup(our_name_localized)); } @@ -235,10 +230,10 @@ char * example_get_name(magic_api * api, int which) // When Tux Paint is starting up and loading plugins, it asks us to // provide names (labels) for the "Magic" tool buttons. -char * example_get_description(magic_api * api, int which, int mode) +char *example_get_description(magic_api * api, int which, int mode) { - const char * our_desc_english; - const char * our_desc_localized; + const char *our_desc_english; + const char *our_desc_localized; // Get our desc from the "descs[]" array. // @@ -260,7 +255,7 @@ char * example_get_description(magic_api * api, int which, int mode) // send it to Tux Paint. (Tux Paint keeps track of the string and // will free it for us, so we have one less thing to keep track of.) - return(strdup(our_desc_localized)); + return (strdup(our_desc_localized)); } // Report whether we accept colors @@ -309,8 +304,7 @@ void example_shutdown(magic_api * api) // Affect the canvas on click: 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 @@ -326,8 +320,7 @@ void example_click(magic_api * api, int which, int mode, // Affect the canvas on drag: 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. // @@ -338,16 +331,27 @@ void 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 // (ox,oy) is always the top left, and (x,y) is always the bottom right, // so the values we put inside "update_rect" make sense: - if (ox > x) { int tmp = ox; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } // Fill in the elements of the "update_rect" SDL_Rect structure @@ -369,17 +373,15 @@ void example_drag(magic_api * api, int which, SDL_Surface * canvas, // (So the sound will pan from speaker to speaker as you drag the // mouse around the canvas!) - api->playsound(snd_effect[which], - (x * 255) / canvas->w, // pan - 255); // distance + api->playsound(snd_effect[which], (x * 255) / canvas->w, // pan + 255); // distance } // Affect the canvas on release: 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... @@ -423,9 +425,7 @@ 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. @@ -436,7 +436,7 @@ void example_line_callback(void * ptr, int which, // // (The "(magic_api *)" casts the generic pointer into the 'type' of // pointer we want, a pointer to a "magic_api".) - magic_api * api = (magic_api *) ptr; + magic_api *api = (magic_api *) ptr; int xx, yy; @@ -445,43 +445,37 @@ void example_line_callback(void * ptr, int which, // 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 - - 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. - } - 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++) { - for (xx = -4; xx < 4; xx++) - { - api->putpixel(canvas, x + xx, y + yy, - api->getpixel(snapshot, - canvas->w - x - xx, - canvas->h - y - yy)); + // Tool number 1 simply draws a single pixel at the (x,y) location. + // It's a 1x1 pixel brush - // 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). - } + 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. + } + 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++) + { + 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'. + + // 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 diff --git a/magic/src/.indent.pro b/magic/src/.indent.pro new file mode 120000 index 000000000..bd43730f5 --- /dev/null +++ b/magic/src/.indent.pro @@ -0,0 +1 @@ +../../src/.indent.pro \ No newline at end of file diff --git a/magic/src/alien.c b/magic/src/alien.c index 9306fb672..360dd57c5 100644 --- a/magic/src/alien.c +++ b/magic/src/alien.c @@ -43,48 +43,50 @@ #define gettext_noop(String) String #endif -static const double alien_ANGLE[] = {0,0,0}; -static const double alien_FREQUENCY[] = {1,1,1}; +static const double alien_ANGLE[] = { 0, 0, 0 }; +static const double alien_FREQUENCY[] = { 1, 1, 1 }; + static const int alien_RADIUS = 16; -enum { - TOOL_alien, - alien_NUM_TOOLS +enum +{ + TOOL_alien, + alien_NUM_TOOLS }; -static Mix_Chunk * alien_snd_effect[alien_NUM_TOOLS]; +static Mix_Chunk *alien_snd_effect[alien_NUM_TOOLS]; -const char * alien_snd_filenames[alien_NUM_TOOLS] = { +const char *alien_snd_filenames[alien_NUM_TOOLS] = { "alien.ogg", }; -const char * alien_icon_filenames[alien_NUM_TOOLS] = { + +const char *alien_icon_filenames[alien_NUM_TOOLS] = { "alien.png", }; -const char * alien_names[alien_NUM_TOOLS] = { + +const char *alien_names[alien_NUM_TOOLS] = { gettext_noop("Color Shift"), }; -const char * alien_descs[alien_NUM_TOOLS][2] = { + +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 to change the colors in your entire picture."),}, + gettext_noop("Click to change the colors in your entire picture."),}, }; // Prototypes Uint32 alien_api_version(void); int alien_init(magic_api * api); int alien_get_tool_count(magic_api * api); -SDL_Surface * alien_get_icon(magic_api * api, int which); -char * alien_get_name(magic_api * api, int which); -char * alien_get_description(magic_api * api, int which, int mode); +SDL_Surface *alien_get_icon(magic_api * api, int which); +char *alien_get_name(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); -Mix_Chunk * magic_loadsound(char* file); + 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); + 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_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); @@ -93,109 +95,134 @@ void alien_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas) int alien_modes(magic_api * api, int which); -Uint32 alien_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 alien_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} //Load sounds -int alien_init(magic_api * api){ +int alien_init(magic_api * api) +{ int i; char fname[1024]; srand(time(0)); - for (i = 0; i < alien_NUM_TOOLS; i++){ - snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, alien_snd_filenames[i]); - alien_snd_effect[i] = Mix_LoadWAV(fname); - } - return(1); + for (i = 0; i < alien_NUM_TOOLS; i++) + { + snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, alien_snd_filenames[i]); + alien_snd_effect[i] = Mix_LoadWAV(fname); + } + return (1); } -int alien_get_tool_count(magic_api * api ATTRIBUTE_UNUSED){ - return(alien_NUM_TOOLS); +int alien_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) +{ + return (alien_NUM_TOOLS); } // Load our icons: -SDL_Surface * alien_get_icon(magic_api * api, int which){ +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]); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } // Return our names, localized: -char * alien_get_name(magic_api * api ATTRIBUTE_UNUSED, int which){ - return(strdup(gettext_noop(alien_names[which]))); +char *alien_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) +{ + return (strdup(gettext_noop(alien_names[which]))); } // Return our descriptions, localized: -char * alien_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode){ - return(strdup(gettext_noop(alien_descs[which][mode-1]))); +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){ - magic_api * api = (magic_api *) ptr; +static void do_alien_pixel(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) +{ + magic_api *api = (magic_api *) ptr; Uint8 temp[3]; double temp2[3]; int k; - SDL_GetRGB(api->getpixel(canvas,x, y), canvas->format, &temp[0], &temp[1], &temp[2]); - for (k =0;k<3;k++){ + 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) +{ - magic_api * api = (magic_api *) ptr; + magic_api *api = (magic_api *) ptr; - int x,y; + int x, y; - for (y = 0; y < last->h; y++){ - for (x=0; x < last->w; x++){ - do_alien_pixel(ptr, which, canvas, last, x, y); - } - } + for (y = 0; y < last->h; y++) + { + for (x = 0; x < last->w; x++) + { + 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; + 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++) { - if (api->in_circle(xx - x, yy - y, alien_RADIUS) && - !api->touched(xx, yy)) - { - do_alien_pixel(api, which, canvas, last, xx, yy); - } + 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); + } + } } - } } // 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){ - - api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, do_alien_brush); + 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->playsound(alien_snd_effect[which], (x * 255) / canvas->w, 255); - if (ox > x) { int tmp = ox; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - alien_RADIUS; update_rect->y = oy - alien_RADIUS; @@ -205,53 +232,60 @@ void alien_drag(magic_api * api, int which, SDL_Surface * canvas, int use_sound = 1; -Mix_Chunk * magic_loadsound(char* file){ - Mix_Chunk * temp; +Mix_Chunk *magic_loadsound(char *file) +{ + Mix_Chunk *temp; - if (!use_sound){ - return (Mix_Chunk*)-1; - } + if (!use_sound) + { + 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); - } + 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); + } } // 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } // No setup happened: void alien_shutdown(magic_api * api ATTRIBUTE_UNUSED) { - //Clean up sounds - int i; - for(i=0; idata_directory); - blind_snd = Mix_LoadWAV(fname); + snprintf(fname, sizeof(fname), "%s/sounds/magic/blind.ogg", api->data_directory); + blind_snd = Mix_LoadWAV(fname); - return(1); + return (1); } int blind_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) @@ -102,24 +102,25 @@ int blind_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) return BLIND_NUMTOOLS; } -SDL_Surface * blind_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *blind_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/blind.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/blind.png", api->data_directory); - return(IMG_Load(fname)); + 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")); + return strdup(gettext_noop("Blind")); } -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.")); +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) @@ -128,24 +129,26 @@ int blind_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_ } 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) + 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_shutdown(magic_api * api ATTRIBUTE_UNUSED) { - Mix_FreeChunk(blind_snd); + Mix_FreeChunk(blind_snd); } // 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) +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) { - magic_api * api = (magic_api *) ptr_to_api; + 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, (blind_b + blind_light) / 2)); + api->putpixel(canvas, x, y, + SDL_MapRGB(canvas->format, (blind_r + blind_light) / 2, (blind_g + blind_light) / 2, + (blind_b + blind_light) / 2)); } /* void blind_do_blind(void * ptr_to_api, int which_tool, @@ -159,31 +162,31 @@ 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) - { - 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; - } - } + 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; @@ -194,19 +197,19 @@ void blind_drag(magic_api * api, int which, SDL_Surface * canvas, 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; - } - } + { + 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); @@ -218,19 +221,19 @@ void blind_drag(magic_api * api, int which, SDL_Surface * canvas, 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; - } - } + { + 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; @@ -242,19 +245,19 @@ void blind_drag(magic_api * api, int which, SDL_Surface * canvas, 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; - } - } + { + 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); @@ -266,37 +269,44 @@ void blind_drag(magic_api * api, int which, SDL_Surface * canvas, } 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 (y < canvas->h / 2) - { - if (x < y) blind_side = 1; /* left */ - else if (canvas->w - x < y) blind_side = 3; /* right */ - else blind_side = 0; /* top */ - } + { + if (x < y) + blind_side = 1; /* left */ + else if (canvas->w - x < y) + blind_side = 3; /* right */ else - { - if (x < canvas->h - y) blind_side = 1; /* left */ - else if (canvas->w - x < canvas->h - y) blind_side = 3; /* right */ - else blind_side = 2; /* bottom */ - } + blind_side = 0; /* top */ + } + else + { + if (x < canvas->h - y) + blind_side = 1; /* left */ + else if (canvas->w - x < canvas->h - y) + blind_side = 3; /* right */ + else + blind_side = 2; /* bottom */ + } - blind_drag(api, which, canvas, last, x, y, x, y, update_rect); + 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, SDL_Surface * canvas ATTRIBUTE_UNUSED) -{ - -} - -void blind_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas 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, + SDL_Surface * canvas ATTRIBUTE_UNUSED) +{ + } int blind_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_FULLSCREEN | MODE_PAINT); + return (MODE_FULLSCREEN | MODE_PAINT); } diff --git a/magic/src/blocks_chalk_drip.c b/magic/src/blocks_chalk_drip.c index b95ea7a36..ddd8ce03e 100644 --- a/magic/src/blocks_chalk_drip.c +++ b/magic/src/blocks_chalk_drip.c @@ -35,7 +35,8 @@ /* What tools we contain: */ -enum { +enum +{ TOOL_BLOCKS, TOOL_CHALK, TOOL_DRIP, @@ -45,7 +46,7 @@ enum { /* Our globals: */ -static Mix_Chunk * snd_effect[NUM_TOOLS]; +static Mix_Chunk *snd_effect[NUM_TOOLS]; /* Our function prototypes: */ @@ -53,21 +54,16 @@ static Mix_Chunk * snd_effect[NUM_TOOLS]; int blocks_chalk_drip_init(magic_api * api); Uint32 blocks_chalk_drip_api_version(void); int blocks_chalk_drip_get_tool_count(magic_api * api); -SDL_Surface * blocks_chalk_drip_get_icon(magic_api * api, int which); -char * blocks_chalk_drip_get_name(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); +SDL_Surface *blocks_chalk_drip_get_icon(magic_api * api, int which); +char *blocks_chalk_drip_get_name(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); 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); @@ -81,90 +77,82 @@ int blocks_chalk_drip_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/blocks.wav", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/blocks.wav", api->data_directory); snd_effect[0] = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%s/sounds/magic/chalk.wav", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/chalk.wav", api->data_directory); snd_effect[1] = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%s/sounds/magic/drip.wav", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/drip.wav", api->data_directory); snd_effect[2] = Mix_LoadWAV(fname); - return(1); + return (1); } -Uint32 blocks_chalk_drip_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 blocks_chalk_drip_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} // We have multiple tools: int blocks_chalk_drip_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(NUM_TOOLS); + return (NUM_TOOLS); } // Load our icons: -SDL_Surface * blocks_chalk_drip_get_icon(magic_api * api, int which) +SDL_Surface *blocks_chalk_drip_get_icon(magic_api * api, int which) { char fname[1024]; if (which == TOOL_BLOCKS) - { - snprintf(fname, sizeof(fname), "%s/images/magic/blocks.png", - api->data_directory); - } + { + snprintf(fname, sizeof(fname), "%s/images/magic/blocks.png", api->data_directory); + } else if (which == TOOL_CHALK) - { - snprintf(fname, sizeof(fname), "%s/images/magic/chalk.png", - api->data_directory); - } + { + snprintf(fname, sizeof(fname), "%s/images/magic/chalk.png", api->data_directory); + } else if (which == TOOL_DRIP) - { - snprintf(fname, sizeof(fname), "%s/images/magic/drip.png", - api->data_directory); - } + { + snprintf(fname, sizeof(fname), "%s/images/magic/drip.png", api->data_directory); + } - return(IMG_Load(fname)); + return (IMG_Load(fname)); } // Return our names, localized: -char * blocks_chalk_drip_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) +char *blocks_chalk_drip_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) { if (which == TOOL_BLOCKS) - return(strdup(gettext_noop("Blocks"))); + return (strdup(gettext_noop("Blocks"))); else if (which == TOOL_CHALK) - return(strdup(gettext_noop("Chalk"))); + return (strdup(gettext_noop("Chalk"))); else if (which == TOOL_DRIP) - return(strdup(gettext_noop("Drip"))); + return (strdup(gettext_noop("Drip"))); - return(NULL); + return (NULL); } // Return our descriptions, localized: -char * blocks_chalk_drip_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode ATTRIBUTE_UNUSED) +char *blocks_chalk_drip_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode ATTRIBUTE_UNUSED) { if (which == TOOL_BLOCKS) - return(strdup(gettext_noop( -"Click and drag the mouse around to make the picture blocky."))); + return (strdup(gettext_noop("Click and drag the mouse around to make the picture blocky."))); else if (which == TOOL_CHALK) - return(strdup(gettext_noop( -"Click and drag the mouse around to turn the picture into a chalk drawing."))); + return (strdup(gettext_noop("Click and drag the mouse around to turn the picture into a chalk drawing."))); else if (which == TOOL_DRIP) - return(strdup(gettext_noop( -"Click and drag the mouse around to make the picture drip."))); + return (strdup(gettext_noop("Click and drag the mouse around to make the picture drip."))); - return(NULL); + 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; + magic_api *api = (magic_api *) ptr; int xx, yy; int h; SDL_Rect src, dest; @@ -172,113 +160,126 @@ static void blocks_chalk_drip_linecb(void * ptr, int which, Uint32 colr; if (which == TOOL_BLOCKS) - { - /* Put x/y on exact grid points: */ - - x = (x / 4) * 4; - y = (y / 4) * 4; - - if (!api->touched(x, y)) { - for (yy = y - 8; yy < y + 8; yy = yy + 4) - { - for (xx = x - 8; xx < x + 8; xx = xx + 4) - { - Uint32 pix[16]; - Uint32 p_or = 0; - Uint32 p_and = ~0; - unsigned i = 16; - 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 = 16; - 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 / 16.0); - g = api->linear_to_sRGB(g_sum / 16.0); - b = api->linear_to_sRGB(b_sum / 16.0); - } + /* Put x/y on exact grid points: */ - /* Draw block: */ + x = (x / 4) * 4; + y = (y / 4) * 4; - dest.x = xx; - dest.y = yy; - dest.w = 4; - dest.h = 4; + if (!api->touched(x, y)) + { + for (yy = y - 8; yy < y + 8; yy = yy + 4) + { + for (xx = x - 8; xx < x + 8; xx = xx + 4) + { + Uint32 pix[16]; + Uint32 p_or = 0; + Uint32 p_and = ~0; + unsigned i = 16; - SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, r, g, b)); - } - } + 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 = 16; + 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 / 16.0); + g = api->linear_to_sRGB(g_sum / 16.0); + b = api->linear_to_sRGB(b_sum / 16.0); + } + + /* Draw block: */ + + dest.x = xx; + dest.y = yy; + dest.w = 4; + dest.h = 4; + + SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, r, g, b)); + } + } + } } - } else if (which == TOOL_CHALK) - { - - for (yy = y - 8; yy <= y + 8; yy = yy + 4) { - for (xx = x - 8; xx <= x + 8; xx = xx + 4) - { - dest.x = xx + ((rand() % 5) - 2); - dest.y = yy + ((rand() % 5) - 2); - dest.w = (rand() % 4) + 2; - dest.h = (rand() % 4) + 2; - colr = api->getpixel(last, clamp(0, xx, canvas->w - 1), - clamp(0, yy, canvas->h - 1)); - SDL_FillRect(canvas, &dest, colr); - } + for (yy = y - 8; yy <= y + 8; yy = yy + 4) + { + for (xx = x - 8; xx <= x + 8; xx = xx + 4) + { + dest.x = xx + ((rand() % 5) - 2); + dest.y = yy + ((rand() % 5) - 2); + dest.w = (rand() % 4) + 2; + dest.h = (rand() % 4) + 2; + + 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++) { - h = (rand() % 8) + 8; + for (xx = x - 8; xx <= x + 8; xx++) + { + 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; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - 16; update_rect->y = oy - 16; @@ -290,16 +291,15 @@ 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 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) { blocks_chalk_drip_drag(api, which, canvas, last, x, y, x, y, update_rect); } // 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -315,7 +315,7 @@ 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) { } @@ -325,15 +325,17 @@ int blocks_chalk_drip_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int whic return 0; } -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_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 ATTRIBUTE_UNUSED) { - return(MODE_PAINT); /* FIXME - Blocks and Chalk, at least, can also be turned into a full-image effect */ + return (MODE_PAINT); /* FIXME - Blocks and Chalk, at least, can also be turned into a full-image effect */ } diff --git a/magic/src/blur.c b/magic/src/blur.c index 065bc038e..e7b0afd66 100644 --- a/magic/src/blur.c +++ b/magic/src/blur.c @@ -42,18 +42,15 @@ Uint32 blur_api_version(void); int blur_init(magic_api * api); int blur_get_tool_count(magic_api * api); -SDL_Surface * blur_get_icon(magic_api * api, int which); -char * blur_get_name(magic_api * api, int which); -char * blur_get_description(magic_api * api, int which, int mode); +SDL_Surface *blur_get_icon(magic_api * api, int which); +char *blur_get_name(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); + 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); + 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 * 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); @@ -61,142 +58,177 @@ 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 { - TOOL_blur, - blur_NUM_TOOLS +enum +{ + TOOL_blur, + blur_NUM_TOOLS }; static const int blur_RADIUS = 16; -static Mix_Chunk * blur_snd_effect[blur_NUM_TOOLS]; +static Mix_Chunk *blur_snd_effect[blur_NUM_TOOLS]; const char *blur_snd_filenames[blur_NUM_TOOLS] = { "blur.wav", }; -const char * blur_icon_filenames[blur_NUM_TOOLS] = { + +const char *blur_icon_filenames[blur_NUM_TOOLS] = { "blur.png", }; -const char * blur_names[blur_NUM_TOOLS] = { + +const char *blur_names[blur_NUM_TOOLS] = { gettext_noop("Blur"), }; -const char * blur_descs[blur_NUM_TOOLS][2] = { - {gettext_noop("Click and drag the mouse around to blur the image."), - gettext_noop("Click to blur the entire image.")}, + +const char *blur_descs[blur_NUM_TOOLS][2] = { + {gettext_noop("Click and drag the mouse around to blur the image."), + gettext_noop("Click to blur the entire image.")}, }; -Uint32 blur_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 blur_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} //Load sounds -int blur_init(magic_api * api){ +int blur_init(magic_api * api) +{ int i; char fname[1024]; - for (i = 0; i < blur_NUM_TOOLS; i++){ - snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, blur_snd_filenames[i]); - blur_snd_effect[i] = Mix_LoadWAV(fname); - } - return(1); + for (i = 0; i < blur_NUM_TOOLS; i++) + { + snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, blur_snd_filenames[i]); + blur_snd_effect[i] = Mix_LoadWAV(fname); + } + return (1); } -int blur_get_tool_count(magic_api * api ATTRIBUTE_UNUSED){ - return(blur_NUM_TOOLS); +int blur_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) +{ + return (blur_NUM_TOOLS); } // Load our icons: -SDL_Surface * blur_get_icon(magic_api * api, int which){ +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]); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } // Return our names, localized: -char * blur_get_name(magic_api * api ATTRIBUTE_UNUSED, int which){ - return(strdup(gettext_noop(blur_names[which]))); +char *blur_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) +{ + return (strdup(gettext_noop(blur_names[which]))); } // Return our descriptions, localized: -char * blur_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode){ - return(strdup(gettext_noop(blur_descs[which][mode-1]))); +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){ - magic_api * api = (magic_api *) ptr; - int i,j,k; - Uint8 temp[3]; +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; + Uint8 temp[3]; double blurValue[3]; + //5x5 gaussiann weighting window - const int weight[5][5] = { {1,4,7,4,1}, - {4,16,26,16,4}, - {7,26,41,26,7}, - {4,16,26,16,4}, - {1,4,7,4,1}}; + const int weight[5][5] = { {1, 4, 7, 4, 1}, + {4, 16, 26, 16, 4}, + {7, 26, 41, 26, 7}, + {4, 16, 26, 16, 4}, + {1, 4, 7, 4, 1} + }; - for (k =0;k<3;k++){ - blurValue[k] = 0; - } - - for (i=-2;i<3;i++){ - 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]; - } + for (k = 0; k < 3; k++) + { + blurValue[k] = 0; } - } - for (k =0;k<3;k++){ - blurValue[k] /= 273; - } - api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, blurValue[0], blurValue[1], blurValue[2])); + + for (i = -2; i < 3; i++) + { + 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]; + } + } + } + for (k = 0; k < 3; k++) + { + 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; + int x, y; - for (y = 0; y < last->h; y++){ - for (x=0; x < last->w; x++){ - do_blur_pixel(ptr, which, canvas, last, x, y); - } - } + for (y = 0; y < last->h; y++) + { + for (x = 0; x < last->w; x++) + { + do_blur_pixel(ptr, 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; + 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++) { - if (api->in_circle(xx - x, yy - y, blur_RADIUS) && - !api->touched(xx, yy)) - { - do_blur_pixel(api, which, canvas, last, xx, yy); - } + 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); + } + } } - } } // 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; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - blur_RADIUS; update_rect->y = oy - blur_RADIUS; @@ -206,41 +238,46 @@ 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); - } + 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); + } } // 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } // No setup happened: void blur_shutdown(magic_api * api ATTRIBUTE_UNUSED) { - //Clean up sounds - int i; - for(i=0; i #include -#include /* For RAND_MAX */ +#include /* For RAND_MAX */ #include "tp_magic_api.h" #include "SDL_image.h" #include "SDL_mixer.h" /* What tools we contain: */ -enum { +enum +{ TOOL_LARGEBRICKS, TOOL_SMALLBRICKS, NUM_TOOLS @@ -47,29 +48,24 @@ enum { /* Our globals: */ -static Mix_Chunk * brick_snd; +static Mix_Chunk *brick_snd; 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); -SDL_Surface * bricks_get_icon(magic_api * api, int which); -char * bricks_get_name(magic_api * api, int which); -char * bricks_get_description(magic_api * api, int which, int mode); +SDL_Surface *bricks_get_icon(magic_api * api, int which); +char *bricks_get_name(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); + 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. + 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); @@ -82,65 +78,64 @@ int bricks_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/brick.wav", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/brick.wav", api->data_directory); brick_snd = Mix_LoadWAV(fname); - return(1); + return (1); } -Uint32 bricks_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 bricks_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} // We have multiple tools: int bricks_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(NUM_TOOLS); + return (NUM_TOOLS); } // Load our icons: -SDL_Surface * bricks_get_icon(magic_api * api, int which) +SDL_Surface *bricks_get_icon(magic_api * api, int which) { char fname[1024]; if (which == TOOL_LARGEBRICKS) - { - snprintf(fname, sizeof(fname), "%s/images/magic/largebrick.png", - api->data_directory); - } + { + snprintf(fname, sizeof(fname), "%s/images/magic/largebrick.png", api->data_directory); + } else if (which == TOOL_SMALLBRICKS) - { - snprintf(fname, sizeof(fname), "%s/images/magic/smallbrick.png", - api->data_directory); - } + { + snprintf(fname, sizeof(fname), "%s/images/magic/smallbrick.png", api->data_directory); + } - return(IMG_Load(fname)); + 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: */ - return(strdup(gettext_noop("Bricks"))); + return (strdup(gettext_noop("Bricks"))); } // 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."))); + return (strdup(gettext_noop("Click and drag to draw large bricks."))); else if (which == TOOL_SMALLBRICKS) - return(strdup(gettext_noop("Click and drag to draw small bricks."))); + return (strdup(gettext_noop("Click and drag to draw small bricks."))); - return(NULL); + return (NULL); } // 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; + magic_api *api = (magic_api *) ptr; // "specified" means the brick itself, w/o morter // "nominal" means brick-to-brick (includes morter) @@ -148,22 +143,22 @@ static void do_bricks(void * ptr, int which, SDL_Surface * canvas, SDL_Surface * int nominal_length; int brick_x, brick_y; - int vertical_joint = 2; // between a brick and the one above/below - int horizontal_joint = 2; // between a brick and the one to the side + int vertical_joint = 2; // between a brick and the one above/below + int horizontal_joint = 2; // between a brick and the one to the side int nominal_width = 18; - int nominal_height = 12; // 11 to 14, for joints of 2 + int nominal_height = 12; // 11 to 14, for joints of 2 static unsigned char *map; static int x_count; static int y_count; 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; @@ -171,69 +166,79 @@ 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) - { - 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 ((unsigned)x < (unsigned)canvas->w && (unsigned)y < (unsigned)canvas->h && !*mybrick) { - if (mybrick[1]) - my_w = specified_length; + 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); + */ } - 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; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = x - 64; update_rect->y = y - 64; @@ -245,15 +250,14 @@ 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -278,23 +282,16 @@ int bricks_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE 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); @@ -315,16 +312,16 @@ static void do_brick(magic_api * api, SDL_Surface * canvas, } void bricks_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas 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) + int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { } int bricks_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_PAINT); + return (MODE_PAINT); } diff --git a/magic/src/calligraphy.c b/magic/src/calligraphy.c index c73d1f21e..ae2a455d3 100644 --- a/magic/src/calligraphy.c +++ b/magic/src/calligraphy.c @@ -41,32 +41,29 @@ typedef struct float x, y; } Point2D; -static Mix_Chunk * calligraphy_snd; +static Mix_Chunk *calligraphy_snd; static Point2D calligraphy_control_points[4]; static int calligraphy_r, calligraphy_g, calligraphy_b; static int calligraphy_old_thick; static Uint32 calligraphy_last_time; -static SDL_Surface * calligraphy_brush, * calligraphy_colored_brush; +static SDL_Surface *calligraphy_brush, *calligraphy_colored_brush; /* Local Function Prototypes */ -static Point2D calligraphy_PointOnCubicBezier(Point2D* cp, float t); -static void calligraphy_ComputeBezier(Point2D* cp, int numberOfPoints, Point2D* curve); +static Point2D calligraphy_PointOnCubicBezier(Point2D * cp, float t); +static void calligraphy_ComputeBezier(Point2D * cp, int numberOfPoints, Point2D * curve); static float calligraphy_dist(float x1, float y1, float x2, float y2); int calligraphy_init(magic_api * api); Uint32 calligraphy_api_version(void); int calligraphy_get_tool_count(magic_api * api); -SDL_Surface * calligraphy_get_icon(magic_api * api, int which); -char * calligraphy_get_name(magic_api * api, int which); -char * calligraphy_get_description(magic_api * api, int which, int mode); +SDL_Surface *calligraphy_get_icon(magic_api * api, int which); +char *calligraphy_get_name(magic_api * api, int which); +char *calligraphy_get_description(magic_api * api, int which, int mode); void calligraphy_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, - SDL_Rect * update_rect); + SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); void calligraphy_click(magic_api * api, int which, int mode, - SDL_Surface * canvas, SDL_Surface * last, - int x, int y, SDL_Rect * update_rect); + SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); void calligraphy_release(magic_api * api, int which, - SDL_Surface * canvas, SDL_Surface * last, - int x, int y, SDL_Rect * update_rect); + SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect); void calligraphy_shutdown(magic_api * api); void calligraphy_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int calligraphy_requires_colors(magic_api * api, int which); @@ -79,19 +76,17 @@ int calligraphy_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/calligraphy.ogg", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/calligraphy.ogg", api->data_directory); calligraphy_snd = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%s/images/magic/calligraphy_brush.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/calligraphy_brush.png", api->data_directory); calligraphy_brush = IMG_Load(fname); calligraphy_colored_brush = NULL; if (calligraphy_brush == NULL) - return(0); + return (0); calligraphy_last_time = 0; @@ -100,53 +95,54 @@ int calligraphy_init(magic_api * api) calligraphy_g = -1; calligraphy_b = -1; - return(1); + return (1); } -Uint32 calligraphy_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 calligraphy_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} // Only one tool: int calligraphy_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(1); + return (1); } // Load our icon: -SDL_Surface * calligraphy_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *calligraphy_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/calligraphy.png", - api->data_directory); - return(IMG_Load(fname)); + snprintf(fname, sizeof(fname), "%s/images/magic/calligraphy.png", api->data_directory); + return (IMG_Load(fname)); } // Return our name, localized: -char * calligraphy_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *calligraphy_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(strdup(gettext_noop("Calligraphy"))); + return (strdup(gettext_noop("Calligraphy"))); } // Return our description, localized: -char * calligraphy_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *calligraphy_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED) { - return(strdup( - gettext_noop("Click and drag the mouse around to draw in calligraphy."))); + return (strdup(gettext_noop("Click and drag the mouse around to draw in calligraphy."))); } void calligraphy_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, - SDL_Surface * last ATTRIBUTE_UNUSED, int ox, int oy, int x, int y, - SDL_Rect * update_rect) + SDL_Surface * last ATTRIBUTE_UNUSED, int ox, int oy, int x, int y, SDL_Rect * update_rect) { - Point2D * curve; + Point2D *curve; int i, n_points, thick, new_thick; Uint32 colr; SDL_Rect src, dest; // if (SDL_GetTicks() < calligraphy_last_time + 5) // return; - + calligraphy_control_points[0].x = calligraphy_control_points[1].x; calligraphy_control_points[0].y = calligraphy_control_points[1].y; calligraphy_control_points[1].x = calligraphy_control_points[2].x; @@ -170,82 +166,76 @@ void calligraphy_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * */ n_points = calligraphy_dist(calligraphy_control_points[0].x, - calligraphy_control_points[0].y, - calligraphy_control_points[1].x, - calligraphy_control_points[1].y) + - calligraphy_dist(calligraphy_control_points[1].x, - calligraphy_control_points[1].y, - calligraphy_control_points[2].x, - calligraphy_control_points[2].y) + - calligraphy_dist(calligraphy_control_points[2].x, - calligraphy_control_points[2].y, - calligraphy_control_points[3].x, - calligraphy_control_points[3].y); + calligraphy_control_points[0].y, + calligraphy_control_points[1].x, + calligraphy_control_points[1].y) + + calligraphy_dist(calligraphy_control_points[1].x, + calligraphy_control_points[1].y, + calligraphy_control_points[2].x, + calligraphy_control_points[2].y) + + calligraphy_dist(calligraphy_control_points[2].x, + calligraphy_control_points[2].y, calligraphy_control_points[3].x, calligraphy_control_points[3].y); if (n_points == 0) - return; // No-op; not any points to plot + return; // No-op; not any points to plot curve = (Point2D *) malloc(sizeof(Point2D) * n_points); calligraphy_ComputeBezier(calligraphy_control_points, n_points, curve); - colr = SDL_MapRGB(canvas->format, - calligraphy_r, - calligraphy_g, - calligraphy_b); + colr = SDL_MapRGB(canvas->format, calligraphy_r, calligraphy_g, calligraphy_b); - new_thick = 40 - min((n_points /* / 2 */), 32); + new_thick = 40 - min((n_points /* / 2 */ ), 32); for (i = 0; i < n_points - 1; i++) - { - thick = ((new_thick * i) + - (calligraphy_old_thick * (n_points - i))) / n_points; - - - /* The new way, using an antialiased brush bitmap */ - - x = curve[i].x; - y = curve[i].y; - - src.x = calligraphy_brush->w - thick / 2 - thick / 4; - src.w = thick / 2 + thick / 4; - src.y = 0; - src.h = thick / 4; - - dest.x = x - thick / 4; - dest.y = y - thick / 4; - - SDL_BlitSurface(calligraphy_colored_brush, &src, canvas, &dest); - - - src.x = 0; - src.w = thick / 2 + thick / 4; - src.y = calligraphy_brush->h - thick / 4; - src.h = thick / 4; - - dest.x = x - thick / 2; - dest.y = y; - - SDL_BlitSurface(calligraphy_colored_brush, &src, canvas, &dest); - - /* Old way; using putpixel: - SDL_LockSurface(canvas); - - for (j = -(thick / 2); j < (thick / 2) + 1; j++) { - x = curve[i].x + j; - y = curve[i].y - (j / 2); // 30 degrees + thick = ((new_thick * i) + (calligraphy_old_thick * (n_points - i))) / n_points; - api->putpixel(canvas, x, y, colr); - api->putpixel(canvas, x + 1, y, colr); - api->putpixel(canvas, x, y + 1, colr); - api->putpixel(canvas, x + 1, y + 1, colr); + + /* The new way, using an antialiased brush bitmap */ + + x = curve[i].x; + y = curve[i].y; + + src.x = calligraphy_brush->w - thick / 2 - thick / 4; + src.w = thick / 2 + thick / 4; + src.y = 0; + src.h = thick / 4; + + dest.x = x - thick / 4; + dest.y = y - thick / 4; + + SDL_BlitSurface(calligraphy_colored_brush, &src, canvas, &dest); + + + src.x = 0; + src.w = thick / 2 + thick / 4; + src.y = calligraphy_brush->h - thick / 4; + src.h = thick / 4; + + dest.x = x - thick / 2; + dest.y = y; + + SDL_BlitSurface(calligraphy_colored_brush, &src, canvas, &dest); + + /* Old way; using putpixel: + SDL_LockSurface(canvas); + + for (j = -(thick / 2); j < (thick / 2) + 1; j++) + { + x = curve[i].x + j; + y = curve[i].y - (j / 2); // 30 degrees + + api->putpixel(canvas, x, y, colr); + api->putpixel(canvas, x + 1, y, colr); + api->putpixel(canvas, x, y + 1, colr); + api->putpixel(canvas, x + 1, y + 1, colr); + } + + SDL_UnlockSurface(canvas); + */ } - - SDL_UnlockSurface(canvas); - */ - } calligraphy_old_thick = (calligraphy_old_thick + new_thick) / 2; @@ -253,8 +243,20 @@ void calligraphy_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * - if (ox > x) { int tmp = ox; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - 16; update_rect->y = oy - 16; @@ -272,8 +274,8 @@ void calligraphy_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * } void calligraphy_click(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, - SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, - int x, int y, SDL_Rect * update_rect ATTRIBUTE_UNUSED) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x, int y, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { calligraphy_old_thick = 8; calligraphy_last_time = 0; @@ -290,8 +292,8 @@ void calligraphy_click(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNU void calligraphy_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, - int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -313,9 +315,7 @@ void calligraphy_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b) Uint8 a; Uint32 amask; - if (calligraphy_r == r && - calligraphy_g == g && - calligraphy_b == b) + if (calligraphy_r == r && calligraphy_g == g && calligraphy_b == b) return; calligraphy_r = r; @@ -325,9 +325,7 @@ void calligraphy_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b) if (calligraphy_colored_brush != NULL) SDL_FreeSurface(calligraphy_colored_brush); - amask = ~(calligraphy_brush->format->Rmask | - calligraphy_brush->format->Gmask | - calligraphy_brush->format->Bmask); + amask = ~(calligraphy_brush->format->Rmask | calligraphy_brush->format->Gmask | calligraphy_brush->format->Bmask); calligraphy_colored_brush = SDL_CreateRGBSurface(SDL_SWSURFACE, @@ -335,30 +333,25 @@ void calligraphy_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b) calligraphy_brush->h, calligraphy_brush->format->BitsPerPixel, calligraphy_brush->format->Rmask, - calligraphy_brush->format->Gmask, - calligraphy_brush->format->Bmask, amask); + calligraphy_brush->format->Gmask, calligraphy_brush->format->Bmask, amask); if (calligraphy_colored_brush == NULL) - return; // FIXME: Error! + return; // FIXME: Error! SDL_LockSurface(calligraphy_brush); SDL_LockSurface(calligraphy_colored_brush); for (y = 0; y < calligraphy_brush->h; y++) - { - for (x = 0; x < calligraphy_brush->w; x++) { - SDL_GetRGBA(api->getpixel(calligraphy_brush, x, y), - calligraphy_brush->format, &r, &g, &b, &a); + for (x = 0; x < calligraphy_brush->w; x++) + { + SDL_GetRGBA(api->getpixel(calligraphy_brush, x, y), calligraphy_brush->format, &r, &g, &b, &a); - api->putpixel(calligraphy_colored_brush, x, y, - SDL_MapRGBA(calligraphy_colored_brush->format, - calligraphy_r, - calligraphy_g, - calligraphy_b, a)); + api->putpixel(calligraphy_colored_brush, x, y, + SDL_MapRGBA(calligraphy_colored_brush->format, calligraphy_r, calligraphy_g, calligraphy_b, a)); + } } - } SDL_UnlockSurface(calligraphy_colored_brush); SDL_UnlockSurface(calligraphy_brush); @@ -384,32 +377,32 @@ cp[3] is the end point, or P3 in the above diagram t is the parameter value, 0 <= t <= 1 */ -static Point2D calligraphy_PointOnCubicBezier( Point2D* cp, float t ) +static Point2D calligraphy_PointOnCubicBezier(Point2D * cp, float t) { - float ax, bx, cx; - float ay, by, cy; - float tSquared, tCubed; - Point2D result; + float ax, bx, cx; + float ay, by, cy; + float tSquared, tCubed; + Point2D result; - /* calculate the polynomial coefficients */ + /* calculate the polynomial coefficients */ - cx = 3.0 * (cp[1].x - cp[0].x); - bx = 3.0 * (cp[2].x - cp[1].x) - cx; - ax = cp[3].x - cp[0].x - cx - bx; + cx = 3.0 * (cp[1].x - cp[0].x); + bx = 3.0 * (cp[2].x - cp[1].x) - cx; + ax = cp[3].x - cp[0].x - cx - bx; - cy = 3.0 * (cp[1].y - cp[0].y); - by = 3.0 * (cp[2].y - cp[1].y) - cy; - ay = cp[3].y - cp[0].y - cy - by; + cy = 3.0 * (cp[1].y - cp[0].y); + by = 3.0 * (cp[2].y - cp[1].y) - cy; + ay = cp[3].y - cp[0].y - cy - by; - /* calculate the curve point at parameter value t */ + /* calculate the curve point at parameter value t */ - tSquared = t * t; - tCubed = tSquared * t; + tSquared = t * t; + tCubed = tSquared * t; - result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x; - result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y; + result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x; + result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y; - return result; + return result; } @@ -420,34 +413,37 @@ static Point2D calligraphy_PointOnCubicBezier( Point2D* cp, float t ) */ -static void calligraphy_ComputeBezier(Point2D* cp, int numberOfPoints, Point2D* curve) +static void calligraphy_ComputeBezier(Point2D * cp, int numberOfPoints, Point2D * curve) { - float dt; - int i; + float dt; + int i; - dt = 1.0 / ( numberOfPoints - 1 ); + dt = 1.0 / (numberOfPoints - 1); - for( i = 0; i < numberOfPoints; i++) - curve[i] = calligraphy_PointOnCubicBezier( cp, i*dt ); + for (i = 0; i < numberOfPoints; i++) + curve[i] = calligraphy_PointOnCubicBezier(cp, i * dt); } static float calligraphy_dist(float x1, float y1, float x2, float y2) { float d; + d = (sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))); return d; } -void calligraphy_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void calligraphy_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void calligraphy_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void calligraphy_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } int calligraphy_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_PAINT); + return (MODE_PAINT); } diff --git a/magic/src/cartoon.c b/magic/src/cartoon.c index 630883198..2dfee87ef 100644 --- a/magic/src/cartoon.c +++ b/magic/src/cartoon.c @@ -38,7 +38,7 @@ /* Our globals: */ -static Mix_Chunk * cartoon_snd; +static Mix_Chunk *cartoon_snd; #define OUTLINE_THRESH 48 @@ -46,20 +46,16 @@ static Mix_Chunk * cartoon_snd; int cartoon_init(magic_api * api); Uint32 cartoon_api_version(void); int cartoon_get_tool_count(magic_api * api); -SDL_Surface * cartoon_get_icon(magic_api * api, int which); -char * cartoon_get_name(magic_api * api, int which); -char * cartoon_get_description(magic_api * api, int which, int mode); -static void do_cartoon(void * ptr, int which, SDL_Surface * canvas, SDL_Surface * last, - int x, int y); +SDL_Surface *cartoon_get_icon(magic_api * api, int which); +char *cartoon_get_name(magic_api * api, int which); +char *cartoon_get_description(magic_api * api, int which, int mode); +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); + 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); + 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 * 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); @@ -74,51 +70,50 @@ int cartoon_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/cartoon.wav", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/cartoon.wav", api->data_directory); cartoon_snd = Mix_LoadWAV(fname); - return(1); + return (1); } -Uint32 cartoon_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 cartoon_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} // We have multiple tools: int cartoon_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(1); + return (1); } // Load our icons: -SDL_Surface * cartoon_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *cartoon_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/cartoon.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/cartoon.png", api->data_directory); - return(IMG_Load(fname)); + 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 (strdup(gettext_noop("Cartoon"))); } // Return our descriptions, localized: -char * cartoon_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *cartoon_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 turn the picture into a cartoon."))); + return (strdup(gettext_noop("Click and drag the mouse around to turn the picture into a cartoon."))); } // Do the effect: -static void do_cartoon(void * ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * last, - int x, int y) +static void do_cartoon(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * last, int x, int y) { - magic_api * api = (magic_api *) ptr; + magic_api *api = (magic_api *) ptr; int xx, yy; Uint8 r1, g1, b1, r2, g2, b2; Uint8 r, g, b; @@ -127,86 +122,90 @@ static void do_cartoon(void * ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * can /* First, convert colors to more cartoony ones: */ for (yy = y - 16; yy < y + 16; yy = yy + 1) - { - for (xx = x - 16; xx < x + 16; xx = xx + 1) { - if (api->in_circle(xx - x, yy - y, 16)) - { - /* Get original color: */ + for (xx = x - 16; xx < x + 16; xx = xx + 1) + { + if (api->in_circle(xx - x, yy - y, 16)) + { + /* Get original color: */ - SDL_GetRGB(api->getpixel(last, xx, yy), last->format, &r, &g, &b); + SDL_GetRGB(api->getpixel(last, xx, yy), last->format, &r, &g, &b); - api->rgbtohsv(r, g, b, &hue, &sat, &val); + api->rgbtohsv(r, g, b, &hue, &sat, &val); - val = val - 0.5; - val = val * 4; - val = val + 0.5; + val = val - 0.5; + val = val * 4; + val = val + 0.5; - if (val < 0) - val = 0; - else if (val > 1.0) - val = 1.0; + if (val < 0) + val = 0; + else if (val > 1.0) + val = 1.0; - val = floor(val * 4) / 4; - hue = floor(hue * 4) / 4; + val = floor(val * 4) / 4; + hue = floor(hue * 4) / 4; - sat = floor(sat * 4) / 4; + sat = floor(sat * 4) / 4; - api->hsvtorgb(hue, sat, val, &r, &g, &b); + api->hsvtorgb(hue, sat, val, &r, &g, &b); - api->putpixel(canvas, xx, yy, SDL_MapRGB(canvas->format, r, g, b)); - } + api->putpixel(canvas, xx, yy, SDL_MapRGB(canvas->format, r, g, b)); + } + } } - } /* Then, draw dark outlines where there's a large contrast change */ for (yy = y - 16; yy < y + 16; yy++) - { - for (xx = x - 16; xx < x + 16; xx++) { - if (api->in_circle(xx - x, yy - y, 16)) - { - /* Get original color: */ - - SDL_GetRGB(api->getpixel(last, xx, yy), last->format, &r, &g, &b); - - SDL_GetRGB(api->getpixel(last, xx + 1, yy), - last->format, &r1, &g1, &b1); - - SDL_GetRGB(api->getpixel(last, xx + 1, yy + 1), - last->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) + for (xx = x - 16; xx < x + 16; xx++) { - api->putpixel(canvas, xx - 1, yy, - SDL_MapRGB(canvas->format, 0, 0, 0)); - api->putpixel(canvas, xx, yy - 1, - SDL_MapRGB(canvas->format, 0, 0, 0)); - api->putpixel(canvas, xx - 1, yy - 1, - SDL_MapRGB(canvas->format, 0, 0, 0)); + if (api->in_circle(xx - x, yy - y, 16)) + { + /* Get original color: */ + + SDL_GetRGB(api->getpixel(last, xx, yy), last->format, &r, &g, &b); + + SDL_GetRGB(api->getpixel(last, xx + 1, yy), last->format, &r1, &g1, &b1); + + SDL_GetRGB(api->getpixel(last, xx + 1, yy + 1), last->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(canvas, xx - 1, yy, SDL_MapRGB(canvas->format, 0, 0, 0)); + api->putpixel(canvas, xx, yy - 1, SDL_MapRGB(canvas->format, 0, 0, 0)); + api->putpixel(canvas, xx - 1, yy - 1, SDL_MapRGB(canvas->format, 0, 0, 0)); + } + } } - } } - } } // 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) { - 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); - if (ox > x) { int tmp = ox; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - 16; update_rect->y = oy - 16; @@ -218,16 +217,15 @@ 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 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) { cartoon_drag(api, which, canvas, last, x, y, x, y, update_rect); } // 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -239,7 +237,8 @@ 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, Uint8 b ATTRIBUTE_UNUSED) +void cartoon_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, + Uint8 b ATTRIBUTE_UNUSED) { } @@ -249,15 +248,17 @@ int cartoon_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUT return 0; } -void cartoon_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void cartoon_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void cartoon_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void cartoon_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } int cartoon_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_PAINT); /* FIXME - Can also be turned into a full-image effect */ + return (MODE_PAINT); /* FIXME - Can also be turned into a full-image effect */ } diff --git a/magic/src/confetti.c b/magic/src/confetti.c index 6da7b8008..7d5dbc419 100644 --- a/magic/src/confetti.c +++ b/magic/src/confetti.c @@ -1,70 +1,67 @@ -#include //For time() +#include //For time() #include "tp_magic_api.h" #include "SDL_image.h" #include "SDL_mixer.h" -#define CONFETTI_BRUSH_SIZE 8 //radius of each confetti circle -#define CONFETTI_QUANTITY 3 //how many circles will be created every click? +#define CONFETTI_BRUSH_SIZE 8 //radius of each confetti circle +#define CONFETTI_QUANTITY 3 //how many circles will be created every click? struct confetti_rgb { - Uint8 r, g, b; + Uint8 r, g, b; }; -struct confetti_rgb confetti_colors; //storage for colors, just for having everything in one place +struct confetti_rgb confetti_colors; //storage for colors, just for having everything in one place -Mix_Chunk * confetti_snd; +Mix_Chunk *confetti_snd; /* Local function prototypes: */ Uint32 confetti_api_version(void); void confetti_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int confetti_init(magic_api * api); int confetti_get_tool_count(magic_api * api); -SDL_Surface * confetti_get_icon(magic_api * api, int which); -char * confetti_get_name(magic_api * api, int which); -char * confetti_get_description(magic_api * api, int which, int mode); +SDL_Surface *confetti_get_icon(magic_api * api, int which); +char *confetti_get_name(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); +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); + 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 +// 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) { - return(TP_MAGIC_API_VERSION); + return (TP_MAGIC_API_VERSION); } -void confetti_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) //get the colors from API and store it in structure +void confetti_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) //get the colors from API and store it in structure { - confetti_colors.r=r; - confetti_colors.g=g; - confetti_colors.b=b; + confetti_colors.r = r; + confetti_colors.g = g; + confetti_colors.b = b; } int confetti_init(magic_api * api) { char fname[1024]; - - snprintf(fname, sizeof(fname), "%s/sounds/magic/confetti.ogg", api->data_directory); - confetti_snd = Mix_LoadWAV(fname); - return(1); + snprintf(fname, sizeof(fname), "%s/sounds/magic/confetti.ogg", api->data_directory); + confetti_snd = Mix_LoadWAV(fname); + + return (1); } int confetti_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) @@ -72,137 +69,168 @@ int confetti_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) return 1; } -SDL_Surface * confetti_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *confetti_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/confetti.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/confetti.png", api->data_directory); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } -char * confetti_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { return strdup(gettext_noop("Confetti")); } +char *confetti_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +{ + return strdup(gettext_noop("Confetti")); +} -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!")); } +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) { return 1; } +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) + 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_shutdown(magic_api * api ATTRIBUTE_UNUSED) - { Mix_FreeChunk(confetti_snd); } +{ + Mix_FreeChunk(confetti_snd); +} //private functions - -inline char confetti_get_greater(const char what1, const char what2) { if (what1>what2) return what1; else return what2; } -inline char confetti_get_lesser(const char what1, const char what2) { if (what1 what2) + return what1; + else + return what2; +} + +inline char confetti_get_lesser(const char what1, const char what2) +{ + if (what1 < what2) + return what1; + else + return what2; +} // Interactivity functions -Uint32 confetti_get_new_color(void * ptr, SDL_Surface * canvas) //this function creates new color very similar to the one choosen +Uint32 confetti_get_new_color(void *ptr, SDL_Surface * canvas) //this function creates new color very similar to the one choosen { - magic_api * api = (magic_api *) ptr; - - float hsv_h, hsv_s, hsv_v; - Uint8 temp_r, temp_g, temp_b; - - api->rgbtohsv(confetti_colors.r, confetti_colors.g, confetti_colors.b, &hsv_h, &hsv_s, &hsv_v); //color choosen by user is converted - //to HSV palette + magic_api *api = (magic_api *) ptr; - hsv_h+=((rand()%60)-30)%360; //Every circle has different, but - //smilar color - if (hsv_h<0) - hsv_h *= -1; - - api->hsvtorgb(hsv_h, hsv_s, hsv_v, &temp_r, &temp_g, &temp_b); //...and come back to RGB + float hsv_h, hsv_s, hsv_v; + Uint8 temp_r, temp_g, temp_b; - return SDL_MapRGB(canvas->format, temp_r, temp_g, temp_b); + api->rgbtohsv(confetti_colors.r, confetti_colors.g, confetti_colors.b, &hsv_h, &hsv_s, &hsv_v); //color choosen by user is converted + //to HSV palette + + hsv_h += ((rand() % 60) - 30) % 360; //Every circle has different, but + //smilar color + if (hsv_h < 0) + hsv_h *= -1; + + api->hsvtorgb(hsv_h, hsv_s, hsv_v, &temp_r, &temp_g, &temp_b); //...and come back to RGB + + return SDL_MapRGB(canvas->format, temp_r, temp_g, temp_b); } - - -static void confetti_circle(void * ptr, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, - int x, int y) + + +static void confetti_circle(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { - magic_api * api = (magic_api *) ptr; - + 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 (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); + Uint32 color = confetti_get_new_color(api, canvas); + + 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++) + + 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 - 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); - } + unsigned char i; + char min_x = 0, max_x = 0, min_y = 0, max_y = 0; + char dx = 0, dy = 0; - update_rect->x = x+min_x - CONFETTI_BRUSH_SIZE/2; - update_rect->y = y+ min_y - CONFETTI_BRUSH_SIZE/2; - update_rect->w = CONFETTI_BRUSH_SIZE*1.5+max_x-min_x; - update_rect->h = CONFETTI_BRUSH_SIZE*1.5+max_y-min_y; - - api->playsound(confetti_snd, (x * 255) / canvas->w,255); + 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 + + 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; + update_rect->w = CONFETTI_BRUSH_SIZE * 1.5 + max_x - min_x; + update_rect->h = CONFETTI_BRUSH_SIZE * 1.5 + max_y - min_y; + + api->playsound(confetti_snd, (x * 255) / canvas->w, 255); } 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;} - if (oy>y) {temp=y; y=oy; oy=temp; } - - confetti_click(api, which, MODE_PAINT, canvas, snapshot, x, y, update_rect); + int temp; + + if (ox > x) + { + temp = x; + x = ox; + ox = temp; + } + if (oy > y) + { + 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, SDL_Surface * canvas 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, SDL_Surface * canvas 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) { - return(MODE_PAINT); + return (MODE_PAINT); } diff --git a/magic/src/distortion.c b/magic/src/distortion.c index 124b81dcd..e61b4e4bf 100644 --- a/magic/src/distortion.c +++ b/magic/src/distortion.c @@ -31,11 +31,11 @@ /* -------------------------- */ #include -#include // For "strdup()" +#include // For "strdup()" -#include "tp_magic_api.h" // Tux Paint "Magic" tool API header -#include "SDL_image.h" // For IMG_Load(), to load our PNG icon -#include "SDL_mixer.h" // For Mix_LoadWAV(), to load our sound effects +#include "tp_magic_api.h" // Tux Paint "Magic" tool API header +#include "SDL_image.h" // For IMG_Load(), to load our PNG icon +#include "SDL_mixer.h" // For Mix_LoadWAV(), to load our sound effects @@ -43,7 +43,7 @@ /* --------------------- */ /* Sound effects: */ -static Mix_Chunk * snd_effect; +static Mix_Chunk *snd_effect; /* Our local function prototypes: */ @@ -57,18 +57,16 @@ static Mix_Chunk * snd_effect; Uint32 distortion_api_version(void); int distortion_init(magic_api * api); int distortion_get_tool_count(magic_api * api); -SDL_Surface * distortion_get_icon(magic_api * api, int which); -char * distortion_get_name(magic_api * api, int which); -char * distortion_get_description(magic_api * api, int which, int mode); +SDL_Surface *distortion_get_icon(magic_api * api, int which); +char *distortion_get_name(magic_api * api, int which); +char *distortion_get_description(magic_api * api, int which, int mode); 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); + 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_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); void distortion_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); @@ -76,12 +74,9 @@ void distortion_switchout(magic_api * api, int which, int mode, SDL_Surface * ca 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: */ @@ -89,7 +84,7 @@ static void distortion_line_callback(void * ptr, int which, Uint32 distortion_api_version(void) { - return(TP_MAGIC_API_VERSION); + return (TP_MAGIC_API_VERSION); } @@ -99,15 +94,13 @@ int distortion_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), - "%s/sounds/magic/distortion.ogg", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/distortion.ogg", api->data_directory); // Try to load the file! snd_effect = Mix_LoadWAV(fname); - return(1); + return (1); } @@ -115,39 +108,39 @@ int distortion_init(magic_api * api) int distortion_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(1); + return (1); } // Load icons -SDL_Surface * distortion_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *distortion_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/distortion.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/distortion.png", api->data_directory); // Try to load the image, and return the results to Tux Paint: - return(IMG_Load(fname)); + return (IMG_Load(fname)); } // 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"))); + return (strdup(gettext_noop("Distortion"))); } // Report our "Magic" tool descriptions -char * distortion_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode 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 @@ -172,8 +165,7 @@ 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); } @@ -182,15 +174,25 @@ 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; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - 8; @@ -199,33 +201,32 @@ void distortion_drag(magic_api * api, int which, SDL_Surface * canvas, update_rect->h = (y + 8) - update_rect->h; - api->playsound(snd_effect, - (x * 255) / canvas->w, // pan - 255); // distance + api->playsound(snd_effect, (x * 255) / canvas->w, // pan + 255); // distance } // 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) + 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, Uint8 b ATTRIBUTE_UNUSED) +void distortion_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, + Uint8 b ATTRIBUTE_UNUSED) { } // Our "callback" function -static void distortion_line_callback(void * ptr, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * snapshot, - int x, int y) +static void distortion_line_callback(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y) { - magic_api * api = (magic_api *) ptr; + magic_api *api = (magic_api *) ptr; int xx, yy; @@ -234,28 +235,28 @@ 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++) { - if (api->in_circle(xx, yy, 8)) - { - api->putpixel(canvas, x + xx, y + yy, - api->getpixel(snapshot, - x + xx / 2, y + yy)); - } + 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)); + } + } } - } } -void distortion_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas 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, SDL_Surface * canvas 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) { - return(MODE_PAINT); + return (MODE_PAINT); } diff --git a/magic/src/emboss.c b/magic/src/emboss.c index 2abbd800b..0ffcbf43c 100644 --- a/magic/src/emboss.c +++ b/magic/src/emboss.c @@ -35,27 +35,24 @@ /* Our globals: */ -static Mix_Chunk * emboss_snd; +static Mix_Chunk *emboss_snd; // Prototypes Uint32 emboss_api_version(void); int emboss_init(magic_api * api); int emboss_get_tool_count(magic_api * api); -SDL_Surface * emboss_get_icon(magic_api * api, int which); -char * emboss_get_name(magic_api * api, int which); -char * emboss_get_description(magic_api * api, int which, int mode); +SDL_Surface *emboss_get_icon(magic_api * api, int which); +char *emboss_get_name(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); @@ -65,7 +62,10 @@ void emboss_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas int emboss_modes(magic_api * api, int which); -Uint32 emboss_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 emboss_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} // No setup required: @@ -73,118 +73,125 @@ int emboss_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/emboss.ogg", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/emboss.ogg", api->data_directory); emboss_snd = Mix_LoadWAV(fname); - return(1); + return (1); } // We have multiple tools: int emboss_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(1); + return (1); } // Load our icons: -SDL_Surface * emboss_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *emboss_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/emboss.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/emboss.png", api->data_directory); - return(IMG_Load(fname)); + 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 (strdup(gettext_noop("Emboss"))); } // Return our descriptions, localized: -char * emboss_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *emboss_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 emboss the picture."))); + return (strdup(gettext_noop("Click and drag the mouse to emboss the picture."))); } // Do the effect: -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; + magic_api *api = (magic_api *) ptr; int xx, yy; - Uint8 r1, g1, b1, - r2, g2, b2; + Uint8 r1, g1, b1, r2, g2, b2; int r, g, b; float h, s, v; int avg1, avg2; for (yy = -16; yy < 16; yy++) - { - for (xx = -16; xx < 16; xx++) { - if (api->in_circle(xx, yy, 16)) - { - if (!api->touched(x + xx, y + yy)) + for (xx = -16; xx < 16; xx++) { - SDL_GetRGB(api->getpixel(last, x + xx, y + yy), last->format, &r1, &g1, &b1); - SDL_GetRGB(api->getpixel(last, x + xx + 2, y + yy + 2), last->format, &r2, &g2, &b2); + if (api->in_circle(xx, yy, 16)) + { + if (!api->touched(x + xx, y + yy)) + { + SDL_GetRGB(api->getpixel(last, x + xx, y + yy), last->format, &r1, &g1, &b1); + SDL_GetRGB(api->getpixel(last, x + xx + 2, y + yy + 2), last->format, &r2, &g2, &b2); - avg1 = (r1 + g1 + b1) / 3; - avg2 = (r2 + g2 + b2) / 3; + avg1 = (r1 + g1 + b1) / 3; + avg2 = (r2 + g2 + b2) / 3; - api->rgbtohsv(r1, g1, b1, &h, &s, &v); + api->rgbtohsv(r1, g1, b1, &h, &s, &v); - r = 128 + (((avg1 - avg2) * 3) / 2); - if (r < 0) r = 0; - if (r > 255) r = 255; - g = b = r; + r = 128 + (((avg1 - avg2) * 3) / 2); + if (r < 0) + r = 0; + if (r > 255) + r = 255; + g = b = r; - v = (r / 255.0); + v = (r / 255.0); - api->hsvtorgb(h, s, v, &r1, &g1, &b1); + api->hsvtorgb(h, s, v, &r1, &g1, &b1); - api->putpixel(canvas, x + xx, y + yy, SDL_MapRGB(canvas->format, r1, g1, b1)); + api->putpixel(canvas, x + xx, y + yy, SDL_MapRGB(canvas->format, r1, g1, b1)); + } + } } - } } - } } // 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; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - 16; update_rect->y = oy - 16; update_rect->w = (x + 16) - update_rect->x; update_rect->h = (y + 16) - update_rect->h; - api->playsound(emboss_snd, - (x * 255) / canvas->w, 255); + api->playsound(emboss_snd, (x * 255) / canvas->w, 255); } // Affect the canvas on click: void emboss_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) { emboss_drag(api, which, canvas, last, x, y, x, y, update_rect); } // 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -196,7 +203,8 @@ 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, Uint8 b ATTRIBUTE_UNUSED) +void emboss_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, + Uint8 b ATTRIBUTE_UNUSED) { } @@ -206,15 +214,17 @@ int emboss_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE return 0; } -void emboss_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas 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, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void emboss_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } int emboss_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_PAINT); /* FIXME - Can also be turned into a full-image effect */ + return (MODE_PAINT); /* FIXME - Can also be turned into a full-image effect */ } diff --git a/magic/src/fade_darken.c b/magic/src/fade_darken.c index 4c97f03b0..e681998d3 100644 --- a/magic/src/fade_darken.c +++ b/magic/src/fade_darken.c @@ -33,13 +33,14 @@ #include "SDL_image.h" #include "SDL_mixer.h" -enum { +enum +{ TOOL_FADE, TOOL_DARKEN, NUM_TOOLS }; -static Mix_Chunk * snd_effects[NUM_TOOLS]; +static Mix_Chunk *snd_effects[NUM_TOOLS]; /* Local function prototypes: */ @@ -47,24 +48,17 @@ static Mix_Chunk * snd_effects[NUM_TOOLS]; int fade_darken_init(magic_api * api); Uint32 fade_darken_api_version(void); int fade_darken_get_tool_count(magic_api * api); -SDL_Surface * fade_darken_get_icon(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); +SDL_Surface *fade_darken_get_icon(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); 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); + 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_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); @@ -76,139 +70,144 @@ int fade_darken_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/fade.wav", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/fade.wav", api->data_directory); snd_effects[TOOL_FADE] = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%s/sounds/magic/darken.wav", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/darken.wav", api->data_directory); snd_effects[TOOL_DARKEN] = Mix_LoadWAV(fname); - return(1); + return (1); } -Uint32 fade_darken_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 fade_darken_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} // Multiple tools: int fade_darken_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(NUM_TOOLS); + return (NUM_TOOLS); } // Load our icon: -SDL_Surface * fade_darken_get_icon(magic_api * api, int which) +SDL_Surface *fade_darken_get_icon(magic_api * api, int which) { char fname[1024]; if (which == TOOL_FADE) - { - snprintf(fname, sizeof(fname), "%s/images/magic/fade.png", - api->data_directory); - } + { + snprintf(fname, sizeof(fname), "%s/images/magic/fade.png", api->data_directory); + } else if (which == TOOL_DARKEN) - { - snprintf(fname, sizeof(fname), "%s/images/magic/darken.png", - api->data_directory); - } + { + snprintf(fname, sizeof(fname), "%s/images/magic/darken.png", api->data_directory); + } - return(IMG_Load(fname)); + return (IMG_Load(fname)); } // Return our name, localized: -char * fade_darken_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) +char *fade_darken_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) { if (which == TOOL_FADE) - return(strdup(gettext_noop("Lighten"))); + return (strdup(gettext_noop("Lighten"))); else if (which == TOOL_DARKEN) - return(strdup(gettext_noop("Darken"))); + return (strdup(gettext_noop("Darken"))); - return(NULL); + return (NULL); } // 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); + 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; + magic_api *api = (magic_api *) ptr; 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; + magic_api *api = (magic_api *) ptr; for (yy = y - 16; yy < y + 16; yy++) - { - 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); - } + 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); + } + } } - } } // 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); api->playsound(snd_effects[which], (x * 255) / canvas->w, 255); - if (ox > x) { int tmp = ox; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - 16; update_rect->y = oy - 16; @@ -219,32 +218,31 @@ 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -260,7 +258,7 @@ 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) { } @@ -270,15 +268,17 @@ int fade_darken_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTR return 0; } -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_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, SDL_Surface * canvas 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) { - return(MODE_PAINT | MODE_FULLSCREEN); + return (MODE_PAINT | MODE_FULLSCREEN); } diff --git a/magic/src/fill.c b/magic/src/fill.c index c2922d62e..75f4fda12 100644 --- a/magic/src/fill.c +++ b/magic/src/fill.c @@ -39,15 +39,13 @@ /* Our globals: */ -static Mix_Chunk * fill_snd; +static Mix_Chunk *fill_snd; static Uint8 fill_r, fill_g, fill_b; /* Local function prototypes: */ -static int colors_close(magic_api * api, SDL_Surface * canvas, - Uint32 c1, Uint32 c2); -static void do_flood_fill(magic_api * api, SDL_Surface * canvas, int x, int y, - Uint32 cur_colr, Uint32 old_colr); +static int colors_close(magic_api * api, SDL_Surface * canvas, Uint32 c1, Uint32 c2); +static void do_flood_fill(magic_api * api, SDL_Surface * canvas, int x, int y, Uint32 cur_colr, Uint32 old_colr); int fill_modes(magic_api * api, int which); void fill_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); void fill_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas); @@ -55,18 +53,15 @@ int fill_requires_colors(magic_api * api, int which); void fill_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); void fill_shutdown(magic_api * api); void fill_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 fill_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 fill_drag(magic_api * api, int which, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, - SDL_Rect * update_rect); -char * fill_get_description(magic_api * api, int which, int mode); -char * fill_get_name(magic_api * api, int which); + SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect); +char *fill_get_description(magic_api * api, int which, int mode); +char *fill_get_name(magic_api * api, int which); int fill_get_tool_count(magic_api * api); -SDL_Surface * fill_get_icon(magic_api * api, int which); +SDL_Surface *fill_get_icon(magic_api * api, int which); Uint32 fill_api_version(void); int fill_init(magic_api * api); @@ -76,61 +71,59 @@ int fill_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/fill.wav", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/fill.wav", api->data_directory); fill_snd = Mix_LoadWAV(fname); - return(1); + return (1); } -Uint32 fill_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 fill_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} // We have multiple tools: int fill_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(1); + return (1); } // Load our icons: -SDL_Surface * fill_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *fill_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/fill.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/fill.png", api->data_directory); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } // Return our names, localized: -char * fill_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *fill_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(strdup(gettext_noop("Fill"))); + return (strdup(gettext_noop("Fill"))); } // Return our descriptions, localized: -char * fill_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *fill_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) { - return(strdup(gettext_noop( -"Click in the picture to fill that area with color."))); + return (strdup(gettext_noop("Click in the picture to fill that area with color."))); } // Affect the canvas on drag: void fill_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) + 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) { } // Affect the canvas on click: void fill_click(magic_api * api, 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x, int y, SDL_Rect * update_rect) { - do_flood_fill(api, canvas, x, y, SDL_MapRGB(canvas->format, - fill_r, fill_g, fill_b), - api->getpixel(canvas, x, y)); + do_flood_fill(api, canvas, x, y, SDL_MapRGB(canvas->format, fill_r, fill_g, fill_b), api->getpixel(canvas, x, y)); update_rect->x = 0; update_rect->y = 0; @@ -139,8 +132,8 @@ void fill_click(magic_api * api, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_ } void fill_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) { } @@ -164,42 +157,41 @@ int fill_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_U } -static int colors_close(magic_api * api, SDL_Surface * canvas, - Uint32 c1, Uint32 c2) +static int colors_close(magic_api * api, 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 1; - } + return 1; + } else - { - double r, g, b; - SDL_GetRGB(c1, canvas->format, &r1, &g1, &b1); - SDL_GetRGB(c2, canvas->format, &r2, &g2, &b2); + { + double r, g, b; - // use distance in linear RGB space - r = api->sRGB_to_linear(r1) - api->sRGB_to_linear(r2); - r *= r; - g = api->sRGB_to_linear(g1) - api->sRGB_to_linear(g2); - g *= g; - b = api->sRGB_to_linear(b1) - api->sRGB_to_linear(b2); - b *= b; + SDL_GetRGB(c1, canvas->format, &r1, &g1, &b1); + SDL_GetRGB(c2, canvas->format, &r2, &g2, &b2); - // easy to confuse: - // dark grey, brown, purple - // light grey, tan - // red, orange - return r + g + b < 0.04; - } + // use distance in linear RGB space + r = api->sRGB_to_linear(r1) - api->sRGB_to_linear(r2); + r *= r; + g = api->sRGB_to_linear(g1) - api->sRGB_to_linear(g2); + g *= g; + b = api->sRGB_to_linear(b1) - api->sRGB_to_linear(b2); + b *= b; + + // easy to confuse: + // dark grey, brown, purple + // light grey, tan + // red, orange + return r + g + b < 0.04; + } } -static void do_flood_fill(magic_api * api, SDL_Surface * canvas, int x, int y, - Uint32 cur_colr, Uint32 old_colr) +static void do_flood_fill(magic_api * api, SDL_Surface * canvas, int x, int y, Uint32 cur_colr, Uint32 old_colr) { int fillL, fillR, i, in_line; static unsigned char prog_anim; @@ -214,10 +206,10 @@ static void do_flood_fill(magic_api * api, SDL_Surface * canvas, int x, int y, prog_anim++; if ((prog_anim % 4) == 0) - { - api->update_progress_bar(); - api->playsound(fill_snd, (x * 255) / canvas->w, 255); - } + { + api->update_progress_bar(); + api->playsound(fill_snd, (x * 255) / canvas->w, 255); + } /* Find left side, filling along the way */ @@ -225,15 +217,12 @@ static void do_flood_fill(magic_api * api, SDL_Surface * canvas, int x, int y, in_line = 1; while (in_line) - { - api->putpixel(canvas, fillL, y, cur_colr); - fillL--; + { + api->putpixel(canvas, fillL, y, cur_colr); + fillL--; - in_line = - (fillL < 0) ? 0 : colors_close(api, canvas, - api->getpixel(canvas, fillL, y), - old_colr); - } + in_line = (fillL < 0) ? 0 : colors_close(api, canvas, api->getpixel(canvas, fillL, y), old_colr); + } fillL++; @@ -241,14 +230,12 @@ static void do_flood_fill(magic_api * api, SDL_Surface * canvas, int x, int y, in_line = 1; while (in_line) - { - api->putpixel(canvas, fillR, y, cur_colr); - fillR++; + { + api->putpixel(canvas, fillR, y, cur_colr); + fillR++; - in_line = (fillR >= canvas->w) ? 0 : colors_close(api, canvas, - api->getpixel(canvas, fillR, y), - old_colr); - } + in_line = (fillR >= canvas->w) ? 0 : colors_close(api, canvas, api->getpixel(canvas, fillR, y), old_colr); + } fillR--; @@ -256,26 +243,26 @@ static void do_flood_fill(magic_api * api, SDL_Surface * canvas, int x, int y, /* Search top and bottom */ for (i = fillL; i <= fillR; i++) - { - if (y > 0 && colors_close(api, canvas, api->getpixel(canvas, i, y - 1), - old_colr)) - do_flood_fill(api, canvas, i, y - 1, cur_colr, old_colr); + { + if (y > 0 && colors_close(api, canvas, api->getpixel(canvas, i, y - 1), old_colr)) + do_flood_fill(api, canvas, i, y - 1, cur_colr, old_colr); - if (y < canvas->h - && colors_close(api, canvas, api->getpixel(canvas, i, y + 1), old_colr)) - do_flood_fill(api, canvas, i, y + 1, cur_colr, old_colr); - } + if (y < canvas->h && colors_close(api, canvas, api->getpixel(canvas, i, y + 1), old_colr)) + do_flood_fill(api, canvas, i, y + 1, cur_colr, old_colr); + } } -void fill_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void fill_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void fill_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void fill_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } int fill_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_PAINT); + return (MODE_PAINT); } diff --git a/magic/src/fisheye.c b/magic/src/fisheye.c index 9f3182dce..80d01f347 100644 --- a/magic/src/fisheye.c +++ b/magic/src/fisheye.c @@ -31,7 +31,7 @@ #include "SDL_image.h" #include "SDL_mixer.h" -Mix_Chunk * fisheye_snd; +Mix_Chunk *fisheye_snd; int last_x, last_y; /* Local function prototypes */ @@ -39,39 +39,35 @@ Uint32 fisheye_api_version(void); void fisheye_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int fisheye_init(magic_api * api); int fisheye_get_tool_count(magic_api * api); -SDL_Surface * fisheye_get_icon(magic_api * api, int which); -char * fisheye_get_name(magic_api * api, int which); -char * fisheye_get_description(magic_api * api, int which, int mode); +SDL_Surface *fisheye_get_icon(magic_api * api, int which); +char *fisheye_get_name(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); + 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); + 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 +// 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); + return (TP_MAGIC_API_VERSION); } -void fisheye_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) +void fisheye_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, + Uint8 b ATTRIBUTE_UNUSED) { } @@ -79,11 +75,11 @@ void fisheye_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSE int fisheye_init(magic_api * api) { char fname[1024]; - - snprintf(fname, sizeof(fname), "%s/sounds/magic/fisheye.ogg", api->data_directory); - fisheye_snd = Mix_LoadWAV(fname); - return(1); + snprintf(fname, sizeof(fname), "%s/sounds/magic/fisheye.ogg", api->data_directory); + fisheye_snd = Mix_LoadWAV(fname); + + return (1); } int fisheye_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) @@ -91,186 +87,203 @@ int fisheye_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) return 1; } -SDL_Surface * fisheye_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *fisheye_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/fisheye.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/fisheye.png", api->data_directory); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } -char * fisheye_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { return strdup(gettext_noop("Fisheye")); } //Needs better name +char *fisheye_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +{ + return strdup(gettext_noop("Fisheye")); +} //Needs better name -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.")); } +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.")); +} -int fisheye_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { return 0; } +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) + 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_shutdown(magic_api * api ATTRIBUTE_UNUSED) - { Mix_FreeChunk(fisheye_snd); } +{ + Mix_FreeChunk(fisheye_snd); +} // 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; - - SDL_Surface * oryg, *temp_src, *temp_dest, *output; - SDL_Rect rect, temp_rect; - int xx, yy; - unsigned short int i; + magic_api *api = (magic_api *) ptr; - if(api->in_circle(last_x - x, last_y - y, 80)) return; + SDL_Surface *oryg, *temp_src, *temp_dest, *output; + SDL_Rect rect, temp_rect; + int xx, yy; + unsigned short int i; - last_x = x; - last_y = y; + if (api->in_circle(last_x - x, last_y - y, 80)) + return; - oryg=SDL_CreateRGBSurface(SDL_SWSURFACE, 80, 80, canvas->format->BitsPerPixel, - canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, canvas->format->Amask); + last_x = x; + last_y = y; - 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; - rect.w=rect.h=80; - - SDL_BlitSurface(canvas, &rect, oryg, NULL); //here we have a piece of source image. Now we've to scale it (keeping aspect ratio) - - //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); - - //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 - - 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_rect.x=0; - temp_rect.y=i; - temp_rect.w=1; - temp_rect.h=80; + oryg = SDL_CreateRGBSurface(SDL_SWSURFACE, 80, 80, canvas->format->BitsPerPixel, + canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, + canvas->format->Amask); - SDL_BlitSurface(temp_dest, &temp_rect, output, &rect); //let's copy it to output - - //right side then - - rect.x=79-i; - - 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 + output = SDL_CreateRGBSurface(SDL_SWSURFACE, 80, 80, canvas->format->BitsPerPixel, + canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, + canvas->format->Amask); - 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_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; - - temp_rect.x=i; - temp_rect.y=0; - temp_rect.w=80; - temp_rect.h=1; - - 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); - - //lower side then + rect.x = x - 40; + rect.y = y - 40; + rect.w = rect.h = 80; - 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); - } + SDL_BlitSurface(canvas, &rect, oryg, NULL); //here we have a piece of source image. Now we've to scale it (keeping aspect ratio) - rect.x=x-40; - rect.y=y-40; - rect.w=rect.h=80; + //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); - //let's blit an area surrounded by a circle + //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; - for (yy = y-40; yy < y+40; yy++) - for (xx = x-40; xx < x+40; xx++) + SDL_BlitSurface(oryg, &rect, temp_src, NULL); //this bar is copied to temp_src - if (api->in_circle(xx-x, yy-y, 40)) - api->putpixel(canvas, xx, yy, api->getpixel(output, xx+40-x, yy+40-y)); + 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_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 + + //right side then + + rect.x = 79 - i; + + 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 + + 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_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; + + temp_rect.x = i; + temp_rect.y = 0; + temp_rect.w = 80; + temp_rect.h = 1; + + 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); + + //lower side then + + 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); + } + + rect.x = x - 40; + rect.y = y - 40; + rect.w = rect.h = 80; + + //let's blit an area surrounded by a circle + + for (yy = y - 40; yy < y + 40; yy++) + 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)); - SDL_FreeSurface(oryg); - SDL_FreeSurface(output); - SDL_FreeSurface(temp_dest); - SDL_FreeSurface(temp_src); - - api->playsound(fisheye_snd, (x * 255) / canvas->w,255); + SDL_FreeSurface(oryg); + SDL_FreeSurface(output); + SDL_FreeSurface(temp_dest); + SDL_FreeSurface(temp_src); + + api->playsound(fisheye_snd, (x * 255) / canvas->w, 255); } 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); - update_rect->x = min(ox, x) - 40; - update_rect->y = min(oy, y) - 40; - update_rect->w = max(ox, x) - update_rect->x + 40; - update_rect->h = max(oy, y) - update_rect->y + 40; + api->line(api, which, canvas, snapshot, ox, oy, x, y, 1, fisheye_draw); + update_rect->x = min(ox, x) - 40; + update_rect->y = min(oy, y) - 40; + update_rect->w = max(ox, x) - update_rect->x + 40; + update_rect->h = max(oy, y) - update_rect->y + 40; } 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); + 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, SDL_Surface * canvas 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, SDL_Surface * canvas 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) { - return(MODE_PAINT); + return (MODE_PAINT); } diff --git a/magic/src/flower.c b/magic/src/flower.c index 2f0987da6..89688d79e 100644 --- a/magic/src/flower.c +++ b/magic/src/flower.c @@ -36,19 +36,21 @@ /* Our globals: */ -enum { SIDE_LEFT, SIDE_RIGHT }; -enum { LEAFSIDE_RIGHT_DOWN, - LEAFSIDE_LEFT_DOWN, - LEAFSIDE_RIGHT_UP, - LEAFSIDE_LEFT_UP }; +enum +{ SIDE_LEFT, SIDE_RIGHT }; +enum +{ LEAFSIDE_RIGHT_DOWN, + LEAFSIDE_LEFT_DOWN, + LEAFSIDE_RIGHT_UP, + LEAFSIDE_LEFT_UP +}; -static Mix_Chunk * flower_click_snd, * flower_release_snd; +static Mix_Chunk *flower_click_snd, *flower_release_snd; 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: */ @@ -59,36 +61,31 @@ typedef struct 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 Point2D flower_PointOnCubicBezier(Point2D* cp, float t); -static void flower_ComputeBezier(Point2D* cp, int numberOfPoints, Point2D* curve); +static Point2D flower_PointOnCubicBezier(Point2D * cp, float t); +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); int flower_get_tool_count(magic_api * api); -SDL_Surface * flower_get_icon(magic_api * api, int which); -char * flower_get_name(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); +SDL_Surface *flower_get_icon(magic_api * api, int which); +char *flower_get_name(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); 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); + 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); + 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); + 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 Point2D flower_PointOnCubicBezier(Point2D * cp, float t); +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); @@ -96,7 +93,10 @@ int flower_modes(magic_api * api, int which); -Uint32 flower_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 flower_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} // No setup required: @@ -104,61 +104,55 @@ int flower_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/flower_click.ogg", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/flower_click.ogg", api->data_directory); flower_click_snd = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%s/sounds/magic/flower_release.ogg", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/flower_release.ogg", api->data_directory); flower_release_snd = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%s/images/magic/flower_base.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/flower_base.png", api->data_directory); flower_base = IMG_Load(fname); - snprintf(fname, sizeof(fname), "%s/images/magic/flower_leaf.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/flower_leaf.png", api->data_directory); flower_leaf = IMG_Load(fname); - snprintf(fname, sizeof(fname), "%s/images/magic/flower_petals.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/flower_petals.png", api->data_directory); flower_petals = IMG_Load(fname); - return(1); + return (1); } // We have multiple tools: int flower_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(1); + return (1); } // Load our icons: -SDL_Surface * flower_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *flower_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/flower.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/flower.png", api->data_directory); - return(IMG_Load(fname)); + 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 (strdup(gettext_noop("Flower"))); } // 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) + SDL_Surface * last ATTRIBUTE_UNUSED, int ox, int oy, int x, int y) { if (x < flower_min_x) flower_min_x = x; @@ -177,23 +171,22 @@ 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) { - flower_side_first = SIDE_LEFT; - flower_side_decided = 1; + 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; + } } - 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) + SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect) { flower_predrag(api, canvas, last, ox, oy, x, y); @@ -206,21 +199,19 @@ 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); update_rect->x = 0; update_rect->y = 0; - update_rect->w = canvas->w; + update_rect->w = canvas->w; update_rect->h = canvas->h; } // Affect the canvas on click: void 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; @@ -231,14 +222,13 @@ void flower_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, flower_side_first = SIDE_LEFT; flower_drag(api, which, canvas, last, x, y, x, y, update_rect); - + api->playsound(flower_click_snd, (x * 255) / canvas->w, 255); } // 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: */ @@ -250,7 +240,7 @@ void flower_release(magic_api * api, int which ATTRIBUTE_UNUSED, flower_predrag(api, canvas, last, x, y, x, y); - + /* Erase any old stuff: */ SDL_BlitSurface(last, NULL, canvas, NULL); @@ -258,15 +248,13 @@ 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); flower_drawbase(api, canvas); - + update_rect->x = 0; update_rect->y = 0; update_rect->w = canvas->w; @@ -297,11 +285,10 @@ static void flower_drawbase(magic_api * api ATTRIBUTE_UNUSED, SDL_Surface * canv } 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) + int top_x, int top_y, int minx, int maxx, int bottom_x, int bottom_y, int final) { Point2D control_points[4]; - Point2D * curve; + Point2D *curve; int i, n_points; int left, right; SDL_Rect dest, src; @@ -315,22 +302,22 @@ 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; - + control_points[3].x = bottom_x; control_points[3].y = bottom_y; - + if (final == 0) n_points = 8; else @@ -342,129 +329,128 @@ 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) { - 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 (abs(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++) + if (final == 0) { - 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); + dest.y = curve[i].y; + dest.w = 2; + dest.h = 2; } - } - else if (side == LEAFSIDE_LEFT_UP) - { - for (xx = 0; xx < flower_leaf->w; xx++) + else { - for (yy = 0; yy < flower_leaf->h; yy++) - { - src.x = xx; - src.y = yy; - src.w = 1; - src.h = 1; + left = min(curve[i].x, curve[i + 1].x); + right = max(curve[i].x, curve[i + 1].x); - dest.x = curve[i].x - xx; - dest.y = curve[i].y - yy; - - SDL_BlitSurface(flower_leaf, &src, canvas, &dest); - } + 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 (abs(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); } @@ -518,32 +504,32 @@ cp[3] is the end point, or P3 in the above diagram t is the parameter value, 0 <= t <= 1 */ -static Point2D flower_PointOnCubicBezier( Point2D* cp, float t ) +static Point2D flower_PointOnCubicBezier(Point2D * cp, float t) { - float ax, bx, cx; - float ay, by, cy; - float tSquared, tCubed; - Point2D result; + float ax, bx, cx; + float ay, by, cy; + float tSquared, tCubed; + Point2D result; - /* calculate the polynomial coefficients */ + /* calculate the polynomial coefficients */ - cx = 3.0 * (cp[1].x - cp[0].x); - bx = 3.0 * (cp[2].x - cp[1].x) - cx; - ax = cp[3].x - cp[0].x - cx - bx; - - cy = 3.0 * (cp[1].y - cp[0].y); - by = 3.0 * (cp[2].y - cp[1].y) - cy; - ay = cp[3].y - cp[0].y - cy - by; - - /* calculate the curve point at parameter value t */ - - tSquared = t * t; - tCubed = tSquared * t; - - result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x; - result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y; - - return result; + cx = 3.0 * (cp[1].x - cp[0].x); + bx = 3.0 * (cp[2].x - cp[1].x) - cx; + ax = cp[3].x - cp[0].x - cx - bx; + + cy = 3.0 * (cp[1].y - cp[0].y); + by = 3.0 * (cp[2].y - cp[1].y) - cy; + ay = cp[3].y - cp[0].y - cy - by; + + /* calculate the curve point at parameter value t */ + + tSquared = t * t; + tCubed = tSquared * t; + + result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x; + result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y; + + return result; } /* @@ -553,15 +539,15 @@ 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; + float dt; + int i; - dt = 1.0 / ( numberOfPoints - 1 ); + dt = 1.0 / (numberOfPoints - 1); - for( i = 0; i < numberOfPoints; i++) - curve[i] = flower_PointOnCubicBezier( cp, i*dt ); + for (i = 0; i < numberOfPoints; i++) + curve[i] = flower_PointOnCubicBezier(cp, i * dt); } @@ -576,9 +562,7 @@ 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, @@ -586,8 +570,7 @@ 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: */ @@ -595,40 +578,37 @@ 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++) { - SDL_GetRGBA(api->getpixel(flower_petals, x, y), - flower_petals->format, &r, &g, &b, &a); + for (x = 0; x < flower_petals->w; x++) + { + 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, SDL_Surface * canvas 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, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void flower_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } int flower_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_PAINT_WITH_PREVIEW); + return (MODE_PAINT_WITH_PREVIEW); } diff --git a/magic/src/foam.c b/magic/src/foam.c index a4e86631e..2537e6898 100644 --- a/magic/src/foam.c +++ b/magic/src/foam.c @@ -36,26 +36,23 @@ /* Our globals: */ -static Mix_Chunk * foam_snd; +static Mix_Chunk *foam_snd; static Uint8 foam_r, foam_g, foam_b; static int foam_mask_w, foam_mask_h; -static int * foam_mask, * foam_mask_tmp; -static SDL_Surface * foam_7, * foam_5, * foam_3, * foam_1; +static int *foam_mask, *foam_mask_tmp; +static SDL_Surface *foam_7, *foam_5, *foam_3, *foam_1; Uint32 foam_api_version(void); int foam_init(magic_api * api); -char * foam_get_description(magic_api * api, int which, int mode); +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); + 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); + 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); 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); @@ -67,107 +64,113 @@ int foam_requires_colors(magic_api * api, int which); #define FOAM_PROP 8 #define FOAM_RADIUS 3 -Uint32 foam_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 foam_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} // No setup required: int foam_init(magic_api * api) { char fname[1024]; - SDL_Surface * foam_data; + SDL_Surface *foam_data; - snprintf(fname, sizeof(fname), "%s/sounds/magic/foam.ogg", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/foam.ogg", api->data_directory); foam_snd = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%s/images/magic/foam_data.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/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); - - return(1); + + return (1); } // We have multiple tools: int foam_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(1); + return (1); } // Load our icons: -SDL_Surface * foam_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *foam_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/foam.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/foam.png", api->data_directory); - return(IMG_Load(fname)); + 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 (strdup(gettext_noop("Foam"))); } // 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, SDL_Surface * last ATTRIBUTE_UNUSED, - int x, int y) +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; + magic_api *api = (magic_api *) ptr; int xx, yy, nx, ny; + /* SDL_Rect dest; */ for (yy = -FOAM_RADIUS; yy < FOAM_RADIUS; yy++) - { - 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 (nx >= 0 && ny >= 0 && - nx < foam_mask_w && - ny < foam_mask_h) + for (xx = -FOAM_RADIUS; xx < FOAM_RADIUS; xx++) { - foam_mask[ny * foam_mask_w + nx] = 1; + 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; + } + } } - } } - } } // 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; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = 0; update_rect->y = 0; @@ -179,19 +182,19 @@ 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) + 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; @@ -207,30 +210,30 @@ 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++) { - 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; - } + 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; + } + } } - } - return(tot); + 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) + 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; SDL_Rect dest; int n; - SDL_Surface * img; + SDL_Surface *img; SDL_BlitSurface(last, NULL, canvas, NULL); @@ -240,161 +243,161 @@ void foam_release(magic_api * api ATTRIBUTE_UNUSED ATTRIBUTE_UNUSED, int which A max_iters = 2; do - { - 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) + changes = 0; + max_iters--; + + for (yy = 0; yy < foam_mask_h - 7; yy++) { - 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; + 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 + 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; + 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; + } + } } - 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++) { - n = foam_mask[yy * foam_mask_w + xx]; + for (xx = 0; xx < 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 (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 (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; - SDL_BlitSurface(img, NULL, canvas, &dest); - } + 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); + } + } } - } - + memcpy(foam_mask, foam_mask_tmp, (sizeof(int) * (foam_mask_w * foam_mask_h))); - + update_rect->x = 0; update_rect->y = 0; update_rect->w = canvas->w; @@ -409,7 +412,7 @@ void foam_shutdown(magic_api * api ATTRIBUTE_UNUSED) if (foam_mask != NULL) free(foam_mask); - + if (foam_1 != NULL) SDL_FreeSurface(foam_1); if (foam_3 != NULL) @@ -431,18 +434,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) { - return 0; /* FIXME: Would be nice to tint the bubbles */ + 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, SDL_Surface * canvas 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, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void foam_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } int foam_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_PAINT); + return (MODE_PAINT); } diff --git a/magic/src/fold.c b/magic/src/fold.c index a8afa3641..60e8f07f6 100644 --- a/magic/src/fold.c +++ b/magic/src/fold.c @@ -15,76 +15,67 @@ int fold_ox, fold_oy; int fold_x, fold_y; Uint8 fold_shadow_value; Uint8 corner; -Mix_Chunk * fold_snd; +Mix_Chunk *fold_snd; Uint8 fold_r, fold_g, fold_b; Uint32 fold_color; -SDL_Surface * fold_surface_src, * fold_surface_dst; +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); -void translate_coords(SDL_Surface * canvas,int angle); -SDL_Surface * rotate(magic_api * api, SDL_Surface * canvas, int angle); + 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); int fold_get_tool_count(magic_api * api); -SDL_Surface * fold_get_icon(magic_api * api, int which); -char * fold_get_name(magic_api * api, int which); -char * fold_get_description(magic_api * api, int which, int mode); +SDL_Surface *fold_get_icon(magic_api * api, int which); +char *fold_get_name(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 + +// 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); + 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); Uint32 fold_api_version(void) - { - return(TP_MAGIC_API_VERSION); + return (TP_MAGIC_API_VERSION); } -void fold_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) //get the colors from API and store it in structure +void fold_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) //get the colors from API and store it in structure { - fold_r=r; - fold_g=g; - fold_b=b; + fold_r = r; + fold_g = g; + fold_b = b; } int fold_init(magic_api * api) { char fname[1024]; - - snprintf(fname, sizeof(fname), "%s/sounds/magic/fold.wav", api->data_directory); - fold_snd = Mix_LoadWAV(fname); - - return(1); + + snprintf(fname, sizeof(fname), "%s/sounds/magic/fold.wav", api->data_directory); + fold_snd = Mix_LoadWAV(fname); + + return (1); } int fold_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) @@ -92,386 +83,421 @@ 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), "%s/images/magic/fold.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/fold.png", api->data_directory); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } -char * fold_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { return(gettext_noop("Fold")); } - -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.")); } - -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) +char *fold_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - 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)); + return (gettext_noop("Fold")); +} + +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.")); +} + +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) +{ + 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)); } 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; int left_y, right_x; 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); - SDL_BlitSurface(canvas,0,temp,0); + SDL_Surface *temp; - 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); - for (w=0;w < canvas->w;w+=0.5) - for(h=0;h < canvas->h;h+=0.5) + 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); + for (w = 0; w < canvas->w; w += 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) { - left_y=(float)right_arm_y/left_arm_x*(left_arm_x-canvas->w); + left_y = (float)right_arm_y / left_arm_x * (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); + 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) { - right_x=(float)left_arm_x/right_arm_y*(right_arm_y-canvas->h); + right_x = (float)left_arm_x / right_arm_y * (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); + 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); + 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); - SDL_BlitSurface(canvas,0,temp,0); + 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); + SDL_BlitSurface(canvas, 0, temp, 0); - for (fold_shadow_value = 0; fold_shadow_value < 40; fold_shadow_value+=1) + 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, left_arm_x, left_arm_y, right_arm_x, right_arm_y, 1, + fold_print_dark_line); } -SDL_Surface * rotate(magic_api * api, SDL_Surface * canvas, int angle) +SDL_Surface *rotate(magic_api * api, SDL_Surface * canvas, int angle) { - SDL_Surface * temp; - int x,y; - int a,b; + SDL_Surface *temp; + int x, y; + 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); + 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); 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; xw; x++) - for (y=0; yh; y++) - { - translate_xy(canvas,x,y,&a,&b,90); - api->putpixel(temp,a,b,api->getpixel(canvas, x, y)); - } - break; - + 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; xw; x++) - for (y=0; yh; y++) - { - translate_xy(canvas,x,y,&a,&b,180); - api->putpixel(temp,a,b,api->getpixel(canvas, x, y)); - } + 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; xw; x++) - for (y=0; yh; y++) - { - translate_xy(canvas,x,y,&a,&b,270); - api->putpixel(temp,a,b,api->getpixel(canvas, x, y)); - } + 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; } - void translate_coords(SDL_Surface * canvas,int angle) +void translate_coords(SDL_Surface * canvas, int angle) { - int a,b; + 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; + *a = y; + *b = canvas->w - 1 - x; break; case 180: - *a=canvas->w -1 -x; - *b=canvas->h -1 -y; + *a = canvas->w - 1 - x; + *b = canvas->h - 1 - y; break; case 270: - *a=canvas->h -1 -y; - *b=x; - break; + *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; + int a, b; + SDL_Surface *temp, *temp2; - x=fold_x; - y=fold_y; - fold_ox=fold_oy=0; + x = fold_x; + y = fold_y; + 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); + 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); + 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; + 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; + 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; - update_rect->h=canvas->h; + update_rect->x = update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; api->playsound(fold_snd, (x * 255) / canvas->w, 255); } -void fold_shutdown(magic_api * api ATTRIBUTE_UNUSED) - { - Mix_FreeChunk(fold_snd); - SDL_FreeSurface(fold_surface_dst); - SDL_FreeSurface(fold_surface_src); - } +void fold_shutdown(magic_api * api ATTRIBUTE_UNUSED) +{ + Mix_FreeChunk(fold_snd); + SDL_FreeSurface(fold_surface_dst); + SDL_FreeSurface(fold_surface_src); +} // Interactivity functions 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; - } - else - { - if (y>=canvas->h/2) return 3; - else return 2; - } + if (x >= canvas->w / 2) + { + if (y >= canvas->h / 2) + return 4; + else + return 1; + } + else + { + 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) -{ - 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. + 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_erase(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(canvas->format, fold_r, fold_g, fold_b)); + 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) +{ + magic_api *api = (magic_api *) ptr; + + 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); - + 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; + fold_ox = canvas->w - 1; + fold_oy = 0; break; case 2: - fold_ox=fold_oy=0; + fold_ox = fold_oy = 0; break; case 3: - fold_ox=0; - fold_oy=canvas->h-1; + fold_ox = 0; + fold_oy = canvas->h - 1; break; case 4: - fold_ox=canvas->w-1; - fold_oy=canvas->h-1; + fold_ox = canvas->w - 1; + fold_oy = canvas->h - 1; break; } - fold_drag(api, which, canvas, snapshot, x, y, x, y, update_rect); + 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_Rect * update_rect) + SDL_Surface * snapshot, int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, int x, int y, + SDL_Rect * update_rect) { int middle_point_x; int middle_point_y; - fold_x=x; - fold_y=y; - SDL_BlitSurface(snapshot,0,canvas,0); + fold_x = x; + fold_y = y; + SDL_BlitSurface(snapshot, 0, canvas, 0); - middle_point_x=(fold_ox+x)/2; - middle_point_y=(fold_oy+y)/2; + middle_point_x = (fold_ox + x) / 2; + middle_point_y = (fold_oy + y) / 2; - switch(corner) + 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); + 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; - - 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); + left_arm_x = middle_point_x + middle_point_y * middle_point_y / middle_point_x; + left_arm_y = fold_oy; 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 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-(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; + 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); + + 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; } @@ -479,30 +505,31 @@ void fold_preview(magic_api * api, int which, SDL_Surface * canvas, 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; - update_rect->h=canvas->h; + update_rect->x = update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; } 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); - y=clamp(2,y,canvas->h-2); + x = clamp(2, x, canvas->w - 2); + y = clamp(2, y, canvas->h - 2); 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, SDL_Surface * canvas 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, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void fold_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } int fold_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_PAINT_WITH_PREVIEW); + return (MODE_PAINT_WITH_PREVIEW); } diff --git a/magic/src/fretwork.c b/magic/src/fretwork.c index ef59e851d..90a8814a8 100644 --- a/magic/src/fretwork.c +++ b/magic/src/fretwork.c @@ -22,84 +22,80 @@ #define SEG_RIGHT_TOP_BOTTOM (SEG_RIGHT | SEG_TOP | SEG_BOTTOM) #define SEG_LEFT_RIGHT_TOP_BOTTOM (SEG_LEFT | SEG_RIGHT | SEG_TOP | SEG_BOTTOM) -Mix_Chunk * fretwork_snd; +Mix_Chunk *fretwork_snd; unsigned int img_w, img_h; -unsigned int fretwork_segments_x, fretwork_segments_y; //how many segments do we have? -static int fretwork_math_ceil(int x, int y); //ceil() in cstdlib returns float and is relative slow, so we'll use our one -static Uint8 * fretwork_status_of_segments; //a place to store an info about bitmap used for selected segment -static char ** fretwork_images; //the pathes to all the images needed -static unsigned int fretwork_segment_modified; //which segment was modified this time? -static unsigned int fretwork_segment_modified_last =0; //which segment was last modified -static unsigned int fretwork_segment_to_add =0; //a segment that should be added to solve corner joint +unsigned int fretwork_segments_x, fretwork_segments_y; //how many segments do we have? +static int fretwork_math_ceil(int x, int y); //ceil() in cstdlib returns float and is relative slow, so we'll use our one +static Uint8 *fretwork_status_of_segments; //a place to store an info about bitmap used for selected segment +static char **fretwork_images; //the pathes to all the images needed +static unsigned int fretwork_segment_modified; //which segment was modified this time? +static unsigned int fretwork_segment_modified_last = 0; //which segment was last modified +static unsigned int fretwork_segment_to_add = 0; //a segment that should be added to solve corner joint static unsigned int fretwork_segment_last_clicked; -static Uint8 fretwork_r, fretwork_g, fretwork_b; -static unsigned int fretwork_full_runs; //The count of the clicks in full mode -static unsigned int fretwork_segment_start_rectangle; //the segment were the update_rectangle will start -static unsigned int fretwork_update_rectangle_width; //the width of the update_rectangle -static unsigned int fretwork_update_rectangle_height; //the height of the update_rectangle +static Uint8 fretwork_r, fretwork_g, fretwork_b; +static unsigned int fretwork_full_runs; //The count of the clicks in full mode +static unsigned int fretwork_segment_start_rectangle; //the segment were the update_rectangle will start +static unsigned int fretwork_update_rectangle_width; //the width of the update_rectangle +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 *canvas_backup; +static SDL_Surface *fretwork_one_back, *fretwork_three_back, *fretwork_four_back, *fretwork_corner_back; -// Housekeeping functions +// Housekeeping functions 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); -char * fretwork_get_name(magic_api * api, int which); -char * fretwork_get_description(magic_api * api, int which, int mode); +SDL_Surface *fretwork_get_icon(magic_api * api, int which); +char *fretwork_get_name(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); inline void fretwork_extract_coords_from_segment(unsigned int segment, Sint16 * x, Sint16 * 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); -SDL_Surface * fretwork_one, * fretwork_three, * fretwork_four, * fretwork_corner; +SDL_Surface *fretwork_one, *fretwork_three, *fretwork_four, *fretwork_corner; Uint32 fretwork_api_version(void) { - return(TP_MAGIC_API_VERSION); + return (TP_MAGIC_API_VERSION); } int fretwork_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_PAINT|MODE_FULLSCREEN); + return (MODE_PAINT | MODE_FULLSCREEN); } void fretwork_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b) { - fretwork_r=r; - fretwork_g=g; - fretwork_b=b; - fretwork_colorize(api,fretwork_one, fretwork_one_back); + fretwork_r = r; + fretwork_g = g; + fretwork_b = b; + fretwork_colorize(api, fretwork_one, fretwork_one_back); fretwork_colorize(api, fretwork_three, fretwork_three_back); fretwork_colorize(api, fretwork_four, fretwork_four_back); fretwork_colorize(api, fretwork_corner, fretwork_corner_back); } /* 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; @@ -110,14 +106,11 @@ static void fretwork_colorize(magic_api * api, SDL_Surface * dest, SDL_Surface * for (y = 0; y < src->h; y++) { 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); @@ -128,34 +121,34 @@ static void fretwork_colorize(magic_api * api, SDL_Surface * dest, SDL_Surface * 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); - - for (i = 0; i < 4; i++) - fretwork_images[i]=(char *)malloc(sizeof(char)*1024); - - snprintf(fretwork_images[0], 1024*sizeof(char), "%s/images/magic/fretwork_one.png", api->data_directory); - snprintf(fretwork_images[1], 1024*sizeof(char), "%s/images/magic/fretwork_three.png", api->data_directory); - snprintf(fretwork_images[2], 1024*sizeof(char), "%s/images/magic/fretwork_four.png", api->data_directory); - snprintf(fretwork_images[3], 1024*sizeof(char), "%s/images/magic/fretwork_corner.png", api->data_directory); + Uint8 i; //is always < 4, so Uint8 seems to be a good idea - fretwork_one=IMG_Load(fretwork_images[0]); - fretwork_three=IMG_Load(fretwork_images[1]); - fretwork_four=IMG_Load(fretwork_images[2]); - fretwork_corner=IMG_Load(fretwork_images[3]); - fretwork_one_back=IMG_Load(fretwork_images[0]); - fretwork_three_back=IMG_Load(fretwork_images[1]); - fretwork_four_back=IMG_Load(fretwork_images[2]); - fretwork_corner_back=IMG_Load(fretwork_images[3]); + fretwork_images = (char **)malloc(sizeof(char *) * 4); + + for (i = 0; i < 4; i++) + fretwork_images[i] = (char *)malloc(sizeof(char) * 1024); + + snprintf(fretwork_images[0], 1024 * sizeof(char), "%s/images/magic/fretwork_one.png", api->data_directory); + snprintf(fretwork_images[1], 1024 * sizeof(char), "%s/images/magic/fretwork_three.png", api->data_directory); + snprintf(fretwork_images[2], 1024 * sizeof(char), "%s/images/magic/fretwork_four.png", api->data_directory); + snprintf(fretwork_images[3], 1024 * sizeof(char), "%s/images/magic/fretwork_corner.png", api->data_directory); + + fretwork_one = IMG_Load(fretwork_images[0]); + fretwork_three = IMG_Load(fretwork_images[1]); + fretwork_four = IMG_Load(fretwork_images[2]); + fretwork_corner = IMG_Load(fretwork_images[3]); + fretwork_one_back = IMG_Load(fretwork_images[0]); + fretwork_three_back = IMG_Load(fretwork_images[1]); + fretwork_four_back = IMG_Load(fretwork_images[2]); + fretwork_corner_back = IMG_Load(fretwork_images[3]); img_w = fretwork_one->w; img_h = fretwork_one->h; - + snprintf(fname, sizeof(fname), "%s/sounds/magic/fretwork.ogg", api->data_directory); fretwork_snd = Mix_LoadWAV(fname); - return(1); + return (1); } int fretwork_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) @@ -163,38 +156,44 @@ int fretwork_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) return 1; } -SDL_Surface * fretwork_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *fretwork_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/fretwork.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/fretwork.png", api->data_directory); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } -char * fretwork_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { return strdup(gettext_noop("Fretwork")); } +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) { - if (mode==MODE_PAINT) +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. ")); else 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) { return 1;} +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) + 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_shutdown(magic_api * api ATTRIBUTE_UNUSED) { Uint8 i; - - if (fretwork_snd!=NULL) + + if (fretwork_snd != NULL) Mix_FreeChunk(fretwork_snd); SDL_FreeSurface(fretwork_one); SDL_FreeSurface(fretwork_three); @@ -205,7 +204,7 @@ void fretwork_shutdown(magic_api * api ATTRIBUTE_UNUSED) SDL_FreeSurface(fretwork_four_back); SDL_FreeSurface(fretwork_corner_back); SDL_FreeSurface(canvas_backup); - + for (i = 0; i < 4; i++) free(fretwork_images[i]); free(fretwork_images); @@ -213,22 +212,25 @@ 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); - fretwork_segments_y=fretwork_math_ceil(canvas->h,img_h); - fretwork_status_of_segments=(Uint8 *)calloc(fretwork_segments_x*fretwork_segments_y + 1, sizeof(Uint8)); //segments starts at 1, while fretwork_status_of_segments[] starts at 0 - fretwork_full_runs=1; + fretwork_segments_x = fretwork_math_ceil(canvas->w, img_w); + fretwork_segments_y = fretwork_math_ceil(canvas->h, img_h); + fretwork_status_of_segments = (Uint8 *) calloc(fretwork_segments_x * fretwork_segments_y + 1, sizeof(Uint8)); //segments starts at 1, while fretwork_status_of_segments[] starts at 0 + fretwork_full_runs = 1; } -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; @@ -238,29 +240,32 @@ void fretwork_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UN static int fretwork_math_ceil(int x, int y) -{ +{ int temp; - temp=(int)x/y; - if (x%y) - return temp+1; - else return temp; + + temp = (int)x / y; + if (x % y) + return temp + 1; + else + return temp; } - + inline unsigned int fretwork_get_segment(int x, int y) { - int xx; //segments are numerated just like pixels - int yy; //in computer graphics: left upper (=1), ... ,right upper, - //left bottom, ... , right bottom - xx=fretwork_math_ceil(x, img_w); - yy=fretwork_math_ceil(y, img_h); + int xx; //segments are numerated just like pixels + int yy; //in computer graphics: left upper (=1), ... ,right upper, - return (yy-1)*fretwork_segments_x+xx; + //left bottom, ... , right bottom + xx = fretwork_math_ceil(x, img_w); + yy = fretwork_math_ceil(y, img_h); + + return (yy - 1) * fretwork_segments_x + xx; } inline void fretwork_extract_coords_from_segment(unsigned int segment, Sint16 * x, Sint16 * 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; + *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; } /* static void fretwork_flip(void * ptr, SDL_Surface * dest, SDL_Surface * src) */ @@ -274,75 +279,80 @@ inline void fretwork_extract_coords_from_segment(unsigned int segment, Sint16 * /* 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; + magic_api *api = (magic_api *) ptr; Sint16 x, y; - for (x=0; xw; x++) - for (y=0; yh; y++) - api->putpixel(dest, dest->w-1-x, dest->h-1-y, api->getpixel(src, 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)); } -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; - Sint16 x,y; - - if (direction) //rotate -90 degs + magic_api *api = (magic_api *) ptr; + Sint16 x, y; + + if (direction) //rotate -90 degs { - for (x = 0; xw; x++) - for (y =0; yh; 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 + else //rotate +90 degs { - for (x=0; xw; x++) - for (y=0; yh; 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) + 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) - { - 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); + 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 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; + } } } @@ -355,64 +365,73 @@ static Uint8 fretwork_select_image(Uint16 segment) int TOP = 0, BOTTOM = 0, LEFT = 0, RIGHT = 0; //Checking from were we come... - if (fretwork_segment_modified_last>0) + if (fretwork_segment_modified_last > 0) { if (segment == fretwork_segment_modified_last + 1) - from_left = 1; + from_left = 1; else if (segment == fretwork_segment_modified_last - 1) - from_right = 1; + from_right = 1; else if (segment == fretwork_segment_modified_last - fretwork_segments_x) - from_bottom = 1; + from_bottom = 1; else if (segment == fretwork_segment_modified_last + fretwork_segments_x) - from_top = 1; - + 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; - } + { + 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; - } + { + 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; - } + { + 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; - } + { + from_bottom_left = 1; + fretwork_segment_to_add = segment - 1; + } } - take_up=segment-fretwork_segments_x; - if (take_up<=0) val_up = SEG_NONE; - else val_up = fretwork_status_of_segments[take_up]; + take_up = segment - fretwork_segments_x; + if (take_up <= 0) + val_up = SEG_NONE; + else + val_up = fretwork_status_of_segments[take_up]; - take_down=segment+fretwork_segments_x; - if (take_down>(signed)(fretwork_segments_x*fretwork_segments_y)) val_down = SEG_NONE; - else val_down = fretwork_status_of_segments[take_down]; - - if ((segment%fretwork_segments_x)==1) val_left=SEG_NONE; - else val_left = fretwork_status_of_segments[segment-1]; - - if ((segment%fretwork_segments_x)==0) val_right=SEG_NONE; - else val_right = fretwork_status_of_segments[segment+1]; + take_down = segment + fretwork_segments_x; + if (take_down > (signed)(fretwork_segments_x * fretwork_segments_y)) + val_down = SEG_NONE; + else + val_down = fretwork_status_of_segments[take_down]; - if ( from_left || (val_left & SEG_RIGHT) || from_bottom_left) + if ((segment % fretwork_segments_x) == 1) + val_left = SEG_NONE; + else + val_left = fretwork_status_of_segments[segment - 1]; + + if ((segment % fretwork_segments_x) == 0) + val_right = SEG_NONE; + else + val_right = fretwork_status_of_segments[segment + 1]; + + if (from_left || (val_left & SEG_RIGHT) || from_bottom_left) { - LEFT = 1;} - if ( from_right || (val_right & SEG_LEFT) || from_top_right) - RIGHT=1; - if ( from_top || (val_up & SEG_BOTTOM) || from_top_left) - TOP=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) + TOP = 1; if (from_bottom || (val_down & SEG_TOP) || from_bottom_right) - BOTTOM=1; + BOTTOM = 1; if (TOP && BOTTOM && LEFT && RIGHT) @@ -425,91 +444,93 @@ static Uint8 fretwork_select_image(Uint16 segment) return SEG_LEFT_TOP_BOTTOM; if (TOP && BOTTOM && RIGHT) return SEG_RIGHT_TOP_BOTTOM; - if (LEFT &&RIGHT) + if (LEFT && RIGHT) return SEG_LEFT_RIGHT; - if (TOP&&BOTTOM) + if (TOP && BOTTOM) return SEG_TOP_BOTTOM; - if (LEFT&&TOP) + if (LEFT && TOP) return SEG_LEFT_TOP; - if (LEFT&&BOTTOM) + if (LEFT && BOTTOM) return SEG_LEFT_BOTTOM; - if (RIGHT&&TOP) + if (RIGHT && TOP) return SEG_RIGHT_TOP; - if (RIGHT&&BOTTOM) + if (RIGHT && BOTTOM) return SEG_RIGHT_BOTTOM; - if (LEFT|RIGHT) + if (LEFT | RIGHT) return SEG_LEFT_RIGHT; //if (TOP||BOTTOM) return SEG_TOP_BOTTOM; } -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; + magic_api *api = (magic_api *) ptr; + SDL_Surface *result, *temp; Uint8 image; _Bool use_temp; - - use_temp=0; - if ((segment<1)|(segment>fretwork_segments_x*fretwork_segments_y)) + + 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); - modification_rect.h=img_w; - modification_rect.w=img_h; - - image=fretwork_select_image(segment); //select the image to display + modification_rect.h = img_w; + modification_rect.w = img_h; + + image = fretwork_select_image(segment); //select the image to display if (fretwork_status_of_segments[segment] == image) return; - 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); + fretwork_status_of_segments[segment] = image; //and write it to global table - 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); + 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); SDL_BlitSurface(canvas_backup, &modification_rect, result, NULL); - - switch(image) + + switch (image) { case 0: - case SEG_TOP_BOTTOM: + 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; + 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: SDL_BlitSurface(fretwork_three, NULL, result, NULL); break; - + case SEG_LEFT_RIGHT_BOTTOM: fretwork_flip_flop(api, temp, fretwork_three); - use_temp=1; + use_temp = 1; break; - + case SEG_LEFT_TOP_BOTTOM: fretwork_rotate(api, temp, fretwork_three, 0); - use_temp=1; + use_temp = 1; break; - + case SEG_RIGHT_TOP_BOTTOM: fretwork_rotate(api, temp, fretwork_three, 1); - use_temp=1; + use_temp = 1; break; case SEG_RIGHT_TOP: @@ -517,24 +538,24 @@ static void fretwork_draw(void * ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * break; case SEG_RIGHT_BOTTOM: - fretwork_rotate(api, temp, fretwork_corner,1); - use_temp=1; + 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; + use_temp = 1; break; case SEG_LEFT_BOTTOM: fretwork_flip_flop(api, temp, fretwork_corner); - use_temp=1; + use_temp = 1; break; } - - if (use_temp) + + if (use_temp) SDL_BlitSurface(temp, NULL, result, NULL); - + SDL_FreeSurface(temp); SDL_BlitSurface(result, NULL, canvas, &modification_rect); SDL_FreeSurface(result); @@ -542,53 +563,58 @@ static void fretwork_draw(void * ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * } -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_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) + 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;} + 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_segment_modified_last=fretwork_segment_modified; + 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) -{ int start_x, end_x, start_y, end_y, segment_start, segment_end, w, h; - if ((xw)&&(yh)&&(oxw)&&(oyh)&&((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); + 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; - 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; + 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); - update_rect->x=x; - update_rect->y=y; - update_rect->w=w; - update_rect->h=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; + } } diff --git a/magic/src/glasstile.c b/magic/src/glasstile.c index e37f2a7b3..232d15a3c 100644 --- a/magic/src/glasstile.c +++ b/magic/src/glasstile.c @@ -36,26 +36,22 @@ /* Our globals: */ -static Mix_Chunk * glasstile_snd; +static Mix_Chunk *glasstile_snd; // Prototypes Uint32 glasstile_api_version(void); int glasstile_init(magic_api * api); int glasstile_get_tool_count(magic_api * api); -SDL_Surface * glasstile_get_icon(magic_api * api, int which); -char * glasstile_get_name(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); +SDL_Surface *glasstile_get_icon(magic_api * api, int which); +char *glasstile_get_name(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); 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); + 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_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); @@ -63,9 +59,12 @@ void glasstile_switchin(magic_api * api, int which, int mode, SDL_Surface * canv 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) { return(TP_MAGIC_API_VERSION); } +Uint32 glasstile_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} -static int * * glasstile_hit; +static int **glasstile_hit; static int glasstile_hit_xsize; static int glasstile_hit_ysize; @@ -74,60 +73,53 @@ int glasstile_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/glasstile.ogg", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/glasstile.ogg", api->data_directory); glasstile_snd = Mix_LoadWAV(fname); - + glasstile_hit = NULL; glasstile_hit_ysize = 0; - return(1); + return (1); } // We have multiple tools: int glasstile_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(1); + return (1); } // Load our icons: -SDL_Surface * glasstile_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *glasstile_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/glasstile.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/glasstile.png", api->data_directory); - return(IMG_Load(fname)); + 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 (strdup(gettext_noop("Glass Tile"))); } // 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; + magic_api *api = (magic_api *) ptr; int xx, yy, xl, xr, yt, yb; - Uint8 r1, g1, b1, - r2, g2, b2, - r3, g3, b3, - r4, g4, b4, - r, g, b; + Uint8 r1, g1, b1, r2, g2, b2, r3, g3, b3, r4, g4, b4, r, g, b; Uint32 rgb; @@ -154,66 +146,61 @@ static void do_glasstile(void * ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * c /* Apply the effect: */ for (yy = -GT_SIZE; yy < GT_SIZE; yy = yy + 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); + 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); - 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; @@ -246,21 +233,20 @@ 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++) @@ -269,24 +255,24 @@ 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; - /* FIXME: Play sfx */ - } + /* FIXME: Play sfx */ + } } // 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -297,20 +283,21 @@ void glasstile_shutdown(magic_api * api ATTRIBUTE_UNUSED) if (glasstile_snd != NULL) Mix_FreeChunk(glasstile_snd); - + if (glasstile_hit != NULL) - { - for (y = 0; y < glasstile_hit_ysize; y++) { - if (glasstile_hit[y] != NULL) - free(glasstile_hit[y]); + for (y = 0; y < glasstile_hit_ysize; y++) + { + if (glasstile_hit[y] != NULL) + free(glasstile_hit[y]); + } + free(glasstile_hit); } - 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, Uint8 b ATTRIBUTE_UNUSED) +void glasstile_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, + Uint8 b ATTRIBUTE_UNUSED) { } @@ -320,15 +307,17 @@ int glasstile_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIB return 0; } -void glasstile_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas 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, SDL_Surface * canvas 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) { - return(MODE_PAINT | MODE_FULLSCREEN); + return (MODE_PAINT | MODE_FULLSCREEN); } diff --git a/magic/src/grass.c b/magic/src/grass.c index cba45de44..e5d6105e3 100644 --- a/magic/src/grass.c +++ b/magic/src/grass.c @@ -30,39 +30,34 @@ #include #include -#include /* for RAND_MAX */ +#include /* for RAND_MAX */ #include "tp_magic_api.h" #include "SDL_image.h" #include "SDL_mixer.h" /* Our globals: */ -static Mix_Chunk * grass_snd; +static Mix_Chunk *grass_snd; static Uint8 grass_r, grass_g, grass_b; -static SDL_Surface * img_grass; +static SDL_Surface *img_grass; // Prototypes int grass_init(magic_api * api); Uint32 grass_api_version(void); int grass_get_tool_count(magic_api * api); -SDL_Surface * grass_get_icon(magic_api * api, int which); -char * grass_get_name(magic_api * api, int which); -char * grass_get_description(magic_api * api, int which, int mode); +SDL_Surface *grass_get_icon(magic_api * api, int which); +char *grass_get_name(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); + 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); + 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 * 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); @@ -74,81 +69,90 @@ int grass_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/grass.wav", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/grass.wav", api->data_directory); grass_snd = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%s/images/magic/grass_data.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/grass_data.png", api->data_directory); img_grass = IMG_Load(fname); - return(1); + return (1); } -Uint32 grass_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 grass_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} // We have multiple tools: int grass_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(1); + return (1); } // Load our icons: -SDL_Surface * grass_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *grass_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/grass.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/grass.png", api->data_directory); - return(IMG_Load(fname)); + 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 (strdup(gettext_noop("Grass"))); } // 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; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - 64; update_rect->y = oy - 64; update_rect->w = 128; update_rect->h = 192; - api->playsound(grass_snd, - (x * 255) / canvas->w, (y * 255) / canvas->h); + api->playsound(grass_snd, (x * 255) / canvas->w, (y * 255) / canvas->h); } // 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -173,12 +177,12 @@ int grass_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_ return 1; } -static void do_grass(void * ptr, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, - int x, int y) +static void do_grass(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { - magic_api * api = (magic_api *) ptr; + magic_api *api = (magic_api *) ptr; int xx, yy; + // grass color: 82,180,17 static int bucket; double tmp_red, tmp_green, tmp_blue; @@ -187,92 +191,79 @@ 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++) { - for (xx = 0; xx < 64; xx++) - { - double rd, gd, bd; + int rank = log2int(((double)y / canvas->h) * (0.99 + (rand() / (double)RAND_MAX)) * 64); + int ah = 1 << rank; - SDL_GetRGBA(api->getpixel(img_grass, xx + src.x, yy + src.y), - img_grass->format, &r, &g, &b, &a); + bucket -= ah; + src.x = (rand() % 4) * 64; + src.y = ah; + src.w = 64; + src.h = ah; - 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; + dest.x = x - 32; + dest.y = y - 30 + (int)((rand() / (double)RAND_MAX) * 30); - SDL_GetRGB(api->getpixel(canvas, xx + dest.x, yy + dest.y), - canvas->format, &r, &g, &b); + 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); - 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); + for (yy = 0; yy < ah; yy++) + { + for (xx = 0; xx < 64; xx++) + { + double rd, gd, bd; - api->putpixel(canvas, xx + dest.x, yy + dest.y, - SDL_MapRGB(canvas->format, r, g, b)); - } + 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)); + } + } } - } } // this one rounds down static int log2int(int x) { int y = 0; + if (x <= 1) 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, SDL_Surface * canvas 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, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void grass_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } int grass_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_PAINT); + return (MODE_PAINT); } diff --git a/magic/src/halftone.c b/magic/src/halftone.c index 9c7b714cd..71047b2c8 100644 --- a/magic/src/halftone.c +++ b/magic/src/halftone.c @@ -16,55 +16,51 @@ #include "SDL_image.h" #include "SDL_mixer.h" -enum { +enum +{ TOOL_HALFTONE, NUM_TOOLS }; -const char * snd_filenames[NUM_TOOLS] = { +const char *snd_filenames[NUM_TOOLS] = { "halftone.wav", }; -const char * icon_filenames[NUM_TOOLS] = { +const char *icon_filenames[NUM_TOOLS] = { "halftone.png", }; -const char * names[NUM_TOOLS] = { +const char *names[NUM_TOOLS] = { gettext_noop("Halftone"), }; -const char * descs[NUM_TOOLS] = { +const char *descs[NUM_TOOLS] = { gettext_noop("Click and drag to turn your drawing into a newspaper."), }; -Mix_Chunk * snd_effect[NUM_TOOLS]; +Mix_Chunk *snd_effect[NUM_TOOLS]; -static SDL_Surface * canvas_backup, * square; +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); -SDL_Surface * halftone_get_icon(magic_api * api, int which); -char * halftone_get_name(magic_api * api, int which); -char * halftone_get_description(magic_api * api, int which, int mode); +SDL_Surface *halftone_get_icon(magic_api * api, int which); +char *halftone_get_name(magic_api * api, int which); +char *halftone_get_description(magic_api * api, int which, int mode); 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); + 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_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); @@ -72,7 +68,7 @@ void halftone_rgb2cmyk(Uint8 r, Uint8 g, Uint8 b, float cmyk[]); Uint32 halftone_api_version(void) { - return(TP_MAGIC_API_VERSION); + return (TP_MAGIC_API_VERSION); } int halftone_init(magic_api * api) @@ -84,58 +80,56 @@ int halftone_init(magic_api * api) square = NULL; for (i = 0; i < NUM_TOOLS; i++) - { - 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]); - snd_effect[i] = Mix_LoadWAV(fname); - if (snd_effect[i] == NULL) { - SDL_FreeSurface(canvas_backup); - SDL_FreeSurface(square); - return(0); + snd_effect[i] = Mix_LoadWAV(fname); + if (snd_effect[i] == NULL) + { + SDL_FreeSurface(canvas_backup); + SDL_FreeSurface(square); + return (0); + } } - } - return(1); + return (1); } int halftone_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(NUM_TOOLS); + return (NUM_TOOLS); } -SDL_Surface * halftone_get_icon(magic_api * api, int which) +SDL_Surface *halftone_get_icon(magic_api * api, int which) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/%s", - api->data_directory, icon_filenames[which]); + snprintf(fname, sizeof(fname), "%s/images/magic/%s", api->data_directory, icon_filenames[which]); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } -char * halftone_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) +char *halftone_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) { - const char * our_name_english; - const char * our_name_localized; + const char *our_name_english; + const char *our_name_localized; our_name_english = names[which]; our_name_localized = gettext_noop(our_name_english); - return(strdup(our_name_localized)); + return (strdup(our_name_localized)); } -char * halftone_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode ATTRIBUTE_UNUSED) +char *halftone_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode ATTRIBUTE_UNUSED) { - const char * our_desc_english; - const char * our_desc_localized; + const char *our_desc_english; + const char *our_desc_localized; our_desc_english = descs[which]; our_desc_localized = gettext_noop(our_desc_english); - return(strdup(our_desc_localized)); + return (strdup(our_desc_localized)); } int halftone_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) @@ -160,33 +154,42 @@ void halftone_shutdown(magic_api * api ATTRIBUTE_UNUSED) } void halftone_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) { halftone_drag(api, which, canvas, snapshot, x, y, x, y, update_rect); } 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; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - 16; update_rect->y = oy - 16; update_rect->w = (x + 16) - update_rect->x; update_rect->h = (y + 16) - update_rect->h; - api->playsound(snd_effect[which], - (x * 255) / canvas->w, // pan - 255); // distance + api->playsound(snd_effect[which], (x * 255) / canvas->w, // pan + 255); // distance } -enum { +enum +{ CHAN_CYAN, CHAN_MAGENTA, CHAN_YELLOW, @@ -195,10 +198,10 @@ enum { }; Uint8 chan_colors[NUM_CHANS][3] = { - { 0, 255, 255 }, - { 255, 0, 255 }, - { 255, 255, 0 }, - { 0, 0, 0 } + {0, 255, 255}, + {255, 0, 255}, + {255, 255, 0}, + {0, 0, 0} }; int chan_angles[NUM_CHANS] = { @@ -209,28 +212,25 @@ int chan_angles[NUM_CHANS] = { }; 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) - + 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) +void halftone_line_callback(void *ptr, int which ATTRIBUTE_UNUSED, + 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; Uint32 pixel; int xx, yy, xxx, yyy, channel, ox, oy, sqx, sqy; SDL_Rect dest; - magic_api * api = (magic_api *) ptr; + magic_api *api = (magic_api *) ptr; float cmyk[4]; pixel = SDL_MapRGB(square->format, 255, 255, 255); @@ -240,62 +240,77 @@ void halftone_line_callback(void * ptr, int which ATTRIBUTE_UNUSED, x = ((x / 8) - 1) * 8; y = ((y / 8) - 1) * 8; - if (api->touched(x, y)) { return; } - - for (xx = 0; xx < 16; xx = xx + 4) { - for (yy = 0; yy < 16; yy = yy + 4) { - /* Get avg color around the mouse */ - total_r = total_g = total_b = 0; - for (xxx = 0; xxx < 4; xxx++) { - for (yyy = 0; yyy < 4; yyy++) { - SDL_GetRGB(api->getpixel(canvas_backup, x + xx + xxx, y + yy + yyy), canvas_backup->format, &r, &g, &b); - total_r += r; - total_g += g; - total_b += b; - } - } - total_r /= 16; - total_g /= 16; - total_b /= 16; - - /* Convert to CMYK values */ - halftone_rgb2cmyk(total_r, total_g, total_b, cmyk); - - /* Draw C, M, Y and K blobs into our 'square' surface */ - for (channel = 0; channel < NUM_CHANS; channel++) { - r = chan_colors[channel][0]; - g = chan_colors[channel][1]; - b = chan_colors[channel][2]; - - for (xxx = 0; xxx < 8; xxx++) { - for (yyy = 0; yyy < 8; yyy++) { - /* A circle blob, radius based upon channel (C, M, Y or K) strength for this color */ - - /* FIXME: Base it upon this channel's angle! -bjk 2011.07.17 */ - ox = xxx; - oy = yyy; - - sqx = (xx + ox) % 16; - sqy = (yy + oy) % 16; - - if (api->in_circle(xxx - 4, yyy - 4, cmyk[channel] * 6.0)) { - SDL_GetRGB(api->getpixel(square, sqx, sqy), square->format, &or, &og, &ob); - - if (or == 255 && og == 255 && ob == 255) { - /* If it's just white, put full color down */ - pixel = SDL_MapRGB(square->format, r, g, b); - } else { - /* Otherwise, blend a little */ - pixel = SDL_MapRGB(square->format, (r + or) / 2, (g + og) / 2, (b + ob) / 2); - } - - api->putpixel(square, sqx, sqy, pixel); - } - } - } - } + if (api->touched(x, y)) + { + return; + } + + for (xx = 0; xx < 16; xx = xx + 4) + { + for (yy = 0; yy < 16; yy = yy + 4) + { + /* Get avg color around the mouse */ + total_r = total_g = total_b = 0; + for (xxx = 0; xxx < 4; xxx++) + { + for (yyy = 0; yyy < 4; yyy++) + { + SDL_GetRGB(api->getpixel(canvas_backup, x + xx + xxx, y + yy + yyy), canvas_backup->format, &r, &g, + &b); + total_r += r; + total_g += g; + total_b += b; + } + } + total_r /= 16; + total_g /= 16; + total_b /= 16; + + /* Convert to CMYK values */ + halftone_rgb2cmyk(total_r, total_g, total_b, cmyk); + + /* Draw C, M, Y and K blobs into our 'square' surface */ + for (channel = 0; channel < NUM_CHANS; channel++) + { + r = chan_colors[channel][0]; + g = chan_colors[channel][1]; + b = chan_colors[channel][2]; + + for (xxx = 0; xxx < 8; xxx++) + { + for (yyy = 0; yyy < 8; yyy++) + { + /* A circle blob, radius based upon channel (C, M, Y or K) strength for this color */ + + /* FIXME: Base it upon this channel's angle! -bjk 2011.07.17 */ + ox = xxx; + oy = yyy; + + sqx = (xx + ox) % 16; + sqy = (yy + oy) % 16; + + if (api->in_circle(xxx - 4, yyy - 4, cmyk[channel] * 6.0)) + { + SDL_GetRGB(api->getpixel(square, sqx, sqy), square->format, &or, &og, &ob); + + if (or == 255 && og == 255 && ob == 255) + { + /* If it's just white, put full color down */ + pixel = SDL_MapRGB(square->format, r, g, b); + } + else + { + /* Otherwise, blend a little */ + pixel = SDL_MapRGB(square->format, (r + or) / 2, (g + og) / 2, (b + ob) / 2); + } + + api->putpixel(square, sqx, sqy, pixel); + } + } + } + } + } } - } dest.x = x; dest.y = y; @@ -303,22 +318,25 @@ 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, 16, 16, canvas->format->BitsPerPixel, canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, canvas->format->Amask); + square = + SDL_CreateRGBSurface(SDL_SWSURFACE, 16, 16, canvas->format->BitsPerPixel, canvas->format->Rmask, + canvas->format->Gmask, canvas->format->Bmask, canvas->format->Amask); /* FIXME: What to do if they come back NULL!? :( */ 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) { } @@ -331,25 +349,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); - - 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; - } + { + 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; + } cmyk[0] = c; cmyk[1] = m; diff --git a/magic/src/kalidescope.c b/magic/src/kalidescope.c index 8de563882..28b76048b 100644 --- a/magic/src/kalidescope.c +++ b/magic/src/kalidescope.c @@ -35,12 +35,13 @@ /* Our globals: */ -static Mix_Chunk * kalidescope_snd; +static Mix_Chunk *kalidescope_snd; static Uint8 kalidescope_r, kalidescope_g, kalidescope_b; static int square_size = 128; -enum { +enum +{ KAL_UD, KAL_LR, KAL_BOTH, @@ -49,7 +50,7 @@ enum { KAL_COUNT }; -char * kal_icon_names[KAL_COUNT] = { +char *kal_icon_names[KAL_COUNT] = { "symmetric_updown.png", "symmetric_leftright.png", "kalidescope.png", @@ -62,20 +63,16 @@ char * kal_icon_names[KAL_COUNT] = { Uint32 kalidescope_api_version(void); int kalidescope_init(magic_api * api); int kalidescope_get_tool_count(magic_api * api); -SDL_Surface * kalidescope_get_icon(magic_api * api, int which); -char * kalidescope_get_name(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); +SDL_Surface *kalidescope_get_icon(magic_api * api, int which); +char *kalidescope_get_name(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); 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); + 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_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); @@ -83,120 +80,144 @@ void kalidescope_switchin(magic_api * api, int which, int mode, SDL_Surface * ca 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) { return(TP_MAGIC_API_VERSION); } +Uint32 kalidescope_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} // No setup required: int kalidescope_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/kaleidoscope.ogg", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/kaleidoscope.ogg", api->data_directory); kalidescope_snd = Mix_LoadWAV(fname); - return(1); + return (1); } int kalidescope_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(KAL_COUNT); + return (KAL_COUNT); } // Load our icons: -SDL_Surface * kalidescope_get_icon(magic_api * api, int which) +SDL_Surface *kalidescope_get_icon(magic_api * api, int which) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/%s", - api->data_directory, kal_icon_names[which]); + snprintf(fname, sizeof(fname), "%s/images/magic/%s", api->data_directory, kal_icon_names[which]); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } // Return our names, localized: -char * kalidescope_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) +char *kalidescope_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) { - if (which == KAL_LR) { - return(strdup(gettext_noop("Symmetric Left/Right"))); - } else if (which == KAL_UD) { - return(strdup(gettext_noop("Symmetric Up/Down"))); - } else if (which == KAL_PATTERN) { - return(strdup(gettext_noop("Pattern"))); - } else if (which == KAL_TILES) { - return(strdup(gettext_noop("Tiles"))); - } else { /* KAL_BOTH */ - return(strdup(gettext_noop("Kaleidoscope"))); - } + if (which == KAL_LR) + { + return (strdup(gettext_noop("Symmetric Left/Right"))); + } + else if (which == KAL_UD) + { + return (strdup(gettext_noop("Symmetric Up/Down"))); + } + else if (which == KAL_PATTERN) + { + return (strdup(gettext_noop("Pattern"))); + } + else if (which == KAL_TILES) + { + return (strdup(gettext_noop("Tiles"))); + } + else + { /* KAL_BOTH */ + return (strdup(gettext_noop("Kaleidoscope"))); + } } // 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."))); - } 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."))); - } else if (which == KAL_PATTERN) { - 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."))); - } else { /* KAL_BOTH */ - return(strdup(gettext_noop("Click and drag the mouse to draw with symmetric brushes (a kaleidoscope)."))); - } + 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."))); + } + 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."))); + } + else if (which == KAL_PATTERN) + { + 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."))); + } + else + { /* KAL_BOTH */ + return (strdup(gettext_noop("Click and drag the mouse to draw with symmetric brushes (a kaleidoscope)."))); + } } // Do the effect: -static void do_kalidescope(void * ptr, int which, SDL_Surface * canvas, - SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) +static void do_kalidescope(void *ptr, int which, SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { - magic_api * api = (magic_api *) ptr; + magic_api *api = (magic_api *) ptr; int xx, yy; 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++) { - if (api->in_circle(xx, yy, 8)) - { - api->putpixel(canvas, x + xx, y + yy, colr); + 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) { - api->putpixel(canvas, canvas->w - 1 - 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_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); + } + } + } } - 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); - } - } - } } - } } // 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; @@ -208,17 +229,15 @@ 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(); } @@ -244,17 +263,17 @@ int kalidescope_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTR 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) { - return(MODE_PAINT); + return (MODE_PAINT); } diff --git a/magic/src/light.c b/magic/src/light.c index f1ee32395..fec3df08c 100644 --- a/magic/src/light.c +++ b/magic/src/light.c @@ -38,25 +38,21 @@ /* Our globals: */ -static Mix_Chunk * light1_snd, * light2_snd; +static Mix_Chunk *light1_snd, *light2_snd; static float light_h, light_s, light_v; Uint32 light_api_version(void); int light_init(magic_api * api); int light_get_tool_count(magic_api * api); -SDL_Surface * light_get_icon(magic_api * api, int which); -char * light_get_name(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); +SDL_Surface *light_get_icon(magic_api * api, int which); +char *light_get_name(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); 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); void light_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 light_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 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); @@ -65,7 +61,10 @@ void light_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas) int light_modes(magic_api * api, int which); -Uint32 light_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 light_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} // No setup required: @@ -73,52 +72,49 @@ int light_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/light1.ogg", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/light1.ogg", api->data_directory); light1_snd = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%s/sounds/magic/light2.ogg", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/light2.ogg", api->data_directory); light2_snd = Mix_LoadWAV(fname); - return(1); + return (1); } // We have multiple tools: int light_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(1); + return (1); } // Load our icons: -SDL_Surface * light_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *light_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/light.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/light.png", api->data_directory); - return(IMG_Load(fname)); + 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 (strdup(gettext_noop("Light"))); } // 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; + magic_api *api = (magic_api *) ptr; int xx, yy; Uint32 pix; Uint8 r, g, b; @@ -126,64 +122,74 @@ static void do_light(void * ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canva float adj; for (yy = -8; yy < 8; yy++) - { - 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) + for (xx = -8; xx < 8; xx++) { - 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; - } + if (api->in_circle(xx, yy, 8)) + { + pix = api->getpixel(canvas, x + xx, y + yy); - api->hsvtorgb(new_h, new_s, new_v, &r, &g, &b); + SDL_GetRGB(pix, canvas->format, &r, &g, &b); - api->putpixel(canvas, x + xx, y + yy, - SDL_MapRGB(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)); + } + } } - } } // 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; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - 8; update_rect->y = oy - 8; @@ -195,16 +201,15 @@ 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); } // 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) + SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, + int x, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { api->playsound(light2_snd, (x * 255) / canvas->w, 255); } @@ -230,15 +235,17 @@ int light_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_ return 1; } -void light_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas 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, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void light_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } int light_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_PAINT); + return (MODE_PAINT); } diff --git a/magic/src/metalpaint.c b/magic/src/metalpaint.c index f461d4bea..e2efb56af 100644 --- a/magic/src/metalpaint.c +++ b/magic/src/metalpaint.c @@ -35,26 +35,22 @@ /* Our globals: */ -static Mix_Chunk * metalpaint_snd; +static Mix_Chunk *metalpaint_snd; static Uint8 metalpaint_r, metalpaint_g, metalpaint_b; Uint32 metalpaint_api_version(void); int metalpaint_init(magic_api * api); int metalpaint_get_tool_count(magic_api * api); -SDL_Surface * metalpaint_get_icon(magic_api * api, int which); -char * metalpaint_get_name(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); +SDL_Surface *metalpaint_get_icon(magic_api * api, int which); +char *metalpaint_get_name(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); 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); + 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_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); @@ -63,7 +59,10 @@ void metalpaint_switchout(magic_api * api, int which, int mode, SDL_Surface * ca int metalpaint_modes(magic_api * api, int which); -Uint32 metalpaint_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 metalpaint_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} // No setup required: @@ -71,40 +70,39 @@ int metalpaint_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/metalpaint.wav", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/metalpaint.wav", api->data_directory); metalpaint_snd = Mix_LoadWAV(fname); - return(1); + return (1); } // We have multiple tools: int metalpaint_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(1); + return (1); } // Load our icons: -SDL_Surface * metalpaint_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *metalpaint_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/metalpaint.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/metalpaint.png", api->data_directory); - return(IMG_Load(fname)); + 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 (strdup(gettext_noop("Metal Paint"))); } // Return our descriptions, localized: -char * metalpaint_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode 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 @@ -112,7 +110,7 @@ char * metalpaint_get_description(magic_api * api ATTRIBUTE_UNUSED, int which AT /* Based on 'Golden' gradient in The GIMP: */ static int metalpaint_gradient[METALPAINT_CYCLE] = { - 56, 64, 73, 83, 93, 102, 113, 123, + 56, 64, 73, 83, 93, 102, 113, 123, 139, 154, 168, 180, 185, 189, 183, 174, 164, 152, 142, 135, 129, 138, 149, 158, 166, 163, 158, 149, 140, 122, 103, 82 @@ -120,38 +118,49 @@ static int metalpaint_gradient[METALPAINT_CYCLE] = { // Do the effect: -static void do_metalpaint(void * ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, - int x, int y) +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; + magic_api *api = (magic_api *) ptr; int xx, yy; int n; Uint8 r, g, b; for (yy = -8; yy < 8; yy++) - { - for (xx = -8; xx < 8; xx++) { - n = metalpaint_gradient[((x + xx + y + yy) / 4) % METALPAINT_CYCLE]; + for (xx = -8; xx < 8; xx++) + { + 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; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - 8; update_rect->y = oy - 8; @@ -162,17 +171,16 @@ 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) +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) { 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -197,15 +205,17 @@ int metalpaint_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRI return 1; } -void metalpaint_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas 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, SDL_Surface * canvas 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) { - return(MODE_PAINT); + return (MODE_PAINT); } diff --git a/magic/src/mirror_flip.c b/magic/src/mirror_flip.c index 08ce93e98..c1a2254b3 100644 --- a/magic/src/mirror_flip.c +++ b/magic/src/mirror_flip.c @@ -35,31 +35,25 @@ /* What tools we contain: */ -enum { +enum +{ TOOL_MIRROR, TOOL_FLIP, NUM_TOOLS }; -static Mix_Chunk * snd_effects[NUM_TOOLS]; +static Mix_Chunk *snd_effects[NUM_TOOLS]; /* Prototypes */ int mirror_flip_init(magic_api *); Uint32 mirror_flip_api_version(void); int mirror_flip_get_tool_count(magic_api *); -SDL_Surface * mirror_flip_get_icon(magic_api *, int); -char * mirror_flip_get_name(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 *); +SDL_Surface *mirror_flip_get_icon(magic_api *, int); +char *mirror_flip_get_name(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_shutdown(magic_api *); void mirror_flip_set_color(magic_api *, Uint8, Uint8, Uint8); int mirror_flip_requires_colors(magic_api *, int); @@ -72,134 +66,127 @@ int mirror_flip_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/mirror.wav", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/mirror.wav", api->data_directory); snd_effects[TOOL_MIRROR] = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%s/sounds/magic/flip.wav", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/flip.wav", api->data_directory); snd_effects[TOOL_FLIP] = Mix_LoadWAV(fname); - return(1); + return (1); } -Uint32 mirror_flip_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 mirror_flip_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} // We have multiple tools: int mirror_flip_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(NUM_TOOLS); + return (NUM_TOOLS); } // Load our icons: -SDL_Surface * mirror_flip_get_icon(magic_api * api, int which) +SDL_Surface *mirror_flip_get_icon(magic_api * api, int which) { char fname[1024]; if (which == TOOL_MIRROR) - { - snprintf(fname, sizeof(fname), "%s/images/magic/mirror.png", - api->data_directory); - } + { + snprintf(fname, sizeof(fname), "%s/images/magic/mirror.png", api->data_directory); + } else if (which == TOOL_FLIP) - { - snprintf(fname, sizeof(fname), "%s/images/magic/flip.png", - api->data_directory); - } + { + snprintf(fname, sizeof(fname), "%s/images/magic/flip.png", api->data_directory); + } - return(IMG_Load(fname)); + return (IMG_Load(fname)); } // Return our names, localized: -char * mirror_flip_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) +char *mirror_flip_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) { if (which == TOOL_MIRROR) - return(strdup(gettext_noop("Mirror"))); + return (strdup(gettext_noop("Mirror"))); else if (which == TOOL_FLIP) - return(strdup(gettext_noop("Flip"))); + return (strdup(gettext_noop("Flip"))); - return(NULL); + return (NULL); } // 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."))); + return (strdup(gettext_noop("Click to make a mirror image."))); else - return(strdup( - gettext_noop("Click to flip the picture upside-down."))); + return (strdup(gettext_noop("Click to flip the picture upside-down."))); - return(NULL); + return (NULL); } // 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, - 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 mirror_flip_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 } void mirror_flip_release(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) + 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 } // 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) + SDL_Surface * canvas, SDL_Surface * last, + 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++) { - src.x = xx; - src.y = 0; - src.w = 1; - src.h = canvas->h; + for (xx = 0; xx < canvas->w; xx++) + { + 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); + SDL_BlitSurface(last, &src, canvas, &dest); + } + + api->special_notify(SPECIAL_MIRROR); } - - api->special_notify(SPECIAL_MIRROR); - } else if (which == TOOL_FLIP) - { - for (yy = 0; yy < canvas->h; yy++) { - src.x = 0; - src.y = yy; - src.w = canvas->w; - src.h = 1; + for (yy = 0; yy < canvas->h; yy++) + { + 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); + SDL_BlitSurface(last, &src, canvas, &dest); + } + + api->special_notify(SPECIAL_FLIP); } - api->special_notify(SPECIAL_FLIP); - } - update_rect->x = 0; update_rect->y = 0; update_rect->w = canvas->w; @@ -218,33 +205,28 @@ 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) +void mirror_flip_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 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) +void mirror_flip_switchin(magic_api * api 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) +void mirror_flip_switchout(magic_api * api 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); + return (MODE_FULLSCREEN); } diff --git a/magic/src/mosaic.c b/magic/src/mosaic.c index 061519968..499eab2cf 100644 --- a/magic/src/mosaic.c +++ b/magic/src/mosaic.c @@ -45,29 +45,22 @@ #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 */ Uint32 mosaic_api_version(void); int mosaic_init(magic_api *); int mosaic_get_tool_count(magic_api *); -SDL_Surface * mosaic_get_icon(magic_api *, int); -char * mosaic_get_name(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 *); +SDL_Surface *mosaic_get_icon(magic_api *, int); +char *mosaic_get_name(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_shutdown(magic_api *); void mosaic_set_color(magic_api *, Uint8, Uint8, Uint8); int mosaic_requires_colors(magic_api *, int); @@ -75,155 +68,169 @@ void mosaic_switchin(magic_api *, int, int, SDL_Surface *); void mosaic_switchout(magic_api *, int, int, SDL_Surface *); int mosaic_modes(magic_api *, int); -static const int mosaic_AMOUNT= 300; +static const int mosaic_AMOUNT = 300; static const int mosaic_RADIUS = 16; static const double mosaic_SHARPEN = 1.0; static int randnoise ATTRIBUTE_UNUSED; -Uint8 * mosaic_blured; -enum { - TOOL_MOSAIC, - mosaic_NUM_TOOLS +Uint8 *mosaic_blured; +enum +{ + TOOL_MOSAIC, + mosaic_NUM_TOOLS }; -static Mix_Chunk * mosaic_snd_effect[mosaic_NUM_TOOLS]; -static SDL_Surface * canvas_noise; -static SDL_Surface * canvas_blur; -static SDL_Surface * canvas_sharp; +static Mix_Chunk *mosaic_snd_effect[mosaic_NUM_TOOLS]; +static SDL_Surface *canvas_noise; +static SDL_Surface *canvas_blur; +static SDL_Surface *canvas_sharp; -const char * mosaic_snd_filenames[mosaic_NUM_TOOLS] = { - "mosaic.ogg", /* FIXME */ +const char *mosaic_snd_filenames[mosaic_NUM_TOOLS] = { + "mosaic.ogg", /* FIXME */ }; -const char * mosaic_icon_filenames[mosaic_NUM_TOOLS] = { + +const char *mosaic_icon_filenames[mosaic_NUM_TOOLS] = { "mosaic.png", }; -const char * mosaic_names[mosaic_NUM_TOOLS] = { + +const char *mosaic_names[mosaic_NUM_TOOLS] = { gettext_noop("Mosaic"), }; -const char * mosaic_descs[mosaic_NUM_TOOLS][2] = { + +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 to add a mosaic effect to your entire picture."),}, + gettext_noop("Click to add a mosaic effect to your entire picture."),}, }; -Uint32 mosaic_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 mosaic_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} //Load sounds -int mosaic_init(magic_api * api){ +int mosaic_init(magic_api * api) +{ int i; char fname[1024]; - for (i = 0; i < mosaic_NUM_TOOLS; i++){ - snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, mosaic_snd_filenames[i]); - mosaic_snd_effect[i] = Mix_LoadWAV(fname); - } + for (i = 0; i < mosaic_NUM_TOOLS; i++) + { + snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, mosaic_snd_filenames[i]); + mosaic_snd_effect[i] = Mix_LoadWAV(fname); + } - return(1); + return (1); } -int mosaic_get_tool_count(magic_api * api ATTRIBUTE_UNUSED){ - return(mosaic_NUM_TOOLS); +int mosaic_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) +{ + return (mosaic_NUM_TOOLS); } // Load our icons: -SDL_Surface * mosaic_get_icon(magic_api * api, int which){ +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]); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } // Return our names, localized: -char * mosaic_get_name(magic_api * api ATTRIBUTE_UNUSED, int which){ - return(strdup(gettext_noop(mosaic_names[which]))); +char *mosaic_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) +{ + return (strdup(gettext_noop(mosaic_names[which]))); } // Return our descriptions, localized: -char * mosaic_get_description(magic_api * api ATTRIBUTE_UNUSED, - int which, int mode){ - return(strdup(gettext_noop(mosaic_descs[which][mode-1]))); +char *mosaic_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode) +{ + return (strdup(gettext_noop(mosaic_descs[which][mode - 1]))); } //Calculates the grey scale value for a rgb pixel -static int mosaic_grey(Uint8 r1,Uint8 g1,Uint8 b1){ - return 0.3*r1+.59*g1+0.11*b1; +static int mosaic_grey(Uint8 r1, Uint8 g1, Uint8 b1) +{ + return 0.3 * r1 + .59 * g1 + 0.11 * 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){ +static void do_mosaic_full(void *ptr, SDL_Surface * canvas, + SDL_Surface * last ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +{ - magic_api * api = (magic_api *) ptr; + magic_api *api = (magic_api *) ptr; + + int x, y; + + 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->Rmask, + canvas->format->Gmask, + canvas->format->Bmask, amask); -int x,y; - -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->Rmask, - canvas->format->Gmask, - canvas->format->Bmask, amask); - api->update_progress_bar(); - for (y = 0; y < canvas->h; y++){ + 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++){ - mosaic_sharpen_pixel(api, canvas, mosaic_temp, x, y); + + for (y = 0; y < canvas->h; y++) + { + for (x = 0; x < canvas->w; x++) + { + 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) +void mosaic_paint(void *ptr_to_api, int which ATTRIBUTE_UNUSED, + 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; + 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 */ - } + 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++) + 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)); - } + 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)); + } } // 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); update_rect->x = min(ox, x) - mosaic_RADIUS; @@ -236,8 +243,8 @@ 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) { @@ -246,7 +253,7 @@ void mosaic_click(magic_api * api, int which, int mode, update_rect->w = canvas->w; update_rect->h = canvas->h; - do_mosaic_full(api, canvas_noise, last, which); + do_mosaic_full(api, canvas_noise, last, which); SDL_BlitSurface(canvas_noise, NULL, canvas, NULL); api->playsound(mosaic_snd_effect[which], 128, 255); } @@ -256,195 +263,200 @@ void mosaic_click(magic_api * api, int which, int mode, } // Affect the canvas on release: -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) +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) { } // No setup happened: void mosaic_shutdown(magic_api * api ATTRIBUTE_UNUSED) { - //Clean up sounds - int i; - for(i=0; igetpixel(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); - } + 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])); } //Blur a pixel -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; - Uint8 temp[3]; +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; + Uint8 temp[3]; double blurValue[3]; + //5x5 gaussiann weighting window - const int weight[5][5] = { {1,4,7,4,1}, - {4,16,26,16,4}, - {7,26,41,26,7}, - {4,16,26,16,4}, - {1,4,7,4,1}}; + const int weight[5][5] = { {1, 4, 7, 4, 1}, + {4, 16, 26, 16, 4}, + {7, 26, 41, 26, 7}, + {4, 16, 26, 16, 4}, + {1, 4, 7, 4, 1} + }; - for (k =0;k<3;k++){ - blurValue[k] = 0; - } - - for (i=-2;i<3;i++){ - 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]; - } + for (k = 0; k < 3; k++) + { + blurValue[k] = 0; } - } - for (k =0;k<3;k++){ - blurValue[k] /= 273; - } - api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, blurValue[0], blurValue[1], blurValue[2])); + + for (i = -2; i < 3; i++) + { + 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]; + } + } + } + for (k = 0; k < 3; k++) + { + 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; + magic_api *api = (magic_api *) ptr; - Uint8 r1, g1, b1; - int grey; - int i,j; - double sobel_1=0,sobel_2=0; - double temp; + Uint8 r1, g1, b1; + int grey; + int i, j; + double sobel_1 = 0, sobel_2 = 0; + double temp; - //Sobel weighting masks - const int sobel_weights_1[3][3] = { {1,2,1}, - {0,0,0}, - {-1,-2,-1}}; - const int sobel_weights_2[3][3] = { {-1,0,1}, - {-2,0,2}, - {-1,0,1}}; + //Sobel weighting masks + const int sobel_weights_1[3][3] = { {1, 2, 1}, + {0, 0, 0}, + {-1, -2, -1} + }; + const int sobel_weights_2[3][3] = { {-1, 0, 1}, + {-2, 0, 2}, + {-1, 0, 1} + }; - sobel_1=0; - sobel_2=0; - for (i=-1;i<2;i++){ - 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]; - } - } - - temp = sqrt(sobel_1*sobel_1 + sobel_2*sobel_2); - temp = (temp/1443)*255.0; + sobel_1 = 0; + sobel_2 = 0; + for (i = -1; i < 2; i++) + { + 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]; + } + } + + 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); - } - - 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); - - SDL_BlitSurface(canvas, NULL, canvas_noise, NULL); - - for (y = 0; y < canvas->h; y++){ - for (x=0; x < canvas->w; x++){ - mosaic_noise_pixel(api, canvas_noise, mosaic_AMOUNT, x, y); + { + fprintf(stderr, "\nError: Can't build drawing touch mask!\n"); + exit(1); } - } - canvas_blur = SDL_CreateRGBSurface(SDL_SWSURFACE, - canvas->w, - canvas->h, - canvas->format->BitsPerPixel, - canvas->format->Rmask, - canvas->format->Gmask, - canvas->format->Bmask, amask); + amask = ~(canvas->format->Rmask | canvas->format->Gmask | canvas->format->Bmask); - canvas_sharp = SDL_CreateRGBSurface(SDL_SWSURFACE, - canvas->w, - canvas->h, - canvas->format->BitsPerPixel, - canvas->format->Rmask, - canvas->format->Gmask, - canvas->format->Bmask, amask); - reset_mosaic_blured(canvas); + canvas_noise = SDL_CreateRGBSurface(SDL_SWSURFACE, + canvas->w, + canvas->h, + canvas->format->BitsPerPixel, + 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++) + { + 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_sharp = SDL_CreateRGBSurface(SDL_SWSURFACE, + canvas->w, + canvas->h, + canvas->format->BitsPerPixel, + 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) +void mosaic_switchout(magic_api * api ATTRIBUTE_UNUSED, + int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) { SDL_FreeSurface(canvas_noise); SDL_FreeSurface(canvas_blur); SDL_FreeSurface(canvas_sharp); - free (mosaic_blured); + free(mosaic_blured); } int mosaic_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_PAINT|MODE_FULLSCREEN); + return (MODE_PAINT | MODE_FULLSCREEN); } void reset_mosaic_blured(SDL_Surface * canvas) diff --git a/magic/src/mosaic_shaped.c b/magic/src/mosaic_shaped.c index 5a472050b..a0b0a3320 100644 --- a/magic/src/mosaic_shaped.c +++ b/magic/src/mosaic_shaped.c @@ -49,35 +49,31 @@ #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 deform(magic_api * api, SDL_Surface * srfc); -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 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); Uint32 mosaic_shaped_api_version(void); int mosaic_shaped_init(magic_api * api); int mosaic_shaped_get_tool_count(magic_api * api); -SDL_Surface * mosaic_shaped_get_icon(magic_api * api, int which); -char * mosaic_shaped_get_name(magic_api * api, int which); +SDL_Surface *mosaic_shaped_get_icon(magic_api * api, int which); +char *mosaic_shaped_get_name(magic_api * api, int which); -char * mosaic_shaped_get_description(magic_api * api, int which, int mode); +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); @@ -91,10 +87,11 @@ void mosaic_shaped_switchout(magic_api * api, int which, int mode, SDL_Surface * 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_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; @@ -102,214 +99,218 @@ Uint32 pixel_average, black, white; enum { - TOOL_SQUARE, - TOOL_HEX, - TOOL_IRREGULAR, - mosaic_shaped_NUM_TOOLS + TOOL_SQUARE, + TOOL_HEX, + TOOL_IRREGULAR, + mosaic_shaped_NUM_TOOLS }; -static Mix_Chunk * mosaic_shaped_snd_effect[mosaic_shaped_NUM_TOOLS]; -static SDL_Surface * canvas_shaped; -static SDL_Surface * canvas_back; -static SDL_Surface * mosaic_shaped_pattern; +static Mix_Chunk *mosaic_shaped_snd_effect[mosaic_shaped_NUM_TOOLS]; +static SDL_Surface *canvas_shaped; +static SDL_Surface *canvas_back; +static SDL_Surface *mosaic_shaped_pattern; -const char * mosaic_shaped_snd_filenames[mosaic_shaped_NUM_TOOLS] = -{ - "mosaic_shaped_square.ogg", - "mosaic_shaped_hex.ogg", - "mosaic_shaped_irregular.ogg", /* FIXME */ /*what's problem?*/ +const char *mosaic_shaped_snd_filenames[mosaic_shaped_NUM_TOOLS] = { + "mosaic_shaped_square.ogg", + "mosaic_shaped_hex.ogg", + "mosaic_shaped_irregular.ogg", /* FIXME *//*what's problem? */ }; -const char * mosaic_shaped_icon_filenames[mosaic_shaped_NUM_TOOLS] = -{ - "mosaic_shaped_square.png", - "mosaic_shaped_hex.png", - "mosaic_shaped_irregular.png", +const char *mosaic_shaped_icon_filenames[mosaic_shaped_NUM_TOOLS] = { + "mosaic_shaped_square.png", + "mosaic_shaped_hex.png", + "mosaic_shaped_irregular.png", }; -const char * mosaic_shaped_pattern_filenames[mosaic_shaped_NUM_TOOLS] = -{ - "mosaic_shaped_square_pattern.png", - "mosaic_shaped_hex_pattern.png", - "mosaic_shaped_irregular_pattern.png", +const char *mosaic_shaped_pattern_filenames[mosaic_shaped_NUM_TOOLS] = { + "mosaic_shaped_square_pattern.png", + "mosaic_shaped_hex_pattern.png", + "mosaic_shaped_irregular_pattern.png", }; -const char * mosaic_shaped_names[mosaic_shaped_NUM_TOOLS] = -{ - gettext_noop("Square Mosaic"), - gettext_noop("Hexagon Mosaic"), - gettext_noop("Irregular Mosaic"), +const char *mosaic_shaped_names[mosaic_shaped_NUM_TOOLS] = { + gettext_noop("Square Mosaic"), + gettext_noop("Hexagon Mosaic"), + gettext_noop("Irregular Mosaic"), }; -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 to add a square mosaic to your entire picture."), - }, +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 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 to add a hexagonal 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 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 to add an irregular 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 to add an irregular mosaic to your entire picture."), + }, }; Uint32 mosaic_shaped_api_version(void) { - return (TP_MAGIC_API_VERSION); + return (TP_MAGIC_API_VERSION); } //Load sounds int mosaic_shaped_init(magic_api * api) { - int i; - char fname[1024]; - mosaic_shaped_pattern = NULL; + int i; + char fname[1024]; - for (i = 0; i < mosaic_shaped_NUM_TOOLS; i++) + mosaic_shaped_pattern = NULL; + + for (i = 0; i < mosaic_shaped_NUM_TOOLS; i++) { - snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, mosaic_shaped_snd_filenames[i]); - mosaic_shaped_snd_effect[i] = Mix_LoadWAV(fname); + snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, mosaic_shaped_snd_filenames[i]); + mosaic_shaped_snd_effect[i] = Mix_LoadWAV(fname); } - return (1); + return (1); } int mosaic_shaped_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return (mosaic_shaped_NUM_TOOLS); + return (mosaic_shaped_NUM_TOOLS); } // Load our icons: -SDL_Surface * mosaic_shaped_get_icon(magic_api * api, int which) +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]); - return (IMG_Load(fname)); + char fname[1024]; + + snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, mosaic_shaped_icon_filenames[which]); + return (IMG_Load(fname)); } // Return our names, localized: -char * mosaic_shaped_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) +char *mosaic_shaped_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) { - return (strdup(gettext_noop(mosaic_shaped_names[which]))); + return (strdup(gettext_noop(mosaic_shaped_names[which]))); } // 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]))); + return (strdup(gettext_noop(mosaic_shaped_descs[which][mode - 1]))); } //Calculates the grey scale value for a rgb pixel static int mosaic_shaped_grey(Uint8 r1, Uint8 g1, Uint8 b1) { - return 0.3 * r1 + .59 * g1 + 0.11 * b1; + return 0.3 * r1 + .59 * g1 + 0.11 * 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); + int i, j, size; + Uint32 mosaic_shaped_color; + magic_api *api = (magic_api *) ptr; - for (i = 3; i < canvas->w - 3; i += 2) + 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(); + api->playsound(mosaic_shaped_snd_effect[which], 128, 255); + api->update_progress_bar(); - for (j = 3; j < canvas->h - 3; j += 2) + 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_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, +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; - magic_api * api = (magic_api *) ptr_to_api; - 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_average_r = 0; - mosaic_shaped_average_g = 0; - mosaic_shaped_average_b = 0; - mosaic_shaped_average_count = 0; + Uint32 mosaic_shaped_color; + int size; + magic_api *api = (magic_api *) ptr_to_api; - if (api->getpixel(canvas_shaped, x, y) == black) + 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_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); + scan_fill(api, canvas, canvas_shaped, x, y, 1, 0, 1, mosaic_shaped_color); - if (mosaic_shaped_average_count > 0) + 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; - update_rect->y = min(oy, y) - mosaic_shaped_pattern->h; - update_rect->w = max(ox, x) + mosaic_shaped_pattern->w - update_rect->x; - update_rect->h = max(oy, y) + mosaic_shaped_pattern->h - update_rect->y; - api->playsound(mosaic_shaped_snd_effect[which], (x * 255) / canvas->w, 255); + api->line(api, which, canvas, last, ox, oy, x, y, 1, mosaic_shaped_fill); + update_rect->x = min(ox, x) - mosaic_shaped_pattern->w; + update_rect->y = min(oy, y) - mosaic_shaped_pattern->h; + update_rect->w = max(ox, x) + mosaic_shaped_pattern->w - update_rect->x; + update_rect->h = max(oy, y) + mosaic_shaped_pattern->h - update_rect->y; + api->playsound(mosaic_shaped_snd_effect[which], (x * 255) / canvas->w, 255); } // 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) + 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 + 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); } } @@ -323,14 +324,14 @@ void mosaic_shaped_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE // No setup happened: void mosaic_shaped_shutdown(magic_api * api ATTRIBUTE_UNUSED) { - //Clean up sounds - int i; + //Clean up sounds + int i; - for (i = 0; i < mosaic_shaped_NUM_TOOLS; 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]); } } } @@ -338,58 +339,58 @@ void mosaic_shaped_shutdown(magic_api * api ATTRIBUTE_UNUSED) // Record the color from Tux Paint: void mosaic_shaped_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) { - mosaic_shaped_r = r; - mosaic_shaped_g = g; - mosaic_shaped_b = b; + mosaic_shaped_r = r; + mosaic_shaped_g = g; + mosaic_shaped_b = b; } // Use colors: int mosaic_shaped_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return 1; + 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) +static void mosaic_shaped_sharpen_pixel(void *ptr, + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last, int x, int y) { - magic_api * api = (magic_api *) ptr; - Uint8 r1, g1, b1; - int grey; - int i, j; - double sobel_1 = 0, sobel_2 = 0; - double temp; - //Sobel weighting masks - const int sobel_weights_1[3][3] = { {1, 2, 1}, - {0, 0, 0}, - { -1, -2, -1} - }; - const int sobel_weights_2[3][3] = { { -1, 0, 1}, - { -2, 0, 2}, - { -1, 0, 1} - }; - sobel_1 = 0; - sobel_2 = 0; + magic_api *api = (magic_api *) ptr; + Uint8 r1, g1, b1; + int grey; + int i, j; + double sobel_1 = 0, sobel_2 = 0; + double temp; - for (i = -1; i < 2; i++) + //Sobel weighting masks + const int sobel_weights_1[3][3] = { {1, 2, 1}, + {0, 0, 0}, + {-1, -2, -1} + }; + const int sobel_weights_2[3][3] = { {-1, 0, 1}, + {-2, 0, 2}, + {-1, 0, 1} + }; + 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_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; + temp = sqrt(sobel_1 * sobel_1 + sobel_2 * sobel_2); + temp = (temp / 1443) * 255.0; - if (temp > 25) + 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)); } } @@ -397,92 +398,99 @@ static void mosaic_shaped_sharpen_pixel(void * ptr, void mosaic_shaped_switchin(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas) { - int y, x; - int i, j; - SDL_Rect rect; - SDL_Surface * surf_aux, *tmp, *tmp2; - Uint32 amask; - mosaic_shaped_counted = (Uint8 *) malloc(sizeof(Uint8) * (canvas->w * canvas->h)); + int y, x; + int i, j; + SDL_Rect rect; + SDL_Surface *surf_aux, *tmp, *tmp2; + Uint32 amask; - if (mosaic_shaped_counted == NULL) + 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) + 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_shaped = SDL_ConvertSurfaceFormat(tmp, SDL_PIXELFORMAT_RGB888, 0); - SDL_FreeSurface(tmp); + tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, + canvas->w, + canvas->h, + canvas->format->BitsPerPixel, + canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, amask); + canvas_shaped = SDL_ConvertSurfaceFormat(tmp, SDL_PIXELFORMAT_RGB888, 0); + SDL_FreeSurface(tmp); - tmp2 = SDL_CreateRGBSurface(SDL_SWSURFACE, - canvas->w + 10, - canvas->h + 10, - canvas->format->BitsPerPixel, - canvas->format->Rmask, - canvas->format->Gmask, - canvas->format->Bmask, amask); + tmp2 = SDL_CreateRGBSurface(SDL_SWSURFACE, + canvas->w + 10, + canvas->h + 10, + canvas->format->BitsPerPixel, + canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, amask); - /* Generation of patterns now in the program, solves #210 */ - if (which == TOOL_SQUARE) + /* 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)); + 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)); - } + 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)); + 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, 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) + 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)); + 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); @@ -495,8 +503,8 @@ void mosaic_shaped_switchin(magic_api * api, int which, int mode ATTRIBUTE_UNUSE 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, 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); @@ -514,245 +522,252 @@ void mosaic_shaped_switchin(magic_api * api, int which, int mode ATTRIBUTE_UNUSE api->line(api, which, mosaic_shaped_pattern, NULL, 61, 44, 52, 31, 1, mosaic_shaped_paint); } - else if(which == TOOL_HEX) + 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)); } - rect.w = mosaic_shaped_pattern->w; - rect.h = mosaic_shaped_pattern->h; + rect.w = mosaic_shaped_pattern->w; + rect.h = mosaic_shaped_pattern->h; - 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); - } + 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); + } - if (which == TOOL_IRREGULAR) + 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); - SDL_FreeSurface(surf_aux); - black = SDL_MapRGBA(canvas->format, 0, 0, 0, 0); - white = SDL_MapRGBA(canvas->format, 255, 255, 255, 0); + SDL_SetSurfaceBlendMode(surf_aux, SDL_BLENDMODE_NONE); + SDL_BlitSurface(surf_aux, NULL, canvas_shaped, NULL); + SDL_FreeSurface(surf_aux); + black = SDL_MapRGBA(canvas->format, 0, 0, 0, 0); + white = SDL_MapRGBA(canvas->format, 255, 255, 255, 0); - /* Two black lines at the edges */ - for (i = 0; i < canvas->w; i++) + /* 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++) + 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); - SDL_BlitSurface(canvas, NULL, canvas_back, NULL); + /* 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); + 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++) - { - mosaic_shaped_sharpen_pixel(api, canvas_shaped, canvas, x, y); - } - } + 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++) + { + mosaic_shaped_sharpen_pixel(api, canvas_shaped, canvas, x, y); + } + } - reset_counter(canvas, mosaic_shaped_counted); - reset_counter(canvas, mosaic_shaped_done); + 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, SDL_Surface * canvas 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); - SDL_FreeSurface(canvas_back); - SDL_FreeSurface(mosaic_shaped_pattern); - free(mosaic_shaped_counted); + SDL_FreeSurface(canvas_shaped); + SDL_FreeSurface(canvas_back); + SDL_FreeSurface(mosaic_shaped_pattern); + free(mosaic_shaped_counted); } int mosaic_shaped_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return (MODE_PAINT | MODE_FULLSCREEN); + return (MODE_PAINT | MODE_FULLSCREEN); } void reset_counter(SDL_Surface * canvas, Uint8 * counter) { - int i, j; + int i, j; - for (j = 0; j < canvas->h; j++) - for (i = 0; i < canvas->w; i++) - { - counter[j * canvas->w + i] = 0; - } + for (j = 0; j < canvas->h; j++) + for (i = 0; i < canvas->w; i++) + { + 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; - int i; + int leftx, rightx; + Uint8 r, g, b, a; + int i; - leftx = x - 1; - rightx = x + 1; + leftx = x - 1; + rightx = x + 1; - /* Abort, if we recurse too deep! -bjk 2014.08.05 */ - scan_fill_count++; - if (scan_fill_count > 500 ) + /* 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) + 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 (api->getpixel(srfc, x, y) == black) { - if (fill_edge == 1) + if (fill_edge == 1) { - fill_square(api, canvas, x, y, size, color); + fill_square(api, canvas, x, y, size, color); } - scan_fill_count--; - return (0); /* No need to check more */ + scan_fill_count--; + return (0); /* No need to check more */ } - if (fill_tile == 1) + if (fill_tile == 1) { - 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; + 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; } - else + 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)) + /* Search right */ + while (scan_fill(api, canvas, srfc, rightx, y, fill_edge, fill_tile, size, color) && (rightx < canvas->w)) { - rightx ++; + rightx++; } - /* Search left */ - while (scan_fill(api, canvas, srfc, leftx, y, fill_edge, fill_tile, size, color) && (leftx >= 0)) + /* Search left */ + while (scan_fill(api, canvas, srfc, leftx, y, fill_edge, fill_tile, size, color) && (leftx >= 0)) { - leftx --; + leftx--; } - /* Top / bottom */ - for (i = leftx; i <= rightx; i++) + /* 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); + scan_fill(api, canvas, srfc, i, y - 1, fill_edge, fill_tile, size, color); } - if (y + 1 < canvas->w) + 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); } } - scan_fill_count--; - return (1); + scan_fill_count--; + return (1); } void fill_square(magic_api * api, SDL_Surface * canvas, int x, int y, int size, Uint32 color) { - int i, j; + 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); - } + for (i = (x - size); i < (x + 1 + size); i++) + for (j = (y - size); j < (y + 1 + size); j++) + { + api->putpixel(canvas, i, j, color); + } } void deform(magic_api * api, SDL_Surface * srfc) { - int i, j; - - 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)); - } + int i, j; + for (j = 0; j < srfc->h; 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 + 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)); + } } /* 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; + magic_api *api = (magic_api *) ptr; int radius, shadow; int i, j, ii, jj; Uint8 r, g, b, a; @@ -765,34 +780,41 @@ static void mosaic_shaped_paint(void * ptr, int which ATTRIBUTE_UNUSED, SDL_Surf 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) { - SDL_GetRGBA(shadow_tone, canvas->format, &r, &g, &b, &a); + /* 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; - /* Shadows should be shadows, not black */ - if (r > 10) r -= 9; - if (g > 10) g -= 9; - if (b > 10) b -= 9; + /* Shadow_tone is also used as a marker, anything already painted on black must finally be black */ + shadow_tone = api->getpixel(canvas, ii, jj); - api->putpixel(canvas, ii, jj, SDL_MapRGBA(canvas->format, r, g, b, SDL_ALPHA_OPAQUE)); + // 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)); + + } } - - } } diff --git a/magic/src/negative.c b/magic/src/negative.c index 178d8590a..53479761f 100644 --- a/magic/src/negative.c +++ b/magic/src/negative.c @@ -33,27 +33,22 @@ #include "SDL_image.h" #include "SDL_mixer.h" -static Mix_Chunk * negative_snd; +static Mix_Chunk *negative_snd; int negative_init(magic_api * api); Uint32 negative_api_version(void); int negative_get_tool_count(magic_api * api); -SDL_Surface * negative_get_icon(magic_api * api, int which); -char * negative_get_name(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); +SDL_Surface *negative_get_icon(magic_api * api, int which); +char *negative_get_name(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); 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); + 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_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); @@ -66,90 +61,98 @@ int negative_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/negative.wav", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/negative.wav", api->data_directory); negative_snd = Mix_LoadWAV(fname); - return(1); + return (1); } -Uint32 negative_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 negative_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} // Only one tool: int negative_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(1); + return (1); } // Load our icon: -SDL_Surface * negative_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *negative_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/negative.png", - api->data_directory); - return(IMG_Load(fname)); + snprintf(fname, sizeof(fname), "%s/images/magic/negative.png", api->data_directory); + return (IMG_Load(fname)); } // Return our name, localized: -char * negative_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *negative_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(strdup(gettext_noop("Negative"))); + return (strdup(gettext_noop("Negative"))); } // Return our description, localized: -char * negative_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode) +char *negative_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 make your painting negative."))); /* Does this make more sense? */ + return (strdup(gettext_noop("Click and drag the mouse around to make your painting negative."))); /* Does this make more sense? */ else if (mode == MODE_FULLSCREEN) - return(strdup( - gettext_noop("Click to turn your painting into its negative."))); + return (strdup(gettext_noop("Click to turn your painting into its negative."))); else - return(NULL); + return (NULL); } // Callback that does the negative color effect on a circle centered around x,y -static void do_negative(void * ptr, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * last, - int x, int y) +static void do_negative(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * last, int x, int y) { int xx, yy; Uint8 r, g, b; - magic_api * api = (magic_api *) ptr; + magic_api *api = (magic_api *) ptr; for (yy = y - 16; yy < y + 16; yy++) - { - 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); + 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); - r = 0xFF - r; - g = 0xFF - g; - b = 0xFF - b; + r = 0xFF - r; + g = 0xFF - g; + b = 0xFF - b; - api->putpixel(canvas, xx, yy, SDL_MapRGB(canvas->format, r, g, b)); - } + api->putpixel(canvas, xx, yy, SDL_MapRGB(canvas->format, r, g, 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; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - 16; update_rect->y = oy - 16; @@ -165,41 +168,40 @@ 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; - - 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); + int xx, yy; + Uint8 r, g, b; - r = 0xFF - r; - g = 0xFF - g; - b = 0xFF - 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); - api->putpixel(canvas, xx, yy, SDL_MapRGB(canvas->format, r, g, b)); - } + r = 0xFF - r; + g = 0xFF - g; + b = 0xFF - b; + + api->putpixel(canvas, xx, yy, 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; } - - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; - } } 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -211,7 +213,8 @@ 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, Uint8 b ATTRIBUTE_UNUSED) +void negative_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, + Uint8 b ATTRIBUTE_UNUSED) { } @@ -221,15 +224,17 @@ int negative_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBU return 0; } -void negative_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas 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, SDL_Surface * canvas 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) { - return(MODE_PAINT | MODE_FULLSCREEN); + return (MODE_PAINT | MODE_FULLSCREEN); } diff --git a/magic/src/noise.c b/magic/src/noise.c index 806b98b8c..83858503e 100644 --- a/magic/src/noise.c +++ b/magic/src/noise.c @@ -46,46 +46,45 @@ static const int noise_AMOUNT = 100.0; static const int noise_RADIUS = 16; -enum { - TOOL_NOISE, - noise_NUM_TOOLS +enum +{ + TOOL_NOISE, + noise_NUM_TOOLS }; -static Mix_Chunk * noise_snd_effect[noise_NUM_TOOLS]; +static Mix_Chunk *noise_snd_effect[noise_NUM_TOOLS]; -const char * noise_snd_filenames[noise_NUM_TOOLS] = { +const char *noise_snd_filenames[noise_NUM_TOOLS] = { "noise.ogg", }; -const char * noise_icon_filenames[noise_NUM_TOOLS] = { + +const char *noise_icon_filenames[noise_NUM_TOOLS] = { "noise.png", }; -const char * noise_names[noise_NUM_TOOLS] = { + +const char *noise_names[noise_NUM_TOOLS] = { gettext_noop("Noise"), }; -const char * noise_descs[noise_NUM_TOOLS][2] = { + +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 to add noise to your entire picture."),}, + gettext_noop("Click to add noise to your entire picture."),}, }; Uint32 noise_api_version(void); int noise_init(magic_api * api); -SDL_Surface * noise_get_icon(magic_api * api, int which); -char * noise_get_name(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); +SDL_Surface *noise_get_icon(magic_api * api, int which); +char *noise_get_name(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); 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); 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); void noise_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 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); @@ -94,101 +93,127 @@ 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); -Uint32 noise_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 noise_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} //Load sounds -int noise_init(magic_api * api){ +int noise_init(magic_api * api) +{ int i; char fname[1024]; srand(time(0)); - for (i = 0; i < noise_NUM_TOOLS; i++){ - snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, noise_snd_filenames[i]); - noise_snd_effect[i] = Mix_LoadWAV(fname); - } - return(1); + for (i = 0; i < noise_NUM_TOOLS; i++) + { + snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, noise_snd_filenames[i]); + noise_snd_effect[i] = Mix_LoadWAV(fname); + } + return (1); } -int noise_get_tool_count(magic_api * api ATTRIBUTE_UNUSED){ - return(noise_NUM_TOOLS); +int noise_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) +{ + return (noise_NUM_TOOLS); } // Load our icons: -SDL_Surface * noise_get_icon(magic_api * api, int which){ +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]); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } // Return our names, localized: -char * noise_get_name(magic_api * api ATTRIBUTE_UNUSED, int which){ - return(strdup(gettext_noop(noise_names[which]))); +char *noise_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) +{ + return (strdup(gettext_noop(noise_names[which]))); } // Return our descriptions, localized: -char * noise_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode){ - return(strdup(gettext_noop(noise_descs[which][mode-1]))); +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){ - magic_api * api = (magic_api *) ptr; +static void do_noise_pixel(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) +{ + magic_api *api = (magic_api *) ptr; Uint8 temp[3]; double temp2[3]; int k; - 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])); + 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])); } // Do the effect for the full image -static void do_noise_full(void * ptr,SDL_Surface * canvas, SDL_Surface * last, int which){ - int x,y; +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++){ - do_noise_pixel(ptr, which, canvas, last, x, y); - } - } + for (y = 0; y < last->h; y++) + { + for (x = 0; x < last->w; x++) + { + 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; + 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++) { - if (api->in_circle(xx - x, yy - y, noise_RADIUS) && - !api->touched(xx, yy)) - { - do_noise_pixel(api, which, canvas, last, xx, yy); - } + 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); + } + } } - } } // 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){ - - api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, do_noise_brush); + 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->playsound(noise_snd_effect[which], (x * 255) / canvas->w, 255); - if (ox > x) { int tmp = ox; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - noise_RADIUS; update_rect->y = oy - noise_RADIUS; @@ -198,41 +223,46 @@ 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); - } + 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); + } } // 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } // No setup happened: void noise_shutdown(magic_api * api ATTRIBUTE_UNUSED) { - //Clean up sounds - int i; - for(i=0; idata_directory, perspective_snd_filenames[i]); - perspective_snd_effect[i] = Mix_LoadWAV(fname); - } - return(1); +Uint32 perspective_api_version(void) +{ + return (TP_MAGIC_API_VERSION); } -int perspective_get_tool_count(magic_api * api ATTRIBUTE_UNUSED){ - return(perspective_NUM_TOOLS); +//Load sounds +int perspective_init(magic_api * api) +{ + int i; + char fname[1024]; + + for (i = 0; i <= perspective_NUM_TOOLS; i++) + { + snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, perspective_snd_filenames[i]); + perspective_snd_effect[i] = Mix_LoadWAV(fname); + } + return (1); +} + +int perspective_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) +{ + return (perspective_NUM_TOOLS); } // Load our icons: -SDL_Surface * perspective_get_icon(magic_api * api, int which){ +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]); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } // Return our names, localized: -char * perspective_get_name(magic_api * api ATTRIBUTE_UNUSED, int which){ - return(strdup(gettext_noop(perspective_names[which]))); +char *perspective_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) +{ + return (strdup(gettext_noop(perspective_names[which]))); } // Return our descriptions, localized: -char * perspective_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode ATTRIBUTE_UNUSED){ - return(strdup(gettext_noop(perspective_descs[which]))); +char *perspective_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode ATTRIBUTE_UNUSED) +{ + return (strdup(gettext_noop(perspective_descs[which]))); } - + // 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_Rect * update_rect){ + SDL_Surface * last, int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, int x, int y, + SDL_Rect * update_rect) +{ switch (which) { case TOOL_PERSPECTIVE: { - switch (corner) - { - case TOP_LEFT: - { - top_left_x = x; - top_left_y = y; - } - break; + switch (corner) + { + 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; - } + case BOTTOM_RIGHT: + { + bottom_right_x = x; + bottom_right_y = y; + } + break; + } - SDL_BlitSurface(canvas_back, NULL, canvas, NULL); + SDL_BlitSurface(canvas_back, NULL, canvas, NULL); - perspective_preview( api, which, - canvas , last, - x, y , update_rect , 2.0); + 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); + /* 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); + /* 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); + api->playsound(perspective_snd_effect[which], (x * 255) / canvas->w, 255); } break; case TOOL_ZOOM: { - int x_distance, y_distance; + int x_distance, y_distance; - update_rect->x = update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; + 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)); + 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; + 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; + 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; + 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); + 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; + update_rect->x = update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; } break; @@ -303,43 +316,44 @@ 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){ - switch(which) - {case TOOL_PERSPECTIVE: - { + SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect) +{ + switch (which) + { + case TOOL_PERSPECTIVE: + { - if (x < canvas->w / 2) - { - if (y < canvas->h / 2) - { - corner = TOP_LEFT; - } - else - { - corner = BOTTOM_LEFT; - } - } - else - { - if (y < canvas->h / 2) - { - corner = TOP_RIGHT; - } - else - { - corner = BOTTOM_RIGHT; - } - } + if (x < canvas->w / 2) + { + if (y < canvas->h / 2) + { + corner = TOP_LEFT; + } + else + { + corner = BOTTOM_LEFT; + } + } + else + { + if (y < canvas->h / 2) + { + corner = TOP_RIGHT; + } + else + { + corner = BOTTOM_RIGHT; + } + } - } - break; + } + break; case TOOL_ZOOM: { - click_x = x; - click_y = y; - old_h = new_h; + click_x = x; + click_y = y; + old_h = new_h; } break; } @@ -349,70 +363,67 @@ void perspective_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, // 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) { switch (which) { - case TOOL_PERSPECTIVE:{ - perspective_preview( api, which, - canvas , last, - x, y , update_rect , 0.5); - } + case TOOL_PERSPECTIVE: + { + perspective_preview(api, which, canvas, last, x, y, update_rect, 0.5); + } break; case TOOL_ZOOM: - { - SDL_Surface * aux_surf; - SDL_Surface * scaled_surf; + { + 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; + 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)); + SDL_FillRect(canvas, update_rect, SDL_MapRGB(canvas->format, perspective_r, perspective_g, perspective_b)); - if(new_h < canvas->h) - { - scaled_surf = api->scale(canvas_back, new_w, new_h, 0); - update_rect->x = (canvas->w - new_w) / 2; - update_rect->y = (canvas->h - new_h) / 2; - update_rect->w = new_w; - update_rect->h = new_h; - SDL_BlitSurface(scaled_surf, NULL, canvas, update_rect); - } - else - { - int aux_x, aux_y, aux_h, aux_w; - aux_h = canvas->h * canvas->h / new_h; - aux_w = canvas->w * aux_h / canvas->h; - aux_x = canvas->w / 2 - aux_w / 2; - aux_y = canvas->h / 2 - aux_h / 2; + if (new_h < canvas->h) + { + scaled_surf = api->scale(canvas_back, new_w, new_h, 0); + update_rect->x = (canvas->w - new_w) / 2; + update_rect->y = (canvas->h - new_h) / 2; + update_rect->w = new_w; + update_rect->h = new_h; + SDL_BlitSurface(scaled_surf, NULL, canvas, update_rect); + } + else + { + int aux_x, aux_y, aux_h, aux_w; - 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_h = canvas->h * canvas->h / new_h; + aux_w = canvas->w * aux_h / canvas->h; + aux_x = canvas->w / 2 - aux_w / 2; + aux_y = canvas->h / 2 - aux_h / 2; - aux_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, - aux_w, - aux_h, - canvas->format->BitsPerPixel, - canvas->format->Rmask, - canvas->format->Gmask, - canvas->format->Bmask, 0); + 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; - 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); + aux_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, + aux_w, + aux_h, + canvas->format->BitsPerPixel, + canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, 0); - update_rect->x = update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; + 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; } break; @@ -420,8 +431,8 @@ void perspective_release(magic_api * api, int which, } void perspective_preview(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, 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; @@ -437,7 +448,7 @@ void perspective_preview(magic_api * api, int which ATTRIBUTE_UNUSED, 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; @@ -454,21 +465,21 @@ void perspective_preview(magic_api * api, int which ATTRIBUTE_UNUSED, 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) + 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; + 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) - { - 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)); + } + } } // No setup happened: @@ -476,11 +487,14 @@ void perspective_shutdown(magic_api * api ATTRIBUTE_UNUSED) { //Clean up sounds int i; - for(i=0; iw; new_h = canvas->h; @@ -513,36 +529,36 @@ void perspective_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_ 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->w, + canvas->h, + canvas->format->BitsPerPixel, + 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, SDL_Surface * canvas 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); } int perspective_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_PAINT_WITH_PREVIEW); + return (MODE_PAINT_WITH_PREVIEW); } -void perspective_line(void * ptr_to_api, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * snapshot ATTRIBUTE_UNUSED, int x, int y) +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; + magic_api *api = (magic_api *) ptr_to_api; + dash += 1; - if (dash > 8) dash = 0; + if (dash > 8) + dash = 0; if (dash > 3) api->putpixel(canvas, x, y, black); else diff --git a/magic/src/puzzle.c b/magic/src/puzzle.c index 6f794b2b9..362339004 100644 --- a/magic/src/puzzle.c +++ b/magic/src/puzzle.c @@ -27,12 +27,12 @@ */ -#include //for time() +#include //for time() #include "tp_magic_api.h" #include "SDL_image.h" #include "SDL_mixer.h" -#define RATIO 5 //change this value to get bigger puzzle +#define RATIO 5 //change this value to get bigger puzzle //TODO: Fullscreen mode //In fullscreen mode RATIO _should_ be 1 @@ -40,49 +40,48 @@ //else not whole the screen will be affected -static Mix_Chunk * puzzle_snd; -static int puzzle_gcd=0; //length of side of each rectangle; 0 is temporary value. -// static int puzzle_rect_q=4; //quantity of rectangles when using paint mode. Must be an odd value - but it's even! -static int rects_w, rects_h; -SDL_Surface * canvas_backup; +static Mix_Chunk *puzzle_snd; +static int puzzle_gcd = 0; //length of side of each rectangle; 0 is temporary value. -Uint32 puzzle_api_version(void) ; +// static int puzzle_rect_q=4; //quantity of rectangles when using paint mode. Must be an odd value - but it's even! +static int rects_w, rects_h; +SDL_Surface *canvas_backup; + +Uint32 puzzle_api_version(void); int puzzle_init(magic_api * api); int puzzle_get_tool_count(magic_api * api); -SDL_Surface * puzzle_get_icon(magic_api * api, int which); -char * puzzle_get_name(magic_api * api, int which); -char * puzzle_get_description(magic_api * api, int which, int mode); +SDL_Surface *puzzle_get_icon(magic_api * api, int which); +char *puzzle_get_name(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); 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) { return(TP_MAGIC_API_VERSION); } +Uint32 puzzle_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} int puzzle_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/puzzle.wav", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/puzzle.wav", api->data_directory); puzzle_snd = Mix_LoadWAV(fname); - return 1 ; + return 1; } int puzzle_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) @@ -90,32 +89,31 @@ int puzzle_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) return 1; } -SDL_Surface * puzzle_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *puzzle_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/puzzle.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/puzzle.png", api->data_directory); - return(IMG_Load(fname)); + 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"))); + return (strdup(gettext_noop("Puzzle"))); } -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.")); + 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 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -125,7 +123,8 @@ 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, Uint8 b ATTRIBUTE_UNUSED) +void puzzle_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, + Uint8 b ATTRIBUTE_UNUSED) { } @@ -134,21 +133,26 @@ int puzzle_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE return 0; } -int gcd(int a, int b) //greatest common divisor +int gcd(int a, int b) //greatest common divisor { - if (b==0) return a; - return gcd(b, a%b); + if (b == 0) + return a; + return gcd(b, a % b); } -void puzzle_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas) +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; - 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); + puzzle_gcd = RATIO * gcd(canvas->w, canvas->h); + 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); } -void puzzle_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas 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); canvas_backup = NULL; @@ -156,17 +160,17 @@ void puzzle_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUS int puzzle_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_PAINT); + return (MODE_PAINT); } -static void puzzle_draw(void * ptr, int which_tool ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * snapshot ATTRIBUTE_UNUSED, int x, int y) -{ - - - magic_api * api = (magic_api *) ptr; - - Uint8 r; //r - random value +static void puzzle_draw(void *ptr, int which_tool ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * snapshot ATTRIBUTE_UNUSED, int x, int y) +{ + + + magic_api *api = (magic_api *) ptr; + + Uint8 r; //r - random value SDL_Rect rect_this, rect_that; SDL_BlitSurface(canvas, NULL, canvas_backup, NULL); @@ -175,70 +179,68 @@ 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) - { - case 0: //upper - if (y>puzzle_gcd) - rect_that.y=y-puzzle_gcd; - - break; - case 1: //right - if (xw-puzzle_gcd) - rect_that.x=x-puzzle_gcd; - - break; - case 2: //lower - if (yh-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; + { + srand(rand()); - - 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); - } + r = rand() % 4; + + rect_that.x = x; + rect_that.y = y; + + 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); + } } 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_Rect * update_rect) + 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); - - update_rect->x=0; - update_rect->y=0; - update_rect->h=canvas->h; - update_rect->w=canvas->w; + 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; + update_rect->h = canvas->h; + update_rect->w = canvas->w; } 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); + 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 c7f5b092f..8ea4f618f 100644 --- a/magic/src/rails.c +++ b/magic/src/rails.c @@ -21,96 +21,94 @@ #define SEG_RIGHT_TOP_BOTTOM (SEG_RIGHT | SEG_TOP | SEG_BOTTOM) #define SEG_LEFT_RIGHT_TOP_BOTTOM (SEG_LEFT | SEG_RIGHT | SEG_TOP | SEG_BOTTOM) -Mix_Chunk * rails_snd; +Mix_Chunk *rails_snd; unsigned int img_w, img_h; -unsigned int rails_segments_x, rails_segments_y; //how many segments do we have? -static int rails_math_ceil(int x, int y); //ceil() in cstdlib returns float and is relative slow, so we'll use our one -static Uint8 * rails_status_of_segments; //a place to store an info about bitmap used for selected segment -static char ** rails_images; //the pathes to all the images needed -static unsigned int rails_segment_modified; //which segment was modified this time? -static unsigned int rails_segment_modified_last =0; //which segment was last modified -static unsigned int rails_segment_to_add =0; //a segment that should be added to solve corner joints +unsigned int rails_segments_x, rails_segments_y; //how many segments do we have? +static int rails_math_ceil(int x, int y); //ceil() in cstdlib returns float and is relative slow, so we'll use our one +static Uint8 *rails_status_of_segments; //a place to store an info about bitmap used for selected segment +static char **rails_images; //the pathes to all the images needed +static unsigned int rails_segment_modified; //which segment was modified this time? +static unsigned int rails_segment_modified_last = 0; //which segment was last modified +static unsigned int rails_segment_to_add = 0; //a segment that should be added to solve corner joints static SDL_Rect modification_rect; -static SDL_Surface * canvas_backup; -// Housekeeping functions +static SDL_Surface *canvas_backup; -SDL_Surface * rails_one, * rails_three, * rails_four, * rails_corner; +// Housekeeping functions + +SDL_Surface *rails_one, *rails_three, *rails_four, *rails_corner; Uint32 rails_api_version(void); int rails_modes(magic_api * api, int which); void rails_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int rails_init(magic_api * api); int rails_get_tool_count(magic_api * api); -SDL_Surface * rails_get_icon(magic_api * api, int which); -char * rails_get_name(magic_api * api, int which); -char * rails_get_description(magic_api * api, int which, int mode); +SDL_Surface *rails_get_icon(magic_api * api, int which); +char *rails_get_name(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); static int rails_math_ceil(int x, int y); -inline unsigned int rails_get_segment(int x, int y); +inline unsigned int rails_get_segment(int x, int y); inline void rails_extract_coords_from_segment(unsigned int segment, Sint16 * x, Sint16 * 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); +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); + 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) { - return(TP_MAGIC_API_VERSION); + return (TP_MAGIC_API_VERSION); } int rails_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_PAINT); + return (MODE_PAINT); } -void rails_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) +void rails_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, + Uint8 b ATTRIBUTE_UNUSED) { } 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); - - for (i = 0; i < 4; i++) - rails_images[i]=(char *)malloc(sizeof(char)*1024); - - snprintf(rails_images[0], 1024*sizeof(char), "%s/images/magic/rails_one.png", api->data_directory); - snprintf(rails_images[1], 1024*sizeof(char), "%s/images/magic/rails_three.png", api->data_directory); - snprintf(rails_images[2], 1024*sizeof(char), "%s/images/magic/rails_four.png", api->data_directory); - snprintf(rails_images[3], 1024*sizeof(char), "%s/images/magic/rails_corner.png", api->data_directory); + char fname[1024]; + Uint8 i; //is always < 3, so Uint8 seems to be a good idea - rails_one=IMG_Load(rails_images[0]); - rails_three=IMG_Load(rails_images[1]); - rails_four=IMG_Load(rails_images[2]); - rails_corner=IMG_Load(rails_images[3]); + rails_images = (char **)malloc(sizeof(char *) * 4); - img_w = rails_one->w; - img_h = rails_one->h; - - snprintf(fname, sizeof(fname), "%s/sounds/magic/rails.wav", api->data_directory); - rails_snd = Mix_LoadWAV(fname); + for (i = 0; i < 4; i++) + rails_images[i] = (char *)malloc(sizeof(char) * 1024); - return(1); + snprintf(rails_images[0], 1024 * sizeof(char), "%s/images/magic/rails_one.png", api->data_directory); + snprintf(rails_images[1], 1024 * sizeof(char), "%s/images/magic/rails_three.png", api->data_directory); + snprintf(rails_images[2], 1024 * sizeof(char), "%s/images/magic/rails_four.png", api->data_directory); + snprintf(rails_images[3], 1024 * sizeof(char), "%s/images/magic/rails_corner.png", api->data_directory); + + rails_one = IMG_Load(rails_images[0]); + rails_three = IMG_Load(rails_images[1]); + rails_four = IMG_Load(rails_images[2]); + rails_corner = IMG_Load(rails_images[3]); + + img_w = rails_one->w; + img_h = rails_one->h; + + snprintf(fname, sizeof(fname), "%s/sounds/magic/rails.wav", api->data_directory); + rails_snd = Mix_LoadWAV(fname); + + return (1); } int rails_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) @@ -118,407 +116,434 @@ int rails_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) return 1; } -SDL_Surface * rails_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *rails_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/rails.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/rails.png", api->data_directory); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } -char * rails_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { return strdup(gettext_noop("Rails")); } +char *rails_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +{ + return strdup(gettext_noop("Rails")); +} -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.")); } +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.")); +} -int rails_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { return 0;} +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) + 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_shutdown(magic_api * api ATTRIBUTE_UNUSED) { - Uint8 i; - - if (rails_snd!=NULL) - Mix_FreeChunk(rails_snd); - SDL_FreeSurface(rails_one); - SDL_FreeSurface(rails_three); - SDL_FreeSurface(rails_four); - SDL_FreeSurface(rails_corner); - SDL_FreeSurface(canvas_backup); - - for (i = 0; i < 4; i++) - free(rails_images[i]); - free(rails_images); - if (rails_status_of_segments != NULL) - free(rails_status_of_segments); + Uint8 i; + + if (rails_snd != NULL) + Mix_FreeChunk(rails_snd); + SDL_FreeSurface(rails_one); + SDL_FreeSurface(rails_three); + SDL_FreeSurface(rails_four); + SDL_FreeSurface(rails_corner); + SDL_FreeSurface(canvas_backup); + + for (i = 0; i < 4; i++) + free(rails_images[i]); + free(rails_images); + if (rails_status_of_segments != NULL) + free(rails_status_of_segments); } -void rails_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas) +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 + //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)); + 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)); } -void rails_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas 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; - } + if (rails_status_of_segments != NULL) + { + free(rails_status_of_segments); + rails_status_of_segments = NULL; + } } // Interactivity functions static int rails_math_ceil(int x, int y) -{ - int temp; - temp=(int)x/y; - - if (x%y) - return temp+1; - else return temp; -} - -inline unsigned int rails_get_segment(int x, int y) { - int xx; //segments are numerated just like pixels - int yy; //in computer graphics: left upper (=1), ... ,right upper, - //left bottom, ... , right bottom - xx=rails_math_ceil(x, img_w); - yy=rails_math_ceil(y, img_h); - - return (yy-1)*rails_segments_x+xx; - + int temp; + + temp = (int)x / y; + + if (x % y) + return temp + 1; + else + return temp; +} + +inline unsigned int rails_get_segment(int x, int y) +{ + int xx; //segments are numerated just like pixels + int yy; //in computer graphics: left upper (=1), ... ,right upper, + + //left bottom, ... , right bottom + xx = rails_math_ceil(x, img_w); + yy = rails_math_ceil(y, img_h); + + return (yy - 1) * rails_segments_x + xx; + } inline void rails_extract_coords_from_segment(unsigned int segment, Sint16 * x, Sint16 * 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; +{ //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; } -static void rails_flip(void * ptr, SDL_Surface * dest, SDL_Surface * src) +static void rails_flip(void *ptr, SDL_Surface * dest, SDL_Surface * src) { - magic_api * api = (magic_api *) ptr; + magic_api *api = (magic_api *) ptr; - Sint16 x, y; - - for (x=0; xw; x++) - for (y=0; yh; y++) - api->putpixel(dest, x, y, api->getpixel(src, x, src->h-y-1)); -} -static void rails_flip_flop(void * ptr, SDL_Surface * dest, SDL_Surface * src) -{ - magic_api * api = (magic_api *) ptr; Sint16 x, y; - for (x=0; xw; x++) - for (y=0; yh; y++) + + for (x = 0; x < dest->w; x++) + for (y = 0; y < dest->h; y++) + api->putpixel(dest, x, y, api->getpixel(src, x, src->h - y - 1)); +} + +static void rails_flip_flop(void *ptr, SDL_Surface * dest, SDL_Surface * src) +{ + magic_api *api = (magic_api *) ptr; + Sint16 x, y; + + for (x = 0; x < dest->w; x++) + for (y = 0; y < dest->h; y++) 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; - Sint16 x,y; - - if (direction) //rotate -90 degs - { - for (x = 0; xw; x++) - for (y =0; yh; y++) - api->putpixel(dest, x, y, api->getpixel(src, y, src->w-x-1)); - } - else //rotate +90 degs - { - for (x=0; xw; x++) - for (y=0; yh; y++) - api->putpixel(dest,x,y,api->getpixel(src,src->h-y-1,x)); - } - + magic_api *api = (magic_api *) ptr; + Sint16 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)); + } + 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)); + } + } 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); + rails_segment_modified_last = 0; + rails_drag(api, which, canvas, snapshot, x, y, x, y, update_rect); } 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 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; + 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 TOP = 0, BOTTOM = 0, LEFT = 0, RIGHT = 0; - if (segment == rails_segment_modified_last - 1) - from_right = 1; + //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 - rails_segments_x) - from_bottom = 1; + if (segment == rails_segment_modified_last - 1) + from_right = 1; - if (segment == rails_segment_modified_last + rails_segments_x) - from_top = 1; + if (segment == rails_segment_modified_last - rails_segments_x) + from_bottom = 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) + from_top = 1; - if (segment == rails_segment_modified_last + rails_segments_x - 1) - { - from_top_right = 1; - rails_segment_to_add = segment + 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_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; - } - } + if (segment == rails_segment_modified_last + rails_segments_x - 1) + { + from_top_right = 1; + rails_segment_to_add = segment + 1; + } - - - - - take_up=segment-rails_segments_x; - if (take_up<=0) val_up = SEG_NONE; - else val_up = rails_status_of_segments[take_up]; - - take_down=segment+rails_segments_x; - if (take_down>(signed)(rails_segments_x*rails_segments_y)) val_down = SEG_NONE; - else val_down = rails_status_of_segments[take_down]; - - if ((segment%rails_segments_x)==1) val_left=SEG_NONE; - else val_left = rails_status_of_segments[segment-1]; - - if ((segment%rails_segments_x)==0) val_right=SEG_NONE; - else val_right = rails_status_of_segments[segment+1]; - - if ( from_left || (val_left & SEG_RIGHT) || from_bottom_left) - { - LEFT = 1;} - if ( from_right || (val_right & SEG_LEFT) || from_top_right) - RIGHT=1; - if ( from_top || (val_up & SEG_BOTTOM) || from_top_left) - TOP=1; - if (from_bottom || (val_down & SEG_TOP) || from_bottom_right) - BOTTOM=1; - - - if (TOP && BOTTOM && LEFT && RIGHT) - return SEG_LEFT_RIGHT_TOP_BOTTOM; - if (LEFT && RIGHT && TOP) - return SEG_LEFT_RIGHT_TOP; - if (LEFT && RIGHT && BOTTOM) - return SEG_LEFT_RIGHT_BOTTOM; - if (TOP && BOTTOM && LEFT) - return SEG_LEFT_TOP_BOTTOM; - if (TOP && BOTTOM && RIGHT) - return SEG_RIGHT_TOP_BOTTOM; - if (LEFT &&RIGHT) - return SEG_LEFT_RIGHT; - if (TOP&&BOTTOM) - return SEG_TOP_BOTTOM; - if (LEFT&&TOP) - return SEG_LEFT_TOP; - if (LEFT&&BOTTOM) - return SEG_LEFT_BOTTOM; - if (RIGHT&&TOP) - return SEG_RIGHT_TOP; - if (RIGHT&&BOTTOM) - return SEG_RIGHT_BOTTOM; - if (LEFT|RIGHT) - return SEG_LEFT_RIGHT; - return SEG_TOP_BOTTOM; - - - -} - -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; - Uint8 image; - unsigned int use_temp; - - use_temp=0; - 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); - modification_rect.h=img_w; - modification_rect.w=img_h; - - image=rails_select_image(segment); //select the image to display - - if (rails_status_of_segments[segment] == image) - return; - - 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); - - 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 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: - 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_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: - SDL_BlitSurface(rails_corner, NULL, result, NULL); - 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_BOTTOM: - rails_flip_flop(api, temp, rails_corner); - use_temp=1; - break; - } - - if (use_temp) - SDL_BlitSurface(temp, NULL, result, NULL); - - SDL_FreeSurface(temp); - SDL_BlitSurface(result, NULL, canvas, &modification_rect); - SDL_FreeSurface(result); - 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) -{ - rails_segment_modified=rails_get_segment(x,y); - - - 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); - } - if (rails_segment_modified_last>0) - 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; + 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; + } } -if (rails_segment_modified>0) - rails_segment_modified_last=rails_segment_modified; + + + + + + take_up = segment - rails_segments_x; + if (take_up <= 0) + val_up = SEG_NONE; + else + val_up = rails_status_of_segments[take_up]; + + take_down = segment + rails_segments_x; + if (take_down > (signed)(rails_segments_x * rails_segments_y)) + val_down = SEG_NONE; + else + val_down = rails_status_of_segments[take_down]; + + if ((segment % rails_segments_x) == 1) + val_left = SEG_NONE; + else + val_left = rails_status_of_segments[segment - 1]; + + if ((segment % rails_segments_x) == 0) + val_right = SEG_NONE; + else + val_right = rails_status_of_segments[segment + 1]; + + if (from_left || (val_left & SEG_RIGHT) || from_bottom_left) + { + LEFT = 1; + } + if (from_right || (val_right & SEG_LEFT) || from_top_right) + RIGHT = 1; + if (from_top || (val_up & SEG_BOTTOM) || from_top_left) + TOP = 1; + if (from_bottom || (val_down & SEG_TOP) || from_bottom_right) + BOTTOM = 1; + + + if (TOP && BOTTOM && LEFT && RIGHT) + return SEG_LEFT_RIGHT_TOP_BOTTOM; + if (LEFT && RIGHT && TOP) + return SEG_LEFT_RIGHT_TOP; + if (LEFT && RIGHT && BOTTOM) + return SEG_LEFT_RIGHT_BOTTOM; + if (TOP && BOTTOM && LEFT) + return SEG_LEFT_TOP_BOTTOM; + if (TOP && BOTTOM && RIGHT) + return SEG_RIGHT_TOP_BOTTOM; + if (LEFT && RIGHT) + return SEG_LEFT_RIGHT; + if (TOP && BOTTOM) + return SEG_TOP_BOTTOM; + if (LEFT && TOP) + return SEG_LEFT_TOP; + if (LEFT && BOTTOM) + return SEG_LEFT_BOTTOM; + if (RIGHT && TOP) + return SEG_RIGHT_TOP; + if (RIGHT && BOTTOM) + return SEG_RIGHT_BOTTOM; + if (LEFT | RIGHT) + return SEG_LEFT_RIGHT; + return SEG_TOP_BOTTOM; + + + +} + +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; + Uint8 image; + unsigned int use_temp; + + use_temp = 0; + 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); + modification_rect.h = img_w; + modification_rect.w = img_h; + + image = rails_select_image(segment); //select the image to display + + if (rails_status_of_segments[segment] == image) + return; + + 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); + + 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 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: + 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_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: + SDL_BlitSurface(rails_corner, NULL, result, NULL); + 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_BOTTOM: + rails_flip_flop(api, temp, rails_corner); + use_temp = 1; + break; + } + + if (use_temp) + SDL_BlitSurface(temp, NULL, result, NULL); + + SDL_FreeSurface(temp); + SDL_BlitSurface(result, NULL, canvas, &modification_rect); + SDL_FreeSurface(result); + 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) +{ + rails_segment_modified = rails_get_segment(x, y); + + + 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); + } + if (rails_segment_modified_last > 0) + 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; + } + 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 ((xw)&&(yh)&&(oxw)&&(oyh)&&((signed)x>0)&&((signed)y>0)&&((signed)ox>0)&&((signed)oy>0)) + 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); + api->line((void *)api, which, canvas, snapshot, ox, oy, x, y, img_w / 2, rails_draw_wrapper); - 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); + start_x = min(ox, x); + end_x = max(ox, x); + start_y = min(oy, y); + end_y = max(oy, y); - 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; + 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); - update_rect->x=x; - update_rect->y=y; - update_rect->w=w; - update_rect->h=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; + } } diff --git a/magic/src/rain.c b/magic/src/rain.c index 5dcabf38b..d3764eaf9 100644 --- a/magic/src/rain.c +++ b/magic/src/rain.c @@ -48,47 +48,45 @@ void rain_click(magic_api *, int, int, SDL_Surface *, SDL_Surface *, int, int, S static const int rain_SIZE = 30; static const int rain_AMOUNT = 200; -enum { - TOOL_rain, - rain_NUM_TOOLS +enum +{ + TOOL_rain, + rain_NUM_TOOLS }; -static Mix_Chunk * rain_snd_effect[rain_NUM_TOOLS]; +static Mix_Chunk *rain_snd_effect[rain_NUM_TOOLS]; -const char * rain_snd_filenames[rain_NUM_TOOLS] = { +const char *rain_snd_filenames[rain_NUM_TOOLS] = { "rain.ogg", }; -const char * rain_icon_filenames[rain_NUM_TOOLS] = { + +const char *rain_icon_filenames[rain_NUM_TOOLS] = { "rain.png", }; -const char * rain_names[rain_NUM_TOOLS] = { + +const char *rain_names[rain_NUM_TOOLS] = { gettext_noop("Rain"), }; -const char * rain_descs[rain_NUM_TOOLS][2] = { + +const char *rain_descs[rain_NUM_TOOLS][2] = { {gettext_noop("Click to place a rain drop onto your picture."), - gettext_noop("Click to cover your picture with rain drops."),}, + gettext_noop("Click to cover your picture with rain drops."),}, }; Uint32 rain_api_version(void); int rain_init(magic_api * api); int rain_get_tool_count(magic_api * api); -SDL_Surface * rain_get_icon(magic_api * api, int which); -char * rain_get_name(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); +SDL_Surface *rain_get_icon(magic_api * api, int which); +char *rain_get_name(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); 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); 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); void rain_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 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); @@ -96,96 +94,121 @@ 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) { return(TP_MAGIC_API_VERSION); } +Uint32 rain_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} //Checks if a a pixel is inside a raindrop shape centered on the origin -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; - } +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 0; } -int rain_init(magic_api * api){ +int rain_init(magic_api * api) +{ int i; char fname[1024]; - //Load sounds - for (i = 0; i < rain_NUM_TOOLS; i++){ - snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, rain_snd_filenames[i]); - rain_snd_effect[i] = Mix_LoadWAV(fname); - } - return(1); + //Load sounds + for (i = 0; i < rain_NUM_TOOLS; i++) + { + snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, rain_snd_filenames[i]); + rain_snd_effect[i] = Mix_LoadWAV(fname); + } + + return (1); } -int rain_get_tool_count(magic_api * api ATTRIBUTE_UNUSED){ - return(rain_NUM_TOOLS); +int rain_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) +{ + return (rain_NUM_TOOLS); } // Load our icons: -SDL_Surface * rain_get_icon(magic_api * api, int which){ +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]); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } // Return our names, localized: -char * rain_get_name(magic_api * api ATTRIBUTE_UNUSED, int which){ - return(strdup(gettext_noop(rain_names[which]))); +char *rain_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) +{ + return (strdup(gettext_noop(rain_names[which]))); } // Return our descriptions, localized: -char * rain_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode){ - return(strdup(gettext_noop(rain_descs[which][mode-1]))); +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, SDL_Surface * last ATTRIBUTE_UNUSED, - int x, int y){ - magic_api * api = (magic_api *) ptr; +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; int xx, yy; - Uint8 r,g,b; + 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++){ - 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))); - } + for (yy = y - rain_SIZE / 2; yy < y + rain_SIZE / 2; yy++) + { + 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))); + } + } } - } } -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; + 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); - } + 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); + } } // 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){ - api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, rain_linecb); + 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); - if (ox > x) { int tmp = ox; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - rain_SIZE * 2; update_rect->y = oy - rain_SIZE * 2; @@ -195,55 +218,64 @@ 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); + if (mode == MODE_PAINT) + { + 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); - }else{ - - int i; - for(i=0; iw, rand() % canvas->h); + api->playsound(rain_snd_effect[which], (x * 255) / canvas->w, 255); } + else + { - update_rect->x = 0; - update_rect->y = 0; - update_rect->w = canvas->w; - update_rect->h = canvas->h; + int i; - api->playsound(rain_snd_effect[which], 128, 255); - } + 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); + } } // 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } // No setup happened: void rain_shutdown(magic_api * api ATTRIBUTE_UNUSED) { - //Clean up sounds - int i; - for(i=0; idata_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/rainbow.wav", api->data_directory); rainbow_snd = Mix_LoadWAV(fname); - return(1); + return (1); } // We have multiple tools: int rainbow_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(1); + return (1); } // Load our icons: -SDL_Surface * rainbow_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *rainbow_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/rainbow.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/rainbow.png", api->data_directory); - return(IMG_Load(fname)); + 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) { - return(strdup(gettext_noop("Rainbow"))); + return (strdup(gettext_noop("Rainbow"))); } // 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!"))); + return (strdup(gettext_noop("You can draw in rainbow colors!"))); } // 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) +static void rainbow_linecb(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { - magic_api * api = (magic_api *) ptr; + 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++) { - if (api->in_circle(xx - x, yy - y, 16)) - { - api->putpixel(canvas, xx, yy, rainbow_rgb); - } + for (xx = x - 16; xx < x + 16; xx++) + { + 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) { rainbow_color = (rainbow_color + 1) % NUM_RAINBOW_COLORS; rainbow_rgb = SDL_MapRGB(canvas->format, - rainbow_hexes[rainbow_color][0], - rainbow_hexes[rainbow_color][1], - rainbow_hexes[rainbow_color][2]); + rainbow_hexes[rainbow_color][0], + rainbow_hexes[rainbow_color][1], rainbow_hexes[rainbow_color][2]); - 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; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - 16; update_rect->y = oy - 16; @@ -193,17 +195,14 @@ 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -215,7 +214,8 @@ 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, Uint8 b ATTRIBUTE_UNUSED) +void rainbow_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, + Uint8 b ATTRIBUTE_UNUSED) { } @@ -225,15 +225,17 @@ int rainbow_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUT return 0; } -void rainbow_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas 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, SDL_Surface * canvas 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) { - return(MODE_PAINT); + return (MODE_PAINT); } diff --git a/magic/src/realrainbow.c b/magic/src/realrainbow.c index c5b712454..d4c9ece7e 100644 --- a/magic/src/realrainbow.c +++ b/magic/src/realrainbow.c @@ -20,48 +20,39 @@ FIXME: #include "tp_magic_api.h" -Mix_Chunk * realrainbow_snd; +Mix_Chunk *realrainbow_snd; int realrainbow_x1, realrainbow_y1, realrainbow_x2, realrainbow_y2; SDL_Rect realrainbow_rect; -SDL_Surface * realrainbow_colors[2]; +SDL_Surface *realrainbow_colors[2]; 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); + 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); -SDL_Surface * realrainbow_get_icon(magic_api * api, int which); -char * realrainbow_get_name(magic_api * api, int which); -char * realrainbow_get_description(magic_api * api, int which, int mode); +SDL_Surface *realrainbow_get_icon(magic_api * api, int which); +char *realrainbow_get_name(magic_api * api, int which); +char *realrainbow_get_description(magic_api * api, int which, int mode); int realrainbow_modes(magic_api * api, int which); 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); + 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); + 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); + 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) { - return(TP_MAGIC_API_VERSION); + return (TP_MAGIC_API_VERSION); } int realrainbow_init(magic_api * api) @@ -71,60 +62,60 @@ int realrainbow_init(magic_api * api) snprintf(fname, sizeof(fname), "%s/images/magic/realrainbow-colors.png", api->data_directory); realrainbow_colors[0] = IMG_Load(fname); if (realrainbow_colors[0] == NULL) - return(0); + return (0); snprintf(fname, sizeof(fname), "%s/images/magic/realrainbow-roygbiv-colors.png", api->data_directory); realrainbow_colors[1] = IMG_Load(fname); if (realrainbow_colors[1] == NULL) - return(0); + return (0); - snprintf(fname, sizeof(fname), "%s/sounds/magic/realrainbow.ogg", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/realrainbow.ogg", api->data_directory); realrainbow_snd = Mix_LoadWAV(fname); - return(1); + return (1); } int realrainbow_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(2); + return (2); } -SDL_Surface * realrainbow_get_icon(magic_api * api, int which) +SDL_Surface *realrainbow_get_icon(magic_api * api, int which) { char fname[1024]; if (which == 0) - snprintf(fname, sizeof(fname), "%s/images/magic/realrainbow.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/realrainbow.png", api->data_directory); else - snprintf(fname, sizeof(fname), "%s/images/magic/realrainbow-roygbiv.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/realrainbow-roygbiv.png", api->data_directory); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } -char * realrainbow_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) +char *realrainbow_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) { if (which == 0) - return(strdup(gettext_noop("Real Rainbow"))); + return (strdup(gettext_noop("Real Rainbow"))); else - return(strdup(gettext_noop("ROYGBIV Rainbow"))); + return (strdup(gettext_noop("ROYGBIV Rainbow"))); } -char * realrainbow_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *realrainbow_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, + int mode ATTRIBUTE_UNUSED) { - return(strdup(gettext_noop("Click where you want your rainbow to start, drag to where you want it to end, and then let go to draw a rainbow."))); + return (strdup + (gettext_noop + ("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) { - return(MODE_PAINT_WITH_PREVIEW); + return (MODE_PAINT_WITH_PREVIEW); } int realrainbow_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(0); + return (0); } void realrainbow_shutdown(magic_api * api ATTRIBUTE_UNUSED) @@ -137,14 +128,14 @@ 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, Uint8 b 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) + int x, int y, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { realrainbow_x1 = x; realrainbow_y1 = y; @@ -157,8 +148,7 @@ 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; @@ -168,7 +158,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)); @@ -194,9 +185,7 @@ 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; @@ -206,7 +195,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)); @@ -233,16 +223,19 @@ 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, SDL_Surface * canvas 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, SDL_Surface * canvas 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; @@ -250,71 +243,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); - - a1 = 0; - theta = -180; - } - else - { - if (y1 > y2) { - lowx = x1; - lowy = y1; - hix = x2; - hiy = y2; - } - else - { - lowx = x2; - lowy = y2; - hix = x1; - hiy = y1; - } + xc = x1 + (x2 - x1) / 2; + yc = y1; + r = abs(xc - x1); - 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; - } + theta = -180; + } + else + { + 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; + } 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) @@ -324,52 +317,51 @@ 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++) { - ox = (rr * cos(oa * M_PI / 180.0)) + xc; - oy = (rr * sin(oa * M_PI / 180.0)) + yc; + 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; - 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); + 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; + } } - 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; update_rect->w = r * 2 + thick * 2; } -static void realrainbow_linecb(void * ptr, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * last, - int x, int y) +static void realrainbow_linecb(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * last, int x, int y) { - magic_api * api = (magic_api *) ptr; + magic_api *api = (magic_api *) ptr; Uint8 origr, origg, origb; Uint8 newr, newg, newb; - SDL_GetRGB(api->getpixel(last, x, y), - last->format, &origr, &origg, &origb); + 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); @@ -377,4 +369,3 @@ static void realrainbow_linecb(void * ptr, int which ATTRIBUTE_UNUSED, api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, newr, newg, newb)); } - diff --git a/magic/src/ripples.c b/magic/src/ripples.c index 0d02faff9..07a65970b 100644 --- a/magic/src/ripples.c +++ b/magic/src/ripples.c @@ -37,28 +37,23 @@ /* Our globals: */ -static Mix_Chunk * ripples_snd; +static Mix_Chunk *ripples_snd; static int ripples_z, ripples_brite; Uint32 ripples_api_version(void); int ripples_init(magic_api * api); int ripples_get_tool_count(magic_api * api); -SDL_Surface * ripples_get_icon(magic_api * api, int which); -char * ripples_get_name(magic_api * api, int which); -char * ripples_get_description(magic_api * api, int which, int mode); +SDL_Surface *ripples_get_icon(magic_api * api, int which); +char *ripples_get_name(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); + 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); + 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 * 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); @@ -66,7 +61,10 @@ 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) { return(TP_MAGIC_API_VERSION); } +Uint32 ripples_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} #define deg_cos(x) cos((x) * M_PI / 180.0) #define deg_sin(x) sin((x) * M_PI / 180.0) @@ -76,54 +74,51 @@ int ripples_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/ripples.ogg", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/ripples.ogg", api->data_directory); ripples_snd = Mix_LoadWAV(fname); - return(1); + return (1); } // We have multiple tools: int ripples_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(1); + return (1); } // Load our icons: -SDL_Surface * ripples_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *ripples_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/ripples.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/ripples.png", api->data_directory); - return(IMG_Load(fname)); + 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 (strdup(gettext_noop("Ripples"))); } // 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) + 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) +static void ripples_linecb(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * last, int x, int y) { - magic_api * api = (magic_api *) ptr; + magic_api *api = (magic_api *) ptr; Uint8 r, g, b; Uint32 pix; @@ -139,8 +134,7 @@ 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; @@ -149,25 +143,25 @@ 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)) { - nx = fli * deg_cos(d) + x; - ny = -fli * deg_sin(d) + y; - - ripples_brite = (ripples_z * 20 * deg_sin(d + 45)) / ((fli / 4) + 1); + ripples_z = (10 * deg_sin(((50 * 50) / (fli + 4)) * 10)); - api->line((void *) api, which, canvas, last, ox, oy, nx, ny, 1, ripples_linecb); - - ox = nx; - oy = ny; + 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)) + { + nx = fli * deg_cos(d) + x; + ny = -fli * deg_sin(d) + y; + + 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; + } } - } update_rect->x = x - 100; update_rect->y = y - 100; @@ -179,8 +173,8 @@ 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -192,7 +186,8 @@ 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, Uint8 b ATTRIBUTE_UNUSED) +void ripples_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, + Uint8 b ATTRIBUTE_UNUSED) { } @@ -202,15 +197,17 @@ int ripples_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUT return 0; } -void ripples_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas 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, SDL_Surface * canvas 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) { - return(MODE_ONECLICK); + return (MODE_ONECLICK); } diff --git a/magic/src/rosette.c b/magic/src/rosette.c index 4f452022c..25b4e55bf 100644 --- a/magic/src/rosette.c +++ b/magic/src/rosette.c @@ -32,69 +32,64 @@ #include "tp_magic_api.h" #include "SDL_image.h" #include "SDL_mixer.h" -#include //for sin, cos, ... -#define ROSETTE_R 8 //circle's diameter +#include //for sin, cos, ... +#define ROSETTE_R 8 //circle's diameter static int xmid, ymid; struct rosette_rgb { - Uint8 r, g, b; + Uint8 r, g, b; }; struct rosette_rgb rosette_colors; -Mix_Chunk * rosette_snd; +Mix_Chunk *rosette_snd; -// Housekeeping functions +// Housekeeping functions Uint32 rosette_api_version(void); void rosette_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int rosette_init(magic_api * api); int rosette_get_tool_count(magic_api * api); -SDL_Surface * rosette_get_icon(magic_api * api, int which); -char * rosette_get_name(magic_api * api, int which); -char * rosette_get_description(magic_api * api, int which, int mode); +SDL_Surface *rosette_get_icon(magic_api * api, int which); +char *rosette_get_name(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); + 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) { - return(TP_MAGIC_API_VERSION); + return (TP_MAGIC_API_VERSION); } -void rosette_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) //get the colors from API and store it in structure +void rosette_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b) //get the colors from API and store it in structure { - rosette_colors.r=r; - rosette_colors.g=g; - rosette_colors.b=b; + rosette_colors.r = r; + rosette_colors.g = g; + rosette_colors.b = b; } int rosette_init(magic_api * api) { char fname[1024]; - - snprintf(fname, sizeof(fname), "%s/sounds/magic/picasso.ogg", api->data_directory); - rosette_snd = Mix_LoadWAV(fname); - return(1); + snprintf(fname, sizeof(fname), "%s/sounds/magic/picasso.ogg", api->data_directory); + rosette_snd = Mix_LoadWAV(fname); + + return (1); } int rosette_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) @@ -102,131 +97,146 @@ int rosette_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) return 2; } -SDL_Surface * rosette_get_icon(magic_api * api, int which) +SDL_Surface *rosette_get_icon(magic_api * api, int which) { char fname[1024]; - - if (!which) - snprintf(fname, sizeof(fname), "%s/images/magic/rosette.png", api->data_directory); - else snprintf(fname, sizeof(fname), "%s/images/magic/picasso.png", api->data_directory); - return(IMG_Load(fname)); + if (!which) + snprintf(fname, sizeof(fname), "%s/images/magic/rosette.png", api->data_directory); + else + snprintf(fname, sizeof(fname), "%s/images/magic/picasso.png", api->data_directory); + + return (IMG_Load(fname)); } -char * rosette_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) { if (!which) return strdup(gettext_noop("Rosette")); else return strdup(gettext_noop("Picasso"));} - -char * rosette_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode ATTRIBUTE_UNUSED) +char *rosette_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) { - if (!which) - return strdup(gettext_noop("Click and start drawing your rosette.")); //just k'scope with 3 bits? - else - return strdup(gettext_noop("You can draw just like Picasso!")); //what is this actually doing? + if (!which) + return strdup(gettext_noop("Rosette")); + else + return strdup(gettext_noop("Picasso")); } -int rosette_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { return 1; } +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? + else + 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) +{ + 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) + 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_shutdown(magic_api * api ATTRIBUTE_UNUSED) { Mix_FreeChunk(rosette_snd); } +void rosette_shutdown(magic_api * api ATTRIBUTE_UNUSED) +{ + Mix_FreeChunk(rosette_snd); +} // Interactivity functions -void rosette_circle(void * ptr, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * snapshot ATTRIBUTE_UNUSED, - int x, int y) +void rosette_circle(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * snapshot ATTRIBUTE_UNUSED, int x, int y) { - magic_api * api = (magic_api *) ptr; - - int xx, yy; - - 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)); + magic_api *api = (magic_api *) ptr; + + int xx, yy; + + 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)); } -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; - - double angle; - double xx, yy; //distance to the center of the image - int x1, y1, x2, y2; - - xx=(double)(xmid-x); - yy=(double)(y-ymid); - - if (which==0) - { - 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)); - - 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); - - 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; + magic_api *api = (magic_api *) ptr; - 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)); - - x2=(int)(xx*cos(2*angle)-yy*sin(2*angle)); - y2=(int)(xx*sin(2*angle)+yy*cos(2*angle)); - } + double angle; + double xx, yy; //distance to the center of the image + int x1, y1, x2, y2; - rosette_circle(api, which, canvas, snapshot, x, y); - rosette_circle(api, which, canvas, snapshot, (-1)*(x1-xmid), y1+ymid); - rosette_circle(api, which, canvas, snapshot, (-1)*(x2-xmid), y2+ymid); + xx = (double)(xmid - x); + yy = (double)(y - ymid); + + if (which == 0) + { + 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)); + + 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); + + 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 ((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)); + + 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); + rosette_circle(api, which, canvas, snapshot, (-1) * (x2 - xmid), y2 + ymid); } 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->playsound(rosette_snd, (x * 255) / canvas->w, 255); + 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; - update_rect->w=canvas->w; - update_rect->h=canvas->h; + update_rect->x = update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; } 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); + 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, SDL_Surface * canvas ATTRIBUTE_UNUSED) -{ - xmid=canvas->w/2; - ymid=canvas->h/2; +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, SDL_Surface * canvas 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) { - return(MODE_PAINT); + return (MODE_PAINT); } - diff --git a/magic/src/sharpen.c b/magic/src/sharpen.c index 40529c67d..5cb80844e 100644 --- a/magic/src/sharpen.c +++ b/magic/src/sharpen.c @@ -44,11 +44,12 @@ /* Our globals: */ -enum { - TOOL_TRACE, - TOOL_SHARPEN, - TOOL_SILHOUETTE, - sharpen_NUM_TOOLS +enum +{ + TOOL_TRACE, + TOOL_SHARPEN, + TOOL_SILHOUETTE, + sharpen_NUM_TOOLS }; static const int THRESHOLD = 50; @@ -57,54 +58,52 @@ static const int sharpen_RADIUS = 16; static const double SHARPEN = 0.5; -static Mix_Chunk * sharpen_snd_effect[sharpen_NUM_TOOLS]; +static Mix_Chunk *sharpen_snd_effect[sharpen_NUM_TOOLS]; -const char * sharpen_snd_filenames[sharpen_NUM_TOOLS] = { +const char *sharpen_snd_filenames[sharpen_NUM_TOOLS] = { "edges.ogg", "sharpen.ogg", "silhouette.ogg" }; -const char * sharpen_icon_filenames[sharpen_NUM_TOOLS] = { + +const char *sharpen_icon_filenames[sharpen_NUM_TOOLS] = { "edges.png", "sharpen.png", "silhouette.png" }; -const char * sharpen_names[sharpen_NUM_TOOLS] = { + +const char *sharpen_names[sharpen_NUM_TOOLS] = { gettext_noop("Edges"), gettext_noop("Sharpen"), gettext_noop("Silhouette") }; -const char * sharpen_descs[sharpen_NUM_TOOLS][2] = { + +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 to trace edges in your entire 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 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 to create a black and white silhouette of your entire picture.")}, }; Uint32 sharpen_api_version(void); int sharpen_init(magic_api * api); int sharpen_get_tool_count(magic_api * api); -SDL_Surface * sharpen_get_icon(magic_api * api, int which); -char * sharpen_get_name(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); +SDL_Surface *sharpen_get_icon(magic_api * api, int which); +char *sharpen_get_name(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); 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); 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); 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); @@ -113,149 +112,181 @@ 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) { return(TP_MAGIC_API_VERSION); } +Uint32 sharpen_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} // No setup required: -int sharpen_init(magic_api * api){ - +int sharpen_init(magic_api * api) +{ + int i; char fname[1024]; - for (i = 0; i < sharpen_NUM_TOOLS; i++){ - snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, sharpen_snd_filenames[i]); - sharpen_snd_effect[i] = Mix_LoadWAV(fname); - } + for (i = 0; i < sharpen_NUM_TOOLS; i++) + { + snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, sharpen_snd_filenames[i]); + sharpen_snd_effect[i] = Mix_LoadWAV(fname); + } - return(1); + return (1); } // We have multiple tools: int sharpen_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(sharpen_NUM_TOOLS); + return (sharpen_NUM_TOOLS); } // Load our icons: -SDL_Surface * sharpen_get_icon(magic_api * api, int which){ +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]); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } // Return our names, localized: -char * sharpen_get_name(magic_api * api ATTRIBUTE_UNUSED, int which){ - return(strdup(gettext_noop(sharpen_names[which]))); +char *sharpen_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) +{ + return (strdup(gettext_noop(sharpen_names[which]))); } // Return our descriptions, localized: -char * sharpen_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode){ - return(strdup(gettext_noop(sharpen_descs[which][mode-1]))); +char *sharpen_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode) +{ + return (strdup(gettext_noop(sharpen_descs[which][mode - 1]))); } //Calculates the grey scale value for a rgb pixel -static int sharpen_grey(Uint8 r1,Uint8 g1,Uint8 b1){ - return 0.3*r1+.59*g1+0.11*b1; +static int sharpen_grey(Uint8 r1, Uint8 g1, Uint8 b1) +{ + return 0.3 * r1 + .59 * g1 + 0.11 * 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; + magic_api *api = (magic_api *) ptr; - Uint8 r1, g1, b1; - int grey; - int i,j; - double sobel_1=0,sobel_2=0; - double temp; + Uint8 r1, g1, b1; + int grey; + int i, j; + double sobel_1 = 0, sobel_2 = 0; + double temp; - //Sobel weighting masks - const int sobel_weights_1[3][3] = { {1,2,1}, - {0,0,0}, - {-1,-2,-1}}; - const int sobel_weights_2[3][3] = { {-1,0,1}, - {-2,0,2}, - {-1,0,1}}; + //Sobel weighting masks + const int sobel_weights_1[3][3] = { {1, 2, 1}, + {0, 0, 0}, + {-1, -2, -1} + }; + const int sobel_weights_2[3][3] = { {-1, 0, 1}, + {-2, 0, 2}, + {-1, 0, 1} + }; - sobel_1=0; - sobel_2=0; - for (i=-1;i<2;i++){ - 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]; - } - } - - temp = sqrt(sobel_1*sobel_1 + sobel_2*sobel_2); - temp = (temp/1443)*255.0; + sobel_1 = 0; + sobel_2 = 0; + for (i = -1; i < 2; i++) + { + 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]; + } + } - // set image to white where edge value is below THRESHOLD - if (which == TOOL_TRACE){ - if (tempputpixel(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)); - } - //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))); - } + 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) + { + 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)); + } + //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))); + } } // 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; + int x, y; - for (y = 0; y < last->h; y++){ - for (x=0; x < last->w; x++){ - do_sharpen_pixel(ptr, which, canvas, last, x, y); - } - } + for (y = 0; y < last->h; y++) + { + for (x = 0; x < last->w; x++) + { + do_sharpen_pixel(ptr, 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; + 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++) { - if (api->in_circle(xx - x, yy - y, sharpen_RADIUS) && - !api->touched(xx, yy)) - { - do_sharpen_pixel(api, which, canvas, last, xx, yy); - } + 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); + } + } } - } } // 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; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - sharpen_RADIUS; update_rect->y = oy - sharpen_RADIUS; @@ -265,41 +296,46 @@ 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); - } + 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); + } } // 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } // No setup happened: void sharpen_shutdown(magic_api * api ATTRIBUTE_UNUSED) { - //Clean up sounds - int i; - for(i=0; idata_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/shift.ogg", api->data_directory); shift_snd = Mix_LoadWAV(fname); - return(1); + return (1); } // We have multiple tools: int shift_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(1); + return (1); } // 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), "%s/images/magic/shift.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/shift.png", api->data_directory); - return(IMG_Load(fname)); + 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 (strdup(gettext_noop("Shift"))); } // 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 */ + return; /* No-op */ 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, - SDL_Rect * update_rect, int crosshairs) + SDL_Surface * last, int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED, int x, int y, + SDL_Rect * update_rect, int crosshairs) { SDL_Rect dest; int dx, dy; @@ -159,123 +155,123 @@ static void shift_doit(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNU if (dy > 0) - { - if (dx > 0) { - /* Top Left */ + if (dx > 0) + { + /* Top Left */ - dest.x = dx - canvas->w; + 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); + } } - /* 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) { - /* Bottom Left */ + if (dx > 0) + { + /* Bottom Left */ - dest.x = dx - canvas->w; + 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); + } } - /* 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! */ @@ -290,19 +286,17 @@ 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; - + shift_doit(api, which, canvas, last, x, y, x, y, update_rect, 1); } // 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(); @@ -318,7 +312,7 @@ 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) { } @@ -328,15 +322,17 @@ int shift_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_ return 0; } -void shift_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas 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, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void shift_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } int shift_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_PAINT_WITH_PREVIEW); + return (MODE_PAINT_WITH_PREVIEW); } diff --git a/magic/src/smudge.c b/magic/src/smudge.c index 86627a91f..efa9506e2 100644 --- a/magic/src/smudge.c +++ b/magic/src/smudge.c @@ -37,25 +37,21 @@ /* Our globals: */ -static Mix_Chunk * smudge_snd; +static Mix_Chunk *smudge_snd; static Uint8 smudge_r, smudge_g, smudge_b; int smudge_init(magic_api * api); Uint32 smudge_api_version(void); -SDL_Surface * smudge_get_icon(magic_api * api, int which); -char * smudge_get_name(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); +SDL_Surface *smudge_get_icon(magic_api * api, int which); +char *smudge_get_name(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); 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); void smudge_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 smudge_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 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); @@ -69,121 +65,126 @@ int smudge_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/smudge.wav", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/smudge.wav", api->data_directory); smudge_snd = Mix_LoadWAV(fname); - return(1); + return (1); } -Uint32 smudge_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 smudge_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} // We have multiple tools: int smudge_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(2); + return (2); } // Load our icons: -SDL_Surface * smudge_get_icon(magic_api * api, int which) +SDL_Surface *smudge_get_icon(magic_api * api, int which) { char fname[1024]; if (which == 0) - snprintf(fname, sizeof(fname), "%s/images/magic/smudge.png", - api->data_directory); - else /* if (which == 1) */ - snprintf(fname, sizeof(fname), "%s/images/magic/wetpaint.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/smudge.png", api->data_directory); + else /* if (which == 1) */ + snprintf(fname, sizeof(fname), "%s/images/magic/wetpaint.png", api->data_directory); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } // Return our names, localized: -char * smudge_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) +char *smudge_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) { if (which == 0) - return(strdup(gettext_noop("Smudge"))); - else /* if (which == 1) */ - return(strdup(gettext_noop("Wet Paint"))); + return (strdup(gettext_noop("Smudge"))); + else /* if (which == 1) */ + return (strdup(gettext_noop("Wet Paint"))); } // 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."))); - 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 smudge the picture."))); + else /* if (which == 1) */ + 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; + magic_api *api = (magic_api *) ptr; static double state[32][32][3]; unsigned i = 32 * 32; - double rate = api->button_down() ? 0.5 : 0.0; + double rate = api->button_down()? 0.5 : 0.0; Uint8 r, g, b; 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; - // 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 + { + int iy = i >> 5; + int ix = i & 0x1f; - 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); + // 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 - // 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]))); - } + 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]))); + } } // 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; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - 16; update_rect->y = oy - 16; @@ -193,16 +194,15 @@ 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -226,19 +226,21 @@ int smudge_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE { if (which == 0) return 0; - else /* if (which == 1) */ + else /* if (which == 1) */ return 1; } -void smudge_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas 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, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void smudge_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } int smudge_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_PAINT); + return (MODE_PAINT); } diff --git a/magic/src/snow.c b/magic/src/snow.c index 6bdb2f268..6083b1108 100644 --- a/magic/src/snow.c +++ b/magic/src/snow.c @@ -45,30 +45,34 @@ static const int snow_AMOUNT = 400; static const int snow_RADIUS = 5; -static SDL_Surface * snow_flake1; -static SDL_Surface * snow_flake2; +static SDL_Surface *snow_flake1; +static SDL_Surface *snow_flake2; -enum { +enum +{ TOOL_SNOWBALL, TOOL_SNOWFLAKE, - snow_NUM_TOOLS + snow_NUM_TOOLS }; -static Mix_Chunk * snow_snd_effect[snow_NUM_TOOLS]; +static Mix_Chunk *snow_snd_effect[snow_NUM_TOOLS]; -const char * snow_snd_filenames[snow_NUM_TOOLS] = { +const char *snow_snd_filenames[snow_NUM_TOOLS] = { "snowball.ogg", "snowflake.ogg", }; -const char * snow_icon_filenames[snow_NUM_TOOLS] = { + +const char *snow_icon_filenames[snow_NUM_TOOLS] = { "snowball.png", "snowflake.png", }; -const char * snow_names[snow_NUM_TOOLS] = { + +const char *snow_names[snow_NUM_TOOLS] = { gettext_noop("Snow Ball"), gettext_noop("Snow Flake"), }; -const char * snow_descs[snow_NUM_TOOLS] = { + +const char *snow_descs[snow_NUM_TOOLS] = { gettext_noop("Click to add snow balls to your picture."), gettext_noop("Click to add snow flakes to your picture."), }; @@ -76,156 +80,187 @@ const char * snow_descs[snow_NUM_TOOLS] = { Uint32 snow_api_version(void); int snow_init(magic_api * api); int snow_get_tool_count(magic_api * api); -SDL_Surface * snow_get_icon(magic_api * api, int which); -char * snow_get_name(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); +SDL_Surface *snow_get_icon(magic_api * api, int which); +char *snow_get_name(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); 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); + 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 * 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); int snow_modes(magic_api * api, int which); -Uint32 snow_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 snow_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} //Load sounds -int snow_init(magic_api * api){ +int snow_init(magic_api * api) +{ int i; char fname[1024]; + srand(time(0)); snprintf(fname, sizeof(fname), "%s/images/magic/Snow_flake4.png", api->data_directory); snow_flake1 = IMG_Load(fname); - if (snow_flake1==NULL){ - return(0); - } + if (snow_flake1 == NULL) + { + return (0); + } snprintf(fname, sizeof(fname), "%s/images/magic/Snow_flake5.png", api->data_directory); snow_flake2 = IMG_Load(fname); - if (snow_flake2==NULL){ - return(0); - } + if (snow_flake2 == NULL) + { + return (0); + } - if (snow_flake2==NULL){printf("meh\n");} - for (i = 0; i < snow_NUM_TOOLS; i++){ - snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, snow_snd_filenames[i]); - snow_snd_effect[i] = Mix_LoadWAV(fname); - } - return(1); + if (snow_flake2 == NULL) + { + printf("meh\n"); + } + for (i = 0; i < snow_NUM_TOOLS; i++) + { + snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, snow_snd_filenames[i]); + snow_snd_effect[i] = Mix_LoadWAV(fname); + } + return (1); } -int snow_get_tool_count(magic_api * api ATTRIBUTE_UNUSED){ - return(snow_NUM_TOOLS); +int snow_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) +{ + return (snow_NUM_TOOLS); } // Load our icons: -SDL_Surface * snow_get_icon(magic_api * api, int which){ +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]); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } // Return our names, localized: -char * snow_get_name(magic_api * api ATTRIBUTE_UNUSED, int which){ - return(strdup(gettext_noop(snow_names[which]))); +char *snow_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) +{ + return (strdup(gettext_noop(snow_names[which]))); } // Return our descriptions, localized: -char * snow_get_description(magic_api * api ATTRIBUTE_UNUSED, int which){ - return(strdup(gettext_noop(snow_descs[which]))); +char *snow_get_description(magic_api * api ATTRIBUTE_UNUSED, int which) +{ + return (strdup(gettext_noop(snow_descs[which]))); } // Do the effect: -static void do_snow(void * ptr,SDL_Surface * canvas, SDL_Surface * last, int which, int snowAmount){ - magic_api * api = (magic_api *) ptr; +static void do_snow(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int which, int snowAmount) +{ + magic_api *api = (magic_api *) ptr; - int i,x,y,centre_x,centre_y; - Uint8 r,g,b; + int i, x, y, centre_x, centre_y; + Uint8 r, g, b; SDL_Rect dest; - for(i=0; iw; - 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++){ - 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)); + for (i = 0; i < snowAmount; i++) + { + 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++) + { + 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(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){ - // No-op + 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 } // 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){ + SDL_Surface * canvas, SDL_Surface * last, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect) +{ update_rect->x = 0; update_rect->y = 0; update_rect->w = canvas->w; update_rect->h = canvas->h; - - do_snow(api, canvas, last, which, snow_AMOUNT); + + do_snow(api, canvas, last, which, snow_AMOUNT); api->playsound(snow_snd_effect[which], 128, 255); } // 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } // No setup happened: void snow_shutdown(magic_api * api ATTRIBUTE_UNUSED) { - //Clean up sounds - int i; - for(i=0; idata_directory); break; - case STRING_TOOL_TRIANGLE: + case STRING_TOOL_TRIANGLE: snprintf(fname, sizeof(fname), "%s/images/magic/string_art_triangles.png", api->data_directory); break; case STRING_TOOL_ANGLE: @@ -111,18 +111,19 @@ SDL_Surface * string_get_icon(magic_api * api, int which) break; } - return(IMG_Load(fname)); + return (IMG_Load(fname)); } -char * string_get_name(__attribute__((unused)) magic_api * api, __attribute__((unused)) int which) +char *string_get_name( __attribute__ ((unused)) magic_api * api, __attribute__ ((unused)) + int which) { switch (which) { case STRING_TOOL_FULL_BY_OFFSET: return strdup(gettext_noop("String edges")); break; - case STRING_TOOL_TRIANGLE: + case STRING_TOOL_TRIANGLE: return strdup(gettext_noop("String corner")); break; default: @@ -130,13 +131,17 @@ char * string_get_name(__attribute__((unused)) magic_api * api, __attribute__((u } } -char * string_get_description(__attribute__((unused)) magic_api * api, int which, __attribute__((unused)) int mode) { +char *string_get_description( __attribute__ ((unused)) magic_api * api, int which, __attribute__ ((unused)) + int mode) +{ 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.")); + 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: + case STRING_TOOL_TRIANGLE: return strdup(gettext_noop("Click and drag to draw arrows made of string art.")); break; default: @@ -144,181 +149,192 @@ char * string_get_description(__attribute__((unused)) magic_api * api, int which } } -int string_requires_colors(__attribute__((unused)) magic_api * api, __attribute__((unused)) int which) { return 1;} +int string_requires_colors( __attribute__ ((unused)) magic_api * api, __attribute__ ((unused)) + int which) +{ + 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); - if (which==STRING_TOOL_ANGLE) + + if (which == STRING_TOOL_TRIANGLE) + 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 - { - 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); + 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); } } -int string_init(__attribute__((unused)) magic_api * api) +int string_init( __attribute__ ((unused)) magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/string.ogg", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/string.ogg", api->data_directory); string_snd[STRING_TOOL_FULL_BY_OFFSET] = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%s/sounds/magic/string2.ogg", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/string2.ogg", api->data_directory); string_snd[STRING_TOOL_TRIANGLE] = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%s/sounds/magic/string3.ogg", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/string3.ogg", api->data_directory); string_snd[STRING_TOOL_ANGLE] = Mix_LoadWAV(fname); - return(1); + return (1); } -void string_shutdown(__attribute__((unused)) magic_api * api) +void string_shutdown( __attribute__ ((unused)) magic_api * api) { int i = 0; - if (canvas_backup) + if (canvas_backup) SDL_FreeSurface(canvas_backup); while (i < STRING_NUMTOOLS) { if (string_snd[i] != NULL) - Mix_FreeChunk(string_snd[i]); - i ++; + Mix_FreeChunk(string_snd[i]); + i++; } } -void string_switchin(__attribute__((unused)) magic_api * api, __attribute__((unused)) int which, __attribute__((unused)) int mode, SDL_Surface * canvas, __attribute__((unused)) SDL_Surface * snapshot) +void string_switchin( __attribute__ ((unused)) magic_api * api, __attribute__ ((unused)) + int which, __attribute__ ((unused)) + int mode, SDL_Surface * canvas, __attribute__ ((unused)) SDL_Surface * snapshot) { - 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(__attribute__((unused)) magic_api * api, __attribute__((unused)) int which, __attribute__((unused)) int mode, __attribute__((unused)) SDL_Surface * canvas, __attribute__((unused)) SDL_Surface * snapshot) +void string_switchout( __attribute__ ((unused)) magic_api * api, __attribute__ ((unused)) + int which, __attribute__ ((unused)) + int mode, __attribute__ ((unused)) SDL_Surface * canvas, + __attribute__ ((unused)) SDL_Surface * snapshot) { SDL_FreeSurface(canvas_backup); - canvas_backup=NULL; + canvas_backup = NULL; } // Interactivity functions -void string_callback(void * ptr,__attribute__((unused)) int which, - SDL_Surface * canvas,__attribute__((unused)) SDL_Surface * snapshot, int x, int y) +void string_callback(void *ptr, __attribute__ ((unused)) + int which, SDL_Surface * canvas, __attribute__ ((unused)) SDL_Surface * snapshot, 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)); + magic_api *api = (magic_api *) ptr; + + api->putpixel(canvas, x, y, SDL_MapRGBA(canvas->format, string_r, string_g, string_b, 255)); } -void string_click(magic_api * api, int which,__attribute__((unused)) int mode, - SDL_Surface * canvas, SDL_Surface * snapshot, - int x, int y, SDL_Rect * update_rect) +void string_click(magic_api * api, int which, __attribute__ ((unused)) + int mode, SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect) { SDL_BlitSurface(canvas, NULL, canvas_backup, NULL); - string_ox=x; - string_oy=y; - string_vertex_distance=0; - string_vertex_done=0; + string_ox = x; + string_oy = y; + string_vertex_distance = 0; + string_vertex_done = 0; string_drag(api, which, canvas, snapshot, x, y, x, y, update_rect); } -static void string_draw_full_by_offset(void * ptr, __attribute__((unused)) int which, SDL_Surface * canvas, __attribute__((unused)) SDL_Surface * snapshot, - int x, int y, SDL_Rect * update_rect) +static void string_draw_full_by_offset(void *ptr, __attribute__ ((unused)) + int which, SDL_Surface * canvas, __attribute__ ((unused)) SDL_Surface * snapshot, + int x, int y, SDL_Rect * update_rect) { - magic_api * api = (magic_api *) ptr; + magic_api *api = (magic_api *) ptr; int u; int i; - int o; //offset + int o; //offset + // int n=y/5; - int ** a; + int **a; float step_w, step_h, aux; - int side=(int)(y/3); + int side = (int)(y / 3); - SDL_BlitSurface(snapshot,0,canvas,0); + SDL_BlitSurface(snapshot, 0, canvas, 0); - if (side<3) side=3; + 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); + a = malloc(sizeof(int *) * side * 4 * 2); - for (i=0;ih; - } - 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;iline((void *) api, which, canvas, snapshot,a[i][0],a[i][1],a[u][0],a[u][1],1, string_callback); + 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; + } } - for (i=0;iline((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); - 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; } -void scale_xcoord(int * xcoord) +void scale_xcoord(int *xcoord) { - if (*xcoord < string_ox) *xcoord=string_ox-(string_ox - *xcoord)*4; - else *xcoord=string_ox+(*xcoord-string_ox)*4; + if (*xcoord < string_ox) + *xcoord = string_ox - (string_ox - *xcoord) * 4; + else + *xcoord = string_ox + (*xcoord - string_ox) * 4; } -void scale_ycoord(int * ycoord) +void scale_ycoord(int *ycoord) { - if (*ycoord < string_oy) *ycoord=string_oy-(string_oy - *ycoord)*4; - else *ycoord=string_oy+(*ycoord-string_oy)*4; + if (*ycoord < string_oy) + *ycoord = string_oy - (string_oy - *ycoord) * 4; + else + *ycoord = string_oy + (*ycoord - string_oy) * 4; } -void scale_coords(int * ox, int * oy, int * x, int * y) +void scale_coords(int *ox, int *oy, int *x, int *y) { scale_xcoord(ox); scale_xcoord(x); @@ -326,161 +342,174 @@ void scale_coords(int * ox, int * oy, int * x, int * y) scale_ycoord(y); } -void compute_middle( int start_point, int end_point, int vertex, int * middle) +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; int w, h; - scale_coords(&ox, &oy,&x, &y); - w=max(string_ox,x)-min(string_ox,x); - h=max(string_oy,y)-min(string_oy,y); + + scale_coords(&ox, &oy, &x, &y); + w = max(string_ox, x) - min(string_ox, x); + h = max(string_oy, y) - min(string_oy, y); /* - This is enouth if you move the mouse slowly, but if you move the mouse fast, - there are rests of old previews left around. - update_rect->w=max(max(string_ox,x),max(ox,x))-min(min(string_ox,x),min(ox,x)) +80; - update_rect->h=max(max(string_oy,y),max(oy,y))-min(min(string_oy,y),min(oy,y)) +80; - update_rect->x=min(string_ox,x) -40; - update_rect->y=min(string_oy,y) -40; - */ - update_rect->x=0; - update_rect->y=0; - update_rect->w=canvas->w; - update_rect->h=canvas->h; - SDL_BlitSurface(canvas_backup,update_rect,canvas,update_rect); + This is enouth if you move the mouse slowly, but if you move the mouse fast, + there are rests of old previews left around. + update_rect->w=max(max(string_ox,x),max(ox,x))-min(min(string_ox,x),min(ox,x)) +80; + update_rect->h=max(max(string_oy,y),max(oy,y))-min(min(string_oy,y),min(oy,y)) +80; + update_rect->x=min(string_ox,x) -40; + update_rect->y=min(string_oy,y) -40; + */ + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + update_rect->h = canvas->h; + SDL_BlitSurface(canvas_backup, update_rect, canvas, update_rect); 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, __attribute__((unused)) int ox, __attribute__ ((unused)) int oy, int x, int y, - SDL_Rect * update_rect) -{ + SDL_Surface * canvas, SDL_Surface * snapshot, __attribute__ ((unused)) + int ox, __attribute__ ((unused)) + int oy, int x, int y, SDL_Rect * update_rect) +{ int w, h; int middle_x, middle_y; int dx, dy; - w=max(string_ox,x)-min(string_ox,x); - h=max(string_oy,y)-min(string_oy,y); - update_rect->x=0; - update_rect->y=0; - update_rect->w=canvas->w; - update_rect->h=canvas->h; - SDL_BlitSurface(canvas_backup,update_rect,canvas,update_rect); + w = max(string_ox, x) - min(string_ox, x); + h = max(string_oy, y) - min(string_oy, y); - api->line((void *) api, which, canvas, snapshot, string_ox,string_oy, string_vertex_x , string_vertex_y,1, string_callback); - if(!string_vertex_done) + update_rect->x = 0; + update_rect->y = 0; + update_rect->w = canvas->w; + 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); + 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; - } + 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); + 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, __attribute__((unused)) int which, - SDL_Surface * canvas, __attribute__((unused))SDL_Surface * snapshot, __attribute__ ((unused)) int ox, __attribute__ ((unused)) int oy, int x, int y, - SDL_Rect * update_rect) +void string_draw_angle(magic_api * api, __attribute__ ((unused)) + int which, + SDL_Surface * canvas, __attribute__ ((unused)) SDL_Surface * snapshot, __attribute__ ((unused)) + int ox, __attribute__ ((unused)) + int oy, 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; int i; - int max_wh , steps; - int max_separation=10; + int max_wh, steps; + int max_separation = 10; - update_rect->x=min(min(string_ox,string_vertex_x),x); - update_rect->y=min(min(string_oy,string_vertex_y),y); - update_rect->w=max(max(string_ox,string_vertex_x),x)-update_rect->x; - update_rect->h=max(max(string_oy,string_vertex_y),y)-update_rect->y; - SDL_BlitSurface(canvas_backup,update_rect,canvas,update_rect); + update_rect->x = min(min(string_ox, string_vertex_x), x); + update_rect->y = min(min(string_oy, string_vertex_y), y); + update_rect->w = max(max(string_ox, string_vertex_x), x) - update_rect->x; + update_rect->h = max(max(string_oy, string_vertex_y), y) - update_rect->y; + 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_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)); - 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; + 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; - for (i=0;i<=steps;i++) + 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, __attribute__((unused)) int which, - SDL_Surface * canvas, SDL_Surface * snapshot, int ox, int oy, int x, int y, - SDL_Rect * update_rect) +void string_draw_triangle(magic_api * api, __attribute__ ((unused)) + int which, + 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); - scale_coords(&ox, &oy,&x, &y); + SDL_BlitSurface(canvas_backup, 0, canvas, 0); + scale_coords(&ox, &oy, &x, &y); - string_vertex_x=string_ox; - string_vertex_y=string_oy; - string_ox=string_vertex_x; - string_oy=y; - y=string_vertex_y; + string_vertex_x = string_ox; + string_vertex_y = string_oy; + string_ox = string_vertex_x; + 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); - else if (which==STRING_TOOL_TRIANGLE) - 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); + if (which == STRING_TOOL_FULL_BY_OFFSET) + 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); + else if (which == STRING_TOOL_ANGLE) + string_draw_angle_preview((void *)api, which, canvas, snapshot, ox, oy, x, y, update_rect); } void string_set_vertex(int x, int y) { int dx, dy; - if (string_vertex_done) return; - dx=max(string_ox,x)-min(string_ox,x); - dy=max(string_oy,y)-min(string_oy,y); - if(dx+dy>string_vertex_distance) + + if (string_vertex_done) + return; + 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+30w)&&(yh)&&(oxw)&&(oyh)&&((signed)x>0)&&((signed)y>0)&&((signed)ox>0)&&((signed)oy>0)) + 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); + 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 496999b13..b17c66098 100644 --- a/magic/src/tint.c +++ b/magic/src/tint.c @@ -46,59 +46,58 @@ #define gettext_noop(String) String #endif -enum { - TOOL_TINT, - TOOL_THRESHOLD, - tint_NUM_TOOLS +enum +{ + TOOL_TINT, + TOOL_THRESHOLD, + tint_NUM_TOOLS }; static Uint8 tint_r, tint_g, tint_b; static int tint_min = INT_MAX; static int tint_max = 0; -static const int tint_RADIUS =16; +static const int tint_RADIUS = 16; -static Mix_Chunk * tint_snd_effect[tint_NUM_TOOLS]; +static Mix_Chunk *tint_snd_effect[tint_NUM_TOOLS]; -const char * tint_snd_filenames[tint_NUM_TOOLS] = { +const char *tint_snd_filenames[tint_NUM_TOOLS] = { "tint.wav", - "fold.ogg" /* FIXME */ + "fold.ogg" /* FIXME */ }; -const char * tint_icon_filenames[tint_NUM_TOOLS] = { + +const char *tint_icon_filenames[tint_NUM_TOOLS] = { "tint.png", "colornwhite.png" }; -const char * tint_names[tint_NUM_TOOLS] = { + +const char *tint_names[tint_NUM_TOOLS] = { gettext_noop("Tint"), gettext_noop("Color & White") // It does more than this but more intuitive than threshold. }; -const char * tint_descs[tint_NUM_TOOLS][2] = { + +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 to change the color of your entire 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 to turn your entire picture into white and a color you choose.")} }; int tint_init(magic_api * api); Uint32 tint_api_version(void); int tint_get_tool_count(magic_api * api); -SDL_Surface * tint_get_icon(magic_api * api, int which); -char * tint_get_name(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); +SDL_Surface *tint_get_icon(magic_api * api, int which); +char *tint_get_name(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); 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); 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); void tint_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 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); @@ -106,112 +105,145 @@ 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) { return(TP_MAGIC_API_VERSION); } +Uint32 tint_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} //Load sounds -int tint_init(magic_api * api){ +int tint_init(magic_api * api) +{ int i; char fname[1024]; - for (i = 0; i < tint_NUM_TOOLS; i++){ - snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, tint_snd_filenames[i]); - tint_snd_effect[i] = Mix_LoadWAV(fname); - } - return(1); + for (i = 0; i < tint_NUM_TOOLS; i++) + { + snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, tint_snd_filenames[i]); + tint_snd_effect[i] = Mix_LoadWAV(fname); + } + return (1); } -int tint_get_tool_count(magic_api * api ATTRIBUTE_UNUSED){ - return(tint_NUM_TOOLS); +int tint_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) +{ + return (tint_NUM_TOOLS); } // Load our icons: -SDL_Surface * tint_get_icon(magic_api * api, int which){ +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]); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } // Return our names, localized: -char * tint_get_name(magic_api * api ATTRIBUTE_UNUSED, int which){ - return(strdup(gettext_noop(tint_names[which]))); +char *tint_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) +{ + return (strdup(gettext_noop(tint_names[which]))); } // Return our descriptions, localized: -char * tint_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode){ - return(strdup(gettext_noop(tint_descs[which][mode-1]))); +char *tint_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode) +{ + return (strdup(gettext_noop(tint_descs[which][mode - 1]))); } //Calculates the grey scale value for a rgb pixel -static int tint_grey(Uint8 r1,Uint8 g1,Uint8 b1){ - return 0.3*r1+.59*g1+0.11*b1; +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){ - - magic_api * api = (magic_api *) ptr; - Uint8 r,g,b; - float h,s,v; - - SDL_GetRGB(api->getpixel(last, x, y), last->format, &r, &g, &b); +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; + Uint8 r, g, b; + float h, s, v; + + SDL_GetRGB(api->getpixel(last, x, y), last->format, &r, &g, &b); + { + + 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)); + } + else if (which == TOOL_THRESHOLD) + { + 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)); + } + } + } +} + +// Do the effect: +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++) { - - 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)); - } else if (which == TOOL_THRESHOLD){ - 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)); + for (x = 0; x < last->w; x++) + { + 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) +{ + 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++) + { + if (api->in_circle(xx - x, yy - y, tint_RADIUS) && !api->touched(xx, yy)) + { + do_tint_pixel(api, which, canvas, last, xx, yy); } } } } -// Do the effect: -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++){ - 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){ - 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++) - { - 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; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - tint_RADIUS; update_rect->y = oy - tint_RADIUS; @@ -221,37 +253,41 @@ 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); - } + 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); + } } // 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } // No setup happened: void tint_shutdown(magic_api * api ATTRIBUTE_UNUSED) { - //Clean up sounds - int i; - for(i=0; ih; y++){ - 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); - if (greyValuetint_max){ - tint_max=greyValue; - } - } - } - } + for (y = 0; y < canvas->h; y++) + { + 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); + + 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, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void tint_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } int tint_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_FULLSCREEN|MODE_PAINT); + return (MODE_FULLSCREEN | MODE_PAINT); } diff --git a/magic/src/toothpaste.c b/magic/src/toothpaste.c index a20eddf3f..11f452af5 100644 --- a/magic/src/toothpaste.c +++ b/magic/src/toothpaste.c @@ -46,25 +46,29 @@ double pi; static Uint8 toothpaste_r, toothpaste_g, toothpaste_b; static const int toothpaste_RADIUS = 10; -double* toothpaste_weights = NULL; +double *toothpaste_weights = NULL; -enum { - TOOL_toothpaste, - toothpaste_NUM_TOOLS +enum +{ + TOOL_toothpaste, + toothpaste_NUM_TOOLS }; -static Mix_Chunk * toothpaste_snd_effect[toothpaste_NUM_TOOLS]; +static Mix_Chunk *toothpaste_snd_effect[toothpaste_NUM_TOOLS]; -const char * toothpaste_snd_filenames[toothpaste_NUM_TOOLS] = { +const char *toothpaste_snd_filenames[toothpaste_NUM_TOOLS] = { "toothpaste.ogg", }; -const char * toothpaste_icon_filenames[toothpaste_NUM_TOOLS] = { + +const char *toothpaste_icon_filenames[toothpaste_NUM_TOOLS] = { "toothpaste.png", }; -const char * toothpaste_names[toothpaste_NUM_TOOLS] = { + +const char *toothpaste_names[toothpaste_NUM_TOOLS] = { gettext_noop("Toothpaste"), }; -const char * toothpaste_descs[toothpaste_NUM_TOOLS] = { + +const char *toothpaste_descs[toothpaste_NUM_TOOLS] = { gettext_noop("Click and drag to squirt toothpaste onto your picture."), }; @@ -72,20 +76,16 @@ const char * toothpaste_descs[toothpaste_NUM_TOOLS] = { Uint32 toothpaste_api_version(void); int toothpaste_init(magic_api * api); int toothpaste_get_tool_count(magic_api * api); -SDL_Surface * toothpaste_get_icon(magic_api * api, int which); -char * toothpaste_get_name(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); +SDL_Surface *toothpaste_get_icon(magic_api * api, int which); +char *toothpaste_get_name(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); 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); + 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_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); @@ -93,89 +93,112 @@ void toothpaste_switchin(magic_api * api, int which, int mode, SDL_Surface * can 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) { return(TP_MAGIC_API_VERSION); } +Uint32 toothpaste_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} -int toothpaste_init(magic_api * api){ +int toothpaste_init(magic_api * api) +{ int i; char fname[1024]; - int k,j; + int k, j; + //Load sounds - for (i = 0; i < toothpaste_NUM_TOOLS; i++){ - snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, toothpaste_snd_filenames[i]); - toothpaste_snd_effect[i] = Mix_LoadWAV(fname); - } - + for (i = 0; i < toothpaste_NUM_TOOLS; i++) + { + snprintf(fname, sizeof(fname), "%s/sounds/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)); - if (toothpaste_weights == NULL){ - return(0); - } - - for (k = - toothpaste_RADIUS; k < + toothpaste_RADIUS; k++){ - 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); - } + toothpaste_weights = (double *)malloc(toothpaste_RADIUS * 2 * toothpaste_RADIUS * 2 * sizeof(double)); + if (toothpaste_weights == NULL) + { + return (0); } - } - return(1); + for (k = -toothpaste_RADIUS; k < +toothpaste_RADIUS; k++) + { + 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); + } + } + } + + return (1); } -int toothpaste_get_tool_count(magic_api * api ATTRIBUTE_UNUSED){ - return(toothpaste_NUM_TOOLS); +int toothpaste_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) +{ + return (toothpaste_NUM_TOOLS); } // Load our icons: -SDL_Surface * toothpaste_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED){ +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]); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } // Return our names, localized: -char * toothpaste_get_name(magic_api * api ATTRIBUTE_UNUSED, int which){ - return(strdup(gettext_noop(toothpaste_names[which]))); +char *toothpaste_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) +{ + return (strdup(gettext_noop(toothpaste_names[which]))); } // Return our descriptions, localized: -char * toothpaste_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode ATTRIBUTE_UNUSED){ - return(strdup(gettext_noop(toothpaste_descs[which]))); +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, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y){ - magic_api * api = (magic_api *) ptr; +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; int xx, yy; + // double colr; - float h,s,v; - Uint8 r,g,b; + float h, s, v; + Uint8 r, g, b; - for (yy = y - toothpaste_RADIUS; yy < y + toothpaste_RADIUS; yy++){ - for (xx = x - toothpaste_RADIUS; xx < x + toothpaste_RADIUS; xx++){ - if (api->in_circle(xx - x, yy - y, toothpaste_RADIUS) && - !api->touched(xx, yy)){ + for (yy = y - toothpaste_RADIUS; yy < y + toothpaste_RADIUS; yy++) + { + for (xx = x - toothpaste_RADIUS; xx < x + toothpaste_RADIUS; xx++) + { + 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); @@ -188,33 +211,37 @@ 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } // No setup happened: void toothpaste_shutdown(magic_api * api ATTRIBUTE_UNUSED) { - //Clean up sounds - int i; - for(i=0; idata_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/tornado_release.ogg", api->data_directory); tornado_release_snd = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%s/images/magic/tornado_base.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/tornado_base.png", api->data_directory); tornado_base = IMG_Load(fname); - snprintf(fname, sizeof(fname), "%s/images/magic/tornado_cloud.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/tornado_cloud.png", api->data_directory); tornado_cloud = IMG_Load(fname); - return(1); + return (1); } // We have multiple tools: int tornado_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(1); + return (1); } // Load our icons: -SDL_Surface * tornado_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *tornado_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/tornado.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/tornado.png", api->data_directory); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } // Return our names, localized: -char * tornado_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +char *tornado_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(strdup(gettext_noop("Tornado"))); + return (strdup(gettext_noop("Tornado"))); } // Return our descriptions, localized: -char * tornado_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) +char *tornado_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) { - return(strdup(gettext_noop("Click and drag to draw a tornado funnel on your picture."))); + return (strdup(gettext_noop("Click and drag to draw a tornado funnel on your picture."))); } // Affect the canvas on drag: static void tornado_predrag(magic_api * api ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED, - SDL_Surface * last ATTRIBUTE_UNUSED, int ox, int oy, int x, int y) + SDL_Surface * last ATTRIBUTE_UNUSED, int ox, int oy, int x, int y) { if (x < tornado_min_x) tornado_min_x = x; @@ -184,23 +180,22 @@ static void tornado_predrag(magic_api * api ATTRIBUTE_UNUSED, SDL_Surface * canv // Determine which way to bend first: // if (tornado_side_decided == 0) - { - if (x < tornado_bottom_x - 10) { - tornado_side_first = SIDE_LEFT; - tornado_side_decided = 1; + if (x < tornado_bottom_x - 10) + { + tornado_side_first = SIDE_LEFT; + tornado_side_decided = 1; + } + else if (x > tornado_bottom_x + 10) + { + tornado_side_first = SIDE_RIGHT; + tornado_side_decided = 1; + } } - else if (x > tornado_bottom_x + 10) - { - tornado_side_first = SIDE_RIGHT; - tornado_side_decided = 1; - } - } } void tornado_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, - SDL_Surface * last, int ox, int oy, int x, int y, - SDL_Rect * update_rect) + SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect) { tornado_predrag(api, canvas, last, ox, oy, x, y); @@ -213,32 +208,30 @@ void tornado_drag(magic_api * api, int which ATTRIBUTE_UNUSED, SDL_Surface * can /* Draw the base and the stalk (low-quality) for now: */ tornado_drawstalk(api, canvas, last, - x, y, tornado_min_x, tornado_max_x, - tornado_bottom_x, tornado_bottom_y, !(api->button_down())); - + x, y, tornado_min_x, tornado_max_x, tornado_bottom_x, tornado_bottom_y, !(api->button_down())); + tornado_drawbase(api, canvas); update_rect->x = 0; update_rect->y = 0; - update_rect->w = canvas->w; + update_rect->w = canvas->w; update_rect->h = canvas->h; } // Affect the canvas on click: void tornado_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * last, - int x, int y, SDL_Rect * update_rect) + SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect) { tornado_min_x = x; tornado_max_x = x; tornado_bottom_x = x; - tornado_bottom_y = y;// - tornado_base->h; + tornado_bottom_y = y; // - tornado_base->h; tornado_side_decided = 0; tornado_side_first = SIDE_LEFT; tornado_drag(api, which, canvas, last, x, y, x, y, update_rect); - + /* api->playsound(tornado_click_snd, (x * 255) / canvas->w, 255); */ @@ -246,8 +239,7 @@ void tornado_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, // Affect the canvas on release: void tornado_release(magic_api * api, int which ATTRIBUTE_UNUSED, - SDL_Surface * canvas, SDL_Surface * last, - int x, int y, SDL_Rect * update_rect) + SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect) { /* Don't let tornado be too low compared to base: */ @@ -259,7 +251,7 @@ void tornado_release(magic_api * api, int which ATTRIBUTE_UNUSED, tornado_predrag(api, canvas, last, x, y, x, y); - + /* Erase any old stuff: */ SDL_BlitSurface(last, NULL, canvas, NULL); @@ -267,15 +259,13 @@ void tornado_release(magic_api * api, int which ATTRIBUTE_UNUSED, /* Draw high-quality stalk, and tornado: */ - tornado_drawstalk(api, canvas, last, - x, y, tornado_min_x, tornado_max_x, - tornado_bottom_x, tornado_bottom_y, 1); + tornado_drawstalk(api, canvas, last, x, y, tornado_min_x, tornado_max_x, tornado_bottom_x, tornado_bottom_y, 1); - tornado_drawtornado(api, canvas, x, y); + tornado_drawtornado(api, canvas, x, y); + + tornado_drawbase(api, canvas); - tornado_drawbase(api, canvas); - update_rect->x = 0; update_rect->y = 0; update_rect->w = canvas->w; @@ -287,10 +277,10 @@ void tornado_release(magic_api * api, int which ATTRIBUTE_UNUSED, static void tornado_drawtornado(magic_api * api, SDL_Surface * canvas, int x, int y) { - SDL_Surface * aux_surf; + SDL_Surface *aux_surf; SDL_Rect dest; - aux_surf = api->scale(tornado_cloud_colorized, top_w *2, top_w,0); + aux_surf = api->scale(tornado_cloud_colorized, top_w * 2, top_w, 0); dest.x = x - (aux_surf->w / 2); dest.y = y - (aux_surf->h / 2); @@ -311,21 +301,19 @@ static void tornado_drawbase(magic_api * api ATTRIBUTE_UNUSED, SDL_Surface * can static Uint32 tornado_mess(Uint32 pixel, SDL_Surface * canvas) { Uint8 r, g, b, a; - float f = (float)rand()*255/RAND_MAX; + float f = (float)rand() * 255 / RAND_MAX; + SDL_GetRGBA(pixel, canvas->format, &r, &g, &b, &a); return (SDL_MapRGBA(canvas->format, - (tornado_r + r + (Uint8)f * 2) / 4, - (tornado_g + g + (Uint8)f * 2) / 4, - (tornado_b + b + (Uint8)f * 2) / 4, - a)); + (tornado_r + r + (Uint8) f * 2) / 4, + (tornado_g + g + (Uint8) f * 2) / 4, (tornado_b + b + (Uint8) f * 2) / 4, a)); } static void tornado_drawstalk(magic_api * api, SDL_Surface * canvas, SDL_Surface * last, - int top_x, int top_y, int minx, int maxx, - int bottom_x, int bottom_y, int final) + int top_x, int top_y, int minx, int maxx, int bottom_x, int bottom_y, int final) { Point2D control_points[4]; - Point2D * curve; + Point2D *curve; int i, n_points; int left, right; SDL_Rect dest; @@ -340,22 +328,22 @@ static void tornado_drawstalk(magic_api * api, SDL_Surface * canvas, SDL_Surface control_points[0].y = top_y; if (tornado_side_first == SIDE_LEFT) - { - control_points[1].x = minx; - control_points[2].x = maxx; - } + { + control_points[1].x = minx; + control_points[2].x = maxx; + } else - { - control_points[1].x = maxx; - control_points[2].x = minx; - } + { + control_points[1].x = maxx; + control_points[2].x = minx; + } control_points[1].y = ((bottom_y - top_y) / 3) + top_y; control_points[2].y = (((bottom_y - top_y) / 3) * 2) + top_y; - + control_points[3].x = bottom_x; control_points[3].y = bottom_y; - + if (final == 0) n_points = 8; else @@ -370,58 +358,61 @@ static void tornado_drawstalk(magic_api * api, SDL_Surface * canvas, SDL_Surface top_w = max(32, n_points * n_points / 1000); /* Draw the curve: */ - + for (i = 0; i < n_points - 1; i++) - { - if (final == 0) { - dest.x = curve[i].x; - dest.y = curve[i].y; - dest.w = 2; - dest.h = 2; - SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, 0, 0, 0)); - } - else - { - ii = n_points - i; - /* min 10 pixels then ii^2 / 2000 or 4 * ii^2 / canvas->w, - don't let the top of funnel be wider than the half of canvas */ - if (n_points * n_points / 2000 > canvas->w / 4) - ww = 4 * n_points * n_points / canvas->w; + if (final == 0) + { + dest.x = curve[i].x; + dest.y = curve[i].y; + dest.w = 2; + dest.h = 2; + SDL_FillRect(canvas, &dest, SDL_MapRGB(canvas->format, 0, 0, 0)); + } else - ww = 2000; + { + ii = n_points - i; + /* min 10 pixels then ii^2 / 2000 or 4 * ii^2 / canvas->w, + don't let the top of funnel be wider than the half of canvas */ + if (n_points * n_points / 2000 > canvas->w / 4) + ww = 4 * n_points * n_points / canvas->w; + else + ww = 2000; - left = min(curve[i].x, curve[i + 1].x)-5-ii*ii/ww; - right = max(curve[i].x, curve[i + 1].x)+5+ii*ii/ww; + left = min(curve[i].x, curve[i + 1].x) - 5 - ii * ii / ww; + right = max(curve[i].x, curve[i + 1].x) + 5 + ii * ii / ww; - dest.x = left; - dest.y = curve[i].y; - dest.w = right - left + 1; - dest.h = 2; + dest.x = left; + dest.y = curve[i].y; + dest.w = right - left + 1; + dest.h = 2; + } + + rotation += 3; + /* The body of the tornado: 3x 1y rotation + some random particles */ + for (p = dest.x; p < dest.x + dest.w; p++) + { + if ((float)rand() * 100 / RAND_MAX > 10) + { + api->putpixel(canvas, p, dest.y, api->getpixel(last, dest.x + (p - dest.x + rotation) % dest.w, dest.y)); + } + else + { + api->putpixel(canvas, p, dest.y, + tornado_mess(api->getpixel(last, dest.x + (p - dest.x + rotation) % dest.w, dest.y), + canvas)); + } + } + + /* Some random particles flying around the tornado */ + for (p = dest.x - dest.w * 20 / 100; p < dest.x + dest.w + dest.w * 20 / 100; p++) + { + if ((float)rand() * 100 / RAND_MAX < 5 && ((p < dest.x) || (p > dest.w))) + api->putpixel(canvas, p, dest.y, + tornado_mess(api->getpixel(last, dest.x + (p - dest.x + rotation) % dest.w, dest.y), canvas)); + } } - rotation +=3; - /* The body of the tornado: 3x 1y rotation + some random particles */ - for (p = dest.x; p < dest.x + dest.w; p++) - { - if ((float)rand() * 100 / RAND_MAX > 10 ) - { - api->putpixel(canvas, p, dest.y, api->getpixel(last, dest.x + (p - dest.x + rotation) % dest.w , dest.y)); - } - else - { - api->putpixel(canvas, p, dest.y, tornado_mess(api->getpixel(last, dest.x + (p - dest.x + rotation) % dest.w , dest.y), canvas)); - } - } - - /* Some random particles flying around the tornado */ - for (p = dest.x - dest.w * 20 / 100; p < dest.x + dest.w + dest.w * 20 / 100; p++) - { - if ((float)rand() * 100 / RAND_MAX < 5 && ((p < dest.x) || (p > dest.w))) - api->putpixel(canvas, p, dest.y, tornado_mess(api->getpixel(last, dest.x + (p - dest.x + rotation) % dest.w , dest.y), canvas)); - } - } - free(curve); } @@ -474,32 +465,32 @@ cp[3] is the end point, or P3 in the above diagram t is the parameter value, 0 <= t <= 1 */ -static Point2D tornado_PointOnCubicBezier( Point2D* cp, float t ) +static Point2D tornado_PointOnCubicBezier(Point2D * cp, float t) { - float ax, bx, cx; - float ay, by, cy; - float tSquared, tCubed; - Point2D result; + float ax, bx, cx; + float ay, by, cy; + float tSquared, tCubed; + Point2D result; - /* calculate the polynomial coefficients */ + /* calculate the polynomial coefficients */ - cx = 3.0 * (cp[1].x - cp[0].x); - bx = 3.0 * (cp[2].x - cp[1].x) - cx; - ax = cp[3].x - cp[0].x - cx - bx; - - cy = 3.0 * (cp[1].y - cp[0].y); - by = 3.0 * (cp[2].y - cp[1].y) - cy; - ay = cp[3].y - cp[0].y - cy - by; - - /* calculate the curve point at parameter value t */ - - tSquared = t * t; - tCubed = tSquared * t; - - result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x; - result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y; - - return result; + cx = 3.0 * (cp[1].x - cp[0].x); + bx = 3.0 * (cp[2].x - cp[1].x) - cx; + ax = cp[3].x - cp[0].x - cx - bx; + + cy = 3.0 * (cp[1].y - cp[0].y); + by = 3.0 * (cp[2].y - cp[1].y) - cy; + ay = cp[3].y - cp[0].y - cy - by; + + /* calculate the curve point at parameter value t */ + + tSquared = t * t; + tCubed = tSquared * t; + + result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x; + result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y; + + return result; } /* @@ -509,15 +500,15 @@ static Point2D tornado_PointOnCubicBezier( Point2D* cp, float t ) */ -static void tornado_ComputeBezier( Point2D* cp, int numberOfPoints, Point2D* curve ) +static void tornado_ComputeBezier(Point2D * cp, int numberOfPoints, Point2D * curve) { - float dt; - int i; + float dt; + int i; - dt = 1.0 / ( numberOfPoints - 1 ); + dt = 1.0 / (numberOfPoints - 1); - for( i = 0; i < numberOfPoints; i++) - curve[i] = tornado_PointOnCubicBezier( cp, i*dt ); + for (i = 0; i < numberOfPoints; i++) + curve[i] = tornado_PointOnCubicBezier(cp, i * dt); } @@ -532,9 +523,7 @@ static void tornado_colorize_cloud(magic_api * api) /* Create a surface to render into: */ - amask = ~(tornado_cloud->format->Rmask | - tornado_cloud->format->Gmask | - tornado_cloud->format->Bmask); + amask = ~(tornado_cloud->format->Rmask | tornado_cloud->format->Gmask | tornado_cloud->format->Bmask); tornado_cloud_colorized = SDL_CreateRGBSurface(SDL_SWSURFACE, @@ -542,8 +531,7 @@ static void tornado_colorize_cloud(magic_api * api) tornado_cloud->h, tornado_cloud->format->BitsPerPixel, tornado_cloud->format->Rmask, - tornado_cloud->format->Gmask, - tornado_cloud->format->Bmask, amask); + tornado_cloud->format->Gmask, tornado_cloud->format->Bmask, amask); /* Render the new cloud: */ @@ -551,32 +539,33 @@ static void tornado_colorize_cloud(magic_api * api) SDL_LockSurface(tornado_cloud_colorized); for (y = 0; y < tornado_cloud->h; y++) - { - for (x = 0; x < tornado_cloud->w; x++) { - SDL_GetRGBA(api->getpixel(tornado_cloud, x, y), - tornado_cloud->format, &r, &g, &b, &a); + for (x = 0; x < tornado_cloud->w; x++) + { + SDL_GetRGBA(api->getpixel(tornado_cloud, x, y), tornado_cloud->format, &r, &g, &b, &a); - api->putpixel(tornado_cloud_colorized, x, y, - SDL_MapRGBA(tornado_cloud_colorized->format, - (tornado_r + r * 2) / 3, (tornado_g + g * 2) / 3, (tornado_b + b * 2) / 3, a)); + api->putpixel(tornado_cloud_colorized, x, y, + SDL_MapRGBA(tornado_cloud_colorized->format, + (tornado_r + r * 2) / 3, (tornado_g + g * 2) / 3, (tornado_b + b * 2) / 3, a)); + } } - } SDL_UnlockSurface(tornado_cloud_colorized); SDL_UnlockSurface(tornado_cloud); } -void tornado_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void tornado_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } -void tornado_switchout(magic_api * api, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void tornado_switchout(magic_api * api, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { api->stopsound(); } int tornado_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_PAINT_WITH_PREVIEW); + return (MODE_PAINT_WITH_PREVIEW); } diff --git a/magic/src/tv.c b/magic/src/tv.c index 4c08ce156..95e893d8b 100644 --- a/magic/src/tv.c +++ b/magic/src/tv.c @@ -32,42 +32,37 @@ int RADIUS = 16; -Mix_Chunk * tv_snd; +Mix_Chunk *tv_snd; Uint32 tv_api_version(void); void tv_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); int tv_init(magic_api * api); int tv_get_tool_count(magic_api * api); -SDL_Surface * tv_get_icon(magic_api * api, int which); -char * tv_get_name(magic_api * api, int which); -char * tv_get_description(magic_api * api, int which, int mode); +SDL_Surface *tv_get_icon(magic_api * api, int which); +char *tv_get_name(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); + 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 * 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); -// Housekeeping functions +// Housekeeping functions Uint32 tv_api_version(void) { - return(TP_MAGIC_API_VERSION); + return (TP_MAGIC_API_VERSION); } -void tv_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) //get the colors from API and store it in structure +void tv_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) //get the colors from API and store it in structure { } @@ -75,79 +70,83 @@ void tv_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Ui int tv_init(magic_api * api ATTRIBUTE_UNUSED) { char fname[1024]; - - snprintf(fname, sizeof(fname), "%s/sounds/magic/tv.ogg", api->data_directory); - tv_snd = Mix_LoadWAV(fname); - return(1); + snprintf(fname, sizeof(fname), "%s/sounds/magic/tv.ogg", api->data_directory); + tv_snd = Mix_LoadWAV(fname); + + return (1); } -int tv_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) +int tv_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { return 1; } -SDL_Surface * tv_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *tv_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/tv.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/tv.png", api->data_directory); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } -char * tv_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { return strdup(gettext_noop("TV")); } +char *tv_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) +{ + return strdup(gettext_noop("TV")); +} -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) { return 0; } +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) { } -void tv_shutdown(magic_api * api ATTRIBUTE_UNUSED) - { Mix_FreeChunk(tv_snd); } +void tv_shutdown(magic_api * api ATTRIBUTE_UNUSED) +{ + Mix_FreeChunk(tv_snd); +} // Interactivity functions -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) +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) { int i, j; - magic_api * api = (magic_api *) ptr_to_api; + magic_api *api = (magic_api *) ptr_to_api; for (i = x - RADIUS; i < x + RADIUS; i++) for (j = y - RADIUS; j < y + RADIUS; j++) - if ((j + 1) % 2 && - api->in_circle(i - x, j - y, RADIUS) && - ! api->touched(i, j)) - api->putpixel(canvas, i, j, SDL_MapRGB(canvas->format, 128, 128, 165)); + if ((j + 1) % 2 && api->in_circle(i - x, j - y, RADIUS) && !api->touched(i, j)) + api->putpixel(canvas, i, j, SDL_MapRGB(canvas->format, 128, 128, 165)); } -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) +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) { - magic_api * api = (magic_api *) ptr_to_api; - - api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, 128, 128, 165)); - //api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, 0, 0, 255)); + magic_api *api = (magic_api *) ptr_to_api; + + api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, 128, 128, 165)); + //api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, 0, 0, 255)); } 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); @@ -159,38 +158,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) { - int i; + int i; - for (i=0; ih; i+=2) - api->line(api, which, canvas, last, 0, i, canvas->w, i, 1, tv_do_tv); + for (i = 0; i < canvas->h; i += 2) + api->line(api, which, canvas, last, 0, i, canvas->w, i, 1, tv_do_tv); - 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, SDL_Surface * canvas 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, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void tv_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { - + } int tv_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_FULLSCREEN | MODE_PAINT); + return (MODE_FULLSCREEN | MODE_PAINT); } diff --git a/magic/src/waves.c b/magic/src/waves.c index f734b1156..a4fcfd40c 100644 --- a/magic/src/waves.c +++ b/magic/src/waves.c @@ -34,25 +34,22 @@ /* Our globals: */ -static Mix_Chunk * waves_snd[2]; +static Mix_Chunk *waves_snd[2]; /* Local function prototypes: */ Uint32 waves_api_version(void); int waves_init(magic_api * api); int waves_get_tool_count(magic_api * api); -SDL_Surface * waves_get_icon(magic_api * api, int which); -char * waves_get_name(magic_api * api, int which); -char * waves_get_description(magic_api * api, int which, int mode); +SDL_Surface *waves_get_icon(magic_api * api, int which); +char *waves_get_name(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); + 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); + 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 * 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); @@ -60,7 +57,10 @@ 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) { return(TP_MAGIC_API_VERSION); } +Uint32 waves_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} // No setup required: @@ -68,16 +68,14 @@ int waves_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/waves.ogg", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/waves.ogg", api->data_directory); waves_snd[0] = Mix_LoadWAV(fname); - snprintf(fname, sizeof(fname), "%s/sounds/magic/wavelet.ogg", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/wavelet.ogg", api->data_directory); waves_snd[1] = Mix_LoadWAV(fname); - return(1); + return (1); } // We have multiple tools: @@ -87,35 +85,43 @@ int waves_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) } // Load our icons: -SDL_Surface * waves_get_icon(magic_api * api, int which) +SDL_Surface *waves_get_icon(magic_api * api, int which) { char fname[1024]; - if (!which) snprintf(fname, sizeof(fname), "%s/images/magic/waves.png", api->data_directory); - else snprintf(fname, sizeof(fname), "%s/images/magic/wavelet.png", api->data_directory); + if (!which) + snprintf(fname, sizeof(fname), "%s/images/magic/waves.png", api->data_directory); + else + snprintf(fname, sizeof(fname), "%s/images/magic/wavelet.png", api->data_directory); - return(IMG_Load(fname)); + return (IMG_Load(fname)); } // Return our names, localized: -char * waves_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) +char *waves_get_name(magic_api * api ATTRIBUTE_UNUSED, int which) { - if (!which) return(strdup(gettext_noop("Waves"))); - else return strdup(gettext_noop("Wavelets")); + if (!which) + return (strdup(gettext_noop("Waves"))); + else + return strdup(gettext_noop("Wavelets")); } // 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(gettext_noop("Click to make the picture horizontally wavy. Click toward the top for shorter waves, the bottom for taller waves, the left for small waves, and the right for long waves."))); - return strdup(gettext_noop("Click to make the picture vertically wavy. Click toward the top for shorter waves, the bottom for taller waves, the left for small waves, and the right for long waves.")); -} + return (strdup + (gettext_noop + ("Click to make the picture horizontally wavy. Click toward the top for shorter waves, the bottom for taller waves, the left for small waves, and the right for long waves."))); + return + strdup(gettext_noop + ("Click to make the picture vertically wavy. Click toward the top for shorter waves, the bottom for taller waves, the left for small waves, and the right for long waves.")); +} 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) + 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; @@ -123,48 +129,48 @@ void waves_drag(magic_api * api ATTRIBUTE_UNUSED, int which, SDL_Surface * canva int height; 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++) - { - xx = sin((yy * height) * M_PI / 180.0) * width; - src.x = 0; - src.y = yy; - src.w = canvas->w; - src.h = 1; + if (which == 0) + { + //waves effect + width = ((x * 10) / canvas->w) + 10; + height = ((canvas->h - y) / 10) + 1; - dest.x = xx; - dest.y = yy; + for (yy = 0; yy < canvas->h; yy++) + { + xx = sin((yy * height) * M_PI / 180.0) * width; - SDL_BlitSurface(last, &src, canvas, &dest); - } - } + src.x = 0; + src.y = yy; + src.w = canvas->w; + src.h = 1; + + dest.x = xx; + dest.y = yy; + + 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++) - { - yy = sin((xx * height) * M_PI / 180.0) * width; + { + width = ((x * 10) / canvas->w) + 10; + height = ((canvas->h - y) / 10) + 1; - src.x = xx; - src.y = 0; - src.w = 1; - src.h = canvas->h; + for (xx = 0; xx < canvas->w; xx++) + { + yy = sin((xx * height) * M_PI / 180.0) * width; - dest.x = xx; - dest.y = yy; + src.x = xx; + src.y = 0; + src.w = 1; + src.h = canvas->h; - SDL_BlitSurface(last, &src, canvas, &dest); - } - } + dest.x = xx; + dest.y = yy; + + SDL_BlitSurface(last, &src, canvas, &dest); + } + } update_rect->x = 0; update_rect->y = 0; update_rect->w = canvas->w; @@ -173,8 +179,7 @@ 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); @@ -182,8 +187,8 @@ void waves_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, // 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) + SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, + int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED) { } @@ -197,7 +202,8 @@ 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, Uint8 b ATTRIBUTE_UNUSED) +void waves_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, + Uint8 b ATTRIBUTE_UNUSED) { } @@ -207,15 +213,17 @@ int waves_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_ return 0; } -void waves_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas 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, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void waves_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } int waves_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_PAINT); + return (MODE_PAINT); } diff --git a/magic/src/xor.c b/magic/src/xor.c index e5f3753da..8eff1df69 100644 --- a/magic/src/xor.c +++ b/magic/src/xor.c @@ -34,26 +34,23 @@ #include "SDL_image.h" #include "SDL_mixer.h" -static Mix_Chunk * xor_snd; +static Mix_Chunk *xor_snd; Uint32 xor_api_version(void); int xor_init(magic_api * api); int xor_get_tool_count(magic_api * api); -SDL_Surface * xor_get_icon(magic_api * api, int which); -char * xor_get_name(magic_api * api, int which); -char * xor_get_description(magic_api * api, int which, int mode); +SDL_Surface *xor_get_icon(magic_api * api, int which); +char *xor_get_name(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); @@ -62,123 +59,138 @@ 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); int xor_modes(magic_api * api, int which); -Uint32 xor_api_version(void) { return(TP_MAGIC_API_VERSION); } +Uint32 xor_api_version(void) +{ + return (TP_MAGIC_API_VERSION); +} int xor_init(magic_api * api) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/sounds/magic/xor.ogg", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/sounds/magic/xor.ogg", api->data_directory); xor_snd = Mix_LoadWAV(fname); - return(1); + return (1); } int xor_get_tool_count(magic_api * api ATTRIBUTE_UNUSED) { - return(1); + return (1); } -SDL_Surface * xor_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) +SDL_Surface *xor_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED) { char fname[1024]; - snprintf(fname, sizeof(fname), "%s/images/magic/xor.png", - api->data_directory); + snprintf(fname, sizeof(fname), "%s/images/magic/xor.png", api->data_directory); - return(IMG_Load(fname)); + 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"))); + return (strdup(gettext_noop("Xor Colors"))); } -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"))); + 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) +static void do_xor(void *ptr, int which ATTRIBUTE_UNUSED, + SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) { - magic_api * api = (magic_api *) ptr; - Uint8 r,g,b,xor; - float hue,sat,val; + magic_api *api = (magic_api *) ptr; + Uint8 r, g, b, xor; + float hue, sat, val; Uint32 pixel; - - 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; - else 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) -{ - magic_api * api = (magic_api *) ptr; - int xx,yy; - - for (yy = -16; yy < 16; yy++) - { - 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); - } - } - } -} - -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) -{ - api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, do_xor_circle); - if (ox > x) { int tmp = ox; ox = x; x = tmp; } - if (oy > y) { int tmp = oy; oy = y; y = tmp; } + 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; + else + 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) +{ + magic_api *api = (magic_api *) ptr; + int xx, yy; + + for (yy = -16; yy < 16; yy++) + { + 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); + } + } + } +} + +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) +{ + api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_xor_circle); + + if (ox > x) + { + int tmp = ox; + + ox = x; + x = tmp; + } + if (oy > y) + { + int tmp = oy; + + oy = y; + y = tmp; + } update_rect->x = ox - 16; update_rect->y = oy - 16; update_rect->w = (x + 16) - update_rect->x; update_rect->h = (y + 16) - update_rect->h; - api->playsound(xor_snd,(x * 255) / canvas->w, 255); + api->playsound(xor_snd, (x * 255) / canvas->w, 255); } 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) { } @@ -188,7 +200,8 @@ 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) { } @@ -197,15 +210,17 @@ int xor_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UN return 0; } -void xor_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas 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, SDL_Surface * canvas ATTRIBUTE_UNUSED) +void xor_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, + SDL_Surface * canvas ATTRIBUTE_UNUSED) { } int xor_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { - return(MODE_PAINT | MODE_FULLSCREEN); + return (MODE_PAINT | MODE_FULLSCREEN); } diff --git a/src/.indent.pro b/src/.indent.pro new file mode 100644 index 000000000..4b7868fd0 --- /dev/null +++ b/src/.indent.pro @@ -0,0 +1,13 @@ +--blank-lines-after-declarations +--blank-lines-after-procedures +--break-before-boolean-operator +--continue-at-parentheses +--declaration-indentation1 +--dont-break-function-decl-args +--dont-break-procedure-type +--line-length120 +--no-blank-lines-after-commas +--no-space-after-casts +--no-space-after-function-call-names +--no-space-after-parentheses +--no-tabs diff --git a/src/BeOS_print.h b/src/BeOS_print.h index ec4fbfcd9..ce9465951 100644 --- a/src/BeOS_print.h +++ b/src/BeOS_print.h @@ -1,8 +1,8 @@ -/* BeOS_print.h */ - -/* printing support for Tux Paint */ -/* Marcin 'Shard' Konicki */ - +/* BeOS_print.h */ + +/* printing support for Tux Paint */ +/* Marcin 'Shard' Konicki */ + /* 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 @@ -18,25 +18,25 @@ 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) -*/ - -/* Jan. 17, 2003 */ -/* $Id$ */ +*/ + +/* Jan. 17, 2003 */ +/* $Id$ */ #ifndef __BEOS_PRINT_H__ #define __BEOS_PRINT_H__ - + #include "SDL.h" - + #ifdef __cplusplus extern "C" { - -#endif /* __cplusplus */ + +#endif /* __cplusplus */ extern int SurfacePrint(SDL_Surface * surf); extern int IsPrinterAvailable(); #ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif /* __BEOS_PRINT__ */ +} +#endif /* __cplusplus */ + +#endif /* __BEOS_PRINT__ */ diff --git a/src/colors.h b/src/colors.h index 8ab266d40..3c6582f44 100644 --- a/src/colors.h +++ b/src/colors.h @@ -59,23 +59,23 @@ enum /* Hex codes: */ const int default_color_hexes[NUM_DEFAULT_COLORS][3] = { - {0, 0, 0}, /* Black */ - {128, 128, 128}, /* Dark grey */ - {192, 192, 192}, /* Light grey */ - {255, 255, 255}, /* White */ - {255, 0, 0}, /* Red */ - {255, 128, 0}, /* Orange */ - {255, 255, 0}, /* Yellow */ - {160, 228, 128}, /* Light green */ - {33, 148, 70}, /* Dark green */ - {138, 168, 205}, /* Sky blue */ - {50, 100, 255}, /* Blue */ - {186, 157, 255}, /* Lavender */ - {128, 0, 128}, /* Purple */ - {255, 165, 211}, /* Pink */ - {128, 80, 0}, /* Brown */ - {226, 189, 166}, /* Tan */ - {247, 228, 219} /* Beige */ + {0, 0, 0}, /* Black */ + {128, 128, 128}, /* Dark grey */ + {192, 192, 192}, /* Light grey */ + {255, 255, 255}, /* White */ + {255, 0, 0}, /* Red */ + {255, 128, 0}, /* Orange */ + {255, 255, 0}, /* Yellow */ + {160, 228, 128}, /* Light green */ + {33, 148, 70}, /* Dark green */ + {138, 168, 205}, /* Sky blue */ + {50, 100, 255}, /* Blue */ + {186, 157, 255}, /* Lavender */ + {128, 0, 128}, /* Purple */ + {255, 165, 211}, /* Pink */ + {128, 80, 0}, /* Brown */ + {226, 189, 166}, /* Tan */ + {247, 228, 219} /* Beige */ }; diff --git a/src/compiler.h b/src/compiler.h index 3e82ad34b..bbbf50534 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -84,7 +84,7 @@ // since gcc-2.5 #ifdef __GNUC__ #define NORETURN __attribute__((__noreturn__)) -#define FUNCTION __attribute__((__const__)) // no access to global mem, even via ptr, and no side effect +#define FUNCTION __attribute__((__const__)) // no access to global mem, even via ptr, and no side effect #else #define NORETURN #define FUNCTION diff --git a/src/cursor.c b/src/cursor.c index 8517196d3..949d2286a 100644 --- a/src/cursor.c +++ b/src/cursor.c @@ -84,8 +84,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 02dc13228..6f8098954 100644 --- a/src/dirwalk.c +++ b/src/dirwalk.c @@ -25,7 +25,7 @@ #include #include #ifndef __USE_GNU -#define __USE_GNU /* for strcasestr() */ +#define __USE_GNU /* for strcasestr() */ #endif #include #include @@ -67,188 +67,189 @@ ///////////////// directory walking callers and callbacks ////////////////// -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) +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; while (i--) - { - int loadable = 0; - const char *restrict const cp = strchr(files[i].str, '.'); - show_progress_bar_(screen, texture, renderer); - if (cp) { - // 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; - } - } - } - 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); + int loadable = 0; + const char *restrict const cp = strchr(files[i].str, '.'); + + show_progress_bar_(screen, texture, renderer); + if (cp) + { + // 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; + } + } + } + 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("Loading font: %s (locale is: %s)\n", fname, (locale ? locale : "NULL")); //EP + printf("Loading font: %s (locale is: %s)\n", fname, (locale ? locale : "NULL")); //EP #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); + 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("checking \"%s\" vs \"%s\"\n", fname_check, fname); //EP + printf("checking \"%s\" vs \"%s\"\n", 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); + 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 - int numfaces = TTF_FontFaces(font->ttf_font); - if (numfaces != 1) - printf("Found %d faces in %s, %s, %s\n", numfaces, files[i].str, - family, style); + int numfaces = TTF_FontFaces(font->ttf_font); - printf("success: tpf: 0x%x tpf->ttf_font: 0x%x\n", - (unsigned int)(intptr_t) font, (unsigned int)(intptr_t) font->ttf_font); //EP added (intptr_t) to avoid warning on x64 + if (numfaces != 1) + printf("Found %d faces in %s, %s, %s\n", numfaces, files[i].str, family, style); + + printf("success: tpf: 0x%x tpf->ttf_font: 0x%x\n", (unsigned int)(intptr_t) font, (unsigned int)(intptr_t) font->ttf_font); //EP added (intptr_t) to avoid warning on x64 #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. - - // distinct uppercase and lowercase (e.g., 'o' vs. 'O') - user_font_styles[num_font_styles]->score += charset_works(font, gettext("oO")); + // 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. - // common punctuation (e.g., '?', '!', '.', ',', etc.) - user_font_styles[num_font_styles]->score += charset_works(font, gettext(",.?!")); + // distinct uppercase and lowercase (e.g., 'o' vs. 'O') + user_font_styles[num_font_styles]->score += charset_works(font, gettext("oO")); - // uncommon 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(",.?!")); - // digits (e.g., '0', '1' and '7') - user_font_styles[num_font_styles]->score += charset_works(font, gettext("017")); + // uncommon punctuation (e.g., '@', '#', '*', etc.) + user_font_styles[num_font_styles]->score += charset_works(font, gettext("`\%_@$~#{<(^&*")); - // distinct circle-like characters (e.g., 'O' (capital oh) vs. '0' (zero)) - user_font_styles[num_font_styles]->score += charset_works(font, gettext("O0")); + // digits (e.g., '0', '1' and '7') + user_font_styles[num_font_styles]->score += charset_works(font, gettext("017")); - // 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 circle-like characters (e.g., 'O' (capital oh) vs. '0' (zero)) + user_font_styles[num_font_styles]->score += charset_works(font, gettext("O0")); - // translation spares -- design not finalized + // 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 #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 - { + files[i].str = NULL; // so free() won't crash -- we stole the memory + } + else + { #if 0 // THREADED_FONTS - printf("Font is too defective: %s, %s, %s\n", files[i].str, family, - style); + printf("Font is too defective: %s, %s, %s\n", files[i].str, family, style); #endif - } - TuxPaint_Font_CloseFont(font); - } - else - { + } + TuxPaint_Font_CloseFont(font); + } + else + { #if 0 // THREADED_FONTS - printf("could not open %s\n", files[i].str); + printf("could not open %s\n", files[i].str); #endif - } + } + } + free(files[i].str); } - free(files[i].str); - } free(files); } @@ -258,16 +259,14 @@ int compare_ftw_str(const void *v1, const void *v2) { const char *restrict const s1 = ((tp_ftw_str *) v1)->str; const char *restrict const s2 = ((tp_ftw_str *) v2)->str; - return -strcmp(s1, s2); /* FIXME: Should we try strcasecmp, to group things together despite uppercase/lowercase in filenames (e.g., Jigsaw* vs jigsaw* Starters)??? -bjk 2009.10.11 */ + + return -strcmp(s1, s2); /* FIXME: Should we try strcasecmp, to group things together despite uppercase/lowercase in filenames (e.g., Jigsaw* vs jigsaw* Starters)??? -bjk 2009.10.11 */ } -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; @@ -289,123 +288,124 @@ void tp_ftw(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer return; for (;;) - { - struct dirent *f = readdir(d); - int filetype = TP_FTW_UNKNOWN; + { + struct dirent *f = readdir(d); + int filetype = TP_FTW_UNKNOWN; - if (!f) - break; - if (f->d_name[0] == '.') - continue; + 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; - 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 (filetype == TP_FTW_UNKNOWN) + { + struct stat sbuf; - 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++; + 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++; + } } - 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) + 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 - { + 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 - 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--) { - 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); + 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); } - free(dir_names); - } } diff --git a/src/dirwalk.h b/src/dirwalk.h index 171ed9614..8dd092c2b 100644 --- a/src/dirwalk.h +++ b/src/dirwalk.h @@ -45,16 +45,14 @@ 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, const char *restrict const locale); +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/fonts.c b/src/fonts.c index 5d757bd2c..88d3f38b0 100644 --- a/src/fonts.c +++ b/src/fonts.c @@ -24,7 +24,7 @@ #include #ifndef __USE_GNU -#define __USE_GNU /* for strcasestr() */ +#define __USE_GNU /* for strcasestr() */ #endif #include #include @@ -85,7 +85,7 @@ static const char *problemFonts[] = { /* font types that cause TTF_OpenFont to crash */ static const char *problemFontExtensions[] = { - ".pfb", /* Ubuntu 14.04 (libsdl-ttf2.0-0 2.0.11-3, libfreetype6 2.5.2-1ubuntu2) -bjk 2014.04.19 */ + ".pfb", /* Ubuntu 14.04 (libsdl-ttf2.0-0 2.0.11-3, libfreetype6 2.5.2-1ubuntu2) -bjk 2014.04.19 */ NULL }; @@ -153,7 +153,7 @@ int num_font_styles; int num_font_styles_max; int text_state; -unsigned text_size = 4; // initial text size +unsigned text_size = 4; // initial text size int button_label_y_nudge; @@ -167,12 +167,12 @@ static TuxPaint_Font *try_alternate_font(int size) strcpy(prefix, lang_prefix); if ((p = strrchr(prefix, '_')) != NULL) - { - *p = 0; - snprintf(str, sizeof(str), "%sfonts/locale/%s.ttf", DATA_PREFIX, prefix); + { + *p = 0; + snprintf(str, sizeof(str), "%sfonts/locale/%s.ttf", DATA_PREFIX, prefix); - return TuxPaint_Font_OpenFont("", str, size); - } + return TuxPaint_Font_OpenFont("", str, size); + } return NULL; } #endif @@ -183,53 +183,54 @@ 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]; - snprintf(str, sizeof(str), "%sfonts/locale/%s.ttf", DATA_PREFIX, lang_prefix); + { + char str[128]; - ret = TuxPaint_Font_OpenFont("", str, size); + snprintf(str, sizeof(str), "%sfonts/locale/%s.ttf", DATA_PREFIX, lang_prefix); + + 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", macosx.fontsPath, lang_prefix); - ret = TuxPaint_Font_OpenFont("", str, size); - } + if (!ret) + { + snprintf(str, sizeof(str), "%s/%s.ttf", macosx.fontsPath, lang_prefix); + ret = TuxPaint_Font_OpenFont("", str, size); + } #endif #ifndef NO_SDLPANGO - if (!ret) - { - ret = try_alternate_font(size); if (!ret) - { - fprintf(stderr, - "\nWarning: Can't load font for this locale:\n" - "%s\n" - "The Simple DirectMedia Layer error that occurred was:\n" - "%s\n\n" "Will use default (American English) instead.\n\n", str, SDL_GetError()); - button_label_y_nudge = smash_i18n(); - } - } + { + ret = try_alternate_font(size); + if (!ret) + { + fprintf(stderr, + "\nWarning: Can't load font for this locale:\n" + "%s\n" + "The Simple DirectMedia Layer error that occurred was:\n" + "%s\n\n" "Will use default (American English) instead.\n\n", str, SDL_GetError()); + button_label_y_nudge = smash_i18n(); + } + } #endif - return ret ? ret : fallback; - } + return ret ? ret : fallback; + } } #endif @@ -238,20 +239,21 @@ void TuxPaint_Font_CloseFont(TuxPaint_Font * tpf) #ifdef DEBUG printf("TuxPaint_Font_CloseFont step 1 (%p)\n", tpf); //EP #endif - if (!tpf) return; //EP - + if (!tpf) + return; //EP + #ifndef NO_SDLPANGO #ifdef DEBUG printf("TuxPaint_Font_CloseFont step 2 (%p, %d)\n", tpf->pango_context, tpf->typ); //EP #endif if (tpf->typ == FONT_TYPE_PANGO) - if (tpf->pango_context) //EP - { -#ifndef __APPLE__ //EP added ifdef because SDLPango_FreeContext sometimes crashed with "pointer being freed was not allocated" + if (tpf->pango_context) //EP + { +#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; - } + } #endif #ifdef DEBUG @@ -259,16 +261,17 @@ void TuxPaint_Font_CloseFont(TuxPaint_Font * tpf) fflush(stdout); #endif if (tpf->typ == FONT_TYPE_TTF) - if (tpf->ttf_font) //EP - { - TTF_CloseFont(tpf->ttf_font); - tpf->ttf_font = NULL; - } + if (tpf->ttf_font) //EP + { + TTF_CloseFont(tpf->ttf_font); + tpf->ttf_font = NULL; + } - if (tpf->desc != NULL) { - free(tpf->desc); - tpf->desc = NULL; - } + if (tpf->desc != NULL) + { + free(tpf->desc); + tpf->desc = NULL; + } free(tpf); } @@ -277,6 +280,7 @@ TuxPaint_Font *TuxPaint_Font_OpenFont(const char *pangodesc, const char *ttffile { TuxPaint_Font *tpf = NULL; int i; + #ifndef NO_SDLPANGO char desc[1024]; #endif @@ -288,84 +292,84 @@ TuxPaint_Font *TuxPaint_Font_OpenFont(const char *pangodesc, const char *ttffile #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("Creating context: \"%s\"\n", 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("Failed to load %s\n", desc); + printf("Creating context: \"%s\"\n", desc); #endif - free(tpf); - tpf = NULL; + + tpf->pango_context = SDLPango_CreateContext_GivenFontDesc(desc); + if (tpf->pango_context == NULL) + { +#ifdef DEBUG + printf("Failed to load %s\n", desc); +#endif + free(tpf); + tpf = NULL; + } + else + tpf->height = size; /* FIXME: Is this accurate!? -bjk 2007.07.12 */ + +#ifdef DEBUG + printf("TuxPaint_Font_OpenFont() done\n"); + fflush(stdout); +#endif + + return (tpf); } - else - tpf->height = size; /* FIXME: Is this accurate!? -bjk 2007.07.12 */ - -#ifdef DEBUG - printf("TuxPaint_Font_OpenFont() done\n"); - fflush(stdout); -#endif - - return (tpf); - } #endif if (ttffilename != NULL && ttffilename[0] != '\0') - { -#ifdef DEBUG - printf("Opening TTF\n"); - fflush(stdout); -#endif - - i = 0; - while (problemFonts[i] != NULL) - { - if (!strcmp(ttffilename, problemFonts[i++])) - return NULL; /* bail on known problematic fonts that cause TTF_OpenFont to crash */ - } - - i = 0; - while (problemFontExtensions[i] != NULL) - { - if (strstr(ttffilename, problemFontExtensions[i++])) - return NULL; /* bail on known problematic font types that cause TTF_OpenFont to crash */ - } - - tpf = (TuxPaint_Font *) malloc(sizeof(TuxPaint_Font)); - tpf->typ = FONT_TYPE_TTF; - tpf->ttf_font = TTF_OpenFont(ttffilename, size); - tpf->desc = strdup(ttffilename); - -#ifdef DEBUG - printf("Loaded %s: %d->%d\n", ttffilename, tpf, tpf->ttf_font); - fflush(stdout); -#endif - - if (tpf->ttf_font == NULL) { #ifdef DEBUG - printf("Failed to load %s: %s\n", ttffilename, SDL_GetError()); + printf("Opening TTF\n"); + fflush(stdout); #endif - free(tpf); - tpf = NULL; - } - else - { + + i = 0; + while (problemFonts[i] != NULL) + { + if (!strcmp(ttffilename, problemFonts[i++])) + return NULL; /* bail on known problematic fonts that cause TTF_OpenFont to crash */ + } + + i = 0; + while (problemFontExtensions[i] != NULL) + { + if (strstr(ttffilename, problemFontExtensions[i++])) + return NULL; /* bail on known problematic font types that cause TTF_OpenFont to crash */ + } + + tpf = (TuxPaint_Font *) malloc(sizeof(TuxPaint_Font)); + tpf->typ = FONT_TYPE_TTF; + tpf->ttf_font = TTF_OpenFont(ttffilename, size); + tpf->desc = strdup(ttffilename); + #ifdef DEBUG - printf("Succeeded loading %s\n", ttffilename); + printf("Loaded %s: %d->%d\n", ttffilename, tpf, tpf->ttf_font); + fflush(stdout); #endif - tpf->height = TTF_FontHeight(tpf->ttf_font); + + if (tpf->ttf_font == NULL) + { +#ifdef DEBUG + printf("Failed to load %s: %s\n", ttffilename, SDL_GetError()); +#endif + free(tpf); + tpf = NULL; + } + else + { +#ifdef DEBUG + printf("Succeeded loading %s\n", ttffilename); +#endif + tpf->height = TTF_FontHeight(tpf->ttf_font); + } } - } #ifdef DEBUG printf("TuxPaint_Font_OpenFont() done\n"); @@ -381,30 +385,32 @@ TuxPaint_Font *TuxPaint_Font_OpenFont(const char *pangodesc, const char *ttffile void reliable_write(int fd, const void *buf, size_t count) { struct pollfd p; + do - { - ssize_t rc = write(fd, buf, count); - if (rc == -1) { - 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; - } + ssize_t rc = write(fd, buf, count); + + if (rc == -1) + { + 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; } - buf += rc; - count -= rc; - } while (count); } @@ -412,31 +418,33 @@ void reliable_write(int fd, const void *buf, size_t count) static void reliable_read(int fd, void *buf, size_t count) { struct pollfd p; + do - { - ssize_t rc = read(fd, buf, count); - if (rc == -1) { - 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; - } + ssize_t rc = read(fd, buf, count); + + if (rc == -1) + { + 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; } - if (rc == 0) - break; // EOF. Better not happen before the end! - buf += rc; - count -= rc; - } while (count); } @@ -456,14 +464,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(" %s\n", base[i]->style); + printf("\n::::::: %d styles in %s:\n", count, base[0]->family); + i = count; + while (i--) + { + printf(" %s\n", base[i]->style); + } } - } #endif i = count; @@ -476,90 +484,90 @@ 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--) { - 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; + 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 (zmin != zmid) - boldmap[boldmid] = 0; - else if (zmid != zmax) - boldmap[boldmid] = 1; - else if (boldmin == 0 && boldmid == 1) - { - boldmap[0] = -1; - boldmap[1] = 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]; - } + int claimed_bold = boldcounts[3]; + int claimed_norm = boldcounts[1]; - // classify lightface - if (claimed_norm < 2) - { - boldmap[0] = 0; - //claimed_norm += boldcounts[0]; + // 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; @@ -568,49 +576,50 @@ 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]; - if (b == -1) { + int b = boldmap[base[i]->boldness]; + + 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]) - { + 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]); + 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; + continue; + } + fi->filename[spot] = strdup(base[i]->filename); + fi->score += 2; } - 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]); + } } @@ -683,125 +692,126 @@ static void parse_font_style(style_info * si) while (*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 += 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 (*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); - if (!stumped) - { - stumped = 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; + } + + 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) si->boldness = 2; - else if (have_bold || have_black || have_heavy) // TODO: split these + else if (have_bold || have_black || have_heavy) // TODO: split these si->boldness = 3; else if (have_light) si->boldness = 0; @@ -809,7 +819,7 @@ 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"); } @@ -818,21 +828,23 @@ static void dupe_markdown_range(family_info ** base, int count) int bestscore = -999; int bestslot = 0; int i = count; + while (i--) - { - int score = base[i]->score; - if (score <= bestscore) - continue; - bestscore = score; - bestslot = i; - } + { + int score = base[i]->score; + + 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; + } } @@ -850,50 +862,52 @@ static void groupfonts(void) //fflush(stdout); for (;;) - { - int high = low; - 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; + int high = low; + + 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; + } + 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 + user_font_styles = NULL; // just to catch bugs qsort(user_font_families, num_font_families, sizeof user_font_families[0], compar_fontkiller); low = 0; for (;;) - { - int high = low; - 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; + int high = low; + + 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; + } + dupe_markdown_range(user_font_families + low, high - low); + low = high; } - 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); @@ -904,22 +918,22 @@ static void groupfonts(void) 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; - } + { + 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); @@ -927,7 +941,8 @@ 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); @@ -942,58 +957,60 @@ static void loadfonts(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer } -/* static */ int load_user_fonts(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, void *vp, const char *restrict const locale) +/* 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); 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; - result = find_directory(B_SYSTEM_FONTS_DIRECTORY, volume, false, buffer, sizeof(buffer)); - loadfonts(screen, texture, renderer, buffer); - result = find_directory(B_COMMON_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); + 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_COMMON_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); #elif defined(__APPLE__) - loadfonts(screen, texture, renderer, "/System/Library/Fonts"); - loadfonts(screen, texture, renderer, "/Library/Fonts"); - loadfonts(screen, texture, renderer, macosx.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, macosx.fontsPath); + loadfonts(screen, texture, renderer, "/usr/share/fonts"); + loadfonts(screen, texture, renderer, "/usr/X11R6/lib/X11/fonts"); #elif defined(__ANDROID__) - loadfonts(screen, texture, renderer, "/system/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); @@ -1010,13 +1027,14 @@ static void loadfonts(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer font_thread_done = 1; waiting_for_fonts = 0; // FIXME: need a memory barrier here - return 0; // useless, wanted by threading library + return 0; // useless, wanted by threading library } #ifdef FORKED_FONTS -void run_font_scanner(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, const char *restrict const locale) +void run_font_scanner(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer, + const char *restrict const locale) { int sv[2]; int size, i; @@ -1026,24 +1044,24 @@ 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__ - nice(42); // be nice, letting the main thread get the CPU + nice(42); // be nice, letting the main thread get the CPU #endif - sched_yield(); // try to let the parent run right now - prctl(PR_SET_PDEATHSIG, 9); // get killed if parent exits + sched_yield(); // try to let the parent run right now + prctl(PR_SET_PDEATHSIG, 9); // get killed if parent exits if (getppid() == 1) - _exit(99); // parent is already init, and won't be dying :-) + _exit(99); // parent is already init, and won't be dying :-) font_socket_fd = sv[1]; close(sv[0]); progress_bar_disabled = 1; reliable_read(font_socket_fd, &no_system_fonts, sizeof no_system_fonts); - sched_yield(); // try to let the parent run right now + sched_yield(); // try to let the parent run right now SDL_Init(SDL_INIT_NOPARACHUTE); TTF_Init(); load_user_fonts(screen, texture, renderer, NULL, locale); @@ -1051,29 +1069,30 @@ void run_font_scanner(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer size = 0; i = num_font_families; while (i--) - { - 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 - } - size += 2; // for 2-byte font count + { + 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 + } + size += 2; // for 2-byte font count buf = malloc(size); walk = buf; #ifdef DEBUG @@ -1083,64 +1102,64 @@ void run_font_scanner(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer *walk++ = num_font_families >> 8u; i = num_font_families; while (i--) - { - int len; - char *s; - - s = user_font_families[i]->directory; - if (s) { - len = strlen(s); - memcpy(walk, s, len); - walk += len; - } - *walk++ = '\0'; + int len; + char *s; - s = user_font_families[i]->family; - if (s) - { - len = strlen(s); - memcpy(walk, s, len); - walk += len; - } - *walk++ = '\0'; + s = user_font_families[i]->directory; + 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]->family; + 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[0]; + 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[1]; + 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; + 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'; } - *walk++ = '\0'; - } reliable_write(font_socket_fd, buf, size); exit(0); } @@ -1160,58 +1179,58 @@ 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) { - buf_size = buf_size * 5 / 4 + 256; + if (buf_size <= buf_fill * 9 / 8 + 128) + { + 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("read: fd=%d buf_fill=%u buf_size=%u rc=%ld\n", font_socket_fd, buf_fill, buf_size, (long int) rc); + printf("read: fd=%d buf_fill=%u buf_size=%u rc=%ld\n", font_socket_fd, buf_fill, buf_size, (long int)rc); #endif - if (rc == -1) - { - 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; - } + if (rc == -1) + { + 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; } - 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) - { - printf("child killed by signal %u\n", WTERMSIG(status)); - user_font_families = NULL; - num_font_families = 0; - font_thread_done = 1; + { + printf("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("Got %u bytes with %u families.\n", buf_fill, num_font_families); #endif @@ -1222,38 +1241,39 @@ void receive_some_font_info(SDL_Surface * screen, SDL_Texture * texture, SDL_Ren i = num_font_families; while (i--) - { - unsigned len; - user_font_families[i] = fip + i; + { + unsigned len; - len = strlen(walk); - user_font_families[i]->directory = len ? walk : NULL; - walk += len + 1; + user_font_families[i] = fip + i; - len = strlen(walk); - user_font_families[i]->family = 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]->filename[0] = 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[1] = 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[2] = 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[3] = len ? walk : NULL; - walk += len + 1; + len = strlen(walk); + user_font_families[i]->filename[2] = len ? walk : NULL; + walk += len + 1; - user_font_families[i]->handle = NULL; + len = strlen(walk); + user_font_families[i]->filename[3] = len ? walk : NULL; + walk += len + 1; - // score left uninitialized - } + user_font_families[i]->handle = NULL; + + // score left uninitialized + } font_thread_done = 1; } @@ -1276,47 +1296,47 @@ TuxPaint_Font *getfonthandle(int desire) #endif if (fi == NULL) - { + { #ifdef DEBUG - printf("getfonthandle(%d) points to a NULL family\n", desire); - fflush(stdout); + printf("getfonthandle(%d) points to a NULL family\n", desire); + fflush(stdout); #endif - return NULL; - } + return NULL; + } if (fi->filename != NULL) - { + { #ifdef DEBUG - printf("Setting 'name' to fi->filename[%d (0x%x)]\n", (int) text_state, (int) text_state); - fflush(stdout); + printf("Setting 'name' to fi->filename[%d (0x%x)]\n", (int)text_state, (int)text_state); + fflush(stdout); #endif - name = fi->filename[text_state]; + name = fi->filename[text_state]; #ifdef DEBUG - printf("Which is: %s\n", name); - fflush(stdout); + printf("Which is: %s\n", name); + fflush(stdout); #endif - } + } else - { -#ifdef DEBUG //EP fixed typo: replaced DBEUG with DEBUG - printf("fi->filename is NULL\n"); - fflush(stdout); + { +#ifdef DEBUG //EP fixed typo: replaced DBEUG with DEBUG + printf("fi->filename is NULL\n"); + fflush(stdout); #endif - name = NULL; - } + name = NULL; + } if (fi->handle) - { + { #ifdef DEBUG - printf("fi->handle was set (0x%x)\n", (int)(intptr_t) fi->handle); //EP added (intptr_t) to avoid warning on x64 + printf("fi->handle was set (0x%x)\n", (int)(intptr_t) fi->handle); //EP added (intptr_t) to avoid warning on x64 - fflush(stdout); + fflush(stdout); #endif - return fi->handle; - } + return fi->handle; + } #ifdef DEBUG printf("fi->handle was not yet set\n"); @@ -1348,28 +1368,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("name is still NULL\n"); - fflush(stdout); + printf("name is still NULL\n"); + fflush(stdout); #endif - return (NULL); - } + return (NULL); + } pathname = alloca(strlen(fi->directory) + 1 + strlen(name) + 1); sprintf(pathname, "%s/%s", fi->directory, name); @@ -1381,32 +1401,32 @@ TuxPaint_Font *getfonthandle(int desire) // if the font doesn't load, we die -- it did load OK before though if (fi->handle == NULL) - { -#ifdef DEBUG - printf("fi->handle is NULL!\n"); - fflush(stdout); -#endif - return (NULL); - } - - if (fi->handle->typ == FONT_TYPE_TTF) - { - if (fi->handle->ttf_font == NULL) { #ifdef DEBUG - printf("fi->handle->ttf_font is NULL!\n"); + printf("fi->handle is NULL!\n"); fflush(stdout); #endif return (NULL); } + if (fi->handle->typ == FONT_TYPE_TTF) + { + if (fi->handle->ttf_font == NULL) + { #ifdef DEBUG - printf("calling TTF_SetFontStyle(0x%x)\n", missing); - fflush(stdout); + printf("fi->handle->ttf_font is NULL!\n"); + fflush(stdout); +#endif + return (NULL); + } + +#ifdef DEBUG + printf("calling TTF_SetFontStyle(0x%x)\n", 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) @@ -1430,21 +1450,21 @@ static int do_surfcmp(const SDL_Surface * const *const v1, const SDL_Surface * c int i; if (s1 == s2) - { - printf("s1==s2?\n"); - return 0; - } + { + printf("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; - } + { + 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) @@ -1453,19 +1473,20 @@ 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) return memcmp(c1, c2, width * s1->h); 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; @@ -1475,6 +1496,7 @@ static int do_surfcmp(const SDL_Surface * const *const v1, const SDL_Surface * c static int surfcmp(const void *s1, const void *s2) { int diff = do_surfcmp(s1, s2); + if (!diff) was_bad_font = 1; return diff; @@ -1490,57 +1512,58 @@ int charset_works(TuxPaint_Font * font, const char *s) SDL_Surface **surfs = malloc(strlen(s) * sizeof surfs[0]); unsigned count = 0; int ret = 0; - while (*s) - { - 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'; + while (*s) + { + 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'; #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; + goto out; + } + surfs[count++] = tmp_surf; } - 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) - printf("TRYING TO RE-FREE!"); - else { - SDL_FreeSurface(surfs[count]); - surfs[count] = NULL; + if (surfs[count] == NULL) + printf("TRYING TO RE-FREE!"); + else + { + SDL_FreeSurface(surfs[count]); + surfs[count] = NULL; + } } - } free(surfs); return ret; } @@ -1548,13 +1571,13 @@ out: int TuxPaint_Font_FontHeight(TuxPaint_Font * tpf) { if (tpf == NULL) - { + { #ifdef DEBUG - printf("TuxPaint_Font_FontHeight() received NULL\n"); - fflush(stdout); + printf("TuxPaint_Font_FontHeight() received NULL\n"); + fflush(stdout); #endif - return (1); - } + return (1); + } return (tpf->height); } @@ -1562,22 +1585,22 @@ int TuxPaint_Font_FontHeight(TuxPaint_Font * tpf) const char *TuxPaint_Font_FontFaceFamilyName(TuxPaint_Font * tpf) { if (tpf == NULL) - { + { #ifdef DEBUG - printf("TuxPaint_Font_FontFaceFamilyName() received NULL\n"); - fflush(stdout); + printf("TuxPaint_Font_FontFaceFamilyName() received NULL\n"); + 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) @@ -1593,22 +1616,22 @@ const char *TuxPaint_Font_FontFaceFamilyName(TuxPaint_Font * tpf) const char *TuxPaint_Font_FontFaceStyleName(TuxPaint_Font * tpf) { if (tpf == NULL) - { + { #ifdef DEBUG - printf("TuxPaint_Font_FontFaceStyleName() received NULL\n"); - fflush(stdout); + printf("TuxPaint_Font_FontFaceStyleName() received NULL\n"); + 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) @@ -1650,6 +1673,7 @@ 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) { Uint8 pc[4][4]; diff --git a/src/fonts.h b/src/fonts.h index d3b18b8ba..821bcf74b 100644 --- a/src/fonts.h +++ b/src/fonts.h @@ -63,10 +63,10 @@ #define SDL_WaitThread(tid,rcp) do{(void)tid;(void)rcp;}while(0) #define SDL_Thread int #define SDL_mutex int -#define SDL_CreateMutex() 0 // creates in released state +#define SDL_CreateMutex() 0 // creates in released state #define SDL_DestroyMutex(lock) -#define SDL_mutexP(lock) // take lock -#define SDL_mutexV(lock) // release lock +#define SDL_mutexP(lock) // take lock +#define SDL_mutexV(lock) // release lock #endif #endif @@ -91,21 +91,23 @@ TTF_Font *BUGFIX_TTF_OpenFont206(const char *const file, int ptsize); /* Stuff that wraps either SDL_Pango or SDL_TTF for font rendering: */ -enum { +enum +{ #ifndef NO_SDLPANGO FONT_TYPE_PANGO, #endif FONT_TYPE_TTF }; -typedef struct TuxPaint_Font_s { +typedef struct TuxPaint_Font_s +{ #ifndef NO_SDLPANGO - SDLPango_Context * pango_context; + SDLPango_Context *pango_context; #endif int typ; - TTF_Font * ttf_font; + TTF_Font *ttf_font; int height; - char * desc; + char *desc; } TuxPaint_Font; int TuxPaint_Font_FontHeight(TuxPaint_Font * tpf); @@ -140,7 +142,7 @@ static int text_sizes[] = { #endif 12, 18, 24, 36, 48, 56, 64, 96, 112, 128, 160 -}; // point sizes +}; // point sizes #define MIN_TEXT_SIZE 0u #define MAX_TEXT_SIZE (sizeof text_sizes / sizeof text_sizes[0] - 1) @@ -150,12 +152,12 @@ typedef struct style_info { char *filename; char *directory; - char *family; // name like "FooCorp Thunderstruck" - char *style; // junk like "Oblique Demi-Bold" + char *family; // name like "FooCorp Thunderstruck" + char *style; // junk like "Oblique Demi-Bold" int italic; int boldness; int score; - int truetype; // Is it? (TrueType gets priority) + int truetype; // Is it? (TrueType gets priority) } style_info; // user's notion of a font @@ -183,15 +185,15 @@ 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); +const char *TuxPaint_Font_FontFaceFamilyName(TuxPaint_Font * tpf); +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, const char *restrict const locale); diff --git a/src/get_fname.c b/src/get_fname.c index c7f64bea9..8d113233b 100644 --- a/src/get_fname.c +++ b/src/get_fname.c @@ -33,7 +33,7 @@ /* DIR_SAVE: Where is the user's saved directory? This is where their saved files are stored and where the "current_id.txt" file is saved. - + Windows predefines "savedir" as: "C:\Documents and Settings\%USERNAME%\Application Data\TuxPaint" though it may get overridden with "--savedir" option @@ -60,10 +60,10 @@ const char *datadir; char *get_fname(const char *const name, int kind) { char f[512]; - const char *restrict const dir = (kind==DIR_SAVE) ? savedir : datadir; + const char *restrict const dir = (kind == DIR_SAVE) ? savedir : datadir; // Some mkdir()'s don't like trailing slashes - snprintf(f, sizeof(f), "%s%c%s", dir, (*name)?'/':'\0', name); + snprintf(f, sizeof(f), "%s%c%s", dir, (*name) ? '/' : '\0', name); return strdup(f); } diff --git a/src/get_fname.h b/src/get_fname.h index bf04e9369..d5e7ebdc4 100644 --- a/src/get_fname.h +++ b/src/get_fname.h @@ -28,7 +28,8 @@ extern const char *savedir; extern const char *datadir; -enum { +enum +{ DIR_SAVE, DIR_DATA }; diff --git a/src/i18n.c b/src/i18n.c index 39eea4c22..6507e0398 100644 --- a/src/i18n.c +++ b/src/i18n.c @@ -54,20 +54,21 @@ // setlocale cannot get current default locale of the device. // Here, JNI and Java Locale class can get the default locale // if user has not set locale and lang in the config file yet. -static char * android_locale () +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); - const char* locale = (*mEnv)->GetStringUTFChars(mEnv, mLocaleString, 0); - strcpy(android_locale_buf, locale); - (*mEnv)->ReleaseStringUTFChars(mEnv, mLocaleString, locale); - printf("android locale %s\n", android_locale_buf); - return android_locale_buf; + 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); + const char *locale = (*mEnv)->GetStringUTFChars(mEnv, mLocaleString, 0); + + strcpy(android_locale_buf, locale); + (*mEnv)->ReleaseStringUTFChars(mEnv, mLocaleString, locale); + printf("android locale %s\n", android_locale_buf); + return android_locale_buf; } #endif @@ -255,7 +256,7 @@ int need_right_to_left_word; const char *lang_prefix, *short_lang_prefix; int num_wished_langs = 0; -w_langs wished_langs[255]; +w_langs wished_langs[255]; static const language_to_locale_struct language_to_locale_array[] = { {"english", "C"}, @@ -390,10 +391,10 @@ static const language_to_locale_struct language_to_locale_array[] = { {"northern-sotho", "nso_ZA.UTF-8"}, {"sesotho-sa-leboa", "nso_ZA.UTF-8"}, {"occitan", "oc_FR.UTF-8"}, - {"odia", "or_IN.UTF-8"}, // Proper spelling - {"oriya", "or_IN.UTF-8"}, // Alternative - {"ojibwe", "oj_CA.UTF-8"}, // Proper spelling - {"ojibway", "oj_CA.UTF-8"}, // For compatibility + {"odia", "or_IN.UTF-8"}, // Proper spelling + {"oriya", "or_IN.UTF-8"}, // Alternative + {"ojibwe", "oj_CA.UTF-8"}, // Proper spelling + {"ojibway", "oj_CA.UTF-8"}, // For compatibility {"punjabi", "pa_IN.UTF-8"}, {"panjabi", "pa_IN.UTF-8"}, {"polish", "pl_PL.UTF-8"}, @@ -409,7 +410,7 @@ static const language_to_locale_struct language_to_locale_array[] = { {"sanskrit", "sa_IN.UTF-8"}, {"santali-devaganari", "sat_IN.UTF-8"}, {"santali-ol-chiki", "sat@olchiki"}, - {"serbian", "sr_RS.UTF-8"}, /* Was sr_YU, but that's not in /usr/share/i18n/SUPPORTED, and sr_RS is -bjk 2014.08.04 */ + {"serbian", "sr_RS.UTF-8"}, /* Was sr_YU, but that's not in /usr/share/i18n/SUPPORTED, and sr_RS is -bjk 2014.08.04 */ {"serbian-latin", "sr_RS@latin"}, {"shuswap", "shs_CA.UTF-8"}, {"secwepemctin", "shs_CA.UTF-8"}, @@ -426,7 +427,7 @@ static const language_to_locale_struct language_to_locale_array[] = { {"swahili", "sw_TZ.UTF-8"}, {"tagalog", "tl_PH.UTF-8"}, {"thai", "th_TH.UTF-8"}, - {"tibetan", "bo_CN.UTF-8"}, /* Based on: http://texinfo.org/pipermail/texinfo-pretest/2005-April/000334.html */ + {"tibetan", "bo_CN.UTF-8"}, /* Based on: http://texinfo.org/pipermail/texinfo-pretest/2005-April/000334.html */ {"turkish", "tr_TR.UTF-8"}, {"twi", "tw_GH.UTF-8"}, {"ukrainian", "uk_UA.UTF-8"}, @@ -449,13 +450,12 @@ static const language_to_locale_struct language_to_locale_array[] = { /* Show available languages: */ static void show_lang_usage(int exitcode) { - FILE * f = exitcode ? stderr : stdout; + FILE *f = exitcode ? stderr : stdout; const char *const prg = "tuxpaint"; - fprintf(f, - "\n" - "Usage: %s [--lang LANGUAGE]\n" "\n" "LANGUAGE may be one of:\n" -/* C */ " english american-english\n" -/* ach */" acholi acoli\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" /* ak */ " akan twi-fante\n" /* sq */ " albanian\n" @@ -464,7 +464,7 @@ static void show_lang_usage(int exitcode) /* an */ " aragones\n" /* hy */ " armenian hayeren\n" /* as */ " assamese\n" -/* ast */" asturian\n" +/* ast */ " asturian\n" /* en_AU */ " australian-english\n" /* az */ " azerbaijani\n" /* bm */ " bambara\n" @@ -498,7 +498,7 @@ static void show_lang_usage(int exitcode) /* ka */ " georgian\n" /* de */ " german deutsch\n" /* el */ " greek\n" -/* gos */" gronings zudelk-veenkelonioals\n" +/* gos */ " gronings zudelk-veenkelonioals\n" /* gu */ " gujarati\n" /* he */ " hebrew\n" /* hi */ " hindi\n" @@ -513,18 +513,18 @@ static void show_lang_usage(int exitcode) /* ks@devanagari */ " kashmiri-devanagari\n" /* ks */ " kashmiri-perso-arabic\n" /* km */ " khmer\n" -/* cgg */" kiga chiga\n" +/* cgg */ " kiga chiga\n" /* rw */ " kinyarwanda\n" -/* tlh */" klingon tlhIngan\n" -/* kok */" konkani-devaganari\n" -/* kok@roman */" konkani-roman\n" +/* tlh */ " klingon tlhIngan\n" +/* kok */ " konkani-devaganari\n" +/* kok@roman */ " konkani-roman\n" /* ko */ " korean\n" /* ku */ " kurdish\n" /* lv */ " latvian\n" /* lt */ " lithuanian lietuviu\n" /* lg */ " luganda\n" /* lb */ " luxembourgish letzebuergesch\n" -/* mai */" maithili\n" +/* mai */ " maithili\n" /* mk */ " macedonian\n" /* ms */ " malay\n" /* ml */ " malayalam\n" @@ -535,7 +535,7 @@ static void show_lang_usage(int exitcode) /* mn */ " mongolian\n" /* nr */ " ndebele\n" /* ne */ " nepali\n" -/* nso */" northern-sotho sesotho-sa-leboa\n" +/* nso */ " northern-sotho sesotho-sa-leboa\n" /* nn */ " norwegian nynorsk norsk\n" /* oc */ " occitan\n" /* or */ " odia oriya\n" @@ -547,8 +547,8 @@ static void show_lang_usage(int exitcode) /* ro */ " romanian\n" /* ru */ " russian russkiy\n" /* sa */ " sanskrit\n" -/* sat */" santali-devaganari\n" -/* sat@olchiki */" santali-ol-chiki\n" +/* sat */ " santali-devaganari\n" +/* sat@olchiki */ " santali-ol-chiki\n" /* gd */ " scottish scottish-gaelic ghaidhlig\n" /* sr */ " serbian\n" /* sr@latin */ " serbian-latin\n" @@ -573,15 +573,15 @@ static void show_lang_usage(int exitcode) /* tr */ " turkish\n" /* uk */ " ukrainian\n" /* ur */ " urdu\n" -/* ca@valencia */ " valencian valencia\n" +/* ca@valencia */ " valencian valencia\n" /* ve */ " venda\n" -/* vec */" venetian veneto\n" +/* vec */ " venetian veneto\n" /* vi */ " vietnamese\n" /* wa */ " walloon walon\n" /* wo */ " wolof\n" /* cy */ " welsh cymraeg\n" /* xh */ " xhosa\n" -/* zam */" zapotec miahuatlan-zapotec\n" +/* zam */ " zapotec miahuatlan-zapotec\n" /* zu */ " zulu\n" "\n", prg); exit(exitcode); @@ -593,13 +593,13 @@ static void show_lang_usage(int exitcode) static void show_locale_usage(FILE * f, const char *const prg) { fprintf(f, - "\n" - "Usage: %s [--locale LOCALE]\n" - "\n" - "LOCALE may be one of:\n" - " C (English American English)\n" + "\n" + "Usage: %s [--locale LOCALE]\n" + "\n" + "LOCALE may be one of:\n" + " C (English American English)\n" " ach_UG (Acholi Acoli)\n" - " af_ZA (Afrikaans)\n" + " af_ZA (Afrikaans)\n" " ak_GH (Akan Twi-Fante)\n" " am_ET (Amharic)\n" " ar_SA (Arabic)\n" @@ -615,44 +615,44 @@ static void show_locale_usage(FILE * f, const char *const prg) " brx_IN (Bodo)\n" " bs_BA (Bosnian)\n" " nb_NO (Bokmal)\n" - " pt_BR (Brazilian Brazilian Portuguese Portugues Brazilian)\n" - " br_FR (Breton Brezhoneg)\n" - " en_AU (Australian English)\n" - " en_CA (Canadian English)\n" - " en_GB (British British English)\n" - " en_ZA (South African English)\n" - " bg_BG (Bulgarian)\n" - " ca_ES (Catalan Catala)\n" + " pt_BR (Brazilian Brazilian Portuguese Portugues Brazilian)\n" + " br_FR (Breton Brezhoneg)\n" + " en_AU (Australian English)\n" + " en_CA (Canadian English)\n" + " en_GB (British British English)\n" + " en_ZA (South African English)\n" + " bg_BG (Bulgarian)\n" + " ca_ES (Catalan Catala)\n" " ca_ES@valencia (Valencian Valencia)n" - " zh_CN (Chinese-Simplified)\n" - " zh_TW (Chinese-Traditional)\n" - " cs_CZ (Czech Cesky)\n" - " da_DK (Danish Dansk)\n" - " doi_IN (Dogri)\n" - " nl_NL (Dutch)\n" + " zh_CN (Chinese-Simplified)\n" + " zh_TW (Chinese-Traditional)\n" + " cs_CZ (Czech Cesky)\n" + " da_DK (Danish Dansk)\n" + " doi_IN (Dogri)\n" + " nl_NL (Dutch)\n" " fa_IR (Persian)\n" " ff_SN (Fulah)\n" - " fi_FI (Finnish Suomi)\n" - " fo_FO (Faroese)\n" - " fr_FR (French Francais)\n" - " ga_IE (Irish Gaelic Gaidhlig)\n" - " gd_GB (Scottish Gaelic Ghaidhlig)\n" - " gl_ES (Galician Galego)\n" - " gos_NL (Gronings Zudelk Veenkelonioals)\n" - " gu_IN (Gujarati)\n" - " de_DE (German Deutsch)\n" + " fi_FI (Finnish Suomi)\n" + " fo_FO (Faroese)\n" + " fr_FR (French Francais)\n" + " ga_IE (Irish Gaelic Gaidhlig)\n" + " gd_GB (Scottish Gaelic Ghaidhlig)\n" + " gl_ES (Galician Galego)\n" + " gos_NL (Gronings Zudelk Veenkelonioals)\n" + " gu_IN (Gujarati)\n" + " de_DE (German Deutsch)\n" " eo (Esperanto)\n" - " et_EE (Estonian)\n" - " el_GR (Greek)\n" - " he_IL (Hebrew)\n" - " hi_IN (Hindi)\n" - " hr_HR (Croatian Hrvatski)\n" - " hu_HU (Hungarian Magyar)\n" + " et_EE (Estonian)\n" + " el_GR (Greek)\n" + " he_IL (Hebrew)\n" + " hi_IN (Hindi)\n" + " hr_HR (Croatian Hrvatski)\n" + " hu_HU (Hungarian Magyar)\n" " cgg_UG (Kiga Chiga)\n" - " tlh (Klingon tlhIngan)\n" - " is_IS (Icelandic Islenska)\n" - " id_ID (Indonesian Bahasa Indonesia)\n" - " it_IT (Italian Italiano)\n" + " tlh (Klingon tlhIngan)\n" + " is_IS (Icelandic Islenska)\n" + " id_ID (Indonesian Bahasa Indonesia)\n" + " it_IT (Italian Italiano)\n" " iu_CA (Inuktitut)\n" " ja_JP (Japanese)\n" " ka_GE (Georgian)\n" @@ -679,16 +679,16 @@ static void show_locale_usage(FILE * f, const char *const prg) " nr_ZA (Ndebele)\n" " ne_NP (Nepali)\n" " nso_ZA (Northern Sotho Sotho sa Leboa)\n" - " nn_NO (Norwegian Nynorsk Norsk)\n" - " oc_FR (Occitan)\n" - " oj_CA (Ojibway)\n" + " nn_NO (Norwegian Nynorsk Norsk)\n" + " oc_FR (Occitan)\n" + " oj_CA (Ojibway)\n" " or_IN (Odia Oriya)\n" - " pa_IN (Punjabi Panjabi)\n" - " pl_PL (Polish Polski)\n" - " pt_PT (Portuguese Portugues)\n" - " ro_RO (Romanian)\n" - " ru_RU (Russian Russkiy)\n" - " rw_RW (Kinyarwanda)\n" + " pa_IN (Punjabi Panjabi)\n" + " pl_PL (Polish Polski)\n" + " pt_PT (Portuguese Portugues)\n" + " ro_RO (Romanian)\n" + " ru_RU (Russian Russkiy)\n" + " rw_RW (Kinyarwanda)\n" " sa_IN (Sanskrit)\n" " sat_IN (Santali)\n" " sat@olchiki (Santali (Ol-Chiki))\n" @@ -696,10 +696,10 @@ static void show_locale_usage(FILE * f, const char *const prg) " sd_IN (Sindhii (Perso-Arabic))\n" " shs_CA (Shuswap Secwepemctin)\n" " si_LK (Sinhala)\n" - " sk_SK (Slovak)\n" - " sl_SI (Slovenian)\n" + " sk_SK (Slovak)\n" + " sl_SI (Slovenian)\n" " son (Songhay)\n" - " sq_AL (Albanian)\n" + " sq_AL (Albanian)\n" " sr_YU (Serbian (cyrillic))\n" " sr_RS@latin (Serbian (latin))\n" " es_ES (Spanish Espanol)\n" @@ -707,12 +707,12 @@ static void show_locale_usage(FILE * f, const char *const prg) " es_MX (Mexican Mexican Spanish Espanol Mejicano)\n" " sw_TZ (Swahili)\n" " sv_SE (Swedish Svenska)\n" - " ta_IN (Tamil)\n" + " ta_IN (Tamil)\n" " te_IN (Telugu)\n" - " tl_PH (Tagalog)\n" - " bo_CN (Tibetan)\n" - " th_TH (Thai)\n" - " tr_TR (Turkish)\n" + " tl_PH (Tagalog)\n" + " bo_CN (Tibetan)\n" + " th_TH (Thai)\n" + " tr_TR (Turkish)\n" " tw_GH (Twi)\n" " uk_UA (Ukrainian)\n" " ur_IN (Urdu)\n" @@ -722,10 +722,7 @@ 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); } @@ -740,10 +737,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; } @@ -754,17 +751,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"}; - 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]); - } + 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" }; + 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]); + } fprintf(stderr, "Failed to find a locale with iswprint() working!\n"); #endif } @@ -773,11 +772,12 @@ 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]; + 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); @@ -797,6 +797,7 @@ static void set_langint_from_locale_string(const char *restrict loc) size_t len_baseloc; int found = 0; int i; + // printf("langint %i\n", langint); if (!loc) @@ -808,75 +809,73 @@ static void set_langint_from_locale_string(const char *restrict loc) if it still fails, try to find language and country code without the variant, finally scan just the lang part. as a last resource reverse the scanning - */ + */ - if(dot) + if (dot) *dot = '\0'; if (cntrycode) - { - ccodeaux = strdup(cntrycode); - *cntrycode = '\0'; - } + { + ccodeaux = strdup(cntrycode); + *cntrycode = '\0'; + } if (at) - { - 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); + 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); 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; - } - } + { + // 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); - 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) + if (cntrycode) { /* ll_CC */ //if (found == 0) printf("ll_CC check\n"); - snprintf(straux, 255, "%s%s",baseloc, ccodeaux); + 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 */ @@ -885,15 +884,14 @@ 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]))) { - langint = i; - found = 1; + // 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; + } } - } /* Last resource, we should never arrive here, this check depends on the right order in lang_prefixes[] @@ -903,21 +901,21 @@ 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]))) { - langint = i; - found = 1; + // Case-insensitive (both "pt_BR" and "pt_br" work, etc.) + if (!strncasecmp(loc, lang_prefixes[i], strlen(lang_prefixes[i]))) + { + langint = i; + found = 1; + } } - } // printf("langint %i, lang_ext %s\n", langint, lang_prefixes[langint]); free(baseloc); if (ataux) free(ataux); if (ccodeaux) - free(ccodeaux); + free(ccodeaux); } #define HAVE_SETENV @@ -930,8 +928,9 @@ static void mysetenv(const char *name, const char *value) #ifdef HAVE_SETENV setenv(name, value, 1); #else - int len = strlen(name)+1+strlen(value)+1; + int len = strlen(name) + 1 + strlen(value) + 1; char *str = malloc(len); + sprintf(str, "%s=%s", name, value); putenv(str); #endif @@ -939,32 +938,32 @@ static void mysetenv(const char *name, const char *value) static int set_current_language(const char *restrict locale_choice) MUST_CHECK; -static int set_current_language(const char *restrict loc) + static int set_current_language(const char *restrict loc) { int i; int y_nudge = 0; - char * oldloc; + char *oldloc; char *env_language; if (strlen(loc) > 0) - { - /* Got command line or config file language */ - mysetenv("LANGUAGE", loc); - } - else - { - /* Find what language to use from env vars */ - if (getenv("LANGUAGE") == NULL) { - if (getenv("LC_ALL")) - mysetenv("LANGUAGE", getenv("LC_ALL")); - else if (getenv("LC_MESSAGES")) - mysetenv("LANGUAGE", getenv("LC_MESSAGES")); - else if (getenv("LANG")) - mysetenv("LANGUAGE", getenv("LANG")); + /* Got command line or config file language */ + mysetenv("LANGUAGE", loc); + } + else + { + /* Find what language to use from env vars */ + if (getenv("LANGUAGE") == NULL) + { + if (getenv("LC_ALL")) + mysetenv("LANGUAGE", getenv("LC_ALL")); + else if (getenv("LC_MESSAGES")) + mysetenv("LANGUAGE", getenv("LC_MESSAGES")); + else if (getenv("LANG")) + mysetenv("LANGUAGE", getenv("LANG")); + } } - } oldloc = strdup(loc); @@ -972,14 +971,14 @@ static int set_current_language(const char *restrict 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. */ #ifdef DEBUG -printf ("Locale BEFORE is: %s\n", setlocale(LC_ALL,NULL));//EP + printf("Locale BEFORE is: %s\n", setlocale(LC_ALL, NULL)); //EP #endif setlocale(LC_ALL, ""); setlocale(LC_ALL, loc); ctype_utf8(); #ifdef DEBUG -printf ("Locale AFTER is: %s\n", setlocale(LC_ALL,NULL));//EP -#endif + printf("Locale AFTER is: %s\n", setlocale(LC_ALL, NULL)); //EP +#endif bindtextdomain("tuxpaint", LOCALEDIR); /* Old version of glibc does not have bind_textdomain_codeset() */ @@ -997,78 +996,82 @@ printf ("Locale AFTER is: %s\n", setlocale(LC_ALL,NULL));//EP loc = setlocale(LC_MESSAGES, NULL); #endif - 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 */ + 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 */ - /* 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 - - 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(). + /* 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. - [see also: https://sourceforge.net/mailarchive/message.php?msg_name=787b0d920912222352i5ab22834x92686283b565016b%40mail.gmail.com ] - */ + http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/localedata/SUPPORTED?content-type=text/x-cvsweb-markup&cvsroot=glibc - /* 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? */ + 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. - mysetenv("LANGUAGE", oldloc); - set_langint_from_locale_string(oldloc); - } else { + Maybe it's time to give up on gettext(). + + [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? */ + + 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"); - } - env_language = strdup(getenv("LANGUAGE")); - int j = 0; - char *env_language_lang; - if (*env_language) - { + if (getenv("LANGUAGE") == NULL) + mysetenv("LANGUAGE", "C"); + } + env_language = strdup(getenv("LANGUAGE")); + int j = 0; + char *env_language_lang; + + if (*env_language) + { 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) - { - wished_langs[j].lang_y_nudge = lang_y_nudge[i][1]; - //printf("y_nudge = %d\n", y_nudge); - break; - } - } - + { + 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]; + //printf("y_nudge = %d\n", y_nudge); + break; + } + } - j++; - env_language_lang = strtok(NULL, ":"); - } + + j++; + env_language_lang = strtok(NULL, ":"); + } if (*env_language) - free(env_language); + free(env_language); } - // set_langint_from_locale_string(loc); + // set_langint_from_locale_string(loc); lang_prefix = lang_prefixes[wished_langs[0].langint]; @@ -1084,21 +1087,19 @@ printf ("Locale AFTER is: %s\n", setlocale(LC_ALL,NULL));//EP #if 0 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) { - y_nudge = lang_y_nudge[i][1]; - //printf("y_nudge = %d\n", y_nudge); - break; + // printf("lang_y_nudge[%d][0] = %d\n", i, lang_y_nudge[i][0]); + if (lang_y_nudge[i][0] == langint) + { + y_nudge = lang_y_nudge[i][1]; + //printf("y_nudge = %d\n", y_nudge); + break; + } } - } #endif #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 @@ -1118,29 +1119,29 @@ int setup_i18n(const char *restrict lang, const char *restrict locale) printf("lang \"%s\", locale \"%s\"\n", lang, locale); #endif - if(locale) - { - if(!strcmp(locale,"help")) + if (locale) { - show_locale_usage(stdout,"tuxpaint"); - exit(0); + if (!strcmp(locale, "help")) + { + show_locale_usage(stdout, "tuxpaint"); + exit(0); + } } - } - if(lang) + if (lang) locale = language_to_locale(lang); #ifdef __APPLE__ - patch_i18n(locale); //EP + patch_i18n(locale); //EP #endif #ifdef __ANDROID__ - if(locale == NULL) - locale = android_locale(); + if (locale == NULL) + locale = android_locale(); #endif - if(locale == NULL) - locale = ""; + if (locale == NULL) + locale = ""; return set_current_language(locale); } diff --git a/src/i18n.h b/src/i18n.h index f19c8122a..b2ffa383e 100644 --- a/src/i18n.h +++ b/src/i18n.h @@ -40,133 +40,133 @@ enum { LANG_ACH, /* Acholi */ - LANG_AF, /* Afrikaans */ + LANG_AF, /* Afrikaans */ LANG_AK, /* Akan */ LANG_AM, /* Amharic */ LANG_AN, /* Aragones */ - LANG_AR, /* Arabic */ + LANG_AR, /* Arabic */ LANG_AS, /* Assamese */ - LANG_AST, /* Asturian */ - LANG_AZ, /* Azerbaijani */ - LANG_BE, /* Belarusian */ - LANG_BG, /* Bulgarian */ - LANG_BM, /* Bambara */ + LANG_AST, /* Asturian */ + LANG_AZ, /* Azerbaijani */ + LANG_BE, /* Belarusian */ + LANG_BG, /* Bulgarian */ + LANG_BM, /* Bambara */ LANG_BN, /* Bengali */ - LANG_BO, /* Tibetan */ - LANG_BR, /* Breton */ - LANG_BRX, /* Bodo */ + LANG_BO, /* Tibetan */ + LANG_BR, /* Breton */ + LANG_BRX, /* Bodo */ LANG_BS, /* Bosnian */ LANG_CA_VALENCIA, /* Valencian */ - LANG_CA, /* Catalan */ + LANG_CA, /* Catalan */ LANG_CGG, /* Kiga */ - LANG_CS, /* Czech */ - LANG_CY, /* Welsh */ - LANG_DA, /* Danish */ - LANG_DE, /* German */ - LANG_DOI, /* Dogri */ - LANG_EL, /* Greek */ - LANG_EN, /* English (American) (DEFAULT) */ - LANG_EN_AU, /* English (Australian) */ - LANG_EN_CA, /* English (Canadian) */ - LANG_EN_GB, /* English (British) */ - LANG_EN_ZA, /* English (South African) */ - LANG_EO, /* Esperanto */ - LANG_ES_MX, /* Spanish (Mexican) */ - LANG_ES, /* Spanish */ - LANG_ET, /* Estonian */ - LANG_EU, /* Basque */ - LANG_FA, /* Persian */ + LANG_CS, /* Czech */ + LANG_CY, /* Welsh */ + LANG_DA, /* Danish */ + LANG_DE, /* German */ + LANG_DOI, /* Dogri */ + LANG_EL, /* Greek */ + LANG_EN, /* English (American) (DEFAULT) */ + LANG_EN_AU, /* English (Australian) */ + LANG_EN_CA, /* English (Canadian) */ + LANG_EN_GB, /* English (British) */ + LANG_EN_ZA, /* English (South African) */ + LANG_EO, /* Esperanto */ + LANG_ES_MX, /* Spanish (Mexican) */ + LANG_ES, /* Spanish */ + LANG_ET, /* Estonian */ + LANG_EU, /* Basque */ + LANG_FA, /* Persian */ LANG_FF, /* Fulah */ - LANG_FI, /* Finnish */ - LANG_FO, /* Faroese */ - LANG_FR, /* French */ - LANG_GA, /* Irish Gaelic */ - LANG_GD, /* Scottish Gaelic */ - LANG_GL, /* Galician */ - LANG_GR, /* Gronings */ - LANG_GU, /* Gujarati */ - LANG_HE, /* Hebrew */ - LANG_HI, /* Hindi */ - LANG_HR, /* Croatian */ - LANG_HU, /* Hungarian */ - LANG_HY, /* Armenian */ - LANG_I_KLINGON_ROMANIZED, /* Klingon (Romanized) */ - LANG_ID, /* Indonesian */ - LANG_IS, /* Icelandic */ - LANG_IT, /* Italian */ + LANG_FI, /* Finnish */ + LANG_FO, /* Faroese */ + LANG_FR, /* French */ + LANG_GA, /* Irish Gaelic */ + LANG_GD, /* Scottish Gaelic */ + LANG_GL, /* Galician */ + LANG_GR, /* Gronings */ + LANG_GU, /* Gujarati */ + LANG_HE, /* Hebrew */ + LANG_HI, /* Hindi */ + LANG_HR, /* Croatian */ + LANG_HU, /* Hungarian */ + LANG_HY, /* Armenian */ + LANG_I_KLINGON_ROMANIZED, /* Klingon (Romanized) */ + LANG_ID, /* Indonesian */ + LANG_IS, /* Icelandic */ + LANG_IT, /* Italian */ LANG_IU, /* Inuktitut */ - LANG_JA, /* Japanese */ - LANG_KA, /* Georgian */ + LANG_JA, /* Japanese */ + LANG_KA, /* Georgian */ LANG_KAB, /* Kabyle */ LANG_KN, /* Kannada */ - LANG_KM, /* Khmer */ + LANG_KM, /* Khmer */ LANG_KOK_ROMAN, /* Konkani (Roman) */ LANG_KOK, /* Konkani (Devaganari) */ - LANG_KO, /* Korean */ - LANG_KS_DEVANAGARI, /* Kashmiri (Devanagari script) */ - LANG_KS, /* Kashmiri (Perso-Arabic script) */ - LANG_KU, /* Kurdish */ + LANG_KO, /* Korean */ + LANG_KS_DEVANAGARI, /* Kashmiri (Devanagari script) */ + LANG_KS, /* Kashmiri (Perso-Arabic script) */ + LANG_KU, /* Kurdish */ LANG_LB, /* Luxembourgish */ LANG_LG, /* Luganda */ - LANG_LT, /* Lithuanian */ - LANG_LV, /* Latvian */ + LANG_LT, /* Lithuanian */ + LANG_LV, /* Latvian */ LANG_MAI, /* Maithili */ LANG_ML, /* Malayalam */ - LANG_MK, /* Macedonian */ - LANG_MN, /* Mongolian */ - LANG_MNI_BENGALI, /* Manipuri (Bengali script)*/ + LANG_MK, /* Macedonian */ + LANG_MN, /* Mongolian */ + LANG_MNI_BENGALI, /* Manipuri (Bengali script) */ LANG_MNI_METEI_MAYEK, /* Manipuri (Metei Mayek script) */ LANG_MR, /* Marath */ - LANG_MS, /* Malay */ - LANG_NB, /* Norwegian Bokmal */ + LANG_MS, /* Malay */ + LANG_NB, /* Norwegian Bokmal */ LANG_NE, /* Nepali */ - LANG_NL, /* Dutch */ - LANG_NN, /* Norwegian Nynorsk */ + LANG_NL, /* Dutch */ + LANG_NN, /* Norwegian Nynorsk */ LANG_NR, /* Ndebele */ LANG_NSO, /* Northern Sotho */ - LANG_OC, /* Occitan */ - LANG_OJ, /* Ojibway */ + LANG_OC, /* Occitan */ + LANG_OJ, /* Ojibway */ LANG_OR, /* Odia */ - LANG_PA, /* Punjabi */ - LANG_PL, /* Polish */ - LANG_PT_BR, /* Portuguese (Brazilian) */ - LANG_PT, /* Portuguese */ - LANG_RO, /* Romanian */ - LANG_RU, /* Russian */ - LANG_RW, /* Kinyarwanda */ + LANG_PA, /* Punjabi */ + LANG_PL, /* Polish */ + LANG_PT_BR, /* Portuguese (Brazilian) */ + LANG_PT, /* Portuguese */ + LANG_RO, /* Romanian */ + LANG_RU, /* Russian */ + LANG_RW, /* Kinyarwanda */ LANG_SAT_OL_CHIKI, LANG_SAT, /* Santali */ LANG_SA, /* Sanskrit */ LANG_SD, /* Sindhi (Perso-Arabic) */ LANG_SD_DEVANAGARI, /* Sindhi (Devanagari) */ - LANG_SHS, /* Shuswap */ + LANG_SHS, /* Shuswap */ LANG_SI, /* Sinhala */ - LANG_SK, /* Slovak */ - LANG_SL, /* Slovenian */ + LANG_SK, /* Slovak */ + LANG_SL, /* Slovenian */ LANG_SON, /* Songhay */ - LANG_SQ, /* Albanian */ - LANG_SR_LATIN, /* Serbian (latin) */ - LANG_SR, /* Serbian (cyrillic) */ + LANG_SQ, /* Albanian */ + LANG_SR_LATIN, /* Serbian (latin) */ + LANG_SR, /* Serbian (cyrillic) */ LANG_SU, /* Sundanese */ - LANG_SV, /* Swedish */ - LANG_SW, /* Swahili */ - LANG_TA, /* Tamil */ + LANG_SV, /* Swedish */ + LANG_SW, /* Swahili */ + LANG_TA, /* Tamil */ LANG_TE, /* Telugu */ - LANG_TH, /* Thai */ - LANG_TL, /* Tagalog */ - LANG_TR, /* Turkish */ - LANG_TW, /* Twi */ - LANG_UK, /* Ukrainian */ - LANG_UR, /* Urdu */ - LANG_VEC, /* Venetian */ - LANG_VE, /* Venda */ - LANG_VI, /* Vietnamese */ - LANG_WA, /* Walloon */ + LANG_TH, /* Thai */ + LANG_TL, /* Tagalog */ + LANG_TR, /* Turkish */ + LANG_TW, /* Twi */ + LANG_UK, /* Ukrainian */ + LANG_UR, /* Urdu */ + LANG_VEC, /* Venetian */ + LANG_VE, /* Venda */ + LANG_VI, /* Vietnamese */ + LANG_WA, /* Walloon */ LANG_WO, /* Wolof */ - LANG_XH, /* Xhosa */ - LANG_ZAM, /* Zapotec (Miahuatlan) */ - LANG_ZH_CN, /* Chinese (Simplified) */ - LANG_ZH_TW, /* Chinese (Traditional) */ + LANG_XH, /* Xhosa */ + LANG_ZAM, /* Zapotec (Miahuatlan) */ + LANG_ZH_CN, /* Chinese (Simplified) */ + LANG_ZH_TW, /* Chinese (Traditional) */ LANG_ZU, /* Zulu */ NUM_LANGS }; @@ -185,8 +185,8 @@ typedef struct language_to_locale_struct extern const char *lang_prefixes[NUM_LANGS]; extern int need_own_font; -extern int need_right_to_left; // Right-justify -extern int need_right_to_left_word; // Words need to be reversed, too! (e.g., Hebrew, but not Arabic) +extern int need_right_to_left; // Right-justify +extern int need_right_to_left_word; // Words need to be reversed, too! (e.g., Hebrew, but not Arabic) extern const char *lang_prefix, *short_lang_prefix; extern int num_wished_langs; @@ -207,8 +207,9 @@ extern w_langs wished_langs[255]; int get_current_language(void); int setup_i18n(const char *restrict lang, const char *restrict locale) MUST_CHECK; + #ifdef NO_SDLPANGO -int smash_i18n(void) MUST_CHECK; + int smash_i18n(void) MUST_CHECK; #endif #endif diff --git a/src/im.c b/src/im.c index 4b0587c2e..3683569e6 100644 --- a/src/im.c +++ b/src/im.c @@ -41,7 +41,7 @@ #include #include #include -#include +#include #include "im.h" #ifdef __ANDROID__ @@ -58,7 +58,8 @@ #endif -enum { +enum +{ IM_TIP_NONE, IM_TIP_ENGLISH, IM_TIP_HIRAGANA, @@ -70,8 +71,7 @@ enum { }; -static const char* const im_tip_text[NUM_IM_TIPS] = -{ +static const char *const im_tip_text[NUM_IM_TIPS] = { NULL, // Input Method: English mode @@ -100,9 +100,9 @@ static const char* const im_tip_text[NUM_IM_TIPS] = /* #define IM_DEBUG 1 */ -#define MAX_SECTIONS 8 /* Maximum numbers of sections in *.im file */ -#define MAX_UNICODE_SEQ 16 /* Output of state machine, including NUL */ -#define INITIAL_SMSIZE 8 /* Initial num of transitions in STATE_MACHINE */ +#define MAX_SECTIONS 8 /* Maximum numbers of sections in *.im file */ +#define MAX_UNICODE_SEQ 16 /* Output of state machine, including NUL */ +#define INITIAL_SMSIZE 8 /* Initial num of transitions in STATE_MACHINE */ #ifndef LANG_DEFAULT #define LANG_DEFAULT (LANG_EN) @@ -112,12 +112,13 @@ static const char* const im_tip_text[NUM_IM_TIPS] = /** * Event types that im_event_*() functions need to handle. */ -enum { - IM_REQ_TRANSLATE, /* The ever-more important IM translation request */ - IM_REQ_INIT, /* Initialization request */ - IM_REQ_RESET_SOFT, /* Soft reset request */ - IM_REQ_RESET_FULL, /* Full reset request */ - IM_REQ_FREE, /* Free resources */ +enum +{ + IM_REQ_TRANSLATE, /* The ever-more important IM translation request */ + IM_REQ_INIT, /* Initialization request */ + IM_REQ_RESET_SOFT, /* Soft reset request */ + IM_REQ_RESET_FULL, /* Full reset request */ + IM_REQ_FREE, /* Free resources */ NUM_IM_REQUESTS }; @@ -125,10 +126,11 @@ enum { /** * Match statuses. */ -enum { - MATCH_STAT_NONE = 0x00, +enum +{ + MATCH_STAT_NONE = 0x00, MATCH_STAT_NOMOSTATES = 0x01, - MATCH_STAT_NOMOBUF = 0x02, + MATCH_STAT_NOMOBUF = 0x02, }; @@ -139,7 +141,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 */ /** @@ -148,9 +150,10 @@ typedef int (*IM_EVENT_FN)(IM_DATA*, SDL_Event); /* IM_EVENT_FN type */ * * @see STATE_MACHINE */ -typedef struct SM_WITH_KEY { +typedef struct SM_WITH_KEY +{ char key; - struct STATE_MACHINE* state; + struct STATE_MACHINE *state; } SM_WITH_KEY; @@ -167,13 +170,14 @@ typedef struct SM_WITH_KEY { * * @see SM_WITH_KEY */ -typedef struct STATE_MACHINE { +typedef struct STATE_MACHINE +{ wchar_t output[MAX_UNICODE_SEQ]; char flag; - SM_WITH_KEY* next; /* Possible transitions */ - size_t next_maxsize; /* Potential size of the next pointer */ - size_t next_size; /* Used size of the next pointer */ + SM_WITH_KEY *next; /* Possible transitions */ + size_t next_maxsize; /* Potential size of the next pointer */ + size_t next_size; /* Used size of the next pointer */ } STATE_MACHINE; @@ -183,7 +187,8 @@ typedef struct STATE_MACHINE { * section is used in determining which STATE_MACHINE to use for the * key mapping. */ -typedef struct { +typedef struct +{ STATE_MACHINE sections[MAX_SECTIONS]; int section; @@ -191,8 +196,8 @@ typedef struct { int match_count; /* How many char seq was used for output */ int match_is_final; /* T/F - tells if match is final */ int match_stats; /* Statistics gathering */ - STATE_MACHINE* match_state; - STATE_MACHINE* match_state_prev; + STATE_MACHINE *match_state; + STATE_MACHINE *match_state_prev; } CHARMAP; @@ -226,11 +231,11 @@ 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; - size_t len = wcslen(src)+1; /* Copy over all src string + NUL */ + wchar_t *dest = s; + wchar_t *src = s + count; + size_t len = wcslen(src) + 1; /* Copy over all src string + NUL */ memmove(dest, src, len * sizeof(wchar_t)); } @@ -239,10 +244,12 @@ 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; - if(peg < 0) peg = 0; + + if (peg < 0) + peg = 0; s[peg] = L'\0'; } @@ -255,10 +262,10 @@ static void wcs_pull(wchar_t* s, size_t count) /** * Compare two SM_WITH_KEY, return appropriate result. */ -static int swk_compare(const void* swk1, const void* swk2) +static int swk_compare(const void *swk1, const void *swk2) { - SM_WITH_KEY* sk1 = (SM_WITH_KEY*)swk1; - SM_WITH_KEY* sk2 = (SM_WITH_KEY*)swk2; + SM_WITH_KEY *sk1 = (SM_WITH_KEY *) swk1; + SM_WITH_KEY *sk2 = (SM_WITH_KEY *) swk2; return (sk1->key) - (sk2->key); } @@ -267,15 +274,16 @@ static int swk_compare(const void* swk1, const void* swk2) /** * Initialize the State Machine. */ -static int sm_init(STATE_MACHINE* sm) +static int sm_init(STATE_MACHINE * sm) { memset(sm, 0, sizeof(STATE_MACHINE)); sm->next = calloc(INITIAL_SMSIZE, sizeof(SM_WITH_KEY)); - if(!sm->next) { - perror("sm_init"); - return 1; - } + if (!sm->next) + { + perror("sm_init"); + return 1; + } sm->next_maxsize = INITIAL_SMSIZE; return 0; @@ -285,21 +293,25 @@ static int sm_init(STATE_MACHINE* sm) /** * Free the State Machine resources. */ -static void sm_free(STATE_MACHINE* sm) +static void sm_free(STATE_MACHINE * sm) { - if(sm->next) { - int i = 0; + if (sm->next) + { + int i = 0; - 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; + 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; } - free(sm->next); - sm->next = NULL; - } - memset(sm, 0, sizeof(STATE_MACHINE)); } @@ -307,15 +319,16 @@ static void sm_free(STATE_MACHINE* sm) /** * Double the storage space of the possible transition states. */ -static int sm_dblspace(STATE_MACHINE* sm) +static int sm_dblspace(STATE_MACHINE * sm) { size_t newsize = sm->next_maxsize * 2; - SM_WITH_KEY* next = realloc(sm->next, sizeof(SM_WITH_KEY) * newsize); + SM_WITH_KEY *next = realloc(sm->next, sizeof(SM_WITH_KEY) * newsize); - if(next == NULL) { - perror("sm_dblspace"); - return 1; - } + if (next == NULL) + { + perror("sm_dblspace"); + return 1; + } sm->next = next; sm->next_maxsize = newsize; @@ -328,15 +341,15 @@ static int sm_dblspace(STATE_MACHINE* sm) * Return NULL if none is found. The search is done only at 1 level, and does * not recurse deep. */ -static STATE_MACHINE* sm_search_shallow(STATE_MACHINE* sm, char key) +static STATE_MACHINE *sm_search_shallow(STATE_MACHINE * sm, char key) { SM_WITH_KEY smk = { key, NULL }; - SM_WITH_KEY* smk_found; + 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; + if (!smk_found) + return NULL; return smk_found->state; } @@ -354,22 +367,24 @@ 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, STATE_MACHINE** end) +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); - const wchar_t* unicode; + STATE_MACHINE *sm = sm_search_shallow(start, (char)*key); + const wchar_t *unicode; /* No match - stop recursion */ - if(!sm) { - *matched = 0; - *end = start; + if (!sm) + { + *matched = 0; + *end = start; - return start->output; - } + return start->output; + } /* Match - recurse */ *penult = start; - unicode = sm_search(sm, key+1, matched, penult, end); + unicode = sm_search(sm, key + 1, matched, penult, end); (*matched)++; return unicode; @@ -380,7 +395,7 @@ static const wchar_t* sm_search(STATE_MACHINE* start, wchar_t* key, int* matched * Sort the state machine's transition keys so it can be binary-searched. * The sort is done only at 1 level, and does not recurse deep. */ -static void sm_sort_shallow(STATE_MACHINE* sm) +static void sm_sort_shallow(STATE_MACHINE * sm) { qsort(sm->next, sm->next_size, sizeof(SM_WITH_KEY), swk_compare); } @@ -389,53 +404,61 @@ 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]); + STATE_MACHINE *sm_found = sm_search_shallow(sm, seq[0]); /* Empty sequence */ - if(seq[0] == '\0') { - if(wcslen(sm->output)) { - size_t i; + if (seq[0] == '\0') + { + if (wcslen(sm->output)) + { + 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"); + 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; } - 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]; + 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) { - perror("sm_add"); - return 1; + /* 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); + + /* 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; + } + } + + sm_found = next->state; } - sm_init(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; - } - } - - sm_found = next->state; - } /* Recurse */ - sm_add(sm_found, seq+1, unicode, flag); + sm_add(sm_found, seq + 1, unicode, flag); /* Sort the states */ sm_sort_shallow(sm); @@ -452,16 +475,17 @@ static int sm_add(STATE_MACHINE* sm, char* seq, const wchar_t* unicode, char fla /** * Initialize the character map table. */ -static int charmap_init(CHARMAP* cm) +static int charmap_init(CHARMAP * cm) { int error_code = 0; int i = 0; memset(cm, 0, sizeof(CHARMAP)); - for(i = 0; i < MAX_SECTIONS; i++) { - error_code += sm_init(&cm->sections[i]); - } + for (i = 0; i < MAX_SECTIONS; i++) + { + error_code += sm_init(&cm->sections[i]); + } return error_code; } @@ -478,17 +502,19 @@ 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; - } + if (section >= MAX_SECTIONS) + { + 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 - } + 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 + } return sm_add(&cm->sections[section], seq, unicode, flag[0]); } @@ -501,91 +527,108 @@ static int charmap_add(CHARMAP* cm, int section, char* seq, const wchar_t* unico * @param path The path of the file to load. * @return Zero if the file is loaded fine, nonzero otherwise. */ -static int charmap_load(CHARMAP* cm, const char* path) +static int charmap_load(CHARMAP * cm, const char *path) { - FILE* is = NULL; + FILE *is = NULL; int section = 0; int error_code = 0; /* Open */ is = fopen(path, "rt"); - if(!is) { - 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) { - fprintf(stderr, "%s: Character map syntax error\n", path); + if (!is) + { + perror("path"); return 1; } - /* Handle the first argument */ - if(strcmp(buf, "section") == 0) { /* Section division */ - section++; - continue; - } - else if(buf[0] == '#') { /* Comment */ - fscanf(is, "%*[^\n]"); - continue; - } - else { - char* bp = buf; - int u; + /* Load */ + while (!feof(is)) + { + wchar_t unicode[MAX_UNICODE_SEQ]; + int ulen = 0; - do { - if(sscanf(bp, "%x", &u) == 1) { /* Unicode */ - unicode[ulen++] = u; - } - else { - fprintf(stderr, "%s: Syntax error at '%s'\n", path, buf); + 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) + { + fprintf(stderr, "%s: Character map syntax error\n", path); return 1; } - bp = strchr(bp, ':'); - if(bp) bp++; - } while(bp && ulen < MAX_UNICODE_SEQ-1); - unicode[ulen] = L'\0'; - } + /* Handle the first argument */ + if (strcmp(buf, "section") == 0) + { /* Section division */ + section++; + continue; + } + else if (buf[0] == '#') + { /* Comment */ + fscanf(is, "%*[^\n]"); + continue; + } + else + { + char *bp = buf; + int u; - /* Scan some more */ - scanned = fscanf(is, "%255s\t%255s", buf, flag); - if(scanned < 0) break; + do + { + if (sscanf(bp, "%x", &u) == 1) + { /* Unicode */ + unicode[ulen++] = u; + } + else + { + fprintf(stderr, "%s: Syntax error at '%s'\n", path, buf); + return 1; + } - /* Input count checking */ - switch(scanned) { - case 0: case 1: - fprintf(stderr, "%s: Character map syntax error\n", path); - return 1; + bp = strchr(bp, ':'); + if (bp) + bp++; + } + while (bp && ulen < MAX_UNICODE_SEQ - 1); + unicode[ulen] = L'\0'; + } - default: - if(charmap_add(cm, section, buf, unicode, flag)) { - size_t i = 0; + /* 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)) + { + size_t i = 0; #ifndef __BEOS__ #if defined __GLIBC__ && __GLIBC__ == 2 && __GLIBC_MINOR__ >=2 || __GLIBC__ > 2 || __APPLE__ - 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); @@ -597,13 +640,14 @@ static int charmap_load(CHARMAP* cm, const char* path) /** * Free the resources used by a character map. */ -static void charmap_free(CHARMAP* cm) +static void charmap_free(CHARMAP * cm) { int i; - for(i = 0; i < MAX_SECTIONS; i++) { - sm_free(&cm->sections[i]); - } + for (i = 0; i < MAX_SECTIONS; i++) + { + sm_free(&cm->sections[i]); + } memset(cm, 0, sizeof(CHARMAP)); } @@ -612,15 +656,16 @@ 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; + STATE_MACHINE *start; + const wchar_t *unicode; int section; /* Determine the starting state based on the charmap's active section */ section = cm->section; - if(!IN_RANGE(0, section, (int)ARRAYLEN(cm->sections))) section = 0; + if (!IN_RANGE(0, section, (int)ARRAYLEN(cm->sections))) + section = 0; start = &cm->sections[section]; cm->match_state = NULL; @@ -639,19 +684,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; - } + 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; + } return unicode; } @@ -666,22 +714,34 @@ static const wchar_t* charmap_search(CHARMAP* cm, wchar_t* s) * * @see im_read */ -static int im_event_c(IM_DATA* im, SDL_Event event) +static int im_event_c(IM_DATA * im, SDL_Event event) { SDL_Keysym ks = event.key.keysym; /* Handle event requests */ im->s[0] = L'\0'; - if(im->request != IM_REQ_TRANSLATE) return 0; + if (im->request != IM_REQ_TRANSLATE) + return 0; /* 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); - } + 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); + } //im->s[1] = L'\0'; printf("im->s %ls, event.text.text %s\n", im->s, event.text.text); @@ -710,7 +770,7 @@ static int im_event_c(IM_DATA* im, SDL_Event event) * @see im_event_c() * @see im_event_fns */ -int im_read(IM_DATA* im, SDL_Event event) +int im_read(IM_DATA * im, SDL_Event event) { IM_EVENT_FN im_event_fp = NULL; int redraw = 0; @@ -735,21 +795,24 @@ int im_read(IM_DATA* im, SDL_Event event) } /* 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; - } + 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; + } /* Function pointer to the language-specific im_event_* function */ im_event_fp = im_event_fns[im->lang]; /* Run the language-specific IM or run the default C IM */ - if(im_event_fp) redraw = (*im_event_fp)(im, event); - else redraw = im_event_c(im, event); + if (im_event_fp) + redraw = (*im_event_fp) (im, event); + else + redraw = im_event_c(im, event); - #ifdef IM_DEBUG +#ifdef IM_DEBUG wprintf(L"* [%8ls] [%8ls] %2d %2d (%2d)\n", im->s, im->buf, wcslen(im->s), wcslen(im->buf), im->redraw); - #endif +#endif return redraw; } @@ -762,7 +825,7 @@ int im_read(IM_DATA* im, SDL_Event event) * Generic event handler that calls the appropriate language handler. * im->request should have the event ID. */ -static void im_event(IM_DATA* im) +static void im_event(IM_DATA * im) { SDL_Event event; @@ -776,7 +839,7 @@ static void im_event(IM_DATA* im) /** * Make an event request and call the event handler. */ -static void im_request(IM_DATA* im, int request) +static void im_request(IM_DATA * im, int request) { im->request = request; im_event(im); @@ -790,13 +853,13 @@ static void im_request(IM_DATA* im, int request) /** * Free any allocated resources. */ -static void im_free(IM_DATA* im) +static void im_free(IM_DATA * im) { im_request(im, IM_REQ_FREE); } -void im_softreset(IM_DATA* im) +void im_softreset(IM_DATA * im) { im->s[0] = L'\0'; im->buf[0] = L'\0'; @@ -805,7 +868,7 @@ void im_softreset(IM_DATA* im) } -static void im_fullreset(IM_DATA* im) +static void im_fullreset(IM_DATA * im) { im->s[0] = L'\0'; im->buf[0] = L'\0'; @@ -860,29 +923,32 @@ static void im_fullreset(IM_DATA* im) * * @see im_read */ -static int im_event_zh_tw(IM_DATA* im, SDL_Event event) +static int im_event_zh_tw(IM_DATA * im, SDL_Event event) { SDL_Keysym ks = event.key.keysym; - static const char* lang_file = IMDIR "zh_tw.im"; - enum { SEC_ENGLISH, SEC_ZH_TW, SEC_TOTAL }; + static const char *lang_file = IMDIR "zh_tw.im"; + enum + { SEC_ENGLISH, SEC_ZH_TW, SEC_TOTAL }; static CHARMAP cm; /* Handle event requests */ - switch(im->request) { - case 0: break; + switch (im->request) + { + case 0: + break; - case IM_REQ_FREE: /* Free allocated resources */ + case IM_REQ_FREE: /* Free allocated resources */ charmap_free(&cm); /* go onto full reset */ - case IM_REQ_RESET_FULL: /* Full reset */ + case IM_REQ_RESET_FULL: /* Full reset */ cm.section = SEC_ENGLISH; im->tip_text = im_tip_text[IM_TIP_ENGLISH]; /* go onto soft reset */ - case IM_REQ_RESET_SOFT: /* Soft reset */ + case IM_REQ_RESET_SOFT: /* Soft reset */ im->s[0] = L'\0'; im->buf[0] = L'\0'; im->redraw = 0; @@ -892,155 +958,184 @@ static int im_event_zh_tw(IM_DATA* im, SDL_Event event) cm.match_state_prev = &cm.sections[cm.section]; break; - case IM_REQ_INIT: /* Initialization */ + 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); - } + 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); - #ifdef DEBUG +#ifdef DEBUG printf("IM: Loaded '%s'\n", lang_file); - #endif +#endif break; - } - if(im->request != IM_REQ_TRANSLATE) return 0; + } + if (im->request != IM_REQ_TRANSLATE) + return 0; /* Discard redraw characters, so they can be redrawn */ - if((int)wcslen(im->s) < im->redraw) im->redraw = wcslen(im->s); - wcs_lshift(im->s, (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: + 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: + 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 % SEC_TOTAL); /* Change section */ - im_softreset(im); /* Soft reset */ + /* Left-Alt & Right-Alt mapped to mode-switch */ + case SDLK_RALT: + case SDLK_LALT: + cm.section = (++cm.section % 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; - } + 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; + } break; - /* Enter finalizes previous redraw */ + /* Enter finalizes previous redraw */ case SDLK_RETURN: - if(im->redraw <= 0) { - im->s[0] = L'\r'; - im->s[1] = L'\0'; - } + 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 */ + /* 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); + 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 - { - wchar_t u = event.text.text[0]; + 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 */ + 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 + /* Translate the characters */ + im->redraw = 0; + while (1) + { + const wchar_t *us = charmap_search(&cm, im->buf); - /* Match was found? */ - if(us && wcslen(us)) { - #ifdef IM_DEBUG - wprintf(L" 1\n"); - #endif +#ifdef IM_DEBUG + wprintf(L" [%8ls] [%8ls] %2d %2d\n", im->s, im->buf, wcslen(im->s), wcslen(im->buf)); +#endif - wcscat(im->s, us); + /* Match was found? */ + if (us && wcslen(us)) + { +#ifdef IM_DEBUG + wprintf(L" 1\n"); +#endif - /* 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; - } + wcscat(im->s, us); - /* Is this the end? */ - if(cm.match_is_final) break; - } - } - } - } + /* 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; } @@ -1051,29 +1146,32 @@ static int im_event_zh_tw(IM_DATA* im, SDL_Event event) * * @see im_read */ -static int im_event_th(IM_DATA* im, SDL_Event event) +static int im_event_th(IM_DATA * im, SDL_Event event) { SDL_Keysym ks = event.key.keysym; - static const char* lang_file = IMDIR "th.im"; - enum { SEC_ENGLISH, SEC_THAI, SEC_TOTAL }; + static const char *lang_file = IMDIR "th.im"; + enum + { SEC_ENGLISH, SEC_THAI, SEC_TOTAL }; static CHARMAP cm; /* Handle event requests */ - switch(im->request) { - case 0: break; + switch (im->request) + { + case 0: + break; - case IM_REQ_FREE: /* Free allocated resources */ + case IM_REQ_FREE: /* Free allocated resources */ charmap_free(&cm); /* go onto full reset */ - case IM_REQ_RESET_FULL: /* Full reset */ + case IM_REQ_RESET_FULL: /* Full reset */ cm.section = SEC_ENGLISH; im->tip_text = im_tip_text[IM_TIP_ENGLISH]; /* go onto soft reset */ - case IM_REQ_RESET_SOFT: /* Soft reset */ + case IM_REQ_RESET_SOFT: /* Soft reset */ im->s[0] = L'\0'; im->buf[0] = L'\0'; im->redraw = 0; @@ -1083,152 +1181,181 @@ static int im_event_th(IM_DATA* im, SDL_Event event) cm.match_state_prev = &cm.sections[cm.section]; break; - case IM_REQ_INIT: /* Initialization */ + 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); - } + 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); - #ifdef DEBUG +#ifdef DEBUG printf("IM: Loaded '%s'\n", lang_file); - #endif +#endif break; - } - if(im->request != IM_REQ_TRANSLATE) return 0; + } + if (im->request != IM_REQ_TRANSLATE) + return 0; /* Discard redraw characters, so they can be redrawn */ - if((int)wcslen(im->s) < im->redraw) im->redraw = wcslen(im->s); - wcs_lshift(im->s, (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: + 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_LGUI: + case SDLK_RGUI: case SDLK_MENU: - case SDLK_MODE: case SDLK_APPLICATION: + case SDLK_MODE: + case SDLK_APPLICATION: break; - /* Right-Alt mapped to mode-switch */ + /* Right-Alt mapped to mode-switch */ case SDLK_RALT: - cm.section = (++cm.section % SEC_TOTAL); /* Change section */ - im_softreset(im); /* Soft reset */ + cm.section = (++cm.section % 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; - } + 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; + } break; - /* Enter finalizes previous redraw */ + /* Enter finalizes previous redraw */ case SDLK_RETURN: - if(im->redraw <= 0) { - im->s[0] = L'\r'; - im->s[1] = L'\0'; - } + 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 */ + /* 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; + 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; } @@ -1239,29 +1366,32 @@ static int im_event_th(IM_DATA* im, SDL_Event event) * * @see im_read */ -static int im_event_ja(IM_DATA* im, SDL_Event event) +static int im_event_ja(IM_DATA * im, SDL_Event event) { SDL_Keysym ks = event.key.keysym; - static const char* lang_file = IMDIR "ja.im"; - enum { SEC_ENGLISH, SEC_HIRAGANA, SEC_KATAKANA, SEC_TOTAL }; + static const char *lang_file = IMDIR "ja.im"; + enum + { SEC_ENGLISH, SEC_HIRAGANA, SEC_KATAKANA, SEC_TOTAL }; static CHARMAP cm; /* Handle event requests */ - switch(im->request) { - case 0: break; + switch (im->request) + { + case 0: + break; - case IM_REQ_FREE: /* Free allocated resources */ + case IM_REQ_FREE: /* Free allocated resources */ charmap_free(&cm); /* go onto full reset */ - case IM_REQ_RESET_FULL: /* Full reset */ + case IM_REQ_RESET_FULL: /* Full reset */ cm.section = SEC_ENGLISH; im->tip_text = im_tip_text[IM_TIP_ENGLISH]; /* go onto soft reset */ - case IM_REQ_RESET_SOFT: /* Soft reset */ + case IM_REQ_RESET_SOFT: /* Soft reset */ im->s[0] = L'\0'; im->buf[0] = L'\0'; im->redraw = 0; @@ -1271,156 +1401,187 @@ static int im_event_ja(IM_DATA* im, SDL_Event event) cm.match_state_prev = &cm.sections[cm.section]; break; - case IM_REQ_INIT: /* Initialization */ + 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); - } + 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); - #ifdef DEBUG +#ifdef DEBUG printf("IM: Loaded '%s'\n", lang_file); - #endif +#endif break; - } - if(im->request != IM_REQ_TRANSLATE) return 0; + } + if (im->request != IM_REQ_TRANSLATE) + return 0; /* Discard redraw characters, so they can be redrawn */ - if((int)wcslen(im->s) < im->redraw) im->redraw = wcslen(im->s); - wcs_lshift(im->s, (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: + 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_LGUI: + case SDLK_RGUI: case SDLK_MENU: - case SDLK_MODE: case SDLK_APPLICATION: + case SDLK_MODE: + case SDLK_APPLICATION: break; - /* Right-Alt mapped to mode-switch */ + /* Right-Alt mapped to mode-switch */ case SDLK_RALT: - cm.section = (++cm.section % SEC_TOTAL); /* Change section */ - im_softreset(im); /* Soft reset */ + cm.section = (++cm.section % 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; - } + 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; + } break; - /* Enter finalizes previous redraw */ + /* Enter finalizes previous redraw */ case SDLK_RETURN: - if(im->redraw <= 0) { - im->s[0] = L'\r'; - im->s[1] = L'\0'; - } + 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 */ + /* 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); + 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 { - 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; + im->buf[0] = L'\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; - } + /* Hiragana and Katakana modes */ + else + { + wchar_t u = event.text.text[0]; - /* Is this the end? */ - if(cm.match_is_final) break; + 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; } @@ -1431,15 +1592,16 @@ static int im_event_ja(IM_DATA* im, SDL_Event event) * * @see im_event_ko */ -static int im_event_ko_isvowel(CHARMAP* cm, wchar_t c) +static int im_event_ko_isvowel(CHARMAP * cm, wchar_t c) { STATE_MACHINE *start, *next; - const wchar_t* unicode; + const wchar_t *unicode; int section; /* Determine the starting state based on the charmap's active section */ section = cm->section; - if(!IN_RANGE(0, section, (int)ARRAYLEN(cm->sections))) section = 0; + if (!IN_RANGE(0, section, (int)ARRAYLEN(cm->sections))) + section = 0; start = &cm->sections[section]; next = sm_search_shallow(start, (char)c); @@ -1454,29 +1616,32 @@ static int im_event_ko_isvowel(CHARMAP* cm, wchar_t c) * * @see im_read */ -static int im_event_ko(IM_DATA* im, SDL_Event event) +static int im_event_ko(IM_DATA * im, SDL_Event event) { SDL_Keysym ks = event.key.keysym; - static const char* lang_file = IMDIR "ko.im"; - enum { SEC_ENGLISH, SEC_HANGUL, SEC_TOTAL }; + static const char *lang_file = IMDIR "ko.im"; + enum + { SEC_ENGLISH, SEC_HANGUL, SEC_TOTAL }; static CHARMAP cm; /* Handle event requests */ - switch(im->request) { - case 0: break; + switch (im->request) + { + case 0: + break; - case IM_REQ_FREE: /* Free allocated resources */ + case IM_REQ_FREE: /* Free allocated resources */ charmap_free(&cm); /* go onto full reset */ - case IM_REQ_RESET_FULL: /* Full reset */ + case IM_REQ_RESET_FULL: /* Full reset */ cm.section = SEC_ENGLISH; im->tip_text = im_tip_text[IM_TIP_ENGLISH]; /* go onto soft reset */ - case IM_REQ_RESET_SOFT: /* Soft reset */ + case IM_REQ_RESET_SOFT: /* Soft reset */ im->s[0] = L'\0'; im->buf[0] = L'\0'; im->redraw = 0; @@ -1486,200 +1651,236 @@ static int im_event_ko(IM_DATA* im, SDL_Event event) cm.match_state_prev = &cm.sections[cm.section]; break; - case IM_REQ_INIT: /* Initialization */ + 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); - } + 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); - #ifdef DEBUG +#ifdef DEBUG printf("IM: Loaded '%s'\n", lang_file); - #endif +#endif break; - } - if(im->request != IM_REQ_TRANSLATE) return 0; + } + if (im->request != IM_REQ_TRANSLATE) + return 0; /* Discard redraw characters, so they can be redrawn */ - if((int)wcslen(im->s) < im->redraw) im->redraw = wcslen(im->s); - wcs_lshift(im->s, (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: + 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: + case SDLK_MODE: + case SDLK_APPLICATION: break; - /* Right-Alt mapped to mode-switch */ - case SDLK_LALT: case SDLK_RALT: - cm.section = (++cm.section % SEC_TOTAL); /* Change section */ - im_softreset(im); /* Soft reset */ + /* Right-Alt mapped to mode-switch */ + case SDLK_LALT: + case SDLK_RALT: + cm.section = (++cm.section % 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; - } + 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; + } break; - /* Backspace removes only a single buffered character */ + /* 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'; - } + if (wcslen(im->buf) > 0) + { + wcs_pull(im->buf, 1); + if (im->redraw > 0) + im->redraw--; + event.text.text[0] = L'\0'; + } /* continue processing: */ - /* Actual character 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; + 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 */ + 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 + /* Translate the characters */ + im->redraw = 0; + while (1) + { + const wchar_t *us = charmap_search(&cm, bp); - /* 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]; +#ifdef IM_DEBUG + wprintf(L" [%8ls] [%8ls] %2d %2d\n", im->s, im->buf, wcslen(im->s), wcslen(im->buf)); +#endif - /* If there is no more buffer, output it */ - if(cm.match_stats & MATCH_STAT_NOMOBUF) { - #ifdef IM_DEBUG - wprintf(L" 1a\n"); - #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]; - 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 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; } - /* 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; } @@ -1695,32 +1896,35 @@ static int im_event_ko(IM_DATA* im, SDL_Event event) * @param im IM_DATA structure to initialize. * @param lang LANG_* defined constant to initialize the structure with. */ -void im_init(IM_DATA* im, int lang) +void im_init(IM_DATA * im, int lang) { /* Free already allocated resources if initialized before */ - if(im_initialized) { - im_free(im); - } + if (im_initialized) + { + im_free(im); + } /* Initialize */ memset(im, 0, sizeof(IM_DATA)); im->lang = 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; + 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; - im_initialized = 1; - } + im_initialized = 1; + } - #ifdef DEBUG +#ifdef DEBUG assert(0 <= im->lang && im->lang < NUM_LANGS); - if(im_initialized) printf("Initializing IM for %s...\n", lang_prefixes[im->lang]); - #endif + if (im_initialized) + printf("Initializing IM for %s...\n", lang_prefixes[im->lang]); +#endif /* Initialize the individual IM */ im_request(im, IM_REQ_INIT); diff --git a/src/im.h b/src/im.h index 0a12dcaf9..14b6656d1 100644 --- a/src/im.h +++ b/src/im.h @@ -33,15 +33,16 @@ * TYPES */ -typedef struct IM_DATA { - int lang; /* Language used in sequence translation */ - wchar_t s[16]; /* Characters that should be displayed */ - const char* tip_text; /* Tip text, read-only please */ +typedef struct IM_DATA +{ + int lang; /* Language used in sequence translation */ + wchar_t s[16]; /* Characters that should be displayed */ + const char *tip_text; /* Tip text, read-only please */ /* For use by language-specific im_event_ calls. PRIVATE! */ - wchar_t buf[8]; /* Buffered characters */ - int redraw; /* Redraw this many characters next time */ - int request; /* Event request */ + wchar_t buf[8]; /* Buffered characters */ + int redraw; /* Redraw this many characters next time */ + int request; /* Event request */ } IM_DATA; @@ -49,9 +50,9 @@ typedef struct IM_DATA { * FUNCTIONS */ -void im_init(IM_DATA* im, int lang); /* Initialize IM */ -void im_softreset(IM_DATA* im); /* Soft Reset IM */ -int im_read(IM_DATA* im, SDL_Event event); +void im_init(IM_DATA * im, int lang); /* Initialize IM */ +void im_softreset(IM_DATA * im); /* Soft Reset IM */ +int im_read(IM_DATA * im, SDL_Event event); #endif /* TUXPAINT_IM_H */ diff --git a/src/macosx_print.h b/src/macosx_print.h index 28c5f831b..7c0a2aaaa 100644 --- a/src/macosx_print.h +++ b/src/macosx_print.h @@ -25,17 +25,16 @@ #include "SDL.h" -const char *SurfacePrint(SDL_Surface *surface, int showDialog); -int DisplayPageSetup(const SDL_Surface *surface); +const char *SurfacePrint(SDL_Surface * surface, int showDialog); +int DisplayPageSetup(const SDL_Surface * surface); #ifdef OBJECTIVEC -@interface PrintSheetController : NSObject +@interface PrintSheetController:NSObject { - bool displayPrintSetupSheet; - bool displayPrintSheet; + bool displayPrintSetupSheet; + bool displayPrintSheet; } -- -@end -#endif OBJECTIVEC +-@end +#endif /* OBJECTIVEC */ diff --git a/src/onscreen_keyboard.c b/src/onscreen_keyboard.c index 806f2a085..b78bb8b9f 100644 --- a/src/onscreen_keyboard.c +++ b/src/onscreen_keyboard.c @@ -1,34 +1,35 @@ #ifdef __APPLE__ -#include "patch.h" //EP +#include "patch.h" //EP #endif #include "onscreen_keyboard.h" #define DEBUG_OSK_COMPOSEMAP -static SDL_Color def_bgcolor = {255, 255, 255, 255}; -static SDL_Color def_fgcolor = {0, 0, 0, 0}; +static SDL_Color def_bgcolor = { 255, 255, 255, 255 }; +static SDL_Color def_fgcolor = { 0, 0, 0, 0 }; -static void load_hlayout(osk_layout *layout, char * layout_name); -static void load_keymap(osk_layout *layout, char * keymap_name); -static void load_composemap(osk_layout *layout, char * composemap_name); +static void load_hlayout(osk_layout * layout, char *layout_name); +static void load_keymap(osk_layout * layout, char *keymap_name); +static void load_composemap(osk_layout * layout, char *composemap_name); static int is_blank_or_comment(char *line); + /* static int isw_blank_or_comment(wchar_t *line); */ -static void keybd_prepare(on_screen_keyboard *keyboard); +static void keybd_prepare(on_screen_keyboard * keyboard); static void draw_key(osk_key key, on_screen_keyboard * keyboard, int hot); -static void label_key(osk_key key, on_screen_keyboard *keyboard); -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 void label_key(osk_key key, on_screen_keyboard * keyboard); +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); #ifdef DEBUG_OSK_COMPOSEMAP -static void print_composemap(osk_composenode *composemap, char * sp); +static void print_composemap(osk_composenode * composemap, char *sp); #endif #ifdef WIN32 @@ -36,9 +37,9 @@ static void print_composemap(osk_composenode *composemap, char * sp); #define wcstok(line, delim, pointer) wcstok(line, delim) #define strtok_r(line, delim, pointer) strtok(line, delim) -static void mtw(wchar_t * wtok, char * tok); +static void mtw(wchar_t * wtok, char *tok); -static void mtw(wchar_t * wtok, char * tok) +static void mtw(wchar_t * wtok, char *tok) { /* workaround using iconv to get a functionallity somewhat approximate as mbstowcs() */ Uint16 *ui16; @@ -50,58 +51,61 @@ static void mtw(wchar_t * wtok, char * tok) in = 250; out = 250; ui16 = malloc(sizeof(Uint16) * 255); - wrptr = (char *) ui16; + wrptr = (char *)ui16; trans = iconv_open("WCHAR_T", "UTF-8"); - iconv(trans, (const char **) &tok, &in, &wrptr, &out); + iconv(trans, (const char **)&tok, &in, &wrptr, &out); *((wchar_t *) wrptr) = L'\0'; swprintf(wtok, L"%ls", ui16); free(ui16); iconv_close(trans); } + #define mbstowcs(wtok, tok, size) mtw(wtok, tok) #endif -struct osk_keyboard *osk_create(char *layout_name, SDL_Surface *canvas, SDL_Surface *button_up, SDL_Surface *button_down, SDL_Surface *button_off, SDL_Surface *button_nav, SDL_Surface *button_hold, SDL_Surface *oskdel, SDL_Surface *osktab, SDL_Surface *oskenter, SDL_Surface *oskcapslock, SDL_Surface *oskshift, int disable_change) +struct osk_keyboard *osk_create(char *layout_name, SDL_Surface * canvas, SDL_Surface * button_up, + SDL_Surface * button_down, SDL_Surface * button_off, SDL_Surface * button_nav, + SDL_Surface * button_hold, SDL_Surface * oskdel, SDL_Surface * osktab, + SDL_Surface * oskenter, SDL_Surface * oskcapslock, SDL_Surface * oskshift, + int disable_change) { SDL_Surface *surface; osk_layout *layout; - on_screen_keyboard * keyboard; + on_screen_keyboard *keyboard; keyboard = malloc(sizeof(on_screen_keyboard)); keyboard->osk_fonty = NULL; - - keyboard->disable_change = disable_change; + + keyboard->disable_change = disable_change; layout = load_layout(keyboard, layout_name); if (!layout) - { - printf("Error trying to load the required layout %s\n", layout_name); - layout = load_layout(keyboard, strdup("default.layout")); - if (!layout) { - printf("Error trying to load the default layout\n"); - return NULL; + printf("Error trying to load the required layout %s\n", layout_name); + layout = load_layout(keyboard, strdup("default.layout")); + if (!layout) + { + printf("Error trying to load the default layout\n"); + return NULL; + } + printf("Loaded the default layout instead.\n"); } - printf("Loaded the default layout instead.\n"); - } #ifdef DEBUG printf("w %i, h %i\n", layout->width, layout->height); #endif 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); + layout->width * button_up->w, + layout->height * button_up->h, + canvas->format->BitsPerPixel, + canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, 0); if (!surface) - { - printf("Error creating the onscreen keyboard surface\n"); - return NULL; - } + { + printf("Error creating the onscreen keyboard surface\n"); + return NULL; + } // keyboard->name = layout_name; keyboard->layout = layout; keyboard->surface = surface; @@ -136,7 +140,9 @@ struct osk_keyboard *osk_create(char *layout_name, SDL_Surface *canvas, SDL_Surf keyboard->kmdf.dead3 = NULL; keyboard->kmdf.dead4 = NULL; - SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, keyboard->layout->bgcolor.r, keyboard->layout->bgcolor.g, keyboard->layout->bgcolor.b)); + SDL_FillRect(surface, NULL, + SDL_MapRGB(surface->format, keyboard->layout->bgcolor.r, keyboard->layout->bgcolor.g, + keyboard->layout->bgcolor.b)); keybd_prepare(keyboard); @@ -144,12 +150,12 @@ struct osk_keyboard *osk_create(char *layout_name, SDL_Surface *canvas, SDL_Surf 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; - char * line; - char * filename; + char *line; + char *filename; char *key, *value; osk_layout *layout; @@ -161,39 +167,39 @@ static struct osk_layout *load_layout(on_screen_keyboard *keyboard, char *layout #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) { - /* Try with DATA_PREFIX */ - - snprintf(filename, 255, "%sosk/%s", DATA_PREFIX, layout_name); - fi = fopen(filename, "r"); + keyboard->name = strdup(layout_name); + /* Try full path */ + fi = fopen(layout_name, "r"); if (fi == NULL) - { - printf("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"); - } + { + /* Try with DATA_PREFIX */ + + snprintf(filename, 255, "%sosk/%s", DATA_PREFIX, layout_name); + fi = fopen(filename, "r"); + if (fi == NULL) + { + printf("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"); - } + { + snprintf(filename, 255, "%sosk/default.layout", DATA_PREFIX); + fi = fopen(filename, "r"); + keyboard->name = strdup("default.layout"); + } free(filename); if (fi == NULL) - { - printf("Can't load the on screen keyboard layout\n"); - return NULL; - } + { + printf("Can't load the on screen keyboard layout\n"); + return NULL; + } line = malloc(sizeof(char) * 1024); @@ -201,52 +207,52 @@ static struct osk_layout *load_layout(on_screen_keyboard *keyboard, char *layout value = malloc(sizeof(char) * 255); while (!feof(fi)) - { - fgets(line, 1023, fi); - - if (is_blank_or_comment(line)) - continue; - - sscanf(line, "%s %s", key, value); - if (strcmp("layout", key) == 0 && !hlayout_loaded) { + 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 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) - { + load_hlayout(layout, value); + hlayout_loaded = 1; + } + else if (strncmp("keymap", key, 6) == 0) + { #ifdef DEBUG - printf("keymap found: %s\n", value); + printf("keymap found: %s\n", value); #endif - load_keymap(layout, value); - } - else if (strncmp("composemap", key, 10) == 0) - { + load_keymap(layout, value); + } + else if (strncmp("composemap", key, 10) == 0) + { #ifdef DEBUG - printf("composemap found: %s\n", value); + 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); - } - + 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 DEBUG - printf("key %s, value %s\n", key, value); + printf("key %s, value %s\n", key, value); #endif - key[0] = '\0'; - value[0] = '\0'; - } + key[0] = '\0'; + value[0] = '\0'; + } @@ -260,7 +266,7 @@ static struct osk_layout *load_layout(on_screen_keyboard *keyboard, char *layout /* A hlayout contains the definitions of the keyboard as seen in the screen. Things like the number of rows of the keyboard, the font used to render the keys, the width of the keys, and a code that matches each key like in real hardware keyboards */ -void load_hlayout(osk_layout *layout, char * hlayout_name) +void load_hlayout(osk_layout * layout, char *hlayout_name) { int width, height; int key_number, line_number; @@ -273,7 +279,7 @@ void load_hlayout(osk_layout *layout, char * hlayout_name) char *line; char *key, *fontpath; char *plain_label, *top_label, *altgr_label, *shift_altgr_label; - FILE * fi; + FILE *fi; key_number = line_number = 0; width = height = 0; @@ -285,19 +291,19 @@ void load_hlayout(osk_layout *layout, char * hlayout_name) /* Try full path */ fi = fopen(hlayout_name, "r"); if (fi == NULL) - { - /* Try with DATA_PREFIX */ - - snprintf(filename, 255, "%sosk/%s", DATA_PREFIX, hlayout_name); - fi = fopen(filename, "r"); - if (fi == NULL) { - printf("Can't open either %s nor %s\n", hlayout_name, filename); - layout->keys = NULL; - free(filename); - return; + /* Try with DATA_PREFIX */ + + snprintf(filename, 255, "%sosk/%s", DATA_PREFIX, hlayout_name); + fi = fopen(filename, "r"); + if (fi == NULL) + { + printf("Can't open either %s nor %s\n", hlayout_name, filename); + layout->keys = NULL; + free(filename); + return; + } } - } free(filename); @@ -316,128 +322,123 @@ void load_hlayout(osk_layout *layout, char * hlayout_name) while (!feof(fi)) - { - if(width && height && !allocated) { - layout->keys = malloc(height * sizeof(osk_key *)); - layout->keys[0] = malloc(width * sizeof(osk_key )); + if (width && height && !allocated) + { + 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 DEBUG - printf("w %i, h %i\n" , layout->width, layout->height); + printf("w %i, h %i\n", layout->width, layout->height); #endif - allocated = 1; - } + allocated = 1; + } - fgets(line, 1023, fi); + fgets(line, 1023, fi); - if (is_blank_or_comment(line)) - continue; + if (is_blank_or_comment(line)) + continue; - if (strncmp(line, "WIDTH", 5) == 0) - sscanf(line, "%s %i", key, &width); + 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, "HEIGHT", 5) == 0) + sscanf(line, "%s %i", key, &height); - else if (strncmp(line, "FONTPATH", 8) == 0) - { + else if (strncmp(line, "FONTPATH", 8) == 0) + { #ifdef DEBUG - printf("linefont %s\n", line); + 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) - { + sscanf(line, "%s %s", key, fontpath); + if (!is_blank_or_comment(fontpath)) + have_fontpath = 1; + } + else if (strncmp(line, "FGCOLOR", 5) == 0) + { #ifdef DEBUG - printf("linefont %s\n", line); + 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) - { + 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 DEBUG - printf("linefont %s\n", line); + 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", 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 ++; + 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); @@ -450,98 +451,97 @@ void load_hlayout(osk_layout *layout, char * hlayout_name) /* { */ /* printf(" %i, \n ", j); */ /* if(layout.keys[i][j].width) */ - /* printf("keycode %d, width %f, plain %ls, caps %ls\n", */ - /* layout.keys[i][j].keycode, */ - /* layout.keys[i][j].width, */ - /* layout.keys[i][j].plain_label, */ - /* layout.keys[i][j].caps_label); */ + /* printf("keycode %d, width %f, plain %ls, caps %ls\n", */ + /* layout.keys[i][j].keycode, */ + /* layout.keys[i][j].width, */ + /* layout.keys[i][j].plain_label, */ + /* layout.keys[i][j].caps_label); */ /* } */ /* } */ } /* A keymap contains the keysyms (X keysym mnemonics) associated to each keycode in the hlayout.*/ -void load_keymap(osk_layout *layout, char * keymap_name) +void load_keymap(osk_layout * layout, char *keymap_name) { int i, keycode, readed; char *filename; char *ksname1, *ksname2, *ksname3, *ksname4; char *line; - FILE * fi; + FILE *fi; filename = malloc(sizeof(char) * 255); /* Try full path */ fi = fopen(keymap_name, "r"); if (fi == NULL) - { - /* Try with DATA_PREFIX */ - - snprintf(filename, 255, "%sosk/%s", DATA_PREFIX, keymap_name); - fi = fopen(filename, "r"); - if (fi == NULL) { - printf("Can't open either %s nor %s\n", keymap_name, filename); - layout->keys = NULL; - free(filename); - return; + /* Try with DATA_PREFIX */ + + snprintf(filename, 255, "%sosk/%s", DATA_PREFIX, keymap_name); + fi = fopen(filename, "r"); + if (fi == NULL) + { + printf("Can't open either %s nor %s\n", keymap_name, filename); + layout->keys = NULL; + free(filename); + return; + } } - } free(filename); line = malloc(sizeof(char) * 1024); 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; - } + 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; + } while (!feof(fi)) - { - 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; + 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; + } } - 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); @@ -550,10 +550,10 @@ void load_keymap(osk_layout *layout, char * keymap_name) /* { */ /* if (layout.keymap[i].plain) */ /* printf("%i, %i, %i, %i, %i\n", i, */ - /* layout.keymap[i].plain, */ - /* layout.keymap[i].caps, */ - /* layout.keymap[i].altgr, */ - /* layout.keymap[i].shiftaltgr); */ + /* layout.keymap[i].plain, */ + /* layout.keymap[i].caps, */ + /* layout.keymap[i].altgr, */ + /* layout.keymap[i].shiftaltgr); */ /* } */ @@ -561,10 +561,10 @@ 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; + char *tok; wchar_t *result, *wtok; osk_composenode *auxnode; @@ -572,89 +572,89 @@ static void gettokens(char * line, char * delim, char ** pointer, osk_composenod tok = strdup(strtok_r(line, delim, pointer)); - if(!tok) + if (!tok) 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) + if (tok[0] == ':') /* End of precompose keysyms, next will be the result in UTF-8. */ { - 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; + free(tok); + tok = strdup(strtok_r(line, ": \"\t", pointer)); - /* printf("size %d, keysym %ls => ", composenode->size, composenode->childs[0]->keysym); */ + mbstowcs(wtok, tok, 255); - gettokens(NULL, delim, pointer, composenode->childs[0], layout); + result = wcsdup(wtok); + /* printf("->%ls<-\n", wtok); */ free(wtok); free(tok); + composenode->result = result; + return; + } + else + { + if (composenode->size == 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); */ + + gettokens(NULL, delim, pointer, composenode->childs[0], layout); + free(wtok); + free(tok); + 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; } - 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; - } } /* A compose map contains the sequences of keysyms (X keysym mnemonics) needed to generate another keysym. The last in the sequence is the result, the others will be searched in the order they appear. They will be classified in a multiway tree.*/ -static void load_composemap(osk_layout *layout, char * composemap_name) +static void load_composemap(osk_layout * layout, char *composemap_name) { char *filename; char **pointer; char *line; - FILE * fi; + FILE *fi; pointer = malloc(sizeof(wchar_t *)); filename = malloc(sizeof(char) * 255); @@ -662,19 +662,19 @@ 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 */ - - snprintf(filename, 255, "%sosk/%s", DATA_PREFIX, composemap_name); - fi = fopen(filename, "r"); - if (fi == NULL) { - printf("Can't open either %s nor %s\n", composemap_name, filename); - layout->keys = NULL; - free(filename); - return; + /* Try with DATA_PREFIX */ + + snprintf(filename, 255, "%sosk/%s", DATA_PREFIX, composemap_name); + fi = fopen(filename, "r"); + if (fi == NULL) + { + printf("Can't open either %s nor %s\n", composemap_name, filename); + layout->keys = NULL; + free(filename); + return; + } } - } free(filename); @@ -686,14 +686,14 @@ static void load_composemap(osk_layout *layout, char * composemap_name) line = malloc(1024 * sizeof(char)); while (!feof(fi)) - { - fgets(line, 1023, fi); + { + 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); @@ -704,10 +704,10 @@ static void load_composemap(osk_layout *layout, char * composemap_name) } #ifdef DEBUG_OSK_COMPOSEMAP -static void print_composemap(osk_composenode *composemap, char * sp) +static void print_composemap(osk_composenode * composemap, char *sp) { int i; - char * space; + char *space; space = malloc(sizeof(char) * 255); @@ -716,27 +716,27 @@ static void print_composemap(osk_composenode *composemap, char * sp) printf("%d ==> ", composemap->size); #endif if (composemap->size == 0) - { + { #ifdef 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 DEBUG printf("%s", space); #endif for (i = 0; i < composemap->size; i++) { - print_composemap(composemap->childs[i], space); + print_composemap(composemap->childs[i], space); // free(space); } /* for (i = 0; i < composemap->size; i++) */ @@ -747,9 +747,9 @@ static void print_composemap(osk_composenode *composemap, char * sp) /* printf("childs %d ==> ", composemap->childs[i]->size); */ /* if (composemap->childs[i]->size == 0) */ - /* printf("result %ls\n", composemap->childs[i]->result); */ + /* printf("result %ls\n", composemap->childs[i]->result); */ /* else */ - /* print_composemap(composemap->childs[i], space); */ + /* print_composemap(composemap->childs[i], space); */ /* // free(space); */ /* } */ } @@ -757,57 +757,55 @@ static void print_composemap(osk_composenode *composemap, char * sp) /* This parses the contents of keysymdef.h from the source of xorg. Therefore, if somebody wants to provide custom keysymdefs, he has to follow its syntax. */ -static void load_keysymdefs(osk_layout *layout, char * keysymdefs_name) +static void load_keysymdefs(osk_layout * layout, char *keysymdefs_name) { int i; char *filename; char *line; - FILE * fi; + FILE *fi; filename = malloc(sizeof(char) * 255); /* Try full path */ fi = fopen(keysymdefs_name, "r"); if (fi == NULL) - { - /* Try with DATA_PREFIX */ - - snprintf(filename, 255, "%sosk/%s", DATA_PREFIX, keysymdefs_name); - fi = fopen(filename, "r"); - if (fi == NULL) { - printf("Can't open either %s nor %s\n", keysymdefs_name, filename); - layout->keysymdefs = NULL; - free(filename); - return; + /* Try with DATA_PREFIX */ + + snprintf(filename, 255, "%sosk/%s", DATA_PREFIX, keysymdefs_name); + fi = fopen(filename, "r"); + if (fi == NULL) + { + printf("Can't open either %s nor %s\n", keysymdefs_name, filename); + layout->keysymdefs = NULL; + free(filename); + return; + } } - } free(filename); layout->keysymdefs = malloc(sizeof(keysymdefs)); layout->keysymdefs[0].unicode = 0; i = 0; - line = malloc(1024*sizeof(wchar_t)); + line = malloc(1024 * sizeof(wchar_t)); while (!feof(fi)) - { - fgets(line, 1023, fi); - if (strncmp("#define XK_", line, 11) != 0) - continue; + { + 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); @@ -826,26 +824,26 @@ static void load_keysymdefs(osk_layout *layout, char * keysymdefs_name) /* } */ /* Returns the x keysym corresponding to a mnemonic string */ -static int mnemo2keysym(char * mnemo, on_screen_keyboard * keyboard) +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? */ - if(sscanf(mnemo, "U%x", &i)) - return(i | 0x01000000); + if (sscanf(mnemo, "U%x", &i)) + return (i | 0x01000000); /* Or maybe mnemo is already a keysym? */ if (sscanf(mnemo, "0x%x", &i)) - return(i); + return (i); - return(0); + return (0); } /* Returns the unicode value of a x keysym if any, otherwise returns 0 */ @@ -862,10 +860,9 @@ 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 */ if ((keysym & 0xff000000) == 0x01000000) return keysym & 0x00ffffff; @@ -874,9 +871,9 @@ static int keysym2unicode(int keysym, on_screen_keyboard * keyboard) for (i = 0; i < keyboard->layout->sizeofkeysymdefs; i++) if (keysym == keyboard->layout->keysymdefs[i].keysym) - return(keyboard->layout->keysymdefs[i].unicode); + return (keyboard->layout->keysymdefs[i].unicode); - return(keysym); + return (keysym); } @@ -886,60 +883,60 @@ static void get_composed_keysym(on_screen_keyboard * keyboard, osk_composenode * 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 (!composenode) + { + 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 (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 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; + } + } } - } /* 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; - } - else /* reset */ - { - keyboard->composing = keyboard->layout->composemap; - if(keyboard->composed) - free(keyboard->composed); - keyboard->composed = NULL; - 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; + } } @@ -952,14 +949,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; } @@ -987,66 +984,66 @@ static int is_blank_or_comment(char *line) /* Fixme: Is it safe to supose that if a font is loaded at one size, it will be loaded at any size? */ /* Fixme: sizes should be dynamically adapted to the button size */ /* Fixme: starting a layout with one font causes all other layouts be in that font */ -static void keybd_prepare(on_screen_keyboard *keyboard) +static void keybd_prepare(on_screen_keyboard * keyboard) { - char * fontname; + char *fontname; fontname = malloc(sizeof(char) * 255); - if (keyboard->osk_fonty == NULL) { - if (keyboard->layout->fontpath) + if (keyboard->osk_fonty == NULL) { - /* First try if it is an absolute path */ - keyboard->osk_fonty = TTF_OpenFont( keyboard->layout->fontpath, 12 ); - 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, 12 ); - if (keyboard->osk_fonty == NULL) + if (keyboard->layout->fontpath) { - /* Perhaps it is relative to DATA_PREFIX only? */ - snprintf(fontname, 255, "%s/%s", DATA_PREFIX, keyboard->layout->fontpath); - keyboard->osk_fonty = TTF_OpenFont( fontname, 12 ); - 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, 12 ); - } + /* First try if it is an absolute path */ + keyboard->osk_fonty = TTF_OpenFont(keyboard->layout->fontpath, 12); + 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, 12); + 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, 12); + 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, 12); + } + } + } } - } - } - if (keyboard->osk_fonty == NULL) - { - /* Going with the default */ - sprintf(fontname, "%s/fonts/FreeSansBold.ttf", DATA_PREFIX); - keyboard->osk_fonty = TTF_OpenFont( fontname, 12 ); - } + if (keyboard->osk_fonty == NULL) + { + /* Going with the default */ + sprintf(fontname, "%s/fonts/FreeSansBold.ttf", DATA_PREFIX); + keyboard->osk_fonty = TTF_OpenFont(fontname, 12); + } + + 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); + } - 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; - - offset.x = x; - offset.y = y; + SDL_Rect offset; - SDL_BlitSurface( source, clip, destination, &offset ); + offset.x = x; + offset.y = y; + + SDL_BlitSurface(source, clip, destination, &offset); } @@ -1075,10 +1072,10 @@ static void apply_surface (int x, int y, SDL_Surface *source, SDL_Surface *desti /* } */ /* Stretches a button from the middle, keeping the extrems intact */ -static SDL_Surface * stretch_surface(SDL_Surface * orig, int width) +static SDL_Surface *stretch_surface(SDL_Surface * orig, int width) { int i; - SDL_Surface * dest; + SDL_Surface *dest; SDL_Rect rect; SDL_Rect orig_rect; @@ -1087,80 +1084,80 @@ static SDL_Surface * stretch_surface(SDL_Surface * orig, int width) orig_rect.w = 1; orig_rect.h = orig->h; - dest = SDL_CreateRGBSurface(orig->flags, - width, - orig->h, - orig->format->BitsPerPixel, - orig->format->Rmask, - orig->format->Gmask, - orig->format->Bmask, 0); + dest = SDL_CreateRGBSurface(orig->flags, + width, + orig->h, + orig->format->BitsPerPixel, + 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.w = 1; - for (i = orig->w / 2; i < width - orig->w / 2; i++) { - rect.x = i; - SDL_BlitSurface(orig, &orig_rect, 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); + } } - } 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.x = dest->w - i; - orig_rect.x = orig->w - i; - SDL_BlitSurface(orig, &orig_rect, dest, &rect); + 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); + } } - } return dest; } /* Draws the keyboard surface */ -static void draw_keyboard(on_screen_keyboard *keyboard) +static void draw_keyboard(on_screen_keyboard * keyboard) { int i, j; int key_height, accumulated_width, accumulated_height; float key_width; + key_width = keyboard->button_up->w; key_height = keyboard->button_up->h; accumulated_height = 0; for (j = 0; j < keyboard->layout->height; j++) - { - accumulated_width = 0; - for (i = 0; i < keyboard->layout->width; i++) { - if (keyboard->layout->keys[j][i].width) - { + accumulated_width = 0; + for (i = 0; i < keyboard->layout->width; i++) + { + 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); + draw_key(keyboard->layout->keys[j][i], keyboard, 0); + } + accumulated_width += (keyboard->layout->keys[j][i].width * key_width); + } + accumulated_height += key_height; } - accumulated_height += key_height; - } /* draw_key(keyboard->keymodifiers.shift, keyboard, 0); */ /* draw_key(keyboard->keymodifiers.altgr, keyboard, 0); */ @@ -1178,29 +1175,29 @@ static void draw_key(osk_key key, on_screen_keyboard * keyboard, int hot) text = malloc(sizeof(char) * 255); - snprintf(text, 6,"%s", key.plain_label); + 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 (strncmp("NULL", text, 4) != 0 && key.keycode != 0) { - 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); - } + 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 - skey = stretch_surface(keyboard->button_up, key.width * keyboard->button_up->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 + 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); @@ -1213,7 +1210,7 @@ static void draw_key(osk_key key, on_screen_keyboard * keyboard, int hot) /* FIXME: TODO draw top and bottom_right (altgr) labels */ -static void label_key(osk_key key, on_screen_keyboard *keyboard) +static void label_key(osk_key key, on_screen_keyboard * keyboard) { SDL_Surface *messager; int modstate; @@ -1227,164 +1224,163 @@ static void label_key(osk_key key, on_screen_keyboard *keyboard) /* FIXME There MUST be a simpler way to do this. Pere 2011/8/3 */ /* First the plain ones */ if (modstate == KMOD_NONE || (modstate == (KMOD_NONE | KMOD_LALT))) - text=strdup(key.plain_label); + 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); + 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); + { + 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); - } + { + 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); - } + if (strncmp("DELETE", text, 6) == 0) + { + 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); - } + else if (strncmp("TAB", text, 3) == 0) + { + 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); - } + else if (strncmp("ENTER", text, 5) == 0) + { + 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); - } + else if (strncmp("CAPSLOCK", text, 8) == 0) + { + 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); - } + else if (strncmp("SHIFT", text, 5) == 0) + { + 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); + else if (strncmp("SPACE", text, 5) != 0 && strncmp("NULL", text, 4) != 0) + { + 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); } /* Searches the key corresponding to coordinates */ -static osk_key * find_key(on_screen_keyboard * keyboard, int x, int y) +static osk_key *find_key(on_screen_keyboard * keyboard, int x, int y) { int i, j; osk_key *key; key = NULL; - for (j = 0; j 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; - } - } + 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; + } + } return NULL; } /* Copies orig to dest or sets dest to defaults if orig is NULL. if firstime is setted, don't frees the strings as there aren't. */ -static void set_key(osk_key *orig, osk_key *dest, int firsttime) +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) +static char *find_keysym(osk_key key, on_screen_keyboard * keyboard) { int keycode; char *keysym; @@ -1403,60 +1399,60 @@ 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; + if (modstate & KMOD_CAPS) + { + if (key.shiftcaps) + keysym = keysyms.altgr; + else + keysym = keysyms.shiftaltgr; + } else - keysym = keysyms.shiftaltgr; + { + 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; - } + { + 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); + 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; @@ -1464,81 +1460,80 @@ 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) { - keyboard->modifiers = mod & 0xFFF0; - key->stick = 0; - keyboard->kmdf.shift->stick = 0; + 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; } - 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); - - 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) - { - 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; + 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 0; - } + + /* 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) + { + 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; + } 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; - } + if (mod & KMOD_CAPS) + { + keyboard->modifiers = mod & 0x0FFF; + key->stick = 0; + } + else + { + keyboard->modifiers = mod | KMOD_CAPS; + key->stick = 1; + } - return 1; - } + return 1; + } if (mod & KMOD_CAPS) - { - keyboard->modifiers = KMOD_CAPS; - } + { + keyboard->modifiers = KMOD_CAPS; + } else keyboard->modifiers = KMOD_NONE; - if(keyboard->kmdf.shift) + if (keyboard->kmdf.shift) keyboard->kmdf.shift->stick = 0; - if(keyboard->kmdf.altgr) + if (keyboard->kmdf.altgr) keyboard->kmdf.altgr->stick = 0; return 0; @@ -1546,44 +1541,44 @@ static int handle_keymods(char * keysym, osk_key * key, on_screen_keyboard *keyb /* set_dead_sticks and clear_dead_sticks deals with the persistence of the keys that are still affecting other key presses. */ -static void set_dead_sticks(osk_key *key, on_screen_keyboard *keyboard) +static void set_dead_sticks(osk_key * key, on_screen_keyboard * keyboard) { - key->stick= 1; - if(!keyboard->kmdf.dead) + key->stick = 1; + if (!keyboard->kmdf.dead) keyboard->kmdf.dead = key; - else if(!keyboard->kmdf.dead2) + else if (!keyboard->kmdf.dead2) keyboard->kmdf.dead2 = key; - else if(!keyboard->kmdf.dead3) + else if (!keyboard->kmdf.dead3) keyboard->kmdf.dead3 = key; - else if(!keyboard->kmdf.dead4) + else if (!keyboard->kmdf.dead4) keyboard->kmdf.dead4 = key; } -static void clear_dead_sticks(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; - } - if(keyboard->kmdf.dead2) - { - keyboard->kmdf.dead2->stick = 0; - keyboard->kmdf.dead2 = NULL; - } - if(keyboard->kmdf.dead3) - { - keyboard->kmdf.dead3->stick = 0; - keyboard->kmdf.dead3 = NULL; - } - if(keyboard->kmdf.dead4) - { - keyboard->kmdf.dead4->stick = 0; - keyboard->kmdf.dead4 = NULL; - } + if (keyboard->kmdf.dead) + { + keyboard->kmdf.dead->stick = 0; + keyboard->kmdf.dead = NULL; + } + if (keyboard->kmdf.dead2) + { + keyboard->kmdf.dead2->stick = 0; + keyboard->kmdf.dead2 = NULL; + } + if (keyboard->kmdf.dead3) + { + keyboard->kmdf.dead3->stick = 0; + keyboard->kmdf.dead3 = NULL; + } + if (keyboard->kmdf.dead4) + { + keyboard->kmdf.dead4->stick = 0; + keyboard->kmdf.dead4 = NULL; + } } -struct osk_keyboard * osk_clicked(on_screen_keyboard *keyboard, int x, int y) +struct osk_keyboard *osk_clicked(on_screen_keyboard * keyboard, int x, int y) { int i; osk_key *key; @@ -1592,7 +1587,7 @@ struct osk_keyboard * osk_clicked(on_screen_keyboard *keyboard, int x, int y) char *name, *aux_name, *aux_list, *aux_list_ptr; wchar_t *wkeysym; wchar_t *ks; - on_screen_keyboard * new_keyboard; + on_screen_keyboard *new_keyboard; #ifdef DEBUG printf("list: %s\n", keyboard->keyboard_list); @@ -1605,226 +1600,229 @@ 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) { - if (keyboard->disable_change) - { - // free(key); - return(keyboard); - } + /* 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; + aux_list = strdup(keyboard->keyboard_list); + aux_list_ptr = aux_list; #ifdef DEBUG - printf("auxlist: %s\n", aux_list); - printf("kn %s\n", keyboard->name); + 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 (key->keycode == 1) + { + for (i = 0;; i++, aux_list = NULL) + { + name = strtok(aux_list, " \n\r\t"); - if (i == 0) - aux_name = name; + 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; - } - } - } + 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->surface, keyboard->button_up, keyboard->button_down, keyboard->button_off, + keyboard->button_nav, keyboard->button_hold, keyboard->oskdel, keyboard->osktab, + keyboard->oskenter, keyboard->oskcapslock, keyboard->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 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 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'; + } + 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'; + } + else if (wcsncmp(L"BackSpace", ks, 9) == 0) + { + event.key.keysym.sym = SDLK_BACKSPACE; + event.text.text[0] = '\b'; + } + else if (wcsncmp(L"NoSymbol", ks, 8) == 0) + return (keyboard); + + else + // printf("kcomposed %ls\n", *keyboard->composed); + if (keyboard->composed_type == 1) + wcstombs(event.text.text, keyboard->composed, 16); + // event.text.text = *keyboard->composed; + else + snprintf(event.text.text, 16, "%lc", keysym2unicode(mnemo2keysym(mnemo, keyboard), keyboard)); + //event.text.text = keysym2unicode(mnemo2keysym(mnemo, keyboard), keyboard); + + clear_dead_sticks(keyboard); + event.type = SDL_KEYDOWN; + SDL_PushEvent(&event); + free(mnemo); + } 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->surface, keyboard->button_up, keyboard->button_down, keyboard->button_off, keyboard->button_nav, keyboard->button_hold, keyboard->oskdel, keyboard->osktab, keyboard->oskenter, keyboard->oskcapslock, keyboard->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); - + { + if (keyboard->composing == keyboard->layout->composemap) + { #ifdef DEBUG - printf("wkeysym %ls %i\n\n", wkeysym, (int)wcslen(wkeysym)); + printf("compose sequence resetted\n"); #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; - + 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 DEBUG - printf("keysym found %ls\n", ks); + printf("still composing\n"); #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'; - } - 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'; - } - else if (wcsncmp(L"BackSpace", ks, 9) == 0) - { - event.key.keysym.sym = SDLK_BACKSPACE; - event.text.text[0] = '\b'; - } - else if (wcsncmp(L"NoSymbol", ks, 8) == 0) - return(keyboard); - - else - // printf("kcomposed %ls\n", *keyboard->composed); - if (keyboard->composed_type == 1) - wcstombs(event.text.text, keyboard->composed, 16); - // event.text.text = *keyboard->composed; - else - snprintf(event.text.text, 16, "%lc", keysym2unicode(mnemo2keysym(mnemo, keyboard), keyboard)); - //event.text.text = keysym2unicode(mnemo2keysym(mnemo, keyboard), keyboard); - - clear_dead_sticks(keyboard); - event.type = SDL_KEYDOWN; - SDL_PushEvent(&event); - free(mnemo); + set_dead_sticks(key, keyboard); + /* Fixme: Would be nice if we can highlight next available-to-compose keys, but how? */ + } + } + free(wkeysym); } - else - { - if (keyboard->composing == keyboard->layout->composemap) - { -#ifdef 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 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); + return (keyboard); } -void osk_released(on_screen_keyboard *keyboard) +void osk_released(on_screen_keyboard * keyboard) { osk_key *key; + 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); } -static void free_keymap(osk_keymap *keymap) +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); - } + + 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); + } free(keymap); } 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]); - } + + for (i = 0; i < composenode->size; i++) + { + free_composemap(composenode->childs[i]); + free(composenode->childs[i]); + } if (composenode->result) free(composenode->result); else @@ -1834,7 +1832,7 @@ static void free_composemap(osk_composenode * composenode) free(composenode->keysym); } -static void free_keysymdefs(keysymdefs *ksd, int size) +static void free_keysymdefs(keysymdefs * ksd, int size) { int i; @@ -1842,30 +1840,30 @@ static void free_keysymdefs(keysymdefs *ksd, int size) free(ksd[i].mnemo); } -static void free_keys(osk_layout *layout) +static void free_keys(osk_layout * layout) { int i, j; for (j = 0; j < layout->height; j++) - { - 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); + 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); + } + free(layout->keys[j]); } - free(layout->keys[j]); - } free(layout->keys); } -static void free_layout(osk_layout *layout) +static void free_layout(osk_layout * layout) { if (layout->name != NULL) free(layout->name); @@ -1881,15 +1879,15 @@ static void free_layout(osk_layout *layout) free(layout); } -void osk_free(on_screen_keyboard *keyboard) +void osk_free(on_screen_keyboard * keyboard) { free(keyboard->name); free_layout(keyboard->layout); - if(keyboard->composed) + if (keyboard->composed) free(keyboard->composed); - if(keyboard->last_key_pressed) + if (keyboard->last_key_pressed) free(keyboard->last_key_pressed); - if(keyboard->keyboard_list) + if (keyboard->keyboard_list) free(keyboard->keyboard_list); SDL_FreeSurface(keyboard->surface); set_key(NULL, &keyboard->keymodifiers.shift, 0); @@ -1898,7 +1896,7 @@ void osk_free(on_screen_keyboard *keyboard) set_key(NULL, &keyboard->keymodifiers.dead, 0); if (keyboard->osk_fonty != NULL) TTF_CloseFont(keyboard->osk_fonty); - + free(keyboard); } @@ -1909,7 +1907,7 @@ void osk_free(on_screen_keyboard *keyboard) /* int i; */ /* if (key_board != NULL) */ /* SDL_FreeSurface(key_board); */ - + /* key_board = SDL_CreateRGBSurface(canvas->flags, */ /* key_width * 19, */ /* key_height * 3, */ @@ -1981,7 +1979,7 @@ void osk_free(on_screen_keyboard *keyboard) /* SDL_Event event; */ /* dest.x = x; */ /* dest.y = y; */ - + /* // Check whether the button should be hot */ /* if (regionhit(x, y, 24, 24)) */ /* { */ @@ -2056,13 +2054,12 @@ void osk_free(on_screen_keyboard *keyboard) /* if (enter_flag == 0) */ /* { */ - /* event.key.type=SDL_KEYDOWN; */ - /* SDL_PushEvent(&event); */ - /* event.key.type=SDL_KEYUP; */ - /* SDL_PushEvent(&event); */ + /* event.key.type=SDL_KEYDOWN; */ + /* SDL_PushEvent(&event); */ + /* event.key.type=SDL_KEYUP; */ + /* SDL_PushEvent(&event); */ + - /* } */ /* } */ /* } */ - diff --git a/src/onscreen_keyboard.h b/src/onscreen_keyboard.h index 516e13e5d..717b7f06e 100644 --- a/src/onscreen_keyboard.h +++ b/src/onscreen_keyboard.h @@ -26,41 +26,41 @@ KMOD_ALT An Alt key is down typedef struct osk_keymap { - int keycode; - int disable_caps; /* If caps lock should affect this key */ - char * plain; /* The default Xkeysym for the keycode */ - char * caps; /* If CapsLock or Shift + key */ - char * altgr; /* If AltGr + key */ - char * shiftaltgr; /* If AltGr + Shift + key */ + int keycode; + int disable_caps; /* If caps lock should affect this key */ + char *plain; /* The default Xkeysym for the keycode */ + char *caps; /* If CapsLock or Shift + key */ + char *altgr; /* If AltGr + key */ + char *shiftaltgr; /* If AltGr + Shift + key */ } osk_keymap; typedef struct osk_key { - int keycode; /* The code associated to this key. If 0, then it is an empty key. */ + int keycode; /* The code associated to this key. If 0, then it is an empty key. */ int row; int x; int y; - float width; /* The width in buttons */ - char *plain_label; /* The text that will show the key */ - char *top_label; /* The text that will show the key above the plain label. */ - char *altgr_label; /* The text that will show the key at the right of the plain label */ - char *shift_altgr_label; /* The text that will show the key when shift and altgr are activated */ - int shiftcaps; /* If the value of the key should be shifted when capslock is active */ - int stick; /* If the key currently affects the others */ + float width; /* The width in buttons */ + char *plain_label; /* The text that will show the key */ + char *top_label; /* The text that will show the key above the plain label. */ + char *altgr_label; /* The text that will show the key at the right of the plain label */ + char *shift_altgr_label; /* The text that will show the key when shift and altgr are activated */ + int shiftcaps; /* If the value of the key should be shifted when capslock is active */ + int stick; /* If the key currently affects the others */ } osk_key; typedef struct osk_composenode { - wchar_t * keysym; - wchar_t * result; - int size; /* How many childs are there. */ - struct osk_composenode ** childs; + wchar_t *keysym; + wchar_t *result; + int size; /* How many childs are there. */ + struct osk_composenode **childs; // struct osk_composenode **parent; } osk_composenode; typedef struct keysymdefs { - char * mnemo; + char *mnemo; int keysym; int unicode; } keysymdefs; @@ -71,11 +71,11 @@ typedef struct osk_layout int *rows; int width; int height; - char * fontpath; + char *fontpath; osk_key **keys; osk_keymap *keymap; - osk_composenode * composemap; - keysymdefs * keysymdefs; + osk_composenode *composemap; + keysymdefs *keysymdefs; unsigned int sizeofkeysymdefs; SDL_Color bgcolor; SDL_Color fgcolor; @@ -91,56 +91,61 @@ typedef struct osk_keymodifiers typedef struct osk_kmdf { - osk_key * shift; - osk_key * altgr; - osk_key * compose; - osk_key * dead; - osk_key * dead2; - osk_key * dead3; - osk_key * dead4; + osk_key *shift; + osk_key *altgr; + osk_key *compose; + osk_key *dead; + osk_key *dead2; + osk_key *dead3; + osk_key *dead4; } osk_kmdf; 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 keyboard */ - SDL_Surface *button_up; /* The surfaces containing the buttons */ + 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 keyboard */ + SDL_Surface *button_up; /* The surfaces containing the buttons */ SDL_Surface *button_down; SDL_Surface *button_off; SDL_Surface *button_nav; SDL_Surface *button_hold; - SDL_Surface *oskdel; /* The surfaces containing some symbols for the buttons, delete arrow */ - SDL_Surface *osktab; /* Tab arrows */ - SDL_Surface *oskenter; /* Return hook/arrow */ - SDL_Surface *oskcapslock; /* CapsLock */ - SDL_Surface *oskshift; /* Shift */ - int changed; /* If the surface has been modified (painted) */ - SDL_Rect rect; /* The rectangle that has changed */ - int recreated; /* If the surface has been deleted and newly created */ - int modifiers; /* The state of Alt, CTRL, Shift, CapsLock, AltGr keys */ - osk_keymodifiers keymodifiers; /* A shortcurt to find the place of the pressed modifiers */ + SDL_Surface *oskdel; /* The surfaces containing some symbols for the buttons, delete arrow */ + SDL_Surface *osktab; /* Tab arrows */ + SDL_Surface *oskenter; /* Return hook/arrow */ + SDL_Surface *oskcapslock; /* CapsLock */ + SDL_Surface *oskshift; /* Shift */ + int changed; /* If the surface has been modified (painted) */ + SDL_Rect rect; /* The rectangle that has changed */ + int recreated; /* If the surface has been deleted and newly created */ + int modifiers; /* The state of Alt, CTRL, Shift, CapsLock, AltGr keys */ + osk_keymodifiers keymodifiers; /* A shortcurt to find the place of the pressed modifiers */ osk_kmdf kmdf; - osk_layout *layout; /* The layout struct */ - char *layout_name[256]; /* The layout name */ - TTF_Font * osk_fonty; /* Font */ - int disable_change; /* If true, stay with the first layout found */ - wchar_t * key[256]; /* The text of the key */ - int keycode; /* The unicode code corresponding to the key */ - wchar_t * composed; /* The unicode char found after a sequence of key presses */ - 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 */ + osk_layout *layout; /* The layout struct */ + char *layout_name[256]; /* The layout name */ + TTF_Font *osk_fonty; /* Font */ + int disable_change; /* If true, stay with the first layout found */ + wchar_t *key[256]; /* The text of the key */ + int keycode; /* The unicode code corresponding to the key */ + wchar_t *composed; /* The unicode char found after a sequence of key presses */ + 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 */ } on_screen_keyboard; -struct osk_keyboard *osk_create(char *layout_name, SDL_Surface *canvas, SDL_Surface *button_up, SDL_Surface *button_down, SDL_Surface *button_off, SDL_Surface *button_nav, SDL_Surface *button_hold, SDL_Surface *oskdel, SDL_Surface *osktab, SDL_Surface *oskenter, SDL_Surface *oskcapslock, SDL_Surface *oskshift, int disable_change); +struct osk_keyboard *osk_create(char *layout_name, SDL_Surface * canvas, SDL_Surface * button_up, + SDL_Surface * button_down, SDL_Surface * button_off, SDL_Surface * button_nav, + SDL_Surface * button_hold, SDL_Surface * oskdel, SDL_Surface * osktab, + SDL_Surface * oskenter, SDL_Surface * oskcapslock, SDL_Surface * 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, char *layout_keycodes); -void osk_reset(on_screen_keyboard *osk); -struct osk_keyboard * osk_clicked(on_screen_keyboard *keyboard, int x, int y); -void osk_released(on_screen_keyboard *osk); -void osk_hover(on_screen_keyboard *keyboard, int x, int y); -void osk_free(on_screen_keyboard *osk); -void osk_change_layout(on_screen_keyboard *osk); +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); +void osk_released(on_screen_keyboard * osk); +void osk_hover(on_screen_keyboard * keyboard, int x, int y); +void osk_free(on_screen_keyboard * osk); +void osk_change_layout(on_screen_keyboard * osk); diff --git a/src/parse.h b/src/parse.h index 3eb7dcd1d..fd124f991 100644 --- a/src/parse.h +++ b/src/parse.h @@ -86,4 +86,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 a3737d6e9..5602b2fd7 100644 --- a/src/pixels.c +++ b/src/pixels.c @@ -37,21 +37,19 @@ 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: */ @@ -60,21 +58,19 @@ 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: */ @@ -83,33 +79,31 @@ 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))) - { - // 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) + if (likely(likely((unsigned)x < (unsigned)surface->w) && likely((unsigned)y < (unsigned)surface->h))) { - 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; - } + // 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; + } + + } } /* Draw a single pixel into the surface: */ @@ -118,21 +112,19 @@ 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: */ @@ -141,17 +133,17 @@ 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 in question: */ - p = (Uint8 *) (((Uint8 *) surface->pixels) + /* Start at top of RAM */ - (y * surface->pitch) + /* Go down Y lines */ - x); /* Go in X pixels */ + p = (Uint8 *) (((Uint8 *) surface->pixels) + /* Start at top of RAM */ + (y * surface->pitch) + /* Go down Y lines */ + x); /* Go in X pixels */ /* Return the correctly-sized piece of data containing the @@ -167,17 +159,17 @@ 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 in question: */ - p = (Uint8 *) (((Uint8 *) surface->pixels) + /* Start at top of RAM */ - (y * surface->pitch) + /* Go down Y lines */ - (x * 2)); /* Go in X pixels */ + p = (Uint8 *) (((Uint8 *) surface->pixels) + /* Start at top of RAM */ + (y * surface->pitch) + /* Go down Y lines */ + (x * 2)); /* Go in X pixels */ /* Return the correctly-sized piece of data containing the @@ -194,17 +186,17 @@ 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 in question: */ - p = (Uint8 *) (((Uint8 *) surface->pixels) + /* Start at top of RAM */ - (y * surface->pitch) + /* Go down Y lines */ - (x * 3)); /* Go in X pixels */ + p = (Uint8 *) (((Uint8 *) surface->pixels) + /* Start at top of RAM */ + (y * surface->pitch) + /* Go down Y lines */ + (x * 3)); /* Go in X pixels */ /* Return the correctly-sized piece of data containing the @@ -227,24 +219,24 @@ 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 in question: */ - p = (Uint8 *) (((Uint8 *) surface->pixels) + /* Start at top of RAM */ - (y * surface->pitch) + /* Go down Y lines */ - (x * 4)); /* Go in X pixels */ + p = (Uint8 *) (((Uint8 *) surface->pixels) + /* Start at top of RAM */ + (y * surface->pitch) + /* Go down Y lines */ + (x * 4)); /* Go in X pixels */ /* Return the correctly-sized piece of data containing the * pixel's value (an 8-bit palette value, or a 16-, 24- or 32-bit * RGB value) */ - return *(Uint32 *) p; // 32-bit display + return *(Uint32 *) p; // 32-bit display } void (*putpixels[]) (SDL_Surface *, int, int, Uint32) = diff --git a/src/playsound.c b/src/playsound.c index 4238bdabf..3bed4d732 100644 --- a/src/playsound.c +++ b/src/playsound.c @@ -33,55 +33,54 @@ int mute; int use_sound = 1; static int old_sound[4] = { -1, -1, -1, -1 }; -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) - { - if (override || !Mix_Playing(chan)) { - Mix_PlayChannel(chan, sounds[s], 0); + if (override || !Mix_Playing(chan)) + { + Mix_PlayChannel(chan, sounds[s], 0); - old_sound[chan] = s; + 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 (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); + } + + + + Mix_SetPanning(chan, left, (255 - dist) - left); + } } - - 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 (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); - } - - - - Mix_SetPanning(chan, left, (255 - dist) - left); - } - } #endif } diff --git a/src/playsound.h b/src/playsound.h index fe17dd43b..d6f39a822 100644 --- a/src/playsound.h +++ b/src/playsound.h @@ -37,7 +37,6 @@ extern Mix_Chunk *sounds[NUM_SOUNDS]; extern int mute, use_sound; -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 b202bf40c..39d3f441c 100644 --- a/src/postscript_print.c +++ b/src/postscript_print.c @@ -62,7 +62,7 @@ #include "pixels.h" -#define MARGIN 36 /* Margin to put around image, in points (inch/72) (36pt = 0.5") */ +#define MARGIN 36 /* Margin to put around image, in points (inch/72) (36pt = 0.5") */ #define my_min(x,y) ((x < y) ? (x) : (y)) @@ -78,12 +78,9 @@ static int f2dec(float f) /* 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; + const struct paper *ppr; int img_w = surf->w; int img_h = surf->h; int r_img_w, r_img_h; @@ -94,8 +91,8 @@ int do_ps_save(FILE * fi, int plane; 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; @@ -104,40 +101,40 @@ int do_ps_save(FILE * fi, /* Determine paper size: */ - paperinit(); // FIXME: Should we do this at startup? -bjk 2007.06.25 + paperinit(); // FIXME: Should we do this at startup? -bjk 2007.06.25 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) { - /* No setting, env. var. or /etc/ file; use the default! */ + /* 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 = defaultpapername(); + pprsize = systempapername(); + + if (pprsize == NULL) + { + /* No setting, env. var. or /etc/ file; use the default! */ + + pprsize = defaultpapername(); #ifdef DEBUG - printf("Using default paper\n"); + printf("Using default paper\n"); #endif - } + } #ifdef DEBUG - else - { - printf("Using system paper\n"); - } + else + { + printf("Using system paper\n"); + } #endif - } + } #ifdef DEBUG else - { - printf("Using user paper\n"); - } + { + printf("Using user paper\n"); + } #endif - + #ifdef DEBUG printf("Using paper size: %s\n", pprsize); #endif @@ -151,28 +148,26 @@ 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 + paperdone(); // FIXME: Should we do this at quit? -bjk 2007.06.25 /* 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); @@ -183,15 +178,13 @@ 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 @@ -206,7 +199,7 @@ int do_ps_save(FILE * fi, /* Begin PostScript output with some useful meta info in comments: */ - fprintf(fi, "%%!PS-Adobe-2.0 EPSF-2.0\n"); // we need LanguageLevel2 for color + fprintf(fi, "%%!PS-Adobe-2.0 EPSF-2.0\n"); // we need LanguageLevel2 for color fprintf(fi, "%%%%Title: (%s)\n", fname); @@ -217,8 +210,7 @@ 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"); @@ -237,24 +229,20 @@ 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)); + 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)); /* 'scale' modifies the unit lengths independently along the current - x and y axes, leaving the origin location and the orientation of the - axes unchanged. */ + 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) @@ -274,23 +262,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 (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) + for (plane = 0; plane < 3; plane++) { - fprintf(fi, "\n"); - cur_line_len = 0; + 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; + } + } } - } } - } fprintf(fi, "\n"); fprintf(fi, "grestore\n"); @@ -299,16 +287,16 @@ 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; - child_pid = pclose(fi); + child_pid = pclose(fi); /* debug */ /* @@ -316,15 +304,18 @@ int do_ps_save(FILE * fi, printf("errno = %d\n", errno); fflush(stdout); */ - if (child_pid < 0 || (errno != 0 && errno != EAGAIN)) { /* FIXME: This right? */ - return 0; - } else if (child_pid == 0) { - return 1; - } + 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); + do + { + w = waitpid(child_pid, &status, 0); /* debug */ /* @@ -339,15 +330,14 @@ int do_ps_save(FILE * fi, printf("continued\n"); } */ + } + while (w != -1 && !WIFEXITED(status) && !WIFSIGNALED(status)); + + if (WIFEXITED(status) && WEXITSTATUS(status) != 0) /* Not happy exit */ + return 0; + + return 1; } - 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 58f0f3bd2..8bf2ecc82 100644 --- a/src/postscript_print.h +++ b/src/postscript_print.h @@ -79,12 +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 #endif /* POSTSCRIPT_PRINT_H */ - diff --git a/src/progressbar.c b/src/progressbar.c index c049b64f7..44c220ebd 100644 --- a/src/progressbar.c +++ b/src/progressbar.c @@ -34,7 +34,7 @@ SDL_Surface *img_progress; 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) { SDL_Rect dest, src, r; int x; @@ -45,39 +45,39 @@ void show_progress_bar_(SDL_Surface * screen, SDL_Texture *texture, SDL_Renderer return; newtime = SDL_GetTicks(); - if (newtime > oldtime + 15) // trying not to eat some serious CPU time! - { - for (x = 0; x < screen->w; x = x + 65) + if (newtime > oldtime + 15) // trying not to eat some serious CPU time! { - src.x = 65 - (prog_bar_ctr % 65); - src.y = 0; - src.w = 65; - src.h = 24; + 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; - dest.x = x; - dest.y = screen->h - 24; + dest.x = x; + dest.y = screen->h - 24; - SDL_BlitSurface(img_progress, &src, screen, &dest); + SDL_BlitSurface(img_progress, &src, screen, &dest); + } + + prog_bar_ctr++; + + // FIXME SDL2 + // SDL_UpdateRect(screen, 0, screen->h - 24, screen->w, 24); + 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); + + // NOTE docs says one should clear the renderer, however this means a refresh of the whole thing. + // SDL_RenderClear(renderer); + // SDL_RenderCopy(renderer, texture, NULL, NULL); + SDL_RenderCopy(renderer, texture, &r, &r); + + SDL_RenderPresent(renderer); } - - prog_bar_ctr++; - - // FIXME SDL2 - // SDL_UpdateRect(screen, 0, screen->h - 24, screen->w, 24); - 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); - - // NOTE docs says one should clear the renderer, however this means a refresh of the whole thing. - // SDL_RenderClear(renderer); - // SDL_RenderCopy(renderer, texture, NULL, NULL); - SDL_RenderCopy(renderer, texture, &r, &r); - - SDL_RenderPresent(renderer); - } oldtime = newtime; diff --git a/src/rgblinear.c b/src/rgblinear.c index d0bb299ea..e915e895f 100644 --- a/src/rgblinear.c +++ b/src/rgblinear.c @@ -33,13 +33,14 @@ unsigned char linear_to_sRGB(float linear) { unsigned slot; + 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 7bca5f64f..7fe31ec9e 100644 --- a/src/rgblinear.h +++ b/src/rgblinear.h @@ -305,8 +305,7 @@ 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 17ce470a7..e89f29d6d 100644 --- a/src/shapes.h +++ b/src/shapes.h @@ -62,84 +62,84 @@ enum /* How many sides do they have? */ const int shape_sides[NUM_SHAPES] = { - 4, /* Square */ - 4, /* Square */ - 4, /* Rectangle */ - 4, /* Rectangle */ - 72, /* Circle */ - 72, /* Circle */ - 72, /* Ellipse */ - 72, /* Ellipse */ - 3, /* Triangle */ - 3, /* Triangle */ - 5, /* Pentagon */ - 5, /* Pentagon */ - 4, /* Rhombus */ - 4, /* Rhombus */ - 8, /* Octagon */ - 8, /* Octagon */ - 3, /* 3 points star*/ - 3, /* 3 points star*/ - 4, /* 4 points star*/ - 4, /* 4 points star*/ - 5, /* 5 points star*/ - 5 /* 5 points star*/ + 4, /* Square */ + 4, /* Square */ + 4, /* Rectangle */ + 4, /* Rectangle */ + 72, /* Circle */ + 72, /* Circle */ + 72, /* Ellipse */ + 72, /* Ellipse */ + 3, /* Triangle */ + 3, /* Triangle */ + 5, /* Pentagon */ + 5, /* Pentagon */ + 4, /* Rhombus */ + 4, /* Rhombus */ + 8, /* Octagon */ + 8, /* Octagon */ + 3, /* 3 points star */ + 3, /* 3 points star */ + 4, /* 4 points star */ + 4, /* 4 points star */ + 5, /* 5 points star */ + 5 /* 5 points star */ }; /* Which shapes are 1:1 aspect? */ const int shape_locked[NUM_SHAPES] = { - 1, /* Square */ - 1, /* Square */ - 0, /* Rectangle */ - 0, /* Rectangle */ - 1, /* Circle */ - 1, /* Circle */ - 0, /* Ellipse */ - 0, /* Ellipse */ - 0, /* Triangle */ - 0, /* Triangle */ - 0, /* Pentagon */ - 0, /* Pentagon */ - 0, /* Rhombus */ - 0, /* Rhombus */ - 1, /* Octagon */ - 1, /* Octagon */ - 0, /* 3 points star*/ - 0, /* 3 points star*/ - 0, /* 4 points star*/ - 0, /* 4 points star*/ - 0, /* 5 points star*/ - 0 /* 5 points star*/ + 1, /* Square */ + 1, /* Square */ + 0, /* Rectangle */ + 0, /* Rectangle */ + 1, /* Circle */ + 1, /* Circle */ + 0, /* Ellipse */ + 0, /* Ellipse */ + 0, /* Triangle */ + 0, /* Triangle */ + 0, /* Pentagon */ + 0, /* Pentagon */ + 0, /* Rhombus */ + 0, /* Rhombus */ + 1, /* Octagon */ + 1, /* Octagon */ + 0, /* 3 points star */ + 0, /* 3 points star */ + 0, /* 4 points star */ + 0, /* 4 points star */ + 0, /* 5 points star */ + 0 /* 5 points star */ }; /* Which shapes are filled? */ const int shape_filled[NUM_SHAPES] = { - 0, /* Square */ - 1, /* Square */ - 0, /* Rectangle */ - 1, /* Rectangle */ - 0, /* Circle */ - 1, /* Circle */ - 0, /* Ellipse */ - 1, /* Ellipse */ - 0, /* Triangle */ - 1, /* Triangle */ - 0, /* Pentagon */ - 1, /* Pentagon */ - 0, /* Rhombus */ - 1, /* Rhombus */ - 0, /* Octagon */ - 1, /* Octagon */ - 0, /* 3 points star*/ - 1, /* 3 points star*/ - 0, /* 4 points star*/ - 1, /* 4 points star*/ - 0, /* 5 points star*/ - 1 /* 5 points star*/ + 0, /* Square */ + 1, /* Square */ + 0, /* Rectangle */ + 1, /* Rectangle */ + 0, /* Circle */ + 1, /* Circle */ + 0, /* Ellipse */ + 1, /* Ellipse */ + 0, /* Triangle */ + 1, /* Triangle */ + 0, /* Pentagon */ + 1, /* Pentagon */ + 0, /* Rhombus */ + 1, /* Rhombus */ + 0, /* Octagon */ + 1, /* Octagon */ + 0, /* 3 points star */ + 1, /* 3 points star */ + 0, /* 4 points star */ + 1, /* 4 points star */ + 0, /* 5 points star */ + 1 /* 5 points star */ }; @@ -147,83 +147,83 @@ const int shape_filled[NUM_SHAPES] = { /* Initial angles for shapes: */ const int shape_init_ang[NUM_SHAPES] = { - 45, /* Square */ - 45, /* Square */ - 45, /* Rectangle */ - 45, /* Rectangle */ - 0, /* Circle */ - 0, /* Circle */ - 0, /* Ellipse */ - 0, /* Ellipse */ - 210, /* Triangle */ - 210, /* Triangle */ - 162, /* Pentagon */ - 162, /* Pentagon */ - 0, /* Rhombus */ - 0, /* Rhombus */ - 22, /* Octagon */ - 22, /* Octagon */ - 210, /* 3 points star*/ - 210, /* 3 points star*/ - 0, /* 4 points star*/ - 0, /* 4 points star*/ - 162, /* 5 points star */ - 162 /* 5 points star */ + 45, /* Square */ + 45, /* Square */ + 45, /* Rectangle */ + 45, /* Rectangle */ + 0, /* Circle */ + 0, /* Circle */ + 0, /* Ellipse */ + 0, /* Ellipse */ + 210, /* Triangle */ + 210, /* Triangle */ + 162, /* Pentagon */ + 162, /* Pentagon */ + 0, /* Rhombus */ + 0, /* Rhombus */ + 22, /* Octagon */ + 22, /* Octagon */ + 210, /* 3 points star */ + 210, /* 3 points star */ + 0, /* 4 points star */ + 0, /* 4 points star */ + 162, /* 5 points star */ + 162 /* 5 points star */ }; /* Shapes that don't make sense rotating (e.g., circles): */ const int shape_no_rotate[NUM_SHAPES] = { - 0, /* Square */ - 0, /* Square */ - 0, /* Rectangle */ - 0, /* Rectangle */ - 1, /* Circle */ - 1, /* Circle */ - 0, /* Ellipse */ - 0, /* Ellipse */ - 0, /* Triangle */ - 0, /* Triangle */ - 0, /* Pentagon */ - 0, /* Pentagon */ - 0, /* Rhombus */ - 0, /* Rhombus */ - 0, /* Octagon */ - 0, /* Octagon */ - 0, /* 3 points star */ - 0, /* 3 points star */ - 0, /* 4 points star */ - 0, /* 4 points star */ - 0, /* 5 points star */ - 0 /* 5 points star */ + 0, /* Square */ + 0, /* Square */ + 0, /* Rectangle */ + 0, /* Rectangle */ + 1, /* Circle */ + 1, /* Circle */ + 0, /* Ellipse */ + 0, /* Ellipse */ + 0, /* Triangle */ + 0, /* Triangle */ + 0, /* Pentagon */ + 0, /* Pentagon */ + 0, /* Rhombus */ + 0, /* Rhombus */ + 0, /* Octagon */ + 0, /* Octagon */ + 0, /* 3 points star */ + 0, /* 3 points star */ + 0, /* 4 points star */ + 0, /* 4 points star */ + 0, /* 5 points star */ + 0 /* 5 points star */ }; /* Valley of stars in percent of size */ const int shape_valley[NUM_SHAPES] = { - 100, /* Square */ - 100, /* Square */ - 100, /* Rectangle */ - 100, /* Rectangle */ - 100, /* Circle */ - 100, /* Circle */ - 100, /* Ellipse */ - 100, /* Ellipse */ - 100, /* Triangle */ - 100, /* Triangle */ - 100, /* Pentagon */ - 100, /* Pentagon */ - 100, /* Rhombus */ - 100, /* Rhombus */ - 100, /* Octagon */ - 100, /* Octagon */ - 20, /* 3 points star */ - 20, /* 3 points star */ - 30, /* 4 points star */ - 30, /* 4 points star */ - 35, /* 5 points star */ - 35 /* 5 points star */ + 100, /* Square */ + 100, /* Square */ + 100, /* Rectangle */ + 100, /* Rectangle */ + 100, /* Circle */ + 100, /* Circle */ + 100, /* Ellipse */ + 100, /* Ellipse */ + 100, /* Triangle */ + 100, /* Triangle */ + 100, /* Pentagon */ + 100, /* Pentagon */ + 100, /* Rhombus */ + 100, /* Rhombus */ + 100, /* Octagon */ + 100, /* Octagon */ + 20, /* 3 points star */ + 20, /* 3 points star */ + 30, /* 4 points star */ + 30, /* 4 points star */ + 35, /* 5 points star */ + 35 /* 5 points star */ }; @@ -279,7 +279,6 @@ const char *const shape_names[NUM_SHAPES] = { // Pentagone star (5 points star) gettext_noop("Star") - }; @@ -295,10 +294,8 @@ 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."), @@ -313,16 +310,12 @@ const char *const shape_tips[NUM_SHAPES] = { gettext_noop("A pentagon has five 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."), // Description of an octagon - gettext_noop - ("An octagon has eight equal sides."), - gettext_noop - ("An octagon has eight equal sides."), + gettext_noop("An octagon has eight equal sides."), + gettext_noop("An octagon has eight equal sides."), gettext_noop("A star with 3 points."), gettext_noop("A star with 3 points."), @@ -330,7 +323,6 @@ const char *const shape_tips[NUM_SHAPES] = { gettext_noop("A star with 4 points."), gettext_noop("A star with 5 points."), gettext_noop("A star with 5 points.") - }; @@ -359,5 +351,4 @@ const char *const shape_img_fnames[NUM_SHAPES] = { DATA_PREFIX "images/shapes/star4p_f.png", DATA_PREFIX "images/shapes/star5p.png", DATA_PREFIX "images/shapes/star5p_f.png" - }; diff --git a/src/sounds.h b/src/sounds.h index a800365c0..ed850f74c 100644 --- a/src/sounds.h +++ b/src/sounds.h @@ -37,34 +37,34 @@ enum { - SND_HARP, /* Begin / New */ - SND_CLICK, /* Tool selections */ - SND_BLEEP, /* Selector selection */ - SND_BUBBLE, /* Color selection */ - SND_STAMP, /* Using stamp tool */ - SND_LINE_START, /* Using line tool */ + SND_HARP, /* Begin / New */ + SND_CLICK, /* Tool selections */ + SND_BLEEP, /* Selector selection */ + SND_BUBBLE, /* Color selection */ + SND_STAMP, /* Using stamp tool */ + SND_LINE_START, /* Using line tool */ SND_LINE_END, - SND_SCROLL, /* Selector scroll buttons */ - SND_PAINT1, /* Sound while painting */ + SND_SCROLL, /* Selector scroll buttons */ + SND_PAINT1, /* Sound while painting */ SND_PAINT2, SND_PAINT3, SND_PAINT4, - SND_ERASER1, /* Sound while erasing */ + SND_ERASER1, /* Sound while erasing */ SND_ERASER2, - SND_SAVE, /* Save sound effect */ - SND_PROMPT, /* Prompt animation sound effect */ - SND_FLIP, /* Magic flip */ - SND_MIRROR, /* Magic mirror */ - SND_KEYCLICK, /* Text tool keyboard click feedback */ - SND_KEYCLICKRING, /* Text tool keyboard click feedback with bell ring */ - SND_RETURN, /* Text tool carriage return sound */ - SND_SHRINK, /* Stamp shrink */ - SND_GROW, /* Stamp grow */ - SND_ITALIC_ON, /* Italic on */ - SND_ITALIC_OFF, /* Italic off */ - SND_AREYOUSURE, /* "Are you sure?" */ - SND_YOUCANNOT, /* "No no no!" */ - SND_TUXOK, /* "Ok" */ + SND_SAVE, /* Save sound effect */ + SND_PROMPT, /* Prompt animation sound effect */ + SND_FLIP, /* Magic flip */ + SND_MIRROR, /* Magic mirror */ + SND_KEYCLICK, /* Text tool keyboard click feedback */ + SND_KEYCLICKRING, /* Text tool keyboard click feedback with bell ring */ + SND_RETURN, /* Text tool carriage return sound */ + SND_SHRINK, /* Stamp shrink */ + SND_GROW, /* Stamp grow */ + SND_ITALIC_ON, /* Italic on */ + SND_ITALIC_OFF, /* Italic off */ + SND_AREYOUSURE, /* "Are you sure?" */ + SND_YOUCANNOT, /* "No no no!" */ + SND_TUXOK, /* "Ok" */ SND_THICK, SND_THIN, NUM_SOUNDS diff --git a/src/tools.h b/src/tools.h index 89599d266..0336e27fc 100644 --- a/src/tools.h +++ b/src/tools.h @@ -121,13 +121,16 @@ const char *const tool_tips[NUM_TOOLS] = { gettext_noop("Click to start drawing a line. Let go to complete it."), // Shape tool instructions - gettext_noop("Pick a shape. Click to pick the center, drag, then let go when it is the size you want. Move around to rotate it, and click to draw it."), + gettext_noop + ("Pick a shape. Click to pick the center, drag, then let go when it is the size you want. Move around to rotate it, and click to draw it."), // Text tool instructions - gettext_noop("Choose a style of text. Click on your drawing and you can start typing. Press [Enter] or [Tab] to complete the text."), + gettext_noop + ("Choose a style of text. Click on your drawing and you can start typing. Press [Enter] or [Tab] to complete the text."), // Label tool instructions - gettext_noop("Choose a style of text. Click on your drawing and you can start typing. Press [Enter] or [Tab] to complete the text. By using the selector button and clicking an existing label, you can move it, edit it and change its text style."), + gettext_noop + ("Choose a style of text. Click on your drawing and you can start typing. Press [Enter] or [Tab] to complete the text. By using the selector button and clicking an existing label, you can move it, edit it and change its text style."), // Reserved... " ", diff --git a/src/tuxpaint.c b/src/tuxpaint.c index d3e6578f4..fc7b1f783 100644 --- a/src/tuxpaint.c +++ b/src/tuxpaint.c @@ -39,29 +39,29 @@ /* Color depth for Tux Paint to run in, and store canvases in: */ #if defined(NOKIA_770) -# define VIDEO_BPP 16 +#define VIDEO_BPP 16 #endif #if defined(OLPC_XO) -# define VIDEO_BPP 15 +#define VIDEO_BPP 15 #endif #ifndef VIDEO_BPP -/*# define VIDEO_BPP 15 */ /* saves memory */ -/*# define VIDEO_BPP 16 */ /* causes discoloration */ -/*# define VIDEO_BPP 24 */ /* compromise */ -# define VIDEO_BPP 32 /* might be fastest, if conversion funcs removed */ + /*# define VIDEO_BPP 15 *//* saves memory */ + /*# define VIDEO_BPP 16 *//* causes discoloration */ + /*# define VIDEO_BPP 24 *//* compromise */ +#define VIDEO_BPP 32 /* might be fastest, if conversion funcs removed */ #endif -/* #define CORNER_SHAPES */ /* need major work! */ + /* #define CORNER_SHAPES *//* need major work! */ /* Method for printing images: */ -#define PRINTMETHOD_PS /* Direct to PostScript */ -/*#define PRINTMETHOD_PNM_PS*/ /* Output PNM, assuming it gets printed */ -/*#define PRINTMETHOD_PNG_PNM_PS*/ /* Output PNG, assuming it gets printed */ +#define PRINTMETHOD_PS /* Direct to PostScript */ + /*#define PRINTMETHOD_PNM_PS*//* Output PNM, assuming it gets printed */ + /*#define PRINTMETHOD_PNG_PNM_PS*//* Output PNG, assuming it gets printed */ #define MAX_PATH 256 @@ -71,10 +71,10 @@ #include "debug.h" #ifdef NOKIA_770 -# define LOW_QUALITY_THUMBNAILS -# define LOW_QUALITY_STAMP_OUTLINE -# define NO_PROMPT_SHADOWS -# define USE_HWSURFACE +#define LOW_QUALITY_THUMBNAILS +#define LOW_QUALITY_STAMP_OUTLINE +#define NO_PROMPT_SHADOWS +#define USE_HWSURFACE #else /* #define DEBUG_MALLOC */ /* #define LOW_QUALITY_THUMBNAILS */ @@ -96,12 +96,12 @@ /* #define LARGE_CURSOR_FULLSCREEN_BUG */ /* control the color selector */ -#define COLORSEL_DISABLE 0 /* disable and draw the (greyed out) colors */ -#define COLORSEL_ENABLE 1 /* enable and draw the colors */ -#define COLORSEL_CLOBBER 2 /* colors get scribbled over */ -#define COLORSEL_REFRESH 4 /* redraw the colors, either on or off */ +#define COLORSEL_DISABLE 0 /* disable and draw the (greyed out) colors */ +#define COLORSEL_ENABLE 1 /* enable and draw the colors */ +#define COLORSEL_CLOBBER 2 /* colors get scribbled over */ +#define COLORSEL_REFRESH 4 /* redraw the colors, either on or off */ #define COLORSEL_CLOBBER_WIPE 8 /* draw the (greyed out) colors, but don't disable */ -#define COLORSEL_FORCE_REDRAW 16 /* enable, and force redraw (to make color picker work) */ +#define COLORSEL_FORCE_REDRAW 16 /* enable, and force redraw (to make color picker work) */ /* FIXME: Why are we checking this BEFORE the #include "SDL.h"!? Does this even work? -bjk 2010.04.24 */ /* Setting the amask value based on endianness*/ @@ -120,40 +120,40 @@ typedef struct scaleparams } scaleparams; static scaleparams scaletable[] = { - {1, 256}, /* 0.00390625 */ - {3, 512}, /* 0.005859375 */ - {1, 128}, /* 0.0078125 */ - {3, 256}, /* 0.01171875 */ - {1, 64}, /* 0.015625 */ - {3, 128}, /* 0.0234375 */ - {1, 32}, /* 0.03125 */ - {3, 64}, /* 0.046875 */ - {1, 16}, /* 0.0625 */ - {3, 32}, /* 0.09375 */ - {1, 8}, /* 0.125 */ - {3, 16}, /* 0.1875 */ - {1, 4}, /* 0.25 */ - {3, 8}, /* 0.375 */ - {1, 2}, /* 0.5 */ - {3, 4}, /* 0.75 */ - {1, 1}, /* 1 */ - {3, 2}, /* 1.5 */ - {2, 1}, /* 2 */ - {3, 1}, /* 3 */ - {4, 1}, /* 4 */ - {6, 1}, /* 6 */ - {8, 1}, /* 8 */ - {12, 1}, /* 12 */ - {16, 1}, /* 16 */ - {24, 1}, /* 24 */ - {32, 1}, /* 32 */ - {48, 1}, /* 48 */ + {1, 256}, /* 0.00390625 */ + {3, 512}, /* 0.005859375 */ + {1, 128}, /* 0.0078125 */ + {3, 256}, /* 0.01171875 */ + {1, 64}, /* 0.015625 */ + {3, 128}, /* 0.0234375 */ + {1, 32}, /* 0.03125 */ + {3, 64}, /* 0.046875 */ + {1, 16}, /* 0.0625 */ + {3, 32}, /* 0.09375 */ + {1, 8}, /* 0.125 */ + {3, 16}, /* 0.1875 */ + {1, 4}, /* 0.25 */ + {3, 8}, /* 0.375 */ + {1, 2}, /* 0.5 */ + {3, 4}, /* 0.75 */ + {1, 1}, /* 1 */ + {3, 2}, /* 1.5 */ + {2, 1}, /* 2 */ + {3, 1}, /* 3 */ + {4, 1}, /* 4 */ + {6, 1}, /* 6 */ + {8, 1}, /* 8 */ + {12, 1}, /* 12 */ + {16, 1}, /* 16 */ + {24, 1}, /* 24 */ + {32, 1}, /* 32 */ + {48, 1}, /* 48 */ }; /* Macros: */ -#define HARD_MIN_STAMP_SIZE 0 /* bottom of scaletable */ +#define HARD_MIN_STAMP_SIZE 0 /* bottom of scaletable */ #define HARD_MAX_STAMP_SIZE (sizeof scaletable / sizeof scaletable[0] - 1) #define MIN_STAMP_SIZE (stamp_data[stamp_group][cur_stamp[stamp_group]]->min) @@ -167,12 +167,12 @@ static scaleparams scaletable[] = { #define CUR_STAMP_H SCALE_LIKE_STAMP(active_stamp->h) -#define REPEAT_SPEED 300 /* Initial repeat speed for scrollbars */ -#define CURSOR_BLINK_SPEED 500 /* Initial repeat speed for cursor */ +#define REPEAT_SPEED 300 /* Initial repeat speed for scrollbars */ +#define CURSOR_BLINK_SPEED 500 /* Initial repeat speed for cursor */ #ifndef _GNU_SOURCE -#define _GNU_SOURCE /* for strcasestr() */ +#define _GNU_SOURCE /* for strcasestr() */ #endif #include @@ -180,7 +180,7 @@ static scaleparams scaletable[] = { #include #include #include -#include //EP added this include for basename() +#include //EP added this include for basename() /* On Linux, we can use 'wordexp()' to expand env. vars. in settings pulled from config. files */ @@ -221,7 +221,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; } @@ -260,7 +260,7 @@ char *strcasestr(const char *haystack, const char *needle) #endif #ifdef DEBUG -#undef gettext //EP to avoid warning on following line +#undef gettext //EP to avoid warning on following line #define gettext(String) debug_gettext(String) #endif @@ -280,7 +280,7 @@ char *strcasestr(const char *haystack, const char *needle) #include #include -#if defined __BEOS__ //|| defined __HAIKU__ +#if defined __BEOS__ //|| defined __HAIKU__ /* BeOS */ @@ -296,6 +296,7 @@ typedef struct safer_dirent unsigned short d_reclen; char d_name[FILENAME_MAX]; } safer_dirent; + #define dirent safer_dirent #else /* __BEOS__ */ @@ -345,23 +346,24 @@ extern WrapperData macosx; #define mkdir(path,access) _mkdir(path) -static void mtw(wchar_t * wtok, char * tok) +static void mtw(wchar_t * wtok, char *tok) { /* workaround using iconv to get a functionallity somewhat approximate as mbstowcs() */ Uint16 *ui16; + ui16 = malloc(255); - char *wrptr = (char *) ui16; + char *wrptr = (char *)ui16; size_t n, in, out; iconv_t trans; - wchar_t * wch; + wchar_t *wch; n = 255; in = 250; out = 250; - wch =malloc(255); + wch = malloc(255); trans = iconv_open("WCHAR_T", "UTF-8"); - iconv(trans, (const char **) &tok, &in, &wrptr, &out); + iconv(trans, (const char **)&tok, &in, &wrptr, &out); *((wchar_t *) wrptr) = L'\0'; swprintf(wtok, L"%ls", ui16); free(ui16); @@ -484,7 +486,7 @@ static void mtw(wchar_t * wtok, char * tok) #endif -#include //EP added for PNG upgrade from 1.2 to 1.5 +#include //EP added for PNG upgrade from 1.2 to 1.5 #define PNG_INTERNAL #include #define FNAME_EXTENSION ".png" @@ -569,25 +571,25 @@ static void mtw(wchar_t * wtok, char * tok) //#define fmemopen_alternative */ /* Uncomment this to test the fmemopen alternative in systems were fmemopen exists */ -#if defined (WIN32) || defined (__APPLE__) || defined(__NetBSD__) || defined(__sun) || defined(__ANDROID__) // MINGW/MSYS, NetBSD, and MacOSX need it, at least for now -#define fmemopen_alternative +#if defined (WIN32) || defined (__APPLE__) || defined(__NetBSD__) || defined(__sun) || defined(__ANDROID__) // MINGW/MSYS, NetBSD, and MacOSX need it, at least for now +#define fmemopen_alternative #endif #ifdef fmemopen_alternative #undef fmemopen -FILE * my_fmemopen(unsigned char * data, size_t size, const char * mode); +FILE *my_fmemopen(unsigned char *data, size_t size, const char *mode); -FILE * my_fmemopen(unsigned char * data, size_t size, const char * mode) +FILE *my_fmemopen(unsigned char *data, size_t size, const char *mode) { unsigned int i; char *fname; - FILE * fi; + FILE *fi; #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 @@ -595,19 +597,19 @@ FILE * my_fmemopen(unsigned char * data, size_t size, const char * mode) if (fi == NULL) { free(fname); - return(NULL); + return (NULL); } for (i = 0; i < size; i++) { fwrite(data, 1, 1, fi); - data ++; + data++; } - + fclose(fi); fi = fopen(fname, mode); free(fname); - return(fi); + return (fi); } #define fmemopen my_fmemopen @@ -641,7 +643,7 @@ enum { LABEL_OFF, LABEL_LABEL, - LABEL_SELECT //, + LABEL_SELECT //, // LABEL_ROTATE }; @@ -650,8 +652,8 @@ enum /* Color globals (copied from colors.h, if no colors specified by user) */ static int NUM_COLORS; -static Uint8 * * color_hexes; -static char * * color_names; +static Uint8 **color_hexes; +static char **color_names; /* Show debugging stuff: */ @@ -659,7 +661,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); @@ -693,32 +695,33 @@ typedef struct Uint8 rows, cols; } grid_dims; -/* static SDL_Rect r_screen; */ /* was 640x480 @ 0,0 -- but this isn't so useful */ -static SDL_Rect r_canvas; /* was 448x376 @ 96,0 */ -static SDL_Rect r_tools; /* was 96x336 @ 0,40 */ + /* static SDL_Rect r_screen; *//* was 640x480 @ 0,0 -- but this isn't so useful */ +static SDL_Rect r_canvas; /* was 448x376 @ 96,0 */ +static SDL_Rect r_tools; /* was 96x336 @ 0,40 */ static SDL_Rect r_sfx; -static SDL_Rect r_toolopt; /* was 96x336 @ 544,40 */ -static SDL_Rect r_colors; /* was 544x48 @ 96,376 */ -static SDL_Rect r_ttools; /* was 96x40 @ 0,0 (title for tools, "Tools") */ -static SDL_Rect r_tcolors; /* was 96x48 @ 0,376 (title for colors, "Colors") */ -static SDL_Rect r_ttoolopt; /* was 96x40 @ 544,0 (title for tool options) */ -static SDL_Rect r_tuxarea; /* was 640x56 */ +static SDL_Rect r_toolopt; /* was 96x336 @ 544,40 */ +static SDL_Rect r_colors; /* was 544x48 @ 96,376 */ +static SDL_Rect r_ttools; /* was 96x40 @ 0,0 (title for tools, "Tools") */ +static SDL_Rect r_tcolors; /* was 96x48 @ 0,376 (title for colors, "Colors") */ +static SDL_Rect r_ttoolopt; /* was 96x40 @ 544,0 (title for tool options) */ +static SDL_Rect r_tuxarea; /* was 640x56 */ static SDL_Rect r_label; static SDL_Rect old_dest; -static int button_w; /* was 48 */ -static int button_h; /* was 48 */ +static int button_w; /* was 48 */ +static int button_h; /* was 48 */ -static int color_button_w; /* was 32 */ -static int color_button_h; /* was 48 */ +static int color_button_w; /* was 32 */ +static int color_button_h; /* was 48 */ /* Define button grid dimensions. (in button units) */ /* These are the maximum slots -- some may be unused. */ -static grid_dims gd_tools; /* was 2x7 */ +static grid_dims gd_tools; /* was 2x7 */ static grid_dims gd_sfx; -static grid_dims gd_toolopt; /* was 2x7 */ -/* static grid_dims gd_open; */ /* was 4x4 */ -static grid_dims gd_colors; /* was 17x1 */ +static grid_dims gd_toolopt; /* was 2x7 */ + + /* static grid_dims gd_open; *//* was 4x4 */ +static grid_dims gd_colors; /* was 17x1 */ #define HEIGHTOFFSET (((WINDOW_HEIGHT - 480) / 48) * 48) #define TOOLOFFSET (HEIGHTOFFSET / 48 * 2) @@ -806,8 +809,8 @@ static void setup_normal_screen_layout(void) r_sfx.x = r_tuxarea.x; r_sfx.y = r_tuxarea.y; - r_sfx.w = button_w; /* Two half-sized buttons across */ - r_sfx.h = button_h >> 1; /* One half-sized button down */ + r_sfx.w = button_w; /* Two half-sized buttons across */ + r_sfx.h = button_h >> 1; /* One half-sized button down */ gd_sfx.rows = 1; gd_sfx.cols = 2; @@ -894,21 +897,23 @@ static void SDL_WarpMouse(Uint16 x, Uint16 y) SDL_WarpMouseInWindow(window_screen, x, y); } -static SDL_Surface *SDL_DisplayFormat(SDL_Surface *surface) +static SDL_Surface *SDL_DisplayFormat(SDL_Surface * surface) { SDL_Surface *tmp; + tmp = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGB888, 0); - return(tmp); + return (tmp); } -static SDL_Surface *SDL_DisplayFormatAlpha(SDL_Surface *surface) +static SDL_Surface *SDL_DisplayFormatAlpha(SDL_Surface * surface) { SDL_Surface *tmp; - tmp = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ARGB8888 , 0); - return(tmp); + + tmp = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ARGB8888, 0); + return (tmp); } -static void SDL_Flip(SDL_Surface *screen) +static void SDL_Flip(SDL_Surface * screen) { //SDL_UpdateWindowSurface(window_screen); SDL_UpdateTexture(texture, NULL, screen->pixels, screen->pitch); @@ -920,6 +925,7 @@ static void SDL_Flip(SDL_Surface *screen) static void SDL_UpdateRect(SDL_Surface * screen, Sint32 x, Sint32 y, Sint32 w, Sint32 h) { SDL_Rect r; + r.x = x; r.y = y; r.w = w; @@ -939,25 +945,25 @@ static void show_progress_bar(SDL_Surface * screen) show_progress_bar_(screen, texture, renderer); } - + /* Update a rect. based on two x/y coords (not necessarly in order): */ 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; @@ -1009,8 +1015,7 @@ static int hit_test(const SDL_Rect * const r, unsigned x, unsigned y) w,h are the size of a grid item Return the grid box. NOTE: grid items must fill full SDL_Rect width exactly */ -static int grid_hit_wh(const SDL_Rect * const r, unsigned x, unsigned y, - unsigned w, unsigned h) +static int grid_hit_wh(const SDL_Rect * const r, unsigned x, unsigned y, unsigned w, unsigned h) { return (x - r->x) / w + (y - r->y) / h * (r->w / w); } @@ -1026,16 +1031,16 @@ static int grid_hit_wh(const SDL_Rect * const r, unsigned x, unsigned y, /* x,y are pixel-wise screen-relative (mouse location), not grid-wise Return the grid box. NOTE: returns -1 if hit is below or to the right of 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; unsigned col = (x - r->x) / item_w; unsigned row = (y - r->y) / item_h; + #ifdef DEBUG printf("%d,%d resolves to %d,%d in a %dx%d grid, index is %d\n", x, y, col, - row, gd->cols, gd->rows, col + row * gd->cols); + row, gd->cols, gd->rows, col + row * gd->cols); #endif if (col >= gd->cols || row >= gd->rows) return -1; @@ -1069,12 +1074,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); + } // printf("%d\n", canvas ); //printf("%d, %d, %d, %d\n", dest.x, dest.y, dest.w, dest.h); //printf("%d\n", screen); @@ -1087,8 +1092,8 @@ static void update_canvas_ex_r(int x1, int y1, int x2, int y2, int screen_too) /* If label is not disabled, cover canvas with label layer */ - if(!disable_label) - SDL_BlitSurface(label, &src, screen, &dest); + if (!disable_label) + SDL_BlitSurface(label, &src, screen, &dest); if (screen_too) update_screen(x1 + 96, y1, x2 + 96, y2); @@ -1097,30 +1102,31 @@ static void update_canvas_ex_r(int x1, int y1, int x2, int y2, int screen_too) 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); /* If label is not disabled, cover canvas with label layer */ - if(!disable_label) - SDL_BlitSurface(label, NULL, screen, &r_label); + if (!disable_label) + SDL_BlitSurface(label, NULL, screen, &r_label); if (screen_too) update_screen(x1 + 96, y1, x2 + 96, y2); @@ -1137,7 +1143,7 @@ static void update_canvas(int x1, int y1, int x2, int y2) static int emulate_button_pressed = 0; static int mouseaccessibility = 0; static int onscreen_keyboard = 0; -static char * onscreen_keyboard_layout = NULL; +static char *onscreen_keyboard_layout = NULL; static on_screen_keyboard *kbd = NULL; static int onscreen_keyboard_disable_change = 0; static int joystick_low_threshold = 3200; @@ -1167,6 +1173,7 @@ static Uint32 old_hat_ticks = 0; static int oldpos_x; static int oldpos_y; static int disable_screensaver; + #ifdef NOKIA_770 static int fullscreen = 1; #else @@ -1190,9 +1197,7 @@ static int button_down; static int scrolling; 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; @@ -1223,7 +1228,7 @@ static int template_personal; static int starter_modified; static Uint8 canvas_color_r, canvas_color_g, canvas_color_b; -static Uint8 * touched; +static Uint8 *touched; static int last_print_time = 0; static int shape_radius; @@ -1239,23 +1244,23 @@ typedef struct label_node Uint16 save_x; Uint16 save_y; int save_cur_font; - char * save_font_type; + char *save_font_type; int save_text_state; unsigned save_text_size; int save_undoid; int is_enabled; - struct label_node* disables; - struct label_node* next_to_up_label_node; - struct label_node* next_to_down_label_node; - SDL_Surface* label_node_surface; + struct label_node *disables; + struct label_node *next_to_up_label_node; + struct label_node *next_to_down_label_node; + SDL_Surface *label_node_surface; } label_node; -static struct label_node* start_label_node; -static struct label_node* current_label_node; -static struct label_node* first_label_node_in_redo_stack; -static struct label_node* label_node_to_edit; -static struct label_node* highlighted_label_node; +static struct label_node *start_label_node; +static struct label_node *current_label_node; +static struct label_node *first_label_node_in_redo_stack; +static struct label_node *label_node_to_edit; +static struct label_node *highlighted_label_node; static unsigned int select_texttool_len; static wchar_t select_texttool_str[256]; @@ -1270,10 +1275,10 @@ 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 load_info_about_label_surface(FILE *lfi); +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); @@ -1282,15 +1287,14 @@ static void do_undo_label_node(void); static void do_redo_label_node(void); static void rec_undo_label(void); -static void render_all_nodes_starting_at(struct label_node**); -static void simply_render_node(struct label_node*); +static void render_all_nodes_starting_at(struct label_node **); +static void simply_render_node(struct label_node *); -static void derender_node(struct label_node**); +static void derender_node(struct label_node **); -static void delete_label_list(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); @@ -1298,17 +1302,19 @@ 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, int *hat_motioner, Uint32 *old_hat_ticks); +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, int val_y, int valhat_x, int valhat_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); #ifdef __ANDROID__ -static void start_motion_convert (SDL_Event event); +static void start_motion_convert(SDL_Event event); static void convert_motion_to_wheel(SDL_Event event); -static void stop_motion_convert (SDL_Event event); +static void stop_motion_convert(SDL_Event event); #endif /* Magic tools API and tool handles: */ @@ -1318,49 +1324,52 @@ static void stop_motion_convert (SDL_Event event); static void update_progress_bar(void); static void special_notify(int flags); -typedef struct magic_funcs_s { - int (*get_tool_count)(magic_api *); - 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 *); +typedef struct magic_funcs_s +{ + int (*get_tool_count) (magic_api *); + 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; -typedef struct magic_s { +typedef struct magic_s +{ int place; - int handle_idx; /* Index to magic funcs for each magic tool (shared objs may report more than 1 tool) */ - int idx; /* Index to magic tools within shared objects (shared objs may report more than 1 tool) */ - int mode; /* Current mode (paint or fullscreen) */ - int avail_modes; /* Available modes (paint &/or fullscreen) */ - int colors; /* Whether magic tool accepts colors */ - char * name; /* Name of magic tool */ - char * tip[MAX_MODES]; /* Description of magic tool, in each possible mode */ - SDL_Surface * img_icon; - SDL_Surface * img_name; + int handle_idx; /* Index to magic funcs for each magic tool (shared objs may report more than 1 tool) */ + int idx; /* Index to magic tools within shared objects (shared objs may report more than 1 tool) */ + int mode; /* Current mode (paint or fullscreen) */ + int avail_modes; /* Available modes (paint &/or fullscreen) */ + int colors; /* Whether magic tool accepts colors */ + char *name; /* Name of magic tool */ + char *tip[MAX_MODES]; /* Description of magic tool, in each possible mode */ + SDL_Surface *img_icon; + SDL_Surface *img_name; } magic_t; /* FIXME: Drop the 512 constants :^P */ -static int num_plugin_files; /* How many shared object files we went through */ -static void * magic_handle[512]; /* Handle to shared object (to be unloaded later) */ /* FIXME: Unload them! */ -static magic_funcs_t magic_funcs[512]; /* Pointer to shared objects' functions */ +static int num_plugin_files; /* How many shared object files we went through */ +static void *magic_handle[512]; /* Handle to shared object (to be unloaded later) *//* FIXME: Unload them! */ +static magic_funcs_t magic_funcs[512]; /* Pointer to shared objects' functions */ static magic_t magics[512]; -static int num_magics; /* How many magic tools were loaded (note: shared objs may report more than 1 tool) */ +static int num_magics; /* How many magic tools were loaded (note: shared objs may report more than 1 tool) */ -enum { +enum +{ MAGIC_PLACE_GLOBAL, MAGIC_PLACE_LOCAL, #ifdef __APPLE__ @@ -1369,7 +1378,7 @@ enum { NUM_MAGIC_PLACES }; -static magic_api *magic_api_struct; /* Pointer to our internal functions; passed to shared object's functions when we call them */ +static magic_api *magic_api_struct; /* Pointer to our internal functions; passed to shared object's functions when we call them */ #if !defined(WIN32) && !defined(__APPLE__) && !defined(__BEOS__) && !defined(__HAIKU__) && !defined(__ANDROID__) @@ -1387,7 +1396,7 @@ static const char *papersize; #endif -#if 1 /* FIXME: ifdef for platforms that lack fribidi? */ +#if 1 /* FIXME: ifdef for platforms that lack fribidi? */ #include #if !defined(_FRIBIDI_H) && !defined(FRIBIDI_H) #error "---------------------------------------------------" @@ -1439,16 +1448,13 @@ static SDL_Surface *img_label, *img_label_select; static SDL_Surface *img_color_picker, *img_color_picker_thumb, *img_paintwell, *img_color_sel; static int color_picker_x, color_picker_y; -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 *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); @@ -1456,53 +1462,53 @@ static SDL_Surface *zoom(SDL_Surface * src, int new_x, int new_y); -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; + #ifndef NO_SDLPANGO SDLPango_Matrix pango_color; #endif if (font == NULL) - { - printf("render_text() received a NULL font!\n"); - fflush(stdout); - return NULL; - } - + { + printf("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; @@ -1510,7 +1516,7 @@ static SDL_Surface *render_text(TuxPaint_Font * restrict font, /* Sometimes a font will be missing a character we need. Sometimes the library will substitute a rectangle without telling us. Sometimes it returns NULL. Probably we should use FreeType directly. For now though... */ - + height = 2; return thumbnail(img_title_large_off, height * strlen(str) / 2, height, 0); @@ -1527,115 +1533,114 @@ static Uint16 *wcstou16(const wchar_t * 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; } -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; Uint16 *ustr; + #ifndef NO_SDLPANGO unsigned int i, j; int utfstr_max; - char * utfstr; + char *utfstr; SDLPango_Matrix pango_color; #endif #ifndef NO_SDLPANGO if (font->typ == FONT_TYPE_PANGO) - { - sdl_color_to_pango_color(color, &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); - memset(utfstr, 0, utfstr_max); - - j = 0; - for (i = 0; i < wcslen(str); i++) { - if (str[i] <= 0x0000007F) - { - /* 0x00000000 - 0x0000007F: - 0xxxxxxx */ + sdl_color_to_pango_color(color, &pango_color); - utfstr[j++] = (str[i] & 0x7F); - } - else if (str[i] <= 0x000007FF) - { - /* 0x00000080 - 0x000007FF: - - 00000abc defghijk - 110abcde 10fghijk */ + SDLPango_SetDefaultColor(font->pango_context, &pango_color); - utfstr[j++] = (((str[i] & 0x0700) >> 6) | /* -----abc -------- to ---abc-- */ - ((str[i] & 0x00C0) >> 6) | /* -------- de------ to ------de */ - (0xC0)); /* add 110----- */ + /* Convert from 16-bit UNICODE to UTF-8 encoded for SDL_Pango: */ - utfstr[j++] = (((str[i] & 0x003F)) | /* -------- --fghijk to --fghijk */ - (0x80)); /* add 10------ */ - } - else if (str[i] <= 0x0000FFFF) - { - /* 0x00000800 - 0x0000FFFF: + utfstr_max = (sizeof(char) * 4 * (wcslen(str) + 1)); + utfstr = (char *)malloc(utfstr_max); + memset(utfstr, 0, utfstr_max); - abcdefgh ijklmnop - 1110abcd 10efghij 10klmnop */ + j = 0; + for (i = 0; i < wcslen(str); i++) + { + if (str[i] <= 0x0000007F) + { + /* 0x00000000 - 0x0000007F: + 0xxxxxxx */ - utfstr[j++] = (((str[i] & 0xF000) >> 12) | /* abcd---- -------- to ----abcd */ - (0xE0)); /* add 1110---- */ - utfstr[j++] = (((str[i] & 0x0FC0) >> 6) | /* ----efgh ij------ to --efghij */ - (0x80)); /* add 10------ */ - utfstr[j++] = (((str[i] & 0x003F)) | /* -------- --klmnop to --klmnop */ - (0x80)); /* add 10------ */ - } - else - { - /* 0x00010000 - 0x001FFFFF: - 11110abc 10defghi 10jklmno 10pqrstu */ + utfstr[j++] = (str[i] & 0x7F); + } + else if (str[i] <= 0x000007FF) + { + /* 0x00000080 - 0x000007FF: - utfstr[j++] = (((str[i] & 0x1C0000) >> 18) | /* ---abc-- -------- -------- to -----abc */ - (0xF0)); /* add 11110000 */ - utfstr[j++] = (((str[i] & 0x030000) >> 12) | /* ------de -------- -------- to --de---- */ - ((str[i] & 0x00F000) >> 12) | /* -------- fghi---- -------- to ----fghi */ - (0x80)); /* add 10------ */ - utfstr[j++] = (((str[i] & 0x000F00) >> 6) | /* -------- ----jklm -------- to --jklm-- */ - ((str[i] & 0x0000C0) >> 6) | /* -------- -------- no------ to ------no */ - (0x80)); /* add 10------ */ - utfstr[j++] = ((str[i] & 0x00003F) | /* -------- -------- --pqrstu to --prqstu */ - (0x80)); /* add 10------ */ - } + 00000abc defghijk + 110abcde 10fghijk */ + + utfstr[j++] = (((str[i] & 0x0700) >> 6) | /* -----abc -------- to ---abc-- */ + ((str[i] & 0x00C0) >> 6) | /* -------- de------ to ------de */ + (0xC0)); /* add 110----- */ + + utfstr[j++] = (((str[i] & 0x003F)) | /* -------- --fghijk to --fghijk */ + (0x80)); /* add 10------ */ + } + else if (str[i] <= 0x0000FFFF) + { + /* 0x00000800 - 0x0000FFFF: + + abcdefgh ijklmnop + 1110abcd 10efghij 10klmnop */ + + utfstr[j++] = (((str[i] & 0xF000) >> 12) | /* abcd---- -------- to ----abcd */ + (0xE0)); /* add 1110---- */ + utfstr[j++] = (((str[i] & 0x0FC0) >> 6) | /* ----efgh ij------ to --efghij */ + (0x80)); /* add 10------ */ + utfstr[j++] = (((str[i] & 0x003F)) | /* -------- --klmnop to --klmnop */ + (0x80)); /* add 10------ */ + } + else + { + /* 0x00010000 - 0x001FFFFF: + 11110abc 10defghi 10jklmno 10pqrstu */ + + utfstr[j++] = (((str[i] & 0x1C0000) >> 18) | /* ---abc-- -------- -------- to -----abc */ + (0xF0)); /* add 11110000 */ + utfstr[j++] = (((str[i] & 0x030000) >> 12) | /* ------de -------- -------- to --de---- */ + ((str[i] & 0x00F000) >> 12) | /* -------- fghi---- -------- to ----fghi */ + (0x80)); /* add 10------ */ + utfstr[j++] = (((str[i] & 0x000F00) >> 6) | /* -------- ----jklm -------- to --jklm-- */ + ((str[i] & 0x0000C0) >> 6) | /* -------- -------- no------ to ------no */ + (0x80)); /* add 10------ */ + utfstr[j++] = ((str[i] & 0x00003F) | /* -------- -------- --pqrstu to --prqstu */ + (0x80)); /* add 10------ */ + } + } + utfstr[j] = '\0'; + + + SDLPango_SetText(font->pango_context, utfstr, -1); + ret = SDLPango_CreateSurfaceDraw(font->pango_context); } - utfstr[j] = '\0'; - - - 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; @@ -1643,8 +1648,8 @@ static SDL_Surface *render_text_w(TuxPaint_Font * restrict font, /* Sometimes a font will be missing a character we need. Sometimes the library will substitute a rectangle without telling us. Sometimes it returns NULL. Probably we should use FreeType directly. For now though... */ - - height = 2; + + height = 2; return thumbnail(img_title_large_off, height * wcslen(str) / 2, height, 0); } @@ -1667,12 +1672,12 @@ typedef struct stamp_type unsigned no_preflip:1; unsigned no_premirrorflip:1; - unsigned processed:1; /* got *.dat, computed size limits, etc. */ + unsigned processed:1; /* got *.dat, computed size limits, etc. */ unsigned no_sound:1; unsigned no_descsound:1; unsigned no_txt:1; -/* unsigned no_local_sound : 1; */ /* to remember, if code written to discard sound */ + /* unsigned no_local_sound : 1; *//* to remember, if code written to discard sound */ unsigned tinter:3; unsigned colorable:1; @@ -1691,7 +1696,7 @@ typedef struct stamp_type #define MAX_STAMP_GROUPS 256 -static unsigned int stamp_group_dir_depth = 1; /* How deep (how many slashes in a subdirectory path) we think a new stamp group should be */ +static unsigned int stamp_group_dir_depth = 1; /* How deep (how many slashes in a subdirectory path) we think a new stamp group should be */ static int stamp_group = 0; @@ -1720,14 +1725,13 @@ static int stamp_tintable(int stamp) #define SHAPE_BRUSH_NAME "aa_round_03.png" static int num_brushes, num_brushes_max, shape_brush = 0; static SDL_Surface **img_brushes; -static int * brushes_frames = NULL; -static int * brushes_spacing = NULL; -static short * brushes_directional = NULL; +static int *brushes_frames = NULL; +static int *brushes_spacing = NULL; +static short *brushes_directional = NULL; static SDL_Surface *img_shapes[NUM_SHAPES], *img_shape_names[NUM_SHAPES]; static SDL_Surface *img_openlabels_open, *img_openlabels_erase, - *img_openlabels_slideshow, *img_openlabels_back, *img_openlabels_play, - *img_openlabels_next; + *img_openlabels_slideshow, *img_openlabels_back, *img_openlabels_play, *img_openlabels_next; static SDL_Surface *img_tux[NUM_TIP_TUX]; @@ -1736,13 +1740,14 @@ static SDL_Surface *img_mouse, *img_mouse_click; #ifdef LOW_QUALITY_COLOR_SELECTOR static SDL_Surface *img_paintcan; #else -static SDL_Surface * * img_color_btns; +static SDL_Surface **img_color_btns; static SDL_Surface *img_color_btn_off; #endif static int colors_are_selectable; -enum { +enum +{ BRUSH_DIRECTION_RIGHT, BRUSH_DIRECTION_DOWN_RIGHT, BRUSH_DIRECTION_DOWN, @@ -1756,12 +1761,12 @@ enum { static SDL_Surface *img_cur_brush; static int img_cur_brush_frame_w, img_cur_brush_w, img_cur_brush_h, - img_cur_brush_frames, img_cur_brush_directional, img_cur_brush_spacing; + img_cur_brush_frames, img_cur_brush_directional, img_cur_brush_spacing; static int brush_counter, brush_frame; -#define NUM_ERASERS 12 /* How many sizes of erasers - (from ERASER_MIN to _MAX as squares, then again - from ERASER_MIN to _MAX as circles) */ +#define NUM_ERASERS 12 /* How many sizes of erasers + (from ERASER_MIN to _MAX as squares, then again + from ERASER_MIN to _MAX as circles) */ #define ERASER_MIN 13 #define ERASER_MAX 128 @@ -1772,7 +1777,7 @@ static int cur_tool, cur_brush, old_tool; static int cur_stamp[MAX_STAMP_GROUPS]; static int cur_shape, cur_magic; static int cur_font, cur_eraser; -static int cursor_left, cursor_x, cursor_y, cursor_textwidth; /* canvas-relative */ +static int cursor_left, cursor_x, cursor_y, cursor_textwidth; /* canvas-relative */ static int old_cursor_x, old_cursor_y; static int cur_label, cur_select; static int been_saved; @@ -1782,7 +1787,7 @@ static char template_id[FILENAME_MAX]; static int brush_scroll; static int stamp_scroll[MAX_STAMP_GROUPS]; static int font_scroll, magic_scroll, tool_scroll; -static int eraser_scroll, shape_scroll; /* dummy variables for now */ +static int eraser_scroll, shape_scroll; /* dummy variables for now */ static int eraser_sound; @@ -1814,6 +1819,7 @@ typedef struct fpoint_type typedef enum { Left, Right, Bottom, Top } an_edge; + #define NUM_EDGES 4 static SDL_Event scrolltimer_event; @@ -1828,6 +1834,7 @@ typedef struct dirent2 } dirent2; SDL_Joystick *joystick; + /* Local function prototypes: */ static void mainloop(void); @@ -1841,8 +1848,7 @@ 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); @@ -1887,18 +1893,14 @@ static void enable_avail_tools(void); static void reset_avail_tools(void); static int compare_dirent2s(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 wordwrap_text(const char *const str, SDL_Color color, - int left, int top, int right, - int want_right_to_left); +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 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); + 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); + #ifndef NOSOUND static Mix_Chunk *loadsound(const char *const fname); static Mix_Chunk *loaddescsound(const char *const fname); @@ -1908,45 +1910,39 @@ static void do_wait(int counter); static void load_current(void); 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); + 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); 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); + 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); 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); + const char *const btn_no, SDL_Surface * img1, + 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); + 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); 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); + const char *const btn_no, int snd, int ox, int oy); static void cleanup(void); static void free_surface(SDL_Surface ** surface_array); static void free_surface_array(SDL_Surface * surface_array[], int count); + /*static void update_shape(int cx, int ox1, int ox2, int cy, int oy1, int oy2, int fixed); */ static void do_shape(int cx, int cy, int ox, int oy, 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 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); +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); static void get_new_file_id(void); static int do_quit(int tool); static int do_open(void); @@ -1954,14 +1950,11 @@ static int do_new_dialog(void); static int do_color_picker(void); static int do_color_sel(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 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); @@ -1971,7 +1964,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); @@ -1980,16 +1973,16 @@ static Uint32 drawtext_callback(Uint32 interval, void *param); 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(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 *remove_slash(char *path); + /*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_id(char *saved_id, FILE * fil); static void load_starter(char *img_id); static void load_template(char *img_id); static SDL_Surface *duplicate_surface(SDL_Surface * orig); @@ -2000,21 +1993,20 @@ static int in_circle(int x, int y); static int in_circle_rad(int x, int y, int rad); static int paintsound(int size); static void load_magic_plugins(void); -static int magic_sort(const void * a, const void * b); +static int magic_sort(const void *a, const void *b); -Mix_Chunk * magic_current_snd_ptr; +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)); +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)); static Uint8 magic_linear_to_sRGB(float lin); static float magic_sRGB_to_linear(Uint8 srgb); static int magic_button_down(void); -static SDL_Surface * magic_scale(SDL_Surface * surf, int w, int h, int aspect); +static SDL_Surface *magic_scale(SDL_Surface * surf, int w, int h, int aspect); static void reset_touched(void); static Uint8 magic_touched(int x, int y); @@ -2027,16 +2019,16 @@ static char *debug_gettext(const char *str); static int charsize(Uint16 c); #endif -static SDL_Surface * load_kpx(char * file); +static SDL_Surface *load_kpx(char *file); + #ifndef NOSVG -static SDL_Surface * load_svg(char * file); -static float pick_best_scape(unsigned int orig_w, unsigned int orig_h, - unsigned int max_w, unsigned int max_h); +static SDL_Surface *load_svg(char *file); +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(char * file); -static SDL_Surface * myIMG_Load(char * file); -static int trash(char * path); -int file_exists(char * path); +static SDL_Surface *myIMG_Load_RWops(char *file); +static SDL_Surface *myIMG_Load(char *file); +static int trash(char *path); +int file_exists(char *path); #define MAX_UTF8_CHAR_LENGTH 6 @@ -2059,33 +2051,32 @@ static void do_wait(int counter) done = 0; do - { - while (SDL_PollEvent(&event)) { - if (event.type == SDL_QUIT) - { - done = 1; + 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_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; + } + } + + counter--; + SDL_Delay(100); } - - counter--; - SDL_Delay(100); - } while (!done && counter > 0); } @@ -2103,39 +2094,40 @@ static void eat_sdl_events(void) SDL_Event event; while (SDL_PollEvent(&event)) - { - if (event.type == SDL_QUIT) { - SDL_Quit(); - exit(0); /* can't safely use do_quit during start-up */ - } - else if (event.type == SDL_WINDOWEVENT) - handle_active(&event); - else if (event.type == SDL_KEYDOWN) - { - SDLKey key = event.key.keysym.sym; - SDLMod ctrl = event.key.keysym.mod & KMOD_CTRL; - SDLMod alt = event.key.keysym.mod & KMOD_ALT; - if ((key == SDLK_c && ctrl) || (key == SDLK_F4 && alt)) - { - SDL_Quit(); - exit(0); - } - else if (key == SDLK_ESCAPE && waiting_for_fonts) - { - /* abort font loading! */ + if (event.type == SDL_QUIT) + { + SDL_Quit(); + exit(0); /* can't safely use do_quit during start-up */ + } + else if (event.type == SDL_WINDOWEVENT) + handle_active(&event); + else if (event.type == SDL_KEYDOWN) + { + SDLKey key = event.key.keysym.sym; + SDLMod ctrl = event.key.keysym.mod & KMOD_CTRL; + SDLMod alt = event.key.keysym.mod & KMOD_ALT; - printf("Aborting font load!\n"); + if ((key == SDLK_c && ctrl) || (key == SDLK_F4 && alt)) + { + SDL_Quit(); + exit(0); + } + else if (key == SDLK_ESCAPE && waiting_for_fonts) + { + /* abort font loading! */ - font_thread_aborted = 1; - /* waiting_for_fonts = 0; */ - } - else - bypass_splash_wait = 1; + printf("Aborting font load!\n"); + + font_thread_aborted = 1; + /* waiting_for_fonts = 0; */ + } + else + bypass_splash_wait = 1; + } + else if (event.type == SDL_MOUSEBUTTONDOWN) + bypass_splash_wait = 1; } - else if (event.type == SDL_MOUSEBUTTONDOWN) - bypass_splash_wait = 1; - } } #endif @@ -2205,7 +2197,8 @@ enum }; -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; /* --- MAIN LOOP! --- */ @@ -2213,9 +2206,7 @@ int cur_thing; static void mainloop(void) { int done, val_x, val_y, valhat_x, valhat_y, new_x, new_y, - shape_tool_mode, - shape_ctr_x, shape_ctr_y, shape_outer_x, shape_outer_y, - old_stamp_group, which; + shape_tool_mode, shape_ctr_x, shape_ctr_y, shape_outer_x, shape_outer_y, old_stamp_group, which; int num_things; int *thing_scroll; int do_draw, max; @@ -2243,15 +2234,15 @@ static void mainloop(void) 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; + #ifdef DEBUG Uint16 key_unicode; SDLKey key_down; #endif - on_screen_keyboard *new_kbd; + on_screen_keyboard *new_kbd; SDL_Rect kbd_rect; num_things = num_brushes; @@ -2259,7 +2250,7 @@ static void mainloop(void) cur_thing = 0; do_draw = 0; old_x = 0; - old_y = 0; + old_y = 0; which = 0; shape_ctr_x = 0; shape_ctr_y = 0; @@ -2278,1835 +2269,1839 @@ static void mainloop(void) done = 0; keyglobal = 0; - if (NUM_TOOLS > 14 + TOOLOFFSET) - { - real_r_tools.h = r_tools.h - button_h; - real_r_tools.y = r_tools.y + button_h/2; - } + if (NUM_TOOLS > 14 + TOOLOFFSET) + { + 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)) { - current_event_time = SDL_GetTicks(); + ignoring_motion = 0; - /* 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) - 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") */ + pre_event_time = SDL_GetTicks(); - if (event.type == SDL_QUIT) - { - magic_switchout(canvas); - done = do_quit(cur_tool); - if (!done) - { - magic_switchin(canvas); + while (SDL_PollEvent(&event)) + { + current_event_time = SDL_GetTicks(); - 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); - } + /* 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) + 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") */ - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput (); - } - } - } - } + + 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) + { + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); + update_screen_rect(&kbd_rect); + } + + if (onscreen_keyboard && !kbd) + { + 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); - } - } + 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); + } + } #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_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_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.key.keysym.unicode, -// (int)event.key.keysym.unicode, -// (key_unicode>' ' && key_unicode<127)?(char)event.key.keysym.unicode:' ', +// 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.key.keysym.unicode, +// (int)event.key.keysym.unicode, +// (key_unicode>' ' && key_unicode<127)?(char)event.key.keysym.unicode:' ', // iswprint(key_unicode)?"":"not ", // (unsigned)key_down // ); #endif - 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); + if (cur_tool == TOOL_STAMP) + { + SDL_Rect r_stamps_sizesel; - /* handle_keymouse_buttons will move one button at a time */ - handle_keymouse_buttons(key, &whicht, &whichc, real_r_tools); + 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); - if ((key == SDLK_ESCAPE || key == SDLK_AC_BACK) && !disable_quit) - { - magic_switchout(canvas); - done = do_quit(cur_tool); - if (!done) - { - magic_switchin(canvas); + if ((key == SDLK_ESCAPE || key == SDLK_AC_BACK) && !disable_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) - { - 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) - { - printf("modstate at mainloop %d, mod %d\n", SDL_GetModState(), mod); + if (use_sound) + { + printf("modstate at mainloop %d, mod %d\n", SDL_GetModState(), mod); - 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 (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_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]) - { - 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 */ + if (tool_avail[TOOL_REDO]) + { + hide_blinking_cursor(); + do_redo(); + update_screen_rect(&r_tools); + shape_tool_mode = SHAPE_TOOL_MODE_DONE; + } - magic_switchout(canvas); + magic_switchin(canvas); + } + else if (key == SDLK_o && (mod & KMOD_CTRL) && !noshortcuts) + { + /* Ctrl-O - Open */ - disable_avail_tools(); - draw_toolbar(); - draw_colors(COLORSEL_CLOBBER_WIPE); - draw_none(); + magic_switchout(canvas); - if (do_open() == 0) - { - if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - do_render_cur_text(0); - } + disable_avail_tools(); + draw_toolbar(); + draw_colors(COLORSEL_CLOBBER_WIPE); + draw_none(); - enable_avail_tools(); + if (do_open() == 0) + { + if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + do_render_cur_text(0); + } - draw_toolbar(); - update_screen_rect(&r_tools); - draw_colors(COLORSEL_REFRESH); + enable_avail_tools(); - 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); - } + draw_toolbar(); + update_screen_rect(&r_tools); + draw_colors(COLORSEL_REFRESH); - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput (); - } - } - else if (cur_tool == TOOL_SHAPES) - draw_shapes(); - else if (cur_tool == TOOL_ERASER) - draw_erasers(); + 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); + } - draw_tux_text(TUX_GREAT, tool_tips[cur_tool], 1); + if (onscreen_keyboard && !kbd) + { + SDL_StartTextInput(); + } + } + else if (cur_tool == TOOL_SHAPES) + draw_shapes(); + else if (cur_tool == TOOL_ERASER) + draw_erasers(); - /* FIXME: Make delay configurable: */ - control_drawtext_timer(1000, tool_tips[cur_tool], 0); - - magic_switchin(canvas); - } - else if ((key == SDLK_n && (mod & KMOD_CTRL)) && !noshortcuts) - { - /* Ctrl-N - New */ + draw_tux_text(TUX_GREAT, tool_tips[cur_tool], 1); - magic_switchout(canvas); + /* FIXME: Make delay configurable: */ + control_drawtext_timer(1000, tool_tips[cur_tool], 0); - hide_blinking_cursor(); - shape_tool_mode = SHAPE_TOOL_MODE_DONE; + magic_switchin(canvas); + } + else if ((key == SDLK_n && (mod & KMOD_CTRL)) && !noshortcuts) + { + /* Ctrl-N - New */ - disable_avail_tools(); - draw_toolbar(); - draw_colors(COLORSEL_CLOBBER_WIPE); - draw_none(); + magic_switchout(canvas); - 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); - } + hide_blinking_cursor(); + shape_tool_mode = SHAPE_TOOL_MODE_DONE; - enable_avail_tools(); + disable_avail_tools(); + draw_toolbar(); + draw_colors(COLORSEL_CLOBBER_WIPE); + draw_none(); - draw_toolbar(); - update_screen_rect(&r_tools); - draw_colors(COLORSEL_REFRESH); + if (do_new_dialog() == 0) + { + draw_tux_text(tool_tux[TUX_DEFAULT], TIP_NEW_ABORT, 1); - 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_TEXT || cur_tool == TOOL_LABEL) + do_render_cur_text(0); + } - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput (); - } - } - else if (cur_tool == TOOL_SHAPES) - draw_shapes(); - else if (cur_tool == TOOL_ERASER) - draw_erasers(); - - 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 */ + enable_avail_tools(); - magic_switchout(canvas); - hide_blinking_cursor(); + draw_toolbar(); + update_screen_rect(&r_tools); + draw_colors(COLORSEL_REFRESH); - if (do_save(cur_tool, 0, 0)) - { - /* Only think it's been saved if it HAS been saved :^) */ + 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); + } - been_saved = 1; - tool_avail[TOOL_SAVE] = 0; - } + if (onscreen_keyboard && !kbd) + { + SDL_StartTextInput(); + } + } + else if (cur_tool == TOOL_SHAPES) + draw_shapes(); + else if (cur_tool == TOOL_ERASER) + draw_erasers(); - 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); - } + 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 */ - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput (); - } - } + magic_switchout(canvas); + hide_blinking_cursor(); - magic_switchin(canvas); - } + 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; + } + + 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(); + } + } + + 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 (!disable_print) + { + magic_switchout(canvas); - /* 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))) - { - /* 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))) + { + /* 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)) - { - 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)) + { + static int redraw = 0; + wchar_t *im_cp = im_data.s; #ifdef DEBUG -// key_down = key; -// key_unicode = event.key.keysym.unicode; -// printf( -// "character 0x%04x %d <%c> is %d pixels, %sprintable, key_down 0x%x\n", -// (unsigned)event.key.keysym.unicode, -// (int)event.key.keysym.unicode, -// (key_unicode>' ' && key_unicode<127)?(char)event.key.keysym.unicode:' ', +// key_down = key; +// key_unicode = event.key.keysym.unicode; +// printf( +// "character 0x%04x %d <%c> is %d pixels, %sprintable, key_down 0x%x\n", +// (unsigned)event.key.keysym.unicode, +// (int)event.key.keysym.unicode, +// (key_unicode>' ' && key_unicode<127)?(char)event.key.keysym.unicode:' ', // (int)charsize(event.key.keysym.unicode), // iswprint(key_unicode)?"":"not ", // (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 - /* 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); - - /* Queue each character to be displayed */ - while(*im_cp) { - if (*im_cp == L'\b') - { - 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); - - if (been_saved) - { - been_saved = 0; - - if (!disable_save) - tool_avail[TOOL_SAVE] = 1; - - draw_toolbar(); - update_screen_rect(&r_tools); - } - - } - } - else if (*im_cp == L'\r') - { - int font_height; - font_height = TuxPaint_Font_FontHeight(getfonthandle(cur_font)); - - hide_blinking_cursor(); - if (texttool_len > 0) - { - 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 (!disable_save) - tool_avail[TOOL_SAVE] = 1; - - draw_toolbar(); - update_screen_rect(&r_tools); - } - - - cursor_x = cursor_left; - cursor_y = min(cursor_y + font_height, canvas->h - font_height); - - playsound(screen, 0, SND_RETURN, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - - } - 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; -// playsound(screen, 0, SND_DELETE_LABEL, 0, SNDPOS_CENTER); // FIXME lack of specific sound - - if (been_saved) - { - been_saved = 0; - - if (!disable_save) - tool_avail[TOOL_SAVE] = 1; - - draw_toolbar(); - update_screen_rect(&r_tools); - } - } - - /* Select a node to edit */ - else if (cur_tool == TOOL_LABEL && - cur_label == LABEL_SELECT) - { - 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) - { - cur_label = LABEL_LABEL; - cur_thing=label_node_to_edit->save_cur_font; - do_setcursor(cursor_insertion); - i = 0; - label_node_to_edit->is_enabled = FALSE; - derender_node(&label_node_to_edit); - - texttool_len = select_texttool_len; - while(i < texttool_len) - { - texttool_str[i] = select_texttool_str[i]; - i = i+1; - } - texttool_str[i] = L'\0'; - cur_color = select_color; - old_x = select_x; - old_y = select_y; - cur_font = select_cur_font; - text_state = select_text_state; - text_size = select_text_size; - // int j; - 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; - } - } - draw_fonts(); - update_screen_rect(&r_toolopt); - - cursor_x = old_x; - cursor_y = old_y; - cursor_left = old_x; - - draw_colors(COLORSEL_REFRESH); - draw_fonts(); - } - - + /* Korean Hangul needs this to refresh when buffered chars gets emptied */ + if (!*im_cp) do_render_cur_text(0); - - } - else - { - cursor_x = cursor_left; - cursor_y = min(cursor_y + font_height, canvas->h - font_height); - } -#ifdef SPEECH -#ifdef __APPLE__ - if (use_sound) - speak_string(texttool_str); -#endif -#endif - im_softreset(&im_data); - } - else if (*im_cp == L'\t') - { + /* Queue each character to be displayed */ + while (*im_cp) + { + if (*im_cp == L'\b') + { + 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 (texttool_len > 0) - { - 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); - } + 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); - } - } - 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; + draw_toolbar(); + update_screen_rect(&r_tools); + } + + } + } + else if (*im_cp == L'\r') + { + int font_height; + + font_height = TuxPaint_Font_FontHeight(getfonthandle(cur_font)); + + hide_blinking_cursor(); + if (texttool_len > 0) + { + 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 (!disable_save) + tool_avail[TOOL_SAVE] = 1; + + draw_toolbar(); + update_screen_rect(&r_tools); + } + + + cursor_x = cursor_left; + cursor_y = min(cursor_y + font_height, canvas->h - font_height); + + playsound(screen, 0, SND_RETURN, 1, SNDPOS_RIGHT, SNDDIST_NEAR); + + } + 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; // playsound(screen, 0, SND_DELETE_LABEL, 0, SNDPOS_CENTER); // FIXME lack of specific sound - 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); - } - } - /* Cycle accross the nodes */ - else if (cur_tool == TOOL_LABEL && - cur_label == LABEL_SELECT) - { - cycle_highlighted_label_node(); - highlight_label_nodes(); - } + draw_toolbar(); + update_screen_rect(&r_tools); + } + } + + /* Select a node to edit */ + else if (cur_tool == TOOL_LABEL && cur_label == LABEL_SELECT) + { + 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) + { + cur_label = LABEL_LABEL; + cur_thing = label_node_to_edit->save_cur_font; + do_setcursor(cursor_insertion); + i = 0; + label_node_to_edit->is_enabled = FALSE; + derender_node(&label_node_to_edit); + + texttool_len = select_texttool_len; + while (i < texttool_len) + { + texttool_str[i] = select_texttool_str[i]; + i = i + 1; + } + texttool_str[i] = L'\0'; + cur_color = select_color; + old_x = select_x; + old_y = select_y; + cur_font = select_cur_font; + text_state = select_text_state; + text_size = select_text_size; + // int j; + 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; + } + } + draw_fonts(); + update_screen_rect(&r_toolopt); + + cursor_x = old_x; + cursor_y = old_y; + cursor_left = old_x; + + draw_colors(COLORSEL_REFRESH); + draw_fonts(); + } - + do_render_cur_text(0); + + } + else + { + cursor_x = cursor_left; + cursor_y = min(cursor_y + font_height, canvas->h - font_height); + } + #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 +#endif + im_softreset(&im_data); + } + else if (*im_cp == L'\t') + { + + if (texttool_len > 0) + { + 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 (!disable_save) + tool_avail[TOOL_SAVE] = 1; + + draw_toolbar(); + update_screen_rect(&r_tools); + } + } + 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; +// playsound(screen, 0, SND_DELETE_LABEL, 0, SNDPOS_CENTER); // FIXME lack of specific sound + + if (been_saved) + { + been_saved = 0; + + if (!disable_save) + tool_avail[TOOL_SAVE] = 1; + + draw_toolbar(); + update_screen_rect(&r_tools); + } + } + /* Cycle accross the nodes */ + else if (cur_tool == TOOL_LABEL && cur_label == LABEL_SELECT) + { + cycle_highlighted_label_node(); + highlight_label_nodes(); + } + + + +#ifdef SPEECH +#ifdef __APPLE__ + 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 #ifndef __ANDROID__ - if (!iswprint(*im_cp)) - break; + if (!iswprint(*im_cp)) + break; #endif - 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); + 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); + } + + + 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); + } + + } + } + } + + 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) + { + /* 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) + 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) + { + 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 - 96, (48 * 7) + 40 + HEIGHTOFFSET); + 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_tux_text(tool_tux[cur_tool], tool_tips[cur_tool], 1); - 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...? */ + /* Draw items for this tool: */ - playsound(screen, 0, SND_KEYCLICK, 0, SNDPOS_CENTER, - SNDDIST_NEAR); - } - } - } + 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])); + set_active_stamp(); + update_stamp_xor(); + } + 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_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) + { + kbd_rect.x = button_w * 2 + (canvas->w - kbd->surface->w) / 2; + if (old_y > canvas->h / 2) + kbd_rect.y = 0; + else + kbd_rect.y = canvas->h - kbd->surface->h; + 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); + } - im_cp++; - } /* while(*im_cp) */ + if (onscreen_keyboard && !kbd) + { + SDL_StartTextInput(); + } + if (!font_thread_done) + { + draw_colors(COLORSEL_DISABLE); + draw_none(); + update_screen_rect(&r_toolopt); + update_screen_rect(&r_ttoolopt); + do_setcursor(cursor_watch); - /* Show IM tip text */ - if(im_data.tip_text) { - draw_tux_text(TUX_DEFAULT, im_data.tip_text, 1); - } + /* Wait while Text tool finishes loading fonts */ + draw_tux_text(TUX_WAIT, gettext("Please wait…"), 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) - { - /* 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) - 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) - { - 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 - 96, (48 * 7) + 40 + HEIGHTOFFSET); - 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_tux_text(tool_tux[cur_tool], tool_tips[cur_tool], 1); - - /* 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])); - set_active_stamp(); - update_stamp_xor(); - } - 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_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) - { - kbd_rect.x = button_w * 2 + (canvas->w - kbd->surface->w)/2; - if(old_y > canvas->h / 2) - kbd_rect.y = 0; - else - kbd_rect.y = canvas->h - kbd->surface->h; - 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 (onscreen_keyboard && !kbd) - { - SDL_StartTextInput (); - } - 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); - } - draw_tux_text(tool_tux[cur_tool], tool_tips[cur_tool], 1); + set_label_fonts(); + do_setcursor(cursor_arrow); + } + draw_tux_text(tool_tux[cur_tool], tool_tips[cur_tool], 1); - 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; - num_things = num_magics; - thing_scroll = &magic_scroll; - magic_current_snd_ptr = NULL; - draw_magic(); - draw_colors(magics[cur_magic].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; + num_things = num_magics; + thing_scroll = &magic_scroll; + magic_current_snd_ptr = NULL; + draw_magic(); + draw_colors(magics[cur_magic].colors); - if (magics[cur_magic].colors) - magic_funcs[magics[cur_magic].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[cur_magic].colors) + magic_funcs[magics[cur_magic].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; - } - 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; + } + 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; - } - 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; + } + 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); - } + if (do_open() == 0) + { + if (old_tool == TOOL_TEXT || old_tool == TOOL_LABEL) + do_render_cur_text(0); + } - enable_avail_tools(); + enable_avail_tools(); - cur_tool = old_tool; - draw_toolbar(); - update_screen_rect(&r_tools); + cur_tool = old_tool; + draw_toolbar(); + update_screen_rect(&r_tools); - draw_tux_text(TUX_GREAT, tool_tips[cur_tool], 1); + draw_tux_text(TUX_GREAT, tool_tips[cur_tool], 1); - draw_colors(COLORSEL_REFRESH); + 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_SAVE) - { - if (do_save(old_tool, 0, 0)) - { - been_saved = 1; - tool_avail[TOOL_SAVE] = 0; - } + 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_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 (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 (); - } - } + if (onscreen_keyboard && !kbd) + { + SDL_StartTextInput(); + } + } - cur_tool = old_tool; - draw_toolbar(); - update_screen_rect(&r_tools); - } - else if (cur_tool == TOOL_NEW) - { - shape_tool_mode = SHAPE_TOOL_MODE_DONE; + 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(); + disable_avail_tools(); + draw_toolbar(); + draw_colors(COLORSEL_CLOBBER_WIPE); + draw_none(); - if (do_new_dialog() == 0) - { - cur_tool = old_tool; + 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); - } + draw_tux_text(tool_tux[TUX_DEFAULT], TIP_NEW_ABORT, 1); - cur_tool = old_tool; + if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + do_render_cur_text(0); + } - enable_avail_tools(); + cur_tool = old_tool; - draw_toolbar(); - update_screen_rect(&r_tools); - draw_colors(COLORSEL_REFRESH); + enable_avail_tools(); - 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); - } + draw_toolbar(); + update_screen_rect(&r_tools); + draw_colors(COLORSEL_REFRESH); - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput (); + 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); + } - } - } - else if (cur_tool == TOOL_SHAPES) - draw_shapes(); - else if (cur_tool == TOOL_ERASER) - draw_erasers(); - } - 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 (onscreen_keyboard && !kbd) + { + SDL_StartTextInput(); - 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); - } + } + } + else if (cur_tool == TOOL_SHAPES) + draw_shapes(); + else if (cur_tool == TOOL_ERASER) + draw_erasers(); + } + 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 (onscreen_keyboard && !kbd) - { - SDL_StartTextInput (); - } - } + 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); + } - 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 (onscreen_keyboard && !kbd) + { + SDL_StartTextInput(); + } + } - 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); - } + 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 (onscreen_keyboard && !kbd) - { - SDL_StartTextInput (); + 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(); - 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) - { - tool_scroll -= gd_tools.cols; - playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, - SNDDIST_NEAR); + cur_tool = old_tool; + draw_toolbar(); + update_screen_rect(&r_tools); + } + update_screen_rect(&r_toolopt); + update_screen_rect(&r_ttoolopt); + } - draw_toolbar(); - update_screen_rect(&r_tools); + if (!done) + magic_switchin(canvas); + } + else if ((event.button.y < r_tools.y + button_h / 2) && tool_scroll > 0) + { + tool_scroll -= gd_tools.cols; + playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, SNDDIST_NEAR); - } - else if((event.button.y > real_r_tools.y + real_r_tools.h) && (tool_scroll < NUM_TOOLS - 12 - TOOLOFFSET)) - { - tool_scroll += gd_tools.cols; - draw_toolbar(); - playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, - SNDDIST_NEAR); + draw_toolbar(); + update_screen_rect(&r_tools); - update_screen_rect(&r_tools); + } + else if ((event.button.y > real_r_tools.y + real_r_tools.h) + && (tool_scroll < NUM_TOOLS - 12 - TOOLOFFSET)) + { + tool_scroll += gd_tools.cols; + draw_toolbar(); + playsound(screen, 1, SND_SCROLL, 1, SNDPOS_CENTER, SNDDIST_NEAR); - } - } + update_screen_rect(&r_tools); - 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. */ + } + } - // magic_switchout(canvas); + 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) - { - 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; + // magic_switchout(canvas); - grid_dims gd_controls = { 0, 0 }; /* might become 2-by-2 */ - grid_dims gd_items = { 2, 2 }; /* generally becoming 2-by-whatever */ + 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) + { + 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 */ - /* Note set of things we're dealing with */ - /* (stamps, brushes, etc.) */ + gd_controls.rows = 0; + gd_controls.cols = 0; + gd_items.rows = 2; + gd_items.cols = 2; - if (cur_tool == TOOL_STAMP) - { - if (!disable_stamp_controls) - gd_controls = (grid_dims) - { - 3, 2}; /* was 2,2 before adding left/right stamp group buttons -bjk 2007.05.15 */ - else - gd_controls = (grid_dims) - { - 1, 2}; /* was left 0,0 before adding left/right stamp group buttons -bjk 2007.05.03 */ - } - else if (cur_tool == TOOL_TEXT) - { - if (!disable_stamp_controls) - gd_controls = (grid_dims) - { - 2, 2}; - } - else if (cur_tool == TOOL_LABEL) - { - if (!disable_stamp_controls) - gd_controls = (grid_dims) { 3 , 2 }; - else - gd_controls = (grid_dims) { 1 , 2}; - } + /* Note set of things we're dealing with */ + /* (stamps, brushes, etc.) */ - else if (cur_tool == TOOL_MAGIC) - { - if (!disable_magic_controls) - gd_controls = (grid_dims) - { - 1, 2}; - } + 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; + } + } - /* 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; + else if (cur_tool == TOOL_MAGIC) + { + if (!disable_magic_controls) + { + gd_controls.rows = 1; + gd_controls.cols = 2; + } + } - do_draw = 0; + /* 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; - 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; + do_draw = 0; - 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_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_items.x = r_notcontrols.x; - r_items.y = r_notcontrols.y; - r_items.w = r_notcontrols.w; - r_items.h = r_notcontrols.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; - 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; + r_items.x = r_notcontrols.x; + r_items.y = r_notcontrols.y; + r_items.w = r_notcontrols.w; + r_items.h = r_notcontrols.h; - toolopt_changed = 0; + 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 (HIT(r_items)) - { - which = GRIDHIT_GD(r_items, gd_items) + *thing_scroll; + toolopt_changed = 0; - if (which < num_things) - { - toolopt_changed = 1; + if (HIT(r_items)) + { + which = GRIDHIT_GD(r_items, gd_items) + *thing_scroll; + + if (which < num_things) + { + 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)) - { - which = GRIDHIT_GD(r_controls, gd_controls); - if (cur_tool == TOOL_STAMP) - { - /* Stamp controls! */ - int control_sound = -1; + cur_thing = which; + do_draw = 1; + } + } + else if (HIT(r_controls)) + { + which = GRIDHIT_GD(r_controls, gd_controls); + if (cur_tool == TOOL_STAMP) + { + /* 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; -#ifdef DEBUG - float choice; -#endif - - 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 - - 96))) / 96) + - MIN_STAMP_SIZE; + int old_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); + float choice; #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; + 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 - 96))) / 96) + 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); #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 (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: */ - 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; - } + old_stamp_group = stamp_group; - 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 (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 (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(); - } - } - else if (cur_tool == TOOL_MAGIC) - { - /* Magic controls! */ - if (which == 1 && magics[cur_magic].avail_modes & MODE_FULLSCREEN) - { - magic_switchout(canvas); - magics[cur_magic].mode = MODE_FULLSCREEN; - magic_switchin(canvas); - draw_magic(); - update_screen_rect(&r_toolopt); - } - else if (which == 0 && magics[cur_magic].avail_modes & MODE_PAINT) - { - magic_switchout(canvas); - magics[cur_magic].mode = MODE_PAINT; - magic_switchin(canvas); - draw_magic(); - update_screen_rect(&r_toolopt); - } - else if (which == 0 && magics[cur_magic].avail_modes & MODE_PAINT_WITH_PREVIEW) - { - magic_switchout(canvas); - magics[cur_magic].mode = MODE_PAINT_WITH_PREVIEW; - magic_switchin(canvas); - draw_magic(); - update_screen_rect(&r_toolopt); - } - else if (which == 0 && magics[cur_magic].avail_modes & MODE_ONECLICK) - { - magic_switchout(canvas); - magics[cur_magic].mode = MODE_ONECLICK; - magic_switchin(canvas); - draw_magic(); - update_screen_rect(&r_toolopt); - } - /* FIXME: Sfx */ - } - 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 (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(); + } + } + else if (cur_tool == TOOL_MAGIC) + { + /* Magic controls! */ + if (which == 1 && magics[cur_magic].avail_modes & MODE_FULLSCREEN) + { + magic_switchout(canvas); + magics[cur_magic].mode = MODE_FULLSCREEN; + magic_switchin(canvas); + draw_magic(); + update_screen_rect(&r_toolopt); + } + else if (which == 0 && magics[cur_magic].avail_modes & MODE_PAINT) + { + magic_switchout(canvas); + magics[cur_magic].mode = MODE_PAINT; + magic_switchin(canvas); + draw_magic(); + update_screen_rect(&r_toolopt); + } + else if (which == 0 && magics[cur_magic].avail_modes & MODE_PAINT_WITH_PREVIEW) + { + magic_switchout(canvas); + magics[cur_magic].mode = MODE_PAINT_WITH_PREVIEW; + magic_switchin(canvas); + draw_magic(); + update_screen_rect(&r_toolopt); + } + else if (which == 0 && magics[cur_magic].avail_modes & MODE_ONECLICK) + { + magic_switchout(canvas); + magics[cur_magic].mode = MODE_ONECLICK; + magic_switchin(canvas); + draw_magic(); + update_screen_rect(&r_toolopt); + } + /* FIXME: Sfx */ + } + 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 */ + 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); - } - } - } + int i; - /* Label controls! */ - else if(cur_tool == TOOL_LABEL) - { - int control_sound = -1; + 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); + } + } + } - 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) - { - /* Select button: */ - if (cur_label == LABEL_SELECT) - { - cur_label = LABEL_LABEL; - update_canvas(0, 0, WINDOW_WIDTH - 96, (48 * 7) + 40 + HEIGHTOFFSET); - if (onscreen_keyboard) - { - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); - } + /* Label controls! */ + else if (cur_tool == TOOL_LABEL) + { + int control_sound = -1; - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput (); + 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 - { - if (are_labels()) - { - update_canvas_ex_r(kbd_rect.x- 96, 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; + else + { + /* One of the top buttons: */ + if (which & 1) + { + /* Select button: */ + if (cur_label == LABEL_SELECT) + { + cur_label = LABEL_LABEL; + update_canvas(0, 0, WINDOW_WIDTH - 96, (48 * 7) + 40 + HEIGHTOFFSET); + if (onscreen_keyboard) + { + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); + update_screen_rect(&kbd_rect); + } - } + if (onscreen_keyboard && !kbd) + { + SDL_StartTextInput(); - cur_label = LABEL_SELECT; - highlight_label_nodes(); - } - } - toolopt_changed = 1; - } - } - } + } + } + else + { + if (are_labels()) + { + update_canvas_ex_r(kbd_rect.x - 96, 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; - if (control_sound != -1) - { - playsound(screen, 0, control_sound, 0, SNDPOS_CENTER, - SNDDIST_NEAR); + } + + cur_label = LABEL_SELECT; + highlight_label_nodes(); + } + } + 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 */ + 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); + int i; - } - } - else - { - /* scroll button */ - 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); + 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); - if (scrolltimer != NULL) - { - SDL_RemoveTimer(scrolltimer); - scrolltimer = NULL; - } + } + } + else + { + /* scroll button */ + int is_upper = event.button.y < r_toolopt.y + button_h / 2; - if (!scrolling && event.type == SDL_MOUSEBUTTONDOWN) - { - /* printf("Starting scrolling\n"); */ - memcpy(&scrolltimer_event, &event, sizeof(SDL_Event)); - scrolltimer_event.type = TP_SDL_MOUSEBUTTONSCROLL; + 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); - scrolling = 1; + if (scrolltimer != NULL) + { + SDL_RemoveTimer(scrolltimer); + scrolltimer = NULL; + } - scrolltimer = - SDL_AddTimer(REPEAT_SPEED, scrolltimer_callback, - (void *) &scrolltimer_event); - } - else - { - /* printf("Continuing scrolling\n"); */ - scrolltimer = - SDL_AddTimer(REPEAT_SPEED / 3, scrolltimer_callback, - (void *) &scrolltimer_event); - } + if (!scrolling && event.type == SDL_MOUSEBUTTONDOWN) + { + /* printf("Starting scrolling\n"); */ + memcpy(&scrolltimer_event, &event, sizeof(SDL_Event)); + scrolltimer_event.type = TP_SDL_MOUSEBUTTONSCROLL; - if (*thing_scroll == 0) - { - do_setcursor(cursor_arrow); - if (scrolling) - { - if (scrolltimer != NULL) - { - SDL_RemoveTimer(scrolltimer); - scrolltimer = NULL; - } - scrolling = 0; - } - } - } - } + scrolling = 1; + + scrolltimer = + SDL_AddTimer(REPEAT_SPEED, scrolltimer_callback, (void *)&scrolltimer_event); + } + else + { + /* printf("Continuing scrolling\n"); */ + scrolltimer = + SDL_AddTimer(REPEAT_SPEED / 3, scrolltimer_callback, (void *)&scrolltimer_event); + } + + if (*thing_scroll == 0) + { + do_setcursor(cursor_arrow); + if (scrolling) + { + if (scrolltimer != NULL) + { + SDL_RemoveTimer(scrolltimer); + scrolltimer = NULL; + } + scrolling = 0; + } + } + } + } - /* Assign the change(s), if any / redraw, if needed: */ + /* 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 (cur_tool == TOOL_BRUSH || cur_tool == TOOL_LINES) + { + cur_brush = cur_thing; + render_brush(); - if (do_draw) - draw_brushes(); - } - else if (cur_tool == TOOL_ERASER) - { - cur_eraser = cur_thing; + if (do_draw) + draw_brushes(); + } + else if (cur_tool == TOOL_ERASER) + { + cur_eraser = cur_thing; - if (do_draw) - draw_erasers(); - } - else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - { - /* FIXME */ /* char font_tux_text[512]; */ + if (do_draw) + draw_erasers(); + } + else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + { + /* FIXME *//* char font_tux_text[512]; */ - cur_font = cur_thing; + cur_font = cur_thing; /* FIXME */ /* @@ -4116,1544 +4111,1524 @@ printf("screenrectr_tools %d, %d, %d, %d\n", r_tools.x, r_tools.y, r_tools.w, r_ draw_tux_text(TUX_GREAT, font_tux_text, 1); */ - if (do_draw) - draw_fonts(); + 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) - { + /* 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 there's an SFX, play it! */ + /* Only play when picking a different stamp */ + if (toolopt_changed && !mute) + { + /* 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... */ + 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); + Mix_PlayChannel(2, stamp_data[stamp_group][cur_thing]->ssnd, 0); - /* If there's a description sound, play it after the SFX! */ + /* 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_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); - } - } - } + 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]) - { - cur_stamp[stamp_group] = cur_thing; - set_active_stamp(); - update_stamp_xor(); - } + if (cur_thing != cur_stamp[stamp_group]) + { + cur_stamp[stamp_group] = cur_thing; + set_active_stamp(); + update_stamp_xor(); + } - if (do_draw) - draw_stamps(); + if (do_draw) + draw_stamps(); - if (stamp_data[stamp_group][cur_stamp[stamp_group]]->stxt != NULL) - { + 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); + 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); + 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) - { - 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; + /* Enable or disable color selector: */ + draw_colors(stamp_colorable(cur_stamp[stamp_group]) + || stamp_tintable(cur_stamp[stamp_group])); + if (!scrolling) + { + 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); - } + /* 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); + } - draw_tux_text(TUX_GREAT, shape_tips[cur_shape], 1); + draw_tux_text(TUX_GREAT, shape_tips[cur_shape], 1); - if (do_draw) - draw_shapes(); - } - else if (cur_tool == TOOL_MAGIC) - { - if (cur_thing != cur_magic) - { - magic_switchout(canvas); + if (do_draw) + draw_shapes(); + } + else if (cur_tool == TOOL_MAGIC) + { + if (cur_thing != cur_magic) + { + magic_switchout(canvas); - cur_magic = cur_thing; - draw_colors(magics[cur_magic].colors); - - if (magics[cur_magic].colors) - magic_funcs[magics[cur_magic].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); - } + cur_magic = cur_thing; + draw_colors(magics[cur_magic].colors); - draw_tux_text(TUX_GREAT, magics[cur_magic].tip[magic_modeint(magics[cur_magic].mode)], 1); + if (magics[cur_magic].colors) + magic_funcs[magics[cur_magic].handle_idx].set_color(magic_api_struct, + color_hexes[cur_color][0], + color_hexes[cur_color][1], + color_hexes[cur_color][2]); - if (do_draw) - draw_magic(); - } + magic_switchin(canvas); + } - /* 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); + draw_tux_text(TUX_GREAT, magics[cur_magic].tip[magic_modeint(magics[cur_magic].mode)], 1); - if (valid_click(event.button.button)) - { - // magic_switchout(canvas); + if (do_draw) + draw_magic(); + } - if (whichc >= 0 && whichc < NUM_COLORS) - { - cur_color = whichc; - draw_tux_text(TUX_KISS, color_names[cur_color], 1); - - if (cur_color == (unsigned) (NUM_COLORS - 1) || cur_color == (unsigned) (NUM_COLORS - 2)) - { - disable_avail_tools(); - draw_toolbar(); - draw_colors(COLORSEL_CLOBBER_WIPE); - draw_none(); - - if (cur_color == (unsigned) (NUM_COLORS - 1)) - do_color_picker(); - else - do_color_sel(); - - 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_tux_text(TUX_GREAT, tool_tips[cur_tool], 1); - - 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) - 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(); - - playsound(screen, 1, SND_BUBBLE, 1, SNDPOS_CENTER, SNDDIST_NEAR); - - SDL_Flip(screen); - } - else - { - draw_colors(COLORSEL_REFRESH); - - playsound(screen, 1, SND_BUBBLE, 1, event.button.x, SNDDIST_NEAR); - } - - 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[cur_magic].handle_idx].set_color( - magic_api_struct, - color_hexes[cur_color][0], - color_hexes[cur_color][1], - color_hexes[cur_color][2]); - } - } - } - else if (HIT(r_canvas) && valid_click(event.button.button) && keyglobal == 0) - { - /* 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_ctr_x = old_x; - shape_ctr_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_ctr_x, shape_ctr_y, shape_outer_x, shape_outer_y, - shape_rotation(shape_ctr_x, shape_ctr_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, tool_tips[TOOL_SHAPES], 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) - { - 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[cur_magic].handle_idx].click(magic_api_struct, - magics[cur_magic].idx, - magics[cur_magic].mode, - canvas, last, - old_x, old_y, - &update_rect); - - draw_tux_text(TUX_GREAT, magics[cur_magic].tip[magic_modeint(magics[cur_magic].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[cur_magic].mode != MODE_FULLSCREEN && magics[cur_magic].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); - - if (mouseaccessibility) - emulate_button_pressed = !emulate_button_pressed; - } - else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) - { - /* Text and Label Tools! */ - if(cur_tool == TOOL_LABEL && cur_label == LABEL_SELECT) - { - label_node_to_edit=search_label_list(&highlighted_label_node, old_x, old_y, 0); - if(label_node_to_edit) - { - cur_label = LABEL_LABEL; - cur_thing=label_node_to_edit->save_cur_font; - do_setcursor(cursor_insertion); - i = 0; - label_node_to_edit->is_enabled = FALSE; - derender_node(&label_node_to_edit); - - texttool_len = select_texttool_len; - while(i < texttool_len) - { - texttool_str[i] = select_texttool_str[i]; - i = i+1; - } - texttool_str[i] = L'\0'; - cur_color = select_color; - old_x = select_x; - old_y = select_y; - cur_font = select_cur_font; - text_state = select_text_state; - text_size = select_text_size; - // int j; - 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; + /* Update the screen: */ + if (do_draw) + update_screen_rect(&r_toolopt); } - } - draw_fonts(); - update_screen_rect(&r_toolopt); - if (onscreen_keyboard && kbd) - { - if (old_y < r_canvas.h/2) - kbd_rect.y = r_canvas.h - kbd->surface->h; - else - kbd_rect.y = 0; - - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); - } - - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput (); - } - do_render_cur_text(0); - draw_colors(COLORSEL_REFRESH); - draw_fonts(); } - - } - else - hide_blinking_cursor(); + else if (HIT(r_colors) && colors_are_selectable) + { + /* Color! */ + whichc = GRIDHIT_GD(r_colors, gd_colors); - - - 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)) - { - 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 */ - kbd_rect.x = button_w * 2 + (canvas->w - kbd->surface->w)/2; - if(kbd_rect.y != 0) - kbd_rect.y = canvas->h - kbd->surface->h; - 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); - } - else - { - cursor_x = old_x; - cursor_y = old_y; - cursor_left = old_x; + if (valid_click(event.button.button)) + { + // magic_switchout(canvas); - if (onscreen_keyboard && kbd && !(cur_tool == TOOL_LABEL && cur_label == LABEL_SELECT)) - { - if (old_y < r_canvas.h/2) - { - if (kbd_rect.y != r_canvas.h - kbd->surface->h) - { - 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); - kbd_rect.y = r_canvas.h - kbd->surface->h; - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); - } - } - else - { - if (kbd_rect.y != 0) - { - 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); - kbd_rect.y = 0; - SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); - update_screen_rect(&kbd_rect); - } - } - } + if (whichc >= 0 && whichc < NUM_COLORS) + { + cur_color = whichc; + draw_tux_text(TUX_KISS, color_names[cur_color], 1); - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput (); - } - } + if (cur_color == (unsigned)(NUM_COLORS - 1) || cur_color == (unsigned)(NUM_COLORS - 2)) + { + disable_avail_tools(); + draw_toolbar(); + draw_colors(COLORSEL_CLOBBER_WIPE); + draw_none(); - do_render_cur_text(0); - - } + if (cur_color == (unsigned)(NUM_COLORS - 1)) + do_color_picker(); + else + do_color_sel(); - button_down = 1; - } - else if (HIT(r_sfx) && valid_click(event.button.button)) - { - /* A sound player button on the lower left has been pressed! */ + 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_tux_text(TUX_GREAT, tool_tips[cur_tool], 1); + + 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) + 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(); + + playsound(screen, 1, SND_BUBBLE, 1, SNDPOS_CENTER, SNDDIST_NEAR); + + SDL_Flip(screen); + } + else + { + draw_colors(COLORSEL_REFRESH); + + playsound(screen, 1, SND_BUBBLE, 1, event.button.x, SNDDIST_NEAR); + } + + 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[cur_magic].handle_idx].set_color(magic_api_struct, + color_hexes[cur_color][0], + color_hexes[cur_color][1], + color_hexes[cur_color][2]); + } + } + } + else if (HIT(r_canvas) && valid_click(event.button.button) && keyglobal == 0) + { + /* 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_ctr_x = old_x; + shape_ctr_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_ctr_x, shape_ctr_y, shape_outer_x, shape_outer_y, + shape_rotation(shape_ctr_x, shape_ctr_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, tool_tips[TOOL_SHAPES], 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) + { + 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[cur_magic].handle_idx].click(magic_api_struct, + magics[cur_magic].idx, + magics[cur_magic].mode, + canvas, last, old_x, old_y, &update_rect); + + draw_tux_text(TUX_GREAT, magics[cur_magic].tip[magic_modeint(magics[cur_magic].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[cur_magic].mode != MODE_FULLSCREEN && magics[cur_magic].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); + + if (mouseaccessibility) + emulate_button_pressed = !emulate_button_pressed; + } + else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) + { + /* Text and Label Tools! */ + if (cur_tool == TOOL_LABEL && cur_label == LABEL_SELECT) + { + label_node_to_edit = search_label_list(&highlighted_label_node, old_x, old_y, 0); + if (label_node_to_edit) + { + cur_label = LABEL_LABEL; + cur_thing = label_node_to_edit->save_cur_font; + do_setcursor(cursor_insertion); + i = 0; + label_node_to_edit->is_enabled = FALSE; + derender_node(&label_node_to_edit); + + texttool_len = select_texttool_len; + while (i < texttool_len) + { + texttool_str[i] = select_texttool_str[i]; + i = i + 1; + } + texttool_str[i] = L'\0'; + cur_color = select_color; + old_x = select_x; + old_y = select_y; + cur_font = select_cur_font; + text_state = select_text_state; + text_size = select_text_size; + // int j; + 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; + } + } + draw_fonts(); + update_screen_rect(&r_toolopt); + if (onscreen_keyboard && kbd) + { + if (old_y < r_canvas.h / 2) + kbd_rect.y = r_canvas.h - kbd->surface->h; + else + kbd_rect.y = 0; + + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); + update_screen_rect(&kbd_rect); + } + + if (onscreen_keyboard && !kbd) + { + SDL_StartTextInput(); + } + do_render_cur_text(0); + draw_colors(COLORSEL_REFRESH); + draw_fonts(); + } + + } + 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)) + { + 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 */ + kbd_rect.x = button_w * 2 + (canvas->w - kbd->surface->w) / 2; + if (kbd_rect.y != 0) + kbd_rect.y = canvas->h - kbd->surface->h; + 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); + } + else + { + cursor_x = old_x; + cursor_y = old_y; + cursor_left = old_x; + + if (onscreen_keyboard && kbd && !(cur_tool == TOOL_LABEL && cur_label == LABEL_SELECT)) + { + if (old_y < r_canvas.h / 2) + { + if (kbd_rect.y != r_canvas.h - kbd->surface->h) + { + 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); + kbd_rect.y = r_canvas.h - kbd->surface->h; + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); + update_screen_rect(&kbd_rect); + } + } + else + { + if (kbd_rect.y != 0) + { + 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); + kbd_rect.y = 0; + SDL_BlitSurface(kbd->surface, &kbd->rect, screen, &kbd_rect); + update_screen_rect(&kbd_rect); + } + } + } + + if (onscreen_keyboard && !kbd) + { + 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 (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: */ + 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); - } + 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); - } + 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) - { - - /* Left tools scroll */ - 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 */ - else - { - grid_dims gd_controls = { 0, 0 }; /* might become 2-by-2 */ - grid_dims gd_items = { 2, 2 }; /* generally becoming 2-by-whatever */ - - /* Note set of things we're dealing with */ - /* (stamps, brushes, etc.) */ - - if (cur_tool == TOOL_STAMP) - { - if (!disable_stamp_controls) - gd_controls = (grid_dims) - { - 3, 2}; /* was 2,2 before adding left/right stamp group buttons -bjk 2007.05.15 */ - else - gd_controls = (grid_dims) - { - 1, 2}; /* was left 0,0 before adding left/right stamp group buttons -bjk 2007.05.03 */ - } - else if (cur_tool == TOOL_TEXT) - { - if (!disable_stamp_controls) - gd_controls = (grid_dims) - { - 2, 2}; - } - else if(cur_tool == TOOL_LABEL) - { - if (!disable_stamp_controls) - gd_controls = (grid_dims){ 3 , 2 }; - else - gd_controls = (grid_dims) { 1 , 2}; + start_motion_convert(event); +#endif } - else if (cur_tool == TOOL_MAGIC) - { - if (!disable_magic_controls) - gd_controls = (grid_dims) + + else if (event.type == SDL_MOUSEWHEEL && wheely) { - 1, 2}; - } + int most = 14; + int num_rows_needed; + int xpos, ypos; + SDL_Rect r_controls; + SDL_Rect r_notcontrols; + SDL_Rect r_items; /* = r_notcontrols; */ - /* 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; + SDL_GetMouseState(&xpos, &ypos); - do_draw = 0; + /* Scroll wheel code. + WARNING: this must be kept in sync with the mouse-move + code (for cursor changes) and mouse-click code. */ - 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; + 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) + { - 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; + /* Left tools scroll */ + if (hit_test(&r_tools, xpos, ypos) && NUM_TOOLS > most + TOOLOFFSET) + { + int is_upper = (event.wheel.y > 0); - 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 (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 (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 (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); + } - if (0) - { - } - else - { - /* scroll button */ - int is_upper = (event.wheel.y > (Sint32)0); + 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); + } - 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); - } - } - } + 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 */ + 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 = 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: */ + /* 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_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(); - } + 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_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: */ + /* Update the screen: */ + if (do_draw) + update_screen_rect(&r_toolopt); - 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 + else if (event.type == SDL_USEREVENT) { - 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) */ + if (event.user.code == USEREVENT_TEXT_UPDATE) + { + /* Time to replace "Great!" with old tip text: */ - debug("Playing description sound..."); + 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! */ + 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); + 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 + } } - } -#endif - } - } - else if (event.type == SDL_MOUSEBUTTONUP) - { - if (scrolling) - { - if (scrolltimer != NULL) - { - SDL_RemoveTimer(scrolltimer); - scrolltimer = NULL; - } - scrolling = 0; - - /* printf("Killing 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(old_x >= 0 && - old_y >= 0 && - old_x <= r_canvas.w && - old_y <= r_canvas.h) - { - /* Draw a stamp! */ - - rec_undo_buffer(); - - stamp_draw(old_x, old_y); - stamp_xor(old_x, old_y); - playsound(screen, 1, SND_STAMP, 1, event.button.x, SNDDIST_NEAR); - - 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 (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)) + else if (event.type == SDL_MOUSEBUTTONUP) { - if (shape_tool_mode == SHAPE_TOOL_MODE_STRETCH) - { - /* Now we can rotate the shape... */ + if (scrolling) + { + if (scrolltimer != NULL) + { + SDL_RemoveTimer(scrolltimer); + scrolltimer = NULL; + } + scrolling = 0; - shape_outer_x = event.button.x - r_canvas.x; - shape_outer_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_ctr_x - shape_outer_x) * (shape_ctr_x - shape_outer_x) + (shape_ctr_y - shape_outer_y) * (shape_ctr_y - shape_outer_y)); - - SDL_WarpMouse(shape_outer_x + 96, shape_ctr_y); - do_setcursor(cursor_rotate); + /* printf("Killing 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); + } - /* Erase stretchy XOR: */ + 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 (old_x >= 0 && old_y >= 0 && old_x <= r_canvas.w && old_y <= r_canvas.h) + { + /* Draw a stamp! */ - if (abs(shape_ctr_x - shape_outer_x) > 15 || - abs(shape_ctr_y - shape_outer_y) > 15) - do_shape(shape_ctr_x, shape_ctr_y, old_x, old_y, 0, 0); + rec_undo_buffer(); - /* Make an initial rotation XOR to be erased: */ + stamp_draw(old_x, old_y); + stamp_xor(old_x, old_y); + playsound(screen, 1, SND_STAMP, 1, event.button.x, SNDDIST_NEAR); - do_shape(shape_ctr_x, shape_ctr_y, - shape_outer_x, shape_outer_y, - shape_rotation(shape_ctr_x, shape_ctr_y, - shape_outer_x, shape_outer_y), 0); + draw_tux_text(TUX_GREAT, great_str(), 1); - playsound(screen, 1, SND_LINE_START, 1, event.button.x, - SNDDIST_NEAR); - draw_tux_text(TUX_BORED, TIP_SHAPE_NEXT, 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 (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_outer_x = event.button.x - r_canvas.x; + shape_outer_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_ctr_x - shape_outer_x) * (shape_ctr_x - shape_outer_x) + + (shape_ctr_y - shape_outer_y) * (shape_ctr_y - shape_outer_y)); + + SDL_WarpMouse(shape_outer_x + 96, shape_ctr_y); + do_setcursor(cursor_rotate); - /* FIXME: Do something less intensive! */ + /* Erase stretchy XOR: */ - SDL_Flip(screen); - } - else - { - reset_brush_counter(); + if (abs(shape_ctr_x - shape_outer_x) > 15 || abs(shape_ctr_y - shape_outer_y) > 15) + do_shape(shape_ctr_x, shape_ctr_y, old_x, old_y, 0, 0); + + /* Make an initial rotation XOR to be erased: */ + + do_shape(shape_ctr_x, shape_ctr_y, + shape_outer_x, shape_outer_y, + shape_rotation(shape_ctr_x, shape_ctr_y, shape_outer_x, shape_outer_y), 0); + + playsound(screen, 1, SND_LINE_START, 1, event.button.x, SNDDIST_NEAR); + draw_tux_text(TUX_BORED, TIP_SHAPE_NEXT, 1); - playsound(screen, 1, SND_LINE_END, 1, event.button.x, - SNDDIST_NEAR); - do_shape(shape_ctr_x, shape_ctr_y, shape_outer_x, - shape_outer_y, 0, 1); + /* FIXME: Do something less intensive! */ - SDL_Flip(screen); + SDL_Flip(screen); + } + else + { + reset_brush_counter(); - shape_tool_mode = SHAPE_TOOL_MODE_DONE; - draw_tux_text(TUX_GREAT, tool_tips[TOOL_SHAPES], 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_ctr_x, shape_ctr_y, shape_outer_x, shape_outer_y, - shape_rotation(shape_ctr_x, shape_ctr_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); + do_shape(shape_ctr_x, shape_ctr_y, shape_outer_x, shape_outer_y, 0, 1); - shape_tool_mode = SHAPE_TOOL_MODE_DONE; - draw_tux_text(TUX_GREAT, tool_tips[TOOL_SHAPES], 1); - } - } - } - else if (cur_tool == TOOL_MAGIC && (magics[cur_magic].mode == MODE_PAINT || magics[cur_magic].mode == MODE_ONECLICK || magics[cur_magic].mode == MODE_PAINT_WITH_PREVIEW)) - { - if(!mouseaccessibility || (mouseaccessibility && !emulate_button_pressed)) - { - int undo_ctr; - SDL_Surface * last; + SDL_Flip(screen); - /* Releasing button: Finish the magic: */ + shape_tool_mode = SHAPE_TOOL_MODE_DONE; + draw_tux_text(TUX_GREAT, tool_tips[TOOL_SHAPES], 1); + } + } + else if (shape_tool_mode == SHAPE_TOOL_MODE_ROTATE) + { + reset_brush_counter(); - if (cur_undo > 0) - undo_ctr = cur_undo - 1; - else - undo_ctr = NUM_UNDO_BUFS - 1; + playsound(screen, 1, SND_LINE_END, 1, event.button.x, SNDDIST_NEAR); + do_shape(shape_ctr_x, shape_ctr_y, shape_outer_x, shape_outer_y, + shape_rotation(shape_ctr_x, shape_ctr_y, + event.button.x - r_canvas.x, event.button.y - r_canvas.y), 1); - last = undo_bufs[undo_ctr]; - - update_rect.x = 0; - update_rect.y = 0; - update_rect.w = 0; - update_rect.h = 0; + shape_tool_mode = SHAPE_TOOL_MODE_DONE; + draw_tux_text(TUX_GREAT, tool_tips[TOOL_SHAPES], 1); + } + } + } + else if (cur_tool == TOOL_MAGIC + && (magics[cur_magic].mode == MODE_PAINT || magics[cur_magic].mode == MODE_ONECLICK + || magics[cur_magic].mode == MODE_PAINT_WITH_PREVIEW)) + { + if (!mouseaccessibility || (mouseaccessibility && !emulate_button_pressed)) + { + int undo_ctr; + SDL_Surface *last; - magic_funcs[magics[cur_magic].handle_idx].release(magic_api_struct, - magics[cur_magic].idx, - canvas, last, - old_x, old_y, - &update_rect); - - draw_tux_text(TUX_GREAT, magics[cur_magic].tip[magic_modeint(magics[cur_magic].mode)], 1); + /* Releasing button: Finish the magic: */ - 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_TEXT || - (cur_tool == TOOL_LABEL && cur_label != LABEL_SELECT)) - { - 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 (cur_undo > 0) + undo_ctr = cur_undo - 1; + else + undo_ctr = NUM_UNDO_BUFS - 1; - if (onscreen_keyboard && !kbd) - { - SDL_StartTextInput (); - } - } - } - button_down = 0; + last = undo_bufs[undo_ctr]; + + update_rect.x = 0; + update_rect.y = 0; + update_rect.w = 0; + update_rect.h = 0; + + magic_funcs[magics[cur_magic].handle_idx].release(magic_api_struct, + magics[cur_magic].idx, + canvas, last, old_x, old_y, &update_rect); + + draw_tux_text(TUX_GREAT, magics[cur_magic].tip[magic_modeint(magics[cur_magic].mode)], 1); + + 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_TEXT || (cur_tool == TOOL_LABEL && cur_label != LABEL_SELECT)) + { + 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(); + } + } + } + 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; + 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); + convert_motion_to_wheel(event); #endif - oldpos_x = event.motion.x; - oldpos_y = event.motion.y; + oldpos_x = event.motion.x; + oldpos_y = event.motion.y; - /* FIXME: Is doing this every event too intensive? */ - /* Should I check current cursor first? */ + /* FIXME: Is doing this every event too intensive? */ + /* Should I check current cursor first? */ - if (HIT(r_tools)) - { - int most = 14; - /* Tools: */ + if (HIT(r_tools)) + { + int most = 14; - if (NUM_TOOLS > most + TOOLOFFSET) - { - 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 - 12 - TOOLOFFSET) - do_setcursor(cursor_down); - else - do_setcursor(cursor_arrow); - } + /* Tools: */ - 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); - } + if (NUM_TOOLS > most + TOOLOFFSET) + { + 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 - 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); + } - 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)) - { - /* mouse cursor code - WARNING: this must be kept in sync with the mouse-click - and mouse-click code. (it isn't, currently!) */ + 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: */ - /* 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) - { - } + 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)) + { + /* mouse cursor code + WARNING: this must be kept in sync with the mouse-click + and mouse-click code. (it isn't, currently!) */ - max = 14; - if (cur_tool == TOOL_STAMP && !disable_stamp_controls) - max = 8; /* was 10 before left/right group buttons -bjk 2007.05.03 */ - if(cur_tool == TOOL_LABEL) - { - max = 12; - if (!disable_stamp_controls) - max = 8; - } - - if (cur_tool == TOOL_TEXT && !disable_stamp_controls) - max = 10; - if (cur_tool == TOOL_MAGIC && !disable_magic_controls) - max = 12; + /* 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) + { + } + + max = 14; + if (cur_tool == TOOL_STAMP && !disable_stamp_controls) + max = 8; /* was 10 before left/right group buttons -bjk 2007.05.03 */ + if (cur_tool == TOOL_LABEL) + { + max = 12; + if (!disable_stamp_controls) + max = 8; + } + + if (cur_tool == TOOL_TEXT && !disable_stamp_controls) + max = 10; + if (cur_tool == TOOL_MAGIC && !disable_magic_controls) + max = 12; - if (num_things > max + TOOLOFFSET) - { - /* Are there scroll buttons? */ - if (event.button.y < 40 + 24) - { - /* Up button; is it available? */ + if (num_things > max + TOOLOFFSET) + { + /* Are there scroll buttons? */ + if (event.button.y < 40 + 24) + { + /* Up button; is it available? */ - if (*thing_scroll > 0) - do_setcursor(cursor_up); - else - do_setcursor(cursor_arrow); - } - else if (event.button.y > - (48 * ((max - 2) / 2 + TOOLOFFSET / 2)) + 40 + 24 - && event.button.y <= - (48 * ((max - 2) / 2 + TOOLOFFSET / 2)) + 40 + 24 + 24) - { - /* Down button; is it available? */ + if (*thing_scroll > 0) + do_setcursor(cursor_up); + else + do_setcursor(cursor_arrow); + } + else if (event.button.y > + (48 * ((max - 2) / 2 + TOOLOFFSET / 2)) + 40 + 24 + && event.button.y <= (48 * ((max - 2) / 2 + TOOLOFFSET / 2)) + 40 + 24 + 24) + { + /* Down button; is it available? */ - if (*thing_scroll < num_things - (max - 2)) - do_setcursor(cursor_down); - else - do_setcursor(cursor_arrow); - } - else - { - /* One of the selectors: */ + if (*thing_scroll < num_things - (max - 2)) + do_setcursor(cursor_down); + else + do_setcursor(cursor_arrow); + } + else + { + /* One of the selectors: */ - which = ((event.button.y - 40 - 24) / 48) * 2 + - (event.button.x - (WINDOW_WIDTH - 96)) / 48; + which = ((event.button.y - 40 - 24) / 48) * 2 + (event.button.x - (WINDOW_WIDTH - 96)) / 48; - if (which < num_things) - do_setcursor(cursor_hand); - else - do_setcursor(cursor_arrow); - } - } - else - { - /* No scroll buttons - must be a selector: */ + if (which < num_things) + do_setcursor(cursor_hand); + else + do_setcursor(cursor_arrow); + } + } + else + { + /* No scroll buttons - must be a selector: */ - which = ((event.button.y - 40) / 48) * 2 + - (event.button.x - (WINDOW_WIDTH - 96)) / 48; + which = ((event.button.y - 40) / 48) * 2 + (event.button.x - (WINDOW_WIDTH - 96)) / 48; - if (which < num_things) - do_setcursor(cursor_hand); - else - do_setcursor(cursor_arrow); - } - } - else if (HIT(r_canvas) && keyglobal == 0) - { - /* Canvas: */ + 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) - do_setcursor(cursor_tiny); - else if (cur_tool == TOOL_LINES) - 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) - { - if (search_label_list(¤t_label_node, event.button.x - 96, event.button.y, 1)) - do_setcursor(cursor_hand); - else - do_setcursor(cursor_arrow); - } - } + if (cur_tool == TOOL_BRUSH) + do_setcursor(cursor_brush); + else if (cur_tool == TOOL_STAMP) + do_setcursor(cursor_tiny); + else if (cur_tool == TOOL_LINES) + 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) + { + if (search_label_list(¤t_label_node, event.button.x - 96, 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); - } + 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: */ + 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); + 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: */ + 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, old_x, old_y); - line_xor(line_start_x, line_start_y, new_x, new_y); + line_xor(line_start_x, line_start_y, new_x, new_y); #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(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); #else - /* Anyway SDL_UpdateRect() backward compatibility function refreshes all the screen on Android */ - SDL_UpdateRect(screen, 0,0,WINDOW_WIDTH,WINDOW_HEIGHT); -#endif - } - 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_ctr_x, shape_ctr_y, old_x, old_y, 0, 0); - - do_shape(shape_ctr_x, shape_ctr_y, new_x, new_y, 0, 0); - - - /* FIXME: Fix update shape function! */ - - /* update_shape(shape_ctr_x, old_x, new_x, - shape_ctr_y, old_y, new_y, - shape_locked[cur_shape]); */ - - SDL_Flip(screen); - } - } - else if (cur_tool == TOOL_MAGIC && (magics[cur_magic].mode == MODE_PAINT || magics[cur_magic].mode == MODE_ONECLICK || magics[cur_magic].mode == MODE_PAINT_WITH_PREVIEW)) - { - int undo_ctr; - SDL_Surface * last; - - /* Pushing button and moving: Continue doing 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[cur_magic].handle_idx].drag(magic_api_struct, - magics[cur_magic].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) - { - /* Still pushing, and moving - Erase! */ - - do_eraser(new_x, new_y); - } - } - - - 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: */ - - - 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; - } - - if (old_x >= 0 && old_x < r_canvas.w && - old_y >= 0 && old_y < r_canvas.h) - { - if (cur_tool == TOOL_STAMP) - { - stamp_xor(old_x, old_y); - - 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 - { - 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_xor(new_x, new_y); - - 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 - { - 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); - } - } - 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; + /* Anyway SDL_UpdateRect() backward compatibility function refreshes all the screen on Android */ + SDL_UpdateRect(screen, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); #endif + } + else if (cur_tool == TOOL_SHAPES) + { + /* Still pushing button, while moving: + Draw XOR where shape will go: */ - old_size = stamp_data[stamp_group][cur_stamp[stamp_group]]->size; - w = CUR_STAMP_W; - h = CUR_STAMP_H; + if (shape_tool_mode == SHAPE_TOOL_MODE_STRETCH) + { + do_shape(shape_ctr_x, shape_ctr_y, old_x, old_y, 0, 0); - 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 - - 96))) / 96) + - 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); -#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(); - 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 && - shape_tool_mode == SHAPE_TOOL_MODE_ROTATE) - { - do_shape(shape_ctr_x, shape_ctr_y, - shape_outer_x, shape_outer_y, - shape_rotation(shape_ctr_x, shape_ctr_y, old_x, old_y), 0); + do_shape(shape_ctr_x, shape_ctr_y, new_x, new_y, 0, 0); - do_shape(shape_ctr_x, shape_ctr_y, - shape_outer_x, shape_outer_y, - shape_rotation(shape_ctr_x, shape_ctr_y, new_x, new_y), 0); + /* FIXME: Fix update shape function! */ + /* update_shape(shape_ctr_x, old_x, new_x, + shape_ctr_y, old_y, new_y, + shape_locked[cur_shape]); */ - /* FIXME: Do something less intensive! */ - SDL_Flip(screen); - } + SDL_Flip(screen); + } + } + else if (cur_tool == TOOL_MAGIC + && (magics[cur_magic].mode == MODE_PAINT || magics[cur_magic].mode == MODE_ONECLICK + || magics[cur_magic].mode == MODE_PAINT_WITH_PREVIEW)) + { + int undo_ctr; + SDL_Surface *last; - old_x = new_x; - old_y = new_y; - oldpos_x = event.button.x; - oldpos_y = event.button.y; - } - } + /* Pushing button and moving: Continue doing the magic: */ - if (cur_tool == TOOL_TEXT || (cur_tool == TOOL_LABEL && cur_label != LABEL_SELECT)) - { - /* if (onscreen_keyboard) */ - /* osk_clicked(kbd, old_x, old_y); */ - /* on_screen_keyboardd(); */ - cur_cursor_blink = SDL_GetTicks(); + if (cur_undo > 0) + undo_ctr = cur_undo - 1; + else + undo_ctr = NUM_UNDO_BUFS - 1; - if( cursor_x != -1 && cursor_y != -1 && - cur_cursor_blink > last_cursor_blink + CURSOR_BLINK_SPEED) - { - last_cursor_blink = SDL_GetTicks(); - draw_blinking_cursor(); + last = undo_bufs[undo_ctr]; + + update_rect.x = 0; + update_rect.y = 0; + update_rect.w = 0; + update_rect.h = 0; + + magic_funcs[magics[cur_magic].handle_idx].drag(magic_api_struct, + magics[cur_magic].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) + { + /* Still pushing, and moving - Erase! */ + + do_eraser(new_x, new_y); + } } + + + 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: */ + + + 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; + } + + if (old_x >= 0 && old_x < r_canvas.w && old_y >= 0 && old_y < r_canvas.h) + { + if (cur_tool == TOOL_STAMP) + { + stamp_xor(old_x, old_y); + + 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 + { + 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_xor(new_x, new_y); + + 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 + { + 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); + } + } + 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; +#endif + + 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 - 96))) / 96) + 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); +#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(); + 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 && shape_tool_mode == SHAPE_TOOL_MODE_ROTATE) + { + do_shape(shape_ctr_x, shape_ctr_y, + shape_outer_x, shape_outer_y, shape_rotation(shape_ctr_x, shape_ctr_y, old_x, old_y), 0); + + + do_shape(shape_ctr_x, shape_ctr_y, + shape_outer_x, shape_outer_y, shape_rotation(shape_ctr_x, shape_ctr_y, new_x, new_y), 0); + + + /* 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) - { - if (onscreen_keyboard && !kbd) - { - SDL_StopTextInput (); - } + if (cur_tool == TOOL_TEXT || (cur_tool == TOOL_LABEL && cur_label != LABEL_SELECT)) + { + /* 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(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(1); - } while (!done); } @@ -5661,9 +5636,9 @@ printf("screenrectr_tools %d, %d, %d, %d\n", r_tools.x, r_tools.y, r_tools.w, r_ static void hide_blinking_cursor(void) { if (cur_toggle_count & 1) - { - draw_blinking_cursor(); - } + { + draw_blinking_cursor(); + } } static void draw_blinking_cursor(void) @@ -5671,14 +5646,12 @@ 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, + cursor_x + r_canvas.x + cursor_textwidth, + cursor_y + r_canvas.y + TuxPaint_Font_FontHeight(getfonthandle(cur_font))); } /* Draw using the current brush: */ @@ -5698,7 +5671,7 @@ static void brush_draw(int x1, int y1, int x2, int y2, int update) frame_w = img_brushes[cur_brush]->w / abs(brushes_frames[cur_brush]); - w = frame_w / (brushes_directional[cur_brush] ? 3: 1); + w = frame_w / (brushes_directional[cur_brush] ? 3 : 1); h = img_brushes[cur_brush]->h / (brushes_directional[cur_brush] ? 3 : 1); x1 = x1 - (w >> 1); @@ -5710,84 +5683,81 @@ static void brush_draw(int x1, int y1, int x2, int y2, int update) direction = BRUSH_DIRECTION_NONE; if (brushes_directional[cur_brush]) - { - r = brush_rotation(x1, y1, x2, y2) + 22; - if (r < 0) - r = r + 360; + { + r = brush_rotation(x1, y1, x2, y2) + 22; + if (r < 0) + r = r + 360; - if (x1 != x2 || y1 != y2) - direction = (r / 45); - } + if (x1 != x2 || y1 != y2) + direction = (r / 45); + } 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) { - y1 = m * x1 + b; - y2 = m * (x1 + dx) + b; + m = ((float)dy) / ((float)dx); + b = y1 - m * x1; - if (y1 > y2) - { - for (y = y1; y >= y2; y--) - blit_brush(x1, y, direction); - } + if (x2 >= x1) + dx = 1; else - { - for (y = y1; y <= y2; y++) - blit_brush(x1, y, direction); - } + dx = -1; - x1 = x1 + dx; + + 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); + } + else + { + for (y = y1; y <= y2; y++) + blit_brush(x1, y, direction); + } + + x1 = x1 + dx; + } } - } else - { - if (y1 > y2) { - y = y1; - y1 = y2; - y2 = y; - } + if (y1 > y2) + { + y = y1; + y1 = y2; + y2 = y; + } - for (y = y1; y <= y2; y++) - blit_brush(x1, y, direction); - } + for (y = y1; y <= y2; y++) + blit_brush(x1, y, direction); + } 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) - { - update_canvas(orig_x1 - (w >> 1), - orig_y1 - (h >> 1), - orig_x2 + (w >> 1), - orig_y2 + (h >> 1)); - } + { + update_canvas(orig_x1 - (w >> 1), orig_y1 - (h >> 1), orig_x2 + (w >> 1), orig_y2 + (h >> 1)); + } } void reset_brush_counter_and_frame(void) @@ -5811,94 +5781,89 @@ static void blit_brush(int x, int y, int direction) brush_counter++; if (brush_counter >= img_cur_brush_spacing) - { - brush_counter = 0; - - if (img_cur_brush_frames >= 0) { - brush_frame++; - if (brush_frame >= img_cur_brush_frames) - brush_frame = 0; + brush_counter = 0; + + 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; + + SDL_BlitSurface(img_cur_brush, &src, canvas, &dest); } - 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; - - SDL_BlitSurface(img_cur_brush, &src, canvas, &dest); - } } /* stamp tinter */ -#define TINTER_ANYHUE 0 /* like normal, but remaps all hues in the stamp */ -#define TINTER_NARROW 1 /* like normal, but narrow hue angle */ -#define TINTER_NORMAL 2 /* normal */ -#define TINTER_VECTOR 3 /* map black->white to black->destination */ +#define TINTER_ANYHUE 0 /* like normal, but remaps all hues in the stamp */ +#define TINTER_NARROW 1 /* like normal, but narrow hue angle */ +#define TINTER_NORMAL 2 /* normal */ +#define TINTER_VECTOR 3 /* map black->white to black->destination */ typedef struct multichan { - double L, hue, sat; /* L,a,b would be better -- 2-way formula unknown */ - unsigned char or, og, ob, alpha; /* old 8-bit values */ + double L, hue, sat; /* L,a,b would be better -- 2-way formula unknown */ + unsigned char or, og, ob, alpha; /* old 8-bit values */ } multichan; #define X0 ((double)0.9505) @@ -5911,8 +5876,8 @@ typedef struct multichan static void fill_multichan(multichan * mc, double *up, double *vp) { double X, Y, Z, u, v; - double u_prime, v_prime; /* temp, part of official formula */ - double Y_norm, fract; /* severely temp */ + double u_prime, v_prime; /* temp, part of official formula */ + double Y_norm, fract; /* severely temp */ double r = sRGB_to_linear_table[mc->or]; double g = sRGB_to_linear_table[mc->og]; @@ -5928,9 +5893,7 @@ 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); @@ -5949,44 +5912,42 @@ static double tint_part_1(multichan * work, SDL_Surface * in) double u_total = 0; 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++) { - 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); + for (xx = 0; xx < in->w; xx++) + { + multichan *mc = work + yy * in->w + xx; - fill_multichan(mc, &u, &v); + /* put pixels into a more tolerable form */ + SDL_GetRGBA(getpixel(in, xx, yy), in->format, &mc->or, &mc->og, &mc->ob, &mc->alpha); - /* average out u and v, giving more weight to opaque - high-saturation pixels - (this is to take an initial guess at the primary hue) */ + fill_multichan(mc, &u, &v); - u_total += mc->alpha * u * mc->sat; - v_total += mc->alpha * v * mc->sat; + /* 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; + + } } - } 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); } -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; @@ -5998,7 +5959,7 @@ static void change_colors(SDL_Surface * out, multichan * work, double newsat; double L; double X, Y, Z; - double u_prime, v_prime; /* temp, part of official formula */ + double u_prime, v_prime; /* temp, part of official formula */ unsigned tries; double u; double v; @@ -6006,20 +5967,21 @@ static void change_colors(SDL_Surface * out, multichan * work, /* prepare source and destination color info - should reset hue_range or not? won't bother for now*/ - multichan key_color = *key_color_ptr; /* want to work from a copy, for safety */ + should reset hue_range or not? won't bother for now */ + multichan key_color = *key_color_ptr; /* want to work from a copy, for safety */ + 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]; @@ -6033,71 +5995,64 @@ static void change_colors(SDL_Surface * out, multichan * work, SDL_LockSurface(out); for (yy = 0; yy < out->h; yy++) - { - for (xx = 0; xx < out->w; xx++) { - multichan *mc = work + yy * out->w + xx; - double oldhue = mc->hue; + for (xx = 0; xx < out->w; xx++) + { + 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); } -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; @@ -6111,18 +6066,18 @@ static multichan *find_most_saturated(double initial_hue, multichan * work, 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:; @@ -6131,40 +6086,39 @@ 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) { - max_sat = mc->sat; - key_color_ptr = mc; + 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; + } } - } 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; @@ -6175,10 +6129,9 @@ hue_range_retry:; 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]]; @@ -6186,24 +6139,22 @@ 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++) { - unsigned char r8, g8, b8, a8; - double old; + for (xx = 0; xx < in->w; xx++) + { + 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); } @@ -6221,42 +6172,40 @@ 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 - printf("initial_hue = %f\n", initial_hue); -#endif - - key_color_ptr = find_most_saturated(initial_hue, work, - width * height, &hue_range); - -#ifdef DEBUG - 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; + initial_hue = tint_part_1(work, surf_ptr); - change_colors(tmp_surf, work, hue_range, key_color_ptr); +#ifdef DEBUG + printf("initial_hue = %f\n", initial_hue); +#endif + + key_color_ptr = find_most_saturated(initial_hue, work, width * height, &hue_range); + +#ifdef DEBUG + 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"); + } 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); } @@ -6272,6 +6221,7 @@ static void stamp_draw(int x, int y) Uint32 amask; Uint8 r, g, b, a; int xx, yy, dont_free_tmp_surf, base_x, base_y; + Uint32(*getpixel) (SDL_Surface *, int, int); void (*putpixel) (SDL_Surface *, int, int, Uint32); @@ -6283,38 +6233,34 @@ static void stamp_draw(int x, int y) /* 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()); + amask = ~(surf_ptr->format->Rmask | surf_ptr->format->Gmask | surf_ptr->format->Bmask); - cleanup(); - exit(1); + 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; } - - dont_free_tmp_surf = 0; - } else - { - /* Not altering color; no need to create temp surf if we don't use it! */ + { + /* Not altering color; no need to create temp surf if we don't use it! */ - tmp_surf = NULL; - dont_free_tmp_surf = 1; - } + tmp_surf = NULL; + dont_free_tmp_surf = 1; + } if (tmp_surf != NULL) putpixel = putpixels[tmp_surf->format->BytesPerPixel]; @@ -6325,47 +6271,44 @@ static void stamp_draw(int x, int y) /* 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); + /* Render the stamp in the chosen color: */ - 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)); - } + /* 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); } - - SDL_UnlockSurface(tmp_surf); - SDL_UnlockSurface(surf_ptr); - } else if (stamp_tintable(cur_stamp[stamp_group])) - { - 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); - } + { + 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; - } + { + /* No color change, just use it! */ + tmp_surf = surf_ptr; + } /* Shrink or grow it! */ final_surf = thumbnail(tmp_surf, CUR_STAMP_W, CUR_STAMP_H, 0); @@ -6377,11 +6320,10 @@ static void stamp_draw(int x, int y) /* And blit it! */ dest.x = base_x; dest.y = base_y; - SDL_BlitSurface(final_surf, NULL, canvas, &dest); /* FIXME: Conditional jump or move depends on uninitialised value(s) */ + SDL_BlitSurface(final_surf, NULL, canvas, &dest); /* FIXME: Conditional jump or move depends on uninitialised value(s) */ update_canvas(x - (CUR_STAMP_W + 1) / 2, - y - (CUR_STAMP_H + 1) / 2, - x + (CUR_STAMP_W + 1) / 2, y + (CUR_STAMP_H + 1) / 2); + y - (CUR_STAMP_H + 1) / 2, x + (CUR_STAMP_W + 1) / 2, y + (CUR_STAMP_H + 1) / 2); /* Free the temporary surfaces */ @@ -6413,30 +6355,29 @@ static void rec_undo_buffer(void) newest_undo = cur_undo; #ifdef DEBUG - 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); + } } @@ -6585,73 +6526,69 @@ void show_usage(int exitcode) blank[i] = ' '; fprintf(f, - "\n" - "Usage: %s {--usage | --help | --version | --verbose-version | --copying}\n" - "\n" - " %s [--windowed | --fullscreen]\n" - " %s [--WIDTHxHEIGHT | --native]\n" + "\n" + "Usage: %s {--usage | --help | --version | --verbose-version | --copying}\n" + "\n" + " %s [--windowed | --fullscreen]\n" + " %s [--WIDTHxHEIGHT | --native]\n" " %s [--disablescreensaver | --allowscreensaver ]\n" " %s [--orient=landscape | --orient=portrait]\n" - " %s [--startblank | --startlast]\n" - " %s [--sound | --nosound]\n" + " %s [--startblank | --startlast]\n" + " %s [--sound | --nosound]\n" " %s [--quit | --noquit]\n" - " %s [--print | --noprint]\n" + " %s [--print | --noprint]\n" " %s [--complexshapes | --simpleshapes]\n" - " %s [--mixedcase | --uppercase]\n" - " %s [--fancycursors | --nofancycursors]\n" + " %s [--mixedcase | --uppercase]\n" + " %s [--fancycursors | --nofancycursors]\n" " %s [--hidecursor | --showcursor]\n" - " %s [--mouse | --keyboard]\n" + " %s [--mouse | --keyboard]\n" " %s [--dontgrab | --grab]\n" - " %s [--noshortcuts | --shortcuts]\n" + " %s [--noshortcuts | --shortcuts]\n" " %s [--wheelmouse | --nowheelmouse]\n" - " %s [--nobuttondistinction | --buttondistinction]\n" - " %s [--outlines | --nooutlines]\n" + " %s [--nobuttondistinction | --buttondistinction]\n" + " %s [--outlines | --nooutlines]\n" " %s [--stamps | --nostamps]\n" - " %s [--sysfonts | --nosysfonts]\n" - " %s [--nostampcontrols | --stampcontrols]\n" - " %s [--nomagiccontrols | --magiccontrols]\n" + " %s [--sysfonts | --nosysfonts]\n" + " %s [--nostampcontrols | --stampcontrols]\n" + " %s [--nomagiccontrols | --magiccontrols]\n" " %s [--nolabel | --label]\n" - " %s [--mirrorstamps | --dontmirrorstamps]\n" - " %s [--stampsize=[0-10] | --stampsize=default]\n" - " %s [--saveoverask | --saveover | --saveovernew]\n" - " %s [--nosave | --save]\n" - " %s [--autosave | --noautosave]\n" - " %s [--savedir DIRECTORY]\n" - " %s [--datadir DIRECTORY]\n" + " %s [--mirrorstamps | --dontmirrorstamps]\n" + " %s [--stampsize=[0-10] | --stampsize=default]\n" + " %s [--saveoverask | --saveover | --saveovernew]\n" + " %s [--nosave | --save]\n" + " %s [--autosave | --noautosave]\n" " %s [--savedir DIRECTORY]\n" " %s [--datadir DIRECTORY]\n" #if defined(WIN32) || defined(__APPLE__) - " %s [--printcfg | --noprintcfg]\n" + " %s [--printcfg | --noprintcfg]\n" #endif - " %s [--printdelay=SECONDS]\n" - " %s [--altprintmod | --altprintalways | --altprintnever]\n" + " %s [--printdelay=SECONDS]\n" " %s [--altprintmod | --altprintalways | --altprintnever]\n" #if !defined(WIN32) && !defined(__APPLE__) && !defined(__BEOS__) && !defined(__HAIKU__) && !defined(__ANDROID__) - " %s [--papersize PAPERSIZE | --papersize help]\n" + " %s [--papersize PAPERSIZE | --papersize help]\n" #endif - " %s [--lang LANGUAGE | --locale LOCALE | --lang help]\n" - " %s [--nosysconfig]\n" + " %s [--lang LANGUAGE | --locale LOCALE | --lang help]\n" + " %s [--nosysconfig]\n" " %s [--nolockfile]\n" - " %s [--colorfile FILE]\n" - " %s [--mouse-accessibility]\n" - " %s [--onscreen-keyboard]\n" + " %s [--colorfile FILE]\n" + " %s [--mouse-accessibility]\n" + " %s [--onscreen-keyboard]\n" " %s [--joystick-dev N] (default=0)\n" - " %s [--joystick-slowness N] (0-500; default value is 15)\n" - " %s [--joystick-threshold N] (0-32766; default value is 3200)\n" - " %s [--joystick-maxsteps N] (1-7; default value is 7)\n" - "\n", - progname, progname, - blank, blank, blank, blank, - blank, blank, blank, blank, - blank, blank, blank, blank, - blank, blank, blank, blank, - blank, blank, blank, blank, - blank, blank, blank, blank, blank, blank, blank, blank, blank, + " %s [--joystick-slowness N] (0-500; default value is 15)\n" + " %s [--joystick-threshold N] (0-32766; default value is 3200)\n" + " %s [--joystick-maxsteps N] (1-7; default value is 7)\n" + "\n", + progname, progname, + blank, blank, blank, blank, + blank, blank, blank, blank, + blank, blank, blank, blank, + blank, blank, blank, blank, + blank, blank, blank, blank, blank, blank, blank, blank, blank, blank, blank, blank, blank, #ifdef WIN32 - blank, + blank, #endif - blank, blank, + blank, blank, #if !defined(WIN32) && !defined(__APPLE__) && !defined(__BEOS__) && !defined(__HAIKU__) && !defined(__ANDROID__) - blank, + blank, #endif - blank, blank, blank, blank, blank, blank, blank, blank, blank, blank); + blank, blank, blank, blank, blank, blank, blank, blank, blank, blank); free(blank); } @@ -6670,19 +6607,16 @@ static unsigned compute_default_scale_factor(double ratio) double good_def = ratio * sqrt(new_diag / old_diag); double good_log = log(good_def); 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); - if (fabs(next_err) > fabs(this_err)) - break; - defsize--; - } + { + 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--; + } return defsize; } @@ -6690,13 +6624,12 @@ static unsigned compute_default_scale_factor(double ratio) /* directory walking... */ static void loadbrush_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) + SDL_Texture * texture, + SDL_Renderer * renderer, + const char *restrict const dir, + unsigned dirlen, tp_ftw_str * files, unsigned i, const char *restrict const locale) { - FILE * fi; + FILE *fi; char buf[64]; int want_rand; @@ -6706,77 +6639,72 @@ static void loadbrush_callback(SDL_Surface * screen, qsort(files, i, sizeof *files, compare_ftw_str); while (i--) - { - 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; + show_progress_bar(screen); + if (strcasestr(files[i].str, ".png")) + { + char fname[512]; - 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); - brushes_frames = - realloc(brushes_frames, num_brushes_max * sizeof(int)); - brushes_directional = - realloc(brushes_directional, num_brushes_max * sizeof(short)); - brushes_spacing = - realloc(brushes_spacing, num_brushes_max * sizeof(int)); - } - img_brushes[num_brushes] = loadimage(fname); + if (strcasecmp(files[i].str, SHAPE_BRUSH_NAME) == 0) + shape_brush = num_brushes; + + 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); + brushes_frames = realloc(brushes_frames, num_brushes_max * sizeof(int)); + brushes_directional = realloc(brushes_directional, 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: */ + /* Load brush metadata, if any: */ - brushes_frames[num_brushes] = 1; - brushes_directional[num_brushes] = 0; - brushes_spacing[num_brushes] = img_brushes[num_brushes]->h / 4; + brushes_frames[num_brushes] = 1; + brushes_directional[num_brushes] = 0; + brushes_spacing[num_brushes] = img_brushes[num_brushes]->h / 4; - strcpy(strcasestr(fname, ".png"), ".dat"); - fi = fopen(fname, "r"); + strcpy(strcasestr(fname, ".png"), ".dat"); + fi = fopen(fname, "r"); - want_rand = 0; + want_rand = 0; - if (fi != NULL) - { - do - { - fgets(buf, sizeof(buf), fi); + if (fi != NULL) + { + do + { + 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, "random") != NULL) - { - want_rand = 1; - } - } - while (!feof(fi)); - fclose(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, "random") != NULL) + { + want_rand = 1; + } + } + while (!feof(fi)); + fclose(fi); - if (want_rand) - brushes_frames[num_brushes] *= -1; - } + if (want_rand) + brushes_frames[num_brushes] *= -1; + } - num_brushes++; + num_brushes++; + } + free(files[i].str); } - free(files[i].str); - } free(files); } @@ -6804,28 +6732,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++) { - src.x = x; - src.y = 0; - src.w = 1; - src.h = s->h; + for (x = 0; x < s->w; x++) + { + 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_BlitSurface(s, &src, new_surf, &dest); + } + + SDL_FreeSurface(s); + + return (new_surf); } - - SDL_FreeSurface(s); - - return (new_surf); - } else - { - return (s); - } + { + return (s); + } } SDL_Surface *flip_surface(SDL_Surface * s) @@ -6841,34 +6769,33 @@ SDL_Surface *flip_surface(SDL_Surface * s) SDL_SetSurfaceBlendMode(s, SDL_BLENDMODE_NONE); if (new_surf != NULL) - { - for (y = 0; y < s->h; y++) { - src.x = 0; - src.y = y; - src.w = s->w; - src.h = 1; + for (y = 0; y < s->h; y++) + { + 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_BlitSurface(s, &src, new_surf, &dest); + } + + SDL_FreeSurface(s); + + return (new_surf); } - - SDL_FreeSurface(s); - - return (new_surf); - } else - { - return (s); - } + { + return (s); + } } static unsigned default_stamp_size; -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; @@ -6884,88 +6811,95 @@ static void loadstamp_finisher(stamp_type * sd, unsigned w, unsigned h, 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); + 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) - { + 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); + 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; - } + if (ph > canvas->h) + { + underscanned_upper = upper - 1; + } + else + { + underscanned_upper = upper; + } + break; + } #else - if (pw <= canvas->w * 1 && ph <= canvas->h * 1) - { + if (pw <= canvas->w * 1 && ph <= canvas->h * 1) + { #ifdef DEBUG - printf("Upper at %d with proposed size %dx%d\n", upper, pw, ph); + printf("Upper at %d with proposed size %dx%d\n", upper, pw, ph); +#endif + underscanned_upper = upper; + break; + } #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) mid = compute_default_scale_factor(ratio); /* Ratio override for SVGs! */ - if (ratio == 1.0 && sd->is_svg) { - mid = compute_default_scale_factor(0.2); - } + if (ratio == 1.0 && sd->is_svg) + { + mid = compute_default_scale_factor(0.2); + } if (mid > upper) mid = upper; @@ -6985,12 +6919,12 @@ static void loadstamp_finisher(stamp_type * sd, unsigned w, unsigned h, #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 @@ -7021,220 +6955,220 @@ 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) { -#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: */ + /* Want mirrored and flipped, both */ #ifdef DEBUG - printf("didn't find a _mirror_flip\n"); + printf("want both mirrored & flipped\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) + if (!sd->no_premirrorflip) { #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, "_flip.png", 10); - active_stamp = do_loadimage(buf, 0); - } + { + memcpy(buf + len, "_mirror_flip.png", 17); + active_stamp = do_loadimage(buf, 0); + } } - if (active_stamp != NULL) + + 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); + 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, either\n"); + printf("didn't find a _flip\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); - 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\n"); -#endif - } - } else if (sd->mirrored && !sd->no_premirror) - { - /* Want mirrored only */ + { + /* Want mirrored only */ #ifdef DEBUG - printf("want mirrored only\n"); + 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); +#ifndef NOSVG + memcpy(buf + len, "_mirror.svg", 12); active_stamp = do_loadimage(buf, 0); - } - - if (active_stamp != NULL) - { -#ifdef DEBUG - printf("found a _mirror!\n"); #endif - needs_mirror = 0; - } - else - { + + if (active_stamp == NULL) + { + memcpy(buf + len, "_mirror.png", 12); + active_stamp = do_loadimage(buf, 0); + } + + if (active_stamp != NULL) + { #ifdef DEBUG - printf("didn't find a _mirror\n"); + 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); -#endif - - if (active_stamp == NULL) - { - memcpy(buf + len, ".png", 5); + 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); + } + + } /* Never allow a NULL image! */ if (!active_stamp) - active_stamp = thumbnail(img_dead40x40, 40, 40, 1); /* copy it */ + active_stamp = thumbnail(img_dead40x40, 40, 40, 1); /* copy it */ /* If we wanted mirrored or flipped, and didn't get something pre-rendered, 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"); @@ -7258,80 +7192,79 @@ static void get_stamp_thumb(stamp_type * sd) 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... - - -bjk 2009.09.29 */ + So far, I'm doing (b), in loadstamp_finisher... - ratio = 1.0; - } + -bjk 2009.09.29 */ + + 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 /* 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; - } + { + /* 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; - } + { + /* damn thing wants a .png extension; give it one */ + memcpy(buf + len, ".png", 5); + sd->sdesc = loaddescsound(buf); + sd->no_descsound = !sd->sdesc; + } #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) { - /* It's already the way we want */ - #ifdef DEBUG - printf("mirrored == flipped == thumb_mirrored_flipped [bye]\n"); + printf("have an sd->thumbnail\n"); #endif - return; + 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 */ + +#ifdef DEBUG + printf("mirrored == flipped == thumb_mirrored_flipped [bye]\n"); +#endif + + return; + } } - } /* nope, see if there's a pre-rendered one we can use */ @@ -7341,179 +7274,179 @@ static void get_stamp_thumb(stamp_type * sd) bigimg = NULL; if (sd->mirrored && sd->flipped) - { + { #ifdef DEBUG - printf("want mirrored & flipped\n"); + printf("want mirrored & flipped\n"); #endif - if (!sd->no_premirrorflip) + 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) { - memcpy(buf + len, "_mirror_flip.png", 17); +#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_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); +#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 == NULL) + { + memcpy(buf + len, "_flip.svg", 10); + bigimg = do_loadimage(buf, 0); } +#endif - if (bigimg) + if (bigimg) { #ifdef DEBUG - printf("found a _flip\n"); + printf("found a _flip!\n"); #endif - need_flip = 0; } - } - } - } - else if (sd->mirrored && !sd->no_premirror) - { + else + { #ifdef DEBUG - printf("want mirrored only\n"); + printf("didn't find a flip\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); + sd->no_preflip = 1; + } } -#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); - } + if (bigimg == NULL) + { + memcpy(buf + len, ".svg", 5); + bigimg = do_loadimage(buf, 0); + } #endif - } + } /* Scale the stamp down to its thumbnail size: */ @@ -7521,18 +7454,18 @@ static void get_stamp_thumb(stamp_type * sd) w = 40; h = 40; if (bigimg) - { - w = bigimg->w; - h = bigimg->h; - } + { + w = bigimg->w; + h = bigimg->h; + } if (!bigimg) - sd->thumbnail = thumbnail(img_dead40x40, 40, 40, 1); /* copy */ + sd->thumbnail = thumbnail(img_dead40x40, 40, 40, 1); /* copy */ else if (bigimg->w > 40 || bigimg->h > 40) - { - sd->thumbnail = thumbnail(bigimg, 40, 40, 1); - SDL_FreeSurface(bigimg); - } + { + sd->thumbnail = thumbnail(bigimg, 40, 40, 1); + SDL_FreeSurface(bigimg); + } else sd->thumbnail = bigimg; @@ -7540,20 +7473,20 @@ static void get_stamp_thumb(stamp_type * sd) /* 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 @@ -7577,17 +7510,16 @@ static void get_stamp_thumb(stamp_type * sd) if (sd->processed) return; - sd->processed = 1; /* not really, but on the next line... */ + sd->processed = 1; /* not really, but on the next line... */ loadstamp_finisher(sd, w, h, ratio); } static void loadstamp_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) + SDL_Texture * texture, + SDL_Renderer * renderer, + const char *restrict const dir, + unsigned dirlen, tp_ftw_str * files, unsigned i, const char *restrict const locale) { (void)locale; #ifdef DEBUG @@ -7595,120 +7527,120 @@ static void loadstamp_callback(SDL_Surface * screen, #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 (dir[i] == '/' || dir[i] == '\\') - slashcount++; - } + /* If previous group had any stamps... */ - if (slashcount <= stamp_group_dir_depth) - { - stamp_group++; + 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 - printf("\n...counts as a new group! now: %d\n", stamp_group); + printf("\n...counts as a new group! now: %d\n", stamp_group); #endif - } - else - { + } + else + { #ifdef DEBUG - printf("...is still part of group %d\n", stamp_group); + 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; - snprintf(svgname, sizeof(svgname), "%s/%s", dir, files[i].str); - strcpy(strcasestr(svgname, ".png"), ".svg"); + snprintf(svgname, sizeof(svgname), "%s/%s", dir, files[i].str); + strcpy(strcasestr(svgname, ".png"), ".svg"); - 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 - show_progress_bar(screen); + show_progress_bar(screen); - if (dotext > files[i].str && !strcasecmp(dotext, ext) - && (dotext - files[i].str + 1 + dirlen < sizeof fname) - && !strcasestr(files[i].str, mirror_ext) - && !strcasestr(files[i].str, flip_ext) - && !strcasestr(files[i].str, mirrorflip_ext)) - { - 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 < sizeof fname) + && !strcasestr(files[i].str, mirror_ext) + && !strcasestr(files[i].str, flip_ext) && !strcasestr(files[i].str, mirrorflip_ext)) + { + 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]++; + num_stamps[stamp_group]++; + } + free(files[i].str); } - free(files[i].str); - } free(files); } @@ -7718,6 +7650,7 @@ static void load_stamp_dir(SDL_Surface * screen, const char *const dir) { char buf[TP_FTW_PATHSIZE]; unsigned dirlen = strlen(dir); + memcpy(buf, dir, dirlen); load_stamp_basedir = dir; tp_ftw(screen, texture, renderer, buf, dirlen, 0, loadstamp_callback, NULL); @@ -7742,11 +7675,9 @@ 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; @@ -7767,63 +7698,79 @@ 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); + while (fontconfig_thread_done == 0) + { + 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))) { - printf("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))) + { + printf("Aborting!\n"); + fflush(stdout); + return (1); + } + } } - } - return(0); + return (0); } static int generate_fontconfig_cache_real(void) { - TuxPaint_Font * tmp_font; - SDL_Surface * tmp_surf; + TuxPaint_Font *tmp_font; + SDL_Surface *tmp_surf; SDL_Color black = { 0, 0, 0, 0 }; #ifdef DEBUG - printf("-- Hello from generate_fontconfig_cache() (thread # %d)\n", SDL_ThreadID()); fflush(stdout); + 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); + printf("-- Generated a font.\n"); + fflush(stdout); #endif - tmp_surf = render_text(tmp_font, "Test", black); - if (tmp_surf != NULL) { + tmp_surf = render_text(tmp_font, "Test", black); + if (tmp_surf != NULL) + { #ifdef DEBUG - printf("-- Generated a surface\n"); fflush(stdout); + printf("-- Generated a surface\n"); + fflush(stdout); #endif - SDL_FreeSurface(tmp_surf); - } else { + SDL_FreeSurface(tmp_surf); + } + else + { #ifdef DEBUG - printf("-- Failed to make a surface!\n"); fflush(stdout); + 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); #endif } - TuxPaint_Font_CloseFont(tmp_font); - } else { -#ifdef DEBUG - printf("-- Failed to generate a font!\n"); fflush(stdout); -#endif - } fontconfig_thread_done = 1; #ifdef DEBUG - printf("-- generate_fontconfig_cache() is done\n"); fflush(stdout); + printf("-- generate_fontconfig_cache() is done\n"); + fflush(stdout); #endif - return(0); + return (0); } static int generate_fontconfig_cache(void *vp) @@ -7879,7 +7826,7 @@ static void create_button_labels(void) img_shape_names[i] = do_render_button_label(shape_names[i]); /* buttons for the file open dialog */ - + /* Open dialog: 'Open' button, to load the selected picture */ img_openlabels_open = do_render_button_label(gettext_noop("Open")); @@ -7917,13 +7864,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; + } #ifndef WIN32 @@ -7947,18 +7894,17 @@ 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); + } } /* 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]; @@ -7966,34 +7912,30 @@ 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)); } @@ -8016,50 +7958,46 @@ 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) { - 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()); + 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()); - cleanup(); - exit(1); + cleanup(); + exit(1); + } + else + { + return (NULL); + } } - else - { - return (NULL); - } - } /* Convert to the display format: */ disp_fmt_s = SDL_DisplayFormatAlpha(s); if (disp_fmt_s == NULL) - { - 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()); + 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()); - SDL_FreeSurface(s); - cleanup(); - exit(1); + SDL_FreeSurface(s); + cleanup(); + exit(1); + } + else + { + SDL_FreeSurface(s); + return (NULL); + } } - else - { - SDL_FreeSurface(s); - return (NULL); - } - } /* Free the temp. surface & return the converted one: */ @@ -8086,94 +8024,95 @@ static void draw_toolbar(void) - /* Do we need scrollbars? */ - if (NUM_TOOLS > most + TOOLOFFSET) + /* Do we need scrollbars? */ + if (NUM_TOOLS > most + TOOLOFFSET) { - off_y = 24; - max = most - 2 + TOOLOFFSET; - gd_tools.rows = max / 2; - - dest.x = 0; - dest.y = 40; + off_y = 24; + max = most - 2 + TOOLOFFSET; + gd_tools.rows = max / 2; - 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 = 40; - dest.x = 0; - dest.y = 40 + 24 + ((6 + TOOLOFFSET / 2) * 48); - + 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 = 40 + 24 + ((6 + TOOLOFFSET / 2) * 48); - if (tool_scroll < NUM_TOOLS - (most - 2) - TOOLOFFSET) - { - SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + + if (tool_scroll < NUM_TOOLS - (most - 2) - TOOLOFFSET) + { + SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); + } } - else + else { - SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); + off_y = 0; + max = 14 + TOOLOFFSET; } - } - else - { - off_y = 0; - max = 14 + TOOLOFFSET; - } - + for (tool = tool_scroll; tool < tool_scroll + max; tool++) - { - i = tool - tool_scroll; - dest.x = ((i % 2) * 48); - dest.y = ((i / 2) * 48) + 40 + off_y; - - - 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; - } + i = tool - tool_scroll; + dest.x = ((i % 2) * 48); + dest.y = ((i / 2) * 48) + 40 + off_y; + + + 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) * 48) + 4; + dest.y = ((i / 2) * 48) + 40 + 2 + off_y; + + SDL_BlitSurface(img_tools[tool], NULL, screen, &dest); + + + dest.x = ((i % 2) * 48) + 4 + (40 - img_tool_names[tool]->w) / 2; + dest.y = ((i / 2) * 48) + 40 + 2 + (44 + button_label_y_nudge - img_tool_names[tool]->h) + off_y; + + SDL_BlitSurface(img_tool_names[tool], NULL, screen, &dest); + } 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) * 48) + 4; - dest.y = ((i / 2) * 48) + 40 + 2 + off_y; - - SDL_BlitSurface(img_tools[tool], NULL, screen, &dest); - - - dest.x = ((i % 2) * 48) + 4 + (40 - img_tool_names[tool]->w) / 2; - dest.y = ((i / 2) * 48) + 40 + 2 + (44 + button_label_y_nudge - img_tool_names[tool]->h) + off_y; - - SDL_BlitSurface(img_tool_names[tool], NULL, screen, &dest); + { + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + } } - else - { - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - } - } } @@ -8195,124 +8134,124 @@ static void draw_magic(void) most = 14; if (num_magics > most + TOOLOFFSET) - { - off_y = 24; - max = (most - 2) + TOOLOFFSET; - - dest.x = WINDOW_WIDTH - 96; - dest.y = 40; - - if (magic_scroll > 0) { - SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); - } + off_y = 24; + max = (most - 2) + TOOLOFFSET; - dest.x = WINDOW_WIDTH - 96; - dest.y = 40 + 24 + ((((most - 2) / 2) + TOOLOFFSET / 2) * 48); + dest.x = WINDOW_WIDTH - 96; + dest.y = 40; - if (magic_scroll < num_magics - (most - 2) - TOOLOFFSET) - { - SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + if (magic_scroll > 0) + { + SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); + } + + dest.x = WINDOW_WIDTH - 96; + dest.y = 40 + 24 + ((((most - 2) / 2) + TOOLOFFSET / 2) * 48); + + if (magic_scroll < num_magics - (most - 2) - TOOLOFFSET) + { + SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_scroll_down_off, 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 (magic = magic_scroll; magic < magic_scroll + max; magic++) - { - i = magic - magic_scroll; - - dest.x = ((i % 2) * 48) + (WINDOW_WIDTH - 96); - dest.y = ((i / 2) * 48) + 40 + off_y; - - if (magic < num_magics) { - if (magic == cur_magic) - { - SDL_BlitSurface(img_btn_down, NULL, screen, &dest); - } + i = magic - magic_scroll; + + dest.x = ((i % 2) * 48) + (WINDOW_WIDTH - 96); + dest.y = ((i / 2) * 48) + 40 + off_y; + + if (magic < num_magics) + { + if (magic == cur_magic) + { + SDL_BlitSurface(img_btn_down, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + } + + dest.x = WINDOW_WIDTH - 96 + ((i % 2) * 48) + 4; + dest.y = ((i / 2) * 48) + 40 + 4 + off_y; + + SDL_BlitSurface(magics[magic].img_icon, NULL, screen, &dest); + + + dest.x = WINDOW_WIDTH - 96 + ((i % 2) * 48) + 4 + (40 - magics[magic].img_name->w) / 2; + dest.y = (((i / 2) * 48) + 40 + 4 + (44 - magics[magic].img_name->h) + off_y); + + SDL_BlitSurface(magics[magic].img_name, NULL, screen, &dest); + } else - { - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); - } - - dest.x = WINDOW_WIDTH - 96 + ((i % 2) * 48) + 4; - dest.y = ((i / 2) * 48) + 40 + 4 + off_y; - - SDL_BlitSurface(magics[magic].img_icon, NULL, screen, &dest); - - - dest.x = WINDOW_WIDTH - 96 + ((i % 2) * 48) + 4 + - (40 - magics[magic].img_name->w) / 2; - dest.y = (((i / 2) * 48) + 40 + 4 + - (44 - magics[magic].img_name->h) + off_y); - - SDL_BlitSurface(magics[magic].img_name, NULL, screen, &dest); + { + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + } } - else - { - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - } - } /* Draw text controls: */ if (!disable_magic_controls) - { - SDL_Surface *button_color; + { + SDL_Surface *button_color; - /* Show paint button: */ + /* Show paint button: */ - if (magics[cur_magic].mode == MODE_PAINT || magics[cur_magic].mode == MODE_ONECLICK || magics[cur_magic].mode == MODE_PAINT_WITH_PREVIEW) - button_color = img_btn_down; /* Active */ - else if (magics[cur_magic].avail_modes & MODE_PAINT || magics[cur_magic].avail_modes & MODE_ONECLICK || magics[cur_magic].avail_modes & MODE_PAINT_WITH_PREVIEW) - button_color = img_btn_up; /* Available, but not active */ - else - button_color = img_btn_off; /* Unavailable */ + if (magics[cur_magic].mode == MODE_PAINT || magics[cur_magic].mode == MODE_ONECLICK + || magics[cur_magic].mode == MODE_PAINT_WITH_PREVIEW) + button_color = img_btn_down; /* Active */ + else if (magics[cur_magic].avail_modes & MODE_PAINT || magics[cur_magic].avail_modes & MODE_ONECLICK + || magics[cur_magic].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 - 96; - dest.y = 40 + ((6 + TOOLOFFSET / 2) * 48); + dest.x = WINDOW_WIDTH - 96; + dest.y = 40 + ((6 + TOOLOFFSET / 2) * 48); - SDL_BlitSurface(button_color, NULL, screen, &dest); + SDL_BlitSurface(button_color, NULL, screen, &dest); - dest.x = WINDOW_WIDTH - 96 + (48 - img_magic_paint->w) / 2; - dest.y = (40 + ((6 + TOOLOFFSET / 2) * 48) + (48 - img_magic_paint->h) / 2); + dest.x = WINDOW_WIDTH - 96 + (48 - img_magic_paint->w) / 2; + dest.y = (40 + ((6 + TOOLOFFSET / 2) * 48) + (48 - 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[cur_magic].mode == MODE_FULLSCREEN) - button_color = img_btn_down; /* Active */ - else if (magics[cur_magic].avail_modes & MODE_FULLSCREEN) - button_color = img_btn_up; /* Available, but not active */ - else - button_color = img_btn_off; /* Unavailable */ + if (magics[cur_magic].mode == MODE_FULLSCREEN) + button_color = img_btn_down; /* Active */ + else if (magics[cur_magic].avail_modes & MODE_FULLSCREEN) + button_color = img_btn_up; /* Available, but not active */ + else + button_color = img_btn_off; /* Unavailable */ - dest.x = WINDOW_WIDTH - 48; - dest.y = 40 + ((6 + TOOLOFFSET / 2) * 48); + dest.x = WINDOW_WIDTH - 48; + dest.y = 40 + ((6 + TOOLOFFSET / 2) * 48); - SDL_BlitSurface(button_color, NULL, screen, &dest); + SDL_BlitSurface(button_color, NULL, screen, &dest); - dest.x = WINDOW_WIDTH - 48 + (48 - img_magic_fullscreen->w) / 2; - dest.y = (40 + ((6 + TOOLOFFSET / 2) * 48) + (48 - img_magic_fullscreen->h) / 2); + dest.x = WINDOW_WIDTH - 48 + (48 - img_magic_fullscreen->w) / 2; + dest.y = (40 + ((6 + TOOLOFFSET / 2) * 48) + (48 - img_magic_fullscreen->h) / 2); - SDL_BlitSurface(img_magic_fullscreen, NULL, screen, &dest); - } + SDL_BlitSurface(img_magic_fullscreen, NULL, screen, &dest); + } } @@ -8326,7 +8265,7 @@ static unsigned draw_colors(unsigned action) SDL_Rect dest; static unsigned old_color; unsigned old_colors_state; - + old_colors_state = colors_state; if (action == COLORSEL_CLOBBER || action == COLORSEL_CLOBBER_WIPE) @@ -8349,33 +8288,30 @@ 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; -#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); -#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)); - - if (i == cur_color && colors_state == COLORSEL_ENABLE) + for (i = 0; i < (unsigned int)NUM_COLORS; i++) { - dest.y += 4; - SDL_BlitSurface(img_paintcan, NULL, screen, &dest); - } + 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); +#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)); + + 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 */ @@ -8383,19 +8319,17 @@ static unsigned draw_colors(unsigned action) return old_colors_state; 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); @@ -8418,83 +8352,82 @@ static void draw_brushes(void) /* Do we need scrollbars? */ if (num_brushes > 14 + TOOLOFFSET) - { - off_y = 24; - max = 12 + TOOLOFFSET; - - dest.x = WINDOW_WIDTH - 96; - dest.y = 40; - - if (brush_scroll > 0) { - SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); - } + off_y = 24; + max = 12 + TOOLOFFSET; - dest.x = WINDOW_WIDTH - 96; - dest.y = 40 + 24 + ((6 + TOOLOFFSET / 2) * 48); + dest.x = WINDOW_WIDTH - 96; + dest.y = 40; - if (brush_scroll < num_brushes - 12 - TOOLOFFSET) - { - SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + 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 - 96; + dest.y = 40 + 24 + ((6 + TOOLOFFSET / 2) * 48); + + if (brush_scroll < num_brushes - 12 - TOOLOFFSET) + { + SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); + } } - else - { - SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); - } - } else - { - off_y = 0; - max = 14 + TOOLOFFSET; - } + { + off_y = 0; + max = 14 + TOOLOFFSET; + } /* Draw each of the shown brushes: */ for (brush = brush_scroll; brush < brush_scroll + max; brush++) - { - i = brush - brush_scroll; + { + i = brush - brush_scroll; - dest.x = ((i % 2) * 48) + (WINDOW_WIDTH - 96); - dest.y = ((i / 2) * 48) + 40 + off_y; + dest.x = ((i % 2) * 48) + (WINDOW_WIDTH - 96); + dest.y = ((i / 2) * 48) + 40 + 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) - { - if (brushes_directional[brush]) - src.x = (img_brushes[brush]->w / abs(brushes_frames[brush])) / 3; + 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 - src.x = 0; + { + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + } - src.y = brushes_directional[brush] ? (img_brushes[brush]->h / 3) : 0; + if (brush < num_brushes) + { + if (brushes_directional[brush]) + src.x = (img_brushes[brush]->w / abs(brushes_frames[brush])) / 3; + else + src.x = 0; - src.w = (img_brushes[brush]->w / abs(brushes_frames[brush])) / - (brushes_directional[brush] ? 3 : 1); - src.h = (img_brushes[brush]->h / (brushes_directional[brush] ? 3 : 1)); + src.y = brushes_directional[brush] ? (img_brushes[brush]->h / 3) : 0; - dest.x = ((i % 2) * 48) + (WINDOW_WIDTH - 96) + ((48 - src.w) >> 1); - dest.y = ((i / 2) * 48) + 40 + ((48 - src.h) >> 1) + off_y; + src.w = (img_brushes[brush]->w / abs(brushes_frames[brush])) / (brushes_directional[brush] ? 3 : 1); + src.h = (img_brushes[brush]->h / (brushes_directional[brush] ? 3 : 1)); - SDL_BlitSurface(img_brushes[brush], &src, screen, &dest); + dest.x = ((i % 2) * 48) + (WINDOW_WIDTH - 96) + ((48 - src.w) >> 1); + dest.y = ((i / 2) * 48) + 40 + ((48 - src.h) >> 1) + off_y; + + SDL_BlitSurface(img_brushes[brush], &src, screen, &dest); + } } - } } @@ -8511,18 +8444,18 @@ static void draw_fonts(void) /* How many can we show? */ - if(cur_tool == TOOL_LABEL) - { - most = 8; + if (cur_tool == TOOL_LABEL) + { + most = 8; if (disable_stamp_controls) most = 12; - } + } else - { - most = 10; + { + most = 10; if (disable_stamp_controls) most = 14; - } + } #ifdef DEBUG printf("there are %d font families\n", num_font_families); @@ -8532,276 +8465,276 @@ static void draw_fonts(void) /* Do we need scrollbars? */ if (num_font_families > most + TOOLOFFSET) - { - off_y = 24; - max = most - 2 + TOOLOFFSET; - - dest.x = WINDOW_WIDTH - 96; - dest.y = 40; - - if (font_scroll > 0) { - SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); - } + off_y = 24; + max = most - 2 + TOOLOFFSET; - dest.x = WINDOW_WIDTH - 96; - if (cur_tool == TOOL_LABEL) - dest.y = 40 + 24 + ((5 + TOOLOFFSET / 2) * 48); - else - dest.y = 40 + 24 + ((6 + TOOLOFFSET / 2) * 48); + dest.x = WINDOW_WIDTH - 96; + dest.y = 40; - if (!disable_stamp_controls) - dest.y = dest.y - (48 * 2); + if (font_scroll > 0) + { + SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); + } - if (font_scroll < num_font_families - (most - 2) - TOOLOFFSET) - { - SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + dest.x = WINDOW_WIDTH - 96; + if (cur_tool == TOOL_LABEL) + dest.y = 40 + 24 + ((5 + TOOLOFFSET / 2) * 48); + else + dest.y = 40 + 24 + ((6 + TOOLOFFSET / 2) * 48); + + if (!disable_stamp_controls) + dest.y = dest.y - (48 * 2); + + if (font_scroll < num_font_families - (most - 2) - TOOLOFFSET) + { + SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_scroll_down_off, 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) - font_scroll = min(font_scroll, max(0, num_font_families - max)); /* FIXAM COMENTARI */ + font_scroll = min(font_scroll, max(0, num_font_families - max)); /* FIXAM COMENTARI */ else - font_scroll = min(font_scroll, max(0, num_font_families + 1 - max)); /* FIXAM COMENTARI */ + 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; - - - dest.x = ((i % 2) * 48) + (WINDOW_WIDTH - 96); - dest.y = ((i / 2) * 48) + 40 + 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); - } + i = font - font_scroll; - 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) - { - printf("render_text() returned NULL!\n"); - return; - } - - if (tmp_surf_1->w > 48 || tmp_surf_1->h > 48) - { - tmp_surf = thumbnail(tmp_surf_1, 48, 48, 1); - SDL_FreeSurface(tmp_surf_1); - } - else - tmp_surf = tmp_surf_1; - - src.x = (tmp_surf->w - 48) / 2; - src.y = (tmp_surf->h - 48) / 2; - src.w = 48; - src.h = 48; - - if (src.x < 0) - src.x = 0; - if (src.y < 0) - src.y = 0; dest.x = ((i % 2) * 48) + (WINDOW_WIDTH - 96); - if (src.w > tmp_surf->w) - { - src.w = tmp_surf->w; - dest.x = dest.x + ((48 - (tmp_surf->w)) / 2); - } - - dest.y = ((i / 2) * 48) + 40 + off_y; - if (src.h > tmp_surf->h) - { - src.h = tmp_surf->h; - dest.y = dest.y + ((48 - (tmp_surf->h)) / 2); - } - SDL_BlitSurface(tmp_surf, &src, screen, &dest); + 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_FreeSurface(tmp_surf); + 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) + { + printf("render_text() returned NULL!\n"); + return; + } + + if (tmp_surf_1->w > 48 || tmp_surf_1->h > 48) + { + tmp_surf = thumbnail(tmp_surf_1, 48, 48, 1); + SDL_FreeSurface(tmp_surf_1); + } + else + tmp_surf = tmp_surf_1; + + src.x = (tmp_surf->w - 48) / 2; + src.y = (tmp_surf->h - 48) / 2; + src.w = 48; + src.h = 48; + + if (src.x < 0) + src.x = 0; + if (src.y < 0) + src.y = 0; + + dest.x = ((i % 2) * 48) + (WINDOW_WIDTH - 96); + if (src.w > tmp_surf->w) + { + src.w = tmp_surf->w; + dest.x = dest.x + ((48 - (tmp_surf->w)) / 2); + } + + + dest.y = ((i / 2) * 48) + 40 + off_y; + if (src.h > tmp_surf->h) + { + src.h = tmp_surf->h; + dest.y = dest.y + ((48 - (tmp_surf->h)) / 2); + } + + SDL_BlitSurface(tmp_surf, &src, screen, &dest); + + SDL_FreeSurface(tmp_surf); + } } - } /* Draw text controls: */ if (!disable_stamp_controls) - { - SDL_Surface *button_color; - SDL_Surface *button_body; - - if (cur_tool == TOOL_LABEL) { + SDL_Surface *button_color; + SDL_Surface *button_body; - /* disabling rotation as I am not sure how this should be implemented */ - dest.x = WINDOW_WIDTH - 96; - dest.y = 40 + ((4 + TOOLOFFSET / 2) * 48); - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - - /* if(cur_label == LABEL_ROTATE) */ - /* SDL_BlitSurface(img_btn_down, NULL, screen, &dest); */ - /* else */ - /* SDL_BlitSurface(img_btn_up, NULL, screen, &dest); */ - - /* dest.x = WINDOW_WIDTH - 96 + (48 - img_label->w) / 2; */ - /* dest.y = (40 + ((4 + TOOLOFFSET / 2) * 48) + (48 - img_label->h) / 2); */ - - /* SDL_BlitSurface(img_label, NULL, screen, &dest); */ - - dest.x = WINDOW_WIDTH - 48; - dest.y = 40 + ((4 + TOOLOFFSET / 2) * 48); - - if(cur_label == LABEL_SELECT) - SDL_BlitSurface(img_btn_down, NULL, screen, &dest); - - else + if (cur_tool == TOOL_LABEL) { - if(are_labels()) - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); - else + + /* disabling rotation as I am not sure how this should be implemented */ + dest.x = WINDOW_WIDTH - 96; + dest.y = 40 + ((4 + TOOLOFFSET / 2) * 48); + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + + /* if(cur_label == LABEL_ROTATE) */ + /* SDL_BlitSurface(img_btn_down, NULL, screen, &dest); */ + /* else */ + /* SDL_BlitSurface(img_btn_up, NULL, screen, &dest); */ + + /* dest.x = WINDOW_WIDTH - 96 + (48 - img_label->w) / 2; */ + /* dest.y = (40 + ((4 + TOOLOFFSET / 2) * 48) + (48 - img_label->h) / 2); */ + + /* SDL_BlitSurface(img_label, NULL, screen, &dest); */ + + dest.x = WINDOW_WIDTH - 48; + dest.y = 40 + ((4 + TOOLOFFSET / 2) * 48); + + 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 - 48 + (48 - img_label_select->w) / 2; + dest.y = (40 + ((4 + TOOLOFFSET / 2) * 48) + (48 - img_label_select->h) / 2); + + SDL_BlitSurface(img_label_select, NULL, screen, &dest); } - - - dest.x = WINDOW_WIDTH - 48 + (48 - img_label_select->w) / 2; - dest.y = (40 + ((4 + TOOLOFFSET / 2) * 48) + (48 - img_label_select->h) / 2); - SDL_BlitSurface(img_label_select, NULL, screen, &dest); - } + /* Show bold button: */ - /* Show bold button: */ + dest.x = WINDOW_WIDTH - 96; + dest.y = 40 + ((5 + TOOLOFFSET / 2) * 48); - dest.x = WINDOW_WIDTH - 96; - dest.y = 40 + ((5 + TOOLOFFSET / 2) * 48); + if (text_state & TTF_STYLE_BOLD) + SDL_BlitSurface(img_btn_down, NULL, screen, &dest); + else + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); - 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 - 96 + (48 - img_bold->w) / 2; + dest.y = (40 + ((5 + TOOLOFFSET / 2) * 48) + (48 - img_bold->h) / 2); - dest.x = WINDOW_WIDTH - 96 + (48 - img_bold->w) / 2; - dest.y = (40 + ((5 + TOOLOFFSET / 2) * 48) + (48 - img_bold->h) / 2); - - SDL_BlitSurface(img_bold, NULL, screen, &dest); + SDL_BlitSurface(img_bold, NULL, screen, &dest); - /* Show italic button: */ + /* Show italic button: */ - dest.x = WINDOW_WIDTH - 48; - dest.y = 40 + ((5 + TOOLOFFSET / 2) * 48); + dest.x = WINDOW_WIDTH - 48; + dest.y = 40 + ((5 + TOOLOFFSET / 2) * 48); - if (text_state & TTF_STYLE_ITALIC) - SDL_BlitSurface(img_btn_down, NULL, screen, &dest); - else - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + 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 - 48 + (48 - img_italic->w) / 2; - dest.y = (40 + ((5 + TOOLOFFSET / 2) * 48) + (48 - img_italic->h) / 2); + dest.x = WINDOW_WIDTH - 48 + (48 - img_italic->w) / 2; + dest.y = (40 + ((5 + TOOLOFFSET / 2) * 48) + (48 - img_italic->h) / 2); - SDL_BlitSurface(img_italic, NULL, screen, &dest); + SDL_BlitSurface(img_italic, NULL, screen, &dest); - /* Show shrink button: */ + /* Show shrink button: */ - dest.x = WINDOW_WIDTH - 96; - dest.y = 40 + ((6 + TOOLOFFSET / 2) * 48); - - 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 - 96 + (48 - img_shrink->w) / 2; - dest.y = (40 + ((6 + TOOLOFFSET / 2) * 48) + (48 - 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 - 48; - dest.y = 40 + ((6 + TOOLOFFSET / 2) * 48); - - 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 - 48 + (48 - img_grow->w) / 2; - dest.y = (40 + ((6 + TOOLOFFSET / 2) * 48) + (48 - img_grow->h) / 2); - - SDL_BlitSurface(button_color, NULL, img_grow, NULL); - SDL_BlitSurface(img_grow, NULL, screen, &dest); - } - else - { - if (cur_tool == TOOL_LABEL) - { dest.x = WINDOW_WIDTH - 96; dest.y = 40 + ((6 + TOOLOFFSET / 2) * 48); - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + 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 - 96 + (48 - img_label->w) / 2; - dest.y = (40 + ((6 + TOOLOFFSET / 2) * 48) + (48 - img_label->h) / 2); + dest.x = WINDOW_WIDTH - 96 + (48 - img_shrink->w) / 2; + dest.y = (40 + ((6 + TOOLOFFSET / 2) * 48) + (48 - img_shrink->h) / 2); - SDL_BlitSurface(img_label, NULL, screen, &dest); + SDL_BlitSurface(button_color, NULL, img_shrink, NULL); + SDL_BlitSurface(img_shrink, NULL, screen, &dest); + + + /* Show grow button: */ dest.x = WINDOW_WIDTH - 48; dest.y = 40 + ((6 + TOOLOFFSET / 2) * 48); - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + 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 - 48 + (48 - img_label_select->w) / 2; - dest.y = (40 + ((6 + TOOLOFFSET / 2) * 48) + (48 - img_label_select->h) / 2); + dest.x = WINDOW_WIDTH - 48 + (48 - img_grow->w) / 2; + dest.y = (40 + ((6 + TOOLOFFSET / 2) * 48) + (48 - img_grow->h) / 2); - SDL_BlitSurface(img_label_select, NULL, screen, &dest); + SDL_BlitSurface(button_color, NULL, img_grow, NULL); + SDL_BlitSurface(img_grow, NULL, screen, &dest); + } + else + { + if (cur_tool == TOOL_LABEL) + { + dest.x = WINDOW_WIDTH - 96; + dest.y = 40 + ((6 + TOOLOFFSET / 2) * 48); + + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + + dest.x = WINDOW_WIDTH - 96 + (48 - img_label->w) / 2; + dest.y = (40 + ((6 + TOOLOFFSET / 2) * 48) + (48 - img_label->h) / 2); + + SDL_BlitSurface(img_label, NULL, screen, &dest); + + dest.x = WINDOW_WIDTH - 48; + dest.y = 40 + ((6 + TOOLOFFSET / 2) * 48); + + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + + dest.x = WINDOW_WIDTH - 48 + (48 - img_label_select->w) / 2; + dest.y = (40 + ((6 + TOOLOFFSET / 2) * 48) + (48 - img_label_select->h) / 2); + + SDL_BlitSurface(img_label_select, NULL, screen, &dest); + } } - } } @@ -8827,7 +8760,7 @@ static void draw_stamps(void) /* How many can we show? */ - most = 8; /* was 10 and 14, before left/right controls -bjk 2007.05.03 */ + most = 8; /* was 10 and 14, before left/right controls -bjk 2007.05.03 */ if (disable_stamp_controls) most = 12; @@ -8835,93 +8768,90 @@ static void draw_stamps(void) /* Do we need scrollbars? */ if (num_stamps[stamp_group] > most + TOOLOFFSET) - { - off_y = 24; - max = (most - 2) + TOOLOFFSET; - - dest.x = WINDOW_WIDTH - 96; - dest.y = 40; - - if (stamp_scroll[stamp_group] > 0) { - SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); - } + off_y = 24; + max = (most - 2) + TOOLOFFSET; + + dest.x = WINDOW_WIDTH - 96; + dest.y = 40; + + 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 - 96; - dest.y = 40 + 24 + ((5 + TOOLOFFSET / 2) * 48); /* was 6, before left/right controls -bjk 2007.05.03 */ + dest.x = WINDOW_WIDTH - 96; + dest.y = 40 + 24 + ((5 + TOOLOFFSET / 2) * 48); /* was 6, before left/right controls -bjk 2007.05.03 */ - if (!disable_stamp_controls) - dest.y = dest.y - (48 * 2); + if (!disable_stamp_controls) + dest.y = dest.y - (48 * 2); - if (stamp_scroll[stamp_group] < - num_stamps[stamp_group] - (most - 2) - TOOLOFFSET) - { - SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + 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 - { - 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++) - { - i = stamp - stamp_scroll[stamp_group]; - - - dest.x = ((i % 2) * 48) + (WINDOW_WIDTH - 96); - dest.y = ((i / 2) * 48) + 40 + off_y; - - if (stamp == cur_stamp[stamp_group]) + for (stamp = stamp_scroll[stamp_group]; stamp < stamp_scroll[stamp_group] + max; stamp++) { - SDL_BlitSurface(img_btn_down, NULL, screen, &dest); + i = stamp - stamp_scroll[stamp_group]; + + + dest.x = ((i % 2) * 48) + (WINDOW_WIDTH - 96); + dest.y = ((i / 2) * 48) + 40 + 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]) + { + get_stamp_thumb(stamp_data[stamp_group][stamp]); + img = stamp_data[stamp_group][stamp]->thumbnail; + + base_x = ((i % 2) * 48) + (WINDOW_WIDTH - 96) + ((48 - (img->w)) / 2); + + base_y = ((i / 2) * 48) + 40 + ((48 - (img->h)) / 2) + off_y; + + dest.x = base_x; + dest.y = base_y; + + SDL_BlitSurface(img, 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]) - { - get_stamp_thumb(stamp_data[stamp_group][stamp]); - img = stamp_data[stamp_group][stamp]->thumbnail; - - base_x = ((i % 2) * 48) + (WINDOW_WIDTH - 96) + ((48 - (img->w)) / 2); - - base_y = ((i / 2) * 48) + 40 + ((48 - (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): */ /* Show prev button: */ - + button_color = img_black; button_body = img_btn_nav; @@ -8940,7 +8870,7 @@ static void draw_stamps(void) button_color = img_black; button_body = img_btn_nav; - + dest.x = WINDOW_WIDTH - 48; dest.y = 40 + (((most + TOOLOFFSET) / 2) * 48); @@ -8956,153 +8886,151 @@ static void draw_stamps(void) /* Draw stamp controls: */ if (!disable_stamp_controls) - { - /* Show mirror button: */ - - dest.x = WINDOW_WIDTH - 96; - dest.y = 40 + ((5 + TOOLOFFSET / 2) * 48); - - 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; - } + /* Show mirror button: */ + + dest.x = WINDOW_WIDTH - 96; + dest.y = 40 + ((5 + TOOLOFFSET / 2) * 48); + + 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; + } + } else - { - 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); + { + button_color = img_grey; + button_body = img_btn_off; + } + SDL_BlitSurface(button_body, NULL, screen, &dest); - dest.x = WINDOW_WIDTH - 96 + (48 - img_mirror->w) / 2; - dest.y = (40 + ((5 + TOOLOFFSET / 2) * 48) + - (48 - img_mirror->h) / 2); + dest.x = WINDOW_WIDTH - 96 + (48 - img_mirror->w) / 2; + dest.y = (40 + ((5 + TOOLOFFSET / 2) * 48) + (48 - 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 - 48; - dest.y = 40 + ((5 + TOOLOFFSET / 2) * 48); + dest.x = WINDOW_WIDTH - 48; + dest.y = 40 + ((5 + TOOLOFFSET / 2) * 48); - 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; - } + 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; + } + } else - { - 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); + { + button_color = img_grey; + button_body = img_btn_off; + } + SDL_BlitSurface(button_body, NULL, screen, &dest); - dest.x = WINDOW_WIDTH - 48 + (48 - img_flip->w) / 2; - dest.y = (40 + ((5 + TOOLOFFSET / 2) * 48) + - (48 - img_flip->h) / 2); + dest.x = WINDOW_WIDTH - 48 + (48 - img_flip->w) / 2; + dest.y = (40 + ((5 + TOOLOFFSET / 2) * 48) + (48 - 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 - 96; - dest.y = 40 + ((6 + TOOLOFFSET / 2) * 48); + dest.x = WINDOW_WIDTH - 96; + dest.y = 40 + ((6 + TOOLOFFSET / 2) * 48); - 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 - 96 + (48 - img_shrink->w) / 2; - dest.y = (40 + ((6 + TOOLOFFSET / 2) * 48) + (48 - img_shrink->h) / 2); + dest.x = WINDOW_WIDTH - 96 + (48 - img_shrink->w) / 2; + dest.y = (40 + ((6 + TOOLOFFSET / 2) * 48) + (48 - 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 - 48; - dest.y = 40 + ((6 + TOOLOFFSET / 2) * 48); + dest.x = WINDOW_WIDTH - 48; + dest.y = 40 + ((6 + TOOLOFFSET / 2) * 48); - 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 - 48 + (48 - img_grow->w) / 2; - dest.y = (40 + ((6 + TOOLOFFSET / 2) * 48) + (48 - img_grow->h) / 2); + dest.x = WINDOW_WIDTH - 48 + (48 - img_grow->w) / 2; + dest.y = (40 + ((6 + TOOLOFFSET / 2) * 48) + (48 - 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); - x_per = 96.0 / sizes; - y_per = 48.0 / sizes; + 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 = 96.0 / sizes; + y_per = 48.0 / 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, 48 - yy, 0); + blnk = thumbnail(img_btn_off, xx, 48 - yy, 0); - /* FIXME: Check for NULL! */ + /* FIXME: Check for NULL! */ - dest.x = (WINDOW_WIDTH - 96) + (i * x_per); - dest.y = (((7 + TOOLOFFSET / 2) * 48)) - 8; - SDL_BlitSurface(blnk, NULL, screen, &dest); + dest.x = (WINDOW_WIDTH - 96) + (i * x_per); + dest.y = (((7 + TOOLOFFSET / 2) * 48)) - 8; + SDL_BlitSurface(blnk, NULL, screen, &dest); - dest.x = (WINDOW_WIDTH - 96) + (i * x_per); - dest.y = (((8 + TOOLOFFSET / 2) * 48)) - 8 - (y_per * i); - SDL_BlitSurface(btn, NULL, screen, &dest); + dest.x = (WINDOW_WIDTH - 96) + (i * x_per); + dest.y = (((8 + TOOLOFFSET / 2) * 48)) - 8 - (y_per * i); + SDL_BlitSurface(btn, NULL, screen, &dest); - SDL_FreeSurface(btn); - SDL_FreeSurface(blnk); - } + SDL_FreeSurface(btn); + SDL_FreeSurface(blnk); + } #endif - } + } } @@ -9118,76 +9046,74 @@ static void draw_shapes(void) if (NUM_SHAPES > 14 + TOOLOFFSET) - { - off_y = 24; - max = 12 + TOOLOFFSET; - - dest.x = WINDOW_WIDTH - 96; - dest.y = 40; - - if (shape_scroll > 0) { - SDL_BlitSurface(img_scroll_up, NULL, screen, &dest); - } - else - { - SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); - } + off_y = 24; + max = 12 + TOOLOFFSET; - dest.x = WINDOW_WIDTH - 96; - dest.y = 40 + 24 + ((6 + TOOLOFFSET / 2) * 48); + dest.x = WINDOW_WIDTH - 96; + dest.y = 40; - if (shape_scroll < NUM_SHAPES - 12 - TOOLOFFSET) - { - SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + 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 - 96; + dest.y = 40 + 24 + ((6 + TOOLOFFSET / 2) * 48); + + if (shape_scroll < NUM_SHAPES - 12 - TOOLOFFSET) + { + SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + } + else + { + SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); + } } - else - { - SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); - } - } else - { - off_y = 0; - max = 14 + TOOLOFFSET; - } + { + off_y = 0; + max = 14 + TOOLOFFSET; + } for (shape = shape_scroll; shape < shape_scroll + max; shape++) - { - i = shape - shape_scroll; - - dest.x = ((i % 2) * 48) + WINDOW_WIDTH - 96; - dest.y = ((i / 2) * 48) + 40 + off_y; - - if (shape == cur_shape) { - SDL_BlitSurface(img_btn_down, NULL, screen, &dest); + i = shape - shape_scroll; + + dest.x = ((i % 2) * 48) + WINDOW_WIDTH - 96; + dest.y = ((i / 2) * 48) + 40 + 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) * 48) + 4 + WINDOW_WIDTH - 96; + dest.y = ((i / 2) * 48) + 40 + 4 + off_y; + + SDL_BlitSurface(img_shapes[shape], NULL, screen, &dest); + + dest.x = ((i % 2) * 48) + 4 + WINDOW_WIDTH - 96 + (40 - img_shape_names[shape]->w) / 2; + dest.y = ((i / 2) * 48) + 40 + 4 + (44 - img_shape_names[shape]->h) + off_y; + + SDL_BlitSurface(img_shape_names[shape], 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) * 48) + 4 + WINDOW_WIDTH - 96; - dest.y = ((i / 2) * 48) + 40 + 4 + off_y; - - SDL_BlitSurface(img_shapes[shape], NULL, screen, &dest); - - dest.x = ((i % 2) * 48) + 4 + WINDOW_WIDTH - 96 + - (40 - img_shape_names[shape]->w) / 2; - dest.y = ((i / 2) * 48) + 40 + 4 + - (44 - img_shape_names[shape]->h) + off_y; - - SDL_BlitSurface(img_shape_names[shape], NULL, screen, &dest); - } - } } @@ -9205,104 +9131,95 @@ static void draw_erasers(void) draw_image_title(TITLE_ERASERS, r_ttoolopt); for (i = 0; i < 14 + TOOLOFFSET; i++) - { - dest.x = ((i % 2) * 48) + WINDOW_WIDTH - 96; - dest.y = ((i / 2) * 48) + 40; - - - 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); - } + dest.x = ((i % 2) * 48) + WINDOW_WIDTH - 96; + dest.y = ((i / 2) * 48) + 40; - if (i < NUM_ERASERS) - { - if (i < NUM_ERASERS / 2) - { - /* Square */ - - sz = - (2 + - (((NUM_ERASERS / 2) - 1 - i) * (38 / ((NUM_ERASERS / 2) - 1)))); - - x = ((i % 2) * 48) + WINDOW_WIDTH - 96 + 24 - sz / 2; - y = ((i / 2) * 48) + 40 + 24 - sz / 2; - - 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)); - } + 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 - { - /* Circle */ + { + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + } - sz = - (2 + - (((NUM_ERASERS / 2) - 1 - - (i - NUM_ERASERS / 2)) * (38 / ((NUM_ERASERS / 2) - 1)))); - x = ((i % 2) * 48) + WINDOW_WIDTH - 96 + 24 - sz / 2; - y = ((i / 2) * 48) + 40 + 24 - sz / 2; + if (i < NUM_ERASERS) + { + if (i < NUM_ERASERS / 2) + { + /* Square */ - for (yy = 0; yy <= sz; yy++) - { - for (xx = 0; xx <= sz; xx++) - { - n = (xx * xx) + (yy * yy) - ((sz / 2) * (sz / 2)); + sz = (2 + (((NUM_ERASERS / 2) - 1 - i) * (38 / ((NUM_ERASERS / 2) - 1)))); - if (n >= -sz && n <= sz) - { - putpixel(screen, (x + sz / 2) + xx, (y + sz / 2) + yy, - SDL_MapRGB(screen->format, 0, 0, 0)); + x = ((i % 2) * 48) + WINDOW_WIDTH - 96 + 24 - sz / 2; + y = ((i / 2) * 48) + 40 + 24 - sz / 2; - putpixel(screen, (x + sz / 2) - xx, (y + sz / 2) + yy, - SDL_MapRGB(screen->format, 0, 0, 0)); + dest.x = x; + dest.y = y; + dest.w = sz; + dest.h = 2; - putpixel(screen, (x + sz / 2) + xx, (y + sz / 2) - yy, - SDL_MapRGB(screen->format, 0, 0, 0)); + SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 0, 0, 0)); - putpixel(screen, (x + sz / 2) - xx, (y + sz / 2) - yy, - 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)))); + + x = ((i % 2) * 48) + WINDOW_WIDTH - 96 + 24 - sz / 2; + y = ((i / 2) * 48) + 40 + 24 - sz / 2; + + 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)); + + } + } + } + } + } } - } } @@ -9318,30 +9235,29 @@ static void draw_none(void) SDL_BlitSurface(img_title_off, NULL, screen, &dest); for (i = 0; i < 14 + TOOLOFFSET; i++) - { - dest.x = ((i % 2) * 48) + WINDOW_WIDTH - 96; - dest.y = ((i / 2) * 48) + 40; + { + dest.x = ((i % 2) * 48) + WINDOW_WIDTH - 96; + dest.y = ((i / 2) * 48) + 40; - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - } + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + } } /* 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)); } -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; SDL_Surface *s; + #ifdef GAMMA_CORRECTED_THUMBNAILS float tr, tg, tb, ta; #else @@ -9351,60 +9267,60 @@ static SDL_Surface *thumbnail2(SDL_Surface * src, int max_x, int max_y, float xscale, yscale; int tmp; 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); - - 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 - { - xscale = (float) ((float) src->w / (float) max_x); - yscale = xscale; + yscale = (float)((float)src->h / (float)max_y); + xscale = (float)((float)src->w / (float)max_x); off_x = 0; - off_y = ((src->w - src->h) / xscale) / 2; + 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 + { + xscale = (float)((float)src->w / (float)max_x); + yscale = xscale; + + off_x = 0; + off_y = ((src->w - src->h) / xscale) / 2; + } } - } #ifndef NO_BILINEAR if (max_x > src->w && max_y > src->h) - return(zoom(src, max_x, max_y)); + return (zoom(src, max_x, max_y)); #endif /* Create surface for thumbnail: */ - s = SDL_CreateRGBSurface(src->flags, /* SDL_SWSURFACE, */ - max_x, max_y, src->format->BitsPerPixel, src->format->Rmask, src->format->Gmask, src->format->Bmask, src->format->Amask); + s = SDL_CreateRGBSurface(src->flags, /* SDL_SWSURFACE, */ + 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]; @@ -9414,95 +9330,86 @@ static SDL_Surface *thumbnail2(SDL_Surface * src, int max_x, int max_y, 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 #ifdef GAMMA_CORRECTED_THUMBNAILS - /* per: http://www.4p8.com/eric.brasseur/gamma.html */ - float gamma = 2.2; - float gamma_invert = 1.0 / gamma; + /* per: http://www.4p8.com/eric.brasseur/gamma.html */ + float gamma = 2.2; + float gamma_invert = 1.0 / gamma; #endif - - tr = 0; - tg = 0; - tb = 0; - ta = 0; - tmp = 0; + tr = 0; + tg = 0; + tb = 0; + ta = 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); + 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); #ifdef GAMMA_CORRECTED_THUMBNAILS -// tr = tr + pow((float)r, gamma); -// tb = tb + pow((float)b, gamma); -// tg = tg + pow((float)g, gamma); - tr = tr + sRGB_to_linear_table[r]; - tg = tg + sRGB_to_linear_table[g]; - tb = tb + sRGB_to_linear_table[b]; +// tr = tr + pow((float)r, gamma); +// tb = tb + pow((float)b, gamma); +// tg = tg + pow((float)g, gamma); + 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++; - } - } + tmp++; + } + } - if (tmp != 0) - { - tr = tr / tmp; - tb = tb / tmp; - tg = tg / tmp; - ta = ta / tmp; + if (tmp != 0) + { + tr = tr / tmp; + tb = tb / tmp; + tg = tg / tmp; + ta = ta / tmp; #ifdef GAMMA_CORRECTED_THUMBNAILS // tr = ceil(pow(tr, gamma_invert)); // tg = ceil(pow(tg, gamma_invert)); // tb = ceil(pow(tb, gamma_invert)); - tr = linear_to_sRGB(tr); - tg = linear_to_sRGB(tg); - tb = linear_to_sRGB(tb); + 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); + 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)); - } - } + 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; + src_x = x * xscale; + src_y = y * yscale; - putpixel(s, x + off_x, y + off_y, getpixel(src, src_x, src_y)); + putpixel(s, x + off_x, y + off_y, getpixel(src, src_x, src_y)); #endif + } } - } SDL_UnlockSurface(s); SDL_UnlockSurface(src); @@ -9518,14 +9425,13 @@ static SDL_Surface *thumbnail2(SDL_Surface * src, int max_x, int max_y, static SDL_Surface *zoom(SDL_Surface * src, int new_w, int new_h) { - SDL_Surface * s; + SDL_Surface *s; 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; @@ -9536,23 +9442,19 @@ static SDL_Surface *zoom(SDL_Surface * src, int new_w, int new_h) /* Create surface for zoom: */ - 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); + 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); 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]; @@ -9560,109 +9462,115 @@ 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++) { - floor_x = floor((float) x * xscale); - ceil_x = floor_x + 1; - if (ceil_x >= src->w) - ceil_x = floor_x; + 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_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; - 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); - */ + { //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); + */ #else - { - Uint8 r, g, b, a; - r = g = b = a = 0; /* Unused, bah! */ + { + 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; + r = g = b = a = 0; /* Unused, bah! */ - 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, 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, 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, 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, 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, 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; + } #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); @@ -9681,8 +9589,7 @@ static void xorpixel(int x, int y) int BytesPerPixel; /* 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 */ x += r_canvas.x; @@ -9692,23 +9599,23 @@ static void xorpixel(int x, int y) BytesPerPixel = screen->format->BytesPerPixel; /* Set a pointer to the exact location in memory of the pixel */ - p = (Uint8 *) (((Uint8 *) screen->pixels) + /* Start: beginning of RAM */ - (y * screen->pitch) + /* Go down Y lines */ - (x * BytesPerPixel)); /* Go in X pixels */ + p = (Uint8 *) (((Uint8 *) screen->pixels) + /* Start: beginning of RAM */ + (y * screen->pitch) + /* Go down Y lines */ + (x * BytesPerPixel)); /* Go in X pixels */ /* XOR the (correctly-sized) piece of data in the screen's RAM */ if (likely(BytesPerPixel == 4)) - *(Uint32 *) p ^= 0x80808080u; /* 32-bit display */ + *(Uint32 *) p ^= 0x80808080u; /* 32-bit display */ else if (BytesPerPixel == 1) *p ^= 0x80; 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; + } } @@ -9721,61 +9628,60 @@ 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(); - } + 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 - 96), (48 * 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; } - update_canvas(0, 0, (WINDOW_WIDTH - 96), (48 * 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 } @@ -9785,44 +9691,43 @@ static void do_undo(void) static void do_redo(void) { if (cur_undo != newest_undo) - { - 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(); - } + 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 - 96), (48 * 7) + 40 + HEIGHTOFFSET); + + been_saved = 0; } - 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 - 96), (48 * 7) + 40 + HEIGHTOFFSET); - - been_saved = 0; - } - -#ifdef DEBUG - printf("REDO: Current=%d Oldest=%d Newest=%d\n", - cur_undo, oldest_undo, newest_undo); + 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; @@ -9838,10 +9743,9 @@ static void render_brush(void) Uint32 amask; 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! */ @@ -9853,35 +9757,32 @@ 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, - img_brushes[cur_brush]->w, - 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]->w, + 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); 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: */ @@ -9890,37 +9791,34 @@ 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++) { - SDL_GetRGBA(getpixel_brush(img_brushes[cur_brush], x, y), - img_brushes[cur_brush]->format, &r, &g, &b, &a); + 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); - 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_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]; @@ -9948,61 +9846,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) { - y1 = m * x1 + b; - y2 = m * (x1 + dx) + b; + m = ((float)dy) / ((float)dx); + b = y1 - m * x1; - if (y1 > y2) - { - y = y1; - y1 = y2; - y2 = y; - } + if (x2 >= x1) + dx = 1; + else + dx = -1; - for (y = y1; y <= y2; y++) - { - num_drawn++; - if (num_drawn < 10 || dont_do_xor == 0) - xorpixel(x1, y); - } - x1 = x1 + dx; + 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++; + 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); + } + } + else + { + 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); + } + } } - } /* SDL_UnlockSurface(screen); */ } @@ -10052,100 +9950,92 @@ static void do_eraser(int x, int y) int xx, yy, n, hit; if (cur_eraser < NUM_ERASERS / 2) - { - /* Square eraser: */ - - sz = (ERASER_MIN + - (((NUM_ERASERS / 2) - 1 - cur_eraser) * - ((ERASER_MAX - ERASER_MIN) / ((NUM_ERASERS / 2) - 1)))); - - 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)); + /* Square eraser: */ + + sz = (ERASER_MIN + + (((NUM_ERASERS / 2) - 1 - cur_eraser) * ((ERASER_MAX - ERASER_MIN) / ((NUM_ERASERS / 2) - 1)))); + + 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); + } } - else - { - SDL_BlitSurface(img_starter_bkgd, &dest, canvas, &dest); - } - } else - { - /* Round eraser: */ - - sz = (ERASER_MIN + - (((NUM_ERASERS / 2) - 1 - (cur_eraser - (NUM_ERASERS / 2))) * - ((ERASER_MAX - ERASER_MIN) / ((NUM_ERASERS / 2) - 1)))); - - for (yy = 0; yy < sz; yy++) { - hit = 0; - for (xx = 0; xx <= sz && hit == 0; xx++) - { - n = (xx * xx) + (yy * yy) - ((sz / 2) * (sz / 2)); + /* Round eraser: */ - if (n >= -sz && n <= sz) - hit = 1; + sz = (ERASER_MIN + + (((NUM_ERASERS / 2) - 1 - (cur_eraser - (NUM_ERASERS / 2))) * + ((ERASER_MAX - ERASER_MIN) / ((NUM_ERASERS / 2) - 1)))); - if (hit) - { - dest.x = x - xx; - dest.y = y - yy; - dest.w = xx * 2; - dest.h = 1; + for (yy = 0; yy < sz; yy++) + { + hit = 0; + for (xx = 0; xx <= sz && hit == 0; xx++) + { + n = (xx * xx) + (yy * yy) - ((sz / 2) * (sz / 2)); - 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 (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); + } - 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)) { - eraser_sound = (eraser_sound + 1) % 2; + if (!Mix_Playing(0)) + { + 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 update_canvas(x - sz / 2, y - sz / 2, x + sz / 2, y + sz / 2); @@ -10164,12 +10054,12 @@ static void do_eraser(int x, int y) static void reset_avail_tools(void) { int i; - int disallow_print = disable_print; /* set to 1 later if printer unavail */ + 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: */ @@ -10250,10 +10140,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; + } } /* Restore and enable available tools (for End-Of-Open-Dialog) */ @@ -10263,9 +10153,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]; + } } @@ -10286,14 +10176,13 @@ static int compare_dirent2s(struct dirent2 *f1, struct dirent2 *f2) /* 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); } static int latest_tux; -static const char * latest_tux_text; +static const char *latest_tux_text; static int latest_r2l; static Uint8 latest_locale_text; @@ -10302,13 +10191,12 @@ static void redraw_tux_text(void) draw_tux_text_ex(latest_tux, latest_tux_text, latest_r2l, latest_locale_text); } -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 }; int w; - SDL_Surface * btn; + SDL_Surface *btn; latest_tux = which_tux; latest_tux_text = str; @@ -10332,79 +10220,75 @@ static void draw_tux_text_ex(int which_tux, const char *const str, /* 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) + 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); } -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); } 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; SDL_Rect dest; + #ifdef NO_SDLPANGO int len; int x, y, j; @@ -10422,16 +10306,16 @@ static void wordwrap_text_ex(const char *const str, SDL_Color color, if (str == NULL || str[0] == '\0') - return; /* No-op! */ + return; /* No-op! */ if (need_own_font && (strcmp(gettext(str), str) || locale_text)) 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 @@ -10442,39 +10326,41 @@ static void wordwrap_text_ex(const char *const str, SDL_Color color, SDLPango_SetDefaultColor(myfont->pango_context, &pango_color); 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) { - 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); - } - else - { - SDLPango_SetBaseDirection(locale_font->pango_context, SDLPANGO_DIRECTION_LTR); - if (only_uppercase) - { - 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_SetBaseDirection(locale_font->pango_context, SDLPANGO_DIRECTION_RTL); + if (only_uppercase) + { + char *upper_str = uppercase(gettext(str)); - text = SDLPango_CreateSurfaceDraw(myfont->pango_context); + 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) + { + 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); + } + + 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: */ @@ -10489,314 +10375,314 @@ 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 (locale_str[i] < 128) - { - utf8_str[utf8_str_len++] = locale_str[i]; - utf8_str[utf8_str_len] = '\0'; + if (want_right_to_left == 0) + locale_str = (unsigned char *)strdup(gettext(str)); + else + locale_str = (unsigned char *)textdir(gettext(str)); - /* Space? Blit the word! (Word-wrap if necessary) */ + /* For each UTF8 character: */ - 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); - } + 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 - text = render_text(myfont, (char *) utf8_str, color); + { + 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; + } + } - 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); } - - free(locale_str); - } else if (strlen(str) != 0) - { - /* Truncate if too big! (sorry!) */ - { - char *s1 = gettext(str); - if (want_right_to_left) + /* Truncate if too big! (sorry!) */ + { - char *freeme = s1; - s1 = textdir(s1); - free(freeme); + char *s1 = gettext(str); + + if (want_right_to_left) + { + char *freeme = s1; + + s1 = textdir(s1); + free(freeme); + } + tstr = uppercase(s1); + free(s1); } - 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); } - 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 } @@ -10807,16 +10693,16 @@ 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..."); + 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..."); - 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 @@ -10834,15 +10720,15 @@ static Mix_Chunk *loadsound_extra(const char *const fname, const char *extra) if (strcasestr(fname, ".png") != NULL) - { - strcpy(ext, ".png"); - } + { + strcpy(ext, ".png"); + } else - { - /* Sorry, we only handle images */ + { + /* Sorry, we only handle images */ - return (NULL); - } + return (NULL); + } /* First, check for localized version of sound: */ @@ -10850,71 +10736,71 @@ static Mix_Chunk *loadsound_extra(const char *const fname, const char *extra) strcpy(snd_fname, fname); snprintf(tmp_str, sizeof(tmp_str), "%s_%s.ogg", extra, lang_prefix); - strcpy((char *) strcasestr(snd_fname, ext), tmp_str); + strcpy((char *)strcasestr(snd_fname, ext), tmp_str); debug(snd_fname); tmp_snd = Mix_LoadWAV(snd_fname); if (tmp_snd == NULL) - { - debug("...No local version of sound (OGG)!"); - - strcpy(snd_fname, fname); - snprintf(tmp_str, sizeof(tmp_str), "%s_%s.wav", extra, lang_prefix); - strcpy((char *) strcasestr(snd_fname, ext), tmp_str); - debug(snd_fname); - tmp_snd = Mix_LoadWAV(snd_fname); - - if (tmp_snd == NULL) { - debug("...No local version of sound (WAV)!"); - - /* Check for non-country-code locale */ + debug("...No local version of sound (OGG)!"); strcpy(snd_fname, fname); - snprintf(tmp_str, sizeof(tmp_str), "%s_%s.ogg", extra, short_lang_prefix); - strcpy((char *) strcasestr(snd_fname, ext), tmp_str); + snprintf(tmp_str, sizeof(tmp_str), "%s_%s.wav", extra, lang_prefix); + strcpy((char *)strcasestr(snd_fname, ext), tmp_str); 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); - snprintf(tmp_str, sizeof(tmp_str), "%s_%s.wav", extra, short_lang_prefix); - strcpy((char *) strcasestr(snd_fname, ext), tmp_str); - 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)!"); + debug("...No local version of sound (WAV)!"); + + /* Check for non-country-code locale */ strcpy(snd_fname, fname); - snprintf(tmp_str, sizeof(tmp_str), "%s.ogg", extra); - strcpy((char *) strcasestr(snd_fname, ext), tmp_str); + snprintf(tmp_str, sizeof(tmp_str), "%s_%s.ogg", extra, short_lang_prefix); + strcpy((char *)strcasestr(snd_fname, ext), tmp_str); debug(snd_fname); tmp_snd = Mix_LoadWAV(snd_fname); if (tmp_snd == NULL) - { - debug("...No default version of sound (OGG)!"); + { + debug("...No short local version of sound (OGG)!"); - strcpy(snd_fname, fname); - snprintf(tmp_str, sizeof(tmp_str), "%s.wav", extra); - strcpy((char *) strcasestr(snd_fname, ext), tmp_str); - debug(snd_fname); - tmp_snd = Mix_LoadWAV(snd_fname); + strcpy(snd_fname, fname); + snprintf(tmp_str, sizeof(tmp_str), "%s_%s.wav", extra, short_lang_prefix); + strcpy((char *)strcasestr(snd_fname, ext), tmp_str); + debug(snd_fname); + tmp_snd = Mix_LoadWAV(snd_fname); - if (tmp_snd == NULL) - debug("...No default version of sound (WAV)!"); - } + if (tmp_snd == NULL) + { + /* Now, check for default sound: */ + + debug("...No short local version of sound (WAV)!"); + + strcpy(snd_fname, fname); + snprintf(tmp_str, sizeof(tmp_str), "%s.ogg", extra); + strcpy((char *)strcasestr(snd_fname, ext), tmp_str); + debug(snd_fname); + tmp_snd = Mix_LoadWAV(snd_fname); + + if (tmp_snd == NULL) + { + debug("...No default version of sound (OGG)!"); + + strcpy(snd_fname, fname); + snprintf(tmp_str, sizeof(tmp_str), "%s.wav", extra); + strcpy((char *)strcasestr(snd_fname, ext), tmp_str); + debug(snd_fname); + tmp_snd = Mix_LoadWAV(snd_fname); + + if (tmp_snd == NULL) + debug("...No default version of sound (WAV)!"); + } + } + } } - } } - } free(snd_fname); return (tmp_snd); @@ -10939,12 +10825,13 @@ static Mix_Chunk *loaddescsound(const char *const fname) 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'; + } } @@ -10969,100 +10856,100 @@ static char *loaddesc(const char *const fname, Uint8 * locale_text) #endif if (extptr != NULL) - { - found = 0; - - /* Set the first available language */ - for(i = 0; i < num_wished_langs && !found; i++) { - strcpy((char *) extptr, ".txt"); + found = 0; - fi = fopen(txt_fname, "r"); + /* Set the first available language */ + for (i = 0; i < num_wished_langs && !found; i++) + { + strcpy((char *)extptr, ".txt"); - if (!fi) - return NULL; + fi = fopen(txt_fname, "r"); + + if (!fi) + return NULL; - got_first = 0; - // found = 0; + got_first = 0; + // found = 0; - strcpy(def_buf, ""); + strcpy(def_buf, ""); - do - { - fgets(buf, sizeof(buf), fi); + do + { + fgets(buf, sizeof(buf), fi); - if (!feof(fi)) - { - strip_trailing_whitespace(buf); + if (!feof(fi)) + { + strip_trailing_whitespace(buf); - if (!got_first) - { - /* First one is the default: */ + if (!got_first) + { + /* First one is the default: */ - strcpy(def_buf, buf); - got_first = 1; - } + strcpy(def_buf, buf); + got_first = 1; + } - debug(buf); + debug(buf); - // lang_prefix = lang_prefixes[langint]; - /* See if it's the one for this locale... */ + // lang_prefix = lang_prefixes[langint]; + /* See if it's the one for this locale... */ - if ((char *) strcasestr(buf, wished_langs[i].lang_prefix) == buf) - { + 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'; + 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; + 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; + found = 1; - debug("...FOUND!"); - } - } - } + debug("...FOUND!"); + } + } + } + } + while (!feof(fi) && !found); + + fclose(fi); + + } + free(txt_fname); + + + /* 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)); + } } - while (!feof(fi) && !found); - - fclose(fi); - - } - free(txt_fname); - - - /* 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 - { - return NULL; - } + { + return NULL; + } } @@ -11084,92 +10971,92 @@ static double loadinfo(const char *const fname, stamp_type * inf) return ratio; do - { - fgets(buf, sizeof buf, fi); - - if (!feof(fi)) { - strip_trailing_whitespace(buf); + fgets(buf, sizeof buf, fi); - 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); - } + if (!feof(fi)) + { + strip_trailing_whitespace(buf); - /* printf("tinter=%d\n", inf->tinter); */ - } - else if (strcmp(buf, "nomirror") == 0) - inf->mirrorable = 0; - else if (strcmp(buf, "noflip") == 0) - inf->flipable = 0; + 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); + } + + /* printf("tinter=%d\n", inf->tinter); */ + } + else if (strcmp(buf, "nomirror") == 0) + inf->mirrorable = 0; + else if (strcmp(buf, "noflip") == 0) + inf->flipable = 0; + } } - } while (!feof(fi)); fclose(fi); @@ -11177,8 +11064,7 @@ static double loadinfo(const char *const fname, stamp_type * inf) } -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; @@ -11186,56 +11072,55 @@ static int SDLCALL NondefectiveBlit(SDL_Surface * src, SDL_Rect * srcrect, int srcy = 0; 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; - } - if (dstrect) - { - dstx = dstrect->x; - dsty = dstrect->y; - } - if (dsty < 0) - { - srcy += -dsty; - srch -= -dsty; - dsty = 0; - } - if (dstx < 0) - { - srcx += -dstx; - srcw -= -dstx; - dstx = 0; - } - if (dstx + srcw > dst->w - 1) - { - srcw -= (dstx + srcw) - (dst->w - 1); - } - if (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 */ - while (i--) { - putpixel(dst, i + dstx, srch + dsty, - getpixel(src, i + srcx, srch + srcy)); + srcx = srcrect->x; + srcy = srcrect->y; + srcw = srcrect->w; + srch = srcrect->h; + } + if (dstrect) + { + dstx = dstrect->x; + dsty = dstrect->y; + } + if (dsty < 0) + { + srcy += -dsty; + srch -= -dsty; + dsty = 0; + } + if (dstx < 0) + { + srcx += -dstx; + srcw -= -dstx; + dstx = 0; + } + if (dstx + srcw > dst->w - 1) + { + srcw -= (dstx + srcw) - (dst->w - 1); + } + if (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 */ + + while (i--) + { + putpixel(dst, i + dstx, srch + dsty, getpixel(src, i + srcx, srch + srcy)); + } } - } return (0); } @@ -11243,82 +11128,83 @@ static int SDLCALL NondefectiveBlit(SDL_Surface * src, SDL_Rect * srcrect, /* For the 3rd arg, pass either NondefectiveBlit or SDL_BlitSurface. */ 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)) + int SDLCALL(*blit) (SDL_Surface * src, + SDL_Rect * srcrect, SDL_Surface * dst, SDL_Rect * dstrect)) { SDL_Surface *src1; SDL_Rect dest; + /* What to do when in 640x480 mode, and loading an 800x600 (or larger) image!? Scale it. Starters must 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; - sour.w = 1; - sour.x = 0; - sour.h = src->h; - sour.y = 0; - while (i-- > 0) { - dest.x = i; - blit(src, &sour, dst, &dest); + /* 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.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; - sour.w = src->w; - sour.x = 0; - sour.h = 1; - sour.y = 0; - while (i-- > 0) { - dest.y = i; - blit(src, &sour, dst, &dest); + /* 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.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); } -static void load_starter_id(char *saved_id, FILE *fil) +static void load_starter_id(char *saved_id, FILE * fil) { char *rname; char fname[FILENAME_MAX]; @@ -11327,106 +11213,109 @@ static void load_starter_id(char *saved_id, FILE *fil) int r, g, b; rname = NULL; - - if (saved_id != NULL) - { - snprintf(fname, sizeof(fname), "saved/%s.dat", saved_id); - rname = get_fname(fname, DIR_SAVE); - fi = fopen(rname, "r"); - } + if (saved_id != NULL) + { + snprintf(fname, sizeof(fname), "saved/%s.dat", saved_id); + rname = get_fname(fname, DIR_SAVE); + + fi = fopen(rname, "r"); + } else - fi = fil; + fi = fil; starter_id[0] = '\0'; template_id[0] = '\0'; if (fi != NULL) - { - fgets(starter_id, sizeof(starter_id), fi); - starter_id[strlen(starter_id) - 1] = '\0'; - - fscanf(fi, "%d", &starter_mirrored); - fscanf(fi, "%d", &starter_flipped); - fscanf(fi, "%d", &starter_personal); - - do { - color_tag = fgetc(fi); - } - while ((color_tag == '\n' || color_tag == '\r') && !feof(fi)); + fgets(starter_id, sizeof(starter_id), fi); + starter_id[strlen(starter_id) - 1] = '\0'; - if (!feof(fi) && color_tag == 'c') - { - fscanf(fi, "%d", &r); - fscanf(fi, "%d", &g); - fscanf(fi, "%d", &b); + fscanf(fi, "%d", &starter_mirrored); + fscanf(fi, "%d", &starter_flipped); + fscanf(fi, "%d", &starter_personal); - canvas_color_r = (Uint8) r; - canvas_color_g = (Uint8) g; - canvas_color_b = (Uint8) b; + do + { + color_tag = fgetc(fi); + } + while ((color_tag == '\n' || color_tag == '\r') && !feof(fi)); + + if (!feof(fi) && color_tag == 'c') + { + fscanf(fi, "%d", &r); + fscanf(fi, "%d", &g); + 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; + } + + do + { + color_tag = fgetc(fi); + } + while ((color_tag == '\n' || color_tag == '\r') && !feof(fi)); + { + if (!feof(fi) && color_tag == 'T') + { + fgets(template_id, sizeof(template_id), fi); + template_id[strlen(template_id) - 1] = '\0'; + fscanf(fi, "%d", &template_personal); + printf("template = %s\n (Personal=%d)", template_id, template_personal); + } + if (!feof(fi) && color_tag == 'M') + { + starter_modified = fgetc(fi); + } + } + + fclose(fi); } - else + 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)); - { - if (!feof(fi) && color_tag == 'T') - { - fgets(template_id, sizeof(template_id), fi); - template_id[strlen(template_id) - 1] = '\0'; - fscanf(fi, "%d", &template_personal); - printf("template = %s\n (Personal=%d)", template_id, template_personal); - } - 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; - } - - if ( saved_id != NULL) - free(rname); + if (saved_id != NULL) + free(rname); } -static SDL_Surface * load_starter_helper(char * path_and_basename, char * extension, SDL_Surface * (*load_func)(char *)) { - char * ext; +static SDL_Surface *load_starter_helper(char *path_and_basename, char *extension, SDL_Surface * (*load_func) (char *)) +{ + char *ext; char fname[256]; - SDL_Surface * surf; + SDL_Surface *surf; int i; ext = strdup(extension); snprintf(fname, sizeof(fname), "%s.%s", path_and_basename, ext); - surf = (*load_func)(fname); + surf = (*load_func) (fname); - if (surf == NULL) { - for (i = 0; i < strlen(ext); i++) { - ext[i] = toupper(ext[i]); + if (surf == NULL) + { + for (i = 0; i < strlen(ext); i++) + { + ext[i] = toupper(ext[i]); + } + snprintf(fname, sizeof(fname), "%s.%s", path_and_basename, ext); + surf = (*load_func) (fname); } - snprintf(fname, sizeof(fname), "%s.%s", path_and_basename, ext); - surf = (*load_func)(fname); - } free(ext); - return(surf); + return (surf); } @@ -11452,36 +11341,36 @@ static void load_starter(char *img_id) #ifndef NOSVG if (tmp_surf == NULL) - { - /* Try loading an SVG */ + { + /* Try loading an SVG */ - snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); - tmp_surf = load_starter_helper(fname, "svg", &load_svg); - } + snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); + tmp_surf = load_starter_helper(fname, "svg", &load_svg); + } #endif if (tmp_surf == NULL) - { - snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); - tmp_surf = load_starter_helper(fname, "png", &IMG_Load); - } + { + 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 */ - snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); - tmp_surf = load_starter_helper(fname, "kpx", &myIMG_Load); - } - + { + /* Try loading a KPX */ + 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: */ @@ -11490,38 +11379,38 @@ static void load_starter(char *img_id) #ifndef NOSVG /* (Try SVG) */ if (tmp_surf == NULL) - { - snprintf(fname, sizeof(fname), "%s/%s-back", dirname, img_id); - tmp_surf = load_starter_helper(fname, "svg", &load_svg); - } + { + 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) - { - snprintf(fname, sizeof(fname), "%s/%s-back", dirname, img_id); - tmp_surf = load_starter_helper(fname, "jpeg", &IMG_Load); - } + { + 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) */ - snprintf(fname, sizeof(fname), "%s/%s-back", dirname, img_id); - tmp_surf = load_starter_helper(fname, "jpg", &IMG_Load); - } + { + /* (Then just JPG) */ + 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) - { - snprintf(fname, sizeof(fname), "%s/%s-back", dirname, img_id); - tmp_surf = load_starter_helper(fname, "png", &IMG_Load); - } + { + 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 @@ -11529,95 +11418,88 @@ 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++) { - 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); + int x, y; - if (a < 255) - any_transparency = 1; - } - } + 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; - if (!any_transparency) - { - /* No transparency found! We MUST check for white pixels to save - the day! */ + any_transparency = 0; - for (y = 0; y < img_starter->h; y++) - { - for (x = 0; x < img_starter->w; x++) + for (y = 0; y < img_starter->h && !any_transparency; y++) { - p = getpixel(img_starter, x, y); - SDL_GetRGBA(p, img_starter->format, &r, &g, &b, &a); + 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 (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); } @@ -11646,56 +11528,53 @@ static void load_template(char *img_id) #ifndef NOSVG /* (Failed? Try SVG next) */ - if (tmp_surf == NULL) - { - snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); - tmp_surf = load_starter_helper(fname, "svg", &load_svg); - } + if (tmp_surf == NULL) + { + snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); + tmp_surf = load_starter_helper(fname, "svg", &load_svg); + } #endif /* (JPEG) */ if (tmp_surf == NULL) - { - snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); - tmp_surf = load_starter_helper(fname, "jpeg", &IMG_Load); - } + { + 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) */ - snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); - tmp_surf = load_starter_helper(fname, "jpg", &IMG_Load); - } + { + /* (Then just JPG) */ + 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) - { - snprintf(fname, sizeof(fname), "%s/%s", dirname, img_id); - tmp_surf = load_starter_helper(fname, "png", &IMG_Load); - } + { + 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); } @@ -11712,25 +11591,23 @@ 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'; - } - else - { - fgets(file_id, sizeof(file_id), fi); - if (strlen(file_id) > 0) { - file_id[strlen(file_id) - 1] = '\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 + { + fgets(file_id, sizeof(file_id), fi); + if (strlen(file_id) > 0) + { + file_id[strlen(file_id) - 1] = '\0'; + } + fclose(fi); } - fclose(fi); - } free(fname); } @@ -11750,18 +11627,18 @@ static void load_current(void) #ifdef AUTOSAVE_GOING_BACKGROUND /* Look for an automatically saved file */ - snprintf(ftmp, sizeof(ftmp),"saved/%s%s", AUTOSAVED_NAME, FNAME_EXTENSION); + snprintf(ftmp, sizeof(ftmp), "saved/%s%s", AUTOSAVED_NAME, FNAME_EXTENSION); fname = get_fname(ftmp, DIR_SAVE); 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) @@ -11770,80 +11647,77 @@ 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; - - 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'; + 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; + + 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); } - 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; + } } @@ -11857,15 +11731,12 @@ static int make_directory(const char *path, const char *errmsg) fname = get_fname(path, DIR_SAVE); 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; } @@ -11878,29 +11749,27 @@ static void save_current(void) FILE *fi; if (!make_directory("", "Can't create user data directory")) - { - draw_tux_text(TUX_OOPS, strerror(errno), 0); - return; - } + { + 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); } @@ -11908,58 +11777,49 @@ static void save_current(void) /* 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)); } 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) + 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)); } 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) + const char *const btn_no, SDL_Surface * img1, + 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)); } 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) + 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) { - 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)); } 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) + 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) { - 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_LEFT 96 #define PROMPT_W 440 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) + 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) { int oox, ooy, nx, ny; SDL_Event event; @@ -11969,7 +11829,8 @@ static int do_prompt_image_flash_snd(const char *const text, SDLKey key; SDLKey key_y, key_n; char *keystr; - SDL_Surface * backup; + SDL_Surface *backup; + #ifndef NO_PROMPT_SHADOWS int i; SDL_Surface *alpha_surf; @@ -12006,36 +11867,34 @@ static int do_prompt_image_flash_snd(const char *const text, playsound(screen, 0, SND_PROMPT, 1, SNDPOS_CENTER, SNDDIST_NEAR); 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->BitsPerPixel, + screen->format->Rmask, + screen->format->Gmask, screen->format->Bmask, screen->format->Amask); SDL_BlitSurface(screen, NULL, backup, NULL); for (w = 0; w <= 96; w = w + 2) - { - oox = ox - w; - ooy = oy - w; + { + oox = ox - w; + ooy = oy - w; - nx = PROMPT_LEFT + 96 - w + PROMPTOFFSETX; - ny = 94 + 96 - w + PROMPTOFFSETY; + nx = PROMPT_LEFT + 96 - w + PROMPTOFFSETX; + ny = 94 + 96 - w + PROMPTOFFSETY; - dest.x = ((nx * w) + (oox * (96 - w))) / 96; - dest.y = ((ny * w) + (ooy * (96 - w))) / 96; - dest.w = (PROMPT_W - 96 * 2) + w * 2; - dest.h = w * 2; - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 224 - w, 224 - w, 244 -w)); + dest.x = ((nx * w) + (oox * (96 - w))) / 96; + dest.y = ((ny * w) + (ooy * (96 - w))) / 96; + dest.w = (PROMPT_W - 96 * 2) + w * 2; + dest.h = w * 2; + SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 224 - w, 224 - w, 244 - w)); - 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 == 94) - SDL_BlitSurface(backup, NULL, screen, NULL); - } + if (w == 94) + SDL_BlitSurface(backup, NULL, screen, NULL); + } SDL_FreeSurface(backup); @@ -12044,33 +11903,31 @@ static int do_prompt_image_flash_snd(const char *const text, #ifndef NO_PROMPT_SHADOWS alpha_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, - (PROMPT_W - 96 * 2) + (w - 4) * 2, - (w - 4) * 2, - screen->format->BitsPerPixel, - screen->format->Rmask, - screen->format->Gmask, - screen->format->Bmask, - screen->format->Amask); + (PROMPT_W - 96 * 2) + (w - 4) * 2, + (w - 4) * 2, + screen->format->BitsPerPixel, + screen->format->Rmask, + screen->format->Gmask, screen->format->Bmask, screen->format->Amask); if (alpha_surf != NULL) - { - - SDL_FillRect(alpha_surf, NULL, SDL_MapRGB(alpha_surf->format, 0, 0, 0)); - SDL_SetSurfaceAlphaMod(alpha_surf, 64); - - - for (i = 8; i > 0; i = i - 2) { - dest.x = PROMPT_LEFT + 96 - (w - 4) + i + PROMPTOFFSETX; - dest.y = 94 + 96 - (w - 4) + i + PROMPTOFFSETY; - dest.w = (PROMPT_W - 96 * 2) + (w - 4) * 2; - dest.h = (w - 4) * 2; - SDL_BlitSurface(alpha_surf, NULL, screen, &dest); + SDL_FillRect(alpha_surf, NULL, SDL_MapRGB(alpha_surf->format, 0, 0, 0)); + SDL_SetSurfaceAlphaMod(alpha_surf, 64); + + + for (i = 8; i > 0; i = i - 2) + { + dest.x = PROMPT_LEFT + 96 - (w - 4) + i + PROMPTOFFSETX; + dest.y = 94 + 96 - (w - 4) + i + PROMPTOFFSETY; + dest.w = (PROMPT_W - 96 * 2) + (w - 4) * 2; + dest.h = (w - 4) * 2; + + SDL_BlitSurface(alpha_surf, NULL, screen, &dest); + } + + SDL_FreeSurface(alpha_surf); } - - SDL_FreeSurface(alpha_surf); - } #endif @@ -12091,17 +11948,17 @@ static int do_prompt_image_flash_snd(const char *const text, img1b = NULL; if (img1 != NULL) - { - if (img1->h > 64 && img2 != NULL /* Only scale if it matters */) { - img1b = thumbnail(img1, 80, 64, 1); - free_img1b = 1; + if (img1->h > 64 && img2 != NULL /* Only scale if it matters */ ) + { + img1b = thumbnail(img1, 80, 64, 1); + free_img1b = 1; + } + else + { + img1b = img1; + } } - else - { - img1b = img1; - } - } /* If we're showing any images on the right, determine the widest width @@ -12125,23 +11982,23 @@ 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, 100 + PROMPTOFFSETY, txt_right, 1); @@ -12152,39 +12009,39 @@ static int do_prompt_image_flash_snd(const char *const text, img_y = 100 + PROMPTOFFSETY + 4; 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) { - dest.x = img_left + (max_img_w - img2->w) / 2; - dest.y = img_y; + if (img2 != NULL) + { + 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; + 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 */ + } } - 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: */ @@ -12195,21 +12052,19 @@ static int do_prompt_image_flash_snd(const char *const text, /* (Bound to UTF8 domain, so always ask for UTF8 rendering!) */ - wordwrap_text(btn_yes, black, txt_btn_left, - 183 + PROMPTOFFSETY, txt_btn_right, 1); + wordwrap_text(btn_yes, black, txt_btn_left, 183 + PROMPTOFFSETY, txt_btn_right, 1); /* Draw no button: */ if (strlen(btn_no) != 0) - { - dest.x = btn_left; - dest.y = 230 + PROMPTOFFSETY; - SDL_BlitSurface(img_no, NULL, screen, &dest); + { + dest.x = btn_left; + dest.y = 230 + PROMPTOFFSETY; + SDL_BlitSurface(img_no, NULL, screen, &dest); - wordwrap_text(btn_no, black, txt_btn_left, - 235 + PROMPTOFFSETY, txt_btn_right, 1); - } + wordwrap_text(btn_no, black, txt_btn_left, 235 + PROMPTOFFSETY, txt_btn_right, 1); + } /* Draw Tux, waiting... */ @@ -12223,161 +12078,156 @@ static int do_prompt_image_flash_snd(const char *const text, ans = 0; do - { - 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; + 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_KEYUP, 24, NULL, NULL); + } + else if (event.type == SDL_KEYDOWN) + { + key = event.key.keysym.sym; - handle_keymouse(key, SDL_KEYDOWN, 24, NULL, NULL); + handle_keymouse(key, SDL_KEYDOWN, 24, NULL, NULL); - /* FIXME: Should use SDLK_{c} instead of '{c}'? How? */ + /* FIXME: Should use SDLK_{c} instead of '{c}'? How? */ - if (key == key_y || key == SDLK_RETURN) - { - /* Y or ENTER - Yes! */ + 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! */ + 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: */ + 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 >= 178 + PROMPTOFFSETY && - event.button.y < 178 + PROMPTOFFSETY + img_yes->h) - { - ans = 1; - done = 1; - } - else if (strlen(btn_no) != 0 && - event.button.y >= 230 + PROMPTOFFSETY && - event.button.y < 230 + PROMPTOFFSETY + 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 >= 178 + PROMPTOFFSETY && - event.button.y < 178 + img_yes->h + PROMPTOFFSETY) || - (strlen(btn_no) != 0 && - event.button.y >= 230 + PROMPTOFFSETY && - event.button.y < 230 + img_yes->h + PROMPTOFFSETY))) - { - do_setcursor(cursor_hand); - } - else - { - do_setcursor(cursor_arrow); - } - oldpos_x = event.button.x; - oldpos_y = event.button.y; - } + 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 >= 178 + PROMPTOFFSETY && event.button.y < 178 + PROMPTOFFSETY + img_yes->h) + { + ans = 1; + done = 1; + } + else if (strlen(btn_no) != 0 && + event.button.y >= 230 + PROMPTOFFSETY && event.button.y < 230 + PROMPTOFFSETY + 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 >= 178 + PROMPTOFFSETY && + event.button.y < 178 + img_yes->h + PROMPTOFFSETY) || + (strlen(btn_no) != 0 && + event.button.y >= 230 + PROMPTOFFSETY && event.button.y < 230 + img_yes->h + PROMPTOFFSETY))) + { + 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_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_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_JOYBALLMOTION) + handle_joyballmotion(event, oldpos_x, oldpos_y); - else if (event.type == SDL_JOYBUTTONDOWN) - handle_joybuttonupdown(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; + } + } } - - 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); @@ -12403,34 +12253,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][i]->stampname); + free(stamp_data[j][i]); + stamp_data[j][i] = NULL; + } + free(stamp_data[j]); } - free(stamp_data[j]); - } free_surface(&active_stamp); free_surface_array(img_brushes, num_brushes); @@ -12441,10 +12291,10 @@ static void cleanup(void) free_surface_array(img_tool_names, NUM_TOOLS); free_surface_array(img_title_names, NUM_TITLES); for (i = 0; i < num_magics; i++) - { - free_surface(&(magics[i].img_icon)); - free_surface(&(magics[i].img_name)); - } + { + free_surface(&(magics[i].img_icon)); + free_surface(&(magics[i].img_name)); + } free_surface_array(img_shapes, NUM_SHAPES); free_surface_array(img_shape_names, NUM_SHAPES); free_surface_array(img_tux, NUM_TIP_TUX); @@ -12532,18 +12382,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); @@ -12553,78 +12403,79 @@ static void cleanup(void) free_surface(&img_cur_brush); if (touched != NULL) - { - free(touched); - touched = NULL; - } + { + free(touched); + 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 */ + 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]) { - 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 (user_font_families[i]) + { + 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; + } } - } #endif #ifndef NOSOUND if (use_sound) - { - for (i = 0; i < NUM_SOUNDS; i++) { - if (sounds[i]) - { - Mix_FreeChunk(sounds[i]); - sounds[i] = NULL; - } - } + for (i = 0; i < NUM_SOUNDS; i++) + { + if (sounds[i]) + { + Mix_FreeChunk(sounds[i]); + sounds[i] = NULL; + } + } - Mix_CloseAudio(); - } + Mix_CloseAudio(); + } #endif for (i = 0; i < num_plugin_files; i++) @@ -12643,10 +12494,10 @@ static void cleanup(void) free_cursor(&cursor_rotate); 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); @@ -12661,37 +12512,36 @@ 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); + 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); } - 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) @@ -12725,12 +12575,12 @@ static void cleanup(void) 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; - } + if (surface_array) //EP added this line to avoid app crash + if (*surface_array) + { + SDL_FreeSurface(*surface_array); + *surface_array = NULL; + } } @@ -12738,11 +12588,11 @@ static void free_surface_array(SDL_Surface * surface_array[], int count) { int i; - if (surface_array) //EP added this line to avoid app crash - for (i = 0; i < count; ++i) - { - free_surface(&surface_array[i]); - } + if (surface_array) //EP added this line to avoid app crash + for (i = 0; i < count; ++i) + { + free_surface(&surface_array[i]); + } } @@ -12781,8 +12631,7 @@ static void update_shape(int cx, int ox1, int ox2, int cy, int oy1, int oy2, int static void do_shape(int cx, int cy, int ox, int oy, int rotn, int use_brush) { - int side, angle_skip, init_ang, rx, ry, rmax, x1, y1, x2, y2, xp, yp, - xv, yv, old_brush, step; + int side, angle_skip, init_ang, rx, ry, rmax, x1, y1, x2, y2, xp, yp, xv, yv, old_brush, step; float a1, a2, rotn_rad; int xx, yy; @@ -12795,18 +12644,18 @@ static void do_shape(int cx, int cy, int ox, int oy, int rotn, int use_brush) int tmp = 0; if (cx > ox) - { - tmp = cx; - cx = ox; - ox = tmp; - } + { + tmp = cx; + cx = ox; + ox = tmp; + } if (cy > oy) - { - tmp = cy; - cy = oy; - oy = tmp; - } + { + tmp = cy; + cy = oy; + oy = tmp; + } x1 = cx; x2 = ox; @@ -12823,31 +12672,31 @@ static void do_shape(int cx, int cy, int ox, int oy, int rotn, int use_brush) /* 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; + } /* Render a default brush: */ if (use_brush) - { - old_brush = cur_brush; - cur_brush = shape_brush; /* Now only semi-ludgy! */ - render_brush(); - } + { + old_brush = cur_brush; + cur_brush = shape_brush; /* Now only semi-ludgy! */ + render_brush(); + } /* Draw the shape: */ @@ -12860,206 +12709,206 @@ static void do_shape(int cx, int cy, int ox, int oy, int rotn, int use_brush) 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); + } 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) { - rotn_rad = rotn * M_PI / 180; + a1 = (angle_skip * side + init_ang) * M_PI / 180; + a2 = (angle_skip * (side + 1) + init_ang) * M_PI / 180; - xp = x1 * cos(rotn_rad) - y1 * sin(rotn_rad); - yp = x1 * sin(rotn_rad) + y1 * cos(rotn_rad); + x1 = (int)(cos(a1) * rx); + y1 = (int)(-sin(a1) * ry); - x1 = xp; - y1 = yp; + x2 = (int)(cos(a2) * rx); + y2 = (int)(-sin(a2) * ry); - xp = x2 * cos(rotn_rad) - y2 * sin(rotn_rad); - yp = x2 * sin(rotn_rad) + y2 * cos(rotn_rad); - - x2 = xp; - y2 = yp; - - xp = xv * cos(rotn_rad) - yv * sin(rotn_rad); - yp = xv * sin(rotn_rad) + yv * cos(rotn_rad); - - xv = xp; - yv = yp; - } + xv = (int)(cos((a1 + a2) / 2) * rx * shape_valley[cur_shape] / 100); + yv = (int)(-sin((a1 + a2) / 2) * ry * shape_valley[cur_shape] / 100); - /* Center the line around the center of the shape: */ - - x1 = x1 + cx; - y1 = y1 + cy; - x2 = x2 + cx; - y2 = y2 + cy; - xv = xv + cx; - yv = yv + cy; - /* Draw: */ + /* Rotate the line: */ - if (!use_brush) - { - /* (XOR) */ - if (shape_valley[cur_shape] == 100) - line_xor(x1, y1, x2, y2); + if (rotn != 0) + { + rotn_rad = rotn * M_PI / 180; + + xp = x1 * cos(rotn_rad) - y1 * sin(rotn_rad); + yp = x1 * sin(rotn_rad) + y1 * cos(rotn_rad); + + x1 = xp; + y1 = yp; + + xp = x2 * cos(rotn_rad) - y2 * sin(rotn_rad); + yp = x2 * sin(rotn_rad) + y2 * cos(rotn_rad); + + x2 = xp; + y2 = yp; + + xp = xv * cos(rotn_rad) - yv * sin(rotn_rad); + yp = xv * sin(rotn_rad) + yv * cos(rotn_rad); + + xv = xp; + yv = yp; + } + + + /* Center the line around the center of the shape: */ + + x1 = x1 + cx; + y1 = y1 + cy; + x2 = x2 + cx; + y2 = y2 + cy; + xv = xv + cx; + yv = yv + cy; + + + /* 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 - { - line_xor(x1, y1, xv, yv); - line_xor(xv, yv, x2, y2); - } - } - else - { - if (shape_valley[cur_shape] == 100) - /* Brush */ + { + 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); - } + 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); + /* FIXME: In the meantime, we'll do this lame radius-based fill: */ - 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; + for (xx = max(abs(rx), abs(ry)); xx > 0; xx--) + { + yy = min(xx * rx / ry, xx * ry / rx); - if (yy == xx * ry / rx) - { - x1 = (int) (cos(a1) * xx); - y1 = (int) (-sin(a1) * yy); + 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; - x2 = (int) (cos(a2) * xx); - y2 = (int) (-sin(a2) * yy); + if (yy == xx * ry / rx) + { + x1 = (int)(cos(a1) * xx); + y1 = (int)(-sin(a1) * 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) * xx); + y2 = (int)(-sin(a2) * yy); - x2 = (int) (cos(a2) * yy); - y2 = (int) (-sin(a2) * xx); + 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); - xv = (int) (cos((a1 + a2) / 2) * yy * shape_valley[cur_shape] / 100); - yv = (int) (-sin((a1 + a2) / 2) * xx * shape_valley[cur_shape] / 100); - } + x2 = (int)(cos(a2) * yy); + y2 = (int)(-sin(a2) * xx); - /* Rotate the line: */ + xv = (int)(cos((a1 + a2) / 2) * yy * shape_valley[cur_shape] / 100); + yv = (int)(-sin((a1 + a2) / 2) * xx * shape_valley[cur_shape] / 100); + } - if (rotn != 0) - { - rotn_rad = rotn * M_PI / 180; + /* Rotate the line: */ - xp = x1 * cos(rotn_rad) - y1 * sin(rotn_rad); - yp = x1 * sin(rotn_rad) + y1 * cos(rotn_rad); + if (rotn != 0) + { + rotn_rad = rotn * M_PI / 180; - x1 = xp; - y1 = yp; + xp = x1 * cos(rotn_rad) - y1 * sin(rotn_rad); + yp = x1 * sin(rotn_rad) + y1 * cos(rotn_rad); - xp = x2 * cos(rotn_rad) - y2 * sin(rotn_rad); - yp = x2 * sin(rotn_rad) + y2 * cos(rotn_rad); + x1 = xp; + y1 = yp; - x2 = xp; - y2 = yp; + xp = x2 * cos(rotn_rad) - y2 * sin(rotn_rad); + yp = x2 * sin(rotn_rad) + y2 * cos(rotn_rad); - xp = xv * cos(rotn_rad) - yv * sin(rotn_rad); - yp = xv * sin(rotn_rad) + yv * cos(rotn_rad); + x2 = xp; + y2 = yp; - xv = xp; - yv = yp; - } + xp = xv * cos(rotn_rad) - yv * sin(rotn_rad); + yp = xv * sin(rotn_rad) + yv * cos(rotn_rad); + + xv = xp; + yv = yp; + } - /* Center the line around the center of the shape: */ + /* Center the line around the center of the shape: */ - x1 = x1 + cx; - y1 = y1 + cy; - x2 = x2 + cx; - y2 = y2 + cy; - xv = xv + cx; - yv = yv + cy; + x1 = x1 + cx; + y1 = y1 + cy; + x2 = x2 + cx; + y2 = y2 + cy; + xv = xv + cx; + yv = yv + cy; - /* 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); - } - } + /* 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 - 96, (48 * 7) + 40 + HEIGHTOFFSET); + if (xx % 10 == 0) + update_canvas(0, 0, WINDOW_WIDTH - 96, (48 * 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(cx - rmax, cy - rmax, cx + rmax, cy + rmax); - } + update_canvas(cx - rmax, cy - rmax, cx + rmax, cy + rmax); + } /* Return to normal brush (for paint brush and line tools): */ if (use_brush) - { - cur_brush = old_brush; - render_brush(); - } + { + cur_brush = old_brush; + render_brush(); + } } @@ -13078,13 +12927,14 @@ static int shape_rotation(int ctr_x, int ctr_y, int ox, int oy) deg = ((deg - 7) / 15) * 15; if (shape_locked[cur_shape]) - { - int angle_skip; - angle_skip = 360 / shape_sides[cur_shape]; - deg = deg % angle_skip; - } + { + int angle_skip; - return(deg); + angle_skip = 360 / shape_sides[cur_shape]; + deg = deg % angle_skip; + } + + return (deg); } @@ -13096,7 +12946,7 @@ static int brush_rotation(int ctr_x, int ctr_y, int ox, int oy) deg = (atan2(oy - ctr_y, ox - ctr_x) * 180 / M_PI); - return(deg); + return (deg); } @@ -13132,99 +12982,98 @@ static int do_save(int tool, int dont_show_success_results, int autosave) 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! */ + { + /* Never save over - _always_ save a new file! */ - get_new_file_id(); - } + 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? */ + /* Saving the same picture? */ - 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) * 48 + 24, - (TOOL_SAVE / 2) * 48 + 40 + 24) == 0) - { - /* No - Let's save a new picture! */ + if (file_id[0] != '\0') + { + /* We sure we want to do that? */ - get_new_file_id(); - } - if (tool == TOOL_TEXT || tool == TOOL_LABEL) - do_render_cur_text(0); + 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) * 48 + 24, (TOOL_SAVE / 2) * 48 + 40 + 24) == 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 - { - /* 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("", "Can't create user data directory")) - { - fprintf(stderr, "Cannot save the any pictures! SORRY!\n\n"); - draw_tux_text(TUX_OOPS, strerror(errno), 0); - return 0; - } + { + 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("saved", "Can't create user data directory")) - { - fprintf(stderr, "Cannot save any pictures! SORRY!\n\n"); - draw_tux_text(TUX_OOPS, strerror(errno), 0); - return 0; - } + { + 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("saved/.thumbs", "Can't create user data thumbnail directory")) - { - fprintf(stderr, "Cannot save any pictures! SORRY!\n\n"); - draw_tux_text(TUX_OOPS, strerror(errno), 0); - return 0; - } + { + 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); + } /* Save the file: */ @@ -13238,28 +13087,26 @@ 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)); - - draw_tux_text(TUX_OOPS, strerror(errno), 0); - } - else - { - if (!do_png_save(fi, fname, save_canvas, 1)) { - free(fname); - return 0; + 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); + } + else + { + if (!do_png_save(fi, fname, save_canvas, 1)) + { + free(fname); + return 0; + } } - } free(fname); - if(autosave) + if (autosave) /* No more process needed */ return 1; @@ -13274,18 +13121,17 @@ 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" */ - snprintf(tmp, sizeof(tmp), "saved/.thumbs/%s-t%s", file_id, - FNAME_EXTENSION); - fname = get_fname(tmp, DIR_SAVE); - } + snprintf(tmp, sizeof(tmp), "saved/.thumbs/%s-t%s", file_id, FNAME_EXTENSION); + fname = get_fname(tmp, DIR_SAVE); + } debug(fname); @@ -13293,48 +13139,39 @@ 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); - } + } SDL_FreeSurface(thm); free(fname); -#if 0 /* No more writing the .dat file */ +#if 0 /* No more writing the .dat file */ /* 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) - { - snprintf(tmp, sizeof(tmp), "saved/%s.dat", file_id); - fname = get_fname(tmp, DIR_SAVE); - fi = fopen(fname, "w"); - if (fi != NULL) + template_id[0] != '\0' || canvas_color_r != 255 || canvas_color_g != 255 || canvas_color_b != 255) { - 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); - } + 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); - } + free(fname); + } #endif /* All happy! */ @@ -13342,22 +13179,22 @@ 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(); - + return 1; } static void set_chunk_data(unsigned char **chunk_data, size_t * chunk_data_len, size_t uncompressed_size, Bytef * data, - size_t dataLen) + size_t dataLen) { int headersLen; unsigned int i; - char * line, * headers, * cdata; + char *line, *headers, *cdata; headersLen = 0; headers = calloc(256, 1); @@ -13377,8 +13214,8 @@ static void set_chunk_data(unsigned char **chunk_data, size_t * chunk_data_len, strcat(cdata, headers); for (i = 0; i < dataLen; i++) - cdata[headersLen + i] = data[i]; - *chunk_data = (unsigned char *) cdata; + cdata[headersLen + i] = data[i]; + *chunk_data = (unsigned char *)cdata; free(line); free(headers); @@ -13435,381 +13272,381 @@ static void do_png_embed_data(png_structp png_ptr) /* Starter foreground */ if (img_starter) - { - printf("Saving starter... %d\n", (int)(intptr_t) img_starter); //EP added (intptr_t) to avoid warning on x64 - sbk_pixs = malloc(img_starter->h * img_starter->w * 4); - compressedLen = compressBound(img_starter->h * img_starter->w * 4); + { + printf("Saving starter... %d\n", (int)(intptr_t) img_starter); //EP added (intptr_t) to avoid warning on x64 + 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) { - if (SDL_MUSTLOCK(img_starter)) - SDL_LockSurface(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); + 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); + 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); - 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); + 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); + + 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 (SDL_MUSTLOCK(img_starter_bkgd)) + SDL_UnlockSurface(img_starter_bkgd); + + printf("%d \n", (int)compressedLen); + + 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); + printf("%d \n", (int)compressedLen); + + + 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_bkgd)) - SDL_UnlockSurface(img_starter_bkgd); - - printf("%d \n", (int) compressedLen); - - 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); - printf("%d \n", (int) compressedLen); - - - 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++) { - 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); + sbk_pixs = malloc(label->h * label->w * 4); + compressedLen = (uLongf) compressBound(label->h * label->w * 4); + compressed_data = malloc(compressedLen * sizeof(Bytef *)); - 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_LockSurface(label); + if (SDL_MUSTLOCK(canvas)) + SDL_LockSurface(canvas); - if (SDL_MUSTLOCK(label)) - SDL_UnlockSurface(label); - if (SDL_MUSTLOCK(canvas)) - SDL_UnlockSurface(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) + { + SDL_GetRGB(getpixels[canvas->format->BytesPerPixel] (canvas, x, y), canvas->format, &r, &g, &b); - 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); + 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; + } + } + } - 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'; + if (SDL_MUSTLOCK(label)) + SDL_UnlockSurface(label); + if (SDL_MUSTLOCK(canvas)) + SDL_UnlockSurface(canvas); - 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); + 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); - /* Label data */ + 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); + + /* 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) - { - 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; - } + 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); + 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) - { + 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) + { #ifdef WIN32 - iconv_t trans; - wchar_t *wch; - char *conv, *conv2; - size_t in, out; + iconv_t trans; + wchar_t *wch; + char *conv, *conv2; + size_t in, out; - in = out = 1; - conv = malloc(255); - trans = iconv_open("UTF-8", "WCHAR_T"); + in = out = 1; + conv = malloc(255); + trans = iconv_open("UTF-8", "WCHAR_T"); - fprintf(lfi, "%u\n", current_node->save_texttool_len); - for (i = 0; i < current_node->save_texttool_len; i++) - { - conv2 =conv; - in = 2; - out = 10; - wch = ¤t_node->save_texttool_str[i]; - iconv(trans, (char **) &wch, &in, &conv, &out); - conv[0] = '\0'; - fprintf(lfi, "%s", conv2); - } + fprintf(lfi, "%u\n", current_node->save_texttool_len); + for (i = 0; i < current_node->save_texttool_len; i++) + { + conv2 = conv; + in = 2; + out = 10; + wch = ¤t_node->save_texttool_str[i]; + iconv(trans, (char **)&wch, &in, &conv, &out); + conv[0] = '\0'; + fprintf(lfi, "%s", conv2); + } #elif defined(__ANDROID__) - fprintf(lfi, "%u\n", current_node->save_texttool_len); + fprintf(lfi, "%u\n", current_node->save_texttool_len); - for (i = 0; i < current_node->save_texttool_len; i++) - { - fprintf(lfi, "%d ", (int) current_node->save_texttool_str[i]); - } + for (i = 0; i < current_node->save_texttool_len; i++) + { + fprintf(lfi, "%d ", (int)current_node->save_texttool_str[i]); + } #else - fprintf(lfi, "%u\n", current_node->save_texttool_len); + fprintf(lfi, "%u\n", current_node->save_texttool_len); - for (i = 0; i < current_node->save_texttool_len; i++) - { - fprintf(lfi, "%lc", (wint_t) current_node->save_texttool_str[i]); - } - #endif + for (i = 0; i < current_node->save_texttool_len; i++) + { + fprintf(lfi, "%lc", (wint_t) current_node->save_texttool_str[i]); + } +#endif - fprintf(lfi, "\n"); + 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); + 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); - } + 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); + 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++) - { - pix = - getpixels[current_node->label_node_surface->format->BytesPerPixel] (current_node->label_node_surface, x, - y); - SDL_GetRGBA(pix, current_label_node->label_node_surface->format, &r, &g, &b, &a); - fwrite(&a, alpha_size, 1, lfi); + 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++) + { + pix = + getpixels[current_node->label_node_surface->format->BytesPerPixel] (current_node-> + label_node_surface, x, y); + 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; - printf("cur %p, red %p\n", current_node, first_label_node_in_redo_stack); - } + } + SDL_UnlockSurface(current_node->label_node_surface); + fprintf(lfi, "\n\n"); + } + current_node = current_node->next_to_up_label_node; + printf("cur %p, red %p\n", current_node, first_label_node_in_redo_stack); + } #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++) + 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 = calloc(256, 1); - char_stream = calloc(256 + sizeof(starter_id) + sizeof(template_id) , 1); + { + /* Usually the .dat data are less than 100 bytes, hope this keeps line and char_stream in the safe side */ + line = calloc(256, 1); + char_stream = calloc(256 + sizeof(starter_id) + sizeof(template_id), 1); - sprintf(char_stream, "%s\n", starter_id); + sprintf(char_stream, "%s\n", starter_id); - sprintf(line, "%d %d %d\n", starter_mirrored, starter_flipped, starter_personal); - strcat(char_stream, line); + sprintf(line, "%d %d %d\n", starter_mirrored, starter_flipped, starter_personal); + strcat(char_stream, line); - sprintf(line, "c%d %d %d\n", canvas_color_r, canvas_color_g, canvas_color_b); - strcat(char_stream, line); + sprintf(line, "c%d %d %d\n", canvas_color_r, canvas_color_g, canvas_color_b); + strcat(char_stream, line); - sprintf(line, "T%s\n", template_id); - strcat(char_stream, line); + sprintf(line, "T%s\n", template_id); + strcat(char_stream, line); - sprintf(line, "%d\n", template_personal); - strcat(char_stream, line); + sprintf(line, "%d\n", template_personal); + strcat(char_stream, line); - sprintf(line, "M%d\n", starter_modified); - strcat(char_stream, line); + sprintf(line, "M%d\n", starter_modified); + strcat(char_stream, line); - 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); + } } /* Actually save the PNG data to the file stream: */ @@ -13821,23 +13658,12 @@ static int do_png_save(FILE * fi, const char *const fname, SDL_Surface * surf, i unsigned char **png_rows; 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); @@ -13845,89 +13671,99 @@ 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 { - if (setjmp(png_jmpbuf(png_ptr))) - { - fclose(fi); - png_destroy_write_struct(&png_ptr, (png_infopp) NULL); + info_ptr = png_create_info_struct(png_ptr); + if (info_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); - - return 0; - } + fprintf(stderr, "\nError: Couldn't save the image!\n%s\n\n", fname); + draw_tux_text(TUX_OOPS, strerror(errno), 0); + } else - { - png_init_io(png_ptr, fi); + { + if (setjmp(png_jmpbuf(png_ptr))) + { + fclose(fi); + png_destroy_write_struct(&png_ptr, (png_infopp) NULL); - 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); + fprintf(stderr, "\nError: Couldn't save the image!\n%s\n\n", fname); + draw_tux_text(TUX_OOPS, strerror(errno), 0); - png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, - PNG_sRGB_INTENT_PERCEPTUAL); + return 0; + } + else + { + png_init_io(png_ptr, fi); - /* Set headers */ + 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); - count = 0; + png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL); - /* - 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++; - } - */ + /* Set headers */ - 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++; + 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_set_text(png_ptr, info_ptr, text_ptr, count); - png_write_info(png_ptr, info_ptr); + png_write_info(png_ptr, info_ptr); - if (embed) - do_png_embed_data(png_ptr); - - /* Save the picture: */ + if (embed) + do_png_embed_data(png_ptr); - png_rows = malloc(sizeof(char *) * surf->h); + /* Save the picture: */ - for (y = 0; y < surf->h; y++) - { - png_rows[y] = malloc(sizeof(char) * 3 * surf->w); + png_rows = malloc(sizeof(char *) * surf->h); - for (x = 0; x < surf->w; x++) - { - SDL_GetRGB(getpixel(surf, x, y), surf->format, &r, &g, &b); + for (y = 0; y < surf->h; y++) + { + png_rows[y] = malloc(sizeof(char) * 3 * surf->w); - png_rows[y][x * 3 + 0] = r; - png_rows[y][x * 3 + 1] = g; - png_rows[y][x * 3 + 2] = b; - } - } + for (x = 0; x < surf->w; x++) + { + SDL_GetRGB(getpixel(surf, x, y), surf->format, &r, &g, &b); - png_write_image(png_ptr, png_rows); + png_rows[y][x * 3 + 0] = r; + png_rows[y][x * 3 + 1] = g; + png_rows[y][x * 3 + 2] = b; + } + } - for (y = 0; y < surf->h; y++) - free(png_rows[y]); + png_write_image(png_ptr, png_rows); - free(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); + png_write_end(png_ptr, NULL); + png_destroy_write_struct(&png_ptr, &info_ptr); + fclose(fi); - return 1; - } + return 1; + } + } } - } return 0; } @@ -13954,43 +13790,40 @@ static int do_quit(int tool) int done, tmp_tool; done = do_prompt_snd(PROMPT_QUIT_TXT, - PROMPT_QUIT_YES, PROMPT_QUIT_NO, SND_AREYOUSURE, - (TOOL_QUIT % 2) * 48 + 24, - (TOOL_QUIT / 2) * 48 + 40 + 24); + PROMPT_QUIT_YES, PROMPT_QUIT_NO, SND_AREYOUSURE, + (TOOL_QUIT % 2) * 48 + 24, (TOOL_QUIT / 2) * 48 + 40 + 24); 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 (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 (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! */ - 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); @@ -14027,8 +13860,7 @@ static int do_open(void) int *d_places; FILE *fi; char fname[1024]; - int num_files, i, done, slideshow, update_list, want_erase, cur, which, - num_files_in_dirs, j, any_saved_files; + int num_files, i, done, slideshow, update_list, want_erase, cur, which, num_files_in_dirs, j, any_saved_files; SDL_Rect dest; SDL_Event event; SDLKey key; @@ -14044,1093 +13876,1057 @@ 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++) { - if (places_to_look == PLACE_SAVED_DIR) - { - /* Saved-images: */ + do_setcursor(cursor_watch); - 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; - } + /* 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: */ + + qsort(fs, num_files_in_dirs, sizeof(struct dirent2), (int (*)(const void *, const void *))compare_dirent2s); /* Read directory of images and build thumbnails: */ - d = opendir(dirname[places_to_look]); - - if (d != NULL) - { - /* Gather list of files (for sorting): */ - - do + for (j = 0; j < num_files_in_dirs; j++) { - f = readdir(d); + f = &(fs[j].f); + place = fs[j].place; + + show_progress_bar(screen); 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; + debug(f->d_name); - /* FIXME: Valgrind says this is leaked -bjk 2007.07.19 */ - fs = (struct dirent2 *) realloc(fs, - sizeof(struct dirent2) * - things_alloced); - } - } - } - while (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) + { + strcpy(fname, f->d_name); + if (strcasestr(fname, FNAME_EXTENSION) != NULL) + { + d_exts[num_files] = strdup(strcasestr(fname, FNAME_EXTENSION)); + strcpy((char *)strcasestr(fname, FNAME_EXTENSION), ""); + } - closedir(d); - } - } + if (strcasestr(fname, ".bmp") != NULL) + { + d_exts[num_files] = strdup(strcasestr(fname, ".bmp")); + strcpy((char *)strcasestr(fname, ".bmp"), ""); + } + + d_names[num_files] = strdup(fname); + d_places[num_files] = place; - /* (Re)allocate space for the information about these files: */ + /* Is it the 'current' file we just loaded? + We'll make it the current selection! */ - 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); + 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; + } - /* Sort: */ + /* Try to load thumbnail first: */ - qsort(fs, num_files_in_dirs, sizeof(struct dirent2), - (int (*)(const void *, const void *)) compare_dirent2s); + 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/"): */ + + 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("saved", "Can't create user data directory")) + { + /* (Make sure we have a .../saved/.thumbs/ directory:) */ + make_directory("saved/.thumbs", "Can't create user data thumbnail directory"); + } - /* 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 - ) - { - strcpy(fname, f->d_name); - if (strcasestr(fname, FNAME_EXTENSION) != NULL) - { - d_exts[num_files] = strdup(strcasestr(fname, FNAME_EXTENSION)); - strcpy((char *) strcasestr(fname, FNAME_EXTENSION), ""); - } - - if (strcasestr(fname, ".bmp") != NULL) - { - d_exts[num_files] = strdup(strcasestr(fname, ".bmp")); - strcpy((char *) strcasestr(fname, ".bmp"), ""); - } - - d_names[num_files] = strdup(fname); - d_places[num_files] = place; + if (img == NULL) + { + snprintf(fname, sizeof(fname), "%s/%s", dirname[d_places[num_files]], f->d_name); + debug(fname); + img = myIMG_Load(fname); + } - /* Is it the 'current' file we just loaded? - We'll make it the current selection! */ + show_progress_bar(screen); - if (strcmp(d_names[num_files], file_id) == 0) - { - which = num_files; - cur = (which / 4) * 4; + 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()); - /* Center the cursor (useful for when the last item is - selected first!) */ + free(d_names[num_files]); + free(d_exts[num_files]); + } + else + { + /* Turn it into a thumbnail: */ - if (cur - 8 >= 0) - cur = cur - 8; - else if (cur - 4 >= 0) - cur = cur - 4; - } + 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); - /* Try to load thumbnail first: */ + /* Let's save this thumbnail, so we don't have to + create it again next time 'Open' is called: */ - 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 (d_places[num_files] == PLACE_SAVED_DIR) + { + debug("Saving thumbnail for this one!"); - if (img == NULL) - { - /* No thumbnail in the new location ("saved/.thumbs"), - try the old locatin ("saved/"): */ + snprintf(fname, sizeof(fname), "%s/.thumbs/%s-t.png", + dirname[d_places[num_files]], d_names[num_files]); - snprintf(fname, sizeof(fname), "%s/%s-t.png", - dirname[d_places[num_files]], d_names[num_files]); - debug(fname); + 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); + } - 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("saved", "Can't create user data directory")) - { - /* (Make sure we have a .../saved/.thumbs/ directory:) */ - make_directory("saved/.thumbs", "Can't create user data thumbnail directory"); - } + show_progress_bar(screen); + } - if (img == NULL) - { - 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]); - } + 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); + /* It was a thumbnail file ("...-t.png") */ } - - 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!"); - - 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) - { - do_prompt_snd(PROMPT_OPEN_NOFILES_TXT, PROMPT_OPEN_NOFILES_YES, "", - SND_YOUCANNOT, - (TOOL_OPEN % 2) * 48 + 24, (TOOL_OPEN / 2) * 48 + 40 + 24); - } - else - { - /* Let user choose an image: */ - - /* Instructions for 'Open' file dialog */ - char *freeme = - textdir(gettext_noop("Choose the picture you want, " - "then click “Open”.")); - draw_tux_text(TUX_BORED, freeme, 1); - free(freeme); - - /* 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; - - 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) + if (num_files == 0) { - /* Erase screen: */ - - dest.x = 96; - dest.y = 0; - dest.w = WINDOW_WIDTH - 96 - 96; - dest.h = 48 * 7 + 40 + HEIGHTOFFSET; - - 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) + 96; - dest.y = THUMB_H * ((i - cur) / 4) + 24; - - 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) + 96 + 10 + - (THUMB_W - 20 - thumbs[i]->w) / 2; - dest.y = THUMB_H * ((i - cur) / 4) + 24 + 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 = (48 * 7 + 40 + HEIGHTOFFSET) - 48; - - 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 = 96; - dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; - SDL_BlitSurface(img_open, NULL, screen, &dest); - - dest.x = 96 + (48 - img_openlabels_open->w) / 2; - dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - img_openlabels_open->h; - SDL_BlitSurface(img_openlabels_open, NULL, screen, &dest); - - - /* "Slideshow" button: */ - - dest.x = 96 + 48; - dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; - if (any_saved_files) - SDL_BlitSurface(img_btn_up, NULL, screen, &dest); - else - SDL_BlitSurface(img_btn_off, NULL, screen, &dest); - - dest.x = 96 + 48; - dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; - SDL_BlitSurface(img_slideshow, NULL, screen, &dest); - - dest.x = 96 + 48 + (48 - img_openlabels_slideshow->w) / 2; - dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - img_openlabels_slideshow->h; - SDL_BlitSurface(img_openlabels_slideshow, NULL, screen, &dest); - - - /* "Back" button: */ - - dest.x = WINDOW_WIDTH - 96 - 48; - dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; - SDL_BlitSurface(img_back, NULL, screen, &dest); - - dest.x = WINDOW_WIDTH - 96 - 48 + (48 - img_openlabels_back->w) / 2; - dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - img_openlabels_back->h; - SDL_BlitSurface(img_openlabels_back, NULL, screen, &dest); - - - /* "Erase" button: */ - - dest.x = WINDOW_WIDTH - 96 - 48 - 48; - dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; - - 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 - 96 - 48 - 48 + (48 - img_openlabels_erase->w) / 2; - dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - img_openlabels_erase->h; - SDL_BlitSurface(img_openlabels_erase, NULL, screen, &dest); - - - SDL_Flip(screen); - - update_list = 0; + do_prompt_snd(PROMPT_OPEN_NOFILES_TXT, PROMPT_OPEN_NOFILES_YES, "", + SND_YOUCANNOT, (TOOL_OPEN % 2) * 48 + 24, (TOOL_OPEN / 2) * 48 + 40 + 24); } - - while (SDL_PollEvent(&event)) - { - if (event.type == SDL_QUIT) + else { - done = 1; + /* Let user choose an image: */ - /* 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; + /* Instructions for 'Open' file dialog */ + char *freeme = textdir(gettext_noop("Choose the picture you want, " "then click “Open”.")); - handle_keymouse(key, SDL_KEYUP, 24, NULL, NULL); - } - else if (event.type == SDL_KEYDOWN) - { - key = event.key.keysym.sym; + draw_tux_text(TUX_BORED, freeme, 1); + free(freeme); - handle_keymouse(key, SDL_KEYDOWN, 24, NULL, NULL); + /* 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' */ - /* 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)) - { - if (event.button.x >= 96 && event.button.x < WINDOW_WIDTH - 96 && - event.button.y >= 24 && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET - 48)) - { - /* Picked an icon! */ - - int old_which = which; - - which = ((event.button.x - 96) / (THUMB_W) + - (((event.button.y - 24) / 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 < 24) - { - /* 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 >= (48 * 7 + 40 + HEIGHTOFFSET - 48) && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET - 24)) - { - /* 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; - } - } - else if (event.button.x >= 96 && event.button.x < 96 + 48 && - event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET)) - { - /* Open */ - - done = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); - } - else if (event.button.x >= 96 + 48 && event.button.x < 96 + 48 + 48 && - event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET) && - 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 - 96 - 48) && - event.button.x < (WINDOW_WIDTH - 96) && - event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET)) - { - /* Back */ - - which = -1; - done = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - } - else if (event.button.x >= (WINDOW_WIDTH - 96 - 48 - 48) && - event.button.x < (WINDOW_WIDTH - 48 - 96) && - event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET) && - d_places[which] != PLACE_STARTERS_DIR && - d_places[which] != PLACE_PERSONAL_STARTERS_DIR) - { - /* Erase */ - - want_erase = 1; - } -#ifdef __ANDROID__ - start_motion_convert (event); -#endif - } - else if (event.type == SDL_MOUSEBUTTONUP) - { -#ifdef __ANDROID__ - stop_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 < 24 && - 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 >= (48 * 7 + 40 + HEIGHTOFFSET - 48) && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET - 24) && - 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 >= 96 && event.button.x < 96 + 48 + 48) || - (event.button.x >= (WINDOW_WIDTH - 96 - 48) && - event.button.x < (WINDOW_WIDTH - 96)) || - (event.button.x >= (WINDOW_WIDTH - 96 - 48 - 48) && - event.button.x < (WINDOW_WIDTH - 48 - 96) && - d_places[which] != PLACE_STARTERS_DIR && - d_places[which] != PLACE_PERSONAL_STARTERS_DIR)) && - event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET)) - { - /* One of the command buttons: */ - - do_setcursor(cursor_hand); - } - else if (event.button.x >= 96 && event.button.x < WINDOW_WIDTH - 96 && - event.button.y > 24 && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET) - 48 && - ((((event.button.x - 96) / (THUMB_W) + - (((event.button.y - 24) / 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_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); - - if (want_erase) - { + update_list = 1; 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 - 96 - 48 - 48 + 24, - 48 * 7 + 40 + HEIGHTOFFSET - 48 + 24)) - { - snprintf(fname, sizeof(fname), "saved/%s%s", - d_names[which], d_exts[which]); + done = 0; + slideshow = 0; - rfname = get_fname(fname, DIR_SAVE); + last_click_which = -1; + last_click_time = 0; + last_click_button = -1; - if (trash(rfname) == 0) + + do_setcursor(cursor_arrow); + + + do { - update_list = 1; + /* Update screen: */ + + if (update_list) + { + /* Erase screen: */ + + dest.x = 96; + dest.y = 0; + dest.w = WINDOW_WIDTH - 96 - 96; + dest.h = 48 * 7 + 40 + HEIGHTOFFSET; + + SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 255, 255, 255)); - /* Delete the thumbnail, too: */ + /* Draw icons: */ - snprintf(fname, sizeof(fname), - "saved/.thumbs/%s-t.png", d_names[which]); + for (i = cur; i < cur + 16 && i < num_files; i++) + { + /* Draw cursor: */ - free(rfname); - rfname = get_fname(fname, DIR_SAVE); + dest.x = THUMB_W * ((i - cur) % 4) + 96; + dest.y = THUMB_H * ((i - cur) / 4) + 24; - unlink(rfname); - - - /* Try deleting old-style thumbnail, too: */ - - 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: */ - - snprintf(fname, sizeof(fname), "saved/%s.dat", d_names[which]); - - free(rfname); - rfname = get_fname(fname, DIR_SAVE); - - trash(rfname); + if (i == which) + { + SDL_BlitSurface(img_cursor_down, NULL, screen, &dest); + debug(d_names[i]); + } + else + SDL_BlitSurface(img_cursor_up, NULL, screen, &dest); - /* Move all other files up a notch: */ + dest.x = THUMB_W * ((i - cur) % 4) + 96 + 10 + (THUMB_W - 20 - thumbs[i]->w) / 2; + dest.y = THUMB_H * ((i - cur) / 4) + 24 + 10 + (THUMB_H - 20 - thumbs[i]->h) / 2; - 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--; + if (thumbs[i] != NULL) + SDL_BlitSurface(thumbs[i], NULL, screen, &dest); + } - /* Make sure the cursor doesn't go off the end! */ + /* Draw arrows: */ - if (which >= num_files) - which = num_files - 1; + 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 = (48 * 7 + 40 + HEIGHTOFFSET) - 48; + + if (cur < num_files - 16) + SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + else + SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); - /* Scroll up if the cursor goes off top of screen! */ + /* "Open" button: */ - if (which < cur && cur >= 4) - { - cur = cur - 4; - update_list = 1; - } + dest.x = 96; + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; + SDL_BlitSurface(img_open, NULL, screen, &dest); + + dest.x = 96 + (48 - img_openlabels_open->w) / 2; + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - img_openlabels_open->h; + SDL_BlitSurface(img_openlabels_open, NULL, screen, &dest); - /* No files to open now? */ + /* "Slideshow" button: */ - if (which < 0) - { - do_prompt_snd(PROMPT_OPEN_NOFILES_TXT, - PROMPT_OPEN_NOFILES_YES, "", SND_YOUCANNOT, - screen->w / 2, screen->h / 2); - done = 1; - } + dest.x = 96 + 48; + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; + if (any_saved_files) + SDL_BlitSurface(img_btn_up, NULL, screen, &dest); + else + SDL_BlitSurface(img_btn_off, NULL, screen, &dest); + + dest.x = 96 + 48; + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; + SDL_BlitSurface(img_slideshow, NULL, screen, &dest); + + dest.x = 96 + 48 + (48 - img_openlabels_slideshow->w) / 2; + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - img_openlabels_slideshow->h; + SDL_BlitSurface(img_openlabels_slideshow, NULL, screen, &dest); + + + /* "Back" button: */ + + dest.x = WINDOW_WIDTH - 96 - 48; + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; + SDL_BlitSurface(img_back, NULL, screen, &dest); + + dest.x = WINDOW_WIDTH - 96 - 48 + (48 - img_openlabels_back->w) / 2; + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - img_openlabels_back->h; + SDL_BlitSurface(img_openlabels_back, NULL, screen, &dest); + + + /* "Erase" button: */ + + dest.x = WINDOW_WIDTH - 96 - 48 - 48; + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; + + 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 - 96 - 48 - 48 + (48 - img_openlabels_erase->w) / 2; + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 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)) + { + if (event.button.x >= 96 && event.button.x < WINDOW_WIDTH - 96 && + event.button.y >= 24 && event.button.y < (48 * 7 + 40 + HEIGHTOFFSET - 48)) + { + /* Picked an icon! */ + + int old_which = which; + + which = ((event.button.x - 96) / (THUMB_W) + (((event.button.y - 24) / 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 < 24) + { + /* 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 >= (48 * 7 + 40 + HEIGHTOFFSET - 48) && + event.button.y < (48 * 7 + 40 + HEIGHTOFFSET - 24)) + { + /* 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; + } + } + else if (event.button.x >= 96 && event.button.x < 96 + 48 && + event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && + event.button.y < (48 * 7 + 40 + HEIGHTOFFSET)) + { + /* Open */ + + done = 1; + playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); + } + else if (event.button.x >= 96 + 48 && event.button.x < 96 + 48 + 48 && + event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && + event.button.y < (48 * 7 + 40 + HEIGHTOFFSET) && 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 - 96 - 48) && + event.button.x < (WINDOW_WIDTH - 96) && + event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && + event.button.y < (48 * 7 + 40 + HEIGHTOFFSET)) + { + /* Back */ + + which = -1; + done = 1; + playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); + } + else if (event.button.x >= (WINDOW_WIDTH - 96 - 48 - 48) && + event.button.x < (WINDOW_WIDTH - 48 - 96) && + event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && + event.button.y < (48 * 7 + 40 + HEIGHTOFFSET) && + d_places[which] != PLACE_STARTERS_DIR && d_places[which] != PLACE_PERSONAL_STARTERS_DIR) + { + /* Erase */ + + want_erase = 1; + } +#ifdef __ANDROID__ + start_motion_convert(event); +#endif + } + else if (event.type == SDL_MOUSEBUTTONUP) + { +#ifdef __ANDROID__ + stop_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 < 24 && + 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 >= (48 * 7 + 40 + HEIGHTOFFSET - 48) && + event.button.y < (48 * 7 + 40 + HEIGHTOFFSET - 24) && + 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 >= 96 && event.button.x < 96 + 48 + 48) || + (event.button.x >= (WINDOW_WIDTH - 96 - 48) && + event.button.x < (WINDOW_WIDTH - 96)) || + (event.button.x >= (WINDOW_WIDTH - 96 - 48 - 48) && + event.button.x < (WINDOW_WIDTH - 48 - 96) && + d_places[which] != PLACE_STARTERS_DIR && + d_places[which] != PLACE_PERSONAL_STARTERS_DIR)) && + event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && + event.button.y < (48 * 7 + 40 + HEIGHTOFFSET)) + { + /* One of the command buttons: */ + + do_setcursor(cursor_hand); + } + else if (event.button.x >= 96 && event.button.x < WINDOW_WIDTH - 96 && + event.button.y > 24 && + event.button.y < (48 * 7 + 40 + HEIGHTOFFSET) - 48 && + ((((event.button.x - 96) / (THUMB_W) + + (((event.button.y - 24) / 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_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); + + 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 - 96 - 48 - 48 + 24, 48 * 7 + 40 + HEIGHTOFFSET - 48 + 24)) + { + 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: */ + + 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: */ + + 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: */ + + 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; + } + } } - else - { - perror(rfname); + while (!done); - do_prompt_snd("CAN'T", "OK", "", SND_YOUCANNOT, 0, 0); - update_list = 1; + + 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: */ + + 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; + + strcpy(file_id, d_names[which]); + 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; + } + } } - free(rfname); - } - else - { - update_list = 1; - } + + update_canvas(0, 0, WINDOW_WIDTH - 96 - 96, 48 * 7 + 40 + HEIGHTOFFSET); } - } - while (!done); - if (!slideshow) - { - /* Load the chosen picture: */ + /* Clean up: */ - if (which != -1) + free_surface_array(thumbs, num_files); + + free(thumbs); + + for (i = 0; i < num_files; i++) { - /* 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: */ - - 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; - - strcpy(file_id, d_names[which]); - 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; - } + 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); - update_canvas(0, 0, WINDOW_WIDTH - 96 - 96, 48 * 7 + 40 + HEIGHTOFFSET); + if (slideshow) + { + slideshow = do_slideshow(); + } } - - - /* 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); + return (opened_something); } @@ -15152,8 +14948,7 @@ 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; @@ -15173,7 +14968,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; @@ -15191,228 +14986,214 @@ static int do_slideshow(void) d = opendir(dirname); if (d != NULL) - { - /* Gather list of files (for sorting): */ - - do { - f = readdir(d); + /* Gather list of files (for sorting): */ - if (f != NULL) - { - memcpy(&(fs[num_files_in_dir].f), f, sizeof(struct dirent)); - fs[num_files_in_dir].place = PLACE_SAVED_DIR; + do + { + f = readdir(d); - num_files_in_dir++; + if (f != NULL) + { + memcpy(&(fs[num_files_in_dir].f), f, sizeof(struct dirent)); + fs[num_files_in_dir].place = PLACE_SAVED_DIR; - if (num_files_in_dir >= things_alloced) - { - things_alloced = things_alloced + 32; - fs = (struct dirent2 *) realloc(fs, - sizeof(struct dirent2) * - things_alloced); - } - } + 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); + } + } + } + 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: */ - 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); /* 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) { - debug(f->d_name); + f = &(fs[j].f); - 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 - ) - { - strcpy(fname, f->d_name); - if (strcasestr(fname, FNAME_EXTENSION) != NULL) - { - d_exts[num_files] = strdup(strcasestr(fname, FNAME_EXTENSION)); - strcpy((char *) strcasestr(fname, FNAME_EXTENSION), ""); - } + show_progress_bar(screen); - if (strcasestr(fname, ".bmp") != NULL) - { - d_exts[num_files] = strdup(strcasestr(fname, ".bmp")); - strcpy((char *) strcasestr(fname, ".bmp"), ""); - } + if (f != NULL) + { + debug(f->d_name); - 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: */ - - 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/"): */ - - 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("saved", "Can't create user data directory")) + if (strcasestr(f->d_name, "-t.") == NULL && strcasestr(f->d_name, "-back.") == NULL) { - /* (Make sure we have a .../saved/.thumbs/ directory:) */ - make_directory("saved/.thumbs", "Can't create user data thumbnail directory"); + if (strcasestr(f->d_name, FNAME_EXTENSION) != NULL + /* Support legacy BMP files for load: */ + || strcasestr(f->d_name, ".bmp") != NULL) + { + strcpy(fname, f->d_name); + if (strcasestr(fname, FNAME_EXTENSION) != NULL) + { + d_exts[num_files] = strdup(strcasestr(fname, FNAME_EXTENSION)); + strcpy((char *)strcasestr(fname, FNAME_EXTENSION), ""); + } + + if (strcasestr(fname, ".bmp") != NULL) + { + d_exts[num_files] = strdup(strcasestr(fname, ".bmp")); + strcpy((char *)strcasestr(fname, ".bmp"), ""); + } + + 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: */ + + 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/"): */ + + 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("saved", "Can't create user data directory")) + { + /* (Make sure we have a .../saved/.thumbs/ directory:) */ + make_directory("saved/.thumbs", "Can't create user data thumbnail directory"); + } + + 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!"); + + 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++; + } + } + } + } } - - 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!"); - - 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); @@ -15420,8 +15201,7 @@ static int do_slideshow(void) /* Let user choose images: */ /* Instructions for Slideshow file dialog (FIXME: Make a #define) */ - freeme = textdir(gettext_noop("Choose the pictures you want, " - "then click “Play”.")); + freeme = textdir(gettext_noop("Choose the pictures you want, " "then click “Play”.")); draw_tux_text(TUX_BORED, freeme, 1); free(freeme); @@ -15441,474 +15221,465 @@ static int do_slideshow(void) do - { - /* Update screen: */ - - if (update_list) { - /* Erase screen: */ + /* Update screen: */ - dest.x = 96; - dest.y = 0; - dest.w = WINDOW_WIDTH - 96 - 96; - dest.h = 48 * 7 + 40 + HEIGHTOFFSET; + if (update_list) + { + /* Erase screen: */ - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 255, 255, 255)); + dest.x = 96; + dest.y = 0; + dest.w = WINDOW_WIDTH - 96 - 96; + dest.h = 48 * 7 + 40 + HEIGHTOFFSET; + + SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 255, 255, 255)); - /* Draw icons: */ + /* Draw icons: */ - for (i = cur; i < cur + 16 && i < num_files; i++) - { - /* Draw cursor: */ + for (i = cur; i < cur + 16 && i < num_files; i++) + { + /* Draw cursor: */ - dest.x = THUMB_W * ((i - cur) % 4) + 96; - dest.y = THUMB_H * ((i - cur) / 4) + 24; + dest.x = THUMB_W * ((i - cur) % 4) + 96; + dest.y = THUMB_H * ((i - cur) / 4) + 24; - 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 (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) + 96 + 10 + - (THUMB_W - 20 - thumbs[i]->w) / 2; - dest.y = THUMB_H * ((i - cur) / 4) + 24 + 10 + - (THUMB_H - 20 - thumbs[i]->h) / 2; + if (thumbs[i] != NULL) + { + dest.x = THUMB_W * ((i - cur) % 4) + 96 + 10 + (THUMB_W - 20 - thumbs[i]->w) / 2; + dest.y = THUMB_H * ((i - cur) / 4) + 24 + 10 + (THUMB_H - 20 - thumbs[i]->h) / 2; - SDL_BlitSurface(thumbs[i], NULL, screen, &dest); - } + SDL_BlitSurface(thumbs[i], NULL, screen, &dest); + } - found = -1; + found = -1; - for (j = 0; j < num_selected && found == -1; j++) - { - if (selected[j] == i) - found = j; - } + 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) + 96 + 10 + - (THUMB_W - 20 - thumbs[i]->w) / 2) + thumbs[i]->w; - dest.y = (THUMB_H * ((i - cur) / 4) + 24 + 10 + - (THUMB_H - 20 - thumbs[i]->h) / 2) + thumbs[i]->h; + if (found != -1) + { + dest.x = (THUMB_W * ((i - cur) % 4) + 96 + 10 + (THUMB_W - 20 - thumbs[i]->w) / 2) + thumbs[i]->w; + dest.y = (THUMB_H * ((i - cur) / 4) + 24 + 10 + (THUMB_H - 20 - thumbs[i]->h) / 2) + thumbs[i]->h; - draw_selection_digits(dest.x, dest.y, found + 1); - } - } + draw_selection_digits(dest.x, dest.y, found + 1); + } + } - /* Draw arrows: */ + /* Draw arrows: */ - dest.x = (WINDOW_WIDTH - img_scroll_up->w) / 2; - dest.y = 0; + 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); + 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 = (48 * 7 + 40 + HEIGHTOFFSET) - 48; + dest.x = (WINDOW_WIDTH - img_scroll_up->w) / 2; + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; - 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) + SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); + else + SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); - /* "Play" button: */ + /* "Play" button: */ - dest.x = 96; - dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; - SDL_BlitSurface(img_play, NULL, screen, &dest); + dest.x = 96; + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; + SDL_BlitSurface(img_play, NULL, screen, &dest); - dest.x = 96 + (48 - img_openlabels_play->w) / 2; - dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - img_openlabels_play->h; - SDL_BlitSurface(img_openlabels_play, NULL, screen, &dest); + dest.x = 96 + (48 - img_openlabels_play->w) / 2; + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - img_openlabels_play->h; + SDL_BlitSurface(img_openlabels_play, NULL, screen, &dest); - /* "Back" button: */ + /* "Back" button: */ - dest.x = WINDOW_WIDTH - 96 - 48; - dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; - SDL_BlitSurface(img_back, NULL, screen, &dest); + dest.x = WINDOW_WIDTH - 96 - 48; + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; + SDL_BlitSurface(img_back, NULL, screen, &dest); - dest.x = WINDOW_WIDTH - 96 - 48 + (48 - img_openlabels_back->w) / 2; - dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - img_openlabels_back->h; - SDL_BlitSurface(img_openlabels_back, NULL, screen, &dest); + dest.x = WINDOW_WIDTH - 96 - 48 + (48 - img_openlabels_back->w) / 2; + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - img_openlabels_back->h; + SDL_BlitSurface(img_openlabels_back, NULL, screen, &dest); - /* Speed control: */ + /* Speed control: */ - speeds = 10; - x_per = 96.0 / speeds; - y_per = 48.0 / speeds; + speeds = 10; + x_per = 96.0 / speeds; + y_per = 48.0 / speeds; - for (i = 0; i < speeds; i++) - { - xx = ceil(x_per); - yy = ceil(y_per * i); + 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); + 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, 48 - yy, 0); + blnk = thumbnail(img_btn_off, xx, 48 - yy, 0); - /* FIXME: Check for NULL! */ + /* FIXME: Check for NULL! */ - dest.x = 96 + 48 + (i * x_per); - dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; - SDL_BlitSurface(blnk, NULL, screen, &dest); + dest.x = 96 + 48 + (i * x_per); + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; + SDL_BlitSurface(blnk, NULL, screen, &dest); - dest.x = 96 + 48 + (i * x_per); - dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - (y_per * i); - SDL_BlitSurface(btn, NULL, screen, &dest); + dest.x = 96 + 48 + (i * x_per); + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - (y_per * i); + SDL_BlitSurface(btn, NULL, screen, &dest); - SDL_FreeSurface(btn); - SDL_FreeSurface(blnk); - } + SDL_FreeSurface(btn); + SDL_FreeSurface(blnk); + } - SDL_Flip(screen); + SDL_Flip(screen); - update_list = 0; + 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 = (48 * 7 + 40 + HEIGHTOFFSET) - 48; + 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 = (48 * 7 + 40 + HEIGHTOFFSET) - 48 + 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)) + { + if (event.button.x >= 96 && event.button.x < WINDOW_WIDTH - 96 && + event.button.y >= 24 && event.button.y < (48 * 7 + 40 + HEIGHTOFFSET - 48)) + { + /* Picked an icon! */ + + which = ((event.button.x - 96) / (THUMB_W) + (((event.button.y - 24) / 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++) + { + if (selected[i] == which) + found = i; + } + + if (found == -1) + { + /* No! Select it! */ + + 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 < 24) + { + /* 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 >= (48 * 7 + 40 + HEIGHTOFFSET - 48) && + event.button.y < (48 * 7 + 40 + HEIGHTOFFSET - 24)) + { + /* 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; + } + } + else if (event.button.x >= 96 && event.button.x < 96 + 48 && + event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && + event.button.y < (48 * 7 + 40 + HEIGHTOFFSET)) + { + /* 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 (FIXME: Make a #define) */ + freeme = textdir(gettext_noop("Choose the pictures you want, " "then click “Play”.")); + draw_tux_text(TUX_BORED, freeme, 1); + free(freeme); + + SDL_Flip(screen); + + update_list = 1; + } + else if (event.button.x >= 96 + 48 && event.button.x < 96 + 48 + 96 && + event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && + event.button.y < (48 * 7 + 40 + HEIGHTOFFSET)) + { + /* Speed slider */ + + int old_speed, control_sound, click_x; + + old_speed = speed; + + click_x = event.button.x - 96 - 48; + speed = ((10 * click_x) / 96); + + 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 - 96 - 48) && + event.button.x < (WINDOW_WIDTH - 96) && + event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && + event.button.y < (48 * 7 + 40 + HEIGHTOFFSET)) + { + /* Back */ + + go_back = 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_MOUSEBUTTONUP) + { +#ifdef __ANDROID__ + stop_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 < 24 && + 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 >= (48 * 7 + 40 + HEIGHTOFFSET - 48) && + event.button.y < (48 * 7 + 40 + HEIGHTOFFSET - 24) && + 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 >= 96 && event.button.x < 96 + 48 + 96) || + (event.button.x >= (WINDOW_WIDTH - 96 - 48) && + event.button.x < (WINDOW_WIDTH - 96))) && + event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && + event.button.y < (48 * 7 + 40 + HEIGHTOFFSET)) + { + /* One of the command buttons: */ + + do_setcursor(cursor_hand); + } + else if (event.button.x >= 96 && event.button.x < WINDOW_WIDTH - 96 + && event.button.y > 24 + && event.button.y < (48 * 7 + 40 + HEIGHTOFFSET) - 48 + && + ((((event.button.x - 96) / (THUMB_W) + + (((event.button.y - 24) / 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_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); } - - /* 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 = (48 * 7 + 40 + HEIGHTOFFSET) - 48; - 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 = (48 * 7 + 40 + HEIGHTOFFSET) - 48 + 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)) - { - if (event.button.x >= 96 && event.button.x < WINDOW_WIDTH - 96 && - event.button.y >= 24 && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET - 48)) - { - /* Picked an icon! */ - - which = ((event.button.x - 96) / (THUMB_W) + - (((event.button.y - 24) / 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++) - { - if (selected[i] == which) - found = i; - } - - if (found == -1) - { - /* No! Select it! */ - - 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 < 24) - { - /* 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 >= (48 * 7 + 40 + HEIGHTOFFSET - 48) && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET - 24)) - { - /* 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; - } - } - else if (event.button.x >= 96 && event.button.x < 96 + 48 && - event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET)) - { - /* 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 (FIXME: Make a #define) */ - freeme = textdir(gettext_noop("Choose the pictures you want, " "then click “Play”.")); - draw_tux_text(TUX_BORED, freeme, 1); - free(freeme); - - SDL_Flip(screen); - - update_list = 1; - } - else if (event.button.x >= 96 + 48 && event.button.x < 96 + 48 + 96 && - event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET)) - { - /* Speed slider */ - - int old_speed, control_sound, click_x; - - old_speed = speed; - - click_x = event.button.x - 96 - 48; - speed = ((10 * click_x) / 96); - - 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 - 96 - 48) && - event.button.x < (WINDOW_WIDTH - 96) && - event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET)) - { - /* Back */ - - go_back = 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_MOUSEBUTTONUP) - { -#ifdef __ANDROID__ - stop_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 < 24 && - 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 >= (48 * 7 + 40 + HEIGHTOFFSET - 48) && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET - 24) && - 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 >= 96 && event.button.x < 96 + 48 + 96) || - (event.button.x >= (WINDOW_WIDTH - 96 - 48) && - event.button.x < (WINDOW_WIDTH - 96))) && - event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET)) - { - /* One of the command buttons: */ - - do_setcursor(cursor_hand); - } - else if (event.button.x >= 96 && event.button.x < WINDOW_WIDTH - 96 - && event.button.y > 24 - && event.button.y < (48 * 7 + 40 + HEIGHTOFFSET) - 48 - && - ((((event.button.x - 96) / (THUMB_W) + - (((event.button.y - 24) / 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_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); @@ -15919,10 +15690,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); @@ -15935,15 +15706,14 @@ 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) { int i, which, next, done; int val_x, val_y, motioner; int valhat_x, valhat_y, hatmotioner; - SDL_Surface * img; - char * tmp_starter_id, * tmp_template_id, * tmp_file_id; + SDL_Surface *img; + char *tmp_starter_id, *tmp_template_id, *tmp_file_id; int tmp_starter_mirrored, tmp_starter_flipped, tmp_starter_personal; char fname[1024]; SDL_Event event; @@ -15968,200 +15738,197 @@ static void play_slideshow(int * selected, int num_selected, char * dirname, done = 0; do - { - for (i = 0; i < num_selected && !done; i++) { - which = selected[i]; - show_progress_bar(screen); + for (i = 0; i < num_selected && !done; i++) + { + which = selected[i]; + show_progress_bar(screen); - /* Figure out filename: */ + /* Figure out filename: */ - snprintf(fname, sizeof(fname), "%s/%s%s", - dirname, d_names[which], d_exts[which]); + 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); - strcpy(file_id, d_names[which]); + strcpy(file_id, d_names[which]); /* 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 - 48; - dest.y = screen->h - 48; - SDL_BlitSurface(img_back, NULL, screen, &dest); - - dest.x = screen->w - 48 + (48 - 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 - 48; - SDL_BlitSurface(img_play, NULL, screen, &dest); - - dest.x = (48 - 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); + if (starter_flipped) + flip_starter(); + } + else + load_template(template_id); } - } - else if (event.type == SDL_MOUSEBUTTONDOWN) - { - /* Mouse click! */ - if (event.button.x >= screen->w - 48 && - event.button.y >= screen->h - 48) - { - /* Back button */ + /* "Back" button: */ - next = 1; - done = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_RIGHT, SNDDIST_NEAR); - } - else - { - /* Otherwise, skip to next image right away! */ + dest.x = screen->w - 48; + dest.y = screen->h - 48; + SDL_BlitSurface(img_back, NULL, screen, &dest); - next = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); - } - } - else if (event.type == SDL_MOUSEMOTION) - { - /* Deal with mouse pointer shape! */ + dest.x = screen->w - 48 + (48 - img_openlabels_back->w) / 2; + dest.y = screen->h - img_openlabels_back->h; + SDL_BlitSurface(img_openlabels_back, NULL, screen, &dest); - if ((event.button.x >= screen->w - 48 || - event.button.x < 48) && - event.button.y >= screen->h - 48) - { - /* Back or Next buttons */ + /* "Next" button: */ - do_setcursor(cursor_hand); - } - else - { - /* Otherwise, minimal cursor... */ + dest.x = 0; + dest.y = screen->h - 48; + SDL_BlitSurface(img_play, NULL, screen, &dest); - 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); + dest.x = (48 - img_openlabels_next->w) / 2; + dest.y = screen->h - img_openlabels_next->h; + SDL_BlitSurface(img_openlabels_next, NULL, screen, &dest); - /* Automatically skip to the next one after time expires: */ + SDL_Flip(screen); - if (speed != 0) - { - if (SDL_GetTicks() >= last_ticks + (10 - speed) * 500) - next = 1; - } - } - while (!next); + + /* 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 - 48 && event.button.y >= screen->h - 48) + { + /* 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 - 48 || event.button.x < 48) && event.button.y >= screen->h - 48) + { + /* 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); strcpy(starter_id, tmp_starter_id); @@ -16192,41 +15959,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; + } } @@ -16236,10 +16003,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 } @@ -16270,9 +16037,7 @@ 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; @@ -16283,6 +16048,7 @@ static void update_stamp_xor(void) int xx, yy, rx, ry; Uint8 dummy; SDL_Surface *src; + Uint32(*getpixel) (SDL_Surface *, int, int); unsigned char *alphabits; int new_w; @@ -16300,16 +16066,15 @@ static void update_stamp_xor(void) SDL_LockSurface(src); for (yy = 0; yy < src->h; yy++) - { - 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)); + 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)); + } } - } SDL_UnlockSurface(src); new_w = src->w + 4; @@ -16318,46 +16083,46 @@ static void update_stamp_xor(void) outline = calloc(new_w, new_h); for (yy = 1; yy < new_h - 1; yy++) - { - for (xx = 1; xx < new_w - 1; xx++) { - unsigned char above = 0; - unsigned char below = 0xff; - unsigned char tmp; + for (xx = 1; xx < new_w - 1; xx++) + { + 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; @@ -16374,25 +16139,24 @@ 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++) { - 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); + 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); + } } - } SDL_UnlockSurface(screen); } #endif -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; @@ -16407,91 +16171,90 @@ static void rgbtohsv(Uint8 r8, Uint8 g8, Uint8 b8, float *h, float *s, 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); + } } -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; - } + 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)); + { + 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; + 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; + } } - 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; - } - } *r8 = (Uint8) (r * 255); @@ -16501,44 +16264,39 @@ static void hsvtorgb(float h, float s, float v, Uint8 * r8, Uint8 * g8, static void print_image(void) { - int cur_time; - + int cur_time; + cur_time = SDL_GetTicks() / 1000; - #ifdef DEBUG +#ifdef DEBUG printf("Current time = %d\n", cur_time); - #endif +#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) * 48 + 24, - (TOOL_PRINT / 2) * 48 + 40 + 24)) { - do_print(); - - last_print_time = cur_time; + 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) * 48 + 24, (TOOL_PRINT / 2) * 48 + 40 + 24)) + { + do_print(); + + 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); + } } void do_print(void) @@ -16561,28 +16319,25 @@ 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); + 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 */ @@ -16590,11 +16345,10 @@ void do_print(void) char f[512]; int show = want_alt_printcommand; - snprintf(f, sizeof(f), "%s/%s", savedir, "print.cfg"); /* FIXME */ + snprintf(f, sizeof(f), "%s/%s", savedir, "print.cfg"); /* FIXME */ { - const char *error = - SurfacePrint(save_canvas, use_print_config ? f : NULL, show); + const char *error = SurfacePrint(save_canvas, use_print_config ? f : NULL, show); if (error) fprintf(stderr, "%s\n", error); @@ -16605,48 +16359,49 @@ void do_print(void) SurfacePrint(save_canvas); #elif defined(__APPLE__) /* Mac OS X */ - int show = ( ( want_alt_printcommand || macosx.menuAction ) && !fullscreen); + int show = ((want_alt_printcommand || macosx.menuAction) && !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); - } - -#elif defined(__ANDROID__) + { + fprintf(stderr, "Cannot print: %s\n", error); + do_prompt_snd(error, PROMPT_PRINT_YES, "", SND_TUXOK, 0, 0); + } + +#elif defined(__ANDROID__) int x, y; 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, - screen->format->Rmask, - screen->format->Gmask, - screen->format->Bmask, 0); + SDL_Surface *save_canvas_and = SDL_CreateRGBSurface(0, + WINDOW_WIDTH - (96 * 2), + (48 * 7) + 40 + HEIGHTOFFSET, + screen->format->BitsPerPixel, + screen->format->Rmask, + screen->format->Gmask, + screen->format->Bmask, 0); - for (x = 0; xw; x++) - for (y = 0; yh; y++) + 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 +#endif #endif } @@ -16654,6 +16409,7 @@ void do_print(void) static void do_render_cur_text(int do_blit) { int w, h; + SDL_Color color = { color_hexes[cur_color][0], color_hexes[cur_color][1], color_hexes[cur_color][2], @@ -16670,19 +16426,17 @@ static void do_render_cur_text(int do_blit) /* Keep cursor on the screen! */ - if (cursor_y > ((48 * 7 + 40 + HEIGHTOFFSET) - - TuxPaint_Font_FontHeight(getfonthandle(cur_font)))) - { - cursor_y = ((48 * 7 + 40 + HEIGHTOFFSET) - - TuxPaint_Font_FontHeight(getfonthandle(cur_font))); - } + if (cursor_y > ((48 * 7 + 40 + HEIGHTOFFSET) - TuxPaint_Font_FontHeight(getfonthandle(cur_font)))) + { + cursor_y = ((48 * 7 + 40 + HEIGHTOFFSET) - TuxPaint_Font_FontHeight(getfonthandle(cur_font))); + } /* Render the text: */ if (texttool_len > 0) - { - #if defined(_FRIBIDI_H) || defined(FRIBIDI_H) + { +#if defined(_FRIBIDI_H) || defined(FRIBIDI_H) //FriBidiCharType baseDir = FRIBIDI_TYPE_LTR; //FriBidiCharType baseDir = FRIBIDI_TYPE_WL; /* Per: Shai Ayal , 2009-01-14 */ FriBidiParType baseDir = FRIBIDI_TYPE_WL; //EP to avoid warning on types in now commented line above @@ -16703,168 +16457,155 @@ static void do_render_cur_text(int do_blit) reposition the text */ for (i = 0; i < texttool_len; i++) - str[i] = (long) unicodeOut[i]; + str[i] = (long)unicodeOut[i]; str[texttool_len] = L'\0'; free(unicodeIn); free(unicodeOut); - #else +#else str = uppercase_w(texttool_str); - #endif +#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; + w = tmp_surf->w; + h = tmp_surf->h; - cursor_textwidth = w; - } - else /* Erase the stalle letter . */ - { - if (cur_label != LABEL_SELECT) + cursor_textwidth = w; + } + else /* Erase the stalle letter . */ { - update_canvas_ex_r(old_dest.x - 96, 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; + if (cur_label != LABEL_SELECT) + { + update_canvas_ex_r(old_dest.x - 96, 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 */ + /* 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; - - } + 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 - 96, 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 + 96; - dest.y = cursor_y - 2; - dest.w = w + 4; - dest.h = h + 4; - - if (dest.x + dest.w > WINDOW_WIDTH - 96) - dest.w = WINDOW_WIDTH - 96 - dest.x; - if (dest.y + dest.h > (48 * 7 + 40 + HEIGHTOFFSET)) - dest.h = (48 * 7 + 40 + HEIGHTOFFSET) - 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 + 96; - dest.y = cursor_y; - dest.w = w; - dest.h = h; - - if (dest.x + dest.w > WINDOW_WIDTH - 96) - dest.w = WINDOW_WIDTH - 96 - dest.x; - if (dest.y + dest.h > (48 * 7 + 40 + HEIGHTOFFSET)) - dest.h = (48 * 7 + 40 + HEIGHTOFFSET) - 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!... */ + update_canvas_ex_r(old_dest.x - 96, old_dest.y, old_dest.x + old_dest.w, old_dest.y + old_dest.h, 0); - SDL_FillRect(screen, &dest, SDL_MapRGB(canvas->format, 64, 64, 64)); - } - else - { - /* White background, normally... */ + /* 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); */ - SDL_FillRect(screen, &dest, SDL_MapRGB(canvas->format, 255, 255, 255)); + + /* Draw outline around text: */ + + dest.x = cursor_x - 2 + 96; + dest.y = cursor_y - 2; + dest.w = w + 4; + dest.h = h + 4; + + if (dest.x + dest.w > WINDOW_WIDTH - 96) + dest.w = WINDOW_WIDTH - 96 - dest.x; + if (dest.y + dest.h > (48 * 7 + 40 + HEIGHTOFFSET)) + dest.h = (48 * 7 + 40 + HEIGHTOFFSET) - 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 + 96; + dest.y = cursor_y; + dest.w = w; + dest.h = h; + + if (dest.x + dest.w > WINDOW_WIDTH - 96) + dest.w = WINDOW_WIDTH - 96 - dest.x; + if (dest.y + dest.h > (48 * 7 + 40 + HEIGHTOFFSET)) + dest.h = (48 * 7 + 40 + HEIGHTOFFSET) - 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)); + } } - } /* 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 - 96 - 96) - src.w = WINDOW_WIDTH - 96 - 96 - dest.x; - if (dest.y + src.h > (48 * 7 + 40 + HEIGHTOFFSET)) - src.h = (48 * 7 + 40 + HEIGHTOFFSET) - 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); + 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 - 96 - 96) + src.w = WINDOW_WIDTH - 96 - 96 - dest.x; + if (dest.y + src.h > (48 * 7 + 40 + HEIGHTOFFSET)) + src.h = (48 * 7 + 40 + HEIGHTOFFSET) - 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 - { - 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); + { + dest.x = dest.x + 96; + SDL_BlitSurface(tmp_surf, &src, screen, &dest); + } } - else - { - dest.x = dest.x + 96; - SDL_BlitSurface(tmp_surf, &src, screen, &dest); - } - } /* FIXME: Only update what's changed! */ @@ -16876,8 +16617,8 @@ static void do_render_cur_text(int do_blit) if (tmp_surf != NULL) SDL_FreeSurface(tmp_surf); /* if (tmp_label != NULL) */ - /* SDL_FreeSurface(tmp_label); */ - // SDL_Delay(5000); + /* SDL_FreeSurface(tmp_label); */ + // SDL_Delay(5000); } @@ -16896,15 +16637,17 @@ static char *uppercase(const char *restrict const str) /* watch out: uppercase chars may need extra bytes! http://en.wikipedia.org/wiki/Turkish_dotted_and_dotless_I */ n = strlen(str) + 1; - dest = alloca(sizeof(wchar_t)*n); - ustr = malloc(n*2); /* use *2 in case 'i' becomes U+0131 */ + dest = alloca(sizeof(wchar_t) * n); + ustr = malloc(n * 2); /* use *2 in case 'i' becomes U+0131 */ - mbstowcs(dest, str, sizeof(wchar_t)*n); /* at most n wchar_t written */ + mbstowcs(dest, str, sizeof(wchar_t) * n); /* at most n wchar_t written */ i = 0; - do{ - dest[i] = towupper(dest[i]); - }while(dest[i++]); - wcstombs(ustr, dest, n); /* at most n bytes written */ + do + { + dest[i] = towupper(dest[i]); + } + while (dest[i++]); + wcstombs(ustr, dest, n); /* at most n bytes written */ #ifdef DEBUG printf(" ORIGINAL: %s\n" "UPPERCASE: %s\n\n", str, ustr); @@ -16912,19 +16655,23 @@ static char *uppercase(const char *restrict const str) return ustr; } -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); - memcpy(ustr,str,sizeof(wchar_t) * n); + + memcpy(ustr, str, sizeof(wchar_t) * n); if (only_uppercase) - { - unsigned i = 0; - do{ - ustr[i] = towupper(ustr[i]); - }while(ustr[i++]); - } + { + unsigned i = 0; + + do + { + ustr[i] = towupper(ustr[i]); + } + while (ustr[i++]); + } return ustr; } @@ -16945,55 +16692,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++) { - j = (strlen(str) - i - 1); + dstr[strlen(str)] = '\0'; - c1 = (unsigned char) str[i + 0]; - c2 = (unsigned char) str[i + 1]; - c3 = (unsigned char) str[i + 2]; - c4 = (unsigned char) str[i + 3]; + for (i = 0; i < strlen(str); i++) + { + j = (strlen(str) - i - 1); - 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; - } + 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; + } + } } - } else - { - strcpy((char *) dstr, str); - } + { + strcpy((char *)dstr, str); + } #ifdef DEBUG printf("L2R_DIR: %s\n", dstr); #endif - return ((char *) dstr); + return ((char *)dstr); } @@ -17003,16 +16750,16 @@ static Uint32 scrolltimer_callback(Uint32 interval, void *param) { /* printf("scrolltimer_callback(%d) -- ", interval); */ if (scrolling) - { - /* printf("(Still scrolling)\n"); */ - SDL_PushEvent((SDL_Event *) param); - return interval; - } + { + /* printf("(Still scrolling)\n"); */ + SDL_PushEvent((SDL_Event *) param); + return interval; + } else - { - /* printf("(all done)\n"); */ - return 0; - } + { + /* printf("(all done)\n"); */ + return 0; + } } @@ -17028,25 +16775,24 @@ 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; } @@ -17055,10 +16801,10 @@ static void control_drawtext_timer(Uint32 interval, const char *const text, Uint static Uint32 drawtext_callback(Uint32 interval, void *param) { - (void) interval; + (void)interval; SDL_PushEvent((SDL_Event *) param); - return 0; /* Remove timer */ + return 0; /* Remove timer */ } @@ -17068,11 +16814,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)); } @@ -17114,288 +16860,291 @@ 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) { - 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; - } + /* make the compiler happy */ + r1.x = r1.y = r1.w = r1.h = 0; + r2.x = r2.y = r2.w = r2.h = 0; - 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) - { - 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 (area1) + { + 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 (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) - { - 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); - } + 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; + } - else if (key == SDLK_1 || key == SDLK_KP_1) - SDL_WarpMouse(left, bottom); + /* 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_3 || key == SDLK_KP_3) - SDL_WarpMouse(right, 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_7 || key == SDLK_KP_7) - SDL_WarpMouse(left, up); + /* 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_9 || key == SDLK_KP_9) - SDL_WarpMouse(right, 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_2 || key == SDLK_KP_2) - SDL_WarpMouse(oldpos_x, bottom); + 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_8 || key == SDLK_KP_8) - SDL_WarpMouse(oldpos_x, up); + 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_6 || key == SDLK_KP_6) - SDL_WarpMouse(right, oldpos_y); + 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_4 || key == SDLK_KP_4) - SDL_WarpMouse(left, oldpos_y); + else + { + if (key == SDLK_LEFT) + 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. */ + 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. */ #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 - } + } + } } - } } /* 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)) - { - *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; - } + + 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); } - while (*whicht >= tool_scroll + 2 * real_r_tools.h / button_h) + else if (key == SDLK_F11 && hit_test(&r_colors, oldpos_x, oldpos_y)) { - tool_scroll += 2; - draw_toolbar(); - update_screen_rect(&r_tools); + *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)); } - while (*whicht < tool_scroll) + + else if (key == SDLK_F12 && hit_test(&r_colors, oldpos_x, oldpos_y)) { - tool_scroll -= 2; - draw_toolbar(); - update_screen_rect(&r_tools); + *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)); } - 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)); - } - - 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)); - } - 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); + } } /* Unblank screen in fullscreen mode, if needed: */ @@ -17403,23 +17152,25 @@ static void handle_keymouse_buttons(SDLKey key, int *whicht, int *whichc, SDL_Re 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_FOCUS_GAINED) - { - if (mouseaccessibility) { - magic_switchin(canvas); + { + // if (fullscreen) + SDL_Flip(screen); } - else if (mouseaccessibility && emulate_button_pressed) { - magic_switchout(canvas); - } + if (event->window.event == SDL_WINDOWEVENT_FOCUS_GAINED) + { + if (mouseaccessibility) + { + magic_switchin(canvas); + } + else if (mouseaccessibility && emulate_button_pressed) + { + magic_switchout(canvas); + } #ifdef _WIN32 - SetActivationState(event->active.gain); + SetActivationState(event->active.gain); #endif - } + } } @@ -17467,8 +17218,7 @@ static char *replace_tilde(const char* const path) 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; @@ -17532,57 +17282,57 @@ static void mirror_starter(void) img_starter = duplicate_surface(orig); if (img_starter != NULL) - { - for (x = 0; x < orig->w; x++) { - src.x = x; - src.y = 0; - src.w = 1; - src.h = orig->h; + 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, &dest); + SDL_BlitSurface(orig, &src, img_starter, &dest); + } + + SDL_FreeSurface(orig); } - - 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) { - for (x = 0; x < orig->w; x++) - { - src.x = x; - src.y = 0; - src.w = 1; - src.h = orig->h; + orig = img_starter_bkgd; + img_starter_bkgd = duplicate_surface(orig); - dest.x = orig->w - x - 1; - dest.y = 0; + 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; - SDL_BlitSurface(orig, &src, img_starter_bkgd, &dest); - } + dest.x = orig->w - x - 1; + dest.y = 0; - SDL_FreeSurface(orig); + SDL_BlitSurface(orig, &src, img_starter_bkgd, &dest); + } + + SDL_FreeSurface(orig); + } + else + { + img_starter_bkgd = orig; + } } - else - { - img_starter_bkgd = orig; - } - } } @@ -17599,57 +17349,57 @@ static void flip_starter(void) img_starter = duplicate_surface(orig); if (img_starter != NULL) - { - for (y = 0; y < orig->h; y++) { - src.x = 0; - src.y = y; - src.w = orig->w; - src.h = 1; + 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, &dest); + SDL_BlitSurface(orig, &src, img_starter, &dest); + } + + SDL_FreeSurface(orig); } - - 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) { - for (y = 0; y < orig->h; y++) - { - src.x = 0; - src.y = y; - src.w = orig->w; - src.h = 1; + orig = img_starter_bkgd; + img_starter_bkgd = duplicate_surface(orig); - dest.x = 0; - dest.y = orig->h - y - 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; - SDL_BlitSurface(orig, &src, img_starter_bkgd, &dest); - } + dest.x = 0; + dest.y = orig->h - y - 1; - SDL_FreeSurface(orig); + SDL_BlitSurface(orig, &src, img_starter_bkgd, &dest); + } + + SDL_FreeSurface(orig); + } + else + { + img_starter_bkgd = orig; + } } - else - { - img_starter_bkgd = orig; - } - } } @@ -17681,9 +17431,9 @@ static int in_circle_rad(int x, int y, int rad) static int paintsound(int size) { if (SND_PAINT1 + (size / 12) >= SND_PAINT4) - return(SND_PAINT4); + return (SND_PAINT4); else - return(SND_PAINT1 + (size / 12)); + return (SND_PAINT1 + (size / 12)); } @@ -17695,17 +17445,17 @@ static int paintsound(int size) Based on cairo-demo/sdl/main.c from Cairo (GPL'd, (c) 2004 Eric Windisch): */ -static SDL_Surface * load_svg(char * file) +static SDL_Surface *load_svg(char *file) { - svg_cairo_t * scr; + svg_cairo_t *scr; int bpp, btpp, stride; unsigned int width, height; unsigned int rwidth, rheight; float scale; - unsigned char * image; - cairo_surface_t * cairo_surface; - cairo_t * cr; - SDL_Surface * sdl_surface, * sdl_surface_tmp; + unsigned char *image; + cairo_surface_t *cairo_surface; + cairo_t *cr; + SDL_Surface *sdl_surface, *sdl_surface_tmp; Uint32 rmask, gmask, bmask, amask; svg_cairo_status_t res; @@ -17716,22 +17466,22 @@ static SDL_Surface * load_svg(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); @@ -17740,13 +17490,13 @@ static SDL_Surface * load_svg(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... */ @@ -17754,13 +17504,13 @@ static SDL_Surface * load_svg(char * file) btpp = 4; /* We want to render at full Tux Paint canvas size, so that the stamp - at its largest scale remains highest quality (no pixelization): - (but not messing up the aspect ratio) */ + at its largest scale remains highest quality (no pixelization): + (but not messing up the aspect ratio) */ 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); @@ -17773,26 +17523,25 @@ static SDL_Surface * load_svg(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 :( */ + cairo_scale(cr, scale, scale); /* no return value :( */ /* Render SVG to our surface: */ res = svg_cairo_render(scr, cr); @@ -17803,12 +17552,12 @@ static SDL_Surface * load_svg(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 @@ -17819,17 +17568,15 @@ static SDL_Surface * load_svg(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: */ @@ -17837,37 +17584,36 @@ static SDL_Surface * load_svg(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); + return (sdl_surface); } #else /* New libcairo2, rsvg and rsvg-cairo based code */ -static SDL_Surface * load_svg(char * file) +static SDL_Surface *load_svg(char *file) { - cairo_surface_t * cairo_surf; - cairo_t * cr; - RsvgHandle * rsvg_handle; - GError * gerr; - unsigned char * image; + cairo_surface_t *cairo_surf; + cairo_t *cr; + RsvgHandle *rsvg_handle; + GError *gerr; + unsigned char *image; int rwidth, rheight; int width, height, stride; float scale; int bpp = 32, btpp = 4; RsvgDimensionData dimensions; - SDL_Surface * sdl_surface, * sdl_surface_tmp; + SDL_Surface *sdl_surface, *sdl_surface_tmp; Uint32 rmask, gmask, bmask, amask; #ifdef DEBUG @@ -17880,12 +17626,12 @@ static SDL_Surface * load_svg(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; @@ -17897,15 +17643,15 @@ static SDL_Surface * load_svg(char * file) /* Pick best scale to render to (for the canvas in this instance of Tux Paint) */ - + scale = pick_best_scape(rwidth, rheight, r_canvas.w, r_canvas.h); #ifdef DEBUG 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); @@ -17917,45 +17663,43 @@ static SDL_Surface * load_svg(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: */ @@ -17969,51 +17713,48 @@ static SDL_Surface * load_svg(char * file) cairo_surface_finish(cairo_surf); - + /* Adjust the SDL surface to match the cairo surface created - (surface mask of ARGB) NOTE: Is this endian-agnostic? -bjk 2006.10.25 */ + (surface mask of ARGB) NOTE: Is this endian-agnostic? -bjk 2006.10.25 */ rmask = 0x00ff0000; gmask = 0x0000ff00; bmask = 0x000000ff; 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 @@ -18024,26 +17765,24 @@ static SDL_Surface * load_svg(char * file) free(image); cairo_destroy(cr); - return(sdl_surface); + return (sdl_surface); } #endif -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); @@ -18052,55 +17791,47 @@ static float pick_best_scape(unsigned int orig_w, unsigned int orig_h, #endif if (aspect >= 1) - { - /* Image is wider-than-tall (or square) */ - - 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)); -#endif - - if ((float) orig_h * scale > (float) max_h) { - scale = hscale; + /* Image is wider-than-tall (or square) */ -#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 - { - /* 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)); -#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)); + 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 + { + /* 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)); +#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 @@ -18108,62 +17839,67 @@ static float pick_best_scape(unsigned int orig_w, unsigned int orig_h, printf("Final scale: %.4f\n", scale); #endif - return(scale); + return (scale); } #endif /* FIXME: we can remove this after SDL folks fix their bug at http://bugzilla.libsdl.org/show_bug.cgi?id=1485 */ /* Try to load an image with IMG_Load(), if it fails, then try with RWops() */ -static SDL_Surface * myIMG_Load_RWops(char * file) +static SDL_Surface *myIMG_Load_RWops(char *file) { - SDL_Surface * surf; - FILE * fi; - SDL_RWops * data; + SDL_Surface *surf; + FILE *fi; + SDL_RWops *data; surf = IMG_Load(file); if (surf != NULL) - return(surf); + return (surf); /* From load_kpx() */ fi = fopen(file, "rb"); if (fi == NULL) return NULL; - data = SDL_RWFromFP(fi, 1); /* 1 = Close when we're done */ + data = SDL_RWFromFP(fi, 1); /* 1 = Close when we're done */ if (data == NULL) - return(NULL); + return (NULL); - surf = IMG_Load_RW(data, 1); /* 1 = Free when we're done */ + surf = IMG_Load_RW(data, 1); /* 1 = Free when we're done */ if (surf == NULL) - return(NULL); + return (NULL); - return(surf); + return (surf); } /* Load an image; call load_svg() (above, to call Cairo and SVG-Cairo funcs) if we notice it's an SVG file (if available!); call load_kpx() if we notice it's a KPX file (JPEG with wrapper); otherwise call SDL_Image lib's IMG_Load() (for PNGs, JPEGs, BMPs, etc.) */ -static SDL_Surface * myIMG_Load(char * file) +static SDL_Surface *myIMG_Load(char *file) { - if (strlen(file) > 4 && strcasecmp(file + strlen(file) - 4, ".kpx") == 0) { - return(load_kpx(file)); + if (strlen(file) > 4 && strcasecmp(file + strlen(file) - 4, ".kpx") == 0) + { + 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)); -} + } + else + { + return (myIMG_Load_RWops(file)); + } } -static SDL_Surface * load_kpx(char * file) +static SDL_Surface *load_kpx(char *file) { - SDL_RWops * data; - FILE * fi; - SDL_Surface * surf; + SDL_RWops *data; + FILE *fi; + SDL_Surface *surf; int i; fi = fopen(file, "rb"); @@ -18174,23 +17910,23 @@ static SDL_Surface * load_kpx(char * file) for (i = 0; i < 60; i++) fgetc(fi); - data = SDL_RWFromFP(fi, 1); /* 1 = Close when we're done */ + data = SDL_RWFromFP(fi, 1); /* 1 = Close when we're done */ if (data == NULL) - return(NULL); + return (NULL); - surf = IMG_Load_RW(data, 1); /* 1 = Free when we're done */ + surf = IMG_Load_RW(data, 1); /* 1 = Free when we're done */ if (surf == NULL) - return(NULL); + return (NULL); - return(surf); + return (surf); } static void load_magic_plugins(void) { int res, n, i, plc; - char * place; + char *place; int err; DIR *d; struct dirent *f; @@ -18202,403 +17938,375 @@ static void load_magic_plugins(void) num_magics = 0; for (plc = 0; plc < NUM_MAGIC_PLACES; plc++) - { - if (plc == MAGIC_PLACE_GLOBAL) - place = strdup(MAGIC_PREFIX); - 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); + place = strdup(MAGIC_PREFIX); else if (plc == MAGIC_PLACE_LOCAL) - magic_api_struct->data_directory = get_fname("plugins/data/", DIR_DATA); + place = get_fname("plugins/", DIR_DATA); #ifdef __APPLE__ else if (plc == MAGIC_PLACE_ALLUSERS) - magic_api_struct->data_directory = strdup("/Library/Application Support/TuxPaint/plugins/data"); + place = strdup("/Library/Application Support/TuxPaint/plugins/"); #endif else - magic_api_struct->data_directory = strdup("./"); + continue; /* Huh? */ - 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->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; +#ifdef DEBUG + printf("\n"); + printf("Loading magic plug-ins from %s\n", place); + fflush(stdout); +#endif + /* Gather list of files (for sorting): */ - do - { - f = readdir(d); + d = opendir(place); - if (f != NULL) + if (d != NULL) { - struct stat sbuf; + /* Set magic API hooks: */ - 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: */ + magic_api_struct = (magic_api *) malloc(sizeof(magic_api)); + magic_api_struct->tp_version = strdup(VER_VERSION); - strcpy(objname, f->d_name); - strcpy(strchr(objname, '.'), ""); + 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->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; + + 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: */ + + strcpy(objname, f->d_name); + strcpy(strchr(objname, '.'), ""); #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); - - if (magic_handle[num_plugin_files] != NULL) - { + magic_handle[num_plugin_files] = SDL_LoadObject(fname); + + if (magic_handle[num_plugin_files] != NULL) + { #ifdef DEBUG - printf("loading: %s\n", fname); - fflush(stdout); + printf("loading: %s\n", fname); + fflush(stdout); #endif - 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); + 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); - snprintf(funcname, sizeof(funcname), "%s_%s", objname, - "get_name"); - magic_funcs[num_plugin_files].get_name = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + snprintf(funcname, sizeof(funcname), "%s_%s", objname, "get_name"); + magic_funcs[num_plugin_files].get_name = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - snprintf(funcname, sizeof(funcname), "%s_%s", objname, - "get_icon"); - magic_funcs[num_plugin_files].get_icon = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + snprintf(funcname, sizeof(funcname), "%s_%s", objname, "get_icon"); + magic_funcs[num_plugin_files].get_icon = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - snprintf(funcname, sizeof(funcname), "%s_%s", objname, - "get_description"); - magic_funcs[num_plugin_files].get_description = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + snprintf(funcname, sizeof(funcname), "%s_%s", objname, "get_description"); + magic_funcs[num_plugin_files].get_description = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - snprintf(funcname, sizeof(funcname), "%s_%s", objname, - "requires_colors"); - magic_funcs[num_plugin_files].requires_colors = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + snprintf(funcname, sizeof(funcname), "%s_%s", objname, "requires_colors"); + magic_funcs[num_plugin_files].requires_colors = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - snprintf(funcname, sizeof(funcname), "%s_%s", objname, - "modes"); - magic_funcs[num_plugin_files].modes= - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + snprintf(funcname, sizeof(funcname), "%s_%s", objname, "modes"); + magic_funcs[num_plugin_files].modes = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - snprintf(funcname, sizeof(funcname), "%s_%s", objname, - "set_color"); - magic_funcs[num_plugin_files].set_color = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + snprintf(funcname, sizeof(funcname), "%s_%s", objname, "set_color"); + magic_funcs[num_plugin_files].set_color = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - snprintf(funcname, sizeof(funcname), "%s_%s", objname, - "init"); - magic_funcs[num_plugin_files].init = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + snprintf(funcname, sizeof(funcname), "%s_%s", objname, "init"); + magic_funcs[num_plugin_files].init = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - snprintf(funcname, sizeof(funcname), "%s_%s", objname, - "api_version"); - magic_funcs[num_plugin_files].api_version = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + snprintf(funcname, sizeof(funcname), "%s_%s", objname, "api_version"); + magic_funcs[num_plugin_files].api_version = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - snprintf(funcname, sizeof(funcname), "%s_%s", objname, - "shutdown"); - magic_funcs[num_plugin_files].shutdown = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + snprintf(funcname, sizeof(funcname), "%s_%s", objname, "shutdown"); + magic_funcs[num_plugin_files].shutdown = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - snprintf(funcname, sizeof(funcname), "%s_%s", objname, - "click"); - magic_funcs[num_plugin_files].click = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + snprintf(funcname, sizeof(funcname), "%s_%s", objname, "click"); + magic_funcs[num_plugin_files].click = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - snprintf(funcname, sizeof(funcname), "%s_%s", objname, - "drag"); - magic_funcs[num_plugin_files].drag = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + snprintf(funcname, sizeof(funcname), "%s_%s", objname, "drag"); + magic_funcs[num_plugin_files].drag = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - snprintf(funcname, sizeof(funcname), "%s_%s", objname, - "release"); - magic_funcs[num_plugin_files].release = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + snprintf(funcname, sizeof(funcname), "%s_%s", objname, "release"); + magic_funcs[num_plugin_files].release = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - snprintf(funcname, sizeof(funcname), "%s_%s", objname, - "switchin"); - magic_funcs[num_plugin_files].switchin = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + snprintf(funcname, sizeof(funcname), "%s_%s", objname, "switchin"); + magic_funcs[num_plugin_files].switchin = + SDL_LoadFunction(magic_handle[num_plugin_files], funcname); - snprintf(funcname, sizeof(funcname), "%s_%s", objname, - "switchout"); - magic_funcs[num_plugin_files].switchout = - SDL_LoadFunction(magic_handle[num_plugin_files], funcname); + 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_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_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_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_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 (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; + 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; - for (i = 0; i < n; i++) - { - magics[num_magics].idx = i; - magics[num_magics].place = plc; - magics[num_magics].handle_idx = num_plugin_files; - magics[num_magics].name = magic_funcs[num_plugin_files].get_name(magic_api_struct, i); + for (i = 0; i < n; i++) + { + magics[num_magics].idx = i; + magics[num_magics].place = plc; + magics[num_magics].handle_idx = num_plugin_files; + magics[num_magics].name = + magic_funcs[num_plugin_files].get_name(magic_api_struct, i); - magics[num_magics].avail_modes = magic_funcs[num_plugin_files].modes(magic_api_struct, i); + magics[num_magics].avail_modes = + magic_funcs[num_plugin_files].modes(magic_api_struct, i); - for (j = 0; j < MAX_MODES; j++) - { - magics[num_magics].tip[j] = NULL; - if (j) - { - if (magics[num_magics].avail_modes & MODE_FULLSCREEN) - magics[num_magics].tip[j] = magic_funcs[num_plugin_files].get_description(magic_api_struct, i, MODE_FULLSCREEN); - } - else - { - if (magics[num_magics].avail_modes & MODE_PAINT) - magics[num_magics].tip[j] = magic_funcs[num_plugin_files].get_description(magic_api_struct, i, MODE_PAINT); - else if (magics[num_magics].avail_modes & MODE_ONECLICK) - magics[num_magics].tip[j] = magic_funcs[num_plugin_files].get_description(magic_api_struct, i, MODE_ONECLICK); - else if (magics[num_magics].avail_modes & MODE_PAINT_WITH_PREVIEW) - magics[num_magics].tip[j] = magic_funcs[num_plugin_files].get_description(magic_api_struct, i, MODE_PAINT_WITH_PREVIEW); - } - } + for (j = 0; j < MAX_MODES; j++) + { + magics[num_magics].tip[j] = NULL; + if (j) + { + if (magics[num_magics].avail_modes & MODE_FULLSCREEN) + magics[num_magics].tip[j] = + magic_funcs[num_plugin_files].get_description(magic_api_struct, i, + MODE_FULLSCREEN); + } + else + { + if (magics[num_magics].avail_modes & MODE_PAINT) + magics[num_magics].tip[j] = + magic_funcs[num_plugin_files].get_description(magic_api_struct, i, + MODE_PAINT); + else if (magics[num_magics].avail_modes & MODE_ONECLICK) + magics[num_magics].tip[j] = + magic_funcs[num_plugin_files].get_description(magic_api_struct, i, + MODE_ONECLICK); + else if (magics[num_magics].avail_modes & MODE_PAINT_WITH_PREVIEW) + magics[num_magics].tip[j] = + magic_funcs[num_plugin_files].get_description(magic_api_struct, i, + MODE_PAINT_WITH_PREVIEW); + } + } - magics[num_magics].colors = magic_funcs[num_plugin_files].requires_colors(magic_api_struct, i); - if (magics[num_magics].avail_modes & MODE_PAINT) - magics[num_magics].mode = MODE_PAINT; - else if (magics[num_magics].avail_modes & MODE_ONECLICK) - magics[num_magics].mode = MODE_ONECLICK; - else if (magics[num_magics].avail_modes & MODE_PAINT_WITH_PREVIEW) - magics[num_magics].mode = MODE_PAINT_WITH_PREVIEW; - else - magics[num_magics].mode = MODE_FULLSCREEN; + magics[num_magics].colors = + magic_funcs[num_plugin_files].requires_colors(magic_api_struct, i); + if (magics[num_magics].avail_modes & MODE_PAINT) + magics[num_magics].mode = MODE_PAINT; + else if (magics[num_magics].avail_modes & MODE_ONECLICK) + magics[num_magics].mode = MODE_ONECLICK; + else if (magics[num_magics].avail_modes & MODE_PAINT_WITH_PREVIEW) + magics[num_magics].mode = MODE_PAINT_WITH_PREVIEW; + else + magics[num_magics].mode = MODE_FULLSCREEN; - magics[num_magics].img_icon = magic_funcs[num_plugin_files].get_icon(magic_api_struct, i); + magics[num_magics].img_icon = + magic_funcs[num_plugin_files].get_icon(magic_api_struct, i); #ifdef DEBUG - printf("-- %s\n", magics[num_magics].name); - printf("avail_modes = %d\n", magics[num_magics].avail_modes); + printf("-- %s\n", magics[num_magics].name); + printf("avail_modes = %d\n", magics[num_magics].avail_modes); #endif - num_magics++; - } + num_magics++; + } - num_plugin_files++; + num_plugin_files++; + } + } + } + else + { + fprintf(stderr, "Warning: Failed to load object %s: %s\n", fname, SDL_GetError()); + fflush(stderr); + } + } } - } } - else - { - fprintf(stderr, "Warning: Failed to load object %s: %s\n", fname, SDL_GetError()); - fflush(stderr); - } - } - } - } - while (f != NULL); + while (f != NULL); - closedir(d); + closedir(d); + } } - } qsort(magics, num_magics, sizeof(magic_t), magic_sort); #ifdef DEBUG - printf("Loaded %d magic tools from %d plug-in files\n", num_magics, - num_plugin_files); + printf("Loaded %d magic tools from %d plug-in files\n", num_magics, num_plugin_files); printf("\n"); fflush(stdout); #endif @@ -18606,12 +18314,12 @@ static void load_magic_plugins(void) -static int magic_sort(const void * a, const void * b) +static int magic_sort(const void *a, const void *b) { - magic_t * am = (magic_t *) a; - magic_t * bm = (magic_t *) b; + magic_t *am = (magic_t *) a; + magic_t *bm = (magic_t *) b; - return(strcoll(gettext(am->name), gettext(bm->name))); + return (strcoll(gettext(am->name), gettext(bm->name))); } @@ -18620,11 +18328,10 @@ static void update_progress_bar(void) show_progress_bar(screen); } -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)) +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 dx, dy, y; float m, b; @@ -18636,64 +18343,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) { - y1 = m * x1 + b; - y2 = m * (x1 + dx) + b; + m = ((float)dy) / ((float)dx); + b = y1 - m * x1; - if (y1 > y2) - { - for (y = y1; y >= y2; y--) - { - cnt = (cnt + 1) % step; - if (cnt == 0) - cb((void *) mapi, which, canvas, last, x1, y); - } - } + if (x2 >= x1) + dx = 1; else - { - for (y = y1; y <= y2; y++) - { - cnt = (cnt + 1) % step; - if (cnt == 0) - cb((void *) mapi, which, canvas, last, x1, y); - } - } + dx = -1; - x1 = x1 + dx; + + 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--) - { - cnt = (cnt + 1) % step; - if (cnt == 0) - cb((void *) mapi, which, canvas, last, x1, y); - } + 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); + } + } } - 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? */ } @@ -18707,31 +18414,32 @@ static void special_notify(int flags) int tmp_int; tmp_int = cur_undo - 1; - if (tmp_int < 0) tmp_int = NUM_UNDO_BUFS - 1; + if (tmp_int < 0) + 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(); - - undo_starters[tmp_int] = UNDO_STARTER_FLIPPED; - } + if (img_starter != NULL) + flip_starter(); + + undo_starters[tmp_int] = UNDO_STARTER_FLIPPED; + } } static void magic_stopsound(void) @@ -18753,13 +18461,13 @@ static void magic_playsound(Mix_Chunk * snd, int left_right, int up_down) /* Don't play if sound is disabled (nosound), or sound is temporarily muted (Alt+S), or sound ptr is NULL */ - + if (mute || !use_sound || snd == NULL) return; /* Don't override the same sound, if it's already playing */ - + if (!Mix_Playing(0) || magic_current_snd_ptr != snd) Mix_PlayChannel(0, snd, 0); @@ -18767,7 +18475,7 @@ static void magic_playsound(Mix_Chunk * snd, int left_right, int up_down) /* Adjust panning */ - + if (up_down < 0) up_down = 0; else if (up_down > 255) @@ -18788,22 +18496,22 @@ static void magic_playsound(Mix_Chunk * snd, int left_right, int up_down) static Uint8 magic_linear_to_sRGB(float lin) { - return(linear_to_sRGB(lin)); + return (linear_to_sRGB(lin)); } static float magic_sRGB_to_linear(Uint8 srgb) { - return(sRGB_to_linear_table[srgb]); + return (sRGB_to_linear_table[srgb]); } static int magic_button_down(void) { - return(button_down || emulate_button_pressed); + return (button_down || emulate_button_pressed); } -static SDL_Surface * magic_scale(SDL_Surface * surf, int w, int h, int aspect) +static SDL_Surface *magic_scale(SDL_Surface * surf, int w, int h, int aspect) { - return(thumbnail2(surf, w, h, aspect, 1)); + return (thumbnail2(surf, w, h, aspect, 1)); } /* FIXME: This, do_open() and do_slideshow() should be combined and modularized! */ @@ -18822,8 +18530,7 @@ static int do_new_dialog(void) int *d_places; FILE *fi; char fname[1024]; - int num_files, i, done, update_list, cur, which, - num_files_in_dirs, j; + int num_files, i, done, update_list, cur, which, num_files_in_dirs, j; SDL_Rect dest; SDL_Event event; SDLKey key; @@ -18841,14 +18548,14 @@ static int do_new_dialog(void) val_x = val_y = motioner = 0; - valhat_x = valhat_y = hatmotioner = 0; + valhat_x = valhat_y = hatmotioner = 0; do_setcursor(cursor_watch); /* Allocate some space: */ 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; @@ -18858,76 +18565,73 @@ 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++) - { - if (places_to_look == PLACE_SAVED_DIR) + for (places_to_look = 0; places_to_look < NUM_PLACES_TO_LOOK; places_to_look++) { - /* 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) + if (places_to_look == PLACE_SAVED_DIR) { - 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); - } + /* Skip saved images; only want starters! */ + dirname[places_to_look] = NULL; + continue; /* ugh */ } - } - while (f != NULL); + else if (places_to_look == PLACE_PERSONAL_STARTERS_DIR) + { + /* Check for coloring-book style 'starter' images in our folder: */ - closedir(d); + 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) + { + 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); + } } - } /* (Re)allocate space for the information about these files: */ @@ -18935,15 +18639,14 @@ static int do_new_dialog(void) tot = num_files_in_dirs + 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: */ - 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: */ @@ -18951,67 +18654,64 @@ static int do_new_dialog(void) white_in_palette = -1; for (j = -1; j < NUM_COLORS; j++) - { - added = 0; - - if (j < NUM_COLORS - 1) { - 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: */ + added = 0; - 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 < NUM_COLORS - 1) { - if (j == -1) - { - r = g = b = 255; /* White */ - } + 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; + } + } 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)); + { + white_in_palette = j; + } + } + else + { + /* Color picker: */ + + thumbs[num_files] = thumbnail(img_color_picker, THUMB_W - 20, THUMB_H - 20, 0); added = 1; } - } - else - { - white_in_palette = j; - } - } - else - { - /* Color picker: */ - thumbs[num_files] = thumbnail(img_color_picker, 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; - if (added) - { - d_places[num_files] = PLACE_COLOR_PALETTE; - d_names[num_files] = NULL; - d_exts[num_files] = NULL; - - num_files++; + num_files++; + } } - } first_starter = num_files; - first_template = -1; /* In case there are none... */ + first_template = -1; /* In case there are none... */ /* Read directory of images and build thumbnails: */ @@ -19019,298 +18719,297 @@ static int do_new_dialog(void) oldplace = -1; 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) && oldplace != place) - first_template = num_files; - - oldplace = place; - - - show_progress_bar(screen); - - if (f != NULL) { - debug(f->d_name); + f = &(fs[j].f); + place = fs[j].place; - 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 - ) + if ((place == PLACE_PERSONAL_TEMPLATES_DIR || place == PLACE_TEMPLATES_DIR) && oldplace != place) + first_template = num_files; + + oldplace = place; + + + show_progress_bar(screen); + + if (f != NULL) { - strcpy(fname, f->d_name); - skip = 0; - - if (strcasestr(fname, FNAME_EXTENSION) != NULL) - { - d_exts[num_files] = strdup(strcasestr(fname, FNAME_EXTENSION)); - strcpy((char *) strcasestr(fname, FNAME_EXTENSION), ""); - } - - if (strcasestr(fname, ".bmp") != NULL) - { - d_exts[num_files] = strdup(strcasestr(fname, ".bmp")); - strcpy((char *) strcasestr(fname, ".bmp"), ""); - } + 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 + /* 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 - if (strcasestr(fname, ".svg") != NULL) - { - d_exts[num_files] = strdup(strcasestr(fname, ".svg")); - strcpy((char *) strcasestr(fname, ".svg"), ""); - } + || strcasestr(f->d_name, ".svg") != NULL #endif - - if (strcasestr(fname, ".kpx") != NULL) - { - d_exts[num_files] = strdup(strcasestr(fname, ".kpx")); - strcpy((char *) strcasestr(fname, ".kpx"), ""); - } - - if (strcasestr(fname, ".jpg") != NULL) - { - d_exts[num_files] = strdup(strcasestr(fname, ".jpg")); - strcpy((char *) strcasestr(fname, ".jpg"), ""); - } - -#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); - strcpy(fname2, f2->d_name); - - if (strstr(fname2, fname) == fname2 && - 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: */ - - 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/"): */ - - 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("saved", "Can't create user data directory")) - { - /* (Make sure we have a .../saved/.thumbs/ directory:) */ - make_directory("saved/.thumbs", "Can't create user data thumbnail directory"); - } - - 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... */ - - /* FIXME: Add .jpg support -bjk 2007.03.22 */ - - /* (Try JPEG first) */ - 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) - { - 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) */ - 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) */ - snprintf(fname, sizeof(fname), "%s/%s-back", - dirname[d_places[num_files]], d_names[num_files]); - img = load_starter_helper(fname, "png", &IMG_Load); - } - } + strcpy(fname, f->d_name); + skip = 0; - if (img == NULL) - { - /* Didn't load a starter background (or didn't try!), - try loading the actual image... */ + if (strcasestr(fname, FNAME_EXTENSION) != NULL) + { + d_exts[num_files] = strdup(strcasestr(fname, FNAME_EXTENSION)); + strcpy((char *)strcasestr(fname, FNAME_EXTENSION), ""); + } - snprintf(fname, sizeof(fname), "%s/%s", - dirname[d_places[num_files]], f->d_name); - debug(fname); - img = myIMG_Load(fname); - } + if (strcasestr(fname, ".bmp") != NULL) + { + d_exts[num_files] = strdup(strcasestr(fname, ".bmp")); + strcpy((char *)strcasestr(fname, ".bmp"), ""); + } +#ifndef NOSVG + if (strcasestr(fname, ".svg") != NULL) + { + d_exts[num_files] = strdup(strcasestr(fname, ".svg")); + strcpy((char *)strcasestr(fname, ".svg"), ""); + } +#endif - show_progress_bar(screen); + if (strcasestr(fname, ".kpx") != NULL) + { + d_exts[num_files] = strdup(strcasestr(fname, ".kpx")); + strcpy((char *)strcasestr(fname, ".kpx"), ""); + } - 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()); + if (strcasestr(fname, ".jpg") != NULL) + { + d_exts[num_files] = strdup(strcasestr(fname, ".jpg")); + strcpy((char *)strcasestr(fname, ".jpg"), ""); + } - free(d_names[num_files]); - free(d_exts[num_files]); - } - else - { - /* Turn it into a thumbnail: */ +#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]; - img1 = SDL_DisplayFormatAlpha(img); - img2 = thumbnail2(img1, THUMB_W - 20, THUMB_H - 20, 0, 0); - SDL_FreeSurface(img1); + f2 = &(fs[k].f); + strcpy(fname2, f2->d_name); - 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!"); - - snprintf(fname, sizeof(fname), "%s/.thumbs/%s-t.png", - dirname[d_places[num_files]], d_names[num_files]); - - if (!make_directory("starters", "Can't create user data directory") || - !make_directory("templates", "Can't create user data directory") || - !make_directory("starters/.thumbs", "Can't create user data directory") || - !make_directory("templates/.thumbs", "Can't create user data directory")) - 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)); - } + if (strstr(fname2, fname) == fname2 && strcasestr(fname2, ".svg") != NULL) + { + /* SVG of this bitmap exists; we'll skip it */ + skip = 1; + } + } + } +#endif + if (skip) + { + free(d_exts[num_files]); + } else - { - do_png_save(fi, fname, thumbs[num_files], 0); - } + { + d_names[num_files] = strdup(fname); + d_places[num_files] = place; + + + /* Try to load thumbnail first: */ + + 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/"): */ + + 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("saved", "Can't create user data directory")) + { + /* (Make sure we have a .../saved/.thumbs/ directory:) */ + make_directory("saved/.thumbs", "Can't create user data thumbnail directory"); + } + + 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... */ + + /* FIXME: Add .jpg support -bjk 2007.03.22 */ + + /* (Try JPEG first) */ + 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) + { + 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) */ + 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) */ + 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... */ + + 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!"); + + snprintf(fname, sizeof(fname), "%s/.thumbs/%s-t.png", + dirname[d_places[num_files]], d_names[num_files]); + + if (!make_directory("starters", "Can't create user data directory") || + !make_directory("templates", "Can't create user data directory") || + !make_directory("starters/.thumbs", "Can't create user data directory") || + !make_directory("templates/.thumbs", "Can't create user data directory")) + 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++; + } + } + } } - - show_progress_bar(screen); - } - - - num_files++; } - } + else + { + /* It was a thumbnail file ("...-t.png") or immutable scene starter's + overlay layer ("...-front.png") */ + } } - } - } - else - { - /* It was a thumbnail file ("...-t.png") or immutable scene starter's - overlay layer ("...-front.png") */ - } } - } @@ -19340,634 +19039,610 @@ static int do_new_dialog(void) do - { - /* Update screen: */ - - if (update_list) { - /* Erase screen: */ + /* Update screen: */ - dest.x = 96; - dest.y = 0; - dest.w = WINDOW_WIDTH - 96 - 96; - dest.h = 48 * 7 + 40 + HEIGHTOFFSET; - - 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) + 96; - dest.y = THUMB_H * ((i - cur) / 4) + 24; - - if (d_places[i] == PLACE_SAVED_DIR) + if (update_list) { - if (i == which) - { - SDL_BlitSurface(img_cursor_down, NULL, screen, &dest); - debug(d_names[i]); - } + /* Erase screen: */ + + dest.x = 96; + dest.y = 0; + dest.w = WINDOW_WIDTH - 96 - 96; + dest.h = 48 * 7 + 40 + HEIGHTOFFSET; + + 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) + 96; + dest.y = THUMB_H * ((i - cur) / 4) + 24; + + 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) + 96 + 10 + (THUMB_W - 20 - thumbs[i]->w) / 2; + dest.y = THUMB_H * ((i - cur) / 4) + 24 + 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_cursor_up, NULL, screen, &dest); - } - else - { - if (i == which) - { - SDL_BlitSurface(img_cursor_starter_down, NULL, screen, &dest); - debug(d_names[i]); - } + SDL_BlitSurface(img_scroll_up_off, NULL, screen, &dest); + + dest.x = (WINDOW_WIDTH - img_scroll_up->w) / 2; + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; + + if (cur < num_files - 16) + SDL_BlitSurface(img_scroll_down, NULL, screen, &dest); else - SDL_BlitSurface(img_cursor_starter_up, NULL, screen, &dest); + SDL_BlitSurface(img_scroll_down_off, NULL, screen, &dest); + + + /* "Open" button: */ + + dest.x = 96; + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; + SDL_BlitSurface(img_open, NULL, screen, &dest); + + dest.x = 96 + (48 - img_openlabels_open->w) / 2; + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - img_openlabels_open->h; + SDL_BlitSurface(img_openlabels_open, NULL, screen, &dest); + + + /* "Back" button: */ + + dest.x = WINDOW_WIDTH - 96 - 48; + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; + SDL_BlitSurface(img_back, NULL, screen, &dest); + + dest.x = WINDOW_WIDTH - 96 - 48 + (48 - img_openlabels_back->w) / 2; + dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - img_openlabels_back->h; + SDL_BlitSurface(img_openlabels_back, NULL, screen, &dest); + + + 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; + + 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)) + { + if (event.button.x >= 96 && event.button.x < WINDOW_WIDTH - 96 && + event.button.y >= 24 && event.button.y < (48 * 7 + 40 + HEIGHTOFFSET - 48)) + { + /* Picked an icon! */ + + which = ((event.button.x - 96) / (THUMB_W) + (((event.button.y - 24) / THUMB_H) * 4)) + cur; + + if (which < num_files) + { + playsound(screen, 1, SND_BLEEP, 1, event.button.x, SNDDIST_NEAR); + update_list = 1; - dest.x = THUMB_W * ((i - cur) % 4) + 96 + 10 + - (THUMB_W - 20 - thumbs[i]->w) / 2; - dest.y = THUMB_H * ((i - cur) / 4) + 24 + 10 + - (THUMB_H - 20 - thumbs[i]->h) / 2; + if (which == last_click_which && + SDL_GetTicks() < last_click_time + 1000 && event.button.button == last_click_button) + { + /* Double-click! */ - if (thumbs[i] != NULL) - SDL_BlitSurface(thumbs[i], NULL, screen, &dest); - } + 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 < 24) + { + /* 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 >= (48 * 7 + 40 + HEIGHTOFFSET - 48) && + event.button.y < (48 * 7 + 40 + HEIGHTOFFSET - 24)) + { + /* Down scroll button: */ - dest.x = (WINDOW_WIDTH - img_scroll_up->w) / 2; - dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; + 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; + } + } + else if (event.button.x >= 96 && event.button.x < 96 + 48 && + event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && + event.button.y < (48 * 7 + 40 + HEIGHTOFFSET)) + { + /* Open */ - /* "Open" button: */ + done = 1; + playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); + } + else if (event.button.x >= (WINDOW_WIDTH - 96 - 48) && + event.button.x < (WINDOW_WIDTH - 96) && + event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && + event.button.y < (48 * 7 + 40 + HEIGHTOFFSET)) + { + /* Back */ - dest.x = 96; - dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; - SDL_BlitSurface(img_open, 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_MOUSEBUTTONUP) + { +#ifdef __ANDROID__ + stop_motion_convert(event); +#endif + } + else if (event.type == SDL_MOUSEWHEEL && wheely) + { + /* Scroll wheel! */ - dest.x = 96 + (48 - img_openlabels_open->w) / 2; - dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - img_openlabels_open->h; - SDL_BlitSurface(img_openlabels_open, 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); - /* "Back" button: */ + 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); - dest.x = WINDOW_WIDTH - 96 - 48; - dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - 48; - SDL_BlitSurface(img_back, NULL, screen, &dest); + if (cur >= num_files - 16) + do_setcursor(cursor_arrow); - dest.x = WINDOW_WIDTH - 96 - 48 + (48 - img_openlabels_back->w) / 2; - dest.y = (48 * 7 + 40 + HEIGHTOFFSET) - img_openlabels_back->h; - SDL_BlitSurface(img_openlabels_back, NULL, screen, &dest); + if (which < cur) + which = which + 4; + } + } + else if (event.type == SDL_MOUSEMOTION) + { + /* Deal with mouse pointer shape! */ + if (event.button.y < 24 && + 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: */ - SDL_Flip(screen); + do_setcursor(cursor_up); + } + else if (event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET - 48) && + event.button.y < (48 * 7 + 40 + HEIGHTOFFSET - 24) && + 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: */ - update_list = 0; + do_setcursor(cursor_down); + } + else if (((event.button.x >= 96 && event.button.x < 96 + 48 + 48) || + (event.button.x >= (WINDOW_WIDTH - 96 - 48) && + event.button.x < (WINDOW_WIDTH - 96)) || + (event.button.x >= (WINDOW_WIDTH - 96 - 48 - 48) && + event.button.x < (WINDOW_WIDTH - 48 - 96) && + d_places[which] != PLACE_STARTERS_DIR && + d_places[which] != PLACE_PERSONAL_STARTERS_DIR)) && + event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && + event.button.y < (48 * 7 + 40 + HEIGHTOFFSET)) + { + /* One of the command buttons: */ + + do_setcursor(cursor_hand); + } + else if (event.button.x >= 96 && event.button.x < WINDOW_WIDTH - 96 && + event.button.y > 24 && + event.button.y < (48 * 7 + 40 + HEIGHTOFFSET) - 48 && + ((((event.button.x - 96) / (THUMB_W) + + (((event.button.y - 24) / 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_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); } - - /* 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)) - { - if (event.button.x >= 96 && event.button.x < WINDOW_WIDTH - 96 && - event.button.y >= 24 && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET - 48)) - { - /* Picked an icon! */ - - which = ((event.button.x - 96) / (THUMB_W) + - (((event.button.y - 24) / 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 < 24) - { - /* 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 >= (48 * 7 + 40 + HEIGHTOFFSET - 48) && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET - 24)) - { - /* 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; - } - } - else if (event.button.x >= 96 && event.button.x < 96 + 48 && - event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET)) - { - /* Open */ - - done = 1; - playsound(screen, 1, SND_CLICK, 1, SNDPOS_LEFT, SNDDIST_NEAR); - } - else if (event.button.x >= (WINDOW_WIDTH - 96 - 48) && - event.button.x < (WINDOW_WIDTH - 96) && - event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET)) - { - /* 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_MOUSEBUTTONUP) - { -#ifdef __ANDROID__ - stop_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 < 24 && - 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 >= (48 * 7 + 40 + HEIGHTOFFSET - 48) && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET - 24) && - 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 >= 96 && event.button.x < 96 + 48 + 48) || - (event.button.x >= (WINDOW_WIDTH - 96 - 48) && - event.button.x < (WINDOW_WIDTH - 96)) || - (event.button.x >= (WINDOW_WIDTH - 96 - 48 - 48) && - event.button.x < (WINDOW_WIDTH - 48 - 96) && - d_places[which] != PLACE_STARTERS_DIR && - d_places[which] != PLACE_PERSONAL_STARTERS_DIR)) && - event.button.y >= (48 * 7 + 40 + HEIGHTOFFSET) - 48 && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET)) - { - /* One of the command buttons: */ - - do_setcursor(cursor_hand); - } - else if (event.button.x >= 96 && event.button.x < WINDOW_WIDTH - 96 && - event.button.y > 24 && - event.button.y < (48 * 7 + 40 + HEIGHTOFFSET) - 48 && - ((((event.button.x - 96) / (THUMB_W) + - (((event.button.y - 24) / 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_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) { - 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); - } - } + /* Save old one first? */ - /* Clear label surface */ + 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); + } + } - SDL_FillRect(label, NULL, SDL_MapRGBA(label->format, 0, 0, 0, 0)); + /* Clear label surface */ - /* Clear all info related to label surface */ + SDL_FillRect(label, NULL, SDL_MapRGBA(label->format, 0, 0, 0, 0)); - 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)) - { - /* Load a starter: */ + /* Clear all info related to label surface */ - /* Figure out filename: */ + 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; - snprintf(fname, sizeof(fname), "%s/%s%s", - dirname[d_places[which]], d_names[which], d_exts[which]); + if (which >= first_starter && (first_template == -1 || which < first_template)) + { + /* Load a starter: */ - img = myIMG_Load(fname); + /* Figure out filename: */ - 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()); + snprintf(fname, sizeof(fname), "%s/%s%s", dirname[d_places[which]], d_names[which], d_exts[which]); - do_prompt(PROMPT_OPEN_UNOPENABLE_TXT, - PROMPT_OPEN_UNOPENABLE_YES, "", 0 ,0); - } + 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'; + strcpy(starter_id, d_names[which]); + 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) + { + /* Load a template: */ + + /* Figure out filename: */ + + 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'; + strcpy(template_id, d_names[which]); + 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 - { - free_surface(&img_starter); - free_surface(&img_starter_bkgd); - starter_mirrored = 0; - starter_flipped = 0; - starter_personal = 0; - starter_modified = 0; + { + /* A color! */ - 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'; - strcpy(starter_id, d_names[which]); - template_id[0] = '\0'; - - if (d_places[which] == PLACE_PERSONAL_STARTERS_DIR) - starter_personal = 1; - else + free_surface(&img_starter); + free_surface(&img_starter_bkgd); + starter_mirrored = 0; + starter_flipped = 0; starter_personal = 0; + starter_modified = 0; - load_starter(starter_id); + /* Launch color picker if they chose that: */ - canvas_color_r = 255; - canvas_color_g = 255; - canvas_color_b = 255; + if (which == NUM_COLORS - 1) + { + if (do_color_picker() == 0) + return (0); + } - 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); - } + /* 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); + } } - else if (first_template != -1 && which >= first_template) - { - /* Load a template: */ - - /* Figure out filename: */ - - 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'; - strcpy(template_id, d_names[which]); - 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; - - /* Launch color picker if they chose that: */ - - if (which == NUM_COLORS - 1) - { - if (do_color_picker() == 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 - 96 - 96, 48 * 7 + 40 + HEIGHTOFFSET); @@ -19979,12 +19654,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) @@ -19994,7 +19669,7 @@ static int do_new_dialog(void) free(d_exts); free(d_places); - return(which != -1); + return (which != -1); } /* FIXME: Use a bitmask! */ @@ -20004,12 +19679,12 @@ static void reset_touched(void) int x, y; for (y = 0; y < canvas->h; y++) - { - for (x = 0; x < canvas->w; x++) { - touched[(y * canvas->w) + x] = 0; + for (x = 0; x < canvas->w; x++) + { + touched[(y * canvas->w) + x] = 0; + } } - } } static Uint8 magic_touched(int x, int y) @@ -20017,12 +19692,12 @@ static Uint8 magic_touched(int x, int y) Uint8 res; if (x < 0 || x >= canvas->w || y < 0 || y >= canvas->h) - return(1); + return (1); res = touched[(y * canvas->w) + x]; - touched[(y* canvas->w) + x] = 1; + touched[(y * canvas->w) + x] = 1; - return(res); + return (res); } static int do_color_sel(void) @@ -20041,7 +19716,8 @@ static int do_color_sel(void) int done, chose; int back_left, back_top; int color_sel_x, color_sel_y; - SDL_Surface * tmp_btn_up, * tmp_btn_down; + SDL_Surface *tmp_btn_up, *tmp_btn_down; + Uint32(*getpixel_tmp_btn_up) (SDL_Surface *, int, int); Uint32(*getpixel_tmp_btn_down) (SDL_Surface *, int, int); Uint32(*getpixel_img_paintwell) (SDL_Surface *, int, int); @@ -20052,14 +19728,15 @@ static int do_color_sel(void) SDLKey key; SDL_Rect r_color_sel; SDL_Rect color_example_dest; - SDL_Surface * backup; + SDL_Surface *backup; SDL_Rect r_color_picker; + val_x = val_y = motioner = 0; valhat_x = valhat_y = hatmotioner = 0; /* FIXME this is the first step to make animated popups optional, to be removed from here when implemented in a more general way */ - int want_animated_popups=1; + int want_animated_popups = 1; hide_blinking_cursor(); @@ -20071,11 +19748,9 @@ static int do_color_sel(void) playsound(screen, 0, SND_PROMPT, 1, SNDPOS_RIGHT, 128); 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->BitsPerPixel, + screen->format->Rmask, + screen->format->Gmask, screen->format->Bmask, screen->format->Amask); SDL_BlitSurface(screen, NULL, backup, NULL); @@ -20086,74 +19761,72 @@ static int do_color_sel(void) 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++) { - w = w + (128 + 6 +4) / number_of_steps; - dx = i * stepx; - dy = i * stepy; + /* 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; - 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, - r_color_sel.w, - r_color_sel.h, - screen->format->BitsPerPixel, - screen->format->Rmask, - screen->format->Gmask, - screen->format->Bmask, - screen->format->Amask); + alpha_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, + r_color_sel.w, + r_color_sel.h, + screen->format->BitsPerPixel, + screen->format->Rmask, + screen->format->Gmask, screen->format->Bmask, screen->format->Amask); - if (alpha_surf != NULL) + if (alpha_surf != NULL) { SDL_FillRect(alpha_surf, NULL, SDL_MapRGB(alpha_surf->format, 0, 0, 0)); SDL_SetSurfaceAlphaMod(alpha_surf, 64); 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; + { + 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_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)); - + /* Determine spot for example color: */ @@ -20163,16 +19836,14 @@ static int do_color_sel(void) 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; @@ -20184,10 +19855,9 @@ static int do_color_sel(void) /* Draw current color picker color: */ SDL_FillRect(screen, &color_example_dest, - SDL_MapRGB(screen->format, - color_hexes[NUM_COLORS - 2][0], - color_hexes[NUM_COLORS - 2][1], - color_hexes[NUM_COLORS - 2][2])); + SDL_MapRGB(screen->format, + color_hexes[NUM_COLORS - 2][0], + color_hexes[NUM_COLORS - 2][1], color_hexes[NUM_COLORS - 2][2])); @@ -20224,238 +19894,221 @@ static int do_color_sel(void) 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)) { - 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) + while (SDL_PollEvent(&event)) { - 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! */ + 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 = 1; - done = 1; + handle_keymouse(key, SDL_KEYUP, 24, NULL, NULL); + } + else if (event.type == SDL_KEYDOWN) + { + key = event.key.keysym.sym; - x = event.button.x - r_canvas.x; - y = event.button.y - r_canvas.y; + handle_keymouse(key, SDL_KEYDOWN, 24, &r_color_picker, NULL); - color_sel_x = x; - color_sel_y = y; - } - 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 >= 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 colors! */ + if (key == SDLK_ESCAPE) + { + 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! */ - do_setcursor(cursor_hand); + 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 (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 >= 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 colors! */ + + do_setcursor(cursor_hand); - /* Show a big solid example of the color: */ - - x = event.button.x - r_canvas.x; - y = event.button.y - r_canvas.y; + /* Show a big solid example of the color: */ - getpixel_img_color_picker = getpixels[canvas->format->BytesPerPixel]; - SDL_GetRGB(getpixel_img_color_picker(canvas, x, y), canvas->format, &r, &g, &b); + x = event.button.x - r_canvas.x; + y = event.button.y - r_canvas.y; - SDL_FillRect(screen, &color_example_dest, - SDL_MapRGB(screen->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_UpdateRect(screen, - color_example_dest.x, - color_example_dest.y, - color_example_dest.w, - color_example_dest.h); - } - else - { - /* 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, r, g, b)); - SDL_FillRect(screen, &color_example_dest, - SDL_MapRGB(screen->format, - color_hexes[NUM_COLORS - 2][0], - color_hexes[NUM_COLORS - 2][1], - color_hexes[NUM_COLORS - 2][2])); + SDL_UpdateRect(screen, + color_example_dest.x, + color_example_dest.y, color_example_dest.w, color_example_dest.h); + } + else + { + /* Revert to current color picker color, so we know what it was, + and what we'll get if we go Back: */ - SDL_UpdateRect(screen, - color_example_dest.x, - color_example_dest.y, - color_example_dest.w, - color_example_dest.h); + SDL_FillRect(screen, &color_example_dest, + SDL_MapRGB(screen->format, + color_hexes[NUM_COLORS - 2][0], + color_hexes[NUM_COLORS - 2][1], color_hexes[NUM_COLORS - 2][2])); + + 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); + 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); } - oldpos_x = event.motion.x; - oldpos_y = event.motion.y; - } - else if (event.type == SDL_JOYAXISMOTION) - handle_joyaxismotion(event, &motioner, &val_x, &val_y); + if (motioner | hatmotioner) + handle_motioners(oldpos_x, oldpos_y, motioner, hatmotioner, old_hat_ticks, val_x, val_y, valhat_x, valhat_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); + SDL_Delay(10); } - - 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); - - color_hexes[NUM_COLORS - 2][0] = r; - color_hexes[NUM_COLORS - 2][1] = g; - color_hexes[NUM_COLORS - 2][2] = b; - - - /* Re-render color picker to show the current color it contains: */ - - 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); - - getpixel_tmp_btn_up = getpixels[tmp_btn_up->format->BytesPerPixel]; - getpixel_tmp_btn_down = getpixels[tmp_btn_down->format->BytesPerPixel]; - getpixel_img_paintwell = getpixels[img_paintwell->format->BytesPerPixel]; - - rh = sRGB_to_linear_table[color_hexes[NUM_COLORS - 2][0]]; - gh = sRGB_to_linear_table[color_hexes[NUM_COLORS - 2][1]]; - bh = sRGB_to_linear_table[color_hexes[NUM_COLORS - 2][2]]; - - - - SDL_LockSurface(img_color_btns[NUM_COLORS - 2]); - SDL_LockSurface(img_color_btns[NUM_COLORS - 2 + NUM_COLORS]); - - for (y = 0; y < tmp_btn_up->h /* 48 */ ; y++) { - for (x = 0; x < tmp_btn_up->w; x++) - { - double ru, gu, bu, rd, gd, bd, aa; - Uint8 a; + 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); - SDL_GetRGB(getpixel_tmp_btn_up(tmp_btn_up, x, y), tmp_btn_up->format, - &r, &g, &b); + color_hexes[NUM_COLORS - 2][0] = r; + color_hexes[NUM_COLORS - 2][1] = g; + color_hexes[NUM_COLORS - 2][2] = 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); + /* Re-render color picker to show the current color it contains: */ - aa = a / 255.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); - if (a == 255) + getpixel_tmp_btn_up = getpixels[tmp_btn_up->format->BytesPerPixel]; + getpixel_tmp_btn_down = getpixels[tmp_btn_down->format->BytesPerPixel]; + getpixel_img_paintwell = getpixels[img_paintwell->format->BytesPerPixel]; + + rh = sRGB_to_linear_table[color_hexes[NUM_COLORS - 2][0]]; + gh = sRGB_to_linear_table[color_hexes[NUM_COLORS - 2][1]]; + bh = sRGB_to_linear_table[color_hexes[NUM_COLORS - 2][2]]; + + + + SDL_LockSurface(img_color_btns[NUM_COLORS - 2]); + SDL_LockSurface(img_color_btns[NUM_COLORS - 2 + NUM_COLORS]); + + for (y = 0; y < tmp_btn_up->h /* 48 */ ; y++) { - putpixels[img_color_btns[NUM_COLORS - 2]->format->BytesPerPixel] - (img_color_btns[NUM_COLORS - 2], 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)))); + for (x = 0; x < tmp_btn_up->w; x++) + { + double ru, gu, bu, rd, gd, bd, aa; + Uint8 a; - putpixels[img_color_btns[NUM_COLORS - 2 + NUM_COLORS]->format->BytesPerPixel] - (img_color_btns[NUM_COLORS - 2 + 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)))); + 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; + + if (a == 255) + { + putpixels[img_color_btns[NUM_COLORS - 2]->format->BytesPerPixel] + (img_color_btns[NUM_COLORS - 2], 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[NUM_COLORS - 2 + NUM_COLORS]->format->BytesPerPixel] + (img_color_btns[NUM_COLORS - 2 + 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)))); + } + } } - } + + SDL_UnlockSurface(img_color_btns[NUM_COLORS - 2]); + SDL_UnlockSurface(img_color_btns[NUM_COLORS - 2 + NUM_COLORS]); + + dest.x = (img_color_btns[NUM_COLORS - 2]->w - img_color_sel->w) / 2; + dest.y = (img_color_btns[NUM_COLORS - 2]->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[NUM_COLORS - 2], &dest); + + dest.x = (img_color_btns[NUM_COLORS - 2 + NUM_COLORS]->w - img_color_sel->w) / 2; + dest.y = (img_color_btns[NUM_COLORS - 2 + NUM_COLORS]->h - img_color_sel->h) / 2; + SDL_BlitSurface(img_color_sel, NULL, img_color_btns[NUM_COLORS - 2 + NUM_COLORS], &dest); } - SDL_UnlockSurface(img_color_btns[NUM_COLORS - 2]); - SDL_UnlockSurface(img_color_btns[NUM_COLORS - 2 + NUM_COLORS]); - - dest.x = (img_color_btns[NUM_COLORS - 2]->w - img_color_sel->w) / 2; - dest.y = (img_color_btns[NUM_COLORS - 2]->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[NUM_COLORS - 2], &dest); - - dest.x = (img_color_btns[NUM_COLORS - 2 + NUM_COLORS]->w - img_color_sel->w) / 2; - dest.y = (img_color_btns[NUM_COLORS - 2 + NUM_COLORS]->h - img_color_sel->h) / 2; - SDL_BlitSurface(img_color_sel, NULL, img_color_btns[NUM_COLORS - 2 + NUM_COLORS], &dest); - } - - return(chose); + return (chose); } static int do_color_picker(void) @@ -20469,7 +20122,8 @@ static int do_color_picker(void) int ox, oy, oox, ooy, nx, ny; int val_x, val_y, motioner; int valhat_x, valhat_y, hatmotioner; - SDL_Surface * tmp_btn_up, * tmp_btn_down; + SDL_Surface *tmp_btn_up, *tmp_btn_down; + Uint32(*getpixel_tmp_btn_up) (SDL_Surface *, int, int); Uint32(*getpixel_tmp_btn_down) (SDL_Surface *, int, int); Uint32(*getpixel_img_paintwell) (SDL_Surface *, int, int); @@ -20482,7 +20136,7 @@ static int do_color_picker(void) int color_picker_left, color_picker_top; int back_left, back_top; SDL_Rect color_example_dest; - SDL_Surface * backup; + SDL_Surface *backup; SDL_Rect r_color_picker; val_x = val_y = motioner = 0; @@ -20497,11 +20151,9 @@ static int do_color_picker(void) playsound(screen, 0, SND_PROMPT, 1, SNDPOS_RIGHT, 128); 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->BitsPerPixel, + screen->format->Rmask, + screen->format->Gmask, screen->format->Bmask, screen->format->Amask); SDL_BlitSurface(screen, NULL, backup, NULL); @@ -20509,25 +20161,25 @@ static int do_color_picker(void) oy = r_colors.y + r_colors.h / 2; for (w = 0; w <= 128 + 6 + 4; w = w + 4) - { - oox = ox - w; - ooy = oy - w; + { + oox = ox - w; + ooy = oy - w; - nx = PROMPT_LEFT + 96 - w + PROMPTOFFSETX; - ny = 94 + 96 - w + PROMPTOFFSETY; + nx = PROMPT_LEFT + 96 - w + PROMPTOFFSETX; + ny = 94 + 96 - w + PROMPTOFFSETY; - dest.x = ((nx * w) + (oox * (128 - w))) / 128; - dest.y = ((ny * w) + (ooy * (128 - w))) / 128; + dest.x = ((nx * w) + (oox * (128 - w))) / 128; + dest.y = ((ny * w) + (ooy * (128 - w))) / 128; - dest.w = (PROMPT_W - 96 * 2) + w * 2; - dest.h = w * 2; - SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 255 - w, 255 - w, 255 - w)); + dest.w = (PROMPT_W - 96 * 2) + w * 2; + dest.h = w * 2; + 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); - 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); @@ -20537,28 +20189,26 @@ static int do_color_picker(void) (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_MapRGB(alpha_surf->format, 0, 0, 0)); - SDL_SetSurfaceAlphaMod(alpha_surf, 64); - - - for (i = 8; i > 0; i = i - 2) { - dest.x = PROMPT_LEFT + 96 - (w - 4) + i + PROMPTOFFSETX; - dest.y = 94 + 96 - (w - 4) + i + PROMPTOFFSETY; - dest.w = (PROMPT_W - 96 * 2) + (w - 4) * 2; - dest.h = (w - 4) * 2; + SDL_FillRect(alpha_surf, NULL, SDL_MapRGB(alpha_surf->format, 0, 0, 0)); + SDL_SetSurfaceAlphaMod(alpha_surf, 64); - SDL_BlitSurface(alpha_surf, NULL, screen, &dest); + + for (i = 8; i > 0; i = i - 2) + { + dest.x = PROMPT_LEFT + 96 - (w - 4) + i + PROMPTOFFSETX; + dest.y = 94 + 96 - (w - 4) + i + PROMPTOFFSETY; + dest.w = (PROMPT_W - 96 * 2) + (w - 4) * 2; + dest.h = (w - 4) * 2; + + SDL_BlitSurface(alpha_surf, NULL, screen, &dest); + } + + SDL_FreeSurface(alpha_surf); } - - SDL_FreeSurface(alpha_surf); - } #endif @@ -20619,7 +20269,7 @@ static int do_color_picker(void) SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 255, 255, 255)); - + /* Determine spot for example color: */ color_example_dest.x = color_picker_left + img_color_picker->w + 2; @@ -20628,16 +20278,14 @@ static int do_color_picker(void) color_example_dest.h = 124; - 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; @@ -20648,16 +20296,16 @@ static int do_color_picker(void) /* Draw current color picker color: */ SDL_FillRect(screen, &color_example_dest, - SDL_MapRGB(screen->format, - color_hexes[NUM_COLORS - 1][0], - color_hexes[NUM_COLORS - 1][1], - color_hexes[NUM_COLORS - 1][2])); + SDL_MapRGB(screen->format, + color_hexes[NUM_COLORS - 1][0], + color_hexes[NUM_COLORS - 1][1], color_hexes[NUM_COLORS - 1][2])); /* Show "Back" button */ - back_left = (((PROMPT_W - 96 * 2) + w * 2 - img_color_picker->w) - img_back->w) / 2 + color_picker_left + img_color_picker->w; + back_left = + (((PROMPT_W - 96 * 2) + w * 2 - img_color_picker->w) - img_back->w) / 2 + color_picker_left + img_color_picker->w; back_top = color_picker_top + img_color_picker->h - img_back->h - 2; dest.x = back_left; @@ -20681,235 +20329,216 @@ static int do_color_picker(void) SDL_WarpMouse(back_left + button_w / 2, back_top - button_w / 2); do - { - 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) + while (SDL_PollEvent(&event)) { - 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! */ + 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 = 1; - done = 1; + 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; - } - 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_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 (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! */ - do_setcursor(cursor_hand); + chose = 1; + done = 1; + + x = event.button.x - color_picker_left; + y = event.button.y - color_picker_top; + + color_picker_x = x; + color_picker_y = y; + } + 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_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_hand); - /* Show a big solid example of the color: */ - - x = event.button.x - color_picker_left; - y = event.button.y - color_picker_top; + /* Show a big solid example of the color: */ - getpixel_img_color_picker = getpixels[img_color_picker->format->BytesPerPixel]; - 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)); + getpixel_img_color_picker = getpixels[img_color_picker->format->BytesPerPixel]; + SDL_GetRGB(getpixel_img_color_picker(img_color_picker, x, y), img_color_picker->format, &r, &g, &b); - SDL_UpdateRect(screen, - color_example_dest.x, - color_example_dest.y, - color_example_dest.w, - color_example_dest.h); - } - else - { - /* 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, r, g, b)); - SDL_FillRect(screen, &color_example_dest, - SDL_MapRGB(screen->format, - color_hexes[NUM_COLORS - 1][0], - color_hexes[NUM_COLORS - 1][1], - color_hexes[NUM_COLORS - 1][2])); + SDL_UpdateRect(screen, + color_example_dest.x, + color_example_dest.y, color_example_dest.w, color_example_dest.h); + } + else + { + /* Revert to current color picker color, so we know what it was, + and what we'll get if we go Back: */ - SDL_UpdateRect(screen, - color_example_dest.x, - color_example_dest.y, - color_example_dest.w, - color_example_dest.h); + SDL_FillRect(screen, &color_example_dest, + SDL_MapRGB(screen->format, + color_hexes[NUM_COLORS - 1][0], + color_hexes[NUM_COLORS - 1][1], color_hexes[NUM_COLORS - 1][2])); + + 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); + 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); } - oldpos_x = event.motion.x; - oldpos_y = event.motion.y; - } - else if (event.type == SDL_JOYAXISMOTION) - handle_joyaxismotion(event, &motioner, &val_x, &val_y); + if (motioner | hatmotioner) + handle_motioners(oldpos_x, oldpos_y, motioner, hatmotioner, old_hat_ticks, val_x, val_y, valhat_x, valhat_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); + SDL_Delay(10); } - - 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[img_color_picker->format->BytesPerPixel]; - SDL_GetRGB(getpixel_img_color_picker(img_color_picker, x, y), img_color_picker->format, &r, &g, &b); - - color_hexes[NUM_COLORS - 1][0] = r; - color_hexes[NUM_COLORS - 1][1] = g; - color_hexes[NUM_COLORS - 1][2] = b; - - - /* Re-render color picker to show the current color it contains: */ - - 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); - - getpixel_tmp_btn_up = getpixels[tmp_btn_up->format->BytesPerPixel]; - getpixel_tmp_btn_down = getpixels[tmp_btn_down->format->BytesPerPixel]; - getpixel_img_paintwell = getpixels[img_paintwell->format->BytesPerPixel]; - - rh = sRGB_to_linear_table[color_hexes[NUM_COLORS - 1][0]]; - gh = sRGB_to_linear_table[color_hexes[NUM_COLORS - 1][1]]; - bh = sRGB_to_linear_table[color_hexes[NUM_COLORS - 1][2]]; - SDL_BlitSurface(tmp_btn_down, NULL, img_color_btns[NUM_COLORS - 1], NULL); - SDL_BlitSurface(tmp_btn_up, NULL, img_color_btns[NUM_COLORS - 1 + NUM_COLORS], NULL); - - SDL_LockSurface(img_color_btns[NUM_COLORS - 1]); - SDL_LockSurface(img_color_btns[NUM_COLORS - 1 + NUM_COLORS]); - - for (y = 0; y < tmp_btn_up->h /* 48 */ ; y++) { - for (x = 0; x < tmp_btn_up->w; x++) - { - double ru, gu, bu, rd, gd, bd, aa; - Uint8 a; + getpixel_img_color_picker = getpixels[img_color_picker->format->BytesPerPixel]; + SDL_GetRGB(getpixel_img_color_picker(img_color_picker, x, y), img_color_picker->format, &r, &g, &b); - SDL_GetRGB(getpixel_tmp_btn_up(tmp_btn_up, x, y), tmp_btn_up->format, - &r, &g, &b); + color_hexes[NUM_COLORS - 1][0] = r; + color_hexes[NUM_COLORS - 1][1] = g; + color_hexes[NUM_COLORS - 1][2] = 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); + /* Re-render color picker to show the current color it contains: */ - aa = a / 255.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); - putpixels[img_color_btns[NUM_COLORS - 1]->format->BytesPerPixel] - (img_color_btns[NUM_COLORS - 1], x, y, - getpixels[img_color_picker_thumb->format->BytesPerPixel] - (img_color_picker_thumb, x, y)); - putpixels[img_color_btns[NUM_COLORS - 1 + NUM_COLORS]->format->BytesPerPixel] - (img_color_btns[NUM_COLORS - 1 + NUM_COLORS], x, y, - getpixels[img_color_picker_thumb->format->BytesPerPixel] - (img_color_picker_thumb, x, y)); + getpixel_tmp_btn_up = getpixels[tmp_btn_up->format->BytesPerPixel]; + getpixel_tmp_btn_down = getpixels[tmp_btn_down->format->BytesPerPixel]; + getpixel_img_paintwell = getpixels[img_paintwell->format->BytesPerPixel]; - if (a == 255) + rh = sRGB_to_linear_table[color_hexes[NUM_COLORS - 1][0]]; + gh = sRGB_to_linear_table[color_hexes[NUM_COLORS - 1][1]]; + bh = sRGB_to_linear_table[color_hexes[NUM_COLORS - 1][2]]; + SDL_BlitSurface(tmp_btn_down, NULL, img_color_btns[NUM_COLORS - 1], NULL); + SDL_BlitSurface(tmp_btn_up, NULL, img_color_btns[NUM_COLORS - 1 + NUM_COLORS], NULL); + + SDL_LockSurface(img_color_btns[NUM_COLORS - 1]); + SDL_LockSurface(img_color_btns[NUM_COLORS - 1 + NUM_COLORS]); + + for (y = 0; y < tmp_btn_up->h /* 48 */ ; y++) { - putpixels[img_color_btns[NUM_COLORS - 1]->format->BytesPerPixel] - (img_color_btns[NUM_COLORS - 1], 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)))); + for (x = 0; x < tmp_btn_up->w; x++) + { + double ru, gu, bu, rd, gd, bd, aa; + Uint8 a; - putpixels[img_color_btns[NUM_COLORS - 1 + NUM_COLORS]->format->BytesPerPixel] - (img_color_btns[NUM_COLORS - 1 + 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)))); + 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; + + putpixels[img_color_btns[NUM_COLORS - 1]->format->BytesPerPixel] + (img_color_btns[NUM_COLORS - 1], x, y, + getpixels[img_color_picker_thumb->format->BytesPerPixel] (img_color_picker_thumb, x, y)); + putpixels[img_color_btns[NUM_COLORS - 1 + NUM_COLORS]->format->BytesPerPixel] + (img_color_btns[NUM_COLORS - 1 + NUM_COLORS], x, y, + getpixels[img_color_picker_thumb->format->BytesPerPixel] (img_color_picker_thumb, x, y)); + + if (a == 255) + { + putpixels[img_color_btns[NUM_COLORS - 1]->format->BytesPerPixel] + (img_color_btns[NUM_COLORS - 1], 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[NUM_COLORS - 1 + NUM_COLORS]->format->BytesPerPixel] + (img_color_btns[NUM_COLORS - 1 + 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)))); + } + } } - } - } - SDL_UnlockSurface(img_color_btns[NUM_COLORS - 1]); - SDL_UnlockSurface(img_color_btns[NUM_COLORS - 1 + NUM_COLORS]); - } + SDL_UnlockSurface(img_color_btns[NUM_COLORS - 1]); + SDL_UnlockSurface(img_color_btns[NUM_COLORS - 1 + NUM_COLORS]); + } /* Remove the prompt: */ @@ -20917,18 +20546,18 @@ static int do_color_picker(void) update_canvas(0, 0, canvas->w, canvas->h); - return(chose); + return (chose); } static void magic_putpixel(SDL_Surface * surface, int x, int y, Uint32 pixel) { - putpixels[surface->format->BytesPerPixel](surface, x, y, pixel); + putpixels[surface->format->BytesPerPixel] (surface, x, y, pixel); } static Uint32 magic_getpixel(SDL_Surface * surface, int x, int y) { - return(getpixels[surface->format->BytesPerPixel](surface, x, y)); + return (getpixels[surface->format->BytesPerPixel] (surface, x, y)); } @@ -20936,47 +20565,45 @@ 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; - } + 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; + } if (cur_tool == TOOL_MAGIC) - { - magic_funcs[magics[cur_magic].handle_idx].switchout(magic_api_struct, - magics[cur_magic].idx, - magics[cur_magic].mode, - canvas, last); - update_canvas(0, 0, canvas->w, canvas->h); + { + magic_funcs[magics[cur_magic].handle_idx].switchout(magic_api_struct, + magics[cur_magic].idx, magics[cur_magic].mode, canvas, last); + update_canvas(0, 0, canvas->w, canvas->h); - if (was_clicking && magics[cur_magic].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); + if (was_clicking && magics[cur_magic].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); + } } - } } static void magic_switchin(SDL_Surface * last) { if (cur_tool == TOOL_MAGIC) - { - magic_funcs[magics[cur_magic].handle_idx].switchin(magic_api_struct, - magics[cur_magic].idx, - magics[cur_magic].mode, - canvas, last); + { + magic_funcs[magics[cur_magic].handle_idx].switchin(magic_api_struct, + magics[cur_magic].idx, magics[cur_magic].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); + } } static int magic_modeint(int mode) @@ -20989,193 +20616,191 @@ static int magic_modeint(int mode) return 0; } -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; + struct label_node *new_node = malloc(sizeof(struct label_node)); + struct label_node *aux_node; unsigned int i = 0; - 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_color.r = color_hexes[cur_color][0]; - new_node->save_color.g = color_hexes[cur_color][1]; - new_node->save_color.b = color_hexes[cur_color][2]; - new_node->save_width = w; - new_node->save_height = h; - new_node->save_x = x; - new_node->save_y = y; - new_node->save_cur_font = cur_font; - new_node->save_text_state = text_state; - new_node->save_text_size = text_size; - new_node->save_undoid = 255; - if (texttool_len > 0) - { - new_node->is_enabled=TRUE; - } - else - { - new_node->is_enabled = FALSE; - } + 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_color.r = color_hexes[cur_color][0]; + new_node->save_color.g = color_hexes[cur_color][1]; + new_node->save_color.b = color_hexes[cur_color][2]; + new_node->save_width = w; + new_node->save_height = h; + new_node->save_x = x; + new_node->save_y = y; + new_node->save_cur_font = cur_font; + new_node->save_text_state = text_state; + new_node->save_text_size = text_size; + new_node->save_undoid = 255; - new_node->save_font_type = NULL; + if (texttool_len > 0) + { + new_node->is_enabled = TRUE; + } + else + { + new_node->is_enabled = FALSE; + } - if (label_node_to_edit) - { - new_node->disables = label_node_to_edit; - } - else - new_node->disables = NULL; + new_node->save_font_type = NULL; - if (label_node_surface != NULL) - { - new_node->label_node_surface = label_node_surface; - new_node->label_node_surface->refcount++; - } - else new_node->label_node_surface = NULL; + if (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++; + } + else + new_node->label_node_surface = NULL; - new_node->next_to_up_label_node=0; - - 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; - } - - current_label_node = new_node; + new_node->next_to_up_label_node = 0; - if (start_label_node == NULL) start_label_node = current_label_node; + 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; + } - highlighted_label_node = new_node; - if(highlighted_label_node->is_enabled == FALSE) - cycle_highlighted_label_node(); + current_label_node = new_node; + + if (start_label_node == NULL) + start_label_node = current_label_node; + + highlighted_label_node = new_node; + if (highlighted_label_node->is_enabled == FALSE) + cycle_highlighted_label_node(); } -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; + struct label_node *current_node; + struct label_node *tmp_node = NULL; unsigned u; int done = FALSE; Uint8 r, g, b, a; - int i,j, k; + int i, j, k; if (*ref_head == NULL) - return(NULL); + return (NULL); - current_node = *ref_head; - - while(done != TRUE) + current_node = *ref_head; + + while (done != TRUE) { - 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)+(current_node->save_height)) - { - if (current_node->is_enabled == TRUE) - { - if (hover == 1) - return(current_node); - tmp_node = current_node; - done = TRUE; - } - } - } - } - } + 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) + (current_node->save_height)) + { + if (current_node->is_enabled == 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; + current_node = current_label_node; if (current_node == *ref_head) - done = TRUE; + done = TRUE; } if (tmp_node != NULL) - { - select_texttool_len = tmp_node->save_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; - } + u = 0; + while (u < select_texttool_len) + { + select_texttool_str[u] = tmp_node->save_texttool_str[u]; + u = u + 1; + } - 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 < NUM_COLORS - 1)) - { - select_color = k; - cur_color = k; - break; - } - - if (k == NUM_COLORS - 1) - { - cur_color = NUM_COLORS - 1; - select_color = NUM_COLORS - 1; - 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[NUM_COLORS - 1]); - SDL_LockSurface(img_color_btns[NUM_COLORS - 1 + 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[NUM_COLORS - 1]->format->BytesPerPixel] - (img_color_btns[NUM_COLORS - 1 ], i, j, - SDL_MapRGB(img_color_btns[NUM_COLORS - 1]->format, - tmp_node->save_color.r, - tmp_node->save_color.g, - tmp_node->save_color.b)); - putpixels[img_color_btns[NUM_COLORS - 1 + NUM_COLORS]->format->BytesPerPixel] - (img_color_btns[NUM_COLORS - 1 + NUM_COLORS], i, j, - SDL_MapRGB(img_color_btns[NUM_COLORS - 1 + NUM_COLORS]->format, - tmp_node->save_color.r, - tmp_node->save_color.g, - tmp_node->save_color.b)); - } - } - } - SDL_UnlockSurface(img_color_btns[NUM_COLORS - 1]); - SDL_UnlockSurface(img_color_btns[NUM_COLORS - 1 + NUM_COLORS]); + 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 < NUM_COLORS - 1)) + { + select_color = k; + cur_color = k; + break; + } - 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; + if (k == NUM_COLORS - 1) + { + cur_color = NUM_COLORS - 1; + select_color = NUM_COLORS - 1; + 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[NUM_COLORS - 1]); + SDL_LockSurface(img_color_btns[NUM_COLORS - 1 + 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[NUM_COLORS - 1]->format->BytesPerPixel] + (img_color_btns[NUM_COLORS - 1], i, j, + SDL_MapRGB(img_color_btns[NUM_COLORS - 1]->format, + tmp_node->save_color.r, tmp_node->save_color.g, tmp_node->save_color.b)); + putpixels[img_color_btns[NUM_COLORS - 1 + NUM_COLORS]->format->BytesPerPixel] + (img_color_btns[NUM_COLORS - 1 + NUM_COLORS], i, j, + SDL_MapRGB(img_color_btns[NUM_COLORS - 1 + NUM_COLORS]->format, + tmp_node->save_color.r, tmp_node->save_color.g, tmp_node->save_color.b)); + } + } + } + SDL_UnlockSurface(img_color_btns[NUM_COLORS - 1]); + SDL_UnlockSurface(img_color_btns[NUM_COLORS - 1 + 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 tmp_node; - } - return NULL; } @@ -21183,13 +20808,13 @@ 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 + if (coming_from_undo_or_redo) // yet recorded, avoiding to write text_undo { - coming_from_undo_or_redo=FALSE; + coming_from_undo_or_redo = FALSE; return; } @@ -21208,13 +20833,13 @@ static void rec_undo_label(void) 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; + } } static void do_undo_label_node() @@ -21222,77 +20847,77 @@ 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->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->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; - 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; - } + derender_node(&first_label_node_in_redo_stack); + coming_from_undo_or_redo = TRUE; + } } highlighted_label_node = current_label_node; } static void do_redo_label_node() { - if ( (text_undo[cur_undo] == 1) && - (first_label_node_in_redo_stack != NULL) ) + if ((text_undo[cur_undo] == 1) && (first_label_node_in_redo_stack != NULL)) { 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; - - if (start_label_node == NULL) - start_label_node = current_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; 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); + 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; + } } } -static void simply_render_node(struct label_node* node) +static void simply_render_node(struct label_node *node) { SDL_Surface *tmp_surf; SDL_Rect dest, src; wchar_t *str; wchar_t tmp_str[256]; - int j,w; + int j, w; 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) - { - tmp_str[i] = node->save_texttool_str[i]; - i = i+1; - } + 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); @@ -21301,73 +20926,72 @@ static void simply_render_node(struct label_node* node) 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; + 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 (tmp_surf != NULL) + + node->label_node_surface = tmp_surf; } - if (node->label_node_surface != NULL) - { - w = node->label_node_surface->w; + if (node->label_node_surface != NULL) + { + 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 - 96 - 96) - src.w = WINDOW_WIDTH - 96 - 96 - dest.x; - if (dest.y + src.h > (48 * 7 + 40 + HEIGHTOFFSET)) - src.h = (48 * 7 + 40 + HEIGHTOFFSET) - dest.y; + if (dest.x + src.w > WINDOW_WIDTH - 96 - 96) + src.w = WINDOW_WIDTH - 96 - 96 - dest.x; + if (dest.y + src.h > (48 * 7 + 40 + HEIGHTOFFSET)) + src.h = (48 * 7 + 40 + HEIGHTOFFSET) - dest.y; - 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); + myblit(node->label_node_surface, &src, label, &dest); - /* Setting the sizes correctly */ - node->save_width = node->label_node_surface->w; - node->save_height = 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; + } } -static void render_all_nodes_starting_at(struct label_node** node) +static void render_all_nodes_starting_at(struct label_node **node) { - struct label_node* current_node; - if (*node!=NULL) + struct label_node *current_node; + + if (*node != NULL) { - 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; - } + 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; + } } } /* FIXME: This should search for the top-down of the overlaping labels and only re-render from it */ -static void derender_node(struct label_node** ref_head) +static void derender_node(struct label_node **ref_head) { SDL_Rect r_tmp_derender; @@ -21381,18 +21005,18 @@ static void derender_node(struct label_node** ref_head) render_all_nodes_starting_at(&start_label_node); } -static void delete_label_list(struct label_node** ref_head) +static void delete_label_list(struct label_node **ref_head) { - struct label_node* current = *ref_head; - struct label_node* next; + struct label_node *current = *ref_head; + struct label_node *next; - while(current != NULL) + while (current != NULL) { fflush(stdout); next = current->next_to_up_label_node; if (current->label_node_surface) - SDL_FreeSurface(current->label_node_surface); + SDL_FreeSurface(current->label_node_surface); free(current); current = next; } @@ -21402,504 +21026,496 @@ 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; Uint8 dest_r, dest_g, dest_b, dest_a; - - for (x = src_rect->x; xw + src_rect->x; x++) - for (y = src_rect->y; yh + src_rect->y; y++) + + 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) - { - 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)); - } - } - } + 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 + { + 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)); + } + } + } } -} +} static void load_info_about_label_surface(FILE * lfi) { - struct label_node* new_node; - int list_ctr; - int tmp_scale_w; - int tmp_scale_h; - SDL_Surface *label_node_surface, *label_node_surface_aux; - float new_text_size; + struct label_node *new_node; + int list_ctr; + int tmp_scale_w; + int tmp_scale_h; + SDL_Surface *label_node_surface, *label_node_surface_aux; + float new_text_size; - int k; - unsigned l; - unsigned tmp_pos; - wchar_t tmp_char; - int old_width; - int old_height; - int new_width; - int new_height; - float new_ratio; - float old_ratio; - float new_to_old_ratio; - int old_pos; - int new_pos; - int x, y, pix_size; - Uint8 a; - - /* Clear label surface */ + int k; + unsigned l; + unsigned tmp_pos; + wchar_t tmp_char; + int old_width; + int old_height; + int new_width; + int new_height; + float new_ratio; + float old_ratio; + float new_to_old_ratio; + int old_pos; + int new_pos; + int x, y, pix_size; + Uint8 a; - SDL_FillRect(label, NULL, SDL_MapRGBA(label->format, 0, 0, 0, 0)); + /* Clear label surface */ - /* Clear all info related to label surface */ + SDL_FillRect(label, NULL, SDL_MapRGBA(label->format, 0, 0, 0, 0)); - 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; + /* 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; // lfi = fopen(lfname, "r"); - if (lfi == NULL) - return; - fscanf(lfi, "%d\n", &list_ctr); - fscanf(lfi, "%d\n", &tmp_scale_w); - fscanf(lfi, "%d\n\n", &tmp_scale_h); + if (lfi == NULL) + return; + fscanf(lfi, "%d\n", &list_ctr); + fscanf(lfi, "%d\n", &tmp_scale_w); + fscanf(lfi, "%d\n\n", &tmp_scale_h); - 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; - if (new_ratio < old_ratio) - new_to_old_ratio = (float)new_width / old_width; - else - new_to_old_ratio = (float)new_height / old_height; + 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; + if (new_ratio < old_ratio) + new_to_old_ratio = (float)new_width / old_width; + else + new_to_old_ratio = (float)new_height / old_height; - for(k = 0; k < list_ctr; k++) - { - new_node = malloc(sizeof(struct label_node)); + for (k = 0; k < list_ctr; k++) + { + new_node = malloc(sizeof(struct label_node)); - fscanf(lfi , "%u\n", &new_node->save_texttool_len); + fscanf(lfi, "%u\n", &new_node->save_texttool_len); #ifdef WIN32 - char *tmpstr; - wchar_t *wtmpstr; - tmpstr = malloc(1024); - wtmpstr = malloc(1024); - fgets(tmpstr, 1024, lfi); - mtw(wtmpstr, tmpstr); - for(l = 0; l < new_node->save_texttool_len; l++) - { - new_node->save_texttool_str[l] = wtmpstr[l]; - } + char *tmpstr; + wchar_t *wtmpstr; + + tmpstr = malloc(1024); + wtmpstr = malloc(1024); + fgets(tmpstr, 1024, lfi); + mtw(wtmpstr, tmpstr); + for (l = 0; l < new_node->save_texttool_len; l++) + { + new_node->save_texttool_str[l] = wtmpstr[l]; + } #elif defined(__ANDROID__) - 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, "\n"); -#else - for(l = 0; l < new_node->save_texttool_len; l++) - { - fscanf(lfi, "%lc", &tmp_char); - new_node->save_texttool_str[l] = tmp_char; - } - fscanf(lfi, "\n"); -#endif - fscanf(lfi, "%u\n", &l); - new_node->save_color.r = (Uint8) l; - fscanf(lfi, "%u\n", &l); - new_node->save_color.g = (Uint8) l; - fscanf(lfi, "%u\n", &l); - new_node->save_color.b = (Uint8) l; - fscanf(lfi, "%d\n", &new_node->save_width); - fscanf(lfi, "%d\n", &new_node->save_height); - 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; - 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; - 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; - } - - printf("Original label size %dx%d\n", new_node->save_width, new_node->save_height); - - fscanf(lfi, "%d\n", &new_node->save_cur_font); - new_node->save_cur_font = 0; - - new_node->save_font_type = malloc(64); - fgets(new_node->save_font_type, 64, lfi); - - fscanf(lfi, "%d\n", &new_node->save_text_state); - fscanf(lfi, "%u\n", &new_node->save_text_size); - - 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;xsave_width;x++) - for (y=0;ysave_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); - - new_text_size = (float)new_node->save_text_size * new_to_old_ratio; - 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; - fscanf(lfi, "\n"); - - 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); - + for (l = 0; l < new_node->save_texttool_len; l++) + { + fscanf(lfi, "%d ", &tmp_char); + new_node->save_texttool_str[l] = tmp_char; } - first_label_node_in_redo_stack = NULL; - fclose(lfi); + fscanf(lfi, "\n"); +#else + for (l = 0; l < new_node->save_texttool_len; l++) + { + fscanf(lfi, "%lc", &tmp_char); + new_node->save_texttool_str[l] = tmp_char; + } + fscanf(lfi, "\n"); +#endif + fscanf(lfi, "%u\n", &l); + new_node->save_color.r = (Uint8) l; + fscanf(lfi, "%u\n", &l); + new_node->save_color.g = (Uint8) l; + fscanf(lfi, "%u\n", &l); + new_node->save_color.b = (Uint8) l; + fscanf(lfi, "%d\n", &new_node->save_width); + fscanf(lfi, "%d\n", &new_node->save_height); + fscanf(lfi, "%d\n", &tmp_pos); + old_pos = (int)tmp_pos; - if (font_thread_done) - set_label_fonts(); + if (new_ratio < old_ratio) + { + new_pos = (old_pos * new_to_old_ratio); + tmp_pos = new_pos; + new_node->save_x = tmp_pos; + 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; + 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; + } + + printf("Original label size %dx%d\n", new_node->save_width, new_node->save_height); + + fscanf(lfi, "%d\n", &new_node->save_cur_font); + new_node->save_cur_font = 0; + + new_node->save_font_type = malloc(64); + fgets(new_node->save_font_type, 64, lfi); + + fscanf(lfi, "%d\n", &new_node->save_text_state); + fscanf(lfi, "%u\n", &new_node->save_text_size); + + 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); + + new_text_size = (float)new_node->save_text_size * new_to_old_ratio; + 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; + fscanf(lfi, "\n"); + + 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); + + if (font_thread_done) + set_label_fonts(); } static void set_label_fonts() { - struct label_node* node; - int i; - char * ttffont; - - node = current_label_node; - while (node != NULL) + struct label_node *node; + int i; + char *ttffont; + 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; - /* 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'; + 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'; #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; + + } } static void tmp_apply_uncommited_text() { - have_to_rec_label_node_back = have_to_rec_label_node; - - if (texttool_len > 0) + 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 && 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; - - derender_node(&label_node_to_edit); - } + + 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; + + derender_node(&label_node_to_edit); + } } static void undo_tmp_applied_text() { - struct label_node* aux_label_node; + struct label_node *aux_label_node; - if (texttool_len > 0) + 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); - } + 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 && 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; + 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); - } + 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); + } } /* Painting on the screen surface to avoid unnecessary complexity */ static void highlight_label_nodes() { - int j; - SDL_Rect rect, rect1; - struct label_node* aux_node; + int j; + SDL_Rect rect, rect1; + struct label_node *aux_node; - if (highlighted_label_node != NULL) + 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; - - - while (aux_node != highlighted_label_node) - { - if (aux_node->is_enabled) + 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++) { - 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)); + 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)); - 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); - } - + 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 = 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)); + 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; - 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)); + 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_BlitSurface(aux_node->label_node_surface, NULL, screen, &rect); } - SDL_Flip(screen); -} + SDL_Flip(screen); + } } 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; + 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 = 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; + 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; - } - } - + highlighted_label_node = aux_node; + } + } + } static int are_labels() { - struct label_node* aux_node; - if (current_label_node) + 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); + } + return (FALSE); } int chunk_is_valid(const char *chunk_name, png_unknown_chunk unknown) @@ -21920,58 +21536,58 @@ int chunk_is_valid(const char *chunk_name, png_unknown_chunk unknown) 'p' == unknown.data[3] && 'a' == unknown.data[4] && '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) { - 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; - } - 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); + /* 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; + } + 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++; + new_field = 0; + } + count++; + } } - } return (FALSE); } 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; @@ -21982,56 +21598,56 @@ Bytef *get_chunk_data(FILE * fp, char *fname, png_structp png_ptr, z_streamp zstp; control = malloc(50); - softwr = malloc(50); - sscanf((char *) unknown.data, "%s\n%s\n%d\n%d\n", control, softwr, unc_size, &comp); + softwr = malloc(50); + 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) { - comp_buff[i - count] = unknown.data[i]; - // printf("%d, %d, %d ",i-count, comp_buff[i - count], unknown.data[i]); - } + 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; + 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); */ @@ -22040,7 +21656,7 @@ Bytef *get_chunk_data(FILE * fp, char *fname, png_structp png_ptr, zstp->avail_in = comp; zstp->total_in = comp; - zstp->next_out =unc_buff; + zstp->next_out = unc_buff; zstp->avail_out = *unc_size; zstp->total_out = 0; @@ -22053,17 +21669,17 @@ Bytef *get_chunk_data(FILE * fp, char *fname, png_structp png_ptr, inflateEnd(zstp); if (unc_err != Z_STREAM_END) - { - printf("\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); + { + printf("\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); - printf("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); - } + printf("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); @@ -22095,322 +21711,321 @@ 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 - { - printf("%s\n", fname); - - info_ptr = png_create_info_struct(png_ptr); - if (info_ptr == NULL) { fclose(fp); - png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); + 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; } - - 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); - - printf("num_unknowns %i\n", num_unknowns); - if (num_unknowns) + else { - have_label_delta = have_label_data = have_background = have_foreground = FALSE; - ldelta = ldata = fgnd = bgnd = FALSE; + printf("%s\n", fname); + + info_ptr = png_create_info_struct(png_ptr); + if (info_ptr == NULL) + { + 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); + + printf("num_unknowns %i\n", num_unknowns); + 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++) - { - printf("%s, %d\n", unknowns[u].name, unknowns[u].size); + for (u = 0; u < num_unknowns; u++) + { + printf("%s, %d\n", unknowns[u].name, unknowns[u].size); - if (chunk_is_valid("tpDT", unknowns[u])) - { - printf("Valid tpDT\n"); - 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); + if (chunk_is_valid("tpDT", unknowns[u])) + { + printf("Valid tpDT\n"); + 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 */ - } + 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); - fgets(control, 49, fi); - fgets(control, 49, fi); - fgets(control, 49, fi); - fgets(control, 49, fi); - free(control); + control = malloc(50); + fgets(control, 49, fi); + fgets(control, 49, fi); + fgets(control, 49, fi); + fgets(control, 49, fi); + free(control); - load_starter_id(NULL, fi); // fi will be closed in load_starter_id() - if (!starter_modified) - { - /* Code adapted from load_current() */ - if (starter_id[0] != '\0') - { - load_starter(starter_id); + load_starter_id(NULL, fi); // fi will be closed in load_starter_id() + 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_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; - } + 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])) - { - printf("Valid tpLD\n"); + /* 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])) + { + printf("Valid tpLD\n"); - 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; - } + 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_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); + SDL_UnlockSurface(org_surf); - free(unc_buff); - ldelta = TRUE; - } + free(unc_buff); + ldelta = TRUE; + } - /* Label Data */ - if (!disable_label && chunk_is_valid("tpLL", unknowns[u])) - { - printf("Valid tpLL\n"); + /* Label Data */ + if (!disable_label && chunk_is_valid("tpLL", unknowns[u])) + { + printf("Valid tpLL\n"); - 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) - { - printf("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); + 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) + { + printf("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); - } + draw_tux_text(TUX_OOPS, strerror(errno), 0); + return; + } + else + load_info_about_label_surface(fi); + } - free(unc_buff); - ldata = TRUE; - printf("Out of label data\n"); - } - } - } - /* Apply the original canvas */ - if (ldelta && ldata) - autoscale_copy_smear_free(org_surf, canvas, SDL_BlitSurface); - else - SDL_FreeSurface(org_surf); + free(unc_buff); + ldata = TRUE; + printf("Out of label data\n"); + } + } + } + /* 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) - { - printf("Can't recover the background 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); + /* 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) + { + printf("Can't recover the background 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); + draw_tux_text(TUX_OOPS, strerror(errno), 0); - free(unc_buff); - return; - } - SDL_LockSurface(aux_surf); + free(unc_buff); + return; + } + SDL_LockSurface(aux_surf); - printf("Bkgd!!!\n"); - 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); + printf("Bkgd!!!\n"); + 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 (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); + 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); - } + 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])) - { - printf("Frgd!!!\n"); + if ((starter_modified || !img_starter) && chunk_is_valid("tpFG", unknowns[u])) + { + printf("Frgd!!!\n"); - unc_buff = get_chunk_data(fp, fname, png_ptr, info_ptr, "tpFG", unknowns[u], &unc_size); - if (unc_buff == NULL) - return; + 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) - { - printf("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); + 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) + { + printf("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); + draw_tux_text(TUX_OOPS, strerror(errno), 0); - free(unc_buff); - return; - } + 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); + 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); + 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); + /* 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); + /* 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); - } - } - } + free(unc_buff); + } + } + } + } } - } png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); fclose(fp); @@ -22423,7 +22038,7 @@ void load_embedded_data(char *fname, SDL_Surface * org_surf) static void show_available_papersizes(int exitcode) { FILE *fi = exitcode ? stderr : stdout; - const struct paper * ppr; + const struct paper *ppr; int cnt; fprintf(fi, "Usage: %s [--papersize PAPERSIZE]\n", progname); @@ -22434,17 +22049,17 @@ static void show_available_papersizes(int exitcode) cnt = 0; while (ppr != NULL) - { - fprintf(fi, "\t%s", papername(ppr)); - cnt++; - if (cnt == 5) { - cnt = 0; - fprintf(fi, "\n"); - } + fprintf(fi, "\t%s", papername(ppr)); + cnt++; + if (cnt == 5) + { + cnt = 0; + fprintf(fi, "\n"); + } - ppr = papernext(ppr); - } + ppr = papernext(ppr); + } fprintf(fi, "\n"); if (cnt != 0) @@ -22457,142 +22072,146 @@ static void show_available_papersizes(int exitcode) static void parse_file_options(struct cfginfo *restrict tmpcfg, const char *filename) { char str[256]; - char* arg; + char *arg; FILE *fi = fopen(filename, "r"); - if(!fi) + + if (!fi) return; - while(fgets(str, sizeof(str), fi)) - { - if(!isalnum(*str)) - continue; - strip_trailing_whitespace(str); - arg = strchr(str,'='); - if(arg) - *arg++ = '\0'; + while (fgets(str, sizeof(str), fi)) + { + if (!isalnum(*str)) + continue; + strip_trailing_whitespace(str); + arg = strchr(str, '='); + if (arg) + *arg++ = '\0'; #ifdef __linux__ - /* Perform shell expansion */ - wordexp_t result; + /* Perform shell expansion */ + wordexp_t result; - wordexp(arg, &result, 0); - arg = strdup(result.we_wordv[0]); - wordfree(&result); + wordexp(arg, &result, 0); + arg = strdup(result.we_wordv[0]); + wordfree(&result); #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); + // 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__ - free(arg); + free(arg); #endif - } + } fclose(fi); // These interact in horrid ways. - if(tmpcfg->parsertmp_lang && tmpcfg->parsertmp_locale) - fprintf( - stderr, - "Warning: option 'lang=%s' overrides option 'locale=%s' in '%s'\n", - tmpcfg->parsertmp_lang, - tmpcfg->parsertmp_locale, - filename - ); - if(tmpcfg->parsertmp_lang) + if (tmpcfg->parsertmp_lang && tmpcfg->parsertmp_locale) + fprintf(stderr, + "Warning: option 'lang=%s' overrides option 'locale=%s' in '%s'\n", + tmpcfg->parsertmp_lang, tmpcfg->parsertmp_locale, filename); + if (tmpcfg->parsertmp_lang) tmpcfg->parsertmp_locale = PARSE_CLOBBER; - else if(tmpcfg->parsertmp_locale) + else if (tmpcfg->parsertmp_locale) tmpcfg->parsertmp_lang = PARSE_CLOBBER; } static void parse_argv_options(struct cfginfo *restrict tmpcfg, char *argv[]) { char *str, *arg; - const char * short_synonyms[][2] = { - {"-c", "copying"}, - {"-h", "help"}, - {"-u", "usage"}, - {"-v", "version"}, + + const char *short_synonyms[][2] = { + {"-c", "copying"}, + {"-h", "help"}, + {"-u", "usage"}, + {"-v", "version"}, {"-vv", "verbose-version"}, - {"-l", "lang"}, - {"-L", "locale"}, - {"-b", "startblank"}, - {"-f", "fullscreen"}, - {"-m", "mixedcase"}, - {"-p", "noprint"}, - {"-q", "nosound"}, - {"-s", "simpleshapes"}, - {"-w", "windowed"}, - {"-x", "noquit"}, - {NULL, NULL} + {"-l", "lang"}, + {"-L", "locale"}, + {"-b", "startblank"}, + {"-f", "fullscreen"}, + {"-m", "mixedcase"}, + {"-p", "noprint"}, + {"-q", "nosound"}, + {"-s", "simpleshapes"}, + {"-w", "windowed"}, + {"-x", "noquit"}, + {NULL, NULL} }; - while(( str = *++argv )) - { - if(str[0]=='-' && str[1]!='-' && str[1]!='\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; + while ((str = *++argv)) + { + if (str[0] == '-' && str[1] != '-' && str[1] != '\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; } - } - 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; + 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); } - 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); - } - if(tmpcfg->parsertmp_lang) + 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); + } + if (tmpcfg->parsertmp_lang) tmpcfg->parsertmp_locale = PARSE_CLOBBER; - else if(tmpcfg->parsertmp_locale) + else if (tmpcfg->parsertmp_locale) tmpcfg->parsertmp_lang = PARSE_CLOBBER; } // merge two configs, with the winner taking priority static void tmpcfg_merge(struct cfginfo *loser, const struct cfginfo *winner) { - int i = CFGINFO_MAXOFFSET / sizeof(char*); - while(i--) - { - 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); - } + int i = CFGINFO_MAXOFFSET / sizeof(char *); + + while (i--) + { + 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); + } } static void setup_config(char *argv[]) { char str[128]; + #if !defined(_WIN32) && !defined(__ANDROID__) const char *home = getenv("HOME"); #endif @@ -22607,54 +22226,56 @@ static void setup_config(char *argv[]) parse_argv_options(&tmpcfg_cmd, argv); -#if defined(__APPLE__) //EP added this conditional section for Mac to allow for a config in the current directory, that supersedes sys and user configs - /* Mac OS X: Use a "tuxpaint.cfg" file in the current folder */ - struct cfginfo tmpcfg_curdir; - memset(&tmpcfg_curdir, '\0', sizeof tmpcfg_curdir); - parse_file_options(&tmpcfg_curdir, "./tuxpaint.cfg"); - tmpcfg_merge(&tmpcfg_curdir, &tmpcfg_cmd); +#if defined(__APPLE__) //EP added this conditional section for Mac to allow for a config in the current directory, that supersedes sys and user configs + /* Mac OS X: Use a "tuxpaint.cfg" file in the current folder */ + struct cfginfo tmpcfg_curdir; + + memset(&tmpcfg_curdir, '\0', sizeof tmpcfg_curdir); + parse_file_options(&tmpcfg_curdir, "./tuxpaint.cfg"); + tmpcfg_merge(&tmpcfg_curdir, &tmpcfg_cmd); #endif - - /* Set default options: */ + + /* Set default options: */ #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); - } + if (!home) + { + /* Woah, don't know where $HOME is? */ + fprintf(stderr, "Error: You have no $HOME environment variable!\n"); + exit(1); + } #endif - if(tmpcfg_cmd.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; - result = find_directory(B_USER_DIRECTORY, volume, false, buffer, sizeof(buffer)); - asprintf((char**)&savedir, "%s/%s", buffer, "TuxPaint"); + /* 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_DIRECTORY, volume, false, buffer, sizeof(buffer)); + asprintf((char **)&savedir, "%s/%s", buffer, "TuxPaint"); #elif __APPLE__ - savedir = strdup(macosx.preferencesPath); + savedir = strdup(macosx.preferencesPath); #elif __ANDROID__ - savedir = SDL_AndroidGetExternalStoragePath(); + savedir = SDL_AndroidGetExternalStoragePath(); #else - asprintf((char**)&savedir, "%s/%s", home, ".tuxpaint"); + asprintf((char **)&savedir, "%s/%s", home, ".tuxpaint"); #endif - } + } /* Load options from user's own configuration (".rc" / ".cfg") file: */ #if defined(_WIN32) /* Default local config file in users savedir directory on Windows */ - snprintf(str, sizeof(str), "%s/tuxpaint.cfg", savedir); /* FIXME */ + snprintf(str, sizeof(str), "%s/tuxpaint.cfg", savedir); /* FIXME */ #elif defined(__BEOS__) || defined(__HAIKU__) /* BeOS: Use a "tuxpaint.cfg" file: */ strcpy(str, "tuxpaint.cfg"); @@ -22674,47 +22295,48 @@ static void setup_config(char *argv[]) -#if defined(__APPLE__) //EP added this conditional section for Mac - tmpcfg_merge(&tmpcfg_usr, &tmpcfg_curdir); +#if defined(__APPLE__) //EP added this conditional section for Mac + tmpcfg_merge(&tmpcfg_usr, &tmpcfg_curdir); #else - tmpcfg_merge(&tmpcfg_usr, &tmpcfg_cmd); + tmpcfg_merge(&tmpcfg_usr, &tmpcfg_cmd); #endif if (tmpcfg_usr.parsertmp_sysconfig != PARSE_NO) - { - struct cfginfo tmpcfg_sys; - memset(&tmpcfg_sys, '\0', sizeof tmpcfg_sys); + { + struct cfginfo 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) - /* Mac OS X: Use a "tuxpaint.cfg" file in the *global* Tux Paint application support folder */ - snprintf(str, sizeof(str), "%s/tuxpaint.cfg", macosx.globalPreferencesPath); - parse_file_options(&tmpcfg_sys, str); + /* Mac OS X: Use a "tuxpaint.cfg" file in the *global* Tux Paint application support folder */ + snprintf(str, sizeof(str), "%s/tuxpaint.cfg", macosx.globalPreferencesPath); + parse_file_options(&tmpcfg_sys, str); #elif defined(__ANDROID__) /* Try to find the second config file: /data/data/org.tuxpaint/files/tuxpaint.cfg */ - /* This file is unzipped from "assets/tuxpaint.zip" and will be modified when users want to set differnt configuraion*/ - snprintf(str, sizeof(str), "%s/tuxpaint.cfg", SDL_AndroidGetInternalStoragePath()); - parse_file_options(&tmpcfg_sys, str); + /* This file is unzipped from "assets/tuxpaint.zip" and will be modified when users want to set differnt configuraion */ + snprintf(str, sizeof(str), "%s/tuxpaint.cfg", SDL_AndroidGetInternalStoragePath()); + 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; - } + + if (tmpcfg.savedir) + { + free((char *)savedir); + savedir = tmpcfg.savedir; + } datadir = tmpcfg.datadir ? tmpcfg.datadir : savedir; - if(tmpcfg.parsertmp_lang == PARSE_CLOBBER) + if (tmpcfg.parsertmp_lang == PARSE_CLOBBER) tmpcfg.parsertmp_lang = NULL; - if(tmpcfg.parsertmp_locale == PARSE_CLOBBER) + if (tmpcfg.parsertmp_locale == PARSE_CLOBBER) tmpcfg.parsertmp_locale = NULL; button_label_y_nudge = setup_i18n(tmpcfg.parsertmp_lang, tmpcfg.parsertmp_locale); @@ -22722,7 +22344,7 @@ static void setup_config(char *argv[]) // FIXME: most of this is not required before starting the font scanner #ifdef PAPER_H - if(tmpcfg_cmd.papersize && !strcmp(tmpcfg_cmd.papersize, "help")) + if (tmpcfg_cmd.papersize && !strcmp(tmpcfg_cmd.papersize, "help")) show_available_papersizes(0); #endif @@ -22763,335 +22385,365 @@ static void setup_config(char *argv[]) SETBOOL(_promptless_save_over_ask); #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); - if (tmpcfg.parsertmp_windowsize==endp1 || endp1+1==endp2 || *endp1!='x' || *endp2) + if (tmpcfg.parsertmp_windowsize) { - fprintf(stderr,"Window size '%s' is not understood.\n",tmpcfg.parsertmp_windowsize); - exit(97); + 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 (w<500 || w>32000 || h<480 || h>32000 || h>w*3 || w>h*4) + if (tmpcfg.parsertmp_fullscreen_native) { - fprintf(stderr,"Window size '%s' is not reasonable.\n",tmpcfg.parsertmp_windowsize); - exit(93); + // 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"); } - 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"); - } - if(tmpcfg.stamp_size_override) - { - if (!strcmp(tmpcfg.stamp_size_override, "default")) - stamp_size_override = -1; - else + if (tmpcfg.stamp_size_override) { - // FIXME: needs to be a scaling factor - stamp_size_override = atoi(tmpcfg.stamp_size_override); - if (stamp_size_override > 10) - stamp_size_override = 10; + 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: 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) - strcpy(colorfile, tmpcfg.colorfile); // FIXME can overflow - if(tmpcfg.print_delay) { - print_delay = atoi(tmpcfg.print_delay); - last_print_time = -print_delay; - } + if (tmpcfg.rotate_orientation) + rotate_orientation = !strcmp(tmpcfg.rotate_orientation, "portrait"); // alternative is "landscape" + if (tmpcfg.colorfile) + strcpy(colorfile, tmpcfg.colorfile); // FIXME can overflow + if (tmpcfg.print_delay) + { + print_delay = atoi(tmpcfg.print_delay); + last_print_time = -print_delay; + } #ifdef PAPER_H - if(tmpcfg.printcommand) + if (tmpcfg.printcommand) printcommand = tmpcfg.printcommand; - if(tmpcfg.altprintcommand) + if (tmpcfg.altprintcommand) 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") - } + 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") + } #ifdef PAPER_H - if(tmpcfg.papersize) + if (tmpcfg.papersize) papersize = tmpcfg.papersize; #endif - if(tmpcfg.joystick_dev) + if (tmpcfg.joystick_dev) { - if(strcmp(tmpcfg.joystick_dev, "list") == 0) { - joystick_dev = -1; - } else { - if(strtof(tmpcfg.joystick_dev, NULL) < 0 || strtof(tmpcfg.joystick_dev, NULL) > 100) - { - printf("Joystick dev (now %s) must be between 0 and 100.\n", tmpcfg.joystick_dev); - exit(1); - } - joystick_dev = strtof(tmpcfg.joystick_dev, NULL); - } + if (strcmp(tmpcfg.joystick_dev, "list") == 0) + { + joystick_dev = -1; + } + else + { + if (strtof(tmpcfg.joystick_dev, NULL) < 0 || strtof(tmpcfg.joystick_dev, NULL) > 100) + { + printf("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 (tmpcfg.joystick_slowness) { - if(strtof(tmpcfg.joystick_slowness, NULL) < 0 || strtof(tmpcfg.joystick_slowness, NULL) > 500) - { - printf("Joystick slowness (now %s) must be between 0 and 500.\n", tmpcfg.joystick_slowness); - exit(1); - } + if (strtof(tmpcfg.joystick_slowness, NULL) < 0 || strtof(tmpcfg.joystick_slowness, NULL) > 500) + { + printf("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 (tmpcfg.joystick_lowthreshold) { if (strtof(tmpcfg.joystick_lowthreshold, NULL) < 0 || strtof(tmpcfg.joystick_lowthreshold, NULL) > 32766) - { - /* FIXME: Find better exit code */ - printf("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 */ + printf("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 (tmpcfg.joystick_maxsteps) { if (strtof(tmpcfg.joystick_maxsteps, NULL) < 1 || strtof(tmpcfg.joystick_maxsteps, NULL) > 7) - { - /* FIXME: Find better exit code */ - printf("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 */ + printf("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 (tmpcfg.joystick_hat_slowness) { - if(strtof(tmpcfg.joystick_hat_slowness, NULL) < 0 || strtof(tmpcfg.joystick_hat_slowness, NULL) > 500) - { - printf("Joystick hat slowness (now %s) must be between 0 and 500.\n", tmpcfg.joystick_hat_slowness); - exit(1); - } + if (strtof(tmpcfg.joystick_hat_slowness, NULL) < 0 || strtof(tmpcfg.joystick_hat_slowness, NULL) > 500) + { + printf("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 (tmpcfg.joystick_hat_timeout) { if (strtof(tmpcfg.joystick_hat_timeout, NULL) < 0 || strtof(tmpcfg.joystick_hat_timeout, NULL) > 3000) - { - /* FIXME: Find better exit code */ - printf("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 */ + printf("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 (tmpcfg.joystick_button_escape) { if (strtof(tmpcfg.joystick_button_escape, NULL) < 0 || strtof(tmpcfg.joystick_button_escape, NULL) > 254) - { - /* FIXME: Find better exit code */ - printf("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 */ + printf("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 (tmpcfg.joystick_button_selectbrushtool) { - if (strtof(tmpcfg.joystick_button_selectbrushtool, NULL) < 0 || strtof(tmpcfg.joystick_button_selectbrushtool, NULL) > 254) - { - /* FIXME: Find better exit code */ - printf("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 (strtof(tmpcfg.joystick_button_selectbrushtool, NULL) < 0 + || strtof(tmpcfg.joystick_button_selectbrushtool, NULL) > 254) + { + /* FIXME: Find better exit code */ + printf("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 (tmpcfg.joystick_button_selectstamptool) { - if (strtof(tmpcfg.joystick_button_selectstamptool, NULL) < 0 || strtof(tmpcfg.joystick_button_selectstamptool, NULL) > 254) - { - /* FIXME: Find better exit code */ - printf("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 (strtof(tmpcfg.joystick_button_selectstamptool, NULL) < 0 + || strtof(tmpcfg.joystick_button_selectstamptool, NULL) > 254) + { + /* FIXME: Find better exit code */ + printf("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 (tmpcfg.joystick_button_selectlinestool) { - if (strtof(tmpcfg.joystick_button_selectlinestool, NULL) < 0 || strtof(tmpcfg.joystick_button_selectlinestool, NULL) > 254) - { - /* FIXME: Find better exit code */ - printf("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 (strtof(tmpcfg.joystick_button_selectlinestool, NULL) < 0 + || strtof(tmpcfg.joystick_button_selectlinestool, NULL) > 254) + { + /* FIXME: Find better exit code */ + printf("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 (tmpcfg.joystick_button_selectshapestool) { - if (strtof(tmpcfg.joystick_button_selectshapestool, NULL) < 0 || strtof(tmpcfg.joystick_button_selectshapestool, NULL) > 254) - { - /* FIXME: Find better exit code */ - printf("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 (strtof(tmpcfg.joystick_button_selectshapestool, NULL) < 0 + || strtof(tmpcfg.joystick_button_selectshapestool, NULL) > 254) + { + /* FIXME: Find better exit code */ + printf("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 (tmpcfg.joystick_button_selecttexttool) { - if (strtof(tmpcfg.joystick_button_selecttexttool, NULL) < 0 || strtof(tmpcfg.joystick_button_selecttexttool, NULL) > 254) - { - /* FIXME: Find better exit code */ - printf("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 (strtof(tmpcfg.joystick_button_selecttexttool, NULL) < 0 + || strtof(tmpcfg.joystick_button_selecttexttool, NULL) > 254) + { + /* FIXME: Find better exit code */ + printf("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 (tmpcfg.joystick_button_selectlabeltool) { - if (strtof(tmpcfg.joystick_button_selectlabeltool, NULL) < 0 || strtof(tmpcfg.joystick_button_selectlabeltool, NULL) > 254) - { - /* FIXME: Find better exit code */ - printf("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 (strtof(tmpcfg.joystick_button_selectlabeltool, NULL) < 0 + || strtof(tmpcfg.joystick_button_selectlabeltool, NULL) > 254) + { + /* FIXME: Find better exit code */ + printf("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 (tmpcfg.joystick_button_selectmagictool) { - if (strtof(tmpcfg.joystick_button_selectmagictool, NULL) < 0 || strtof(tmpcfg.joystick_button_selectmagictool, NULL) > 254) - { - /* FIXME: Find better exit code */ - printf("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 (strtof(tmpcfg.joystick_button_selectmagictool, NULL) < 0 + || strtof(tmpcfg.joystick_button_selectmagictool, NULL) > 254) + { + /* FIXME: Find better exit code */ + printf("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 (tmpcfg.joystick_button_undo) { if (strtof(tmpcfg.joystick_button_undo, NULL) < 0 || strtof(tmpcfg.joystick_button_undo, NULL) > 254) - { - /* FIXME: Find better exit code */ - printf("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 */ + printf("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 (tmpcfg.joystick_button_redo) { if (strtof(tmpcfg.joystick_button_redo, NULL) < 0 || strtof(tmpcfg.joystick_button_redo, NULL) > 254) - { - /* FIXME: Find better exit code */ - printf("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 */ + printf("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 (tmpcfg.joystick_button_selecterasertool) { - if (strtof(tmpcfg.joystick_button_selecterasertool, NULL) < 0 || strtof(tmpcfg.joystick_button_selecterasertool, NULL) > 254) - { - /* FIXME: Find better exit code */ - printf("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 (strtof(tmpcfg.joystick_button_selecterasertool, NULL) < 0 + || strtof(tmpcfg.joystick_button_selecterasertool, NULL) > 254) + { + /* FIXME: Find better exit code */ + printf("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 (tmpcfg.joystick_button_new) { if (strtof(tmpcfg.joystick_button_new, NULL) < 0 || strtof(tmpcfg.joystick_button_new, NULL) > 254) - { - /* FIXME: Find better exit code */ - printf("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 */ + printf("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 (tmpcfg.joystick_button_open) { if (strtof(tmpcfg.joystick_button_open, NULL) < 0 || strtof(tmpcfg.joystick_button_open, NULL) > 254) - { - /* FIXME: Find better exit code */ - printf("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 */ + printf("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 (tmpcfg.joystick_button_save) { if (strtof(tmpcfg.joystick_button_save, NULL) < 0 || strtof(tmpcfg.joystick_button_save, NULL) > 254) - { - /* FIXME: Find better exit code */ - printf("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 */ + printf("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 (tmpcfg.joystick_button_pagesetup) { if (strtof(tmpcfg.joystick_button_pagesetup, NULL) < 0 || strtof(tmpcfg.joystick_button_pagesetup, NULL) > 254) - { - /* FIXME: Find better exit code */ - printf("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 */ + printf("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 (tmpcfg.joystick_button_print) { if (strtof(tmpcfg.joystick_button_print, NULL) < 0 || strtof(tmpcfg.joystick_button_print, NULL) > 254) - { - /* FIXME: Find better exit code */ - printf("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 */ + printf("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) + if (tmpcfg.joystick_buttons_ignore) { int i; - char * token; + char *token; token = strtok(tmpcfg.joystick_buttons_ignore, ","); - while (token != NULL) { - if (strtof(token, NULL) < 0 || strtof(token, NULL) > 254) { - /* FIXME: Find better exit code */ - printf("Joystick buttons must be between 0 and 254", tmpcfg.joystick_button_print); - exit(1); + while (token != NULL) + { + if (strtof(token, NULL) < 0 || strtof(token, NULL) > 254) + { + /* FIXME: Find better exit code */ + printf("Joystick buttons must be between 0 and 254", tmpcfg.joystick_button_print); + exit(1); + } + joystick_buttons_ignore[joystick_buttons_ignore_len++] = strtof(token, NULL); + token = strtok(NULL, ","); } - 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) + /* 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 = TRUE; } - if(tmpcfg.onscreen_keyboard_disable_change) + if (tmpcfg.onscreen_keyboard_disable_change) { 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, _promptless_save_over); + 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; - } else if (_promptless_save_over_new) { - promptless_save = SAVE_OVER_NO; - } else if (_promptless_save_over) { - promptless_save = SAVE_OVER_ALWAYS; - } + if (_promptless_save_over_ask) + { + promptless_save = SAVE_OVER_PROMPT; + } + else if (_promptless_save_over_new) + { + promptless_save = SAVE_OVER_NO; + } + else if (_promptless_save_over) + { + promptless_save = SAVE_OVER_ALWAYS; + } } @@ -23099,12 +22751,13 @@ static void setup_config(char *argv[]) static void chdir_to_binary(char *argv0) { - char curdir[256]; //EP added this block to print out of current directory - getcwd(curdir, sizeof(curdir)); + char curdir[256]; //EP added this block to print out of current directory + + getcwd(curdir, sizeof(curdir)); #ifdef DEBUG - printf("Binary Path: %s\nCurrent directory at launchtime: %s\n", argv0, curdir); + printf("Binary Path: %s\nCurrent directory at launchtime: %s\n", argv0, curdir); #endif - + #if defined(__BEOS__) || defined(WIN32) || defined(__APPLE__) //EP added __APPLE__ /* if run from gui, like OpenTracker in BeOS or Explorer in Windows, find path from which binary was run and change dir to it @@ -23116,33 +22769,34 @@ 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(__APPLE__) //EP added to fix 10.9 issue of current directory set by Finder to something else than folder where app bundle resides - // typical path of app's binary on Mac OS : /Applications/Tux Paint.app/Contents/MacOS/Tux Paint - int levels = 3; // we need to back up 3 levels - 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... - } +#if defined(__APPLE__) //EP added to fix 10.9 issue of current directory set by Finder to something else than folder where app bundle resides + // typical path of app's binary on Mac OS : /Applications/Tux Paint.app/Contents/MacOS/Tux Paint + int levels = 3; // we need to back up 3 levels + + 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 = 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 + 1) = '\0'; - chdir(app_path); - } - free(app_path); - getcwd(curdir, sizeof(curdir)); - printf("New current directory for runtime: %s\n", curdir); - } #else (void)argv0; #endif @@ -23150,149 +22804,140 @@ static void chdir_to_binary(char *argv0) ///////////////////////////////////////////////////////////////////// -static void setup_colors(void){ +static void setup_colors(void) +{ FILE *fi; int i, j; /* Load colors, or use default ones: */ if (colorfile[0] != '\0') - { - fi = fopen(colorfile, "r"); - if (fi == NULL) { - fprintf(stderr, - "\nWarning, could not open color file. Using defaults.\n"); - perror(colorfile); - colorfile[0] = '\0'; + 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 + { + 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); + } + } } - else - { - int max = 0, per = 5; - char str[80], tmp_str[80]; - int count; - - NUM_COLORS = 0; - - do - { - 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++) { - color_hexes[i] = malloc(sizeof(Uint8 *) * 3); + NUM_COLORS = NUM_DEFAULT_COLORS; - for (j = 0; j < 3; j++) - color_hexes[i][j] = default_color_hexes[i][j]; + color_hexes = malloc(sizeof(Uint8 *) * NUM_COLORS); + color_names = malloc(sizeof(char *) * NUM_COLORS); - color_names[i] = strdup(default_color_names[i]); + 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]); + } } - } /* Add "Color Select" color: */ color_hexes = (Uint8 **) realloc(color_hexes, sizeof(Uint8 *) * (NUM_COLORS + 1)); - color_names = (char **) realloc(color_names, sizeof(char *) * (NUM_COLORS + 1)); + color_names = (char **)realloc(color_names, sizeof(char *) * (NUM_COLORS + 1)); 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; @@ -23304,7 +22949,7 @@ static void setup_colors(void){ color_hexes = (Uint8 **) realloc(color_hexes, sizeof(Uint8 *) * (NUM_COLORS + 1)); - color_names = (char **) realloc(color_names, sizeof(char *) * (NUM_COLORS + 1)); + color_names = (char **)realloc(color_names, sizeof(char *) * (NUM_COLORS + 1)); color_names[NUM_COLORS] = strdup(gettext("Pick a color.")); color_hexes[NUM_COLORS] = (Uint8 *) malloc(sizeof(Uint8) * 3); color_hexes[NUM_COLORS][0] = 0; @@ -23344,33 +22989,32 @@ 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) { - /* Has it not been 30 seconds yet? */ + /* If it exists, read its contents: */ - 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 (fread(&time_lock, sizeof(time_t), 1, fi) > 0) + { + /* Has it not been 30 seconds yet? */ - free(lock_fname); + 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"); - fclose(fi); - exit(0); - } + free(lock_fname); + + fclose(fi); + exit(0); + } + } + + fclose(fi); } - fclose(fi); - } - /* Okay to run; create/update the lockfile */ @@ -23382,19 +23026,18 @@ 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); } @@ -23419,8 +23062,7 @@ int TP_EventFilter(void *data, const SDL_Event * event) 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; @@ -23437,6 +23079,7 @@ static void setup(void) SDL_Surface *tmp_surf; SDL_Rect dest; int scale; + #ifndef LOW_QUALITY_COLOR_SELECTOR int x, y; SDL_Surface *tmp_btn_up; @@ -23447,22 +23090,24 @@ static void setup(void) Uint32 init_flags; char tmp_str[128]; SDL_Surface *img1; + Uint32(*getpixel_tmp_btn_up) (SDL_Surface *, int, int); Uint32(*getpixel_tmp_btn_down) (SDL_Surface *, int, int); Uint32(*getpixel_img_paintwell) (SDL_Surface *, int, int); int big_title; + #ifndef NO_SDLPANGO - SDL_Thread * fontconfig_thread; + SDL_Thread *fontconfig_thread; #endif #ifdef _WIN32 if (fullscreen) - { - InstallKeyboardHook(); - SetActivationState(1); - } + { + InstallKeyboardHook(); + SetActivationState(1); + } #endif im_init(&im_data, get_current_language()); @@ -23472,19 +23117,19 @@ 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 < 1 || - (SDL_MAJOR_VERSION >= 1 && SDL_MINOR_VERSION < 2) || - (SDL_MAJOR_VERSION >= 1 && SDL_MINOR_VERSION >= 2 && SDL_PATCHLEVEL < 12)) { - fprintf(stderr, "Note: 'allowscreensaver' requires SDL 1.2.12 or higher\n"); + putenv((char *)"SDL_VIDEO_ALLOW_SCREENSAVER=1"); + if (SDL_MAJOR_VERSION < 1 || + (SDL_MAJOR_VERSION >= 1 && SDL_MINOR_VERSION < 2) || + (SDL_MAJOR_VERSION >= 1 && SDL_MINOR_VERSION >= 2 && SDL_PATCHLEVEL < 12)) + { + fprintf(stderr, "Note: 'allowscreensaver' requires SDL 1.2.12 or higher\n"); + } } - } if (joystick_dev != -1) do_lock_file(); @@ -23493,34 +23138,33 @@ static void setup(void) if (use_sound) init_flags |= SDL_INIT_AUDIO; if (!fullscreen) - init_flags |= SDL_INIT_NOPARACHUTE; /* allow debugger to catch crash */ + init_flags |= SDL_INIT_NOPARACHUTE; /* allow debugger to catch crash */ /* Init SDL */ if (SDL_Init(init_flags) < 0) - { + { #ifndef NOSOUND - 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 + 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); + { + 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 */ @@ -23529,30 +23173,34 @@ static void setup(void) /* Set up joystick */ - if (joystick_dev == -1) { - printf("%i joystick(s) were found:\n", SDL_NumJoysticks() ); - - for( i=0; i < SDL_NumJoysticks(); i++ ) + if (joystick_dev == -1) { - printf(" %d: %s\n", i, SDL_JoystickName(i)); + printf("%i joystick(s) were found:\n", SDL_NumJoysticks()); + + for (i = 0; i < SDL_NumJoysticks(); i++) + { + printf(" %d: %s\n", i, SDL_JoystickName(i)); + } + + SDL_Quit(); + exit(0); } - 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()); - } else { - SDL_JoystickEventState(SDL_ENABLE); + if (joystick == NULL) + { + fprintf(stderr, "Could not open joystick device %d: %s\n", joystick_dev, SDL_GetError()); + } + else + { + 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 @@ -23561,29 +23209,26 @@ 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]); - - 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; + 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; + } } - } #endif @@ -23592,15 +23237,14 @@ static void setup(void) /* 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); + } setup_colors(); @@ -23611,221 +23255,199 @@ static void setup(void) seticon(); #endif if (hide_cursor) - SDL_ShowCursor (SDL_DISABLE); + SDL_ShowCursor(SDL_DISABLE); /* Deal with orientation rotation option */ if (rotate_orientation) - { - if (native_screensize && fullscreen) { - fprintf(stderr, "Warning: Asking for native screen size overrides request to rotate orientation.\n"); + if (native_screensize && fullscreen) + { + fprintf(stderr, "Warning: Asking for native screen size overrides request to rotate orientation.\n"); + } + else + { + int tmp; + + tmp = WINDOW_WIDTH; + WINDOW_WIDTH = WINDOW_HEIGHT; + WINDOW_HEIGHT = tmp; + } } - else - { - int tmp; - tmp = WINDOW_WIDTH; - WINDOW_WIDTH = WINDOW_HEIGHT; - WINDOW_HEIGHT = tmp; - } - } /* Deal with 'native' screen size option */ if (native_screensize) - { - if (!fullscreen) { - fprintf(stderr, "Warning: Asking for native screensize in a window. Ignoring.\n"); + if (!fullscreen) + { + fprintf(stderr, "Warning: Asking for native screensize in a window. Ignoring.\n"); + } + else + { + WINDOW_WIDTH = 0; + WINDOW_HEIGHT = 0; + } } - 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); - 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; - - SDL_RenderSetScale(renderer, window_scale_w, window_scale_w); - } - else - { - ww = window_scale_h * ww; - hh = window_scale_h * hh; - - SDL_RenderSetScale(renderer, window_scale_h, window_scale_h); - } - } - } - 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: */ + renderer = SDL_CreateRenderer(window_screen, -1, 0); if (native_screensize) - { - WINDOW_WIDTH = screen->w; - WINDOW_HEIGHT = screen->h; - } + { + 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; + + SDL_RenderSetScale(renderer, window_scale_w, window_scale_w); + } + else + { + ww = window_scale_h * ww; + hh = window_scale_h * hh; + + SDL_RenderSetScale(renderer, window_scale_h, window_scale_h); + } + } + } + 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; + } + } } - } if (!fullscreen) - { - int set_window_pos = 0; - if (getenv((char *) "SDL_VIDEO_WINDOW_POS") == NULL) { - set_window_pos = 1; - putenv((char *) "SDL_VIDEO_WINDOW_POS=center"); - } + int set_window_pos = 0; + + 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, - NULL); - /* 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, NULL); + /* 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); @@ -23842,7 +23464,7 @@ VIDEO_BPP, SDL_SWSURFACE);*/ img_title_credits = loadimage(DATA_PREFIX "images/title-credits.png"); img_progress = loadimage(DATA_PREFIX "images/ui/progress.png"); - if (screen->w - img_title->w >= 410 && screen->h - img_progress->h - img_title_credits->h - 40) /* FIXME: Font */ + if (screen->w - img_title->w >= 410 && screen->h - img_progress->h - img_title_credits->h - 40) /* FIXME: Font */ big_title = 1; else big_title = 0; @@ -23855,9 +23477,7 @@ VIDEO_BPP, SDL_SWSURFACE);*/ 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); @@ -23883,13 +23503,12 @@ VIDEO_BPP, SDL_SWSURFACE);*/ #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 @@ -23900,15 +23519,13 @@ VIDEO_BPP, SDL_SWSURFACE);*/ #ifdef SMALL_CURSOR_SHAPES scale = 2; #endif - + #ifdef __APPLE__ - cursor_arrow = SDL_GetCursor(); /* use standard system cursor */ + cursor_arrow = SDL_GetCursor(); /* use standard system cursor */ #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); @@ -23922,35 +23539,43 @@ VIDEO_BPP, SDL_SWSURFACE);*/ fontconfig_thread_done = 0; #ifdef DEBUG - printf("Spawning Pango thread\n"); fflush(stdout); + printf("Spawning Pango thread\n"); + fflush(stdout); #endif 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()); - } else { -#ifdef DEBUG - printf("Thread spawned\n"); fflush(stdout); -#endif - if (generate_fontconfig_cache_spinner(screen)) /* returns 1 if aborted */ + if (fontconfig_thread == NULL) { - 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 */ + fprintf(stderr, "Failed to create Pango setup thread: %s\n", SDL_GetError()); } + else + { #ifdef DEBUG - printf("Done generating cache\n"); fflush(stdout); + printf("Thread spawned\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 + } #ifdef FORKED_FONTS /* NOW we can fork our own font scanner stuff, and let it run in the bgkd -bjk 2010.04.27 */ #ifdef DEBUG - printf("Now running font scanner\n"); fflush(stdout); + printf("Now running font scanner\n"); + fflush(stdout); #endif run_font_scanner(screen, texture, renderer, lang_prefixes[get_current_language()]); #endif @@ -23958,23 +23583,20 @@ VIDEO_BPP, SDL_SWSURFACE);*/ #endif medium_font = TuxPaint_Font_OpenFont(PANGO_DEFAULT_FONT, - DATA_PREFIX "fonts/default_font.ttf", - 18 - (only_uppercase * 3)); + DATA_PREFIX "fonts/default_font.ttf", 18 - (only_uppercase * 3)); 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); + } - snprintf(tmp_str, sizeof(tmp_str), "Version: %s – %s", VER_VERSION, - VER_DATE); + 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; @@ -24006,58 +23628,44 @@ VIDEO_BPP, SDL_SWSURFACE);*/ #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_wand = get_cursor(wand_bits, wand_mask_bits, - wand_width, wand_height, 4 / scale, 4 / scale); + cursor_hand = get_cursor(hand_bits, hand_mask_bits, hand_width, hand_height, 12 / scale, 1 / 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_up = get_cursor(up_bits, up_mask_bits, - up_width, up_height, 15 / scale, 1 / scale); + cursor_rotate = get_cursor(rotate_bits, rotate_mask_bits, rotate_width, rotate_height, 15 / scale, 15 / scale); - cursor_down = get_cursor(down_bits, down_mask_bits, - down_width, down_height, 15 / scale, 30 / scale); + cursor_up = get_cursor(up_bits, up_mask_bits, up_width, up_height, 15 / scale, 1 / scale); - cursor_tiny = get_cursor(tiny_bits, tiny_mask_bits, tiny_width, tiny_height, 3, 3); /* Exactly the same in SMALL (16x16) size! */ + 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! */ /* Create drawing canvas: */ canvas = SDL_CreateRGBSurface(screen->flags, - WINDOW_WIDTH - (96 * 2), - (48 * 7) + 40 + HEIGHTOFFSET, - screen->format->BitsPerPixel, - screen->format->Rmask, - screen->format->Gmask, - screen->format->Bmask, 0); + WINDOW_WIDTH - (96 * 2), + (48 * 7) + 40 + HEIGHTOFFSET, + screen->format->BitsPerPixel, + screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, 0); save_canvas = SDL_CreateRGBSurface(screen->flags, - WINDOW_WIDTH - (96 * 2), - (48 * 7) + 40 + HEIGHTOFFSET, - screen->format->BitsPerPixel, - screen->format->Rmask, - screen->format->Gmask, - screen->format->Bmask, 0); + WINDOW_WIDTH - (96 * 2), + (48 * 7) + 40 + HEIGHTOFFSET, + screen->format->BitsPerPixel, + screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, 0); img_starter = NULL; @@ -24068,23 +23676,22 @@ VIDEO_BPP, SDL_SWSURFACE);*/ 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!\n"); + { + fprintf(stderr, "\nError: Can't build drawing touch mask!\n"); - cleanup(); - exit(1); - } + cleanup(); + exit(1); + } canvas_color_r = 255; canvas_color_g = 255; @@ -24095,12 +23702,10 @@ VIDEO_BPP, SDL_SWSURFACE);*/ /* Creating the label surface: */ label = SDL_CreateRGBSurface(screen->flags, - WINDOW_WIDTH - (96 * 2), - (48 * 7) + 40 + HEIGHTOFFSET, - screen->format->BitsPerPixel, - screen->format->Rmask, - screen->format->Gmask, - screen->format->Bmask, TPAINT_AMASK); + WINDOW_WIDTH - (96 * 2), + (48 * 7) + 40 + HEIGHTOFFSET, + screen->format->BitsPerPixel, + 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)); @@ -24108,29 +23713,27 @@ VIDEO_BPP, SDL_SWSURFACE);*/ /* Create undo buffer space: */ for (i = 0; i < NUM_UNDO_BUFS; i++) - { - undo_bufs[i] = SDL_CreateRGBSurface(screen->flags, - WINDOW_WIDTH - (96 * 2), - (48 * 7) + 40 + HEIGHTOFFSET, - screen->format->BitsPerPixel, - screen->format->Rmask, - screen->format->Gmask, - screen->format->Bmask, 0); - - - 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()); + undo_bufs[i] = SDL_CreateRGBSurface(screen->flags, + WINDOW_WIDTH - (96 * 2), + (48 * 7) + 40 + HEIGHTOFFSET, + screen->format->BitsPerPixel, + screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, 0); - cleanup(); - exit(1); + + 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; } - undo_starters[i] = UNDO_STARTER_NONE; - } - /* Load other images: */ @@ -24159,27 +23762,22 @@ VIDEO_BPP, SDL_SWSURFACE);*/ img_speak = loadimage(DATA_PREFIX "images/tools/speak.png"); img_black = SDL_CreateRGBSurface(SDL_SWSURFACE, - 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->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); SDL_SetSurfaceAlphaMod(img_black, SDL_ALPHA_TRANSPARENT); SDL_FillRect(img_black, NULL, SDL_MapRGBA(screen->format, 0, 0, 0, 255)); img_grey = SDL_CreateRGBSurface(SDL_SWSURFACE, - 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->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); 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); @@ -24242,8 +23840,7 @@ VIDEO_BPP, SDL_SWSURFACE);*/ img_scroll_down = loadimage(DATA_PREFIX "images/ui/scroll_down.png"); img_scroll_up_off = loadimage(DATA_PREFIX "images/ui/scroll_up_off.png"); - img_scroll_down_off = - loadimage(DATA_PREFIX "images/ui/scroll_down_off.png"); + img_scroll_down_off = loadimage(DATA_PREFIX "images/ui/scroll_down_off.png"); img_color_sel = loadimage(DATA_PREFIX "images/ui/csel.png"); #ifdef LOW_QUALITY_COLOR_SELECTOR @@ -24251,26 +23848,32 @@ VIDEO_BPP, SDL_SWSURFACE);*/ #endif if (onscreen_keyboard) - { - img_oskdel = loadimage(DATA_PREFIX "images/ui/osk_delete.png"); - img_osktab = loadimage(DATA_PREFIX "images/ui/osk_tab.png"); - img_oskenter = loadimage(DATA_PREFIX "images/ui/osk_enter.png"); - img_oskcapslock = loadimage(DATA_PREFIX "images/ui/osk_capslock.png"); - img_oskshift = loadimage(DATA_PREFIX "images/ui/osk_shift.png"); + { + img_oskdel = loadimage(DATA_PREFIX "images/ui/osk_delete.png"); + img_osktab = loadimage(DATA_PREFIX "images/ui/osk_tab.png"); + img_oskenter = loadimage(DATA_PREFIX "images/ui/osk_enter.png"); + img_oskcapslock = loadimage(DATA_PREFIX "images/ui/osk_capslock.png"); + img_oskshift = loadimage(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, screen, img_btnsm_up, img_btnsm_down, img_btnsm_off, img_btnsm_nav, img_btnsm_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(onscreen_keyboard_layout, screen, img_btnsm_up, img_btnsm_down, img_btnsm_off, img_btnsm_nav, + img_btnsm_hold, img_oskdel, img_osktab, img_oskenter, img_oskcapslock, img_oskshift, + onscreen_keyboard_disable_change); + } + else + { + kbd = + osk_create(strdup("default.layout"), screen, img_btnsm_up, img_btnsm_down, img_btnsm_off, img_btnsm_nav, + img_btnsm_hold, img_oskdel, img_osktab, img_oskenter, img_oskcapslock, img_oskshift, + onscreen_keyboard_disable_change); + } } - else - { - kbd = osk_create(strdup("default.layout"), screen, img_btnsm_up, img_btnsm_down, img_btnsm_off, img_btnsm_nav, img_btnsm_hold, img_oskdel, img_osktab, img_oskenter, img_oskcapslock, img_oskshift, onscreen_keyboard_disable_change); - } - } show_progress_bar(screen); @@ -24286,13 +23889,11 @@ VIDEO_BPP, SDL_SWSURFACE);*/ #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); @@ -24300,41 +23901,37 @@ VIDEO_BPP, SDL_SWSURFACE);*/ /* Load system fonts: */ large_font = TuxPaint_Font_OpenFont(PANGO_DEFAULT_FONT, - DATA_PREFIX "fonts/default_font.ttf", - 30 - (only_uppercase * 3)); + DATA_PREFIX "fonts/default_font.ttf", 30 - (only_uppercase * 3)); 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)); + 12 - (only_uppercase * 2)); #else - 13 - (only_uppercase * 2)); + 13 - (only_uppercase * 2)); #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 @@ -24382,27 +23979,26 @@ VIDEO_BPP, SDL_SWSURFACE);*/ /* Create toolbox and selector labels: */ for (i = 0; i < NUM_TITLES; i++) - { - if (strlen(title_names[i]) > 0) { - TuxPaint_Font *myfont = large_font; - char *td_str = textdir(gettext(title_names[i])); + if (strlen(title_names[i]) > 0) + { + 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(84, tmp_surf->w), tmp_surf->h, 0); - SDL_FreeSurface(tmp_surf); + 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(84, tmp_surf->w), tmp_surf->h, 0); + SDL_FreeSurface(tmp_surf); + } + else + { + img_title_names[i] = NULL; + } } - else - { - img_title_names[i] = NULL; - } - } @@ -24415,40 +24011,35 @@ VIDEO_BPP, SDL_SWSURFACE);*/ 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 - 96) / 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) { - fprintf(stderr, "\nError: Can't build color button!\n" - "The Simple DirectMedia Layer error that occurred was:\n" - "%s\n\n", SDL_GetError()); + img_color_btns[i] = SDL_CreateRGBSurface(screen->flags, + /* (WINDOW_WIDTH - 96) / NUM_COLORS, 48, */ + tmp_btn_up->w, tmp_btn_up->h, + screen->format->BitsPerPixel, + screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, 0); - cleanup(); - exit(1); + 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]); } - SDL_LockSurface(img_color_btns[i]); - } - /* Generate the buttons based on the thumbnails: */ @@ -24461,76 +24052,70 @@ VIDEO_BPP, SDL_SWSURFACE);*/ for (y = 0; y < tmp_btn_up->h /* 48 */ ; y++) - { - for (x = 0; x < tmp_btn_up->w /* (WINDOW_WIDTH - 96) / 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 == NUM_COLORS - 1) - { - 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 < NUM_COLORS - 1 || a == 255) + for (x = 0; x < tmp_btn_up->w /* (WINDOW_WIDTH - 96) / NUM_COLORS */ ; + x++) { - 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)))); - } - } + 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 == NUM_COLORS - 1) + { + 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 < NUM_COLORS - 1 || 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 == NUM_COLORS - 2 || i == 2 * NUM_COLORS - 2) { - 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); + SDL_UnlockSurface(img_color_btns[i]); + if (i == NUM_COLORS - 2 || i == 2 * NUM_COLORS - 2) + { + 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); + } } - } SDL_UnlockSurface(tmp_btn_up); SDL_UnlockSurface(tmp_btn_down); @@ -24592,9 +24177,7 @@ 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); @@ -24602,13 +24185,13 @@ static void claim_to_be_ready(void) SDL_FreeSurface(img_title_tuxpaint); dest.x = 0; - dest.w = WINDOW_WIDTH; /* SDL mangles this! So, do repairs. */ + dest.w = WINDOW_WIDTH; /* SDL mangles this! So, do repairs. */ update_screen_rect(&dest); do_setcursor(cursor_arrow); playsound(screen, 0, SND_HARP, 1, SNDPOS_CENTER, SNDDIST_NEAR); #if !defined (__ANDROID__) - do_wait(50); /* about 5 seconds */ + do_wait(50); /* about 5 seconds */ #endif /* Set defaults! */ @@ -24646,8 +24229,8 @@ static void claim_to_be_ready(void) brush_scroll = 0; for (i = 0; i < MAX_STAMP_GROUPS; i++) - stamp_scroll[i] = 0; - stamp_group = 0; /* reset! */ + stamp_scroll[i] = 0; + stamp_group = 0; /* reset! */ font_scroll = 0; magic_scroll = 0; tool_scroll = 0; @@ -24679,7 +24262,7 @@ static void claim_to_be_ready(void) int main(int argc, char *argv[]) { - int i; + int i; CLOCK_TYPE time1; CLOCK_TYPE time2; CLOCK_TYPE time3; @@ -24690,8 +24273,8 @@ int main(int argc, char *argv[]) // do not add code (slowness) here unless required for scanning fonts progname = argv[0]; - -#if defined(DEBUG) //EP added block to log messages + +#if defined(DEBUG) //EP added block to log messages #if defined(__APPLE__) freopen("/tmp/tuxpaint.log", "w", stdout); // redirect stdout to a file @@ -24699,11 +24282,12 @@ int main(int argc, char *argv[]) freopen("/mnt/sdcard/tuxpaint/tuxpaint.log", "w", stdout); // redirect stdout to a file #endif - dup2(fileno(stdout), fileno(stderr)); // redirect stderr to stdout + dup2(fileno(stdout), fileno(stderr)); // redirect stderr to stdout setvbuf(stdout, NULL, _IONBF, 0); // we don't want buffering to avoid de-sync'ing stdout and stderr setvbuf(stderr, NULL, _IONBF, 0); // we don't want buffering to avoid de-sync'ing stdout and stderr - char logTime[100]; - time_t t = time(NULL); + char logTime[100]; + time_t t = time(NULL); + strftime(logTime, sizeof(logTime), "%A %d/%m/%Y %H:%M:%S", localtime(&t)); printf("Tux Paint log - %s\n", logTime); #endif @@ -24717,27 +24301,32 @@ int main(int argc, char *argv[]) #ifdef NO_SDLPANGO /* Only fork it now if we're not planning on creating a thread to handle fontconfig stuff -bjk 2010.04.27 */ #ifdef DEBUG - printf("Running font scanner\n"); fflush(stdout); + printf("Running font scanner\n"); + fflush(stdout); #endif run_font_scanner(screen, texture, renderer, lang_prefixes[get_current_language()]); #else #ifdef DEBUG - printf("NOT running font scanner\n"); fflush(stdout); + printf("NOT running font scanner\n"); + fflush(stdout); #endif #endif #endif /* 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"); - } - if (disable_save && (promptless_save != SAVE_OVER_UNSET)) { - fprintf(stderr, "Warning: Save-over option specified, but saving is disabled.\n"); - } + if (disable_save && autosave_on_quit) + { + 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"); + } - if (promptless_save == SAVE_OVER_UNSET) { - promptless_save = SAVE_OVER_PROMPT; - } + if (promptless_save == SAVE_OVER_UNSET) + { + promptless_save = SAVE_OVER_PROMPT; + } /* Set up! */ setup(); @@ -24745,12 +24334,12 @@ int main(int argc, char *argv[]) CLOCK_ASM(time3); #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 - + claim_to_be_ready(); mainloop(); @@ -24767,18 +24356,17 @@ int main(int argc, char *argv[]) /* Moves a file to the trashcan (or deletes it) */ -static int trash(char * path) { +static int trash(char *path) +{ #ifdef UNLINK_ONLY - return(unlink(path)); + return (unlink(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; int cnt; - FILE * fi, * fo; + FILE *fi, *fo; unsigned char buf[1024]; size_t len; @@ -24787,30 +24375,37 @@ 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)); - } - + if (basename(path) == NULL) + { + debug("Can't get basename! Deleting instead."); + return (unlink(path)); + } + printf("trash: basename=%s", basename(path)); //EP strcpy(fname, basename(path)); - if (!file_exists(path)) { - debug("Does't exist anyway, so skipping"); - return(1); - } + if (!file_exists(path)) + { + debug("Does't exist anyway, so skipping"); + return (1); + } /* Move file into Trash folder */ - if (getenv("XDG_DATA_HOME") != NULL) { - sprintf(trashpath, "%s/Trash", getenv("XDG_DATA_HOME")); - } else if (getenv("HOME") != NULL) { - sprintf(trashpath, "%s/.local/share/Trash", getenv("HOME")); - } else { - debug("Can't move to trash! Deleting instead."); - return(unlink(path)); - } + if (getenv("XDG_DATA_HOME") != NULL) + { + sprintf(trashpath, "%s/Trash", getenv("XDG_DATA_HOME")); + } + else if (getenv("HOME") != NULL) + { + sprintf(trashpath, "%s/.local/share/Trash", getenv("HOME")); + } + else + { + debug("Can't move to trash! Deleting instead."); + return (unlink(path)); + } mkdir(trashpath, 0x777); sprintf(dest, "%s/files", trashpath); @@ -24821,64 +24416,75 @@ static int trash(char * path) { sprintf(dest, "%s/files/%s", trashpath, fname); strcpy(bname, fname); - if (strstr(bname, ".") != NULL) { - strcpy(strstr(bname, "."), "\0"); - strcpy(ext, strstr(fname, ".") + 1); - } else { - debug("Filename format unfamiliar! Deleting instead."); - return(unlink(path)); - } + if (strstr(bname, ".") != NULL) + { + strcpy(strstr(bname, "."), "\0"); + strcpy(ext, strstr(fname, ".") + 1); + } + else + { + debug("Filename format unfamiliar! Deleting instead."); + return (unlink(path)); + } sprintf(infoname, "%s/info/%s.trashinfo", trashpath, fname); cnt = 1; - while (file_exists(dest) && cnt < 100) { - sprintf(fname, "%s_%d.%s", bname, cnt, ext); + while (file_exists(dest) && cnt < 100) + { + sprintf(fname, "%s_%d.%s", bname, cnt, ext); - sprintf(dest, "%s/files/%s", trashpath, fname); - sprintf(infoname, "%s/info/%s.trashinfo", trashpath, fname); - cnt++; - } + sprintf(dest, "%s/files/%s", trashpath, fname); + sprintf(infoname, "%s/info/%s.trashinfo", trashpath, fname); + cnt++; + } - if (cnt >= 100) { - debug("Too many identically-named files! Deleting instead."); - return(unlink(path)); - } + if (cnt >= 100) + { + 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."); + 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 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."); + 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(buf), 1, fi); + if (len > 0) + { + fwrite(buf, sizeof(buf), 1, fo); + } + } fclose(fi); - return(unlink(path)); - } - while (!feof(fi)) { - len = fread(buf, sizeof(buf), 1, fi); - if (len > 0) { - fwrite(buf, sizeof(buf), 1, fo); - } - } - fclose(fi); - fclose(fo); + fclose(fo); - unlink(path); - } + unlink(path); + } /* Create info file */ fo = fopen(infoname, "w"); - if (fo == NULL) { - debug("Error: Couldn't create info file!"); - return(1); - } + if (fo == NULL) + { + debug("Error: Couldn't create info file!"); + return (1); + } now = time(NULL); tim = *(localtime(&now)); @@ -24915,21 +24521,23 @@ static int trash(char * path) { /* FIXME: Haiku */ - return(0); + return (0); #endif /* UNLINK_ONLY */ } -int file_exists(char * path) { +int file_exists(char *path) +{ struct stat buf; int res; res = stat(path, &buf); - return(res == 0); + return (res == 0); } /* 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; if (event.jaxis.which != 0) @@ -24939,87 +24547,92 @@ 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; - } - 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; - - // printf("i %d valx %d j %d val_y %d\n", i, val_x, j, val_y); - if (*val_x || *val_y) { - *motioner = TRUE; - } - else *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; + + // printf("i %d valx %d j %d val_y %d\n", i, val_x, j, val_y); + if (*val_x || *val_y) + { + *motioner = TRUE; + } + else + *motioner = FALSE; + } } -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) { +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; - } - if(*valhat_x || *valhat_y) + 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; + } + if (*valhat_x || *valhat_y) SDL_WarpMouse(oldpos_x + *valhat_x, oldpos_y + *valhat_y); *old_hat_ticks = SDL_GetTicks(); } -static void handle_joyballmotion(SDL_Event event, int oldpos_x, int oldpos_y) { +static void handle_joyballmotion(SDL_Event event, int oldpos_x, int oldpos_y) +{ int val_x, val_y; + /* FIXME: NOT TESTED Should this act like handle_joyaxismotion? in the sense of setting the values for the moving but don't move the mouse here? */ /* printf("\n ball movement \n"); */ @@ -25029,10 +24642,12 @@ static void handle_joyballmotion(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_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; Uint32 ticks; + ticks = SDL_GetTicks(); vx = vy = 0; @@ -25040,26 +24655,28 @@ static void handle_motioners(int oldpos_x, int oldpos_y, int motioner, int hatmo vy = oldpos_y + val_y; -if (ticks - old_hat_ticks > joystick_hat_timeout) - { - vx += valhat_x; - vy += valhat_y; - } -SDL_WarpMouse(vx, vy); + if (ticks - old_hat_ticks > joystick_hat_timeout) + { + vx += valhat_x; + vy += valhat_y; + } + SDL_WarpMouse(vx, vy); -if (motioner && joystick_slowness) - SDL_Delay(joystick_slowness); + if (motioner && joystick_slowness) + SDL_Delay(joystick_slowness); -if (hatmotioner && joystick_hat_slowness) - SDL_Delay(joystick_hat_slowness); + if (hatmotioner && joystick_hat_slowness) + SDL_Delay(joystick_hat_slowness); } -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); } - -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; SDL_Event ev; @@ -25071,152 +24688,152 @@ 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) { - ev.type = SDL_KEYDOWN; - ev.key.keysym.sym = SDLK_ESCAPE; - ev.key.keysym.mod = KMOD_CTRL; + /* 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; + } } - 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); @@ -25224,11 +24841,13 @@ 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]) { - ignore = 1; + for (i = 0; i < joystick_buttons_ignore_len && !ignore; i++) + { + if (event.button.button == joystick_buttons_ignore[i]) + { + ignore = 1; + } } - } if (!ignore) SDL_PushEvent(&ev); @@ -25243,91 +24862,96 @@ static void handle_joybuttonupdownscl(SDL_Event event, int oldpos_x, int oldpos_ int motion_convert; int motion_dx, motion_dy; -static void start_motion_convert (SDL_Event event) { - if(event.type != SDL_MOUSEBUTTONDOWN) - return; +static void start_motion_convert(SDL_Event event) +{ + if (event.type != SDL_MOUSEBUTTONDOWN) + return; - int scroll = 0; - 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)) - scroll = 1; - else if ((cur_tool == TOOL_OPEN) && HIT(r_canvas)) - scroll = 1; - else if ((cur_tool == TOOL_NEW) && HIT(r_canvas)) - scroll = 1; + int scroll = 0; - if (scroll != 1) - return; + 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)) + scroll = 1; + else if ((cur_tool == TOOL_OPEN) && HIT(r_canvas)) + scroll = 1; + else if ((cur_tool == TOOL_NEW) && HIT(r_canvas)) + scroll = 1; - motion_convert = 1; - motion_dx = motion_dy = 0; + if (scroll != 1) + return; + + motion_convert = 1; + motion_dx = motion_dy = 0; } #ifdef __ANDROID__ -static void stop_motion_convert (SDL_Event event) { - if(event.type != SDL_MOUSEBUTTONUP) - return; +static void stop_motion_convert(SDL_Event event) +{ + if (event.type != SDL_MOUSEBUTTONUP) + return; - motion_convert = 0; - motion_dx = motion_dy = 0; + motion_convert = 0; + motion_dx = motion_dy = 0; } -static void convert_motion_to_wheel (SDL_Event event) { - if(event.type != SDL_MOUSEMOTION) - return; +static void convert_motion_to_wheel(SDL_Event event) +{ + if (event.type != SDL_MOUSEMOTION) + return; - if (motion_convert == 0) - return; + if (motion_convert == 0) + return; - int scroll = 0; - 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; - } - else if ((cur_tool == TOOL_OPEN) && HIT(r_canvas)) - { - scroll = 1; - high = THUMB_H; - } - else if ((cur_tool == TOOL_NEW) && HIT(r_canvas)) - { - scroll = 1; - high = THUMB_H; - } + int scroll = 0; + int high = 0; - if (scroll != 1) - return; + 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; + } + else if ((cur_tool == TOOL_OPEN) && HIT(r_canvas)) + { + scroll = 1; + high = THUMB_H; + } + else if ((cur_tool == TOOL_NEW) && HIT(r_canvas)) + { + scroll = 1; + high = THUMB_H; + } - motion_dx += event.button.x - oldpos_x; - motion_dy += event.button.y - oldpos_y; + if (scroll != 1) + return; - if (motion_dy > 0) - { - while (motion_dy - high > 0) - { - SDL_SendMouseWheel (NULL, event.motion.which, 0, 1); - motion_dy -= high; - } - } - else - { - while (motion_dy + high < 0) - { - SDL_SendMouseWheel (NULL, event.motion.which, 0, -1); - motion_dy += high; - } - } + motion_dx += event.button.x - oldpos_x; + motion_dy += event.button.y - oldpos_y; + + if (motion_dy > 0) + { + while (motion_dy - high > 0) + { + SDL_SendMouseWheel(NULL, event.motion.which, 0, 1); + motion_dy -= high; + } + } + else + { + while (motion_dy + high < 0) + { + SDL_SendMouseWheel(NULL, event.motion.which, 0, -1); + motion_dy += high; + } + } } #endif diff --git a/src/win32_dirent.c b/src/win32_dirent.c index 6ddcd8a7f..5a4f1756f 100644 --- a/src/win32_dirent.c +++ b/src/win32_dirent.c @@ -1,128 +1,125 @@ -/****************************************************/ -/* */ -/* For Win32 that lacks Unix direct support. */ -/* - avoids including "windows.h" */ -/* */ -/* Copyright (c) 2002 John Popplewell */ -/* john@johnnypops.demon.co.uk */ -/* */ -/* Version 1.0.1 - fixed bug in opendir() */ -/* Version 1.0.0 - initial version */ -/* */ -/****************************************************/ - -/* - 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 - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ - -/* $Id$ */ - -#include -#include -#include - -#include "win32_dirent.h" -#include "debug.h" - DIR * opendir(const char *pSpec) +/****************************************************/ +/* */ +/* For Win32 that lacks Unix direct support. */ +/* - avoids including "windows.h" */ +/* */ +/* Copyright (c) 2002 John Popplewell */ +/* john@johnnypops.demon.co.uk */ +/* */ +/* Version 1.0.1 - fixed bug in opendir() */ +/* Version 1.0.0 - initial version */ +/* */ +/****************************************************/ + +/* + 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 + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +/* $Id$ */ + +#include +#include +#include + +#include "win32_dirent.h" +#include "debug.h" + DIR * opendir(const char *pSpec) { - char pathname[MAX_PATH + 2]; - DIR * pDir = calloc(1, sizeof(DIR)); - if (!pDir) - return NULL; - strcpy(pathname, pSpec); - strcat(pathname, "/*"); - pDir->hFind = FindFirstFile(pathname, &pDir->wfd); - if (pDir->hFind == INVALID_HANDLE_VALUE) - - { - free(pDir); - pDir = NULL; - } - return pDir; - } - void closedir(DIR * pDir) -{ - assert(pDir != NULL); - free(pDir); - } 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)) - - { - FindClose(pDir->hFind); - pDir->hFind = NULL; - } - return &pDir->de; - } - return NULL; - } - int alphasort(const void *a, const void *b) -{ - return (strcmp - ((*(const struct dirent **) a)->d_name, - (*(const struct dirent **) b)->d_name)); - } 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 *))); - if (*namelist == NULL) - return -1; - 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; - memcpy(block, entry, size); - return ++i; - } - int scandir(const char *dir, struct dirent ***namelist, selectCB select, - comparCB compar) -{ - DIR * pDir; - int count; - struct dirent *entry; - assert((dir != NULL) && (namelist != NULL)); - pDir = opendir(dir); - if (!pDir) - return -1; - count = 0; - while ((entry = readdir(pDir)) != NULL) - - { - 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); - return count; - } + char pathname[MAX_PATH + 2]; + + DIR * pDir = calloc(1, sizeof(DIR)); + if (!pDir) + return NULL; + strcpy(pathname, pSpec); + strcat(pathname, "/*"); + pDir->hFind = FindFirstFile(pathname, &pDir->wfd); + if (pDir->hFind == INVALID_HANDLE_VALUE) + + { + free(pDir); + pDir = NULL; + } + return pDir; +} + +void closedir(DIR * pDir) +{ + assert(pDir != NULL); + free(pDir); +} 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)) + + { + FindClose(pDir->hFind); + pDir->hFind = NULL; + } + return &pDir->de; + } + return NULL; +} + +int alphasort(const void *a, const void *b) +{ + return (strcmp((*(const struct dirent **)a)->d_name, (*(const struct dirent **)b)->d_name)); +} 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 *))); + if (*namelist == NULL) + return -1; + 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; + memcpy(block, entry, size); + return ++i; +} + +int scandir(const char *dir, struct dirent ***namelist, selectCB select, comparCB compar) +{ + DIR * pDir; + int count; + struct dirent *entry; + + assert((dir != NULL) && (namelist != NULL)); + pDir = opendir(dir); + if (!pDir) + return -1; + count = 0; + while ((entry = readdir(pDir)) != NULL) + + { + 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); + return count; +} + - diff --git a/src/win32_dirent.h b/src/win32_dirent.h index 35cfc0507..c9e57d468 100644 --- a/src/win32_dirent.h +++ b/src/win32_dirent.h @@ -1,73 +1,76 @@ -/****************************************************/ -/* */ -/* For Win32 that lacks Unix direct support. */ -/* - avoids including "windows.h" */ -/* */ -/* Copyright (c) 2002 John Popplewell */ -/* john@johnnypops.demon.co.uk */ -/* */ -/****************************************************/ -/* $Id$ */ - typedef long BOOL; - typedef unsigned int DWORD; - typedef wchar_t TCHAR; - typedef void *HANDLE; - -#define MAX_PATH 256 -#define INVALID_HANDLE_VALUE ((HANDLE)(-1)) -#define WINAPI __stdcall - typedef struct +/****************************************************/ +/* */ +/* For Win32 that lacks Unix direct support. */ +/* - avoids including "windows.h" */ +/* */ +/* Copyright (c) 2002 John Popplewell */ +/* john@johnnypops.demon.co.uk */ +/* */ +/****************************************************/ +/* $Id$ */ +typedef long BOOL; +typedef unsigned int DWORD; +typedef wchar_t TCHAR; +typedef void *HANDLE; + + +#define MAX_PATH 256 +#define INVALID_HANDLE_VALUE ((HANDLE)(-1)) +#define WINAPI __stdcall +typedef struct { - DWORD dwLowDateTime; - DWORD dwHighDateTime; - } FILETIME; - typedef struct + DWORD dwLowDateTime; + DWORD dwHighDateTime; +} FILETIME; +typedef struct { - DWORD dwFileAttributes; - FILETIME ftCreationTime; - FILETIME ftLastAccessTime; - FILETIME ftLastWriteTime; - DWORD nFileSizeHigh; - DWORD nFileSizeLow; - DWORD dwReserved0; - DWORD dwReserved1; - TCHAR cFileName[MAX_PATH]; - TCHAR cAlternateFileName[14]; - } WIN32_FIND_DATA; - -#define FindFirstFile FindFirstFileA -#define FindNextFile FindNextFileA -#define FindClose FindClose - -#ifdef __cplusplus + DWORD dwFileAttributes; + FILETIME ftCreationTime; + FILETIME ftLastAccessTime; + FILETIME ftLastWriteTime; + DWORD nFileSizeHigh; + DWORD nFileSizeLow; + DWORD dwReserved0; + DWORD dwReserved1; + TCHAR cFileName[MAX_PATH]; + TCHAR cAlternateFileName[14]; +} WIN32_FIND_DATA; + + +#define FindFirstFile FindFirstFileA +#define FindNextFile FindNextFileA +#define FindClose FindClose + +#ifdef __cplusplus extern "C" { - -#endif /* */ - extern HANDLE WINAPI FindFirstFile(const char *, WIN32_FIND_DATA *); - extern BOOL WINAPI FindNextFile(HANDLE, WIN32_FIND_DATA *); - extern BOOL WINAPI FindClose(HANDLE); - -#ifdef __cplusplus + +#endif /* */ + extern HANDLE WINAPI FindFirstFile(const char *, WIN32_FIND_DATA *); + extern BOOL WINAPI FindNextFile(HANDLE, WIN32_FIND_DATA *); + extern BOOL WINAPI FindClose(HANDLE); + +#ifdef __cplusplus }; - -#endif /* */ - struct dirent + + +#endif /* */ +struct dirent { - char d_name[MAX_PATH]; - }; - typedef struct + char d_name[MAX_PATH]; +}; +typedef struct { - WIN32_FIND_DATA wfd; - HANDLE hFind; - struct dirent de; - } DIR; - 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 *); - extern int alphasort(const void *a, const void *b); - extern int scandir(const char *dir, struct dirent ***namelist, - selectCB select, comparCB compar); - + WIN32_FIND_DATA wfd; + HANDLE hFind; + struct dirent de; +} DIR; +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 *); +extern int alphasort(const void *a, const void *b); +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 2bdbcbe3d..74dfcd76e 100644 --- a/src/win32_print.c +++ b/src/win32_print.c @@ -67,20 +67,19 @@ 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 + 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 + SDL_FreeSurface(surf24); // Free temp surface return surfDIB; } @@ -101,17 +100,16 @@ 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; } @@ -138,8 +136,7 @@ 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; @@ -152,8 +149,7 @@ 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; @@ -163,8 +159,7 @@ 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; @@ -185,39 +180,38 @@ err_exit: } -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; } static int FileExists(const char *filepath) { - FILE *fp; + FILE *fp; - if ((fp = fopen(filepath, "rb")) != NULL) + if ((fp = fopen(filepath, "rb")) != NULL) { fclose(fp); return 1; } - return 0; + return 0; } static int GetCustomPrinterDC(HWND hWnd, const char *printcfg, int show) @@ -237,23 +231,22 @@ static int GetCustomPrinterDC(HWND hWnd, const char *printcfg, int show) pd.hDevMode = LoadCustomPrinterHDEVMODE(hWnd, printcfg); if (show || !FileExists(printcfg)) - { - if (PrintDlg(&pd)) { - hDCprinter = pd.hDC; - SaveCustomPrinterHDEVMODE(hWnd, printcfg, pd.hDevMode); + if (PrintDlg(&pd)) + { + hDCprinter = pd.hDC; + SaveCustomPrinterHDEVMODE(hWnd, printcfg, pd.hDevMode); + GlobalFree(pd.hDevMode); + return 1; + } GlobalFree(pd.hDevMode); - return 1; + return 0; } - 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); } @@ -276,9 +269,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; } @@ -292,8 +285,7 @@ int IsPrinterAvailable(void) #define STRETCH_TO_FIT 0 #define SCALE_TO_FIT 1 -const char *SurfacePrint(SDL_Surface * surf, const char *printcfg, - int showdialog) +const char *SurfacePrint(SDL_Surface * surf, const char *printcfg, int showdialog) { const char *res = NULL; HWND hWnd; @@ -316,10 +308,10 @@ const char *SurfacePrint(SDL_Surface * surf, const char *printcfg, hWnd = wminfo.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."; @@ -334,26 +326,26 @@ const char *SurfacePrint(SDL_Surface * surf, const char *printcfg, 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); @@ -363,104 +355,105 @@ const char *SurfacePrint(SDL_Surface * surf, const char *printcfg, bmih.biWidth = surf24->w; bmih.biHeight = surf24->h; - pageWidth = GetDeviceCaps(hDCprinter, HORZRES); + pageWidth = GetDeviceCaps(hDCprinter, HORZRES); pageHeight = GetDeviceCaps(hDCprinter, VERTRES); - sX = GetDeviceCaps(hDCprinter, LOGPIXELSX); - sY = GetDeviceCaps(hDCprinter, LOGPIXELSY); + sX = GetDeviceCaps(hDCprinter, LOGPIXELSX); + 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; + 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 width = bmih.biWidth; int height = bmih.biHeight; if (width < pageWidth && height < pageHeight) - { - float dW = (float)pageWidth / width; - float dH = (float)pageHeight / height; + { + float dW = (float)pageWidth / width; + float dH = (float)pageHeight / height; if (dW < dH) - { - width = pageWidth; - height = (int)((height * dW * (sY/sX)) + 0.5f); - } + { + width = pageWidth; + height = (int)((height * dW * (sY / sX)) + 0.5f); + } else - { - width = (int)((width * dH * (sX/sY)) + 0.5f); + { + width = (int)((width * dH * (sX / sY)) + 0.5f); height = pageHeight; - } - } + } + } if (width > pageWidth) - { - height= height*width/pageWidth; + { + height = height * width / pageWidth; width = pageWidth; - } + } if (height > pageHeight) - { - width= width*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; + 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; - } + 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; - } - - if (hDCCaps & RC_STRETCHDIB) - { - SetStretchBltMode(hDCprinter, COLORONCOLOR); - - nError = StretchDIBits(hDCprinter, rcDst.left, rcDst.top, - rcDst.right - rcDst.left, - rcDst.bottom - rcDst.top, - 0, 0, bmih.biWidth, bmih.biHeight, - surf24->pixels, (BITMAPINFO *) & bmih, - DIB_RGB_COLORS, SRCCOPY); - if (nError == GDI_ERROR) { - res = "win32_print: StretchDIBits() failed."; + res = "win32_print: printer context requires palette."; goto error; } - } + + if (hDCCaps & RC_STRETCHDIB) + { + SetStretchBltMode(hDCprinter, COLORONCOLOR); + + nError = StretchDIBits(hDCprinter, rcDst.left, rcDst.top, + rcDst.right - rcDst.left, + rcDst.bottom - rcDst.top, + 0, 0, bmih.biWidth, bmih.biHeight, + surf24->pixels, (BITMAPINFO *) & bmih, DIB_RGB_COLORS, SRCCOPY); + if (nError == GDI_ERROR) + { + res = "win32_print: StretchDIBits() failed."; + goto error; + } + } else - { - res = "win32_print: StretchDIBits() not available."; - goto error; - } + { + 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); @@ -482,8 +475,7 @@ 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; @@ -491,9 +483,7 @@ static HRESULT ReadRegistry(const char *key, const char *option, char *value, res = RegOpenKeyEx(HKEY_CURRENT_USER, key, 0, KEY_READ, &hKey); if (res != ERROR_SUCCESS) goto err_exit; - res = - RegQueryValueEx(hKey, option, NULL, NULL, (LPBYTE) value, - (LPDWORD) & size); + res = RegQueryValueEx(hKey, option, NULL, NULL, (LPBYTE) value, (LPDWORD) & size); if (res != ERROR_SUCCESS) goto err_exit; res = ERROR_SUCCESS; @@ -529,18 +519,17 @@ 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"); } @@ -551,16 +540,15 @@ 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"); } @@ -573,9 +561,9 @@ static char *GetUserTempDir(void) char *temp = getenv("TEMP"); if (!temp) - { - temp = "userdata"; - } + { + temp = "userdata"; + } return strdup(temp); } @@ -594,27 +582,27 @@ char *get_temp_fname(const char *const name) */ static HHOOK g_hKeyboardHook = NULL; -static int g_bWindowActive = 0; +static int g_bWindowActive = 0; LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { int bEatKeystroke = 0; - KBDLLHOOKSTRUCT *p = (KBDLLHOOKSTRUCT*)lParam; + KBDLLHOOKSTRUCT *p = (KBDLLHOOKSTRUCT *) lParam; if (nCode < 0 || nCode != HC_ACTION) - return CallNextHookEx(g_hKeyboardHook, nCode, wParam, lParam); - - switch (wParam) - { - case WM_KEYDOWN: - case WM_KEYUP: + return CallNextHookEx(g_hKeyboardHook, nCode, wParam, lParam); + + switch (wParam) { - bEatKeystroke = g_bWindowActive && ((p->vkCode == VK_LWIN) || (p->vkCode == VK_RWIN)); - break; + case WM_KEYDOWN: + case WM_KEYUP: + { + bEatKeystroke = g_bWindowActive && ((p->vkCode == VK_LWIN) || (p->vkCode == VK_RWIN)); + break; + } } - } - - if(bEatKeystroke) + + if (bEatKeystroke) return 1; return CallNextHookEx(g_hKeyboardHook, nCode, wParam, lParam); } @@ -640,5 +628,3 @@ void SetActivationState(int state) { g_bWindowActive = state; } - - diff --git a/src/win32_print.h b/src/win32_print.h index a12054860..184f85530 100644 --- a/src/win32_print.h +++ b/src/win32_print.h @@ -15,8 +15,7 @@ #endif /* if printcfg is NULL, uses the default printer */ -extern const char *SurfacePrint(SDL_Surface * surf, - const char *printcfg, int showdialog); +extern const char *SurfacePrint(SDL_Surface * surf, const char *printcfg, int showdialog); extern int IsPrinterAvailable(void); /* additional windows functions requiring */ @@ -25,8 +24,8 @@ extern char *GetSystemFontDir(void); extern char *get_temp_fname(const char *const name); /* keyboard hooking functions */ -extern int InstallKeyboardHook(void); -extern int RemoveKeyboardHook(void); +extern int InstallKeyboardHook(void); +extern int RemoveKeyboardHook(void); extern void SetActivationState(int state); #endif