Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define WIN32_LEAN_AND_MEAN
- #define WIN32_EXTRA_LEAN
- #include <windows.h>
- #include <cmath>
- #include <gl/gl.h>
- #include "GLWater.h"
- const unsigned int windowWidth = 800;
- const unsigned int windowHeight = 600;
- const float openGLNear = 1.0f;
- const float openGLFar = 1000.0f;
- bool fullScreen = false;
- RECT windowRect;
- #define THROTTLE_FPS 1
- #define TARGET_FPS 60
- #define WND_CLASSNAME "GLWater"
- #define WND_TITLE "Water Simulation Demo"
- HWND hwnd;
- HINSTANCE hinstance;
- HGLRC hglrc;
- LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);
- bool CheckIfAlreadyRunning();
- void OpenGLUnbindContext(HWND hwnd, HDC hdc, HGLRC hglrc);
- void OpenGLResetProjection();
- HGLRC OpenGLBindContext(HDC hdc);
- void ToggleFullscreen();
- void SetDisplayMode(int width, int height, int bpp, int refreshRate);
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
- if (!hPrevInstance)
- if (CheckIfAlreadyRunning())
- return FALSE;
- #if THROTTLE_FPS
- const int SKIP_TICKS = 1000 / TARGET_FPS;
- DWORD next_game_tick = GetTickCount();
- int sleep_time = 0;
- #endif
- MSG msg; HDC hdc;
- hinstance = hInstance;
- WNDCLASSEX wndclass;
- wndclass.cbSize = sizeof(WNDCLASSEX);
- wndclass.style = CS_HREDRAW | CS_VREDRAW;
- wndclass.lpfnWndProc = WndProc;
- wndclass.cbClsExtra = 0;
- wndclass.cbWndExtra = 0;
- wndclass.hInstance = hInstance;
- wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
- wndclass.lpszMenuName = 0;
- wndclass.lpszClassName = WND_CLASSNAME;
- RegisterClassEx(&wndclass);
- 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));
- AdjustWindowRectEx(&windowRect, WS_VISIBLE | WS_OVERLAPPEDWINDOW, FALSE, 0);
- 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);
- hdc = GetDC(hwnd);
- hglrc = OpenGLBindContext(hdc);
- if (fullScreen) {
- fullScreen = false;
- ToggleFullscreen();
- }
- Initialize();
- ShowWindow(hwnd, SW_SHOW);
- UpdateWindow(hwnd);
- OpenGLResetProjection();
- while (true) {
- if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
- if (msg.message == WM_QUIT)
- break;
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- #if THROTTLE_FPS
- next_game_tick += SKIP_TICKS;
- sleep_time = next_game_tick - GetTickCount();
- if(sleep_time >= 0)
- Sleep(sleep_time);
- #endif
- Update();
- Render();
- SwapBuffers(hdc);
- }
- Shutdown();
- OpenGLUnbindContext(hwnd, hdc, hglrc);
- return (int)msg.wParam;
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) {
- switch (iMsg) {
- case WM_CREATE :
- break;
- case WM_CLOSE :
- DestroyWindow(hwnd);
- break;
- case WM_DESTROY :
- PostQuitMessage(0);
- break;
- case WM_KEYDOWN:
- case WM_SYSKEYDOWN:
- if (wParam == VK_RETURN)
- if ((HIWORD(lParam) & KF_ALTDOWN))
- ToggleFullscreen();
- break;
- case WM_SIZE:
- OpenGLResetProjection();
- break;
- case WM_ACTIVATE:
- if (LOWORD(wParam) != WA_INACTIVE)
- Suspend(false);
- else
- Suspend(true);
- break;
- case WM_SYSKEYUP:
- case WM_SYSCHAR:
- case WM_PAINT:
- case WM_ERASEBKGND:
- return 0;
- }
- return DefWindowProc(hwnd, iMsg, wParam, lParam);
- }
- HGLRC OpenGLBindContext(HDC hdc) {
- PIXELFORMATDESCRIPTOR pfd;
- memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
- pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
- pfd.nVersion = 1;
- pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
- pfd.iPixelType = PFD_TYPE_RGBA;
- pfd.cColorBits = 24;
- pfd.cDepthBits = 32;
- pfd.cStencilBits = 8;
- pfd.iLayerType = PFD_MAIN_PLANE;
- int pixelFormat = ChoosePixelFormat(hdc, &pfd);
- SetPixelFormat(hdc, pixelFormat, &pfd);
- HGLRC context = wglCreateContext(hdc);
- wglMakeCurrent(hdc, context);
- return context;
- }
- void OpenGLUnbindContext(HWND hwnd, HDC hdc, HGLRC hglrc) {
- wglMakeCurrent(NULL, NULL);
- wglDeleteContext(hglrc);
- ReleaseDC(hwnd, hdc);
- }
- bool CheckIfAlreadyRunning(void) {
- HWND hWnd = FindWindow(WND_CLASSNAME, WND_TITLE);
- if (hWnd) {
- if (IsIconic(hWnd))
- ShowWindow(hWnd, SW_RESTORE);
- SetForegroundWindow(hWnd);
- return true;
- }
- return false;
- }
- void ToggleFullscreen() {
- if(!fullScreen) {
- GetWindowRect(hwnd, &windowRect);
- HDC hdc = GetDC(hwnd);
- SetDisplayMode(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), GetDeviceCaps(hdc, BITSPIXEL), GetDeviceCaps(hdc, VREFRESH));
- SetWindowLongPtr(hwnd, GWL_STYLE, WS_POPUP);
- SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE);
- SetWindowPos(hwnd, 0, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), SWP_NOZORDER);
- ShowCursor(FALSE);
- ReleaseDC(hwnd, hdc);
- fullScreen = true;
- } else {
- SetDisplayMode(0, 0, 0, 0);
- SetWindowLongPtr(hwnd, GWL_STYLE, WS_OVERLAPPEDWINDOW);
- SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE);
- int iWindowWidth = windowRect.right - windowRect.left;
- int iWindowHeight = windowRect.bottom - windowRect.top;
- SetWindowPos(hwnd, 0, windowRect.left, windowRect.top, iWindowWidth, iWindowHeight, SWP_NOZORDER);
- ShowCursor(TRUE);
- fullScreen = false;
- }
- OpenGLResetProjection();
- }
- void SetDisplayMode(int width, int height, int bpp, int refreshRate) {
- if(width == 0 && height == 0 && bpp == 0 && refreshRate == 0) {
- ChangeDisplaySettings(NULL, 0);
- return;
- }
- DEVMODE dm;
- dm.dmSize = sizeof(DEVMODE);
- int i = 0;
- while(EnumDisplaySettings(NULL, i++, &dm)) {
- if(dm.dmPelsWidth == width && dm.dmPelsHeight == height &&
- dm.dmBitsPerPel == bpp && dm.dmDisplayFrequency == refreshRate) {
- if(ChangeDisplaySettings(&dm, CDS_TEST) == DISP_CHANGE_SUCCESSFUL) {
- ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
- return;
- }
- }
- }
- }
- void OpenGLResetProjection() {
- RECT window = { 0 };
- GetClientRect(hwnd, &window);
- glViewport(0, 0, window.right - window.left, window.bottom - window.top);
- float fov = 62.0f;
- float aspect = (float)(window.right - window.left) / (float)(window.bottom - window.top);
- float top = openGLNear * float(tanf(fov * 3.14159265f / 360.0f));
- float bottom = -1.0f * top;
- float right = bottom * aspect;
- float left = top * aspect;
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glFrustum(left, right, bottom, top, openGLNear, openGLFar);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement