Maelstrom: Save the handle to the preferences and use it next time.

https://github.com/libsdl-org/Maelstrom/commit/2a4e96694337237379f19742f13ee16e4295f007

From 2a4e96694337237379f19742f13ee16e4295f007 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 4 Nov 2011 21:46:55 -0400
Subject: [PATCH] Save the handle to the preferences and use it next time. Also
 don't save a high score if a name isn't entered, since that's usually a
 mistake.

---
 netlogic/game.cpp | 42 ++++++++++++++++++++++++------------------
 1 file changed, 24 insertions(+), 18 deletions(-)

diff --git a/netlogic/game.cpp b/netlogic/game.cpp
index acc97151..5ed528db 100644
--- a/netlogic/game.cpp
+++ b/netlogic/game.cpp
@@ -29,8 +29,10 @@
 #include "game.h"
 #include "../screenlib/UIElement.h"
 
+#define PREFERENCES_HANDLE "Handle"
 
 // Global variables set in this file...
+int	gScore;
 int	gGameOn;
 int	gPaused;
 int	gWave;
@@ -890,9 +892,10 @@ static void DoGameOver(void)
 		Delay(SOUND_DELAY);
 
 	/* -- See if they got a high score */
+	gScore = OurShip->GetScore();
 	LoadScores();
 	for ( i = 0; i<10; ++i ) {
-		if ( OurShip->GetScore() > hScores[i].score ) {
+		if ( gScore > hScores[i].score ) {
 			which = i;
 			break;
 		}
@@ -904,33 +907,28 @@ static void DoGameOver(void)
 	if ((which != -1) && (gStartLevel == 1) && (gStartLives == 3) &&
 					(gNumPlayers == 1) && !gDeathMatch ) {
 		sound->PlaySound(gBonusShot, 5);
-		for ( i = 8; i >= which ; --i ) {
-			hScores[i + 1].score = hScores[i].score;
-			hScores[i + 1].wave = hScores[i].wave;
-			strcpy(hScores[i+1].name, hScores[i].name);
+
+		/* Get the previously used handle, if possible */
+		const char *text = prefs->GetString(PREFERENCES_HANDLE);
+		if (text) {
+			SDL_strlcpy(handle, text, sizeof(handle));
+		} else {
+			*handle = '\0';
 		}
+		chars_in_handle = SDL_strlen(handle);
 
 		/* -- Let them enter their name */
-		const char *text = NULL;
 		label = panel->GetElement<UIElement>("name_label");
 		if (label) {
 			label->Show();
 		}
 		label = panel->GetElement<UIElement>("name");
 		if (label) {
-			text = label->GetText();
+			label->SetText(handle);
 			label->Show();
 		}
 		ui->Draw();
 
-		/* Get the previously used handle, if possible */
-		if (text) {
-			SDL_strlcpy(handle, text, sizeof(handle));
-		} else {
-			handle[0] = '\0';
-		}
-		chars_in_handle = SDL_strlen(handle);
-
 		while ( screen->PollEvent(&event) ) /* Loop, flushing events */;
 		SDL_StartTextInput();
 		while ( label && !done ) {
@@ -971,9 +969,17 @@ static void DoGameOver(void)
 		}
 		SDL_StopTextInput();
 
-		hScores[which].wave = gWave;
-		hScores[which].score = OurShip->GetScore();
-		strcpy(hScores[which].name, handle);
+		if (*handle) {
+			for ( i = 8; i >= which ; --i ) {
+				hScores[i + 1].score = hScores[i].score;
+				hScores[i + 1].wave = hScores[i].wave;
+				strcpy(hScores[i+1].name, hScores[i].name);
+			}
+			hScores[which].wave = gWave;
+			hScores[which].score = OurShip->GetScore();
+			strcpy(hScores[which].name, handle);
+			prefs->SetString(PREFERENCES_HANDLE, handle);
+		}
 
 		sound->HaltSound();
 		sound->PlaySound(gGotPrize, 6);