Пример #1
0
        public static GlfwWindow CreateWindow()
        {
            GlfwVidMode videoMode = GetVideoMode(GetPrimaryMonitor());

            WindowHint(WindowAttrib.Decorated, 0);
            WindowHint(WindowAttrib.Resizable, 0);

            return(GlfwCore.glfwCreateWindow(videoMode.Width, videoMode.Height, string.Empty, GlfwMonitor.Null, GlfwWindow.Null));
        }
Пример #2
0
        public static void CenterWindow(GlfwWindow window)
        {
            int windowW, windowH;

            GetWindowSize(window, out windowW, out windowH);

            GlfwVidMode mode = GetVideoMode(GetPrimaryMonitor());

            int desktopW = mode.Width;
            int desktopH = mode.Height;

            SetWindowPos(window, desktopW / 2 - windowW / 2, desktopH / 2 - windowH / 2);
        }
Пример #3
0
        public static GlfwWindowPtr initWindow(string windowTitle)
        {
            GlfwMonitorPtr mon    = Glfw.GetPrimaryMonitor();
            GlfwVidMode    mode   = Glfw.GetVideoMode(mon);
            GlfwWindowPtr  window = Glfw.CreateWindow(g_width, g_height, windowTitle, GlfwMonitorPtr.Null, GlfwWindowPtr.Null);

            Glfw.MakeContextCurrent(window);
            WindowTitle = windowTitle;

            //Get Version Info
            string renderer = GL.GetString(StringName.Renderer);
            string version  = GL.GetString(StringName.Version);

            Log.GL_Log("Renderer: " + renderer);
            Log.GL_Log("Version: " + version);
            Log.GL_Log("-----------------------------------");

            return(window);
        }
