Skip to content

Commit

Permalink
Support for setting the Window icon.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomspilman committed Sep 9, 2024
1 parent 67054df commit f2b0c2f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions MonoGame.Framework/Platform/Native/GameWindow.Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ public unsafe NativeGameWindow(NativeGamePlatform platform, bool primaryWindow)

_windows[(nint)_handle] = this;

var icon = AssemblyHelper.GetDefaultWindowIcon();
if (icon != null)
MGP.Window_SetIconBitmap(_handle, icon, icon.Length);

Handle = MGP.Window_GetNativeHandle(_handle);
}

Expand Down
3 changes: 3 additions & 0 deletions MonoGame.Framework/Platform/Native/Platform.Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ internal static unsafe partial class MGP
[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGP_Window_Destroy", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Window_Destroy(MGP_Window* window);

[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGP_Window_SetIconBitmap", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Window_SetIconBitmap(MGP_Window* window, byte[] icon, int length);

[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGP_Window_GetNativeHandle", StringMarshalling = StringMarshalling.Utf8)]
public static partial nint Window_GetNativeHandle(MGP_Window* window);

Expand Down
25 changes: 25 additions & 0 deletions MonoGame.Framework/Platform/Utilities/AssemblyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// file 'LICENSE.txt', which is part of this source code package.

using System;
using System.IO;
using System.Reflection;

namespace MonoGame.Framework.Utilities
Expand Down Expand Up @@ -37,5 +38,29 @@ public static string GetDefaultWindowTitle()

return windowTitle;
}

public static byte[] GetDefaultWindowIcon()
{
var entryAssembly = Assembly.GetEntryAssembly();

if (entryAssembly != null)
{
using (
var stream =
entryAssembly.GetManifestResourceStream(entryAssembly.GetName().Name + ".Icon.bmp") ??
entryAssembly.GetManifestResourceStream("Icon.bmp") ??
typeof(AssemblyHelper).Assembly.GetManifestResourceStream("MonoGame.bmp"))
{
if (stream != null)
using (var br = new BinaryReader(stream))
{
var bytes = br.ReadBytes((int)stream.Length);
return bytes;
}
}
}

return null;
}
}
}
1 change: 1 addition & 0 deletions src/monogame/include/csharp_MGP.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ MG_EXPORT mgbool MGP_Platform_BeforeUpdate(MGP_Platform* platform);
MG_EXPORT mgbool MGP_Platform_BeforeDraw(MGP_Platform* platform);
MG_EXPORT MGP_Window* MGP_Window_Create(MGP_Platform* platform, mgint width, mgint height, const char* title);
MG_EXPORT void MGP_Window_Destroy(MGP_Window* window);
MG_EXPORT void MGP_Window_SetIconBitmap(MGP_Window* window, mgbyte* icon, mgint length);
MG_EXPORT void* MGP_Window_GetNativeHandle(MGP_Window* window);
MG_EXPORT mgbool MGP_Window_GetAllowUserResizing(MGP_Window* window);
MG_EXPORT void MGP_Window_SetAllowUserResizing(MGP_Window* window, mgbool allow);
Expand Down
10 changes: 10 additions & 0 deletions src/monogame/sdl/MGP_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,16 @@ void MGP_Window_Destroy(MGP_Window* window)
delete window;
}

void MGP_Window_SetIconBitmap(MGP_Window* window, mgbyte* icon, mgint length)
{
assert(window != nullptr);

auto src = SDL_RWFromConstMem(icon, length);
auto surface = SDL_LoadBMP_RW(src, 1);

SDL_SetWindowIcon(window->window, surface);
}

void* MGP_Window_GetNativeHandle(MGP_Window* window)
{
assert(window != nullptr);
Expand Down

0 comments on commit f2b0c2f

Please sign in to comment.