1 // -*- Mode: C++; tab-width:2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi:tw=80:et:ts=2:sts=2
3 //
4 // -----------------------------------------------------------------------
5 //
6 // This file is part of RLVM, a RealLive virtual machine clone.
7 //
8 // -----------------------------------------------------------------------
9 //
10 // Copyright (C) 2007 Elliot Glaysher
11 //
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
25 //
26 // -----------------------------------------------------------------------
27 
28 #ifndef SRC_SYSTEMS_BASE_GRAPHICS_OBJECT_DATA_H_
29 #define SRC_SYSTEMS_BASE_GRAPHICS_OBJECT_DATA_H_
30 
31 #include <boost/serialization/access.hpp>
32 
33 #include <memory>
34 #include <string>
35 #include <vector>
36 
37 class GraphicsObject;
38 class Point;
39 class RLMachine;
40 class Rect;
41 class Surface;
42 
43 // Describes what is rendered in a graphics object; Subclasses will
44 // store image or text data that need to be associated with a
45 // GraphicsObject.
46 class GraphicsObjectData {
47  public:
48   enum AfterAnimation { AFTER_NONE, AFTER_CLEAR, AFTER_LOOP };
49 
50  public:
51   GraphicsObjectData();
52   explicit GraphicsObjectData(const GraphicsObjectData& obj);
53   virtual ~GraphicsObjectData();
54 
set_after_action(AfterAnimation after)55   void set_after_action(AfterAnimation after) { after_animation_ = after; }
56 
set_owned_by(GraphicsObject & godata)57   void set_owned_by(GraphicsObject& godata) { owned_by_ = &godata; }
58 
set_is_currently_playing(bool in)59   void set_is_currently_playing(bool in) { currently_playing_ = in; }
is_currently_playing()60   bool is_currently_playing() const { return currently_playing_; }
61 
62   // Returns when an animation has completed. (This only returns true when
63   // afterAnimation() is set to AFTER_NONE.)
animation_finished()64   bool animation_finished() const { return animation_finished_; }
65 
66   virtual void Render(const GraphicsObject& go,
67                       const GraphicsObject* parent,
68                       std::ostream* tree);
69 
70   virtual int PixelWidth(const GraphicsObject& rendering_properties) = 0;
71   virtual int PixelHeight(const GraphicsObject& rendering_properties) = 0;
72 
73   virtual GraphicsObjectData* Clone() const = 0;
74 
75   virtual void Execute(RLMachine& machine) = 0;
76 
77   virtual bool IsAnimation() const;
78   virtual void PlaySet(int set);
79 
80   // Whether this object data owns another layer of objects.
81   virtual bool IsParentLayer() const;
82 
83   // Returns the destination rectangle on the screen to draw srcRect()
84   // to. Override to return custom rectangles in the case of a custom animation
85   // format.
86   virtual Rect DstRect(const GraphicsObject& go, const GraphicsObject* parent);
87 
88  protected:
89   // Function called after animation ends when this object has been
90   // set up to loop. Default implementation does nothing.
91   virtual void LoopAnimation();
92 
93   // Takes the specified action when we've reached the last frame of
94   // animation.
95   void EndAnimation();
96 
97   void PrintGraphicsObjectToTree(const GraphicsObject& go, std::ostream* tree);
98 
99   void PrintStringVector(const std::vector<std::string>& names,
100                          std::ostream* tree);
101 
102   // Template method used during rendering to get the surface to render.
103   // Return a null shared_ptr to disable rendering.
104   virtual std::shared_ptr<const Surface> CurrentSurface(
105       const GraphicsObject& rp) = 0;
106 
107   // Returns the rectangle in currentSurface() to draw to the screen. Override
108   // to return custom rectangles in the case of a custom animation format.
109   virtual Rect SrcRect(const GraphicsObject& go);
110 
111   // Returns the offset to the destination, which is set on a per surface
112   // basis. This template method can be ignored if you override dstRect().
113   virtual Point DstOrigin(const GraphicsObject& go);
114 
115   // Controls the alpha during rendering. Default implementation just consults
116   // the GraphicsObject.
117   virtual int GetRenderingAlpha(const GraphicsObject& go,
118                                 const GraphicsObject* parent);
119 
120   // Prints a description of this object for the RenderTree log.
121   virtual void ObjectInfo(std::ostream& tree) = 0;
122 
123  private:
124   // Policy of what to do after an animation is finished.
125   AfterAnimation after_animation_;
126 
127   GraphicsObject* owned_by_;
128 
129   bool currently_playing_;
130 
131   // Whether we're on the final frame (and are in AFTER_NONE mode).
132   bool animation_finished_;
133 
134   friend class boost::serialization::access;
135 
136   // boost::serialization support
137   template <class Archive>
serialize(Archive & ar,unsigned int version)138   void serialize(Archive& ar, unsigned int version) {
139     // boost::serialization should take care of the swizzling of
140     // owned_by_.
141     ar& after_animation_& owned_by_& currently_playing_;
142   }
143 };
144 
145 #endif  // SRC_SYSTEMS_BASE_GRAPHICS_OBJECT_DATA_H_
146