forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflatten.cpp
More file actions
84 lines (65 loc) · 2.21 KB
/
flatten.cpp
File metadata and controls
84 lines (65 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Aseprite
// Copyright (C) 2001-2015 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "base/unique_ptr.h"
#include "doc/cel.h"
#include "doc/frame.h"
#include "doc/image.h"
#include "doc/layer.h"
#include "doc/sprite.h"
#include "gfx/rect.h"
#include "render/render.h"
namespace app {
using namespace doc;
static bool has_cels(const Layer* layer, frame_t frame);
LayerImage* create_flatten_layer_copy(Sprite* dstSprite, const Layer* srcLayer,
const gfx::Rect& bounds,
frame_t frmin, frame_t frmax)
{
base::UniquePtr<LayerImage> flatLayer(new LayerImage(dstSprite));
render::Render render;
for (frame_t frame=frmin; frame<=frmax; ++frame) {
// Does this frame have cels to render?
if (has_cels(srcLayer, frame)) {
// Create a new image to render each frame.
ImageRef image(Image::create(flatLayer->sprite()->pixelFormat(), bounds.w, bounds.h));
// Create the new cel for the output layer.
base::UniquePtr<Cel> cel(new Cel(frame, image));
cel->setPosition(bounds.x, bounds.y);
// Render this frame.
render.renderLayer(image.get(), srcLayer, frame,
gfx::Clip(0, 0, bounds));
// Add the cel (and release the base::UniquePtr).
flatLayer->addCel(cel);
cel.release();
}
}
return flatLayer.release();
}
// Returns true if the "layer" or its children have any cel to render
// in the given "frame".
static bool has_cels(const Layer* layer, frame_t frame)
{
if (!layer->isVisible())
return false;
switch (layer->type()) {
case ObjectType::LayerImage:
return (layer->cel(frame) ? true: false);
case ObjectType::LayerFolder: {
LayerConstIterator it = static_cast<const LayerFolder*>(layer)->getLayerBegin();
LayerConstIterator end = static_cast<const LayerFolder*>(layer)->getLayerEnd();
for (; it != end; ++it) {
if (has_cels(*it, frame))
return true;
}
break;
}
}
return false;
}
} // namespace app