source: tuxpaint/trunk/fuentes/magic/src/rainbow.c @ 219

Last change on this file since 219 was 219, checked in by mabarracus, 4 years ago

added sources from trusty

File size: 6.6 KB
Line 
1/*
2  rainbow.c
3
4  Rainbow Magic Tool Plugin
5  Tux Paint - A simple drawing program for children.
6
7  Copyright (c) 2002-2008 by Bill Kendrick and others; see AUTHORS.txt
8  bill@newbreedsoftware.com
9  http://www.tuxpaint.org/
10
11  This program is free software; you can redistribute it and/or modify
12  it under the terms of the GNU General Public License as published by
13  the Free Software Foundation; either version 2 of the License, or
14  (at your option) any later version.
15
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  GNU General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  (See COPYING.txt)
25
26  Last updated: July 8, 2008
27  $Id: rainbow.c,v 1.13 2011/12/19 09:32:26 perepujal Exp $
28*/
29
30#include <stdio.h>
31#include <string.h>
32#include "tp_magic_api.h"
33#include "SDL_image.h"
34#include "SDL_mixer.h"
35
36/* Our globals: */
37
38#define NUM_RAINBOW_COLORS 23
39
40static const int rainbow_hexes[NUM_RAINBOW_COLORS][3] = {
41  {255, 0, 0},
42  {255, 64, 0},
43  {255, 128, 0},
44  {255, 192, 0},
45  {255, 255, 0},
46  {192, 255, 0},
47  {128, 255, 0},
48  {64, 255, 0},
49  {0, 255, 0},
50  {0, 255, 64},
51  {0, 255, 128},
52  {0, 255, 192},
53  {0, 255, 255},
54  {0, 192, 255},
55  {0, 128, 255},
56  {0, 64, 255},
57  {64, 0, 255},
58  {128, 0, 255},
59  {192, 0, 255},
60  {255, 0, 255},
61  {255, 0, 192},
62  {255, 0, 128},
63  {255, 0, 64}
64};
65
66static int rainbow_color;
67static Uint32 rainbow_rgb;
68static Mix_Chunk * rainbow_snd;
69
70int rainbow_init(magic_api * api);
71Uint32 rainbow_api_version(void);
72int rainbow_get_tool_count(magic_api * api);
73SDL_Surface * rainbow_get_icon(magic_api * api, int which);
74char * rainbow_get_name(magic_api * api, int which);
75char * rainbow_get_description(magic_api * api, int which, int mode);
76static void rainbow_linecb(void * ptr, int which,
77                    SDL_Surface * canvas, SDL_Surface * last,
78                    int x, int y);
79
80void rainbow_drag(magic_api * api, int which, SDL_Surface * canvas,
81                  SDL_Surface * last, int ox, int oy, int x, int y,
82                  SDL_Rect * update_rect);
83
84void rainbow_click(magic_api * api, int which, int mode,
85                   SDL_Surface * canvas, SDL_Surface * last,
86                   int x, int y,
87                   SDL_Rect * update_rect);
88
89void rainbow_release(magic_api * api, int which,
90                   SDL_Surface * canvas, SDL_Surface * last,
91                   int x, int y,
92                   SDL_Rect * update_rect);
93
94
95void rainbow_shutdown(magic_api * api);
96void rainbow_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b);
97int rainbow_requires_colors(magic_api * api, int which);
98void rainbow_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas);
99void rainbow_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas);
100int rainbow_modes(magic_api * api, int which);
101
102Uint32 rainbow_api_version(void) { return(TP_MAGIC_API_VERSION); }
103
104// Load our sfx:
105int rainbow_init(magic_api * api)
106{
107  char fname[1024];
108
109
110  rainbow_color = 0;
111
112  snprintf(fname, sizeof(fname), "%s/sounds/magic/rainbow.wav",
113           api->data_directory);
114  rainbow_snd = Mix_LoadWAV(fname);
115
116  return(1);
117}
118
119// We have multiple tools:
120int rainbow_get_tool_count(magic_api * api ATTRIBUTE_UNUSED)
121{
122  return(1);
123}
124
125// Load our icons:
126SDL_Surface * rainbow_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED)
127{
128  char fname[1024];
129
130  snprintf(fname, sizeof(fname), "%s/images/magic/rainbow.png",
131           api->data_directory);
132
133  return(IMG_Load(fname));
134}
135
136// Return our names, localized:
137char * rainbow_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
138{
139  return(strdup(gettext_noop("Rainbow")));
140}
141
142// Return our descriptions, localized:
143char * rainbow_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED)
144{
145  return(strdup(
146           gettext_noop("You can draw in rainbow colors!")));
147}
148
149// Do the effect:
150
151static void rainbow_linecb(void * ptr, int which ATTRIBUTE_UNUSED,
152                    SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED,
153                    int x, int y)
154{
155  magic_api * api = (magic_api *) ptr;
156  int xx, yy;
157
158  for (yy = y - 16; yy < y + 16; yy++)
159  {
160    for (xx = x - 16; xx < x + 16; xx++)
161    {
162      if (api->in_circle(xx - x, yy - y, 16))
163      {
164        api->putpixel(canvas, xx, yy, rainbow_rgb);
165      }
166    }
167  }
168}
169
170// Affect the canvas on drag:
171void rainbow_drag(magic_api * api, int which, SDL_Surface * canvas,
172                  SDL_Surface * last, int ox, int oy, int x, int y,
173                  SDL_Rect * update_rect)
174{
175  rainbow_color = (rainbow_color + 1) % NUM_RAINBOW_COLORS;
176  rainbow_rgb = SDL_MapRGB(canvas->format,
177                           rainbow_hexes[rainbow_color][0],
178                           rainbow_hexes[rainbow_color][1],
179                           rainbow_hexes[rainbow_color][2]);
180
181  api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, rainbow_linecb);
182
183  if (ox > x) { int tmp = ox; ox = x; x = tmp; }
184  if (oy > y) { int tmp = oy; oy = y; y = tmp; }
185
186  update_rect->x = ox - 16;
187  update_rect->y = oy - 16;
188  update_rect->w = (x + 16) - update_rect->x;
189  update_rect->h = (y + 16) - update_rect->y;
190
191  api->playsound(rainbow_snd, (x * 255) / canvas->w, 255);
192}
193
194// Affect the canvas on click:
195void rainbow_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED,
196                   SDL_Surface * canvas, SDL_Surface * last,
197                   int x, int y,
198                   SDL_Rect * update_rect)
199{
200  rainbow_drag(api, which, canvas, last, x, y, x, y, update_rect);
201}
202
203void rainbow_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED,
204                   SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED,
205                   int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED,
206                   SDL_Rect * update_rect ATTRIBUTE_UNUSED)
207{
208}
209
210// Clean up
211void rainbow_shutdown(magic_api * api ATTRIBUTE_UNUSED)
212{
213  if (rainbow_snd != NULL)
214    Mix_FreeChunk(rainbow_snd);
215}
216
217// Record the color from Tux Paint:
218void rainbow_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED)
219{
220}
221
222// Use colors:
223int rainbow_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
224{
225  return 0;
226}
227
228void rainbow_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED)
229{
230}
231
232void rainbow_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED)
233{
234}
235
236int rainbow_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
237{
238  return(MODE_PAINT);
239}
Note: See TracBrowser for help on using the repository browser.