2023-09-23 18:03:33 -07:00
|
|
|
//#include "win32.h"
|
2023-09-26 14:26:35 -07:00
|
|
|
#include "engine.hpp"
|
2023-09-28 18:21:05 -07:00
|
|
|
#include "engine_to_platform_api.hpp"
|
2023-09-26 14:26:35 -07:00
|
|
|
#include "handmade.hpp"
|
2023-09-15 18:35:27 -07:00
|
|
|
|
|
|
|
NS_ENGINE_BEGIN
|
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
#define pressed( btn ) (btn.ended_down && btn.half_transitions > 0)
|
2023-09-28 10:44:43 -07:00
|
|
|
|
|
|
|
// Used to determine if analog input is at move threshold
|
|
|
|
constexpr f32 analog__move_threshold = 0.5f;
|
|
|
|
|
|
|
|
struct EngineActions
|
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
b32 move_up;
|
|
|
|
b32 move_down;
|
|
|
|
b32 move_left;
|
|
|
|
b32 move_right;
|
2023-09-28 10:44:43 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
b32 loop_mode_engine;
|
|
|
|
b32 loop_mode_game;
|
2023-09-28 10:44:43 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
b32 raise_volume;
|
|
|
|
b32 lower_volume;
|
|
|
|
b32 raise_tone_hz;
|
|
|
|
b32 lower_tone_hz;
|
2023-09-28 10:44:43 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
b32 toggle_wave_tone;
|
2023-09-28 10:44:43 -07:00
|
|
|
|
2023-09-28 12:41:30 -07:00
|
|
|
#if Build_Development
|
2023-09-30 07:05:37 -07:00
|
|
|
b32 pause_renderer;
|
2023-09-28 12:41:30 -07:00
|
|
|
#endif
|
2023-09-28 10:44:43 -07:00
|
|
|
};
|
|
|
|
|
2023-09-18 17:16:40 -07:00
|
|
|
struct EngineState
|
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
s32 wave_tone_hz;
|
|
|
|
s32 tone_volume;
|
|
|
|
s32 x_offset;
|
|
|
|
s32 y_offset;
|
2023-09-28 10:44:43 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
b32 renderer_paused;
|
2023-09-28 10:44:43 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
f32 sample_wave_sine_time;
|
|
|
|
b32 sample_wave_switch;
|
2023-09-28 10:44:43 -07:00
|
|
|
|
|
|
|
hh::Memory game_memory;
|
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-30 07:05:37 -07:00
|
|
|
s32 wave_period = sound_buffer->samples_per_second / state->wave_tone_hz;
|
2023-09-24 19:37:05 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
s32 sample_value = (sound_buffer->running_sample_index / (wave_period / 2) ) % 2 ?
|
|
|
|
state->tone_volume : - state->tone_volume;
|
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-30 07:05:37 -07:00
|
|
|
f32& time = state->sample_wave_sine_time;
|
2023-09-16 15:41:07 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
s32 wave_period = sound_buffer->samples_per_second / state->wave_tone_hz;
|
2023-09-24 19:37:05 -07:00
|
|
|
|
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-30 07:05:37 -07:00
|
|
|
s16 sample_value = scast(s16, sine_value * scast(f32, state->tone_volume));
|
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
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
s16* sample_out = sound_buffer->samples;
|
|
|
|
for ( s32 sample_index = 0; sample_index < sound_buffer->num_samples; ++ 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-30 07:05:37 -07:00
|
|
|
sound_buffer->running_sample_index++;
|
2023-09-17 18:20:11 -07:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
};
|
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
u8* row = rcast( u8*, buffer->memory);
|
2023-09-15 18:35:27 -07:00
|
|
|
local_persist float wildcard = 0;
|
2023-09-30 07:05:37 -07:00
|
|
|
for ( u32 y = 0; y < buffer->height; ++ y )
|
2023-09-15 18:35:27 -07:00
|
|
|
{
|
|
|
|
// u8* pixel = rcast(u8*, row);
|
|
|
|
// Pixel* pixel = rcast( Pixel*, row );
|
|
|
|
u32* pixel = rcast(u32*, row);
|
2023-09-30 07:05:37 -07:00
|
|
|
for ( u32 x = 0; x < buffer->width; ++ x )
|
2023-09-15 18:35:27 -07:00
|
|
|
{
|
|
|
|
/* 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
|
2023-09-30 07:05:37 -07:00
|
|
|
u8 red = scast(u8, y + y_offset);
|
|
|
|
u8 green = scast(u8, x + x_offset);
|
|
|
|
u8 blue = scast(u8, x + y_offset) - scast(u8, y + y_offset);
|
2023-09-29 12:58:18 -07:00
|
|
|
// blue *= 2;
|
2023-09-15 18:35:27 -07:00
|
|
|
#endif
|
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
*pixel++ = u32(red/2 << 16) | u32(green/6 << 0) | blue/2 << 0;
|
2023-09-15 18:35:27 -07:00
|
|
|
}
|
|
|
|
wildcard += 0.5375f;
|
2023-09-30 07:05:37 -07:00
|
|
|
row += buffer->pitch;
|
2023-09-15 18:35:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 10:44:43 -07:00
|
|
|
internal void
|
|
|
|
render_player( OffscreenBuffer* buffer, s32 pos_x, s32 pos_y )
|
2023-09-17 18:20:11 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
u8* end_of_buffer = rcast(u8*, buffer->memory)
|
|
|
|
- buffer->bytes_per_pixel * buffer->width
|
|
|
|
+ buffer->pitch * buffer->height;
|
2023-09-28 10:44:43 -07:00
|
|
|
|
|
|
|
s32 top = pos_y;
|
|
|
|
s32 bottom = pos_y + 10;
|
|
|
|
|
|
|
|
u32 color = 0xFFFFFFFF;
|
|
|
|
|
|
|
|
for ( s32 coord_x = pos_x; coord_x < (pos_x+ 10); ++ coord_x )
|
|
|
|
{
|
|
|
|
u8*
|
2023-09-30 07:05:37 -07:00
|
|
|
pixel_byte = rcast(u8*, buffer->memory);
|
|
|
|
pixel_byte += coord_x * buffer->bytes_per_pixel;
|
|
|
|
pixel_byte += top * buffer->pitch;
|
2023-09-17 18:20:11 -07:00
|
|
|
|
2023-09-28 10:44:43 -07:00
|
|
|
for ( s32 coord_y = top; coord_y < bottom; ++ coord_y )
|
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
if ( pixel_byte < buffer->memory || pixel_byte >= end_of_buffer )
|
2023-09-28 10:44:43 -07:00
|
|
|
continue;
|
|
|
|
|
|
|
|
s32* pixel = rcast(s32*, pixel_byte);
|
|
|
|
*pixel = color;
|
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
|
|
|
|
|
|
|
|
pixel_byte += buffer->pitch;
|
2023-09-28 10:44:43 -07:00
|
|
|
}
|
|
|
|
}
|
2023-09-18 17:16:40 -07:00
|
|
|
}
|
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
using SnapshotFn = void ( s32 slot, Memory* memory, platform::ModuleAPI* platform_api );
|
|
|
|
|
|
|
|
internal
|
|
|
|
void load_engine_snapshot( s32 slot, Memory* memory, platform::ModuleAPI* platform_api )
|
|
|
|
{
|
|
|
|
platform_api->memory_copy( memory->persistent, memory->total_size(), memory->snapshots[ slot ].memory );
|
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
|
|
|
void load_game_snapshot( s32 slot, Memory* memory, platform::ModuleAPI* platform_api )
|
|
|
|
{
|
|
|
|
EngineState* state = rcast( EngineState*, memory->persistent );
|
|
|
|
|
|
|
|
void* persistent_slot = memory->snapshots[ slot ].memory;
|
|
|
|
void* transient_slot = rcast( Byte*, memory->snapshots[ slot ].memory ) + state->game_memory.persistent_size;
|
|
|
|
|
|
|
|
platform_api->memory_copy( state->game_memory.persistent, state->game_memory.persistent_size, persistent_slot );
|
|
|
|
platform_api->memory_copy( state->game_memory.transient, state->game_memory.transient_size, transient_slot );
|
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
|
|
|
void take_engine_snapshot( s32 slot, Memory* memory, platform::ModuleAPI* platform_api )
|
|
|
|
{
|
|
|
|
platform_api->memory_copy( memory->snapshots[ slot ].memory, memory->total_size(), memory->persistent );
|
|
|
|
}
|
|
|
|
|
2023-09-28 10:44:43 -07:00
|
|
|
internal
|
2023-09-30 07:05:37 -07:00
|
|
|
void take_game_snapshot( s32 slot, Memory* memory, platform::ModuleAPI* platform_api )
|
|
|
|
{
|
|
|
|
EngineState* state = rcast( EngineState*, memory->persistent );
|
|
|
|
|
|
|
|
void* persistent_slot = memory->snapshots[ slot ].memory;
|
|
|
|
void* transient_slot = rcast( Byte*, memory->snapshots[ slot ].memory ) + state->game_memory.persistent_size;
|
|
|
|
|
|
|
|
platform_api->memory_copy( persistent_slot, state->game_memory.persistent_size, state->game_memory.persistent );
|
|
|
|
platform_api->memory_copy( transient_slot, state->game_memory.transient_size, state->game_memory.transient );
|
|
|
|
}
|
|
|
|
|
|
|
|
#define Snapshot_New_Way 1
|
|
|
|
internal
|
|
|
|
void begin_recording_input( Memory* memory, InputState* input, platform::ModuleAPI* platform_api )
|
2023-09-18 17:16:40 -07:00
|
|
|
{
|
2023-09-29 12:58:18 -07:00
|
|
|
Str file_name = str_ascii("test_input.hmi");
|
|
|
|
StrPath file_path = {};
|
|
|
|
file_path.concat( platform_api->path_scratch, file_name );
|
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
memory->active_recording_file.path = file_path;
|
|
|
|
memory->input_recording_index = 1;
|
2023-09-18 17:16:40 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
#if Snapshot_New_Way
|
|
|
|
platform_api->file_delete( memory->active_recording_file.path );
|
|
|
|
#else
|
2023-09-28 10:44:43 -07:00
|
|
|
platform_api->file_write_content( & state->ActiveInputRecordingFile, scast(u32, state->game_memory.PersistentSize), state->game_memory.Persistent );
|
2023-09-30 07:05:37 -07:00
|
|
|
#endif
|
2023-09-28 10:44:43 -07:00
|
|
|
}
|
2023-09-17 18:20:11 -07:00
|
|
|
|
2023-09-28 10:44:43 -07:00
|
|
|
internal
|
2023-09-30 07:05:37 -07:00
|
|
|
void end_recording_input( Memory* memory, InputState* input, platform::ModuleAPI* platform_api )
|
2023-09-28 10:44:43 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
memory->input_recording_index = 0;
|
|
|
|
platform_api->file_close( & memory->active_recording_file );
|
2023-09-28 10:44:43 -07:00
|
|
|
}
|
2023-09-17 18:20:11 -07:00
|
|
|
|
2023-09-28 10:44:43 -07:00
|
|
|
internal
|
2023-09-30 07:05:37 -07:00
|
|
|
void begin_playback_input( Memory* memory, InputState* input, platform::ModuleAPI* platform_api )
|
2023-09-28 10:44:43 -07:00
|
|
|
{
|
|
|
|
Str file_name = str_ascii("test_input.hmi");
|
2023-09-29 12:58:18 -07:00
|
|
|
StrPath file_path = {};
|
|
|
|
file_path.concat( platform_api->path_scratch, file_name );
|
|
|
|
if ( platform_api->file_check_exists( file_path ) )
|
2023-09-28 10:44:43 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
memory->active_playback_file.path = file_path;
|
|
|
|
memory->input_playback_index = 1;
|
2023-09-28 10:44:43 -07:00
|
|
|
}
|
2023-09-17 18:20:11 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
#if Snapshot_New_Way
|
|
|
|
#else
|
2023-09-28 10:44:43 -07:00
|
|
|
if ( state->ActiveInputRecordingFile.OpaqueHandle == nullptr )
|
|
|
|
{
|
|
|
|
platform_api->file_read_stream( & state->ActivePlaybackFile, scast(u32, state->game_memory.PersistentSize), state->game_memory.Persistent );
|
|
|
|
}
|
2023-09-30 07:05:37 -07:00
|
|
|
#endif
|
2023-09-28 10:44:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-09-30 07:05:37 -07:00
|
|
|
void end_playback_input( Memory* memory, InputState* input, platform::ModuleAPI* platform_api )
|
2023-09-28 10:44:43 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
memory->input_playback_index = 0;
|
|
|
|
|
|
|
|
#if Snapshot_New_Way
|
|
|
|
#else
|
2023-09-28 10:44:43 -07:00
|
|
|
platform_api->file_rewind( & state->ActivePlaybackFile );
|
|
|
|
platform_api->file_read_stream( & state->ActivePlaybackFile, scast(u32, state->game_memory.PersistentSize), state->game_memory.Persistent );
|
2023-09-30 07:05:37 -07:00
|
|
|
#endif
|
2023-09-28 10:44:43 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
platform_api->file_close( & memory->active_playback_file );
|
2023-09-28 10:44:43 -07:00
|
|
|
}
|
2023-09-21 23:16:40 -07:00
|
|
|
|
2023-09-28 10:44:43 -07:00
|
|
|
InputStateSnapshot input_state_snapshot( InputState* input )
|
|
|
|
{
|
|
|
|
InputStateSnapshot snapshot = {};
|
2023-09-30 07:05:37 -07:00
|
|
|
for ( s32 idx = 0; idx < array_count( snapshot.controllers ); ++ idx )
|
2023-09-26 14:26:35 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
ControllerState* controller = & input->controllers[idx];
|
2023-09-28 10:44:43 -07:00
|
|
|
if ( controller == nullptr )
|
|
|
|
continue;
|
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
if ( controller->ds_pad )
|
|
|
|
snapshot.controllers[idx].ds_pad = *controller->ds_pad;
|
2023-09-28 10:44:43 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
if ( controller->xpad )
|
|
|
|
snapshot.controllers[idx].xpad = *controller->xpad;
|
2023-09-18 17:16:40 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
if ( controller->keyboard )
|
2023-09-20 11:43:55 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
snapshot.controllers[idx].keyboard = *controller->keyboard;
|
2023-09-20 11:43:55 -07:00
|
|
|
}
|
2023-09-28 10:44:43 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
if ( controller->mouse )
|
|
|
|
snapshot.controllers[idx].mouse = *controller->mouse;
|
2023-09-26 14:26:35 -07:00
|
|
|
}
|
2023-09-28 10:44:43 -07:00
|
|
|
return snapshot;
|
2023-09-26 14:26:35 -07:00
|
|
|
}
|
|
|
|
|
2023-09-28 10:44:43 -07:00
|
|
|
internal
|
2023-09-30 07:05:37 -07:00
|
|
|
void record_input( Memory* memory, InputState* input, platform::ModuleAPI* platform_api )
|
2023-09-26 14:26:35 -07:00
|
|
|
{
|
2023-09-28 10:44:43 -07:00
|
|
|
InputStateSnapshot snapshot = input_state_snapshot( input );
|
2023-09-30 07:05:37 -07:00
|
|
|
if ( platform_api->file_write_stream( & memory->active_recording_file, sizeof(snapshot), &snapshot ) == 0 )
|
2023-09-28 10:44:43 -07:00
|
|
|
{
|
|
|
|
// TODO(Ed) : Logging
|
|
|
|
}
|
2023-09-26 14:26:35 -07:00
|
|
|
}
|
|
|
|
|
2023-09-28 10:44:43 -07:00
|
|
|
internal
|
2023-09-30 07:05:37 -07:00
|
|
|
void play_input( SnapshotFn* load_snapshot, Memory* memory, InputState* input, platform::ModuleAPI* platform_api )
|
2023-09-26 14:26:35 -07:00
|
|
|
{
|
2023-09-28 10:44:43 -07:00
|
|
|
InputStateSnapshot new_input;
|
2023-09-30 07:05:37 -07:00
|
|
|
if ( platform_api->file_read_stream( & memory->active_playback_file, sizeof(InputStateSnapshot), & new_input ) == 0 )
|
2023-09-28 10:44:43 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
end_playback_input( memory, input, platform_api );
|
|
|
|
load_snapshot( memory->active_snapshot_slot, memory, platform_api );
|
|
|
|
begin_playback_input( memory, input, platform_api );
|
2023-09-28 10:44:43 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
for ( s32 idx = 0; idx < array_count( new_input.controllers ); ++ idx )
|
2023-09-28 10:44:43 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
ControllerState* controller = & input->controllers[idx];
|
2023-09-28 10:44:43 -07:00
|
|
|
if ( controller == nullptr )
|
|
|
|
continue;
|
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
if ( controller->ds_pad )
|
|
|
|
*controller->ds_pad = new_input.controllers[idx].ds_pad;
|
2023-09-28 10:44:43 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
if ( controller->xpad )
|
|
|
|
*controller->xpad = new_input.controllers[idx].xpad;
|
2023-09-28 10:44:43 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
if ( controller->keyboard )
|
2023-09-28 10:44:43 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
*controller->keyboard = new_input.controllers[idx].keyboard;
|
2023-09-28 10:44:43 -07:00
|
|
|
}
|
2023-09-17 18:20:11 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
if ( controller->mouse )
|
|
|
|
*controller->mouse = new_input.controllers[idx].mouse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void process_loop_mode( SnapshotFn* take_snapshot, SnapshotFn* load_snapshot
|
|
|
|
, Memory* memory, EngineState* state, InputState* input, platform::ModuleAPI* platform_api )
|
|
|
|
{
|
|
|
|
if ( memory->input_recording_index == 0 && memory->input_playback_index == 0 )
|
|
|
|
{
|
|
|
|
take_snapshot( 0, memory, platform_api );
|
|
|
|
memory->active_snapshot_slot = 0;
|
|
|
|
begin_recording_input( memory, input, platform_api );
|
|
|
|
}
|
|
|
|
else if ( memory->input_playback_index )
|
|
|
|
{
|
|
|
|
end_playback_input( memory, input, platform_api );
|
|
|
|
load_snapshot( memory->active_snapshot_slot, memory, platform_api );
|
|
|
|
}
|
|
|
|
else if ( memory->input_recording_index )
|
|
|
|
{
|
|
|
|
end_recording_input( memory, input, platform_api );
|
|
|
|
load_snapshot( memory->active_snapshot_slot, memory, platform_api );
|
|
|
|
begin_playback_input( memory, input, platform_api );
|
2023-09-28 10:44:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
|
|
|
void input_poll_engine_actions( InputState* input, EngineActions* actions )
|
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
ControllerState* controller = & input->controllers[0];
|
|
|
|
KeyboardState* keyboard = controller->keyboard;
|
2023-09-22 12:13:18 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
// actions->move_right |= keyboard->D.EndedDown;
|
|
|
|
// actions->move_left |= keyboard->A.EndedDown;
|
|
|
|
// actions->move_up |= keyboard->W.EndedDown;
|
|
|
|
// actions->move_down |= keyboard->S.EndedDown;
|
2023-09-22 12:13:18 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
actions->raise_volume |= keyboard->up.ended_down;
|
|
|
|
actions->lower_volume |= keyboard->down.ended_down;
|
2023-09-22 12:13:18 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
actions->raise_tone_hz |= keyboard->right.ended_down;
|
|
|
|
actions->lower_tone_hz |= keyboard->left.ended_down;
|
2023-09-22 12:13:18 -07:00
|
|
|
|
2023-09-28 12:41:30 -07:00
|
|
|
#if Build_Development
|
2023-09-30 07:05:37 -07:00
|
|
|
actions->pause_renderer |= pressed( keyboard->pause );
|
2023-09-28 12:41:30 -07:00
|
|
|
#endif
|
2023-09-17 18:20:11 -07:00
|
|
|
|
2023-09-28 10:44:43 -07:00
|
|
|
actions->toggle_wave_tone |= pressed( keyboard->Q );
|
2023-09-24 19:37:05 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
actions->loop_mode_game |= pressed( keyboard->L ) && ! keyboard->right_shift.ended_down;
|
|
|
|
actions->loop_mode_engine |= pressed( keyboard->L ) && keyboard->right_shift.ended_down;
|
|
|
|
|
|
|
|
MousesState* mouse = controller->mouse;
|
|
|
|
|
|
|
|
actions->move_right = (mouse->horizontal_wheel.end > 0.f) * 20;
|
|
|
|
actions->move_left = (mouse->horizontal_wheel.end < 0.f) * 20;
|
|
|
|
|
|
|
|
actions->move_up = (mouse->vertical_wheel.end > 0.f) * 10;
|
|
|
|
actions->move_down = (mouse->vertical_wheel.end < 0.f) * 10;
|
2023-09-28 10:44:43 -07:00
|
|
|
}
|
2023-09-22 12:13:18 -07:00
|
|
|
|
2023-09-28 10:44:43 -07:00
|
|
|
internal
|
2023-09-30 07:05:37 -07:00
|
|
|
void input_poll_player_actions( InputState* input, hh::PlayerActions* actions )
|
2023-09-28 10:44:43 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
ControllerState* controller = & input->controllers[0];
|
2023-09-24 19:37:05 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
if ( controller->ds_pad )
|
2023-09-17 18:20:11 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
DualsensePadState* pad = controller->ds_pad;
|
2023-09-22 12:13:18 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
actions->jump |= pressed( pad->cross );
|
2023-09-22 12:13:18 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
actions->player_x_move_analog += pad->stick.left.X.end;
|
|
|
|
actions->player_y_move_analog += pad->stick.left.Y.end;
|
2023-09-17 18:20:11 -07:00
|
|
|
}
|
2023-09-30 07:05:37 -07:00
|
|
|
if ( controller->xpad )
|
2023-09-17 18:20:11 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
XInputPadState* pad = controller->xpad;
|
2023-09-22 12:13:18 -07:00
|
|
|
|
2023-09-28 10:44:43 -07:00
|
|
|
actions->jump |= pressed( pad->A );
|
2023-09-22 12:13:18 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
actions->player_x_move_analog += pad->stick.left.X.end;
|
|
|
|
actions->player_y_move_analog += pad->stick.left.Y.end;
|
2023-09-17 18:20:11 -07:00
|
|
|
}
|
2023-09-28 10:44:43 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
if ( controller->keyboard )
|
2023-09-20 21:26:23 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
KeyboardState* keyboard = controller->keyboard;
|
|
|
|
actions->jump |= pressed( keyboard->space );
|
2023-09-28 10:44:43 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
actions->player_x_move_digital += keyboard->D.ended_down - keyboard->A.ended_down;
|
|
|
|
actions->player_y_move_digital += keyboard->W.ended_down - keyboard->S.ended_down;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( controller->mouse )
|
|
|
|
{
|
|
|
|
MousesState* mouse = controller->mouse;
|
2023-09-28 10:44:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Engine_API
|
|
|
|
void on_module_reload( Memory* memory, platform::ModuleAPI* platfom_api )
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Engine_API
|
|
|
|
void startup( Memory* memory, platform::ModuleAPI* platform_api )
|
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
memory->active_snapshot_slot = -1;
|
|
|
|
memory->input_recording_index = 0;
|
|
|
|
memory->input_playback_index = 0;
|
|
|
|
memory->active_recording_file = {};
|
|
|
|
memory->active_playback_file = {};
|
|
|
|
memory->engine_loop_active = false;
|
|
|
|
memory->game_loop_active = false;
|
|
|
|
|
|
|
|
for ( s32 slot = 0; slot < memory->Num_Snapshot_Slots; ++ slot )
|
|
|
|
{
|
|
|
|
// TODO(Ed) : Specify default file paths for saving slots ?
|
|
|
|
}
|
2023-09-22 12:13:18 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
EngineState* state = rcast( EngineState*, memory->persistent );
|
|
|
|
assert( sizeof(EngineState) <= memory->persistent_size );
|
2023-09-22 12:13:18 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
state->tone_volume = 1000;
|
2023-09-22 12:13:18 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
state->x_offset = 0;
|
|
|
|
state->y_offset = 0;
|
2023-09-24 19:37:05 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
state->sample_wave_switch = false;
|
|
|
|
state->wave_tone_hz = 60;
|
|
|
|
state->sample_wave_sine_time = 0.f;
|
2023-09-22 12:13:18 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
state->renderer_paused = false;
|
|
|
|
state->game_memory.persistent_size = memory->persistent_size / 2;
|
|
|
|
state->game_memory.persistent = rcast(Byte*, memory->persistent) + state->game_memory.persistent_size;
|
|
|
|
state->game_memory.transient_size = memory->transient_size / 2;
|
|
|
|
state->game_memory.transient = rcast(Byte*, memory->transient) + state->game_memory.transient_size;
|
2023-09-20 21:26:23 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
hh::PlayerState* player = rcast( hh::PlayerState*, state->game_memory.persistent );
|
|
|
|
assert( sizeof(hh::PlayerState) <= state->game_memory.persistent_size );
|
2023-09-28 10:44:43 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
player->pos_x = 100;
|
|
|
|
player->pos_y = 100;
|
|
|
|
player->mid_jump = false;
|
|
|
|
player->jump_time = 0.f;
|
2023-09-28 10:44:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2023-09-30 07:05:37 -07:00
|
|
|
void update_and_render( InputState* input, OffscreenBuffer* back_buffer, Memory* memory, platform::ModuleAPI* platform_api, ThreadContext* thread )
|
2023-09-28 10:44:43 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
EngineState* state = rcast( EngineState*, memory->persistent );
|
|
|
|
assert( sizeof(EngineState) <= memory->persistent_size );
|
2023-09-28 10:44:43 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
ControllerState* controller = & input->controllers[0];
|
2023-09-28 10:44:43 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
EngineActions engine_actions {};
|
|
|
|
hh::PlayerActions player_actions {};
|
2023-09-28 10:44:43 -07:00
|
|
|
|
|
|
|
input_poll_engine_actions( input, & engine_actions );
|
2023-09-30 07:05:37 -07:00
|
|
|
|
|
|
|
// Ease of use: Allow user to press L key without shift if engine loop recording is active.
|
|
|
|
engine_actions.loop_mode_engine |= engine_actions.loop_mode_game && memory->engine_loop_active;
|
|
|
|
|
|
|
|
if ( engine_actions.loop_mode_engine && ! memory->game_loop_active )
|
|
|
|
{
|
|
|
|
process_loop_mode( & take_engine_snapshot, & load_engine_snapshot, memory, state, input, platform_api );
|
|
|
|
memory->engine_loop_active = memory->input_playback_index || memory->input_recording_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Input recording and playback for engine state
|
|
|
|
if ( memory->engine_loop_active )
|
|
|
|
{
|
|
|
|
if ( memory->input_recording_index )
|
|
|
|
{
|
|
|
|
record_input( memory, input, platform_api );
|
|
|
|
}
|
|
|
|
if ( memory->input_playback_index )
|
|
|
|
{
|
|
|
|
play_input( & load_engine_snapshot, memory, input, platform_api );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process Engine Actions
|
2023-09-21 23:16:40 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
state->x_offset += 3 * engine_actions.move_right;
|
|
|
|
state->x_offset -= 3 * engine_actions.move_left;
|
|
|
|
state->y_offset += 3 * engine_actions.move_down;
|
|
|
|
state->y_offset -= 3 * engine_actions.move_up;
|
2023-09-28 10:44:43 -07:00
|
|
|
|
|
|
|
if ( engine_actions.raise_volume )
|
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
state->tone_volume += 10;
|
2023-09-28 10:44:43 -07:00
|
|
|
}
|
|
|
|
if ( engine_actions.lower_volume )
|
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
state->tone_volume -= 10;
|
|
|
|
if ( state->tone_volume <= 0 )
|
|
|
|
state->tone_volume = 0;
|
2023-09-28 10:44:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( engine_actions.raise_tone_hz )
|
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
state->wave_tone_hz += 1;
|
2023-09-28 10:44:43 -07:00
|
|
|
}
|
|
|
|
if ( engine_actions.lower_tone_hz )
|
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
state->wave_tone_hz -= 1;
|
|
|
|
if ( state->wave_tone_hz <= 0 )
|
|
|
|
state->wave_tone_hz = 1;
|
2023-09-28 10:44:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( engine_actions.toggle_wave_tone )
|
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
state->sample_wave_switch ^= true;
|
2023-09-28 10:44:43 -07:00
|
|
|
}
|
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
if ( engine_actions.loop_mode_game && ! memory->engine_loop_active )
|
2023-09-28 10:44:43 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
process_loop_mode( & take_game_snapshot, & load_game_snapshot, memory, state, input, platform_api );
|
|
|
|
memory->game_loop_active = memory->input_playback_index || memory->input_recording_index;
|
2023-09-28 10:44:43 -07:00
|
|
|
}
|
|
|
|
|
2023-09-28 12:41:30 -07:00
|
|
|
#if Build_Development
|
2023-09-28 10:44:43 -07:00
|
|
|
if ( engine_actions.pause_renderer )
|
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
if ( state->renderer_paused )
|
2023-09-28 10:44:43 -07:00
|
|
|
{
|
|
|
|
platform_api->debug_set_pause_rendering(false);
|
2023-09-30 07:05:37 -07:00
|
|
|
state->renderer_paused = false;
|
2023-09-28 10:44:43 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
platform_api->debug_set_pause_rendering(true);
|
2023-09-30 07:05:37 -07:00
|
|
|
state->renderer_paused = true;
|
2023-09-28 10:44:43 -07:00
|
|
|
}
|
|
|
|
}
|
2023-09-28 12:41:30 -07:00
|
|
|
#endif
|
2023-09-21 23:16:40 -07:00
|
|
|
}
|
2023-09-20 21:26:23 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
if ( ! memory->engine_loop_active )
|
2023-09-21 23:16:40 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
// Input recording and playback for game state
|
|
|
|
if ( memory->input_recording_index )
|
|
|
|
{
|
|
|
|
record_input( memory, input, platform_api );
|
|
|
|
}
|
|
|
|
if ( memory->input_playback_index )
|
|
|
|
{
|
|
|
|
play_input( & load_game_snapshot, memory, input, platform_api );
|
|
|
|
}
|
2023-09-21 23:16:40 -07:00
|
|
|
}
|
2023-09-20 21:26:23 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
hh::PlayerState* player = rcast( hh::PlayerState*, state->game_memory.persistent );
|
|
|
|
assert( sizeof(hh::PlayerState) <= state->game_memory.persistent_size );
|
2023-09-24 19:37:05 -07:00
|
|
|
|
2023-09-28 10:44:43 -07:00
|
|
|
input_poll_player_actions( input, & player_actions );
|
2023-09-24 19:37:05 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
player->pos_x += player_actions.player_x_move_digital * 5;
|
|
|
|
player->pos_y -= player_actions.player_y_move_digital * 5;
|
|
|
|
player->pos_x += scast(u32, player_actions.player_x_move_analog * 5);
|
|
|
|
player->pos_y -= scast(u32, player_actions.player_y_move_analog * 5) - scast(u32, sinf( player->jump_time * TAU ) * 10);
|
2023-09-28 10:44:43 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
if ( player->jump_time > 0.f )
|
2023-09-24 19:37:05 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
player->jump_time -= 0.025f;
|
2023-09-24 19:37:05 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
player->jump_time = 0.f;
|
|
|
|
player->mid_jump = false;
|
2023-09-28 10:44:43 -07:00
|
|
|
}
|
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
if ( ! player->mid_jump && player_actions.jump )
|
2023-09-28 10:44:43 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
player->jump_time = 1.f;
|
|
|
|
player->mid_jump = true;
|
2023-09-24 19:37:05 -07:00
|
|
|
}
|
2023-09-20 21:26:23 -07:00
|
|
|
}
|
2023-09-28 10:44:43 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
render_weird_graident( back_buffer, state->x_offset, state->y_offset );
|
|
|
|
render_player( back_buffer, player->pos_x, player->pos_y );
|
|
|
|
|
|
|
|
if ( memory->input_recording_index )
|
|
|
|
render_player( back_buffer, player->pos_x + 20, player->pos_y - 20 );
|
|
|
|
|
|
|
|
render_player( back_buffer, (s32)input->controllers[0].mouse->X.end, (s32)input->controllers[0].mouse->Y.end );
|
2023-09-28 10:44:43 -07:00
|
|
|
|
2023-09-30 07:05:37 -07:00
|
|
|
// Mouse buttons test
|
|
|
|
{
|
|
|
|
if ( input->controllers[0].mouse->left.ended_down == true )
|
|
|
|
render_player( back_buffer, 5, 5 );
|
|
|
|
|
|
|
|
if ( input->controllers[0].mouse->middle.ended_down == true )
|
|
|
|
|
|
|
|
render_player( back_buffer, 5, 20 );
|
|
|
|
|
|
|
|
if ( input->controllers[0].mouse->right.ended_down == true )
|
|
|
|
render_player( back_buffer, 5, 35 );
|
|
|
|
}
|
2023-09-24 19:37:05 -07:00
|
|
|
}
|
|
|
|
|
2023-09-26 14:26:35 -07:00
|
|
|
Engine_API
|
2023-09-30 07:05:37 -07:00
|
|
|
void update_audio( AudioBuffer* audio_buffer, Memory* memory, platform::ModuleAPI* platform_api, ThreadContext* thread )
|
2023-09-24 19:37:05 -07:00
|
|
|
{
|
2023-09-30 07:05:37 -07:00
|
|
|
EngineState* state = rcast( EngineState*, memory->persistent );
|
2023-09-24 19:37:05 -07:00
|
|
|
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-30 07:05:37 -07:00
|
|
|
if ( ! state->sample_wave_switch )
|
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
|
2023-09-28 10:44:43 -07:00
|
|
|
|
|
|
|
#undef pressed
|