FreeGlut MacOS Support

This commit is contained in:
Colton Staiduhar
2026-02-17 21:22:16 -05:00
parent a3bac40258
commit ad2175b9a6
2 changed files with 342 additions and 271 deletions

View File

@@ -10,6 +10,9 @@ set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
# Declare Project where "GSM_TEMPLATE" is the project name
project(GSM_TEMPLATE LANGUAGES C CXX)
# Use FreeGlut on Macos (Requires X11 to be installed, but doesn't use it)
option(USE_FREEGLUT "Use FreeGLUT on Mac (ignored on Windows)" ON)
# Allow cmake to fetch web repositories
include(FetchContent)
@@ -41,13 +44,28 @@ FetchContent_MakeAvailable(stb_image)
# If using the Windows compiler
if (MSVC)
if (MSVC OR USE_FREEGLUT)
if (APPLE)
find_path(XQUARTZ_INCLUDE_DIR X11/Xlib.h
PATHS /opt/X11/include
NO_DEFAULT_PATH
)
if(NOT XQUARTZ_INCLUDE_DIR)
message(FATAL_ERROR "To use FreeGLUT on Mac, you need XQuartz installed. Please install it from https://www.xquartz.org/ or set the USE_FREEGLUT cache option to OFF")
endif()
endif()
set(CMAKE_POLICY_VERSION_MINIMUM 3.16)
# Download freeglut
FetchContent_Declare(
freeglut
GIT_REPOSITORY https://github.com/freeglut/freeglut.git
GIT_TAG "master"
GIT_TAG "v3.8.0"
GIT_SHALLOW TRUE
GIT_PROGRESS ON
)
@@ -56,6 +74,11 @@ if (MSVC)
set(FREEGLUT_BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
set(FREEGLUT_BUILD_STATIC_LIBS ON CACHE BOOL "" FORCE)
# Force Cocca on Mac
if(APPLE)
set(FREEGLUT_USE_X11 OFF CACHE BOOL "" FORCE)
set(FREEGLUT_COCOA ON CACHE BOOL "" FORCE)
endif()
FetchContent_MakeAvailable(freeglut)
@@ -76,8 +99,8 @@ endif()
# Tell Cmake to find the locations of libraries
find_package(OpenGL REQUIRED)
# Only require glut if on macos
if (APPLE)
# Only require glut if on macos and not using freeglut
if (APPLE AND NOT USE_FREEGLUT)
find_package(GLUT REQUIRED)
endif()
# Define MY_SOURCES to be a list of all the source files for my game
@@ -117,6 +140,37 @@ target_sources("${CMAKE_PROJECT_NAME}" PRIVATE ${MY_SOURCES})
# If on mac
if (APPLE)
# FreeGlut for mac
if (USE_FREEGLUT)
target_sources("${CMAKE_PROJECT_NAME}" PRIVATE "${glad_SOURCE_DIR}/src/glad.c")
target_include_directories("${CMAKE_PROJECT_NAME}" PUBLIC "${glad_SOURCE_DIR}/include")
# Add our include files
target_include_directories("${CMAKE_PROJECT_NAME}" PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/include/"
"${freeglut_SOURCE_DIR}/include"
"${glad_SOURCE_DIR}/include"
)
target_compile_definitions("${CMAKE_PROJECT_NAME}" PUBLIC use_freeglut=TRUE)
target_link_libraries("${CMAKE_PROJECT_NAME}"
PRIVATE
OpenGL::GL
freeglut_static
"-framework Cocoa"
"-framework IOKit"
"-framework CoreVideo"
"-lpthread"
stb_image
miniaudio
)
# Native GLUT for Mac
else()
# Add our include files
target_include_directories("${CMAKE_PROJECT_NAME}" PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include/")
@@ -138,6 +192,7 @@ target_link_libraries("${CMAKE_PROJECT_NAME}"
stb_image
miniaudio
)
endif()
# If on windows