# [.net] Gametime class in a windows app

This topic is 4220 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi online, Im trying to develop a level designer for my xna project. Obviously this has to be done in windows. Unfortunately most of my classes use the Gametime class that is sent to the Draw and Update functions of the Game class. Since im doing a Windows project, I dont have any Gametime to work with, because Im not using a Game class. I tried creating a new Gametime object but it does not update, and I dont know how to get it to update. Can anyone give me some suggestions? Thanks

##### Share on other sites
Hmm, that brings up a good question ... I thought for sure that GameTime would either be a struct, or allow you to update it's methods. However, a little bit of casual inspection of the meta data shows that it is in fact a class (Reference type), and the only way to set it's values are to call the constructor.

So that suggests to me that one is being new'd up every frame? that's crazy talk.

##### Share on other sites
Yeah I know. I really dont want to write a new timer when there is already one that we should use anyway built into XNA.

##### Share on other sites
The GameTime is used by Game to handle the timing of the loop. So you can't use GameTime without having Game in the project. What I am wondering is why you are not using Game? Yes there is some extra over head but I think that you could use it to make things a bit simpler.

My idea; Create a standard window's app. Create a new thread for the Game object. Let the Game object run over there taking care of your drawing and all the XNA world. Have the main thread for your Level designer.

theTroll

##### Share on other sites
How exactly do you do that? Because the game class does not have a normal windows form. So how do you tell it to render (in my case anyway) to a panel inside a windows form?
Thanks again for any help.

##### Share on other sites
Going to try to work up some code for it. Be back in a bit with some code.

theTroll

##### Share on other sites
SavyCat - do you actually need your objects Updating in the editor? It seems like in that context, an all-zeros GameTime would work fine because in the editor its sorta always time index 0. Things should draw fine - they'll just be inert.

But if you want a nice timer, you can swipe the one in the Sample Frameworks source code. (DX SDK Install Dir)\Samples\Managed\Common\dxmutmisc.cs Then you can drive it with a regular old Timer control. The raft of calcs you have to make to get the constructor params set up are a bit of a chore. I wrote the code once, a while back but I don't know if I still have it - I changed my plumbing around to decouple my model from XNA. I'll take a look around and will post if I find it.

On the other hand, why not just make the editor a Game just like the actual game. I don't know if you have to put some sort of Do Events in the game's pump functions so that the Windows.Forms objects can process their messages. I've never done this myself but I've heard other people mention doing it.

Quote:
 Original post by joelmartinezSo that suggests to me that one is being new'd up every frame? that's crazy talk.

It must have "internal" members back inside the XNA assemblies. I just checked and its the same instance being provided each call:

private GameTime lastGameTime = null;protected override void Update(GameTime gameTime){    if (lastGameTime == null)        lastGameTime = gameTime;    else    {        if (object.ReferenceEquals(lastGameTime, gameTime))        {            // it stops on a break point set here            int wtf = 4;        }    }    base.Update(gameTime);}

[Edited by - dalep on January 28, 2007 11:45:16 AM]

##### Share on other sites
The first idea did not work, trying something else. Be back in a bit.

theTroll

##### Share on other sites
Making progress, hope to have an idea for you soon.

theTroll

##### Share on other sites
Got a possible solution for you. This will allow your XNA Game window to be fully active while having complete access to the editor at the same time.

#region Using Statementsusing System;using System.Collections.Generic;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Audio;using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Storage;#endregionnamespace XNACore{    /// <summary>    /// This is the main type for your game    /// </summary>    public class Game1 : Microsoft.Xna.Framework.Game    {        GraphicsDeviceManager graphics;        ContentManager content;        private System.Windows.Forms.Form form;        private Microsoft.Xna.Framework.Graphics.Viewport xnaViewport1;        private System.Windows.Forms.Panel panelLeft;        private System.Windows.Forms.Panel panelRightBottom;        private System.Windows.Forms.Button button1;        public Game1()        {            graphics = new GraphicsDeviceManager(this);            content = new ContentManager(Services);                    }        /// <summary>        /// Allows the game to perform any initialization it needs to before starting to run.        /// This is where it can query for any required services and load any non-graphic        /// related content.  Calling base.Initialize will enumerate through any components        /// and initialize them as well.        /// </summary>        protected override void Initialize()        {            // TODO: Add your initialization logic here            base.Initialize();            xnaViewport1 = new Viewport();            xnaViewport1.X = this.Window.ClientBounds.Width/2;            xnaViewport1.Y = 0;            xnaViewport1.Width = this.Window.ClientBounds.Width / 2;            xnaViewport1.Height = this.Window.ClientBounds.Height / 2;            this.graphics.GraphicsDevice.Viewport = xnaViewport1;                        IntPtr ptr = this.Window.Handle;            form = (System.Windows.Forms.Form)System.Windows.Forms.Control.FromHandle(ptr);            panelLeft = new System.Windows.Forms.Panel();            panelLeft.Width = this.Window.ClientBounds.Width / 2;            panelLeft.Height = this.Window.ClientBounds.Height;            panelLeft.Top = 0;            panelLeft.Left = 0;            form.Controls.Add(panelLeft);            panelRightBottom = new System.Windows.Forms.Panel();            panelRightBottom.Width = this.Window.ClientBounds.Width / 2;            panelRightBottom.Height = this.Window.ClientBounds.Height / 2;            panelRightBottom.Top = this.Window.ClientBounds.Height / 2;            panelRightBottom.Left = this.Window.ClientBounds.Width / 2;            form.Controls.Add(panelRightBottom);            button1 = new System.Windows.Forms.Button();            button1.Text = "It works!!!";            button1.Top = 20;            button1.Left = 20;            panelLeft.Controls.Add(button1);        }        /// <summary>        /// Load your graphics content.  If loadAllContent is true, you should        /// load content from both ResourceManagementMode pools.  Otherwise, just        /// load ResourceManagementMode.Manual content.        /// </summary>        /// <param name="loadAllContent">Which type of content to load.</param>        protected override void LoadGraphicsContent(bool loadAllContent)        {            if (loadAllContent)            {                // TODO: Load any ResourceManagementMode.Automatic content            }            // TODO: Load any ResourceManagementMode.Manual content        }        /// <summary>        /// Unload your graphics content.  If unloadAllContent is true, you should        /// unload content from both ResourceManagementMode pools.  Otherwise, just        /// unload ResourceManagementMode.Manual content.  Manual content will get        /// Disposed by the GraphicsDevice during a Reset.        /// </summary>        /// <param name="unloadAllContent">Which type of content to unload.</param>        protected override void UnloadGraphicsContent(bool unloadAllContent)        {            if (unloadAllContent == true)            {                content.Unload();            }        }        /// <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)        {            // Allows the default game to exit on Xbox 360 and Windows            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)                this.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)        {            graphics.GraphicsDevice.Clear(Color.Blue);                // TODO: Add your drawing code here                        base.Draw(gameTime);        }    }}

I have tested it and everything seems to work just great.

theTroll

1. 1
2. 2
3. 3
4. 4
frob
13
5. 5

• 16
• 13
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632171
• Total Posts
3004553

×