Skip to content

Commit

Permalink
create and update component scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
goopey7 committed Jan 3, 2024
1 parent e5471d9 commit 4d0fe36
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 94 deletions.
117 changes: 68 additions & 49 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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}
Expand All @@ -169,39 +195,32 @@ if(WIN32)
${CMAKE_SOURCE_DIR}/rust_lib/target/release/rust_hello_lib.dll
$<TARGET_FILE_DIR:${PROJECT_NAME}>)
add_custom_command(
TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/windows/sdl2/lib/SDL2.dll
$<TARGET_FILE_DIR:${PROJECT_NAME}>)
TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/windows/sdl2/lib/SDL2.dll
$<TARGET_FILE_DIR:${PROJECT_NAME}>)
add_custom_command(
TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/windows/assimp/lib/assimp-vc143-mt.dll
$<TARGET_FILE_DIR:${PROJECT_NAME}>)
TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/windows/assimp/lib/assimp-vc143-mt.dll
$<TARGET_FILE_DIR:${PROJECT_NAME}>)
add_custom_command(
TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/res
$<TARGET_FILE_DIR:${PROJECT_NAME}>/res)
TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/res
$<TARGET_FILE_DIR:${PROJECT_NAME}>/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
$<TARGET_FILE_DIR:${PROJECT_NAME}>/shaders)
TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/shaders
$<TARGET_FILE_DIR:${PROJECT_NAME}>/shaders)

endif()
13 changes: 9 additions & 4 deletions components/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()

26 changes: 12 additions & 14 deletions components/CustomComponents.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
#include "TestCustomComp.h"
#include <goop/Components.h>
#include <map>

static std::map<std::string, std::function<goop::CustomComponent*(entt::entity, goop::Scene*)>>
customComponentMap;
static std::map<std::string, std::function<goop::CustomComponent*(entt::entity, goop::Scene*)>> 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);
13 changes: 0 additions & 13 deletions components/TestCustomComp.cpp

This file was deleted.

14 changes: 0 additions & 14 deletions components/TestCustomComp.h

This file was deleted.

48 changes: 48 additions & 0 deletions components/createComponent.sh
Original file line number Diff line number Diff line change
@@ -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 <<EOF >"${HEADER_FILE}"
#pragma once
#include <goop/Components.h>
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 <<EOF >"${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!"

68 changes: 68 additions & 0 deletions components/updateComponents.sh
Original file line number Diff line number Diff line change
@@ -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 <goop/Components.h>" >>CustomComponents.h
echo "#include <map>" >>CustomComponents.h
echo >>CustomComponents.h
echo "static std::map<std::string, std::function<goop::CustomComponent*(entt::entity, goop::Scene*)>> 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 <goop/Components.h>" >>CustomComponents.h
echo "#include <map>" >>CustomComponents.h
echo >>CustomComponents.h
echo "static std::map<std::string, std::function<goop::CustomComponent*(entt::entity, goop::Scene*)>> 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

1 change: 1 addition & 0 deletions game/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 4d0fe36

Please sign in to comment.