Domanda

I made a game that utilizes graphics.h. I use a while loop to create the screen output, so the x1 value in the below example decreases by 5 all the time. This means that I keep on creating all screens of the game all the time, except as the while loop continues, the player is only shown the part of the screen he is on. This is obviously very inefficient so I was wondering if anybody has tips of doing this in a more efficient way. I also have a more concrete problem... outtextxy doesn't allow a negative x value, meaning

outtextxy(-10,400,"text that is long enough that should be shown on screen even though it starts before screen beginning");

doesn't show up on screen, so my code is:

outtextxy(x1+301,400,"Use 'd' to duck");

It stops printing to screen right before that screen in the game is passed, the moment the x value becomes negative, rather than when the x value of the ending letter is passed, as I'd like it to.

È stato utile?

Soluzione

You need to clip the output. Wrap outtextxy like this (you may need to adjust the parameters and return value to match outtextxy). I am assuming that you are using a fixed-width font.

Clipping text in the x-coordinate would be something similar to this:

int clippedouttext(int x, int y, char *text) {
  const int CHAR_WIDTH = 8;  /* Replace 8 by actual character width */
  if (text == NULL || *text = '\0') { return 0; }  /* No text was submitted */
  while (x < 0) {
    x += CHAR_WIDTH;
    ++text;
    if (*text == `\0`) { return 0; }  /* The text is outside the viewport. */
  }
  return outtextxy(x, y, text);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top