93 lines
3.3 KiB
C++
93 lines
3.3 KiB
C++
#include "handmade.h"
|
|
#include "sdl_handmade.h"
|
|
|
|
#include <math.h>
|
|
|
|
static void GameOutputSound(game_state *GameState, game_sound_output_buffer *SoundBuffer, S32 ToneHz)
|
|
{
|
|
S16 ToneVolume = 8000;
|
|
S32 WavePeriod = SoundBuffer->SamplesPerSecond / ToneHz;
|
|
|
|
S16 *SampleOut = SoundBuffer->Samples;
|
|
for(S32 SampleIndex = 0; SampleIndex < SoundBuffer->SampleCount; SampleIndex++) {
|
|
F32 SineValue = sinf(GameState->tSine);
|
|
S16 SampleValue = S16(SineValue * ToneVolume);
|
|
*SampleOut++ = SampleValue;
|
|
*SampleOut++ = SampleValue;
|
|
|
|
GameState->tSine += 2.0f * (F32)PI * 1.0f / (F32)WavePeriod;
|
|
if(GameState->tSine > 2.0f*(F32)PI) {
|
|
GameState->tSine -= 2.0f*(F32)PI;
|
|
}
|
|
}
|
|
}
|
|
|
|
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[] = "/home/igor/Developer/handmade/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;
|
|
GameState->tSine = 0.0f;
|
|
|
|
// 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(GameState, SoundBuffer, GameState->ToneHz);
|
|
RenderWeirdGradient(Buffer, GameState->BlueOffset, GameState->GreenOffset);
|
|
}
|