From b667e3ac7192f24ce53b88bb6efa97062bdf9bd9 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Fri, 28 May 2021 17:35:50 +0200 Subject: [PATCH 01/60] some steps, x11 not found --- CMakeLists.txt | 6 ++++++ emsdk.sh | 19 +++++++++++++++++++ lib/raylib/Findraylib.cmake | 3 +++ lib/wal/sources/Wal.hpp | 34 ++++++++++++++++++++++++++++++++++ wasm-server.py | 24 ++++++++++++++++++++++++ 5 files changed, 86 insertions(+) create mode 100755 emsdk.sh create mode 100644 wasm-server.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 45d0247d..37b6a26b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,12 @@ include_directories(bomberman sources) add_subdirectory(${PROJECT_SOURCE_DIR}/lib/wal) add_subdirectory(${PROJECT_SOURCE_DIR}/lib/Ray) +if (EMSCRIPTEN) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") + set(CMAKE_EXECUTABLE_SUFFIX ".html") # This line is used to set your executable to build with the emscripten html template so taht you can directly open it. +endif () + + set(SOURCES sources/Models/GameState.hpp sources/Runner/Runner.cpp diff --git a/emsdk.sh b/emsdk.sh new file mode 100755 index 00000000..93816bb0 --- /dev/null +++ b/emsdk.sh @@ -0,0 +1,19 @@ +#! /bin/bash +PRJECTDIR=`pwd` +EMSDK_PATH=$HOME/emsdk +cd $EMSDK_PATH && git pull && +./emcmdprompt +./emsdk install latest && +./emsdk activate latest && +source ./emsdk_env.sh && +HELLO=WORLD +EMSCRIPTEN_PATH=$EMSDK_PATH/upstream/emscripten +CLANG_PATH=$EMSDK_PATH/upstream/bin +NODE_PATH=$EMSDK_PATH/node/12.9.1_64bit/bin +PATH=$PATH:$EMSDK_PATH:$EMSCRIPTEN_PATH:$CLANG_PATH:$NODE_PATH && +cd $PRJECTDIR && +emcmake cmake -S . -B build && +make PLATFORM=PLATFORM_WEB -B && +cmake --build build && +make -C build PLATFORM=PLATFORM_WEB -B && +python3 wasm-server.py diff --git a/lib/raylib/Findraylib.cmake b/lib/raylib/Findraylib.cmake index 4baf8bd8..d836183b 100644 --- a/lib/raylib/Findraylib.cmake +++ b/lib/raylib/Findraylib.cmake @@ -14,6 +14,9 @@ if (NOT raylib_FOUND) SET(FETCHCONTENT_QUIET NO) FetchContent_Populate(raylib) SET(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) + if (EMSCRIPTEN) + SET(PLATFORM Web) + endif() ADD_SUBDIRECTORY(${raylib_SOURCE_DIR} ${raylib_BINARY_DIR}) SET(raylib_FOUND TRUE) endif() diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index 50a87188..c222862f 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -15,6 +15,12 @@ #include "System/System.hpp" #include "Models/Callback.hpp" +#ifdef PLATFORM_WEB +#include +WAL::Wal *walPtr = nullptr; +void *callbackPtr = nullptr; +#endif + namespace WAL { //! @brief The main WAL class, it is used to setup and run the ECS. @@ -109,7 +115,14 @@ namespace WAL void run(const std::function &callback, T state = T()) { Callback update(callback); + + #ifdef PLATFORM_WEB + walPtr = this; + callbackPtr = &callback; + return emscripten_set_main_loop_arg(runWASM, &state, 0, 1); + #else return this->run(update, state); + #endif } //! @brief Start the game loop @@ -137,6 +150,27 @@ namespace WAL } } + #ifdef PLATFORM_WEB + template + static void runIteration(T *state) + { + static std::function callback = callbackPtr; + static auto lastTick = std::chrono::steady_clock::now(); + static std::chrono::nanoseconds fBehind(0); + + auto now = std::chrono::steady_clock::now(); + std::chrono::nanoseconds dtime = now - lastTick; + fBehind += dtime; + lastTick = now; + while (fBehind > Wal::timestep) { + fBehind -= Wal::timestep; + this->_fixedUpdate(); + } + walPtr->_update(dtime); + callback(wal, state); + } + #endif + //! @brief A default constructor Wal() = default; //! @brief A WAL can't be copy constructed diff --git a/wasm-server.py b/wasm-server.py new file mode 100644 index 00000000..77aad1db --- /dev/null +++ b/wasm-server.py @@ -0,0 +1,24 @@ +# Python 3 + +import sys +import socketserver +from http.server import SimpleHTTPRequestHandler + +class WasmHandler(SimpleHTTPRequestHandler): + def end_headers(self): + # Include additional response headers here. CORS for example: + #self.send_header('Access-Control-Allow-Origin', '*') + SimpleHTTPRequestHandler.end_headers(self) + + +# Python 3.7.5 adds in the WebAssembly Media Type. If this is an older +# version, add in the Media Type. +if sys.version_info < (3, 7, 5): + WasmHandler.extensions_map['.wasm'] = 'application/wasm' + + +if __name__ == '__main__': + PORT = 8080 + with socketserver.TCPServer(("", PORT), WasmHandler) as httpd: + print("Listening on port {}. Press Ctrl+C to stop.".format(PORT)) + httpd.serve_forever() From 7915985b7418378127f412140f0eae7eaaa94c85 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Fri, 28 May 2021 18:05:26 +0200 Subject: [PATCH 02/60] workflow --- .github/workflows/webassembly.yml | 17 +++++++++++++++++ emsdk.sh | 9 +++++---- 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/webassembly.yml diff --git a/.github/workflows/webassembly.yml b/.github/workflows/webassembly.yml new file mode 100644 index 00000000..c2999586 --- /dev/null +++ b/.github/workflows/webassembly.yml @@ -0,0 +1,17 @@ +name: Web Building +on: + push: + branches: + wasm + +jobs: + Push: + runs-on: windows-latest + steps: + - uses: actions/checkout@v1 + with: + submodules: true + ref: master + - name: Exec + shell: bash + run: ./emsdk.sh \ No newline at end of file diff --git a/emsdk.sh b/emsdk.sh index 93816bb0..3cd21922 100755 --- a/emsdk.sh +++ b/emsdk.sh @@ -1,15 +1,16 @@ #! /bin/bash PRJECTDIR=`pwd` EMSDK_PATH=$HOME/emsdk -cd $EMSDK_PATH && git pull && -./emcmdprompt +if [[ ! -d $HOME/emsdk ]]; then + git clone https://github.com/emscripten-core/emsdk.git +fi +cd $EMSDK_PATH && +./emsdk install tot && ./emsdk install latest && ./emsdk activate latest && source ./emsdk_env.sh && -HELLO=WORLD EMSCRIPTEN_PATH=$EMSDK_PATH/upstream/emscripten CLANG_PATH=$EMSDK_PATH/upstream/bin -NODE_PATH=$EMSDK_PATH/node/12.9.1_64bit/bin PATH=$PATH:$EMSDK_PATH:$EMSCRIPTEN_PATH:$CLANG_PATH:$NODE_PATH && cd $PRJECTDIR && emcmake cmake -S . -B build && From 7a8c8c8457338d29c07f47bb7d9aadd93d174edc Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Fri, 28 May 2021 18:06:53 +0200 Subject: [PATCH 03/60] workflow --- .github/workflows/webassembly.yml | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/.github/workflows/webassembly.yml b/.github/workflows/webassembly.yml index c2999586..c839c9ef 100644 --- a/.github/workflows/webassembly.yml +++ b/.github/workflows/webassembly.yml @@ -14,4 +14,20 @@ jobs: ref: master - name: Exec shell: bash - run: ./emsdk.sh \ No newline at end of file + run: | + PRJECTDIR=`pwd` + EMSDK_PATH=$HOME/emsdk + cd $EMSDK_PATH && + ./emsdk install tot && + ./emsdk install latest && + ./emsdk activate latest && + source ./emsdk_env.sh && + EMSCRIPTEN_PATH=$EMSDK_PATH/upstream/emscripten + CLANG_PATH=$EMSDK_PATH/upstream/bin + PATH=$PATH:$EMSDK_PATH:$EMSCRIPTEN_PATH:$CLANG_PATH:$NODE_PATH && + cd $PRJECTDIR && + emcmake cmake -S . -B build && + make PLATFORM=PLATFORM_WEB -B && + cmake --build build && + make -C build PLATFORM=PLATFORM_WEB -B && + python3 wasm-server.py \ No newline at end of file From 2b26c9714c2b8f71d818a55a038b241878222ab0 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Fri, 28 May 2021 18:08:51 +0200 Subject: [PATCH 04/60] workflow --- .github/workflows/webassembly.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/webassembly.yml b/.github/workflows/webassembly.yml index c839c9ef..db8befd4 100644 --- a/.github/workflows/webassembly.yml +++ b/.github/workflows/webassembly.yml @@ -16,6 +16,7 @@ jobs: shell: bash run: | PRJECTDIR=`pwd` + git clone https://github.com/emscripten-core/emsdk.git EMSDK_PATH=$HOME/emsdk cd $EMSDK_PATH && ./emsdk install tot && From f9e307e73b639972f7fd61fa968cc55cc7edfe36 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Fri, 28 May 2021 18:11:59 +0200 Subject: [PATCH 05/60] workflow --- .github/workflows/webassembly.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/webassembly.yml b/.github/workflows/webassembly.yml index db8befd4..02c8c5af 100644 --- a/.github/workflows/webassembly.yml +++ b/.github/workflows/webassembly.yml @@ -17,7 +17,7 @@ jobs: run: | PRJECTDIR=`pwd` git clone https://github.com/emscripten-core/emsdk.git - EMSDK_PATH=$HOME/emsdk + EMSDK_PATH=$PRJECTDIR/emsdk cd $EMSDK_PATH && ./emsdk install tot && ./emsdk install latest && From 0ffb8b2a1533026124140f466fb2c6a31cda5401 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Fri, 28 May 2021 22:41:46 +0200 Subject: [PATCH 06/60] more step toward wasm compilation, still not it --- .github/workflows/webassembly.yml | 1 + .gitignore | 3 ++- CMakeLists.txt | 3 ++- emsdk | 1 + emsdk.sh | 24 ++++++++---------------- lib/Ray/sources/Exceptions/RayError.hpp | 1 + lib/wal/sources/Wal.cpp | 6 +++--- lib/wal/sources/Wal.hpp | 20 +++++++++++--------- 8 files changed, 29 insertions(+), 30 deletions(-) create mode 160000 emsdk diff --git a/.github/workflows/webassembly.yml b/.github/workflows/webassembly.yml index 02c8c5af..3682dcb6 100644 --- a/.github/workflows/webassembly.yml +++ b/.github/workflows/webassembly.yml @@ -27,6 +27,7 @@ jobs: CLANG_PATH=$EMSDK_PATH/upstream/bin PATH=$PATH:$EMSDK_PATH:$EMSCRIPTEN_PATH:$CLANG_PATH:$NODE_PATH && cd $PRJECTDIR && + emcmake cmake -S . -B build && make PLATFORM=PLATFORM_WEB -B && cmake --build build && diff --git a/.gitignore b/.gitignore index 42cd6b29..51721e91 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ cmake-build-debug ./bomberman .vscode build/* -docs/* \ No newline at end of file +docs/* +emsdk/* \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 37b6a26b..6d851867 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,8 +11,9 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/lib/wal) add_subdirectory(${PROJECT_SOURCE_DIR}/lib/Ray) if (EMSCRIPTEN) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") - set(CMAKE_EXECUTABLE_SUFFIX ".html") # This line is used to set your executable to build with the emscripten html template so taht you can directly open it. + set(CMAKE_EXECUTABLE_SUFFIX ".html") endif () diff --git a/emsdk b/emsdk new file mode 160000 index 00000000..f26ebb74 --- /dev/null +++ b/emsdk @@ -0,0 +1 @@ +Subproject commit f26ebb74c0aa12d783a8552888cd9516b635838e diff --git a/emsdk.sh b/emsdk.sh index 3cd21922..75885814 100755 --- a/emsdk.sh +++ b/emsdk.sh @@ -1,20 +1,12 @@ #! /bin/bash PRJECTDIR=`pwd` -EMSDK_PATH=$HOME/emsdk -if [[ ! -d $HOME/emsdk ]]; then +EMSDK_PATH=./emsdk +if [[ ! -d ./emsdk ]]; then git clone https://github.com/emscripten-core/emsdk.git fi -cd $EMSDK_PATH && -./emsdk install tot && -./emsdk install latest && -./emsdk activate latest && -source ./emsdk_env.sh && -EMSCRIPTEN_PATH=$EMSDK_PATH/upstream/emscripten -CLANG_PATH=$EMSDK_PATH/upstream/bin -PATH=$PATH:$EMSDK_PATH:$EMSCRIPTEN_PATH:$CLANG_PATH:$NODE_PATH && -cd $PRJECTDIR && -emcmake cmake -S . -B build && -make PLATFORM=PLATFORM_WEB -B && -cmake --build build && -make -C build PLATFORM=PLATFORM_WEB -B && -python3 wasm-server.py +./emsdk/emsdk install tot +./emsdk/emsdk activate latest +source ./emsdk/emsdk_env.sh +mkdir -p build && +cmake -S . -B build && +cmake --build build \ No newline at end of file diff --git a/lib/Ray/sources/Exceptions/RayError.hpp b/lib/Ray/sources/Exceptions/RayError.hpp index 52068b22..f700ac17 100644 --- a/lib/Ray/sources/Exceptions/RayError.hpp +++ b/lib/Ray/sources/Exceptions/RayError.hpp @@ -9,6 +9,7 @@ #define RAYERROR_HPP_ #include +#include namespace RAY::Exception { //! @brief base exception class for RAY lib diff --git a/lib/wal/sources/Wal.cpp b/lib/wal/sources/Wal.cpp index 875e3bd8..6ab6542a 100644 --- a/lib/wal/sources/Wal.cpp +++ b/lib/wal/sources/Wal.cpp @@ -41,8 +41,8 @@ namespace WAL { // TODO use an hashmap to cache results. const auto &dependency = system.getDependencies(); - return std::ranges::all_of(dependency.begin(), dependency.end(), [&entity](const auto &dependency) { - return entity.hasComponent(dependency); - }); + //return std::ranges::all_of(dependency.begin(), dependency.end(), [&entity](const auto &dependency) { + // return entity.hasComponent(dependency); + //}); } } // namespace WAL \ No newline at end of file diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index c222862f..bce38bec 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -17,8 +17,8 @@ #ifdef PLATFORM_WEB #include -WAL::Wal *walPtr = nullptr; -void *callbackPtr = nullptr; +void *walPtr = nullptr; +void *statePtr = nullptr; #endif namespace WAL @@ -118,8 +118,8 @@ namespace WAL #ifdef PLATFORM_WEB walPtr = this; - callbackPtr = &callback; - return emscripten_set_main_loop_arg(runWASM, &state, 0, 1); + statePtr = &state; + return emscripten_set_main_loop_arg((em_arg_callback_func)&runIteration, (void *)&callback, 0, 1); #else return this->run(update, state); #endif @@ -152,9 +152,11 @@ namespace WAL #ifdef PLATFORM_WEB template - static void runIteration(T *state) + static void runIteration(void *callbackPtr) { - static std::function callback = callbackPtr; + static const Callback callback = *((Callback *)callbackPtr); + static Wal *wal = (Wal *)walPtr; + static T *state = (T *)statePtr; static auto lastTick = std::chrono::steady_clock::now(); static std::chrono::nanoseconds fBehind(0); @@ -164,10 +166,10 @@ namespace WAL lastTick = now; while (fBehind > Wal::timestep) { fBehind -= Wal::timestep; - this->_fixedUpdate(); + wal->_fixedUpdate(); } - walPtr->_update(dtime); - callback(wal, state); + wal->_update(dtime); + callback(*wal, *state); } #endif From 5498649829312dea5dcb59de32fd01ef73021502 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Sat, 29 May 2021 12:54:39 +0200 Subject: [PATCH 07/60] add missing compilation flag --- CMakeLists.txt | 12 ++++++------ emsdk | 1 - emsdk.sh | 2 +- lib/wal/sources/Wal.hpp | 16 +++++++--------- 4 files changed, 14 insertions(+), 17 deletions(-) delete mode 160000 emsdk diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d851867..d2cc777a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,12 @@ project(bomberman) set(CMAKE_CXX_STANDARD 20) +if (EMSCRIPTEN) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") + set(CMAKE_EXECUTABLE_SUFFIX ".html") +endif () + include_directories(bomberman lib/Ray/sources) include_directories(bomberman lib/wal/sources) include_directories(bomberman sources) @@ -10,12 +16,6 @@ include_directories(bomberman sources) add_subdirectory(${PROJECT_SOURCE_DIR}/lib/wal) add_subdirectory(${PROJECT_SOURCE_DIR}/lib/Ray) -if (EMSCRIPTEN) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") - set(CMAKE_EXECUTABLE_SUFFIX ".html") -endif () - set(SOURCES sources/Models/GameState.hpp diff --git a/emsdk b/emsdk deleted file mode 160000 index f26ebb74..00000000 --- a/emsdk +++ /dev/null @@ -1 +0,0 @@ -Subproject commit f26ebb74c0aa12d783a8552888cd9516b635838e diff --git a/emsdk.sh b/emsdk.sh index 75885814..4b88ab77 100755 --- a/emsdk.sh +++ b/emsdk.sh @@ -8,5 +8,5 @@ fi ./emsdk/emsdk activate latest source ./emsdk/emsdk_env.sh mkdir -p build && -cmake -S . -B build && +emcmake cmake -S . -B build -DPLATFORM=Web && cmake --build build \ No newline at end of file diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index bce38bec..8feb2507 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -17,8 +17,6 @@ #ifdef PLATFORM_WEB #include -void *walPtr = nullptr; -void *statePtr = nullptr; #endif namespace WAL @@ -117,9 +115,8 @@ namespace WAL Callback update(callback); #ifdef PLATFORM_WEB - walPtr = this; - statePtr = &state; - return emscripten_set_main_loop_arg((em_arg_callback_func)&runIteration, (void *)&callback, 0, 1); + void *paramPtr[3] = {this, &callback, &state}; + return emscripten_set_main_loop_arg((em_arg_callback_func)&runIteration, (void *)¶mPtr, 0, 1); #else return this->run(update, state); #endif @@ -152,11 +149,12 @@ namespace WAL #ifdef PLATFORM_WEB template - static void runIteration(void *callbackPtr) + static void runIteration(void *param) { - static const Callback callback = *((Callback *)callbackPtr); - static Wal *wal = (Wal *)walPtr; - static T *state = (T *)statePtr; + void *paramsPtr[3] = *param; + static const Callback callback = *((Callback *)param[1]); + static Wal *wal = (Wal *)param[0]; + static T *state = (T *)param[2]; static auto lastTick = std::chrono::steady_clock::now(); static std::chrono::nanoseconds fBehind(0); From ab8ee23e4015edf07a42ca8d05ba1f04df17d106 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Sat, 29 May 2021 13:46:28 +0200 Subject: [PATCH 08/60] ok --- CMakeLists.txt | 11 +++++------ emsdk.sh | 4 ++-- lib/wal/sources/Wal.hpp | 16 ++++++++-------- wasm-server.py | 2 +- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d2cc777a..143502f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,12 +3,6 @@ project(bomberman) set(CMAKE_CXX_STANDARD 20) -if (EMSCRIPTEN) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") - set(CMAKE_EXECUTABLE_SUFFIX ".html") -endif () - include_directories(bomberman lib/Ray/sources) include_directories(bomberman lib/wal/sources) include_directories(bomberman sources) @@ -16,6 +10,11 @@ include_directories(bomberman sources) add_subdirectory(${PROJECT_SOURCE_DIR}/lib/wal) add_subdirectory(${PROJECT_SOURCE_DIR}/lib/Ray) +if (EMSCRIPTEN) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") + set(CMAKE_EXECUTABLE_SUFFIX ".html") +endif () set(SOURCES sources/Models/GameState.hpp diff --git a/emsdk.sh b/emsdk.sh index 4b88ab77..4ffd22d2 100755 --- a/emsdk.sh +++ b/emsdk.sh @@ -4,9 +4,9 @@ EMSDK_PATH=./emsdk if [[ ! -d ./emsdk ]]; then git clone https://github.com/emscripten-core/emsdk.git fi -./emsdk/emsdk install tot +./emsdk/emsdk install latest ./emsdk/emsdk activate latest source ./emsdk/emsdk_env.sh -mkdir -p build && +mkdir -p build emcmake cmake -S . -B build -DPLATFORM=Web && cmake --build build \ No newline at end of file diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index 8feb2507..6d3121de 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -15,7 +15,7 @@ #include "System/System.hpp" #include "Models/Callback.hpp" -#ifdef PLATFORM_WEB +#if defined(PLATFORM_WEB) #include #endif @@ -115,8 +115,8 @@ namespace WAL Callback update(callback); #ifdef PLATFORM_WEB - void *paramPtr[3] = {this, &callback, &state}; - return emscripten_set_main_loop_arg((em_arg_callback_func)&runIteration, (void *)¶mPtr, 0, 1); + void *paramPtr[3] = {(void *)this, (void *)&callback, (void *)&state}; + return emscripten_set_main_loop_arg((em_arg_callback_func)&runIteration, (void *)paramPtr, 0, 1); #else return this->run(update, state); #endif @@ -147,14 +147,14 @@ namespace WAL } } - #ifdef PLATFORM_WEB + #if defined(PLATFORM_WEB) template static void runIteration(void *param) { - void *paramsPtr[3] = *param; - static const Callback callback = *((Callback *)param[1]); - static Wal *wal = (Wal *)param[0]; - static T *state = (T *)param[2]; + void **paramsPtr = (void **)param; + static const Callback callback = *((Callback *)paramsPtr[1]); + static Wal *wal = (Wal *)paramsPtr[0]; + static T *state = (T *)paramsPtr[2]; static auto lastTick = std::chrono::steady_clock::now(); static std::chrono::nanoseconds fBehind(0); diff --git a/wasm-server.py b/wasm-server.py index 77aad1db..d9dde32c 100644 --- a/wasm-server.py +++ b/wasm-server.py @@ -18,7 +18,7 @@ if sys.version_info < (3, 7, 5): if __name__ == '__main__': - PORT = 8080 + PORT = 8081 with socketserver.TCPServer(("", PORT), WasmHandler) as httpd: print("Listening on port {}. Press Ctrl+C to stop.".format(PORT)) httpd.serve_forever() From 80735abea55f5792fc3a9570e787a8b0e114ee65 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Sat, 29 May 2021 21:11:29 +0200 Subject: [PATCH 09/60] fix ranges loop, which didn't let the compuilation go with em++ --- .gitignore | 2 +- emsdk.sh | 1 + lib/wal/sources/Wal.cpp | 8 +++++--- lib/wal/sources/Wal.hpp | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 51721e91..06e5830c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ cmake-build-debug .vscode build/* docs/* -emsdk/* \ No newline at end of file +emsdk/ \ No newline at end of file diff --git a/emsdk.sh b/emsdk.sh index 4ffd22d2..70cd7654 100755 --- a/emsdk.sh +++ b/emsdk.sh @@ -1,4 +1,5 @@ #! /bin/bash +rm -rf build PRJECTDIR=`pwd` EMSDK_PATH=./emsdk if [[ ! -d ./emsdk ]]; then diff --git a/lib/wal/sources/Wal.cpp b/lib/wal/sources/Wal.cpp index 6ab6542a..371c5774 100644 --- a/lib/wal/sources/Wal.cpp +++ b/lib/wal/sources/Wal.cpp @@ -41,8 +41,10 @@ namespace WAL { // TODO use an hashmap to cache results. const auto &dependency = system.getDependencies(); - //return std::ranges::all_of(dependency.begin(), dependency.end(), [&entity](const auto &dependency) { - // return entity.hasComponent(dependency); - //}); + for (const auto &dependency : system.getDependencies()) { + if (!entity.hasComponent(dependency)) + return false; + } + return true; } } // namespace WAL \ No newline at end of file diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index 6d3121de..7b9ea705 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -115,7 +115,7 @@ namespace WAL Callback update(callback); #ifdef PLATFORM_WEB - void *paramPtr[3] = {(void *)this, (void *)&callback, (void *)&state}; + void *paramPtr[3] = {(void *)this, (void *)&update, (void *)&state}; return emscripten_set_main_loop_arg((em_arg_callback_func)&runIteration, (void *)paramPtr, 0, 1); #else return this->run(update, state); From 1dc217cdaa9c68832d64636755e278dfb15a36ca Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Sat, 29 May 2021 22:52:40 +0200 Subject: [PATCH 10/60] assets preloaded, updating workflow --- .github/workflows/webassembly.yml | 24 ++++++------------------ CMakeLists.txt | 2 +- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/.github/workflows/webassembly.yml b/.github/workflows/webassembly.yml index 3682dcb6..33f1671b 100644 --- a/.github/workflows/webassembly.yml +++ b/.github/workflows/webassembly.yml @@ -14,22 +14,10 @@ jobs: ref: master - name: Exec shell: bash + run: ./emsdk.sh + - name: Check files creation run: | - PRJECTDIR=`pwd` - git clone https://github.com/emscripten-core/emsdk.git - EMSDK_PATH=$PRJECTDIR/emsdk - cd $EMSDK_PATH && - ./emsdk install tot && - ./emsdk install latest && - ./emsdk activate latest && - source ./emsdk_env.sh && - EMSCRIPTEN_PATH=$EMSDK_PATH/upstream/emscripten - CLANG_PATH=$EMSDK_PATH/upstream/bin - PATH=$PATH:$EMSDK_PATH:$EMSCRIPTEN_PATH:$CLANG_PATH:$NODE_PATH && - cd $PRJECTDIR && - - emcmake cmake -S . -B build && - make PLATFORM=PLATFORM_WEB -B && - cmake --build build && - make -C build PLATFORM=PLATFORM_WEB -B && - python3 wasm-server.py \ No newline at end of file + test -f build/bomberman.html + test -f build/bomberman.js + test -f build/bomberman.wasm + test -f build/bomberman.data diff --git a/CMakeLists.txt b/CMakeLists.txt index 143502f5..84be48cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/lib/Ray) if (EMSCRIPTEN) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY --preload-file ../assets") set(CMAKE_EXECUTABLE_SUFFIX ".html") endif () From f4c94c6cc58a22e44a081a7eb763d67e36417d22 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Sat, 29 May 2021 22:54:12 +0200 Subject: [PATCH 11/60] fixing workflow, using action wit current branch --- .github/workflows/webassembly.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/webassembly.yml b/.github/workflows/webassembly.yml index 33f1671b..70cc786e 100644 --- a/.github/workflows/webassembly.yml +++ b/.github/workflows/webassembly.yml @@ -11,7 +11,6 @@ jobs: - uses: actions/checkout@v1 with: submodules: true - ref: master - name: Exec shell: bash run: ./emsdk.sh From 74ace1cfda65d10996a22b035e244fce134acfa2 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Sat, 29 May 2021 22:56:52 +0200 Subject: [PATCH 12/60] worflow now based on linux --- .github/workflows/webassembly.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/webassembly.yml b/.github/workflows/webassembly.yml index 70cc786e..b514e6eb 100644 --- a/.github/workflows/webassembly.yml +++ b/.github/workflows/webassembly.yml @@ -6,7 +6,7 @@ on: jobs: Push: - runs-on: windows-latest + runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 with: From 0f41290b0fbe29dd777591f5c700464691d2ec3e Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Mon, 31 May 2021 09:17:13 +0200 Subject: [PATCH 13/60] seperate std buld from build for web --- .gitignore | 3 ++- emsdk.sh | 8 +++----- lib/wal/sources/Wal.hpp | 8 ++++---- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 06e5830c..9e44c2a3 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ cmake-build-debug .vscode build/* docs/* -emsdk/ \ No newline at end of file +emsdk/ +build_web/ \ No newline at end of file diff --git a/emsdk.sh b/emsdk.sh index 70cd7654..b5975bba 100755 --- a/emsdk.sh +++ b/emsdk.sh @@ -1,6 +1,4 @@ #! /bin/bash -rm -rf build -PRJECTDIR=`pwd` EMSDK_PATH=./emsdk if [[ ! -d ./emsdk ]]; then git clone https://github.com/emscripten-core/emsdk.git @@ -8,6 +6,6 @@ fi ./emsdk/emsdk install latest ./emsdk/emsdk activate latest source ./emsdk/emsdk_env.sh -mkdir -p build -emcmake cmake -S . -B build -DPLATFORM=Web && -cmake --build build \ No newline at end of file +mkdir -p build_web +emcmake cmake -S . -B build_web -DPLATFORM=Web && +cmake --build build_web \ No newline at end of file diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index 7b9ea705..a2feacbe 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -114,9 +114,9 @@ namespace WAL { Callback update(callback); - #ifdef PLATFORM_WEB + #if defined(PLATFORM_WEB) void *paramPtr[3] = {(void *)this, (void *)&update, (void *)&state}; - return emscripten_set_main_loop_arg((em_arg_callback_func)&runIteration, (void *)paramPtr, 0, 1); + return emscripten_set_main_loop_arg((em_arg_callback_func)runIteration, (void *)paramPtr, 0, 1); #else return this->run(update, state); #endif @@ -151,9 +151,9 @@ namespace WAL template static void runIteration(void *param) { - void **paramsPtr = (void **)param; - static const Callback callback = *((Callback *)paramsPtr[1]); + static void **paramsPtr = (void **)param; static Wal *wal = (Wal *)paramsPtr[0]; + static const Callback callback = *((Callback *)paramsPtr[1]); static T *state = (T *)paramsPtr[2]; static auto lastTick = std::chrono::steady_clock::now(); static std::chrono::nanoseconds fBehind(0); From 1cc6907ff024b9efab303dd78f97f26c5c0705f3 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Tue, 1 Jun 2021 13:52:52 +0200 Subject: [PATCH 14/60] CI: update build folder name --- .github/workflows/webassembly.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/webassembly.yml b/.github/workflows/webassembly.yml index b514e6eb..0b729418 100644 --- a/.github/workflows/webassembly.yml +++ b/.github/workflows/webassembly.yml @@ -16,7 +16,7 @@ jobs: run: ./emsdk.sh - name: Check files creation run: | - test -f build/bomberman.html - test -f build/bomberman.js - test -f build/bomberman.wasm - test -f build/bomberman.data + test -f build_web/bomberman.html + test -f build_web/bomberman.js + test -f build_web/bomberman.wasm + test -f build_web/bomberman.data From e298ce03eb784c5f0908417b427e6f51b0405bb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Tue, 1 Jun 2021 15:52:47 +0200 Subject: [PATCH 15/60] starting to implement a AnimationComponent.cpp/.hpp --- CMakeLists.txt | 2 ++ .../Animation/AnimationComponent.cpp | 16 ++++++++++ .../Animation/AnimationComponent.hpp | 31 +++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 sources/Component/Animation/AnimationComponent.cpp create mode 100644 sources/Component/Animation/AnimationComponent.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 763064ac..81b3c154 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,8 @@ set(SOURCES sources/Component/Renderer/CameraComponent.hpp sources/System/Renderer/Render2DScreenSystem.cpp sources/System/Renderer/Render2DScreenSystem.hpp + sources/Component/Animation/AnimationComponent.cpp + sources/Component/Animation/AnimationComponent.hpp ) add_executable(bomberman diff --git a/sources/Component/Animation/AnimationComponent.cpp b/sources/Component/Animation/AnimationComponent.cpp new file mode 100644 index 00000000..d1f8210b --- /dev/null +++ b/sources/Component/Animation/AnimationComponent.cpp @@ -0,0 +1,16 @@ +// +// Created by cbihan on 01/06/2021. +// + +#include "AnimationComponent.hpp" +#include "Entity/Entity.hpp" + +namespace BBM +{ + AnimationComponent::AnimationComponent(WAL::Entity &entity, RAY::ModelAnimation &modelAnimation) + : WAL::Component(entity), + _modelAnimation(modelAnimation) + { + + } +} \ No newline at end of file diff --git a/sources/Component/Animation/AnimationComponent.hpp b/sources/Component/Animation/AnimationComponent.hpp new file mode 100644 index 00000000..36946c16 --- /dev/null +++ b/sources/Component/Animation/AnimationComponent.hpp @@ -0,0 +1,31 @@ +// +// Created by cbihan on 01/06/2021. +// + +#pragma once + +#include +#include "Component/Component.hpp" +#include "Entity/Entity.hpp" +#include "Model/Model.hpp" + +namespace BBM +{ + + class AnimationComponent : public WAL::Component + { + private: + //! @brief To get the animation data + RAY::ModelAnimation &_modelAnimation; + public: + //! @brief ctor entity and the path of the animation file + explicit AnimationComponent(WAL::Entity &entity, RAY::ModelAnimation &modelAnimation); + //! @brief copy ctor + AnimationComponent(const AnimationComponent &) = default; + //! @brief dtor + ~AnimationComponent() override = default; + //! @brief assignment operator + AnimationComponent &operator=(const AnimationComponent &) = delete; + }; + +} From b4d783d6c21788a9469e13331138488aada318e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Tue, 1 Jun 2021 17:26:01 +0200 Subject: [PATCH 16/60] adding AnimationsSystem.cpp --- CMakeLists.txt | 9 ++-- .../Animation/AnimationComponent.cpp | 16 ------- .../Animation/AnimationComponent.hpp | 31 ------------ .../Animation/AnimationsComponent.cpp | 43 +++++++++++++++++ .../Animation/AnimationsComponent.hpp | 47 +++++++++++++++++++ sources/System/Animation/AnimationsSystem.cpp | 27 +++++++++++ sources/System/Animation/AnimationsSystem.hpp | 25 ++++++++++ .../Controllable/ControllableSystem.cpp | 2 - .../Controllable/ControllableSystem.hpp | 2 +- 9 files changed, 149 insertions(+), 53 deletions(-) delete mode 100644 sources/Component/Animation/AnimationComponent.cpp delete mode 100644 sources/Component/Animation/AnimationComponent.hpp create mode 100644 sources/Component/Animation/AnimationsComponent.cpp create mode 100644 sources/Component/Animation/AnimationsComponent.hpp create mode 100644 sources/System/Animation/AnimationsSystem.cpp create mode 100644 sources/System/Animation/AnimationsSystem.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 81b3c154..b17dc1aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,13 +54,16 @@ set(SOURCES sources/Component/Renderer/CameraComponent.hpp sources/System/Renderer/Render2DScreenSystem.cpp sources/System/Renderer/Render2DScreenSystem.hpp - sources/Component/Animation/AnimationComponent.cpp - sources/Component/Animation/AnimationComponent.hpp + sources/Component/Animation/AnimationsComponent.cpp + sources/Component/Animation/AnimationsComponent.hpp + sources/System/Animation/AnimationsSystem.cpp + sources/System/Animation/AnimationsSystem.hpp ) add_executable(bomberman sources/main.cpp - ${SOURCES}) + ${SOURCES} + ) target_include_directories(bomberman PUBLIC sources) target_link_libraries(bomberman PUBLIC wal ray) diff --git a/sources/Component/Animation/AnimationComponent.cpp b/sources/Component/Animation/AnimationComponent.cpp deleted file mode 100644 index d1f8210b..00000000 --- a/sources/Component/Animation/AnimationComponent.cpp +++ /dev/null @@ -1,16 +0,0 @@ -// -// Created by cbihan on 01/06/2021. -// - -#include "AnimationComponent.hpp" -#include "Entity/Entity.hpp" - -namespace BBM -{ - AnimationComponent::AnimationComponent(WAL::Entity &entity, RAY::ModelAnimation &modelAnimation) - : WAL::Component(entity), - _modelAnimation(modelAnimation) - { - - } -} \ No newline at end of file diff --git a/sources/Component/Animation/AnimationComponent.hpp b/sources/Component/Animation/AnimationComponent.hpp deleted file mode 100644 index 36946c16..00000000 --- a/sources/Component/Animation/AnimationComponent.hpp +++ /dev/null @@ -1,31 +0,0 @@ -// -// Created by cbihan on 01/06/2021. -// - -#pragma once - -#include -#include "Component/Component.hpp" -#include "Entity/Entity.hpp" -#include "Model/Model.hpp" - -namespace BBM -{ - - class AnimationComponent : public WAL::Component - { - private: - //! @brief To get the animation data - RAY::ModelAnimation &_modelAnimation; - public: - //! @brief ctor entity and the path of the animation file - explicit AnimationComponent(WAL::Entity &entity, RAY::ModelAnimation &modelAnimation); - //! @brief copy ctor - AnimationComponent(const AnimationComponent &) = default; - //! @brief dtor - ~AnimationComponent() override = default; - //! @brief assignment operator - AnimationComponent &operator=(const AnimationComponent &) = delete; - }; - -} diff --git a/sources/Component/Animation/AnimationsComponent.cpp b/sources/Component/Animation/AnimationsComponent.cpp new file mode 100644 index 00000000..d32c6818 --- /dev/null +++ b/sources/Component/Animation/AnimationsComponent.cpp @@ -0,0 +1,43 @@ +// +// Created by cbihan on 01/06/2021. +// + +#include "AnimationsComponent.hpp" +#include "Entity/Entity.hpp" +#include "Model/ModelAnimations.hpp" + +namespace BBM +{ + AnimationsComponent::AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations &modelAnimation) + : WAL::Component(entity), + _modelAnimation(modelAnimation), + _animFrameCounter(0) + { + } + + WAL::Component *AnimationsComponent::clone(WAL::Entity &entity) const + { + return new AnimationsComponent(entity, this->_modelAnimation); + } + + size_t AnimationsComponent::getAnimFrameCounter() const + { + return this->_animFrameCounter; + } + + RAY::ModelAnimation AnimationsComponent::getCurrentModelAnim() const + { + return this->_modelAnimation[static_cast(this->_animFrameCounter)]; + } + + void AnimationsComponent::setAnimFrameCounter(size_t animFrameCounter) + { + this->_animFrameCounter = animFrameCounter; + this->_animFrameCounter %= this->_modelAnimation.getAnimationsCount(); + } + + void AnimationsComponent::resetAnimFrameCounter() + { + this->_animFrameCounter = 0; + } +} \ No newline at end of file diff --git a/sources/Component/Animation/AnimationsComponent.hpp b/sources/Component/Animation/AnimationsComponent.hpp new file mode 100644 index 00000000..a3d741a9 --- /dev/null +++ b/sources/Component/Animation/AnimationsComponent.hpp @@ -0,0 +1,47 @@ +// +// Created by cbihan on 01/06/2021. +// + +#pragma once + +#include +#include +#include +#include + +namespace BBM +{ + class AnimationsComponent : public WAL::Component + { + private: + //! @brief To get the animation data + RAY::ModelAnimations &_modelAnimation; + //! @brief the frame animation counter + size_t _animFrameCounter; + public: + //! @inherit + WAL::Component *clone(WAL::Entity &entity) const override; + + //! @brief get animation frame counter + size_t getAnimFrameCounter() const; + + //! @brief get the current + RAY::ModelAnimation getCurrentModelAnim() const; + + //! @brief + void setAnimFrameCounter(size_t animFrameCounter); + + //! @brief Set the internal anim counter to 0 + void resetAnimFrameCounter(); + + //! @brief ctor entity and the path of the animation file + explicit AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations &modelAnimation); + //! @brief copy ctor + AnimationsComponent(const AnimationsComponent &) = default; + //! @brief dtor + ~AnimationsComponent() override = default; + //! @brief assignment operator + AnimationsComponent &operator=(const AnimationsComponent &) = delete; + }; + +} diff --git a/sources/System/Animation/AnimationsSystem.cpp b/sources/System/Animation/AnimationsSystem.cpp new file mode 100644 index 00000000..42425acf --- /dev/null +++ b/sources/System/Animation/AnimationsSystem.cpp @@ -0,0 +1,27 @@ +// +// Created by cbihan on 01/06/2021. +// + +#include "AnimationsSystem.hpp" +#include "Component/Animation/AnimationsComponent.hpp" +#include "Model/Model.hpp" +#include "Component/Renderer/Drawable3DComponent.hpp" + +namespace BBM +{ + + AnimationsSystem::AnimationsSystem() + : WAL::System({typeid(AnimationsComponent), + typeid(Drawable3DComponent)}) + { + } + + void AnimationsSystem::onFixedUpdate(WAL::Entity &entity) + { + auto &model = entity.getComponent>(); + auto &anim = entity.getComponent(); + + model.member.setAnimation(anim.getCurrentModelAnim()); + anim.setAnimFrameCounter(anim.getAnimFrameCounter() + 1); + } +} \ No newline at end of file diff --git a/sources/System/Animation/AnimationsSystem.hpp b/sources/System/Animation/AnimationsSystem.hpp new file mode 100644 index 00000000..eb508b3e --- /dev/null +++ b/sources/System/Animation/AnimationsSystem.hpp @@ -0,0 +1,25 @@ +// +// Created by cbihan on 01/06/2021. +// + +#pragma once + +#include + +namespace BBM +{ + class AnimationsSystem : public WAL::System + { + //! @inherit + void onFixedUpdate(WAL::Entity &entity) override; + + //! @brief A default constructor + AnimationsSystem(); + //! @brief A Controllable system is copy constructable + AnimationsSystem(const AnimationsSystem &) = default; + //! @brief A default destructor + ~AnimationsSystem() override = default; + //! @brief A Controllable system is assignable. + AnimationsSystem &operator=(const AnimationsSystem &) = default; + }; +} \ No newline at end of file diff --git a/sources/System/Controllable/ControllableSystem.cpp b/sources/System/Controllable/ControllableSystem.cpp index 0a9c789c..41c54895 100644 --- a/sources/System/Controllable/ControllableSystem.cpp +++ b/sources/System/Controllable/ControllableSystem.cpp @@ -10,8 +10,6 @@ namespace BBM { - float ControllableSystem::speed = .25f; - ControllableSystem::ControllableSystem() : WAL::System({ typeid(ControllableComponent), diff --git a/sources/System/Controllable/ControllableSystem.hpp b/sources/System/Controllable/ControllableSystem.hpp index fac4db08..ec6eef9d 100644 --- a/sources/System/Controllable/ControllableSystem.hpp +++ b/sources/System/Controllable/ControllableSystem.hpp @@ -14,7 +14,7 @@ namespace BBM { public: //! @brief The speed applied to every controllable entities. - static float speed; + static constexpr const float speed = .25f; //! @inherit void onFixedUpdate(WAL::Entity &entity) override; From ef0c323d945f2e8d8f6a70e8ab0eb8e1199d44b1 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Wed, 2 Jun 2021 14:33:52 +0200 Subject: [PATCH 17/60] addition + correct drawing modes --- lib/Ray/sources/Window.cpp | 5 +++++ lib/Ray/sources/Window.hpp | 4 +++- sources/Runner/Runner.cpp | 17 +++++++++++------ .../System/Renderer/Render2DScreenSystem.cpp | 8 +++++--- .../System/Renderer/Render2DScreenSystem.hpp | 3 --- sources/System/Renderer/RenderScreenSystem.cpp | 14 +++++++++++--- sources/System/Renderer/Renderer2DSystem.hpp | 3 ++- sources/System/Renderer/Renderer3DSystem.hpp | 2 +- 8 files changed, 38 insertions(+), 18 deletions(-) diff --git a/lib/Ray/sources/Window.cpp b/lib/Ray/sources/Window.cpp index c30c2575..9deba6e0 100644 --- a/lib/Ray/sources/Window.cpp +++ b/lib/Ray/sources/Window.cpp @@ -167,3 +167,8 @@ void RAY::Window::setIcon(RAY::Image &img) { SetWindowIcon(img); } + +bool RAY::Window::isReady() const +{ + return IsWindowReady(); +} \ No newline at end of file diff --git a/lib/Ray/sources/Window.hpp b/lib/Ray/sources/Window.hpp index 91acd870..c16eae4b 100644 --- a/lib/Ray/sources/Window.hpp +++ b/lib/Ray/sources/Window.hpp @@ -7,7 +7,7 @@ #ifndef WINDOW_HPP_ #define WINDOW_HPP_ - +#define INTERNAL public #include #include #include @@ -131,6 +131,8 @@ namespace RAY { //! @brief Draw a 3d mesh with material and transform void draw(const Mesh &mesh, const Material &material, const Matrix &transform); + bool isReady() const; + private: //! @brief Creates window, and opens it if openNow is set to true diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 9e256b05..c97c125d 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -34,27 +35,31 @@ namespace BBM void enableRaylib(WAL::Wal &wal) { - RAY::TraceLog::setLevel(LOG_WARNING); - RAY::Window &window = RAY::Window::getInstance(600, 400, "Bomberman", FLAG_WINDOW_RESIZABLE); + //RAY::TraceLog::setLevel(LOG_WARNING); + RAY::Window &window = RAY::Window::getInstance(800, 600, "Bomberman", FLAG_WINDOW_RESIZABLE); - wal.addSystem>(); + wal.addSystem(window) + .addSystem>() + .addSystem>(); wal.addSystem(window) .addSystem>(); - wal.addSystem(window); } std::shared_ptr loadGameScene() { auto scene = std::make_shared(); scene->addEntity("cube") + .addComponent(10, 10, 0) + .addComponent>(Vector2f(), Vector2f(10, 10), GREEN); + scene->addEntity("cube2") .addComponent() - .addComponent>(Vector2f(), Vector2f(10, 10), RED); + .addComponent>(Vector3f(), Vector3f(1, 1, 1), RED); scene->addEntity("player") .addComponent() .addComponent>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")); scene->addEntity("camera") - .addComponent(10, 10, 10) + .addComponent(10, 10, 15) .addComponent(); return scene; } diff --git a/sources/System/Renderer/Render2DScreenSystem.cpp b/sources/System/Renderer/Render2DScreenSystem.cpp index 938d107f..b21c35da 100644 --- a/sources/System/Renderer/Render2DScreenSystem.cpp +++ b/sources/System/Renderer/Render2DScreenSystem.cpp @@ -8,12 +8,14 @@ namespace BBM { Render2DScreenSystem::Render2DScreenSystem(RAY::Window &window) : WAL::System({}), - _window(window), - _camera(RAY::Vector2(10, 10), RAY::Vector2(), 0) + _window(window) {} void Render2DScreenSystem::onSelfUpdate() { - this->_window.useCamera(this->_camera); + EndMode3D(); + printf("EndMode3D\n"); + DrawText("Try selecting the box with mouse!", 10, 10, 20, WHITE); + //this->_window.unuseCamera(); } } \ No newline at end of file diff --git a/sources/System/Renderer/Render2DScreenSystem.hpp b/sources/System/Renderer/Render2DScreenSystem.hpp index 12adaf34..4fa00ba1 100644 --- a/sources/System/Renderer/Render2DScreenSystem.hpp +++ b/sources/System/Renderer/Render2DScreenSystem.hpp @@ -13,9 +13,6 @@ namespace BBM { //! @brief The window to render on RAY::Window &_window; - - //! @brief The camera used to render. - RAY::Camera::Camera2D _camera; public: //! @brief A method called after all entities that this system manage has been updated. //! @note render on screen here diff --git a/sources/System/Renderer/RenderScreenSystem.cpp b/sources/System/Renderer/RenderScreenSystem.cpp index 5523ee48..1550bd65 100644 --- a/sources/System/Renderer/RenderScreenSystem.cpp +++ b/sources/System/Renderer/RenderScreenSystem.cpp @@ -15,18 +15,26 @@ namespace BBM }), _window(window), _camera(Vector3f(), Vector3f(), Vector3f(0, 1, 0), 50, CAMERA_PERSPECTIVE) - {} + { + this->_camera.setMode(CAMERA_FREE); + } void RenderScreenSystem::onSelfUpdate() { - this->_window.draw(); + //this->_window.draw(); + //EndMode2D(); + EndDrawing(); + this->_camera.update(); + BeginDrawing(); this->_window.clear(); - this->_window.useCamera(this->_camera); + BeginMode3D(this->_camera); + printf("BeginMode3D\n"); } void RenderScreenSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) { const auto &pos = entity.getComponent(); _camera.setPosition(pos.position); + //this->_camera.update(); } } \ No newline at end of file diff --git a/sources/System/Renderer/Renderer2DSystem.hpp b/sources/System/Renderer/Renderer2DSystem.hpp index a1622c0d..3fd55346 100644 --- a/sources/System/Renderer/Renderer2DSystem.hpp +++ b/sources/System/Renderer/Renderer2DSystem.hpp @@ -35,7 +35,8 @@ namespace BBM auto &pos = entity.getComponent(); comp.member.setPosition({pos.getX(), pos.getY()}); - comp.member.drawOn(this->_window); + this->_window.draw(comp.member); + printf("Drawing smth\n"); } //! @brief default copy ctor diff --git a/sources/System/Renderer/Renderer3DSystem.hpp b/sources/System/Renderer/Renderer3DSystem.hpp index 8dba98f6..0fa3a9bb 100644 --- a/sources/System/Renderer/Renderer3DSystem.hpp +++ b/sources/System/Renderer/Renderer3DSystem.hpp @@ -35,7 +35,7 @@ namespace BBM auto &pos = entity.getComponent(); comp.member.setPosition(static_cast(pos.position)); - comp.member.drawOn(this->_window); + this->_window.draw(comp.member); } //! @brief Default copy ctor From d986a24c47025bc4de9127aadd9ebe15c74fe920 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Wed, 2 Jun 2021 15:08:46 +0200 Subject: [PATCH 18/60] animation component should theoretically work --- lib/Ray/sources/Model/ModelAnimations.cpp | 2 +- .../Animation/AnimationsComponent.cpp | 37 +++++++++++++------ .../Animation/AnimationsComponent.hpp | 23 ++++++++---- sources/System/Animation/AnimationsSystem.cpp | 2 +- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/lib/Ray/sources/Model/ModelAnimations.cpp b/lib/Ray/sources/Model/ModelAnimations.cpp index 224b9979..2b7328f4 100644 --- a/lib/Ray/sources/Model/ModelAnimations.cpp +++ b/lib/Ray/sources/Model/ModelAnimations.cpp @@ -13,7 +13,7 @@ RAY::ModelAnimations::ModelAnimations(const std::string &filePath): ::ModelAnimation *ptr = this->_animationsPtr.get(); for (int i = 0; i < this->_animationCount; i++) - this->_animations.push_back(RAY::ModelAnimation(ptr[i])); + this->_animations.emplace_back(RAY::ModelAnimation(ptr[i])); } RAY::ModelAnimations::~ModelAnimations() diff --git a/sources/Component/Animation/AnimationsComponent.cpp b/sources/Component/Animation/AnimationsComponent.cpp index d32c6818..ce29964d 100644 --- a/sources/Component/Animation/AnimationsComponent.cpp +++ b/sources/Component/Animation/AnimationsComponent.cpp @@ -8,36 +8,51 @@ namespace BBM { - AnimationsComponent::AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations &modelAnimation) + AnimationsComponent::AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations &modelAnimation, int animIndex) : WAL::Component(entity), _modelAnimation(modelAnimation), - _animFrameCounter(0) + _currentAnimIndex(animIndex) { + this->_modelAnimation[this->_currentAnimIndex].setFrameCounter(0); } WAL::Component *AnimationsComponent::clone(WAL::Entity &entity) const { - return new AnimationsComponent(entity, this->_modelAnimation); + return new AnimationsComponent(entity, this->_modelAnimation, this->_currentAnimIndex); } - size_t AnimationsComponent::getAnimFrameCounter() const + size_t AnimationsComponent::getCurrentAnimFrameCounter() const { - return this->_animFrameCounter; + return this->_modelAnimation[this->_currentAnimIndex].getFrameCounter(); } RAY::ModelAnimation AnimationsComponent::getCurrentModelAnim() const { - return this->_modelAnimation[static_cast(this->_animFrameCounter)]; + return this->_modelAnimation[this->_currentAnimIndex]; } - void AnimationsComponent::setAnimFrameCounter(size_t animFrameCounter) + void AnimationsComponent::setCurrentAnimFrameCounter(size_t animFrameCounter) { - this->_animFrameCounter = animFrameCounter; - this->_animFrameCounter %= this->_modelAnimation.getAnimationsCount(); + this->_modelAnimation[this->_currentAnimIndex].setFrameCounter(animFrameCounter); } - void AnimationsComponent::resetAnimFrameCounter() + void AnimationsComponent::resetCurrentAnimFrameCounter() { - this->_animFrameCounter = 0; + this->_modelAnimation[this->_currentAnimIndex].setFrameCounter(0); + } + + size_t AnimationsComponent::getCurrentAnimIndex() const + { + return this->_currentAnimIndex; + } + + void AnimationsComponent::setAnimIndex(int animIndex) + { + this->_currentAnimIndex = animIndex % static_cast(this->_modelAnimation.getAnimationsCount()); + } + + void AnimationsComponent::incCurrentAnimFrameCounter() + { + this->_modelAnimation[this->_currentAnimIndex].incrementFrameCounter(); } } \ No newline at end of file diff --git a/sources/Component/Animation/AnimationsComponent.hpp b/sources/Component/Animation/AnimationsComponent.hpp index a3d741a9..239c9470 100644 --- a/sources/Component/Animation/AnimationsComponent.hpp +++ b/sources/Component/Animation/AnimationsComponent.hpp @@ -16,26 +16,35 @@ namespace BBM private: //! @brief To get the animation data RAY::ModelAnimations &_modelAnimation; - //! @brief the frame animation counter - size_t _animFrameCounter; + //! @brief The index of the + int _currentAnimIndex; public: //! @inherit WAL::Component *clone(WAL::Entity &entity) const override; + //! @brief get the current animation index + size_t getCurrentAnimIndex() const; + + //! @brief Set the animation index to use + void setAnimIndex(int animIndex); + //! @brief get animation frame counter - size_t getAnimFrameCounter() const; + size_t getCurrentAnimFrameCounter() const; //! @brief get the current RAY::ModelAnimation getCurrentModelAnim() const; - //! @brief - void setAnimFrameCounter(size_t animFrameCounter); + //! @brief set the anim frame counter + void setCurrentAnimFrameCounter(size_t animFrameCounter); //! @brief Set the internal anim counter to 0 - void resetAnimFrameCounter(); + void resetCurrentAnimFrameCounter(); + + //! @brief Increment the internal anim counter + void incCurrentAnimFrameCounter(); //! @brief ctor entity and the path of the animation file - explicit AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations &modelAnimation); + explicit AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations &modelAnimation, int animIndex); //! @brief copy ctor AnimationsComponent(const AnimationsComponent &) = default; //! @brief dtor diff --git a/sources/System/Animation/AnimationsSystem.cpp b/sources/System/Animation/AnimationsSystem.cpp index 42425acf..f9f7c9ef 100644 --- a/sources/System/Animation/AnimationsSystem.cpp +++ b/sources/System/Animation/AnimationsSystem.cpp @@ -22,6 +22,6 @@ namespace BBM auto &anim = entity.getComponent(); model.member.setAnimation(anim.getCurrentModelAnim()); - anim.setAnimFrameCounter(anim.getAnimFrameCounter() + 1); + anim.setCurrentAnimFrameCounter(anim.getCurrentAnimFrameCounter() + 1); } } \ No newline at end of file From f245fe727b0d6619c639a2b1697dafe5ed8b0b52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Wed, 2 Jun 2021 15:58:37 +0200 Subject: [PATCH 19/60] Animation is working segfault on unload --- lib/Ray/sources/Model/ModelAnimations.cpp | 15 +++++++++++++-- lib/Ray/sources/Model/ModelAnimations.hpp | 12 ++++++++++++ .../Component/Animation/AnimationsComponent.cpp | 12 +++++++----- .../Component/Animation/AnimationsComponent.hpp | 6 +++--- sources/Runner/Runner.cpp | 5 +++++ sources/System/Animation/AnimationsSystem.cpp | 9 ++++++--- sources/System/Animation/AnimationsSystem.hpp | 3 ++- 7 files changed, 48 insertions(+), 14 deletions(-) diff --git a/lib/Ray/sources/Model/ModelAnimations.cpp b/lib/Ray/sources/Model/ModelAnimations.cpp index 7ab39f41..5d5d297f 100644 --- a/lib/Ray/sources/Model/ModelAnimations.cpp +++ b/lib/Ray/sources/Model/ModelAnimations.cpp @@ -10,7 +10,8 @@ RAY::Cache<::ModelAnimation> RAY::ModelAnimations::_animationsCache(LoadModelAnimations, UnloadModelAnimations); RAY::ModelAnimations::ModelAnimations(const std::string &filePath): - _animationsPtr(_animationsCache.fetch(filePath, &this->_animationCount)) + _animationsPtr(_animationsCache.fetch(filePath, &this->_animationCount)), + _filePath(filePath) { ::ModelAnimation *ptr = this->_animationsPtr.get(); @@ -20,7 +21,7 @@ RAY::ModelAnimations::ModelAnimations(const std::string &filePath): RAY::ModelAnimation &RAY::ModelAnimations::operator[](int index) { - return this->_animations[index]; + return this->_animations.at(index); } size_t RAY::ModelAnimations::getAnimationsCount() const @@ -28,3 +29,13 @@ size_t RAY::ModelAnimations::getAnimationsCount() const return this->_animationCount; } +std::string RAY::ModelAnimations::getFilePath() const +{ + return this->_filePath; +} + +const RAY::ModelAnimation &RAY::ModelAnimations::at(int index) const +{ + return this->_animations.at(index); +} + diff --git a/lib/Ray/sources/Model/ModelAnimations.hpp b/lib/Ray/sources/Model/ModelAnimations.hpp index ba46a0e4..9188955a 100644 --- a/lib/Ray/sources/Model/ModelAnimations.hpp +++ b/lib/Ray/sources/Model/ModelAnimations.hpp @@ -27,15 +27,24 @@ namespace RAY { //! @brief Default constructor ~ModelAnimations() = default; + //! @brief Default move ctor + ModelAnimations(ModelAnimations &&) = default; + //! @brief Only single entity can hold these animations pointers ModelAnimations &operator=(const ModelAnimations &) = delete; //! @brief Castin Object to raw model animation pointer ModelAnimation &operator[](int index); + //! @brief Same usage as the operator[] but const + const ModelAnimation &at(int index) const; + //! @return the number of loaded animations size_t getAnimationsCount() const; + //! @brief Get the creation file + std::string getFilePath() const; + private: //! @brief Holds the pointer returned by the loading function std::shared_ptr<::ModelAnimation> _animationsPtr; @@ -46,6 +55,9 @@ namespace RAY { //! @brief the number of loaded animations int _animationCount; + //! @brief The file where the animations were loaded (used to create a copy of this class) + const std::string _filePath; + static Cache<::ModelAnimation> _animationsCache; }; } diff --git a/sources/Component/Animation/AnimationsComponent.cpp b/sources/Component/Animation/AnimationsComponent.cpp index ce29964d..843dcbcc 100644 --- a/sources/Component/Animation/AnimationsComponent.cpp +++ b/sources/Component/Animation/AnimationsComponent.cpp @@ -8,9 +8,9 @@ namespace BBM { - AnimationsComponent::AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations &modelAnimation, int animIndex) + AnimationsComponent::AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations modelAnimation, int animIndex) : WAL::Component(entity), - _modelAnimation(modelAnimation), + _modelAnimation(std::move(modelAnimation)), _currentAnimIndex(animIndex) { this->_modelAnimation[this->_currentAnimIndex].setFrameCounter(0); @@ -18,15 +18,17 @@ namespace BBM WAL::Component *AnimationsComponent::clone(WAL::Entity &entity) const { - return new AnimationsComponent(entity, this->_modelAnimation, this->_currentAnimIndex); + return new AnimationsComponent(entity, + RAY::ModelAnimations(this->_modelAnimation.getFilePath()), + this->_currentAnimIndex); } size_t AnimationsComponent::getCurrentAnimFrameCounter() const { - return this->_modelAnimation[this->_currentAnimIndex].getFrameCounter(); + return this->_modelAnimation.at(this->_currentAnimIndex).getFrameCounter(); } - RAY::ModelAnimation AnimationsComponent::getCurrentModelAnim() const + RAY::ModelAnimation AnimationsComponent::getCurrentModelAnim() { return this->_modelAnimation[this->_currentAnimIndex]; } diff --git a/sources/Component/Animation/AnimationsComponent.hpp b/sources/Component/Animation/AnimationsComponent.hpp index 239c9470..ffaf0599 100644 --- a/sources/Component/Animation/AnimationsComponent.hpp +++ b/sources/Component/Animation/AnimationsComponent.hpp @@ -15,7 +15,7 @@ namespace BBM { private: //! @brief To get the animation data - RAY::ModelAnimations &_modelAnimation; + RAY::ModelAnimations _modelAnimation; //! @brief The index of the int _currentAnimIndex; public: @@ -32,7 +32,7 @@ namespace BBM size_t getCurrentAnimFrameCounter() const; //! @brief get the current - RAY::ModelAnimation getCurrentModelAnim() const; + RAY::ModelAnimation getCurrentModelAnim(); //! @brief set the anim frame counter void setCurrentAnimFrameCounter(size_t animFrameCounter); @@ -44,7 +44,7 @@ namespace BBM void incCurrentAnimFrameCounter(); //! @brief ctor entity and the path of the animation file - explicit AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations &modelAnimation, int animIndex); + explicit AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations modelAnimation, int animIndex); //! @brief copy ctor AnimationsComponent(const AnimationsComponent &) = default; //! @brief dtor diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index d2a1adc9..6ca30a27 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -22,6 +22,9 @@ #include "Component/Renderer/CameraComponent.hpp" #include "Runner.hpp" #include "Models/GameState.hpp" +#include +#include "Component/Animation/AnimationsComponent.hpp" +#include "System/Animation/AnimationsSystem.hpp" #include "Map/Map.hpp" namespace RAY2D = RAY::Drawables::Drawables2D; @@ -53,6 +56,7 @@ namespace BBM RAY::Window &window = RAY::Window::getInstance(600, 400, "Bomberman", FLAG_WINDOW_RESIZABLE); wal.addSystem>(); + wal.addSystem(); wal.addSystem(window) .addSystem>(); @@ -73,6 +77,7 @@ namespace BBM .addComponent>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) .addComponent() .addComponent() + .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 3) .addComponent(); scene->addEntity("camera") .addComponent(8, 20, 7) diff --git a/sources/System/Animation/AnimationsSystem.cpp b/sources/System/Animation/AnimationsSystem.cpp index f9f7c9ef..66d1253e 100644 --- a/sources/System/Animation/AnimationsSystem.cpp +++ b/sources/System/Animation/AnimationsSystem.cpp @@ -2,6 +2,7 @@ // Created by cbihan on 01/06/2021. // +#include #include "AnimationsSystem.hpp" #include "Component/Animation/AnimationsComponent.hpp" #include "Model/Model.hpp" @@ -11,12 +12,14 @@ namespace BBM { AnimationsSystem::AnimationsSystem() - : WAL::System({typeid(AnimationsComponent), - typeid(Drawable3DComponent)}) + : WAL::System({ + typeid(Drawable3DComponent), + typeid(AnimationsComponent) + }) { } - void AnimationsSystem::onFixedUpdate(WAL::Entity &entity) + void AnimationsSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) { auto &model = entity.getComponent>(); auto &anim = entity.getComponent(); diff --git a/sources/System/Animation/AnimationsSystem.hpp b/sources/System/Animation/AnimationsSystem.hpp index eb508b3e..fb08b2ef 100644 --- a/sources/System/Animation/AnimationsSystem.hpp +++ b/sources/System/Animation/AnimationsSystem.hpp @@ -10,8 +10,9 @@ namespace BBM { class AnimationsSystem : public WAL::System { + public: //! @inherit - void onFixedUpdate(WAL::Entity &entity) override; + void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) override; //! @brief A default constructor AnimationsSystem(); From b3eae9ebdfe024548a4683b38ba4ecd869338854 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Wed, 2 Jun 2021 16:11:14 +0200 Subject: [PATCH 20/60] adding the possibility to pause/resume an animation --- .../Component/Animation/AnimationsComponent.cpp | 15 +++++++++++++-- .../Component/Animation/AnimationsComponent.hpp | 10 +++++++++- sources/System/Animation/AnimationsSystem.cpp | 2 ++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/sources/Component/Animation/AnimationsComponent.cpp b/sources/Component/Animation/AnimationsComponent.cpp index 843dcbcc..72be399f 100644 --- a/sources/Component/Animation/AnimationsComponent.cpp +++ b/sources/Component/Animation/AnimationsComponent.cpp @@ -8,10 +8,11 @@ namespace BBM { - AnimationsComponent::AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations modelAnimation, int animIndex) + AnimationsComponent::AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations modelAnimation, int animIndex, bool play) : WAL::Component(entity), _modelAnimation(std::move(modelAnimation)), - _currentAnimIndex(animIndex) + _currentAnimIndex(animIndex), + _animDisabled(play) { this->_modelAnimation[this->_currentAnimIndex].setFrameCounter(0); } @@ -57,4 +58,14 @@ namespace BBM { this->_modelAnimation[this->_currentAnimIndex].incrementFrameCounter(); } + + void AnimationsComponent::setAnimDisabled(bool disable) + { + this->_animDisabled = disable; + } + + bool AnimationsComponent::isAnimDisabled() const + { + return this->_animDisabled; + } } \ No newline at end of file diff --git a/sources/Component/Animation/AnimationsComponent.hpp b/sources/Component/Animation/AnimationsComponent.hpp index ffaf0599..62e2e13b 100644 --- a/sources/Component/Animation/AnimationsComponent.hpp +++ b/sources/Component/Animation/AnimationsComponent.hpp @@ -18,6 +18,8 @@ namespace BBM RAY::ModelAnimations _modelAnimation; //! @brief The index of the int _currentAnimIndex; + //! @brief Bool allowing to play pause an animation + bool _animDisabled; public: //! @inherit WAL::Component *clone(WAL::Entity &entity) const override; @@ -43,8 +45,14 @@ namespace BBM //! @brief Increment the internal anim counter void incCurrentAnimFrameCounter(); + //! @brief Allow to play pause animations + void setAnimDisabled(bool disable); + + //! @brief To know if the animation will be updated or not + bool isAnimDisabled() const; + //! @brief ctor entity and the path of the animation file - explicit AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations modelAnimation, int animIndex); + explicit AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations modelAnimation, int animIndex, bool play = true); //! @brief copy ctor AnimationsComponent(const AnimationsComponent &) = default; //! @brief dtor diff --git a/sources/System/Animation/AnimationsSystem.cpp b/sources/System/Animation/AnimationsSystem.cpp index 66d1253e..7e54adbd 100644 --- a/sources/System/Animation/AnimationsSystem.cpp +++ b/sources/System/Animation/AnimationsSystem.cpp @@ -24,6 +24,8 @@ namespace BBM auto &model = entity.getComponent>(); auto &anim = entity.getComponent(); + if (anim.isDisabled()) + return; model.member.setAnimation(anim.getCurrentModelAnim()); anim.setCurrentAnimFrameCounter(anim.getCurrentAnimFrameCounter() + 1); } From 09d8b1a90b23ef8b9423dd491d3c446cc2b24a29 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Wed, 2 Jun 2021 16:18:26 +0200 Subject: [PATCH 21/60] Reworking drawable components --- CMakeLists.txt | 12 ++--- lib/wal/CMakeLists.txt | 2 +- lib/wal/sources/Entity/Entity.hpp | 13 +++++ lib/wal/sources/Models/TypeHolder.hpp | 13 +++++ .../Renderer/Drawable2DComponent.hpp | 20 ++++---- .../Renderer/Drawable3DComponent.hpp | 16 +++--- sources/Runner/Runner.cpp | 27 +++++----- .../System/Renderer/Render2DScreenSystem.cpp | 19 ------- .../System/Renderer/Render2DScreenSystem.hpp | 33 ------------- .../System/Renderer/RenderScreenSystem.cpp | 32 ------------ sources/System/Renderer/RenderSystem.cpp | 49 +++++++++++++++++++ ...enderScreenSystem.hpp => RenderSystem.hpp} | 14 ++++-- sources/System/Renderer/Renderer2DSystem.hpp | 48 ------------------ sources/System/Renderer/Renderer3DSystem.hpp | 48 ------------------ sources/main.cpp | 40 --------------- 15 files changed, 118 insertions(+), 268 deletions(-) create mode 100644 lib/wal/sources/Models/TypeHolder.hpp delete mode 100644 sources/System/Renderer/Render2DScreenSystem.cpp delete mode 100644 sources/System/Renderer/Render2DScreenSystem.hpp delete mode 100644 sources/System/Renderer/RenderScreenSystem.cpp create mode 100644 sources/System/Renderer/RenderSystem.cpp rename sources/System/Renderer/{RenderScreenSystem.hpp => RenderSystem.hpp} (69%) delete mode 100644 sources/System/Renderer/Renderer2DSystem.hpp delete mode 100644 sources/System/Renderer/Renderer3DSystem.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 84be48cd..01c04fb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,17 +28,13 @@ set(SOURCES sources/System/Movable/MovableSystem.cpp sources/Models/Vector3.hpp sources/Models/Vector2.hpp - sources/Component/Renderer/Drawable3DComponent.hpp sources/Component/Renderer/Drawable2DComponent.hpp - sources/System/Renderer/Renderer3DSystem.hpp - sources/System/Renderer/Renderer2DSystem.hpp - sources/System/Renderer/RenderScreenSystem.hpp - sources/System/Renderer/RenderScreenSystem.cpp + sources/Component/Renderer/Drawable3DComponent.hpp + sources/System/Renderer/RenderSystem.hpp + sources/System/Renderer/RenderSystem.cpp sources/Component/Renderer/CameraComponent.cpp sources/Component/Renderer/CameraComponent.hpp - sources/System/Renderer/Render2DScreenSystem.cpp - sources/System/Renderer/Render2DScreenSystem.hpp -) + ) add_executable(bomberman sources/main.cpp diff --git a/lib/wal/CMakeLists.txt b/lib/wal/CMakeLists.txt index e8cd8655..d216b9f3 100644 --- a/lib/wal/CMakeLists.txt +++ b/lib/wal/CMakeLists.txt @@ -17,6 +17,6 @@ add_library(wal sources/Component/Component.cpp sources/System/System.cpp sources/Models/Callback.hpp -) + sources/Models/TypeHolder.hpp) target_include_directories(wal PUBLIC sources) \ No newline at end of file diff --git a/lib/wal/sources/Entity/Entity.hpp b/lib/wal/sources/Entity/Entity.hpp index 8e157c6e..b64bfb82 100644 --- a/lib/wal/sources/Entity/Entity.hpp +++ b/lib/wal/sources/Entity/Entity.hpp @@ -10,6 +10,7 @@ #include #include "Component/Component.hpp" #include "Exception/WalError.hpp" +#include "Models/TypeHolder.hpp" namespace WAL { @@ -83,6 +84,18 @@ namespace WAL return *this; } + //! @brief Add a component to this entity. The component is constructed in place. + //! @throw DuplicateError is thrown if a component with the same type already exist. + //! @return This entity is returned + template + Entity &addComponent(Types &&...params) + { + if (this->hasComponent()) + throw DuplicateError("A component of the type \"" + std::string(typeid(T).name()) + "\" already exists."); + this->_components.push_back(std::make_unique(*this, TypeHolder(), std::forward(params)...)); + return *this; + } + //! @brief Copy a component to this entity. //! @return This entity is returned. Entity &addComponent(const Component &component); diff --git a/lib/wal/sources/Models/TypeHolder.hpp b/lib/wal/sources/Models/TypeHolder.hpp new file mode 100644 index 00000000..45057ed1 --- /dev/null +++ b/lib/wal/sources/Models/TypeHolder.hpp @@ -0,0 +1,13 @@ +// +// Created by Zoe Roux on 2021-06-02. +// + + +#pragma once + +namespace WAL +{ + //! @brief A class only used to specify template arguments. + template + class TypeHolder {}; +} \ No newline at end of file diff --git a/sources/Component/Renderer/Drawable2DComponent.hpp b/sources/Component/Renderer/Drawable2DComponent.hpp index 63aaaf2b..0fc75f26 100644 --- a/sources/Component/Renderer/Drawable2DComponent.hpp +++ b/sources/Component/Renderer/Drawable2DComponent.hpp @@ -5,35 +5,35 @@ #pragma once #include "Component/Component.hpp" -#include "Drawables/ADrawable2D.hpp" +#include "Drawables/ADrawable3D.hpp" +#include "Model/Model.hpp" namespace BBM { - template class Drawable2DComponent : public WAL::Component { public: //! @brief The type of the component - T member; + std::shared_ptr drawable; - //! ctor - Drawable2DComponent(WAL::Entity &entity, T member) + //! @brief ctor + Drawable2DComponent(WAL::Entity &entity, std::shared_ptr drawable) : WAL::Component(entity), - member(std::move(member)) + drawable(std::move(drawable)) {} //! ctor - template + template explicit Drawable2DComponent(WAL::Entity &entity, Params &&...params) : WAL::Component(entity), - member(std::forward(params)...) + drawable(std::move(T(std::forward(params)...))) {} //! @brief Clone a component for another or the same entity. //! @param entity The entity that owns the ne component. WAL::Component *clone(WAL::Entity &entity) const override { - return new Drawable2DComponent(entity, this->member); + return new Drawable2DComponent(entity, this->drawable); } //! @brief Default copy ctor @@ -42,7 +42,5 @@ namespace BBM ~Drawable2DComponent() override = default; //! @brief Default assignment operator Drawable2DComponent &operator=(const Drawable2DComponent &) = delete; - - }; } \ No newline at end of file diff --git a/sources/Component/Renderer/Drawable3DComponent.hpp b/sources/Component/Renderer/Drawable3DComponent.hpp index e4f85321..5a04da07 100644 --- a/sources/Component/Renderer/Drawable3DComponent.hpp +++ b/sources/Component/Renderer/Drawable3DComponent.hpp @@ -4,37 +4,37 @@ #pragma once +#include #include "Component/Component.hpp" #include "Drawables/ADrawable3D.hpp" #include "Model/Model.hpp" namespace BBM { - template class Drawable3DComponent : public WAL::Component { public: //! @brief The type of the component - T member; + std::shared_ptr drawable; //! @brief ctor - Drawable3DComponent(WAL::Entity &entity, T member) + Drawable3DComponent(WAL::Entity &entity, std::shared_ptr drawable) : WAL::Component(entity), - member(std::move(member)) + drawable(std::move(drawable)) {} //! ctor - template - explicit Drawable3DComponent(WAL::Entity &entity, Params &&...params) + template + explicit Drawable3DComponent(WAL::Entity &entity, WAL::TypeHolder, Params &&...params) : WAL::Component(entity), - member(std::forward(params)...) + drawable(new T(std::forward(params)...)) {} //! @brief Clone a component for another or the same entity. //! @param entity The entity that owns the ne component. WAL::Component *clone(WAL::Entity &entity) const override { - return new Drawable3DComponent(entity, this->member); + return new Drawable3DComponent(entity, this->drawable); } //! @brief Default copy ctor diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 9e256b05..d45a9273 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -4,16 +4,16 @@ #include #include -#include -#include -#include -#include +#include "System/Movable/MovableSystem.hpp" +#include "System/Renderer/RenderSystem.hpp" #include #include #include -#include +#include "Component/Position/PositionComponent.hpp" #include "Models/Vector2.hpp" #include "Component/Renderer/CameraComponent.hpp" +#include "Component/Renderer/Drawable2DComponent.hpp" +#include "Component/Renderer/Drawable3DComponent.hpp" #include "Runner.hpp" #include "Models/GameState.hpp" @@ -36,23 +36,20 @@ namespace BBM { RAY::TraceLog::setLevel(LOG_WARNING); RAY::Window &window = RAY::Window::getInstance(600, 400, "Bomberman", FLAG_WINDOW_RESIZABLE); - - wal.addSystem>(); - - wal.addSystem(window) - .addSystem>(); - wal.addSystem(window); + wal.addSystem(wal, window); } std::shared_ptr loadGameScene() { +// Drawable2DComponent cmp = Drawable2DComponent(Vector2f(), Vector2f(), RED); + auto scene = std::make_shared(); - scene->addEntity("cube") - .addComponent() - .addComponent>(Vector2f(), Vector2f(10, 10), RED); +// scene->addEntity("cube") +// .addComponent() +// .addComponent(Vector2f(), Vector2f(10, 10), RED); scene->addEntity("player") .addComponent() - .addComponent>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")); + .addComponent("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")); scene->addEntity("camera") .addComponent(10, 10, 10) .addComponent(); diff --git a/sources/System/Renderer/Render2DScreenSystem.cpp b/sources/System/Renderer/Render2DScreenSystem.cpp deleted file mode 100644 index 938d107f..00000000 --- a/sources/System/Renderer/Render2DScreenSystem.cpp +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by Zoe Roux on 5/27/21. -// - -#include "Render2DScreenSystem.hpp" - -namespace BBM -{ - Render2DScreenSystem::Render2DScreenSystem(RAY::Window &window) - : WAL::System({}), - _window(window), - _camera(RAY::Vector2(10, 10), RAY::Vector2(), 0) - {} - - void Render2DScreenSystem::onSelfUpdate() - { - this->_window.useCamera(this->_camera); - } -} \ No newline at end of file diff --git a/sources/System/Renderer/Render2DScreenSystem.hpp b/sources/System/Renderer/Render2DScreenSystem.hpp deleted file mode 100644 index 12adaf34..00000000 --- a/sources/System/Renderer/Render2DScreenSystem.hpp +++ /dev/null @@ -1,33 +0,0 @@ -// -// Created by Zoe Roux on 5/27/21. -// - -#pragma once - -#include -#include - -namespace BBM -{ - class Render2DScreenSystem : public WAL::System - { - //! @brief The window to render on - RAY::Window &_window; - - //! @brief The camera used to render. - RAY::Camera::Camera2D _camera; - public: - //! @brief A method called after all entities that this system manage has been updated. - //! @note render on screen here - void onSelfUpdate() override; - - //! @brief ctor - explicit Render2DScreenSystem(RAY::Window &window); - //! @brief Default copy ctor - Render2DScreenSystem(const Render2DScreenSystem &) = default; - //! @brief Default dtor - ~Render2DScreenSystem() override = default; - //! @brief A render screen system can't be assigned. - Render2DScreenSystem &operator=(const Render2DScreenSystem &) = delete; - }; -} \ No newline at end of file diff --git a/sources/System/Renderer/RenderScreenSystem.cpp b/sources/System/Renderer/RenderScreenSystem.cpp deleted file mode 100644 index 5523ee48..00000000 --- a/sources/System/Renderer/RenderScreenSystem.cpp +++ /dev/null @@ -1,32 +0,0 @@ -// -// Created by Zoe Roux on 5/27/21. -// - -#include "RenderScreenSystem.hpp" -#include "Component/Renderer/CameraComponent.hpp" -#include "Component/Position/PositionComponent.hpp" - -namespace BBM -{ - RenderScreenSystem::RenderScreenSystem(RAY::Window &window) - : WAL::System({ - typeid(CameraComponent), - typeid(PositionComponent) - }), - _window(window), - _camera(Vector3f(), Vector3f(), Vector3f(0, 1, 0), 50, CAMERA_PERSPECTIVE) - {} - - void RenderScreenSystem::onSelfUpdate() - { - this->_window.draw(); - this->_window.clear(); - this->_window.useCamera(this->_camera); - } - - void RenderScreenSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) - { - const auto &pos = entity.getComponent(); - _camera.setPosition(pos.position); - } -} \ No newline at end of file diff --git a/sources/System/Renderer/RenderSystem.cpp b/sources/System/Renderer/RenderSystem.cpp new file mode 100644 index 00000000..590a9262 --- /dev/null +++ b/sources/System/Renderer/RenderSystem.cpp @@ -0,0 +1,49 @@ +// +// Created by Zoe Roux on 5/27/21. +// + +#undef INTERNAL +#define INTERNAL public +#include +#include "RenderSystem.hpp" +#include "Component/Renderer/CameraComponent.hpp" +#include "Component/Position/PositionComponent.hpp" + +namespace BBM +{ + RenderSystem::RenderSystem(WAL::Wal &wal, RAY::Window &window) + : WAL::System({ + typeid(CameraComponent), + typeid(PositionComponent) + }), + _wal(wal), + _window(window), + _camera(Vector3f(), Vector3f(), Vector3f(0, 1, 0), 50, CAMERA_PERSPECTIVE) + {} + + void RenderSystem::onSelfUpdate() + { + this->_camera.update(); + BeginDrawing(); + ClearBackground(BLACK); + BeginMode3D(this->_camera); + for (auto &entity : this->_wal.scene->getEntities()) { + if (!entity.hasComponent() + || !entity.hasComponent()) + continue; + auto &drawable = entity.getComponent(); + auto &pos = entity.getComponent(); + +// drawable.drawable->setPosition(static_cast(pos.position)); +// drawable.drawable->drawOn(this->_window); + } + EndMode3D(); + EndDrawing(); + } + + void RenderSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) + { + const auto &pos = entity.getComponent(); + _camera.setPosition(pos.position); + } +} \ No newline at end of file diff --git a/sources/System/Renderer/RenderScreenSystem.hpp b/sources/System/Renderer/RenderSystem.hpp similarity index 69% rename from sources/System/Renderer/RenderScreenSystem.hpp rename to sources/System/Renderer/RenderSystem.hpp index e9b2ea85..5524f614 100644 --- a/sources/System/Renderer/RenderScreenSystem.hpp +++ b/sources/System/Renderer/RenderSystem.hpp @@ -7,11 +7,15 @@ #include "System/System.hpp" #include "Camera/Camera2D.hpp" #include "Window.hpp" +#include "Wal.hpp" namespace BBM { - class RenderScreenSystem : public WAL::System + class RenderSystem : public WAL::System { + //! @brief The ECS to update. + WAL::Wal &_wal; + //! @brief The window to render on RAY::Window &_window; @@ -26,12 +30,12 @@ namespace BBM void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) override; //! @brief ctor - explicit RenderScreenSystem(RAY::Window &window); + RenderSystem(WAL::Wal &wal, RAY::Window &window); //! @brief Default copy ctor - RenderScreenSystem(const RenderScreenSystem &) = default; + RenderSystem(const RenderSystem &) = default; //! @brief Default dtor - ~RenderScreenSystem() override = default; + ~RenderSystem() override = default; //! @brief A render screen system can't be assigned. - RenderScreenSystem &operator=(const RenderScreenSystem &) = delete; + RenderSystem &operator=(const RenderSystem &) = delete; }; } diff --git a/sources/System/Renderer/Renderer2DSystem.hpp b/sources/System/Renderer/Renderer2DSystem.hpp deleted file mode 100644 index a1622c0d..00000000 --- a/sources/System/Renderer/Renderer2DSystem.hpp +++ /dev/null @@ -1,48 +0,0 @@ -// -// Created by cbihan on 24/05/2021. -// - -#pragma once - -#include -#include "System/System.hpp" -#include "Entity/Entity.hpp" -#include "Component/Position/PositionComponent.hpp" -#include "Component/Renderer/Drawable2DComponent.hpp" -#include "Window.hpp" - -namespace BBM -{ - template - class Renderer2DSystem : public WAL::System - { - private: - //! @brief The class to render - RAY::Window &_window; - public: - explicit Renderer2DSystem() - : WAL::System({typeid(PositionComponent), typeid(Drawable2DComponent)}), - _window(RAY::Window::getInstance()) - { - } - - //! @brief Update the corresponding component of the given entity - //! @param entity The entity to update. - //! @param dtime The delta time. - void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) override - { - auto &comp = entity.getComponent>(); - auto &pos = entity.getComponent(); - - comp.member.setPosition({pos.getX(), pos.getY()}); - comp.member.drawOn(this->_window); - } - - //! @brief default copy ctor - Renderer2DSystem(const Renderer2DSystem &) = default; - //! @brief default dtor - ~Renderer2DSystem() override = default; - //! @brief Default assignment operator - Renderer2DSystem &operator=(const Renderer2DSystem &) = delete; - }; -} \ No newline at end of file diff --git a/sources/System/Renderer/Renderer3DSystem.hpp b/sources/System/Renderer/Renderer3DSystem.hpp deleted file mode 100644 index 8dba98f6..00000000 --- a/sources/System/Renderer/Renderer3DSystem.hpp +++ /dev/null @@ -1,48 +0,0 @@ -// -// Created by cbihan on 24/05/2021. -// - -#pragma once - -#include -#include "System/System.hpp" -#include "Entity/Entity.hpp" -#include "Component/Position/PositionComponent.hpp" -#include "Component/Renderer/Drawable3DComponent.hpp" -#include "Window.hpp" - -namespace BBM -{ - template - class Renderer3DSystem : public WAL::System - { - private: - //! @brief The class to render - RAY::Window &_window; - public: - //! @brief ctor - explicit Renderer3DSystem() - : WAL::System({typeid(PositionComponent), typeid(Drawable3DComponent)}), - _window(RAY::Window::getInstance()) - {} - - //! @brief Update the corresponding component of the given entity - //! @param entity The entity to update. - //! @param dtime The delta time. - void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) override - { - auto &comp = entity.getComponent>(); - auto &pos = entity.getComponent(); - - comp.member.setPosition(static_cast(pos.position)); - comp.member.drawOn(this->_window); - } - - //! @brief Default copy ctor - Renderer3DSystem(const Renderer3DSystem &) = default; - //! @brief Default dtor - ~Renderer3DSystem() override = default; - //! @brief Default assignment operator - Renderer3DSystem &operator=(const Renderer3DSystem &) = delete; - }; -} \ No newline at end of file diff --git a/sources/main.cpp b/sources/main.cpp index 43dccd7d..bd13441a 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -9,45 +9,6 @@ #include #include "Runner/Runner.hpp" -// Dependencies of the demo -#include "Camera/Camera3D.hpp" -#include "Controllers/Keyboard.hpp" -#include "Drawables/2D/Text.hpp" -#include "Drawables/Image.hpp" -#include "Drawables/3D/Grid.hpp" -#include "Drawables/Texture.hpp" -#include "Drawables/3D/Circle.hpp" -#include "Drawables/2D/Circle.hpp" -#include "Drawables/3D/Cube.hpp" -#include "Drawables/3D/Sphere.hpp" -#include "Model/Model.hpp" -#include "Model/ModelAnimations.hpp" -#include "System/Renderer/Renderer3DSystem.hpp" -#include "System/Renderer/Renderer2DSystem.hpp" -#include "Component/Renderer/Drawable3DComponent.hpp" -#include "Component/Renderer/Drawable2DComponent.hpp" -#include "System/Renderer/RenderScreenSystem.hpp" -#include "Vector/Vector3.hpp" -#include "Window.hpp" -#include "TraceLog.hpp" -#include "Wal.hpp" - -const std::vectortextures = { - "blue", "cyan", "green", "purple", "red", "yellow" -}; - -std::string get_full_path(const std::string &color) -{ - std::string path = "assets/player/"; - - path += color; - path += ".png"; - return path; -} - - - - void usage(const std::string &bin) { std::cout << "Bomberman." << std::endl @@ -62,6 +23,5 @@ int main(int argc, char **argv) usage(argv[0]); return 1; } -// return demo(); return BBM::run(); } From 88e086b2728d4eccff1af5e232cfdcd05e080b58 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Wed, 2 Jun 2021 16:22:26 +0200 Subject: [PATCH 22/60] Finishing to rework the drawable system --- .../Renderer/Drawable2DComponent.hpp | 5 ++-- sources/Runner/Runner.cpp | 8 +++--- sources/System/Renderer/RenderSystem.cpp | 25 ++++++++++++++++--- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/sources/Component/Renderer/Drawable2DComponent.hpp b/sources/Component/Renderer/Drawable2DComponent.hpp index 0fc75f26..4bab9434 100644 --- a/sources/Component/Renderer/Drawable2DComponent.hpp +++ b/sources/Component/Renderer/Drawable2DComponent.hpp @@ -4,6 +4,7 @@ #pragma once +#include #include "Component/Component.hpp" #include "Drawables/ADrawable3D.hpp" #include "Model/Model.hpp" @@ -24,9 +25,9 @@ namespace BBM //! ctor template - explicit Drawable2DComponent(WAL::Entity &entity, Params &&...params) + explicit Drawable2DComponent(WAL::Entity &entity, WAL::TypeHolder, Params &&...params) : WAL::Component(entity), - drawable(std::move(T(std::forward(params)...))) + drawable(new T(std::forward(params)...)) {} //! @brief Clone a component for another or the same entity. diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index d45a9273..1341bc45 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -41,12 +41,10 @@ namespace BBM std::shared_ptr loadGameScene() { -// Drawable2DComponent cmp = Drawable2DComponent(Vector2f(), Vector2f(), RED); - auto scene = std::make_shared(); -// scene->addEntity("cube") -// .addComponent() -// .addComponent(Vector2f(), Vector2f(10, 10), RED); + scene->addEntity("cube") + .addComponent() + .addComponent(Vector2f(), Vector2f(10, 10), RED); scene->addEntity("player") .addComponent() .addComponent("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")); diff --git a/sources/System/Renderer/RenderSystem.cpp b/sources/System/Renderer/RenderSystem.cpp index 590a9262..99a94f0e 100644 --- a/sources/System/Renderer/RenderSystem.cpp +++ b/sources/System/Renderer/RenderSystem.cpp @@ -4,10 +4,14 @@ #undef INTERNAL #define INTERNAL public -#include +#include +#include "Models/Vector2.hpp" #include "RenderSystem.hpp" #include "Component/Renderer/CameraComponent.hpp" #include "Component/Position/PositionComponent.hpp" +#include "Component/Renderer/Drawable2DComponent.hpp" +#include "Drawables/ADrawable2D.hpp" +#include "Drawables/ADrawable3D.hpp" namespace BBM { @@ -26,7 +30,21 @@ namespace BBM this->_camera.update(); BeginDrawing(); ClearBackground(BLACK); + BeginMode3D(this->_camera); + for (auto &entity : this->_wal.scene->getEntities()) { + if (!entity.hasComponent() + || !entity.hasComponent()) + continue; + auto &drawable = entity.getComponent(); + auto &pos = entity.getComponent(); + + drawable.drawable->setPosition(pos.position); + drawable.drawable->drawOn(this->_window); + } + EndMode3D(); + + // TODO sort entities based on the Z axis for (auto &entity : this->_wal.scene->getEntities()) { if (!entity.hasComponent() || !entity.hasComponent()) @@ -34,10 +52,9 @@ namespace BBM auto &drawable = entity.getComponent(); auto &pos = entity.getComponent(); -// drawable.drawable->setPosition(static_cast(pos.position)); -// drawable.drawable->drawOn(this->_window); + drawable.drawable->setPosition(Vector2f(pos.position.x, pos.position.y)); + drawable.drawable->drawOn(this->_window); } - EndMode3D(); EndDrawing(); } From 37fb527d5bcf5cecf718f250ff17d3e90d15c32c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Wed, 2 Jun 2021 16:30:16 +0200 Subject: [PATCH 23/60] little fix --- sources/Component/Animation/AnimationsComponent.hpp | 1 - sources/System/Animation/AnimationsSystem.cpp | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/sources/Component/Animation/AnimationsComponent.hpp b/sources/Component/Animation/AnimationsComponent.hpp index 62e2e13b..463f241f 100644 --- a/sources/Component/Animation/AnimationsComponent.hpp +++ b/sources/Component/Animation/AnimationsComponent.hpp @@ -60,5 +60,4 @@ namespace BBM //! @brief assignment operator AnimationsComponent &operator=(const AnimationsComponent &) = delete; }; - } diff --git a/sources/System/Animation/AnimationsSystem.cpp b/sources/System/Animation/AnimationsSystem.cpp index 7e54adbd..fb9f79e0 100644 --- a/sources/System/Animation/AnimationsSystem.cpp +++ b/sources/System/Animation/AnimationsSystem.cpp @@ -27,6 +27,6 @@ namespace BBM if (anim.isDisabled()) return; model.member.setAnimation(anim.getCurrentModelAnim()); - anim.setCurrentAnimFrameCounter(anim.getCurrentAnimFrameCounter() + 1); + anim.incCurrentAnimFrameCounter(); } } \ No newline at end of file From f3d9effb838255e213d986daed69a87c94eb5a61 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Wed, 2 Jun 2021 16:47:05 +0200 Subject: [PATCH 24/60] fix double free for model animation unloading --- lib/Ray/sources/Utils/Cache.hpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/Ray/sources/Utils/Cache.hpp b/lib/Ray/sources/Utils/Cache.hpp index f30fa3ff..a1eef87a 100644 --- a/lib/Ray/sources/Utils/Cache.hpp +++ b/lib/Ray/sources/Utils/Cache.hpp @@ -60,11 +60,13 @@ namespace RAY { {}; std::shared_ptr<::ModelAnimation> fetch(const std::string &path, int *counter) { + ::ModelAnimation *animations = this->_dataLoader(path.c_str(), counter); + unsigned int animCount = *counter; + if (this->_cache.find(path) == this->_cache.end()) this->_cache.emplace(path, std::shared_ptr<::ModelAnimation>( - this->_dataLoader(path.c_str(), counter), [this, counter](::ModelAnimation *p) { - this->_dataUnloader(p, *counter); - delete p; + animations, [this, animCount](::ModelAnimation *p) { + this->_dataUnloader(p, animCount); })); return _cache[path]; }; From 34cbe24c197e71e25fe1cd0e4eeaec689f27e026 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Wed, 2 Jun 2021 17:54:16 +0200 Subject: [PATCH 25/60] remove unwanted defines + upade encapsulation for window drawing function --- lib/Ray/sources/Window.cpp | 8 ++- lib/Ray/sources/Window.hpp | 9 ++-- .../System/Renderer/Render2DScreenSystem.cpp | 21 -------- .../System/Renderer/Render2DScreenSystem.hpp | 30 ------------ .../System/Renderer/RenderScreenSystem.cpp | 40 --------------- sources/System/Renderer/RenderSystem.cpp | 12 ++--- sources/System/Renderer/Renderer2DSystem.hpp | 49 ------------------- sources/System/Renderer/Renderer3DSystem.hpp | 48 ------------------ 8 files changed, 17 insertions(+), 200 deletions(-) delete mode 100644 sources/System/Renderer/Render2DScreenSystem.cpp delete mode 100644 sources/System/Renderer/Render2DScreenSystem.hpp delete mode 100644 sources/System/Renderer/RenderScreenSystem.cpp delete mode 100644 sources/System/Renderer/Renderer2DSystem.hpp delete mode 100644 sources/System/Renderer/Renderer3DSystem.hpp diff --git a/lib/Ray/sources/Window.cpp b/lib/Ray/sources/Window.cpp index 9deba6e0..c89c6363 100644 --- a/lib/Ray/sources/Window.cpp +++ b/lib/Ray/sources/Window.cpp @@ -107,10 +107,14 @@ void RAY::Window::clear(const RAY::Color &color) ClearBackground(color); } -void RAY::Window::draw() +void RAY::Window::beginDrawing() +{ + BeginDrawing(); +} + +void RAY::Window::endDrawing() { EndDrawing(); - BeginDrawing(); } void RAY::Window::useCamera(RAY::Camera::Camera2D &camera) diff --git a/lib/Ray/sources/Window.hpp b/lib/Ray/sources/Window.hpp index c16eae4b..a02c78f9 100644 --- a/lib/Ray/sources/Window.hpp +++ b/lib/Ray/sources/Window.hpp @@ -7,7 +7,7 @@ #ifndef WINDOW_HPP_ #define WINDOW_HPP_ -#define INTERNAL public + #include #include #include @@ -102,8 +102,10 @@ namespace RAY { NONE, }; - //! @brief Draw the content of the buffer on the screen. - void draw(); + //! @brief Setup canvas (framebuffer) to start drawing + void beginDrawing(); + //! @brief End canvas drawing and swap buffers (double buffering) + void endDrawing(); //! @brief Initialize 2D mode with custom camera (2D) void useCamera(Camera::Camera2D &camera); @@ -131,6 +133,7 @@ namespace RAY { //! @brief Draw a 3d mesh with material and transform void draw(const Mesh &mesh, const Material &material, const Matrix &transform); + //! @return true if the window's context has been correctly initialized bool isReady() const; diff --git a/sources/System/Renderer/Render2DScreenSystem.cpp b/sources/System/Renderer/Render2DScreenSystem.cpp deleted file mode 100644 index b21c35da..00000000 --- a/sources/System/Renderer/Render2DScreenSystem.cpp +++ /dev/null @@ -1,21 +0,0 @@ -// -// Created by Zoe Roux on 5/27/21. -// - -#include "Render2DScreenSystem.hpp" - -namespace BBM -{ - Render2DScreenSystem::Render2DScreenSystem(RAY::Window &window) - : WAL::System({}), - _window(window) - {} - - void Render2DScreenSystem::onSelfUpdate() - { - EndMode3D(); - printf("EndMode3D\n"); - DrawText("Try selecting the box with mouse!", 10, 10, 20, WHITE); - //this->_window.unuseCamera(); - } -} \ No newline at end of file diff --git a/sources/System/Renderer/Render2DScreenSystem.hpp b/sources/System/Renderer/Render2DScreenSystem.hpp deleted file mode 100644 index 4fa00ba1..00000000 --- a/sources/System/Renderer/Render2DScreenSystem.hpp +++ /dev/null @@ -1,30 +0,0 @@ -// -// Created by Zoe Roux on 5/27/21. -// - -#pragma once - -#include -#include - -namespace BBM -{ - class Render2DScreenSystem : public WAL::System - { - //! @brief The window to render on - RAY::Window &_window; - public: - //! @brief A method called after all entities that this system manage has been updated. - //! @note render on screen here - void onSelfUpdate() override; - - //! @brief ctor - explicit Render2DScreenSystem(RAY::Window &window); - //! @brief Default copy ctor - Render2DScreenSystem(const Render2DScreenSystem &) = default; - //! @brief Default dtor - ~Render2DScreenSystem() override = default; - //! @brief A render screen system can't be assigned. - Render2DScreenSystem &operator=(const Render2DScreenSystem &) = delete; - }; -} \ No newline at end of file diff --git a/sources/System/Renderer/RenderScreenSystem.cpp b/sources/System/Renderer/RenderScreenSystem.cpp deleted file mode 100644 index 1550bd65..00000000 --- a/sources/System/Renderer/RenderScreenSystem.cpp +++ /dev/null @@ -1,40 +0,0 @@ -// -// Created by Zoe Roux on 5/27/21. -// - -#include "RenderScreenSystem.hpp" -#include "Component/Renderer/CameraComponent.hpp" -#include "Component/Position/PositionComponent.hpp" - -namespace BBM -{ - RenderScreenSystem::RenderScreenSystem(RAY::Window &window) - : WAL::System({ - typeid(CameraComponent), - typeid(PositionComponent) - }), - _window(window), - _camera(Vector3f(), Vector3f(), Vector3f(0, 1, 0), 50, CAMERA_PERSPECTIVE) - { - this->_camera.setMode(CAMERA_FREE); - } - - void RenderScreenSystem::onSelfUpdate() - { - //this->_window.draw(); - //EndMode2D(); - EndDrawing(); - this->_camera.update(); - BeginDrawing(); - this->_window.clear(); - BeginMode3D(this->_camera); - printf("BeginMode3D\n"); - } - - void RenderScreenSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) - { - const auto &pos = entity.getComponent(); - _camera.setPosition(pos.position); - //this->_camera.update(); - } -} \ No newline at end of file diff --git a/sources/System/Renderer/RenderSystem.cpp b/sources/System/Renderer/RenderSystem.cpp index 99a94f0e..5073ee30 100644 --- a/sources/System/Renderer/RenderSystem.cpp +++ b/sources/System/Renderer/RenderSystem.cpp @@ -2,8 +2,6 @@ // Created by Zoe Roux on 5/27/21. // -#undef INTERNAL -#define INTERNAL public #include #include "Models/Vector2.hpp" #include "RenderSystem.hpp" @@ -28,10 +26,10 @@ namespace BBM void RenderSystem::onSelfUpdate() { this->_camera.update(); - BeginDrawing(); - ClearBackground(BLACK); + this->_window.beginDrawing(); + this->_window.clear(); - BeginMode3D(this->_camera); + this->_window.useCamera(this->_camera); for (auto &entity : this->_wal.scene->getEntities()) { if (!entity.hasComponent() || !entity.hasComponent()) @@ -42,7 +40,7 @@ namespace BBM drawable.drawable->setPosition(pos.position); drawable.drawable->drawOn(this->_window); } - EndMode3D(); + this->_window.unuseCamera(); // TODO sort entities based on the Z axis for (auto &entity : this->_wal.scene->getEntities()) { @@ -55,7 +53,7 @@ namespace BBM drawable.drawable->setPosition(Vector2f(pos.position.x, pos.position.y)); drawable.drawable->drawOn(this->_window); } - EndDrawing(); + this->_window.endDrawing(); } void RenderSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) diff --git a/sources/System/Renderer/Renderer2DSystem.hpp b/sources/System/Renderer/Renderer2DSystem.hpp deleted file mode 100644 index 3fd55346..00000000 --- a/sources/System/Renderer/Renderer2DSystem.hpp +++ /dev/null @@ -1,49 +0,0 @@ -// -// Created by cbihan on 24/05/2021. -// - -#pragma once - -#include -#include "System/System.hpp" -#include "Entity/Entity.hpp" -#include "Component/Position/PositionComponent.hpp" -#include "Component/Renderer/Drawable2DComponent.hpp" -#include "Window.hpp" - -namespace BBM -{ - template - class Renderer2DSystem : public WAL::System - { - private: - //! @brief The class to render - RAY::Window &_window; - public: - explicit Renderer2DSystem() - : WAL::System({typeid(PositionComponent), typeid(Drawable2DComponent)}), - _window(RAY::Window::getInstance()) - { - } - - //! @brief Update the corresponding component of the given entity - //! @param entity The entity to update. - //! @param dtime The delta time. - void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) override - { - auto &comp = entity.getComponent>(); - auto &pos = entity.getComponent(); - - comp.member.setPosition({pos.getX(), pos.getY()}); - this->_window.draw(comp.member); - printf("Drawing smth\n"); - } - - //! @brief default copy ctor - Renderer2DSystem(const Renderer2DSystem &) = default; - //! @brief default dtor - ~Renderer2DSystem() override = default; - //! @brief Default assignment operator - Renderer2DSystem &operator=(const Renderer2DSystem &) = delete; - }; -} \ No newline at end of file diff --git a/sources/System/Renderer/Renderer3DSystem.hpp b/sources/System/Renderer/Renderer3DSystem.hpp deleted file mode 100644 index 0fa3a9bb..00000000 --- a/sources/System/Renderer/Renderer3DSystem.hpp +++ /dev/null @@ -1,48 +0,0 @@ -// -// Created by cbihan on 24/05/2021. -// - -#pragma once - -#include -#include "System/System.hpp" -#include "Entity/Entity.hpp" -#include "Component/Position/PositionComponent.hpp" -#include "Component/Renderer/Drawable3DComponent.hpp" -#include "Window.hpp" - -namespace BBM -{ - template - class Renderer3DSystem : public WAL::System - { - private: - //! @brief The class to render - RAY::Window &_window; - public: - //! @brief ctor - explicit Renderer3DSystem() - : WAL::System({typeid(PositionComponent), typeid(Drawable3DComponent)}), - _window(RAY::Window::getInstance()) - {} - - //! @brief Update the corresponding component of the given entity - //! @param entity The entity to update. - //! @param dtime The delta time. - void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) override - { - auto &comp = entity.getComponent>(); - auto &pos = entity.getComponent(); - - comp.member.setPosition(static_cast(pos.position)); - this->_window.draw(comp.member); - } - - //! @brief Default copy ctor - Renderer3DSystem(const Renderer3DSystem &) = default; - //! @brief Default dtor - ~Renderer3DSystem() override = default; - //! @brief Default assignment operator - Renderer3DSystem &operator=(const Renderer3DSystem &) = delete; - }; -} \ No newline at end of file From 3790b2a8cdd03d99f41f55a408e09fe174c4a2a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Wed, 2 Jun 2021 18:14:44 +0200 Subject: [PATCH 26/60] first pr fixes from reviews --- CMakeLists.txt | 4 +++- lib/Ray/sources/Model/ModelAnimations.cpp | 2 +- lib/Ray/sources/Utils/Cache.hpp | 14 ++++++++------ .../Component/Animation/AnimationsComponent.cpp | 4 ++-- sources/Runner/Runner.cpp | 10 +++++----- sources/System/Animation/AnimationsSystem.cpp | 2 +- 6 files changed, 20 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 99872204..589b5630 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -80,7 +80,9 @@ add_executable(unit_tests EXCLUDE_FROM_ALL tests/MainTest.cpp tests/EngineTests.cpp tests/CallbackTest.cpp - tests/MoveTests.cpp) + tests/MoveTests.cpp + tests/CollisionTest.cpp + ) target_include_directories(unit_tests PUBLIC sources) target_link_libraries(unit_tests PUBLIC wal ray) diff --git a/lib/Ray/sources/Model/ModelAnimations.cpp b/lib/Ray/sources/Model/ModelAnimations.cpp index 5d5d297f..b70abf56 100644 --- a/lib/Ray/sources/Model/ModelAnimations.cpp +++ b/lib/Ray/sources/Model/ModelAnimations.cpp @@ -16,7 +16,7 @@ RAY::ModelAnimations::ModelAnimations(const std::string &filePath): ::ModelAnimation *ptr = this->_animationsPtr.get(); for (int i = 0; i < this->_animationCount; i++) - this->_animations.emplace_back(RAY::ModelAnimation(ptr[i])); + this->_animations.emplace_back(ptr[i]); } RAY::ModelAnimation &RAY::ModelAnimations::operator[](int index) diff --git a/lib/Ray/sources/Utils/Cache.hpp b/lib/Ray/sources/Utils/Cache.hpp index a1eef87a..7f041853 100644 --- a/lib/Ray/sources/Utils/Cache.hpp +++ b/lib/Ray/sources/Utils/Cache.hpp @@ -60,15 +60,17 @@ namespace RAY { {}; std::shared_ptr<::ModelAnimation> fetch(const std::string &path, int *counter) { + if (this->_cache.find(path) != this->_cache.end()) + return this->_cache[path]; + ::ModelAnimation *animations = this->_dataLoader(path.c_str(), counter); unsigned int animCount = *counter; - if (this->_cache.find(path) == this->_cache.end()) - this->_cache.emplace(path, std::shared_ptr<::ModelAnimation>( - animations, [this, animCount](::ModelAnimation *p) { - this->_dataUnloader(p, animCount); - })); - return _cache[path]; + this->_cache.emplace(path, std::shared_ptr<::ModelAnimation>( + animations, [this, animCount](::ModelAnimation *p) { + this->_dataUnloader(p, animCount); + })); + return this->_cache[path]; }; private: //! @brief function to call to load data diff --git a/sources/Component/Animation/AnimationsComponent.cpp b/sources/Component/Animation/AnimationsComponent.cpp index 72be399f..4461cc8e 100644 --- a/sources/Component/Animation/AnimationsComponent.cpp +++ b/sources/Component/Animation/AnimationsComponent.cpp @@ -20,8 +20,8 @@ namespace BBM WAL::Component *AnimationsComponent::clone(WAL::Entity &entity) const { return new AnimationsComponent(entity, - RAY::ModelAnimations(this->_modelAnimation.getFilePath()), - this->_currentAnimIndex); + RAY::ModelAnimations(this->_modelAnimation.getFilePath()), + this->_currentAnimIndex); } size_t AnimationsComponent::getCurrentAnimFrameCounter() const diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 8b9f70e7..00218852 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -60,8 +60,8 @@ namespace BBM RAY::Window &window = RAY::Window::getInstance(600, 400, "Bomberman", FLAG_WINDOW_RESIZABLE); wal.addSystem>(); - wal.addSystem(); wal.addSystem>(); + wal.addSystem(); wal.addSystem(window) .addSystem>(); @@ -76,12 +76,12 @@ namespace BBM .addComponent>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) .addComponent() .addComponent() - .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 3) + .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 1) .addComponent(2) .addComponent(); scene->addEntity("cube") - .addComponent(-5, 0, -5) - .addComponent>(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED) + .addComponent(5, 0, 5) + .addComponent>(Vector3f(5, 0, 5), Vector3f(3, 3, 3), RED) .addComponent() .addComponent() .addComponent([](WAL::Entity &, const WAL::Entity &){}, @@ -96,7 +96,7 @@ namespace BBM .addComponent(8, 20, 7) .addComponent(Vector3f(8, 0, 8)); std::srand(std::time(NULL)); - MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); + //MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); return scene; } diff --git a/sources/System/Animation/AnimationsSystem.cpp b/sources/System/Animation/AnimationsSystem.cpp index fb9f79e0..3400cf10 100644 --- a/sources/System/Animation/AnimationsSystem.cpp +++ b/sources/System/Animation/AnimationsSystem.cpp @@ -13,7 +13,7 @@ namespace BBM AnimationsSystem::AnimationsSystem() : WAL::System({ - typeid(Drawable3DComponent), + typeid(Drawable3DComponent), typeid(AnimationsComponent) }) { From 883022d255c2673323273f654a6d38b9f4d7f16c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Wed, 2 Jun 2021 18:17:32 +0200 Subject: [PATCH 27/60] reenable map drawing --- sources/Runner/Runner.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 00218852..8b1f36fd 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -80,8 +80,8 @@ namespace BBM .addComponent(2) .addComponent(); scene->addEntity("cube") - .addComponent(5, 0, 5) - .addComponent>(Vector3f(5, 0, 5), Vector3f(3, 3, 3), RED) + .addComponent(-5, 0, -5) + .addComponent>(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED) .addComponent() .addComponent() .addComponent([](WAL::Entity &, const WAL::Entity &){}, @@ -96,7 +96,7 @@ namespace BBM .addComponent(8, 20, 7) .addComponent(Vector3f(8, 0, 8)); std::srand(std::time(NULL)); - //MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); + MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); return scene; } From 4ea98204a22a3524d870407b8ff690fddd6b6241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Wed, 2 Jun 2021 18:32:58 +0200 Subject: [PATCH 28/60] indents fixes --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 589b5630..fd2543bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,8 +58,8 @@ set(SOURCES sources/System/Renderer/Render2DScreenSystem.hpp sources/Component/Animation/AnimationsComponent.cpp sources/Component/Animation/AnimationsComponent.hpp - sources/System/Animation/AnimationsSystem.cpp - sources/System/Animation/AnimationsSystem.hpp + sources/System/Animation/AnimationsSystem.cpp + sources/System/Animation/AnimationsSystem.hpp sources/Component/Collision/CollisionComponent.cpp sources/Component/Collision/CollisionComponent.hpp sources/System/Collision/CollisionSystem.hpp @@ -81,7 +81,7 @@ add_executable(unit_tests EXCLUDE_FROM_ALL tests/EngineTests.cpp tests/CallbackTest.cpp tests/MoveTests.cpp - tests/CollisionTest.cpp + tests/CollisionTest.cpp ) target_include_directories(unit_tests PUBLIC sources) target_link_libraries(unit_tests PUBLIC wal ray) From 066e77c47b9469206b265bf0d29be68881a96051 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Wed, 2 Jun 2021 18:42:21 +0200 Subject: [PATCH 29/60] hope you're HaPpY --- lib/Ray/sources/Model/ModelAnimations.cpp | 9 +++++++++ lib/Ray/sources/Model/ModelAnimations.hpp | 8 +++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/Ray/sources/Model/ModelAnimations.cpp b/lib/Ray/sources/Model/ModelAnimations.cpp index b70abf56..1b5f9cc4 100644 --- a/lib/Ray/sources/Model/ModelAnimations.cpp +++ b/lib/Ray/sources/Model/ModelAnimations.cpp @@ -39,3 +39,12 @@ const RAY::ModelAnimation &RAY::ModelAnimations::at(int index) const return this->_animations.at(index); } +const RAY::ModelAnimation &RAY::ModelAnimations::operator[](int index) const +{ + return this->_animations[index]; +} + +RAY::ModelAnimation &RAY::ModelAnimations::at(int index) +{ + return this->_animations.at(index); +} diff --git a/lib/Ray/sources/Model/ModelAnimations.hpp b/lib/Ray/sources/Model/ModelAnimations.hpp index 9188955a..83978b82 100644 --- a/lib/Ray/sources/Model/ModelAnimations.hpp +++ b/lib/Ray/sources/Model/ModelAnimations.hpp @@ -36,9 +36,15 @@ namespace RAY { //! @brief Castin Object to raw model animation pointer ModelAnimation &operator[](int index); - //! @brief Same usage as the operator[] but const + //! @brief std [] const + const ModelAnimation &operator[](int index) const; + + //! @brief std at const const ModelAnimation &at(int index) const; + //! @brief std at + ModelAnimation &at(int index); + //! @return the number of loaded animations size_t getAnimationsCount() const; From d29b0dc4edcfafbc62cecaa56e7e3688585cec9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Wed, 2 Jun 2021 18:44:31 +0200 Subject: [PATCH 30/60] pain --- lib/Ray/sources/Model/ModelAnimations.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Ray/sources/Model/ModelAnimations.cpp b/lib/Ray/sources/Model/ModelAnimations.cpp index 1b5f9cc4..6d41832d 100644 --- a/lib/Ray/sources/Model/ModelAnimations.cpp +++ b/lib/Ray/sources/Model/ModelAnimations.cpp @@ -21,7 +21,7 @@ RAY::ModelAnimations::ModelAnimations(const std::string &filePath): RAY::ModelAnimation &RAY::ModelAnimations::operator[](int index) { - return this->_animations.at(index); + return this->_animations[index]; } size_t RAY::ModelAnimations::getAnimationsCount() const From a3804c170f228275c311c75b4d0f1a370feb1bd5 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Wed, 2 Jun 2021 21:29:14 +0200 Subject: [PATCH 31/60] Optimizing component handling --- lib/wal/sources/Entity/Entity.cpp | 19 +++++++------------ lib/wal/sources/Entity/Entity.hpp | 23 ++++++++++------------- sources/Map/Map.cpp | 19 +++++++++---------- 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/lib/wal/sources/Entity/Entity.cpp b/lib/wal/sources/Entity/Entity.cpp index 56627a16..1dd1a1b3 100644 --- a/lib/wal/sources/Entity/Entity.cpp +++ b/lib/wal/sources/Entity/Entity.cpp @@ -21,7 +21,7 @@ namespace WAL _disabled(other._disabled) { for (const auto &cmp : other._components) - this->addComponent(*cmp); + this->addComponent(*cmp.second); } unsigned Entity::getUid() const @@ -46,25 +46,20 @@ namespace WAL Entity &Entity::addComponent(const Component &component) { - if (this->hasComponent(typeid(component))) - throw DuplicateError("A component of the type \"" + std::string(typeid(component).name()) + "\" already exists."); - this->_components.emplace_back(component.clone(*this)); + const std::type_index &type = typeid(component); + if (this->hasComponent(type)) + throw DuplicateError("A component of the type \"" + std::string(type.name()) + "\" already exists."); + this->_components.emplace(type, component.clone(*this)); return *this; } bool Entity::hasComponent(const std::type_info &type) const { - auto existing = std::find_if(this->_components.begin(), this->_components.end(), [&type] (const auto &cmp) { - return typeid(*cmp) == type; - }); - return existing != this->_components.end(); + return this->hasComponent(static_cast(type)); } bool Entity::hasComponent(const std::type_index &type) const { - auto existing = std::find_if(this->_components.begin(), this->_components.end(), [&type] (const auto &cmp) { - return std::type_index(typeid(*cmp)) == type; - }); - return existing != this->_components.end(); + return this->_components.contains(type); } } // namespace WAL \ No newline at end of file diff --git a/lib/wal/sources/Entity/Entity.hpp b/lib/wal/sources/Entity/Entity.hpp index 8e157c6e..e9fa672e 100644 --- a/lib/wal/sources/Entity/Entity.hpp +++ b/lib/wal/sources/Entity/Entity.hpp @@ -5,7 +5,7 @@ #pragma once #include -#include +#include #include #include #include "Component/Component.hpp" @@ -24,7 +24,7 @@ namespace WAL //! @brief Is this entity enabled? bool _disabled = false; //! @brief The list of the components of this entity - std::vector> _components = {}; + std::unordered_map> _components = {}; //! @brief This ID will be the one of the next entity created. static unsigned nextID; @@ -45,13 +45,11 @@ namespace WAL template T &getComponent() { - const std::type_info &type = typeid(T); - auto existing = std::find_if(this->_components.begin(), this->_components.end(), [&type] (const auto &cmp) { - return typeid(*cmp) == type; - }); + const std::type_index &type = typeid(T); + auto existing = this->_components.find(type); if (existing == this->_components.end()) throw NotFoundError("No component could be found with the type \"" + std::string(type.name()) + "\"."); - return *static_cast(existing->get()); + return *static_cast(existing->second.get()); } //! @brief Check if this entity has a component. @@ -77,9 +75,10 @@ namespace WAL template Entity &addComponent(Types &&...params) { - if (this->hasComponent()) - throw DuplicateError("A component of the type \"" + std::string(typeid(T).name()) + "\" already exists."); - this->_components.push_back(std::make_unique(*this, std::forward(params)...)); + const std::type_index &type = typeid(T); + if (this->hasComponent(type)) + throw DuplicateError("A component of the type \"" + std::string(type.name()) + "\" already exists."); + this->_components[type] = std::make_unique(*this, std::forward(params)...); return *this; } @@ -94,9 +93,7 @@ namespace WAL Entity &removeComponent() { const std::type_info &type = typeid(T); - auto existing = std::find_if(this->_components.begin(), this->_components.end(), [&type] (const auto &cmp) { - return typeid(*cmp) == type; - }); + auto existing = this->_components.find(type); if (existing == this->_components.end()) throw NotFoundError("No component could be found with the type \"" + std::string(type.name()) + "\"."); this->_components.erase(existing); diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index c90609f4..338a1f42 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -3,6 +3,7 @@ // Edited by Benjamin Henry on 5/26/21. // +#include #include "Map.hpp" namespace RAY3D = RAY::Drawables::Drawables3D; @@ -18,8 +19,8 @@ namespace BBM for (int j = 0; j < height + 1; j++) { if (!(i % 2) && !(j % 2)) { scene->addEntity("Unbreakable Wall") - .addComponent(Vector3f(i, 0, j)) - //.addComponent(1) + .addComponent(i, 0, j) + .addComponent(1) .addComponent>(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj)); } } @@ -33,19 +34,19 @@ namespace BBM scene->addEntity("Bottom Wall") .addComponent(Vector3f((width + 1) / 2, 0, -1)) - //.addComponent(1) + .addComponent(1) .addComponent>(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Upper Wall") .addComponent(Vector3f((width + 1) / 2, 0, height + 1)) - //.addComponent(1) + .addComponent(1) .addComponent>(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Left Wall") .addComponent(Vector3f(width + 1, 0, (height + 1) / 2)) - //.addComponent(1) + .addComponent(1) .addComponent>(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 3)); scene->addEntity("Right Wall") .addComponent(Vector3f(-1, 0, (height + 1) / 2)) - //.addComponent(1) + .addComponent(1) .addComponent>(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 3)); } @@ -53,7 +54,6 @@ namespace BBM { scene->addEntity("Floor") .addComponent(Vector3f(width / 2, -1, height / 2)) - //.addComponent(1) .addComponent>("assets/wall/floor.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/floor.png"), RAY::Vector3(width + 2, 0, height + 2)); } @@ -81,7 +81,7 @@ namespace BBM scene->addEntity("Breakable Block") .addComponent(coords) .addComponent(1) - //.addComponent(1) + .addComponent(1) .addComponent>("assets/wall/breakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/breakable_wall.png")); } @@ -89,7 +89,6 @@ namespace BBM { scene->addEntity("Floor") .addComponent(Vector3f(coords)) - //.addComponent(1) .addComponent>("assets/wall/floor.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/floor.png")); } @@ -97,7 +96,7 @@ namespace BBM { scene->addEntity("Unbreakable Block") .addComponent(coords) - //.addComponent(1) + .addComponent(1) .addComponent>("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png")); } From ae2e41983275f7008a70d7d38861b56117651f7f Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Wed, 2 Jun 2021 23:30:39 +0200 Subject: [PATCH 32/60] Creating a templated view class --- lib/wal/CMakeLists.txt | 2 +- lib/wal/sources/Scene/Scene.hpp | 3 +++ lib/wal/sources/View/View.hpp | 35 +++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 lib/wal/sources/View/View.hpp diff --git a/lib/wal/CMakeLists.txt b/lib/wal/CMakeLists.txt index e8cd8655..ba2a60a0 100644 --- a/lib/wal/CMakeLists.txt +++ b/lib/wal/CMakeLists.txt @@ -17,6 +17,6 @@ add_library(wal sources/Component/Component.cpp sources/System/System.cpp sources/Models/Callback.hpp -) + sources/View/View.hpp) target_include_directories(wal PUBLIC sources) \ No newline at end of file diff --git a/lib/wal/sources/Scene/Scene.hpp b/lib/wal/sources/Scene/Scene.hpp index bddf1e5c..840d2ec5 100644 --- a/lib/wal/sources/Scene/Scene.hpp +++ b/lib/wal/sources/Scene/Scene.hpp @@ -7,6 +7,7 @@ #include #include +#include "View/View.hpp" #include "Entity/Entity.hpp" namespace WAL @@ -17,6 +18,8 @@ namespace WAL private: //! @brief The list of registered entities std::vector _entities = {}; + //! @brief A list of cached views. +// std::vector _views = {}; public: //! @brief Get the list of entities. std::vector &getEntities(); diff --git a/lib/wal/sources/View/View.hpp b/lib/wal/sources/View/View.hpp new file mode 100644 index 00000000..9f004188 --- /dev/null +++ b/lib/wal/sources/View/View.hpp @@ -0,0 +1,35 @@ +// +// Created by Zoe Roux on 2021-06-02. +// + + +#pragma once + +#include +#include + +namespace WAL +{ + //! @brief A view caching entities containing requested components + template + class View + { + //! @brief A list of reference to entities that contains the + std::vector> entities; + + explicit View(std::vector &entities) + : entities() + { + std::copy_if(entities.begin(), entities.end(), std::back_inserter(this->entities), [](Entity &entity) { + return (entity.hasComponent() && ...); + }); + } + + //! @brief A default copy constructor. + View(const View &) = default; + //! @brief A default destructor. + ~View() = default; + //! @brief A View is assignable. + View &operator=(const View &) = default; + }; +} \ No newline at end of file From 72d66bc9702ba2d0a932df4c5dbee33b3dfdc626 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Thu, 3 Jun 2021 09:13:58 +0200 Subject: [PATCH 33/60] setting copy ctor of ModelAnimations.hpp to default --- lib/Ray/sources/Model/ModelAnimations.hpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/Ray/sources/Model/ModelAnimations.hpp b/lib/Ray/sources/Model/ModelAnimations.hpp index 83978b82..2b99e1d7 100644 --- a/lib/Ray/sources/Model/ModelAnimations.hpp +++ b/lib/Ray/sources/Model/ModelAnimations.hpp @@ -21,17 +21,14 @@ namespace RAY { //! @param filePath Path to the file containing animations ModelAnimations(const std::string &filePath); - //! @brief Only single entity can hold these animations pointers - ModelAnimations(const ModelAnimations &) = delete; + //! @brief default copy ctor + ModelAnimations(const ModelAnimations &) = default; //! @brief Default constructor ~ModelAnimations() = default; - //! @brief Default move ctor - ModelAnimations(ModelAnimations &&) = default; - - //! @brief Only single entity can hold these animations pointers - ModelAnimations &operator=(const ModelAnimations &) = delete; + //! @brief Default assignment operator + ModelAnimations &operator=(const ModelAnimations &) = default; //! @brief Castin Object to raw model animation pointer ModelAnimation &operator[](int index); From edd92efdbe2f45dc6d0f97243ced001a39b6c8bb Mon Sep 17 00:00:00 2001 From: Arthi-chaud <60505370+Arthi-chaud@users.noreply.github.com> Date: Thu, 3 Jun 2021 10:59:31 +0200 Subject: [PATCH 34/60] Update readme's build instruction --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 18e93e7a..0d19b943 100644 --- a/README.md +++ b/README.md @@ -40,12 +40,13 @@ mkdir build cd build cmake .. cmake --build . +cd - ``` Enjoy ! ```bash -./bomberman +./build/bomberman ``` From 0e00a599f616a2061fd520d0c3f3aa67403511db Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Thu, 3 Jun 2021 11:19:22 +0200 Subject: [PATCH 35/60] use custom html --- CMakeLists.txt | 3 +- sources/wasm/frontend.html | 147 +++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 sources/wasm/frontend.html diff --git a/CMakeLists.txt b/CMakeLists.txt index 78906406..9fc51470 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,8 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/lib/Ray) if (EMSCRIPTEN) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY -s NO_DISABLE_EXCEPTION_CATCHING --preload-file ../assets") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s NO_DISABLE_EXCEPTION_CATCHING --shell-file ../sources/wasm/frontend.html --preload-file ../assets") set(CMAKE_EXECUTABLE_SUFFIX ".html") endif () diff --git a/sources/wasm/frontend.html b/sources/wasm/frontend.html new file mode 100644 index 00000000..2816e377 --- /dev/null +++ b/sources/wasm/frontend.html @@ -0,0 +1,147 @@ + + + + + + Emscripten-Generated Code + + + +
+
emscripten
+
Downloading...
+
+ +
+
+ +
+
+
+ Resize canvas + Lock/hide mouse pointer +     + +
+ +
+ + {{{ SCRIPT }}} + + \ No newline at end of file From 56f1b321c0568ec28b61ee3218b4d942166f6bd1 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Thu, 3 Jun 2021 11:46:56 +0200 Subject: [PATCH 36/60] better workflow for wasm compilation --- .github/workflows/build_web.yml | 6 ++---- .gitignore | 4 ++-- CMakeLists.txt | 2 +- sources/wasm/frontend.html | 2 +- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/.github/workflows/build_web.yml b/.github/workflows/build_web.yml index 9c84b83b..c2604c47 100644 --- a/.github/workflows/build_web.yml +++ b/.github/workflows/build_web.yml @@ -1,12 +1,10 @@ name: Web Build (Emscripten) -on: - push: - branches: - wasm +on: [push, pull_request] jobs: Push: runs-on: ubuntu-latest + if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository steps: - uses: actions/checkout@v1 with: diff --git a/.gitignore b/.gitignore index 74044c49..407c1f67 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,5 @@ cmake-build-debug .vscode build/* docs/* -emsdk/* -build_web/* \ No newline at end of file +emsdk*/ +build_web/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 9fc51470..fa75fb54 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/lib/Ray) if (EMSCRIPTEN) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s NO_DISABLE_EXCEPTION_CATCHING --shell-file ../sources/wasm/frontend.html --preload-file ../assets") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s NO_DISABLE_EXCEPTION_CATCHING --preload-file ../assets") set(CMAKE_EXECUTABLE_SUFFIX ".html") endif () diff --git a/sources/wasm/frontend.html b/sources/wasm/frontend.html index 2816e377..b6f04e36 100644 --- a/sources/wasm/frontend.html +++ b/sources/wasm/frontend.html @@ -1,5 +1,5 @@ - + From c1be763a9b7d52d27631ee980d7a51dc94cd2fd8 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Thu, 3 Jun 2021 12:01:39 +0200 Subject: [PATCH 37/60] Revert "better workflow for wasm compilation" This reverts commit 56f1b321c0568ec28b61ee3218b4d942166f6bd1. --- .github/workflows/build_web.yml | 6 ++++-- .gitignore | 4 ++-- CMakeLists.txt | 2 +- sources/wasm/frontend.html | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build_web.yml b/.github/workflows/build_web.yml index c2604c47..9c84b83b 100644 --- a/.github/workflows/build_web.yml +++ b/.github/workflows/build_web.yml @@ -1,10 +1,12 @@ name: Web Build (Emscripten) -on: [push, pull_request] +on: + push: + branches: + wasm jobs: Push: runs-on: ubuntu-latest - if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository steps: - uses: actions/checkout@v1 with: diff --git a/.gitignore b/.gitignore index 407c1f67..74044c49 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,5 @@ cmake-build-debug .vscode build/* docs/* -emsdk*/ -build_web/ \ No newline at end of file +emsdk/* +build_web/* \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 16771554..71343bba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/lib/Ray) if (EMSCRIPTEN) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s NO_DISABLE_EXCEPTION_CATCHING --preload-file ../assets") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s NO_DISABLE_EXCEPTION_CATCHING --shell-file ../sources/wasm/frontend.html --preload-file ../assets") set(CMAKE_EXECUTABLE_SUFFIX ".html") endif () diff --git a/sources/wasm/frontend.html b/sources/wasm/frontend.html index b6f04e36..2816e377 100644 --- a/sources/wasm/frontend.html +++ b/sources/wasm/frontend.html @@ -1,5 +1,5 @@ - + From c40dc583309407fbf8263005c8fe05e6a69e2b53 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Thu, 3 Jun 2021 12:02:49 +0200 Subject: [PATCH 38/60] Revert "use custom html" This reverts commit 0e00a599f616a2061fd520d0c3f3aa67403511db. --- CMakeLists.txt | 3 +- sources/wasm/frontend.html | 147 ------------------------------------- 2 files changed, 1 insertion(+), 149 deletions(-) delete mode 100644 sources/wasm/frontend.html diff --git a/CMakeLists.txt b/CMakeLists.txt index 71343bba..156d2a31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,8 +12,7 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/lib/Ray) if (EMSCRIPTEN) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s NO_DISABLE_EXCEPTION_CATCHING --shell-file ../sources/wasm/frontend.html --preload-file ../assets") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY -s NO_DISABLE_EXCEPTION_CATCHING --preload-file ../assets") set(CMAKE_EXECUTABLE_SUFFIX ".html") endif () diff --git a/sources/wasm/frontend.html b/sources/wasm/frontend.html deleted file mode 100644 index 2816e377..00000000 --- a/sources/wasm/frontend.html +++ /dev/null @@ -1,147 +0,0 @@ - - - - - - Emscripten-Generated Code - - - -
-
emscripten
-
Downloading...
-
- -
-
- -
-
-
- Resize canvas - Lock/hide mouse pointer -     - -
- -
- - {{{ SCRIPT }}} - - \ No newline at end of file From a0ffafbd6da717706bdfd767054f57397f40713c Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Thu, 3 Jun 2021 12:03:06 +0200 Subject: [PATCH 39/60] Revert "Revert "use custom html"" This reverts commit c40dc583309407fbf8263005c8fe05e6a69e2b53. --- CMakeLists.txt | 3 +- sources/wasm/frontend.html | 147 +++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 sources/wasm/frontend.html diff --git a/CMakeLists.txt b/CMakeLists.txt index 156d2a31..71343bba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,8 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/lib/Ray) if (EMSCRIPTEN) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY -s NO_DISABLE_EXCEPTION_CATCHING --preload-file ../assets") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s NO_DISABLE_EXCEPTION_CATCHING --shell-file ../sources/wasm/frontend.html --preload-file ../assets") set(CMAKE_EXECUTABLE_SUFFIX ".html") endif () diff --git a/sources/wasm/frontend.html b/sources/wasm/frontend.html new file mode 100644 index 00000000..2816e377 --- /dev/null +++ b/sources/wasm/frontend.html @@ -0,0 +1,147 @@ + + + + + + Emscripten-Generated Code + + + +
+
emscripten
+
Downloading...
+
+ +
+
+ +
+
+
+ Resize canvas + Lock/hide mouse pointer +     + +
+ +
+ + {{{ SCRIPT }}} + + \ No newline at end of file From 63d6d8479b0888298d188f6e7e32bd5078b5adaf Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Thu, 3 Jun 2021 12:33:57 +0200 Subject: [PATCH 40/60] fix merge --- .gitignore | 2 +- sources/System/Animation/AnimationsSystem.cpp | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 74044c49..7c2238fc 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,5 @@ cmake-build-debug .vscode build/* docs/* -emsdk/* +emsdk/ build_web/* \ No newline at end of file diff --git a/sources/System/Animation/AnimationsSystem.cpp b/sources/System/Animation/AnimationsSystem.cpp index 3400cf10..cce35719 100644 --- a/sources/System/Animation/AnimationsSystem.cpp +++ b/sources/System/Animation/AnimationsSystem.cpp @@ -13,7 +13,7 @@ namespace BBM AnimationsSystem::AnimationsSystem() : WAL::System({ - typeid(Drawable3DComponent), + typeid(Drawable3DComponent), typeid(AnimationsComponent) }) { @@ -21,12 +21,15 @@ namespace BBM void AnimationsSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) { - auto &model = entity.getComponent>(); + auto &model = entity.getComponent(); auto &anim = entity.getComponent(); if (anim.isDisabled()) return; - model.member.setAnimation(anim.getCurrentModelAnim()); - anim.incCurrentAnimFrameCounter(); + auto modelPtr = std::dynamic_pointer_cast(model.drawable); + if (modelPtr) { + modelPtr->setAnimation(anim.getCurrentModelAnim()); + anim.incCurrentAnimFrameCounter(); + } } } \ No newline at end of file From dd62fc806c707cfaa859af0ea9e2400196115897 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Thu, 3 Jun 2021 14:08:32 +0200 Subject: [PATCH 41/60] remove python server script --- wasm-server.py | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 wasm-server.py diff --git a/wasm-server.py b/wasm-server.py deleted file mode 100644 index 77aad1db..00000000 --- a/wasm-server.py +++ /dev/null @@ -1,24 +0,0 @@ -# Python 3 - -import sys -import socketserver -from http.server import SimpleHTTPRequestHandler - -class WasmHandler(SimpleHTTPRequestHandler): - def end_headers(self): - # Include additional response headers here. CORS for example: - #self.send_header('Access-Control-Allow-Origin', '*') - SimpleHTTPRequestHandler.end_headers(self) - - -# Python 3.7.5 adds in the WebAssembly Media Type. If this is an older -# version, add in the Media Type. -if sys.version_info < (3, 7, 5): - WasmHandler.extensions_map['.wasm'] = 'application/wasm' - - -if __name__ == '__main__': - PORT = 8080 - with socketserver.TCPServer(("", PORT), WasmHandler) as httpd: - print("Listening on port {}. Press Ctrl+C to stop.".format(PORT)) - httpd.serve_forever() From ba315e4010f170142481318f5ab6eb6ef78c66bb Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Thu, 3 Jun 2021 15:00:42 +0200 Subject: [PATCH 42/60] add removed target to camera --- sources/System/Renderer/RenderSystem.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sources/System/Renderer/RenderSystem.cpp b/sources/System/Renderer/RenderSystem.cpp index 5073ee30..03330498 100644 --- a/sources/System/Renderer/RenderSystem.cpp +++ b/sources/System/Renderer/RenderSystem.cpp @@ -59,6 +59,8 @@ namespace BBM void RenderSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) { const auto &pos = entity.getComponent(); + const auto &cam = entity.getComponent(); _camera.setPosition(pos.position); + _camera.setTarget(cam.target); } } \ No newline at end of file From d69198ca1bff7eb83cd9bfe964ee62f3329bc7db Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Thu, 3 Jun 2021 15:01:59 +0200 Subject: [PATCH 43/60] remove unnecessary comment --- sources/main.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/sources/main.cpp b/sources/main.cpp index cfb244b4..bd13441a 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -23,6 +23,5 @@ int main(int argc, char **argv) usage(argv[0]); return 1; } - //return demo(); return BBM::run(); } From 0b2a151ba6f3a19887528830de24629a68aa40f4 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Thu, 3 Jun 2021 15:41:32 +0200 Subject: [PATCH 44/60] using tuples instead of bad, ugly vC void casts --- .gitignore | 3 ++- lib/wal/sources/Wal.hpp | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 7c2238fc..f93eff30 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ cmake-build-debug build/* docs/* emsdk/ -build_web/* \ No newline at end of file +build_web/* +wasm-python.py \ No newline at end of file diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index a2feacbe..3785081a 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -115,8 +115,8 @@ namespace WAL Callback update(callback); #if defined(PLATFORM_WEB) - void *paramPtr[3] = {(void *)this, (void *)&update, (void *)&state}; - return emscripten_set_main_loop_arg((em_arg_callback_func)runIteration, (void *)paramPtr, 0, 1); + std::tuple iterationParams(this, &update, &state); + return emscripten_set_main_loop_arg((em_arg_callback_func)runIteration, (void *)&iterationParams, 0, 1); #else return this->run(update, state); #endif @@ -151,10 +151,10 @@ namespace WAL template static void runIteration(void *param) { - static void **paramsPtr = (void **)param; - static Wal *wal = (Wal *)paramsPtr[0]; - static const Callback callback = *((Callback *)paramsPtr[1]); - static T *state = (T *)paramsPtr[2]; + static auto iterationParams = reinterpret_cast *, T *> *>(param); + static const Callback callback = *((Callback *)std::get<1>(*iterationParams)); + static T *state = (T *)std::get<2>(*iterationParams); + static Wal *wal = (Wal *)std::get<0>(*iterationParams); static auto lastTick = std::chrono::steady_clock::now(); static std::chrono::nanoseconds fBehind(0); From 0d37a560d7a71e905ef166dbede34f57102ba3f0 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Thu, 3 Jun 2021 18:20:37 +0200 Subject: [PATCH 45/60] Reworking the view --- CMakeLists.txt | 1 + lib/wal/CMakeLists.txt | 2 +- lib/wal/sources/Entity/Entity.cpp | 5 +++- lib/wal/sources/Entity/Entity.hpp | 21 ++++++++++++- lib/wal/sources/Scene/Scene.cpp | 16 ++++++++++ lib/wal/sources/Scene/Scene.hpp | 50 ++++++++++++++++++++++++------- lib/wal/sources/System/System.hpp | 5 +++- lib/wal/sources/View/View.hpp | 35 ---------------------- lib/wal/sources/Wal.cpp | 1 + lib/wal/sources/Wal.hpp | 5 ++-- sources/Runner/Runner.cpp | 8 ++--- tests/CallbackTest.cpp | 4 ++- tests/CollisionTest.cpp | 4 +-- tests/EntityTests.cpp | 10 +++++-- tests/MoveTests.cpp | 3 +- tests/ViewTest.cpp | 33 ++++++++++++++++++++ 16 files changed, 141 insertions(+), 62 deletions(-) delete mode 100644 lib/wal/sources/View/View.hpp create mode 100644 tests/ViewTest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f1f860c8..142d675a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,6 +77,7 @@ add_executable(unit_tests EXCLUDE_FROM_ALL tests/CallbackTest.cpp tests/CollisionTest.cpp tests/MoveTests.cpp + tests/ViewTest.cpp ) target_include_directories(unit_tests PUBLIC sources) target_link_libraries(unit_tests PUBLIC wal ray) diff --git a/lib/wal/CMakeLists.txt b/lib/wal/CMakeLists.txt index ba2a60a0..e8cd8655 100644 --- a/lib/wal/CMakeLists.txt +++ b/lib/wal/CMakeLists.txt @@ -17,6 +17,6 @@ add_library(wal sources/Component/Component.cpp sources/System/System.cpp sources/Models/Callback.hpp - sources/View/View.hpp) +) target_include_directories(wal PUBLIC sources) \ No newline at end of file diff --git a/lib/wal/sources/Entity/Entity.cpp b/lib/wal/sources/Entity/Entity.cpp index 1dd1a1b3..90e9a41a 100644 --- a/lib/wal/sources/Entity/Entity.cpp +++ b/lib/wal/sources/Entity/Entity.cpp @@ -10,13 +10,15 @@ namespace WAL { unsigned Entity::nextID = 0; - Entity::Entity(std::string name) + Entity::Entity(Scene &scene, std::string name) : _uid(Entity::nextID++), + _scene(scene), _name(std::move(name)) { } Entity::Entity(const Entity &other) : _uid(Entity::nextID++), + _scene(other._scene), _name(other._name), _disabled(other._disabled) { @@ -50,6 +52,7 @@ namespace WAL if (this->hasComponent(type)) throw DuplicateError("A component of the type \"" + std::string(type.name()) + "\" already exists."); this->_components.emplace(type, component.clone(*this)); + this->_scene._componentAdded(*this, type); return *this; } diff --git a/lib/wal/sources/Entity/Entity.hpp b/lib/wal/sources/Entity/Entity.hpp index e9fa672e..60bd0090 100644 --- a/lib/wal/sources/Entity/Entity.hpp +++ b/lib/wal/sources/Entity/Entity.hpp @@ -10,9 +10,23 @@ #include #include "Component/Component.hpp" #include "Exception/WalError.hpp" +#include "Wal.hpp" namespace WAL { + + class Scene { + public: + //! @brief Notify this scene that a component has been added to the given entity. + //! @param entity The entity with the new component + //! @param type The type of the component added. + void _componentAdded(const Entity &entity, std::type_index type); + //! @brief Notify this scene that a component has been removed to the given entity. + //! @param entity The entity with the removed component + //! @param type The type of the component removed. namespace WAL + void _componentRemoved(const Entity &entity, std::type_index type); + }; + //! @brief An entity of the WAL's ECS. class Entity { @@ -28,6 +42,9 @@ namespace WAL //! @brief This ID will be the one of the next entity created. static unsigned nextID; + protected: + //! @brief A reference to the ECS. + Scene &_scene; public: //! @brief Get the ID of the entity. unsigned getUid() const; @@ -79,6 +96,7 @@ namespace WAL if (this->hasComponent(type)) throw DuplicateError("A component of the type \"" + std::string(type.name()) + "\" already exists."); this->_components[type] = std::make_unique(*this, std::forward(params)...); + this->_scene._componentAdded(*this, type); return *this; } @@ -97,11 +115,12 @@ namespace WAL if (existing == this->_components.end()) throw NotFoundError("No component could be found with the type \"" + std::string(type.name()) + "\"."); this->_components.erase(existing); + this->_scene._componentRemoved(*this, type); return *this; } //! @brief A default constructor - explicit Entity(std::string name); + explicit Entity(Scene &wal, std::string name); //! @brief An entity is copyable Entity(const Entity &); //! @brief An entity is movable. diff --git a/lib/wal/sources/Scene/Scene.cpp b/lib/wal/sources/Scene/Scene.cpp index 3c07f3c8..930382f7 100644 --- a/lib/wal/sources/Scene/Scene.cpp +++ b/lib/wal/sources/Scene/Scene.cpp @@ -10,8 +10,24 @@ namespace WAL { return this->_entities; } + Scene &Scene::operator=(const Scene &) { return *this; } + + Entity &Scene::addEntity(const std::string &name) + { + return this->_entities.emplace_back(*this, name); + } + + void Scene::_componentAdded(const Entity &entity, std::type_index type) + { + + } + + void Scene::_componentRemoved(const Entity &entity, std::type_index type) + { + + } } // namespace WAL \ No newline at end of file diff --git a/lib/wal/sources/Scene/Scene.hpp b/lib/wal/sources/Scene/Scene.hpp index 840d2ec5..4ccf9316 100644 --- a/lib/wal/sources/Scene/Scene.hpp +++ b/lib/wal/sources/Scene/Scene.hpp @@ -3,11 +3,11 @@ // -#pragma once +#ifndef WAL_SCENE +#define WAL_SCENE #include #include -#include "View/View.hpp" #include "Entity/Entity.hpp" namespace WAL @@ -18,20 +18,44 @@ namespace WAL private: //! @brief The list of registered entities std::vector _entities = {}; - //! @brief A list of cached views. -// std::vector _views = {}; + + //! @brief Notify this scene that a component has been added to the given entity. + //! @param entity The entity with the new component + //! @param type The type of the component added. + void _componentAdded(const Entity &entity, std::type_index type); + //! @brief Notify this scene that a component has been removed to the given entity. + //! @param entity The entity with the removed component + //! @param type The type of the component removed. + void _componentRemoved(const Entity &entity, std::type_index type); public: //! @brief Get the list of entities. std::vector &getEntities(); - //! @brief Add a new entity to the scene, you can use this method with the same arguments as the entity's constructor. - //! @return The current scene is returned to allow you to chain call. - template - Entity &addEntity(Params &&...params) + //! @brief Add a new entity to the scene. + //! @param name The name of the created entity. + //! @return The created entity is returned. + Entity &addEntity(const std::string &name); + + template + std::vector> &view() { - return this->_entities.emplace_back(std::forward(params)...); + return this->view(typeid(Components)...); } +#pragma clang diagnostic push +#pragma ide diagnostic ignored "NotImplementedFunctions" + template + std::vector> &view(const Components &...index) requires(std::is_same_v) + { + static std::vector> view; + + std::copy_if(this->_entities.begin(), this->_entities.end(), std::back_inserter(view), [&index...](Entity &entity) { + return (entity.hasComponent(index) && ...); + }); + return view; + } +#pragma clang diagnostic pop + //! @brief A default constructor Scene() = default; //! @brief A scene is copy constructable @@ -41,5 +65,11 @@ namespace WAL //! @brief A scene is assignable Scene &operator=(const Scene &); Scene(Scene &&) = default; + + friend Entity; }; -} // namespace WAL \ No newline at end of file +} // namespace WAL + +#else + +#endif \ No newline at end of file diff --git a/lib/wal/sources/System/System.hpp b/lib/wal/sources/System/System.hpp index 59c82c4a..c3e5ee83 100644 --- a/lib/wal/sources/System/System.hpp +++ b/lib/wal/sources/System/System.hpp @@ -6,10 +6,13 @@ #include #include -#include "Entity/Entity.hpp" +#include +#include namespace WAL { + class Entity; + //! @brief A base system of WAL class System { diff --git a/lib/wal/sources/View/View.hpp b/lib/wal/sources/View/View.hpp deleted file mode 100644 index 9f004188..00000000 --- a/lib/wal/sources/View/View.hpp +++ /dev/null @@ -1,35 +0,0 @@ -// -// Created by Zoe Roux on 2021-06-02. -// - - -#pragma once - -#include -#include - -namespace WAL -{ - //! @brief A view caching entities containing requested components - template - class View - { - //! @brief A list of reference to entities that contains the - std::vector> entities; - - explicit View(std::vector &entities) - : entities() - { - std::copy_if(entities.begin(), entities.end(), std::back_inserter(this->entities), [](Entity &entity) { - return (entity.hasComponent() && ...); - }); - } - - //! @brief A default copy constructor. - View(const View &) = default; - //! @brief A default destructor. - ~View() = default; - //! @brief A View is assignable. - View &operator=(const View &) = default; - }; -} \ No newline at end of file diff --git a/lib/wal/sources/Wal.cpp b/lib/wal/sources/Wal.cpp index 875e3bd8..d4262b73 100644 --- a/lib/wal/sources/Wal.cpp +++ b/lib/wal/sources/Wal.cpp @@ -5,6 +5,7 @@ #include #include #include "Wal.hpp" +#include "Scene/Scene.hpp" namespace WAL { diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index 50a87188..a16e4f5c 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -10,13 +10,14 @@ #include #include #include "Exception/WalError.hpp" -#include "Scene/Scene.hpp" -#include "Entity/Entity.hpp" #include "System/System.hpp" #include "Models/Callback.hpp" namespace WAL { + class Entity; + class Scene; + //! @brief The main WAL class, it is used to setup and run the ECS. class Wal { diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index db3896c9..7e03bfef 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -64,9 +64,9 @@ namespace BBM wal.addSystem(window); } - std::shared_ptr loadGameScene() + std::shared_ptr loadGameScene(WAL::Wal &wal) { - auto scene = std::make_shared(); + auto scene = std::make_shared(wal); scene->addEntity("player") .addComponent() .addComponent>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) @@ -90,7 +90,7 @@ namespace BBM scene->addEntity("camera") .addComponent(8, 20, 7) .addComponent(Vector3f(8, 0, 8)); - std::srand(std::time(NULL)); + std::srand(std::time(nullptr)); MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); return scene; } @@ -100,7 +100,7 @@ namespace BBM WAL::Wal wal; addSystems(wal); enableRaylib(wal); - wal.scene = loadGameScene(); + wal.scene = loadGameScene(wal); try { wal.run(updateState); diff --git a/tests/CallbackTest.cpp b/tests/CallbackTest.cpp index 534ca9f4..44b3a103 100644 --- a/tests/CallbackTest.cpp +++ b/tests/CallbackTest.cpp @@ -4,6 +4,7 @@ #include #include +#include #include "Entity/Entity.hpp" #include "Models/Callback.hpp" @@ -36,6 +37,7 @@ TEST_CASE("Callback multiple arguments", "[Callback]") callback.addCallback([](const std::string& str, int a, unsigned *value, Entity &entity) { throw std::runtime_error(""); }); - Entity entity("name"); + Wal wal; + Entity entity(wal, "name"); REQUIRE_THROWS_AS(callback("1", 0, nullptr, entity), std::runtime_error); } diff --git a/tests/CollisionTest.cpp b/tests/CollisionTest.cpp index 69c75207..42b7d186 100644 --- a/tests/CollisionTest.cpp +++ b/tests/CollisionTest.cpp @@ -21,7 +21,7 @@ TEST_CASE("Collision test", "[Component][System]") { Wal wal; CollisionSystem collision(wal); - wal.scene = std::shared_ptr(new Scene); + wal.scene = std::make_shared(wal); wal.scene->addEntity("player") .addComponent() .addComponent([](Entity &actual, const Entity &) { @@ -65,7 +65,7 @@ TEST_CASE("Collsion test with movable", "[Component][System]") Wal wal; CollisionSystem collision(wal); MovableSystem movable; - wal.scene = std::shared_ptr(new Scene); + wal.scene = std::make_shared(wal); wal.scene->addEntity("player") .addComponent() .addComponent([](Entity &actual, const Entity &) {}, [](Entity &actual, const Entity &) {}, 5.0) diff --git a/tests/EntityTests.cpp b/tests/EntityTests.cpp index f58fd0c1..7a0a3490 100644 --- a/tests/EntityTests.cpp +++ b/tests/EntityTests.cpp @@ -5,13 +5,15 @@ #include "Entity/Entity.hpp" #include "Component/Position/PositionComponent.hpp" #include +#include using namespace WAL; using namespace BBM; TEST_CASE("Component", "[Entity]") { - Entity entity("Bob"); + Wal wal; + Entity entity(wal, "Bob"); entity.addComponent(2, 3, 4); SECTION("Check value") { @@ -31,13 +33,15 @@ TEST_CASE("Component", "[Entity]") TEST_CASE("ComponentNotFound", "[Entity]") { - Entity entity("Bob"); + Wal wal; + Entity entity(wal, "Bob"); REQUIRE_THROWS_AS(entity.getComponent(), NotFoundError); } TEST_CASE("Add component by reference", "[Entity]") { - Entity entity("Bob"); + Wal wal; + Entity entity(wal, "Bob"); PositionComponent component(entity, 4, 5, 6); REQUIRE(&entity.addComponent(component) == &entity); diff --git a/tests/MoveTests.cpp b/tests/MoveTests.cpp index cd37d22a..0a47bd10 100644 --- a/tests/MoveTests.cpp +++ b/tests/MoveTests.cpp @@ -19,7 +19,8 @@ using namespace BBM; TEST_CASE("Move test", "[Component][System]") { - Scene scene; + Wal wal; + Scene scene(wal); scene.addEntity("player") .addComponent() .addComponent() diff --git a/tests/ViewTest.cpp b/tests/ViewTest.cpp new file mode 100644 index 00000000..90328acd --- /dev/null +++ b/tests/ViewTest.cpp @@ -0,0 +1,33 @@ +// +// Created by Zoe Roux on 6/3/21. +// + +#include "Entity/Entity.hpp" +#include "Component/Position/PositionComponent.hpp" +#include "System/Movable/MovableSystem.hpp" +#include "System/Controllable/ControllableSystem.hpp" +#include +#include +#include + +#define private public +#include + +using namespace WAL; +using namespace BBM; + +TEST_CASE("View creation", "[View]") +{ + Wal wal; + Scene scene(wal); + scene.addEntity("player") + .addComponent() + .addComponent(); + scene.addEntity("Box") + .addComponent(); + REQUIRE(scene.view().size() == 2); + REQUIRE(scene.view().size() == 1); + Entity &entity = *scene.getEntities().begin(); + Entity &firstView = *scene.view().begin(); + REQUIRE(&entity == &firstView); +} \ No newline at end of file From a11bd21ec308c7fc2b528dfb4e5724246fd1037a Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Thu, 3 Jun 2021 22:42:38 +0200 Subject: [PATCH 46/60] Creating a basic view --- lib/wal/CMakeLists.txt | 2 +- lib/wal/sources/Entity/Entity.cpp | 11 +++++ lib/wal/sources/Entity/Entity.hpp | 25 ++++------ lib/wal/sources/Scene/Scene.cpp | 23 +++++++-- lib/wal/sources/Scene/Scene.hpp | 36 +++++--------- lib/wal/sources/System/System.hpp | 3 +- lib/wal/sources/View/BaseView.hpp | 50 ++++++++++++++++++++ lib/wal/sources/Wal.hpp | 1 + sources/Runner/Runner.cpp | 2 +- sources/System/Collision/CollisionSystem.cpp | 1 + tests/CallbackTest.cpp | 5 +- tests/CollisionTest.cpp | 4 +- tests/EntityTests.cpp | 13 ++--- tests/MoveTests.cpp | 3 +- tests/ViewTest.cpp | 5 +- 15 files changed, 121 insertions(+), 63 deletions(-) create mode 100644 lib/wal/sources/View/BaseView.hpp diff --git a/lib/wal/CMakeLists.txt b/lib/wal/CMakeLists.txt index e8cd8655..088188b4 100644 --- a/lib/wal/CMakeLists.txt +++ b/lib/wal/CMakeLists.txt @@ -17,6 +17,6 @@ add_library(wal sources/Component/Component.cpp sources/System/System.cpp sources/Models/Callback.hpp -) + sources/View/BaseView.hpp) target_include_directories(wal PUBLIC sources) \ No newline at end of file diff --git a/lib/wal/sources/Entity/Entity.cpp b/lib/wal/sources/Entity/Entity.cpp index 90e9a41a..35fa2c53 100644 --- a/lib/wal/sources/Entity/Entity.cpp +++ b/lib/wal/sources/Entity/Entity.cpp @@ -3,6 +3,7 @@ // #include "Entity/Entity.hpp" +#include "Scene/Scene.hpp" #include #include @@ -65,4 +66,14 @@ namespace WAL { return this->_components.contains(type); } + + void Entity::_componentAdded(const std::type_index &type) + { + this->_scene._componentAdded(*this, type); + } + + void Entity::_componentRemoved(const std::type_index &type) + { + this->_scene._componentRemoved(*this, type); + } } // namespace WAL \ No newline at end of file diff --git a/lib/wal/sources/Entity/Entity.hpp b/lib/wal/sources/Entity/Entity.hpp index 60bd0090..b7e46dc2 100644 --- a/lib/wal/sources/Entity/Entity.hpp +++ b/lib/wal/sources/Entity/Entity.hpp @@ -10,22 +10,10 @@ #include #include "Component/Component.hpp" #include "Exception/WalError.hpp" -#include "Wal.hpp" namespace WAL { - - class Scene { - public: - //! @brief Notify this scene that a component has been added to the given entity. - //! @param entity The entity with the new component - //! @param type The type of the component added. - void _componentAdded(const Entity &entity, std::type_index type); - //! @brief Notify this scene that a component has been removed to the given entity. - //! @param entity The entity with the removed component - //! @param type The type of the component removed. namespace WAL - void _componentRemoved(const Entity &entity, std::type_index type); - }; + class Scene; //! @brief An entity of the WAL's ECS. class Entity @@ -42,6 +30,13 @@ namespace WAL //! @brief This ID will be the one of the next entity created. static unsigned nextID; + + //! @brief Callback called when a component is added + //! @param type The type of component + void _componentAdded(const std::type_index &type); + //! @brief Callback called when a component is removed + //! @param type The type of component + void _componentRemoved(const std::type_index &type); protected: //! @brief A reference to the ECS. Scene &_scene; @@ -96,7 +91,7 @@ namespace WAL if (this->hasComponent(type)) throw DuplicateError("A component of the type \"" + std::string(type.name()) + "\" already exists."); this->_components[type] = std::make_unique(*this, std::forward(params)...); - this->_scene._componentAdded(*this, type); + this->_componentAdded(type); return *this; } @@ -115,7 +110,7 @@ namespace WAL if (existing == this->_components.end()) throw NotFoundError("No component could be found with the type \"" + std::string(type.name()) + "\"."); this->_components.erase(existing); - this->_scene._componentRemoved(*this, type); + this->_componentRemoved(type); return *this; } diff --git a/lib/wal/sources/Scene/Scene.cpp b/lib/wal/sources/Scene/Scene.cpp index 930382f7..db5c9d6a 100644 --- a/lib/wal/sources/Scene/Scene.cpp +++ b/lib/wal/sources/Scene/Scene.cpp @@ -21,13 +21,28 @@ namespace WAL return this->_entities.emplace_back(*this, name); } - void Scene::_componentAdded(const Entity &entity, std::type_index type) + void Scene::_componentAdded(Entity &entity, const std::type_index &type) { - + for (auto &view : this->_views) { + if (std::find(view->types.begin(), view->types.end(), type) == view->types.end()) + continue; + bool valid = std::all_of(view->types.begin(), view->types.end(), [&entity](const auto &type){ + return entity.hasComponent(type); + }); + if (valid) + view->entities.emplace_back(entity); + } } - void Scene::_componentRemoved(const Entity &entity, std::type_index type) + void Scene::_componentRemoved(const Entity &entity, const std::type_index &type) { - + for (auto &view : this->_views) { + if (std::find(view->types.begin(), view->types.end(), type) == view->types.end()) + continue; + view->entities.erase(std::remove_if(view->entities.begin(), view->entities.end(), [&entity](const auto &ref) + { + return &ref.get() == &entity; + }), view->entities.end()); + } } } // namespace WAL \ No newline at end of file diff --git a/lib/wal/sources/Scene/Scene.hpp b/lib/wal/sources/Scene/Scene.hpp index 4ccf9316..e233debc 100644 --- a/lib/wal/sources/Scene/Scene.hpp +++ b/lib/wal/sources/Scene/Scene.hpp @@ -3,11 +3,11 @@ // -#ifndef WAL_SCENE -#define WAL_SCENE +#pragma once #include #include +#include #include "Entity/Entity.hpp" namespace WAL @@ -18,15 +18,17 @@ namespace WAL private: //! @brief The list of registered entities std::vector _entities = {}; + //! @brief The list of cached views to update. + std::vector> _views = {}; //! @brief Notify this scene that a component has been added to the given entity. //! @param entity The entity with the new component //! @param type The type of the component added. - void _componentAdded(const Entity &entity, std::type_index type); + void _componentAdded(Entity &entity, const std::type_index &type); //! @brief Notify this scene that a component has been removed to the given entity. //! @param entity The entity with the removed component //! @param type The type of the component removed. - void _componentRemoved(const Entity &entity, std::type_index type); + void _componentRemoved(const Entity &entity, const std::type_index &type); public: //! @brief Get the list of entities. std::vector &getEntities(); @@ -37,25 +39,13 @@ namespace WAL Entity &addEntity(const std::string &name); template - std::vector> &view() + View &view() { - return this->view(typeid(Components)...); + static auto view = std::make_shared>(this->_entities); + this->_views.emplace_back(view); + return *view; } -#pragma clang diagnostic push -#pragma ide diagnostic ignored "NotImplementedFunctions" - template - std::vector> &view(const Components &...index) requires(std::is_same_v) - { - static std::vector> view; - - std::copy_if(this->_entities.begin(), this->_entities.end(), std::back_inserter(view), [&index...](Entity &entity) { - return (entity.hasComponent(index) && ...); - }); - return view; - } -#pragma clang diagnostic pop - //! @brief A default constructor Scene() = default; //! @brief A scene is copy constructable @@ -68,8 +58,4 @@ namespace WAL friend Entity; }; -} // namespace WAL - -#else - -#endif \ No newline at end of file +} // namespace WAL \ No newline at end of file diff --git a/lib/wal/sources/System/System.hpp b/lib/wal/sources/System/System.hpp index c3e5ee83..8dc164a7 100644 --- a/lib/wal/sources/System/System.hpp +++ b/lib/wal/sources/System/System.hpp @@ -8,11 +8,10 @@ #include #include #include +#include "Entity/Entity.hpp" namespace WAL { - class Entity; - //! @brief A base system of WAL class System { diff --git a/lib/wal/sources/View/BaseView.hpp b/lib/wal/sources/View/BaseView.hpp new file mode 100644 index 00000000..4a8502d4 --- /dev/null +++ b/lib/wal/sources/View/BaseView.hpp @@ -0,0 +1,50 @@ +// +// Created by Zoe Roux on 2021-06-03. +// + + +#pragma once + +#include +#include +#include +#include +#include "Entity/Entity.hpp" + +namespace WAL +{ + class BaseView + { + public: + std::vector> entities = {}; + + std::vector types = {}; + + size_t size() + { + return entities.size(); + } + +// std::vector> &view(const Components &...index) requires(std::is_same_v) +// { +// static std::vector> view; +// +// std::copy_if(this->_entities.begin(), this->_entities.end(), std::back_inserter(view), [&index...](Entity &entity) { +// return (entity.hasComponent(index) && ...); +// }); +// return view; +// } + }; + + template + class View : public BaseView + { + public: + explicit View(std::vector &scene) + { + std::copy_if(scene.begin(), scene.end(), std::back_inserter(this->entities), [](Entity &entity) { + return (entity.hasComponent() && ...); + }); + } + }; +} \ No newline at end of file diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index a16e4f5c..7e361904 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -12,6 +12,7 @@ #include "Exception/WalError.hpp" #include "System/System.hpp" #include "Models/Callback.hpp" +#include "Scene/Scene.hpp" namespace WAL { diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 7e03bfef..c6270366 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -66,7 +66,7 @@ namespace BBM std::shared_ptr loadGameScene(WAL::Wal &wal) { - auto scene = std::make_shared(wal); + auto scene = std::make_shared(); scene->addEntity("player") .addComponent() .addComponent>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index 24971304..6bb58d51 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -6,6 +6,7 @@ #include "Component/Position/PositionComponent.hpp" #include "Component/Collision/CollisionComponent.hpp" #include "System/Collision/CollisionSystem.hpp" +#include "Scene/Scene.hpp" namespace BBM { diff --git a/tests/CallbackTest.cpp b/tests/CallbackTest.cpp index 44b3a103..30d67e76 100644 --- a/tests/CallbackTest.cpp +++ b/tests/CallbackTest.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "Entity/Entity.hpp" #include "Models/Callback.hpp" @@ -37,7 +38,7 @@ TEST_CASE("Callback multiple arguments", "[Callback]") callback.addCallback([](const std::string& str, int a, unsigned *value, Entity &entity) { throw std::runtime_error(""); }); - Wal wal; - Entity entity(wal, "name"); + Scene scene; + Entity entity(scene, "name"); REQUIRE_THROWS_AS(callback("1", 0, nullptr, entity), std::runtime_error); } diff --git a/tests/CollisionTest.cpp b/tests/CollisionTest.cpp index 42b7d186..447a8dbb 100644 --- a/tests/CollisionTest.cpp +++ b/tests/CollisionTest.cpp @@ -21,7 +21,7 @@ TEST_CASE("Collision test", "[Component][System]") { Wal wal; CollisionSystem collision(wal); - wal.scene = std::make_shared(wal); + wal.scene = std::make_shared(); wal.scene->addEntity("player") .addComponent() .addComponent([](Entity &actual, const Entity &) { @@ -65,7 +65,7 @@ TEST_CASE("Collsion test with movable", "[Component][System]") Wal wal; CollisionSystem collision(wal); MovableSystem movable; - wal.scene = std::make_shared(wal); + wal.scene = std::make_shared(); wal.scene->addEntity("player") .addComponent() .addComponent([](Entity &actual, const Entity &) {}, [](Entity &actual, const Entity &) {}, 5.0) diff --git a/tests/EntityTests.cpp b/tests/EntityTests.cpp index 7a0a3490..3520f862 100644 --- a/tests/EntityTests.cpp +++ b/tests/EntityTests.cpp @@ -6,14 +6,15 @@ #include "Component/Position/PositionComponent.hpp" #include #include +#include using namespace WAL; using namespace BBM; TEST_CASE("Component", "[Entity]") { - Wal wal; - Entity entity(wal, "Bob"); + Scene scene; + Entity entity(scene, "Bob"); entity.addComponent(2, 3, 4); SECTION("Check value") { @@ -33,15 +34,15 @@ TEST_CASE("Component", "[Entity]") TEST_CASE("ComponentNotFound", "[Entity]") { - Wal wal; - Entity entity(wal, "Bob"); + Scene scene; + Entity entity(scene, "Bob"); REQUIRE_THROWS_AS(entity.getComponent(), NotFoundError); } TEST_CASE("Add component by reference", "[Entity]") { - Wal wal; - Entity entity(wal, "Bob"); + Scene scene; + Entity entity(scene, "Bob"); PositionComponent component(entity, 4, 5, 6); REQUIRE(&entity.addComponent(component) == &entity); diff --git a/tests/MoveTests.cpp b/tests/MoveTests.cpp index 0a47bd10..cd37d22a 100644 --- a/tests/MoveTests.cpp +++ b/tests/MoveTests.cpp @@ -19,8 +19,7 @@ using namespace BBM; TEST_CASE("Move test", "[Component][System]") { - Wal wal; - Scene scene(wal); + Scene scene; scene.addEntity("player") .addComponent() .addComponent() diff --git a/tests/ViewTest.cpp b/tests/ViewTest.cpp index 90328acd..21315f2b 100644 --- a/tests/ViewTest.cpp +++ b/tests/ViewTest.cpp @@ -18,8 +18,7 @@ using namespace BBM; TEST_CASE("View creation", "[View]") { - Wal wal; - Scene scene(wal); + Scene scene; scene.addEntity("player") .addComponent() .addComponent(); @@ -28,6 +27,6 @@ TEST_CASE("View creation", "[View]") REQUIRE(scene.view().size() == 2); REQUIRE(scene.view().size() == 1); Entity &entity = *scene.getEntities().begin(); - Entity &firstView = *scene.view().begin(); + Entity &firstView = *scene.view().entities.begin(); REQUIRE(&entity == &firstView); } \ No newline at end of file From 4f14d380bff2031d4766354e284a33017e774485 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Thu, 3 Jun 2021 23:13:22 +0200 Subject: [PATCH 47/60] Adding some tests --- lib/wal/CMakeLists.txt | 2 +- lib/wal/sources/Scene/Scene.hpp | 2 +- lib/wal/sources/View/BaseView.hpp | 50 ------------------------ lib/wal/sources/View/View.hpp | 65 +++++++++++++++++++++++++++++++ tests/ViewTest.cpp | 48 ++++++++++++++++++++--- 5 files changed, 109 insertions(+), 58 deletions(-) delete mode 100644 lib/wal/sources/View/BaseView.hpp create mode 100644 lib/wal/sources/View/View.hpp diff --git a/lib/wal/CMakeLists.txt b/lib/wal/CMakeLists.txt index 088188b4..ba2a60a0 100644 --- a/lib/wal/CMakeLists.txt +++ b/lib/wal/CMakeLists.txt @@ -17,6 +17,6 @@ add_library(wal sources/Component/Component.cpp sources/System/System.cpp sources/Models/Callback.hpp - sources/View/BaseView.hpp) + sources/View/View.hpp) target_include_directories(wal PUBLIC sources) \ No newline at end of file diff --git a/lib/wal/sources/Scene/Scene.hpp b/lib/wal/sources/Scene/Scene.hpp index e233debc..c3b000b6 100644 --- a/lib/wal/sources/Scene/Scene.hpp +++ b/lib/wal/sources/Scene/Scene.hpp @@ -7,7 +7,7 @@ #include #include -#include +#include #include "Entity/Entity.hpp" namespace WAL diff --git a/lib/wal/sources/View/BaseView.hpp b/lib/wal/sources/View/BaseView.hpp deleted file mode 100644 index 4a8502d4..00000000 --- a/lib/wal/sources/View/BaseView.hpp +++ /dev/null @@ -1,50 +0,0 @@ -// -// Created by Zoe Roux on 2021-06-03. -// - - -#pragma once - -#include -#include -#include -#include -#include "Entity/Entity.hpp" - -namespace WAL -{ - class BaseView - { - public: - std::vector> entities = {}; - - std::vector types = {}; - - size_t size() - { - return entities.size(); - } - -// std::vector> &view(const Components &...index) requires(std::is_same_v) -// { -// static std::vector> view; -// -// std::copy_if(this->_entities.begin(), this->_entities.end(), std::back_inserter(view), [&index...](Entity &entity) { -// return (entity.hasComponent(index) && ...); -// }); -// return view; -// } - }; - - template - class View : public BaseView - { - public: - explicit View(std::vector &scene) - { - std::copy_if(scene.begin(), scene.end(), std::back_inserter(this->entities), [](Entity &entity) { - return (entity.hasComponent() && ...); - }); - } - }; -} \ No newline at end of file diff --git a/lib/wal/sources/View/View.hpp b/lib/wal/sources/View/View.hpp new file mode 100644 index 00000000..9df63f55 --- /dev/null +++ b/lib/wal/sources/View/View.hpp @@ -0,0 +1,65 @@ +// +// Created by Zoe Roux on 2021-06-03. +// + + +#pragma once + +#include +#include +#include +#include +#include "Entity/Entity.hpp" + +namespace WAL +{ + //! @brief A basic view used to manipulate view without knowing their type at compile time. + class BaseView + { + public: + //! @brief The list of entities in the view. + std::vector> entities = {}; + + //! @brief The list of types that every entity of the view has. + std::vector types = {}; + + size_t size() + { + return entities.size(); + } + + //! @brief A default destructor + ~BaseView() = default; + protected: + //! @brief A basic view can't be constructed, you should use the View templated class. + BaseView() = default; + //! @brief A basic view can't be constructed, you should use the View templated class. + BaseView(const BaseView &) = default; + //! @brief A basic view can't be assigned. See the View template class. + BaseView &operator=(const BaseView &) = default; + }; + + //! @brief A view allowing one to easily access entities containing a set list of component. + //! A view is always updated and only references to entities are kept. + template + class View : public BaseView + { + public: + //! @brief Construct a view from a list of entities. + //! Those entities are never copied but references to them are kept internally. + explicit View(std::vector &scene) + { + this->types = {typeid(Components)...}; + std::copy_if(scene.begin(), scene.end(), std::back_inserter(this->entities), [](Entity &entity) { + return (entity.hasComponent() && ...); + }); + } + + //! @brief Copying a view is not possible since a view must be managed by a scene. + View(const View &) = delete; + //! @brief A default destructor + ~View() = default; + //! @brief A view is not assignable. + View &operator=(const View &) = delete; + }; +} \ No newline at end of file diff --git a/tests/ViewTest.cpp b/tests/ViewTest.cpp index 21315f2b..8e0c1aef 100644 --- a/tests/ViewTest.cpp +++ b/tests/ViewTest.cpp @@ -4,15 +4,10 @@ #include "Entity/Entity.hpp" #include "Component/Position/PositionComponent.hpp" -#include "System/Movable/MovableSystem.hpp" -#include "System/Controllable/ControllableSystem.hpp" #include #include #include -#define private public -#include - using namespace WAL; using namespace BBM; @@ -29,4 +24,45 @@ TEST_CASE("View creation", "[View]") Entity &entity = *scene.getEntities().begin(); Entity &firstView = *scene.view().entities.begin(); REQUIRE(&entity == &firstView); -} \ No newline at end of file +} + +TEST_CASE("View update", "[View]") +{ + Scene scene; + scene.addEntity("player") + .addComponent() + .addComponent(); + auto &view = scene.view(); + auto &entity = scene.addEntity("Box") + .addComponent(); + REQUIRE(view.size() == 2); + entity.removeComponent(); + REQUIRE(view.size() == 1); +} + +TEST_CASE("View cache", "[View]") +{ + Scene scene; + scene.addEntity("player") + .addComponent() + .addComponent(); + auto &view = scene.view(); + REQUIRE(&view == &scene.view()); +} + +//TEST_CASE("View iteration", "[View]") +//{ +// Scene scene; +// scene.addEntity("player") +// .addComponent() +// .addComponent(); +// scene.addEntity("Box") +// .addComponent(); +// int i = 0; +// for (auto &entity : scene.view()) { +// if (i == 0) +// REQUIRE(entity.getName() == "player"); +// else +// REQUIRE(entity.getName() == "Box"); +// } +//} \ No newline at end of file From 306f07972da2466130dd13e6639fe5e783be1cf7 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Fri, 4 Jun 2021 00:50:38 +0200 Subject: [PATCH 48/60] Reworking systems --- lib/wal/CMakeLists.txt | 4 +- lib/wal/sources/System/ISystem.hpp | 37 ++++++++++++++ lib/wal/sources/System/System.cpp | 28 ----------- lib/wal/sources/System/System.hpp | 34 ++++++++----- lib/wal/sources/View/View.hpp | 2 +- lib/wal/sources/Wal.cpp | 49 ------------------- lib/wal/sources/Wal.hpp | 32 ++++++------ sources/System/Collision/CollisionSystem.cpp | 6 +-- sources/System/Collision/CollisionSystem.hpp | 11 ++--- .../Controllable/ControllableSystem.cpp | 7 +-- .../Controllable/ControllableSystem.hpp | 10 ++-- sources/System/Gamepad/GamepadSystem.cpp | 7 +-- sources/System/Gamepad/GamepadSystem.hpp | 10 ++-- .../GridCentered/GridCenteredSystem.cpp | 8 +-- .../GridCentered/GridCenteredSystem.hpp | 9 ++-- sources/System/Health/HealthSystem.cpp | 6 +-- sources/System/Health/HealthSystem.hpp | 9 ++-- sources/System/Keyboard/KeyboardSystem.cpp | 7 +-- sources/System/Keyboard/KeyboardSystem.hpp | 10 ++-- sources/System/Movable/MovableSystem.cpp | 7 +-- sources/System/Movable/MovableSystem.hpp | 9 ++-- tests/CollisionTest.cpp | 4 +- 22 files changed, 129 insertions(+), 177 deletions(-) create mode 100644 lib/wal/sources/System/ISystem.hpp delete mode 100644 lib/wal/sources/System/System.cpp delete mode 100644 lib/wal/sources/Wal.cpp diff --git a/lib/wal/CMakeLists.txt b/lib/wal/CMakeLists.txt index ba2a60a0..c0aac922 100644 --- a/lib/wal/CMakeLists.txt +++ b/lib/wal/CMakeLists.txt @@ -7,7 +7,6 @@ add_library(wal sources/Entity/Entity.hpp sources/Component/Component.hpp sources/System/System.hpp - sources/Wal.cpp sources/Wal.hpp sources/Scene/Scene.cpp sources/Scene/Scene.hpp @@ -15,8 +14,7 @@ add_library(wal sources/Exception/WalError.hpp sources/Entity/Entity.cpp sources/Component/Component.cpp - sources/System/System.cpp sources/Models/Callback.hpp - sources/View/View.hpp) + sources/View/View.hpp sources/System/ISystem.hpp) target_include_directories(wal PUBLIC sources) \ No newline at end of file diff --git a/lib/wal/sources/System/ISystem.hpp b/lib/wal/sources/System/ISystem.hpp new file mode 100644 index 00000000..be755d65 --- /dev/null +++ b/lib/wal/sources/System/ISystem.hpp @@ -0,0 +1,37 @@ +// +// Created by Zoe Roux on 2021-06-04. +// + + +#pragma once + +#include "Entity/Entity.hpp" +#include "View/View.hpp" +#include + +namespace WAL +{ + //! @brief A base class that represent a system. + class ISystem + { + public: + //! @brief Update the corresponding component of the given entity + //! @param entity The entity to update. + //! @param dtime The delta time. + virtual void onUpdate(Entity &entity, std::chrono::nanoseconds dtime) = 0; + + //! @brief An alternative of onUpdate that is called every 8ms (120 times per seconds). If the system slow down, it will try to catch up. + //! @remark This should be used for Physics, AI and everything that could be imprecise due to float rounding. + //! @param entity The entity to update. + virtual void onFixedUpdate(Entity &entity) = 0; + + //! @brief A method called after all entities that this system manage has been updated. + virtual void onSelfUpdate() = 0; + + //! @brief Get a view containing every entity this system should update. + virtual BaseView &getView() = 0; + + //! @brief A virtual default destructor. + virtual ~ISystem() = default; + }; +} \ No newline at end of file diff --git a/lib/wal/sources/System/System.cpp b/lib/wal/sources/System/System.cpp deleted file mode 100644 index 0dd69f30..00000000 --- a/lib/wal/sources/System/System.cpp +++ /dev/null @@ -1,28 +0,0 @@ -// -// Created by Zoe Roux on 5/17/21. -// - -#include "System.hpp" -#include -#include - -namespace WAL -{ - System::System(std::vector dependencies) - : _dependencies(std::move(dependencies)) - {} - - void System::onUpdate(Entity &entity, std::chrono::nanoseconds dtime) - {} - - void System::onFixedUpdate(Entity &entity) - {} - - void System::onSelfUpdate() - {} - - const std::vector &System::getDependencies() const - { - return this->_dependencies; - } -} // namespace WAL \ No newline at end of file diff --git a/lib/wal/sources/System/System.hpp b/lib/wal/sources/System/System.hpp index 8dc164a7..56a5404d 100644 --- a/lib/wal/sources/System/System.hpp +++ b/lib/wal/sources/System/System.hpp @@ -9,39 +9,49 @@ #include #include #include "Entity/Entity.hpp" +#include "Wal.hpp" +#include "View/View.hpp" +#include "ISystem.hpp" namespace WAL { //! @brief A base system of WAL - class System + //! @tparam Dependencies The list of dependencies this system has. + template + class System : public ISystem { - private: - //! @brief The list of dependencies of this system - std::vector _dependencies = {}; public: //! @brief A virtual, default, destructor - virtual ~System() = default; + ~System() override = default; //! @brief A system can be moved - System(System &&) = default; + System(System &&) noexcept = default; - //! @brief Get the name of the component corresponding to this system. - const std::vector &getDependencies() const; + //! @brief Get a view of all entities containing every dependencies of this system. + View &getView() override + { + return this->_wal.scene->template view(); + } //! @brief Update the corresponding component of the given entity //! @param entity The entity to update. //! @param dtime The delta time. - virtual void onUpdate(Entity &entity, std::chrono::nanoseconds dtime); + void onUpdate(Entity &entity, std::chrono::nanoseconds dtime) override {} //! @brief An alternative of onUpdate that is called every 8ms (120 times per seconds). If the system slow down, it will try to catch up. //! @remark This should be used for Physics, AI and everything that could be imprecise due to float rounding. //! @param entity The entity to update. - virtual void onFixedUpdate(Entity &entity); + void onFixedUpdate(Entity &entity) override {} //! @brief A method called after all entities that this system manage has been updated. - virtual void onSelfUpdate(); + void onSelfUpdate() override {} protected: + //! @brief A reference to the ECS. + Wal &_wal; + //! @brief A system can't be instantiated, it should be derived. - explicit System(std::vector dependencies); + explicit System(Wal &wal) + : _wal(wal) + {} //! @brief A system can't be instantiated, it should be derived. System(const System &) = default; //! @brief A system can't be instantiated, it should be derived. diff --git a/lib/wal/sources/View/View.hpp b/lib/wal/sources/View/View.hpp index 9df63f55..2db9e2ff 100644 --- a/lib/wal/sources/View/View.hpp +++ b/lib/wal/sources/View/View.hpp @@ -23,7 +23,7 @@ namespace WAL //! @brief The list of types that every entity of the view has. std::vector types = {}; - size_t size() + size_t size() const { return entities.size(); } diff --git a/lib/wal/sources/Wal.cpp b/lib/wal/sources/Wal.cpp deleted file mode 100644 index d4262b73..00000000 --- a/lib/wal/sources/Wal.cpp +++ /dev/null @@ -1,49 +0,0 @@ -// -// Created by Zoe Roux on 2021-05-14. -// - -#include -#include -#include "Wal.hpp" -#include "Scene/Scene.hpp" - -namespace WAL -{ - std::chrono::nanoseconds Wal::timestep = std::chrono::milliseconds(8); - - void Wal::_update(std::chrono::nanoseconds dtime) - { - auto &entities = this->scene->getEntities(); - - for (auto &system : this->_systems) { - for (auto &entity : entities) { - if (!Wal::_hasDependencies(entity, *system)) - continue; - system->onUpdate(entity, dtime); - } - system->onSelfUpdate(); - } - } - - void Wal::_fixedUpdate() - { - auto &entities = this->scene->getEntities(); - - for (auto &system : this->_systems) { - for (auto &entity : entities) { - if (!Wal::_hasDependencies(entity, *system)) - continue; - system->onFixedUpdate(entity); - } - } - } - - bool Wal::_hasDependencies(const Entity &entity, const System &system) - { - // TODO use an hashmap to cache results. - const auto &dependency = system.getDependencies(); - return std::ranges::all_of(dependency.begin(), dependency.end(), [&entity](const auto &dependency) { - return entity.hasComponent(dependency); - }); - } -} // namespace WAL \ No newline at end of file diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index 7e361904..4bb9500a 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -10,7 +10,7 @@ #include #include #include "Exception/WalError.hpp" -#include "System/System.hpp" +#include "System/ISystem.hpp" #include "Models/Callback.hpp" #include "Scene/Scene.hpp" @@ -19,31 +19,22 @@ namespace WAL class Entity; class Scene; + template + class System; + //! @brief The main WAL class, it is used to setup and run the ECS. class Wal { private: //! @brief The list of registered systems - std::vector> _systems = {}; - - //! @brief Call the onUpdate of every system with every component - void _update(std::chrono::nanoseconds dtime); - - //! @brief Call the onFixedUpdate of every system with every component - void _fixedUpdate(); - - //! @brief Check if an entity met a system's dependencies. - //! @param entity The entity to check - //! @param system The system that will list dependencies - //! @return True if all dependencies are met, false otherwise. - static bool _hasDependencies(const Entity &entity, const System &system); + std::vector> _systems = {}; public: //! @brief The scene that contains entities. std::shared_ptr scene; //! @brief True if the engine should close after the end of the current tick. bool shouldClose = false; //! @brief The time between each fixed update. - static std::chrono::nanoseconds timestep; + static constexpr std::chrono::nanoseconds timestep = std::chrono::milliseconds(8); //! @brief Create a new system in place. //! @return The wal instance used to call this function is returned. This allow method chaining. @@ -132,9 +123,16 @@ namespace WAL while (fBehind > Wal::timestep) { fBehind -= Wal::timestep; - this->_fixedUpdate(); + for (auto &system : this->_systems) { + for (auto &entity : system->getView().entities) + system->onFixedUpdate(entity); + } + } + for (auto &system : this->_systems) { + for (auto &entity : system->getView().entities) + system->onUpdate(entity, dtime); + system->onSelfUpdate(); } - this->_update(dtime); callback(*this, state); } } diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index 6bb58d51..7561b984 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -11,10 +11,8 @@ namespace BBM { CollisionSystem::CollisionSystem(WAL::Wal &wal) - : WAL::System({typeid(PositionComponent), typeid(CollisionComponent)}), _wal(wal) - { - - } + : System(wal) + { } bool CollisionSystem::collide(Vector3f minA, Vector3f maxA, Vector3f minB, Vector3f maxB) { diff --git a/sources/System/Collision/CollisionSystem.hpp b/sources/System/Collision/CollisionSystem.hpp index c4c2de25..4e3961d9 100644 --- a/sources/System/Collision/CollisionSystem.hpp +++ b/sources/System/Collision/CollisionSystem.hpp @@ -12,23 +12,20 @@ namespace BBM { //! @brief A system to handle collisions. - class CollisionSystem : public WAL::System + class CollisionSystem : public WAL::System { - private: - //! @brief reference to the ECS engine to get other entities - WAL::Wal &_wal; public: //! @inherit void onFixedUpdate(WAL::Entity &entity) override; //! @brief A default constructor - CollisionSystem(WAL::Wal &wal); + explicit CollisionSystem(WAL::Wal &wal); //! @brief A Collision system is copy constructable CollisionSystem(const CollisionSystem &) = default; //! @brief A default destructor ~CollisionSystem() override = default; - //! @brief A Collision system is assignable. - CollisionSystem &operator=(const CollisionSystem &) = default; + //! @brief A system is not assignable. + CollisionSystem &operator=(const CollisionSystem &) = delete; //! @brief check AABB collision static bool collide(Vector3f minA, Vector3f maxA, Vector3f minB, Vector3f maxB); diff --git a/sources/System/Controllable/ControllableSystem.cpp b/sources/System/Controllable/ControllableSystem.cpp index 0a9c789c..941319cf 100644 --- a/sources/System/Controllable/ControllableSystem.cpp +++ b/sources/System/Controllable/ControllableSystem.cpp @@ -12,11 +12,8 @@ namespace BBM { float ControllableSystem::speed = .25f; - ControllableSystem::ControllableSystem() - : WAL::System({ - typeid(ControllableComponent), - typeid(MovableComponent) - }) + ControllableSystem::ControllableSystem(WAL::Wal &wal) + : System(wal) {} void ControllableSystem::onFixedUpdate(WAL::Entity &entity) diff --git a/sources/System/Controllable/ControllableSystem.hpp b/sources/System/Controllable/ControllableSystem.hpp index fac4db08..45e7513a 100644 --- a/sources/System/Controllable/ControllableSystem.hpp +++ b/sources/System/Controllable/ControllableSystem.hpp @@ -5,12 +5,14 @@ #pragma once +#include "Component/Movable/MovableComponent.hpp" +#include "Component/Controllable/ControllableComponent.hpp" #include "System/System.hpp" namespace BBM { //! @brief A system to handle Controllable entities. - class ControllableSystem : public WAL::System + class ControllableSystem : public WAL::System { public: //! @brief The speed applied to every controllable entities. @@ -20,12 +22,12 @@ namespace BBM void onFixedUpdate(WAL::Entity &entity) override; //! @brief A default constructor - ControllableSystem(); + explicit ControllableSystem(WAL::Wal &wal); //! @brief A Controllable system is copy constructable ControllableSystem(const ControllableSystem &) = default; //! @brief A default destructor ~ControllableSystem() override = default; - //! @brief A Controllable system is assignable. - ControllableSystem &operator=(const ControllableSystem &) = default; + //! @brief A system is not assignable. + ControllableSystem &operator=(const ControllableSystem &) = delete; }; } diff --git a/sources/System/Gamepad/GamepadSystem.cpp b/sources/System/Gamepad/GamepadSystem.cpp index 5df6bd96..9392dc02 100644 --- a/sources/System/Gamepad/GamepadSystem.cpp +++ b/sources/System/Gamepad/GamepadSystem.cpp @@ -13,11 +13,8 @@ using Gamepad = RAY::Controller::GamePad; namespace BBM { - GamepadSystem::GamepadSystem() - : WAL::System({ - typeid(GamepadComponent), - typeid(ControllableComponent) - }) + GamepadSystem::GamepadSystem(WAL::Wal &wal) + : System(wal) {} void GamepadSystem::onFixedUpdate(WAL::Entity &entity) diff --git a/sources/System/Gamepad/GamepadSystem.hpp b/sources/System/Gamepad/GamepadSystem.hpp index 9c8c705f..8daa5e06 100644 --- a/sources/System/Gamepad/GamepadSystem.hpp +++ b/sources/System/Gamepad/GamepadSystem.hpp @@ -6,23 +6,25 @@ #include "System/System.hpp" #include +#include "Component/Gamepad/GamepadComponent.hpp" +#include "Component/Controllable/ControllableComponent.hpp" namespace BBM { //! @brief A system to handle Gamepad entities. - class GamepadSystem : public WAL::System + class GamepadSystem : public WAL::System { public: //! @inherit void onFixedUpdate(WAL::Entity &entity) override; //! @brief A default constructor - GamepadSystem(); + explicit GamepadSystem(WAL::Wal &wal); //! @brief A Gamepad system is copy constructable GamepadSystem(const GamepadSystem &) = default; //! @brief A default destructor ~GamepadSystem() override = default; - //! @brief A Gamepad system is assignable. - GamepadSystem &operator=(const GamepadSystem &) = default; + //! @brief A system is not assignable. + GamepadSystem &operator=(const GamepadSystem &) = delete; }; } diff --git a/sources/System/GridCentered/GridCenteredSystem.cpp b/sources/System/GridCentered/GridCenteredSystem.cpp index 5dcca1a9..d6337c3a 100644 --- a/sources/System/GridCentered/GridCenteredSystem.cpp +++ b/sources/System/GridCentered/GridCenteredSystem.cpp @@ -8,12 +8,8 @@ namespace BBM { - GridCenteredSystem::GridCenteredSystem() - : WAL::System({ - typeid(GridCenteredComponent), - typeid(MovableComponent), -// typeid(PositionComponent) - }) + GridCenteredSystem::GridCenteredSystem(WAL::Wal &wal) + : System(wal) {} void GridCenteredSystem::onFixedUpdate(WAL::Entity &entity) diff --git a/sources/System/GridCentered/GridCenteredSystem.hpp b/sources/System/GridCentered/GridCenteredSystem.hpp index e84e65fb..3af49ea1 100644 --- a/sources/System/GridCentered/GridCenteredSystem.hpp +++ b/sources/System/GridCentered/GridCenteredSystem.hpp @@ -5,22 +5,23 @@ #pragma once #include +#include "Component/Position/PositionComponent.hpp" namespace BBM { //! @brief The system handling GridCenteredComponent - class GridCenteredSystem : public WAL::System + class GridCenteredSystem : public WAL::System { public: void onFixedUpdate(WAL::Entity &entity) override; //! @brief A default constructor - GridCenteredSystem(); + explicit GridCenteredSystem(WAL::Wal &wal); //! @brief A GridCenteredSystem is copyable. GridCenteredSystem(const GridCenteredSystem &) = default; //! @brief A default destructor ~GridCenteredSystem() override = default; - //! @brief A GridCenteredSystem is assignable - GridCenteredSystem &operator=(const GridCenteredSystem &) = default; + //! @brief A system is not assignable + GridCenteredSystem &operator=(const GridCenteredSystem &) = delete; }; } diff --git a/sources/System/Health/HealthSystem.cpp b/sources/System/Health/HealthSystem.cpp index 90bbfbb9..e9aaa155 100644 --- a/sources/System/Health/HealthSystem.cpp +++ b/sources/System/Health/HealthSystem.cpp @@ -10,10 +10,8 @@ namespace BBM { - HealthSystem::HealthSystem() - : WAL::System({ - typeid(HealthComponent) - }) + HealthSystem::HealthSystem(WAL::Wal &wal) + : System(wal) {} void HealthSystem::onFixedUpdate(WAL::Entity &entity) diff --git a/sources/System/Health/HealthSystem.hpp b/sources/System/Health/HealthSystem.hpp index 15ca2062..fe8bb1ab 100644 --- a/sources/System/Health/HealthSystem.hpp +++ b/sources/System/Health/HealthSystem.hpp @@ -5,24 +5,25 @@ #pragma once +#include "Component/Health/HealthComponent.hpp" #include "System/System.hpp" namespace BBM { //! @brief A system to handle Health entities. - class HealthSystem : public WAL::System + class HealthSystem : public WAL::System { public: //! @inherit void onFixedUpdate(WAL::Entity &entity) override; //! @brief A default constructor - HealthSystem(); + explicit HealthSystem(WAL::Wal &wal); //! @brief A Health system is copy constructable HealthSystem(const HealthSystem &) = default; //! @brief A default destructor ~HealthSystem() override = default; - //! @brief A Health system is assignable. - HealthSystem &operator=(const HealthSystem &) = default; + //! @brief A system is not assignable. + HealthSystem &operator=(const HealthSystem &) = delete; }; } diff --git a/sources/System/Keyboard/KeyboardSystem.cpp b/sources/System/Keyboard/KeyboardSystem.cpp index 13ad9804..044373b4 100644 --- a/sources/System/Keyboard/KeyboardSystem.cpp +++ b/sources/System/Keyboard/KeyboardSystem.cpp @@ -14,11 +14,8 @@ using Keyboard = RAY::Controller::Keyboard; namespace BBM { - KeyboardSystem::KeyboardSystem() - : WAL::System({ - typeid(KeyboardComponent), - typeid(ControllableComponent) - }) + KeyboardSystem::KeyboardSystem(WAL::Wal &wal) + : System(wal) {} void KeyboardSystem::onFixedUpdate(WAL::Entity &entity) diff --git a/sources/System/Keyboard/KeyboardSystem.hpp b/sources/System/Keyboard/KeyboardSystem.hpp index f17c5f15..56977386 100644 --- a/sources/System/Keyboard/KeyboardSystem.hpp +++ b/sources/System/Keyboard/KeyboardSystem.hpp @@ -7,23 +7,25 @@ #include "System/System.hpp" #include +#include "Component/Keyboard/KeyboardComponent.hpp" +#include "Component/Controllable/ControllableComponent.hpp" namespace BBM { //! @brief A system to handle keyboard entities. - class KeyboardSystem : public WAL::System + class KeyboardSystem : public WAL::System { public: //! @inherit void onFixedUpdate(WAL::Entity &entity) override; //! @brief A default constructor - KeyboardSystem(); + explicit KeyboardSystem(WAL::Wal &wal); //! @brief A keyboard system is copy constructable KeyboardSystem(const KeyboardSystem &) = default; //! @brief A default destructor ~KeyboardSystem() override = default; - //! @brief A keyboard system is assignable. - KeyboardSystem &operator=(const KeyboardSystem &) = default; + //! @brief A system is not assignable. + KeyboardSystem &operator=(const KeyboardSystem &) = delete; }; } diff --git a/sources/System/Movable/MovableSystem.cpp b/sources/System/Movable/MovableSystem.cpp index 1c439345..c6d86457 100644 --- a/sources/System/Movable/MovableSystem.cpp +++ b/sources/System/Movable/MovableSystem.cpp @@ -8,11 +8,8 @@ namespace BBM { - MovableSystem::MovableSystem() - : WAL::System({ - typeid(MovableComponent), - typeid(PositionComponent) - }) + MovableSystem::MovableSystem(WAL::Wal &wal) + : System(wal) {} void MovableSystem::onFixedUpdate(WAL::Entity &entity) diff --git a/sources/System/Movable/MovableSystem.hpp b/sources/System/Movable/MovableSystem.hpp index 51a56ea9..173bc5a8 100644 --- a/sources/System/Movable/MovableSystem.hpp +++ b/sources/System/Movable/MovableSystem.hpp @@ -5,25 +5,26 @@ #pragma once +#include "Component/Movable/MovableComponent.hpp" #include "System/System.hpp" #include "Entity/Entity.hpp" namespace BBM { //! @brief A system to handle movable entities. This system update velocity based on accelerations and positions based on velocity. - class MovableSystem : public WAL::System + class MovableSystem : public WAL::System { public: //! @inherit void onFixedUpdate(WAL::Entity &entity) override; //! @brief A default constructor - MovableSystem(); + explicit MovableSystem(WAL::Wal &wal); //! @brief A movable system is copy constructable MovableSystem(const MovableSystem &) = default; //! @brief A default destructor ~MovableSystem() override = default; - //! @brief A movable system is assignable. - MovableSystem &operator=(const MovableSystem &) = default; + //! @brief A system is not assignable. + MovableSystem &operator=(const MovableSystem &) = delete; }; } // namespace WAL diff --git a/tests/CollisionTest.cpp b/tests/CollisionTest.cpp index 447a8dbb..f869bb3e 100644 --- a/tests/CollisionTest.cpp +++ b/tests/CollisionTest.cpp @@ -20,7 +20,7 @@ using namespace BBM; TEST_CASE("Collision test", "[Component][System]") { Wal wal; - CollisionSystem collision(wal); + CollisionSystem collision(<#initializer#>, wal); wal.scene = std::make_shared(); wal.scene->addEntity("player") .addComponent() @@ -63,7 +63,7 @@ TEST_CASE("Collision test", "[Component][System]") TEST_CASE("Collsion test with movable", "[Component][System]") { Wal wal; - CollisionSystem collision(wal); + CollisionSystem collision(<#initializer#>, wal); MovableSystem movable; wal.scene = std::make_shared(); wal.scene->addEntity("player") From 0bd22502eefe79ba51dd62aed2569ad08d87e3d1 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Fri, 4 Jun 2021 01:22:55 +0200 Subject: [PATCH 49/60] Cleaning up --- lib/wal/sources/Wal.hpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index d5630c8d..2456296a 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -21,10 +21,6 @@ namespace WAL { class Entity; - class Scene; - - template - class System; //! @brief The main WAL class, it is used to setup and run the ECS. class Wal From 97add1b6d99d1d4108c9463b1a8a6edf668c44a1 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Fri, 4 Jun 2021 12:56:26 +0200 Subject: [PATCH 50/60] Fixing view caches --- lib/wal/sources/Scene/Scene.cpp | 4 +++- lib/wal/sources/Scene/Scene.hpp | 16 +++++++++++++--- lib/wal/sources/View/View.hpp | 4 ++-- tests/CollisionTest.cpp | 6 +++--- tests/MoveTests.cpp | 2 +- tests/ViewTest.cpp | 16 ++++++++++++++++ 6 files changed, 38 insertions(+), 10 deletions(-) diff --git a/lib/wal/sources/Scene/Scene.cpp b/lib/wal/sources/Scene/Scene.cpp index db5c9d6a..fe9fbb19 100644 --- a/lib/wal/sources/Scene/Scene.cpp +++ b/lib/wal/sources/Scene/Scene.cpp @@ -6,7 +6,9 @@ namespace WAL { - std::vector &Scene::getEntities() + int Scene::_nextID = 0; + + std::list &Scene::getEntities() { return this->_entities; } diff --git a/lib/wal/sources/Scene/Scene.hpp b/lib/wal/sources/Scene/Scene.hpp index c3b000b6..be5e160b 100644 --- a/lib/wal/sources/Scene/Scene.hpp +++ b/lib/wal/sources/Scene/Scene.hpp @@ -6,6 +6,7 @@ #pragma once #include +#include #include #include #include "Entity/Entity.hpp" @@ -16,8 +17,12 @@ namespace WAL class Scene { private: + static int _nextID; + //! @brief An ID representing this scene. + int _id = _nextID++; + //! @brief The list of registered entities - std::vector _entities = {}; + std::list _entities = {}; //! @brief The list of cached views to update. std::vector> _views = {}; @@ -31,7 +36,7 @@ namespace WAL void _componentRemoved(const Entity &entity, const std::type_index &type); public: //! @brief Get the list of entities. - std::vector &getEntities(); + std::list &getEntities(); //! @brief Add a new entity to the scene. //! @param name The name of the created entity. @@ -41,8 +46,13 @@ namespace WAL template View &view() { - static auto view = std::make_shared>(this->_entities); + static std::unordered_map>> cache; + auto existing = cache.find(this->_id); + if (existing != cache.end() && !existing->second.expired()) + return *existing->second.lock(); + auto view = std::make_shared>(this->_entities); this->_views.emplace_back(view); + cache.emplace(this->_id, view); return *view; } diff --git a/lib/wal/sources/View/View.hpp b/lib/wal/sources/View/View.hpp index 2db9e2ff..52a46294 100644 --- a/lib/wal/sources/View/View.hpp +++ b/lib/wal/sources/View/View.hpp @@ -5,7 +5,7 @@ #pragma once -#include +#include #include #include #include @@ -47,7 +47,7 @@ namespace WAL public: //! @brief Construct a view from a list of entities. //! Those entities are never copied but references to them are kept internally. - explicit View(std::vector &scene) + explicit View(std::list &scene) { this->types = {typeid(Components)...}; std::copy_if(scene.begin(), scene.end(), std::back_inserter(this->entities), [](Entity &entity) { diff --git a/tests/CollisionTest.cpp b/tests/CollisionTest.cpp index d0ed5f78..77188df1 100644 --- a/tests/CollisionTest.cpp +++ b/tests/CollisionTest.cpp @@ -32,7 +32,7 @@ TEST_CASE("Collision test", "[Component][System]") pos.position.z = 1; } catch (std::exception &e) {}; }, [](Entity &, const Entity &){}, 5.0); - Entity &entity = wal.scene->getEntities()[0]; + Entity &entity = wal.scene->getEntities().front(); REQUIRE(entity.getComponent().position == Vector3f()); entity.getComponent().bound.x = 5; @@ -48,7 +48,7 @@ TEST_CASE("Collision test", "[Component][System]") wal.scene->addEntity("block") .addComponent(2,2,2) .addComponent(1); - Entity &player = wal.scene->getEntities()[0]; + Entity &player = wal.scene->getEntities().front(); collision.onUpdate(entity, std::chrono::nanoseconds(1)); REQUIRE(player.hasComponent(typeid(PositionComponent))); collision.onFixedUpdate(player); @@ -79,7 +79,7 @@ TEST_CASE("Collsion test with movable", "[Component][System]") mov.resetVelocity(); } catch (std::exception &e) {}; }, 1); - Entity &entity = wal.scene->getEntities()[0]; + Entity &entity = wal.scene->getEntities().front(); REQUIRE(entity.getComponent().position == Vector3f()); entity.getComponent().bound.x = 5; diff --git a/tests/MoveTests.cpp b/tests/MoveTests.cpp index ba46a55d..ac035c86 100644 --- a/tests/MoveTests.cpp +++ b/tests/MoveTests.cpp @@ -25,7 +25,7 @@ TEST_CASE("Move test", "[Component][System]") .addComponent() .addComponent() .addComponent(); - Entity &entity = scene.getEntities()[0]; + Entity &entity = *scene.getEntities().begin(); REQUIRE(entity.getComponent().position == Vector3f()); diff --git a/tests/ViewTest.cpp b/tests/ViewTest.cpp index 8e0c1aef..194e04a6 100644 --- a/tests/ViewTest.cpp +++ b/tests/ViewTest.cpp @@ -50,6 +50,22 @@ TEST_CASE("View cache", "[View]") REQUIRE(&view == &scene.view()); } +TEST_CASE("View cache switch", "[View]") +{ + Scene scene; + scene.addEntity("player") + .addComponent() + .addComponent(); + auto &view = scene.view(); + Scene scene2; + scene2.addEntity("box") + .addComponent(); + + REQUIRE(&view == &scene.view()); + REQUIRE(view.entities.begin()->get().getName() == "player"); + REQUIRE(scene2.view().entities.begin()->get().getName() == "box"); +} + //TEST_CASE("View iteration", "[View]") //{ // Scene scene; From 181ccb7934d02082cda3212497b2161215e37b9f Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Fri, 4 Jun 2021 13:02:27 +0200 Subject: [PATCH 51/60] Using views for the collision system --- sources/System/Collision/CollisionSystem.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index 7561b984..de4ef82b 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -32,12 +32,9 @@ namespace BBM position += entity.getComponent().getVelocity(); Vector3f minA = Vector3f::min(position, position + col.bound); Vector3f maxA = Vector3f::max(position, position + col.bound); - for (auto &other : _wal.scene->getEntities()) { + for (WAL::Entity &other : _wal.scene->view().entities) { if (&other == &entity) continue; - if (!other.hasComponent() || - !other.hasComponent()) - continue; auto colB = other.getComponent(); auto posB = other.getComponent().position; Vector3f minB = Vector3f::min(posB, posB + colB.bound); From eac5c2c847a210d1418da44812c76b1825b3b60b Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Fri, 4 Jun 2021 13:16:14 +0200 Subject: [PATCH 52/60] Oups --- sources/System/Collision/CollisionSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index de4ef82b..4c91fc45 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -32,7 +32,7 @@ namespace BBM position += entity.getComponent().getVelocity(); Vector3f minA = Vector3f::min(position, position + col.bound); Vector3f maxA = Vector3f::max(position, position + col.bound); - for (WAL::Entity &other : _wal.scene->view().entities) { + for (WAL::Entity &other : this->getView().entities) { if (&other == &entity) continue; auto colB = other.getComponent(); From a40b61845a7bc94d065aaddcae4608b9312ceaaf Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Fri, 4 Jun 2021 18:59:42 +0200 Subject: [PATCH 53/60] Adding an iterator --- CMakeLists.txt | 36 ++--- lib/wal/sources/Entity/Entity.hpp | 10 ++ lib/wal/sources/Scene/Scene.cpp | 13 +- lib/wal/sources/Scene/Scene.hpp | 2 +- lib/wal/sources/System/ISystem.hpp | 17 +-- lib/wal/sources/System/System.hpp | 24 ++- lib/wal/sources/View/View.hpp | 150 ++++++++++++++++--- lib/wal/sources/Wal.hpp | 13 +- sources/Map/Map.hpp | 1 - sources/Runner/Runner.cpp | 31 ++-- sources/System/Collision/CollisionSystem.cpp | 18 +-- sources/System/Collision/CollisionSystem.hpp | 3 +- 12 files changed, 220 insertions(+), 98 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fd96ef8e..70d6a98e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,34 +35,34 @@ set(SOURCES sources/Component/Keyboard/KeyboardComponent.hpp sources/Component/Health/HealthComponent.cpp sources/Component/Health/HealthComponent.hpp - sources/System/Movable/MovableSystem.hpp - sources/System/Movable/MovableSystem.cpp - sources/System/Controllable/ControllableSystem.cpp - sources/System/Controllable/ControllableSystem.hpp - sources/System/Gamepad/GamepadSystem.cpp - sources/System/Gamepad/GamepadSystem.hpp - sources/System/Health/HealthSystem.cpp - sources/System/Health/HealthSystem.hpp - sources/System/Keyboard/KeyboardSystem.cpp - sources/System/Keyboard/KeyboardSystem.hpp - sources/System/Movable/MovableSystem.cpp - sources/System/Movable/MovableSystem.hpp +# sources/System/Movable/MovableSystem.hpp +# sources/System/Movable/MovableSystem.cpp +# sources/System/Controllable/ControllableSystem.cpp +# sources/System/Controllable/ControllableSystem.hpp +# sources/System/Gamepad/GamepadSystem.cpp +# sources/System/Gamepad/GamepadSystem.hpp +# sources/System/Health/HealthSystem.cpp +# sources/System/Health/HealthSystem.hpp +# sources/System/Keyboard/KeyboardSystem.cpp +# sources/System/Keyboard/KeyboardSystem.hpp +# sources/System/Movable/MovableSystem.cpp +# sources/System/Movable/MovableSystem.hpp sources/Models/Vector3.hpp sources/Component/GridCentered/GridCenteredComponent.cpp sources/Component/GridCentered/GridCenteredComponent.hpp - sources/System/GridCentered/GridCenteredSystem.cpp - sources/System/GridCentered/GridCenteredSystem.hpp +# sources/System/GridCentered/GridCenteredSystem.cpp +# sources/System/GridCentered/GridCenteredSystem.hpp sources/Models/Vector2.hpp sources/Component/Renderer/Drawable2DComponent.hpp sources/Component/Renderer/Drawable3DComponent.hpp - sources/System/Renderer/RenderSystem.hpp - sources/System/Renderer/RenderSystem.cpp +# sources/System/Renderer/RenderSystem.hpp +# sources/System/Renderer/RenderSystem.cpp sources/Component/Renderer/CameraComponent.cpp sources/Component/Renderer/CameraComponent.hpp sources/Component/Animation/AnimationsComponent.cpp sources/Component/Animation/AnimationsComponent.hpp - sources/System/Animation/AnimationsSystem.cpp - sources/System/Animation/AnimationsSystem.hpp +# sources/System/Animation/AnimationsSystem.cpp +# sources/System/Animation/AnimationsSystem.hpp sources/Component/Collision/CollisionComponent.cpp sources/Component/Collision/CollisionComponent.hpp sources/System/Collision/CollisionSystem.hpp diff --git a/lib/wal/sources/Entity/Entity.hpp b/lib/wal/sources/Entity/Entity.hpp index d8948f87..c4a4e9cf 100644 --- a/lib/wal/sources/Entity/Entity.hpp +++ b/lib/wal/sources/Entity/Entity.hpp @@ -65,6 +65,16 @@ namespace WAL return *static_cast(existing->second.get()); } + template + T *getComponentOrDefault() + { + const std::type_index &type = typeid(T); + auto existing = this->_components.find(type); + if (existing == this->_components.end()) + return nullptr; + return *static_cast(existing->second.get()); + } + //! @brief Check if this entity has a component. //! @tparam T The type of the component template diff --git a/lib/wal/sources/Scene/Scene.cpp b/lib/wal/sources/Scene/Scene.cpp index fe9fbb19..22ed03ff 100644 --- a/lib/wal/sources/Scene/Scene.cpp +++ b/lib/wal/sources/Scene/Scene.cpp @@ -26,25 +26,22 @@ namespace WAL void Scene::_componentAdded(Entity &entity, const std::type_index &type) { for (auto &view : this->_views) { - if (std::find(view->types.begin(), view->types.end(), type) == view->types.end()) + if (std::find(view->getTypes().begin(), view->getTypes().end(), type) == view->getTypes().end()) continue; - bool valid = std::all_of(view->types.begin(), view->types.end(), [&entity](const auto &type){ + bool valid = std::all_of(view->getTypes().begin(), view->getTypes().end(), [&entity](const auto &type){ return entity.hasComponent(type); }); if (valid) - view->entities.emplace_back(entity); + view->emplace_back(entity); } } void Scene::_componentRemoved(const Entity &entity, const std::type_index &type) { for (auto &view : this->_views) { - if (std::find(view->types.begin(), view->types.end(), type) == view->types.end()) + if (std::find(view->getTypes().begin(), view->getTypes().end(), type) == view->getTypes().end()) continue; - view->entities.erase(std::remove_if(view->entities.begin(), view->entities.end(), [&entity](const auto &ref) - { - return &ref.get() == &entity; - }), view->entities.end()); + view->erase(entity); } } } // namespace WAL \ No newline at end of file diff --git a/lib/wal/sources/Scene/Scene.hpp b/lib/wal/sources/Scene/Scene.hpp index be5e160b..aab47566 100644 --- a/lib/wal/sources/Scene/Scene.hpp +++ b/lib/wal/sources/Scene/Scene.hpp @@ -24,7 +24,7 @@ namespace WAL //! @brief The list of registered entities std::list _entities = {}; //! @brief The list of cached views to update. - std::vector> _views = {}; + std::vector> _views = {}; //! @brief Notify this scene that a component has been added to the given entity. //! @param entity The entity with the new component diff --git a/lib/wal/sources/System/ISystem.hpp b/lib/wal/sources/System/ISystem.hpp index be755d65..ab8dc3f3 100644 --- a/lib/wal/sources/System/ISystem.hpp +++ b/lib/wal/sources/System/ISystem.hpp @@ -15,21 +15,16 @@ namespace WAL class ISystem { public: - //! @brief Update the corresponding component of the given entity - //! @param entity The entity to update. - //! @param dtime The delta time. - virtual void onUpdate(Entity &entity, std::chrono::nanoseconds dtime) = 0; + //! @brief Update the whole system (every entities that this system is responsible can be updated. + //! @param dtime The delta time since the last call to this method. + virtual void update(std::chrono::nanoseconds dtime) = 0; - //! @brief An alternative of onUpdate that is called every 8ms (120 times per seconds). If the system slow down, it will try to catch up. + //! @brief An alternative of update that is called every 8ms (120 times per seconds). If the system slow down, it will try to catch up. //! @remark This should be used for Physics, AI and everything that could be imprecise due to float rounding. - //! @param entity The entity to update. - virtual void onFixedUpdate(Entity &entity) = 0; - - //! @brief A method called after all entities that this system manage has been updated. - virtual void onSelfUpdate() = 0; + virtual void fixedUpdate() = 0; //! @brief Get a view containing every entity this system should update. - virtual BaseView &getView() = 0; + virtual IView &getView() = 0; //! @brief A virtual default destructor. virtual ~ISystem() = default; diff --git a/lib/wal/sources/System/System.hpp b/lib/wal/sources/System/System.hpp index 56a5404d..c21de9d2 100644 --- a/lib/wal/sources/System/System.hpp +++ b/lib/wal/sources/System/System.hpp @@ -35,15 +35,33 @@ namespace WAL //! @brief Update the corresponding component of the given entity //! @param entity The entity to update. //! @param dtime The delta time. - void onUpdate(Entity &entity, std::chrono::nanoseconds dtime) override {} + virtual void onUpdate(ViewEntity &entity, std::chrono::nanoseconds dtime) {} //! @brief An alternative of onUpdate that is called every 8ms (120 times per seconds). If the system slow down, it will try to catch up. //! @remark This should be used for Physics, AI and everything that could be imprecise due to float rounding. //! @param entity The entity to update. - void onFixedUpdate(Entity &entity) override {} + virtual void onFixedUpdate(ViewEntity &entity) {} //! @brief A method called after all entities that this system manage has been updated. - void onSelfUpdate() override {} + virtual void onSelfUpdate() {} + + + //! @brief Update the whole system (every entities that this system is responsible can be updated. + //! @param dtime The delta time since the last call to this method. + void update(std::chrono::nanoseconds dtime) final + { + for (auto entity : this->getView()) + this->onUpdate(entity, dtime); + this->onSelfUpdate(); + } + + //! @brief An alternative of update that is called every 8ms (120 times per seconds). If the system slow down, it will try to catch up. + //! @remark This should be used for Physics, AI and everything that could be imprecise due to float rounding. + void fixedUpdate() final + { + for (auto entity : this->getView()) + this->onFixedUpdate(entity); + } protected: //! @brief A reference to the ECS. Wal &_wal; diff --git a/lib/wal/sources/View/View.hpp b/lib/wal/sources/View/View.hpp index 52a46294..22c02b60 100644 --- a/lib/wal/sources/View/View.hpp +++ b/lib/wal/sources/View/View.hpp @@ -13,46 +13,152 @@ namespace WAL { - //! @brief A basic view used to manipulate view without knowing their type at compile time. - class BaseView + template + class ViewEntity { + private: + std::tuple, std::reference_wrapper...> _value; public: - //! @brief The list of entities in the view. - std::vector> entities = {}; + explicit ViewEntity(std::tuple, std::reference_wrapper...> value) + : _value(value) + {} - //! @brief The list of types that every entity of the view has. - std::vector types = {}; - - size_t size() const + Entity *operator->() { - return entities.size(); + return &(std::get<0>(this->_value).get()); } + Entity &operator*() + { + return std::get<0>(this->_value); + } + + operator Entity &() + { + return std::get<0>(this->_value); + } + + template + T &get() + { + return std::get>(this->_value); + } + }; + + template + class ViewIterator + { + private: + It _it; + + public: + ViewEntity operator*() + { + ViewEntity entity(*this->_it); + return entity; + } + + ViewEntity operator->() + { + ViewEntity entity(*this->_it); + return entity; + } + + ViewIterator &operator++() + { + this->_it++; + return *this; + } + + ViewIterator operator++(int) + { + ViewIterator copy = *this; + this->_it++; + return *this; + } + + bool operator==(const ViewIterator &other) const + { + return this->_it == other._it; + } + + bool operator!=(const ViewIterator &other) const + { + return !this->operator==(other); + } + + explicit ViewIterator(It current) + : _it(current) + {} + }; + + //! @brief A basic view used to manipulate view without knowing their type at compile time. + class IView + { + public: + //! @brief The list of types that every entity of the view has. + virtual const std::vector &getTypes() const = 0; + + virtual void emplace_back(Entity &) = 0; + + virtual void erase(const Entity &) = 0; + //! @brief A default destructor - ~BaseView() = default; - protected: - //! @brief A basic view can't be constructed, you should use the View templated class. - BaseView() = default; - //! @brief A basic view can't be constructed, you should use the View templated class. - BaseView(const BaseView &) = default; - //! @brief A basic view can't be assigned. See the View template class. - BaseView &operator=(const BaseView &) = default; + virtual ~IView() = default; }; //! @brief A view allowing one to easily access entities containing a set list of component. //! A view is always updated and only references to entities are kept. template - class View : public BaseView + class View : public IView { + private: + using entity_type = std::tuple, std::reference_wrapper...>; + + //! @brief The list of entities in the view. + std::vector _entities = {}; + //! @brief The list of types that every entity of the view has. + std::vector _types = {}; public: + using iterator = ViewIterator::iterator, Components...>; + + iterator begin() + { + return iterator(this->_entities.begin()); + } + + iterator end() + { + return iterator(this->_entities.begin()); + } + + const std::vector &getTypes() const override + { + return this->_types; + } + + void emplace_back(Entity &) override + { + + } + + void erase(const Entity &) override + { + + } + //! @brief Construct a view from a list of entities. //! Those entities are never copied but references to them are kept internally. explicit View(std::list &scene) { - this->types = {typeid(Components)...}; - std::copy_if(scene.begin(), scene.end(), std::back_inserter(this->entities), [](Entity &entity) { - return (entity.hasComponent() && ...); - }); + this->_types = {typeid(Components)...}; + for (auto &entity : scene) { + auto tuple = std::make_tuple(entity, entity.getComponentOrDefault()...); + std::apply(&this->_entities.emplace_back, tuple); + } + // std::copy_if(scene.begin(), scene.end(), std::back_inserter(this->entities), [](Entity &entity) { +// return (entity.hasComponent() && ...); +// }); } //! @brief Copying a view is not possible since a view must be managed by a scene. diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index 2456296a..ef138871 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -129,16 +129,11 @@ namespace WAL while (fBehind > Wal::timestep) { fBehind -= Wal::timestep; - for (auto &system : this->_systems) { - for (auto &entity : system->getView().entities) - system->onFixedUpdate(entity); - } - } - for (auto &system : this->_systems) { - for (auto &entity : system->getView().entities) - system->onUpdate(entity, dtime); - system->onSelfUpdate(); + for (auto &system : this->_systems) + system->fixedUpdate(); } + for (auto &system : this->_systems) + system->update(dtime); callback(*this, state); } } diff --git a/sources/Map/Map.hpp b/sources/Map/Map.hpp index 177d836f..7f396b31 100644 --- a/sources/Map/Map.hpp +++ b/sources/Map/Map.hpp @@ -14,7 +14,6 @@ #include #include #include "Component/Renderer/Drawable3DComponent.hpp" -#include "System/Renderer/RenderSystem.hpp" #include "Scene/Scene.hpp" #include "Model/Model.hpp" #include "Component/Component.hpp" diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index b64a61c1..ff933fff 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -4,30 +4,30 @@ #include #include -#include "System/Movable/MovableSystem.hpp" -#include "System/Renderer/RenderSystem.hpp" +//#include "System/Movable/MovableSystem.hpp" +//#include "System/Renderer/RenderSystem.hpp" #include #include #include -#include +//#include #include -#include -#include +//#include +//#include #include #include #include #include #include -#include -#include "Models/Vector2.hpp" +//#include +//#include "Models/Vector2.hpp" #include "Component/Renderer/CameraComponent.hpp" -#include "Component/Renderer/Drawable2DComponent.hpp" +//#include "Component/Renderer/Drawable2DComponent.hpp" #include "Component/Renderer/Drawable3DComponent.hpp" #include "Runner.hpp" #include "Models/GameState.hpp" #include #include "Component/Animation/AnimationsComponent.hpp" -#include "System/Animation/AnimationsSystem.hpp" +//#include "System/Animation/AnimationsSystem.hpp" #include "Map/Map.hpp" namespace RAY2D = RAY::Drawables::Drawables2D; @@ -47,18 +47,19 @@ namespace BBM void addSystems(WAL::Wal &wal) { - wal.addSystem() - .addSystem() - .addSystem() - .addSystem() - .addSystem(); + wal.addSystem(); + // wal.addSystem() +// .addSystem() +// .addSystem() +// .addSystem() +// .addSystem(); } void enableRaylib(WAL::Wal &wal) { RAY::TraceLog::setLevel(LOG_WARNING); RAY::Window &window = RAY::Window::getInstance(600, 400, "Bomberman", FLAG_WINDOW_RESIZABLE); - wal.addSystem(window); +// wal.addSystem(window); } std::shared_ptr loadGameScene(WAL::Wal &wal) diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index 4c91fc45..79351729 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -23,20 +23,20 @@ namespace BBM return (overlapX && overlapY && overlapZ); } - void CollisionSystem::onFixedUpdate(WAL::Entity &entity) + void CollisionSystem::onFixedUpdate(WAL::ViewEntity &entity) { - auto &posA = entity.getComponent(); - auto &col = entity.getComponent(); + auto &posA = entity.get(); + auto &col = entity.get(); Vector3f position = posA.position; - if (entity.hasComponent(typeid(MovableComponent))) - position += entity.getComponent().getVelocity(); +// if (entity.hasComponent(typeid(MovableComponent))) +// position += entity.getComponent().getVelocity(); Vector3f minA = Vector3f::min(position, position + col.bound); Vector3f maxA = Vector3f::max(position, position + col.bound); - for (WAL::Entity &other : this->getView().entities) { - if (&other == &entity) + for (auto other : this->getView()) { + if (other->getUid() == entity->getUid()) continue; - auto colB = other.getComponent(); - auto posB = other.getComponent().position; + auto colB = other.get(); + auto posB = other.get().position; Vector3f minB = Vector3f::min(posB, posB + colB.bound); Vector3f maxB = Vector3f::max(posB, posB + colB.bound); if (collide(minA, maxA, minB, maxB)) { diff --git a/sources/System/Collision/CollisionSystem.hpp b/sources/System/Collision/CollisionSystem.hpp index 45ee9816..2c0f5491 100644 --- a/sources/System/Collision/CollisionSystem.hpp +++ b/sources/System/Collision/CollisionSystem.hpp @@ -10,6 +10,7 @@ #include "System/System.hpp" #include "Models/Vector3.hpp" #include "Component/Collision/CollisionComponent.hpp" +#include "Component/Position/PositionComponent.hpp" namespace BBM { @@ -18,7 +19,7 @@ namespace BBM { public: //! @inherit - void onFixedUpdate(WAL::Entity &entity) override; + void onFixedUpdate(WAL::ViewEntity &entity) override; //! @brief A default constructor explicit CollisionSystem(WAL::Wal &wal); From acb3935c7c6ee98584d0f4a8a62e9c9b6649bd84 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sat, 5 Jun 2021 16:42:53 +0200 Subject: [PATCH 54/60] Finishing views --- lib/wal/sources/Entity/Entity.hpp | 18 +++++++++++------- lib/wal/sources/View/View.hpp | 13 +++++++------ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/wal/sources/Entity/Entity.hpp b/lib/wal/sources/Entity/Entity.hpp index c4a4e9cf..50770d18 100644 --- a/lib/wal/sources/Entity/Entity.hpp +++ b/lib/wal/sources/Entity/Entity.hpp @@ -54,25 +54,29 @@ namespace WAL void setDisable(bool disabled); //! @brief Get a component of a specific type + //! @tparam The type of the component //! @throw NotFoundError if the component could not be found + //! @return The component of the requested type. template T &getComponent() { - const std::type_index &type = typeid(T); - auto existing = this->_components.find(type); - if (existing == this->_components.end()) - throw NotFoundError("No component could be found with the type \"" + std::string(type.name()) + "\"."); - return *static_cast(existing->second.get()); + T *ret = this->tryGetComponent(); + if (ret == nullptr) + throw NotFoundError("No component could be found with the type \"" + std::string(typeid(T).name()) + "\"."); + return *ret; } + //! @brief Get a component of a specific type or null if not found. + //! @tparam The type of the component + //! @return The component or nullptr if not found. template - T *getComponentOrDefault() + T *tryGetComponent() { const std::type_index &type = typeid(T); auto existing = this->_components.find(type); if (existing == this->_components.end()) return nullptr; - return *static_cast(existing->second.get()); + return static_cast(existing->second.get()); } //! @brief Check if this entity has a component. diff --git a/lib/wal/sources/View/View.hpp b/lib/wal/sources/View/View.hpp index 22c02b60..1c3ba2d9 100644 --- a/lib/wal/sources/View/View.hpp +++ b/lib/wal/sources/View/View.hpp @@ -153,18 +153,19 @@ namespace WAL { this->_types = {typeid(Components)...}; for (auto &entity : scene) { - auto tuple = std::make_tuple(entity, entity.getComponentOrDefault()...); - std::apply(&this->_entities.emplace_back, tuple); + auto tuple = std::make_tuple(entity.tryGetComponent()...); + if (std::apply([](const auto *...component) {return ((component == nullptr) || ...);}, tuple)) + continue; + std::apply([&](auto *...component) { + this->_entities.emplace_back(entity, *component...); + }, tuple); } - // std::copy_if(scene.begin(), scene.end(), std::back_inserter(this->entities), [](Entity &entity) { -// return (entity.hasComponent() && ...); -// }); } //! @brief Copying a view is not possible since a view must be managed by a scene. View(const View &) = delete; //! @brief A default destructor - ~View() = default; + ~View() override = default; //! @brief A view is not assignable. View &operator=(const View &) = delete; }; From f20445cdc824b8e5bdf2de95b5eb2734757bbf94 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sat, 5 Jun 2021 18:42:10 +0200 Subject: [PATCH 55/60] Update systems to the new view --- CMakeLists.txt | 36 ++++---- lib/wal/sources/System/System.hpp | 5 +- lib/wal/sources/View/View.hpp | 86 ++++++++++++++----- sources/Runner/Runner.cpp | 35 ++++---- sources/System/Animation/AnimationsSystem.cpp | 6 +- sources/System/Animation/AnimationsSystem.hpp | 2 +- sources/System/Collision/CollisionSystem.cpp | 4 +- .../Controllable/ControllableSystem.cpp | 6 +- .../Controllable/ControllableSystem.hpp | 2 +- sources/System/Gamepad/GamepadSystem.cpp | 6 +- sources/System/Gamepad/GamepadSystem.hpp | 2 +- .../GridCentered/GridCenteredSystem.cpp | 6 +- .../GridCentered/GridCenteredSystem.hpp | 2 +- sources/System/Health/HealthSystem.cpp | 4 +- sources/System/Health/HealthSystem.hpp | 2 +- sources/System/Keyboard/KeyboardSystem.cpp | 8 +- sources/System/Keyboard/KeyboardSystem.hpp | 2 +- sources/System/Movable/MovableSystem.cpp | 6 +- sources/System/Movable/MovableSystem.hpp | 2 +- sources/System/Renderer/RenderSystem.cpp | 6 +- sources/System/Renderer/RenderSystem.hpp | 2 +- tests/CollisionTest.cpp | 16 ++-- tests/MoveTests.cpp | 18 ++-- tests/ViewTest.cpp | 80 +++++++++++++---- 24 files changed, 213 insertions(+), 131 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 70d6a98e..fd96ef8e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,34 +35,34 @@ set(SOURCES sources/Component/Keyboard/KeyboardComponent.hpp sources/Component/Health/HealthComponent.cpp sources/Component/Health/HealthComponent.hpp -# sources/System/Movable/MovableSystem.hpp -# sources/System/Movable/MovableSystem.cpp -# sources/System/Controllable/ControllableSystem.cpp -# sources/System/Controllable/ControllableSystem.hpp -# sources/System/Gamepad/GamepadSystem.cpp -# sources/System/Gamepad/GamepadSystem.hpp -# sources/System/Health/HealthSystem.cpp -# sources/System/Health/HealthSystem.hpp -# sources/System/Keyboard/KeyboardSystem.cpp -# sources/System/Keyboard/KeyboardSystem.hpp -# sources/System/Movable/MovableSystem.cpp -# sources/System/Movable/MovableSystem.hpp + sources/System/Movable/MovableSystem.hpp + sources/System/Movable/MovableSystem.cpp + sources/System/Controllable/ControllableSystem.cpp + sources/System/Controllable/ControllableSystem.hpp + sources/System/Gamepad/GamepadSystem.cpp + sources/System/Gamepad/GamepadSystem.hpp + sources/System/Health/HealthSystem.cpp + sources/System/Health/HealthSystem.hpp + sources/System/Keyboard/KeyboardSystem.cpp + sources/System/Keyboard/KeyboardSystem.hpp + sources/System/Movable/MovableSystem.cpp + sources/System/Movable/MovableSystem.hpp sources/Models/Vector3.hpp sources/Component/GridCentered/GridCenteredComponent.cpp sources/Component/GridCentered/GridCenteredComponent.hpp -# sources/System/GridCentered/GridCenteredSystem.cpp -# sources/System/GridCentered/GridCenteredSystem.hpp + sources/System/GridCentered/GridCenteredSystem.cpp + sources/System/GridCentered/GridCenteredSystem.hpp sources/Models/Vector2.hpp sources/Component/Renderer/Drawable2DComponent.hpp sources/Component/Renderer/Drawable3DComponent.hpp -# sources/System/Renderer/RenderSystem.hpp -# sources/System/Renderer/RenderSystem.cpp + sources/System/Renderer/RenderSystem.hpp + sources/System/Renderer/RenderSystem.cpp sources/Component/Renderer/CameraComponent.cpp sources/Component/Renderer/CameraComponent.hpp sources/Component/Animation/AnimationsComponent.cpp sources/Component/Animation/AnimationsComponent.hpp -# sources/System/Animation/AnimationsSystem.cpp -# sources/System/Animation/AnimationsSystem.hpp + sources/System/Animation/AnimationsSystem.cpp + sources/System/Animation/AnimationsSystem.hpp sources/Component/Collision/CollisionComponent.cpp sources/Component/Collision/CollisionComponent.hpp sources/System/Collision/CollisionSystem.hpp diff --git a/lib/wal/sources/System/System.hpp b/lib/wal/sources/System/System.hpp index c21de9d2..58f49970 100644 --- a/lib/wal/sources/System/System.hpp +++ b/lib/wal/sources/System/System.hpp @@ -12,6 +12,7 @@ #include "Wal.hpp" #include "View/View.hpp" #include "ISystem.hpp" +#include namespace WAL { @@ -50,7 +51,7 @@ namespace WAL //! @param dtime The delta time since the last call to this method. void update(std::chrono::nanoseconds dtime) final { - for (auto entity : this->getView()) + for (auto &entity : this->getView()) this->onUpdate(entity, dtime); this->onSelfUpdate(); } @@ -59,7 +60,7 @@ namespace WAL //! @remark This should be used for Physics, AI and everything that could be imprecise due to float rounding. void fixedUpdate() final { - for (auto entity : this->getView()) + for (auto &entity : this->getView()) this->onFixedUpdate(entity); } protected: diff --git a/lib/wal/sources/View/View.hpp b/lib/wal/sources/View/View.hpp index 1c3ba2d9..cbee789e 100644 --- a/lib/wal/sources/View/View.hpp +++ b/lib/wal/sources/View/View.hpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include "Entity/Entity.hpp" namespace WAL @@ -43,6 +45,12 @@ namespace WAL { return std::get>(this->_value); } + + template + auto &get() + { + return std::get(this->_value); + } }; template @@ -50,18 +58,19 @@ namespace WAL { private: It _it; + std::optional> _entity; public: - ViewEntity operator*() + ViewEntity &operator*() { - ViewEntity entity(*this->_it); - return entity; + this->_entity.emplace(*this->_it); + return this->_entity.value(); } - ViewEntity operator->() + ViewEntity *operator->() { - ViewEntity entity(*this->_it); - return entity; + this->_entity.emplace(*this->_it); + return &this->_entity; } ViewIterator &operator++() @@ -88,7 +97,8 @@ namespace WAL } explicit ViewIterator(It current) - : _it(current) + : _it(current), + _entity(std::nullopt) {} }; @@ -129,7 +139,22 @@ namespace WAL iterator end() { - return iterator(this->_entities.begin()); + return iterator(this->_entities.end()); + } + + std::size_t size() const + { + return this->_entities.size(); + } + + ViewEntity front() + { + return *iterator(this->_entities.begin()); + } + + ViewEntity back() + { + return *iterator(--this->_entities.end()); } const std::vector &getTypes() const override @@ -137,14 +162,21 @@ namespace WAL return this->_types; } - void emplace_back(Entity &) override + void emplace_back(Entity &entity) override { - + auto tuple = std::make_tuple(entity.tryGetComponent()...); + if (std::apply([](const auto *...component) {return ((component == nullptr) || ...);}, tuple)) + return; + std::apply([&](auto *...component) { + this->_entities.emplace_back(entity, *component...); + }, tuple); } - void erase(const Entity &) override + void erase(const Entity &entity) override { - + this->_entities.erase(std::remove_if(this->_entities.begin(), this->_entities.end(), [&entity](const auto &ref){ + return &std::get<0>(ref).get() == &entity; + })); } //! @brief Construct a view from a list of entities. @@ -152,14 +184,8 @@ namespace WAL explicit View(std::list &scene) { this->_types = {typeid(Components)...}; - for (auto &entity : scene) { - auto tuple = std::make_tuple(entity.tryGetComponent()...); - if (std::apply([](const auto *...component) {return ((component == nullptr) || ...);}, tuple)) - continue; - std::apply([&](auto *...component) { - this->_entities.emplace_back(entity, *component...); - }, tuple); - } + for (auto &entity : scene) + this->emplace_back(entity); } //! @brief Copying a view is not possible since a view must be managed by a scene. @@ -169,4 +195,24 @@ namespace WAL //! @brief A view is not assignable. View &operator=(const View &) = delete; }; +} + +namespace std +{ + template + struct tuple_size<::WAL::ViewEntity> + : public std::integral_constant + {}; + + template + struct tuple_element<0, ::WAL::ViewEntity> + { + using type = WAL::Entity &; + }; + + template + struct tuple_element> + { + using type = typename std::tuple_element>::type; + }; } \ No newline at end of file diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index ff933fff..6dbce849 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -4,33 +4,28 @@ #include #include -//#include "System/Movable/MovableSystem.hpp" -//#include "System/Renderer/RenderSystem.hpp" +#include "System/Movable/MovableSystem.hpp" +#include "System/Renderer/RenderSystem.hpp" #include #include -#include -//#include #include -//#include -//#include +#include +#include #include #include #include #include #include -//#include -//#include "Models/Vector2.hpp" +#include #include "Component/Renderer/CameraComponent.hpp" -//#include "Component/Renderer/Drawable2DComponent.hpp" #include "Component/Renderer/Drawable3DComponent.hpp" #include "Runner.hpp" #include "Models/GameState.hpp" #include #include "Component/Animation/AnimationsComponent.hpp" -//#include "System/Animation/AnimationsSystem.hpp" +#include "System/Animation/AnimationsSystem.hpp" #include "Map/Map.hpp" -namespace RAY2D = RAY::Drawables::Drawables2D; namespace RAY3D = RAY::Drawables::Drawables3D; namespace BBM @@ -47,22 +42,22 @@ namespace BBM void addSystems(WAL::Wal &wal) { - wal.addSystem(); - // wal.addSystem() -// .addSystem() -// .addSystem() -// .addSystem() -// .addSystem(); + wal.addSystem() + .addSystem() + .addSystem() + .addSystem() + .addSystem(); } void enableRaylib(WAL::Wal &wal) { RAY::TraceLog::setLevel(LOG_WARNING); RAY::Window &window = RAY::Window::getInstance(600, 400, "Bomberman", FLAG_WINDOW_RESIZABLE); -// wal.addSystem(window); + wal.addSystem() + .addSystem(window); } - std::shared_ptr loadGameScene(WAL::Wal &wal) + std::shared_ptr loadGameScene() { auto scene = std::make_shared(); scene->addEntity("player") @@ -99,7 +94,7 @@ namespace BBM WAL::Wal wal; addSystems(wal); enableRaylib(wal); - wal.scene = loadGameScene(wal); + wal.scene = loadGameScene(); try { wal.run(updateState); diff --git a/sources/System/Animation/AnimationsSystem.cpp b/sources/System/Animation/AnimationsSystem.cpp index afc22e31..5aaa4597 100644 --- a/sources/System/Animation/AnimationsSystem.cpp +++ b/sources/System/Animation/AnimationsSystem.cpp @@ -15,10 +15,10 @@ namespace BBM : System(wal) {} - void AnimationsSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) + void AnimationsSystem::onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds) { - auto &model = entity.getComponent(); - auto &anim = entity.getComponent(); + auto &model = entity.get(); + auto &anim = entity.get(); if (anim.isDisabled()) return; diff --git a/sources/System/Animation/AnimationsSystem.hpp b/sources/System/Animation/AnimationsSystem.hpp index b17b1935..6ace28d8 100644 --- a/sources/System/Animation/AnimationsSystem.hpp +++ b/sources/System/Animation/AnimationsSystem.hpp @@ -14,7 +14,7 @@ namespace BBM { public: //! @inherit - void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) override; + void onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds) override; //! @brief A default constructor explicit AnimationsSystem(WAL::Wal &wal); diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index 79351729..4e992bda 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -28,8 +28,8 @@ namespace BBM auto &posA = entity.get(); auto &col = entity.get(); Vector3f position = posA.position; -// if (entity.hasComponent(typeid(MovableComponent))) -// position += entity.getComponent().getVelocity(); + if (auto *movable = entity->tryGetComponent()) + position += movable->getVelocity(); Vector3f minA = Vector3f::min(position, position + col.bound); Vector3f maxA = Vector3f::max(position, position + col.bound); for (auto other : this->getView()) { diff --git a/sources/System/Controllable/ControllableSystem.cpp b/sources/System/Controllable/ControllableSystem.cpp index b028a2c7..f14c46b3 100644 --- a/sources/System/Controllable/ControllableSystem.cpp +++ b/sources/System/Controllable/ControllableSystem.cpp @@ -14,10 +14,10 @@ namespace BBM : System(wal) {} - void ControllableSystem::onFixedUpdate(WAL::Entity &entity) + void ControllableSystem::onFixedUpdate(WAL::ViewEntity &entity) { - auto &controllable = entity.getComponent(); - auto &movable = entity.getComponent(); + auto &controllable = entity.get(); + auto &movable = entity.get(); Vector2f move = controllable.move.normalized() * ControllableSystem::speed; movable.addForce(Vector3f(move.x, controllable.jump, move.y)); diff --git a/sources/System/Controllable/ControllableSystem.hpp b/sources/System/Controllable/ControllableSystem.hpp index 042b17df..25a9e837 100644 --- a/sources/System/Controllable/ControllableSystem.hpp +++ b/sources/System/Controllable/ControllableSystem.hpp @@ -19,7 +19,7 @@ namespace BBM static constexpr const float speed = .25f; //! @inherit - void onFixedUpdate(WAL::Entity &entity) override; + void onFixedUpdate(WAL::ViewEntity &entity) override; //! @brief A default constructor explicit ControllableSystem(WAL::Wal &wal); diff --git a/sources/System/Gamepad/GamepadSystem.cpp b/sources/System/Gamepad/GamepadSystem.cpp index 9392dc02..f6d67d99 100644 --- a/sources/System/Gamepad/GamepadSystem.cpp +++ b/sources/System/Gamepad/GamepadSystem.cpp @@ -17,10 +17,10 @@ namespace BBM : System(wal) {} - void GamepadSystem::onFixedUpdate(WAL::Entity &entity) + void GamepadSystem::onFixedUpdate(WAL::ViewEntity &entity) { - const auto &gamepadComponent = entity.getComponent(); - auto &controllable = entity.getComponent(); + const auto &gamepadComponent = entity.get(); + auto &controllable = entity.get(); Gamepad gamepad(gamepadComponent.getID()); const std::map keyPressedMap = { diff --git a/sources/System/Gamepad/GamepadSystem.hpp b/sources/System/Gamepad/GamepadSystem.hpp index 8daa5e06..61efdd5e 100644 --- a/sources/System/Gamepad/GamepadSystem.hpp +++ b/sources/System/Gamepad/GamepadSystem.hpp @@ -16,7 +16,7 @@ namespace BBM { public: //! @inherit - void onFixedUpdate(WAL::Entity &entity) override; + void onFixedUpdate(WAL::ViewEntity &entity) override; //! @brief A default constructor explicit GamepadSystem(WAL::Wal &wal); diff --git a/sources/System/GridCentered/GridCenteredSystem.cpp b/sources/System/GridCentered/GridCenteredSystem.cpp index d6337c3a..3c86175b 100644 --- a/sources/System/GridCentered/GridCenteredSystem.cpp +++ b/sources/System/GridCentered/GridCenteredSystem.cpp @@ -12,10 +12,10 @@ namespace BBM : System(wal) {} - void GridCenteredSystem::onFixedUpdate(WAL::Entity &entity) + void GridCenteredSystem::onFixedUpdate(WAL::ViewEntity &entity) { - auto &grid = entity.getComponent(); - auto &movement = entity.getComponent(); + auto &grid = entity.get(); + auto &movement = entity.get(); // movement.addForce(grid.force * ) } } \ No newline at end of file diff --git a/sources/System/GridCentered/GridCenteredSystem.hpp b/sources/System/GridCentered/GridCenteredSystem.hpp index 3af49ea1..6ab97339 100644 --- a/sources/System/GridCentered/GridCenteredSystem.hpp +++ b/sources/System/GridCentered/GridCenteredSystem.hpp @@ -13,7 +13,7 @@ namespace BBM class GridCenteredSystem : public WAL::System { public: - void onFixedUpdate(WAL::Entity &entity) override; + void onFixedUpdate(WAL::ViewEntity &entity) override; //! @brief A default constructor explicit GridCenteredSystem(WAL::Wal &wal); diff --git a/sources/System/Health/HealthSystem.cpp b/sources/System/Health/HealthSystem.cpp index e9aaa155..29e0db1f 100644 --- a/sources/System/Health/HealthSystem.cpp +++ b/sources/System/Health/HealthSystem.cpp @@ -14,9 +14,9 @@ namespace BBM : System(wal) {} - void HealthSystem::onFixedUpdate(WAL::Entity &entity) + void HealthSystem::onFixedUpdate(WAL::ViewEntity &entity) { - auto &health = entity.getComponent(); + auto &health = entity.get(); if (health.getHealthPoint() == 0) health.onDeath(entity); diff --git a/sources/System/Health/HealthSystem.hpp b/sources/System/Health/HealthSystem.hpp index fe8bb1ab..f1cd2397 100644 --- a/sources/System/Health/HealthSystem.hpp +++ b/sources/System/Health/HealthSystem.hpp @@ -15,7 +15,7 @@ namespace BBM { public: //! @inherit - void onFixedUpdate(WAL::Entity &entity) override; + void onFixedUpdate(WAL::ViewEntity &entity) override; //! @brief A default constructor explicit HealthSystem(WAL::Wal &wal); diff --git a/sources/System/Keyboard/KeyboardSystem.cpp b/sources/System/Keyboard/KeyboardSystem.cpp index 044373b4..ccc207ab 100644 --- a/sources/System/Keyboard/KeyboardSystem.cpp +++ b/sources/System/Keyboard/KeyboardSystem.cpp @@ -3,11 +3,9 @@ // Edited by Benjamin Henry on 2021-05-20. // -#include #include "KeyboardSystem.hpp" #include "Component/Keyboard/KeyboardComponent.hpp" #include "Component/Controllable/ControllableComponent.hpp" -#include "Entity/Entity.hpp" #include "Controllers/Keyboard.hpp" using Keyboard = RAY::Controller::Keyboard; @@ -18,10 +16,10 @@ namespace BBM : System(wal) {} - void KeyboardSystem::onFixedUpdate(WAL::Entity &entity) + void KeyboardSystem::onFixedUpdate(WAL::ViewEntity &entity) { - const auto &keyboard = entity.getComponent(); - auto &controllable = entity.getComponent(); + const auto &keyboard = entity.get(); + auto &controllable = entity.get(); const std::map keyPressedMap = { {keyboard.keyJump, controllable.jump}, diff --git a/sources/System/Keyboard/KeyboardSystem.hpp b/sources/System/Keyboard/KeyboardSystem.hpp index 56977386..cf7309e1 100644 --- a/sources/System/Keyboard/KeyboardSystem.hpp +++ b/sources/System/Keyboard/KeyboardSystem.hpp @@ -17,7 +17,7 @@ namespace BBM { public: //! @inherit - void onFixedUpdate(WAL::Entity &entity) override; + void onFixedUpdate(WAL::ViewEntity &entity) override; //! @brief A default constructor explicit KeyboardSystem(WAL::Wal &wal); diff --git a/sources/System/Movable/MovableSystem.cpp b/sources/System/Movable/MovableSystem.cpp index c6d86457..eff5a79a 100644 --- a/sources/System/Movable/MovableSystem.cpp +++ b/sources/System/Movable/MovableSystem.cpp @@ -12,10 +12,10 @@ namespace BBM : System(wal) {} - void MovableSystem::onFixedUpdate(WAL::Entity &entity) + void MovableSystem::onFixedUpdate(WAL::ViewEntity &entity) { - auto &movable = entity.getComponent(); - auto &position = entity.getComponent(); + auto &movable = entity.get(); + auto &position = entity.get(); position.position += movable._velocity; movable._velocity = movable._acceleration; diff --git a/sources/System/Movable/MovableSystem.hpp b/sources/System/Movable/MovableSystem.hpp index 7032d97e..e109da13 100644 --- a/sources/System/Movable/MovableSystem.hpp +++ b/sources/System/Movable/MovableSystem.hpp @@ -17,7 +17,7 @@ namespace BBM { public: //! @inherit - void onFixedUpdate(WAL::Entity &entity) override; + void onFixedUpdate(WAL::ViewEntity &entity) override; //! @brief A default constructor explicit MovableSystem(WAL::Wal &wal); diff --git a/sources/System/Renderer/RenderSystem.cpp b/sources/System/Renderer/RenderSystem.cpp index 16190445..1607183e 100644 --- a/sources/System/Renderer/RenderSystem.cpp +++ b/sources/System/Renderer/RenderSystem.cpp @@ -52,10 +52,10 @@ namespace BBM this->_window.endDrawing(); } - void RenderSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) + void RenderSystem::onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) { - const auto &pos = entity.getComponent(); - const auto &cam = entity.getComponent(); + const auto &pos = entity.get(); + const auto &cam = entity.get(); _camera.setPosition(pos.position); _camera.setTarget(cam.target); } diff --git a/sources/System/Renderer/RenderSystem.hpp b/sources/System/Renderer/RenderSystem.hpp index 0ba6c4d3..9c5c4c27 100644 --- a/sources/System/Renderer/RenderSystem.hpp +++ b/sources/System/Renderer/RenderSystem.hpp @@ -27,7 +27,7 @@ namespace BBM void onSelfUpdate() override; //! @inherit - void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) override; + void onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) override; //! @brief ctor RenderSystem(WAL::Wal &wal, RAY::Window &window); diff --git a/tests/CollisionTest.cpp b/tests/CollisionTest.cpp index 77188df1..ce153238 100644 --- a/tests/CollisionTest.cpp +++ b/tests/CollisionTest.cpp @@ -39,8 +39,8 @@ TEST_CASE("Collision test", "[Component][System]") entity.getComponent().bound.y = 5; entity.getComponent().bound.z = 5; - collision.onUpdate(entity, std::chrono::nanoseconds(1)); - collision.onFixedUpdate(entity); + collision.update(std::chrono::nanoseconds(1)); + collision.fixedUpdate(); REQUIRE(entity.getComponent().position.x == 0.0); REQUIRE(entity.getComponent().position.y == 0.0); REQUIRE(entity.getComponent().position.z == 0.0); @@ -49,9 +49,9 @@ TEST_CASE("Collision test", "[Component][System]") .addComponent(2,2,2) .addComponent(1); Entity &player = wal.scene->getEntities().front(); - collision.onUpdate(entity, std::chrono::nanoseconds(1)); + collision.update(std::chrono::nanoseconds(1)); REQUIRE(player.hasComponent(typeid(PositionComponent))); - collision.onFixedUpdate(player); + collision.fixedUpdate(); REQUIRE(wal.scene->getEntities().size() == 2); REQUIRE(player.hasComponent(typeid(PositionComponent))); REQUIRE(player.getComponent().position.x == 1.0); @@ -87,10 +87,10 @@ TEST_CASE("Collsion test with movable", "[Component][System]") entity.getComponent().bound.z = 5; entity.getComponent().addForce({1, 1, 1}); - collision.onUpdate(entity, std::chrono::nanoseconds(1)); - collision.onFixedUpdate(entity); - movable.onUpdate(entity, std::chrono::nanoseconds(1)); - movable.onFixedUpdate(entity); + collision.update(std::chrono::nanoseconds(1)); + collision.fixedUpdate(); + movable.update(std::chrono::nanoseconds(1)); + movable.fixedUpdate(); REQUIRE(entity.getComponent().position.x == 0.0); REQUIRE(entity.getComponent().position.y == 0.0); REQUIRE(entity.getComponent().position.z == 0.0); diff --git a/tests/MoveTests.cpp b/tests/MoveTests.cpp index ac035c86..b19a68fc 100644 --- a/tests/MoveTests.cpp +++ b/tests/MoveTests.cpp @@ -20,32 +20,32 @@ using namespace BBM; TEST_CASE("Move test", "[Component][System]") { Wal wal; - Scene scene; - scene.addEntity("player") + wal.scene = std::make_shared(); + wal.scene->addEntity("player") .addComponent() .addComponent() .addComponent(); - Entity &entity = *scene.getEntities().begin(); + Entity &entity = wal.scene->getEntities().front(); REQUIRE(entity.getComponent().position == Vector3f()); entity.getComponent().move = Vector2f(1, 1); ControllableSystem controllable(wal); - controllable.onUpdate(entity, std::chrono::nanoseconds(1)); - controllable.onFixedUpdate(entity); + controllable.update(std::chrono::nanoseconds(1)); + controllable.fixedUpdate(); REQUIRE(entity.getComponent()._acceleration.x > 0); REQUIRE(entity.getComponent()._acceleration.z > 0); MovableSystem movable(wal); - movable.onUpdate(entity, std::chrono::nanoseconds(1)); - movable.onFixedUpdate(entity); + movable.update(std::chrono::nanoseconds(1)); + movable.fixedUpdate(); REQUIRE(entity.getComponent()._velocity.x > 0); REQUIRE(entity.getComponent()._velocity.z > 0); REQUIRE(entity.getComponent()._acceleration.x == 0); REQUIRE(entity.getComponent()._acceleration.z == 0); - movable.onUpdate(entity, std::chrono::nanoseconds(1)); - movable.onFixedUpdate(entity); + movable.update(std::chrono::nanoseconds(1)); + movable.fixedUpdate(); REQUIRE(entity.getComponent().position.x > 0); REQUIRE(entity.getComponent().position.z > 0); diff --git a/tests/ViewTest.cpp b/tests/ViewTest.cpp index 194e04a6..569d42ad 100644 --- a/tests/ViewTest.cpp +++ b/tests/ViewTest.cpp @@ -22,7 +22,7 @@ TEST_CASE("View creation", "[View]") REQUIRE(scene.view().size() == 2); REQUIRE(scene.view().size() == 1); Entity &entity = *scene.getEntities().begin(); - Entity &firstView = *scene.view().entities.begin(); + Entity &firstView = scene.view().front(); REQUIRE(&entity == &firstView); } @@ -62,23 +62,65 @@ TEST_CASE("View cache switch", "[View]") .addComponent(); REQUIRE(&view == &scene.view()); - REQUIRE(view.entities.begin()->get().getName() == "player"); - REQUIRE(scene2.view().entities.begin()->get().getName() == "box"); + REQUIRE(view.front()->getName() == "player"); + REQUIRE(scene2.view().front()->getName() == "box"); } -//TEST_CASE("View iteration", "[View]") -//{ -// Scene scene; -// scene.addEntity("player") -// .addComponent() -// .addComponent(); -// scene.addEntity("Box") -// .addComponent(); -// int i = 0; -// for (auto &entity : scene.view()) { -// if (i == 0) -// REQUIRE(entity.getName() == "player"); -// else -// REQUIRE(entity.getName() == "Box"); -// } -//} \ No newline at end of file +TEST_CASE("View entity iteration", "[View]") +{ + Scene scene; + scene.addEntity("player") + .addComponent() + .addComponent(); + scene.addEntity("Box") + .addComponent(); + int i = 0; + for (Entity &entity : scene.view()) { + if (i == 0) + REQUIRE(entity.getName() == "player"); + else + REQUIRE(entity.getName() == "Box"); + i++; + } + REQUIRE(i == 2); +} + +TEST_CASE("ViewEntity<> iteration", "[View]") +{ + Scene scene; + scene.addEntity("player") + .addComponent(1, 1, 1) + .addComponent(); + scene.addEntity("Box") + .addComponent(1, 1, 1); + int i = 0; + for (auto entity : scene.view()) { + if (i == 0) + REQUIRE(entity->getName() == "player"); + else + REQUIRE(entity->getName() == "Box"); + REQUIRE(entity.get().position == Vector3f(1, 1, 1)); + i++; + } + REQUIRE(i == 2); +} + +TEST_CASE("View [entity, component] iteration", "[View]") +{ + Scene scene; + scene.addEntity("player") + .addComponent(1, 1, 1) + .addComponent(); + scene.addEntity("Box") + .addComponent(1, 1, 1); + int i = 0; + for (auto &[entity, position] : scene.view()) { + if (i == 0) + REQUIRE(entity.getName() == "player"); + else + REQUIRE(entity.getName() == "Box"); + REQUIRE(position.position == Vector3f(1, 1, 1)); + i++; + } + REQUIRE(i == 2); +} \ No newline at end of file From f3ce14caca373e1ba5296808b5d3edd85d054f0c Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sat, 5 Jun 2021 19:11:26 +0200 Subject: [PATCH 56/60] Cleaning up callbacks --- lib/wal/sources/Models/Callback.hpp | 13 +- lib/wal/sources/Wal.hpp | 115 +++++++++--------- .../Collision/CollisionComponent.cpp | 8 -- .../Collision/CollisionComponent.hpp | 6 - sources/System/Renderer/RenderSystem.cpp | 16 +-- 5 files changed, 67 insertions(+), 91 deletions(-) diff --git a/lib/wal/sources/Models/Callback.hpp b/lib/wal/sources/Models/Callback.hpp index be2c092f..dc12a18b 100644 --- a/lib/wal/sources/Models/Callback.hpp +++ b/lib/wal/sources/Models/Callback.hpp @@ -24,10 +24,14 @@ namespace WAL //! @brief Add a method to be called when this callback is invoked. //! @param callback The list of arguments of the callback method //! @return A unique ID for this callback. That can be used to remove the callback later. - int addCallback(std::function callback) + template + int addCallback(Func callback) { int id = this->_nextID++; - this->_functions[id] = std::move(callback); + if constexpr(std::is_same_v>) + this->_functions[id] = std::move(callback); + else + this->_functions[id] = std::function(callback); return id; } @@ -53,8 +57,9 @@ namespace WAL //! @brief A default assignment operator Callback &operator=(const Callback &) = default; - //! @brief Implicitly transform a function into a callback. - Callback(std::function callback) // NOLINT(google-explicit-constructor) + //! @brief Implicitly transform a callable into a callback. + template + Callback(Func callback) // NOLINT(google-explicit-constructor) { this->addCallback(callback); } diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index ef138871..f0c8dd50 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -28,6 +28,56 @@ namespace WAL private: //! @brief The list of registered systems std::vector> _systems = {}; + + //! @brief Start the game loop + //! @param callback A callback called after each update of the game. It allow you to update the engine based on a specific game state. (you can also update the game state here) + //! @param state An initial game state. If not specified, it will be defaulted. + //! @tparam T A type used to track your game state. It must be default constructable. + template + void _run(const Callback &callback, T state = T()) + { + auto lastTick = std::chrono::steady_clock::now(); + std::chrono::nanoseconds fBehind(0); + + while (!this->shouldClose) { + auto now = std::chrono::steady_clock::now(); + std::chrono::nanoseconds dtime = now - lastTick; + fBehind += dtime; + lastTick = now; + + while (fBehind > Wal::timestep) { + fBehind -= Wal::timestep; + for (auto &system : this->_systems) + system->fixedUpdate(); + } + for (auto &system : this->_systems) + system->update(dtime); + callback(*this, state); + } + } + +#if defined(PLATFORM_WEB) + template + static void _runIteration(void *param) + { + static auto [wal, callback, state] = *reinterpret_cast &, T &> *>(param); + static auto lastTick = std::chrono::steady_clock::now(); + static std::chrono::nanoseconds fBehind(0); + + auto now = std::chrono::steady_clock::now(); + std::chrono::nanoseconds dtime = now - lastTick; + fBehind += dtime; + lastTick = now; + while (fBehind > Wal::timestep) { + fBehind -= Wal::timestep; + for (auto &system : wal._systems) + system->fixedUpdate(); + } + for (auto &system : wal._systems) + system->update(dtime); + callback(wal, state); + } +#endif public: //! @brief The scene that contains entities. std::shared_ptr scene; @@ -94,23 +144,6 @@ namespace WAL return *this; } - //! @brief Start the game loop - //! @param callback A callback called after each update of the game. It allow you to update the engine based on a specific game state. (you can also update the game state here) - //! @param state An initial game state. If not specified, it will be defaulted. - //! @tparam T A type used to track your game state. It must be default constructable. - template - void run(const std::function &callback, T state = T()) - { - Callback update(callback); - - #if defined(PLATFORM_WEB) - std::tuple iterationParams(this, &update, &state); - return emscripten_set_main_loop_arg((em_arg_callback_func)runIteration, (void *)&iterationParams, 0, 1); - #else - return this->run(update, state); - #endif - } - //! @brief Start the game loop //! @param callback A callback called after each update of the game. It allow you to update the engine based on a specific game state. (you can also update the game state here) //! @param state An initial game state. If not specified, it will be defaulted. @@ -118,50 +151,14 @@ namespace WAL template void run(const Callback &callback, T state = T()) { - auto lastTick = std::chrono::steady_clock::now(); - std::chrono::nanoseconds fBehind(0); - - while (!this->shouldClose) { - auto now = std::chrono::steady_clock::now(); - std::chrono::nanoseconds dtime = now - lastTick; - fBehind += dtime; - lastTick = now; - - while (fBehind > Wal::timestep) { - fBehind -= Wal::timestep; - for (auto &system : this->_systems) - system->fixedUpdate(); - } - for (auto &system : this->_systems) - system->update(dtime); - callback(*this, state); - } + #if defined(PLATFORM_WEB) + std::tuple &, T &> iterationParams(*this, callback, state); + return emscripten_set_main_loop_arg((em_arg_callback_func)_runIteration, (void *)&iterationParams, 0, 1); + #else + return this->_run(callback, state); + #endif } - #if defined(PLATFORM_WEB) - template - static void runIteration(void *param) - { - static auto iterationParams = reinterpret_cast *, T *> *>(param); - static const Callback callback = *((Callback *)std::get<1>(*iterationParams)); - static T *state = (T *)std::get<2>(*iterationParams); - static Wal *wal = (Wal *)std::get<0>(*iterationParams); - static auto lastTick = std::chrono::steady_clock::now(); - static std::chrono::nanoseconds fBehind(0); - - auto now = std::chrono::steady_clock::now(); - std::chrono::nanoseconds dtime = now - lastTick; - fBehind += dtime; - lastTick = now; - while (fBehind > Wal::timestep) { - fBehind -= Wal::timestep; - wal->_fixedUpdate(); - } - wal->_update(dtime); - callback(*wal, *state); - } - #endif - //! @brief A default constructor Wal() = default; //! @brief A WAL can't be copy constructed diff --git a/sources/Component/Collision/CollisionComponent.cpp b/sources/Component/Collision/CollisionComponent.cpp index 22cbdef8..df727b2d 100644 --- a/sources/Component/Collision/CollisionComponent.cpp +++ b/sources/Component/Collision/CollisionComponent.cpp @@ -16,14 +16,6 @@ namespace BBM return new CollisionComponent(entity); } - CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function onCollide, std::function onCollided, Vector3f bound) - : WAL::Component(entity), onCollide(onCollide), onCollided(onCollided), bound(bound) - { } - - CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function onCollide, std::function onCollided, float boundSize) - : WAL::Component(entity), onCollide(onCollide), onCollided(onCollided), bound({boundSize, boundSize, boundSize}) - { } - CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback onCollide, WAL::Callback onCollided, Vector3f bound) : WAL::Component(entity), onCollide(onCollide), onCollided(onCollided), bound(bound) { } diff --git a/sources/Component/Collision/CollisionComponent.hpp b/sources/Component/Collision/CollisionComponent.hpp index 4636c79a..5bfe7e16 100644 --- a/sources/Component/Collision/CollisionComponent.hpp +++ b/sources/Component/Collision/CollisionComponent.hpp @@ -27,12 +27,6 @@ namespace BBM //! @brief A component can't be instantiated, it should be derived. explicit CollisionComponent(WAL::Entity &entity); - //! @brief Constructor with a callback function - CollisionComponent(WAL::Entity &entity, std::function onCollide, std::function onCollided, Vector3f bound); - - //! @brief Constructor with a callback function, same boundSize for all axis - CollisionComponent(WAL::Entity &entity, std::function onCollide, std::function onCollided, float boundSize = 0); - //! @brief Constructor with a WAL::Callback CollisionComponent(WAL::Entity &entity, WAL::Callback onCollide, WAL::Callback onCollided,Vector3f bound); diff --git a/sources/System/Renderer/RenderSystem.cpp b/sources/System/Renderer/RenderSystem.cpp index 1607183e..bd6a3adc 100644 --- a/sources/System/Renderer/RenderSystem.cpp +++ b/sources/System/Renderer/RenderSystem.cpp @@ -26,26 +26,14 @@ namespace BBM this->_window.clear(); this->_window.useCamera(this->_camera); - for (auto &entity : this->_wal.scene->getEntities()) { - if (!entity.hasComponent() - || !entity.hasComponent()) - continue; - auto &drawable = entity.getComponent(); - auto &pos = entity.getComponent(); - + for (auto &[_, pos, drawable] : this->_wal.scene->view()) { drawable.drawable->setPosition(pos.position); drawable.drawable->drawOn(this->_window); } this->_window.unuseCamera(); // TODO sort entities based on the Z axis - for (auto &entity : this->_wal.scene->getEntities()) { - if (!entity.hasComponent() - || !entity.hasComponent()) - continue; - auto &drawable = entity.getComponent(); - auto &pos = entity.getComponent(); - + for (auto &[_, pos, drawable] : this->_wal.scene->view()) { drawable.drawable->setPosition(Vector2f(pos.position.x, pos.position.y)); drawable.drawable->drawOn(this->_window); } From 1b4e8d2151832be6cac3eee6864e21e35c4aa601 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sat, 5 Jun 2021 19:55:38 +0200 Subject: [PATCH 57/60] Optimizing view iterators --- lib/wal/sources/View/View.hpp | 14 +++++++++----- lib/wal/sources/Wal.hpp | 2 +- sources/System/Collision/CollisionSystem.cpp | 10 ++++------ 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/wal/sources/View/View.hpp b/lib/wal/sources/View/View.hpp index cbee789e..15b31e09 100644 --- a/lib/wal/sources/View/View.hpp +++ b/lib/wal/sources/View/View.hpp @@ -19,9 +19,9 @@ namespace WAL class ViewEntity { private: - std::tuple, std::reference_wrapper...> _value; + std::tuple, std::reference_wrapper...> &_value; public: - explicit ViewEntity(std::tuple, std::reference_wrapper...> value) + explicit ViewEntity(std::tuple, std::reference_wrapper...> &value) : _value(value) {} @@ -63,19 +63,22 @@ namespace WAL public: ViewEntity &operator*() { - this->_entity.emplace(*this->_it); - return this->_entity.value(); + if (!this->_entity) + this->_entity.emplace(*this->_it); + return *this->_entity; } ViewEntity *operator->() { - this->_entity.emplace(*this->_it); + if (!this->_entity) + this->_entity =(*this->_it); return &this->_entity; } ViewIterator &operator++() { this->_it++; + this->_entity = std::nullopt; return *this; } @@ -83,6 +86,7 @@ namespace WAL { ViewIterator copy = *this; this->_it++; + this->_entity = std::nullopt; return *this; } diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index f0c8dd50..85a7804e 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -84,7 +84,7 @@ namespace WAL //! @brief True if the engine should close after the end of the current tick. bool shouldClose = false; //! @brief The time between each fixed update. - static constexpr std::chrono::nanoseconds timestep = std::chrono::milliseconds(8); + static constexpr std::chrono::nanoseconds timestep = std::chrono::milliseconds(16); //! @brief Create a new system in place. //! @return The wal instance used to call this function is returned. This allow method chaining. diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index 4e992bda..6a3e36cd 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -32,13 +32,11 @@ namespace BBM position += movable->getVelocity(); Vector3f minA = Vector3f::min(position, position + col.bound); Vector3f maxA = Vector3f::max(position, position + col.bound); - for (auto other : this->getView()) { - if (other->getUid() == entity->getUid()) + for (auto &[other, posB, colB] : this->getView()) { + if (other.getUid() == entity->getUid()) continue; - auto colB = other.get(); - auto posB = other.get().position; - Vector3f minB = Vector3f::min(posB, posB + colB.bound); - Vector3f maxB = Vector3f::max(posB, posB + colB.bound); + Vector3f minB = Vector3f::min(posB.position, posB.position + colB.bound); + Vector3f maxB = Vector3f::max(posB.position, posB.position + colB.bound); if (collide(minA, maxA, minB, maxB)) { col.onCollide(entity, other); colB.onCollided(entity, other); From 0ebfa77e1a910c0dc05227ea823e1c82e95ee860 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sat, 5 Jun 2021 20:54:17 +0200 Subject: [PATCH 58/60] Fixing include for windowsw --- lib/wal/sources/Scene/Scene.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/wal/sources/Scene/Scene.cpp b/lib/wal/sources/Scene/Scene.cpp index 22ed03ff..77526662 100644 --- a/lib/wal/sources/Scene/Scene.cpp +++ b/lib/wal/sources/Scene/Scene.cpp @@ -3,6 +3,7 @@ // #include "Scene.hpp" +#include namespace WAL { From 01ea9053fab81c9c2303b949abbba4b6529aff9d Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 6 Jun 2021 16:54:27 +0200 Subject: [PATCH 59/60] Making block solids --- lib/wal/sources/Entity/Entity.hpp | 26 +++++++++++++ lib/wal/sources/Models/Callback.hpp | 3 ++ .../Component/Movable/MovableComponent.cpp | 5 --- .../Component/Movable/MovableComponent.hpp | 4 +- sources/Map/Map.cpp | 38 ++++++++++++++----- sources/Map/Map.hpp | 2 + sources/Models/Vector3.hpp | 10 ++++- sources/Runner/Runner.cpp | 14 +------ 8 files changed, 70 insertions(+), 32 deletions(-) diff --git a/lib/wal/sources/Entity/Entity.hpp b/lib/wal/sources/Entity/Entity.hpp index 50770d18..25234f31 100644 --- a/lib/wal/sources/Entity/Entity.hpp +++ b/lib/wal/sources/Entity/Entity.hpp @@ -79,6 +79,32 @@ namespace WAL return static_cast(existing->second.get()); } + //! @brief Get a component of a specific type + //! @tparam The type of the component + //! @throw NotFoundError if the component could not be found + //! @return The component of the requested type. + template + const T &getComponent() const + { + const T *ret = this->tryGetComponent(); + if (ret == nullptr) + throw NotFoundError("No component could be found with the type \"" + std::string(typeid(T).name()) + "\"."); + return *ret; + } + + //! @brief Get a component of a specific type or null if not found. + //! @tparam The type of the component + //! @return The component or nullptr if not found. + template + const T *tryGetComponent() const + { + const std::type_index &type = typeid(T); + auto existing = this->_components.find(type); + if (existing == this->_components.end()) + return nullptr; + return static_cast(existing->second.get()); + } + //! @brief Check if this entity has a component. //! @tparam T The type of the component template diff --git a/lib/wal/sources/Models/Callback.hpp b/lib/wal/sources/Models/Callback.hpp index dc12a18b..2c988db4 100644 --- a/lib/wal/sources/Models/Callback.hpp +++ b/lib/wal/sources/Models/Callback.hpp @@ -64,4 +64,7 @@ namespace WAL this->addCallback(callback); } }; + + template + static constexpr Callback EmptyCallback; } // namespace WAL \ No newline at end of file diff --git a/sources/Component/Movable/MovableComponent.cpp b/sources/Component/Movable/MovableComponent.cpp index 51f0707d..26a8fd11 100644 --- a/sources/Component/Movable/MovableComponent.cpp +++ b/sources/Component/Movable/MovableComponent.cpp @@ -20,11 +20,6 @@ namespace BBM this->_acceleration += force; } - void MovableComponent::resetVelocity(void) - { - this->_velocity = {0, 0, 0}; - } - const Vector3f &MovableComponent::getVelocity(void) const { return _velocity; diff --git a/sources/Component/Movable/MovableComponent.hpp b/sources/Component/Movable/MovableComponent.hpp index 13c8bf90..6fecf89e 100644 --- a/sources/Component/Movable/MovableComponent.hpp +++ b/sources/Component/Movable/MovableComponent.hpp @@ -23,9 +23,6 @@ namespace BBM //! @param force The force to add to this entity's acceleration. The force is added instantly and in one go. void addForce(Vector3f force); - //! @brief Set velocity to 0 - void resetVelocity(void); - //! @brief Get velocity const Vector3f &getVelocity(void) const; @@ -42,5 +39,6 @@ namespace BBM MovableComponent &operator=(const MovableComponent &) = delete; friend class MovableSystem; + friend class MapGenerator; }; } // namespace WAL \ No newline at end of file diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index 258dcd9d..722f0433 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -5,11 +5,31 @@ #include #include "Map.hpp" +#include namespace RAY3D = RAY::Drawables::Drawables3D; namespace BBM -{ +{ + void MapGenerator::wallCollide(WAL::Entity &entity, const WAL::Entity &wall) + { + auto *mov = entity.tryGetComponent(); + if (!mov) + return; + auto &pos = entity.getComponent(); + const auto &wallPos = wall.getComponent(); + auto diff = pos.position + mov->getVelocity() - wallPos.position; + std::cout << diff << std::endl; + if (diff.x <= 0 && mov->_velocity.x < 0) + mov->_velocity.x = 0; + if (diff.x >= 0 && mov->_velocity.x > 0) + mov->_velocity.x = 0; + if (diff.z <= 0 && mov->_velocity.z < 0) + mov->_velocity.z = 0; + if (diff.z >= 0 && mov->_velocity.z > 0) + mov->_velocity.z = 0; + } + void MapGenerator::generateUnbreakableBlock(int width, int height, std::shared_ptr scene) { std::string unbreakableObj = "assets/wall/unbreakable_wall.obj"; @@ -20,7 +40,7 @@ namespace BBM if (!(i % 2) && !(j % 2)) { scene->addEntity("Unbreakable Wall") .addComponent(i, 0, j) - .addComponent(1) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj)); } } @@ -34,19 +54,19 @@ namespace BBM scene->addEntity("Bottom Wall") .addComponent(Vector3f((width + 1) / 2, 0, -1)) - .addComponent(1) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Upper Wall") .addComponent(Vector3f((width + 1) / 2, 0, height + 1)) - .addComponent(1) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Left Wall") .addComponent(Vector3f(width + 1, 0, (height + 1) / 2)) - .addComponent(1) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 3)); scene->addEntity("Right Wall") .addComponent(Vector3f(-1, 0, (height + 1) / 2)) - .addComponent(1) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 3)); } @@ -81,7 +101,7 @@ namespace BBM scene->addEntity("Breakable Block") .addComponent(coords) .addComponent(1) - .addComponent(1) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent("assets/wall/breakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/breakable_wall.png")); } @@ -96,7 +116,7 @@ namespace BBM { scene->addEntity("Unbreakable Block") .addComponent(coords) - .addComponent(1) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png")); } @@ -155,7 +175,7 @@ namespace BBM MapGenerator::MapBlock MapGenerator::createHeight(MapBlock map, int width, int height) { - double rnd = static_cast(std::rand())/RAND_MAX; + double rnd = static_cast(std::rand()) / RAND_MAX; if (rnd > 0.60) { for (int i = 0; i < width; i++) { diff --git a/sources/Map/Map.hpp b/sources/Map/Map.hpp index 7f396b31..4059925b 100644 --- a/sources/Map/Map.hpp +++ b/sources/Map/Map.hpp @@ -119,6 +119,8 @@ namespace BBM public: + static void wallCollide(WAL::Entity &entity, const WAL::Entity &wall); + //! @param width Width of the map //! @param height Height of the map diff --git a/sources/Models/Vector3.hpp b/sources/Models/Vector3.hpp index dc1752f1..d4ba7337 100644 --- a/sources/Models/Vector3.hpp +++ b/sources/Models/Vector3.hpp @@ -71,7 +71,13 @@ namespace BBM } template - Vector3 &operator*=(T2 d) + Vector3 operator-(const Vector3 &vec) const + { + return Vector3(this->x - vec.x, this->y - vec.y, this->z - vec.z); + } + + template + Vector3 &operator*=(const T2 d) { this->x *= d; this->y *= d; @@ -80,7 +86,7 @@ namespace BBM } template - Vector3 operator*(T2 d) const + Vector3 operator*(const T2 d) const { return Vector3(this->x * d, this->y * d, this->z * d); } diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 6dbce849..bfcddef7 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -66,20 +66,8 @@ namespace BBM .addComponent() .addComponent() .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 1) - .addComponent(2) + .addComponent(1) .addComponent(); - scene->addEntity("cube") - .addComponent(-5, 0, -5) - .addComponent(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED) - .addComponent() - .addComponent() - .addComponent([](WAL::Entity &, const WAL::Entity &){}, - [](WAL::Entity &actual, const WAL::Entity &) { - try { - auto &mov = actual.getComponent(); - mov.resetVelocity(); - } catch (std::exception &e) { } - }, 3); scene->addEntity("camera") .addComponent(8, 20, 7) From e624a2f7fecf54886919d3649c67ab35679391fc Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 6 Jun 2021 17:26:05 +0200 Subject: [PATCH 60/60] Fixing unit test compilation --- tests/CollisionTest.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/CollisionTest.cpp b/tests/CollisionTest.cpp index ce153238..394b1ccc 100644 --- a/tests/CollisionTest.cpp +++ b/tests/CollisionTest.cpp @@ -5,12 +5,12 @@ #include #include "Entity/Entity.hpp" #include "Component/Position/PositionComponent.hpp" -#include "Component/Movable/MovableComponent.hpp" -#include "System/Movable/MovableSystem.hpp" -#include "System/Collision/CollisionSystem.hpp" #include "Wal.hpp" #define private public +#include "System/Collision/CollisionSystem.hpp" +#include "System/Movable/MovableSystem.hpp" +#include "Component/Movable/MovableComponent.hpp" #include "Component/Collision/CollisionComponent.hpp" using namespace WAL; @@ -76,7 +76,7 @@ TEST_CASE("Collsion test with movable", "[Component][System]") .addComponent([](Entity &actual, const Entity &){}, [](Entity &actual, const Entity &) { try { auto &mov = actual.getComponent(); - mov.resetVelocity(); + mov._velocity = Vector3f(); } catch (std::exception &e) {}; }, 1); Entity &entity = wal.scene->getEntities().front();