Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "handmade.h"
- internal void GameOutputSound(game_sound_output_buffer *SoundBuffer, int toneHz)
- {
- // Declares and initializes constants and variables used in determining where we are in the buffer.
- local_persist float tSine;
- int toneVolume = 3000;
- int wavePeriod = SoundBuffer->samplesPerSecond / toneHz;
- int16_t *SampleOut = SoundBuffer->Samples;
- // Determine the number of samples to iterate over, depending on the respective buffer partitions.
- for (int sampleIndex = 0; sampleIndex < SoundBuffer->sampleCount; ++sampleIndex) {
- // Transforms the period of running sample index into the period of sine.
- float sineValue = sinf(tSine);
- // Scale our sine wave from [-1, 1] to [-toneVolume, toneVolume] so it's scaled into the 16 bit space properly.
- int16_t sampleValue = (int16_t)(sineValue * toneVolume);
- *SampleOut++ = sampleValue;
- *SampleOut++ = sampleValue;
- tSine += 2.0f * 3.14159265359f * 1.0f / (float)wavePeriod;
- // Normalize sine to it's period, for greater accuracy. Prevents the sound change bug that happens relatively soon.
- if (tSine > 2.0f * 3.14159265359f)
- tSine -= 2.0f * 3.14159265359f;
- }
- }
- // Draws the gradient to the backbuffer.
- internal void RenderWeirdGradient(game_offscreen_buffer *Buffer, int blueOffset, int greenOffset)
- {
- // C doesn't understand a void pointer, so cast it to a pointer to bytes.
- uint8_t *row = (uint8_t *)Buffer->Memory;
- // Actually draw the pixels, going row by row.
- for (int y = 0; y < Buffer->height; ++y) {
- // Cast the row pointer to a 32-bit integer, going pixel by pixel along a row.
- uint32_t *pixel = (uint32_t *)row;
- for (int x = 0; x < Buffer->width; ++x) {
- // Set the blue and green value to write to the pixel.
- uint8_t blue = (uint8_t)(x + blueOffset);
- uint8_t green = (uint8_t)(y + greenOffset);
- // Actually write to the pixel and increment to the next pixel.
- *pixel++ = ((green << 8) | blue);
- }
- // Go on to the next row.
- row += Buffer->pitch;
- }
- }
- extern "C" GAME_UPDATE_AND_RENDER(GameUpdateAndRender)
- {
- // Really, Casey? Paranoid check using pointer math for the button structure.
- Assert(((&Input->Controllers[0].terminator) - (&Input->Controllers[0].Buttons[0])) == (elementsInArray(Input->Controllers[0].Buttons)));
- // Initially, set the game state to be the permanent storage we allocated.
- game_state *GameState = (game_state *)Memory->permanentStorage;
- // The game state structure must fit inside the size of the permanent storage!
- Assert(sizeof(game_state) <= Memory->permanantStorageSize);
- // If the memory is not initialized, intialize the game state and memory.
- if (!Memory->isIntialized) {
- // Treat reading and writing files as a memory allocation. Get the file pointer and file size.
- char const *Filename = __FILE__;
- debug_read_file_result File = Memory->DEBUGPlatformReadEntireFile((char *)(Filename));
- // We were able to read the file and allocate the memory for it.
- if (File.Contents) {
- Memory->DEBUGPlatformWriteEntireFile((char *)"test.out", File.contentSize, File.Contents);
- Memory->DEBUGPlatformFreeFileMemory(File.Contents);
- }
- GameState->blueOffset = 0;
- GameState->greenOffset = 0;
- GameState->toneHz = 512;
- //Todo: Might be better to handle in platform layer?
- Memory->isIntialized = true;
- }
- // Now, check all controllers for input.
- for (int controllerIndex = 0; controllerIndex < elementsInArray(Input->Controllers); ++controllerIndex)
- {
- // By default, get the first player's input. Later, allow for multiplayer input.
- game_controller_input *Controller = GetController(Input, controllerIndex);
- // Determine what method to tune the input by, either analog or digital.
- if (Controller->isAnalog) {
- GameState->blueOffset += (int)(4.0f * (Controller->stickAverageX));
- GameState->toneHz = 512 + (int)(128.0f * (Controller->stickAverageY));
- }
- // If analog input is not available, use digital input.
- else {
- if (Controller->moveLeft.endedDown)
- GameState->blueOffset -= 1;
- if (Controller->moveRight.endedDown)
- GameState->blueOffset += 1;
- }
- // If the a button ended down, scroll.
- if (Controller->actionDown.endedDown)
- GameState->greenOffset += 1;
- }
- RenderWeirdGradient(Buffer, GameState->blueOffset, GameState->greenOffset);
- }
- extern "C" GAME_GET_SOUND_SAMPLES(GameGetSoundSamples)
- {
- game_state *GameState = (game_state *)Memory->permanentStorage;
- GameOutputSound(SoundBuffer, GameState->toneHz);
- }
- // Windows calls into our DLL always, but we don't need to do anything. Substitude a stub function.
- #if HANDMADE_WIN32
- #include "windows.h"
- BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
- {
- return TRUE;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement