Exemple #1
0
 public void DestroyWindow()
 {
     if (glfwWindow.HasValue)
     {
         Glfw.DestroyWindow(glfwWindow.Value);
     }
 }
Exemple #2
0
        static void Main(string[] args)
        {
            if (!Glfw.Init())
            {
                return;
            }

            window = Glfw.CreateWindow(width, height, "", GlfwMonitorPtr.Null, GlfwWindowPtr.Null);
            Glfw.MakeContextCurrent(window);
            Glfw.SetInputMode(window, InputMode.CursorMode, CursorMode.CursorCaptured);
            Glfw.SetErrorCallback(OnError);
            Input.Init(window);

            Renderer.Init();
            Camera.Init(new Vector3(0, 0, -5), Vector3.Zero);
            Terrain.Init(Terrain.GenerateHeights());
            Init();

            while (!Glfw.WindowShouldClose(window))
            {
                MainLoop();
            }

            Glfw.DestroyWindow(window);
            Glfw.Terminate();

            Renderer.CleanUp();
        }
Exemple #3
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);
        }
Exemple #4
0
        static void Main(string[] args)
        {
            Glfw.Init();

            Glfw.WindowHint(Hint.ContextVersionMajor, 4);
            Glfw.WindowHint(Hint.ContextVersionMinor, 6);
            Glfw.WindowHint(Hint.OpenglProfile, Profile.Compatibility);
            Window window = Glfw.CreateWindow(1080, 720, "Yeet", Monitor.None, Window.None);

            // `Gl.Initialize()` has to be don before `Glfw.MakeContextCurrent(window)`
            // [How Do I Initialize OpenGL.NET with GLFW.Net?](https://stackoverflow.com/questions/61318104/how-do-i-initialize-opengl-net-with-glfw-net/61319044?noredirect=1#comment108476826_61319044)
            Gl.Initialize();
            Glfw.MakeContextCurrent(window);

            var v = Gl.GetString(StringName.Version);

            Console.WriteLine(v);

            uint vao = Gl.CreateVertexArray();

            Gl.BindVertexArray(vao);

            uint vbo = Gl.GenBuffer();

            Gl.BindBuffer(BufferTarget.ArrayBuffer, vbo);

            var vertices = new float[] { -0.5f, -0.5f, 0.5f, -0.5f, 0.0f, 0.5f };

            Gl.BufferData(BufferTarget.ArrayBuffer, (uint)(4 * vertices.Length), null, BufferUsage.StaticDraw);

            IntPtr unmanagedPointer = Marshal.AllocHGlobal(4 * vertices.Length);

            Marshal.Copy(vertices, 0, unmanagedPointer, vertices.Length);
            Gl.BufferSubData(BufferTarget.ArrayBuffer, new IntPtr(0), (uint)(4 * vertices.Length), unmanagedPointer);
            Marshal.FreeHGlobal(unmanagedPointer);

            //Gl.BufferSubData(BufferTarget.ArrayBuffer, new IntPtr(0), (uint)(4 * vertices.Length), vertices);

            Gl.VertexAttribPointer(0, 2, VertexAttribType.Float, false, 0, null);

            while (!Glfw.WindowShouldClose(window))
            {
                Glfw.PollEvents();

                Gl.ClearColor(0.0f, 1.0f, 1.0f, 1.0f);
                Gl.Clear(ClearBufferMask.ColorBufferBit);
                Gl.BindVertexArray(vao);
                Gl.EnableVertexAttribArray(0);
                Gl.DrawArrays(PrimitiveType.Triangles, 0, 3);
                Gl.DisableVertexAttribArray(0);
                Gl.BindVertexArray(0);

                Glfw.SwapBuffers(window);
            }

            Glfw.DestroyWindow(window);
            Glfw.Terminate();
        }
Exemple #5
0
 public void SetWindowed()
 {
     Glfw.DestroyWindow(nativeWindow);
     CreateWindow(windowedSize, false);
     if (FullscreenChanged != null)
     {
         FullscreenChanged(windowedSize, false);
     }
 }
Exemple #6
0
 public virtual void SetFullscreen(Size setFullscreenViewportSize)
 {
     windowedSize = viewportSize;
     Glfw.DestroyWindow(nativeWindow);
     CreateWindow(setFullscreenViewportSize, true);
     if (FullscreenChanged != null)
     {
         FullscreenChanged(setFullscreenViewportSize, true);
     }
 }
Exemple #7
0
        public void Destroy()
        {
            // - Warn extern resource before destroying the window
            Invalidating?.Invoke(this, EventArgs.Empty);

            // - Destroy the window
            Glfw.DestroyWindow(GLFWHAndle);

            // - Set the pointer to null
            GLFWHAndle = Window.None;
        }
Exemple #8
0
        static void Main()
        {
            Init();

            Glfw.Window window;
            var         starCursors  = new Glfw.Cursor[CURSOR_FRAME_COUNT];
            var         currentFrame = Glfw.Cursor.None;

            Gl.Initialize();

            if (!Glfw.Init())
            {
                Environment.Exit(1);
            }

            for (int i = 0; i < CURSOR_FRAME_COUNT; i++)
            {
                starCursors[i] = CreateCursorFrame(i / (float)CURSOR_FRAME_COUNT);
                if (!starCursors[i])
                {
                    Glfw.Terminate();
                    Environment.Exit(1);
                }
            }

            Glfw.CursorType[] shapes =
            {
                Glfw.CursorType.Arrow,
                Glfw.CursorType.Beam,
                Glfw.CursorType.Crosshair,
                Glfw.CursorType.Hand,
                Glfw.CursorType.ResizeX,
                Glfw.CursorType.ResizeY
            };

            standardCursors = new Glfw.Cursor[6];

            for (int i = 0; i < standardCursors.Length; i++)
            {
                standardCursors[i] = Glfw.CreateStandardCursor(shapes[i]);

                if (!standardCursors[i])
                {
                    Glfw.Terminate();
                    Environment.Exit(1);
                }
            }

            window = Glfw.CreateWindow(640, 480, "Cursor Test");

            if (!window)
            {
                Glfw.Terminate();
                Environment.Exit(1);
            }

            Glfw.MakeContextCurrent(window);

            Glfw.GetCursorPos(window, out cursorX, out cursorY);
            Log("Cursor position: {0} {1}", cursorX, cursorY);

            Glfw.SetCursorPosCallback(window, CursorPositionCallback);
            Glfw.SetKeyCallback(window, KeyCallback);

            while (!Glfw.WindowShouldClose(window))
            {
                Gl.Clear(ClearBufferMask.ColorBufferBit);

                if (trackCursor)
                {
                    int   wnd_width, wnd_height, fb_width, fb_height;
                    float scale;

                    Glfw.GetWindowSize(window, out wnd_width, out wnd_height);
                    Glfw.GetFramebufferSize(window, out fb_width, out fb_height);

                    scale = (float)fb_width / (float)wnd_width;

                    Gl.Viewport(0, 0, fb_width, fb_height);

                    Gl.MatrixMode(MatrixMode.Projection);
                    Gl.LoadIdentity();
                    Gl.Ortho(0f, fb_width, 0f, fb_height, 0f, 1f);

                    Gl.Begin(PrimitiveType.Lines);
                    Gl.Vertex2(0f, (float)(fb_height - cursorY * scale));
                    Gl.Vertex2((float)fb_width, (float)(fb_height - cursorY * scale));
                    Gl.Vertex2((float)cursorX * scale, 0f);
                    Gl.Vertex2((float)cursorX * scale, (float)fb_height);
                    Gl.End();
                }

                Glfw.SwapBuffers(window);

                if (animateCursor)
                {
                    var i = (int)(Glfw.GetTime() * 30.0) % CURSOR_FRAME_COUNT;

                    if (currentFrame != starCursors[i])
                    {
                        Glfw.SetCursor(window, starCursors[i]);
                        currentFrame = starCursors[i];
                    }
                }
                else
                {
                    currentFrame = Glfw.Cursor.None;
                }

                if (waitEvents)
                {
                    if (animateCursor)
                    {
                        Glfw.WaitEventsTimeout(1.0 / 30.0);
                    }
                    else
                    {
                        Glfw.WaitEvents();
                    }
                }
                else
                {
                    Glfw.PollEvents();
                }
            }

            Glfw.DestroyWindow(window);

            for (int i = 0; i < CURSOR_FRAME_COUNT; i++)
            {
                Glfw.DestroyCursor(starCursors[i]);
            }

            for (int i = 0; i < standardCursors.Length; i++)
            {
                Glfw.DestroyCursor(standardCursors[i]);
            }

            Glfw.Terminate();
        }
Exemple #9
0
 public void ShutDown()
 {
     Glfw.DestroyWindow(m_Window);
 }
        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);
            }
        }