00001 // /////////////////////////////////////////////////////////////////////////// 00002 // xrb_engine2_sprite.hpp by Victor Dods, created 2004/08/07 00003 // /////////////////////////////////////////////////////////////////////////// 00004 // Unless a different license was explicitly granted in writing by the 00005 // copyright holder (Victor Dods), this software is freely distributable under 00006 // the terms of the GNU General Public License, version 2. Any works deriving 00007 // from this work must also be released under the GNU GPL. See the included 00008 // file LICENSE for details. 00009 // /////////////////////////////////////////////////////////////////////////// 00010 00011 #if !defined(_XRB_ENGINE2_SPRITE_HPP_) 00012 #define _XRB_ENGINE2_SPRITE_HPP_ 00013 00014 #include "xrb.hpp" 00015 00016 #include <string> 00017 00018 #include "xrb_engine2_object.hpp" 00019 #include "xrb_gltexture.hpp" 00020 #include "xrb_resourcelibrary.hpp" 00021 00022 namespace Xrb 00023 { 00024 00025 namespace Engine2 00026 { 00027 00028 // implements drawing functions, paired with the physicality of Object 00029 class Sprite : public Object 00030 { 00031 public: 00032 00033 virtual ~Sprite () { } 00034 00035 // /////////////////////////////////////////////////////////////////// 00036 // public serialization functions 00037 // /////////////////////////////////////////////////////////////////// 00038 00039 static Sprite *Create (std::string const &texture_path); 00040 // create an instance of this class by reading from the given Serializer 00041 static Sprite *Create (Serializer &serializer); 00042 // makes calls to WriteClassSpecific for this and all superclasses 00043 virtual void Write (Serializer &serializer) const; 00044 00045 // /////////////////////////////////////////////////////////////////// 00046 // public Object interface methods 00047 // /////////////////////////////////////////////////////////////////// 00048 00049 // draws this sprite 00050 virtual void Draw ( 00051 Object::DrawData const &draw_data, 00052 Float alpha_mask) const; 00053 00054 // /////////////////////////////////////////////////////////////////// 00055 // public accessors and modifiers 00056 // /////////////////////////////////////////////////////////////////// 00057 00058 // returns the sprite texture 00059 inline Resource<GLTexture> const &GetTexture () const { return m_texture; } 00060 // returns true iff this is a "round" sprite (see comment 00061 // above m_is_round). 00062 inline bool IsRound () const { return m_is_round; } 00063 // returns the relative physical sizes (component-wise ratios of the 00064 // physical geometry scale factors over the visible geometry scale 00065 // factors). 00066 inline FloatVector2 const &PhysicalSizeRatios () const 00067 { 00068 return m_physical_size_ratios; 00069 } 00070 // returns the relative physical size -- only valid when both 00071 // components of the size ratios are equal. 00072 // see @c PhysicalSizeRatios 00073 inline Float PhysicalSizeRatio () const 00074 { 00075 ASSERT1(m_physical_size_ratios[Dim::X] == m_physical_size_ratios[Dim::Y]); 00076 return m_physical_size_ratios[Dim::X]; 00077 } 00078 // returns the calculated scale factors for the physical geometry, 00079 // based upon the object's scale factors, and the physical size ratios. 00080 inline FloatVector2 PhysicalScaleFactors () const { return m_physical_size_ratios * ScaleFactors(); } 00081 // returns the calculated scale factor for the physical geometry, 00082 // based upon the object's scale factor, and the physical size ratio. 00083 inline Float PhysicalScaleFactor () const { return PhysicalSizeRatio() * ScaleFactor(); } 00084 00085 // sets the physical size ratios 00086 void SetPhysicalSizeRatios (FloatVector2 const &physical_size_ratio); 00087 // sets the physical size ratios (both components to the given value) 00088 void SetPhysicalSizeRatio (Float physical_size_ratio); 00089 00090 // resets the physical size ratios to [1, 1] 00091 inline void ResetPhysicalSizeRatios () { SetPhysicalSizeRatio(1.0f); } 00092 00093 protected: 00094 00095 // protected constructor so you must use Create() 00096 Sprite (Resource<GLTexture> const &texture); 00097 00098 // /////////////////////////////////////////////////////////////////// 00099 // protected serialization functions 00100 // /////////////////////////////////////////////////////////////////// 00101 00102 // does the guts of serializing reading for this class (doesn't read 00103 // the object subtype) 00104 void ReadClassSpecific (Serializer &serializer); 00105 // does the guts of serializing writing for this class (doesn't write 00106 // the object subtype) 00107 void WriteClassSpecific (Serializer &serializer) const; 00108 00109 // /////////////////////////////////////////////////////////////////// 00110 // protected Object interface methods 00111 // /////////////////////////////////////////////////////////////////// 00112 00113 virtual void CalculateRadius (QuadTreeType quad_tree_type) const; 00114 00115 // /////////////////////////////////////////////////////////////////// 00116 00117 // copies the properties of the given object to this object 00118 void CloneProperties (Object const *object); 00119 00120 // the texture to apply to this 00121 Resource<GLTexture> m_texture; 00122 // if true, indicates that the effective area of the sprite is the 00123 // circle/ellipse inscribed in the square/rectangle of the sprite 00124 // texture as it would appear in the world (this can be used for 00125 // planets and such). if false, the effective area is the rectangular 00126 // area as indicated by the texture. this property can be used or 00127 // ignored by the PhysicsHandler subclass as appropriate. 00128 bool m_is_round; 00129 // the component-wise ratios of the physical to the visible scales 00130 FloatVector2 m_physical_size_ratios; 00131 }; // end of class Engine2::Sprite 00132 00133 } // end of namespace Engine2 00134 00135 } // end of namespace Xrb 00136 00137 #endif // !defined(_XRB_ENGINE2_SPRITE_HPP_)