Strange Font & Math Problems, Basic Math would Probably Help

Hello, first time posting sorry if this is vauge, just needing some help with what I thought should be basic math.
For random reasons I felt making a sandbox version of a terminal window to help teach someone more powerful commands without damaging anything (mostly their concern). And I’ve gotten the vast majority working well (not linked here) there seems to be a strange bug as more text draws on the screen. Once lineCount*20 (Font size is 20) = 140) the text starts to print over each other. Any idea why this is happening or how to fix it? {My code is Below}


String result="";
String stack="";
String dir="super-user@ip~:";
int lineCount;
//PFont cmd;
void setup() {
  size(600, 400);
  lineCount = 2;
  //cmd = loadFont("Consolas-20.vlw");
  //textFont(cmd);
  textSize(20);//this is changed to help others view the code without having to download anything extra
  
}

void draw() {
  background(0);
  text(stack, 0, 10);
  text(dir+result, 0, lineCount*20);  //this works until ==140 then the text starts to overlap
                                     //and once we get past 340 the problem becomes horrible
  println(lineCount*20);//for debugging purposes
}

void keyPressed() {
 if (key==BACKSPACE && result.length() > 0) {
   result = result.substring(0, result.length()-1);
 } else if (key== ENTER || key == RETURN) {
   stack += ENTER+dir+result;
   lineCount++;
   result = "";
 } else if (key != SHIFT) {
  result = result+key; 
 }
}

The reason the program displays the text incorrectly is that lineCount*20 only leaves 20 pixels of room between the texts, and you have to take into account the space between the texts, not just how tall the text is.

You can use textLeading(); (reference) to change the space between the text.

Just to make it easier, I suggest you use a StringList (reference) to store your input values.

Instead of using stack, when enter or return is pressed, use <StringList>.append(result); to add the value. Then you can loop through the StringList, and display the text. (It can get a bit messy to store all the values in 1 String, plus you might find it helpful later on)

2 Likes

Here’s some code of how I would do it:

String result="";
String stack="";
String dir="super-user@ip~:";
int lineCount;

int TEXT_GAP = 25; // Adjust the gap between the text

//PFont cmd;
void setup() {
  size(600, 400);
  lineCount = 2;
  //cmd = loadFont("Consolas-20.vlw");
  //textFont(cmd);
  textSize(20);//this is changed to help others view the code without having to download anything extra
}

void draw() {
  background(0);
  textLeading(TEXT_GAP);
  text(stack, 0, TEXT_GAP);
  text(dir+result, 0, lineCount*TEXT_GAP);  //this works until ==140 then the text starts to overlap
  //and once we get past 340 the problem becomes horrible
  println(lineCount);//for debugging purposes
}

void keyPressed() {
  if (key==BACKSPACE && result.length() > 0) {
    result = result.substring(0, result.length()-1);
  } else if (key== ENTER || key == RETURN) {
    stack += "\n"+dir+result;
    lineCount++;
    result = "";
  } else if (key != SHIFT) {
    result = result+key;
  }
}
1 Like

Thank you so much, this code works perfectly and really clears up some messy code I have later on.
I appreciate your time and can’t believe I was missing something like that, just the space between the font, you’re rad!