From d97b4e69b928337640bf6638c3139a3677231204 Mon Sep 17 00:00:00 2001 From: Lenheart <947330670@qq.com> Date: Sun, 29 Mar 2026 12:33:10 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E7=AA=97=E5=8F=A3):=20=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E7=AA=97=E5=8F=A3=E5=A4=A7=E5=B0=8F=E5=92=8C=E4=BD=8D=E7=BD=AE?= =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加窗口大小和位置调整的回调处理,支持通过按键触发窗口尺寸和位置变化 完善Window类的setSize、setPos和pos方法实现,添加SDL窗口操作 --- .../src/frostbite2D/core/application.cpp | 6 ++++++ Frostbite2D/src/frostbite2D/core/window.cpp | 21 +++++++++++++++++-- Game/src/main.cpp | 12 +++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Frostbite2D/src/frostbite2D/core/application.cpp b/Frostbite2D/src/frostbite2D/core/application.cpp index 8a95377..494bf60 100644 --- a/Frostbite2D/src/frostbite2D/core/application.cpp +++ b/Frostbite2D/src/frostbite2D/core/application.cpp @@ -149,6 +149,12 @@ bool Application::initCoreModules() { return false; } + window_->onResize([this](int width, int height) { + renderer_->setViewport(0, 0, width, height); + camera_->setViewport(width, height); + SDL_Log("Window resized to %dx%d", width, height); + }); + // 初始化渲染器 renderer_ = &Renderer::get(); if (!renderer_->init()) { diff --git a/Frostbite2D/src/frostbite2D/core/window.cpp b/Frostbite2D/src/frostbite2D/core/window.cpp index 400276a..1bfc9e6 100644 --- a/Frostbite2D/src/frostbite2D/core/window.cpp +++ b/Frostbite2D/src/frostbite2D/core/window.cpp @@ -134,11 +134,21 @@ void Window::close() { shouldClose_ = true; } void Window::setTitle(const std::string &title) {} void Window::setSize(int w, int h) { + if (sdlWindow_) { + SDL_SetWindowSize(sdlWindow_, w, h); + } width_ = w; height_ = h; + if (resizeCb_) { + resizeCb_(w, h); + } } -void Window::setPos(int x, int y) {} +void Window::setPos(int x, int y) { + if (sdlWindow_) { + SDL_SetWindowPosition(sdlWindow_, x, y); + } +} void Window::setFullscreen(bool fs) { fullscreen_ = fs; } @@ -154,7 +164,14 @@ Size Window::size() const { return Size{static_cast(width_), static_cast(height_)}; } -Vec2 Window::pos() const { return Vec2{0.0f, 0.0f}; } +Vec2 Window::pos() const { + if (sdlWindow_) { + int x, y; + SDL_GetWindowPosition(sdlWindow_, &x, &y); + return Vec2{static_cast(x), static_cast(y)}; + } + return Vec2{0.0f, 0.0f}; +} bool Window::fullscreen() const { return fullscreen_; } diff --git a/Game/src/main.cpp b/Game/src/main.cpp index 72a19c4..0c5d7cb 100644 --- a/Game/src/main.cpp +++ b/Game/src/main.cpp @@ -100,6 +100,18 @@ int main(int argc, char **argv) { if(ke.getButton() == 6) { Application::get().quit(); } + if (ke.getButton() == 0) { + Application::get().getWindow()->setPos(0, 0); + SDL_Log("setPos ret=%d, %d", Application::get().getWindow()->pos().x, Application::get().getWindow()->pos().y); + + Application::get().getWindow()->setSize(1920, 1080); + } + if (ke.getButton() == 1) { + Application::get().getWindow()->setPos(0, 0); + + Application::get().getWindow()->setSize(1280, 720); + } + return true; });