draw.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. // Draw a half section of icons (16 width)
  76. void draw_icon_half_world(Canvas *canvas, bool right, const Icon *icon)
  77. {
  78. for (int i = 0; i < 10; i++)
  79. {
  80. if (right)
  81. {
  82. draw_icon_line(canvas, (Vector){WORLD_WIDTH / 2 + 6, i * 19 + 2}, 11, true, icon);
  83. }
  84. else
  85. {
  86. draw_icon_line(canvas, (Vector){0, i * 19 + 2}, 11, true, icon);
  87. }
  88. }
  89. }
  90. char g_temp_spawn_name[32];
  91. // Draw an icon at a specific position (with collision detection)
  92. void spawn_icon(Level *level, const char *icon_id, float x, float y)
  93. {
  94. snprintf(g_temp_spawn_name, sizeof(g_temp_spawn_name), "%s", icon_id);
  95. Entity *e = level_add_entity(level, &icon_desc);
  96. entity_pos_set(e, (Vector){x, y});
  97. }
  98. // Draw a line of icons at a specific position (with collision detection)
  99. void spawn_icon_line(Level *level, const char *icon_id, float x, float y, uint8_t amount, bool horizontal)
  100. {
  101. for (int i = 0; i < amount; i++)
  102. {
  103. if (horizontal)
  104. {
  105. // check if element is outside the world
  106. if (x + (i * 17) > WORLD_WIDTH)
  107. {
  108. break;
  109. }
  110. spawn_icon(level, icon_id, x + (i * 17), y);
  111. }
  112. else
  113. {
  114. // check if element is outside the world
  115. if (y + (i * 17) > WORLD_HEIGHT)
  116. {
  117. break;
  118. }
  119. spawn_icon(level, icon_id, x, y + (i * 17));
  120. }
  121. }
  122. }