Drop the bool operator for ObjectURI, to avoid getting the kind of side-effect that...
[gnash.git] / libcore / parser / sprite_definition.cpp
blob2eb2261b6a4c725abf4f6f5dd66f349c20be0379
1 // sprite_definition.cpp: for Gnash.
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
4 // 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" // GNASH_USE_GC
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 // Define the following macro to get a dump the prototype
38 // members of classes registered to definitions.
39 //#define DEBUG_REGISTER_CLASS 1
41 namespace gnash {
43 DisplayObject*
44 sprite_definition::createDisplayObject(Global_as& gl, DisplayObject* parent)
45 const
47 #ifdef DEBUG_REGISTER_CLASS
48 log_debug(_("Instantiating sprite_def %p"), (void*)this);
49 #endif
51 // Should not call MovieClip constructor (probably), but should
52 // attach MovieClip.prototype
54 as_object* obj = getObjectWithPrototype(gl, NSV::CLASS_MOVIE_CLIP);
55 DisplayObject* mc = new MovieClip(obj, this, parent->get_root(), parent);
56 return mc;
59 sprite_definition::~sprite_definition()
63 /*private*/
64 // only called from constructors
65 void
66 sprite_definition::read(SWFStream& in, const RunResources& runResources)
68 const size_t tag_end = in.get_tag_end_position();
70 in.ensureBytes(2);
71 m_frame_count = in.read_u16();
73 IF_VERBOSE_PARSE (
74 log_parse(_(" frames = %d"), m_frame_count);
77 m_loading_frame = 0;
79 SWFParser parser(in, this, runResources);
81 // This can throw a ParserException; we will let the SWFMovieDefintion
82 // catch it, as a failure means the whole stream is invalid.
83 parser.read(tag_end - in.tell());
85 if (m_frame_count > m_loading_frame) {
86 IF_VERBOSE_MALFORMED_SWF(
87 log_swferror(_("%d frames advertised in header, but "
88 "only %d SHOWFRAME tags found in define "
89 "sprite."), m_frame_count, m_loading_frame );
92 // this should be safe
93 m_loading_frame = m_frame_count;
96 IF_VERBOSE_PARSE(
97 log_parse(_(" -- sprite END --"));
101 void
102 sprite_definition::add_frame_name(const std::string& name)
105 // It's fine for loaded frames to exceed frame count. Should be
106 // adjusted at the end of parsing.
107 _namedFrames.insert(std::make_pair(name, m_loading_frame));
110 bool
111 sprite_definition::get_labeled_frame(const std::string& label,
112 size_t& frame_number) const
114 NamedFrameMap::const_iterator it = _namedFrames.find(label);
115 if ( it == _namedFrames.end() ) return false;
116 frame_number = it->second;
117 return true;
120 sprite_definition::sprite_definition(movie_definition& m, SWFStream& in,
121 const RunResources& runResources, boost::uint16_t id)
123 movie_definition(id),
124 m_movie_def(m),
125 m_frame_count(0),
126 m_loading_frame(0),
127 registeredClass(0),
128 _loadingSoundStream(-1)
130 read(in, runResources);
133 void
134 sprite_definition::registerClass(as_function* the_class)
136 registeredClass = the_class;
137 #ifdef DEBUG_REGISTER_CLASS
138 assert(registeredClass);
140 log_debug(_("Registered class %p for sprite_def %p"),
141 (void*)registeredClass.get(), (void*)this);
142 as_object* proto =
143 registeredClass->getMember(NSV::PROP_PROTOTYPE).to_object(
144 getGlobal(*registeredClass));
146 log_debug(_(" Exported interface: "));
147 proto->dump_members();
148 #endif
151 #ifdef GNASH_USE_GC
152 void
153 sprite_definition::markReachableResources() const
155 if ( registeredClass.get() ) registeredClass->setReachable();
157 #endif // GNASH_USE_GC
159 } // namespace gnash