I know this question has been asked before by people in the past. I HAVE looked up and read old threads (the best one being at http://www.allegro.cc/forums/view_thread.php?_id=329755) but even that, although kinda helpful, wasn't enough
. I can't even get it working properly. It compiles and runs but the test image (I made it a .bmp instead of .pcx) doesn't display properly... plus I want to do more (read from dat files and such to make tools for a game that'll actually look alright with a decent GUI).
Anyway, ARE there any resources online SOMEWHERE that I'd be able to use to learn how to integrate and use Allegro with wxWidgets? I just need some code that does somethings I want to do (display bitmaps and read from a dat file) and I should be well off.
Thanks for any help.
According to the wxWidgets wiki here, it should not be too difficult. Not many, as you have seen, have tried merging both.
What does "display properly" means? Can you load other pictures? Post some code, I am too lazy to begin from the begining to test something
Are you using Windows or Linux?
(OT: According to that wiki, Development has been stuck in the same version for years, there are a lack of core developers (original developer is no longer on the team), and there are some internal disputes which may lead to a fork. Someone should go edit there and put the right facts
).
What does "display properly" means?
Look at the attached screenshot. =)
Can you load other pictures?
As in other BMPs? I haven't tried. ... Ok I just tried, same thing happens with other BMPs as you see in the screenshot.
And I'm using Win2K. DevC++ Beta 5(4.9.9.2) with the newest (or close to it) Mingw compiler.
| 1 | #define ALLEGRO_NO_MAGIC_MAIN |
| 2 | #include <allegro.h> |
| 3 | |
| 4 | #ifdef __GNUG__ |
| 5 | #pragma implementation |
| 6 | #pragma interface |
| 7 | #endif |
| 8 | |
| 9 | #include <wx/wx.h> |
| 10 | #include <wx/image.h> |
| 11 | |
| 12 | |
| 13 | #include <stdlib.h> |
| 14 | #include <math.h> |
| 15 | #include <time.h> |
| 16 | |
| 17 | BITMAP *myBitmap = NULL; |
| 18 | |
| 19 | static bool running = FALSE; |
| 20 | static wxMenuBar *menuBar = NULL; |
| 21 | |
| 22 | // Define a new application type |
| 23 | class MyApp: public wxApp |
| 24 | { public: |
| 25 | bool OnInit(); |
| 26 | }; |
| 27 | |
| 28 | IMPLEMENT_APP(MyApp) |
| 29 | |
| 30 | // Define a new frame type |
| 31 | class MyFrame: public wxFrame { |
| 32 | public: |
| 33 | MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size); |
| 34 | |
| 35 | void OnCloseWindow(wxCloseEvent& event); |
| 36 | void OnExit(wxCommandEvent& event); |
| 37 | DECLARE_EVENT_TABLE(); |
| 38 | }; |
| 39 | |
| 40 | // Define a new canvas which can receive some events |
| 41 | class MyCanvas: public wxWindow { |
| 42 | public: |
| 43 | MyCanvas(wxFrame *frame); |
| 44 | void Draw(wxDC& dc); |
| 45 | |
| 46 | private: |
| 47 | void OnPaint(wxPaintEvent& event); |
| 48 | void DrawAllegroBitmap(wxDC& dc, int x1, int y1, int x2, int y2, BITMAP *bmp); |
| 49 | DECLARE_EVENT_TABLE(); |
| 50 | }; |
| 51 | |
| 52 | // `Main program' equivalent, creating windows and returning main app frame |
| 53 | bool MyApp::OnInit() { |
| 54 | // Create the main frame window |
| 55 | MyFrame *frame = new MyFrame(NULL, _T("Allegro BITMAP on wxWindows"), wxPoint(-1, -1), wxSize(160, 240)); |
| 56 | |
| 57 | allegro_init(); |
| 58 | set_color_depth(32); |
| 59 | |
| 60 | myBitmap = load_bitmap("test.bmp",NULL); |
| 61 | |
| 62 | // Make a menubar |
| 63 | wxMenu *file_menu = new wxMenu; |
| 64 | file_menu->Append(wxID_EXIT, _T("E&xit")); |
| 65 | menuBar = new wxMenuBar; |
| 66 | menuBar->Append(file_menu, _T("&File")); |
| 67 | frame->SetMenuBar(menuBar); |
| 68 | |
| 69 | int width, height; |
| 70 | frame->GetClientSize(&width, &height); |
| 71 | |
| 72 | MyCanvas *ref = new MyCanvas(frame); |
| 73 | wxPanel *panel = new wxPanel(ref, -1, wxPoint(0,0), wxSize(160,40),-1,_T("huh?") ); |
| 74 | wxButton *btn1 = new wxButton( panel, wxID_OK, wxT("E&xit") ); |
| 75 | //wxButton *btn2 = new wxButton( panel, wxID_OK, wxT("Foobar") ); |
| 76 | btn1->SetDefault(); |
| 77 | |
| 78 | // Show the frame |
| 79 | frame->Show(TRUE); |
| 80 | panel->Show(TRUE); |
| 81 | return TRUE; |
| 82 | } |
| 83 | |
| 84 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
| 85 | EVT_CLOSE(MyFrame::OnCloseWindow) |
| 86 | EVT_MENU(wxID_EXIT, MyFrame::OnExit) |
| 87 | END_EVENT_TABLE() |
| 88 | |
| 89 | |
| 90 | // My frame constructor |
| 91 | MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size):wxFrame(frame, -1, title, pos, size) { |
| 92 | |
| 93 | } |
| 94 | |
| 95 | // Intercept menu commands |
| 96 | void MyFrame::OnExit(wxCommandEvent& event) { |
| 97 | this->Destroy(); |
| 98 | } |
| 99 | |
| 100 | void MyFrame::OnCloseWindow(wxCloseEvent& event) { |
| 101 | static bool destroyed = FALSE; |
| 102 | if (destroyed) |
| 103 | return; |
| 104 | |
| 105 | this->Destroy(); |
| 106 | |
| 107 | destroyed = TRUE; |
| 108 | } |
| 109 | |
| 110 | BEGIN_EVENT_TABLE(MyCanvas, wxWindow) |
| 111 | EVT_PAINT(MyCanvas::OnPaint) |
| 112 | END_EVENT_TABLE() |
| 113 | |
| 114 | // Define a constructor for my canvas |
| 115 | MyCanvas::MyCanvas(wxFrame *frame): |
| 116 | wxWindow(frame, -1) { |
| 117 | } |
| 118 | |
| 119 | void MyCanvas::OnPaint(wxPaintEvent& event) { |
| 120 | wxPaintDC dc(this); |
| 121 | Draw(dc); |
| 122 | } |
| 123 | |
| 124 | void MyCanvas::Draw(wxDC& dc) { |
| 125 | if (running) return; |
| 126 | |
| 127 | running = TRUE; |
| 128 | menuBar->EnableTop(0, FALSE); |
| 129 | |
| 130 | int Right = 0, Bottom = 0, Left = 0, Top = 0; |
| 131 | GetClientSize(&Right, &Bottom); |
| 132 | DrawAllegroBitmap(dc, Left, Top, Right, Bottom, myBitmap); |
| 133 | menuBar->EnableTop(0, TRUE); |
| 134 | running = FALSE; |
| 135 | } |
| 136 | |
| 137 | void MyCanvas::DrawAllegroBitmap(wxDC& dc, int x1, int y1, int x2, int y2, BITMAP *bmp) { |
| 138 | BITMAP *tmp = create_bitmap(x2-x1, y2-y1); |
| 139 | //stretch_blit(bmp,tmp,0,0,bmp->w, bmp->h, 0,0, tmp->w, tmp->h); |
| 140 | draw_sprite(tmp, bmp, 0, 0); |
| 141 | wxImage img = wxImage(tmp->w, tmp->h, (unsigned char*)tmp->dat, true); |
| 142 | dc.DrawBitmap(img.ConvertToBitmap(),x1,y1,false); |
| 143 | destroy_bitmap(tmp); |
| 144 | } |
This code is the exact code from the thread I posted about in my frist post except for the lines:
//stretch_blit(bmp,tmp,0,0,bmp->w, bmp->h, 0,0, tmp->w, tmp->h); draw_sprite(tmp, bmp, 0, 0);
I just wanted to see if maybe that could have been it but it wasn't. I HAVE tried a few other things too (rather simple ideas) but nothing worked.
As of the OT: I thought the same thing when I read that before, heh.
I wrote a wx widgets and Allegro thing, and it paints the bitmap loaded from allegro on the screen:
allegrowx.cpp
| 1 | #define ALLEGRO_NO_MAGIC_MAIN |
| 2 | #include <allegro.h> |
| 3 | #include <winalleg.h> |
| 4 | |
| 5 | #include <wx/wx.h> |
| 6 | #include <wx/image.h> |
| 7 | #include <wx/dc.h> |
| 8 | |
| 9 | enum |
| 10 | { |
| 11 | Wizard_Quit = 100, |
| 12 | Draw_It = 101 |
| 13 | }; |
| 14 | |
| 15 | void blit_from_allegro_to_wx(BITMAP *src, wxDC *target, int sx, int sy, int dx, int dy, int w, int h) |
| 16 | { |
| 17 | //#ifndef WIN32 |
| 18 | //wxPen *pen; |
| 19 | wxPen pn(wxColour(0,0,0), 1, wxDOT); |
| 20 | //wxColour cl; |
| 21 | for(int x = 0; x < w; ++x) |
| 22 | { |
| 23 | for(int y = 0; y < h; ++y) |
| 24 | { |
| 25 | // make the pen |
| 26 | /* |
| 27 | int color = getpixel(src, sx + x, sy + y); |
| 28 | int r = getr(color); |
| 29 | int g = getg(color); |
| 30 | int b = getb(color); |
| 31 | */ |
| 32 | |
| 33 | //pen = new wxPen(wxColour(r, g, b), 1, wxDOT); |
| 34 | //cl.Set((int **)src->line[y][x] >> 16, ((int **)src->line[y][x] >> 8) & 0xF, (int **)src->line[y][x] & 0xF); |
| 35 | pn.SetColour(((int **)src->line)[y][x] >> 16, (((int **)src->line)[y][x] >> 8) & 0xF, ((int **)src->line)[y][x] & 0xF); |
| 36 | // make it active |
| 37 | target->SetPen(pn); |
| 38 | |
| 39 | // "putpixel" |
| 40 | target->DrawPoint(dx + x, dy + y); |
| 41 | |
| 42 | // ok clear up the pen |
| 43 | //target->SetPen(wxNullPen); |
| 44 | //delete pen; |
| 45 | } |
| 46 | } |
| 47 | //#else |
| 48 | //#endif |
| 49 | } |
| 50 | |
| 51 | class MyApp : public wxApp |
| 52 | { |
| 53 | public: |
| 54 | virtual bool OnInit(); |
| 55 | }; |
| 56 | |
| 57 | class MyFrame : public wxFrame |
| 58 | { |
| 59 | public: |
| 60 | MyFrame(const wxString &title); |
| 61 | void OnDraw(wxCommandEvent &event); |
| 62 | void OnQuit(wxCommandEvent &event); |
| 63 | private: |
| 64 | DECLARE_EVENT_TABLE() |
| 65 | }; |
| 66 | |
| 67 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
| 68 | EVT_MENU(Draw_It, MyFrame::OnDraw) |
| 69 | EVT_MENU(Wizard_Quit, MyFrame::OnQuit) |
| 70 | END_EVENT_TABLE() |
| 71 | |
| 72 | IMPLEMENT_APP(MyApp) |
| 73 | |
| 74 | |
| 75 | bool MyApp::OnInit() |
| 76 | { |
| 77 | |
| 78 | install_allegro(SYSTEM_NONE, &errno, atexit); |
| 79 | set_color_depth(32); |
| 80 | |
| 81 | MyFrame *frame = new MyFrame(_T("Allegro-wxWidgets Text")); // make the frame |
| 82 | |
| 83 | frame->Show(true); // show the frame |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | MyFrame::MyFrame(const wxString &title) : wxFrame((wxFrame *)NULL, -1, title, wxDefaultPosition, wxSize(800,800)) |
| 89 | { |
| 90 | wxMenu *FileMenu = new wxMenu; // make the file menu |
| 91 | FileMenu->Append(Draw_It, _T("&Draw It\tAlt-D"), _("Draw the bitmap")); |
| 92 | FileMenu->Append(Wizard_Quit, _T("E&xit\tAlt-X"), _T("Quit the test program")); |
| 93 | |
| 94 | wxMenuBar *MenuBar = new wxMenuBar(); // make the menu bar |
| 95 | MenuBar->Append(FileMenu, _T("&File")); |
| 96 | |
| 97 | SetMenuBar(MenuBar); // use the menu bar |
| 98 | |
| 99 | CreateStatusBar(); // make the status bar |
| 100 | } |
| 101 | |
| 102 | void MyFrame::OnDraw(wxCommandEvent &WXUNUSED(event)) |
| 103 | { |
| 104 | wxString filename = wxFileSelector(_("Open a BITMAP FILE... like now."), wxT(""), wxT(""), wxT(""), wxT("Bitmap Files|*.bmp")); |
| 105 | if (filename == wxEmptyString) |
| 106 | { |
| 107 | wxMessageBox(_T("You didnt choose a file, silly, try again (File -> Draw)"), _T(":p"), wxOK); |
| 108 | return ; |
| 109 | } |
| 110 | BITMAP *testbmp = load_bitmap(filename.c_str(), NULL); |
| 111 | if(testbmp == NULL) |
| 112 | { |
| 113 | wxMessageBox(_T("Couldn't load iamge"), _T("Error"), wxICON_ERROR | wxOK); |
| 114 | return; |
| 115 | } |
| 116 | // void blit_to_hdc(BITMAP *bitmap, HDC dc, int sx, sy, dx, dy, w, h); |
| 117 | /*PAINTSTRUCT ps; |
| 118 | HDC hdc; |
| 119 | HWND hwnd = (HWND)GetHandle(); |
| 120 | hdc = BeginPaint(hwnd, &ps); |
| 121 | blit_to_hdc(testbmp, hdc, 0, 0, 0, 0, testbmp->w, testbmp->h); |
| 122 | EndPaint(hwnd, &ps);*/ |
| 123 | |
| 124 | /*wxScreenDC *bdest = new wxScreenDC; |
| 125 | int x, y; |
| 126 | GetPosition(&x, &y); |
| 127 | //blit_from_allegro_to_wx(testbmp, bdest, 0, 0, x+10, y+10, testbmp->w, testbmp->h); |
| 128 | delete bdest;*/ |
| 129 | |
| 130 | wxClientDC *bdest = new wxClientDC(this); |
| 131 | blit_from_allegro_to_wx(testbmp, bdest, 0, 0, 0, 0, testbmp->w, testbmp->h); |
| 132 | delete bdest; |
| 133 | |
| 134 | destroy_bitmap(testbmp); |
| 135 | } |
| 136 | |
| 137 | void MyFrame::OnQuit(wxCommandEvent &WXUNUSED(event)) |
| 138 | { |
| 139 | Close(true); // close the app |
| 140 | } |
I'm at work right now so I can't try out that code but when I get home I will. Thanks for the help and I'll be back if I need it.