Umorna -- Tiny game written to test 'chads' library, it uses assets from itch.io...
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

200 lines
7.4KB

  1. #include "game.h"
  2. #include "render.h"
  3. #include "engine.h"
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. static void (* action_list [signal_count]) (void);
  8. static int menu_items [menu_limit];
  9. static int menu_show [menu_limit];
  10. static char * menu_text [menu_limit] [menu_item_limit];
  11. static int menu_icon [menu_limit] [menu_item_limit];
  12. static void (* menu_action [menu_limit] [menu_item_limit]) (void);
  13. static void limit (int * pointer, int minimum, int maximum) {
  14. if (* pointer < minimum) { * pointer = minimum; }
  15. if (* pointer > maximum) { * pointer = maximum; }
  16. }
  17. static void idle_game (void) { return; }
  18. static void exit_game (void) { engine_active = 0; }
  19. static void camera_move_right (void) { camera_x++; limit (& camera_x, 0, world_width - (window_width () / (int) (BASE_SIZE * render_zoom))); }
  20. static void camera_move_left (void) { camera_x--; limit (& camera_x, 0, world_width - (window_width () / (int) (BASE_SIZE * render_zoom))); }
  21. static void camera_move_down (void) { camera_y++; limit (& camera_y, 0, world_height - (window_height () / (int) (BASE_SIZE * render_zoom))); }
  22. static void camera_move_up (void) { camera_y--; limit (& camera_y, 0, world_height - (window_height () / (int) (BASE_SIZE * render_zoom))); }
  23. static void show_trait_menu (void) { menu_show [menu_traits] = menu_show [menu_traits] ? 0 : 1; }
  24. static void show_skill_menu (void) { menu_show [menu_skills] = menu_show [menu_skills] ? 0 : 1; }
  25. static void show_value_menu (void) { menu_show [menu_values] = menu_show [menu_values] ? 0 : 1; }
  26. static void render_layout (int x, int y, int width, int height, int small, int alpha) {
  27. int i, j, u, v;
  28. u = width % ICON_SIZE;
  29. v = height % ICON_SIZE;
  30. width /= ICON_SIZE;
  31. height /= ICON_SIZE;
  32. if (alpha == 0) {
  33. for (i = 0; i < width - 1; ++i) {
  34. for (j = 0; j < height - 1; ++j) {
  35. render_sprite (menus, x + i * ICON_SIZE + ICON_SIZE / 2, y + j * ICON_SIZE + ICON_SIZE / 2, 0, 0, ICON_SIZE, ICON_SIZE);
  36. }
  37. render_sprite (menus, x + i * ICON_SIZE + ICON_SIZE / 2, y + height * ICON_SIZE + ICON_SIZE / 2, 0, 0, u, ICON_SIZE);
  38. }
  39. render_sprite (menus, x + width * ICON_SIZE + ICON_SIZE / 2, y + height * ICON_SIZE + ICON_SIZE / 2, 0, 0, ICON_SIZE, v);
  40. }
  41. for (i = (small == 0) + 1; i < width - (small != 0); ++i) {
  42. render_sprite (menus, x + (i - (small == 0)) * ICON_SIZE, y, 0, 544, ICON_SIZE, ICON_SIZE);
  43. render_sprite (menus, x + (i - (small == 0)) * ICON_SIZE, y + ICON_SIZE * (height - 1) + v, 0, 544, ICON_SIZE, ICON_SIZE);
  44. }
  45. for (i = (small == 0) + 1; i < height - (small != 0); ++i) {
  46. render_sprite (menus, x, y + (i - (small == 0)) * ICON_SIZE, 256, 128, ICON_SIZE, ICON_SIZE);
  47. render_sprite (menus, x + ICON_SIZE * (width - 1) + u, y + (i - (small == 0)) * ICON_SIZE + v, 256, 128, ICON_SIZE, ICON_SIZE);
  48. }
  49. u += ICON_SIZE * (width - 1 - ((small == 0) ? 2 : 1)); /* TOIDOI */
  50. v += ICON_SIZE * (height - 1 - ((small == 0) ? 2 : 1));
  51. if (small == 0) {
  52. render_sprite (menus, x, y, 0, 256, 3 * ICON_SIZE, 3 * ICON_SIZE);
  53. render_sprite (menus, x + u, y, 96, 256, 3 * ICON_SIZE, 3 * ICON_SIZE);
  54. render_sprite (menus, x, y + v, 0, 352, 3 * ICON_SIZE, 3 * ICON_SIZE);
  55. render_sprite (menus, x + u, y + v, 96, 352, 3 * ICON_SIZE, 3 * ICON_SIZE);
  56. } else {
  57. render_sprite (menus, x, y, 192, 256, 2 * ICON_SIZE, 2 * ICON_SIZE);
  58. render_sprite (menus, x + u, y, 256, 256, 2 * ICON_SIZE, 2 * ICON_SIZE);
  59. render_sprite (menus, x, y + v, 192, 320, 2 * ICON_SIZE, 2 * ICON_SIZE);
  60. render_sprite (menus, x + u, y + v, 256, 320, 2 * ICON_SIZE, 2 * ICON_SIZE);
  61. }
  62. }
  63. static void define_menu_item (int menu, char * text, int icon, void (* action) (void)) {
  64. menu_text [menu] [menu_items [menu]] = text;
  65. menu_icon [menu] [menu_items [menu]] = icon;
  66. menu_action [menu] [menu_items [menu]] = action;
  67. menu_items [menu]++;
  68. }
  69. int camera_x = 0;
  70. int camera_y = 0;
  71. void render_side (void) {
  72. int offset_x = window_width () - 512 + ICON_SIZE - SIDE_SIZE;
  73. int offset_y = window_height () - 512 + ICON_SIZE;
  74. render_sprite (menus, ICON_SIZE, offset_y, 512, 0, 512, 512);
  75. render_sprite (menus, offset_x, ICON_SIZE, 512, 512, 512, 512);
  76. render_layout (window_width () - SIDE_SIZE, 0, SIDE_SIZE, window_height (), 0, 0);
  77. render_layout (0, 0, window_width () - SIDE_SIZE, window_height (), 0, 1);
  78. }
  79. void render_menu (int menu, int x, int y, int small, int align, int alpha) {
  80. int item, aligned_x, aligned_y, i, j;
  81. if (menu_show [menu] == 0) {
  82. return;
  83. }
  84. i = (int) strlen (menu_text [menu] [0]);
  85. j = menu_items [menu];
  86. i += 2 * (small == 0) + 2;
  87. j += 2 * (small == 0) + 2;
  88. aligned_x = (align == 0) ? x : ((window_width () - ICON_SIZE * i) / 2);
  89. aligned_y = (align == 0) ? y : ((window_height () - ICON_SIZE * j) / 2);
  90. render_layout (aligned_x, aligned_y, i * ICON_SIZE, j * ICON_SIZE, small, alpha);
  91. aligned_x += ICON_SIZE + (small == 0) * ICON_SIZE;
  92. aligned_y += ICON_SIZE + (small == 0) * ICON_SIZE;
  93. for (item = 0; item < menu_items [menu]; ++item) {
  94. i = (menu_icon [menu] [item] / 10) * ICON_SIZE;
  95. j = (menu_icon [menu] [item] % 10) * ICON_SIZE;
  96. render_sprite (icons, aligned_x, aligned_y + item * ICON_SIZE, i, j, ICON_SIZE, ICON_SIZE);
  97. render_string (menu_text [menu] [item], aligned_x + ICON_SIZE, aligned_y + item * ICON_SIZE);
  98. }
  99. }
  100. void render_map (int offset_x, int offset_y) {
  101. int x, y, index;
  102. for (x = 0; (x < (window_width () - SIDE_SIZE) / (BASE_SIZE * render_zoom)) && (x < world_width); ++x) {
  103. for (y = 0; (y < window_height () / (BASE_SIZE * render_zoom)) && (y < world_height); ++y) {
  104. render_sprite (ashlands, (int) (x * BASE_SIZE * render_zoom), (int) (y * BASE_SIZE * render_zoom),
  105. 0,
  106. world [0] [(offset_x + x) * world_height + offset_y + y] * BASE_SIZE, BASE_SIZE, BASE_SIZE);
  107. }
  108. }
  109. (void)index;
  110. /*for (index = 0; index < world_model_limit; ++index) {
  111. if (world_model_x [0] [index] < offset_x) continue;
  112. if (world_model_y [0] [index] < offset_y) continue;
  113. if (world_model_x [0] [index] > (window_width () - SIDE_SIZE) / (BASE_SIZE * render_zoom)) continue;
  114. if (world_model_y [0] [index] > window_height () / (BASE_SIZE * render_zoom)) continue;
  115. x = (int) ((world_model_x [0] [index] - offset_x) * BASE_SIZE * render_zoom);
  116. y = (int) ((world_model_y [0] [index] - offset_y) * BASE_SIZE * render_zoom);
  117. render_sprite (model_1_1, x, y, 0, world_model [0] [index] * BASE_SIZE, BASE_SIZE, BASE_SIZE);
  118. }*/
  119. }
  120. void bind (int signal_id, void (* action) (void)) {
  121. action_list [signal_id] = action;
  122. }
  123. void unbind (int signal_id) {
  124. action_list [signal_id] = idle_game;
  125. }
  126. void layout_configure (void) {
  127. int index;
  128. for (index = 0; index < signal_count; ++index) {
  129. unbind (index);
  130. }
  131. bind (signal_escape, exit_game);
  132. bind (signal_up, camera_move_up);
  133. bind (signal_down, camera_move_down);
  134. bind (signal_left, camera_move_left);
  135. bind (signal_right, camera_move_right);
  136. bind (signal_t, show_trait_menu);
  137. bind (signal_s, show_skill_menu);
  138. bind (signal_v, show_value_menu);
  139. for (index = 0; index < trait_count; ++index) {
  140. define_menu_item (menu_traits, trait_name [index], index, NULL);
  141. }
  142. for (index = 0; index < skill_count; ++index) {
  143. define_menu_item (menu_skills, skill_name [index], 3 + index, NULL);
  144. }
  145. for (index = 0; index < value_count; ++index) {
  146. define_menu_item (menu_values, value_name [index], 27 + index, NULL);
  147. }
  148. }
  149. void layout_synchronize (void) {
  150. action_list [signal] ();
  151. }