Esempio n. 1
0
        int ChoosePixelFormat(ref NativeAPI.PixelFormatDescriptor pfd)
        {
            try
            {
                using (DummyWindow dummyWindow = new DummyWindow(ref pfd))
                {
                    EnumerateSupportedSamples(dummyWindow);

                    if (dummyWindow.Extensions.Contains("WGL_EXT_swap_control"))
                    {
                        dummyWindow.GetNativeProc("wglSwapIntervalEXT", out SwapInterval);
                    }

                    if (dummyWindow.Extensions.Contains("WGL_ARB_pixel_format"))
                    {
                        NativeAPI.wglChoosePixelFormatARB ChoosePixelFormatARB;

                        dummyWindow.GetNativeProc("wglChoosePixelFormatARB", out ChoosePixelFormatARB);

                        if (dummyWindow.Extensions.Contains("WGL_ARB_multisample") == false)
                        {
                            Samples = 0;
                        }

                        int[] attrib =
                        {
                            NativeAPI.WGL_DRAW_TO_WINDOW_ARB, 1,
                            NativeAPI.WGL_ACCELERATION_ARB, NativeAPI.WGL_FULL_ACCELERATION_ARB,
                            NativeAPI.WGL_SUPPORT_OPENGL_ARB, 1,
                            NativeAPI.WGL_DOUBLE_BUFFER_ARB, 1,
                            NativeAPI.WGL_COLOR_BITS_ARB, 24,
                            NativeAPI.WGL_RED_BITS_ARB, 8,
                            NativeAPI.WGL_GREEN_BITS_ARB, 8,
                            NativeAPI.WGL_BLUE_BITS_ARB, 8,
                            NativeAPI.WGL_ALPHA_BITS_ARB, 0,
                            NativeAPI.WGL_DEPTH_BITS_ARB, 24,
                            NativeAPI.WGL_STENCIL_BITS_ARB, 8,
                            0, 0,
                            0, 0,
                            0
                        };

                        if (Samples > 0)
                        {
                            attrib[attrib.Length - 5 + 0] = NativeAPI.WGL_SAMPLE_BUFFERS_ARB;
                            attrib[attrib.Length - 5 + 1] = 1;
                            attrib[attrib.Length - 5 + 2] = NativeAPI.WGL_SAMPLES_ARB;
                            attrib[attrib.Length - 5 + 3] = Samples;
                        }

                        int count;
                        bool validpf = false;
                        int[] piFormats = new int[1];
                        do
                        {
                            if (!ChoosePixelFormatARB(dummyWindow.dc, attrib, null, 1, piFormats, out count))
                            {
                                return 0;
                            }
                            if (count == 0)
                            {
                                if (Samples != 0)
                                {
                                    Samples--;
                                    attrib[attrib.Length - 5 + 3] = Samples;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            else
                            {
                                validpf = true;
                            }

                        }
                        while (!validpf && count == 0);

                        if (validpf)
                        {
                            return piFormats[0];
                        }
                    }
                }
            }
            catch (ApplicationException)
            {
            }

            return NativeAPI.ChoosePixelFormat(this.dc, ref pfd);
        }
Esempio n. 2
0
 void MoveMouseCursorToMiddle(NativeAPI.Rectangle screenRect)
 {
     ignoreMouseMove = true;
     NativeAPI.SetCursorPos((screenRect.Left + screenRect.Right) / 2, (screenRect.Top + screenRect.Bottom) / 2);
 }
Esempio n. 3
0
        uint WindowProc(IntPtr hwnd, NativeAPI.WindowMessage uMsg, uint wParam, uint lParam)
        {
            switch (uMsg)
            {
                case NativeAPI.WindowMessage.DESTROY:
                    NativeAPI.PostQuitMessage(0);
                    break;

                case NativeAPI.WindowMessage.CLOSE:
                    OnClose();
                    return 0;

                case NativeAPI.WindowMessage.SIZE:
                    NativeAPI.Rectangle rect = new NativeAPI.Rectangle();
                    NativeAPI.GetClientRect(handle, out rect);
                    if (!Fullscreen && (Width != rect.Right || Height != rect.Bottom))
                    {
                        Width = rect.Right;
                        Height = rect.Bottom;
                        OnResize(Width, Height);
                    }
                    break;

                case NativeAPI.WindowMessage.ACTIVATE:
                    if (wParam == 0)
                    {
                        Focused = false;
                        OnFocusLost();

                        if (mouseCaptured)
                        {
                            NativeAPI.ClipCursor(IntPtr.Zero);
                            NativeAPI.ShowCursor(true);
                        }
                    }
                    else
                    {
                        Focused = true;
                        OnFocusGained();

                        if (mouseCaptured)
                        {
                            NativeAPI.ShowCursor(false);

                            lastX = Width / 2;
                            lastY = Height / 2;

                            NativeAPI.Rectangle crect = new NativeAPI.Rectangle(0, 0, Width, Height);
                            NativeAPI.ClientToScreen(handle, ref crect.LeftTop);
                            NativeAPI.ClientToScreen(handle, ref crect.RightBottom);

                            MoveMouseCursorToMiddle(crect);

                            NativeAPI.ClipCursor(ref crect);
                        }
                    }
                    break;

                case NativeAPI.WindowMessage.CHAR:
                    OnEvent(new InputEvents.TextEvent((char)wParam));
                    break;

                case NativeAPI.WindowMessage.KEYDOWN:
                case NativeAPI.WindowMessage.SYSKEYDOWN:
                    if ((lParam & (1 << 30)) == 0)
                    {
                        InputEvents.Key keyDown = (wParam == NativeAPI.VK_SHIFT ? GetShiftState(true) : FromVirtualKeyCode(wParam, lParam));
                        bool altDown = (NativeAPI.GetAsyncKeyState(NativeAPI.VK_MENU) >> 16) != 0;
                        bool ctrlDown = (NativeAPI.GetAsyncKeyState(NativeAPI.VK_CONTROL) >> 16) != 0;
                        bool shiftDown = (NativeAPI.GetAsyncKeyState(NativeAPI.VK_SHIFT) >> 16) != 0;
                        OnEvent(new InputEvents.KeyPressedEvent(keyDown, altDown, ctrlDown, shiftDown));
                    }
                    break;

                case NativeAPI.WindowMessage.KEYUP:
                case NativeAPI.WindowMessage.SYSKEYUP:
                    InputEvents.Key keyUp = (wParam == NativeAPI.VK_SHIFT ? GetShiftState(false) : FromVirtualKeyCode(wParam, lParam));
                    bool altUp = (NativeAPI.GetAsyncKeyState(NativeAPI.VK_MENU) >> 16) != 0;
                    bool ctrlUp = (NativeAPI.GetAsyncKeyState(NativeAPI.VK_CONTROL) >> 16) != 0;
                    bool shiftUp = (NativeAPI.GetAsyncKeyState(NativeAPI.VK_SHIFT) >> 16) != 0;
                    OnEvent(new InputEvents.KeyReleasedEvent(keyUp, altUp, ctrlUp, shiftUp));
                    break;

                case NativeAPI.WindowMessage.MOUSEWHEEL:
                    OnEvent(new InputEvents.MouseWheelEvent((int)(wParam >> 16) / 120));
                    break;

                case NativeAPI.WindowMessage.LBUTTONDOWN:
                    NativeAPI.SetCapture(hwnd);
                    OnEvent(new InputEvents.MouseButtonPressedEvent(InputEvents.MouseButton.Left, (int)(lParam & 0xFFFF), (int)(lParam >> 16)));
                    break;

                case NativeAPI.WindowMessage.LBUTTONUP:
                    OnEvent(new InputEvents.MouseButtonReleasedEvent(InputEvents.MouseButton.Left, (int)(lParam & 0xFFFF), (int)(lParam >> 16)));
                    NativeAPI.ReleaseCapture();
                    break;

                case NativeAPI.WindowMessage.RBUTTONDOWN:
                    NativeAPI.SetCapture(hwnd);
                    OnEvent(new InputEvents.MouseButtonPressedEvent(InputEvents.MouseButton.Right, (int)(lParam & 0xFFFF), (int)(lParam >> 16)));
                    break;

                case NativeAPI.WindowMessage.RBUTTONUP:
                    OnEvent(new InputEvents.MouseButtonReleasedEvent(InputEvents.MouseButton.Right, (int)(lParam & 0xFFFF), (int)(lParam >> 16)));
                    NativeAPI.ReleaseCapture();
                    break;

                case NativeAPI.WindowMessage.MBUTTONDOWN:
                    NativeAPI.SetCapture(hwnd);
                    OnEvent(new InputEvents.MouseButtonPressedEvent(InputEvents.MouseButton.Middle, (int)(lParam & 0xFFFF), (int)(lParam >> 16)));
                    break;

                case NativeAPI.WindowMessage.MBUTTONUP:
                    OnEvent(new InputEvents.MouseButtonReleasedEvent(InputEvents.MouseButton.Middle, (int)(lParam & 0xFFFF), (int)(lParam >> 16)));
                    NativeAPI.ReleaseCapture();
                    break;

                case NativeAPI.WindowMessage.XBUTTONDOWN:
                    NativeAPI.SetCapture(hwnd);
                    InputEvents.MouseButton xButtonDown = (wParam >> 16 == NativeAPI.VK_XBUTTON1 ? InputEvents.MouseButton.XButton1 : InputEvents.MouseButton.XButton2);
                    OnEvent(new InputEvents.MouseButtonPressedEvent(xButtonDown, (int)(lParam & 0xFFFF), (int)(lParam >> 16)));
                    break;

                case NativeAPI.WindowMessage.XBUTTONUP:
                    InputEvents.MouseButton xButtonUp = (wParam >> 16 == NativeAPI.VK_XBUTTON1 ? InputEvents.MouseButton.XButton1 : InputEvents.MouseButton.XButton2);
                    OnEvent(new InputEvents.MouseButtonReleasedEvent(xButtonUp, (int)(lParam & 0xFFFF), (int)(lParam >> 16)));
                    NativeAPI.ReleaseCapture();
                    break;

                case NativeAPI.WindowMessage.MOUSEMOVE:
                    if (ignoreMouseMove)
                    {
                        ignoreMouseMove = false;
                    }
                    else if (Focused)
                    {
                        int newX = (int)(lParam & 0xFFFF);
                        int newY = (int)(lParam >> 16);

                        if (lastX != newX || lastY != newY)
                        {
                            OnEvent(new InputEvents.MouseMoveEvent(newX, newY));

                            lastX = newX;
                            lastY = newY;
                        }

                        if (mouseCaptured)
                        {
                            NativeAPI.Rectangle mrect = new NativeAPI.Rectangle(0, 0, Width, Height);

                            NativeAPI.ClientToScreen(handle, ref mrect.LeftTop);
                            NativeAPI.ClientToScreen(handle, ref mrect.RightBottom);

                            MoveMouseCursorToMiddle(mrect);
                        }
                    }

                    break;
            }

            return NativeAPI.DefWindowProc(hwnd, uMsg, wParam, lParam);
        }
Esempio n. 4
0
        void GetWindowStyle(bool fullscreen, bool resizable, ref int width, ref int height, out int x, out int y, out NativeAPI.WindowStyle style, out NativeAPI.WindowStyleEx styleEx)
        {
            style = NativeAPI.WindowStyle.ClipChildren | NativeAPI.WindowStyle.ClipSiblings;
            styleEx = 0;

            if (fullscreen)
            {
                style |= NativeAPI.WindowStyle.Popup;
                styleEx |= NativeAPI.WindowStyleEx.Topmost | NativeAPI.WindowStyleEx.ApplicationWindow;
                x = 0;
                y = 0;
            }
            else
            {
                style |= NativeAPI.WindowStyle.Caption | NativeAPI.WindowStyle.SystemMenu | NativeAPI.WindowStyle.MinimizeBox;
                if (resizable)
                {
                    style |= NativeAPI.WindowStyle.SizeBox | NativeAPI.WindowStyle.MaximizeBox;
                }
                styleEx |= NativeAPI.WindowStyleEx.WindowEdge;

                NativeAPI.Rectangle rect = new NativeAPI.Rectangle(0, 0, width, height);
                NativeAPI.AdjustWindowRectEx(ref rect, style, false, styleEx);
                x = NativeAPI.CW_USEDEFAULT;
                y = NativeAPI.CW_USEDEFAULT;
                width = rect.Right - rect.Left;
                height = rect.Bottom - rect.Top;
            }
        }