Line data Source code
1 : /*
2 : * sadplay - AdLib music player with graphics.
3 : *
4 : * sdl_driver.cc - implementation for SDL channel bar.
5 : *
6 : * Copyright (C) 2019 Marco Confalonieri <marco at marcoconfalonieri.it>
7 : *
8 : * This program is free software: you can redistribute it and/or modify
9 : * it under the terms of the GNU General Public License as published by
10 : * the Free Software Foundation, either version 3 of the License, or
11 : * (at your option) any later version.
12 : *
13 : * This program is distributed in the hope that it will be useful,
14 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : * GNU General Public License for more details.
17 : *
18 : * You should have received a copy of the GNU General Public License
19 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 : */
21 :
22 : #include "sdl_channel_bar.h"
23 :
24 : // Constructor: it creates the channel vector and initializes the mutex.
25 21 : sdl_channel_bar::sdl_channel_bar(int num_channels): mutex(NULL), channels(8) {
26 7 : this->mutex = SDL_CreateMutex();
27 7 : reset_channels();
28 7 : }
29 :
30 : // Destructor: destroys the mutex.
31 28 : sdl_channel_bar::~sdl_channel_bar() {
32 7 : SDL_DestroyMutex(this->mutex);
33 14 : }
34 :
35 : // Updates the specified channel.
36 16 : void sdl_channel_bar::update(int channel, int value) {
37 16 : int status = SDL_LockMutex(this->mutex);
38 16 : if (status != 0) {
39 : return;
40 : }
41 16 : this->channels[channel] = value;
42 8 : SDL_UnlockMutex(this->mutex);
43 : }
44 :
45 : // Updates all channels.
46 15 : void sdl_channel_bar::update_all(const int values[]) {
47 15 : int status = SDL_LockMutex(this->mutex);
48 270 : for (int c = 0; c < this->channels.size(); c++) {
49 120 : int new_value = (values[c] > 100)? 100 : values[c];
50 240 : this->channels[c] = new_value;
51 : }
52 15 : SDL_UnlockMutex(this->mutex);
53 15 : }
54 :
55 : // Reset all channels
56 9 : void sdl_channel_bar::reset_channels() {
57 18 : int zeroes[this->channels.size()] = {};
58 18 : this->update_all(zeroes);
59 9 : }
60 :
61 : // Executes the operations associated with the timer.
62 1 : void sdl_channel_bar::time_elapsed(Uint32 time_elapsed) {
63 1 : int status = SDL_LockMutex(this->mutex);
64 1 : if (status != 0) {
65 : return;
66 : }
67 26 : for (int c = 0; c < this->channels.size(); c++) {
68 16 : int& ch = this->channels[c];
69 8 : if (ch > 0) {
70 8 : int new_value = ch - (time_elapsed * 100 / DECAY_TIMER);
71 8 : ch = (new_value < 0)? 0 : new_value;
72 : }
73 : }
74 1 : SDL_UnlockMutex(this->mutex);
75 : }
76 :
77 2 : int sdl_channel_bar::get_numchannels() {
78 2 : int status = SDL_LockMutex(this->mutex);
79 2 : if (status != 0) {
80 : return 0;
81 : }
82 2 : int num_channels = this->channels.size();
83 1 : SDL_UnlockMutex(this->mutex);
84 1 : return num_channels;
85 : }
86 :
87 2 : void sdl_channel_bar::get_channels(int* channels) {
88 2 : int status = SDL_LockMutex(this->mutex);
89 2 : if (status != 0) {
90 : return;
91 : }
92 26 : for (int i = 0; i < this->channels.size(); i++) {
93 16 : int value = this->channels[i];
94 8 : channels[i] = value;
95 : }
96 1 : SDL_UnlockMutex(this->mutex);
97 : }
|