Start resource files

This commit is contained in:
Miloslav Ciz 2024-12-30 00:49:41 +01:00
parent 69d87ab26d
commit c3dcd1ff0f
6 changed files with 154 additions and 11 deletions

119
game.h
View file

@ -79,6 +79,26 @@ void LCR_drawPixel(unsigned long index, uint16_t color);
*/
void LCR_log(const char *str);
/**
Implement this in your frontend. This function serves for loading optional
resource file that allows to add more maps, replays etc. If your frontend
won't support this, just make the function return 0. Otherwise it must return
characters from the resource file one by one; after reaching the end of file
0 must be returned and the reading position will be reset to start again.
*/
char LCR_getNextResourceFileChar(void);
/**
Implement this in your frontend. This serves to store data in the optional
resource file, e.g. replays. If your frontend doesn't support this (e.g.
because the file is read only), the function may ignore the append, but if
the file is otherwise supported, a rewind of the read position must still be
done. If appending is supported, the function must append the provided string
to the resource file AND then reset the resource file reading position back to
the start.
*/
void LCR_appendResourceStr(const char *str);
/**
Call this function in your frontend at the start of the program.
*/
@ -142,11 +162,13 @@ struct
uint8_t state;
uint32_t stateStartTime;
uint32_t time;
uint32_t frame;
uint32_t nextRenderFrameTime;
uint32_t nextRacingTickTime;
uint8_t controlMode;
uint8_t debugDraw;
uint8_t musicVolume;
int resourceFileState; ///< -1 if reading external res. f., else pos.
} LCR_game;
uint8_t LCR_gameGetMusicVolume(void)
@ -224,26 +246,103 @@ void LCR_gameResetRun(void)
LCR_gameSetState(LCR_GAME_STATE_RUN_STARTING);
}
int aaaa = 0; // tmp
char aaa(void)
void LCR_gameRewindResourceFile(void)
{
return LCR_maps[0][aaaa++];
LCR_appendResourceStr("");
LCR_game.resourceFileState = 0;
}
/**
Reads the next resource file character while merging the internal resource
file with the optional user file. First the internal file will be read,
immediately followed by the user file, then zero char will return and
reading will start over.
*/
char LCR_gameGetNextResourceFileChar(void)
{
#if LCR_SETTING_ENABLE_RESOURCE_FILE
char c;
if (LCR_game.resourceFileState < 0) // external file?
{
c = LCR_getNextResourceFileChar();
if (c == 0)
LCR_game.resourceFileState = 0; // move to internal file next
}
else // internal file
{
c = LCR_internalResourceFile[LCR_game.resourceFileState];
LCR_game.resourceFileState++;
if (c == 0)
{
c = LCR_getNextResourceFileChar();
LCR_game.resourceFileState = c ? -1 : 0; // trust this
}
}
return c;
#else
if (LCR_internalResourceFile[LCR_game.resourceFileState] == 0)
{
LCR_game.resourceFileState = 0;
return 0;
}
return LCR_internalResourceFile[LCR_game.resourceFileState++];
#endif
}
/**
Similar to LCR_gameGetNextResourceFileChar, but returns 0 instead of the
resource string separator character. This function is means to be used by
functions that load something from a string while expecting a zero terminated
string.
*/
char LCR_gameGetNextResourceStrChar(void)
{
char c = LCR_gameGetNextResourceFileChar();
return c != LCR_RESOURCE_FILE_SEPARATOR ? c : 0;
}
/**
Seeks to the Nth resource string in the global resource file, after its name,
so that the pure resource string will now be available for reading.
*/
void LCR_seekResourceByIndex(unsigned int index)
{
char c;
LCR_LOG0("seeking resource string");
LCR_gameRewindResourceFile();
while (index)
{
do
c = LCR_gameGetNextResourceFileChar();
while (c != LCR_RESOURCE_FILE_SEPARATOR && c != 0);
index--;
}
do // skip the name
c = LCR_gameGetNextResourceFileChar();
while (c != LCR_RESOURCE_FILE_SEPARATOR2 &&
c != LCR_RESOURCE_FILE_SEPARATOR && c != 0);
}
void LCR_gameStartRun(void)
{
LCR_mapLoadFromStr(aaa);
LCR_seekResourceByIndex(0); // TODO
LCR_mapLoadFromStr(LCR_gameGetNextResourceStrChar);
LCR_rendererLoadMap();
LCR_gameResetRun();
LCR_racingRestart();
}
void LCR_gameInit(void)
{
LCR_LOG0("initializing");
@ -255,6 +354,9 @@ void LCR_gameInit(void)
LCR_racingInit();
LCR_audioInit();
LCR_game.resourceFileState = 0;
LCR_game.frame = 0;
LCR_game.musicVolume = 255;
LCR_game.nextRenderFrameTime = 0;
LCR_game.nextRacingTickTime = 0;
@ -444,6 +546,7 @@ LCR_audioSetEngineIntensity((2 * val) < 256 ? (2 * val) : 255);
LCR_LOG1("can't sleep, frames take too long!");
}
LCR_game.frame++;
LCR_LOG2("game step end");
return 1;