public static int Run(DXSample pSample, HINSTANCE hInstance, int nCmdShow)
        {
            var useWarpDevice = false;

            // Parse the command line parameters
            foreach (var arg in Environment.GetCommandLineArgs())
            {
                if (Program.Matches(arg, "warp"))
                {
                    useWarpDevice = true;
                    continue;
                }
            }

            fixed(char *lpszClassName = "DXSampleClass")
            fixed(char *lpWindowName = pSample.Name)
            {
                // Initialize the window class.
                var windowClass = new WNDCLASSEXW {
                    cbSize        = (uint)sizeof(WNDCLASSEXW),
                    style         = CS_HREDRAW | CS_VREDRAW,
                    lpfnWndProc   = &WindowProc,
                    hInstance     = hInstance,
                    hCursor       = LoadCursorW(IntPtr.Zero, (ushort *)IDC_ARROW),
                    lpszClassName = (ushort *)lpszClassName
                };

                _ = RegisterClassExW(&windowClass);

                // Create the window and store a handle to it.
                s_hwnd = CreateWindowExW(
                    0,
                    windowClass.lpszClassName,
                    (ushort *)lpWindowName,
                    WS_OVERLAPPEDWINDOW,
                    CW_USEDEFAULT,
                    CW_USEDEFAULT,
                    CW_USEDEFAULT,
                    CW_USEDEFAULT,
                    HWND.NULL,                              // We have no parent window.
                    HMENU.NULL,                             // We aren't using menus.
                    hInstance,
                    ((IntPtr)GCHandle.Alloc(pSample)).ToPointer()
                    );
            }

            RECT windowRect;

            _ = GetClientRect(s_hwnd, &windowRect);

            // Initialize the sample. OnInit is defined in each child-implementation of DXSample.
            var backgroundColor = new Vector4(0.0f, 0.2f, 0.4f, 1.0f);

            var size = new Size((windowRect.right - windowRect.left), (windowRect.bottom - windowRect.top));

            pSample.OnInit(DXGI_FORMAT_UNKNOWN, backgroundColor, DXGI_FORMAT_UNKNOWN, 1.0f, 2, s_hwnd, true, size, useWarpDevice);

            _ = ShowWindow(s_hwnd, nCmdShow);

            // Main sample loop.
            MSG msg;

            do
            {
                // Process any messages in the queue.
                if (PeekMessageW(&msg, IntPtr.Zero, 0, 0, PM_REMOVE) != 0)
                {
                    _ = TranslateMessage(&msg);
                    _ = DispatchMessageW(&msg);
                }
            }while (msg.message != WM_QUIT);

            pSample.OnDestroy();

            // Return this part of the WM_QUIT message to Windows.
            return((int)msg.wParam);
        }
Ejemplo n.º 2
0
        public static int Run(DXSample pSample, HINSTANCE hInstance, int nCmdShow)
        {
            // Parse the command line parameters
            pSample.ParseCommandLineArgs(Environment.GetCommandLineArgs());

            fixed(char *lpszClassName = "DXSampleClass")
            fixed(char *lpWindowName = pSample.Title)
            {
                // Requires an explicit cast until C# handles UnmanagedCallersOnly
                var wndProc = (delegate * stdcall <IntPtr, uint, nuint, nint, nint>)(delegate * managed <IntPtr, uint, nuint, nint, nint>) & WindowProc;

                // Initialize the window class.
                var windowClass = new WNDCLASSEXW {
                    cbSize        = (uint)sizeof(WNDCLASSEXW),
                    style         = CS_HREDRAW | CS_VREDRAW,
                    lpfnWndProc   = wndProc,
                    hInstance     = hInstance,
                    hCursor       = LoadCursorW(IntPtr.Zero, (ushort *)IDC_ARROW),
                    lpszClassName = (ushort *)lpszClassName
                };

                _ = RegisterClassExW(&windowClass);

                var windowRect = new RECT {
                    right  = unchecked ((int)pSample.Width),
                    bottom = unchecked ((int)pSample.Height)
                };

                _ = AdjustWindowRect(&windowRect, WS_OVERLAPPEDWINDOW, FALSE);

                // Create the window and store a handle to it.
                s_hwnd = CreateWindowExW(
                    0,
                    windowClass.lpszClassName,
                    (ushort *)lpWindowName,
                    WS_OVERLAPPEDWINDOW,
                    CW_USEDEFAULT,
                    CW_USEDEFAULT,
                    windowRect.right - windowRect.left,
                    windowRect.bottom - windowRect.top,
                    HWND.NULL,                              // We have no parent window.
                    HMENU.NULL,                             // We aren't using menus.
                    hInstance,
                    ((IntPtr)GCHandle.Alloc(pSample)).ToPointer()
                    );
            }

            // Initialize the sample. OnInit is defined in each child-implementation of DXSample.
            pSample.OnInit();

            _ = ShowWindow(s_hwnd, nCmdShow);

            // Main sample loop.
            MSG msg;

            do
            {
                // Process any messages in the queue.
                if (PeekMessageW(&msg, IntPtr.Zero, 0, 0, PM_REMOVE) != 0)
                {
                    _ = TranslateMessage(&msg);
                    _ = DispatchMessageW(&msg);
                }
            }while (msg.message != WM_QUIT);

            pSample.OnDestroy();

            // Return this part of the WM_QUIT message to Windows.
            return((int)msg.wParam);
        }