Start with black background instead of white

How do I change the game window background at startup?
I don’t mean in the draw function. I mean at the very beginning when the game runs, before the draw occurs.
There is usually a small window with a white background before the game class is called. Which means that I need to change the window features in the program class.
I noticed an attribute [STAThread] which looks similar to this line of code

[SWF(backgroundColor="#000000", width="800", height="600", frameRate="60")] 

which is used in flash, to set the window feature at start up. Can I use something similar in C#?
How do I go about setting the window parameters?

Which MonoGame platform are you using? Windows? WindowsGL? Mac? Linux?

It’s most certainly not STAThread, that attribute tells .NET what threading model to use, specifically Single Threaded Apartment model.

That window is going to be created on an operating system by operating system basis, for example on Windows it’s ultimate a Windows.Form object ( https://github.com/mono/MonoGame/blob/develop/MonoGame.Framework/Windows/WinFormsGameForm.cs ), you could set the WinForm background color here easily. Not much to be gained from it though, and you would have to implement it for each project and build from source to benefit.

You could potentially hook it to get the winform handle out and change the color without building from source, but at that point, the window would already be created and you might as well use draw().

Which MonoGame platform are you using? Windows?

I’m developing for WindowsOpenGL, on Windows 8.1 using Visual Studio 2013.

you could set the WinForm background color here easily

I’m not sure this will work in the entry point, since I understand that Forms should be used after initialization.

Can someone help me with this please.
Go here and see how bad my game start looks.
It’s terrible. I can’t make a game looking like this.
I need help.

did you have device.Clear(Color.White);" somewhere?
change it to device.Clear(Color.Black);
In monogame you have to clear the background on every frame.

His problem is pre draw(). Im guessing he is doing some processing before the event loop is fired off resulting in the pre cleared window being shown longer

Again he could fix it by modifying the form code in the library itself, but that would probably need to be done on a platform by platform basis. He could also streamline is startup procedure so the first draw() call happens earlier.

The window for the WindowsGL platform is created by OpenTK, and we have very little control over how it is created. To change the background colour of that window before we start drawing requires intercepting the WM_ERASEBKGND message and using the DC in the message’s wParam member to use with a call to the Win32 API FillRect() with a black brush.

Loading lots of content in Game.LoadContent() will delay the first call to Game.Draw(). We used to avoid this delay in Windows Phone 7 by drawing the splash screen directly in Game.Initialize() after the call to base.Initialize().

The other way to avoid the long stall is to load the content asynchronously. In your Game class, add a field

volatile bool _loading;

in Game.LoadContent(), set the _loading field to true and spawn a thread to load the actual content.

protected override void LoadContent()
{
    _loading = true;
    Task.Factory.StartNew(() => { LoadContentThread(); });
}

void LoadContentThread()
{
    // Make calls to Content.Load here
    // ...

    // Let the Update/Draw loop know we're done loading
    _loading = false;
}

This will now continue on with the normal Update/Draw loop while the content loads in the background. In Game.Update and Draw, check the _loading field. If it is true, do no updates and draw a black screen.

protected override void Update(GameTime gameTime)
{
    if (_loading)
        return;
    // The rest of your normal update
    // ...
}

protected override void Draw(GameTime gameTime)
{
    if (_loading)
    {
        GraphicsDevice.Clear(Color.Black);
        return;
    }
    // The rest of your normal draw
    // ...
}

If you want to get tricky, you can draw a simple loading bar or spinning icon after the GraphicsDevice.Clear() to let the user know something is happening.

Just so that you can get a better picture, and see that I am not doing anything different, I took out everything else. Now I am left with just this. That black screen pops out of nowhere. I’d be happy if the white screen just vanished too.

using System;
//using System.Windows.Forms;

namespace Game1_WindowsOpenGL
{
#if WINDOWS || LINUX
    /// <summary>
    /// The main class.
    /// </summary>
    public static class Program
    {
        // The main entry point for the application.
        [STAThread]
        static void Main()
        {
            using (var game = new Game1()) {game.Run(); }
        }
    }
#endif
}

// Game1.cs
using System; // get TimeSpan
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using FarseerPhysics;

// my Classes 
using myScreenSys;

namespace Game1_WindowsOpenGL
{
    // This is the main type for your game.
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public Game1()
        {
            //Window.Title = "JaguarCreations - Testing Farseer";
            //Window.Position = new Point(80, 5);
            graphics = new GraphicsDeviceManager(this);
            //graphics.PreferMultiSampling = true;

#if WINDOWS || XBOX 
            //graphics.PreferredBackBufferWidth = 1200;
            //graphics.PreferredBackBufferHeight = 720;
            ////ConvertUnits.SetDisplayUnitToSimUnitRatio(24f); // new class - ConvertUnits (farseer member)
            //IsFixedTimeStep = true;
#elif WINDOWS_PHONE
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 480;
            ConvertUnits.SetDisplayUnitToSimUnitRatio(16f);
            IsFixedTimeStep = false;
#endif
#if WINDOWS 
            graphics.IsFullScreen = false;
#elif XBOX || WINDOWS_PHONE
            graphics.IsFullScreen = true;
#endif

            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// game-specific content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}

Have a look again.
I’m wondering if it’s a graphics card issue, since this is an OpenGL project.

…he could fix it by modifying the form code in the library itself,…
He could also streamline is startup procedure so the first draw() call happens earlier.

Could you explain these a bit. I haven’t the slightest idea what these mean.

Konaju just explained exactly what I meant.

However, if you are getting the behavior you showed in the video with the code you just posted, its something wrong with your computer.

That ugly window you are seeing is simply the default window before Draw gets ahold of it and clears it. Thing is, between Run() being called and the first iteration of the game loop should literally be milliseconds with the code you just posted.

Yeah, I figured it had to be something wrong on my system, because this looks weird, and I never seen this happen before. I bought a laptop thinking it was brand new, only to learn it was refurbished, after realizing I could not update my GPU drivers.

Trying the Windows ( DirectX ) instead of the WindowsGL (OpenGL) project, this will give you a good idea if it’s a driver issue or not. Also take Farseer completely out of the equation. Just run the project wizard, create a GL project and a non-GL project and run both. If one works and the other doesn’t, yep, its a driver issue.

Thanks
KonajuGames, your code got rid of that horrible black screen. Thanks
I’m sure you can help me turn the start screen black.
Could you explain how I can do this:

To change the background colour of that window before we start drawing requires intercepting the WM_ERASEBKGND message and using the DC in the message’s wParam member to use with a call to the Win32 API FillRect() with a black brush.

In fact, not just yet. I just saw there is information on this. Let me see if I can find out on my own. I like to learn new things. If I don’t get through, I’ll get back to you. Thanks for all your help.

Edit
HA HA.I was wrong. The black screen is still there. It just went a bit fast. I didn’t see it.
Anyhow, I found this site.
So I am reading up. It looks useful.

Edit
OK
The closest I have come so far is this:
In the following example, the window procedure draws a large checkerboard pattern that fits neatly in the window. The procedure fills the client area with a white brush and then draws thirteen 20-by-20 rectangles using a gray brush. The display device context to use when drawing the background is specified in the wParam parameter for the message.

C++


HBRUSH hbrWhite, hbrGray; 
 
  . 
  . 
  . 
 
case WM_CREATE: 
    hbrWhite = GetStockObject(WHITE_BRUSH); 
    hbrGray  = GetStockObject(GRAY_BRUSH); 
    return 0L; 
 
case WM_ERASEBKGND: 
    hdc = (HDC) wParam; 
    GetClientRect(hwnd, &rc); 
    SetMapMode(hdc, MM_ANISOTROPIC); 
    SetWindowExtEx(hdc, 100, 100, NULL); 
    SetViewportExtEx(hdc, rc.right, rc.bottom, NULL); 
    FillRect(hdc, &rc, hbrWhite); 
 
    for (i = 0; i < 13; i++) 
    { 
        x = (i * 40) % 100; 
        y = ((i * 40) / 100) * 20; 
        SetRect(&rc, x, y, x + 20, y + 20); 
        FillRect(hdc, &rc, hbrGray); 
    } 
  return 1L;

But c++ is not one of my favorite languages - that’s putting it mildly.
Can someone help me understand this in c#?
In the meantime, I’m still searching. There are a lot of links, but hardly any simple explanations.