Refactored 'extras' stuff in Sprite class.

Now it is called "ExtraCel" which is the union of a Cel and an Image.
This commit is contained in:
David Capello 2010-04-24 16:09:35 -03:00
parent d79d29c1a5
commit bb6815524f
8 changed files with 88 additions and 64 deletions

View File

@ -106,9 +106,7 @@ void ChangeImageTypeCommand::execute(Context* context)
undoable.set_imgtype(m_imgtype, m_dithering); undoable.set_imgtype(m_imgtype, m_dithering);
// Regenerate extras // Regenerate extras
sprite->prepareExtra(); sprite->destroyExtraCel();
image_clear(sprite->getExtras(),
sprite->getExtras()->mask_color);
undoable.commit(); undoable.commit();
} }

View File

@ -97,9 +97,7 @@ void PlayAnimationCommand::execute(Context* context)
clear_bitmap(ji_screen); clear_bitmap(ji_screen);
// Clear extras (e.g. pen preview) // Clear extras (e.g. pen preview)
sprite->prepareExtra(); sprite->destroyExtraCel();
image_clear(sprite->getExtras(),
sprite->getExtras()->mask_color);
/* do animation */ /* do animation */
oldpal = NULL; oldpal = NULL;

View File

@ -368,10 +368,10 @@ public:
return m_bound.seg; return m_bound.seg;
} }
void prepareExtra(); void prepareExtraCel(int x, int y, int w, int h, int opacity);
Image* getExtras() { return m_extras; } void destroyExtraCel();
int getExtrasOpacity() const { return m_extras_opacity; } Cel* getExtraCel() const;
void setExtrasOpacity(int opacity) { m_extras_opacity = opacity; } Image* getExtraCelImage() const;
private: private:
Sprite* m_self; // pointer to the Sprite Sprite* m_self; // pointer to the Sprite
@ -403,8 +403,8 @@ private:
PreferredEditorSettings m_preferred; PreferredEditorSettings m_preferred;
Image* m_extras; // Image with the sprite size to draw some extra stuff (e.g. editor's cursor) Cel* m_extraCel; // Extra cel used to draw extra stuff (e.g. editor's pen preview, pixels in movement, etc.)
int m_extras_opacity; // Opacity to be used to draw the extra image Image* m_extraImage; // Image of the extra cel
// Mutex to modify the 'locked' flag. // Mutex to modify the 'locked' flag.
Vaca::Mutex* m_mutex; Vaca::Mutex* m_mutex;
@ -444,7 +444,8 @@ SpriteImpl::SpriteImpl(Sprite* sprite, int imgtype, int width, int height, int n
m_undo = undo_new(m_self); m_undo = undo_new(m_self);
m_repository.paths = jlist_new(); m_repository.paths = jlist_new();
m_repository.masks = jlist_new(); m_repository.masks = jlist_new();
m_extras = NULL; m_extraCel = NULL;
m_extraImage = NULL;
// Boundary stuff // Boundary stuff
m_bound.nseg = 0; m_bound.nseg = 0;
@ -628,7 +629,8 @@ SpriteImpl::~SpriteImpl()
// Destroy undo, mask, etc. // Destroy undo, mask, etc.
delete m_undo; delete m_undo;
delete m_mask; delete m_mask;
delete m_extras; // image delete m_extraCel;
delete m_extraImage;
if (m_bound.seg) jfree(m_bound.seg); if (m_bound.seg) jfree(m_bound.seg);
delete m_mutex; delete m_mutex;
@ -921,16 +923,42 @@ void SpriteImpl::deletePalette(Palette* pal)
jlist_delete_link(m_palettes, link); jlist_delete_link(m_palettes, link);
} }
void SpriteImpl::prepareExtra() void SpriteImpl::destroyExtraCel()
{ {
if (!m_extras || delete m_extraCel;
m_extras->imgtype != m_imgtype || delete m_extraImage;
m_extras->w != m_width ||
m_extras->h != m_height) { m_extraCel = NULL;
delete m_extras; // image m_extraImage = NULL;
m_extras = image_new(m_imgtype, m_width, m_height);
image_clear(m_extras, m_extras->mask_color = 0);
} }
void SpriteImpl::prepareExtraCel(int x, int y, int w, int h, int opacity)
{
if (!m_extraCel)
m_extraCel = new Cel(0, 0); // Ignored fields for this cell (frame, and image index)
m_extraCel->x = x;
m_extraCel->y = y;
m_extraCel->opacity = opacity;
if (!m_extraImage ||
m_extraImage->imgtype != m_imgtype ||
m_extraImage->w != w ||
m_extraImage->h != h) {
delete m_extraImage; // image
m_extraImage = image_new(m_imgtype, w, h);
image_clear(m_extraImage,
m_extraImage->mask_color = 0);
}
}
Cel* SpriteImpl::getExtraCel() const
{
return m_extraCel;
}
Image* SpriteImpl::getExtraCelImage() const
{
return m_extraImage;
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
@ -1484,24 +1512,24 @@ const _BoundSeg* Sprite::getBoundariesSegments() const
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Extras // Extras
void Sprite::prepareExtra() void Sprite::prepareExtraCel(int x, int y, int w, int h, int opacity)
{ {
m_impl->prepareExtra(); m_impl->prepareExtraCel(x, y, w, h, opacity);
} }
Image* Sprite::getExtras() void Sprite::destroyExtraCel()
{ {
return m_impl->getExtras(); m_impl->destroyExtraCel();
} }
int Sprite::getExtrasOpacity() const Cel* Sprite::getExtraCel() const
{ {
return m_impl->getExtrasOpacity(); return m_impl->getExtraCel();
} }
void Sprite::setExtrasOpacity(int opacity) Image* Sprite::getExtraCelImage() const
{ {
m_impl->setExtrasOpacity(opacity); return m_impl->getExtraCelImage();
} }
static Layer* index2layer(const Layer* layer, int index, int* index_count) static Layer* index2layer(const Layer* layer, int index, int* index_count)

View File

@ -206,12 +206,13 @@ public:
const _BoundSeg* getBoundariesSegments() const; const _BoundSeg* getBoundariesSegments() const;
//////////////////////////////////////// ////////////////////////////////////////
// Extras // Extra Cel (it is used to draw pen preview, pixels in movement,
// etc.)
void prepareExtra(); void prepareExtraCel(int x, int y, int w, int h, int opacity);
Image* getExtras(); void destroyExtraCel();
int getExtrasOpacity() const; Cel* getExtraCel() const;
void setExtrasOpacity(int opacity); Image* getExtraCelImage() const;
private: private:
Sprite(); Sprite();

View File

@ -1548,9 +1548,7 @@ static void chunk_set_imgtype_invert(UndoStream* stream, UndoChunkSetImgType *ch
sprite->setImgType(chunk->imgtype); sprite->setImgType(chunk->imgtype);
// Regenerate extras // Regenerate extras
sprite->prepareExtra(); sprite->destroyExtraCel();
image_clear(sprite->getExtras(),
sprite->getExtras()->mask_color);
} }
} }

