indent rails.c
This commit is contained in:
parent
6a89ec2384
commit
b06b7e51b4
1 changed files with 413 additions and 388 deletions
|
|
@ -32,6 +32,7 @@ static unsigned int rails_segment_modified_last =0; //which segment was last
|
||||||
static unsigned int rails_segment_to_add = 0; //a segment that should be added to solve corner joints
|
static unsigned int rails_segment_to_add = 0; //a segment that should be added to solve corner joints
|
||||||
static SDL_Rect modification_rect;
|
static SDL_Rect modification_rect;
|
||||||
static SDL_Surface *canvas_backup;
|
static SDL_Surface *canvas_backup;
|
||||||
|
|
||||||
// Housekeeping functions
|
// Housekeeping functions
|
||||||
|
|
||||||
SDL_Surface *rails_one, *rails_three, *rails_four, *rails_corner;
|
SDL_Surface *rails_one, *rails_three, *rails_four, *rails_corner;
|
||||||
|
|
@ -46,8 +47,7 @@ char * rails_get_name(magic_api * api, int which);
|
||||||
char *rails_get_description(magic_api * api, int which, int mode);
|
char *rails_get_description(magic_api * api, int which, int mode);
|
||||||
int rails_requires_colors(magic_api * api, int which);
|
int rails_requires_colors(magic_api * api, int which);
|
||||||
void rails_release(magic_api * api, int which,
|
void rails_release(magic_api * api, int which,
|
||||||
SDL_Surface * canvas, SDL_Surface * snapshot,
|
SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect);
|
||||||
int x, int y, SDL_Rect * update_rect);
|
|
||||||
void rails_shutdown(magic_api * api);
|
void rails_shutdown(magic_api * api);
|
||||||
void rails_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas);
|
void rails_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas);
|
||||||
void rails_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas);
|
void rails_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas);
|
||||||
|
|
@ -58,17 +58,14 @@ static void rails_flip(void * ptr, SDL_Surface * dest, SDL_Surface * src);
|
||||||
static void rails_flip_flop(void *ptr, SDL_Surface * dest, SDL_Surface * src);
|
static void rails_flip_flop(void *ptr, SDL_Surface * dest, SDL_Surface * src);
|
||||||
static void rails_rotate(void *ptr, SDL_Surface * dest, SDL_Surface * src, unsigned int direction);
|
static void rails_rotate(void *ptr, SDL_Surface * dest, SDL_Surface * src, unsigned int direction);
|
||||||
void rails_click(magic_api * api, int which, int mode,
|
void rails_click(magic_api * api, int which, int mode,
|
||||||
SDL_Surface * canvas, SDL_Surface * snapshot,
|
SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect);
|
||||||
int x, int y, SDL_Rect * update_rect);
|
|
||||||
static Uint8 rails_select_image(Uint16 segment);
|
static Uint8 rails_select_image(Uint16 segment);
|
||||||
static void rails_draw(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last,
|
static void rails_draw(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last,
|
||||||
int x, int y, unsigned int segment);
|
int x, int y, unsigned int segment);
|
||||||
|
|
||||||
static void rails_draw_wrapper(void * ptr, int which, SDL_Surface * canvas, SDL_Surface * last,
|
static void rails_draw_wrapper(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y);
|
||||||
int x, int y);
|
|
||||||
void rails_drag(magic_api * api, int which, SDL_Surface * canvas,
|
void rails_drag(magic_api * api, int which, SDL_Surface * canvas,
|
||||||
SDL_Surface * snapshot, int ox, int oy, int x, int y,
|
SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect);
|
||||||
SDL_Rect * update_rect);
|
|
||||||
|
|
||||||
Uint32 rails_api_version(void)
|
Uint32 rails_api_version(void)
|
||||||
{
|
{
|
||||||
|
|
@ -80,7 +77,8 @@ int rails_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
|
||||||
return (MODE_PAINT);
|
return (MODE_PAINT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void rails_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED)
|
void rails_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED,
|
||||||
|
Uint8 b ATTRIBUTE_UNUSED)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -122,17 +120,25 @@ SDL_Surface * rails_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED)
|
||||||
{
|
{
|
||||||
char fname[1024];
|
char fname[1024];
|
||||||
|
|
||||||
snprintf(fname, sizeof(fname), "%s/images/magic/rails.png",
|
snprintf(fname, sizeof(fname), "%s/images/magic/rails.png", api->data_directory);
|
||||||
api->data_directory);
|
|
||||||
|
|
||||||
return (IMG_Load(fname));
|
return (IMG_Load(fname));
|
||||||
}
|
}
|
||||||
|
|
||||||
char * rails_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { return strdup(gettext_noop("Rails")); }
|
char *rails_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
|
||||||
|
{
|
||||||
|
return strdup(gettext_noop("Rails"));
|
||||||
|
}
|
||||||
|
|
||||||
char * rails_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED) { return strdup(gettext_noop("Click and drag to draw train track rails on your picture.")); }
|
char *rails_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED)
|
||||||
|
{
|
||||||
|
return strdup(gettext_noop("Click and drag to draw train track rails on your picture."));
|
||||||
|
}
|
||||||
|
|
||||||
int rails_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED) { return 0;}
|
int rails_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void rails_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED,
|
void rails_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED,
|
||||||
SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * snapshot ATTRIBUTE_UNUSED,
|
SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * snapshot ATTRIBUTE_UNUSED,
|
||||||
|
|
@ -159,12 +165,14 @@ void rails_shutdown(magic_api * api ATTRIBUTE_UNUSED)
|
||||||
free(rails_status_of_segments);
|
free(rails_status_of_segments);
|
||||||
}
|
}
|
||||||
|
|
||||||
void rails_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas)
|
void rails_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED,
|
||||||
|
SDL_Surface * canvas)
|
||||||
{
|
{
|
||||||
//we've to compute the quantity of segments in each direction
|
//we've to compute the quantity of segments in each direction
|
||||||
|
|
||||||
canvas_backup = SDL_CreateRGBSurface(SDL_ANYFORMAT, canvas->w, canvas->h, canvas->format->BitsPerPixel,
|
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);
|
canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask,
|
||||||
|
canvas->format->Amask);
|
||||||
|
|
||||||
SDL_BlitSurface(canvas, NULL, canvas_backup, NULL);
|
SDL_BlitSurface(canvas, NULL, canvas_backup, NULL);
|
||||||
rails_segments_x = rails_math_ceil(canvas->w, img_w);
|
rails_segments_x = rails_math_ceil(canvas->w, img_w);
|
||||||
|
|
@ -173,7 +181,8 @@ void rails_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED
|
||||||
rails_status_of_segments = (Uint8 *) calloc(rails_segments_x * rails_segments_y + 1, sizeof(Uint8));
|
rails_status_of_segments = (Uint8 *) calloc(rails_segments_x * rails_segments_y + 1, sizeof(Uint8));
|
||||||
}
|
}
|
||||||
|
|
||||||
void rails_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED)
|
void rails_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED,
|
||||||
|
SDL_Surface * canvas ATTRIBUTE_UNUSED)
|
||||||
{
|
{
|
||||||
if (rails_status_of_segments != NULL)
|
if (rails_status_of_segments != NULL)
|
||||||
{
|
{
|
||||||
|
|
@ -187,17 +196,20 @@ void rails_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSE
|
||||||
static int rails_math_ceil(int x, int y)
|
static int rails_math_ceil(int x, int y)
|
||||||
{
|
{
|
||||||
int temp;
|
int temp;
|
||||||
|
|
||||||
temp = (int)x / y;
|
temp = (int)x / y;
|
||||||
|
|
||||||
if (x % y)
|
if (x % y)
|
||||||
return temp + 1;
|
return temp + 1;
|
||||||
else return temp;
|
else
|
||||||
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline unsigned int rails_get_segment(int x, int y)
|
inline unsigned int rails_get_segment(int x, int y)
|
||||||
{
|
{
|
||||||
int xx; //segments are numerated just like pixels
|
int xx; //segments are numerated just like pixels
|
||||||
int yy; //in computer graphics: left upper (=1), ... ,right upper,
|
int yy; //in computer graphics: left upper (=1), ... ,right upper,
|
||||||
|
|
||||||
//left bottom, ... , right bottom
|
//left bottom, ... , right bottom
|
||||||
xx = rails_math_ceil(x, img_w);
|
xx = rails_math_ceil(x, img_w);
|
||||||
yy = rails_math_ceil(y, img_h);
|
yy = rails_math_ceil(y, img_h);
|
||||||
|
|
@ -222,10 +234,12 @@ static void rails_flip(void * ptr, SDL_Surface * dest, SDL_Surface * src)
|
||||||
for (y = 0; y < dest->h; y++)
|
for (y = 0; y < dest->h; y++)
|
||||||
api->putpixel(dest, x, y, api->getpixel(src, x, src->h - y - 1));
|
api->putpixel(dest, x, y, api->getpixel(src, x, src->h - y - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void rails_flip_flop(void *ptr, SDL_Surface * dest, SDL_Surface * src)
|
static void rails_flip_flop(void *ptr, SDL_Surface * dest, SDL_Surface * src)
|
||||||
{
|
{
|
||||||
magic_api *api = (magic_api *) ptr;
|
magic_api *api = (magic_api *) ptr;
|
||||||
Sint16 x, y;
|
Sint16 x, y;
|
||||||
|
|
||||||
for (x = 0; x < dest->w; x++)
|
for (x = 0; x < dest->w; x++)
|
||||||
for (y = 0; y < dest->h; y++)
|
for (y = 0; y < dest->h; y++)
|
||||||
api->putpixel(dest, x, y, api->getpixel(src, y, x));
|
api->putpixel(dest, x, y, api->getpixel(src, y, x));
|
||||||
|
|
@ -253,8 +267,7 @@ static void rails_rotate (void * ptr, SDL_Surface * dest, SDL_Surface * src, uns
|
||||||
}
|
}
|
||||||
|
|
||||||
void rails_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED,
|
void rails_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED,
|
||||||
SDL_Surface * canvas, SDL_Surface * snapshot,
|
SDL_Surface * canvas, SDL_Surface * snapshot, int x, int y, SDL_Rect * update_rect)
|
||||||
int x, int y, SDL_Rect * update_rect)
|
|
||||||
{
|
{
|
||||||
rails_segment_modified_last = 0;
|
rails_segment_modified_last = 0;
|
||||||
rails_drag(api, which, canvas, snapshot, x, y, x, y, update_rect);
|
rails_drag(api, which, canvas, snapshot, x, y, x, y, update_rect);
|
||||||
|
|
@ -314,22 +327,31 @@ static Uint8 rails_select_image(Uint16 segment)
|
||||||
|
|
||||||
|
|
||||||
take_up = segment - rails_segments_x;
|
take_up = segment - rails_segments_x;
|
||||||
if (take_up<=0) val_up = SEG_NONE;
|
if (take_up <= 0)
|
||||||
else val_up = rails_status_of_segments[take_up];
|
val_up = SEG_NONE;
|
||||||
|
else
|
||||||
|
val_up = rails_status_of_segments[take_up];
|
||||||
|
|
||||||
take_down = segment + rails_segments_x;
|
take_down = segment + rails_segments_x;
|
||||||
if (take_down>(signed)(rails_segments_x*rails_segments_y)) val_down = SEG_NONE;
|
if (take_down > (signed)(rails_segments_x * rails_segments_y))
|
||||||
else val_down = rails_status_of_segments[take_down];
|
val_down = SEG_NONE;
|
||||||
|
else
|
||||||
|
val_down = rails_status_of_segments[take_down];
|
||||||
|
|
||||||
if ((segment%rails_segments_x)==1) val_left=SEG_NONE;
|
if ((segment % rails_segments_x) == 1)
|
||||||
else val_left = rails_status_of_segments[segment-1];
|
val_left = SEG_NONE;
|
||||||
|
else
|
||||||
|
val_left = rails_status_of_segments[segment - 1];
|
||||||
|
|
||||||
if ((segment%rails_segments_x)==0) val_right=SEG_NONE;
|
if ((segment % rails_segments_x) == 0)
|
||||||
else val_right = rails_status_of_segments[segment+1];
|
val_right = SEG_NONE;
|
||||||
|
else
|
||||||
|
val_right = rails_status_of_segments[segment + 1];
|
||||||
|
|
||||||
if (from_left || (val_left & SEG_RIGHT) || from_bottom_left)
|
if (from_left || (val_left & SEG_RIGHT) || from_bottom_left)
|
||||||
{
|
{
|
||||||
LEFT = 1;}
|
LEFT = 1;
|
||||||
|
}
|
||||||
if (from_right || (val_right & SEG_LEFT) || from_top_right)
|
if (from_right || (val_right & SEG_LEFT) || from_top_right)
|
||||||
RIGHT = 1;
|
RIGHT = 1;
|
||||||
if (from_top || (val_up & SEG_BOTTOM) || from_top_left)
|
if (from_top || (val_up & SEG_BOTTOM) || from_top_left)
|
||||||
|
|
@ -368,8 +390,8 @@ static Uint8 rails_select_image(Uint16 segment)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void rails_draw(void * ptr, int which ATTRIBUTE_UNUSED, ATTRIBUTE_UNUSED SDL_Surface * canvas, SDL_Surface * last ATTRIBUTE_UNUSED,
|
static void rails_draw(void *ptr, int which ATTRIBUTE_UNUSED, ATTRIBUTE_UNUSED SDL_Surface * canvas,
|
||||||
int x, int y ATTRIBUTE_UNUSED, unsigned int segment)
|
SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y ATTRIBUTE_UNUSED, unsigned int segment)
|
||||||
{
|
{
|
||||||
magic_api *api = (magic_api *) ptr;
|
magic_api *api = (magic_api *) ptr;
|
||||||
SDL_Surface *result, *temp;
|
SDL_Surface *result, *temp;
|
||||||
|
|
@ -393,10 +415,12 @@ static void rails_draw(void * ptr, int which ATTRIBUTE_UNUSED, ATTRIBUTE_UNUSED
|
||||||
|
|
||||||
|
|
||||||
result = SDL_CreateRGBSurface(SDL_ANYFORMAT, img_w, img_h, rails_one->format->BitsPerPixel,
|
result = SDL_CreateRGBSurface(SDL_ANYFORMAT, img_w, img_h, rails_one->format->BitsPerPixel,
|
||||||
rails_one->format->Rmask, rails_one->format->Gmask, rails_one->format->Bmask, rails_one->format->Amask);
|
rails_one->format->Rmask, rails_one->format->Gmask, rails_one->format->Bmask,
|
||||||
|
rails_one->format->Amask);
|
||||||
|
|
||||||
temp = SDL_CreateRGBSurface(SDL_ANYFORMAT, img_w, img_h, rails_one->format->BitsPerPixel,
|
temp = SDL_CreateRGBSurface(SDL_ANYFORMAT, img_w, img_h, rails_one->format->BitsPerPixel,
|
||||||
rails_one->format->Rmask, rails_one->format->Gmask, rails_one->format->Bmask, rails_one->format->Amask);
|
rails_one->format->Rmask, rails_one->format->Gmask, rails_one->format->Bmask,
|
||||||
|
rails_one->format->Amask);
|
||||||
|
|
||||||
SDL_BlitSurface(canvas_backup, &modification_rect, result, NULL);
|
SDL_BlitSurface(canvas_backup, &modification_rect, result, NULL);
|
||||||
|
|
||||||
|
|
@ -468,8 +492,7 @@ static void rails_draw(void * ptr, int which ATTRIBUTE_UNUSED, ATTRIBUTE_UNUSED
|
||||||
api->playsound(rails_snd, (x * 255) / canvas->w, 255);
|
api->playsound(rails_snd, (x * 255) / canvas->w, 255);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void rails_draw_wrapper(void * ptr, int which, SDL_Surface * canvas, SDL_Surface * last,
|
static void rails_draw_wrapper(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y)
|
||||||
int x, int y)
|
|
||||||
{
|
{
|
||||||
rails_segment_modified = rails_get_segment(x, y);
|
rails_segment_modified = rails_get_segment(x, y);
|
||||||
|
|
||||||
|
|
@ -494,12 +517,13 @@ if (rails_segment_modified>0)
|
||||||
}
|
}
|
||||||
|
|
||||||
void rails_drag(magic_api * api, int which, SDL_Surface * canvas,
|
void rails_drag(magic_api * api, int which, SDL_Surface * canvas,
|
||||||
SDL_Surface * snapshot, int ox, int oy, int x, int y,
|
SDL_Surface * snapshot, int ox, int oy, int x, int y, SDL_Rect * update_rect)
|
||||||
SDL_Rect * update_rect)
|
|
||||||
{
|
{
|
||||||
int start_x, end_x, start_y, end_y, segment_start, segment_end, w, h;
|
int start_x, end_x, start_y, end_y, segment_start, segment_end, w, h;
|
||||||
|
|
||||||
// avoiding to write out of the canvas
|
// avoiding to write out of the canvas
|
||||||
if ((x<canvas->w)&&(y<canvas->h)&&(ox<canvas->w)&&(oy<canvas->h)&&((signed)x>0)&&((signed)y>0)&&((signed)ox>0)&&((signed)oy>0))
|
if ((x < canvas->w) && (y < canvas->h) && (ox < canvas->w) && (oy < canvas->h) && ((signed)x > 0) && ((signed)y > 0)
|
||||||
|
&& ((signed)ox > 0) && ((signed)oy > 0))
|
||||||
{
|
{
|
||||||
api->line((void *)api, which, canvas, snapshot, ox, oy, x, y, img_w / 2, rails_draw_wrapper);
|
api->line((void *)api, which, canvas, snapshot, ox, oy, x, y, img_w / 2, rails_draw_wrapper);
|
||||||
|
|
||||||
|
|
@ -519,6 +543,7 @@ void rails_drag(magic_api * api, int which, SDL_Surface * canvas,
|
||||||
update_rect->x = x;
|
update_rect->x = x;
|
||||||
update_rect->y = y;
|
update_rect->y = y;
|
||||||
update_rect->w = w;
|
update_rect->w = w;
|
||||||
update_rect->h=h;}
|
update_rect->h = h;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue