public PencilGamingWindows(int width, int height, string title, bool isFullscreen, bool shouldVsync)
        {
            onWindowOpened = new Trigger<PencilGamingWindows>();
            OnWindowOpened = onWindowOpened;

            onWindowClosed = new Trigger<PencilGamingWindows>();
            OnWindowClosed = onWindowClosed;

            onWindowResized = new Trigger<PencilGamingWindows>();
            OnWindowResized = onWindowResized;

            onKeyboard = new Trigger<PencilKeyInfo>();
            OnKeyboard = onKeyboard;

            onExit = new Trigger<PencilGamingWindows>();
            OnExit = onExit;

            window = GlfwWindowPtr.Null;
            this.width = width;
            this.height = height;
            this.title = title;
            this.isFullscreen = isFullscreen;
            this.shouldVsync = shouldVsync;
            this.firstTime = true;
            if (!Glfw.Init())
            {
                Console.Error.WriteLine("ERROR: Could not initialize GLFW, shutting down.");
                Environment.Exit(1);
            }
        }
Beispiel #2
0
		public static void create(int w, int h) {
			Width = w;
			Height = h;
			AspectRatio = Width / (float) Height;

			Glfw.WindowHint(WindowHint.Samples, 4);
			Glfw.WindowHint(WindowHint.ContextVersionMajor, 3);
			Glfw.WindowHint(WindowHint.ContextVersionMinor, 3);
			Glfw.WindowHint(WindowHint.OpenGLForwardCompat, GL_TRUE);
			Glfw.WindowHint(WindowHint.OpenGLProfile, OpenGLProfile.Core.GetHashCode());
			GlfwVidMode[] modes = Glfw.GetVideoModes(Glfw.GetPrimaryMonitor());

			Console.WriteLine("Displays");
			foreach (GlfwVidMode mode in modes) {
				Console.WriteLine(mode.Width + "x" + mode.Height + "\tr" + mode.RedBits + " g" + mode.GreenBits + " b" + mode.BlueBits + "\t " + mode.RefreshRate + "Hz");
			}
			GlfwVidMode defMode = Glfw.GetVideoMode(Glfw.GetPrimaryMonitor());
			Console.WriteLine("Default Display\n" + defMode.Width + "x" + defMode.Height + "\tr" + defMode.RedBits + " g" + defMode.GreenBits + " b" + defMode.BlueBits + "\t " + defMode.RefreshRate + "Hz");

			//Width = defMode.Width;
			//Height = defMode.Height;
			//window = Glfw.CreateWindow(Width, Height, title, Glfw.GetPrimaryMonitor(), GlfwWindowPtr.Null);

			window = Glfw.CreateWindow(Width, Height, title, GlfwMonitorPtr.Null, GlfwWindowPtr.Null);
			if (window.Equals(GlfwWindowPtr.Null)) {
				Glfw.Terminate();
				Console.WriteLine("Failed to create Window");
				Console.WriteLine("Press Any Key to continue ...");
				Console.ReadKey();
				Environment.Exit(1);
			}

			Glfw.MakeContextCurrent(window);

		}
		public static KeyboardState GetState(GlfwWindowPtr window) {
			KeyboardState result = new KeyboardState();

			for (int i = 0; i < allKeys.Length; ++i) {
				Key k = allKeys[i];
				result.keys[k] = Glfw.GetKey(window, k);
			}

			return result;
		}
