user2685022
user2685022

Reputation: 41

OpenTK: Why is GraphicsMode not available?

I just started to learn OpenTK and stumbled upon a problem when going through this tutorial.

This is what I tried:

using System;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Windowing.Desktop;
using OpenTK.Windowing.GraphicsLibraryFramework;
namespace Testing
{
    public class GraphicsWindow : GameWindow
    {
        public GraphicsWindow(int width, int height, string title) : base(width, height, GraphicsMode.Default, title)
        {
            
        }
    }
}

The Enum GraphicsMode is not found for some reason (should be found in namespace OpenTK.Graphics). Another one is that GameWindow does not have a constructor taking 4 arguments.

I have installed the latest version of the OpenTK Nuget package (4.0.6). The project I created targets .NET Core.

Any ideas?

Upvotes: 4

Views: 2074

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

The tutorial is based on OpenTK 3.x, which was written for .NET framwork. OpenTK 4.x is written for .NET core. In 3.x the GameWindow was part of the OpenTK.Graphics namespace. Now the class is contained in OpenTK.Windowing.Desktop and behaves different. The constructor has 2 arguments, GameWindowSettings and NativeWindowSettings.

namespace Testing
{
    public class GraphicsWindow : GameWindow
    {
        public GraphicsWindow(int width, int height, string title)
            : base(
                  new GameWindowSettings(),
                  new NativeWindowSettings()
                  {
                      Size = new OpenTK.Mathematics.Vector2i(width, height),
                      Title = title
                  })
            { }
    }
}

Alternatively create a static factory method:

namespace Testing
{
    public class GraphicsWindow : GameWindow
    {
        public static GraphicsWindow New(int width, int height, string title)
        {
            GameWindowSettings setting = new GameWindowSettings();
            NativeWindowSettings nativeSettings = new NativeWindowSettings();
            nativeSettings.Size = new OpenTK.Mathematics.Vector2i(width, height);
            nativeSettings.Title = title;
            return new GraphicsWindow(setting, nativeSettings);
        }

        public GraphicsWindow(GameWindowSettings setting, NativeWindowSettings nativeSettings) 
            : base(setting, nativeSettings)
        {}
    }
}
var myGraphicsWindow = GraphicsWindow.New(800, 600);

See also OpenTK_hello_triangle

Upvotes: 7

Related Questions