我正在开发一个用C++编写并使用SFML 2D库的小蛇游戏.问题是:为了渲染一个窗口,并在其中打印任何东西,你必须通过一个窗口
while (App->IsOpened())
{
//Do the stuff
App->Clear();
App->Display();
}
Run Code Online (Sandbox Code Playgroud)
但是,我想以更通用的方式构建我的程序,这将使我能够只是初始化窗口,然后向其发送信号,例如其中的"RenderARect"或"ClearTheWindow",从外部声明.它会让我使用我的渲染类实例作为动态库例如,制作游戏代码,渲染代码两个不同且独立的东西......
你对如何在我的SFML程序中实现这样一个信号系统有什么建议吗?
PS:我已经习惯了libsigc ++,但不知道如何实现它......
谢谢!
我已经阅读了整个教程部分sfml-dev.org并且有一些问题.
什么是视图以及如何使用它们?
我应该在视图中还是在渲染窗口中渲染内容?
当我在特定状态下关闭我的应用程序时,此代码运行:
void GameEndState::exit(){
delete m_pGameObjectManager;
delete m_pSceneManager;
}
Run Code Online (Sandbox Code Playgroud)
运行m_pGameObjectManager的析构函数后,它是:
GameObjectManager::~GameObjectManager(){
std::for_each(gameObjects.begin(),gameObjects.end(),GameObjectDeallocator());
}
//gameObjects is a std::map<sf::String,VisibleGameObject*>
///////////////
struct GameObjectDeallocator{
void operator()(const std::pair<sf::String,VisibleGameObject*>&p) const{
delete p.second;
}
};
Run Code Online (Sandbox Code Playgroud)
它会产生标题的错误.我没有从谷歌上找到任何这个错误的结果.我正在使用SFML 2.0.
VisibleGameObject的析构函数:
VisibleGameObject::~VisibleGameObject(){
m_pSceneManager->removeSprite(name);
}
void SceneManager::removeSprite(sf::String spritename){
std::cout << "removed " << std::string(spritename) << std::endl;
sprites.erase(sprites.find(spritename));
}
//sprites is a std::map<sf::String,sf::Sprite>
Run Code Online (Sandbox Code Playgroud)
提前致谢
int Game::MouseOnDot(float x, float y, RenderWindow &renderWindow) {
Rect<int> Dot;
Event event;
Dot.left = x;
Dot.top = y;
Dot.width = 20;
Dot.height = 20;
while (renderWindow.pollEvent(event)) {
float Mx = sf::Mouse::getPosition().x;
float My = sf::Mouse::getPosition().y;
if (event.type == Event::MouseButtonReleased&&Mx > x && Mx < Dot.height && My > y && My < Dot.width){
return 1;
}
else
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么如果在返回 1 的点上按下按钮,这将不起作用,它告诉其他函数关闭窗口。我在鼠标位置上做错了吗?
while (renderWindow.isOpen()) {
processEvents(renderWindow);
float Time = clock.getElapsedTime().asSeconds();
float TimeDifference = Time - LastUpdateTime;
if (TimeDifference …Run Code Online (Sandbox Code Playgroud) 休斯顿,我们有一个问题.这是一个简化的代码版本:
main.cpp中
#include <SFML/Graphics.hpp>
#include "global.hpp"
#include "init.hpp"
int main(void)
{
createWindow();
loadLevel();
while(window.isOpen())
{
if(!handleEvents()) window.close();
window.clear();
window.draw(bgSprite);
window.display();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
global.hpp
sf::Texture bgTexture;
sf::Sprite bgSprite;
Run Code Online (Sandbox Code Playgroud)
init.hpp
void loadGraphics(void)
{
bgTexture.loadFromFile("bg.png");
bgSprite.setTexture(bgTexture);
}
Run Code Online (Sandbox Code Playgroud)
即使纹理和子画面变量是全局的,窗口屏幕仍然是黑色的.但是,当我将变量放在main()函数中时,一切都很完美.有人可以解释为什么会这样吗?
我认为你可以随时随地调用全局变量,只有当程序本身终止时它们才会消失.
顺便说一句,我也尝试将变量和loadGraphics()放在main()后面(不在头文件中),仍然没有结果.我还注释掉了所有其他代码,所以问题肯定在这里.
我没有调用loadGraphics(函数).谢谢!我很抱歉你浪费了时间.一切都开始正常.伤心的哈哈 - 花了1个多小时修理这个东西......
我想让我Enemy搬到我家Player.
我知道的事情:
我需要做的事情:
所以我认为我需要做的是根据球员的位置"规范化"敌人的位置,这样我就知道要去哪里了,两者都有基于的位置Vector2f.
这就是我在敌人身上的代码:
void Enemy::Move()
{
//cout << "Move" << endl;
// Make movement
Vector2f playerPosition = EntityManager::Instance().player.GetPosition();
Vector2f thisPosition;
thisPosition.x = xPos;
thisPosition.y = yPos;
//Vector2f direction = normalize(playerPosition - thisPosition);
speed = 5;
//EntityManager::Instance().enemy.enemyVisual.move(speed * direction);
}
Vector2f normalize(const Vector2f& source)
{
float length = sqrt((source.x * source.x) + (source.y * source.y));
if (length != 0)
return Vector2f(source.x / length, source.y / length);
else
return source; …Run Code Online (Sandbox Code Playgroud) 我正在使用SFML进行游戏,我正在尝试添加射击,但由于某种原因,sprite.move()似乎无法正常工作.这是相关的代码:
Weapon.cpp
void Weapon::update(float delta, sf::RenderWindow& window, Player player) {
for (auto s : shots) {
s.move(delta);
s.draw(window);
}
switch (type) {
case RANGED:
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
shots.push_back(Shot(shot, player.position, Helper::getMousePos(window)));
Helper::log(std::to_string(shots.size()));
}
break;
case MELEE:
break;
}
}
Run Code Online (Sandbox Code Playgroud)
Shot.cpp
Shot::Shot(sf::Sprite sprite_, sf::Vector2f origin, sf::Vector2f target)
{
sprite = sprite_;
sprite.setPosition(origin);
direction = Helper::normalizeVector(target - origin);
speed = 200;
}
void Shot::move(float delta) {
sprite.move(direction * speed * delta);
}
void Shot::draw(sf::RenderWindow& window) {
window.draw(sprite);
}
Run Code Online (Sandbox Code Playgroud)
发生的事情是在玩家所在地产生的镜头,但它们不会移动.相反,他们有点振动,好像他们正试图移动但有些东西阻止他们.如果您需要更多代码,请与我们联系.
我收到此错误,但在尝试创建新对象指针时不知道为什么。
这是我的头类的代码
#ifndef CURSOR_H_INCLUDED
#define CURSOR_H_INCLUDED
#include <SFML\Graphics.hpp>
#Include "Mango.h"
#include <stack>
using namespace sf;
using namespace std;
struct cursor{
Texture tCursor;
Sprite sCursor;
stack<Mango*> inv;
float money;
void Sell();
cursor();
~cursor();
};
#endif CURSOR_H_INCLUDED
Run Code Online (Sandbox Code Playgroud)
主要是尝试这样做
cursor * cursor = new cursor();
Run Code Online (Sandbox Code Playgroud)
但它给了我那个错误。
程序崩溃。我正在尝试实现一个Brick ** create_bricks()函数,该函数返回一个指向 Brick 指针的指针(因为我想创建一个 Brick 数组并返回第一个的位置)。我正在拔头发,经过多次尝试和失败、错误和研究后,我做的最后一件事是简化我的代码并逐行检查指针的值。当我在 draw 函数游戏循环中取消引用时the_brick,*the_brick它不会获得与new Brick()分配给ptr_brick内部create_bricks()函数的值相同的值 。我正在使用 SFML,但问题在于指针。
注意:如果我通过适当的修改返回 a Brick *,create_bricks它可以正常工作(并绘制砖块),但我需要创建多个砖块,而不仅仅是一个。
#include "Brick.h"
int main()
{
...
while (window.isOpen())
{
Brick ** ptr_bricks = create_bricks();
Brick * ptr_brick = *ptr_bricks;
window.draw(*the_brick);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Brick ** create_bricks()
{
Brick * ptr_brick = new Brick();
ptrBrick->setPosition(150, 20);
return &ptrBrick;
}
Run Code Online (Sandbox Code Playgroud)
#include "Brick.h"
Brick::Brick()
{
LOG(INFO) << "Brick constructor";
setPosition(10, …Run Code Online (Sandbox Code Playgroud) 我在一个类中有一些函数 func Class:
void func(int arg1, int arg2, Vector2f vec = Vector2f(0,0)){};
Run Code Online (Sandbox Code Playgroud)
我收到一个构建错误:
error C2572: 'Classs::func': redefinition of default argument: parameter 1
Run Code Online (Sandbox Code Playgroud)
我尝试了许多不同的语法,但没有一个奏效。如何定义这种类型的默认参数?