Files
Seele/src/Engine/MinimalEngine.h
T

167 lines
7.1 KiB
C++
Raw Normal View History

#pragma once
2020-04-01 02:17:49 +02:00
#include "EngineTypes.h"
2024-01-16 19:24:49 +01:00
#include <assert.h>
#include <cstddef>
#include <memory>
2024-06-09 12:20:04 +02:00
#include <utility>
2024-06-09 12:20:04 +02:00
#define DEFINE_REF(x) \
typedef ::Seele::RefPtr<x> P##x; \
typedef ::Seele::UniquePtr<x> UP##x; \
2023-11-01 23:12:30 +01:00
typedef ::Seele::OwningPtr<x> O##x;
2024-06-09 12:20:04 +02:00
#define DECLARE_REF(x) \
class x; \
typedef ::Seele::RefPtr<x> P##x; \
typedef ::Seele::UniquePtr<x> UP##x; \
2023-11-01 23:12:30 +01:00
typedef ::Seele::OwningPtr<x> O##x;
2020-03-02 19:07:49 +01:00
2024-06-09 12:20:04 +02:00
#define DECLARE_NAME_REF(nmsp, x) \
namespace nmsp { \
class x; \
typedef RefPtr<x> P##x; \
typedef UniquePtr<x> UP##x; \
typedef OwningPtr<x> O##x; \
2021-10-23 00:22:35 +02:00
}
2020-05-05 01:51:13 +02:00
2024-06-09 12:20:04 +02:00
namespace Seele {
2025-08-10 19:16:55 +02:00
template <typename T> class OwningPtr;
2024-06-09 12:20:04 +02:00
template <typename T> class RefPtr {
public:
constexpr RefPtr() noexcept : object(nullptr) {}
constexpr RefPtr(std::nullptr_t) noexcept : object(nullptr) {}
2025-08-10 19:16:55 +02:00
constexpr RefPtr(OwningPtr<T>&&) = delete;
2024-06-09 12:20:04 +02:00
RefPtr(T* ptr) : object(ptr) {}
constexpr RefPtr(const RefPtr& other) noexcept : object(other.object) {}
constexpr RefPtr(RefPtr&& rhs) noexcept : object(std::move(rhs.object)) { rhs.object = nullptr; }
template <typename F> constexpr RefPtr<F> cast() {
2023-11-05 10:36:01 +01:00
T* t = object;
F* f = dynamic_cast<F*>(t);
2024-06-09 12:20:04 +02:00
if (f == nullptr) {
2021-10-23 00:22:35 +02:00
return nullptr;
}
2023-11-01 23:12:30 +01:00
return RefPtr<F>(f);
2021-10-23 00:22:35 +02:00
}
2024-06-09 12:20:04 +02:00
template <typename F> constexpr const RefPtr<F> cast() const {
2023-11-05 10:36:01 +01:00
T* t = object;
F* f = dynamic_cast<F*>(t);
2024-06-09 12:20:04 +02:00
if (f == nullptr) {
2023-11-05 10:36:01 +01:00
return nullptr;
}
return RefPtr<F>(f);
}
2025-08-10 19:16:55 +02:00
constexpr RefPtr& operator=(OwningPtr<T>&&) = delete;
2024-06-09 12:20:04 +02:00
constexpr RefPtr& operator=(const RefPtr& other) {
if (this != &other) {
2021-10-23 00:22:35 +02:00
object = other.object;
}
return *this;
}
2024-06-09 12:20:04 +02:00
constexpr RefPtr& operator=(RefPtr&& rhs) noexcept {
if (this != &rhs) {
2021-10-23 00:22:35 +02:00
object = std::move(rhs.object);
rhs.object = nullptr;
}
return *this;
}
2025-08-10 19:16:55 +02:00
constexpr RefPtr& operator=(std::nullptr_t) noexcept {
object = nullptr;
return *this;
}
2024-06-09 12:20:04 +02:00
constexpr ~RefPtr() {}
constexpr bool operator==(const RefPtr& rhs) const noexcept { return object == rhs.object; }
constexpr auto operator<=>(const RefPtr& rhs) const noexcept { return object <=> rhs.object; }
template <typename F> constexpr bool operator==(const RefPtr<F>& rhs) const noexcept { return object == rhs.getHandle(); }
template <typename F> constexpr auto operator<=>(const RefPtr<F>& rhs) const noexcept { return object <=> rhs.getHandle(); }
template <typename F> constexpr operator RefPtr<F>() { return RefPtr<F>(static_cast<F*>(object)); }
template <typename F> constexpr operator RefPtr<F>() const { return RefPtr<F>(static_cast<F*>(object)); }
constexpr T* operator->() { return object; }
constexpr const T* operator->() const { return object; }
constexpr T* getHandle() { return object; }
constexpr const T* getHandle() const { return object; }
private:
2023-11-01 23:12:30 +01:00
T* object;
2020-05-05 01:51:13 +02:00
};
2025-08-10 19:16:55 +02:00
template <typename T> class OwningPtr {
2024-06-09 12:20:04 +02:00
public:
OwningPtr() : pointer(nullptr) {}
OwningPtr(T* ptr) : pointer(ptr) {}
template <typename F> OwningPtr(OwningPtr<F>&& other) {
2023-11-05 10:36:01 +01:00
pointer = dynamic_cast<T*>(*other);
2023-11-07 16:55:13 +01:00
other.clear();
2023-11-05 10:36:01 +01:00
}
OwningPtr(const OwningPtr& other) = delete;
2024-06-09 12:20:04 +02:00
OwningPtr(OwningPtr&& other) noexcept {
2023-11-05 10:36:01 +01:00
pointer = other.pointer;
other.pointer = nullptr;
}
OwningPtr& operator=(const OwningPtr& other) = delete;
2024-06-09 12:20:04 +02:00
OwningPtr& operator=(OwningPtr&& other) noexcept {
if (this != &other) {
if (pointer != nullptr) {
2025-08-10 19:16:55 +02:00
std::default_delete<T> d;
d(pointer);
2023-11-06 14:47:21 +01:00
}
2023-11-05 10:36:01 +01:00
pointer = other.pointer;
2023-11-07 16:55:13 +01:00
other.pointer = nullptr;
2023-11-05 10:36:01 +01:00
}
return *this;
}
2025-08-10 19:16:55 +02:00
~OwningPtr() {
std::default_delete<T> d;
d(pointer);
}
2025-06-30 21:15:14 +02:00
constexpr operator RefPtr<T>() { return RefPtr<T>(pointer); }
constexpr operator RefPtr<T>() const { return RefPtr<T>(pointer); }
2024-06-09 12:20:04 +02:00
constexpr T* operator->() { return pointer; }
constexpr const T* operator->() const { return pointer; }
constexpr T* operator*() { return pointer; }
constexpr const T* operator*() const { return pointer; }
constexpr bool operator==(std::nullptr_t) const noexcept { return pointer == nullptr; }
constexpr auto operator<=>(const OwningPtr& rhs) const noexcept { return pointer <=> rhs.pointer; }
2023-11-07 16:55:13 +01:00
// INTERNAL USE
2024-06-09 12:20:04 +02:00
constexpr void clear() { pointer = nullptr; }
private:
2023-11-05 10:36:01 +01:00
friend class RefPtr<T>;
T* pointer;
};
2024-06-09 12:20:04 +02:00
template <typename T> class UniquePtr {
public:
UniquePtr() : handle(nullptr) {}
UniquePtr(std::nullptr_t) : handle(nullptr) {}
UniquePtr(T* ptr) : handle(ptr) {}
UniquePtr(const UniquePtr& rhs) = delete;
UniquePtr(UniquePtr&& rhs) noexcept : handle(rhs.handle) { rhs.handle = nullptr; }
UniquePtr& operator=(const UniquePtr& rhs) = delete;
UniquePtr& operator=(UniquePtr&& rhs) {
if (this != &rhs) {
2023-11-05 10:36:01 +01:00
handle = rhs.handle;
rhs.handle = nullptr;
}
2021-10-23 00:22:35 +02:00
return *this;
}
2024-06-09 12:20:04 +02:00
~UniquePtr() { delete handle; }
inline bool operator==(const UniquePtr& other) const { return handle == other.handle; }
inline bool operator!=(const UniquePtr& other) const { return handle != other.handle; }
inline T* operator->() { return handle; }
inline T* getHandle() { return handle; }
bool isValid() { return handle != nullptr; }
2020-05-05 01:51:13 +02:00
2024-06-09 12:20:04 +02:00
private:
T* handle;
2020-05-05 01:51:13 +02:00
};
2024-07-05 12:02:46 +02:00
// idk if it should be here?
struct Globals {
bool usePositionOnly = true;
bool useDepthCulling = true;
bool useLightCulling = true;
2025-07-12 13:50:31 +02:00
bool useImagebasedLighting = true;
2024-12-25 14:59:08 +01:00
bool useRayTracing = false;
2024-07-05 12:02:46 +02:00
bool running = true;
};
Globals& getGlobals();
2020-05-05 01:51:13 +02:00
} // namespace Seele