#include "dl_system.h" #ifndef DL_DISABLE_RENDER #if (DL_PLATFORM == DL_PLATFORM_WIN32) #include #pragma comment(lib, "Winmm.lib") #endif #if (DL_PLATFORM != DL_PLATFORM_ANDROID) // glfw必须包含在windows.h后面 #include #if (DL_PLATFORM == DL_PLATFORM_WIN32) #define GLFW_EXPOSE_NATIVE_WIN32 #include #elif (DL_PLATFORM == DL_PLATFORM_LINUX) #define GLFW_EXPOSE_NATIVE_X11 #include #undef Bool #endif #endif #include "io/dl_log.h" #include "dl_time.h" #include "vk/dl_vk.h" #include "io/dl_json.h" #include "render/dl_factory.h" #include "io/dl_input.h" #include "render/dl_physics.h" #include "render/dl_action.h" #include "ui/dl_ui_manager.h" #include "system/dl_audio.h" #include "ui/dl_ui_scene.h" #include "script/dl_lua.h" namespace dl { System* g_system; class SystemImp { public: SystemImp() : _window{ nullptr }, _monitor{ nullptr }, _bClearCursorDt{ false } {} GLFWwindow* _window; GLFWmonitor* _monitor;//主显示器 //是否清空光标差值 bool _bClearCursorDt; }; //大小变化回调函数(创建窗口不会调用) static void CallbackFrameBufferResize(GLFWwindow* window, int width, int height) { g_vk->SetFrameBufferResize(); g_ui->_resize(); } static void CallbackKey(GLFWwindow* window, int key, int scancode, int action, int mods) { #if DL_PLATFORM != DL_PLATFORM_ANDROID //action GLFW_PRESS //mods GLFW_MOD_SHIFT /*if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GLFW_TRUE);*/ if (key < 0) { log_msg0("未知按键消息"); return; } g_input->_set_key(key, action); if (action == GLFW_REPEAT || action == GLFW_PRESS) { g_ui->_on_key(key, mods & GLFW_MOD_CONTROL); } /*mods& GLFW_MOD_CONTROL, mods& GLFW_MOD_SHIFT, mods& GLFW_MOD_ALT, mods& GLFW_MOD_SUPER, mods& GLFW_MOD_CAPS_LOCK, mods& GLFW_MOD_NUM_LOCK*/ #endif } static void CallbackChar(GLFWwindow* window, unsigned int codepoint) { g_ui->_on_char((char32_t)codepoint); //string str = CodeCvt::cvt_wc_u8(std::wstring_view((wchar_t*)&codepoint, 1)); //debug_test(format("打字:{}", str)); } static void CallbackCursorPos(GLFWwindow* window, double xpos, double ypos) { bool& clear = g_system->GetImp()->_bClearCursorDt; g_input->_set_cursor((int)xpos, (int)ypos, clear); clear = false; //debug_test(format("光标移动:{}, {}", xpos, ypos)); } static void CallbackMouseButton(GLFWwindow* window, int button, int action, int mods) { /*if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) popup_menu();*/ g_input->_set_key_mouse(button, action); } static void CallbackScroll(GLFWwindow* window, double dx, double dy) { g_input->_set_scroll(dx, -dy); //log_test("滚动操作:{}, {}", dx, -dy); } void CallbackDrop(GLFWwindow* window, int count, const char** paths) { for (int i = 0; i < count; ++i) g_system->_add_drop_file(String::CvtPathSeparator(paths[i])); } System::System() : _funcInit{ nullptr }, _funcRun{ nullptr } { _imp = new SystemImp; } System::~System() { delete _imp; } void System::Start(const ParamWindow& param) { #if (DL_PLATFORM != DL_PLATFORM_ANDROID) //------------------创建窗口------------------------ _imp->_window = glfwCreateWindow((int)param._size[0], (int)param._size[1], param._strTitle.c_str(), nullptr, nullptr); if (!_imp->_window) { log_err("窗口创建失败!"); return; } //SetWindowCenter(); // 设置大小变化回调 glfwSetFramebufferSizeCallback(_imp->_window, CallbackFrameBufferResize); // 设置按键回调 glfwSetKeyCallback(_imp->_window, CallbackKey); // 设置输入回调 glfwSetCharCallback(_imp->_window, CallbackChar); // 设置光标位置回调 glfwSetCursorPosCallback(_imp->_window, CallbackCursorPos); // 设置鼠标按键回调 glfwSetMouseButtonCallback(_imp->_window, CallbackMouseButton); // 设置滚动回调 glfwSetScrollCallback(_imp->_window, CallbackScroll); // 拖入文件回调 glfwSetDropCallback(_imp->_window, CallbackDrop); #elif DL_PLATFORM == DL_PLATFORM_ANDROID _imp->_window = new GLFWwindow; _imp->_monitor = new GLFWmonitor; #endif //------------------创建VK------------------------ g_vk = new VK(_imp->_window); g_factory->_init_path(); g_factory->_init_defualt_res(); g_audio->_load(); g_ui->_resize(); // 初始化ui大小 if (_funcInit) _funcInit(); // 初始化函数 g_factory->_save_cache(); //-----------------游戏循环----------------------- #if (DL_PLATFORM == DL_PLATFORM_WIN32) timeBeginPeriod(1); #endif // 获取窗口句柄 glfwWindowShouldClose(_imp->_window); #if DL_PLATFORM == DL_PLATFORM_WIN32 _nativeWindow = glfwGetWin32Window(_imp->_window); #elif DL_PLATFORM == DL_PLATFORM_LINUX //_nativeWindow = glfwGetX11Window(_imp->_window); #elif DL_PLATFORM == DL_PLATFORM_ANDROID #endif //glfwGetCocoaWindow macos // while (!glfwWindowShouldClose(_imp->_window)) { g_time->_frame._timeStamp = Time::GetTimeStamp(); g_time->_frame._timeStampMill = Time::GetTimeStampMillisecond(); g_input->_update();// 覆盖旧状态 glfwPollEvents(); g_input->_update_late(); g_physics->Update();// 计算物理 g_ui->_update_pre(); g_ui->_update(); g_action->_update(); g_scene->_update_pre(); g_vk->GfxLock(); g_scene->_update(); if (_funcRun) _funcRun();//帧函数 if (!_lua._funcUpdate.empty()) { g_lua->ExcuteFunction(_lua._funcUpdate); } g_ui->_render(); g_scene->_render(); g_vk->GfxUnlock(); //-----------------vk------------------ std::vector list_dc; std::vector list_dc_translucent; g_vk->Update(list_dc, list_dc_translucent); g_vk->Render(_imp->_window, list_dc, list_dc_translucent); // g_time->_sleep();// 延时 g_time->_update();//计时 } //----------------资源释放------------------------ g_vk->Release(); delete g_vk; glfwDestroyWindow(_imp->_window); #if (DL_PLATFORM == DL_PLATFORM_WIN32) timeEndPeriod(1); #endif } void System::Start(std::string_view json_window, std::string_view json_path) { auto fn_load_path = [&](){ if (json_path.empty()) return; json j = Json::Parse(json_path); if (j.is_null()) return; g_factory->SetPath( j["image"].get(), j["model"].get(), j["binary"].get(), j["temp"].get()); //删除输出目录 bool delete_binary = false; bool delete_2d = false; bool delete_3d = false; Json::Get(j, "delete_binary", delete_binary); Json::Get(j, "delete_2d", delete_2d); Json::Get(j, "delete_3d", delete_3d); g_factory->DeleteBinary(delete_binary, delete_2d, delete_3d); g_audio->SetPath(j["audio"].get()); }; ParamWindow param_window; auto fn_load_window = [&]() { if (json_window.empty()) return; json j = Json::Parse(json_window); if (j.is_null()) return; Json::Get(j, "title", param_window._strTitle); Json::Get(j, "size", param_window._size); }; fn_load_path(); fn_load_window(); return Start(param_window); } void System::Exit() { glfwSetWindowShouldClose(_imp->_window, 1); } void System::SetCursorPosition(Point xy) { glfwSetCursorPos(_imp->_window, xy[0], xy[1]); } Size System::GetWindowSize() { int width, height; glfwGetWindowSize(_imp->_window, &width, &height); return toSizeNeg2Zero(Point{ width, height }); } Size System::GetWorkArea() { int x, y, w, h; glfwGetMonitorWorkarea(_imp->_monitor, &x, &y, &w, &h); return { (unsigned)w, (unsigned)h }; } void System::SetWindowPoint(const Point& xy) { glfwSetWindowPos(_imp->_window, xy[0], xy[1]); } void System::SetWindowCenter() { Size s = GetWorkArea(); Size size_window = GetWindowSize(); Point xy = (toPoint(s) - toPoint(size_window)) / 2; SetWindowPoint(xy); } void System::SetWindowTitle(std::string_view str) { #if DL_PLATFORM != DL_PLATFORM_ANDROID glfwSetWindowTitle(_imp->_window, str.data()); #endif } bool System::IsWindowExist() { return _imp->_window; } void System::SetCursorMode(CursorMode mode) { #if (DL_PLATFORM == DL_PLATFORM_ANDROID) assert(0); return; #else if (_cursorMode == mode) return; _imp->_bClearCursorDt = true; _cursorMode = mode; switch (mode) { case CursorMode::NORMAL: glfwSetInputMode(_imp->_window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); break; case CursorMode::HIDE: glfwSetInputMode(_imp->_window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); break; case CursorMode::DISABLE: glfwSetInputMode(_imp->_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); break; default: assert(0); break; } #endif } void System::SetClipboardString(std::string_view str) { glfwSetClipboardString(nullptr, str.data()); } std::string System::GetClipboardString() { const char* str = glfwGetClipboardString(nullptr); if (!str) return {}; return str; } void System::ShowMessageBox(std::string_view title, std::string_view text) { #if DL_PLATFORM == DL_PLATFORM_WIN32 ::MessageBoxW((HWND)_nativeWindow, CodeCvt::Utf8ToWideChar(text).c_str(), CodeCvt::Utf8ToWideChar(title).c_str(), MB_OK); #else assert(0); #endif } void System::_init_glfw() { #if (DL_PLATFORM != DL_PLATFORM_ANDROID) //------------------创建窗口------------------------ if (!glfwInit()) { log_err("glfw 初始化失败!"); return; } glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);//设置不使用OpenGL //glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);//设置窗口禁止修改大小 _imp->_monitor = glfwGetPrimaryMonitor(); log_msg("当前显示器:{}", glfwGetMonitorName(_imp->_monitor)); #endif } void System::_delete_glfw() { glfwTerminate(); } void System::lua_SetFuncUpdate(std::string_view str) { _lua._funcUpdate = str; } const RenderInfo2D& System::GetRenderInfo2D() { return g_vk->GetRenderInfo2D(); } const RenderInfo3D& System::GetRenderInfo3D() { return g_vk->GetRenderInfo3D(); } void System::ScreenShot(std::string_view path_name) { g_vk->ScreenShot(path_name); } } #endif