首页 \ 问答 \ 在整个窗口中拉伸sf :: Sprite(Stretch sf::Sprite across entire window)

在整个窗口中拉伸sf :: Sprite(Stretch sf::Sprite across entire window)

我有一个sf :: Sprite,当它绘制到窗口时,我希望它填满整个窗口。

sf :: RenderWindow.draw采用可选的sf :: RenderStates。 那是我需要搞砸的吗?


I have a sf::Sprite and when drawing it to the window I want it to fill the entire window.

sf::RenderWindow.draw takes an optional sf::RenderStates. Is that what I need to mess with?


原文:https://stackoverflow.com/questions/30203708
更新时间:2020-11-02 20:04

最满意答案

首先,本教程中的基本Sprite用法

摘自我在SFML 2.0调整大小的答案,顺便说一句,这是搜索“sfml sprite填充屏幕”时的第一个google结果。

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

// 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);

请注意,如果不保持纵横比,这可能会拉伸图像。 您可能希望添加代码来调整案例的决策。

然后,如果你想拉伸RenderWindow来填充所有屏幕,我可以建议你使用全屏模式吗?

以下是它如何完成的片段:

// 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);

如果你正在寻找背景,那么最优雅的方法可能就是定义一个sf :: VertexArray,它将使用正确的纹理坐标渲染四边形填充窗口。

这取自第二个谷歌结果: SFML2中的sprite :: setSize发生了什么?


First, basic Sprite usage from the tutorial.

Taken from my own answer on Resizing in SFML 2.0 which, by the way, is the first google result when searching for "sfml sprite fill the screen".

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.

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);

If you're looking for a background, the most elegant way to do it would probably be to just define a sf::VertexArray which will render a quad filling your window using the correct texture coordinates.

This was taken from the second google result: What happened to sprite::setSize in SFML2?

2017-05-23

相关文章

更多

最新问答

更多
  • 如何保护Solr只允许SELECT请求给用户并禁止其他任何东西?(How to secure Solr to allow SELECT request to only users and disallow anything else?)
  • XPath表达式无效/错误TFHpple SWIFT 1.2(XPath Expression not working/incorrect TFHpple SWIFT 1.2)
  • css3 3D变换不能平滑地制作动画(css3 3D transform doesn't animate smoothly)
  • 运行时错误'91'和Outlook.Application = <对象变量或没有设置块变量>?(Run-time error '91' & Outlook.Application = Object variable or With block variable not set?)
  • 慢的webservice问题(Slow webservice problem)
  • textview的不正确对齐方式(Improper alignment of a textview)
  • 在第一步“Hello World”中出现Java错误(Cannot run “Hello World” program in Eclipse)
  • 为什么十六进制地址是14个字符?(why the hex address is 14 character?)
  • 如何在Python中的不同类中使用变量?(How to use variable in different classes in Python?)
  • asp:GridView HYPERLINKFIELD - datanavigateurlformatstring中的asp代码(asp:GridView HYPERLINKFIELD - asp code inside datanavigateurlformatstring)
  • 关于adaboost算法(About adaboost algorithm)
  • 在Matlab上内置图像(Built in Images on Matlab)
  • java swing:输入键事件时焦点丢失(java swing: Focus lost on enter key event)
  • C#中的通用约束,T是相同的TSomethingElse,对吧?(Generic constraints in C#, T is the same TSomethingElse, right?)
  • 从mybatis中的光标获取数据(Fetching data from cursor in mybatis)
  • 在运行时从XML构建对象的最佳方法(Best way for building objects out of XMLs at runtime)
  • 在整个窗口中拉伸sf :: Sprite(Stretch sf::Sprite across entire window)
  • Selenium Node API Web驱动程序等待超时处理程序(Selenium Node API web driver wait timeout handler)
  • 函数使用并且是map的一部分(循环依赖?)(Function uses and is part of map (circular dependency?))
  • 是否可以在C ++ 14中使用可选模板参数创建类型元组?(Is possible to make a tuple of types with optional template parameters in C++14?)
  • PHP从窗帘后面打印/ f(PHP prints /f from behind the curtains)
  • JFrame的contentPane的LayoutManager(LayoutManager of JFrame's contentPane)
  • 用于Instagram Feed的图像滚动(Image Roll Overs for Instagram Feed)
  • 如何显示拉伸字体(双倍宽度/高度)?(How to display stretched font (double width/height)?)
  • 文件操作API在fileapi.h和stdio.h中的WP8差异?(WP8 differences between file manipulation APIs in fileapi.h and stdio.h?)
  • 保存失败后Rails没有回滚事务()(Rails not rolling back transaction after failed save())
  • jqgrid中的分页问题与数组数据(Pagination problem in jqgrid with array data)
  • 重定向时,通过其他页面上的URL调用javascript函数(Call javascript function through url on otherpage while Redirecting)
  • 如何使用InvokeCommandAction调用我的方法并传入参数?(How do I go about using InvokeCommandAction to call a method of mine and pass in parameters?)
  • Jquerymobile按钮仅显示页面的第一个外观(Jquerymobile buttons are shown only first apperance of the page)