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); } }
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; }
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(); }
internal static extern void glfwGetFramebufferSize(GlfwWindowPtr window, out int width, out int height);
public static void GetWindowSize(GlfwWindowPtr window, out int width, out int height) { GlfwDelegates.glfwGetWindowSize(window, out width, out height); }
internal static extern void glfwMakeContextCurrent(GlfwWindowPtr window);
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(); }
internal static extern void glfwSetWindowShouldClose(GlfwWindowPtr window, int value);
internal static extern void glfwSetWindowSize(GlfwWindowPtr window, int width, int height);
public static GlfwWindowRefreshFun SetWindowRefreshCallback(GlfwWindowPtr window, GlfwWindowRefreshFun cbfun) { windowRefreshFun = cbfun; return(GlfwDelegates.glfwSetWindowRefreshCallback(window, cbfun)); }
public static GlfwWindowIconifyFun SetWindowIconifyCallback(GlfwWindowPtr window, GlfwWindowIconifyFun cbfun) { windowIconifyFun = cbfun; return(GlfwDelegates.glfwSetWindowIconifyCallback(window, cbfun)); }
public static IntPtr GetWindowUserPointer(GlfwWindowPtr window) { return(GlfwDelegates.glfwGetWindowUserPointer(window)); }
public static GlfwFramebufferSizeFun SetFramebufferSizeCallback(GlfwWindowPtr window, GlfwFramebufferSizeFun cbfun) { framebufferSizeFun = cbfun; return(GlfwDelegates.glfwSetFramebufferSizeCallback(window, cbfun)); }
public static void SetWindowUserPointer(GlfwWindowPtr window, IntPtr pointer) { GlfwDelegates.glfwSetWindowUserPointer(window, pointer); }
public static int GetWindowAttrib(GlfwWindowPtr window, WindowHint param) { return(GlfwDelegates.glfwGetWindowAttrib(window, (int)param)); }
public static GlfwMonitorPtr GetWindowMonitor(GlfwWindowPtr window) { return(GlfwDelegates.glfwGetWindowMonitor(window)); }
internal static extern IntPtr glfwGetWin32Window(GlfwWindowPtr window);
public static int GetInputMode(GlfwWindowPtr window, InputMode mode) { return(GlfwDelegates.glfwGetInputMode(window, mode)); }
internal static extern void glfwDestroyWindow(GlfwWindowPtr window);
public static void SetInputMode(GlfwWindowPtr window, InputMode mode, CursorMode value) { GlfwDelegates.glfwSetInputMode(window, mode, value); }
internal static extern void glfwGetWindowPos(GlfwWindowPtr window, out int xpos, out int ypos);
public static bool GetKey(GlfwWindowPtr window, Key key) { return(GlfwDelegates.glfwGetKey(window, key) != 0); }
internal static extern void glfwRestoreWindow(GlfwWindowPtr window);
public static bool GetMouseButton(GlfwWindowPtr window, MouseButton button) { return(GlfwDelegates.glfwGetMouseButton(window, button) != 0); }
internal static extern int glfwGetWindowAttrib(GlfwWindowPtr window, int param);
public static void GetCursorPos(GlfwWindowPtr window, out double xpos, out double ypos) { GlfwDelegates.glfwGetCursorPos(window, out xpos, out ypos); }
public static void HideWindow(GlfwWindowPtr window) { GlfwDelegates.glfwHideWindow(window); }
public static void SetCursorPos(GlfwWindowPtr window, double xpos, double ypos) { GlfwDelegates.glfwSetCursorPos(window, xpos, ypos); }
internal static extern sbyte* glfwGetClipboardString(GlfwWindowPtr window);
public static GlfwKeyFun SetKeyCallback(GlfwWindowPtr window, GlfwKeyFun cbfun) { keyFun = cbfun; return(GlfwDelegates.glfwSetKeyCallback(window, cbfun)); }
internal static extern void glfwSwapBuffers(GlfwWindowPtr window);
public static GlfwCharFun SetCharCallback(GlfwWindowPtr window, GlfwCharFun cbfun) { charFun = cbfun; return(GlfwDelegates.glfwSetCharCallback(window, cbfun)); }
internal static extern GlfwFramebufferSizeFun glfwSetFramebufferSizeCallback(GlfwWindowPtr window, GlfwFramebufferSizeFun cbfun);
public static GlfwMouseButtonFun SetMouseButtonCallback(GlfwWindowPtr window, GlfwMouseButtonFun cbfun) { mouseButtonFun = cbfun; return(GlfwDelegates.glfwSetMouseButtonCallback(window, cbfun)); }
internal static extern GlfwWindowPtr glfwCreateWindow(int width, int height, [MarshalAs(UnmanagedType.LPStr)] string title, GlfwMonitorPtr monitor, GlfwWindowPtr share);
public static GlfwCursorEnterFun SetCursorEnterCallback(GlfwWindowPtr window, GlfwCursorEnterFun cbfun) { cursorEnterFun = cbfun; return(GlfwDelegates.glfwSetCursorEnterCallback(window, cbfun)); }
internal static extern int glfwWindowShouldClose(GlfwWindowPtr window);
public static GlfwScrollFun SetScrollCallback(GlfwWindowPtr window, GlfwScrollFun cbfun) { scrollFun = cbfun; return(GlfwDelegates.glfwSetScrollCallback(window, cbfun)); }
internal static extern void glfwSetWindowTitle(GlfwWindowPtr window, [MarshalAs(UnmanagedType.LPStr)] string title);
public static void SetClipboardString(GlfwWindowPtr window, string @string) { GlfwDelegates.glfwSetClipboardString(window, @string); }
internal static extern void glfwSetWindowPos(GlfwWindowPtr window, int xpos, int ypos);
public static string GetClipboardString(GlfwWindowPtr window) { return(new string(GlfwDelegates.glfwGetClipboardString(window))); }
internal static extern void glfwIconifyWindow(GlfwWindowPtr window);
public static void MakeContextCurrent(GlfwWindowPtr window) { GlfwDelegates.glfwMakeContextCurrent(window); }
internal static extern void glfwShowWindow(GlfwWindowPtr window);
public static void SetWindowSize(GlfwWindowPtr window, int width, int height) { GlfwDelegates.glfwSetWindowSize(window, width, height); }
internal static extern GlfwMonitorPtr glfwGetWindowMonitor(GlfwWindowPtr window);
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(); }
internal static extern void glfwSetWindowUserPointer(GlfwWindowPtr window, IntPtr pointer);
public static void RestoreWindow(GlfwWindowPtr window) { GlfwDelegates.glfwRestoreWindow(window); }
public static void SetWindowPos(GlfwWindowPtr window, int xpos, int ypos) { GlfwDelegates.glfwSetWindowPos(window, xpos, ypos); }
public static void ShowWindow(GlfwWindowPtr window) { GlfwDelegates.glfwShowWindow(window); }
public static void SwapBuffers(GlfwWindowPtr window) { GlfwDelegates.glfwSwapBuffers(window); }
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; }
public static void GetWindowPos(GlfwWindowPtr window, out int xpos, out int ypos) { GlfwDelegates.glfwGetWindowPos(window, out xpos, out ypos); }