/// <summary> /// Setup the native platform and creates a window. /// </summary> /// <param name="config">Configuration for the platform - usually passed from the engine.</param> protected virtual void Setup(Configurator config) { _config = config; SetupInput(); SetupInternal(config); // Check if the platform and graphics initialization was successful. if (Context == null) { Engine.CriticalError(new Exception("Platform couldn't create graphics context.")); return; } // Make this the current context, and bind it. // "There /can/ be only one." Context.MakeCurrent(); Gl.BindAPI(Context); // Set display mode, show and focus. DisplayMode = config.InitialDisplayMode; if (!config.HiddenWindow) WindowState = WindowState.Normal; IsOpen = true; _pauseOnFocusLoss = !config.DebugMode; }
/// <inheritdoc /> public override void Setup(Vector2 renderSize) { Gl.BindAPI(s => Glfw.GetProcAddress(s)); Gl.QueryContextVersion(); // Check if context was created. if (Gl.CurrentVersion == null) { ErrorHandler.SubmitError(new Exception("Couldn't connect to OpenGL context.")); return; } // Bind current thread as the GLThread. GLThread.BindThread(); // Renderer bootstrap. Engine.Log.Info("Creating OpenGL GraphicsManager...", MessageSource.GL); Engine.Log.Info($"GL: {Gl.CurrentVersion} on {Gl.CurrentRenderer}", MessageSource.GL); Engine.Log.Info($"GLSL: {Gl.CurrentShadingVersion}", MessageSource.GL); CreateDefaultShader(); CreateDefaultIbo(); // Set default state. DefaultGLState(); ResetState(); // Clear to transparent black. Gl.ClearColor(0, 0, 0, 0); Engine.Log.Info("GraphicsManager ready.", MessageSource.GL); }
static ContextManager() { Gl.Initialize(); Glfw.Init(); Glfw.DefaultWindowHints(); Glfw.WindowHint(Glfw.ContextVersionMajor, 4); Glfw.WindowHint(Glfw.ContextVersionMinor, 1); Glfw.WindowHint(Glfw.OpenglProfile, Glfw.OpenglCoreProfile); Glfw.WindowHint(Glfw.OpenglForwardCompat, Glfw.True); Glfw.WindowHint(Glfw.Doublebuffer, Glfw.True); Glfw.WindowHint(Glfw.Visible, Glfw.False); DefaultContext = Glfw.CreateWindow(1, 1, "", IntPtr.Zero, IntPtr.Zero); if (DefaultContext == IntPtr.Zero) { throw new NullReferenceException("Default OpenGL context failed to initialize."); } SetActiveContext(DefaultContext); Glx.IsRequired = true; Gl.BindAPI(new KhronosVersion(4, 1, "gl"), new Gl.Extensions()); }
public void Gl_BindAPI_Exceptions() { Assert.Throws <ArgumentNullException>(() => Gl.BindAPI(null, null)); Assert.Throws <ArgumentNullException>(() => Gl.BindAPI(null, new Gl.Extensions())); // Assert.Throws<Exception>(() => Gl.QueryContextVersion()); }
public void TestBindAPIPerformance() { // Ensure cached attributes Gl.BindAPI(); Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i < 10; i++) { Gl.BindAPI(); } sw.Stop(); Console.WriteLine("BindAPI(): {0} ms", sw.ElapsedMilliseconds / 10.0f); }
static void Main(string[] args) { Gl.Initialize(); using (var window = new NativeWindow(800, 600, "Walking Simulator")) { Glfw.MakeContextCurrent(window); Gl.BindAPI(); while (!window.IsClosing) { Gl.ClearColor(0.0F, 0.0F, 0.0F, 1.0F); Gl.Clear((ClearBufferMask)Gl.COLOR_BUFFER_BIT); Console.WriteLine(Glfw.Time); window.SwapBuffers(); Glfw.PollEvents(); } } }
/// <summary> /// Setup the native platform and creates a window. /// </summary> /// <param name="config">Configuration for the platform - usually passed from the engine.</param> internal virtual void Setup(Configurator config) { OnMouseScroll.AddListener(scroll => { _mouseScrollAccum += scroll; return(true); }); PopulateKeyCodes(); SetupPlatform(config); // Check if the platform initialization was successful. if (Window == null) { Engine.SubmitError(new Exception("Platform couldn't create window.")); return; } if (Window.Context == null) { Engine.SubmitError(new Exception("Platform couldn't create context.")); return; } // Bind this window and its context. // "There /can/ be only one." Window.Context.MakeCurrent(); Gl.BindAPI(Window.Context.GetProcAddress); // Set display mode, show and focus. Window.DisplayMode = config.InitialDisplayMode; Window.WindowState = WindowState.Normal; // Attach default key behavior. OnKey.AddListener(DefaultButtonBehavior); IsSetup = true; IsOpen = true; }
internal static IntPtr CreateContext(Vector2I size, string title) { Glfw.WindowHint(Glfw.ContextVersionMajor, 4); Glfw.WindowHint(Glfw.ContextVersionMinor, 1); Glfw.WindowHint(Glfw.OpenglProfile, Glfw.OpenglCoreProfile); Glfw.WindowHint(Glfw.OpenglForwardCompat, Glfw.True); Glfw.WindowHint(Glfw.Doublebuffer, Glfw.True); var context = Glfw.CreateWindow(size.X, size.Y, title, IntPtr.Zero, DefaultContext); if (context == IntPtr.Zero) { throw new NullReferenceException($"Context \"{title}\" failed to initialize."); } RunInSeparateContext(() => { Glx.IsRequired = true; Gl.BindAPI(new KhronosVersion(4, 1, "gl"), new Gl.Extensions()); }, context); return(context); }
public void Gl_TestBindAPI() { Assert.Throws <ArgumentNullException>(() => Gl.BindAPI(null, null)); Assert.Throws <ArgumentNullException>(() => Gl.BindAPI(null, new Gl.Extensions())); }