Files
ConsoleMenu/console_functions.c
2025-03-24 19:11:24 -05:00

179 lines
6.6 KiB
C

//
// Created by tony on 11/27/2024.
//
#include "console_function.h"
// Function to clear the console
void clearConsole()
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coordScreen = {0, 0};
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
// Get the console screen buffer info
if (!GetConsoleScreenBufferInfo(hConsole, &csbi))
{
return;
}
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
// Fill the console with spaces
FillConsoleOutputCharacter(hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten);
// Reset the cursor to the top left
SetConsoleCursorPosition(hConsole, coordScreen);
}
void enableANSI()
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode = 0;
// Get the current console mode
if (GetConsoleMode(hOut, &dwMode))
{
// Enable the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag
SetConsoleMode(hOut, dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
}
}
// Function to set cursor position
void gotoxy(int x, int y)
{
COORD coord;
coord.X = (short)x;
coord.Y = (short)y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
bool updateConsole(struct Entity *pEntity)
{
HANDLE lhConsole = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD pCharsWritten;
Entity *toScreen = pEntity;
/* if (!WriteConsoleOutputCharacter(lhConsole,
toScreen.avatar,
1,
toScreen.position,
&pCharsWritten)) {
printf("Error: Unable to write to console. Error code: %ld\n", GetLastError());
return 0;
}*/
return 1;
}
void getBufferInfo(HANDLE *hHandle, CONSOLE_SCREEN_BUFFER_INFO *pCSBI)
{
GetConsoleScreenBufferInfo(hHandle, pCSBI);
}
void printList(HANDLE *pHConsole, DWORD *pCharsWritten, Entity *pE)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
//COORD coordScreen = {0, 0};
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConsole, &csbi);
HANDLE *bhConsole;
DWORD *bCharsWritten;
for (int counter = 0; counter < 5 ;counter++)
{
Entity bE = pE[counter];
WriteConsoleOutputCharacter(pHConsole,
(LPCSTR) pE[counter].avatar,
1,
pE[counter].position,
pCharsWritten);
/*if (!WriteConsoleOutputCharacter(pHConsole,
&bE.avatar,
1,
bE.position,
pCharsWritten)) {
// // printf("Error: Unable to write to console. Error code: %ld\n", GetLastError());
}*/
// printf("Avatar :%s",&e->avatar);
//e = (Entity *) e->next;
}
}
void printBoundsToConsole(HANDLE *pHConsole,
/*DWORD *pCharsWritten,*/
SMALL_RECT *pScreenBounds)
{
char strLeft[12]; // Buffer to hold the integer as a string
char strTop[12]; // Buffer to hold the integer as a string
char strRight[12]; // Buffer to hold the integer as a string
char strBottom[12]; // Buffer to hold the integer as a string
snprintf(strLeft, sizeof(strLeft), "%d", pScreenBounds->Left);
snprintf(strTop, sizeof(strTop), "%d", pScreenBounds->Top);
snprintf(strRight, sizeof(strRight), "%d", pScreenBounds->Right);
snprintf(strBottom, sizeof(strBottom), "%d", pScreenBounds->Bottom);
size_t lLeft = strlen(strLeft);
size_t lTop = strlen(strTop);
size_t lRight = strlen(strRight);
size_t lBottom = strlen(strBottom);
CHAR_INFO bLeft[12]; // Same size as strBuffer to avoid overflow
CHAR_INFO bTop[12]; // Same size as strBuffer to avoid overflow
CHAR_INFO bRight[12]; // Same size as strBuffer to avoid overflow
CHAR_INFO bBottom[12]; // Same size as strBuffer to avoid overflow
for (size_t i = 0; i < lLeft; i++)
{
bLeft[i].Char.AsciiChar = strLeft[i]; // Assign character
bLeft[i].Attributes = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY; // Yellow text
}
for (size_t i = 0; i < lTop; i++)
{
bTop[i].Char.AsciiChar = strTop[i]; // Assign character
bTop[i].Attributes = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY; // Yellow text
}
for (size_t i = 0; i < lRight; i++)
{
bRight[i].Char.AsciiChar = strRight[i]; // Assign character
bRight[i].Attributes = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY; // Yellow text
}
for (size_t i = 0; i < lBottom; i++)
{
bBottom[i].Char.AsciiChar = strBottom[i]; // Assign character
bBottom[i].Attributes = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY; // Yellow text
}
SMALL_RECT rLeft = {0, 0, (SHORT)(lLeft - 1), 0}; // Start at top-left corner, width of the string
SMALL_RECT rTop = {0, 1, (SHORT)(lTop - 1), 0}; // Start at top-left corner, width of the string
SMALL_RECT rRight = {0, 2, (SHORT)(lRight - 1), 0}; // Start at top-left corner, width of the string
SMALL_RECT rBottom = {0, 3, (SHORT)(lBottom - 1), 0}; // Start at top-left corner, width of the string
COORD sLeft = {(SHORT)lLeft, 1}; // Width is the length of the string, height is 1
COORD sTop = {(SHORT)lTop, 1}; // Width is the length of the string, height is 1
COORD sRight = {(SHORT)lRight, 1}; // Width is the length of the string, height is 1
COORD sBottom = {(SHORT)lBottom, 1}; // Width is the length of the string, height is 1
COORD cLeft = {0, 0}; // Starting point in the CHAR_INFO buffer
COORD cTop = {0, 1}; // Starting point in the CHAR_INFO buffer
COORD cRight = {0, 2}; // Starting point in the CHAR_INFO buffer
COORD cBottom = {0, 3}; // Starting point in the CHAR_INFO buffer
WriteConsoleOutput(pHConsole,
bLeft,
sLeft,
cLeft,
&rLeft);
WriteConsoleOutput(pHConsole,
bTop,
sTop,
cTop,
&rTop);
WriteConsoleOutput(pHConsole,
bRight,
sRight,
cRight,
&rRight);
WriteConsoleOutput(pHConsole,
bBottom,
sBottom,
cBottom,
&rBottom);
}