indent grass.c

This commit is contained in:
Bill Kendrick 2017-10-15 11:46:50 -07:00
parent 9fd870b985
commit fb1e2a092d

View file

@ -30,39 +30,34 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* for RAND_MAX */
#include <stdlib.h> /* for RAND_MAX */
#include "tp_magic_api.h"
#include "SDL_image.h"
#include "SDL_mixer.h"
/* Our globals: */
static Mix_Chunk * grass_snd;
static Mix_Chunk *grass_snd;
static Uint8 grass_r, grass_g, grass_b;
static SDL_Surface * img_grass;
static SDL_Surface *img_grass;
// Prototypes
int grass_init(magic_api * api);
Uint32 grass_api_version(void);
int grass_get_tool_count(magic_api * api);
SDL_Surface * grass_get_icon(magic_api * api, int which);
char * grass_get_name(magic_api * api, int which);
char * grass_get_description(magic_api * api, int which, int mode);
SDL_Surface *grass_get_icon(magic_api * api, int which);
char *grass_get_name(magic_api * api, int which);
char *grass_get_description(magic_api * api, int which, int mode);
void grass_drag(magic_api * api, int which, SDL_Surface * canvas,
SDL_Surface * last, int ox, int oy, int x, int y,
SDL_Rect * update_rect);
SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect);
void grass_click(magic_api * api, int which, int mode,
SDL_Surface * canvas, SDL_Surface * last,
int x, int y, SDL_Rect * update_rect);
SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect);
void grass_release(magic_api * api, int which,
SDL_Surface * canvas, SDL_Surface * last,
int x, int y, SDL_Rect * update_rect);
SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect);
void grass_shutdown(magic_api * api);
void grass_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b);
int grass_requires_colors(magic_api * api, int which);
static void do_grass(void * ptr, int which,
SDL_Surface * canvas, SDL_Surface * last,
int x, int y);
static void do_grass(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y);
static int log2int(int x);
void grass_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas);
void grass_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas);
@ -74,81 +69,90 @@ int grass_init(magic_api * api)
{
char fname[1024];
snprintf(fname, sizeof(fname), "%s/sounds/magic/grass.wav",
api->data_directory);
snprintf(fname, sizeof(fname), "%s/sounds/magic/grass.wav", api->data_directory);
grass_snd = Mix_LoadWAV(fname);
snprintf(fname, sizeof(fname), "%s/images/magic/grass_data.png",
api->data_directory);
snprintf(fname, sizeof(fname), "%s/images/magic/grass_data.png", api->data_directory);
img_grass = IMG_Load(fname);
return(1);
return (1);
}
Uint32 grass_api_version(void) { return(TP_MAGIC_API_VERSION); }
Uint32 grass_api_version(void)
{
return (TP_MAGIC_API_VERSION);
}
// We have multiple tools:
int grass_get_tool_count(magic_api * api ATTRIBUTE_UNUSED)
{
return(1);
return (1);
}
// Load our icons:
SDL_Surface * grass_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED)
SDL_Surface *grass_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED)
{
char fname[1024];
snprintf(fname, sizeof(fname), "%s/images/magic/grass.png",
api->data_directory);
snprintf(fname, sizeof(fname), "%s/images/magic/grass.png", api->data_directory);
return(IMG_Load(fname));
return (IMG_Load(fname));
}
// Return our names, localized:
char * grass_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
char *grass_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
{
return(strdup(gettext_noop("Grass")));
return (strdup(gettext_noop("Grass")));
}
// Return our descriptions, localized:
char * grass_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED)
char *grass_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED)
{
return(strdup(gettext_noop("Click and drag to draw grass. Dont forget the dirt!")));
return (strdup(gettext_noop("Click and drag to draw grass. Dont forget the dirt!")));
}
// Affect the canvas on drag:
void grass_drag(magic_api * api, int which, SDL_Surface * canvas,
SDL_Surface * last, int ox, int oy, int x, int y,
SDL_Rect * update_rect)
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, 4, do_grass);
api->line((void *)api, which, canvas, last, ox, oy, x, y, 4, do_grass);
if (ox > x) { int tmp = ox; ox = x; x = tmp; }
if (oy > y) { int tmp = oy; oy = y; y = tmp; }
if (ox > x)
{
int tmp = ox;
ox = x;
x = tmp;
}
if (oy > y)
{
int tmp = oy;
oy = y;
y = tmp;
}
update_rect->x = ox - 64;
update_rect->y = oy - 64;
update_rect->w = 128;
update_rect->h = 192;
api->playsound(grass_snd,
(x * 255) / canvas->w, (y * 255) / canvas->h);
api->playsound(grass_snd, (x * 255) / canvas->w, (y * 255) / canvas->h);
}
// Affect the canvas on click:
void grass_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED,
SDL_Surface * canvas, SDL_Surface * last,
int x, int y, SDL_Rect * update_rect)
SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect)
{
grass_drag(api, which, canvas, last, x, y, x, y, update_rect);
}
void grass_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED,
SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED,
int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED)
SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED,
int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED)
{
}
@ -173,12 +177,12 @@ int grass_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_
return 1;
}
static void do_grass(void * ptr, int which ATTRIBUTE_UNUSED,
SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED,
int x, int y)
static void do_grass(void *ptr, int which ATTRIBUTE_UNUSED,
SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y)
{
magic_api * api = (magic_api *) ptr;
magic_api *api = (magic_api *) ptr;
int xx, yy;
// grass color: 82,180,17
static int bucket;
double tmp_red, tmp_green, tmp_blue;
@ -187,92 +191,79 @@ static void do_grass(void * ptr, int which ATTRIBUTE_UNUSED,
if (!api->button_down())
bucket = 0;
bucket += (3.5 + (rand() / (double) RAND_MAX)) * 7.0;
bucket += (3.5 + (rand() / (double)RAND_MAX)) * 7.0;
while (bucket >= 0)
{
int rank =
log2int(((double) y / canvas->h) *
(0.99 + (rand() / (double) RAND_MAX)) * 64);
int ah = 1 << rank;
bucket -= ah;
src.x = (rand() % 4) * 64;
src.y = ah;
src.w = 64;
src.h = ah;
dest.x = x - 32;
dest.y = y - 30 + (int) ((rand() / (double) RAND_MAX) * 30);
tmp_red =
api->sRGB_to_linear(grass_r) * 2.0 +
(rand() / (double) RAND_MAX);
tmp_green =
api->sRGB_to_linear(grass_g) * 2.0 +
(rand() / (double) RAND_MAX);
tmp_blue =
api->sRGB_to_linear(grass_b) * 2.0 +
api->sRGB_to_linear(17);
for (yy = 0; yy < ah; yy++)
{
for (xx = 0; xx < 64; xx++)
{
double rd, gd, bd;
int rank = log2int(((double)y / canvas->h) * (0.99 + (rand() / (double)RAND_MAX)) * 64);
int ah = 1 << rank;
SDL_GetRGBA(api->getpixel(img_grass, xx + src.x, yy + src.y),
img_grass->format, &r, &g, &b, &a);
bucket -= ah;
src.x = (rand() % 4) * 64;
src.y = ah;
src.w = 64;
src.h = ah;
rd = api->sRGB_to_linear(r) * 8.0 + tmp_red;
rd = rd * (a / 255.0) / 11.0;
gd = api->sRGB_to_linear(g) * 8.0 + tmp_green;
gd = gd * (a / 255.0) / 11.0;
bd = api->sRGB_to_linear(b) * 8.0 + tmp_blue;
bd = bd * (a / 255.0) / 11.0;
dest.x = x - 32;
dest.y = y - 30 + (int)((rand() / (double)RAND_MAX) * 30);
SDL_GetRGB(api->getpixel(canvas, xx + dest.x, yy + dest.y),
canvas->format, &r, &g, &b);
tmp_red = api->sRGB_to_linear(grass_r) * 2.0 + (rand() / (double)RAND_MAX);
tmp_green = api->sRGB_to_linear(grass_g) * 2.0 + (rand() / (double)RAND_MAX);
tmp_blue = api->sRGB_to_linear(grass_b) * 2.0 + api->sRGB_to_linear(17);
r =
api->linear_to_sRGB(api->sRGB_to_linear(r) * (1.0 - a / 255.0) +
rd);
g =
api->linear_to_sRGB(api->sRGB_to_linear(g) * (1.0 - a / 255.0) +
gd);
b =
api->linear_to_sRGB(api->sRGB_to_linear(b) * (1.0 - a / 255.0) +
bd);
for (yy = 0; yy < ah; yy++)
{
for (xx = 0; xx < 64; xx++)
{
double rd, gd, bd;
api->putpixel(canvas, xx + dest.x, yy + dest.y,
SDL_MapRGB(canvas->format, r, g, b));
}
SDL_GetRGBA(api->getpixel(img_grass, xx + src.x, yy + src.y), img_grass->format, &r, &g, &b, &a);
rd = api->sRGB_to_linear(r) * 8.0 + tmp_red;
rd = rd * (a / 255.0) / 11.0;
gd = api->sRGB_to_linear(g) * 8.0 + tmp_green;
gd = gd * (a / 255.0) / 11.0;
bd = api->sRGB_to_linear(b) * 8.0 + tmp_blue;
bd = bd * (a / 255.0) / 11.0;
SDL_GetRGB(api->getpixel(canvas, xx + dest.x, yy + dest.y), canvas->format, &r, &g, &b);
r = api->linear_to_sRGB(api->sRGB_to_linear(r) * (1.0 - a / 255.0) + rd);
g = api->linear_to_sRGB(api->sRGB_to_linear(g) * (1.0 - a / 255.0) + gd);
b = api->linear_to_sRGB(api->sRGB_to_linear(b) * (1.0 - a / 255.0) + bd);
api->putpixel(canvas, xx + dest.x, yy + dest.y, SDL_MapRGB(canvas->format, r, g, b));
}
}
}
}
}
// this one rounds down
static int log2int(int x)
{
int y = 0;
if (x <= 1)
return 0;
x >>= 1;
while (x)
{
x >>= 1;
y++;
}
{
x >>= 1;
y++;
}
return y;
}
void grass_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED)
void grass_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED,
SDL_Surface * canvas ATTRIBUTE_UNUSED)
{
}
void grass_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED)
void grass_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED,
SDL_Surface * canvas ATTRIBUTE_UNUSED)
{
}
int grass_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
{
return(MODE_PAINT);
return (MODE_PAINT);
}