store.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #include "eruta.h"
  2. #include "store.h"
  3. /**
  4. * Store is a "global" resource container. It's simply an array of STORE_MAX
  5. * resources. Rationale for using global state is that this container is a
  6. * singleton for the whole application. The global state of the store isn't
  7. * accessible directly, but only though safer accessor functions.
  8. */
  9. #define STORE_MAX 30000
  10. static Resor * store_array[STORE_MAX];
  11. /* Initialises the resource storage. */
  12. bool store_init() {
  13. int index;
  14. for (index =0; index < STORE_MAX; index ++) {
  15. store_array[index] = NULL;
  16. }
  17. return true;
  18. }
  19. /* Range check for the index. */
  20. bool store_index_ok(int index) {
  21. if (index < 0) return FALSE;
  22. if (index > STORE_MAX) return FALSE;
  23. return TRUE;
  24. }
  25. /* Returns STORE_MAX */
  26. int store_max() {
  27. return STORE_MAX;
  28. }
  29. /* Gets a resource with the given index. */
  30. Resor * store_get(int index) {
  31. if(!store_index_ok(index)) return NULL;
  32. return store_array[index];
  33. }
  34. /* Puts a resource in the store without cleaning up what was there before. */
  35. static Resor * store_put_raw(int index, Resor * value) {
  36. if(!store_index_ok(index)) return NULL;
  37. return store_array[index] = value;
  38. }
  39. /* Puts a null without cleaning up what was there before. */
  40. static Resor * store_put_null(int index) {
  41. return store_put_raw(index, NULL);
  42. }
  43. /* Drops the stores resouce with the given index.
  44. * The resource will be cleaned up.
  45. */
  46. bool store_drop(int index) {
  47. bool res = false;
  48. Resor * old = store_get(index);
  49. if (old) {
  50. resor_free(old);
  51. res = true;
  52. }
  53. store_put_null(index);
  54. return res;
  55. }
  56. /* Puts a resource in the store. Any old value at the same
  57. * index will be dropped first, unless if value is NULL. */
  58. Resor * store_put(int index, Resor * value) {
  59. Resor * old;
  60. if(!store_index_ok(index)) return NULL;
  61. store_drop(index);
  62. return store_put_raw(index, value);
  63. }
  64. /* Cleans up the store. */
  65. bool store_done() {
  66. int index;
  67. for (index = 0; index < STORE_MAX; index ++) {
  68. store_drop(index);
  69. }
  70. return true;
  71. }
  72. /* Loads a font and puts it in the store. */
  73. Resor *
  74. store_load_ttf_font_stretch(int index, const char * vpath, int w, int h, int f) {
  75. return store_put(index, resor_load_ttf_font_stretch(vpath, w, h, f));
  76. }
  77. /* Loads a font and puts it in the store. */
  78. Resor *
  79. store_load_ttf_font(int index, const char * vpath, int h, int f) {
  80. return store_put(index, resor_load_ttf_font(vpath, h, f));
  81. }
  82. /* Loads a font and puts it in the store. */
  83. Resor *
  84. store_load_bitmap_font_flags(int index, const char * vpath, int f) {
  85. return store_put(index, resor_load_bitmap_font_flags(vpath, f));
  86. }
  87. /* Loads a font and puts it in the store. */
  88. Resor *
  89. store_load_bitmap_font(int index, const char * vpath) {
  90. return store_put(index, resor_load_bitmap_font(vpath));
  91. }
  92. /* Grabs a font from a bitmap and puts it in the store */
  93. Resor *
  94. store_grab_font_from_resor(int index, Resor * resor, int count, int ranges[]) {
  95. return store_put(index, resor_grab_font_from_resor(resor, count, ranges));
  96. }
  97. /* Grabs a font from a bitmap resouce at bmp_index and puts it in the store */
  98. Resor *
  99. store_grab_font(int index, int bmp_index, int count, int ranges[]) {
  100. Resor * bmp_res;
  101. bmp_res = store_get(index);
  102. if (!bmp_res) return NULL;
  103. return store_grab_font_from_resor(index, bmp_res, count, ranges);
  104. }
  105. /* Loads an audio stream and puts it in the storage. */
  106. Resor *
  107. store_load_audio_stream
  108. (int index, const char * vpath, size_t buffer_count, int samples) {
  109. return store_put(index, resor_load_audio_stream(vpath, buffer_count, samples));
  110. }
  111. /* Loads a sample and puts it in the storage. */
  112. Resor *
  113. store_load_sample
  114. (int index, const char * vpath) {
  115. return store_put(index, resor_load_sample(vpath));
  116. }
  117. /* Loads a bitmap and puts it in the storage. */
  118. Resor *
  119. store_load_bitmap_flags
  120. (int index, const char * vpath, int flags) {
  121. return store_put(index, resor_load_bitmap_flags(vpath, flags));
  122. }
  123. /* Loads a bitmap and puts it in the storage. */
  124. Resor *
  125. store_load_bitmap(int index, const char * vpath) {
  126. return store_put(index, resor_load_bitmap(vpath));
  127. }
  128. /* Loads "other" data and puts it in the storage. */
  129. Resor *
  130. store_load_other(int index, const char* vpath, ResorKind kind, ResorLoader* loader,
  131. ResorDestructor* destroy, void* extra) {
  132. return store_put(index, resor_load_other(vpath, kind, loader, destroy, extra));
  133. }
  134. /* Loads a tile map and puts it in the storage. */
  135. Resor * store_load_tilemap(int index, const char * vpath) {
  136. return store_put(index, xresor_load_tilemap(vpath, NULL));
  137. }
  138. /* Returns the kind of stored item. */
  139. int store_kind(int index) {
  140. return resor_kind(store_get(index));
  141. }
  142. /* Returns the font stored at the index or nil if nothing there or not a font. */
  143. ALLEGRO_FONT * store_get_font(int index) {
  144. return resor_font(store_get(index));
  145. }
  146. /* Returns the bitmap stored at the index or nil if nothing there or not a bitmap.*/
  147. ALLEGRO_BITMAP * store_get_bitmap(int index) {
  148. return resor_bitmap(store_get(index));
  149. }
  150. /* Returns the sample stored at the index or nil if nothing there or not a sample.*/
  151. ALLEGRO_SAMPLE * store_get_sample(int index) {
  152. return resor_sample(store_get(index));
  153. }
  154. /* Returns the audio stream stored at the index or nil if nothing there or not a
  155. * sample.*/
  156. ALLEGRO_AUDIO_STREAM * store_get_audio_stream(int index) {
  157. return resor_audio_stream(store_get(index));
  158. }
  159. /* Returns an "other" type of data from storage. */
  160. void * store_get_other(int index, unsigned kind) {
  161. return resor_other(store_get(index), kind);
  162. }
  163. bool store_get_bitmap_format(int index, int *value) {
  164. return resor_get_bitmap_format(store_get(index), value);
  165. }
  166. bool store_get_bitmap_flags(int index,int *value) {
  167. return resor_get_bitmap_flags(store_get(index), value);
  168. }
  169. bool store_get_bitmap_height(int index,int *value) {
  170. return resor_get_bitmap_height(store_get(index), value);
  171. }
  172. bool store_get_bitmap_width(int index,int *value) {
  173. return resor_get_bitmap_width(store_get(index), value);
  174. }
  175. bool store_get_ustr_dimensions(int index,ALLEGRO_USTR *text, Rebox *value) {
  176. return resor_get_ustr_dimensions(store_get(index), text, value);
  177. }
  178. bool store_get_ustr_width(int index,ALLEGRO_USTR *text, int *value) {
  179. return resor_get_ustr_width(store_get(index), text, value);
  180. }
  181. bool store_get_text_dimensions(int index, char *text, Rebox *value) {
  182. return resor_get_text_dimensions(store_get(index), text, value);
  183. }
  184. bool store_get_text_width(int index, char *text, int *value) {
  185. return resor_get_text_width(store_get(index), text, value);
  186. }
  187. bool store_get_font_ascent(int index, int * value) {
  188. return resor_get_font_ascent(store_get(index), value);
  189. }
  190. bool store_get_font_descent(int index, int * value) {
  191. return resor_get_font_descent(store_get(index), value);
  192. }
  193. bool store_get_font_line_height(int index, int * value) {
  194. return resor_get_font_line_height(store_get(index), value);
  195. }
  196. /* Returns the first unused store ID larger than minimum. */
  197. int store_get_unused_id(int minimum) {
  198. int index, stop;
  199. if (minimum < 0) return -2;
  200. stop = store_max();
  201. for (index = minimum; index < stop; index++) {
  202. Resor * resource = store_get(index);
  203. if (!resource) {
  204. return index;
  205. }
  206. }
  207. return -3;
  208. }
  209. #define STORE_WITH_ID(INDEX, MIN, FUN, ARGS) \
  210. { int INDEX = store_get_unused_id(MIN); \
  211. if (FUN ARGS) { \
  212. return index; \
  213. } else { \
  214. return -1; \
  215. }}
  216. /* Loads a font and puts it in the store. . */
  217. int store_load_ttf_font_stretch_id(int min, const char * vpath, int w, int h, int f) {
  218. STORE_WITH_ID(index, min, store_load_ttf_font_stretch, (index, vpath, w, h, f))
  219. }
  220. /* Loads a font and puts it in the store. */
  221. int
  222. store_load_ttf_font_id(int min, const char * vpath, int h, int f) {
  223. STORE_WITH_ID(index, min, store_load_ttf_font, (index, vpath, h, f))
  224. }
  225. /* Loads a font and puts it in the store. */
  226. int
  227. store_load_bitmap_font_flags_id(int min, const char * vpath, int f) {
  228. STORE_WITH_ID(index, min, store_load_bitmap_font_flags, (index, vpath, f))
  229. }
  230. /* Loads a font and puts it in the store. */
  231. int
  232. store_load_bitmap_font_id(int min, const char * vpath) {
  233. STORE_WITH_ID(index, min, store_load_bitmap_font, (index, vpath))
  234. }
  235. /* Grabs a font from a bitmap resouce at bmp_index and puts it in the store */
  236. int store_grab_font_id(int min, int from, int count, int ranges[]) {
  237. STORE_WITH_ID(index, min, store_grab_font, (index, from, count, ranges));
  238. }
  239. /* Loads an audio stream and puts it in the storage. */
  240. int store_load_audio_stream_id
  241. (int min, const char * vpath, size_t buffer_count, int samples) {
  242. STORE_WITH_ID(index, min, store_load_audio_stream, (index, vpath, buffer_count, samples));
  243. }
  244. /* Loads a sample and puts it in the storage. */
  245. int store_load_sample_id(int min, const char * vpath) {
  246. STORE_WITH_ID(index, min, store_load_sample, (index, vpath));
  247. }
  248. /* Loads a bitmap and puts it in the storage. */
  249. int store_load_bitmap_flags_id
  250. (int min, const char * vpath, int flags) {
  251. STORE_WITH_ID(index, min, store_load_bitmap_flags, (index, vpath, flags));
  252. }
  253. /* Loads a bitmap and puts it in the storage. */
  254. int store_load_bitmap_id(int min, const char * vpath) {
  255. STORE_WITH_ID(index, min, store_load_bitmap, (index, vpath));
  256. }
  257. /* Loads "other" data and puts it in the storage. */
  258. int
  259. store_load_other_id(int min, const char* vpath, ResorKind kind, ResorLoader* loader,
  260. ResorDestructor* destroy, void* extra) {
  261. STORE_WITH_ID(index, min, store_load_other, (index, vpath, kind, loader, destroy, extra));
  262. }
  263. /* Loads a tile map and puts it in the storage. */
  264. int store_load_tilemap_id(int min, const char * vpath) {
  265. STORE_WITH_ID(index, min, store_load_tilemap, (index, vpath));
  266. }