Re: [Tuxpaint-devel] Magic API updated: switchin() and switchout()
An award-winning drawing program for children of all ages
Brought to you by:
wkendrick
|
From: Albert C. <aca...@gm...> - 2008-07-09 06:26:01
|
On Tue, Jul 8, 2008 at 8:35 PM, Bill Kendrick <nb...@so...> wrote:
> I understand the jist, but could you give me an example chunk of code
> (within a Magic tool) where this is used, so I can make sure I do it
> properly? (I don't want to forget to do this, which I will in a matter
> of days :^/ )
magic/src/bricks.c
static unsigned char *map;
...
if (!api->button_down())
{
if (map)
free(map);
// the "+ 3" allows for both ends and misalignment
x_count = (canvas->w + nominal_width - 1) / nominal_width + 3;
y_count = (canvas->h + nominal_height - 1) / nominal_height + 3;
map = calloc(x_count, y_count);
}
That "map" variable should be associated with the history.
Better code would be:
When we draw a brick, we start by creating a brick map.
If the top history item is the same size brick, then we do
a memcpy() of that. Otherwise, we zero-initialize the map.
We then push the history stack, attach our brick map to
the newly added history entry, and attach a destructor
(a function pointer) to the newly added history entry.
When the oldest history entry is freed by Tux Paint, the
core code calls that function pointer. That then lets the
magic plug-in free the brick map.
Note that a callback is needed because some tools may
wish to use a linked list or tree. It would be nice to have
a function pointer that is compatible with free() though, so
that magic plug-ins can directly use that. For plug-ins that
do not store state in the history, there should not be any
need to do anything. Tux Paint can initialize the pointer to
a dummy function.
|