2020-01-02 02:41:43 +00:00
|
|
|
//
|
|
|
|
// File: foldhaus_assembly.cpp
|
|
|
|
// Author: Peter Slattery
|
|
|
|
// Creation Date: 2020-01-01
|
|
|
|
//
|
|
|
|
#ifndef FOLDHAUS_ASSEMBLY_CPP
|
|
|
|
|
2019-11-23 00:07:25 +00:00
|
|
|
internal assembly
|
|
|
|
ConstructAssemblyFromDefinition (assembly_definition Definition,
|
|
|
|
string AssemblyName,
|
2019-12-27 02:40:14 +00:00
|
|
|
v4 RootPosition,
|
2019-11-23 00:07:25 +00:00
|
|
|
r32 Scale,
|
2019-12-23 01:47:26 +00:00
|
|
|
memory_arena Arena)
|
2019-11-23 00:07:25 +00:00
|
|
|
{
|
|
|
|
assembly Assembly = {};
|
2019-12-23 01:47:26 +00:00
|
|
|
Assembly.Arena = Arena;
|
2019-11-23 00:07:25 +00:00
|
|
|
|
2019-11-23 07:33:48 +00:00
|
|
|
Assembly.Name = MakeString(PushArray(&Assembly.Arena, char, AssemblyName.Length), AssemblyName.Length);
|
2019-11-23 00:07:25 +00:00
|
|
|
CopyStringTo(AssemblyName, &Assembly.Name);
|
|
|
|
|
|
|
|
// NOTE(Peter): Setting this to zero so we can check at the end of the loop that creates leds
|
|
|
|
// and make sure we created to correct number. By the time this function returns it should be
|
|
|
|
// the case that: (Assembly.LEDCount == Definition.TotalLEDCount)
|
2020-03-08 21:44:28 +00:00
|
|
|
Assembly.LEDBuffer.LEDCount = 0;
|
|
|
|
Assembly.LEDBuffer.Colors = PushArray(&Assembly.Arena, pixel, Definition.TotalLEDCount);
|
|
|
|
Assembly.LEDBuffer.LEDs = PushArray(&Assembly.Arena, led, Definition.TotalLEDCount);
|
2019-11-23 07:33:48 +00:00
|
|
|
Assembly.LEDUniverseMapCount = Definition.LEDStripCount;
|
|
|
|
Assembly.LEDUniverseMap = PushArray(&Assembly.Arena, leds_in_universe_range, Definition.LEDStripCount);
|
2019-11-23 00:07:25 +00:00
|
|
|
|
|
|
|
// Add LEDs
|
2020-01-14 01:04:40 +00:00
|
|
|
for (u32 StripIdx = 0; StripIdx < Definition.LEDStripCount; StripIdx++)
|
2019-11-23 00:07:25 +00:00
|
|
|
{
|
|
|
|
led_strip_definition StripDef = Definition.LEDStrips[StripIdx];
|
|
|
|
|
2019-11-23 07:33:48 +00:00
|
|
|
leds_in_universe_range* LEDUniverseRange = Assembly.LEDUniverseMap + StripIdx;
|
|
|
|
LEDUniverseRange->Universe = StripDef.StartUniverse;
|
2020-03-08 21:44:28 +00:00
|
|
|
LEDUniverseRange->RangeStart = Assembly.LEDBuffer.LEDCount;
|
|
|
|
LEDUniverseRange->RangeOnePastLast = Assembly.LEDBuffer.LEDCount + StripDef.LEDsPerStrip;
|
2019-11-23 07:33:48 +00:00
|
|
|
|
2019-11-23 00:07:25 +00:00
|
|
|
// NOTE(Peter): this should be a switch on the type, but we only have one for
|
|
|
|
// now. The assert is to remind you to create more cases when necessary
|
|
|
|
Assert(StripDef.InterpolationType == StripInterpolate_Points);
|
|
|
|
|
2019-12-27 02:40:14 +00:00
|
|
|
v4 WS_StripStart = RootPosition + V4(StripDef.InterpolatePositionStart * Scale, 1);
|
|
|
|
v4 WS_StripEnd = RootPosition + V4(StripDef.InterpolatePositionEnd * Scale, 1);
|
2019-11-23 00:07:25 +00:00
|
|
|
s32 LEDsInStripCount = StripDef.LEDsPerStrip;
|
|
|
|
|
2020-03-08 21:44:28 +00:00
|
|
|
Assert(Assembly.LEDBuffer.LEDCount + LEDsInStripCount <= Definition.TotalLEDCount);
|
2019-11-23 00:07:25 +00:00
|
|
|
|
|
|
|
v4 SingleStep = (WS_StripEnd - WS_StripStart) / (r32)LEDsInStripCount;
|
|
|
|
for (s32 Step = 0; Step < LEDsInStripCount; Step++)
|
|
|
|
{
|
2020-03-08 21:44:28 +00:00
|
|
|
s32 LEDIndex = Assembly.LEDBuffer.LEDCount++;
|
|
|
|
Assembly.LEDBuffer.LEDs[LEDIndex].Position = WS_StripStart + (SingleStep * Step);
|
|
|
|
Assembly.LEDBuffer.LEDs[LEDIndex].Index = LEDIndex;
|
2019-11-23 00:07:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE(Peter): Did we create the correct number of LEDs?
|
2020-03-08 21:44:28 +00:00
|
|
|
Assert(Assembly.LEDBuffer.LEDCount == Definition.TotalLEDCount);
|
2019-11-23 00:07:25 +00:00
|
|
|
return Assembly;
|
|
|
|
}
|
2019-12-26 22:45:27 +00:00
|
|
|
|
2020-02-05 20:29:48 +00:00
|
|
|
// NOTE(Peter): These are here so that if we load 2+ sculptures, they don't all
|
|
|
|
// end up on top of one another. Purely aesthetic. Can remove once we implement
|
|
|
|
// scene editing tools
|
2019-12-27 02:40:14 +00:00
|
|
|
static v4 TempAssemblyOffsets[] = { v4{0, 0, 0, 0}, v4{250, 0, 75, 0}, v4{-250, 0, 75, 0} };
|
|
|
|
s32 TempAssemblyOffsetsCount = 3;
|
|
|
|
|
2019-12-26 22:45:27 +00:00
|
|
|
internal void
|
2020-05-30 22:09:06 +00:00
|
|
|
LoadAssembly (app_state* State, context Context, string Path)
|
2019-12-26 22:45:27 +00:00
|
|
|
{
|
2020-05-30 21:54:37 +00:00
|
|
|
platform_memory_result AssemblyFile = ReadEntireFile(Context, Path);
|
2020-02-06 04:24:34 +00:00
|
|
|
if (AssemblyFile.Error == PlatformMemory_NoError)
|
2020-02-05 08:03:56 +00:00
|
|
|
{
|
2020-02-06 04:24:34 +00:00
|
|
|
assembly_definition AssemblyDefinition = ParseAssemblyFile(AssemblyFile.Base, AssemblyFile.Size, &State->Transient, State->GlobalLog);
|
2020-02-05 20:29:48 +00:00
|
|
|
|
2020-05-30 22:09:06 +00:00
|
|
|
s32 IndexOfLastSlash = FastLastIndexOfCharInCharArray(Path.Memory, Path.Length, '\\');
|
|
|
|
string FileName = Substring(Path, IndexOfLastSlash + 1);
|
2020-02-05 20:29:48 +00:00
|
|
|
|
|
|
|
memory_arena AssemblyArena = {};
|
|
|
|
AssemblyArena.Alloc = (gs_memory_alloc*)Context.PlatformAlloc;
|
|
|
|
AssemblyArena.Realloc = (gs_memory_realloc*)Context.PlatformRealloc;
|
|
|
|
|
|
|
|
v4 Offset = TempAssemblyOffsets[State->ActiveAssemblyIndecies.Used % TempAssemblyOffsetsCount];
|
|
|
|
r32 Scale = 100;
|
|
|
|
assembly NewAssembly = ConstructAssemblyFromDefinition(AssemblyDefinition, FileName, Offset, Scale, AssemblyArena);
|
|
|
|
gs_list_handle NewAssemblyHandle = State->AssemblyList.PushElementOnList(NewAssembly);
|
|
|
|
|
|
|
|
State->ActiveAssemblyIndecies.PushElementOnList(NewAssemblyHandle);
|
2020-03-08 21:44:28 +00:00
|
|
|
State->TotalLEDsCount += NewAssembly.LEDBuffer.LEDCount;
|
2020-02-05 20:29:48 +00:00
|
|
|
|
2020-02-06 04:24:34 +00:00
|
|
|
Context.PlatformFree(AssemblyFile.Base, AssemblyFile.Size);
|
2020-02-05 20:29:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-06 04:24:34 +00:00
|
|
|
LogError(State->GlobalLog, "Unable to load assembly file");
|
2020-02-05 08:03:56 +00:00
|
|
|
}
|
2019-12-26 22:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal void
|
2019-12-31 04:26:28 +00:00
|
|
|
UnloadAssembly (u32 AssemblyIndex, app_state* State, context Context)
|
2019-12-26 22:45:27 +00:00
|
|
|
{
|
2019-12-31 04:26:28 +00:00
|
|
|
assembly* Assembly = State->AssemblyList.GetElementAtIndex(AssemblyIndex);
|
2020-03-08 21:44:28 +00:00
|
|
|
State->TotalLEDsCount -= Assembly->LEDBuffer.LEDCount;
|
2019-12-26 22:45:27 +00:00
|
|
|
FreeMemoryArena(&Assembly->Arena, (gs_memory_free*)Context.PlatformFree);
|
|
|
|
|
2019-12-31 04:26:28 +00:00
|
|
|
State->AssemblyList.FreeElementAtIndex(AssemblyIndex);
|
|
|
|
for (u32 i = 0; i < State->ActiveAssemblyIndecies.Used; i++)
|
2019-12-26 22:45:27 +00:00
|
|
|
{
|
2019-12-31 04:26:28 +00:00
|
|
|
gs_list_handle Handle = *State->ActiveAssemblyIndecies.GetElementAtIndex(i);
|
2019-12-26 22:45:27 +00:00
|
|
|
if (Handle.Index == AssemblyIndex)
|
|
|
|
{
|
2019-12-31 04:26:28 +00:00
|
|
|
State->ActiveAssemblyIndecies.FreeElementAtIndex(i);
|
2019-12-26 22:45:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-02 02:41:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define FOLDHAUS_ASSEMBLY_CPP
|
|
|
|
#endif // FOLDHAUS_ASSEMBLY_CPP
|