draw.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include <game/draw.h>
  2. // Global variables to store camera position
  3. int camera_x = 0;
  4. int camera_y = 0;
  5. // Draw the user stats (health, xp, and level)
  6. void draw_user_stats(Canvas *canvas, Vector pos, GameManager *manager)
  7. {
  8. GameContext *game_context = game_manager_game_context_get(manager);
  9. PlayerContext *player = game_context->player_context;
  10. // first draw a black rectangle to make the text more readable
  11. canvas_invert_color(canvas);
  12. canvas_draw_box(canvas, pos.x - 1, pos.y - 7, 34, 21);
  13. canvas_invert_color(canvas);
  14. char health[32];
  15. char xp[32];
  16. char level[32];
  17. snprintf(health, sizeof(health), "HP : %ld", player->health);
  18. snprintf(level, sizeof(level), "LVL: %ld", player->level);
  19. if (player->xp < 10000)
  20. snprintf(xp, sizeof(xp), "XP : %ld", player->xp);
  21. else
  22. snprintf(xp, sizeof(xp), "XP : %ldK", player->xp / 1000);
  23. canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
  24. canvas_draw_str(canvas, pos.x, pos.y, health);
  25. canvas_draw_str(canvas, pos.x, pos.y + 7, xp);
  26. canvas_draw_str(canvas, pos.x, pos.y + 14, level);
  27. }
  28. void draw_username(Canvas *canvas, Vector pos, char *username)
  29. {
  30. // first draw a black rectangle to make the text more readable
  31. // draw box around the username
  32. canvas_invert_color(canvas);
  33. canvas_draw_box(canvas, pos.x - camera_x - (strlen(username) * 2) - 1, pos.y - camera_y - 14, strlen(username) * 4 + 1, 8);
  34. canvas_invert_color(canvas);
  35. // draw username over player's head
  36. canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
  37. canvas_draw_str(canvas, pos.x - camera_x - (strlen(username) * 2), pos.y - camera_y - 7, username);
  38. }
  39. // Draw a line of icons (16 width)
  40. void draw_icon_line(Canvas *canvas, Vector pos, int amount, bool horizontal, const Icon *icon)
  41. {
  42. for (int i = 0; i < amount; i++)
  43. {
  44. if (horizontal)
  45. {
  46. // check if element is outside the world
  47. if (pos.x + (i * 17) > WORLD_WIDTH)
  48. {
  49. break;
  50. }
  51. canvas_draw_icon(canvas, pos.x + (i * 17) - camera_x, pos.y - camera_y, icon);
  52. }
  53. else
  54. {
  55. // check if element is outside the world
  56. if (pos.y + (i * 17) > WORLD_HEIGHT)
  57. {
  58. break;
  59. }
  60. canvas_draw_icon(canvas, pos.x - camera_x, pos.y + (i * 17) - camera_y, icon);
  61. }
  62. }
  63. }
  64. char g_name[32];
  65. // Draw an icon at a specific position (with collision detection)
  66. void spawn_icon(GameManager *manager, Level *level, const char *icon_id, float x, float y)
  67. {
  68. snprintf(g_name, sizeof(g_name), "%s", icon_id);
  69. Entity *e = level_add_entity(level, &icon_desc);
  70. entity_pos_set(e, (Vector){x, y});
  71. GameContext *game_context = game_manager_game_context_get(manager);
  72. game_context->icon_count++;
  73. }
  74. // Draw a line of icons at a specific position (with collision detection)
  75. void spawn_icon_line(GameManager *manager, Level *level, const char *icon_id, float x, float y, uint8_t amount, bool horizontal)
  76. {
  77. for (int i = 0; i < amount; i++)
  78. {
  79. if (horizontal)
  80. {
  81. // check if element is outside the world
  82. if (x + (i * 17) > WORLD_WIDTH)
  83. {
  84. break;
  85. }
  86. spawn_icon(manager, level, icon_id, x + (i * 17), y);
  87. }
  88. else
  89. {
  90. // check if element is outside the world
  91. if (y + (i * 17) > WORLD_HEIGHT)
  92. {
  93. break;
  94. }
  95. spawn_icon(manager, level, icon_id, x, y + (i * 17));
  96. }
  97. }
  98. }
  99. static void draw_menu(GameManager *manager, Canvas *canvas)
  100. {
  101. GameContext *game_context = game_manager_game_context_get(manager);
  102. // draw background rectangle
  103. canvas_draw_icon(
  104. canvas,
  105. 0,
  106. 0,
  107. &I_icon_menu_128x64px);
  108. // draw menu options
  109. switch (game_context->menu_screen)
  110. {
  111. case GAME_MENU_INFO:
  112. // draw info
  113. // first option is highlighted
  114. char health[32];
  115. char xp[32];
  116. char level[32];
  117. char strength[32];
  118. snprintf(level, sizeof(level), "Level : %ld", game_context->player_context->level);
  119. snprintf(health, sizeof(health), "Health : %ld", game_context->player_context->health);
  120. snprintf(xp, sizeof(xp), "XP : %ld", game_context->player_context->xp);
  121. snprintf(strength, sizeof(strength), "Strength: %ld", game_context->player_context->strength);
  122. canvas_set_font(canvas, FontPrimary);
  123. canvas_draw_str(canvas, 7, 16, game_context->player_context->username);
  124. canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
  125. canvas_draw_str(canvas, 7, 30, level);
  126. canvas_draw_str(canvas, 7, 37, health);
  127. canvas_draw_str(canvas, 7, 44, xp);
  128. canvas_draw_str(canvas, 7, 51, strength);
  129. // draw a box around the selected option
  130. canvas_draw_frame(canvas, 80, 18, 36, 30);
  131. canvas_set_font(canvas, FontPrimary);
  132. canvas_draw_str(canvas, 86, 30, "Info");
  133. canvas_set_font(canvas, FontSecondary);
  134. canvas_draw_str(canvas, 86, 42, "More");
  135. break;
  136. case GAME_MENU_MORE:
  137. // draw settings
  138. switch (game_context->menu_selection)
  139. {
  140. case 0:
  141. // first option is highlighted
  142. break;
  143. case 1:
  144. // second option is highlighted
  145. break;
  146. default:
  147. break;
  148. }
  149. canvas_set_font(canvas, FontPrimary);
  150. canvas_draw_str(canvas, 7, 16, VERSION_TAG);
  151. canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
  152. canvas_draw_str_multi(canvas, 7, 25, "Developed by\nJBlanked and Derek \nJamison. Graphics\nfrom Pr3!\n\nwww.github.com/jblanked");
  153. // draw a box around the selected option
  154. canvas_draw_frame(canvas, 80, 18, 36, 30);
  155. canvas_set_font(canvas, FontSecondary);
  156. canvas_draw_str(canvas, 86, 30, "Info");
  157. canvas_set_font(canvas, FontPrimary);
  158. canvas_draw_str(canvas, 86, 42, "More");
  159. break;
  160. default:
  161. break;
  162. }
  163. }
  164. void background_render(Canvas *canvas, GameManager *manager)
  165. {
  166. if (!canvas || !manager)
  167. return;
  168. GameContext *game_context = game_manager_game_context_get(manager);
  169. // get player position
  170. Vector posi = entity_pos_get(game_context->player);
  171. // draw username over player's head
  172. draw_username(canvas, posi, game_context->player_context->username);
  173. // draw switch world icon
  174. if (game_context->is_switching_level)
  175. {
  176. canvas_draw_icon(
  177. canvas,
  178. 0,
  179. 0,
  180. &I_icon_world_change_128x64px);
  181. }
  182. // draw menu
  183. if (game_context->is_menu_open)
  184. {
  185. draw_menu(manager, canvas);
  186. }
  187. };