Code:
#include "windows.h"
#include "commctrl.h"
#include "resource.h"
#include "gl/gl.h"
#include "gl/glu.h"
#include "rates.h"
HWND hWnd, hStatusBar, hRunTimeInfo, ogl;
HDC hDC, hOGLDC, runHDC;
HGLRC hRC, hOGLRC;
RECT rcStatusBar, rcMain;
const LPCWSTR appname = TEXT("OpenGL Sample");
const LPCWSTR oglname = TEXT("OpenGLName");
double r, b, g;
void DrawGraphics()
{
//wglMakeCurrent(hOGLDC, hOGLRC);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslated(0, 0, -10);
double x = -3, y =-0.25;
double w = 0.5 , h = 0.005;
// Draw a square
for (x=-3;x<=3;x+=h)
{
r = 255; b = 0, g = 0;
glBegin(GL_QUADS);
glColor3d(r/255,g/255,b/255);
glVertex3d(y,x, 0);
glVertex3d(y+w, x, 0);
glVertex3d(y+w, x+h, 0);
glVertex3d(y, x+h, 0);
glEnd();
}
SwapBuffers(hDC);
}
// Set up pixel format for graphics initialization
void SetupPixelFormat()
{
PIXELFORMATDESCRIPTOR pfd, *ppfd;
int pixelformat;
ppfd = &pfd;
ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
ppfd->nVersion = 1;
ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
ppfd->dwLayerMask = PFD_MAIN_PLANE;
ppfd->iPixelType = PFD_TYPE_COLORINDEX;
ppfd->cColorBits = 16;
ppfd->cDepthBits = 16;
ppfd->cAccumBits = 0;
ppfd->cStencilBits = 0;
pixelformat = ChoosePixelFormat(hDC, ppfd);
SetPixelFormat(hDC, pixelformat, ppfd);
}
// Initialize OpenGL graphics
void InitGraphics()
{
hDC = GetDC(hWnd);
SetupPixelFormat();
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
glClearColor(0, 0, 0, 0.5);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
}
// Resize graphics to fit window
// Handle window events and messages
LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
const LPCWSTR file = TEXT("File");
const LPCWSTR save = TEXT("Save");
const LPCWSTR saveas = TEXT("Save As");
const LPCWSTR exit = TEXT("Exit");
switch (uMsg)
{
case WM_CREATE:
{
ogl = CreateWindowEx(0, oglname, TEXT(""), WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE, 225 , 0, rcMain.top - 225, rcMain.bottom-rcStatusBar.bottom, hWnd, NULL, GetModuleHandle(NULL), NULL);
InitGraphicsOGL();
}
case WM_SIZE:
break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
// Default event handler
default:
return DefWindowProc (hWnd, uMsg, wParam, lParam);
break;
}
return 1;
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wndclass;
MSG msg;
// Define the window class
wndclass.style = 0;
wndclass.lpfnWndProc = (WNDPROC)MainWndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance, appname);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_HIGHLIGHT);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = appname;
WNDCLASS oc;
WNDPROC oglproc;
// Define the window class
oc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
oc.lpfnWndProc = (WNDPROC)MainWndProc;
oc.hInstance = hInstance;
oc.hbrBackground = (HBRUSH)COLOR_WINDOW;
oc.lpszClassName = oglname;
RegisterClass(&wndclass);
RegisterClass(&oc);
hWnd = CreateWindow(appname, appname, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
InitGraphics();
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
InitCommonControls();
hStatusBar = CreateWindowEx(0, STATUSCLASSNAME, NULL, WS_CHILD|WS_VISIBLE|SBARS_SIZEGRIP, 0, 0, 0, 0, hWnd, NULL, hInstance,NULL);
GetClientRect(hWnd, &rcMain);
GetClientRect(hStatusBar, &rcStatusBar);
hRunTimeInfo = CreateWindowEx(0, TEXT("STATIC"), NULL, WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE , 0, 0, 225, rcMain.bottom-rcStatusBar.bottom, hWnd, NULL, hInstance, 0); //rcMain.bottom-rcStatusBar.bottom
// Event loop
while (1)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == TRUE)
{
if (!GetMessage(&msg, NULL, 0, 0)) return TRUE;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
DrawGraphics();
}
wglDeleteContext(hRC);
ReleaseDC(hWnd, hDC);
}