Forums

Styles lost after game runs twice.
Last Post 14 Jun 2017 03:12 AM by Kagamia. 6 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
KagamiaUser is Offline
New Member
New Member
Posts:7


--
13 Jun 2017 04:09 PM
    [STAThread]
    static void Main()
    {
    ServiceManager.Instance.AddService<IClipboardService>(new ClipboardService());

    using (var game = new Game1())
    game.Run();
    using (var game = new Game1())
    game.Run();
    }

    How to fix it?
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:674


    --
    13 Jun 2017 05:04 PM
    No idea. What's your game class implementation looks like?
    KagamiaUser is Offline
    New Member
    New Member
    Posts:7


    --
    13 Jun 2017 05:15 PM
    I'm working on Basic UI example project.
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:674


    --
    13 Jun 2017 05:22 PM
    You would have to change code. Creating GraphicsDeviceManager and initializing everything just one time. Running two Game classes after each other without that does not make sense.
    KagamiaUser is Offline
    New Member
    New Member
    Posts:7


    --
    13 Jun 2017 05:46 PM
    After hours of tracing and debugging I found the solution:

    Clear static stuffs with reflection code manually.

    Both monogame and EKUI are not designed for my workaround, they use too much singleton and global services. sad

    post something essential here:
    InputManager.Current (private static field)
    
    Border.BackgroundTextureProperty.DefaultValue (public static property/ private set)
    Border.BackgroundTextureProperty.DefaultMetadata.DefaultValue (public static set)
    Border.BorderTextureProperty.DefaultValue (private static property/set)
    Border.BorderTextureProperty.DefaultMetadata.DefaultValue (public static set)
    
    EmptyKeysTheme.Dictionary (private static field)
    ResourceDictionary.DefaultDictionary (public static)
     


    and fix monogame keyboard input by using SharpDX.RawInput.

    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:674


    --
    13 Jun 2017 06:08 PM
    There is no need to change statics using reflection. You can initialize them properly. You should move initialization out of Game class. Creating two GraphicDeviceManagers is just wrong.

    Important classes like Input Manager, Renderer for UI are created in MonoGameEngine constructor. That part is open source, so if you don't like that behavior you can change it.

    http://github.com/EmptyKeys/UI_Engi...eEngine.cs

    Let me know if you have any problem.

    KagamiaUser is Offline
    New Member
    New Member
    Posts:7


    --
    14 Jun 2017 03:12 AM
    Maybe you make sense, I should maintain only one GraphicDeviceManager among games.

    But I'm facing more difficulties than my thought. Monogame doesn't allow a custom IGraphicsDeviceManager in Game class because:

    https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Game.cs#L715

    So I have to create a GraphicDeviceManager inherics the default Manager class, and clear Dispose method to prevent GraphicsDevice being disposed on game exiting.

     
    class GraphicsDeviceManagerEx : GraphicsDeviceManager
    {
        public GraphicsDeviceManagerEx(Game game) : base (game)
        {
        }
    
        public void Reset(Game game)
        {
            var type = typeof(GraphicsDeviceManager);
            type.GetField("_game", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(this, game);
            type.GetField("_firstLaunch", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(this, true);
            game.Services.AddService<IGraphicsDeviceManager>(this);
            game.Services.AddService<IGraphicsDeviceService>(this);
        }
    
        protected override void Dispose(bool disposing)
        {
            //base.Dispose(disposing);
        }
    }
    


    public class Game1 : Game
    {
        static GraphicsDeviceManagerEx graphics;        
    
        public Game1()
            : base()
        {
            if (graphics == null)
            {
                graphics = new GraphicsDeviceManagerEx(this);
            }
            else
            {
                graphics.Reset(this);
                
            }
            
            .....................
        }
    
        protected override void Initialize()
        {
            graphics.ApplyChanges();
    
            base.Initialize();
        }
    }
    


    The first game works well, but the second game can't draw anything, even simply GraphicsDevice.Clear(Color.Black) does not work;
    Am I missing something?
    You are not authorized to post a reply.