Mercurial > hg > beaglert
diff projects/tank_wars/render.cpp @ 108:3068421c0737 ultra-staging
Merged default into ultra-staging
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Tue, 18 Aug 2015 00:35:15 +0100 |
parents | 3c3a1357657d |
children | 8d80eda512cd |
line wrap: on
line diff
--- a/projects/tank_wars/render.cpp Mon Jun 08 01:07:48 2015 +0100 +++ b/projects/tank_wars/render.cpp Tue Aug 18 00:35:15 2015 +0100 @@ -6,8 +6,8 @@ */ -#include "../../include/RTAudio.h" -#include "../../include/Utilities.h" +#include <BeagleRT.h> +#include <Utilities.h> #include "game.h" #include <rtdk.h> #include <cmath> @@ -78,7 +78,7 @@ void screen_update(); -// initialise_render() is called once before the audio rendering starts. +// setup() is called once before the audio rendering starts. // Use it to perform any initialisation and allocation which is dependent // on the period size or sample rate. // @@ -87,26 +87,22 @@ // // Return true on success; returning false halts the program. -bool initialise_render(int numAnalogChannels, int numDigitalChannels, int numAudioChannels, - int numAnalogFramesPerPeriod, - int numAudioFramesPerPeriod, - float analogSampleRate, float audioSampleRate, - void *userData) +bool setup(BeagleRTContext *context, void *userData) { srandom(time(NULL)); // Verify we are running with matrix enabled - if(numAnalogFramesPerPeriod == 0 || numAnalogChannels < 4) { + if(context->analogFrames == 0 || context->analogChannels < 4) { rt_printf("Error: this example needs the matrix enabled with at least 4 channels\n"); return false; } // Initialise audio variables - gAudioFramesPerMatrixFrame = numAudioFramesPerPeriod / numAnalogFramesPerPeriod; - gOscillatorPhaseScaler = 2.0 * M_PI / audioSampleRate; + gAudioFramesPerMatrixFrame = context->audioFrames / context->analogFrames; + gOscillatorPhaseScaler = 2.0 * M_PI / context->audioSampleRate; // Initialise the screen buffers - gScreenBufferMaxLength = 2 * analogSampleRate / gScreenFramesPerSecond; + gScreenBufferMaxLength = 2 * context->analogSampleRate / gScreenFramesPerSecond; gScreenBuffer1 = new float[gScreenBufferMaxLength]; gScreenBuffer2 = new float[gScreenBufferMaxLength]; if(gScreenBuffer1 == 0 || gScreenBuffer2 == 0) { @@ -123,12 +119,12 @@ // Initialise the game setupGame(gScreenWidth, gScreenHeight); - gGameFrameInterval = analogSampleRate / gGameFramesPerSecond; + gGameFrameInterval = context->analogSampleRate / gGameFramesPerSecond; gSamplesUntilNextFrame = gGameFrameInterval; // Initialise auxiliary tasks - if((gScreenUpdateTask = createAuxiliaryTaskLoop(&screen_update, 90, - "beaglert-screen-update")) == 0) + if((gScreenUpdateTask = BeagleRT_createAuxiliaryTask(&screen_update, 90, + "beaglert-screen-update")) == 0) return false; return true; @@ -159,12 +155,11 @@ // ADCs and DACs (if available). If only audio is available, numMatrixFrames // will be 0. -void render(int numAnalogFrames, int numDigitalFrames, int numAudioFrames, float *audioIn, float *audioOut, - float *analogIn, float *analogOut, uint32_t *digital) +void render(BeagleRTContext *context, void *userData) { int audioIndex = 0; - for(int n = 0; n < numAnalogFrames; n++) { + for(unsigned int n = 0; n < context->analogFrames; n++) { for(int k = 0; k < gAudioFramesPerMatrixFrame; k++) { // Render music and sound float audioSample = 0; @@ -193,12 +188,12 @@ gSoundProjectileOscillatorPhase -= 2.0 * M_PI; } - audioOut[2*audioIndex] = audioOut[2*audioIndex + 1] = audioSample; + context->audioOut[2*audioIndex] = context->audioOut[2*audioIndex + 1] = audioSample; audioIndex++; } // First-order lowpass filter to remove noise on launch FSR - float rawSample = AnalogRead(gInputLauncher, n); + float rawSample = analogReadFrame(context, n, gInputLauncher); float launchSample = gLauncherFilterPole * gLauncherLastSample + (1.0f - gLauncherFilterPole) * rawSample; gLauncherLastSample = launchSample; @@ -232,9 +227,9 @@ // Update game physics and cannon angles gSamplesUntilNextFrame = gGameFrameInterval; - setTank1CannonAngle(map(AnalogRead(gInputTank1Angle, n), + setTank1CannonAngle(map(analogReadFrame(context, n, gInputTank1Angle), 0, 1.0, M_PI, 0)); - setTank2CannonAngle(map(AnalogRead(gInputTank2Angle, n), + setTank2CannonAngle(map(analogReadFrame(context, n, gInputTank2Angle), 0, 1.0, M_PI, 0)); nextGameFrame(); @@ -257,21 +252,21 @@ // Rescale screen coordinates to matrix ranges; invert the Y // coordinate to go from normal screen coordinates to scope coordinates - AnalogWriteFrame(gOutputX, n, constrain(map(x, 0, gScreenWidth, 0, 1.0), 0, 1.0)); - AnalogWriteFrame(gOutputY, n, constrain(map(y, 0, gScreenHeight, 1.0, 0), 0, 1.0)); + analogWriteFrameOnce(context, n, gOutputX, constrain(map(x, 0, gScreenWidth, 0, 1.0), 0, 1.0)); + analogWriteFrameOnce(context, n, gOutputY, constrain(map(y, 0, gScreenHeight, 1.0, 0), 0, 1.0)); } else { // Still not ready! Write 0 until something happens - AnalogWriteFrame(gOutputX, n, 0); - AnalogWriteFrame(gOutputY, n, 0); + analogWriteFrameOnce(context, n, gOutputX, 0); + analogWriteFrameOnce(context, n, gOutputY, 0); } if(gameStatusWinner() != 0) { // Blink one LED to show who won // Blink both LEDs when projectile is in motion float val = (gSampleCounter % 4000 > 2000) ? 1.0 : 0; - AnalogWriteFrame(gOutputPlayer1LED, n, gameStatusWinner() == 1 ? val : 0); - AnalogWriteFrame(gOutputPlayer2LED, n, gameStatusWinner() == 2 ? val : 0); + analogWriteFrameOnce(context, n, gOutputPlayer1LED, gameStatusWinner() == 1 ? val : 0); + analogWriteFrameOnce(context, n, gOutputPlayer2LED, gameStatusWinner() == 2 ? val : 0); // After 5 seconds, restart the game gSamplesSinceFinish++; @@ -281,23 +276,23 @@ else if(gameStatusProjectileInMotion()) { // Blink both LEDs when projectile is in motion float val = (gSampleCounter % 2000 > 1000) ? 1.0 : 0; - AnalogWriteFrame(gOutputPlayer1LED, n, val); - AnalogWriteFrame(gOutputPlayer2LED, n, val); + analogWriteFrameOnce(context, n, gOutputPlayer1LED, val); + analogWriteFrameOnce(context, n, gOutputPlayer2LED, val); } else if(gameStatusPlayer1Turn()) { - AnalogWriteFrame(gOutputPlayer1LED, n, 1.0); - AnalogWriteFrame(gOutputPlayer2LED, n, 0); + analogWriteFrameOnce(context, n, gOutputPlayer1LED, 1.0); + analogWriteFrameOnce(context, n, gOutputPlayer2LED, 0); } else { - AnalogWriteFrame(gOutputPlayer2LED, n, 1.0); - AnalogWriteFrame(gOutputPlayer1LED, n, 0); + analogWriteFrameOnce(context, n, gOutputPlayer2LED, 1.0); + analogWriteFrameOnce(context, n, gOutputPlayer1LED, 0); } // Check if we have reached the point where we should next update if(gScreenBufferReadPointer >= gScreenBufferNextUpdateLocation && !gScreenNextBufferReady) { // Update the screen at lower priority than the audio thread - scheduleAuxiliaryTask(gScreenUpdateTask); + BeagleRT_scheduleAuxiliaryTask(gScreenUpdateTask); } gSampleCounter++; @@ -320,10 +315,10 @@ gScreenNextBufferReady = true; } -// cleanup_render() is called once at the end, after the audio has stopped. -// Release any resources that were allocated in initialise_render(). +// cleanup() is called once at the end, after the audio has stopped. +// Release any resources that were allocated in setup(). -void cleanup_render() +void cleanup(BeagleRTContext *context, void *userData) { // Clean up the game state cleanupGame();