[.net] XNA questions

Started by
19 comments, last by gharen2 16 years, 9 months ago
I can say what I'd do: not use the GameComponent class in either situation. I really don't like that part of xna, as it binds you too tightly to an api, and makes your code harder to port (which is the same reason I gave for disliking the Game class).

The GameComponent class is very simple anyways. It's essentially just a container that the game automatically updates each frame. There's nothing keeping you from creating your own class and calling it's update function yourself each frame. You can supply any information it needs through that function, such as elapsed time.

edit:

For giggles I decided to put together a simple game framework of my own, since I already have all the necessary parts lying around and some people may find it useful. Plus I've been looking for a nice little side project. Looking at the horrible hacks in that blog inspired me. This framework should be usable without Game Studio Express, you'll just have to load content manually rather than through the content pipeline.

I've applied for a sourceforge project for it, and once it's approved I'll make the source available. I've called it the Minimalist Game Framework for Xna (MGFX) and it's designed to be both minimal and flexible. Features it shares with Xna's game class are the content pipeline and simplified device management. But it's designed be cleanly usable in a Control as well, and gives you direct control over the rendering loop. A lot of fluff such as GraphicsDeviceManager have been cut out. Classes also aren't as tightly coupled (for example, in Xna, the GameWindow and Game classes are interdependent. In my framework, the Window class has no knowledge of the Game class).

Here's a project template as it stands right now. It's been tested in windows, the 360, and inside a WinForm control.

using 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;using MGFX;namespace MGFXWindowsGame{    public class Game1        : MGFX.Game    {        /// <summary>        /// Perform any initialisations needed before the game runs.  This method is called        /// before the first frame is rendered.  The GraphicsDevice has yet to be created, so        /// only non-graphical resources should be created or altered here.  The one exception        /// is that you may alter the PresentationParameter property however you like.        /// </summary>        /// <returns>An instance of an IWindow implementation.</returns>        protected override IWindow Initialise()        {            // TODO: Add your initialization logic here.            // At this point PresentationParameter has been set to default values, but may            // be altered as you choose.            // Create and return an IWindow implementation.            IWindow window = new Window(PresentationParameters.BackBufferWidth, PresentationParameters.BackBufferHeight);            window.Text = "MGFX Windows Game";            return window;        }        /// <summary>        /// Load GraphicsDevice dependent resources.        /// </summary>        /// <param name="loadAllResources">If true, load resources with either        /// ResourceManagementModes.  If false, only load resources with        /// ResourceManagementMode.Manual.</param>        protected override void LoadGraphicsResources(bool loadAllResources)        {            if (loadAllResources)            {                // TODO: Load any ResourceManagementMode.Automatic resources.            }            // TODO: Load any ResourceManagementMode.Manual resources.        }        /// <summary>        /// Dispose GraphicsDevice dependent resources.        /// </summary>        /// <param name="disposeAllResources">If true, dispose resources with either        /// ResourceManagementModes.  If false, only dispose resources with        /// ResourceManagementMode.Manual.</param>        protected override void DisposeGraphicsResources(bool disposeAllResources)        {            if (disposeAllResources)            {                // TODO: Dispose any ResourceManagementMode.Automatic resources.                ContentManager.Unload();            }            // TODO: Dispose any ResourceManagementMode.Manual resources.        }        /// <summary>        /// Perform per-frame logic not related to rendering.  This includes        /// physics, audio, and input.        /// </summary>        /// <param name="timeSpan">The duration of the previous frame.</param>        protected override void Update(TimeSpan timeSpan)        {            // Exit the game if the gamepad's back button or the escape button is hit.            if ((GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) ||                Keyboard.GetState().IsKeyDown(Keys.Escape))            {                Dispose();                return;            }            // TODO: Add your update logic here.        }        /// <summary>        /// Render a frame.        /// </summary>        protected override void Render()        {            GraphicsDevice.Clear(Color.CornflowerBlue);            // TODO: Add your rendering code here.            GraphicsDevice.Present();        }    }}


edit 2:

Sourceforge project

I'm not going to release a package until it's more complete. For example, at the moment there's zero error handling, and plenty of features missing like the ability to hide the cursor. But the source is there to look at.

[Edited by - gharen2 on June 29, 2007 12:39:05 PM]

This topic is closed to new replies.

Advertisement