首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将我的GameWindow设置为全屏会将其扩展到桌面分辨率

将我的GameWindow设置为全屏会将其扩展到桌面分辨率
EN

Stack Overflow用户
提问于 2021-02-13 06:53:58
回答 1查看 280关注 0票数 1

将我的GameWindow设置为全屏扩展它,如何设置它的全屏而不丢失使用黑条的原始窗口分辨率?

这是我的密码:

代码语言:javascript
复制
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();
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-13 07:23:33

使用GL.Viewport将视图矩形设置为窗口的子区域。作为GL.Clear的“复制”操作不受视口矩形的影响。因此,您还需要设置一个剪刀试验

您需要知道窗口的当前大小(current_wcurrent_h)和窗口的原始大小(original_woriginal_h)。计算当前窗口(current_aspect)的纵横比和原始窗口的纵横比(original_aspect):

代码语言:javascript
复制
double current_aspect = (double)current_w / current_h;
double original_aspect = (double)original_w / original_h;

计算子区域并设置视口和剪刀矩形:

代码语言:javascript
复制
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);

用黑色清除整扇窗户。然后限制视口矩形并启用剪刀测试。

注意,必须在GL.ClearColor指令之前设置清晰的颜色( GL.Clear )。

代码语言:javascript
复制
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();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66182561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档