不复制而返回数组

问题描述 投票:0回答:1

我有一个程序,它具有sf :: RenderWindows的公共静态数组,该数组是sfml库中的不可复制窗口。无论如何,当我尝试将push_back返回到向量时,都会出现错误,因为它无法复制。我已经尝试过使用refrancs和指针来做到这一点,并且总是会遇到某种错误。如果我尝试使用refrances,则会出现20条错误,指出指向refrance的指针是非法的;如果我尝试使用.draw和.clear之类的指针构造函数,则“不存在”。如果可以使用不复制的类似push_back之类的方法,那很好,如果没有,我在尝试使用refrances和指针时会给出代码。

法郎

    static vector <MakeKey::NewKey> KeyArray;
    static vector <sf::RenderWindow&> WindowArray;
    static void StepWindows(sf::RenderWindow & window, MakeKey::NewKey key)
    {
        sf::Clock clock;
        MakeTopWindow(window);
        setShape(window, key.Img);
        window.clear(sf::Color::Transparent);
        window.draw(key.Sprite);
        window.display();
        if (clock.getElapsedTime().asMicroseconds() > 1000)
        {
            window.setPosition(MakeKey::Gravity(window, key));
        }
    }

我后推时的法郎

        MakeKey::KeyArray.push_back(Key);
        MakeKey::WindowArray.push_back(window);

指针

    static vector <MakeKey::NewKey> KeyArray;
    static vector <sf::RenderWindow*> WindowArray;
    static void StepWindows(sf::RenderWindow & window, MakeKey::NewKey key)
    {
        sf::Clock clock;
        MakeTopWindow(window);
        setShape(window, key.Img);
        window.clear(sf::Color::Transparent);
        window.draw(key.Sprite);
        window.display();
        if (clock.getElapsedTime().asMicroseconds() > 1000)
        {
            window.setPosition(MakeKey::Gravity(window, key));
        }
    }

当我回推时的指针

        MakeKey::KeyArray.push_back(Key);
        MakeKey::WindowArray.push_back(&window);
        //The error here is that is 2 undefined external symbol because I declaired both arrays as static but if they are not static I get a error on where I push_back saying refrance must be relative to specific class.

谢谢

我的全班学习,因为有人想要它-仍然出现链接器错误,但我试图找出原因。

