Skip to content

Commit

Permalink
fixed getch to return null if there is no input
Browse files Browse the repository at this point in the history
  • Loading branch information
DeveloperGY committed Jan 11, 2023
1 parent 78181a8 commit 2cbe6ac
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 8 deletions.
Binary file modified bin/libtermDisplay.a
Binary file not shown.
Binary file modified bin/out
Binary file not shown.
Binary file modified obj/draw.o
Binary file not shown.
Binary file modified obj/helpers.o
Binary file not shown.
Binary file modified obj/initialization.o
Binary file not shown.
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main(void)
td_drawPoint(x, y, '@', TD_COLOR_CYAN, TD_COLOR_DEFAULT);
td_display();

ch = getch();
ch = getch(0);

if (ch == 'd')
{
Expand Down
30 changes: 26 additions & 4 deletions src/termDisplay/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,41 @@ void td_display()
return;
}

printf("\e[1;1H\e[2J"); // Clear the terminal using escape sequences
// printf("\e[1;1H\e[2J"); // Clear the terminal using escape sequences
printf("\e[H");

// change to create a string with a singular print call
char fg_color_str[3];
char bg_color_str[3];

size_t index = 0;

for (int i=0; i<TD_HEIGHT; i++)
{
for (int j=0; j<TD_WIDTH; j++)
{
printf("\x1b[%d;%dm%c\x1b[m", TD_FG_COLOR_BUFFER[i][j], TD_BG_COLOR_BUFFER[i][j], TD_CHAR_BUFFER[i][j]);
snprintf(fg_color_str, 3, "%d", TD_FG_COLOR_BUFFER[i][j]);
snprintf(bg_color_str, 3, "%d", TD_BG_COLOR_BUFFER[i][j]);

TD_PRINT_BUFFER[index++] = '\x1b';
TD_PRINT_BUFFER[index++] = '[';
TD_PRINT_BUFFER[index++] = fg_color_str[0];
TD_PRINT_BUFFER[index++] = fg_color_str[1];
TD_PRINT_BUFFER[index++] = ';';
TD_PRINT_BUFFER[index++] = bg_color_str[0];
TD_PRINT_BUFFER[index++] = bg_color_str[1];
TD_PRINT_BUFFER[index++] = 'm';
TD_PRINT_BUFFER[index++] = TD_CHAR_BUFFER[i][j];
TD_PRINT_BUFFER[index++] = '\x1b';
TD_PRINT_BUFFER[index++] = '[';
TD_PRINT_BUFFER[index++] = 'm';
}
printf("\n");
TD_PRINT_BUFFER[index++] = '\n';
}

TD_PRINT_BUFFER[index] = '\0';

printf("%s", TD_PRINT_BUFFER);

for (int i=0; i<TD_HEIGHT; i++)
{
for (int j=0; j<TD_WIDTH; j++)
Expand Down
27 changes: 26 additions & 1 deletion src/termDisplay/helpers.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "termDisplay.h"

char getch()
char basic_getch()
{
char ch;

Expand All @@ -15,5 +15,30 @@ char getch()
ch = (char)getchar();
tcsetattr(0, TCSANOW, &old);

return ch;
}

// returns NULL if there was no character
// max wait time is in milliseconds
char getch(unsigned int max_wait_time)
{
char ch = NULL;

fd_set rfds;
struct timeval tv;

FD_ZERO(&rfds);
FD_SET(0, &rfds);

tv.tv_sec = 0;
tv.tv_usec = max_wait_time *10;

int has_input = select(1, &rfds, NULL, NULL, &tv);

if (has_input == 1)
{
ch = basic_getch();
}

return ch;
}
74 changes: 74 additions & 0 deletions src/termDisplay/initialization.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ int TD_HEIGHT = 0;
char **TD_CHAR_BUFFER = NULL;
int **TD_FG_COLOR_BUFFER = NULL;
int **TD_BG_COLOR_BUFFER = NULL;
char *TD_PRINT_BUFFER = NULL;

struct termios old_settings;
struct termios new_settings;

/**
* @brief Initializes the character buffer if it hasn't already been initialized
Expand All @@ -27,6 +31,13 @@ int initializeFGColorBuffer();
*/
int initializeBGColorBuffer();

/**
* @brief Initializes the print buffer if it hasn't been already
*
* @return 1 on success 0 on failure
*/
int initializePrintBuffer();

/**
* @brief Uninitializes the character buffer if it hasn't been already
*
Expand All @@ -45,6 +56,12 @@ void terminateFGColorBuffer();
*/
void terminateBGColorBuffer();

/**
* @brief Uninitializes the print buffer if it hasn't been already
*
*/
void terminatePrintBuffer();

int td_initialize(int width, int height)
{
TD_WIDTH = width;
Expand Down Expand Up @@ -74,6 +91,24 @@ int td_initialize(int width, int height)
return 0;
}

if (!initializePrintBuffer())
{
terminateBGColorBuffer();
terminateFGColorBuffer();
terminateCharBuffer();
TD_WIDTH = 0;
TD_HEIGHT = 0;
return 0;
}

tcgetattr(0, &old_settings);
new_settings = old_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECHO;
tcsetattr(0, TCSANOW, &new_settings);

printf("\x1b[?25l\x1b[H\x1b[2J");

return 1;
}

Expand All @@ -82,9 +117,13 @@ void td_terminate()
terminateCharBuffer();
terminateFGColorBuffer();
terminateBGColorBuffer();
terminatePrintBuffer();

TD_WIDTH = 0;
TD_HEIGHT = 0;

printf("\x1b[?25h");
tcsetattr(0, TCSANOW, &old_settings);
return;
}

Expand Down Expand Up @@ -229,6 +268,30 @@ int initializeBGColorBuffer()
return 1;
}

int initializePrintBuffer()
{
if (!(TD_WIDTH && TD_HEIGHT)) // API not initialized
{
return 0;
}

if (TD_PRINT_BUFFER) // already initialized, if you want to change the size, terminate the current api first and re initalize it
{
return 0;
}

size_t size = (12 * TD_WIDTH * TD_HEIGHT) + TD_HEIGHT;

TD_PRINT_BUFFER = (char *)malloc(size);

if (!TD_PRINT_BUFFER) // malloc failed
{
return 0;
}

return 1;
}

void terminateCharBuffer()
{
if (!TD_CHAR_BUFFER) // Already deallocated
Expand Down Expand Up @@ -275,4 +338,15 @@ void terminateBGColorBuffer()

free(TD_BG_COLOR_BUFFER);
TD_BG_COLOR_BUFFER = NULL;
}

void terminatePrintBuffer()
{
if (!TD_PRINT_BUFFER) // Already deallocated
{
return;
}

free(TD_PRINT_BUFFER);
TD_PRINT_BUFFER = NULL;
}
24 changes: 22 additions & 2 deletions src/termDisplay/termDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string.h>

#include <termios.h>
#include <sys/select.h>

/**
* @brief Width of the buffers
Expand Down Expand Up @@ -36,6 +37,12 @@ extern int **TD_FG_COLOR_BUFFER;
*/
extern int **TD_BG_COLOR_BUFFER;

/**
* @brief Buffer to combine the color and character buffers
*
*/
extern char *TD_PRINT_BUFFER;

/**
* Colors
* Uses ansi escape sequences to display colors
Expand All @@ -56,6 +63,17 @@ extern int TD_COLOR_WHITE;
*/
extern int TD_FG_TO_BG_OFFSET;

/**
* @brief Saved terminal settings
*
*/
extern struct termios old_settings;

/**
* @brief TD terminal settings
*/
extern struct termios new_settings;

/**
* Initialization Functions
*
Expand Down Expand Up @@ -226,10 +244,12 @@ extern "C" {
* @brief An unbuffered character input function to remove the need
* to press enter to input a character
*
* @param max_wait_time the amount of time in milliseconds to wait for input, use zero if you dont want to wait
*
* @warning Linux only!!!
*
* @return returns the pressed character, or NULL if there is no pressed character or there is an error
*/
char getch();
char getch(unsigned int max_wait_time);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 2cbe6ac

Please sign in to comment.