draw.c 6.4 KB

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