31 using error::throwTrace;
32 using error::FrameworkError;
50 void setSeed(
unsigned int seed);
62 int nextInt(
int a = 0,
int b = std::numeric_limits<int>::max());
69 double nextDouble(
double a = 0.0,
double max = 1.0);
86 virtual void update() = 0;
88 virtual void render() = 0;
105 virtual void update()
final override;
116 virtual void loadOnSubThread(std::atomic_bool &cancel) = 0;
119 virtual void updateOnMainThread() = 0;
124 void startLoadThread();
128 bool isLoading()
const;
131 std::atomic_bool m_cancel =
false;
132 std::future<void> m_future;
134 void updateLoadStatus();
151 short ret = ::GetAsyncKeyState(vKey);
152 return (ret & 0x8000) != 0;
170 std::lock_guard<std::mutex> lock(m_lock);
171 if (m_resPtr ==
nullptr) {
172 throwTrace<FrameworkError>(
"Resource not loaded");
179 std::lock_guard<std::mutex> lock(m_lock);
180 if (m_resPtr !=
nullptr) {
185 auto res = m_loadFunc();
187 std::lock_guard<std::mutex> lock(m_lock);
189 throwTrace<FrameworkError>(
"Multiple load async detected");
196 std::lock_guard<std::mutex> lock(m_lock);
198 throwTrace<FrameworkError>(
"Unload resource while loading");
204 mutable std::mutex m_lock;
205 bool m_loading =
false;
216 void addTexture(
size_t setId,
const char *resId,
218 void addFont(
size_t setId,
const char *resId,
220 void addSoundEffect(
size_t setId,
const char *resId,
222 void addBgm(
size_t setId,
const char *resId,
225 void setSealed(
bool sealed);
228 void loadResourceSet(
size_t setId, std::atomic_bool &cancel);
229 void unloadResourceSet(
size_t setId);
232 size_t setId,
const char *resId)
const;
234 size_t setId,
const char *resId)
const;
236 size_t setId,
const char *resId)
const;
238 size_t setId,
const char *resId)
const;
241 bool m_sealed =
true;
245 using ResMapVec = std::vector<std::unordered_map<IdString, Resource<T>>>;
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;
257 bool shouldSkipFrame()
const;
259 double getFramePerSec()
const;
263 int64_t m_counterPerFrame;
265 uint32_t m_skipCount;
266 uint32_t m_frameCount = 0;
269 uint32_t m_fpsPeriod;
270 uint32_t m_fpsCount = 0;
271 int64_t m_fpsBase = 0;
272 uint32_t m_fpsFrameAcc = 0;
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;
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);
372 void sealResource(
bool seal);
380 void loadResourceSet(
size_t setId, std::atomic_bool &cancel);
384 void unloadResourceSet(
size_t setId);
391 size_t setId,
const char *resId)
const;
397 size_t setId,
const char *resId)
const;
403 size_t setId,
const char *resId)
const;
409 size_t setId,
const char *resId)
const;
415 virtual void init() = 0;
419 virtual void update() = 0;
423 virtual void render() = 0;
426 const UINT_PTR TimerEventId = 0xffff0001;
429 std::unique_ptr<graphics::DGraphics> m_dg;
430 std::unique_ptr<sound::XAudio2> m_ds;
431 std::unique_ptr<input::DInput> m_di;
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);
443 void updateInternal();
444 void renderInternal();
454 m_app.sealResource(
false);
458 m_app.sealResource(
true);
void getGraphicsParam(graphics::GraphicsParam *param)
Get graphics parameters.
int nextInt(int a, int b)
Get next int random number.
std::shared_ptr< BgmResource > BgmResourcePtr
double nextDouble(double a, double b)
Get next double random number.
std::array< char, 16 > IdString
Fixed size char array for string ID.
sound::XAudio2 & sound()
Get XAudio2 manager.
void setSeed(unsigned int seed)
Set random seed.
unsigned int nextRawUInt32()
Get next uint32 random number.
graphics::DGraphics & graph()
Get DirectGraphics manager.
Resource(LoadFuncType loadFunc_)
bool keyPressedAsync(int vKey)
Get key state by GetAsyncKeyState().
std::shared_ptr< FontResource > FontResourcePtr
std::shared_ptr< SeResource > SeResourcePtr
HWND getHWnd()
Get HWND of the window managed by this class.
char msg[LINE_DATA_SIZE-sizeof(LARGE_INTEGER)-sizeof(uint32_t)]
input::DInput & input()
Get DirectInput manager.
unsigned int generateRandomSeed()
Generate nondeterministic random number for seed.
std::shared_ptr< T > PtrType
std::shared_ptr< TextureResource > TextureResourcePtr
std::vector< std::wstring > parseCommandLine()
Command line utility.
util::IdString IdString
Resource ID is fixed-length string; char[16].
Scene class with an async loading task.
const PtrType getPtr() const
std::function< PtrType()> LoadFuncType
User application base, which manages a window and DirectX objects.
UnsealResource(Application &app)