Advertisement
expired6978

Hud Component Sample

Apr 12th, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.52 KB | None | 0 0
  1. void __stdcall InstallHudComponents(HUDMenu * menu)
  2. {
  3.     BSTArrayFunctor<HUDObject*> alloc(&menu->hudComponents);
  4.  
  5.     GFxValue hudComponent;
  6.     GFxValue args[2];
  7.     args[0].SetString("hudExtension");
  8.     menu->view->Invoke("_root.getNextHighestDepth", &args[1], NULL, 0);
  9.     menu->view->Invoke("_root.createEmptyMovieClip", &hudComponent, args, 2);
  10.     args[0].SetString("hud_extension.swf");
  11.     hudComponent.Invoke("loadMovie", NULL, &args[0], 1);
  12.  
  13.     g_hudExtension = new HUDExtension(menu->view);
  14.     g_hudExtension->object = hudComponent;
  15.     alloc.Push((HUDObject*&)g_hudExtension);
  16. }
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24. template<class T>
  25. class BSTArrayFunctor
  26. {
  27. public:
  28.     BSTArrayFunctor(tArray<T> * arr, UInt32 growSize = 10, UInt32 shrinkSize = 10) : m_array(arr)
  29.     {
  30.         m_growSize = growSize;
  31.         if(m_growSize == 0)
  32.             m_growSize = 1;
  33.  
  34.         m_shrinkSize = shrinkSize;
  35.     }
  36.  
  37.     bool Allocate(UInt32 numEntries)
  38.     {
  39.         if(!m_array)
  40.             return false;
  41.  
  42.         m_array->arr.entries = (T *)FormHeap_Allocate(sizeof(T) * numEntries);
  43.         if(!m_array->arr.entries) return false;
  44.  
  45.         for(UInt32 i = 0; i < numEntries; i++)
  46.             new (&m_array->arr.entries[i]) T;
  47.  
  48.         m_array->arr.capacity = numEntries;
  49.         count = numEntries;
  50.         return true;
  51.     };
  52.  
  53.     bool Push(T & entry)
  54.     {
  55.         if(!m_array) return false;
  56.  
  57.         if(m_array->count + 1 > m_array->arr.capacity) {
  58.             if(!Grow(m_growSize))
  59.                 return false;
  60.         }
  61.  
  62.         m_array->arr.entries[m_array->count] = entry;
  63.         m_array->count++;
  64.         return true;
  65.     };
  66.  
  67.     bool Insert(UInt32 index, T & entry)
  68.     {
  69.         if(!m_array->arr.entries || index < m_array->count)
  70.             return false;
  71.  
  72.         m_array->arr.entries[index] = entry;
  73.         return true;
  74.     };
  75.  
  76.     bool Remove(UInt32 index)
  77.     {
  78.         if(!m_array->arr.entries || index < m_array->count)
  79.             return false;
  80.  
  81.         m_array->arr.entries[index] = NULL;
  82.         if(index + 1 < m_array->count) {
  83.             UInt32 remaining = m_array->count - index;
  84.             memmove_s(&m_array->arr.entries[index + 1], sizeof(T) * remaining, &m_array->arr.entries[index], sizeof(T) * remaining); // Move the rest up
  85.         }
  86.         m_array->count--;
  87.  
  88.         if(m_array->arr.capacity > m_array->count + m_shrinkSize)
  89.             Shrink();
  90.  
  91.         return true;
  92.     };
  93.  
  94.     bool Shrink()
  95.     {
  96.         if(!m_array || m_array->count == m_array->arr.capacity) return false;
  97.  
  98.         try {
  99.             UInt32 newSize = m_array->count;
  100.             T * oldArray = m_array->arr.entries;
  101.             T * newArray = (T *)FormHeap_Allocate(sizeof(T) * newSize); // Allocate new block
  102.             memmove_s(newArray, sizeof(T) * newSize, m_array->arr.entries, sizeof(T) * newSize); // Move the old block
  103.             m_array->arr.entries = newArray;
  104.             m_array->arr.capacity = m_array->count;
  105.             FormHeap_Free(oldArray); // Free the old block
  106.             return true;
  107.         }
  108.         catch(...) {
  109.             return false;
  110.         }
  111.  
  112.         return false;
  113.     }
  114.  
  115.     bool Grow(UInt32 entries)
  116.     {
  117.         if(!m_array) return false;
  118.  
  119.         try {
  120.             UInt32 oldSize = m_array->arr.capacity;
  121.             UInt32 newSize = oldSize + entries;
  122.             T * oldArray = m_array->arr.entries;
  123.             T * newArray = (T *)FormHeap_Allocate(sizeof(T) * m_array->arr.capacity + entries); // Allocate new block
  124.             memmove_s(newArray, sizeof(T) * newSize, m_array->arr.entries, sizeof(T) * m_array->arr.capacity); // Move the old block
  125.             m_array->arr.entries = newArray;
  126.             m_array->arr.capacity = newSize;
  127.             FormHeap_Free(oldArray); // Free the old block
  128.  
  129.             for(UInt32 i = oldSize; i < newSize; i++) // Allocate the rest of the free blocks
  130.                 new (&m_array->arr.entries[i]) T;
  131.  
  132.             return true;
  133.         }
  134.         catch(...) {
  135.             return false;
  136.         }
  137.  
  138.         return false;
  139.     };
  140.  
  141. private:
  142.     tArray<T> * m_array;
  143.     UInt32  m_growSize;
  144.     UInt32  m_shrinkSize;
  145. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement