draw.c 7.5 KB

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