[studio,turbine] Add support for window icons
Some checks are pending
Build / build (push) Waiting to run

This commit is contained in:
Gary Talent 2025-04-11 21:37:22 -05:00
parent 5ca7e2f226
commit a521887ddd
9 changed files with 149 additions and 30 deletions

View File

@ -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)

View File

@ -1,6 +1,7 @@
add_executable(
NostalgiaStudio WIN32 MACOSX_BUNDLE
ns.rc
icon.cpp
)
target_link_libraries(

View File

@ -0,0 +1,39 @@
#include <ox/std/span.hpp>
namespace studio {
static constexpr ox::Array<uint8_t, 275> 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<uint8_t> WindowIcon() noexcept { return WindowIconData; }
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 275 B

View File

@ -32,11 +32,15 @@ static void keyEventHandler(turbine::Context &ctx, turbine::Key key, bool down)
sctx->ui.handleKeyEvent(key, down);
}
[[nodiscard]]
ox::SpanView<uint8_t> WindowIcon() noexcept;
static ox::Error runApp(
ox::StringViewCR appName,
ox::StringViewCR projectDataDir,
ox::UPtr<ox::FileSystem> &&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);

View File

@ -26,6 +26,8 @@ void removeDrawer(Context &ctx, Drawer *cd) noexcept;
ox::Error initGfx(Context &ctx) noexcept;
ox::Error setWindowIcon(Context &ctx, ox::SpanView<uint8_t> iconPng) noexcept;
void setWindowTitle(Context &ctx, ox::StringViewCR title) noexcept;
void focusWindow(Context &ctx) noexcept;

View File

@ -21,4 +21,5 @@ target_link_libraries(
glad
glfw
imgui
lodepng
)

View File

@ -4,6 +4,7 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <lodepng.h>
#if TURBINE_USE_IMGUI
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
@ -228,6 +229,34 @@ ox::Error initGfx(Context &ctx) noexcept {
return {};
}
struct IconData {
std::vector<uint8_t> pixels;
int w{}, h{};
};
[[nodiscard]]
static ox::Result<IconData> toGlfwImgPixels(ox::SpanView<uint8_t> iconPng) noexcept {
ox::Result<IconData> 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<int>(w);
out.value.h = static_cast<int>(h);
return out;
}
ox::Error setWindowIcon(Context &ctx, ox::SpanView<uint8_t> 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());

73
util/scripts/file-to-cpp.py Executable file
View File

@ -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 <ox/std/array.hpp>\n\n'
cpp = '\n#include <ox/std/span.hpp>\n\n'
cpp += push_ns
cpp += f'\nstatic constexpr ox::Array<uint8_t, {dataLen}> {var_name}Data {{\n{data}\n}};\n'
cpp += f'\nox::SpanView<uint8_t> {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 <ox/std/span.hpp>\n\n'
hpp += push_ns
hpp += f'\n[[nodiscard]]\nox::SpanView<uint8_t> {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)