Skip to content

Instantly share code, notes, and snippets.

@yknishidate
Created March 6, 2022 09:43
Show Gist options
  • Save yknishidate/0f5c0364258cf245e80651d538067c48 to your computer and use it in GitHub Desktop.
Save yknishidate/0f5c0364258cf245e80651d538067c48 to your computer and use it in GitHub Desktop.
#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
}
@yknishidate
Copy link
Author

Game Programming Patterns, Chapter 15 Event Queue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment