+ Added Context parameter to CurrentSprite.

+ Now CurrentSprite is defined in current_sprite.h.
+ Modified several routines to get the current sprite as parameter.
This commit is contained in:
David Capello 2009-06-02 14:08:56 +00:00
parent c622ca2535
commit 59ae779fca
28 changed files with 214 additions and 137 deletions

View File

@ -1,3 +1,7 @@
2009-06-01 David A. Capello <davidcapello@gmail.com>
* src/ase/current_sprite.h (CurrentSprite): Added.
2009-05-31 David A. Capello <davidcapello@gmail.com>
* src/file/file.cpp (fop_free): Fixed a mutex-handle leak.

View File

@ -38,6 +38,9 @@ const char *msgids_get(const char *id); /* src/intl/msgids.[ch] */
#define _(msgid) (msgids_get(msgid))
// asserts
#include <cassert>
#include <math.h>
#undef PI
#define PI 3.14159265358979323846
@ -48,6 +51,9 @@ typedef uint8_t ase_uint8;
typedef uint16_t ase_uint16;
typedef uint32_t ase_uint32;
//////////////////////////////////////////////////////////////////////
// Overloaded new/delete operators to detect memory-leaks
#if defined __cplusplus && defined MEMLEAK
#include <new>

View File

@ -7,6 +7,7 @@ ASE = aseprite$(EXE)
COMMON_SOURCES = \
src/ase/context.cpp \
src/ase/current_sprite.cpp \
src/ase/ui_context.cpp \
src/commands/cmd_about.cpp \
src/commands/cmd_advanced_mode.cpp \

View File

@ -0,0 +1,60 @@
/* ASE - Allegro Sprite Editor
* Copyright (C) 2001-2009 David Capello
*
* 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
*/
#include "config.h"
#include "ase/context.h"
#include "ase/current_sprite.h"
#include "ase/ui_context.h" // TODO remove this line
#include "raster/sprite.h"
CurrentSprite::CurrentSprite()
{
m_context = UIContext::instance();
m_sprite = m_context->get_current_sprite();
if (m_sprite)
m_writeable = m_sprite->lock();
}
CurrentSprite::CurrentSprite(Context* context)
{
assert(context != NULL);
m_context = context;
m_sprite = m_context->get_current_sprite();
if (m_sprite)
m_writeable = m_sprite->lock();
}
CurrentSprite::~CurrentSprite()
{
if (m_sprite)
m_sprite->unlock();
}
void CurrentSprite::destroy()
{
if (m_sprite) {
m_context->remove_sprite(m_sprite);
m_sprite->unlock();
delete m_sprite;
m_sprite = NULL;
m_writeable = false;
}
}

57
src/ase/current_sprite.h Normal file
View File

@ -0,0 +1,57 @@
/* ASE - Allegro Sprite Editor
* Copyright (C) 2001-2009 David Capello
*
* 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
*/
#ifndef ASE_CURRENT_SPRITE_H
#define ASE_CURRENT_SPRITE_H
#include <list>
class Context;
class Sprite;
class CurrentSprite
{
Context* m_context;
Sprite* m_sprite;
bool m_writeable;
// // No-default constructor (undefined)
// CurrentSprite();
// Non-copyable
CurrentSprite(const CurrentSprite&);
CurrentSprite& operator=(const CurrentSprite&);
public:
CurrentSprite();
CurrentSprite(Context* context);
~CurrentSprite();
bool writeable() const { return m_writeable; }
void destroy();
operator Sprite* () { return m_sprite; }
Sprite* operator->() {
assert(m_sprite != NULL);
return m_sprite;
}
};
#endif // ASE_CURRENT_SPRITE_H

View File

@ -31,7 +31,8 @@ static bool cmd_copy_cel_enabled(const char *argument)
static void cmd_copy_cel_execute(const char *argument)
{
copy_cel();
CurrentSprite sprite;
copy_cel(sprite);
}
Command cmd_copy_cel = {

View File

@ -32,7 +32,8 @@ static bool cmd_mask_by_color_enabled(const char *argument)
static void cmd_mask_by_color_execute(const char *argument)
{
dialogs_mask_color();
CurrentSprite sprite;
dialogs_mask_color(sprite);
}
Command cmd_mask_by_color = {

View File

@ -31,7 +31,8 @@ static bool cmd_move_cel_enabled(const char *argument)
static void cmd_move_cel_execute(const char *argument)
{
move_cel();
CurrentSprite sprite;
move_cel(sprite);
}
Command cmd_move_cel = {

View File

@ -20,6 +20,7 @@
#define COMMANDS_COMMANDS_H
#include "jinete/jbase.h"
#include "ase/current_sprite.h"
#define CMD_ABOUT "about"
#define CMD_ADVANCED_MODE "advanced_mode"

View File

@ -639,7 +639,7 @@ static bool anieditor_msg_proc(JWidget widget, JMessage msg)
/* move the cel */
else if (msg->mouse.left) {
if (movement) {
move_cel();
move_cel(anieditor->sprite);
destroy_thumbnails();
anieditor_regenerate_layers(widget);

View File

@ -39,7 +39,7 @@
#include "widgets/colbut.h"
#include "widgets/groupbut.h"
void canvas_resize()
void canvas_resize(Sprite* sprite)
{
JWidget window, box1, box2, box3, box4, box5, box6;
JWidget label_w, label_h;
@ -47,7 +47,6 @@ void canvas_resize()
JWidget check_w, check_h;
JWidget button_offset;
JWidget button_ok, button_cancel;
CurrentSprite sprite;
if (!is_interactive () || !sprite)
return;

View File

@ -19,7 +19,9 @@
#ifndef DIALOGS_CANVASZE_H
#define DIALOGS_CANVASZE_H
void canvas_resize();
class Sprite;
void canvas_resize(Sprite* sprite);
#endif /* DIALOGS_CANVASZE_H */

View File

@ -42,18 +42,17 @@
static JWidget font_button;
static Image *render_text(FONT *f, const char *text, int color);
static Image *render_text(Sprite* sprite, FONT *f, const char *text, int color);
static FONT *my_load_font(const char *filename);
static void button_font_command(JWidget widget);
static void update_button_text();
void dialogs_draw_text()
void dialogs_draw_text(Sprite* sprite)
{
Image *image, *dest_image;
JWidget window, button_ok, color_box, color_but;
JWidget entry_size, entry_text;
CurrentSprite sprite;
char buf[256];
if (!is_interactive() || !sprite)
@ -136,7 +135,7 @@ void dialogs_draw_text()
set_config_int("DrawText", "Size", size);
/* render text */
image = render_text(f, text, color);
image = render_text(sprite, f, text, color);
if (image) {
clipboard::copy_image(image, sprite_get_palette(sprite, sprite->frame));
clipboard::paste(sprite);
@ -152,7 +151,7 @@ void dialogs_draw_text()
jwidget_free(window);
}
Image *RenderText(const char *fontname, int size, int color, const char *text)
Image *RenderText(Sprite* sprite, const char *fontname, int size, int color, const char *text)
{
Image *render;
FONT *f;
@ -163,14 +162,14 @@ Image *RenderText(const char *fontname, int size, int color, const char *text)
ji_font_set_size(f, size);
render = render_text(f, text, color);
render = render_text(sprite, f, text, color);
destroy_font(f);
return render;
}
static Image *render_text(FONT *f, const char *text, int color)
static Image *render_text(Sprite* sprite, FONT *f, const char *text, int color)
{
/* TODO warning this uses Image->dat and not Image->line */
#define DO(type, colfunc) \
@ -186,7 +185,6 @@ static Image *render_text(FONT *f, const char *text, int color)
} \
}
CurrentSprite sprite;
int i, pixels, w, h;
Image *image;
BITMAP *bmp;

View File

@ -20,10 +20,11 @@
#define DIALOGS_DRAWTEXT_H
class Image;
class Sprite;
void dialogs_draw_text();
void dialogs_draw_text(Sprite* sprite);
Image* RenderText(const char* fontname, int size, int color, const char* text);
Image* RenderText(Sprite* sprite, const char* fontname, int size, int color, const char* text);
#endif /* DIALOGS_DRAWTEXT_H */

View File

@ -49,16 +49,15 @@ static bool color_change_hook(JWidget widget, void *data);
static bool slider_change_hook(JWidget widget, void *data);
static bool preview_change_hook(JWidget widget, void *data);
static Mask *gen_mask();
static void mask_preview();
static Mask *gen_mask(Sprite* sprite);
static void mask_preview(Sprite* sprite);
void dialogs_mask_color()
void dialogs_mask_color(Sprite* sprite)
{
JWidget window, box1, box2, box3, box4;
JWidget label_color, button_1, button_2;
JWidget label_fuzziness;
JWidget button_ok, button_cancel;
CurrentSprite sprite;
Image *image;
if (!is_interactive () || !sprite)
@ -90,12 +89,15 @@ void dialogs_mask_color()
if (get_config_bool("MaskColor", "Preview", TRUE))
jwidget_select(check_preview);
button_1->user_data[1] = sprite;
button_2->user_data[1] = sprite;
jbutton_add_command(button_1, button_1_command);
jbutton_add_command(button_2, button_2_command);
HOOK(button_color, SIGNAL_COLORBUTTON_CHANGE, color_change_hook, 0);
HOOK(slider_fuzziness, JI_SIGNAL_SLIDER_CHANGE, slider_change_hook, 0);
HOOK(check_preview, JI_SIGNAL_CHECK_CHANGE, preview_change_hook, 0);
HOOK(button_color, SIGNAL_COLORBUTTON_CHANGE, color_change_hook, sprite);
HOOK(slider_fuzziness, JI_SIGNAL_SLIDER_CHANGE, slider_change_hook, sprite);
HOOK(check_preview, JI_SIGNAL_CHECK_CHANGE, preview_change_hook, sprite);
jwidget_magnetic(button_ok, TRUE);
jwidget_expansive(button_color, TRUE);
@ -113,7 +115,7 @@ void dialogs_mask_color()
jwindow_center(window);
/* mask first preview */
mask_preview();
mask_preview(sprite);
/* load window configuration */
load_window_pos(window, "MaskColor");
@ -131,7 +133,7 @@ void dialogs_mask_color()
}
/* change the mask */
mask = gen_mask();
mask = gen_mask(sprite);
sprite_set_mask(sprite, mask);
mask_free(mask);
@ -159,38 +161,37 @@ static void button_1_command(JWidget widget)
{
colorbutton_set_color(button_color,
colorbar_get_fg_color(app_get_colorbar()));
mask_preview();
mask_preview((Sprite*)widget->user_data[1]);
}
static void button_2_command(JWidget widget)
{
colorbutton_set_color(button_color,
colorbar_get_bg_color(app_get_colorbar()));
mask_preview();
mask_preview((Sprite*)widget->user_data[1]);
}
static bool color_change_hook(JWidget widget, void *data)
{
mask_preview();
mask_preview((Sprite*)data);
return FALSE;
}
static bool slider_change_hook(JWidget widget, void *data)
{
mask_preview();
mask_preview((Sprite*)data);
return FALSE;
}
static bool preview_change_hook(JWidget widget, void *data)
{
mask_preview();
mask_preview((Sprite*)data);
return FALSE;
}
static Mask *gen_mask()
static Mask *gen_mask(Sprite* sprite)
{
int xpos, ypos, color, fuzziness;
CurrentSprite sprite;
Image* image = GetImage2(sprite, &xpos, &ypos, NULL);
@ -205,11 +206,10 @@ static Mask *gen_mask()
return mask;
}
static void mask_preview()
static void mask_preview(Sprite* sprite)
{
if (jwidget_is_selected (check_preview)) {
CurrentSprite sprite;
Mask *mask = gen_mask();
if (jwidget_is_selected(check_preview)) {
Mask *mask = gen_mask(sprite);
Mask *old_mask = sprite->mask;
sprite->mask = mask;

View File

@ -19,6 +19,8 @@
#ifndef DIALOGS_MASKCOL_H
#define DIALOGS_MASKCOL_H
void dialogs_mask_color();
class Sprite;
void dialogs_mask_color(Sprite* sprite);
#endif /* DIALOGS_MASKCOL_H */

View File

@ -123,11 +123,10 @@ static int image_getpixel4 (Image *image, double x, double y)
return bilinear4(a, b, c, d, ftofix(x), ftofix(y));
}
void dialogs_vector_map()
void dialogs_vector_map(Sprite* sprite)
{
#define PROJECT() project(image, x, y, dmax, &u, &v)
CurrentSprite sprite;
Image *image, *image_copy;
double u, v, dmax;
int c, x, y;

View File

@ -19,6 +19,8 @@
#ifndef DIALOGS_VECTMAP_H
#define DIALOGS_VECTMAP_H
void dialogs_vector_map();
class Sprite;
void dialogs_vector_map(Sprite* sprite);
#endif /* DIALOGS_VECTMAP_H */

View File

@ -351,7 +351,6 @@ void jwidget::set_text_quiet(const char *text)
void jwidget::align(int align)
{
m_align = align;
dirty();
}
FONT *jwidget::font()

View File

@ -398,6 +398,7 @@ static void theme_init_widget(JWidget widget)
case JI_SLIDER:
BORDER(4);
widget->child_spacing = jwidget_get_text_height(widget);
widget->align(JI_CENTER | JI_MIDDLE);
break;
case JI_TEXTBOX:
@ -1146,8 +1147,7 @@ static void theme_draw_slider(JWidget widget, JRect clip)
usprintf(buf, "%d", value);
widget->align(JI_CENTER | JI_MIDDLE);
widget->text(buf);
widget->set_text_quiet(buf);
r = jrect_new(x1, y1, x2+1, y2+1);
@ -1172,7 +1172,7 @@ static void theme_draw_slider(JWidget widget, JRect clip)
set_clip(ji_screen, cx1, cy1, cx2, cy2);
widget->text(old_text.c_str());
widget->set_text_quiet(old_text.c_str());
jrect_free(r);
}
}

View File

@ -20,6 +20,7 @@
#include "jinete/jinete.h"
#include "ase/current_sprite.h"
#include "ase/ui_context.h"
#include "core/app.h"
#include "modules/editors.h"

View File

@ -38,38 +38,6 @@ static void layer_get_pos(Sprite* sprite, Layer *layer, int target, bool write,
//////////////////////////////////////////////////////////////////////
CurrentSprite::CurrentSprite()
{
UIContext* context = UIContext::instance();
m_sprite = context->get_current_sprite();
if (m_sprite)
m_writeable = m_sprite->lock();
}
CurrentSprite::~CurrentSprite()
{
if (m_sprite)
m_sprite->unlock();
}
void CurrentSprite::destroy()
{
if (m_sprite) {
UIContext* context = UIContext::instance();
context->remove_sprite(m_sprite);
m_sprite->unlock();
delete m_sprite;
m_sprite = NULL;
m_writeable = false;
}
}
//////////////////////////////////////////////////////////////////////
ImageRef *images_ref_get_from_sprite(Sprite* sprite, int target, bool write)
{
Layer *layer = target & TARGET_ALL_LAYERS ? sprite->set:

View File

@ -19,11 +19,7 @@
#ifndef MODULES_SPRITES_H
#define MODULES_SPRITES_H
#include <cassert>
#include "jinete/jbase.h"
#include "raster/sprite.h"
class Sprite;
class Image;
class Layer;
class Cel;
@ -36,27 +32,6 @@ struct ImageRef
ImageRef* next;
};
class CurrentSprite
{
Sprite* m_sprite;
bool m_writeable;
public:
CurrentSprite();
~CurrentSprite();
bool writeable() const { return m_writeable; }
void destroy();
operator Sprite* () { return m_sprite; }
Sprite* operator->() {
assert(m_sprite != NULL);
return m_sprite;
}
};
ImageRef* images_ref_get_from_sprite(Sprite* sprite, int target, bool write);
void images_ref_free(ImageRef* image_ref);

View File

@ -37,8 +37,8 @@
#include "util/celmove.h"
#include "util/functions.h"
/* these variables indicate what cel to move (and the current_sprite
frame indicates to where move it) */
/* these variables indicate what cel to move (and the sprite's
frame indicates where to move it) */
static Layer *src_layer = NULL; /* TODO warning not thread safe */
static Layer *dst_layer = NULL;
static int src_frame = 0;
@ -53,9 +53,8 @@ void set_frame_to_handle(Layer *_src_layer, int _src_frame,
dst_frame = _dst_frame;
}
void move_cel()
void move_cel(Sprite* sprite)
{
CurrentSprite sprite;
Cel *src_cel, *dst_cel;
assert(src_layer != NULL);
@ -64,7 +63,7 @@ void move_cel()
assert(dst_frame >= 0 && dst_frame < sprite->frames);
if (layer_is_background(src_layer)) {
copy_cel();
copy_cel(sprite);
return;
}
@ -145,9 +144,8 @@ void move_cel()
set_frame_to_handle(NULL, 0, NULL, 0);
}
void copy_cel()
void copy_cel(Sprite* sprite)
{
CurrentSprite sprite;
Cel *src_cel, *dst_cel;
assert(src_layer != NULL);

View File

@ -21,12 +21,13 @@
class Cel;
class Layer;
class Sprite;
void set_frame_to_handle(Layer* src_layer, int src_frame,
Layer* dst_layer, int dst_frame);
void move_cel();
void copy_cel();
void move_cel(Sprite* sprite);
void copy_cel(Sprite* sprite);
#endif /* UTIL_CELMOVE_H */

View File

@ -21,6 +21,7 @@
#include <assert.h>
#include "jinete/jinete.h"
#include "ase/current_sprite.h"
#include "ase/ui_context.h"
#include "console/console.h"
#include "core/app.h"

View File

@ -81,39 +81,38 @@ Image* GetImage2(Sprite* sprite, int* x, int* y, int* opacity)
return image;
}
void LoadPalette(const char *filename)
void LoadPalette(Sprite* sprite, const char *filename)
{
CurrentSprite sprite;
if (sprite) {
DIRS *dir, *dirs;
char buf[512];
assert(sprite != NULL);
dirs = dirs_new();
dirs_add_path(dirs, filename);
DIRS *dir, *dirs;
char buf[512];
usprintf(buf, "palettes/%s", filename);
dirs_cat_dirs(dirs, filename_in_datadir (buf));
dirs = dirs_new();
dirs_add_path(dirs, filename);
for (dir=dirs; dir; dir=dir->next) {
if (exists(dir->path)) {
Palette *pal = palette_load(dir->path);
if (pal != NULL) {
/* set the palette calling the hooks */
set_current_palette(pal, FALSE);
usprintf(buf, "palettes/%s", filename);
dirs_cat_dirs(dirs, filename_in_datadir (buf));
/* just one palette */
sprite_reset_palettes(sprite);
sprite_set_palette(sprite, pal, 0);
for (dir=dirs; dir; dir=dir->next) {
if (exists(dir->path)) {
Palette *pal = palette_load(dir->path);
if (pal != NULL) {
/* set the palette calling the hooks */
set_current_palette(pal, FALSE);
/* redraw the entire screen */
jmanager_refresh_screen();
}
break;
/* just one palette */
sprite_reset_palettes(sprite);
sprite_set_palette(sprite, pal, 0);
/* redraw the entire screen */
jmanager_refresh_screen();
}
break;
}
dirs_free(dirs);
}
dirs_free(dirs);
}
/* returns a new layer created from the current mask in the current

View File

@ -31,7 +31,7 @@ class Undo;
Image* GetImage(Sprite* sprite);
Image* GetImage2(Sprite* sprite, int *x, int *y, int *opacity);
void LoadPalette(const char* filename);
void LoadPalette(Sprite* sprite, const char* filename);
Layer* NewLayerFromMask(Sprite* src, Sprite* dst);
Image* NewImageFromMask(Sprite* src);