Create XNA like Game Framework?

boyamer

05-08-2009 07:58:46

Anyone knows how to create a XNA like Game Framework?

Thanks

AndroidAdam

05-08-2009 18:10:08

I did make a Mogre application that worked like xna, I'll post it if I can find it. The ogre startup sequence would be a good place to look to set things up like XNA. Mono XNA is also an invaluable source in setting it up.

boyamer

10-08-2009 08:12:43

Could you post it here?

Thanks

AndroidAdam

10-08-2009 20:16:48

Here's the classes, You probably want to make your own changes, so I won't make a .dll


public class Game
{

protected Root root;
protected SceneManager scene;
GameComponentCollection components = new GameComponentCollection();
GameTime gameTime;
IGameClock clock;
TimeSpan targetElapsedTime;
long currentUpdate;

public Game()
{
components.ComponentAdded += new EventHandler<GameComponentCollectionEventArgs>(components_ComponentAdded);
components.ComponentRemoved += new EventHandler<GameComponentCollectionEventArgs>(components_ComponentRemoved);
}

void components_ComponentRemoved(object sender, GameComponentCollectionEventArgs e)
{
e.GameComponent.UnloadContent();
}

void components_ComponentAdded(object sender, GameComponentCollectionEventArgs e)
{
e.GameComponent.LoadContent();
}

public SceneManager Scene
{

get { return scene; }
protected set { scene = value; }
}
public GameComponentCollection Components
{
get { return components; }
}

protected virtual void Initalize()
{
InitializeState(new GameClock());
root = new Root();

ConfigFile cf = new ConfigFile();
cf.Load("resources.cfg", "\t:=", true);
ConfigFile.SectionIterator seci = cf.GetSectionIterator();
String secName, typeName, archName;

while (seci.MoveNext())
{
secName = seci.CurrentKey;
ConfigFile.SettingsMultiMap settings = seci.Current;
foreach (KeyValuePair<string, string> pair in settings)
{
typeName = pair.Key;
archName = pair.Value;
ResourceGroupManager.Singleton.AddResourceLocation(archName, typeName, secName);
}
}


if (!root.ShowConfigDialog())
{
return;
}
root.Initialise(true, "Main Ogre Window");

TextureManager.Singleton.DefaultNumMipmaps = 5;
ResourceGroupManager.Singleton.InitialiseAllResourceGroups();

LoadContent();

}
void InitializeState(IGameClock gameClock)
{
targetElapsedTime = TimeSpan.FromMilliseconds(1000 / 60);
clock = gameClock;
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.Zero, TimeSpan.Zero, TimeSpan.Zero, false);
}
void Tick()
{
clock.Tick();
gameTime.TotalRealTime = TimeSpan.FromMilliseconds(clock.TotalElapsedMilliseconds);

currentUpdate += clock.ElapsedMilliseconds;

TimeSpan elapsed = TimeSpan.FromMilliseconds(clock.ElapsedMilliseconds);
gameTime.ElapsedRealTime = elapsed;

long updatesDue = currentUpdate / targetElapsedTime.Milliseconds;

if (updatesDue > 0)
{
gameTime.ElapsedGameTime = targetElapsedTime;
while (updatesDue > 0)
{
Update(gameTime);
gameTime.TotalGameTime += targetElapsedTime;
updatesDue--;
}
currentUpdate = 0;
Draw();
}

gameTime.ElapsedGameTime = TimeSpan.Zero;
}
protected virtual void Draw()
{

}
protected virtual void LoadContent()
{
clock.Start();
//in your Game1 class be sure to select a scene manager, create a camera, and add the camera to the root's auto created viewport.


root.FrameStarted += new FrameListener.FrameStartedHandler(root_FrameStarted);
root.StartRendering();
UnloadContent();

}
protected virtual void UnloadContent()
{
root.Dispose();
root = null;
}
bool root_FrameStarted(FrameEvent evt)
{
Tick();
return true;
}
protected virtual void Update(GameTime gameTime)
{

foreach (GameComponent component in components)
{
component.Update(gameTime);
}
}
public void Run()
{
Initalize();
}
}



public interface IGameComponent
{
void LoadContent();
void UnloadContent();

void Update(GameTime gameTime);

}
public abstract class GameComponent : IGameComponent
{
Game game;

public GameComponent(Game game)
{
this.game = game;
}

public Game Game
{
get { return game; }
}
#region IGameComponent Members

public abstract void LoadContent();

public virtual void UnloadContent()
{

}
public virtual void Update(GameTime gameTime)
{
}

#endregion
}
public class GameComponentCollection : Collection<IGameComponent>
{
public event EventHandler<GameComponentCollectionEventArgs> ComponentAdded;
public event EventHandler<GameComponentCollectionEventArgs> ComponentRemoved;


public GameComponentCollection()
{
}

protected override void ClearItems()
{
foreach (IGameComponent component in this)
{
OnComponentRemoved(new GameComponentCollectionEventArgs(component));
}

base.ClearItems();
}

protected override void InsertItem(int index, IGameComponent item)
{
base.InsertItem(index, item);
OnComponentAdded(new GameComponentCollectionEventArgs(item));
}

void OnComponentAdded(GameComponentCollectionEventArgs eventArgs)
{
if (ComponentAdded != null)
ComponentAdded(this, eventArgs);
}

void OnComponentRemoved(GameComponentCollectionEventArgs eventArgs)
{
if (ComponentRemoved != null)
ComponentRemoved(this, eventArgs);
}

protected override void RemoveItem(int index)
{
IGameComponent item = this[index];
base.RemoveItem(index);
OnComponentRemoved(new GameComponentCollectionEventArgs(item));
}

protected override void SetItem(int index, IGameComponent item)
{
IGameComponent oldItem = this[index];
if (!oldItem.Equals(item))
{
OnComponentRemoved(new GameComponentCollectionEventArgs(oldItem));
base.SetItem(index, item);
OnComponentAdded(new GameComponentCollectionEventArgs(item));
}
}
}
public class GameComponentCollectionEventArgs : EventArgs
{
private IGameComponent gameComponent;


public GameComponentCollectionEventArgs(IGameComponent gameComponent)
{
this.gameComponent = gameComponent;
}

public IGameComponent GameComponent
{
get { return this.gameComponent; }
}

}



public class GameTime
{
TimeSpan elapsedGameTime, elapsedRealTime;
TimeSpan totalGameTime, totalRealTime;
bool isRunningSlowly = false;


public GameTime(TimeSpan totalRealTime, TimeSpan elapsedRealTime, TimeSpan totalGameTime, TimeSpan elapsedGameTime, bool isRunningSlowly)
{
this.totalRealTime = totalRealTime;
this.elapsedRealTime = elapsedRealTime;
this.totalGameTime = totalGameTime;
this.elapsedGameTime = elapsedGameTime;
this.isRunningSlowly = isRunningSlowly;
}

public TimeSpan ElapsedGameTime
{
get { return elapsedGameTime; }
internal set { elapsedGameTime = value; }
}
public TimeSpan ElapsedRealTime
{
get { return elapsedRealTime; }
internal set { elapsedRealTime = value; }
}
public TimeSpan TotalGameTime
{
get { return totalGameTime; }
internal set { totalGameTime = value; }
}
public TimeSpan TotalRealTime
{
get { return totalRealTime; }
internal set { totalRealTime = value; }
}
public bool IsRunningSlowly
{
get { return isRunningSlowly; }
internal set { isRunningSlowly = value; }
}
}
interface IGameClock
{
void Start();
void Tick();

long ElapsedMilliseconds { get; }
long TotalElapsedMilliseconds { get; }
}
class GameClock : IGameClock
{

Stopwatch timer;
long elapsedMilliseconds;
long lastTick = 0;

public GameClock()
{
timer = new Stopwatch();
}
#region IGameClock Members

public void Start()
{
timer.Start();
}

public void Tick()
{
long tick = timer.ElapsedMilliseconds;
elapsedMilliseconds = tick - lastTick;
lastTick = tick;
}

public long ElapsedMilliseconds
{
get { return elapsedMilliseconds; }
}

public long TotalElapsedMilliseconds
{
get { return timer.ElapsedMilliseconds; }
}

#endregion
}


So make a class called Game1 and have it inherit from Game, override the functions, and add what you want. LoadContent is the equivalent of CreateScene.

boyamer

10-08-2009 20:33:32

I'm missing IGameClock..

AndroidAdam

10-08-2009 20:36:52

Just edited my post, I forgot all of the time related classes, apologies.

boyamer

12-08-2009 07:52:18

Have you ever update it to latest XNA 3.1 like framework?
I tested it but i think Game calculation is different from XNA,ElapsedGameTime appears different and so.

AndroidAdam

13-08-2009 03:23:01

I haven't tried 3.1 yet, still though the point is that it's keeping track of the time. It really shouldn't matter how the time is kept track of, as long as it keeps the time.