Clone of Akilla's ac2d @ https://github.com/deregtd/AC2D

cGraphics.cpp 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "stdafx.h"
  2. #include "cGraphics.h"
  3. extern PFNGLCOMPRESSEDTEXIMAGE2DARBPROC glCompressedTexImage2DARB;
  4. float g_fFontWidth[25][256];
  5. #define FPS_SAMPLES 10
  6. int LastFPS[10];
  7. GLuint g_textureID;
  8. cGraphics::cGraphics(HWND hWnd)
  9. {
  10. m_hWnd = hWnd;
  11. EnableOpenGL();
  12. }
  13. cGraphics::~cGraphics()
  14. {
  15. DisableOpenGL();
  16. }
  17. void cGraphics::SetInterface(cInterface *Interface)
  18. {
  19. m_Interface = Interface;
  20. }
  21. void cGraphics::EnableOpenGL()
  22. {
  23. // get the device context (DC)
  24. m_hDC = GetDC( m_hWnd );
  25. // set the pixel format for the DC
  26. PIXELFORMATDESCRIPTOR pfd = {
  27. sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
  28. 1, // version number
  29. PFD_DRAW_TO_WINDOW | // support window
  30. PFD_SUPPORT_OPENGL | // support OpenGL
  31. PFD_DOUBLEBUFFER | PFD_GENERIC_ACCELERATED | PFD_SWAP_EXCHANGE, // double buffered
  32. PFD_TYPE_RGBA, // RGBA type
  33. 16, // 24-bit color depth
  34. 0, 0, 0, 0, 0, 0, // color bits ignored
  35. 0, // no alpha buffer
  36. 0, // shift bit ignored
  37. 0, // no accumulation buffer
  38. 0, 0, 0, 0, // accum bits ignored
  39. 32, // 32-bit z-buffer
  40. 0, // no stencil buffer
  41. 0, // no auxiliary buffer
  42. PFD_MAIN_PLANE, // main layer
  43. 0, // reserved
  44. 0, 0, 0 // layer masks ignored
  45. };
  46. SetPixelFormat( m_hDC, ChoosePixelFormat( m_hDC, &pfd ), &pfd );
  47. // create and enable the render context (RC)
  48. m_hGLRC = wglCreateContext( m_hDC );
  49. wglMakeCurrent( m_hDC, m_hGLRC );
  50. //setup options
  51. glClearColor( 0, 0, 0, 1.0f );
  52. glEnable(GL_TEXTURE_2D);
  53. glDisable(GL_DEPTH_TEST);
  54. // glEnable(GL_LIGHTING);
  55. glEnable(GL_LIGHT0);
  56. glShadeModel(GL_SMOOTH);
  57. // glDepthFunc(GL_LEQUAL);
  58. // glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
  59. //// glEnable (GL_BLEND);
  60. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  61. glEnable(GL_ALPHA_TEST);
  62. glAlphaFunc(GL_GREATER, 0);
  63. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  64. glEnableClientState (GL_COLOR_ARRAY);
  65. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  66. // glEnableClientState (GL_NORMAL_ARRAY);
  67. glEnableClientState (GL_VERTEX_ARRAY);
  68. //setup light0
  69. GLfloat amb0[4] = { 0.10f, 0.10f, 0.10f, 1.0f }; glLightfv(GL_LIGHT0,GL_AMBIENT,amb0);
  70. GLfloat dif0[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; glLightfv(GL_LIGHT0,GL_DIFFUSE,dif0);
  71. GLfloat spc0[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; glLightfv(GL_LIGHT0,GL_SPECULAR,dif0);
  72. for (int i=4; i<=24; i++)
  73. {
  74. HFONT tpfont = CreateFont(i+2, 0, 0, 0, FW_NORMAL, false, false, 0, DEFAULT_CHARSET,
  75. OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH/* | FF_SWISS*/,
  76. "Tahoma");
  77. SelectObject (m_hDC, tpfont);
  78. ABCFLOAT tpABC[256];
  79. GetCharABCWidthsFloat(m_hDC, 0, 255, tpABC);
  80. for (int h=0; h<256; h++)
  81. {
  82. g_fFontWidth[i][h] = tpABC[h].abcfA + tpABC[h].abcfB + tpABC[h].abcfC;
  83. }
  84. wglUseFontBitmaps(m_hDC, 0, 255, 0x01000000 | (i << 8));
  85. DeleteObject(tpfont);
  86. }
  87. glClearDepth(1.0f);
  88. glCompressedTexImage2DARB = (PFNGLCOMPRESSEDTEXIMAGE2DARBPROC)wglGetProcAddress("glCompressedTexImage2DARB");
  89. }
  90. // Disable OpenGL
  91. void cGraphics::DisableOpenGL()
  92. {
  93. // delete our 256 glyph display lists
  94. glDeleteLists(1000, 256) ;
  95. wglMakeCurrent( NULL, NULL );
  96. wglDeleteContext( m_hGLRC );
  97. ReleaseDC( m_hWnd, m_hDC );
  98. }
  99. void cGraphics::Resize()
  100. {
  101. m_bNeedResize = true;
  102. }
  103. void cGraphics::Run()
  104. {
  105. LARGE_INTEGER liTimerFreq, liLastTimer;
  106. QueryPerformanceFrequency(&liTimerFreq);
  107. QueryPerformanceCounter(&liLastTimer);
  108. while (!m_bQuit)
  109. {
  110. if (m_bNeedResize)
  111. {
  112. GetClientRect(m_hWnd, &m_rRect);
  113. m_iWidth = m_rRect.right - m_rRect.left;
  114. m_iHeight = m_rRect.bottom - m_rRect.top;
  115. glViewport(0, 0, m_iWidth, m_iHeight);
  116. m_Interface->Resize(m_iWidth, m_iHeight);
  117. m_bNeedResize = false;
  118. }
  119. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  120. int tricount = m_Interface->Draw(m_rRect, m_hDC);
  121. SwapBuffers( m_hDC );
  122. //fps calc
  123. LARGE_INTEGER liTemp;
  124. QueryPerformanceCounter(&liTemp);
  125. for (int i=FPS_SAMPLES-2;i>=0;i--) LastFPS[i+1] = LastFPS[i];
  126. LastFPS[0] = (int) (liTimerFreq.QuadPart/(liTemp.QuadPart - liLastTimer.QuadPart));
  127. liLastTimer = liTemp;
  128. int iFPS = 0;
  129. for (int i=0;i<FPS_SAMPLES;i++) iFPS += LastFPS[i];
  130. iFPS /= FPS_SAMPLES;
  131. char pbTemp[100];
  132. _snprintf(pbTemp, 100, "AC2D - %04i FPS - %i Tris - Cell: %i, Portal: %i - %0.1f Speed - %04X Landblock", iFPS, tricount, m_Cell->GetPoolSize(), m_Portal->GetPoolSize(), m_Interface->GetZoomSpeed(), m_Interface->GetPosition());
  133. SetWindowText(m_hWnd,pbTemp);
  134. //sleep so other windows can have some cpu
  135. Sleep(0);
  136. }
  137. }