From a682121423f91f43a5d3b2147ad236a4cdb8890c Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Thu, 21 Mar 2019 15:18:10 -0400 Subject: Refactor layout code slightly --- sol.c | 58 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/sol.c b/sol.c index f0d9616..8ab89d5 100644 --- a/sol.c +++ b/sol.c @@ -112,39 +112,43 @@ struct Item { Sint8 card; }; -struct Layout { +struct List { uint len; struct Item items[52]; }; -static void layoutClear(struct Layout *list) { - list->len = 0; -} -static void layoutPush(struct Layout *list, struct Item item) { +static void listPush(struct List *list, struct Item item) { assert(list->len < 52); list->items[list->len++] = item; } static struct { - struct Layout base; - struct Layout main; - struct Layout drag; + struct List base; + struct List main; + struct List drag; struct Item dragItem; -} layout; + uint cardBack; +} layout = { + .cardBack = Cards_Back1, +}; -static uint cardBack = Cards_Back1; +static void layoutClear(void) { + layout.base.len = 0; + layout.main.len = 0; + layout.drag.len = 0; +} -static void layoutCard(struct Layout **list, SDL_Rect *rect, Sint8 card) { +static void layoutCard(struct List **list, SDL_Rect *rect, Sint8 card) { if (card == layout.dragItem.card) { *list = &layout.drag; *rect = layout.dragItem.rect; } - struct Item item = { *rect, (card > 0 ? card : cardBack), card }; - layoutPush(*list, item); + struct Item item = { *rect, (card > 0 ? card : layout.cardBack), card }; + listPush(*list, item); } static void layoutStack(SDL_Rect *rect, const struct Stack *stack, uint depth) { - struct Layout *list = &layout.main; + struct List *list = &layout.main; for (uint i = 0; i < stack->len; ++i) { layoutCard(&list, rect, stack->cards[i]); if (i > 0 && i % (depth / 3) == 0) { @@ -157,14 +161,14 @@ static void layoutStack(SDL_Rect *rect, const struct Stack *stack, uint depth) { static void layoutStock(void) { SDL_Rect rect = { StockX, StockY, Cards_Width, Cards_Height }; struct Item item = { rect, Cards_O, 0 }; - layoutPush(&layout.base, item); + listPush(&layout.base, item); layoutStack(&rect, &stacks[Stock], 24); } static void layoutWaste(void) { SDL_Rect rect = { WasteX, WasteY, Cards_Width, Cards_Height }; layoutStack(&rect, &stacks[Waste1], 24); - struct Layout *list = &layout.main; + struct List *list = &layout.main; for (uint i = 0; i < stacks[Waste3].len; ++i) { layoutCard(&list, &rect, stacks[Waste3].cards[i]); rect.x += Waste3DeltaX; @@ -176,7 +180,7 @@ static void layoutFoundations(void) { SDL_Rect base = { FoundationX, FoundationY, Cards_Width, Cards_Height }; for (uint i = Foundation1; i <= Foundation4; ++i) { struct Item item = { base, Cards_Empty, 0 }; - layoutPush(&layout.base, item); + listPush(&layout.base, item); SDL_Rect rect = base; layoutStack(&rect, &stacks[i], 13); base.x += Cards_Width + StackMarginX; @@ -186,7 +190,7 @@ static void layoutFoundations(void) { static void layoutTableau(void) { SDL_Rect base = { TableauX, TableauY, Cards_Width, Cards_Height }; for (uint i = Tableau1; i <= Tableau7; ++i) { - struct Layout *list = &layout.main; + struct List *list = &layout.main; SDL_Rect rect = base; for (uint j = 0; j < stacks[i].len; ++j) { Sint8 card = stacks[i].cards[j]; @@ -201,7 +205,7 @@ static void layoutTableau(void) { static SDL_Renderer *render; static SDL_Texture *textures[Cards_Count]; -static void renderLayout(const struct Layout *list) { +static void renderList(const struct List *list) { for (uint i = 0; i < list->len; ++i) { SDL_RenderCopy( render, @@ -278,7 +282,10 @@ int main(void) { stackFlipTo(&stacks[Tableau7], &stacks[Stock], 1); } break; case SDLK_b: { - cardBack = Cards_Back1 + (cardBack - Cards_Back1 + 1) % 12; + layout.cardBack++; + layout.cardBack -= Cards_Back1; + layout.cardBack %= 12; + layout.cardBack += Cards_Back1; } } } @@ -302,10 +309,7 @@ int main(void) { layout.dragItem.rect.y += event.motion.yrel; } - layoutClear(&layout.base); - layoutClear(&layout.main); - layoutClear(&layout.drag); - + layoutClear(); layoutStock(); layoutWaste(); layoutFoundations(); @@ -314,9 +318,9 @@ int main(void) { SDL_SetRenderDrawColor(render, 0x00, 0xAA, 0x55, 0xFF); SDL_RenderClear(render); - renderLayout(&layout.base); - renderLayout(&layout.main); - renderLayout(&layout.drag); + renderList(&layout.base); + renderList(&layout.main); + renderList(&layout.drag); SDL_RenderPresent(render); } -- cgit 1.4.1