Skip to content

Commit

Permalink
Mark chunk_flag as a @bitfield
Browse files Browse the repository at this point in the history
  • Loading branch information
scallyw4g committed Aug 31, 2024
1 parent ef160ad commit 6d103ad
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 12 deletions.
15 changes: 15 additions & 0 deletions generated/do_editor_ui_for_compound_type_world.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ DoEditorUi(renderer_2d *Ui, window_layout *Window, world *Element, cs Name, ui_r

PushNewRow(Ui);

DoEditorUi(Ui,
Window,
// Cast to remove const/volatile keywords if they're there
Cast(world_chunk*, Element->ChunkHash),
CSz("ChunkHash"),
Params
);








DoEditorUi(Ui,
Window,
// Cast to remove const/volatile keywords if they're there
Expand Down
96 changes: 90 additions & 6 deletions generated/do_editor_ui_for_enum_chunk_flag.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,21 @@ DoEditorUi(renderer_2d *Ui, window_layout *Window, chunk_flag *Element, cs Name,
if (Name.Count) { PushColumn(Ui, CSz("|")); } // Skip the first Name column
if (Button(Ui, CSz("Uninitialized"), UiId(Window, "enum Chunk_Uninitialized", Element), Params))
{
*Element = Chunk_Uninitialized;
if (Chunk_Uninitialized == chunk_flag(0))
{
*Element = chunk_flag(0);
}
else
{
if ((*Element & Chunk_Uninitialized) == Chunk_Uninitialized)
{
*Element = chunk_flag(*Element&~Chunk_Uninitialized);
}
else
{
*Element = chunk_flag(*Element|Chunk_Uninitialized);
}
}


SetToggleButton(Ui, ToggleButtonId, False);
Expand All @@ -22,7 +36,21 @@ DoEditorUi(renderer_2d *Ui, window_layout *Window, chunk_flag *Element, cs Name,
if (Name.Count) { PushColumn(Ui, CSz("|")); } // Skip the first Name column
if (Button(Ui, CSz("Queued"), UiId(Window, "enum Chunk_Queued", Element), Params))
{
*Element = Chunk_Queued;
if (Chunk_Queued == chunk_flag(0))
{
*Element = chunk_flag(0);
}
else
{
if ((*Element & Chunk_Queued) == Chunk_Queued)
{
*Element = chunk_flag(*Element&~Chunk_Queued);
}
else
{
*Element = chunk_flag(*Element|Chunk_Queued);
}
}


SetToggleButton(Ui, ToggleButtonId, False);
Expand All @@ -31,7 +59,21 @@ DoEditorUi(renderer_2d *Ui, window_layout *Window, chunk_flag *Element, cs Name,
if (Name.Count) { PushColumn(Ui, CSz("|")); } // Skip the first Name column
if (Button(Ui, CSz("VoxelsInitialized"), UiId(Window, "enum Chunk_VoxelsInitialized", Element), Params))
{
*Element = Chunk_VoxelsInitialized;
if (Chunk_VoxelsInitialized == chunk_flag(0))
{
*Element = chunk_flag(0);
}
else
{
if ((*Element & Chunk_VoxelsInitialized) == Chunk_VoxelsInitialized)
{
*Element = chunk_flag(*Element&~Chunk_VoxelsInitialized);
}
else
{
*Element = chunk_flag(*Element|Chunk_VoxelsInitialized);
}
}


SetToggleButton(Ui, ToggleButtonId, False);
Expand All @@ -40,7 +82,21 @@ DoEditorUi(renderer_2d *Ui, window_layout *Window, chunk_flag *Element, cs Name,
if (Name.Count) { PushColumn(Ui, CSz("|")); } // Skip the first Name column
if (Button(Ui, CSz("Garbage"), UiId(Window, "enum Chunk_Garbage", Element), Params))
{
*Element = Chunk_Garbage;
if (Chunk_Garbage == chunk_flag(0))
{
*Element = chunk_flag(0);
}
else
{
if ((*Element & Chunk_Garbage) == Chunk_Garbage)
{
*Element = chunk_flag(*Element&~Chunk_Garbage);
}
else
{
*Element = chunk_flag(*Element|Chunk_Garbage);
}
}


SetToggleButton(Ui, ToggleButtonId, False);
Expand All @@ -49,7 +105,21 @@ DoEditorUi(renderer_2d *Ui, window_layout *Window, chunk_flag *Element, cs Name,
if (Name.Count) { PushColumn(Ui, CSz("|")); } // Skip the first Name column
if (Button(Ui, CSz("Deallocate"), UiId(Window, "enum Chunk_Deallocate", Element), Params))
{
*Element = Chunk_Deallocate;
if (Chunk_Deallocate == chunk_flag(0))
{
*Element = chunk_flag(0);
}
else
{
if ((*Element & Chunk_Deallocate) == Chunk_Deallocate)
{
*Element = chunk_flag(*Element&~Chunk_Deallocate);
}
else
{
*Element = chunk_flag(*Element|Chunk_Deallocate);
}
}


SetToggleButton(Ui, ToggleButtonId, False);
Expand All @@ -58,7 +128,21 @@ DoEditorUi(renderer_2d *Ui, window_layout *Window, chunk_flag *Element, cs Name,
if (Name.Count) { PushColumn(Ui, CSz("|")); } // Skip the first Name column
if (Button(Ui, CSz("Freelist"), UiId(Window, "enum Chunk_Freelist", Element), Params))
{
*Element = Chunk_Freelist;
if (Chunk_Freelist == chunk_flag(0))
{
*Element = chunk_flag(0);
}
else
{
if ((*Element & Chunk_Freelist) == Chunk_Freelist)
{
*Element = chunk_flag(*Element&~Chunk_Freelist);
}
else
{
*Element = chunk_flag(*Element|Chunk_Freelist);
}
}


SetToggleButton(Ui, ToggleButtonId, False);
Expand Down
43 changes: 41 additions & 2 deletions generated/string_and_value_tables_chunk_flag.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,30 @@ ToStringPrefixless(chunk_flag Type)
case Chunk_Deallocate: { Result = CSz("Deallocate"); } break;
case Chunk_Freelist: { Result = CSz("Freelist"); } break;


// TODO(Jesse): This is pretty barf and we could do it in a single allocation,
// but the metaprogram might have to be a bit fancier..
default:
{
u32 CurrentFlags = u32(Type);

if (CountBitsSet_Kernighan(CurrentFlags) == 1)
{
Result = FSz("(invalid value for chunk_flag (%d))", CurrentFlags);
}
else
{
u32 FirstValue = UnsetLeastSignificantSetBit(&CurrentFlags);
Result = ToStringPrefixless(chunk_flag(FirstValue));

while (CurrentFlags)
{
u32 Value = UnsetLeastSignificantSetBit(&CurrentFlags);
cs Next = ToStringPrefixless(chunk_flag(Value));
Result = FSz("%S | %S", Result, Next);
}
}
} break;

}
/* if (Result.Start == 0) { Info("Could not convert value(%d) to (EnumType.name)", Type); } */
return Result;
Expand All @@ -32,7 +55,23 @@ ToString(chunk_flag Type)
case Chunk_Deallocate: { Result = CSz("Chunk_Deallocate"); } break;
case Chunk_Freelist: { Result = CSz("Chunk_Freelist"); } break;


// TODO(Jesse): This is pretty barf and we could do it in a single allocation,
// but the metaprogram might have to be a bit fancier..
default:
{
u32 CurrentFlags = u32(Type);

u32 FirstValue = UnsetLeastSignificantSetBit(&CurrentFlags);
Result = ToString(chunk_flag(FirstValue));

while (CurrentFlags)
{
u32 Value = UnsetLeastSignificantSetBit(&CurrentFlags);
cs Next = ToString(chunk_flag(Value));
Result = FSz("%S | %S", Result, Next);
}
} break;

}
/* if (Result.Start == 0) { Info("Could not convert value(%d) to (EnumType.name)", Type); } */
return Result;
Expand Down
4 changes: 2 additions & 2 deletions src/engine/world_chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ FinalizeChunkInitialization(world_chunk *Chunk)
FullBarrier;

/* UnSetFlag(Chunk, Chunk_Garbage); */
/* UnSetFlag(&Chunk->Flags, Chunk_Queued); */
UnSetFlag(&Chunk->Flags, Chunk_Queued);
SetFlag(&Chunk->Flags, Chunk_VoxelsInitialized);
}

Expand Down Expand Up @@ -3455,7 +3455,7 @@ InitializeWorldChunkEmpty(world_chunk *DestChunk)
#endif

FinalizeChunkInitialization(DestChunk);
UnSetFlag(&DestChunk->Flags, Chunk_Queued);
/* UnSetFlag(&DestChunk->Flags, Chunk_Queued); */

return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/engine/world_chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ enum pick_chunk_state
PickedChunkState_Hover,
};

enum chunk_flag
enum chunk_flag poof(@bitfield)
{
Chunk_Uninitialized = 0 << 0,

Expand Down Expand Up @@ -436,7 +436,7 @@ struct world
u32 HashSlotsUsed;
u32 HashSize;
world_chunk **ChunkHashMemory[2]; poof(@ui_skip)
world_chunk **ChunkHash; poof(@ui_skip)
world_chunk **ChunkHash; poof(@array_length(Element->HashSize))


bonsai_futex ChunkFreelistFutex; poof(@ui_skip)
Expand Down

3 comments on commit 6d103ad

@bitfield
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! 👍

@scallyw4g
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! 👍

AHahhahahaha. I chose the @ symbol to prefix tags in a little metaprogramming DSL I made.
Apparently that had some unintended side effects when using github 😆

@bitfield
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But not unwelcome—it's always nice to make a new friend!

Please sign in to comment.