draw.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_temp_spawn_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_temp_spawn_name, sizeof(g_temp_spawn_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. }