/* Transformation matrix stack code V1.21 19/8/04
   See tmatstack.h for docs
   Copyright 2008 Jeffrey Lee
   This file is part of WOUM.
   WOUM is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   WOUM is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   You should have received a copy of the GNU General Public License
   along with WOUM.  If not, see <http://www.gnu.org/licenses/>.
*/

NAME *fix_op(NAME,_alloc)
{
	NAME *s;
	s = malloc(sizeof(NAME));
	if (s == 0)
		return 0;
	s->top = 0;
	fix_op(TYPE,_ident,&(s->s[0])); /* Load with identity matrix */
	return s;
}

void fix_op(NAME,_free,NAME *s)
{
	if (s)
		free(s);
}

TYPE *fix_op(NAME,_top,NAME *s)
{
	return &(s->s[s->top]);
}

TYPE *fix_op(NAME,_dup,NAME *s)
{
	int i,j;
	if (s->top == TMATSTACK_SIZE-1)
		return 0;
	for (i=0;i<4;i++)
		for (j=0;j<4;j++)
			s->s[s->top+1].t[i][j] = s->s[s->top].t[i][j];
	return &(s->s[++(s->top)]);
}

TYPE *fix_op(NAME,_pop,NAME *s)
{
	if (s->top == 0)
	{
		fix_op(TYPE,_ident,&(s->s[0])); /* Just load with ident */
		return &(s->s[0]);
	}
	return &(s->s[--(s->top)]);
}
