Rework menu a bit

This commit is contained in:
Miloslav Ciz 2025-01-05 14:47:01 +01:00
parent 1efeee9c36
commit c017297e06
2 changed files with 71 additions and 27 deletions

86
game.h
View file

@ -176,7 +176,7 @@ uint8_t LCR_gameGetNextAudioSample(void);
#define LCR_GAME_STATE_END 0xff #define LCR_GAME_STATE_END 0xff
// TODO: move to consts? // TODO: move to consts?
#define LCR_MENU_MAX_ITEMS 9 #define LCR_MENU_MAX_ITEMS 9 // don't change
#define LCR_RESOURCE_ITEM_CHUNK (LCR_MENU_MAX_ITEMS - 1) #define LCR_RESOURCE_ITEM_CHUNK (LCR_MENU_MAX_ITEMS - 1)
#define LCR_MENU_TABS 4 #define LCR_MENU_TABS 4
#define LCR_MENU_STRING_SIZE 16 #define LCR_MENU_STRING_SIZE 16
@ -202,13 +202,16 @@ struct
uint8_t menuSelectedTab; uint8_t menuSelectedTab;
uint8_t menuSelectedItem; uint8_t menuSelectedItem;
char menuItemNames[LCR_MENU_MAX_ITEMS][LCR_MENU_STRING_SIZE];
uint8_t menuItemCount;
const char *menuItemNamePointers[LCR_MENU_MAX_ITEMS];
struct struct
{ {
int state; ///< -1 if reading external res. f., else pos. int state; ///< -1 if reading external res. f., else pos.
// indices and counts are among the resources of the same type // indices and counts are among the resources of the same type
unsigned char loadedItemCount;
char loadedItemNames[LCR_RESOURCE_ITEM_CHUNK * LCR_MENU_STRING_SIZE];
unsigned int firstItemIndex; unsigned int firstItemIndex;
unsigned int itemsTotal; unsigned int itemsTotal;
} resourceFile; } resourceFile;
@ -385,6 +388,34 @@ void LCR_gameStartRun(unsigned int mapIndex)
LCR_gameResetRun(); LCR_gameResetRun();
} }
void LCR_gameEraseMenuItemNames(void)
{
for (int i = 0; i < LCR_MENU_MAX_ITEMS; ++i)
for (int j = 0; j < LCR_MENU_STRING_SIZE; ++j)
LCR_game.menuItemNames[i][j] = 0;
LCR_game.menuItemCount = 0;
}
void LCR_gameSetMenuItemStr(uint8_t item, const char *str)
{
for (int i = 0; i < LCR_MENU_STRING_SIZE - 1; ++i)
{
LCR_game.menuItemNames[item][i] = str[i];
LCR_game.menuItemNames[item][i + 1] = 0;
}
}
void LCR_gameLoadMainMenuItems(void)
{
for (int i = 0; i < 5; ++i)
LCR_gameSetMenuItemStr(i,LCR_texts[4 + i]);
LCR_game.menuItemCount = 5;
}
void LCR_gameInit(void) void LCR_gameInit(void)
{ {
LCR_LOG0("initializing"); LCR_LOG0("initializing");
@ -399,8 +430,10 @@ void LCR_gameInit(void)
LCR_game.resourceFile.state = 0; LCR_game.resourceFile.state = 0;
LCR_game.resourceFile.loadedItemCount = 0; for (int i = 0; i < LCR_MENU_MAX_ITEMS; ++i)
LCR_game.resourceFile.itemsTotal = 0; LCR_game.menuItemNamePointers[i] = LCR_game.menuItemNames[i];
LCR_gameLoadMainMenuItems();
LCR_game.menuSelectedTab = 0; LCR_game.menuSelectedTab = 0;
LCR_game.menuSelectedItem = 0; LCR_game.menuSelectedItem = 0;
@ -416,21 +449,20 @@ LCR_game.resourceFile.itemsTotal = 0;
LCR_currentMap.blockCount = 0; // means no map loaded LCR_currentMap.blockCount = 0; // means no map loaded
} }
/** /**
Loads up to LCR_RESOURCE_ITEM_CHUNK items of given type, starting at given Loads up to LCR_RESOURCE_ITEM_CHUNK items of given type, starting at given
index (among items of the same type). index (among items of the same type). This will also load the menu item
names.
*/ */
void LCR_gameLoadResourceFileChunk(unsigned int startIndex, char magicNumber) void LCR_gameLoadResourceFileChunk(unsigned int startIndex, char magicNumber)
{ {
LCR_LOG1("loading resources");
char c; char c;
unsigned char state = 0; unsigned char state = 0;
for (int i = 0; i < LCR_RESOURCE_ITEM_CHUNK * LCR_MENU_STRING_SIZE; ++i) LCR_gameEraseMenuItemNames();
LCR_game.resourceFile.loadedItemNames[i] = 0;
LCR_game.resourceFile.loadedItemCount = 0;
LCR_game.resourceFile.firstItemIndex = startIndex; LCR_game.resourceFile.firstItemIndex = startIndex;
LCR_game.resourceFile.itemsTotal = 0; LCR_game.resourceFile.itemsTotal = 0;
@ -473,14 +505,13 @@ for (int i = 0; i < LCR_RESOURCE_ITEM_CHUNK * LCR_MENU_STRING_SIZE; ++i)
state >= 1 + LCR_MENU_STRING_SIZE - 1) state >= 1 + LCR_MENU_STRING_SIZE - 1)
{ {
state = 255; state = 255;
LCR_game.resourceFile.loadedItemCount++; LCR_game.menuItemCount++;
if (LCR_game.resourceFile.loadedItemCount >= LCR_RESOURCE_ITEM_CHUNK) if (LCR_game.menuItemCount >= LCR_RESOURCE_ITEM_CHUNK)
break; break;
} }
else else
LCR_game.resourceFile.loadedItemNames[LCR_MENU_STRING_SIZE * LCR_game.menuItemNames[LCR_game.menuItemCount][state - 1] = c;
LCR_game.resourceFile.loadedItemCount + state - 1] = c;
state++; state++;
} }
@ -496,6 +527,9 @@ void LCR_gameEnd(void)
LCR_LOG0("ending"); LCR_LOG0("ending");
} }
void LCR_gameDraw3DView(void) void LCR_gameDraw3DView(void)
{ {
LCR_GameUnit carTransform[6]; LCR_GameUnit carTransform[6];
@ -581,7 +615,7 @@ void LCR_gameDraw3DView(void)
void LCR_gameHandleInput(void) void LCR_gameHandleInput(void)
{ {
int tabSwitchedTo = 0; int tabSwitchedTo = -1;
int scrolled = 0; int scrolled = 0;
switch (LCR_game.state) switch (LCR_game.state)
@ -625,8 +659,7 @@ void LCR_gameHandleInput(void)
if (LCR_game.menuSelectedItem < 4) if (LCR_game.menuSelectedItem < 4)
LCR_game.menuSelectedItem++; LCR_game.menuSelectedItem++;
} }
else if (LCR_game.menuSelectedItem < else if (LCR_game.menuSelectedItem < LCR_game.menuItemCount - 1)
LCR_game.resourceFile.loadedItemCount - 1)
LCR_game.menuSelectedItem++; LCR_game.menuSelectedItem++;
else if (LCR_game.resourceFile.firstItemIndex + else if (LCR_game.resourceFile.firstItemIndex +
LCR_RESOURCE_ITEM_CHUNK < LCR_game.resourceFile.itemsTotal) LCR_RESOURCE_ITEM_CHUNK < LCR_game.resourceFile.itemsTotal)
@ -687,11 +720,15 @@ void LCR_gameHandleInput(void)
break; break;
} }
if (tabSwitchedTo || scrolled != 0) if (tabSwitchedTo == 0)
LCR_gameLoadMainMenuItems();
else if (tabSwitchedTo > 0 || scrolled != 0)
LCR_gameLoadResourceFileChunk( LCR_gameLoadResourceFileChunk(
tabSwitchedTo ? 0 : (LCR_game.resourceFile.firstItemIndex + (tabSwitchedTo > 0) ? 0 : (LCR_game.resourceFile.firstItemIndex +
scrolled * LCR_RESOURCE_ITEM_CHUNK), scrolled * LCR_RESOURCE_ITEM_CHUNK),
LCR_game.menuSelectedTab == 1 ? 'M' : 'R'); LCR_game.menuSelectedTab == 1 ? 'M' : 'R');
} }
uint8_t LCR_gameStep(uint32_t time) uint8_t LCR_gameStep(uint32_t time)
@ -804,6 +841,8 @@ uint8_t LCR_gameStep(uint32_t time)
if (LCR_game.state == LCR_GAME_STATE_MENU) if (LCR_game.state == LCR_GAME_STATE_MENU)
{ {
/*
const char *items[LCR_MENU_MAX_ITEMS]; const char *items[LCR_MENU_MAX_ITEMS];
uint8_t itemCount = 1; uint8_t itemCount = 1;
@ -819,12 +858,15 @@ if (LCR_game.menuSelectedTab == 0)
else else
{ {
for (int i = 0; i < LCR_game.resourceFile.loadedItemCount; ++i) for (int i = 0; i < LCR_game.resourceFile.loadedItemCount; ++i)
items[i + 1] = LCR_game.resourceFile.loadedItemNames + i * LCR_MENU_STRING_SIZE; items[i + 1] = LCR_game.menuItemNames + i * LCR_MENU_STRING_SIZE;
itemCount = LCR_game.resourceFile.loadedItemCount + 1; itemCount = LCR_game.resourceFile.loadedItemCount + 1;
} }
*/
LCR_rendererDrawMenu(items,itemCount,LCR_game.menuSelectedItem); LCR_rendererDrawMenu(LCR_texts[LCR_game.menuSelectedTab],
LCR_game.menuItemNamePointers,LCR_game.menuItemCount + 1,
LCR_game.menuSelectedItem);
} }
else else
LCR_gameDraw3DView(); LCR_gameDraw3DView();

