#include "Gunngine_Win32.h" #include #pragma comment(lib, "opengl32.lib") #pragma comment(lib, "glew32s.lib") LRESULT CALLBACK WindProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { //case WM_SETCURSOR: //{ // SetCursor(0); //} break; case WM_DESTROY: { wglDeleteContext(hglrc); ReleaseDC(hWnd, hDc); DestroyWindow(hWnd); PostQuitMessage(0); } break; default: return DefWindowProc(hWnd, msg, wParam, lParam); } } #ifdef GUNN_DEBUG int main() { WinMain(GetModuleHandle(nullptr), GetModuleHandle(nullptr), nullptr, SW_SHOW); return(0); } #endif int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevinstance, LPSTR cmdLine, int nCmdShow) { HINSTANCE instance = GetModuleHandle(nullptr); wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WindProc; wc.hInstance = instance; wc.lpszClassName = "GunngineWindowClass"; wc.hCursor = LoadCursor(NULL, IDC_ARROW); if (!RegisterClass(&wc)) { printf("Error registering window class\n"); return(-1); } hWnd = CreateWindow("GunngineWindowClass", "Gunngine", WS_OVERLAPPEDWINDOW&~WS_MAXIMIZEBOX ^ WS_THICKFRAME | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 1280, 720, NULL, NULL, hInstance, NULL); if (!hWnd) { printf("Error creating window\n"); return(-1); } hDc = GetDC(hWnd); if (!hDc) { printf("Error creating device context"); return(-1); } DesiredPixelFormat.nSize = sizeof(PIXELFORMATDESCRIPTOR); DesiredPixelFormat.nVersion = 1; DesiredPixelFormat.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER; DesiredPixelFormat.cDepthBits = 24; DesiredPixelFormat.cColorBits = 32; DesiredPixelFormat.cAlphaBits = 8; DesiredPixelFormat.iLayerType = PFD_MAIN_PLANE; SuggestedPixelFormatIndex = ChoosePixelFormat(hDc, &DesiredPixelFormat); DescribePixelFormat(hDc, SuggestedPixelFormatIndex, sizeof(SuggestedPixelFormat), &SuggestedPixelFormat); if (!SetPixelFormat(hDc, SuggestedPixelFormatIndex, &SuggestedPixelFormat)) { printf("Error setting pixel format\n"); return(-1); } int attribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 3, WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, 0, }; hglrc = wglCreateContext(hDc); if (!wglMakeCurrent(hDc, hglrc)) { printf("Error creating OpenGL Context!"); return(-1); } else { wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB"); wglSwapInterval = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT"); HGLRC sharedContext = 0; modernContext = wglCreateContextAttribsARB(hDc, sharedContext, attribs); if (!wglMakeCurrent(hDc, modernContext)) { printf("Error creating OpenGL Context with Attribs!"); return(-1); } wglDeleteContext(hglrc); hglrc = modernContext; } ReleaseDC(hWnd, hDc); wglSwapInterval(1); //if (glewInit() != GLEW_OK) //{ // printf("Failed to initailize GLEW\n"); // return(-1); //} std::cout << glGetString(GL_VERSION); QueryPerformanceCounter((LARGE_INTEGER*)&prevTime); QueryPerformanceFrequency((LARGE_INTEGER*)&countsPerSec); secondsPerCount = 1.0f / countsPerSec; while (msg.message != WM_QUIT) { if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { currentTime = 0; QueryPerformanceCounter((LARGE_INTEGER*)¤tTime); deltaTime = (currentTime - prevTime) * secondsPerCount * 1000; HandleEvents(); Update(); Render(hDc); prevTime = currentTime; } } return(0); } void HandleEvents() { } void Update() { #if 0 printf("%F\n", deltaTime); std::stringstream ss; ss << "Frame Time: " << deltaTime; SetWindowText(hWnd, ss.str().c_str()); #endif } void Render(HDC hDc) { glViewport(0, 0, 1280, 720); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); SwapBuffers(hDc); }