예제 #1
0
        static GlfwWindowPtr CreateNewWindow(int Width, int Height, string Title, GlfwMonitorPtr Monitor, GlfwWindowPtr OldWindow)
        {
            GlfwWindowPtr NewWindow = Glfw.CreateWindow(Width, Height, Title, Monitor, OldWindow);

            Glfw.DestroyWindow(OldWindow);
            return(NewWindow);
        }
예제 #2
0
        static void OnKeyPress(GlfwWindowPtr window, Key KeyCode, int Scancode, KeyAction Action, KeyModifiers Mods)
        {
            int texmax = GL.GetInteger(GetPName.MaxTextureSize);

            if (Action == KeyAction.Press)
            {
                switch (KeyCode)
                {
                case Key.Escape:
                    Glfw.SetWindowShouldClose(window, true);
                    break;

                case Key.Space:
                case Key.Right:
                    currentDemoPage++;
                    break;

                case Key.Backspace:
                case Key.Left:
                    currentDemoPage--;
                    break;

                case Key.Enter:
                {
                    if (currentDemoPage == 4)
                    {
                        boundsAnimationCnt = 0f;
                    }
                    break;
                }

                case Key.Up:
                {
                    if (currentDemoPage == 4)
                    {
                        if (cycleAlignment == QFontAlignment.Justify)
                        {
                            cycleAlignment = QFontAlignment.Left;
                        }
                        else
                        {
                            cycleAlignment++;
                        }
                    }
                    break;
                }

                case Key.Down:
                {
                    if (currentDemoPage == 4)
                    {
                        if (cycleAlignment == QFontAlignment.Left)
                        {
                            cycleAlignment = QFontAlignment.Justify;
                        }
                        else
                        {
                            cycleAlignment--;
                        }
                    }
                    break;
                }
                }
            }

            if (currentDemoPage > lastPage)
            {
                currentDemoPage = lastPage;
            }
            if (currentDemoPage < 0)
            {
                currentDemoPage = 0;
            }
        }
예제 #3
0
 private void OnCbfun(GlfwWindowPtr wnd, MouseButton btn, KeyAction action)
 {
     _gui.MouseButton(btn.ToButton(), action == KeyAction.Press);
 }
예제 #4
0
        public override void Run(Size size)
        {
            _gui = _guiManager.Create(size, ViewModel);
            Glfw.Init();
            // Create GLFW window
            _window = Glfw.CreateWindow(size.Width, size.Height, "OpenCAD", GlfwMonitorPtr.Null, GlfwWindowPtr.Null);
            Glfw.SetWindowSizeCallback(_window, (wnd, newwidth, newheight) =>
            {
                GL.Viewport(0, 0, newwidth, newheight);
                _gui.Resize(new Size(newwidth, newheight));
            });

            Glfw.SetCursorPosCallback(_window, (wnd, x, y) => _gui.MouseMove(new Point((int)x, (int)y)));
            Glfw.SetWindowFocusCallback(_window, (wnd, focus) => _messageAggregator.Add(new FocusChangedMessage(this, focus)));
            Glfw.SetMouseButtonCallback(_window, OnCbfun);
            // Enable the OpenGL context for the current window
            Glfw.MakeContextCurrent(_window);

            GL.Enable(EnableCap.Texture2D);
            GL.Enable(EnableCap.VertexArray);
            GL.Enable(EnableCap.CullFace);
            GL.Enable(EnableCap.DepthTest);
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

            //var back = new BackgroundRenderer(new GradientBackground(Color.YellowGreen, Color.Blue, Color.Plum, Color.Aquamarine));
            var back = new BackgroundRenderer(new SolidBackground(Color.FromArgb(35, 30, 32)));
            var gui  = new GUIRenderer();

            _modelRenderer = new ModelRenderer();
            _gui.Resize(size);

            while (!Glfw.WindowShouldClose(_window))
            {
                // Poll GLFW window events
                Glfw.PollEvents();

                // If you press escape the window will close
                if (Glfw.GetKey(_window, Key.Escape))
                {
                    Glfw.SetWindowShouldClose(_window, true);
                }

                _gui.Update();
                if (_gui.IsDirty)
                {
                    //Console.WriteLine("Dirty");
                    gui.Texture.Update(_gui);
                }
                // Set OpenGL clear colour to red
                GL.ClearColor(1.0f, 0.0f, 0.0f, 1.0f);

                // Clear the screen
                GL.Clear(ClearBufferMask.ColorBufferBit);
                back.Render();

                var model = ViewModel as IHasScenes;
                if (model != null)
                {
                    _modelRenderer.Render(model.CurrentScene);
                }

                //if (CurrentScene != null)
                //{
                //    _modelRenderer.Render(CurrentScene);
                //}

                gui.Render();

                // Swap the front and back buffer, displaying the scene
                Glfw.SwapBuffers(_window);
            }

            // Finally, clean up Glfw, and close the window
            Glfw.Terminate();
        }
예제 #5
0
 public GlfwWinInfo(GlfwWindowPtr glfwWindowPtr)
 {
     _glfwWindowPtr = glfwWindowPtr;
     _glfwHandle    = glfwWindowPtr.inner_ptr;
 }
예제 #6
0
 private void UpdateScrollWheelValue(GlfwWindowPtr window, double offsetX, double offsetY)
 {
     ScrollWheelValue += (int)offsetY;
 }
예제 #7
0
        public static void Start()
        {
            if (!GLFWPlatforms.Init())
            {
                Console.WriteLine("can't init");
            }

            GlfwWindowPtr glWindow = Glfw.CreateWindow(800, 600,
                                                       "PixelFarm on GLfw and OpenGLES2",
                                                       new GlfwMonitorPtr(), //default monitor
                                                       new GlfwWindowPtr()); //default top window

            /* Make the window's context current */
            Glfw.MakeContextCurrent(glWindow);
            Glfw.SwapInterval(1);

            GlfwWindowPtr currentContext = Glfw.GetCurrentContext();
            var           contextHandler = new OpenTK.ContextHandle(currentContext.inner_ptr);

            var  glfwContext = new GLFWContextForOpenTK(contextHandler);
            var  context     = OpenTK.Graphics.GraphicsContext.CreateExternalContext(glfwContext);
            bool isCurrent   = context.IsCurrent;

            PixelFarm.GlfwWinInfo winInfo = new PixelFarm.GlfwWinInfo(glWindow);
            context.MakeCurrent(winInfo);
            //--------------------------------------

            //--------------------------------------
            //var demo = new OpenTkEssTest.T52_HelloTriangle2();
            //var demo = new OpenTkEssTest.T107_SampleDrawImage();
            //var demo = new OpenTkEssTest.T107_SampleDrawImage();

            var demoContext = new Mini.GLDemoContext(800, 600);

            demoContext.LoadDemo(new OpenTkEssTest.T108_LionFill());



            //var demo = new OpenTkEssTest.T107_SampleDrawImage();
            //demo.Width = 800;
            //demo.Height = 600;
            //--------------------------------------------------------------------------------
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            GL.ClearColor(1, 1, 1, 1);
            //--------------------------------------------------------------------------------
            //setup viewport size
            //set up canvas
            int ww_w = 800;
            int ww_h = 600;
            int max  = Math.Max(ww_w, ww_h);

            GL.Viewport(0, 0, max, max);


            while (!Glfw.WindowShouldClose(glWindow))
            {
                demoContext.Render();
                /* Render here */
                /* Swap front and back buffers */
                Glfw.SwapBuffers(glWindow);
                /* Poll for and process events */
                Glfw.PollEvents();
            }
            demoContext.Close();
            Glfw.Terminate();
        }
 /// <summary>
 /// [Event] GLFW filtered keyboard event occurred.
 /// </summary>
 private void OnChar(GlfwWindowPtr wnd, char ch)
 {
     CurrentInputState.ReportConsoleInput(ch);
 }