Пример #4
0
        static void ApplySettingsAndCreateWindow()
        {
            #region Apply render settings BEFORE window init
            if (Settings.Window.Borders)
            {
                Glfw.WindowHint(WindowHint.Decorated, 1);
            }
            else
            {
                Glfw.WindowHint(WindowHint.Decorated, 0);
            }

            if (Settings.Window.Fullscreen)
            {
                Glfw.WindowHint(WindowHint.Floating, 1); // Always On Top
            }
            else
            {
                Glfw.WindowHint(WindowHint.Floating, 0);
            }

            Glfw.WindowHint(WindowHint.Focused, 1);     // Set Focus On Init
            Glfw.WindowHint(WindowHint.Resizeable, 1);
            Glfw.WindowHint(WindowHint.AutoIconify, 1); // Auto Mininize and Restore FullScreen Window
            #endregion

            if (Settings.Window.Fullscreen)
            {
                if (Settings.Window.FullscreenWindowed)
                {
                    #region Fullscreen Windowed
                    if (Settings.Window.Borders)
                    {
                        Settings.Window.X      = Screen.PrimaryScreen.WorkingArea.X;
                        Settings.Window.Y      = Screen.PrimaryScreen.WorkingArea.Y;
                        Settings.Window.Width  = Screen.PrimaryScreen.WorkingArea.Width;
                        Settings.Window.Height = Screen.PrimaryScreen.WorkingArea.Height;
                    }
                    else
                    {
                        Settings.Window.X      = Screen.PrimaryScreen.Bounds.X;
                        Settings.Window.Y      = Screen.PrimaryScreen.Bounds.Y;
                        Settings.Window.Width  = Screen.PrimaryScreen.Bounds.Width;
                        Settings.Window.Height = Screen.PrimaryScreen.Bounds.Height;
                    }

                    Window = CreateNewWindow(Settings.Window.Width, Settings.Window.Height, Settings.Window.Title,
                                             GlfwMonitorPtr.Null, Window);

                    Glfw.SetWindowPos(Window, Settings.Window.X, Settings.Window.Y);
                    #endregion
                }
                else
                {
                    #region Real Fullscreen
                    Settings.Window.X      = 0;
                    Settings.Window.Y      = 0;
                    Settings.Window.Width  = Settings.Window.OriginalWidth;
                    Settings.Window.Height = Settings.Window.OriginalHeight;

                    GlfwMonitorPtr Monitor;
                    GlfwVidMode    VideoMode = new GlfwVidMode
                    {
                        RedBits     = Settings.Window.RedBits,
                        GreenBits   = Settings.Window.GreenBits,
                        BlueBits    = Settings.Window.BlueBits,
                        Width       = Settings.Window.Width,
                        Height      = Settings.Window.Height,
                        RefreshRate = Settings.Window.RefreshRate
                    };

                    GlfwMonitorPtr[] MonitorsList = Glfw.GetMonitors();
                    GlfwVidMode[]    VideoModes;

                    if (Settings.Window.ActiveMonitor >= 0 && MonitorsList.Length - 1 >= Settings.Window.ActiveMonitor)
                    {
                        Monitor = MonitorsList[Settings.Window.ActiveMonitor];
                    }
                    else
                    {
                        Monitor = Glfw.GetPrimaryMonitor();
                    }

                    VideoModes = Glfw.GetVideoModes(Monitor);

                    int VModeIndex = -1;
                    for (int i = 0; i < VideoModes.Length; i++)
                    {
                        if (VideoModes[i].Equals(VideoMode))
                        {
                            VModeIndex = i;
                        }
                    }

                    if (VModeIndex == -1)
                    {
                        VideoMode = Glfw.GetVideoMode(Monitor);
                    }

                    Glfw.WindowHint(WindowHint.RedBits, VideoMode.RedBits);
                    Glfw.WindowHint(WindowHint.GreenBits, VideoMode.GreenBits);
                    Glfw.WindowHint(WindowHint.BlueBits, VideoMode.BlueBits);
                    Glfw.WindowHint(WindowHint.RefreshRate, VideoMode.RefreshRate);

                    //Fullscreen refresh rate limit, work only if 'V-Sync=true'
                    Glfw.WindowHint(WindowHint.RefreshRate, Settings.Window.RefreshRateLimit);

                    Window = CreateNewWindow(Settings.Window.Width, Settings.Window.Height, Settings.Window.Title, Monitor, Window);
                    #endregion
                }
            }
            else // Fullscreen = False
            {
                #region Window Mode
                Window = CreateNewWindow(Settings.Window.Width, Settings.Window.Height, Settings.Window.Title,
                                         GlfwMonitorPtr.Null, Window);

                Settings.Window.X      = Settings.Window.OriginalX;
                Settings.Window.Y      = Settings.Window.OriginalY;
                Settings.Window.Width  = Settings.Window.OriginalWidth;
                Settings.Window.Height = Settings.Window.OriginalHeight;

                // Move Window To Center
                if (Settings.Window.CenterWindow)
                {
                    Settings.Window.X = (Screen.PrimaryScreen.WorkingArea.Width - Settings.Window.Width) / 2;
                    Settings.Window.Y = (Screen.PrimaryScreen.WorkingArea.Height - Settings.Window.Height) / 2;
                }

                Glfw.SetWindowSize(Window, Settings.Window.Width, Settings.Window.Height);
                Glfw.SetWindowPos(Window, Settings.Window.X, Settings.Window.Y);
                #endregion
            }

            Glfw.MakeContextCurrent(Window);

            #region Apply render settings AFTER window init
            Log.WriteLine("GPU: " + GL.GetString(StringName.Renderer));
            Settings.Window.IsFocused = true;
            #region Mouse jump fix
            Glfw.SetInputMode(Window, InputMode.CursorMode, CursorMode.CursorCaptured);
            Glfw.GetWindowPos(Window, out Settings.Window.X, out Settings.Window.Y);
            Glfw.SetCursorPos(Window,
                              (double)(Settings.Window.X + Settings.Window.Width / 2),
                              (double)(Settings.Window.Y + Settings.Window.Height / 2));
            #endregion

            Glfw.SwapInterval(Settings.Graphics.VSyncSwapInterval); //V-sync

            GL.ClearColor(Color4.Gray);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            #endregion
        }
Пример #5
0
        public void Run(float targetFPS)
        {
            TargetFrameRate = targetFPS;

            if (!Glfw.Init())
            {
                throw new Exception("Failed to initialize glfw!");
            }

            try
            {
                // Setup the error callback
                Glfw.SetErrorCallback(OnError);

                // Configure the window settings
                Glfw.WindowHint(WindowHint.Resizeable, startResizable ? 1 : 0);
                Glfw.WindowHint(WindowHint.Samples, 0);

                // Create the window
                handle = Glfw.CreateWindow(startWidth, startHeight, title,
                                           GlfwMonitorPtr.Null, GlfwWindowPtr.Null);
                HasFocus = true;

                // TODO: check if window was initialized correctly

                // Set the gl context
                Glfw.MakeContextCurrent(handle);

                // Get the primary monitor
                primaryMonitor          = Glfw.GetMonitors()[0];
                primaryMonitorVideoMode = Glfw.GetVideoMode(primaryMonitor);

                Glfw.GetWindowSize(handle, out width, out height);
                Center();

                // Setup window events
                Glfw.SetScrollCallback(handle, OnInputScroll);
                Glfw.SetCursorPosCallback(handle, OnMouseMove);
                Glfw.SetWindowSizeCallback(handle, OnSizeChanged);
                Glfw.SetWindowFocusCallback(handle, OnWindowFocusChanged);
                Glfw.SetKeyCallback(handle, OnInputKeyChanged);
                Input.Initialize(this);

                // Set defaults and load
                SetVSync(false);

                Renderer = new MasterRenderer(width, height, startOptions);
                InitOpenAL();

                GLError.Begin();
                Load();
                ErrorCode initError = GLError.End();
                if (initError != ErrorCode.NoError)
                {
                    throw new Exception(string.Format("Uncaught opengl initialization error! {0}", initError));
                }

                // Begin game loop
                double lastTime = Glfw.GetTime();

                while (!Glfw.WindowShouldClose(handle))
                {
                    double now = Glfw.GetTime();
                    float  dt  = (float)(now - lastTime);
                    lastTime = now;

                    // Process current deltatime
                    HandleFPS(dt);

                    // Check for input events before we call Input.Begin
                    Glfw.PollEvents();

                    // Check for window size change.
                    // We only call the OnResized event here so that
                    // when a user is custom resizing it doesn't get invoked
                    // a thousand times.
                    if (lastDrawnWidth != width || lastDrawnHeight != height)
                    {
                        lastDrawnWidth  = width;
                        lastDrawnHeight = height;
                        OnSafeResized();
                    }

                    // Update
                    Input.Begin();
                    Renderer.Update(dt);
                    Update(dt);
                    Input.End();

                    // Draw
                    Renderer.Prepare(dt);
                    Draw(dt);
                    Renderer.Render(dt);

                    // Check for any uncaught opengl exceptions
                    ErrorCode glError = GL.GetError();
                    if (glError != ErrorCode.NoError)
                    {
                        throw new Exception(string.Format("Uncaught OpenGL Error: {0}", glError));
                    }

                    // Draw the buffers
                    Glfw.SwapBuffers(handle);

                    if (!vsyncEnabled)
                    {
                        // Sleep to avoid cpu cycle burning
                        double startSleepNow = Glfw.GetTime();
                        double timeToWait    = targetDeltaTime - (startSleepNow - now);

                        while (timeToWait > 0)
                        {
                            Thread.Sleep(0);
                            double sleepNow = Glfw.GetTime();
                            timeToWait   -= (sleepNow - startSleepNow);
                            startSleepNow = sleepNow;
                        }
                    }
                }

                Unload();
            }
            finally
            {
                Glfw.DestroyWindow(handle);
            }
        }