Load all textures & fonts in one place.

GitOrigin-RevId: 076c86b24f4e4e314a52457a01d5c77e197a2fa2
This commit is contained in:
Colin McMillen 2020-02-19 11:19:23 -05:00
parent 8f5514b776
commit 57d15cbbd9
7 changed files with 76 additions and 75 deletions

View File

@ -96,13 +96,13 @@ namespace SemiColinGames {
AddLine(start.ToPoint(), end.ToPoint(), color); AddLine(start.ToPoint(), end.ToPoint(), color);
} }
public static void DrawToasts(SpriteBatch spriteBatch, SpriteFont font) { public static void DrawToasts(SpriteBatch spriteBatch) {
if (!Enabled) { if (!Enabled) {
return; return;
} }
int y = 10; int y = 10;
foreach (var toast in toasts) { foreach (var toast in toasts) {
spriteBatch.DrawString(font, toast, new Vector2(10, y), Color.Teal); spriteBatch.DrawString(Textures.DebugFont, toast, new Vector2(10, y), Color.Teal);
y += 30; y += 30;
} }
} }

View File

@ -18,7 +18,6 @@ namespace SemiColinGames {
private const int spriteWidth = 96; private const int spriteWidth = 96;
private const int spriteHeight = 64; private const int spriteHeight = 64;
private const int spriteCenterYOffset = 1; private const int spriteCenterYOffset = 1;
private readonly Texture2D texture;
// Details of the actual Player model. // Details of the actual Player model.
@ -36,8 +35,7 @@ namespace SemiColinGames {
private const int swordSwingMax = 6; private const int swordSwingMax = 6;
private float ySpeed = 0; private float ySpeed = 0;
public Player(ContentManager content) { public Player() {
this.texture = content.Load<Texture2D>("sprites/ccg/ninja_female");
} }
public int Facing { get; private set; } = 1; public int Facing { get; private set; } = 1;
@ -234,8 +232,8 @@ namespace SemiColinGames {
Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset); Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset);
SpriteEffects effect = Facing == 1 ? SpriteEffects effect = Facing == 1 ?
SpriteEffects.FlipHorizontally : SpriteEffects.None; SpriteEffects.FlipHorizontally : SpriteEffects.None;
spriteBatch.Draw(texture, position.ToVector2(), textureSource, Color.White, 0f, spriteCenter, spriteBatch.Draw(Textures.Player, position.ToVector2(), textureSource, Color.White, 0f,
Vector2.One, effect, 0f); spriteCenter, Vector2.One, effect, 0f);
} }
} }
} }

View File

