Skip to content

Commit

Permalink
Set runtime window icon
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz-h committed Nov 16, 2023
1 parent 716db32 commit b6f16dc
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 2 deletions.
10 changes: 10 additions & 0 deletions frontend/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@ if (MSVC)
set(resource_files "${CMAKE_CURRENT_SOURCE_DIR}/extra/metadata.rc")
endif ()

# Icons
find_package(CMakeRC CONFIG REQUIRED)
set(icons
"${CMAKE_CURRENT_SOURCE_DIR}/extra/icon_016.bin"
"${CMAKE_CURRENT_SOURCE_DIR}/extra/icon_032.bin"
"${CMAKE_CURRENT_SOURCE_DIR}/extra/icon_048.bin"
"${CMAKE_CURRENT_SOURCE_DIR}/extra/icon_256.bin")
cmrc_add_resource_library(megamol_icons WHENCE "${CMAKE_CURRENT_SOURCE_DIR}/extra" ${icons})

# Add target
add_executable(megamol ${header_files} ${source_files} "${resource_files}")
target_include_directories(megamol PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src")
target_link_libraries(megamol
PRIVATE
core
frontend_services
megamol_icons
cxxopts::cxxopts
${CMAKE_DL_LIBS})

Expand Down
Binary file added frontend/main/extra/icon_016.bin
Binary file not shown.
Binary file added frontend/main/extra/icon_032.bin
Binary file not shown.
Binary file added frontend/main/extra/icon_048.bin
Binary file not shown.
Binary file added frontend/main/extra/icon_256.bin
Binary file not shown.
9 changes: 9 additions & 0 deletions frontend/main/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* All rights reserved.
*/

#include <cmrc/cmrc.hpp>

#include "CLIConfigParsing.h"
#include "CUDA_Service.hpp"
#include "Command_Service.hpp"
Expand Down Expand Up @@ -50,6 +52,8 @@ static void log_error(std::string const& text) {

void loadPlugins(megamol::frontend_resources::PluginsResource& pluginsRes);

CMRC_DECLARE(megamol_icons);

int main(const int argc, const char** argv) {
#ifdef MEGAMOL_USE_TRACY
ZoneScoped;
Expand Down Expand Up @@ -98,6 +102,11 @@ int main(const int argc, const char** argv) {
openglConfig.windowPlacement.noCursor = config.window_mode & RuntimeConfig::WindowMode::nocursor;
openglConfig.windowPlacement.hidden = config.window_mode & RuntimeConfig::WindowMode::hidden;
openglConfig.forceWindowSize = config.force_window_size;
static const auto icons_fs = cmrc::megamol_icons::get_filesystem();
openglConfig.windowIcons.push_back({16, 16, icons_fs.open("icon_016.bin").begin()});
openglConfig.windowIcons.push_back({32, 32, icons_fs.open("icon_032.bin").begin()});
openglConfig.windowIcons.push_back({48, 48, icons_fs.open("icon_048.bin").begin()});
openglConfig.windowIcons.push_back({256, 256, icons_fs.open("icon_256.bin").begin()});
gl_service.setPriority(2);

megamol::frontend::GUI_Service gui_service;
Expand Down
12 changes: 10 additions & 2 deletions frontend/services/opengl_glfw/OpenGL_GLFW_Service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#pragma once

#include <memory>
#include <vector>

#include "AbstractFrontendService.hpp"
#include "Framebuffer_Events.h"
Expand All @@ -30,6 +31,12 @@ struct WindowPlacement {
bool hidden = false;
};

struct WindowIcon {
int width;
int height;
const char* pixels;
};

class OpenGL_GLFW_Service final : public AbstractFrontendService {
using KeyboardEvents = megamol::frontend_resources::KeyboardEvents;
using MouseEvents = megamol::frontend_resources::MouseEvents;
Expand All @@ -43,8 +50,9 @@ class OpenGL_GLFW_Service final : public AbstractFrontendService {
int versionMinor = 6;
std::string windowTitlePrefix = "MegaMol";
WindowPlacement windowPlacement{}; // window position, glfw creation hints // TODO: sane defaults??
bool enableKHRDebug = true; // max error reporting
bool enableVsync = false; // max frame rate
std::vector<WindowIcon> windowIcons{};
bool enableKHRDebug = true; // max error reporting
bool enableVsync = false; // max frame rate
bool glContextCoreProfile = false;
bool forceWindowSize = false;
};
Expand Down
12 changes: 12 additions & 0 deletions frontend/services/opengl_glfw/gl/OpenGL_GLFW_Service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,18 @@ bool OpenGL_GLFW_Service::init(const Config& config) {
if (m_pimpl->config.windowPlacement.pos || m_pimpl->config.windowPlacement.fullScreen)
::glfwSetWindowPos(window_ptr, m_pimpl->config.windowPlacement.x, m_pimpl->config.windowPlacement.y);

if (!m_pimpl->config.windowIcons.empty()) {
std::vector<GLFWimage> images;
for (const auto& icon : m_pimpl->config.windowIcons) {
// const_cast is super annoying, but making an extra copy of the data just to remove const would be not
// better. Cast is required because GLFWimage member is defined non const, but glfwSetWindowIcon does only
// read the data and makes an internal copy anyway.
images.push_back(
{icon.width, icon.height, reinterpret_cast<unsigned char*>(const_cast<char*>(icon.pixels))});
}
glfwSetWindowIcon(window_ptr, images.size(), images.data());
}

register_glfw_callbacks();

int vsync = (m_pimpl->config.enableVsync) ? 1 : 0;
Expand Down

0 comments on commit b6f16dc

Please sign in to comment.