Advertisement
Guest User

handmade.cpp

a guest
Jun 9th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.98 KB | None | 0 0
  1. #include "handmade.h"
  2.  
  3. static void gameOutputSound(game_state *gameState, game_sound_output_buffer *soundBuffer, int toneHz)
  4. {
  5.     int16_t toneVolume = 3000;
  6.     int wavePeriod = soundBuffer->samplesPerSecond / toneHz;
  7.        
  8.     int16_t *sampleOut = soundBuffer->samples;
  9.     for(int sampleIndex = 0; sampleIndex < soundBuffer->sampleCount ; ++sampleIndex)
  10.     {
  11. #if 0
  12.         float sineValue = sinf(gameState->tSine);
  13.         int16_t sampleValue = (int16_t)(sineValue * toneVolume);
  14. #else
  15.         int16_t sampleValue = 0;
  16. #endif
  17.         *sampleOut++ = sampleValue;
  18.         *sampleOut++ = sampleValue;
  19.         gameState->tSine += 2.0f*PI32*1.0f /(float)wavePeriod;
  20.         if(gameState->tSine > 2.0f * PI32)
  21.         {
  22.             gameState->tSine -= 2.0f * PI32;
  23.         }
  24.     }
  25. }
  26.  
  27. static void renderWeirdGradient(game_offscreen_buffer *buffer, int xOffset, int yOffset)
  28. {
  29.   uint8_t *Row = (uint8_t *)buffer->memory;
  30.  
  31.   for(int y = 0; y < buffer->height; ++y)
  32.   {
  33.     uint32_t *pixel = (uint32_t *)Row;
  34.     for(int x = 0; x < buffer->width; ++x)
  35.     {
  36.       uint8_t Blue = (uint8_t)(x + xOffset);
  37.       uint8_t Green = (uint8_t)(y + yOffset);
  38.  
  39.      
  40.       *pixel++ = (Green << 16) | (Blue);
  41.     }
  42.     Row += buffer->pitch;
  43.   }
  44. }
  45.  
  46. static void renderPlayer(game_offscreen_buffer *buffer, int playerX, int playerY)
  47. {
  48.     uint8_t *endOfBuffer = (uint8_t*)buffer->memory + buffer->bytesPerPixel*buffer->width + buffer->pitch*buffer->height;
  49.     uint32_t color = 0xFFFFFF;
  50.     int top = playerY;
  51.     int bottom = top + 10;
  52.     for(int x = playerX; x < playerX + 10; ++x)
  53.     {
  54.         uint8_t * drawPixel =(uint8_t*)buffer->memory + x*buffer->bytesPerPixel + top *buffer->pitch;
  55.         for(int y = top; y < bottom; ++y)
  56.         {
  57.             if((drawPixel >= buffer->memory) && (drawPixel < endOfBuffer))
  58.             {
  59.                 *(uint32_t *)drawPixel = color;
  60.                 drawPixel += buffer->pitch;
  61.             }
  62.         }
  63.     }
  64. }
  65.  
  66. extern "C" GAME_UPDATE_AND_RENDER(gameUpdateAndRender)
  67. {
  68.     Assert(((&input->controllers[0].start) - &input->controllers[0].buttons[0]) == (arrayCount(input->controllers[0].buttons) - 1));
  69.     Assert(sizeof(game_state) <= gameMemory->permanantStorageSize);
  70.    
  71.     game_state *gameState = (game_state*)gameMemory;
  72.     if(!gameMemory->isInitialized)
  73.     {
  74.         char *fileName = __FILE__;
  75.        
  76.         debug_read_file_result result = gameMemory->DEBUGPlatformReadEntireFile(fileName);
  77.         if(result.contents)
  78.         {
  79.             gameMemory->DEBUGPlatformWriteEntireFile("test.out", result.contents, result.contentSize);
  80.             gameMemory->DEBUGPlatformFreeFileMemory(result.contents);
  81.         }
  82.        
  83.        
  84.         gameState->toneHz = 256;
  85.         gameState->tSine = 0.0f;
  86.         gameMemory->isInitialized = true;
  87.        
  88.         gameState->playerX = 100;
  89.         gameState->playerY = 100;
  90.         gameState->tJump = 0;
  91.     }
  92.  
  93.     for(int controllerIndex = 0; controllerIndex < arrayCount(input->controllers); ++controllerIndex)
  94.     {  
  95.         game_controller_input *thisInput = getController(input, controllerIndex);
  96.         if(thisInput->isAnalog)
  97.         {
  98.             gameState->toneHz  = 256 + (int)((128.0f *(thisInput->stickAverageX)));
  99.             gameState->blueOffset += (int)(4.0f*(thisInput->stickAverageX));
  100.         }
  101.         else
  102.         {
  103.             if(thisInput->moveDown.endedDown)
  104.             {
  105.                 gameState->toneHz -= 20;
  106.             }
  107.             else if (thisInput->moveUp.endedDown)
  108.             {
  109.                 gameState->toneHz += 20;
  110.             }
  111.             else if (thisInput->moveLeft.endedDown)
  112.             {
  113.                 gameState->blueOffset -= 10;
  114.             }
  115.             else if(thisInput->moveRight.endedDown)
  116.             {
  117.                 gameState->blueOffset += 10;
  118.             }
  119.         }
  120.        
  121.         if(thisInput->actionDown.endedDown)
  122.         {
  123.             gameState->tJump = 1.0;
  124.         }
  125.         gameState->tJump -= 0.033f;
  126.         gameState->playerX += (int)(4.0f*(thisInput->stickAverageX));
  127.         gameState->playerY -= (int)(4.0f*(thisInput->stickAverageY));
  128.         if(gameState->tJump > 0)
  129.         {
  130.             gameState->playerY -= (int)(10.0f * sinf(gameState->tJump));
  131.         }
  132.     }
  133.    
  134.    
  135.     renderWeirdGradient(buffer, gameState->blueOffset, gameState->greenOffset);
  136.     renderPlayer(buffer, gameState->playerX, gameState->playerY);
  137. }
  138.  
  139. extern "C" GAME_GET_SOUND_SAMPLES(gameGetSoundSamples)
  140. {
  141.     game_state *gameState = (game_state*)gameMemory;
  142.     gameOutputSound(gameState, soundBuffer, gameState->toneHz);
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement