draw.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 a line of icons (16 width)
  20. void draw_icon_line(Canvas *canvas, Vector pos, int amount, bool horizontal, const Icon *icon)
  21. {
  22. for (int i = 0; i < amount; i++)
  23. {
  24. if (horizontal)
  25. {
  26. // check if element is outside the world
  27. if (pos.x + (i * 17) > WORLD_WIDTH)
  28. {
  29. break;
  30. }
  31. canvas_draw_icon(canvas, pos.x + (i * 17) - camera_x, pos.y - camera_y, icon);
  32. }
  33. else
  34. {
  35. // check if element is outside the world
  36. if (pos.y + (i * 17) > WORLD_HEIGHT)
  37. {
  38. break;
  39. }
  40. canvas_draw_icon(canvas, pos.x - camera_x, pos.y + (i * 17) - camera_y, icon);
  41. }
  42. }
  43. }
  44. // Draw a half section of icons (16 width)
  45. void draw_icon_half_world(Canvas *canvas, bool right, const Icon *icon)
  46. {
  47. for (int i = 0; i < 10; i++)
  48. {
  49. if (right)
  50. {
  51. draw_icon_line(canvas, (Vector){WORLD_WIDTH / 2 + 6, i * 19 + 2}, 11, true, icon);
  52. }
  53. else
  54. {
  55. draw_icon_line(canvas, (Vector){0, i * 19 + 2}, 11, true, icon);
  56. }
  57. }
  58. }
  59. // Draw an icon at a specific position (with collision detection)
  60. void spawn_icon(Level *level, const Icon *icon, float x, float y, uint8_t width, uint8_t height)
  61. {
  62. Entity *e = level_add_entity(level, &icon_desc);
  63. IconContext *icon_ctx = entity_context_get(e);
  64. icon_ctx->icon = icon;
  65. icon_ctx->width = width;
  66. icon_ctx->height = height;
  67. // Set the entity position to the center of the icon
  68. entity_pos_set(e, (Vector){x + (width / 2), y + (height / 2)});
  69. }
  70. // Draw a line of icons at a specific position (with collision detection)
  71. void spawn_icon_line(Level *level, const Icon *icon, float x, float y, uint8_t width, uint8_t height, uint8_t amount, bool horizontal)
  72. {
  73. for (int i = 0; i < amount; i++)
  74. {
  75. if (horizontal)
  76. {
  77. // check if element is outside the world
  78. if (x + (i * 17) > WORLD_WIDTH)
  79. {
  80. break;
  81. }
  82. spawn_icon(level, icon, x + (i * 17), y, width, height);
  83. }
  84. else
  85. {
  86. // check if element is outside the world
  87. if (y + (i * 17) > WORLD_HEIGHT)
  88. {
  89. break;
  90. }
  91. spawn_icon(level, icon, x, y + (i * 17), width, height);
  92. }
  93. }
  94. }