Skip to content

Commit

Permalink
Merge pull request #60 from Techiesplash/main
Browse files Browse the repository at this point in the history
Framework and Platform improvements (App, Math, Graphical, Storage)
  • Loading branch information
NoelFB committed Feb 5, 2024
2 parents 33ac6f2 + 3618df0 commit 6bf6134
Show file tree
Hide file tree
Showing 12 changed files with 541 additions and 222 deletions.
7 changes: 4 additions & 3 deletions Framework/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ public static void Register<T>() where T : Module, new()

/// <summary>
/// Runs the Application with the given Module automatically registered.
/// Functionally the same as calling <see cref="Register{T}"/> followed by <see cref="Run(string, int, int, bool)"/>
/// Functionally the same as calling <see cref="Register{T}"/> followed by <see cref="Run(string, int, int, bool, Renderers)"/>
/// </summary>
public static void Run<T>(string applicationName, int width, int height, bool fullscreen = false) where T : Module, new()
public static void Run<T>(string applicationName, int width, int height, bool fullscreen = false, Renderers renderer = Renderers.None) where T : Module, new()
{
Register<T>();
Run(applicationName, width, height, fullscreen);
Expand All @@ -276,7 +276,7 @@ public static void Run<T>(string applicationName, int width, int height, bool fu
/// <summary>
/// Runs the Application
/// </summary>
public static void Run(string applicationName, int width, int height, bool fullscreen = false)
public static void Run(string applicationName, int width, int height, bool fullscreen = false, Renderers renderer = Renderers.None)
{
Debug.Assert(!Running, "Application is already running");
Debug.Assert(!Exiting, "Application is still exiting");
Expand All @@ -301,6 +301,7 @@ public static void Run(string applicationName, int width, int height, bool fulls
applicationName = name,
width = width,
height = height,
renderer = renderer,
flags = App.flags,
onText = Input.OnText,
onKey = Input.OnKey,
Expand Down
3 changes: 2 additions & 1 deletion Framework/Graphics/Batcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1638,7 +1638,8 @@ public void Text(SpriteFont font, ReadOnlySpan<char> text, Vector2 position, Vec
if (last != 0)
at.X += font.GetKerning(last, ch.Codepoint);

Image(ch.Subtexture, at + ch.Offset, color);
if (ch.Subtexture.Texture != null)
Image(ch.Subtexture, at + ch.Offset, color);

last = ch.Codepoint;
at.X += ch.Advance;
Expand Down
62 changes: 31 additions & 31 deletions Framework/Graphics/ShaderDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,37 @@ internal static class ShaderDefaults
[Renderers.OpenGL] = new()
{
VertexShader =
"#version 330\n" +
"uniform mat4 u_matrix;\n" +
"layout(location=0) in vec2 a_position;\n" +
"layout(location=1) in vec2 a_tex;\n" +
"layout(location=2) in vec4 a_color;\n" +
"layout(location=3) in vec4 a_type;\n" +
"out vec2 v_tex;\n" +
"out vec4 v_col;\n" +
"out vec4 v_type;\n" +
"void main(void)\n" +
"{\n" +
" gl_Position = u_matrix * vec4(a_position.xy, 0, 1);\n" +
" v_tex = a_tex;\n" +
" v_col = a_color;\n" +
" v_type = a_type;\n" +
"}",
FragmentShader =
"#version 330\n" +
"uniform sampler2D u_texture;\n" +
"in vec2 v_tex;\n" +
"in vec4 v_col;\n" +
"in vec4 v_type;\n" +
"out vec4 o_color;\n" +
"void main(void)\n" +
"{\n" +
" vec4 color = texture(u_texture, v_tex);\n" +
" o_color = \n" +
" v_type.x * color * v_col + \n" +
" v_type.y * color.a * v_col + \n" +
" v_type.z * v_col;\n" +
"}"
@"#version 330
uniform mat4 u_matrix;
layout(location=0) in vec2 a_position;
layout(location=1) in vec2 a_tex;
layout(location=2) in vec4 a_color;
layout(location=3) in vec4 a_type;
out vec2 v_tex;
out vec4 v_col;
out vec4 v_type;
void main(void)
{
gl_Position = u_matrix * vec4(a_position.xy, 0, 1);
v_tex = a_tex;
v_col = a_color;
v_type = a_type;
}",
FragmentShader =
@"#version 330
uniform sampler2D u_texture;
in vec2 v_tex;
in vec4 v_col;
in vec4 v_type;
out vec4 o_color;
void main(void)
{
vec4 color = texture(u_texture, v_tex);
o_color =
v_type.x * color * v_col +
v_type.y * color.a * v_col +
v_type.z * v_col;
}"
}
};
}
121 changes: 121 additions & 0 deletions Framework/Storage/Content.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Foster.Framework.Storage
{
/// <summary>
/// Default Content implementation.
/// Should work well for PC, etc, but can be overridden for custom handling.
/// </summary>
public class Content
{
public string CurrentDirectory { get; set; } = "";

private class ContentEnumerator : IEnumerator<string>
{
public string[] Locations;
public int Index = -1;

public string Current
{
get
{
try
{
return Locations[Index];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}

object IEnumerator.Current => Current;

public ContentEnumerator(string[] locations)
{
Locations = locations;
}

public bool MoveNext()
{
Index++;
if (Index >= Locations.Length)
return false;
return true;
}

public void Reset() => Index = -1;

public void Dispose() { }
}

public Content() { }
public Content(string content) : this()
{
CurrentDirectory = content;
}

#region Directory
public virtual bool FileExists(string relativePath)
{
return File.Exists(CurrentDirectory + relativePath);
}
public virtual bool DirectoryExists(string relativePath)
{
return Directory.Exists(CurrentDirectory + relativePath);
}
public virtual bool Exists(string name)
{
return FileExists(name) || DirectoryExists(name);
}

public virtual IEnumerator<string> EnumerateFiles(string path, string searchPattern, bool recursive)
{
return new ContentEnumerator(
Directory.GetFiles(
CurrentDirectory + path,
searchPattern,
recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly
));
}

public virtual IEnumerator<string> EnumerateDirectories(string path, string searchPattern, bool recursive)
{

return new ContentEnumerator(
Directory.GetDirectories(
CurrentDirectory + path,
searchPattern,
recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly
));
}

#endregion

#region File

public virtual Stream OpenRead(string relativePath)
{
return File.OpenRead(CurrentDirectory + relativePath);
}

public virtual byte[] ReadAllBytes(string relativePath)
{
return File.ReadAllBytes(CurrentDirectory + relativePath);
}

public virtual string ReadAllText(string relativePath)
{
return File.ReadAllText(CurrentDirectory + relativePath);
}

#endregion
}
}
61 changes: 61 additions & 0 deletions Framework/Storage/UserStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Foster.Framework.Storage
{
/// <summary>
/// A storage location for user data (save files, etc)
/// </summary>
public static class UserStorage
{
public static WritableContent Provider { get; set; } = new WritableContent(App.UserPath);

#region Directory

public static bool FileExists(string relativePath)
=> Provider.FileExists(relativePath);
public static bool DirectoryExists(string relativePath)
=> Provider.DirectoryExists(relativePath);
public static bool Exists(string name)
=> Provider.Exists(name);

public static IEnumerator<string> EnumerateFiles(string path, string searchPattern, bool recursive)
=> Provider.EnumerateFiles(path, searchPattern, recursive);
public static IEnumerator<string> EnumerateDirectories(string path, string searchPattern, bool recursive)
=> Provider.EnumerateDirectories(path, searchPattern, recursive);

public static void CreateDirectory(string path)
=> Provider.CreateDirectory(path);
public static void DeleteDirectory(string path, bool recursive)
=> Provider.DeleteDirectory(path, recursive);
public static void DeleteFile(string path)
=> Provider.DeleteFile(path);

#endregion

#region File

public static Stream OpenRead(string relativePath)
=> Provider.OpenRead(relativePath);

public static byte[] ReadAllBytes(string relativePath)
=> Provider.ReadAllBytes(relativePath);

public static string ReadAllText(string relativePath)
=> Provider.ReadAllText(relativePath);


public static Stream OpenWrite(string path)
=> Provider.OpenWrite(path);
public static void WriteAllBytes(string path, byte[] bytes)
=> Provider.WriteAllBytes(path, bytes);
public static void WriteAllText(string path, string text)
=> Provider.WriteAllText(path, text);

#endregion

}
}
54 changes: 54 additions & 0 deletions Framework/Storage/WritableContent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Foster.Framework.Storage
{
/// <summary>
/// A Content that may also be written to.
/// </summary>
public class WritableContent : Content
{
public WritableContent() { }
public WritableContent(string currentDirectory) : base(currentDirectory) { }

#region Directory
public virtual void CreateDirectory(string path)
{
Directory.CreateDirectory(CurrentDirectory + path);
}

public virtual void DeleteDirectory(string path, bool recursive)
{
Directory.Delete(CurrentDirectory + path, recursive);
}

public virtual void DeleteFile(string path)
{
File.Delete(CurrentDirectory + path);
}

#endregion

#region File

public virtual Stream OpenWrite(string path)
{
return File.OpenWrite(CurrentDirectory + path);
}

public virtual void WriteAllBytes(string path, byte[] bytes)
{
File.WriteAllBytes(CurrentDirectory + path, bytes);
}

public virtual void WriteAllText(string path, string text)
{
File.WriteAllText(CurrentDirectory + path, text);
}

#endregion
}
}
Loading

0 comments on commit 6bf6134

Please sign in to comment.