Saturday 15 January 2011

c++ - SFML - Rendering a character when the key is pressed? -


I have found this program that is supposed to be copied to the console (with a little coding help): < / P>

  #include & lt; Sfml / graphics HP & gt; # Include & lt; Sfml / system HP & gt; # Include & lt; Sfml / window.hpp> gt; SF :: Color Font Color; Sf :: font main fonts; Sf :: Clock myClock; Bool showCursor = true; Zero load font () {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 :: Vector 2U (1366, 768)); LoadFont (); Sf :: text myTxt; MyTxt.setColor (FONTCOLOR); MyTxt.setString ("System Module:"); MyTxt.setFont (mainFont); MyTxt.setCharacterSize (18); MyTxt.setStyle (SF :: text :: Regular); MyTxt.setPosition (0, 0); While (wnd.isOpen ()) {sf :: Event myEvent; While (wnd.pollEvent (myEvent)) {if (myEvent.type == sf :: event :: off} {wnd.close (); } If (myEvent.type == sf :: Event :: key is pressed) {if (myEvent.key.code == SF :: Keyboard :: Escape) {wnd.close (); }}} Wnd.clear (); If (myClock.getElapsedTime ()> gt; = sf :: milliseconds (500)) {myClock.restart (); ShowCursor =! Show cursor; If (showcursor == true) {myTxt.setString ("system module: _"); } And {myTxt.setString ("System Module:"); }} Wnd.draw (myTxt); Wnd.display (); }}   

I need to be able to type a key on the keyboard, and then present that key on the screen. I am thinking of using a std :: vector sf :: keyboard :: key , and to check the time that the key is ( Study :: Vector ) if without using a full set of details, but I do not know exactly How to settle so far, so I would like to know what is an easy way to fulfill my main goal? notes?

Thank you for your time, ~ Mike

SFL is a good Is the facility for, sf :: Event :: TextEntered (). That usually what you want and it avoids doing crazy things to explain the text you entered by the user. Enter your text by adding each letter to sf :: string (instead of std :: string ), it can behave better with SFLL's Unicode types, but It is not certain that a little bit of investigation will be required) which is the perfect type for sf :: Text :: setString !

Do not hesitate to see it, in this, each document sections page.

Example:

  sf :: string user input; // ... while (wnd.pollEvent (event)) {if (event.type == sf :: Event :: TextEntered) {/ * Select one of the following, and note that the insert method is more efficient Because it protects from creating a new string and then copies into user input * / // userInput + = event.text.unicode; UserInput.insert (userInput.getSize (), event.text.unicode); } Else if (event.type == sf :: Event :: KeyPressed) {if (event.key.code == SF :: Keyboard :: Backspace) / remove the last character {userInput.erase (userInput.getSize ( ) - 1); }}}    

No comments:

Post a Comment