Render backgrounds from back-to-front in an array.

GitOrigin-RevId: 1f4ca760d4dc4a73a4f96f6b04ee2929e789a5eb
This commit is contained in:
Colin McMillen 2020-02-19 15:00:29 -05:00
parent cf5167fba7
commit b06155aa88
2 changed files with 16 additions and 32 deletions

View File

@ -44,36 +44,22 @@ namespace SemiColinGames {
return; return;
} }
// Draw scene to sceneTarget.
graphics.SetRenderTarget(sceneTarget); graphics.SetRenderTarget(sceneTarget);
graphics.Clear(backgroundColor); graphics.Clear(backgroundColor);
// Draw scene to sceneTarget.
spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
// Draw background. // Draw background.
spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
// 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));
Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height); Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
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( Rectangle bgSource = new Rectangle(
(int) (camera.Left * 1f / 16), yOffset, camera.Width, camera.Height); (int) (camera.Left * xScale), yOffset, camera.Width, camera.Height);
spriteBatch.Draw(Textures.Background1, bgTarget, bgSource, bgBlend); spriteBatch.Draw(Textures.Backgrounds[i], bgTarget, bgSource, Color.White);
xScale *= 2;
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);
spriteBatch.End(); spriteBatch.End();
// Set up transformation matrix for drawing world objects. // Set up transformation matrix for drawing world objects.

View File

@ -6,10 +6,8 @@ namespace SemiColinGames {
public static Texture2D Player; public static Texture2D Player;
public static Texture2D Background1; // Backgrounds are indexed by draw order; the first element should be drawn furthest back.
public static Texture2D Background2; public static Texture2D[] Backgrounds = new Texture2D[4];
public static Texture2D Background3;
public static Texture2D Background4;
public static Texture2D Cemetery; public static Texture2D Cemetery;
public static Texture2D Crypt; public static Texture2D Crypt;
@ -27,10 +25,10 @@ namespace SemiColinGames {
public static void Load(ContentManager content) { public static void Load(ContentManager content) {
Player = content.Load<Texture2D>("sprites/ccg/ninja_female"); Player = content.Load<Texture2D>("sprites/ccg/ninja_female");
Background1 = content.Load<Texture2D>("backgrounds/szadiart/pf4/background1_day"); Backgrounds[0] = content.Load<Texture2D>("backgrounds/szadiart/pf4/background1_day");
Background2 = content.Load<Texture2D>("backgrounds/szadiart/pf4/background2a_day"); Backgrounds[1] = content.Load<Texture2D>("backgrounds/szadiart/pf4/background2a_day");
Background3 = content.Load<Texture2D>("backgrounds/szadiart/pf4/background3_day"); Backgrounds[2] = content.Load<Texture2D>("backgrounds/szadiart/pf4/background3_day");
Background4 = content.Load<Texture2D>("backgrounds/szadiart/pf4/background4_day"); Backgrounds[3] = content.Load<Texture2D>("backgrounds/szadiart/pf4/background4_day");
Cemetery = content.Load<Texture2D>("tiles/anokolisa/cemetery"); Cemetery = content.Load<Texture2D>("tiles/anokolisa/cemetery");
Crypt = content.Load<Texture2D>("tiles/anokolisa/crypt"); Crypt = content.Load<Texture2D>("tiles/anokolisa/crypt");