Skip to content

Commit 6f70af0

Browse files
[Dependencies] Update BGFX;
[Player] Add fullscreen switch; [Android] Fix crashing issues and context recreation logic and re-add vulkan support; [Resources] Add tracking of vertex/index buffers;
1 parent 2407a84 commit 6f70af0

File tree

13 files changed

+144
-76
lines changed

13 files changed

+144
-76
lines changed

Engine/Core/MessagePackGenerated/MessagePackGenerated.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ public void Serialize(ref global::MessagePack.MessagePackWriter writer, global::
785785
}
786786

787787
global::MessagePack.IFormatterResolver formatterResolver = options.Resolver;
788-
writer.WriteArrayHeader(25);
788+
writer.WriteArrayHeader(26);
789789
writer.Write(value.runInBackground);
790790
formatterResolver.GetFormatterWithVerify<string>().Serialize(ref writer, value.appName, options);
791791
formatterResolver.GetFormatterWithVerify<string>().Serialize(ref writer, value.companyName, options);
@@ -811,6 +811,7 @@ public void Serialize(ref global::MessagePack.MessagePackWriter writer, global::
811811
formatterResolver.GetFormatterWithVerify<global::Staple.Color>().Serialize(ref writer, value.ambientLight, options);
812812
formatterResolver.GetFormatterWithVerify<global::Staple.AppSettings.ProfilingMode>().Serialize(ref writer, value.profilingMode, options);
813813
writer.Write(value.enableLighting);
814+
writer.Write(value.allowFullscreenSwitch);
814815
}
815816

816817
public global::Staple.AppSettings Deserialize(ref global::MessagePack.MessagePackReader reader, global::MessagePack.MessagePackSerializerOptions options)
@@ -904,6 +905,9 @@ public void Serialize(ref global::MessagePack.MessagePackWriter writer, global::
904905
case 24:
905906
____result.enableLighting = reader.ReadBoolean();
906907
break;
908+
case 25:
909+
____result.allowFullscreenSwitch = reader.ReadBoolean();
910+
break;
907911
default:
908912
reader.Skip();
909913
break;

Engine/Core/Player/Android/StapleActivity.cs

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ private void Frame()
405405

406406
lock (renderWindow.renderLock)
407407
{
408-
renderWindow.shouldRender = renderWindow.window.Unavailable == false && (AppSettings.Current.runInBackground == true || renderWindow.window.IsFocused == true);
408+
renderWindow.shouldRender = renderWindow.window.Unavailable == false && renderWindow.window.IsFocused;
409409
}
410410

411411
if (renderWindow.window.Unavailable)
@@ -431,7 +431,7 @@ private void Frame()
431431

432432
renderWindow.CheckContextLost();
433433

434-
if (AppSettings.Current.runInBackground == false && renderWindow.window.IsFocused != renderWindow.hasFocus)
434+
if (renderWindow.window.IsFocused != renderWindow.hasFocus)
435435
{
436436
renderWindow.hasFocus = renderWindow.window.IsFocused;
437437

@@ -505,8 +505,6 @@ void Finish()
505505
renderWindow.screenHeight = height;
506506
renderWindow.window = nativeWindow;
507507
renderWindow.unavailable = false;
508-
509-
renderWindow.ContextLost = true;
510508
});
511509

512510
new Handler(Looper.MainLooper).Post(() =>
@@ -551,6 +549,7 @@ public void SurfaceDestroyed(ISurfaceHolder holder)
551549
AndroidRenderWindow.Instance.Mutate((renderWindow) =>
552550
{
553551
renderWindow.unavailable = true;
552+
renderWindow.contextLost = true;
554553
});
555554
}
556555

@@ -574,6 +573,35 @@ protected virtual string[] AdditionalLibraries()
574573
return outValue.ToArray();
575574
}
576575

