📄 util.cpp
字号:
cout << "CLOSED: "; for (i=0; i<CLOSED.size(); i++) { cout << CLOSED[i].x << "," << CLOSED[i].y << ","; cout << CLOSED[i].gone << "," << CLOSED[i].heuristic << " "; } cout << endl << endl; int ch = _getch(); //*/ } } } } if (CLOSED.size() > 0) { // Create the path from elements of the CLOSED container PATH.clear(); PATH.push_back(CLOSED.back()); CLOSED.pop_back(); while (!CLOSED.empty()) { if ((CLOSED.back().x == PATH.back().px) && (CLOSED.back().y == PATH.back().py)) PATH.push_back(CLOSED.back()); CLOSED.pop_back(); } // Populate the vector that was passed in by reference Location Fix; pVector->clear(); for (int i=(PATH.size()-1); i>=0; i--) { //for (container::iterator i=PATH.begin(); i!= PATH.end(); ++i) Fix.x = PATH[i].x; Fix.y = PATH[i].y; Fix.z = 0; pVector->push_back(Fix); } }}///////////////////////////////////////////////////////////// Needed because the template doesn't know what a PathNode is//bool operator<(const CPathNode &a, const CPathNode &b) {// return a.f < b.f;//}///////////////////////////////////////////////////////////// Needed because the template doesn't know what a PathNode is//bool operator>(const CPathNode &a, const CPathNode &b) {// return a.f > b.f;//}float Util::dot_product(float v1[3], float v2[3]) { return (v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2]);}void Util::normalize(float v[3]) { float f = 1.0f / sqrt(dot_product(v, v)); v[0] *= f; v[1] *= f; v[2] *= f;}void Util::cross_product(const float *v1, const float *v2, float *out) { out[0] = v1[1] * v2[2] - v1[2] * v2[1]; out[1] = v1[2] * v2[0] - v1[0] * v2[2]; out[2] = v1[0] * v2[1] - v1[1] * v2[0];}void Util::multiply_vector_by_matrix(const float m[9], float v[3]) { float tmp[3]; tmp[0] = v[0] * m[0] + v[1] * m[3] + v[2] * m[6]; tmp[1] = v[0] * m[1] + v[1] * m[4] + v[2] * m[7]; tmp[2] = v[0] * m[2] + v[1] * m[5] + v[2] * m[8]; v[0] = tmp[0]; v[1] = tmp[1]; v[2] = tmp[2];}// Return a string containing the last OpenGL error.// Useful to debug strange OpenGL behaviorschar * Util :: getOpenGLError(){ int error; error = glGetError(); // All openGl errors possible switch(error){ case GL_NO_ERROR : return "GL_NO_ERROR";break; case GL_INVALID_ENUM : return "GL_INVALID_ENUM"; break; case GL_INVALID_VALUE : return "GL_INVALID_VALUE"; break; case GL_INVALID_OPERATION : return "GL_INVALID_OPERATION"; break; case GL_STACK_OVERFLOW : return "GL_STACK_OVERFLOW"; break; case GL_OUT_OF_MEMORY : return "GL_OUT_OF_MEMORY"; break; default : return "Unknown error"; break; }}// Returns next word from the given position. If there is not a space at the given// position, the function suppose it is the first letter of the word wanted. string Util::getNextWord(const string theInput, int fromPos, int &endWord){ int firstChar, lastStringChar; string sub; //sub.clear(); sub.erase(sub.begin(), sub.end()); if (theInput.empty() || fromPos==-1) {return sub;} lastStringChar = theInput.find_last_not_of(' '); if(theInput[fromPos] == ' '){ firstChar = theInput.find_first_not_of(' ', fromPos); } else{ firstChar = fromPos; } endWord = theInput.find_first_of(' ', firstChar); // cout << "line :" << theInput << endl; // cout << "\t\tpos = " << fromPos << " firstChar = " << firstChar << " endWord = " << endWord << " lastStringChar =" << lastStringChar << endl; if(endWord == -1){ if( (lastStringChar >= firstChar)&&(firstChar!=-1)){ sub = theInput.substr(firstChar, lastStringChar - firstChar + 1); } } else{ sub = theInput.substr(firstChar, endWord - firstChar); } return sub;}float Util::getAngle(float fx, float fy, float fw, float fd, float tx, float ty, float tw, float td) { // figure out targetCreatureAngle float sx = fx + (fw / 2); float sy = fy - (fd / 2); float ex = tx + (tw / 2); float ey = ty - (td / 2); float x = ex - sx; float y = ey - sy; float angle = Constants::toAngle(atan(y / x)); // read about the arctan problem: // http://hyperphysics.phy-astr.gsu.edu/hbase/ttrig.html#c3 // q = 1; if(x < 0) { // Quadrant 2 & 3 // q = ( y >= 0 ? 2 : 3); angle += 180; } else if(y < 0) { // Quadrant 4 // q = 4; angle += 360; } return angle;}void Util::drawBar(int x, int y, float barLength, float value, float maxValue, float red, float green, float blue, float gradient) { float percent = (maxValue == 0 ? 0 : (value >= maxValue ? 100.0f : value / (maxValue / 100.0f))); float length = barLength * (percent / 100.0f); if(length < 0) { length = percent = 0; } glPushMatrix(); glTranslatef( x, y, 0 ); glColor3f( 0.8f, 0.5f, 0.2f ); glBegin( GL_QUADS ); glVertex3f( barLength + 1, -4, 0 ); glVertex3f( -1, -4, 0 ); glVertex3f( -1, 4, 0 ); glVertex3f( barLength + 1, 4, 0 ); glEnd(); glLineWidth(6.0f); // glColor3f( 0.2f, 0.2f, 0.2f ); glColor3f( 1, 0.75f, 0.45f ); glBegin( GL_LINES ); glVertex3f( 0, 0, 0 ); glVertex3f( barLength, 0, 0 ); glEnd(); // default args so I don't have to recompile .h file if(red == -1) { red = 0.5f; green = 1.0f; blue = 0.5f; } if(!gradient || percent > 40.0f) { glColor3f( red, green, blue ); } else if(percent > 25.0f) { glColor3f( 1.0f, 1.0f, 0.5f ); } else { glColor3f( 1.0f, 0.5f, 0.5f ); } glBegin( GL_LINES ); glVertex3f( 0, 0, 0 ); glVertex3f( length, 0, 0 ); glEnd(); glLineWidth(1.0f); if(percent > 0.0f && percent < 100.0f) { glColor3f( 0.8f, 0.5f, 0.2f ); glBegin( GL_LINES ); glVertex3f( length, -4, 0 ); glVertex3f( length, 4, 0 ); glEnd(); } glPopMatrix();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -