使sfml c ++滚动条不会在视图内绘制形状

making sfml c++ scroll bar won't draw shapes inside a veiw

所以我正在尝试使用 sfml 制作一个基本的图形用户界面,并且需要一个滚动条来滚动可绘制对象的循环。经过研究,我了解到视图操作将是实现它的方法。我有一个矩形作为轮廓,我试图将其用作视图并设置了栏本身,我只需要它来更改视图。我的问题是我没有在视图中绘制任何内容。如果它不会在视图中绘制任何内容,则很难滚动它。感谢所有帮助。

我的妄想

    projectsBox = sf::RectangleShape(sf::Vector2f((400 * scale) - (2 * (10 * scale)), 225 * scale));
    projectsBox.setOrigin(sf::Vector2f(-10 * scale, -130 * scale));
    projectsBox.setOutlineColor(sf::Color::Black);
    projectsBox.setOutlineThickness(10 * scale);
    projectsBox.setFillColor(sf::Color::Transparent);
    projectsVeiw.setViewport(projectsBox.getGlobalBounds());

void sfmlWindow::drawProjects() {
    sf::CircleShape base;
    base.setRadius(30);
    base.setScale(0, userProjects->projects[1].name.length());
    base.setFillColor(sf::Color::Blue/*sf::Color(33, 33, 33, 270)*/);
    base.setOrigin(0, 0);
    sf::Text text;
    textInit(&text, userProjects->projects[1].name, 96);
    text.setOrigin(0, 0);
    projectsDrawables.emplace_back(std::make_unique<sf::CircleShape>(base));
    projectsDrawables.emplace_back(std::make_unique<sf::Text>(text));
}

我的循环

        while (mainWindow.isOpen()) {
            eventLoop();

            mainWindow.clear(sf::Color::Transparent);

            //Do project veiw
            mainWindow.setView(projectsVeiw);
            drawProjects();
            for (std::unique_ptr<sf::Drawable>& i : projectsDrawables) {
                mainWindow.draw(*i);
            }

            //draw drawbles
            mainWindow.setView(mainWindow.getDefaultView());
            for (std::unique_ptr<sf::Drawable>& i : allDrawables) {
                mainWindow.draw(*i);
            }
            mainWindow.draw(projectsBox);
            projectsSlider.setSize(sf::Vector2f(15, slidersize()));
            mainWindow.draw(projectsSlider);

            //render
            mainWindow.display();
        }

我已经将您的代码改编为可以编译并实际在屏幕上绘制内容的代码。这不是您想要的外观,而是通过两个视图显示绘图。你应该能够尝试这个来取得进步。

projectDrawables 在您的世界中位于 1000,1000 左右,而 allDrawables 位于 0,0 左右。要将 projectBox 缩小到角落,您应该增加 projectView 的大小。

#include <iostream>
#include <SFML/Graphics.hpp>

const int BOARD_SIZE = 40;
const float TILE_SIZE = 20.0f;

std::vector<std::unique_ptr<sf::Drawable> > projectsDrawables;
std::vector<std::unique_ptr<sf::Drawable> > allDrawables;

sf::Font font;

void drawProjects() {
   sf::CircleShape base;
   base.setRadius(30);
   base.setFillColor(sf::Color::Blue);
   base.setPosition(1000, 1000);

   sf::Text text;
   text.setFont(font);
   text.setFillColor(sf::Color::White);
   text.setString("hello");
   text.setCharacterSize(96);
   text.setOrigin(1100, 1000);

   projectsDrawables.emplace_back(std::make_unique<sf::Text>(text));
   projectsDrawables.emplace_back(std::make_unique<sf::CircleShape>(base));


   sf::CircleShape base2;
   base2.setRadius(50);
   base2.setFillColor(sf::Color::Red);
   base2.setPosition(30, 80);

   sf::Text text2;
   text2.setFont(font);
   text2.setFillColor(sf::Color::Green);
   text2.setString("Cheese");
   text2.setCharacterSize(48);
   text2.setPosition(30, 30);

   allDrawables.emplace_back(std::make_unique<sf::Text>(text2));
   allDrawables.emplace_back(std::make_unique<sf::CircleShape>(base2));
}


int main()
{
   sf::RenderWindow mainWindow(sf::VideoMode((2+BOARD_SIZE) * (int)TILE_SIZE, (2+BOARD_SIZE) * (int)TILE_SIZE), "Snake");

   if (!font.loadFromFile("Instruction.ttf") ) {
      std::cerr << "Font error." << std::endl;
      exit( -1 );
   }
   sf::Clock clock;

   auto scale = 1;
   auto projectsBox = sf::RectangleShape(sf::Vector2f(400, 225));
   projectsBox.setPosition(1000,1000);
   projectsBox.setOutlineColor(sf::Color::Green);
   projectsBox.setOutlineThickness(10 * scale);
   projectsBox.setFillColor(sf::Color::Transparent);
   sf::View projectsView( projectsBox.getGlobalBounds());

   while (mainWindow.isOpen()) {
      sf::Time elapsed = clock.getElapsedTime();
      if (elapsed.asSeconds() > 0.2f) {
         clock.restart();
      }

      sf::Event event;
      while (mainWindow.pollEvent(event)) {
         if (event.type == sf::Event::Closed)
            mainWindow.close();
         // Respond to key pressed events
         if (event.type == sf::Event::KeyPressed){
            if (event.key.code == sf::Keyboard::Escape){
               return 0;
            }
         }
      }

      mainWindow.clear(sf::Color::Black);

      mainWindow.setView(projectsView);
      mainWindow.draw(projectsBox);

      //Do project veiw
      drawProjects();
      for (std::unique_ptr<sf::Drawable>& i : projectsDrawables) {
         mainWindow.draw(*i);
      }

      //draw drawbles
      mainWindow.setView(mainWindow.getDefaultView());
      for (std::unique_ptr<sf::Drawable>& i : allDrawables) {
         mainWindow.draw(*i);
      }
      //projectsSlider.setSize(sf::Vector2f(15, slidersize()));
      //mainWindow.draw(projectsSlider);

      //render
      mainWindow.display();


   }

   return 0;
}