indent tint.c

This commit is contained in:
Bill Kendrick 2017-10-15 11:51:06 -07:00
parent d311aeb236
commit 5871ef9b3a

View file

@ -46,7 +46,8 @@
#define gettext_noop(String) String #define gettext_noop(String) String
#endif #endif
enum { enum
{
TOOL_TINT, TOOL_TINT,
TOOL_THRESHOLD, TOOL_THRESHOLD,
tint_NUM_TOOLS tint_NUM_TOOLS
@ -55,23 +56,26 @@ static Uint8 tint_r, tint_g, tint_b;
static int tint_min = INT_MAX; static int tint_min = INT_MAX;
static int tint_max = 0; static int tint_max = 0;
static const int tint_RADIUS =16; static const int tint_RADIUS = 16;
static Mix_Chunk * tint_snd_effect[tint_NUM_TOOLS]; static Mix_Chunk *tint_snd_effect[tint_NUM_TOOLS];
const char * tint_snd_filenames[tint_NUM_TOOLS] = { const char *tint_snd_filenames[tint_NUM_TOOLS] = {
"tint.wav", "tint.wav",
"fold.ogg" /* FIXME */ "fold.ogg" /* FIXME */
}; };
const char * tint_icon_filenames[tint_NUM_TOOLS] = {
const char *tint_icon_filenames[tint_NUM_TOOLS] = {
"tint.png", "tint.png",
"colornwhite.png" "colornwhite.png"
}; };
const char * tint_names[tint_NUM_TOOLS] = {
const char *tint_names[tint_NUM_TOOLS] = {
gettext_noop("Tint"), gettext_noop("Tint"),
gettext_noop("Color & White") // It does more than this but more intuitive than threshold. gettext_noop("Color & White") // It does more than this but more intuitive than threshold.
}; };
const char * tint_descs[tint_NUM_TOOLS][2] = {
const char *tint_descs[tint_NUM_TOOLS][2] = {
{gettext_noop("Click and drag the mouse around to change the color of parts of your picture."), {gettext_noop("Click and drag the mouse around to change the color of parts of your picture."),
gettext_noop("Click to change the color of your entire picture."),}, gettext_noop("Click to change the color of your entire picture."),},
{gettext_noop("Click and drag the mouse around to turn parts of your picture into white and a color you choose."), {gettext_noop("Click and drag the mouse around to turn parts of your picture into white and a color you choose."),
@ -81,24 +85,19 @@ const char * tint_descs[tint_NUM_TOOLS][2] = {
int tint_init(magic_api * api); int tint_init(magic_api * api);
Uint32 tint_api_version(void); Uint32 tint_api_version(void);
int tint_get_tool_count(magic_api * api); int tint_get_tool_count(magic_api * api);
SDL_Surface * tint_get_icon(magic_api * api, int which); SDL_Surface *tint_get_icon(magic_api * api, int which);
char * tint_get_name(magic_api * api, int which); char *tint_get_name(magic_api * api, int which);
char * tint_get_description(magic_api * api, int which, int mode); char *tint_get_description(magic_api * api, int which, int mode);
static int tint_grey(Uint8 r1,Uint8 g1,Uint8 b1); static int tint_grey(Uint8 r1, Uint8 g1, Uint8 b1);
static void do_tint_pixel(void * ptr, int which, static void do_tint_pixel(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y);
SDL_Surface * canvas, SDL_Surface * last, static void do_tint_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int which);
int x, int y); static void do_tint_brush(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y);
static void do_tint_full(void * ptr,SDL_Surface * canvas, SDL_Surface * last, int which);
static void do_tint_brush(void * ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y);
void tint_drag(magic_api * api, int which, SDL_Surface * canvas, void tint_drag(magic_api * api, int which, SDL_Surface * canvas,
SDL_Surface * last, int ox, int oy, int x, int y, SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect);
SDL_Rect * update_rect);
void tint_click(magic_api * api, int which, int mode, void tint_click(magic_api * api, int which, int mode,
SDL_Surface * canvas, SDL_Surface * last, SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect);
int x, int y, SDL_Rect * update_rect);
void tint_release(magic_api * api, int which, void tint_release(magic_api * api, int which,
SDL_Surface * canvas, SDL_Surface * last, SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect);
int x, int y, SDL_Rect * update_rect);
void tint_shutdown(magic_api * api); void tint_shutdown(magic_api * api);
void tint_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); void tint_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b);
int tint_requires_colors(magic_api * api, int which); int tint_requires_colors(magic_api * api, int which);
@ -106,68 +105,85 @@ void tint_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas);
void tint_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); void tint_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas);
int tint_modes(magic_api * api, int which); int tint_modes(magic_api * api, int which);
Uint32 tint_api_version(void) { return(TP_MAGIC_API_VERSION); } Uint32 tint_api_version(void)
{
return (TP_MAGIC_API_VERSION);
}
//Load sounds //Load sounds
int tint_init(magic_api * api){ int tint_init(magic_api * api)
{
int i; int i;
char fname[1024]; char fname[1024];
for (i = 0; i < tint_NUM_TOOLS; i++){ for (i = 0; i < tint_NUM_TOOLS; i++)
{
snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, tint_snd_filenames[i]); snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, tint_snd_filenames[i]);
tint_snd_effect[i] = Mix_LoadWAV(fname); tint_snd_effect[i] = Mix_LoadWAV(fname);
} }
return(1); return (1);
} }
int tint_get_tool_count(magic_api * api ATTRIBUTE_UNUSED){ int tint_get_tool_count(magic_api * api ATTRIBUTE_UNUSED)
return(tint_NUM_TOOLS); {
return (tint_NUM_TOOLS);
} }
// Load our icons: // Load our icons:
SDL_Surface * tint_get_icon(magic_api * api, int which){ SDL_Surface *tint_get_icon(magic_api * api, int which)
{
char fname[1024]; char fname[1024];
snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, tint_icon_filenames[which]); snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, tint_icon_filenames[which]);
return(IMG_Load(fname)); return (IMG_Load(fname));
} }
// Return our names, localized: // Return our names, localized:
char * tint_get_name(magic_api * api ATTRIBUTE_UNUSED, int which){ char *tint_get_name(magic_api * api ATTRIBUTE_UNUSED, int which)
return(strdup(gettext_noop(tint_names[which]))); {
return (strdup(gettext_noop(tint_names[which])));
} }
// Return our descriptions, localized: // Return our descriptions, localized:
char * tint_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode){ char *tint_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode)
return(strdup(gettext_noop(tint_descs[which][mode-1]))); {
return (strdup(gettext_noop(tint_descs[which][mode - 1])));
} }
//Calculates the grey scale value for a rgb pixel //Calculates the grey scale value for a rgb pixel
static int tint_grey(Uint8 r1,Uint8 g1,Uint8 b1){ static int tint_grey(Uint8 r1, Uint8 g1, Uint8 b1)
return 0.3*r1+.59*g1+0.11*b1; {
return 0.3 * r1 + .59 * g1 + 0.11 * b1;
} }
static void do_tint_pixel(void * ptr, int which, static void do_tint_pixel(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y)
SDL_Surface * canvas, SDL_Surface * last, {
int x, int y){
magic_api * api = (magic_api *) ptr; magic_api *api = (magic_api *) ptr;
Uint8 r,g,b; Uint8 r, g, b;
float h,s,v; float h, s, v;
SDL_GetRGB(api->getpixel(last, x, y), last->format, &r, &g, &b); SDL_GetRGB(api->getpixel(last, x, y), last->format, &r, &g, &b);
{ {
int greyValue = tint_grey(r,g,b); int greyValue = tint_grey(r, g, b);
if (which == TOOL_TINT){ if (which == TOOL_TINT)
{
api->rgbtohsv(tint_r, tint_g, tint_b, &h, &s, &v); api->rgbtohsv(tint_r, tint_g, tint_b, &h, &s, &v);
api->hsvtorgb(h, s, greyValue/255.0, &r, &g, &b); api->hsvtorgb(h, s, greyValue / 255.0, &r, &g, &b);
api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, r, g, b)); api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, r, g, b));
} else if (which == TOOL_THRESHOLD){ }
int thresholdValue = (tint_max-tint_min)/2; else if (which == TOOL_THRESHOLD)
if (greyValue < thresholdValue){ {
int thresholdValue = (tint_max - tint_min) / 2;
if (greyValue < thresholdValue)
{
api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, tint_r, tint_g, tint_b)); api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, tint_r, tint_g, tint_b));
} else{ }
else
{
api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, 255, 255, 255)); api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, 255, 255, 255));
} }
} }
@ -175,25 +191,29 @@ int x, int y){
} }
// Do the effect: // Do the effect:
static void do_tint_full(void * ptr,SDL_Surface * canvas, SDL_Surface * last, int which){ static void do_tint_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int which)
int x,y; {
for (y = 0; y < last->h; y++){ int x, y;
for (x=0; x < last->w; x++){
for (y = 0; y < last->h; y++)
{
for (x = 0; x < last->w; x++)
{
do_tint_pixel(ptr, which, canvas, last, x, y); do_tint_pixel(ptr, which, canvas, last, x, y);
} }
} }
} }
static void do_tint_brush(void * ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y){ static void do_tint_brush(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y)
{
int xx, yy; int xx, yy;
magic_api * api = (magic_api *) ptr; magic_api *api = (magic_api *) ptr;
for (yy = y - tint_RADIUS; yy < y + tint_RADIUS; yy++) for (yy = y - tint_RADIUS; yy < y + tint_RADIUS; yy++)
{ {
for (xx = x - tint_RADIUS; xx < x + tint_RADIUS; xx++) for (xx = x - tint_RADIUS; xx < x + tint_RADIUS; xx++)
{ {
if (api->in_circle(xx - x, yy - y, tint_RADIUS) && if (api->in_circle(xx - x, yy - y, tint_RADIUS) && !api->touched(xx, yy))
!api->touched(xx, yy))
{ {
do_tint_pixel(api, which, canvas, last, xx, yy); do_tint_pixel(api, which, canvas, last, xx, yy);
} }
@ -203,15 +223,27 @@ static void do_tint_brush(void * ptr, int which, SDL_Surface * canvas, SDL_Surfa
// Affect the canvas on drag: // Affect the canvas on drag:
void tint_drag(magic_api * api, int which, SDL_Surface * canvas, void tint_drag(magic_api * api, int which, SDL_Surface * canvas,
SDL_Surface * last, int ox, int oy, int x, int y, SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect)
SDL_Rect * update_rect){ {
api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, do_tint_brush); api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_tint_brush);
api->playsound(tint_snd_effect[which], (x * 255) / canvas->w, 255); api->playsound(tint_snd_effect[which], (x * 255) / canvas->w, 255);
if (ox > x) { int tmp = ox; ox = x; x = tmp; } if (ox > x)
if (oy > y) { int tmp = oy; oy = y; y = tmp; } {
int tmp = ox;
ox = x;
x = tmp;
}
if (oy > y)
{
int tmp = oy;
oy = y;
y = tmp;
}
update_rect->x = ox - tint_RADIUS; update_rect->x = ox - tint_RADIUS;
update_rect->y = oy - tint_RADIUS; update_rect->y = oy - tint_RADIUS;
@ -221,11 +253,12 @@ void tint_drag(magic_api * api, int which, SDL_Surface * canvas,
// Affect the canvas on click: // Affect the canvas on click:
void tint_click(magic_api * api, int which, int mode, void tint_click(magic_api * api, int which, int mode,
SDL_Surface * canvas, SDL_Surface * last, SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect)
int x, int y, SDL_Rect * update_rect){ {
if (mode == MODE_PAINT) if (mode == MODE_PAINT)
tint_drag(api, which, canvas, last, x, y, x, y, update_rect); tint_drag(api, which, canvas, last, x, y, x, y, update_rect);
else{ else
{
update_rect->x = 0; update_rect->x = 0;
update_rect->y = 0; update_rect->y = 0;
update_rect->w = canvas->w; update_rect->w = canvas->w;
@ -247,8 +280,11 @@ void tint_shutdown(magic_api * api ATTRIBUTE_UNUSED)
{ {
//Clean up sounds //Clean up sounds
int i; int i;
for(i=0; i<tint_NUM_TOOLS; i++){
if(tint_snd_effect[i] != NULL){ for (i = 0; i < tint_NUM_TOOLS; i++)
{
if (tint_snd_effect[i] != NULL)
{
Mix_FreeChunk(tint_snd_effect[i]); Mix_FreeChunk(tint_snd_effect[i]);
} }
} }
@ -268,32 +304,39 @@ int tint_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_U
return 1; return 1;
} }
void tint_switchin(magic_api * api, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas){ void tint_switchin(magic_api * api, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas)
{
int x,y; int x, y;
Uint8 r1,g1,b1; Uint8 r1, g1, b1;
for (y = 0; y < canvas->h; y++){ for (y = 0; y < canvas->h; y++)
for (x=0; x < canvas->w; x++){ {
for (x = 0; x < canvas->w; x++)
{
SDL_GetRGB(api->getpixel(canvas, x, y), canvas->format, &r1, &g1, &b1); SDL_GetRGB(api->getpixel(canvas, x, y), canvas->format, &r1, &g1, &b1);
{ {
int greyValue = tint_grey(r1,g1,b1); int greyValue = tint_grey(r1, g1, b1);
if (greyValue<tint_min){
tint_min=greyValue; if (greyValue < tint_min)
{
tint_min = greyValue;
} }
if (greyValue>tint_max){ if (greyValue > tint_max)
tint_max=greyValue; {
tint_max = greyValue;
} }
} }
} }
} }
} }
void tint_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) void tint_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED,
SDL_Surface * canvas ATTRIBUTE_UNUSED)
{ {
} }
int tint_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) int tint_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
{ {
return(MODE_FULLSCREEN|MODE_PAINT); return (MODE_FULLSCREEN | MODE_PAINT);
} }