@ -4,6 +4,8 @@ using Microsoft.Xna.Framework.Graphics;
namespace SemiColinGames { namespace SemiColinGames {
class Scene { class Scene {
public bool Enabled = false;
Color backgroundColor = Color.CornflowerBlue; Color backgroundColor = Color.CornflowerBlue;
readonly GraphicsDevice graphics; readonly GraphicsDevice graphics;
@ -14,12 +16,8 @@ namespace SemiColinGames {
readonly BasicEffect lightingEffect; readonly BasicEffect lightingEffect;
readonly SpriteBatch spriteBatch; readonly SpriteBatch spriteBatch;
readonly SpriteFont font;
readonly Texture2D grasslandBg1;
readonly Texture2D grasslandBg2;
public Scene(ContentManager content, GraphicsDevice graphics, Camera camera) { public Scene(GraphicsDevice graphics, Camera camera) {
Enabled = false;
this.graphics = graphics; this.graphics = graphics;
this.camera = camera; this.camera = camera;
@ -35,15 +33,9 @@ namespace SemiColinGames {
lightingEffect.View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up); lightingEffect.View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up);
lightingEffect.VertexColorEnabled = true; lightingEffect.VertexColorEnabled = true;
// TODO: handle unloading of resources when the level is done.
spriteBatch = new SpriteBatch(graphics); spriteBatch = new SpriteBatch(graphics);
font = content.Load<SpriteFont>("font");
grasslandBg1 = content.Load<Texture2D>("backgrounds/anokolisa/grassland_bg1");
grasslandBg2 = content.Load<Texture2D>("backgrounds/anokolisa/grassland_bg2");
} }
public bool Enabled { get; set; }
public void Draw(World world, Player player, LinesOfSight linesOfSight) { public void Draw(World world, Player player, LinesOfSight linesOfSight) {
graphics.SetRenderTarget(null); graphics.SetRenderTarget(null);
graphics.Clear(backgroundColor); graphics.Clear(backgroundColor);
@ -62,10 +54,10 @@ namespace SemiColinGames {
Rectangle bgSource = new Rectangle( Rectangle bgSource = new Rectangle(
(int) (camera.Left * 0.25), 0, camera.Width, camera.Height); (int) (camera.Left * 0.25), 0, camera.Width, camera.Height);
Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height); Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, bgBlend); spriteBatch.Draw(Textures.Background2, bgTarget, bgSource, bgBlend);
bgSource = new Rectangle( bgSource = new Rectangle(
(int) (camera.Left * 0.5), 0, camera.Width, camera.Height); (int) (camera.Left * 0.5), 0, camera.Width, camera.Height);
spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, bgBlend); spriteBatch.Draw(Textures.Background1, bgTarget, bgSource, bgBlend);
spriteBatch.End(); spriteBatch.End();
// Set up transformation matrix for drawing world objects. // Set up transformation matrix for drawing world objects.
@ -102,7 +94,7 @@ namespace SemiColinGames {
spriteBatch.Draw(lightingTarget, drawRect, Color.White); spriteBatch.Draw(lightingTarget, drawRect, Color.White);
// Draw debug toasts. // Draw debug toasts.
Debug.DrawToasts(spriteBatch, font); Debug.DrawToasts(spriteBatch);
spriteBatch.End(); spriteBatch.End();
} }

View File

@ -10,6 +10,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Camera.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Camera.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Textures.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Clock.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Clock.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Geometry.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Geometry.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Input.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Input.cs" />

View File

@ -63,6 +63,7 @@ namespace SemiColinGames {
// Called once per game. Loads all game content. // Called once per game. Loads all game content.
protected override void LoadContent() { protected override void LoadContent() {
base.LoadContent(); base.LoadContent();
Textures.Load(Content);
linesOfSight = new LinesOfSight(GraphicsDevice); linesOfSight = new LinesOfSight(GraphicsDevice);
LoadLevel(); LoadLevel();
} }
@ -70,9 +71,9 @@ namespace SemiColinGames {
private void LoadLevel() { private void LoadLevel() {
framesToSuppress = 2; framesToSuppress = 2;
camera = new Camera(); camera = new Camera();
player = new Player(Content); player = new Player();
world = new World(Content, Levels.ONE_ONE); world = new World(Levels.ONE_ONE);
scene = new Scene(Content, GraphicsDevice, camera); scene = new Scene(GraphicsDevice, camera);
} }
// Called once per game. Unloads all game content. // Called once per game. Unloads all game content.

45
Shared/Textures.cs Normal file
View File

@ -0,0 +1,45 @@
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace SemiColinGames {
class Textures {
public static Texture2D Player;
public static Texture2D Background1;
public static Texture2D Background2;
public static Texture2D Cemetery;
public static Texture2D Crypt;
public static Texture2D Dungeon;
public static Texture2D Forest;
public static Texture2D Garden;
public static Texture2D Grassland;
public static Texture2D Ruins;
public static Texture2D Sewer;
public static Texture2D Temple;
public static Texture2D Village;
public static SpriteFont DebugFont;
public static void Load(ContentManager content) {
Player = content.Load<Texture2D>("sprites/ccg/ninja_female");
Background1 = content.Load<Texture2D>("backgrounds/anokolisa/grassland_bg1");
Background2 = content.Load<Texture2D>("backgrounds/anokolisa/grassland_bg2");
Cemetery = content.Load<Texture2D>("tiles/anokolisa/cemetery");
Crypt = content.Load<Texture2D>("tiles/anokolisa/crypt");
Dungeon = content.Load<Texture2D>("tiles/anokolisa/dungeon");
Forest = content.Load<Texture2D>("tiles/anokolisa/forest");
Garden = content.Load<Texture2D>("tiles/anokolisa/garden");
Grassland = content.Load<Texture2D>("tiles/anokolisa/grassland");
Ruins = content.Load<Texture2D>("tiles/anokolisa/ruins");
Sewer = content.Load<Texture2D>("tiles/anokolisa/sewer");
Temple = content.Load<Texture2D>("tiles/anokolisa/temple");
Village = content.Load<Texture2D>("tiles/anokolisa/village");
DebugFont = content.Load<SpriteFont>("font");
}
}
}

View File

@ -1,5 +1,4 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -17,19 +16,6 @@ namespace SemiColinGames {
Block Block
} }
enum TileSet {
Cemetery,
Crypt,
Dungeon,
Forest,
Garden,
Grassland,
Ruins,
Sewer,
Temple,
Village,
}
class TileFactory { class TileFactory {
struct TextureSource { struct TextureSource {
@ -42,40 +28,19 @@ namespace SemiColinGames {
} }
} }
static readonly Dictionary<TileSet, string> tileSetToContentPath = readonly Dictionary<Terrain, TextureSource> terrainToTexture;
new Dictionary<TileSet, string>() {
{ TileSet.Cemetery, "tiles/anokolisa/cemetery" }, public TileFactory() {
{ TileSet.Crypt, "tiles/anokolisa/crypt" }, terrainToTexture = new Dictionary<Terrain, TextureSource>() {
{ TileSet.Dungeon, "tiles/anokolisa/dungeon" }, { Terrain.GrassL, GetTextureSource(Textures.Grassland, 2, 0) },
{ TileSet.Forest, "tiles/anokolisa/forest" }, { Terrain.Grass, GetTextureSource(Textures.Grassland, 3, 0) },
{ TileSet.Garden, "tiles/anokolisa/garden" }, { Terrain.GrassR, GetTextureSource(Textures.Grassland, 4, 0) },
{ TileSet.Grassland, "tiles/anokolisa/grassland" }, { Terrain.Rock, GetTextureSource(Textures.Grassland, 3, 1) },
{ TileSet.Ruins, "tiles/anokolisa/ruins" }, { Terrain.RockL, GetTextureSource(Textures.Grassland, 1, 2) },
{ TileSet.Sewer, "tiles/anokolisa/sewer" }, { Terrain.RockR, GetTextureSource(Textures.Grassland, 5, 2) },
{ TileSet.Temple, "tiles/anokolisa/temple" }, { Terrain.Water, GetTextureSource(Textures.Grassland, 9, 2) },
{ TileSet.Village, "tiles/anokolisa/village" }, { Terrain.Block, GetTextureSource(Textures.Grassland, 6, 3) },
}; };
readonly Dictionary<Terrain, TextureSource> terrainToTexture =
new Dictionary<Terrain, TextureSource>();
readonly Texture2D[] textures;
public TileFactory(ContentManager content) {
Array tileSets = Enum.GetValues(typeof(TileSet));
textures = new Texture2D[tileSets.Length];
foreach (TileSet tileSet in tileSets) {
textures[(int) tileSet] = content.Load<Texture2D>(tileSetToContentPath[tileSet]);
}
terrainToTexture[Terrain.Grass] = GetTextureSource(TileSet.Grassland, 3, 0);
terrainToTexture[Terrain.GrassL] = GetTextureSource(TileSet.Grassland, 2, 0);
terrainToTexture[Terrain.GrassR] = GetTextureSource(TileSet.Grassland, 4, 0);
terrainToTexture[Terrain.Rock] = GetTextureSource(TileSet.Grassland, 3, 1);
terrainToTexture[Terrain.RockL] = GetTextureSource(TileSet.Grassland, 1, 2);
terrainToTexture[Terrain.RockR] = GetTextureSource(TileSet.Grassland, 5, 2);
terrainToTexture[Terrain.Water] = GetTextureSource(TileSet.Grassland, 9, 2);
terrainToTexture[Terrain.Block] = GetTextureSource(TileSet.Grassland, 6, 3);
} }
public Tile MakeTile(Terrain terrain, Rectangle position) { public Tile MakeTile(Terrain terrain, Rectangle position) {
@ -83,8 +48,7 @@ namespace SemiColinGames {
return new Tile(terrain, position, textureSource.texture, textureSource.source); return new Tile(terrain, position, textureSource.texture, textureSource.source);
} }
private TextureSource GetTextureSource(TileSet tileSet, int x, int y) { private TextureSource GetTextureSource(Texture2D texture, int x, int y) {
Texture2D texture = textures[(int) tileSet];
int size = World.TileSize; int size = World.TileSize;
Rectangle position = new Rectangle(x * size, y * size, size, size); Rectangle position = new Rectangle(x * size, y * size, size, size);
return new TextureSource(texture, position); return new TextureSource(texture, position);
@ -140,8 +104,8 @@ namespace SemiColinGames {
{ 'X', Terrain.Block } { 'X', Terrain.Block }
}; };
public World(ContentManager content, string levelSpecification) { public World(string levelSpecification) {
TileFactory factory = new TileFactory(content); TileFactory factory = new TileFactory();
var tilesList = new List<Tile>(); var tilesList = new List<Tile>();
string[] worldDesc = levelSpecification.Split('\n'); string[] worldDesc = levelSpecification.Split('\n');
tileWidth = worldDesc.AsQueryable().Max(a => a.Length); tileWidth = worldDesc.AsQueryable().Max(a => a.Length);