所以我已经下载了SFML,到目前为止我很喜欢它。我遇到了一个障碍,我试图弄清楚如何实现一个闪烁光标到下面的代码。我还需要弄清楚如何在窗口上打印单个字符(当用户按键盘上的键时)。以下是我下载SFML 2.0后使用的一些代码:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <iostream>
int main() {
sf::RenderWindow wnd(sf::VideoMode(650, 300), "SFML Console");
sf::Vector2u myVector(650, 300);
wnd.setSize(myVector);
sf::Font myFont;
myFont.loadFromFile("theFont.ttf");
sf::Color myClr;
myClr.r = 0;
myClr.g = 203;
myClr.b = 0;
sf::String myStr = "Hello world!";
std::char myCursor = '_';
sf::Text myTxt;
myTxt.setColor(myClr);
myTxt.setString(myStr);
myTxt.setFont(myFont);
myTxt.setCharacterSize(12);
myTxt.setStyle(sf::Text::Regular);
myTxt.setPosition(0, 0);
std::int myCounter = 0;
while(wnd.isOpen()) {
sf::Event myEvent;
while (wnd.pollEvent(myEvent)) {
if (myEvent.type == sf::Event::Closed) {
wnd.close();
}
if (myEvent.type == sf::Event::KeyPressed) {
if (myEvent.key.code == sf::Keyboard::Escape) {
wnd.close();
}
}
wnd.clear();
wnd.draw(myTxt);
myCounter++;
std::if (myCounter >= 1000) {
myCounter = 0;
}
std::if (myCounter < 1000) {
myTxt.setString("Hello world!_");
}
wnd.display();
}
}
}
使用sf::Clock
(doc)。
在主循环之前声明时钟和其他变量,这也会自动启动时钟。在您的循环中,检查所经过的时间,如果超过您想要的时间,则重置时钟。例子:
sf::Clock myClock; // starts the clock
bool showCursor = false;
// ...
wnd.draw(myTxt);
if(clock.getElapsedTime() >= sf::milliseconds(500))
{
clock.restart();
showCursor = !showCursor;
if(showCursor)
myTxt.setString("Hello World!_");
else
myTxt.setString("Hello World!");
}
// ...
这将使光标每0.5秒闪烁一次。
顺便问一下,为什么使用std::if()
而不是语言中包含的普通if
?