UTILITY.CPP
Upload User: nthssl
Upload Date: 2022-04-05
Package Size: 25357k
Code Size: 2k
Category:

OpenCV

Development Platform:

Visual C++

  1. // Utility.cpp: implementation of the CUtility class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "MyGame.h"
  6. #include "Utility.h"
  7. #include "string.h"
  8. #include <stdio.h>
  9. #include <stdarg.h>
  10. #include <assert.h>
  11. #include <math.h>
  12. #include "utility.h"
  13. #ifdef _DEBUG
  14. #undef THIS_FILE
  15. static char THIS_FILE[]=__FILE__;
  16. #define new DEBUG_NEW
  17. #endif
  18. //////////////////////////////////////////////////////////////////////
  19. // Construction/Destruction
  20. //////////////////////////////////////////////////////////////////////
  21. CUtility::CUtility()
  22. {
  23. }
  24. CUtility::~CUtility()
  25. {
  26. }
  27. void CUtility::PrintString(void *font, char *string)
  28. {
  29. int len,i;
  30. len=(int)strlen(string);
  31. for(i=0;i<len;i++)
  32. glutBitmapCharacter(font,string[i]);
  33. }
  34. void CUtility::glPrintf(int ScreenW,int ScreenH,GLuint x, GLuint y, int align, GLfloat scale, char* format, ...)
  35. {
  36.     va_list args;
  37.     char buffer[255], *p;
  38. int len;
  39. float dx=0;
  40.     GLfloat font_scale = 119.05f;
  41.     va_start(args, format);
  42.     vsprintf(buffer, format, args);
  43.     va_end(args);
  44. if(align) 
  45. {
  46. len=strlen(buffer);
  47. dx=((scale*len)/2)*104.76/119.05;
  48. }
  49.     glMatrixMode(GL_PROJECTION);
  50.     glPushMatrix();
  51.     glLoadIdentity();
  52. gluOrtho2D(0,ScreenW,0,ScreenH);
  53.     glMatrixMode(GL_MODELVIEW);
  54.     glPushMatrix();
  55.     glLoadIdentity();
  56.     glPushAttrib(GL_ENABLE_BIT);
  57.     glDisable(GL_LIGHTING);
  58.     glDisable(GL_TEXTURE_2D);
  59.     glEnable(GL_DEPTH_TEST);
  60.     glEnable(GL_LINE_SMOOTH);
  61.     glEnable(GL_BLEND);
  62.     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  63.     glTranslatef(x-dx, y, 0.0);
  64.     glScalef(scale/font_scale, scale/font_scale, scale/font_scale);
  65.     for(p = buffer; *p; p++)
  66. glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, *p);
  67.     glPopAttrib();
  68.     glPopMatrix();
  69.     glMatrixMode(GL_PROJECTION);
  70.     glPopMatrix();
  71.     glMatrixMode(GL_MODELVIEW);
  72. }
  73. float CUtility::FPS(int LastFrameTime)
  74. {
  75. static int LastTimes[10]={0,0,0,0,0,0,0,0,0,0};
  76. static int c=0;
  77. int i;
  78. float average=0;
  79. c=(c+1)%10;
  80. LastTimes[c]=LastFrameTime;
  81. for(i=0;i<10;i++)
  82. average+=LastTimes[i];
  83. return 1000.0/(average/10.0);
  84. }