1. 程式人生 > 實用技巧 >wxWidgets 動態繫結

wxWidgets 動態繫結

#include <wx/wx.h>
#include <wx/glcanvas.h>

class GLCanvas : public wxGLCanvas
{
public:
    GLCanvas(wxWindow* parent, wxGLAttributes & attr)
        :wxGLCanvas(parent, attr)
    {
        wxGLContextAttrs contextAttrs;
        contextAttrs.PlatformDefaults().OGLVersion(4, 0).EndList();

        m_pGLContext = new wxGLContext(this, nullptr, &contextAttrs);

      
        Bind(wxEVT_IDLE, &GLCanvas::OnIdle, this, wxID_ANY);
       
    }

    void OnIdle(wxIdleEvent& event)
    {
        SetCurrent(*m_pGLContext);
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        SwapBuffers();

        event.RequestMore();
    }

private:
    wxGLContext* m_pGLContext;
};


#define BTN_SCALE_ID 5000
class MyApp : public wxApp
{
public:
    virtual bool OnInit()
    {
        wxFrame* frame = new wxFrame(nullptr, wxID_ANY, "opengl");
        wxGLAttributes attr;
        attr.PlatformDefaults().Defaults().DoubleBuffer().EndList();
        wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
        GLCanvas* canvas = new GLCanvas(frame, attr);

        wxBoxSizer* toolSizer = new wxBoxSizer(wxVERTICAL);
        btnRot = new wxButton(frame, BTN_SCALE_ID, wxT("Rot"));

        Bind(wxEVT_BUTTON, &MyApp::OnRot, this, BTN_SCALE_ID);
        btnScale = new wxButton(frame, wxID_ANY, wxT("Scale"));
        toolSizer->Add(btnRot, wxSizerFlags().Expand());
        toolSizer->Add(btnScale, wxSizerFlags().Expand());
        sizer->Add(toolSizer, wxSizerFlags(1).Expand());
        sizer->Add(canvas, wxSizerFlags(5).Expand());

        frame->SetSizer(sizer);
        frame->SetSize(1024, 800);
        frame->Show();


        return true;
    }

    void OnRot(wxCommandEvent & event)
    {

    }
private:
    wxButton* btnRot;
    wxButton* btnScale;
    
};




int main()
{
    MyApp* app = new MyApp;
    wxApp::SetInstance(app);
    return wxEntry();
}