View File

@ -473,20 +473,21 @@ void RenderEngine::renderLayer(Sprite *sprite, Layer *layer, Image *image,
} }
// Draw extras // Draw extras
if (layer == sprite->getCurrentLayer() && sprite->getExtras() != NULL) { if (layer == sprite->getCurrentLayer() && sprite->getExtraCel() != NULL) {
int opacity = sprite->getExtrasOpacity(); Cel* extraCel = sprite->getExtraCel();
if (opacity > 0) { if (extraCel->opacity > 0) {
Image* extraImage = sprite->getExtraCelImage();
if (zoom == 0) { if (zoom == 0) {
image_merge(image, sprite->getExtras(), image_merge(image, extraImage,
-source_x, extraCel->x - source_x,
-source_y, extraCel->y - source_y,
opacity, BLEND_MODE_NORMAL); extraCel->opacity, BLEND_MODE_NORMAL);
} }
else { else {
(*zoomed_func)(image, sprite->getExtras(), (*zoomed_func)(image, extraImage,
-source_x, (extraCel->x << zoom) - source_x,
-source_y, (extraCel->y << zoom) - source_y,
opacity, BLEND_MODE_NORMAL, zoom); extraCel->opacity, BLEND_MODE_NORMAL, zoom);
} }
} }
} }

View File

@ -273,11 +273,11 @@ void Editor::editor_draw_cursor(int x, int y, bool refresh)
int new_mask_color; int new_mask_color;
Pen* pen = editor_get_current_pen(); Pen* pen = editor_get_current_pen();
// Set the opacity for the 'extra' layer used to show the cursor preview // Create the extra cel to show the pen preview
m_sprite->setExtrasOpacity(tool_settings->getOpacity()); m_sprite->prepareExtraCel(x-pen->get_size()/2,
y-pen->get_size()/2,
// Create the 'extra' image/layer pen->get_size(), pen->get_size(),
m_sprite->prepareExtra(); tool_settings->getOpacity());
// In 'indexed' images, if the current color is 0, we have to use // In 'indexed' images, if the current color is 0, we have to use
// a different mask color (different from 0) to draw the extra layer // a different mask color (different from 0) to draw the extra layer
@ -288,10 +288,10 @@ void Editor::editor_draw_cursor(int x, int y, bool refresh)
new_mask_color = 0; new_mask_color = 0;
} }
Image* extras = m_sprite->getExtras(); Image* extraImage = m_sprite->getExtraCelImage();
if (extras->mask_color != new_mask_color) if (extraImage->mask_color != new_mask_color)
image_clear(extras, extras->mask_color = new_mask_color); image_clear(extraImage, extraImage->mask_color = new_mask_color);
image_putpen(extras, pen, x, y, pen_color, extras->mask_color); image_putpen(extraImage, pen, pen->get_size()/2, pen->get_size()/2, pen_color, extraImage->mask_color);
if (refresh) { if (refresh) {
editors_draw_sprite(m_sprite, editors_draw_sprite(m_sprite,
@ -405,10 +405,10 @@ void Editor::editor_clean_cursor(bool refresh)
m_state != EDITOR_STATE_DRAWING) { m_state != EDITOR_STATE_DRAWING) {
Pen* pen = editor_get_current_pen(); Pen* pen = editor_get_current_pen();
m_sprite->prepareExtra(); m_sprite->prepareExtraCel(x-pen->get_size()/2,
image_putpen(m_sprite->getExtras(), pen, x, y, y-pen->get_size()/2,
m_sprite->getExtras()->mask_color, pen->get_size(), pen->get_size(),
m_sprite->getExtras()->mask_color); 0); // Opacity = 0
if (refresh) { if (refresh) {
editors_draw_sprite(m_sprite, editors_draw_sprite(m_sprite,

View File

@ -523,8 +523,8 @@ void Editor::flashCurrentLayer()
int x, y; int x, y;
const Image* src_image = m_sprite->getCurrentImage(&x, &y); const Image* src_image = m_sprite->getCurrentImage(&x, &y);
if (src_image) { if (src_image) {
m_sprite->prepareExtra(); m_sprite->prepareExtraCel(0, 0, m_sprite->getWidth(), m_sprite->getHeight(), 255);
Image* flash_image = m_sprite->getExtras(); Image* flash_image = m_sprite->getExtraCelImage();
int u, v; int u, v;
image_clear(flash_image, flash_image->mask_color); image_clear(flash_image, flash_image->mask_color);