在SFML 2.0中调整大小 [英] Resizing in SFML 2.0

查看:115
本文介绍了在SFML 2.0中调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个简单的问题,但是我很难找到一个直接的答案:有没有办法在SFML 2.0中调整已加载的Texture的大小?例如,如果我有一个3264x2448 png,并且我想将其按比例缩小以适合900x1200渲染的窗口而不进行裁剪,该怎么办?

This may be a simple question but I'm having a hard time finding a straight answer to it: is there a way to resize a loaded Texture in SFML 2.0? For instance, if I have a 3264x2448 png and I want to scale it down to fit a 900x1200 rendered window without cropping, how would I do so?

推荐答案

是否有一种方法可以缩放所有渲染的窗口以适合任何监视器应用程序在哪个系统上运行?

Is there a way to scale all rendered windows to fit whatever monitor of whatever system the application is running on?

首先,这是一种将图像缩放到当前RenderWindow大小的方法.

First, here's a way to scale the image to the current RenderWindow size.

// assuming the given dimension
// change this dynamically with the myRenderWindow->getView().getSize()
sf::Vector2f targetSize(900.0f, 1200.0f); 

yourSprite.setScale(
    targetSize.x / yourSprite.getLocalBounds().width, 
    targetSize.y / yourSprite.getLocalBounds().height);

请注意,如果不保持宽高比,这可能会拉伸图像.您可能需要添加代码来调整适合您情况的决定.

Be aware that this may stretch your image if the aspect ratio is not maintained. You might want to add code to adjust the decision for your case.

然后,如果您想拉伸RenderWindow以填充整个屏幕,我是否建议您使用全屏模式?

Then, if you want to stretch the RenderWindow to fill all the screen, may I suggest you use fullscreen mode?

下面是它的完成方式的片段:

Here's a snippet of how it's done:

// add the flag to the other ones
mStyleFlag = sf::Style::Default | sf::Style::Fullscreen;

// get the video mode (which tells you the size and BPP information of the current display
std::vector<sf::VideoMode> VModes = sf::VideoMode::getFullscreenModes();

// then create (or automatically recreate) the RenderWindow
mMainWindow.create(VModes.at(0), "My window title", mStyleFlag);

这篇关于在SFML 2.0中调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