Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/magic/src/halftone.c
blob: 92bb8479c25263968a6ac6ac75edc6c2b2cb794f (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* halftone.c

   Last modified: 2011.07.15
*/


/* Inclusion of header files: */
/* -------------------------- */

#include <stdio.h>
#include <string.h>
#include <libintl.h>

#include "tp_magic_api.h"
#include "SDL_image.h"
#include "SDL_mixer.h"

enum {
  TOOL_HALFTONE,
  NUM_TOOLS
};


const char * snd_filenames[NUM_TOOLS] = {
  "halftone.wav",
};

const char * icon_filenames[NUM_TOOLS] = {
  "halftone.png",
};

const char * names[NUM_TOOLS] = {
  gettext_noop("Halftone"),
};

const char * descs[NUM_TOOLS] = {
  gettext_noop("Click and drag to turn your drawing into a newspaper."),
};

Mix_Chunk * snd_effect[NUM_TOOLS];

static SDL_Surface * canvas_backup;

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


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

int halftone_init(magic_api * api)
{
  int i;
  char fname[1024];

  for (i = 0; i < NUM_TOOLS; i++)
  {
    snprintf(fname, sizeof(fname),
             "%s/sounds/magic/%s",
	     api->data_directory, snd_filenames[i]);

/* FIXME    snd_effect[i] = Mix_LoadWAV(fname); */
  }

  return(1);
}

int halftone_get_tool_count(magic_api * api)
{
  return(NUM_TOOLS);
}

SDL_Surface * halftone_get_icon(magic_api * api, int which)
{
  char fname[1024];

  snprintf(fname, sizeof(fname), "%s/images/magic/%s.png",
	     api->data_directory, icon_filenames[which]);

  return(IMG_Load(fname));
}

char * halftone_get_name(magic_api * api, int which)
{
  const char * our_name_english;
  const char * our_name_localized;

  our_name_english = names[which];
  our_name_localized = gettext(our_name_english);

  return(strdup(our_name_localized));
}

char * halftone_get_description(magic_api * api, int which, int mode)
{
  const char * our_desc_english;
  const char * our_desc_localized;

  our_desc_english = descs[which];
  our_desc_localized = gettext(our_desc_english);

  return(strdup(our_desc_localized));
}

int halftone_requires_colors(magic_api * api, int which)
{
  return 0;
}

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

void halftone_shutdown(magic_api * api)
{
  int i;

  for (i = 0; i < NUM_TOOLS; i++)
    Mix_FreeChunk(snd_effect[i]);

  SDL_FreeSurface(canvas_backup);
}

void halftone_click(magic_api * api, int which, int mode,
	           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)
{
  api->line((void *) api, which, canvas, snapshot,
            ox, oy, x, y, 1, halftone_line_callback);

  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;

/* FIXME
  api->playsound(snd_effect[which],
                 (x * 255) / canvas->w, // pan
	         255); // distance
*/
}

enum {
  CHAN_CYAN,
  CHAN_MAGENTA,
  CHAN_YELLOW,
  CHAN_BLACK,
  NUM_CHANS
};

Uint8 chan_colors[NUM_CHANS][3] = {
  {   0, 255, 255 },
  { 255,   0, 255 },
  { 255, 255,   0 },
  {   0,   0,   0 }
};

int chan_angles[NUM_CHANS] = {
  100,
  15,
  0,
  45
};

void halftone_release(magic_api * api, int which,
	           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_line_callback(void * ptr, int which,
                           SDL_Surface * canvas, SDL_Surface * snapshot,
                           int x, int y)
{
  Uint8 r, g, b;
  Uint32 total_r, total_g, total_b;
  Uint32 pixel;
  int xx, yy, xxx, yyy, channel;
  SDL_Rect dest;
  magic_api * api = (magic_api *) ptr;

  pixel = SDL_MapRGB(canvas->format, 255, 255, 255);
  dest.x = x - 8;
  dest.y = y - 8;
  dest.w = 16;
  dest.h = 16;
  SDL_FillRect(canvas, &dest, pixel);

  for (xx = x - 8; xx < x + 8; xx = xx + 4) {
    for (yy = y - 8; yy < y + 8; yy = yy + 4) {
      total_r = total_g = total_b = 0;
      for (xxx = xx; xxx < xx + 4; xxx++) {
        for (yyy = xx; yyy < xx + 4; yyy++) {
          SDL_GetRGB(api->getpixel(canvas_backup, x, y), canvas_backup->format, &r, &g, &b);
          total_r += r;
          total_g += g;
          total_b += b;
        }
      }
      total_r /= 16;
      total_g /= 16;
      total_b /= 16;

      /* FIXME: Do some magic here! */
      for (channel = 0; channel < NUM_CHANS; channel++) {
        r = total_r & chan_colors[channel][0];
        g = total_g & chan_colors[channel][2];
        b = total_b & chan_colors[channel][1];

        pixel = SDL_MapRGB(canvas->format, r, g, b);
        for (xxx = xx; xxx < xx + 4; xxx++) {
          for (yyy = yy; yyy < yy + 4; yyy++) {
            api->putpixel(canvas, xxx, yyy, pixel);
          }
        }
      }
    }
  }
}

void halftone_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas)
{
  canvas_backup=SDL_CreateRGBSurface(SDL_ANYFORMAT, 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);
}

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