576+
public override void OnWindowFocusChanged(bool hasFocus)
577+
{
578+
base.OnWindowFocusChanged(hasFocus);
579+
580+
if(hasFocus)
581+
{
582+
if(OperatingSystem.IsAndroidVersionAtLeast(35))
583+
{
584+
Window.InsetsController.Hide(WindowInsets.Type.SystemBars());
585+
Window.InsetsController.SystemBarsBehavior = (int)WindowInsetsControllerBehavior.ShowTransientBarsBySwipe;
586+
}
587+
else if (OperatingSystem.IsAndroidVersionAtLeast(30))
588+
{
589+
Window.SetDecorFitsSystemWindows(false);
590+
Window.InsetsController.Hide(WindowInsets.Type.SystemBars());
591+
Window.InsetsController.SystemBarsBehavior = (int)WindowInsetsControllerBehavior.ShowTransientBarsBySwipe;
592+
}
593+
else if (OperatingSystem.IsAndroidVersionAtLeast(19))
594+
{
595+
Window.DecorView.SystemUiFlags = SystemUiFlags.LayoutStable |
596+
SystemUiFlags.LayoutHideNavigation |
597+
SystemUiFlags.LayoutFullscreen |
598+
SystemUiFlags.HideNavigation |
599+
SystemUiFlags.Fullscreen |
600+
SystemUiFlags.ImmersiveSticky;
601+
}
602+
}
603+
}
604+
577605
protected override void OnCreate(Bundle? savedInstanceState)
578606
{
579607
base.OnCreate(savedInstanceState);
@@ -587,6 +615,10 @@ protected override void OnCreate(Bundle? savedInstanceState)
587615
Java.Lang.JavaSystem.LoadLibrary(library);
588616
}
589617

618+
RequestWindowFeature(WindowFeatures.NoTitle);
619+
620+
Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);
621+
590622
if(OperatingSystem.IsAndroidVersionAtLeast(23))
591623
{
592624
WindowManagerLayoutParams p = Window.Attributes;
@@ -621,22 +653,6 @@ protected override void OnCreate(Bundle? savedInstanceState)
621653
Window.Attributes.LayoutInDisplayCutoutMode = LayoutInDisplayCutoutMode.Always;
622654
}
623655

624-
if (OperatingSystem.IsAndroidVersionAtLeast(30))
625-
{
626-
Window.SetDecorFitsSystemWindows(false);
627-
Window.InsetsController.Hide(WindowInsets.Type.StatusBars() | WindowInsets.Type.NavigationBars());
628-
Window.InsetsController.SystemBarsBehavior = (int)WindowInsetsControllerBehavior.ShowTransientBarsBySwipe;
629-
}
630-
else if (OperatingSystem.IsAndroidVersionAtLeast(19))
631-
{
632-
Window.DecorView.SystemUiFlags = SystemUiFlags.HideNavigation |
633-
SystemUiFlags.LayoutHideNavigation |
634-
SystemUiFlags.LayoutFullscreen |
635-
SystemUiFlags.Fullscreen |
636-
SystemUiFlags.LayoutStable |
637-
SystemUiFlags.ImmersiveSticky;
638-
}
639-
640656
MessagePackInit.Initialize();
641657

642658
ResourceManager.instance.assetManager = Assets;

Engine/Core/Player/AppPlayer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ void HandleTypes<T>(string caption, Func<Type, bool> check, Action<T> callback)
237237

238238
renderWindow.OnUpdate = () =>
239239
{
240+
if((AppSettings.Current?.allowFullscreenSwitch ?? true) && Input.GetKey(KeyCode.LeftAlt) && Input.GetKeyDown(KeyCode.Enter))
241+
{
242+
Screen.SetResolution(Screen.Width, Screen.Height, Screen.WindowMode == WindowMode.Windowed ? WindowMode.BorderlessFullscreen : WindowMode.Windowed);
243+
}
244+
240245
SubsystemManager.instance.Update(SubsystemType.Update);
241246
};
242247

Engine/Core/Player/AppSettings.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ public enum ProfilingMode
174174
[Key(24)]
175175
public bool enableLighting = true;
176176

