Skip to content

Instantly share code, notes, and snippets.

@ousttrue
Created April 1, 2009 23:57
Show Gist options
  • Save ousttrue/88954 to your computer and use it in GitHub Desktop.
Save ousttrue/88954 to your computer and use it in GitHub Desktop.
// CFLAGS=`wx-config --cflags`
// LDFLAGS=`wx-config --libs gl`
#ifndef _FRAME_H
#define _FRAME_H
#include <wx/wx.h>
#include <wx/glcanvas.h>
#ifdef _MSC_VER
#pragma comment(lib, "opengl32.lib")
#endif
class OpenGL
{
public:
void initialize()
{
glClearColor(0, 0, 1, 1);
}
void setViewport(int w, int h)
{
glViewport(0, 0, (GLint)w, (GLint)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glFrustum(-0.5f, 0.5f, -0.5f, 0.5f, 0.1f, 100.0f);
glOrtho(-w / 200.0, w / 200.0, -h / 200.0, h / 200.0, -1.0, 1.0);
}
void draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
// view
glLoadIdentity();
glBegin(GL_POLYGON);
glColor3d(1.0, 0.0, 0.0);
glVertex2d(-0.9, -0.9);
glColor3d(0.0, 1.0, 0.0);
glVertex2d(0.9, -0.9);
glColor3d(0.0, 0.0, 1.0);
glVertex2d(0.9, 0.9);
glColor3d(1.0, 1.0, 0.0);
glVertex2d(-0.9, 0.9);
glEnd();
}
};
class MyPanel : public wxGLCanvas
{
bool isInitialized_;
OpenGL gl_;
public:
MyPanel(wxWindow* parent, wxGLContext* sharedContext, wxWindowID id)
: wxGLCanvas(parent, sharedContext, id), isInitialized_(false)
{
Connect(wxEVT_SIZE
, wxSizeEventHandler(MyPanel::OnSize));
Connect(wxEVT_PAINT
, wxPaintEventHandler(MyPanel::OnPaint));
Connect(wxEVT_ERASE_BACKGROUND
, wxEraseEventHandler(MyPanel::OnEraseBackground));
}
void OnPaint(wxPaintEvent &event)
{
wxPaintDC dc(this);
if(!GetContext()){
return;
}
if(!isInitialized_){
SetCurrent();
gl_.initialize();
int w;
int h;
GetClientSize(&w, &h);
gl_.setViewport(w, h);
isInitialized_=true;
}
gl_.draw();
glFlush();
SwapBuffers();
}
void OnSize(wxSizeEvent &event)
{
wxGLCanvas::OnSize(event);
if(GetContext())
{
SetCurrent();
gl_.setViewport(event.GetSize().GetWidth(), event.GetSize().GetHeight());
Refresh();
}
}
void OnEraseBackground(wxEraseEvent& event)
{
// Do nothing, to avoid flashing.
}
};
class TopFrame : public wxFrame
{
MyPanel *panel_;
wxMenuBar *menubar_;
wxMenu *file_;
public:
TopFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title)
{
menubar_ = new wxMenuBar;
file_ = new wxMenu;
file_->Append(wxID_OPEN, wxT("&Open"));
file_->Append(wxID_EXIT, wxT("&Quit"));
menubar_->Append(file_, wxT("&File"));
SetMenuBar(menubar_);
panel_=new MyPanel(this, NULL, wxID_ANY);
Connect(wxID_OPEN, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(TopFrame::OnOpen));
Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(TopFrame::OnQuit));
}
void OnOpen(wxCommandEvent& WXUNUSED(event))
{
wxString filename = wxFileSelector(
wxT("Choose a file to open")
, wxT("")
, wxT("")
, wxT("mqo")
, wxT("MQO files (*.mqo)|*.mqo")
);
if (filename.empty())
{
return;
}
// open
}
void OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(true);
}
};
#endif // _FRAME_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment