Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Managed Directx application window issues

Started by panface 1 reply 850+ views
Original Post
panface
panface
I am simply rendering a teapot mesh using a window form with no device event handlers being managed. Works great, but the first minute I click into another window the cpu is at a 100 percent and xp is almost completely handicapped. I click back into the rendered window and bamb...everthing is back. I know this has to be something simple... Any ideas? using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; namespace DSEdit { public partial class Form1 : Form { private Device GraphicDevice; //My objects private myTeapot centreTeapot; private float angle = 0; public Form1() { InitializeComponent(); } public void InitializeGraphics() { PresentParameters pp = new PresentParameters(); pp.Windowed = true; pp.BackBufferFormat = Format.X8R8G8B8; pp.SwapEffect = SwapEffect.Discard; pp.AutoDepthStencilFormat = DepthFormat.D16; pp.EnableAutoDepthStencil = true; GraphicDevice = new Device(0, DeviceType.Hardware, this.Handle, CreateFlags.SoftwareVertexProcessing, pp); GraphicDevice.RenderState.Lighting = true; GraphicDevice.RenderState.Ambient = Color.Gray; GraphicDevice.DeviceLost += new EventHandler(GraphicDevice_DeviceLost); GraphicDevice.DeviceReset += new EventHandler(GraphicDevice_DeviceReset); GraphicDevice.Disposing += new EventHandler(GraphicDevice_Disposing); GraphicDevice. } void GraphicDevice_Disposing(object sender, EventArgs e) { } void GraphicDevice_DeviceReset(object sender, EventArgs e) { } void GraphicDevice_DeviceLost(object sender, EventArgs e) { MessageBox.Show("device lost!"); } public void DisposeGraphics() { GraphicDevice.Dispose(); } public void SetupScene() { GraphicDevice.Transform.Projection = Matrix.PerspectiveFovLH(( float)Math.PI / 4, this.Width / this.Height, 1.0f, 500.0f); GraphicDevice.Transform.View = Matrix.LookAtLH( new Vector3(0, 0, -20), new Vector3(0, 0, 0), new Vector3(0, 1, 0)); GraphicDevice.RenderState.Lighting = true; GraphicDevice.Lights[0].Type = LightType.Directional; GraphicDevice.Lights[0].Position = new Vector3(-10, 0, -5); GraphicDevice.Lights[0].Attenuation1 = 0.1f; GraphicDevice.Lights[0].Range = 20.0f; GraphicDevice.Lights[0].Enabled = true; centreTeapot = new myTeapot(ref GraphicDevice); } public void Render() { GraphicDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer,Color.CornflowerBlue ,1,0); GraphicDevice.BeginScene(); //Get Cursor Position Point cursor = Point.Empty; cursor = this.PointToClient(Cursor.Position); if(cursor.X < 0) cursor.X = this.Width; if(cursor.Y < 0) cursor.Y = this.Height; //Centre Teapot GraphicDevice.Transform.World = Matrix.Identity; GraphicDevice.Transform.World = Matrix.Multiply(GraphicDevice.Transform.World, Matrix.Translation(0,-5,0)); centreTeapot.Intersect(cursor.X, cursor.Y, GraphicDevice.Viewport, GraphicDevice.Transform.Projection, GraphicDevice.Transform.View, GraphicDevice.Transform.World); centreTeapot.Render(ref GraphicDevice); angle += 1f; if (angle == 360.0f) { angle = 0; } GraphicDevice.EndScene(); GraphicDevice.Present(); } } class myTeapot { Mesh mesh; Material material; public myTeapot(ref Device dev) { mesh = Mesh.Teapot(dev); material = new Material(); material.Diffuse = Color.SeaGreen; material.Ambient = Color.SeaGreen; material.Specular = Color.SeaGreen; } public void Intersect(int X, int Y, Viewport viewport, Matrix projection, Matrix view, Matrix world) { Vector3 Nearcor; Vector3 Farcor; Nearcor = new Vector3(X, Y, 0); Nearcor = Vector3.Unproject(Nearcor, viewport, projection, view, world); Farcor = new Vector3(X, Y, 1); Farcor = Vector3.Unproject(Farcor, viewport, projection, view, world); Farcor.Normalize(); if (mesh.Intersect(Nearcor, Farcor)) { material.Diffuse = Color.Gold; material.Ambient = Color.Gold; material.Specular = Color.Gold; } else { material.Diffuse = Color.SeaGreen; material.Ambient = Color.SeaGreen; material.Specular = Color.SeaGreen; } } public void Render(ref Device dev) { dev.Material = material; mesh.DrawSubset(0) } } }
remigius
remigius
The code you posted seems to be missing your render loop and that's probably where the problem is coming from, so it's going to be hard to even offer suggestions :)

In a typical render loop though, you'll essentially have an infinite loop constantly redrawing your scene as fast as possible, which will indeed consume all of your CPU. One option to make your app play nice with the the other windows would be to hook your forms Activated and Deactivated events and reduce your framerate when the form's inactive. A quick way to do this would be to have the render loop thread sleep for a few ms, like this:

[source lang=C#]public Form1(){    InitializeComponent();        this.Activated += new EventHandler(Form1_Activated);    this.Deactivate += new EventHandler(Form1_Deactivate);}private bool isActive = false;void Form1_Deactivate(object sender, EventArgs e){    isActive = false;}void Form1_Activated(object sender, EventArgs e){    isActive = true;            }void Render(){    if (!isActive)    {        System.Threading.Thread.Sleep(50);    }    // normal rendering}
panface
panface
Hey thanks for the suggestions!

My render loop is actually based on when the renderTarget should be drawn

renderPanel1.Paint += new PaintEventHandler(Render);


That is all I was doing..nothing more....

One of the problems I found out is that the rendering panel is basically gone when another window covers it. If you attempt to draw to a window that does not exist, it will throw an exception. These exception (if not catched) will continue to throw at each frame which resulted in the cpu going nuts. I fixed this in the render loop with this line...

if (e.ClipRectangle.IsEmpty)
return;


Walla! The cpu is now back to normal.

I will also implement your suggestion!

Thanks!

Jake

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.