Skip to content

Commit

Permalink
z layer slice handle, wip
Browse files Browse the repository at this point in the history
  • Loading branch information
r2dev committed Feb 26, 2021
1 parent 6891994 commit ec71a72
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 221 deletions.
2 changes: 1 addition & 1 deletion code/handmade.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ inline void
ZeroSize(memory_index Size, void* Ptr) {
u8* Byte = (u8 *)Ptr;
while(Size--) {
*Byte++ = 0; // should be *Byte++ = 0
*Byte++ = 0;
}
}

Expand Down
1 change: 1 addition & 0 deletions code/handmade_brain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ ExecuteBrain(game_state *GameState, game_mode_world *WorldMode, game_input *Inpu
}
} break;
case Type_brain_familiar: {

brain_familiar *Familiar = &Brain->Familiar;
entity *FamiliarHead = Familiar->Head;
if (FamiliarHead) {
Expand Down
211 changes: 211 additions & 0 deletions code/handmade_entity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
internal void
DrawHitPoints(entity* Entity, render_group* PieceGroup, object_transform ObjectTransform) {
if (Entity->HitPointMax >= 1) {
v2 HealthDim = { 0.2f, 0.2f };
r32 SpacingX = 1.5f * HealthDim.x;
v2 HitP = { -0.5f * (Entity->HitPointMax - 1) * SpacingX, -0.2f };
v2 dHitP = { SpacingX, 0.0f };
for (u32 HealthIndex = 0; HealthIndex < Entity->HitPointMax; ++HealthIndex) {
hit_point* HitPoint = Entity->HitPoint + HealthIndex;
v4 Color = { 1.0f, 0.0f, 0.0f, 1.0f };
if (HitPoint->FilledAmount == 0) {
Color = { 0.2f, 0.2f, 0.2f, 1.0f };
}
PushRect(PieceGroup, ObjectTransform, V3(HitP, 0), HealthDim, Color);
HitP += dHitP;
}
}
}

inline s32
ConvertToLayerRelative(world *World, r32 *Z) {
s32 RelativeIndex = 0;
RecanonicalizeCoord(World->ChunkDimInMeters.z, &RelativeIndex, Z);
return(RelativeIndex);
}

internal void
UpdateAndRenderEntities(sim_region *SimRegion, game_mode_world *WorldMode, render_group *RenderGroup, v3 CameraP, loaded_bitmap *DrawBuffer, v4 BackgroundColor, r32 dt, transient_state *TranState, v2 MouseP) {

r32 FadeTopEnd = 0.75f * WorldMode->TypicalFloorHeight;
r32 FadeTopStart = 0.5f * WorldMode->TypicalFloorHeight;

r32 FadeBottomStart = -1.0f * WorldMode->TypicalFloorHeight;
r32 FadeBottomEnd = -3.25f * WorldMode->TypicalFloorHeight;

#define MinimumLevelIndex -4
#define MaximumLevelIndex 1

u32 ClipRectIndex[(MaximumLevelIndex - MinimumLevelIndex + 1)];
for (u32 LevelIndex = 0; LevelIndex < ArrayCount(ClipRectIndex); ++LevelIndex) {
s32 RelativeLayerIndex = LevelIndex + MinimumLevelIndex;
r32 CameraRelativeGroundZ = SimRegion->Origin.Offset_.z + WorldMode->TypicalFloorHeight * (r32)RelativeLayerIndex;


clip_rect_fx FX = {};

if (CameraRelativeGroundZ > FadeTopStart) {
RenderGroup->CurrentClipRectIndex = ClipRectIndex[0];
r32 t = Clamp01MapToRange(FadeTopStart, CameraRelativeGroundZ, FadeTopEnd);

FX.tColor = v4{0, 0, 0, t};
} else if (CameraRelativeGroundZ < FadeBottomStart) {
RenderGroup->CurrentClipRectIndex = ClipRectIndex[1];
r32 t = Clamp01MapToRange(FadeBottomStart, CameraRelativeGroundZ, FadeBottomEnd);

FX.tColor = v4{t, t, t, 0};
FX.Color = BackgroundColor;
} else {
RenderGroup->CurrentClipRectIndex = ClipRectIndex[2];

}

ClipRectIndex[LevelIndex] = PushClipRect(RenderGroup, 0, 0, DrawBuffer->Width, DrawBuffer->Height, FX);
}
transient_clip_rect Rect(RenderGroup);
for (u32 EntityIndex = 0; EntityIndex < SimRegion->EntityCount; ++EntityIndex) {
entity* Entity = SimRegion->Entities + EntityIndex;
if (Entity->Updatable) {
//hero_bitmaps* HeroBitmap = &GameState->HeroBitmaps[Entity->FacingDirection];

switch (Entity->MovementMode) {
case MovementMode_Planted: {

} break;
case MovementMode_Hopping: {
r32 tTotal = Entity->tMovement;
r32 tJump = 0.1f;
r32 tLand = 0.9f;
v3 MovementFrom = GetSimSpaceTraversable(Entity->CameFrom).P;
v3 MovementTo = GetSimSpaceTraversable(Entity->Occupying).P;

if (tTotal < tJump) {
Entity->ddtBob = 45.0f;
}

if (tTotal < tLand) {
r32 t = Clamp01MapToRange(tJump, tTotal, tLand);
v3 a = v3{0, -2.0f, 0};
v3 b = MovementTo - MovementFrom - a;
Entity->P = a * t * t + b * t + MovementFrom;
}

if (Entity->tMovement >= 1.0f) {
Entity->P = MovementTo;
Entity->CameFrom = Entity->Occupying;
Entity->MovementMode = MovementMode_Planted;
Entity->dtBob = -2.0f;
}
Entity->tMovement += 6.0f * dt;
if (Entity->tMovement > 1.0f) {
Entity->tMovement = 1.0f;
}

} break;
case MovementMode_AttackSwipe: {
if (Entity->tMovement < 1.0f) {
Entity->AngleCurrent = Lerp(Entity->AngleStart, Entity->tMovement, Entity->AngleTarget);

Entity->AngleCurrentDistance = Lerp(Entity->AngleBaseDistance, Triangle01(Entity->tMovement), Entity->AngleSwipeDistance);
} else {
Entity->MovementMode = MovementMode_AngleOffset;
Entity->AngleCurrent = Entity->AngleTarget;
Entity->AngleCurrentDistance = Entity->AngleBaseDistance;
}
Entity->tMovement += 1.0f * dt;
if (Entity->tMovement > 1.0f) {
Entity->tMovement = 1.0f;
}

}
case MovementMode_AngleOffset: {

v2 Arm = Entity->AngleCurrentDistance * Arm2( Entity->AngleCurrent + Entity->FacingDirection);
Entity->P = Entity->AngleBase + V3(Arm.x, Arm.y, 0.0f);
} break;
}
r32 Cv = 10.0f;
Entity->ddtBob += -100.0f * Entity->tBob + Cv * -Entity->dtBob;
Entity->tBob = Entity->ddtBob * dt * dt + Entity->dtBob*dt;
Entity->dtBob += Entity->ddtBob * dt;

if (LengthSq(Entity->dP) > 0 || LengthSq(Entity->ddP) > 0) {
MoveEntity(WorldMode, SimRegion, Entity, dt, Entity->ddP);
}

// rendering
object_transform EntityTransform = DefaultUprightTransform();
EntityTransform.OffsetP = GetEntityGroundPoint(Entity) - CameraP;
s32 RelativeLayer = ConvertToLayerRelative(WorldMode->World, &EntityTransform.OffsetP.z);
if (RelativeLayer >= MinimumLevelIndex && RelativeLayer >= MaximumLevelIndex) {
RenderGroup->CurrentClipRectIndex = ClipRectIndex[RelativeLayer - MinimumLevelIndex];
asset_vector MatchVector = {};
MatchVector.E[Tag_FaceDirection] = (r32)Entity->FacingDirection;
asset_vector WeightVector = {};
WeightVector.E[Tag_FaceDirection] = 1.0f;

DrawHitPoints(Entity, RenderGroup, EntityTransform);

for (u32 PieceIndex = 0; PieceIndex < Entity->PieceCount; ++PieceIndex) {
entity_visible_piece *Piece = Entity->Pieces + PieceIndex;
bitmap_id BitmapID = GetBestMatchBitmapFrom(TranState->Assets, Piece->AssetTypeID, &MatchVector, &WeightVector);

v2 XAxis = {1.0f , 0};
v2 YAxis = {0, 1.0f};
if (Piece->Flags & PieceMove_AxesDeform) {
XAxis = Entity->XAxis;
YAxis = Entity->YAxis;
}
v3 Offset = {};
if (Piece->Flags & PieceMove_BobOffset) {
Offset = V3(Entity->FloorDisplace, 0.0f);
Offset.y += Entity->tBob;
}
PushBitmap(RenderGroup, EntityTransform, BitmapID, Piece->Height, Piece->Offset + Offset, Piece->Color, 1.0f, XAxis, YAxis);
}
EntityTransform.Upright = false;
for (u32 VolumeIndex = 0; VolumeIndex < Entity->Collision->VolumeCount; VolumeIndex++) {
entity_collision_volume* Volume = Entity->Collision->Volumes + VolumeIndex;
PushRectOutline(RenderGroup, EntityTransform, V3(Volume->OffsetP.xy, 0.0f), Volume->Dim.xy, v4{ 0, 0.5f, 1.0f, 1.0f });
}
for (u32 Index = 0; Index < Entity->TraversableCount; Index++) {
entity_traversable_point* Traversable = Entity->Traversables + Index;

PushRect(RenderGroup, EntityTransform, Traversable->P, v2{1.2f, 1.2f},
Traversable->Occupier? v4{ 1.0f, 0.5f, 0.0f, 1.0f }: v4{ 0.05f, 0.25f, 0.25f, 1.0f });
PushRectOutline(RenderGroup, EntityTransform, Traversable->P, v2{1.2f, 1.2f}, v4{0, 0, 0, 1});
}
if (DEBUG_UI_ENABLED) {
debug_id EntityDebugID = DEBUG_POINTER_ID((void *)Entity->ID.Value);
for (u32 VolumeIndex = 0; VolumeIndex < Entity->Collision->VolumeCount; VolumeIndex++) {
entity_collision_volume* Volume = Entity->Collision->Volumes + VolumeIndex;
v3 WorldMousePoint = Unproject(RenderGroup, EntityTransform, MouseP);
#if 0
PushRect(RenderGroup, V3(WorldMousePoint, 0.0f), v2{1.0f, 1.0f}, v4{1.0f, 0.0f, 1.0f, 1.0f});
#endif
if ((WorldMousePoint.x > -0.5f * Volume->Dim.x && WorldMousePoint.x < 0.5f * Volume->Dim.x) &&
(WorldMousePoint.y > -0.5f * Volume->Dim.y && WorldMousePoint.y < 0.5f * Volume->Dim.y)){

DEBUG_HIT(EntityDebugID, WorldMousePoint.z);
}
v4 EntityOutlineColor;
if (DEBUG_HIGHLIGHTED(EntityDebugID, &EntityOutlineColor)) {
PushRectOutline(RenderGroup, EntityTransform, V3(Volume->OffsetP.xy, 0.0f), Volume->Dim.xy, EntityOutlineColor, 0.05f);
}

}

if (DEBUG_REQUESTED(EntityDebugID)) {
DEBUG_DATA_BLOCK("Simulation/Entity");
DEBUG_VALUE(Entity->P);
DEBUG_VALUE(Entity->dP);
DEBUG_VALUE(Entity->FacingDirection);
DEBUG_VALUE(Entity->WalkableDim);
DEBUG_VALUE(Entity->WalkableHeight);
DEBUG_VALUE(Entity->ID.Value);
}
}
}
}
}
}
6 changes: 3 additions & 3 deletions code/handmade_opengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,14 @@ OpenGLRenderCommands(game_render_commands *Commands, s32 WindowWidth, s32 Window
switch (Header->Type) {
case RenderGroupEntryType_render_entry_clear: {
render_entry_clear* Entry = (render_entry_clear*)Data;
glClearColor(Entry->Color.r, Entry->Color.g, Entry->Color.b, Entry->Color.a);
glClearColor(Entry->PremulColor.r, Entry->PremulColor.g, Entry->PremulColor.b, Entry->PremulColor.a);
glClear(GL_COLOR_BUFFER_BIT);
} break;

case RenderGroupEntryType_render_entry_rectangle: {
render_entry_rectangle* Entry = (render_entry_rectangle*)Data;
glDisable(GL_TEXTURE_2D);
OpenGLRectangle(Entry->P, Entry->P + Entry->Dim, Entry->Color);
OpenGLRectangle(Entry->P, Entry->P + Entry->Dim, Entry->PremulColor);
glEnable(GL_TEXTURE_2D);
} break;

Expand All @@ -221,7 +221,7 @@ OpenGLRenderCommands(game_render_commands *Commands, s32 WindowWidth, s32 Window
v2 MaxXMaxY = MinP + XAxis + YAxis;

glBegin(GL_TRIANGLES);
glColor4fv(Entry->Color.E);
glColor4fv(Entry->PremulColor.E);

glTexCoord2f(MinUV.x, MinUV.y);
glVertex2fv(MinXMinY.E);
Expand Down
9 changes: 4 additions & 5 deletions code/handmade_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,22 +192,22 @@ RenderCommandsToBitmap(game_render_commands* RenderCommands, loaded_bitmap* Outp
case RenderGroupEntryType_render_entry_clear: {
render_entry_clear* Entry = (render_entry_clear*)Data;

DrawRectangle(OutputTarget, v2{ 0, 0 }, v2{ (r32)OutputTarget->Width, (r32)OutputTarget->Height }, Entry->Color, ClipRect, Even);
DrawRectangle(OutputTarget, v2{ 0, 0 }, v2{ (r32)OutputTarget->Width, (r32)OutputTarget->Height }, Entry->PremulColor, ClipRect, Even);
BaseAddress += sizeof(*Entry);
} break;

case RenderGroupEntryType_render_entry_rectangle: {
render_entry_rectangle* Entry = (render_entry_rectangle*)Data;

DrawRectangle(OutputTarget, Entry->P, Entry->P + Entry->Dim, Entry->Color, ClipRect, Even);
DrawRectangle(OutputTarget, Entry->P, Entry->P + Entry->Dim, Entry->PremulColor, ClipRect, Even);

BaseAddress += sizeof(*Entry);
} break;
case RenderGroupEntryType_render_entry_bitmap: {
render_entry_bitmap* Entry = (render_entry_bitmap*)Data;
v2 XAxis = {1, 0};
v2 YAxis = {0, 1};
DrawRectangle2(OutputTarget, Entry->P, Entry->XAxis, Entry->YAxis, Entry->Color, Entry->Bitmap, NullPixelsToMeters, ClipRect, Even);
DrawRectangle2(OutputTarget, Entry->P, Entry->XAxis, Entry->YAxis, Entry->PremulColor, Entry->Bitmap, NullPixelsToMeters, ClipRect, Even);

BaseAddress += sizeof(*Entry);

Expand Down Expand Up @@ -657,8 +657,7 @@ void
DrawRectangle2(loaded_bitmap* Buffer, v2 Origin, v2 AxisX, v2 AxisY, v4 Color, loaded_bitmap* Texture, r32 PixelsToMeters, rectangle2i ClipRect, b32 Even) {
TIMED_FUNCTION();
Assert(Texture->Memory);
// premultiply alpha
Color.rgb *= Color.a;


r32 AxisXLength = Length(AxisX);
r32 AxisYLength = Length(AxisY);
Expand Down
26 changes: 21 additions & 5 deletions code/handmade_render_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ GetBitmapDim(render_group* Group, object_transform ObjectTransform, loaded_bitma
return(Result);
}

inline v4
StoreColor(render_group *Group, v4 Src) {
v4 Dest;
v4 t = Group->tGlobalColor;
v4 C = Group->GlobalColor;

Dest.a = Lerp(Src.a, t.a, C.a);

Dest.r = Dest.a * Lerp(Src.r, t.r, C.r);
Dest.g = Dest.a * Lerp(Src.g, t.g, C.g);
Dest.b = Dest.a * Lerp(Src.b, t.b, C.b);


return(Dest);
}

inline void
PushBitmap(render_group* Group, object_transform ObjectTransform, loaded_bitmap* Bitmap, r32 Height, v3 Offset, v4 Color = v4{ 1.0f, 1.0, 1.0f, 1.0f }, r32 CAlign = 1.0f, v2 XAxis = v2{1, 0}, v2 YAxis = v2{0, 1}) {
used_bitmap_dim BitmapDim = GetBitmapDim(Group, ObjectTransform, Bitmap, Height, Offset, CAlign, XAxis, YAxis);
Expand All @@ -89,7 +105,7 @@ PushBitmap(render_group* Group, object_transform ObjectTransform, loaded_bitmap*
if (Entry) {
Entry->P = BitmapDim.Basis.P;
Entry->Bitmap = Bitmap;
Entry->Color = Group->GlobalAlpha * Color;
Entry->PremulColor = StoreColor(Group, Color);
v2 Size = BitmapDim.Basis.Scale * BitmapDim.Size;
Entry->XAxis = Size.x * XAxis;
Entry->YAxis = Size.y * YAxis;
Expand Down Expand Up @@ -139,7 +155,7 @@ PushRect(render_group* Group, object_transform ObjectTransform, v3 Offset, v2 Di
render_entry_rectangle* Entry = PushRenderElement(Group, render_entry_rectangle, Basis.SortKey);
if (Entry) {
Entry->P = Basis.P;
Entry->Color = Color;
Entry->PremulColor = StoreColor(Group, Color);
Entry->Dim = Basis.Scale * Dim;
}
}
Expand All @@ -165,7 +181,7 @@ Clear(render_group* Group, v4 Color) {

render_entry_clear* Piece = PushRenderElement(Group, render_entry_clear, Real32Minimum);
if (Piece) {
Piece->Color = Color;
Piece->PremulColor = StoreColor(Group, Color);
}
}

Expand All @@ -188,7 +204,7 @@ CoordinateSystem(render_group* Group, v2 Origin, v2 AxisX, v2 AxisY, v4 Color, l
}

inline u32
PushClipRect(render_group* Group, u32 X, u32 Y, u32 W, u32 H) {
PushClipRect(render_group* Group, u32 X, u32 Y, u32 W, u32 H, clip_rect_fx FX = {}) {
u32 Result = 0;
u32 Size = sizeof(render_entry_cliprect);
game_render_commands *Commands = Group->Commands;
Expand Down Expand Up @@ -291,7 +307,7 @@ BeginRenderGroup(game_assets* Assets, game_render_commands *Commands, u32 Genera
Result.Commands = Commands;

Result.Assets = Assets;
Result.GlobalAlpha = 1.0f;

Result.MissingResourceCount = 0;
Result.RenderInBackground = RenderInBackground;

Expand Down
Loading

0 comments on commit ec71a72

Please sign in to comment.