Object.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #ifndef OBJ_H_
  2. #define OBJ_H_
  3. #include "FileIO.h"
  4. namespace gameplay
  5. {
  6. /**
  7. * Object is the abstract base class of all the objects that can be written in the GamePlay Binary file.
  8. */
  9. class Object
  10. {
  11. public:
  12. // TypeID's
  13. enum TypeID
  14. {
  15. SCENE_ID = 1,
  16. NODE_ID = 2,
  17. ANIMATIONS_ID = 3,
  18. ANIMATION_ID = 4,
  19. ANIMATIONCHANNEL_ID = 5,
  20. NODEINSTANCE_ID = 8,
  21. MODEL_ID = 11,
  22. MATERIAL_ID = 16,
  23. EFFECT_ID = 17,
  24. CAMERA_ID = 32,
  25. LIGHT_ID = 33,
  26. MESH_ID = 34,
  27. MESHPART_ID = 35,
  28. MESHSKIN_ID = 36,
  29. FONT_ID = 128,
  30. };
  31. /**
  32. * Constructor.
  33. */
  34. Object(void);
  35. /**
  36. * Destructor.
  37. */
  38. virtual ~Object(void);
  39. /**
  40. * Returns the Object TypeID.
  41. */
  42. virtual unsigned int getTypeId(void) const;
  43. /**
  44. * Returns the string element name of the object.
  45. * Used for printing the gameplayfile as text.
  46. */
  47. virtual const char* getElementName(void) const = 0;
  48. /**
  49. * Writes this object to the file stream as binary.
  50. */
  51. virtual void writeBinary(FILE* file);
  52. /**
  53. * Writes this object to the file stream as text.
  54. */
  55. virtual void writeText(FILE* file) = 0;
  56. /**
  57. * Returns this objects id string.
  58. */
  59. const std::string& getId() const;
  60. /**
  61. * Sets this object's id string.
  62. */
  63. void setId(const char* id);
  64. /**
  65. * Sets this object's id string.
  66. */
  67. void setId(const std::string& id);
  68. /**
  69. * Prints an XML start element with the name of this object to the text file stream.
  70. * Also prints the id as an attribute if the id length is greater than zero.
  71. */
  72. void fprintElementStart(FILE* file);
  73. /**
  74. * Prints an XML end element with the name of this object to the text file stream.
  75. */
  76. void fprintElementEnd(FILE* file);
  77. /**
  78. * Writes the xref of this object to the binary file stream.
  79. */
  80. void writeBinaryXref(FILE* file);
  81. /**
  82. * Returns the file position that this object was written to.
  83. * An offset of zero means this object has not been written yet.
  84. */
  85. unsigned int getFilePosition();
  86. /**
  87. * Writes out a list of objects to a binary file stream.
  88. */
  89. template <class T>
  90. static void writeBinaryObjects(std::list<T> list, FILE* file)
  91. {
  92. // First write the size of the list
  93. write(list.size(), file);
  94. // Then write each element
  95. typename std::list<T>::const_iterator i;
  96. for (i = list.begin(); i != list.end(); ++i)
  97. {
  98. (*i)->writeBinary(file);
  99. }
  100. }
  101. /**
  102. * Writes out a vector of objects to a binary file stream.
  103. */
  104. template <class T>
  105. static void writeBinaryObjects(std::vector<T> vector, FILE* file)
  106. {
  107. // First write the size of the vector
  108. write(vector.size(), file);
  109. // Then write each element
  110. typename std::vector<T>::const_iterator i;
  111. for (i = vector.begin(); i != vector.end(); ++i)
  112. {
  113. (*i)->writeBinary(file);
  114. }
  115. }
  116. private:
  117. /**
  118. * Saves where this object was written to in the binary file.
  119. */
  120. void saveFilePosition(FILE* file);
  121. private:
  122. std::string _id;
  123. long _fposition;
  124. };
  125. }
  126. #endif