Initialize Monogame without window?

I was just wondering if it was possible to make a monogame library it which I use graphics, sound and input but without the game class or game window?

something like modules in which they auto initialize when they are used.

1 Like

MonoGame isn’t written to be modular and you’ll find input and graphics (for OpenGL more than others) are closely tied to the window, so it’s not easy having a system that does then modularly. There’s ways to run MG without Game or GameWindow though, as shown by the projects embedding MG inside WinForms for example. But it’s not like you can easily disable parts of MG or anything like that.

What’s the your purpose, your end goal with this? I’m not sure MG is suited for it.

well I want to make a static module library that I can use in a monogame project or window

something simple that contains the following…

  • a system module
  • a graphics module
  • a sound module
  • a input module
  • a object module

I want to use monogame because its cross platform and uses C# code it which both are very valuable
also monogame forms :smiley:

I don’t understand what you mean.
How would you define a module? You want to be able to only load some of the modules? Do you want to be able to swap out a MG module for something else? You have a use case in mind or is it not so much a practical project?

no swapping I just want to be able to use it easily in the engine and editor
I want to make a cross platform zelda like editor.

the module would be used something like this…

protected override void Draw()
{
Gfx.Start();
var img = Gfx.NewImage("Test.png");
Gfx.SetCanvas(img);
Gfx.DrawRect(0,0,img.W,img.H);
Gfx.SetCanvas();
Gfx.Draw(img, 0, 0, img.W, img.H, 0)
Gfx.End();
}

@Jjagg so here is a example of what I need to do to get monogame to run without a game class…
public class Sys { SysManager Manager; internal static ContentManager Content { get; private set; } = ???; internal static GraphicsManager Graphics {get; private set; } = ???; internal static SpriteBatch Sprites { get; private set; } = ???;
I need to know how to get a handle on these objects without the gameclass.
then I use them to create my moduals

@Jjagg

idk this is one of the reasons I dislike monogame.
I would like to just be able to draw and create content without having to use a game class.
then I could just use it later in the game loop I desire.

is there no solution for this problem?
is there anyway to add support for something like this?

It’s definitely possible, but very tricky. There’s some articles about it, but I can’t tell you how to go about it off the top of my head.

I also dislike how MG pretty much forces you to use the Game class, but part of that is because of the XNA API that it inherited, so it’s hard to solve the issue.

Hey, I doubt you’re still trying to do this, but I found this topic from searching the internet looking to do something similar. I got a solution that works for me, so I thought I’d leave it here for any other travellers who find this page.

Note: This uses the Game class, but NOT the Game Window, which was what I needed in my case.

var game = new UpheavalGame();
game.RunOneFrame();

Task.Run(() =>
{
    while (game != null)
    {
        Thread.Sleep(170); // 60 "FPS"
        game.RunOneFrame();
    }
});

...

public class UpheavalGame : Game
{
    public UpheavalGame()
    {
        // Without this next line, the "RunOneFrame" function throws an error.
        var _ = new GraphicsDeviceManager(this);
    }

    protected override void LoadContent()
    {
        base.LoadContent();
        // Use the "Content" object in here to load sounds or graphics you need.
    }

    protected override void Update(GameTime gameTime)
    {
        // Whatever you want in here.
            
        base.Update(gameTime);
    }
}

I recommend you avoid doing this. It’s adding an extra layer of lag on top of whatever your existing update processing time is, so you’ll get 60 FPS when you’re doing nothing and 30FPS when you should be getting 60.

Monogame can clamp the FPS natively. Just set game.IsFixedTimeStep to true. The fixed time step processing happens within game.RunOneFrame(), so it’ll work even without the game window.

1 Like

Nice, I did not know this! Thank you! :smiley:

I’ve updated my code above trying to use your advice. My constructor now looks like this:

public UpheavalGame()
{
    // Without this next line, the "RunOneFrame" function throws an error.
    var _ = new GraphicsDeviceManager(this);

    IsFixedTimeStep = true;
    TargetElapsedTime = TimeSpan.FromMilliseconds(170);
}

and my Task.Run now looks like this:

Task.Run(() =>
{
    while (upheavalConsoleGame != null)
    {
        upheavalConsoleGame.RunOneFrame();
    }
});