Skip to content

Commit

Permalink
Fix nullptr errors in Windows VS builds (#18)
Browse files Browse the repository at this point in the history
* update to build without errors on windows

* update targets for lua5.4 (github action requirement)

* add correct project name

* move target for lua to after voxel-engine definition

* fix lua linking of github action for sandbox project

* use glfw os package on linux

* set m_Window to nullptr when window is destroyed
  • Loading branch information
arozx authored Feb 25, 2025
1 parent df59749 commit 9c73f42
Show file tree
Hide file tree
Showing 40 changed files with 9,314 additions and 64 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ vcpkg_installed/

cmake-build-debug/
cmake-build-release
/external/glfw/lib-vc2017
/external/glfw/lib-vc2013
/external/glfw/lib-static-ucrt
/external/glfw/lib-vc2019
/external/glfw/lib-vc2015
/external/glfw/lib-mingw-w64
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
path = external/imgui
url = https://github.com/ocornut/imgui.git
branch = docking
[submodule "external/glm"]
path = external/glm
url = https://github.com/g-truc/glm.git
2 changes: 1 addition & 1 deletion .vs/ProjectSettings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"CurrentProjectSetting": "x64-Release"
"CurrentProjectSetting": "x86 Release"
}
30 changes: 18 additions & 12 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
{
"DevContainersInfoBarHidden": true,
"ActiveTargetSystem": "Local Machine",
"OutputFoldersPerTargetSystem": {
"Local Machine": [
"out\\build\\x64-Debug",
"out\\install\\x64-Debug",
"out\\build\\x64-Release",
"out\\install\\x64-Release"
"out\\build\\x64-debug",
"out\\install\\x64-debug",
"out\\build\\x64-release",
"out\\install\\x64-release",
"out\\build\\x86-debug",
"out\\install\\x86-debug",
"out\\build\\x86-release",
"out\\install\\x86-release"
],
"WSL: Arch": [
"out\\build\\x64-Debug",
Expand All @@ -18,15 +23,16 @@
"out\\install\\x64-Release"
]
},
"ActiveTargetSystem": "Local Machine",
"ExpandedNodes": [
"\\sandbox",
"\\sandbox\\src",
"\\src",
"\\src\\Renderer",
"\\src\\Scene",
"\\src\\Scripting"
"",
"\\external\\glad",
"\\external\\glad\\include",
"\\external\\glad\\include\\glad",
"\\external\\glad\\include\\KHR",
"\\external\\glad\\src",
"\\external\\glfw",
"\\external\\glfw\\lib-vc2022"
],
"SelectedNode": "\\CMakeLists.txt",
"SelectedNode": "\\external",
"PreviewInSolutionExplorer": false
}
11 changes: 11 additions & 0 deletions .vs/launch.vs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
"version": "0.2.1",
"defaults": {},
"configurations": [
{
"type": "default",
"project": "CMakeLists.txt",
"projectTarget": "sandbox.exe (out\\build\\x64-Debug\\sandbox.exe)",
"name": "sandbox.exe",
"currentDir": "${workspaceRoot}",
"args": [],
"env": {
"PATH": "${env.PATH};${workspaceRoot}/out/build/x64-Debug"
}
},
{
"type": "default",
"project": "CMakeLists.txt",
Expand Down
93 changes: 69 additions & 24 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Export compile commands for SonarQube
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_CXX_COMPILER_LAUNCHER ccache)

if(WIN32)
set(CMAKE_TOOLCHAIN_FILE "$ENV{USERPROFILE}/vcpkg/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
else()
include_directories(/usr/include)
link_directories(/usr/lib)
endif()
# If using submodule or system GLM
set(GLM_DIR ${CMAKE_SOURCE_DIR}/external/glm)
include_directories(${GLM_DIR})

set(OpenGL_GL_PREFERENCE GLVND)

Expand All @@ -23,14 +18,19 @@ if (NOT OPENGL_FOUND)
message(FATAL_ERROR "OpenGL not found. Please install OpenGL package.")
endif()

find_package(glfw3 CONFIG REQUIRED)
if (NOT glfw3_FOUND)
message(FATAL_ERROR "GLFW not found. Please install GLFW3 package.")
endif()
# WINDOW SPECIFIC
if(WIN32)
set(GLFW_DIR ${CMAKE_SOURCE_DIR}/external/glfw)
set(GLFW_INCLUDE_DIR ${GLFW_DIR}/include)
set(GLFW_LIBRARY ${GLFW_DIR}/lib-vc2022/glfw3.lib)

find_package(Lua 5.4 REQUIRED)
if(NOT LUA_FOUND)
message(FATAL_ERROR "Lua >= 5.4 not found. Please install lua5.4 and lua5.4-dev packages.")
include_directories(${GLFW_INCLUDE_DIR})
link_directories(${GLFW_DIR}/lib-vc2022)
else()
find_package(glfw3 REQUIRED)
if(NOT glfw3_FOUND)
message(FATAL_ERROR "GLFW not found. Please install libglfw3 and libglfw3-dev packages.")
endif()
endif()

# Add GLAD
Expand All @@ -41,6 +41,30 @@ target_include_directories(glad PUBLIC
external/glad/include
)

if(WIN32)
# Set Lua paths manually if using local installation
set(LUA_DIR ${CMAKE_SOURCE_DIR}/external/lua)
set(LUA_INCLUDE_DIR ${LUA_DIR}/include)

# Create an imported library for Lua DLL
add_library(lua54 SHARED IMPORTED)
set_target_properties(lua54 PROPERTIES
IMPORTED_IMPLIB "${LUA_DIR}/lua54.lib"
IMPORTED_LOCATION "${LUA_DIR}/lua54.dll"
)

# Add the include directory
include_directories(${LUA_INCLUDE_DIR})

# Update LUA_LIBRARIES to use our imported target
set(LUA_LIBRARIES lua54)
else()
find_package(Lua 5.4 REQUIRED)
if(NOT LUA_FOUND)
message(FATAL_ERROR "Lua >= 5.4 not found. Please install lua5.4 and lua5.4-dev packages.")
endif()
endif()

# Add ImGui source files
set(IMGUI_DIR external/imgui)
set(IMGUI_SOURCES
Expand All @@ -55,6 +79,7 @@ set(IMGUI_SOURCES

# Engine library sources
set(ENGINE_SOURCES
src/pch.cpp
src/Application.cpp
src/Window/OpenGLWindow.cpp
src/Renderer/Buffer.cpp
Expand Down Expand Up @@ -105,11 +130,16 @@ target_include_directories(voxel-engine PUBLIC
${CMAKE_SOURCE_DIR}/external/sol2
)

# Required for Github Actions
if(NOT WIN32)
target_include_directories(voxel-engine PRIVATE /usr/include/lua5.4)
endif()

target_link_libraries(voxel-engine PUBLIC
glfw
$<IF:$<BOOL:${WIN32}>,${GLFW_LIBRARY},glfw>
OpenGL::GL
glad
${LUA_LIBRARIES} # Add Lua libraries
${LUA_LIBRARIES}
)

# Create sandbox executable
Expand All @@ -120,6 +150,28 @@ set(SANDBOX_SOURCES
add_executable(sandbox ${SANDBOX_SOURCES})
target_link_libraries(sandbox PRIVATE voxel-engine)

# Copy DLLs and assets after targets are created
if(WIN32)
add_custom_command(TARGET sandbox POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${LUA_DIR}/lua54.dll"
$<TARGET_FILE_DIR:sandbox>
)
endif()

if(MSVC)
# Enable Edit and Continue for Debug builds
string(REPLACE "/Zi" "/ZI" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")

# Or alternatively, you can add it directly:
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /ZI")
endif()

# Required for Github Actions
if(NOT WIN32)
target_include_directories(sandbox PRIVATE /usr/include/lua5.4)
endif()

add_custom_command(TARGET sandbox POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/sandbox/assets/scripts
Expand All @@ -130,10 +182,3 @@ add_custom_command(TARGET sandbox POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/assets $<TARGET_FILE_DIR:sandbox>/assets
)

# Enable precompiled headers - specify C++ explicitly
target_precompile_headers(voxel-engine
PRIVATE
$<INSTALL_INTERFACE:pch.h>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/pch.h>
)
1 change: 1 addition & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

25 changes: 24 additions & 1 deletion CMakeSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,30 @@
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "msvc_x64_x64" ],
"inheritEnvironments": [ "msvc_x64_x64" ]
},
{
"name": "x86-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "msvc_x86" ],
"variables": []
},
{
"name": "x86-Release",
"generator": "Ninja",
"configurationType": "RelWithDebInfo",
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "msvc_x86" ],
"variables": []
}
]
Expand Down
21 changes: 21 additions & 0 deletions CppProperties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"configurations": [
{
"inheritEnvironments": [
"msvc_x86"
],
"name": "x86-Debug",
"includePath": [
"${env.INCLUDE}",
"${workspaceRoot}\\**"
],
"defines": [
"WIN32",
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"intelliSenseMode": "windows-msvc-x86"
}
]
}
23 changes: 23 additions & 0 deletions external/glfw/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Copyright (c) 2002-2006 Marcus Geelnard

Copyright (c) 2006-2019 Camilla Löwy

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would
be appreciated but is not required.

2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.

3. This notice may not be removed or altered from any source
distribution.

76 changes: 76 additions & 0 deletions external/glfw/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# GLFW binaries for 32-bit Windows

This archive contains documentation, headers, pre-compiled static libraries,
import libraries and DLLs for GLFW 3.4.

Binaries for the following compilers are included

- Visual C++ 2022 (built with 17.9.0)
- Visual C++ 2019 (built with 16.11.34)
- Visual C++ 2017 (built with 15.9.60)
- Visual C++ 2015 (built with 14.0.25431.01)
- Visual C++ 2013 (built with 12.0.40629.00)
- MinGW-w64 (built with 13.2.0-win32-dwarf-msvcrt)

Binaries for the original MinGW distribution are no longer included. MinGW
appears to no longer be maintained and should not be used. The much more
capable MinGW-w64 project should be used instead. This release of GLFW can
still be compiled with the original MinGW if necessary, but future releases will
drop this support.


## Binaries for Visual C++

All binaries for Visual C++ 2017 and earlier are compatible with Windows XP, but
this is not supported by Visual C++ 2019. This support has been deprecated by
Microsoft and GLFW will also drop support for Windows XP in a future release.

### GLFW as a DLL

To use GLFW as a DLL, link against the `glfw3dll.lib` file for your
environment. This will add a load time dependency on `glfw3.dll`. The
remaining files in the same directory are not needed.

This DLL is built in release mode for the Multithreaded DLL runtime library.

There is also a GLFW DLL and import library pair in the `lib-static-ucrt`
directory. These are built with Visual C++ 2019 and the static Multithreaded
runtime library.

### GLFW as a static library

To use GLFW as a static library, link against `glfw3.lib` if your application
is using the Multithreaded DLL runtime library, or `glfw3_mt.lib` if it is
using the static Multithreaded runtime library. The remaining files in the same
directory are not needed.

The static libraries are built in release mode and do not contain debug
information but can still be linked with the debug versions of the runtime
library.


## Binaries for MinGW-w64

### GLFW as a DLL

To use GLFW as a DLL, link against the `libglfw3dll.a` file for your
environment. This will add a load time dependency on `glfw3.dll`. The
remaining files in the same directory are not needed.

The DLLs are built in release mode.

The DLLs depend on the `msvcrt.dll` C runtime library. There is also a GLFW
DLL and import library in the `lib-static-ucrt` directory that is built with
Visual C++ 2019 and statically linked against the UCRT.

All DLLs in this archive provide the same ABI and can be used as drop-in
replacements for one another, as long as the C runtime library they depend on is
available.

### GLFW as a static library

To use GLFW as a static library, link against the `libglfw3.a` file for your
environment. The other files in the same directory are not needed.

The library is built in release mode and do not contain debug information.

Loading

0 comments on commit 9c73f42

Please sign in to comment.