WIP "Spiral" & "Square Spiral" magic tools
Needs docs, icons, sound effects, and correct grouping/ordering. For https://sourceforge.net/p/tuxpaint/feature-requests/261/
This commit is contained in:
parent
f296e98acf
commit
cc8b903850
134 changed files with 3590 additions and 265 deletions
351
magic/src/spiral.c
Normal file
351
magic/src/spiral.c
Normal file
|
|
@ -0,0 +1,351 @@
|
|||
/*
|
||||
spiral.c
|
||||
|
||||
Draws a spiral shape centered around the mouse click point.
|
||||
|
||||
Tux Paint - A simple drawing program for children.
|
||||
|
||||
Copyright (c) 2024 by Bill Kendrick
|
||||
|
||||
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: October 3, 2024
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "tp_magic_api.h"
|
||||
#include "SDL_image.h"
|
||||
#include "SDL_mixer.h"
|
||||
|
||||
enum {
|
||||
TOOL_SPIRAL_CIRCLE,
|
||||
TOOL_SPIRAL_SQUARE,
|
||||
NUM_TOOLS
|
||||
};
|
||||
|
||||
const char * spiral_names[NUM_TOOLS] = {
|
||||
gettext_noop("Spiral"),
|
||||
gettext_noop("Square Spiral"),
|
||||
};
|
||||
|
||||
const char * spiral_descrs[NUM_TOOLS] = {
|
||||
gettext_noop("Click and drag to create a spiral."),
|
||||
gettext_noop("Click and drag to create a square spiral."),
|
||||
};
|
||||
|
||||
static Mix_Chunk *spiral_snd;
|
||||
static int spiral_thickness = 2;
|
||||
Uint32 spiral_color;
|
||||
int spiral_cx, spiral_cy, spiral_has_dragged;
|
||||
|
||||
/* When first clicking, and when click-and-release-ing
|
||||
(without dragging), we'll draw a default spiral that
|
||||
is `MIN_RADIUS` * `sprial_thickness` in radius. */
|
||||
#define MIN_RADIUS 32
|
||||
|
||||
/* Angle will be the radius multiplied by `ITER` divided
|
||||
by `spiral_thickness`; the larger the thickness, the
|
||||
fewer the iterations (the slower the spiral will go around) */
|
||||
#define ITER 50
|
||||
|
||||
Uint32 spiral_api_version(void);
|
||||
int spiral_init(magic_api * api, Uint8 disabled_features, Uint8 complexity_level);
|
||||
int spiral_get_tool_count(magic_api * api);
|
||||
SDL_Surface *spiral_get_icon(magic_api * api, int which);
|
||||
char *spiral_get_name(magic_api * api, int which);
|
||||
int spiral_get_group(magic_api * api, int which);
|
||||
int spiral_get_order(int which);
|
||||
char *spiral_get_description(magic_api * api, int which, int mode);
|
||||
|
||||
void spiral_drag(magic_api * api, int which, SDL_Surface * canvas,
|
||||
SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect);
|
||||
|
||||
void spiral_click(magic_api * api, int which, int mode,
|
||||
SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect);
|
||||
|
||||
void spiral_release(magic_api * api, int which,
|
||||
SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect);
|
||||
|
||||
void spiral_shutdown(magic_api * api);
|
||||
void spiral_set_color(magic_api * api, int which, SDL_Surface * canvas,
|
||||
SDL_Surface * last, Uint8 r, Uint8 g, Uint8 b, SDL_Rect * update_rect);
|
||||
int spiral_requires_colors(magic_api * api, int which);
|
||||
void spiral_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas);
|
||||
void spiral_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas);
|
||||
int spiral_modes(magic_api * api, int which);
|
||||
Uint8 spiral_accepted_sizes(magic_api * api, int which, int mode);
|
||||
Uint8 spiral_default_size(magic_api * api, int which, int mode);
|
||||
void spiral_set_size(magic_api * api, int which, int mode, SDL_Surface * canvas, SDL_Surface * last, Uint8 size,
|
||||
SDL_Rect * update_rect);
|
||||
|
||||
|
||||
Uint32 spiral_api_version(void)
|
||||
{
|
||||
return (TP_MAGIC_API_VERSION);
|
||||
}
|
||||
|
||||
int spiral_init(magic_api * api, Uint8 disabled_features ATTRIBUTE_UNUSED, Uint8 complexity_level ATTRIBUTE_UNUSED)
|
||||
{
|
||||
char fname[1024];
|
||||
|
||||
snprintf(fname, sizeof(fname), "%ssounds/magic/xor.ogg", api->data_directory);
|
||||
spiral_snd = Mix_LoadWAV(fname);
|
||||
|
||||
return (1);
|
||||
}
|
||||
|
||||
int spiral_get_tool_count(magic_api * api ATTRIBUTE_UNUSED)
|
||||
{
|
||||
return (NUM_TOOLS);
|
||||
}
|
||||
|
||||
SDL_Surface *spiral_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED)
|
||||
{
|
||||
char fname[1024];
|
||||
|
||||
snprintf(fname, sizeof(fname), "%simages/magic/xor.png", api->data_directory);
|
||||
|
||||
return (IMG_Load(fname));
|
||||
}
|
||||
|
||||
char *spiral_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
|
||||
{
|
||||
return (strdup(gettext(spiral_names[which])));
|
||||
}
|
||||
|
||||
int spiral_get_group(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
|
||||
{
|
||||
return MAGIC_TYPE_COLOR_FILTERS;
|
||||
}
|
||||
|
||||
int spiral_get_order(int which ATTRIBUTE_UNUSED)
|
||||
{
|
||||
return 800;
|
||||
}
|
||||
|
||||
char *spiral_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode)
|
||||
{
|
||||
return (strdup(gettext(spiral_descrs[which])));
|
||||
}
|
||||
|
||||
static void do_spiral_render(void *ptr, int which,
|
||||
SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y)
|
||||
{
|
||||
magic_api *api = (magic_api *) ptr;
|
||||
SDL_Rect dest;
|
||||
int thick;
|
||||
|
||||
thick = spiral_thickness * 4;
|
||||
|
||||
if (which == TOOL_SPIRAL_CIRCLE)
|
||||
{
|
||||
int xx, yy;
|
||||
|
||||
for (yy = -thick / 2; yy <= thick / 2; yy++)
|
||||
for (xx = -thick / 2; xx <= thick / 2; xx++)
|
||||
if (api->in_circle(xx, yy, thick / 2))
|
||||
api->putpixel(canvas, x + xx, y + yy, spiral_color);
|
||||
}
|
||||
else if (which == TOOL_SPIRAL_SQUARE)
|
||||
{
|
||||
dest.x = x - thick / 2;
|
||||
dest.y = y - thick / 2;
|
||||
dest.w = thick;
|
||||
dest.h = thick;
|
||||
|
||||
SDL_FillRect(canvas, &dest, spiral_color);
|
||||
}
|
||||
}
|
||||
|
||||
void do_spiral(magic_api * api, int which, SDL_Surface * canvas,
|
||||
SDL_Surface * last, int x, int y, SDL_Rect * update_rect,
|
||||
int final)
|
||||
{
|
||||
float radius, i, xx, yy, xsgn, ysgn, ox, oy, stp, oxx, oyy;
|
||||
|
||||
SDL_BlitSurface(last, NULL, canvas, NULL);
|
||||
|
||||
if (x < spiral_cx)
|
||||
xsgn = -1;
|
||||
else
|
||||
xsgn = 1;
|
||||
|
||||
if (y < spiral_cy)
|
||||
ysgn = -1;
|
||||
else
|
||||
ysgn = 1;
|
||||
|
||||
if (final)
|
||||
stp = 0.1;
|
||||
else
|
||||
stp = 0.5;
|
||||
|
||||
if (which == TOOL_SPIRAL_CIRCLE)
|
||||
{
|
||||
radius = sqrt(pow((x - spiral_cx), 2) + pow((y - spiral_cy), 2));
|
||||
|
||||
oxx = 0;
|
||||
oyy = 0;
|
||||
|
||||
for (i = 0; i < radius; i += stp)
|
||||
{
|
||||
xx = (i * cos((i * (ITER / spiral_thickness)) / 180.0 * M_PI)) * xsgn;
|
||||
yy = (i * sin((i * (ITER / spiral_thickness)) / 180.0 * M_PI)) * ysgn;
|
||||
if (final)
|
||||
{
|
||||
api->line((void *) api, which, canvas, NULL,
|
||||
((int) oxx) + spiral_cx, ((int) oyy) + spiral_cy,
|
||||
((int) xx) + spiral_cx, ((int) yy) + spiral_cy,
|
||||
1, do_spiral_render);
|
||||
oxx = xx;
|
||||
oyy = yy;
|
||||
}
|
||||
else
|
||||
{
|
||||
do_spiral_render(api, which, canvas, NULL, ((int) xx) + spiral_cx, ((int) yy) + spiral_cy);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (which == TOOL_SPIRAL_SQUARE)
|
||||
{
|
||||
int dir, oi, ooi, ix, iy;
|
||||
|
||||
radius = max(abs(x - spiral_cx), abs(y - spiral_cy));
|
||||
|
||||
dir = 0;
|
||||
ooi = 0;
|
||||
oi = 0;
|
||||
for (i = spiral_thickness; i < radius; i += spiral_thickness * 2)
|
||||
{
|
||||
if (dir == 0) // right
|
||||
api->line((void *) api, which, canvas, NULL,
|
||||
spiral_cx - (ooi * xsgn), spiral_cy - (oi * ysgn),
|
||||
spiral_cx + (i * xsgn), spiral_cy - (oi * ysgn),
|
||||
1, do_spiral_render);
|
||||
else if (dir == 1) // down
|
||||
api->line((void *) api, which, canvas, NULL,
|
||||
spiral_cx + (oi * xsgn), spiral_cy - (ooi * ysgn),
|
||||
spiral_cx + (oi * xsgn), spiral_cy + (i * ysgn),
|
||||
1, do_spiral_render);
|
||||
else if (dir == 2) // left
|
||||
api->line((void *) api, which, canvas, NULL,
|
||||
spiral_cx + (ooi * xsgn), spiral_cy + (oi * ysgn),
|
||||
spiral_cx - (i * xsgn), spiral_cy + (oi * ysgn),
|
||||
1, do_spiral_render);
|
||||
else if (dir == 3) // up
|
||||
api->line((void *) api, which, canvas, NULL,
|
||||
spiral_cx - (oi * xsgn), spiral_cy + (ooi * ysgn),
|
||||
spiral_cx - (oi * xsgn), spiral_cy - (i * ysgn),
|
||||
1, do_spiral_render);
|
||||
|
||||
dir = (dir + 1) % 4;
|
||||
|
||||
ooi = oi;
|
||||
oi = i;
|
||||
}
|
||||
}
|
||||
|
||||
/* FIXME */
|
||||
update_rect->x = 0;
|
||||
update_rect->y = 0;
|
||||
update_rect->w = canvas->w;
|
||||
update_rect->h = canvas->h;
|
||||
|
||||
api->playsound(spiral_snd, (x * 255) / canvas->w, 255);
|
||||
}
|
||||
|
||||
void spiral_drag(magic_api * api, int which, SDL_Surface * canvas,
|
||||
SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect)
|
||||
{
|
||||
spiral_has_dragged = 1;
|
||||
do_spiral(api, which, canvas, last, x, y, update_rect, 0);
|
||||
}
|
||||
|
||||
void spiral_click(magic_api * api, int which, int mode,
|
||||
SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect)
|
||||
{
|
||||
spiral_cx = x;
|
||||
spiral_cy = y;
|
||||
|
||||
spiral_drag(api, which, canvas, last, x, y, x + (spiral_thickness * MIN_RADIUS), y, update_rect);
|
||||
|
||||
spiral_has_dragged = 0;
|
||||
}
|
||||
|
||||
void spiral_release(magic_api * api, int which,
|
||||
SDL_Surface * canvas,
|
||||
SDL_Surface * last , int x,
|
||||
int y, SDL_Rect * update_rect)
|
||||
{
|
||||
float radius;
|
||||
|
||||
radius = sqrt(pow((x - spiral_cx), 2) + pow((y - spiral_cy), 2));
|
||||
if (radius < spiral_thickness * MIN_RADIUS && spiral_has_dragged == 0)
|
||||
x = spiral_cx + (spiral_thickness * MIN_RADIUS);
|
||||
|
||||
do_spiral(api, which, canvas, last, x, y, update_rect, 1);
|
||||
}
|
||||
|
||||
void spiral_shutdown(magic_api * api ATTRIBUTE_UNUSED)
|
||||
{
|
||||
if (spiral_snd != NULL)
|
||||
Mix_FreeChunk(spiral_snd);
|
||||
}
|
||||
|
||||
void spiral_set_color(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas,
|
||||
SDL_Surface * last ATTRIBUTE_UNUSED, Uint8 r, Uint8 g, Uint8 b, SDL_Rect * update_rect ATTRIBUTE_UNUSED)
|
||||
{
|
||||
spiral_color = SDL_MapRGB(canvas->format, r, g, b);
|
||||
}
|
||||
|
||||
int spiral_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
void spiral_switchin(magic_api * api ATTRIBUTE_UNUSED,
|
||||
int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED)
|
||||
{
|
||||
}
|
||||
|
||||
void spiral_switchout(magic_api * api ATTRIBUTE_UNUSED,
|
||||
int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED)
|
||||
{
|
||||
}
|
||||
|
||||
int spiral_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
|
||||
{
|
||||
return MODE_PAINT;
|
||||
}
|
||||
|
||||
|
||||
Uint8 spiral_accepted_sizes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode)
|
||||
{
|
||||
return 8;
|
||||
}
|
||||
|
||||
Uint8 spiral_default_size(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED)
|
||||
{
|
||||
return 4;
|
||||
}
|
||||
|
||||
void spiral_set_size(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED,
|
||||
SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED,
|
||||
Uint8 size ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED)
|
||||
{
|
||||
spiral_thickness = size;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue