Cleaned up system start up, update, and cleanup

This commit is contained in:
PS 2021-01-11 00:02:42 -08:00
parent 71547b05dc
commit 914523cb60
18 changed files with 446 additions and 352 deletions

View File

@ -358,7 +358,7 @@ Editor_Render(app_state* State, context* Context, render_command_buffer* RenderB
}
Context->GeneralWorkQueue->CompleteQueueWork(Context->GeneralWorkQueue, Context->ThreadContext);
Context->GeneralWorkQueue->ResetWorkQueue(Context->GeneralWorkQueue);
ResetWorkQueue(Context->GeneralWorkQueue);
}

View File

@ -353,28 +353,28 @@ DrawPanelBorder(panel Panel, v2 PanelMin, v2 PanelMax, v4 Color, mouse_state* Mo
v2 LeftEdgeMin = PanelMin;
v2 LeftEdgeMax = v2{PanelMin.x + HighlightThickness, PanelMax.y};
PushRenderQuad2D(RenderBuffer, LeftEdgeMin, LeftEdgeMax, HighlightColor);
Mouse->CursorType = CursorType_HorizontalArrows;
Mouse->CursorType = CursorType_HArrows;
}
else if (MouseRightEdgeDistance < PANEL_EDGE_CLICK_MAX_DISTANCE)
{
v2 RightEdgeMin = v2{PanelMax.x - HighlightThickness, PanelMin.y};
v2 RightEdgeMax = PanelMax;
PushRenderQuad2D(RenderBuffer, RightEdgeMin, RightEdgeMax, HighlightColor);
Mouse->CursorType = CursorType_HorizontalArrows;
Mouse->CursorType = CursorType_HArrows;
}
else if (MouseTopEdgeDistance < PANEL_EDGE_CLICK_MAX_DISTANCE)
{
v2 TopEdgeMin = v2{PanelMin.x, PanelMax.y - HighlightThickness};
v2 TopEdgeMax = PanelMax;
PushRenderQuad2D(RenderBuffer, TopEdgeMin, TopEdgeMax, HighlightColor);
Mouse->CursorType = CursorType_VerticalArrows;
Mouse->CursorType = CursorType_VArrows;
}
else if (MouseBottomEdgeDistance < PANEL_EDGE_CLICK_MAX_DISTANCE)
{
v2 BottomEdgeMin = PanelMin;
v2 BottomEdgeMax = v2{PanelMax.x, PanelMin.y + HighlightThickness};
PushRenderQuad2D(RenderBuffer, BottomEdgeMin, BottomEdgeMax, HighlightColor);
Mouse->CursorType = CursorType_VerticalArrows;
Mouse->CursorType = CursorType_VArrows;
}
}

View File

@ -5,6 +5,12 @@
//
#ifndef FOLDHAUS_PANEL_SCULPTURE_VIEW_H
// Definitions
#define PIXEL_TO_WORLD_SCALE 0.01f
//
struct sculpture_view_panel_state
{
camera Camera;
@ -24,8 +30,8 @@ OPERATION_RENDER_PROC(Update3DViewMouseRotate)
v2 TotalDeltaPos = Mouse.Pos - Mouse.DownPos;
m44 XRotation = M44RotationX(-TotalDeltaPos.y * State->PixelsToWorldScale);
m44 YRotation = M44RotationY(TotalDeltaPos.x * State->PixelsToWorldScale);
m44 XRotation = M44RotationX(-TotalDeltaPos.y * PIXEL_TO_WORLD_SCALE);
m44 YRotation = M44RotationY(TotalDeltaPos.x * PIXEL_TO_WORLD_SCALE);
m44 Combined = XRotation * YRotation;
OpState->Camera->Position = (Combined * OpState->CameraStartPos).xyz;

View File

@ -192,6 +192,8 @@ AnimationSystem_RenderToLedBuffers(animation_system* System, assembly_array Asse
}
}
}
System->LastUpdatedFrame = System->CurrentFrame;
}
#define FOLDHAUS_ANIMATION_RENDERER_CPP

View File

@ -72,7 +72,7 @@ AssemblyArray_Filter(assembly_array Array, assembly_array_filter_proc* Filter, g
///////////////////////////
internal led_system
LedSystemInitialize(gs_allocator PlatformMemory, u32 BuffersMax)
LedSystem_Create(gs_allocator PlatformMemory, u32 BuffersMax)
{
led_system Result = {};
Result.PlatformMemory = PlatformMemory;

View File

@ -95,5 +95,14 @@ AddressedDataBuffer_SetCOMPort(addressed_data_buffer* Buffer, gs_const_string Co
Buffer->ComPort = ComPort;
}
internal addressed_data_buffer_list
AddressedDataBufferList_Create(gs_thread_context TC)
{
addressed_data_buffer_list Result = {};
Result.Arena = AllocatorAllocStruct(TC.Allocator, gs_memory_arena);
*Result.Arena = CreateMemoryArena(TC.Allocator);
return Result;
}
#define FOLDHAUS_ADDRESSED_DATA_H
#endif // FOLDHAUS_ADDRESSED_DATA_H

View File

@ -45,114 +45,49 @@ INITIALIZE_APPLICATION(InitializeApplication)
State->Permanent = CreateMemoryArena(Context.ThreadContext.Allocator);
State->Transient = Context.ThreadContext.Transient;
State->Assemblies = AssemblyArray_Create(8, &State->Permanent);
State->GlobalLog = PushStruct(State->Transient, event_log);
*State->GlobalLog = {0};
State->GlobalLog = PushStruct(&State->Permanent, event_log);
State->CommandQueue = CommandQueue_Create(&State->Permanent, 32);
State->Patterns = Patterns_Create(&State->Permanent, 10);
ClearAndPushPatterns(&State->Patterns);
// TODO(Peter): put in InitializeInterface?
r32 FontSize = 14;
{
gs_file FontFile = ReadEntireFile(Context.ThreadContext.FileHandler, ConstString("data/Anonymous Pro.ttf"));
if (FileNoError(FontFile))
{
bitmap_font* Font = PushStruct(&State->Permanent, bitmap_font);
Font->BitmapWidth = 512;
Font->BitmapHeight = 512;
Font->BitmapBytesPerPixel = 4;
Font->BitmapMemory = PushArray(&State->Permanent, u8, Font->BitmapWidth * Font->BitmapHeight * Font->BitmapBytesPerPixel);
Font->BitmapStride = Font->BitmapWidth * Font->BitmapBytesPerPixel;
ZeroMemoryBlock(Font->BitmapMemory, Font->BitmapStride * Font->BitmapHeight);
platform_font_info FontInfo = Context.PlatformGetFontInfo("Anonymous Pro", FontSize, FontWeight_Normal, false, false, false);
Font->PixelHeight = FontInfo.PixelHeight;
Font->Ascent = FontInfo.Ascent;
Font->Descent = FontInfo.Descent;
Font->Leading = FontInfo.Leading;
Font->MaxCharWidth = FontInfo.MaxCharWidth;
Font->CodepointDictionarySize = (FontInfo.CodepointOnePastLast - FontInfo.CodepointStart);
Font->CodepointDictionaryCount = 0;
Font->CodepointKeys = PushArray(&State->Permanent, char, Font->CodepointDictionarySize);
Font->CodepointValues = PushArray(&State->Permanent, codepoint_bitmap, Font->CodepointDictionarySize);
for (s32 Codepoint = FontInfo.CodepointStart;
Codepoint < FontInfo.CodepointOnePastLast;
Codepoint++)
{
u32 CodepointX, CodepointY;
GetNextCodepointOffset(Font, &CodepointX, &CodepointY);
u32 CodepointW, CodepointH;
Context.PlatformDrawFontCodepoint(
Font->BitmapMemory,
Font->BitmapWidth,
Font->BitmapHeight,
CodepointX, CodepointY,
Codepoint, FontInfo,
&CodepointW, &CodepointH);
AddCodepointToFont(Font, Codepoint, 0, 0, CodepointW, CodepointH, CodepointX, CodepointY);
}
State->Interface.Style.Font = Font;
Font->BitmapTextureHandle = Context.PlatformGetGPUTextureHandle(Font->BitmapMemory,
Font->BitmapWidth, Font->BitmapHeight);
}
else
{
LogError(State->GlobalLog, "Unable to load font");
}
}
State->Interface.Style.FontSize = FontSize;
State->Interface.Style.PanelBGColors[0] = v4{.3f, .3f, .3f, 1};
State->Interface.Style.PanelBGColors[1] = v4{.4f, .4f, .4f, 1};
State->Interface.Style.PanelBGColors[2] = v4{.5f, .5f, .5f, 1};
State->Interface.Style.PanelBGColors[3] = v4{.6f, .6f, .6f, 1};
State->Interface.Style.ButtonColor_Inactive = BlackV4;
State->Interface.Style.ButtonColor_Active = v4{.1f, .1f, .1f, 1};
State->Interface.Style.ButtonColor_Selected = v4{.3f, .3f, .3f, 1};
State->Interface.Style.TextColor = WhiteV4;
State->Interface.Style.ListBGColors[0] = v4{ .16f, .16f, .16f, 1.f };
State->Interface.Style.ListBGColors[1] = v4{ .18f, .18f, .18f, 1.f };
State->Interface.Style.ListBGHover = v4{ .22f, .22f, .22f, 1.f };
State->Interface.Style.ListBGSelected = v4{.44f, .44f, .44f, 1.f };
State->Interface.Style.Margin = v2{5, 5};
State->Interface.Style.RowHeight = ui_GetTextLineHeight(State->Interface) + (2 * State->Interface.Style.Margin.y);
State->Interface.WidgetsCountMax = 4096;
State->Interface.Widgets = PushArray(&State->Permanent, ui_widget, State->Interface.WidgetsCountMax);
State->Interface.PerFrameMemory = PushStruct(&State->Permanent, gs_memory_arena);
*State->Interface.PerFrameMemory = CreateMemoryArena(Context.ThreadContext.Allocator);
State->Interface.Permanent = &State->Permanent;
interface_config IConfig = {0};
IConfig.FontSize = 14;
IConfig.PanelBGColors[0] = v4{.3f, .3f, .3f, 1};
IConfig.PanelBGColors[1] = v4{.4f, .4f, .4f, 1};
IConfig.PanelBGColors[2] = v4{.5f, .5f, .5f, 1};
IConfig.PanelBGColors[3] = v4{.6f, .6f, .6f, 1};
IConfig.ButtonColor_Inactive = BlackV4;
IConfig.ButtonColor_Active = v4{.1f, .1f, .1f, 1};
IConfig.ButtonColor_Selected = v4{.3f, .3f, .3f, 1};
IConfig.TextColor = WhiteV4;
IConfig.ListBGColors[0] = v4{ .16f, .16f, .16f, 1.f };
IConfig.ListBGColors[1] = v4{ .18f, .18f, .18f, 1.f };
IConfig.ListBGHover = v4{ .22f, .22f, .22f, 1.f };
IConfig.ListBGSelected = v4{.44f, .44f, .44f, 1.f };
IConfig.Margin = v2{5, 5};
State->Interface = ui_InterfaceCreate(Context, IConfig, &State->Permanent);
State->SACN = SACN_Initialize(Context);
State->LedSystem = LedSystemInitialize(Context.ThreadContext.Allocator, 128);
State->LedSystem = LedSystem_Create(Context.ThreadContext.Allocator, 128);
GlobalDebugServices->Interface.RenderSculpture = true;
PanelSystem_Init(&State->PanelSystem, GlobalPanelDefs, GlobalPanelDefsCount, &State->Permanent);
PanelSystem_PushPanel(&State->PanelSystem, PanelType_SculptureView, State, Context);
State->Modes = OperationModeSystemInit(&State->Permanent, Context.ThreadContext);
ReloadStaticData(Context, GlobalDebugServices);
#if 1
gs_const_string SculpturePath = ConstString("data/test_blumen.fold");
LoadAssembly(&State->Assemblies, &State->LedSystem, State->Transient, Context, SculpturePath, State->GlobalLog);
#endif
State->PixelsToWorldScale = .01f;
GlobalDebugServices->Interface.RenderSculpture = true;
ReloadStaticData(Context, GlobalDebugServices);
State->Modes = OperationModeSystemInit(&State->Permanent, Context.ThreadContext);
{ // Animation PLAYGROUND
State->AnimationSystem = {};
State->AnimationSystem.Storage = &State->Permanent;
@ -176,9 +111,6 @@ INITIALIZE_APPLICATION(InitializeApplication)
State->AnimationSystem.TimelineShouldAdvance = true;
} // End Animation Playground
PanelSystem_Init(&State->PanelSystem, GlobalPanelDefs, GlobalPanelDefsCount, &State->Permanent);
PanelSystem_PushPanel(&State->PanelSystem, PanelType_SculptureView, State, Context);
}
UPDATE_AND_RENDER(UpdateAndRender)
@ -197,7 +129,6 @@ UPDATE_AND_RENDER(UpdateAndRender)
AnimationSystem_Update(&State->AnimationSystem);
if (AnimationSystem_NeedsRender(State->AnimationSystem))
{
State->AnimationSystem.LastUpdatedFrame = State->AnimationSystem.CurrentFrame;
AnimationSystem_RenderToLedBuffers(&State->AnimationSystem,
State->Assemblies,
&State->LedSystem,
@ -205,14 +136,13 @@ UPDATE_AND_RENDER(UpdateAndRender)
State->Transient);
}
{
// NOTE(pjs): Building data buffers to be sent out to the sculpture
// This array is used on the platform side to actually send the information
assembly_array SACNAssemblies = AssemblyArray_Filter(State->Assemblies, AssemblyFilter_OutputsViaSACN, State->Transient);
assembly_array UARTAssemblies = AssemblyArray_Filter(State->Assemblies, AssemblyFilter_OutputsViaUART, State->Transient);
SACN_BuildOutputData(&State->SACN, OutputData, SACNAssemblies, &State->LedSystem);
UART_BuildOutputData(OutputData, UARTAssemblies, &State->LedSystem);
}
// NOTE(pjs): Building data buffers to be sent out to the sculpture
// This array is used on the platform side to actually send the information
assembly_array SACNAssemblies = AssemblyArray_Filter(State->Assemblies, AssemblyFilter_OutputsViaSACN, State->Transient);
assembly_array UARTAssemblies = AssemblyArray_Filter(State->Assemblies, AssemblyFilter_OutputsViaUART, State->Transient);
SACN_BuildOutputData(&State->SACN, OutputData, SACNAssemblies, &State->LedSystem);
UART_BuildOutputData(OutputData, UARTAssemblies, &State->LedSystem);
Editor_Render(State, Context, RenderBuffer);
}

View File

@ -65,8 +65,6 @@ struct app_state
ui_interface Interface;
panel_system PanelSystem;
panel* HotPanel;
r32 PixelsToWorldScale;
};
internal void OpenColorPicker(app_state* State, v4* Address);

View File

@ -15,6 +15,14 @@ AllocateRenderCommandBuffer (u8* Memory, s32 Size, renderer_realloc* Realloc)
Result.Realloc = Realloc;
return Result;
}
internal render_command_buffer
AllocateRenderCommandBuffer(u32 MemorySize,
gs_memory_arena* Arena,
renderer_realloc* Realloc)
{
u8* Memory = PushSize(Arena, MemorySize);
return AllocateRenderCommandBuffer(Memory, MemorySize, Realloc);
}
internal void
Render3DQuadBatch (u8* CommandData, s32 TriCount)

View File

@ -1528,5 +1528,26 @@ ui_EndLabeledDropdown(ui_interface* Interface)
ui_EndRow(Interface);
}
internal ui_interface
ui_InterfaceCreate(context Context, interface_config Style, gs_memory_arena* Permanent)
{
ui_interface Result = {0};
Result.Style = Style;
gs_file FontFile = ReadEntireFile(Context.ThreadContext.FileHandler, ConstString("data/Anonymous Pro.ttf"));
Result.Style.Font = PushStruct(Permanent, bitmap_font);
*Result.Style.Font = TextFont_Create(FontFile, 512, Style.FontSize, Context, Permanent);
Result.Style.RowHeight = ui_GetTextLineHeight(Result) + (2 * Result.Style.Margin.y);
Result.WidgetsCountMax = 4096;
Result.Widgets = PushArray(Permanent, ui_widget, Result.WidgetsCountMax);
Result.PerFrameMemory = PushStruct(Permanent, gs_memory_arena);
*Result.PerFrameMemory = CreateMemoryArena(Context.ThreadContext.Allocator);
Result.Permanent = Permanent;
return Result;
}
#define INTERFACE_H
#endif // INTERFACE_H

View File