Beispiel #4
0
        public void Initialize()
        {
            try
            {
                // Throws and Exception if GLFW cannot be initialized
                if (!Glfw.Init())
                    throw new GlfwInitException();

                // Creates the Window
                window = Glfw.CreateWindow(Width, Height, Caption, GlfwMonitorPtr.Null, GlfwWindowPtr.Null);

                // Window Hints
                Glfw.WindowHint(WindowHint.ContextVersionMajor, 3);
                Glfw.WindowHint(WindowHint.ContextVersionMinor, 0);
                Glfw.WindowHint(WindowHint.OpenGLProfile, (int)OpenGLProfile.Core);
                Glfw.WindowHint((WindowHint)WindowAttrib.Resizeable, Convert.ToInt32(false));
                Glfw.WindowHint(WindowHint.OpenGLForwardCompat, Convert.ToInt32(true)); // This is for the client to work in MacOS

                // Without this the window will not pop up
                Glfw.MakeContextCurrent(window);

                // This is shown as a false in C++ but false = 0 so that's why it's a 0
                Glfw.SwapInterval(0);

                // Set Key callback for the window
                Glfw.SetKeyCallback(window, OnKeyCallback);

                // Anything else you want to initialize should go here
                // i.e ShaderProgram if you want to use Modern openGL

                // END

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

                    // Add Anything you need to Render HERE

                    OnWindowRender();

                    // END

                    Glfw.SwapBuffers(window);
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                Glfw.Terminate();
            }
        }
		public static MouseState GetMouseState(GlfwWindowPtr window) {
			MouseState result = new MouseState();

			result.LeftButton = Glfw.GetMouseButton(window, MouseButton.LeftButton);
			result.MiddleButton = Glfw.GetMouseButton(window, MouseButton.MiddleButton);
			result.RightButton = Glfw.GetMouseButton(window, MouseButton.RightButton);
			int x, y;
			Glfw.GetCursorPos(window, out x, out y);
			result.X = x;
			result.Y = y;

			return result;
		}
        // the pragram starts here
        private static void AppMain()
        {
            // initialize GLFW
            if (!Glfw.Init())
                throw new Exception("glfwInit failed");

            // open a window with GLFW
            Glfw.WindowHint(WindowHint.OpenGLProfile, (int)OpenGLProfile.Core);
            Glfw.WindowHint(WindowHint.ContextVersionMajor, 3);
            Glfw.WindowHint(WindowHint.ContextVersionMinor, 2);
            _window = Glfw.CreateWindow(ScreenSize.X, ScreenSize.Y, "", GlfwMonitorPtr.Null, GlfwWindowPtr.Null);
            if (_window.Equals(GlfwWindowPtr.Null))
                throw new Exception("glfwOpenWindow failed. Can your hardware handle OpenGL 3.2?");
            Glfw.MakeContextCurrent(_window);

            // TODO: GLEW in C#

            // print out some info about the graphics drivers
            Console.WriteLine("OpenGL version: {0}", GL.GetString(StringName.Version));
            Console.WriteLine("GLSL version: {0}", GL.GetString(StringName.ShadingLanguageVersion));
            Console.WriteLine("Vendor: {0}", GL.GetString(StringName.Vendor));
            Console.WriteLine("Renderer: {0}", GL.GetString(StringName.Renderer));

            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Less);

            // load vertex and fragment shaders into opengl
            LoadShaders();

            // load the texture
            LoadTexture();

            // create buffer and fill it with the points of the triangle
            LoadCube();

            double lastTime = Glfw.GetTime();
            // run while window is open
            while (!Glfw.WindowShouldClose(_window)) {
                // update the scene based on the time elapsed since last update
                double thisTime = Glfw.GetTime();
                Update(thisTime - lastTime);
                lastTime = thisTime;

                // draw one frame
                Render();
            }

            // clean up and exit
            Glfw.Terminate();
        }
Beispiel #7
0
 internal static extern void glfwGetFramebufferSize(GlfwWindowPtr window, out int width, out int height);
Beispiel #8
0
 public static void GetWindowSize(GlfwWindowPtr window, out int width, out int height)
 {
     GlfwDelegates.glfwGetWindowSize(window, out width, out height);
 }
Beispiel #9
0
 internal static extern void glfwMakeContextCurrent(GlfwWindowPtr window);
