Set position Cursor Windows & Linux with same code

Asked

Viewed 179 times

1

I’m developing a cross-platform application.

For this I would like to develop with the minimum of "ifdefs" possible, and what I need to do is basically a fixed screen of 32 x 16 characters.

But for me to have performance, I need to just redesign what changes on the screen and not all of it all the time, and not keep giving system("cls"), every routine.

I searched and could not find any solution that is applicable both on Windows (Prompt) how much in the Linux (Terminal).

  • 2

    Your target platform has what CPU? Something coming up less than 20MHz? On text screen, unless you want to work with a few dozen FPS, it won’t do the right performance to redesign the text screen in C. (And it doesn’t make sense to have a text interface with more than 4 FPS)

2 answers

0

Well, as for the linux routine I don’t know how to inform, actually I don’t know if there is any native way to do this, because this is something related to the operating system, it’s a specific call where the language has to ask for "authorization" because both work differently.

It is easy to notice the difference between the terminal and the prompt in this question, because in the terminal when we type the "clear" command it actually prints a series of blank spaces and arrow the cursor to the beginning, however if we roll it up we can see clearly what has been done previously.

Already the prompt it actually does a buffer wipe, leaving the screen "new".

To inform a cursor coordinate in Windows, follows an easy function for this:

#include <stdio.h>
#include <windows.h>

void gotoxy( int x, int y )
{
    COORD coord;
    coord.X = (short)x;
    coord.Y = (short)y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

int main()
{
      gotoxy(5,5);
      printf("Hello");

      return 0;
}

In this code we will write the message "Hello" at the X and Y position of the windows prompt.

Now how much the screen cleansza, sorry but I believe only with the system("cls || clear"); to solve even.

-1

If you want a more sophisticated way of doing graphical interface on the terminal, there is the ncurses. Unfortunately it is not cross-platform. However, there is a cross-platform alternative to ncurses calling for pdcurses. I never used it, but it’s worth a look.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.