-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffbeat_gui.h
More file actions
518 lines (461 loc) · 19.5 KB
/
offbeat_gui.h
File metadata and controls
518 lines (461 loc) · 19.5 KB
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#pragma once
#include "offbeat.h"
#include "ui.h"
#include "file_io.h"
#define OFFBEAT_FRAME_COUNT 60
static char* g_EmissionShapeStrings[OFFBEAT_EmissionCount] = {
"Point",
"Ring",
"Disk",
"Square",
"Sphere",
"Sphere Volume",
"Cube Volume",
};
static char* g_EmissionVelocityStrings[OFFBEAT_VelocityCount] = {
"Random",
"Cone",
};
static char* g_MotionPrimitiveStrings[OFFBEAT_MotionCount] = {
"None",
"Point",
"Line",
"Sphere",
};
static char* g_FunctionStrings[OFFBEAT_FunctionCount] = {
"Const",
"Lerp",
"Max Lerp",
"Triangle",
"Two Triangles",
"Four Triangles",
"Step",
"Periodic",
"Periodic Time",
"Periodic Square",
"Periodic Square Time",
};
static char* g_ParameterStrings[OFFBEAT_ParameterCount] = {
"Age",
"Velocity",
"ID",
"Random",
"CollisionCount",
"CameraDistance",
};
static uint32_t g_TextureCount;
static char g_TextureStrings[OFFBEAT_PARTICLE_SYSTEM_COUNT][50];
static uint32_t g_CurrentFrame;
static uint32_t g_TotalParticleCounts[OFFBEAT_FRAME_COUNT];
static int64_t g_CycleCounts[OFFBEAT_FRAME_COUNT];
static float g_MilisecondCounts[OFFBEAT_FRAME_COUNT];
const char*
OffbeatGUITextureString(const void* Data, int Index)
{
return g_TextureStrings[Index];
}
const char*
OffbeatGUIPathArrayToString(const void* Data, int Index)
{
path* Paths = (path*)Data;
return Paths[Index].Name;
}
void
OffbeatGUIAddTexture(Resource::resource_manager* Resources, char* Name, char* Path)
{
strcpy(g_TextureStrings[g_TextureCount++], Name);
rid TextureRID;
if(!Resources->GetTexturePathRID(&TextureRID, Path))
{
TextureRID = Resources->RegisterTexture(Path);
}
OffbeatAddTexture(Resources->GetTexture(TextureRID));
}
void
OffbeatGUIAddFrameInfo(uint32_t TotalParticleCount, int64_t CycleCount, float MilisecondCount)
{
g_TotalParticleCounts[g_CurrentFrame] = TotalParticleCount;
g_CycleCounts[g_CurrentFrame] = CycleCount;
g_MilisecondCounts[g_CurrentFrame] = MilisecondCount;
g_CurrentFrame = (g_CurrentFrame + 1) % OFFBEAT_FRAME_COUNT;
}
void
OffbeatExportCurrentParticleSystem(char* Path)
{
ob_file_data FileData = OffbeatPackCurrentParticleSystemBytes();
Platform::WriteEntireFile(Path, FileData.Size, FileData.Data);
}
void
OffbeatImportParticleSystem(game_state* GameState, char* Path)
{
GameState->TemporaryMemStack->NullifyClear();
debug_read_file_result ReadResult = Platform::ReadEntireFile(GameState->TemporaryMemStack, Path);
OffbeatAssert(ReadResult.Contents);
ob_particle_system NewParticleSystem = {};
// TODO(rytis): Check if unpack succeeded.
OffbeatUnpackParticleSystem(&NewParticleSystem, ReadResult.Contents);
OffbeatAddParticleSystem(&NewParticleSystem);
}
static void
OffbeatDragF32(const char* Label, ov4* Value, float MinValue, float MaxValue, float ScreenDelta)
{
UI::DragFloat(Label, Value->E, MinValue, MaxValue, ScreenDelta);
}
static void
OffbeatDragOV3(const char* Label, ov4* Value, float MinValue, float MaxValue, float ScreenDelta)
{
UI::DragFloat3(Label, Value->E, MinValue, MaxValue, ScreenDelta);
}
static void
OffbeatDragOV4(const char* Label, ov4* Value, float MinValue, float MaxValue, float ScreenDelta)
{
UI::DragFloat4(Label, Value->E, MinValue, MaxValue, ScreenDelta);
}
#define OffbeatGUIExpression(type) void OffbeatGUIExpression##type(const char* Name, ob_expr* Expression, bool* Header, bool MinZero, float ScreenDelta = 5.0f)\
{\
char FunctionName[50];\
char ParameterName[50];\
char FreqName[50];\
char MaxValName[50];\
char StepName[50];\
char LowName[50];\
char HighName[50];\
\
strcpy(FunctionName, Name);\
strcat(FunctionName, " Function");\
strcpy(ParameterName, Name);\
strcat(ParameterName, " Parameter");\
strcpy(FreqName, Name);\
strcat(FreqName, " Frequency");\
strcpy(MaxValName, Name);\
strcat(MaxValName, " Max Value");\
strcpy(StepName, Name);\
strcat(StepName, " Step Count");\
strcpy(LowName, Name);\
strcat(LowName, " Low");\
strcpy(HighName, Name);\
strcat(HighName, " High");\
\
if(UI::CollapsingHeader(Name, Header))\
{\
UI::Combo(FunctionName, (int*)&Expression->Function, g_FunctionStrings, OFFBEAT_FunctionCount, UI::StringArrayToString);\
\
if(Expression->Function == OFFBEAT_FunctionConst)\
{\
if(MinZero)\
{\
OffbeatDrag##type(HighName, &Expression->High, 0.0f, INFINITY, ScreenDelta);\
}\
else\
{\
OffbeatDrag##type(HighName, &Expression->High, -INFINITY, INFINITY, ScreenDelta);\
}\
}\
else\
{\
if(Expression->Function == OFFBEAT_FunctionPeriodicTime ||\
Expression->Function == OFFBEAT_FunctionPeriodicSquareTime)\
{\
UI::DragFloat(FreqName, &Expression->Float, -INFINITY, INFINITY, 5.0f);\
}\
else\
{\
if(Expression->Function ==OFFBEAT_FunctionMaxLerp)\
{\
UI::DragFloat(MaxValName, &Expression->Float, 0.0f, INFINITY, 10.0f);\
}\
else if(Expression->Function == OFFBEAT_FunctionPeriodic ||\
Expression->Function == OFFBEAT_FunctionPeriodicSquare)\
{\
UI::DragFloat(FreqName, &Expression->Float, -INFINITY, INFINITY, 5.0f);\
}\
else if(Expression->Function == OFFBEAT_FunctionStep)\
{\
UI::DragFloat(MaxValName, &Expression->Float, 0.0f, INFINITY, 10.0f);\
UI::SliderInt(StepName, (int32_t*)&Expression->Uint, 1, 30);\
}\
UI::Combo(ParameterName, (int*)&Expression->Parameter, g_ParameterStrings, OFFBEAT_ParameterCount, UI::StringArrayToString);\
}\
\
if(MinZero)\
{\
OffbeatDrag##type(LowName, &Expression->Low, 0.0f, INFINITY, ScreenDelta);\
OffbeatDrag##type(HighName, &Expression->High, 0.0f, INFINITY, ScreenDelta);\
}\
else\
{\
OffbeatDrag##type(LowName, &Expression->Low, -INFINITY, INFINITY, ScreenDelta);\
OffbeatDrag##type(HighName, &Expression->High, -INFINITY, INFINITY, ScreenDelta);\
}\
}\
}\
}
static OffbeatGUIExpression(F32);
static OffbeatGUIExpression(OV3);
static OffbeatGUIExpression(OV4);
void
OffbeatWindow(game_state* GameState, const game_input* Input)
{
ob_state* OffbeatState = GameState->OffbeatState;
// TODO(rytis): Handle the case when ParticleSystemCount is zero.
ob_particle_system* ParticleSystem = OffbeatGetCurrentParticleSystem();
UI::BeginWindow("Offbeat Window", {25, 25}, {500, 800});
{
static u32 s_OffbeatCurrentParticleSystem = 0;
static s32 s_OffbeatCurrentFile = 0;
static bool s_OffbeatShowEmission = false;
static bool s_OffbeatShowMotion = false;
static bool s_OffbeatShowAppearance = false;
static bool s_OffbeatEmissionLocation = false;
static bool s_OffbeatEmissionRate = false;
static bool s_OffbeatEmissionLifetime = false;
static bool s_OffbeatEmissionRadius = false;
static bool s_OffbeatEmissionNormal = false;
static bool s_OffbeatEmissionInitialVelocityScale = false;
static bool s_OffbeatEmissionConeDirection = false;
static bool s_OffbeatEmissionConeHeight = false;
static bool s_OffbeatEmissionConeRadius = false;
static bool s_OffbeatMotionStrength = false;
static bool s_OffbeatMotionGravity = false;
static bool s_OffbeatMotionDrag = false;
static bool s_OffbeatMotionPosition = false;
static bool s_OffbeatMotionLineDirection = false;
static bool s_OffbeatMotionSphereRadius = false;
static bool s_OffbeatAppearanceColor = false;
static bool s_OffbeatAppearanceSize = false;
char SystemIDBuffer[50];
sprintf(SystemIDBuffer, "Current particle system ID: %u", OffbeatState->CurrentParticleSystem);
UI::Text(SystemIDBuffer);
if(UI::Button("<"))
{
ParticleSystem = OffbeatPreviousParticleSystem();
}
UI::SameLine();
if(UI::Button("Add Particle System"))
{
ParticleSystem = OffbeatNewParticleSystem();
}
UI::SameLine();
if(UI::Button(">"))
{
ParticleSystem = OffbeatNextParticleSystem();
}
UI::NewLine();
if(UI::Button("Remove Particle System"))
{
OffbeatRemoveCurrentParticleSystem();
ParticleSystem = OffbeatGetCurrentParticleSystem();
}
UI::NewLine();
if(GameState->Resources.ParticleSystemPathCount > 0)
{
static int SelectedParticleSystemIndex = 0;
if(UI::Button("Load"))
{
OffbeatImportParticleSystem(GameState, GameState->Resources.ParticleSystemPaths[SelectedParticleSystemIndex].Name);
ParticleSystem = OffbeatGetCurrentParticleSystem();
}
UI::SameLine();
UI::Combo("Load Path", &SelectedParticleSystemIndex, GameState->Resources.ParticleSystemPaths, GameState->Resources.ParticleSystemPathCount, OffbeatGUIPathArrayToString);
UI::NewLine();
}
if(OffbeatState->ParticleSystemCount == 0)
{
s_OffbeatShowEmission = false;
s_OffbeatShowMotion = false;
s_OffbeatShowAppearance = false;
s_OffbeatEmissionLocation = false;
s_OffbeatEmissionRate = false;
s_OffbeatEmissionLifetime = false;
s_OffbeatEmissionRadius = false;
s_OffbeatEmissionNormal = false;
s_OffbeatEmissionInitialVelocityScale = false;
s_OffbeatEmissionConeDirection = false;
s_OffbeatEmissionConeHeight = false;
s_OffbeatEmissionConeRadius = false;
s_OffbeatMotionStrength = false;
s_OffbeatMotionGravity = false;
s_OffbeatMotionDrag = false;
s_OffbeatMotionPosition = false;
s_OffbeatMotionLineDirection = false;
s_OffbeatMotionSphereRadius = false;
s_OffbeatAppearanceColor = false;
s_OffbeatAppearanceSize = false;
goto end_window;
}
if(GameState->Resources.ParticleSystemPathCount > 0)
{
static s32 SelectedParticleSystemIndex = 0;
if(UI::Button("Save"))
{
OffbeatExportCurrentParticleSystem(GameState->Resources.ParticleSystemPaths[SelectedParticleSystemIndex].Name);
}
UI::SameLine();
UI::Combo("Save Path", &SelectedParticleSystemIndex, GameState->Resources.ParticleSystemPaths, GameState->Resources.ParticleSystemPathCount, OffbeatGUIPathArrayToString);
UI::NewLine();
}
UI::NewLine();
if(UI::Button("Save Current As New"))
{
struct tm* TimeInfo;
time_t CurrentTime;
char Path[100];
time(&CurrentTime);
TimeInfo = localtime(&CurrentTime);
strftime(Path, sizeof(Path), "data/offbeat/%H_%M_%S.obp", TimeInfo);
OffbeatExportCurrentParticleSystem(Path);
}
bool DrawDebugData = OffbeatState->DrawDebugData;
UI::Checkbox("Draw Debug", &DrawDebugData);
OffbeatState->DrawDebugData = DrawDebugData;
bool UseGPU = ParticleSystem->UseGPU;
UI::Checkbox("Use GPU", &UseGPU);
if(UseGPU != (bool)ParticleSystem->UseGPU)
{
OffbeatToggleGPU(ParticleSystem);
}
if(UI::CollapsingHeader("Emission", &s_OffbeatShowEmission))
{
OffbeatGUIExpressionOV3("Location", &ParticleSystem->Emission.Location,
&s_OffbeatEmissionLocation, false, 10.0f);
OffbeatGUIExpressionF32("Emission Rate", &ParticleSystem->Emission.EmissionRate,
&s_OffbeatEmissionRate, true, 500.0f);
OffbeatGUIExpressionF32("Particle Lifetime", &ParticleSystem->Emission.ParticleLifetime,
&s_OffbeatEmissionLifetime, true);
ob_emission_shape* CurrentShape = &ParticleSystem->Emission.Shape;
UI::Combo("Emission Shape", (int*)CurrentShape, g_EmissionShapeStrings, OFFBEAT_EmissionCount, UI::StringArrayToString);
switch(*CurrentShape)
{
case OFFBEAT_EmissionPoint:
{
} break;
case OFFBEAT_EmissionRing:
case OFFBEAT_EmissionDisk:
case OFFBEAT_EmissionSquare:
case OFFBEAT_EmissionCubeVolume:
{
OffbeatGUIExpressionF32("Radius", &ParticleSystem->Emission.EmissionRadius,
&s_OffbeatEmissionRadius, true, 10.0f);
OffbeatGUIExpressionOV3("Normal", &ParticleSystem->Emission.EmissionNormal,
&s_OffbeatEmissionNormal, false, 10.0f);
} break;
case OFFBEAT_EmissionSphere:
case OFFBEAT_EmissionSphereVolume:
{
OffbeatGUIExpressionF32("Radius", &ParticleSystem->Emission.EmissionRadius,
&s_OffbeatEmissionRadius, true, 10.0f);
} break;
}
OffbeatGUIExpressionF32("Initial Velocity Scale",
&ParticleSystem->Emission.InitialVelocityScale,
&s_OffbeatEmissionInitialVelocityScale, false);
ob_emission_velocity* CurrentVelocityType = &ParticleSystem->Emission.VelocityType;
UI::Combo("Velocity Type", (int*)CurrentVelocityType, g_EmissionVelocityStrings, OFFBEAT_VelocityCount, UI::StringArrayToString);
switch(*CurrentVelocityType)
{
case OFFBEAT_VelocityRandom:
{
} break;
case OFFBEAT_VelocityCone:
{
OffbeatGUIExpressionOV3("Cone Direction", &ParticleSystem->Emission.ConeDirection,
&s_OffbeatEmissionConeDirection, false, 10.0f);
OffbeatGUIExpressionF32("Cone Height", &ParticleSystem->Emission.ConeHeight,
&s_OffbeatEmissionConeHeight, true, 10.0f);
OffbeatGUIExpressionF32("Cone Radius", &ParticleSystem->Emission.ConeRadius,
&s_OffbeatEmissionConeRadius, true, 10.0f);
} break;
}
}
if(UI::CollapsingHeader("Motion", &s_OffbeatShowMotion))
{
OffbeatGUIExpressionOV3("Gravity", &ParticleSystem->Motion.Gravity,
&s_OffbeatMotionGravity, false, 10.0f);
OffbeatGUIExpressionF32("Drag Strength", &ParticleSystem->Motion.Drag,
&s_OffbeatMotionDrag, false, 1.0f);
OffbeatGUIExpressionF32("Strength", &ParticleSystem->Motion.Strength,
&s_OffbeatMotionStrength, false);
UI::Checkbox("Collision Detection", (bool*)&ParticleSystem->Motion.Collision);
ob_motion_primitive* CurrentPrimitive = &ParticleSystem->Motion.Primitive;
UI::Combo("Motion Primitive", (int*)CurrentPrimitive, g_MotionPrimitiveStrings, OFFBEAT_MotionCount, UI::StringArrayToString);
switch(*CurrentPrimitive)
{
case OFFBEAT_MotionPoint:
{
OffbeatGUIExpressionOV3("Position", &ParticleSystem->Motion.Position,
&s_OffbeatMotionPosition, false, 10.0f);
} break;
case OFFBEAT_MotionLine:
{
OffbeatGUIExpressionOV3("Position", &ParticleSystem->Motion.Position,
&s_OffbeatMotionPosition, false, 10.0f);
OffbeatGUIExpressionOV3("Direction", &ParticleSystem->Motion.LineDirection,
&s_OffbeatMotionLineDirection, false, 10.0f);
} break;
case OFFBEAT_MotionSphere:
{
OffbeatGUIExpressionOV3("Position", &ParticleSystem->Motion.Position,
&s_OffbeatMotionPosition, false, 10.0f);
OffbeatGUIExpressionF32("Radius", &ParticleSystem->Motion.SphereRadius,
&s_OffbeatMotionSphereRadius, false, 10.0f);
} break;
}
}
if(UI::CollapsingHeader("Appearance", &s_OffbeatShowAppearance))
{
OffbeatGUIExpressionOV4("Color", &ParticleSystem->Appearance.Color,
&s_OffbeatAppearanceColor, true);
OffbeatGUIExpressionF32("Size", &ParticleSystem->Appearance.Size,
&s_OffbeatAppearanceSize, true);
if(GameState->Resources.TexturePathCount > 0)
{
int CurrentTextureIndex = ParticleSystem->Appearance.TextureIndex;
int NewTextureIndex = CurrentTextureIndex;
UI::Combo("Texture", &NewTextureIndex, g_TextureStrings, g_TextureCount, OffbeatGUITextureString);
if(NewTextureIndex != CurrentTextureIndex)
{
CurrentTextureIndex = NewTextureIndex;
ParticleSystem->Appearance.TextureIndex = CurrentTextureIndex;
}
}
}
{
uint32_t ParticleSum = 0;
int64_t CycleSum = 0;
float MilisecondSum = 0;
for(int i = 0; i < OFFBEAT_FRAME_COUNT; ++i)
{
ParticleSum += g_TotalParticleCounts[i];
CycleSum += g_CycleCounts[i];
MilisecondSum += g_MilisecondCounts[i];
}
u64 FrameCount = OFFBEAT_FRAME_COUNT;
u64 AvgParticleCount = ParticleSum / FrameCount;
u64 AvgCycleCount = CycleSum / FrameCount;
f32 AvgCyclesPerParticle = AvgParticleCount ? ((f32)AvgCycleCount / (f32)AvgParticleCount) : 0.0f;
f32 AvgMSCount = MilisecondSum / (f32)FrameCount;
u32 AvgParticlesPerMS = (u32)((f32)AvgParticleCount / AvgMSCount);
static bool BufferInit = false;
static char ParticleBuffer[50];
static char TimeBuffer[50];
static char MSBuffer[50];
static char ParticlesPerMSBuffer[50];
static char CountBuffer[50];
static char PerParticleBuffer[50];
sprintf(ParticleBuffer, "Particles: %llu", (unsigned long long)OffbeatState->TotalParticleCount);
sprintf(TimeBuffer, "%.2f s", OffbeatState->t);
sprintf(MSBuffer, "%.3f ms average", AvgMSCount);
sprintf(ParticlesPerMSBuffer, "%u particles/ms", AvgParticlesPerMS);
sprintf(CountBuffer, "%llu cycle average", (unsigned long long)AvgCycleCount);
sprintf(PerParticleBuffer, "%.2f cycles/particle", AvgCyclesPerParticle);
UI::Text(ParticleBuffer);
UI::Text(TimeBuffer);
UI::Text(MSBuffer);
UI::Text(ParticlesPerMSBuffer);
UI::Text(CountBuffer);
UI::Text(PerParticleBuffer);
}
}
end_window:
UI::EndWindow();
}