Esempio n. 1
0
        static void Main(string[] args)
        {
            GameWindowSettings gameWinSettings = new GameWindowSettings();
            var nativeWindowSettings           = new NativeWindowSettings()
            {
                Size       = new Vector2i(800, 600),
                Title      = "Habi",
                APIVersion = new System.Version(4, 5),
                API        = ContextAPI.OpenGL,
                Flags      = ContextFlags.ForwardCompatible,
                Profile    = ContextProfile.Core,
            };

            int width, height, xpos, ypos;

            unsafe
            {
                var monitor = GLFW.GetPrimaryMonitor();
                var vidmode = GLFW.GetVideoMode(monitor);
                width  = vidmode->Width - 200;
                height = vidmode->Height - 100;
                xpos   = (vidmode->Width - width) / 2;
                ypos   = (vidmode->Height - height) / 2;
            }

            nativeWindowSettings.Size     = new Vector2i(width, height);
            nativeWindowSettings.Location = new Vector2i(xpos, ypos);

            using (var game = new Draw2DGame(gameWinSettings, nativeWindowSettings))
            {
                game.Run();
            }
        }
Esempio n. 2
0
        private void InitWindow()
        {
            GLFW.WindowHint(WindowHintBool.SrgbCapable, true);
            GLFW.WindowHint(WindowHintInt.ContextVersionMajor, MinimumOpenGLVersion.Major);
            GLFW.WindowHint(WindowHintInt.ContextVersionMinor, MinimumOpenGLVersion.Minor);
            GLFW.WindowHint(WindowHintBool.OpenGLForwardCompat, true);
            GLFW.WindowHint(WindowHintOpenGlProfile.OpenGlProfile, OpenGlProfile.Core);
#if DEBUG
            GLFW.WindowHint(WindowHintBool.OpenGLDebugContext, true);
#endif
            GLFW.WindowHint(WindowHintString.X11ClassName, "SS14");
            GLFW.WindowHint(WindowHintString.X11InstanceName, "SS14");

            var width  = _configurationManager.GetCVar <int>("display.width");
            var height = _configurationManager.GetCVar <int>("display.height");

            Monitor *monitor = null;

            if (WindowMode == WindowMode.Fullscreen)
            {
                monitor = GLFW.GetPrimaryMonitor();
                var mode = GLFW.GetVideoMode(monitor);
                width  = mode->Width;
                height = mode->Height;
            }

            _glfwWindow = GLFW.CreateWindow(width, height, string.Empty, monitor, null);

            LoadWindowIcon();

            GLFW.SetCharCallback(_glfwWindow, _charCallback);
            GLFW.SetKeyCallback(_glfwWindow, _keyCallback);
            GLFW.SetWindowCloseCallback(_glfwWindow, _windowCloseCallback);
            GLFW.SetCursorPosCallback(_glfwWindow, _cursorPosCallback);
            GLFW.SetWindowSizeCallback(_glfwWindow, _windowSizeCallback);
            GLFW.SetScrollCallback(_glfwWindow, _scrollCallback);
            GLFW.SetMouseButtonCallback(_glfwWindow, _mouseButtonCallback);
            GLFW.SetWindowContentScaleCallback(_glfwWindow, _windowContentScaleCallback);

            GLFW.MakeContextCurrent(_glfwWindow);

            VSyncChanged();

            GLFW.GetFramebufferSize(_glfwWindow, out var fbW, out var fbH);
            _screenSize = (fbW, fbH);

            GLFW.GetWindowContentScale(_glfwWindow, out var scaleX, out var scaleY);
            _windowScale = (scaleX, scaleY);

            InitGLContext();

            // Initializing OTK 3 seems to mess with the current context, so ensure it's still set.
            // This took me f*****g *forever* to debug because this manifested differently on nvidia drivers vs intel mesa.
            // So I thought it was a calling convention issue with the calli OpenTK emits.
            // Because, in my tests, I had InitGLContext() AFTER the test with a delegate-based invoke of the proc.
            GLFW.MakeContextCurrent(_glfwWindow);

            InitOpenGL();
        }
Esempio n. 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NativeWindowSettings"/> class.
 /// </summary>
 public NativeWindowSettings()
 {
     unsafe
     {
         GLFWProvider.EnsureInitialized();
         CurrentMonitor = new MonitorHandle((IntPtr)GLFW.GetPrimaryMonitor());
     }
 }
Esempio n. 4
0
        private Matrix4x4 WorldMatrix; // r_world_matrix

        public unsafe GLDevice(GameWindow form, Monitor openTKDevice)
            : base(typeof(GLDeviceDesc),
                   typeof(GLGraphics),
                   typeof(GLTextureAtlas),
                   typeof(GLModel),
                   typeof(GLModelDesc),
                   typeof(GLAliasModel),
                   typeof(GLAliasModelDesc),
                   typeof(GLTexture),
                   typeof(GLTextureDesc))
        {
            this.Form         = form;
            this.OpenTKDevice = GLFW.GetPrimaryMonitor();

            this.TextureFilters = new GLTextureFilter[]
            {
                new("GL_NEAREST", TextureMinFilter.Nearest, TextureMagFilter.Nearest),
                new("GL_LINEAR", TextureMinFilter.Linear, TextureMagFilter.Linear),
                new("GL_NEAREST_MIPMAP_NEAREST", TextureMinFilter.NearestMipmapNearest, TextureMagFilter.Nearest),
                new("GL_LINEAR_MIPMAP_NEAREST", TextureMinFilter.LinearMipmapNearest, TextureMagFilter.Linear),
                new("GL_NEAREST_MIPMAP_LINEAR", TextureMinFilter.NearestMipmapLinear, TextureMagFilter.Nearest),
                new("GL_LINEAR_MIPMAP_LINEAR", TextureMinFilter.LinearMipmapLinear, TextureMagFilter.Linear)
            };

            this.BlendModes = new GLTextureBlendMode[]
            {
                new("GL_MODULATE", TextureEnvMode.Modulate),
                new("GL_ADD", TextureEnvMode.Add),
                new("GL_REPLACE", TextureEnvMode.Replace),
                new("GL_DECAL", TextureEnvMode.Decal),
                new("GL_REPLACE_EXT", TextureEnvMode.ReplaceExt),
                new("GL_TEXTURE_ENV_BIAS_SGIX", TextureEnvMode.TextureEnvBiasSgix),
                new("GL_COMBINE", TextureEnvMode.Combine)
            };

            this.PixelFormats = new GLPixelFormat[]
            {
                new("GL_LUMINANCE", PixelFormat.Luminance),
                new("GL_RGBA", PixelFormat.Rgba),
                new("GL_RGB", PixelFormat.Rgb),
                new("GL_BGR", PixelFormat.Bgr),
                new("GL_BGRA", PixelFormat.Bgra),
                new("GL_ALPHA", PixelFormat.Alpha)
            };
        }
Esempio n. 5
0
 public unsafe DisplayMode[] getModeList()
 {
     return(GLFW.GetVideoModes(GLFW.GetPrimaryMonitor())
            .Where(videoMode => videoMode.RedBits + videoMode.GreenBits + videoMode.BlueBits == this.oldDisplayMode.BitDepth)
            .GroupBy(videoMode => $"{videoMode.Height},{videoMode.Height}")
            .Select(videoModes => videoModes.OrderBy(videoMode => videoMode.RefreshRate).Last())
            .OrderBy(videoMode => videoMode.Width)
            .ThenBy(videoMode => videoMode.Height)
            .Select(
                videoMode => new DisplayMode(
                    videoMode.Width,
                    videoMode.Height,
                    videoMode.RefreshRate,
                    videoMode.RedBits + videoMode.GreenBits + videoMode.BlueBits
                    )
                )
            .ToArray());
 }
Esempio n. 6
0
        protected override void WindowModeChanged()
        {
            if (_glfwWindow == null)
            {
                return;
            }

            if (WindowMode == WindowMode.Fullscreen)
            {
                var monitor = GLFW.GetPrimaryMonitor();
                var mode    = GLFW.GetVideoMode(monitor);
                GLFW.SetWindowMonitor(_glfwWindow, GLFW.GetPrimaryMonitor(), 0, 0, mode->Width, mode->Height,
                                      mode->RefreshRate);
            }
            else
            {
                GLFW.SetWindowMonitor(_glfwWindow, null, 0, 0, 1280, 720, 0);
            }
        }
Esempio n. 7
0
            // glfwGetWindowMonitor only works for fullscreen windows.
            // Picks the monitor with the top-left corner of the window.
            private Monitor *MonitorForWindow(Window *window)
            {
                GLFW.GetWindowPos(window, out var winPosX, out var winPosY);
                var monitors = GLFW.GetMonitorsRaw(out var count);

                for (var i = 0; i < count; i++)
                {
                    var monitor = monitors[i];
                    GLFW.GetMonitorPos(monitor, out var monPosX, out var monPosY);
                    var videoMode = GLFW.GetVideoMode(monitor);

                    var box = Box2i.FromDimensions(monPosX, monPosY, videoMode->Width, videoMode->Height);
                    if (box.Contains(winPosX, winPosY))
                    {
                        return(monitor);
                    }
                }

                // Fallback
                return(GLFW.GetPrimaryMonitor());
            }
Esempio n. 8
0
        public unsafe int setMode(Size dim, int mode, bool fullscreen)
        {
            var windowSize = new Size();

            VID.Printf(Defines.PRINT_ALL, "Initializing OpenGL display\n");
            VID.Printf(Defines.PRINT_ALL, $"...setting mode {mode}:");

            if (this.oldDisplayMode == null)
            {
                GLFW.Init();
                var videoMode = GLFW.GetVideoMode(GLFW.GetPrimaryMonitor())[0];

                this.oldDisplayMode =
                    new(videoMode.Width, videoMode.Height, videoMode.RefreshRate, videoMode.RedBits + videoMode.GreenBits + videoMode.BlueBits);
            }

            if (!VID.GetModeInfo(ref windowSize, mode))
            {
                VID.Printf(Defines.PRINT_ALL, " invalid mode\n");

                return(Base.rserr_invalid_mode);
            }

            VID.Printf(Defines.PRINT_ALL, $" {windowSize.Width} {windowSize.Height}{'\n'}");

            if (this.window != null)
            {
                this.shutdown();
            }

            if (fullscreen)
            {
                var displayMode =
                    this.getModeList().FirstOrDefault(displayMode => displayMode.Width == windowSize.Width && displayMode.Height == windowSize.Height)
                    ?? this.oldDisplayMode;

                this.window = new(GameWindowSettings.Default, new()
                {
                    Profile = ContextProfile.Compatability, Size = new(displayMode.Width, displayMode.Height), IsFullscreen = true
                });

                VID.Printf(
                    Defines.PRINT_ALL,
                    $"...setting fullscreen {displayMode.Width}x{displayMode.Height}x{displayMode.BitDepth}@{displayMode.RefreshRate}Hz\n"
                    );
            }
            else
            {
                this.window = new(GameWindowSettings.Default, new()
                {
                    Profile = ContextProfile.Compatability, Size = new(windowSize.Width, windowSize.Height)
                });

                VID.Printf(Defines.PRINT_ALL, $"...setting window {windowSize.Width}x{windowSize.Height}\n");
            }

            this.window.Focus();
            this.window.Closed += OpenTkDriver.QuitOnClose;

            OpenTkKBD.Window        = this.window;
            this.window.KeyDown    += OpenTkKBD.Listener.KeyDown;
            this.window.KeyUp      += OpenTkKBD.Listener.KeyUp;
            this.window.MouseDown  += OpenTkKBD.Listener.MouseDown;
            this.window.MouseUp    += OpenTkKBD.Listener.MouseUp;
            this.window.MouseMove  += OpenTkKBD.Listener.MouseMove;
            this.window.MouseWheel += OpenTkKBD.Listener.MouseWheel;

            Program.UpdateLoop = _ => this.window.Run();

            var initialized = false;

            var updateAccumulator = 0.0;
            var renderAccumulator = 0.0;

            this.window.UpdateFrame += args =>
            {
                updateAccumulator += args.Time * 1000;

                var elapsed = (int)updateAccumulator;

                if (elapsed <= 0)
                {
                    return;
                }

                Qcommon.FrameUpdate(elapsed);
                updateAccumulator -= elapsed;
            };

            this.window.RenderFrame += args =>
            {
                if (!initialized)
                {
                    this.init(0, 0);
                    initialized = true;
                }

                renderAccumulator += args.Time * 1000;

                var elapsed = (int)renderAccumulator;

                if (elapsed <= 0)
                {
                    return;
                }

                Qcommon.FrameRender(elapsed);
                renderAccumulator -= elapsed;
            };

            this.window.Resize += args =>
            {
                Base.setVid(this.window.ClientSize.X, this.window.ClientSize.Y);
                VID.NewWindow(this.window.ClientSize.X, this.window.ClientSize.Y);
            };

            return(Base.rserr_ok);
        }
Esempio n. 9
0
        public virtual int SetMode(Size dim, int mode, bool fullscreen)
        {
            VID.Printf(Defines.PRINT_ALL, "Initializing OpenGL display\\n");
            VID.Printf(Defines.PRINT_ALL, "...setting mode " + mode + ":");

            unsafe {
                Monitor *device = GLFW.GetPrimaryMonitor();
                if (oldDisplayMode == null)
                {
                    oldDisplayMode = GLFW.GetVideoMode(device)[0];
                }
            }

            if (!VID.GetModeInfo(out var newDim, mode))
            {
                VID.Printf(Defines.PRINT_ALL, " invalid mode\\n");
                return(Base.rserr_invalid_mode);
            }

            VID.Printf(Defines.PRINT_ALL, " " + newDim.Width + " " + newDim.Height + '\\');
            Shutdown();

            var newInstance = (window == null);

            if (!newInstance)
            {
                window.Dispose();
            }

            window = new GameWindow(new GameWindowSettings(), new NativeWindowSettings
            {
                Title        = "Q2Sharp (jogl)",
                Size         = new OpenTK.Mathematics.Vector2i(newDim.Width, newDim.Height),
                StartVisible = false,
                WindowBorder = OpenTK.Windowing.Common.WindowBorder.Fixed,
                IsFullscreen = false
                               //Icon = new OpenTK.Windowing.Common.Input.WindowIcon(new OpenTK.Windowing.Common.Input.Image(32,32,null)),
            });

            window.RenderFrame += (t) => Program.Frame();
            Program.RunWindow  += () => window.Run();

            //ImageIcon icon = new ImageIcon(GetType().GetResource("/icon-small.png"));
            Bitmap bitmap = (Bitmap)Bitmap.FromStream(GetType().Assembly.GetManifestResourceStream("/icon-small.png"));

            byte[] pixels = new byte[bitmap.Width * bitmap.Height];

            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    var color = bitmap.GetPixel(x, y);
                    Array.Copy(new byte[] { color.R, color.G, color.B, color.A }, 0, pixels, (y * bitmap.Width + x) * 4, 4);
                }
            }

            Image icon = new Image(bitmap.Width, bitmap.Height, pixels);

            window.Icon = new WindowIcon(icon);

            window.Minimized += (e) => JOGLKBD.listener.ComponentHidden(e);
            window.Maximized += (e) =>
            {
                JOGLKBD.c = window;
                JOGLKBD.listener.ComponentShown(e);
            };
            window.Move       += (e) => JOGLKBD.listener.ComponentMoved(e);
            window.Closing    += (e) => Cbuf.ExecuteText(Defines.EXEC_APPEND, "quit");
            window.Resize     += (e) => JOGLKBD.listener.ComponentResized(e);
            window.KeyDown    += (e) => JOGLKBD.listener.KeyPressed(e);
            window.KeyUp      += (e) => JOGLKBD.listener.KeyReleased(e);
            window.TextInput  += (e) => JOGLKBD.listener.KeyTyped(e);
            window.MouseEnter += () => JOGLKBD.listener.MouseEntered();
            window.MouseLeave += () => JOGLKBD.listener.MouseExited();
            window.MouseMove  += (e) => JOGLKBD.listener.MouseMoved(e);
            window.MouseDown  += (e) => JOGLKBD.listener.MousePressed(e);
            window.MouseUp    += (e) => JOGLKBD.listener.MouseReleased(e);
            window.MouseWheel += (e) => JOGLKBD.listener.MouseWheelMoved(e);
            //window.drag += ( e ) => JOGLKBD.listener.MouseDragged( e );

            if (fullscreen)
            {
                window.WindowState = OpenTK.Windowing.Common.WindowState.Fullscreen;
                VideoMode VideoMode = FindDisplayMode(newDim);
                newDim.Width       = VideoMode.Width;
                newDim.Height      = VideoMode.Height;
                window.WindowState = WindowState.Fullscreen;
                if (window.IsFullscreen)
                {
                    unsafe
                    {
                        GLFW.SetWindowSize(window.WindowPtr, VideoMode.Width, VideoMode.Height);
                    }
                }
                window.Location = new OpenTK.Mathematics.Vector2i();
                window.Size     = new OpenTK.Mathematics.Vector2i(VideoMode.Width, VideoMode.Height);
                VID.Printf(Defines.PRINT_ALL, "...setting fullscreen " + GetModeString(VideoMode) + '\\');
                window.IsVisible = true;
            }
            else
            {
                window.Location  = new OpenTK.Mathematics.Vector2i(window_xpos, window_ypos);
                window.IsVisible = true;
            }

            Base.SetVid(newDim.Width, newDim.Height);
            VID.NewWindow(newDim.Width, newDim.Height);

            return(Base.rserr_ok);
        }