Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • gamedev/fggl
  • onuralpsezer/fggl
2 results
Show changes
Showing
with 870 additions and 222 deletions
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 26/06/22.
//
#ifndef FGGL_PLATFORM_PATHS_HPP
#define FGGL_PLATFORM_PATHS_HPP
#include <filesystem>
#include <vector>
#ifdef __linux__
#define FGGL_PLATFORM_PATHS linux
#include "fggl/platform/linux/paths.hpp"
#else
#define FGGL_PLATFORM_PATHS fallback
#include "fggl/platform/fallback/paths.hpp"
#endif
#endif //FGGL_PLATFORM_PATHS_HPP
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 20/11/2021.
//
#ifndef FGGL_SCENE_H
#define FGGL_SCENE_H
#ifndef FGGL_SCENES_SCENE_HPP
#define FGGL_SCENES_SCENE_HPP
#include <memory>
#include <string>
......@@ -50,4 +64,4 @@ namespace fggl::scenes {
}
#endif //FGGL_SCENE_H
#endif //FGGL_SCENES_SCENE_HPP
/*
* ${license.title}
* Copyright (C) 2022 ${license.owner}
* ${license.mailto}
* This file is part of FGGL.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
......@@ -26,33 +20,68 @@
#define FGGL_SCENES_GAME_HPP
#include "fggl/app.hpp"
#include "fggl/entity/entity.hpp"
#include "fggl/phys/types.hpp"
namespace fggl::scenes {
class GameBase : public fggl::AppState {
public:
explicit GameBase(fggl::App &app);
void update(float dt) override;
void render(fggl::gfx::Graphics &gfx) override = 0;
protected:
inline auto input() -> input::Input & {
return *m_input;
}
inline void returnToMenu() {
m_owner.change_state(m_previous);
}
private:
input::Input *m_input;
std::string m_previous = "menu";
};
class Game : public fggl::AppState {
public:
explicit Game(fggl::App& app);
explicit Game(fggl::App &app);
void activate() override;
void deactivate() override;
void update() override;
void render(fggl::gfx::Graphics& gfx) override;
void update(float dt) override;
void render(fggl::gfx::Graphics &gfx) override;
protected:
inline auto world() -> ecs3::World& {
inline auto world() -> entity::EntityManager & {
return *m_world;
}
inline auto input() -> input::Input& {
protected:
bool hasPhys() const {
return m_phys != nullptr;
}
inline auto phys() -> phys::PhysicsEngine & {
assert(m_phys != nullptr);
return *m_phys;
}
inline auto input() -> input::Input & {
return *m_input;
}
bool m_debug;
private:
std::shared_ptr<input::Input> m_input;
std::unique_ptr<ecs3::World> m_world;
input::Input *m_input;
std::unique_ptr<entity::EntityManager> m_world;
std::unique_ptr<phys::PhysicsEngine> m_phys;
std::string m_previous = "menu";
};
......
#ifndef FGGL_SCENES_MENU_H
#define FGGL_SCENES_MENU_H
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef FGGL_SCENES_MENU_HPP
#define FGGL_SCENES_MENU_HPP
#include <functional>
#include <map>
......@@ -12,29 +26,29 @@
namespace fggl::scenes {
using callback = std::function<void(void)>;
using Callback = std::function<void(void)>;
class BasicMenu : public AppState {
public:
explicit BasicMenu(App &owner);
void update() override;
void update(float dt) override;
void render(gfx::Graphics &paint) override;
void activate() override;
void deactivate() override;
void add(const std::string &label, callback cb);
void add(const std::string &label, const Callback& /*cb*/);
private:
std::shared_ptr<input::Input> m_inputs;
std::map<const std::string, callback> m_items;
input::Input *m_inputs;
std::map<const std::string, Callback> m_items;
// menu state
std::string m_active;
math::vec2 m_cursorPos;
gui::Container m_canvas;
gui::Widget* m_hover;
gui::Widget *m_hover;
};
} // namepace fggl::scenes
......
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 15/10/22.
//
#ifndef FGGL_SCRIPT_ENGINE_H
#define FGGL_SCRIPT_ENGINE_H
#include "fggl/modules/module.hpp"
namespace fggl::script {
class ScriptEngine {
public:
virtual ~ScriptEngine() = default;
// TODO use protected virtual pattern
// scene callbacks
virtual void onActivate() = 0;
virtual void onDeactivate() = 0;
virtual void onUpdate() = 0;
// trigger callback
virtual void onEvent(const std::string& name) = 0;
// run code in engine
virtual bool run(const char* script) = 0;
virtual bool load(const char* filename) = 0;
virtual void setGlobal(const char* name, void* ptr) = 0;
};
class ScriptProvider {
public:
constexpr static const modules::ServiceName service = modules::make_service("fggl::script::service");
virtual ScriptEngine* create() = 0;
};
}
#endif //FGGL_SCRIPT_ENGINE_H
#ifndef FGGL_UTIL_CHRONO_H
#define FGGL_UTIL_CHRONO_H
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef FGGL_UTIL_CHRONO_HPP
#define FGGL_UTIL_CHRONO_HPP
namespace fggl::util {
......
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 23/07/22.
// See http://www.isthe.com/chongo/tech/comp/fnv/
//
#ifndef FGGL_UTIL_GUID_HPP
#define FGGL_UTIL_GUID_HPP
#include <cstdint>
#include <cassert>
#include "fggl/util/safety.hpp"
#include "fggl/debug/logging.hpp"
namespace fggl::util {
using GUID = OpaqueName<std::uint64_t, struct GuidTag>;
constexpr uint32_t FNV_PRIME_32 = 0x01000193;
constexpr uint32_t FNV_OFFSET_BASIS_32 = 0x811c9dc5;
constexpr uint64_t FNV_PRIME_64 = 0x00000100000001B3;
constexpr uint64_t FNV_OFFSET_BASIS_64 = 0xcbf29ce484222325;
/**
* Folwer-Noll-Vo 32-bit hash function.
*
* @param str the string to hash.
* @return the hashed value
*/
constexpr uint32_t hash_fnv1a_32(const char *str) {
assert(str != nullptr);
uint32_t hash = FNV_OFFSET_BASIS_32;
for (int i = 0; str[i] != '\0'; i++) {
hash = hash ^ str[i];
hash = hash * FNV_PRIME_32;
}
return hash;
}
/**
* Folwer-Noll-Vo 64-bit hash function.
*
* @param str the string to be hashed
* @return the hashed value
*/
constexpr uint64_t hash_fnv1a_64(const char *str) {
assert(str != nullptr);
uint64_t hash = FNV_OFFSET_BASIS_64;
for (int i = 0; str[i] != '\0'; i++) {
hash = hash ^ str[i];
hash = hash * FNV_PRIME_64;
}
return hash;
}
template<unsigned N>
struct FString {
char c[N];
};
// https://stackoverflow.com/a/65440575
template<unsigned ...Len>
constexpr auto cat(const char (&...strings)[Len]) {
constexpr unsigned N = (... + Len) - sizeof...(Len);
FString<N + 1> result = {};
result.c[N] = '\0';
char* dst = result.c;
for (const char* src : {strings...}) {
for (; *src != '\0'; src++, dst++) {
*dst = *src;
}
}
return result;
}
// debug-only functions
#ifndef NDEBUG
GUID intern_string(const char *str);
std::string guid_to_string(GUID guid);
#endif
constexpr GUID make_guid(const char *str) {
return GUID(hash_fnv1a_64(str));
}
inline GUID make_guid_rt(const char* str) {
#ifndef NDEBUG
return intern_string(str);
#else
return make_guid(str);
#endif
}
inline GUID make_guid_rt(const std::string &str) {
return make_guid_rt(str.c_str());
}
} // namespace fggl::util
// formatter
template<> struct fmt::formatter<fggl::util::GUID> {
constexpr auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}
template <typename FormatContext>
auto format(const fggl::util::GUID& guid, FormatContext& ctx) const -> decltype(ctx.out()) {
#ifndef NDEBUG
return fmt::format_to(ctx.out(), "GUID[{}]", guid_to_string(guid));
#else
return fmt::format_to(ctx.out(), "GUID[{}]", guid.get());
#endif
}
};
fggl::util::GUID operator "" _fid(const char *str);
fggl::util::GUID operator "" _fid(const char *str, std::size_t);
#endif //FGGL_UTIL_GUID_HPP
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 17/04/22.
//
......
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 27/06/22.
//
#ifndef FGGL_UTIL_SAFETY_HPP
#define FGGL_UTIL_SAFETY_HPP
#include <string>
namespace fggl::util {
/**
* A type-safe opaque handle.
*
* Lots of low-level libraries we use pass around handles as some primative type. It's fairly easy to accidentally
* mix these up. This wrapper's job is to make sure that mixing up handle types is impossible (and results in
* compiler errors).
*
* @tparam T the underling type of the handle.
* @tparam Tag a unique tag used to identify the handle type.
*/
template<typename T, typename Tag>
struct OpaqueName {
private:
T m_value;
public:
explicit constexpr OpaqueName(T value) : m_value(value) {}
constexpr OpaqueName() : m_value() {}
constexpr T get() const {
return m_value;
}
/**
* Check for equality of two handles.
*
* Two values are considered the same of the values contained inside them are considered equal, and both
* types share the same tagging interface.
*
* @param other the value being compared against.
* @return true iff the contained values are equal
*/
bool operator==(const OpaqueName<T, Tag> &other) const {
return m_value == other.m_value;
}
/**
* Check for equality of two handles.
*
* Two values are considered the same of the values contained inside them are considered equal, and both
* types share the same tagging interface.
*
* @param other the value being compared against.
* @return true iff the contained values are not equal
*/
bool operator!=(const OpaqueName<T, Tag> &other) const {
return m_value != other.m_value;
}
bool operator<(const OpaqueName<T, Tag> &other) const {
return m_value < other.m_value;
}
std::strong_ordering operator<=>(const OpaqueName<T, Tag> &other) const noexcept {
return m_value <=> other.m_value;
}
/**
* Generate a new tagged instance of a handle.
*/
constexpr static OpaqueName<T, Tag> make(T value) {
return OpaqueName<T, Tag>(value);
}
};
} // namespace fggl::util
#endif //FGGL_UTIL_SAFETY_HPP
//
// Created by webpigeon on 20/11/2021.
//
#ifndef FGGL_SERVICE_H
#define FGGL_SERVICE_H
#include <memory>
#include <typeindex>
#include <unordered_map>
#include <stdexcept>
#include <iostream>
namespace fggl::util {
class ServiceLocator {
inline static ServiceLocator *s_instance;
std::unordered_map<std::type_index, std::shared_ptr<void>> m_services;
ServiceLocator() = default;
public:
// don't allow copying or moving
ServiceLocator(ServiceLocator &other) = delete;
ServiceLocator(ServiceLocator &&other) = delete;
static ServiceLocator &instance() {
if (s_instance == nullptr) {
s_instance = new ServiceLocator();
}
return *s_instance;
}
template<typename T>
std::shared_ptr<T> supply(std::shared_ptr<T> ptr) {
auto info = std::type_index(typeid(T));
m_services[info] = ptr;
return ptr;
}
template<typename T>
std::shared_ptr<T> get() {
try {
auto info = std::type_index(typeid(T));
return std::static_pointer_cast<T>(m_services.at(info));
} catch ( std::out_of_range& e ){
std::cerr << "someone requested a service that doesn't exist!" << std::endl;
return nullptr;
}
}
template<typename T>
std::shared_ptr<T> providePtr() {
return get<T>();
}
};
}
#endif //FGGL_SERVICE_H
/**
* FOSS Galaxy Game Library
* Copyright (c) 2022 Joseph Walton-Rivers
* webpigeon@fossgalaxy.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
/*
* This file is part of FGGL.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef FGGL_UTIL_STATES_H
#define FGGL_UTIL_STATES_H
#ifndef FGGL_UTIL_STATES_HPP
#define FGGL_UTIL_STATES_HPP
#include <cassert>
#include <string>
#include <memory>
#include <unordered_map>
#include "fggl/debug/logging.hpp"
namespace fggl::util {
template<typename S, typename I>
......@@ -54,16 +50,20 @@ namespace fggl::util {
return (T *) (m_states[name].get());
}
void change(const Identifer &name) {
assertm(m_states.find(name) != m_states.end(), "state does not exist");
bool change(const Identifer &name) {
if (m_states.find(name) == m_states.end()) {
debug::error("attempted to change to non-existent state {}, ignoring you.", name);
return false;
}
active().deactivate();
m_active = name;
active().activate();
return true;
}
StateType &active() const {
assertm(m_states.find(m_active) != m_states.end(), "active state does not exist!");
ASSERT_MSG(m_states.find(m_active) != m_states.end(), "active state does not exist!");
return *(m_states.at(m_active).get());
}
......
// dear imgui: Renderer Backend for modern OpenGL with shaders / programmatic pipeline
// - Desktop GL: 2.x 3.x 4.x
// - Embedded GL: ES 2.0 (WebGL 1.0), ES 3.0 (WebGL 2.0)
// This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..)
// Implemented features:
// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID!
// [x] Renderer: Desktop GL only: Support for large meshes (64k+ vertices) with 16-bit indices.
// You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
// Read online: https://github.com/ocornut/imgui/tree/master/docs
// About Desktop OpenGL function loaders:
// Modern Desktop OpenGL doesn't have a standard portable header file to load OpenGL function pointers.
// Helper libraries are often used for this purpose! Here we are supporting a few common ones (gl3w, glew, glad).
// You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own.
// About GLSL version:
// The 'glsl_version' initialization parameter should be NULL (default) or a "#version XXX" string.
// On computer platform the GLSL version default to "#version 130". On OpenGL ES 3 platform it defaults to "#version 300 es"
// Only override if your GL version doesn't handle this GLSL version. See GLSL version table at the top of imgui_impl_opengl3.cpp.
#pragma once
#include "imgui.h" // IMGUI_IMPL_API
// Backend API
IMGUI_IMPL_API bool ImGui_ImplOpenGL3_Init(const char* glsl_version = NULL);
IMGUI_IMPL_API void ImGui_ImplOpenGL3_Shutdown();
IMGUI_IMPL_API void ImGui_ImplOpenGL3_NewFrame();
IMGUI_IMPL_API void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data);
// (Optional) Called by Init/NewFrame/Shutdown
IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateFontsTexture();
IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyFontsTexture();
IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateDeviceObjects();
IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects();
// Specific OpenGL ES versions
//#define IMGUI_IMPL_OPENGL_ES2 // Auto-detected on Emscripten
//#define IMGUI_IMPL_OPENGL_ES3 // Auto-detected on iOS/Android
// Attempt to auto-detect the default Desktop GL loader based on available header files.
// If auto-detection fails or doesn't select the same GL loader file as used by your application,
// you are likely to get a crash in ImGui_ImplOpenGL3_Init().
// You can explicitly select a loader by using one of the '#define IMGUI_IMPL_OPENGL_LOADER_XXX' in imconfig.h or compiler command-line.
#if !defined(IMGUI_IMPL_OPENGL_ES2) \
&& !defined(IMGUI_IMPL_OPENGL_ES3) \
&& !defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) \
&& !defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) \
&& !defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) \
&& !defined(IMGUI_IMPL_OPENGL_LOADER_GLAD2) \
&& !defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2) \
&& !defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3) \
&& !defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM)
// Try to detect GLES on matching platforms
#if defined(__APPLE__)
#include "TargetConditionals.h"
#endif
#if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) || (defined(__ANDROID__))
#define IMGUI_IMPL_OPENGL_ES3 // iOS, Android -> GL ES 3, "#version 300 es"
#elif defined(__EMSCRIPTEN__)
#define IMGUI_IMPL_OPENGL_ES2 // Emscripten -> GL ES 2, "#version 100"
// Otherwise try to detect supported Desktop OpenGL loaders..
#elif defined(__has_include)
#if __has_include(<GL/glew.h>)
#define IMGUI_IMPL_OPENGL_LOADER_GLEW
#elif __has_include(<glad/glad.h>)
#define IMGUI_IMPL_OPENGL_LOADER_GLAD
#elif __has_include(<glad/gl.h>)
#define IMGUI_IMPL_OPENGL_LOADER_GLAD2
#elif __has_include(<GL/gl3w.h>)
#define IMGUI_IMPL_OPENGL_LOADER_GL3W
#elif __has_include(<glbinding/glbinding.h>)
#define IMGUI_IMPL_OPENGL_LOADER_GLBINDING3
#elif __has_include(<glbinding/Binding.h>)
#define IMGUI_IMPL_OPENGL_LOADER_GLBINDING2
#else
#error "Cannot detect OpenGL loader!"
#endif
#else
#define IMGUI_IMPL_OPENGL_LOADER_GL3W // Default to GL3W embedded in our repository
#endif
#endif
# Integration Modules
This adds support for 3rd party libraries to the engine. These can be included in projects
to expose modules which can then be used in your applications.
## Integrations Provided
The following integration modules are povided in this folder. For more information please see the readme for that module.
| Integration | Module Name | Description |
|-------------|-----------------------|----------------------------------------------------|
| Bullet | `fggl::phys::Bullet3` | Provides Bullet3 integration into the game engine. |
| Lua | `fggl::script::Lua` | Provides LUA scripting support for the engine |
Modules that cannot be included for legal reasons will be packaged independently of this repository.
\ No newline at end of file
# bullet integration support
find_package( Bullet CONFIG )
if ( NOT Bullet_FOUND )
message(WARNING "Bullet not found - disabling bullet physics integration")
else()
message( STATUS "Bullet is poorly packaged, you might need to disable support for it" )
add_library(fgglbt STATIC)
if ( MSVC )
# see https://github.com/microsoft/vcpkg/issues/7877
target_link_libraries(fgglbt PUBLIC LinearMath Bullet3Common BulletDynamics BulletSoftBody BulletCollision BulletInverseDynamics)
else()
# FIXME: this shouldn't be necessary, for modern cmake, linking the libraries should be enough
target_compile_definitions(fgglbt PUBLIC ${BULLET_DEFINITIONS})
if ( BULLET_INCLUDE_DIRS STREQUAL "include/bullet" )
message( STATUS "Bullet include path is relative - hard-coding" )
# FIXME possible debian packing bug: path is relative in BulletConfig.cmake
# FIXME debian packaging bug: BulletConfig.cmake lists BulletInverseDynamics, but that's packaged in bullet-extras
target_include_directories(fgglbt PUBLIC ${BULLET_ROOT_DIR}/${BULLET_INCLUDE_DIRS})
else()
target_include_directories(fgglbt PUBLIC ${BULLET_INCLUDE_DIRS})
endif()
target_link_libraries(fgglbt PUBLIC ${BULLET_LIBRARIES})
endif()
target_link_libraries( fgglbt PUBLIC fggl )
target_include_directories( fgglbt
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
$<INSTALL_INTERFACE:include>
)
# bullet cpp files
target_sources( fgglbt
PRIVATE
src/module.cpp
src/simulation.cpp
src/phys_draw.cpp
src/service.cpp
)
endif()
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 24/04/22.
//
#ifndef FGGL_PHYS_BULLET_BULLET_HPP
#define FGGL_PHYS_BULLET_BULLET_HPP
#define FGGL_MODULE_BULLET fggl::phys::Bullet3
#include "fggl/phys/types.hpp"
#include "fggl/phys/bullet/types.hpp"
#include "fggl/phys/bullet/module.hpp"
#endif //FGGL_PHYS_BULLET_BULLET_HPP
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 31/07/22.
//
#ifndef FGGL_INTEGRATIONS_BULLET_PHYS_BULLET_MODULE_HPP
#define FGGL_INTEGRATIONS_BULLET_PHYS_BULLET_MODULE_HPP
#include "fggl/modules/module.hpp"
#include "fggl/entity/module.hpp"
#include "fggl/phys/types.hpp"
#include "fggl/phys/service.hpp"
#include "fggl/phys/bullet/types.hpp"
namespace fggl::phys::bullet {
constexpr util::GUID CONFIG_PHYS_BODY = util::make_guid("phys::body");
struct Bullet {
constexpr static const char* name = "fggl::phys::bullet";
constexpr static const std::array<modules::ModuleService, 1> provides = {
phys::PhysicsProvider::service
};
constexpr static const std::array<modules::ModuleService, 1> depends = {
entity::EntityFactory::service
};
static bool factory(modules::ModuleService name, modules::Services& serviceManager);
};
} // namespace fggl::phys::bullet
namespace fggl::phys {
using Bullet3 = bullet::Bullet;
} // namespace fggl::phys
#endif //FGGL_INTEGRATIONS_BULLET_PHYS_BULLET_MODULE_HPP
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 11/06/22.
//
#ifndef FGGL_PHYS_BULLET_MOTION_HPP
#define FGGL_PHYS_BULLET_MOTION_HPP
#include "types.hpp"
#include "fggl/debug/logging.hpp"
namespace fggl::phys::bullet {
class FgglMotionState : public btMotionState {
public:
FgglMotionState(entity::EntityManager* world, entity::EntityID entity) : m_world(world), m_entity(entity) {
}
~FgglMotionState() override = default;
void getWorldTransform(btTransform& worldTrans) const override {
const auto* transform = m_world->tryGet<fggl::math::Transform>(m_entity);
if ( transform == nullptr ) {
debug::error("BT: attempted to get transform of entity without transform component.");
return;
}
worldTrans.setFromOpenGLMatrix(glm::value_ptr(transform->model()));
}
void setWorldTransform(const btTransform& worldTrans) override {
auto& transform = m_world->get<fggl::math::Transform>(m_entity);
// set position
auto btOrigin = worldTrans.getOrigin();
transform.origin( {btOrigin.x(), btOrigin.y(), btOrigin.z()} );
// set rotation
math::vec3 angles;
worldTrans.getRotation().getEulerZYX(angles.x, angles.y, angles.z);
transform.euler(angles);
}
private:
entity::EntityManager* m_world;
entity::EntityID m_entity;
};
} // namespace fggl::phys::bullet
#endif //FGGL_PHYS_BULLET_INTEGRATIONS_HPP
/*
* ${license.title}
* Copyright (C) 2022 ${license.owner}
* ${license.mailto}
* This file is part of FGGL.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 30/05/22.
//
#ifndef FGGL_PHYS_BULLET_DEBUG_DRAW_H
#define FGGL_PHYS_BULLET_DEBUG_DRAW_H
#ifndef FGGL_PHYS_BULLET_PHYS_DRAW_HPP
#define FGGL_PHYS_BULLET_PHYS_DRAW_HPP
# if WIN32
#include "bullet/LinearMath/btIDebugDraw.h"
# else
#include "LinearMath/btIDebugDraw.h"
# endif
#include "LinearMath/btIDebugDraw.h"
#include "fggl/gfx/ogl/types.hpp"
#include <array>
......@@ -63,4 +62,4 @@ namespace fggl::debug {
};
}
#endif //FGGL_PHYS_BULLET_DEBUG_DRAW_H
#endif //FGGL_PHYS_BULLET_PHYS_DRAW_HPP
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 01/08/22.
//
#ifndef FGGL_INTEGRATIONS_BULLET_PHYS_BULLET_BULLETSERVICE_H
#define FGGL_INTEGRATIONS_BULLET_PHYS_BULLET_BULLETSERVICE_H
#include "fggl/phys/service.hpp"
namespace fggl::phys::bullet {
class BulletProvider : public phys::PhysicsProvider {
public:
PhysicsEngine* create(entity::EntityManager* entityManager, entity::EntityFactory* factory) override;
};
}
#endif //FGGL_INTEGRATIONS_BULLET_PHYS_BULLET_BULLETSERVICE_H
/*
* ${license.title}
* Copyright (C) 2022 ${license.owner}
* ${license.mailto}
* This file is part of FGGL.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
......@@ -25,9 +33,8 @@
#ifndef FGGL_PHYS_BULLET_TYPES_HPP
#define FGGL_PHYS_BULLET_TYPES_HPP
#include "fggl/ecs3/ecs.hpp"
#include "fggl/phys/types.hpp"
#include "fggl/phys/bullet/phys_draw.hpp"
#include "phys_draw.hpp"
#include <bullet/btBulletDynamicsCommon.h>
#include <bullet/btBulletCollisionCommon.h>
......@@ -65,34 +72,52 @@ namespace fggl::phys::bullet {
*/
class BulletPhysicsEngine : public PhysicsEngine {
public:
explicit BulletPhysicsEngine(ecs3::World* world);
explicit BulletPhysicsEngine(entity::EntityManager* world);
~BulletPhysicsEngine() override;
// copy is not allowed
BulletPhysicsEngine(const BulletPhysicsEngine&) = delete;
BulletPhysicsEngine& operator=(const BulletPhysicsEngine&) = delete;
// move is not allowed
BulletPhysicsEngine(BulletPhysicsEngine&&) = delete;
BulletPhysicsEngine& operator=(BulletPhysicsEngine&&) = delete;
void step() override;
void onEntityDeath(ecs::entity_t entity);
void onEntityDeath(entity::EntityID entity);
std::vector<ContactPoint> scanCollisions(entity::EntityID entity) override;
entity::EntityID raycast(math::vec3 from, math::vec3 to) override;
std::vector<entity::EntityID> raycastAll(math::vec3 from, math::vec3 to) override;
std::vector<entity::EntityID> sweep(PhyShape& shape, math::Transform& from, math::Transform& to) override;
std::vector<ContactPoint> scanCollisions(ecs3::entity_t entity) override;
ecs3::entity_t raycast(math::vec3 from, math::vec3 to) override;
std::vector<ecs3::entity_t> raycastAll(math::vec3 from, math::vec3 to) override;
std::vector<ecs3::entity_t> sweep(PhyShape& shape, math::Transform& from, math::Transform& to) override;
/**
* Add a rigid body to the physics world.
*
* The body should not currently be in the simulation. Physics position will be synced with the motion
* state prior to it being added to the simulation. This method should not need to be called manually, it
* is called when the entity is decoded.
*
* @param entity the entity the BulletBody is attached to.
* @param body the pre-configured bullet body.
*/
void addBody(entity::EntityID entity, BulletBody& body);
void setDebugDraw(bool enabled) override;
private:
ecs3::World* m_ecs;
entity::EntityManager* m_ecs;
BulletConfiguration m_config;
btDiscreteDynamicsWorld* m_world;
std::unique_ptr<debug::BulletDebugDrawList> m_debug;
ContactCache m_contactCache;
/**
* Check for ECS components which aren't in the physics world and add them.
*/
void checkForPhys();
/**
* Sync the bullet world state back to the ECS.
* Force position and rotation in the ECS to match those from the physics world.
*
* As FGGL uses motion states, this should not be required. As it iterates all entities in a scene this is
* also a very expensive operation.
*/
void forceSyncToECS();
void syncTransformToECS();
/**
* Deal with physics collisions
......