From 4d0fe360911e2f4707c34324244077653ec5572f Mon Sep 17 00:00:00 2001 From: Sam Collier Date: Wed, 3 Jan 2024 22:34:24 +0000 Subject: [PATCH] create and update component scripts --- CMakeLists.txt | 117 +++++++++++++++++++-------------- components/CMakeLists.txt | 13 ++-- components/CustomComponents.h | 26 ++++---- components/TestCustomComp.cpp | 13 ---- components/TestCustomComp.h | 14 ---- components/createComponent.sh | 48 ++++++++++++++ components/updateComponents.sh | 68 +++++++++++++++++++ game/CMakeLists.txt | 1 + 8 files changed, 206 insertions(+), 94 deletions(-) delete mode 100644 components/TestCustomComp.cpp delete mode 100644 components/TestCustomComp.h create mode 100755 components/createComponent.sh create mode 100755 components/updateComponents.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 31a5aab..3716db5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,7 +60,7 @@ file(GLOB STB_IMAGE_HEADER "libs/stb/stb_image.h") # Define the C++ executable add_executable(${PROJECT_NAME} ${ENGINE_SRC_FILES} ${STB_VORBIS_SOURCE}) -#define app type +# define app type target_compile_definitions(${PROJECT_NAME} PRIVATE GOOP_APPTYPE_EDITOR=1) # define systems @@ -71,8 +71,8 @@ target_compile_definitions(${PROJECT_NAME} PRIVATE GOOP_MESHLOADER_ASSIMP) target_compile_definitions(${PROJECT_NAME} PRIVATE GOOP_IMGLOADER_STB) target_compile_definitions(${PROJECT_NAME} PRIVATE GOOP_PHYSICS_BULLET) -# ##################### Build Soloud -# Add a custom target to build Soloud using CMake +# ##################### Build Soloud Add a custom target to build Soloud using +# CMake add_custom_target( build_soloud SOURCES ${SOLOUD_SOURCES} @@ -106,25 +106,39 @@ if(WIN32) set(RUST_LIB ${CMAKE_SOURCE_DIR}/rust_lib/target/release/rust_hello_lib.dll.lib) else() - set(RUST_LIB - ${CMAKE_SOURCE_DIR}/rust_lib/target/release/librust_hello_lib.so) + set(RUST_LIB ${CMAKE_SOURCE_DIR}/rust_lib/target/release/librust_hello_lib.so) endif() # Link the C++ executable add_subdirectory(editor) add_subdirectory(game) add_subdirectory(components) -target_link_libraries(${PROJECT_NAME} PRIVATE volk ${SDL2_LIBRARY} ${RUST_LIB} ${GLFW3_LIBRARY} - ${ASSIMP_LIBRARY} SoLoud imgui_lib editor_lib game_lib components_lib BulletDynamics BulletCollision LinearMath) +target_link_libraries( + ${PROJECT_NAME} + PRIVATE volk + ${SDL2_LIBRARY} + ${RUST_LIB} + ${GLFW3_LIBRARY} + ${ASSIMP_LIBRARY} + SoLoud + imgui_lib + editor_lib + game_lib + components_lib + BulletDynamics + BulletCollision + LinearMath) # ############################################################################## # ##################### Build shaders if(WIN32) - find_program(GLSLC_EXECUTABLE NAMES glslc + find_program( + GLSLC_EXECUTABLE + NAMES glslc PATHS ${CMAKE_SOURCE_DIR}/windows/glslc) else() - find_program(GLSLC_EXECUTABLE glslc) + find_program(GLSLC_EXECUTABLE glslc) endif() if(NOT GLSLC_EXECUTABLE) message( @@ -139,16 +153,20 @@ add_custom_target( compile_shaders COMMAND ${GLSLC_EXECUTABLE} -o ${CMAKE_BINARY_DIR}/shaders/shader.vert.spv ${CMAKE_SOURCE_DIR}/shaders/shader.vert - COMMAND ${GLSLC_EXECUTABLE} -o ${CMAKE_BINARY_DIR}/shaders/shader_editor.frag.spv - ${CMAKE_SOURCE_DIR}/shaders/shader_editor.frag - COMMAND ${GLSLC_EXECUTABLE} -o ${CMAKE_BINARY_DIR}/shaders/shader_game.frag.spv - ${CMAKE_SOURCE_DIR}/shaders/shader_game.frag + COMMAND + ${GLSLC_EXECUTABLE} -o ${CMAKE_BINARY_DIR}/shaders/shader_editor.frag.spv + ${CMAKE_SOURCE_DIR}/shaders/shader_editor.frag + COMMAND + ${GLSLC_EXECUTABLE} -o ${CMAKE_BINARY_DIR}/shaders/shader_game.frag.spv + ${CMAKE_SOURCE_DIR}/shaders/shader_game.frag COMMAND ${GLSLC_EXECUTABLE} -o ${CMAKE_SOURCE_DIR}/shaders/shader.vert.spv ${CMAKE_SOURCE_DIR}/shaders/shader.vert - COMMAND ${GLSLC_EXECUTABLE} -o ${CMAKE_SOURCE_DIR}/shaders/shader_editor.frag.spv - ${CMAKE_SOURCE_DIR}/shaders/shader_editor.frag - COMMAND ${GLSLC_EXECUTABLE} -o ${CMAKE_SOURCE_DIR}/shaders/shader_game.frag.spv - ${CMAKE_SOURCE_DIR}/shaders/shader_game.frag + COMMAND + ${GLSLC_EXECUTABLE} -o ${CMAKE_SOURCE_DIR}/shaders/shader_editor.frag.spv + ${CMAKE_SOURCE_DIR}/shaders/shader_editor.frag + COMMAND + ${GLSLC_EXECUTABLE} -o ${CMAKE_SOURCE_DIR}/shaders/shader_game.frag.spv + ${CMAKE_SOURCE_DIR}/shaders/shader_game.frag COMMENT "Compiling GLSL shaders...") # Specify the output directory for shader files @@ -158,8 +176,16 @@ set(SHADER_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/shaders) add_dependencies(${PROJECT_NAME} compile_shaders) # ############################################################################## -# Copy the rust_hello_lib.dll to the build directory where the .exe goes -# And copy SDL2.dll +add_custom_target(update_components + COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/components/updateComponents.sh + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMENT "Running updateComponents.sh script" +) + +add_dependencies(${PROJECT_NAME} update_components) + +# Copy the rust_hello_lib.dll to the build directory where the .exe goes And +# copy SDL2.dll if(WIN32) add_custom_command( TARGET ${PROJECT_NAME} @@ -169,39 +195,32 @@ if(WIN32) ${CMAKE_SOURCE_DIR}/rust_lib/target/release/rust_hello_lib.dll $) add_custom_command( - TARGET ${PROJECT_NAME} - POST_BUILD - COMMAND - ${CMAKE_COMMAND} -E copy - ${CMAKE_SOURCE_DIR}/windows/sdl2/lib/SDL2.dll - $) + TARGET ${PROJECT_NAME} + POST_BUILD + COMMAND + ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/windows/sdl2/lib/SDL2.dll + $) add_custom_command( - TARGET ${PROJECT_NAME} - POST_BUILD - COMMAND - ${CMAKE_COMMAND} -E copy - ${CMAKE_SOURCE_DIR}/windows/assimp/lib/assimp-vc143-mt.dll - $) + TARGET ${PROJECT_NAME} + POST_BUILD + COMMAND + ${CMAKE_COMMAND} -E copy + ${CMAKE_SOURCE_DIR}/windows/assimp/lib/assimp-vc143-mt.dll + $) add_custom_command( - TARGET ${PROJECT_NAME} - POST_BUILD - COMMAND - ${CMAKE_COMMAND} -E copy_directory - ${CMAKE_SOURCE_DIR}/res - $/res) + TARGET ${PROJECT_NAME} + POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/res + $/res) add_custom_command( - TARGET ${PROJECT_NAME} - POST_BUILD - COMMAND - ${CMAKE_COMMAND} -E copy_directory - ${CMAKE_SOURCE_DIR}/res - ${CMAKE_BINARY_DIR}/res) + TARGET ${PROJECT_NAME} + POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/res + ${CMAKE_BINARY_DIR}/res) add_custom_command( - TARGET ${PROJECT_NAME} - POST_BUILD - COMMAND - ${CMAKE_COMMAND} -E copy_directory - ${CMAKE_SOURCE_DIR}/shaders - $/shaders) + TARGET ${PROJECT_NAME} + POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/shaders + $/shaders) endif() diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index f8363e5..6f591f5 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -1,10 +1,15 @@ -# Set the minimum required CMake version cmake_minimum_required(VERSION 3.12) project(components_lib) -file(GLOB_RECURSE COMPONENTS_SRC_FILES "*.cpp" "*.h") +file(GLOB_RECURSE COMPONENTS_CPP_FILES "*.cpp") +file(GLOB_RECURSE COMPONENTS_H_FILES "*.h") -add_library(${PROJECT_NAME} ${COMPONENTS_SRC_FILES}) -target_compile_definitions(${PROJECT_NAME} PRIVATE GOOP_RENDERER_VULKAN) +if (COMPONENTS_CPP_FILES) + add_library(${PROJECT_NAME} ${COMPONENTS_CPP_FILES} ${COMPONENTS_H_FILES}) + target_compile_definitions(${PROJECT_NAME} PRIVATE GOOP_RENDERER_VULKAN) +else() + add_library(${PROJECT_NAME} INTERFACE) + target_sources(${PROJECT_NAME} INTERFACE ${COMPONENTS_H_FILES}) +endif() diff --git a/components/CustomComponents.h b/components/CustomComponents.h index 280444a..611722d 100644 --- a/components/CustomComponents.h +++ b/components/CustomComponents.h @@ -1,18 +1,16 @@ -#include "TestCustomComp.h" +#include #include -static std::map> - customComponentMap; +static std::map> customComponentMap; -#define REGISTER_CUSTOM_COMPONENT(name, type) \ - static goop::CustomComponent* create##type(entt::entity e, goop::Scene* s) \ - { \ - return new type(goop::Entity(e, s)); \ - } \ - static bool registered##type = []() \ - { \ - customComponentMap[name] = create##type; \ - return true; \ - }() +#define REGISTER_CUSTOM_COMPONENT(name, type) \ + static goop::CustomComponent* create##type(entt::entity e, goop::Scene* s) \ + { \ + return new type(goop::Entity(e, s)); \ + } \ + static bool registered##type = []() \ + { \ + customComponentMap[name] = create##type; \ + return true; \ + }() -REGISTER_CUSTOM_COMPONENT("TestCustomComp", TestCustomComp); diff --git a/components/TestCustomComp.cpp b/components/TestCustomComp.cpp deleted file mode 100644 index fb333aa..0000000 --- a/components/TestCustomComp.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "TestCustomComp.h" - -void TestCustomComp::init() -{ -} - -void TestCustomComp::update(float dt) -{ - timer += dt; - auto& tc = entity.getComponent(); - tc.position.x = sin(timer); -} - diff --git a/components/TestCustomComp.h b/components/TestCustomComp.h deleted file mode 100644 index ee4e88e..0000000 --- a/components/TestCustomComp.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -#include -#include - -class TestCustomComp : public goop::CustomComponent -{ - public: - TestCustomComp(goop::Entity e) : goop::CustomComponent(e) {} - void init() final; - void update(float dt) final; - - private: - float timer = 0.0f; -}; diff --git a/components/createComponent.sh b/components/createComponent.sh new file mode 100755 index 0000000..377a1f1 --- /dev/null +++ b/components/createComponent.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +if [ "$#" -ne 1 ]; then + echo "Usage: $0 ClassName" + exit 1 +fi + +CLASS_NAME="$1" +HEADER_FILE="${CLASS_NAME}.h" +CPP_FILE="${CLASS_NAME}.cpp" + +echo "Creating C++ class: ${CLASS_NAME}..." + +# Generate header file +cat <"${HEADER_FILE}" +#pragma once +#include + +class ${CLASS_NAME} : public goop::CustomComponent +{ +public: + ${CLASS_NAME}(goop::Entity e) : goop::CustomComponent(e) {} + void init() final; + void update(float dt) final; +private: +}; +EOF + +echo "Created ${HEADER_FILE}" + +# Generate cpp file +cat <"${CPP_FILE}" +#include "${HEADER_FILE}" + +void ${CLASS_NAME}::init() +{ + // Implement initialization +} + +void ${CLASS_NAME}::update(float dt) +{ + // Implement update +} +EOF + +echo "Created ${CPP_FILE}" +echo "C++ class '${CLASS_NAME}' created successfully!" + diff --git a/components/updateComponents.sh b/components/updateComponents.sh new file mode 100755 index 0000000..a800f37 --- /dev/null +++ b/components/updateComponents.sh @@ -0,0 +1,68 @@ +#!/bin/bash + +# Get the directory of the script +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +# Move to the script's directory +cd "$script_dir" || exit + +# Find .cpp files +shopt -s nullglob +cpp_files=("$script_dir"/*.cpp) +shopt -u nullglob + +# Start by creating or clearing CustomComponents.h +echo -n >CustomComponents.h + +# Check if there are no .cpp files +if [ ${#cpp_files[@]} -eq 0 ]; then + echo "No .cpp files found in the directory." +echo "#include " >>CustomComponents.h +echo "#include " >>CustomComponents.h +echo >>CustomComponents.h +echo "static std::map> customComponentMap;" >>CustomComponents.h +echo >>CustomComponents.h +echo "#define REGISTER_CUSTOM_COMPONENT(name, type) \\" >>CustomComponents.h +echo " static goop::CustomComponent* create##type(entt::entity e, goop::Scene* s) \\" >>CustomComponents.h +echo " { \\" >>CustomComponents.h +echo " return new type(goop::Entity(e, s)); \\" >>CustomComponents.h +echo " } \\" >>CustomComponents.h +echo " static bool registered##type = []() \\" >>CustomComponents.h +echo " { \\" >>CustomComponents.h +echo " customComponentMap[name] = create##type; \\" >>CustomComponents.h +echo " return true; \\" >>CustomComponents.h +echo " }()" >>CustomComponents.h +echo >>CustomComponents.h + exit 0 +fi + + +# Add #includes +for file in "${cpp_files[@]}"; do + filename=$(basename "$file" .cpp) + echo "#include \"$filename.h\"" >>CustomComponents.h +done + +echo "#include " >>CustomComponents.h +echo "#include " >>CustomComponents.h +echo >>CustomComponents.h +echo "static std::map> customComponentMap;" >>CustomComponents.h +echo >>CustomComponents.h +echo "#define REGISTER_CUSTOM_COMPONENT(name, type) \\" >>CustomComponents.h +echo " static goop::CustomComponent* create##type(entt::entity e, goop::Scene* s) \\" >>CustomComponents.h +echo " { \\" >>CustomComponents.h +echo " return new type(goop::Entity(e, s)); \\" >>CustomComponents.h +echo " } \\" >>CustomComponents.h +echo " static bool registered##type = []() \\" >>CustomComponents.h +echo " { \\" >>CustomComponents.h +echo " customComponentMap[name] = create##type; \\" >>CustomComponents.h +echo " return true; \\" >>CustomComponents.h +echo " }()" >>CustomComponents.h +echo >>CustomComponents.h + +# Register components +for file in "${cpp_files[@]}"; do + filename=$(basename "$file" .cpp) + echo "REGISTER_CUSTOM_COMPONENT(\"$filename\", $filename);" >>CustomComponents.h +done + diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index db524ab..5d5e72b 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -4,6 +4,7 @@ cmake_minimum_required(VERSION 3.12) project(game_lib) file(GLOB_RECURSE GAME_SRC_FILES "*.cpp" "*.h") + add_library(${PROJECT_NAME} ${GAME_SRC_FILES}) include_directories("../libs/json") target_compile_definitions(${PROJECT_NAME} PRIVATE GOOP_RENDERER_VULKAN)