Skip to content

Commit f466424

Browse files
Merge pull request #10 from codingminecraft/multiplayer
Update to PreRelease 0.6
2 parents 3324ceb + d7c7886 commit f466424

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+7171
-2331
lines changed

.gitmodules

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
[submodule "Minecraft/vendor/yamlCpp"]
55
path = Minecraft/vendor/yamlCpp
66
url = https://github.com/jbeder/yaml-cpp
7-
[submodule "Minecraft/vendor/simplex"]
8-
path = Minecraft/vendor/simplex
9-
url = https://github.com/SRombauts/SimplexNoise
107
[submodule "Minecraft/vendor/cppUtils"]
118
path = Minecraft/vendor/cppUtils
129
url = https://github.com/ambrosiogabe/CppUtils
@@ -25,3 +22,6 @@
2522
[submodule "Minecraft/vendor/enet"]
2623
path = Minecraft/vendor/enet
2724
url = https://github.com/lsalzman/enet
25+
[submodule "Minecraft/vendor/FastNoiseLite"]
26+
path = Minecraft/vendor/FastNoiseLite
27+
url = https://github.com/Auburn/FastNoiseLite

Minecraft/include/core.h

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <glm/vec4.hpp>
1111
#include <glm/mat4x4.hpp>
1212
#include <glm/gtc/type_ptr.hpp>
13+
#include <glm/gtc/integer.hpp>
1314
#include <glm/gtx/string_cast.hpp>
1415
#include <glm/gtx/norm.hpp>
1516
#include <glm/gtx/rotate_vector.hpp>
@@ -50,12 +51,13 @@
5051
// stb
5152
#include <stb/stb_image.h>
5253
#include <stb/stb_write.h>
54+
#include <stb/stb_image_resize.h>
5355

5456
// Yaml
5557
#include <yaml-cpp/yaml.h>
5658

57-
// Simplex
58-
#include <SimplexNoise.h>
59+
// Fast Noise
60+
#include <FastNoiseLite.h>
5961

6062
// Freetype
6163
#include <freetype/freetype.h>
@@ -64,11 +66,42 @@
6466
#include <magic_enum.hpp>
6567

6668
// Optick
67-
//#define OPTICK_ENABLE_GPU_D3D12 0
68-
//#define OPTICK_ENABLE_GPU_VULKAN 0
69-
//#include <optick.h>
69+
#ifdef _USE_OPTICK
70+
#define OPTICK_ENABLE_GPU_D3D12 0
71+
#define OPTICK_ENABLE_GPU_VULKAN 0
72+
#include <optick.h>
73+
#endif
7074

7175
// User defined literals
7276
glm::vec4 operator""_hex(const char* hexColor, size_t length);
7377

78+
struct RawMemory
79+
{
80+
uint8* data;
81+
size_t size;
82+
size_t offset;
83+
84+
void init(size_t initialSize);
85+
void free();
86+
void shrinkToFit();
87+
void resetReadWriteCursor();
88+
89+
void writeDangerous(const uint8* data, size_t dataSize);
90+
void readDangerous(uint8* data, size_t dataSize);
91+
92+
template<typename T>
93+
void write(const T* data)
94+
{
95+
writeDangerous((uint8*)data, sizeof(T));
96+
}
97+
98+
template<typename T>
99+
void read(T* data)
100+
{
101+
readDangerous((uint8*)data, sizeof(T));
102+
}
103+
104+
void setCursor(size_t offset);
105+
};
106+
74107
#endif

Minecraft/include/core/Application.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace Minecraft
66
{
77
struct Window;
88
struct Framebuffer;
9+
class GlobalThreadPool;
910

1011
namespace Application
1112
{
@@ -16,8 +17,10 @@ namespace Minecraft
1617
void takeScreenshot(const char* filename = "", bool mustBeSquare = false);
1718

1819
Window& getWindow();
19-
2020
Framebuffer& getMainFramebuffer();
21+
GlobalThreadPool& getGlobalThreadPool();
22+
23+
extern float deltaTime;
2124
}
2225
}
2326

Minecraft/include/core/Components.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ namespace Minecraft
88
{
99
None,
1010
Player,
11-
Camera
11+
Camera,
12+
13+
// TODO: Temporary remove this
14+
RandomEntity,
1215
};
1316

1417
struct Tag

Minecraft/include/core/Ecs.h

Lines changed: 134 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ namespace Minecraft
8282
return (index / sparseSetPoolSize) * sparseSetPoolSize;
8383
}
8484

85-
inline SparseSetPool* getPool(EntityIndex index)
85+
inline SparseSetPool* getPool(EntityIndex index) const
8686
{
8787
// TODO: Change this to a hashmap or something for O(1) lookup time
8888
// For now do a simple linear search through the pools
@@ -101,6 +101,7 @@ namespace Minecraft
101101
template<typename T>
102102
inline T* get(EntityIndex index)
103103
{
104+
// TODO: Use the other get function
104105
SparseSetPool* pool = getPool(index);
105106
if (!pool)
106107
{
@@ -113,6 +114,20 @@ namespace Minecraft
113114
return (T*)(data + denseArrayIndex * componentSize);
114115
}
115116

117+
inline uint8* get(EntityIndex index)
118+
{
119+
SparseSetPool* pool = getPool(index);
120+
if (!pool)
121+
{
122+
g_logger_error("Invalid entity '%d' for component '%d'", index, componentId);
123+
return nullptr;
124+
}
125+
126+
EntityIndex denseArrayIndex = pool->entities[index - pool->startIndex];
127+
g_logger_assert((denseArrayIndex < numComponents&& denseArrayIndex >= 0), "Invalid dense array index.");
128+
return (uint8*)(data + denseArrayIndex * componentSize);
129+
}
130+
116131
template<typename T>
117132
inline void add(EntityId entity, const T& component)
118133
{
@@ -159,6 +174,51 @@ namespace Minecraft
159174
numComponents++;
160175
}
161176

177+
inline void add(EntityId entity)
178+
{
179+
EntityIndex index = getEntityIndex(entity);
180+
SparseSetPool* pool = getPool(index);
181+
if (!pool)
182+
{
183+
const int newNumPools = numPools + 1;
184+
SparseSetPool* newPools = (SparseSetPool*)g_memory_realloc(pools, newNumPools * sizeof(SparseSetPool));
185+
if (!newPools)
186+
{
187+
g_logger_error("Failed to allocate memory for new sparse set pool for component '%d'", componentId);
188+
return;
189+
}
190+
numPools = newNumPools;
191+
pools = newPools;
192+
pools[numPools - 1].init();
193+
pools[numPools - 1].startIndex = getPoolAlignedIndex(index);
194+
pool = &pools[numPools - 1];
195+
}
196+
197+
uint32 nextIndex = numComponents;
198+
if (nextIndex >= maxNumComponents)
199+
{
200+
int newMaxNumComponents = maxNumComponents * 2;
201+
char* newComponentMemory = (char*)g_memory_realloc(data, componentSize * newMaxNumComponents);
202+
EntityIndex* newEntityMemory = (EntityIndex*)g_memory_realloc(entities, sizeof(EntityIndex) * newMaxNumComponents);
203+
if (!newComponentMemory || !newEntityMemory)
204+
{
205+
// Just free both of the reallocs if it ever fails
206+
g_memory_free(newComponentMemory);
207+
g_memory_free(newEntityMemory);
208+
g_logger_error("Failed to allocate new memory for component pool or entities for component '%d'", componentId);
209+
return;
210+
}
211+
data = newComponentMemory;
212+
entities = newEntityMemory;
213+
maxNumComponents = newMaxNumComponents;
214+
}
215+
216+
pool->entities[index - pool->startIndex] = nextIndex;
217+
g_memory_zeroMem(data + nextIndex * componentSize, componentSize);
218+
entities[nextIndex] = index;
219+
numComponents++;
220+
}
221+
162222
template<typename T>
163223
inline T* addOrGet(EntityId entity)
164224
{
@@ -170,7 +230,17 @@ namespace Minecraft
170230
return get<T>(Internal::getEntityIndex(entity));
171231
}
172232

173-
inline bool exists(EntityId entity)
233+
inline uint8* addOrGet(EntityId entity)
234+
{
235+
if (!exists(entity))
236+
{
237+
add(entity);
238+
}
239+
240+
return get(Internal::getEntityIndex(entity));
241+
}
242+
243+
inline bool exists(EntityId entity) const
174244
{
175245
const EntityIndex index = getEntityIndex(entity);
176246
SparseSetPool* pool = getPool(index);
@@ -263,6 +333,7 @@ namespace Minecraft
263333
std::vector<EntityId> entities;
264334
std::vector<Internal::SparseSet> componentSets;
265335
std::vector<EntityIndex> freeEntities;
336+
std::vector<std::string> debugComponentNames;
266337

267338
EntityId createEntity()
268339
{
@@ -279,8 +350,14 @@ namespace Minecraft
279350
return entities.back();
280351
}
281352

353+
RawMemory serialize();
354+
355+
void deserialize(RawMemory& memory);
356+
282357
void free();
283358

359+
int numComponents(EntityId entity) const;
360+
284361
void clear()
285362
{
286363
entities.clear();
@@ -289,28 +366,40 @@ namespace Minecraft
289366
set.free();
290367
}
291368
componentSets.clear();
369+
componentSets = std::vector<Internal::SparseSet>();
370+
debugComponentNames.clear();
292371
freeEntities.clear();
293372
}
294373

374+
template<typename T>
375+
void registerComponent(const char* debugName)
376+
{
377+
int32 compId = Ecs::componentId<T>();
378+
379+
// TODO: Maybe do something different here?
380+
EntityIndex index = 0;
381+
382+
g_logger_assert(compId == componentSets.size(), "Tried to register component '%s' twice.", debugName);
383+
componentSets.emplace_back(Internal::SparseSet::defaultSet<T>(index));
384+
g_logger_assert(compId < Internal::MaxNumComponents, "Exceeded the maximum number of components, you can increase this if needed.");
385+
debugComponentNames.emplace_back(std::string(debugName));
386+
}
387+
295388
template<typename T>
296389
T& addComponent(EntityId id)
297390
{
298391
int32 componentId = Ecs::componentId<T>();
299392
const EntityIndex index = Internal::getEntityIndex(id);
300393

301-
if (componentSets.size() <= componentId)
302-
{
303-
componentSets.resize(componentId + 1, Internal::SparseSet::defaultSet<T>(index));
304-
g_logger_assert(componentId < Internal::MaxNumComponents, "Exceeded the maximum number of components, you can increase this if needed.");
305-
}
394+
g_logger_assert(componentId < componentSets.size(), "You need to register all components in the same order *everywhere*. Component '%s' was not registered.", typeid(T).name());
306395

307396
// Get or add the component at this index
308397
T& component = *componentSets[componentId].addOrGet<T>(index);
309398

310399
return component;
311400
}
312401

313-
bool validEntity(EntityId id)
402+
bool validEntity(EntityId id) const
314403
{
315404
return Internal::getEntityIndex(id) < entities.size() && !isNull(id);
316405
}
@@ -321,7 +410,7 @@ namespace Minecraft
321410
return hasComponentById(id, componentId<T>());
322411
}
323412

324-
bool hasComponentById(EntityId id, int32 componentId)
413+
bool hasComponentById(EntityId id, int32 componentId) const
325414
{
326415
if (!validEntity(id))
327416
{
@@ -331,19 +420,53 @@ namespace Minecraft
331420

332421
if (componentId >= componentSets.size() || componentId < 0)
333422
{
334-
g_logger_warning("Tried to check if an entity had component '%d', but a component of type '%d' does not exist in the registry.", componentId, componentId);
423+
g_logger_warning("Tried to check if an entity had component '%d', but a component of type '%d' does not exist in the registry which only has '%d' components.", componentId, componentId, componentSets.size());
335424
return false;
336425
}
337426

338427
return componentSets[componentId].exists(id);
339428
}
340429

430+
uint8* getComponentById(EntityId id, int32 componentId)
431+
{
432+
if (!validEntity(id))
433+
{
434+
g_logger_error("Cannot check if invalid entity %d has a component.", Internal::getEntityIndex(id));
435+
return nullptr;
436+
}
437+
438+
if (componentId >= componentSets.size() || componentId < 0)
439+
{
440+
g_logger_warning("Tried to check if an entity had component '%d', but a component of type '%d' does not exist in the registry.", componentId, componentId);
441+
return nullptr;
442+
}
443+
444+
return componentSets[componentId].get(Internal::getEntityIndex(id));
445+
}
446+
447+
uint8* addOrGetComponentById(EntityId id, int32 componentId)
448+
{
449+
if (!validEntity(id))
450+
{
451+
g_logger_error("Cannot check if invalid entity %d has a component.", Internal::getEntityIndex(id));
452+
return nullptr;
453+
}
454+
455+
if (componentId >= componentSets.size() || componentId < 0)
456+
{
457+
g_logger_warning("Tried to check if an entity had component '%d', but a component of type '%d' does not exist in the registry.", componentId, componentId);
458+
return nullptr;
459+
}
460+
461+
return componentSets[componentId].addOrGet(id);
462+
}
463+
341464
template<typename T>
342465
T& getComponent(EntityId id)
343466
{
344467
const EntityIndex index = Internal::getEntityIndex(id);
345468
int32 compId = Ecs::componentId<T>();
346-
g_logger_assert(hasComponent<T>(id), "Entity '%d' does not have component '%d'", id, compId);
469+
g_logger_assert(hasComponent<T>(id), "Entity '%d' does not have component '%s'", id, debugComponentNames[compId].c_str());
347470

348471
if (compId >= componentSets.size() || compId < 0)
349472
{

0 commit comments

Comments
 (0)