gharen1234

In order to incorporate Xna into an existing engine without a lot of ugly hacks, I need to force the GraphicsDevice to be initialised before the program loop starts (Game.Run). Can this be done

Assuming this isn't possible (as seems likely), I'd need to ditch Game and handle device creation myself. This looks straight forward enough, except that it requires a handle to a window. This is no problem in windows, but what about on the xbox 360 Is there some sort of factory available that returns a GameWindow implementation for the platform it's running on Has anyone manually created a graphicsdevice on the 360


Re: XNA Framework Forcing GraphicsDevice creation before Game.Run

remorse

I don't know if this will help, as I am fairly new to the XNA myself.  But I am working on an application also where I want the power of XNA but I want it to run in my own window (Game/Level Editor).  I was able to create the device without having to call Game.Run by calling GraphicsDeviceManager.ApplyChanges within the Game constructor.  I can now utilize the XNA, and draw to my own windows.  Might not be completely what you were looking for, but nonetheless, I hope it helps.

public Game()

{

graphics = new GraphicsDeviceManager(this);

//In order to create a graphics device we need to ApplyChanges

graphics.ApplyChanges();

spriteBatch = new Microsoft.Xna.Framework.Graphics.SpriteBatch(graphics.GraphicsDevice);

cm = new ContentManager(Services);

}





Re: XNA Framework Forcing GraphicsDevice creation before Game.Run

gharen1234

Have you tested if you can use that sprite What you have there is actually what I currently do. The thing is, you can create the sprite in the Game constructor after graphics.ApplyChange, and Xna won't throw an exception. But in my code, the sprite then throws a nullreference exception when calling Sprite.End(). If I create the sprite in Game.Update (where the device is certain to have been created), the sprite works perfectly.

So the impression I had is that graphics.ApplyChanges stores the settings you provided, but the device isn't actually created until after Game.Run is called. And that the fact you can create a sprite is an oversight.

Or maybe I'm totally off.




Re: XNA Framework Forcing GraphicsDevice creation before Game.Run

remorse

I never ran the Game.Update or Game.Run and I am able to use the SpriteBatch.Begin, Draw and End in my own rendering loop.  I stepped the code to be sure that the GraphicsDevice is created after the ApplyChanges and it is, but I am not trying to do the SpriteBatch.Begin, Draw or End within the constructor.



Re: XNA Framework Forcing GraphicsDevice creation before Game.Run

gharen1234

Pretty peculiar that we're getting different behavior. Think you could post some code For example, if you're not calling Game.Run, where are you getting your GameWindow from As far as I know, Game.Window isn't created until Game.Run is called.

edit: yeah same here, I'm only creating the spritebatch in the constructor, after GraphicsDeviceManager.ApplyChanges().  I don't use it until later.

edit2: and yeah, I just checked and GraphicsDeviceManager.GraphicsDevice is no longer null after calling ApplyChanges().  I wonder if I have a problem elsewhere.

Would an MS rep care to confirm whether or not the graphicsdevice is actually created when you call GraphicsDeviceManager.ApplyChanges, or after Game.Run

And hopefully answer whether you can manually create a graphicsdevice on the 360




Re: XNA Framework Forcing GraphicsDevice creation before Game.Run

Shawn Hargreaves - MSFT

Yes, you can manually create your own GraphicsDevice on Xbox: just pass null for the window.

In fact this is what I'd recommend if you want to host XNA code in some environment. The Game infrastructure is designed to handle all that stuff for you, and while it may be possible to somehow coax Game into working some other way, it wasn't particulary designed to be that flexible and we haven't tested any scenarios other than just letting it create everything for you.

Creating a GraphicsDevice manually, on the other hand, is totally fine and a sensible thing to do. There's nothing internal or magic about the way Game creates its GraphicsDevice, so anything our Game class does, you can also implement yourself if you want to do things a different way.





Re: XNA Framework Forcing GraphicsDevice creation before Game.Run

remorse

I am definitely all for creating my own GraphicsDevice instead of the hack mentioned above. But what my code requires is use of the content manager and I require the Services from a Game instance. I am not familiar with how to go about creating a Service instance. Any help here



Re: XNA Framework Forcing GraphicsDevice creation before Game.Run

Michael Hansen

this is what you need it create the device and setup a basic service provider and use the contentmanager without the game dll

http://www.3devolution.net/DevBlog/tabid/90/EntryID/5/Default.aspx

the problem is when you create the device on xbox360 you can onlye specifere system.intptr = (handle) and there is no form on windows xbox what do i do with the handle

myDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, DeviceType.Hardware, handle, CreateOptions.HardwareVertexProcessing, GraphicsSettings.DefaultSetting);

 

mabye some microsoft folks can help here

the will create on xbox and i have setup a basic tread that handle the game loop

but evertime i clear the screen i will be painted with the color of my choice  

the xbox360 will shot down and exit

i think is is the handle but you cant set null in the handle

can you please help i will post some code 2 classes

the game class

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 ContentManagerSampleXbox;

using System.Threading;

 

namespace SampleXbox

{

/// <summary>

/// This is the main type for your game

/// </summary>

public partial class Game1 : IDisposable

{

public CreateSettings MySampleXbox;

public int ScreenWidth = 1280;

public int ScreenHeight = 720;

public IntPtr MyHandle;

public Game1()

{

MySampleXbox = new CreateSettings(this.MyHandle, ScreenWidth, ScreenHeight, true);

}

Thread myThread;

public void StartSample()

{

ThreadStart myStarter = delegate { RunGame(); };

myThread = new Thread(myStarter);

myThread.Start();

}

public void RunGame()

{

if (MySampleXbox.HealthSystem())

{

MySampleXbox.ManualRender_one();

}

if (GamePad.GetState(PlayerIndex.One).Buttons.B == ButtonState.Pressed)

{

myThread.Abort();

MySampleXbox.EvoCloseGamesEngine();

Dispose();

}

}

 

 

#region IDisposable Members

void Dispose()

{

GC.SuppressFinalize(this);

}

void IDisposable.Dispose()

{

Dispose();

}

#endregion

}

}

 

and the device class

using System;

using System.Collections.Generic;

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Audio;

using Microsoft.Xna.Framework.Graphics;

using Microsoft.Xna.Framework.Input;

using Microsoft.Xna.Framework.Storage;

using Microsoft.Xna.Framework.Content;

using MyContentService;

namespace ContentManagerSampleXbox

