store.c 9.7 KB

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