- 注册时间
- 2004-11-1
- 最后登录
- 2018-4-24
版主
- 积分
- 548
|
- //test.d
- //save as utf 16 LE
- //dmd test.d -L/exet:nt/su:windows:4.0
- import win32.windows;
- import std.c.string;
- import opengl.gl;
- pragma(lib,"win32.lib");
- pragma(lib,"gdi32.lib");
- pragma(lib,"opengl32.lib");
- pragma(lib,"glu32.lib");
- /* Make the class name into a global variable */
- const wchar* szClassName = "OpenGL";
- const wchar* szAppName = "OpenGL测试";
- extern (C) void gc_init();
- extern (C) void gc_term();
- extern (C) void _minit();
- extern (C) void _moduleCtor();
- extern (C) void _moduleDtor();
- extern (C) void _moduleUnitTests();
- extern (Windows) int WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
- {
- int result;
- gc_init(); // initialize garbage collector
- _minit(); // initialize module constructor table
- try
- {
- _moduleCtor(); // call module constructors
- _moduleUnitTests(); // run unit tests (optional)
- result = myWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
- _moduleDtor(); // call module destructors
- }
- catch (Object o) // catch any uncaught exceptions
- {
- MessageBoxA(null, cast(char *)o.toString(), "Error",
- MB_OK | MB_ICONEXCLAMATION);
- result = 0; // failed
- }
- gc_term(); // run finalizers; terminate garbage collector
- return result;
- }
- int myWinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nFunsterStil)
- {
- HWND hWnd; /* This is the handle for our window */
- MSG messages; /* Here messages to the application are saved */
- WNDCLASSEXW wincl; /* Data structure for the windowclass */
- HDC hDC;
- HGLRC hRC;
- bool bQuit = false;
- /* The Window structure */
- wincl.hInstance = hThisInstance;
- wincl.lpszClassName = szClassName;
- wincl.lpfnWndProc = &WindowProcedure; /* This function is called by windows */
- wincl.style = CS_OWNDC; /* Catch double-clicks */
- wincl.cbSize = WNDCLASSEXW.sizeof;
- /* Use default icon and mouse-pointer */
- wincl.hIcon = null;
- wincl.hIconSm = null;
- wincl.hCursor = null;
- wincl.lpszMenuName = null; /* No menu */
- wincl.cbClsExtra = 0; /* No extra bytes after the window class */
- wincl.cbWndExtra = 0; /* structure or the window instance */
- /* Use Windows's default color as the background of the window */
- wincl.hbrBackground = cast(HBRUSH) COLOR_BACKGROUND;
- /* Register the window class, and if it fails quit the program */
- if (!RegisterClassExW (&wincl))
- return 0;
- /* The class is registered, let's create the program*/
- hWnd = CreateWindowExW (
- 0, /* Extended possibilites for variation */
- szClassName, /* Classname */
- szAppName, /* Title Text */
- WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE, /* default window */
- CW_USEDEFAULT, /* Windows decides the position */
- CW_USEDEFAULT, /* where the window ends up on the screen */
- 250, /* The programs width */
- 250, /* and height in pixels */
- HWND_DESKTOP, /* The window is a child-window to desktop */
- null, /* No menu */
- hThisInstance, /* Program Instance handler */
- null /* No Window Creation data */
- );
- /* enable OpenGL for the window */
- EnableOpenGL (hWnd, &hDC, &hRC);
- /* Make the window visible on the screen */
- ShowWindow (hWnd, nFunsterStil);
- while (!bQuit)
- {
- if (PeekMessage(&messages, null, 0, 0, PM_REMOVE))
- {
- if (messages.message == WM_QUIT)
- {
- bQuit = true;
- }
- else
- {
- /* Translate virtual-key messages into character messages */
- TranslateMessage(&messages);
- /* Send message to WindowProcedure */
- DispatchMessageW(&messages);
- }
- }
- else
- {
- draw(hDC);
- }
- }
- /* shutdown OpenGL */
- DisableOpenGL (hWnd, hDC, hRC);
- /* destroy the window explicitly */
- DestroyWindow (hWnd);
- /* The program return-value is 0 - The value that PostQuitMessage() gave */
- return messages.wParam;
- }
- /* This function is called by the Windows function DispatchMessage() */
- extern(Windows) LRESULT WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message) /* handle the messages */
- {
- case WM_DESTROY:
- PostQuitMessage (0); /* send a WM_QUIT to the message queue */
- break;
- default: /* for messages that we don't deal with */
- return DefWindowProcW (hwnd, message, wParam, lParam);
- }
- return 0;
- }
- void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
- {
- PIXELFORMATDESCRIPTOR pfd;
- int iFormat;
- /* get the device context (DC) */
- *hDC = GetDC (hWnd);
- /* set the pixel format for the DC */
- memset(&pfd, pfd.sizeof,1);
- pfd.nSize = pfd.sizeof;
- pfd.nVersion = 1;
- pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
- pfd.iPixelType = PFD_TYPE_RGBA;
- pfd.cColorBits = 24;
- pfd.cDepthBits = 16;
- pfd.iLayerType = 0;
- //pfd.iLayerType = PFD_MAIN_PLANE;
- iFormat = ChoosePixelFormat (*hDC, &pfd);
- SetPixelFormat (*hDC, iFormat, &pfd);
- /* create and enable the render context (RC) */
- *hRC = wglCreateContext( *hDC );
- wglMakeCurrent( *hDC, *hRC );
- }
- void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
- {
- wglMakeCurrent (null,null);
- wglDeleteContext (hRC);
- ReleaseDC (hWnd, hDC);
- }
- void draw(HDC hDC)
- {
- glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
- glClear (GL_COLOR_BUFFER_BIT);
- glPushMatrix ();
- glRotatef (0.0f, 0.0f, 0.0f, 1.0f);
- glBegin (GL_TRIANGLES);
- glColor3f (1.0f, 0.0f, 0.0f);
- glVertex2f (0.0f, 1.0f);
- glColor3f (0.0f, 1.0f, 0.0f);
- glVertex2f (0.87f, -0.5f);
- glColor3f (0.0f, 0.0f, 1.0f);
- glVertex2f (-0.87f, -0.5f);
- glEnd ();
- glPopMatrix ();
- SwapBuffers (hDC);
- Sleep (1);
- }
复制代码 |
|