Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

D3D problem with device and classes

Status
Not open for further replies.

xorl

Programmer
Aug 8, 2006
10
SE
Hey

I have an annoying problem that i hope someone got the answer for:

I have an application so far with D3D layout. One mainclass, one middleclass for objects(images and stuff) and one class for setting position of the current objects.

Ill just post the relevant listing from the mainclass:


public bool InitializeGraphics()
{
try {

PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
g_device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);

g_device.DeviceLost += new EventHandler(this.InvalidateDeviceObjects);

g_device.DeviceReset += new EventHandler(this.RestoreDeviceObjects);

g_device.Disposing += new EventHandler(this.DeleteDeviceObjects);

g_device.DeviceResizing += new CancelEventHandler(this.EnvironmentResizing);

return true;
} catch (DX.DirectXException) {
return false;
}
}

/* I want to show the sprite in this method or onpaint
* but it doesn't work because i think it has todo something
* with the device in the other sprite class. */

protected virtual void Render()
{
if (g_device != null) {
g_device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);
g_device.BeginScene();

//i want to be able to use the sprite class like so:

CSprite newobj = new CSprite(this, g_device);
newobj.InitializeObjects(); //some testcode in initobjects()
newobj.DrawObjects();

//note: when i run i get the "Err in app" exception.

g_device.EndScene();
g_device.Present();
}
}

**************************************

SpriteClass:



public class CSprite
{
private float x;
private float y;

private Sprite starobj;
private Texture textsprite;
private PresentParameters presparam;
//private Rectangle rect;


public CSprite(System.Windows.Forms.Form parent, Device dev)
{
/* here is the real error. It has to be something with
* the device not being properly setup but i cant be
* sure about that. The exception i get is "error in the
* application" and the source and debug log says its
* D3D related. Maybe an override on Render()?. Then id
* still have the first device in MainClass.
try
{
presparam = new PresentParameters();
presparam.Windowed = true;
presparam.SwapEffect = SwapEffect.Discard;
dev = new Device(0, DeviceType.Hardware, parent, CreateFlags.SoftwareVertexProcessing, presparam);

textsprite = TextureLoader.FromFile(dev, "star.png");
starobj = new Sprite(dev);

} catch (Exception e) {
System.Windows.Forms.MessageBox.Show(e.Message + " " +e.Source);

}
}

public void InitializeObjects()
{
try
{
starobj.Begin(SpriteFlags.None);
} catch (Exception errfile) {
throw new Exception(errfile.Message + " " + errfile.Source);
}
}

public void DrawObjects(int pwidth, int pheight)
{
DrawStar(new CObjPos(pwidth, pheight));
}

private void DrawStar(CObjPos nPos)
{
for (x = 0; x < nPos.StarXPos; x++)
{
for (y = 0; y < nPos.StarYPos; y++)
{
starobj.Draw(textsprite, new Rectangle(0,0,5,5), Vector3.Empty, new Vector3(x, y, 0), Color.White.ToArgb());
}
}
}

*************************************

Is the problem that two devices are set up at the same time?

or something else?

It would be much appreciated if i could get an answer of this.

Best regards
/xorl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top