Moverel Function In C

The moveRel function is frequently used in graphics programming to shift the current position of the object to a new location in a relative manner. The moveRel function syntax is as depicted below:

void moveRel(int dx, int dy);

In this instance, dx represents an adjustment on the x-axis, whereas dy indicates a modification on the y-axis. The purpose of this function is to determine the updated position relative to the current one based on these two parameters.

Let's consider a specific instance to showcase the functionality of the moverel function. For instance, below is a basic C program containing the moverel function, enabling us to shift the cursor within the console.

Example

#include <stdio.h>

#include <stdlib.h>

 

// Function to clear the screen

void clearScreen() {

    // Use "cls" for Windows and "clear" for Linux/Unix

    #ifdef _WIN32

        system("cls");

    #else

        system("clear");

    #endif

}

 

// Function to move the cursor to a specific position

void gotoxy(int x, int y) {

    printf("\033[%d;%dH", y + 1, x + 1);

}

 

// Function to move the cursor relative to its current position

void moveCursor(int *x, int *y, int dx, int dy) {

    int newX = *x + dx;

    int newY = *y + dy;

 

    // Ensure the cursor stays within the console boundaries (adjust as needed)

    if (newX >= 0 && newX < 80 && newY >= 0 && newY < 24) {

        *x = newX;

        *y = newY;

    }

}

 

int main() {

    // Display initial message

    printf("Use arrow keys to move the cursor. Press 'Q' to quit.\n");

 

    // Initial cursor position

    int x = 0, y = 0;

 

    while (1) {

        // Clear the screen

        clearScreen();

 

        // Display cursor at current position

        gotoxy(x, y);

        printf("*");

 

        // Capture user input

        char ch = getchar();

 

        // Move cursor based on user input

        switch (ch) {

            case 'Q':

            case 'q':

                // Exit the program if 'Q' is pressed

                return 0;

            case 27: // Escape key (for arrow keys)

                // Capture the '[' character that follows the Escape key

                getchar();

                switch (getchar()) {

                    case 'A': // Arrow key 'up'

                        moveCursor(&x, &y, 0, -1);

                        break;

                    case 'B': // Arrow key 'down'

                        moveCursor(&x, &y, 0, 1);

                        break;

                    case 'C': // Arrow key 'right'

                        moveCursor(&x, &y, 1, 0);

                        break;

                    case 'D': // Arrow key 'left'

                        moveCursor(&x, &y, -1, 0);

                        break;

                }

                break;

            default:

                // Do nothing for other keys

                break;

        }

    }

 

    return 0;

}

Output:

Output

Use arrow keys to move the cursor. Press 'Q' to quit.

*

Explanation of code:

Clear Screen Function:

The clearScreen function utilizes system-specific commands (cls for Windows, clear for Linux/Unix) to clear the console screen.

Cursor Positioning Function:

The gotoxy function positions the cursor at a specified coordinate on the console screen using escape sequences.

Move Cursor Function:

The moveCursor function is responsible for modifying the cursor's location by employing delta values (dx and dy) to ensure that the cursor remains within the boundaries of the console.

Main Function:

  • Shows an opening message explaining to the user how he should operate with this program.
  • Sets the cursor position (x and y) at the top-left corner of the console.
  • An infinite loop (while(1)) . For continuous user interaction.
  • Clear screen, display cursor at current position.
  • Captures user input using getchar.

Handles user input:

  • If 'Q' or 'q' is pressed, the program closes.
  • Pressing the Escape key (ASCII 27) makes your program capture the '[' character which follows, and interpret input from arrow keys.
  • 'A' moves the cursor up.
  • 'B' moves the cursor down.
  • 'C' moves the cursor right.
  • 'D' moves the cursor left.
  • Cursor movement is done by calling the moveCursor function with the current position of cursor and appropriate delta values.

Escape Sequences:

Escape sequences are employed to manipulate the position of the console cursor, providing a basic user interface functionality.

Console Clearing:

We empty the console screen at the start of every iteration to simulate the movement of a cursor without any residual traces.

Exiting the Program:

If the user presses the 'Q' or 'q' key, the program terminates the main loop and concludes its execution.

Conclusion:

Finally, the provided C code demonstrates the practical implementation of the moverel function from the <conio.h> library for cursor movement. The program's handling of user input, particularly the directional cues, grants users control over the console's mouse pointer. This interactive approach has the potential to enhance user engagement with console-driven applications. The code serves as a foundation, covering fundamental concepts such as screen refreshing, cursor positioning, and input handling. Proficiency in this area empowers developers to create dynamic and engaging console applications using the C programming language, facilitating the creation of interactive software with customizable elements.

Input Required

This code uses input(). Please provide values below: