I don't want to do anything with or in the game. The problem is when i'm using the directx in my program since the graphics device is in use when i switch to the game full screen that need to use directx too my program is crash in fact visual studio 2012 pro is shut down.
I need somehow to release/dispose in my program the device when switching to the game(in this case it's a game) and then when switching back to windows to restart/init my device again.
This is the code where i init my directx :
public Boolean InitializeDirectX(PictureBox pb)
{
DispMode = Manager.Adapters[Manager.Adapters.Default.Adapter].CurrentDisplayMode;
D3Dpp = new PresentParameters();
D3Dpp.BackBufferFormat = DispMode.Format;
D3Dpp.PresentFlag = PresentFlag.LockableBackBuffer;
D3Dpp.SwapEffect = SwapEffect.Discard;
D3Dpp.PresentationInterval = PresentInterval.One; //wait for vertical sync. Synchronizes the painting with
//monitor refresh rate for smoooth animation
D3Dpp.Windowed = true; //the application has borders
try
{
D3Ddev = new Device(Manager.Adapters.Default.Adapter, DeviceType.Hardware, pb.Handle,
CreateFlags.SoftwareVertexProcessing, D3Dpp);
D3Ddev.VertexFormat = CustomVertex.PositionColored.Format;
D3Ddev.RenderState.Lighting = false;
D3Ddev.RenderState.CullMode = Cull.CounterClockwise;
//load imagesBmp to panelTexture
//panelTexture = Texture.FromBitmap(D3Ddev, imagesBmp, Usage.Dynamic, Pool.Default)
backTexture = TextureLoader.FromStream(D3Ddev, mymem);
scannedCloudsTexture = new Texture(D3Ddev, 512, 512, 1, Usage.Dynamic, Format.A8R8G8B8, Pool.Default);
//sprite is used to draw the texture
D3Dsprite = new Sprite(D3Ddev);
return true;
}
catch
{
return false;
}
}
And this is where i'm getting the exception when switching to the game full screen:
Bitmap bmpn;
float angle = 0;
private void DisplayOnScreen(float angleF)
{
try
{
if (angle < 360)
{
bmpn = new Bitmap(512, 512);
angle++;
}
else
{
angle = 361;
}
Surface backbuffer;
Brush myBrush = new SolidBrush(Color.FromArgb(110, 0, 255, 0)); //semi transparent color to draw the rotating cone
Graphics g;
//clear the backbuffer with Color.FromArgb(56, 56, 56). This is the double buffer mechanism. Drawing to offscreen
//backbuffer and in the end flipping it to main one which is our panelContainer
D3Ddev.Clear(ClearFlags.Target, Color.FromArgb(56, 56, 56), 1, 0);
D3Ddev.BeginScene();
//Draw Sprites
//////////////////////////////////////////////////////
D3Dsprite.Begin(SpriteFlags.AlphaBlend);
// bitmap with clouds
D3Dsprite.Draw2D(backTexture, new PointF(0, 0), 0F, new PointF(0F, 0F), Color.White);
//the part of clouds that are inside the cone
D3Dsprite.Draw2D(scannedCloudsTexture, new PointF(0F, 0F), 0F, new PointF(0F, 0F), Color.White);
//rotate cone
//D3Dsprite.Draw2D(scannerTexture, new PointF(104.5F, 0F), angle, new PointF(256F, 255F), Color.White);
D3Dsprite.Flush();
D3Dsprite.End();
//////////////////////////////////////////////////////
//Draw the cone.
using (backbuffer = D3Ddev.GetBackBuffer(0, 0, BackBufferType.Mono))
{
using (g = backbuffer.GetGraphics())
{
g.SmoothingMode = SmoothingMode.AntiAlias;
/*if (angle < 360)
{
g = Graphics.FromImage(bmpn);
}*/
g.FillPie(myBrush, 256F - distanceFromCenterPixels, 255F - distanceFromCenterPixels,
distanceFromCenterPixels * 2F, distanceFromCenterPixels * 2F, angleF - 23F, 46F);
if (angle <= 360)
{
this.Invalidate();
/*pictureBox1.Image.Save(@"c:\coneimages\" + "PictureBox_Images" + angle + ".gif",ImageFormat.Gif);
bmpn.Save(@"c:\coneimages\" + "Cone_Images" + angle + ".gif");
bmpn.Dispose();*/
//Rectangle rect = new Rectangle(0,0,512,512);
//this.DrawToBitmap(bmpn, rect);
//g.DrawImage(bmpn, rect);
//bmpn.Save(@"c:\coneimages\" + "Cone_Images" + angle + ".gif");
//bmpn.Dispose();
}
}
}
D3Ddev.EndScene();
D3Ddev.Present(); //performs the flipping
}
The exception is on the line:
D3Ddev.Present();
And this is the exception message:
Microsoft.DirectX.Direct3D.DeviceLostException occurred
HResult=-2005530520
Message=Error in the application.
Source=Microsoft.DirectX.Direct3D
ErrorCode=-2005530520
ErrorString=D3DERR_DEVICELOST
StackTrace:
at Microsoft.DirectX.Direct3D.Device.PresentInternal(tagRECT* sourceRectangle, tagRECT* destRectangle, IntPtr overrideWindow)
at Microsoft.DirectX.Direct3D.Device.PresentInternal(tagRECT* sourceRectangle, tagRECT* destRectangle, Control overrideWindow)
at Microsoft.DirectX.Direct3D.Device.Present()
at mws.ScanningClouds.DisplayOnScreen(Single angleF) in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\ScanningClouds.cs:line 347
InnerException:
Line 347 is:
D3Ddev.Present();
I tried to google but didn't find how to do it only found that i need somehow to release/dispose i think the device when switching to the full screen and then when switching back maybe i need to re InitializeDirectX again. Not sure if this is the problem but the exception happen only when i switch to the full screen game that also using the directx.
Aucun commentaire:
Enregistrer un commentaire