From 7b4847e165958d6f40054789f18dfc6d22076f6b Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Sat, 1 Feb 2025 07:51:53 -0500 Subject: [PATCH] Select cards --- card.c | 7 ++++++- card.h | 1 + main.c | 12 +++++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/card.c b/card.c index bc3c3ef..1d0efc7 100644 --- a/card.c +++ b/card.c @@ -1,7 +1,7 @@ #include "card.h" void draw_card(Card *c) { - DrawRectangleV(c->position, card_size, RED); + DrawRectangleV(c->position, card_size, c->selected ? RED : BLACK); DrawRectangle(c->position.x + CARD_BORDER, c->position.y + CARD_BORDER, card_size.x - (CARD_BORDER * 2), card_size.y - (CARD_BORDER * 2) , WHITE); DrawText(month_english_abbr[c->month], c->position.x + CARD_BORDER + 2, c->position.y + CARD_BORDER + 2, 14, BLACK); switch (c->type) { @@ -23,3 +23,8 @@ void draw_card(Card *c) { break; } } + +bool point_within_card(Card *c, Vector2 point) { + return point.x > c->position.x && point.x < c->position.x + card_size.x && + point.y > c->position.y && point.y < c->position.y + card_size.y; +} diff --git a/card.h b/card.h index d4266b5..4a59314 100644 --- a/card.h +++ b/card.h @@ -48,6 +48,7 @@ struct Card { RibbonType ribbon_type; Month month; Vector2 position; + bool selected; }; struct Hand { diff --git a/main.c b/main.c index 750ca6b..8a11e2d 100644 --- a/main.c +++ b/main.c @@ -65,7 +65,7 @@ int main(int argc, char** argv) { case 41: t = ANIMAL; break; } - cards[i] = (Card) { i, t, rt, month, (Vector2) { month * 65, (i % 4) * 110 } }; + cards[i] = (Card) { i, t, rt, month, (Vector2) { month * 65, (i % 4) * 110 }, false }; } float delta; @@ -74,6 +74,16 @@ int main(int argc, char** argv) { while (!WindowShouldClose()) { delta = GetFrameTime(); + if (IsMouseButtonPressed(0)) { + Vector2 mouse_pos = GetMousePosition(); + for (int i = 0; i < 48; i++) { + if (point_within_card(&cards[i], mouse_pos)) { + cards[i].selected = !cards[i].selected; + break; + } + } + } + BeginDrawing(); ClearBackground(RAYWHITE); for (int i = 0; i < 48; i++) {