From a3a2cd9de26943efbc2a566228f95e7abb7cc4da Mon Sep 17 00:00:00 2001 From: Warwick Date: Wed, 25 Jun 2025 12:01:54 +0100 Subject: [PATCH] Added GL_Context and fixed debug memory leak detection --- .gitignore | 1 + CMakeLists.txt | 38 +++++++++++++++++++------------------- src/main.c | 21 ++++++++++++++++----- 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index bc41e13..d382326 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ CPackSourceConfig.cmake Makefile _deps/ cmake_install.cmake +.cache/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 0b60519..3a88bdd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.16 FATAL_ERROR) +cmake_minimum_required(VERSION 3.25 FATAL_ERROR) project(webgpu VERSION 0 DESCRIPTION "Simple Engine Experiment in C" @@ -16,21 +16,11 @@ if(CMAKE_EXPORT_COMPILE_COMMANDS) ${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}) endif() -# Error on memory address issues in debug mode -if(CMAKE_BUILD_TYPE MATCHES "Debug") - set( - CMAKE_C_FLAGS - "${CMAKE_C_FLAGS} -Werror -fsanitize=undefined -fsanitize=address" - ) - target_link_options(${PROJECT_NAME} - BEFORE PUBLIC -fsanitize=undefined PUBLIC -fsanitize=address - ) -endif() - - option(USE_VENDORED_LIBRARIES "Use vendored libraries" ON) if(USE_VENDORED_LIBRARIES) + set(OpenGL_GL_PREFERENCE GLVND) + add_definitions(-DSDL_STATIC=ON) add_subdirectory(extern/SDL3 EXCLUDE_FROM_ALL) set(SDL3_LIBRARIES SDL3::SDL3) @@ -59,7 +49,6 @@ if(USE_VENDORED_LIBRARIES) set(GLEW_INCLUDE_DIRS ${glew_SOURCE_DIR}/include) - set(OpenGL_GL_PREFERENCE GLVND) find_package(OpenGL REQUIRED) set(OPENGL_LIBRARIES ${OPENGL_gl_LIBRARY}) set(OPENGL_INCLUDE_DIRS ${OPENGL_INCLUDE_DIR}) @@ -67,11 +56,11 @@ else() find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared) endif() -get_cmake_property(_variableNames VARIABLES) -list (SORT _variableNames) -foreach (_variableName ${_variableNames}) - message(STATUS "${_variableName}=${${_variableName}}") -endforeach() +#get_cmake_property(_variableNames VARIABLES) +#list (SORT _variableNames) +#foreach (_variableName ${_variableNames}) +# message(STATUS "${_variableName}=${${_variableName}}") +#endforeach() file(GLOB_RECURSE SOURCE_FILES ${CMAKE_SOURCE_DIR}/src/*.c) @@ -79,6 +68,17 @@ file(GLOB_RECURSE HEADER_FILES ${CMAKE_SOURCE_DIR}/src/*.h) add_executable(${PROJECT_NAME} ${HEADER_FILES} ${SOURCE_FILES}) +# Error on memory address issues in debug mode +if(CMAKE_BUILD_TYPE MATCHES "Debug") + set( + CMAKE_C_FLAGS + "${CMAKE_C_FLAGS} -Werror -fsanitize=undefined -fsanitize=address" + ) + target_link_options(${PROJECT_NAME} + BEFORE PUBLIC -fsanitize=undefined PUBLIC -fsanitize=address + ) +endif() + target_include_directories(${PROJECT_NAME} PRIVATE ${SDL3_INCLUDE_DIRS} ${SDL3_image_INCLUDE_DIRS} diff --git a/src/main.c b/src/main.c index a278470..8fb0f80 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,6 @@ #include "SDL3/SDL_init.h" #include "SDL3/SDL_log.h" +#include "SDL3/SDL_opengl.h" #include "SDL3/SDL_render.h" #include "SDL3/SDL_video.h" #include @@ -15,25 +16,35 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); - window = SDL_CreateWindow("Hello SDL3", 320, 240, SDL_WINDOW_OPENGL); + window = SDL_CreateWindow("Hello SDL3", 320, 240, + SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); if (!window) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, - "Couldn't create window and renderer: %s", SDL_GetError()); + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create windowe: %s", + SDL_GetError()); return EXIT_FAILURE; } + SDL_GLContext glcontext = SDL_GL_CreateContext(window); + while (1) { + // handle input SDL_PollEvent(&event); if (event.type == SDL_EVENT_QUIT) { break; } + + // render + glClearColor(0, 0, 0, 1); + glClear(GL_COLOR_BUFFER_BIT); + SDL_GL_SwapWindow(window); } + SDL_GL_DestroyContext(glcontext); SDL_DestroyWindow(window); SDL_Quit();