Shall not pass 80 characters in width.

This commit is contained in:
2026-03-22 11:34:06 -07:00
parent 8af1c59bb6
commit b6f7e027b6
5 changed files with 589 additions and 229 deletions
+68 -32
View File
@@ -4,17 +4,27 @@
#include <math.h>
static void GameOutputSound(game_state *GameState, game_sound_output_buffer *SoundBuffer, S32 ToneHz)
static void GameOutputSound(game_state *GameState,
game_sound_output_buffer *SoundBuffer,
S32 ToneHz)
{
S16 ToneVolume = 8000;
S32 WavePeriod = SoundBuffer->SamplesPerSecond / ToneHz;
S16 ToneVolume, *SampleOut;
S32 WavePeriod, SampleIndex;
S16 *SampleOut = SoundBuffer->Samples;
ToneVolume = 8000;
WavePeriod = SoundBuffer->SamplesPerSecond / ToneHz;
S32 SampleIndex;
for(SampleIndex = 0; SampleIndex < SoundBuffer->SampleCount; SampleIndex++) {
F32 SineValue = sinf(GameState->tSine);
S16 SampleValue = (S16)(SineValue * ToneVolume);
SampleOut = SoundBuffer->Samples;
for(SampleIndex = 0;
SampleIndex < SoundBuffer->SampleCount;
SampleIndex++)
{
F32 SineValue;
S16 SampleValue;
SineValue = sinf(GameState->tSine);
SampleValue = (S16)(SineValue * ToneVolume);
*SampleOut++ = SampleValue;
*SampleOut++ = SampleValue;
@@ -25,19 +35,26 @@ static void GameOutputSound(game_state *GameState, game_sound_output_buffer *Sou
}
}
static void RenderWeirdGradient(game_offscreen_buffer *Buffer, S32 XOffset, S32 YOffset)
static void RenderWeirdGradient(game_offscreen_buffer *Buffer, S32 XOffset,
S32 YOffset)
{
S32 Width = Buffer->Width;
S32 Height = Buffer->Height;
S32 Width, Height, Y;
U8 *Row;
U8 *Row = (U8 *)Buffer->Memory;
S32 Y;
Width = Buffer->Width;
Height = Buffer->Height;
Row = (U8 *)Buffer->Memory;
for(Y = 0; Y < Height; Y++) {
U32 *Pixel = (U32 *)Row;
S32 X;
Pixel = (U32 *)Row;
for(X = 0; X < Width; X++) {
U8 Blue = (U8)(X + XOffset);
U8 Green = (U8)(Y + YOffset);
U8 Blue, Green;
Blue = (U8)(X + XOffset);
Green = (U8)(Y + YOffset);
*Pixel = (Green << 8) | Blue;
Pixel++;
@@ -46,18 +63,30 @@ static void RenderWeirdGradient(game_offscreen_buffer *Buffer, S32 XOffset, S32
}
}
void UpdateAndRender(game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer, game_sound_output_buffer *SoundBuffer)
void UpdateAndRender(game_memory *Memory, game_input *Input,
game_offscreen_buffer *Buffer,
game_sound_output_buffer *SoundBuffer)
{
Assert((&Input->Controllers[0].Terminator - &Input->Controllers[0].Buttons[0]) == ARRAY_SIZE(Input->Controllers[0].Buttons));
game_state *GameState;
U32 ControllerIndex;
Assert((&Input->Controllers[0].u.buttons.Terminator -
&Input->Controllers[0].u.Buttons[0]) ==
ARRAY_SIZE(Input->Controllers[0].u.Buttons));
Assert(sizeof(game_state) <= Memory->PermanentStorageSize);
game_state *GameState = (game_state *)Memory->PermanentStorage;
GameState = (game_state *)Memory->PermanentStorage;
if(!Memory->IsInitialized) {
#if BUILD_INTERNAL
char FileName[] = "/home/igor/Developer/handmade/sdl_handmade.cpp";
debug_read_file_result BitmapMemory = Memory->DEBUGPlatformReadEntireFile(FileName);
char *FileName;
debug_read_file_result BitmapMemory;
FileName = "/home/igor/Developer/handmade/sdl_handmade.cpp";
BitmapMemory = Memory->DEBUGPlatformReadEntireFile(FileName);
if(BitmapMemory.Contents) {
Memory->DEBUGPlatformWriteEntireFile("test.out", BitmapMemory.ContentsSize, BitmapMemory.Contents);
Memory->DEBUGPlatformWriteEntireFile("test.out",
BitmapMemory.ContentsSize,
BitmapMemory.Contents);
Memory->DEBUGPlatformFreeFileMemory(BitmapMemory.Contents);
}
#endif
@@ -72,29 +101,36 @@ void UpdateAndRender(game_memory *Memory, game_input *Input, game_offscreen_buff
/* GameState.GreenOffset; */
/* GameState.BlueOffset; */
U32 ControllerIndex;
for(ControllerIndex = 0; ControllerIndex < ARRAY_SIZE(Input->Controllers); ControllerIndex++) {
game_controller_input *Controller = GetController(Input, ControllerIndex);
for(ControllerIndex = 0;
ControllerIndex < ARRAY_SIZE(Input->Controllers);
ControllerIndex++)
{
game_controller_input *Controller =
GetController(Input, ControllerIndex);
if(Controller->IsAnalog) {
GameState->GreenOffset += (int)(4.0f*(Controller->StickAverageY));
GameState->BlueOffset += (int)(4.0f*(Controller->StickAverageX));
GameState->ToneHz = 256 + (S32)(128.0f*(Controller->StickAverageY));
GameState->GreenOffset +=
(int)(4.0f*(Controller->StickAverageY));
GameState->BlueOffset +=
(int)(4.0f*(Controller->StickAverageX));
GameState->ToneHz =
256 + (S32)(128.0f*(Controller->StickAverageY));
} else {
if(Controller->MoveUp.EndedDown) {
if(Controller->u.buttons.MoveUp.EndedDown) {
GameState->GreenOffset -= 5;
}
if(Controller->MoveDown.EndedDown) {
if(Controller->u.buttons.MoveDown.EndedDown) {
GameState->GreenOffset += 5;
}
if(Controller->MoveLeft.EndedDown) {
if(Controller->u.buttons.MoveLeft.EndedDown) {
GameState->BlueOffset -= 5;
}
if(Controller->MoveRight.EndedDown) {
if(Controller->u.buttons.MoveRight.EndedDown) {
GameState->BlueOffset += 5;
}
}
}
GameOutputSound(GameState, SoundBuffer, GameState->ToneHz);
RenderWeirdGradient(Buffer, GameState->BlueOffset, GameState->GreenOffset);
RenderWeirdGradient(Buffer, GameState->BlueOffset,
GameState->GreenOffset);
}