C# Glfw.net ( old version from August 2016 but it understands me better


#1

Hello everyone I have problem with C# and passing arguments like I want pass arguments from command line or shortcut’s parameter like mygame.exe -w 1200 -h 720 -windowed

-w / -width is width of glfwWindow
-h / -height is height of glfwWindow
-windowed is glfwWindow runs in windowed mode ( like normal windowed game launcher )

Since Monday I tried to resolve glfw by Chman And I want switch fullscreen and windowed mode.
And width and height can not work.

But Console.WriteLine(); shows correct but glfwWindow doesn’t work with. Why does it happen - If I use passed arguments.
WARNING ONLY C#!

Because I am C# coder. I hate C++ because it is hard to understand with C++.
glfwWindow.cs

using Glfw3;
using System;
using System.Windows.Forms;

namespace HL
{
    public class glfwWindow
    {
        private int width, height;
        private bool fullscreen;
        private Glfw.Window window;
        private Glfw.VideoMode vid;

        public glfwWindow()
        {
            // lwjgl: GLFWErrorCallback.createPrint(System.err).set();
            Glfw.SetErrorCallback(ErrorCallBack);

            if (!Glfw.Init())
                throw new InvalidOperationException("Unable to initialize GLFW");

            Glfw.DefaultWindowHints();
            Glfw.WindowHint(Glfw.Hint.Visible, false);
            Glfw.WindowHint(Glfw.Hint.Resizable, false);
            Glfw.WindowHint(Glfw.Hint.Maximized, false);
            setSize(640, 480);
        }

        private void ErrorCallBack(Glfw.ErrorCode error, string description)
        {
            MessageBox.Show(description, "Error: Glfw is not initalized.");
        }

        public void createWindow(string title)
        {
            window = Glfw.CreateWindow(width,
                height,
                title,
                // lwjgl: "0" from fullscreen ? glfwPrimaryMonitor() : 0  in C# ´doesn't support "0" or "null" => IntPtr.Zero
                fullscreen ? Glfw.GetPrimaryMonitor() : Glfw.Monitor.None,
                null);

            if (window == null)
            {
                throw new InvalidOperationException("Error: Failed to create window!");
            }

            if(!fullscreen)
            {
                // windowed mode
                vid = Glfw.GetVideoMode(Glfw.GetPrimaryMonitor());
                Glfw.SetWindowPos(window, (vid.Width - width)/2, (vid.Height - height) / 2);
            }else
            {
                // fullscreen mode
                Glfw.SetWindowSize(window, vid.Width, vid.Height);
                Glfw.SetWindowPos(window, 0, 0);
            }

            Glfw.ShowWindow(window);

            Glfw.MakeContextCurrent(window);
        }

        public bool ShouldClose()
        {
            return Glfw.WindowShouldClose(window);
        }

        public void SwapBuffer()
        {
            Glfw.SwapBuffers(window);

            Glfw.PollEvents();
        }

        public void setSize(int width, int height)
        {
            this.width = width;
            this.height = height;
        //    Glfw.SetWindowSize(window, width, height);
        }

        public void setFullscreen(bool fullscreen)
        {
            this.fullscreen = fullscreen;
            if (!fullscreen)
            {
                // windowed mode
                vid = Glfw.GetVideoMode(Glfw.GetPrimaryMonitor());
                Glfw.SetWindowPos(window, (vid.Width - width) / 2, (vid.Height - height) / 2);
            }
            else
            {
                // fullscreen mode
                vid = Glfw.GetVideoMode(Glfw.GetWindowMonitor(window));
                Glfw.SetWindowPos(window, 0, 0);
            }
        }

        public int getWidth
        {
            get
            {
                return width;
            }

            set
            {
                width = value;
            }
        }

        public int getHeight
        {
            get
            {
                return height;
            }

            set
            {
                height = value;
            }
        }

        public bool getFullscreen
        {
            get
            {
                return fullscreen;
            }

            set
            {
                fullscreen = value;
            }
        }
    }
}

And MyApp.cs

using OpenGL;
using System;

namespace HL
{
    public class MyApp
    {
        private static glfwWindow win;
        private static int glfwWidth;
        private static int glfwHeight;
        private static string glfwWindowed;

