Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro Primitives Wrapper

This thread is locked; no one can reply to it. rss feed Print
Allegro Primitives Wrapper
soaphhh
Member #16,107
October 2015

Hello, I've recently started using the allegro 5 library and have a question. If I were to make a 'wrapper' class so the primitives(e.g. circles), would I have to call al_init() and al_init_primitives_addon() in the class constructor. If so, wouldn't that conflict with al_init() and al_init_primitives_addon() being called in the main function. Or, could I just called skip calling it main, and call them once the first circle is constructed, and not after any other circles are constructed? Here's my goal:

Instead of

#SelectExpand
1 2al_draw_filled_circle(X,Y, radius, c); 3while (true) 4{ 5 X++; 6 Y++; 7 al_draw_filled_circle(X,Y, radius, color); 8}

I would use

#SelectExpand
1 alCircle C(X,Y, radius, color); 2 while (true) 3 { 4 C++; 5 //ideally, will achieve the same thing 6 }

I'm pretty sure that allegro has a built-in class that does this already. However, I wasn't able to find while briefly skimming over the reference manual. Any thoughts?

RPG Hacker
Member #12,492
January 2011
avatar

You have to call al_init() and al_init_primitives_addon() exactly once before using any Allegro primitive functions, that is basically your only rule. Therefore, you don't have to call it in your main function, as long as you call it somewhere in your main thread, so you COULD, in theory, put it in the constructor of your circle class. This doesn't sound like a good design choice, though. Simply speaking, "initialising Allegro" and "drawing a circle" are two completely different tasks, so why would you want to do them both in one class? This would get especially messy once you added other primitive classes, like "line", "rectangle", "triangle" etc. Basically you would have to put that code into the constructor of each primitive and still somehow make it execute only once. And if you ever had to change one of the constructors to fix something, you would have to change the other constructors as well.

Why not just call the init functions once at the beginning of main and then only put the actual drawing code in your circle class? Or, if you absolutely, for whatever reason, want to hide all Allegro functionality in classes, make a second class called "Application" or something like that and give it a static function called "Init()", where you put the calls to the Allegro initialization functions, then call only that Init() function in your main.

soaphhh
Member #16,107
October 2015

That's a much better idea RPG Hacker, thanks. I'm trying to minimize lines of code in my main function, but I wouldn't want to hide the init functions. Just the updating primitives. It will allow me to tie the primitives, like a circle to rigid body and do basic physics inside a contained class. Thanks for the tip.

jmasterx
Member #11,410
October 2009

The usual way would be that when it is time to render, the engine gives you a GraphicsContext* that is assumed to be valid. It is up to the engine to do any preparation so that the context is valid at the time you need it. Usually when your game engine starts, it initializes any global or thread-global state like Allegro and its addons.

Go to: