1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
sf::Color fontColor;
sf::Font mainFont;
sf::Clock myClock;
bool showCursor = false;
bool newLine = false;
void LoadFont() {
mainFont.loadFromFile("dos.ttf");
fontColor.r = 0;
fontColor.g = 203;
fontColor.b = 0;
}
int main() {
sf::RenderWindow wnd(sf::VideoMode(1366, 768), "SFML Console");
wnd.setSize(sf::Vector2u(1366, 768));
LoadFont();
sf::Text myChar;
myChar.setColor(fontColor);
myChar.setFont(mainFont);
myChar.setCharacterSize(18);
myChar.setStyle(sf::Text::Regular);
sf::String mainString = "System$~> ";
sf::Text myTxt;
myTxt.setColor(fontColor);
myTxt.setString(mainString);
myTxt.setFont(mainFont);
myTxt.setCharacterSize(18);
myTxt.setStyle(sf::Text::Regular);
myTxt.setPosition(0, 0);
sf::String cursor = "_";
sf::Text myCursor;
myCursor.setColor(fontColor);
myCursor.setFont(mainFont);
myCursor.setCharacterSize(18);
myCursor.setStyle(sf::Text::Regular);
myCursor.setPosition(myTxt.getGlobalBounds().width + 1, 0);
sf::String bText;
sf::Text buffer;
buffer.setColor(fontColor);
buffer.setFont(mainFont);
buffer.setCharacterSize(18);
buffer.setStyle(sf::Text::Regular);
buffer.setPosition(myTxt.getGlobalBounds().width + 1, 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();
}
if (myEvent.key.code == sf::Keyboard::BackSpace) {
if (!bText.isEmpty()) {
bText.erase(bText.getSize(), 1);
}
}
if (myEvent.key.code == sf::Keyboard::Return) {
newLine = true;
}
}
if (myEvent.type == sf::Event::TextEntered) {
if (myEvent.text.unicode < 128) {
bText.insert(bText.getSize(), myEvent.text.unicode);
buffer.setString(bText);
}
}
}
if (newLine == true) {
myTxt.setPosition(0, myTxt.getGlobalBounds().height + 1);
bText.clear();
buffer.setString(bText);
buffer.setPosition((myTxt.getGlobalBounds().width + myCursor.getGlobalBounds().width) + 1, myTxt.getGlobalBounds().height + 1);
newLine = false;
}
wnd.draw(myTxt);
wnd.draw(buffer);
if (myClock.getElapsedTime() >= sf::milliseconds(500)) {
myClock.restart();
showCursor = !showCursor;
}
if(showCursor == true) {
cursor.insert(cursor.getSize(), "_");
myCursor.setPosition((myTxt.getGlobalBounds().width + buffer.getGlobalBounds().width) + 1, 0);
wnd.draw(myCursor);
} else {
cursor.erase(cursor.getSize(), 1);
}
wnd.display();
}
}
|