HandmadeHero/project/engine/engine.cpp

315 lines
7.9 KiB
C++
Raw Normal View History

2023-09-23 18:03:33 -07:00
//#include "win32.h"
2023-09-26 14:26:35 -07:00
#include "engine.hpp"
#include "platform/platform_engine_api.hpp"
#include "handmade.hpp"
2023-09-15 18:35:27 -07:00
NS_ENGINE_BEGIN
2023-09-18 17:16:40 -07:00
struct EngineState
{
s32 WaveToneHz;
s32 ToneVolume;
s32 XOffset;
s32 YOffset;
2023-09-26 14:26:35 -07:00
b32 RendererPaused;
f32 SampleWaveSineTime;
b32 SampleWaveSwitch;
2023-09-18 17:16:40 -07:00
};
2023-09-16 15:41:07 -07:00
2023-09-24 19:37:05 -07:00
using GetSoundSampleValueFn = s16( EngineState* state, AudioBuffer* sound_buffer );
2023-09-16 15:41:07 -07:00
internal s16
2023-09-24 19:37:05 -07:00
square_wave_sample_value( EngineState* state, AudioBuffer* sound_buffer )
2023-09-16 15:41:07 -07:00
{
2023-09-24 19:37:05 -07:00
s32 wave_period = sound_buffer->SamplesPerSecond / state->WaveToneHz;
s32 sample_value = (sound_buffer->RunningSampleIndex / (wave_period / 2) ) % 2 ?
2023-09-18 17:16:40 -07:00
state->ToneVolume : - state->ToneVolume;
2023-09-16 15:41:07 -07:00
2023-09-20 21:26:23 -07:00
return scast(s16, sample_value);
2023-09-16 15:41:07 -07:00
}
internal s16
2023-09-24 19:37:05 -07:00
sine_wave_sample_value( EngineState* state, AudioBuffer* sound_buffer )
2023-09-16 15:41:07 -07:00
{
2023-09-26 14:26:35 -07:00
f32& time = state->SampleWaveSineTime;
2023-09-16 15:41:07 -07:00
2023-09-24 19:37:05 -07:00
s32 wave_period = sound_buffer->SamplesPerSecond / state->WaveToneHz;
2023-09-17 18:20:11 -07:00
// time = TAU * (f32)sound_buffer->RunningSampleIndex / (f32)SoundTest_WavePeriod;
2023-09-16 15:41:07 -07:00
f32 sine_value = sinf( time );
2023-09-20 21:26:23 -07:00
s16 sample_value = scast(s16, sine_value * scast(f32, state->ToneVolume));
2023-09-16 15:41:07 -07:00
2023-09-24 19:37:05 -07:00
time += TAU * 1.0f / scast(f32, wave_period );
if ( time > TAU )
{
time -= TAU;
}
2023-09-16 15:41:07 -07:00
return sample_value;
}
internal void
2023-09-24 19:37:05 -07:00
output_sound( EngineState* state, AudioBuffer* sound_buffer, GetSoundSampleValueFn* get_sample_value )
2023-09-16 15:41:07 -07:00
{
s16* sample_out = sound_buffer->Samples;
2023-09-20 21:26:23 -07:00
for ( s32 sample_index = 0; sample_index < sound_buffer->NumSamples; ++ sample_index )
2023-09-16 15:41:07 -07:00
{
2023-09-18 17:16:40 -07:00
s16 sample_value = get_sample_value( state, sound_buffer );
2023-09-17 18:20:11 -07:00
sound_buffer->RunningSampleIndex++;
// char ms_timing_debug[256] {};
// wsprintfA( ms_timing_debug, "sample_value: %d\n", sample_value );
// OutputDebugStringA( ms_timing_debug );
2023-09-16 15:41:07 -07:00
*sample_out = sample_value;
++ sample_out;
*sample_out = sample_value;
++ sample_out;
}
}
2023-09-15 18:35:27 -07:00
internal void
render_weird_graident(OffscreenBuffer* buffer, u32 x_offset, u32 y_offset )
{
// TODO(Ed): See if with optimizer if buffer should be passed by value.
struct Pixel {
u8 Blue;
u8 Green;
u8 Red;
u8 Alpha;
};
u8* row = rcast( u8*, buffer->Memory);
local_persist float wildcard = 0;
for ( u32 y = 0; y < buffer->Height; ++ y )
{
// u8* pixel = rcast(u8*, row);
// Pixel* pixel = rcast( Pixel*, row );
u32* pixel = rcast(u32*, row);
for ( u32 x = 0; x < buffer->Width; ++ x )
{
/* Pixel in memory:
-----------------------------------------------
Pixel + 0 Pixel + 1 Pixel + 2 Pixel + 3
RR GG GG XX
-----------------------------------------------
x86-64 : Little Endian Arch
0x XX BB GG RR
*/
#if 0
u8 blue = scast(u8, x + x_offset * u8(wildcard) % 256);
u8 green = scast(u8, y + y_offset - u8(wildcard) % 128);
u8 red = scast(u8, wildcard) % 256 - x * 0.4f;
#else
u8 blue = scast(u8, x + x_offset);
u8 green = scast(u8, y + y_offset);
u8 red = 0;
#endif
2023-09-26 22:16:41 -07:00
*pixel++ = u32(red << 16) | u32(green/2 << 16) | blue/2 << 0;
2023-09-15 18:35:27 -07:00
}
wildcard += 0.5375f;
row += buffer->Pitch;
}
}
2023-09-26 14:26:35 -07:00
Engine_API
void on_module_reload( Memory* memory, platform::ModuleAPI* platfom_api )
2023-09-17 18:20:11 -07:00
{
2023-09-18 17:16:40 -07:00
}
2023-09-26 14:26:35 -07:00
Engine_API
void startup( Memory* memory, platform::ModuleAPI* platform_api )
2023-09-18 17:16:40 -07:00
{
2023-09-26 14:26:35 -07:00
EngineState* state = rcast( EngineState*, memory->Persistent );
assert( sizeof(EngineState) <= memory->PersistentSize );
2023-09-18 17:16:40 -07:00
2023-09-26 14:26:35 -07:00
state->ToneVolume = 1000;
2023-09-17 18:20:11 -07:00
2023-09-26 14:26:35 -07:00
state->XOffset = 0;
state->YOffset = 0;
2023-09-17 18:20:11 -07:00
2023-09-26 14:26:35 -07:00
state->SampleWaveSwitch = false;
state->WaveToneHz = 120;
state->SampleWaveSineTime = 0.f;
2023-09-17 18:20:11 -07:00
2023-09-26 14:26:35 -07:00
state->RendererPaused = false;
2023-09-21 23:16:40 -07:00
2023-09-26 14:26:35 -07:00
#if Build_Debug && 0
{
using namespace platform;
2023-09-18 17:16:40 -07:00
2023-09-26 14:26:35 -07:00
char const* file_path = __FILE__;
Debug_FileContent file_content = platform_api->debug_file_read_content( file_path );
if ( file_content.Size )
2023-09-20 11:43:55 -07:00
{
2023-09-26 14:26:35 -07:00
platform_api->debug_file_write_content( "test.out", file_content.Size, file_content.Data );
platform_api->debug_file_free_content( & file_content );
2023-09-20 11:43:55 -07:00
}
2023-09-26 14:26:35 -07:00
}
#endif
}
Engine_API
void shutdown( Memory* memory, platform::ModuleAPI* platform_api )
{
}
Engine_API
// TODO : I rather expose the back_buffer and sound_buffer using getters for access in any function.
void update_and_render( InputState* input, OffscreenBuffer* back_buffer, Memory* memory, platform::ModuleAPI* platform_api )
{
EngineState* state = rcast( EngineState*, memory->Persistent );
assert( sizeof(EngineState) <= memory->PersistentSize );
2023-09-17 18:20:11 -07:00
ControllerState* controller = & input->Controllers[0];
2023-09-22 12:13:18 -07:00
// Abstracting the actionables as booleans and processing within this scope
2023-09-21 23:16:40 -07:00
// for now until proper callbacks for input bindings are setup.
b32 move_up = false;
b32 move_down = false;
b32 move_left = false;
b32 move_right = false;
2023-09-22 12:13:18 -07:00
2023-09-21 23:16:40 -07:00
b32 action_up = false;
b32 action_down = false;
b32 action_left = false;
b32 action_right = false;
2023-09-22 12:13:18 -07:00
2023-09-21 23:16:40 -07:00
b32 raise_volume = false;
b32 lower_volume = false;
b32 raise_tone_hz = false;
b32 lower_tone_hz = false;
2023-09-22 12:13:18 -07:00
2023-09-21 23:16:40 -07:00
b32 toggle_wave_tone = false;
2023-09-17 18:20:11 -07:00
2023-09-24 19:37:05 -07:00
b32 pause_renderer = false;
2023-09-22 12:13:18 -07:00
f32 analog_threshold = 0.5f;
2023-09-24 19:37:05 -07:00
#define pressed( btn ) (btn.EndedDown && btn.HalfTransitions < 1)
2023-09-17 18:20:11 -07:00
if ( controller->DSPad )
{
DualsensePadState* pad = controller->DSPad;
2023-09-22 12:13:18 -07:00
2023-09-21 23:16:40 -07:00
move_right |= pad->DPad.Right.EndedDown || pad->Stick.Left.X.End > analog_threshold;
move_left |= pad->DPad.Left.EndedDown || pad->Stick.Left.X.End < -analog_threshold;
move_up |= pad->DPad.Up.EndedDown || pad->Stick.Left.Y.End > analog_threshold;
move_down |= pad->DPad.Down.EndedDown || pad->Stick.Left.Y.End < -analog_threshold;
2023-09-22 12:13:18 -07:00
2023-09-21 23:16:40 -07:00
raise_volume |= pad->Triangle.EndedDown;
lower_volume |= pad->Circle.EndedDown;
2023-09-22 12:13:18 -07:00
2023-09-21 23:16:40 -07:00
raise_tone_hz |= pad->Square.EndedDown;
lower_tone_hz |= pad->X.EndedDown;
2023-09-22 12:13:18 -07:00
2023-09-24 19:37:05 -07:00
toggle_wave_tone |= pressed( pad->Options );
2023-09-17 18:20:11 -07:00
}
2023-09-20 21:26:23 -07:00
if ( controller->XPad )
2023-09-17 18:20:11 -07:00
{
XInputPadState* pad = controller->XPad;
2023-09-22 12:13:18 -07:00
2023-09-21 23:16:40 -07:00
move_right |= pad->DPad.Right.EndedDown || pad->Stick.Left.X.End > analog_threshold;
move_left |= pad->DPad.Left.EndedDown || pad->Stick.Left.X.End < -analog_threshold;
move_up |= pad->DPad.Up.EndedDown || pad->Stick.Left.Y.End > analog_threshold;
move_down |= pad->DPad.Down.EndedDown || pad->Stick.Left.Y.End < -analog_threshold;
2023-09-22 12:13:18 -07:00
2023-09-21 23:16:40 -07:00
raise_volume |= pad->Y.EndedDown;
lower_volume |= pad->B.EndedDown;
2023-09-22 12:13:18 -07:00
2023-09-21 23:16:40 -07:00
raise_tone_hz |= pad->X.EndedDown;
lower_tone_hz |= pad->A.EndedDown;
2023-09-22 12:13:18 -07:00
2023-09-24 19:37:05 -07:00
toggle_wave_tone |= pressed( pad->Start );
2023-09-17 18:20:11 -07:00
}
2023-09-20 21:26:23 -07:00
if ( controller->Keyboard )
{
KeyboardState* keyboard = controller->Keyboard;
2023-09-22 12:13:18 -07:00
2023-09-21 23:16:40 -07:00
move_right |= keyboard->D.EndedDown;
move_left |= keyboard->A.EndedDown;
move_up |= keyboard->W.EndedDown;
move_down |= keyboard->S.EndedDown;
2023-09-22 12:13:18 -07:00
2023-09-21 23:16:40 -07:00
raise_volume |= keyboard->Up.EndedDown;
lower_volume |= keyboard->Down.EndedDown;
2023-09-22 12:13:18 -07:00
2023-09-21 23:16:40 -07:00
raise_tone_hz |= keyboard->Right.EndedDown;
lower_tone_hz |= keyboard->Left.EndedDown;
2023-09-22 12:13:18 -07:00
2023-09-24 19:37:05 -07:00
pause_renderer |= pressed( keyboard->Pause );
toggle_wave_tone |= pressed( keyboard->Space );
2023-09-21 23:16:40 -07:00
}
2023-09-22 12:13:18 -07:00
2023-09-26 14:26:35 -07:00
state->XOffset += 3 * move_right;
state->XOffset -= 3 * move_left;
state->YOffset += 3 * move_down;
state->YOffset -= 3 * move_up;
2023-09-20 21:26:23 -07:00
2023-09-21 23:16:40 -07:00
if ( raise_volume )
{
state->ToneVolume += 10;
}
if ( lower_volume )
{
state->ToneVolume -= 10;
2023-09-24 19:37:05 -07:00
if ( state->ToneVolume <= 0 )
state->ToneVolume = 0;
2023-09-21 23:16:40 -07:00
}
2023-09-20 21:26:23 -07:00
2023-09-21 23:16:40 -07:00
if ( raise_tone_hz )
{
state->WaveToneHz += 1;
}
if ( lower_tone_hz )
{
state->WaveToneHz -= 1;
2023-09-24 19:37:05 -07:00
if ( state->WaveToneHz <= 0 )
state->WaveToneHz = 1;
2023-09-21 23:16:40 -07:00
}
2023-09-20 21:26:23 -07:00
2023-09-21 23:16:40 -07:00
if ( toggle_wave_tone )
{
2023-09-26 14:26:35 -07:00
state->SampleWaveSwitch ^= true;
2023-09-24 19:37:05 -07:00
}
2023-09-26 14:26:35 -07:00
render_weird_graident( back_buffer, state->XOffset, state->YOffset );
2023-09-24 19:37:05 -07:00
if ( pause_renderer )
{
2023-09-26 14:26:35 -07:00
if ( state->RendererPaused )
2023-09-24 19:37:05 -07:00
{
2023-09-26 14:26:35 -07:00
platform_api->debug_set_pause_rendering(false);
state->RendererPaused = false;
2023-09-24 19:37:05 -07:00
}
else
{
2023-09-26 14:26:35 -07:00
platform_api->debug_set_pause_rendering(true);
state->RendererPaused = true;
2023-09-24 19:37:05 -07:00
}
2023-09-20 21:26:23 -07:00
}
2023-09-24 19:37:05 -07:00
}
2023-09-26 14:26:35 -07:00
Engine_API
void update_audio( AudioBuffer* audio_buffer, Memory* memory, platform::ModuleAPI* platform_api )
2023-09-24 19:37:05 -07:00
{
EngineState* state = rcast( EngineState*, memory->Persistent );
do_once_start
do_once_end
2023-09-17 18:20:11 -07:00
2023-09-16 15:41:07 -07:00
// TODO(Ed) : Allow sample offsets here for more robust platform options
2023-09-26 14:26:35 -07:00
if ( ! state->SampleWaveSwitch )
2023-09-24 19:37:05 -07:00
output_sound( state, audio_buffer, sine_wave_sample_value );
2023-09-17 18:20:11 -07:00
else
2023-09-24 19:37:05 -07:00
output_sound( state, audio_buffer, square_wave_sample_value );
2023-09-15 18:35:27 -07:00
}
NS_ENGINE_END