Files
handmadehero/handmade.cpp
T

89 lines
3.2 KiB
C++

#include "handmade.h"
#include "sdl_handmade.h"
#include <math.h>
static void GameOutputSound(game_sound_output_buffer *SoundBuffer, S32 ToneHz)
{
static 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 * PI * 1.0f / (F32)WavePeriod;
}
}
static 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;
}
}
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));
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 = Memory->DEBUGPlatformReadEntireFile(FileName);
if(BitmapMemory.Contents) {
Memory->DEBUGPlatformWriteEntireFile("test.out", BitmapMemory.ContentsSize, BitmapMemory.Contents);
Memory->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 < 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));
} else {
if(Controller->MoveUp.EndedDown) {
GameState->GreenOffset -= 1;
}
if(Controller->MoveDown.EndedDown) {
GameState->GreenOffset += 2;
}
if(Controller->MoveLeft.EndedDown) {
GameState->BlueOffset -= 1;
}
if(Controller->MoveRight.EndedDown) {
GameState->BlueOffset += 1;
}
}
}
GameOutputSound(SoundBuffer, GameState->ToneHz);
RenderWeirdGradient(Buffer, GameState->BlueOffset, GameState->GreenOffset);
}