Skip to content

Commit 6bf6134

Browse files
authored
Merge pull request #60 from Techiesplash/main
Framework and Platform improvements (App, Math, Graphical, Storage)
2 parents 33ac6f2 + 3618df0 commit 6bf6134

File tree

12 files changed

+541
-222
lines changed

12 files changed

+541
-222
lines changed

Framework/App.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,9 @@ public static bool MouseVisible
265265

266266
/// <summary>
267267
/// Runs the Application with the given Module automatically registered.
268-
/// Functionally the same as calling <see cref="Register{T}"/> followed by <see cref="Run(string, int, int, bool)"/>
268+
/// Functionally the same as calling <see cref="Register{T}"/> followed by <see cref="Run(string, int, int, bool, Renderers)"/>
269269
/// </summary>
270-
public static void Run<T>(string applicationName, int width, int height, bool fullscreen = false) where T : Module, new()
270+
public static void Run<T>(string applicationName, int width, int height, bool fullscreen = false, Renderers renderer = Renderers.None) where T : Module, new()
271271
{
272272
Register<T>();
273273
Run(applicationName, width, height, fullscreen);
@@ -276,7 +276,7 @@ public static bool MouseVisible
276276
/// <summary>
277277
/// Runs the Application
278278
/// </summary>
279-
public static void Run(string applicationName, int width, int height, bool fullscreen = false)
279+
public static void Run(string applicationName, int width, int height, bool fullscreen = false, Renderers renderer = Renderers.None)
280280
{
281281
Debug.Assert(!Running, "Application is already running");
282282
Debug.Assert(!Exiting, "Application is still exiting");
@@ -301,6 +301,7 @@ public static void Run(string applicationName, int width, int height, bool fulls
301301
applicationName = name,
302302
width = width,
303303
height = height,
304+
renderer = renderer,
304305
flags = App.flags,
305306
onText = Input.OnText,
306307
onKey = Input.OnKey,

Framework/Graphics/Batcher.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1638,7 +1638,8 @@ public void Text(SpriteFont font, ReadOnlySpan<char> text, Vector2 position, Vec
16381638
if (last != 0)
16391639
at.X += font.GetKerning(last, ch.Codepoint);
16401640

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

16431644
last = ch.Codepoint;
16441645
at.X += ch.Advance;

Framework/Graphics/ShaderDefaults.cs

+31-31
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,37 @@ internal static class ShaderDefaults
77
[Renderers.OpenGL] = new()
88
{
99
VertexShader =
10-
"#version 330\n" +
11-
"uniform mat4 u_matrix;\n" +
12-
"layout(location=0) in vec2 a_position;\n" +
13-
"layout(location=1) in vec2 a_tex;\n" +
14-
"layout(location=2) in vec4 a_color;\n" +
15-
"layout(location=3) in vec4 a_type;\n" +
16-
"out vec2 v_tex;\n" +
17-
"out vec4 v_col;\n" +
18-
"out vec4 v_type;\n" +
19-
"void main(void)\n" +
20-
"{\n" +
21-
" gl_Position = u_matrix * vec4(a_position.xy, 0, 1);\n" +
22-
" v_tex = a_tex;\n" +
23-
" v_col = a_color;\n" +
24-
" v_type = a_type;\n" +
25-
"}",
26-
FragmentShader =
27-
"#version 330\n" +
28-
"uniform sampler2D u_texture;\n" +
29-
"in vec2 v_tex;\n" +
30-
"in vec4 v_col;\n" +
31-
"in vec4 v_type;\n" +
32-
"out vec4 o_color;\n" +
33-
"void main(void)\n" +
34-
"{\n" +
35-
" vec4 color = texture(u_texture, v_tex);\n" +
36-
" o_color = \n" +
37-
" v_type.x * color * v_col + \n" +
38-
" v_type.y * color.a * v_col + \n" +
39-
" v_type.z * v_col;\n" +
40-
"}"
10+
@"#version 330
11+
uniform mat4 u_matrix;
12+
layout(location=0) in vec2 a_position;
13+
layout(location=1) in vec2 a_tex;
14+
layout(location=2) in vec4 a_color;
15+
layout(location=3) in vec4 a_type;
16+
out vec2 v_tex;
17+
out vec4 v_col;
18+
out vec4 v_type;
19+
void main(void)
20+
{
21+
gl_Position = u_matrix * vec4(a_position.xy, 0, 1);
22+
v_tex = a_tex;
23+
v_col = a_color;
24+
v_type = a_type;
25+
}",
26+
FragmentShader =
27+
@"#version 330
28+
uniform sampler2D u_texture;
29+
in vec2 v_tex;
30+
in vec4 v_col;
31+
in vec4 v_type;
32+
out vec4 o_color;
33+
void main(void)
34+
{
35+
vec4 color = texture(u_texture, v_tex);
36+
o_color =
37+
v_type.x * color * v_col +
38+
v_type.y * color.a * v_col +
39+
v_type.z * v_col;
40+
}"
4141
}
4242
};
4343
}

Framework/Storage/Content.cs

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Linq.Expressions;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Foster.Framework.Storage
10+
{
11+
/// <summary>
12+
/// Default Content implementation.
13+
/// Should work well for PC, etc, but can be overridden for custom handling.
14+
/// </summary>
15+
public class Content
16+
{
17+
public string CurrentDirectory { get; set; } = "";
18+
19+
private class ContentEnumerator : IEnumerator<string>
20+
{
21+
public string[] Locations;
22+
public int Index = -1;
23+
24+
public string Current
25+
{
26+
get
27+
{
28+
try
29+
{
30+
return Locations[Index];
31+
}
32+
catch (IndexOutOfRangeException)
33+
{
34+
throw new InvalidOperationException();
35+
}
36+
}
37+
}
38+
39+
object IEnumerator.Current => Current;
40+
41+
public ContentEnumerator(string[] locations)
42+
{
43+
Locations = locations;
44+
}
45+
46+
public bool MoveNext()
47+
{
48+
Index++;
49+
if (Index >= Locations.Length)
50+
return false;
51+
return true;
52+
}
53+
54+
public void Reset() => Index = -1;
55+
56+
public void Dispose() { }
57+
}
58+
59+
public Content() { }
60+
public Content(string content) : this()
61+
{
62+
CurrentDirectory = content;
63+
}
64+
65+
#region Directory
66+
public virtual bool FileExists(string relativePath)
67+
{
68+
return File.Exists(CurrentDirectory + relativePath);
69+
}
70+
public virtual bool DirectoryExists(string relativePath)
71+
{
72+
return Directory.Exists(CurrentDirectory + relativePath);
73+
}
74+
public virtual bool Exists(string name)
75+
{
76+
return FileExists(name) || DirectoryExists(name);
77+
}
78+
79+
public virtual IEnumerator<string> EnumerateFiles(string path, string searchPattern, bool recursive)
80+
{
81+
return new ContentEnumerator(
82+
Directory.GetFiles(
83+
CurrentDirectory + path,
84+
searchPattern,
85+
recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly
86+
));
87+
}
88+
89+
public virtual IEnumerator<string> EnumerateDirectories(string path, string searchPattern, bool recursive)
90+
{
91+
92+
return new ContentEnumerator(
93+
Directory.GetDirectories(
94+
CurrentDirectory + path,
95+
searchPattern,
96+
recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly
97+
));
98+
}
99+
100+
#endregion
101+
102+
#region File
103+
104+
public virtual Stream OpenRead(string relativePath)
105+
{
106+
return File.OpenRead(CurrentDirectory + relativePath);
107+
}
108+
109+
public virtual byte[] ReadAllBytes(string relativePath)
110+
{
111+
return File.ReadAllBytes(CurrentDirectory + relativePath);
112+
}
113+
114+
public virtual string ReadAllText(string relativePath)
115+
{
116+
return File.ReadAllText(CurrentDirectory + relativePath);
117+
}
118+
119+
#endregion
120+
}
121+
}

Framework/Storage/UserStorage.cs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Foster.Framework.Storage
8+
{
9+
/// <summary>
10+
/// A storage location for user data (save files, etc)
11+
/// </summary>
12+
public static class UserStorage
13+
{
14+
public static WritableContent Provider { get; set; } = new WritableContent(App.UserPath);
15+
16+
#region Directory
17+
18+
public static bool FileExists(string relativePath)
19+
=> Provider.FileExists(relativePath);
20+
public static bool DirectoryExists(string relativePath)
21+
=> Provider.DirectoryExists(relativePath);
22+
public static bool Exists(string name)
23+
=> Provider.Exists(name);
24+
25+
public static IEnumerator<string> EnumerateFiles(string path, string searchPattern, bool recursive)
26+
=> Provider.EnumerateFiles(path, searchPattern, recursive);
27+
public static IEnumerator<string> EnumerateDirectories(string path, string searchPattern, bool recursive)
28+
=> Provider.EnumerateDirectories(path, searchPattern, recursive);
29+
30+
public static void CreateDirectory(string path)
31+
=> Provider.CreateDirectory(path);
32+
public static void DeleteDirectory(string path, bool recursive)
33+
=> Provider.DeleteDirectory(path, recursive);
34+
public static void DeleteFile(string path)
35+
=> Provider.DeleteFile(path);
36+
37+
#endregion
38+
39+
#region File
40+
41+
public static Stream OpenRead(string relativePath)
42+
=> Provider.OpenRead(relativePath);
43+
44+
public static byte[] ReadAllBytes(string relativePath)
45+
=> Provider.ReadAllBytes(relativePath);
46+
47+
public static string ReadAllText(string relativePath)
48+
=> Provider.ReadAllText(relativePath);
49+
50+
51+
public static Stream OpenWrite(string path)
52+
=> Provider.OpenWrite(path);
53+
public static void WriteAllBytes(string path, byte[] bytes)
54+
=> Provider.WriteAllBytes(path, bytes);
55+
public static void WriteAllText(string path, string text)
56+
=> Provider.WriteAllText(path, text);
57+
58+
#endregion
59+
60+
}
61+
}

Framework/Storage/WritableContent.cs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Foster.Framework.Storage
8+
{
9+
/// <summary>
10+
/// A Content that may also be written to.
11+
/// </summary>
12+
public class WritableContent : Content
13+
{
14+
public WritableContent() { }
15+
public WritableContent(string currentDirectory) : base(currentDirectory) { }
16+
17+
#region Directory
18+
public virtual void CreateDirectory(string path)
19+
{
20+
Directory.CreateDirectory(CurrentDirectory + path);
21+
}
22+
23+
public virtual void DeleteDirectory(string path, bool recursive)
24+
{
25+
Directory.Delete(CurrentDirectory + path, recursive);
26+
}
27+
28+
public virtual void DeleteFile(string path)
29+
{
30+
File.Delete(CurrentDirectory + path);
31+
}
32+
33+
#endregion
34+
35+
#region File
36+
37+
public virtual Stream OpenWrite(string path)
38+
{
39+
return File.OpenWrite(CurrentDirectory + path);
40+
}
41+
42+
public virtual void WriteAllBytes(string path, byte[] bytes)
43+
{
44+
File.WriteAllBytes(CurrentDirectory + path, bytes);
45+
}
46+
47+
public virtual void WriteAllText(string path, string text)
48+
{
49+
File.WriteAllText(CurrentDirectory + path, text);
50+
}
51+
52+
#endregion
53+
}
54+
}

0 commit comments

Comments
 (0)