card.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #pragma once
  2. #include <gui/gui.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include "dml.h"
  6. #define CARD_HEIGHT 23
  7. #define CARD_HALF_HEIGHT 11
  8. #define CARD_WIDTH 17
  9. #define CARD_HALF_WIDTH 8
  10. //region types
  11. typedef struct {
  12. uint8_t pip; //Pip index 0:spades, 1:hearths, 2:diamonds, 3:clubs
  13. uint8_t character; //Card letter [0-12], 0 means 2, 12 is Ace
  14. bool disabled;
  15. bool flipped;
  16. } Card;
  17. typedef struct {
  18. uint8_t deck_count; //Number of decks used
  19. Card *cards; //Cards in the deck
  20. int card_count;
  21. int index; //Card index (to know where we at in the deck)
  22. } Deck;
  23. typedef struct {
  24. Card *cards; //Cards in the deck
  25. uint8_t index; //Current index
  26. uint8_t max; //How many cards we want to store
  27. } Hand;
  28. //endregion
  29. void set_card_graphics(const Icon *graphics);
  30. /**
  31. * Gets card coordinates at the index (range: 0-20).
  32. *
  33. * @param index Index to check 0-20
  34. * @return Position of the card
  35. */
  36. Vector card_pos_at_index(uint8_t index);
  37. /**
  38. * Draws card at a given coordinate (top-left corner)
  39. *
  40. * @param pos_x X position
  41. * @param pos_y Y position
  42. * @param pip Pip index 0:spades, 1:hearths, 2:diamonds, 3:clubs
  43. * @param character Letter [0-12] 0 is 2, 12 is A
  44. * @param canvas Pointer to Flipper's canvas object
  45. */
  46. void draw_card_at(int8_t pos_x, int8_t pos_y, uint8_t pip, uint8_t character, Canvas *const canvas);
  47. /**
  48. * Draws card at a given coordinate (top-left corner)
  49. *
  50. * @param pos_x X position
  51. * @param pos_y Y position
  52. * @param pip Pip index 0:spades, 1:hearths, 2:diamonds, 3:clubs
  53. * @param character Letter [0-12] 0 is 2, 12 is A
  54. * @param inverted Invert colors
  55. * @param canvas Pointer to Flipper's canvas object
  56. */
  57. void
  58. draw_card_at_colored(int8_t pos_x, int8_t pos_y, uint8_t pip, uint8_t character, bool inverted, Canvas *const canvas);
  59. /**
  60. * Draws 'count' cards at the bottom right corner
  61. *
  62. * @param cards List of cards
  63. * @param count Count of cards
  64. * @param canvas Pointer to Flipper's canvas object
  65. */
  66. void draw_deck(const Card *cards, uint8_t count, Canvas *const canvas);
  67. /**
  68. * Draws card back at a given coordinate (top-left corner)
  69. *
  70. * @param pos_x X coordinate
  71. * @param pos_y Y coordinate
  72. * @param canvas Pointer to Flipper's canvas object
  73. */
  74. void draw_card_back_at(int8_t pos_x, int8_t pos_y, Canvas *const canvas);
  75. /**
  76. * Generates the deck
  77. *
  78. * @param deck_ptr Pointer to the deck
  79. * @param deck_count Number of decks
  80. */
  81. void generate_deck(Deck *deck_ptr, uint8_t deck_count);
  82. /**
  83. * Shuffles the deck
  84. *
  85. * @param deck_ptr Pointer to the deck
  86. */
  87. void shuffle_deck(Deck *deck_ptr);
  88. /**
  89. * Calculates the hand count for blackjack
  90. *
  91. * @param cards List of cards
  92. * @param count Count of cards
  93. * @return Hand value
  94. */
  95. uint8_t hand_count(const Card *cards, uint8_t count);
  96. /**
  97. * Draws card animation
  98. *
  99. * @param animatingCard Card to animate
  100. * @param from Starting position
  101. * @param control Quadratic lerp control point
  102. * @param to End point
  103. * @param t Current time (0-1)
  104. * @param extra_margin Use extra margin at the end (arrives 0.2 unit before the end so it can stay there a bit)
  105. * @param canvas Pointer to Flipper's canvas object
  106. */
  107. void draw_card_animation(Card animatingCard, Vector from, Vector control, Vector to, float t, bool extra_margin,
  108. Canvas *const canvas);
  109. /**
  110. * Init hand pointer
  111. * @param hand_ptr Pointer to hand
  112. * @param count Number of cards we want to store
  113. */
  114. void init_hand(Hand *hand_ptr, uint8_t count);
  115. /**
  116. * Free hand resources
  117. * @param hand_ptr Pointer to hand
  118. */
  119. void free_hand(Hand *hand_ptr);
  120. /**
  121. * Add card to hand
  122. * @param hand_ptr Pointer to hand
  123. * @param card Card to add
  124. */
  125. void add_to_hand(Hand *hand_ptr, Card card);
  126. /**
  127. * Draw card placement position at coordinate
  128. * @param pos_x X coordinate
  129. * @param pos_y Y coordinate
  130. * @param highlighted Apply highlight effect
  131. * @param canvas Canvas object
  132. */
  133. void draw_card_space(int16_t pos_x, int16_t pos_y, bool highlighted, Canvas *const canvas);
  134. /**
  135. * Draws a column of card, displaying the last 4 cards on the list
  136. * @param hand Hand object
  137. * @param pos_x X coordinate to draw
  138. * @param pos_y Y coordinate to draw
  139. * @param highlight Index to highlight, negative means no highlight
  140. * @param canvas Canvas object
  141. */
  142. void
  143. draw_hand_column(Hand hand, int16_t pos_x, int16_t pos_y, int8_t highlight, Canvas *const canvas);
  144. /**
  145. * Removes a card from the deck (Be aware, if you remove the first item, the deck index will be at -1 so you have to handle that)
  146. * @param index Index to remove
  147. * @param deck Deck reference
  148. * @return The removed card
  149. */
  150. Card remove_from_deck(uint16_t index, Deck *deck);
  151. int first_non_flipped_card(Hand hand);
  152. void extract_hand_region(Hand *hand, Hand *to, uint8_t start_index);
  153. void add_hand_region(Hand *to, Hand *from);