游戏中的资源管理器

来源:互联网 发布:上海电气电站待遇知乎 编辑:程序博客网 时间:2024/04/25 23:22
 

我写过一个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. }

原创粉丝点击