Beispiel #10
0
 internal static extern void glfwHideWindow(GlfwWindowPtr window);
        // the pragram starts here
        private static void AppMain()
        {
            // initialize GLFW
            if (!Glfw.Init())
                throw new Exception("glfwInit failed");

            // open a window with GLFW
            Glfw.WindowHint(WindowHint.OpenGLProfile, (int)OpenGLProfile.Core);
            Glfw.WindowHint(WindowHint.ContextVersionMajor, 3);
            Glfw.WindowHint(WindowHint.ContextVersionMinor, 2);
            _window = Glfw.CreateWindow(ScreenSize.X, ScreenSize.Y, "", GlfwMonitorPtr.Null, GlfwWindowPtr.Null);
            if (_window.Equals(GlfwWindowPtr.Null))
                throw new Exception("glfwOpenWindow failed. Can your hardware handle OpenGL 3.2?");
            Glfw.MakeContextCurrent(_window);

            // GLFW settings
            Glfw.SetInputMode(_window, InputMode.CursorMode, CursorMode.CursorHidden | CursorMode.CursorCaptured);
            Glfw.SetCursorPos(_window, 0, 0);
            Glfw.SetScrollCallback(_window, new GlfwScrollFun((win, x, y) => {
                //increase or decrease field of view based on mouse wheel
                float zoomSensitivity = -0.2f;
                float fieldOfView = _gCamera.FieldOfView + zoomSensitivity * (float)y;
                if (fieldOfView < 5.0f) fieldOfView = 5.0f;
                if (fieldOfView > 130.0f) fieldOfView = 130.0f;
                _gCamera.FieldOfView = fieldOfView;
            }));

            // TODO: GLEW in C#

            // print out some info about the graphics drivers
            Console.WriteLine("OpenGL version: {0}", GL.GetString(StringName.Version));
            Console.WriteLine("GLSL version: {0}", GL.GetString(StringName.ShadingLanguageVersion));
            Console.WriteLine("Vendor: {0}", GL.GetString(StringName.Vendor));
            Console.WriteLine("Renderer: {0}", GL.GetString(StringName.Renderer));

            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Less);

            // initialise the gWoodenCrate asset
            LoadWoodenCrateAsset();

            // create all the instances in the 3D scene based on the gWoodenCrate asset
            CreateInstances();

            _gCamera.Position = new Vector3(-4, 0, 17);
            _gCamera.ViewportAspectRatio = (float)ScreenSize.X / (float)ScreenSize.Y;
            _gCamera.SetNearAndFarPlanes(0.5f, 100.0f);

            // setup light
            _gLight = new Light() {
                Position = _gCamera.Position,
                Intensities = new Vector3(1, 1, 1), // white
                Attenuation = 0.2f,
                AmbientCoefficient = 0.005f
            };

            float lastTime = (float)Glfw.GetTime();
            // run while window is open
            while (!Glfw.WindowShouldClose(_window)) {
                // update the scene based on the time elapsed since last update
                float thisTime = (float)Glfw.GetTime();
                Update(thisTime - lastTime);
                lastTime = thisTime;

                // draw one frame
                Render();
                //exit program if escape key is pressed
                if (Glfw.GetKey(_window, Key.Escape))
                    Glfw.SetWindowShouldClose(_window, true);
            }

            // clean up and exit
            Glfw.Terminate();
        }
Beispiel #12
0
 internal static extern void glfwSetWindowShouldClose(GlfwWindowPtr window, int value);
Beispiel #13
0
 internal static extern void glfwSetWindowSize(GlfwWindowPtr window, int width, int height);
Beispiel #14
0
 public static GlfwWindowRefreshFun SetWindowRefreshCallback(GlfwWindowPtr window, GlfwWindowRefreshFun cbfun)
 {
     windowRefreshFun = cbfun;
     return(GlfwDelegates.glfwSetWindowRefreshCallback(window, cbfun));
 }
Beispiel #15
0
 public static GlfwWindowIconifyFun SetWindowIconifyCallback(GlfwWindowPtr window, GlfwWindowIconifyFun cbfun)
 {
     windowIconifyFun = cbfun;
     return(GlfwDelegates.glfwSetWindowIconifyCallback(window, cbfun));
 }
Beispiel #16
0
 public static IntPtr GetWindowUserPointer(GlfwWindowPtr window)
 {
     return(GlfwDelegates.glfwGetWindowUserPointer(window));
 }
Beispiel #17
0
 public static GlfwFramebufferSizeFun SetFramebufferSizeCallback(GlfwWindowPtr window, GlfwFramebufferSizeFun cbfun)
 {
     framebufferSizeFun = cbfun;
     return(GlfwDelegates.glfwSetFramebufferSizeCallback(window, cbfun));
 }
Beispiel #18
0
 public static void SetWindowUserPointer(GlfwWindowPtr window, IntPtr pointer)
 {
     GlfwDelegates.glfwSetWindowUserPointer(window, pointer);
 }
Beispiel #19
0
 public static int GetWindowAttrib(GlfwWindowPtr window, WindowHint param)
 {
     return(GlfwDelegates.glfwGetWindowAttrib(window, (int)param));
 }
Beispiel #20
0
 public static GlfwMonitorPtr GetWindowMonitor(GlfwWindowPtr window)
 {
     return(GlfwDelegates.glfwGetWindowMonitor(window));
 }
Beispiel #21
0
 internal static extern IntPtr glfwGetWin32Window(GlfwWindowPtr window);
Beispiel #22
0
 public static int GetInputMode(GlfwWindowPtr window, InputMode mode)
 {
     return(GlfwDelegates.glfwGetInputMode(window, mode));
 }
Beispiel #23
0
 internal static extern void glfwDestroyWindow(GlfwWindowPtr window);
Beispiel #24
0
 public static void SetInputMode(GlfwWindowPtr window, InputMode mode, CursorMode value)
 {
     GlfwDelegates.glfwSetInputMode(window, mode, value);
 }
Beispiel #25
0
 internal static extern void glfwGetWindowPos(GlfwWindowPtr window, out int xpos, out int ypos);
Beispiel #26
0
 public static bool GetKey(GlfwWindowPtr window, Key key)
 {
     return(GlfwDelegates.glfwGetKey(window, key) != 0);
 }
Beispiel #27
0
 internal static extern void glfwRestoreWindow(GlfwWindowPtr window);
Beispiel #28
0
 public static bool GetMouseButton(GlfwWindowPtr window, MouseButton button)
 {
     return(GlfwDelegates.glfwGetMouseButton(window, button) != 0);
 }
Beispiel #29
0
 internal static extern int glfwGetWindowAttrib(GlfwWindowPtr window, int param);
Beispiel #30
0
 public static void GetCursorPos(GlfwWindowPtr window, out double xpos, out double ypos)
 {
     GlfwDelegates.glfwGetCursorPos(window, out xpos, out ypos);
 }
Beispiel #31
0
 public static void HideWindow(GlfwWindowPtr window)
 {
     GlfwDelegates.glfwHideWindow(window);
 }
Beispiel #32
0
 public static void SetCursorPos(GlfwWindowPtr window, double xpos, double ypos)
 {
     GlfwDelegates.glfwSetCursorPos(window, xpos, ypos);
 }
Beispiel #33
0
 internal static extern sbyte* glfwGetClipboardString(GlfwWindowPtr window);
Beispiel #34
0
 public static GlfwKeyFun SetKeyCallback(GlfwWindowPtr window, GlfwKeyFun cbfun)
 {
     keyFun = cbfun;
     return(GlfwDelegates.glfwSetKeyCallback(window, cbfun));
 }
Beispiel #35
0
 internal static extern void glfwSwapBuffers(GlfwWindowPtr window);
Beispiel #36
0
 public static GlfwCharFun SetCharCallback(GlfwWindowPtr window, GlfwCharFun cbfun)
 {
     charFun = cbfun;
     return(GlfwDelegates.glfwSetCharCallback(window, cbfun));
 }
Beispiel #37
0
 internal static extern GlfwFramebufferSizeFun glfwSetFramebufferSizeCallback(GlfwWindowPtr window, GlfwFramebufferSizeFun cbfun);
Beispiel #38
0
 public static GlfwMouseButtonFun SetMouseButtonCallback(GlfwWindowPtr window, GlfwMouseButtonFun cbfun)
 {
     mouseButtonFun = cbfun;
     return(GlfwDelegates.glfwSetMouseButtonCallback(window, cbfun));
 }
Beispiel #39
0
 internal static extern GlfwWindowPtr glfwCreateWindow(int width, int height, [MarshalAs(UnmanagedType.LPStr)] string title, GlfwMonitorPtr monitor, GlfwWindowPtr share);
Beispiel #40
0
 public static GlfwCursorEnterFun SetCursorEnterCallback(GlfwWindowPtr window, GlfwCursorEnterFun cbfun)
 {
     cursorEnterFun = cbfun;
     return(GlfwDelegates.glfwSetCursorEnterCallback(window, cbfun));
 }
Beispiel #41
0
 internal static extern int glfwWindowShouldClose(GlfwWindowPtr window);
Beispiel #42
0
 public static GlfwScrollFun SetScrollCallback(GlfwWindowPtr window, GlfwScrollFun cbfun)
 {
     scrollFun = cbfun;
     return(GlfwDelegates.glfwSetScrollCallback(window, cbfun));
 }
Beispiel #43
0
 internal static extern void glfwSetWindowTitle(GlfwWindowPtr window, [MarshalAs(UnmanagedType.LPStr)] string title);
Beispiel #44
0
 public static void SetClipboardString(GlfwWindowPtr window, string @string)
 {
     GlfwDelegates.glfwSetClipboardString(window, @string);
 }
Beispiel #45
0
 internal static extern void glfwSetWindowPos(GlfwWindowPtr window, int xpos, int ypos);
Beispiel #46
0
 public static string GetClipboardString(GlfwWindowPtr window)
 {
     return(new string(GlfwDelegates.glfwGetClipboardString(window)));
 }
Beispiel #47
0
 internal static extern void glfwIconifyWindow(GlfwWindowPtr window);
Beispiel #48
0
 public static void MakeContextCurrent(GlfwWindowPtr window)
 {
     GlfwDelegates.glfwMakeContextCurrent(window);
 }
Beispiel #49
0
 internal static extern void glfwShowWindow(GlfwWindowPtr window);
Beispiel #50
0
 public static void SetWindowSize(GlfwWindowPtr window, int width, int height)
 {
     GlfwDelegates.glfwSetWindowSize(window, width, height);
 }
Beispiel #51
0
 internal static extern GlfwMonitorPtr glfwGetWindowMonitor(GlfwWindowPtr window);
Beispiel #52
0
 private void OnKeyCallback(GlfwWindowPtr wnd, Key key, int scanCode, KeyAction action, KeyModifiers mods)
 {
     if (key == Key.LeftAlt | key == Key.RightAlt && key == Key.F4 & action == KeyAction.Press)
         Glfw.Terminate();
 }
Beispiel #53
0
 internal static extern void glfwSetWindowUserPointer(GlfwWindowPtr window, IntPtr pointer);
Beispiel #54
0
 public static void RestoreWindow(GlfwWindowPtr window)
 {
     GlfwDelegates.glfwRestoreWindow(window);
 }
Beispiel #55
0
 public static void SetWindowPos(GlfwWindowPtr window, int xpos, int ypos)
 {
     GlfwDelegates.glfwSetWindowPos(window, xpos, ypos);
 }
Beispiel #56
0
 public static void ShowWindow(GlfwWindowPtr window)
 {
     GlfwDelegates.glfwShowWindow(window);
 }
Beispiel #57
0
 public static void SwapBuffers(GlfwWindowPtr window)
 {
     GlfwDelegates.glfwSwapBuffers(window);
 }
Beispiel #58
0
 public static void IconifyWindow(GlfwWindowPtr window)
 {
     GlfwDelegates.glfwIconifyWindow(window);
 }
        public void CreateWindow()
        {
            var monitor = isFullscreen ? Glfw.GetPrimaryMonitor() : GlfwMonitorPtr.Null;
            var currentWindow = Glfw.CreateWindow(Width, Height, Title, monitor, window);

            Glfw.SetKeyCallback(currentWindow, new GlfwKeyFun((w, key, scancode, action, modifiers) =>
            {
                var info = new PencilKeyInfo(key, scancode, action, modifiers);
                onKeyboard.Fire(info);
            }));

            Glfw.SetWindowCloseCallback(currentWindow, new GlfwWindowCloseFun(w =>
            {
                Exit();
            }));

            Glfw.SetWindowSizeCallback(currentWindow, new GlfwWindowSizeFun((w, width, height) =>
            {
                this.width = width;
                this.height = height;
                onWindowResized.Fire(this);
            }));

            Glfw.SwapInterval(shouldVsync ? 1 : 0);
            Glfw.MakeContextCurrent(currentWindow);
            if(!firstTime) Glfw.SetWindowShouldClose(window, true);

            window = currentWindow;
            rebuiltWindow = true;
            onWindowOpened.Fire(this);

            firstTime = false;
        }
Beispiel #60
0
 public static void GetWindowPos(GlfwWindowPtr window, out int xpos, out int ypos)
 {
     GlfwDelegates.glfwGetWindowPos(window, out xpos, out ypos);
 }