wxWidgets and Allegro

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
zhouhao
Earned some good credits
Earned some good credits
Posts: 144
Joined: Tue Dec 06, 2005 7:02 am

wxWidgets and Allegro

Post by zhouhao »

I've posted a simular message to forum of www.allegro.cc and I'd also like to try my luck here to see if I can get any help.

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();
}
According to Allegro's document, it should not filter out any messages from windows. Does anybody have any idea about this?
User avatar
Ryan Norton
wxWorld Domination!
wxWorld Domination!
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post by Ryan Norton »

There are several things you could try:

1) Try connecting dynamically with the paint event to make sure there isn't something silently messed up with the event table -

mywindow->Connect(mywindow->GetId(), wxEVT_PAINT, wxPaintEventHandler(MyWindow::OnPaint()));

2) Debug and set a breakpoint in wxWindow::MSWWindowProc - make sure it's being called. If it's not then allegro has grabbed it's own WndProc and isn't calling the wx one.

3) Debug in MSWWindowProc in WM_PAINT message and trace to see if OnPaint/your event is getting called etc.
[Mostly retired moderator, still check in to clean up some stuff]
Post Reply