1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
#include "game.hpp"
#include <cstring>
GameSettings pkeSettings{};
const uint64_t consoleBufferCount = 30;
const uint64_t consoleLineLength = 128;
const long readLineLength = 128;
char readLine[readLineLength];
const char *levelName = "demo-level";
const char *PKE_FILE_BEGIN = ":PKFB:";
const char *PKE_FILE_END = ":PKFE:";
const char *PKE_FILE_VERSION = ":0:";
const char *PKE_FILE_OBJ_END = "";
const char *PKE_FILE_OBJ_ENTITY_TYPE = "EntityType:";
const char *PKE_FILE_ENTITY_TYPE_MODELS_DIR = "ModelsDir: ";
const char *PKE_FILE_ENTITY_TYPE_MODEL_FILE = "ModelFile: ";
const char *PKE_FILE_ENTITY_TYPE_ENTITY_TYPE_CODE = "EntityTypeCode: ";
const char *PKE_FILE_ENTITY_TYPE_ENTITY_HANDLE = "EntityHandle: ";
const char *PKE_FILE_ENTITY_TYPE_STARTING_INSTANCE_COUNT = "StartingInstanceCount: ";
const char *PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_VERTEX = "Importer_GLTF::AccessorIndexVertex: ";
const char *PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_NORMAL = "Importer_GLTF::AccessorIndexNormal: ";
const char *PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_UV = "Importer_GLTF::AccessorIndexUV: ";
const char *PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_INDEX = "Importer_GLTF::AccessorIndexIndex: ";
char consoleBuffer[consoleBufferCount][consoleLineLength];
long consoleBufferIndex = 0;
bool shouldCreateEntityType = false;
EntityType *entityTypeToCreate = nullptr;
void SerializeEntityType(std::ofstream &stream, const EntityType &et) {
char handleStr[19] = { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' };
snprintf(handleStr, 19, "0x%016lX",static_cast<EntityHandle_T>(et.entityHandle));
EntityType e{};
if (et.modelsDir != e.modelsDir)
stream << PKE_FILE_ENTITY_TYPE_MODELS_DIR << et.modelsDir << "\n";
if (et.modelFile != e.modelFile)
stream << PKE_FILE_ENTITY_TYPE_MODEL_FILE << et.modelFile << "\n";
if (et.entityTypeCode != e.entityTypeCode)
stream << PKE_FILE_ENTITY_TYPE_ENTITY_TYPE_CODE << et.entityTypeCode << "\n";
if (et.entityHandle != e.entityHandle)
stream << PKE_FILE_ENTITY_TYPE_ENTITY_HANDLE << handleStr << "\n";
if (et.startingInstanceCount != e.startingInstanceCount)
stream << PKE_FILE_ENTITY_TYPE_STARTING_INSTANCE_COUNT << et.startingInstanceCount << "\n";
if (et.Importer_GLTF.AccessorIndexVertex != e.Importer_GLTF.AccessorIndexVertex)
stream << PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_VERTEX << et.Importer_GLTF.AccessorIndexVertex << "\n";
if (et.Importer_GLTF.AccessorIndexNormal != e.Importer_GLTF.AccessorIndexNormal)
stream << PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_NORMAL << et.Importer_GLTF.AccessorIndexNormal << "\n";
if (et.Importer_GLTF.AccessorIndexUV != e.Importer_GLTF.AccessorIndexUV)
stream << PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_UV << et.Importer_GLTF.AccessorIndexUV << "\n";
if (et.Importer_GLTF.AccessorIndexIndex != e.Importer_GLTF.AccessorIndexIndex)
stream << PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_INDEX << et.Importer_GLTF.AccessorIndexIndex << "\n";
}
void ParseEntityType(std::ifstream &stream) {
EntityType et{};
while (stream.getline(readLine, readLineLength)) {
if (strcmp(PKE_FILE_OBJ_END, readLine) == 0) {
int64_t existingEntityTypeIndex = EntityType_FindByTypeCode(et.entityTypeCode);
if (existingEntityTypeIndex != -1) {
continue;
}
et.entityHandle = ECS_CreateEntity();
EntityType_Load(et);
GlobalEntityTypes.Push(et);
return;
}
if (strstr(readLine, PKE_FILE_ENTITY_TYPE_MODELS_DIR)) {
uint64_t prefixLen = strlen(PKE_FILE_ENTITY_TYPE_MODELS_DIR);
uint64_t len = strlen(readLine + prefixLen) + 1;
char *val = Pke_New<char>(len);
memset(reinterpret_cast<void *>(val), '\0', len);
memcpy(val, readLine + prefixLen, len);
et.modelsDir = val;
continue;
}
if (strstr(readLine, PKE_FILE_ENTITY_TYPE_MODEL_FILE)) {
uint64_t prefixLen = strlen(PKE_FILE_ENTITY_TYPE_MODEL_FILE);
uint64_t len = strlen(readLine + prefixLen) + 1;
char *val = Pke_New<char>(len);
memset(reinterpret_cast<void *>(val), '\0', len);
memcpy(val, readLine + prefixLen, len);
et.modelFile = val;
continue;
}
if (strstr(readLine, PKE_FILE_ENTITY_TYPE_ENTITY_TYPE_CODE)) {
uint64_t prefixLen = strlen(PKE_FILE_ENTITY_TYPE_ENTITY_TYPE_CODE);
uint64_t len = strlen(readLine + prefixLen) + 1;
char *val = Pke_New<char>(len);
memset(reinterpret_cast<void *>(val), '\0', len);
memcpy(val, readLine + prefixLen, len);
et.entityTypeCode = val;
continue;
}
if (strstr(readLine, PKE_FILE_ENTITY_TYPE_ENTITY_HANDLE)) {
// TODO parse from hex ?
uint64_t prefixLen = strlen(PKE_FILE_ENTITY_TYPE_ENTITY_HANDLE);
EntityHandle_T handle_t;
STR2NUM_ERROR result = str2num(handle_t, readLine + prefixLen);
assert(result == STR2NUM_ERROR::SUCCESS);
et.entityHandle = EntityHandle{handle_t};
continue;
}
if (strstr(readLine, PKE_FILE_ENTITY_TYPE_STARTING_INSTANCE_COUNT)) {
uint64_t prefixLen = strlen(PKE_FILE_ENTITY_TYPE_STARTING_INSTANCE_COUNT);
STR2NUM_ERROR result = str2num(et.startingInstanceCount, readLine + prefixLen);
assert(result == STR2NUM_ERROR::SUCCESS);
continue;
}
if (strstr(readLine, PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_VERTEX)) {
uint64_t prefixLen = strlen(PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_VERTEX);
STR2NUM_ERROR result = str2num(et.Importer_GLTF.AccessorIndexVertex, readLine + prefixLen);
assert(result == STR2NUM_ERROR::SUCCESS);
continue;
}
if (strstr(readLine, PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_NORMAL)) {
uint64_t prefixLen = strlen(PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_NORMAL);
STR2NUM_ERROR result = str2num(et.Importer_GLTF.AccessorIndexNormal, readLine + prefixLen);
assert(result == STR2NUM_ERROR::SUCCESS);
continue;
}
if (strstr(readLine, PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_UV)) {
uint64_t prefixLen = strlen(PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_UV);
STR2NUM_ERROR result = str2num(et.Importer_GLTF.AccessorIndexUV, readLine + prefixLen);
assert(result == STR2NUM_ERROR::SUCCESS);
continue;
}
if (strstr(readLine, PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_INDEX)) {
uint64_t prefixLen = strlen(PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_INDEX);
STR2NUM_ERROR result = str2num(et.Importer_GLTF.AccessorIndexIndex, readLine + prefixLen);
assert(result == STR2NUM_ERROR::SUCCESS);
continue;
}
}
}
void SaveSceneFile(const char *sceneFilePath) {
std::ofstream f(sceneFilePath);
assert(f.is_open());
f << PKE_FILE_BEGIN << "\n";
f << PKE_FILE_VERSION << "\n";
f << "" << "\n";
for (long i = 0; i < GlobalEntityTypes.Count(); ++i) {
f << PKE_FILE_OBJ_ENTITY_TYPE << "\n";
const auto &et = GlobalEntityTypes[i];
const CompGrBinds *grBinds = ECS_GetGrBinds(et.entityHandle);
// TODO ignore if no instances
if (grBinds == nullptr) {
continue;
}
SerializeEntityType(f, et);
f << PKE_FILE_OBJ_END << "\n";
}
f << PKE_FILE_END << "\n";
f.flush();
f.close();
}
void LoadSceneFile(const char *sceneFilePath) {
std::ifstream f(sceneFilePath);
assert(f.is_open());
memset(readLine, '\0', readLineLength);
while (f.getline(readLine, readLineLength)) {
// EntityTypes
if (strcmp(PKE_FILE_OBJ_ENTITY_TYPE, readLine) == 0) {
ParseEntityType(f);
continue;
}
}
f.close();
}
void Game_Tick(double delta) {
/*
* ECS_Tick() gets called first because it updates the public
* `EntitiesToBeRemoved` for all other ticks to use.
*/
ECS_Tick(delta);
if (shouldCreateEntityType) {
assert(entityTypeToCreate != nullptr);
assert(entityTypeToCreate != CAFE_BABE(EntityType));
assert(entityTypeToCreate->entityHandle == EntityHandle_MAX);
// TODO this needs to be more elegant
int64_t existingEntityTypeIndex = EntityType_FindByTypeCode(entityTypeToCreate->entityTypeCode);
if (existingEntityTypeIndex == -1) {
entityTypeToCreate->entityHandle = ECS_CreateEntity();
EntityType_Load(*entityTypeToCreate);
GlobalEntityTypes.Push(*entityTypeToCreate);
}
Pke_Delete<EntityType>(entityTypeToCreate);
shouldCreateEntityType = false;
}
}
void RecordImGuiEditorWrapper() {
ImGui::DockSpaceOverViewport(nullptr, ImGuiDockNodeFlags_PassthruCentralNode);
ImGui::BeginMainMenuBar();
if (ImGui::BeginMenu("File")) {
if (ImGui::MenuItem("Exit")) {
glfwSetWindowShouldClose(window, true);
}
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
void RecordImGuiConsole() {
static bool scrollToBottom = true;
if (!ImGui::Begin("Console", &pkeSettings.editorSettings.isShowingConsole)) {
ImGui::End();
return;
}
ImVec2 region = ImGui::GetContentRegionAvail();
region.y -= 27;
if (ImGui::BeginListBox("##ConsoleHistory", region)) {
for (long i = consoleBufferIndex + 1; i < consoleBufferCount; ++i) {
ImGui::Text("%s", consoleBuffer[i]);
}
for (long i = 0; i < consoleBufferIndex; ++i) {
ImGui::Text("%s", consoleBuffer[i]);
}
if (scrollToBottom) ImGui::SetScrollHereY(1);
scrollToBottom = false;
ImGui::EndListBox();
}
ImGui::Separator();
if (ImGui::InputText("##ConsoleInput", consoleBuffer[consoleBufferIndex], consoleLineLength, ImGuiInputTextFlags_EnterReturnsTrue)) {
// TODO parse and execute.
scrollToBottom = true;
consoleBufferIndex = (consoleBufferIndex + 1) % consoleBufferCount;
memset(consoleBuffer[consoleBufferIndex], '\0', consoleLineLength);
}
auto focusedFlags = (ImGuiFocusedFlags_ChildWindows);
if (ImGui::IsWindowFocused(focusedFlags) && !ImGui::IsAnyItemFocused() && !ImGui::IsAnyItemHovered() && !ImGui::IsMouseClicked(ImGuiMouseButton_Left) && !ImGui::IsMouseClicked(ImGuiMouseButton_Left, true)) {
ImGui::SetKeyboardFocusHere(-1);
}
ImGui::End();
}
void RecordImGuiEntityList() {
if (!ImGui::Begin("EntityList", &pkeSettings.editorSettings.isShowingEntityList)) {
ImGui::End();
return;
}
static ImGuiTableFlags tableFlags{
ImGuiTableFlags_Borders |
ImGuiTableFlags_RowBg
};
if (ImGui::BeginTable("Entities", 5, tableFlags)) {
ImGui::TableSetupColumn("EntityHandle");
ImGui::TableSetupColumn("ParentEntityHandle");
ImGui::TableSetupColumn("GrBindsHandle");
ImGui::TableSetupColumn("InstanceHandle");
ImGui::TableSetupColumn("IsMarkedForRemoval");
ImGui::TableHeadersRow();
uint64_t bucketCount = ECS_GetEntities_BucketCount();
for (long bucket = 0; bucket <= bucketCount; ++bucket) {
uint64_t itemCount = 0;
auto *entities = ECS_GetEntities(bucket, itemCount);
for (long row = 0; row < itemCount; row++) {
auto *entity = &entities[row];
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::Text("0x%016lX", static_cast<EntityHandle_T>(entity->handle));
ImGui::TableSetColumnIndex(1);
ImGui::Text("0x%016lX", static_cast<EntityHandle_T>(entity->parentHandle));
ImGui::TableSetColumnIndex(2);
ImGui::Text("0x%016lX", static_cast<GrBindsHandle_T>(entity->grBindsHandle));
ImGui::TableSetColumnIndex(3);
ImGui::Text("0x%016lX", static_cast<InstanceHandle_T>(entity->instanceHandle));
ImGui::TableSetColumnIndex(4);
ImGui::Text("%u", entity->isMarkedForRemoval);
}
}
ImGui::EndTable();
}
ImGui::End();
}
void RecordImGuiModalCreateEntityType() {
if (ImGui::BeginPopupModal("CreateEntityType", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
static bool needsReset = true;
static char modelsDir[64];
static char modelFile[64];
static char entityTypeCode[32];
if (needsReset) {
memset(modelsDir, '\0', 64);
memset(modelFile, '\0', 64);
memset(entityTypeCode, '\0', 32);
entityTypeToCreate = Pke_New<EntityType>();
needsReset = false;
}
ImGui::InputText("Models Dir", modelsDir, 63);
ImGui::InputText("Model File", modelFile, 63);
ImGui::InputText("Entity Type Code", entityTypeCode, 31);
ImGui::InputScalar("Starting Instance Count", ImGuiDataType_U32, &entityTypeToCreate->startingInstanceCount);
ImGui::InputScalar("GLTF Import Index - Vertex", ImGuiDataType_S16, &entityTypeToCreate->Importer_GLTF.AccessorIndexVertex);
ImGui::InputScalar("GLTF Import Index - Normal", ImGuiDataType_S16, &entityTypeToCreate->Importer_GLTF.AccessorIndexNormal);
ImGui::InputScalar("GLTF Import Index - UV", ImGuiDataType_S16, &entityTypeToCreate->Importer_GLTF.AccessorIndexUV);
ImGui::InputScalar("GLTF Import Index - Index", ImGuiDataType_S16, &entityTypeToCreate->Importer_GLTF.AccessorIndexIndex);
ImGui::Separator();
if (ImGui::Button("Create")) {
// TODO some type of validation
char *sModelsDir = Pke_New<char>(strlen(modelsDir) + 1);
strncpy(sModelsDir, modelsDir, 63);
entityTypeToCreate->modelsDir = sModelsDir;
char *sModelFile = Pke_New<char>(strlen(modelFile) + 1);
strncpy(sModelFile, modelFile, 63);
entityTypeToCreate->modelFile = sModelFile;
char *sEntityTypeCode = Pke_New<char>(strlen(entityTypeCode) + 1);
strncpy(sEntityTypeCode, entityTypeCode, 31);
entityTypeToCreate->entityTypeCode = sEntityTypeCode;
shouldCreateEntityType = true;
needsReset = true;
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if (ImGui::Button("Cancel")) {
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
}
void RecordImGuiSceneEditor() {
{
if (!ImGui::Begin("SceneEditorEntityTypes", &pkeSettings.editorSettings.isShowingSceneEditor)) {
ImGui::End();
return;
}
if (ImGui::Button("Create Entity Type")) {
ImGui::OpenPopup("CreateEntityType");
}
if (ImGui::Button("Save")) {
SaveSceneFile("test.pstf");
}
if (ImGui::Button("Load")) {
LoadSceneFile("test.pstf");
}
RecordImGuiModalCreateEntityType();
ImGui::End();
}
}
void RecordImGuiEditor() {
if (pkeSettings.isShowingEditor) {
RecordImGuiEditorWrapper();
RecordImGuiConsole();
RecordImGuiEntityList();
RecordImGuiSceneEditor();
}
}
void Game_Init() {
for (long i = 0; i < consoleBufferCount; ++i) {
memset(consoleBuffer[i], '\0', consoleLineLength);
}
Event_RegisterCallback("RenderImGui", RecordImGuiEditor);
}
|