Reasons for using Allegro for your wx application gfx
- graphics are a lot faster than when you'd use wx equivalent functions.
- easy to add AllegroGL (it's just one include after allegro.h) to add OpenGL hardware acceleration, and there are tons of other extensions for Allegro.
- allegro has a very clean and well documented manual/API.
- allegro is also multi-platform, although I've only tested this on windows.
Sample project
Attached is a sample project with two wxStaticBitmap controls. I've included the source, Visual Studio 2010 and DialogBlocks project files, could not include the binaries (attachment was not allowed due to filesize). I used DialogBlocks for my wxWidgets stuff. This code also works with the DigitalMars Compiler, mingw, WATCOM, all tested under Windows. Maybe I will upload binary later to my website and post the URL.
I use the OnCreate() event to initialize allegro, and OnIdle() to start rendering the next frame. You might not need continuous redrawing, but in my demo I created a silly animation on one canvas (wxStaticBitmapCanvas), but another smaller canvas (wxStaticBitmapSmallCanvas) is only redrawn when necessary.
I made that animation a little bit interactive with the mouse to demonstrate how you can use the mouse on the canvas, etc.
The required code summarized
Include allegro first like this.
Code: Select all
#define ALLEGRO_NO_MAGIC_MAIN
#define RGB AL_RGB
#include <allegro.h>
#include <winalleg.h>
#undef RGB
Code: Select all
install_allegro(SYSTEM_NONE, &errno, NULL);
set_palette(desktop_palette); // example
set_color_depth(32); // example
Code: Select all
wxPaintDC dc(wxDynamicCast(event.GetEventObject(), wxWindow)); // you probably already have the wxPaintDC
// Found the following code by searching for a very long time in the wxWidgets source code ;)
WXHDC wxHDC = wxPaintDC::FindDCInCache((wxWindow*) event.GetEventObject());
HDC hDC = (HDC) wxHDC;
... // do your thing here
draw_to_hdc(hDC, allegroBitmap, 0, 0); // where allegroBitmap is a valid "BITMAP *"
I'd really appreciate a reply if this was useful for you, I'm also curious if there's anybody else using allegro combined with wxWidgets.
Also feedback on my C++ code is greatly appreciated, like if I could use std::vector more efficiently I'd like to know how.