Author Topic: Splash Screen while loading  (Read 8342 times)

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Splash Screen while loading
« on: 2009-03-12, 09:07:42 AM »
Hi guys, I've got another question for you.  (I hope I'm not getting on your nerves, I'm asking a lot of stuff lately.)  :-[

Is there a way to show a splash screen while loading a sgdk compiled project?  It takes a while to bring the game up and a "please wait" custom splash screen (or something like that) would help a lot to keep users from wondering if the game is loading or has crashed. 

I looked in the forums, but all mention of splash screens refer to the SGDK2 splash screen.  I also looked into the help file, but couldn't find anything.  Does this functionality exist?  What must be done to make this work?

Thanks!  :D
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Splash Screen while loading
« Reply #1 on: 2009-03-12, 11:38:41 AM »
I don't think so.  It just takes a while the first time you start up a .NET-based program because I think it has to compile it for the current CPU.  I assume SGDK2 behaves the same way -- takes a while before the splash screen appears.  If not, then maybe there's something in the initialization of your project that's doing a lot of work... like generating collision masks for many or large sprites.  You could try adding a message box to the beginning of the main function (I don't have SGDK2 handy so I don't remember exactly where that is -- maybe in GameProject.cs or Project.cs).  System.windows.Forms.MessageBox.Show("Test").  If you add that at the beginning of the main function, that's the very first code that executes, and the earliest your splash screen could possibly appear.  If there's a noticeable period of time after you close the message and before the game begins, then there's cause for a splash screen.

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: Splash Screen while loading
« Reply #2 on: 2009-03-12, 11:41:42 AM »
Great!  I'll try this tonight! :)

Thanks!
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: Splash Screen while loading
« Reply #3 on: 2009-03-12, 04:27:23 PM »
Well, there is indeed a long time between the splash message box and the loading of the game.  I guess I'm going to make a splash screen after all. :)
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Splash Screen while loading
« Reply #4 on: 2009-03-12, 04:38:28 PM »
Are you interested in narrowing down where it's taking a long time so that it can maybe be optimized?  If it's taking a long time to initialize a particular map, you could just create a splash screen in the form of a simple map that appears before switching to the more slowly-loading map.  And maybe it would be possible to change something to run more efficiently if we know what's taking so long.

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: Splash Screen while loading
« Reply #5 on: 2009-03-12, 05:13:57 PM »
Well, it is obvious that there are large graphics and big sprites in my map.    :-[

 The line of code that is taking a long tmie to run is this one:

Code: [Select]
Project.game = new GameForm(mode, windowed, windowTitle, startupMapType, overlayMapType);
I placed a show message between each line of code in the main to narrow down where the bottleneck was.
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Splash Screen while loading
« Reply #6 on: 2009-03-13, 06:50:54 AM »
If you change your startup map to a simpler map, you might be able to get something displayed sooner; then you could switch to the real startup map while displaying something friendly on the startup map.

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: Splash Screen while loading
« Reply #7 on: 2009-03-13, 07:17:52 AM »
Well, I added a form to the project to do the Splashscreen.  I don't want to bother myself with other maps for the moment.  I will get there eventually, but I want to focus my work on the mechanics of the game.  :)

Here is the code I added to GameForm.cs to display a splash screen.  This part was added outside of the gameform class (of course).

Code: [Select]
///********************************************************************
///**************************added by Vincent**************************
///*******************************Begin********************************
/// <summary>
/// Summary description for Splash.
/// </summary>
public class Splash : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
    static Splash ms_frmSplash = null;
    static Thread ms_oThread = null;

public Splash()
{
InitializeComponent();

//Bitmap b = new Bitmap(this.BackgroundImage);
//b.MakeTransparent(b.GetPixel(320,1));
//this.TransparencyKey = b.GetPixel(320,1);
//this.BackgroundImage = b;
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

   private static void ShowForm()
   {
     ms_frmSplash = new Splash();
     Application.Run(ms_frmSplash);
   }
   // A static method to close the SplashScreen

   public static void CloseForm()
   {
      ms_oThread = null;  // we do not need these any more.
      ms_frmSplash.Hide();
      ms_frmSplash = null;
   }

   static public void ShowSplashScreen()
   {
      // Make sure it is only launched once.

      if( ms_frmSplash != null )
         return;
      ms_oThread = new Thread( new ThreadStart(Splash.ShowForm));
      ms_oThread.IsBackground = true;
      ms_oThread.ApartmentState = ApartmentState.STA;
      ms_oThread.Start();
   }

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Splash));
//
// Splash
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);     
this.BackgroundImage = new Bitmap(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "LokRevival.bmp"), true);
this.ClientSize = new System.Drawing.Size(640, 142);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Splash";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Splash";
this.TransparencyKey = System.Drawing.Color.Blue;
      this.ShowInTaskbar = false;
}
}
///********************************End*********************************
///**************************added by Vincent**************************
///********************************************************************

This part was added into the Project.cs file.  I modified the main like this:

Code: [Select]
public static void Main()
   {
      try
      {
         ///********************************************************************
         ///**************************added by Vincent**************************
         ///*******************************Begin********************************
         Splash.ShowSplashScreen();
         ///********************************End*********************************
         ///**************************added by Vincent**************************
         ///********************************************************************
         GameDisplayMode mode = (GameDisplayMode)System.Enum.Parse(typeof(GameDisplayMode), m_res.GetString("_DisplayMode"));
         bool windowed = bool.Parse(m_res.GetString("_Windowed"));
         string windowTitle = m_res.GetString("_WindowTitle");
         System.Type startupMapType = System.Reflection.Assembly.GetExecutingAssembly().GetType(m_res.GetString("_StartupMapType").Replace(" ","_") + "_Map", true);
         System.Type overlayMapType = null;
         if (m_res.GetString("_OverlayMapType") != null)
             overlayMapType = System.Reflection.Assembly.GetExecutingAssembly().GetType(m_res.GetString("_OverlayMapType").Replace(" ","_") + "_Map", true);

         Project.game = new GameForm(mode, windowed, windowTitle, startupMapType, overlayMapType);
         ///********************************************************************
         ///**************************added by Vincent**************************
         ///*******************************Begin********************************
         Splash.CloseForm();
         ///********************************End*********************************
         ///**************************added by Vincent**************************
         ///********************************************************************
         game.Show();
         game.Run();
      }
      catch (System.Exception ex)
      {
         GameForm.HandleException(ex);
      }
   }

So, there is really only 2 new lines of code into the main.  Showing the splash screen and removing it.

As you can see, I am using a separate thread to display the splashscreen, so it should not slowdown the rest of the loading process.

It can easily be used by anyone.  Just change the name of the image file.  Change the size of the Splash form to fit the image.  And, of course, the image must be present in the folder of the exe.

I hope it can be useful to someone! :)

Thanks again bluemonkmn! :D
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Splash Screen while loading
« Reply #8 on: 2009-03-13, 07:52:26 AM »
I wonder if putting it in a separate thread is really necessary.  I mean, the creation of a form and displaying an image only takes a tiny fraction of a second, so there shouldn't be any need to make another thread.  When you introduce another thread, you may end up making things slower since you need to context-switch, whereas if you do it synchronously, it just runs right through.

BlueMonk was suggesting that you create a new map because the line you specified as being slow is where it loads the first map of the game, which is why it's so slow.  If you do put in a simple startup map, then your game will appear to load a lot faster, and shouldn't need a splash screen.  This map can either be a splashscreen, if you really want one, or just the text "loading", or a level select, or a main menu (new game/load game, etc), or even just an image of what your real first map looks like, so the map appears to load right away, which will then be replaced by your real map when it loads.

Most games don't use splash screens any way; it's mostly an application thing.  So be sure it's really the best solution before you... decide to leave it in, since it's already there, I guess.
Edward Dassmesser

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: Splash Screen while loading
« Reply #9 on: 2009-03-13, 08:10:56 AM »
Thanks for your input durnurd. :)

I'll leave things as they are for the moment.  When I gent to the creation of the main menu, I will come back to this to verify if it is still necessary to have a splash screen.  I guess the main menu will be much faster anyway...

Oh, about the threading, I thought that if I showed the splash screen form in the same thread, the loading would have been interrupted until the splash screen was closed.  Is there a way to show it and then hide it without passing by another thread, without interrupting the loading process?

Thanks :)
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Splash Screen while loading
« Reply #10 on: 2009-03-16, 05:08:45 AM »
You didn't tell it to close the splash screen until after the loading was complete, so it will leave the splash screen visible the whole time and it won't interfere with the loading at all, even on the same thread.  It's not doing anything; just sitting there.  Threads are almost never necessary, and they often introduce more problems than they solve, especially if you don't have a college degree in computer science ;), so I suggest you avoid them.  SGDK2 doesn't do any multi-threading directly (although I think FMOD may run sounds on a separate thread).

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: Splash Screen while loading
« Reply #11 on: 2009-03-16, 10:03:59 AM »
Hi guys :)

I don't think I follow you bluemonkmn.  The splashscreen is not visible after the loading: I'm calling this function in the project main :

Code: [Select]
Splash.CloseForm();it hides the splash form. 

The thread is not closed, but since it is launched by the main thread, it is supposed to be terminated by the main thread when the application finishes.  It guess I could "kill" the splash form as well by calling a Thread.Abort(), but it seemed a little too much, since the splashform is so light and that it gets killed anyway as the game closes.

Who told you I don't have a college degree in computer science? ;)  I might have one... :educated:
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Splash Screen while loading
« Reply #12 on: 2009-03-16, 01:24:40 PM »
What he's saying is that the splash screen will remain visible while loading, and won't be hidden until after your game completes loading, even if you don't use threads.

Splash.ShowForm is a non-blocking call, so the control returns to your code immediately, even if the form is still showing.  Then your game continues to load, while the splashscreen is showing.  Once the game is done loading, your code calls Splash.CloseForm, and that's when it disappears, threaded or not.
Edward Dassmesser

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: Splash Screen while loading
« Reply #13 on: 2009-03-16, 01:35:01 PM »
Oh!  Thanks durnurd, I understand now. :)
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Splash Screen while loading
« Reply #14 on: 2009-03-16, 01:56:18 PM »
Who told you I don't have a college degree in computer science? ;)  I might have one... :educated:

If you had a college degree in computer science, you'd be as scared of threads as I am :).