draw.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // Draw the user stats (health, xp, and level)
  21. void draw_user_stats(Canvas *canvas, Vector pos, GameManager *manager)
  22. {
  23. GameContext *game_context = game_manager_game_context_get(manager);
  24. PlayerContext *player = game_context->player_context;
  25. // first draw a black rectangle to make the text more readable
  26. canvas_invert_color(canvas);
  27. canvas_draw_box(canvas, pos.x - 1, pos.y - 7, 32, 21);
  28. canvas_invert_color(canvas);
  29. char health[32];
  30. char xp[32];
  31. char level[32];
  32. snprintf(health, sizeof(health), "HP : %ld", player->health);
  33. snprintf(xp, sizeof(xp), "XP : %ld", player->xp);
  34. snprintf(level, sizeof(level), "LVL: %ld", player->level);
  35. canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
  36. canvas_draw_str(canvas, pos.x, pos.y, health);
  37. canvas_draw_str(canvas, pos.x, pos.y + 7, xp);
  38. canvas_draw_str(canvas, pos.x, pos.y + 14, level);
  39. }
  40. // Draw a line of icons (16 width)
  41. void draw_icon_line(Canvas *canvas, Vector pos, int amount, bool horizontal, const Icon *icon)
  42. {
  43. for (int i = 0; i < amount; i++)
  44. {
  45. if (horizontal)
  46. {
  47. // check if element is outside the world
  48. if (pos.x + (i * 17) > WORLD_WIDTH)
  49. {
  50. break;
  51. }
  52. canvas_draw_icon(canvas, pos.x + (i * 17) - camera_x, pos.y - camera_y, icon);
  53. }
  54. else
  55. {
  56. // check if element is outside the world
  57. if (pos.y + (i * 17) > WORLD_HEIGHT)
  58. {
  59. break;
  60. }
  61. canvas_draw_icon(canvas, pos.x - camera_x, pos.y + (i * 17) - camera_y, icon);
  62. }
  63. }
  64. }
  65. // Draw a half section of icons (16 width)
  66. void draw_icon_half_world(Canvas *canvas, bool right, const Icon *icon)
  67. {
  68. for (int i = 0; i < 10; i++)
  69. {
  70. if (right)
  71. {
  72. draw_icon_line(canvas, (Vector){WORLD_WIDTH / 2 + 6, i * 19 + 2}, 11, true, icon);
  73. }
  74. else
  75. {
  76. draw_icon_line(canvas, (Vector){0, i * 19 + 2}, 11, true, icon);
  77. }
  78. }
  79. }
  80. // Draw an icon at a specific position (with collision detection)
  81. void spawn_icon(Level *level, const Icon *icon, float x, float y, uint8_t width, uint8_t height)
  82. {
  83. Entity *e = level_add_entity(level, &icon_desc);
  84. IconContext *icon_ctx = entity_context_get(e);
  85. icon_ctx->icon = icon;
  86. icon_ctx->width = width;
  87. icon_ctx->height = height;
  88. // Set the entity position to the center of the icon
  89. entity_pos_set(e, (Vector){x + (width / 2), y + (height / 2)});
  90. }
  91. // Draw a line of icons at a specific position (with collision detection)
  92. void spawn_icon_line(Level *level, const Icon *icon, float x, float y, uint8_t width, uint8_t height, uint8_t amount, bool horizontal)
  93. {
  94. for (int i = 0; i < amount; i++)
  95. {
  96. if (horizontal)
  97. {
  98. // check if element is outside the world
  99. if (x + (i * 17) > WORLD_WIDTH)
  100. {
  101. break;
  102. }
  103. spawn_icon(level, icon, x + (i * 17), y, width, height);
  104. }
  105. else
  106. {
  107. // check if element is outside the world
  108. if (y + (i * 17) > WORLD_HEIGHT)
  109. {
  110. break;
  111. }
  112. spawn_icon(level, icon, x, y + (i * 17), width, height);
  113. }
  114. }
  115. }