From a521887dddeb50d218dbba9dfb0a52a5eb9f50ba Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 11 Apr 2025 21:37:22 -0500 Subject: [PATCH] [studio,turbine] Add support for window icons --- deps/buildcore/scripts/file-to-cpp.py | 30 -------- src/nostalgia/studio/CMakeLists.txt | 1 + src/nostalgia/studio/icon.cpp | 39 +++++++++++ src/nostalgia/studio/ns_logo264.png | Bin 0 -> 275 bytes src/olympic/studio/applib/src/app.cpp | 4 ++ src/olympic/turbine/include/turbine/gfx.hpp | 2 + src/olympic/turbine/src/glfw/CMakeLists.txt | 1 + src/olympic/turbine/src/glfw/gfx.cpp | 29 ++++++++ util/scripts/file-to-cpp.py | 73 ++++++++++++++++++++ 9 files changed, 149 insertions(+), 30 deletions(-) delete mode 100755 deps/buildcore/scripts/file-to-cpp.py create mode 100644 src/nostalgia/studio/icon.cpp create mode 100644 src/nostalgia/studio/ns_logo264.png create mode 100755 util/scripts/file-to-cpp.py diff --git a/deps/buildcore/scripts/file-to-cpp.py b/deps/buildcore/scripts/file-to-cpp.py deleted file mode 100755 index 057976af..00000000 --- a/deps/buildcore/scripts/file-to-cpp.py +++ /dev/null @@ -1,30 +0,0 @@ -#! /usr/bin/env python3 - -# -# Copyright 2016 - 2025 gary@drinkingtea.net -# -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. -# - -import argparse -import sys - - -def main() -> int: - parser = argparse.ArgumentParser() - parser.add_argument('--file', help='path to file') - parser.add_argument('--out-cpp', help='path to output cpp file') - parser.add_argument('--out-hpp', help='path to output hpp file') - parser.add_argument('--namespace', help='path to output hpp file') - args = parser.parse_args() - return 0 - - -if __name__ == '__main__': - try: - err = main() - sys.exit(err) - except KeyboardInterrupt: - sys.exit(1) diff --git a/src/nostalgia/studio/CMakeLists.txt b/src/nostalgia/studio/CMakeLists.txt index fdd98efb..859eecb1 100644 --- a/src/nostalgia/studio/CMakeLists.txt +++ b/src/nostalgia/studio/CMakeLists.txt @@ -1,6 +1,7 @@ add_executable( NostalgiaStudio WIN32 MACOSX_BUNDLE ns.rc + icon.cpp ) target_link_libraries( diff --git a/src/nostalgia/studio/icon.cpp b/src/nostalgia/studio/icon.cpp new file mode 100644 index 00000000..b11f3c77 --- /dev/null +++ b/src/nostalgia/studio/icon.cpp @@ -0,0 +1,39 @@ + +#include + +namespace studio { + +static constexpr ox::Array WindowIconData { + 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, + 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x01, 0x08, + 0x00, 0x00, 0x01, 0x08, 0x02, 0x03, 0x00, 0x00, 0x00, 0xde, + 0x98, 0x82, 0x6c, 0x00, 0x00, 0x00, 0x0c, 0x50, 0x4c, 0x54, + 0x45, 0x00, 0x00, 0x28, 0xb0, 0xb0, 0xb0, 0x40, 0x40, 0xa0, + 0xd8, 0xd8, 0xd8, 0x8f, 0x37, 0x17, 0x71, 0x00, 0x00, 0x00, + 0xc2, 0x49, 0x44, 0x41, 0x54, 0x78, 0x01, 0xed, 0xd5, 0x41, + 0x0e, 0x82, 0x30, 0x10, 0x46, 0x61, 0x30, 0x61, 0xe3, 0xbe, + 0xf7, 0xe1, 0x08, 0x2c, 0xfa, 0xdf, 0xc7, 0xa3, 0xb8, 0xf6, + 0x12, 0xde, 0xcc, 0x3a, 0x1a, 0x8b, 0x62, 0x63, 0x62, 0x42, + 0x88, 0xd3, 0xfa, 0xde, 0x66, 0x42, 0x98, 0xf9, 0x58, 0xd2, + 0x11, 0xfd, 0x71, 0x41, 0x56, 0x84, 0x80, 0x68, 0x91, 0x18, + 0xa4, 0xf1, 0x36, 0x7b, 0x69, 0x82, 0x80, 0x68, 0x8b, 0x58, + 0xde, 0x65, 0x0d, 0x02, 0x02, 0x02, 0x02, 0xc2, 0x3f, 0x41, + 0x44, 0x44, 0x44, 0x44, 0x44, 0xdb, 0x16, 0x64, 0xc5, 0x55, + 0xbb, 0x10, 0x10, 0x4e, 0x89, 0x41, 0x1a, 0xcb, 0xcd, 0xfd, + 0x69, 0xee, 0x58, 0xbe, 0xed, 0xa5, 0x09, 0x02, 0xc2, 0x3f, + 0xf1, 0xc6, 0x3d, 0xda, 0xe5, 0xeb, 0x94, 0x2e, 0xf7, 0x79, + 0x28, 0x77, 0xe6, 0x2f, 0x43, 0x40, 0xd4, 0x49, 0x9c, 0x53, + 0xca, 0x14, 0x04, 0x44, 0x9d, 0x84, 0x8f, 0xff, 0x08, 0x04, + 0xc4, 0x76, 0x84, 0x15, 0x64, 0xc5, 0xee, 0xab, 0x3e, 0xec, + 0x42, 0x40, 0xf8, 0x25, 0x88, 0x88, 0x88, 0x88, 0x88, 0xe8, + 0xb7, 0xf5, 0xd2, 0xf4, 0x7c, 0x1a, 0xa4, 0x11, 0x02, 0x02, + 0x02, 0x02, 0xa2, 0x0e, 0xe2, 0xe5, 0x6e, 0xa9, 0x41, 0x40, + 0x34, 0x42, 0x58, 0x41, 0x56, 0xec, 0xd6, 0x04, 0x01, 0xe1, + 0x97, 0x20, 0xaa, 0xbb, 0x2b, 0xd0, 0x36, 0xee, 0x24, 0x18, + 0x4b, 0x42, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, + 0x44, 0xae, 0x42, 0x60, 0x82, +}; + +ox::SpanView WindowIcon() noexcept { return WindowIconData; } + +} diff --git a/src/nostalgia/studio/ns_logo264.png b/src/nostalgia/studio/ns_logo264.png new file mode 100644 index 0000000000000000000000000000000000000000..dce287ded1a659317c0c0cb439b170014a3ca5de GIT binary patch literal 275 zcmeAS@N?(olHy`uVBq!ia0y~yVB`Q{CT5_>y%|k8K#C{8C&ZP3L1V*)4Gsf=0 z&1uHZh)aTh3mwB+8ZstIEcs|wo!Qio(H(Iwm6r(!4%s`pfWU3Bl%t`$1qA#$9i>j6 z{hRqNV&B`PoIvT1$82vseLY3*d!R&%x&ra&%tt}tVJ~C=)2W+ zZ=4){sCIzBi|t=8eW{UJA_y{m5#OVyc`F?qOq?Pd!`@tDVSG4UVbyN!3ufui.handleKeyEvent(key, down); } +[[nodiscard]] +ox::SpanView WindowIcon() noexcept; + static ox::Error runApp( ox::StringViewCR appName, ox::StringViewCR projectDataDir, ox::UPtr &&fs) noexcept { OX_REQUIRE_M(ctx, turbine::init(std::move(fs), appName)); + oxLogError(turbine::setWindowIcon(*ctx, WindowIcon())); turbine::setWindowTitle(*ctx, keelCtx(*ctx).appName); turbine::setKeyEventHandler(*ctx, keyEventHandler); turbine::setRefreshWithin(*ctx, 0); diff --git a/src/olympic/turbine/include/turbine/gfx.hpp b/src/olympic/turbine/include/turbine/gfx.hpp index b6183907..4356423e 100644 --- a/src/olympic/turbine/include/turbine/gfx.hpp +++ b/src/olympic/turbine/include/turbine/gfx.hpp @@ -26,6 +26,8 @@ void removeDrawer(Context &ctx, Drawer *cd) noexcept; ox::Error initGfx(Context &ctx) noexcept; +ox::Error setWindowIcon(Context &ctx, ox::SpanView iconPng) noexcept; + void setWindowTitle(Context &ctx, ox::StringViewCR title) noexcept; void focusWindow(Context &ctx) noexcept; diff --git a/src/olympic/turbine/src/glfw/CMakeLists.txt b/src/olympic/turbine/src/glfw/CMakeLists.txt index 47f7885e..4eb91b4f 100644 --- a/src/olympic/turbine/src/glfw/CMakeLists.txt +++ b/src/olympic/turbine/src/glfw/CMakeLists.txt @@ -21,4 +21,5 @@ target_link_libraries( glad glfw imgui + lodepng ) diff --git a/src/olympic/turbine/src/glfw/gfx.cpp b/src/olympic/turbine/src/glfw/gfx.cpp index 40f3b103..de601f74 100644 --- a/src/olympic/turbine/src/glfw/gfx.cpp +++ b/src/olympic/turbine/src/glfw/gfx.cpp @@ -4,6 +4,7 @@ #include #include +#include #if TURBINE_USE_IMGUI #include #include @@ -228,6 +229,34 @@ ox::Error initGfx(Context &ctx) noexcept { return {}; } +struct IconData { + std::vector pixels; + int w{}, h{}; +}; + +[[nodiscard]] +static ox::Result toGlfwImgPixels(ox::SpanView iconPng) noexcept { + ox::Result out; + unsigned w{}, h{}; + if (lodepng::decode(out.value.pixels, w, h, iconPng.data(), LodePNGColorType::LCT_RGBA)) { + return ox::Error{1, "unable to decode window icon PNG data"}; + } + out.value.w = static_cast(w); + out.value.h = static_cast(h); + return out; +} + +ox::Error setWindowIcon(Context &ctx, ox::SpanView iconPng) noexcept { + OX_REQUIRE_M(icon, toGlfwImgPixels(iconPng)); + GLFWimage const img { + .pixels = icon.pixels.data(), + .width = icon.w, + .height = icon.h, + }; + glfwSetWindowIcon(ctx.window, 1, &img); + return {}; +} + void setWindowTitle(Context &ctx, ox::StringViewCR title) noexcept { auto cstr = ox_malloca(title.bytes() + 1, char); ox::strncpy(cstr.get(), title.data(), title.bytes()); diff --git a/util/scripts/file-to-cpp.py b/util/scripts/file-to-cpp.py new file mode 100755 index 00000000..c0444634 --- /dev/null +++ b/util/scripts/file-to-cpp.py @@ -0,0 +1,73 @@ +#! /usr/bin/env python3 + +# +# Copyright 2016 - 2025 gary@drinkingtea.net +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# + +import argparse +import sys + + +def write_txt_file(path: str, txt: str): + with open(path, "w") as f: + f.write(txt) + print(f'Wrote {path}') + + +def file_to_hex(path: str, line_prefix: str) -> tuple[str, int]: + with open(path, 'rb') as f: + out = line_prefix + data = f.read() + i = 1 + for b in data: + out += f"{b:#0{4}x}," + if i % 10 == 0: + out += '\n\t' + else: + out += ' ' + i += 1 + return out[:-1], len(data) + + +def file_to_cpp(path: str, var_name: str, namespace: str, cpp_path: str, hpp_path: str): + if len(namespace) > 0: + push_ns = f'namespace {namespace} {{\n' + pop_ns = '\n}\n' + data, dataLen = file_to_hex(path, "\t") + cpp = '\n#include \n\n' + cpp = '\n#include \n\n' + cpp += push_ns + cpp += f'\nstatic constexpr ox::Array {var_name}Data {{\n{data}\n}};\n' + cpp += f'\nox::SpanView {var_name}() noexcept {{ return {var_name}Data; }}\n' + cpp += pop_ns + write_txt_file(cpp_path, cpp) + if hpp_path is not None: + hpp = '\n#include \n\n' + hpp += push_ns + hpp += f'\n[[nodiscard]]\nox::SpanView {var_name}() noexcept;\n' + hpp += pop_ns + write_txt_file(hpp_path, hpp) + + +def main() -> int: + parser = argparse.ArgumentParser() + parser.add_argument('--file', help='path to file', required=True) + parser.add_argument('--cpp', help='path to output cpp file', required=True) + parser.add_argument('--hpp', help='path to output hpp file') + parser.add_argument('--namespace', help='path to output hpp file') + parser.add_argument('--var-name', help='path to output hpp file', required=True) + args = parser.parse_args() + file_to_cpp(args.file, args.var_name, args.namespace, args.cpp, args.hpp) + return 0 + + +if __name__ == '__main__': + try: + err = main() + sys.exit(err) + except KeyboardInterrupt: + sys.exit(1)