Drawing In LoadContent & Other Odd Questions

Question 1: Draw in LoadContent?:
If I had a lot of data to load on a non-SSD, it takes a couple seconds and I like to show a progress animation for that. I’m wondering if I can make a second Draw that works during the load so I can trigger the drawing & page-flip as it goes? I suppose there are other ways around it too.
I’m open to any random ideas or suggestions.

Question 2: DeviceLost already handled? [using DesktopGL btw]:
Also I tried switching full-screen, windowed, and between apps which try to hog the graphics card and never noticed any device lost or texture data lost in any scenario so far. Would I be guessing correctly that Monogame is already handling for weird device-lost scenarios?

I don’t think you have to load everything in LoadContent, do you? I’ve never actually tried… presumably you could load your progress assets in LoadContent and transition into a gameplay state that loads assets during the Update loop while drawing the status in the Draw loop.

I used to do a lot of work with this kind of thing at an old job (not using MonoGame). We used remote desktop to remote into a server playing sound using SlimDX and when the remote session was closed, the device was lost. We had to trap an error event and reconnect the device, it was a huge pain in the butt.

I don’t think you have to do this for MonoGame. I’ve never tested the remote desktop scenario, but I did a lot of Android testing with this when working on a game I made, CodeLogic. I had no issues with data or device connections being lost when task switching, even after long pauses. The only actual work I had to do was trap the pause and resume events so that I could handle audio playback. Audio playback was not automatically resumed when you came back, for example.

So for your test scenario for DesktopGL, the only thing extra that I can think to try is that remote desktop scenario. Remote into another machine and run the app. Kill the remote session and rejoin. If the app is still rendering correctly, you’re probably ok! Maybe check audio too if that’s a concern for you.

1 Like

Ah yes, I can do it that way. I’ll load those little essentials and load the remaining in a level-init condition in the main game loop.

Interesting - oh and I didn’t even think of that: if window becomes inactive and it goes into a pause-state, I’ll probably want to resume music where it left off when it becomes active again. I try a bunch of things and if I can’t break it, then it’s probably good to go. XD

Thanks for the suggestions. :slight_smile:

1 Like

I just thought of another odd question.

My level and graphics data are consuming a lot of memory. I want to free up memory between levels.
My current weird strategy is to use a couple different content managers and free up the old one as I swap them. (I think it will work ok?) I’m wondering if anyone out there has experience with this sort of thing and maybe knows if this might cause issues or if there are other ideas that may work?
This is what I just started doing (not tested yet):

// Cn is Content
if (init1) {
    init1 = false;
    OldContent  = new ContentManager(Cn.ServiceProvider, Cn.RootDirectory);
    LevContent  = new ContentManager(Cn.ServiceProvider, Cn.RootDirectory);
    SwapContent = new ContentManager(Cn.ServiceProvider, Cn.RootDirectory);
}		
//...
//and later:
//...
if (newLevel) {
    SwapContent = OldContent; 
    OldContent = LevContent; 
    LevContent = SwapContent; 
    OldContent.Unload(); 
    
    // Now LevContent will be used to load the new level and next time it's swapped to old and unloaded
    Load_Graphx_and_Sound(LevContent, level);
}

I’ll need to finish / fix a lot more code before I can test it because everything’s in a state of chaos at the moment. Perhaps after, I’ll report back whether this seems to work well without any issues.
Other ideas are most welcome.

[ E D I T ] - I realize now that I don’t need to worry about any unload overlap issue. So no need to do the swap. All that’s needed is LevContent.Unload and then proceed to load up new level data. (And shared data can stay in the original Content). Some were saying a unique ContentManager is needed for each level but that doesn’t seem to be so.

Have you tried loading your content in a separate thread?

Might be worth trying.

1 Like

Interesting - I shall investigate. Thanks for the tip. :slight_smile:

I did as suggested above and it was working and then, I just stumbled across this little gem too.

gpu.Present();

It allows you to draw whatever you want when or wherever you want and shows what you’ve drawn so far (no need to call draws from inside Draw).
Now I can do like:

Load("gfx/dancing_penguins"); 
ShowProgress(percent: 20, "loading dancing penguins");  // <-- contains gpu.Present(); 
Load("music/penguin_songs");
ShowProgress(percent: 35, "loading penguin songs"); 

The problem with that is you will still be stopping your main game thread which will be blocking mouse movement etc.

You could go something like this (Not tested)
edit

class LoadingScreen
{
    volatile bool finishedLoading;
    volatile float percentDone;

    public LoadingScreen()
    {
        ThreadPool.QueueUserWorkItem(GoLoad);
    }

    public void GoLoad(Object stateInfo)
    {
        //Load stuff
        //percentDone = whatever;
        //Load stuff
        finishedLoading = true;
    }

    public override void Update()
    {
        If (finishedLoading)
        {
            //change to next screen
        }
        
    }

    public override void Draw()
    {
          //Draw screen and percent done
    }

}
1 Like

Even better - much thanks. ^-^

No worries. Probably want to put the drawing in the draw method (not the update method)

Update method when finished loading will change the screen or whatever way you have set your project to do

edit changed to fix it above

1 Like
  • My simple content managers implementation handling System, Level and Self :
  • My simple loading progress bar : )
  • I don’t think OpenGL has device lost like in Direct X : ) AFAIK device lost trapping is only done when using DirectX, but I’m maybe wrong ^_^y
1 Like

Thanks. :slight_smile: Very similar setup now. ;d
One manager for common content, title-screen, menu
Another for cut-scenes which totally unloads if won’t be seen again.
Another for levels which now unloads between levels.
I like the idea of having one for audio management like that.

Now loading progress indicator shows if “now_loading > 0” which then indicates percentage. It’s so quick you almost don’t see it but I thought it might be good for ppl with slow mechanical hard drives.

Yah, I don’t remember seeing DeviceLost handling in SDL - maybe it does it automatically under the hood. I don’t imagine it’s very common for VRAM to be overwritten by other apps upon losing focus but I suppose it wouldn’t hurt to see if I can break it and then make something that reloads lost content when Game1’s “this.Active” switches back to true. It’s probably such a rare thing that it might be something to leave for last when almost ready to publish.