Here is the description about win_set_window function:
" ...
in particular, you are responsible for processing the events as usual (Allegro will automatically monitor a few of them, but will not filter out any of them).
..."
I'm using this function in a wxWidget application. Inside the constructor of a wxFrame, I passed the HWND handler to win_set_window. Unfortunately it seems the EVT_PAINT event will not triggered anymore. Does anybody know the reason?
By the way, if I want to port my application to other platforms, are there any simular function like win_set_window in other platforms ( for example linux, max ox )?
Nope. Once you use win_set_window, you're stuck with Windows. Also, you're going to have to show us some code describing the problem.
Here is part of my code:
//-------------------------------
//app.cpp:
//-------------------------------
#include "app.h"
#include "wxallegroframe.h"
IMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
wxAllegroFrame* frame = new wxAllegroFrame(NULL,0L,NULL);
frame->Show();
return true;
}
//------------------------------------
//wxallegroframe.h:
//------------------------------------
#ifndef WXALLEGROFRAME_H
#define WXALLEGROFRAME_H
#include <wx/wxprec.h>
#ifdef _BORLANDC_
#pragma hdrstop
#endif
#include <wx/frame.h>
#include <wx/intl.h>
#include <wx/panel.h>
#include <wx/sizer.h>
#include <wx/toolbar.h>
class CAllegroWin;
class wxAllegroFrame: public wxFrame
{
public:
wxAllegroFrame(wxWindow* parent,wxWindowID id,const char* data);
virtual ~wxAllegroFrame();
enum Identifiers
{
ID_ALLEG_PN = 0x1000,
ID_ALTOOLBAR_PN,
ID_FRAME_ALLEG
};
void OnClose(wxCloseEvent& event);
void OnPaint(wxPaintEvent& event);
private:
wxToolBar* m_tbPlay;
CAllegroWin* m_pAllegWin;
DECLARE_EVENT_TABLE()
};
#endif
//---------------------------------
//wxallegrofrrame.cpp:
//---------------------------------
#include "allegrowin.h"
#include "wxallegroframe.h"
BEGIN_EVENT_TABLE(wxAllegroFrame,wxFrame)
EVT_CLOSE(wxAllegroFrame::OnClose)
EVT_PAINT(wxAllegroFrame::OnPaint)
END_EVENT_TABLE()
wxAllegroFrame::wxAllegroFrame(wxWindow* parent,wxWindowID id,const char* data)
{
Create(parent,id,_("Game Player"),wxDefaultPosition,
wxDefaultSize,wxDEFAULT_FRAME_STYLE|wxFRAME_TOOL_WINDOW );
SetClientSize(640,480);
HANDLER handler = (HANDLER)GetHandle();
int width,height;
GetClientSize(&width, &height);
m_pAllegWin = new CAllegroWin(handler,width,height,data);
m_tbPlay = CreateToolBar(wxTB_HORIZONTAL,-1);
}
wxAllegroFrame::~wxAllegroFrame()
{
}
void wxAllegroFrame::OnPaint(wxPaintEvent& event)
{
m_pAllegWin->showText();
}
void wxAllegroFrame::OnClose(wxCloseEvent& event)
{
delete m_pAllegWin;
Destroy();
}
//-----------------------------------
// allegrowin.h
//-----------------------------------
#ifndef CALLEGROWIN_H
#define CALLEGROWIN_H
#include "pre.h"
#ifdef _WXMSW_
typedef HWND HANDLER;
#endif
class CAllegroWin
{
public:
CAllegroWin(HANDLER handler,int w,int h);
virtual ~CAllegroWin();
void showText();
private:
HANDLER m_handler;
};
#endif // CALLEGROWIN_H
//-----------------------------------
//allegrowin.cpp:
//-----------------------------------
#include "allegrowin.h"
CAllegroWin::CAllegroWin(HANDLER handler,int w,int h)
:m_handler(handler)
{
//ctor
#ifdef _GNUWIN32_
win_set_window(handler);
#endif
if (allegro_init() != 0)
{
// TODO: throw exception
}
install_keyboard();
if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, w, h, 0, 0) != 0)
{
allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
// TODO: throw exception
}
}
CAllegroWin::~CAllegroWin()
{
allegro_exit();
}
void CAllegroWin::showText()
{
/* set the color palette */
set_palette(desktop_palette);
/* clear the screen to white */
clear_to_color(screen, makecol(255, 255, 255));
/* you don't need to do this, but on some platforms (eg. Windows) things
* will be drawn more quickly if you always acquire the screen before
* trying to draw onto it.
*/
acquire_screen();
/* write some text to the screen with black letters and transparent background */
textout_centre_ex(screen, font, "Hello, world!", SCREEN_W/2, SCREEN_H/2, makecol(0,0,0), -1);
/* you must always release bitmaps before calling any input functions */
release_screen();
}
I can catch the OnClose event without problem. But I can't catch OnPaint event.
I can't read that, sorry. Please use [code] tags... See http://www.allegro.cc/mockup.html. Edit your post to add that and I'll try again
I repost my source code.
| 1 | //------------------------------- |
| 2 | //app.cpp: |
| 3 | //------------------------------- |
| 4 | #include "app.h" |
| 5 | #include "wxallegroframe.h" |
| 6 | |
| 7 | IMPLEMENT_APP(MyApp); |
| 8 | |
| 9 | bool MyApp::OnInit() |
| 10 | { |
| 11 | wxAllegroFrame* frame = new wxAllegroFrame(NULL,0L,NULL); |
| 12 | frame->Show(); |
| 13 | return true; |
| 14 | } |
| 15 | |
| 16 | //------------------------------------ |
| 17 | //wxallegroframe.h: |
| 18 | //------------------------------------ |
| 19 | #ifndef WXALLEGROFRAME_H |
| 20 | #define WXALLEGROFRAME_H |
| 21 | |
| 22 | #include <wx/wxprec.h> |
| 23 | |
| 24 | #ifdef __BORLANDC__ |
| 25 | #pragma hdrstop |
| 26 | #endif |
| 27 | |
| 28 | #include <wx/frame.h> |
| 29 | #include <wx/intl.h> |
| 30 | #include <wx/panel.h> |
| 31 | #include <wx/sizer.h> |
| 32 | #include <wx/toolbar.h> |
| 33 | |
| 34 | class CAllegroWin; |
| 35 | |
| 36 | class wxAllegroFrame: public wxFrame |
| 37 | { |
| 38 | public: |
| 39 | wxAllegroFrame(wxWindow* parent,wxWindowID id,const char* data); |
| 40 | virtual ~wxAllegroFrame(); |
| 41 | |
| 42 | enum Identifiers |
| 43 | { |
| 44 | ID_ALLEG_PN = 0x1000, |
| 45 | ID_ALTOOLBAR_PN, |
| 46 | ID_FRAME_ALLEG |
| 47 | }; |
| 48 | |
| 49 | void OnClose(wxCloseEvent& event); |
| 50 | void OnPaint(wxPaintEvent& event); |
| 51 | |
| 52 | private: |
| 53 | wxToolBar* m_tbPlay; |
| 54 | CAllegroWin* m_pAllegWin; |
| 55 | |
| 56 | DECLARE_EVENT_TABLE() |
| 57 | }; |
| 58 | |
| 59 | #endif |
| 60 | |
| 61 | //--------------------------------- |
| 62 | //wxallegrofrrame.cpp: |
| 63 | //--------------------------------- |
| 64 | #include "allegrowin.h" |
| 65 | #include "wxallegroframe.h" |
| 66 | |
| 67 | BEGIN_EVENT_TABLE(wxAllegroFrame,wxFrame) |
| 68 | EVT_CLOSE(wxAllegroFrame::OnClose) |
| 69 | EVT_PAINT(wxAllegroFrame::OnPaint) |
| 70 | END_EVENT_TABLE() |
| 71 | |
| 72 | wxAllegroFrame::wxAllegroFrame(wxWindow* parent,wxWindowID id,const char* data) |
| 73 | { |
| 74 | Create(parent,id,_("Game Player"),wxDefaultPosition, |
| 75 | wxDefaultSize,wxDEFAULT_FRAME_STYLE|wxFRAME_TOOL_WINDOW ); |
| 76 | |
| 77 | SetClientSize(640,480); |
| 78 | |
| 79 | HANDLER handler = (HANDLER)GetHandle(); |
| 80 | |
| 81 | int width,height; |
| 82 | GetClientSize(&width, &height); |
| 83 | m_pAllegWin = new CAllegroWin(handler,width,height,data); |
| 84 | m_tbPlay = CreateToolBar(wxTB_HORIZONTAL,-1); |
| 85 | } |
| 86 | |
| 87 | wxAllegroFrame::~wxAllegroFrame() |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | void wxAllegroFrame::OnPaint(wxPaintEvent& event) |
| 92 | { |
| 93 | m_pAllegWin->showText(); |
| 94 | } |
| 95 | |
| 96 | void wxAllegroFrame::OnClose(wxCloseEvent& event) |
| 97 | { |
| 98 | delete m_pAllegWin; |
| 99 | Destroy(); |
| 100 | } |
| 101 | |
| 102 | //----------------------------------- |
| 103 | // allegrowin.h |
| 104 | //----------------------------------- |
| 105 | #ifndef CALLEGROWIN_H |
| 106 | #define CALLEGROWIN_H |
| 107 | |
| 108 | #include "pre.h" |
| 109 | |
| 110 | #ifdef __WXMSW__ |
| 111 | typedef HWND HANDLER; |
| 112 | #endif |
| 113 | |
| 114 | class CAllegroWin |
| 115 | { |
| 116 | public: |
| 117 | CAllegroWin(HANDLER handler,int w,int h); |
| 118 | virtual ~CAllegroWin(); |
| 119 | |
| 120 | void showText(); |
| 121 | |
| 122 | private: |
| 123 | HANDLER m_handler; |
| 124 | }; |
| 125 | |
| 126 | #endif // CALLEGROWIN_H |
| 127 | |
| 128 | //----------------------------------- |
| 129 | //allegrowin.cpp: |
| 130 | //----------------------------------- |
| 131 | #include "allegrowin.h" |
| 132 | |
| 133 | CAllegroWin::CAllegroWin(HANDLER handler,int w,int h) |
| 134 | :m_handler(handler) |
| 135 | { |
| 136 | #ifdef __GNUWIN32__ |
| 137 | win_set_window(handler); |
| 138 | #endif |
| 139 | |
| 140 | if (allegro_init() != 0) |
| 141 | { |
| 142 | // TODO: throw exception |
| 143 | } |
| 144 | |
| 145 | install_keyboard(); |
| 146 | |
| 147 | if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, w, h, 0, 0) != 0) |
| 148 | { |
| 149 | allegro_message("Unable to set any graphic mode\n%s\n", allegro_error); |
| 150 | // TODO: throw exception |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | CAllegroWin::~CAllegroWin() |
| 155 | { |
| 156 | allegro_exit(); |
| 157 | } |
| 158 | |
| 159 | void CAllegroWin::showText() |
| 160 | { |
| 161 | set_palette(desktop_palette); |
| 162 | |
| 163 | /* clear the screen to white */ |
| 164 | clear_to_color(screen, makecol(255, 255, 255)); |
| 165 | |
| 166 | acquire_screen(); |
| 167 | |
| 168 | textout_centre_ex(screen, font, "Hello, world!", SCREEN_W/2, SCREEN_H/2, makecol(0,0,0), -1); |
| 169 | |
| 170 | release_screen(); |
| 171 | |
| 172 | } |
Hmm, looks interesting, I'll have to check into that later, mail frank.
I've changed the program a bit. Now I'm passing wxWindow handle to Allegro, then I add the window into a frame. Inside the frame, you even can add some other panels. Through the "OnPaint" event in frame or menu item "about", you can see "Hello, World!" displayed by Allegro. But still there is nothing happen in wxWindow's "OnPaint" event, it seems Allegro filter out the "OnPaint" event.
Please refer to my attachment for all my source code for testing.
There was a thread about WM_PAINT message not being sent. IIRC Windows won't send that message unless window gets changed in way they can detect it (minimized and restored, overlapped by other window, etc). Try triggering OnPaint event manually.
There is no WM_PAINT message even when I resize the window.