comparison projects/tank_wars/main.cpp @ 10:49f22e1246b2

Tank wars!
author andrewm
date Thu, 13 Nov 2014 15:58:08 +0100
parents
children 901d205d1a3c
comparison
equal deleted inserted replaced
5:09f03ac40fcc 10:49f22e1246b2
1 /*
2 * main.cpp
3 *
4 * Created on: Oct 24, 2014
5 * Author: parallels
6 */
7
8 #include <iostream>
9 #include <cstdlib>
10 #include <libgen.h>
11 #include <signal.h>
12 #include <getopt.h>
13 #include "../../include/RTAudio.h"
14
15 extern int gScreenFramesPerSecond;
16
17 using namespace std;
18
19 // Handle Ctrl-C by requesting that the audio rendering stop
20 void interrupt_handler(int var)
21 {
22 gShouldStop = true;
23 }
24
25 // Print usage information
26 void usage(const char * processName)
27 {
28 cerr << "Usage: " << processName << " [options]" << endl;
29
30 BeagleRT_usage();
31
32 cerr << " --fps [-f] value: Set target frames per second\n";
33 cerr << " --help [-h]: Print this menu\n";
34 }
35
36 int main(int argc, char *argv[])
37 {
38 RTAudioSettings settings; // Standard audio settings
39
40 struct option customOptions[] =
41 {
42 {"help", 0, NULL, 'h'},
43 {"fps", 1, NULL, 'f'},
44 {NULL, 0, NULL, 0}
45 };
46
47 // Set default settings
48 BeagleRT_defaultSettings(&settings);
49
50 // Parse command-line arguments
51 while (1) {
52 int c;
53 if ((c = BeagleRT_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0)
54 break;
55 switch (c) {
56 case 'f':
57 gScreenFramesPerSecond = atoi(optarg);
58 if(gScreenFramesPerSecond < 1)
59 gScreenFramesPerSecond = 1;
60 if(gScreenFramesPerSecond > 100)
61 gScreenFramesPerSecond = 100;
62 break;
63 case 'h':
64 usage(basename(argv[0]));
65 exit(0);
66 case '?':
67 default:
68 usage(basename(argv[0]));
69 exit(1);
70 }
71 }
72
73 // Initialise the PRU audio device
74 if(BeagleRT_initAudio(&settings, 0) != 0) {
75 cout << "Error: unable to initialise audio" << endl;
76 return -1;
77 }
78
79 // Start the audio device running
80 if(BeagleRT_startAudio()) {
81 cout << "Error: unable to start real-time audio" << endl;
82 return -1;
83 }
84
85 // Set up interrupt handler to catch Control-C
86 signal(SIGINT, interrupt_handler);
87
88 // Run until told to stop
89 while(!gShouldStop) {
90 usleep(100000);
91 }
92
93 // Stop the audio device
94 BeagleRT_stopAudio();
95
96 // Clean up any resources allocated for audio
97 BeagleRT_cleanupAudio();
98
99 // All done!
100 return 0;
101 }