1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-29 11:33:37 +02:00

renamed examples to demos and added an auto tiling demo

This commit is contained in:
Ellpeck 2019-08-14 14:15:07 +02:00
parent 5d2f77e95c
commit 75deef068a
10 changed files with 145 additions and 14 deletions

74
Demos/AutoTilingDemo.cs Normal file
View file

@ -0,0 +1,74 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Misc;
using MLEM.Startup;
namespace Demos {
/// <summary>
/// This is a demo for <see cref="AutoTiling"/>.
/// </summary>
public class AutoTilingDemo : MlemGame {
private Texture2D texture;
private string[] layout;
protected override void LoadContent() {
base.LoadContent();
// The layout of the texture is important for auto tiling to work correctly.
// It needs to be laid out as follows: Five tiles aligned horizontally within the texture file, with the following information
// 1. The texture used for filling big areas
// 2. The texture used for straight, horizontal borders, with the borders facing away from the center
// 3. The texture used for outer corners, with the corners facing away from the center
// 4. The texture used for straight, vertical borders, with the borders facing away from the center
// 5. The texture used for inner corners, with the corners facing away from the center
// Note that the AutoTiling.png texture contains an example of this layout
this.texture = LoadContent<Texture2D>("Textures/AutoTiling");
// in this example, a simple string array is used for layout purposes. As the AutoTiling method allows any kind of
// comparison, the actual implementation can vary (for instance, based on a more in-depth tile map)
this.layout = new[] {
"XXX X",
"XXXXX",
"XX ",
"XXXX ",
" X "
};
}
protected override void Draw(GameTime gameTime) {
this.GraphicsDevice.Clear(Color.Black);
// the texture region supplied to the AutoTiling method should only encompass the first filler tile's location and size
const int tileSize = 8;
var region = new Rectangle(0, 0, tileSize, tileSize);
// drawing the auto tiles
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, transformMatrix: Matrix.CreateScale(10));
for (var x = 0; x < 5; x++) {
for (var y = 0; y < 5; y++) {
// don't draw non-grass tiles ( )
if (this.layout[y][x] != 'X')
continue;
var x1 = x;
var y1 = y;
// the connectsTo function determines for any given tile if it should connect to, that is, auto-tile with the
// neighbor in the supplied offset direction. In this example, the layout determines where grass tiles are (X)
// and where there are none ( ).
AutoTiling.ConnectsTo connectsTo = (xOff, yOff) => {
// don't auto-tile out of bounds
if (x1 + xOff < 0 || y1 + yOff < 0 || x1 + xOff >= 5 || y1 + yOff >= 5)
return false;
// check if the neighboring tile is also grass (X)
return this.layout[y1 + yOff][x1 + xOff] == 'X';
};
AutoTiling.DrawAutoTile(this.SpriteBatch, new Vector2(x, y) * tileSize, this.texture, region, connectsTo, Color.White);
}
}
this.SpriteBatch.End();
base.Draw(gameTime);
}
}
}

View file

@ -20,6 +20,18 @@
/processorParam:TextureFormat=Compressed
/build:Fonts/TestFont.spritefont
#begin Textures/AutoTiling.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:Textures/AutoTiling.png
#begin Textures/Test.png
/importer:TextureImporter
/processor:TextureProcessor

Binary file not shown.

After

Width:  |  Height:  |  Size: 604 B

View file

Before

Width:  |  Height:  |  Size: 896 B

After

Width:  |  Height:  |  Size: 896 B

39
Demos/Program.cs Normal file
View file

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
namespace Demos {
public static class Program {
private static readonly Dictionary<string, Func<Game>> Demos = new Dictionary<string, Func<Game>>();
static Program() {
Demos.Add("Ui", () => new UiDemo());
Demos.Add("AutoTiling", () => new AutoTilingDemo());
}
public static void Main(string[] args) {
Func<Game> demoUsed;
if (args.Length <= 0 || !Demos.ContainsKey(args[0])) {
beforeDemo:
Console.WriteLine("Please type the name of the demo you want to see and press Enter.");
Console.WriteLine("The following demos are available: " + string.Join(", ", Demos.Keys));
Console.WriteLine("(Alternatively, you can supply the name of the demo you want to see as the first argument)");
var demo = Console.ReadLine();
if (!Demos.ContainsKey(demo)) {
Console.WriteLine("Not a valid demo.");
goto beforeDemo;
} else {
demoUsed = Demos[demo];
}
} else {
demoUsed = Demos[args[0]];
}
using (var game = demoUsed.Invoke())
game.Run();
}
}
}

View file

@ -11,13 +11,20 @@ using MLEM.Ui;
using MLEM.Ui.Elements;
using MLEM.Ui.Style;
namespace Examples {
public class GameImpl : MlemGame {
namespace Demos {
/// <remarks>
/// NOTE: This ui demo derives from <see cref="MlemGame"/>. To use MLEM.Ui, it's not required to use MLEM.Startup (which MlemGame is a part of).
/// If using your own game class that derives from <see cref="Game"/>, however, you will have to do a few additional things to get MLEM.Ui up and running:
/// - Create an instance of <see cref="UiSystem"/>
/// - Call the instance's Update method in your game's Update method
/// - Call the instance's Draw method in your game's Draw method
/// </remarks>
public class UiDemo : MlemGame {
private Texture2D testTexture;
private NinePatch testPatch;
public GameImpl() {
public UiDemo() {
this.IsMouseVisible = true;
}
@ -26,7 +33,11 @@ namespace Examples {
this.testPatch = new NinePatch(new TextureRegion(this.testTexture, 0, 8, 24, 24), 8);
base.LoadContent();
// create a new style
// this one derives form UntexturedStyle so that stuff like the hover colors don't have to be set again
var style = new UntexturedStyle(this.SpriteBatch) {
// when using a SpriteFont, use GenericSpriteFont. When using a MonoGame.Extended BitmapFont, use GenericBitmapFont.
// Wrapping fonts like this allows for both types to be usable within MLEM.Ui easily
Font = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont")),
TextScale = 0.1F,
PanelTexture = this.testPatch,
@ -40,9 +51,12 @@ namespace Examples {
RadioCheckmark = new TextureRegion(this.testTexture, 32, 0, 8, 8)
};
var untexturedStyle = this.UiSystem.Style;
// set the defined style as the current one
this.UiSystem.Style = style;
// scale every ui up by 5
this.UiSystem.GlobalScale = 5;
// create the root panel that all the other components sit on and add it to the ui system
var root = new Panel(Anchor.Center, new Vector2(80, 100), Vector2.Zero, false, true, new Point(5, 10));
this.UiSystem.Add("Test", root);
@ -62,6 +76,8 @@ namespace Examples {
this.UiSystem.Style = this.UiSystem.Style == untexturedStyle ? style : untexturedStyle;
},
PositionOffset = new Vector2(0, 1),
// set HasCustomStyle to true before changing style information so that, when changing the style globally
// (like above), these custom values don't get undone
HasCustomStyle = true,
Texture = this.testPatch,
HoveredColor = Color.LightGray

View file

@ -1,10 +0,0 @@
namespace Examples {
public static class Program {
public static void Main() {
using (var game = new GameImpl())
game.Run();
}
}
}

View file

@ -6,7 +6,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLEM.Extended", "MLEM.Exten
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLEM.Startup", "MLEM.Startup\MLEM.Startup.csproj", "{997F4739-7BEC-4621-B9CA-68DEB2D74412}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples", "Examples\Examples.csproj", "{1BC4682B-AA14-4937-B5C7-707E20FE88FF}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demos", "Demos\Demos.csproj", "{1BC4682B-AA14-4937-B5C7-707E20FE88FF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLEM.Ui", "MLEM.Ui\MLEM.Ui.csproj", "{6F00629A-8B87-4264-8896-19983285E32F}"
EndProject