basic template

This commit is contained in:
2026-01-27 17:02:07 +00:00
parent a3d6407f02
commit 0736a73254
10 changed files with 75 additions and 24 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+26 -8
View File
@@ -61,6 +61,7 @@ file(GLOB_RECURSE PROJECT_HEADERS
${CMAKE_SOURCE_DIR}/source/*.h ${CMAKE_SOURCE_DIR}/source/*.h
) )
# Combine all sources # Combine all sources
set(ALL_SOURCES ${PROJECT_SOURCES} ${LIBRARY_SOURCES}) set(ALL_SOURCES ${PROJECT_SOURCES} ${LIBRARY_SOURCES})
@@ -86,6 +87,23 @@ target_include_directories(${EXECUTABLE_NAME} PRIVATE
${CMAKE_SOURCE_DIR}/libraries ${CMAKE_SOURCE_DIR}/libraries
) )
# 1. Find the GLFW library in your lib folder
find_library(GLFW_LIB NAMES glfw glfw3 PATHS "${CMAKE_SOURCE_DIR}/libraries" NO_DEFAULT_PATH)
# 2. Link GLFW and the required Apple Frameworks
if(APPLE)
target_link_libraries(${EXECUTABLE_NAME} PRIVATE
${GLFW_LIB}
"-framework Cocoa"
"-framework IOKit"
"-framework CoreVideo"
)
else()
# For Windows/Linux
target_link_libraries(${EXECUTABLE_NAME} PRIVATE ${GLFW_LIB})
endif()
# Compiler-specific flags # Compiler-specific flags
if(MSVC) if(MSVC)
target_compile_options(${EXECUTABLE_NAME} PRIVATE target_compile_options(${EXECUTABLE_NAME} PRIVATE
@@ -95,16 +113,16 @@ if(MSVC)
/Zc:inline # Remove unreferenced COMDAT /Zc:inline # Remove unreferenced COMDAT
/WX- # Don't treat warnings as errors by default /WX- # Don't treat warnings as errors by default
) )
if(ENABLE_STATIC_ANALYSIS) if(ENABLE_STATIC_ANALYSIS)
target_compile_options(${EXECUTABLE_NAME} PRIVATE /analyze) target_compile_options(${EXECUTABLE_NAME} PRIVATE /analyze)
endif() endif()
# MSVC debug flags # MSVC debug flags
if(CMAKE_BUILD_TYPE STREQUAL "Debug") if(CMAKE_BUILD_TYPE STREQUAL "Debug")
target_compile_options(${EXECUTABLE_NAME} PRIVATE /Zi /Od) target_compile_options(${EXECUTABLE_NAME} PRIVATE /Zi /Od)
endif() endif()
else() # GCC/Clang else() # GCC/Clang
target_compile_options(${EXECUTABLE_NAME} PRIVATE target_compile_options(${EXECUTABLE_NAME} PRIVATE
-Wall -Wall
@@ -120,7 +138,7 @@ else() # GCC/Clang
-Wformat=2 -Wformat=2
-Wnull-dereference -Wnull-dereference
) )
# Additional warnings for static analysis # Additional warnings for static analysis
if(ENABLE_STATIC_ANALYSIS) if(ENABLE_STATIC_ANALYSIS)
target_compile_options(${EXECUTABLE_NAME} PRIVATE target_compile_options(${EXECUTABLE_NAME} PRIVATE
@@ -129,14 +147,14 @@ else() # GCC/Clang
-Wold-style-cast -Wold-style-cast
) )
endif() endif()
# Sanitizers (Debug builds) # Sanitizers (Debug builds)
if(ENABLE_SANITIZERS AND CMAKE_BUILD_TYPE STREQUAL "Debug") if(ENABLE_SANITIZERS AND CMAKE_BUILD_TYPE STREQUAL "Debug")
# Check if sanitizers are available # Check if sanitizers are available
include(CheckCXXCompilerFlag) include(CheckCXXCompilerFlag)
check_cxx_compiler_flag("-fsanitize=address" HAS_ASAN) check_cxx_compiler_flag("-fsanitize=address" HAS_ASAN)
check_cxx_compiler_flag("-fsanitize=undefined" HAS_UBSAN) check_cxx_compiler_flag("-fsanitize=undefined" HAS_UBSAN)
if(HAS_ASAN AND HAS_UBSAN) if(HAS_ASAN AND HAS_UBSAN)
target_compile_options(${EXECUTABLE_NAME} PRIVATE target_compile_options(${EXECUTABLE_NAME} PRIVATE
-fsanitize=address,undefined,leak -fsanitize=address,undefined,leak
@@ -151,7 +169,7 @@ else() # GCC/Clang
message(WARNING "Sanitizers requested but not available - skipping") message(WARNING "Sanitizers requested but not available - skipping")
endif() endif()
endif() endif()
# Optimization flags for Release # Optimization flags for Release
if(CMAKE_BUILD_TYPE STREQUAL "Release") if(CMAKE_BUILD_TYPE STREQUAL "Release")
target_compile_options(${EXECUTABLE_NAME} PRIVATE target_compile_options(${EXECUTABLE_NAME} PRIVATE
@@ -160,7 +178,7 @@ else() # GCC/Clang
-DNDEBUG -DNDEBUG
) )
endif() endif()
# Debug flags # Debug flags
if(CMAKE_BUILD_TYPE STREQUAL "Debug") if(CMAKE_BUILD_TYPE STREQUAL "Debug")
target_compile_options(${EXECUTABLE_NAME} PRIVATE target_compile_options(${EXECUTABLE_NAME} PRIVATE
BIN
View File
Binary file not shown.
-10
View File
@@ -1,10 +0,0 @@
[ 25%] Creating symlink to compile_commands.json in project root
[ 50%] Building C object CMakeFiles/test.dir/source/glad.c.o
[ 75%] Building CXX object CMakeFiles/test.dir/source/main.cpp.o
[ 75%] Built target symlink_compile_commands
/Users/AfonsoCMSosua/Developer/C++/algorithm_visualizer/source/glad.c:192:79: warning: implicit conversion changes signedness: 'int' to 'GLuint' (aka 'unsigned int') [-Wsign-conversion]
const char *gl_str_tmp = (const char*)glGetStringi(GL_EXTENSIONS, index);
~~~~~~~~~~~~ ^~~~~
1 warning generated.
[100%] Linking CXX executable bin/test
[100%] Built target test
+49 -6
View File
@@ -1,9 +1,52 @@
#include <glad/glad.h> // Include GLAD before GLFW
#include <GLFW/glfw3.h>
#include <iostream> #include <iostream>
#include <glad/glad.h> // Callback to resize the viewport when the window is resized
#include <GLFW/glfw3.h> void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
(void)window; // Unused parameter
int main() glViewport(0, 0, width, height);
{ }
return 0;
int main() {
// 1. Initialize GLFW
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// 2. Create the Window
GLFWwindow *window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// 3. Initialize GLAD (Load function pointers)
if (!gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress))){
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// 4. The Render Loop
while (!glfwWindowShouldClose(window)) {
// Input
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
// Rendering commands here
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); // Set clear color (Teal)
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
// Swap buffers and poll IO events
glfwSwapBuffers(window);
glfwPollEvents();
}
// 5. Clean up
glfwTerminate();
return 0;
} }
BIN
View File
Binary file not shown.