Customizable Information Box Library in C

Customizable Information Box Library in C

This C code provides a simple and customizable implementation for creating text boxes that can be used to display tips, hints, or information in a game or application. The code is designed to be beginner-friendly and easy to integrate into C programs.

You can create customizable text boxes by defining their position, size, title, and text content. The title and text will be centered within the specified width, providing a neat and organized appearance. The code utilizes dynamic memory allocation to create and free memory for drawing the box borders. This ensures efficient memory usage.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Constants for title and text length
#define TITLE_LENGTH 20
#define TEXT_LENGTH 20

// Function prototypes
void make_textbox(struct textbox box);
void draw_box(int left, int top, int width, int height);

// Structure definition for a textbox
struct textbox
{
    int left;
    int top;
    int width;
    int height;
    char title[TITLE_LENGTH + 30];
    char text[TEXT_LENGTH + 30];
};

int main(void)
{
    // Initializing textbox structures
    struct textbox aboutbox = { 10, 3, 62, 15, "About Textbox Program CopyRight (c) 2020 by mycplus.com." };
    struct textbox errorbox = { 20, 7, 40, 9, "Warning Press Esc to Exit!" };

    // Clearing the screen with black background
    printf("\033[2J");

    // Displaying the aboutbox
    make_textbox(aboutbox);
    getchar();

    // Clearing the screen with black background
    printf("\033[2J");

    // Displaying the errorbox
    make_textbox(errorbox);
    getchar();

    return 0;
}

// Function to create a textbox on the screen
void make_textbox(struct textbox box)
{
    // Setting text color and background color
    printf("\033[0;37;44m"); // White text on blue background

    // Drawing the box with title and text
    draw_box(box.left, box.top, box.width, box.height);

    // Placing title at the center
    printf("\033[%d;%dH", box.top, box.left + (box.width - strlen(box.title)) / 2 - 1);
    printf("%s", box.title);

    // Placing text at the center vertically
    printf("\033[%d;%dH", box.top + (box.height - 1) / 2, box.left + (box.width - strlen(box.text)) / 2 - 1);
    printf("%s", box.text);
}

// Function to draw a box on the screen
void draw_box(int left, int top, int width, int height)
{
    int j;
    char* string = malloc((width + 1) * sizeof(char));

    if (string == NULL)
    {
        fprintf(stderr, "Memory allocation failed\n");
        exit(1); // Use 1 as the error code
    }

    // Drawing the horizontal borders
    for (j = 1; j < width - 1; j++)
        string[j] = ' ';
    string[0] = '+';
    string[width - 1] = '+';
    string[width] = '\0';

    printf("\033[%d;%dH", top, left);
    printf("%s", string);

    printf("\033[%d;%dH", top + height - 1, left);
    printf("%s", string);

    // Drawing the vertical borders
    for (j = 2; j < height; j++)
    {
        printf("\033[%d;%dH", top + j - 1, left);
        printf("+");

        printf("\033[%d;%dH", top + j - 1, left + width - 1);
        printf("+");
    }

    free(string);
}

How to use this program in your own code?

First, create instances of the struct textbox to define the characteristics of your information boxes. Adjust the position, size, title, and text content accordingly. Clear the screen for a clean presentation. This is achieved using the ANSI escape code printf("\033[2J");. Call the make_textbox function by passing the desired struct textbox instance. This will draw the specified text box on the screen. After displaying the information box, use getchar() to wait for user input before proceeding. This can be useful for showing tips or hints that the user can dismiss when ready.

int main(void)
{
    // Initialize textbox structures
    struct textbox aboutbox = {10, 3, 62, 15, "About Textbox Program CopyRight (c) 1999 by Muhammad Saqib."};
    struct textbox errorbox = {20, 7, 40, 9, "Warning Press Esc to Exit!"};

    // Clear the screen with a black background
    printf("\033[2J");

    // Display the aboutbox
    make_textbox(aboutbox);
    getchar();

    // Clear the screen with a black background
    printf("\033[2J");

    // Display the errorbox
    make_textbox(errorbox);
    getchar();

    return 0;
}
M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post