Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Getting Started

This thread is locked; no one can reply to it. rss feed Print
Getting Started
Neil Black
Member #7,867
October 2006
avatar

I'm working my way through the Allegro tutorials and every time I try to call al_draw_bitmap() the program crashes. I'm not sure why. After some further testing I've found that drawing primitives also crashes the program.

I've created a bitmap and set it as the target bitmap, I'm not sure what else needs to be done before I can draw to it.

Ariesnl
Member #2,902
November 2002
avatar

post your code...

- Wisdom is the art of using knowledge
- String theory: There's music in everything

Arthur Kalliokoski
Second in Command
February 2005
avatar

I betcha a weeks supply of Pepsi(TM)(Nectar of the Godz)that you forgot to do al_init_image_addon(void).

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Chris Katko
Member #1,881
January 2002
avatar

I'm really starting to wonder if Allegro 5+/"6" should have a different API mentality. Ever since I learned about "the common case should be default/easy" for data-oriented design, it really makes sense. In my opinion, everything in Allegro should be defaulted unless you opt out.

allegro_init() should give you every common case "for free". (Or at least have one new function that does that.) Graphics (at default desktop depth), primitives, jpeg/png/whatever input, sound, a single event queue with the mouse/keyboard/joysticks attached, and even a FPS timer occurring once a second with a variable you can send to print (or even have one that hijacks onto al_flip_display and overlays it automatically in the corner).

And, when you want something different than the default, you override those defaults. Default depth is desktop depth, until you manually ask for it. etc.

This would likely be easier in C++ with default arguments, but perhaps with some kind of C method/functioning chaining it could be done.

The people who know what they're doing, are used to writing lines of code for "on the metal" features. The people who don't, should already be in an environment that lets them play around instead of a crash. Maybe the color depth "is wrong", and they can google it/ask about it. But there will still be pixels on the screen which will help them diagnose the problem.

Also, one great thing about D (not suggesting we port it for everyone) is the built-in asserts that allow you to pack a message in. In debug mode, asserts catch a file missing, or a addon not initialized, and halts the program. But you also get a custom message with the assert. "You failed to initialize the addon!" Instead of just a line number in a piece of code. Maybe C supports that (with macros?), but IIRC it doesn't.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs

Edgar Reynaldo
Member #8,592
May 2007
avatar

Eagle 5 does all this for you. It's super simple, and super easy. Here's the code it takes to display Hello World :

#SelectExpand
1#include "Eagle/backends/Allegro5Backend.hpp" 2 3int hello_main(int argc , char** argv) { 4 5 (void)argc; 6 (void)argv; 7 8 Allegro5System* a5sys = GetAllegro5System(); 9 10 a5sys->Initialize(EAGLE_FULL_SETUP); 11 12 EagleGraphicsContext* win = a5sys->GetWindowManager()->CreateWindow("hello_main::win" , 800 , 600 , EAGLE_OPENGL | EAGLE_WINDOWED); 13 14 win->Clear(EagleColor(0,0,0)); 15 16 win->DrawTextString(win->DefaultFont() , "Hello World" , win->Width()/2 , win->Height()/2 , EagleColor(255,255,255) , HALIGN_CENTER , VALIGN_CENTER); 17 18 win->FlipDisplay(); 19 20 a5sys->Rest(3); 21 22 Eagle::EagleLibrary::ShutdownEagle(); 23 24 return 0; 25}

Of course, this is overkill for hello world, but the thing is, it sets up and installs and registers every single input and addon there is for you without having to do it over and over again.

There is a default system timer, queue, input handler, and window manager that does pretty much everything for you.

beoran
Member #12,636
March 2011

I think an official add-on with such an "easy" API would be a nice addition to Allegro5. Even with just C, there is quite a bit we could do.

Neil Black
Member #7,867
October 2006
avatar

Edgar that Eagle 5 thing looks interesting. Where can I read up on it?

Here's my code, which I'm modifying from a basic A5 sample I found somewhere. I did in fact do al_init_image_addon() on line 22. Or rather, the sample did.

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_native_dialog.h> 3#include <allegro5/allegro_image.h> 4#include <allegro5/allegro_primitives.h> 5 6int main() 7{ 8 ALLEGRO_DISPLAY *display; 9 10 if(!al_init()) 11 { 12 al_show_native_message_box(NULL,NULL,NULL,"Could not initialize Allegro 5", NULL, 0); 13 } 14 15 display = al_create_display(800,600); 16 17 if(!display) 18 { 19 al_show_native_message_box(display, "Sample Title", "Display Settings", "Display Window Was Not Created", NULL, 0); 20 } 21 22 if(!al_init_image_addon()) 23 { 24 al_show_native_message_box(NULL,NULL,NULL,"Could not initialize Image Addon", NULL, 0); 25 } 26 27 al_install_keyboard(); 28 29 ALLEGRO_BITMAP* Square = NULL; 30 ALLEGRO_BITMAP* Screen = NULL; 31 32 ALLEGRO_COLOR Black = al_map_rgb(0,0,0); 33 34 Screen = al_create_bitmap(800,600); 35 Square = al_load_bitmap("Square.bmp"); 36 37 al_set_target_bitmap(Screen); 38 39 //al_clear_to_color(Black); 40 41 ALLEGRO_EVENT_QUEUE* Queue = al_create_event_queue(); 42 ALLEGRO_EVENT_SOURCE* KeySource = al_get_keyboard_event_source(); 43 al_register_event_source(Queue, KeySource); 44 45 al_wait_for_event(Queue, NULL); 46 47 al_show_native_message_box(NULL,NULL,NULL,"Program Complete", NULL, 0); 48 49 //al_draw_rectangle(20, 20, 100, 100, al_map_rgb(255,255,255), 10); 50 51 //al_draw_bitmap(Square, 20, 20, ALLEGRO_FLIP_HORIZONTAL); 52 53 54 55 al_destroy_display(display); 56 57 return 0; 58}

Chris Katko
Member #1,881
January 2002
avatar

You don't check if Square is null.

It you're on Linux, filenames are case sensitive too so make sure the name AND the extension match both in letters and case.

And if you're on windows, sometimes the "Base" directory, isn't the one with your files. It could be my_project/bin/ or some other directory. It's called the "Working directory" in Windows.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs

Neil Black
Member #7,867
October 2006
avatar

The problem persists even if I comment out all the references to the Square variable.

Chris Katko
Member #1,881
January 2002
avatar

Are you sure running with the same version of the library you linked with? I had problems where they didn't match before and it exploded. Try compiling monolithic so there's no DLL/SO.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs

Elias
Member #358
May 2000

al_init_primitives_addon is required for al_draw_rectangle

--
"Either help out or stop whining" - Evert

Neil Black
Member #7,867
October 2006
avatar

Success! I have it drawing a thing.

Chris Katko
Member #1,881
January 2002
avatar

Elias said:

al_init_primitives_addon is required for al_draw_rectangle

I thought we covered that already. :o

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs

bamccaig
Member #7,536
July 2006
avatar

Also, free software debugger 101:

1. Find gdb on your system. Or if it didn't come with your toolchain you can find a download for it.
2. gdb .\game.exe
3. run
4. After a crash occurs, bt to view the call stack, ending at the point in your code where it crashed.

You should get a function name in C, and a line number too if you have debugging symbols built in. C++ might be a mess unless you have debugging symbols due to name mangling though.

Neil Black
Member #7,867
October 2006
avatar

We did cover that Chris. But I tried adding it without the if statement and it worked.

Edgar Reynaldo
Member #8,592
May 2007
avatar

You can find Eagle here : http://members.allegro.cc/EdgarReynaldo/Eagle.html

Some of the GUI functionality is still undone, but you can do many things with Eagle. Graphics, input, and event handling are all much simplified.

Right now it takes CodeBlocks to build, with MinGW on Windows, gcc on Linux, and I haven't had the opportunity to try and build Eagle on OSX. I wanted to release binaries but I'm still working out a few bugs in the multi threaded aspect of Eagle, trying to make the essentials thread safe.

It works fine for single window (thread) apps though.

Go to: