-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |