1 /*
2 mirror_flip.c
3
4 Mirror and Flip Magic Tools 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$
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 /* What tools we contain: */
37
38 enum
39 {
40 TOOL_MIRROR,
41 TOOL_FLIP,
42 NUM_TOOLS
43 };
44
45 static Mix_Chunk *snd_effects[NUM_TOOLS];
46
47 /* Prototypes */
48 int mirror_flip_init(magic_api *);
49 Uint32 mirror_flip_api_version(void);
50 int mirror_flip_get_tool_count(magic_api *);
51 SDL_Surface *mirror_flip_get_icon(magic_api *, int);
52 char *mirror_flip_get_name(magic_api *, int);
53 char *mirror_flip_get_description(magic_api *, int, int);
54 void mirror_flip_drag(magic_api *, int, SDL_Surface *, SDL_Surface *, int, int, int, int, SDL_Rect *);
55 void mirror_flip_release(magic_api *, int, SDL_Surface *, SDL_Surface *, int, int, int, int, SDL_Rect *);
56 void mirror_flip_click(magic_api *, int, int, SDL_Surface *, SDL_Surface *, int, int, SDL_Rect *);
57 void mirror_flip_shutdown(magic_api *);
58 void mirror_flip_set_color(magic_api *, Uint8, Uint8, Uint8);
59 int mirror_flip_requires_colors(magic_api *, int);
60 void mirror_flip_switchin(magic_api *, int, int, SDL_Surface *);
61 void mirror_flip_switchout(magic_api *, int, int, SDL_Surface *);
62 int mirror_flip_modes(magic_api *, int);
63
64 // No setup required:
mirror_flip_init(magic_api * api)65 int mirror_flip_init(magic_api * api)
66 {
67 char fname[1024];
68
69 snprintf(fname, sizeof(fname), "%s/sounds/magic/mirror.wav", api->data_directory);
70 snd_effects[TOOL_MIRROR] = Mix_LoadWAV(fname);
71
72 snprintf(fname, sizeof(fname), "%s/sounds/magic/flip.wav", api->data_directory);
73 snd_effects[TOOL_FLIP] = Mix_LoadWAV(fname);
74
75 return (1);
76 }
77
mirror_flip_api_version(void)78 Uint32 mirror_flip_api_version(void)
79 {
80 return (TP_MAGIC_API_VERSION);
81 }
82
83 // We have multiple tools:
mirror_flip_get_tool_count(magic_api * api ATTRIBUTE_UNUSED)84 int mirror_flip_get_tool_count(magic_api * api ATTRIBUTE_UNUSED)
85 {
86 return (NUM_TOOLS);
87 }
88
89 // Load our icons:
mirror_flip_get_icon(magic_api * api,int which)90 SDL_Surface *mirror_flip_get_icon(magic_api * api, int which)
91 {
92 char fname[1024];
93
94 if (which == TOOL_MIRROR)
95 {
96 snprintf(fname, sizeof(fname), "%s/images/magic/mirror.png", api->data_directory);
97 }
98 else if (which == TOOL_FLIP)
99 {
100 snprintf(fname, sizeof(fname), "%s/images/magic/flip.png", api->data_directory);
101 }
102
103 return (IMG_Load(fname));
104 }
105
106 // Return our names, localized:
mirror_flip_get_name(magic_api * api ATTRIBUTE_UNUSED,int which)107 char *mirror_flip_get_name(magic_api * api ATTRIBUTE_UNUSED, int which)
108 {
109 if (which == TOOL_MIRROR)
110 return (strdup(gettext_noop("Mirror")));
111 else if (which == TOOL_FLIP)
112 return (strdup(gettext_noop("Flip")));
113
114 return (NULL);
115 }
116
117 // Return our descriptions, localized:
mirror_flip_get_description(magic_api * api ATTRIBUTE_UNUSED,int which,int mode ATTRIBUTE_UNUSED)118 char *mirror_flip_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode ATTRIBUTE_UNUSED)
119 {
120 if (which == TOOL_MIRROR)
121 return (strdup(gettext_noop("Click to make a mirror image.")));
122 else
123 return (strdup(gettext_noop("Click to flip the picture upside-down.")));
124
125 return (NULL);
126 }
127
128 // We affect the whole canvas, so only do things on click, not drag:
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)129 void mirror_flip_drag(magic_api * api ATTRIBUTE_UNUSED,
130 int which ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED,
131 SDL_Surface * last ATTRIBUTE_UNUSED,
132 int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED,
133 int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED)
134 {
135 // No-op
136 }
137
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)138 void mirror_flip_release(magic_api * api ATTRIBUTE_UNUSED,
139 int which ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED,
140 SDL_Surface * last ATTRIBUTE_UNUSED,
141 int ox ATTRIBUTE_UNUSED, int oy ATTRIBUTE_UNUSED,
142 int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED)
143 {
144 // No-op
145 }
146
147 // Affect the canvas on click:
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)148 void mirror_flip_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED,
149 SDL_Surface * canvas, SDL_Surface * last,
150 int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect)
151 {
152 int xx, yy;
153 SDL_Rect src, dest;
154
155 if (which == TOOL_MIRROR)
156 {
157 for (xx = 0; xx < canvas->w; xx++)
158 {
159 src.x = xx;
160 src.y = 0;
161 src.w = 1;
162 src.h = canvas->h;
163
164 dest.x = canvas->w - xx - 1;
165 dest.y = 0;
166
167 SDL_BlitSurface(last, &src, canvas, &dest);
168 }
169
170 api->special_notify(SPECIAL_MIRROR);
171 }
172 else if (which == TOOL_FLIP)
173 {
174 for (yy = 0; yy < canvas->h; yy++)
175 {
176 src.x = 0;
177 src.y = yy;
178 src.w = canvas->w;
179 src.h = 1;
180
181 dest.x = 0;
182 dest.y = canvas->h - yy - 1;
183
184 SDL_BlitSurface(last, &src, canvas, &dest);
185 }
186
187 api->special_notify(SPECIAL_FLIP);
188 }
189
190 update_rect->x = 0;
191 update_rect->y = 0;
192 update_rect->w = canvas->w;
193 update_rect->h = canvas->h;
194
195 api->playsound(snd_effects[which], 128, 255);
196 }
197
198 // No setup happened:
mirror_flip_shutdown(magic_api * api ATTRIBUTE_UNUSED)199 void mirror_flip_shutdown(magic_api * api ATTRIBUTE_UNUSED)
200 {
201 if (snd_effects[0] != NULL)
202 Mix_FreeChunk(snd_effects[0]);
203 if (snd_effects[1] != NULL)
204 Mix_FreeChunk(snd_effects[1]);
205 }
206
207 // We don't use colors:
mirror_flip_set_color(magic_api * api ATTRIBUTE_UNUSED,Uint8 r ATTRIBUTE_UNUSED,Uint8 g ATTRIBUTE_UNUSED,Uint8 b ATTRIBUTE_UNUSED)208 void mirror_flip_set_color(magic_api * api ATTRIBUTE_UNUSED,
209 Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED)
210 {
211 }
212
213 // We don't use colors:
mirror_flip_requires_colors(magic_api * api ATTRIBUTE_UNUSED,int which ATTRIBUTE_UNUSED)214 int mirror_flip_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
215 {
216 return 0;
217 }
218
mirror_flip_switchin(magic_api * api ATTRIBUTE_UNUSED,int which ATTRIBUTE_UNUSED,int mode ATTRIBUTE_UNUSED,SDL_Surface * canvas ATTRIBUTE_UNUSED)219 void mirror_flip_switchin(magic_api * api ATTRIBUTE_UNUSED,
220 int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED)
221 {
222 }
223
mirror_flip_switchout(magic_api * api ATTRIBUTE_UNUSED,int which ATTRIBUTE_UNUSED,int mode ATTRIBUTE_UNUSED,SDL_Surface * canvas ATTRIBUTE_UNUSED)224 void mirror_flip_switchout(magic_api * api ATTRIBUTE_UNUSED,
225 int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED)
226 {
227 }
228
mirror_flip_modes(magic_api * api ATTRIBUTE_UNUSED,int which ATTRIBUTE_UNUSED)229 int mirror_flip_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
230 {
231 return (MODE_FULLSCREEN);
232 }
233