read_asf.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. //----------------------------------------------------------------------------
  19. // File_Class
  20. //----------------------------------------------------------------------------
  21. class File_Class
  22. {
  23. public:
  24. FILE * fp;
  25. File_Class ( const char * filename, const char * mode )
  26. {
  27. fp = fopen ( filename, mode );
  28. }
  29. ~File_Class ()
  30. {
  31. if ( fp != NULL )
  32. fclose ( fp );
  33. fp = NULL;
  34. }
  35. };
  36. //----------------------------------------------------------------------------
  37. // ASF_Lexeme_Type
  38. //----------------------------------------------------------------------------
  39. enum ASF_Lexeme_Type
  40. {
  41. EOF_MARKER,
  42. TOKEN,
  43. NEWLINE
  44. };
  45. //----------------------------------------------------------------------------
  46. // ASF_Lexer
  47. //----------------------------------------------------------------------------
  48. #define MAX_TOKEN_LENGTH 512
  49. class ASF_Lexer
  50. {
  51. public:
  52. ASF_Lexer ( const char * file_name );
  53. ASF_Lexeme_Type type () const { return Current_type; }
  54. const char * text () const { return Current_text; }
  55. void advance ();
  56. protected:
  57. void skip_whitespace ();
  58. File_Class Input_file;
  59. int Current_char;
  60. ASF_Lexeme_Type Current_type;
  61. char Current_text [ MAX_TOKEN_LENGTH ];
  62. };
  63. //----------------------------------------------------------------------------
  64. // Bone_Class
  65. //----------------------------------------------------------------------------
  66. class Bone_Class
  67. {
  68. public:
  69. Bone_Class ():
  70. Name (""),
  71. Direction (Point3 (0,0,0)),
  72. Parent_bone (NULL),
  73. Next_bone (NULL),
  74. Length (0.0f)
  75. {
  76. Axis_tm.IdentityMatrix ();
  77. }
  78. ~Bone_Class () { delete [] Name; }
  79. void set_name ( const char * name )
  80. {
  81. Name = new char [ strlen (name) + 1 ];
  82. strcpy ( Name, name );
  83. }
  84. void set_next_bone ( Bone_Class * next_bone )
  85. {
  86. Next_bone = next_bone;
  87. }
  88. void set_length ( float length ) { Length = length; }
  89. void set_direction ( Point3 direction ) { Direction = direction; }
  90. void set_axis_tm ( const Matrix3 & axis_tm ) { Axis_tm = axis_tm; }
  91. void add_axis ( Axis_Names axis ) { Active_axes.add_axis ( axis ); }
  92. void set_parent ( Bone_Class * parent_bone );
  93. const char * name () const { return Name; }
  94. Bone_Class * next_bone () const { return Next_bone; }
  95. void create_node
  96. (
  97. ImpInterface * import_interface,
  98. Interface * max_interface
  99. );
  100. void add_app_data ( int chunk_id, void * data, int data_size );
  101. private:
  102. char * Name;
  103. Point3 Direction;
  104. Matrix3 Axis_tm;
  105. Bone_Class * Parent_bone;
  106. Bone_Class * Next_bone;
  107. ImpNode * Node;
  108. float Length;
  109. ASF_Data_Chunk Active_axes;
  110. };
  111. //----------------------------------------------------------------------------
  112. // Skeleton_Class
  113. //----------------------------------------------------------------------------
  114. #define DEGREES_TO_RADIANS 1.7453293e-2f
  115. class Skeleton_Class
  116. {
  117. public:
  118. Skeleton_Class
  119. (
  120. const char * file_name,
  121. ImpInterface *,
  122. Interface *
  123. );
  124. ~Skeleton_Class ();
  125. protected:
  126. void add_bone_to_list ( Bone_Class * new_bone );
  127. Bone_Class * find_bone ( const char * name );
  128. void parse_units_block ( ASF_Lexer & );
  129. void parse_root_block ( ASF_Lexer & );
  130. void parse_bonedata_block ( ASF_Lexer & );
  131. void parse_hierarchy_block ( ASF_Lexer & );
  132. void parse_hierarchy_line ( ASF_Lexer & );
  133. void parse_bone ( ASF_Lexer & );
  134. void skip_unrecognized_blocks ( ASF_Lexer & );
  135. void skip_to_next_line ( ASF_Lexer & );
  136. void match_newline ( ASF_Lexer & );
  137. void match_token ( ASF_Lexer &, const char * );
  138. void skip_token ( ASF_Lexer & );
  139. void verify_token ( ASF_Lexer & );
  140. float float_token ( ASF_Lexer & );
  141. float Angle_multiplier; // Converts angle to radians.
  142. float Length_multiplier;
  143. ImpInterface * Import_interface;
  144. Interface * Max_interface;
  145. Bone_Class * First_bone;
  146. };