| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- #include "card.h"
- #include "dml.h"
- #include "ui.h"
- #define CARD_DRAW_X_START 108
- #define CARD_DRAW_Y_START 38
- #define CARD_DRAW_X_SPACE 10
- #define CARD_DRAW_Y_SPACE 8
- #define CARD_DRAW_X_OFFSET 4
- #define CARD_DRAW_FIRST_ROW_LENGTH 7
- uint8_t pips[4][3] = {
- {21, 10, 7}, //spades
- {7, 10, 7}, //hearts
- {0, 10, 7}, //diamonds
- {14, 10, 7}, //clubs
- };
- uint8_t letters[13][3] = {
- {0, 0, 5},
- {5, 0, 5},
- {10, 0, 5},
- {15, 0, 5},
- {20, 0, 5},
- {25, 0, 5},
- {30, 0, 5},
- {0, 5, 5},
- {5, 5, 5},
- {10, 5, 5},
- {15, 5, 5},
- {20, 5, 5},
- {25, 5, 5},
- };
- //region Player card positions
- uint8_t playerCardPositions[22][4] = {
- //first row
- {108, 38},
- {98, 38},
- {88, 38},
- {78, 38},
- {68, 38},
- {58, 38},
- {48, 38},
- {38, 38},
- //second row
- {104, 26},
- {94, 26},
- {84, 26},
- {74, 26},
- {64, 26},
- {54, 26},
- {44, 26},
- //third row
- {99, 14},
- {89, 14},
- {79, 14},
- {69, 14},
- {59, 14},
- {49, 14},
- };
- //endregion
- Icon* card_graphics = NULL;
- void set_card_graphics(const Icon* graphics) {
- card_graphics = (Icon*)graphics;
- }
- void draw_card_at_colored(
- int8_t pos_x,
- int8_t pos_y,
- uint8_t pip,
- uint8_t character,
- bool inverted,
- Canvas* const canvas) {
- DrawMode primary = inverted ? Black : White;
- DrawMode secondary = inverted ? White : Black;
- draw_rounded_box(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT, primary);
- draw_rounded_box_frame(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT, Black);
- uint8_t* drawInfo = pips[pip];
- uint8_t px = drawInfo[0], py = drawInfo[1], s = drawInfo[2];
- uint8_t left = pos_x + 2;
- uint8_t right = (pos_x + CARD_WIDTH - s - 2);
- uint8_t top = pos_y + 2;
- uint8_t bottom = (pos_y + CARD_HEIGHT - s - 2);
- draw_icon_clip(canvas, card_graphics, right, top, px, py, s, s, secondary);
- draw_icon_clip_flipped(canvas, card_graphics, left, bottom, px, py, s, s, secondary);
- drawInfo = letters[character];
- px = drawInfo[0], py = drawInfo[1], s = drawInfo[2];
- left = pos_x + 2;
- right = (pos_x + CARD_WIDTH - s - 2);
- top = pos_y + 2;
- bottom = (pos_y + CARD_HEIGHT - s - 2);
- draw_icon_clip(canvas, card_graphics, left, top + 1, px, py, s, s, secondary);
- draw_icon_clip_flipped(canvas, card_graphics, right, bottom - 1, px, py, s, s, secondary);
- }
- void draw_card_at(int8_t pos_x, int8_t pos_y, uint8_t pip, uint8_t character, Canvas* const canvas) {
- draw_card_at_colored(pos_x, pos_y, pip, character, false, canvas);
- }
- void draw_deck(const Card* cards, uint8_t count, Canvas* const canvas) {
- for(int i = count - 1; i >= 0; i--) {
- draw_card_at(
- playerCardPositions[i][0],
- playerCardPositions[i][1],
- cards[i].pip,
- cards[i].character,
- canvas);
- }
- }
- Vector card_pos_at_index(uint8_t index) {
- return (Vector){playerCardPositions[index][0], playerCardPositions[index][1]};
- }
- void draw_card_back_at(int8_t pos_x, int8_t pos_y, Canvas* const canvas) {
- draw_rounded_box(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT, White);
- draw_rounded_box_frame(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT, Black);
- draw_icon_clip(canvas, card_graphics, pos_x + 1, pos_y + 1, 35, 0, 15, 21, Black);
- }
- void generate_deck(Deck* deck_ptr, uint8_t deck_count) {
- uint16_t counter = 0;
- if(deck_ptr->cards != NULL) {
- free(deck_ptr->cards);
- }
- deck_ptr->deck_count = deck_count;
- deck_ptr->card_count = deck_count * 52;
- deck_ptr->cards = malloc(sizeof(Card) * deck_ptr->card_count);
- for(uint8_t deck = 0; deck < deck_count; deck++) {
- for(uint8_t pip = 0; pip < 4; pip++) {
- for(uint8_t label = 0; label < 13; label++) {
- deck_ptr->cards[counter] = (Card){pip, label, false, false};
- counter++;
- }
- }
- }
- }
- void shuffle_deck(Deck* deck_ptr) {
- srand(DWT->CYCCNT);
- deck_ptr->index = 0;
- int max = deck_ptr->deck_count * 52;
- for(int i = 0; i < max; i++) {
- int r = i + (rand() % (max - i));
- Card tmp = deck_ptr->cards[i];
- deck_ptr->cards[i] = deck_ptr->cards[r];
- deck_ptr->cards[r] = tmp;
- }
- }
- uint8_t hand_count(const Card* cards, uint8_t count) {
- uint8_t aceCount = 0;
- uint8_t score = 0;
- for(uint8_t i = 0; i < count; i++) {
- if(cards[i].character == 12)
- aceCount++;
- else {
- if(cards[i].character > 8)
- score += 10;
- else
- score += cards[i].character + 2;
- }
- }
- for(uint8_t i = 0; i < aceCount; i++) {
- if((score + 11) <= 21)
- score += 11;
- else
- score++;
- }
- return score;
- }
- void draw_card_animation(
- Card animatingCard,
- Vector from,
- Vector control,
- Vector to,
- float t,
- bool extra_margin,
- Canvas* const canvas) {
- float time = t;
- if(extra_margin) {
- time += 0.2;
- }
- Vector currentPos = quadratic_2d(from, control, to, time);
- if(t > 1) {
- draw_card_at(
- currentPos.x, currentPos.y, animatingCard.pip, animatingCard.character, canvas);
- } else {
- if(t < 0.5)
- draw_card_back_at(currentPos.x, currentPos.y, canvas);
- else
- draw_card_at(
- currentPos.x, currentPos.y, animatingCard.pip, animatingCard.character, canvas);
- }
- }
- void init_hand(Hand* hand_ptr, uint8_t count) {
- hand_ptr->cards = malloc(sizeof(Card) * count);
- hand_ptr->index = 0;
- hand_ptr->max = count;
- }
- void free_hand(Hand* hand_ptr) {
- FURI_LOG_D("CARD", "Freeing hand");
- free(hand_ptr->cards);
- }
- void add_to_hand(Hand* hand_ptr, Card card) {
- FURI_LOG_D("CARD", "Adding to hand");
- if(hand_ptr->index < hand_ptr->max) {
- hand_ptr->cards[hand_ptr->index] = card;
- hand_ptr->index++;
- }
- }
- void draw_card_space(int16_t pos_x, int16_t pos_y, bool highlighted, Canvas* const canvas) {
- if(highlighted) {
- draw_rounded_box_frame(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT, Black);
- draw_rounded_box_frame(
- canvas, pos_x + 2, pos_y + 2, CARD_WIDTH - 4, CARD_HEIGHT - 4, White);
- } else {
- draw_rounded_box(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT, Black);
- draw_rounded_box_frame(
- canvas, pos_x + 2, pos_y + 2, CARD_WIDTH - 4, CARD_HEIGHT - 4, White);
- }
- }
- int first_non_flipped_card(Hand hand) {
- for(int i = 0; i < hand.index; i++) {
- if(!hand.cards[i].flipped) {
- return i;
- }
- }
- return hand.index;
- }
- void draw_hand_column(
- Hand hand,
- int16_t pos_x,
- int16_t pos_y,
- int8_t highlight,
- Canvas* const canvas) {
- if(hand.index == 0) {
- draw_card_space(pos_x, pos_y, highlight > 0, canvas);
- if(highlight == 0)
- draw_rounded_box(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT, Inverse);
- return;
- }
- int loopEnd = hand.index;
- int hStart = max(loopEnd - 4, 0);
- int pos = 0;
- int first = first_non_flipped_card(hand);
- bool wastop = false;
- if(first >= 0 && first <= hStart && highlight != first) {
- if(first > 0) {
- draw_card_back_at(pos_x, pos_y + pos, canvas);
- pos += 4;
- hStart++;
- wastop = true;
- }
- draw_card_at_colored(
- pos_x, pos_y + pos, hand.cards[first].pip, hand.cards[first].character, false, canvas);
- pos += 8;
- hStart++;
- }
- if(hStart > highlight && highlight >= 0) {
- if(!wastop && first > 0) {
- draw_card_back_at(pos_x, pos_y + pos, canvas);
- pos += 4;
- hStart++;
- }
- draw_card_at_colored(
- pos_x,
- pos_y + pos,
- hand.cards[highlight].pip,
- hand.cards[highlight].character,
- true,
- canvas);
- pos += 8;
- hStart++;
- }
- for(int i = hStart; i < loopEnd; i++, pos += 4) {
- if(hand.cards[i].flipped) {
- draw_card_back_at(pos_x, pos_y + pos, canvas);
- if(i == highlight)
- draw_rounded_box(
- canvas, pos_x + 1, pos_y + pos + 1, CARD_WIDTH - 2, CARD_HEIGHT - 2, Inverse);
- } else {
- draw_card_at_colored(
- pos_x,
- pos_y + pos,
- hand.cards[i].pip,
- hand.cards[i].character,
- (i == highlight),
- canvas);
- if(i == highlight || i == first) pos += 4;
- }
- }
- }
- Card remove_from_deck(uint16_t index, Deck* deck) {
- FURI_LOG_D("CARD", "Removing from deck");
- Card result = {0, 0, true, false};
- if(deck->card_count > 0) {
- deck->card_count--;
- for(int i = 0, curr_index = 0; i <= deck->card_count; i++) {
- if(i != index) {
- deck->cards[curr_index] = deck->cards[i];
- curr_index++;
- } else {
- result = deck->cards[i];
- }
- }
- if(deck->index >= 0) {
- deck->index--;
- }
- }
- return result;
- }
- void extract_hand_region(Hand* hand, Hand* to, uint8_t start_index) {
- FURI_LOG_D("CARD", "Extracting hand region");
- if(start_index >= hand->index) return;
- for(uint8_t i = start_index; i < hand->index; i++) {
- add_to_hand(to, hand->cards[i]);
- }
- hand->index = start_index;
- }
- void add_hand_region(Hand* to, Hand* from) {
- FURI_LOG_D("CARD", "Adding hand region");
- if((to->index + from->index) <= to->max) {
- for(int i = 0; i < from->index; i++) {
- add_to_hand(to, from->cards[i]);
- }
- }
- }
|