C++ SFML 2.2 vectors

C++ SFML 2.2 vectors

本文关键字:vectors SFML C++      更新时间:2023-10-16

我只是在玩c++ SFML的东西,我有点不明白为什么我的代码不工作。我想做的是用矢量在窗口中随机绘制5个正方形,但我不明白为什么它不起作用。

这是主要的游戏类:

#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 class

#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 projectsblahblahblahblahtestobject.cpp(18): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>d:visual studio projectsblahblahblahblahtestobject.cpp(22): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>  main_game.cpp
1>d:visual studio projectsblahblahblahblahmain_game.cpp(16): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>d:visual studio projectsblahblah3rdpartylibsincludesfmlwindowwindow.hpp(521): error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'
1>          d:visual studio projectsblahblah3rdpartylibsincludesfmlsystemnoncopyable.hpp(67) : see declaration of 'sf::NonCopyable::NonCopyable'
1>          d:visual studio projectsblahblah3rdpartylibsincludesfmlsystemnoncopyable.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 projectsblahblah3rdpartylibsincludesfmlgraphicsrendertarget.hpp(419): error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'
1>          d:visual studio projectsblahblah3rdpartylibsincludesfmlsystemnoncopyable.hpp(67) : see declaration of 'sf::NonCopyable::NonCopyable'
1>          d:visual studio projectsblahblah3rdpartylibsincludesfmlsystemnoncopyable.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 ==========

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

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

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

好的,我修复了这个问题,但现在它没有给我任何错误,这很好,但它没有渲染任何东西

'Blahblah.exe' (Win32): Loaded 'D:Visual Studio ProjectsBlahblahDebugBlahblah.exe'. Symbols loaded.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64ntdll.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64kernel32.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64KernelBase.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'D:Visual Studio ProjectsBlahblahBlahblahsfml-graphics-d-2.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'D:Visual Studio ProjectsBlahblahBlahblahsfml-system-d-2.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'D:Visual Studio ProjectsBlahblahBlahblahsfml-window-d-2.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64msvcp120d.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64msvcr120d.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64opengl32.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64winmm.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64gdi32.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64user32.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64advapi32.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64msvcrt.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64glu32.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64ddraw.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64winmmbase.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64sechost.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64rpcrt4.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64dciman32.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64cfgmgr32.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64devobj.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64sspicli.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64cryptbase.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64bcryptprimitives.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64imm32.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64msctf.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64nvinit.dll'. Cannot find or open the PDB file.
'Blahblah.exe' (Win32): Loaded 'C:WindowsSysWOW64version.dll'. Cannot find or open the PDB file.