        [STAThread]
        static void Main()
        {
            string[] args = Environment.GetCommandLineArgs();
            win = new glfwWindow();
            win.setFullscreen(true);
            win.createWindow("My App");

            for (int i = 1; i < args.Length; i++)
            {
                switch (args[i])
                {
                    case "-width":
                    case "-w":
                        glfwWidth = Int32.Parse(args[i + 1]);
                        win.setSize(glfwWidth, win.getHeight);
                        Console.WriteLine("argument width/w is " + win.getWidth.ToString() + " " + glfwWidth);
                        break;

                    case "-height":
                    case "-h":
                        glfwHeight = Int32.Parse(args[i + 1]);
                        win.setSize(win.getWidth, glfwHeight);
                        Console.WriteLine("argument height/h is " + win.getHeight.ToString() + " " + glfwHeight);
                        break;

                    case "-windowed":
                        glfwWindowed = "False";
                        if(!win.getFullscreen)
                        {
                            win.setFullscreen(bool.Parse(glfwWindowed));
                        }
                        Console.WriteLine("argument windowed is "+win.getFullscreen.ToString()+" "+glfwWindowed);
                        break;

                    default:
                        break;
                }
            }

            while (!win.ShouldClose())
            {
                Gl.ClearColor(1.0f, 1.0f, 0.0f, 1.0f);

                win.SwapBuffer();
            }
        }
    }
}

If i use passed argument than console.writeline() output:
argument w/width is 1200 1200
argument h/height is 720 720
argument windowed is False False . It means win.setFullscreen(false);

If I don’t use passed argument -windowed than glfwWindow will switch into fullscreen windowed mode.

How do I fix? Thanks! I am searching very lonnger But. I don’t know glfw has forum now. I am surprised. That is why GameDev forces me. :frowning:

Sorry my bad English.


#2

Your code first creates window by using width/height members of glfwWindow (which are set to fixed 640x480 values) and only then parses arguments and sets width/height members with values from command-line.

You need to do other way around - first parse command-line and only then call Glfw.CreateWindow function when you know what size window should be created.


#3

@mmozeiko correctly notes that this would work better if first you get up the correct values from your command line arguments, then pass these values to the window before creating it.

You should also note your program has a few other errors:

  1. You have commented out the Glfw.SetWindowSize call in your setSize function, so this won’t work. You probably did this because you call it from the constructor before your window is created, so you should remove that call from the constructor and uncomment the SetWindowSize line.
  2. To change a fullscreen window to windowed and visa versa use glfwSetWindowMonitor in your function setFullscreen.

Good luck.


#4

@mmozeiko Thanks for explanation! But dougbinks is right.

I told you that glfw.SetWindow(window, width, height) throws error exception. If I use glfwWindow -> setSize(width, height)

And It cannot resize. Why does exception throw like it sees crazy. What is newest version of glfw3 net? Possible is it correct version of 3.2.1

But I already tried net for C# and library doesn’t see implementations like createWindow(), etc… I really don’t understand where is documents of glfw3-net But I have tried this. I try to use older 3.1.2 of current glfw3 net

Do you think that method setWindowsSize has bug because it throws exception?
System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

@dougbinks Thanks for explanation:

glfw.SetWindowMonitor() you mean - apply window than it will switch window as fullscreen or windowed? Or I understand wrong?

I will try If it works.


#5

@mmozeiko is correct. You should first read your arguments, then set up your parameters and create your window.

I noted that you had two extra errors.

Further explanation to make this clear:

Do not call setSize(640,48) in constructor glfwWindow(), as this will cause an exception in Glfw.SetWindowSize() if you uncomment this line as required since window is not a valid glfw window yet since you have not created it. Instead you can put:

width =640;
height = 480;

Second the function setFullscreen() is wrong. You need to use the function glfwSetWindowMonitor to change from fullscreen to windowed or back.

However if you do as @mmozeiko suggests you will not need the change the size or the fullscreen settings since your window will be created with the correct value to start with.


#6

Why do you not say me about current version or current library:
Like Glfw3 by Chman released since August 2016 or
GLFW3 Net by Victor Prm released since July 2017??

What is better?

But I always have problem ( why do they scam me?

“System.TypeInitializationException: 'The type initializer for ‘glfw3.Glfw’ threw an exception.”

I want be finish my own game. :frowning:

I always updated from Github and it throws error: I can not understand why do you take advantage me? Grr. I feel stupid.
My code just example. If I try and try… But it throws exceptions…

using OpenGL;
using glfw3;
using System.Windows.Forms;

namespace HL
{
    public class MyApp
    {
        static int Main(string[] args)
        {
            Glfw.SetErrorCallback(ErrorCallBack);

            if (Glfw.Init() == 0)
                MessageBox.Show("Unable to initialize GLFW", "Error:");

            Glfw.DefaultWindowHints();
            Glfw.WindowHint(GlfwContants.MAXIMIZED, GlfwContants.FALSE);
            Glfw.WindowHint(GlfwContants.VISIBLE, GlfwContants.FALSE);
            Glfw.WindowHint(GlfwContants.RESIZABLE, GlfwContants.FALSE);

            GLFWwindow win = Glfw.CreateWindow(640, 480, "Test", null, null);
            if (win == null)
                MessageBox.Show("Failed to create the GLFW window", "Error:");

            Glfw.MakeContextCurrent(win);

            Glfw.ShowWindow(win);

            int argIndex = args.Length;
            for (int i = 0; i < argIndex; i++)
            {
                switch (args[i])
                {
                    case "-width":
                    case "-w":
                        
                        break;

                    case "-height":
                    case "-h":

                        break;

                    case "-windowed":

                        break;

                    default:
                        break;
                }
            }

            if(Glfw.WindowShouldClose(win) == 0)
            {
                Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                Gl.ClearColor(1.0f, 0.0f, 0.0f, 1.0f);
                Gl.Enable(EnableCap.DepthTest);

                Glfw.PollEvents();
                Glfw.SwapBuffers(win);
            }

            Glfw.Terminate();
            return 0;
        }

        private static void ErrorCallBack(int _0, string _1)
        {
            MessageBox.Show("GLFW is not installed.", "Error:");
        }
    }
}

Please say me seriously! I know nay developers are serious but they are maybe wrong. Because I found since Monday and I tried sometimes passing arguments. I really don’t understand that - I know Glfw.GetPrimaryMonitor() is fullscreen of monitor.

But I really want latest version of Glfw and try and it throws exceptions…

I already copy glfw3.dll from github of Glfw3-Net by Victor Brm and set “copy always” from Visual Studio 2017 Community.
And I try but it won’t show…
Just without passing arguments If I check…


#7

You really need to ask this to whoever wrote this .NET wrapper. Because this exception comes from .NET, not native glfw. This error you are seeing is not glfw issue.

Most likely it is trying to import/call missing function from native dll (mismatched versions?) or maybe it is trying to load wrong architecture (32-bit vs 64-bit), or maybe it simply cannot find dll file. Usually exception has a bit more info describing what is wrong, like a message or at least call stack.


#8

No I am using Any CPU means whatever x86 or x64.

You’re right I need optimize whole glfw3 from source and make 2 wrappers for glfw3.dll ( x64 ) and /x86).

