Skip to content

Commit

Permalink
Add Vector methods (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
jthomperoo committed Jan 17, 2022
1 parent ed8aea9 commit a1115b8
Show file tree
Hide file tree
Showing 21 changed files with 421 additions and 48 deletions.
38 changes: 18 additions & 20 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,16 @@ jobs:
build:
name: Build
runs-on: ubuntu-20.04
container: jamjarlabs/jamjar-build-image:v0.1.0
steps:
- name: Check out code into the CI directory
uses: actions/checkout@v1
- name: Setup Emscripten
uses: mymindstorm/setup-emsdk@v9
with:
emscripten-version: '2.0.20'
- name: Lint, test and build
- name: Lint, beautify, and build
env:
CC: emcc
CXX: em++
run: |
# Install required tools
sudo apt-get install clang-11 clang-tidy-11 clang-format-11 cmake -y
# Link tools to expected locations
sudo ln -sf /usr/bin/clang-11 /usr/bin/clang
sudo ln -sf /usr/bin/clang-tidy-11 /usr/bin/clang-tidy
sudo ln -sf /usr/bin/clang-format-11 /usr/bin/clang-format
# Print versions for debugging
cmake --version
em++ --version
clang --version
clang-format --version
clang-tidy --version
# Set up build
emcmake cmake -D CMAKE_EXPORT_COMPILE_COMMANDS=ON . -B build
cd build
Expand All @@ -48,5 +30,21 @@ jobs:
# Build the project
make
# Clean up build
cd ..
rm -rf build/
- name: Test
env:
CC: gcc
CXX: g++
run: |
# Set up build
cmake -D CMAKE_EXPORT_COMPILE_COMMANDS=ON . -B build
cd build
# Build unit tests
make JamJarTests
# Run unit tests
ctest
24 changes: 11 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set (CMAKE_CXX_STANDARD 17)

project(JamJar VERSION 0.0.0)

option(JAMJAR_NATIVE_COMPILE_UNIT_TESTS "Compile JamJar unit tests" OFF)
option(JAMJAR_NATIVE_COMPILE_UNIT_TESTS "Compile JamJar unit tests" ON)

### ===== dependencies ===== ###

Expand Down Expand Up @@ -35,15 +35,12 @@ FetchContent_MakeAvailable(box2d)

if(JAMJAR_NATIVE_COMPILE_UNIT_TESTS)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.0.0-preview3
)

# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

enable_testing()
FetchContent_MakeAvailable(Catch2)
endif()

### ===== core ===== ###
Expand All @@ -52,7 +49,7 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

if (EMSCRIPTEN)
set(CMAKE_CXX_FLAGS "-s USE_SDL=2 -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2")
set(CMAKE_CXX_FLAGS "-s USE_SDL=2")
endif()

set(LIB_SOURCES
Expand Down Expand Up @@ -101,7 +98,7 @@ set(LIB_SOURCES
)

set(TEST_SOURCES
src/game_test.cpp
src/geometry/vector_2d.test.cpp
)

add_library(JamJar
Expand All @@ -128,11 +125,12 @@ if(JAMJAR_NATIVE_COMPILE_UNIT_TESTS)
target_link_libraries(
JamJarTests
JamJar
gtest_main
Catch2::Catch2WithMain
)

include(GoogleTest)
gtest_discover_tests(JamJarTests)
enable_testing()
add_test(NAME JamJarTests COMMAND JamJarTests)

endif()

add_subdirectory (examples/Collision)
Expand Down
17 changes: 15 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ The project has the following dependencies:
- `clang` == `11.1.X`
- `clang-tidy` == `11.1.X`
- `clang-format` == `11.1.X`
- `emscripten` == `2.0.X`
- `emscripten` == `3.1.X`

Later minor and patch versions of these dependencies should still work, the CI build just uses the versions above.

Expand Down Expand Up @@ -71,7 +71,20 @@ This will generate the `build/libJamJar.a` library archive which can be used whe

##### Running unit tests

Run the unit tests by running the following in the `build/` directory:
The unit tests run locally, not in the browser after compiling with emscripten. This means that they need to be built
either with GCC or clang, you can do that by removing the `build/` directory, then running this command:

```bash
cmake -D CMAKE_EXPORT_COMPILE_COMMANDS=ON . -B build
```

This command will generate the build files for GCC/Clang in the `build/` directory, inside which you can run;

```bash
make JamJarTests
```

Then you can run the unit tests by running the following in the `build/` directory:

```bash
ctest
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ See the [examples directory for working code examples](./examples).

**Dev Dependencies**

- [GoogleTest](https://github.com/google/googletest) - BSD-3
- [Catch2](https://github.com/catchorg/Catch2) - BSL-1.0
- [Emscripten](https://github.com/emscripten-core/emscripten) - MIT/UIUC
- [llvm/clang/clang-tidy/clang-format](https://llvm.org/) - UIUC/Apache-2.0 with LLVM Exceptions
- [CMake](https://cmake.org/) - BSD-3
Expand Down
3 changes: 2 additions & 1 deletion examples/Collision/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ FetchContent_MakeAvailable(JamJar)

### ===== core ===== ###

set(CMAKE_CXX_FLAGS "-s USE_SDL=2 -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2")
set(CMAKE_CXX_FLAGS "-s USE_SDL=2")
SET(CMAKE_EXE_LINKER_FLAGS "-s LLD_REPORT_UNDEFINED -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2")

set(EXE_SOURCES
src/box.cpp
Expand Down
4 changes: 2 additions & 2 deletions examples/Fullscreen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ FetchContent_MakeAvailable(JamJar)

### ===== core ===== ###

set(CMAKE_CXX_FLAGS "-s USE_SDL=2 -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2 --preload-file ${PROJECT_SOURCE_DIR}/assets@/assets")
SET(CMAKE_EXE_LINKER_FLAGS "-s LLD_REPORT_UNDEFINED")
set(CMAKE_CXX_FLAGS "-s USE_SDL=2")
SET(CMAKE_EXE_LINKER_FLAGS "-s LLD_REPORT_UNDEFINED -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2 --preload-file ${PROJECT_SOURCE_DIR}/assets@/assets")

set(EXE_SOURCES
src/main.cpp
Expand Down
4 changes: 2 additions & 2 deletions examples/MouseInput/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ FetchContent_MakeAvailable(JamJar)

### ===== core ===== ###

set(CMAKE_CXX_FLAGS "-s USE_SDL=2 -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2 --preload-file ${PROJECT_SOURCE_DIR}/assets@/assets")
SET(CMAKE_EXE_LINKER_FLAGS "-s LLD_REPORT_UNDEFINED")
set(CMAKE_CXX_FLAGS "-s USE_SDL=2")
SET(CMAKE_EXE_LINKER_FLAGS "-s LLD_REPORT_UNDEFINED -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2 --preload-file ${PROJECT_SOURCE_DIR}/assets@/assets")

set(EXE_SOURCES
src/main.cpp
Expand Down
3 changes: 2 additions & 1 deletion examples/Physics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ FetchContent_MakeAvailable(JamJar)

### ===== core ===== ###

set(CMAKE_CXX_FLAGS "-s USE_SDL=2 -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2")
set(CMAKE_CXX_FLAGS "-s USE_SDL=2")
SET(CMAKE_EXE_LINKER_FLAGS "-s LLD_REPORT_UNDEFINED -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2")

set(EXE_SOURCES
src/input_listener.cpp
Expand Down
3 changes: 2 additions & 1 deletion examples/PhysicsResizing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ FetchContent_MakeAvailable(JamJar)

### ===== core ===== ###

set(CMAKE_CXX_FLAGS "-s USE_SDL=2 -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2")
set(CMAKE_CXX_FLAGS "-s USE_SDL=2")
SET(CMAKE_EXE_LINKER_FLAGS "-s LLD_REPORT_UNDEFINED -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2")

set(EXE_SOURCES
src/resizing_system.cpp
Expand Down
3 changes: 2 additions & 1 deletion examples/Primitives/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ FetchContent_MakeAvailable(JamJar)

### ===== core ===== ###

set(CMAKE_CXX_FLAGS "-s USE_SDL=2 -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2")
set(CMAKE_CXX_FLAGS "-s USE_SDL=2")
SET(CMAKE_EXE_LINKER_FLAGS "-s LLD_REPORT_UNDEFINED -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2")

set(EXE_SOURCES
src/main.cpp
Expand Down
4 changes: 2 additions & 2 deletions examples/Rotator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ FetchContent_MakeAvailable(JamJar)

### ===== core ===== ###

set(CMAKE_CXX_FLAGS "-s USE_SDL=2 -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2 -s NO_DISABLE_EXCEPTION_CATCHING")
SET(CMAKE_EXE_LINKER_FLAGS "-s LLD_REPORT_UNDEFINED")
set(CMAKE_CXX_FLAGS "-s USE_SDL=2")
SET(CMAKE_EXE_LINKER_FLAGS "-s LLD_REPORT_UNDEFINED -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2")

set(EXE_SOURCES
src/main.cpp
Expand Down
4 changes: 2 additions & 2 deletions examples/Sprites/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ FetchContent_MakeAvailable(JamJar)

### ===== core ===== ###

set(CMAKE_CXX_FLAGS "-s NO_DISABLE_EXCEPTION_CATCHING -s USE_SDL=2 -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2 --preload-file ${PROJECT_SOURCE_DIR}/assets@/assets")
SET(CMAKE_EXE_LINKER_FLAGS "-s LLD_REPORT_UNDEFINED")
set(CMAKE_CXX_FLAGS "-s USE_SDL=2")
SET(CMAKE_EXE_LINKER_FLAGS "-s LLD_REPORT_UNDEFINED -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2 --preload-file ${PROJECT_SOURCE_DIR}/assets@/assets")

set(EXE_SOURCES
src/main.cpp
Expand Down
15 changes: 15 additions & 0 deletions include/geometry/vector_2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Vector2D {
float x;
float y;
Vector2D();
Vector2D(const Vector2D &vec);
explicit Vector2D(float x, float y);
Vector2D operator+(const Vector2D &other) const;
void operator+=(const Vector2D &other);
Expand All @@ -16,6 +17,20 @@ class Vector2D {
void operator*=(const Vector2D &other);
Vector2D operator*(float scaler) const;
void operator*=(float scaler);

float Dot(const Vector2D &other) const;
float Magnitude() const;
float Distance(const Vector2D &other) const;

Vector2D Rotate(const Vector2D &center, float angle) const;
Vector2D RotateDeg(const Vector2D &center, float angle) const;
Vector2D Invert() const;
Vector2D Normalize() const;

Vector2D *RotateInPlace(const Vector2D &center, float angle);
Vector2D *RotateDegInPlace(const Vector2D &center, float angle);
Vector2D *InvertInPlace();
Vector2D *NormalizeInPlace();
};
}; // namespace JamJar

Expand Down
1 change: 1 addition & 0 deletions include/system/bucket_map_system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "entity/entity.hpp"
#include "system/stateful_system.hpp"
#include "system/system_entity.hpp"
#include <functional>
#include <map>
#include <optional>
#include <vector>
Expand Down
1 change: 1 addition & 0 deletions include/system/bucket_system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "entity/entity.hpp"
#include "system/stateful_system.hpp"
#include "system/system_entity.hpp"
#include <functional>
#include <map>
#include <optional>
#include <vector>
Expand Down
1 change: 1 addition & 0 deletions include/system/map_system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "entity/entity.hpp"
#include "system/stateful_system.hpp"
#include "system/system_entity.hpp"
#include <functional>
#include <map>

namespace JamJar {
Expand Down
1 change: 1 addition & 0 deletions include/system/single_entity_system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "entity/entity.hpp"
#include "system/stateful_system.hpp"
#include "system/system_entity.hpp"
#include <functional>
#include <vector>

namespace JamJar {
Expand Down
1 change: 1 addition & 0 deletions include/system/vector_system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "entity/entity.hpp"
#include "system/stateful_system.hpp"
#include "system/system_entity.hpp"
#include <functional>
#include <vector>

namespace JamJar {
Expand Down
Empty file removed src/game_test.cpp
Empty file.

0 comments on commit a1115b8

Please sign in to comment.