您的位置:首页 > 运维架构

ATL opengl

2015-10-08 09:55 344 查看
#include <atlbase.h>
#include <atlwin.h>
#include <gl/glew.h>
#include <gl/GL.h>
#pragma comment(lib, "opengl32.lib")
extern CComModule _Module;

CComModule _Module;

class MyWindow : public CWindowImpl<MyWindow, CWindow, CFrameWinTraits>
{
public:
MyWindow()
: m_hDC(0)
, m_hGLRC(0)
{
Create(NULL, rcDefault);
}
public:
DECLARE_WND_CLASS(_T("MyWindow"));
BEGIN_MSG_MAP(MyWindow)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
MESSAGE_HANDLER(WM_SIZE, OnSize)
MESSAGE_HANDLER(WM_PAINT, OnPaint)
MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBkgnd)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
END_MSG_MAP()
public:
LRESULT OnCreate(UINT msg, WPARAM wParam, LPARAM lParam, BOOL & bHandled)
{
m_hDC = GetDC();

PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),  /* size */
1,                              /* version */
PFD_SUPPORT_OPENGL |
PFD_DRAW_TO_WINDOW |
PFD_DOUBLEBUFFER,               /* support double-buffering */
PFD_TYPE_RGBA,                  /* color type */
16,                             /* prefered color depth */
0, 0, 0, 0, 0, 0,               /* color bits (ignored) */
0,                              /* no alpha buffer */
0,                              /* alpha bits (ignored) */
0,                              /* no accumulation buffer */
0, 0, 0, 0,                     /* accum bits (ignored) */
16,                             /* depth buffer */
0,                              /* no stencil buffer */
0,                              /* no auxiliary buffers */
PFD_MAIN_PLANE,                 /* main layer */
0,                              /* reserved */
0, 0, 0,                        /* no layer, visible, damage masks */
};
int pixelFormat = ChoosePixelFormat(m_hDC, &pfd);
SetPixelFormat(m_hDC, pixelFormat, &pfd);
m_hGLRC = wglCreateContext(m_hDC);
if (!m_hGLRC)
{
PostQuitMessage(0);
}
wglMakeCurrent(m_hDC, m_hGLRC);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
return true;
}

LRESULT OnSize(UINT msg, WPARAM wParam, LPARAM lParam, BOOL & bHandled)
{
return true;
}

LRESULT OnPaint(UINT msg, WPARAM wParam, LPARAM lParam, BOOL & bHandled)
{
if (m_hGLRC)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
if (m_hDC)
SwapBuffers(m_hDC);
return true;
}

LRESULT OnEraseBkgnd(UINT msg, WPARAM wParam, LPARAM lParam, BOOL & bHandled)
{

return false;
}

LRESULT OnDestroy(UINT msg, WPARAM wParam, LPARAM lParam, BOOL & bHandled)
{
wglMakeCurrent(NULL, NULL);
if (m_hGLRC)
wglDeleteContext(m_hGLRC);
if (m_hDC)
ReleaseDC(m_hDC);
m_hDC = 0;
m_hGLRC = 0;
PostQuitMessage(0);
return true;
}
public:
void Show()
{
ShowWindow(SW_SHOW);
UpdateWindow();
}
private:
HDC  m_hDC;
HGLRC m_hGLRC;
};

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
_Module.Init(NULL, hInstance);
MyWindow win;
win.Show();
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
_Module.Term();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: