Advertisement
Guest User

GLWaterWinMain

a guest
Oct 17th, 2011
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.89 KB | None | 0 0
  1. #define WIN32_LEAN_AND_MEAN
  2. #define WIN32_EXTRA_LEAN
  3. #include <windows.h>
  4. #include <cmath>
  5. #include <gl/gl.h>
  6. #include "GLWater.h"
  7.  
  8. const unsigned int windowWidth = 800;
  9. const unsigned int windowHeight = 600;
  10. const float openGLNear = 1.0f;
  11. const float openGLFar = 1000.0f;
  12. bool fullScreen = false;
  13. RECT windowRect;
  14. #define THROTTLE_FPS 1
  15. #define TARGET_FPS 60
  16. #define WND_CLASSNAME "GLWater"
  17. #define WND_TITLE "Water Simulation Demo"
  18.  
  19. HWND hwnd;
  20. HINSTANCE hinstance;
  21. HGLRC hglrc;
  22.  
  23. LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);
  24. bool CheckIfAlreadyRunning();
  25. void OpenGLUnbindContext(HWND hwnd, HDC hdc, HGLRC hglrc);
  26. void OpenGLResetProjection();
  27. HGLRC OpenGLBindContext(HDC hdc);
  28. void ToggleFullscreen();
  29. void SetDisplayMode(int width, int height, int bpp, int refreshRate);
  30.  
  31. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)  {
  32.     if (!hPrevInstance)
  33.         if (CheckIfAlreadyRunning())
  34.             return FALSE;
  35. #if THROTTLE_FPS
  36.     const int SKIP_TICKS = 1000 / TARGET_FPS;
  37.     DWORD next_game_tick = GetTickCount();
  38.     int sleep_time = 0;
  39. #endif
  40.     MSG msg; HDC hdc;
  41.     hinstance = hInstance;
  42.  
  43.     WNDCLASSEX wndclass;
  44.     wndclass.cbSize         = sizeof(WNDCLASSEX);
  45.     wndclass.style          = CS_HREDRAW | CS_VREDRAW;
  46.     wndclass.lpfnWndProc    = WndProc;
  47.     wndclass.cbClsExtra     = 0;
  48.     wndclass.cbWndExtra     = 0;
  49.     wndclass.hInstance      = hInstance;
  50.     wndclass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
  51.     wndclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);
  52.     wndclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  53.     wndclass.hbrBackground  = (HBRUSH)(COLOR_BTNFACE + 1);
  54.     wndclass.lpszMenuName   = 0;
  55.     wndclass.lpszClassName  = WND_CLASSNAME;
  56.     RegisterClassEx(&wndclass);
  57.  
  58.     SetRect(&windowRect, (GetSystemMetrics(SM_CXSCREEN) / 2) - (windowWidth / 2), (GetSystemMetrics(SM_CYSCREEN) / 2) - (windowHeight / 2),  (GetSystemMetrics(SM_CXSCREEN) / 2) + (windowWidth / 2), (GetSystemMetrics(SM_CYSCREEN) / 2) + (windowHeight / 2));
  59.     AdjustWindowRectEx(&windowRect, WS_VISIBLE | WS_OVERLAPPEDWINDOW, FALSE, 0);
  60.  
  61.     hwnd = CreateWindowEx(0, WND_CLASSNAME, WND_TITLE, WS_VISIBLE | WS_OVERLAPPEDWINDOW, windowRect.left, windowRect.top, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top, NULL, NULL, hInstance, szCmdLine);
  62.  
  63.     hdc = GetDC(hwnd);
  64.     hglrc = OpenGLBindContext(hdc);
  65.  
  66.     if (fullScreen) {
  67.         fullScreen = false;
  68.         ToggleFullscreen();
  69.     }
  70.  
  71.     Initialize();
  72.     ShowWindow(hwnd, SW_SHOW);
  73.     UpdateWindow(hwnd);
  74.     OpenGLResetProjection();
  75.  
  76.     while (true) {
  77.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  78.             if (msg.message == WM_QUIT)
  79.                 break;
  80.             TranslateMessage(&msg);
  81.             DispatchMessage(&msg);
  82.         }      
  83. #if THROTTLE_FPS
  84.         next_game_tick += SKIP_TICKS;
  85.         sleep_time = next_game_tick - GetTickCount();
  86.         if(sleep_time >= 0)
  87.             Sleep(sleep_time);
  88. #endif
  89.         Update();
  90.         Render();
  91.         SwapBuffers(hdc);
  92.     }
  93.  
  94.     Shutdown();
  95.     OpenGLUnbindContext(hwnd, hdc, hglrc);
  96.  
  97.     return (int)msg.wParam;
  98. }
  99.  
  100. LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) {
  101.     switch (iMsg)  {
  102.        case WM_CREATE :
  103.            break;
  104.        case WM_CLOSE :
  105.            DestroyWindow(hwnd);
  106.            break;
  107.        case WM_DESTROY :
  108.            PostQuitMessage(0);
  109.            break;
  110.        case WM_KEYDOWN:
  111.        case WM_SYSKEYDOWN:
  112.            if (wParam == VK_RETURN)
  113.                if ((HIWORD(lParam) & KF_ALTDOWN))
  114.                    ToggleFullscreen();
  115.            break;
  116.        case WM_SIZE:
  117.            OpenGLResetProjection();
  118.            break;
  119.        case WM_ACTIVATE:
  120.            if (LOWORD(wParam) != WA_INACTIVE)
  121.                Suspend(false);
  122.            else
  123.                Suspend(true);
  124.            break;
  125.        case WM_SYSKEYUP:
  126.        case WM_SYSCHAR:
  127.        case WM_PAINT:
  128.        case WM_ERASEBKGND:
  129.            return 0;
  130.     }
  131.  
  132.     return DefWindowProc(hwnd, iMsg, wParam, lParam);
  133. }
  134.  
  135. HGLRC OpenGLBindContext(HDC hdc) {
  136.     PIXELFORMATDESCRIPTOR pfd;
  137.     memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  138.  
  139.     pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  140.     pfd.nVersion = 1;
  141.     pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
  142.     pfd.iPixelType = PFD_TYPE_RGBA;
  143.     pfd.cColorBits = 24;
  144.     pfd.cDepthBits = 32;
  145.     pfd.cStencilBits = 8;
  146.     pfd.iLayerType = PFD_MAIN_PLANE;
  147.  
  148.     int pixelFormat = ChoosePixelFormat(hdc, &pfd);
  149.     SetPixelFormat(hdc, pixelFormat, &pfd);
  150.  
  151.     HGLRC context = wglCreateContext(hdc);
  152.     wglMakeCurrent(hdc, context);
  153.     return context;
  154. }
  155.  
  156. void OpenGLUnbindContext(HWND hwnd, HDC hdc, HGLRC hglrc) {
  157.     wglMakeCurrent(NULL, NULL);
  158.     wglDeleteContext(hglrc);
  159.     ReleaseDC(hwnd, hdc);
  160. }
  161.  
  162. bool CheckIfAlreadyRunning(void) {
  163.     HWND hWnd = FindWindow(WND_CLASSNAME, WND_TITLE);
  164.  
  165.     if (hWnd) {
  166.         if (IsIconic(hWnd))
  167.             ShowWindow(hWnd, SW_RESTORE);
  168.         SetForegroundWindow(hWnd);
  169.         return true;
  170.     }
  171.  
  172.     return false;
  173. }
  174.  
  175. void ToggleFullscreen() {
  176.     if(!fullScreen) {
  177.         GetWindowRect(hwnd, &windowRect);
  178.         HDC hdc = GetDC(hwnd);
  179.         SetDisplayMode(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), GetDeviceCaps(hdc, BITSPIXEL), GetDeviceCaps(hdc, VREFRESH));
  180.         SetWindowLongPtr(hwnd, GWL_STYLE, WS_POPUP);
  181.         SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE);
  182.         SetWindowPos(hwnd, 0, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), SWP_NOZORDER);
  183.         ShowCursor(FALSE);
  184.         ReleaseDC(hwnd, hdc);
  185.         fullScreen = true;
  186.     } else {
  187.         SetDisplayMode(0, 0, 0, 0);
  188.         SetWindowLongPtr(hwnd, GWL_STYLE, WS_OVERLAPPEDWINDOW);
  189.         SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE);
  190.         int iWindowWidth = windowRect.right - windowRect.left;
  191.         int iWindowHeight = windowRect.bottom - windowRect.top;
  192.         SetWindowPos(hwnd, 0, windowRect.left, windowRect.top, iWindowWidth, iWindowHeight, SWP_NOZORDER);
  193.         ShowCursor(TRUE);
  194.         fullScreen = false;
  195.     }
  196.  
  197.     OpenGLResetProjection();
  198. }
  199.  
  200. void SetDisplayMode(int width, int height, int bpp, int refreshRate) {
  201.     if(width == 0 && height == 0 && bpp == 0 && refreshRate == 0) {
  202.         ChangeDisplaySettings(NULL, 0);
  203.         return;
  204.     }
  205.  
  206.     DEVMODE dm;
  207.     dm.dmSize = sizeof(DEVMODE);
  208.  
  209.     int i = 0;
  210.     while(EnumDisplaySettings(NULL, i++, &dm)) {
  211.         if(dm.dmPelsWidth == width && dm.dmPelsHeight == height &&
  212.             dm.dmBitsPerPel == bpp && dm.dmDisplayFrequency == refreshRate) {
  213.                 if(ChangeDisplaySettings(&dm, CDS_TEST) == DISP_CHANGE_SUCCESSFUL) {
  214.                     ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
  215.                     return;
  216.                 }
  217.         }
  218.     }
  219. }
  220.  
  221. void OpenGLResetProjection() {
  222.     RECT window = { 0 };
  223.     GetClientRect(hwnd, &window);
  224.     glViewport(0, 0, window.right - window.left, window.bottom - window.top);
  225.  
  226.     float fov = 62.0f;
  227.     float aspect = (float)(window.right - window.left) / (float)(window.bottom - window.top);
  228.  
  229.     float top = openGLNear * float(tanf(fov * 3.14159265f / 360.0f));
  230.     float bottom = -1.0f * top;
  231.     float right  = bottom * aspect;
  232.     float left = top * aspect;
  233.  
  234.     glMatrixMode(GL_PROJECTION);
  235.     glLoadIdentity();
  236.     glFrustum(left, right, bottom, top, openGLNear, openGLFar);
  237.     glMatrixMode(GL_MODELVIEW);
  238.     glLoadIdentity();
  239. }
  240.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement