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 > 2019-05-27Break nicks with ZWNJJune McEnroe This should prevent bad wrapping. 2019-05-26Add DawnJune McEnroe 2019-05-20Declare vasprintf(3) for GNUJune McEnroe who the fuck is scraeming "#define _GNU_SOURCE" at my house. show yourself, coward. i will never #define _GNU_SOURCE 2019-05-20Fix comparison warning in ttpreJune McEnroe 2019-05-20Add AuthorityJune McEnroe 2019-05-19Specify precedence of unary versions of operatorsJune McEnroe 2019-05-18Add compound assignment operators to orderJune McEnroe 2019-05-15Support simple assignment in orderJune McEnroe 2019-05-15Implement sizeof in orderJune McEnroe 2019-05-15Add orderJune McEnroe 2019-05-12Add T suffix in bitJune McEnroe 2019-05-10Highlight yacc and lex files as CJune McEnroe Their %-prefixed directives should probably be highlighted Macro. 2019-05-10Use val instead of suboptargJune McEnroe suboptarg doesn't exist in GNU. Hopefully BSD getsubopt also sets val on failure? 2019-05-09Add Parable of the SowerJune McEnroe 2019-05-07Add bit without buildJune McEnroe Need to do some stuff in the Makefile for lex and yacc and generating HTML pages for it. 2019-05-04Fix MANDIR typoJune McEnroe 2019-05-04Move relay to binJune McEnroe