- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我找到了这个 sfml tutorial写于 2009 年,我正在尝试将其转换为 2.4。
我无法转换这两行:
win.draw(sf::Shape::Rectangle(le.Blocks[0].fRect, sf::Color(255, 0, 0)));
rt.draw(sf::Shape(sf::Shape::Line(l.position, end, 1, l.color)));
我一直在尝试按照 SFML 论坛上的建议使用 sf::RectangleShape 进行转换,但我就是找不到合适的参数。这是转换为 2.x 的完整代码,除了这两行。
如果有人愿意“完成”转换,我将不胜感激,我相信本教程会对其他人有所帮助。
主要内容:
#include <SFML/Graphics.hpp>
#include <iostream>
#include "LightEngine.h"
int main()
{
sf::RenderWindow win(sf::VideoMode(800, 600), "Light Tutorial");
sf::Event event;
LightEngine le;
Light light;
light.radius = 600;
light.angleSpread = 100;
light.position = sf::Vector2f(100, 150);
le.Lights.push_back(light);
Block block;
block.fRect = sf::FloatRect(0, 0, 50, 50);
le.Blocks.push_back(block);
while (win.isOpen())
{
while (win.pollEvent(event))
{
if (event.type == sf::Event::Closed) win.close();
}
win.clear();
le.Blocks[0].fRect.left = sf::Mouse::getPosition(win).x;
le.Blocks[0].fRect.top = sf::Mouse::getPosition(win).y;
/*HERE*/ win.draw(sf::Shape::Rectangle(le.Blocks[0].fRect, sf::Color(255, 0, 0)));
le.Step(win);
win.display();
}
}
LightEngine.h
#pragma once//don't allow this header to be included more than once
#include "Light.hpp"
#include "Block.hpp"
#include <vector>
#include <SFML/Graphics/RenderTarget.hpp> //Place to draw on
#include <SFML/Graphics/Shape.hpp> //SFML programmable Shapes
class LightEngine
{
public:
void Step(sf::RenderTarget &rt);
std::vector <Light> Lights; //Container for Lights
std::vector <Block> Blocks; //Container for Blocks
private:
void ShineLight(Light &lig, sf::RenderTarget &rt);
static const float Distance(const sf::Vector2f &p1, const sf::Vector2f &p2);
static const sf::Vector2f GetCenter(const sf::FloatRect &fr); //Get the center of a rectangle
struct FindDistance //if a light's radius manages to intersect multiple blocks, we need to find the sortest distance to shorten the light
{
FindDistance();
float shortest;
bool LightHitsBlock(Light &l, Block &b, const float cur_ang, float &reflength);
bool start; //to get the first distance to refer off of
};
FindDistance findDis;
};
LightEngine.cpp
#include "LightEngine.h"
LightEngine::FindDistance::FindDistance()
{
start = false;
shortest = 0;
}
const sf::Vector2f LightEngine::GetCenter(const sf::FloatRect &fr)
{
return sf::Vector2f(fr.left + (fr.width / 2), fr.top + (fr.height / 2));
}
const float LightEngine::Distance(const sf::Vector2f &p1, const sf::Vector2f &p2)
{
//We need to look at this as a triangle
/*
/|p1.y
/ |
/ |
/ |
/ |
/ |b
/ |
/ |
/ |
/ |
-----------
a p2.y
p1.x p2.x
*/
float a = p2.x - p1.x; //width length
float b = p2.y - p1.y; //height length
float c = sqrt((a * a) + (b * b)); //Pythagorean Theorem. (c² = a² + b²). c = squareroot(a² + b²)
return c;
}
void LightEngine::Step(sf::RenderTarget &rt)
{
for (unsigned i = 0; i < Lights.size(); i++)
{
ShineLight(Lights[i], rt); //Shine all lights
}
}
void LightEngine::ShineLight(Light &l, sf::RenderTarget &rt)
{
/*
remember back in the Light class, we had something called 'angleSpread' ?
that's to create a tunnel, or arc shaped light. Like this:
/)
/ )
/ )
/ )
< )
\ )
\ )
\ )
\)
Obviously it'll look better than an ascii drawing
*/
float current_angle = l.angle - (l.angleSpread / 2); //This will rotate the angle back far enough to get a desired arc
/*
Lights Angle (if it was at 0):
-------------
Current_Angle:
/
/
/
(slanted)
*/
float dyn_len = l.radius; //dynamic length of the light. This will be changed in the function LightHitsBlock()
float addto = 1.f / l.radius;
for (current_angle; current_angle < l.angle + (l.angleSpread / 2); current_angle += addto * (180.f / 3.14f)) //we need to add to the current angle, until it reaches the end of the arc. we divide 1.f by radius for a more solid shape. Otherwize you could see lines seperating
{
dyn_len = l.radius; //Reset the length
findDis.start = true; //Start of finding a light, we need to reset
findDis.shortest = 0; //Reset the shortest.
if (l.dynamic) //can this change?
{
for (unsigned i = 0; i < Blocks.size(); i++)
{
findDis.LightHitsBlock(l, Blocks[i], current_angle, dyn_len);
}
}
float radians = current_angle * (3.14f / 180); //Convert to radians for trig functions
sf::Vector2f end = l.position;
end.x += cos(radians) * dyn_len;
end.y += sin(radians) * dyn_len;
/*HERE*/rt.draw(sf::Shape(sf::Shape::Line(l.position, end, 1, l.color)));
}
}
bool LightEngine::FindDistance::LightHitsBlock(Light &l, Block &b, float cur_ang, float &refleng)
{
if (b.allowBlock) //can this even block?
{
float distance = Distance(l.position, GetCenter(b.fRect));
if (l.radius >= distance) //check if it's radius is even long enough to hit a block
{
float radians = cur_ang * (3.14f / 180); //convert cur_ang to radians for trig functions
sf::Vector2f pointpos = l.position;
pointpos.x += cos(radians) * distance;
pointpos.y += sin(radians) * distance;
//By doing this, we check if the angle is in the direciton of the block.
if (b.fRect.contains(pointpos)) //If it was, than the point would be intersecting the rectangle of the block
{
if (start || distance < shortest) //If this is the first block, or it has a shorter distance
{
start = false; //definately not the start so other blocks can't automatically set the distance
shortest = distance; //shortest is set to this
refleng = distance; //This is where the dynamic comes in, it changes the length of the reference towhere it's going to stop after it hits the distance from the point to the block
}
return true;
}
}
}
return false;
}
block .hpp
#pragma once//don't allow this header to be included more than once
#include <SFML/Graphics/Rect.hpp>
class Block
{
public:
Block()
{
allowBlock = true;
fRect = sf::FloatRect(0, 0, 0, 0);
}
/* Like the light class, you can do this in an initializer list, whatever works for you
Block()
: fRect(0,0,0,0), allowBlock(false)
{
}
*/
sf::FloatRect fRect; //Area that will be blocking light
bool allowBlock; //Sometimes we want to keep a block, but don't want it to actually block until a certain point
};
最佳答案
win.draw(sf::Shape::Rectangle(le.Blocks[0].fRect, sf::Color(255, 0, 0)));
会是:
sf::RectangleShape rshape;
rshape.setSize(sf::Vector2f(le.Blocks[0].fRect.width, le.Blocks[0].fRect.height));
rshape.setPosition(le.Blocks[0].fRect.left, le.Blocks[0].fRect.top);
rshape.setFillColor(sf::Color(255, 0, 0));
win.draw(rshape);
因此:
rt.draw(sf::Shape::Line(l.position, end, 1, l.color));
因为不再有线形了:
sf::Vertex line[] =
{
sf::Vertex(l.position, l.color),
sf::Vertex(end, l.color)
};
rt.draw(line, 2, sf::Lines);
关于c++ - 带 SFML 的动态光照程序,1.6 到 2.x 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44620031/
我有一个包含纹理的文件。我加载到 sf::Texture并用 setTextureRect 拆分成 Sprite . 现在假设一个 Sprite 包含 20 像素宽的纹理的一部分。如何渲染它以适应例如
一段时间以来,我一直在使用 SDL 来制作小东西的原型(prototype)并了解有关图形编程的更多信息。 然后我看到了SFML并想试一试,发现它更面向对象并修复了我认为 SDL 存在的一些问题。 但
我正在使用 rust-sfml ( rsfml::graphics ) 目前,将像素绘制到屏幕上。 (我只是从 Rust 和项目开始。)我将数据存储在 Image 中。 , 然后复制到 Texture
我试图在 SFML 2 中获取关键输入,在 SFML 1.6 im 中使用 while (App.GetEvent(Event)) { if (App.GetI
我做了一个非常简单的双人游戏,一个是水母,另一个是SFML中的鲨鱼。唯一的问题是一次只能有一个 Sprite 移动。这可能修复吗?我该如何修复? 编辑:我的尝试是这样的: if (event.type
我已经尝试了几个小时,但似乎无法做到我已经下载了扩展程序并寻求帮助,但此时一切都让我感到困惑。 我想在我的项目中包含 SFML 库,我正在尝试使用 Visual Studio Code 编辑器,但由于
下面链接中的图片包含 20 多张图片(每个 body 部位和盔甲类型都在单独的文件中)。这个角色在名为“Spriter”的软件中被动画化。该代码采用 spriter 的输出文件 (.scml),然后计
我的第一个问题是 C++ 是否: pTexture.getSize().x 等于 C#(因为这对我来说没有结果): pTexture.Size().X 第二个问题是关于C++代码的(如何在SFML.n
我正在尝试在 Windows 10 下用 sfml 构建游戏。它在构建时显示成功。每当我调试解决方案时,它都会显示如下错误消息: Unhandled exception at 0x00007FFA62
我开始使用 SFML 和 C++ 创建游戏。但是,我遇到了一个问题。我有基本的骨架类代码,但由于某种原因,当我创建一个窗口(sf::Window)时,我立即崩溃了!此代码不包含任何事件检查代码,但它包
我的项目中有 2 个 CMakeLists.txt 文件。 CMakeLists.txt cmake_minimum_required(VERSION 3.2) project(Game) set(C
所以我正在尝试编写一些与网络相关的代码,特别是使用 SFML 的端口扫描器。我正在运行 VS 2017 并下载了 2015 年的 SFML 版本,但是人们说这应该适用于 2017 年。 到目前为止,我
我刚刚开始使用 C++ 和 SFML,最终一切都运行良好。在出现 undefined reference 错误之前,我意识到我一直在下载错误类型的 SFML,得到的是 SJLJ 而不是 DW2。问题已
尝试将新的 C++ 项目与 SFML 链接,我执行了以下操作: 创建了一个空项目。 C/C++ >> 常规:附加包含目录:添加了 SFML 包含目录并从项目默认值继承。 链接器 >> 附加库目录:添加
当我运行代码时,除了不播放声音外,一切正常。链接很好,当我放置断点并查看声音对象时,它显示 .我在正确的位置添加了 sfml-audio-d.lib 和 sfml-audio.lib。欢迎提出任何建议
下面的代码给出了未捕获的异常(特别是 txtr.loadFromFile("C:/Users/kidz/Documents/Visual Studio 2012/Projects/SFML/Debug
Closed. This question needs to be more focused。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅关注editing this post的一个问题。
我想知道是否可以使用 SFML 库在 C 中使用颜色设置碰撞?因为我使用的是带有图 block 的 map ,我不知道它是否可以一起工作。 谢谢! 最佳答案 据我所知,您使用具有两种颜色的图像来定义碰
我正在制作 Frogger 的 C++ SFML 版本,目前正在尝试实现 Frog 和其他物体(汽车、卡车、原木等)之间的碰撞检测。我为 Frog 和游戏对象有单独的类,并希望使用边界框来检测交叉点,
嗨,大家好 为了缩放我的 sfml 应用程序,我正在尝试解决这个问题,该程序最初使用 运行(640, 480) 方面 显示这样的窗口... 我的目标是以全屏模式执行程序并放置旧的 '窗口' (检查图像
我是一名优秀的程序员,十分优秀!