#include "dl_system.h" #if (DL_PLATFORM == DL_PLATFORM_WIN32) #include #pragma comment(lib, "Winmm.lib") #endif #if (DL_PLATFORM != DL_PLATFORM_ANDROID) // glfw必须包含在windows.h后面 #include #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 "ui/dl_ui_manager.h" namespace dl { System* g_system; class SystemImp { public: SystemImp() : _window{ 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) { //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); } static void CallbackChar(GLFWwindow* window, unsigned int 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); } 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) //------------------创建窗口------------------------ if (!glfwInit()) { log("glfw 初始化失败!"); return; } glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);//设置不使用OpenGL //glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);//设置窗口禁止修改大小 _imp->_monitor = glfwGetPrimaryMonitor(); log(fmt::format("当前显示器:{}", glfwGetMonitorName(_imp->_monitor))); _imp->_window = glfwCreateWindow((int)param._size[0], (int)param._size[1], param._strTitle.c_str(), nullptr, nullptr); if (!_imp->_window) { log("窗口创建失败!"); return; } //SetWindowCenter(); //设置大小变化回调 glfwSetFramebufferSizeCallback(_imp->_window, CallbackFrameBufferResize); //设置按键回调 glfwSetKeyCallback(_imp->_window, CallbackKey); //设置输入回调 glfwSetCharCallback(_imp->_window, CallbackChar); //设置光标位置回调 glfwSetCursorPosCallback(_imp->_window, CallbackCursorPos); //设置鼠标按键回调 glfwSetMouseButtonCallback(_imp->_window, CallbackMouseButton); #endif //------------------创建VK------------------------ g_vk = new VK(_imp->_window); g_factory->_init_path(); g_factory->_init_defualt_res(); //g_audio->_init_path(); g_ui->_resize();//初始化ui大小 if (_funcInit) _funcInit();//初始化函数 g_factory->_save_cache(); //-----------------游戏循环----------------------- #if (DL_PLATFORM == DL_PLATFORM_WIN32) timeBeginPeriod(1); #endif while (!glfwWindowShouldClose(_imp->_window)) { g_input->_update();//覆盖旧状态 glfwPollEvents(); g_physics->Update();//计算物理 g_ui->_pre_update(); g_ui->_update(); g_vk->GfxLock(); if (_funcRun) _funcRun();//帧函数 g_vk->GfxUnlock(); g_ui->_render(); //-----------------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); glfwTerminate(); #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"], j["model"], j["binary"], j["temp"]); //删除输出目录 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"]); }; 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(); Start(param_window); } 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::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 } 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); } }