Skip to content

Commit

Permalink
Add basic support for title screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeeynamo committed May 23, 2020
1 parent f8e622c commit 7214c29
Show file tree
Hide file tree
Showing 10 changed files with 309 additions and 0 deletions.
10 changes: 10 additions & 0 deletions OpenKh.Game/DataContent/StandardDataContent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.IO;
using OpenKh.Game.Infrastructure;

namespace OpenKh.Game.DataContent
{
public class StandardDataContent : IDataContent
{
public Stream FileOpen(string path) => File.OpenRead(path);
}
}
66 changes: 66 additions & 0 deletions OpenKh.Game/Infrastructure/ArchiveManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using OpenKh.Kh2;
using System;
using System.Collections.Generic;
using System.IO;

namespace OpenKh.Game.Infrastructure
{
public class ArchiveManager
{
private class Entry
{
public Stream Stream { get; set; }
public object Object { get; set; }
}

private readonly Dictionary<Bar.EntryType, Func<Entry, object>> getters = new Dictionary<Bar.EntryType, Func<Entry, object>>
{
[Bar.EntryType.Layout] = entry => Layout.Read(entry.Stream),
[Bar.EntryType.Seqd] = entry => Sequence.Read(entry.Stream),
[Bar.EntryType.Imgd] = entry => Imgd.Read(entry.Stream),
[Bar.EntryType.Imgz] = entry => new Imgz(entry.Stream),
};
private readonly Dictionary<(string name, Bar.EntryType type), Entry> archives;
private readonly IDataContent dataContent;

public ArchiveManager(IDataContent dataContent)
{
archives = new Dictionary<(string name, Bar.EntryType type), Entry>();
this.dataContent = dataContent;
}

public void LoadArchive(string fileName)
{
var stream = dataContent.FileOpen(fileName);
var entries = Bar.Read(stream);

foreach (var entry in entries)
archives[(entry.Name, entry.Type)] = new Entry
{
Stream = entry.Stream
};
}

public T Get<T>(string resourceName)
where T : class
{
if (typeof(T) == typeof(Layout)) return GetItem<T>(resourceName, Bar.EntryType.Layout);
if (typeof(T) == typeof(Sequence)) return GetItem<T>(resourceName, Bar.EntryType.Seqd);
if (typeof(T) == typeof(Imgd)) return GetItem<T>(resourceName, Bar.EntryType.Imgd);
if (typeof(T) == typeof(Imgz)) return GetItem<T>(resourceName, Bar.EntryType.Imgz);
return null;
}

private T GetItem<T>(string resourceName, Bar.EntryType type)
where T : class
{
var entry = archives[(resourceName, type)];
if (entry.Object is T item)
return item;

entry.Stream.Position = 0;
entry.Object = getters[type](entry);
return entry.Object as T;
}
}
}
7 changes: 7 additions & 0 deletions OpenKh.Game/Infrastructure/DeltaTimes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace OpenKh.Game.Infrastructure
{
public class DeltaTimes
{
public double DeltaTime { get; set; }
}
}
9 changes: 9 additions & 0 deletions OpenKh.Game/Infrastructure/IDataContent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.IO;

namespace OpenKh.Game.Infrastructure
{
public interface IDataContent
{
Stream FileOpen(string fileName);
}
}
11 changes: 11 additions & 0 deletions OpenKh.Game/Infrastructure/StateInitDesc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.Xna.Framework;

namespace OpenKh.Game.Infrastructure
{
public class StateInitDesc
{
public IDataContent DataContent { get; set; }
public ArchiveManager ArchiveManager { get; set; }
public GraphicsDeviceManager GraphicsDevice { get; set; }
}
}
117 changes: 117 additions & 0 deletions OpenKh.Game/MonoDrawing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using Microsoft.Xna.Framework.Graphics;
using System.Drawing;
using Xe.Drawing;

