update copyright date
[gnash.git] / libcore / parser / sprite_definition.cpp
blob4fbbfe1888937200ae0a3e20d9da163ea50ecc4c
1 // sprite_definition.cpp: for Gnash.
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010,
4 // 2011 Free Software Foundation, Inc
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "RunResources.h"
21 #include "smart_ptr.h"
22 #include "MovieClip.h"
23 #include "sprite_definition.h"
24 #include "ControlTag.h" // for dtor visibility
25 #include "as_function.h"
26 #include "SWFStream.h"
27 #include "GnashAlgorithm.h"
28 #include "SWFParser.h"
29 #include "namedStrings.h"
30 #include "Global_as.h"
32 #include <vector>
33 #include <string>
34 #include <cassert>
37 namespace gnash {
39 DisplayObject*
40 sprite_definition::createDisplayObject(Global_as& gl, DisplayObject* parent)
41 const
43 // Should not call MovieClip constructor (probably), but should
44 // attach MovieClip.prototype
45 as_object* obj = getObjectWithPrototype(gl, NSV::CLASS_MOVIE_CLIP);
46 DisplayObject* mc = new MovieClip(obj, this, parent->get_root(), parent);
47 return mc;
50 sprite_definition::~sprite_definition()
54 /*private*/
55 // only called from constructors
56 void
57 sprite_definition::read(SWFStream& in, const RunResources& runResources)
59 const size_t tag_end = in.get_tag_end_position();
61 in.ensureBytes(2);
62 m_frame_count = in.read_u16();
64 IF_VERBOSE_PARSE (
65 log_parse(_(" frames = %d"), m_frame_count);
68 m_loading_frame = 0;
70 SWFParser parser(in, this, runResources);
72 // This can throw a ParserException; we will let the SWFMovieDefintion
73 // catch it, as a failure means the whole stream is invalid.
74 parser.read(tag_end - in.tell());
76 if (m_frame_count > m_loading_frame) {
77 IF_VERBOSE_MALFORMED_SWF(
78 log_swferror(_("%d frames advertised in header, but "
79 "only %d SHOWFRAME tags found in define "
80 "sprite."), m_frame_count, m_loading_frame );
83 // this should be safe
84 m_loading_frame = m_frame_count;
87 IF_VERBOSE_PARSE(
88 log_parse(_(" -- sprite END --"));
92 void
93 sprite_definition::add_frame_name(const std::string& name)
96 // It's fine for loaded frames to exceed frame count. Should be
97 // adjusted at the end of parsing.
98 _namedFrames.insert(std::make_pair(name, m_loading_frame));
101 bool
102 sprite_definition::get_labeled_frame(const std::string& label,
103 size_t& frame_number) const
105 NamedFrameMap::const_iterator it = _namedFrames.find(label);
106 if ( it == _namedFrames.end() ) return false;
107 frame_number = it->second;
108 return true;
111 sprite_definition::sprite_definition(movie_definition& m, SWFStream& in,
112 const RunResources& runResources, boost::uint16_t id)
114 movie_definition(id),
115 m_movie_def(m),
116 m_frame_count(0),
117 m_loading_frame(0),
118 registeredClass(0),
119 _loadingSoundStream(-1)
121 read(in, runResources);
124 void
125 sprite_definition::registerClass(as_function* the_class)
127 registeredClass = the_class;
130 void
131 sprite_definition::markReachableResources() const
133 if (registeredClass) registeredClass->setReachable();
136 } // namespace gnash