[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>
This commit is contained in:
Geralt
2026-05-02 14:28:52 -07:00
committed by GitHub
co-authored by MathisMartin31
parent da988ba7f5
commit e7f550ce22
38 changed files with 1231 additions and 185 deletions
+7
View File
@@ -33,6 +33,13 @@
*/
enum AffineBackgroundID
{
/**
* @brief Display background for the initial splash screen.
*
* Signifies that no background is currently in use.
*/
AFFINE_BG_NONE,
/**
* @brief Display background for main menu.
*
+9 -3
View File
@@ -9,10 +9,16 @@
#include <mm_types.h>
/**
* @def MM_FULL_VOLUME
* @def MM_MODULE_FULL_VOLUME
* @brief The maximum volume for maxmod module volume (main theme)
*/
#define MM_MODULE_FULL_VOLUME 1024
/**
* @def MM_SFX_FULL_VOLUME
* @brief The maximum volume for maxmod mm_sound_effect.volume
*/
#define MM_FULL_VOLUME 255
#define MM_SFX_FULL_VOLUME 255
/**
* @def MM_PAN_CENTER
@@ -32,7 +38,7 @@
* @def SFX_DEFAULT_VOLUME
* @brief Default volume for sound effects
*/
#define SFX_DEFAULT_VOLUME MM_FULL_VOLUME
#define SFX_DEFAULT_VOLUME MM_SFX_FULL_VOLUME
/**
* @def SFX_DEFAULT_PAN
+1 -1
View File
@@ -11,7 +11,7 @@
#define BTN_HIGHLIGHT_COLOR 0xFFFF
#define BUTTON_SFX_VOLUME 154 // 60% of MM_FULL_VOLUME
#define BUTTON_SFX_VOLUME 154 // 60% of MM_SFX_FULL_VOLUME
/**
* @brief The function to be called when the button is pressed
+1
View File
@@ -56,6 +56,7 @@ typedef struct CardObject
// Card functions
void card_init();
void toggle_high_contrast_cards(bool enable);
// Card methods
Card* card_new(u8 suit, u8 rank);
+10 -9
View File
@@ -1,12 +1,13 @@
// clang-format off
// (stateEnum, on_init, on_update, on_exit)
DEF_STATE_INFO(GAME_STATE_SPLASH_SCREEN, splash_screen_on_init, splash_screen_on_update, splash_screen_on_exit)
DEF_STATE_INFO(GAME_STATE_MAIN_MENU, game_main_menu_on_init, game_main_menu_on_update, game_main_menu_on_exit)
DEF_STATE_INFO(GAME_STATE_GAME_START, noop, game_start, noop)
DEF_STATE_INFO(GAME_STATE_PLAYING, game_round_on_init, game_playing_on_update, noop)
DEF_STATE_INFO(GAME_STATE_ROUND_END, noop, game_round_end_on_update, game_round_end_on_exit)
DEF_STATE_INFO(GAME_STATE_SHOP, noop, game_shop_on_update, game_shop_on_exit)
DEF_STATE_INFO(GAME_STATE_BLIND_SELECT, game_blind_select_on_init, game_blind_select_on_update, game_blind_select_on_exit)
DEF_STATE_INFO(GAME_STATE_LOSE, game_lose_on_init, game_lose_on_update, game_over_on_exit)
DEF_STATE_INFO(GAME_STATE_WIN, game_win_on_init, game_win_on_update, game_over_on_exit)
DEF_STATE_INFO(GAME_STATE_SPLASH_SCREEN, splash_screen_on_init, splash_screen_on_update, splash_screen_on_exit )
DEF_STATE_INFO(GAME_STATE_MAIN_MENU, game_main_menu_on_init, game_main_menu_on_update, game_main_menu_on_exit )
DEF_STATE_INFO(GAME_STATE_OPTIONS_MENU, game_options_menu_on_init, game_options_menu_on_update, game_options_menu_on_exit)
DEF_STATE_INFO(GAME_STATE_GAME_START, noop, game_start, noop )
DEF_STATE_INFO(GAME_STATE_PLAYING, game_round_on_init, game_playing_on_update, noop )
DEF_STATE_INFO(GAME_STATE_ROUND_END, noop, game_round_end_on_update, game_round_end_on_exit )
DEF_STATE_INFO(GAME_STATE_SHOP, noop, game_shop_on_update, game_shop_on_exit )
DEF_STATE_INFO(GAME_STATE_BLIND_SELECT, game_blind_select_on_init, game_blind_select_on_update, game_blind_select_on_exit)
DEF_STATE_INFO(GAME_STATE_LOSE, game_lose_on_init, game_lose_on_update, game_over_on_exit )
DEF_STATE_INFO(GAME_STATE_WIN, game_win_on_init, game_win_on_update, game_over_on_exit )
// clang-format on
+9 -9
View File
@@ -1,6 +1,7 @@
#ifndef GAME_H
#define GAME_H
#include "blind.h"
#include "game/common_ui.h"
#include "game_variables.h"
@@ -13,7 +14,7 @@ extern GameVariables g_game_vars;
#define MAX_JOKERS_HELD_SIZE 5 // This doesn't account for negatives right now.
#define MAX_SHOP_JOKERS 2 // TODO: Make this dynamic and allow for other items besides jokers
#define MAX_SELECTION_SIZE 5
#define FRAMES(x) (((x) + game_speed - 1) / game_speed)
#define FRAMES(x) (((x) + (g_game_vars.game_speed) - 1) / (g_game_vars.game_speed))
// TODO: Can make these dynamic to support interest-related jokers and vouchers
#define MAX_INTEREST 5
@@ -123,7 +124,7 @@ typedef struct ContainedHandTypes
} ContainedHandTypes;
// clang-format on
typedef void (*GameStateCallback)();
typedef void (*GameStateCallback)(void);
typedef struct
{
@@ -158,17 +159,16 @@ int get_num_hands_remaining(void);
u32 get_chips(void);
void set_chips(u32 new_chips);
void display_chips();
void display_chips(void);
u32 get_mult(void);
void set_mult(u32 new_mult);
void display_mult();
int get_money(void);
void set_money(int new_money);
void display_money();
void display_mult(void);
void display_money(void);
void set_retrigger(bool new_retrigger);
enum BlindType get_current_blind(void);
enum BlindType get_next_boss_blind(void);
int get_game_speed(void);
void set_game_speed(int new_game_speed);
u32 get_rand(void);
// joker specific functions
bool is_shortcut_joker_active(void);
+1
View File
@@ -17,6 +17,7 @@ enum BackgroundId
BG_ROUND_END,
BG_SHOP,
BG_BLIND_SELECT,
BG_OPTIONS_MENU,
BG_MAIN_MENU
};
+1 -7
View File
@@ -15,23 +15,17 @@ void game_main_menu_change_background(void);
/**
* @brief Main menu state initialization
*
* @param vars passed @ref GameVariables struct
*/
void game_main_menu_on_init(void);
/**
* @brief Main menu state update
*
* @param vars passed @ref GameVariables struct
*/
void game_main_menu_on_update(void);
/**
* @brief Main menu cleanup (called when transitioning to game start)
*
* @param vars passed @ref GameVariables struct
*/
void game_main_menu_on_exit(void);
#endif // GAME_MAIN_MENU_H
#endif // GAME_MAIN_MENU_H
+31
View File
@@ -0,0 +1,31 @@
/**
* @file options_menu.h
*
* @brief Options menu state functions.
*/
#ifndef GAME_OPTIONS_MENU_H
#define GAME_OPTIONS_MENU_H
#include "game_variables.h"
/**
* @brief Change the options menu background
*/
void game_options_menu_change_background(void);
/**
* @brief Options menu state initialization
*/
void game_options_menu_on_init(void);
/**
* @brief Options menu state update
*/
void game_options_menu_on_update(void);
/**
* @brief Options menu cleanup (called when going back to main menu)
*/
void game_options_menu_on_exit(void);
#endif // GAME_OPTIONS_MENU_H
+32 -1
View File
@@ -8,6 +8,19 @@
#include <tonc.h>
#define GAME_SPEED_MIN 1
#define GAME_SPEED_MAX 4
// Volume is stored from 0 to 5 but is an increment of 20 so 0 to 100 will be displayed
#define VOLUME_OPTION_MIN 0
#define VOLUME_OPTION_MAX 5
#define VOLUME_OPTION_INCREMENT 20
#define DEFAULT_GAME_SPEED 1
#define DEFAULT_HIGH_CONTRAST false
#define DEFAULT_MUSIC_VOLUME VOLUME_OPTION_MAX
#define DEFAULT_SOUND_VOLUME VOLUME_OPTION_MAX
/**
* @brief A central location for all game variables.
*
@@ -16,8 +29,26 @@
*/
typedef struct
{
s32 timer;
// Internal variables
s32 timer; // This might already exist in libtonc but idk so i'm just making my own
u32 rng_seed;
u32 rng_step; // Position in the rng sequence.
// Variables visible by the player
int round;
int ante;
int money;
// Options variables
// BY DEFAULT IS SET TO 1, but if changed to 2 or more, should speed up all (or most) of the
// game aspects that should be sped up by speed, as in the original game.
u8 game_speed;
bool high_contrast;
u8 music_volume;
u8 sound_volume;
} GameVariables;
extern GameVariables g_game_vars;
+25
View File
@@ -0,0 +1,25 @@
/**
* @file save.h
*
* @brief Utils functions to save/load data structures from/to the SRAM.
*/
#ifndef SAVE_H
#define SAVE_H
#include "game_variables.h"
#include <tonc.h>
/**
* @brief Save game data to SRAM.
*/
void save_game();
/**
* @brief Load game data from SRAM.
*
* @sa save_game
*/
void load_game();
#endif // SAVE_H
+1 -1
View File
@@ -9,4 +9,4 @@
#define TM_ZERO 0
#endif // TIMER_H
#endif // TIMER_H
+11
View File
@@ -0,0 +1,11 @@
/**
* @file version.h
*
* @brief Game ROM version global var declaration.
*/
#ifndef VERSION_H
#define VERSION_H
extern const char gbalatro_version[];
#endif // VERSION_H