【问题标题】:C++ SFML 2.2 vectorsC++ SFML 2.2 向量
【发布时间】:2015-06-08 14:28:59
【问题描述】:

我只是在玩 C++ SFML 的东西,我有点不明白为什么我的代码不起作用。我想做的事情是像 5 一样绘制,Window 中的正方形使用矢量随机放置在屏幕周围,但我不明白为什么它不起作用。

这是主要的游戏类:

#include "main_game.h"
#include "main_menu.h"

void main_game::Initialize(sf::RenderWindow* window)
{
    this->Player = new player();
    this->Player->setOrigin(this->Player->getGlobalBounds().width / 2, this->Player->getGlobalBounds().height / 2);

    this->TestObject = new testObject();
    this->TestObject->Initialize();
    this->TestObject->setOrigin(this->TestObject->getGlobalBounds().width / 2, this->TestObject->getGlobalBounds().height / 2);
}

void main_game::Update(sf::RenderWindow* window)
{
    this->Player->setPosition(sf::Mouse::getPosition(*window).x, sf::Mouse::getPosition(*window).y);
    this->Player->Update();

    if (this->Player->CheckCollision(TestObject))
    {
        this->TestObject->setColor(sf::Color::Red);
    }
    else
    {
        this->TestObject->setColor(sf::Color::Cyan);
    }

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape))
    {
        coreState.SetState(new main_menu());
    }
}

void main_game::Render(sf::RenderWindow* window, std::vector<sf::Sprite> sprites)
{
    this->TestObject->Render(*window, sprites);

    window->draw(*this->Player);
}

void main_game::Destroy(sf::RenderWindow* window)
{
    delete this->Player;
    delete this->TestObject;
}

这是 testObject.h 类

#pragma once

#include "entity.h"

class testObject : public Entity
{
public:
    testObject();
    void Initialize();
    void Render(sf::RenderWindow window, std::vector<sf::Sprite> sprites);
    void Update();
private:
    sf::RenderWindow window;
};

这是 testObject.cpp 类

#include "testObject.h"

testObject::testObject()
{
    this->Load("testObject.png");
}

void testObject::Initialize()
{
    sf::Texture testObjectTexture;
    sf::Sprite testObjectSprite;

    testObjectTexture.loadFromFile("testObject.png");
    testObjectSprite.setTexture(testObjectTexture);

    std::vector<sf::Sprite> sprites(5, sf::Sprite(testObjectSprite));

    srand(time(0));

    for (unsigned int i = 0; i < sprites.size(); i++)
    {
        sprites[i].setPosition(1 + (rand() % 1024 - 32), rand() % 640 - 32);
    }
}

void testObject::Render(sf::RenderWindow window, std::vector<sf::Sprite> sprites)
{
    for (unsigned int i = 0; i < sprites.size(); i++)
    {
        window.draw(sprites[i]);
    }
}

void testObject::Update()
{
    Entity::Update();
}

错误信息是

1>------ Build started: Project: Blahblah, Configuration: Debug Win32 ------
1>  testObject.cpp
1>d:\visual studio projects\blahblah\blahblah\testobject.cpp(18): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>d:\visual studio projects\blahblah\blahblah\testobject.cpp(22): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>  main_game.cpp
1>d:\visual studio projects\blahblah\blahblah\main_game.cpp(16): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>d:\visual studio projects\blahblah\3rdpartylibs\include\sfml\window\window.hpp(521): error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'
1>          d:\visual studio projects\blahblah\3rdpartylibs\include\sfml\system\noncopyable.hpp(67) : see declaration of 'sf::NonCopyable::NonCopyable'
1>          d:\visual studio projects\blahblah\3rdpartylibs\include\sfml\system\noncopyable.hpp(42) : see declaration of 'sf::NonCopyable'
1>          This diagnostic occurred in the compiler generated function 'sf::Window::Window(const sf::Window &)'
1>d:\visual studio projects\blahblah\3rdpartylibs\include\sfml\graphics\rendertarget.hpp(419): error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'
1>          d:\visual studio projects\blahblah\3rdpartylibs\include\sfml\system\noncopyable.hpp(67) : see declaration of 'sf::NonCopyable::NonCopyable'
1>          d:\visual studio projects\blahblah\3rdpartylibs\include\sfml\system\noncopyable.hpp(42) : see declaration of 'sf::NonCopyable'
1>          This diagnostic occurred in the compiler generated function 'sf::RenderTarget::RenderTarget(const sf::RenderTarget &)'
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

【问题讨论】:

  • 请比“我不明白为什么它不起作用”更能描述您的问题
  • 基本上,我确实尝试在 main() 中只有 main.cpp 的全新项目上使用矢量绘制多个对象,并且它的工作原理很吸引人。但是这里的问题可能是我不能在类和多个函数之间正确地做到这一点。
  • 我的意思是让你用 “它不工作” 来描述你的意思。是编译器错误吗?如果是这样,错误信息是什么?还是运行时错误?如果是这样,实际发生了什么,它与您对应该发生什么的预期有何不同?
  • 哦,我忘了输入错误:D.
  • 它只是构建了它给我的错误。

标签: c++ sfml


【解决方案1】:

这个特定的错误,(虽然我很确定一旦你修复它你会遇到更多),可能来自这个:

void testObject::Render(sf::RenderWindow window, std::vector<sf::Sprite> sprites)

您正在通过值传递RenderWindow,但它是不可复制的。而是通过引用或指针传递它。

【讨论】:

  • 好的,我解决了这个问题,但现在它没有给我任何错误,所以这很好,但它没有渲染任何东西。
  • @KuziNs:这是一个完全不同的问题。它属于不同的帖子。
  • 你的精灵没有显示的原因是因为它已经被卸载了,你需要创建一个常量数组/向量/映射来存储纹理,然后在你的其他对象中设置一个对纹理,然后设置你的精灵。
【解决方案2】:

好的,我解决了这个问题,但现在它没有给我任何错误,所以这很好,但它没有渲染任何东西。

'Blahblah.exe' (Win32): Loaded 'D:\Visual Studio Projects\Blahblah\Debug\Blahblah.exe'. Symbols loaded.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'D:\Visual Studio Projects\Blahblah\Blahblah\sfml-graphics-d-2.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'D:\Visual Studio Projects\Blahblah\Blahblah\sfml-system-d-2.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'D:\Visual Studio Projects\Blahblah\Blahblah\sfml-window-d-2.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp120d.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr120d.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\opengl32.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\winmm.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\gdi32.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\user32.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\glu32.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ddraw.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\winmmbase.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dciman32.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cfgmgr32.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\devobj.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\imm32.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msctf.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\nvinit.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\version.dll'. Cannot find or open the PDB file.

【讨论】:

  • 这不是答案。如果您想更新它,您应该编辑您的问题。此外,您发布的消息并不表示任何错误 - 它们只是告诉您系统库没有可用的调试符号 - 这是完全正常的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-23
  • 2014-02-16
  • 2023-03-23
  • 2020-09-19
  • 1970-01-01
  • 1970-01-01
  • 2019-04-09
相关资源
最近更新 更多