00001 /* 00002 * animation-state.cpp 00003 * 00004 * Copyright (C) 2010 Thomas A. Vaughan 00005 * All rights reserved. 00006 * 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions are met: 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * * Neither the name of the <organization> nor the 00016 * names of its contributors may be used to endorse or promote products 00017 * derived from this software without specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY 00020 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00022 * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY 00023 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00026 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 * 00030 * 00031 * Simple, general per-object animation state tracking. See animation-state.h 00032 */ 00033 00034 // includes -------------------------------------------------------------------- 00035 #include "animation-state.h" // always include our own header first! 00036 00037 00038 namespace glut { 00039 00040 00041 //////////////////////////////////////////////////////////////////////////////// 00042 // 00043 // static helper methods 00044 // 00045 //////////////////////////////////////////////////////////////////////////////// 00046 00047 00048 00049 //////////////////////////////////////////////////////////////////////////////// 00050 // 00051 // AnimationState -- public class methods 00052 // 00053 //////////////////////////////////////////////////////////////////////////////// 00054 00055 AnimationState::AnimationState 00056 ( 00057 void 00058 ) 00059 throw() 00060 { 00061 } 00062 00063 00064 00065 void 00066 AnimationState::serialize 00067 ( 00068 OUT std::string& state 00069 ) 00070 const 00071 { 00072 state.clear(); 00073 00074 const int bufsize = 64; 00075 char buffer[bufsize]; 00076 00077 int N = this->size(); 00078 for (int i = 0; i < N; ++i) { 00079 const anim_channel_state_t& acs = this->operator[](i); 00080 snprintf(buffer, bufsize, "%d:%d|", 00081 (int) acs.animIndex, (int) acs.msElapsed); 00082 state += buffer; 00083 } 00084 //DPRINTF("Animation state: '%s'", state.c_str()); 00085 } 00086 00087 00088 00089 bool 00090 AnimationState::deserialize 00091 ( 00092 IN const char * state 00093 ) 00094 { 00095 ASSERT(state, "null"); 00096 00097 this->clear(); // nuke our state, we will replace with input 00098 const char * p = state; 00099 while (*p) { 00100 anim_channel_state_t acs; 00101 acs.animIndex = (int16_t) atoi(p); 00102 while (*p && ':' != *p) { ++p; } 00103 if (!*p) { 00104 DPRINTF("Malformed animation state?"); 00105 return false; 00106 } 00107 ++p; // skip colon 00108 acs.msElapsed = (int32_t) atoi(p); 00109 while (*p && '|' != *p) { ++p; } 00110 if (!*p) { 00111 DPRINTF("Malformed animation state?"); 00112 return false; 00113 } 00114 ++p; // skip pipe 00115 this->push_back(acs); 00116 } 00117 // DPRINTF("Read %d animation channels", this->size()); 00118 return true; 00119 } 00120 00121 00122 00123 //////////////////////////////////////////////////////////////////////////////// 00124 // 00125 // public API 00126 // 00127 //////////////////////////////////////////////////////////////////////////////// 00128 00129 00130 00131 }; // glut namespace 00132