Skip to content

Commit 256ff8f

Browse files
committed
Int Grid rendering and Example project created
1 parent 8d0c6b7 commit 256ff8f

37 files changed

+50253
-231
lines changed

Example/LDtkExample.cs

Lines changed: 73 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using LDtk;
1+
using System;
2+
3+
using LDtk;
24

35
using Microsoft.Xna.Framework;
46
using Microsoft.Xna.Framework.Graphics;
@@ -8,66 +10,66 @@ namespace Example
810
{
911
public class LDtkExample : Game
1012
{
11-
GraphicsDeviceManager graphics;
12-
SpriteBatch spriteBatch;
13-
Project projectFile;
13+
private readonly GraphicsDeviceManager graphics;
14+
private SpriteBatch spriteBatch;
15+
private Project projectFile;
1416

15-
Vector3 cameraPosition;
16-
Vector3 cameraOrigin;
17-
float cameraZoom = 1f;
17+
// Camera
18+
private Vector3 cameraPosition;
19+
private Vector3 cameraOrigin;
20+
private float cameraZoom = 1f;
21+
private readonly int currentLevel = 1;
22+
private readonly bool[] activeLayers = { true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, };
1823

19-
int currentLevel = 0;
24+
KeyboardState oldKeyboardState;
2025

2126
public LDtkExample()
2227
{
2328
graphics = new GraphicsDeviceManager(this);
29+
IsFixedTimeStep = false;
2430
}
2531

2632
protected override void Initialize()
2733
{
28-
MonogameStuff();
29-
30-
projectFile = new Project("Data/AutoLayers_4_Advanced.ldtk");
31-
projectFile.Render(spriteBatch, 0);
34+
MonogameInitialize();
3235

33-
Window.ClientSizeChanged += (o, e) => cameraOrigin = new Vector3(GraphicsDevice.Viewport.Width / 2f, GraphicsDevice.Viewport.Height / 2f, 0);
34-
cameraOrigin = new Vector3(GraphicsDevice.Viewport.Width / 2f, GraphicsDevice.Viewport.Height / 2f, 0);
36+
projectFile = new Project(spriteBatch, "samples/Test_file_for_API_showing_all_features.ldtk");
37+
projectFile.Render(currentLevel);
3538

3639
base.Initialize();
3740
}
3841

39-
void MonogameStuff()
42+
private void OnWindowResized()
43+
{
44+
cameraOrigin = new Vector3(GraphicsDevice.Viewport.Width / 2f, GraphicsDevice.Viewport.Height / 2f, 0);
45+
cameraZoom = MathF.Max(1, GraphicsDevice.Viewport.Height / 160);
46+
}
47+
48+
private void MonogameInitialize()
4049
{
4150
Window.AllowUserResizing = true;
4251
IsMouseVisible = true;
4352
spriteBatch = new SpriteBatch(GraphicsDevice);
44-
}
53+
IsFixedTimeStep = true;
4554

46-
protected override void Draw(GameTime gameTime)
47-
{
48-
Level level = projectFile.Levels[currentLevel];
49-
GraphicsDevice.Clear(level.BgColor);
55+
TargetElapsedTime = TimeSpan.FromSeconds(1d / 60d);
5056

51-
spriteBatch.Begin(samplerState: SamplerState.PointClamp, transformMatrix: Matrix.CreateTranslation(cameraPosition) * Matrix.CreateScale(cameraZoom) * Matrix.CreateTranslation(cameraOrigin));
52-
{
53-
for(int i = 0; i < level.layers.Length; i++)
54-
{
55-
spriteBatch.Draw(level.layers[i], Vector2.Zero, Color.White);
56-
}
57-
}
58-
spriteBatch.End();
57+
graphics.ApplyChanges();
5958

60-
base.Draw(gameTime);
59+
Window.ClientSizeChanged += (o, e) => OnWindowResized();
60+
OnWindowResized();
6161
}
6262

6363
protected override void Update(GameTime gameTime)
6464
{
65-
var keyboard = Keyboard.GetState();
65+
double deltaTime = gameTime.ElapsedGameTime.TotalSeconds;
66+
67+
KeyboardState keyboard = Keyboard.GetState();
6668

6769
float h = (keyboard.IsKeyDown(Keys.D) ? -1f : 0f) + (keyboard.IsKeyDown(Keys.A) ? 1f : 0f);
6870
float v = (keyboard.IsKeyDown(Keys.S) ? -1f : 0f) + (keyboard.IsKeyDown(Keys.W) ? 1f : 0f);
6971

70-
cameraPosition += new Vector3(h, v, 0) * 50f * (float)gameTime.ElapsedGameTime.TotalSeconds;
72+
cameraPosition += new Vector3(h, v, 0) * 100 * (float)deltaTime;
7173

7274
if(keyboard.IsKeyDown(Keys.R))
7375
{
@@ -84,7 +86,46 @@ protected override void Update(GameTime gameTime)
8486
cameraZoom -= (float)gameTime.ElapsedGameTime.TotalSeconds;
8587
}
8688

89+
if(keyboard.IsKeyDown(Keys.D1) == false && oldKeyboardState.IsKeyDown(Keys.D1) == true)
90+
{
91+
activeLayers[0] = !activeLayers[0];
92+
}
93+
if(keyboard.IsKeyDown(Keys.D2) == false && oldKeyboardState.IsKeyDown(Keys.D2) == true)
94+
{
95+
activeLayers[1] = !activeLayers[1];
96+
}
97+
if(keyboard.IsKeyDown(Keys.D3) == false && oldKeyboardState.IsKeyDown(Keys.D3) == true)
98+
{
99+
activeLayers[2] = !activeLayers[2];
100+
}
101+
if(keyboard.IsKeyDown(Keys.D4) == false && oldKeyboardState.IsKeyDown(Keys.D4) == true)
102+
{
103+
activeLayers[3] = !activeLayers[3];
104+
}
105+
106+
oldKeyboardState = keyboard;
107+
87108
base.Update(gameTime);
88109
}
110+
111+
protected override void Draw(GameTime gameTime)
112+
{
113+
Level level = projectFile.Levels[currentLevel];
114+
GraphicsDevice.Clear(level.BgColor);
115+
116+
spriteBatch.Begin(samplerState: SamplerState.PointClamp, transformMatrix: Matrix.CreateTranslation(cameraPosition) * Matrix.CreateScale(cameraZoom) * Matrix.CreateTranslation(cameraOrigin));
117+
{
118+
for(int i = level.layers.Length - 1; i >= 0; i--)
119+
{
120+
if(activeLayers[i] == true)
121+
{
122+
spriteBatch.Draw(level.layers[i], Vector2.Zero, Color.White);
123+
}
124+
}
125+
}
126+
spriteBatch.End();
127+
128+
base.Draw(gameTime);
129+
}
89130
}
90-
}
131+
}

