Help with Nuclex.GameControl

ideAb's picture

Hi all! First of all, excuse me for my english. I know i've to improve it :)
Second, thanks for releasing you library. It's exactly what I was looking for, but i've a small problem.

Ok, i get that WinForms GameComponent crashes when you use contentManager but.. what do I have to be able to edit the Window itself?

I mean, with your pongproject, you can edit the WinForms, but this is what happens to me:

http://img530.imageshack.us/img530/9917/visualerrorlb4.jpg

Here's the code of the class that inherits Nuclex.GameControl, in case anything is wrong:

using XNAColor = Microsoft.Xna.Framework.Graphics.Color;
using XNARectangle = Microsoft.Xna.Framework.Rectangle;
using Microsoft.Xna.Framework.Graphics;
namespace XBEditor
{
    public partial class DrawComponent : Nuclex.GameControl
    {
        #region Vars
        private ContentManager m_Content;
        private Texture2D m_Texture;
        private SpriteBatch m_Sprite;
        #endregion
        #region User Control Functions
        public DrawComponent()
        {
            InitializeComponent();
            //Init ContentManager
            m_Content = new ContentManager(Services);
        }
        private void DrawComponent_Load(object sender, EventArgs e)
        {
        }
        #endregion
        #region Game Functions (Load,Unload,Draw,Update)
        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            m_Sprite = new SpriteBatch(graphics.GraphicsDevice);
            m_Texture = m_Content.Load("Content/Textures/smiley");
            base.LoadGraphicsContent(loadAllContent);
        }
        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            base.UnloadGraphicsContent(unloadAllContent);
        }
        protected override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            this.graphics.GraphicsDevice.Clear(XNAColor.Cyan);
            m_Sprite.Begin();
            m_Sprite.Draw(m_Texture, new XNARectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height), XNAColor.White);
            m_Sprite.End();
            base.Draw(gameTime);
        }
        protected override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            base.Update(gameTime);
        }
        #endregion
    }
}

Thanks for reading ^^

Cygon's picture

Wrong Working Directory

Hi!

That's easy to explain: When you view your control in the Form Designer, it will have the working directory set to some, well, random place (in this case, probably the path of the last assembly that was loaded).

To work around this, do one of the following:

  • Explicitly specify the path to your assets when you create the content manager, eg. content = new ContentManager(Services, @"D:\Devel\MyStuff\Xyz");. Of course there's the danger of this little hack slipping into the release build, so maybe it's best used with some check like

    if(!Assembly.GetEntryAssembly().FullName.EndsWith("devenv.exe"))
      content = new ContentManager(Services, @"D:\Devel\MyStuff\Xyz");
    else
      content = new ContentManager(Services);
  • Disable any resource loading (and possibly also drawing) code when your control is shown in the designer. Check Windows.Forms on MSDN, there should be somthing like IsDesignerModeActive() or similar allowing you to notice when your control is being run in the windows forms designer.

ideAb's picture

Now it works

Thanks for all your effort and fast replies Cygon :D

Anonymous's picture

The GetIsInDesignMode method

The GetIsInDesignMode method Cygon mentions is here, save those who dont know from googling everywhere like I did :D

http://msdn2.microsoft.com/en-us/library/system.componentmodel.designerproperties.getisindesignmode.aspx

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <br> <a> <em> <strong> <u> <i> <b> <cite> <blockcode> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <pre> <span>
  • You can highlight code with any of the following tags: <blockcode>

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.