Re: [AD] Fwd: Suggestions & Problems |
[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]
In reply to Michael G <thebestprogrammer@xxxxxxxxxx>: >2. Creating or Destroying BITMAPs when allegro isn't initialized crashes >the program (system_driver is NULL). This makes it hard to construct c++ >classes that have BITMAPs as members. Generally, this is only a problem when you are instantiating or deleting a global class -- in this case, Allegro is not initialised (or has been removed). Your solution to this is to ensure that allegro_init() is called *befure* anything creates a bitmap, and that allegro_exit() is called *after* everything else is destroyed. Here I propose a method for doing that: // al_class.cpp // // Source file for dealing with Allegro and classes... // #include <allegro.h> #include <stdexcept.h> // This class instance *must* come first! class allegro_init_class { public: allegro_init_class() throw(std::except) { if(allegro_init()) throw std::except(); } }allegro_init_class_instance; // now instantiate all of your global classes here // This relies on the fact that the constructor of the first instance is executed first, ctor of 2nd instance is executed 2nd, etc. (ref 1). Since this behaviour is only guaranteed within compilation units (in this case, .cpp files), you will need to instantiate all affected global classes in the same file. This should not be a problem, however. It also uses the fact that allegro_init() will register allegro_exit() with atexit() in the first constructor. (ref 2). This means that you don't call allegro_exit() within your program (remember, you can remove components with remove_keyboard(), remove_sound(), et al., and you can switch to text mode by requesting GFX_TEXT). Because allegro_exit() is registered in the first constructor, the other classes have not yet been instantiated; this occurs *after* the atexit() call. Therefore their destructors will be executed *before* allegro_exit() is called (ref 3). References: 1. Stroustrup, p.252, paragraph 4. 2. allegro/src/allegro.c, line 347. 3. Stroustrup, p.219, paragraph 2. Stroustrup = `The C++ Programming Language, 3rd Edition' by Bjarne Stroustrup; ISBN 0-201-88954-4. Well, I hope this sheds some light on the situation, and maybe provides a useable solution. Perhaps we should add this to the FAQ, or create some sort of Allegro knowledge base? Bye for now, -- Laurence Withers, lwithers@xxxxxxxxxx http://www.lwithers.demon.co.uk/
Attachment:
signature.asc
Description: PGP signature
Mail converted by MHonArc 2.6.19+ | http://listengine.tuxfamily.org/ |