[英]Setting my GameWindow to fullscreen will stretch it to the desktop resolution
將我的 GameWindow 設置為全屏會拉伸它,如何在不丟失原始 window 分辨率的情況下使用黑條將其設置為全屏?
這是我的代碼:
using System;
using System.Collections.Generic;
using System.Text;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.GraphicsLibraryFramework;
using OpenTK.Windowing.Desktop;
using System.Drawing;
namespace OnlineGame
{
public class Game : GameWindow
{
public Game(GameWindowSettings gameWindowSettings, NativeWindowSettings nativeWindowSettings)
: base(gameWindowSettings, nativeWindowSettings)
{
WindowState = WindowState.Fullscreen;
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
if (KeyboardState.IsKeyDown(Keys.Escape))
{
Close();
}
base.OnUpdateFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.ClearColor(Color.CornflowerBlue);
this.SwapBuffers();
}
}
}
使用GL.Viewport
將視口矩形設置為 window 的子區域。 作為GL.Clear
的“復制”操作不受視口矩形的影響。 因此,您還需要設置一個Scissors Test 。
您需要知道 window 的當前大小( current_w
, current_h
)和 window 的原始大小( original_w
, original_h
)。 計算當前 window ( current_aspect
) 的縱橫比和原始 window ( original_aspect
) 的縱橫比:
double current_aspect = (double)current_w / current_h;
double original_aspect = (double)original_w / original_h;
計算子區域並設置視口和剪刀矩形:
int w = (int)(current_w * original_aspect / current_aspect);
int x = (current_w - w) / 2;
GL.Scissor(x, 0, w, this.Size.Y);
GL.Viewport(x, 0, w, this.Size.Y);
用黑色清除整個 window。 然后限制視口矩形並啟用剪刀測試。
請注意,您必須在GL.Clear
指令之前設置清除顏色( GL.ClearColor
)。
protected override void OnUpdateFrame(FrameEventArgs e)
{
if (KeyboardState.IsKeyDown(Keys.Escape))
{
Close();
}
base.OnUpdateFrame(e);
int original_w = ...;
int original_h = ...;
int current_w = this.Size.X;
int current_h = this.Size.Y;
double current_aspect = (double)current_w / current_h;
double original_aspect = (double)original_w / original_h;
GL.Disable(EnableCap.ScissorTest);
GL.Viewport(0, 0, current_w, current_h);
GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.Enable(EnableCap.ScissorTest);
if (current_aspect > original_aspect)
{
int w = (int)(current_w * original_aspect / current_aspect);
int x = (current_w - w) / 2;
GL.Scissor(x, 0, w, this.Size.Y);
GL.Viewport(x, 0, w, this.Size.Y);
}
else
{
int h = (int)(current_h * current_aspect / original_aspect);
int y = (current_h - h) / 2;
GL.Scissor(0, y, this.Size.X, h);
GL.Viewport(0, y, this.Size.X, h);
}
GL.ClearColor(Color.CornflowerBlue);
GL.Clear(ClearBufferMask.ColorBufferBit);
// [...]
this.SwapBuffers();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.