Skip to content

Commit 203e8c9

Browse files
committed
Background Images Working
1 parent 2767974 commit 203e8c9

File tree

12 files changed

+124
-68
lines changed

12 files changed

+124
-68
lines changed

LDtkMonogame.Examples/Icon.ico

102 KB
Binary file not shown.

LDtkMonogame.Examples/LDtkMonogame.Examples.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
4+
<OutputType>WinExe</OutputType>
55
<TargetFramework>net5.0</TargetFramework>
6+
<ApplicationIcon>Icon.ico</ApplicationIcon>
67
</PropertyGroup>
78

89
<ItemGroup>

LDtkMonogame.Examples/LDtkExample.cs renamed to LDtkMonogame.Examples/Source/LDtkExample.cs

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ public class LDtkExample : Game
1919
private Vector3 cameraOrigin;
2020
private float cameraZoom = 1f;
2121

22-
private readonly int currentLevel = 1;
23-
private readonly bool[] activeLayers = { true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, };
22+
private int currentLevel = 3;
2423

25-
KeyboardState oldKeyboardState;
24+
KeyboardState oldKeyboard;
2625

26+
string filename = "SeparateLevelFiles";
2727
public LDtkExample()
2828
{
2929
graphics = new GraphicsDeviceManager(this);
@@ -34,7 +34,7 @@ protected override void Initialize()
3434
{
3535
MonogameInitialize();
3636

37-
projectFile = new Project(spriteBatch, "samples/Test_file_for_API_showing_all_features.ldtk");
37+
projectFile = new Project(spriteBatch, "samples/" + filename + ".ldtk");
3838
projectFile.Render(currentLevel);
3939

4040
base.Initialize();
@@ -43,7 +43,7 @@ protected override void Initialize()
4343
private void OnWindowResized()
4444
{
4545
cameraOrigin = new Vector3(GraphicsDevice.Viewport.Width / 2f, GraphicsDevice.Viewport.Height / 2f, 0);
46-
cameraZoom = MathF.Max(1, GraphicsDevice.Viewport.Height / 240);
46+
cameraZoom = 2;
4747
}
4848

4949
private void MonogameInitialize()
@@ -53,6 +53,8 @@ private void MonogameInitialize()
5353
spriteBatch = new SpriteBatch(GraphicsDevice);
5454
IsFixedTimeStep = false;
5555

56+
Window.Title = filename;
57+
5658
TargetElapsedTime = TimeSpan.FromSeconds(1d / 60d);
5759

5860
graphics.ApplyChanges();
@@ -77,37 +79,27 @@ protected override void Update(GameTime gameTime)
7779
cameraZoom = 1;
7880
}
7981

80-
if(keyboard.IsKeyDown(Keys.E))
81-
{
82-
cameraZoom += (float)deltaTime;
83-
}
84-
85-
if(keyboard.IsKeyDown(Keys.Q))
86-
{
87-
cameraZoom -= (float)deltaTime;
88-
}
89-
90-
if(keyboard.IsKeyDown(Keys.D1) == false && oldKeyboardState.IsKeyDown(Keys.D1) == true)
91-
{
92-
activeLayers[0] = !activeLayers[0];
93-
}
94-
95-
if(keyboard.IsKeyDown(Keys.D2) == false && oldKeyboardState.IsKeyDown(Keys.D2) == true)
82+
if(keyboard.IsKeyDown(Keys.E) == false && oldKeyboard.IsKeyDown(Keys.E))
9683
{
97-
activeLayers[1] = !activeLayers[1];
98-
}
99-
100-
if(keyboard.IsKeyDown(Keys.D3) == false && oldKeyboardState.IsKeyDown(Keys.D3) == true)
101-
{
102-
activeLayers[2] = !activeLayers[2];
84+
currentLevel++;
85+
if(currentLevel >= projectFile.Levels.Length)
86+
{
87+
currentLevel = 0;
88+
}
89+
projectFile.Render(currentLevel);
10390
}
10491

105-
if(keyboard.IsKeyDown(Keys.D4) == false && oldKeyboardState.IsKeyDown(Keys.D4) == true)
92+
if(keyboard.IsKeyDown(Keys.Q) == false && oldKeyboard.IsKeyDown(Keys.Q))
10693
{
107-
activeLayers[3] = !activeLayers[3];
94+
currentLevel--;
95+
if(currentLevel < 0)
96+
{
97+
currentLevel = projectFile.Levels.Length - 1;
98+
}
99+
projectFile.Render(currentLevel);
108100
}
109101

110-
oldKeyboardState = keyboard;
102+
oldKeyboard = keyboard;
111103

112104
base.Update(gameTime);
113105
}
@@ -119,12 +111,14 @@ protected override void Draw(GameTime gameTime)
119111

120112
spriteBatch.Begin(samplerState: SamplerState.PointClamp, transformMatrix: Matrix.CreateTranslation(cameraPosition) * Matrix.CreateScale(cameraZoom) * Matrix.CreateTranslation(cameraOrigin));
121113
{
122-
for(int i = level.layers.Length - 1; i >= 0; i--)
114+
if(level.Background != null)
115+
{
116+
spriteBatch.Draw(level.Background.Image, level.Background.TopLeft, level.Background.CropRect, Color.White, 0, Vector2.Zero, level.Background.Scale, SpriteEffects.None, 0);
117+
}
118+
119+
for(int i = level.Layers.Length - 1; i >= 0; i--)
123120
{
124-
if(activeLayers[i] == true)
125-
{
126-
spriteBatch.Draw(level.layers[i], Vector2.Zero, Color.White);
127-
}
121+
spriteBatch.Draw(level.Layers[i], Vector2.Zero, Color.White);
128122
}
129123
}
130124
spriteBatch.End();
File renamed without changes.

LDtkMonogame.Examples/samples/Test_file_for_API_showing_all_features.ldtk

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
"doc": "https://ldtk.io/json",
66
"schema": "https://ldtk.io/files/JSON_SCHEMA.json",
77
"appAuthor": "Sebastien 'deepnight' Benard",
8-
"appVersion": "0.7.0",
8+
"appVersion": "0.7.1",
99
"url": "https://ldtk.io"
1010
},
11-
"jsonVersion": "0.7.0",
11+
"jsonVersion": "0.7.1",
1212
"defaultPivotX": 0.5,
1313
"defaultPivotY": 0.5,
1414
"defaultGridSize": 16,
@@ -640,6 +640,8 @@
640640
"pxOffsetX": 0,
641641
"pxOffsetY": 0,
642642
"intGrid": [
643+
{ "coordId": 0, "v": 0 },
644+
{ "coordId": 84, "v": 0 },
643645
{ "coordId": 1260, "v": 0 },
644646
{ "coordId": 1262, "v": 0 },
645647
{ "coordId": 1264, "v": 0 },
@@ -711,7 +713,9 @@
711713
{ "coordId": 2198, "v": 0 },
712714
{ "coordId": 2200, "v": 0 },
713715
{ "coordId": 2202, "v": 0 },
714-
{ "coordId": 2204, "v": 0 }
716+
{ "coordId": 2204, "v": 0 },
717+
{ "coordId": 4675, "v": 0 },
718+
{ "coordId": 4759, "v": 0 }
715719
],
716720
"autoLayerTiles": [],
717721
"seed": 7676281,
@@ -2131,10 +2135,10 @@
21312135
"__bgColor": "#373852",
21322136
"bgColor": null,
21332137
"bgRelPath": "Inca_back2_by_Kronbits.png",
2134-
"bgPos": "Contain",
2138+
"bgPos": "Cover",
21352139
"bgPivotX": 0.5,
21362140
"bgPivotY": 0.5,
2137-
"__bgPos": { "topLeftPx": [36,0], "scale": [1.1428571428571428,1.1428571428571428], "cropRect": [0,0,160,224] },
2141+
"__bgPos": { "topLeftPx": [0,0], "scale": [1.6,1.6], "cropRect": [0,32,160,160] },
21382142
"externalRelPath": null,
21392143
"layerInstances": [
21402144
{
@@ -2152,7 +2156,7 @@
21522156
"layerDefUid": 34,
21532157
"pxOffsetX": 0,
21542158
"pxOffsetY": 0,
2155-
"intGrid": [],
2159+
"intGrid": [ { "coordId": 0, "v": 0 }, { "coordId": 31, "v": 0 }, { "coordId": 992, "v": 0 }, { "coordId": 1023, "v": 0 } ],
21562160
"autoLayerTiles": [],
21572161
"seed": 3866698,
21582162
"gridTiles": [],

LDtkMonogame/Level.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

LDtkMonogame/Source/Level.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Microsoft.Xna.Framework;
2+
using Microsoft.Xna.Framework.Graphics;
3+
4+
namespace LDtk
5+
{
6+
public struct Level
7+
{
8+
/// <summary>
9+
/// The clear color for the level
10+
/// </summary>
11+
public Color BgColor { get; internal set; }
12+
13+
/// <summary>
14+
/// The background image related fields
15+
/// </summary>
16+
public Background Background { get; internal set; }
17+
18+
/// <summary>
19+
/// Prerendered layer textures created from <see cref="Project.Render(int)"/>
20+
/// </summary>
21+
public RenderTarget2D[] Layers { get; internal set; }
22+
}
23+
24+
/// <summary>
25+
/// The background image class
26+
/// </summary>
27+
public class Background
28+
{
29+
/// <summary>
30+
/// The croped texture if your settings caused it to
31+
/// clip out side the level
32+
/// </summary>
33+
public Vector2 TopLeft { get; internal set; }
34+
35+
/// <summary>
36+
/// The croped texture if your settings caused it to
37+
/// clip out side the level
38+
/// </summary>
39+
public Vector2 Scale { get; internal set; }
40+
41+
/// <summary>
42+
/// Levels Background Image
43+
/// </summary>
44+
public Texture2D Image { get; internal set; }
45+
46+
/// <summary>
47+
/// The croped texture if your settings caused it to
48+
/// clip out side the level
49+
/// </summary>
50+
public Rectangle CropRect { get; internal set; }
51+
}
52+
}

LDtkMonogame/Project.cs renamed to LDtkMonogame/Source/Project.cs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,41 @@ public Project(SpriteBatch spriteBatch, string ldtkFile)
4545
/// <param name="levelId">The id of the level</param>
4646
public void Render(int levelId)
4747
{
48+
GraphicsDevice GraphicsDevice = spriteBatch.GraphicsDevice;
49+
50+
51+
4852
// Cache the Background Color
4953
Levels[levelId].BgColor = Utility.ConvertStringToColor(json.Levels[levelId].BgColor);
5054

55+
// The current Level has a background
56+
if(json.Levels[levelId].BgRelPath != null)
57+
{
58+
Levels[levelId].Background = new Background();
59+
60+
Levels[levelId].Background.Image = Texture2D.FromFile(GraphicsDevice, Path.Combine(absoluteProjectFolder, json.Levels[levelId].BgRelPath));
61+
62+
long[] topleft = json.Levels[levelId].BgPos.TopLeftPx;
63+
Levels[levelId].Background.TopLeft = new Vector2(topleft[0], topleft[1]);
64+
65+
double[] rect = json.Levels[levelId].BgPos.CropRect;
66+
Levels[levelId].Background.CropRect = new Rectangle((int)rect[0], (int)rect[1], (int)rect[2], (int)rect[3]);
67+
68+
var scale = json.Levels[levelId].BgPos.Scale;
69+
Levels[levelId].Background.Scale = new Vector2((float)scale[0], (float)scale[1]);
70+
}
71+
5172
LayerInstance[] jsonLayerInstances = json.Levels[levelId].LayerInstances;
52-
Levels[levelId].layers = new RenderTarget2D[jsonLayerInstances.Length];
73+
Levels[levelId].Layers = new RenderTarget2D[jsonLayerInstances.Length];
5374

54-
GraphicsDevice GraphicsDevice = spriteBatch.GraphicsDevice;
5575

5676
for(int i = jsonLayerInstances.Length - 1; i >= 0; i--)
5777
{
5878
LayerInstance jsonLayer = jsonLayerInstances[i];
5979

60-
Levels[levelId].layers[i] = new RenderTarget2D(GraphicsDevice, (int)(jsonLayer.CWid * jsonLayer.GridSize), (int)(jsonLayer.CHei * jsonLayer.GridSize), false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
80+
Levels[levelId].Layers[i] = new RenderTarget2D(GraphicsDevice, (int)(jsonLayer.CWid * jsonLayer.GridSize), (int)(jsonLayer.CHei * jsonLayer.GridSize), false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
6181

62-
GraphicsDevice.SetRenderTarget(Levels[levelId].layers[i]);
82+
GraphicsDevice.SetRenderTarget(Levels[levelId].Layers[i]);
6383
Texture2D texture;
6484

6585
if(jsonLayer.TilesetRelPath != null)
@@ -68,6 +88,7 @@ public void Render(int levelId)
6888
}
6989
else
7090
{
91+
// Create single pixel texture
7192
texture = new Texture2D(GraphicsDevice, 1, 1);
7293
texture.SetData(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF });
7394
}
@@ -108,7 +129,6 @@ public void Render(int levelId)
108129
}
109130
else
110131
{
111-
112132
LayerDefinition layerInstance = GetLayerDefinitionFromUid(jsonLayer.LayerDefUid);
113133

114134
foreach(IntGridValueInstance tile in jsonLayer.IntGrid)
@@ -127,12 +147,12 @@ public void Render(int levelId)
127147
case Const.LayerTypeEntities:
128148
foreach(EntityInstance entity in jsonLayer.EntityInstances)
129149
{
130-
EntityDefinition def = GetEntityDefinitionFromUid(entity.DefUid);
131-
Vector2 position = new Vector2((int)(entity.Px[0] + jsonLayer.PxTotalOffsetX), (int)(entity.Px[1] + jsonLayer.PxTotalOffsetY));
132-
Vector2 size = new Vector2(def.Width, def.Height);
133-
Color color = Utility.ConvertStringToColor(def.Color);
134-
135-
spriteBatch.Draw(texture, position, null, color, 0, Vector2.Zero, size, SpriteEffects.None, 0);
150+
//EntityDefinition def = GetEntityDefinitionFromUid(entity.DefUid);
151+
//Vector2 position = new Vector2((int)(entity.Px[0] + jsonLayer.PxTotalOffsetX), (int)(entity.Px[1] + jsonLayer.PxTotalOffsetY));
152+
//Vector2 size = new Vector2(def.Width, def.Height);
153+
//Color color = Utility.ConvertStringToColor(def.Color);
154+
//
155+
//spriteBatch.Draw(texture, position, null, color, 0, Vector2.Zero, size, SpriteEffects.None, 0);
136156
}
137157
break;
138158

0 commit comments

Comments
 (0)