Rendering Stack (Of Functions)

I’ve used functions from different places before for registering
callbacks.
These range from simple things like atexit() to glutIdleFunc()

What I want to do is make my OWN registration functions,
I’ll call them, PushRenderFunc() and PopRenderFunc().
I want to pass to PushRenderFunc() a single function that
I can call from somewhere else in my code. PopRenderFunc()
should return that function.

Questions:

  1. How would I define Push/PopRenderFunc()

  2. How would I call the function I passed to PushRenderFunc()

Ultimate Goal:
Make an implementation of a STACK on function calls, thus enabling me to
do things like add the rendering of a health bar after the rest of the
scene is
completed. Temorarily (between pushes of the ENTER key) add a text area
to the bottom of the screen, then pop it back off the stack. My render
function
would then go through the stack, from bottom to top calling each
function in
the stack. It would be TRULY kickass (excuse my language).

Is this all possible?
-Randall

Randall Leeds wrote:

  1. How would I define Push/PopRenderFunc()

  2. How would I call the function I passed to PushRenderFunc()

This has nothing to do with SDL. Your questions are answered in any good
textbook on C programing

I’ve gotten a few messages telling me this was very off-topic for
this list.

To those who felt this way, i’m sorry, my apologies. But as can be
proven by the bit of code below, and by a previous post that was
also very helpful, this list is full of talented people. In the future
I will try to post to other mailing lists for such topics.

My full apologies, and thanks,
-RandallOn Friday, November 30, 2001, at 09:49 AM, kendallb at optonline.net wrote:

Easiest way is this:

typedef int (*renderfunc)();

typedef struct stack_el_struct {
renderfunc data;
stack_struct *next;
} stack_el_t;

typedef struct stack_stuct {
stack_el_t head;
} stack_t;

stack_t *new_stack ()
{
stack_t *stack = malloc (sizeof(stack_t));
if (stack != NULL)
{
stack->head.next = NULL;
}
return (stack);
}

int push (renderfunc fn)
{
stack_t *stack = _internal_stack (NULL);
stack_el_t *new_element = (stack_el_t *)malloc (sizeof(stack_el_t));
if (new_element == NULL)
{
return (0);
}

new_element->data = fn;
new_element->next = stack->head.next;
stack->head.next = new_element;
}

renderfunc pop ()
{
stack_t *stack = _internal_stack (NULL);
stack_el_t *element = stack->head.next;
renderfunc tmp = NULL;
if (element != NULL)
{
stack->head.next = stack->head.next->next;
tmp = element->data;
free (element);
}

return (tmp);
}

stack_t *_internal_stack (stack_t *arg)
{
static stack_t *render_stack;
if (arg != NULL)
{
render_stack = arg;
}
return (stack_t);
}

void graphics_init ()
{
_internal_stack (new_stack ());
}

void draw ()
{
renderfunc fn;
while ((fn = pop ()) != NULL)
{
fn ();
}
}

and just write all your blitting functions like …
int blit_background ()
{
/* do something … */
}

int blit_healthbar ()
{
/* do something else */
}

and then in your graphics loop …

while (/something/)
{
push (blit_healthbar);
push (blit_background);
/* Do stuff here, too */
draw ();
}

and you should be good. Get it?

Kendall

On Thursday 29 November 2001 08:33 pm, you wrote:

I’ve used functions from different places before for registering
callbacks.
These range from simple things like atexit() to glutIdleFunc()

What I want to do is make my OWN registration functions,
I’ll call them, PushRenderFunc() and PopRenderFunc().
I want to pass to PushRenderFunc() a single function that
I can call from somewhere else in my code. PopRenderFunc()
should return that function.

Questions:

  1. How would I define Push/PopRenderFunc()

  2. How would I call the function I passed to PushRenderFunc()

Ultimate Goal:
Make an implementation of a STACK on function calls, thus enabling me
to
do things like add the rendering of a health bar after the rest of the
scene is
completed. Temorarily (between pushes of the ENTER key) add a text
area
to the bottom of the screen, then pop it back off the stack. My render
function
would then go through the stack, from bottom to top calling each
function in
the stack. It would be TRULY kickass (excuse my language).

Is this all possible?
-Randall