icon.c 2.1 KB

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