card.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 draw_card_at_colored(
  58. int8_t pos_x,
  59. int8_t pos_y,
  60. uint8_t pip,
  61. uint8_t character,
  62. bool inverted,
  63. Canvas* const canvas);
  64. /**
  65. * Draws 'count' cards at the bottom right corner
  66. *
  67. * @param cards List of cards
  68. * @param count Count of cards
  69. * @param canvas Pointer to Flipper's canvas object
  70. */
  71. void draw_deck(const Card* cards, uint8_t count, Canvas* const canvas);
  72. /**
  73. * Draws card back at a given coordinate (top-left corner)
  74. *
  75. * @param pos_x X coordinate
  76. * @param pos_y Y coordinate
  77. * @param canvas Pointer to Flipper's canvas object
  78. */
  79. void draw_card_back_at(int8_t pos_x, int8_t pos_y, Canvas* const canvas);
  80. /**
  81. * Generates the deck
  82. *
  83. * @param deck_ptr Pointer to the deck
  84. * @param deck_count Number of decks
  85. */
  86. void generate_deck(Deck* deck_ptr, uint8_t deck_count);
  87. /**
  88. * Shuffles the deck
  89. *
  90. * @param deck_ptr Pointer to the deck
  91. */
  92. void shuffle_deck(Deck* deck_ptr);
  93. /**
  94. * Calculates the hand count for blackjack
  95. *
  96. * @param cards List of cards
  97. * @param count Count of cards
  98. * @return Hand value
  99. */
  100. uint8_t hand_count(const Card* cards, uint8_t count);
  101. /**
  102. * Draws card animation
  103. *
  104. * @param animatingCard Card to animate
  105. * @param from Starting position
  106. * @param control Quadratic lerp control point
  107. * @param to End point
  108. * @param t Current time (0-1)
  109. * @param extra_margin Use extra margin at the end (arrives 0.2 unit before the end so it can stay there a bit)
  110. * @param canvas Pointer to Flipper's canvas object
  111. */
  112. void draw_card_animation(
  113. Card animatingCard,
  114. Vector from,
  115. Vector control,
  116. Vector to,
  117. float t,
  118. bool extra_margin,
  119. Canvas* const canvas);
  120. /**
  121. * Init hand pointer
  122. * @param hand_ptr Pointer to hand
  123. * @param count Number of cards we want to store
  124. */
  125. void init_hand(Hand* hand_ptr, uint8_t count);
  126. /**
  127. * Free hand resources
  128. * @param hand_ptr Pointer to hand
  129. */
  130. void free_hand(Hand* hand_ptr);
  131. /**
  132. * Add card to hand
  133. * @param hand_ptr Pointer to hand
  134. * @param card Card to add
  135. */
  136. void add_to_hand(Hand* hand_ptr, Card card);
  137. /**
  138. * Draw card placement position at coordinate
  139. * @param pos_x X coordinate
  140. * @param pos_y Y coordinate
  141. * @param highlighted Apply highlight effect
  142. * @param canvas Canvas object
  143. */
  144. void draw_card_space(int16_t pos_x, int16_t pos_y, bool highlighted, Canvas* const canvas);
  145. /**
  146. * Draws a column of card, displaying the last [max_cards] cards on the list
  147. * @param hand Hand object
  148. * @param pos_x X coordinate to draw
  149. * @param pos_y Y coordinate to draw
  150. * @param highlight Index to highlight, negative means no highlight
  151. * @param canvas Canvas object
  152. */
  153. void draw_hand_column(
  154. Hand hand,
  155. int16_t pos_x,
  156. int16_t pos_y,
  157. int8_t highlight,
  158. Canvas* const canvas);
  159. /**
  160. * 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)
  161. * @param index Index to remove
  162. * @param deck Deck reference
  163. * @return The removed card
  164. */
  165. Card remove_from_deck(uint16_t index, Deck* deck);
  166. int first_non_flipped_card(Hand hand);
  167. void extract_hand_region(Hand* hand, Hand* to, uint8_t start_index);
  168. void add_hand_region(Hand* to, Hand* from);