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

游戏引擎

开发平台:

Visual C++

  1. /*
  2. ** Haaf's Game Engine 1.7
  3. ** Copyright (C) 2003-2007, 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, int resgroup)
  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=resgroup;
  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, int resgroup)
  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=resgroup;
  128. resource->mipmap=false;
  129. strcpy(resource->name, name);
  130. strcpy(resource->filename, name);
  131. AddRes(this, RES_TEXTURE, resource);
  132. return reshandle;
  133. }
  134. }
  135. return 0;
  136. }
  137. HEFFECT hgeResourceManager::GetEffect(const char *name, int resgroup)
  138. {
  139. HEFFECT reshandle;
  140. REffect *resource;
  141. ResDesc *Res=FindRes(this, RES_EFFECT, name);
  142. if(Res) return (HEFFECT)Res->Get(this);
  143. else
  144. {
  145. reshandle=hge->Effect_Load(name);
  146. if(reshandle)
  147. {
  148. resource=new REffect();
  149. resource->handle=reshandle;
  150. resource->resgroup=resgroup;
  151. strcpy(resource->name, name);
  152. strcpy(resource->filename, name);
  153. AddRes(this, RES_EFFECT, resource);
  154. return reshandle;
  155. }
  156. }
  157. return 0;
  158. }
  159. HMUSIC hgeResourceManager::GetMusic(const char *name, int resgroup)
  160. {
  161. HMUSIC reshandle;
  162. RMusic *resource;
  163. ResDesc *Res=FindRes(this, RES_MUSIC, name);
  164. if(Res) return (HMUSIC)Res->Get(this);
  165. else
  166. {
  167. reshandle=hge->Music_Load(name);
  168. if(reshandle)
  169. {
  170. resource=new RMusic();
  171. resource->handle=reshandle;
  172. resource->resgroup=resgroup;
  173. strcpy(resource->name, name);
  174. strcpy(resource->filename, name);
  175. AddRes(this, RES_MUSIC, resource);
  176. return reshandle;
  177. }
  178. }
  179. return 0;
  180. }
  181. HSTREAM hgeResourceManager::GetStream(const char *name, int resgroup)
  182. {
  183. HSTREAM reshandle;
  184. RStream *resource;
  185. ResDesc *Res=FindRes(this, RES_STREAM, name);
  186. if(Res) return (HSTREAM)Res->Get(this);
  187. else
  188. {
  189. reshandle=hge->Stream_Load(name);
  190. if(reshandle)
  191. {
  192. resource=new RStream();
  193. resource->handle=reshandle;
  194. resource->resgroup=resgroup;
  195. strcpy(resource->name, name);
  196. strcpy(resource->filename, name);
  197. AddRes(this, RES_STREAM, resource);
  198. return reshandle;
  199. }
  200. }
  201. return 0;
  202. }
  203. HTARGET hgeResourceManager::GetTarget(const char *name)
  204. {
  205. ResDesc *Res=FindRes(this, RES_TARGET, name);
  206. if(Res) return (HTARGET)Res->Get(this);
  207. else return 0;
  208. }
  209. hgeSprite* hgeResourceManager::GetSprite(const char *name)
  210. {
  211. ResDesc *Res=FindRes(this, RES_SPRITE, name);
  212. if(Res) return (hgeSprite *)Res->Get(this);
  213. else return 0;
  214. }
  215. hgeAnimation* hgeResourceManager::GetAnimation(const char *name)
  216. {
  217. ResDesc *Res=FindRes(this, RES_ANIMATION, name);
  218. if(Res) return (hgeAnimation *)Res->Get(this);
  219. else return 0;
  220. }
  221. hgeFont* hgeResourceManager::GetFont(const char *name)
  222. {
  223. ResDesc *Res=FindRes(this, RES_FONT, name);
  224. if(Res) return (hgeFont *)Res->Get(this);
  225. else return 0;
  226. }
  227. hgeParticleSystem* hgeResourceManager::GetParticleSystem(const char *name)
  228. {
  229. ResDesc *Res=FindRes(this, RES_PARTICLE, name);
  230. if(Res) return (hgeParticleSystem *)Res->Get(this);
  231. else return 0;
  232. }
  233. hgeDistortionMesh* hgeResourceManager::GetDistortionMesh(const char *name)
  234. {
  235. ResDesc *Res=FindRes(this, RES_DISTORT, name);
  236. if(Res) return (hgeDistortionMesh *)Res->Get(this);
  237. else return 0;
  238. }
  239. hgeStringTable* hgeResourceManager::GetStringTable(const char *name, int resgroup)
  240. {
  241. hgeStringTable *strtable;
  242. RStringTable *resource;
  243. ResDesc *Res=FindRes(this, RES_STRTABLE, name);
  244. if(Res) return (hgeStringTable*)Res->Get(this);
  245. else
  246. {
  247. strtable=new hgeStringTable(name);
  248. if(strtable)
  249. {
  250. resource=new RStringTable();
  251. resource->handle=(DWORD)strtable;
  252. resource->resgroup=resgroup;
  253. strcpy(resource->name, name);
  254. strcpy(resource->filename, name);
  255. AddRes(this, RES_STRTABLE, resource);
  256. return strtable;
  257. }
  258. }
  259. return 0;
  260. }