Initial commit.
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
#include "handmade.h"
|
||||
|
||||
internal void GameOutputSound(game_sound_output_buffer *SoundBuffer, S32 ToneHz)
|
||||
{
|
||||
local_persist F32 tSine;
|
||||
S16 ToneVolume = 8000;
|
||||
S32 WavePeriod = SoundBuffer->SamplesPerSecond / ToneHz;
|
||||
|
||||
S16 *SampleOut = SoundBuffer->Samples;
|
||||
for(S32 SampleIndex = 0; SampleIndex < SoundBuffer->SampleCount; SampleIndex++) {
|
||||
F32 SineValue = sinf(tSine);
|
||||
S16 SampleValue = S16(SineValue * ToneVolume);
|
||||
*SampleOut++ = SampleValue;
|
||||
*SampleOut++ = SampleValue;
|
||||
|
||||
tSine += 2.0f * pi32 * 1.0f / (F32)WavePeriod;
|
||||
}
|
||||
}
|
||||
|
||||
internal void RenderWeirdGradient(game_offscreen_buffer *Buffer, S32 XOffset, S32 YOffset)
|
||||
{
|
||||
S32 Width = Buffer->Width;
|
||||
S32 Height = Buffer->Height;
|
||||
|
||||
U8 *Row = (U8 *)Buffer->Memory;
|
||||
for(S32 Y = 0; Y < Height; Y++) {
|
||||
U32 *Pixel = (U32 *)Row;
|
||||
for(S32 X = 0; X < Width; X++) {
|
||||
U8 Blue = (U8)(X + XOffset);
|
||||
U8 Green = (U8)(Y + YOffset);
|
||||
*Pixel = (Green << 8) | Blue;
|
||||
Pixel++;
|
||||
}
|
||||
Row += Buffer->Pitch;
|
||||
}
|
||||
}
|
||||
|
||||
internal void GameUpdateAndRender(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]) == ArrayCount(Input->Controllers[0].Buttons));
|
||||
Assert(sizeof(game_state) <= Memory->PermanentStorageSize);
|
||||
|
||||
game_state *GameState = (game_state *)Memory->PermanentStorage;
|
||||
if(!Memory->IsInitialized) {
|
||||
char FileName[] = "/Users/igor/Developer/handmade_hero_course/handmade/code/sdl_handmade.cpp";
|
||||
debug_read_file_result BitmapMemory = DEBUGPlatformReadEntireFile(FileName);
|
||||
if(BitmapMemory.Contents) {
|
||||
DEBUGPlatformWriteEntireFile("test.out", BitmapMemory.ContentsSize, BitmapMemory.Contents);
|
||||
DEBUGPlatformFreeFileMemory(BitmapMemory.Contents);
|
||||
}
|
||||
|
||||
GameState->ToneHz = 256;
|
||||
|
||||
// TODO: Maybe more appropriate to do in the platform layer?
|
||||
Memory->IsInitialized = true;
|
||||
}
|
||||
// GameState.ToneHz;
|
||||
// GameState.GreenOffset;
|
||||
// GameState.BlueOffset;
|
||||
|
||||
for(U32 ControllerIndex = 0; ControllerIndex < ArrayCount(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));
|
||||
} else {
|
||||
if(Controller->MoveUp.EndedDown) {
|
||||
GameState->GreenOffset -= 1;
|
||||
}
|
||||
if(Controller->MoveDown.EndedDown) {
|
||||
GameState->GreenOffset += 1;
|
||||
}
|
||||
if(Controller->MoveLeft.EndedDown) {
|
||||
GameState->BlueOffset -= 1;
|
||||
}
|
||||
if(Controller->MoveRight.EndedDown) {
|
||||
GameState->BlueOffset += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GameOutputSound(SoundBuffer, GameState->ToneHz);
|
||||
RenderWeirdGradient(Buffer, GameState->BlueOffset, GameState->GreenOffset);
|
||||
}
|
||||
Reference in New Issue
Block a user