Mercurial > hg > beaglert
comparison projects/tank_wars/render.cpp @ 10:49f22e1246b2
Tank wars!
author | andrewm |
---|---|
date | Thu, 13 Nov 2014 15:58:08 +0100 |
parents | |
children | 06f93bef7dd2 |
comparison
equal
deleted
inserted
replaced
5:09f03ac40fcc | 10:49f22e1246b2 |
---|---|
1 /* | |
2 * render.cpp | |
3 * | |
4 * Created on: Oct 24, 2014 | |
5 * Author: parallels | |
6 */ | |
7 | |
8 | |
9 #include "../../include/RTAudio.h" | |
10 #include "../../include/Utilities.h" | |
11 #include "game.h" | |
12 #include <rtdk.h> | |
13 #include <cmath> | |
14 #include <cstdlib> | |
15 #include <time.h> | |
16 | |
17 float gFrequency; | |
18 float gPhase; | |
19 float gInverseSampleRate; | |
20 int gNumChannels; | |
21 | |
22 int gInputTank1Angle = 0; // Inputs for the cannon angles | |
23 int gInputTank2Angle = 1; | |
24 int gInputLauncher = 2; // Input for launcher FSR | |
25 | |
26 int gOutputX = 0; // Outputs for the scope | |
27 int gOutputY = 1; | |
28 int gOutputPlayer1LED = 2; | |
29 int gOutputPlayer2LED = 3; | |
30 | |
31 int gGameFramesPerSecond = 60; // How often the physics are updated | |
32 int gGameFrameInterval; // ...and in frames | |
33 int gSamplesUntilNextFrame; // Counter until next update | |
34 int gSamplesSinceFinish = 0; // How long since somebody won? | |
35 bool gGameShouldRestart = false;// Whether we need to reinitiliase the game | |
36 | |
37 // Counter for overall number of samples that have elapsed | |
38 unsigned int gSampleCounter = 0; | |
39 | |
40 // 1st-order filter and peak detector for launcher input | |
41 float gLauncherLastSample = 0; | |
42 float gLauncherFilterPole = 0.8; | |
43 float gLauncherPeakValue = 0; | |
44 float gLauncherPeakFilterPole = 0.999; | |
45 float gLauncherNoiseThreshold = 0.01 * MATRIX_MAX; | |
46 float gLauncherMinimumPeak = 0.1 * MATRIX_MAX; | |
47 bool gLauncherTriggered = false; | |
48 | |
49 // Screen update rate; affects buffer size. Actual contents of buffer | |
50 // may be smaller than this | |
51 int gScreenWidth = 512; | |
52 int gScreenHeight = 512; | |
53 int gScreenFramesPerSecond = 25; | |
54 | |
55 // Double buffer for rendering screen. Each is an interleaved buffer | |
56 // of XY data. | |
57 float *gScreenBuffer1, *gScreenBuffer2; | |
58 float *gScreenBufferWrite, *gScreenBufferRead; | |
59 int gScreenBufferMaxLength; // What is the total buffer allocated? | |
60 int gScreenBufferReadLength; // How long is the read buffer? | |
61 int gScreenBufferWriteLength; // How long is the write (next) buffer? | |
62 int gScreenBufferReadPointer; // Where are we in the read buffer now? | |
63 int gScreenBufferNextUpdateLocation; // When should we render the next buffer? | |
64 bool gScreenNextBufferReady; // Is the next buffer ready to go? | |
65 | |
66 // Auxiliary (low-priority) task for updating the screen | |
67 AuxiliaryTask gScreenUpdateTask; | |
68 | |
69 void screen_update(); | |
70 | |
71 // initialise_render() is called once before the audio rendering starts. | |
72 // Use it to perform any initialisation and allocation which is dependent | |
73 // on the period size or sample rate. | |
74 // | |
75 // userData holds an opaque pointer to a data structure that was passed | |
76 // in from the call to initAudio(). | |
77 // | |
78 // Return true on success; returning false halts the program. | |
79 | |
80 bool initialise_render(int numChannels, int numMatrixFramesPerPeriod, | |
81 int numAudioFramesPerPeriod, float matrixSampleRate, | |
82 float audioSampleRate, void *userData) | |
83 { | |
84 srandom(time(NULL)); | |
85 | |
86 // Verify we are running with matrix enabled | |
87 if(numMatrixFramesPerPeriod*2 != numAudioFramesPerPeriod) { | |
88 rt_printf("Error: this example needs the matrix enabled, running at half audio rate\n"); | |
89 return false; | |
90 } | |
91 | |
92 // Initialise the screen buffers | |
93 gScreenBufferMaxLength = 2 * matrixSampleRate / gScreenFramesPerSecond; | |
94 gScreenBuffer1 = new float[gScreenBufferMaxLength]; | |
95 gScreenBuffer2 = new float[gScreenBufferMaxLength]; | |
96 if(gScreenBuffer1 == 0 || gScreenBuffer2 == 0) { | |
97 rt_printf("Error initialising screen buffers\n"); | |
98 return false; | |
99 } | |
100 | |
101 gScreenBufferRead = gScreenBuffer1; | |
102 gScreenBufferWrite = gScreenBuffer2; | |
103 gScreenBufferReadLength = gScreenBufferWriteLength = 0; | |
104 gScreenBufferReadPointer = 0; | |
105 gScreenBufferNextUpdateLocation = 0; | |
106 gScreenNextBufferReady = false; | |
107 | |
108 // Initialise the game | |
109 setupGame(gScreenWidth, gScreenHeight); | |
110 gGameFrameInterval = matrixSampleRate / gGameFramesPerSecond; | |
111 gSamplesUntilNextFrame = gGameFrameInterval; | |
112 | |
113 // Initialise auxiliary tasks | |
114 if((gScreenUpdateTask = createAuxiliaryTaskLoop(&screen_update, 90, | |
115 "beaglert-screen-update")) == 0) | |
116 return false; | |
117 | |
118 return true; | |
119 } | |
120 | |
121 // Swap buffers on the screen | |
122 void swap_buffers() | |
123 { | |
124 if(gScreenBufferRead == gScreenBuffer1) { | |
125 gScreenBufferRead = gScreenBuffer2; | |
126 gScreenBufferWrite = gScreenBuffer1; | |
127 } | |
128 else { | |
129 gScreenBufferRead = gScreenBuffer1; | |
130 gScreenBufferWrite = gScreenBuffer2; | |
131 } | |
132 | |
133 gScreenBufferReadLength = gScreenBufferWriteLength; | |
134 gScreenBufferReadPointer = 0; | |
135 | |
136 // Schedule next update for 3/4 of the way through the buffer | |
137 gScreenBufferNextUpdateLocation = gScreenBufferReadLength * 0.75; | |
138 gScreenNextBufferReady = false; | |
139 } | |
140 | |
141 // render() is called regularly at the highest priority by the audio engine. | |
142 // Input and output are given from the audio hardware and the other | |
143 // ADCs and DACs (if available). If only audio is available, numMatrixFrames | |
144 // will be 0. | |
145 | |
146 void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut, | |
147 uint16_t *matrixIn, uint16_t *matrixOut) | |
148 { | |
149 for(int n = 0; n < numMatrixFrames; n++) { | |
150 // First-order lowpass filter to remove noise on launch FSR | |
151 float rawSample = analogRead(gInputLauncher, n); | |
152 float launchSample = gLauncherFilterPole * gLauncherLastSample + | |
153 (1.0f - gLauncherFilterPole) * rawSample; | |
154 gLauncherLastSample = launchSample; | |
155 | |
156 // Peak-detect on launch signal | |
157 if(launchSample >= gLauncherPeakValue) { | |
158 gLauncherPeakValue = launchSample; | |
159 gLauncherTriggered = false; | |
160 } | |
161 else { | |
162 if(gLauncherPeakValue - launchSample > gLauncherNoiseThreshold && !gLauncherTriggered) { | |
163 // Detected a peak; is it big enough overall? | |
164 if(gLauncherPeakValue >= gLauncherMinimumPeak) { | |
165 gLauncherTriggered = true; | |
166 // Peak detected-- fire!! | |
167 // Set both cannon strengths but only one will | |
168 // fire depending on whose turn it is | |
169 float strength = map(gLauncherPeakValue, | |
170 gLauncherMinimumPeak, MATRIX_MAX, | |
171 0.5f, 10.0f); | |
172 setTank1CannonStrength(strength); | |
173 setTank2CannonStrength(strength); | |
174 fireProjectile(); | |
175 } | |
176 } | |
177 | |
178 gLauncherPeakValue *= gLauncherPeakFilterPole; | |
179 } | |
180 | |
181 if(--gSamplesUntilNextFrame <= 0) { | |
182 // Update game physics and cannon angles | |
183 gSamplesUntilNextFrame = gGameFrameInterval; | |
184 | |
185 setTank1CannonAngle(map(analogRead(gInputTank1Angle, n), | |
186 0, MATRIX_MAX, M_PI, 0)); | |
187 setTank2CannonAngle(map(analogRead(gInputTank2Angle, n), | |
188 0, MATRIX_MAX, M_PI, 0)); | |
189 nextGameFrame(); | |
190 } | |
191 | |
192 if(gScreenBufferReadPointer >= gScreenBufferReadLength - 1 | |
193 && gScreenNextBufferReady) { | |
194 // Got to the end; swap buffers | |
195 swap_buffers(); | |
196 } | |
197 | |
198 // Push current screen buffer to the matrix output | |
199 if(gScreenBufferReadPointer < gScreenBufferReadLength - 1) { | |
200 float x = gScreenBufferRead[gScreenBufferReadPointer++]; | |
201 float y = gScreenBufferRead[gScreenBufferReadPointer++]; | |
202 | |
203 // Rescale screen coordinates to matrix ranges; invert the Y | |
204 // coordinate to go from normal screen coordinates to scope coordinates | |
205 analogWrite(gOutputX, n, constrain(map(x, 0, gScreenWidth, 0, MATRIX_MAX), 0, MATRIX_MAX)); | |
206 analogWrite(gOutputY, n, constrain(map(y, 0, gScreenHeight, MATRIX_MAX, 0), 0, MATRIX_MAX)); | |
207 } | |
208 else { | |
209 // Still not ready! Write 0 until something happens | |
210 analogWrite(gOutputX, n, 0); | |
211 analogWrite(gOutputY, n, 0); | |
212 } | |
213 | |
214 if(gameStatusWinner() != 0) { | |
215 // Blink one LED to show who won | |
216 // Blink both LEDs when projectile is in motion | |
217 uint16_t val = (gSampleCounter % 4000 > 2000) ? MATRIX_MAX : 0; | |
218 analogWrite(gOutputPlayer1LED, n, gameStatusWinner() == 1 ? val : 0); | |
219 analogWrite(gOutputPlayer2LED, n, gameStatusWinner() == 2 ? val : 0); | |
220 | |
221 // After 5 seconds, restart the game | |
222 gSamplesSinceFinish++; | |
223 if(gSamplesSinceFinish > 22050*5) | |
224 gGameShouldRestart = true; | |
225 } | |
226 else if(gameStatusProjectileInMotion()) { | |
227 // Blink both LEDs when projectile is in motion | |
228 uint16_t val = (gSampleCounter % 2000 > 1000) ? MATRIX_MAX : 0; | |
229 analogWrite(gOutputPlayer1LED, n, val); | |
230 analogWrite(gOutputPlayer2LED, n, val); | |
231 } | |
232 else if(gameStatusPlayer1Turn()) { | |
233 analogWrite(gOutputPlayer1LED, n, MATRIX_MAX); | |
234 analogWrite(gOutputPlayer2LED, n, 0); | |
235 } | |
236 else { | |
237 analogWrite(gOutputPlayer2LED, n, MATRIX_MAX); | |
238 analogWrite(gOutputPlayer1LED, n, 0); | |
239 } | |
240 | |
241 // Check if we have reached the point where we should next update | |
242 if(gScreenBufferReadPointer >= gScreenBufferNextUpdateLocation && | |
243 !gScreenNextBufferReady) { | |
244 // Update the screen at lower priority than the audio thread | |
245 scheduleAuxiliaryTask(gScreenUpdateTask); | |
246 } | |
247 | |
248 gSampleCounter++; | |
249 } | |
250 } | |
251 | |
252 void screen_update() | |
253 { | |
254 // If we should restart, reinitialise the game | |
255 if(gGameShouldRestart) { | |
256 restartGame(); | |
257 gGameShouldRestart = false; | |
258 gSamplesSinceFinish = 0; | |
259 } | |
260 | |
261 // Render the game based on the current state | |
262 gScreenBufferWriteLength = drawGame(gScreenBufferWrite, gScreenBufferMaxLength); | |
263 | |
264 // Flag it as ready to go | |
265 gScreenNextBufferReady = true; | |
266 } | |
267 | |
268 // cleanup_render() is called once at the end, after the audio has stopped. | |
269 // Release any resources that were allocated in initialise_render(). | |
270 | |
271 void cleanup_render() | |
272 { | |
273 // Clean up the game state | |
274 cleanupGame(); | |
275 } |