-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWindow.cpp
More file actions
124 lines (110 loc) · 2.48 KB
/
Copy pathWindow.cpp
File metadata and controls
124 lines (110 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "Window.h"
#include "WindowFactory.h"
#include <iostream>
#include <thread>
namespace sh::window {
Window::Window()
{
}
Window::~Window()
{
this->Close();
}
SH_WINDOW_API void Window::Create(const std::string& title, int wsize, int hsize, StyleFlag style)
{
if (bOpen)
return;
std::cout << "Init\n";
this->title = title;
winImpl = WindowFactory::CreateWindowImpl();
handle = winImpl->Create(title, wsize, hsize, style);
bOpen = true;
}
SH_WINDOW_API bool Window::PollEvent(Event& event)
{
if (winImpl.get() == nullptr)
return false;
if (winImpl->IsEmptyEvent())
{
winImpl->ProcessEvent();
}
else
{
event = winImpl->PopEvent();
return true;
}
return false;
}
SH_WINDOW_API void Window::Close()
{
if (winImpl.get() == nullptr)
return;
bOpen = false;
winImpl.reset();
}
SH_WINDOW_API void Window::SetFps(unsigned int fps)
{
this->fps = fps;
}
SH_WINDOW_API void Window::ProcessFrame()
{
using Clock = std::chrono::steady_clock;
static bool bInit = false;
static Clock::time_point last;
static Clock::time_point next;
const auto now0 = Clock::now();
if (!bInit)
{
last = now0;
next = now0;
bInit = true;
}
const auto target = std::chrono::microseconds(1'000'000 / fps); // 144fps = 6944us
next += target;
if (now0 < next)
{
const auto remainUs = std::chrono::duration_cast<std::chrono::microseconds>(next - now0).count();
// 2ms보다 크게 남은 경우 sleep하고 나머지는 busy wait으로 맞춤
if (remainUs > 2000) // 2ms
{
const uint32_t ms = (uint32_t)((remainUs - 1000) / 1000); // 마지막 1ms는 남겨둠
if (ms > 0)
{
if (bUsingSysTimer)
winImpl->StopTimer(ms);
else
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
}
while (Clock::now() < next)
std::this_thread::yield();
}
else
{
next = Clock::now();
}
const auto now1 = Clock::now();
deltaTime = std::chrono::duration<double>(now1 - last).count();
last = now1;
}
SH_WINDOW_API void Window::SetTitle(std::string_view title)
{
winImpl->SetTitle(title);
}
SH_WINDOW_API void Window::SetSize(int width, int height)
{
winImpl->Resize(width, height);
}
SH_WINDOW_API void Window::UseSystemTimer(bool bUse)
{
bUsingSysTimer = bUse;
}
SH_WINDOW_API auto Window::GetWidth() const -> uint32_t
{
return winImpl->GetWidth();
}
SH_WINDOW_API auto Window::GetHeight() const -> uint32_t
{
return winImpl->GetHeight();
}
}//namespace