Files
balatro-gba-chinese-jocker-…/source/card.c
T
Geralt e7f550ce22 [Feature] Implement options menu and reading/writing values from/to the SRAM (#449)
* background

* Implement Options button

* Update main menu artwork

* add options menu tiles

* prepare options menu handling

* incorporate main menu refactor

* toto

* add all menu interactions

* do not reset selection_x in main_menu

* options struct

* fix rebase from ricfehr3's repo

* Implement high contrast cards

* Save/Load options values to SRAM

* load options on game_init and validate data within the load_options function

* Get changes from ricfehr3's PR

* recover fix for glitched ace

* clang format

* clang format

* clang format

* Save 500B of image data by using only minimal set of tiles instead of keeping final window in source file

* clang format

* clang format

* Implement Options button

* Update main menu artwork

* progress, leaving it alone for now until discussion

* make game_vars an extern

* Move options vars into their own struct

* rollback previous commit

* Implement save validation. Version check to come

* implement data validity + version check for game saves

* clang format

* Save position in rng sequence for consistent run after load

* fix rebase issues

* fix rebase issue

* remove unused/duplicate define

* Oops, made a mistake when testing PR #424

* encode high contrast palette into an image, and load it at game_start

* document save.c/h and correct typos

* correct alignment

* game speed and high contrast buttons working

* small cleanup

* sliders also work again now

* clang format

* Moved balatro_version extern declaration to version.h file

* removed unsused variable

* make showing ROM version a toggle

* move ante, round and money to the GameVariables struct

* Rename SaveCheckInfo struct to SaveHeader for clarity

* Add SaveHeader struct explanation

* clang format

* Revert unnecessary tileset optimization

* SFX now follows the sound volume slider

* clang format

* Music now follows volume slider

* Changed volume slider increment from 5 to 20 to avoid having the music sound too rough under 20% volume

* refactor main_menu.c to use SelectionGrid and Button structs

* clang format

* make a "new" save button so we don't save during the state transition

* clang format

* Reduced state transition flicker by only changing affine background to a different one

* Fix loading game too late in game init sequence

* First batch of suggestions from @MeirGavish

* Remove unnecessary intermediary buffers for high contrast palette swapping

* comment row warp hack for button input

* Add some of Copilot's suggestions

* rename balatro_version into gbalatro_version for rebranding reasons + version display erase screen fix

* remove unnecessary NULL check on `on_selection_changed` method

* implement small game speed arrow buttons animation

* factorize some code

* remove unnecessary data validation in load_game(), since we already checked the header

* Implement @MeirGavish 's suggestion regarding splitting the huge `on_update` function

* clang format

* implement @ricfehr3 's suggestion about keeping last selected button instead of using an obscure `on_boot` boolean

* Apply @ricfehr3's suggestions

* fix clang-format flags

---------

Co-authored-by: MathisMartin31 <mathis.martin31@gmail.com>
2026-05-02 14:28:52 -07:00

147 lines
3.4 KiB
C

#include "card.h"
#include "deck_gfx.h"
#include "graphic_utils.h"
#include "high_contrast_deck_pal_gfx.h"
#include <maxmod.h>
#include <stdlib.h>
// Audio
#include "pool.h"
#include "soundbank.h"
// Card sprites lookup table. First index is the suit, second index is the rank. The value is the
// tile index.
const static u16 _card_sprite_lut[NUM_SUITS][NUM_RANKS] = {
{0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192},
{208, 224, 240, 256, 272, 288, 304, 320, 336, 352, 368, 384, 400},
{416, 432, 448, 464, 480, 496, 512, 528, 544, 560, 576, 592, 608},
{624, 640, 656, 672, 688, 704, 720, 736, 752, 768, 784, 800, 816}
};
void card_init()
{
toggle_high_contrast_cards(false);
}
void toggle_high_contrast_cards(bool enable)
{
if (enable)
{
GRIT_CPY(&pal_obj_mem[CARD_PB], high_contrast_deck_pal_gfxPal);
}
else
{
GRIT_CPY(&pal_obj_mem[CARD_PB], deck_gfxPal);
}
}
// Card methods
Card* card_new(u8 suit, u8 rank)
{
Card* card = POOL_GET(Card);
card->suit = suit;
card->rank = rank;
return card;
}
void card_destroy(Card** card)
{
POOL_FREE(Card, *card);
*card = NULL;
}
u8 card_get_value(Card* card)
{
if (card->rank == JACK || card->rank == QUEEN || card->rank == KING)
{
return 10; // Face cards are worth 10
}
else if (card->rank == ACE)
{
return 11; // Ace is worth 11
}
else
{
return card->rank + RANK_OFFSET; // 2-10 are worth their rank + RANK_OFFSET
}
return 0; // Should never reach here, but just in case
}
// CardObject methods
CardObject* card_object_new(Card* card)
{
CardObject* card_object = POOL_GET(CardObject);
card_object->card = card;
card_object->sprite_object = sprite_object_new();
card_object->selected = false;
return card_object;
}
void card_object_destroy(CardObject** card_object)
{
if (*card_object == NULL)
return;
sprite_object_destroy(&((*card_object)->sprite_object));
POOL_FREE(CardObject, *card_object);
*card_object = NULL;
}
void card_object_update(CardObject* card_object)
{
if (card_object == NULL)
return;
sprite_object_update(card_object->sprite_object);
}
void card_object_set_sprite(CardObject* card_object, int layer)
{
int tile_index = CARD_TID + (layer * CARD_SPRITE_OFFSET);
memcpy32(
&tile_mem[TILE_MEM_OBJ_CHARBLOCK0_IDX][tile_index],
&deck_gfxTiles
[_card_sprite_lut[card_object->card->suit][card_object->card->rank] * TILE_SIZE],
TILE_SIZE * CARD_SPRITE_OFFSET
);
Sprite* sprite = sprite_new(
ATTR0_SQUARE | ATTR0_4BPP | ATTR0_AFF,
ATTR1_SIZE_32,
tile_index,
0,
layer + CARD_STARTING_LAYER
);
sprite_object_set_sprite(card_object->sprite_object, sprite);
}
void card_object_shake(CardObject* card_object, mm_word sound_id)
{
sprite_object_shake(card_object->sprite_object, sound_id);
}
void card_object_set_selected(CardObject* card_object, bool selected)
{
if (card_object == NULL)
return;
card_object->selected = selected;
}
bool card_object_is_selected(CardObject* card_object)
{
if (card_object == NULL)
return false;
return card_object->selected;
}
Sprite* card_object_get_sprite(CardObject* card_object)
{
if (card_object == NULL)
return NULL;
return sprite_object_get_sprite(card_object->sprite_object);
}