SFML with Pointers. Image with vector and shared_ptr not showing

家住魔仙堡 提交于 2020-01-07 08:26:21

问题


The problem with the image output. If you directly use class 'picture', then everything is fine. If you use the class 'menu', then the image is not displayed.

Source.cpp — working code using the class 'picture' directly

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <windows.h>
#include <conio.h>
#include "Menu.h"


int main()
{
    sf::RenderWindow window(sf::VideoMode(960, 540), "Programmer Simulator", sf::Style::Close | sf::Style::Titlebar  /*sf::Style::Fullscreen*/);

    sf::Image icon;
    if (!icon.loadFromFile("image/icon.png")) throw;
    window.setIcon(32, 32, icon.getPixelsPtr());

    try
    {
        /*menu mainMenu(window);          // problem
        mainMenu.posСorrection();*/       // problem

        while (window.isOpen())
        {
            sf::Event event;
            while (window.pollEvent(event))
                if (event.type == sf::Event::Closed) window.close();

            /*mainMenu.draw();*/          // problem

            picture picture_background(window, "image/background/0.png");
            picture_background.putFullscreen();
            picture picture_menu_newgame(window, "image/menu/newgame.png");
            picture picture_menu_continue(window, "image/menu/continue.png");
            picture picture_menu_exit(window, "image/menu/exit.png");

            picture_menu_continue.putCenterOver();
            picture_menu_newgame.putOverObj(picture_menu_continue, 30);
            picture_menu_exit.putUnderObj(picture_menu_continue, 30);

            picture_background.draw();
            picture_menu_newgame.draw();
            picture_menu_continue.draw();
            picture_menu_exit.draw();
            window.display();
        }

    }
    catch (const char* msg)
    {
        MessageBoxA(0, msg, "GAME ERROR", MB_OK | MB_ICONASTERISK);
    }
    return 0;
}

Menu.h

#pragma once
#include <memory>
#include <iterator>
#include "Picture.h"

class menu
{
    const int menuPictCount;
    std::vector<std::shared_ptr<picture>> menuPicture;
public:
    menu(sf::RenderWindow& usableArea);
    void draw();
    void posСorrection();
    void shineButton(sf::Color color);
};

Menu.cpp — problem code

#include "Menu.h"

menu::menu(sf::RenderWindow& usableArea) : menuPictCount(4)
{
    this->menuPicture.resize(this->menuPictCount);

    //background
    menuPicture[0] = std::shared_ptr<picture>(new picture(usableArea, "image/background/0.png"));

    //buttons
    menuPicture[1] = std::shared_ptr<picture>(new picture(usableArea, "image/menu/newgame.png"));
    menuPicture[2] = std::shared_ptr<picture>(new picture(usableArea, "image/menu/continue.png"));
    menuPicture[3] = std::shared_ptr<picture>(new picture(usableArea, "image/menu/exit.png"));
}
void menu::draw()
{
    for (size_t i = 0; i < this->menuPictCount; i++) this->menuPicture[i]->draw();
}
void menu::posСorrection()
{
    const int buttonsCount = this->menuPictCount - 1;

    if (buttonsCount % 2 == 0)
    {
        int indexCenterOver = buttonsCount / 2;
        int indexCenterUnder = indexCenterOver + 1;
        this->menuPicture[indexCenterOver]->putCenterOver();
        this->menuPicture[indexCenterUnder]->putCenterUnder();

        for (size_t i = indexCenterOver - 1; i > 0; i--)
        {
            this->menuPicture[i]->putOverObj(*this->menuPicture[i + 1]);
        }
        for (size_t i = indexCenterUnder + 1; i <= buttonsCount; i++)
        {
            this->menuPicture[i]->putUnderObj(*this->menuPicture[i - 1]);
        }
    }
    else
    {
        const int indexCenter = buttonsCount / 2 + 1;

        this->menuPicture[indexCenter]->putCenter();

        for (size_t i = indexCenter - 1; i > 0; i--)
        {
            this->menuPicture[i]->putOverObj(*this->menuPicture[i + 1]);
        }
        for (size_t i = indexCenter + 1; i <= buttonsCount; i++)
        {
            this->menuPicture[i]->putUnderObj(*this->menuPicture[i - 1]);
        }
    }
}
void menu::shineButton(sf::Color color)
{
    /*for (size_t i = 0; i <= this->buttons.GetCount(); i++)
    {
        if (this->buttons.GetElement(i)->data.trackContainsCursor())
            if (this->buttons.GetElement(i)->data.getColor() != color)
            {
                this->buttons.GetElement(i)->data.setColor(color);
            }
        if (!this->buttons.GetElement(i)->data.trackContainsCursor())
            if (this->buttons.GetElement(i)->data.getColor() == color)
            {
                this->buttons.GetElement(i)->data.setColor(color);
            }
    }*/
}

stackoverflow is asking for details, but I have no more details - sorry. stackoverflow is asking for details, but I have no more details - sorry. stackoverflow is asking for details, but I have no more details - sorry. stackoverflow is asking for details, but I have no more details - sorry. stackoverflow is asking for details, but I have no more details - sorry.

来源:https://stackoverflow.com/questions/59601459/sfml-with-pointers-image-with-vector-and-shared-ptr-not-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!