Exemple #1
0
        public void Init()
        {
            if (NativeApp != null)
            {
                throw new InvalidOperationException("Application was already initialized.");
            }

            if (X11Application.IsAvailable())
            {
                NativeApp = new X11Application();
            }
            else if (Win32Application.IsAvailable())
            {
                NativeApp = new Win32Application();
            }
            else
            {
                throw new InvalidOperationException("Cannot determine a suitable API.");
            }

            NativeApp.Init();

            Graphics   = new NGraphics(NativeApp.Graphics);
            ImageCodec = new NImageCodec(NativeApp.ImageCodec);
            Clipboard  = new NClipboard(NativeApp.Clipboard);
        }
Exemple #2
0
        public OpenGLWindow(INativeWindow parent, int x, int y, int width, int height) : base(new WindowCreateStruct {
            WindowTitle  = GameConfig.GameWindowTitle,                    //"IGE OpenGLWindow",
            X            = x,
            Y            = y,
            Width        = width,
            Height       = height,
            Style        = GameConfig.FullScreen ? WindowStyleFlags.Popup : Win32NativeWindow.DefaultStyle,
            ExStyle      = ExtendedWindowStyleFlags.ApplicationWindow,
            ParentWindow = (Win32NativeWindow)parent,
            Menu         = IntPtr.Zero,
            Param        = IntPtr.Zero,

            ClassName   = "IGEOpenGLWindow",
            ClassStyle  = WindowClassStyle.HREDRAW | WindowClassStyle.VREDRAW | WindowClassStyle.OWNDC,
            Background  = (IntPtr)0,                                                                                                                                                                                                                      // no background brush is needed since opengl has its own renderer
            Icon        = (GameConfig.IconResourceId != 0) ? IGE.Platform.Win32.API.Externals.LoadIcon(Win32Application.GetInstanceHandle(), (IntPtr)GameConfig.IconResourceId) : IGE.Platform.Win32.API.Externals.LoadIcon(IntPtr.Zero, (IntPtr) 32512), // 32512 = IDI_APPLICATION
            Cursor      = IGE.Platform.Win32.API.Externals.LoadCursor(IntPtr.Zero, (IntPtr) 32512),                                                                                                                                                       // 32512 = IDC_ARROW
            ClassMenu   = IntPtr.Zero,
            ClassExtra  = 0,
            WindowExtra = 0
        })
        {
            m_DeviceContext = null;
            if (!Exists)
            {
                return;
            }

            m_DeviceContext = new DeviceContext(this);
            if (m_DeviceContext.Disposed)
            {
                GameDebugger.EngineLog(LogLevel.Error, "Failed to properly create a device context");
                throw new UserFriendlyException("Failed to properly create a device context", "Graphics initialization error");
            }

            /*
             * // This enumerates all pixel formats, supported by the device context to the log file
             * for( int i = PixelFormat.GetCount(m_DeviceContext); i > 0; i-- ) {
             *      PixelFormat dpf = new PixelFormat(m_DeviceContext, i);
             *      GameDebugger.Log(dpf);
             * }
             */

            // change pixelformat for the window's device context
            //  | PixelFormatDescriptorFlags.SWAP_EXCHANGE
            PixelFormatDescriptor pfd = new PixelFormatDescriptor {
                Flags       = PixelFormatDescriptorFlags.DOUBLEBUFFER | PixelFormatDescriptorFlags.SUPPORT_OPENGL | PixelFormatDescriptorFlags.DRAW_TO_WINDOW,
                PixelType   = ApiPixelType.RGBA,
                ColorBits   = (byte)GameConfig.ColorBits,
                RedBits     = 8, GreenBits = 8, BlueBits = 8, AlphaBits = 8,
                RedShift    = 0, GreenShift = 0, BlueShift = 0, AlphaShift = 0,
                DepthBits   = (byte)GameConfig.DepthBufferBits,
                StencilBits = (byte)GameConfig.StencilBufferBits,
                AccumBits   = (byte)GameConfig.AccumBufferBits, AccumRedBits = 0, AccumGreenBits = 0, AccumBlueBits = 0, AccumAlphaBits = 0,
                AuxBuffers  = 0, LayerType = 0, LayerMask = 0, VisibleMask = 0, DamageMask = 0
            };

            PixelFormat pf = new PixelFormat(m_DeviceContext, ref pfd);

            GameDebugger.EngineLog(LogLevel.Debug, "{0}", pf);

            if (!pf.Exists)
            {
                throw new Exception("Could not find a suitable pixel format for an OpenGL window.");
            }

            bool res = pf.Apply();

            //GameDebugger.Log("Chosen: {0} {1}", pf.Exists, pf.ToString());
            //GameDebugger.Log("Apply result: {0}", res);
            if (!res)
            {
                GameDebugger.EngineLog(LogLevel.Debug, "Failed applying requested pixel format, trying to apply by a found index");

                PixelFormat pf2 = new PixelFormat(m_DeviceContext, pf.Index);

                GameDebugger.EngineLog(LogLevel.Debug, "{0}", pf2);

                res = pf2.Apply();
                //GameDebugger.Log("Real: {0} {1}", pf2.Exists, pf2.ToString());
                //GameDebugger.Log("Apply result: {0}", res);
                if (!res)
                {
                    throw new Exception(String.Format("Error trying to set {0}", pf2.ToString()));
                }
            }

            m_ResourceContext = new ResourceContext(m_DeviceContext);
            if (m_ResourceContext.Disposed)
            {
                int pixelFormats = PixelFormat.GetCount(m_DeviceContext);
                GameDebugger.EngineLog(LogLevel.Debug, "Supported pixel formats:");
                for (int i = 0; i < pixelFormats; i++)
                {
                    pf = new PixelFormat(m_DeviceContext, i);
                    GameDebugger.EngineLog(LogLevel.Debug, "{0}", pf);
                }
                throw new Exception("Could not create a resource context for the OpenGLWindow");
            }
            m_ResourceContext.Activate();

            // "reload" because we want context specific extension functions to be loaded as well
            WGL.RuntimeImport();
            GL.RuntimeImport();

            Application.IdleEvent += OnIdle;
        }
    /// <summary>
    /// Runs a specified application and starts the main loop to update its state. This is the entry point for a given application,
    /// and it should be called as soon as the process is launched, excluding any other additional initialization needed.
    /// </summary>
    /// <param name="application">The input application instance to run.</param>
    /// <returns>The exit code for the application.</returns>
    public static int Run(Win32Application application)
    {
        Win32ApplicationRunner.application = application;

        HMODULE hInstance = Windows.GetModuleHandleW(null);

        fixed(char *name = Assembly.GetExecutingAssembly().FullName)
        fixed(char *windowTitle = application.GetType().ToString())
        {
            // Initialize the window class
            WNDCLASSEXW windowClassEx = new()
            {
                cbSize        = (uint)sizeof(WNDCLASSEXW),
                style         = CS.CS_HREDRAW | CS.CS_VREDRAW,
                lpfnWndProc   = &WindowProc,
                hInstance     = hInstance,
                hCursor       = Windows.LoadCursorW(HINSTANCE.NULL, IDC.IDC_ARROW),
                lpszClassName = (ushort *)name
            };

            // Register the window class
            _ = Windows.RegisterClassExW(&windowClassEx);

            Rectangle windowRect = new(0, 0, 1280, 720);

            // Set the target window size
            _ = Windows.AdjustWindowRect((RECT *)&windowRect, WS.WS_OVERLAPPEDWINDOW, Windows.FALSE);

            uint height = (uint)(windowRect.Bottom - windowRect.Top);
            uint width  = (uint)(windowRect.Right - windowRect.Left);

            // Create the window and store a handle to it
            hwnd = Windows.CreateWindowExW(
                0,
                windowClassEx.lpszClassName,
                (ushort *)windowTitle,
                WS.WS_OVERLAPPEDWINDOW,
                Windows.CW_USEDEFAULT,
                Windows.CW_USEDEFAULT,
                (int)width,
                (int)height,
                HWND.NULL,
                HMENU.NULL,
                hInstance,
                (void *)GCHandle.ToIntPtr(GCHandle.Alloc(application))
                );

            MARGINS margins = default;

            margins.cxLeftWidth    = -1;
            margins.cxRightWidth   = -1;
            margins.cyTopHeight    = -1;
            margins.cyBottomHeight = -1;

            _ = Windows.DwmExtendFrameIntoClientArea(hwnd, &margins);
        }

        // Initialize the application
        application.OnInitialize(hwnd);

        // Display the window
        _ = Windows.ShowWindow(hwnd, SW.SW_SHOWDEFAULT);

        MSG msg = default;

        // Setup the render thread that enables smooth resizing of the window
        renderThread = new Thread(static args =>
Exemple #4
0
    private static int Run(DXSample sample)
    {
        var hInstance = GetModuleHandleW(null);

        return(Win32Application.Run(sample, hInstance, SW_SHOWDEFAULT));
    }