code style

3/16/2007

Using DirectX with wxWidget

wxWidgets A.K.A. wxWindow is a cross-platform toolkit for creating window application. There is a class called wxGLCanvas for OpenGL, but no information for DirectX. Then, I try to integrate DirectX with wxWidgets. I found flick will happen when I use paint event to draw d3d frame. To avoid from flicking, I use erase event to clear the background of window and redraw d3d frame. Here is the pseudo code:

class MyApp : public wxApp {
virtual bool OnInit();
virtual int OnExit();
};
class MyViewport : public wxPanel {
public:
MyViewport(wxWindow* parent);
void OnIdle(wxIdleEvent& event);
void OnDraw(wxEraseEvent& event);
private:
DECLARE_EVENT_TABLE()
};

class MyFrame: public wxFrame {
MyViewport *mpViewport;
public:
MyFrame(const wxString& title);
private:
DECLARE_EVENT_TABLE()
};

// the event tables connect the wxWidgets events with the functions (event
// handlers) which process them. It can be also done at run-time, but for the
// simple menu events like this the static method is much simpler.
BEGIN_EVENT_TABLE(MyViewport, wxPanel)
EVT_ERASE_BACKGROUND (MyViewport::OnDraw)
EVT_IDLE (MyViewport::OnIdle)
END_EVENT_TABLE()

// Create a new application object: this macro will allow wxWidgets to create
// the application object during program execution (it's better than using a
// static object for many reasons) and also implements the accessor function
// wxGetApp() which will return the reference of the right type (i.e. MyApp and not wxApp)
IMPLEMENT_APP(MyApp)

// 'Main program' equivalent: the program execution "starts" here
bool MyApp::OnInit() {
// create the main application window
MyFrame *frame = new MyFrame( _T("D3D sample") );

create D3D device here

frame->Show(true);
SetTopWindow(frame);
return true;
}

int MyApp::OnExit() {
release D3D device here

return wxApp::OnExit();
}

// frame constructor
MyFrame::MyFrame(const wxString& title) :
wxFrame(NULL, -1, title, wxPoint(0,0)), mpViewport(NULL), mpTreeCtrl(NULL)
{
mpViewport = new MyViewport(this);
}

MyViewport::MyViewport(wxWindow* parent) : wxPanel(parent) {
}

void MyViewport::OnIdle(wxIdleEvent& event) {
update your d3d objects here

// call Refresh() to send a wxEraseEvent to redraw a frame
Refresh();
}

void MyViewport::OnDraw(wxEraseEvent& event) {
g_pd3dDevice->BeginScene();

draw your d3d objects here

g_pd3dDevice->EndScene();
g_pd3dDevice->Present(NULL,NULL, this->GetHWND() ,NULL);
}

4 則留言:

Kim, Hyoun Woo 提到...

Nice :-)

匿名 提到...

Superb post but I was wanting to know if you could write a litte more
on this topic? I'd be very thankful if you could elaborate a little bit more. Cheers!

Feel free to visit my blog 2001 honda s2000 hardtop for sale

匿名 提到...

Terrific work! This is the type of information
that are meant to be shared around the net.
Disgrace on Google for not positioning this publish upper!

Come on over and discuss with my web site . Thank you =)

Also visit my webpage ... mazda rx8 body kits

匿名 提到...

I believe what you said made a ton of sense. However, what about
this? what if you were to create a killer post title?
I ain't saying your content is not solid., but what if you added a title that makes people want more? I mean "Using DirectX with wxWidget" is a little plain. You should glance at Yahoo's
home page and watch how they create article titles to grab viewers to open the links.
You might add a video or a pic or two to grab readers interested about what you've got to say. Just my opinion, it might bring your posts a little bit more interesting.

Here is my homepage home cellulite treatment