2023年9月29日

Opentk Tutorial 1---Basic Window



Opentk 튜토리얼 1---기본 창

Opentk チュートリアル 1---基本ウィンドウ

Opentk教學1---基本視窗 



How to use?


    var nativeWindowSettings = new NativeWindowSettings()
    {
        Size = new Vector2i(800, 600),
        Title = "Basic Window",
        // This is needed to run on macos
        Flags = ContextFlags.ForwardCompatible,
    };
    GameWindowSettings settings = GameWindowSettings.Default;
    using (var window = new Basic_Window(settings, nativeWindowSettings))
    {
 
        window.Run();
 
    }


-----------Window------------------------

60fps

using OpenTK.Graphics.OpenGL4;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;
using OpenTK.Windowing.GraphicsLibraryFramework;
using System.Diagnostics;
 
 
 
public class Basic_Window : GameWindow
{
 
    private Stopwatch stopwatch = new Stopwatch();
    private TimeSpan one_sec = TimeSpan.FromSeconds(1);
    private int count = 0;
    public Basic_Window(GameWindowSettings gameWindowSettings, NativeWindowSettings nativeWindowSettings) : base(gameWindowSettings, nativeWindowSettings)
    {
        this.VSync = VSyncMode.On;
        this.Resize += Basic_Window_Resize;
        this.Load += Basic_Window_Load;
        this.Unload += Basic_Window_Unload;
        stopwatch.Start();
 
    }
 
    private void Basic_Window_Load()
    {
        GL.ClearColor(1, 0, 0, 1);
 
    }
 
    private void Basic_Window_Unload()
    {
      stopwatch.Stop();
    }
 
 
    private void Basic_Window_Resize(ResizeEventArgs obj)
    {
 
 
 
    }
 
    protected override void OnRenderFrame(FrameEventArgs args)
    {
        base.OnRenderFrame(args);
 
        GL.Viewport(0,0,this.Size.X,this.Size.Y);
 
        count++;
        if (stopwatch.Elapsed >= one_sec)
        {
 
            count = 0;
            stopwatch.Restart();
        }
 
        this.SwapBuffers();
 
    }
 
    protected override void OnUpdateFrame(FrameEventArgs e)
    {
        base.OnUpdateFrame(e);
        if (KeyboardState.IsKeyDown(Keys.Escape))
        {
 
            Close();
        }
 
 
    }
 
}

沒有留言:

張貼留言