Maelstrom: Preload more of our data while the splash screen is up.

From ce376e38913d4fc4f1f472a471dd40be621ef1a4 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 23 Oct 2011 07:09:47 -0400
Subject: [PATCH] Preload more of our data while the splash screen is up. Fixed
 issue with fading into the main menu.

---
 init.cpp                   | 83 +++++++++++++++++---------------------
 main.cpp                   |  1 -
 screenlib/SDL_FrameBuf.cpp |  2 +-
 screenlib/UIManager.cpp    | 10 ++++-
 screenlib/UIManager.h      |  2 +-
 5 files changed, 47 insertions(+), 51 deletions(-)

diff --git a/init.cpp b/init.cpp
index ac2a9ca7..bfe4a4b3 100644
--- a/init.cpp
+++ b/init.cpp
@@ -62,37 +62,6 @@ static int LoadSmallSprite(Mac_Resource *spriteres,
 /* Put up an Ambrosia Software splash screen */
 void DoSplash(void)
 {
-	UIPanel *panel;
-	Uint32 start;
-	int i;
-
-	screen->FadeOut();
-	screen->Clear();
-
-	panel = ui->LoadPanel(PANEL_SPLASH);
-	if (panel) {
-		ui->ShowPanel(panel);
-		ui->Draw();
-	}
-	screen->Update();
-
-	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;
-		}
-		SDL_Delay(100);
-	}
-
-	if (panel) {
-		ui->DeletePanel(panel);
-	}
 }
 
 /* ----------------------------------------------------------------- */
@@ -111,10 +80,7 @@ static void DrawLoadBar(int stage)
 		fact = (FULL_WIDTH * stage) / MAX_BAR;
 		progress->SetWidth(fact);
 	}
-
-	screen->Clear();
 	ui->Draw();
-	screen->Update();
 }	/* -- DrawLoadBar */
 
 
@@ -677,6 +643,8 @@ void CleanUp(void)
 /* -- Perform some initializations and report failure if we choke */
 int DoInitializations(Uint32 window_flags, Uint32 render_flags)
 {
+	UIPanel *panel;
+	Uint32 start;
 	int i;
 	SDL_Surface *icon;
 
@@ -772,14 +740,45 @@ int DoInitializations(Uint32 window_flags, Uint32 render_flags)
 	screen->ClipBlit(&gClipRect);
 
 	/* Do the Ambrosia Splash screen */
-	DoSplash();
+	screen->FadeOut();
+	ui->LoadPanel(PANEL_SPLASH);
+	ui->ShowPanel(PANEL_SPLASH);
+	ui->Draw();
+
+	start = SDL_GetTicks();
+
+	/* Preload some of our data while the splash screen is up */
+
+	/* -- Load in the prize CICN's */
+	if ( LoadCICNS() < 0 )
+		return(-1);
+
+	/* -- Load the rest of the UI panels */
+	if (!ui->LoadPanels()) {
+		error("Couldn't load panels: %s\n", ui->Error());
+		return(-1);
+	}
+
+	/* -- Create the stars array */
+	InitStars();
+
+	/* -- Set up the velocity tables */
+	BuildVelocityTable();
+
+	/* Wait for the splash time to finish, or a keypress */
+	while ((SDL_GetTicks() - start) < 5000) {
+		if ( DropEvents() ) {
+			break;
+		}
+		SDL_Delay(100);
+	}
+
+	ui->DeletePanel(PANEL_SPLASH);
 
 	/* -- Throw up our intro screen */
 	ui->ShowPanel(PANEL_LOADING);
 	sound->PlaySound(gPrizeAppears, 1);
-	screen->Clear();
 	ui->Draw();
-	screen->Update();
 
 	/* -- Load in our sprites and other needed resources */
 	{
@@ -801,16 +800,6 @@ int DoInitializations(Uint32 window_flags, Uint32 render_flags)
 	if ( InitSprites() < 0 )
 		return(-1);
 
-	/* -- Load in the prize CICN's */
-	if ( LoadCICNS() < 0 )
-		return(-1);
-
-	/* -- Create the stars array */
-	InitStars();
-
-	/* -- Set up the velocity tables */
-	BuildVelocityTable();
-
 	ui->DeletePanel(PANEL_LOADING);
 
 	return(0);
diff --git a/main.cpp b/main.cpp
index 2b5f5d6c..b7884f8d 100644
--- a/main.cpp
+++ b/main.cpp
@@ -302,7 +302,6 @@ int main(int argc, char *argv[])
 
 	gRunning = true;
 	sound->PlaySound(gNovaBoom, 5);
-	screen->Fade();		/* Fade-out */
 	Delay(SOUND_DELAY);
 	gUpdateBuffer = true;
 	while ( sound->Playing() )
diff --git a/screenlib/SDL_FrameBuf.cpp b/screenlib/SDL_FrameBuf.cpp
index 9e6f1b05..a190e6c9 100644
--- a/screenlib/SDL_FrameBuf.cpp
+++ b/screenlib/SDL_FrameBuf.cpp
@@ -131,7 +131,7 @@ void
 FrameBuf:: Fade(void)
 {
 // Temporary for development
-return;
+//return;
 	const int max = 32;
 	Uint16 ramp[256];   
 
diff --git a/screenlib/UIManager.cpp b/screenlib/UIManager.cpp
index 01ef1fa2..d042b683 100644
--- a/screenlib/UIManager.cpp
+++ b/screenlib/UIManager.cpp
@@ -165,6 +165,7 @@ UIManager::ShowPanel(UIPanel *panel)
 		m_visible.add(panel);
 		panel->Show();
 		if (panel->IsFullscreen()) {
+			Draw();
 			m_screen->FadeIn();
 		}
 	}
@@ -181,6 +182,7 @@ UIManager::HidePanel(UIPanel *panel)
 			for (unsigned int i = m_visible.length(); i--; ) {
 				if (m_visible[i]->IsFullscreen()) {
 					m_visible[i]->Show();
+					Draw();
 					m_screen->FadeIn();
 					break;
 				}
@@ -200,8 +202,11 @@ UIManager::DeletePanel(UIPanel *panel)
 }
 
 void
-UIManager::Draw()
+UIManager::Draw(bool fullUpdate)
 {
+	if (fullUpdate) {
+		m_screen->Clear();
+	}
 	for (unsigned i = 0; i < m_visible.length(); ++i) {
 		UIPanel *panel = m_visible[i];
 
@@ -211,6 +216,9 @@ UIManager::Draw()
 			break;
 		}
 	}
+	if (fullUpdate) {
+		m_screen->Update();
+	}
 }
 
 bool
diff --git a/screenlib/UIManager.h b/screenlib/UIManager.h
index 6578345b..76591797 100644
--- a/screenlib/UIManager.h
+++ b/screenlib/UIManager.h
@@ -76,7 +76,7 @@ class UIManager : public UIArea
 		DeletePanel(GetPanel(name));
 	}
 
-	void Draw();
+	void Draw(bool fullUpdate = true);
 	bool HandleEvent(const SDL_Event &event);
 
 protected: