borgware-2d/games/tetris/highscore.c

103 lines
2.0 KiB
C
Raw Normal View History

2010-08-22 03:07:46 +00:00
#include <stdio.h>
2010-08-28 15:13:35 +00:00
#include <string.h>
2010-08-24 23:00:40 +00:00
#include <stdint.h>
2011-03-07 00:32:38 +00:00
#include <limits.h>
2010-08-22 03:07:46 +00:00
#include "../../config.h"
#include "../../scrolltext/scrolltext.h"
#include "../../joystick/joystick.h"
#include "../../compat/eeprom.h"
2010-08-28 15:13:35 +00:00
#include "highscore.h"
2010-08-22 03:07:46 +00:00
tetris_highscore_table_t g_highScoreTable EEMEM;
2010-08-22 03:07:46 +00:00
uint16_t tetris_highscore_inputName(void)
{
#ifdef SCROLLTEXT_SUPPORT
2011-01-30 22:47:44 +00:00
char pszNick[4] = {'A', 'A', 'A', '\0'};
char pszTmp[26];
uint8_t nPos = 0, nBlink = 0, nHadfire = 0;
2010-08-22 03:07:46 +00:00
2011-01-30 22:47:44 +00:00
while (1)
2010-08-22 03:07:46 +00:00
{
// we need our own blink interval
2011-03-07 00:32:38 +00:00
nBlink = (nBlink + 1) % 4u;
2010-08-22 03:07:46 +00:00
// construct command for scrolltext and execute
2011-01-30 22:47:44 +00:00
static uint8_t const nOffset[3] = {15, 19, 23};
2010-08-22 03:07:46 +00:00
snprintf(pszTmp, sizeof(pszTmp), "x%u+p1#%c#x%u+p1#%c#x%up1#%c",
2011-01-30 22:47:44 +00:00
nOffset[nPos] , (!nBlink && nPos == 0) ? ' ' : pszNick[0],
nOffset[nPos] - 8, (!nBlink && nPos == 1) ? ' ' : pszNick[1],
nOffset[nPos] - 15, (!nBlink && nPos == 2) ? ' ' : pszNick[2]);
2010-08-22 03:07:46 +00:00
scrolltext(pszTmp);
// up and down control current char
if (JOYISUP)
{
pszNick[nPos]++;
if (pszNick[nPos] == '`')
{
pszNick[nPos] = 'A';
}
else if (pszNick[nPos] == '[')
{
pszNick[nPos] = '_';
}
}
else if (JOYISDOWN)
{
pszNick[nPos]--;
if (pszNick[nPos] == '@')
{
pszNick[nPos] = '_';
}
else if (pszNick[nPos] == '^')
{
pszNick[nPos] = 'Z';
}
}
2011-01-30 22:47:44 +00:00
2010-08-22 03:07:46 +00:00
// left and right control char selections
2011-01-30 22:47:44 +00:00
if (JOYISLEFT && nPos > 0)
2010-08-22 03:07:46 +00:00
{
nPos--;
}
else if (JOYISRIGHT && nPos < 2)
{
nPos++;
}
// fire switches to next char or exits
if (JOYISFIRE && !nHadfire)
{
nHadfire = 1;
2011-01-30 22:47:44 +00:00
if (nPos++ == 2)
2010-08-22 03:07:46 +00:00
{
break;
}
}
2011-01-30 22:47:44 +00:00
if (!JOYISFIRE)
2010-08-22 03:07:46 +00:00
{
nHadfire = 0;
}
}
// return result
return (pszNick[0] - 65) << 10 | (pszNick[1] - 65) << 5 | (pszNick[2] - 65);
#else
return (0);
#endif
}
uint16_t tetris_highscore_retrieveHighScore(tetris_highscore_index_t nIndex)
2010-08-22 03:07:46 +00:00
{
uint16_t nHighScore =
eeprom_read_word(&g_highScoreTable.nHighScore[nIndex]);
2010-08-22 03:07:46 +00:00
// a score of 65535 is most likely caused by uninitialized EEPROM addresses
return nHighScore == UINT16_MAX ? 0 : nHighScore;
2010-08-22 03:07:46 +00:00
}