Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "handmade.h"
- static void gameOutputSound(game_state *gameState, game_sound_output_buffer *soundBuffer, int toneHz)
- {
- int16_t toneVolume = 3000;
- int wavePeriod = soundBuffer->samplesPerSecond / toneHz;
- int16_t *sampleOut = soundBuffer->samples;
- for(int sampleIndex = 0; sampleIndex < soundBuffer->sampleCount ; ++sampleIndex)
- {
- #if 0
- float sineValue = sinf(gameState->tSine);
- int16_t sampleValue = (int16_t)(sineValue * toneVolume);
- #else
- int16_t sampleValue = 0;
- #endif
- *sampleOut++ = sampleValue;
- *sampleOut++ = sampleValue;
- gameState->tSine += 2.0f*PI32*1.0f /(float)wavePeriod;
- if(gameState->tSine > 2.0f * PI32)
- {
- gameState->tSine -= 2.0f * PI32;
- }
- }
- }
- static void renderWeirdGradient(game_offscreen_buffer *buffer, int xOffset, int yOffset)
- {
- uint8_t *Row = (uint8_t *)buffer->memory;
- for(int y = 0; y < buffer->height; ++y)
- {
- uint32_t *pixel = (uint32_t *)Row;
- for(int x = 0; x < buffer->width; ++x)
- {
- uint8_t Blue = (uint8_t)(x + xOffset);
- uint8_t Green = (uint8_t)(y + yOffset);
- *pixel++ = (Green << 16) | (Blue);
- }
- Row += buffer->pitch;
- }
- }
- static void renderPlayer(game_offscreen_buffer *buffer, int playerX, int playerY)
- {
- uint8_t *endOfBuffer = (uint8_t*)buffer->memory + buffer->bytesPerPixel*buffer->width + buffer->pitch*buffer->height;
- uint32_t color = 0xFFFFFF;
- int top = playerY;
- int bottom = top + 10;
- for(int x = playerX; x < playerX + 10; ++x)
- {
- uint8_t * drawPixel =(uint8_t*)buffer->memory + x*buffer->bytesPerPixel + top *buffer->pitch;
- for(int y = top; y < bottom; ++y)
- {
- if((drawPixel >= buffer->memory) && (drawPixel < endOfBuffer))
- {
- *(uint32_t *)drawPixel = color;
- drawPixel += buffer->pitch;
- }
- }
- }
- }
- extern "C" GAME_UPDATE_AND_RENDER(gameUpdateAndRender)
- {
- Assert(((&input->controllers[0].start) - &input->controllers[0].buttons[0]) == (arrayCount(input->controllers[0].buttons) - 1));
- Assert(sizeof(game_state) <= gameMemory->permanantStorageSize);
- game_state *gameState = (game_state*)gameMemory;
- if(!gameMemory->isInitialized)
- {
- char *fileName = __FILE__;
- debug_read_file_result result = gameMemory->DEBUGPlatformReadEntireFile(fileName);
- if(result.contents)
- {
- gameMemory->DEBUGPlatformWriteEntireFile("test.out", result.contents, result.contentSize);
- gameMemory->DEBUGPlatformFreeFileMemory(result.contents);
- }
- gameState->toneHz = 256;
- gameState->tSine = 0.0f;
- gameMemory->isInitialized = true;
- gameState->playerX = 100;
- gameState->playerY = 100;
- gameState->tJump = 0;
- }
- for(int controllerIndex = 0; controllerIndex < arrayCount(input->controllers); ++controllerIndex)
- {
- game_controller_input *thisInput = getController(input, controllerIndex);
- if(thisInput->isAnalog)
- {
- gameState->toneHz = 256 + (int)((128.0f *(thisInput->stickAverageX)));
- gameState->blueOffset += (int)(4.0f*(thisInput->stickAverageX));
- }
- else
- {
- if(thisInput->moveDown.endedDown)
- {
- gameState->toneHz -= 20;
- }
- else if (thisInput->moveUp.endedDown)
- {
- gameState->toneHz += 20;
- }
- else if (thisInput->moveLeft.endedDown)
- {
- gameState->blueOffset -= 10;
- }
- else if(thisInput->moveRight.endedDown)
- {
- gameState->blueOffset += 10;
- }
- }
- if(thisInput->actionDown.endedDown)
- {
- gameState->tJump = 1.0;
- }
- gameState->tJump -= 0.033f;
- gameState->playerX += (int)(4.0f*(thisInput->stickAverageX));
- gameState->playerY -= (int)(4.0f*(thisInput->stickAverageY));
- if(gameState->tJump > 0)
- {
- gameState->playerY -= (int)(10.0f * sinf(gameState->tJump));
- }
- }
- renderWeirdGradient(buffer, gameState->blueOffset, gameState->greenOffset);
- renderPlayer(buffer, gameState->playerX, gameState->playerY);
- }
- extern "C" GAME_GET_SOUND_SAMPLES(gameGetSoundSamples)
- {
- game_state *gameState = (game_state*)gameMemory;
- gameOutputSound(gameState, soundBuffer, gameState->toneHz);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement