sgl

diff src/cb.c @ 0:40491760d6e3

starting work on SimplyGL
author John Tsiombikas <nuclear@siggraph.org>
date Tue, 10 May 2011 00:06:20 +0300
parents
children 648f8604d2b2
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/cb.c	Tue May 10 00:06:20 2011 +0300
     1.3 @@ -0,0 +1,50 @@
     1.4 +#include <stdlib.h>
     1.5 +#include <string.h>
     1.6 +#include "sgl.h"
     1.7 +
     1.8 +struct cbnode {
     1.9 +	void (*func[SGL_NUM_CALLBACKS])();
    1.10 +	struct cbnode *next;
    1.11 +};
    1.12 +
    1.13 +struct cbnode first_cbnode;
    1.14 +struct cbnode *cb = &first_cbnode;
    1.15 +
    1.16 +int sgl_push_callbacks(void)
    1.17 +{
    1.18 +	struct cbnode *node;
    1.19 +
    1.20 +	if(!(node = malloc(sizeof *node))) {
    1.21 +		return -1;
    1.22 +	}
    1.23 +	node->next = cb;
    1.24 +	cb = node;
    1.25 +	sgl_clear_callbacks();
    1.26 +	return 0;
    1.27 +}
    1.28 +
    1.29 +int sgl_pop_callbacks(void)
    1.30 +{
    1.31 +	struct cbnode *node;
    1.32 +
    1.33 +	if(!cb->next) {
    1.34 +		return -1;
    1.35 +	}
    1.36 +
    1.37 +	node = cb;
    1.38 +	cb = cb->next;
    1.39 +	free(node);
    1.40 +	return 0;
    1.41 +}
    1.42 +
    1.43 +void sgl_clear_callbacks(void)
    1.44 +{
    1.45 +	memset(cb->func, 0, sizeof cb->func);
    1.46 +}
    1.47 +
    1.48 +void (*sgl_callback(int idx, void (*func)()))()
    1.49 +{
    1.50 +	void (*prev)() = cb->func[idx];
    1.51 +	cb->func[idx] = func;
    1.52 +	return prev;
    1.53 +}