|
- #include <windows.h>
- #include <Wingdi.h>
- #include <GL/gl.h>
-
- #include "example.h"
-
- const char g_szClassName[] = "OpenGLWindowClass";
-
- HDC hdc;
- HGLRC hglrc;
- HWND hwnd;
- int done = 0;
-
- int screenw, screenh;
-
- void SysShutdown (void)
- {
- done = 1;
- exampleFinish();
- wglMakeCurrent(hdc, NULL);
- wglDeleteContext(hglrc);
- PostQuitMessage(0);
- }
-
- void setPixelFormat()
- {
- PIXELFORMATDESCRIPTOR pfd =
- {
- sizeof(PIXELFORMATDESCRIPTOR),
- 1,
- PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,
- PFD_TYPE_RGBA,
- 32,
- 0,0,0,0,0,0,0,0,0,0,0,0,0, // useles parameters
- 16,
- 0,0,0,0,0,0,0
- };
- int indexPixelFormat = ChoosePixelFormat(hdc, &pfd);
- SetPixelFormat(hdc, indexPixelFormat, &pfd);
- }
-
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch(msg)
- {
- case WM_CREATE:
- if ((hdc = GetDC(hwnd)) == NULL) // get device context
- {
- MessageBox(hwnd, "Failed to Get the Window Device Context", "Device Context Error", MB_OK);
- SysShutdown();
- break;
- }
- setPixelFormat();
- if ((hglrc = wglCreateContext(hdc)) == NULL)
- {
- MessageBox(hwnd, "Failed to Create the OpenGL Rendering Context", "OpenGL Rendering Context Error", MB_OK);
- SysShutdown();
- break;
- }
- if (!wglMakeCurrent(hdc, hglrc))
- {
- MessageBox(hwnd, "Failed to make OpenGL Rendering Context current", "OpenGL Rendering Context Error", MB_OK);
- SysShutdown();
- break;
- }
- glDisable(GL_DEPTH_TEST);
- glDisable(GL_CULL_FACE);
- if (!exampleInit())
- {
- SysShutdown();
- break;
- }
- break;
-
- case WM_SIZE:
- screenw = LOWORD(lParam);
- screenh = HIWORD(lParam);
- break;
-
- case WM_PAINT:
- glClearColor(1.0, 1.0, 1.0, 1.0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glViewport(0, 0, screenw, screenh);
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, screenw, screenh, 0, -10, 10);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
-
- if (!exampleRender(screenw, screenh))
- SysShutdown();
-
- SwapBuffers(hdc);
- break;
-
- case WM_CLOSE:
- SysShutdown();
- DestroyWindow(hwnd);
- break;
-
- case WM_DESTROY:
- SysShutdown();
- PostQuitMessage(0);
- break;
-
- default:
- return DefWindowProc(hwnd, msg, wParam, lParam);
- }
- return 0;
- }
-
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- WNDCLASSEX wc;
- MSG Msg;
-
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.style = 0;
- wc.lpfnWndProc = WndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wc.lpszMenuName = NULL;
- wc.lpszClassName = g_szClassName;
- wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
-
- if(!RegisterClassEx(&wc))
- {
- MessageBox(NULL, "Window Registration Failed!", "Error!",
- MB_ICONEXCLAMATION | MB_OK);
- return 0;
- }
-
- if ((hwnd = CreateWindowEx(
- WS_EX_CLIENTEDGE,
- g_szClassName,
- "libTextSuite example",
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
- NULL, NULL, hInstance, NULL)) == NULL)
- {
- MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
- return 0;
- }
-
- ShowWindow(hwnd, nCmdShow);
- UpdateWindow(hwnd);
-
- while(GetMessage(&Msg, NULL, 0, 0) > 0 && done == 0)
- {
- TranslateMessage(&Msg);
- DispatchMessage(&Msg);
- }
- return Msg.wParam;
- }
|