Advertisement
Guest User

handmade.cpp

a guest
Oct 14th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.80 KB | None | 0 0
  1. #include "handmade.h"
  2.  
  3. internal void GameOutputSound(game_sound_output_buffer *SoundBuffer, int toneHz)
  4. {
  5.     // Declares and initializes constants and variables used in determining where we are in the buffer.
  6.     local_persist float tSine;
  7.     int toneVolume = 3000;
  8.     int wavePeriod = SoundBuffer->samplesPerSecond / toneHz;
  9.     int16_t *SampleOut = SoundBuffer->Samples;
  10.  
  11.     // Determine the number of samples to iterate over, depending on the respective buffer partitions.
  12.     for (int sampleIndex = 0; sampleIndex < SoundBuffer->sampleCount; ++sampleIndex) {
  13.  
  14.         // Transforms the period of running sample index into the period of sine.
  15.         float sineValue = sinf(tSine);
  16.  
  17.         // Scale our sine wave from [-1, 1] to [-toneVolume, toneVolume] so it's scaled into the 16 bit space properly.
  18.         int16_t sampleValue = (int16_t)(sineValue * toneVolume);
  19.         *SampleOut++ = sampleValue;
  20.         *SampleOut++ = sampleValue;
  21.         tSine += 2.0f * 3.14159265359f * 1.0f / (float)wavePeriod;
  22.  
  23.         // Normalize sine to it's period, for greater accuracy. Prevents the sound change bug that happens relatively soon.
  24.         if (tSine > 2.0f * 3.14159265359f)
  25.             tSine -= 2.0f * 3.14159265359f;
  26.     }
  27. }
  28.  
  29. // Draws the gradient to the backbuffer.
  30. internal void RenderWeirdGradient(game_offscreen_buffer *Buffer, int blueOffset, int greenOffset)
  31. {
  32.     // C doesn't understand a void pointer, so cast it to a pointer to bytes.
  33.     uint8_t *row = (uint8_t *)Buffer->Memory;
  34.  
  35.     // Actually draw the pixels, going row by row.
  36.     for (int y = 0; y < Buffer->height; ++y) {
  37.  
  38.         // Cast the row pointer to a 32-bit integer, going pixel by pixel along a row.
  39.         uint32_t *pixel = (uint32_t *)row;
  40.  
  41.         for (int x = 0; x < Buffer->width; ++x) {
  42.  
  43.             // Set the blue and green value to write to the pixel.
  44.             uint8_t blue = (uint8_t)(x + blueOffset);
  45.             uint8_t green = (uint8_t)(y + greenOffset);
  46.  
  47.             // Actually write to the pixel and increment to the next pixel.
  48.             *pixel++ = ((green << 8) | blue);
  49.         }
  50.  
  51.         // Go on to the next row.
  52.         row += Buffer->pitch;
  53.     }
  54. }
  55.  
  56. extern "C" GAME_UPDATE_AND_RENDER(GameUpdateAndRender)
  57. {
  58.     // Really, Casey? Paranoid check using pointer math for the button structure.
  59.     Assert(((&Input->Controllers[0].terminator) - (&Input->Controllers[0].Buttons[0])) == (elementsInArray(Input->Controllers[0].Buttons)));
  60.  
  61.     // Initially, set the game state to be the permanent storage we allocated.
  62.     game_state *GameState = (game_state *)Memory->permanentStorage;
  63.  
  64.     // The game state structure must fit inside the size of the permanent storage!
  65.     Assert(sizeof(game_state) <= Memory->permanantStorageSize);
  66.  
  67.     // If the memory is not initialized, intialize the game state and memory.
  68.     if (!Memory->isIntialized) {
  69.  
  70.         // Treat reading and writing files as a memory allocation. Get the file pointer and file size.
  71.         char const *Filename = __FILE__;
  72.         debug_read_file_result File = Memory->DEBUGPlatformReadEntireFile((char *)(Filename));
  73.  
  74.         // We were able to read the file and allocate the memory for it.
  75.         if (File.Contents) {
  76.             Memory->DEBUGPlatformWriteEntireFile((char *)"test.out", File.contentSize, File.Contents);
  77.             Memory->DEBUGPlatformFreeFileMemory(File.Contents);
  78.         }
  79.  
  80.         GameState->blueOffset = 0;
  81.         GameState->greenOffset = 0;
  82.         GameState->toneHz = 512;
  83.  
  84.         //Todo: Might be better to handle in platform layer?
  85.         Memory->isIntialized = true;
  86.     }
  87.    
  88.     // Now, check all controllers for input.
  89.     for (int controllerIndex = 0; controllerIndex < elementsInArray(Input->Controllers); ++controllerIndex)
  90.     {
  91.         // By default, get the first player's input. Later, allow for multiplayer input.
  92.         game_controller_input *Controller = GetController(Input, controllerIndex);
  93.    
  94.         // Determine what method to tune the input by, either analog or digital.
  95.         if (Controller->isAnalog) {
  96.             GameState->blueOffset += (int)(4.0f * (Controller->stickAverageX));
  97.             GameState->toneHz = 512 + (int)(128.0f * (Controller->stickAverageY));
  98.         }
  99.  
  100.         // If analog input is not available, use digital input.
  101.         else {
  102.             if (Controller->moveLeft.endedDown)
  103.                 GameState->blueOffset -= 1;
  104.             if (Controller->moveRight.endedDown)
  105.                 GameState->blueOffset += 1;
  106.         }
  107.  
  108.         // If the a button ended down, scroll.
  109.         if (Controller->actionDown.endedDown)
  110.             GameState->greenOffset += 1;
  111.     }
  112.  
  113.     RenderWeirdGradient(Buffer, GameState->blueOffset, GameState->greenOffset);
  114. }
  115. extern "C" GAME_GET_SOUND_SAMPLES(GameGetSoundSamples)
  116. {
  117.     game_state *GameState = (game_state *)Memory->permanentStorage;
  118.     GameOutputSound(SoundBuffer, GameState->toneHz);
  119. }
  120.  
  121. // Windows calls into our DLL always, but we don't need to do anything. Substitude a stub function.
  122. #if HANDMADE_WIN32
  123. #include "windows.h"
  124. BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
  125. {
  126.     return TRUE;
  127. }
  128. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement