Skip to content

Commit

Permalink
improve support for crop region
Browse files Browse the repository at this point in the history
  • Loading branch information
redthing1 committed Jul 16, 2024
1 parent fb96de7 commit 98ee190
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion demo/three/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Game : Core {
}

override void initialize() {
default_resolution = Vector2(WIDTH / 4, HEIGHT / 4);
default_resolution = Vector2(WIDTH, HEIGHT);
content.paths ~= ["../content/", "content/"];

load_scenes([new PlayScene(), new HUDScene()]);
Expand Down
13 changes: 12 additions & 1 deletion demo/three/source/play.d
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,21 @@ class PlayScene : Scene3D {
private PostProcessor glitch_postproc;
private float[2] sample_offset = [0.01, 0];

override void setup() {
super.setup();

default_viewport.resolution = Vector2(160, 120);
resolution = default_viewport.resolution;
}

override void on_start() {
clear_color = Colors.LIGHTGRAY;
cam = default_viewport.cam;

cam = (cast(Viewport3D) viewports[0]).cam;
// // sample of cropping
// default_viewport.crop_bounds = Rectangle(40, 0, resolution.x - 40, resolution.y);
// default_viewport.output_bounds = default_viewport.crop_bounds.scale(
// Core.window.screen_width / resolution.x);

// load a shader effect and add it as a postprocessor
auto chrm_abr = new Effect(Core.content.load_shader(null,
Expand Down
2 changes: 1 addition & 1 deletion source/re/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ abstract class Core {

foreach (viewport; scene.viewports) {
RenderExt.draw_render_target_crop(
viewport.render_target, viewport.crop_region, viewport.output_bounds, scene.composite_mode.color
viewport.render_target, viewport.crop_bounds, viewport.output_bounds, scene.composite_mode.color
);
}

Expand Down
4 changes: 4 additions & 0 deletions source/re/gfx/render_ext.d
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ static class RenderExt {
}
// negate height to ensure orientation is correct
source_rect.height = -source_rect.height;
// import std.stdio;
// writefln("target texture size: (%s, %s)", target.texture.width, target.texture.height);
// writefln("source rect: (%s, %s, %s, %s)", source_rect.x, source_rect.y, source_rect.width, source_rect.height);
// writefln("dest rect: (%s, %s, %s, %s)", dest_rect.x, dest_rect.y, dest_rect.width, dest_rect.height);
raylib.DrawTexturePro(target.texture, source_rect, dest_rect, Vector2(0, 0), 0, color);
}

Expand Down
16 changes: 16 additions & 0 deletions source/re/math/rectangle_ext.d
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,21 @@ float bottom(Rectangle r) {
return r.y + r.height;
}

Rectangle scale(Rectangle r, float factor) {
return Rectangle(r.x * factor, r.y * factor, r.width * factor, r.height * factor);
}

Rectangle scale(Rectangle r, Vector2 factor) {
return Rectangle(r.x * factor.x, r.y * factor.y, r.width * factor.x, r.height * factor.y);
}

Rectangle scale_inplace(Rectangle r, float factor) {
return Rectangle(r.x, r.y, r.width * factor, r.height * factor);
}

Rectangle scale_inplace(Rectangle r, Vector2 factor) {
return Rectangle(r.x, r.y, r.width * factor.x, r.height * factor.y);
}

enum RectangleZero = Rectangle(0, 0, 0, 0);
enum RectangleUnit = Rectangle(0, 0, 1, 1);
1 change: 1 addition & 0 deletions source/re/ng/scene.d
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ abstract class Scene {
}

// create render target
Core.log.info(format("recreating render target of size %s", viewport.resolution));
viewport.render_target = RenderExt.create_render_target(
cast(int) viewport.resolution.x, cast(int) viewport.resolution.y
);
Expand Down
2 changes: 1 addition & 1 deletion source/re/ng/viewport.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract class Viewport {
/// the render target's output bounds: the area of the screen it renders to
public Rectangle output_bounds;
/// the render target's crop region: the area of the render target that is rendered to the output bounds
public Rectangle crop_region = RectangleZero; // default to full render target
public Rectangle crop_bounds = RectangleZero; // default to full render target
/// the render target's resolution
public Vector2 resolution;

Expand Down

0 comments on commit 98ee190

Please sign in to comment.