遊戲中的資源管理器

 

我寫過一個2d遊戲。寫2d遊戲時大部分的精力花在gui的撰寫和寫腳本的接口上了。之前寫gui的時候,new一個button的時候就load一個texture,如果頻繁切換gui,texture就會頻繁地load和release。

所以把紋理的加載管理與gui寫在一塊是很浪費效率的。最好的方法是紋理的加載與釋放與gui無關。

感謝飯中淹教會了我這個思路:給紋理設置一個重要度,初始值1,當有gui使用到這個紋理的時候,每幀都增加這個紋理的重要度,當沒有任何程序使用這個紋理的時候,每幀減少紋理的重要度,當重要度減少到0以下,就釋放這個紋理。

這樣做的好處是,經常調用的紋理在內存中的時間比較長,這樣程序調用紋理時很容易命中。而且可以讓多個gui調用同一個紋理。

以下是代碼。我用hge的,所以調用紋理的函數是hge的函數。(*^__^*) 嘻嘻……

存儲紋理用的是數組,但是用哈希表會更好。(*^__^*) 嘻嘻……

使用方法:

先調用Init()初始化

然後要用紋理的時候:

HTEXTURE = kResourceSystem::Instance().GetTexture("aaa.bmp");

釋放紋理的時候:

kResourceSystem::Instance().ThrowTexture("aaa.bmp");

每幀便忘了在主循環函數裏調用

kResourceSystem::Instance().Upadate();


  1. #pragma once
  2. #include "hge.h"
  3. #include "Singleton.h"
  4. #define MAXRESOURCENUM 200
  5. struct sResourceList 
  6. {
  7.     char* FileName;
  8.     HTEXTURE tex;
  9.     int Importance;//重要度
  10.     int iRendered;//記錄有幾處引用
  11.     sResourceList(){
  12.         FileName = NULL;
  13.         tex = NULL;
  14.         Importance = 0;
  15.         iRendered = 0;
  16.     }
  17. };
  18. class kResourceSystem:public Pattern::Singleton<kResourceSystem>
  19. {
  20. public:
  21.     kResourceSystem(){;}
  22.     ~kResourceSystem();
  23.     void Init(HGE* Hge){hge = Hge;}
  24.     HTEXTURE GetTexture(const char* FileName);//從列表中返回一個紋理
  25.     void ThrowTexture(const char* FileName);//通知有一個紋理在某處不用了
  26.     void ThrowTexture(HTEXTURE tex);
  27.     void Update();//邏輯更新
  28. protected:
  29. private:
  30.     sResourceList m_list[MAXRESOURCENUM];
  31.     HTEXTURE CreateTexture(const char* FileName);//新建一個紋理
  32.     void ReleaseTexture(int index);//釋放紋理
  33.     int GetIndex(const char* FileName);//返回紋理在數組中的位置
  34.     HGE* hge;
  35. };
  1. #include "ResourceSystem.h"
  2. #include "iostream"
  3. using namespace std;
  4. int kResourceSystem::GetIndex(const char *FileName)
  5. {
  6.     for (int i = 0;i < MAXRESOURCENUM;i++)
  7.     {
  8.         if (m_list[i].FileName != NULL)
  9.         {
  10.             //cout << "第" << i << "個元素是:" << m_list[i].FileName << endl;
  11.         
  12.             if (strcmp(m_list[i].FileName,FileName) == 0)
  13.             {
  14.                 //cout << FileName << "與第" << i << "個元素匹配!" << endl;
  15.                 return i;
  16.             }
  17.         }
  18.     }
  19.     return -1;
  20. }
  21. HTEXTURE kResourceSystem::GetTexture(const char *FileName)
  22. {
  23.     //cout << "GetTexture:" << FileName << endl;
  24.     int nTemp;
  25.     nTemp = GetIndex(FileName);
  26.     if (nTemp == -1)
  27.     {
  28.         return CreateTexture(FileName);
  29.     }
  30.     else
  31.     {
  32.         m_list[nTemp].iRendered++;
  33.         return m_list[nTemp].tex;
  34.     }
  35.     return NULL;
  36. }
  37. void kResourceSystem::ThrowTexture(const char* FileName)
  38. {
  39.     //printf("ThrowTexture,調用函數%s文件的%u行/n",__FILE__,__LINE__);
  40.     int nTemp;
  41.     nTemp = GetIndex(FileName);
  42.     if (nTemp == -1)
  43.     {
  44.         return;
  45.     }
  46.     else{
  47.         m_list[nTemp].iRendered--;
  48.         if (m_list[nTemp].iRendered < 0)
  49.         {
  50.             m_list[nTemp].iRendered = 0;
  51.         }
  52.     }
  53. }
  54. void kResourceSystem::ReleaseTexture(int index)
  55. {
  56.     //printf("ReleaseTexture,調用函數%s文件的%u行/n",__FILE__,__LINE__);
  57.     printf("ReleaseTexture:釋放文件名:%s/n",m_list[index].FileName);
  58.     if (m_list[index].tex)
  59.     {
  60.         hge->Texture_Free(m_list[index].tex);
  61.         m_list[index].tex = NULL;
  62.     }
  63.     
  64.     delete [] m_list[index].FileName;
  65.     m_list[index].FileName = NULL;
  66.     m_list[index].iRendered = 0;
  67.     m_list[index].Importance = 0;
  68. }
  69. HTEXTURE kResourceSystem::CreateTexture(const char* FileName)
  70. {
  71.     //printf("CreateTexture,調用函數%s文件的%u行",__FILE__,__LINE__);
  72.     for (int i = 0;i < MAXRESOURCENUM;i++)
  73.     {
  74.         if (m_list[i].FileName == NULL)
  75.         {
  76.             m_list[i].FileName = new char[256];
  77.             strcpy(m_list[i].FileName,FileName);
  78.             m_list[i].tex = hge->Texture_Load(FileName);
  79.             m_list[i].Importance = 1;
  80.             m_list[i].iRendered = 1;
  81.             return m_list[i].tex;
  82.             break;
  83.         }
  84.     }
  85.     return NULL;
  86. }
  87. void kResourceSystem::Update()
  88. {
  89.     for (int i = 0;i < MAXRESOURCENUM;i++)
  90.     {
  91.         if (m_list[i].iRendered <= 0 && m_list[i].FileName !=NULL)
  92.         {
  93.             m_list[i].Importance--;
  94.             if (m_list[i].Importance <= 0)
  95.             {
  96.                 ReleaseTexture(i);
  97.             }
  98.         }
  99.         else if(m_list[i].iRendered > 0 && m_list[i].FileName != NULL)
  100.         {
  101.             m_list[i].Importance++;
  102.         }
  103.     }
  104. }
  105. void kResourceSystem::ThrowTexture(HTEXTURE tex)
  106. {
  107.     //printf("ThrowTexture,調用函數%s文件的%u行/n",__FILE__,__LINE__);
  108.     for (int i = 0;i < MAXRESOURCENUM;i++)
  109.     {
  110.         if (m_list[i].tex == tex)
  111.         {
  112.             m_list[i].iRendered--;
  113.             //cout << "第" << i << "個元素,iRendered = " << m_list[i].iRendered << endl; 
  114.             return;
  115.             break;
  116.         }
  117.     }
  118. }
  119. kResourceSystem::~kResourceSystem()
  120. {
  121.     /*for (int i = 0;i < MAXRESOURCENUM;i++)
  122.     {
  123.         if (m_list[i].FileName != NULL)
  124.         {
  125.             ReleaseTexture(i);
  126.         }
  127.     }*/
  128. }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章