PS: lwjgl is same good but can not switch fullscreen thanks I need to focus hard if I have many examples like I understand to completed examples. Like any developers have to start simple glfw window to switched mode of glfw window or multi windows like Blender has multiple OpenGL-window-containers ( Main window and preference window are both separated sdl- or glfw-window.

If I have successful examples than I will note you back If I am online. Okay. Thanks and I am sorry for hesitations because I am maybe aggressive or nervous. I am sorry that. I promise that I will keep cool. And I need drink coffee :slight_smile: I hope they’re good developers for C# / Java. If they are good than they should check if it works complety.
Example:

Windows 10:
x86 = working, x64 = not working. 
Linux:
x84 = working, x64 = working
MacOS 10.12.4/10.12.5:
x64 = working
Android:
arm = not working
etc ....

I need to know because our library doesn’t support for x64. And I need to wait for that. Thanks!


#9

I have a hard time understanding what does this example means or what examples are you talking about. But I think you are misunderstanding how “Any CPU” executables work in .NET.

If .NET executable is marked as Any CPU, then .NET system determines what “bits” (64 or 32) your OS supports, and then starts compiling & executing your app in this “bitness”. Which means that at runtime your code is running either as 64-bit code or as 32-bit code. There is no other way. You cannot run part of application in 64-bit code, and part of it as 32-bit code. That would require two different processes.

So when you include native library, such as glfw3.dll, in your project, that means that “bitness” of this native code must match “bitness” of whatever .NET code is executing at runtime. Otherwise you’ll get exceptions about bad format executables, or type load exceptions or similar.

If you want to call native code, you have following options in .NET:

  1. distribute either only 32-bit, or only 64-bit native dll library, and then mark your .NET executable/libraries also with same bitness. This will guarantee that managed code will be able to call native code. Of course if you distribute everything only as 64-bit, then your app won’t run on 32-bit system. Or if you distribute it only as 32-bit app, then you are loosing potential performance gains of 64-bit architecture and your app will run a tiny bit slower.

  2. other option is to distribute both 32-bit and 64-bit native dlls with your application. Then you can mark .NET code as “Any CPU”, and write special loader, that will load 32-bit or 64-bit native dll depending on which mode your .NET code is executing (with help of LoadLibrary/GetProcAddress functions and System.InteropServices.Marshal class). This will give you best performance, but its a tiny bit of work to write a simpler loader.

  3. there is an alternative to keep Any CPU for .net executable. The trick is to keep glfw.dll file with same name for 32-bit and 64-bit code in different directories, and then simply load proper library with LoadLibrary, then you don’t need to write loader with GetProcAddress function, .NET will simply resolve DllImport functions by name from currently loaded dll in your process. Not sure how stable this trick is, but few years ago when I tried it, I remember this working just fine.

And again, this is not a glfw issue. This is issue with how you are writing C#/.NET code. Showing/not showing windows are irrelevant here.


#10

Woah I am happy for resizing…

Resize works only fine. Yeah Thank you my @mmozeiko - PS I love you because you explain me hard. But I always tired -windowed and it won’t

I try and I focus in my head now. And I have no problem now. But why does window not center of my monitor. just 200 and 200 top and left.

using Glfw3;
using OpenGL;
using System;

namespace HL
{
    sealed class MyApp : ErrorCode
    {
        private static int width;
        private static int height;

        private static Glfw.Window window;
        private static Glfw.VideoMode vid;
        private static Glfw.Monitor monitor;

        static void Main(string[] args)
        {
            Init();

            if (!Glfw.Init())
                Environment.Exit(-1);


            if (monitor)
            {
                vid = Glfw.GetVideoMode(Glfw.GetPrimaryMonitor());
                width = vid.Width;
                height = vid.Height;
            }
            else
            {
                width = 640;
                height = 480;
                //Glfw.SetWindowMonitor(window ,monitor, (vid.Width - width) / 2, (vid.Height - height) / 2, width, height, 60);
            }

            window = Glfw.CreateWindow(width, height, "Hello World", monitor, Glfw.Window.None);
            if (!window)
            {
                Glfw.Terminate();
                Environment.Exit(-1);
            }

            int argIndex = args.Length;
            for (int i = 0; i < argIndex; i++)
            {
                switch (args[i])
                {
                    case "-width":
                    case "-w":
                        width = int.Parse(args[i + 1]);
                        Glfw.SetWindowSize(window, width, height);
                        Console.WriteLine("argument is for h/height " + args[i + 1] + " \n");
                        break;
                    case "-height":
                    case "-h":
                        height = int.Parse(args[i + 1]);
                        Glfw.SetWindowSize(window, width, height);
                        Console.WriteLine("argument is for w/height " + args[i + 1] + " \n");
                        break;

                    case "-windowed":
                        monitor = Glfw.Monitor.None;
                        Console.WriteLine("argument is for windowed = False \n");
                        break;

                    default:
                        break;
                }
            }

            Glfw.DefaultWindowHints();
            Glfw.WindowHint(Glfw.Hint.Maximized, false);
            Glfw.WindowHint(Glfw.Hint.Resizable, false);

            Glfw.SetFramebufferSizeCallback(window, FramebufferSizeCallback);
            Glfw.MakeContextCurrent(window);
            Glfw.SwapInterval(1);

            Gl.ClearColor(1.0f, 0.0f, 0.0f, 1.0f);

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

                Glfw.SwapBuffers(window);
                Glfw.WaitEvents();
            }
        }

        private static void FramebufferSizeCallback(Glfw.Window window, int width, int height)
        {
            Gl.Viewport(0, 0, width, height);
        }
    }
}

And ErrorBase.cs like file from Glfw.Test/TestBase.cs

I am really disappointed because window with monitor doesn’t work if game starts fullscreen and I use passing argument “-windowed” than It will run as windowed mode. But I tried. No success :frowning: And resizing of window no problem now. I have found example TestGammaRamp.cs of Glfw3 net and fullscreen and window switcher doesn’t work yet :confused:

Or I am really wrong if I write currently?


#11

Its not centering because you are not asking it to center. See the documentation how to set position of window.