Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.36 KB | None | 0 0
  1. #include "Gunngine_Win32.h"
  2. #include <iostream>
  3.  
  4. #pragma comment(lib, "opengl32.lib")
  5. #pragma comment(lib, "glew32s.lib")
  6.  
  7. LRESULT CALLBACK WindProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  8. {
  9.     switch (msg)
  10.     {
  11.         //case WM_SETCURSOR:
  12.         //{
  13.         //  SetCursor(0);
  14.         //} break;
  15.  
  16.         case WM_DESTROY:
  17.         {
  18.             wglDeleteContext(hglrc);
  19.             ReleaseDC(hWnd, hDc);
  20.             DestroyWindow(hWnd);
  21.  
  22.             PostQuitMessage(0);
  23.         } break;
  24.  
  25.         default:
  26.             return DefWindowProc(hWnd, msg, wParam, lParam);
  27.     }
  28. }
  29.  
  30. #ifdef GUNN_DEBUG
  31. int main()
  32. {
  33.     WinMain(GetModuleHandle(nullptr), GetModuleHandle(nullptr), nullptr, SW_SHOW);
  34.  
  35.     return(0);
  36. }
  37. #endif
  38.  
  39. int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevinstance, LPSTR cmdLine, int nCmdShow)
  40. {
  41.     HINSTANCE instance = GetModuleHandle(nullptr);
  42.  
  43.     wc.style                                     = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
  44.     wc.lpfnWndProc                               = WindProc;
  45.     wc.hInstance                                 = instance;
  46.     wc.lpszClassName                             = "GunngineWindowClass";
  47.     wc.hCursor                                   = LoadCursor(NULL, IDC_ARROW);
  48.  
  49.     if (!RegisterClass(&wc))
  50.     {
  51.         printf("Error registering window class\n");
  52.         return(-1);
  53.     }
  54.  
  55.     hWnd = CreateWindow("GunngineWindowClass", "Gunngine", WS_OVERLAPPEDWINDOW&~WS_MAXIMIZEBOX ^ WS_THICKFRAME | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 1280, 720, NULL, NULL, hInstance, NULL);
  56.     if (!hWnd)
  57.     {
  58.         printf("Error creating window\n");
  59.         return(-1);
  60.     }
  61.  
  62.     hDc = GetDC(hWnd);
  63.     if (!hDc)
  64.     {
  65.         printf("Error creating device context");
  66.         return(-1);
  67.     }
  68.                                                  
  69.     DesiredPixelFormat.nSize                     = sizeof(PIXELFORMATDESCRIPTOR);
  70.     DesiredPixelFormat.nVersion                  = 1;
  71.     DesiredPixelFormat.dwFlags                   = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
  72.     DesiredPixelFormat.cDepthBits                = 24;
  73.     DesiredPixelFormat.cColorBits                = 32;
  74.     DesiredPixelFormat.cAlphaBits                = 8;
  75.     DesiredPixelFormat.iLayerType                = PFD_MAIN_PLANE;
  76.  
  77.     SuggestedPixelFormatIndex = ChoosePixelFormat(hDc, &DesiredPixelFormat);
  78.     DescribePixelFormat(hDc, SuggestedPixelFormatIndex, sizeof(SuggestedPixelFormat), &SuggestedPixelFormat);
  79.     if (!SetPixelFormat(hDc, SuggestedPixelFormatIndex, &SuggestedPixelFormat))
  80.     {
  81.         printf("Error setting pixel format\n");
  82.         return(-1);
  83.     }
  84.  
  85.     int attribs[] =
  86.     {
  87.         WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
  88.         WGL_CONTEXT_MINOR_VERSION_ARB, 3,
  89.         WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
  90.         WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
  91.         0,
  92.     };
  93.  
  94.     hglrc = wglCreateContext(hDc);
  95.     if (!wglMakeCurrent(hDc, hglrc))
  96.     {
  97.         printf("Error creating OpenGL Context!");
  98.         return(-1);
  99.     }
  100.     else
  101.     {
  102.         wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
  103.         wglSwapInterval = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
  104.         HGLRC sharedContext = 0;
  105.         modernContext = wglCreateContextAttribsARB(hDc, sharedContext, attribs);
  106.         if (!wglMakeCurrent(hDc, modernContext))
  107.         {
  108.             printf("Error creating OpenGL Context with Attribs!");
  109.             return(-1);
  110.         }
  111.         wglDeleteContext(hglrc);
  112.         hglrc = modernContext;
  113.     }
  114.     ReleaseDC(hWnd, hDc);
  115.     wglSwapInterval(1);
  116.     //if (glewInit() != GLEW_OK)
  117.     //{
  118.     //  printf("Failed to initailize GLEW\n");
  119.     //  return(-1);
  120.     //}
  121.  
  122.     std::cout << glGetString(GL_VERSION);
  123.  
  124.     QueryPerformanceCounter((LARGE_INTEGER*)&prevTime);
  125.     QueryPerformanceFrequency((LARGE_INTEGER*)&countsPerSec);
  126.     secondsPerCount = 1.0f / countsPerSec;
  127.  
  128.     while (msg.message != WM_QUIT)
  129.     {
  130.         if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
  131.         {
  132.             TranslateMessage(&msg);
  133.             DispatchMessage(&msg);
  134.         }
  135.         else
  136.         {
  137.             currentTime = 0;
  138.             QueryPerformanceCounter((LARGE_INTEGER*)&currentTime);
  139.             deltaTime = (currentTime - prevTime) * secondsPerCount * 1000;
  140.  
  141.             HandleEvents();
  142.             Update();
  143.             Render(hDc);
  144.  
  145.             prevTime = currentTime;
  146.         }
  147.     }
  148.  
  149.     return(0);
  150. }
  151.  
  152. void HandleEvents()
  153. {
  154.  
  155. }
  156.  
  157. void Update()
  158. {
  159. #if 0
  160.     printf("%F\n", deltaTime);
  161.  
  162.     std::stringstream ss;
  163.     ss << "Frame Time: " << deltaTime;
  164.  
  165.     SetWindowText(hWnd, ss.str().c_str());
  166. #endif
  167. }
  168.  
  169. void Render(HDC hDc)
  170. {
  171.     glViewport(0, 0, 1280, 720);
  172.  
  173.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  174.     glClear(GL_COLOR_BUFFER_BIT);
  175.  
  176.     SwapBuffers(hDc);
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement