Object.h 3.5 KB

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