177+
/// <summary>
178+
/// Whether to allow switching to fullscreen and back with ALT+Enter
179+
/// </summary>
180+
[Key(25)]
181+
public bool allowFullscreenSwitch = true;
182+
177183
[IgnoreMember]
178184
public static AppSettings Default
179185
{

Engine/Core/Rendering/Mesh/MeshRenderSystem.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ public static void RenderMesh(Mesh mesh, Vector3 position, Quaternion rotation,
5656
return;
5757
}
5858

59-
if(mesh.changed)
59+
if(mesh.changed || (mesh.vertexBuffer?.Disposed ?? true) || (mesh.indexBuffer?.Disposed ?? true))
6060
{
61+
mesh.changed = true;
62+
6163
mesh.UploadMeshData();
6264
}
6365

Engine/Core/Rendering/Vertex/IndexBuffer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,16 @@ internal unsafe IndexBuffer(bgfx.IndexBufferHandle handle)
3838
{
3939
type = RenderBufferType.Normal;
4040
this.handle = handle;
41+
42+
ResourceManager.instance.userCreatedIndexBuffers.Add(new(this));
4143
}
4244

4345
internal unsafe IndexBuffer(bgfx.DynamicIndexBufferHandle handle)
4446
{
4547
type = RenderBufferType.Dynamic;
4648
dynamicHandle = handle;
49+
50+
ResourceManager.instance.userCreatedIndexBuffers.Add(new(this));
4751
}
4852

4953
internal unsafe IndexBuffer(bgfx.TransientIndexBuffer handle)

Engine/Core/Rendering/Vertex/VertexBuffer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ internal unsafe VertexBuffer(VertexLayout layout, bgfx.VertexBufferHandle handle
2929

3030
this.layout = layout;
3131
this.handle = handle;
32+
33+
ResourceManager.instance.userCreatedVertexBuffers.Add(new(this));
3234
}
3335

3436
internal unsafe VertexBuffer(VertexLayout layout, bgfx.DynamicVertexBufferHandle handle)
@@ -38,6 +40,8 @@ internal unsafe VertexBuffer(VertexLayout layout, bgfx.DynamicVertexBufferHandle
3840
this.layout = layout;
3941

4042
dynamicHandle = handle;
43+
44+
ResourceManager.instance.userCreatedVertexBuffers.Add(new(this));
4145
}
4246

4347
internal unsafe VertexBuffer(VertexLayout layout, bgfx.TransientVertexBuffer buffer)

Engine/Core/Resources/ResourceManager.cs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,19 @@ internal class ResourceManager
2424
internal Android.Content.Res.AssetManager assetManager;
2525
#endif
2626

27-
internal readonly Dictionary<string, Texture> cachedTextures = new();
28-
internal readonly Dictionary<string, Material> cachedMaterials = new();
29-
internal readonly Dictionary<string, Shader> cachedShaders = new();
30-
internal readonly Dictionary<string, Mesh> cachedMeshes = new();
31-
internal readonly Dictionary<string, AudioClip> cachedAudioClips = new();
32-
internal readonly Dictionary<string, MeshAsset> cachedMeshAssets = new();
33-
internal readonly Dictionary<string, FontAsset> cachedFonts = new();
34-
internal readonly Dictionary<string, IStapleAsset> cachedAssets = new();
35-
internal readonly Dictionary<string, Prefab> cachedPrefabs = new();
36-
internal readonly Dictionary<string, ResourcePak> resourcePaks = new();
37-
internal readonly List<WeakReference<Texture>> userCreatedTextures = new();
27+
internal readonly Dictionary<string, Texture> cachedTextures = [];
28+
internal readonly Dictionary<string, Material> cachedMaterials = [];
29+
internal readonly Dictionary<string, Shader> cachedShaders = [];
30+
internal readonly Dictionary<string, Mesh> cachedMeshes = [];
31+
internal readonly Dictionary<string, AudioClip> cachedAudioClips = [];
32+
internal readonly Dictionary<string, MeshAsset> cachedMeshAssets = [];
33+
internal readonly Dictionary<string, FontAsset> cachedFonts = [];
34+
internal readonly Dictionary<string, IStapleAsset> cachedAssets = [];
35+
internal readonly Dictionary<string, Prefab> cachedPrefabs = [];
36+
internal readonly Dictionary<string, ResourcePak> resourcePaks = [];
37+
internal readonly List<WeakReference<Texture>> userCreatedTextures = [];
38+
internal readonly List<WeakReference<VertexBuffer>> userCreatedVertexBuffers = [];
39+
internal readonly List<WeakReference<IndexBuffer>> userCreatedIndexBuffers = [];
3840

3941
public static string ShaderPrefix
4042
{
@@ -141,7 +143,26 @@ internal void Destroy(bool final)
141143
pair.Value?.Destroy();
142144
}
143145

144-
if(final)
146+
foreach(var item in userCreatedVertexBuffers)
147+
{
148+
if(item.TryGetTarget(out var buffer) && buffer.Disposed == false)
149+
{
150+
buffer.Destroy();
151+
}
152+
}
153+
154+
foreach (var item in userCreatedIndexBuffers)
155+
{
156+
if (item.TryGetTarget(out var buffer) && buffer.Disposed == false)
157+
{
158+
buffer.Destroy();
159+
}
160+
}
161+
162+
userCreatedVertexBuffers.Clear();
163+
userCreatedIndexBuffers.Clear();
164+
165+
if (final)
145166
{
146167
foreach (var pair in resourcePaks)
147168
{

Engine/Editor/Editors/Gizmo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static void WireframeBox(Vector3 position, Quaternion rotation, Vector3 s
4444

4545
if(wireCube == null)
4646
{
47-
wireCube = new Mesh
47+
wireCube = new Mesh(true, false)
4848
{
4949
MeshTopology = MeshTopology.LineStrip,
5050
};

0 commit comments

Comments
 (0)