Skip to content

Commit

Permalink
[GLFW] Switch to binary application icon file
Browse files Browse the repository at this point in the history
Can be loaded without any image loading library.
  • Loading branch information
hyazinthh committed Jul 16, 2024
1 parent 8cfddbf commit 4001c51
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<OutputPath>..\..\..\bin\Release\</OutputPath>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="resources\aardvark.png" />
<EmbeddedResource Include="resources\aardvark-icon.bin" />
<Compile Include="GLFW.fs" />
<Compile Include="Application.fs" />
</ItemGroup>
Expand Down
70 changes: 34 additions & 36 deletions src/Application/Aardvark.Application.Slim/GLFW.fs
Original file line number Diff line number Diff line change
Expand Up @@ -383,44 +383,42 @@ type WindowConfig =
WindowConfig.transparent = false
WindowConfig.samples = 1 }

module IconLoader =

type private Self = Self
let private getIcon'() =
let sizes = Array.sortDescending [| 16; 24; 32; 48; 64; 128; 256 |]
let ass = typeof<Self>.Assembly
let name = ass.GetManifestResourceNames() |> Array.find (fun n -> n.EndsWith "aardvark.png")

let img =
use src = ass.GetManifestResourceStream name
PixImage.Load(src).ToPixImage<byte>(Col.Format.RGBA)

let levels =
let mutable last = img
sizes |> Array.map (fun s ->
if s = last.Size.X && s = last.Size.Y then
last :> PixImage
else
let dst = PixImage<byte>(Col.Format.RGBA, V2i(s,s))
NativeVolume.using last.Volume (fun src ->
NativeVolume.using dst.Volume (fun dst ->
NativeVolume.blit src dst
)
)

last <- dst
dst :> PixImage
)

PixImageMipMap levels
module internal IconLoader =
open System.IO

let private tryLoad (name: string) =
try
let asm = typeof<WindowConfig>.Assembly
let resourceName = asm.GetManifestResourceNames() |> Array.tryFind (String.endsWith name)

match resourceName with
| Some rn ->
let pix =
use rs = asm.GetManifestResourceStream(rn)
use ms = new MemoryStream()
rs.CopyTo(ms)
let volume = Volume<uint8>(ms.ToArray(), 0L, V3l(256L, 256L, 4L), V3l(4L, 1024L, 1L))
PixImage<uint8>(Col.Format.RGBA, volume)

Success <| PixImageMipMap.Create(pix, ImageInterpolation.Linear)

| _ ->
Error $"Could not find resource with name '{name}'."

with e ->
Error e.Message

let private icon =
lazy (
match tryLoad "aardvark-icon.bin" with
| Success icon -> Some icon
| Error err ->
Log.warn "[GLFW] Failed to load icon: %s" err
None
)

let getIcon() =
try
getIcon'() |> Some
with e ->
Log.warn "could not load icon. %A" e.Message
None

icon.Value

type GlfwGamepad() =
let a = cval false
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit 4001c51

Please sign in to comment.