draw.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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, 32, 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. // 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. // Draw a half section of icons (16 width)
  65. void draw_icon_half_world(Canvas *canvas, bool right, const Icon *icon)
  66. {
  67. for (int i = 0; i < 10; i++)
  68. {
  69. if (right)
  70. {
  71. draw_icon_line(canvas, (Vector){WORLD_WIDTH / 2 + 6, i * 19 + 2}, 11, true, icon);
  72. }
  73. else
  74. {
  75. draw_icon_line(canvas, (Vector){0, i * 19 + 2}, 11, true, icon);
  76. }
  77. }
  78. }
  79. // Draw an icon at a specific position (with collision detection)
  80. void spawn_icon(Level *level, const Icon *icon, float x, float y, uint8_t width, uint8_t height)
  81. {
  82. Entity *e = level_add_entity(level, &icon_desc);
  83. IconContext *icon_ctx = entity_context_get(e);
  84. icon_ctx->icon = icon;
  85. icon_ctx->width = width;
  86. icon_ctx->height = height;
  87. // Set the entity position to the center of the icon
  88. entity_pos_set(e, (Vector){x + (width / 2), y + (height / 2)});
  89. }
  90. // Draw a line of icons at a specific position (with collision detection)
  91. void spawn_icon_line(Level *level, const Icon *icon, float x, float y, uint8_t width, uint8_t height, uint8_t amount, bool horizontal)
  92. {
  93. for (int i = 0; i < amount; i++)
  94. {
  95. if (horizontal)
  96. {
  97. // check if element is outside the world
  98. if (x + (i * 17) > WORLD_WIDTH)
  99. {
  100. break;
  101. }
  102. spawn_icon(level, icon, x + (i * 17), y, width, height);
  103. }
  104. else
  105. {
  106. // check if element is outside the world
  107. if (y + (i * 17) > WORLD_HEIGHT)
  108. {
  109. break;
  110. }
  111. spawn_icon(level, icon, x, y + (i * 17), width, height);
  112. }
  113. }
  114. }