@ -23,6 +23,7 @@
#include "win32_foldhaus_work_queue.h"
#include "win32_foldhaus_serial.h"
#include "win32_foldhaus_socket.h"
#include "win32_foldhaus_mouse.h"
#include "../foldhaus_renderer.cpp"
@ -369,23 +370,6 @@ Win32Realloc(u8* Buf, s32 OldSize, s32 NewSize)
return NewMemory;
}
// NOTE(Peter): Only meant to take one of the values specified below:
// IDC_APPSTARTING, IDC_ARROW, IDC_CROSS, IDC_HAND, IDC_HELP, IDC_IBEAM,
// IDC_ICON, IDC_NO, IDC_SIZE, IDC_SIZEALL, IDC_SIZENESW, IDC_SIZENS, IDC_SIZENWSE,
// IDC_SIZEWE, IDC_UPARROW, IDC_WAIT
internal HCURSOR
Win32LoadSystemCursor(char* CursorIdentifier)
{
u32 Error = 0;
HCURSOR Result = LoadCursorA(NULL, CursorIdentifier);
if (Result == NULL)
{
Error = GetLastError();
InvalidCodePath;
}
return Result;
}
internal void
Win32_SendAddressedDataBuffers(gs_thread_context Context, addressed_data_buffer_list OutputData)
{
@ -467,6 +451,23 @@ UpackB4(const u8* ptr)
return (u32)(ptr[3] | (ptr[2] << 8) | (ptr[1] << 16) | (ptr[0] << 24));
}
internal bool
ReloadAndLinkDLL(win32_dll_refresh* DLL, context* Context, gs_work_queue* WorkQueue, bool ShouldError)
{
bool Success = false;
if (HotLoadDLL(DLL))
{
SetApplicationLinks(Context, *DLL, WorkQueue);
Context->ReloadStaticData(*Context, GlobalDebugServices);
Success = true;
}
else if(ShouldError)
{
OutputDebugStringA("Unable to load application DLL at startup.\nAborting\n");
}
return Success;
}
int WINAPI
WinMain (
HINSTANCE HInstance,
@ -477,36 +478,6 @@ WinMain (
{
gs_thread_context ThreadContext = Win32CreateThreadContext();
#if 0
// NOTE(pjs): UART TEST CODE
// NOTE(pjs): UART TEST CODE
// NOTE(pjs): UART TEST CODE
u32 LedCount = 48;
u32 MessageBaseSize = sizeof(uart_header) + sizeof(uart_channel) + sizeof(uart_footer);
MessageBaseSize += sizeof(u8) * 3 * LedCount;
gs_data MessageBuffer = PushSizeToData(ThreadContext.Transient);
gs_memory_cursor WriteCursor = CreateMemoryCursor(MessageBuffer);
uart_header* Header = PushStructOnCursor(WriteCursor, uart_header);
UART_FillHeader(Header, Strip.UARTAddr.Channel, UART_SET_CHANNEL_WS2812);
uart_channel* Channel = PushStructOnCursor(WriteCursor, uart_channel);
*Channel = ChannelSettings;
for (u32 i = 0; i < LedCount; i++)
{
u8* OutputPixel = PushArrayOnCursor(WriteCursor, u8, 3);
OutputPixel[Channel->RedIndex] = (u8)(i);
OutputPixel[Channel->GreenIndex] = 0;
OutputPixel[Channel->BlueIndex] = 0;
}
uart_footer* Footer = PushStructOnCursor(WriteCursor, uart_footer);
UART_FillFooter(Footer, (u8*)Header);
#endif
MainWindow = Win32CreateWindow (HInstance, "Foldhaus", 1440, 768, HandleWindowEvents);
Win32UpdateWindowDimension(&MainWindow);
@ -516,14 +487,10 @@ WinMain (
OpenGLWindowInfo.DepthBits = 0;
CreateOpenGLWindowContext(OpenGLWindowInfo, &MainWindow);
s32 InitialMemorySize = MB(64);
u8* InitialMemory = (u8*)Win32Alloc(InitialMemorySize, 0);
context Context = {};
Context.ThreadContext = ThreadContext;
Context.MemorySize = InitialMemorySize;
Context.MemoryBase = InitialMemory;
Context.WindowBounds = rect2{v2{0, 0}, v2{(r32)MainWindow.Width, (r32)MainWindow.Height}};
Context.Mouse = {0};
Context.MemorySize = MB(64);
Context.MemoryBase = (u8*)Win32Alloc(Context.MemorySize, 0);
gs_memory_arena PlatformPermanent = CreateMemoryArena(Context.ThreadContext.Allocator);
@ -533,109 +500,37 @@ WinMain (
r32 LastFrameSecondsElapsed = 0.0f;
GlobalDebugServices = PushStruct(&PlatformPermanent, debug_services);
s32 DebugThreadCount = PLATFORM_THREAD_COUNT + 1;
InitDebugServices(GlobalDebugServices,
PerformanceCountFrequency,
DEBUGAlloc,
Win32Realloc,
GetWallClock,
Win32GetThreadId,
DebugThreadCount);
PLATFORM_THREAD_COUNT + 1);
input_queue InputQueue;
{
s32 InputQueueMemorySize = sizeof(input_entry) * 32;
u8* InputQueueMemory = (u8*)Win32Alloc(InputQueueMemorySize, 0);
InputQueue = InitializeInputQueue(InputQueueMemory, InputQueueMemorySize);
}
input_queue InputQueue = InputQueue_Create(&PlatformPermanent, 32);
//
// Set up worker threads
const s32 WorkerThreadCount = PLATFORM_THREAD_COUNT;
worker_thread_info* WorkerThreads = 0;
if (PLATFORM_THREAD_COUNT > 0)
{
WorkerThreads = PushArray(&PlatformPermanent, worker_thread_info, PLATFORM_THREAD_COUNT);
}
HANDLE WorkQueueSemaphoreHandle = CreateSemaphoreEx(0, 0, PLATFORM_THREAD_COUNT, 0, 0, SEMAPHORE_ALL_ACCESS);
gs_work_queue WorkQueue = {};
WorkQueue.SemaphoreHandle = &WorkQueueSemaphoreHandle;
WorkQueue.JobsMax = 512;
WorkQueue.Jobs = PushArray(&PlatformPermanent, gs_threaded_job, WorkQueue.JobsMax);
WorkQueue.NextJobIndex = 0;
WorkQueue.PushWorkOnQueue = Win32PushWorkOnQueue;
WorkQueue.CompleteQueueWork = Win32DoQueueWorkUntilDone;
WorkQueue.ResetWorkQueue = ResetWorkQueue;
for (s32 i = 0; i < PLATFORM_THREAD_COUNT; i++)
{
// ID = 0 is reserved for this thread
WorkerThreads[i].Queue = &WorkQueue;
WorkerThreads[i].Handle = CreateThread(0, 0, &WorkerThreadProc, (void*)&WorkerThreads[i], 0, 0);
}
// Cursors
HCURSOR CursorArrow = Win32LoadSystemCursor(IDC_ARROW);
HCURSOR CursorPointer = Win32LoadSystemCursor(IDC_HAND);
HCURSOR CursorLoading = Win32LoadSystemCursor(IDC_WAIT);
HCURSOR CursorHorizontalArrows = Win32LoadSystemCursor(IDC_SIZEWE);
HCURSOR CursorVerticalArrows = Win32LoadSystemCursor(IDC_SIZENS);
HCURSOR CursorDiagonalTopLeftArrows = Win32LoadSystemCursor(IDC_SIZENWSE);
HCURSOR CursorDiagonalTopRightArrows = Win32LoadSystemCursor(IDC_SIZENESW);
Win32WorkQueue_Init(&PlatformPermanent, PLATFORM_THREAD_COUNT);
// Platform functions
Context.GeneralWorkQueue = &WorkQueue;
Context.GeneralWorkQueue = &Win32WorkQueue.WorkQueue;
Context.PlatformGetGPUTextureHandle = Win32GetGPUTextureHandle;
Context.PlatformGetSocketHandle = Win32GetSocketHandle;
Context.PlatformGetFontInfo = Win32GetFontInfo;
Context.PlatformDrawFontCodepoint = Win32DrawFontCodepoint;
win32_dll_refresh DLLRefresh = InitializeDLLHotReloading(DLLName, WorkingDLLName, DLLLockFileName);
if (HotLoadDLL(&DLLRefresh))
{
SetApplicationLinks(&Context, DLLRefresh, &WorkQueue);
Context.ReloadStaticData(Context, GlobalDebugServices);
}
else
{
MessageBox(MainWindow.Handle, "Unable to load application DLL at startup.\nAborting\n", "Set Up Error", MB_OK);
return -1;
}
if (!ReloadAndLinkDLL(&DLLRefresh, &Context, &Win32WorkQueue.WorkQueue, true)) { return -1; }
WSADATA WSAData;
WSAStartup(MAKEWORD(2, 2), &WSAData);
Win32Sockets = Win32SocketArray_Create(16, &PlatformPermanent);
Mouse_Init();
#if 0
// NOTE(pjs): SOCKET READING TEST CODE
// NOTE(pjs): SOCKET READING TEST CODE
// NOTE(pjs): SOCKET READING TEST CODE
win32_socket TestSocket = Win32Socket_ConnectToAddress("127.0.0.1", "20185");
test_microphone_packet* Recv = 0;
while (true)
{
gs_data Data = Win32Socket_Receive(&TestSocket, ThreadContext.Transient);
if (Data.Size > 0)
{
OutputDebugStringA("Received\n");
Recv = (test_microphone_packet*)Data.Memory;
}
ClearArena(ThreadContext.Transient);
}
#endif
Win32SocketSystem_Init(&PlatformPermanent);
Win32SerialArray_Create(ThreadContext);
s32 RenderMemorySize = MB(12);
u8* RenderMemory = PushSize(&PlatformPermanent, RenderMemorySize);
render_command_buffer RenderBuffer = AllocateRenderCommandBuffer(RenderMemory, RenderMemorySize, Win32Realloc);
render_command_buffer RenderBuffer = AllocateRenderCommandBuffer(MB(12), &PlatformPermanent, Win32Realloc);
addressed_data_buffer_list OutputData = {};
OutputData.Arena = AllocatorAllocStruct(Context.ThreadContext.Allocator, gs_memory_arena);
*OutputData.Arena = CreateMemoryArena(Context.ThreadContext.Allocator);
addressed_data_buffer_list OutputData = AddressedDataBufferList_Create(ThreadContext);
Context.InitializeApplication(Context);
@ -650,33 +545,12 @@ WinMain (
DEBUG_TRACK_SCOPE(MainLoop);
ResetInputQueue(&InputQueue);
if (HotLoadDLL(&DLLRefresh))
{
SetApplicationLinks(&Context, DLLRefresh, &WorkQueue);
Context.ReloadStaticData(Context, GlobalDebugServices);
}
ReloadAndLinkDLL(&DLLRefresh, &Context, &Win32WorkQueue.WorkQueue, false);
AddressedDataBufferList_Clear(&OutputData);
{ // Mouse
POINT MousePos;
GetCursorPos (&MousePos);
ScreenToClient(MainWindow.Handle, &MousePos);
Context.Mouse.Scroll = 0;
Context.Mouse.OldPos = Context.Mouse.Pos;
Context.Mouse.Pos = v2{(r32)MousePos.x, (r32)MainWindow.Height - MousePos.y};
Context.Mouse.DeltaPos = Context.Mouse.Pos - Context.Mouse.OldPos;
if (KeyIsDown(Context.Mouse.LeftButtonState))
{
SetKeyWasDown(Context.Mouse.LeftButtonState);
}
else
{
SetKeyWasUp(Context.Mouse.LeftButtonState);
}
}
Mouse_Update(MainWindow, &Context);
MSG Message;
while (PeekMessageA(&Message, MainWindow.Handle, 0, 0, PM_REMOVE))
@ -700,45 +574,10 @@ WinMain (
gs_data ProcArg = {};
ProcArg.Memory = (u8*)&OutputData;
ProcArg.Size = sizeof(OutputData);
Win32PushWorkOnQueue(&WorkQueue, Win32_SendAddressedDataBuffers_Job, ProcArg, ConstString("Send UART Data"));
Win32PushWorkOnQueue(&Win32WorkQueue.WorkQueue, Win32_SendAddressedDataBuffers_Job, ProcArg, ConstString("Send UART Data"));
}
Context.Mouse.LeftButtonState = GetMouseButtonStateAdvanced(Context.Mouse.LeftButtonState);
Context.Mouse.MiddleButtonState = GetMouseButtonStateAdvanced(Context.Mouse.MiddleButtonState);
Context.Mouse.RightButtonState = GetMouseButtonStateAdvanced(Context.Mouse.RightButtonState);
switch (Context.Mouse.CursorType)
{
case CursorType_Arrow:
{
SetCursor(CursorArrow);
}break;
case CursorType_Pointer:
{
SetCursor(CursorPointer);
}break;
case CursorType_Loading:
{
SetCursor(CursorLoading);
}break;
case CursorType_HorizontalArrows:
{
SetCursor(CursorHorizontalArrows);
}break;
case CursorType_VerticalArrows:
{
SetCursor(CursorVerticalArrows);
}break;
case CursorType_DiagonalTopLeftArrows:
{
SetCursor(CursorDiagonalTopLeftArrows);
}break;
case CursorType_DiagonalTopRightArrows:
{
SetCursor(CursorDiagonalTopRightArrows);
}break;
InvalidDefaultCase;
}
Mouse_Advance(&Context);
HDC DeviceContext = GetDC(MainWindow.Handle);
SwapBuffers(DeviceContext);
@ -759,28 +598,13 @@ WinMain (
LastFrameSecondsElapsed = SecondsElapsed;
LastFrameEnd = GetWallClock();
//OutputDebugStringA("-- Frame END -- \n");
}
Context.CleanupApplication(Context);
for (s32 SocketIdx = 0; SocketIdx < Win32Sockets.Count; SocketIdx++)
{
Win32Socket_Close(Win32Sockets.Values + SocketIdx);
}
Win32SocketSystem_Cleanup();
s32 CleanupResult = 0;
do {
CleanupResult = WSACleanup();
}while(CleanupResult == SOCKET_ERROR);
for (s32 Thread = 0; Thread < PLATFORM_THREAD_COUNT; Thread++)
{
TerminateThread(WorkerThreads[Thread].Handle, 0);
}
Win32WorkQueue_Cleanup();
return 0;
}

View File

@ -0,0 +1,118 @@
//
// File: win32_mouse.h
// Author: Peter Slattery
// Creation Date: 2021-01-10
//
#ifndef WIN32_MOUSE_H
HCURSOR CursorArrow;
HCURSOR CursorPointer;
HCURSOR CursorLoading;
HCURSOR CursorHArrows;
HCURSOR CursorVArrows;
HCURSOR CursorDTopLeftArrows;
HCURSOR CursorDTopRightArrows;
HCURSOR CurrentCursor;
// NOTE(Peter): Only meant to take one of the values specified below:
// IDC_APPSTARTING, IDC_ARROW, IDC_CROSS, IDC_HAND, IDC_HELP, IDC_IBEAM,
// IDC_ICON, IDC_NO, IDC_SIZE, IDC_SIZEALL, IDC_SIZENESW, IDC_SIZENS, IDC_SIZENWSE,
// IDC_SIZEWE, IDC_UPARROW, IDC_WAIT
internal HCURSOR
Win32LoadSystemCursor(char* CursorIdentifier)
{
u32 Error = 0;
HCURSOR Result = LoadCursorA(NULL, CursorIdentifier);
if (Result == NULL)
{
Error = GetLastError();
InvalidCodePath;
}
return Result;
}
internal void
Mouse_Init()
{
CursorArrow = Win32LoadSystemCursor(IDC_ARROW);
CursorPointer = Win32LoadSystemCursor(IDC_HAND);
CursorLoading = Win32LoadSystemCursor(IDC_WAIT);
CursorHArrows = Win32LoadSystemCursor(IDC_SIZEWE);
CursorVArrows = Win32LoadSystemCursor(IDC_SIZENS);
CursorDTopLeftArrows = Win32LoadSystemCursor(IDC_SIZENWSE);
CursorDTopRightArrows = Win32LoadSystemCursor(IDC_SIZENESW);
}
internal void
Mouse_Update(window Window, context* Context)
{
POINT Pos;
GetCursorPos (&Pos);
ScreenToClient(Window.Handle, &Pos);
Context->Mouse.Scroll = 0;
Context->Mouse.OldPos = Context->Mouse.Pos;
Context->Mouse.Pos = v2{(r32)Pos.x, (r32)Window.Height - Pos.y};
Context->Mouse.DeltaPos = Context->Mouse.Pos - Context->Mouse.OldPos;
if (KeyIsDown(Context->Mouse.LeftButtonState))
{
SetKeyWasDown(Context->Mouse.LeftButtonState);
}
else
{
SetKeyWasUp(Context->Mouse.LeftButtonState);
}
if (KeyIsDown(Context->Mouse.MiddleButtonState))
{
SetKeyWasDown(Context->Mouse.MiddleButtonState);
}
else
{
SetKeyWasUp(Context->Mouse.MiddleButtonState);
}
if (KeyIsDown(Context->Mouse.RightButtonState))
{
SetKeyWasDown(Context->Mouse.RightButtonState);
}
else
{
SetKeyWasUp(Context->Mouse.RightButtonState);
}
}
internal void
Mouse_Advance(context* Context)
{
Context->Mouse.LeftButtonState = GetMouseButtonStateAdvanced(Context->Mouse.LeftButtonState);
Context->Mouse.MiddleButtonState = GetMouseButtonStateAdvanced(Context->Mouse.MiddleButtonState);
Context->Mouse.RightButtonState = GetMouseButtonStateAdvanced(Context->Mouse.RightButtonState);
HCURSOR NewCursor = 0;
switch (Context->Mouse.CursorType)
{
case CursorType_Arrow: { NewCursor = CursorArrow; } break;
case CursorType_Pointer: { NewCursor = CursorPointer; }break;
case CursorType_Loading: { NewCursor = CursorLoading; }break;
case CursorType_HArrows: { NewCursor = CursorHArrows; }break;
case CursorType_VArrows: { NewCursor = CursorVArrows; }break;
case CursorType_DTopLeftArrows: { NewCursor = CursorDTopLeftArrows; }break;
case CursorType_DTopRightArrows: { NewCursor = CursorDTopRightArrows; }break;
InvalidDefaultCase;
}
if (NewCursor != CurrentCursor)
{
CurrentCursor = NewCursor;
SetCursor(NewCursor);
}
}
#define WIN32_MOUSE_H
#endif // WIN32_MOUSE_H

View File

@ -17,6 +17,9 @@ struct win32_socket_array
s32 Count;
};
global WSADATA WSAData;
global win32_socket_array Win32Sockets;
//////////////////////
//
// Win32 Socket Array
@ -50,9 +53,7 @@ Win32SocketArray_Get(win32_socket_array Array, s32 Index)
//////////////////////
//
// Win32 Socket System
global win32_socket_array Win32Sockets;
// Win32 Sockets
internal win32_socket
Win32Socket_Create(s32 AddressFamily, s32 Type, s32 Protocol)
@ -241,6 +242,38 @@ Win32Socket_Close(win32_socket* Socket)
Socket->Socket = INVALID_SOCKET;
}
internal void
Win32Socket_CloseArray(win32_socket_array Array)
{
for (s32 i = 0; i < Array.Count; i++)
{
win32_socket* Socket = Array.Values + i;
Win32Socket_Close(Socket);
}
}
//////////////////////
//
// Win32 Socket System
internal void
Win32SocketSystem_Init(gs_memory_arena* Arena)
{
WSAStartup(MAKEWORD(2, 2), &WSAData);
Win32Sockets = Win32SocketArray_Create(16, Arena);
}
internal void
Win32SocketSystem_Cleanup()
{
Win32Socket_CloseArray(Win32Sockets);
s32 CleanupResult = 0;
do {
CleanupResult = WSACleanup();
}while(CleanupResult == SOCKET_ERROR);
}
PLATFORM_GET_SOCKET_HANDLE(Win32GetSocketHandle)
{
s32 Result = Win32SocketArray_Take(&Win32Sockets);

View File

@ -18,6 +18,16 @@ struct worker_thread_info
gs_work_queue* Queue;
};
struct win32_work_queue
{
u32 ThreadCount;
worker_thread_info* Threads;
gs_work_queue WorkQueue;
};
worker_thread_info* WorkerThreads;
win32_work_queue Win32WorkQueue;
internal s32
Win32GetThreadId()
{
@ -153,5 +163,42 @@ WorkerThreadProc (LPVOID InputThreadInfo)
return 0;
}
internal void
Win32WorkQueue_Init(gs_memory_arena* Arena, u32 ThreadCount)
{
if (ThreadCount > 0)
{
Win32WorkQueue.ThreadCount = ThreadCount;
Win32WorkQueue.Threads = PushArray(Arena, worker_thread_info, ThreadCount);
}
gs_work_queue WQ = {};
WQ.SemaphoreHandle = CreateSemaphoreEx(0, 0, ThreadCount, 0, 0, SEMAPHORE_ALL_ACCESS);;
WQ.JobsMax = 512;
WQ.Jobs = PushArray(Arena, gs_threaded_job, WQ.JobsMax);
WQ.NextJobIndex = 0;
WQ.PushWorkOnQueue = Win32PushWorkOnQueue;
WQ.CompleteQueueWork = Win32DoQueueWorkUntilDone;
Win32WorkQueue.WorkQueue = WQ;
// ID = 0 is reserved for this thread
for (u32 i = 0; i < ThreadCount; i++)
{
worker_thread_info* T = Win32WorkQueue.Threads + i;
T->Queue = &Win32WorkQueue.WorkQueue;
T->Handle = CreateThread(0, 0, &WorkerThreadProc, (void*)T, 0, 0);
}
}
internal void
Win32WorkQueue_Cleanup()
{
for (u32 Thread = 0; Thread < Win32WorkQueue.ThreadCount; Thread++)
{
TerminateThread(Win32WorkQueue.Threads[Thread].Handle, 0);
}
}
#define WIN32_FOLDHAUS_WORK_QUEUE_H
#endif // WIN32_FOLDHAUS_WORK_QUEUE_H

View File

@ -0,0 +1,53 @@
//
// File: win32_test_code.cpp
// Author: Peter Slattery
// Creation Date: 2021-01-10
//
#ifndef WIN32_TEST_CODE_CPP
internal void
Win32_TestCode_UART(gs_thread_context ThreadContext)
{
u32 LedCount = 48;
u32 MessageBaseSize = sizeof(uart_header) + sizeof(uart_channel) + sizeof(uart_footer);
MessageBaseSize += sizeof(u8) * 3 * LedCount;
gs_data MessageBuffer = PushSizeToData(ThreadContext.Transient);
gs_memory_cursor WriteCursor = CreateMemoryCursor(MessageBuffer);
uart_header* Header = PushStructOnCursor(WriteCursor, uart_header);
UART_FillHeader(Header, Strip.UARTAddr.Channel, UART_SET_CHANNEL_WS2812);
uart_channel* Channel = PushStructOnCursor(WriteCursor, uart_channel);
*Channel = ChannelSettings;
for (u32 i = 0; i < LedCount; i++)
{
u8* OutputPixel = PushArrayOnCursor(WriteCursor, u8, 3);
OutputPixel[Channel->RedIndex] = (u8)(i);
OutputPixel[Channel->GreenIndex] = 0;
OutputPixel[Channel->BlueIndex] = 0;
}
uart_footer* Footer = PushStructOnCursor(WriteCursor, uart_footer);
UART_FillFooter(Footer, (u8*)Header);
}
internal void
Win32_TestCode_SocketReading(gs_thread_context ThreadContext)
{
win32_socket TestSocket = Win32Socket_ConnectToAddress("127.0.0.1", "20185");
test_microphone_packet* Recv = 0;
while (true)
{
gs_data Data = Win32Socket_Receive(&TestSocket, ThreadContext.Transient);
if (Data.Size > 0)
{
OutputDebugStringA("Received\n");
Recv = (test_microphone_packet*)Data.Memory;
}
ClearArena(ThreadContext.Transient);
}
}
#define WIN32_TEST_CODE_CPP
#endif // WIN32_TEST_CODE_CPP

View File

@ -5,8 +5,6 @@
//
#ifndef GS_FONT_H
#ifndef GS_FONT_H
struct codepoint_bitmap
{
s32 XOffset, YOffset;
@ -37,20 +35,6 @@ struct bitmap_font
s32 BitmapTextureHandle;
};
internal bitmap_font
InitializeTextFont (s32 CodepointCount, u8* CodepointMemory, s32 CodepointMemorySize)
{
bitmap_font Result = {};
Result.CodepointDictionarySize = CodepointCount;
Result.CodepointDictionaryCount = 0;
Assert(CodepointMemorySize >= (sizeof(char) + sizeof(codepoint_bitmap)) * CodepointCount);
Result.CodepointKeys = (char*)CodepointMemory;
Result.CodepointValues = (codepoint_bitmap*)(CodepointMemory + (sizeof(char) * CodepointCount));
return Result;
}
#define GLYPH_SKIRT 1
internal void
GetNextCodepointOffset (bitmap_font* Font, u32* X, u32* Y)
@ -126,8 +110,58 @@ NewLineYOffset (bitmap_font Font)
return Result;
}
#define GS_FONT_H
#endif
internal bitmap_font
TextFont_Create(gs_file FontFile, u32 BitmapDim, u32 FontSize, context Context, gs_memory_arena* Arena)
{
bitmap_font Result = {};
Assert(FileNoError(FontFile));
Result.BitmapWidth = BitmapDim;
Result.BitmapHeight = BitmapDim;
Result.BitmapBytesPerPixel = 4;
Result.BitmapStride = Result.BitmapWidth * Result.BitmapBytesPerPixel;
u32 BitmapSize = Result.BitmapWidth * Result.BitmapHeight * Result.BitmapBytesPerPixel;
Result.BitmapMemory = PushArray(Arena, u8, BitmapSize);
ZeroMemoryBlock(Result.BitmapMemory, BitmapSize);
platform_font_info FontInfo = Context.PlatformGetFontInfo("Anonymous Pro", FontSize, FontWeight_Normal, false, false, false);
Result.PixelHeight = FontInfo.PixelHeight;
Result.Ascent = FontInfo.Ascent;
Result.Descent = FontInfo.Descent;
Result.Leading = FontInfo.Leading;
Result.MaxCharWidth = FontInfo.MaxCharWidth;
Result.CodepointDictionarySize = (FontInfo.CodepointOnePastLast - FontInfo.CodepointStart);
Result.CodepointDictionaryCount = 0;
Result.CodepointKeys = PushArray(Arena, char, Result.CodepointDictionarySize);
Result.CodepointValues = PushArray(Arena, codepoint_bitmap, Result.CodepointDictionarySize);
for (s32 Codepoint = FontInfo.CodepointStart;
Codepoint < FontInfo.CodepointOnePastLast;
Codepoint++)
{
u32 CodepointX, CodepointY;
GetNextCodepointOffset(&Result, &CodepointX, &CodepointY);
u32 CodepointW, CodepointH;
Context.PlatformDrawFontCodepoint(
Result.BitmapMemory,
Result.BitmapWidth,
Result.BitmapHeight,
CodepointX, CodepointY,
Codepoint, FontInfo,
&CodepointW, &CodepointH);
AddCodepointToFont(&Result, Codepoint, 0, 0, CodepointW, CodepointH, CodepointX, CodepointY);
}
Result.BitmapTextureHandle = Context.PlatformGetGPUTextureHandle(Result.BitmapMemory,
Result.BitmapWidth, Result.BitmapHeight);
return Result;
}
#define GS_FONT_H
#endif // GS_FONT_H

View File

@ -251,10 +251,10 @@ enum cursor_type
CursorType_Arrow,
CursorType_Pointer,
CursorType_Loading,
CursorType_HorizontalArrows,
CursorType_VerticalArrows,
CursorType_DiagonalTopLeftArrows,
CursorType_DiagonalTopRightArrows,
CursorType_HArrows,
CursorType_VArrows,
CursorType_DTopLeftArrows,
CursorType_DTopRightArrows,
CursorType_Count,
};
@ -277,7 +277,7 @@ struct mouse_state
};
internal input_queue
InitializeInputQueue (u8* Memory, s32 MemorySize)
InputQueue_Create (u8* Memory, s32 MemorySize)
{
input_queue Result = {};
s32 EntriesCount = MemorySize / sizeof(input_entry);
@ -286,6 +286,18 @@ InitializeInputQueue (u8* Memory, s32 MemorySize)
Result.Entries = (input_entry*)Memory;
return Result;
}
internal input_queue
InputQueue_Create(gs_memory_arena* Arena, u32 CountMax)
{
input_queue Result = {0};
if (CountMax > 0)
{
s32 Size = sizeof(input_entry) * 32;
u8* Memory = PushSize(Arena, Size);
Result = InputQueue_Create(Memory, Size);
}
return Result;
}
internal void
ResetInputQueue (input_queue* Queue)

View File

@ -1047,7 +1047,6 @@ struct gs_work_queue
// Work Queue
push_work_on_queue* PushWorkOnQueue;
complete_queue_work* CompleteQueueWork;
reset_work_queue* ResetWorkQueue;
};
#define GS_TYPES_H