Skip to content

Instantly share code, notes, and snippets.

@Alan-FGR
Created September 1, 2017 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alan-FGR/838bae004e59a0ecc5925fab4b3cd229 to your computer and use it in GitHub Desktop.
Save Alan-FGR/838bae004e59a0ecc5925fab4b3cd229 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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