langElement.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "core/strings/stringFunctions.h"
  23. #include "core/util/str.h"
  24. #include "langElement.h"
  25. //**************************************************************************
  26. // Language element
  27. //**************************************************************************
  28. Vector<LangElement*> LangElement::elementList( __FILE__, __LINE__ );
  29. //--------------------------------------------------------------------------
  30. // Constructor
  31. //--------------------------------------------------------------------------
  32. LangElement::LangElement()
  33. {
  34. elementList.push_back( this );
  35. static U32 tempNum = 0;
  36. dSprintf( (char*)name, sizeof(name), "tempName%d", tempNum++ );
  37. }
  38. //--------------------------------------------------------------------------
  39. // Find element of specified name
  40. //--------------------------------------------------------------------------
  41. LangElement * LangElement::find( const char *name )
  42. {
  43. for( U32 i=0; i<elementList.size(); i++ )
  44. {
  45. if( !dStrcmp( (char*)elementList[i]->name, name ) )
  46. {
  47. return elementList[i];
  48. }
  49. }
  50. return NULL;
  51. }
  52. //--------------------------------------------------------------------------
  53. // Delete existing elements
  54. //--------------------------------------------------------------------------
  55. void LangElement::deleteElements()
  56. {
  57. for( U32 i=0; i<elementList.size(); i++ )
  58. {
  59. delete elementList[i];
  60. }
  61. elementList.setSize( 0 );
  62. }
  63. //--------------------------------------------------------------------------
  64. // Set name
  65. //--------------------------------------------------------------------------
  66. void LangElement::setName(const char* newName )
  67. {
  68. dStrncpy( ( char* ) name, newName, sizeof( name ) );
  69. name[ sizeof( name ) - 1 ] = '\0';
  70. }
  71. //**************************************************************************
  72. // Variable
  73. //**************************************************************************
  74. U32 Var::texUnitCount = 0;
  75. Var::Var()
  76. {
  77. dStrcpy( (char*)type, "float4" );
  78. structName[0] = '\0';
  79. uniform = false;
  80. vertData = false;
  81. connector = false;
  82. sampler = false;
  83. mapsToSampler = false;
  84. texCoordNum = 0;
  85. constSortPos = cspUninit;
  86. arraySize = 1;
  87. }
  88. Var::Var( const char *inName, const char *inType )
  89. {
  90. structName[0] = '\0';
  91. uniform = false;
  92. vertData = false;
  93. connector = false;
  94. sampler = false;
  95. mapsToSampler = false;
  96. texCoordNum = 0;
  97. constSortPos = cspUninit;
  98. arraySize = 1;
  99. setName( inName );
  100. setType( inType );
  101. }
  102. void Var::setUniform(const String& constType, const String& constName, ConstantSortPosition sortPos)
  103. {
  104. uniform = true;
  105. setType(constType.c_str());
  106. setName(constName.c_str());
  107. constSortPos = cspPass;
  108. }
  109. //--------------------------------------------------------------------------
  110. // Set struct name
  111. //--------------------------------------------------------------------------
  112. void Var::setStructName(const char* newName )
  113. {
  114. dStrncpy( ( char* ) structName, newName, sizeof( structName ) );
  115. structName[ sizeof( structName ) - 1 ] = '\0';
  116. }
  117. //--------------------------------------------------------------------------
  118. // Set connect name
  119. //--------------------------------------------------------------------------
  120. void Var::setConnectName(const char* newName )
  121. {
  122. dStrncpy( ( char* ) connectName, newName, sizeof( connectName ) );
  123. connectName[ sizeof( connectName ) - 1 ] = '\0';
  124. }
  125. //--------------------------------------------------------------------------
  126. // Set type
  127. //--------------------------------------------------------------------------
  128. void Var::setType(const char *newType )
  129. {
  130. dStrncpy( ( char* ) type, newType, sizeof( type ) );
  131. type[ sizeof( type ) - 1 ] = '\0';
  132. }
  133. //--------------------------------------------------------------------------
  134. // print
  135. //--------------------------------------------------------------------------
  136. void Var::print( Stream &stream )
  137. {
  138. if( structName[0] != '\0' )
  139. {
  140. stream.write( dStrlen((char*)structName), structName );
  141. stream.write( 1, "." );
  142. }
  143. stream.write( dStrlen((char*)name), name );
  144. }
  145. //--------------------------------------------------------------------------
  146. // Get next available texture unit number
  147. //--------------------------------------------------------------------------
  148. U32 Var::getTexUnitNum(U32 numElements)
  149. {
  150. U32 ret = texUnitCount;
  151. texUnitCount += numElements;
  152. return ret;
  153. }
  154. //--------------------------------------------------------------------------
  155. // Reset
  156. //--------------------------------------------------------------------------
  157. void Var::reset()
  158. {
  159. texUnitCount = 0;
  160. }
  161. //**************************************************************************
  162. // Multi line statement
  163. //**************************************************************************
  164. void MultiLine::addStatement( LangElement *elem )
  165. {
  166. AssertFatal( elem, "Attempting to add empty statement" );
  167. mStatementList.push_back( elem );
  168. }
  169. //--------------------------------------------------------------------------
  170. // Print
  171. //--------------------------------------------------------------------------
  172. void MultiLine::print( Stream &stream )
  173. {
  174. for( U32 i=0; i<mStatementList.size(); i++ )
  175. {
  176. mStatementList[i]->print( stream );
  177. }
  178. }