Created
March 6, 2022 09:43
-
-
Save yknishidate/0f5c0364258cf245e80651d538067c48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <queue> | |
struct PlayMessage | |
{ | |
int soundId; | |
int volume; | |
}; | |
class Audio | |
{ | |
public: | |
static void update() | |
{ | |
if (messageQueue.empty()) | |
{ | |
return; | |
} | |
int soundId = messageQueue.front().soundId; | |
std::cout << "start sound: " << soundId << std::endl; | |
messageQueue.pop(); | |
} | |
static void playSound(int soundId, int volume) | |
{ | |
messageQueue.push({soundId, volume}); | |
} | |
private: | |
static std::queue<PlayMessage> messageQueue; | |
}; | |
std::queue<PlayMessage> Audio::messageQueue; | |
int main() | |
{ | |
Audio::playSound(0, 1); | |
Audio::playSound(1, 1); | |
Audio::update(); // start sound: 0 | |
Audio::update(); // start sound: 1 | |
Audio::update(); // no output | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Game Programming Patterns, Chapter 15 Event Queue