namespace OpenKh.Game
{
public class MonoDrawing : IDrawing
{
private readonly SpriteBatch spriteBatch;

private class CSurface : ISurface
{
public CSurface(Texture2D texture)
{
Texture = texture;
}

public Texture2D Texture { get; }
public int Width => Texture.Width;
public int Height => Texture.Height;
public Size Size => new Size(Width, Height);

public PixelFormat PixelFormat => throw new System.NotImplementedException();


public void Dispose() => Texture.Dispose();

public IMappedResource Map()
{
throw new System.NotImplementedException();
}

public void Save(string filename)
{
throw new System.NotImplementedException();
}
}

public MonoDrawing(GraphicsDevice graphicsDevice)
{
GraphicsDevice = graphicsDevice;
spriteBatch = new SpriteBatch(graphicsDevice);
}

public GraphicsDevice GraphicsDevice { get; }

public ISurface Surface { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }
public Filter Filter { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }

public void Clear(Color color)
{
GraphicsDevice.Clear(ToXnaColor(color));
}

public ISurface CreateSurface(int width, int height, PixelFormat pixelFormat, SurfaceType type = SurfaceType.Input, DataResource dataResource = null)
{
var texture = new Texture2D(GraphicsDevice, width, height);
texture.SetData(dataResource.Data);

return new CSurface(texture);
}

public ISurface CreateSurface(string filename, Color[] filterColors = null)
{
throw new System.NotImplementedException();
}

public void Dispose()
{
spriteBatch.Dispose();
}

public void DrawRectangle(RectangleF rect, Color color, float width = 1)
{
throw new System.NotImplementedException();
}

public void FillRectangle(RectangleF rect, Color color)
{
throw new System.NotImplementedException();
}

public void DrawSurface(ISurface surface, Rectangle src, RectangleF dst, Flip flip = Flip.None)
{
throw new System.NotImplementedException();
}

public void DrawSurface(ISurface surface, Rectangle src, RectangleF dst, float alpha, Flip flip = Flip.None)
{
throw new System.NotImplementedException();
}

public void DrawSurface(ISurface surface, Rectangle src, RectangleF dst, ColorF color0, ColorF color1, ColorF color2, ColorF color3)
{
var texture = (surface as CSurface)?.Texture;
spriteBatch.Begin();
spriteBatch.Draw(texture,
sourceRectangle: ToXnaRectangle(src),
destinationRectangle: ToXnaRectangle(dst),
color: ToXnaColor(color0));
spriteBatch.End();
}

public void Flush()
{
}

private static Microsoft.Xna.Framework.Rectangle ToXnaRectangle(Rectangle rectangle) =>
new Microsoft.Xna.Framework.Rectangle(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
private static Microsoft.Xna.Framework.Rectangle ToXnaRectangle(RectangleF rectangle) =>
new Microsoft.Xna.Framework.Rectangle((int)rectangle.X, (int)rectangle.Y, (int)rectangle.Width, (int)rectangle.Height);
private static Microsoft.Xna.Framework.Color ToXnaColor(Color color) =>
new Microsoft.Xna.Framework.Color(color.R, color.G, color.B, color.A);
private static Microsoft.Xna.Framework.Color ToXnaColor(ColorF color) =>
new Microsoft.Xna.Framework.Color(color.R, color.G, color.B, color.A);
}
}
6 changes: 6 additions & 0 deletions OpenKh.Game/OpenKh.Game.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@
<Folder Include="Content\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\OpenKh.Engine\OpenKh.Engine.csproj" />
<ProjectReference Include="..\OpenKh.Kh2\OpenKh.Kh2.csproj" />
<ProjectReference Include="..\XeEngine.Tools.Public\Xe.Drawing\Xe.Drawing.csproj" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions OpenKh.Game/OpenKhGame.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using OpenKh.Game.DataContent;
using OpenKh.Game.Infrastructure;
using OpenKh.Game.States;

namespace OpenKh.Game
{
Expand All @@ -9,17 +12,31 @@ public class OpenKhGame : Microsoft.Xna.Framework.Game
private GraphicsDeviceManager graphics;
private SpriteBatch spriteBatch;

private readonly IDataContent dataContent;
private readonly ArchiveManager archiveManager;
private IState state;

public OpenKhGame()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;

dataContent = new StandardDataContent();
archiveManager = new ArchiveManager(dataContent);
}

protected override void Initialize()
{
// TODO: Add your initialization logic here

state = new TitleState();
state.Initialize(new StateInitDesc
{
DataContent = dataContent,
ArchiveManager = archiveManager,
GraphicsDevice = graphics
});
base.Initialize();
}

Expand All @@ -38,6 +55,10 @@ protected override void Update(GameTime gameTime)

// TODO: Add your update logic here

state?.Update(new DeltaTimes
{

});
base.Update(gameTime);
}

Expand All @@ -47,6 +68,10 @@ protected override void Draw(GameTime gameTime)

// TODO: Add your drawing code here

state?.Draw(new DeltaTimes
{

});
base.Draw(gameTime);
}
}
Expand Down
13 changes: 13 additions & 0 deletions OpenKh.Game/States/IState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using OpenKh.Game.Infrastructure;

namespace OpenKh.Game.States
{
public interface IState
{
void Initialize(StateInitDesc initDesc);
void Destroy();

void Update(DeltaTimes deltaTimes);
void Draw(DeltaTimes deltaTimes);
}
}
45 changes: 45 additions & 0 deletions OpenKh.Game/States/TitleState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using OpenKh.Engine;
using OpenKh.Engine.Renderers;
using OpenKh.Game.Infrastructure;
using OpenKh.Kh2;
using System.Collections.Generic;
using System.Linq;
using Xe.Drawing;

namespace OpenKh.Game.States
{
public class TitleState : IState
{
private ArchiveManager archiveManager;
private MonoDrawing drawing;
private Layout titleLayout;
private IEnumerable<ISurface> titleImages;
private LayoutRenderer layoutRenderer;

public void Initialize(StateInitDesc initDesc)
{
archiveManager = initDesc.ArchiveManager;
drawing = new MonoDrawing(initDesc.GraphicsDevice.GraphicsDevice);

archiveManager.LoadArchive("menu/fm/title.2ld");
titleLayout = archiveManager.Get<Layout>("titl");
titleImages = archiveManager.Get<Imgz>("titl")?.Images?.Select(x => drawing.CreateSurface(x));
layoutRenderer = new LayoutRenderer(titleLayout, drawing, titleImages);
}

public void Destroy()
{
throw new System.NotImplementedException();
}

public void Update(DeltaTimes deltaTimes)
{
layoutRenderer.FrameIndex++;
}

public void Draw(DeltaTimes deltaTimes)
{
layoutRenderer.Draw();
}
}
}

0 comments on commit 7214c29

Please sign in to comment.