summaryrefslogtreecommitdiff
path: root/memory.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'memory.cpp')
-rw-r--r--memory.cpp88
1 files changed, 88 insertions, 0 deletions
diff --git a/memory.cpp b/memory.cpp
index 4177448..8ff508f 100644
--- a/memory.cpp
+++ b/memory.cpp
@@ -50,3 +50,91 @@ Debug_Memory_Assert_Cohesion(memory *Memory, memory_table *Table)
#else
#endif
}
+
+static cached_bitmap *
+Memory_RollingBitmap(memory *Memory, source *Source, uint32 FrameToSeek)
+{
+ uint16 Width = Source->Info.Width;
+ uint16 Height = Source->Info.Height;
+ uint16 BytesPerPixel = Source->Info.BytesPerPixel;
+ int32 Pitch = Width*BytesPerPixel;
+
+ memory_table *Table = &Memory->Slot[B_LoadedBitmaps];
+
+ // First check whether we'd run over the buffer at the current
+ // position, and reset it if so.
+ uint64 Size = Bitmap_CalcTotalBytes(Width, Height, BytesPerPixel);
+ if (Table->CurrentPosition + Size > Table->Size) {
+ Table->CurrentPosition = 0;
+ Table->PointerIndex = 0;
+ }
+
+ cached_bitmap *Bitmap = &Memory->Bitmap[Table->PointerIndex];
+
+ if (Table->PointerIndex != Table->NumberOfPointers) {
+ // Next, if there's a pointer in front of the current position,
+ // check whether it's far away enough so that the size fits.
+ bool32 BS = true;
+ if (Bitmap->Data) {
+ uint64 BytesBetween = (uint8 *)Bitmap->Data - ((uint8 *)Table->Address + Table->CurrentPosition);
+ if (BytesBetween > Size) {
+ int16 StopAt = Table->NumberOfPointers - 1;
+ while (StopAt > Table->PointerIndex - 1) {
+ Memory->Bitmap[StopAt + 1] = Memory->Bitmap[StopAt];
+ StopAt--;
+ }
+ Table->NumberOfPointers++;
+ BS = false;
+ }
+ }
+ // If it doesn't fit, then we need to dereference the pointers
+ // until we have enough space.
+ if ((Table->PointerIndex < Table->NumberOfPointers) && BS)
+ {
+ bool32 Avail = false;
+ void *AddressStart = (void *)((uint8 *)Table->Address + Table->CurrentPosition);
+ uint32 Amount = 0;
+ while(!Avail) {
+ // Bail out if we're on the last index, as we don't need to do anything else.
+ // TODO(fox): This could be simplified if we compared
+ // pointer start plus data instead of just the start.
+ if (Table->PointerIndex != Table->NumberOfPointers - 1) {
+ void *Data2 = Memory->Bitmap[Table->PointerIndex+1].Data;
+ uint64 BytesBetween = (uint8 *)Data2 - (uint8 *)AddressStart;
+ if (BytesBetween < Size) {
+ int16 StopAt = Table->PointerIndex;
+ while (StopAt < Table->NumberOfPointers - 1) {
+ Memory->Bitmap[StopAt] = Memory->Bitmap[StopAt + 1];
+ StopAt++;
+ }
+ Amount++;
+ Table->NumberOfPointers--;
+ if (Amount > 2) {
+ Amount += 0;
+ }
+ } else {
+ Avail = true;
+ }
+ } else {
+ Avail = true;
+ }
+ }
+ }
+ } else {
+ // Increment the total number of pointers if the PointerIndex is ahead
+ // of the amount of pointers.
+ Table->NumberOfPointers++;
+ }
+ Bitmap->Data = AllocateMemory(Memory, Size, B_LoadedBitmaps);
+ if (!Bitmap->Data) {
+ Assert(0);
+ }
+ Bitmap->SourceOwner = Source;
+ Bitmap->Frame = FrameToSeek;
+ Table->PointerIndex++;
+
+ // No two pointers on the table should hold the same data
+ // address or be empty.
+ Debug_Memory_Assert_Cohesion(Memory, Table);
+ return Bitmap;
+}