icon.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "game/icon.h"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. // --- Define the global variable so other modules can link to it.
  6. IconGroupContext *g_current_icon_group = NULL;
  7. // ---------------------------------------------------------------------
  8. // Icon Group Entity Implementation
  9. // ---------------------------------------------------------------------
  10. // Render callback: iterate over the group and draw each icon.
  11. static void icon_group_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  12. {
  13. UNUSED(self);
  14. UNUSED(manager);
  15. IconGroupContext *igctx = (IconGroupContext *)context;
  16. if (!igctx)
  17. {
  18. FURI_LOG_E("Game", "Icon group context is NULL in render");
  19. return;
  20. }
  21. for (int i = 0; i < igctx->count; i++)
  22. {
  23. IconSpec *spec = &igctx->icons[i];
  24. int x_pos = spec->pos.x - camera_x - (spec->size.x / 2);
  25. int y_pos = spec->pos.y - camera_y - (spec->size.y / 2);
  26. if (x_pos + spec->size.x < 0 || x_pos > SCREEN_WIDTH ||
  27. y_pos + spec->size.y < 0 || y_pos > SCREEN_HEIGHT)
  28. {
  29. continue;
  30. }
  31. canvas_draw_icon(canvas, x_pos, y_pos, spec->icon);
  32. }
  33. }
  34. // Start callback: nothing special is needed here.
  35. static void icon_group_start(Entity *self, GameManager *manager, void *context)
  36. {
  37. UNUSED(self);
  38. UNUSED(manager);
  39. UNUSED(context);
  40. // The context is assumed to be pre-filled by the world code.
  41. }
  42. // Free callback: free the icon specs array.
  43. static void icon_group_free(Entity *self, GameManager *manager, void *context)
  44. {
  45. UNUSED(self);
  46. UNUSED(manager);
  47. IconGroupContext *igctx = (IconGroupContext *)context;
  48. if (igctx && igctx->icons)
  49. {
  50. free(igctx->icons);
  51. igctx->icons = NULL;
  52. }
  53. }
  54. // Optional helper to free an icon group context allocated dynamically.
  55. void icon_group_context_free(IconGroupContext *ctx)
  56. {
  57. if (ctx)
  58. {
  59. if (ctx->icons)
  60. {
  61. free(ctx->icons);
  62. ctx->icons = NULL;
  63. }
  64. free(ctx);
  65. }
  66. }
  67. // The entity description for the icon group.
  68. // Note: we set context_size to sizeof(IconGroupContext).
  69. const EntityDescription icon_desc = {
  70. .start = icon_group_start,
  71. .stop = icon_group_free,
  72. .update = NULL,
  73. .render = icon_group_render,
  74. // We leave collision as NULL because we now handle collisions in player_update.
  75. .collision = NULL,
  76. .event = NULL,
  77. .context_size = sizeof(IconGroupContext),
  78. };