Fix screen change mechanism; create method for counting screens

This commit is contained in:
weil 2024-07-09 16:58:12 +02:00
parent 57505fd06d
commit bef8baba5f
4 changed files with 53 additions and 2 deletions

View File

@ -51,6 +51,7 @@ void Obs::_bind_methods()
{ {
ClassDB::bind_method(D_METHOD("get_screen_frame"), &Obs::getEncodedScreenFrame); ClassDB::bind_method(D_METHOD("get_screen_frame"), &Obs::getEncodedScreenFrame);
ClassDB::bind_method(D_METHOD("render_frame"), &Obs::renderFrameToMesh); ClassDB::bind_method(D_METHOD("render_frame"), &Obs::renderFrameToMesh);
ClassDB::bind_method(D_METHOD("get_screen_count"), &Obs::getScreenCount);
} }
@ -176,4 +177,9 @@ void Obs::renderFrameToMesh(PackedByteArray frame, Ref<StandardMaterial3D> mat)
#endif #endif
} }
size_t Obs::getScreenCount() const
{
return microtaur::ScreenEnumerator().count();
}
} }

View File

@ -31,6 +31,8 @@ public:
PackedByteArray getEncodedScreenFrame(size_t id); PackedByteArray getEncodedScreenFrame(size_t id);
void renderFrameToMesh(PackedByteArray frame, Ref<StandardMaterial3D> mat); void renderFrameToMesh(PackedByteArray frame, Ref<StandardMaterial3D> mat);
size_t getScreenCount() const;
private: private:
bool m_initialized{false}; bool m_initialized{false};
bool m_decInitialized{false}; bool m_decInitialized{false};

View File

@ -18,8 +18,18 @@
using namespace godot; using namespace godot;
namespace microtaur namespace microtaur {
namespace {
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{ {
auto* count = reinterpret_cast<size_t*>(dwData);
(*count)++;
return TRUE;
}
}
class AcceleratedWindowCapturer class AcceleratedWindowCapturer
{ {
@ -64,7 +74,7 @@ public:
dxgiDevice->Release(); dxgiDevice->Release();
IDXGIOutput* dxgiOutput = nullptr; IDXGIOutput* dxgiOutput = nullptr;
hr = dxgiAdapter->EnumOutputs(1, &dxgiOutput); // TODO: screen choose hr = dxgiAdapter->EnumOutputs(m_currentScreen, &dxgiOutput);
if (FAILED(hr)) { if (FAILED(hr)) {
reset(); reset();
return; return;
@ -377,6 +387,17 @@ public:
init(); init();
} }
void setCurrentScreen(size_t target)
{
m_currentScreen = target;
reset();
}
size_t getCurrentScreen() const
{
return m_currentScreen;
}
private: private:
Microsoft::WRL::ComPtr<ID3D11Device> m_device; Microsoft::WRL::ComPtr<ID3D11Device> m_device;
Microsoft::WRL::ComPtr<ID3D11DeviceContext> m_context; Microsoft::WRL::ComPtr<ID3D11DeviceContext> m_context;
@ -390,6 +411,8 @@ private:
size_t m_width{}; size_t m_width{};
size_t m_height{}; size_t m_height{};
size_t m_currentScreen{};
}; };
@ -404,7 +427,21 @@ WindowCapturer::~WindowCapturer()
Frame WindowCapturer::capture(size_t id, size_t width, size_t height) Frame WindowCapturer::capture(size_t id, size_t width, size_t height)
{ {
if (id != m_impl->getCurrentScreen()) {
m_impl->setCurrentScreen(id);
}
return m_impl->nextFrame(width, height); return m_impl->nextFrame(width, height);
} }
size_t ScreenEnumerator::count()
{
size_t c = 0;
if (EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&c)) {
return c;
}
return -1;
}
} }

View File

@ -27,4 +27,10 @@ private:
}; };
class ScreenEnumerator
{
public:
static size_t count();
};
} }