Example/Program.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using Example;
2-
3-
namespace LDtk
1+
namespace Example
42
{
53
class Program
64
{
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using System;
2+
3+
using LDtk;
4+
5+
using Microsoft.Xna.Framework;
6+
using Microsoft.Xna.Framework.Graphics;
7+
using Microsoft.Xna.Framework.Input;
8+
9+
namespace Example
10+
{
11+
public class LDtkExample : Game
12+
{
13+
private readonly GraphicsDeviceManager graphics;
14+
private SpriteBatch spriteBatch;
15+
private Project projectFile;
16+
17+
// Camera
18+
private Vector3 cameraPosition;
19+
private Vector3 cameraOrigin;
20+
private float cameraZoom = 1f;
21+
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, };
24+
25+
KeyboardState oldKeyboardState;
26+
27+
public LDtkExample()
28+
{
29+
graphics = new GraphicsDeviceManager(this);
30+
IsFixedTimeStep = false;
31+
}
32+
33+
protected override void Initialize()
34+
{
35+
MonogameInitialize();
36+
37+
projectFile = new Project(spriteBatch, "samples/Test_file_for_API_showing_all_features.ldtk");
38+
projectFile.Render(currentLevel);
39+
40+
base.Initialize();
41+
}
42+
43+
private void OnWindowResized()
44+
{
45+
cameraOrigin = new Vector3(GraphicsDevice.Viewport.Width / 2f, GraphicsDevice.Viewport.Height / 2f, 0);
46+
cameraZoom = MathF.Max(1, GraphicsDevice.Viewport.Height / 240);
47+
}
48+
49+
private void MonogameInitialize()
50+
{
51+
Window.AllowUserResizing = true;
52+
IsMouseVisible = true;
53+
spriteBatch = new SpriteBatch(GraphicsDevice);
54+
IsFixedTimeStep = false;
55+
56+
TargetElapsedTime = TimeSpan.FromSeconds(1d / 60d);
57+
58+
graphics.ApplyChanges();
59+
60+
Window.ClientSizeChanged += (o, e) => OnWindowResized();
61+
OnWindowResized();
62+
}
63+
64+
protected override void Update(GameTime gameTime)
65+
{
66+
double deltaTime = gameTime.ElapsedGameTime.TotalSeconds;
67+
68+
KeyboardState keyboard = Keyboard.GetState();
69+
70+
float h = (keyboard.IsKeyDown(Keys.D) ? -1f : 0f) + (keyboard.IsKeyDown(Keys.A) ? 1f : 0f);
71+
float v = (keyboard.IsKeyDown(Keys.S) ? -1f : 0f) + (keyboard.IsKeyDown(Keys.W) ? 1f : 0f);
72+
73+
cameraPosition += new Vector3(h, v, 0) * 100 * (float)deltaTime;
74+
75+
if(keyboard.IsKeyDown(Keys.R))
76+
{
77+
cameraZoom = 1;
78+
}
79+
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)
96+
{
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];
103+
}
104+
105+
if(keyboard.IsKeyDown(Keys.D4) == false && oldKeyboardState.IsKeyDown(Keys.D4) == true)
106+
{
107+
activeLayers[3] = !activeLayers[3];
108+
}
109+
110+
oldKeyboardState = keyboard;
111+
112+
base.Update(gameTime);
113+
}
114+
115+
protected override void Draw(GameTime gameTime)
116+
{
117+
Level level = projectFile.Levels[currentLevel];
118+
GraphicsDevice.Clear(level.BgColor);
119+
120+
spriteBatch.Begin(samplerState: SamplerState.PointClamp, transformMatrix: Matrix.CreateTranslation(cameraPosition) * Matrix.CreateScale(cameraZoom) * Matrix.CreateTranslation(cameraOrigin));
121+
{
122+
for(int i = level.layers.Length - 1; i >= 0; i--)
123+
{
124+
if(activeLayers[i] == true)
125+
{
126+
spriteBatch.Draw(level.layers[i], Vector2.Zero, Color.White);
127+
}
128+
}
129+
}
130+
spriteBatch.End();
131+
132+
base.Draw(gameTime);
133+
}
134+
}
135+
}
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<OutputType>WinExe</OutputType>
4+
<OutputType>Exe</OutputType>
55
<TargetFramework>net5.0</TargetFramework>
6-
<RootNamespace>ldtk</RootNamespace>
76
</PropertyGroup>
87

98
<ItemGroup>
10-
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641" />
11-
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
12-
</ItemGroup>
13-
14-
<ItemGroup>
15-
16-
<None Update="Data\**">
9+
<None Update="samples\**">
1710
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1811
</None>
1912
</ItemGroup>
2013

14+
<ItemGroup>
15+
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641" />
16+
</ItemGroup>
2117

18+
<ItemGroup>
19+
<ProjectReference Include="..\LDtkMonogame\LDtkMonogame.csproj" />
20+
</ItemGroup>
2221

23-
</Project>
22+
</Project>

LDtkMonogame.Examples/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Example
2+
{
3+
class Program
4+
{
5+
static void Main()
6+
{
7+
new LDtkExample().Run();
8+
}
9+
}
10+
}
269 KB
Loading

0 commit comments

Comments
 (0)