Skip to content

Commit

Permalink
Merge pull request #42 from drug007/enabling_4k
Browse files Browse the repository at this point in the history
Enabling 4k
  • Loading branch information
drug007 authored Jan 21, 2024
2 parents 11b4c8e + 9b34a4d commit 85b50b7
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 51 deletions.
14 changes: 2 additions & 12 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
name: Build

on:
push:
branches:
- master
- develop
pull_request:
branches:
- master
- develop
# Use this branch name in your fork to test changes
- github-actions
on: push

jobs:
main:
Expand All @@ -20,7 +10,7 @@ jobs:
fail-fast: false
matrix:
# Latest stable version, update at will
os: [ ubuntu-18.04, windows-2019 ]
os: [ ubuntu-20.04, windows-2019 ]
dc: [ dmd-latest, ldc-latest ]

runs-on: ${{ matrix.os }}
Expand Down
6 changes: 3 additions & 3 deletions auxil/dub.sdl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name "auxil"
description "A minimal D application."
authors "drug007"
copyright "Copyright © 2020-2022, drug007"
copyright "Copyright © 2020-2024, drug007"
license "BSL"
targetType "library"
dependency "taggedalgebraic" version="~>0.11.22"
dependency "automem" version="~>0.6.1"
dependency "automem" version="~>0.6.9"
dependency "nogc" version="~>0.5.0"

configuration "library" {
Expand All @@ -19,5 +19,5 @@ configuration "unittest" {

dflags "-dip25" "-dip1000" "-dip1008"

dependency "unit-threaded" version="~>0.10.7"
dependency "unit-threaded" version="~>2.1.8"
}
8 changes: 4 additions & 4 deletions dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name "nanogui"
description "Port of nanogui to dlang."
license "BSD-like"
authors "drug007"
copyright "Copyright © 2018-2022, drug007"
dependency "arsd-official:nanovega" version="~>10.9.2"
copyright "Copyright © 2018-2024, drug007"
dependency "arsd-official:nanovega" version="~>11.3.0"
dependency "gfm:math" version="~>8.0.6"
dependency "gfm7:opengl" version="~>1.1.2"
dependency "gfm7:sdl2" version="~>1.1.2"
dependency "gfm7:opengl" version="~>1.2.0"
dependency "gfm7:sdl2" version="~>1.2.0"
dependency "auxil" version="*" path="./auxil"
2 changes: 1 addition & 1 deletion examples/renderer/dub.selections.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"automem": "0.6.7",
"auxil": {"path":"../../auxil"},
"bindbc-loader": "0.3.2",
"bindbc-opengl": "0.15.0",
"bindbc-opengl": "1.1.0",
"bindbc-sdl": "0.19.2",
"binrange": "1.0.3",
"dplug": "9.3.4",
Expand Down
4 changes: 2 additions & 2 deletions examples/sdl/dub.sdl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name "nanogui-sdl"
description "Example of nanogui using sdl backend"
authors "drug"
copyright "Copyright © 2018-2022, drug"
copyright "Copyright © 2018-2024, drug"
license "BSL"

targetType "executable"
targetName "example-sdl"

dependency "nanogui" path="../.."
dependency "resusage" version="~>0.3.2"
dependency "resusage" version="~>0.4.0"
dependency "taggedalgebraic" version="0.11.22"
20 changes: 10 additions & 10 deletions examples/sdl/dub.selections.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
{
"fileVersion": 1,
"versions": {
"arsd-official": "10.9.2",
"automem": "0.6.7",
"arsd-official": "11.3.0",
"automem": "0.6.9",
"auxil": {"path":"../../auxil"},
"bindbc-loader": "0.3.2",
"bindbc-opengl": "0.15.0",
"bindbc-sdl": "0.19.2",
"bindbc-loader": "1.0.3",
"bindbc-opengl": "1.1.0",
"bindbc-sdl": "1.2.4",
"gfm": "8.0.6",
"gfm7": "1.1.2",
"intel-intrinsics": "1.10.2",
"gfm7": "1.2.0",
"intel-intrinsics": "1.11.18",
"localimport": "1.3.0",
"nanogui": {"path":"../.."},
"nogc": "0.5.1",
"resusage": "0.3.2",
"resusage": "0.4.0",
"taggedalgebraic": "0.11.22",
"test_allocator": "0.3.3",
"unit-threaded": "0.10.8"
"test_allocator": "0.3.4",
"unit-threaded": "2.1.8"
}
}
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
17 changes: 11 additions & 6 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,11 +39,13 @@ class Screen : Widget
}
}

auto scale() const { return mScale; }

auto lastInteraction() { return mLastInteraction; }

override void draw(ref NanoContext ctx)
{
import arsd.simpledisplay;
import bindbc.opengl;

// draw GLCanvas widgets to textures
foreach(glcanvas; mGLCanvases[])
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 @@ -132,7 +135,7 @@ class Screen : Widget
mTooltipShown = (alpha > threshold - 0.01) ? true : false;

ctx.beginPath;
ctx.fillColor(Color(0, 0, 0, 255));
ctx.fillColor(nvgRGBA(0, 0, 0, 255));
ctx.roundedRect(bounds[0] - 4 - h, bounds[1] - 4,
cast(int) (bounds[2] - bounds[0]) + 8,
cast(int) (bounds[3] - bounds[1]) + 8, 3);
Expand All @@ -143,7 +146,7 @@ class Screen : Widget
ctx.lineTo(px - 7, bounds[1] + 1);
ctx.fill();

ctx.fillColor(Color(255, 255, 255, 255));
ctx.fillColor(nvgRGBA(255, 255, 255, 255));
ctx.fontBlur(0.0f);
ctx.textBox(pos.x - h, pos.y, tooltipWidth,
widget.tooltip);
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 85b50b7

Please sign in to comment.