View file

@ -1705,8 +1705,8 @@ void LCR_rendererBlitImage(uint8_t index, unsigned int x, unsigned int y,
} }
} }
void LCR_rendererDrawMenu(const char **items, unsigned char itemCount, void LCR_rendererDrawMenu(const char *tabName,const char **items,
char selectedItem) unsigned char itemCount,char selectedItem)
{ {
int stripHeight = LCR_EFFECTIVE_RESOLUTION_Y / 4; int stripHeight = LCR_EFFECTIVE_RESOLUTION_Y / 4;
int stripHeight2 = LCR_EFFECTIVE_RESOLUTION_Y / 10; int stripHeight2 = LCR_EFFECTIVE_RESOLUTION_Y / 10;
@ -1750,16 +1750,18 @@ void LCR_rendererDrawMenu(const char **items, unsigned char itemCount,
i = stripHeight + i = stripHeight +
(stripHeight2 - LCR_rendererComputeTextHeight(3)) / 2; (stripHeight2 - LCR_rendererComputeTextHeight(3)) / 2;
for (int j = 0; j < itemCount; ++j) for (int j = 0; j < itemCount + 1; ++j)
{ {
const char *s = j ? items[j - 1] : tabName;
if (j == selectedItem + 1) if (j == selectedItem + 1)
for (int y = i - 10; y < i + LCR_rendererComputeTextHeight(3) + 10; ++y) for (int y = i - 10; y < i + LCR_rendererComputeTextHeight(3) + 10; ++y)
for (int x = LCR_EFFECTIVE_RESOLUTION_X / 4; for (int x = LCR_EFFECTIVE_RESOLUTION_X / 4;
x < LCR_EFFECTIVE_RESOLUTION_X - LCR_EFFECTIVE_RESOLUTION_X / 4; ++x) x < LCR_EFFECTIVE_RESOLUTION_X - LCR_EFFECTIVE_RESOLUTION_X / 4; ++x)
LCR_drawPixelXYSafe(x,y,0x5c1b + 4 * ((x & 0x10) == (y & 0x10))); LCR_drawPixelXYSafe(x,y,0x5c1b + 4 * ((x & 0x10) == (y & 0x10)));
LCR_rendererDrawText(items[j],(LCR_EFFECTIVE_RESOLUTION_X - LCR_rendererDrawText(s,(LCR_EFFECTIVE_RESOLUTION_X -
LCR_rendererComputeTextWidth(items[j],3)) / 2,i,0xffff,3); LCR_rendererComputeTextWidth(s,3)) / 2,i,0xffff,3);
if (j == 0) if (j == 0)
i = stripHeight + stripHeight2; i = stripHeight + stripHeight2;