Implemented basic containers and ref pointers

This commit is contained in:
HOEGLER Stefan
2020-02-25 00:45:20 +01:00
parent 4c491a9378
commit a14237d6ce
31 changed files with 1212 additions and 36 deletions
+2
View File
@@ -0,0 +1,2 @@
add_subdirectory(Engine/)
+8
View File
@@ -0,0 +1,8 @@
target_sources(SeeleEngine
PRIVATE
MinimalEngine.h
main.cpp)
add_subdirectory(Graphics/)
add_subdirectory(Math/)
add_subdirectory(Containers/)
+281
View File
@@ -0,0 +1,281 @@
#pragma once
#include "MinimalEngine.h"
#include <assert.h>
#ifndef DEFAULT_ALLOC_SIZE
#define DEFAULT_ALLOC_SIZE 16
#endif
namespace Seele
{
template<typename T>
struct Array
{
public:
Array()
: allocated(DEFAULT_ALLOC_SIZE)
, arraySize(0)
{
_data = new T[DEFAULT_ALLOC_SIZE];
refreshIterators();
}
Array(size_t size, T value = T())
: allocated(size)
, arraySize(size)
{
_data = new T[size];
for (int i = 0; i < size; ++i)
{
_data[i] = value;
}
refreshIterators();
}
~Array()
{
delete _data;
_data = nullptr;
}
template<typename X>
class IteratorBase {
public:
typedef std::forward_iterator_tag iterator_category;
typedef X value_type;
typedef std::ptrdiff_t difference_type;
typedef X& reference;
typedef X* pointer;
IteratorBase(X* x = nullptr)
: p(x)
{}
IteratorBase(const IteratorBase& i)
: p(i.p)
{}
reference operator*() const
{
return *p;
}
pointer operator->() const
{
return p;
}
inline bool operator!=(const IteratorBase& other)
{
return p != other.p;
}
inline bool operator==(const IteratorBase& other)
{
return p == other.p;
}
inline bool operator-(const IteratorBase& other)
{
return p - other.p;
}
IteratorBase& operator++() {
p++;
return *this;
}
IteratorBase operator++(int) {
IteratorBase tmp(*this);
++*this;
return tmp;
}
private:
X* p;
};
typedef IteratorBase<T> Iterator;
typedef IteratorBase<const T> ConstIterator;
Iterator find(const T& item)
{
for (int i = 0; i < arraySize; ++i)
{
if (_data[i] == item)
{
return Iterator(&_data[i]);
}
}
return endIt;
}
Iterator begin()
{
return beginIt;
}
Iterator end()
{
return endIt;
}
T& add(const T& item)
{
if (arraySize == allocated)
{
allocated += DEFAULT_ALLOC_SIZE;
T* tempArray = new T[allocated];
std::memcpy(tempArray, _data, arraySize * sizeof(T));
delete _data;
_data = tempArray;
}
_data[arraySize++] = item;
refreshIterators();
return _data[arraySize - 1];
}
void remove(Iterator it, bool keepOrder = true)
{
remove(it - beginIt, keepOrder);
}
void remove(int index, bool keepOrder = true)
{
if (keepOrder)
{
std::memcpy(&_data[index], &_data[index + 1], sizeof(T) * (arraySize - index));
}
else
{
_data[index] = _data[arraySize - 1];
}
arraySize--;
}
void clear()
{
arraySize = 0;
allocated = 0;
refreshIterators();
}
void resize(uint32 newSize)
{
if (newSize < allocated)
{
arraySize = newSize;
}
else
{
T* newData = new T[newSize];
allocated = newSize;
std::memcpy(newData, _data, sizeof(T) * arraySize);
arraySize = newSize;
delete _data;
_data = newData;
}
refreshIterators();
}
inline uint32 size() const
{
return arraySize;
}
inline uint32 capacity() const
{
return allocated;
}
inline T* data() const
{
return _data;
}
T& back() const
{
return _data[arraySize - 1];
}
T& operator[](int index) const
{
assert(index >= 0 && index < arraySize);
return _data[index];
}
private:
void refreshIterators()
{
beginIt = Iterator(_data);
endIt = Iterator(_data + arraySize);
}
uint32 arraySize;
uint32 allocated;
Iterator beginIt;
Iterator endIt;
T* _data;
};
template<typename T, uint32 N>
struct StaticArray
{
public:
StaticArray()
{
beginIt = Iterator(_data);
endIt = Iterator(_data + N);
}
StaticArray(T value)
{
for (int i = 0; i < N; ++i)
{
_data[i] = value;
}
beginIt = Iterator(_data);
endIt = Iterator(_data + N);
}
~StaticArray()
{}
inline uint32 size() const
{
return N;
}
inline T* data() const
{
return _data;
}
T& operator[](int index)
{
assert(index >= 0 && index < N);
return _data[index];
}
template<typename X>
class IteratorBase {
public:
typedef std::forward_iterator_tag iterator_category;
typedef X value_type;
typedef std::ptrdiff_t difference_type;
typedef X& reference;
typedef X* pointer;
IteratorBase(X* x = nullptr)
: p(x)
{}
IteratorBase(const IteratorBase& i)
: p(i.p)
{}
reference operator*() const
{
return *p;
}
pointer operator->() const
{
return p;
}
inline bool operator!=(const IteratorBase& other)
{
return p != other.p;
}
inline bool operator==(const IteratorBase& other)
{
return p == other.p;
}
IteratorBase& operator++() {
p++;
return *this;
}
IteratorBase operator++(int) {
IteratorBase tmp(*this);
++*this;
return tmp;
}
private:
X* p;
};
typedef IteratorBase<T> Iterator;
typedef IteratorBase<const T> ConstIterator;
private:
T _data[N];
Iterator beginIt;
Iterator endIt;
};
}
+3
View File
@@ -0,0 +1,3 @@
target_sources(SeeleEngine
PRIVATE
Array.h)
+262
View File
@@ -0,0 +1,262 @@
#pragma once
#include "Array.h"
namespace Seele
{
template<typename K, typename V>
struct Pair
{
public:
Pair()
: key(K())
, value(V())
{}
Pair(K key, V value)
: key(key)
, value(value)
{}
K key;
V value;
};
template<typename K, typename V>
struct Map
{
private:
struct Node
{
Pair<K, V> pair;
Node* leftChild;
Node* rightChild;
Node(const K& key)
: leftChild(nullptr)
, rightChild(nullptr)
, pair(key, V())
{}
Node()
: leftChild(nullptr)
, rightChild(nullptr)
, pair(K(), V())
{}
};
public:
Map()
: root(nullptr)
{}
~Map()
{}
V& operator[](const K& key)
{
root = splay(root, key);
if (root == nullptr || root->pair.key < key || key < root->pair.key)
{
root = insert(root, key);
}
refreshIterators();
return root->pair.value;
}
class Iterator {
public:
typedef std::bidirectional_iterator_tag iterator_category;
typedef Pair<K, V> value_type;
typedef std::ptrdiff_t difference_type;
typedef Pair<K, V>& reference;
typedef Pair<K, V>* pointer;
Iterator(Node* x = nullptr)
: node(x)
{
}
Iterator(const Iterator& i)
: node(i.node)
{}
reference operator*() const
{
return node->pair;
}
pointer operator->() const
{
return &node->pair;
}
inline bool operator!=(const Iterator& other)
{
return node != other.node;
}
inline bool operator==(const Iterator& other)
{
return node == other.node;
}
Iterator& operator++() {
node++;
return *this;
}
Iterator& operator--() {
node--;
return *this;
}
Iterator operator--(int) {
Iterator tmp(*this);
++* this;
return tmp;
}
Iterator operator++(int) {
Iterator tmp(*this);
++* this;
return tmp;
}
private:
Node* node;
};
Iterator find(const K& key)
{
root = splay(root, key);
if (root == nullptr || root->pair.key != key)
{
return endIt;
}
return Iterator(root);
}
bool exists(const K& key)
{
return find(key) != endIt;
}
Iterator begin() const
{
return beginIt;
}
Iterator end() const
{
return endIt;
}
private:
void refreshIterators()
{
beginIt = Iterator(&nodes[0]);
endIt = Iterator(&nodes[0] + nodes.size());
}
Node* root;
Array<Node> nodes;
Iterator beginIt;
Iterator endIt;
Node* rotateRight(Node* node)
{
Node* y = node->leftChild;
node->leftChild = y->rightChild;
y->rightChild = node;
return y;
}
Node* rotateLeft(Node* node)
{
Node* y = node->rightChild;
node->rightChild = y->leftChild;
y->leftChild = node;
return y;
}
Node* makeNode(const K& key)
{
return &nodes.add(Node(key));
}
Node* insert(Node* root, const K& key)
{
if (root == nullptr) return makeNode(key);
root = splay(root, key);
if (!(root->pair.key < key || key < root->pair.key)) return root;
Node* newNode = makeNode(key);
if (key < root->pair.key)
{
newNode->rightChild = root;
newNode->leftChild = root->leftChild;
root->leftChild = nullptr;
}
else
{
newNode->leftChild = root;
newNode->rightChild = root->rightChild;
root->rightChild = nullptr;
}
return newNode;
}
Node* remove(Node* root, const K& key)
{
Node* temp;
if (!root)
return nullptr;
root = splay(root, key);
if (key != root->pair.key)
return root;
if (!root->leftChild)
{
temp = root;
root = root->rightChild;
}
else
{
temp = root;
root = splay(root->leftChild, key);
root->rightChild = temp->rightChild;
}
nodes.remove(temp);
delete temp;
return root;
}
Node* splay(Node* root, const K& key)
{
if (root == nullptr || !(root->pair.key < key || key < root->pair.key))
{
return root;
}
if (key < root->pair.key)
{
if (root->leftChild == nullptr)
return root;
if (key < root->leftChild->pair.key)
{
root->leftChild->leftChild = splay(root->leftChild->leftChild, key);
root = rotateRight(root);
}
else if (root->leftChild->pair.key < key)
{
root->leftChild->rightChild = splay(root->leftChild->rightChild, key);
if (root->leftChild->rightChild != nullptr)
{
root->leftChild = rotateLeft(root->leftChild);
}
}
return (root->leftChild == nullptr) ? root : rotateRight(root);
}
else
{
if (root->rightChild == nullptr)
return root;
if (key < root->rightChild->pair.key)
{
root->rightChild->leftChild = splay(root->rightChild->leftChild, key);
if (root->rightChild->leftChild != nullptr)
{
root->rightChild = rotateRight(root->rightChild);
}
}
else if (root->rightChild->pair.key < key)
{
root->rightChild->rightChild = splay(root->rightChild->rightChild, key);
root = rotateLeft(root);
}
return (root->rightChild == nullptr) ? root : rotateLeft(root);
}
}
};
}
+12
View File
@@ -0,0 +1,12 @@
target_sources(SeeleEngine
PRIVATE
Graphics.h
Graphics.cpp
View.h
WindowManager.h
WindowManager.cpp
Window.h
Window.cpp
GraphicsResources.h)
add_subdirectory(Vulkan/)
+12 -3
View File
@@ -1,11 +1,20 @@
#include "Graphics.h"
#include <iostream>
#include <map>
Graphics::Graphics()
Seele::Graphics::Graphics()
{
Array<uint8> arr;
arr.add('2');
arr.add('4');
std::cout << "Test" << std::endl;
for (auto a : arr)
{
std::cout << "Element: " << a << std::endl;
}
}
Graphics::~Graphics()
Seele::Graphics::~Graphics()
{
}
+28 -6
View File
@@ -1,6 +1,28 @@
class Graphics
{
public:
Graphics();
~Graphics();
};
#pragma once
#include "MinimalEngine.h"
#include "GraphicsResources.h"
namespace Seele {
class Window;
class Graphics
{
public:
static Graphics& getInstance()
{
static Graphics instance;
return instance;
}
void init(GraphicsInitializer initializer);
void beginFrame();
void endFrame();
//Singleton
private:
Graphics();
Graphics(Graphics const&) = delete;
void operator=(Graphics const&) = delete;
~Graphics();
};
}
+9
View File
@@ -0,0 +1,9 @@
#pragma once
namespace Seele
{
struct GraphicsInitializer
{
const char* windowLayoutFile;
};
}
+10
View File
@@ -0,0 +1,10 @@
#pragma once
#include "Window.h"
// A view is a part of the window, which can be anything from a viewport to an editor
class View
{
public:
private:
Window::Section* owner;
};
+12
View File
@@ -0,0 +1,12 @@
#include "Window.h"
#include "Graphics.h"
#include <iostream>
Seele::Window::Window(const WindowCreateInfo& createInfo)
{
}
Seele::Window::~Window()
{
}
+39
View File
@@ -0,0 +1,39 @@
#pragma once
#include "MinimalEngine.h"
namespace Seele {
struct WindowCreateInfo
{
int32 width;
int32 height;
const char* title;
};
class Window
{
public:
Window(const WindowCreateInfo& createInfo);
~Window();
private:
// A window is divided into 5 sections, using a border layout
// +--------------TOP------------------+
// | |
// L R
// E I
// F CENTER G
// T H
// | T
// +-------------BOTTOM----------------+
// In every section, there can be any number
// of views, when there are multiple, they stack to tabs
struct Section
{
};
Section top;
Section bot;
Section left;
Section right;
Section center;
uint32 width;
uint32 height;
};
}
+16
View File
@@ -0,0 +1,16 @@
#include "WindowManager.h"
Seele::WindowManager::WindowManager(GraphicsInitializer initializer)
{
//TODO Parse layout file
WindowCreateInfo mainWindowInfo;
mainWindowInfo.title = "SeeleEngine";
mainWindowInfo.width = 1280;
mainWindowInfo.height = 720;
Window* mainWindow = new Window(mainWindowInfo);
windows.add(mainWindow);
}
Seele::WindowManager::~WindowManager()
{
}
+16
View File
@@ -0,0 +1,16 @@
#pragma once
#include "GraphicsResources.h"
#include "Window.h"
#include "Containers/Array.h"
namespace Seele
{
class WindowManager
{
public:
WindowManager(GraphicsInitializer initializer);
~WindowManager();
private:
Array<RefPtr<Window>> windows;
};
}
+5
View File
@@ -0,0 +1,5 @@
target_sources(SeeleEngine
PRIVATE
Math.h
Matrix.h
Vector.h)
View File
+10
View File
@@ -0,0 +1,10 @@
#pragma once
#include <glm/mat2x2.hpp>
#include <glm/mat3x3.hpp>
#include <glm/mat4x4.hpp>
namespace Seele {
typedef glm::mat2 Matrix2;
typedef glm::mat3 Matrix3;
typedef glm::mat4 Matrix4;
}
+17
View File
@@ -0,0 +1,17 @@
#pragma once
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>
namespace Seele {
typedef glm::vec2 Vector2;
typedef glm::vec3 Vector3;
typedef glm::vec4 Vector4;
typedef glm::uvec2 UVector2;
typedef glm::uvec3 UVector3;
typedef glm::uvec4 UVector4;
typedef glm::ivec2 IVector2;
typedef glm::ivec3 IVector3;
typedef glm::ivec4 IVector4;
}
+129
View File
@@ -0,0 +1,129 @@
#pragma once
#include <stdint.h>
#include <array>
#include <memory>
#include <atomic>
namespace Seele
{
typedef uint64_t uint64;
typedef uint32_t uint32;
typedef uint16_t uint16;
typedef uint8_t uint8;
typedef int64_t int64;
typedef int32_t int32;
typedef int16_t int16;
typedef int8_t int8;
template<typename T>
class RefPtr
{
public:
RefPtr(T* ptr)
{
object = new RefObject(ptr);
}
RefPtr(const RefPtr& other)
: object(other.object)
{
object->addRef();
}
RefPtr& operator=(const RefPtr& other)
{
if (this != &other)
{
object->removeRef();
object = other->object;
object->addRef();
}
return *this;
}
~RefPtr()
{
object->removeRef();
}
T* operator->()
{
return object->getHandle();
}
private:
class RefObject
{
public:
RefObject(T* ptr)
: handle(ptr)
, refCount(1)
{
}
RefObject(const RefObject& rhs)
: handle(rhs.handle)
, refCount(rhs.refCount)
{
}
~RefObject()
{}
void addRef()
{
refCount++;
}
void removeRef()
{
refCount--;
if (refCount.load() <= 0)
{
delete handle;
handle = nullptr;
delete this;
}
}
inline T* getHandle() const
{
return handle;
}
private:
T* handle;
std::atomic_uint64_t refCount;
};
RefObject* object;
};
template<typename T>
class UniquePtr
{
public:
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)
{
handle = rhs.handle;
rhs.handle = nullptr;
return *this;
}
~UniquePtr()
{
delete handle;
}
T* operator->()
{
return handle;
}
bool isValid()
{
return handle != nullptr;
}
private:
T* handle;
};
}
+4 -1
View File
@@ -7,6 +7,7 @@
#include <vulkan/vulkan.h>
#include <glm/vec4.hpp>
#include <GLFW/glfw3.h>
#include "Graphics/Graphics.h"
/////////////////////////////////////////////////////////////
// gps coordinate
@@ -44,7 +45,7 @@ int main() {
// create class instance
const gps_position g(35, 59, 24.567f);
// save data to archive
// save _data to archive
{
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
@@ -65,7 +66,9 @@ int main() {
std::cout << "Hello World! " << std::endl;
VkInstance instance;
glm::vec4 vector(0, 1, 0, 1);
Seele::Graphics& graphics = Seele::Graphics::getInstance();
std::cout << vector.y << std::endl;
std::cout << glfwInit() << std::endl;
return 0;
}