Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kissholic committed Jan 1, 2025
1 parent b52f76a commit c5e361f
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 9 deletions.
34 changes: 34 additions & 0 deletions Include/Base/Event/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,38 @@ class Event {
};


template<typename ObjType, typename ObjType::FuncType>
class ObjectEvent {
public:
void RegisterCallback(ObjType* Obj, ObjType::FuncType Func) noexcept {
auto node = std::make_unique<EventNode>(Obj, Func);
mCallbacks.push_back(std::move(node));
}

void UnregisterCallback(ObjType* Obj, ObjType::FuncType Func) noexcept {
for (auto& Callback : mCallbacks) {
if (Callback->mObj == Obj && Callback->mFunc == Func) {
mCallbacks.erase(Callback);
break;
}
}
}

void Invoke(auto&&... args) noexcept {
for (auto& Callback : mCallbacks) {
(Callback->mObj->*Callback->mFunc)(std::forward<decltype(args)>(args)...);
}
}


private:
struct EventNode {
ObjType* mObj;
ObjType::FuncType mFunc;
};

std::vector<std::unique_ptr<EventNode>> mCallbacks;
};


} // namespace be
24 changes: 24 additions & 0 deletions Include/Component.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* File: Component.h
* Author: kissholic
* Copyright (c) 2024 kissholic. All wrongs reserved.
*/

#pragma once

#include "Object.h"


namespace be {

class Component {
public:
Component() = default;
virtual ~Component() = default;

};


using ComponentRef = ObjectRefT<Component>;

} // namespace be
4 changes: 3 additions & 1 deletion Include/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
#pragma once

#include <memory>
#include "Input/InputSystem.h"
#include "Graphics/GraphicsContext.h"
#include "Input/InputSystem.h"
#include "Scene/Scene.h"

