TextureDictionary.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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, TextureHandle::TextureHandleType type, bool clamp)
  42. {
  43. // Sanity!
  44. AssertISV( type != TextureHandle::InvalidTexture, "Invalid texture type." );
  45. U32 key = HashPointer(textureKey) % smHashTableSize;
  46. TextureObject *walk = smTable[key];
  47. for(; walk; walk = walk->hashNext)
  48. if(walk->mTextureKey == textureKey && walk->mHandleType == type && walk->mClamp == clamp)
  49. break;
  50. return walk;
  51. }
  52. //--------------------------------------------------------------------------------------------------------------------
  53. void TextureDictionary::remove(TextureObject *object)
  54. {
  55. if(object->next)
  56. object->next->prev = object->prev;
  57. if(object->prev)
  58. object->prev->next = object->next;
  59. else
  60. TextureObjectChain = object->next;
  61. if( object->mTextureKey == NULL )
  62. return;
  63. U32 key = HashPointer(object->mTextureKey) % smHashTableSize;
  64. TextureObject **walk = &smTable[key];
  65. while(*walk)
  66. {
  67. if(*walk == object)
  68. {
  69. *walk = object->hashNext;
  70. break;
  71. }
  72. walk = &((*walk)->hashNext);
  73. }
  74. }
  75. //--------------------------------------------------------------------------------------------------------------------
  76. void TextureDictionary::insert(TextureObject *object)
  77. {
  78. object->next = TextureObjectChain;
  79. object->prev = NULL;
  80. if(TextureObjectChain)
  81. TextureObjectChain->prev = object;
  82. TextureObjectChain = object;
  83. if(object->mTextureKey)
  84. {
  85. U32 key = HashPointer(object->mTextureKey) % smHashTableSize;
  86. object->hashNext = smTable[key];
  87. smTable[key] = object;
  88. }
  89. }
  90. //--------------------------------------------------------------------------------------------------------------------
  91. void TextureDictionary::destroy()
  92. {
  93. while(TextureObjectChain)
  94. TextureManager::freeTexture(TextureObjectChain);
  95. delete[] smTable;
  96. }