forked from Shell4026/ShellEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.cpp
More file actions
127 lines (102 loc) · 2.29 KB
/
Copy pathWindow.cpp
File metadata and controls
127 lines (102 loc) · 2.29 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
125
126
127
#include "Window.h"
#include "WindowFactory.h"
#include <iostream>
#include <thread>
namespace sh::window {
Window::Window() :
width(wsize), height(hsize),
isOpen(false),
fps(60), maxFrameMs(1000.0f / static_cast<float>(fps)), deltaTime(0.0f),
wsize(0), hsize(0)
{
startTime = std::chrono::high_resolution_clock::now();
endTime = startTime;
}
Window::~Window()
{
this->Close();
}
void Window::Create(const std::string& title, int wsize, int hsize, StyleFlag style)
{
if (isOpen)
return;
std::cout << "Init\n";
this->title = title;
winImpl = WindowFactory::CreateWindowImpl();
handle = winImpl->Create(title, wsize, hsize, style);
this->wsize = wsize;
this->hsize = hsize;
isOpen = true;
}
bool Window::PollEvent(Event& event)
{
if (winImpl.get() == nullptr)
return false;
if (winImpl->IsEmptyEvent())
{
winImpl->ProcessEvent();
}
else
{
event = winImpl->PopEvent();
if (event.type == Event::EventType::Resize)
{
wsize = GetWidth();
hsize = GetHeight();
}
return true;
}
return false;
}
bool Window::IsOpen() const
{
return isOpen;
}
void Window::Close()
{
if (winImpl.get() == nullptr)
return;
isOpen = false;
winImpl.reset();
}
void Window::SetFps(unsigned int fps)
{
this->fps = fps;
maxFrameMs = 1000.0f / static_cast<float>(fps);
}
auto Window::GetDeltaTime() const -> float
{
return deltaTime;
}
void Window::ProcessFrame()
{
startTime = std::chrono::high_resolution_clock::now();
auto frameDuration = std::chrono::duration_cast<std::chrono::milliseconds>(startTime - endTime);
uint64_t deltaTimeMs = frameDuration.count();
int64_t freeTimeMs = maxFrameMs - deltaTimeMs;
if (freeTimeMs > 0)
{
winImpl->StopTimer(freeTimeMs);
}
endTime = std::chrono::high_resolution_clock::now();
auto sleepTimeMs = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime);
frameDuration += sleepTimeMs;
deltaTime = frameDuration.count() / 1000.f;
}
auto Window::GetNativeHandle() const -> WinHandle
{
return handle;
}
void Window::SetTitle(std::string_view title)
{
winImpl->SetTitle(title);
}
auto Window::GetWidth() const -> uint32_t
{
return winImpl->GetWidth();
}
auto Window::GetHeight() const -> uint32_t
{
return winImpl->GetHeight();
}
}