Lumenarium/src/app/foldhaus_assembly.cpp

102 lines
4.1 KiB
C++
Raw Normal View History

2020-01-02 02:41:43 +00:00
//
// File: foldhaus_assembly.cpp
// Author: Peter Slattery
// Creation Date: 2020-01-01
//
#ifndef FOLDHAUS_ASSEMBLY_CPP
internal void
ConstructAssemblyFromDefinition (assembly* Assembly, string AssemblyName, v4 RootPosition)
2019-11-23 00:07:25 +00:00
{
Assembly->LEDBuffer.LEDCount = 0;
Assembly->LEDBuffer.Colors = PushArray(&Assembly->Arena, pixel, Assembly->LedCountTotal);
Assembly->LEDBuffer.LEDs = PushArray(&Assembly->Arena, led, Assembly->LedCountTotal);
Assembly->LEDUniverseMapCount = Assembly->LedCountTotal;
Assembly->LEDUniverseMap = PushArray(&Assembly->Arena, leds_in_universe_range, Assembly->LedCountTotal);
2019-11-23 00:07:25 +00:00
// Add LEDs
for (u32 StripIdx = 0; StripIdx < Assembly->StripCount; StripIdx++)
2019-11-23 00:07:25 +00:00
{
//led_strip_definition StripDef = Definition.LEDStrips[StripIdx];
v2_strip* StripAt = &Assembly->Strips[StripIdx];
2019-11-23 00:07:25 +00:00
leds_in_universe_range* LEDUniverseRange = &Assembly->LEDUniverseMap[StripIdx];
LEDUniverseRange->Universe = StripAt->StartUniverse;
LEDUniverseRange->RangeStart = Assembly->LEDBuffer.LEDCount;
LEDUniverseRange->RangeOnePastLast = Assembly->LEDBuffer.LEDCount + StripAt->LedCount;
2019-11-23 00:07:25 +00:00
v4 WS_StripStart = RootPosition + V4(StripAt->StartPosition * Assembly->Scale, 1);
v4 WS_StripEnd = RootPosition + V4(StripAt->EndPosition * Assembly->Scale, 1);
s32 LEDsInStripCount = StripAt->LedCount;
2019-11-23 00:07:25 +00:00
v4 SingleStep = (WS_StripEnd - WS_StripStart) / (r32)LEDsInStripCount;
for (s32 Step = 0; Step < LEDsInStripCount; Step++)
{
s32 LEDIndex = Assembly->LEDBuffer.LEDCount;
Assembly->LEDBuffer.LEDs[LEDIndex].Position = WS_StripStart + (SingleStep * Step);
Assembly->LEDBuffer.LEDs[LEDIndex].Index = LEDIndex;
Assembly->LEDBuffer.LEDCount += 1;
2019-11-23 00:07:25 +00:00
}
}
}
2019-12-26 22:45:27 +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
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
LoadAssembly (app_state* State, context Context, string Path)
2019-12-26 22:45:27 +00:00
{
platform_memory_result AssemblyFile = ReadEntireFile(Context, Path);
if (AssemblyFile.Error == PlatformMemory_NoError)
{
string AssemblyFileText = MakeString((char*)AssemblyFile.Base);
s32 IndexOfLastSlash = FastLastIndexOfCharInCharArray(Path.Memory, Path.Length, '\\');
string FileName = Substring(Path, IndexOfLastSlash + 1);
assembly NewAssembly = {};
NewAssembly.Arena.Alloc = (gs_memory_alloc*)Context.PlatformAlloc;
NewAssembly.Arena.Realloc = (gs_memory_realloc*)Context.PlatformRealloc;
ParseAssemblyFile(&NewAssembly, AssemblyFileText, &State->Transient);
v4 Offset = TempAssemblyOffsets[State->ActiveAssemblyIndecies.Used % TempAssemblyOffsetsCount];
ConstructAssemblyFromDefinition(&NewAssembly, FileName, Offset);
gs_list_handle NewAssemblyHandle = State->AssemblyList.PushElementOnList(NewAssembly);
State->ActiveAssemblyIndecies.PushElementOnList(NewAssemblyHandle);
State->TotalLEDsCount += NewAssembly.LEDBuffer.LEDCount;
Context.PlatformFree(AssemblyFile.Base, AssemblyFile.Size);
}
else
{
LogError(State->GlobalLog, "Unable to load assembly file");
}
2019-12-26 22:45:27 +00:00
}
internal void
UnloadAssembly (u32 AssemblyIndex, app_state* State, context Context)
2019-12-26 22:45:27 +00:00
{
assembly* Assembly = State->AssemblyList.GetElementAtIndex(AssemblyIndex);
State->TotalLEDsCount -= Assembly->LEDBuffer.LEDCount;
2019-12-26 22:45:27 +00:00
FreeMemoryArena(&Assembly->Arena, (gs_memory_free*)Context.PlatformFree);
State->AssemblyList.FreeElementAtIndex(AssemblyIndex);
for (u32 i = 0; i < State->ActiveAssemblyIndecies.Used; i++)
2019-12-26 22:45:27 +00:00
{
gs_list_handle Handle = *State->ActiveAssemblyIndecies.GetElementAtIndex(i);
2019-12-26 22:45:27 +00:00
if (Handle.Index == AssemblyIndex)
{
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