Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example showing how to have independent resolution for UI and game in Nez
public class GameScene : Scene, IFinalRenderDelegate
{
public const int UI_RENDER_LAYER = 512;
private readonly Renderer uiRenderer_;
private readonly Renderer gameRenderer_;
private int uiZoom_ = 3;
private int gameZoom_ = 2;
public GameScene()
{
uiRenderer_ = new RenderLayerRenderer( 100, UI_RENDER_LAYER );
uiRenderer_.shouldDebugRender = false;
uiRenderer_.renderTexture = new RenderTexture();
uiRenderer_.renderTargetClearColor = new Color(0,0,0,0);
finalRenderDelegate = this;
gameRenderer_ = new RenderLayerExcludeRenderer( 0, UI_RENDER_LAYER );
gameRenderer_.renderTexture = new RenderTexture();
gameRenderer_.renderTargetClearColor = Color.CornflowerBlue;
gameRenderer_.shouldDebugRender = true;
SetRenderSize(Screen.width, Screen.height);
addRenderer(uiRenderer_);
UICanvas canvas = createEntity( "ui" ).addComponent( new UICanvas() );
canvas.renderLayer = UI_RENDER_LAYER;
var skin = Skin.createDefaultSkin();
var table = canvas.stage.addElement(new Window("window", skin)).setMovable(true).setResizable(true);
table.pad(10).setPosition(10, 10);
var button = new TextButton("click", skin);
table.add(button).size(100,20).setRow();
table.pack();
Physics.gravity = Vector2.Zero;
var p = createEntity("player");
p.addComponent(new PlayerController());
p.position = Vector2.One * 150;
}
public Scene scene { get; set; }
public void onAddedToScene() { }
public void onSceneBackBufferSizeChanged( int newWidth, int newHeight ) { }
private void SetRenderSize(int width, int height)
{
int safeUiW = width / uiZoom_;
int safeUiH = height / uiZoom_;
int safeGameW = width / gameZoom_;
int safeGameH = height / gameZoom_;
setDesignResolution(safeUiW, safeUiH, SceneResolutionPolicy.ExactFit);
uiRenderer_.renderTexture.resize(safeUiW, safeUiH);
gameRenderer_.renderTexture.resize(safeGameW, safeGameH);
}
Point lastResolution_ = Point.Zero;
public void handleFinalRender( Color _, RenderTarget2D __, Rectangle finalRenderDestinationRect, SamplerState samplerState )
{
Point curResolution = new Point(Screen.width, Screen.height);
if (lastResolution_ != curResolution)
SetRenderSize(curResolution.X, curResolution.Y);
lastResolution_ = curResolution;
gameRenderer_.render( scene );
uiRenderer_.render( scene );
Core.graphicsDevice.setRenderTarget( null );
Graphics.instance.batcher.begin( BlendState.Opaque, samplerState, null, null );
Graphics.instance.batcher.draw( gameRenderer_.renderTexture, finalRenderDestinationRect );
Graphics.instance.batcher.end();
Graphics.instance.batcher.begin( BlendState.AlphaBlend, samplerState, DepthStencilState.None, RasterizerState.CullNone, null );
Graphics.instance.batcher.draw( uiRenderer_.renderTexture, finalRenderDestinationRect );
Graphics.instance.batcher.end();
}
}
@Alan-FGR

This comment has been minimized.

Copy link
Owner Author

@Alan-FGR Alan-FGR commented Sep 1, 2017

This has some problems, namely culling and debug drawing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.