Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: What is a good way to handle new line characters? \n  (Read 1386 times)

0 Members and 1 Guest are viewing this topic.

maltman

  • Newbie
  • *
  • Posts: 5
    • View Profile
What is a good way to handle new line characters? \n
« 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++;
                }
        }
« Last Edit: April 10, 2019, 09:38:11 pm by maltman »

FRex

  • Hero Member
  • *****
  • Posts: 1843
    • View Profile
    • My GitHub Page
    • Email
Re: What is a good way to handle new line characters? \n
« Reply #1 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


« Last Edit: April 10, 2019, 10:59:24 pm by FRex »
ZipSavings, script to count rar/7z/zip savings: https://goo.gl/vvBj5M
LuaConsole: https://goo.gl/X4kRUk
FoxRaycaster: https://goo.gl/27nVS8
Small Games - Heart, Routing and Snek: https://goo.gl/15ZGWE https://goo.gl/k5gwWD https://goo.gl/4nKPnT
Botes - a notes app in Pascal: https://goo.gl/bzTqsi

maltman

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: What is a good way to handle new line characters? \n
« Reply #2 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.

FRex

  • Hero Member
  • *****
  • Posts: 1843
    • View Profile
    • My GitHub Page
    • Email
Re: What is a good way to handle new line characters? \n
« Reply #3 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.
ZipSavings, script to count rar/7z/zip savings: https://goo.gl/vvBj5M
LuaConsole: https://goo.gl/X4kRUk
FoxRaycaster: https://goo.gl/27nVS8
Small Games - Heart, Routing and Snek: https://goo.gl/15ZGWE https://goo.gl/k5gwWD https://goo.gl/4nKPnT
Botes - a notes app in Pascal: https://goo.gl/bzTqsi

Hapax

  • Hero Member
  • *****
  • Posts: 3040
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: What is a good way to handle new line characters? \n
« Reply #4 on: April 13, 2019, 10:54:04 pm »
Depending on your requirements, I will also suggest Selba Ward's 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:


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
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

maltman

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: What is a good way to handle new line characters? \n
« Reply #5 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.
« Last Edit: April 25, 2019, 12:48:32 am by maltman »

FRex

  • Hero Member
  • *****
  • Posts: 1843
    • View Profile
    • My GitHub Page
    • Email
Re: What is a good way to handle new line characters? \n
« Reply #6 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
ZipSavings, script to count rar/7z/zip savings: https://goo.gl/vvBj5M
LuaConsole: https://goo.gl/X4kRUk
FoxRaycaster: https://goo.gl/27nVS8
Small Games - Heart, Routing and Snek: https://goo.gl/15ZGWE https://goo.gl/k5gwWD https://goo.gl/4nKPnT
Botes - a notes app in Pascal: https://goo.gl/bzTqsi

maltman

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: What is a good way to handle new line characters? \n
« Reply #7 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.

FRex

  • Hero Member
  • *****
  • Posts: 1843
    • View Profile
    • My GitHub Page
    • Email
Re: What is a good way to handle new line characters? \n
« Reply #8 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.
ZipSavings, script to count rar/7z/zip savings: https://goo.gl/vvBj5M
LuaConsole: https://goo.gl/X4kRUk
FoxRaycaster: https://goo.gl/27nVS8
Small Games - Heart, Routing and Snek: https://goo.gl/15ZGWE https://goo.gl/k5gwWD https://goo.gl/4nKPnT
Botes - a notes app in Pascal: https://goo.gl/bzTqsi

 

anything