TextureDictionary.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "graphics/TextureDictionary.h"
  23. #include "graphics/TextureManager.h"
  24. #include "console/console.h"
  25. #include "console/consoleTypes.h"
  26. #include "collection/vector.h"
  27. //-----------------------------------------------------------------------------
  28. TextureObject **TextureDictionary::smTable = NULL;
  29. TextureObject *TextureDictionary::TextureObjectChain = NULL;
  30. U32 TextureDictionary::smHashTableSize = 0;
  31. //--------------------------------------------------------------------------------------------------------------------
  32. void TextureDictionary::create()
  33. {
  34. TextureObjectChain = NULL;
  35. smHashTableSize = 1023;
  36. smTable = new TextureObject *[smHashTableSize];
  37. for(U32 i = 0; i < smHashTableSize; i++)
  38. smTable[i] = NULL;
  39. }
  40. //-----------------------------------------------------------------------------
  41. TextureObject* TextureDictionary::find( StringTableEntry textureKey )
  42. {
  43. U32 key = HashPointer(textureKey) % smHashTableSize;
  44. TextureObject *walk = smTable[key];
  45. for(; walk; walk = walk->hashNext)
  46. {
  47. if(walk->mTextureKey == textureKey)
  48. break;
  49. }
  50. return walk;
  51. }
  52. //-----------------------------------------------------------------------------
  53. TextureObject *TextureDictionary::find(StringTableEntry textureKey, TextureHandle::TextureHandleType type, bool clamp)
  54. {
  55. // Sanity!
  56. AssertISV( type != TextureHandle::InvalidTexture, "Invalid texture type." );
  57. U32 key = HashPointer(textureKey) % smHashTableSize;
  58. TextureObject *walk = smTable[key];
  59. for(; walk; walk = walk->hashNext)
  60. if(walk->mTextureKey == textureKey && walk->mHandleType == type && walk->mClamp == clamp)
  61. break;
  62. return walk;
  63. }
  64. //--------------------------------------------------------------------------------------------------------------------
  65. void TextureDictionary::remove(TextureObject *object)
  66. {
  67. if(object->next)
  68. object->next->prev = object->prev;
  69. if(object->prev)
  70. object->prev->next = object->next;
  71. else
  72. TextureObjectChain = object->next;
  73. if( object->mTextureKey == NULL )
  74. return;
  75. U32 key = HashPointer(object->mTextureKey) % smHashTableSize;
  76. TextureObject **walk = &smTable[key];
  77. while(*walk)
  78. {
  79. if(*walk == object)
  80. {
  81. *walk = object->hashNext;
  82. break;
  83. }
  84. walk = &((*walk)->hashNext);
  85. }
  86. }
  87. //--------------------------------------------------------------------------------------------------------------------
  88. void TextureDictionary::insert(TextureObject *object)
  89. {
  90. object->next = TextureObjectChain;
  91. object->prev = NULL;
  92. if(TextureObjectChain)
  93. TextureObjectChain->prev = object;
  94. TextureObjectChain = object;
  95. if(object->mTextureKey)
  96. {
  97. U32 key = HashPointer(object->mTextureKey) % smHashTableSize;
  98. object->hashNext = smTable[key];
  99. smTable[key] = object;
  100. }
  101. }
  102. //--------------------------------------------------------------------------------------------------------------------
  103. void TextureDictionary::destroy()
  104. {
  105. while(TextureObjectChain)
  106. TextureManager::freeTexture(TextureObjectChain);
  107. delete[] smTable;
  108. }