Created platform_file_handler and integrated it

This commit is contained in:
Peter Slattery 2020-05-30 14:54:37 -07:00
parent 3418f1417a
commit 0b3d603e04
5 changed files with 88 additions and 36 deletions

View File

@ -92,7 +92,7 @@ INITIALIZE_APPLICATION(InitializeApplication)
// TODO(Peter): put in InitializeInterface? // TODO(Peter): put in InitializeInterface?
r32 FontSize = 14; r32 FontSize = 14;
{ {
platform_memory_result FontFile = Context.PlatformReadEntireFile("Anonymous Pro.ttf"); platform_memory_result FontFile = ReadEntireFile(Context, "data/Anonymous Pro.ttf");
if (!FontFile.Error) if (!FontFile.Error)
{ {
bitmap_font* Font = PushStruct(&State->Permanent, bitmap_font); bitmap_font* Font = PushStruct(&State->Permanent, bitmap_font);
@ -176,7 +176,7 @@ INITIALIZE_APPLICATION(InitializeApplication)
State->Camera.LookAt = v3{0, 0, 0}; State->Camera.LookAt = v3{0, 0, 0};
#if 1 #if 1
char Path[] = "blumen_lumen.fold"; char Path[] = "data/blumen_lumen.fold";
LoadAssembly(State, Context, Path); LoadAssembly(State, Context, Path);
#endif #endif

View File

@ -70,7 +70,7 @@ s32 TempAssemblyOffsetsCount = 3;
internal void internal void
LoadAssembly (app_state* State, context Context, char* Path) LoadAssembly (app_state* State, context Context, char* Path)
{ {
platform_memory_result AssemblyFile = Context.PlatformReadEntireFile(Path); platform_memory_result AssemblyFile = ReadEntireFile(Context, Path);
if (AssemblyFile.Error == PlatformMemory_NoError) if (AssemblyFile.Error == PlatformMemory_NoError)
{ {
assembly_definition AssemblyDefinition = ParseAssemblyFile(AssemblyFile.Base, AssemblyFile.Size, &State->Transient, State->GlobalLog); assembly_definition AssemblyDefinition = ParseAssemblyFile(AssemblyFile.Base, AssemblyFile.Size, &State->Transient, State->GlobalLog);

View File

@ -63,6 +63,8 @@ enum platform_memory_error
PlatformMemory_UnknownError, // You should implement handling this when you see it PlatformMemory_UnknownError, // You should implement handling this when you see it
}; };
// TODO(Peter): Change this to just be data
// - Base, and Size
struct platform_memory_result struct platform_memory_result
{ {
u8* Base; u8* Base;
@ -104,6 +106,13 @@ typedef PLATFORM_WRITE_ENTIRE_FILE(platform_write_entire_file);
#define PLATFORM_GET_FILE_PATH(name) b32 name(char* PathBuffer, s32 BufferLength, const char* FilterStrings) #define PLATFORM_GET_FILE_PATH(name) b32 name(char* PathBuffer, s32 BufferLength, const char* FilterStrings)
typedef PLATFORM_GET_FILE_PATH(platform_get_file_path); typedef PLATFORM_GET_FILE_PATH(platform_get_file_path);
struct platform_file_handler
{
platform_read_entire_file* ReadEntireFile;
platform_write_entire_file* WriteEntireFile;
platform_get_file_path* GetFilePath;
};
#define PLATFORM_GET_GPU_TEXTURE_HANDLE(name) s32 name(u8* Memory, s32 Width, s32 Height) #define PLATFORM_GET_GPU_TEXTURE_HANDLE(name) s32 name(u8* Memory, s32 Width, s32 Height)
typedef PLATFORM_GET_GPU_TEXTURE_HANDLE(platform_get_gpu_texture_handle); typedef PLATFORM_GET_GPU_TEXTURE_HANDLE(platform_get_gpu_texture_handle);
@ -117,13 +126,13 @@ struct platform_network_address
typedef s32 platform_socket_handle; typedef s32 platform_socket_handle;
typedef s32 platform_network_address_handle; typedef s32 platform_network_address_handle;
#define PLATFORM_GET_SOCKET_HANDLE(name) platform_socket_handle name(s32 Multicast_TimeToLive) #define PLATFORM_GET_SOCKET_HANDLE(name) platform_socket_handle name(s32 Multicast_TimeToLive)
typedef PLATFORM_GET_SOCKET_HANDLE(platform_get_socket_handle); typedef PLATFORM_GET_SOCKET_HANDLE(platform_get_socket_handle);
#define PLATFORM_GET_SEND_ADDRESS_HANDLE(name) platform_network_address_handle name(s32 AddressFamily, u16 Port, u32 Address) #define PLATFORM_GET_SEND_ADDRESS_HANDLE(name) platform_network_address_handle name(s32 AddressFamily, u16 Port, u32 Address)
typedef PLATFORM_GET_SEND_ADDRESS_HANDLE(platform_get_send_address); typedef PLATFORM_GET_SEND_ADDRESS_HANDLE(platform_get_send_address);
#define PLATFORM_SET_SOCKET_OPTION(name) s32 name(platform_socket_handle SocketHandle, s32 Level, s32 Option, const char* OptionValue, s32 OptionLength) #define PLATFORM_SET_SOCKET_OPTION(name) s32 name(platform_socket_handle SocketHandle, s32 Level, s32 Option, const char* OptionValue, s32 OptionLength)
typedef PLATFORM_SET_SOCKET_OPTION(platform_set_socket_option); typedef PLATFORM_SET_SOCKET_OPTION(platform_set_socket_option);
#define PLATFORM_SEND_TO(name) s32 name(platform_socket_handle SocketHandle, u32 Address, u32 Port, const char* Buffer, s32 BufferLength, s32 Flags) #define PLATFORM_SEND_TO(name) s32 name(platform_socket_handle SocketHandle, u32 Address, u32 Port, const char* Buffer, s32 BufferLength, s32 Flags)
@ -134,7 +143,7 @@ typedef PLATFORM_CLOSE_SOCKET(platform_close_socket);
// File IO // File IO
// TODO(Peter): // TODO(Peter):
struct directory_listing struct directory_listing
{ {
string Path; string Path;
@ -258,7 +267,9 @@ struct context
platform_alloc* PlatformAlloc; platform_alloc* PlatformAlloc;
platform_free* PlatformFree; platform_free* PlatformFree;
platform_realloc* PlatformRealloc; platform_realloc* PlatformRealloc;
platform_read_entire_file* PlatformReadEntireFile;
platform_file_handler FileHandler;
platform_write_entire_file* PlatformWriteEntireFile; platform_write_entire_file* PlatformWriteEntireFile;
platform_get_file_path* PlatformGetFilePath; platform_get_file_path* PlatformGetFilePath;
platform_get_gpu_texture_handle* PlatformGetGPUTextureHandle; platform_get_gpu_texture_handle* PlatformGetGPUTextureHandle;
@ -270,6 +281,47 @@ struct context
platform_close_socket* PlatformCloseSocket; platform_close_socket* PlatformCloseSocket;
}; };
// File Handler
internal platform_memory_result
ReadEntireFile(platform_file_handler FileHandler, char* Path)
{
// TODO(Peter): Convert Path to be a string
platform_memory_result Result = FileHandler.ReadEntireFile(Path);
return Result;
}
internal platform_memory_result
ReadEntireFile(context Context, char* Path)
{
return ReadEntireFile(Context.FileHandler, Path);
}
internal b32
WriteEntireFile(platform_file_handler FileHandler, char* Path, u8* Contents, u32 Size)
{
// TODO(Peter): Convert Path to be a string
// TODO(Peter): Overload to take a data struct instead of Contents And Size
b32 Result = FileHandler.WriteEntireFile(Path, Contents, Size);
return Result;
}
internal b32
WriteEntireFile(context Context, char* Path, u8* Contents, u32 Size)
{
return WriteEntireFile(Context.FileHandler, Path, Contents, Size);
}
internal b32
GetFilePath(platform_file_handler FileHandler, char* PathBuffer, s32 BufferLength, char* FilterStrings)
{
// TODO(Peter): Convert Path to be a string
b32 Result = FileHandler.GetFilePath(PathBuffer, BufferLength, (const char*)FilterStrings);
return Result;
}
internal b32
GetFilePath(context Context, char* PathBuffer, s32 BufferLength, char* FilterStrings)
{
return GetFilePath(Context.FileHandler, PathBuffer, BufferLength, FilterStrings);
}
#define FOLDHAUS_PLATFORM_H #define FOLDHAUS_PLATFORM_H
#endif // FOLDHAUS_PLATFORM_H #endif // FOLDHAUS_PLATFORM_H

View File

@ -66,7 +66,7 @@ HierarchyView_Render(panel Panel, rect PanelBounds, render_command_buffer* Rende
if (ui_LayoutButton(&State->Interface_, &Layout, TempString, ListItemBGColor, ListItemHover, ListItemSelected)) if (ui_LayoutButton(&State->Interface_, &Layout, TempString, ListItemBGColor, ListItemHover, ListItemSelected))
{ {
char FilePath[256]; char FilePath[256];
b32 Success = Context.PlatformGetFilePath(FilePath, 256, "Foldhaus Files\0*.fold\0\0"); b32 Success = GetFilePath(Context, FilePath, 256, "Foldhaus Files\0*.fold\0\0");
if (Success) if (Success)
{ {
LoadAssembly(State, Context, FilePath); LoadAssembly(State, Context, FilePath);

View File

@ -132,7 +132,7 @@ WorkerThreadProc (LPVOID InputThreadInfo)
Entry = CompleteAndTakeNextJob(ThreadInfo->Queue, Entry); Entry = CompleteAndTakeNextJob(ThreadInfo->Queue, Entry);
if (Entry.IsValid) if (Entry.IsValid)
{ {
ThreadInfo->Queue->Jobs[Entry.Index].WorkProc(ThreadInfo->ID, ThreadInfo->Queue->Jobs[Entry.Index].WorkProc(ThreadInfo->ID,
ThreadInfo->Queue->Jobs[Entry.Index].Data); ThreadInfo->Queue->Jobs[Entry.Index].Data);
} }
else else
@ -176,10 +176,10 @@ PLATFORM_SET_SOCKET_OPTION(Win32SetSocketOption)
PLATFORM_GET_SOCKET_HANDLE(Win32GetSocketHandle) PLATFORM_GET_SOCKET_HANDLE(Win32GetSocketHandle)
{ {
// NOTE(Peter): These used to be passed in as paramters, but we only use this function // NOTE(Peter): These used to be passed in as paramters, but we only use this function
// with AF_INET, SOCK_DGRAM, and Protocol = 0. These are also platform specific values // with AF_INET, SOCK_DGRAM, and Protocol = 0. These are also platform specific values
// so I was having to include windows.h in the platform agnostic code to accomodate that // so I was having to include windows.h in the platform agnostic code to accomodate that
// function signature. // function signature.
s32 AddressFamily = AF_INET; s32 AddressFamily = AF_INET;
s32 Type = SOCK_DGRAM; s32 Type = SOCK_DGRAM;
s32 Protocol = 0; s32 Protocol = 0;
@ -207,7 +207,7 @@ PLATFORM_GET_SOCKET_HANDLE(Win32GetSocketHandle)
SocketValues[NewSocketIndex].Socket = socket(AddressFamily, Type, Protocol); SocketValues[NewSocketIndex].Socket = socket(AddressFamily, Type, Protocol);
int Error = Win32SetSocketOption(NewSocketIndex, IPPROTO_IP, IP_MULTICAST_TTL, int Error = Win32SetSocketOption(NewSocketIndex, IPPROTO_IP, IP_MULTICAST_TTL,
(const char*)(&Multicast_TimeToLive), sizeof(Multicast_TimeToLive)); (const char*)(&Multicast_TimeToLive), sizeof(Multicast_TimeToLive));
return (platform_socket_handle)NewSocketIndex; return (platform_socket_handle)NewSocketIndex;
@ -265,7 +265,7 @@ GET_FONT_INFO(Win32GetFontInfo)
CurrentFont = CreateFont(PixelHeight, 0, 0, 0, CurrentFont = CreateFont(PixelHeight, 0, 0, 0,
FontWeight, FontWeight,
Italic, Italic,
Underline, Underline,
Strikeout, Strikeout,
ANSI_CHARSET, ANSI_CHARSET,
@ -311,7 +311,7 @@ DRAW_FONT_CODEPOINT(Win32DrawFontCodepoint)
COLORREF PixelColor; COLORREF PixelColor;
for (u32 Y = 0; Y < *OutHeight; Y++) for (u32 Y = 0; Y < *OutHeight; Y++)
{ {
// NOTE(Peter): XOffset * 4 b/c its 4 bytes per pixel. // NOTE(Peter): XOffset * 4 b/c its 4 bytes per pixel.
u8* Channel = (u8*)Row + (XOffset * 4); u8* Channel = (u8*)Row + (XOffset * 4);
for (u32 X = 0; X < *OutWidth; X++) for (u32 X = 0; X < *OutWidth; X++)
{ {
@ -392,7 +392,7 @@ HandleWindowMessage (MSG Message, window* Window, input_queue* InputQueue, mouse
b32 AltDown = GetKeyState(VK_MENU) & 0x8000; b32 AltDown = GetKeyState(VK_MENU) & 0x8000;
b32 CtrlDown = GetKeyState(VK_CONTROL) & 0x8000; b32 CtrlDown = GetKeyState(VK_CONTROL) & 0x8000;
AddInputEventEntry(InputQueue, KeyCode_MouseLeftButton, false, true, AddInputEventEntry(InputQueue, KeyCode_MouseLeftButton, false, true,
ShiftDown, AltDown, CtrlDown, false); ShiftDown, AltDown, CtrlDown, false);
Mouse->LeftButtonState = KeyState_IsDown & ~KeyState_WasDown; Mouse->LeftButtonState = KeyState_IsDown & ~KeyState_WasDown;
@ -414,7 +414,7 @@ HandleWindowMessage (MSG Message, window* Window, input_queue* InputQueue, mouse
b32 AltDown = GetKeyState(VK_MENU) & 0x8000; b32 AltDown = GetKeyState(VK_MENU) & 0x8000;
b32 CtrlDown = GetKeyState(VK_CONTROL) & 0x8000; b32 CtrlDown = GetKeyState(VK_CONTROL) & 0x8000;
AddInputEventEntry(InputQueue, KeyCode_MouseMiddleButton, false, true, AddInputEventEntry(InputQueue, KeyCode_MouseMiddleButton, false, true,
ShiftDown, AltDown, CtrlDown, false); ShiftDown, AltDown, CtrlDown, false);
Mouse->MiddleButtonState = KeyState_IsDown & ~KeyState_WasDown; Mouse->MiddleButtonState = KeyState_IsDown & ~KeyState_WasDown;
@ -428,7 +428,7 @@ HandleWindowMessage (MSG Message, window* Window, input_queue* InputQueue, mouse
b32 AltDown = GetKeyState(VK_MENU) & 0x8000; b32 AltDown = GetKeyState(VK_MENU) & 0x8000;
b32 CtrlDown = GetKeyState(VK_CONTROL) & 0x8000; b32 CtrlDown = GetKeyState(VK_CONTROL) & 0x8000;
AddInputEventEntry(InputQueue, KeyCode_MouseRightButton, false, true, AddInputEventEntry(InputQueue, KeyCode_MouseRightButton, false, true,
ShiftDown, AltDown, CtrlDown, false); ShiftDown, AltDown, CtrlDown, false);
Mouse->RightButtonState = KeyState_IsDown & ~KeyState_WasDown; Mouse->RightButtonState = KeyState_IsDown & ~KeyState_WasDown;
Mouse->DownPos = Mouse->Pos; Mouse->DownPos = Mouse->Pos;
@ -443,7 +443,7 @@ HandleWindowMessage (MSG Message, window* Window, input_queue* InputQueue, mouse
b32 AltDown = GetKeyState(VK_MENU) & 0x8000; b32 AltDown = GetKeyState(VK_MENU) & 0x8000;
b32 CtrlDown = GetKeyState(VK_CONTROL) & 0x8000; b32 CtrlDown = GetKeyState(VK_CONTROL) & 0x8000;
AddInputEventEntry(InputQueue, KeyCode_MouseLeftButton, true, false, AddInputEventEntry(InputQueue, KeyCode_MouseLeftButton, true, false,
ShiftDown, AltDown, CtrlDown, false); ShiftDown, AltDown, CtrlDown, false);
Mouse->LeftButtonState = ~KeyState_IsDown & KeyState_WasDown; Mouse->LeftButtonState = ~KeyState_IsDown & KeyState_WasDown;
@ -457,7 +457,7 @@ HandleWindowMessage (MSG Message, window* Window, input_queue* InputQueue, mouse
b32 AltDown = GetKeyState(VK_MENU) & 0x8000; b32 AltDown = GetKeyState(VK_MENU) & 0x8000;
b32 CtrlDown = GetKeyState(VK_CONTROL) & 0x8000; b32 CtrlDown = GetKeyState(VK_CONTROL) & 0x8000;
AddInputEventEntry(InputQueue, KeyCode_MouseMiddleButton, true, false, AddInputEventEntry(InputQueue, KeyCode_MouseMiddleButton, true, false,
ShiftDown, AltDown, CtrlDown, false); ShiftDown, AltDown, CtrlDown, false);
Mouse->MiddleButtonState = ~KeyState_IsDown & KeyState_WasDown; Mouse->MiddleButtonState = ~KeyState_IsDown & KeyState_WasDown;
@ -471,7 +471,7 @@ HandleWindowMessage (MSG Message, window* Window, input_queue* InputQueue, mouse
b32 AltDown = GetKeyState(VK_MENU) & 0x8000; b32 AltDown = GetKeyState(VK_MENU) & 0x8000;
b32 CtrlDown = GetKeyState(VK_CONTROL) & 0x8000; b32 CtrlDown = GetKeyState(VK_CONTROL) & 0x8000;
AddInputEventEntry(InputQueue, KeyCode_MouseRightButton, true, false, AddInputEventEntry(InputQueue, KeyCode_MouseRightButton, true, false,
ShiftDown, AltDown, CtrlDown, false); ShiftDown, AltDown, CtrlDown, false);
Mouse->RightButtonState = ~KeyState_IsDown & KeyState_WasDown; Mouse->RightButtonState = ~KeyState_IsDown & KeyState_WasDown;
@ -496,7 +496,7 @@ HandleWindowMessage (MSG Message, window* Window, input_queue* InputQueue, mouse
b32 CtrlDown = GetKeyState(VK_CONTROL) & 0x8000; b32 CtrlDown = GetKeyState(VK_CONTROL) & 0x8000;
// New Input Queue // New Input Queue
AddInputEventEntry(InputQueue, Key, KeyWasDown, KeyIsDown, AddInputEventEntry(InputQueue, Key, KeyWasDown, KeyIsDown,
ShiftDown, AltDown, CtrlDown, false); ShiftDown, AltDown, CtrlDown, false);
}break; }break;
@ -553,7 +553,7 @@ Win32Realloc(u8* Buf, s32 OldSize, s32 NewSize)
return NewMemory; return NewMemory;
} }
internal s32 internal s32
Win32GetThreadId() Win32GetThreadId()
{ {
s32 Result = GetCurrentThreadId(); s32 Result = GetCurrentThreadId();
@ -561,8 +561,8 @@ Win32GetThreadId()
} }
// NOTE(Peter): Only meant to take one of the values specified below: // 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_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_ICON, IDC_NO, IDC_SIZE, IDC_SIZEALL, IDC_SIZENESW, IDC_SIZENS, IDC_SIZENWSE,
// IDC_SIZEWE, IDC_UPARROW, IDC_WAIT // IDC_SIZEWE, IDC_UPARROW, IDC_WAIT
internal HCURSOR internal HCURSOR
Win32LoadSystemCursor(char* CursorIdentifier) Win32LoadSystemCursor(char* CursorIdentifier)
@ -601,12 +601,12 @@ WinMain (
GlobalDebugServices = (debug_services*)malloc(sizeof(debug_services)); GlobalDebugServices = (debug_services*)malloc(sizeof(debug_services));
s32 DebugThreadCount = PLATFORM_THREAD_COUNT + 1; s32 DebugThreadCount = PLATFORM_THREAD_COUNT + 1;
InitDebugServices(GlobalDebugServices, InitDebugServices(GlobalDebugServices,
PerformanceCountFrequency, PerformanceCountFrequency,
DEBUGAlloc, DEBUGAlloc,
Win32Realloc, Win32Realloc,
GetWallClock, GetWallClock,
Win32GetThreadId, Win32GetThreadId,
DebugThreadCount); DebugThreadCount);
input_queue InputQueue; input_queue InputQueue;
@ -666,9 +666,9 @@ WinMain (
Context.PlatformAlloc = Win32Alloc; Context.PlatformAlloc = Win32Alloc;
Context.PlatformFree = Win32Free; Context.PlatformFree = Win32Free;
Context.PlatformRealloc = Win32Realloc; Context.PlatformRealloc = Win32Realloc;
Context.PlatformReadEntireFile = Win32ReadEntireFile; Context.FileHandler.ReadEntireFile = Win32ReadEntireFile;
Context.PlatformWriteEntireFile = Win32WriteEntireFile; Context.FileHandler.WriteEntireFile = Win32WriteEntireFile;
Context.PlatformGetFilePath = Win32SystemDialogueOpenFile; Context.FileHandler.GetFilePath = Win32SystemDialogueOpenFile;
Context.PlatformGetGPUTextureHandle = Win32GetGPUTextureHandle; Context.PlatformGetGPUTextureHandle = Win32GetGPUTextureHandle;
Context.PlatformGetSocketHandle = Win32GetSocketHandle; Context.PlatformGetSocketHandle = Win32GetSocketHandle;
Context.PlatformSetSocketOption = Win32SetSocketOption; Context.PlatformSetSocketOption = Win32SetSocketOption;
@ -702,9 +702,9 @@ WinMain (
Context.WindowIsVisible = true; Context.WindowIsVisible = true;
while (Running) while (Running)
{ {
if (GlobalDebugServices->RecordFrames) if (GlobalDebugServices->RecordFrames)
{ {
EndDebugFrame(GlobalDebugServices); EndDebugFrame(GlobalDebugServices);
} }
DEBUG_TRACK_SCOPE(MainLoop); DEBUG_TRACK_SCOPE(MainLoop);