Continue menu

This commit is contained in:
Miloslav Ciz 2025-01-07 13:17:41 +01:00
parent 6e8badf4b1
commit 5d30b9d600
4 changed files with 48 additions and 26 deletions

View file

@ -25,9 +25,9 @@ static const char *LCR_texts[] =
"play repl",
// main menu items:
"camera",
"music",
"sound",
"camera: $",
"music: $",
"sound: $",
"save repl",
"exit",

19
audio.h
View file

@ -17,6 +17,7 @@
struct
{
uint32_t frame;
uint8_t on;
uint8_t soundPlayed;
uint16_t soundPlayedFrame;
uint32_t noise;
@ -38,19 +39,14 @@ void LCR_audioInit(void)
{
LCR_LOG0("initializing audio");
LCR_audio.frame = 0;
LCR_audio.on = 1;
LCR_audio.soundPlayed = LCR_SOUND_NONE;
LCR_audio.soundPlayedFrame = 0;
LCR_audio.noise = 0;
LCR_audio.crashSample = 0;
LCR_audio.engineOsc = 0;
LCR_audio.engineInc = 1;
LCR_audio.engineIntensity = 0;
LCR_audio.crashSample = 0;
LCR_audio.engineOsc = 0;
LCR_audio.engineInc = 1;
LCR_audio.engineIntensity = 0;
}
void LCR_audioSetEngineIntensity(uint8_t value)
@ -75,6 +71,9 @@ uint8_t LCR_audioGetNextSample(void)
{
unsigned char result = 128;
if (!LCR_audio.on)
return result;
switch (LCR_audio.soundPlayed)
{
case LCR_SOUND_CRASH_SMALL:

View file

@ -26,11 +26,14 @@ void LCR_appendResourceStr(const char *str)
void audioFillCallback(void *userdata, uint8_t *s, int l)
{
if (musicFile)
if (musicFile && LCR_gameMusicOn())
{
if (!fread(s,1,l,musicFile))
rewind(musicFile);
}
else
for (int i = 0; i < l; ++i)
s[i] = 128;
for (int i = 0; i < l; ++i)
s[i] = s[i] / 2 + LCR_gameGetNextAudioSample() / 2;

44
game.h
View file

@ -61,7 +61,7 @@
- Call the below described functions as described.
- If you want to support music, make your frontend play music from the "music"
file in assets. It is in raw format, storing 8bit unsigned samples at 8000
Hz. Use the LCR_gameGetMusicVolume to check what the music volume is. If you
Hz. Use the LCR_gameMusicOn to check what the music volume is. If you
don't support music, set LCR_SETTING_MUSIC to 0 in your frontend code so
that the game knows music is disabled.
*/
@ -138,7 +138,7 @@ uint8_t LCR_gameStep(uint32_t timeMs);
/**
Gets the current music volume;
*/
uint8_t LCR_gameGetMusicVolume(void);
uint8_t LCR_gameMusicOn(void);
/**
Gets next audio sample (unsigned 8bit samples, 8 KHz).
@ -147,6 +147,8 @@ uint8_t LCR_gameGetNextAudioSample(void);
//------------------------------------------------------------------------------
#include "settings.h"
#define LCR_LOG0(s) ;
#define LCR_LOG1(s) ;
#define LCR_LOG2(s) ;
@ -194,7 +196,7 @@ struct
uint32_t nextRacingTickTime;
uint8_t controlMode;
uint8_t debugDraw;
uint8_t musicVolume;
uint8_t musicOn;
uint8_t keyStates[LCR_KEYS_TOTAL]; /**< Assures unchanging key states
during a single frame, hold number of
frames for which the key has been
@ -218,10 +220,10 @@ const char *menuItemNamePointers[LCR_MENU_MAX_ITEMS];
} resourceFile;
} LCR_game;
uint8_t LCR_gameGetMusicVolume(void)
uint8_t LCR_gameMusicOn(void)
{
#if LCR_SETTING_MUSIC
return LCR_game.musicVolume;
return LCR_game.musicOn;
#else
return 0;
#endif
@ -399,11 +401,11 @@ LCR_game.menuItemCount = 0;
}
void LCR_gameSetMenuItemStr(uint8_t item, const char *str)
void LCR_gameSetMenuItemStr(uint8_t item, const char *str, char replaceChar)
{
for (int i = 0; i < LCR_MENU_STRING_SIZE - 1; ++i)
{
LCR_game.menuItemNames[item][i] = str[i];
LCR_game.menuItemNames[item][i] = str[i] == '$' ? replaceChar : str[i];
LCR_game.menuItemNames[item][i + 1] = 0;
}
}
@ -411,7 +413,12 @@ void LCR_gameSetMenuItemStr(uint8_t item, const char *str)
void LCR_gameLoadMainMenuItems(void)
{
for (int i = 0; i < 5; ++i)
LCR_gameSetMenuItemStr(i,LCR_texts[4 + i]);
{
char replaceChar = i == 0 ? 'X' :
(i == 1 ? '0' + LCR_game.musicOn : ('0' + LCR_audio.on));
LCR_gameSetMenuItemStr(i,LCR_texts[4 + i],replaceChar);
}
LCR_game.menuItemCount = 4;
}
@ -433,17 +440,17 @@ void LCR_gameInit(void)
for (int i = 0; i < LCR_MENU_MAX_ITEMS; ++i)
LCR_game.menuItemNamePointers[i] = LCR_game.menuItemNames[i];
LCR_gameLoadMainMenuItems();
LCR_game.menuSelectedTab = 0;
LCR_game.menuSelectedItem = 0;
LCR_game.frame = 0;
LCR_game.musicVolume = 255;
LCR_game.musicOn = 1;
LCR_game.nextRenderFrameTime = 0;
LCR_game.nextRacingTickTime = 0;
LCR_game.controlMode = LCR_CONTROL_MODE_DRIVE;
LCR_gameLoadMainMenuItems();
LCR_gameSetState(LCR_GAME_STATE_MENU);
LCR_currentMap.blockCount = 0; // means no map loaded
@ -677,6 +684,17 @@ void LCR_gameHandleInput(void)
case 0:
switch (LCR_game.menuSelectedItem)
{
case 0:
break;
case 1:
LCR_game.musicOn = !LCR_game.musicOn;
break;
case 2:
LCR_audio.on = !LCR_audio.on;
break;
case 4:
LCR_gameSetState(LCR_GAME_STATE_END);
break;
@ -684,6 +702,7 @@ void LCR_gameHandleInput(void)
default: break;
}
LCR_gameLoadMainMenuItems();
break;
case 1:
@ -708,6 +727,7 @@ void LCR_gameHandleInput(void)
{
LCR_LOG1("menu open");
LCR_gameSetState(LCR_GAME_STATE_MENU);
LCR_game.menuSelectedItem = 0;
}
else if (LCR_game.keyStates[LCR_KEY_A] == 1)
LCR_gameResetRun();
@ -875,7 +895,7 @@ uint8_t LCR_gameStep(uint32_t time)
LCR_sleep(sleep);
else
{
LCR_LOG1("can't sleep, frames take too long!");
LCR_LOG2("can't sleep");
}
LCR_game.frame++;