From 6b979b4a868b294a6928216b490e783163f55ccb Mon Sep 17 00:00:00 2001 From: tavo Date: Sat, 4 Oct 2025 13:45:52 -0600 Subject: [PATCH] init commit --- .clang-format | 21 ++++++ .gitignore | 21 ++++++ .gitmodules | 8 ++ CMakeLists.txt | 196 +++++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 22 ++++++ README.md | 5 ++ external/cglm | 1 + external/skr | 1 + main/main.c | 36 +++++++++ 9 files changed, 311 insertions(+) create mode 100644 .clang-format create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 CMakeLists.txt create mode 100644 Makefile create mode 100644 README.md create mode 160000 external/cglm create mode 160000 external/skr create mode 100644 main/main.c diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..da6589a --- /dev/null +++ b/.clang-format @@ -0,0 +1,21 @@ +--- +BasedOnStyle: LLVM +UseTab: ForIndentation +IndentWidth: 8 +TabWidth: 8 +ContinuationIndentWidth: 8 +ColumnLimit: 80 +AlignConsecutiveDeclarations: AcrossComments +AlignTrailingComments: true +SpacesBeforeTrailingComments: 1 +PointerAlignment: Left +SpaceBeforeParens: ControlStatements +BreakBeforeBraces: Custom +BraceWrapping: + AfterStruct: false + AfterEnum: false + AfterFunction: false + AfterControlStatement: false +SortIncludes: true +ReflowComments: true +... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..034989d --- /dev/null +++ b/.gitignore @@ -0,0 +1,21 @@ +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +# have a top-level makefile +# (will be overwritten by cmake .) +#Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps +CMakeUserPresets.json +build/ + +.cache/ +scripts/glstri.c +external/stb_image.h +# do not track resources just yet +res/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..b4d09da --- /dev/null +++ b/.gitmodules @@ -0,0 +1,8 @@ +[submodule "external/cglm"] + path = external/cglm + url = https://github.com/recp/cglm + branch = master +[submodule "external/skr"] + path = external/skr + url = git@git.tavo.one:tavo/skr + branch = main diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..b5c05ec --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,196 @@ +cmake_minimum_required(VERSION 3.10) +project(carga-electrica C) + +set(CMAKE_C_STANDARD 99) +# set(CMAKE_C_EXTENSIONS OFF) # Change later after fixing errors +set(CMAKE_C_STANDARD_REQUIRED True) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +set(BIN proyecto) + +function(download_if_missing url output_path) + if(NOT EXISTS ${output_path}) + message(STATUS "Downloading ${url} -> ${output_path}") + file(DOWNLOAD + ${url} + ${output_path} + SHOW_PROGRESS + STATUS status + LOG log + ) + list(GET status 0 status_code) + if(NOT status_code EQUAL 0) + message(FATAL_ERROR "Failed to download ${url}:\n${log}") + endif() + endif() +endfunction() + +# Download external libraries + +file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/external) + +download_if_missing( + "https://raw.githubusercontent.com/nothings/stb/refs/heads/master/stb_image.h" + "${CMAKE_CURRENT_SOURCE_DIR}/external/stb_image.h" +) + +# Download external scripts + +file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/scripts) + +download_if_missing( + "https://raw.githubusercontent.com/Brady-Brandt/glstri/main/main.c" + "${CMAKE_CURRENT_SOURCE_DIR}/scripts/glstri.c" +) + +add_executable(glstri scripts/glstri.c) + +# Find packages + +# OpenGL +set(OpenGL_GL_PREFERENCE GLVND) # "GLVND" Or "LEGACY" +find_package(OpenGL REQUIRED) +if (OPENGL_opengl_LIBRARY) + set(OPENGL_LIB ${OPENGL_opengl_LIBRARY}) +elseif (OPENGL_gl_LIBRARY) + message(STATUS "Using legacy OpenGL") + set(OPENGL_LIB ${OPENGL_gl_LIBRARY}) +else() + message(FATAL_ERROR "No suitable OpenGL library found.") +endif() + +# GLFW +find_package(glfw3 REQUIRED) + +# GLEW +find_package(GLEW REQUIRED) + +# Update submodules + +find_package(Git QUIET) + +if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git") + option(GIT_SUBMODULE "Check submodules during build" ON) + if(GIT_SUBMODULE) + execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + RESULT_VARIABLE GIT_SUBMOD_RESULT) + if(NOT GIT_SUBMOD_RESULT EQUAL 0) + message(FATAL_ERROR "git submodule update --init --recursive failed with ${GIT_SUBMOD_RESULT}. Please check out submodules.") + endif() + endif() +endif() + +# Submodule: cglm +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/external/cglm EXCLUDE_FROM_ALL) + +# Submodule: skr +add_library(skr INTERFACE) +target_include_directories(skr INTERFACE + ${CMAKE_CURRENT_SOURCE_DIR}/external/skr +) + +# Submodule: slog +# add_library(slog INTERFACE) +# target_include_directories(slog INTERFACE +# ${CMAKE_CURRENT_SOURCE_DIR}/external/slog +# ) + +# Submodule: ini +# add_library(ini INTERFACE) +# target_include_directories(ini INTERFACE +# ${CMAKE_CURRENT_SOURCE_DIR}/external/ini/src +# ) + +# Transpile GLSL into C + +set(SHADER_DIR ${CMAKE_SOURCE_DIR}/main/shaders) +set(GLSL_EXTS vert frag comp geom tesc tese) + +foreach(ext ${GLSL_EXTS}) + file(GLOB SHADER_FILES ${SHADER_DIR}/*.${ext}) + + foreach(shader ${SHADER_FILES}) + get_filename_component(shader_name ${shader} NAME) + get_filename_component(base_name ${shader} NAME_WE) + + set(output_file ${SHADER_DIR}/${shader_name}.c) + + add_custom_command( + OUTPUT ${output_file} + COMMAND $ -s -v ${base_name}_${ext} ${shader} ${output_file} + DEPENDS ${shader} glstri + COMMENT "transpiling ${shader_name} -> ${output_file}" + VERBATIM + ) + + list(APPEND TRANSPILED_SHADERS ${output_file}) + endforeach() +endforeach() + +add_custom_target(transpile_shaders ALL DEPENDS ${TRANSPILED_SHADERS}) + +# Main program + +add_executable(${BIN} + main/main.c + + # external/ini/src/ini.c +) + +target_include_directories(${BIN} + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} + external +) + +target_link_libraries(${BIN} + PRIVATE + m + ${OPENGL_LIB} + glfw + GLEW::GLEW + cglm_headers + skr + # slog + # ini +) + +if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang") + target_compile_options(${BIN} PRIVATE + $<$:-g3 -O0 -Wall -Wextra -DDEBUG -fno-omit-frame-pointer -fsanitize=address,undefined> + $<$:-O3 -g0 -DNDEBUG -ffunction-sections -fdata-sections -fmacro-prefix-map=${CMAKE_SOURCE_DIR}/=> + ) + + target_link_options(${BIN} PRIVATE + $<$:-fno-omit-frame-pointer -fsanitize=address,undefined> + $<$:-Wl,--gc-sections> + ) + + # Enable LTO in Release if supported + option(ENABLE_LTO "Enable LTO in Release" ON) + if(ENABLE_LTO) + include(CheckIPOSupported) + check_ipo_supported(RESULT ipo_ok OUTPUT ipo_err) + if(ipo_ok) + set_property(TARGET ${BIN} PROPERTY INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE) + else() + target_compile_options(${BIN} PRIVATE $<$:-flto>) + target_link_options(${BIN} PRIVATE $<$:-flto>) + endif() + endif() + + # Optionally strip symbols in Release + option(STRIP_RELEASE "Strip symbols from Release binaries" ON) + if(STRIP_RELEASE) + if(CMAKE_STRIP) + add_custom_command(TARGET ${BIN} POST_BUILD + COMMAND ${CMAKE_STRIP} $ + ) + else() + target_link_options(${BIN} PRIVATE $<$:-s>) + endif() + endif() +endif() + +add_dependencies(${BIN} transpile_shaders) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0059b77 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +BUILD_DIR = build + +.PHONY: all debug release clean run/debug run/release + +all: debug + +debug: + cmake -B $(BUILD_DIR) -DCMAKE_BUILD_TYPE=Debug + cmake --build $(BUILD_DIR) + +release: + cmake -B $(BUILD_DIR) -DCMAKE_BUILD_TYPE=Release + cmake --build $(BUILD_DIR) + +run/debug: debug + ./$(BUILD_DIR)/proyecto + +run/release: release + ./$(BUILD_DIR)/proyecto + +clean: + rm -rf build diff --git a/README.md b/README.md new file mode 100644 index 0000000..b345f90 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Simulación de Cargas + +## Dependencias para compilar + +- Debian: `cmake pkg-config libglfw3-dev libglew-dev libassimp-dev` diff --git a/external/cglm b/external/cglm new file mode 160000 index 0000000..a4602f2 --- /dev/null +++ b/external/cglm @@ -0,0 +1 @@ +Subproject commit a4602f2d5f1f275c02ef608ef27d4021572d9d6c diff --git a/external/skr b/external/skr new file mode 160000 index 0000000..836aadb --- /dev/null +++ b/external/skr @@ -0,0 +1 @@ +Subproject commit 836aadb32cf5df09cfd0cae71767b9d09b695571 diff --git a/main/main.c b/main/main.c new file mode 100644 index 0000000..d375e29 --- /dev/null +++ b/main/main.c @@ -0,0 +1,36 @@ +#include + +#include +#include + +#include + +#define SKR_BACKEND_API 0 // opengl +#define SKR_BACKEND_WINDOW 0 // glfw +#include + +int main(void) { + SkrWindow window = { + .Title = "Hello SKR", + .Width = 800, + .Height = 600, + }; + + SkrState state = SkrInit(&window); + if (!SKR_OK) { + fprintf(stderr, "Failed to init window: %s\n", SKR_LAST_ERROR); + return 1; + } + + if (glewInit() != GLEW_OK) { + fprintf(stderr, "Failed to init GLEW\n"); + return 1; + } + + while (!SkrWindowShouldClose(&window)) { + SkrRendererRender(&state); + } + + SkrFinalize(&state); + return 0; +}