Lib
QOLを高める
framework.h
Go to the documentation of this file.
1 
16 #pragma once
17 
18 #include "util.h"
19 #include "debug.h"
20 #include "graphics.h"
21 #include "sound.h"
22 #include "input.h"
23 #include <atomic>
24 #include <future>
25 #include <functional>
26 
27 namespace yappy {
29 namespace framework {
30 
31 using error::throwTrace;
32 using error::FrameworkError;
33 
35 namespace random {
36 
44 unsigned int generateRandomSeed();
45 
50 void setSeed(unsigned int seed);
51 
55 unsigned int nextRawUInt32();
56 
62 int nextInt(int a = 0, int b = std::numeric_limits<int>::max());
63 
69 double nextDouble(double a = 0.0, double max = 1.0);
70 
71 } // namespace random
72 
73 namespace scene {
74 
78 class SceneBase : private util::noncopyable {
79 public:
81  SceneBase() = default;
83  virtual ~SceneBase() = default;
84 
86  virtual void update() = 0;
88  virtual void render() = 0;
89 };
90 
93 class AsyncLoadScene : public SceneBase {
94 public:
96  AsyncLoadScene() = default;
102  virtual ~AsyncLoadScene() override;
105  virtual void update() final override;
106 
107 protected:
116  virtual void loadOnSubThread(std::atomic_bool &cancel) = 0;
119  virtual void updateOnMainThread() = 0;
120 
124  void startLoadThread();
128  bool isLoading() const;
129 
130 private:
131  std::atomic_bool m_cancel = false;
132  std::future<void> m_future;
133 
134  void updateLoadStatus();
135 };
136 
137 } // namespace scene
138 
139 
143 std::vector<std::wstring> parseCommandLine();
144 
149 inline bool keyPressedAsync(int vKey)
150 {
151  short ret = ::GetAsyncKeyState(vKey);
152  return (ret & 0x8000) != 0;
153 }
154 
155 
158 
159 template <class T>
160 class Resource : private util::noncopyable {
161 public:
162  using PtrType = std::shared_ptr<T>;
163  using LoadFuncType = std::function<PtrType()>;
164 
165  explicit Resource(LoadFuncType loadFunc_) : m_loadFunc(loadFunc_) {}
166  ~Resource() = default;
167 
168  const PtrType getPtr() const
169  {
170  std::lock_guard<std::mutex> lock(m_lock);
171  if (m_resPtr == nullptr) {
172  throwTrace<FrameworkError>("Resource not loaded");
173  }
174  return m_resPtr;
175  }
176  void load()
177  {
178  {
179  std::lock_guard<std::mutex> lock(m_lock);
180  if (m_resPtr != nullptr) {
181  return;
182  }
183  m_loading = true;
184  }
185  auto res = m_loadFunc();
186  {
187  std::lock_guard<std::mutex> lock(m_lock);
188  if (!m_loading) {
189  throwTrace<FrameworkError>("Multiple load async detected");
190  }
191  m_loading = false;
192  m_resPtr = res;
193  }
194  }
195  void unload() {
196  std::lock_guard<std::mutex> lock(m_lock);
197  if (!m_loading) {
198  throwTrace<FrameworkError>("Unload resource while loading");
199  }
200  m_resPtr.reset();
201  }
202 
203 private:
204  mutable std::mutex m_lock;
205  bool m_loading = false;
206  PtrType m_resPtr;
207  LoadFuncType m_loadFunc;
208 };
209 
210 
212 public:
213  explicit ResourceManager(size_t resSetCount = 1);
214  ~ResourceManager() = default;
215 
216  void addTexture(size_t setId, const char *resId,
217  std::function<graphics::DGraphics::TextureResourcePtr()> loadFunc);
218  void addFont(size_t setId, const char *resId,
219  std::function<graphics::DGraphics::FontResourcePtr()> loadFunc);
220  void addSoundEffect(size_t setId, const char *resId,
221  std::function<sound::XAudio2::SeResourcePtr()> loadFunc);
222  void addBgm(size_t setId, const char *resId,
223  std::function<sound::XAudio2::BgmResourcePtr()> loadFunc);
224 
225  void setSealed(bool sealed);
226  bool isSealed();
227 
228  void loadResourceSet(size_t setId, std::atomic_bool &cancel);
229  void unloadResourceSet(size_t setId);
230 
232  size_t setId, const char *resId) const;
234  size_t setId, const char *resId) const;
235  const sound::XAudio2::SeResourcePtr getSoundEffect(
236  size_t setId, const char *resId) const;
237  const sound::XAudio2::BgmResourcePtr getBgm(
238  size_t setId, const char *resId) const;
239 
240 private:
241  bool m_sealed = true;
242 
243  // int setId -> char[16] resId -> Resource<T>
244  template <class T>
245  using ResMapVec = std::vector<std::unordered_map<IdString, Resource<T>>>;
246 
247  ResMapVec<graphics::DGraphics::TextureResource> m_texMapVec;
248  ResMapVec<graphics::DGraphics::FontResource> m_fontMapVec;
249  ResMapVec<sound::XAudio2::SeResource> m_seMapVec;
250  ResMapVec<sound::XAudio2::BgmResource> m_bgmMapVec;
251 };
252 
254 public:
255  FrameControl(uint32_t fps, uint32_t skipCount);
256  ~FrameControl() = default;
257  bool shouldSkipFrame() const;
258  void endFrame();
259  double getFramePerSec() const;
260 
261 private:
262  int64_t m_freq;
263  int64_t m_counterPerFrame;
264  int64_t m_base = 0;
265  uint32_t m_skipCount;
266  uint32_t m_frameCount = 0;
267 
268  double m_fps = 0.0;
269  uint32_t m_fpsPeriod;
270  uint32_t m_fpsCount = 0;
271  int64_t m_fpsBase = 0;
272  uint32_t m_fpsFrameAcc = 0;
273 };
274 
278 struct AppParam {
280  HINSTANCE hInstance = nullptr;
282  int nCmdShow = SW_SHOW;
284  const wchar_t *wndClsName = L"GameWndCls";
286  const wchar_t *title = L"GameApp";
288  HICON hIcon = nullptr;
290  HICON hIconSm = nullptr;
292  uint32_t frameSkip = 0;
294  bool showCursor = false;
295 };
296 
301 public:
309  Application(const AppParam &appParam, const graphics::GraphicsParam &graphParam,
310  size_t resSetCount);
313  virtual ~Application();
318  int run();
319 
322  HWND getHWnd() { return m_hWnd; }
325  void getGraphicsParam(graphics::GraphicsParam *param) { *param = m_graphParam; }
328  graphics::DGraphics &graph() { return *m_dg.get(); }
331  sound::XAudio2 &sound() { return *m_ds.get(); }
334  input::DInput &input() { return *m_di.get(); }
335 
341  void addTextureResource(size_t setId, const char *resId, const wchar_t *path);
351  void addFontResource(size_t setId, const char *resId,
352  const wchar_t *fontName, uint32_t startChar, uint32_t endChar,
353  uint32_t w, uint32_t h);
359  void addSeResource(size_t setId, const char *resId, const wchar_t *path);
365  void addBgmResource(size_t setId, const char *resId, const wchar_t *path);
366 
372  void sealResource(bool seal);
373 
380  void loadResourceSet(size_t setId, std::atomic_bool &cancel);
384  void unloadResourceSet(size_t setId);
385 
391  size_t setId, const char *resId) const;
397  size_t setId, const char *resId) const;
402  const sound::XAudio2::SeResourcePtr getSoundEffect(
403  size_t setId, const char *resId) const;
408  const sound::XAudio2::BgmResourcePtr getBgm(
409  size_t setId, const char *resId) const;
410 
411 protected:
415  virtual void init() = 0;
419  virtual void update() = 0;
423  virtual void render() = 0;
424 
425 private:
426  const UINT_PTR TimerEventId = 0xffff0001;
427 
428  HWND m_hWnd;
429  std::unique_ptr<graphics::DGraphics> m_dg;
430  std::unique_ptr<sound::XAudio2> m_ds;
431  std::unique_ptr<input::DInput> m_di;
432  ResourceManager m_resMgr;
433 
434  AppParam m_param;
435  graphics::GraphicsParam m_graphParam;
436  FrameControl m_frameCtrl;
437 
438  void initializeWindow();
439  static LRESULT CALLBACK wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
440  LRESULT onSize(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
441 
442  void onIdle();
443  void updateInternal();
444  void renderInternal();
445 };
446 
451 public:
452  explicit UnsealResource(Application &app) : m_app(app)
453  {
454  m_app.sealResource(false);
455  }
457  {
458  m_app.sealResource(true);
459  }
460 
461 private:
462  Application &m_app;
463 };
464 
465 } // namespace framework
466 } // namespace yappy
void getGraphicsParam(graphics::GraphicsParam *param)
Get graphics parameters.
Definition: framework.h:325
Noncopyable class.
Definition: util.h:24
int nextInt(int a, int b)
Get next int random number.
Definition: framework.cpp:34
std::shared_ptr< BgmResource > BgmResourcePtr
Definition: sound.h:78
double nextDouble(double a, double b)
Get next double random number.
Definition: framework.cpp:40
Debug utilities.
std::array< char, 16 > IdString
Fixed size char array for string ID.
Definition: util.h:35
sound::XAudio2 & sound()
Get XAudio2 manager.
Definition: framework.h:331
void setSeed(unsigned int seed)
Set random seed.
Definition: framework.cpp:24
Application parameters.
Definition: framework.h:278
DirectGraphics manager.
Definition: graphics.h:80
unsigned int nextRawUInt32()
Get next uint32 random number.
Definition: framework.cpp:29
graphics::DGraphics & graph()
Get DirectGraphics manager.
Definition: framework.h:328
Simple scene class base.
Definition: framework.h:78
Resource(LoadFuncType loadFunc_)
Definition: framework.h:165
Auto re-seal helper.
Definition: framework.h:450
bool keyPressedAsync(int vKey)
Get key state by GetAsyncKeyState().
Definition: framework.h:149
Definition: config.cpp:6
std::shared_ptr< FontResource > FontResourcePtr
Definition: graphics.h:85
std::shared_ptr< SeResource > SeResourcePtr
Definition: sound.h:76
HWND getHWnd()
Get HWND of the window managed by this class.
Definition: framework.h:322
Utilities.
char msg[LINE_DATA_SIZE-sizeof(LARGE_INTEGER)-sizeof(uint32_t)]
Definition: debug.cpp:159
input::DInput & input()
Get DirectInput manager.
Definition: framework.h:334
unsigned int generateRandomSeed()
Generate nondeterministic random number for seed.
Definition: framework.cpp:19
DirectInput8 wrapper.
Definition: input.h:16
std::shared_ptr< T > PtrType
Definition: framework.h:162
std::shared_ptr< TextureResource > TextureResourcePtr
Definition: graphics.h:83
std::vector< std::wstring > parseCommandLine()
Command line utility.
Definition: framework.cpp:109
util::IdString IdString
Resource ID is fixed-length string; char[16].
Definition: framework.h:157
Scene class with an async loading task.
Definition: framework.h:93
const PtrType getPtr() const
Definition: framework.h:168
std::function< PtrType()> LoadFuncType
Definition: framework.h:163
User application base, which manages a window and DirectX objects.
Definition: framework.h:300
UnsealResource(Application &app)
Definition: framework.h:452