static void PostMsg(project_state *State, char *msg) { State->MsgTime = 120; State->Msg = msg; } static uint16 Source_Generate_Blank(project_data *File, project_state *State, memory *Memory, uint16 Width, uint16 Height, uint16 BytesPerPixel) { Assert(File->Source_Count < MAX_SOURCES); uint16 Index = Memory_Block_AllocateNew(Memory, F_Sources); block_source *Source = (block_source *)Memory_Block_AddressAtIndex(Memory, F_Sources, Index); Source->Occupied = 1; Source->Width = Width; Source->Height = Height; Source->BytesPerPixel= BytesPerPixel; Source->Type = source_type_principal; Source->Bitmap_Index = Memory_Block_PrincipalBitmap_AllocateNew(File, State, Memory); Source->Path_String_Index = String_AddToFile(Memory, "test"); // History_Action_Swap(Memory, F_File, sizeof(File->Source_Count), &File->Source_Count); File->Source_Count++; return Index; } static bool32 Source_IsFileSupported(char *Path, bool32 *IsVideo) { return stbi_info(Path, NULL, NULL, NULL); // AV_IsFileSupported(Path, IsVideo) } static void Source_Delete(project_data *File, memory *Memory, uint32 Index) { block_source *Source = (block_source *)Memory_Block_AddressAtIndex(Memory, F_Sources, Index); History_Action_Block_Swap(Memory, F_Sources, Source); Source->Occupied = 0; History_Action_Swap(Memory, F_File, sizeof(File->Source_Count), &File->Source_Count); File->Source_Count--; } static int16 Source_Generate(project_data *File, project_state *State, memory *Memory, void *TempString) { Assert(File->Source_Count < MAX_SOURCES); bool32 IsVideo = 0; if (Source_IsFileSupported((char *)TempString, &IsVideo)) { uint16 Index = Memory_Block_AllocateNew(Memory, F_Sources); block_source *Source = (block_source *)Memory_Block_AddressAtIndex(Memory, F_Sources, Index); History_Entry_Commit(Memory, "Add source"); History_Action_Block_Swap(Memory, F_Sources, Source); Source->Occupied = 1; Source->Path_String_Index = String_AddToFile(Memory, (char *)TempString); Assert(!IsVideo); Source->Type = source_type_file; History_Action_Swap(Memory, F_File, sizeof(File->Source_Count), &File->Source_Count); File->Source_Count++; History_Entry_End(Memory); return Index; } else { PostMsg(State, "File not supported..."); } return -1; } static bezier_point * Bezier_Lookup(memory *Memory, property_channel *Property, uint16 Index) { Assert(Index < MAX_KEYFRAMES_PER_BLOCK); block_bezier *Bezier = (block_bezier *)Memory_Block_AddressAtIndex(Memory, F_Bezier, Property->Block_Bezier_Index[0]); return &Bezier->Point[Index]; } static void Bezier_Add(memory *Memory, property_channel *Property, bezier_point PointData) { if (!Property->Block_Bezier_Count) { Property->Block_Bezier_Index[0] = Memory_Block_AllocateNew(Memory, F_Bezier); block_bezier *Bezier = (block_bezier *)Memory_Block_AddressAtIndex(Memory, F_Bezier, Property->Block_Bezier_Index[0]); Bezier->Occupied = true; // NOTE(fox): Effects will change this! History_Action_Swap(Memory, F_Layers, sizeof(Property->Block_Bezier_Count), &Property->Block_Bezier_Count); Property->Block_Bezier_Count++; } int k = 0; for (;;) { bezier_point *Point = Bezier_Lookup(Memory, Property, k); if (!Point->Occupied) { History_Action_Swap(Memory, F_Bezier, sizeof(*Point), Point); *Point = PointData; History_Action_Swap(Memory, F_Layers, sizeof(Property->Keyframe_Count), &Property->Keyframe_Count); Property->Keyframe_Count++; return; } k++; } } static property_channel Property_InitFloat(char *Name, real32 Val, real32 ScrubVal, real32 MinVal = PROPERTY_REAL_MIN, real32 MaxVal = PROPERTY_REAL_MAX) { property_channel Property = {}; Property.Name = Name; Property.CurrentValue = Val; Property.MinVal = MinVal; Property.MaxVal = MaxVal; Property.ScrubVal = ScrubVal; return Property; } // static property_info // Property_Sort(memory *Memory, property_channel *Property) // { // } static property_info Property_GetInfo(memory *Memory, property_channel *Property) { property_info PropertyInfo = {}; int h = 0, c = 0, i = 0; while (Block_Loop(Memory, Property, Property->Keyframe_Count, &h, &c, &i)) { bezier_point *Point = Bezier_Lookup(Memory, Property, i); PropertyInfo.MinVal = (Point->Pos[0].y < PropertyInfo.MinVal) ? Point->Pos[0].y : PropertyInfo.MinVal; PropertyInfo.MaxVal = (Point->Pos[0].y > PropertyInfo.MaxVal) ? Point->Pos[0].y : PropertyInfo.MaxVal; } return PropertyInfo; } static graph_info Graph_GetInfo(project_data *File, memory *Memory) { graph_info GraphInfo = {}; for (int a = 0; a < File->Layer_Count; a++) { block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, a); if (!Layer->IsSelected) continue; for (int h = 0; h < AmountOf(Layer->Property); h++) { property_channel *Property = &Layer->Property[h]; if (Property->Block_Bezier_Count) { property_info PropertyInfo = Property_GetInfo(Memory, Property); GraphInfo.MinVal = (PropertyInfo.MinVal < GraphInfo.MinVal) ? PropertyInfo.MinVal : GraphInfo.MinVal; GraphInfo.MaxVal = (PropertyInfo.MaxVal > GraphInfo.MaxVal) ? PropertyInfo.MaxVal : GraphInfo.MaxVal; } } GraphInfo.LowestOffset = (Layer->Vertical_Offset > GraphInfo.LowestOffset) ? Layer->Vertical_Offset : GraphInfo.LowestOffset; } return GraphInfo; } static void Keyframe_Interact_Evaluate(memory *Memory, project_state *State, uint8 IsSelected, v2 Pos_Initial[3], v2 Pos_New[3]) { /* Assert(Pos_New[0].y == Pos_Initial[0].y); if (State->Interact_Active == interact_type_keyframe_move) { Pos_New[0].x = Pos_New[0].x + Offset.x; Pos_New[0].y = Pos_New[0].y + Offset.y; } else if (State->Interact_Active == interact_type_keyframe_scale) { Pos_New[1].x += (IsSelected - 1 == 0 || IsSelected - 1 == 1) ? Offset.x : 0; Pos_New[2].x -= (IsSelected - 1 == 0 || IsSelected - 1 == 2) ? Offset.x : 0; } else if (State->Interact_Active == interact_type_keyframe_rotate) { real32 Rad = State->Interact_Offset[0]; v2 XAxis = V2(8, 8) * V2(cos(Rad), sin(Rad)); v2 YAxis = V2(sin(Rad), -cos(Rad)); Pos_New[2].x += XAxis.x + XAxis.y; Pos_New[2].y += YAxis.x + YAxis.y; } */ } static block_composition * Precomp_Init(project_data *File, memory *Memory) { if (File->Comp_Count + 1 > MAX_COMPS) { Assert(0); } block_composition *Comp = (block_composition *)Memory_Block_AllocateAddress(Memory, F_Precomps); History_Action_Block_Swap(Memory, F_Precomps, Comp); *Comp = {}; Comp->Occupied = 1; Comp->Name_String_Index = Memory_Block_AllocateNew(Memory, F_Strings); block_string *String = (block_string *)Memory_Block_AddressAtIndex(Memory, F_Strings, Comp->Name_String_Index); sprintf(String->Char, "Comp %i", File->Comp_Count); String->Occupied = 1; History_Action_Swap(Memory, F_File, sizeof(File->Comp_Count), &File->Comp_Count); File->Comp_Count++; return Comp; } static layer_transforms Layer_GetTransforms(block_layer *Layer) { return { Layer->x.CurrentValue, Layer->y.CurrentValue, Layer->ax.CurrentValue, Layer->ay.CurrentValue, Layer->rotation.CurrentValue, Layer->scale.CurrentValue }; } static void Layer_Interact_Evaluate(memory *Memory, project_state *State, uint16 Layer_Index_Physical, sorted_comp_info SortedCompInfo, sorted_layer *SortedLayerInfo, int32 *Frame_Start, int32 *Frame_End) { if (State->Interact_Active == interact_type_layer_move) { *Frame_Start += (int32)(State->Interact_Offset[0] + 0); *Frame_End += (int32)(State->Interact_Offset[0] + 0); } if (State->Interact_Active == interact_type_layer_timeadjust) { int Side[2] = {0}; Assert(State->Interact_Offset[1] == 0 || State->Interact_Offset[1] == 1); Side[(int)State->Interact_Offset[1]] = 1; *Frame_Start += (int32)(State->Interact_Offset[0] * Side[0]); if (*Frame_Start >= *Frame_End) *Frame_Start = *Frame_End - 1; *Frame_End += (int32)(State->Interact_Offset[0] * Side[1]); if (*Frame_End <= *Frame_Start) *Frame_End = *Frame_Start + 1; } } static void Layer_Select(memory *Memory, project_state *State, int32 i) { block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, i); Layer->IsSelected = true; State->MostRecentlySelectedLayer = i; // State->RecentSelectionType = selection_layer; } // TODO(fox): Precomps! void Layer_DeselectAll(memory *Memory, uint32 LayerCount) { for (uint32 i = 0; i < LayerCount; i++) { block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, i); if (Layer->IsSelected) Layer->IsSelected = false; } } void Source_DeselectAll(project_data *File, memory *Memory) { int h = 0, c = 0, i = 0; while (Block_Loop(Memory, F_Sources, File->Source_Count, &h, &c, &i)) { block_source *Source = (block_source *)Memory_Block_AddressAtIndex(Memory, F_Sources, i); Source->IsSelected = 0; } } static sorted_layer * Layer_GetSortedArray(sorted_layer *LayerArrayStart, sorted_comp_info *SortedCompStart, uint32 TargetComp) { uint32 LayerOffset = 0; int s = 0; while (s < TargetComp) { LayerOffset += SortedCompStart[s].LayerCount; s++; } return LayerArrayStart + LayerOffset; } int32 Layer_TestSelection(memory *Memory, project_state *State, ui *UI, sorted_comp_info *SortedCompArray, sorted_layer *SortedLayerArray, uint16 PrincipalIndex) { block_composition *Comp = (block_composition *)Memory_Block_AddressAtIndex(Memory, F_Precomps, PrincipalIndex); sorted_comp_info SortedCompInfo = SortedCompArray[PrincipalIndex]; sorted_layer *SortedLayerInfo = Layer_GetSortedArray(SortedLayerArray, SortedCompArray, PrincipalIndex); int32 LayerIndex = -1; // for (int i = 0; i < SortedCompInfo.LayerCount; i++) { for (int i = SortedCompInfo.LayerCount - 1; i >= 0; i--) { sorted_layer SortEntry = SortedLayerInfo[i]; uint32 Index_Physical = SortEntry.Block_Layer_Index; block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, Index_Physical); block_source *Source = (block_source *)Memory_Block_AddressAtIndex(Memory, F_Sources, Layer->Block_Source_Index); layer_transforms T = Layer_GetTransforms(Layer); v2 UV = T_CompUVToLayerUV(T, Comp->Width, Comp->Height, Source->Width, Source->Height, UI->TempZoomRatio); if (UV.x <= 1.0f && UV.x >= 0.0f && UV.y <= 1.0f && UV.y >= 0.0f && !Layer->IsSelected) { LayerIndex = Index_Physical; break; } // if (Layer->IsPrecomp) { // Layer_RecursiveDeselect(Memory, SortedCompArray, SortedLayerArray, TargetIndex, Layer->Block_Source_Index); // } // if (Layer->Block_Composition_Index != TargetIndex) { // Layer->IsSelected = false; // } } return LayerIndex; } void Layer_RecursiveDeselect(memory *Memory, sorted_comp_info *SortedCompArray, sorted_layer *SortedLayerArray, uint16 TargetIndex, uint16 PrincipalIndex) { block_composition *Comp = (block_composition *)Memory_Block_AddressAtIndex(Memory, F_Precomps, PrincipalIndex); sorted_comp_info SortedCompInfo = SortedCompArray[PrincipalIndex]; sorted_layer *SortedLayerInfo = Layer_GetSortedArray(SortedLayerArray, SortedCompArray, PrincipalIndex); for (int i = 0; i < SortedCompInfo.LayerCount; i++) { sorted_layer SortEntry = SortedLayerInfo[i]; uint32 Index_Physical = SortEntry.Block_Layer_Index; block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, Index_Physical); if (Layer->IsPrecomp) { Layer_RecursiveDeselect(Memory, SortedCompArray, SortedLayerArray, TargetIndex, Layer->Block_Source_Index); } if (Layer->Block_Composition_Index != TargetIndex) { Layer->IsSelected = false; } } } void Layer_Sort_CheckPrev(memory *Memory, int i, int Direction, sorted_layer *SortedLayerInfo, sorted_comp_info SortedCompInfo, int *EntriesPassed, sorted_layer *LayerEntry) { int PrevOffsetIndex = i + Direction + (*EntriesPassed * Direction); bool32 OutOfBounds = (Direction > 0) ? (PrevOffsetIndex > (SortedCompInfo.LayerCount - 1)) : (PrevOffsetIndex < 0); if (!OutOfBounds) { sorted_layer *PrevLayerEntry = &SortedLayerInfo[PrevOffsetIndex]; // block_layer *PrevLayer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, PrevLayerEntry->Block_Layer_Index); // real32 PrevOffset = PrevLayer->Vertical_Offset; real32 PrevOffset = PrevLayerEntry->SortedOffset; if (PrevOffset == (LayerEntry->SortedOffset - Direction)) { LayerEntry->SortedOffset -= Direction; (*EntriesPassed)++; Layer_Sort_CheckPrev(Memory, i, Direction, SortedLayerInfo, SortedCompInfo, EntriesPassed, LayerEntry); } } } // TODO(fox): This _could_ be generalized with the layer sorting code... /* Bezier_SortAll() { while (Block_Loop(Memory, F_Layers, File->Layer_Count, &h, &c, &i)) { block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, i); sorted_comp_info *SortedCompInfo = &CompStart[Layer->Block_Composition_Index]; sorted_layer *SortedLayerInfo = Layer_GetSortedArray(LayerArrayStart, CompStart, Layer->Block_Composition_Index); uint32 SortedIndex_Playhead = 0; while (SortedIndex_Playhead < CurrentSortIndex) { sorted_layer LayerEntry = SortedLayerInfo[SortedIndex_Playhead]; block_layer *TestLayer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, LayerEntry.Block_Layer_Index); if (-Layer->Vertical_Offset < -TestLayer->Vertical_Offset) { break; } else { SortedIndex_Playhead++; } } if (SortedIndex_Playhead != CurrentSortIndex) { uint8 *Address_Start = (uint8 *)(SortedLayerInfo + SortedIndex_Playhead); uint8 *Address_End = (uint8 *)(SortedLayerInfo + CurrentSortIndex) - 1; Assert(CurrentSortIndex != SortedCompInfo->LayerCount); Arbitrary_ShiftData(Address_Start, Address_End, sizeof(sorted_layer), 1); } sorted_layer *LayerEntry = SortedLayerInfo + SortedIndex_Playhead; LayerEntry->Block_Layer_Index = i; LayerEntry->SortedOffset = Layer->Vertical_Offset; CurrentSortIndex++; } } */ void Layer_SortAll(project_data *File, project_state *State, memory *Memory, sorted_layer *LayerArrayStart, sorted_comp_info *CompStart, uint32 LayerCount, uint32 CompCount) { int h = 0, c = 0, i = 0; while (Block_Loop(Memory, F_Layers, File->Layer_Count, &h, &c, &i)) { block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, i); Assert(Layer->Block_Composition_Index < CompCount); CompStart[Layer->Block_Composition_Index].LayerCount++; } h = 0, c = 0, i = 0; uint32 CurrentSortIndex = 0; while (Block_Loop(Memory, F_Layers, File->Layer_Count, &h, &c, &i)) { block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, i); sorted_comp_info *SortedCompInfo = &CompStart[Layer->Block_Composition_Index]; sorted_layer *SortedLayerInfo = Layer_GetSortedArray(LayerArrayStart, CompStart, Layer->Block_Composition_Index); uint32 SortedIndex_Playhead = 0; while (SortedIndex_Playhead < CurrentSortIndex) { sorted_layer LayerEntry = SortedLayerInfo[SortedIndex_Playhead]; block_layer *TestLayer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, LayerEntry.Block_Layer_Index); if (-Layer->Vertical_Offset < -TestLayer->Vertical_Offset) { break; } else { SortedIndex_Playhead++; } } if (SortedIndex_Playhead != CurrentSortIndex) { uint8 *Address_Start = (uint8 *)(SortedLayerInfo + SortedIndex_Playhead); uint8 *Address_End = (uint8 *)(SortedLayerInfo + CurrentSortIndex) - 1; Assert(CurrentSortIndex != SortedCompInfo->LayerCount); Arbitrary_ShiftData(Address_Start, Address_End, sizeof(sorted_layer), 1); } sorted_layer *LayerEntry = SortedLayerInfo + SortedIndex_Playhead; LayerEntry->Block_Layer_Index = i; LayerEntry->SortedOffset = Layer->Vertical_Offset; CurrentSortIndex++; } if (State->Interact_Active == interact_type_layer_move) { int32 Offset = (int32)State->Interact_Offset[1]; bool32 Direction = (Offset > 0) ? 1 : -1; for (uint32 c = 0; c < CompCount; c++) { sorted_comp_info *SortedCompInfo = &CompStart[c]; if (!SortedCompInfo->LayerCount) continue; sorted_layer *SortedLayerInfo = Layer_GetSortedArray(LayerArrayStart, CompStart, c); int i = (Direction > 0) ? SortedCompInfo->LayerCount - 1 : 0; bool32 Case = 1; while (Case) { int32 EntriesPassed = 0; sorted_layer *LayerEntry = &SortedLayerInfo[i]; block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, LayerEntry->Block_Layer_Index); Assert(LayerEntry->SortedOffset == Layer->Vertical_Offset); if (Layer->IsSelected) { int32 SpacesToMove = Offset * Direction; while (SpacesToMove) { Layer_Sort_CheckPrev(Memory, i, Direction, SortedLayerInfo, *SortedCompInfo, &EntriesPassed, LayerEntry); LayerEntry->SortedOffset -= Direction; SpacesToMove--; } } int b = 0; while (b < EntriesPassed) { sorted_layer *FrontEntry = &SortedLayerInfo[i+(b*Direction)]; sorted_layer *BackEntry = &SortedLayerInfo[i+((b+1)*Direction)]; sorted_layer Swap = *FrontEntry; *FrontEntry = *BackEntry; *BackEntry = Swap; b++; } i -= Direction; Case = (Direction > 0) ? (i >= 0) : (i < SortedCompInfo->LayerCount); } } } } static void Layer_Delete(project_data *File, memory *Memory, uint32 Index) { block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, Index); History_Action_Block_Swap(Memory, F_Layers, Layer); Layer->Occupied = 0; History_Action_Swap(Memory, F_File, sizeof(File->Layer_Count), &File->Layer_Count); File->Layer_Count--; } block_layer * Layer_Init(project_data *File, memory *Memory) { if (File->Layer_Count + 1 > MAX_LAYERS) { Assert(0); } block_layer *Layer = (block_layer *)Memory_Block_AllocateAddress(Memory, F_Layers); History_Action_Block_Swap(Memory, F_Layers, Layer); *Layer = {}; Layer->Occupied = 1; Layer->Block_String_Index = Memory_Block_AllocateNew(Memory, F_Strings); block_string *String = (block_string *)Memory_Block_AddressAtIndex(Memory, F_Strings, Layer->Block_String_Index); sprintf(String->Char, "Layer %i", File->Layer_Count + 1); // CSbros... String->Occupied = 1; Layer->x = Property_InitFloat("X Position", 0.0f, 1.0f); Layer->y = Property_InitFloat("Y Position", 0.0f, 1.0f); Layer->ax = Property_InitFloat("Anchor X", 0.5f, 0.005f); Layer->ay = Property_InitFloat("Anchor Y", 0.5f, 0.005f); Layer->scale = Property_InitFloat("Scale", 1.0f, 0.005f); Layer->rotation = Property_InitFloat("Rotation", 0.0f, 1.0f); Layer->opacity = Property_InitFloat("Opacity", 1.0f, 0.005f, 0.0f, 1.0f); Layer->time = Property_InitFloat("Frame Number", 0.0f, 1.0f, 0, 100000); Layer->IsVisible = 1; History_Action_Swap(Memory, F_File, sizeof(File->Layer_Count), &File->Layer_Count); File->Layer_Count++; return Layer; } void Source_UICreateButton(project_data *File, project_state *State, memory *Memory, sorted_comp_info *SortedCompArray, sorted_layer *SortedLayerArray) { block_composition *Comp = (block_composition *)Memory_Block_AddressAtIndex(Memory, F_Precomps, File->PrincipalCompIndex); sorted_comp_info *SortedCompInfo = &SortedCompArray[File->PrincipalCompIndex]; int32 TopOffset; if (!File->Layer_Count) { TopOffset = 11; } else { sorted_layer *SortedLayerInfo = Layer_GetSortedArray(SortedLayerArray, SortedCompArray, File->PrincipalCompIndex); block_layer *TopLayer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, SortedLayerInfo[SortedCompInfo->LayerCount - 1].Block_Layer_Index); TopOffset = TopLayer->Vertical_Offset; } bool32 CommitAction = 0; int h = 0, c = 0, i = 0; while (Block_Loop(Memory, F_Sources, File->Source_Count, &h, &c, &i)) { block_source *Source = (block_source *)Memory_Block_AddressAtIndex(Memory, F_Sources, i); if (Source->IsSelected) { if (!CommitAction) { History_Entry_Commit(Memory, "Create layer from source"); CommitAction = 1; } block_layer *Layer = Layer_Init(File, Memory); Layer->Block_Source_Index = i; Layer->x.CurrentValue = Source->Width / 2; Layer->y.CurrentValue = Source->Height / 2; Layer->Vertical_Offset = TopOffset-1; Layer->Frame_End = Comp->Frame_End; TopOffset--; } State->UpdateFrame = true; } if (CommitAction) History_Entry_End(Memory); Source_DeselectAll(File, Memory); } // Helper for working with different bit depths. static render_byte_info Bitmap_ByteInfo(uint32 BytesPerPixel) { render_byte_info Byte = {}; if (BytesPerPixel == 4) { Byte.MaskPixel = 0xFF; Byte.ByteOffset = 1; Byte.Normalized = 1 / 255.0f; Byte.Bits = 255; } else if (BytesPerPixel == 8) { Byte.MaskPixel = 0xFFFF; Byte.ByteOffset = 2; Byte.Normalized = 1 / 65535.0f; Byte.Bits = 65535; } else { Byte.MaskPixel = 0xFFFFFFFF; Byte.ByteOffset = 4; Byte.Normalized = 1 / 4294967295.0f; Byte.Bits = 4294967295; Assert(0); } return Byte; } // TODO(fox): Make separate full-size bitmap that gets scaled on the GPU instead of this static void State_BindBrushTexture(memory *Memory, brush_state *Brush, uint32 BytesPerPixel) { GL_GenAndBindTexture(&Brush->GLTexture, Brush->Size, Brush->Size, BytesPerPixel, Brush->PaintBuffer); } static void Brush_CalcBitmapAlphaFromSize(memory *Memory, brush_state *Brush, uint32 BytesPerPixel) { real32 BrushLength = Brush->Size; real32 BrushCenter = BrushLength / 2; real32 MaxLength = BrushCenter; void *BrushAddress = Brush->PaintBuffer; render_byte_info Byte = Bitmap_ByteInfo(BytesPerPixel); for (int Y = 0; Y < (int)BrushLength; Y++) { for (int X = 0; X < (int)BrushLength; X++) { uint8 *PixelAddress = (uint8 *)BrushAddress + (Y * BytesPerPixel*(int)BrushLength) + (X * BytesPerPixel); uint32 *R_DestAddress = (uint32 *)(PixelAddress + Byte.ByteOffset*0); // uint32 *G_DestAddress = (uint32 *)(PixelAddress + Byte.ByteOffset*1); // uint32 *B_DestAddress = (uint32 *)(PixelAddress + Byte.ByteOffset*2); uint32 *A_DestAddress = (uint32 *)(PixelAddress + Byte.ByteOffset*3); v2 Pos = V2(BrushCenter - X, BrushCenter - Y); real32 L = sqrt(LengthSq(Pos)); real32 Gradient = Ceil(L, MaxLength) / MaxLength; Gradient = pow(Gradient, Brush->Hardness); *R_DestAddress = (*R_DestAddress & ~Byte.MaskPixel) | Byte.Bits; // brush preview is red *A_DestAddress = (*A_DestAddress & ~Byte.MaskPixel) | (uint32)((1.0f - Gradient)*Byte.Bits); } } } // TODO(fox): We could merge this exactly with the standard rendering code. static void PaintTest(memory *Memory, block_source *Source, brush_state *Brush, void *Address, v2 LayerPos, uint32 BytesPerPixel, v4 Color) { uint32 BrushLength = (uint32)Brush->Size; rectangle RenderRegion = { 0, 0, Source->Width, Source->Height }; rectangle BrushPos = { (int32)(LayerPos.x - (BrushLength / 2)), (int32)(LayerPos.y - (BrushLength / 2)), (int32)(LayerPos.x + (BrushLength / 2)), (int32)(LayerPos.y + (BrushLength / 2)) }; rectangle LayerBounds = ClipRectangle(BrushPos, RenderRegion); if (BrushPos.Min.x < Brush->CacheBounds.Min.x) Brush->CacheBounds.Min.x = BrushPos.Min.x; if (BrushPos.Min.y < Brush->CacheBounds.Min.y) Brush->CacheBounds.Min.y = BrushPos.Min.y; if (BrushPos.Max.x > Brush->CacheBounds.Max.x) Brush->CacheBounds.Max.x = BrushPos.Max.x; if (BrushPos.Max.y > Brush->CacheBounds.Max.y) Brush->CacheBounds.Max.y = BrushPos.Max.y; uint32 LayerPitch = Source->Width*Source->BytesPerPixel; uint32 BrushPitch = (int)BrushLength * BytesPerPixel; render_byte_info LayerBits = Bitmap_ByteInfo(Source->BytesPerPixel); render_byte_info BrushBits = Bitmap_ByteInfo(BytesPerPixel); int32 ExtraX = 0; int32 ExtraY = 0; if (BrushPos.Min.x < 0) { ExtraX = BrushPos.Min.x; } if (BrushPos.Min.y < 0) { ExtraY = BrushPos.Min.y; } void *BrushBuffer = Brush->PaintBuffer; // ImGui's color picker works in sRGB, so we need to convert to linear. // real32 R_Brush = (Color.r >= 0.04045) ? pow((Color.r + 0.055) / (1 + 0.055), 2.4) : Color.r / 12.92; // real32 G_Brush = (Color.g >= 0.04045) ? pow((Color.g + 0.055) / (1 + 0.055), 2.4) : Color.g / 12.92; // real32 B_Brush = (Color.b >= 0.04045) ? pow((Color.b + 0.055) / (1 + 0.055), 2.4) : Color.b / 12.92; // real32 A_Brush = (Color.a >= 0.04045) ? pow((Color.a + 0.055) / (1 + 0.055), 2.4) : Color.a / 12.92; real32 R_Brush = Color.r; real32 G_Brush = Color.g; real32 B_Brush = Color.b; real32 A_Brush = Color.a; uint8 *BrushRow = (uint8 *)BrushBuffer; for (int32 Y = LayerBounds.Min.y; Y < LayerBounds.Max.y; Y++) { for (int32 X = LayerBounds.Min.x; X < LayerBounds.Max.x; X++) { uint32 Offset = Y*LayerPitch + X*Source->BytesPerPixel; uint8 *LayerPixel = (uint8 *)Address + Offset; uint32 *R_DestAddress = (uint32 *)(LayerPixel + LayerBits.ByteOffset * 0); uint32 *G_DestAddress = (uint32 *)(LayerPixel + LayerBits.ByteOffset * 1); uint32 *B_DestAddress = (uint32 *)(LayerPixel + LayerBits.ByteOffset * 2); uint32 *A_DestAddress = (uint32 *)(LayerPixel + LayerBits.ByteOffset * 3); real32 R_Layer = (real32)(*R_DestAddress & LayerBits.MaskPixel) * LayerBits.Normalized; real32 G_Layer = (real32)(*G_DestAddress & LayerBits.MaskPixel) * LayerBits.Normalized; real32 B_Layer = (real32)(*B_DestAddress & LayerBits.MaskPixel) * LayerBits.Normalized; real32 A_Layer = (real32)(*A_DestAddress & LayerBits.MaskPixel) * LayerBits.Normalized; int32 TrueX = (X - LayerBounds.Min.x) - ExtraX; int32 TrueY = (Y - LayerBounds.Min.y) - ExtraY; // Assert(TrueX >= 0 && TrueX < BrushLength); // Assert(TrueY >= 0 && TrueY < BrushLength); uint8 *BrushPixel = (uint8 *)BrushBuffer + (TrueY * BrushPitch) + (TrueX * BytesPerPixel); real32 Brush_BitmapAlpha = (real32)((*(uint32 *)(BrushPixel + BrushBits.ByteOffset*3)) & BrushBits.MaskPixel) * BrushBits.Normalized; real32 BrushAlpha = Brush_BitmapAlpha * A_Brush; real32 LayerAlpha = A_Layer; real32 A_Blend = LayerAlpha + BrushAlpha; real32 R_Blend = (R_Layer * (1.0f - BrushAlpha)) + (R_Brush * BrushAlpha); real32 G_Blend = (G_Layer * (1.0f - BrushAlpha)) + (G_Brush * BrushAlpha); real32 B_Blend = (B_Layer * (1.0f - BrushAlpha)) + (B_Brush * BrushAlpha); /* R_Blend = (R_Brush * (1.0f - LayerAlpha)) + (R_Blend * LayerAlpha); G_Blend = (G_Brush * (1.0f - LayerAlpha)) + (G_Blend * LayerAlpha); B_Blend = (B_Brush * (1.0f - LayerAlpha)) + (B_Blend * LayerAlpha); */ Assert(R_Blend <= 1.0f); uint32 R_Out = (uint32)(Normalize(R_Blend) * LayerBits.Bits); uint32 G_Out = (uint32)(Normalize(G_Blend) * LayerBits.Bits); uint32 B_Out = (uint32)(Normalize(B_Blend) * LayerBits.Bits); uint32 A_Out = (uint32)(Normalize(A_Blend) * LayerBits.Bits); *R_DestAddress = (*R_DestAddress & ~LayerBits.MaskPixel) | R_Out; *G_DestAddress = (*G_DestAddress & ~LayerBits.MaskPixel) | G_Out; *B_DestAddress = (*B_DestAddress & ~LayerBits.MaskPixel) | B_Out; *A_DestAddress = (*A_DestAddress & ~LayerBits.MaskPixel) | A_Out; } } } #if 0 static void IncrementFrame(project_data *File, int16 Amount) { if ((File->CurrentFrame <= 0 && Amount < File->StartFrame) || (File->CurrentFrame >= File->EndFrame)) { File->CurrentFrame = 0; } else { File->CurrentFrame += Amount; } } static void CreateKeyframeBlock(property_channel *Property, memory *Memory) { int16 a = Property->NumberOfKeyframeBlocks++; Assert(a < MAX_KEYFRAME_BLOCKS); Property->KeyframeBlock[a] = (keyframe_block *)AllocateMemory(Memory, sizeof(keyframe_block), F_Keyframes); } // Note we use total bytes here so we can use this memory for both packed and unpacked bitmaps. void * Layer_AllocateBitmap(memory *Memory, uint16 Width, uint16 Height, uint16 BytesPerPixel) { uint64 TotalBytes = Bitmap_CalcTotalBytes(Width, Height, BytesPerPixel); void *Address = AllocateMemory(Memory, TotalBytes, B_LayerBitmaps); return Address; } static cached_bitmap * Cache_CheckBitmap(source *Source, layer_bitmap_info *BitmapInfo, memory *Memory, int32 TimelineFrame) { if (!Source || !BitmapInfo) return 0; memory_table *Table = &Memory->Slot[B_LoadedBitmaps]; int32 FrameToSeek = TimelineFrame - BitmapInfo->FrameOffset; if (FrameToSeek < 0) { FrameToSeek = 0; } // Stills have a frame index of one. if (Source->SourceType == source_type_image) { FrameToSeek = 0; } for (int i = 0; i < Table->NumberOfPointers; i++) { cached_bitmap *Bitmap = &Memory->Bitmap[i]; if (Bitmap->Frame == FrameToSeek && Bitmap->SourceOwner == Source) { return Bitmap; } } return 0; } static cached_bitmap * STB_LoadStill(source *Source, layer_bitmap_info *BitmapInfo, memory *Memory) { int n = 0; int h, w; void *temp = stbi_load(Source->Path, &w, &h, &n, 4); Assert(temp); cached_bitmap *Bitmap = Memory_RollingBitmap(Memory, Source, 0); // Note the use of Unpacked since we haven't packed the bitmap and don't have any padding. uint64 Size = Bitmap_CalcUnpackedBytes(Source->Info.Width, Source->Info.Height, Source->Info.BytesPerPixel); Bitmap_CopyToPointer(temp, Bitmap->Data, Source->Info.BytesPerPixel, Size); return Bitmap; } static void Layer_InitSource(project_layer *Layer, source *Source, memory *Memory) { uint16 Width = Source->Info.Width; uint16 Height = Source->Info.Height; uint16 BytesPerPixel = Source->Info.BytesPerPixel; Layer->BitmapInfo.BitmapBuffer = Layer_AllocateBitmap(Memory, Width, Height, BytesPerPixel); } static void Layer_PositionCenter(project_layer *Layer, uint16 Width, uint16 Height) { Layer->x.CurrentValue.f = Width/2; Layer->y.CurrentValue.f = Height/2; } static void Layer_CreateFromSource(project_data *File, project_state *State, memory *Memory, source *Source) { if (File->NumberOfLayers + 1 > MAX_LAYERS) { // TODO(fox): Output! } project_layer *Layer = Layer_Init(File, Memory); Layer->Source = Source; State->UpdateFrame = true; } static void Layer_UpdateBitmap(project_data *File, project_layer *Layer, memory *Memory, int32 CurrentFrame) { source *Source = Layer->Source; layer_bitmap_info *BitmapInfo = &Layer->BitmapInfo; // AVInfo is created here instead of on layer creation so we can save // having to keep some state in the undo/delete commands. if (!BitmapInfo->AVInfo) { BitmapInfo->AVInfo = AllocateMemory(Memory, sizeof(av_info), P_AVInfo); AV_Init(Source, (av_info *)BitmapInfo->AVInfo, Memory); Layer_InitSource(Layer, Source, Memory); Layer_PositionCenter(Layer, File->Width, File->Height); } cached_bitmap *Bitmap = Cache_CheckBitmap(Source, BitmapInfo, Memory, CurrentFrame); if (!Bitmap) { if (Source->SourceType == source_type_image) { Bitmap = STB_LoadStill(Source, BitmapInfo, Memory); } else { Bitmap = AV_LoadVideoFrame(Source, BitmapInfo, Memory, CurrentFrame); } } uint16 Width = Source->Info.Width; uint16 Height = Source->Info.Height; uint16 BytesPerPixel = Source->Info.BytesPerPixel; void *DestBuffer = BitmapInfo->BitmapBuffer; uint64 UnpackedSize = Bitmap_CalcUnpackedBytes(Source->Info.Width, Source->Info.Height, Source->Info.BytesPerPixel); #if PACKEDRGB uint64 PackedSize = Bitmap_CalcTotalBytes(Source->Info.Width, Source->Info.Height, Source->Info.BytesPerPixel); if (Layer->NumberOfMasks == 0 && Layer->NumberOfEffects == 0) { Bitmap_ConvertPacking(Bitmap->Data, Memory->Scratch, Width, Height, BytesPerPixel, 0); Bitmap_CopyToPointer(Memory->Scratch, DestBuffer, BytesPerPixel, PackedSize); } else { Bitmap_CopyToPointer(Bitmap->Data, DestBuffer, BytesPerPixel, UnpackedSize); // GL_InitTexture(&BitmapInfo->Test, DestBuffer, Width, Height); // GL_MaskTexture(&BitmapInfo->TestM, DestBuffer, Width, Height); if (Layer->NumberOfMasks) { for (int i = 0; i < Layer->NumberOfMasks; i++) { mask *Mask = &Layer->Mask[i]; if (Mask->IsClosed) Mask_TriangulateAndRasterize(Memory, Layer, Mask); } } for (int i = 0; i < Layer->NumberOfEffects; i++) { if (Layer->Effect[i]->IsActive) Layer->Effect[i]->func(Source, BitmapInfo, Memory, Layer->Effect[i]->Property); } Bitmap_ConvertPacking(DestBuffer, Memory->Scratch, Width, Height, BytesPerPixel, 0); Bitmap_CopyToPointer(Memory->Scratch, DestBuffer, BytesPerPixel, PackedSize); } #else Bitmap_CopyToPointer(Bitmap->Data, DestBuffer, BytesPerPixel, UnpackedSize); GL_UpdateTexture(&BitmapInfo->Test, DestBuffer, Width, Height, 0); GL_UpdateTexture(&BitmapInfo->TestM, DestBuffer, Width, Height, 1); if (Layer->NumberOfMasks) { for (int i = 0; i < Layer->NumberOfMasks; i++) { mask *Mask = &Layer->Mask[i]; if (Mask->IsClosed) Mask_TriangulateAndRasterize(Memory, Layer, Mask); } Bitmap_StencilAlpha(Bitmap->Data, DestBuffer, BytesPerPixel, UnpackedSize); } if (Layer->NumberOfEffects) { for (int i = 0; i < Layer->NumberOfEffects; i++) { if (Layer->Effect[i]->IsActive) Layer->Effect[i]->func(Source, BitmapInfo, Memory, Layer->Effect[i]->Property); } } #endif } static ImVec2 Layer_LocalToScreenSpace(project_layer *Layer, ui *UI, comp_buffer CompBuffer, v2 Point) { real32 Rad = (Layer->rotation.CurrentValue.f * (PI / 180)); real32 s = Layer->scale.CurrentValue.f; real32 AX = Layer->ax.CurrentValue.f; real32 AY = Layer->ay.CurrentValue.f; source *Source = Layer->Source; v2 XAxis = (Point.x - AX*Source->Info.Width) * s * V2(cos(Rad), sin(Rad)); v2 YAxis = (Point.y - AY*Source->Info.Height) * -s * V2(sin(Rad), -cos(Rad)); v2 LocalPoint = XAxis + YAxis; v2 CompUV = V2((Layer->x.CurrentValue.f + LocalPoint.x) / CompBuffer.Width, (Layer->y.CurrentValue.f + LocalPoint.y) / CompBuffer.Height); v2 ScreenPoint = V2(UI->CompPos.x + CompUV.x * UI->CompZoom.x, UI->CompPos.y + CompUV.y * UI->CompZoom.y); return ImVec2(ScreenPoint.x, ScreenPoint.y); } static v2 Layer_ScreenSpaceToLocal(project_layer *Layer, ui *UI, comp_buffer CompBuffer, ImVec2 ViewportMin, ImVec2 Point) { source *Source = Layer->Source; v2 CompUV = ImGui_ScreenPointToCompUV(ViewportMin, UI->CompPos, UI->CompZoom, Point); v2 LayerUV = CompUVToLayerUV(Layer, &CompBuffer, CompUV); return V2(LayerUV.x * Source->Info.Width, LayerUV.y * Source->Info.Height); } static void LoadTestFootage(project_data *File, project_state *State, memory *Memory) { void *SourceString = String_GenerateFromChar(Memory, "../asset/a.jpg"); Source_Generate(File, State, Memory, SourceString); SourceString = String_GenerateFromChar(Memory, "../asset/24.mp4"); Source_Generate(File, State, Memory, SourceString); SourceString = String_GenerateFromChar(Memory, "../asset/b.jpg"); Source_Generate(File, State, Memory, SourceString); SourceString = String_GenerateFromChar(Memory, "../asset/c.jpg"); Source_Generate(File, State, Memory, SourceString); SourceString = String_GenerateFromChar(Memory, "../asset/p.mp4"); Source_Generate(File, State, Memory, SourceString); Layer_CreateFromSource(File, State, Memory, &File->Source[0]); Keyframe_Insert(&File->Layer[0]->x, Memory, 01, -10); Keyframe_Insert(&File->Layer[0]->x, Memory, 10, -5); Keyframe_Insert(&File->Layer[0]->x, Memory, 23, 0); Keyframe_Insert(&File->Layer[0]->x, Memory, 34, 5); for (int i = 0; i < 5; i++) { keyframe *Keyframe = KeyframeLookup(&File->Layer[0]->x, i); Keyframe->TangentLeft = V2(-3, 0); Keyframe->TangentRight = V2(1.5, 0); Keyframe->Type = bezier; } File->Layer[0]->x.IsToggled = true; SelectLayer(File->Layer[0], State, 0); // AddEffect(File->Layer[0], Memory, 1); // property_channel *Property = &File->Layer[0]->x; // for (int i = 0; i < 16; i++) // Keyframe_Insert(Property, Memory, i*2, i*2*100); // Keyframe_Insert(Property, Memory, 1, 100); // Keyframe_Insert(Property, Memory, 15, 1500); // Keyframe_Insert(Property, Memory, 16, 1600); // Keyframe_Insert(Property, Memory, 31, 3100); // Keyframe_Delete(Property, Memory, 1); // History_Undo(Memory); // History_Redo(Memory); // Property->IsToggled = true; /* mask *Mask = &File->Layer[0]->Mask[0]; File->Layer[0]->NumberOfMasks = 1; Mask->Point[0].Pos = V2(200, 200); Mask->Point[1].Pos = V2(210, 400); Mask->Point[2].Pos = V2(220, 520); Mask->Point[3].Pos = V2(1380, 520); Mask->Point[4].Pos = V2(1480, 200); Mask->Point[0].TangentLeft = V2(-50, 0); Mask->Point[1].TangentLeft = V2(-50, 0); Mask->Point[2].TangentLeft = V2(-50, 0); Mask->Point[3].TangentLeft = V2(-50, 0); Mask->Point[4].TangentLeft = V2(-50, 0); Mask->Point[0].TangentRight = V2(50, 0); Mask->Point[1].TangentRight = V2(50, 0); Mask->Point[2].TangentRight = V2(50, 0); Mask->Point[3].TangentRight = V2(50, 0); Mask->Point[4].TangentRight = V2(50, 0); Mask->Point[0].HandleBezier = true; Mask->Point[1].HandleBezier = true; Mask->Point[2].HandleBezier = true; Mask->Point[3].HandleBezier = true; Mask->Point[4].HandleBezier = true; Mask->NumberOfPoints = 5; Mask->IsClosed = true; */ /* Mask_DeletePoint(Memory, Mask, 1); History_Undo(Memory); // History_Redo(Memory); Mask_AddPointToCurve(Memory, Mask, 1, 0.5); History_Undo(Memory); History_Redo(Memory); */ // if (!Source_Generate(File, Memory, "../asset/test.png")) // PostMsg(State, "File open fail..."); // if (!Source_Generate(File, Memory, "../asset/debug.png")) // PostMsg(State, "File open fail..."); // property_channel *Property = &File->Layer[0]->x; // Keyframe_Insert(Property, Memory, 1, 500); // Keyframe_Insert(Property, Memory, 30, 800); // Keyframe_Insert(Property, Memory, 15, 400); // Keyframe_Insert(Property, Memory, 20, 100); // Property->IsToggled = true; // Property->IsGraphToggled = true; // Property->GraphLength = 150; // Property->GraphYOffset = (Property->GraphWindowHeight - Property->GraphLength)/2; // Layer_CreateFromSource(File, State, Memory, Source); // if (!Source_Generate(File, Memory, "../asset/p.mp4")) // PostMsg(State, "File open fail..."); // source *Source2 = &File->Source[1]; // project_layer *Layer2 = Layer_Init(File, Memory); // AV_Init(Source2, &Layer2->BitmapInfo, Memory); // Layer_InitSource(Layer2, Source2, Memory); // Layer2->StartFrame = 11; // Layer2->EndFrame = 23; // void *SourceString1 = String_GenerateFromChar(Memory, "../asset/b.jpg"); // if (!Source_Generate(File, Memory, SourceString1)) // PostMsg(State, "File open fail..."); // source *Source1 = &File->Source[1]; // for (int i = 0; i < 25; i++) // Layer_CreateFromSource(File, State, Memory, Source1); // project_layer *Layer2 = Layer_Init(File, Memory); // Layer_InitSource(Layer2, Source2, Memory); // AddEffect(File->Layer[0], Memory, 2); // project_layer *Layer1 = CreateDebugLayer(&File, &Memory, 9, 14); // project_layer *Layer1 = CreateSolidLayer(&File, &Memory, 9, 13, V4(1.0, 1.0, 1.0, 1.0)); // Layer1->x.CurrentValue.f = 7; // Layer1->y.CurrentValue.f = 4; // Layer1->StartFrame = 0; // Layer1->EndFrame = File.EndFrame; } static void CreateDemoScene(project_data *File, project_state *State, memory *Memory) { Layer_CreateFromSource(File, State, Memory, &File->Source[1]); project_layer *Layer1 = File->Layer[0]; Layer1->x.CurrentValue.f = 1920/2; Layer1->y.CurrentValue.f = 1080/2; Layer1->StartFrame = 0; Layer1->EndFrame = File->EndFrame; Layer_CreateFromSource(File, State, Memory, &File->Source[2]); project_layer *Layer2 = File->Layer[1]; Layer2->x.CurrentValue.f = 1920/2; Layer2->y.CurrentValue.f = 1080/2; Layer2->StartFrame = 0; Layer2->EndFrame = File->EndFrame; Keyframe_Insert(&Layer2->rotation, Memory, 2, 0); Keyframe_Insert(&Layer2->rotation, Memory, 50, 360); Layer2->rotation.IsToggled = true; Layer2->scale.IsToggled = true; Layer_CreateFromSource(File, State, Memory, &File->Source[3]); project_layer *Layer3 = File->Layer[2]; Layer3->x.CurrentValue.f = 1920/4; Layer3->y.CurrentValue.f = 1080/4; Layer3->opacity.CurrentValue.f = 0.5f; Layer3->StartFrame = 0; Layer3->EndFrame = File->EndFrame; Keyframe_Insert(&Layer3->x, Memory, 2, Layer3->x.CurrentValue.f); Keyframe_Insert(&Layer3->x, Memory, 30, Layer3->x.CurrentValue.f+(1280/2)); Keyframe_Insert(&Layer3->x, Memory, 60, Layer3->x.CurrentValue.f+(1280/3)); Layer3->x.IsToggled = true; Layer3->y.IsToggled = true; } #endif