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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | #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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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; } |