Worldstone
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
Inputs.h
1 #pragma once
2 
3 #include <SDL_events.h>
4 #include <condition_variable>
5 #include <mutex>
6 #include <vector>
7 
8 struct Inputs
9 {
10  struct MouseState
11  {
12  uint32_t buttonsMask;
13  int x;
14  int y;
15  } mouseState;
16 
17  std::vector<SDL_Event> events;
18 
19  void reset() { events.clear(); }
20 };
21 
23 {
24  std::mutex inputMutex;
25  std::condition_variable conditionVariable;
26  Inputs inputs;
27  bool transferNotReceived = false;
28 
29 public:
30  friend struct ScopedTransfer;
32  {
33  InputsManager& inputsManager;
34  ScopedTransfer(const ScopedTransfer&) = delete;
35 
36  public:
37  ScopedTransfer(InputsManager& manager) : inputsManager(manager)
38  {
39  std::unique_lock<std::mutex> lock(inputsManager.inputMutex);
40  inputsManager.inputs.reset();
41  }
43  {
44  inputsManager.transferNotReceived = true;
45  inputsManager.conditionVariable.notify_all();
46  }
47 
48  void PushMouseState(Inputs::MouseState mouseState)
49  {
50  inputsManager.inputs.mouseState = mouseState;
51  }
52 
53  void PushEvent(SDL_Event event) { inputsManager.inputs.events.push_back(event); }
54  };
55 
56  Inputs receiveAndProcessEvents()
57  {
58  std::unique_lock<std::mutex> lock(inputMutex);
59  conditionVariable.wait(lock, [this]() { return transferNotReceived; });
60  transferNotReceived = false;
61  return inputs;
62  }
63 };
Definition: Inputs.h:8