Added some basic font rendering and texas holdem

This commit is contained in:
2021-05-03 21:32:40 -07:00
parent 96db74a546
commit 469750b0a0
31 changed files with 826 additions and 67 deletions

34
src/card/card.c Normal file
View File

@ -0,0 +1,34 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "card.h"
void cardDeckFill(card_t *deck) {
uint8_t i;
for(i = 0; i < CARD_DECK_SIZE; i++) {
deck[i] = i;
}
}
void cardShuffle(card_t *hand, uint8_t cardCount) {
uint8_t i, j;
card_t temporary;
for(i = 0; i < cardCount - 1; i++) {
// Select random element from remaining elements.
j = u8randRange(i, cardCount);
temporary = hand[j];// Take out other card
hand[j] = hand[i];// Move my card there
hand[i] = temporary;// Put other card here.
}
}
void cardDeal(card_t *deck, card_t *hand, uint8_t deckSize, uint8_t handSize) {
card_t card;
card = deck[deckSize-1];
deck[deckSize-1] = 0x00;
hand[handSize] = card;
}

35
src/card/card.h Normal file
View File

@ -0,0 +1,35 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
#include "../util/rand.h"
/**
* Fills a deck with a standard set of cards (unshuffled)
*
* @param deck Deck to fill. Must be at least 52 elements.
*/
void cardDeckFill(card_t *deck);
/**
* Shuffles the given hand or deck.
*
* @param hand The hand/deck to shuffle.
* @param cardCount The amount of cards that are in that deck/hand.
*/
void cardShuffle(card_t *hand, uint8_t cardCount);
/**
* Deals a card of the top of the deck into the given hand.
*
* @param deck Deck to take from.
* @param hand Hand to put into.
* @param deckSize Size of the current deck.
* @param handSize Size of the current hand.
*/
void cardDeal(card_t *deck, card_t *hand, uint8_t deckSize, uint8_t handSize);

169
src/card/poker/holdem.c Normal file
View File

@ -0,0 +1,169 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "holdem.h"
void holdemGameInit(holdemgame_t *game) {
uint8_t i;
holdemplayer_t *player;
// Set Blinds to nil.
game->blindBig = 0;
game->blindSmall = 0;
// Reset the players
for(i = 0; i < HOLDEM_PLAYER_COUNT; i++) {
player = game->players + i;
// Set their state to 0
player->state = 0x00;
// Set their chips to zero
player->chips = 0;
}
}
void holdemRoundInit(holdemgame_t *game) {
uint8_t i;
holdemplayer_t *player;
// Refill the deck
cardDeckFill(game->deck);
game->deckSize = CARD_DECK_SIZE;
// Reset the pot
game->pot = 0;
// Reset the cards
game->cardsFacing = 0;
// Reset the players
for(i = 0; i < HOLDEM_PLAYER_COUNT; i++) {
player = game->players + i;
// Clear Round State(s)
player->state &= ~(
HOLDEM_STATE_FOLDED |
HOLDEM_STATE_SHOWING
);
player->cardCount = 0;
// Set the players' current bet to zero.
player->currentBet = 0;
}
}
void holdemDeal(holdemgame_t *game, holdemplayer_t *player) {
cardDeal(game->deck, player->cards, game->deckSize, player->cardCount);
game->deckSize--;
player->cardCount++;
}
void holdemDealAll(holdemgame_t *game, uint8_t count) {
uint8_t i, j;
for(i = 0; i < count; i++) {
for(j = 0; j < HOLDEM_PLAYER_COUNT; j++) {
holdemDeal(game, game->players + j);
}
}
}
void holdemFlop(holdemgame_t *game) {
if(game->cardsFacing >= HOLDEM_DEALER_HAND) return;
uint8_t i, count;
// Burn the card off the top
game->deckSize -= 1;
// Change count depending on facing
count = game->cardsFacing == 0 ? 0x03 : 0x01;
// Deal
for(i = 0; i < count; i++) {
cardDeal(game->deck, game->cards, game->deckSize, game->cardsFacing);
game->deckSize -= 1;
game->cardsFacing += 1;
}
}
void holdemRenderInit(holdemrender_t *render) {
render->batch = spriteBatchCreate(
CARD_DECK_SIZE + (HOLDEM_PLAYER_COUNT * HOLDEM_PLAYER_HAND)
);
render->texture = assetTextureLoad("cards_normal.png");
render->tileset = tilesetCreate(
CARD_COUNT_PER_SUIT, 6,
render->texture->width, render->texture->height,
0, 0, 0, 0
);
}
void holdemRenderCard(holdemrender_t *render, card_t card, bool faceUp, float x, float y, float z) {
int32_t i;
if(faceUp) {
uint8_t suit = card / CARD_COUNT_PER_SUIT;
uint8_t num = card % CARD_COUNT_PER_SUIT;
i = num + ((
suit == 0 ? 2 :
suit == 1 ? 3 :
suit == 2 ? 1 :
0
) * CARD_COUNT_PER_SUIT);
} else {
i = 4 * render->tileset->columns;
}
tilesetdiv_t *div = render->tileset->divisions + i;
spriteBatchQuad(render->batch, -1,
x, y, z,
3, 4,
div->x0, div->y0, div->x1, div->y1
);
}
void holdemRender(holdemrender_t *render, holdemgame_t *game) {
uint8_t i, j;
holdemplayer_t *player;
shaderUseTexture(GAME_STATE.shaderWorld, render->texture);
// Flush
spriteBatchFlush(render->batch);
// Render Deck
for(i = 0; i < game->deckSize; i++) {
holdemRenderCard(render, game->deck[i], false, 0, 6, i * 0.05f);
}
for(i = 0; i < game->cardsFacing; i++) {
holdemRenderCard(render, game->cards[i], true, 0, -i*1.5, i * 0.05f);
}
// Render each players's hand
for(i = 0; i < HOLDEM_PLAYER_COUNT; i++) {
player = game->players + i;
for(j = 0; j < player->cardCount; j++) {
holdemRenderCard(render, player->cards[j], true,
4*(i+1), -(j * 1), j * 0.05f
);
}
}
// Draw
spriteBatchDraw(render->batch, 0, -1);
}
uint32_t holdemBet(holdemgame_t *game, holdemplayer_t *player, uint32_t amount) {
uint32_t realAmount = mathMin(player->chips, amount);
game->pot += realAmount;
player->chips -= realAmount;
return realAmount;
}

85
src/card/poker/holdem.h Normal file
View File

@ -0,0 +1,85 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
#include "../card.h"
#include "../../util/rand.h"
#include "../../display/spritebatch.h"
#include "../../display/texture.h"
#include "../../display/tileset.h"
#include "../../display/shader.h"
#include "../../file/asset.h"
/**
* Initializes a Teax Hold'em Game for the first time.
*
* @param game Game to initialize.
*/
void holdemGameInit(holdemgame_t *game);
/**
* Initializes a Texas Hold'em Round for the first time. Does not affect the
* global game state.
*
* @param game Game's round you want to initialize.
*/
void holdemRoundInit(holdemgame_t *game);
/**
* Deals a card to the given player, does all the follow up.
*
* @param game Game who's deck to deal from.
* @param player Player to deal into.
*/
void holdemDeal(holdemgame_t *game, holdemplayer_t *player);
/**
* Deal cards to all players.
*
* @param game Game and players to deal around.
* @param count Count of cards to deal to each player.
*/
void holdemDealAll(holdemgame_t *game, uint8_t count);
/**
* Draw the flop, turn or river
*
* @param game Game to flop/turn/river.
*/
void holdemFlop(holdemgame_t *game);
/**
* Initialize the Texas Hold'em Renderer.
*
* @param render Renderer to init.
*/
void holdemRenderInit(holdemrender_t *render);
/**
* Renders a card.
*
* @param render Renderer to render against
* @param card Card to render.
*/
void holdemRenderCard(holdemrender_t *render, card_t card, bool faceUp, float x, float y, float z);
/**
* Render an entire holdem game.
*/
void holdemRender(holdemrender_t *render, holdemgame_t *game);
/**
* Takes the given bet from a player
*
* @param game Game to add the bet to.
* @param player Player to take the bet from.
* @param amount Amount to try and take.
* @return The real amount that was taken, chips considered.
*/
uint32_t holdemBet(holdemgame_t *game, holdemplayer_t *player, uint32_t amount);