Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » A5 feature: push & pop

This thread is locked; no one can reply to it. rss feed Print
A5 feature: push & pop
komons
Member #11,083
June 2009

Hi, is it possible to make in future release something like this:

push_clipping_rectangle();
set_clipping_rectangle( xx, yy, zz, ww );
[draw something]
pop_clipping_rectangle();

or

push_target_bitmap();
set_target_bitmap( buffer );
[draw something]
pop_target_bitmap();

It'd be very usefull.

raynebc
Member #11,908
May 2010

It seems like it would be easy for somebody to write it themselves, using singly linked lists.

Matthew Leverton
Supreme Loser
January 1999
avatar

I've suggested al_push_state(int state) and al_pop_state() before. And yes, it's easy to do yourself.

komons
Member #11,083
June 2009

I did it, but it would be as standard.

Mark Oates
Member #1,146
March 2001
avatar

I like to experiment. Here's the method I'm trying out now:

#SelectExpand
1#include <vector> 2 3inline void af_push_state(int flags); 4inline void af_pop_state(); 5 6class AllegroState 7{ 8private: 9 static AllegroState *instance; 10 AllegroState() { } 11public: 12 static std::vector<ALLEGRO_STATE *> allegro_state; 13 static AllegroState *get_instance() 14 { 15 if (!instance) instance = new AllegroState(); 16 return instance; 17 } 18 static void push_state(int flags) 19 { 20 allegro_state.push_back(new ALLEGRO_STATE); 21 al_store_state(allegro_state.back(), flags); 22 } 23 static void push_state() 24 { 25 if (allegro_state.empty()) return; 26 al_restore_state(allegro_state.back()); 27 delete allegro_state.back(); 28 allegro_state.pop_back(); 29 } 30}; 31AllegroState *AllegroState::instance = NULL; 32std::vector<ALLEGRO_STATE *> AllegroState::allegro_state; 33 34inline void af_push_state(int flags) { return AllegroState::get_instance()->push_state(flags); } 35inline void af_pop_state() { return AllegroState::get_instance()->push_state(); }

The question I have is, wouldn't it be faster to have the ALLEGRO_STATE structure inside your function and not push/pop? I can't answer this because I don't know how/if variables are allocated/created/stored/reused inside functions at runtime. But using the push/pop method definitely allocates and deallocates the memory, which I imagine would be the slower option.

Matthew Leverton
Supreme Loser
January 1999
avatar

static ALLEGRO_STATE foo[100];
static ALLEGRO_STATE *bar = foo;

void push(int f)
{
  al_store_state(bar++, f);
}

void pop()
{
  al_restore_state(--bar);
}

Mark Oates
Member #1,146
March 2001
avatar

Where's the fun in that? ;D

Not to mention you're using... :-X... globals... ghasp!

And what about all the stupid people who pop without a push? or push past 100 elements? Think of the children!? :o

Matthew Leverton
Supreme Loser
January 1999
avatar

In practice, you'd put some asserts in there when running in debug mode. There's no need to make them production code checks.

Mark Oates
Member #1,146
March 2001
avatar

So it's assumed that by the time you're ready for release, the asserts will have exposed use-cases (all possible ones, presumably) in your program and thus you disable them in release to improve speed?

Jonatan Hedborg
Member #4,886
July 2004
avatar

So it's assumed that by the time you're ready for release, the asserts will have exposed use-cases (all possible ones, presumably) in your program and thus you disable them in release to improve speed?

Wouldn't it be pretty hard to handle an error caused by miss-use of this stack in run-time anyway?

-------
Sweden: Free from the shackles of Democracy since 2008-06-18!

GullRaDriel
Member #3,861
September 2003
avatar

exit( FAILURE ); ? ;D

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!
My FlickR ! - Old photo gallery
If no moderator answers you then register and PM here !

Go to: