- initial import
git-svn-id: http://moon:8086/svn/mips@1 a8ebac50-d88d-4704-bea3-6648445a41b3
This commit is contained in:
+269
@@ -0,0 +1,269 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include "ri.h"
|
||||
#include "types.h"
|
||||
#include "matrix.h"
|
||||
#include "graph_state.h"
|
||||
#include "stack.h"
|
||||
#include "object.h"
|
||||
#include "imageio.h"
|
||||
#include "colortypes.h"
|
||||
#include "vars.h"
|
||||
|
||||
RtToken VAR_CLASSES[] = {"vertex", "uniform", "varying", "constant", NULL};
|
||||
RtToken VAR_TYPES[] = {"float", "point", "color", "string", "integer", "vector", "normal", "matrix", "hpoint", NULL};
|
||||
RtInt VAR_SIZES[] = {sizeof(RtFloat), sizeof(RtPoint), sizeof(RtColor), 1, sizeof(RtInt), sizeof(RtVector), sizeof(RtNormal), sizeof(RtMatrix), sizeof(RtHpoint), 0};
|
||||
|
||||
RtInt VAR_ECLASSES[] = {VC_VERTEX, VC_UNIFORM, VC_VARYING, VC_CONSTANT, 0};
|
||||
RtInt VAR_ETYPES[] = {VT_FLOAT, VT_POINT, VT_COLOR, VT_STRING, VT_INTEGER, VT_VECTOR, VT_NORMAL, VT_MATRIX, VT_HPOINT, 0};
|
||||
|
||||
void VarListInit(var_list_t *pObj)
|
||||
{
|
||||
pObj->pVarList = NULL;
|
||||
}
|
||||
|
||||
// Destroy list of variables
|
||||
void VarListFree(var_list_t *pObj)
|
||||
{
|
||||
linkedlist_t *pVarList = pObj->pVarList;
|
||||
ri_var_t *pVar;
|
||||
|
||||
pVarList = LinkedList_find_first(pVarList);
|
||||
|
||||
while(pVarList)
|
||||
{
|
||||
pVar = (ri_var_t*)pVarList->pData;
|
||||
if (pVar->pValue)
|
||||
{
|
||||
jfree(pVar->pValue);
|
||||
}
|
||||
pVarList = pVarList->pNext;
|
||||
}
|
||||
|
||||
LinkedList_free(pObj->pVarList);
|
||||
pObj->pVarList = NULL;
|
||||
}
|
||||
|
||||
// finds variable by its name
|
||||
ri_var_t* VarListFindByName(var_list_t *pObj, UINT8 *name)
|
||||
{
|
||||
linkedlist_t *pFirst, *pVarList;
|
||||
ri_var_t *pVar;
|
||||
|
||||
pFirst = LinkedList_find_first(pObj->pVarList);
|
||||
|
||||
// Find by comparing pointers
|
||||
pVarList = pFirst;
|
||||
while(pVarList)
|
||||
{
|
||||
pVar = (ri_var_t*)pVarList->pData;
|
||||
if (name == pVar->name)
|
||||
{
|
||||
return pVar;
|
||||
}
|
||||
pVarList = pVarList->pNext;
|
||||
}
|
||||
|
||||
// Find by comparing whole strings
|
||||
pVarList = pFirst;
|
||||
while(pVarList)
|
||||
{
|
||||
pVar = (ri_var_t*)pVarList->pData;
|
||||
if (!stricmp(name, pVar->name))
|
||||
{
|
||||
return pVar;
|
||||
}
|
||||
pVarList = pVarList->pNext;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Print variables
|
||||
void VarListPrint(var_list_t *pObj)
|
||||
{
|
||||
linkedlist_t *pFirst, *pVarList;
|
||||
ri_var_t *pVar;
|
||||
|
||||
pFirst = LinkedList_find_first(pObj->pVarList);
|
||||
|
||||
// Find by comparing pointers
|
||||
pVarList = pFirst;
|
||||
while(pVarList)
|
||||
{
|
||||
pVar = (ri_var_t*)pVarList->pData;
|
||||
fprintf(stdout, "\nVariable \"%s\":\n", pVar->name);
|
||||
fprintf(stdout, "\tClass: \"%s\"\n", pVar->cls);
|
||||
fprintf(stdout, "\tType: \"%s\"\n", pVar->type);
|
||||
fprintf(stdout, "\tValue size: %d\n", pVar->value_size);
|
||||
fprintf(stdout, "\tNum. values: %d\n", pVar->nvalues);
|
||||
pVarList = pVarList->pNext;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Declare empty variable and add it to list of variables
|
||||
ri_var_t* VarListDeclare(var_list_t *pObj, UINT8 *name, UINT8 *cls, UINT8 *type)
|
||||
{
|
||||
RtToken *pToken;
|
||||
RtInt *pSize;
|
||||
UINT32 *pEtypes;
|
||||
UINT32 *pEclasses;
|
||||
|
||||
ri_var_t var;
|
||||
|
||||
if (VarListFindByName(pObj, name))
|
||||
{
|
||||
fprintf(stderr, "Variable \"%s\" is already declared!", name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(&var, 0, sizeof(ri_var_t));
|
||||
var.name = (char*)jmalloc(strlen(name)+1);
|
||||
memcpy(var.name, name, strlen(name)+1);
|
||||
|
||||
pToken = VAR_CLASSES;
|
||||
while(*pToken)
|
||||
{
|
||||
if (!stricmp(cls, *pToken))
|
||||
{
|
||||
var.cls = *pToken;
|
||||
break;
|
||||
}
|
||||
|
||||
pToken++;
|
||||
}
|
||||
if (*pToken == NULL)
|
||||
{
|
||||
fprintf(stderr, "Unknown class \"%s\" for variable!", cls);
|
||||
return NULL;
|
||||
}
|
||||
pToken = VAR_TYPES;
|
||||
pSize = VAR_SIZES;
|
||||
pEtypes = VAR_ETYPES;
|
||||
pEclasses = VAR_ECLASSES;
|
||||
while(*pToken)
|
||||
{
|
||||
if (!stricmp(type, *pToken))
|
||||
{
|
||||
var.type = *pToken;
|
||||
var.value_size = *pSize;
|
||||
var.etype = *pEtypes;
|
||||
var.ecls = *pEclasses;
|
||||
break;
|
||||
}
|
||||
pToken++;
|
||||
pSize++;
|
||||
pEtypes++;
|
||||
pEclasses++;
|
||||
}
|
||||
if (*pToken == NULL)
|
||||
{
|
||||
fprintf(stderr, "Unknown type \"%s\" for variable!", type);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pObj->pVarList = LinkedList_add(pObj->pVarList, 1, (void*)&var, sizeof(ri_var_t));
|
||||
|
||||
return (ri_var_t*)pObj->pVarList->pData;
|
||||
|
||||
}
|
||||
|
||||
// Add empty variable to list of variables
|
||||
ri_var_t* VarListAdd(var_list_t *pObj, ri_var_t *pVar)
|
||||
{
|
||||
if (pVar->pValue != NULL)
|
||||
fprintf(stderr, "VarListAdd(): Variable \"%s\" is not empty!\n", pVar->name);
|
||||
|
||||
pObj->pVarList = LinkedList_add(pObj->pVarList, 1, (void*)pVar, sizeof(ri_var_t));
|
||||
|
||||
return (ri_var_t*)pObj->pVarList->pData;
|
||||
|
||||
}
|
||||
|
||||
void VarListDelByName(var_list_t *pObj, UINT8 *name)
|
||||
{
|
||||
VarListDel(pObj, VarListFindByName(pObj, name));
|
||||
}
|
||||
|
||||
void VarListDel(var_list_t *pObj, ri_var_t *pVar)
|
||||
{
|
||||
linkedlist_t *pFirst, *pVarList;
|
||||
|
||||
pFirst = LinkedList_find_first(pObj->pVarList);
|
||||
|
||||
// Find by comparing pointers
|
||||
pVarList = pFirst;
|
||||
while(pVarList)
|
||||
{
|
||||
if (((ri_var_t*)pVarList->pData)->name == pVar->name)
|
||||
{
|
||||
if (pVar->pValue)
|
||||
{
|
||||
jfree(pVar->pValue);
|
||||
pVar->pValue = NULL;
|
||||
}
|
||||
LinkedList_del(pVarList);
|
||||
break;
|
||||
}
|
||||
pVarList = pVarList->pNext;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Allocs empty variable
|
||||
RtPointer VarAlloc(ri_var_t *pObj, UINT32 nvalues)
|
||||
{
|
||||
if (!nvalues)
|
||||
return NULL;
|
||||
|
||||
if (nvalues != pObj->nvalues)
|
||||
{
|
||||
if (pObj->pValue)
|
||||
{
|
||||
jfree(pObj->pValue);
|
||||
pObj->pValue = NULL;
|
||||
}
|
||||
pObj->pValue = (RtPointer)jmalloc(nvalues*pObj->value_size);
|
||||
pObj->nvalues = nvalues;
|
||||
}
|
||||
|
||||
return pObj->pValue;
|
||||
|
||||
}
|
||||
|
||||
// Allocs empty variable and copy data into it
|
||||
RtPointer VarSet(ri_var_t *pObj, RtPointer pValue, UINT32 nvalues)
|
||||
{
|
||||
RtPointer pData;
|
||||
|
||||
pData = VarAlloc(pObj, nvalues);
|
||||
|
||||
if (!pData)
|
||||
return pData;
|
||||
|
||||
if (pValue)
|
||||
memcpy(pData, pValue, nvalues*pObj->value_size);
|
||||
else
|
||||
memset(pData, 0, nvalues*pObj->value_size);
|
||||
|
||||
return pData;
|
||||
|
||||
}
|
||||
RtPointer VarGetByName(var_list_t *pObj, RtToken name)
|
||||
{
|
||||
return VarGet(VarListFindByName(pObj, name));
|
||||
}
|
||||
|
||||
// Return pointer to values of variable
|
||||
RtPointer VarGet(ri_var_t *pObj)
|
||||
{
|
||||
if (!pObj)
|
||||
return (RtPointer)pObj;
|
||||
|
||||
return (RtPointer)pObj->pValue;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user