5 #include <gmath/gmath.h>
7 #include "state_manager.h"
9 StateManager state_manager;
11 State *StateManager::get_state(const char *name)
13 std::map<std::string, int>::iterator it;
14 it = statemap.find(name);
15 return it == statemap.end() ? 0 : &states[it->second];
18 int StateManager::add_state_element(const char *name, int num_floats)
20 State *st = get_state(name);
22 if(st->num != num_floats) {
23 fprintf(stderr, "Uniform %s, type mismatch!\n", name);
27 return st - &states[0];
31 state.num = num_floats;
33 state.name = new char[strlen(name) + 1];
34 strcpy(state.name, name);
36 int sz = sizeof *state.data * num_floats;
37 state.data = new float[sz];
38 memset(state.data, 0, sz);
40 int idx = (int)states.size();
41 states.push_back(state);
45 printf("State manager: added state %s with %d float data.\n", name, num_floats);
49 void StateManager::set_state(const char *name, float value)
51 set_state(name, Vec4(value, 0, 0, 1));
54 void StateManager::set_state(const char *name, const Vec3 &vec)
56 set_state(name, Vec4(vec.x, vec.y, vec.z, 1));
59 void StateManager::set_state(const char *name, const Vec4 &vec)
61 State *state = get_state(name);
67 state->data[3] = vec.w;
69 state->data[2] = vec.z;
71 state->data[1] = vec.y;
73 state->data[0] = vec.x;
79 void StateManager::set_state(const char *name, const Mat4 &mat)
81 State *state = get_state(name);
86 if(state->num != 16) {
87 fprintf(stderr, "State manager: state %s can only be a 4x4 matrix.\n", name);
91 for(int j=0; j<4; j++) {
92 for(int i=0; i<4; i++) {
93 state->data[4 * i + j] = mat.m[i][j];
98 const State *StateManager::get_state(const char *name) const
100 return ((StateManager *)this)->get_state(name);
103 const State *StateManager::get_state(int idx) const