feat(窗口): 实现窗口大小和位置调整功能
添加窗口大小和位置调整的回调处理,支持通过按键触发窗口尺寸和位置变化 完善Window类的setSize、setPos和pos方法实现,添加SDL窗口操作
This commit is contained in:
@@ -149,6 +149,12 @@ bool Application::initCoreModules() {
|
|||||||
return false;
|
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();
|
renderer_ = &Renderer::get();
|
||||||
if (!renderer_->init()) {
|
if (!renderer_->init()) {
|
||||||
|
|||||||
@@ -134,11 +134,21 @@ void Window::close() { shouldClose_ = true; }
|
|||||||
void Window::setTitle(const std::string &title) {}
|
void Window::setTitle(const std::string &title) {}
|
||||||
|
|
||||||
void Window::setSize(int w, int h) {
|
void Window::setSize(int w, int h) {
|
||||||
|
if (sdlWindow_) {
|
||||||
|
SDL_SetWindowSize(sdlWindow_, w, h);
|
||||||
|
}
|
||||||
width_ = w;
|
width_ = w;
|
||||||
height_ = h;
|
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; }
|
void Window::setFullscreen(bool fs) { fullscreen_ = fs; }
|
||||||
|
|
||||||
@@ -154,7 +164,14 @@ Size Window::size() const {
|
|||||||
return Size{static_cast<float>(width_), static_cast<float>(height_)};
|
return Size{static_cast<float>(width_), static_cast<float>(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<float>(x), static_cast<float>(y)};
|
||||||
|
}
|
||||||
|
return Vec2{0.0f, 0.0f};
|
||||||
|
}
|
||||||
|
|
||||||
bool Window::fullscreen() const { return fullscreen_; }
|
bool Window::fullscreen() const { return fullscreen_; }
|
||||||
|
|
||||||
|
|||||||
@@ -100,6 +100,18 @@ int main(int argc, char **argv) {
|
|||||||
if(ke.getButton() == 6) {
|
if(ke.getButton() == 6) {
|
||||||
Application::get().quit();
|
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;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user