diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 5375bbf..6a38b7f 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -3,7 +3,9 @@ { "name": "Win32 msvc", "includePath": [ - "${workspaceFolder}/project/**" + "${workspaceFolder}/project/", + "${workspaceFolder}/project/dependencies/", + "${workspaceFolder}/project/platform/", ], "defines": [ "_DEBUG", @@ -19,7 +21,9 @@ { "name": "Win32 clang", "includePath": [ - "${workspaceFolder}/project/**" + "${workspaceFolder}/project/", + "${workspaceFolder}/project/dependencies/", + "${workspaceFolder}/project/platform/", ], "defines": [ "_DEBUG", diff --git a/.vscode/settings.json b/.vscode/settings.json index b892a69..c380024 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -30,10 +30,11 @@ "list": "cpp", "xhash": "cpp", "shared_mutex": "cpp", - "mutex": "cpp" + "mutex": "cpp", + "system_error": "cpp" }, "C_Cpp.intelliSenseEngineFallback": "disabled", - "C_Cpp.errorSquiggles": "disabled", // This doesn't work well with how the headers are included. + "C_Cpp.errorSquiggles": "enabled", "C_Cpp.default.compilerPath": "cl.exe", "C_Cpp.exclusionPolicy": "checkFilesAndFolders", "C_Cpp.files.exclude": { diff --git a/HandmadeHero.10x b/HandmadeHero.10x index 6bdde83..227eb7d 100644 --- a/HandmadeHero.10x +++ b/HandmadeHero.10x @@ -14,10 +14,10 @@ pwsh $(WorkspaceDirectory)/scripts/clean.ps1 - $(WorkspaceDirectory)/projects/build/handmade_win32.exe - $(WorkspaceDirectory)/projects - $(WorkspaceDirectory)/projects/build/handmade_win32.exe - $(WorkspaceDirectory)/projects/build/handmade_win32.exe + $(WorkspaceDirectory)/build/handmade_win32.exe + $(WorkspaceDirectory)/data + $(WorkspaceDirectory)/build/handmade_win32.exe + $(WorkspaceDirectory)/build/handmade_win32.exe false diff --git a/README.md b/README.md index bfad4ff..596d58e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # HandmadeHero -Any code I do for this series will be here. +Any code I do for this [series](https://handmadehero.org) will be here. ***(Only original hand-written code will be here, no code from the series itself)*** diff --git a/project/handmade_win32.cpp b/project/handmade_win32.cpp index 0fd763e..a56d9f6 100644 --- a/project/handmade_win32.cpp +++ b/project/handmade_win32.cpp @@ -12,23 +12,34 @@ #include "macros.h" #include "types.h" -#include +// #include #include "win32.h" // Using this to get dualsense controllers #include "JoyShockLibrary/JoyShockLibrary.h" +// TOOD(Ed): Redo these macros properly later. + #define congrats( message ) do { \ JslSetLightColour( 0, (255 << 16) | (215 << 8) ); \ - JslSetRumble( 0, 0, 255 ); \ MessageBoxA( 0, message, "Congratulations!", MB_OK | MB_ICONEXCLAMATION ); \ JslSetLightColour( 0, (255 << 8 ) ); \ } while (0) +#define ensure( condition, message ) ensure_impl( condition, message ) +inline bool +ensure_impl( bool condition, char const* message ) { + if ( ! condition ) { + JslSetLightColour( 0, (255 << 16) ); + MessageBoxA( 0, message, "Ensure Failure", MB_OK | MB_ICONASTERISK ); + JslSetLightColour( 0, ( 255 << 8 ) ); + } + return condition; +} + #define fatal(message) do { \ JslSetLightColour( 0, (255 << 16) ); \ - JslSetRumble( 0, 0, 255 ); \ MessageBoxA( 0, message, "Fatal Error", MB_OK | MB_ICONERROR ); \ JslSetLightColour( 0, (255 << 8 ) ); \ } while (0) @@ -59,7 +70,100 @@ struct WinDimensions global OffscreenBuffer BackBuffer; global WinDimensions WindowDimensions; -WinDimensions get_window_dimensions( HWND window_handle ) +HRESULT WINAPI +DirectSoundCreate(LPGUID lpGuid, LPDIRECTSOUND* ppDS, LPUNKNOWN pUnkOuter ); + +using DirectSoundCreateFn = HRESULT WINAPI (LPGUID lpGuid, LPDIRECTSOUND* ppDS, LPUNKNOWN pUnkOuter ); + +global DirectSoundCreateFn* direct_sound_create; + + +internal void +init_sound(HWND window_handle, s32 samples_per_second, s32 buffer_size ) +{ + // Load library + HMODULE sound_library = LoadLibraryA( "dsound.dll" ); + if ( ! ensure(sound_library, "Failed to load direct sound library" ) ) + { + // TOOD : Diagnostic + return; + } + + // Get direct sound object + direct_sound_create = rcast( DirectSoundCreateFn*, GetProcAddress( sound_library, "DirectSoundCreate" )); + if ( ! ensure(direct_sound_create, "Failed to get direct_sound_create_procedure" ) ) + { + // TOOD : Diagnostic + return; + } + + LPDIRECTSOUND direct_sound; + if ( ! SUCCEEDED(direct_sound_create( 0, & direct_sound, 0 )) ) + { + // TODO : Diagnostic + } + + if ( ! SUCCEEDED( direct_sound->SetCooperativeLevel(window_handle, DSSCL_PRIORITY) ) ) + { + // TODO : Diagnostic + } + + WAVEFORMATEX + wave_format {}; + wave_format.wFormatTag = WAVE_FORMAT_PCM; /* format type */ + wave_format.nChannels = 2; /* number of channels (i.e. mono, stereo...) */ + wave_format.nSamplesPerSec = samples_per_second; /* sample rate */ + wave_format.wBitsPerSample = 16; /* number of bits per sample of mono data */ + wave_format.nBlockAlign = wave_format.nChannels * wave_format.wBitsPerSample / 8 ; /* block size of data */ + wave_format.nAvgBytesPerSec = wave_format.nSamplesPerSec * wave_format.nBlockAlign; /* for buffer estimation */ + wave_format.cbSize = 0; /* the count in bytes of the size of */ + + LPDIRECTSOUNDBUFFER primary_buffer; + { + DSBUFFERDESC + buffer_description { sizeof(buffer_description) }; + buffer_description.dwFlags = DSBCAPS_PRIMARYBUFFER; + buffer_description.dwBufferBytes = 0; + + if ( ! SUCCEEDED( direct_sound->CreateSoundBuffer( & buffer_description, & primary_buffer, 0 ) )) + { + // TODO : Diagnostic + } + if ( ! SUCCEEDED( primary_buffer->SetFormat( & wave_format ) ) ) + { + // TODO : Diagnostic + } + + // Format is finally set! + } + + LPDIRECTSOUNDBUFFER secondary_buffer; + { + DSBUFFERDESC + buffer_description { sizeof(buffer_description) }; + buffer_description.dwFlags = 0; + buffer_description.dwBufferBytes = buffer_size; + buffer_description.lpwfxFormat = & wave_format; + + if ( ! SUCCEEDED( direct_sound->CreateSoundBuffer( & buffer_description, & secondary_buffer, 0 ) )) + { + // TODO : Diagnostic + } + if ( ! SUCCEEDED( secondary_buffer->SetFormat( & wave_format ) ) ) + { + // TODO : Diagnostic + } + } + + // Create primary buffer + + // Create secondary buffer + + // Start playing +} + +internal WinDimensions +get_window_dimensions( HWND window_handle ) { RECT client_rect; GetClientRect( window_handle, & client_rect ); @@ -148,7 +252,7 @@ resize_dib_section( OffscreenBuffer* buffer, u32 width, u32 height ) // We want to "touch" a pixel on every 4-byte boundary u32 BitmapMemorySize = (buffer->Width * buffer->Height) * buffer->BytesPerPixel; - buffer->Memory = VirtualAlloc( NULL, BitmapMemorySize, MEM_Commit_Zeroed, Page_Read_Write ); + buffer->Memory = VirtualAlloc( NULL, BitmapMemorySize, MEM_Commit_Zeroed | MEM_Reserve, Page_Read_Write ); // TODO(Ed) : Clear to black } @@ -208,8 +312,9 @@ main_window_callback( case WM_KEYUP: { u32 vk_code = w_param; - bool is_down = (l_param >> 31) == 0; - bool was_down = (l_param >> 30) != 0; + b32 is_down = (l_param >> 31) == 0; + b32 was_down = (l_param >> 30); + b32 alt_down = (l_param & (1 << 29)); switch ( vk_code ) { @@ -273,6 +378,12 @@ main_window_callback( OutputDebugStringA( "Space\n" ); } break; + case VK_F4: + { + if ( alt_down ) + Running = false; + } + break; } } break; @@ -377,6 +488,8 @@ WinMain( WinDimensions dimensions = get_window_dimensions( window_handle ); resize_dib_section( &BackBuffer, 1280, 720 ); + init_sound( window_handle, 48000, 48000 * sizeof(s16) * 2 ); + MSG msg_info; u32 x_offset = 0; diff --git a/project/platform/win32.h b/project/platform/win32.h index 3abafab..3cffb1c 100644 --- a/project/platform/win32.h +++ b/project/platform/win32.h @@ -5,6 +5,8 @@ Alternative header for windows.h #define WIN32_LEAN_AND_MEAN #include #include +#include +#include // #include "windows/windows_base.h" // #include "windows/window.h" @@ -115,12 +117,12 @@ WIN_LIB_API DWORD WINAPI XInputSetState DWORD WINAPI xinput_get_state_stub( DWORD dwUserIndex, XINPUT_STATE* pVibration ) { OutputDebugStringA( "xinput_get_state stubbed!\n"); - return ERROR_CALL_NOT_IMPLEMENTED; + return ERROR_DEVICE_NOT_CONNECTED; } DWORD WINAPI xinput_set_state_stub( DWORD dwUserIndex, XINPUT_VIBRATION* pVibration ) { OutputDebugStringA( "xinput_set_state stubbed!\n"); - return ERROR_CALL_NOT_IMPLEMENTED; + return ERROR_DEVICE_NOT_CONNECTED; } using XInputGetStateFn = DWORD WINAPI( DWORD dwUserIndex, XINPUT_STATE* pVibration ); diff --git a/scripts/handmade.rdbg b/scripts/handmade.rdbg index c40d359..41871f5 100644 Binary files a/scripts/handmade.rdbg and b/scripts/handmade.rdbg differ