MaterialDef.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code 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. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "MaterialDef.h"
  23. /**
  24. * Constructor.
  25. */
  26. MaterialDef::MaterialDef(void) {
  27. type = 0;
  28. quotes = false;
  29. }
  30. /**
  31. * Destructor.
  32. */
  33. MaterialDef::~MaterialDef(void) {
  34. }
  35. /**
  36. * Returns view specific data associated with the material definition.
  37. */
  38. DWORD MaterialDef::GetViewData(const char* viewName) {
  39. DWORD* value = NULL;
  40. viewData.Get(viewName, &value);
  41. return *value;
  42. }
  43. /**
  44. * Sets view specific data for the material definition.
  45. */
  46. void MaterialDef::SetViewData(const char* viewName, DWORD value) {
  47. viewData.Set(viewName, value);
  48. }
  49. #define MATERIAL_DEF_FILE "MaterialEditorDefs.med"
  50. MaterialDefList MaterialDefManager::materialDefs[MaterialDefManager::MATERIAL_DEF_NUM];
  51. /**
  52. * Loads the material definition file instatiates MaterialDef objects for each definition
  53. * and groups the definitions.
  54. */
  55. void MaterialDefManager::InitializeMaterialDefLists() {
  56. char *buffer;
  57. int length = fileSystem->ReadFile( MATERIAL_DEF_FILE, (void **)&buffer);
  58. if ( length == -1 ) {
  59. common->Error( "Couldn't load material editor definition: %s", MATERIAL_DEF_FILE );
  60. return;
  61. }
  62. idLexer src;
  63. if ( !src.LoadMemory( buffer, length, MATERIAL_DEF_FILE ) ) {
  64. common->Error( "Couldn't parse %s", MATERIAL_DEF_FILE );
  65. fileSystem->FreeFile(buffer);
  66. }
  67. InitializeMaterialDefList(&src, "materialprops", &materialDefs[MATERIAL_DEF_MATERIAL]);
  68. InitializeMaterialDefList(&src, "stageprops", &materialDefs[MATERIAL_DEF_STAGE]);
  69. InitializeMaterialDefList(&src, "specialmapstageprops", &materialDefs[MATERIAL_DEF_SPECIAL_STAGE]);
  70. fileSystem->FreeFile(buffer);
  71. }
  72. /**
  73. * Loads a single type of material attributes and adds them to the supplied MaterialDefList object.
  74. * @param src The idLexer object that contains the file.
  75. * @param typeName The name of the attribute grouping to search for in the file.
  76. * @param list The MaterialDefList object to append the MaterialDef instances to.
  77. */
  78. void MaterialDefManager::InitializeMaterialDefList(idLexer* src, const char* typeName, MaterialDefList* list) {
  79. idToken token;
  80. src->Reset();
  81. src->SkipUntilString(typeName);
  82. src->SkipUntilString("{");
  83. while(1) {
  84. if ( !src->ExpectAnyToken( &token ) ) {
  85. //Todo: Add some error checking here
  86. return;
  87. }
  88. if ( token == "}" ) {
  89. break;
  90. }
  91. MaterialDef* newProp = new MaterialDef();
  92. if(!token.Icmp("TYPE_GROUP")) {
  93. newProp->type = MaterialDef::MATERIAL_DEF_TYPE_GROUP;
  94. } else if(!token.Icmp("TYPE_BOOL")) {
  95. newProp->type = MaterialDef::MATERIAL_DEF_TYPE_BOOL;
  96. } else if(!token.Icmp("TYPE_STRING")) {
  97. newProp->type = MaterialDef::MATERIAL_DEF_TYPE_STRING;
  98. } else if(!token.Icmp("TYPE_FLOAT")) {
  99. newProp->type = MaterialDef::MATERIAL_DEF_TYPE_FLOAT;
  100. } else if(!token.Icmp("TYPE_INT")) {
  101. newProp->type = MaterialDef::MATERIAL_DEF_TYPE_INT;
  102. }
  103. //Skip the ,
  104. src->ReadToken(&token);
  105. //Read Dict Name
  106. src->ReadToken(&token);
  107. newProp->dictName = token;
  108. //Skip the ,
  109. src->ReadToken(&token);
  110. //Read Display Name
  111. src->ReadToken(&token);
  112. newProp->displayName = token;
  113. //Skip the ,
  114. src->ReadToken(&token);
  115. //Read Display Info
  116. src->ReadToken(&token);
  117. newProp->displayInfo = token;
  118. //Type Specific Data
  119. if(newProp->type == MaterialDef::MATERIAL_DEF_TYPE_STRING) {
  120. newProp->quotes = false;
  121. //Skip the ,
  122. src->ReadToken(&token);
  123. //Read validate flag
  124. src->ReadToken(&token);
  125. if(token == "1") {
  126. newProp->quotes = true;
  127. }
  128. }
  129. src->SkipRestOfLine();
  130. list->Append(newProp);
  131. }
  132. }
  133. /**
  134. * Destroys all MaterialDef instances and clears the material attribute grouping lists.
  135. */
  136. void MaterialDefManager::DestroyMaterialDefLists() {
  137. for(int i = 0; i < MATERIAL_DEF_NUM; i++) {
  138. for(int j = 0; j < materialDefs[i].Num(); j++) {
  139. delete materialDefs[i][j];
  140. }
  141. materialDefs[i].Clear();
  142. }
  143. }
  144. /**
  145. * Returns the MaterialDefList for the specified attribute grouping.
  146. * @param type The attribute grouping for which to retreive the attribute list.
  147. */
  148. MaterialDefList* MaterialDefManager::GetMaterialDefs(int type) {
  149. if(type >= 0 && type < MATERIAL_DEF_NUM) {
  150. return &materialDefs[type];
  151. }
  152. return NULL;
  153. }