-
I create a sdl opengl window in 1280x720, but it scale by system hdpi. using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Silk.NET.Assimp;
using Silk.NET.Input;
using Silk.NET.Maths;
using Silk.NET.OpenGL;
using Silk.NET.SDL;
using Silk.NET.Windowing;
namespace SpaceFlight
{
public class Game
{
private static GL? _gl = null;
private static IWindow? _window = null;
private static IInputContext? _input = null;
internal static readonly HashSet<GameObject> allGameObjects = new HashSet<GameObject>();
public static GL? gl => _gl;
public static Sdl sdl => SdlProvider.SDL.Value;
public static IWindow? window => _window;
public static IInputContext? input => _input;
public Game(string name)
{
if (_window != null) throw new Exception("Game arrady start");
sdl.SetHint(Sdl.HintWindowsDpiAwareness, "system");
Silk.NET.Windowing.Window.PrioritizeSdl();
//Silk.NET.Windowing.Window.PrioritizeGlfw();
WindowOptions options = new WindowOptions(true, new Vector2D<int>(Sdl.WindowposUndefined, Sdl.WindowposUndefined), new Vector2D<int>(1280, 720), 0.0, 0.0, GraphicsAPI.Default, name, WindowState.Normal, WindowBorder.Resizable, isVSync: false, shouldSwapAutomatically: true, VideoMode.Default);
_window = Silk.NET.Windowing.Window.Create(options);
_window.Load += Loaded;
_window.Update += Update;
_window.Render += Render;
_window.Run();
}
private void Loaded()
{
_gl = _window?.CreateOpenGL();
_input = _window?.CreateInput();
// gl?.DepthFunc(DepthFunction.Gequal);
gl?.ClearColor(0,0,0,1);
gl?.ClearDepth(0);
gl?.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
}
private void Update(double delta)
{
List<GameObject> cache = new List<GameObject>(from x in allGameObjects where !x.Destoryed select x);
foreach(GameObject gameObject in cache) gameObject.Update(delta);
}
private void Render(double delta)
{
gl?.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
gl?.Viewport(0,0,(uint)_window.Size.X,(uint)_window.Size.Y);
List<GameObject> cache = new List<GameObject>(from x in allGameObjects where !x.Destoryed select x);
Dictionary<uint,HashSet<GameObject>> objectAndSignal = new Dictionary<uint, HashSet<GameObject>>();
foreach(GameObject gameObject in cache)
{
foreach(uint signal in gameObject.RenderSignal())
{
if(!objectAndSignal.TryGetValue(signal,out HashSet<GameObject>? list))
{
list = new HashSet<GameObject>();
objectAndSignal.Add(signal,list);
}
list.Add(gameObject);
}
}
List<KeyValuePair<uint,HashSet<GameObject>>> values = new List<KeyValuePair<uint, HashSet<GameObject>>>(objectAndSignal);
for (int i = 0; i < values.Count; i++)
{
var a = values[i];
for (int j = i + 1; j < values.Count; j++)
{
var b = values[j];
if (a.Key > b.Key)
{
values[j] = a;
values[i] = b;
a = b;
}
}
}
for (int i = 0; i < values.Count; i++)
{
uint signal = values[i].Key;
foreach(GameObject gameObject in values[i].Value)
{
gameObject.Render(delta,signal);
}
}
}
public void Destory()
{
_window?.Close();
_window = null;
_gl = null;
_input = null;
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Create an application manifest file in the project to configure DPI awareness so that developers can control scaling through code. Reference documentation: Please refer to Setting the default DPI awareness for a process. |
Beta Was this translation helpful? Give feedback.
-
By checking SDL2 c code and make few test, I found the sloution. sdl.SetHint(Sdl.HintWindowsDpiAwareness, "permonitor");
sdl.VideoInit(null as string); Just invoke |
Beta Was this translation helpful? Give feedback.
By checking SDL2 c code and make few test, I found the sloution.
Just invoke
SDL_VideoInit()
before create window. it looks like some kind of bug on SDL2 thatSDL_Init()
andSDL_InitSubSystem()
not invokeSDL_VideoInit()
. It probably came from SDL2 compiltion issues.