Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/magic/src/kalidescope.c
blob: 4ab84bb08aa54f5938edee4e327c904d43e514a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
  kalidescope.c

  Kaleidoscope Magic Tool Plugin
  Tux Paint - A simple drawing program for children.

  Copyright (c) 2002-2008 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: July 8, 2008
  $Id: kalidescope.c,v 1.12 2010/06/05 01:39:09 wkendrick Exp $
*/

#include <stdio.h>
#include <string.h>
#include "tp_magic_api.h"
#include "SDL_image.h"
#include "SDL_mixer.h"

/* Our globals: */

static Mix_Chunk * kalidescope_snd;
static Uint8 kalidescope_r, kalidescope_g, kalidescope_b;


Uint32 kalidescope_api_version(void) { return(TP_MAGIC_API_VERSION); }

enum {
  KAL_UD,
  KAL_LR,
  KAL_BOTH,
  KAL_COUNT
};

char * kal_icon_names[KAL_COUNT] = {
  "symmetric_updown.png",
  "symmetric_leftright.png",
  "kalidescope.png",
};


// No setup required:
int kalidescope_init(magic_api * api)
{
  char fname[1024];

  snprintf(fname, sizeof(fname), "%s/sounds/magic/kaleidoscope.ogg",
	    api->data_directory);
  kalidescope_snd = Mix_LoadWAV(fname);

  return(1);
}

int kalidescope_get_tool_count(magic_api * api)
{
  return(KAL_COUNT);
}

// Load our icons:
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]);

  return(IMG_Load(fname));
}

// Return our names, localized:
char * kalidescope_get_name(magic_api * api, 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 { /* KAL_BOTH */
    return(strdup(gettext_noop("Kaleidoscope")));
  }
}

// Return our descriptions, localized:
char * kalidescope_get_description(magic_api * api, int which, int mode)
{
  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 { /* 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,
                int x, int y)
{
  magic_api * api = (magic_api *) ptr;
  int xx, yy;
  Uint32 colr;

  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);

        if (which == KAL_LR || which == KAL_BOTH) {
          api->putpixel(canvas, canvas->w - 1 - x + xx, y + yy, colr);

          if (which == KAL_BOTH) {
            api->putpixel(canvas, canvas->w - 1 - x + xx, canvas->h - 1 - y + yy, colr);
          }
        }
        if (which == KAL_UD || which == KAL_BOTH) {
          api->putpixel(canvas, x + xx, canvas->h - 1 - y + yy, colr);
        }
      }
    }
  }
}

// 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)
{
  api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, do_kalidescope);

  update_rect->x = 0;
  update_rect->y = 0;
  update_rect->w = canvas->w;
  update_rect->h = canvas->h;

  api->playsound(kalidescope_snd, 128, 255);
}

// Affect the canvas on click:
void kalidescope_click(magic_api * api, int which, int mode,
	           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,
	           SDL_Surface * canvas, SDL_Surface * last,
	           int x, int y, SDL_Rect * update_rect)
{
  api->stopsound();
}

// No setup happened:
void kalidescope_shutdown(magic_api * api)
{
  if (kalidescope_snd != NULL)
    Mix_FreeChunk(kalidescope_snd);
}

// Record the color from Tux Paint:
void kalidescope_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b)
{
  kalidescope_r = r;
  kalidescope_g = g;
  kalidescope_b = b;
}

// Use colors:
int kalidescope_requires_colors(magic_api * api, int which)
{
  return 1;
}

void kalidescope_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas)
{
}

void kalidescope_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas)
{
}

int kalidescope_modes(magic_api * api, int which)
{
  return(MODE_PAINT);
}