From 4b4a43b0f19c772c70af34ee204a8f5f5897d945 Mon Sep 17 00:00:00 2001 From: Pyrdacor Date: Thu, 2 Mar 2023 11:39:13 +0100 Subject: [PATCH] First refactor milestone --- Ambermoon.Core/Render/Layer.cs | 5 ++-- .../AlphaTextureShader.cs | 5 +++- Ambermoon.Renderer.OpenGL/RenderView.cs | 2 +- Ambermoon.Renderer.OpenGL/TextShader.cs | 2 +- Ambermoon.Renderer.OpenGL/TextureShader.cs | 4 +-- Ambermoon.net/FantasyIntro.cs | 7 ++--- Ambermoon.net/GameWindow.cs | 27 +++++++------------ Ambermoon.net/MainMenu.cs | 6 ++--- Ambermoon.net/Outro.cs | 10 +++---- 9 files changed, 32 insertions(+), 36 deletions(-) diff --git a/Ambermoon.Core/Render/Layer.cs b/Ambermoon.Core/Render/Layer.cs index c277d842..7aad01fe 100644 --- a/Ambermoon.Core/Render/Layer.cs +++ b/Ambermoon.Core/Render/Layer.cs @@ -23,6 +23,7 @@ namespace Ambermoon { public enum Layer { + // Note: Don't add aliases here as this is used for enumerating over all layers. None = -1, Map3DBackground, // Color floor, sky, etc Map3DCeiling, @@ -66,13 +67,13 @@ public enum Layer Cursor, DrugEffect, Misc, // general purpose layer - Images, // non-palette high-resolution images - Last = Images + Images // non-palette high-resolution images } public partial class Global { public const Layer First2DLayer = Layer.MapBackground1; public const Layer Last2DLayer = Layer.MapForeground10; + public const Layer LastLayer = Layer.Images; } } diff --git a/Ambermoon.Renderer.OpenGL/AlphaTextureShader.cs b/Ambermoon.Renderer.OpenGL/AlphaTextureShader.cs index 9d8a5440..67602eb5 100644 --- a/Ambermoon.Renderer.OpenGL/AlphaTextureShader.cs +++ b/Ambermoon.Renderer.OpenGL/AlphaTextureShader.cs @@ -37,7 +37,9 @@ internal class AlphaTextureShader : TextureShader $"flat in float maskColIndex;", $"flat in float a;", $"", - $" vec4 pixelColor;", + $"void main()", + $"{{", + $" vec4 pixelColor = vec4(0);", $" if ({DefaultUsePaletteName} > 0.5f)", $" {{", $" float colorIndex = texture({DefaultSamplerName}, varTexCoord).r * 255.0f;", @@ -61,6 +63,7 @@ internal class AlphaTextureShader : TextureShader $" if (maskColIndex > 0.5f)", $" pixelColor = texture({DefaultPaletteName}, vec2((maskColIndex + 0.5f) / 32.0f, (palIndex + 0.5f) / {Shader.PaletteCount}));", $" {DefaultFragmentOutColorName} = vec4(pixelColor.rgb, pixelColor.a * a);", + $"}}" }; protected static string[] AlphaTextureVertexShader(State state) => new string[] diff --git a/Ambermoon.Renderer.OpenGL/RenderView.cs b/Ambermoon.Renderer.OpenGL/RenderView.cs index a0b177a0..5a188225 100644 --- a/Ambermoon.Renderer.OpenGL/RenderView.cs +++ b/Ambermoon.Renderer.OpenGL/RenderView.cs @@ -612,7 +612,7 @@ public void Render(FloatPosition viewportOffset) set2DViewport = true; } - if (useEffectFrameBuffer && layer.Key == Layer.Last) + if (useEffectFrameBuffer && layer.Key == Global.LastLayer) { State.Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0u); var viewport = frameBufferWindowArea; diff --git a/Ambermoon.Renderer.OpenGL/TextShader.cs b/Ambermoon.Renderer.OpenGL/TextShader.cs index 2618c4c4..41348a80 100644 --- a/Ambermoon.Renderer.OpenGL/TextShader.cs +++ b/Ambermoon.Renderer.OpenGL/TextShader.cs @@ -51,7 +51,7 @@ internal class TextShader : TextureShader $" }}", $" else", $" {{", - $" vec4 pixelColor = texture({DefaultSamplerName}, varTexCoor);", + $" vec4 pixelColor = texture({DefaultSamplerName}, varTexCoord);", $" if (pixelColor.a < 0.5f)", $" discard;", $" else", diff --git a/Ambermoon.Renderer.OpenGL/TextureShader.cs b/Ambermoon.Renderer.OpenGL/TextureShader.cs index 69b89a99..9fade7be 100644 --- a/Ambermoon.Renderer.OpenGL/TextureShader.cs +++ b/Ambermoon.Renderer.OpenGL/TextureShader.cs @@ -49,7 +49,7 @@ internal class TextureShader : ColorShader $"", $"void main()", $"{{", - $" vec4 pixelColor;", + $" vec4 pixelColor = vec4(0);", $" if ({DefaultUsePaletteName} > 0.5f)", $" {{", $" float colorIndex = texture({DefaultSamplerName}, varTexCoord).r * 255.0f;", @@ -119,7 +119,7 @@ protected TextureShader(State state, string[] fragmentShaderLines, string[] vert public void UsePalette(bool use) { - shaderProgram.SetInput(DefaultPaletteName, use ? 1.0f : 0.0f); + shaderProgram.SetInput(DefaultUsePaletteName, use ? 1.0f : 0.0f); } public void SetSampler(int textureUnit = 0) diff --git a/Ambermoon.net/FantasyIntro.cs b/Ambermoon.net/FantasyIntro.cs index 9162140b..34c6f15d 100644 --- a/Ambermoon.net/FantasyIntro.cs +++ b/Ambermoon.net/FantasyIntro.cs @@ -1,4 +1,5 @@ -using Ambermoon.Data.Legacy.Serialization; +using Ambermoon.Data; +using Ambermoon.Data.Legacy.Serialization; using Ambermoon.Render; using System; using System.Collections.Generic; @@ -30,7 +31,7 @@ internal class FantasyIntro bool playFairyAnimation = false; uint fairyAnimationIndex = 0; - static void EnsureTextures(IRenderView renderView, FantasyIntroData fantasyIntroData) + static void EnsureTextures(IRenderView renderView, IFantasyIntroData fantasyIntroData) { if (textureAtlas == null) { @@ -41,7 +42,7 @@ static void EnsureTextures(IRenderView renderView, FantasyIntroData fantasyIntro } } - public FantasyIntro(IRenderView renderView, FantasyIntroData fantasyIntroData, Action finishAction) + public FantasyIntro(IRenderView renderView, IFantasyIntroData fantasyIntroData, Action finishAction) { this.finishAction = finishAction; this.renderView = renderView; diff --git a/Ambermoon.net/GameWindow.cs b/Ambermoon.net/GameWindow.cs index e610b133..f764356d 100644 --- a/Ambermoon.net/GameWindow.cs +++ b/Ambermoon.net/GameWindow.cs @@ -689,23 +689,19 @@ void PlayMusic(Song song) void StartGame(IGameData gameData, string savePath, GameLanguage gameLanguage, Features features, BinaryReader advancedDiffsReader) { // Load fantasy intro data - var fantasyIntroData = new FantasyIntroData(gameData); + var fantasyIntroData = gameData.FantasyIntroData; // Load intro data - var introData = new IntroData(gameData); + var introData = gameData.IntroData; var introFont = new Font(Resources.IntroFont, 12); // Load outro data - var outroData = new OutroData(gameData); + var outroData = gameData.OutroData; var outroFont = new Font(outroData.Glyphs, 6, 0); var outroFontLarge = new Font(outroData.LargeGlyphs, 10, (uint)outroData.Glyphs.Count); // Load game data - var additionalPalettes = new List(); - additionalPalettes.AddRange(introData.IntroPalettes); - additionalPalettes.AddRange(outroData.OutroPalettes); - additionalPalettes.AddRange(fantasyIntroData.FantasyIntroPalettes); - var graphicProvider = gameData.GetGraphicProvider(additionalPalettes); + var graphicProvider = gameData.GraphicProvider; if (audioOutput == null) { @@ -757,7 +753,6 @@ void StartGame(IGameData gameData, string savePath, GameLanguage gameLanguage, F { try { - var textDictionary = TextDictionary.Load(new TextDictionaryReader(), gameData.GetDictionary()); var savegameManager = new SavegameManager(savePath); savegameManager.GetSavegameNames(gameData, out int currentSavegame, 10); if (currentSavegame == 0 && configuration.ExtendedSavegameSlots) @@ -776,8 +771,8 @@ void SetupGameCreator(bool continueGame) gameCreator = () => { var game = new Game(configuration, gameLanguage, renderView, graphicProvider, - savegameManager, savegameSerializer, textDictionary, cursor, audioOutput, musicManager, - FullscreenChangeRequest, ChangeResolution, QueryPressedKeys, + savegameManager, savegameSerializer, gameData.Dictionary, cursor, audioOutput, + musicManager, FullscreenChangeRequest, ChangeResolution, QueryPressedKeys, new OutroFactory(renderView, outroData, outroFont, outroFontLarge), features); game.QuitRequested += window.Close; game.MousePositionChanged += position => @@ -1013,11 +1008,8 @@ GameData LoadGameDataFromDataPath() builtinVersionDataProviders[1] = () => configuration.GameVersionIndex == 1 ? gameData : LoadBuiltinVersionData(versions[1], builtinVersionDataProviders[0]); builtinVersionDataProviders[2] = () => configuration.GameVersionIndex == 2 ? gameData : LoadBuiltinVersionData(versions[2], null); builtinVersionDataProviders[3] = () => configuration.GameVersionIndex == 3 ? gameData : LoadBuiltinVersionData(versions[3], builtinVersionDataProviders[2]); - var executableData = ExecutableData.FromGameData(gameData); - var graphicProvider = new GraphicProvider(gameData, executableData, null, null, null); var textureAtlasManager = TextureAtlasManager.CreateEmpty(); createdTextureAtlasManager = textureAtlasManager; - var fontProvider = new FontProvider(executableData); audioOutput = new AudioOutput(); audioOutput.Volume = Util.Limit(0, configuration.Volume, 100) / 100.0f; @@ -1032,9 +1024,9 @@ GameData LoadGameDataFromDataPath() logoPalettes = new Graphic[1] { new Graphic { Width = 32, Height = 1, IndexedGraphic = false, Data = new byte[32 * 4] } }; } - renderView = CreateRenderView(gameData, configuration, graphicProvider, logoPalettes, () => + renderView = CreateRenderView(gameData, configuration, gameData.GraphicProvider, logoPalettes, () => { - textureAtlasManager.AddUIOnly(graphicProvider, fontProvider); + textureAtlasManager.AddUIOnly(gameData.GraphicProvider, gameData.FontProvider); logoPyrdacor?.Initialize(textureAtlasManager); AdvancedLogo.Initialize(textureAtlasManager); return textureAtlasManager; @@ -1065,8 +1057,7 @@ GameData LoadGameDataFromDataPath() Features = additionalVersionInfo.Value.Advanced ? Features.AmbermoonAdvanced : Features.None }); } - var cursor = new Render.Cursor(renderView, executableData.Cursors.Entries.Select(c => new Position(c.HotspotX, c.HotspotY)).ToList().AsReadOnly(), - textureAtlasManager); + var cursor = new Render.Cursor(renderView, gameData.CursorHotspots, textureAtlasManager); RunTask(() => { diff --git a/Ambermoon.net/MainMenu.cs b/Ambermoon.net/MainMenu.cs index c684d6ad..3bfb6f72 100644 --- a/Ambermoon.net/MainMenu.cs +++ b/Ambermoon.net/MainMenu.cs @@ -1,5 +1,5 @@ -using Ambermoon.Data.Enumerations; -using Ambermoon.Data.Legacy.Serialization; +using Ambermoon.Data; +using Ambermoon.Data.Enumerations; using Ambermoon.Render; using System; using System.Collections.Generic; @@ -26,7 +26,7 @@ public enum CloseAction Fader fader; Fader mainMenuFader; UI.UIText loadingText; - List> mainMenuTexts = new List>(4); + List> mainMenuTexts = new(4); int hoveredTextIndex = -1; DateTime? hoverStartTime = null; const int HoverColorTime = 125; diff --git a/Ambermoon.net/Outro.cs b/Ambermoon.net/Outro.cs index 7e4c5782..618907db 100644 --- a/Ambermoon.net/Outro.cs +++ b/Ambermoon.net/Outro.cs @@ -11,7 +11,7 @@ internal class Outro : IOutro { static ITextureAtlas textureAtlas = null; readonly Action finishAction; - readonly OutroData outroData; + readonly IOutroData outroData; readonly Font outroFont; readonly Font outroFontLarge; readonly IRenderView renderView; @@ -34,7 +34,7 @@ internal class Outro : IOutro long fadeStartTicks = 0; const long HalfFadeDurationInTicks = 3 * Game.TicksPerSecond / 4; - static void EnsureTextures(IRenderView renderView, OutroData outroData, Font outroFont, Font outroFontLarge) + static void EnsureTextures(IRenderView renderView, IOutroData outroData, Font outroFont, Font outroFontLarge) { if (textureAtlas == null) { @@ -48,7 +48,7 @@ static void EnsureTextures(IRenderView renderView, OutroData outroData, Font out } } - public Outro(IRenderView renderView, OutroData outroData, Font outroFont, Font outroFontLarge, Action finishAction) + public Outro(IRenderView renderView, IOutroData outroData, Font outroFont, Font outroFontLarge, Action finishAction) { this.finishAction = finishAction; this.outroData = outroData; @@ -305,11 +305,11 @@ public void Destroy() internal class OutroFactory : IOutroFactory { readonly IRenderView renderView; - readonly OutroData outroData; + readonly IOutroData outroData; readonly Font outroFont; readonly Font outroFontLarge; - public OutroFactory(IRenderView renderView, OutroData outroData, Font outroFont, Font outroFontLarge) + public OutroFactory(IRenderView renderView, IOutroData outroData, Font outroFont, Font outroFontLarge) { this.renderView = renderView; this.outroData = outroData;