예제 #9
0
 void OnSizeChanged(GlfwWindowPtr _, int width, int height)
 {
     this.width  = width;
     this.height = height;
 }
예제 #10
0
 /// <summary>
 /// Initializes a new instance of the RwGlFreeTypeService class.
 /// </summary>
 /// <param name="sharedGlContext">The shared OpenGL context pointer.</param>
 public RwGlFreeTypeService(GlfwWindowPtr sharedGlContext)
 {
     FreeTypeLibrary       = new Library();
     RenderedFontReference = new Dictionary <Face, RwGlTypeFaceData>();
     SharedGlContext       = sharedGlContext;
 }
예제 #11
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);
            }
        }
예제 #12
0
        static void Main(string[] args)
        {
            if (!Glfw.Init())
            {
                Console.WriteLine("can't init glfw");
                return;
            }

            GlfwMonitorPtr monitor  = new GlfwMonitorPtr();
            GlfwWindowPtr  winPtr   = new GlfwWindowPtr();
            GlfwWindowPtr  glWindow = Glfw.CreateWindow(800, 600, "Test Glfw", monitor, winPtr);

            /* Make the window's context current */
            Glfw.MakeContextCurrent(glWindow);
            Glfw.SwapInterval(1);
            GL.Hello(); //after make current
            //--------------------------------------
            //create shader program

            var shaderProgram = new MiniShaderProgram();

            ShaderVtxAttrib a_position;
            ShaderVtxAttrib a_color;

            ShaderUniformMatrix4 u_matrix;
            ShaderUniformVar1    u_useSolidColor;
            ShaderUniformVar4    u_solidColor;

            MyMat4 orthoView;

            string vs = @"        
            attribute vec3 a_position;
            attribute vec4 a_color;  

            uniform mat4 u_mvpMatrix;
            uniform vec4 u_solidColor;
            uniform int u_useSolidColor;              

            varying vec4 v_color;
            varying vec4 a_position_output;
            void main()
            {
                 
                a_position_output =  u_mvpMatrix* vec4(a_position[0],a_position[1],0,1);
                gl_Position = a_position_output;
                v_color=  vec4(1,0,0,1); 
            }
            ";
            //fragment source
            //            string fs = @"void main()
            //                {
            //                    gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
            //                }
            //            ";
            string fs = @"
                varying vec4 v_color;  
                varying vec4 a_position_output;
                void main()
                {
                    if(a_position_output[1]>0.5){
                        gl_FragColor = vec4(0,1,1,1);
                    }else{
                        gl_FragColor= vec4(0,1,0,1); 
                    }
                }
            ";

            if (!shaderProgram.Build(vs, fs))
            {
                throw new NotSupportedException();
            }


            a_position = shaderProgram.GetVtxAttrib("a_position");
            a_color    = shaderProgram.GetVtxAttrib("a_color");

            u_matrix        = shaderProgram.GetUniformMat4("u_mvpMatrix");
            u_useSolidColor = shaderProgram.GetUniform1("u_useSolidColor");
            u_solidColor    = shaderProgram.GetUniform4("u_solidColor");

            //--------------------------------------------------------------------------------
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            GL.ClearColor(1, 1, 1, 1);
            //setup viewport size
            int ww_w = 800;
            int ww_h = 600;
            int max  = Math.Max(ww_w, ww_h);

            //square viewport
            GL.Viewport(0, 0, max, max);
            orthoView = MyMat4.ortho(0, max, 0, max, 0, 1);
            //--------------------------------------------------------------------------------

            //load image

            /* Loop until the user closes the window */
            int width, height;

            Glfw.GetFramebufferSize(glWindow, out width, out height);
            float ratio = (float)width / (float)height;

            GL.Viewport(0, 0, 800, 600);
            shaderProgram.UseProgram();


            while (!Glfw.WindowShouldClose(glWindow))
            {
                //set clear color to white
                GL.ClearColor(1f, 1, 1, 1);
                GL.Clear(ClearBufferMask.ColorBufferBit);
                //---------------------------------
                //---------------------------------------------------------
                u_matrix.SetData(orthoView.data);
                //---------------------------------------------------------

                //DrawLines(0, 0, 300, 300);
                float x1 = 50, y1 = 20,
                      x2 = 300, y2 = 20;


                float[] vtxs = new float[] {
                    x1, y1, 1,
                    x2, y2, 1,
                    50, 300, 1
                };
                unsafe
                {
                    u_useSolidColor.SetValue(1);
                    u_solidColor.SetValue(1f, 0f, 0f, 1f);//use solid color
                    //a_position.LoadV2f(vtxs, 3, 0);
                    a_position.LoadV3f(vtxs, 3, 0);
                    GL.DrawArrays(BeginMode.Triangles, 0, 3);
                }



                //---------------------------------------------------------
                //GL.MatrixMode(MatrixMode.Modelview);
                //GL.LoadIdentity();
                //GL.Begin(BeginMode.Triangles);
                //GL.Color3(1f, 0, 0);
                //GL.Vertex3(-0.6f, -0.4f, 0f);
                //GL.Color3(0f, 1f, 0);
                //GL.Vertex3(0.6f, -0.4f, 0f);
                //GL.Color3(0f, 0, 1f);
                //GL.Vertex3(0.0f, 0.6f, 0f);
                //GL.End();

                //---------------------------------
                /* Render here */
                /* Swap front and back buffers */
                Glfw.SwapBuffers(glWindow);
                /* Poll for and process events */
                Glfw.PollEvents();
            }
            Glfw.Terminate();
        }
예제 #13
0
        public static void Start()
        {
            if (!Glfw.Init())
            {
                Console.WriteLine("can't init glfw");
                return;
            }
            //---------------------------------------------------
            //at this, point no opengl func binding
            //---------------------------------------------------
            //1. we specific which verision we want,
            //here => OpenGLES  2.0
            Glfw.WindowHint(WindowHint.GLFW_CLIENT_API, (int)OpenGLAPI.OpenGLESAPI);
            Glfw.WindowHint(WindowHint.GLFW_CONTEXT_CREATION_API, (int)OpenGLContextCreationAPI.GLFW_EGL_CONTEXT_API);
            Glfw.WindowHint(WindowHint.GLFW_CONTEXT_VERSION_MAJOR, 2);
            Glfw.WindowHint(WindowHint.GLFW_CONTEXT_VERSION_MINOR, 0);
            //---------------------------------------------------
            GlfwMonitorPtr monitor  = new GlfwMonitorPtr(); //default monitor
            GlfwWindowPtr  winPtr   = new GlfwWindowPtr();  //default window
            GlfwWindowPtr  glWindow = Glfw.CreateWindow(800, 600, "Test Glfw", monitor, winPtr);

            /* Make the window's context current */
            Glfw.MakeContextCurrent(glWindow);
            Glfw.SwapInterval(1);
            GlfwWindowPtr currentContext = Glfw.GetCurrentContext();
            var           contextHandler = new OpenTK.ContextHandle(currentContext.inner_ptr);
            //faster: create external context
            var glfwContext = new GLFWContextForOpenTK(contextHandler);
            var context     = OpenTK.Graphics.GraphicsContext.CreateExternalContext(glfwContext);
            //---------------------------
            //slow ... use create dummy context
            //var context = OpenTK.Graphics.GraphicsContext.CreateDummyContext(contextHandler);
            //and you need to load gles2 binding points manaually (below)

            bool isCurrent = context.IsCurrent;

            PixelFarm.GlfwWinInfo winInfo = new PixelFarm.GlfwWinInfo(glWindow);
            context.MakeCurrent(winInfo);
            //--------------------------------------
            //bind open gl funcs here..
            //this not need if we use glfwcontext for opentk
            //new OpenTK.Graphics.ES20.GL().LoadEntryPoints();
            //--------------------------------------
            //create shader program
            var shaderProgram = new MiniShaderProgram();
            //--------------------------------------

            ShaderVtxAttrib3f    a_position;
            ShaderVtxAttrib4f    a_color;
            ShaderUniformMatrix4 u_matrix;
            ShaderUniformVar1    u_useSolidColor;
            ShaderUniformVar4    u_solidColor;

            MyMat4 orthoView;

            string vs = @"        
                attribute vec3 a_position;
                attribute vec4 a_color;  

                uniform mat4 u_mvpMatrix;
                uniform vec4 u_solidColor;
                uniform int u_useSolidColor;              

                varying vec4 v_color;
                varying vec4 a_position_output;
                void main()
                {

                    a_position_output =  u_mvpMatrix* vec4(a_position[0],a_position[1],0,1);
                    gl_Position = a_position_output;
                    v_color=  vec4(1,0,0,1); 
                }
                ";
            //fragment source
            //            string fs = @"void main()
            //                {
            //                    gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
            //                }
            //            ";
            string fs = @"
                    precision mediump float;
                    varying vec4 v_color;  
                    varying vec4 a_position_output;
                    void main()
                    {
                        if(a_position_output[1]>0.5){
                            gl_FragColor = vec4(0,1,1,1);
                        }else{
                            gl_FragColor= vec4(0,1,0,1); 
                        }
                    }
                ";

            if (!shaderProgram.Build(vs, fs))
            {
                throw new NotSupportedException();
            }


            a_position = shaderProgram.GetAttrV3f("a_position");
            a_color    = shaderProgram.GetAttrV4f("a_color");

            u_matrix        = shaderProgram.GetUniformMat4("u_mvpMatrix");
            u_useSolidColor = shaderProgram.GetUniform1("u_useSolidColor");
            u_solidColor    = shaderProgram.GetUniform4("u_solidColor");

            //--------------------------------------------------------------------------------
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            GL.ClearColor(1, 1, 1, 1);
            //setup viewport size
            int ww_w = 800;
            int ww_h = 600;
            int max  = Math.Max(ww_w, ww_h);

            //square viewport
            GL.Viewport(0, 0, max, max);
            orthoView = MyMat4.ortho(0, max, 0, max, 0, 1);
            //--------------------------------------------------------------------------------

            //load image

            /* Loop until the user closes the window */
            //int width, height;
            //Glfw.GetFramebufferSize(glWindow, out width, out height);
            //float ratio = (float)width / (float)height;

            GL.Viewport(0, 0, 800, 600);
            shaderProgram.UseProgram();


            while (!Glfw.WindowShouldClose(glWindow))
            {
                //set clear color to white
                GL.ClearColor(1f, 1, 1, 1);
                GL.Clear(ClearBufferMask.ColorBufferBit);
                //---------------------------------
                //---------------------------------------------------------
                u_matrix.SetData(orthoView.data);
                //---------------------------------------------------------

                //DrawLines(0, 0, 300, 300);
                float x1 = 50, y1 = 20,
                      x2 = 300, y2 = 20;


                float[] vtxs = new float[] {
                    x1, y1, 1,
                    x2, y2, 1,
                    50, 300, 1
                };

                u_useSolidColor.SetValue(1);
                u_solidColor.SetValue(1f, 0f, 0f, 1f);//use solid color
                a_position.LoadPureV3f(vtxs);
                GL.DrawArrays(BeginMode.Triangles, 0, 3);

                //---------------------------------------------------------
                //GL.MatrixMode(MatrixMode.Modelview);
                //GL.LoadIdentity();
                //GL.Begin(BeginMode.Triangles);
                //GL.Color3(1f, 0, 0);
                //GL.Vertex3(-0.6f, -0.4f, 0f);
                //GL.Color3(0f, 1f, 0);
                //GL.Vertex3(0.6f, -0.4f, 0f);
                //GL.Color3(0f, 0, 1f);
                //GL.Vertex3(0.0f, 0.6f, 0f);
                //GL.End();

                //---------------------------------
                /* Render here */
                /* Swap front and back buffers */
                Glfw.SwapBuffers(glWindow);
                /* Poll for and process events */
                Glfw.PollEvents();
            }
            Glfw.Terminate();
        }
예제 #14
0
 public static void glfw_window_size_callback(GlfwWindowPtr window, int width, int height)
 {
     g_win_width  = width;
     g_win_height = height;
     Log.GL_Log("GLFW Window Resize. Width: " + width + "     Height: " + height);
 }
예제 #15
0
        public static void Main(string[] args)
        {
            try
            {
                if (Glfw.Init() == false)
                {
                    Console.Error.WriteLine("Failed to initialize GLFW!");
                    Environment.Exit(1);
                }

                try
                {
                    Glfw.SetErrorCallback((code, des) =>
                    {
                        Console.Error.WriteLine("ERROR ({0}): {1}", code, des);
                    });

                    // Create GLFW window
                    GlfwWindowPtr window = Glfw.CreateWindow(width, height, "FontStash.net-Pencil.Gaming.GLFW3", GlfwMonitorPtr.Null, GlfwWindowPtr.Null);
                    if (window.Equals(GlfwWindowPtr.Null))
                    {                     // Does this line actually work???
                        Console.Error.WriteLine("ERROR: Failed to create GLFW window, shutting down");
                        Environment.Exit(1);
                    }

                    Glfw.SetKeyCallback(window, key);
                    // Enable the OpenGL context for the current window
                    Glfw.MakeContextCurrent(window);

                    //Glfw.SetWindowTitle(window, "This is a GLFW window!");

                    Glfw.SwapInterval(1);
                    Glfw.WindowHint(WindowHint.Samples, 2);                     // Turns on 2× mutlisampling

                    fontNormal = FontStash.FONS_INVALID;

                    fs = GlFontStash.glfonsCreate(512, 512, FONSflags.FONS_ZERO_TOPLEFT);

                    fontNormal = FontStash.fonsAddFont(fs, "sans", "DroidSerif-Regular.ttf");
                    if (fontNormal == FontStash.FONS_INVALID)
                    {
                        throw new Exception("Could not add font normal.\n");
                    }

                    fontItalic = FontStash.fonsAddFont(fs, "sans-italic", "DroidSerif-Italic.ttf");
                    if (fontItalic == FontStash.FONS_INVALID)
                    {
                        throw new Exception("Could not add font italic.\n");
                    }
                    fontBold = FontStash.fonsAddFont(fs, "sans-bold", "DroidSerif-Bold.ttf");
                    if (fontBold == FontStash.FONS_INVALID)
                    {
                        throw new Exception("Could not add font bold.\n");
                    }
                    fontJapanese = FontStash.fonsAddFont(fs, "sans-jp", "DroidSansJapanese.ttf");
                    if (fontJapanese == FontStash.FONS_INVALID)
                    {
                        throw new Exception("Could not add font japanese.\n");
                    }


                    Glfw.PollEvents();                     // Get events
                    while (!Glfw.WindowShouldClose(window))
                    {
                        // Poll GLFW window events
                        Glfw.PollEvents();

                        float sx, sy, dx, dy, lh = 0, pf1 = 0, pf2 = 0;
                        uint  white, black, brown, blue;

                        Glfw.GetWindowSize(window, out width, out height);

                        // Update and render
                        GL.Viewport(0, 0, width, height);
                        GL.ClearColor(0.3f, 0.3f, 0.32f, 1.0f);
                        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                        GL.Enable(EnableCap.Blend);
                        GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
                        GL.Disable(EnableCap.Texture2D);
                        GL.MatrixMode(MatrixMode.Projection);
                        GL.LoadIdentity();
                        GL.Ortho(0, width, height, 0, -1, 1);

                        GL.MatrixMode(MatrixMode.Modelview);
                        GL.LoadIdentity();
                        GL.Disable(EnableCap.DepthTest);
                        GL.Color4(255f, 255f, 255f, 255f);

                        GL.Enable(EnableCap.Blend);
                        GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
                        GL.Enable(EnableCap.CullFace);

                        white = GlFontStash.glfonsRGBA(255, 255, 255, 255);
                        brown = GlFontStash.glfonsRGBA(192, 128, 0, 128);
                        blue  = GlFontStash.glfonsRGBA(0, 192, 255, 255);
                        black = GlFontStash.glfonsRGBA(0, 0, 0, 255);

                        sx = 50;
                        sy = 50;

                        dx = sx;
                        dy = sy;

                        dash(dx, dy);

                        FontStash.fonsClearState(ref fs);

                        FontStash.fonsSetSize(ref fs, 124.0f);
                        FontStash.fonsSetFont(ref fs, fontNormal);
                        pf1 = 0;
                        pf2 = 0;
                        FontStash.fonsVertMetrics(ref fs, ref pf1, ref pf2, ref lh);
                        dx  = sx;
                        dy += lh;
                        dash(dx, dy);

                        FontStash.fonsSetSize(ref fs, 124.0f);
                        FontStash.fonsSetFont(ref fs, fontNormal);
                        FontStash.fonsSetColor(ref fs, white);
                        dx = FontStash.fonsDrawText(ref fs, dx, dy, "The quick ");

                        FontStash.fonsSetSize(ref fs, 48.0f);
                        FontStash.fonsSetFont(ref fs, fontItalic);
                        FontStash.fonsSetColor(ref fs, brown);
                        dx = FontStash.fonsDrawText(ref fs, dx, dy, "brown ");

                        FontStash.fonsSetSize(ref fs, 24.0f);
                        FontStash.fonsSetFont(ref fs, fontNormal);
                        FontStash.fonsSetColor(ref fs, white);
                        dx = FontStash.fonsDrawText(ref fs, dx, dy, "fox ");

                        FontStash.fonsVertMetrics(ref fs, ref pf1, ref pf2, ref lh);
                        dx  = sx;
                        dy += lh * 1.2f;
                        dash(dx, dy);
                        FontStash.fonsSetFont(ref fs, fontItalic);
                        dx = FontStash.fonsDrawText(ref fs, dx, dy, "jumps over ");
                        FontStash.fonsSetFont(ref fs, fontBold);
                        dx = FontStash.fonsDrawText(ref fs, dx, dy, "the lazy ");
                        FontStash.fonsSetFont(ref fs, fontNormal);
                        dx = FontStash.fonsDrawText(ref fs, dx, dy, "dog.");

                        dx  = sx;
                        dy += lh * 1.2f;
                        dash(dx, dy);
                        FontStash.fonsSetSize(ref fs, 12.0f);
                        FontStash.fonsSetFont(ref fs, fontNormal);
                        FontStash.fonsSetColor(ref fs, blue);
                        FontStash.fonsDrawText(ref fs, dx, dy, "Now is the time for all good men to come to the aid of the party.");

                        FontStash.fonsVertMetrics(ref fs, ref pf1, ref pf2, ref lh);
                        dx  = sx;
                        dy += lh * 1.2f * 2;
                        dash(dx, dy);
                        FontStash.fonsSetSize(ref fs, 18.0f);
                        FontStash.fonsSetFont(ref fs, fontItalic);
                        FontStash.fonsSetColor(ref fs, white);
                        FontStash.fonsDrawText(ref fs, dx, dy, "Ég get etið gler án þess að meiða mig.");

                        FontStash.fonsVertMetrics(ref fs, ref pf1, ref pf2, ref lh);
                        dx  = sx;
                        dy += lh * 1.2f;
                        dash(dx, dy);
                        FontStash.fonsSetFont(ref fs, fontJapanese);
                        FontStash.fonsDrawText(ref fs, dx, dy, "私はガラスを食べられます。それは私を傷つけません。");

                        // Font alignment
                        FontStash.fonsSetSize(ref fs, 18.0f);
                        FontStash.fonsSetFont(ref fs, fontNormal);
                        FontStash.fonsSetColor(ref fs, white);

                        dx = 50;
                        dy = 350;
                        line(dx - 10, dy, dx + 250, dy);
                        FontStash.fonsSetAlign(fs, FONSalign.FONS_ALIGN_LEFT | FONSalign.FONS_ALIGN_TOP);
                        dx  = FontStash.fonsDrawText(ref fs, dx, dy, "Top");
                        dx += 10;
                        FontStash.fonsSetAlign(fs, FONSalign.FONS_ALIGN_LEFT | FONSalign.FONS_ALIGN_MIDDLE);
                        dx  = FontStash.fonsDrawText(ref fs, dx, dy, "Middle");
                        dx += 10;
                        FontStash.fonsSetAlign(fs, FONSalign.FONS_ALIGN_LEFT | FONSalign.FONS_ALIGN_BASELINE);
                        dx  = FontStash.fonsDrawText(ref fs, dx, dy, "Baseline");
                        dx += 10;
                        FontStash.fonsSetAlign(fs, FONSalign.FONS_ALIGN_LEFT | FONSalign.FONS_ALIGN_BOTTOM);
                        FontStash.fonsDrawText(ref fs, dx, dy, "Bottom");

                        dx = 150;
                        dy = 400;
                        line(dx, dy - 30, dx, dy + 80.0f);
                        FontStash.fonsSetAlign(fs, FONSalign.FONS_ALIGN_LEFT | FONSalign.FONS_ALIGN_BASELINE);
                        FontStash.fonsDrawText(ref fs, dx, dy, "Left");
                        dy += 30;
                        FontStash.fonsSetAlign(fs, FONSalign.FONS_ALIGN_CENTER | FONSalign.FONS_ALIGN_BASELINE);
                        FontStash.fonsDrawText(ref fs, dx, dy, "Center");
                        dy += 30;
                        FontStash.fonsSetAlign(fs, FONSalign.FONS_ALIGN_RIGHT | FONSalign.FONS_ALIGN_BASELINE);
                        FontStash.fonsDrawText(ref fs, dx, dy, "Right");

                        // Blur
                        dx = 500;
                        dy = 350;
                        FontStash.fonsSetAlign(fs, FONSalign.FONS_ALIGN_LEFT | FONSalign.FONS_ALIGN_BASELINE);

                        FontStash.fonsSetSize(ref fs, 60.0f);
                        FontStash.fonsSetFont(ref fs, fontItalic);
                        FontStash.fonsSetColor(ref fs, white);
                        FontStash.fonsSetSpacing(ref fs, 5.0f);
                        FontStash.fonsSetBlur(ref fs, 10.0f);
                        FontStash.fonsDrawText(ref fs, dx, dy, "Blurry...");

                        dy += 50.0f;

                        FontStash.fonsSetSize(ref fs, 18.0f);
                        FontStash.fonsSetFont(ref fs, fontBold);
                        FontStash.fonsSetColor(ref fs, black);
                        FontStash.fonsSetSpacing(ref fs, 0.0f);
                        FontStash.fonsSetBlur(ref fs, 3.0f);
                        FontStash.fonsDrawText(ref fs, dx, dy + 2, "DROP THAT SHADOW");

                        FontStash.fonsSetColor(ref fs, white);
                        FontStash.fonsSetBlur(ref fs, 0);
                        FontStash.fonsDrawText(ref fs, dx, dy, "DROP THAT SHADOW");

                        if (debug)
                        {
                            FontStash.fonsDrawDebug(fs, 800.0f, 50.0f);
                        }


                        GL.Enable(EnableCap.DepthTest);

                        Glfw.SwapBuffers(window);
                    }
                }
                finally
                {
                    GlFontStash.glfonsDelete(fs);
                }
            }
            finally
            {
                Glfw.Terminate();
            }
        }