Skip to content

Commit

Permalink
Add support for 4K
Browse files Browse the repository at this point in the history
  • Loading branch information
drug007 committed Jan 21, 2024
1 parent d0fd64d commit 9b34a4d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
32 changes: 28 additions & 4 deletions examples/sdl/source/app.d
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module examples.sdl;

import std.datetime : Clock;
import std.getopt : defaultGetoptPrinter, getopt;
import arsd.nanovega;
import nanogui.sdlbackend : SdlBackend;
import nanogui.widget : Widget;
Expand Down Expand Up @@ -179,9 +180,9 @@ class MyGui : SdlBackend
Label lblCpuUsage;
ProcessCPUWatcher cpuWatcher;

this(int w, int h, string title)
this(int w, int h, string title, int scale)
{
super(w, h, title);
super(w, h, title, scale);
}

override void onVisibleForTheFirstTime()
Expand Down Expand Up @@ -551,8 +552,29 @@ union Payload
}
alias Item = TaggedAlgebraic!Payload;

void main () {
auto gui = new MyGui(1000, 800, "Nanogui using SDL2 backend");
int main (string[] args)
{
int scale = 1;

auto helpInformation = getopt(
args,
"scale", "Scale, 2 for 4K monitors and 1 for the rest", &scale,
);

if (helpInformation.helpWanted)
{
defaultGetoptPrinter("Usage:", helpInformation.options);
return 0;
}

if (scale != 1 && scale != 2)
{
import std;
stderr.writeln("Scale can be 1 or 2 only");
return 1;
}

auto gui = new MyGui(1000, 800, "Nanogui using SDL2 backend", scale);
gui.onBeforeLoopStart = () {
import std.datetime : SysTime, seconds, Clock;
static SysTime prevStdTime;
Expand All @@ -566,4 +588,6 @@ void main () {
}
};
gui.run();

return 0;
}
5 changes: 3 additions & 2 deletions source/nanogui/glcanvas.d
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@ public:
if (mDrawBorder)
drawWidgetBorder(ctx);

const scale = screen.scale; // TODO PERFORMANCE it can be expensive operation
auto mImage = glCreateImageFromOpenGLTexture(ctx, mColorBuf.handle, width, height, NVGImageFlag.NoDelete);
assert(mImage.valid);
auto mPaint = ctx.imagePattern(
mPos.x + 1,
mPos.y + 1.0f,
mSize.x - 2,
mSize.y - 2,
mSize.x/scale - 2,
mSize.y/scale - 2,
0,
mImage);

Expand Down
11 changes: 8 additions & 3 deletions source/nanogui/screen.d
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Screen : Widget
{
import nanogui.window : Window;

this(int w, int h, long timestamp)
this(int w, int h, long timestamp, int scale)
{
super(null);
size = vec2i(w, h);
Expand All @@ -23,6 +23,7 @@ class Screen : Widget
mCursor = Cursor.Arrow;
mPixelRatio = 1.0;
mClearEnabled = true;
mScale = scale;
}

auto currTime() const { return mTimestamp; }
Expand All @@ -38,6 +39,8 @@ class Screen : Widget
}
}

auto scale() const { return mScale; }

auto lastInteraction() { return mLastInteraction; }

override void draw(ref NanoContext ctx)
Expand Down Expand Up @@ -72,11 +75,11 @@ class Screen : Widget
// draw the rest
if (mClearEnabled)
{
glViewport(0, 0, size.x, size.y);
glViewport(0, 0, size.x*mScale, size.y*mScale);
glClearColor(0., 0., 0., 0);
glClear(glNVGClearFlags); // use NanoVega API to get flags for OpenGL call
}
ctx.beginFrame(size.x, size.y); // begin rendering
ctx.beginFrame(size.x, size.y, mScale); // begin rendering
scope(exit)
{
if (ctx.inFrame)
Expand Down Expand Up @@ -450,4 +453,6 @@ protected:
void delegate(Vector2i) mResizeCallback;
Array!GLCanvas mGLCanvases;
bool mClearEnabled;
/// Integer scale for 4K support
int mScale;
}
4 changes: 2 additions & 2 deletions source/nanogui/sdlapp.d
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SdlApp
{
alias Event = SDL_Event;

this(int w, int h, string title)
this(int w, int h, string title, int scale = 1)
{
/* Avoid locale-related number parsing issues */
version(Windows) {}
Expand Down Expand Up @@ -66,7 +66,7 @@ class SdlApp
// create an OpenGL-enabled SDL window
window = new SDL2Window(_sdl2,
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
width, height,
width * scale, height * scale,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN );

window.setTitle(title);
Expand Down
10 changes: 5 additions & 5 deletions source/nanogui/sdlbackend.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import nanogui.sdlapp : SdlApp;

class SdlBackend : Screen
{
this(int w, int h, string title)
this(int w, int h, string title, int scale)
{
_sdlApp = new SdlApp(w, h, title);
_sdlApp = new SdlApp(w, h, title, scale);

_sdlApp.onBeforeLoopStart = ()
{
Expand Down Expand Up @@ -77,8 +77,8 @@ class SdlBackend : Screen

_sdlApp.invalidate;

ctx.mouse.x = event.motion.x;
ctx.mouse.y = event.motion.y;
ctx.mouse.x = event.motion.x/scale;
ctx.mouse.y = event.motion.y/scale;

if (event.motion.state & SDL_BUTTON_LMASK)
btn = MouseButton.Left;
Expand Down Expand Up @@ -168,7 +168,7 @@ class SdlBackend : Screen
mCursorSet[Cursor.HResize] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
mCursorSet[Cursor.VResize] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);

super(w, h, Clock.currTime.stdTime);
super(w, h, Clock.currTime.stdTime, scale);
theme = new Theme(ctx);
}

Expand Down

0 comments on commit 9b34a4d

Please sign in to comment.