mirror of
				https://github.com/Ed94/HandmadeHero.git
				synced 2025-10-31 06:50:54 -07:00 
			
		
		
		
	Day 14 complete
This commit is contained in:
		| @@ -3,43 +3,46 @@ | ||||
|  | ||||
| NS_ENGINE_BEGIN | ||||
|  | ||||
| struct EngineState | ||||
| { | ||||
| 	s32 WavePeriod; | ||||
| 	s32 WaveToneHz; | ||||
| 	s32 ToneVolume; | ||||
| 	s32 XOffset; | ||||
| 	s32 YOffset; | ||||
| }; | ||||
|  | ||||
| using GetSoundSampleValueFn = s16( SoundBuffer* sound_buffer ); | ||||
|  | ||||
| global s32 SoundTest_ToneVolume = 3000; | ||||
| global s32 SoundTest_WavePeriod = 0; | ||||
| global s32 SoundTest_WaveToneHz = 262; | ||||
|  | ||||
| using GetSoundSampleValueFn = s16( EngineState* state, SoundBuffer* sound_buffer ); | ||||
|  | ||||
| internal s16 | ||||
| square_wave_sample_value( SoundBuffer* sound_buffer ) | ||||
| square_wave_sample_value( EngineState* state, SoundBuffer* sound_buffer ) | ||||
| { | ||||
| 	s16 sample_value = (sound_buffer->RunningSampleIndex /  (SoundTest_WavePeriod /2)) % 2 ? | ||||
| 		SoundTest_ToneVolume : - SoundTest_ToneVolume; | ||||
| 	s16 sample_value = (sound_buffer->RunningSampleIndex /  (state->WavePeriod / 2) ) % 2 ? | ||||
| 		state->ToneVolume : - state->ToneVolume; | ||||
|  | ||||
| 	return sample_value; | ||||
| } | ||||
|  | ||||
| internal s16 | ||||
| sine_wave_sample_value( SoundBuffer* sound_buffer ) | ||||
| sine_wave_sample_value( EngineState* state, SoundBuffer* sound_buffer ) | ||||
| { | ||||
| 	local_persist f32 time = 0.f; | ||||
|  | ||||
| 	// time =  TAU * (f32)sound_buffer->RunningSampleIndex / (f32)SoundTest_WavePeriod; | ||||
| 	f32 sine_value   = sinf( time ); | ||||
| 	s16 sample_value = scast(u16, sine_value * SoundTest_ToneVolume); | ||||
| 	s16 sample_value = scast(u16, sine_value * state->ToneVolume); | ||||
|  | ||||
| 	time += TAU * 1.0f / scast(f32, SoundTest_WavePeriod ); | ||||
| 	time += TAU * 1.0f / scast(f32, state->WavePeriod ); | ||||
| 	return sample_value; | ||||
| } | ||||
|  | ||||
| internal void | ||||
| output_sound( SoundBuffer* sound_buffer, GetSoundSampleValueFn* get_sample_value ) | ||||
| output_sound( EngineState* state, SoundBuffer* sound_buffer, GetSoundSampleValueFn* get_sample_value ) | ||||
| { | ||||
| 	s16* sample_out = sound_buffer->Samples; | ||||
| 	for ( u32 sample_index = 0; sample_index < sound_buffer->NumSamples; ++ sample_index ) | ||||
| 	{ | ||||
| 		s16 sample_value = get_sample_value( sound_buffer ); | ||||
| 		s16 sample_value = get_sample_value( state, sound_buffer ); | ||||
| 		sound_buffer->RunningSampleIndex++; | ||||
|  | ||||
| 		// char ms_timing_debug[256] {}; | ||||
| @@ -106,9 +109,19 @@ b32 input_using_analog() | ||||
| 	return false; | ||||
| } | ||||
|  | ||||
| internal void | ||||
| startup() | ||||
| { | ||||
| } | ||||
|  | ||||
| internal void | ||||
| shutdown() | ||||
| { | ||||
| } | ||||
|  | ||||
| // TODO : I rather expose the back_buffer and sound_buffer using getters for access in any function. | ||||
| internal void | ||||
| update_and_render( InputState* input, OffscreenBuffer* back_buffer, SoundBuffer* sound_buffer ) | ||||
| update_and_render( InputState* input, OffscreenBuffer* back_buffer, SoundBuffer* sound_buffer, Memory* memory ) | ||||
| { | ||||
| 	// Graphics & Input Test | ||||
| 	local_persist u32 x_offset = 0; | ||||
| @@ -128,11 +141,17 @@ update_and_render( InputState* input, OffscreenBuffer* back_buffer, SoundBuffer* | ||||
| 	} | ||||
| #endif | ||||
|  | ||||
| #if 1 | ||||
| 	EngineState* state = rcast( EngineState*, memory->Persistent ); | ||||
| 	do_once_start | ||||
| 	{ | ||||
| 		SoundTest_WavePeriod = sound_buffer->SamplesPerSecond / SoundTest_WaveToneHz; | ||||
| 	} | ||||
| 		assert( sizeof(EngineState) <= memory->PersistentSize ); | ||||
|  | ||||
| 		state->ToneVolume = 3000; | ||||
| 		state->WaveToneHz = 262; | ||||
| 		state->WavePeriod = sound_buffer->SamplesPerSecond / state->WaveToneHz; | ||||
|  | ||||
| 		state->XOffset = 0; | ||||
| 		state->YOffset = 0; | ||||
|  | ||||
| 	do_once_end | ||||
|  | ||||
| 	ControllerState* controller = & input->Controllers[0]; | ||||
| @@ -151,22 +170,22 @@ update_and_render( InputState* input, OffscreenBuffer* back_buffer, SoundBuffer* | ||||
|  | ||||
| 		if ( pad->Triangle.State ) | ||||
| 		{ | ||||
| 			SoundTest_ToneVolume += 10; | ||||
| 			state->ToneVolume += 10; | ||||
| 		} | ||||
| 		if ( pad->Circle.State ) | ||||
| 		{ | ||||
| 			SoundTest_ToneVolume -= 10; | ||||
| 			state->ToneVolume -= 10; | ||||
| 		} | ||||
|  | ||||
| 		if ( pad->Square.State ) | ||||
| 		{ | ||||
| 			SoundTest_WaveToneHz  += 1; | ||||
| 			SoundTest_WavePeriod  = sound_buffer->SamplesPerSecond / SoundTest_WaveToneHz; | ||||
| 			state->WaveToneHz += 1; | ||||
| 			state->WavePeriod  = sound_buffer->SamplesPerSecond / state->WaveToneHz; | ||||
| 		} | ||||
| 		if ( pad->X.State ) | ||||
| 		{ | ||||
| 			SoundTest_WaveToneHz  -= 1; | ||||
| 			SoundTest_WavePeriod  = sound_buffer->SamplesPerSecond / SoundTest_WaveToneHz; | ||||
| 			state->WaveToneHz -= 1; | ||||
| 			state->WavePeriod  = sound_buffer->SamplesPerSecond / state->WaveToneHz; | ||||
| 		} | ||||
|  | ||||
| 		if ( pad->Options.State ) | ||||
| @@ -193,22 +212,22 @@ update_and_render( InputState* input, OffscreenBuffer* back_buffer, SoundBuffer* | ||||
|  | ||||
| 		if ( pad->Y.State ) | ||||
| 		{ | ||||
| 			SoundTest_ToneVolume += 10; | ||||
| 			state->ToneVolume += 10; | ||||
| 		} | ||||
| 		if ( pad->B.State ) | ||||
| 		{ | ||||
| 			SoundTest_ToneVolume -= 10; | ||||
| 			state->ToneVolume -= 10; | ||||
| 		} | ||||
|  | ||||
| 		if ( pad->X.State ) | ||||
| 		{ | ||||
| 			SoundTest_WaveToneHz  += 1; | ||||
| 			SoundTest_WavePeriod  = sound_buffer->SamplesPerSecond / SoundTest_WaveToneHz; | ||||
| 			state->WaveToneHz += 1; | ||||
| 			state->WavePeriod  = sound_buffer->SamplesPerSecond / state->WaveToneHz; | ||||
| 		} | ||||
| 		if ( pad->A.State ) | ||||
| 		{ | ||||
| 			SoundTest_WaveToneHz  -= 1; | ||||
| 			SoundTest_WavePeriod  = sound_buffer->SamplesPerSecond / SoundTest_WaveToneHz; | ||||
| 			state->WaveToneHz -= 1; | ||||
| 			state->WavePeriod  = sound_buffer->SamplesPerSecond / state->WaveToneHz; | ||||
| 		} | ||||
|  | ||||
| 		if ( pad->Start.State ) | ||||
| @@ -221,13 +240,12 @@ update_and_render( InputState* input, OffscreenBuffer* back_buffer, SoundBuffer* | ||||
| 			// TODO(Ed) : Add rumble test | ||||
| 		} | ||||
| 	} | ||||
| #endif | ||||
|  | ||||
| 	// TODO(Ed) : Allow sample offsets here for more robust platform options | ||||
| 	if ( ! wave_switch ) | ||||
| 		output_sound( sound_buffer, sine_wave_sample_value ); | ||||
| 		output_sound( state, sound_buffer, sine_wave_sample_value ); | ||||
| 	else | ||||
| 		output_sound( sound_buffer, square_wave_sample_value ); | ||||
| 		output_sound( state, sound_buffer, square_wave_sample_value ); | ||||
|  | ||||
| 	render_weird_graident( back_buffer, x_offset, y_offset ); | ||||
| } | ||||
|   | ||||
| @@ -11,6 +11,29 @@ | ||||
|  | ||||
| NS_ENGINE_BEGIN | ||||
|  | ||||
| struct Clocks | ||||
| { | ||||
| 	// TODO(Ed) : Clock values... | ||||
| 	f32 SecondsElapsed; | ||||
| }; | ||||
|  | ||||
| struct Memory | ||||
| { | ||||
| 	// All memory for the engine is required to be zero initialized. | ||||
|  | ||||
| 	// Wiped on shutdown | ||||
| 	void* Persistent; | ||||
| 	u64   PersistentSize; | ||||
|  | ||||
| 	// Wiped on a per-frame basis | ||||
| 	// void* Frame; | ||||
| 	// u64   FrameSize; | ||||
|  | ||||
| 	// Wiped whenever the engine wants to? | ||||
| 	void* Transient; | ||||
| 	u64   TransientSize; | ||||
| }; | ||||
|  | ||||
| struct OffscreenBuffer | ||||
| { | ||||
| 	void*      Memory; // Lets use directly mess with the "pixel's memory buffer" | ||||
| @@ -157,6 +180,6 @@ b32 input_using_analog(); | ||||
|  | ||||
| // Needs a contextual reference to four things: | ||||
| // Timing, Input, Bitmap Buffer, Sound Buffer | ||||
| void update_and_render( InputState* input, OffscreenBuffer* back_buffer, SoundBuffer* sound_buffer ); | ||||
| void update_and_render( InputState* input, OffscreenBuffer* back_buffer, SoundBuffer* sound_buffer, Memory* memory ); | ||||
|  | ||||
| NS_ENGINE_END | ||||
|   | ||||
| @@ -97,7 +97,6 @@ struct SoundOutput | ||||
| 	s32   LatencySampleCount; | ||||
| }; | ||||
|  | ||||
|  | ||||
| HRESULT WINAPI DirectSoundCreate(LPGUID lpGuid, LPDIRECTSOUND* ppDS, LPUNKNOWN  pUnkOuter ); | ||||
|  | ||||
| using DirectSoundCreateFn = HRESULT WINAPI (LPGUID lpGuid, LPDIRECTSOUND* ppDS, LPUNKNOWN  pUnkOuter ); | ||||
| @@ -511,6 +510,48 @@ WinMain( | ||||
| { | ||||
| 	using namespace win32; | ||||
|  | ||||
| 	// Memory | ||||
| 	engine::Memory engine_memory {}; | ||||
| 	{ | ||||
| 		engine_memory.PersistentSize = megabytes( 64 ); | ||||
| 		// engine_memory.FrameSize	     = megabytes( 64 ); | ||||
| 		engine_memory.TransientSize  = gigabytes( 2 ); | ||||
|  | ||||
| 		u64 total_size = engine_memory.PersistentSize | ||||
| 			// + engine_memory.FrameSize | ||||
| 			+ engine_memory.TransientSize; | ||||
|  | ||||
| 	#if Build_Debug | ||||
| 		void* Base_Address      = (void*) terabytes( 1 ); | ||||
| 		// void* Frame_Address     = (void*) terabytes( 2 ); | ||||
| 		// void* Transient_Address = (void*) terabytes( 2 ); | ||||
| 	#else | ||||
| 		void* Base_Address      = 0; | ||||
| 		// void* Frame_Address     = 0; | ||||
| 		// void* Transient_Address = 0; | ||||
| 	#endif | ||||
|  | ||||
| 		engine_memory.Persistent = VirtualAlloc( Base_Address, total_size | ||||
| 			, MEM_Commit_Zeroed | MEM_Reserve, Page_Read_Write ); | ||||
| 		engine_memory.Transient = rcast( u8*, engine_memory.Persistent ) + engine_memory.PersistentSize; | ||||
|  | ||||
| 	#if 0 | ||||
| 		engine_memory.Frame = VirtualAlloc( 0, engine_memory.FrameSize | ||||
| 			, MEM_Commit_Zeroed | MEM_Reserve, Page_Read_Write ); | ||||
|  | ||||
| 		engine_memory.Transient = VirtualAlloc( 0, engine_memory.TransientSize | ||||
| 			, MEM_Commit_Zeroed | MEM_Reserve, Page_Read_Write ); | ||||
| 	#endif | ||||
|  | ||||
| 		if ( engine_memory.Persistent == nullptr | ||||
| 			// || ! engine_memory.Frame | ||||
| 			|| engine_memory.Transient == nullptr ) | ||||
| 		{ | ||||
| 			// TODO : Diagnostic Logging | ||||
| 			return -1; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// MessageBox( 0, L"First message!", L"Handmade Hero", MB_Ok_Btn | MB_Icon_Information ); | ||||
|  | ||||
| 	WNDCLASSW window_class {}; | ||||
| @@ -567,6 +608,8 @@ WinMain( | ||||
| 		SoundBufferSamples = rcast( s16*, VirtualAlloc( 0, 48000 * 2 * sizeof(s16) | ||||
| 			, MEM_Commit_Zeroed | MEM_Reserve, Page_Read_Write )); | ||||
|  | ||||
| 		assert( SoundBufferSamples ); | ||||
|  | ||||
| 		sound_output.RunningSampleIndex = 0; | ||||
| 		sound_output.LatencySampleCount = DS_SecondaryBuffer_SamplesPerSecond / 15; | ||||
| 		// ds_clear_sound_buffer( & sound_output ); | ||||
| @@ -599,8 +642,8 @@ WinMain( | ||||
|  | ||||
| 	using JSL_DeviceHandle = int; | ||||
| 	u32 jsl_num_devices | ||||
| 			// = JslConnectDevices(); | ||||
| 			= 0; | ||||
| 			= JslConnectDevices(); | ||||
| 			// = 0; | ||||
| 	JSL_DeviceHandle jsl_device_handles[4] {}; | ||||
| 	{ | ||||
| 		xinput_load_library_bindings(); | ||||
| @@ -809,7 +852,7 @@ WinMain( | ||||
| 		sound_buffer.SamplesPerSecond   = DS_SecondaryBuffer_SamplesPerSecond; | ||||
| 		sound_buffer.Samples            = SoundBufferSamples; | ||||
|  | ||||
| 		engine::update_and_render( & input, rcast(engine::OffscreenBuffer*, & BackBuffer.Memory), & sound_buffer ); | ||||
| 		engine::update_and_render( & input, rcast(engine::OffscreenBuffer*, & BackBuffer.Memory), & sound_buffer, & engine_memory ); | ||||
|  | ||||
| 		// Rendering | ||||
| 		{ | ||||
|   | ||||
| @@ -18,7 +18,7 @@ | ||||
| #define do_once()          \ | ||||
| 	do                     \ | ||||
| 	{                      \ | ||||
| 		static             \ | ||||
| 		local_persist      \ | ||||
| 		bool Done = false; \ | ||||
| 		if ( Done )        \ | ||||
| 			return;        \ | ||||
| @@ -29,7 +29,7 @@ | ||||
| #define do_once_start      \ | ||||
| 	do                     \ | ||||
| 	{                      \ | ||||
| 		static             \ | ||||
| 		local_persist      \ | ||||
| 		bool Done = false; \ | ||||
| 		if ( Done )        \ | ||||
| 			break;         \ | ||||
| @@ -41,3 +41,29 @@ | ||||
|  | ||||
|  | ||||
| #define array_count( array ) ( sizeof( array ) / sizeof( ( array )[0] ) ) | ||||
|  | ||||
| // TODO(Ed) : Move to memory header eventually | ||||
| #define kilobytes( x ) ( ( x ) * ( s64 )( 1024 ) ) | ||||
| #define megabytes( x ) ( kilobytes( x ) * ( s64 )( 1024 ) ) | ||||
| #define gigabytes( x ) ( megabytes( x ) * ( s64 )( 1024 ) ) | ||||
| #define terabytes( x ) ( gigabytes( x ) * ( s64 )( 1024 ) ) | ||||
|  | ||||
| // TODO(Ed) : Move to debug header eventually | ||||
|  | ||||
| #if Build_Development | ||||
| #	define assert( expression ) \ | ||||
| 		if ( !( expression ) )   \ | ||||
| 		{                        \ | ||||
| 			*( int* )0 = 0;      \ | ||||
| 		} | ||||
| 	// platform::assertion_failure( __FILE__, __LINE__, #expression ); | ||||
| #else | ||||
| #	define assert( expression ) | ||||
| #endif | ||||
|  | ||||
| // TODO(Ed) : Add this sauce later | ||||
| #if 0 | ||||
| #define congrats( message ) | ||||
| #define ensure( condition, expression ) | ||||
| #define fatal( message ) | ||||
| #endif | ||||
|   | ||||
							
								
								
									
										0
									
								
								project/platform/memory.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								project/platform/memory.h
									
									
									
									
									
										Normal file
									
								
							| @@ -9,4 +9,3 @@ | ||||
| #include "generics.h" | ||||
| #include "math_constants.h" | ||||
| #include "types.h" | ||||
|  | ||||
|   | ||||
| @@ -15,7 +15,7 @@ | ||||
| // #include "windows/file.h" | ||||
| // #include "windows/io.h" | ||||
|  | ||||
| // #ifdef Build_Debug | ||||
| // #if Build_Debug | ||||
| // #	include "windows/dbghelp.h" | ||||
| // #endif | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user