I'm currently developping a game-like application by using Allegro. But I dislike the UI made by Allegro. So I decide to use wxWidgets. I managed to integrated Allegro into a wxAllegroWin class which inherits from wxWindow. Then I add this window into a frame. It seems that everything's fine until I found I get I can't catch EVT_PAINT event for the wxAllegroWin. Here is my source code for wxAllegroWin:
wxallegrowin.h
---------------------------------------------------
Code: Select all
#ifndef _WX_ALLEGRO_WIN_H_
#define _WX_ALLEGRO_WIN_H_
#define ALLEGRO_NO_MAGIC_MAIN
#define RGB AL_RGB
#define BITMAP AL_BITMAP
#include <allegro.h>
#include <winalleg.h>
#undef BITMAP
#undef RGB
#include <wx/window.h>
class wxAllegWin : public wxWindow
{
public:
wxAllegWin(wxWindow* parent, wxWindowID id,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = 0,
const wxString& name = "wxAllegWin");
virtual ~wxAllegWin();
/**
* Initialization of allegro
*/
bool Install();
void Uninstall();
protected:
private:
};
#endif // _WX_ALLEGRO_WIN_H_
wxallegrowin.cpp:
--------------------------------------------
#include "wxallegwin.h"
wxAllegWin::wxAllegWin(wxWindow* parent, wxWindowID id,
const wxPoint& pos,const wxSize& size,long style,const wxString& name)
: wxWindow(parent,id,pos,size,style,name)
{
}
wxAllegWin::~wxAllegWin()
{
}
bool wxAllegWin::Install()
{
#ifdef __GNUWIN32__
win_set_window((HWND)GetHandle());
#endif
if (allegro_init() != 0)
{
return false;
}
//install_keyboard();
int width,height;
GetSize(&width, &height);
if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, width, height, 0, 0) != 0)
{
return false;
}
return true;
}
void wxAllegWin::Uninstall()
{
allegro_exit();
}
main.h:
---------------------------------------
#ifndef MAIN_H
#define MAIN_H
#include "app.h"
#include "wxallegwin.h"
class MyWindow : public wxAllegWin
{
public:
MyWindow(wxWindow* parent, wxWindowID id,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = 0,
const wxString& name = "MyWindow")
: wxAllegWin(parent,id,pos,size,style,name)
{
}
virtual ~MyWindow() {}
void SayHello();
private:
void OnPaint(wxPaintEvent& event);
DECLARE_EVENT_TABLE();
};
class MyFrame: public wxFrame
{
public:
MyFrame(wxFrame *frame, const wxString& title);
~MyFrame();
private:
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void OnPaint(wxPaintEvent& event);
DECLARE_EVENT_TABLE();
MyWindow* m_pMyWin;
};
#endif // MAIN_H
main.cpp:
-------------------------------------------
#include "main.h"
#include "wxallegwin.h"
int idMenuQuit = wxNewId();
int idMenuAbout = wxNewId();
BEGIN_EVENT_TABLE(MyWindow, wxAllegWin)
EVT_PAINT(MyWindow::OnPaint)
END_EVENT_TABLE()
void MyWindow::OnPaint(wxPaintEvent& event)
{
set_palette(desktop_palette);
clear_to_color(screen, makecol(255, 255, 255));
acquire_screen();
textout_centre_ex(screen, font, "Hello, world!", SCREEN_W/2, SCREEN_H/2, makecol(0,0,0), -1);
release_screen();
event.Skip();
}
void MyWindow::SayHello()
{
set_palette(desktop_palette);
clear_to_color(screen, makecol(255, 255, 255));
acquire_screen();
textout_centre_ex(screen, font, "Hello, world!", GetSize().GetWidth()/2,
GetSize().GetHeight()/2, makecol(255,0,255), -1);
release_screen();
}
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(idMenuQuit, MyFrame::OnQuit)
EVT_MENU(idMenuAbout, MyFrame::OnAbout)
EVT_PAINT(MyFrame::OnPaint)
END_EVENT_TABLE()
MyFrame::MyFrame(wxFrame *frame, const wxString& title)
: wxFrame(frame, -1, title)
{
wxMenuBar* mbar = new wxMenuBar();
wxMenu* fileMenu = new wxMenu(_(""));
fileMenu->Append(idMenuQuit, _("&Quit\tAlt-F4"), _("Quit the application"));
mbar->Append(fileMenu, _("&File"));
wxMenu* helpMenu = new wxMenu(_(""));
helpMenu->Append(idMenuAbout, _("&About\tF1"), _("Show info about this application"));
mbar->Append(helpMenu, _("&Help"));
SetMenuBar(mbar);
m_pMyWin = new MyWindow(this,-1,wxPoint(0,0),GetSize());
m_pMyWin->Install();
wxStaticBox* sb = new wxStaticBox(this,-1,wxT(""),wxPoint(0,0),wxSize(91,21));
sb->SetTitle(wxT("test"));
wxBoxSizer* bs = new wxBoxSizer(wxVERTICAL);
bs->Add(m_pMyWin,1,wxTOP|wxLEFT|wxBOTTOM|wxRIGHT|wxALIGN_CENTER|wxEXPAND|wxFIXED_MINSIZE,1);
bs->Add(sb,1,wxTOP|wxLEFT|wxBOTTOM|wxRIGHT|wxEXPAND|wxFIXED_MINSIZE,1);
SetSizer(bs);
SetAutoLayout(true);
Layout();
}
MyFrame::~MyFrame()
{
m_pMyWin->Uninstall();
delete m_pMyWin;
}
void MyFrame::OnQuit(wxCommandEvent& event)
{
Close();
}
void MyFrame::OnAbout(wxCommandEvent& event)
{
m_pMyWin->SayHello();
}
void MyFrame::OnPaint(wxPaintEvent& event)
{
// m_pMyWin->SayHello();
event.Skip();
}