{

public struct GraphicsSettings

{

public IntPtr DefaultBackUpHandle;

public PresentationParameters DefaultSetting;

public int ScreenWidth;

public int ScreenHeight;

//public TargetPlatform Platform;

};

public class CreateSettings

{

private GraphicsSettings GraphicsSettings;

private GraphicsDevice myDevice;

public MyServiceProvider MyServiceProvider;

private MyGraphicsDevice MyGraphicsDevice;

private MyContentManager MyContentManager;

public IntPtr get_Handle

{

get { return GraphicsSettings.DefaultBackUpHandle; }

}

public GraphicsDevice GraphicsDevice

{

get { return myDevice; }

set { myDevice = value; }

}

public CreateSettings()

{

}

public CreateSettings(IntPtr handle,int screenwidth,int screenheight,bool fullscreen)

{

GraphicsSettings = new GraphicsSettings();

GraphicsSettings.DefaultBackUpHandle = handle;

GraphicsSettings.ScreenWidth = screenwidth;

GraphicsSettings.ScreenHeight = screenheight;

//GraphicsSettings.Platform = TargetPlatform.Windows;

GraphicsSettings.DefaultSetting = new PresentationParameters();

GraphicsSettings.DefaultSetting.BackBufferCount = 1;

GraphicsSettings.DefaultSetting.IsFullScreen = fullscreen;

GraphicsSettings.DefaultSetting.DeviceWindowHandle = GraphicsSettings.DefaultBackUpHandle;

GraphicsSettings.DefaultSetting.BackBufferHeight = GraphicsSettings.ScreenHeight;

GraphicsSettings.DefaultSetting.BackBufferWidth = GraphicsSettings.ScreenWidth;

GraphicsSettings.DefaultSetting.BackBufferFormat = SurfaceFormat.Color;

GraphicsSettings.DefaultSetting.EnableAutoDepthStencil = true;

GraphicsSettings.DefaultSetting.AutoDepthStencilFormat = DepthFormat.Depth16;//D16;

GraphicsSettings.DefaultSetting.PresentationInterval = PresentInterval.Immediate;

GraphicsSettings.DefaultSetting.SwapEffect = SwapEffect.Discard;

CreateXNADevice(handle);

}

 

private void CreateXNADevice(IntPtr handle)

{

myDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, DeviceType.Hardware, handle, CreateOptions.HardwareVertexProcessing, GraphicsSettings.DefaultSetting);

GraphicsSettings.DefaultBackUpHandle = handle;

MyServiceProvider = new MyServiceProvider();

MyGraphicsDevice = new MyGraphicsDevice(myDevice);

MyGraphicsDevice.AddService(MyServiceProvider);

MyContentManager = new MyContentManager(MyServiceProvider);

MyContentManager.AddService(MyServiceProvider);

DefaultSettings();

}

private void DefaultSettings()

{

myDevice.RenderState.DepthBufferEnable = true;

myDevice.RenderState.DepthBufferWriteEnable = true;

myDevice.RenderState.DepthBufferFunction = CompareFunction.LessEqual;

}

public bool HealthSystem()

{

switch (myDevice.GraphicsDeviceStatus)

{

case GraphicsDeviceStatus.Normal:

return true;

case GraphicsDeviceStatus.Lost:

ReCreateDevice();

return true;

case GraphicsDeviceStatus.NotReset:

ResetFullscreen();

break;

default:

break;

}

return true;

}

private void ReCreateDevice()

{

CreateXNADevice(GraphicsSettings.DefaultBackUpHandle);

}

private void ResetFullscreen()

{

myDevice.Reset(GraphicsSettings.DefaultSetting);

DefaultSettings();

}

private void Reset()

{

GraphicsSettings.ScreenHeight = 0;

GraphicsSettings.ScreenHeight = 0;

myDevice.Reset(GraphicsSettings.DefaultSetting);

DefaultSettings();

}

private Microsoft.Xna.Framework.Vector4 MyVector4;

public void ManualRender(CreateSettings manual)

{

Vector4 Color = new Vector4(0,0,0,0);

MyVector4.X = Color.X;

MyVector4.Y = Color.Y;

MyVector4.Z = Color.Z;

MyVector4.W = Color.W;

manual.GraphicsDevice.Clear(ClearOptions.Target,MyVector4, 24.0f, 8);

manual.GraphicsDevice.Present();

}

public void ManualRender_one()

{

myDevice.Clear(ClearOptions.Target, Color.Red, 24.0f, 8);

//GraphicsDevice.Present(get_Handle);

//myDevice.Present(null, MyRectangle, get_Handle);

GraphicsDevice.Present();

}

public void ManualSceneClear()

{

GraphicsDevice.Clear(Color.CornflowerBlue);

}

public void ManualSceneBegin()

{

GraphicsDevice.Clear(Color.CornflowerBlue);

}

public void ManualSceneEnd()

{

//GraphicsDevice.Present(null, MyRectangle, get_Handle);

}

public void EvoCloseGamesEngine()

{

if (myDevice != null)

{

myDevice.Dispose();

}

}

/// <summary>

/// dispose the device

/// </summary>

~CreateSettings()

{

myDevice.Dispose();

}

}

 

}

 






Re: XNA Framework Forcing GraphicsDevice creation before Game.Run

gharen1234

Shawn Hargreaves - MSFT wrote:
Yes, you can manually create your own GraphicsDevice on Xbox: just pass null for the window.

In fact this is what I'd recommend if you want to host XNA code in some environment. The Game infrastructure is designed to handle all that stuff for you, and while it may be possible to somehow coax Game into working some other way, it wasn't particulary designed to be that flexible and we haven't tested any scenarios other than just letting it create everything for you.

Creating a GraphicsDevice manually, on the other hand, is totally fine and a sensible thing to do. There's nothing internal or magic about the way Game creates its GraphicsDevice, so anything our Game class does, you can also implement yourself if you want to do things a different way.


Shawn, you've just solved a ton of problems for me in a few paragraphs. You're right, Game is very nice, but not flexible enough to easily incorporate into an existing engine. Initialising GraphicsDevice myself will allow me to get rid of several ugly hacks in my code.




Re: XNA Framework Forcing GraphicsDevice creation before Game.Run

Michael Hansen

can you get is to work on xbox can you please post a sample






Re: XNA Framework Forcing GraphicsDevice creation before Game.Run

gharen1234

 Michael Hansen wrote:

can you get is to work on xbox can you please post a sample



I'm afraid I don't own an xbox, so can't test it myself. And I need to do a slew of other things before that could happen, like incorporating the content pipeline into the engine too. If you want to see full source, it's at https://sourceforge.net/projects/facilis-engine . Be warned though, this engine is very alpha, and hence a bit buggy, unstable, and incomplete. I'm pretty much learning as I go :) . Once I get a version working that should run on the 360, I'll post here and maybe someone can try it out.

But the implementation wouldn't be difficult, and isn't far from what you posted. I'm using winforms in windows and passing the handle to the device, and using the XBOX360 compiler directive to ensure the code isn't there in the xbox build (since the xbox doesn't support system.windows.forms). The 360 build will just pass null to it. Personally I hate to use compiler directives if I don't have to, but in this case you have to do a seperate build for the 360 anyways, so why not I haven't committed the following changes to svn yet, but probably will in the next few hours.

edit - These changes work beautifully and are committed to svn.  I don't have a game class any more and have complete control over the rendering loop.  It's great.  Once I implement the content pipeline, I see no reason why this wouldn't run on the 360.

This is part of the Xna implementation of an abstract GraphicsDevice class in my engine:




Re: XNA Framework Forcing GraphicsDevice creation before Game.Run

Michael Hansen

my sample above does not use the xna game.dll

i have providet a link to download

if have tryed it it does not work the xbox shot down

there is no form component on xbox

onlye mscorelib , system , system.xml , and the xna.framework dll

i nortis on xbox it is all .net framework 3.0

the assembely is mark version 3.0






Re: XNA Framework Forcing GraphicsDevice creation before Game.Run

gharen1234

Michael Hansen wrote:

my sample above does not use the xna game.dll

i have providet a link to download

if have tryed it it does not work the xbox shot down

there is no form component on xbox

onlye mscorelib , system , system.xml , and the xna.framework dll

i nortis on xbox it is all .net framework 3.0

the assembely is mark version 3.0



You don't need a Form for the xbox 360. As Shawn said, just pass null (IntPtr.Zero) to the GraphicsDevice constructor. Then use Xna's input stuff for input.

Notice that in my code I'm using the XBOX360 compiler directive. That ensures that the winforms code you see there is only in the windows version. When you build that code for the 360, the winforms code is ignored.

I no longer need microsoft.xna.framework.game either, I removed it and it works fine.

The fact that the 360 .net framework is 3.0 is news to me, but I can't imagine that being a problem. New versions of the framework are backwards compatible.




Re: XNA Framework Forcing GraphicsDevice creation before Game.Run

Michael Hansen

silly me it was the tread that exit the xbox

it is now running at full speed on my plasma screen, cool xna as it looks now i onley have to replace one class in the hole engine and the fps game is running on xbox

the pc version dos not use treads onlye the xbox

tanks for all the help