In process of migrating to ANSI C.

This commit is contained in:
2026-03-21 17:08:46 -07:00
parent a9573a440e
commit 8af1c59bb6
6 changed files with 222 additions and 133 deletions
+100
View File
@@ -0,0 +1,100 @@
#include "core.h"
#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;
S32 SampleIndex;
for(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;
S32 Y;
for(Y = 0; Y < Height; Y++) {
U32 *Pixel = (U32 *)Row;
S32 X;
for(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) {
#if BUILD_INTERNAL
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);
}
#endif
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; */
U32 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));
} else {
if(Controller->MoveUp.EndedDown) {
GameState->GreenOffset -= 5;
}
if(Controller->MoveDown.EndedDown) {
GameState->GreenOffset += 5;
}
if(Controller->MoveLeft.EndedDown) {
GameState->BlueOffset -= 5;
}
if(Controller->MoveRight.EndedDown) {
GameState->BlueOffset += 5;
}
}
}
GameOutputSound(GameState, SoundBuffer, GameState->ToneHz);
RenderWeirdGradient(Buffer, GameState->BlueOffset, GameState->GreenOffset);
}