Sign in to follow this  

MDX render to a panel in a windows form

This topic is 4112 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been searching the hole day for a solution to this, but it looks like my google skills won't help me with this one. I'd like to make a form for a litle server application that will host a game and should show the current gam ein a little window while displaying log messages and other things . So I tried to make a panel and render the device content to it , but this one doesn't work out , because the form components don't get updated and redrawn. It will either update the device content or the components but not both at the same time. Here's the simplified code :

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 App_test {
    public partial class Form1 : Form {

        private float angle = 0.0f;

        private Device panelDevice;
        private PresentParameters pp;
        private Mesh mesh;

        public Form1() {
            InitializeComponent();
   
            this.Setstyle(Controlstyles.AllPaintingInWmPaint |         Controlstyles.Opaque, true);
        }

        public bool InitializeGraphics() {
            pp = new PresentParameters();

            pp.Windowed = true;
            pp.SwapEffect = SwapEffect.Discard;

            panelDevice = new Device(0, DeviceType.Hardware, scenePanel1, CreateFlags.SoftwareVertexProcessing, pp);

            mesh = Mesh.Teapot(panelDevice);

            return true;
        }

        protected override void OnPaint(PaintEventArgs e) {
            angle += 0.01f;

            int i = 0;
            if ((i = new Random().Next(100)) < 10)
                textBox1.Text += "Zahl : " + i + "\r\n";

            panelDevice.Transform.World = Matrix.RotationY(angle);
            panelDevice.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, -5f), new Vector3(0, 0, 1), new Vector3(0, 1, 0));
            panelDevice.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, (float)scenePanel1.Width / (float)scenePanel1.Height, 1.0f, 1000.0f);

            panelDevice.Clear(ClearFlags.Target , Color.CornflowerBlue, 1.0f, 0);

            panelDevice.BeginScene();

            mesh.DrawSubset(0);

            panelDevice.EndScene();

            panelDevice.Present();

            this.Invalidate();
        }
    }

    public class ScenePanel : Panel {
        public ScenePanel() {
            
            this.Setstyle(Controlstyles.AllPaintingInWmPaint | Controlstyles.Opaque, true);
     
        }
    }
}


Thanks for any suggestions . Oh and btw. how can i make a code box in a post? [Edited by - tokenone on September 15, 2006 11:47:53 AM]

Share this post


Link to post
Share on other sites

panelDevice = new Device(0, DeviceType.Hardware, scenePanel1.Handle, CreateFlags.SoftwareVertexProcessing, pp);




I don't know if it helps but you can try this. scenePanel1.Handle insteed of scenePanel1.

http://www.gamedev.net/community/forums/faq.asp#tags <-- add source code ;)

Share this post


Link to post
Share on other sites
What i did is i took the onpaint method of the panel and not from the form. It's not nessesary to change the style of the panel of form. cheers


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 WindowsApplication2
{
public partial class Form1 : Form
{
private float angle = 0.0f;

private Device panelDevice;
private PresentParameters pp;
private Mesh mesh;


public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
pp = new PresentParameters();

pp.Windowed = true;
pp.SwapEffect = SwapEffect.Discard;

panelDevice = new Device(0, DeviceType.Hardware, scenePanel1, CreateFlags.SoftwareVertexProcessing, pp);

mesh = Mesh.Teapot(panelDevice);

}

private void Form1_Paint(object sender, PaintEventArgs e)
{
angle += 0.01f;

int i = 0;
if ((i = new Random().Next(100)) < 10)
textBox1.Text += "Zahl : " + i + "\r\n";

panelDevice.Transform.World = Matrix.RotationY(angle);
panelDevice.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, -5f), new Vector3(0, 0, 1), new Vector3(0, 1, 0));
panelDevice.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, (float)scenePanel1.Width / (float)scenePanel1.Height, 1.0f, 1000.0f);

panelDevice.Clear(ClearFlags.Target, Color.CornflowerBlue, 1.0f, 0);

panelDevice.BeginScene();

mesh.DrawSubset(0);

panelDevice.EndScene();

panelDevice.Present();

this.Invalidate();
}
}
}



Share this post


Link to post
Share on other sites
Well, I tried both , an override of the Panel OnPaint method , and attaching a Render method to the Panel.Paint . Still doesn't paint it right. Maybe I missunderstood what you have written.

Share this post


Link to post
Share on other sites
Great ! Okay , now i have a solid ground to work on . Thanx a lot ! Maybe it's a problem with a device lost state that wasn't taken care of in my code . Because I did nearly the same as in your code except the this.Load . And also my Paint attach was in the constructor.

Again thanks

Share this post


Link to post
Share on other sites
You need to invalidate the the scenePanel not the parent form.

in your render function you need this..

this.scenePanel.Invalidate();

Also I tend to not overrwrite the OnPaint, but simply set the Paint handler for the renderTarget panel in this case its your scenePanel1

this.scenePanel.Paint += PaintEventHandler(Render);

Share this post


Link to post
Share on other sites
I didn'post the other partial class with the windows generated code . There is the declaration of the panel.

Also I saw the Application.DoEvents call in royteusink's code . I get some NullReferenceExceptions after closing the application , in the lien where the world transform is set . Or when I try to move the form it really crashes badly and i have to shut it down with the task manager. Any suggestions which direction i have to search for this one ?

Share this post


Link to post
Share on other sites
In the render function you need to check the clientRect.


Since I use panel.Paint += PaintEventHandler(Render) my Render declaration is like this...

public void Render(object sender, PaintEventArgs e){


//if this window is covered by another window, the panel will basically be gone
//this will throw an exception on every frame (causes severe CPU usage or crash)
//if the clientrect is empty, then simply dont render anything
if (e.ClipRectangle.IsEmpty)
return;




}



Share this post


Link to post
Share on other sites
Quote:
Original post by tokenone
Also I saw the Application.DoEvents call in royteusink's code . I get some NullReferenceExceptions after closing the application


Oops i forgot to remove that one :P

Share this post


Link to post
Share on other sites

This topic is 4112 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this