namespace be {

Expand Down Expand Up @@ -36,6 +37,7 @@ class BlockEngine final {

private:
std::unique_ptr<GraphicsContext> mGraphicsContext;
SceneRef mScene;
};


Expand Down
43 changes: 43 additions & 0 deletions Include/GameObject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* File: GameObject.h
* Author: kissholic
* Copyright (c) 2024 kissholic. All wrongs reserved.
*/

#pragma once

#include "Object.h"

namespace be {

struct GameObjectPosition {
float X;
float Y;
float Z;
};

struct GameObjectScale {
float X;
float Y;
};

class GameObject : public Object {
public:

virtual void Step([[maybe_unused]] double DeltaTime) noexcept {}

GameObjectPosition mPosition;
GameObjectScale mScale;
float mRotation;
};


template<typename T>
concept IsGameObject = std::is_base_of<GameObject, T>::value;


using GameObjectRef = ObjectRefT<GameObject>;



} // namespace be
4 changes: 3 additions & 1 deletion Include/Graphics/GraphicsContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include <string>

struct GLFWwindow;

namespace be {


Expand Down Expand Up @@ -39,7 +41,7 @@ class GraphicsContext {

private:
std::string mTitle;
struct GLFWwindow* mWindow;
GLFWwindow* mWindow;
int mWidth;
int mHeight;

Expand Down
92 changes: 85 additions & 7 deletions Include/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@

namespace be {


class Object;

template<typename T>
concept IsObject = std::is_base_of<Object, T>::value;

class Object {
template<IsObject T>
friend class ObjectRefT;

public:
Object() {}
virtual ~Object() {}

virtual void Step([[maybe_unused]] double DeltaTime) noexcept {}

uint32_t GetRefCount() const noexcept { return mRefCount; }

private:
Expand All @@ -32,10 +39,6 @@ class Object {
};


template<typename T>
concept IsObject = std::is_base_of<Object, T>::value;


class ObjectRef {
public:
ObjectRef(std::nullptr_t) noexcept : mObj(nullptr) {}
Expand Down Expand Up @@ -106,8 +109,83 @@ class ObjectRef {
};


template <IsObject T>
class ObjectRefT {
public:
ObjectRefT() noexcept : mObj(nullptr) {}
ObjectRefT(std::nullptr_t) noexcept : mObj(nullptr) {}

ObjectRefT(T* Obj) noexcept : mObj(Obj) {
if (mObj) {
mObj->IncRefCount();
}
}

ObjectRefT(const ObjectRefT& Ref) noexcept : mObj(Ref.mObj) {
if (mObj) {
mObj->IncRefCount();
}
}

~ObjectRefT() noexcept {
if (mObj) {
mObj->DecRefCount();
}
}

ObjectRefT& operator=(const ObjectRefT& Ref) noexcept {
if (this!= &Ref) {
if (mObj) {
mObj->DecRefCount();
}
mObj = Ref.mObj;
if (mObj) {
mObj->IncRefCount();
}
}
return *this;
}

ObjectRefT& operator=(T* Obj) noexcept {
if (mObj) {
mObj->DecRefCount();
}
mObj = Obj;
if (mObj) {
mObj->IncRefCount();
}
return *this;
}

ObjectRefT& operator=(ObjectRefT&& Ref) noexcept {
if (this!= &Ref) {
if (mObj) {
mObj->DecRefCount();
}
mObj = Ref.mObj;
Ref.mObj = nullptr;
}
return *this;
}

bool operator==(const ObjectRefT& Ref) const noexcept { return mObj == Ref.mObj; }
bool operator!=(const ObjectRefT& Ref) const noexcept { return mObj != Ref.mObj; }

T* Get() const noexcept { return mObj; }

T* operator->() const noexcept { return mObj; }
T* operator->() noexcept { return mObj; }

T& operator*() const noexcept { return *mObj; }
T& operator*() noexcept { return *mObj; }

private:
T* mObj;
};


template<IsObject T>
ObjectRef MakeObjectRef(auto&&... Args) noexcept {
ObjectRef MakeObject(auto&&... Args) noexcept {
return ObjectRef(new T(std::forward<decltype(Args)>(Args)...));
}

Expand Down
43 changes: 43 additions & 0 deletions Include/Scene/Level.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* File: Level.h
* Author: kissholic
* Copyright (c) 2024 kissholic. All wrongs reserved.
*/

#pragma once

#include <algorithm>
#include <string>
#include <vector>

#include "GameObject.h"

namespace be {


class Level : public GameObject {
public:
Level(const std::string& Name) : mName(Name) {}

virtual void Step(double DeltaTime) noexcept override {
for (auto& Obj : mGameObjects) {
Obj->Step(DeltaTime);
}
}

void AddGameObject(GameObjectRef Obj) noexcept {
mGameObjects.push_back(Obj);
}

void RemoveGameObject(GameObjectRef Obj) noexcept {
std::ranges::remove(mGameObjects, Obj);
}

private:
std::string mName;
std::vector<GameObjectRef> mGameObjects;
};

using LevelRef = ObjectRefT<Level>;

} // namespace be
34 changes: 34 additions & 0 deletions Include/Scene/Scene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* File: Scene.h
* Author: kissholic
* Copyright (c) 2024 kissholic. All wrongs reserved.
*/

#pragma once

#include <memory>
#include <vector>

#include "Level.h"


namespace be {


class Scene : public GameObject {
public:

virtual void Step(double DeltaTime) noexcept override {
for (auto& Level : mLevels) {
Level->Step(DeltaTime);
}
}

private:
std::vector<LevelRef> mLevels;
};

using SceneRef = ObjectRefT<Scene>;


} // namespace be
16 changes: 16 additions & 0 deletions Include/Sprite/Sprite.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* File: Sprite.h
* Author: kissholic
* Copyright (c) 2024 kissholic. All wrongs reserved.
*/

#pragma once

#include "GameObject.h"

namespace be {

class Sprite : public GameObject {
};

} // namespace be

0 comments on commit c5e361f

Please sign in to comment.