Maelstrom: Added a step to load panels while the splash logo is being shown

From c2e3197d34ddb16bc1b020925287b986abf6cf2c Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 23 Oct 2011 03:54:33 -0400
Subject: [PATCH] Added a step to load panels while the splash logo is being
 shown

---
 Maelstrom_Globals.h     |  9 ++++++
 UI/UI.lst               |  1 +
 init.cpp                | 14 +++++++--
 screenlib/UIManager.cpp | 63 +++++++++++++++++++++++++++++++++++++++++
 screenlib/UIManager.h   |  1 +
 5 files changed, 85 insertions(+), 3 deletions(-)
 create mode 100644 UI/UI.lst

diff --git a/Maelstrom_Globals.h b/Maelstrom_Globals.h
index ee90ee24..62062bb4 100644
--- a/Maelstrom_Globals.h
+++ b/Maelstrom_Globals.h
@@ -88,6 +88,15 @@ extern int	gNumSprites;
 // in scores.cc :
 extern Bool	gNetScores;
 
+// UI panel definitions...
+#define PANEL_SPLASH	"splash"
+#define PANEL_LOADING	"loading"
+#define PANEL_MAIN	"main"
+#define PANEL_GAME	"game"
+#define PANEL_GAMEOVER	"gameover"
+#define PANEL_ABOUT	"about"
+#define PANEL_CONTROLS	"controls"
+
 // Sound resource definitions...
 #define gShotSound	100
 #define gMultiplier	101
diff --git a/UI/UI.lst b/UI/UI.lst
new file mode 100644
index 00000000..2534a7a6
--- /dev/null
+++ b/UI/UI.lst
@@ -0,0 +1 @@
+# List of panels to load at startup
diff --git a/init.cpp b/init.cpp
index 89dfeb9a..3ac6fdff 100644
--- a/init.cpp
+++ b/init.cpp
@@ -65,23 +65,31 @@ static int LoadSmallSprite(Mac_Resource *spriteres,
 void DoSplash(void)
 {
 	UIPanel *panel;
+	Uint32 start;
 	int i;
 
 	screen->FadeOut();
 	screen->Clear();
 
-	panel = ui->LoadPanel("splash");
+	panel = ui->LoadPanel(PANEL_SPLASH);
 	if (panel) {
 		ui->ShowPanel(panel);
 		ui->Draw();
 	}
 	screen->Update();
 
-	for ( i=0; i<5; ++i ) {
+	start = SDL_GetTicks();
+
+	/* Load the UI panels while we're showing the splash screen */
+	if (!ui->LoadPanels()) {
+		fprintf(stderr, "Warning: Couldn't load panels: %s\n", ui->Error());
+	}
+
+	while ((SDL_GetTicks() - start) < 5000) {
 		if ( DropEvents() ) {
 			break;
 		}
-		Delay(60);
+		SDL_Delay(100);
 	}
 
 	if (panel) {
diff --git a/screenlib/UIManager.cpp b/screenlib/UIManager.cpp
index 8387d9b8..924d9ece 100644
--- a/screenlib/UIManager.cpp
+++ b/screenlib/UIManager.cpp
@@ -20,6 +20,8 @@
     slouken@libsdl.org
 */
 
+#include <physfs.h>
+
 #include "SDL_FrameBuf.h"
 #include "UIManager.h"
 #include "UIPanel.h"
@@ -48,6 +50,67 @@ UIManager::SetLoadPath(const char *path)
 	strcpy(m_loadPath, path);
 }
 
+static const char *GetLine(char *&text)
+{
+	while (*text == '\r' || *text == '\n') {
+		++text;
+	}
+	if (!*text) {
+		return NULL;
+	}
+
+	const char *line = text;
+	while (*text && *text != '\r' && *text != '\n') {
+		++text;
+	}
+	if (*text) {
+		*text++ = '\0';
+	}
+	return line;
+}
+
+bool
+UIManager::LoadPanels()
+{
+	char file[1024];
+	PHYSFS_File *fp;
+	PHYSFS_sint64 size;
+	char *buffer, *spot;
+	const char *line;
+
+	sprintf(file, "%s/UI.lst", m_loadPath);
+	fp = PHYSFS_openRead(file);
+	if (!fp) {
+		SetError("Couldn't open %s: %s", file, PHYSFS_getLastError());
+		return false;
+	}
+
+	size = PHYSFS_fileLength(fp);
+	buffer = new char[size+1];
+	if (PHYSFS_readBytes(fp, buffer, size) != size) {
+		SetError("Couldn't read from %s: %s", file, PHYSFS_getLastError());
+		PHYSFS_close(fp);
+		delete[] buffer;
+		return false;
+	}
+	buffer[size] = '\0';
+	PHYSFS_close(fp);
+
+	spot = buffer;
+	while ((line = GetLine(spot)) != NULL) {
+		if (*line == '#') {
+			continue;
+		}
+		if (!LoadPanel(line)) {
+			delete[] buffer;
+			return false;
+		}
+	}
+
+	delete[] buffer;
+	return true;
+}
+
 UIPanel *
 UIManager::LoadPanel(const char *name)
 {
diff --git a/screenlib/UIManager.h b/screenlib/UIManager.h
index 1f1b8d63..ce765678 100644
--- a/screenlib/UIManager.h
+++ b/screenlib/UIManager.h
@@ -47,6 +47,7 @@ class UIManager : public UIArea
 	}
 
 	void SetLoadPath(const char *path);
+	bool LoadPanels();
 	UIPanel *LoadPanel(const char *name);
 	UIPanel *GetPanel(const char *name);