hgeresource.cpp
上传用户:maxiaolivb
上传日期:2022-06-07
资源大小:915k
文件大小:6k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. /*
  2. ** Haaf's Game Engine 1.5
  3. ** Copyright (C) 2003-2004, Relish Games
  4. ** hge.relishgames.com
  5. **
  6. ** hgeResourceManager helper class implementation
  7. */
  8. #include "....includehgeresource.h"
  9. #include "parser.h"
  10. #include "resources.h"
  11. HGE *hgeResourceManager::hge=0;
  12. hgeResourceManager::hgeResourceManager(const char *scriptname)
  13. {
  14. hge=hgeCreate(HGE_VERSION);
  15. for(int i=0;i<RESTYPES;i++) res[i]=0;
  16. _parse_script(scriptname);
  17. }
  18. hgeResourceManager::~hgeResourceManager()
  19. {
  20. _remove_all();
  21. hge->Release();
  22. }
  23. void hgeResourceManager::_parse_script(const char *scriptname)
  24. {
  25. ResDesc *rc, *rcnext;
  26. if(scriptname)
  27. {
  28. RScript::Parse(this, NULL, scriptname, NULL);
  29. rc=res[RES_SCRIPT];
  30. while(rc)
  31. {
  32. rc->Free();
  33. rcnext=rc->next;
  34. delete rc;
  35. rc=rcnext;
  36. }
  37. res[RES_SCRIPT]=0;
  38. }
  39. }
  40. void hgeResourceManager::_remove_all()
  41. {
  42. int i;
  43. ResDesc *rc, *rcnext;
  44. for(i=0;i<RESTYPES;i++)
  45. {
  46. rc=res[i];
  47. while(rc)
  48. {
  49. rc->Free();
  50. rcnext=rc->next;
  51. delete rc;
  52. rc=rcnext;
  53. }
  54. res[i]=0;
  55. }
  56. }
  57. void hgeResourceManager::ChangeScript(const char *scriptname)
  58. {
  59. _remove_all();
  60. _parse_script(scriptname);
  61. }
  62. bool hgeResourceManager::Precache(int groupid)
  63. {
  64. int i;
  65. ResDesc *rc;
  66. bool bResult=true;
  67. for(i=0;i<RESTYPES;i++)
  68. {
  69. rc=res[i];
  70. while(rc)
  71. {
  72. if(!groupid || groupid==rc->resgroup) bResult=bResult && (rc->Get(this)!=0);
  73. rc=rc->next;
  74. }
  75. }
  76. return bResult;
  77. }
  78. void hgeResourceManager::Purge(int groupid)
  79. {
  80. int i;
  81. ResDesc *rc;
  82. for(i=0;i<RESTYPES;i++)
  83. {
  84. rc=res[i];
  85. while(rc)
  86. {
  87. if(!groupid || groupid==rc->resgroup) rc->Free();
  88. rc=rc->next;
  89. }
  90. }
  91. }
  92. void* hgeResourceManager::GetResource(const char *name)
  93. {
  94. void *reshandle;
  95. RResource *resource;
  96. ResDesc *Res=FindRes(this, RES_RESOURCE, name);
  97. if(Res) return (void *)Res->Get(this);
  98. else
  99. {
  100. reshandle=hge->Resource_Load(name);
  101. if(reshandle)
  102. {
  103. resource=new RResource();
  104. resource->handle=(DWORD)reshandle;
  105. resource->resgroup=0;
  106. strcpy(resource->name, name);
  107. strcpy(resource->filename, name);
  108. AddRes(this, RES_RESOURCE, resource);
  109. return reshandle;
  110. }
  111. }
  112. return 0;
  113. }
  114. HTEXTURE hgeResourceManager::GetTexture(const char *name)
  115. {
  116. HTEXTURE reshandle;
  117. RTexture *resource;
  118. ResDesc *Res=FindRes(this, RES_TEXTURE, name);
  119. if(Res) return (HTEXTURE)Res->Get(this);
  120. else
  121. {
  122. reshandle=hge->Texture_Load(name);
  123. if(reshandle)
  124. {
  125. resource=new RTexture();
  126. resource->handle=reshandle;
  127. resource->resgroup=0;
  128. strcpy(resource->name, name);
  129. strcpy(resource->filename, name);
  130. AddRes(this, RES_TEXTURE, resource);
  131. return reshandle;
  132. }
  133. }
  134. return 0;
  135. }
  136. HEFFECT hgeResourceManager::GetEffect(const char *name)
  137. {
  138. HEFFECT reshandle;
  139. REffect *resource;
  140. ResDesc *Res=FindRes(this, RES_EFFECT, name);
  141. if(Res) return (HEFFECT)Res->Get(this);
  142. else
  143. {
  144. reshandle=hge->Effect_Load(name);
  145. if(reshandle)
  146. {
  147. resource=new REffect();
  148. resource->handle=reshandle;
  149. resource->resgroup=0;
  150. strcpy(resource->name, name);
  151. strcpy(resource->filename, name);
  152. AddRes(this, RES_EFFECT, resource);
  153. return reshandle;
  154. }
  155. }
  156. return 0;
  157. }
  158. HMUSIC hgeResourceManager::GetMusic(const char *name)
  159. {
  160. HMUSIC reshandle;
  161. RMusic *resource;
  162. ResDesc *Res=FindRes(this, RES_MUSIC, name);
  163. if(Res) return (HMUSIC)Res->Get(this);
  164. else
  165. {
  166. reshandle=hge->Music_Load(name);
  167. if(reshandle)
  168. {
  169. resource=new RMusic();
  170. resource->handle=reshandle;
  171. resource->resgroup=0;
  172. strcpy(resource->name, name);
  173. strcpy(resource->filename, name);
  174. AddRes(this, RES_MUSIC, resource);
  175. return reshandle;
  176. }
  177. }
  178. return 0;
  179. }
  180. HSTREAM hgeResourceManager::GetStream(const char *name)
  181. {
  182. HSTREAM reshandle;
  183. RStream *resource;
  184. ResDesc *Res=FindRes(this, RES_STREAM, name);
  185. if(Res) return (HSTREAM)Res->Get(this);
  186. else
  187. {
  188. reshandle=hge->Stream_Load(name);
  189. if(reshandle)
  190. {
  191. resource=new RStream();
  192. resource->handle=reshandle;
  193. resource->resgroup=0;
  194. strcpy(resource->name, name);
  195. strcpy(resource->filename, name);
  196. AddRes(this, RES_STREAM, resource);
  197. return reshandle;
  198. }
  199. }
  200. return 0;
  201. }
  202. HTARGET hgeResourceManager::GetTarget(const char *name)
  203. {
  204. ResDesc *Res=FindRes(this, RES_TARGET, name);
  205. if(Res) return (HTARGET)Res->Get(this);
  206. else return 0;
  207. }
  208. hgeSprite* hgeResourceManager::GetSprite(const char *name)
  209. {
  210. ResDesc *Res=FindRes(this, RES_SPRITE, name);
  211. if(Res) return (hgeSprite *)Res->Get(this);
  212. else return 0;
  213. }
  214. hgeAnimation* hgeResourceManager::GetAnimation(const char *name)
  215. {
  216. ResDesc *Res=FindRes(this, RES_ANIMATION, name);
  217. if(Res) return (hgeAnimation *)Res->Get(this);
  218. else return 0;
  219. }
  220. hgeFont* hgeResourceManager::GetFont(const char *name)
  221. {
  222. ResDesc *Res=FindRes(this, RES_FONT, name);
  223. if(Res) return (hgeFont *)Res->Get(this);
  224. else return 0;
  225. }
  226. hgeParticleSystem* hgeResourceManager::GetParticleSystem(const char *name)
  227. {
  228. ResDesc *Res=FindRes(this, RES_PARTICLE, name);
  229. if(Res) return (hgeParticleSystem *)Res->Get(this);
  230. else return 0;
  231. }
  232. hgeDistortionMesh* hgeResourceManager::GetDistortionMesh(const char *name)
  233. {
  234. ResDesc *Res=FindRes(this, RES_DISTORT, name);
  235. if(Res) return (hgeDistortionMesh *)Res->Get(this);
  236. else return 0;
  237. }
  238. hgeStringTable* hgeResourceManager::GetStringTable(const char *name)
  239. {
  240. hgeStringTable *strtable;
  241. RStringTable *resource;
  242. ResDesc *Res=FindRes(this, RES_STRTABLE, name);
  243. if(Res) return (hgeStringTable*)Res->Get(this);
  244. else
  245. {
  246. strtable=new hgeStringTable(name);
  247. if(strtable)
  248. {
  249. resource=new RStringTable();
  250. resource->handle=(DWORD)strtable;
  251. resource->resgroup=0;
  252. strcpy(resource->name, name);
  253. strcpy(resource->filename, name);
  254. AddRes(this, RES_STRTABLE, resource);
  255. return strtable;
  256. }
  257. }
  258. return 0;
  259. }