From b06155aa886ac1917c0cd6c1e8a5147e05cfc63c Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Wed, 19 Feb 2020 15:00:29 -0500 Subject: [PATCH] Render backgrounds from back-to-front in an array. GitOrigin-RevId: 1f4ca760d4dc4a73a4f96f6b04ee2929e789a5eb --- Shared/Scene.cs | 34 ++++++++++------------------------ Shared/Textures.cs | 14 ++++++-------- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/Shared/Scene.cs b/Shared/Scene.cs index e81f734..3e70022 100644 --- a/Shared/Scene.cs +++ b/Shared/Scene.cs @@ -44,36 +44,22 @@ namespace SemiColinGames { return; } + // Draw scene to sceneTarget. graphics.SetRenderTarget(sceneTarget); graphics.Clear(backgroundColor); - // Draw scene to sceneTarget. - spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null); - // Draw background. - - // We assume that all backgrounds are the same size; if not, the computation of yOffset needs - // to be computed separately for every background. - int yOffset = Textures.Background1.Height - camera.Height - 64; - Color bgBlend = Color.FromNonPremultiplied(new Vector4(1, 1, 1, 1)); + spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null); Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height); - Rectangle bgSource = new Rectangle( - (int) (camera.Left * 1f / 16), yOffset, camera.Width, camera.Height); - spriteBatch.Draw(Textures.Background1, bgTarget, bgSource, bgBlend); - - bgSource = new Rectangle( - (int) (camera.Left * 1f / 8), yOffset, camera.Width, camera.Height); - spriteBatch.Draw(Textures.Background2, bgTarget, bgSource, bgBlend); - - bgSource = new Rectangle( - (int) (camera.Left * 1f / 4), yOffset, camera.Width, camera.Height); - spriteBatch.Draw(Textures.Background3, bgTarget, bgSource, bgBlend); - - bgSource = new Rectangle( - (int) (camera.Left * 1f / 2), yOffset, camera.Width, camera.Height); - spriteBatch.Draw(Textures.Background4, bgTarget, bgSource, bgBlend); - + float xScale = 1f / 16; + for (int i = 0; i < Textures.Backgrounds.Length; i++) { + int yOffset = Textures.Backgrounds[i].Height - camera.Height - 24; + Rectangle bgSource = new Rectangle( + (int) (camera.Left * xScale), yOffset, camera.Width, camera.Height); + spriteBatch.Draw(Textures.Backgrounds[i], bgTarget, bgSource, Color.White); + xScale *= 2; + } spriteBatch.End(); // Set up transformation matrix for drawing world objects. diff --git a/Shared/Textures.cs b/Shared/Textures.cs index 1241fe1..2392271 100644 --- a/Shared/Textures.cs +++ b/Shared/Textures.cs @@ -6,10 +6,8 @@ namespace SemiColinGames { public static Texture2D Player; - public static Texture2D Background1; - public static Texture2D Background2; - public static Texture2D Background3; - public static Texture2D Background4; + // Backgrounds are indexed by draw order; the first element should be drawn furthest back. + public static Texture2D[] Backgrounds = new Texture2D[4]; public static Texture2D Cemetery; public static Texture2D Crypt; @@ -27,10 +25,10 @@ namespace SemiColinGames { public static void Load(ContentManager content) { Player = content.Load("sprites/ccg/ninja_female"); - Background1 = content.Load("backgrounds/szadiart/pf4/background1_day"); - Background2 = content.Load("backgrounds/szadiart/pf4/background2a_day"); - Background3 = content.Load("backgrounds/szadiart/pf4/background3_day"); - Background4 = content.Load("backgrounds/szadiart/pf4/background4_day"); + Backgrounds[0] = content.Load("backgrounds/szadiart/pf4/background1_day"); + Backgrounds[1] = content.Load("backgrounds/szadiart/pf4/background2a_day"); + Backgrounds[2] = content.Load("backgrounds/szadiart/pf4/background3_day"); + Backgrounds[3] = content.Load("backgrounds/szadiart/pf4/background4_day"); Cemetery = content.Load("tiles/anokolisa/cemetery"); Crypt = content.Load("tiles/anokolisa/crypt");