Skip to content

Commit

Permalink
polish render code
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Nov 21, 2023
1 parent 6980d56 commit 29f27b9
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 71 deletions.
5 changes: 3 additions & 2 deletions include/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "helpers/nfd.h"
#include "helpers/utils.h"

#define PLAYER_NAME "ImPlay"

namespace ImPlay {
class Player {
public:
Expand Down Expand Up @@ -63,7 +65,7 @@ class Player {
void writeMpvConf();

void draw();
void drawLogo();
void drawVideo();
void execute(int n_args, const char **args_);

void openFileDlg(NFD::Filters filters, bool append = false);
Expand Down Expand Up @@ -99,7 +101,6 @@ class Player {
virtual void SetWindowPos(int x, int y) = 0;
virtual void GetWindowSize(int *w, int *h) = 0;
virtual void SetWindowSize(int w, int h) = 0;
virtual std::string GetWindowTitle() = 0;
virtual void SetWindowTitle(std::string) = 0;
virtual void SetWindowAspectRatio(int num, int den) = 0;
virtual bool GetWindowMaximized() = 0;
Expand Down
4 changes: 1 addition & 3 deletions include/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Window : Player {
void run();

private:
void initGLFW();
void wakeup();
void updateCursor();

Expand All @@ -33,7 +34,6 @@ class Window : Player {
void sendKeyEvent(std::string key, bool action);
void translateMod(std::vector<std::string> &keys, int mods);

GLFWwindow *createWindow();
void installCallbacks(GLFWwindow *target);
GLFWmonitor *getMonitor(GLFWwindow *target);

Expand All @@ -54,7 +54,6 @@ class Window : Player {
void SetWindowPos(int x, int y) override;
void GetWindowSize(int *w, int *h) override;
void SetWindowSize(int w, int h) override;
std::string GetWindowTitle() override;
void SetWindowTitle(std::string title) override;
void SetWindowAspectRatio(int num, int den) override;
bool GetWindowMaximized() override;
Expand All @@ -65,7 +64,6 @@ class Window : Player {
void SetWindowFullscreen(bool fs) override;
void SetWindowShouldClose(bool c) override;

std::string title = "ImPlay";
GLFWwindow *window = nullptr;
bool ownCursor = true;
double lastInputAt = 0;
Expand Down
Binary file modified resources/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 30 additions & 31 deletions source/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ bool Player::init(std::map<std::string, std::string> &options) {
}

void Player::draw() {
drawLogo();
drawVideo();

about->draw();
debug->draw();
Expand All @@ -90,16 +90,18 @@ void Player::draw() {
drawDialog();
}

void Player::drawLogo() {
if (logoTexture == nullptr || mpv->forceWindow || !idle) return;
void Player::drawVideo() {
auto vp = ImGui::GetMainViewport();
auto drawList = ImGui::GetBackgroundDrawList(vp);

ImGuiViewport *vp = ImGui::GetMainViewport();
const float width = std::min(vp->WorkSize.x, vp->WorkSize.y) * 0.1f;
const ImVec2 delta(width, width);
const ImVec2 center = vp->GetCenter();
ImRect bb(center - delta, center + delta);

ImGui::GetBackgroundDrawList(vp)->AddImage(logoTexture, bb.Min, bb.Max);
if (!idle) {
ImTextureID texture = reinterpret_cast<ImTextureID>(static_cast<intptr_t>(tex));
drawList->AddImage(texture, vp->WorkPos, vp->WorkPos + vp->WorkSize);
} else if (logoTexture != nullptr && !mpv->forceWindow) {
const ImVec2 center = vp->GetWorkCenter();
const ImVec2 delta(64, 64);
drawList->AddImage(logoTexture, center - delta, center + delta);
}
}

void Player::render() {
Expand Down Expand Up @@ -132,16 +134,9 @@ void Player::render() {
}
#endif

if (!idle) {
ImGuiViewport *vp = ImGui::GetMainViewport();
ImTextureID texture = reinterpret_cast<ImTextureID>(static_cast<intptr_t>(tex));
ImGui::GetBackgroundDrawList(vp)->AddImage(texture, vp->Pos, vp->Pos + vp->Size);
}

draw();
ImGui::Render();

const auto targetFps = config->Data.Interface.Fps;
{
ContextGuard guard(this);
GetFramebufferSize(&width, &height);
Expand All @@ -151,14 +146,19 @@ void Player::render() {
glClear(GL_COLOR_BUFFER_BIT);

ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SetSwapInterval(targetFps > 60 ? 0 : 1);
SetSwapInterval(config->Data.Interface.Fps > 60 ? 0 : 1);
SwapBuffers();
mpv->reportSwap();

if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
}
}

// limit fps while player idle
static double time = 0;
double targetDelta = 1.0f / targetFps;
double targetDelta = 1.0f / config->Data.Interface.Fps;
if (idle || mpv->pause) {
double delta = time - ImGui::GetTime();
if (delta > 0 && delta < targetDelta)
Expand All @@ -167,14 +167,6 @@ void Player::render() {
time = ImGui::GetTime();
}
time += targetDelta;

if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
ImGui::UpdatePlatformWindows();

std::lock_guard<std::mutex> lock(contextLock);
ImGui::RenderPlatformWindowsDefault();
DeleteContext();
}
}

void Player::renderVideo() {
Expand Down Expand Up @@ -330,9 +322,14 @@ void Player::onIconifyEvent(bool iconified) {
void Player::onSizeEvent(int w, int h) {
bool m = GetWindowMaximized();
if (maximized != m) mpv->property("window-maximized", m ? "yes" : "no");
auto g = ImGui::GetCurrentContext();
if (g != nullptr && !g->WithinFrameScope) render();
}

void Player::onPosEvent(int x, int y) {}
void Player::onPosEvent(int x, int y) {
auto g = ImGui::GetCurrentContext();
if (g != nullptr && !g->WithinFrameScope) render();
}

void Player::onCursorEvent(double x, double y) {
std::string xs = std::to_string((int)x);
Expand Down Expand Up @@ -395,7 +392,7 @@ void Player::initObservers() {
mpv->observeProperty<int, MPV_FORMAT_FLAG>("idle-active", [this](int flag) {
idle = static_cast<bool>(flag);
if (idle) {
SetWindowTitle(GetWindowTitle());
SetWindowTitle(PLAYER_NAME);
SetWindowAspectRatio(-1, -1);
}
});
Expand Down Expand Up @@ -453,7 +450,7 @@ void Player::writeMpvConf() {

std::filesystem::create_directories(scrips);
std::filesystem::create_directories(scriptOpts);

auto oscLua = scrips / "osc.lua";

if (!std::filesystem::exists(oscLua)) {
Expand Down Expand Up @@ -665,7 +662,9 @@ void Player::drawOpenURL() {
}

void Player::drawDialog() {
if (m_dialog) ImGui::OpenPopup(m_dialog_title.c_str());
if (!m_dialog) return;
ImGui::OpenPopup(m_dialog_title.c_str());

ImGui::SetNextWindowSize(ImVec2(scaled(30), 0), ImGuiCond_Always);
ImGui::SetNextWindowPos(ImGui::GetMainViewport()->GetWorkCenter(), ImGuiCond_Always, ImVec2(0.5f, 0.5f));
if (ImGui::BeginPopupModal(m_dialog_title.c_str(), &m_dialog, ImGuiWindowFlags_NoMove)) {
Expand Down
65 changes: 30 additions & 35 deletions source/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,9 @@

namespace ImPlay {
Window::Window(Config* config) : Player(config) {
glfwSetErrorCallback(
[](int error, const char* desc) { fmt::print(fg(fmt::color::red), "GLFW [{}]: {}\n", error, desc); });
#ifdef GLFW_PATCHED
glfwInitHint(GLFW_WIN32_MESSAGES_IN_FIBER, GLFW_TRUE);
#endif
if (!glfwInit()) throw std::runtime_error("Failed to initialize GLFW!");

window = createWindow();
initGLFW();
window = glfwCreateWindow(1280, 720, PLAYER_NAME, nullptr, nullptr);
if (window == nullptr) throw std::runtime_error("Failed to create window!");
glfwSetWindowSizeLimits(window, 640, 480, GLFW_DONT_CARE, GLFW_DONT_CARE);
installCallbacks(window);

initGui();
Expand All @@ -42,6 +35,33 @@ Window::~Window() {
glfwTerminate();
}

void Window::initGLFW() {
glfwSetErrorCallback(
[](int error, const char* desc) { fmt::print(fg(fmt::color::red), "GLFW [{}]: {}\n", error, desc); });
#ifdef GLFW_PATCHED
glfwInitHint(GLFW_WIN32_MESSAGES_IN_FIBER, GLFW_TRUE);
#endif
if (!glfwInit()) throw std::runtime_error("Failed to initialize GLFW!");

#if defined(IMGUI_IMPL_OPENGL_ES3)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
#elif defined(__APPLE__)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#else
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
glfwWindowHint(GLFW_AUTO_ICONIFY, GLFW_FALSE);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
}

bool Window::init(OptionParser& parser) {
mpv->wakeupCb() = [this](Mpv* ctx) { wakeup(); };
mpv->updateCb() = [this](Mpv* ctx) {
Expand Down Expand Up @@ -131,27 +151,6 @@ void Window::updateCursor() {
ImGui::SetMouseCursor(cursor ? ImGuiMouseCursor_Arrow : ImGuiMouseCursor_None);
}

GLFWwindow* Window::createWindow() {
#if defined(IMGUI_IMPL_OPENGL_ES3)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
#elif defined(__APPLE__)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#else
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
glfwWindowHint(GLFW_AUTO_ICONIFY, GLFW_FALSE);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
return glfwCreateWindow(640, 480, title.c_str(), nullptr, nullptr);
}

void Window::installCallbacks(GLFWwindow* target) {
glfwSetWindowUserPointer(target, this);

Expand All @@ -171,12 +170,10 @@ void Window::installCallbacks(GLFWwindow* target) {
glfwSetWindowSizeCallback(target, [](GLFWwindow* window, int w, int h) {
auto win = static_cast<Window*>(glfwGetWindowUserPointer(window));
win->onSizeEvent(w, h);
win->render();
});
glfwSetWindowPosCallback(target, [](GLFWwindow* window, int x, int y) {
auto win = static_cast<Window*>(glfwGetWindowUserPointer(window));
win->onPosEvent(x, y);
win->render();
});
glfwSetCursorEnterCallback(target, [](GLFWwindow* window, int entered) {
auto win = static_cast<Window*>(glfwGetWindowUserPointer(window));
Expand All @@ -194,7 +191,7 @@ void Window::installCallbacks(GLFWwindow* target) {
#endif
win->onCursorEvent(x, y);
#ifdef GLFW_PATCHED
if (win->mpv->allowDrag() && win->height - y > 150) { // 150: height of the OSC bar
if (win->mpv->allowDrag() && win->height - y > 280) { // 280: height of the OSC bar
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) glfwDragWindow(window);
}
#endif
Expand Down Expand Up @@ -305,8 +302,6 @@ void Window::GetWindowSize(int* w, int* h) { glfwGetWindowSize(window, w, h); }

void Window::SetWindowSize(int w, int h) { glfwSetWindowSize(window, w, h); }

std::string Window::GetWindowTitle() { return title; }

void Window::SetWindowTitle(std::string title) { glfwSetWindowTitle(window, title.c_str()); }

void Window::SetWindowAspectRatio(int num, int den) { glfwSetWindowAspectRatio(window, num, den); }
Expand Down

0 comments on commit 29f27b9

Please sign in to comment.