From 5d9c1d6de5775591f94ffed8a26a46428d79df97 Mon Sep 17 00:00:00 2001
From: Dominic <dominic@domsplace.com>
Date: Tue, 13 Jul 2021 08:36:25 -0700
Subject: [PATCH] Added Linux Support, Improved code cleanliness

---
 include/dawn/libs.h               |  2 ++
 include/dawn/poker/dealer.h       | 18 +++++++++++--
 include/dawn/poker/poker.h        | 11 --------
 include/dawn/poker/winner.h       |  2 +-
 include/dawn/util/array.h         |  2 +-
 src/platform/glfw/glwfwplatform.c |  2 +-
 src/poker/card.c                  |  2 +-
 src/poker/card.h                  |  2 +-
 src/poker/dealer.c                | 42 ++++++++++++++++++++++++++++---
 src/poker/dealer.h                | 40 ++++++++++++++++++++++++++---
 src/poker/player.c                | 17 -------------
 src/poker/player.h                | 16 ------------
 src/poker/round/deal.c            |  4 +--
 src/poker/round/deal.h            |  2 +-
 src/poker/round/flop.c            |  8 +++---
 src/poker/round/start.c           |  5 +---
 src/util/array.c                  |  6 ++---
 src/util/array.h                  |  4 +--
 18 files changed, 110 insertions(+), 75 deletions(-)

diff --git a/include/dawn/libs.h b/include/dawn/libs.h
index 6cf8baae..552810e5 100644
--- a/include/dawn/libs.h
+++ b/include/dawn/libs.h
@@ -22,4 +22,6 @@
   // Windows Fixes
   # define strtok_r strtok_s
   # define sleep(n) _sleep(n)
+#else
+  #include <unistd.h>
 #endif
\ No newline at end of file
diff --git a/include/dawn/poker/dealer.h b/include/dawn/poker/dealer.h
index 3caa5d9b..6b4ecb81 100644
--- a/include/dawn/poker/dealer.h
+++ b/include/dawn/poker/dealer.h
@@ -13,14 +13,28 @@
 #include "../display/primitive.h"
 
 /** How many cards the dealer can hold in their hand */
-#define POKER_DEALER_HAND 5
+#define POKER_DEALER_HAND_SIZE 5
+
+/** How many cards the grave can hold */
+#define POKER_DEALER_GRAVE_SIZE CARD_DECK_SIZE
 
 /** Representation of the dealer state */
 typedef struct {
+  /** Current Card Deck */
+  card_t deck[CARD_DECK_SIZE];
+  uint8_t deckSize;
+
   /** Dealer Hand */
-  card_t cards[POKER_DEALER_HAND];
+  card_t cards[POKER_DEALER_HAND_SIZE];
   uint8_t cardsFacing;
 
+  /** Card grave (where spent cards go when burned */
+  card_t grave[POKER_DEALER_GRAVE_SIZE];
+  uint8_t graveSize;
+
+  
+
+  // Rendering assets (unfinished)
   texture_t dealerTexture;
   tileset_t dealerTileset;
   primitive_t dealerPrimitive;
diff --git a/include/dawn/poker/poker.h b/include/dawn/poker/poker.h
index 97223e92..fcf0975e 100644
--- a/include/dawn/poker/poker.h
+++ b/include/dawn/poker/poker.h
@@ -35,9 +35,6 @@
 #define POKER_ROUND_BET3 0x0A
 #define POKER_ROUND_WINNER 0x0B
 
-/** How many cards the grave can hold */
-#define POKER_GRAVE_SIZE CARD_DECK_SIZE
-
 /** GUI Height fix (To keep gui scaling nicely we use a fixed height) */
 #define POKER_GUI_HEIGHT 2160
 
@@ -47,18 +44,10 @@
 #define POKER_FLOP_CARD_COUNT 3
 #define POKER_TURN_CARD_COUNT 1
 #define POKER_RIVER_CARD_COUNT 1
-
 typedef struct {
   //////////////////////////////////////////////////////////////////////////////
   // Poker Logic Variables
   //////////////////////////////////////////////////////////////////////////////
-  /** Current Card Deck */
-  card_t deck[CARD_DECK_SIZE];
-  uint8_t deckSize;
-
-  /** Card grave (where spent cards go when burned */
-  card_t grave[POKER_GRAVE_SIZE];
-  uint8_t graveSize;
 
   /** Poker betting state */
   pokerbet_t bet;
diff --git a/include/dawn/poker/winner.h b/include/dawn/poker/winner.h
index 8844e46c..dfb5cf99 100644
--- a/include/dawn/poker/winner.h
+++ b/include/dawn/poker/winner.h
@@ -11,7 +11,7 @@
 #include "dealer.h"
 
 /** Size of the FULL hand used to calculate a winning. */
-#define POKER_WINNING_FULL_SIZE POKER_PLAYER_HAND+POKER_DEALER_HAND
+#define POKER_WINNING_FULL_SIZE POKER_PLAYER_HAND+POKER_DEALER_HAND_SIZE
 
 /** How many cards make a winning set */
 #define POKER_WINNING_SET_SIZE 5
diff --git a/include/dawn/util/array.h b/include/dawn/util/array.h
index 9a8ebd48..6cd3d744 100644
--- a/include/dawn/util/array.h
+++ b/include/dawn/util/array.h
@@ -15,4 +15,4 @@
  * @param right The right element in the array.
  * @return -1 for Left priority, 1 for Right and 0 for Equal.
  */
-typedef int32_t *arraysort_t(void*, void*);
\ No newline at end of file
+typedef int32_t arraysort_t(const void*, const void*);
\ No newline at end of file
diff --git a/src/platform/glfw/glwfwplatform.c b/src/platform/glfw/glwfwplatform.c
index 101c81ac..d79e8a16 100644
--- a/src/platform/glfw/glwfwplatform.c
+++ b/src/platform/glfw/glwfwplatform.c
@@ -11,7 +11,7 @@ GLFWwindow *window = NULL;
 
 int32_t main() {
   // Attempt to init GLFW
-  if(!glfwInit()) return NULL;
+  if(!glfwInit()) return 1;
 
   // Setup window hints
   glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, false);
diff --git a/src/poker/card.c b/src/poker/card.c
index 162d07dd..021b56fc 100644
--- a/src/poker/card.c
+++ b/src/poker/card.c
@@ -27,7 +27,7 @@ void cardHandSort(card_t *cards, uint8_t length) {
   arraySort(sizeof(card_t), cards, (int32_t)length, (arraysort_t *)&_cardSorter);
 }
 
-int32_t _cardSorter(void* left, void* right) {
+int32_t _cardSorter(const void* left, const void* right) {
   card_t cardL = *((card_t *)left);
   card_t cardR = *((card_t *)right);
 
diff --git a/src/poker/card.h b/src/poker/card.h
index 7e0d08eb..e643e4bd 100644
--- a/src/poker/card.h
+++ b/src/poker/card.h
@@ -52,7 +52,7 @@ void cardDeal(card_t *source, uint8_t *sourceSize, card_t *dest,
  * @param length Length of the array of cards.
  */
 void cardHandSort(card_t *cards, uint8_t length);
-int32_t _cardSorter(void* left, void* right);
+int32_t _cardSorter(const void* left, const void* right);
 
 /**
  * Check if an array of cards contains a specific card.
diff --git a/src/poker/dealer.c b/src/poker/dealer.c
index 5b2ecd72..14ce44ef 100644
--- a/src/poker/dealer.c
+++ b/src/poker/dealer.c
@@ -6,12 +6,48 @@
  */
 #include "dealer.h"
 
-void pokerDealerDeal(poker_t *poker, uint8_t count) {
+void pokerDealerInit(pokerdealer_t *dealer) {
+  uint8_t i;
+
+  dealer->graveSize = 0;
+  dealer->cardsFacing = 0;
+  dealer->deckSize = CARD_DECK_SIZE;
+  for(i = 0; i < CARD_DECK_SIZE; i++) dealer->deck[i] = i;
+}
+
+void pokerDealerTurn(pokerdealer_t *dealer, uint8_t count) {
   uint8_t i;
   for(i = 0; i < count; i++) {
     cardDeal(
-      poker->deck, &poker->deckSize,
-      poker->dealer.cards, &poker->dealer.cardsFacing
+      dealer->deck, &dealer->deckSize,
+      dealer->cards, &dealer->cardsFacing
     );
   }
+}
+
+void pokerDealerBurn(pokerdealer_t *dealer, uint8_t count) {
+  uint8_t i;
+  for(i = 0; i < count; i++) {
+    cardDeal(
+      dealer->deck, &dealer->deckSize,
+      dealer->grave, &dealer->graveSize
+    );
+  }
+}
+
+void pokerDealerDeal(pokerdealer_t *dealer, pokerplayer_t *player) {
+  cardDeal(dealer->deck, &dealer->deckSize, player->cards, &player->cardCount);
+}
+
+void pokerDealerDealAll(poker_t *poker, uint8_t count) {
+  uint8_t x, y;
+  pokerplayer_t *player;
+
+  for(y = 0; y < count; y++) {
+    for(x = 0; x < POKER_PLAYER_COUNT; x++) {
+      player = poker->players + x;
+      if(!pokerPlayerIsAlive(player)) continue;
+      pokerDealerDeal(&poker->dealer, player);
+    }
+  }
 }
\ No newline at end of file
diff --git a/src/poker/dealer.h b/src/poker/dealer.h
index d2461c11..3b56d540 100644
--- a/src/poker/dealer.h
+++ b/src/poker/dealer.h
@@ -6,12 +6,44 @@
 #pragma once
 #include <dawn/dawn.h>
 #include "card.h"
+#include "player.h"
 
 /**
- * Deals cards to each player from the dealers' deck. Cards are dealt one to
- * each player $count times.
+ * Initializes/Resets a dealer state.
  * 
- * @param poker Poker game instance
+ * @param dealer Dealer's state to reset.
+ */
+void pokerDealerInit(pokerdealer_t *dealer);
+
+/**
+ * Turns over cards from the deck onto the table (from the deck into the dealer
+ * hand)
+ * 
+ * @param dealer Poker dealer instance.
  * @param count Count of cards to deal.
  */
-void pokerDealerDeal(poker_t *poker, uint8_t count);
\ No newline at end of file
+void pokerDealerTurn(pokerdealer_t *dealer, uint8_t count);
+
+/**
+ * Burns a set of cards off the top of the deck into the graveyard.
+ * 
+ * @param dealer Poker dealer instance.
+ * @param count Count of cards to burn.
+ */
+void pokerDealerBurn(pokerdealer_t *dealer, uint8_t count);
+
+/**
+ * Deal a card to a player.
+ * 
+ * @param dealer Poker dealer instance.
+ * @param player Poker player to deal to.
+ */
+void pokerDealerDeal(pokerdealer_t *dealer, pokerplayer_t *player);
+
+/**
+ * Deal card(s) to every active player.
+ * 
+ * @param poker Poker game instance.
+ * @param count Count of cards to deal.
+ */
+void pokerDealerDealAll(poker_t *poker, uint8_t count);
\ No newline at end of file
diff --git a/src/poker/player.c b/src/poker/player.c
index 5b6b79b5..b0c7903d 100644
--- a/src/poker/player.c
+++ b/src/poker/player.c
@@ -13,21 +13,4 @@ bool pokerPlayerIsAlive(pokerplayer_t *player) {
 
 bool pokerPlayerIsHuman(poker_t *poker, pokerplayer_t *player) {
   return (poker->players + POKER_PLAYER_HUMAN_INDEX) == player;
-}
-
-void pokerPlayerDeal(poker_t *poker, pokerplayer_t *player) {
-  cardDeal(poker->deck, &poker->deckSize, player->cards, &player->cardCount);
-}
-
-void pokerPlayerDealAll(poker_t *poker, uint8_t count) {
-  uint8_t x, y;
-  pokerplayer_t *player;
-
-  for(y = 0; y < count; y++) {
-    for(x = 0; x < POKER_PLAYER_COUNT; x++) {
-      player = poker->players + x;
-      if(!pokerPlayerIsAlive(player)) continue;
-      pokerPlayerDeal(poker, player);
-    }
-  }
 }
\ No newline at end of file
diff --git a/src/poker/player.h b/src/poker/player.h
index 44bda366..86cd0881 100644
--- a/src/poker/player.h
+++ b/src/poker/player.h
@@ -25,19 +25,3 @@ bool pokerPlayerIsAlive(pokerplayer_t *player);
  * @returns True if the player is human.
  */
 bool pokerPlayerIsHuman(poker_t *poker, pokerplayer_t *player);
-
-/**
- * Deal a card to a player.
- * 
- * @param poker Poker game instance.
- * @param player Poker player to deal to.
- */
-void pokerPlayerDeal(poker_t *poker, pokerplayer_t *player);
-
-/**
- * Deal card(s) to every active player.
- * 
- * @param poker Poker game instance.
- * @param count Count of cards to deal.
- */
-void pokerPlayerDealAll(poker_t *poker, uint8_t count);
\ No newline at end of file
diff --git a/src/poker/round/deal.c b/src/poker/round/deal.c
index 85ea37a3..a1aed39a 100644
--- a/src/poker/round/deal.c
+++ b/src/poker/round/deal.c
@@ -13,10 +13,10 @@ void pokerDealInit(poker_t *poker) {
   pokerLookAtPlayer(&poker->cameraWorld, POKER_SEAT_DEALER, 0);
 
   // Shuffle the deck
-  cardShuffle(poker->deck, CARD_DECK_SIZE);
+  cardShuffle(poker->dealer.deck, CARD_DECK_SIZE);
 
   // Deal 2 card to each player
-  pokerPlayerDealAll(poker, POKER_DEAL_CARD_EACH);
+  pokerDealerDealAll(poker, POKER_DEAL_CARD_EACH);
   
   printf("Cards Dealt\n");
   pokerBetInit(poker);
diff --git a/src/poker/round/deal.h b/src/poker/round/deal.h
index 1946da66..900a344b 100644
--- a/src/poker/round/deal.h
+++ b/src/poker/round/deal.h
@@ -9,7 +9,7 @@
 #include <dawn/dawn.h>
 #include "../../util/array.h"
 #include "../render/look.h"
-#include "../player.h"
+#include "../dealer.h"
 #include "../card.h"
 #include "bet.h"
 
diff --git a/src/poker/round/flop.c b/src/poker/round/flop.c
index e5807ed8..d2c2db03 100644
--- a/src/poker/round/flop.c
+++ b/src/poker/round/flop.c
@@ -24,11 +24,9 @@ void pokerFlopInit(poker_t *poker) {
     count = POKER_RIVER_CARD_COUNT;
   }
 
-  // Burn a card
-  poker->deckSize--;
-
-  // Flops
-  pokerDealerDeal(poker, count);
+  // Burn and flop.
+  pokerDealerBurn(&poker->dealer, 1);
+  pokerDealerTurn(&poker->dealer, count);
 
   pokerBetInit(poker);
 }
\ No newline at end of file
diff --git a/src/poker/round/start.c b/src/poker/round/start.c
index 48a9737b..bd0977b7 100644
--- a/src/poker/round/start.c
+++ b/src/poker/round/start.c
@@ -17,10 +17,7 @@ void pokerStartInit(poker_t *poker) {
   // Prepare the initial game state
   poker->round = POKER_ROUND_DEAL;
   poker->bet.pot = 0;
-  poker->graveSize = 0;
-  poker->dealer.cardsFacing = 0;
-  poker->deckSize = CARD_DECK_SIZE;
-  for(i = 0; i < CARD_DECK_SIZE; i++) poker->deck[i] = i;
+  pokerDealerInit(&poker->dealer);
 
   // Reset the players
   for(i = 0; i < POKER_PLAYER_COUNT; i++) {
diff --git a/src/util/array.c b/src/util/array.c
index 4470f3cb..511abdbb 100644
--- a/src/util/array.c
+++ b/src/util/array.c
@@ -61,7 +61,7 @@ void arrayCopy(size_t size, void *source, int32_t length, void *dest) {
 }
 
 void arraySort(size_t size, void *array, int32_t length, arraysort_t *sort) {
-  qsort(array, length, size, (_CoreCrtNonSecureSearchSortCompareFunction)sort);
+  qsort(array, length, size, sort);
 }
 
 // Common Sorters:
@@ -69,13 +69,13 @@ void arraySort(size_t size, void *array, int32_t length, arraysort_t *sort) {
 void arraySortInt32(int32_t *array, int32_t length) {
   arraySort(sizeof(int32_t), array, length, &_arraySorterInt32);
 }
-int32_t _arraySorterInt32(void* left, void* right) {
+int32_t _arraySorterInt32(const void* left, const void* right) {
   return *((int32_t *)left) - *((int32_t *)right);
 }
 
 void arraySortUint8(uint8_t *array, int32_t length) {
   arraySort(sizeof(uint8_t), array, length, &_arraySorterUint8);
 }
-int32_t _arraySorterUint8(void* left, void* right) {
+int32_t _arraySorterUint8(const void* left, const void* right) {
   return *((uint8_t *)left) - *((uint8_t *)right);
 }
\ No newline at end of file
diff --git a/src/util/array.h b/src/util/array.h
index ac752262..9e469e63 100644
--- a/src/util/array.h
+++ b/src/util/array.h
@@ -79,7 +79,7 @@ void arraySort(size_t size, void *array, int32_t length, arraysort_t *sort);
  */
 void arraySortInt32(int32_t *array, int32_t length);
 /** Internal int32_t array sorter. */
-int32_t _arraySorterInt32(void *left, void* right);
+int32_t _arraySorterInt32(const void *left, const void* right);
 
 /**
  * Sort a uint8_t array.
@@ -89,4 +89,4 @@ int32_t _arraySorterInt32(void *left, void* right);
  */
 void arraySortUint8(uint8_t *array, int32_t length);
 /** Internal uint8_t array sorter. */
-int32_t _arraySorterUint8(void* left, void* right);
\ No newline at end of file
+int32_t _arraySorterUint8(const void* left, const void* right);
\ No newline at end of file