From fd65f98abc9f5168ad855b3955b2bdd402d7c4fc Mon Sep 17 00:00:00 2001 From: Andrew Corcoran Date: Thu, 24 Jul 2008 15:44:14 +0000 Subject: [PATCH] Added Rain magic tool. Added error checking to previous tools. Added missing static declarations for global variables --- docs/AUTHORS.txt | 4 +- docs/CHANGES.txt | 1 + magic/src/blur.c | 5 +- magic/src/mosaic.c | 13 ++- magic/src/noise.c | 7 +- magic/src/rain.c | 223 ++++++++++++++++++++++++++++++++++++++++++++ magic/src/sharpen.c | 9 +- magic/src/snow.c | 16 +++- magic/src/tint.c | 3 + 9 files changed, 264 insertions(+), 17 deletions(-) create mode 100644 magic/src/rain.c diff --git a/docs/AUTHORS.txt b/docs/AUTHORS.txt index 07303eb2a..40d74cb0a 100644 --- a/docs/AUTHORS.txt +++ b/docs/AUTHORS.txt @@ -47,8 +47,8 @@ $Id$ Blur ('entire image' mode), Sharpen, Trace Contour, Silhouette, Snow Flake, Snow Ball, Black & White, Threshold, - Tint, Noise and Mosaic Magic Tools - Jigsaw 3x3 and Jigsaw 5x5 starter images + Tint ('Brush' mode), Noise, Rain and Mosaic Magic Tools. + Jigsaw 3x3 and Jigsaw 5x5 starter images. by Andrew 'akanewbie' Corcoran Contributed as part of Google Summer of Code, 2008. diff --git a/docs/CHANGES.txt b/docs/CHANGES.txt index e36ebb6a7..53a59e08d 100644 --- a/docs/CHANGES.txt +++ b/docs/CHANGES.txt @@ -28,6 +28,7 @@ $Id$ + Snow Flake - Places random snow flakes over the image. + Noise - Adds random noise to the image. + Mosaic - Gives the image a mosaic effect. + + Rain - Adds rain drops to the image. By Andrew 'akanewbie' Corcoran (Part of Tux4Kids' participation in Google Summer of Code 2008) diff --git a/magic/src/blur.c b/magic/src/blur.c index 73738ca89..f5942738b 100644 --- a/magic/src/blur.c +++ b/magic/src/blur.c @@ -43,7 +43,7 @@ enum { blur_NUM_TOOLS }; -const int blur_RADIUS = 16; +static const int blur_RADIUS = 16; static Mix_Chunk * blur_snd_effect[blur_NUM_TOOLS]; @@ -71,6 +71,9 @@ int blur_init(magic_api * api){ 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); + if (blur_snd_effect[i]==NULL){ + return(0); + } } return(1); } diff --git a/magic/src/mosaic.c b/magic/src/mosaic.c index 1206f7fcf..f3382bc99 100644 --- a/magic/src/mosaic.c +++ b/magic/src/mosaic.c @@ -46,13 +46,13 @@ #endif -const int mosaic_AMOUNT= 300; -const int mosaic_RADIUS = 16; -const double mosaic_SHARPEN = 1.0; +static const int mosaic_AMOUNT= 300; +static const int mosaic_RADIUS = 16; +static const double mosaic_SHARPEN = 1.0; //Holder for the pre calulated pixel values -SDL_Surface * mosaic_temp; -SDL_Surface * mosaic_final; +static SDL_Surface * mosaic_temp; +static SDL_Surface * mosaic_final; enum { TOOL_MOSAIC, @@ -85,6 +85,9 @@ int mosaic_init(magic_api * api){ 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); + if (mosaic_snd_effect[i]==NULL){ + return(0); + } } return(1); diff --git a/magic/src/noise.c b/magic/src/noise.c index 599169f5c..b269c02aa 100644 --- a/magic/src/noise.c +++ b/magic/src/noise.c @@ -43,8 +43,8 @@ #define gettext_noop(String) String #endif -const int noise_AMOUNT = 100.0; -const int noise_RADIUS = 16; +static const int noise_AMOUNT = 100.0; +static const int noise_RADIUS = 16; enum { TOOL_NOISE, @@ -78,6 +78,9 @@ int noise_init(magic_api * api){ 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); + if (noise_snd_effect[i]==NULL){ + return(0); + } } return(1); } diff --git a/magic/src/rain.c b/magic/src/rain.c new file mode 100644 index 000000000..9b3389d23 --- /dev/null +++ b/magic/src/rain.c @@ -0,0 +1,223 @@ +/* + rain.c + + rain, Add a rain effect to the image + Tux Paint - A simple drawing program for children. + + Credits: Andrew Corcoran + + Copyright (c) 2002-2007 by Bill Kendrick and others; see AUTHORS.txt + bill@newbreedsoftware.com + http://www.tuxpaint.org/ + + 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 + (See COPYING.txt) + + Last updated: June 6, 2008 + $Id$ +*/ + +#include +#include +#include +#include "tp_magic_api.h" +#include "SDL_image.h" +#include "SDL_mixer.h" +#include +#include +#include + +#ifndef gettext_noop +#define gettext_noop(String) String +#endif + + +static const int rain_SIZE = 30; +static const int rain_AMOUNT = 200; + +enum { + TOOL_rain, + rain_NUM_TOOLS +}; + +static Mix_Chunk * rain_snd_effect[rain_NUM_TOOLS]; + +const char * rain_snd_filenames[rain_NUM_TOOLS] = { + "flip.wav", +}; +const char * rain_icon_filenames[rain_NUM_TOOLS] = { + "flip.png", +}; +const char * rain_names[rain_NUM_TOOLS] = { + gettext_noop("Rain"), +}; +const char * rain_descs[rain_NUM_TOOLS][2] = { + {gettext_noop("Click to place a rain drop onto the image."), + gettext_noop("Click to cover the image with rain drops."),}, +}; + + +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; + } + return 0; +} + +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); + if (rain_snd_effect[i] == NULL){ + return(0); + } + } + + return(1); +} + +int rain_get_tool_count(magic_api * api){ + return(rain_NUM_TOOLS); +} + +// Load our icons: +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 our names, localized: +char * rain_get_name(magic_api * api, int which){ + return(strdup(gettext(rain_names[which]))); +} + +// Return our descriptions, localized: +char * rain_get_description(magic_api * api, int which, int mode){ + return(strdup(gettext(rain_descs[which][mode-1]))); +} + +// Do the effect: +static void do_rain_drop(void * ptr, int which, SDL_Surface * canvas, SDL_Surface * last, + int x, int y){ + magic_api * api = (magic_api *) ptr; + + int xx, yy; + 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(last, xx , yy), last->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))); + } + } + } + +} + +// 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){ +//noop +} + +// 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){ + + 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; + + api->playsound(rain_snd_effect[which], (x * 255) / canvas->w, 255); + }else{ + + int i; + for(i=0; iw, 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, int which, + SDL_Surface * canvas, SDL_Surface * last, + int x, int y, SDL_Rect * update_rect) +{ +} + +// No setup happened: +void rain_shutdown(magic_api * api) +{ + //Clean up sounds + int i; + for(i=0; idata_directory, sharpen_snd_filenames[i]); sharpen_snd_effect[i] = Mix_LoadWAV(fname); + if (sharpen_snd_effect[i]==NULL){ + return(0); + } } return(1); diff --git a/magic/src/snow.c b/magic/src/snow.c index 0a9d66d50..0f602bda3 100644 --- a/magic/src/snow.c +++ b/magic/src/snow.c @@ -43,8 +43,8 @@ #define gettext_noop(String) String #endif -const int snow_AMOUNT = 400; -const int snow_RADIUS = 5; +static const int snow_AMOUNT = 400; +static const int snow_RADIUS = 5; static SDL_Surface * snow_flake1; static SDL_Surface * snow_flake2; @@ -84,15 +84,23 @@ int snow_init(magic_api * api){ snprintf(fname, sizeof(fname), "%s/images/magic/Snow_flake4.png", api->data_directory); snow_flake1 = IMG_Load(fname); - - + 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){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); + if (snow_snd_effect[i]==NULL){ + return(0); + } } return(1); } diff --git a/magic/src/tint.c b/magic/src/tint.c index aafb39bf7..5c04a4596 100644 --- a/magic/src/tint.c +++ b/magic/src/tint.c @@ -84,6 +84,9 @@ int tint_init(magic_api * api){ 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); + if (tint_snd_effect[i] == NULL){ + return(0); + } } return(1); }