main.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*================================================================
  2. * Copyright: 2020 John Jackson
  3. * simple_containers
  4. Simple example demonstrating how to use various included
  5. container types core to the gunslinger library.
  6. Included:
  7. Dynamic Array -> gs_dyn_array
  8. Hash Table -> gs_hash_table
  9. Slot Array -> gs_slot_array
  10. Press `esc` to exit the application.
  11. =================================================================*/
  12. #include <gs.h>
  13. // Forward Decls.
  14. gs_result app_update(); // Use to update your application
  15. gs_result app_init(); // Use to init application
  16. gs_result app_shutdown(); // Use to shutdown application
  17. // Custom struct object for various containers
  18. typedef struct object_t
  19. {
  20. f32 float_value;
  21. u32 uint_value;
  22. } object_t;
  23. /*
  24. Declaring custom hash table type:
  25. * key: u64 - Key type used to reference stored data
  26. * val: object_t - Value type of stored in table
  27. * hash_key_func: gs_hash_u64 - Hash function used to hash the key (stored in gs_util.h)
  28. * hash_comp_func: gs_hash_key_comp_std_type - Hash compare function (default stored in gs_util.h)
  29. */
  30. gs_hash_table_decl(u64, object_t, gs_hash_u64, gs_hash_key_comp_std_type);
  31. /*
  32. Declaring custom slot array type:
  33. * type: object_t - Value type of stored in table
  34. */
  35. gs_slot_array_decl(object_t);
  36. // Globals
  37. gs_dyn_array(object_t) g_dyn_array;
  38. gs_hash_table(u64, object_t) g_hash_table;
  39. gs_slot_array(object_t) g_slot_array;
  40. u32 g_cur_val;
  41. // Util functions
  42. void print_console_commands();
  43. void print_array(gs_dyn_array(object_t)*);
  44. void print_slot_array(gs_slot_array(object_t)*);
  45. void print_hash_table(gs_hash_table(u64, object_t)*);
  46. void object_to_str(object_t* obj, char* str, usize str_sz);
  47. int main(int argc, char** argv)
  48. {
  49. // This is our app description. It gives internal hints to our engine for various things like
  50. // window size, title, as well as update, init, and shutdown functions to be run.
  51. gs_application_desc_t app = {0};
  52. app.window_title = "Simple Containers";
  53. app.window_width = 800;
  54. app.window_height = 600;
  55. app.update = &app_update;
  56. app.init = &app_init;
  57. app.shutdown = &app_shutdown;
  58. // Construct internal instance of our engine
  59. gs_engine_t* engine = gs_engine_construct(app);
  60. // Run the internal engine loop until completion
  61. gs_result res = engine->run();
  62. // Check result of engine after exiting loop
  63. if (res != gs_result_success)
  64. {
  65. gs_println("Error: Engine did not successfully finish running.");
  66. return -1;
  67. }
  68. gs_println("Gunslinger exited successfully.");
  69. return 0;
  70. }
  71. gs_result app_init()
  72. {
  73. g_cur_val = 0;
  74. // Allocate containers
  75. g_dyn_array = gs_dyn_array_new(object_t);
  76. g_slot_array = gs_slot_array_new(object_t);
  77. g_hash_table = gs_hash_table_new(u64, object_t);
  78. // Add elements to containers
  79. for (g_cur_val = 0; g_cur_val < 10; ++g_cur_val)
  80. {
  81. object_t obj = {0};
  82. obj.float_value = (f32)g_cur_val;
  83. obj.uint_value = g_cur_val;
  84. // Push into dyn array
  85. gs_dyn_array_push(g_dyn_array, obj);
  86. // Push into slot array
  87. u32 id = gs_slot_array_insert(g_slot_array, obj);
  88. // Push into hash table
  89. gs_hash_table_insert(g_hash_table, (u64)g_cur_val, obj);
  90. }
  91. print_console_commands();
  92. return gs_result_success;
  93. }
  94. // Update your application here
  95. gs_result app_update()
  96. {
  97. // Grab global instance of engine
  98. gs_engine_t* engine = gs_engine_instance();
  99. // If we press the escape key, exit the application
  100. if (engine->ctx.platform->key_pressed(gs_keycode_esc))
  101. {
  102. return gs_result_success;
  103. }
  104. // Clear containers
  105. if (engine->ctx.platform->key_pressed(gs_keycode_c))
  106. {
  107. gs_dyn_array_clear(g_dyn_array);
  108. gs_hash_table_clear(g_hash_table);
  109. gs_slot_array_clear(g_slot_array);
  110. print_console_commands();
  111. }
  112. // Insert incrementing val into array
  113. if (engine->ctx.platform->key_pressed(gs_keycode_a))
  114. {
  115. object_t obj = gs_default_val();
  116. obj.float_value = (f32)g_cur_val;
  117. obj.uint_value = g_cur_val;
  118. gs_dyn_array_push(g_dyn_array, obj);
  119. gs_slot_array_insert(g_slot_array, obj);
  120. gs_hash_table_insert(g_hash_table, (u64)g_cur_val, obj);
  121. g_cur_val++;
  122. print_console_commands();
  123. }
  124. // Print out elements of dynamic array (used a timed action macro so it only prints out at set interval)
  125. if (engine->ctx.platform->key_pressed(gs_keycode_p))
  126. {
  127. // Spacing
  128. gs_println("");
  129. print_array(&g_dyn_array);
  130. print_slot_array(&g_slot_array);
  131. print_hash_table(&g_hash_table);
  132. print_console_commands();
  133. }
  134. // Otherwise, continue
  135. return gs_result_in_progress;
  136. }
  137. gs_result app_shutdown()
  138. {
  139. // Release memory for array
  140. gs_dyn_array_free(g_dyn_array);
  141. gs_hash_table_free(g_hash_table);
  142. gs_slot_array_free(g_slot_array);
  143. return gs_result_success;
  144. }
  145. void print_array(gs_dyn_array(object_t)* arr)
  146. {
  147. char buffer[256] = {0};
  148. // Array
  149. gs_println("Array: [ ");
  150. // Loop through all elements of array
  151. u32 sz = gs_dyn_array_size(*arr);
  152. for (u32 i = 0; i < sz; ++i)
  153. {
  154. object_to_str(&(*arr)[i], buffer, 256);
  155. gs_println("\t%s", buffer);
  156. }
  157. gs_printf(" ]\n");
  158. }
  159. void print_slot_array(gs_slot_array(object_t)* sa)
  160. {
  161. // Slot Array
  162. gs_println("Slot Array: [ ");
  163. char buffer[256] = {0};
  164. for (
  165. gs_slot_array_iter(object_t) it = gs_slot_array_iter_new(*sa);
  166. gs_slot_array_iter_valid(*sa, it);
  167. gs_slot_array_iter_advance(*sa, it)
  168. )
  169. {
  170. object_t* obj = it.data;
  171. object_to_str(obj, buffer, 256);
  172. gs_println("\t%s, %zu", buffer, it.cur_idx);
  173. }
  174. gs_printf(" ]\n");
  175. }
  176. void print_hash_table(gs_hash_table(u64, object_t)* ht)
  177. {
  178. // Temp buffer for printing object
  179. char tmp_buffer[256] = {0};
  180. // Hash Table
  181. gs_println("Hash Table: [ ");
  182. // Use iterator to iterate through hash table and print elements
  183. for (
  184. gs_hash_table_iter(u64, object_t) it = gs_hash_table_iter_new(*ht);
  185. gs_hash_table_iter_valid(*ht, it);
  186. gs_hash_table_iter_advance(*ht, it))
  187. {
  188. u64 k = it.data->key;
  189. object_t* v = &it.data->val;
  190. object_to_str(v, tmp_buffer, 256);
  191. gs_println("\t{ k: %zu, %s } ", k, tmp_buffer);
  192. }
  193. gs_println("]");
  194. }
  195. void object_to_str(object_t* obj, char* str, usize str_sz)
  196. {
  197. gs_snprintf(str, str_sz, "{ %.2f, %zu }", obj->float_value, obj->uint_value);
  198. }
  199. void print_console_commands()
  200. {
  201. // Spacing
  202. gs_for_range_i(2)
  203. {
  204. gs_println("");
  205. }
  206. // Command line options (so fancy)
  207. gs_println("Options (press key): ");
  208. gs_println(" * A: Add new object into containers");
  209. gs_println(" * P: Print all containers");
  210. gs_println(" * C: Clear all containers");
  211. gs_println(" * Esc: Quit program");
  212. }