Skip to content

Commit

Permalink
refactor of window and scaled render resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
redthing1 committed Jul 15, 2024
1 parent 533e27b commit 0d7c03e
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 97 deletions.
1 change: 1 addition & 0 deletions demo/celtest/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Game : Core {
this(int width, int height) {
// core init here
Core.render_oversample_hidpi = true;
Core.default_filter_mode = raylib.TextureFilter.TEXTURE_FILTER_TRILINEAR;

super(width, height, vr_enabled ? "celtest [VR]" : "celtest");
}
Expand Down
2 changes: 1 addition & 1 deletion demo/nuidemo/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Game : Core {

this() {
window_resizable = true;
render_oversample_hidpi = true;
render_oversample_factor = 1;
sync_render_target_to_window_resolution = true;

super(WIDTH, HEIGHT, "nuidemo");
Expand Down
3 changes: 1 addition & 2 deletions demo/nuidemo/source/gui_root.d
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ class GuiRoot : Component, Renderable2D, Updatable {
UpdateNuklear(ctx);

// GUI
// auto window_bounds = nk_rect(0, 0, GetRenderWidth(), GetRenderHeight());
auto window_bounds = Rectangle(0, 0, GetRenderWidth(), GetRenderHeight());
auto window_bounds = Rectangle(0, 0, Core.window.render_width, Core.window.render_height);
if (nk_begin(ctx, "Demo", RectangleToNuklear(ctx, window_bounds),
nk_panel_flags.NK_WINDOW_BORDER | nk_panel_flags.NK_WINDOW_TITLE)) {
enum EASY = 0;
Expand Down
35 changes: 11 additions & 24 deletions source/re/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ abstract class Core {
/// oversampling factor for internal rendering
public static int render_oversample_factor = 1;

/// whether to automatically oversample for hidpi
public static bool render_oversample_hidpi = false;

/// whether to automatically resize the render target to the window size
public static bool sync_render_target_to_window_resolution = false;

Expand Down Expand Up @@ -114,11 +111,10 @@ abstract class Core {

default_resolution = Vector2(width, height);
if (!Core.headless) {
window = new Window(width, height);
window = new Window();
window.set_resizable(window_resizable);
window.initialize();
window.initialize(width, height);
window.set_title(title);
calculate_hidpi_scaling();
calculate_render_resolution();
}

Expand Down Expand Up @@ -244,8 +240,10 @@ abstract class Core {
raylib.BeginShaderMode(vr.distortion_shader);
}

RenderExt.draw_render_target(scene.render_target, Rectangle(0, 0,
window.width, window.height), scene.composite_mode.color);
auto render_target_rect = Rectangle(0, 0, window.screen_width, window.screen_height);
RenderExt.draw_render_target(
scene.render_target, render_target_rect, scene.composite_mode.color
);

version (vr) {
if (vr_distort)
Expand Down Expand Up @@ -334,7 +332,7 @@ abstract class Core {
}

private void handle_window_resize() {
log.info("window resized to (%s,%s)", window.width, window.height);
log.info("window resized to (%s,%s)", window.screen_width, window.screen_height);
// window was resized
if (sync_render_target_to_window_resolution) {
calculate_render_resolution();
Expand All @@ -345,22 +343,11 @@ abstract class Core {
}
}

private void calculate_hidpi_scaling() {
// get dpi scale factor
auto scale_dpi = cast(int) window.scale_dpi;

if (render_oversample_hidpi) {
// if oversampling is enabled, calculate the oversample factor
render_oversample_factor = scale_dpi;
log.info("auto-oversampling enabled, setting oversample factor to %d", render_oversample_factor);
}
}

private void calculate_render_resolution() {
// since window was resized, update our render resolution
// first get the new window size
auto render_res_x = window.width;
auto render_res_y = window.height;
auto render_res_x = window.render_width;
auto render_res_y = window.render_height;

// reset mouse scale
raylib.SetMouseScale(1.0, 1.0);
Expand All @@ -376,8 +363,8 @@ abstract class Core {

// set the render resolution
default_resolution = Vector2(render_res_x, render_res_y);
Core.log.info(format("updating render resolution to %s (dpi scale %s) (oversample %s)",
default_resolution, window.scale_dpi, render_oversample_factor));
Core.log.info(format("updating render resolution to %s (oversample %s)",
default_resolution, render_oversample_factor));
}
}

Expand Down
144 changes: 79 additions & 65 deletions source/re/gfx/window.d
Original file line number Diff line number Diff line change
Expand Up @@ -11,81 +11,24 @@ static import raylib;
class Window {
/// whether window has been created
private bool _created = false;
// raw window width and height
private int _width;
private int _height;
/// the window dpi scale
private float _scale_dpi;
/// the monitor
private int _monitor;
/// flags
private uint _window_flags;
/// properties
private bool _resizable;

/// creates a window instance with the given dimensions
this(int width, int height) {
_width = width;
_height = height;
}

/// window width
@property int width() {
update_window();
return cast(int)(_width);
}

/// window height
@property int height() {
update_window();
return cast(int)(_height);
}

@property float scale_dpi() {
update_window();
return _scale_dpi;
}

// window dpi-scaled width
@property int width_dpi() {
return cast(int)(width * scale_dpi);
}

// window dpi-scaled height
@property int height_dpi() {
return cast(int)(height * scale_dpi);
}

/// initializes the window
public void initialize() {
public void initialize(int width, int height) {
// set config flags
// tell raylib we're hidpi aware
_window_flags |= raylib.ConfigFlags.FLAG_WINDOW_HIGHDPI;
raylib.SetConfigFlags(_window_flags);

// create the window
raylib.InitWindow(_width, _height, "");
raylib.InitWindow(width, height, "");
_created = true; // window has been created

// set options
raylib.SetTargetFPS(Core.target_fps);
// // get properties
// _scale_dpi = get_display_dpi_scale();
update_window();
}

public static float get_display_dpi_scale() {
auto scale_dpi_vec = raylib.GetWindowScaleDPI();
return max(scale_dpi_vec.x, scale_dpi_vec.y);
}

public void set_title(string title) {
raylib.SetWindowTitle(toStringz(title));
}

public void resize(int width, int height) {
raylib.SetWindowSize(width, height);
update_window();
}

public void set_resizable(bool resizable) {
Expand All @@ -102,13 +45,84 @@ class Window {
}
}

private void update_window() {
_width = raylib.GetScreenWidth();
_height = raylib.GetScreenHeight();
_scale_dpi = get_display_dpi_scale();
}

public void destroy() {
raylib.CloseWindow();
}

public @property float dpi_scale() {
auto scale_dpi_vec = raylib.GetWindowScaleDPI();
return max(scale_dpi_vec.x, scale_dpi_vec.y);
}

public @property int screen_width() {
return raylib.GetScreenWidth();
}

public @property int screen_height() {
return raylib.GetScreenHeight();
}

public @property int render_width() {
return raylib.GetRenderWidth();
}

public @property int render_height() {
return raylib.GetRenderHeight();
}

public @property bool is_minimized() {
return raylib.IsWindowMinimized();
}

public @property bool is_maximized() {
return raylib.IsWindowMaximized();
}

public @property bool is_focused() {
return raylib.IsWindowFocused();
}

public @property bool is_resized() {
return raylib.IsWindowResized();
}

public void set_icon(raylib.Image image) {
raylib.SetWindowIcon(image);
}

public void resize(int width, int height) {
raylib.SetWindowSize(width, height);
}

public void set_position(int x, int y) {
raylib.SetWindowPosition(x, y);
}

public void set_title(string title) {
raylib.SetWindowTitle(title.toStringz);
}

public void toggle_fullscreen() {
raylib.ToggleFullscreen();
}

public void toggle_borderless_windowed() {
raylib.ToggleBorderlessWindowed();
}

public void maximize() {
raylib.MaximizeWindow();
}

public void minimize() {
raylib.MinimizeWindow();
}

public void restore() {
raylib.RestoreWindow();
}

public void set_focused() {
raylib.SetWindowFocused();
}
}
12 changes: 7 additions & 5 deletions source/re/ng/diag/debugger.d
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ debug class Debugger {
inspector = new Inspector();
console = new Console();
if (!Core.headless) {
ui_bounds = Rectangle(0, 0, Core.window.width, Core.window.height);
ui_bounds = Rectangle(0, 0, Core.window.render_width, Core.window.render_height);
_render_target = RenderExt.create_render_target(cast(int) ui_bounds.width, cast(int) ui_bounds
.height);
Core.log.info(format("debugger info: ui bounds %s, resolution %s, window (%s,%s)",
ui_bounds, Core.default_resolution, Core.window.width, Core.window.height));
Core.log.info(format("debugger info: ui_bounds=%s, resolution=%s",
ui_bounds, Core.default_resolution));
}
}

Expand Down Expand Up @@ -76,8 +76,10 @@ debug class Debugger {
raylib.EndTextureMode();

// draw render target
RenderExt.draw_render_target(_render_target, Rectangle(0, 0,
Core.window.width, Core.window.height), _render_col);
RenderExt.draw_render_target(_render_target,
Rectangle(0, 0, Core.window.render_width, Core.window.render_height),
_render_col
);
}

/// clean up
Expand Down

0 comments on commit 0d7c03e

Please sign in to comment.