Spinning Topp Logo BlackTopp Studios
inc
simplerenderer.cpp
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _uisimplerenderer_cpp
41 #define _uisimplerenderer_cpp
42 
43 #include "UI/simplerenderer.h"
44 
45 #include "serialization.h"
46 #include "exception.h"
47 
48 namespace Mezzanine
49 {
50  namespace UI
51  {
53  Dirty(true)
54  { }
55 
57  { }
58 
59  void SimpleRenderer::PushVertex(const Real& X, const Real& Y, const Vector2& UV, const ColourValue& Colour, const String& Atlas)
60  {
61  VertexData TempVert;
62  TempVert.Vert.Position.X = X;
63  TempVert.Vert.Position.Y = Y;
64  TempVert.Vert.Position.Z = 0;
65  TempVert.Vert.UV = UV;
66  TempVert.Vert.Colour = Colour;
67  TempVert.Atlas = Atlas;
68  this->RenderVertices.push_back(TempVert);
69  }
70 
71  void SimpleRenderer::PushTriangle(const Vector2& A, const Vector2& B, const Vector2& C, const Vector2& UV, const ColourValue& Colour, const String& Atlas)
72  {
73  this->PushVertex(A.X,A.Y,UV,Colour,Atlas);
74  this->PushVertex(B.X,B.Y,UV,Colour,Atlas);
75  this->PushVertex(C.X,C.Y,UV,Colour,Atlas);
76  }
77 
78  ///////////////////////////////////////////////////////////////////////////////
79  // Utility Methods
80 
82  { this->PriAtlas = Atlas; }
83 
85  { return this->PriAtlas; }
86 
87  ///////////////////////////////////////////////////////////////////////////////
88  // Serialization
89 
90  void SimpleRenderer::ProtoSerialize(XML::Node& ParentNode) const
91  {
92  XML::Node SelfRoot = ParentNode.AppendChild( this->GetDerivedSerializableName() );
93 
94  this->ProtoSerializeProperties(SelfRoot);
95  }
96 
98  {
99  XML::Node PropertiesNode = SelfRoot.AppendChild( SimpleRenderer::GetSerializableName() + "Properties" );
100 
101  if( PropertiesNode.AppendAttribute("Version").SetValue("1") &&
102  PropertiesNode.AppendAttribute("PriAtlas").SetValue(this->PriAtlas) )
103  {
104  return;
105  }else{
106  SerializeError("Create XML Attribute Values",SimpleRenderer::GetSerializableName() + "Properties",true);
107  }
108  }
109 
111  {
112  this->ProtoDeSerializeProperties(SelfRoot);
113  }
114 
116  {
117  XML::Attribute CurrAttrib;
118  XML::Node PropertiesNode = SelfRoot.GetChild( SimpleRenderer::GetSerializableName() + "Properties" );
119 
120  if( !PropertiesNode.Empty() ) {
121  if(PropertiesNode.GetAttribute("Version").AsInt() == 1) {
122  CurrAttrib = PropertiesNode.GetAttribute("PriAtlas");
123  if( !CurrAttrib.Empty() )
124  this->PriAtlas = CurrAttrib.AsString();
125  }else{
126  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (SimpleRenderer::GetSerializableName() + "Properties") + ": Not Version 1.");
127  }
128  }else{
129  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,SimpleRenderer::GetSerializableName() + "Properties" + " was not found in the provided XML node, which was expected.");
130  }
131  }
132 
135 
137  { return "SimpleRenderer"; }
138 
139  ///////////////////////////////////////////////////////////////////////////////
140  // Internal Methods
141 
143  {
144  return this->Dirty;
145  }
146 
148  {
149  if( this->Dirty || Force ) {
150  this->RenderVertices.clear();
151  this->RedrawImpl(Force);
152  this->Dirty = false;
153  }
154  }
155 
156  void SimpleRenderer::_AppendVertices(std::vector<VertexData>& Vertices)
157  {
158  Vertices.insert(Vertices.end(),this->RenderVertices.begin(),this->RenderVertices.end());
159  }
160  }//UI
161 }//Mezzanine
162 
163 #endif
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
virtual void ProtoDeSerialize(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite this object with it.
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
static String GetSerializableName()
Get the name of the the XML tag the Renderable class will leave behind as its instances are serialize...
Boole _IsDirty()
Gets whether or not this renderer is dirty.
virtual void PushTriangle(const Vector2 &A, const Vector2 &B, const Vector2 &C, const Vector2 &UV, const ColourValue &Colour, const String &Atlas)
Pushes vertex information for a triangle to a vector. Equivalent to calling "PushVertex" three times...
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
Real X
Coordinate on the X vector.
Definition: vector3.h:85
Real Z
Coordinate on the Z vector.
Definition: vector3.h:89
Vector2 UV
The UV position (or texture coordinates) of this vertex if a texture is being applied to it...
Definition: vertex.h:60
ColourValue Colour
The colour of the vertex.
Definition: vertex.h:58
void _AppendVertices(std::vector< VertexData > &Vertices)
Appends the vertices of this renderable to another vector.
Thrown when the requested identity could not be found.
Definition: exception.h:94
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
String PriAtlas
This contains the name of the atlas that will be used as default when one isn't specified.
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
const Char8 * AsString(const Char8 *def="") const
Attempts to convert the value of the attribute to a String and returns the results.
Vertex Vert
The actual vertex data relevant to rendering.
Definition: vertex.h:68
This is a simple class for holding 4 reals representing the colour any give object or lightsource can...
Definition: colourvalue.h:64
SimpleRenderer()
Class constructor.
bool Empty() const
Is this storing anything at all?
virtual ~SimpleRenderer()
Class destructor.
This implements the exception hiearchy for Mezzanine.
virtual void ProtoSerialize(XML::Node &ParentNode) const
Convert this class to an XML::Node ready for serialization.
Vector3 Position
The 3D position of this vertex for placement in front of the frustrum.
Definition: vertex.h:56
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
std::vector< VertexData > RenderVertices
This is a container storing all the vertices generated by this renderer.
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
The interface for serialization.
bool SetValue(const Char8 *rhs)
Set the value of this.
virtual void ProtoDeSerializeProperties(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the properties of this object with it...
String Atlas
Name of the Atlas the texture belongs to if a texture is being applied to it.
Definition: vertex.h:70
Real Y
Coordinate on the Y vector.
Definition: vector2.h:69
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
Real X
Coordinate on the X vector.
Definition: vector2.h:67
int AsInt(int def=0) const
Attempts to convert the value of the attribute to an int and returns the results. ...
Boole Dirty
This determines whether or not the vertices in this renderer need to be refreshed.
This is used to represent a point on a 2 dimentional area, such as a screen.
Definition: vector2.h:63
bool Empty() const
Is this storing anything at all?
Real Y
Coordinate on the Y vector.
Definition: vector3.h:87
Basic class describing a vertex in the UI to be rendered.
Definition: vertex.h:65
virtual void RedrawImpl(Boole Force)=0
Provides the class specific implementation for regenerating vertices for this renderable.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
virtual String GetPrimaryAtlas() const
Gets the currently set primary atlas.
void SerializeError(const String &FailedTo, const String &ClassName, Boole SOrD)
Simply does some string concatenation, then throws an Exception.
void _Redraw(Boole Force)
Regenerates the verticies in this renderable.
Node AppendChild(NodeType Type=NodeElement)
Creates a Node and makes it a child of this one.
virtual void SetPrimaryAtlas(const String &Atlas)
Sets the Atlas to be assumed when one isn't provided for atlas related tasks.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
virtual String GetDerivedSerializableName() const
Gets the most derived serializable name of this Renderable.
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.
virtual void PushVertex(const Real &X, const Real &Y, const Vector2 &UV, const ColourValue &Colour, const String &Atlas)
Collects all the relevant information for a single vertex and pushes it to a vector.
Node GetChild(const Char8 *Name) const
Attempt to get a child Node with a given name.