SFML community forums

Help => General => Topic started by: maltman on April 10, 2019, 09:30:09 pm

Title: What is a good way to handle new line characters? \n
Post by: maltman on April 10, 2019, 09:30:09 pm
Hi, I'm making a console window with SFML and I've reached my first uncertainty: new line characters.

I want a new line character to create a new line. There seems to be no function in sf::Text to do this automatically.

So, with efficiency in mind, should I create a loop for each sub-string delineated by new line characters and call RenderWindow::Draw on each sub-string? or is there a better way.

https://github.com/FRex/LuaConsole/blob/master/src/LuaConsole/LuaSFMLConsoleView.cpp

^^This guy doesn't even use sf::Text. He creates his own vertex thingies using the glyphs from the font.

What is the recommended approach here?

Edit:
Here is what I have now(it functions but seems wasteful):

        {
                float lineSpacing = sessionText.getCharacterSize() + sessionText.getLineSpacing();
                int i{ 0 };
                for (std::string const & substring : model.get_sessionString())
                {
                        sf::Transform t;
                        t.translate(0.f, lineSpacing * i);
                        sessionText.setString(substring);
                        AppWindow::instance().getAppWindow().draw(sessionText, t);
                        i++;
                }
        }
Title: Re: What is a good way to handle new line characters? \n
Post by: FRex on April 10, 2019, 10:51:54 pm
sf::Text will handle \n properly and make a new line appear. You can see it in the code and while documentation on setString doesn't seem to say it there are functions like setLineSpacing which would make no sense to have if sf::Text was always a single line.

I did it 'myself' (my code is based on sf::Text's, there's a notice at line 83) to be able to color the glyps individually (while keeping them all in a single vertex array to draw all in a single draw call) to display errors in red, messages in white, evaluated expressions in grey, etc. as you can see on screenshots here: https://github.com/FRex/LuaConsoleProgram and on the ECONSOLE_COLOR enum list here: https://github.com/FRex/LuaConsole/blob/master/include/LuaConsole/LuaConsoleModel.hpp#L78


Title: Re: What is a good way to handle new line characters? \n
Post by: maltman on April 11, 2019, 01:48:27 am
Ahh! I see yes it does work with \n. I was expecting it to work with '\r' (carriage return) as well, since that is what I've been getting from event.text.unicode when I press enter.

I wrote a simple if statement to turn \r into \n and everything is working fine. I ditched the vector of substrings.

So all that stuff you wrote was just to have multi color text? That is pretty cool. I might have to steal it if I continue with this.
Title: Re: What is a good way to handle new line characters? \n
Post by: FRex on April 11, 2019, 02:57:32 am
There's not that much code there and almost all of it is a straight copy from sf::Text with minor changes. Other files have much more code (Model has like 5x code, Input and Completion each have as much as View) and it's all custom written too.
Title: Re: What is a good way to handle new line characters? \n
Post by: Hapax on April 13, 2019, 10:54:04 pm
Depending on your requirements, I will also suggest Selba Ward's Console Screen (https://github.com/Hapaxia/SelbaWard/wiki/Console-Screen) as another option.

It acts as just a target screen to modify as you see fit. It doesn't have any content understanding so cannot understand text, commands or any language. It is a console screen that represents only the data you require to display.
You can output to it in a very similar way to actual console displays: outputting strings, changing the foreground and background colours of each cell and moving a cursor around. You can also manipulate cell individually.

Here's a screenshot:
(https://i.imgur.com/Oe75aRo.png)

There's a couple of simple step-by-step tutorials to grasp the main basics of usage:
https://github.com/Hapaxia/SelbaWard/wiki/Tutorial:-'Console-Screen'-Basics
https://github.com/Hapaxia/SelbaWard/wiki/Tutorial:-'Console-Screen'-Colour
(https://i.imgur.com/qZEIATM.png)
Title: Re: What is a good way to handle new line characters? \n
Post by: maltman on April 25, 2019, 12:35:31 am
This is great thanks. I want to program my own because I need the practice. I'll definitely follow the Selba Tutorials.

Right now I am struggling to think how should I handle input into my window.

I currently am using a singleton for my AppWindow and I when I handle the sf events I set std::bitsets for each key press.
In my console.update I check the bitsets like this:
AppWindow::instance().get_IsKeyPressed().any()

I don't want to couple my console to my AppWindow singleton class so this sucks.

@FRex : I see that your ConsoleInput takes an sf::event. How and where do you pass the event to the function?
https://github.com/FRex/LuaConsole/blob/master/src/LuaConsole/LuaSFMLConsoleInput.cpp



auto const keybits = AppWindow::instance().get_IsKeyPressed();
        if (keybits.none())
        {
                return;
        }
        if (keybits[sf::Keyboard::Key::BackSpace])
        {
                std::cout << "backspace\n";
        }
        if (keybits[sf::Keyboard::Key::Delete])
        {
                std::cout << "delete\n";
        }
        if (keybits[sf::Keyboard::Key::Enter])
        {
                m_model.add_to_session(m_model.get_promptLine());
                m_model.reset_promptLine();
        }
 

I also have this nasty stuff.
Title: Re: What is a good way to handle new line characters? \n
Post by: FRex on April 25, 2019, 05:31:04 am
There's a demo program but it has a very unusual event loop: https://github.com/FRex/LuaConsoleProgram
Title: Re: What is a good way to handle new line characters? \n
Post by: maltman on April 26, 2019, 10:28:28 pm
Is it possible to add a cursor without making your own geometry? I couldn't figure it out using the standard sf:: classes.

I am going through your code and the sf::Text code and trying to learn for my own project, I may have other questions about it I hope you don't mind.
Title: Re: What is a good way to handle new line characters? \n
Post by: FRex on April 26, 2019, 11:14:14 pm
Quote
Is it possible to add a cursor without making your own geometry? I couldn't figure it out using the standard sf:: classes.
I don't know what you mean by 'without own geometry'. You have to draw a cursor yourself somehow if you want one.

Quote
I am going through your code and the sf::Text code and trying to learn for my own project, I may have other questions about it I hope you don't mind.
Feel free. I'll try answer.