static class MakeKey
{
public:
    typedef struct KeyStruct {
        sf::Image Img;
        sf::Texture Tex;
        sf::Sprite Sprite;
        typedef struct Velocety {
            static int x;
            static int y;
        };
        typedef struct Acceleration {
            static int x;
            static int y;
        };
        typedef struct HitSide {
            static bool x()
            {
                typedef struct Side {
                    static bool left(sf::RenderWindow window, sf::Image image) {
                        int XPos{ window.getPosition().x };
                        if (XPos > (sf::VideoMode::getDesktopMode().width - image.getSize().x - 1))
                            return true;
                        return false;
                    }
                    static bool right(sf::RenderWindow window, sf::Image image) 
                    {
                        int XPos{ window.getPosition().x };
                        if (XPos < 1 + image.getSize().x)
                            return true;
                        return false;
                    }
                };


            }
            static bool y(sf::RenderWindow window, sf::Image image)
            {
                typedef struct Side {
                    static bool top(sf::RenderWindow window, sf::Image image) {
                        int YPos = window.getPosition().y;
                        if (YPos > (sf::VideoMode::getDesktopMode().width - image.getSize().y - 1))
                            return true;
                        return false;
                    }
                    static bool bottom(sf::RenderWindow window, sf::Image image)
                    {
                        int YPos = window.getPosition().y;
                        if (YPos < 1 + image.getSize().y)
                            return true;
                        return false;
                    }
                };
            }
        };
        static sf::Clock OffGroundClock;
    }NewKey;
    static sf::Vector2i Gravity(sf::RenderWindow * window, MakeKey::NewKey Key)
    {
        int XPos = window->getPosition().x;
        int YPos = window->getPosition().y;
        cout << "Gravity Debug Starting\n" << XPos << " and " << YPos << endl;
        YPos += 2;
        if (YPos < 1 + Key.Img.getSize().y)
            YPos = 1 + Key.Img.getSize().y;
        if (YPos > (sf::VideoMode::getDesktopMode().height - Key.Img.getSize().y - 1))
            YPos = (YPos = sf::VideoMode::getDesktopMode().height - Key.Img.getSize().y);
        if (XPos < 1 + Key.Img.getSize().x)
            XPos = (1 + Key.Img.getSize().x);
        if (XPos > (sf::VideoMode::getDesktopMode().width - Key.Img.getSize().x - 1))
            XPos = sf::VideoMode::getDesktopMode().width - Key.Img.getSize().x;
        cout << XPos << " and " << YPos << endl;
        return sf::Vector2i(XPos, YPos);
    }
    /*void Step() {
        velocity.x += acceleration.x;
        velocity.y += acceleration.y;
    }*/
    static bool setShape(sf::RenderWindow * window, const sf::Image& image)
    {
        HWND hwnd = window->getSystemHandle();
        const sf::Uint8* pixelData = image.getPixelsPtr();
        HRGN hRegion = CreateRectRgn(0, 0, image.getSize().x, image.getSize().y);

        // Determine the visible region
        for (unsigned int y = 0; y < image.getSize().y; y++)
        {
            for (unsigned int x = 0; x < image.getSize().x; x++)
            {
                if (pixelData[y * image.getSize().x * 4 + x * 4 + 3] == 0)
                {
                    HRGN hRegionPixel = CreateRectRgn(x, y, x + 1, y + 1);
                    CombineRgn(hRegion, hRegion, hRegionPixel, RGN_XOR);
                    DeleteObject(hRegionPixel);
                }
            }
        }

        SetWindowRgn(hwnd, hRegion, true);
        DeleteObject(hRegion);
        return true;
    }
    static sf::Vector2i RandSpawn(sf::Image image)
    {
        cout << "Desktop Demensions:" << sf::VideoMode::getDesktopMode().width << " by " << sf::VideoMode::getDesktopMode().height << endl;
        cout << "Starting Relocation Debug" << endl;
        std::random_device rand;
        int RandX = (rand() % sf::VideoMode::getDesktopMode().width) - image.getSize().x;
        int RandY = (rand() % sf::VideoMode::getDesktopMode().height) - image.getSize().y;
        if (RandX < 1 + image.getSize().x)
            cout << "Image X Size: " << image.getSize().x << endl << "RandX: " << RandX << endl;
        RandX = image.getSize().x;
        cout << "Image X Size: " << image.getSize().x << endl << "RandX: " << RandX << endl;
        if (RandY < 1 + image.getSize().y)
            cout << "Image Y Size: " << image.getSize().y << endl << "RandY: " << RandY << endl;
        RandY = image.getSize().y;
        cout << "Image Y Size: " << image.getSize().y << endl << "RandY: " << RandY << endl;
        cout << "Randomly Relocated\n" << RandX << " and " << RandY << endl;
        cout << "Relocation Debug Complete\n\n" << endl;
        return sf::Vector2i(RandX, RandY);
    }
    static bool setTransparency(HWND hwnd, unsigned char alpha)
    {
        SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);
        SetLayeredWindowAttributes(hwnd, 0, alpha, LWA_ALPHA);
        return true;
    }
    static void MakeTopWindow(sf::RenderWindow * windowPoint)
    {
        HWND hwndPoint = windowPoint->getSystemHandle();
        SetWindowPos(hwndPoint, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
    }
    static vector <MakeKey::NewKey> KeyArray;
    static vector <sf::RenderWindow*> WindowArray;
    static void StepWindows()
    {
        for (int i{ 0 }; i > MakeKey::KeyArray.size(); i++)
        {
            sf::Clock clock;
            MakeTopWindow(WindowArray[i]);
            setShape(WindowArray[i], KeyArray[i].Img);
            WindowArray[i]->clear(sf::Color::Transparent);
            WindowArray[i]->draw(KeyArray[i].Sprite);
            WindowArray[i]->display();
            if (clock.getElapsedTime().asMicroseconds() > 1000)
            {
                WindowArray[i]->setPosition(MakeKey::Gravity(WindowArray[i], KeyArray[i]));
            }
        }
    }

    static void DrawKey(string input)
    {
        MakeKey::NewKey Key;
        if (input == "A")
            Key.Img.loadFromFile("Assets/Images/A.png");
        else if (input == "D")
            Key.Img.loadFromFile("Assets/Images/D.png");
        else if (input == "E")
            Key.Img.loadFromFile("Assets/Images/E.png");
        else if (input == "Q")
            Key.Img.loadFromFile("Assets/Images/Q.png");
        else if (input == "S")
            Key.Img.loadFromFile("Assets/Images/S.png");
        else if (input == "W")
            Key.Img.loadFromFile("Assets/Images/W.png");
        else if (input == "X")
            Key.Img.loadFromFile("Assets/Images/X.png");
        else if (input == "Z")
            Key.Img.loadFromFile("Assets/Images/Z.png");
        else if (input == "Esc")
            Key.Img.loadFromFile("Assets/Images/esc.png");
        Key.Tex.loadFromImage(Key.Img);
        Key.Sprite.setTexture(Key.Tex);

        //Open Window
        sf::RenderWindow window(sf::VideoMode(Key.Img.getSize().x, Key.Img.getSize().y, 32), "Key", sf::Style::None);
        window.setPosition(MakeKey::RandSpawn(Key.Img));
        sf::RenderWindow* windowPoint = &window;
        //Make Transparent
        const unsigned char opacity = 1000;
        setTransparency(window.getSystemHandle(), opacity);
        setShape(windowPoint, Key.Img);
        MakeKey::KeyArray.push_back(Key);
        MakeKey::WindowArray.push_back(&window);
    }

};

主要

int main()
{
    sf::RenderWindow window(sf::VideoMode(100, 100, 32), "Main Window", sf::Style::None);
    sf::RenderWindow* windowRef = &window;
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            MakeKey::MakeTopWindow(windowRef);
            //Key Presses
            if (event.type == sf::Event::KeyPressed) {
                if (event.key.code == sf::Keyboard::A)
                    MakeKey::DrawKey("A");
                else if (event.key.code == sf::Keyboard::D)
                    MakeKey::DrawKey("D");
                else if (event.key.code == sf::Keyboard::E)
                    MakeKey::DrawKey("E");
                else if (event.key.code == sf::Keyboard::Q)
                    MakeKey::DrawKey("Q");
                else if (event.key.code == sf::Keyboard::S)
                    MakeKey::DrawKey("S");
                else if (event.key.code == sf::Keyboard::W)
                    MakeKey::DrawKey("W");
                else if (event.key.code == sf::Keyboard::X)
                    MakeKey::DrawKey("X");
                else if (event.key.code == sf::Keyboard::Z)
                    MakeKey::DrawKey("Z");
                else if (event.key.code == sf::Keyboard::Escape)
                    MakeKey::DrawKey("Esc");
            }
            //Close
            if (event.type == sf::Event::Closed)
                window.close();
        }
        MakeKey::StepWindows();
    }
    return EXIT_SUCCESS;
}
c++ c++17 sfml
1个回答
0
投票

我知道您已经得到了答案,但是请发布您的整个课程,以便大家更好地理解。

编辑:我还不能发表评论

© www.soinside.com 2019 - 2024. All rights reserved.